From d3f399aa208b9f25d083ab6f98b459807a289114 Mon Sep 17 00:00:00 2001 From: niansa Date: Thu, 7 Dec 2023 01:31:19 +0100 Subject: [PATCH] Cleanup and translation to english --- .gitmodules | 3 - CMakeLists.txt | 25 +- LuaBridge3/.clang-format | 73 - LuaBridge3/.coveralls.yml | 1 - LuaBridge3/.gitattributes | 1 - LuaBridge3/.github/workflows/amalgamate.yml | 30 - LuaBridge3/.github/workflows/build_linux.yml | 62 - LuaBridge3/.github/workflows/build_macos.yml | 62 - .../.github/workflows/build_windows.yml | 72 - .../.github/workflows/codeql-analysis.yml | 51 - LuaBridge3/.github/workflows/coverage.yml | 32 - LuaBridge3/.gitignore | 14 - LuaBridge3/.gitmodules | 6 - LuaBridge3/CHANGES.md | 160 - LuaBridge3/CMakeLists.txt | 34 - LuaBridge3/Distribution/LuaBridge/LuaBridge.h | 9746 ------------- LuaBridge3/Doxyfile | 326 - LuaBridge3/LICENSE.txt | 27 - LuaBridge3/Manual.md | 1840 --- LuaBridge3/README.md | 166 - LuaBridge3/Source/CMakeLists.txt | 51 - LuaBridge3/Source/LuaBridge/Array.h | 84 - LuaBridge3/Source/LuaBridge/List.h | 81 - LuaBridge3/Source/LuaBridge/LuaBridge.h | 36 - LuaBridge3/Source/LuaBridge/Map.h | 86 - .../Source/LuaBridge/RefCountedObject.h | 372 - LuaBridge3/Source/LuaBridge/Set.h | 80 - LuaBridge3/Source/LuaBridge/UnorderedMap.h | 86 - LuaBridge3/Source/LuaBridge/Vector.h | 81 - .../Source/LuaBridge/detail/CFunctions.h | 1178 -- .../Source/LuaBridge/detail/ClassInfo.h | 183 - LuaBridge3/Source/LuaBridge/detail/Config.h | 53 - LuaBridge3/Source/LuaBridge/detail/Dump.h | 120 - LuaBridge3/Source/LuaBridge/detail/Errors.h | 108 - LuaBridge3/Source/LuaBridge/detail/Expected.h | 1594 -- .../Source/LuaBridge/detail/FuncTraits.h | 508 - LuaBridge3/Source/LuaBridge/detail/Invoke.h | 228 - LuaBridge3/Source/LuaBridge/detail/Iterator.h | 206 - .../Source/LuaBridge/detail/LuaException.h | 187 - .../Source/LuaBridge/detail/LuaHelpers.h | 581 - LuaBridge3/Source/LuaBridge/detail/LuaRef.h | 1405 -- .../Source/LuaBridge/detail/Namespace.h | 1956 --- LuaBridge3/Source/LuaBridge/detail/Overload.h | 65 - LuaBridge3/Source/LuaBridge/detail/Result.h | 146 - .../Source/LuaBridge/detail/ScopeGuard.h | 47 - LuaBridge3/Source/LuaBridge/detail/Security.h | 93 - LuaBridge3/Source/LuaBridge/detail/Stack.h | 1471 -- .../Source/LuaBridge/detail/TypeTraits.h | 104 - LuaBridge3/Source/LuaBridge/detail/Userdata.h | 1010 -- LuaBridge3/Tests/CMakeLists.txt | 293 - LuaBridge3/Tests/Lua/Lua.5.1.5/COPYRIGHT | 34 - LuaBridge3/Tests/Lua/Lua.5.1.5/HISTORY | 183 - LuaBridge3/Tests/Lua/Lua.5.1.5/INSTALL | 99 - LuaBridge3/Tests/Lua/Lua.5.1.5/README | 37 - .../Tests/Lua/Lua.5.1.5/doc/contents.html | 497 - LuaBridge3/Tests/Lua/Lua.5.1.5/doc/cover.png | Bin 3305 -> 0 bytes LuaBridge3/Tests/Lua/Lua.5.1.5/doc/logo.gif | Bin 4232 -> 0 bytes LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.1 | 163 - LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.css | 83 - LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.html | 172 - LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.1 | 136 - LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.html | 145 - LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.css | 24 - .../Tests/Lua/Lua.5.1.5/doc/manual.html | 8804 ------------ .../Tests/Lua/Lua.5.1.5/doc/readme.html | 40 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/README | 37 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/all.c | 38 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.hpp | 9 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.ico | Bin 1078 -> 0 bytes LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.pc | 31 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/luavs.bat | 28 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/min.c | 39 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/noparser.c | 50 - LuaBridge3/Tests/Lua/Lua.5.1.5/etc/strict.lua | 41 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.c | 1087 -- LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.h | 16 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.c | 652 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.h | 174 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lbaselib.c | 653 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.c | 831 -- LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.h | 76 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldblib.c | 398 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.c | 638 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.h | 33 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.c | 519 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.h | 57 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldump.c | 164 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.c | 174 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.h | 34 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.c | 710 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.h | 110 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/linit.c | 38 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/liolib.c | 556 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.c | 463 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.h | 81 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/llimits.h | 128 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmathlib.c | 263 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.c | 86 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.h | 49 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/loadlib.c | 666 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.c | 214 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.h | 381 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.c | 102 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.h | 268 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/loslib.c | 243 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.c | 1339 -- LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.h | 82 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.c | 214 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.h | 169 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.c | 111 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.h | 31 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstrlib.c | 871 -- LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.c | 588 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.h | 40 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltablib.c | 287 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.c | 75 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.h | 54 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.c | 392 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.h | 388 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/luac.c | 200 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/luaconf.h | 763 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lualib.h | 53 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.c | 227 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.h | 36 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.c | 767 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.h | 36 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.c | 82 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.h | 67 - LuaBridge3/Tests/Lua/Lua.5.1.5/src/print.c | 227 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/README | 26 - .../Tests/Lua/Lua.5.1.5/test/bisect.lua | 27 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/cf.lua | 16 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/echo.lua | 5 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/env.lua | 7 - .../Tests/Lua/Lua.5.1.5/test/factorial.lua | 32 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/fib.lua | 40 - .../Tests/Lua/Lua.5.1.5/test/fibfor.lua | 13 - .../Tests/Lua/Lua.5.1.5/test/globals.lua | 13 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/hello.lua | 3 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/life.lua | 111 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/luac.lua | 7 - .../Tests/Lua/Lua.5.1.5/test/printf.lua | 7 - .../Tests/Lua/Lua.5.1.5/test/readonly.lua | 12 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/sieve.lua | 29 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/sort.lua | 66 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/table.lua | 12 - .../Tests/Lua/Lua.5.1.5/test/trace-calls.lua | 32 - .../Lua/Lua.5.1.5/test/trace-globals.lua | 38 - LuaBridge3/Tests/Lua/Lua.5.1.5/test/xd.lua | 14 - LuaBridge3/Tests/Lua/Lua.5.2.4/README | 6 - .../Tests/Lua/Lua.5.2.4/doc/contents.html | 608 - LuaBridge3/Tests/Lua/Lua.5.2.4/doc/logo.gif | Bin 4232 -> 0 bytes LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.1 | 116 - LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.css | 106 - LuaBridge3/Tests/Lua/Lua.5.2.4/doc/luac.1 | 118 - LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.css | 27 - .../Tests/Lua/Lua.5.2.4/doc/manual.html | 10508 -------------- .../Lua/Lua.5.2.4/doc/osi-certified-72x60.png | Bin 3774 -> 0 bytes .../Tests/Lua/Lua.5.2.4/doc/readme.html | 417 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.c | 1284 -- LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.h | 24 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.c | 959 -- LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.h | 212 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbaselib.c | 458 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbitlib.c | 212 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.c | 881 -- LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.h | 83 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcorolib.c | 155 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.c | 52 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.h | 95 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldblib.c | 408 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.c | 610 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.h | 34 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.c | 681 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.h | 46 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldump.c | 173 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.c | 161 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.h | 33 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.c | 1220 -- LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.h | 157 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/linit.c | 67 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/liolib.c | 666 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.c | 530 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.h | 78 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/llimits.h | 309 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmathlib.c | 279 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.c | 99 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.h | 57 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/loadlib.c | 725 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.c | 287 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.h | 607 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.c | 107 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.h | 288 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/loslib.c | 323 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.c | 1638 --- LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.h | 119 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.c | 323 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.h | 228 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.c | 185 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.h | 46 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstrlib.c | 1019 -- LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.c | 588 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.h | 45 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltablib.c | 285 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.c | 77 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.h | 57 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.c | 497 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.h | 444 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.hpp | 9 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/luac.c | 432 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/luaconf.h | 551 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lualib.h | 55 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.c | 258 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.h | 28 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.c | 867 -- LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.h | 44 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.c | 76 - LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.h | 65 - LuaBridge3/Tests/Lua/Lua.5.3.6/README | 6 - .../Tests/Lua/Lua.5.3.6/doc/contents.html | 650 - LuaBridge3/Tests/Lua/Lua.5.3.6/doc/index.css | 21 - LuaBridge3/Tests/Lua/Lua.5.3.6/doc/logo.gif | Bin 9893 -> 0 bytes LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.1 | 112 - LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.css | 161 - LuaBridge3/Tests/Lua/Lua.5.3.6/doc/luac.1 | 118 - LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.css | 21 - .../Tests/Lua/Lua.5.3.6/doc/manual.html | 10982 -------------- .../Lua/Lua.5.3.6/doc/osi-certified-72x60.png | Bin 3774 -> 0 bytes .../Tests/Lua/Lua.5.3.6/doc/readme.html | 365 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.c | 1299 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.h | 24 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.c | 1048 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.h | 264 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbaselib.c | 498 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbitlib.c | 233 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.c | 1203 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.h | 88 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcorolib.c | 168 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.c | 55 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.h | 95 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldblib.c | 456 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.c | 700 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.h | 39 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.c | 802 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.h | 58 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldump.c | 215 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.c | 151 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.h | 61 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.c | 1179 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.h | 147 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/linit.c | 68 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/liolib.c | 778 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.c | 568 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.h | 85 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/llimits.h | 323 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmathlib.c | 410 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.c | 100 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.h | 69 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/loadlib.c | 790 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.c | 522 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.h | 549 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.c | 124 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.h | 297 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/loslib.c | 409 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.c | 1653 --- LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.h | 133 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lprefix.h | 45 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.c | 347 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.h | 253 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.c | 248 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.h | 49 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstrlib.c | 1584 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.c | 688 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.h | 66 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltablib.c | 450 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.c | 165 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.h | 76 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.c | 612 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.h | 485 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.hpp | 9 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/luac.c | 450 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/luaconf.h | 790 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lualib.h | 61 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.c | 287 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.h | 32 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lutf8lib.c | 256 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.c | 1322 -- LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.h | 113 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.c | 68 - LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.h | 66 - LuaBridge3/Tests/Lua/Lua.5.4.4/README | 6 - .../Tests/Lua/Lua.5.4.4/doc/contents.html | 674 - LuaBridge3/Tests/Lua/Lua.5.4.4/doc/index.css | 21 - LuaBridge3/Tests/Lua/Lua.5.4.4/doc/logo.gif | Bin 9893 -> 0 bytes LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.1 | 149 - LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.css | 161 - LuaBridge3/Tests/Lua/Lua.5.4.4/doc/luac.1 | 118 - LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.css | 21 - .../Tests/Lua/Lua.5.4.4/doc/manual.html | 11959 ---------------- .../Lua/Lua.5.4.4/doc/osi-certified-72x60.png | Bin 3774 -> 0 bytes .../Tests/Lua/Lua.5.4.4/doc/readme.html | 340 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.c | 1466 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.h | 49 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.c | 1106 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.h | 301 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lbaselib.c | 549 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.c | 1832 --- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.h | 104 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcorolib.c | 210 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.c | 64 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.h | 101 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldblib.c | 483 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.c | 918 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.h | 63 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.c | 997 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.h | 79 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldump.c | 226 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.c | 294 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.h | 64 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.c | 1731 --- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.h | 199 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/linit.c | 65 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/liolib.c | 828 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/ljumptab.h | 112 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.c | 581 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.h | 91 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/llimits.h | 367 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmathlib.c | 764 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.c | 201 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.h | 93 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/loadlib.c | 762 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.c | 592 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.h | 800 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.c | 104 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.h | 405 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopnames.h | 103 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/loslib.c | 430 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.c | 1966 --- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.h | 171 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lprefix.h | 45 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.c | 440 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.h | 404 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.c | 273 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.h | 57 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstrlib.c | 1874 --- LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.c | 980 -- LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.h | 66 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltablib.c | 430 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.c | 271 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.h | 103 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.c | 666 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.h | 518 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.hpp | 9 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/luac.c | 725 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/luaconf.h | 786 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lualib.h | 52 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.c | 333 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.h | 36 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lutf8lib.c | 286 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.c | 1840 --- LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.h | 136 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.c | 68 - LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.h | 66 - LuaBridge3/Tests/Lua/LuaJIT.2.1/.gitignore | 17 - .../Tests/Lua/LuaJIT.2.1/CMakeLists.txt | 444 - LuaBridge3/Tests/Lua/LuaJIT.2.1/COPYRIGHT | 56 - LuaBridge3/Tests/Lua/LuaJIT.2.1/README | 16 - .../Lua/LuaJIT.2.1/cmake/FindReadline.cmake | 80 - .../Lua/LuaJIT.2.1/cmake/LuaConfig.cmake.in | 9 - .../Tests/Lua/LuaJIT.2.1/cmake/LuaPaths.cmake | 63 - .../Lua/LuaJIT.2.1/doc/bluequad-print.css | 166 - .../Tests/Lua/LuaJIT.2.1/doc/bluequad.css | 325 - .../Tests/Lua/LuaJIT.2.1/doc/contact.html | 112 - .../Tests/Lua/LuaJIT.2.1/doc/ext_buffer.html | 695 - .../Tests/Lua/LuaJIT.2.1/doc/ext_c_api.html | 183 - .../Tests/Lua/LuaJIT.2.1/doc/ext_ffi.html | 326 - .../Tests/Lua/LuaJIT.2.1/doc/ext_ffi_api.html | 566 - .../Lua/LuaJIT.2.1/doc/ext_ffi_semantics.html | 1256 -- .../Lua/LuaJIT.2.1/doc/ext_ffi_tutorial.html | 597 - .../Tests/Lua/LuaJIT.2.1/doc/ext_jit.html | 195 - .../Lua/LuaJIT.2.1/doc/ext_profiler.html | 359 - .../Tests/Lua/LuaJIT.2.1/doc/extensions.html | 472 - LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/faq.html | 195 - .../Tests/Lua/LuaJIT.2.1/doc/img/contact.png | Bin 1340 -> 0 bytes .../Tests/Lua/LuaJIT.2.1/doc/install.html | 776 - .../Tests/Lua/LuaJIT.2.1/doc/luajit.html | 230 - .../Tests/Lua/LuaJIT.2.1/doc/running.html | 312 - .../Tests/Lua/LuaJIT.2.1/doc/status.html | 105 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.h | 461 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.lua | 1125 -- .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.h | 563 - .../Lua/LuaJIT.2.1/dynasm/dasm_arm64.lua | 1219 -- .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.h | 424 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.lua | 1181 -- .../Lua/LuaJIT.2.1/dynasm/dasm_mips64.lua | 12 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.h | 423 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.lua | 1919 --- .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_proto.h | 83 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_x64.lua | 12 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.h | 528 - .../Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.lua | 2388 --- .../Tests/Lua/LuaJIT.2.1/dynasm/dynasm.lua | 1095 -- LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.1 | 88 - LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.pc | 25 - .../Tests/Lua/LuaJIT.2.1/src/.gitignore | 7 - .../Tests/Lua/LuaJIT.2.1/src/Makefile.dep | 259 - .../Tests/Lua/LuaJIT.2.1/src/host/.gitignore | 3 - .../Tests/Lua/LuaJIT.2.1/src/host/README | 4 - .../Tests/Lua/LuaJIT.2.1/src/host/buildvm.c | 528 - .../Tests/Lua/LuaJIT.2.1/src/host/buildvm.h | 105 - .../Lua/LuaJIT.2.1/src/host/buildvm_asm.c | 348 - .../Lua/LuaJIT.2.1/src/host/buildvm_fold.c | 236 - .../Lua/LuaJIT.2.1/src/host/buildvm_lib.c | 459 - .../Lua/LuaJIT.2.1/src/host/buildvm_libbc.h | 81 - .../Lua/LuaJIT.2.1/src/host/buildvm_peobj.c | 379 - .../Lua/LuaJIT.2.1/src/host/genlibbc.lua | 225 - .../Lua/LuaJIT.2.1/src/host/genminilua.lua | 436 - .../Tests/Lua/LuaJIT.2.1/src/host/minilua.c | 7771 ---------- .../Tests/Lua/LuaJIT.2.1/src/jit/.gitignore | 1 - .../Tests/Lua/LuaJIT.2.1/src/jit/bc.lua | 190 - .../Tests/Lua/LuaJIT.2.1/src/jit/bcsave.lua | 705 - .../Tests/Lua/LuaJIT.2.1/src/jit/dis_arm.lua | 689 - .../Lua/LuaJIT.2.1/src/jit/dis_arm64.lua | 1216 -- .../Lua/LuaJIT.2.1/src/jit/dis_arm64be.lua | 12 - .../Tests/Lua/LuaJIT.2.1/src/jit/dis_mips.lua | 694 - .../Lua/LuaJIT.2.1/src/jit/dis_mips64.lua | 17 - .../Lua/LuaJIT.2.1/src/jit/dis_mips64el.lua | 17 - .../Lua/LuaJIT.2.1/src/jit/dis_mips64r6.lua | 17 - .../Lua/LuaJIT.2.1/src/jit/dis_mips64r6el.lua | 17 - .../Lua/LuaJIT.2.1/src/jit/dis_mipsel.lua | 17 - .../Tests/Lua/LuaJIT.2.1/src/jit/dis_ppc.lua | 591 - .../Tests/Lua/LuaJIT.2.1/src/jit/dis_x64.lua | 17 - .../Tests/Lua/LuaJIT.2.1/src/jit/dis_x86.lua | 953 -- .../Tests/Lua/LuaJIT.2.1/src/jit/dump.lua | 726 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/p.lua | 312 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/v.lua | 170 - .../Tests/Lua/LuaJIT.2.1/src/jit/zone.lua | 45 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lauxlib.h | 161 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_aux.c | 370 - .../Tests/Lua/LuaJIT.2.1/src/lib_base.c | 696 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_bit.c | 180 - .../Tests/Lua/LuaJIT.2.1/src/lib_buffer.c | 360 - .../Tests/Lua/LuaJIT.2.1/src/lib_debug.c | 406 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_ffi.c | 870 -- .../Tests/Lua/LuaJIT.2.1/src/lib_init.c | 55 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_io.c | 551 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_jit.c | 761 - .../Tests/Lua/LuaJIT.2.1/src/lib_math.c | 201 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_os.c | 292 - .../Tests/Lua/LuaJIT.2.1/src/lib_package.c | 628 - .../Tests/Lua/LuaJIT.2.1/src/lib_string.c | 676 - .../Tests/Lua/LuaJIT.2.1/src/lib_table.c | 327 - .../Tests/Lua/LuaJIT.2.1/src/lj_alloc.c | 1485 -- .../Tests/Lua/LuaJIT.2.1/src/lj_alloc.h | 18 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_api.c | 1313 -- LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_arch.h | 719 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.c | 2571 ---- LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.h | 17 - .../Tests/Lua/LuaJIT.2.1/src/lj_asm_arm.h | 2290 --- .../Tests/Lua/LuaJIT.2.1/src/lj_asm_arm64.h | 2071 --- .../Tests/Lua/LuaJIT.2.1/src/lj_asm_mips.h | 2808 ---- .../Tests/Lua/LuaJIT.2.1/src/lj_asm_ppc.h | 2326 --- .../Tests/Lua/LuaJIT.2.1/src/lj_asm_x86.h | 3125 ---- .../Tests/Lua/LuaJIT.2.1/src/lj_assert.c | 28 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.c | 14 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.h | 265 - .../Tests/Lua/LuaJIT.2.1/src/lj_bcdump.h | 68 - .../Tests/Lua/LuaJIT.2.1/src/lj_bcread.c | 453 - .../Tests/Lua/LuaJIT.2.1/src/lj_bcwrite.c | 372 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.c | 305 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.h | 198 - .../Tests/Lua/LuaJIT.2.1/src/lj_carith.c | 432 - .../Tests/Lua/LuaJIT.2.1/src/lj_carith.h | 37 - .../Tests/Lua/LuaJIT.2.1/src/lj_ccall.c | 1189 -- .../Tests/Lua/LuaJIT.2.1/src/lj_ccall.h | 194 - .../Tests/Lua/LuaJIT.2.1/src/lj_ccallback.c | 796 - .../Tests/Lua/LuaJIT.2.1/src/lj_ccallback.h | 25 - .../Tests/Lua/LuaJIT.2.1/src/lj_cconv.c | 770 - .../Tests/Lua/LuaJIT.2.1/src/lj_cconv.h | 71 - .../Tests/Lua/LuaJIT.2.1/src/lj_cdata.c | 304 - .../Tests/Lua/LuaJIT.2.1/src/lj_cdata.h | 79 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.c | 43 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.h | 42 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.c | 434 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.h | 29 - .../Tests/Lua/LuaJIT.2.1/src/lj_cparse.c | 1927 --- .../Tests/Lua/LuaJIT.2.1/src/lj_cparse.h | 67 - .../Tests/Lua/LuaJIT.2.1/src/lj_crecord.c | 1948 --- .../Tests/Lua/LuaJIT.2.1/src/lj_crecord.h | 43 - .../Tests/Lua/LuaJIT.2.1/src/lj_ctype.c | 658 - .../Tests/Lua/LuaJIT.2.1/src/lj_ctype.h | 481 - .../Tests/Lua/LuaJIT.2.1/src/lj_debug.c | 708 - .../Tests/Lua/LuaJIT.2.1/src/lj_debug.h | 66 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_def.h | 381 - .../Tests/Lua/LuaJIT.2.1/src/lj_dispatch.c | 559 - .../Tests/Lua/LuaJIT.2.1/src/lj_dispatch.h | 164 - .../Tests/Lua/LuaJIT.2.1/src/lj_emit_arm.h | 361 - .../Tests/Lua/LuaJIT.2.1/src/lj_emit_arm64.h | 424 - .../Tests/Lua/LuaJIT.2.1/src/lj_emit_mips.h | 310 - .../Tests/Lua/LuaJIT.2.1/src/lj_emit_ppc.h | 238 - .../Tests/Lua/LuaJIT.2.1/src/lj_emit_x86.h | 572 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.c | 1098 -- LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.h | 58 - .../Tests/Lua/LuaJIT.2.1/src/lj_errmsg.h | 200 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ff.h | 18 - .../Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.c | 1574 -- .../Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.h | 24 - .../Tests/Lua/LuaJIT.2.1/src/lj_frame.h | 297 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.c | 191 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.h | 24 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.c | 909 -- LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.h | 136 - .../Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.c | 818 -- .../Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.h | 22 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.c | 500 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.h | 614 - .../Tests/Lua/LuaJIT.2.1/src/lj_ircall.h | 383 - .../Tests/Lua/LuaJIT.2.1/src/lj_iropt.h | 162 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_jit.h | 530 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.c | 514 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.h | 93 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.c | 359 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.h | 116 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_load.c | 168 - .../Tests/Lua/LuaJIT.2.1/src/lj_mcode.c | 374 - .../Tests/Lua/LuaJIT.2.1/src/lj_mcode.h | 30 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.c | 482 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.h | 38 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.c | 51 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.h | 1045 -- .../Tests/Lua/LuaJIT.2.1/src/lj_opt_dce.c | 75 - .../Tests/Lua/LuaJIT.2.1/src/lj_opt_fold.c | 2602 ---- .../Tests/Lua/LuaJIT.2.1/src/lj_opt_loop.c | 453 - .../Tests/Lua/LuaJIT.2.1/src/lj_opt_mem.c | 979 -- .../Tests/Lua/LuaJIT.2.1/src/lj_opt_narrow.c | 622 - .../Tests/Lua/LuaJIT.2.1/src/lj_opt_sink.c | 258 - .../Tests/Lua/LuaJIT.2.1/src/lj_opt_split.c | 848 -- .../Tests/Lua/LuaJIT.2.1/src/lj_parse.c | 2747 ---- .../Tests/Lua/LuaJIT.2.1/src/lj_parse.h | 18 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.c | 259 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.h | 24 - .../Tests/Lua/LuaJIT.2.1/src/lj_profile.c | 371 - .../Tests/Lua/LuaJIT.2.1/src/lj_profile.h | 21 - .../Tests/Lua/LuaJIT.2.1/src/lj_record.c | 2838 ---- .../Tests/Lua/LuaJIT.2.1/src/lj_record.h | 47 - .../Tests/Lua/LuaJIT.2.1/src/lj_serialize.c | 539 - .../Tests/Lua/LuaJIT.2.1/src/lj_serialize.h | 28 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.c | 996 -- LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.h | 35 - .../Tests/Lua/LuaJIT.2.1/src/lj_state.c | 335 - .../Tests/Lua/LuaJIT.2.1/src/lj_state.h | 37 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.c | 370 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.h | 31 - .../Tests/Lua/LuaJIT.2.1/src/lj_strfmt.c | 606 - .../Tests/Lua/LuaJIT.2.1/src/lj_strfmt.h | 131 - .../Tests/Lua/LuaJIT.2.1/src/lj_strfmt_num.c | 592 - .../Tests/Lua/LuaJIT.2.1/src/lj_strscan.c | 558 - .../Tests/Lua/LuaJIT.2.1/src/lj_strscan.h | 40 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.c | 693 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.h | 96 - .../Tests/Lua/LuaJIT.2.1/src/lj_target.h | 165 - .../Tests/Lua/LuaJIT.2.1/src/lj_target_arm.h | 271 - .../Lua/LuaJIT.2.1/src/lj_target_arm64.h | 336 - .../Tests/Lua/LuaJIT.2.1/src/lj_target_mips.h | 417 - .../Tests/Lua/LuaJIT.2.1/src/lj_target_ppc.h | 280 - .../Tests/Lua/LuaJIT.2.1/src/lj_target_x86.h | 357 - .../Tests/Lua/LuaJIT.2.1/src/lj_trace.c | 987 -- .../Tests/Lua/LuaJIT.2.1/src/lj_trace.h | 58 - .../Tests/Lua/LuaJIT.2.1/src/lj_traceerr.h | 61 - .../Tests/Lua/LuaJIT.2.1/src/lj_udata.c | 62 - .../Tests/Lua/LuaJIT.2.1/src/lj_udata.h | 17 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vm.h | 116 - .../Tests/Lua/LuaJIT.2.1/src/lj_vmevent.c | 58 - .../Tests/Lua/LuaJIT.2.1/src/lj_vmevent.h | 59 - .../Tests/Lua/LuaJIT.2.1/src/lj_vmmath.c | 118 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ljamalg.c | 91 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.h | 402 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.hpp | 9 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luaconf.h | 152 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.c | 586 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.h | 79 - LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lualib.h | 44 - .../Tests/Lua/LuaJIT.2.1/src/msvcbuild.bat | 127 - .../Tests/Lua/LuaJIT.2.1/src/nxbuild.bat | 160 - .../Tests/Lua/LuaJIT.2.1/src/ps4build.bat | 123 - .../Tests/Lua/LuaJIT.2.1/src/ps5build.bat | 123 - .../Tests/Lua/LuaJIT.2.1/src/psvitabuild.bat | 93 - .../Tests/Lua/LuaJIT.2.1/src/vm_arm.dasc | 4663 ------ .../Tests/Lua/LuaJIT.2.1/src/vm_arm64.dasc | 4160 ------ .../Tests/Lua/LuaJIT.2.1/src/vm_mips.dasc | 5392 ------- .../Tests/Lua/LuaJIT.2.1/src/vm_mips64.dasc | 5538 ------- .../Tests/Lua/LuaJIT.2.1/src/vm_ppc.dasc | 6041 -------- .../Tests/Lua/LuaJIT.2.1/src/vm_x64.dasc | 4935 ------- .../Tests/Lua/LuaJIT.2.1/src/vm_x86.dasc | 5825 -------- .../Tests/Lua/LuaJIT.2.1/src/xb1build.bat | 101 - .../Tests/Lua/LuaJIT.2.1/src/xedkbuild.bat | 92 - LuaBridge3/Tests/Lua/LuaLibrary.h | 70 - LuaBridge3/Tests/Lua/LuaLibrary5.1.5.cpp | 124 - LuaBridge3/Tests/Lua/LuaLibrary5.2.4.cpp | 136 - LuaBridge3/Tests/Lua/LuaLibrary5.3.6.cpp | 137 - LuaBridge3/Tests/Lua/LuaLibrary5.4.4.cpp | 138 - LuaBridge3/Tests/Lua/Luau.cpp | 86 - LuaBridge3/Tests/Lua/LuauSplit.cpp | 67 - LuaBridge3/Tests/README.md | 80 - LuaBridge3/Tests/Source/AmalgamateTests.cpp | 32 - LuaBridge3/Tests/Source/ArrayTests.cpp | 168 - LuaBridge3/Tests/Source/ClassTests.cpp | 2617 ---- .../Tests/Source/DynamicLibraryTests.cpp | 215 - LuaBridge3/Tests/Source/ExceptionTests.cpp | 131 - LuaBridge3/Tests/Source/IssueTests.cpp | 268 - LuaBridge3/Tests/Source/IteratorTests.cpp | 93 - LuaBridge3/Tests/Source/LegacyTests.cpp | 268 - LuaBridge3/Tests/Source/LegacyTests.h | 107 - LuaBridge3/Tests/Source/ListTests.cpp | 150 - LuaBridge3/Tests/Source/LuaRefTests.cpp | 685 - LuaBridge3/Tests/Source/MapTests.cpp | 270 - LuaBridge3/Tests/Source/NamespaceTests.cpp | 532 - LuaBridge3/Tests/Source/ObjCTests.mm | 8 - LuaBridge3/Tests/Source/OptionalTests.cpp | 346 - LuaBridge3/Tests/Source/OverloadTests.cpp | 873 -- LuaBridge3/Tests/Source/PairTests.cpp | 157 - LuaBridge3/Tests/Source/PerformanceTests.cpp | 131 - .../Tests/Source/RefCountedPtrTests.cpp | 131 - LuaBridge3/Tests/Source/ScopeGuardTests.cpp | 29 - LuaBridge3/Tests/Source/SharedCode.cpp | 68 - LuaBridge3/Tests/Source/SharedCode.h | 32 - LuaBridge3/Tests/Source/StackTests.cpp | 2318 --- LuaBridge3/Tests/Source/TestBase.h | 228 - LuaBridge3/Tests/Source/TestTypes.h | 161 - LuaBridge3/Tests/Source/Tests.cpp | 551 - LuaBridge3/Tests/Source/Tests.lua | 107 - LuaBridge3/Tests/Source/TestsMain.cpp | 17 - LuaBridge3/Tests/Source/UnorderedMapTests.cpp | 204 - LuaBridge3/Tests/Source/UserdataTests.cpp | 156 - LuaBridge3/Tests/Source/VectorTests.cpp | 144 - LuaBridge3/_config.yml | 2 - LuaBridge3/amalgamate.py | 217 - LuaBridge3/logo.png | Bin 20688 -> 0 bytes LuaBridge3/lua.png | Bin 4091 -> 0 bytes README.md | 64 +- ideas-keineahnung-scripting.md | 51 - luau | 1 - main.cpp | 8 +- modules/captcha.cpp | 36 +- modules/custom.cpp | 619 - modules/embedmaker.cpp | 24 +- modules/globalchat.cpp | 66 +- modules/globalchat.hpp | 2 +- modules/info.cpp | 54 +- modules/lastseen.cpp | 8 +- modules/levels.cpp | 46 +- modules/management.cpp | 16 +- modules/moderation.cpp | 34 +- modules/ticket.cpp | 70 +- modules/{ => wip}/join2create.cpp | 0 util.cpp | 18 +- 656 files changed, 231 insertions(+), 325387 deletions(-) delete mode 100644 LuaBridge3/.clang-format delete mode 100644 LuaBridge3/.coveralls.yml delete mode 100644 LuaBridge3/.gitattributes delete mode 100644 LuaBridge3/.github/workflows/amalgamate.yml delete mode 100644 LuaBridge3/.github/workflows/build_linux.yml delete mode 100644 LuaBridge3/.github/workflows/build_macos.yml delete mode 100644 LuaBridge3/.github/workflows/build_windows.yml delete mode 100644 LuaBridge3/.github/workflows/codeql-analysis.yml delete mode 100644 LuaBridge3/.github/workflows/coverage.yml delete mode 100644 LuaBridge3/.gitignore delete mode 100644 LuaBridge3/.gitmodules delete mode 100644 LuaBridge3/CHANGES.md delete mode 100644 LuaBridge3/CMakeLists.txt delete mode 100644 LuaBridge3/Distribution/LuaBridge/LuaBridge.h delete mode 100644 LuaBridge3/Doxyfile delete mode 100644 LuaBridge3/LICENSE.txt delete mode 100644 LuaBridge3/Manual.md delete mode 100644 LuaBridge3/README.md delete mode 100644 LuaBridge3/Source/CMakeLists.txt delete mode 100644 LuaBridge3/Source/LuaBridge/Array.h delete mode 100644 LuaBridge3/Source/LuaBridge/List.h delete mode 100644 LuaBridge3/Source/LuaBridge/LuaBridge.h delete mode 100644 LuaBridge3/Source/LuaBridge/Map.h delete mode 100644 LuaBridge3/Source/LuaBridge/RefCountedObject.h delete mode 100644 LuaBridge3/Source/LuaBridge/Set.h delete mode 100644 LuaBridge3/Source/LuaBridge/UnorderedMap.h delete mode 100644 LuaBridge3/Source/LuaBridge/Vector.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/CFunctions.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/ClassInfo.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Config.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Dump.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Errors.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Expected.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/FuncTraits.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Invoke.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Iterator.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/LuaException.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/LuaHelpers.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/LuaRef.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Namespace.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Overload.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Result.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/ScopeGuard.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Security.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Stack.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/TypeTraits.h delete mode 100644 LuaBridge3/Source/LuaBridge/detail/Userdata.h delete mode 100644 LuaBridge3/Tests/CMakeLists.txt delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/COPYRIGHT delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/HISTORY delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/INSTALL delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/README delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/contents.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/cover.png delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/logo.gif delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/doc/readme.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/README delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/all.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.hpp delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.ico delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.pc delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/luavs.bat delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/min.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/noparser.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/etc/strict.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lbaselib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldblib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/linit.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/liolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/llimits.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmathlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/loadlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/loslib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstrlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltablib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/luac.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/luaconf.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lualib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/src/print.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/README delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/bisect.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/cf.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/echo.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/env.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/factorial.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/fib.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/fibfor.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/globals.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/hello.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/life.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/luac.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/printf.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/readonly.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/sieve.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/sort.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/table.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/trace-calls.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/trace-globals.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.1.5/test/xd.lua delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/README delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/contents.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/logo.gif delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/luac.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/osi-certified-72x60.png delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/doc/readme.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbaselib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbitlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcorolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldblib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/linit.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/liolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/llimits.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmathlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/loadlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/loslib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstrlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltablib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.hpp delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/luac.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/luaconf.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lualib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/README delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/contents.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/index.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/logo.gif delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/luac.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/osi-certified-72x60.png delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/doc/readme.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbaselib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbitlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcorolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldblib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/linit.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/liolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/llimits.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmathlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/loadlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/loslib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lprefix.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstrlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltablib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.hpp delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/luac.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/luaconf.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lualib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lutf8lib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/README delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/contents.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/index.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/logo.gif delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/luac.1 delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.css delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/osi-certified-72x60.png delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/doc/readme.html delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lbaselib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcorolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldblib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/linit.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/liolib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ljumptab.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/llimits.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmathlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/loadlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopnames.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/loslib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lprefix.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstrlib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltablib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.hpp delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/luac.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/luaconf.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lualib.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lutf8lib.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.h delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.c delete mode 100644 LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/.gitignore delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/CMakeLists.txt delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/COPYRIGHT delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/README delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/FindReadline.cmake delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaConfig.cmake.in delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaPaths.cmake delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad-print.css delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad.css delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/contact.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_buffer.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_c_api.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_api.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_semantics.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_tutorial.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_jit.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_profiler.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/extensions.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/faq.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/img/contact.png delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/install.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/luajit.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/running.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/status.html delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips64.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_proto.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x64.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dynasm.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.1 delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.pc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/.gitignore delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/Makefile.dep delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/.gitignore delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/README delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_asm.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_fold.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_lib.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_libbc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_peobj.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genlibbc.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genminilua.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/minilua.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/.gitignore delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/bc.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/bcsave.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64be.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64el.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6el.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mipsel.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_ppc.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x64.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x86.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dump.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/p.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/v.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/zone.lua delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lauxlib.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_aux.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_base.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_bit.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_buffer.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_debug.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_ffi.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_init.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_io.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_jit.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_math.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_os.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_package.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_string.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_table.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_api.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_arch.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm64.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_mips.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_ppc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_x86.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_assert.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcdump.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcread.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcwrite.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_def.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm64.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_mips.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_ppc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_x86.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_errmsg.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ff.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_frame.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ircall.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_iropt.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_jit.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_load.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_dce.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_fold.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_loop.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_mem.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_narrow.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_sink.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_split.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt_num.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm64.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_mips.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_ppc.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_x86.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_traceerr.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vm.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmmath.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ljamalg.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.hpp delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luaconf.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.c delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lualib.h delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/msvcbuild.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/nxbuild.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps4build.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps5build.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/psvitabuild.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm64.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips64.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_ppc.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x64.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x86.dasc delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xb1build.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xedkbuild.bat delete mode 100644 LuaBridge3/Tests/Lua/LuaLibrary.h delete mode 100644 LuaBridge3/Tests/Lua/LuaLibrary5.1.5.cpp delete mode 100644 LuaBridge3/Tests/Lua/LuaLibrary5.2.4.cpp delete mode 100644 LuaBridge3/Tests/Lua/LuaLibrary5.3.6.cpp delete mode 100644 LuaBridge3/Tests/Lua/LuaLibrary5.4.4.cpp delete mode 100644 LuaBridge3/Tests/Lua/Luau.cpp delete mode 100644 LuaBridge3/Tests/Lua/LuauSplit.cpp delete mode 100644 LuaBridge3/Tests/README.md delete mode 100644 LuaBridge3/Tests/Source/AmalgamateTests.cpp delete mode 100644 LuaBridge3/Tests/Source/ArrayTests.cpp delete mode 100644 LuaBridge3/Tests/Source/ClassTests.cpp delete mode 100644 LuaBridge3/Tests/Source/DynamicLibraryTests.cpp delete mode 100644 LuaBridge3/Tests/Source/ExceptionTests.cpp delete mode 100644 LuaBridge3/Tests/Source/IssueTests.cpp delete mode 100644 LuaBridge3/Tests/Source/IteratorTests.cpp delete mode 100644 LuaBridge3/Tests/Source/LegacyTests.cpp delete mode 100644 LuaBridge3/Tests/Source/LegacyTests.h delete mode 100644 LuaBridge3/Tests/Source/ListTests.cpp delete mode 100644 LuaBridge3/Tests/Source/LuaRefTests.cpp delete mode 100644 LuaBridge3/Tests/Source/MapTests.cpp delete mode 100644 LuaBridge3/Tests/Source/NamespaceTests.cpp delete mode 100644 LuaBridge3/Tests/Source/ObjCTests.mm delete mode 100644 LuaBridge3/Tests/Source/OptionalTests.cpp delete mode 100644 LuaBridge3/Tests/Source/OverloadTests.cpp delete mode 100644 LuaBridge3/Tests/Source/PairTests.cpp delete mode 100644 LuaBridge3/Tests/Source/PerformanceTests.cpp delete mode 100644 LuaBridge3/Tests/Source/RefCountedPtrTests.cpp delete mode 100644 LuaBridge3/Tests/Source/ScopeGuardTests.cpp delete mode 100644 LuaBridge3/Tests/Source/SharedCode.cpp delete mode 100644 LuaBridge3/Tests/Source/SharedCode.h delete mode 100644 LuaBridge3/Tests/Source/StackTests.cpp delete mode 100644 LuaBridge3/Tests/Source/TestBase.h delete mode 100644 LuaBridge3/Tests/Source/TestTypes.h delete mode 100644 LuaBridge3/Tests/Source/Tests.cpp delete mode 100644 LuaBridge3/Tests/Source/Tests.lua delete mode 100644 LuaBridge3/Tests/Source/TestsMain.cpp delete mode 100644 LuaBridge3/Tests/Source/UnorderedMapTests.cpp delete mode 100644 LuaBridge3/Tests/Source/UserdataTests.cpp delete mode 100644 LuaBridge3/Tests/Source/VectorTests.cpp delete mode 100644 LuaBridge3/_config.yml delete mode 100644 LuaBridge3/amalgamate.py delete mode 100644 LuaBridge3/logo.png delete mode 100644 LuaBridge3/lua.png delete mode 100644 ideas-keineahnung-scripting.md delete mode 160000 luau delete mode 100644 modules/custom.cpp rename modules/{ => wip}/join2create.cpp (100%) diff --git a/.gitmodules b/.gitmodules index 7256a76..6b4010c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [submodule "DPP"] path = DPP url = https://github.com/brainboxdotcc/DPP.git -[submodule "luau"] - path = luau - url = https://github.com/Roblox/luau.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 97ba6ca..49679fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,30 +1,25 @@ cmake_minimum_required(VERSION 3.9) -project(KeineAhnung LANGUAGES CXX) +project(SomeBot LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_subdirectory(DPP) -set(LUAU_BUILD_TESTS No) -add_subdirectory(luau) -set(LUABRIDGE_TESTING No) -set(LUABRIDGE_COVERAGE No) -add_subdirectory(LuaBridge3) - add_compile_definitions(COMPILER_ID="${CMAKE_CXX_COMPILER_ID}") add_compile_definitions(COMPILER_VERSION="${CMAKE_CXX_COMPILER_VERSION}") add_compile_definitions(COMPILER_PLATFORM="${CMAKE_CXX_PLATFORM_ID}") +add_subdirectory(DPP) + file(GLOB MODULES modules/*.cpp modules/*.hpp) -add_executable(KeineAhnung main.cpp util.cpp ${MODULES}) -target_link_libraries(KeineAhnung PRIVATE dpp sqlite3 pthread Luau.VM Luau.Compiler LuaBridge) +add_executable(SomeBot main.cpp util.cpp bot.hpp ${MODULES}) +target_link_libraries(SomeBot PRIVATE dpp sqlite3 pthread) if (CMAKE_BUILD_TYPE STREQUAL "Release") - set_property(TARGET KeineAhnung PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + set_property(TARGET SomeBot PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) message(STATUS "Interprocedural optimization is enabled") endif() -target_compile_options(KeineAhnung PRIVATE +target_compile_options(SomeBot PRIVATE -Wall -Wextra -Wpedantic ) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") @@ -32,10 +27,10 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") if (CMAKE_BUILD_TYPE STREQUAL "Release") set(UBSAN_FLAGS ${UBSAN_FLAGS} "-fsanitize-minimal-runtime") endif() - target_compile_options(KeineAhnung PUBLIC ${UBSAN_FLAGS}) - target_link_options(KeineAhnung PUBLIC ${UBSAN_FLAGS}) + target_compile_options(SomeBot PUBLIC ${UBSAN_FLAGS}) + target_link_options(SomeBot PUBLIC ${UBSAN_FLAGS}) message(STATUS "Undefined sanitizer runtime is enabled") endif() -install(TARGETS KeineAhnung +install(TARGETS SomeBot LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/LuaBridge3/.clang-format b/LuaBridge3/.clang-format deleted file mode 100644 index 93375ad..0000000 --- a/LuaBridge3/.clang-format +++ /dev/null @@ -1,73 +0,0 @@ ---- -AccessModifierOffset: -4 -AlignConsecutiveAssignments: false -AlignConsecutiveDeclarations: false -AlignEscapedNewlines: DontAlign -AlignOperands: true -AlignTrailingComments: false -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: InlineOnly -AllowShortIfStatementsOnASingleLine: false -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: true -BinPackArguments: false -BinPackParameters: false -BraceWrapping: - AfterClass: true - AfterControlStatement: true - AfterEnum: true - AfterFunction: true - AfterNamespace: false - AfterStruct: true - AfterUnion: true - BeforeCatch: true - BeforeElse: true - IndentBraces: false - SplitEmptyFunction: true -BreakBeforeBinaryOperators: None -BreakBeforeBraces: Custom -BreakBeforeInheritanceComma: false -BreakBeforeTernaryOperators: true -BreakConstructorInitializers: BeforeComma -BreakStringLiterals: true -ColumnLimit: 100 -CompactNamespaces: false -ConstructorInitializerAllOnOneLineOrOnePerLine: true -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: true -DerivePointerAlignment: false -DisableFormat: false -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: true -IndentCaseLabels: false -IndentWidth: 4 -IndentWrappedFunctionNames: false -KeepEmptyLinesAtTheStartOfBlocks: false -Language: Cpp -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -PointerAlignment: Left -ReflowComments: true -SortIncludes: true -SortUsingDeclarations: true -SpaceAfterCStyleCast: true -SpaceAfterTemplateKeyword: false -SpaceBeforeAssignmentOperators: true -SpaceBeforeParens: ControlStatements -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: false -SpacesInCStyleCastParentheses: false -SpacesInContainerLiterals: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -Standard: Cpp11 -TabWidth: 4 -UseTab: Never - -... diff --git a/LuaBridge3/.coveralls.yml b/LuaBridge3/.coveralls.yml deleted file mode 100644 index 1c3ae76..0000000 --- a/LuaBridge3/.coveralls.yml +++ /dev/null @@ -1 +0,0 @@ -repo_token: amVxRIVnLlAXJBJo02AKMkxVHN0IeBArV \ No newline at end of file diff --git a/LuaBridge3/.gitattributes b/LuaBridge3/.gitattributes deleted file mode 100644 index 176a458..0000000 --- a/LuaBridge3/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -* text=auto diff --git a/LuaBridge3/.github/workflows/amalgamate.yml b/LuaBridge3/.github/workflows/amalgamate.yml deleted file mode 100644 index ce05720..0000000 --- a/LuaBridge3/.github/workflows/amalgamate.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Amalgamate - -on: - push: - branches: - - master - -jobs: - run: - name: Create Amalgamation - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: 3.9 - - - name: Create amalgamation file - run: python amalgamate.py - - - name: Commit changes - uses: EndBug/add-and-commit@v9 - with: - committer_name: GitHub Actions - committer_email: actions@github.com - message: Update amalgamation file - add: 'Distribution/LuaBridge/*.h' diff --git a/LuaBridge3/.github/workflows/build_linux.yml b/LuaBridge3/.github/workflows/build_linux.yml deleted file mode 100644 index 41c1c8d..0000000 --- a/LuaBridge3/.github/workflows/build_linux.yml +++ /dev/null @@ -1,62 +0,0 @@ -name: Build Linux - -on: [push] - -env: - BUILD_TYPE: Release - -jobs: - build: - runs-on: ubuntu-latest - steps: - - - uses: actions/checkout@v2 - with: - submodules: true - - - uses: seanmiddleditch/gha-setup-ninja@master - - - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Configure - working-directory: ${{runner.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -G Ninja - - - name: Build - working-directory: ${{runner.workspace}}/build - run: cmake --build . --config $BUILD_TYPE - - - name: Test Lua 5.1 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests51 - ./LuaBridgeTests51Noexcept - - - name: Test Lua 5.2 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests52 - ./LuaBridgeTests52Noexcept - - - name: Test Lua 5.3 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests53 - ./LuaBridgeTests53Noexcept - - - name: Test Lua 5.4 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests54 - ./LuaBridgeTests54Noexcept - - - name: Test LuaJIT - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTestsLuaJIT - ./LuaBridgeTestsLuaJITNoexcept - - - name: Test Luau - working-directory: ${{runner.workspace}}/build/Tests - run: ./LuaBridgeTestsLuau diff --git a/LuaBridge3/.github/workflows/build_macos.yml b/LuaBridge3/.github/workflows/build_macos.yml deleted file mode 100644 index 272f7a2..0000000 --- a/LuaBridge3/.github/workflows/build_macos.yml +++ /dev/null @@ -1,62 +0,0 @@ -name: Build MacOS - -on: [push] - -env: - BUILD_TYPE: Release - -jobs: - build: - runs-on: macos-latest - steps: - - - uses: actions/checkout@v2 - with: - submodules: true - - - uses: seanmiddleditch/gha-setup-ninja@master - - - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Configure - working-directory: ${{runner.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -G Ninja - - - name: Build - working-directory: ${{runner.workspace}}/build - run: cmake --build . --config $BUILD_TYPE - - - name: Test Lua 5.1 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests51 - ./LuaBridgeTests51Noexcept - - - name: Test Lua 5.2 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests52 - ./LuaBridgeTests52Noexcept - - - name: Test Lua 5.3 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests53 - ./LuaBridgeTests53Noexcept - - - name: Test Lua 5.4 - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTests54 - ./LuaBridgeTests54Noexcept - - - name: Test LuaJIT - working-directory: ${{runner.workspace}}/build/Tests - run: | - ./LuaBridgeTestsLuaJIT - ./LuaBridgeTestsLuaJITNoexcept - - - name: Test Luau - working-directory: ${{runner.workspace}}/build/Tests - run: ./LuaBridgeTestsLuau diff --git a/LuaBridge3/.github/workflows/build_windows.yml b/LuaBridge3/.github/workflows/build_windows.yml deleted file mode 100644 index e5f39f5..0000000 --- a/LuaBridge3/.github/workflows/build_windows.yml +++ /dev/null @@ -1,72 +0,0 @@ -name: Build Windows - -on: [push] - -env: - BUILD_TYPE: Release - -jobs: - build: - runs-on: windows-latest - steps: - - - uses: actions/checkout@v2 - with: - submodules: true - - - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Configure - shell: bash - working-directory: ${{runner.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE - - - name: Build - working-directory: ${{runner.workspace}}/build - shell: bash - run: cmake --build . --config $BUILD_TYPE --parallel 4 - - - name: Test Lua 5.1 - working-directory: ${{runner.workspace}}/build/Tests/Release - shell: bash - run: | - ./LuaBridgeTests51.exe - ./LuaBridgeTests51Noexcept.exe - - - name: Test Lua 5.2 - working-directory: ${{runner.workspace}}/build/Tests/Release - shell: bash - run: | - ./LuaBridgeTests52.exe - ./LuaBridgeTests52Noexcept.exe - - - name: Test Lua 5.3 - working-directory: ${{runner.workspace}}/build/Tests/Release - shell: bash - run: | - ./LuaBridgeTests53.exe - ./LuaBridgeTests53Noexcept.exe - - - name: Test Lua 5.4 - working-directory: ${{runner.workspace}}/build/Tests/Release - shell: bash - run: | - ./LuaBridgeTests54.exe - ./LuaBridgeTests54Noexcept.exe - - #- name: Test LuaJIT - # working-directory: ${{runner.workspace}}/build/Tests/Release - # shell: bash - # run: ./LuaBridgeTestsLuaJIT.exe - - - name: Test LuaJIT - No Exceptions - working-directory: ${{runner.workspace}}/build/Tests/Release - shell: bash - run: | - ./LuaBridgeTestsLuaJITNoexcept.exe - - - name: Test Luau - working-directory: ${{runner.workspace}}/build/Tests/Release - shell: bash - run: ./LuaBridgeTestsLuau.exe diff --git a/LuaBridge3/.github/workflows/codeql-analysis.yml b/LuaBridge3/.github/workflows/codeql-analysis.yml deleted file mode 100644 index ee1e785..0000000 --- a/LuaBridge3/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: "CodeQL" - -on: - push: - branches: [ master ] - paths: - - "**/Source/*" - - "**/Source/**/*" - -env: - BUILD_TYPE: Release - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - language: [ 'cpp' ] - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - submodules: true - - - name: Install Ninja - uses: seanmiddleditch/gha-setup-ninja@master - - - name: Initialize CodeQL - uses: github/codeql-action/init@v2 - with: - languages: ${{ matrix.language }} - - - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Configure CMake - shell: bash - working-directory: ${{runner.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -G Ninja - - - name: Build - working-directory: ${{runner.workspace}}/build - shell: bash - run: cmake --build . --config $BUILD_TYPE - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 diff --git a/LuaBridge3/.github/workflows/coverage.yml b/LuaBridge3/.github/workflows/coverage.yml deleted file mode 100644 index 7f3116c..0000000 --- a/LuaBridge3/.github/workflows/coverage.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Code Coverage - -on: [push] - -jobs: - build: - runs-on: ubuntu-latest - steps: - - - uses: actions/checkout@v2 - with: - submodules: true - - - name: Install lcov - run: sudo apt-get install -y lcov - - - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build - - - name: Configure - working-directory: ${{runner.workspace}}/build - run: cmake $GITHUB_WORKSPACE - - - name: Build - working-directory: ${{runner.workspace}}/build - run: cmake --build . --target LuaBridgeTestsCoverage -- -j4 - - - name: Coveralls - uses: coverallsapp/github-action@master - with: - path-to-lcov: ${{runner.workspace}}/build/coverage/Merged.info - github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/LuaBridge3/.gitignore b/LuaBridge3/.gitignore deleted file mode 100644 index d76fea2..0000000 --- a/LuaBridge3/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -Documentation -*.swp -Makefile -CMakeCache.txt -CMakeFiles/ -build/ -*.dir/ -*.sln -*.vcxproj -*.vcxproj.filters -*.vcxproj.user -.vs/ -.vscode -.DS_store diff --git a/LuaBridge3/.gitmodules b/LuaBridge3/.gitmodules deleted file mode 100644 index 649ea63..0000000 --- a/LuaBridge3/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "ThirdParty/luau"] - path = ThirdParty/luau - url = https://github.com/kunitoki/luau.git -[submodule "ThirdParty/googletest"] - path = ThirdParty/googletest - url = https://github.com/kunitoki/googletest.git \ No newline at end of file diff --git a/LuaBridge3/CHANGES.md b/LuaBridge3/CHANGES.md deleted file mode 100644 index 8dab62d..0000000 --- a/LuaBridge3/CHANGES.md +++ /dev/null @@ -1,160 +0,0 @@ -## Master - -## Version 3.0 - -* Moved to C++17 as minimum supported standard C++ version. -* Reworked the whole library to be able to use it without c++ exceptions enabled. -* Breaking Change: The method `Stack::push` now takes a `std::error_code&` as last parameter and returns a `bool`. -* Breaking Change: The class `LuaException` has been reworked and it now take a `std::error_code` instead of a int. -* Breaking Change: The class `LuaException` is now thrown if a unregistered class is pushed via the Stack class, also when calling `LuaRef::operator()`, but only if exceptions are enabled. -* Breaking Change: `LuaRef::operator()` now returns the class `LuaResult`, where it is possible to obtain the call results or error message. -* Breaking Change: LuaBridge does not silently enable exceptions when calling `getGlobalNamespace`. Call `enableExceptions(lua_State*)` if you want to enable them explicitly. -* Breaking Change: Removed `RefCounterPtr`, maintaining the reference counts in a unsynchronized global table is not production quality. -* Breaking Change: Removed `Class::addStaticData`, it was just an alias for `Class::addStaticProperty`. -* Breaking Change: Removed `Class::addCFunction`, it was just an alias for `Class::addFunction`. -* Breaking Change: Removed `Class::addStaticCFunction`, it was just an alias for `Class::addStaticFunction`. -* Allow specifying a non virtual base class method when declaring class members (functions or variables) not exposed in the inherited class. -* Allow using capturing lambdas in `Namespace::addFunction`, `Namespace::addProperty`, `Class::addFunction`, `Class::addStaticFunction`, `Class::addProperty` and `Class::addStaticProperty`. -* Added support for specifying factory functor in `Class::addConstructor` to do placement new of the object instance. -* Added `Namespace::addVariable` to allow adding a modifiable value by copy into the namespace without incurring in function calls or metatables generation. -* Added `getNamespaceFromStack` function to construct a namespace object from a table on the stack. -* Added `registerMainThread` function especially useful when using lua 5.1 to register the main lua thread. -* Added `std::shared_ptr` support for types intrusively deriving from `std::enable_shared_from_this`. -* Added `Class::addFunction` overload taking a `lua_CFunction` as if it were a member. -* Added `Class::addIndexMetaMethod` to allow register `__index` metamethod fallback on a registered class. -* Added `Class::addNewIndexMetaMethod` to allow register `__newindex` metamethod fallback on a registered class. -* Added `LuaRef::isValid` to check when the reference is a LUA_NOREF. -* Added `LuaRef::isCallable` to check when the reference is a function or has a `__call` metamethod. -* Added `LuaException::state` to return the `lua_State` associated with the exception. -* Added support for `std::byte` as stack value type. -* Added support for `std::string_view` as stack value type. -* Added support for `std::tuple` as stack value type. -* Added support for `std::optional` as stack value type. -* Added support for `std::set` as stack value type by using `LuaBridge/Set.h`. -* Added support to `LuaRef` for being hashed with `std::hash` (`LuaRef` properly usable in `std::unordered_map`). -* Added single header amalgamated distribution file, to simplify including in projects. -* Added more asserts for functions and property names. -* Renamed `luabridge::Nil` to `luabridge::LuaNil` to allow including LuaBridge in Obj-C sources. -* Removed the limitation of maximum 8 parameters in functions. -* Removed the limitation of maximum 8 parameters in constructors. -* Removed `Class::addData`, it was just an alias for `Class::addProperty`. -* Removed `TypeList` from loki, using parameter packs and `std::tuple` with `std::apply`. -* Removed juce traces from unit tests, simplified unit tests runs. -* Changed all generic functions in `LuaRef` and `TableItem` to accept arguments by const reference instead of by copy. -* Fixed issue when `LuaRef::cast<>` fails with exceptions enabled, popping from the now empty stack could trigger the panic handler twice. -* Fixed unaligned access in user allocated member pointers in 64bit machines reported by ASAN. -* Fixed access of `LuaRef` in garbage collected `lua_thread`. -* Included testing against Luau VM -* Bumped lua 5.2.x in unit tests from lua 5.2.0 to 5.2.4. -* Bumped lua 5.4.x in unit tests from lua 5.4.1 to 5.4.4. -* Run against lua 5.3.6 and 5.4.4 in unit tests. -* Run against Luau and LuaJIT in unit tests. -* Converted the manual from html to markdown. -* Small improvements to code and doxygen comments readability. - -## Version 2.6 - -* Added namespace `addFunction()` accepting `std::function` (C++11 only). -* Added class `addStaticFunction()` accepting `std::function` (C++11 only). -* Update the Doxygen documentation. -* Add brief API reference into the manual. -* Hide non-public `luabridge` members into the `detail` namespace. -* Fix stack cleanup by `LuaRef::isInstance()` method. - -## Version 2.5 - -* Introduce stack `isInstance()` method. -* Introduce LuaRef `isInstance()` method. -* Added a convenience `isInstance()` function template. - -## Version 2.4.1 - -* Do not call the object destructor then its constructor throws. - -## Version 2.4 - -* String stack get specialization doesn't change the stack value anymore. -* Added namespace `addProperty()` accepting C-functions. -* Introduce enableExceptions function. - -## Version 2.3.2 - -* Fixed registration continuation for an already registered class. - -## Version 2.3.1 - -* Fixed registration continuation issues. - -## Version 2.3 - -* Added class `addFunction()` accepting proxy functions (C++11 only). -* Added class `addFunction()` accepting `std::function` (C++11 only). -* Added class `addProperty()` accepting functions with lua_State* parameter. -* Added class `addProperty()` accepting `std::function` (C++11 only). -* Added stack traits for `std::unordered_map` (`UnorderedMap.h`). -* Now using lightuserdata for function pointers. - -## Version 2.2.2 - -* Performance optimization. - -## Version 2.2.1 - -* Refactored namespace and class handling. - -## Version 2.2 - -* Refactored stack operations. -* Handle exceptions in stack operations. - -## Version 2.1.2 - -* Added `operator==` and `operator!=` for `RefCountedPtr` template. - -## Version 2.1.1 - -* Support for `__stdcall` function pointers. - -## Version 2.1 - -* Added stack traits for `std::vector` (`Vector.h`). -* Added stack traits for `std::list` (`List.h`). -* Added stack traits for `std::map` (`Map.h`). -* Added ability to use `LuaRef` objects as an `std::map` keys. -* Fixed some manual errata. - -## Version 2.0 - -* Numerous bug fixes. -* Feature Requests from Github issues. -* Added `LuaRef` object. -* Rewritten documentation. - -## Version 1.1.0 - -* Split code up into several files. -* Add Lua table and type representations (based on Nigel's code). -* Reformat documentation as external HTML file. - -## Version 1.0.3 - -* Pass `nil` to Lua when a null pointer is passed for objects with shared lifetime. - -## Version 1.0.2 - -* Option to hide metatables selectable at runtime, default to true. -* `addStaticMethod()` renamed to `addStaticFunction()` for consistency. -* `addMethod()` renamed to `addFunction()` for consistency. -* `addCFunction()` registrations. -* Convert null pointers to and from `nil`. -* Small performance increase in class pointer extraction. - -## Version 1.0.1 - -* Backward compatibility with Lua 5.1.x. - -## Version 1.0 - -* Explicit lifetime management models. -* Generalized containers. -* Single header distribution. diff --git a/LuaBridge3/CMakeLists.txt b/LuaBridge3/CMakeLists.txt deleted file mode 100644 index 9ab65f4..0000000 --- a/LuaBridge3/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -cmake_minimum_required (VERSION 3.5) - -project (LuaBridge) - -include (CMakeDependentOption) -include (CTest) - -enable_testing() - -set (CMAKE_CXX_STANDARD 17) -set (CMAKE_CXX_STANDARD_REQUIRED ON) -set (CMAKE_CXX_EXTENSIONS OFF) - -find_program (FIND_EXECUTABLE find) -find_program (LCOV_EXECUTABLE lcov) -find_program (GENHTML_EXECUTABLE genhtml) - -cmake_dependent_option (LUABRIDGE_TESTING "Build tests" ON "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF) -cmake_dependent_option (LUABRIDGE_COVERAGE "Enable coverage" ON "LUABRIDGE_TESTING;FIND_EXECUTABLE;LCOV_EXECUTABLE;GENHTML_EXECUTABLE" OFF) - -add_subdirectory (Source) - -if (LUABRIDGE_TESTING) - set (gtest_force_shared_crt ON CACHE BOOL "Use /MD and /MDd" FORCE) - add_subdirectory (ThirdParty/googletest) - add_subdirectory (Tests) -endif () - -add_custom_target (Documentation SOURCES - CHANGES.md - README.md - Manual.md - Doxyfile -) diff --git a/LuaBridge3/Distribution/LuaBridge/LuaBridge.h b/LuaBridge3/Distribution/LuaBridge/LuaBridge.h deleted file mode 100644 index 89e69c8..0000000 --- a/LuaBridge3/Distribution/LuaBridge/LuaBridge.h +++ /dev/null @@ -1,9746 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2023, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -// clang-format off - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -// Begin File: Source/LuaBridge/detail/Config.h - -#if !(__cplusplus >= 201703L || (defined(_MSC_VER) && _HAS_CXX17)) -#error LuaBridge 3 requires a compliant C++17 compiler, or C++17 has not been enabled ! -#endif - -#if defined(_MSC_VER) -#if _CPPUNWIND || _HAS_EXCEPTIONS -#define LUABRIDGE_HAS_EXCEPTIONS 1 -#else -#define LUABRIDGE_HAS_EXCEPTIONS 0 -#endif -#elif defined(__clang__) -#if __EXCEPTIONS && __has_feature(cxx_exceptions) -#define LUABRIDGE_HAS_EXCEPTIONS 1 -#else -#define LUABRIDGE_HAS_EXCEPTIONS 0 -#endif -#elif defined(__GNUC__) -#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) -#define LUABRIDGE_HAS_EXCEPTIONS 1 -#else -#define LUABRIDGE_HAS_EXCEPTIONS 0 -#endif -#endif - -#if defined(LUAU_FASTMATH_BEGIN) -#define LUABRIDGE_ON_LUAU 1 -#elif defined(LUAJIT_VERSION) -#define LUABRIDGE_ON_LUAJIT 1 -#elif defined(LUA_VERSION_NUM) -#define LUABRIDGE_ON_LUA 1 -#else -#error "Lua headers must be included prior to LuaBridge ones" -#endif - -#if defined(__OBJC__) -#define LUABRIDGE_ON_OBJECTIVE_C 1 -#endif - -#if !defined(LUABRIDGE_SAFE_STACK_CHECKS) -#define LUABRIDGE_SAFE_STACK_CHECKS 1 -#endif - -#if !defined(LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE) && LUABRIDGE_HAS_EXCEPTIONS -#define LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE 1 -#endif - - -// End File: Source/LuaBridge/detail/Config.h - -// Begin File: Source/LuaBridge/detail/LuaHelpers.h - -namespace luabridge { - -template -constexpr void unused(Args&&...) -{ -} - -#if LUABRIDGE_ON_LUAU -inline int luaL_ref(lua_State* L, int idx) -{ - assert(idx == LUA_REGISTRYINDEX); - - const int ref = lua_ref(L, -1); - - lua_pop(L, 1); - - return ref; -} - -inline void luaL_unref(lua_State* L, int idx, int ref) -{ - unused(idx); - - lua_unref(L, ref); -} - -template -inline void* lua_newuserdata_x(lua_State* L, size_t sz) -{ - return lua_newuserdatadtor(L, sz, [](void* x) - { - T* object = static_cast(x); - object->~T(); - }); -} - -inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn) -{ - lua_pushcfunction(L, fn, ""); -} - -inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, int n) -{ - lua_pushcclosure(L, fn, "", n); -} - -inline int lua_error_x(lua_State* L) -{ - lua_error(L); - return 0; -} - -inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar) -{ - return lua_getinfo(L, level, what, ar); -} - -#else -using ::luaL_ref; -using ::luaL_unref; - -template -inline void* lua_newuserdata_x(lua_State* L, size_t sz) -{ - return lua_newuserdata(L, sz); -} - -inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn) -{ - lua_pushcfunction(L, fn); -} - -inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, int n) -{ - lua_pushcclosure(L, fn, n); -} - -inline int lua_error_x(lua_State* L) -{ - return lua_error(L); -} - -inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar) -{ - lua_getstack(L, level, ar); - return lua_getinfo(L, what, ar); -} - -#endif - -#if LUA_VERSION_NUM < 503 -inline lua_Number to_numberx(lua_State* L, int idx, int* isnum) -{ - lua_Number n = lua_tonumber(L, idx); - - if (isnum) - *isnum = (n != 0 || lua_isnumber(L, idx)); - - return n; -} - -inline lua_Integer to_integerx(lua_State* L, int idx, int* isnum) -{ - int ok = 0; - lua_Number n = to_numberx(L, idx, &ok); - - if (ok) - { - const auto int_n = static_cast(n); - if (n == static_cast(int_n)) - { - if (isnum) - *isnum = 1; - - return int_n; - } - } - - if (isnum) - *isnum = 0; - - return 0; -} - -#endif - -#if LUA_VERSION_NUM < 502 -using lua_Unsigned = std::make_unsigned_t; - -#if ! LUABRIDGE_ON_LUAU -inline int lua_absindex(lua_State* L, int idx) -{ - if (idx > LUA_REGISTRYINDEX && idx < 0) - return lua_gettop(L) + idx + 1; - else - return idx; -} -#endif - -inline void lua_rawgetp(lua_State* L, int idx, const void* p) -{ - idx = lua_absindex(L, idx); - luaL_checkstack(L, 1, "not enough stack slots"); - lua_pushlightuserdata(L, const_cast(p)); - lua_rawget(L, idx); -} - -inline void lua_rawsetp(lua_State* L, int idx, const void* p) -{ - idx = lua_absindex(L, idx); - luaL_checkstack(L, 1, "not enough stack slots"); - lua_pushlightuserdata(L, const_cast(p)); - lua_insert(L, -2); - lua_rawset(L, idx); -} - -#define LUA_OPEQ 1 -#define LUA_OPLT 2 -#define LUA_OPLE 3 - -inline int lua_compare(lua_State* L, int idx1, int idx2, int op) -{ - switch (op) - { - case LUA_OPEQ: - return lua_equal(L, idx1, idx2); - - case LUA_OPLT: - return lua_lessthan(L, idx1, idx2); - - case LUA_OPLE: - return lua_equal(L, idx1, idx2) || lua_lessthan(L, idx1, idx2); - - default: - return 0; - } -} - -inline int get_length(lua_State* L, int idx) -{ - return static_cast(lua_objlen(L, idx)); -} - -#else -inline int get_length(lua_State* L, int idx) -{ - lua_len(L, idx); - const int len = static_cast(luaL_checknumber(L, -1)); - lua_pop(L, 1); - return len; -} - -#endif - -#ifndef LUA_OK -#define LUABRIDGE_LUA_OK 0 -#else -#define LUABRIDGE_LUA_OK LUA_OK -#endif - -template -std::error_code throw_or_error_code(ErrorType error) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - throw T(makeErrorCode(error).message().c_str()); -#else - return makeErrorCode(error); -#endif -} - -template -std::error_code throw_or_error_code(lua_State* L, ErrorType error) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - throw T(L, makeErrorCode(error)); -#else - return unused(L), makeErrorCode(error); -#endif -} - -template -void throw_or_assert(Args&&... args) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - throw T(std::forward(args)...); -#else - unused(std::forward(args)...); - assert(false); -#endif -} - -template -void pushunsigned(lua_State* L, T value) -{ - static_assert(std::is_unsigned_v); - - lua_pushinteger(L, static_cast(value)); -} - -inline lua_Number tonumber(lua_State* L, int idx, int* isnum) -{ -#if ! LUABRIDGE_ON_LUAU && LUA_VERSION_NUM > 502 - return lua_tonumberx(L, idx, isnum); -#else - return to_numberx(L, idx, isnum); -#endif -} - -inline lua_Integer tointeger(lua_State* L, int idx, int* isnum) -{ -#if ! LUABRIDGE_ON_LUAU && LUA_VERSION_NUM > 502 - return lua_tointegerx(L, idx, isnum); -#else - return to_integerx(L, idx, isnum); -#endif -} - -inline constexpr char main_thread_name[] = "__luabridge_main_thread"; - -inline void register_main_thread(lua_State* threadL) -{ -#if LUA_VERSION_NUM < 502 - if (threadL == nullptr) - lua_pushnil(threadL); - else - lua_pushthread(threadL); - - lua_setglobal(threadL, main_thread_name); -#else - unused(threadL); -#endif -} - -inline lua_State* main_thread(lua_State* threadL) -{ -#if LUA_VERSION_NUM < 502 - lua_getglobal(threadL, main_thread_name); - if (lua_isthread(threadL, -1)) - { - auto L = lua_tothread(threadL, -1); - lua_pop(threadL, 1); - return L; - } - assert(false); - lua_pop(threadL, 1); - return threadL; -#else - lua_rawgeti(threadL, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); - lua_State* L = lua_tothread(threadL, -1); - lua_pop(threadL, 1); - return L; -#endif -} - -inline void rawgetfield(lua_State* L, int index, char const* key) -{ - assert(lua_istable(L, index)); - index = lua_absindex(L, index); - lua_pushstring(L, key); - lua_rawget(L, index); -} - -inline void rawsetfield(lua_State* L, int index, char const* key) -{ - assert(lua_istable(L, index)); - index = lua_absindex(L, index); - lua_pushstring(L, key); - lua_insert(L, -2); - lua_rawset(L, index); -} - -[[nodiscard]] inline bool isfulluserdata(lua_State* L, int index) -{ - return lua_isuserdata(L, index) && !lua_islightuserdata(L, index); -} - -[[nodiscard]] inline bool equalstates(lua_State* L1, lua_State* L2) -{ - return lua_topointer(L1, LUA_REGISTRYINDEX) == lua_topointer(L2, LUA_REGISTRYINDEX); -} - -[[nodiscard]] inline int table_length(lua_State* L, int index) -{ - assert(lua_istable(L, index)); - - int items_count = 0; - - lua_pushnil(L); - while (lua_next(L, index) != 0) - { - ++items_count; - - lua_pop(L, 1); - } - - return items_count; -} - -template -[[nodiscard]] T* align(void* ptr) noexcept -{ - const auto address = reinterpret_cast(ptr); - - const auto offset = address % alignof(T); - const auto aligned_address = (offset == 0) ? address : (address + alignof(T) - offset); - - return reinterpret_cast(aligned_address); -} - -template -[[nodiscard]] constexpr size_t maximum_space_needed_to_align() noexcept -{ - return sizeof(T) + alignof(T) - 1; -} - -template -int lua_deleteuserdata_aligned(lua_State* L) -{ - assert(isfulluserdata(L, 1)); - - T* aligned = align(lua_touserdata(L, 1)); - aligned->~T(); - - return 0; -} - -template -void* lua_newuserdata_aligned(lua_State* L, Args&&... args) -{ -#if LUABRIDGE_ON_LUAU - void* pointer = lua_newuserdatadtor(L, maximum_space_needed_to_align(), [](void* x) - { - T* aligned = align(x); - aligned->~T(); - }); -#else - void* pointer = lua_newuserdata_x(L, maximum_space_needed_to_align()); - - lua_newtable(L); - lua_pushcfunction_x(L, &lua_deleteuserdata_aligned); - rawsetfield(L, -2, "__gc"); - lua_setmetatable(L, -2); -#endif - - T* aligned = align(pointer); - - new (aligned) T(std::forward(args)...); - - return pointer; -} - -inline int raise_lua_error(lua_State *L, const char *fmt, ...) -{ - va_list argp; - va_start(argp, fmt); - - bool pushed_error = false; - for (int level = 2; level > 0; --level) - { - lua_Debug ar; - -#if LUABRIDGE_ON_LUAU - if (lua_getinfo(L, level, "sl", &ar) == 0) - continue; -#else - if (lua_getstack(L, level, &ar) == 0 || lua_getinfo(L, "Sl", &ar) == 0) - continue; -#endif - - if (ar.currentline <= 0) - continue; - - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); - pushed_error = true; - - break; - } - - if (! pushed_error) - lua_pushliteral(L, ""); - - lua_pushvfstring(L, fmt, argp); - va_end(argp); - lua_concat(L, 2); - - return lua_error_x(L); -} - -template -constexpr bool is_integral_representable_by(T value) -{ - constexpr bool same_signedness = (std::is_unsigned_v && std::is_unsigned_v) - || (!std::is_unsigned_v && !std::is_unsigned_v); - - if constexpr (sizeof(T) == sizeof(U)) - { - if constexpr (same_signedness) - return true; - - if constexpr (std::is_unsigned_v) - return value <= static_cast(std::numeric_limits::max()); - - return value >= static_cast(std::numeric_limits::min()) - && static_cast(value) <= std::numeric_limits::max(); - } - - if constexpr (sizeof(T) < sizeof(U)) - { - return static_cast(value) >= std::numeric_limits::min() - && static_cast(value) <= std::numeric_limits::max(); - } - - if constexpr (std::is_unsigned_v) - return value <= static_cast(std::numeric_limits::max()); - - return value >= static_cast(std::numeric_limits::min()) - && value <= static_cast(std::numeric_limits::max()); -} - -template -bool is_integral_representable_by(lua_State* L, int index) -{ - int isValid = 0; - - const auto value = tointeger(L, index, &isValid); - - return isValid ? is_integral_representable_by(value) : false; -} - -template -constexpr bool is_floating_point_representable_by(T value) -{ - if constexpr (sizeof(T) == sizeof(U)) - return true; - - if constexpr (sizeof(T) < sizeof(U)) - return static_cast(value) >= -std::numeric_limits::max() - && static_cast(value) <= std::numeric_limits::max(); - - return value >= static_cast(-std::numeric_limits::max()) - && value <= static_cast(std::numeric_limits::max()); -} - -template -bool is_floating_point_representable_by(lua_State* L, int index) -{ - int isValid = 0; - - const auto value = tonumber(L, index, &isValid); - - return isValid ? is_floating_point_representable_by(value) : false; -} - -} - - -// End File: Source/LuaBridge/detail/LuaHelpers.h - -// Begin File: Source/LuaBridge/detail/Errors.h - -namespace luabridge { - -namespace detail { - -static inline constexpr char error_lua_stack_overflow[] = "stack overflow"; - -} - -enum class ErrorCode -{ - ClassNotRegistered = 1, - - LuaStackOverflow, - - LuaFunctionCallFailed, - - IntegerDoesntFitIntoLuaInteger, - - FloatingPointDoesntFitIntoLuaNumber, - - InvalidTypeCast, - - InvalidTableSizeInCast -}; - -namespace detail { -struct ErrorCategory : std::error_category -{ - const char* name() const noexcept override - { - return "luabridge"; - } - - std::string message(int ev) const override - { - switch (static_cast(ev)) - { - case ErrorCode::ClassNotRegistered: - return "The class is not registered in LuaBridge"; - - case ErrorCode::LuaStackOverflow: - return "The lua stack has overflow"; - - case ErrorCode::LuaFunctionCallFailed: - return "The lua function invocation raised an error"; - - case ErrorCode::IntegerDoesntFitIntoLuaInteger: - return "The native integer can't fit inside a lua integer"; - - case ErrorCode::FloatingPointDoesntFitIntoLuaNumber: - return "The native floating point can't fit inside a lua number"; - - case ErrorCode::InvalidTypeCast: - return "The lua object can't be casted to desired type"; - - case ErrorCode::InvalidTableSizeInCast: - return "The lua table has different size than expected"; - - default: - return "Unknown error"; - } - } - - static const ErrorCategory& getInstance() noexcept - { - static ErrorCategory category; - return category; - } -}; -} - -inline std::error_code makeErrorCode(ErrorCode e) -{ - return { static_cast(e), detail::ErrorCategory::getInstance() }; -} - -inline std::error_code make_error_code(ErrorCode e) -{ - return { static_cast(e), detail::ErrorCategory::getInstance() }; -} -} - -namespace std { -template <> struct is_error_code_enum : true_type {}; -} - - -// End File: Source/LuaBridge/detail/Errors.h - -// Begin File: Source/LuaBridge/detail/Expected.h - -#if LUABRIDGE_HAS_EXCEPTIONS -#endif - -namespace luabridge { -namespace detail { -using std::swap; - -template -T* construct_at(T* ptr, Args&&... args) noexcept(std::is_nothrow_constructible::value) -{ - return static_cast(::new (const_cast(static_cast(ptr))) T(std::forward(args)...)); -} - -template -struct is_swappable_with_impl : std::false_type -{ -}; - -template -struct is_swappable_with_impl(), std::declval()))>> - : std::true_type -{ -}; - -template -struct is_nothrow_swappable_with_impl -{ - static constexpr bool value = noexcept(swap(std::declval(), std::declval())) && noexcept(swap(std::declval(), std::declval())); - - using type = std::bool_constant; -}; - -template -struct is_swappable_with - : std::conjunction< - is_swappable_with_impl, std::add_lvalue_reference_t>, - is_swappable_with_impl, std::add_lvalue_reference_t>>::type -{ -}; - -template -struct is_nothrow_swappable_with - : std::conjunction, is_nothrow_swappable_with_impl>::type -{ -}; - -template -struct is_nothrow_swappable - : std::is_nothrow_swappable_with, std::add_lvalue_reference_t> -{ -}; -} - -template -class Expected; - -struct UnexpectType -{ - constexpr UnexpectType() = default; -}; - -static constexpr const auto& unexpect = UnexpectType(); - -namespace detail { -template , bool = (std::is_void_v || std::is_trivial_v) && std::is_trivial_v> -union expected_storage -{ -public: - template >> - constexpr expected_storage() noexcept - : value_() - { - } - - template - constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept - : value_(std::forward(args)...) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept - : error_(std::forward(args)...) - { - } - - ~expected_storage() = default; - - constexpr const T& value() const noexcept - { - return value_; - } - - constexpr T& value() noexcept - { - return value_; - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - T value_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr expected_storage() noexcept - : dummy_(0) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept - : error_(std::forward(args)...) - { - } - - ~expected_storage() = default; - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - char dummy_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr expected_storage() noexcept(std::is_nothrow_default_constructible_v) - : value_() - { - } - - template - constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : value_(std::forward(args)...) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : error_(std::forward(args)...) - { - } - - ~expected_storage() - { - } - - constexpr const T& value() const noexcept - { - return value_; - } - - constexpr T& value() noexcept - { - return value_; - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - T value_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr explicit expected_storage() noexcept - : dummy_(0) - { - } - - template - constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : value_(std::forward(args)...) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : error_(std::forward(args)...) - { - } - - ~expected_storage() - { - } - - constexpr const T& value() const noexcept - { - return value_; - } - - constexpr T& value() noexcept - { - return value_; - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - char dummy_; - T value_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr expected_storage() noexcept - : dummy_(0) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : error_(std::forward(args)...) - { - } - - ~expected_storage() - { - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - char dummy_; - E error_; -}; - -template -class ExpectedBaseTrivial -{ - using this_type = ExpectedBaseTrivial; - -protected: - using storage_type = expected_storage; - - constexpr ExpectedBaseTrivial() noexcept - : valid_(true) - { - } - - template - constexpr ExpectedBaseTrivial(std::in_place_t, Args&&... args) noexcept - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseTrivial(UnexpectType, Args&&... args) noexcept - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseTrivial(const ExpectedBaseTrivial& other) noexcept - { - if (other.valid_) - { - construct(std::in_place, other.value()); - } - else - { - construct(unexpect, other.error()); - } - } - - ExpectedBaseTrivial(ExpectedBaseTrivial&& other) noexcept - { - if (other.valid_) - { - construct(std::in_place, std::move(other.value())); - } - else - { - construct(unexpect, std::move(other.error())); - } - } - - ~ExpectedBaseTrivial() noexcept = default; - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept - { - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) - { - if (other.valid_) - { - construct(std::in_place, other.value()); - } - else - { - construct(unexpect, other.error()); - } - } - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) - { - if (other.valid_) - { - construct(std::in_place, std::move(other.value())); - } - else - { - construct(unexpect, std::move(other.error())); - } - } - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) = delete; - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) - { - if (other.valid_) - { - construct(std::in_place, std::move(other.value())); - } - else - { - construct(unexpect, std::move(other.error())); - } - } - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - template >> - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) - { - if (other.valid_) - { - construct(std::in_place, other.value()); - } - else - { - construct(unexpect, other.error()); - } - } - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) = delete; - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - template >> - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) = delete; - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) = delete; - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -using ExpectedBase = std::conditional_t< - (std::is_void_v || std::is_trivially_destructible_v) && std::is_trivially_destructible_v, - ExpectedBaseTrivial, - ExpectedBaseNonTrivial>; - -} - -template -class Unexpected -{ - static_assert(!std::is_reference_v && !std::is_void_v, "Unexpected type can't be a reference or void"); - -public: - Unexpected() = delete; - - constexpr explicit Unexpected(E&& e) noexcept(std::is_nothrow_move_constructible_v) - : error_(std::move(e)) - { - } - - constexpr explicit Unexpected(const E& e) noexcept(std::is_nothrow_copy_constructible_v) - : error_(e) - { - } - - constexpr const E& value() const& noexcept - { - return error_; - } - - constexpr E& value() & noexcept - { - return error_; - } - - constexpr const E&& value() const&& noexcept - { - return std::move(error_); - } - - constexpr E&& value() && noexcept - { - return std::move(error_); - } - -private: - E error_; -}; - -template -constexpr bool operator==(const Unexpected& lhs, const Unexpected& rhs) noexcept -{ - return lhs.value() == rhs.value(); -} - -template -constexpr bool operator!=(const Unexpected& lhs, const Unexpected& rhs) noexcept -{ - return lhs.value() != rhs.value(); -} - -template -constexpr inline Unexpected> makeUnexpected(E&& error) noexcept(std::is_nothrow_constructible_v>, E>) -{ - return Unexpected>{ std::forward(error) }; -} - -#if LUABRIDGE_HAS_EXCEPTIONS -template -class BadExpectedAccess; - -template <> -class BadExpectedAccess : public std::exception -{ -public: - explicit BadExpectedAccess() noexcept - { - } -}; -template -class BadExpectedAccess : public BadExpectedAccess -{ -public: - explicit BadExpectedAccess(E error) noexcept(std::is_nothrow_constructible_v) - : error_(std::move(error)) - { - } - - const E& error() const& noexcept - { - return error_; - } - - E& error() & noexcept - { - return error_; - } - - E&& error() && noexcept - { - return std::move(error_); - } - -private: - E error_; -}; -#endif - -template -struct is_expected : std::false_type -{ -}; - -template -struct is_expected> : std::true_type -{ -}; -template -struct is_unexpected : std::false_type -{ -}; - -template -struct is_unexpected> : std::true_type -{ -}; - -template -class Expected : public detail::ExpectedBase, std::is_move_constructible_v> -{ - static_assert(!std::is_reference_v && !std::is_void_v, "Unexpected type can't be a reference or void"); - - using base_type = detail::ExpectedBase, std::is_move_constructible_v>; - using this_type = Expected; - -public: - using value_type = T; - - using error_type = E; - - using unexpected_type = Unexpected; - - template - struct rebind - { - using type = Expected; - }; - - template >> - constexpr Expected() noexcept(std::is_nothrow_default_constructible_v) - : base_type() - { - } - - constexpr Expected(const Expected& other) noexcept(std::is_nothrow_copy_constructible_v) = default; - - constexpr Expected(Expected&& other) noexcept(std::is_nothrow_move_constructible_v) = default; - - template - Expected(const Expected& other) - { - if (other.hasValue()) - { - this->construct(std::in_place, other.value()); - } - else - { - this->construct(unexpect, other.error()); - } - } - - template - Expected(Expected&& other) - { - if (other.hasValue()) - { - this->construct(std::in_place, std::move(other.value())); - } - else - { - this->construct(unexpect, std::move(other.error())); - } - } - - template && std::is_constructible_v && !std::is_same_v, std::in_place_t> && !is_expected>::value && !is_unexpected>::value, int> = 0> - constexpr Expected(U&& value) noexcept(std::is_nothrow_constructible_v) - : base_type(std::in_place, std::forward(value)) - { - } - - template - constexpr explicit Expected(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : base_type(std::in_place, std::forward(args)...) - { - } - - template - constexpr explicit Expected(std::in_place_t, std::initializer_list ilist, Args&&... args) noexcept(std::is_nothrow_constructible_v, Args...>) - : base_type(std::in_place, ilist, std::forward(args)...) - { - } - - template - constexpr Expected(const Unexpected& u) noexcept(std::is_nothrow_constructible_v) - : base_type(unexpect, u.value()) - { - } - - template - constexpr Expected(Unexpected&& u) noexcept(std::is_nothrow_constructible_v) - : base_type(unexpect, std::move(u.value())) - { - } - - template - constexpr explicit Expected(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : base_type(unexpect, std::forward(args)...) - { - } - - template - constexpr explicit Expected(UnexpectType, std::initializer_list ilist, Args&&... args) noexcept(std::is_nothrow_constructible_v, Args...>) - : base_type(unexpect, ilist, std::forward(args)...) - { - } - - Expected& operator=(const Expected& other) - { - if (other.hasValue()) - { - assign(std::in_place, other.value()); - } - else - { - assign(unexpect, other.error()); - } - - return *this; - } - - Expected& operator=(Expected&& other) - { - if (other.hasValue()) - { - assign(std::in_place, std::move(other.value())); - } - else - { - assign(unexpect, std::move(other.error())); - } - - return *this; - } - - template >::value && !is_unexpected>::value, int> = 0> - Expected& operator=(U&& value) - { - assign(std::in_place, std::forward(value)); - return *this; - } - - template - Expected& operator=(const Unexpected& u) - { - assign(unexpect, u.value()); - return *this; - } - - template - Expected& operator=(Unexpected&& u) - { - assign(unexpect, std::move(u.value())); - return *this; - } - - template - T& emplace(Args&&... args) noexcept(noexcept(std::declval().assign(std::in_place, std::forward(args)...))) - { - return assign(std::in_place, std::forward(args)...); - } - - template - T& emplace(std::initializer_list ilist, Args&&... args) noexcept(noexcept(std::declval().assign(std::in_place, ilist, std::forward(args)...))) - { - return assign(std::in_place, ilist, std::forward(args)...); - } - - void swap(Expected& other) noexcept(detail::is_nothrow_swappable::value && detail::is_nothrow_swappable::value) - { - using std::swap; - - if (hasValue()) - { - if (other.hasValue()) - { - swap(value(), other.value()); - } - else - { - E error = std::move(other.error()); - other.assign(std::in_place, std::move(value())); - assign(unexpect, std::move(error)); - } - } - else - { - if (other.hasValue()) - { - other.swap(*this); - } - else - { - swap(error(), other.error()); - } - } - } - - constexpr const T* operator->() const - { - return base_type::valuePtr(); - } - - constexpr T* operator->() - { - return base_type::valuePtr(); - } - - constexpr const T& operator*() const& - { - return value(); - } - - constexpr T& operator*() & - { - return value(); - } - - constexpr const T&& operator*() const&& - { - return std::move(value()); - } - - constexpr T&& operator*() && - { - return std::move(value()); - } - - constexpr explicit operator bool() const noexcept - { - return hasValue(); - } - - constexpr bool hasValue() const noexcept - { - return base_type::valid(); - } - - constexpr const T& value() const& - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - - return base_type::value(); - } - - constexpr T& value() & - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - - return base_type::value(); - } - - constexpr const T&& value() const&& noexcept - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - - return std::move(base_type::value()); - } - - constexpr T&& value() && - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - return std::move(base_type::value()); - } - - constexpr const E& error() const& noexcept - { - return base_type::error(); - } - - constexpr E& error() & noexcept - { - return base_type::error(); - } - - constexpr const E&& error() const&& noexcept - { - return std::move(base_type::error()); - } - - constexpr E&& error() && noexcept - { - return std::move(base_type::error()); - } - - template - constexpr T valueOr(U&& defaultValue) const& - { - return hasValue() ? value() : static_cast(std::forward(defaultValue)); - } - - template - T valueOr(U&& defaultValue) && - { - return hasValue() ? std::move(value()) : static_cast(std::forward(defaultValue)); - } - -private: - template - auto assign(Tag tag, Args&&... args) noexcept(noexcept(std::declval().destroy()) && noexcept(std::declval().construct(tag, std::forward(args)...))) - -> decltype(std::declval().construct(tag, std::forward(args)...)) - { - this->destroy(); - - return this->construct(tag, std::forward(args)...); - } -}; - -template -class Expected : public detail::ExpectedBase, std::is_move_constructible_v> -{ - static_assert(!std::is_reference_v && !std::is_void_v, "Unexpected type can't be a reference or void"); - - using base_type = detail::ExpectedBase, std::is_move_constructible_v>; - using this_type = Expected; - -public: - using value_type = void; - - using error_type = E; - - using unexpected_type = Unexpected; - - template - struct rebind - { - using type = Expected; - }; - - constexpr Expected() = default; - - constexpr Expected(const Expected& other) = default; - - constexpr Expected(Expected&& other) = default; - - template - Expected(const Expected& other) - { - if (other.hasValue()) - { - this->valid_ = true; - } - else - { - this->construct(unexpect, other.error()); - } - } - - template - Expected(Expected&& other) - { - if (other.hasValue()) - { - this->valid_ = true; - } - else - { - this->construct(unexpect, std::move(other.error())); - } - } - - template - constexpr Expected(const Unexpected& u) - : base_type(unexpect, u.value()) - { - } - - template - constexpr Expected(Unexpected&& u) - : base_type(unexpect, std::move(u.value())) - { - } - - template - constexpr explicit Expected(UnexpectType, Args&&... args) - : base_type(unexpect, std::forward(args)...) - { - } - - template - constexpr explicit Expected(UnexpectType, std::initializer_list ilist, Args&&... args) - : base_type(unexpect, ilist, std::forward(args)...) - { - } - - Expected& operator=(const Expected& other) - { - if (other.hasValue()) - { - assign(std::in_place); - } - else - { - assign(unexpect, other.error()); - } - - return *this; - } - - Expected& operator=(Expected&& other) - { - if (other.hasValue()) - { - assign(std::in_place); - } - else - { - assign(unexpect, std::move(other.error())); - } - - return *this; - } - - template - Expected& operator=(const Unexpected& u) - { - assign(unexpect, u.value()); - return *this; - } - - template - Expected& operator=(Unexpected&& u) - { - assign(unexpect, std::move(u.value())); - return *this; - } - - void swap(Expected& other) noexcept(detail::is_nothrow_swappable::value) - { - using std::swap; - - if (hasValue()) - { - if (!other.hasValue()) - { - assign(unexpect, std::move(other.error())); - other.assign(std::in_place); - } - } - else - { - if (other.hasValue()) - { - other.swap(*this); - } - else - { - swap(error(), other.error()); - } - } - } - - constexpr explicit operator bool() const noexcept - { - return hasValue(); - } - - constexpr bool hasValue() const noexcept - { - return base_type::valid(); - } - - constexpr const E& error() const& noexcept - { - return base_type::error(); - } - - constexpr E& error() & noexcept - { - return base_type::error(); - } - - constexpr const E&& error() const&& noexcept - { - return std::move(base_type::error()); - } - - constexpr E&& error() && noexcept - { - return std::move(base_type::error()); - } - -private: - template - void assign(Tag tag, Args&&... args) noexcept(noexcept(std::declval().destroy()) && noexcept(std::declval().construct(tag, std::forward(args)...))) - { - this->destroy(); - this->construct(tag, std::forward(args)...); - } -}; - -template -constexpr bool operator==(const Expected& lhs, const Expected& rhs) -{ - return (lhs && rhs) ? *lhs == *rhs : ((!lhs && !rhs) ? lhs.error() == rhs.error() : false); -} - -template -constexpr bool operator==(const Expected& lhs, const Expected& rhs) -{ - return (lhs && rhs) ? true : ((!lhs && !rhs) ? lhs.error() == rhs.error() : false); -} - -template -constexpr bool operator!=(const Expected& lhs, const Expected& rhs) -{ - return !(lhs == rhs); -} - -template -constexpr bool operator==(const Expected& lhs, const T& rhs) -{ - return lhs ? *lhs == rhs : false; -} - -template -constexpr bool operator==(const T& lhs, const Expected& rhs) -{ - return rhs == lhs; -} - -template -constexpr bool operator!=(const Expected& lhs, const T& rhs) -{ - return !(lhs == rhs); -} - -template -constexpr bool operator!=(const T& lhs, const Expected& rhs) -{ - return rhs != lhs; -} - -template -constexpr bool operator==(const Expected& lhs, const Unexpected& rhs) -{ - return lhs ? false : lhs.error() == rhs.value(); -} - -template -constexpr bool operator==(const Unexpected& lhs, const Expected& rhs) -{ - return rhs == lhs; -} - -template -constexpr bool operator!=(const Expected& lhs, const Unexpected& rhs) -{ - return !(lhs == rhs); -} - -template -constexpr bool operator!=(const Unexpected& lhs, const Expected& rhs) -{ - return rhs != lhs; -} -} - - -// End File: Source/LuaBridge/detail/Expected.h - -// Begin File: Source/LuaBridge/detail/Result.h - -namespace luabridge { - -struct Result -{ - Result() = default; - - Result(std::error_code ec) noexcept - : m_ec(ec) - { - } - - Result(const Result&) = default; - Result(Result&&) = default; - Result& operator=(const Result&) = default; - Result& operator=(Result&&) = default; - - explicit operator bool() const noexcept - { - return !m_ec; - } - - std::error_code error() const noexcept - { - return m_ec; - } - - operator std::error_code() const noexcept - { - return m_ec; - } - - std::string message() const - { - return m_ec.message(); - } - -private: - std::error_code m_ec; -}; - -template -struct TypeResult -{ - TypeResult() = default; - - template && !std::is_same_v, std::error_code>>> - TypeResult(U&& value) noexcept - : m_value(std::in_place, std::forward(value)) - { - } - - TypeResult(std::error_code ec) noexcept - : m_value(makeUnexpected(ec)) - { - } - - TypeResult(const TypeResult&) = default; - TypeResult(TypeResult&&) = default; - TypeResult& operator=(const TypeResult&) = default; - TypeResult& operator=(TypeResult&&) = default; - - explicit operator bool() const - { - return m_value.hasValue(); - } - - const T& value() const - { - return m_value.value(); - } - - T& operator*() & - { - return m_value.value(); - } - - T operator*() && - { - return std::move(m_value.value()); - } - - const T& operator*() const& - { - return m_value.value(); - } - - std::error_code error() const - { - return m_value.error(); - } - - operator std::error_code() const - { - return m_value.error(); - } - - std::string message() const - { - return m_value.error().message(); - } - -private: - Expected m_value; -}; - -template -inline bool operator==(const TypeResult& lhs, const U& rhs) noexcept -{ - return lhs ? *lhs == rhs : false; -} - -template -inline bool operator==(const U& lhs, const TypeResult& rhs) noexcept -{ - return rhs == lhs; -} - -template -inline bool operator!=(const TypeResult& lhs, const U& rhs) noexcept -{ - return !(lhs == rhs); -} - -template -inline bool operator!=(const U& lhs, const TypeResult& rhs) noexcept -{ - return !(rhs == lhs); -} - -} - - -// End File: Source/LuaBridge/detail/Result.h - -// Begin File: Source/LuaBridge/detail/LuaException.h - -namespace luabridge { - -class LuaException : public std::exception -{ -public: - - LuaException(lua_State* L, std::error_code code) - : m_L(L) - , m_code(code) - { - } - - ~LuaException() noexcept override - { - } - - const char* what() const noexcept override - { - return m_what.c_str(); - } - - static void raise(lua_State* L, std::error_code code) - { - assert(areExceptionsEnabled()); - -#if LUABRIDGE_HAS_EXCEPTIONS - throw LuaException(L, code, FromLua{}); -#else - unused(L, code); - - std::abort(); -#endif - } - - static bool areExceptionsEnabled() noexcept - { - return exceptionsEnabled(); - } - - static void enableExceptions(lua_State* L) noexcept - { - exceptionsEnabled() = true; - -#if LUABRIDGE_HAS_EXCEPTIONS && LUABRIDGE_ON_LUAJIT - lua_pushlightuserdata(L, (void*)luajitWrapperCallback); - luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON); - lua_pop(L, 1); -#endif - -#if LUABRIDGE_ON_LUAU - auto callbacks = lua_callbacks(L); - callbacks->panic = +[](lua_State* L, int) { panicHandlerCallback(L); }; -#else - lua_atpanic(L, panicHandlerCallback); -#endif - } - - lua_State* state() const { return m_L; } - -private: - struct FromLua {}; - - LuaException(lua_State* L, std::error_code code, FromLua) - : m_L(L) - , m_code(code) - { - whatFromStack(); - } - - void whatFromStack() - { - std::stringstream ss; - - const char* errorText = nullptr; - - if (lua_gettop(m_L) > 0) - { - errorText = lua_tostring(m_L, -1); - lua_pop(m_L, 1); - } - - ss << (errorText ? errorText : "Unknown error") << " (code=" << m_code.message() << ")"; - - m_what = std::move(ss).str(); - } - - static int panicHandlerCallback(lua_State* L) - { -#if LUABRIDGE_HAS_EXCEPTIONS - throw LuaException(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed), FromLua{}); -#else - unused(L); - - std::abort(); -#endif - } - -#if LUABRIDGE_HAS_EXCEPTIONS && LUABRIDGE_ON_LUAJIT - static int luajitWrapperCallback(lua_State* L, lua_CFunction f) - { - try - { - return f(L); - } - catch (const std::exception& e) - { - lua_pushstring(L, e.what()); - return lua_error_x(L); - } - } -#endif - - static bool& exceptionsEnabled() - { - static bool areExceptionsEnabled = false; - return areExceptionsEnabled; - } - - lua_State* m_L = nullptr; - std::error_code m_code; - std::string m_what; -}; - -inline void enableExceptions(lua_State* L) noexcept -{ -#if LUABRIDGE_HAS_EXCEPTIONS - LuaException::enableExceptions(L); -#else - unused(L); - - assert(false); -#endif -} - -} - - -// End File: Source/LuaBridge/detail/LuaException.h - -// Begin File: Source/LuaBridge/detail/ClassInfo.h - -#if defined __clang__ || defined __GNUC__ -#define LUABRIDGE_PRETTY_FUNCTION __PRETTY_FUNCTION__ -#define LUABRIDGE_PRETTY_FUNCTION_PREFIX '=' -#define LUABRIDGE_PRETTY_FUNCTION_SUFFIX ']' -#elif defined _MSC_VER -#define LUABRIDGE_PRETTY_FUNCTION __FUNCSIG__ -#define LUABRIDGE_PRETTY_FUNCTION_PREFIX '<' -#define LUABRIDGE_PRETTY_FUNCTION_SUFFIX '>' -#endif - -namespace luabridge { -namespace detail { - -[[nodiscard]] constexpr auto fnv1a(const char* s, std::size_t count) noexcept -{ - uint32_t seed = 2166136261u; - - for (std::size_t i = 0; i < count; ++i) - seed ^= static_cast(*s++) * 16777619u; - - if constexpr (sizeof(void*) == 8) - return static_cast(seed); - else - return seed; -} - -template -[[nodiscard]] static constexpr auto typeName() noexcept -{ - constexpr std::string_view prettyName{ LUABRIDGE_PRETTY_FUNCTION }; - - constexpr auto first = prettyName.find_first_not_of(' ', prettyName.find_first_of(LUABRIDGE_PRETTY_FUNCTION_PREFIX) + 1); - - return prettyName.substr(first, prettyName.find_last_of(LUABRIDGE_PRETTY_FUNCTION_SUFFIX) - first); -} - -template ().find_first_of('.')> -[[nodiscard]] static constexpr auto typeHash() noexcept -{ - constexpr auto stripped = typeName(); - - return fnv1a(stripped.data(), stripped.size()); -} - -[[nodiscard]] inline const void* getTypeKey() noexcept -{ - return reinterpret_cast(0x71); -} - -[[nodiscard]] inline const void* getConstKey() noexcept -{ - return reinterpret_cast(0xc07); -} - -[[nodiscard]] inline const void* getClassKey() noexcept -{ - return reinterpret_cast(0xc1a); -} - -[[nodiscard]] inline const void* getPropgetKey() noexcept -{ - return reinterpret_cast(0x6e7); -} - -[[nodiscard]] inline const void* getPropsetKey() noexcept -{ - return reinterpret_cast(0x5e7); -} - -[[nodiscard]] inline const void* getStaticKey() noexcept -{ - return reinterpret_cast(0x57a); -} - -[[nodiscard]] inline const void* getParentKey() noexcept -{ - return reinterpret_cast(0xdad); -} - -[[nodiscard]] inline const void* getIndexFallbackKey() -{ - return reinterpret_cast(0x81ca); -} - -[[nodiscard]] inline const void* getNewIndexFallbackKey() -{ - return reinterpret_cast(0x8107); -} - -template -[[nodiscard]] const void* getStaticRegistryKey() noexcept -{ - static auto value = typeHash(); - - return reinterpret_cast(value); -} - -template -[[nodiscard]] const void* getClassRegistryKey() noexcept -{ - static auto value = typeHash() ^ 1; - - return reinterpret_cast(value); -} - -template -[[nodiscard]] const void* getConstRegistryKey() noexcept -{ - static auto value = typeHash() ^ 2; - - return reinterpret_cast(value); -} -} -} - - -// End File: Source/LuaBridge/detail/ClassInfo.h - -// Begin File: Source/LuaBridge/detail/TypeTraits.h - -namespace luabridge { - -template -struct ContainerTraits -{ - using IsNotContainer = bool; - - using Type = T; -}; - -template -struct ContainerTraits> -{ - static_assert(std::is_base_of_v, T>); - - using Type = T; - - static std::shared_ptr construct(T* t) - { - return t->shared_from_this(); - } - - static T* get(const std::shared_ptr& c) - { - return c.get(); - } -}; - -namespace detail { - -template -class IsContainer -{ -private: - typedef char yes[1]; - typedef char no[2]; - - template - static constexpr no& test(typename C::IsNotContainer*); - - template - static constexpr yes& test(...); - -public: - static constexpr bool value = sizeof(test>(0)) == sizeof(yes); -}; - -} -} - - -// End File: Source/LuaBridge/detail/TypeTraits.h - -// Begin File: Source/LuaBridge/detail/Userdata.h - -namespace luabridge { -namespace detail { - -class Userdata -{ -private: - - static Userdata* getExactClass(lua_State* L, int index, const void* classKey) - { - return (void)classKey, static_cast(lua_touserdata(L, lua_absindex(L, index))); - } - - static Userdata* getClass(lua_State* L, - int index, - const void* registryConstKey, - const void* registryClassKey, - bool canBeConst) - { - index = lua_absindex(L, index); - - lua_getmetatable(L, index); - if (!lua_istable(L, -1)) - { - lua_rawgetp(L, LUA_REGISTRYINDEX, registryClassKey); - return throwBadArg(L, index); - } - - lua_rawgetp(L, -1, getConstKey()); - assert(lua_istable(L, -1) || lua_isnil(L, -1)); - - bool isConst = lua_isnil(L, -1); - if (isConst && canBeConst) - { - lua_rawgetp(L, LUA_REGISTRYINDEX, registryConstKey); - } - else - { - lua_rawgetp(L, LUA_REGISTRYINDEX, registryClassKey); - } - - lua_insert(L, -3); - lua_pop(L, 1); - - for (;;) - { - if (lua_rawequal(L, -1, -2)) - { - lua_pop(L, 2); - return static_cast(lua_touserdata(L, index)); - } - - lua_rawgetp(L, -1, getParentKey()); - - if (lua_isnil(L, -1)) - { - - lua_pop(L, 2); - return throwBadArg(L, index); - } - - lua_remove(L, -2); - } - - } - - static bool isInstance(lua_State* L, int index, const void* registryClassKey) - { - index = lua_absindex(L, index); - - int result = lua_getmetatable(L, index); - if (result == 0) - return false; - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - return false; - } - - lua_rawgetp(L, LUA_REGISTRYINDEX, registryClassKey); - lua_insert(L, -2); - - for (;;) - { - if (lua_rawequal(L, -1, -2)) - { - lua_pop(L, 2); - return true; - } - - lua_rawgetp(L, -1, getParentKey()); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 3); - return false; - } - - lua_remove(L, -2); - } - } - - static Userdata* throwBadArg(lua_State* L, int index) - { - assert(lua_istable(L, -1) || lua_isnil(L, -1)); - - const char* expected = 0; - if (lua_isnil(L, -1)) - { - expected = "unregistered class"; - } - else - { - lua_rawgetp(L, -1, getTypeKey()); - expected = lua_tostring(L, -1); - } - - const char* got = 0; - if (lua_isuserdata(L, index)) - { - lua_getmetatable(L, index); - if (lua_istable(L, -1)) - { - lua_rawgetp(L, -1, getTypeKey()); - if (lua_isstring(L, -1)) - { - got = lua_tostring(L, -1); - } - } - } - - if (!got) - { - got = lua_typename(L, lua_type(L, index)); - } - - luaL_argerror(L, index, lua_pushfstring(L, "%s expected, got %s", expected, got)); - return nullptr; - } - -public: - virtual ~Userdata() {} - - template - static Userdata* getExact(lua_State* L, int index) - { - return getExactClass(L, index, detail::getClassRegistryKey()); - } - - template - static T* get(lua_State* L, int index, bool canBeConst) - { - if (lua_isnil(L, index)) - return nullptr; - - return static_cast(getClass(L, - index, - detail::getConstRegistryKey(), - detail::getClassRegistryKey(), - canBeConst) - ->getPointer()); - } - - template - static bool isInstance(lua_State* L, int index) - { - return isInstance(L, index, detail::getClassRegistryKey()); - } - -protected: - Userdata() = default; - - void* getPointer() const noexcept - { - return m_p; - } - - void* m_p = nullptr; -}; - -template -class UserdataValue : public Userdata -{ -public: - UserdataValue(const UserdataValue&) = delete; - UserdataValue operator=(const UserdataValue&) = delete; - - ~UserdataValue() - { - if (getPointer() != nullptr) - { - getObject()->~T(); - } - } - - static UserdataValue* place(lua_State* L, std::error_code& ec) - { - auto* ud = new (lua_newuserdata_x>(L, sizeof(UserdataValue))) UserdataValue(); - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - ud->~UserdataValue(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - ec = throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - ec = makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - - return nullptr; - } - - lua_setmetatable(L, -2); - - return ud; - } - - template - static auto push(lua_State* L, const U& u) -> std::enable_if_t, Result> - { - std::error_code ec; - auto* ud = place(L, ec); - - if (!ud) - return ec; - - new (ud->getObject()) U(u); - - ud->commit(); - - return {}; - } - - template - static auto push(lua_State* L, U&& u) -> std::enable_if_t, Result> - { - std::error_code ec; - auto* ud = place(L, ec); - - if (!ud) - return ec; - - new (ud->getObject()) U(std::move(u)); - - ud->commit(); - - return {}; - } - - void commit() noexcept - { - m_p = getObject(); - } - - T* getObject() noexcept - { - - return reinterpret_cast(&m_storage); - } - -private: - - UserdataValue() noexcept - : Userdata() - { - } - - std::aligned_storage_t m_storage; -}; - -class UserdataPtr : public Userdata -{ -public: - UserdataPtr(const UserdataPtr&) = delete; - UserdataPtr operator=(const UserdataPtr&) = delete; - - template - static Result push(lua_State* L, T* ptr) - { - if (ptr) - return push(L, ptr, getClassRegistryKey()); - - lua_pushnil(L); - return {}; - } - - template - static Result push(lua_State* L, const T* ptr) - { - if (ptr) - return push(L, ptr, getConstRegistryKey()); - - lua_pushnil(L); - return {}; - } - -private: - - static Result push(lua_State* L, const void* ptr, const void* key) - { - auto* udptr = new (lua_newuserdata_x(L, sizeof(UserdataPtr))) UserdataPtr(const_cast(ptr)); - - lua_rawgetp(L, LUA_REGISTRYINDEX, key); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - udptr->~UserdataPtr(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - - return {}; - } - - explicit UserdataPtr(void* ptr) - { - - assert(ptr != nullptr); - m_p = ptr; - } -}; - -template -class UserdataValueExternal : public Userdata -{ -public: - UserdataValueExternal(const UserdataValueExternal&) = delete; - UserdataValueExternal operator=(const UserdataValueExternal&) = delete; - - ~UserdataValueExternal() - { - if (getObject() != nullptr) - m_dealloc(getObject()); - } - - template - static UserdataValueExternal* place(lua_State* L, T* obj, Dealloc dealloc, std::error_code& ec) - { - auto* ud = new (lua_newuserdata_x>(L, sizeof(UserdataValueExternal))) UserdataValueExternal(obj, dealloc); - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - ud->~UserdataValueExternal(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - ec = throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - ec = makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - - return nullptr; - } - - lua_setmetatable(L, -2); - - return ud; - } - - T* getObject() noexcept - { - return static_cast(m_p); - } - -private: - UserdataValueExternal(void* ptr, void (*dealloc)(T*)) noexcept - { - - assert(ptr != nullptr); - m_p = ptr; - - assert(dealloc != nullptr); - m_dealloc = dealloc; - } - - void (*m_dealloc)(T*) = nullptr; -}; - -template -class UserdataShared : public Userdata -{ -public: - UserdataShared(const UserdataShared&) = delete; - UserdataShared& operator=(const UserdataShared&) = delete; - - ~UserdataShared() = default; - - template - explicit UserdataShared(const U& u) : m_c(u) - { - m_p = const_cast(reinterpret_cast((ContainerTraits::get(m_c)))); - } - - template - explicit UserdataShared(U* u) : m_c(u) - { - m_p = const_cast(reinterpret_cast((ContainerTraits::get(m_c)))); - } - -private: - C m_c; -}; - -template -struct UserdataSharedHelper -{ - using T = std::remove_const_t::Type>; - - static Result push(lua_State* L, const C& c) - { - if (ContainerTraits::get(c) != nullptr) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(c); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } - - static Result push(lua_State* L, T* t) - { - if (t) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(t); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } -}; - -template -struct UserdataSharedHelper -{ - using T = std::remove_const_t::Type>; - - static Result push(lua_State* L, const C& c) - { - if (ContainerTraits::get(c) != nullptr) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(c); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getConstRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } - - static Result push(lua_State* L, T* t) - { - if (t) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(t); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getConstRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } -}; - -template -struct StackHelper -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T& t) - { - return UserdataSharedHelper::Type>>::push(L, t); - } - - static ReturnType get(lua_State* L, int index) - { - using CastType = std::remove_const_t::Type>; - - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return ContainerTraits::construct(result); - } -}; - -template -struct StackHelper -{ - static Result push(lua_State* L, const T& t) - { - return UserdataValue::push(L, t); - } - - static Result push(lua_State* L, T&& t) - { - return UserdataValue::push(L, std::move(t)); - } - - static TypeResult> get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return std::cref(*result); - } -}; - -template -struct RefStackHelper -{ - using ReturnType = TypeResult; - using T = std::remove_const_t::Type>; - - static Result push(lua_State* L, const C& t) - { - return UserdataSharedHelper::Type>>::push(L, t); - } - - static ReturnType get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return ContainerTraits::construct(result); - } -}; - -template -struct RefStackHelper -{ - using ReturnType = TypeResult>; - - static Result push(lua_State* L, const T& t) - { - return UserdataPtr::push(L, std::addressof(t)); - } - - static ReturnType get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return std::ref(*result); - } -}; - -template -struct UserdataGetter -{ - using ReturnType = TypeResult; - - static ReturnType get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return result; - } -}; - -template -struct UserdataGetter> -{ - using ReturnType = TypeResult; - - static ReturnType get(lua_State* L, int index) - { - auto result = StackHelper::value>::get(L, index); - if (! result) - return result.error(); - - return *result; - } -}; - -} - -template -struct Stack -{ - using IsUserdata = void; - - using Getter = detail::UserdataGetter; - using ReturnType = typename Getter::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, const T& value) - { - return detail::StackHelper::value>::push(L, value); - } - - [[nodiscard]] static Result push(lua_State* L, T&& value) - { - return detail::StackHelper::value>::push(L, std::move(value)); - } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) - { - return Getter::get(L, index); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return detail::Userdata::isInstance(L, index); - } -}; - -namespace detail { - -template -struct IsUserdata : std::false_type -{ -}; - -template -struct IsUserdata::IsUserdata>> : std::true_type -{ -}; - -template -struct StackOpSelector; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, T* value) { return UserdataPtr::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Userdata::get(L, index, false); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T* value) { return UserdataPtr::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Userdata::get(L, index, true); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using Helper = RefStackHelper::value>; - using ReturnType = typename Helper::ReturnType; - - static Result push(lua_State* L, T& value) { return UserdataPtr::push(L, &value); } - - static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using Helper = RefStackHelper::value>; - using ReturnType = typename Helper::ReturnType; - - static Result push(lua_State* L, const T& value) { return Helper::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -} -} - - -// End File: Source/LuaBridge/detail/Userdata.h - -// Begin File: Source/LuaBridge/detail/Stack.h - -namespace luabridge { - -class StackRestore final -{ -public: - StackRestore(lua_State* L) - : m_L(L) - , m_stackTop(lua_gettop(L)) - { - } - - ~StackRestore() - { - if (m_doRestoreStack) - lua_settop(m_L, m_stackTop); - } - - void reset() - { - m_doRestoreStack = false; - } - -private: - lua_State* const m_L = nullptr; - int m_stackTop = 0; - bool m_doRestoreStack = true; -}; - -template -struct Stack; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State*) - { - return {}; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, std::nullptr_t) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushnil(L); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (! lua_isnil(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return nullptr; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_isnil(L, index); - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static TypeResult get(lua_State* L, int) - { - return L; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, lua_CFunction f) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushcfunction_x(L, f); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (! lua_iscfunction(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return lua_tocfunction(L, index); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_iscfunction(L, index); - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, bool value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushboolean(L, value ? 1 : 0); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - return lua_toboolean(L, index) ? true : false; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_isboolean(L, index); - } -}; - -template <> -struct Stack -{ - static_assert(sizeof(std::byte) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, std::byte value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - pushunsigned(L, std::to_integer>(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, char value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, &value, 1); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TSTRING) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring(L, index, &length); - - if (str == nullptr || length != 1) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return str[0]; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TSTRING) - { - std::size_t len; - luaL_checklstring(L, index, &len); - return len == 1; - } - - return false; - } -}; - -template <> -struct Stack -{ - static_assert(sizeof(int8_t) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, int8_t value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - static_assert(sizeof(unsigned char) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, unsigned char value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - static_assert(sizeof(short) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, short value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - static_assert(sizeof(unsigned short) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, unsigned short value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, int value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, unsigned int value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, unsigned long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, long long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, unsigned long long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -#if 0 - -template <> -struct Stack<__int128_t> -{ - [[nodiscard]] static Result push(lua_State* L, __int128_t value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult<__int128_t> get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by<__int128_t>(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast<__int128_t>(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by<__int128_t>(L, index); - - return false; - } -}; - -template <> -struct Stack<__uint128_t> -{ - [[nodiscard]] static Result push(lua_State* L, __uint128_t value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult<__uint128_t> get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by<__uint128_t>(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast<__uint128_t>(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by<__uint128_t>(L, index); - - return false; - } -}; -#endif - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, float value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_floating_point_representable_by(value)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - lua_pushnumber(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_floating_point_representable_by(L, index)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - return static_cast(lua_tonumber(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_floating_point_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, double value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_floating_point_representable_by(value)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - lua_pushnumber(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_floating_point_representable_by(L, index)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - return static_cast(lua_tonumber(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_floating_point_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, long double value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_floating_point_representable_by(value)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - lua_pushnumber(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_floating_point_representable_by(L, index)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - return static_cast(lua_tonumber(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_floating_point_representable_by(L, index); - - return false; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const char* str) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (str != nullptr) - lua_pushstring(L, str); - else - lua_pushlstring(L, "", 0); - - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TSTRING) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring(L, index, &length); - if (str == nullptr) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return str; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TSTRING; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, std::string_view str) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, str.data(), str.size()); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TSTRING) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring(L, index, &length); - if (str == nullptr) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return std::string_view{ str, length }; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TSTRING; - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const std::string& str) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, str.data(), str.size()); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - std::size_t length = 0; - const char* str = nullptr; - - if (lua_type(L, index) == LUA_TSTRING) - { - str = lua_tolstring(L, index, &length); - } - else - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushvalue(L, index); - str = lua_tolstring(L, -1, &length); - lua_pop(L, 1); - } - - if (str == nullptr) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return std::string{ str, length }; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TSTRING; - } -}; - -template -struct Stack> -{ - using Type = std::optional; - - [[nodiscard]] static Result push(lua_State* L, const Type& value) - { - if (value) - { - StackRestore stackRestore(L); - - auto result = Stack::push(L, *value); - if (! result) - return result; - - stackRestore.reset(); - return {}; - } - -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushnil(L); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNIL) - return std::nullopt; - - auto result = Stack::get(L, index); - if (! result) - return result.error(); - - return *result; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_isnil(L, index) || Stack::isInstance(L, index); - } -}; - -template -struct Stack> -{ - [[nodiscard]] static Result push(lua_State* L, const std::pair& t) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 2, 0); - - auto result1 = push_element<0>(L, t); - if (! result1) - return result1; - - auto result2 = push_element<1>(L, t); - if (! result2) - return result2; - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult> get(lua_State* L, int index) - { - const StackRestore stackRestore(L); - - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (get_length(L, index) != 2) - return makeErrorCode(ErrorCode::InvalidTableSizeInCast); - - std::pair value; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - auto result1 = pop_element<0>(L, absIndex, value); - if (! result1) - return result1.error(); - - auto result2 = pop_element<1>(L, absIndex, value); - if (! result2) - return result2.error(); - - return value; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TTABLE && get_length(L, index) == 2; - } - -private: - template - static Result push_element(lua_State* L, const std::pair& p) - { - static_assert(Index < 2); - - using T = std::tuple_element_t>; - - lua_pushinteger(L, static_cast(Index + 1)); - - auto result = Stack::push(L, std::get(p)); - if (! result) - { - lua_pushnil(L); - lua_settable(L, -3); - return result; - } - - lua_settable(L, -3); - - return {}; - } - - template - static Result pop_element(lua_State* L, int absIndex, std::pair& p) - { - static_assert(Index < 2); - - using T = std::tuple_element_t>; - - if (lua_next(L, absIndex) == 0) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto result = Stack::get(L, -1); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::get(p) = std::move(*result); - lua_pop(L, 1); - - return {}; - } -}; - -template -struct Stack> -{ - [[nodiscard]] static Result push(lua_State* L, const std::tuple& t) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(Size), 0); - - auto result = push_element(L, t); - if (! result) - return result; - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult> get(lua_State* L, int index) - { - const StackRestore stackRestore(L); - - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (get_length(L, index) != static_cast(Size)) - return makeErrorCode(ErrorCode::InvalidTableSizeInCast); - - std::tuple value; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - auto result = pop_element(L, absIndex, value); - if (! result) - return result.error(); - - return value; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TTABLE && get_length(L, index) == static_cast(Size); - } - -private: - static constexpr std::size_t Size = std::tuple_size_v>; - - template - static auto push_element(lua_State*, const std::tuple&) - -> std::enable_if_t - { - return {}; - } - - template - static auto push_element(lua_State* L, const std::tuple& t) - -> std::enable_if_t - { - using T = std::tuple_element_t>; - - lua_pushinteger(L, static_cast(Index + 1)); - - auto result = Stack::push(L, std::get(t)); - if (! result) - { - lua_pushnil(L); - lua_settable(L, -3); - return result; - } - - lua_settable(L, -3); - - return push_element(L, t); - } - - template - static auto pop_element(lua_State*, int, std::tuple&) - -> std::enable_if_t - { - return {}; - } - - template - static auto pop_element(lua_State* L, int absIndex, std::tuple& t) - -> std::enable_if_t - { - using T = std::tuple_element_t>; - - if (lua_next(L, absIndex) == 0) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto result = Stack::get(L, -1); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::get(t) = std::move(*result); - lua_pop(L, 1); - - return pop_element(L, absIndex, t); - } -}; - -template -struct Stack -{ - static_assert(N > 0, "Unsupported zero sized array"); - - [[nodiscard]] static Result push(lua_State* L, const T (&value)[N]) - { - if constexpr (std::is_same_v) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, value, N - 1); - return {}; - } - -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 2)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(N), 0); - - for (std::size_t i = 0; i < N; ++i) - { - lua_pushinteger(L, static_cast(i + 1)); - - auto result = Stack::push(L, value[i]); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TTABLE && get_length(L, index) == static_cast(N); - } -}; - -namespace detail { - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, T& value) { return Stack::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T& value) { return Stack::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, T* value) { return Stack::push(L, *value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T* value) { return Stack::push(L, *value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -} - -template -struct Stack>> -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, T& value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -struct Stack>> -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, const T& value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -struct Stack -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, T* value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -struct Stack -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, const T* value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -[[nodiscard]] Result push(lua_State* L, const T& t) -{ - return Stack::push(L, t); -} - -template -[[nodiscard]] TypeResult get(lua_State* L, int index) -{ - return Stack::get(L, index); -} - -template -[[nodiscard]] bool isInstance(lua_State* L, int index) -{ - return Stack::isInstance(L, index); -} - -} - - -// End File: Source/LuaBridge/detail/Stack.h - -// Begin File: Source/LuaBridge/Array.h - -namespace luabridge { - -template -struct Stack> -{ - using Type = std::array; - - [[nodiscard]] static Result push(lua_State* L, const Type& array) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(Size), 0); - - for (std::size_t i = 0; i < Size; ++i) - { - lua_pushinteger(L, static_cast(i + 1)); - - auto result = Stack::push(L, array[i]); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (get_length(L, index) != Size) - return makeErrorCode(ErrorCode::InvalidTableSizeInCast); - - const StackRestore stackRestore(L); - - Type array; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - int arrayIndex = 0; - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (!item) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - array[arrayIndex++] = *item; - lua_pop(L, 1); - } - - return array; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index) && get_length(L, index) == Size; - } -}; - -} - - -// End File: Source/LuaBridge/Array.h - -// Begin File: Source/LuaBridge/List.h - -namespace luabridge { - -template -struct Stack> -{ - using Type = std::list; - - [[nodiscard]] static Result push(lua_State* L, const Type& list) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(list.size()), 0); - - auto it = list.cbegin(); - for (lua_Integer tableIndex = 1; it != list.cend(); ++tableIndex, ++it) - { - lua_pushinteger(L, tableIndex); - - auto result = Stack::push(L, *it); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type list; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (! item) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - list.emplace_back(*item); - lua_pop(L, 1); - } - - return list; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} - - -// End File: Source/LuaBridge/List.h - -// Begin File: Source/LuaBridge/detail/FuncTraits.h - -namespace luabridge { -namespace detail { - -[[noreturn]] inline void unreachable() -{ -#if __GNUC__ - __builtin_unreachable(); -#elif _MSC_VER - __assume(false); -#endif -} - -template< class T > -struct remove_cvref -{ - typedef std::remove_cv_t> type; -}; - -template -using remove_cvref_t = typename remove_cvref::type; - -template -struct function_traits_base -{ - using result_type = R; - - using argument_types = std::tuple; - - static constexpr auto arity = sizeof...(Args); - - static constexpr auto is_member = IsMember; - - static constexpr auto is_const = IsConst; -}; - -template -struct function_traits_impl; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -#if _MSC_VER && _M_IX86 -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; -#endif - -template -struct functor_traits_impl : function_traits_impl -{ -}; - -template -struct function_traits : std::conditional_t, - detail::functor_traits_impl, - detail::function_traits_impl> -{ -}; - -template -struct function_argument_or_void -{ - using type = void; -}; - -template -struct function_argument_or_void::argument_types>>> -{ - using type = std::tuple_element_t::argument_types>; -}; - -template -using function_argument_or_void_t = typename function_argument_or_void::type; - -template -using function_result_t = typename function_traits::result_type; - -template -using function_argument_t = std::tuple_element_t::argument_types>; - -template -using function_arguments_t = typename function_traits::argument_types; - -template -static constexpr std::size_t function_arity_v = function_traits::arity; - -template -static constexpr bool function_is_member_v = function_traits::is_member; - -template -static constexpr bool function_is_const_v = function_traits::is_const; - -template -struct is_callable -{ - static constexpr bool value = false; -}; - -template -struct is_callable> -{ - static constexpr bool value = true; -}; - -template -struct is_callable && std::is_function_v>>> -{ - static constexpr bool value = true; -}; - -template -struct is_callable>> -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_callable_v = is_callable::value; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = true; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_const_member_function_pointer_v = is_const_member_function_pointer::value; - -template -struct is_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template <> -struct is_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_cfunction_pointer_v = is_cfunction_pointer::value; - -template -struct is_member_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_member_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -struct is_member_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_member_cfunction_pointer_v = is_member_cfunction_pointer::value; - -template -struct is_const_member_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_const_member_cfunction_pointer_v = is_const_member_cfunction_pointer::value; - -template -inline static constexpr bool is_any_cfunction_pointer_v = is_cfunction_pointer_v || is_member_cfunction_pointer_v; - -template -inline static constexpr bool is_proxy_member_function_v = - !std::is_member_function_pointer_v && - std::is_same_v>>>; - -template -inline static constexpr bool is_const_proxy_function_v = - is_proxy_member_function_v && - std::is_const_v>>; - -template -struct function_arity_excluding -{ -}; - -template < class... Ts, class ExclusionType> -struct function_arity_excluding, ExclusionType> - : std::integral_constant, ExclusionType> ? 0 : 1))> -{ -}; - -template -inline static constexpr std::size_t function_arity_excluding_v = function_arity_excluding, ExclusionType>::value; - -template -struct member_function_arity_excluding -{ -}; - -template -struct member_function_arity_excluding, ExclusionType, std::enable_if_t>> - : std::integral_constant, ExclusionType> ? 0 : 1))> -{ -}; - -template -struct member_function_arity_excluding, ExclusionType, std::enable_if_t>> - : std::integral_constant, ExclusionType> ? 0 : 1)) - 1> -{ -}; - -template -inline static constexpr std::size_t member_function_arity_excluding_v = member_function_arity_excluding, ExclusionType>::value; - -template -static constexpr bool is_const_function = - detail::is_const_member_function_pointer_v || - (detail::function_arity_v > 0 && detail::is_const_proxy_function_v); - -template -inline static constexpr std::size_t const_functions_count = (0 + ... + (is_const_function ? 1 : 0)); - -template -inline static constexpr std::size_t non_const_functions_count = (0 + ... + (is_const_function ? 0 : 1)); - -template -constexpr auto tupleize(Types&&... types) -{ - return std::tuple(std::forward(types)...); -} - -template -struct remove_first_type -{ -}; - -template -struct remove_first_type> -{ - using type = std::tuple; -}; - -template -using remove_first_type_t = typename remove_first_type::type; - -} -} - - -// End File: Source/LuaBridge/detail/FuncTraits.h - -// Begin File: Source/LuaBridge/detail/CFunctions.h - -namespace luabridge { -namespace detail { - -template -auto unwrap_argument_or_error(lua_State* L, std::size_t index) -{ - auto result = Stack::get(L, static_cast(index)); - if (! result) - luaL_error(L, "Error decoding argument #%d: %s", static_cast(index), result.message().c_str()); - - return std::move(*result); -} - -template -auto make_arguments_list_impl(lua_State* L, std::index_sequence) -{ - return tupleize(unwrap_argument_or_error>(L, Start + Indices)...); -} - -template -auto make_arguments_list(lua_State* L) -{ - return make_arguments_list_impl(L, std::make_index_sequence>()); -} - -template -auto push_arguments(lua_State*, std::tuple) - -> std::enable_if_t> -{ - return std::make_tuple(Result(), Index + 1); -} - -template -auto push_arguments(lua_State* L, std::tuple t) - -> std::enable_if_t> -{ - using T = std::tuple_element_t>; - - auto result = Stack::push(L, std::get(t)); - if (! result) - return std::make_tuple(result, Index + 1); - - return push_arguments(L, std::move(t)); -} - -template -auto pop_arguments(lua_State*, std::tuple&) - -> std::enable_if_t -{ - return sizeof...(Types); -} - -template -auto pop_arguments(lua_State* L, std::tuple& t) - -> std::enable_if_t -{ - using T = std::tuple_element_t>; - - std::get(t) = Stack::get(L, Start - Index); - - return pop_arguments(L, t); -} - -inline int index_metamethod(lua_State* L) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 3, detail::error_lua_stack_overflow); -#endif - - assert(lua_istable(L, 1) || lua_isuserdata(L, 1)); - - lua_getmetatable(L, 1); - assert(lua_istable(L, -1)); - - for (;;) - { - lua_pushvalue(L, 2); - lua_rawget(L, -2); - - if (lua_iscfunction(L, -1)) - { - lua_remove(L, -2); - return 1; - } - - assert(lua_isnil(L, -1)); - lua_pop(L, 1); - - lua_rawgetp(L, -1, getPropgetKey()); - assert(lua_istable(L, -1)); - - lua_pushvalue(L, 2); - lua_rawget(L, -2); - lua_remove(L, -2); - - if (lua_iscfunction(L, -1)) - { - lua_remove(L, -2); - lua_pushvalue(L, 1); - lua_call(L, 1, 1); - return 1; - } - - assert(lua_isnil(L, -1)); - lua_pop(L, 1); - - lua_rawgetp(L, -1, getParentKey()); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - lua_rawgetp(L, -1, getIndexFallbackKey()); - lua_remove(L, -2); - if (lua_iscfunction(L, -1)) - { - lua_pushvalue(L, 1); - lua_pushvalue(L, 2); - lua_call(L, 2, 1); - } - else - { - lua_pop(L, 1); - lua_pushnil(L); - } - - return 1; - } - - assert(lua_istable(L, -1)); - lua_remove(L, -2); - } - -} - -inline int newindex_metamethod(lua_State* L, bool pushSelf) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 3, detail::error_lua_stack_overflow); -#endif - - assert(lua_istable(L, 1) || lua_isuserdata(L, 1)); - - lua_getmetatable(L, 1); - assert(lua_istable(L, -1)); - - for (;;) - { - lua_rawgetp(L, -1, getPropsetKey()); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 2); - luaL_error(L, "No member named '%s'", lua_tostring(L, 2)); - } - - assert(lua_istable(L, -1)); - - lua_pushvalue(L, 2); - lua_rawget(L, -2); - lua_remove(L, -2); - - if (lua_iscfunction(L, -1)) - { - lua_remove(L, -2); - if (pushSelf) - lua_pushvalue(L, 1); - lua_pushvalue(L, 3); - lua_call(L, pushSelf ? 2 : 1, 0); - return 0; - } - - assert(lua_isnil(L, -1)); - lua_pop(L, 1); - - lua_rawgetp(L, -1, getParentKey()); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - lua_rawgetp(L, -1, getNewIndexFallbackKey()); - if (lua_iscfunction(L, -1)) - { - lua_pushvalue(L, 1); - lua_pushvalue(L, 2); - lua_pushvalue(L, 3); - lua_call(L, 3, 1); - return 0; - } - - lua_pop(L, 1); - lua_pop(L, 1); - luaL_error(L, "No writable member '%s'", lua_tostring(L, 2)); - return 0; - } - - assert(lua_istable(L, -1)); - lua_remove(L, -2); - - } - - return 0; -} - -inline int newindex_object_metamethod(lua_State* L) -{ - return newindex_metamethod(L, true); -} - -inline int newindex_static_metamethod(lua_State* L) -{ - return newindex_metamethod(L, false); -} - -inline int read_only_error(lua_State* L) -{ - std::string s; - - s = s + "'" + lua_tostring(L, lua_upvalueindex(1)) + "' is read-only"; - - luaL_error(L, "%s", s.c_str()); - - return 0; -} - -template -static int gc_metamethod(lua_State* L) -{ - Userdata* ud = Userdata::getExact(L, 1); - assert(ud); - - ud->~Userdata(); - - return 0; -} - -template -struct property_getter; - -template -struct property_getter -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - T* ptr = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - auto result = Stack::push(L, *ptr); - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } -}; - -#if 0 -template -struct property_getter, void> -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - std::reference_wrapper* ptr = static_cast*>(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - auto result = Stack::push(L, ptr->get()); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - - return 1; - } -}; -#endif - -template -struct property_getter -{ - static int call(lua_State* L) - { - C* c = Userdata::get(L, 1, true); - - T C::** mp = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - - Result result; - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - result = Stack::push(L, c->**mp); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } -}; - -inline void add_property_getter(lua_State* L, const char* name, int tableIndex) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 2, detail::error_lua_stack_overflow); -#endif - - assert(name != nullptr); - assert(lua_istable(L, tableIndex)); - assert(lua_iscfunction(L, -1)); - - lua_rawgetp(L, tableIndex, getPropgetKey()); - lua_pushvalue(L, -2); - rawsetfield(L, -2, name); - lua_pop(L, 2); -} - -template -struct property_setter; - -template -struct property_setter -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - T* ptr = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - auto result = Stack::get(L, 1); - if (! result) - raise_lua_error(L, "%s", result.error().message().c_str()); - - *ptr = std::move(*result); - - return 0; - } -}; - -#if 0 -template -struct property_setter, void> -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - std::reference_wrapper* ptr = static_cast*>(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - ptr->get() = Stack::get(L, 1); - - return 0; - } -}; -#endif - -template -struct property_setter -{ - static int call(lua_State* L) - { - C* c = Userdata::get(L, 1, false); - - T C::** mp = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - auto result = Stack::get(L, 2); - if (! result) - raise_lua_error(L, "%s", result.error().message().c_str()); - - c->** mp = std::move(*result); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - return 0; - } -}; - -inline void add_property_setter(lua_State* L, const char* name, int tableIndex) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 2, detail::error_lua_stack_overflow); -#endif - - assert(name != nullptr); - assert(lua_istable(L, tableIndex)); - assert(lua_iscfunction(L, -1)); - - lua_rawgetp(L, tableIndex, getPropsetKey()); - lua_pushvalue(L, -2); - rawsetfield(L, -2, name); - lua_pop(L, 2); -} - -template -struct function -{ - template - static int call(lua_State* L, F func) - { - Result result; - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - result = Stack::push(L, std::apply(func, make_arguments_list(L))); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } - - template - static int call(lua_State* L, T* ptr, F func) - { - Result result; - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - auto f = [ptr, func](auto&&... args) -> ReturnType { return (ptr->*func)(std::forward(args)...); }; - - result = Stack::push(L, std::apply(f, make_arguments_list(L))); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } -}; - -template -struct function -{ - template - static int call(lua_State* L, F func) - { -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - std::apply(func, make_arguments_list(L)); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - return 0; - } - - template - static int call(lua_State* L, T* ptr, F func) - { -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - auto f = [ptr, func](auto&&... args) { (ptr->*func)(std::forward(args)...); }; - - std::apply(f, make_arguments_list(L)); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - return 0; - } -}; - -template -int invoke_member_function(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - T* ptr = Userdata::get(L, 1, false); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return function::call(L, ptr, func); -} - -template -int invoke_const_member_function(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - const T* ptr = Userdata::get(L, 1, true); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return function::call(L, ptr, func); -} - -template -int invoke_member_cfunction(lua_State* L) -{ - using F = int (T::*)(lua_State * L); - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - T* t = Userdata::get(L, 1, false); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return (t->*func)(L); -} - -template -int invoke_const_member_cfunction(lua_State* L) -{ - using F = int (T::*)(lua_State * L) const; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - const T* t = Userdata::get(L, 1, true); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return (t->*func)(L); -} - -template -int invoke_proxy_function(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - auto func = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return function::call(L, func); -} - -template -int invoke_proxy_functor(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - auto& func = *align(lua_touserdata(L, lua_upvalueindex(1))); - - return function::call(L, func); -} - -template -inline int try_overload_functions(lua_State* L) -{ - const int nargs = lua_gettop(L); - const int effective_args = nargs - (Member ? 1 : 0); - - lua_pushvalue(L, lua_upvalueindex(1)); - assert(lua_istable(L, -1)); - const int idx_overloads = nargs + 1; - const int num_overloads = get_length(L, idx_overloads); - - lua_createtable(L, num_overloads, 0); - const int idx_errors = nargs + 2; - int nerrors = 0; - - lua_pushnil(L); - while (lua_next(L, idx_overloads) != 0) - { - assert(lua_istable(L, -1)); - - lua_rawgeti(L, -1, 1); - assert(lua_isnumber(L, -1)); - - const int overload_arity = static_cast(lua_tointeger(L, -1)); - if (overload_arity >= 0 && overload_arity != effective_args) - { - - lua_pushfstring(L, "Skipped overload #%d with unmatched arity of %d instead of %d", nerrors, overload_arity, effective_args); - lua_rawseti(L, idx_errors, ++nerrors); - - lua_pop(L, 2); - continue; - } - - lua_pop(L, 1); - - lua_pushnumber(L, 2); - lua_gettable(L, -2); - assert(lua_isfunction(L, -1)); - - for (int i = 1; i <= nargs; ++i) - lua_pushvalue(L, i); - - const int err = lua_pcall(L, nargs, LUA_MULTRET, 0); - if (err == LUABRIDGE_LUA_OK) - { - - const int nresults = lua_gettop(L) - nargs - 4; - return nresults; - } - else if (err == LUA_ERRRUN) - { - - lua_rawseti(L, idx_errors, ++nerrors); - } - else - { - return lua_error_x(L); - } - - lua_pop(L, 1); - } - - lua_Debug debug; - lua_getstack_info_x(L, 0, "n", &debug); - lua_pushfstring(L, "All %d overloads of %s returned an error:", nerrors, debug.name); - - for (int i = 1; i <= nerrors; ++i) - { - lua_pushfstring(L, "\n%d: ", i); - lua_rawgeti(L, idx_errors, i); - } - lua_concat(L, nerrors * 2 + 1); - - return lua_error_x(L); -} - -inline void push_function(lua_State* L, lua_CFunction fp) -{ - lua_pushcfunction_x(L, fp); -} - -template -inline void push_function(lua_State* L, ReturnType (*fp)(Params...)) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -inline void push_function(lua_State* L, ReturnType (*fp)(Params...) noexcept) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template && !std::is_pointer_v && !std::is_member_function_pointer_v>> -inline void push_function(lua_State* L, F&& f) -{ - lua_newuserdata_aligned(L, std::forward(f)); - lua_pushcclosure_x(L, &invoke_proxy_functor, 1); -} - -template -void push_member_function(lua_State* L, lua_CFunction fp) -{ - lua_pushcfunction_x(L, fp); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...)) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...) noexcept) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...)) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...) noexcept) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template && - std::is_object_v && - !std::is_pointer_v && - !std::is_member_function_pointer_v>> -void push_member_function(lua_State* L, F&& f) -{ - static_assert(std::is_same_v>>>); - - lua_newuserdata_aligned(L, std::forward(f)); - lua_pushcclosure_x(L, &invoke_proxy_functor, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...)) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_member_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) noexcept) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_member_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const noexcept) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); -} - -template -void push_member_function(lua_State* L, int (U::*mfp)(lua_State*)) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_member_cfunction, 1); -} - -template -void push_member_function(lua_State* L, int (U::*mfp)(lua_State*) const) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_const_member_cfunction, 1); -} - -template -struct constructor; - -template -struct constructor -{ - using empty = std::tuple<>; - - static T* call(const empty&) - { - return new T; - } - - static T* call(void* ptr, const empty&) - { - return new (ptr) T; - } -}; - -template -struct constructor -{ - static T* call(const Args& args) - { - auto alloc = [](auto&&... args) { return new T(std::forward(args)...); }; - - return std::apply(alloc, args); - } - - static T* call(void* ptr, const Args& args) - { - auto alloc = [ptr](auto&&... args) { return new (ptr) T(std::forward(args)...); }; - - return std::apply(alloc, args); - } -}; - -template -struct placement_constructor -{ - template - static T* construct(void* ptr, const F& func, const Args& args) - { - auto alloc = [ptr, &func](auto&&... args) { return func(ptr, std::forward(args)...); }; - - return std::apply(alloc, args); - } - - template - static T* construct(void* ptr, const F& func) - { - return func(ptr); - } -}; - -template -struct external_constructor -{ - template - static T* construct(const F& func, const Args& args) - { - auto alloc = [&func](auto&&... args) { return func(std::forward(args)...); }; - - return std::apply(alloc, args); - } - - template - static T* construct(const F& func) - { - return func(); - } -}; - -template -int constructor_container_proxy(lua_State* L) -{ - using T = typename ContainerTraits::Type; - - T* object = detail::constructor::call(detail::make_arguments_list(L)); - - auto result = detail::UserdataSharedHelper::push(L, object); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - - return 1; -} - -template -int constructor_placement_proxy(lua_State* L) -{ - std::error_code ec; - auto* value = detail::UserdataValue::place(L, ec); - if (! value) - luaL_error(L, "%s", ec.message().c_str()); - - detail::constructor::call(value->getObject(), detail::make_arguments_list(L)); - - value->commit(); - - return 1; -} - -template -struct constructor_forwarder -{ - explicit constructor_forwarder(F f) - : m_func(std::move(f)) - { - } - - T* operator()(lua_State* L) - { - std::error_code ec; - auto* value = UserdataValue::place(L, ec); - if (! value) - luaL_error(L, "%s", ec.message().c_str()); - - using FnTraits = function_traits; - using FnArgs = remove_first_type_t; - - T* obj = placement_constructor::construct( - value->getObject(), m_func, make_arguments_list(L)); - - value->commit(); - - return obj; - } - -private: - F m_func; -}; - -template -struct factory_forwarder -{ - explicit factory_forwarder(Alloc alloc, Dealloc dealloc) - : m_alloc(std::move(alloc)) - , m_dealloc(std::move(dealloc)) - { - } - - T* operator()(lua_State* L) - { - using FnTraits = function_traits; - using FnArgs = typename FnTraits::argument_types; - - T* obj = external_constructor::construct(m_alloc, make_arguments_list(L)); - - std::error_code ec; - auto* value = UserdataValueExternal::place(L, obj, m_dealloc, ec); - if (! value) - luaL_error(L, "%s", ec.message().c_str()); - - return obj; - } - -private: - Alloc m_alloc; - Dealloc m_dealloc; -}; - -} -} - - -// End File: Source/LuaBridge/detail/CFunctions.h - -// Begin File: Source/LuaBridge/detail/LuaRef.h - -namespace luabridge { - -class LuaResult; - -struct LuaNil -{ -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const LuaNil&) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushnil(L); - return {}; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TNIL; - } -}; - -template -class LuaRefBase -{ -protected: - friend struct Stack; - - struct FromStack - { - }; - - LuaRefBase(lua_State* L) - : m_L(L) - { - } - - int createRef() const - { - impl().push(); - - return luaL_ref(m_L, LUA_REGISTRYINDEX); - } - -public: - - std::string tostring() const - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - return {}; -#endif - - const StackRestore stackRestore(m_L); - - lua_getglobal(m_L, "tostring"); - - impl().push(); - - lua_call(m_L, 1, 1); - - const char* str = lua_tostring(m_L, -1); - return str != nullptr ? str : ""; - } - - void print(std::ostream& os) const - { - switch (type()) - { - case LUA_TNONE: - case LUA_TNIL: - os << "nil"; - break; - - case LUA_TNUMBER: - os << unsafe_cast(); - break; - - case LUA_TBOOLEAN: - os << (unsafe_cast() ? "true" : "false"); - break; - - case LUA_TSTRING: - os << '"' << unsafe_cast() << '"'; - break; - - case LUA_TTABLE: - case LUA_TFUNCTION: - case LUA_TTHREAD: - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - os << tostring(); - break; - - default: - os << "unknown"; - break; - } - } - - friend std::ostream& operator<<(std::ostream& os, const LuaRefBase& ref) - { - ref.print(os); - return os; - } - - lua_State* state() const - { - return m_L; - } - - void push(lua_State* L) const - { - assert(equalstates(L, m_L)); - (void) L; - - impl().push(); - } - - void pop(lua_State* L) - { - assert(equalstates(L, m_L)); - (void) L; - - impl().pop(); - } - - int type() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - const int refType = lua_type(m_L, -1); - - return refType; - } - - bool isNil() const { return type() == LUA_TNIL; } - - bool isBool() const { return type() == LUA_TBOOLEAN; } - - bool isNumber() const { return type() == LUA_TNUMBER; } - - bool isString() const { return type() == LUA_TSTRING; } - - bool isTable() const { return type() == LUA_TTABLE; } - - bool isFunction() const { return type() == LUA_TFUNCTION; } - - bool isUserdata() const { return type() == LUA_TUSERDATA; } - - bool isThread() const { return type() == LUA_TTHREAD; } - - bool isLightUserdata() const { return type() == LUA_TLIGHTUSERDATA; } - - bool isCallable() const - { - if (isFunction()) - return true; - - auto metatable = getMetatable(); - return metatable.isTable() && metatable["__call"].isFunction(); - } - - template - TypeResult cast() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - auto result = Stack::get(m_L, -1); - if (! result) - return result.error(); - - return static_cast(*result); - } - else - { - return Stack::get(m_L, -1); - } - } - - template - T unsafe_cast() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - return static_cast(*Stack::get(m_L, -1)); - } - else - { - return *Stack::get(m_L, -1); - } - } - - template - bool isInstance() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - return Stack::isInstance(m_L, -1); - } - else - { - return Stack::isInstance(m_L, -1); - } - } - - template - operator T() const - { - return cast().value(); - } - - LuaRef getMetatable() const - { - if (isNil()) - return LuaRef(m_L); - - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! lua_getmetatable(m_L, -1)) - return LuaRef(m_L); - - return LuaRef::fromStack(m_L); - } - - template - bool operator==(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - return lua_compare(m_L, -2, -1, LUA_OPEQ) == 1; - } - - template - bool operator!=(const T& rhs) const - { - return !(*this == rhs); - } - - template - bool operator<(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType < rhsType; - - return lua_compare(m_L, -2, -1, LUA_OPLT) == 1; - } - - template - bool operator<=(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType <= rhsType; - - return lua_compare(m_L, -2, -1, LUA_OPLE) == 1; - } - - template - bool operator>(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType > rhsType; - - return lua_compare(m_L, -1, -2, LUA_OPLT) == 1; - } - - template - bool operator>=(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType >= rhsType; - - return lua_compare(m_L, -1, -2, LUA_OPLE) == 1; - } - - template - bool rawequal(const T& v) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, v)) - return false; - - return lua_rawequal(m_L, -1, -2) == 1; - } - - int length() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - return get_length(m_L, -1); - } - - template - LuaResult operator()(Args&&... args) const; - -protected: - lua_State* m_L = nullptr; - -private: - const Impl& impl() const { return static_cast(*this); } - - Impl& impl() { return static_cast(*this); } -}; - -class LuaRef : public LuaRefBase -{ - - class TableItem : public LuaRefBase - { - friend class LuaRef; - - public: - - TableItem(lua_State* L, int tableRef) - : LuaRefBase(L) - , m_keyRef(luaL_ref(L, LUA_REGISTRYINDEX)) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(m_L, 1, detail::error_lua_stack_overflow); -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, tableRef); - m_tableRef = luaL_ref(L, LUA_REGISTRYINDEX); - } - - TableItem(const TableItem& other) - : LuaRefBase(other.m_L) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 1)) - return; -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, other.m_tableRef); - m_tableRef = luaL_ref(m_L, LUA_REGISTRYINDEX); - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, other.m_keyRef); - m_keyRef = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - - ~TableItem() - { - if (m_keyRef != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_keyRef); - - if (m_tableRef != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_tableRef); - } - - template - TableItem& operator=(const T& v) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - return *this; -#endif - - const StackRestore stackRestore(m_L); - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_tableRef); - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_keyRef); - - if (! Stack::push(m_L, v)) - return *this; - - lua_settable(m_L, -3); - return *this; - } - - template - TableItem& rawset(const T& v) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - return *this; -#endif - - const StackRestore stackRestore(m_L); - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_tableRef); - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_keyRef); - - if (! Stack::push(m_L, v)) - return *this; - - lua_rawset(m_L, -3); - return *this; - } - - using LuaRefBase::push; - - void push() const - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 3)) - return; -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_tableRef); - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_keyRef); - lua_gettable(m_L, -2); - lua_remove(m_L, -2); - } - - template - TableItem operator[](const T& key) const - { - return LuaRef(*this)[key]; - } - - template - LuaRef rawget(const T& key) const - { - return LuaRef(*this).rawget(key); - } - - private: - int m_tableRef = LUA_NOREF; - int m_keyRef = LUA_NOREF; - }; - - friend struct Stack; - friend struct Stack; - - LuaRef(lua_State* L, FromStack) - : LuaRefBase(L) - , m_ref(luaL_ref(m_L, LUA_REGISTRYINDEX)) - { - } - - LuaRef(lua_State* L, int index, FromStack) - : LuaRefBase(L) - , m_ref(LUA_NOREF) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 1)) - return; -#endif - - lua_pushvalue(m_L, index); - m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - -public: - - LuaRef(lua_State* L) - : LuaRefBase(L) - , m_ref(LUA_NOREF) - { - } - - template - LuaRef(lua_State* L, const T& v) - : LuaRefBase(L) - , m_ref(LUA_NOREF) - { - if (! Stack::push(m_L, v)) - return; - - m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - - LuaRef(const TableItem& v) - : LuaRefBase(v.state()) - , m_ref(v.createRef()) - { - } - - LuaRef(const LuaRef& other) - : LuaRefBase(other.m_L) - , m_ref(other.createRef()) - { - } - - LuaRef(LuaRef&& other) - : LuaRefBase(other.m_L) - , m_ref(std::exchange(other.m_ref, LUA_NOREF)) - { - } - - ~LuaRef() - { - if (m_ref != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref); - } - - static LuaRef fromStack(lua_State* L) - { - return LuaRef(L, FromStack()); - } - - static LuaRef fromStack(lua_State* L, int index) - { - return LuaRef(L, index, FromStack()); - } - - static LuaRef newTable(lua_State* L) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return { L }; -#endif - - lua_newtable(L); - return LuaRef(L, FromStack()); - } - - static LuaRef getGlobal(lua_State* L, const char* name) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return { L }; -#endif - - lua_getglobal(L, name); - return LuaRef(L, FromStack()); - } - - bool isValid() const { return m_ref != LUA_NOREF; } - - LuaRef& operator=(const LuaRef& rhs) - { - LuaRef ref(rhs); - swap(ref); - return *this; - } - - LuaRef& operator=(LuaRef&& rhs) - { - if (m_ref != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref); - - m_L = rhs.m_L; - m_ref = std::exchange(rhs.m_ref, LUA_NOREF); - - return *this; - } - - LuaRef& operator=(const LuaRef::TableItem& rhs) - { - LuaRef ref(rhs); - swap(ref); - return *this; - } - - LuaRef& operator=(const LuaNil&) - { - LuaRef ref(m_L); - swap(ref); - return *this; - } - - template - LuaRef& operator=(const T& rhs) - { - LuaRef ref(m_L, rhs); - swap(ref); - return *this; - } - - using LuaRefBase::push; - - void push() const - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 1)) - return; -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_ref); - } - - void pop() - { - if (m_ref != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref); - - m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - - template - TableItem operator[](const T& key) const - { - if (! Stack::push(m_L, key)) - return TableItem(m_L, m_ref); - - return TableItem(m_L, m_ref); - } - - template - LuaRef rawget(const T& key) const - { - const StackRestore stackRestore(m_L); - - push(m_L); - - if (! Stack::push(m_L, key)) - return LuaRef(m_L); - - lua_rawget(m_L, -2); - return LuaRef(m_L, FromStack()); - } - - std::size_t hash() const - { - std::size_t value; - switch (type()) - { - case LUA_TNONE: - value = std::hash{}(nullptr); - break; - - case LUA_TBOOLEAN: - value = std::hash{}(unsafe_cast()); - break; - - case LUA_TNUMBER: - value = std::hash{}(unsafe_cast()); - break; - - case LUA_TSTRING: - value = std::hash{}(unsafe_cast()); - break; - - case LUA_TNIL: - case LUA_TTABLE: - case LUA_TFUNCTION: - case LUA_TTHREAD: - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - default: - value = static_cast(m_ref); - break; - } - - const std::size_t seed = std::hash{}(type()); - return value + 0x9e3779b9u + (seed << 6) + (seed >> 2); - } - -private: - void swap(LuaRef& other) - { - using std::swap; - - swap(m_L, other.m_L); - swap(m_ref, other.m_ref); - } - - int m_ref = LUA_NOREF; -}; - -template -auto operator==(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return rhs == lhs; -} - -template -auto operator!=(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs == lhs); -} - -template -auto operator<(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs >= lhs); -} - -template -auto operator<=(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs > lhs); -} - -template -auto operator>(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return rhs <= lhs; -} - -template -auto operator>=(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs > lhs); -} - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const LuaRef& v) - { - v.push(L); - - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - return LuaRef::fromStack(L, index); - } -}; - -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const LuaRef::TableItem& v) - { - v.push(L); - - return {}; - } -}; - -[[nodiscard]] inline LuaRef newTable(lua_State* L) -{ - return LuaRef::newTable(L); -} - -[[nodiscard]] inline LuaRef getGlobal(lua_State* L, const char* name) -{ - return LuaRef::getGlobal(L, name); -} - -template -[[nodiscard]] TypeResult cast(const LuaRef& ref) -{ - return ref.cast(); -} - -template -[[nodiscard]] T unsafe_cast(const LuaRef& ref) -{ - return ref.unsafe_cast(); -} -} - -namespace std { -template <> -struct hash -{ - std::size_t operator()(const luabridge::LuaRef& x) const - { - return x.hash(); - } -}; -} - - -// End File: Source/LuaBridge/detail/LuaRef.h - -// Begin File: Source/LuaBridge/detail/Invoke.h - -namespace luabridge { - -class LuaResult -{ -public: - - explicit operator bool() const noexcept - { - return !m_ec; - } - - bool wasOk() const noexcept - { - return !m_ec; - } - - bool hasFailed() const noexcept - { - return !!m_ec; - } - - std::error_code errorCode() const noexcept - { - return m_ec; - } - - std::string errorMessage() const noexcept - { - if (std::holds_alternative(m_data)) - { - const auto& message = std::get(m_data); - return message.empty() ? m_ec.message() : message; - } - - return {}; - } - - std::size_t size() const noexcept - { - if (std::holds_alternative>(m_data)) - return std::get>(m_data).size(); - - return 0; - } - - LuaRef operator[](std::size_t index) const - { - assert(m_ec == std::error_code()); - - if (std::holds_alternative>(m_data)) - { - const auto& values = std::get>(m_data); - - assert(index < values.size()); - return values[index]; - } - - return LuaRef(m_L); - } - -private: - template - friend LuaResult call(const LuaRef&, Args&&...); - - static LuaResult errorFromStack(lua_State* L, std::error_code ec) - { - auto errorString = lua_tostring(L, -1); - lua_pop(L, 1); - - return LuaResult(L, ec, errorString ? errorString : ec.message()); - } - - static LuaResult valuesFromStack(lua_State* L, int stackTop) - { - std::vector values; - - const int numReturnedValues = lua_gettop(L) - stackTop; - if (numReturnedValues > 0) - { - values.reserve(numReturnedValues); - - for (int index = numReturnedValues; index > 0; --index) - values.emplace_back(LuaRef::fromStack(L, -index)); - - lua_pop(L, numReturnedValues); - } - - return LuaResult(L, std::move(values)); - } - - LuaResult(lua_State* L, std::error_code ec, std::string_view errorString) - : m_L(L) - , m_ec(ec) - , m_data(std::string(errorString)) - { - } - - explicit LuaResult(lua_State* L, std::vector values) noexcept - : m_L(L) - , m_data(std::move(values)) - { - } - - lua_State* m_L = nullptr; - std::error_code m_ec; - std::variant, std::string> m_data; -}; - -template -LuaResult call(const LuaRef& object, Args&&... args) -{ - lua_State* L = object.state(); - const int stackTop = lua_gettop(L); - - object.push(); - - { - const auto [result, index] = detail::push_arguments(L, std::forward_as_tuple(args...)); - if (! result) - { - lua_pop(L, static_cast(index) + 1); - return LuaResult(L, result, result.message()); - } - } - - int code = lua_pcall(L, sizeof...(Args), LUA_MULTRET, 0); - if (code != LUABRIDGE_LUA_OK) - { - auto ec = makeErrorCode(ErrorCode::LuaFunctionCallFailed); - -#if LUABRIDGE_HAS_EXCEPTIONS - if (LuaException::areExceptionsEnabled()) - LuaException::raise(L, ec); -#else - return LuaResult::errorFromStack(L, ec); -#endif - } - - return LuaResult::valuesFromStack(L, stackTop); -} - -inline int pcall(lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0) -{ - const int code = lua_pcall(L, nargs, nresults, msgh); - -#if LUABRIDGE_HAS_EXCEPTIONS - if (code != LUABRIDGE_LUA_OK && LuaException::areExceptionsEnabled()) - LuaException::raise(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed)); -#endif - - return code; -} - -template -template -LuaResult LuaRefBase::operator()(Args&&... args) const -{ - return call(*this, std::forward(args)...); -} - -} - - -// End File: Source/LuaBridge/detail/Invoke.h - -// Begin File: Source/LuaBridge/detail/Iterator.h - -namespace luabridge { - -class Iterator -{ -public: - explicit Iterator(const LuaRef& table, bool isEnd = false) - : m_L(table.state()) - , m_table(table) - , m_key(table.state()) - , m_value(table.state()) - { - if (! isEnd) - { - next(); - } - } - - lua_State* state() const noexcept - { - return m_L; - } - - std::pair operator*() const - { - return std::make_pair(m_key, m_value); - } - - LuaRef operator->() const - { - return m_value; - } - - bool operator!=(const Iterator& rhs) const - { - assert(m_L == rhs.m_L); - - return ! m_table.rawequal(rhs.m_table) || ! m_key.rawequal(rhs.m_key); - } - - Iterator& operator++() - { - if (isNil()) - { - - return *this; - } - else - { - next(); - return *this; - } - } - - bool isNil() const noexcept - { - return m_key.isNil(); - } - - LuaRef key() const - { - return m_key; - } - - LuaRef value() const - { - return m_value; - } - -private: - - Iterator operator++(int); - - void next() - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - { - m_key = LuaNil(); - m_value = LuaNil(); - return; - } -#endif - - m_table.push(); - m_key.push(); - - if (lua_next(m_L, -2)) - { - m_value.pop(); - m_key.pop(); - } - else - { - m_key = LuaNil(); - m_value = LuaNil(); - } - - lua_pop(m_L, 1); - } - - lua_State* m_L = nullptr; - LuaRef m_table; - LuaRef m_key; - LuaRef m_value; -}; - -class Range -{ -public: - Range(const Iterator& begin, const Iterator& end) - : m_begin(begin) - , m_end(end) - { - } - - const Iterator& begin() const noexcept - { - return m_begin; - } - - const Iterator& end() const noexcept - { - return m_end; - } - -private: - Iterator m_begin; - Iterator m_end; -}; - -inline Range pairs(const LuaRef& table) -{ - return Range{ Iterator(table, false), Iterator(table, true) }; -} - -} - - -// End File: Source/LuaBridge/detail/Iterator.h - -// Begin File: Source/LuaBridge/detail/Security.h - -namespace luabridge { - -class Security -{ -public: - static bool hideMetatables() noexcept - { - return getSettings().hideMetatables; - } - - static void setHideMetatables(bool shouldHide) noexcept - { - getSettings().hideMetatables = shouldHide; - } - -private: - struct Settings - { - Settings() noexcept - : hideMetatables(true) - { - } - - bool hideMetatables; - }; - - static Settings& getSettings() noexcept - { - static Settings settings; - return settings; - } -}; - -template -TypeResult getGlobal(lua_State* L, const char* name) -{ - lua_getglobal(L, name); - - auto result = luabridge::Stack::get(L, -1); - - lua_pop(L, 1); - - return result; -} - -template -bool setGlobal(lua_State* L, T&& t, const char* name) -{ - if (auto result = push(L, std::forward(t))) - { - lua_setglobal(L, name); - return true; - } - - return false; -} - -inline void setHideMetatables(bool shouldHide) noexcept -{ - Security::setHideMetatables(shouldHide); -} - -} - - -// End File: Source/LuaBridge/detail/Security.h - -// Begin File: Source/LuaBridge/detail/Namespace.h - -namespace luabridge { -namespace detail { - -class Registrar -{ -protected: - Registrar(lua_State* L) - : L(L) - , m_stackSize(0) - { - } - - Registrar(lua_State* L, int skipStackPops) - : L(L) - , m_stackSize(0) - , m_skipStackPops(skipStackPops) - { - } - - Registrar(const Registrar& rhs) - : L(rhs.L) - , m_stackSize(std::exchange(rhs.m_stackSize, 0)) - , m_skipStackPops(std::exchange(rhs.m_skipStackPops, 0)) - { - } - - Registrar& operator=(const Registrar& rhs) - { - m_stackSize = rhs.m_stackSize; - m_skipStackPops = rhs.m_skipStackPops; - - return *this; - } - - ~Registrar() - { - const int popsCount = m_stackSize - m_skipStackPops; - if (popsCount > 0) - { - assert(popsCount <= lua_gettop(L)); - - lua_pop(L, popsCount); - } - } - - void assertIsActive() const - { - if (m_stackSize == 0) - { - throw_or_assert("Unable to continue registration"); - } - } - - lua_State* const L = nullptr; - int mutable m_stackSize = 0; - int mutable m_skipStackPops = 0; -}; - -} - -class Namespace : public detail::Registrar -{ - -#if 0 - - static int luaError(lua_State* L, std::string message) - { - assert(lua_isstring(L, lua_upvalueindex(1))); - std::string s; - - lua_Debug ar; - - int result = lua_getstack(L, 2, &ar); - if (result != 0) - { - lua_getinfo(L, "Sl", &ar); - s = ar.short_src; - if (ar.currentline != -1) - { - - lua_pushnumber(L, ar.currentline); - s = s + ":" + lua_tostring(L, -1) + ": "; - lua_pop(L, 1); - } - } - - s = s + message; - - luaL_error(L, "%s", s.c_str()); - - return 0; - } -#endif - - class ClassBase : public detail::Registrar - { - public: - explicit ClassBase(Namespace& parent) - : Registrar(parent) - { - } - - using Registrar::operator=; - - protected: - - void createConstTable(const char* name, bool trueConst = true) - { - assert(name != nullptr); - - std::string type_name = std::string(trueConst ? "const " : "") + name; - - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setmetatable(L, -2); - - lua_pushstring(L, type_name.c_str()); - lua_rawsetp(L, -2, detail::getTypeKey()); - - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); - - lua_pushcfunction_x(L, &detail::newindex_object_metamethod); - rawsetfield(L, -2, "__newindex"); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropgetKey()); - - if (Security::hideMetatables()) - { - lua_pushnil(L); - rawsetfield(L, -2, "__metatable"); - } - } - - void createClassTable(const char* name) - { - assert(name != nullptr); - - createConstTable(name, false); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropsetKey()); - - lua_pushvalue(L, -2); - lua_rawsetp(L, -2, detail::getConstKey()); - - lua_pushvalue(L, -1); - lua_rawsetp(L, -3, detail::getClassKey()); - } - - void createStaticTable(const char* name) - { - assert(name != nullptr); - - lua_newtable(L); - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setmetatable(L, -3); - lua_insert(L, -2); - rawsetfield(L, -5, name); - -#if 0 - lua_pushlightuserdata(L, this); - lua_pushcclosure_x(L, &tostringMetaMethod, 1); - rawsetfield(L, -2, "__tostring"); -#endif - - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); - - lua_pushcfunction_x(L, &detail::newindex_static_metamethod); - rawsetfield(L, -2, "__newindex"); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropgetKey()); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropsetKey()); - - lua_pushvalue(L, -2); - lua_rawsetp(L, -2, detail::getClassKey()); - - if (Security::hideMetatables()) - { - lua_pushnil(L); - rawsetfield(L, -2, "__metatable"); - } - } - - void assertStackState() const - { - - assert(lua_istable(L, -3)); - assert(lua_istable(L, -2)); - assert(lua_istable(L, -1)); - } - }; - - template - class Class : public ClassBase - { - public: - - Class(const char* name, Namespace& parent) - : ClassBase(parent) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - rawgetfield(L, -1, name); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - - createConstTable(name); -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); - rawsetfield(L, -2, "__gc"); -#endif - ++m_stackSize; - - createClassTable(name); -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); - rawsetfield(L, -2, "__gc"); -#endif - ++m_stackSize; - - createStaticTable(name); - ++m_stackSize; - - lua_pushvalue(L, -1); - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getStaticRegistryKey()); - lua_pushvalue(L, -2); - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - lua_pushvalue(L, -3); - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); - } - else - { - assert(lua_istable(L, -1)); - ++m_stackSize; - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); - lua_insert(L, -2); - ++m_stackSize; - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - lua_insert(L, -2); - ++m_stackSize; - } - } - - Class(const char* name, Namespace& parent, void const* const staticKey) - : ClassBase(parent) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - createConstTable(name); -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); - rawsetfield(L, -2, "__gc"); -#endif - ++m_stackSize; - - createClassTable(name); -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); - rawsetfield(L, -2, "__gc"); -#endif - ++m_stackSize; - - createStaticTable(name); - ++m_stackSize; - - lua_rawgetp(L, LUA_REGISTRYINDEX, staticKey); - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - - throw_or_assert("Base class is not registered"); - return; - } - - assert(lua_istable(L, -1)); - - lua_rawgetp(L, -1, detail::getClassKey()); - assert(lua_istable(L, -1)); - - lua_rawgetp(L, -1, detail::getConstKey()); - assert(lua_istable(L, -1)); - - lua_rawsetp(L, -6, detail::getParentKey()); - lua_rawsetp(L, -4, detail::getParentKey()); - lua_rawsetp(L, -2, detail::getParentKey()); - - lua_pushvalue(L, -1); - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getStaticRegistryKey()); - lua_pushvalue(L, -2); - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - lua_pushvalue(L, -3); - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); - } - - Namespace endClass() - { - assert(m_stackSize > 3); - - m_stackSize -= 3; - lua_pop(L, 3); - return Namespace(*this); - } - - template >> - Class& addStaticProperty(const char* name, const U* value) - { - assert(name != nullptr); - assertStackState(); - - lua_pushlightuserdata(L, const_cast(value)); - lua_pushcclosure_x(L, &detail::property_getter::call, 1); - detail::add_property_getter(L, name, -2); - - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template >> - Class& addStaticProperty(const char* name, U* value, bool isWritable = true) - { - assert(name != nullptr); - assertStackState(); - - lua_pushlightuserdata(L, value); - lua_pushcclosure_x(L, &detail::property_getter::call, 1); - detail::add_property_getter(L, name, -2); - - if (isWritable) - { - lua_pushlightuserdata(L, value); - lua_pushcclosure_x(L, &detail::property_setter::call, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template - Class& addStaticProperty(const char* name, U (*get)(), void (*set)(U) = nullptr) - { - assert(name != nullptr); - assertStackState(); - - lua_pushlightuserdata(L, reinterpret_cast(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template - Class& addStaticProperty(const char* name, U (*get)() noexcept, void (*set)(U) noexcept = nullptr) - { - assert(name != nullptr); - assertStackState(); - - lua_pushlightuserdata(L, reinterpret_cast(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template >> - Class& addStaticProperty(const char* name, Getter get) - { - assert(name != nullptr); - assertStackState(); - - using GetType = decltype(get); - - lua_newuserdata_aligned(L, std::move(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - detail::add_property_getter(L, name, -2); - - return *this; - } - - template && !std::is_pointer_v>> - Class& addStaticProperty(const char* name, Getter get, Setter set) - { - assert(name != nullptr); - assertStackState(); - - using GetType = decltype(get); - using SetType = decltype(set); - - lua_newuserdata_aligned(L, std::move(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - detail::add_property_getter(L, name, -2); - - lua_newuserdata_aligned(L, std::move(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - detail::add_property_setter(L, name, -2); - - return *this; - } - - template - auto addStaticFunction(const char* name, Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Class&> - { - assert(name != nullptr); - assertStackState(); - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_function(L, std::move(functions)); - - } (), ...); - } - else - { - - lua_createtable(L, static_cast(sizeof...(Functions)), 0); - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, name); - - return *this; - } - - template - Class& addProperty(const char* name, U V::*mp, bool isWritable = true) - { - static_assert(std::is_base_of_v); - - using MemberPtrType = decltype(mp); - - assert(name != nullptr); - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(MemberPtrType))) MemberPtrType(mp); - lua_pushcclosure_x(L, &detail::property_getter::call, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (isWritable) - { - new (lua_newuserdata_x(L, sizeof(MemberPtrType))) MemberPtrType(mp); - lua_pushcclosure_x(L, &detail::property_setter::call, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (T::*get)() const, void (T::*set)(TS) = nullptr) - { - using GetType = TG (T::*)() const; - using SetType = void (T::*)(TS); - - assert(name != nullptr); - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (T::*get)() const noexcept, void (T::*set)(TS) noexcept = nullptr) - { - using GetType = TG (T::*)() const noexcept; - using SetType = void (T::*)(TS) noexcept; - - assert(name != nullptr); - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (T::*get)(lua_State*) const, void (T::*set)(TS, lua_State*) = nullptr) - { - using GetType = TG (T::*)(lua_State*) const; - using SetType = void (T::*)(TS, lua_State*); - - assert(name != nullptr); - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (T::*get)(lua_State*) const noexcept, void (T::*set)(TS, lua_State*) noexcept = nullptr) - { - using GetType = TG (T::*)(lua_State*) const noexcept; - using SetType = void (T::*)(TS, lua_State*) noexcept; - - assert(name != nullptr); - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (*get)(const T*), void (*set)(T*, TS) = nullptr) - { - assert(name != nullptr); - assertStackState(); - - lua_pushlightuserdata(L, reinterpret_cast(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - lua_pushlightuserdata( L, reinterpret_cast(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (*get)(const T*) noexcept, void (*set)(T*, TS) noexcept = nullptr) - { - assert(name != nullptr); - assertStackState(); - - lua_pushlightuserdata(L, reinterpret_cast(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - lua_pushlightuserdata( L, reinterpret_cast(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - Class& addProperty(const char* name, lua_CFunction get, lua_CFunction set = nullptr) - { - assert(name != nullptr); - assertStackState(); - - lua_pushcfunction_x(L, get); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -5); - detail::add_property_getter(L, name, -3); - - if (set != nullptr) - { - lua_pushcfunction_x(L, set); - detail::add_property_setter(L, name, -3); - } - - return *this; - } - - template >> - Class& addProperty(const char* name, Getter get) - { - using FirstArg = detail::function_argument_t<0, Getter>; - static_assert(std::is_same_v>, T>); - - assert(name != nullptr); - assertStackState(); - - using GetType = decltype(get); - - lua_newuserdata_aligned(L, std::move(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - lua_pushvalue(L, -1); - detail::add_property_getter(L, name, -4); - detail::add_property_getter(L, name, -4); - - return *this; - } - - template && !std::is_pointer_v>> - Class& addProperty(const char* name, Getter get, Setter set) - { - addProperty(name, std::move(get)); - - using FirstArg = detail::function_argument_t<0, Setter>; - static_assert(std::is_same_v>, T>); - - assert(name != nullptr); - assertStackState(); - - using SetType = decltype(set); - - lua_newuserdata_aligned(L, std::move(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - detail::add_property_setter(L, name, -3); - - return *this; - } - - template - auto addFunction(const char* name, Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Class&> - { - assert(name != nullptr); - assertStackState(); - - if (name == std::string_view("__gc")) - { - throw_or_assert("__gc metamethod registration is forbidden"); - return *this; - } - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_member_function(L, std::move(functions)); - - } (), ...); - - if constexpr (detail::const_functions_count == 1) - { - lua_pushvalue(L, -1); - rawsetfield(L, -4, name); - rawsetfield(L, -4, name); - } - else - { - rawsetfield(L, -3, name); - } - } - else - { - - if constexpr (detail::const_functions_count > 0) - { - lua_createtable(L, static_cast(detail::const_functions_count), 0); - - int idx = 1; - - ([&] - { - if (!detail::is_const_function) - return; - - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::member_function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_member_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - assert(idx > 1); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - lua_pushvalue(L, -1); - rawsetfield(L, -4, name); - rawsetfield(L, -4, name); - } - - if constexpr (detail::non_const_functions_count > 0) - { - lua_createtable(L, static_cast(detail::non_const_functions_count), 0); - - int idx = 1; - - ([&] - { - if (detail::is_const_function) - return; - - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::member_function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_member_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - assert(idx > 1); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - rawsetfield(L, -3, name); - } - } - - return *this; - } - - template - auto addConstructor() - -> std::enable_if_t<(sizeof...(Functions) > 0), Class&> - { - assertStackState(); - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - lua_pushcclosure_x(L, &detail::constructor_placement_proxy>, 0); - - } (), ...); - } - else - { - - lua_createtable(L, static_cast(sizeof...(Functions)), 0); - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - lua_pushcclosure_x(L, &detail::constructor_placement_proxy>, 0); - lua_settable(L, -3); - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, "__call"); - - return *this; - } - - template - auto addConstructorFrom() - -> std::enable_if_t<(sizeof...(Functions) > 0), Class&> - { - assertStackState(); - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - lua_pushcclosure_x(L, &detail::constructor_container_proxy>, 0); - - } (), ...); - } - else - { - - lua_createtable(L, static_cast(sizeof...(Functions)), 0); - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - lua_pushcclosure_x(L, &detail::constructor_container_proxy>, 0); - lua_settable(L, -3); - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, "__call"); - - return *this; - } - - template - auto addConstructor(Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Class&> - { - static_assert(((detail::function_arity_excluding_v >= 1) && ...)); - static_assert(((std::is_same_v, void*>) && ...)); - - assertStackState(); - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_function(L, detail::constructor_forwarder(std::move(functions))); - - } (), ...); - } - else - { - - lua_createtable(L, static_cast(sizeof...(Functions)), 0); - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v) - 1); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_function(L, detail::constructor_forwarder(std::move(functions))); - lua_settable(L, -3); - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, "__call"); - - return *this; - } - - template - Class& addFactory(Allocator allocator, Deallocator deallocator) - { - assertStackState(); - - detail::push_function(L, detail::factory_forwarder(std::move(allocator), std::move(deallocator))); - rawsetfield(L, -2, "__call"); - - return *this; - } - - template - auto addIndexMetaMethod(Function function) - -> std::enable_if_t - && std::is_invocable_v, Class&> - { - using FnType = decltype(function); - - assertStackState(); - - lua_newuserdata_aligned(L, std::move(function)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - lua_rawsetp(L, -3, detail::getIndexFallbackKey()); - - return *this; - } - - Class& addIndexMetaMethod(LuaRef (*idxf)(T&, const LuaRef&, lua_State*)) - { - using FnType = decltype(idxf); - - assertStackState(); - - lua_pushlightuserdata(L, reinterpret_cast(idxf)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - lua_rawsetp(L, -3, detail::getIndexFallbackKey()); - - return *this; - } - - Class& addIndexMetaMethod(LuaRef (T::* idxf)(const LuaRef&, lua_State*)) - { - using MemFnPtr = decltype(idxf); - - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(MemFnPtr))) MemFnPtr(idxf); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - lua_rawsetp(L, -3, detail::getIndexFallbackKey()); - - return *this; - } - - template - auto addNewIndexMetaMethod(Function function) - -> std::enable_if_t - && std::is_invocable_v, Class&> - { - using FnType = decltype(function); - - assertStackState(); - - lua_newuserdata_aligned(L, std::move(function)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - lua_rawsetp(L, -3, detail::getNewIndexFallbackKey()); - - return *this; - } - - Class& addNewIndexMetaMethod(LuaRef (*idxf)(T&, const LuaRef&, const LuaRef&, lua_State*)) - { - using FnType = decltype(idxf); - - assertStackState(); - - lua_pushlightuserdata(L, reinterpret_cast(idxf)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - lua_rawsetp(L, -3, detail::getNewIndexFallbackKey()); - - return *this; - } - - Class& addNewIndexMetaMethod(LuaRef (T::* idxf)(const LuaRef&, const LuaRef&, lua_State*)) - { - using MemFnPtr = decltype(idxf); - - assertStackState(); - - new (lua_newuserdata_x(L, sizeof(MemFnPtr))) MemFnPtr(idxf); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - lua_rawsetp(L, -3, detail::getNewIndexFallbackKey()); - - return *this; - } - }; - - class Table : public detail::Registrar - { - public: - explicit Table(const char* name, Namespace& parent) - : Registrar(parent) - { - lua_newtable(L); - lua_pushvalue(L, -1); - rawsetfield(L, -3, name); - ++m_stackSize; - - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setmetatable(L, -3); - ++m_stackSize; - } - - using Registrar::operator=; - - template - Table& addFunction(const char* name, Function function) - { - using FnType = decltype(function); - - assert(name != nullptr); - assert(lua_istable(L, -1)); - - lua_newuserdata_aligned(L, std::move(function)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - rawsetfield(L, -3, name); - - return *this; - } - - template - Table& addMetaFunction(const char* name, Function function) - { - using FnType = decltype(function); - - assert(name != nullptr); - assert(lua_istable(L, -1)); - - lua_newuserdata_aligned(L, std::move(function)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - rawsetfield(L, -2, name); - - return *this; - } - - Namespace endTable() - { - assert(m_stackSize > 2); - - m_stackSize -= 2; - lua_pop(L, 2); - return Namespace(*this); - } - }; - -private: - struct FromStack {}; - - explicit Namespace(lua_State* L) - : Registrar(L) - { - lua_getglobal(L, "_G"); - - ++m_stackSize; - } - - Namespace(lua_State* L, FromStack) - : Registrar(L, 1) - { - assert(lua_istable(L, -1)); - - { - lua_pushvalue(L, -1); - - lua_setmetatable(L, -2); - - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropgetKey()); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropsetKey()); - } - - ++m_stackSize; - } - - Namespace(const char* name, Namespace& parent) - : Registrar(parent) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - rawgetfield(L, -1, name); - - if (lua_isnil(L, -1)) - { - lua_pop(L, 1); - - lua_newtable(L); - lua_pushvalue(L, -1); - - lua_setmetatable(L, -2); - - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); - - lua_pushcfunction_x(L, &detail::newindex_static_metamethod); - rawsetfield(L, -2, "__newindex"); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropgetKey()); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropsetKey()); - - lua_pushvalue(L, -1); - rawsetfield(L, -3, name); - } - - ++m_stackSize; - } - - explicit Namespace(ClassBase& child) - : Registrar(child) - { - } - - explicit Namespace(Table& child) - : Registrar(child) - { - } - - using Registrar::operator=; - -public: - - static Namespace getGlobalNamespace(lua_State* L) - { - return Namespace(L); - } - - static Namespace getNamespaceFromStack(lua_State* L) - { - return Namespace(L, FromStack{}); - } - - Namespace beginNamespace(const char* name) - { - assertIsActive(); - return Namespace(name, *this); - } - - Namespace endNamespace() - { - if (m_stackSize == 1) - { - throw_or_assert("endNamespace() called on global namespace"); - - return Namespace(*this); - } - - assert(m_stackSize > 1); - --m_stackSize; - lua_pop(L, 1); - return Namespace(*this); - } - - template - Namespace& addVariable(const char* name, const T& value) - { - if (m_stackSize == 1) - { - throw_or_assert("addVariable() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - auto result = Stack::push(L, static_cast(value)); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - } - else - { - auto result = Stack::push(L, value); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - } - - rawsetfield(L, -2, name); - - return *this; - } - - template - Namespace& addProperty(const char* name, T* value, bool isWritable = true) - { - if (m_stackSize == 1) - { - throw_or_assert("addProperty() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); - - lua_pushlightuserdata(L, value); - lua_pushcclosure_x(L, &detail::property_getter::call, 1); - detail::add_property_getter(L, name, -2); - - if (isWritable) - { - lua_pushlightuserdata(L, value); - lua_pushcclosure_x(L, &detail::property_setter::call, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template - Namespace& addProperty(const char* name, TG (*get)(), void (*set)(TS) = nullptr) - { - if (m_stackSize == 1) - { - throw_or_assert("addProperty() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); - - lua_pushlightuserdata(L, reinterpret_cast(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template - Namespace& addProperty(const char* name, TG (*get)() noexcept, void (*set)(TS) noexcept = nullptr) - { - if (m_stackSize == 1) - { - throw_or_assert("addProperty() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); - - lua_pushlightuserdata(L, reinterpret_cast(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template >> - Namespace& addProperty(const char* name, Getter get) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - auto enumGet = [get = std::move(get)] { return static_cast(get); }; - - using GetType = decltype(enumGet); - lua_newuserdata_aligned(L, std::move(enumGet)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - } - else - { - using GetType = decltype(get); - lua_newuserdata_aligned(L, std::move(get)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - } - - detail::add_property_getter(L, name, -2); - - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - detail::add_property_setter(L, name, -2); - - return *this; - } - - template && !std::is_pointer_v>> - Namespace& addProperty(const char* name, Getter get, Setter set) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - addProperty(name, std::move(get)); - - using SetType = decltype(set); - - lua_newuserdata_aligned(L, std::move(set)); - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); - detail::add_property_setter(L, name, -2); - - return *this; - } - - Namespace& addProperty(const char* name, lua_CFunction get, lua_CFunction set = nullptr) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - lua_pushcfunction_x(L, get); - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushcfunction_x(L, set); - detail::add_property_setter(L, name, -2); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - detail::add_property_setter(L, name, -2); - } - - return *this; - } - - template - auto addFunction(const char* name, Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Namespace&> - { - assert(name != nullptr); - assert(lua_istable(L, -1)); - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_function(L, std::move(functions)); - - } (), ...); - } - else - { - - lua_createtable(L, static_cast(sizeof...(Functions)), 0); - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, name); - - return *this; - } - - Table beginTable(const char* name) - { - assertIsActive(); - return Table(name, *this); - } - - template - Class beginClass(const char* name) - { - assertIsActive(); - return Class(name, *this); - } - - template - Class deriveClass(const char* name) - { - assertIsActive(); - return Class(name, *this, detail::getStaticRegistryKey()); - } -}; - -inline Namespace getGlobalNamespace(lua_State* L) -{ - return Namespace::getGlobalNamespace(L); -} - -inline Namespace getNamespaceFromStack(lua_State* L) -{ - return Namespace::getNamespaceFromStack(L); -} - -inline void registerMainThread(lua_State* L) -{ - register_main_thread(L); -} - -} - - -// End File: Source/LuaBridge/detail/Namespace.h - -// Begin File: Source/LuaBridge/detail/Overload.h - -namespace luabridge { -namespace detail { - -template -struct non_const_overload -{ - template - constexpr auto operator()(R (T::*ptr)(Args...)) const noexcept -> decltype(ptr) - { - return ptr; - } -}; - -template -struct const_overload -{ - template - constexpr auto operator()(R (T::*ptr)(Args...) const) const noexcept -> decltype(ptr) - { - return ptr; - } -}; - -template -struct overload : const_overload, non_const_overload -{ - using const_overload::operator(); - using non_const_overload::operator(); - - template - constexpr auto operator()(R (*ptr)(Args...)) const noexcept -> decltype(ptr) - { - return ptr; - } -}; - -} - -template constexpr detail::overload overload = {}; -template constexpr detail::const_overload constOverload = {}; -template constexpr detail::non_const_overload nonConstOverload = {}; - -} - - -// End File: Source/LuaBridge/detail/Overload.h - -// Begin File: Source/LuaBridge/detail/ScopeGuard.h - -namespace luabridge::detail { - -template -class ScopeGuard -{ -public: - template - ScopeGuard(V&& v) - : m_func(std::forward(v)) - , m_shouldRun(true) - { - } - - ~ScopeGuard() - { - if (m_shouldRun) - m_func(); - } - - void reset() - { - m_shouldRun = false; - } - -private: - F m_func; - bool m_shouldRun; -}; - -template -ScopeGuard(F&&) -> ScopeGuard; - -} - - -// End File: Source/LuaBridge/detail/ScopeGuard.h - -// Begin File: Source/LuaBridge/LuaBridge.h - -#define LUABRIDGE_MAJOR_VERSION 3 -#define LUABRIDGE_MINOR_VERSION 1 -#define LUABRIDGE_VERSION 301 - - -// End File: Source/LuaBridge/LuaBridge.h - -// Begin File: Source/LuaBridge/Map.h - -namespace luabridge { - -template -struct Stack> -{ - using Type = std::map; - - [[nodiscard]] static Result push(lua_State* L, const Type& map) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 0, static_cast(map.size())); - - for (auto it = map.begin(); it != map.end(); ++it) - { - auto result = Stack::push(L, it->first); - if (! result) - return result; - - result = Stack::push(L, it->second); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type map; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto value = Stack::get(L, -1); - if (! value) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto key = Stack::get(L, -2); - if (! key) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - map.emplace(*key, *value); - lua_pop(L, 1); - } - - return map; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} - - -// End File: Source/LuaBridge/Map.h - -// Begin File: Source/LuaBridge/RefCountedObject.h - -namespace luabridge { - -template -class RefCountedObjectType -{ -public: - - inline void incReferenceCount() const { ++refCount; } - - inline void decReferenceCount() const - { - assert(getReferenceCount() > 0); - - if (--refCount == 0) - delete this; - } - - inline int getReferenceCount() const { return static_cast(refCount); } - -protected: - - RefCountedObjectType() : refCount() {} - - virtual ~RefCountedObjectType() - { - - assert(getReferenceCount() == 0); - } - -private: - - CounterType mutable refCount; -}; - -typedef RefCountedObjectType RefCountedObject; - -template -class RefCountedObjectPtr -{ -public: - - typedef ReferenceCountedObjectClass ReferencedType; - - inline RefCountedObjectPtr() : referencedObject(0) {} - - inline RefCountedObjectPtr(ReferenceCountedObjectClass* const refCountedObject) - : referencedObject(refCountedObject) - { - if (refCountedObject != 0) - refCountedObject->incReferenceCount(); - } - - inline RefCountedObjectPtr(const RefCountedObjectPtr& other) - : referencedObject(other.referencedObject) - { - if (referencedObject != 0) - referencedObject->incReferenceCount(); - } - - inline RefCountedObjectPtr(RefCountedObjectPtr&& other) - : referencedObject(other.referencedObject) - { - other.referencedObject = 0; - } - - template - inline RefCountedObjectPtr(const RefCountedObjectPtr& other) - : referencedObject(static_cast(other.getObject())) - { - if (referencedObject != 0) - referencedObject->incReferenceCount(); - } - - RefCountedObjectPtr& operator=(const RefCountedObjectPtr& other) - { - return operator=(other.referencedObject); - } - - template - RefCountedObjectPtr& operator=(const RefCountedObjectPtr& other) - { - return operator=(static_cast(other.getObject())); - } - - RefCountedObjectPtr& operator=(RefCountedObjectPtr&& other) - { - using std::swap; - - swap(referencedObject, other.referencedObject); - - return *this; - } - - RefCountedObjectPtr& operator=(ReferenceCountedObjectClass* const newObject) - { - if (referencedObject != newObject) - { - if (newObject != 0) - newObject->incReferenceCount(); - - ReferenceCountedObjectClass* const oldObject = referencedObject; - referencedObject = newObject; - - if (oldObject != 0) - oldObject->decReferenceCount(); - } - - return *this; - } - - ~RefCountedObjectPtr() - { - if (referencedObject != 0) - referencedObject->decReferenceCount(); - } - - operator ReferenceCountedObjectClass*() const { return referencedObject; } - - ReferenceCountedObjectClass* operator->() const { return referencedObject; } - - ReferenceCountedObjectClass* getObject() const { return referencedObject; } - -private: - - ReferenceCountedObjectClass* referencedObject; -}; - -template -bool operator==(const RefCountedObjectPtr& object1, - ReferenceCountedObjectClass* const object2) -{ - return object1.getObject() == object2; -} - -template -bool operator==(const RefCountedObjectPtr& object1, - const RefCountedObjectPtr& object2) -{ - return object1.getObject() == object2.getObject(); -} - -template -bool operator==(ReferenceCountedObjectClass* object1, - RefCountedObjectPtr& object2) -{ - return object1 == object2.getObject(); -} - -template -bool operator!=(const RefCountedObjectPtr& object1, - const ReferenceCountedObjectClass* object2) -{ - return object1.getObject() != object2; -} - -template -bool operator!=(const RefCountedObjectPtr& object1, - RefCountedObjectPtr& object2) -{ - return object1.getObject() != object2.getObject(); -} - -template -bool operator!=(ReferenceCountedObjectClass* object1, - RefCountedObjectPtr& object2) -{ - return object1 != object2.getObject(); -} - -template -struct ContainerTraits> -{ - using Type = T; - - static RefCountedObjectPtr construct(T* c) { return c; } - - static T* get(RefCountedObjectPtr const& c) { return c.getObject(); } -}; - -} - - -// End File: Source/LuaBridge/RefCountedObject.h - -// Begin File: Source/LuaBridge/Set.h - -namespace luabridge { - -template -struct Stack> -{ - using Type = std::set; - - [[nodiscard]] static Result push(lua_State* L, const Type& set) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 0, static_cast(set.size())); - - auto it = set.cbegin(); - for (lua_Integer tableIndex = 1; it != set.cend(); ++tableIndex, ++it) - { - lua_pushinteger(L, tableIndex); - - auto result = Stack::push(L, *it); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeUnexpected(makeErrorCode(ErrorCode::InvalidTypeCast)); - - const StackRestore stackRestore(L); - - Type set; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (! item) - return makeUnexpected(makeErrorCode(ErrorCode::InvalidTypeCast)); - - set.emplace(*item); - lua_pop(L, 1); - } - - return set; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} - - -// End File: Source/LuaBridge/Set.h - -// Begin File: Source/LuaBridge/UnorderedMap.h - -namespace luabridge { - -template -struct Stack> -{ - using Type = std::unordered_map; - - [[nodiscard]] static Result push(lua_State* L, const Type& map) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 0, static_cast(map.size())); - - for (auto it = map.begin(); it != map.end(); ++it) - { - auto result = Stack::push(L, it->first); - if (! result) - return result; - - result = Stack::push(L, it->second); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type map; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto value = Stack::get(L, -1); - if (! value) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto key = Stack::get(L, -2); - if (! key) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - map.emplace(*key, *value); - lua_pop(L, 1); - } - - return map; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} - - -// End File: Source/LuaBridge/UnorderedMap.h - -// Begin File: Source/LuaBridge/Vector.h - -namespace luabridge { - -template -struct Stack> -{ - using Type = std::vector; - - [[nodiscard]] static Result push(lua_State* L, const Type& vector) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(vector.size()), 0); - - for (std::size_t i = 0; i < vector.size(); ++i) - { - lua_pushinteger(L, static_cast(i + 1)); - - auto result = Stack::push(L, vector[i]); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type vector; - vector.reserve(static_cast(get_length(L, index))); - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (! item) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - vector.emplace_back(*item); - lua_pop(L, 1); - } - - return vector; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} - - -// End File: Source/LuaBridge/Vector.h - -// Begin File: Source/LuaBridge/detail/Dump.h - -namespace luabridge { -namespace debug { - -inline void putIndent(std::ostream& stream, unsigned level) -{ - for (unsigned i = 0; i < level; ++i) - { - stream << " "; - } -} - -inline void dumpTable(lua_State* L, int index, std::ostream& stream, unsigned level); - -inline void dumpValue(lua_State* L, int index, std::ostream& stream, unsigned level = 0) -{ - const int type = lua_type(L, index); - switch (type) - { - case LUA_TNIL: - stream << "nil"; - break; - - case LUA_TBOOLEAN: - stream << (lua_toboolean(L, index) ? "true" : "false"); - break; - - case LUA_TNUMBER: - stream << lua_tonumber(L, index); - break; - - case LUA_TSTRING: - stream << '"' << lua_tostring(L, index) << '"'; - break; - - case LUA_TFUNCTION: - if (lua_iscfunction(L, index)) - { - stream << "cfunction@" << lua_topointer(L, index); - } - else - { - stream << "function@" << lua_topointer(L, index); - } - break; - - case LUA_TTHREAD: - stream << "thread@" << lua_tothread(L, index); - break; - - case LUA_TLIGHTUSERDATA: - stream << "lightuserdata@" << lua_touserdata(L, index); - break; - - case LUA_TTABLE: - dumpTable(L, index, stream, level); - break; - - case LUA_TUSERDATA: - stream << "userdata@" << lua_touserdata(L, index); - break; - - default: - stream << lua_typename(L, type); - ; - break; - } -} - -inline void dumpTable(lua_State* L, int index, std::ostream& stream, unsigned level = 0) -{ - stream << "table@" << lua_topointer(L, index); - - if (level > 0) - { - return; - } - - index = lua_absindex(L, index); - stream << " {"; - lua_pushnil(L); - while (lua_next(L, index)) - { - stream << "\n"; - putIndent(stream, level + 1); - dumpValue(L, -2, stream, level + 1); - stream << ": "; - dumpValue(L, -1, stream, level + 1); - lua_pop(L, 1); - } - putIndent(stream, level); - stream << "\n}"; -} - -inline void dumpState(lua_State* L, std::ostream& stream = std::cerr) -{ - int top = lua_gettop(L); - for (int i = 1; i <= top; ++i) - { - stream << "stack #" << i << ": "; - dumpValue(L, i, stream, 0); - stream << "\n"; - } -} - -} -} - - -// End File: Source/LuaBridge/detail/Dump.h -// clang-format on - diff --git a/LuaBridge3/Doxyfile b/LuaBridge3/Doxyfile deleted file mode 100644 index 8152a95..0000000 --- a/LuaBridge3/Doxyfile +++ /dev/null @@ -1,326 +0,0 @@ -# Doxyfile 1.8.13 - -#--------------------------------------------------------------------------- -# Project related configuration options -#--------------------------------------------------------------------------- -DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = LuaBridge -PROJECT_NUMBER = -PROJECT_BRIEF = -PROJECT_LOGO = -OUTPUT_DIRECTORY = -CREATE_SUBDIRS = NO -ALLOW_UNICODE_NAMES = NO -OUTPUT_LANGUAGE = English -BRIEF_MEMBER_DESC = YES -REPEAT_BRIEF = NO -ABBREVIATE_BRIEF = -ALWAYS_DETAILED_SEC = NO -INLINE_INHERITED_MEMB = YES -FULL_PATH_NAMES = NO -STRIP_FROM_PATH = -STRIP_FROM_INC_PATH = -SHORT_NAMES = NO -JAVADOC_AUTOBRIEF = NO -QT_AUTOBRIEF = NO -MULTILINE_CPP_IS_BRIEF = NO -INHERIT_DOCS = YES -SEPARATE_MEMBER_PAGES = NO -TAB_SIZE = 4 -ALIASES = -TCL_SUBST = -OPTIMIZE_OUTPUT_FOR_C = NO -OPTIMIZE_OUTPUT_JAVA = NO -OPTIMIZE_FOR_FORTRAN = NO -OPTIMIZE_OUTPUT_VHDL = NO -EXTENSION_MAPPING = -MARKDOWN_SUPPORT = YES -TOC_INCLUDE_HEADINGS = 0 -AUTOLINK_SUPPORT = YES -BUILTIN_STL_SUPPORT = YES -CPP_CLI_SUPPORT = NO -SIP_SUPPORT = NO -IDL_PROPERTY_SUPPORT = YES -DISTRIBUTE_GROUP_DOC = NO -GROUP_NESTED_COMPOUNDS = NO -SUBGROUPING = YES -INLINE_GROUPED_CLASSES = NO -INLINE_SIMPLE_STRUCTS = NO -TYPEDEF_HIDES_STRUCT = NO -LOOKUP_CACHE_SIZE = 0 -#--------------------------------------------------------------------------- -# Build related configuration options -#--------------------------------------------------------------------------- -EXTRACT_ALL = NO -EXTRACT_PRIVATE = YES -EXTRACT_PACKAGE = NO -EXTRACT_STATIC = NO -EXTRACT_LOCAL_CLASSES = NO -EXTRACT_LOCAL_METHODS = NO -EXTRACT_ANON_NSPACES = NO -HIDE_UNDOC_MEMBERS = NO -HIDE_UNDOC_CLASSES = NO -HIDE_FRIEND_COMPOUNDS = NO -HIDE_IN_BODY_DOCS = NO -INTERNAL_DOCS = NO -CASE_SENSE_NAMES = NO -HIDE_SCOPE_NAMES = NO -HIDE_COMPOUND_REFERENCE= NO -SHOW_INCLUDE_FILES = NO -SHOW_GROUPED_MEMB_INC = NO -FORCE_LOCAL_INCLUDES = NO -INLINE_INFO = NO -SORT_MEMBER_DOCS = NO -SORT_BRIEF_DOCS = NO -SORT_MEMBERS_CTORS_1ST = YES -SORT_GROUP_NAMES = YES -SORT_BY_SCOPE_NAME = YES -STRICT_PROTO_MATCHING = NO -GENERATE_TODOLIST = NO -GENERATE_TESTLIST = NO -GENERATE_BUGLIST = NO -GENERATE_DEPRECATEDLIST= NO -ENABLED_SECTIONS = -MAX_INITIALIZER_LINES = 30 -SHOW_USED_FILES = NO -SHOW_FILES = NO -SHOW_NAMESPACES = NO -FILE_VERSION_FILTER = -LAYOUT_FILE = -CITE_BIB_FILES = -#--------------------------------------------------------------------------- -# Configuration options related to warning and progress messages -#--------------------------------------------------------------------------- -QUIET = YES -WARNINGS = YES -WARN_IF_UNDOCUMENTED = YES -WARN_IF_DOC_ERROR = YES -WARN_NO_PARAMDOC = YES -WARN_AS_ERROR = NO -WARN_FORMAT = "$file:$line: $text" -WARN_LOGFILE = -#--------------------------------------------------------------------------- -# Configuration options related to the input files -#--------------------------------------------------------------------------- -INPUT = Source -INPUT_ENCODING = UTF-8 -FILE_PATTERNS = *.c \ - *.cpp \ - *.h \ - *.hpp -RECURSIVE = YES -EXCLUDE = -EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = -EXCLUDE_SYMBOLS = luabridge::detail::* -EXAMPLE_PATH = -EXAMPLE_PATTERNS = * -EXAMPLE_RECURSIVE = NO -IMAGE_PATH = -INPUT_FILTER = -FILTER_PATTERNS = -FILTER_SOURCE_FILES = NO -FILTER_SOURCE_PATTERNS = -USE_MDFILE_AS_MAINPAGE = -#--------------------------------------------------------------------------- -# Configuration options related to source browsing -#--------------------------------------------------------------------------- -SOURCE_BROWSER = NO -INLINE_SOURCES = NO -STRIP_CODE_COMMENTS = YES -REFERENCED_BY_RELATION = NO -REFERENCES_RELATION = NO -REFERENCES_LINK_SOURCE = YES -SOURCE_TOOLTIPS = YES -USE_HTAGS = NO -VERBATIM_HEADERS = NO -CLANG_ASSISTED_PARSING = NO -CLANG_OPTIONS = -#--------------------------------------------------------------------------- -# Configuration options related to the alphabetical class index -#--------------------------------------------------------------------------- -ALPHABETICAL_INDEX = NO -COLS_IN_ALPHA_INDEX = 5 -IGNORE_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the HTML output -#--------------------------------------------------------------------------- -GENERATE_HTML = YES -HTML_OUTPUT = Documentation -HTML_FILE_EXTENSION = .html -HTML_HEADER = -HTML_FOOTER = -HTML_STYLESHEET = -HTML_EXTRA_STYLESHEET = -HTML_EXTRA_FILES = -HTML_COLORSTYLE_HUE = 240 -HTML_COLORSTYLE_SAT = 64 -HTML_COLORSTYLE_GAMMA = 80 -HTML_TIMESTAMP = NO -HTML_DYNAMIC_SECTIONS = NO -HTML_INDEX_NUM_ENTRIES = 100 -GENERATE_DOCSET = NO -DOCSET_FEEDNAME = "Doxygen generated docs" -DOCSET_BUNDLE_ID = org.doxygen.Project -DOCSET_PUBLISHER_ID = org.doxygen.Publisher -DOCSET_PUBLISHER_NAME = Publisher -GENERATE_HTMLHELP = NO -CHM_FILE = -HHC_LOCATION = -GENERATE_CHI = NO -CHM_INDEX_ENCODING = -BINARY_TOC = NO -TOC_EXPAND = NO -GENERATE_QHP = NO -QCH_FILE = -QHP_NAMESPACE = org.doxygen.Project -QHP_VIRTUAL_FOLDER = doc -QHP_CUST_FILTER_NAME = -QHP_CUST_FILTER_ATTRS = -QHP_SECT_FILTER_ATTRS = -QHG_LOCATION = -GENERATE_ECLIPSEHELP = NO -ECLIPSE_DOC_ID = org.doxygen.Project -DISABLE_INDEX = NO -GENERATE_TREEVIEW = YES -ENUM_VALUES_PER_LINE = 4 -TREEVIEW_WIDTH = 250 -EXT_LINKS_IN_WINDOW = NO -FORMULA_FONTSIZE = 10 -FORMULA_TRANSPARENT = YES -USE_MATHJAX = NO -MATHJAX_FORMAT = HTML-CSS -MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest -MATHJAX_EXTENSIONS = -MATHJAX_CODEFILE = -SEARCHENGINE = YES -SERVER_BASED_SEARCH = NO -EXTERNAL_SEARCH = NO -SEARCHENGINE_URL = -SEARCHDATA_FILE = searchdata.xml -EXTERNAL_SEARCH_ID = -EXTRA_SEARCH_MAPPINGS = -#--------------------------------------------------------------------------- -# Configuration options related to the LaTeX output -#--------------------------------------------------------------------------- -GENERATE_LATEX = NO -LATEX_OUTPUT = latex -LATEX_CMD_NAME = latex -MAKEINDEX_CMD_NAME = makeindex -COMPACT_LATEX = NO -PAPER_TYPE = a4 -EXTRA_PACKAGES = -LATEX_HEADER = -LATEX_FOOTER = -LATEX_EXTRA_STYLESHEET = -LATEX_EXTRA_FILES = -PDF_HYPERLINKS = YES -USE_PDFLATEX = YES -LATEX_BATCHMODE = NO -LATEX_HIDE_INDICES = NO -LATEX_SOURCE_CODE = NO -LATEX_BIB_STYLE = plain -LATEX_TIMESTAMP = NO -#--------------------------------------------------------------------------- -# Configuration options related to the RTF output -#--------------------------------------------------------------------------- -GENERATE_RTF = NO -RTF_OUTPUT = rtf -COMPACT_RTF = NO -RTF_HYPERLINKS = NO -RTF_STYLESHEET_FILE = -RTF_EXTENSIONS_FILE = -RTF_SOURCE_CODE = NO -#--------------------------------------------------------------------------- -# Configuration options related to the man page output -#--------------------------------------------------------------------------- -GENERATE_MAN = NO -MAN_OUTPUT = man -MAN_EXTENSION = .3 -MAN_SUBDIR = -MAN_LINKS = NO -#--------------------------------------------------------------------------- -# Configuration options related to the XML output -#--------------------------------------------------------------------------- -GENERATE_XML = NO -XML_OUTPUT = xml -XML_PROGRAMLISTING = YES -#--------------------------------------------------------------------------- -# Configuration options related to the DOCBOOK output -#--------------------------------------------------------------------------- -GENERATE_DOCBOOK = NO -DOCBOOK_OUTPUT = docbook -DOCBOOK_PROGRAMLISTING = NO -#--------------------------------------------------------------------------- -# Configuration options for the AutoGen Definitions output -#--------------------------------------------------------------------------- -GENERATE_AUTOGEN_DEF = NO -#--------------------------------------------------------------------------- -# Configuration options related to the Perl module output -#--------------------------------------------------------------------------- -GENERATE_PERLMOD = NO -PERLMOD_LATEX = NO -PERLMOD_PRETTY = YES -PERLMOD_MAKEVAR_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the preprocessor -#--------------------------------------------------------------------------- -ENABLE_PREPROCESSING = YES -MACRO_EXPANSION = NO -EXPAND_ONLY_PREDEF = NO -SEARCH_INCLUDES = NO -INCLUDE_PATH = -INCLUDE_FILE_PATTERNS = -PREDEFINED = WIN32 \ - = \ - 1 -EXPAND_AS_DEFINED = -SKIP_FUNCTION_MACROS = YES -#--------------------------------------------------------------------------- -# Configuration options related to external references -#--------------------------------------------------------------------------- -TAGFILES = -GENERATE_TAGFILE = -ALLEXTERNALS = NO -EXTERNAL_GROUPS = YES -EXTERNAL_PAGES = YES -PERL_PATH = /bin/perl -#--------------------------------------------------------------------------- -# Configuration options related to the dot tool -#--------------------------------------------------------------------------- -CLASS_DIAGRAMS = NO -MSCGEN_PATH = -DIA_PATH = -HIDE_UNDOC_RELATIONS = YES -HAVE_DOT = NO -DOT_NUM_THREADS = 0 -DOT_FONTNAME = Helvetica -DOT_FONTSIZE = 10 -DOT_FONTPATH = -CLASS_GRAPH = YES -COLLABORATION_GRAPH = YES -GROUP_GRAPHS = YES -UML_LOOK = NO -UML_LIMIT_NUM_FIELDS = 10 -TEMPLATE_RELATIONS = NO -INCLUDE_GRAPH = YES -INCLUDED_BY_GRAPH = YES -CALL_GRAPH = NO -CALLER_GRAPH = NO -GRAPHICAL_HIERARCHY = YES -DIRECTORY_GRAPH = YES -DOT_IMAGE_FORMAT = png -INTERACTIVE_SVG = NO -DOT_PATH = -DOTFILE_DIRS = -MSCFILE_DIRS = -DIAFILE_DIRS = -PLANTUML_JAR_PATH = -PLANTUML_CFG_FILE = -PLANTUML_INCLUDE_PATH = -DOT_GRAPH_MAX_NODES = 50 -MAX_DOT_GRAPH_DEPTH = 0 -DOT_TRANSPARENT = NO -DOT_MULTI_TARGETS = NO -GENERATE_LEGEND = YES -DOT_CLEANUP = YES diff --git a/LuaBridge3/LICENSE.txt b/LuaBridge3/LICENSE.txt deleted file mode 100644 index c763a6c..0000000 --- a/LuaBridge3/LICENSE.txt +++ /dev/null @@ -1,27 +0,0 @@ -LuaBridge3 is published under the terms of the MIT License - - http://www.opensource.org/licenses/mit-license.html - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -* Copyright 2020, Lucio Asnaghi -* Copyright 2019, Dmitry Tarakanov -* Copyright 2012, Vinnie Falco -* Copyright 2008, Nigel Atkinson -* Copyright 2007, Nathan Reed diff --git a/LuaBridge3/Manual.md b/LuaBridge3/Manual.md deleted file mode 100644 index 460a5d8..0000000 --- a/LuaBridge3/Manual.md +++ /dev/null @@ -1,1840 +0,0 @@ -* * * - -LuaBridge 3.0 Reference Manual -============================== - -* * * - -LuaBridge3 repository is located at [https://github.com/kunitoki/LuaBridge3](https://github.com/kunitoki/LuaBridge3). -Official LuaBridge (up to version 2) repository is located at [https://github.com/vinniefalco/LuaBridge](https://github.com/vinniefalco/LuaBridge). - -* Copyright © 2020 Asnaghi Lucio. -* Copyright © 2019 Dmitry Tarakanov. -* Copyright © 2012 Vinnie Falco. -* Copyright © 2007 Nathan Reed. - -Freely available under the terms of the [MIT License](http://www.opensource.org/licenses/mit-license.html). - -Contents --------- - -* [1 - Introduction](#1---introduction) - - * [1.1 - Design](#11---design) - * [1.2 - Repository](#12---repository) - * [1.3 - License and Credits](#13---license-and-credits) - -* [2 - Accessing C++ from Lua](#2---accessing-c-from-lua) - - * [2.1 - Namespaces](#21---namespaces) - * [2.2 - Properties and Functions](#22---properties-and-functions) - * [2.3 - Class Objects](#23---class-objects) - * [2.4 - Property Member Proxies](#24---property-member-proxies) - * [2.5 - Function Member Proxies](#25---function-member-proxies) - * [2.5.1 - Function Overloading](#251---function-overloading) - * [2.6 - Constructors](#26---constructors) - * [2.6.1 - Constructor Proxies](#261---constructor-proxies) - * [2.6.2 - Constructor Factories](#262---constructor-factories) - * [2.7 - Index and New Index Metamethods Fallback](#27---index-and-new-index-metamethods-fallback) - * [2.8 - Lua Stack](#28---lua-stack) - * [2.9 - lua_State](#29---lua_state) - -* [3 - Passing Objects](#3---passing-objects) - - * [3.1 - C++ Lifetime](#31---c-lifetime) - * [3.2 - Lua Lifetime](#32---lua-lifetime) - * [3.3 - Pointers, References, and Pass by Value](#33---pointers-references-and-pass-by-value) - * [3.4 - Shared Lifetime](#34---shared-lifetime) - * [3.4.1 - Class RefCountedObjectPtr](#341---class-refcountedobjectptr) - * [3.4.2 - User-defined Containers](#342---user-defined-containers) - * [3.4.3 - shared_ptr As Container](#343---shared_ptr-as-container) - * [3.4.4 - Container Constructors](#344---container-constructors) - * [3.5 - Mixing Lifetimes](#35---mixing-lifetimes) - * [3.6 - Convenience Functions](#36---convenience-functions) - -* [4 - Accessing Lua from C++](#4---accessing-lua-from-c) - - * [4.1 - Class LuaRef](#41---class-luaref) - * [4.1.1 - Lifetime, States and Lua Threads](#411---lifetime-states-and-lua-threads) - * [4.1.2 - Type Conversions](#412---type-conversions) - * [4.2 - Table Proxies](#42---table-proxies) - * [4.3 - Calling Lua](#43---calling-lua) - * [4.3.1 - Exceptions](#431---exceptions) - * [4.3.2 - Class LuaException](#432---class-luaexception) - -* [5 - Security](#5---security) - -* [Appendix - API Reference](#appendix---api-reference) - -1 - Introduction -================ - -[LuaBridge](https://github.com/kunitoki/LuaBridge3) is a lightweight and dependency-free library for mapping data, functions, and classes back and forth between C++ and [Lua](http://wwww.lua.org), a powerful, fast, lightweight, embeddable scripting language. LuaBridge has been tested and works with Lua 5.1.5, 5.2.4, 5.3.6 and 5.4.4. It also works transparently with [LuaJIT](http://luajit.org/) 2.x onwards and for the first time also with [Luau](https://luau-lang.org/) 0.556 onwards. - -LuaBridge is usable from a compliant C++17 and offers the following features: - -* [MIT Licensed](http://www.opensource.org/licenses/mit-license.html), no usage restrictions! -* Headers-only: No Makefile, no .cpp files, just one `#include` and one header file (optional) ! -* Works with ANY lua version out there (PUC-Lua, LuaJIT, Luau, you name it). -* Simple, light, and nothing else needed. -* No macros, settings, or configuration scripts needed. -* Supports different object lifetime management models. -* Convenient, type-safe access to the Lua stack. -* Automatic function parameter type binding. -* Easy access to Lua objects like tables and functions. -* Expose C++ classes allowing them to use the flexibility of lua property lookup. -* Interoperable with most common c++ standard library container types. -* Written in a clear and easy to debug style. - -It also offers a set of improvements compared to vanilla LuaBridge: - -* The only binder library that works with PUC-Lua as well as LuaJIT and Luau, wonderful for game development ! -* Can work with both c++ exceptions and without (Works with `-fno-exceptions` and `/EHsc-`). -* Can safely register and use classes exposed across shared library boundaries. -* Full support for capturing lambdas in all namespace and class methods. -* Overloaded function support in Namespace functions, Class constructors, functions and static functions. -* Supports placement allocation or custom allocations/deallocations of C++ classes exposed to lua. -* Lightweight object creation: allow adding lua tables on the stack and register methods and metamethods in them. -* Allows for fallback `__index` and `__newindex` metamethods in exposed C++ classes, to support flexible and dynamic C++ classes ! -* Added `std::shared_ptr` to support shared C++/Lua lifetime for types deriving from `std::enable_shared_from_this`. -* Supports conversion to and from `std::nullptr_t`, `std::byte`, `std::pair`, `std::tuple` and `std::reference_wrapper`. -* Supports conversion to and from C style arrays of any supported type. -* Transparent support of all signed and unsigned integer types up to `int64_t`. -* Consistent numeric handling and conversions (signed, unsigned and floats) across all lua versions. -* Automatic handling of enum types by communicating with lua through `std::underlying_type_t`. -* Opt-out handling of safe stack space checks (automatically avoids exhausting lua stack space when pushing values!). - -LuaBridge is distributed as a a collection of header files. You simply add one line, `#include ` where you want to pass functions, classes, and variables back and forth between C++ and Lua. There are no additional source files, no compilation settings, and no Makefiles or IDE-specific project files. LuaBridge is easy to integrate. - -C++ concepts like variables and classes are made available to Lua through a process called _registration_. Because Lua is weakly typed, the resulting structure is not rigid. The API is based on C++ template metaprogramming. It contains template code to automatically generate at compile-time the various Lua C API calls necessary to export your program's classes and functions to the Lua environment. - -To expose Lua objects to C++, a class called `LuaRef` is provided. The implementation allows C++ code to access Lua objects such as numbers or strings, but more importantly to access things like tables and their values. Using this class makes idioms like calling Lua functions simple and clean. - -1.1 - Design ------------- - -LuaBridge tries to be efficient as possible when creating the "glue" that exposes C++ data and functions to Lua. At the same time, the code was written with the intention that it is all as simple and clear as possible, without resorting to obscure C++ idioms, ugly preprocessor macros, or configuration settings. Furthermore, it is designed to be "header-only", making it very easy to integrate into your projects. - -Because LuaBridge was written with simplicity in mind there are some features that are not available. Although it comes close to the highest possible performance, LuaBridge is not quite the fastest, [OOLua](http://code.google.com/p/oolua/) and [sol2](https://github.com/ThePhD/sol2) outperforms LuaBridge in some tests, but they are also bigger and slower to compile. While being powerful, LuaBridge is pretty compact and simpler to understand and debug, and also does not try to implement every possible feature: [LuaBind](http://www.rasterbar.com/products/luabind.html) (requires Boost) and [sol2](https://github.com/ThePhD/sol2) explores every corner of the C++ language. - -LuaBridge does not support: - -* Global types (types must be registered in a named scope). -* Automatic conversion between STL container types and Lua tables (but conversion can be enabled for `std::array`, `std::vector`, `std::map`, `std::unordered_map`, `std::set` `std::list`, `std::optional`, by including `LuaBridge/Array.h`, `LuaBridge/Vector.h`, `LuaBridge/Map`, `LuaBridge/UnorderedMap.h`, `LuaBridge/Set.h`, `LuaBridge/List.h`, `LuaBridge/Optional.h` respectively) -* Inheriting Lua classes from C++ classes. -* Passing nil to a C++ function that expects a pointer or reference. - -1.2 - Repository ----------------- - -The official repository is located at [https://github.com/kunitoki/LuaBridge3](https://github.com/kunitoki/LuaBridge3). - -The **master** branch contains published library versions. Release versions are marked with tags. - -1.3 - License and Credits -------------------------- - -LuaBridge3 is published under the terms of the [MIT License](http://www.opensource.org/licenses/mit-license.html): - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -The original version of LuaBridge was written by Nathan Reed. The project has been taken over by Vinnie Falco, who added new functionality and wrote the new documentation. Vinnie also incorporated `LuaRef` and other Lua to C++ binding contributions from Nigel Atkinson. - -For questions, comments, or bug reports feel free to open a Github issue or contact Lucio Asnaghi directly at the email address indicated below. - -* Copyright 2020, Lucio Asnaghi [](mailto:kunitoki@gmail.com) -* Copyright 2019, Dmitry Tarakanov -* Copyright 2012, Vinnie Falco [](mailto:vinnie.falco@gmail.com) -* Copyright 2008, Nigel Atkinson [](mailto:suprapilot+LuaCode@gmail.com) -* Copyright 2007, Nathan Reed - -Older versions of LuaBridge up to and including 0.2 (available separately) are distributed under the BSD 3-Clause License. See the corresponding license file in those versions (distributed separately) for more details. - -2 - Accessing C++ from Lua -========================== - -In order to expose C++ data and functions to Lua, each piece of exported information must be _registered_. There are five types of objects that LuaBridge can register: - -**Namespaces**   - -A Lua table that contains other registrations. - -**Data**   - -Global or static variables, data members, and static data members. - -**Functions** - -Regular functions, member functions, and static member functions. - -**CFunctions** - -A regular function, member function, or static member function that uses the `lua_CFunction` calling convention. - -**Properties** - -Global properties, property members, and static property members. These appear like data to Lua, but are implemented in C++ using functions to get and set the values. - -Both data and properties can be marked as _read-only_ at the time of registration. This is different from `const`; the values of these objects can be modified on the C++ side, but Lua scripts cannot change them. Code samples that follow are in C++ or Lua, depending on context. For brevity of exposition code samples in C++ assume the traditional variable `lua_State* L` is defined. - -2.1 - Namespaces ----------------- - -All LuaBridge registrations take place in a _namespace_. When we refer to a _namespace_ we are always talking about a namespace in the Lua sense, which is implemented using tables. The namespace need not correspond to a C++ namespace; in fact no C++ namespaces need to exist at all unless you want them to. LuaBridge namespaces are visible only to Lua scripts; they are used as a logical grouping tool. To obtain access to the global namespace we write: - -```cpp -luabridge::getGlobalNamespace (L); -``` - -This returns an object on which further registrations can be performed. The subsequent registrations will go into the global namespace, a practice which is not recommended. Instead, we can add our own namespace by writing: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test"); -``` - -This creates a table in `_G` called "test". Since we have not performed any registrations, this table will be empty except for some bookkeeping key/value pairs. LuaBridge reserves all identifiers that start with a double underscore. So `__test` would be an invalid name (although LuaBridge will silently accept it). Functions like `beginNamespace` return the corresponding object on which we can make more registrations. Given: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginNamespace ("detail") - .endNamespace () - .beginNamespace ("utility") - .endNamespace () - .endNamespace (); -``` - -The results are accessible to Lua as `test`, `test.detail`, and `test.utility`. Here we introduce the `endNamespace` function; it returns an object representing the original enclosing namespace. All LuaBridge functions which create registrations return an object upon which subsequent registrations can be made, allowing for an unlimited number of registrations to be chained together using the dot operator. Adding two objects with the same name, in the same namespace, results in undefined behavior (although LuaBridge will silently accept it). - -A namespace can be re-opened later to add more functions. This lets you split up the registration between different source files. These are equivalent: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .addFunction ("foo", foo) - .endNamespace (); - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .addFunction ("bar", bar) - .endNamespace (); -``` - -and - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .addFunction ("foo", foo) - .addFunction ("bar", bar) - .endNamespace (); -``` - -It's also possible to obtain a namespace from a table on the stack, and perform the registration of sub namespaces, properties, functions and classes into that table. - -```cpp -lua_newtable (L); - -luabridge::getNamespaceFromStack (L) - .addFunction ("test", +[] (int x) { return x; }) - .addFunction ("bar", &bar); -``` - -The table is still on top of the stack here and has not been popped, so it's possible to further manipulate it or eventually use it as environment for closures. - -2.2 - Properties and Functions ------------------------------- - -These are registered into a namespace using `addProperty` and `addFunction`. When registered functions are called by scripts, LuaBridge automatically takes care of the conversion of arguments into the appropriate data type when doing so is possible. This automated system works for the function's return value, and up to 8 parameters although more can be added by extending the templates. Pointers, references, and objects of class type as parameters are treated specially, and explained later. - -If we have: - -```cpp -int globalVar; -static float staticVar; - -std::string stringProperty; -std::string getString () { return stringProperty; } -void setString (std::string s) { stringProperty = s; } - -std::tuple tuple; - -int foo () { return 42; } -void bar (char const*) { } -int cFunc (lua_State* L) { return 0; } -``` - -These are registered with: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .addProperty ("var1", &globalVar) - .addProperty ("var2", &staticVar, false) // read-only - .addProperty ("prop1", getString, setString) - .addProperty ("prop2", getString) // read only - .addProperty ("tup", &tuple) - .addFunction ("foo", foo) - .addFunction ("bar", bar) - .addFunction ("cfunc", cFunc) - .endNamespace (); -``` - -Variables can be marked _read-only_ by passing `false` in the second optional parameter. If the parameter is omitted, _true_ is used making the variable read/write. Properties are marked read-only by omitting the set function. After the registrations above, the following Lua identifiers are valid: - -```lua -test -- a namespace -test.var1 -- a lua_Number property -test.var2 -- a read-only lua_Number property -test.prop1 -- a lua_String property -test.prop2 -- a read-only lua_String property -test.tup -- a lua_Table property mapping to a c++ tuple -test.foo -- a function returning a lua_Number -test.bar -- a function taking a lua_String as a parameter -test.cfunc -- a function with a variable argument list and multi-return -``` - -Note that `test.prop1` and `test.prop2` both refer to the same value. However, since `test.prop2` is read-only, assignment attempts will generate a run-time error. These Lua statements have the stated effects: - -```lua -test.var1 = 5 -- okay -test.var2 = 6 -- error: var2 is not writable -test.prop1 = "Hello" -- okay -test.prop1 = 68 -- okay, Lua converts the number to a string -test.prop2 = "bar" -- error: prop2 is not writable -test.tup = { 1, "a" } -- okay, converts a table to tuple with the same size -test.tup = { "size" } -- error: table has different size than tuple - -test.foo () -- calls foo and discards the return value -test.var1 = foo () -- calls foo and stores the result in var1 -test.bar ("Employee") -- calls bar with a string -test.bar (test) -- error: bar expects a string not a table -``` - -LuaBridge does not support overloaded functions nor is it likely to in the future. Since Lua is dynamically typed, any system that tries to resolve a set of parameters passed from a script will face considerable ambiguity when trying to choose an appropriately matching C++ function signature. - -2.3 - Class Objects -------------------- - -A class registration is opened using either `beginClass` or `deriveClass` and ended using `endClass`. Once registered, a class can later be re-opened for more registrations using `beginClass`. However, `deriveClass` should only be used once. To add more registrations to an already registered derived class, use `beginClass` on it. - -These declarations: - -```cpp -struct A { - static int staticData; - static float staticProperty; - - static float getStaticProperty () { return staticProperty; } - static void setStaticProperty (float f) { staticProperty = f; } - static void staticFunc () { } - - static int staticCFunc (lua_State *L) { return 0; } - - std::string dataMember; - - char dataProperty; - char getProperty () const { return dataProperty; } - void setProperty (char v) { dataProperty = v; } - std::string toString () const { return dataMember; } - - void func1 () { } - virtual void virtualFunc () { } - - int cfunc (lua_State* L) { return 0; } -}; - -struct B : public A { - double dataMember2; - - void func1 () { } - void func2 () { } - void virtualFunc () { } -}; - -int A::staticData; -float A::staticProperty; -``` - -are registered using: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("A") - .addStaticProperty ("staticData", &A::staticData) - .addStaticProperty ("staticProperty", &A::getStaticProperty, &A::setStaticProperty) - .addStaticFunction ("staticFunc", &A::staticFunc) - .addStaticFunction ("staticCFunc", &A::staticCFunc) - .addProperty ("data", &A::dataMember) - .addProperty ("prop", &A::getProperty, &A::setProperty) - .addFunction ("func1", &A::func1) - .addFunction ("virtualFunc", &A::virtualFunc) - .addFunction ("__tostring", &A::toString) // Metamethod - .addFunction ("cfunc", &A::cfunc) - .endClass () - .deriveClass ("B") - .addProperty ("data", &B::dataMember2) - .addFunction ("func1", &B::func1) - .addFunction ("func2", &B::func2) - .endClass () - .endNameSpace (); -``` - -Method registration works just like function registration. Virtual methods work normally; no special syntax is needed. const methods are detected and const-correctness is enforced, so if a function returns a const object (or a container holding to a const object) to Lua, that reference to the object will be considered const and only const methods can be called on it. It is possible to register Lua metamethods (except `__gc`). Destructors are registered automatically for each class. - -As with regular variables and properties, class properties can be marked read-only by passing false in the second parameter, or omitting the set set function. The `deriveClass` takes two template arguments: the class to be registered, and its base class. Inherited methods do not have to be re-declared and will function normally in Lua. If a class has a base class that is **not** registered with Lua, there is no need to declare it as a subclass. - -Remember that in Lua, the colon operator '`:`' is used for method call syntax: - -```lua -local a = A () - -a.func1 () -- error: func1 expects an object of a registered class -a.func1 (a) -- okay, verbose, this how OOP works in Lua -a:func1 () -- okay, less verbose, equvalent to the previous -``` - -2.4 - Property Member Proxies ------------------------------ - -Sometimes when registering a class which comes from a third party library, the data is not exposed in a way that can be expressed as a pointer to member, there are no get or set functions, or the get and set functons do not have the right function signature. Since the class declaration is closed for changes, LuaBridge allows for a _property member proxy_. This is a pair of get and set flat functions which take as their first parameter a pointer to the object. This is easily understood with the following example: - -```cpp -// Third party declaration, can't be changed -struct Vec -{ - float coord [3]; -}; -``` - -Taking the address of an array element, e.g. `&Vec::coord [0]` results in an error instead of a pointer-to-member. The class is closed for modifications, but we want to export Vec objects to Lua using the familiar object notation. To do this, first we add a "helper" class: - -```cpp -struct VecHelper -{ - template - static float get (Vec const* vec) - { - return vec->coord [index]; - } - - template - static void set (Vec* vec, float value) - { - vec->coord [index] = value; - } -}; -``` - -This helper class is only used to provide property member proxies. `Vec` continues to be used in the C++ code as it was before. Now we can register the `Vec` class with property member proxies for `x`, `y`, and `z`: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Vec") - .addProperty ("x", &VecHelper::get<0>, &VecHelper::set<0>) - .addProperty ("y", &VecHelper::get<1>, &VecHelper::set<1>) - .addProperty ("z", &VecHelper::get<2>, &VecHelper::set<2>) - .endClass () - .endNamespace (); -``` - -It is also possible to use both capturing and non capturing lambdas, as well as `std::function <>` instances as proxies: - -```cpp -std::function get_x = [] (const Vec* vec) { return vec->coord [0]; }; -std::function set_x = [] (Vec* vec, float v) { vec->coord [0] = v; }; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Vec") - .addProperty ("x", get_x, set_x) - // ... same for "y" and "z" - .endClass () - .endNamespace (); -``` - -Or the more concise version (notice the `+` before the lambda is useful to convert a non capturing lambda to a function pointer in order to avoid allocating a std::function internally, where storing the lambda as function pointer might avoid lua usertype allocation overhead): - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Vec") - .addProperty ("x", - +[] (const Vec* vec) { return vec->coord [0]; }, - +[] (Vec* vec, float v) { vec->coord [0] = v; }) - // ... same for "y" and "z" - .endClass () - .endNamespace (); -``` - -2.5 - Function Member Proxies ------------------------------ - -Where it is not possible or inconvenient to add a member to be registered, LuaBridge also allows for a _function member proxy_. This is a flat function which take as its first parameter a pointer to the object: - -```cpp -// Third party declaration, can't be changed -struct Vec -{ - float coord [3]; -}; -``` - -The class is closed for modifications, but we want to extend Vec objects with our member function. To do this, first we add a "helper" function: - -```cpp -void scale (Vec* vec, float value) -{ - vec->coord [0] *= value; - vec->coord [1] *= value; - vec->coord [2] *= value; -} -``` - -Now we can register the `Vec` class with a member function `scale`: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Vec") - .addFunction ("scale", &scale) - .endClass () - .endNamespace (); -``` - -It is also possible to use lambdas (both capturing and non capturing) as functions proxies: - -```cpp -float y = atan (1.0f) * 4.0f; - -luabridge::getGlobalNamespace (L) - .beginClass ("Vec") - .addFunction ("scaleX", +[] (Vec* vec, float v) { vec->coord [0] *= v; }) - .addFunction ("scaleY", [y] (Vec* vec, float v) { vec->coord [1] *= v * y; }) - .endClass () -``` - -Of course when not capturing, it is better to prefix the lambda with `+` so it is converted and stored internally to a function pointer instead of an `std::function`, so it is actually lighter to store and faster to call. - -### 2.5.1 - Function Overloading - -When specifying more than one method to the `addFunction` or `addStaticFunction` of both `Namespace` and `Class`, those overloads will be invoked in case of a call. Only overloads that have matched arguments arity will be considered, and they will be tried from first to last until the call succeeds. - -```cpp -struct Vec { float coord [3]; }; -struct Quat { float values [4]; }; - -void rotateByDegreees (Vec* vec, float degrees); -void rotateByQuaternion (Vec* vec, const Quat& quaternion); -``` - -Now we can register the `Vec` class with a member function `rotate` that will be resolving the call into the two provided overloads: - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Vec") - .addFunction ("rotate", &rotateByDegreees, &rotateByQuaternion) - .endClass () - .endNamespace (); -``` - -In case of members (or functions) with the same name, it's necessary to use `overload`, `constOverload` or `nonConstOverload` to disambiguate which of the functions needs to be registered: - -```cpp -struct Quat { float values [4]; }; - -struct Vec -{ - void rotate (float degrees); - void rotate (const Quat& quaternion); - - void x (float new_value); - float x (float value_if_zero) const; - - float coord [3]; -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Vec") - .addFunction ("rotate", - luabridge::overload (&Vec::rotate), - luabridge::overload (&Vec::rotate)) - .addFunction ("x", - luabridge::nonConstOverload (&Vec::x), - luabridge::constOverload (&Vec::x)) - .endClass () - .endNamespace (); - -``` - -It's possible to mix lambdas, function pointers and member functions in overload creation. Providing a `lua_Cfunction` as last method will ensure it can be reached in case no other overload is successfully executed, kind of like a "catch all" method. - -Special attention needs to be given to the order (priority) of the overloads, based on the number and type of the arguments. Better to place first the overloads that can be called more frequently, and putting "stronger" types first: for example when having an overload taking an `int` and an overload taking `float`, as lua is not able to distinguish between them properly (until lua 5.4) the first overload will always be called. - -2.6 - Constructors ------------------- - -A single constructor may be added for a class using `addConstructor`. LuaBridge cannot automatically determine the number and types of constructor parameters like it can for functions and methods, so you must provide them. This is done by specifying the signature of the desired constructor functions as template parameters to `addConstructor`. The parameter types will be extracted from this (the return type is ignored). For example, these statements register constructors for the given classes: - -```cpp -struct A -{ - A (); - A (std::string_view a); - A (std::string_view a, int b); -}; - -struct B -{ - explicit B (const char* s, int nChars); -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("A") - .addConstructor () - .endClass () - .beginClass ("B") - .addConstructor () - .endClass () - .endNamespace (); -``` - -Constructors added in this fashion are called from Lua using the fully qualified name of the class. This Lua code will create instances of `A` and `B`. - -```lua -a = test.A () -- Create a new A. -b = test.B ("hello", 5) -- Create a new B. -b = test.B () -- Error: expected string in argument 1 -``` - -### 2.6.1 - Constructor Proxies - -Sometimes is not possible to use a constructor for a class, because some of the constructor arguments have types that couldn't be exposed to lua, or more control is needed when constructors need to be invoked (like checking the lau stack). So it is possible to workaround those limitations by using a special `addConstructor` that doesn't need any template specialiation, but takes only one or more functors, which will allow to placement new the c++ class in a c++ lambda, specifying any custom parameter there: - -```cpp -struct NotExposed; -NotExposed* shouldNotSeeMe; - -struct HardToCreate -{ - explicit HardToCreate (const NotExposed* x, int easy); -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("HardToCreate") - .addConstructor ([&shouldNotSeeMe] (void* ptr, int easy) { return new (ptr) HardToCreate (shouldNotSeeMe, easy); }) - .endClass () - .endNamespace (); -``` - -Then in lua: - -```lua -hard = test.HardToCreate (5) -- Create a new HardToCreate. -``` - -The `addConstructor` overload taking a generic functor also accepts a `lua_State*` as last parameter in order to be used for constructors that needs to be overloaded by different numbers of arguments (arguments will start at index 2 of the stack): - -```cpp -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("HardToCreate") - .addConstructor ([] (void* ptr, lua_State* L) { return new (ptr) HardToCreate (shouldNotSeeMe, lua_checkinteger (L, 2)); }) - .endClass () - .endNamespace (); -``` - -As mentioned at the beginning, it's possible to specify multiple functors, that will be tried in order until the object can be constructed: - -```cpp -struct NotExposed; -NotExposed* shouldNotSeeMe; - -struct HardToCreate -{ - HardToCreate (const NotExposed* x, int easy); - HardToCreate (const NotExposed* x, int easy, int lessEasy); -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("HardToCreate") - .addConstructor ( - [&shouldNotSeeMe] (void* ptr, int easy) { return new (ptr) HardToCreate (shouldNotSeeMe, easy); }, - [&shouldNotSeeMe] (void* ptr, int easy, int lessEasy) { return new (ptr) HardToCreate (shouldNotSeeMe, easy, lessEasy); }) - .endClass () - .endNamespace (); -``` - -### 2.6.2 - Constructor Factories - -If granular control over allocation and deallocation of a type is needed, the `addFactory` method can be used to register both and allocator and deallocator of C++ type. This is useful in case of having classes being provided by factory methods from shared libraries. - -```cpp -struct IObject -{ - virtual void overridableMethod () const = 0; -}; - -// These might be defined in a shared library, returning concrete types. -extern "C" IObject* objectFactoryAllocator (); -extern "C" void objectFactoryDeallocator (IObject*); - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("Object") - .addFactory (&objectFactoryAllocator, &objectFactoryDeallocator) - .addFunction ("overridableMethod", &IObject::overridableMethod) - .endClass () - .endNamespace (); -``` - -The object is the perfectly instantiable through lua: - -```lua -a = test.Object () -- Create a new Object using objectFactoryAllocator -a = nil -- Remove any reference count -collectgarbage ("collect") -- The object is garbage collected using objectFactoryDeallocator -``` - -2.7 - Index and New Index Metamethods Fallback ----------------------------------------------- - -In general LuaBridge for each class will add a `__index` and `__newindex` metamethods in order to be able to handle member function, properties and inheritance resolution. This will make it impossible for a user to override them because in doing so, we'll make the exposed classes non functioning. - -If a LuaBridge exposed class still need to handle the case of handling `__index` and `__newindex` metamethods calls, it's possible to use the `addIndexMetaMethod` and `addNewIndexMetaMethod` registration functions that will be executed as fallback in case an already existing function/property is not exposed in the class itself or any of its parent. - -```cpp -struct FlexibleClass -{ - int propertyOne = 42; -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("FlexibleClass") - .addProperty ("propertyOne", &FlexibleClass::propertyOne) - .addIndexMetaMethod ([] (FlexibleClass& self, const luabridge::LuaRef& key, lua_State* L) - { - if (key.tostring () == "existingProperty") - return luabridge::LuaRef (L, 1337); - - return luabridge::LuaRef (L, luabridge::LuaNil ()); // or luaL_error("Failed lookup of key !") - }) - .endClass () - .endNamespace (); - -FlexibleClass flexi; -luabridge::pushGlobal (L, &flexi, "flexi"); -``` - -Then in lua: - -```lua -propertyOne = flexi.propertyOne -assert (propertyOne == 42, "Getting value from LuaBridge exposed property") - -propertyTwo = flexi.existingProperty -assert (propertyTwo == 1337, "Getting value from non exposed LuaBridge property via __index fallback") - -propertyThree = flexi.nonExistingProperty -assert (propertyThree == nil, "Getting value from non exposed LuaBridge property via __index fallback") -``` - -The same can be done for the `__newindex` metamethod fallback: - -```cpp -struct FlexibleClass -{ - std::unordered_map properties; -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("FlexibleClass") - .addIndexMetaMethod ([] (FlexibleClass& self, const luabridge::LuaRef& key, lua_State* L) - { - auto it = self.properties.find(key); - if (it != self.properties.end()) - return it->second; - - return luabridge::LuaRef (L, luabridge::LuaNil ()); // or luaL_error("Failed lookup of key !") - }) - .addNewIndexMetaMethod ([] (FlexibleClass& self, const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* L) - { - self.properties.emplace (std::make_pair (key, value)) - return luabridge::LuaRef (L, luabridge::LuaNil ()); - }) - .endClass () - .endNamespace (); - -FlexibleClass flexi; -luabridge::pushGlobal (L, &flexi, "flexi"); -``` - -Then in lua: - -```lua -propertyOne = flexi.propertyOne -assert (propertyOne == nil, "Value is not existing") - -flexi.propertyOne = 1337 - -propertyOne = flexi.propertyOne -assert (propertyOne == 1337, "Value is now present !") -``` - -2.8 - Lua Stack ---------------- - -In the Lua C API, all operations on the `lua_State` are performed through the Lua stack. In order to pass values back and forth between C++ and Lua, LuaBridge uses specializations of this template class concept: - -```cpp -namespace luabridge { - -template -struct Stack -{ - static Result push (lua_State* L, const T& value); - - static TypeResult get (lua_State* L, int index); - - static bool isInstance (lua_State* L, int index); -}; - -} // namespace luabridge -``` - -When a specialization of `Stack` exists for a given type `T` we say that the `T` is _convertible_. Throughout this document and the LuaBridge API, these types can be used anywhere a convertible type is expected. - -The Stack template class specializations are used automatically for variables, properties, data members, property members, function arguments and return values. These basic types are supported: - -* `bool` -* `char`, converted to a string of length one. -* `char const*` and `std::string` strings. -* `std::byte`, integers, `float`, `double`, `long double` converted to `Lua_number`. - -User-defined types which are convertible to one of the basic types are possible, simply provide a `Stack <>` specialization in the `luabridge` namespace for your user-defined type, modeled after the existing types. For example, here is a specialization for a `juce::String`: - -```cpp -namespace luabridge { - -template <> -struct Stack -{ - static Result push (lua_State* L, const juce::String& s) - { - lua_pushstring (L, s.toUTF8 ()); - return {}; - } - - static TypeResult get (lua_State* L, int index) - { - if (lua_type (L, index) != LUA_TSTRING) - return makeErrorCode (ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring (L, index, &length); - if (str == nullptr) - return makeErrorCode (ErrorCode::InvalidTypeCast); - - return juce::String::fromUTF8 (str); - } - - static bool isInstance (lua_State* L, int index) - { - return lua_type (L, index) == LUA_TSTRING; - } -}; - -} // namespace luabridge -``` - -To make sure the library can work without exceptions enabled, if for some reason the push and get of the value on/from the lua stack cannot be performed, it is mandatory to return a `Result` object that can be constructed from a `std::error_code`. It also good practice to resotre the stack to it's original state in case of failures: - -```cpp -namespace luabridge { - -template -struct Stack> -{ - static Result push (lua_State* L, const Array& array) - { - const int initialStackSize = lua_gettop (L); - - lua_createtable (L, static_cast (array.size ()), 0); - - for (std::size_t i = 0; i < array.size (); ++i) - { - lua_pushinteger (L, static_cast (i + 1)); - - auto result = Stack::push (L, array[i]); - if (! result) - { - lua_pop (L, lua_gettop (L) - initialStackSize); // Restore the stack - return result; // Forward the error code - } - - lua_settable (L, -3); - } - - return {}; // No error - } - - static TypeResult> get (lua_State* L, int index) - { - if (!lua_istable (L, index)) - return makeErrorCode (ErrorCode::InvalidTypeCast); - - const int initialStackSize = lua_gettop (L); - - Array a; - a.reserve (static_cast (get_length(L, index))); - - const int absIndex = lua_absindex (L, index); - - lua_pushnil (L); - while (lua_next (L, absIndex) != 0) - { - auto item = Stack::get (L, -1); - if (! item) - { - lua_pop (L, lua_gettop (L) - initialStackSize); - return item.error (); - } - - a.append (*item); - - lua_pop (L, 1); - } - - return a; - } -}; - -} // namespace luabridge -``` - -2.9 - lua_State ---------------- - -Sometimes it is convenient from within a bound function or member function to gain access to the `lua_State*` normally available to a lua_CFunction. With LuaBridge, all you need to do is add a `lua_State*` as the last parameter of your bound function: - -```cpp -void useState (lua_State* L); - -luabridge::getGlobalNamespace (L).addFunction ("useState", &useState); -``` - -You can still include regular arguments while receiving the state: - -```cpp -void useStateAndArgs (int i, std::string s, lua_State* L); - -luabridge::getGlobalNamespace (L).addFunction ("useStateAndArgs", &useStateAndArgs); -``` - -When the script calls `useStateAndArgs`, it passes only the integer and string parameters. LuaBridge takes care of inserting the `lua_State*` into the argument list for the corresponding C++ function. This will work correctly even for the state created by coroutines. Undefined behavior results if the `lua_State*` is not the last parameter. - -The same is applicable for properties. - -3 - Passing Objects -=================== - -An object of a registered class `T` may be passed to Lua as: - -**`T`** - -Passed by value (a copy), with _Lua lifetime_. - -**`const T`** - -Passed by value (a copy), with _Lua lifetime_. - -**`T*`** - -Passed by reference, with _C++ lifetime_. - -**`T&`** - -Passed by reference, with _C++ lifetime_. - -**`const T*`** - -Passed by const reference, with _C++ lifetime_. - -**`const T&`** - -Passed by const reference, with _C++ lifetime_. - -3.1 - C++ Lifetime ------------------- - -The creation and deletion of objects with _C++ lifetime_ is controlled by the C++ code. Lua does nothing when it garbage collects a reference to such an object. Specifically, the object's destructor is not called (since C++ owns it). Care must be taken to ensure that objects with C++ lifetime are not deleted while still being referenced by a `lua_State*`, or else undefined behavior results. In the previous examples, an instance of `A` can be passed to Lua with C++ lifetime, like this: - -```cpp -A a; - -luabridge::push (L, &a); // pointer to 'a', C++ lifetime -lua_setglobal (L, "a"); - -luabridge::push (L, (const A*) &a); // pointer to 'a const', C++ lifetime -lua_setglobal (L, "ac"); - -luabridge::push (L, &a); // equivalent to push (L, (A const*) &a) -lua_setglobal (L, "ac2"); - -luabridge::push (L, new A); // compiles, but will leak memory -lua_setglobal (L, "ap"); -``` - -3.2 - Lua Lifetime ------------------- - -When an object of a registered class is passed by value to Lua, it will have _Lua lifetime_. A copy of the passed object is constructed inside the userdata. When Lua has no more references to the object, it becomes eligible for garbage collection. When the userdata is collected, the destructor for the class will be called on the object. Care must be taken to ensure that objects with Lua lifetime are not accessed by C++ after they are garbage collected, or else undefined behavior results. An instance of `B` can be passed to Lua with Lua lifetime this way: - -```cpp -B b; - -luabridge::push (L, b); // Copy of b passed, Lua lifetime. -lua_setglobal (L, "b"); -``` - -Given the previous code segments, these Lua statements are applicable: - -```lua -print (test.A.staticData) -- Prints the static data member. -print (test.A.staticProperty) -- Prints the static property member. -test.A.staticFunc () -- Calls the static method. - -print (a.data) -- Prints the data member. -print (a.prop) -- Prints the property member. -a:func1 () -- Calls A::func1 (). -test.A.func1 (a) -- Equivalent to a:func1 (). -test.A.func1 ("hello") -- Error: "hello" is not a class A. -a:virtualFunc () -- Calls A::virtualFunc (). - -print (b.data) -- Prints B::dataMember. -print (b.prop) -- Prints inherited property member. -b:func1 () -- Calls B::func1 (). -b:func2 () -- Calls B::func2 (). -test.B.func2 (a) -- Error: a is not a class B. -test.A.func1 (b) -- Calls A::func1 (). -b:virtualFunc () -- Calls B::virtualFunc (). -test.B.virtualFunc (b) -- Calls B::virtualFunc (). -test.A.virtualFunc (b) -- Calls B::virtualFunc (). -test.B.virtualFunc (a) -- Error: a is not a class B. - -a = nil; collectgarbage () -- 'a' still exists in C++. -b = nil; collectgarbage () -- Lua calls ~B() on the copy of b. -``` - -When Lua script creates an object of class type using a registered constructor, the resulting value will have Lua lifetime. After Lua no longer references the object, it becomes eligible for garbage collection. You can still pass these to C++, either by reference or by value. If passed by reference, the usual warnings apply about accessing the reference later, after it has been garbage collected. - -3.3 - Pointers, References, and Pass by Value ---------------------------------------------- - -When C++ objects are passed from Lua back to C++ as arguments to functions, or set as data members, LuaBridge does its best to automate the conversion. Using the previous definitions, the following functions may be registered to Lua: - -```cpp -void func0 (A a); -void func1 (A* a); -void func2 (A const* a); -void func3 (A& a); -void func4 (A const& a); -``` - -Executing this Lua code will have the prescribed effect: - -```lua -func0 (a) -- Passes a copy of a, using A's copy constructor. -func1 (a) -- Passes a pointer to a. -func2 (a) -- Passes a pointer to a const a. -func3 (a) -- Passes a reference to a. -func4 (a) -- Passes a reference to a const a. -``` - -In the example above, all functions can read the data members and property members of `a`, or call const member functions of `a`. Only `func0`, `func1`, and `func3` can modify the data members and data properties, or call non-const member functions of `a`. - -The usual C++ inheritance and pointer assignment rules apply. Given: - -```cpp -void func5 (B b); -void func6 (B* b); -``` - -These Lua statements hold: - -```lua -func5 (b) -- Passes a copy of b, using B's copy constructor. -func6 (b) -- Passes a pointer to b. -func6 (a) -- Error: Pointer to B expected. -func1 (b) -- Okay, b is a subclass of a. -``` - -When a pointer or pointer to const is passed to Lua and the pointer is null (zero), LuaBridge will pass Lua a `nil` instead. When Lua passes a `nil` to C++ where a pointer is expected, a null (zero) is passed instead. Attempting to pass a null pointer to a C++ function expecting a reference results in `lua_error` being called. - -3.4 - Shared Lifetime ---------------------- - -LuaBridge supports a _shared lifetime_ model: dynamically allocated and reference counted objects whose ownership is shared by both Lua and C++. The object remains in existence until there are no remaining C++ or Lua references, and Lua performs its usual garbage collection cycle. A container is recognized by a specialization of the `ContainerTraits` template class. LuaBridge will automatically recognize when a data type is a container when the correspoding specialization is present. Two styles of containers come with LuaBridge, including the necessary specializations. - -### 3.4.1 - Class RefCountedObjectPtr - -This is an intrusive style container. Your existing class declaration must be changed to be also derived from `RefCountedObject`. Given `class T`, derived from `RefCountedObject`, the container `RefCountedObjectPtr ` may be used. In order for reference counts to be maintained properly, all C++ code must store a container instead of the pointer. This is similar in style to `std::shared_ptr` although there are slight differences. For example: - -```cpp -// A is reference counted. -struct A : public luabridge::RefCountedObject -{ - void foo () { } -}; - -struct B -{ - RefCountedObjectPtr a; // holds a reference to A -}; - -void bar (luabridge::RefCountedObjectPtr a) -{ - a->foo (); -} -``` - -### 3.4.2 - User-defined Containers - -If you have your own container, you must provide a specialization of `ContainerTraits` in the `luabridge` namespace for your type before it will be recognized by LuaBridge (or else the code will not compile): - -```cpp -namespace luabridge { - -template -struct ContainerTraits> -{ - using Type = T; - - static CustomContainer construct (T* c) - { - return c; - } - - static T* get (const CustomContainer& c) - { - return c.getPointerToObject (); - } -}; - -} // namespace luabridge -``` - -Containers must be safely constructible from raw pointers to objects that are already referenced by other instances of the container (such as is the case for the provided containers or for example `boost::intrusive_ptr` but not `std::shared_ptr` or `boost::shared_ptr`). - -### 3.4.3 - shared_ptr As Container - -Standard containers like `std::shared_ptr` or `boost::shared_ptr` will work in LuaBridge3, but they require special care. This is because of type erasure; when the object goes from C++ to Lua and back to C++, constructing a new shared_ptr from the raw pointer will create another reference count and result in undefined behavior, unless it could intrusively reconstruct the container from a raw pointer. - -To overcome this issue classes that should be managed by `shared_ptr` have to provide a way to correctly reconstruct a `shared_ptr` which can be done only if type hold it is deriving publicly from `std::enable_shared_from_this` or `boost::enable_shared_from_this`. No additional specialization of traits is needed in this case. - -```cpp -struct A : public std::enable_shared_from_this -{ - A () { } - A (int) { } - - void foo () { } -}; - -luabridge::getGlobalNamespace (L) - .beginClass ("A") - .addConstructorFrom, void(), void(int)> () - .addFunction ("foo", &A::foo) - .endClass (); - -std::shared_ptr a = std::make_shared (1); -luabridge::setGlobal (L, a, "a"); - -std::shared_ptr retrieveA = luabridge::getGlobal> (L, "a"); -retrieveA->foo (); -``` - -```lua -a.foo () - -anotherA = A () -anotherA.foo () - -anotherA2 = A (1) -anotherA2.foo () -``` - -### 3.4.4 - Container Constructors - -When a constructor is registered for a class, there is an additional optional second template parameter describing the type of container to use. If this parameter is specified, calls to the constructor will create the object dynamically, via operator new, and place it a container of that type. The container must have been previously specialized in `ContainerTraits`, or else a compile error will result. This code will register two objects, each using a constructor that creates an object with Lua lifetime using the specified container: - -```cpp -class C : public luabridge::RefCountedObject -{ - C () { } - C (int) { } -}; - -luabridge::getGlobalNamespace (L) - .beginNamespace ("test") - .beginClass ("C") - .addConstructorFrom, void(), void(int)> () - .endClass () - .endNamespace () -``` - -3.5 - Mixing Lifetimes ----------------------- - -Mixing object lifetime models is entirely possible, subject to the usual caveats of holding references to objects which could get deleted. For example, C++ can be called from Lua with a pointer to an object of class type; the function can modify the object or call non-const data members. These modifications are visible to Lua (since they both refer to the same object). An object store in a container can be passed to a function expecting a pointer. These conversion work seamlessly. - -3.6 - Convenience Functions ---------------------------- - -The `setGlobal` function can be used to assign any convertible value into a global variable. - -4 - Accessing Lua from C++ -========================== - -Because Lua is a _dynamically typed language_, special consideration is required to map values in Lua to C++. The following sections describe the classes and functions used for representing Lua types. Only the essential operations are explained; To gain understanding of all available functions, please refer to the documentation comments in the corresponding source files. - -4.1 - Class LuaRef ------------------- - -The `LuaRef` class is a container which references any Lua type. It can hold anything which a Lua variable can hold: **nil**, number, boolean, string, table, function, thread, userdata, and lightuserdata. Because `LuaRef` uses the `Stack` template specializations to do its work, classes, functions, and data exported to Lua through namespace registrations can also be stored (these are instances of userdata). In general, a `LuaRef` can represent any _convertible_ C++ type as well as all Lua types. - -A `LuaRef` variable constructed with no parameters produces a reference to **nil**: - -```cpp -luabridge::LuaRef v (L); // References nil -``` - -To construct a `LuaRef` to a specific value, the two parameter constructor is used: - -```cpp -luabridge::LuaRef v1 (L, 1); // A LUA_TNUMBER -luabridge::LuaRef v2 (L, 1.1); // Also a LUA_TNUMBER -luabridge::LuaRef v3 (L, true); // A LUA_TBOOLEAN -luabridge::LuaRef v4 (L, "string"); // A LUA_TSTRING -``` - -The functions `newTable` and `getGlobal` create references to new empty table and an existing value in the global table respectively: - -```cpp -LuaRef v1 = newTable (L); // Create a new table -LuaRef v2 = getGlobal (L, "print") // Reference to _G ["print"] -``` - -A `LuaRef` can hold classes _registered_ using LuaBridge: - -```cpp -class A; - -//... - -luabridge::LuaRef v (L, new A); // A LuaBridge userdata holding a pointer to A -``` - -Any convertible type may be assigned to an already-existing `LuaRef`: - -```cpp -luabridge::LuaRef v (L); // Nil -v = luabridge::newTable (L); // An empty table -v = "string"; // A string. The prevous value becomes - // eligible for garbage collection. -``` - -A `LuaRef` is itself a convertible type, and the convertible type `LuaNil` can be used to represent a Lua **nil**. - -```cpp -luabridge::LuaRef v1 (L, "x"); // assign "x" -luabridge::LuaRef v2 (L, "y"); // assign "y" -v2 = v1; // v2 becomes "x" -v1 = "z"; // v1 becomes "z", v2 is unchanged -v1 = luabridge::newTable (L); // An empty table -v2 = v1; // v2 references the same table as v1 -v1 = luabridge::LuaNil (); // v1 becomes nil, table is still - // referenced by v2. -``` - -Values stored in a `LuaRef` object obey the same rules as variables in Lua: tables, functions, threads, and full userdata values are _objects_. The `LuaRef` does not actually _contain_ these values, only _references_ to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy. - -### 4.1.1 - Lifetime, States and Lua Threads - -Lifetime of `LuaRef` is bound to the lua state or thread passed in when constructing the reference. It is responsibility of the developer to keep the passed lua state/thread alive for the duration of the usage of the `LuaRef`. In case of storing objects in those references that might be created in lua threads that could be destroyed during the application lifetime, it is advised to pass `luabridge::main_thread (L)` in place of `L` when constructing a `LuaRef`, to make sure the reference is kept in the main lua state instead of the volatile lua thread where it has been created. - -In order to have `luabridge::main_thread` method working in all lua versions, one have to call `luabridge::registerMainThread` function at the beginning of the usage of luabridge (lua 5.1 doesn't store the main thread in the registry, and this needs to be manually setup by the developer). - -### 4.1.2 - Type Conversions - -A universal C++ conversion operator is provided for implicit conversions which allow a `LuaRef` to be used where any convertible type is expected. These operations will all compile: - -```cpp -void passInt (int); -void passBool (bool); -void passString (std::string); -void passObject (A*); - -luabridge::LuaRef v (L); -//... -passInt (v); // implicit conversion to int -passBool (v); // implicit conversion to bool -passString (v); // implicit conversion to string -passObject (v); // must hold a registered LuaBridge class or a - // lua_error() will be called. -``` - -Since Lua types are dynamic, the conversion is performed at run time using traditional functions like `lua_toboolean` or `lua_tostring`. In some cases, the type information may be incorrect especially when passing objects of registered class types. When performing these conversions, LuaBridge may raise a Lua error by directly or indirectly calling `lua_error` To be bullet-proof, such code must either be wrapped in a `lua_pcall`, or you must install a Lua _panic function_ that throws an exception which you can catch. - -When an explicit conversion is required (such as when writing templates), use the `cast` template function or an explicit C++ style cast. - -```cpp -void passString (std::string); - -luabridge::LuaRef v (L); - -// The following are all equivalent, and they could be potentially unsafe: - -passString (std::string (v)); -passString ((std::string) v); -passString (static_cast (v)); -passString (v.unsafe_cast ()); -``` - -The only way to ensure safety when type casting is to use the `LuaRef::cast` method, which is a safe cast of a lua reference to a type `T`. It will return a `luabridge::TypeResult` which will contain the type if the cast was successful, and an error code otherwise. No exception or abort will be triggered from such call (while it's not the same for `LuaRef::cast`). - -```cpp -void passString (std::string); - -luabridge::LuaRef v (L); - -// The following is safe and will never throw exceptions or call the lua panic handler. - -passString (v.cast ().valueOr ("fallback")); -``` - -4.2 - Table Proxies -------------------- - -As tables are the sole data structuring mechanism in Lua, the `LuaRef` class provides robust facilities for accessing and manipulating table elements using a simple, precise syntax. Any convertible type may be used as a key or value. Applying the array indexing operator `[]` to a `LuaRef` returns a special temporary object called a _table proxy_ which supports all the operations which can be performed on a `LuaRef`. In addition, assignments made to table proxies change the underlying table. Because table proxies are compiler-created temporary objects, you don't work with them directly. A LuaBridge table proxy should not be confused with the Lua proxy table technique described in the book "Programming in Lua"; the LuaBridge table proxy is simply an intermediate C++ class object that works behind the scenes to make table manipulation syntax conform to C++ idioms. These operations all invoke table proxies: - -```cpp -luabridge::LuaRef v (L); -v = luabridge::newTable (L); - -v ["name"] = "John Doe"; // string key, string value -v [1] = 200; // integer key, integer value -v [2] = luabridge::newTable (L); // integer key, LuaRef value -v [3] = v [1]; // assign 200 to integer index 3 -v [1] = 100; // v[1] is 100, v[3] is still 200 -v [3] = v [2]; // v[2] and v[3] reference the same table -v [2] = luabridge::LuaNil (); // Removes the value with key = 2. The table - // is still referenced by v[3]. -``` - -4.3 - Calling Lua ------------------ - -Table proxies and `LuaRef` objects provide a convenient syntax for invoking `lua_pcall` on suitable referenced object. This includes C functions, Lua functions, or Lua objects with an appropriate `__call` metamethod set. The provided implementation supports up to eight parameters (although more can be supported by adding new functions). Any convertible C++ type can be passed as a parameter in its native format. The return value of the function call is provided as a `LuaRef`, which may be **nil**. - -```lua -function same (arg1, arg) - return arg1 == arg2 -end -``` - -```cpp -luabridge::LuaRef same = luabridge::getGlobal (L, "same"); - -// These all evaluate to true -same (1,1); -!same (1,2); -same ("text", "text"); -!same (1, "text"); -same (1, 1, 2); // third param ignored -``` - -Table proxies support all of the Lua call notation that `LuaRef` supports, making these statements possible: - -```lua -t[1](); -t[2]("a", "b"); -t[2](t[1]); // Call t[3] with the value in t[2] -t[4]=t[3](); // Call t[3] and store the result in t[4]. - -t [t[5]()] = "wow"; // Store "wow" at the key returned by - // the call to t[5] - -t = {} -t[1] = function () print ("hello") end -t[2] = function (u, v) print (u, v) end -t[3] = "foo" -``` - -```cpp -luabridge::LuaRef v = luabridge::getGlobal (L, "t"); -``` - -### 4.3.1 - Exceptions - -By default `LuaBridge3` is able to work without exceptions, and it's perfectly compatible with the `-fno-exceptions` or `/EHsc-` flags, which is typically used in games. Even if compiling with exceptions enabled, they are not used internally when calling into lua to convert lua errors, but exceptions are only used in registration code to signal potential issues when registering namespaces, classes and methods. You can use the free function `luabridge::enableExceptions` to enable exceptions once before starting to use any luabridge call, and of course that will work only if the application is compiled with exceptions enabled. - -When using the `luabridge::call` or `LuaRef::operator()` no exception should be raised, only if exceptions are disabled in the application or enabled in the application but disabled in luabridge. To control if the lua function invoked has raised a lua error, it is possible to do so by checking the `LuaResult` object that is returned from those functions. - -```lua -function fail () - error ("A problem occurred") -end -``` - -```cpp -luabridge::LuaRef f (L) = luabridge::getGlobal (L, "fail"); - -luabridge::LuaResult result = f (); -if (! result) - std::cerr << result.errorMessage (); -``` - -It is also possible that pushing an unregistered class instance into those function will generate an error, that can be trapped using the same mechanism in a `LuaResult`: - -```lua -function fail (unregistred) - error ("Should never reach here") -end -``` - -```cpp -struct UnregisteredClass {}; - -luabridge::LuaRef f (L) = luabridge::getGlobal (L, "fail"); - -auto argument = UnregisteredClass(); - -luabridge::LuaResult result = f (argument); -if (! result) - std::cerr << result.errorMessage (); -``` - -Calling `luabridge::pcall` will not return a `LuaResult` but only the status code. It will anyway throw an exception if the return code of `lua_pcall`is not equal `LUA_OK`, and return the error code in case exceptions are disabled. - -When compiling `LuaBridge3` with exceptions disabled, all references to try catch blocks and throws will be removed. - -### 4.3.2 - Class LuaException - -When the application is compiled with exceptions and `luabridge::enableExceptions` function has been called, using `luabridge::call` or `LuaRef::operator()` will uses the C++ exception handling mechanism, throwing a `LuaException` object in case an argument has a type that has not been registered (and cannot be pushed onto the lua stack) or the lua function generated an error: - -```lua -function fail () - error ("A problem occurred") -end -``` - -```cpp -luabridge::LuaRef f (L) = luabridge::getGlobal (L, "fail"); - -try -{ - f (); -} -catch (const luabridge::LuaException& e) -{ - std::cerr << e.what (); -} -``` - -5 - Security -============ - -The metatables and userdata that LuaBridge creates in the `lua_State*` are protected using a security system, to eliminate the possibility of undefined behavior resulting from scripted manipulation of the environment. The security system has these components: - -* Class and const class tables use the _table proxy_ technique. The corresponding metatables have `__index` and `__newindex` metamethods, so these class tables are immutable from Lua. -* Metatables have `__metatable` set to a boolean value. Scripts cannot obtain the metatable from a LuaBridge object. -* Classes are mapped to metatables through the registry, which Lua scripts cannot access. The global environment does not expose metatables -* Metatables created by LuaBridge are tagged with a lightuserdata key which is unique in the process. Other libraries cannot forge a LuaBridge metatable. - -This security system can be easily bypassed if scripts are given access to the debug library (or functionality similar to it, i.e. a raw `getmetatable`). The security system can also be defeated by C code in the host, either by revealing the unique lightuserdata key to another module or by putting a LuaBridge metatable in a place that can be accessed by scripts. - -When a class member function is called, or class property member accessed, the `this` pointer is type-checked. This is because member functions exposed to Lua are just plain functions that usually get called with the Lua colon notation, which passes the object in question as the first parameter. Lua's dynamic typing makes this type-checking mandatory to prevent undefined behavior resulting from improper use. - -If a type check error occurs, LuaBridge uses the `lua_error` mechanism to trigger a failure. A host program can always recover from an error through the use of `lua_pcall`; proper usage of LuaBridge will never result in undefined behavior. - -Appendix - API Reference -======================== - -Free Functions --------------- - -```cpp -/// Enable exceptions globally. Will translate lua_errors into C++ LuaExceptions. Usable only if compiled with C++ exceptions enabled. -void enableExceptions(lua_State* L); - -/// Gets a global Lua variable reference as LuaRef. -LuaRef getGlobal (lua_State* L, const char* name); - -/// Gets a global Lua variable reference as type T. -template -TypeResult getGlobal (lua_State* L, const char* name); - -/// Sets a global Lua variable. Throws or return false if the class is not registered. -template -bool setGlobal (lua_State* L, T* varPtr, const char* name); - -/// Gets the global namespace registration object. -Namespace getGlobalNamespace (lua_State* L); - -/// Gets a namespace registration object using a table on top of the stack. -Namespace getNamespaceFromStack (lua_State* L); - -/// Invokes a LuaRef if it references a lua callable. -template -LuaResult call(const LuaRef& object, Args&&... args) - -/// Wrapper for lua_pcall, converting lua errors into C++ exceptions if they are enabled. -int pcall(lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0) - -/// Return a range iterable view over a lua table. -Range pairs(const LuaRef& table); -``` - -Namespace Registration - Namespace ----------------------------------- - -```cpp -/// Begin or continues namespace registration, returns this namespace object. -template -Namespace beginNamespace (const char* name); - -/// Ends namespace registration, returns the parent namespace object. -template -Namespace endNamespace (); - -/// Registers one or multiple overloaded functions. -template -Namespace addFunction (const char* name, Functions... functions); - -/// Registers a property with a getter and setter. -template -Namespace addProperty (const char* name, V (*getFn)(), void (*setFn)(V)); - -/// Registers a property with a getter and setter. -template -Namespace addProperty (const char* name, std::function getFn, std::function setFn); - -/// Registers a property with a C-function getter and setter. -Namespace addProperty (const char* name, int (*getFn)(lua_State*), int (*setFn)(lua_State*)); - -/// Registers a read-only property with a getter function. -template -Namespace addProperty (const char* name, V (*getFn)()); - -/// Registers a read-only property with a getter function. -template -Namespace addProperty (const char* name, std::function getFn); - -/// Registers a read-only property with a C-function getter. -Namespace addProperty (const char* name, int (*getFn)(lua_State*)); - -/// Registers a variable, writable or read-only. -template -Namespace addProperty (const char* name, V* varPtr, bool isWritable = true); -``` - -Class Registration - Class ------------------------------ - -```cpp -/// Begins or continues class registration, returns this class object. -template -Class beginClass (const char* name); - -/// Begins derived class registration, returns this class object. -template -Class deriveClass (const char* name); - -/// Ends class registration, returns the parent namespace object. -template -Namespace endClass (); -``` - -### Constructor Registration - -```cpp -/// Registers one or multiple overloaded constructors for type T. -template -Class addConstructor (); - -/// Registers one or multiple overloaded constructors for type T using callable arguments. -template -Class addConstructor (Functions... functions); - -/// Registers one or multiple overloaded constructors for type T when usable from intrusive container C. -template -Class addConstructor (); - -/// Registers allocator and deallocators for type T. -template -Class addFactory (Alloc alloc, Dealloc dealloc); -``` - -### Member Function Registration - -```cpp -/// Registers one or multiple overloaded functions as member functions. -template -Class addFunction (const char* name, Functions... functions); -``` - -### Member Property Registration - -```cpp -/// Registers a property with a getter and setter. -template -Class addProperty (const char* name, V (T::* getFn)(), void (T::* setFn)(V)); - -/// Registers a property with a getter and setter. -template -Class addProperty (const char* name, std::function getFn, std::function setFn); - -/// Registers a property with a C-function getter and setter. -Class addProperty (const char* name, int (*getFn)(lua_State*), int (*setFn)(lua_State*)); - -/// Registers a read-only property with a getter member function. -template -Class addProperty (const char* name, V (T::* getFn)()); - -/// Registers a read-only property with a getter function. -template -Class addProperty(const char* name, std::function getFn); - -/// Registers a read-only property with a C-function getter. -Class addProperty (const char* name, int (*getFn)(lua_State*)); - -/// Registers a member variable, writable or read-only. -template -Class addProperty (const char* name, V T::* varPtr, bool isWritable = true); -``` - -### Static Function Registration - -```cpp -/// Registers one function or multiple overloads. -template -Class addStaticFunction (const char* name, Functions... functions); -``` - -### Static Property Registration - -```cpp -/// Registers a property with a getter and setter. -template -Class addStaticProperty (const char* name, V (*getFn)(), void (*setFn)(V)); - -/// Registers a property with a getter and setter. -template -Class addStaticProperty (const char* name, std::function getFn, std::function setFn); - -/// Registers a property with a C-function getter and setter. -Class addStaticProperty (const char* name, int (*getFn)(lua_State*), int (*setFn)(lua_State*)); - -/// Registers a read-only property with a getter function. -template -Class addStaticProperty (const char* name, V (*getFn)()); - -/// Registers a read-only property with a getter function. -template -Class addStaticProperty (const char* name, std::function getFn); - -/// Registers a read-only property with a C-function getter. -Class addStaticProperty (const char* name, int (*getFn)(lua_State*)); - -/// Registers a variable, writable or read-only. -Class addStaticProperty (const char* name, T* varPtr, bool isWritable = true); -``` - -Lua Variable Reference - LuaRef -------------------------------- - -```cpp -/// Creates a nil reference. -LuaRef (lua_State* L); - -/// Returns native Lua string representation. -std::string tostring () const; - -/// Dumps reference to a stream. -void print (std::ostream& stream) const; - -/// Returns the Lua state. -lua_State* state () const; - -/// Place the object onto the Lua stack. -void push (lua_State* L); - -/// Indicate whether it is a valid reference (not a LUA_NOREF). -bool isValid () const; - -/// Return the lua_type. -int type () const; - -/// Indicate whether it is a nil reference. -bool isNil () const; - -/// Indicate whether it is a reference to a boolean. -bool isBool () const; - -/// Indicate whether it is a reference to a number. -bool isNumber () const; - -/// Indicate whether it is a reference to a string. -bool isString () const; - -/// Indicate whether it is a reference to a table. -bool isTable () const; - -/// Indicate whether it is a reference to a function. -bool isFunction () const; - -/// Indicate whether it is a reference to a full userdata. -bool isUserdata () const; - -/// Indicate whether it is a reference to a light userdata. -bool isLightUserdata () const; - -/// Indicate whether it is a reference to a Lua thread. -bool isThread () const; - -/// Indicate whether it is a callable, can be either a lua function or an object with the __call metamethod. -bool isCallable () const; - -/// Perform implicit type conversion. -template -operator T () const; - -/// Perform the explicit type conversion, safe. -template -TypeResult cast () const; - -/// Perform the explicit type conversion, unsafe (throws or abort on failure). -template -T unsafe_cast () const; - -/// Check if the Lua value is convertible to the type T. -template -bool isInstance () const; - -/// Get the metatable for the LuaRef. -LuaRef getMetatable () const; - -/// Compare this reference with a specified value using lua_compare(). This invokes metamethods. -template -bool operator== (T rhs) const; - -/// Compare this reference with a specified value using lua_compare(). This invokes metamethods. -template -bool operator!= (T rhs) const; - -/// Compare this reference with a specified value using lua_compare(). This invokes metamethods. -template -bool operator< (T rhs) const; - -/// Compare this reference with a specified value using lua_compare(). This invokes metamethods. -template -bool operator<= (T rhs) const; - -/// Compare this reference with a specified value using lua_compare(). This invokes metamethods. -template -bool operator> (T rhs) const; - -/// Compare this reference with a specified value using lua_compare(). This invokes metamethods. -template -bool operator>= (T rhs) const; - -/// Compare this reference with a specified value using lua_compare(). This does not invoke metamethods. -template -bool rawequal (T v) const; - -/// Append a value to a referred table. If the table is a sequence this will add another element to it. -template -void append (T v) const; - -/// Return the length of a referred array. This is identical to applying the Lua # operator. -int length () const; - -/// Invoke the lua ref if it references a lua function. -template -LuaResult call (Args&&... args) const; -``` - -Lua Nil Special Value - LuaNil ------------------------------- - -```cpp -/// LuaNil can be used to construct LuaRef. -``` - -Lua Result Of Function Invocation - LuaResult ---------------------------------------------- - -```cpp -explicit operator bool() const; - -/// Return if the invocation was ok and didn't raise a lua error. -bool wasOk() const; - -/// Return if the invocation did raise a lua error. -bool hasFailed() const; - -/// Return the error code, if any. -std::error_code errorCode() const; - -/// Return the error message, if any. -std::string errorMessage() const; - -/// Return the number of return values. -std::size_t size() const; - -/// Get a return value at a specific index. -LuaRef operator[](std::size_t index) const; - -``` - -Stack Traits - Stack ------------------------ - -```cpp -/// Converts the C++ value into the Lua value at the top of the Lua stack. Returns true if the push could be performed. -/// When false is returned, `ec` contains the error code corresponding to the failure. -Result push (lua_State* L, const T& value); - -/// Converts the Lua value at the index into the C++ value of the type T. -TypeResult get (lua_State* L, int index); - -/// Checks if the Lua value at the index is convertible into the C++ value of the type T. -bool isInstance (lua_State* L, int index); -``` diff --git a/LuaBridge3/README.md b/LuaBridge3/README.md deleted file mode 100644 index a6086e0..0000000 --- a/LuaBridge3/README.md +++ /dev/null @@ -1,166 +0,0 @@ - - - - - - -
- -# LuaBridge 3.0 - -[LuaBridge3][1] is a lightweight and dependency-free library for mapping data, -functions, and classes back and forth between C++ and [Lua][2] (a powerful, -fast, lightweight, embeddable scripting language). LuaBridge has been tested -and works with Lua 5.1.5, 5.2.4, 5.3.6 and 5.4.4 as well as [LuaJit][3] 2.x onwards -and for the first time also with [Luau][4] 0.556 onwards. - -## Features - -LuaBridge3 is usable from a compliant C++17 compiler and offers the following features: - -* [MIT Licensed][5], no usage restrictions! -* Headers-only: No Makefile, no .cpp files, just one `#include` and one header file (optional) ! -* Works with ANY lua version out there (PUC-Lua, LuaJIT, Luau, you name it). -* Simple, light, and nothing else needed. -* Fast to compile (even in release mode), scaling linearly with the size of your binded code. -* No macros, settings, or configuration scripts needed. -* Supports different object lifetime management models. -* Convenient, type-safe access to the Lua stack. -* Automatic function parameter type binding. -* Functions and constructors overloading support. -* Easy access to Lua objects like tables and functions. -* Expose C++ classes allowing them to use the flexibility of lua property lookup. -* Interoperable with most common c++ standard library container types. -* Written in a clear and easy to debug style. - -## Improvements Over Vanilla LuaBridge - -LuaBridge3 offers a set of improvements compared to vanilla LuaBridge: - -* The only binder library that works with PUC-Lua as well as LuaJIT and Luau, wonderful for game development ! -* Can work with both c++ exceptions and without (Works with `-fno-exceptions` and `/EHsc-`). -* Can safely register and use classes exposed across shared library boundaries. -* Full support for capturing lambdas in all namespace and class methods. -* Overloaded function support in Namespace functions, Class constructors, functions and static functions. -* Supports placement allocation or custom allocations/deallocations of C++ classes exposed to lua. -* Lightweight object creation: allow adding lua tables on the stack and register methods and metamethods in them. -* Allows for fallback `__index` and `__newindex` metamethods in exposed C++ classes, to support flexible and dynamic C++ classes ! -* Added `std::shared_ptr` to support shared C++/Lua lifetime for types deriving from `std::enable_shared_from_this`. -* Supports conversion to and from `std::nullptr_t`, `std::byte`, `std::pair`, `std::tuple` and `std::reference_wrapper`. -* Supports conversion to and from C style arrays of any supported type. -* Transparent support of all signed and unsigned integer types up to `int64_t`. -* Consistent numeric handling and conversions (signed, unsigned and floats) across all lua versions. -* Automatic handling of enum types by communicating with lua through `std::underlying_type_t`. -* Opt-out handling of safe stack space checks (automatically avoids exhausting lua stack space when pushing values!). - -## Status - -![Build MacOS](https://github.com/kunitoki/LuaBridge3/workflows/Build%20MacOS/badge.svg?branch=master) -![Build Windows](https://github.com/kunitoki/LuaBridge3/workflows/Build%20Windows/badge.svg?branch=master) -![Build Linux](https://github.com/kunitoki/LuaBridge3/workflows/Build%20Linux/badge.svg?branch=master) - -## Code Coverage -[![Coverage Status](https://coveralls.io/repos/github/kunitoki/LuaBridge3/badge.svg?branch=master&kill_cache=1)](https://coveralls.io/github/kunitoki/LuaBridge3?branch=master) - -## Documentation - -Please read the [LuaBridge3 Reference Manual][6] for more details on the API. - -## Release Notes - -Plase read the [LuaBridge3 Release Notes][7] for more details - -## Installing LuaBridge3 (vcpkg) - -You can download and install LuaBridge3 using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager: -```Powershell or bash -git clone https://github.com/Microsoft/vcpkg.git -cd vcpkg -./bootstrap-vcpkg.sh # The name of the script should be "./bootstrap-vcpkg.bat" for Powershell -./vcpkg integrate install -./vcpkg install luabridge3 -``` - -The LuaBridge3 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository. - -## Unit Tests - -Unit test build requires a CMake and C++17 compliant compiler. - -There are 11 unit test flavors: -* `LuaBridgeTests51` - uses Lua 5.1 -* `LuaBridgeTests51Noexcept` - uses Lua 5.1 without exceptions enabled -* `LuaBridgeTests52` - uses Lua 5.2 -* `LuaBridgeTests52Noexcept` - uses Lua 5.2 without exceptions enabled -* `LuaBridgeTests53` - uses Lua 5.3 -* `LuaBridgeTests53Noexcept` - uses Lua 5.3 without exceptions enabled -* `LuaBridgeTests54` - uses Lua 5.4 -* `LuaBridgeTests54Noexcept` - uses Lua 5.4 without exceptions enabled -* `LuaBridgeTestsLuaJIT` - uses LuaJIT 2.1 -* `LuaBridgeTestsLuaJITNoexcept` - uses LuaJIT 2.1 without exceptions enabled -* `LuaBridgeTestsLuau` - uses Luau - -(Luau compiler needs exceptions, so there are no test targets on Luau without exceptions) - -Generate Unix Makefiles and build on Linux: -```bash -git clone --recursive git@github.com:kunitoki/LuaBridge3.git - -mkdir -p LuaBridge3/build -pushd LuaBridge3/build -cmake -G "Unix Makefiles" ../ -cmake --build . -DCMAKE_BUILD_TYPE=Debug -# or cmake --build . -DCMAKE_BUILD_TYPE=Release -# or cmake --build . -DCMAKE_BUILD_TYPE=RelWithDebInfo -popd -``` - -Generate XCode project and build on MacOS: -```bash -git clone --recursive git@github.com:kunitoki/LuaBridge3.git - -mkdir -p LuaBridge3/build -pushd LuaBridge3/build -cmake -G Xcode ../ # Generates XCode project build/LuaBridge.xcodeproj -cmake --build . -DCMAKE_BUILD_TYPE=Debug -# or cmake --build . -DCMAKE_BUILD_TYPE=Release -# or cmake --build . -DCMAKE_BUILD_TYPE=RelWithDebInfo -popd -``` - -Generate VS2019 solution on Windows: -```cmd -git clone --recursive git@github.com:kunitoki/LuaBridge3.git - -mkdir LuaBridge3/build -pushd LuaBridge3/build -cmake -G "Visual Studio 16" ../ # Generates MSVS solution build/LuaBridge.sln -popd -``` - -## Official Repository - -LuaBridge3 is published under the terms of the [MIT License][5]. - -The original version of LuaBridge3 was written by Nathan Reed. The project has -been taken over by Vinnie Falco, who added new functionality, wrote the new -documentation, and incorporated contributions from Nigel Atkinson. Then it has -been forked from the original https://github.com/vinniefalco/LuaBridge into its -own LuaBridge3 repository by Lucio Asnaghi, and development continued there. - -For questions, comments, or bug reports feel free to open a Github issue -or contact Lucio Asnaghi directly at the email address indicated below. - -Copyright 2020, Lucio Asnaghi ()
-Copyright 2019, Dmitry Tarakanov
-Copyright 2012, Vinnie Falco ()
-Copyright 2008, Nigel Atkinson
-Copyright 2007, Nathan Reed
- -[1]: https://github.com/kunitoki/LuaBridge3 "LuaBridge3" -[2]: https://lua.org "The Lua Programming Language" -[3]: https://luajit.org/ "The LuaJIT Project" -[4]: https://luau-lang.org/ "The Luau Project" -[5]: https://www.opensource.org/licenses/mit-license.html "The MIT License" -[6]: https://kunitoki.github.io/LuaBridge3/Manual "LuaBridge3 Reference Manual" -[7]: https://kunitoki.github.io/LuaBridge3/CHANGES "LuaBridge3 Release Notes" diff --git a/LuaBridge3/Source/CMakeLists.txt b/LuaBridge3/Source/CMakeLists.txt deleted file mode 100644 index cfd92ad..0000000 --- a/LuaBridge3/Source/CMakeLists.txt +++ /dev/null @@ -1,51 +0,0 @@ -cmake_minimum_required (VERSION 3.5) - -set (LUABRIDGE_HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/Array.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/List.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/LuaBridge.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/Map.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/RefCountedObject.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/Set.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/UnorderedMap.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/Vector.h) -source_group ("LuaBridge" FILES ${LUABRIDGE_HEADERS}) - -set (LUABRIDGE_DETAIL_HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/CFunctions.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/ClassInfo.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Config.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Dump.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Errors.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Expected.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/FuncTraits.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Invoke.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Iterator.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/LuaException.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/LuaHelpers.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/LuaRef.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Namespace.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Overload.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Result.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/ScopeGuard.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Security.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Stack.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/TypeTraits.h - ${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Userdata.h) -source_group ("LuaBridgeDetail" FILES ${LUABRIDGE_DETAIL_HEADERS}) - -add_library (LuaBridge INTERFACE) -target_sources (LuaBridge INTERFACE - ${LUABRIDGE_HEADERS} - ${LUABRIDGE_DETAIL_HEADERS}) -target_include_directories (LuaBridge INTERFACE .) - -if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) - install(DIRECTORY LuaBridge DESTINATION "LuaBridge3") -endif () - -if (MSVC AND CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) - add_custom_target (LuaBridgeSources SOURCES - ${LUABRIDGE_HEADERS} - ${LUABRIDGE_DETAIL_HEADERS}) -endif () diff --git a/LuaBridge3/Source/LuaBridge/Array.h b/LuaBridge3/Source/LuaBridge/Array.h deleted file mode 100644 index c5fb268..0000000 --- a/LuaBridge3/Source/LuaBridge/Array.h +++ /dev/null @@ -1,84 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#pragma once - -#include "detail/Stack.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack specialization for `std::array`. - */ -template -struct Stack> -{ - using Type = std::array; - - [[nodiscard]] static Result push(lua_State* L, const Type& array) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(Size), 0); - - for (std::size_t i = 0; i < Size; ++i) - { - lua_pushinteger(L, static_cast(i + 1)); - - auto result = Stack::push(L, array[i]); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (get_length(L, index) != Size) - return makeErrorCode(ErrorCode::InvalidTableSizeInCast); - - const StackRestore stackRestore(L); - - Type array; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - int arrayIndex = 0; - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (!item) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - array[arrayIndex++] = *item; - lua_pop(L, 1); - } - - return array; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index) && get_length(L, index) == Size; - } -}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/List.h b/LuaBridge3/Source/LuaBridge/List.h deleted file mode 100644 index 433ebd4..0000000 --- a/LuaBridge3/Source/LuaBridge/List.h +++ /dev/null @@ -1,81 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#pragma once - -#include "detail/Stack.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack specialization for `std::array`. - */ -template -struct Stack> -{ - using Type = std::list; - - [[nodiscard]] static Result push(lua_State* L, const Type& list) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(list.size()), 0); - - auto it = list.cbegin(); - for (lua_Integer tableIndex = 1; it != list.cend(); ++tableIndex, ++it) - { - lua_pushinteger(L, tableIndex); - - auto result = Stack::push(L, *it); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type list; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (! item) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - list.emplace_back(*item); - lua_pop(L, 1); - } - - return list; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/LuaBridge.h b/LuaBridge3/Source/LuaBridge/LuaBridge.h deleted file mode 100644 index d2683ec..0000000 --- a/LuaBridge3/Source/LuaBridge/LuaBridge.h +++ /dev/null @@ -1,36 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#pragma once - -// All #include dependencies are listed here -// instead of in the individual header files. - -#define LUABRIDGE_MAJOR_VERSION 3 -#define LUABRIDGE_MINOR_VERSION 1 -#define LUABRIDGE_VERSION 301 - -#include "detail/Config.h" - -#include "detail/CFunctions.h" -#include "detail/ClassInfo.h" -#include "detail/Errors.h" -#include "detail/Expected.h" -#include "detail/FuncTraits.h" -#include "detail/Invoke.h" -#include "detail/Iterator.h" -#include "detail/LuaException.h" -#include "detail/LuaHelpers.h" -#include "detail/LuaRef.h" -#include "detail/Namespace.h" -#include "detail/Overload.h" -#include "detail/Result.h" -#include "detail/ScopeGuard.h" -#include "detail/Security.h" -#include "detail/Stack.h" -#include "detail/TypeTraits.h" -#include "detail/Userdata.h" diff --git a/LuaBridge3/Source/LuaBridge/Map.h b/LuaBridge3/Source/LuaBridge/Map.h deleted file mode 100644 index 57a3fff..0000000 --- a/LuaBridge3/Source/LuaBridge/Map.h +++ /dev/null @@ -1,86 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2018, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#pragma once - -#include "detail/Stack.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack specialization for `std::map`. - */ -template -struct Stack> -{ - using Type = std::map; - - [[nodiscard]] static Result push(lua_State* L, const Type& map) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 0, static_cast(map.size())); - - for (auto it = map.begin(); it != map.end(); ++it) - { - auto result = Stack::push(L, it->first); - if (! result) - return result; - - result = Stack::push(L, it->second); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type map; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto value = Stack::get(L, -1); - if (! value) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto key = Stack::get(L, -2); - if (! key) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - map.emplace(*key, *value); - lua_pop(L, 1); - } - - return map; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/RefCountedObject.h b/LuaBridge3/Source/LuaBridge/RefCountedObject.h deleted file mode 100644 index d9a6258..0000000 --- a/LuaBridge3/Source/LuaBridge/RefCountedObject.h +++ /dev/null @@ -1,372 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2012, Vinnie Falco -// Copyright 2004-11 by Raw Material Software Ltd. -// SPDX-License-Identifier: MIT - -//============================================================================== -/* - This is a derivative work used by permission from part of - JUCE, available at http://www.rawaterialsoftware.com - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - - This file incorporates work covered by the following copyright and - permission notice: - - This file is part of the JUCE library - "Jules' Utility Class Extensions" - Copyright 2004-11 by Raw Material Software Ltd. -*/ -//============================================================================== - -#pragma once - -#include "detail/Config.h" -#include "detail/TypeTraits.h" - -#include -#include - -namespace luabridge { - -//============================================================================== -/** - Adds reference-counting to an object. - - To add reference-counting to a class, derive it from this class, and - use the RefCountedObjectPtr class to point to it. - - e.g. @code - class MyClass : public RefCountedObjectType - { - void foo(); - - // This is a neat way of declaring a typedef for a pointer class, - // rather than typing out the full templated name each time.. - typedef RefCountedObjectPtr Ptr; - }; - - MyClass::Ptr p = new MyClass(); - MyClass::Ptr p2 = p; - p = 0; - p2->foo(); - @endcode - - Once a new RefCountedObjectType has been assigned to a pointer, be - careful not to delete the object manually. -*/ -template -class RefCountedObjectType -{ -public: - //============================================================================== - /** Increments the object's reference count. - - This is done automatically by the smart pointer, but is public just - in case it's needed for nefarious purposes. - */ - inline void incReferenceCount() const { ++refCount; } - - /** Decreases the object's reference count. - - If the count gets to zero, the object will be deleted. - */ - inline void decReferenceCount() const - { - assert(getReferenceCount() > 0); - - if (--refCount == 0) - delete this; - } - - /** Returns the object's current reference count. - * @returns The reference count. - */ - inline int getReferenceCount() const { return static_cast(refCount); } - -protected: - //============================================================================== - /** Creates the reference-counted object (with an initial ref count of zero). */ - RefCountedObjectType() : refCount() {} - - /** Destructor. */ - virtual ~RefCountedObjectType() - { - // it's dangerous to delete an object that's still referenced by something else! - assert(getReferenceCount() == 0); - } - -private: - //============================================================================== - CounterType mutable refCount; -}; - -//============================================================================== - -/** Non thread-safe reference counted object. - - This creates a RefCountedObjectType that uses a non-atomic integer - as the counter. -*/ -typedef RefCountedObjectType RefCountedObject; - -//============================================================================== -/** - A smart-pointer class which points to a reference-counted object. - - The template parameter specifies the class of the object you want to point - to - the easiest way to make a class reference-countable is to simply make - it inherit from RefCountedObjectType, but if you need to, you could roll - your own reference-countable class by implementing a pair of methods called - incReferenceCount() and decReferenceCount(). - - When using this class, you'll probably want to create a typedef to - abbreviate the full templated name - e.g. - - @code - - typedef RefCountedObjectPtr MyClassPtr; - - @endcode -*/ -template -class RefCountedObjectPtr -{ -public: - /** The class being referenced by this pointer. */ - typedef ReferenceCountedObjectClass ReferencedType; - - //============================================================================== - /** Creates a pointer to a null object. */ - inline RefCountedObjectPtr() : referencedObject(0) {} - - /** Creates a pointer to an object. - This will increment the object's reference-count if it is non-null. - - @param refCountedObject A reference counted object to own. - */ - inline RefCountedObjectPtr(ReferenceCountedObjectClass* const refCountedObject) - : referencedObject(refCountedObject) - { - if (refCountedObject != 0) - refCountedObject->incReferenceCount(); - } - - /** Copies another pointer. - This will increment the object's reference-count (if it is non-null). - - @param other Another pointer. - */ - inline RefCountedObjectPtr(const RefCountedObjectPtr& other) - : referencedObject(other.referencedObject) - { - if (referencedObject != 0) - referencedObject->incReferenceCount(); - } - - /** - Takes-over the object from another pointer. - - @param other Another pointer. - */ - inline RefCountedObjectPtr(RefCountedObjectPtr&& other) - : referencedObject(other.referencedObject) - { - other.referencedObject = 0; - } - - /** Copies another pointer. - This will increment the object's reference-count (if it is non-null). - - @param other Another pointer. - */ - template - inline RefCountedObjectPtr(const RefCountedObjectPtr& other) - : referencedObject(static_cast(other.getObject())) - { - if (referencedObject != 0) - referencedObject->incReferenceCount(); - } - - /** Changes this pointer to point at a different object. - - The reference count of the old object is decremented, and it might be - deleted if it hits zero. The new object's count is incremented. - - @param other A pointer to assign from. - @returns This pointer. - */ - RefCountedObjectPtr& operator=(const RefCountedObjectPtr& other) - { - return operator=(other.referencedObject); - } - - /** Changes this pointer to point at a different object. - The reference count of the old object is decremented, and it might be - deleted if it hits zero. The new object's count is incremented. - - @param other A pointer to assign from. - @returns This pointer. - */ - template - RefCountedObjectPtr& operator=(const RefCountedObjectPtr& other) - { - return operator=(static_cast(other.getObject())); - } - - /** - Takes-over the object from another pointer. - - @param other A pointer to assign from. - @returns This pointer. - */ - RefCountedObjectPtr& operator=(RefCountedObjectPtr&& other) - { - using std::swap; - - swap(referencedObject, other.referencedObject); - - return *this; - } - - /** Changes this pointer to point at a different object. - The reference count of the old object is decremented, and it might be - deleted if it hits zero. The new object's count is incremented. - - @param newObject A reference counted object to own. - @returns This pointer. - */ - RefCountedObjectPtr& operator=(ReferenceCountedObjectClass* const newObject) - { - if (referencedObject != newObject) - { - if (newObject != 0) - newObject->incReferenceCount(); - - ReferenceCountedObjectClass* const oldObject = referencedObject; - referencedObject = newObject; - - if (oldObject != 0) - oldObject->decReferenceCount(); - } - - return *this; - } - - /** Destructor. - This will decrement the object's reference-count, and may delete it if it - gets to zero. - */ - ~RefCountedObjectPtr() - { - if (referencedObject != 0) - referencedObject->decReferenceCount(); - } - - /** Returns the object that this pointer references. - The returned pointer may be null. - - @returns The pointee. - */ - operator ReferenceCountedObjectClass*() const { return referencedObject; } - - /** Returns the object that this pointer references. - The returned pointer may be null. - - @returns The pointee. - */ - ReferenceCountedObjectClass* operator->() const { return referencedObject; } - - /** Returns the object that this pointer references. - The returned pointer may be null. - - @returns The pointee. - */ - ReferenceCountedObjectClass* getObject() const { return referencedObject; } - -private: - //============================================================================== - ReferenceCountedObjectClass* referencedObject; -}; - -/** Compares two ReferenceCountedObjectPointers. */ -template -bool operator==(const RefCountedObjectPtr& object1, - ReferenceCountedObjectClass* const object2) -{ - return object1.getObject() == object2; -} - -/** Compares two ReferenceCountedObjectPointers. */ -template -bool operator==(const RefCountedObjectPtr& object1, - const RefCountedObjectPtr& object2) -{ - return object1.getObject() == object2.getObject(); -} - -/** Compares two ReferenceCountedObjectPointers. */ -template -bool operator==(ReferenceCountedObjectClass* object1, - RefCountedObjectPtr& object2) -{ - return object1 == object2.getObject(); -} - -/** Compares two ReferenceCountedObjectPointers. */ -template -bool operator!=(const RefCountedObjectPtr& object1, - const ReferenceCountedObjectClass* object2) -{ - return object1.getObject() != object2; -} - -/** Compares two ReferenceCountedObjectPointers. */ -template -bool operator!=(const RefCountedObjectPtr& object1, - RefCountedObjectPtr& object2) -{ - return object1.getObject() != object2.getObject(); -} - -/** Compares two ReferenceCountedObjectPointers. */ -template -bool operator!=(ReferenceCountedObjectClass* object1, - RefCountedObjectPtr& object2) -{ - return object1 != object2.getObject(); -} - -//============================================================================== - -template -struct ContainerTraits> -{ - using Type = T; - - static RefCountedObjectPtr construct(T* c) { return c; } - - static T* get(RefCountedObjectPtr const& c) { return c.getObject(); } -}; - -//============================================================================== - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/Set.h b/LuaBridge3/Source/LuaBridge/Set.h deleted file mode 100644 index f943b2b..0000000 --- a/LuaBridge3/Source/LuaBridge/Set.h +++ /dev/null @@ -1,80 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#pragma once - -#include "detail/Stack.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack specialization for `std::set`. - */ -template -struct Stack> -{ - using Type = std::set; - - [[nodiscard]] static Result push(lua_State* L, const Type& set) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 0, static_cast(set.size())); - - auto it = set.cbegin(); - for (lua_Integer tableIndex = 1; it != set.cend(); ++tableIndex, ++it) - { - lua_pushinteger(L, tableIndex); - - auto result = Stack::push(L, *it); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeUnexpected(makeErrorCode(ErrorCode::InvalidTypeCast)); - - const StackRestore stackRestore(L); - - Type set; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (! item) - return makeUnexpected(makeErrorCode(ErrorCode::InvalidTypeCast)); - - set.emplace(*item); - lua_pop(L, 1); - } - - return set; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/UnorderedMap.h b/LuaBridge3/Source/LuaBridge/UnorderedMap.h deleted file mode 100644 index 4128431..0000000 --- a/LuaBridge3/Source/LuaBridge/UnorderedMap.h +++ /dev/null @@ -1,86 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#pragma once - -#include "detail/Stack.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack specialization for `std::unordered_map`. - */ -template -struct Stack> -{ - using Type = std::unordered_map; - - [[nodiscard]] static Result push(lua_State* L, const Type& map) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 0, static_cast(map.size())); - - for (auto it = map.begin(); it != map.end(); ++it) - { - auto result = Stack::push(L, it->first); - if (! result) - return result; - - result = Stack::push(L, it->second); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type map; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto value = Stack::get(L, -1); - if (! value) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto key = Stack::get(L, -2); - if (! key) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - map.emplace(*key, *value); - lua_pop(L, 1); - } - - return map; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/Vector.h b/LuaBridge3/Source/LuaBridge/Vector.h deleted file mode 100644 index 65281a8..0000000 --- a/LuaBridge3/Source/LuaBridge/Vector.h +++ /dev/null @@ -1,81 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2018, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#pragma once - -#include "detail/Stack.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack specialization for `std::vector`. - */ -template -struct Stack> -{ - using Type = std::vector; - - [[nodiscard]] static Result push(lua_State* L, const Type& vector) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(vector.size()), 0); - - for (std::size_t i = 0; i < vector.size(); ++i) - { - lua_pushinteger(L, static_cast(i + 1)); - - auto result = Stack::push(L, vector[i]); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - const StackRestore stackRestore(L); - - Type vector; - vector.reserve(static_cast(get_length(L, index))); - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - while (lua_next(L, absIndex) != 0) - { - auto item = Stack::get(L, -1); - if (! item) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - vector.emplace_back(*item); - lua_pop(L, 1); - } - - return vector; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_istable(L, index); - } -}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/CFunctions.h b/LuaBridge3/Source/LuaBridge/detail/CFunctions.h deleted file mode 100644 index e5d6cfc..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/CFunctions.h +++ /dev/null @@ -1,1178 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Errors.h" -#include "FuncTraits.h" -#include "LuaHelpers.h" -#include "Stack.h" -#include "TypeTraits.h" -#include "Userdata.h" - -#include - -namespace luabridge { -namespace detail { - -//================================================================================================= -/** - * @brief Make argument lists extracting them from the lua state, starting at a stack index. - * - * @tparam ArgsPack Arguments pack to extract from the lua stack. - * @tparam Start Start index where stack variables are located in the lua stack. - */ -template -auto unwrap_argument_or_error(lua_State* L, std::size_t index) -{ - auto result = Stack::get(L, static_cast(index)); - if (! result) - luaL_error(L, "Error decoding argument #%d: %s", static_cast(index), result.message().c_str()); - - return std::move(*result); -} - -template -auto make_arguments_list_impl(lua_State* L, std::index_sequence) -{ - return tupleize(unwrap_argument_or_error>(L, Start + Indices)...); -} - -template -auto make_arguments_list(lua_State* L) -{ - return make_arguments_list_impl(L, std::make_index_sequence>()); -} - -//================================================================================================= -/** - * @brief Helpers for iterating through tuple arguments, pushing each argument to the lua stack. - */ -template -auto push_arguments(lua_State*, std::tuple) - -> std::enable_if_t> -{ - return std::make_tuple(Result(), Index + 1); -} - -template -auto push_arguments(lua_State* L, std::tuple t) - -> std::enable_if_t> -{ - using T = std::tuple_element_t>; - - auto result = Stack::push(L, std::get(t)); - if (! result) - return std::make_tuple(result, Index + 1); - - return push_arguments(L, std::move(t)); -} - -//================================================================================================= -/** - * @brief Helpers for iterating through tuple arguments, popping each argument from the lua stack. - */ -template -auto pop_arguments(lua_State*, std::tuple&) - -> std::enable_if_t -{ - return sizeof...(Types); -} - -template -auto pop_arguments(lua_State* L, std::tuple& t) - -> std::enable_if_t -{ - using T = std::tuple_element_t>; - - std::get(t) = Stack::get(L, Start - Index); - - return pop_arguments(L, t); -} - - -//================================================================================================= -/** - * @brief __index metamethod for a namespace or class static and non-static members. - * - * Retrieves functions from metatables and properties from propget tables. Looks through the class hierarchy if inheritance is present. - */ -inline int index_metamethod(lua_State* L) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 3, detail::error_lua_stack_overflow); -#endif - - assert(lua_istable(L, 1) || lua_isuserdata(L, 1)); // Stack (further not shown): table | userdata, name - - lua_getmetatable(L, 1); // Stack: class/const table (mt) - assert(lua_istable(L, -1)); - - for (;;) - { - lua_pushvalue(L, 2); // Stack: mt, field name - lua_rawget(L, -2); // Stack: mt, field | nil - - if (lua_iscfunction(L, -1)) // Stack: mt, field - { - lua_remove(L, -2); // Stack: field - return 1; - } - - assert(lua_isnil(L, -1)); // Stack: mt, nil - lua_pop(L, 1); // Stack: mt - - lua_rawgetp(L, -1, getPropgetKey()); // Stack: mt, propget table (pg) - assert(lua_istable(L, -1)); - - lua_pushvalue(L, 2); // Stack: mt, pg, field name - lua_rawget(L, -2); // Stack: mt, pg, getter | nil - lua_remove(L, -2); // Stack: mt, getter | nil - - if (lua_iscfunction(L, -1)) // Stack: mt, getter - { - lua_remove(L, -2); // Stack: getter - lua_pushvalue(L, 1); // Stack: getter, table | userdata - lua_call(L, 1, 1); // Stack: value - return 1; - } - - assert(lua_isnil(L, -1)); // Stack: mt, nil - lua_pop(L, 1); // Stack: mt - - // It may mean that the field may be in const table and it's constness violation. - // Don't check that, just return nil - - // Repeat the lookup in the parent metafield, - // or return nil if the field doesn't exist. - lua_rawgetp(L, -1, getParentKey()); // Stack: mt, parent mt | nil - - if (lua_isnil(L, -1)) // Stack: mt, nil - { - lua_pop(L, 1); // Stack: mt - lua_rawgetp(L, -1, getIndexFallbackKey()); // Stack: mt, ifb (may be nil) - lua_remove(L, -2); // Stack: ifb - if (lua_iscfunction(L, -1)) - { - lua_pushvalue(L, 1); // Stack: ifb, arg1 - lua_pushvalue(L, 2); // Stack: ifb, arg2 - lua_call(L, 2, 1); // Stack: ifbresult - } - else - { - lua_pop(L, 1); - lua_pushnil(L); - } - - return 1; - } - - // Remove the metatable and repeat the search in the parent one. - assert(lua_istable(L, -1)); // Stack: mt, parent mt - lua_remove(L, -2); // Stack: parent mt - } - - // no return -} - -//================================================================================================= -/** - * @brief __newindex metamethod for non-static members. - * - * Retrieves properties from propset tables. - */ -inline int newindex_metamethod(lua_State* L, bool pushSelf) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 3, detail::error_lua_stack_overflow); -#endif - - assert(lua_istable(L, 1) || lua_isuserdata(L, 1)); // Stack (further not shown): table | userdata, name, new value - - lua_getmetatable(L, 1); // Stack: metatable (mt) - assert(lua_istable(L, -1)); - - for (;;) - { - lua_rawgetp(L, -1, getPropsetKey()); // Stack: mt, propset table (ps) | nil - - if (lua_isnil(L, -1)) // Stack: mt, nil - { - lua_pop(L, 2); // Stack: - - luaL_error(L, "No member named '%s'", lua_tostring(L, 2)); - } - - assert(lua_istable(L, -1)); - - lua_pushvalue(L, 2); // Stack: mt, ps, field name - lua_rawget(L, -2); // Stack: mt, ps, setter | nil - lua_remove(L, -2); // Stack: mt, setter | nil - - if (lua_iscfunction(L, -1)) // Stack: mt, setter - { - lua_remove(L, -2); // Stack: setter - if (pushSelf) - lua_pushvalue(L, 1); // Stack: setter, table | userdata - lua_pushvalue(L, 3); // Stack: setter, table | userdata, new value - lua_call(L, pushSelf ? 2 : 1, 0); // Stack: - - return 0; - } - - assert(lua_isnil(L, -1)); // Stack: mt, nil - lua_pop(L, 1); // Stack: mt - - lua_rawgetp(L, -1, getParentKey()); // Stack: mt, parent mt | nil - - if (lua_isnil(L, -1)) // Stack: mt, nil - { - lua_pop(L, 1); // Stack: mt - lua_rawgetp(L, -1, getNewIndexFallbackKey()); // Stack: mt, nifb (may be nil) - if (lua_iscfunction(L, -1)) - { - lua_pushvalue(L, 1); // stack: nifb, arg1 - lua_pushvalue(L, 2); // stack: nifb, arg2 - lua_pushvalue(L, 3); // stack: nifb, arg3 - lua_call(L, 3, 1); // stack: nifbresult - return 0; - } - - lua_pop(L, 1); // Stack: mt - lua_pop(L, 1); // Stack: - - luaL_error(L, "No writable member '%s'", lua_tostring(L, 2)); - return 0; - } - - assert(lua_istable(L, -1)); // Stack: mt, parent mt - lua_remove(L, -2); // Stack: parent mt - - // Repeat the search in the parent - } - - return 0; -} - -//================================================================================================= -/** - * @brief __newindex metamethod for objects. - */ -inline int newindex_object_metamethod(lua_State* L) -{ - return newindex_metamethod(L, true); -} - -//================================================================================================= -/** - * @brief __newindex metamethod for namespace or class static members. - * - * Retrieves properties from propset tables. - */ -inline int newindex_static_metamethod(lua_State* L) -{ - return newindex_metamethod(L, false); -} - -//================================================================================================= -/** - * @brief lua_CFunction to report an error writing to a read-only value. - * - * The name of the variable is in the first upvalue. - */ -inline int read_only_error(lua_State* L) -{ - std::string s; - - s = s + "'" + lua_tostring(L, lua_upvalueindex(1)) + "' is read-only"; - - luaL_error(L, "%s", s.c_str()); - - return 0; -} - -//================================================================================================= -/** - * @brief __gc metamethod for a class. - */ -template -static int gc_metamethod(lua_State* L) -{ - Userdata* ud = Userdata::getExact(L, 1); - assert(ud); - - ud->~Userdata(); - - return 0; -} - -//================================================================================================= - -template -struct property_getter; - -/** - * @brief lua_CFunction to get a variable. - * - * This is used for global variables or class static data members. The pointer to the data is in the first upvalue. - */ -template -struct property_getter -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - T* ptr = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - auto result = Stack::push(L, *ptr); - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } -}; - -#if 0 -template -struct property_getter, void> -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - std::reference_wrapper* ptr = static_cast*>(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - auto result = Stack::push(L, ptr->get()); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - - return 1; - } -}; -#endif - -/** - * @brief lua_CFunction to get a class data member. - * - * The pointer-to-member is in the first upvalue. The class userdata object is at the top of the Lua stack. - */ -template -struct property_getter -{ - static int call(lua_State* L) - { - C* c = Userdata::get(L, 1, true); - - T C::** mp = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - - Result result; - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - result = Stack::push(L, c->**mp); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } -}; - -/** - * @brief Helper function to push a property getter on a table at a specific index. - */ -inline void add_property_getter(lua_State* L, const char* name, int tableIndex) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 2, detail::error_lua_stack_overflow); -#endif - - assert(name != nullptr); - assert(lua_istable(L, tableIndex)); - assert(lua_iscfunction(L, -1)); // Stack: getter - - lua_rawgetp(L, tableIndex, getPropgetKey()); // Stack: getter, propget table (pg) - lua_pushvalue(L, -2); // Stack: getter, pg, getter - rawsetfield(L, -2, name); // Stack: getter, pg - lua_pop(L, 2); // Stack: - -} - -//================================================================================================= - -template -struct property_setter; - -/** - * @brief lua_CFunction to set a variable. - * - * This is used for global variables or class static data members. The pointer to the data is in the first upvalue. - */ -template -struct property_setter -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - T* ptr = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - auto result = Stack::get(L, 1); - if (! result) - raise_lua_error(L, "%s", result.error().message().c_str()); - - *ptr = std::move(*result); - - return 0; - } -}; - -#if 0 -template -struct property_setter, void> -{ - static int call(lua_State* L) - { - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - std::reference_wrapper* ptr = static_cast*>(lua_touserdata(L, lua_upvalueindex(1))); - assert(ptr != nullptr); - - ptr->get() = Stack::get(L, 1); - - return 0; - } -}; -#endif - -/** - * @brief lua_CFunction to set a class data member. - * - * The pointer-to-member is in the first upvalue. The class userdata object is at the top of the Lua stack. - */ -template -struct property_setter -{ - static int call(lua_State* L) - { - C* c = Userdata::get(L, 1, false); - - T C::** mp = static_cast(lua_touserdata(L, lua_upvalueindex(1))); - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - auto result = Stack::get(L, 2); - if (! result) - raise_lua_error(L, "%s", result.error().message().c_str()); - - c->** mp = std::move(*result); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - return 0; - } -}; - -/** - * @brief Helper function to push a property setter on a table at a specific index. - */ -inline void add_property_setter(lua_State* L, const char* name, int tableIndex) -{ -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(L, 2, detail::error_lua_stack_overflow); -#endif - - assert(name != nullptr); - assert(lua_istable(L, tableIndex)); - assert(lua_iscfunction(L, -1)); // Stack: setter - - lua_rawgetp(L, tableIndex, getPropsetKey()); // Stack: setter, propset table (ps) - lua_pushvalue(L, -2); // Stack: setter, ps, setter - rawsetfield(L, -2, name); // Stack: setter, ps - lua_pop(L, 2); // Stack: - -} - -//================================================================================================= -/** - * @brief Function generator. - */ -template -struct function -{ - template - static int call(lua_State* L, F func) - { - Result result; - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - result = Stack::push(L, std::apply(func, make_arguments_list(L))); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } - - template - static int call(lua_State* L, T* ptr, F func) - { - Result result; - -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - auto f = [ptr, func](auto&&... args) -> ReturnType { return (ptr->*func)(std::forward(args)...); }; - - result = Stack::push(L, std::apply(f, make_arguments_list(L))); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - if (! result) - raise_lua_error(L, "%s", result.message().c_str()); - - return 1; - } -}; - -template -struct function -{ - template - static int call(lua_State* L, F func) - { -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - std::apply(func, make_arguments_list(L)); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - return 0; - } - - template - static int call(lua_State* L, T* ptr, F func) - { -#if LUABRIDGE_HAS_EXCEPTIONS - try - { -#endif - auto f = [ptr, func](auto&&... args) { (ptr->*func)(std::forward(args)...); }; - - std::apply(f, make_arguments_list(L)); - -#if LUABRIDGE_HAS_EXCEPTIONS - } - catch (const std::exception& e) - { - raise_lua_error(L, "%s", e.what()); - } -#endif - - return 0; - } -}; - -//================================================================================================= -/** - * @brief lua_CFunction to call a class member function with a return value. - * - * The member function pointer is in the first upvalue. The class userdata object is at the top of the Lua stack. - */ -template -int invoke_member_function(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - T* ptr = Userdata::get(L, 1, false); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return function::call(L, ptr, func); -} - -template -int invoke_const_member_function(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - const T* ptr = Userdata::get(L, 1, true); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return function::call(L, ptr, func); -} - -//================================================================================================= -/** - * @brief lua_CFunction to call a class member lua_CFunction. - * - * The member function pointer is in the first upvalue. The object userdata ('this') value is at top ot the Lua stack. - */ -template -int invoke_member_cfunction(lua_State* L) -{ - using F = int (T::*)(lua_State * L); - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - T* t = Userdata::get(L, 1, false); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return (t->*func)(L); -} - -template -int invoke_const_member_cfunction(lua_State* L) -{ - using F = int (T::*)(lua_State * L) const; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - const T* t = Userdata::get(L, 1, true); - - const F& func = *static_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return (t->*func)(L); -} - -//================================================================================================= -/** - * @brief lua_CFunction to call on a object via function pointer. - * - * The proxy function pointer (lightuserdata) is in the first upvalue. The class userdata object is at the top of the Lua stack. - */ -template -int invoke_proxy_function(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(lua_islightuserdata(L, lua_upvalueindex(1))); - - auto func = reinterpret_cast(lua_touserdata(L, lua_upvalueindex(1))); - assert(func != nullptr); - - return function::call(L, func); -} - -//================================================================================================= -/** - * @brief lua_CFunction to call on a object via functor (lambda wrapped in a std::function). - * - * The proxy std::function (lightuserdata) is in the first upvalue. The class userdata object is at the top of the Lua stack. - */ -template -int invoke_proxy_functor(lua_State* L) -{ - using FnTraits = detail::function_traits; - - assert(isfulluserdata(L, lua_upvalueindex(1))); - - auto& func = *align(lua_touserdata(L, lua_upvalueindex(1))); - - return function::call(L, func); -} - -//================================================================================================= -/** - * @brief lua_CFunction to resolve an invocation between several overloads. - * - * The list of overloads is in the first upvalue. The arguments of the function call are at the top of the Lua stack. - */ -template -inline int try_overload_functions(lua_State* L) -{ - const int nargs = lua_gettop(L); - const int effective_args = nargs - (Member ? 1 : 0); - - // get the list of overloads - lua_pushvalue(L, lua_upvalueindex(1)); - assert(lua_istable(L, -1)); - const int idx_overloads = nargs + 1; - const int num_overloads = get_length(L, idx_overloads); - - // create table to hold error messages - lua_createtable(L, num_overloads, 0); - const int idx_errors = nargs + 2; - int nerrors = 0; - - // iterate through table, snippet taken from Lua docs - lua_pushnil(L); // first key - while (lua_next(L, idx_overloads) != 0) - { - assert(lua_istable(L, -1)); - - // check matching arity - lua_rawgeti(L, -1, 1); - assert(lua_isnumber(L, -1)); - - const int overload_arity = static_cast(lua_tointeger(L, -1)); - if (overload_arity >= 0 && overload_arity != effective_args) - { - // store error message and try next overload - lua_pushfstring(L, "Skipped overload #%d with unmatched arity of %d instead of %d", nerrors, overload_arity, effective_args); - lua_rawseti(L, idx_errors, ++nerrors); - - lua_pop(L, 2); // pop arity, value (table) - continue; - } - - lua_pop(L, 1); // pop arity - - // push function - lua_pushnumber(L, 2); - lua_gettable(L, -2); - assert(lua_isfunction(L, -1)); - - // push arguments - for (int i = 1; i <= nargs; ++i) - lua_pushvalue(L, i); - - // call f, this pops the function and its args, pushes result(s) - const int err = lua_pcall(L, nargs, LUA_MULTRET, 0); - if (err == LUABRIDGE_LUA_OK) - { - // calculate number of return values and return - const int nresults = lua_gettop(L) - nargs - 4; // 4: overloads, errors, key, table - return nresults; - } - else if (err == LUA_ERRRUN) - { - // store error message and try next overload - lua_rawseti(L, idx_errors, ++nerrors); - } - else - { - return lua_error_x(L); // critical error: rethrow - } - - lua_pop(L, 1); // pop value (table) - } - - lua_Debug debug; - lua_getstack_info_x(L, 0, "n", &debug); - lua_pushfstring(L, "All %d overloads of %s returned an error:", nerrors, debug.name); - - // Concatenate error messages of each overload - for (int i = 1; i <= nerrors; ++i) - { - lua_pushfstring(L, "\n%d: ", i); - lua_rawgeti(L, idx_errors, i); - } - lua_concat(L, nerrors * 2 + 1); - - return lua_error_x(L); // throw error message just built -} - -//================================================================================================= -// Lua CFunction -inline void push_function(lua_State* L, lua_CFunction fp) -{ - lua_pushcfunction_x(L, fp); -} - -// Generic function pointer -template -inline void push_function(lua_State* L, ReturnType (*fp)(Params...)) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -inline void push_function(lua_State* L, ReturnType (*fp)(Params...) noexcept) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -// Callable object (lambdas) -template && !std::is_pointer_v && !std::is_member_function_pointer_v>> -inline void push_function(lua_State* L, F&& f) -{ - lua_newuserdata_aligned(L, std::forward(f)); - lua_pushcclosure_x(L, &invoke_proxy_functor, 1); -} - -//================================================================================================= -// Lua CFunction -template -void push_member_function(lua_State* L, lua_CFunction fp) -{ - lua_pushcfunction_x(L, fp); -} - -// Generic function pointer -template -void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...)) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...) noexcept) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...)) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...) noexcept) -{ - using FnType = decltype(fp); - - lua_pushlightuserdata(L, reinterpret_cast(fp)); - lua_pushcclosure_x(L, &invoke_proxy_function, 1); -} - -// Callable object (lambdas) -template && - std::is_object_v && - !std::is_pointer_v && - !std::is_member_function_pointer_v>> -void push_member_function(lua_State* L, F&& f) -{ - static_assert(std::is_same_v>>>); - - lua_newuserdata_aligned(L, std::forward(f)); - lua_pushcclosure_x(L, &invoke_proxy_functor, 1); -} - -// Non const member function pointer -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...)) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_member_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) noexcept) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_member_function, 1); -} - -// Const member function pointer -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); -} - -template -void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const noexcept) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); -} - -// Non const member Lua CFunction pointer -template -void push_member_function(lua_State* L, int (U::*mfp)(lua_State*)) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_member_cfunction, 1); -} - -// Const member Lua CFunction pointer -template -void push_member_function(lua_State* L, int (U::*mfp)(lua_State*) const) -{ - static_assert(std::is_same_v || std::is_base_of_v); - - using F = decltype(mfp); - - new (lua_newuserdata_x(L, sizeof(F))) F(mfp); - lua_pushcclosure_x(L, &invoke_const_member_cfunction, 1); -} - -//================================================================================================= -/** - * @brief Constructor generators. - * - * These templates call operator new with the contents of a type/value list passed to the constructor. Two versions of call() are provided. - * One performs a regular new, the other performs a placement new. - */ -template -struct constructor; - -template -struct constructor -{ - using empty = std::tuple<>; - - static T* call(const empty&) - { - return new T; - } - - static T* call(void* ptr, const empty&) - { - return new (ptr) T; - } -}; - -template -struct constructor -{ - static T* call(const Args& args) - { - auto alloc = [](auto&&... args) { return new T(std::forward(args)...); }; - - return std::apply(alloc, args); - } - - static T* call(void* ptr, const Args& args) - { - auto alloc = [ptr](auto&&... args) { return new (ptr) T(std::forward(args)...); }; - - return std::apply(alloc, args); - } -}; - -//================================================================================================= -/** - * @brief Placement constructor generators. - */ -template -struct placement_constructor -{ - template - static T* construct(void* ptr, const F& func, const Args& args) - { - auto alloc = [ptr, &func](auto&&... args) { return func(ptr, std::forward(args)...); }; - - return std::apply(alloc, args); - } - - template - static T* construct(void* ptr, const F& func) - { - return func(ptr); - } -}; - -//================================================================================================= -/** - * @brief External allocator generators. - */ -template -struct external_constructor -{ - template - static T* construct(const F& func, const Args& args) - { - auto alloc = [&func](auto&&... args) { return func(std::forward(args)...); }; - - return std::apply(alloc, args); - } - - template - static T* construct(const F& func) - { - return func(); - } -}; - -//================================================================================================= -/** - * @brief lua_CFunction to construct a class object wrapped in a container. - */ -template -int constructor_container_proxy(lua_State* L) -{ - using T = typename ContainerTraits::Type; - - T* object = detail::constructor::call(detail::make_arguments_list(L)); - - auto result = detail::UserdataSharedHelper::push(L, object); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - - return 1; -} - -/** - * @brief lua_CFunction to construct a class object in-place in the userdata. - */ -template -int constructor_placement_proxy(lua_State* L) -{ - std::error_code ec; - auto* value = detail::UserdataValue::place(L, ec); - if (! value) - luaL_error(L, "%s", ec.message().c_str()); - - detail::constructor::call(value->getObject(), detail::make_arguments_list(L)); - - value->commit(); - - return 1; -} - -//================================================================================================= -/** - * @brief Constructor forwarder. - */ -template -struct constructor_forwarder -{ - explicit constructor_forwarder(F f) - : m_func(std::move(f)) - { - } - - T* operator()(lua_State* L) - { - std::error_code ec; - auto* value = UserdataValue::place(L, ec); - if (! value) - luaL_error(L, "%s", ec.message().c_str()); - - using FnTraits = function_traits; - using FnArgs = remove_first_type_t; - - T* obj = placement_constructor::construct( - value->getObject(), m_func, make_arguments_list(L)); - - value->commit(); - - return obj; - } - -private: - F m_func; -}; - -//================================================================================================= -/** - * @brief Constructor forwarder. - */ -template -struct factory_forwarder -{ - explicit factory_forwarder(Alloc alloc, Dealloc dealloc) - : m_alloc(std::move(alloc)) - , m_dealloc(std::move(dealloc)) - { - } - - T* operator()(lua_State* L) - { - using FnTraits = function_traits; - using FnArgs = typename FnTraits::argument_types; - - T* obj = external_constructor::construct(m_alloc, make_arguments_list(L)); - - std::error_code ec; - auto* value = UserdataValueExternal::place(L, obj, m_dealloc, ec); - if (! value) - luaL_error(L, "%s", ec.message().c_str()); - - return obj; - } - -private: - Alloc m_alloc; - Dealloc m_dealloc; -}; - -} // namespace detail -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/ClassInfo.h b/LuaBridge3/Source/LuaBridge/detail/ClassInfo.h deleted file mode 100644 index 5c70dfc..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/ClassInfo.h +++ /dev/null @@ -1,183 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include -#include -#include - -#if defined __clang__ || defined __GNUC__ -#define LUABRIDGE_PRETTY_FUNCTION __PRETTY_FUNCTION__ -#define LUABRIDGE_PRETTY_FUNCTION_PREFIX '=' -#define LUABRIDGE_PRETTY_FUNCTION_SUFFIX ']' -#elif defined _MSC_VER -#define LUABRIDGE_PRETTY_FUNCTION __FUNCSIG__ -#define LUABRIDGE_PRETTY_FUNCTION_PREFIX '<' -#define LUABRIDGE_PRETTY_FUNCTION_SUFFIX '>' -#endif - -namespace luabridge { -namespace detail { - -[[nodiscard]] constexpr auto fnv1a(const char* s, std::size_t count) noexcept -{ - uint32_t seed = 2166136261u; - - for (std::size_t i = 0; i < count; ++i) - seed ^= static_cast(*s++) * 16777619u; - - if constexpr (sizeof(void*) == 8) - return static_cast(seed); - else - return seed; -} - -template -[[nodiscard]] static constexpr auto typeName() noexcept -{ - constexpr std::string_view prettyName{ LUABRIDGE_PRETTY_FUNCTION }; - - constexpr auto first = prettyName.find_first_not_of(' ', prettyName.find_first_of(LUABRIDGE_PRETTY_FUNCTION_PREFIX) + 1); - - return prettyName.substr(first, prettyName.find_last_of(LUABRIDGE_PRETTY_FUNCTION_SUFFIX) - first); -} - -template ().find_first_of('.')> -[[nodiscard]] static constexpr auto typeHash() noexcept -{ - constexpr auto stripped = typeName(); - - return fnv1a(stripped.data(), stripped.size()); -} - -//================================================================================================= -/** - * @brief A unique key for a type name in a metatable. - */ -[[nodiscard]] inline const void* getTypeKey() noexcept -{ - return reinterpret_cast(0x71); -} - -//================================================================================================= -/** - * @brief The key of a const table in another metatable. - */ -[[nodiscard]] inline const void* getConstKey() noexcept -{ - return reinterpret_cast(0xc07); -} - -//================================================================================================= -/** - * @brief The key of a class table in another metatable. - */ -[[nodiscard]] inline const void* getClassKey() noexcept -{ - return reinterpret_cast(0xc1a); -} - -//================================================================================================= -/** - * @brief The key of a propget table in another metatable. - */ -[[nodiscard]] inline const void* getPropgetKey() noexcept -{ - return reinterpret_cast(0x6e7); -} - -//================================================================================================= -/** - * @brief The key of a propset table in another metatable. - */ -[[nodiscard]] inline const void* getPropsetKey() noexcept -{ - return reinterpret_cast(0x5e7); -} - -//================================================================================================= -/** - * @brief The key of a static table in another metatable. - */ -[[nodiscard]] inline const void* getStaticKey() noexcept -{ - return reinterpret_cast(0x57a); -} - -//================================================================================================= -/** - * @brief The key of a parent table in another metatable. - */ -[[nodiscard]] inline const void* getParentKey() noexcept -{ - return reinterpret_cast(0xdad); -} - -//================================================================================================= -/** - * The key of the index fall back in another metatable. - */ -[[nodiscard]] inline const void* getIndexFallbackKey() -{ - return reinterpret_cast(0x81ca); -} - -//================================================================================================= -/** - * The key of the new index fall back in another metatable. - */ -[[nodiscard]] inline const void* getNewIndexFallbackKey() -{ - return reinterpret_cast(0x8107); -} - -//================================================================================================= -/** - * @brief Get the key for the static table in the Lua registry. - * - * The static table holds the static data members, static properties, and static member functions for a class. - */ -template -[[nodiscard]] const void* getStaticRegistryKey() noexcept -{ - static auto value = typeHash(); - - return reinterpret_cast(value); -} - -//================================================================================================= -/** - * @brief Get the key for the class table in the Lua registry. - * - * The class table holds the data members, properties, and member functions of a class. Read-only data and properties, and const - * member functions are also placed here (to save a lookup in the const table). - */ -template -[[nodiscard]] const void* getClassRegistryKey() noexcept -{ - static auto value = typeHash() ^ 1; - - return reinterpret_cast(value); -} - -//================================================================================================= -/** - * @brief Get the key for the const table in the Lua registry. - * - * The const table holds read-only data members and properties, and const member functions of a class. - */ -template -[[nodiscard]] const void* getConstRegistryKey() noexcept -{ - static auto value = typeHash() ^ 2; - - return reinterpret_cast(value); -} -} // namespace detail -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Config.h b/LuaBridge3/Source/LuaBridge/detail/Config.h deleted file mode 100644 index 3bb15e2..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Config.h +++ /dev/null @@ -1,53 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// Copyright 2019, George Tokmaji -// SPDX-License-Identifier: MIT - -#pragma once - -#if !(__cplusplus >= 201703L || (defined(_MSC_VER) && _HAS_CXX17)) -#error LuaBridge 3 requires a compliant C++17 compiler, or C++17 has not been enabled ! -#endif - -#if defined(_MSC_VER) -#if _CPPUNWIND || _HAS_EXCEPTIONS -#define LUABRIDGE_HAS_EXCEPTIONS 1 -#else -#define LUABRIDGE_HAS_EXCEPTIONS 0 -#endif -#elif defined(__clang__) -#if __EXCEPTIONS && __has_feature(cxx_exceptions) -#define LUABRIDGE_HAS_EXCEPTIONS 1 -#else -#define LUABRIDGE_HAS_EXCEPTIONS 0 -#endif -#elif defined(__GNUC__) -#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) -#define LUABRIDGE_HAS_EXCEPTIONS 1 -#else -#define LUABRIDGE_HAS_EXCEPTIONS 0 -#endif -#endif - -#if defined(LUAU_FASTMATH_BEGIN) -#define LUABRIDGE_ON_LUAU 1 -#elif defined(LUAJIT_VERSION) -#define LUABRIDGE_ON_LUAJIT 1 -#elif defined(LUA_VERSION_NUM) -#define LUABRIDGE_ON_LUA 1 -#else -#error "Lua headers must be included prior to LuaBridge ones" -#endif - -#if defined(__OBJC__) -#define LUABRIDGE_ON_OBJECTIVE_C 1 -#endif - -#if !defined(LUABRIDGE_SAFE_STACK_CHECKS) -#define LUABRIDGE_SAFE_STACK_CHECKS 1 -#endif - -#if !defined(LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE) && LUABRIDGE_HAS_EXCEPTIONS -#define LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE 1 -#endif diff --git a/LuaBridge3/Source/LuaBridge/detail/Dump.h b/LuaBridge3/Source/LuaBridge/detail/Dump.h deleted file mode 100644 index 4401396..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Dump.h +++ /dev/null @@ -1,120 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#pragma once - -#include "ClassInfo.h" - -#include -#include - -namespace luabridge { -namespace debug { - -inline void putIndent(std::ostream& stream, unsigned level) -{ - for (unsigned i = 0; i < level; ++i) - { - stream << " "; - } -} - -inline void dumpTable(lua_State* L, int index, std::ostream& stream, unsigned level); - -inline void dumpValue(lua_State* L, int index, std::ostream& stream, unsigned level = 0) -{ - const int type = lua_type(L, index); - switch (type) - { - case LUA_TNIL: - stream << "nil"; - break; - - case LUA_TBOOLEAN: - stream << (lua_toboolean(L, index) ? "true" : "false"); - break; - - case LUA_TNUMBER: - stream << lua_tonumber(L, index); - break; - - case LUA_TSTRING: - stream << '"' << lua_tostring(L, index) << '"'; - break; - - case LUA_TFUNCTION: - if (lua_iscfunction(L, index)) - { - stream << "cfunction@" << lua_topointer(L, index); - } - else - { - stream << "function@" << lua_topointer(L, index); - } - break; - - case LUA_TTHREAD: - stream << "thread@" << lua_tothread(L, index); - break; - - case LUA_TLIGHTUSERDATA: - stream << "lightuserdata@" << lua_touserdata(L, index); - break; - - case LUA_TTABLE: - dumpTable(L, index, stream, level); - break; - - case LUA_TUSERDATA: - stream << "userdata@" << lua_touserdata(L, index); - break; - - default: - stream << lua_typename(L, type); - ; - break; - } -} - -inline void dumpTable(lua_State* L, int index, std::ostream& stream, unsigned level = 0) -{ - stream << "table@" << lua_topointer(L, index); - - if (level > 0) - { - return; - } - - index = lua_absindex(L, index); - stream << " {"; - lua_pushnil(L); // Initial key - while (lua_next(L, index)) - { - stream << "\n"; - putIndent(stream, level + 1); - dumpValue(L, -2, stream, level + 1); // Key - stream << ": "; - dumpValue(L, -1, stream, level + 1); // Value - lua_pop(L, 1); // Value - } - putIndent(stream, level); - stream << "\n}"; -} - -inline void dumpState(lua_State* L, std::ostream& stream = std::cerr) -{ - int top = lua_gettop(L); - for (int i = 1; i <= top; ++i) - { - stream << "stack #" << i << ": "; - dumpValue(L, i, stream, 0); - stream << "\n"; - } -} - -} // namespace debug -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Errors.h b/LuaBridge3/Source/LuaBridge/detail/Errors.h deleted file mode 100644 index b249ded..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Errors.h +++ /dev/null @@ -1,108 +0,0 @@ -// https://github.com/vinniefalco/LuaBridge -// Copyright 2021, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include - -namespace luabridge { - -//================================================================================================= -namespace detail { - -static inline constexpr char error_lua_stack_overflow[] = "stack overflow"; - -} // namespace detail - -//================================================================================================= -/** - * @brief LuaBridge error codes. - */ -enum class ErrorCode -{ - ClassNotRegistered = 1, - - LuaStackOverflow, - - LuaFunctionCallFailed, - - IntegerDoesntFitIntoLuaInteger, - - FloatingPointDoesntFitIntoLuaNumber, - - InvalidTypeCast, - - InvalidTableSizeInCast -}; - -//================================================================================================= -namespace detail { -struct ErrorCategory : std::error_category -{ - const char* name() const noexcept override - { - return "luabridge"; - } - - std::string message(int ev) const override - { - switch (static_cast(ev)) - { - case ErrorCode::ClassNotRegistered: - return "The class is not registered in LuaBridge"; - - case ErrorCode::LuaStackOverflow: - return "The lua stack has overflow"; - - case ErrorCode::LuaFunctionCallFailed: - return "The lua function invocation raised an error"; - - case ErrorCode::IntegerDoesntFitIntoLuaInteger: - return "The native integer can't fit inside a lua integer"; - - case ErrorCode::FloatingPointDoesntFitIntoLuaNumber: - return "The native floating point can't fit inside a lua number"; - - case ErrorCode::InvalidTypeCast: - return "The lua object can't be casted to desired type"; - - case ErrorCode::InvalidTableSizeInCast: - return "The lua table has different size than expected"; - - default: - return "Unknown error"; - } - } - - static const ErrorCategory& getInstance() noexcept - { - static ErrorCategory category; - return category; - } -}; -} // namespace detail - -//================================================================================================= -/** - * @brief Construct an error code from the error enum. - */ -inline std::error_code makeErrorCode(ErrorCode e) -{ - return { static_cast(e), detail::ErrorCategory::getInstance() }; -} - -/** - * @brief Supports std::error_code construction. - */ -inline std::error_code make_error_code(ErrorCode e) -{ - return { static_cast(e), detail::ErrorCategory::getInstance() }; -} -} // namespace luabridge - -namespace std { -template <> struct is_error_code_enum : true_type {}; -} // namespace std diff --git a/LuaBridge3/Source/LuaBridge/detail/Expected.h b/LuaBridge3/Source/LuaBridge/detail/Expected.h deleted file mode 100644 index ea623e4..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Expected.h +++ /dev/null @@ -1,1594 +0,0 @@ -// https://github.com/vinniefalco/LuaBridge -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include -#include -#include - -#if LUABRIDGE_HAS_EXCEPTIONS -#include -#endif - -namespace luabridge { -namespace detail { -using std::swap; - -template -T* construct_at(T* ptr, Args&&... args) noexcept(std::is_nothrow_constructible::value) -{ - return static_cast(::new (const_cast(static_cast(ptr))) T(std::forward(args)...)); -} - -template -struct is_swappable_with_impl : std::false_type -{ -}; - -template -struct is_swappable_with_impl(), std::declval()))>> - : std::true_type -{ -}; - -template -struct is_nothrow_swappable_with_impl -{ - static constexpr bool value = noexcept(swap(std::declval(), std::declval())) && noexcept(swap(std::declval(), std::declval())); - - using type = std::bool_constant; -}; - -template -struct is_swappable_with - : std::conjunction< - is_swappable_with_impl, std::add_lvalue_reference_t>, - is_swappable_with_impl, std::add_lvalue_reference_t>>::type -{ -}; - -template -struct is_nothrow_swappable_with - : std::conjunction, is_nothrow_swappable_with_impl>::type -{ -}; - -template -struct is_nothrow_swappable - : std::is_nothrow_swappable_with, std::add_lvalue_reference_t> -{ -}; -} // namespace detail - -template -class Expected; - -struct UnexpectType -{ - constexpr UnexpectType() = default; -}; - -static constexpr const auto& unexpect = UnexpectType(); - -namespace detail { -template , bool = (std::is_void_v || std::is_trivial_v) && std::is_trivial_v> -union expected_storage -{ -public: - template >> - constexpr expected_storage() noexcept - : value_() - { - } - - template - constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept - : value_(std::forward(args)...) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept - : error_(std::forward(args)...) - { - } - - ~expected_storage() = default; - - constexpr const T& value() const noexcept - { - return value_; - } - - constexpr T& value() noexcept - { - return value_; - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - T value_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr expected_storage() noexcept - : dummy_(0) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept - : error_(std::forward(args)...) - { - } - - ~expected_storage() = default; - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - char dummy_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr expected_storage() noexcept(std::is_nothrow_default_constructible_v) - : value_() - { - } - - template - constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : value_(std::forward(args)...) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : error_(std::forward(args)...) - { - } - - ~expected_storage() - { - } - - constexpr const T& value() const noexcept - { - return value_; - } - - constexpr T& value() noexcept - { - return value_; - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - T value_; - E error_; -}; - -template -union expected_storage -{ -public: - constexpr explicit expected_storage() noexcept - : dummy_(0) - { - } - - template - constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : value_(std::forward(args)...) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : error_(std::forward(args)...) - { - } - - ~expected_storage() - { - } - - constexpr const T& value() const noexcept - { - return value_; - } - - constexpr T& value() noexcept - { - return value_; - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - char dummy_; - T value_; - E error_; -}; - - -template -union expected_storage -{ -public: - constexpr expected_storage() noexcept - : dummy_(0) - { - } - - template - constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : error_(std::forward(args)...) - { - } - - ~expected_storage() - { - } - - constexpr const E& error() const noexcept - { - return error_; - } - - constexpr E& error() noexcept - { - return error_; - } - -private: - char dummy_; - E error_; -}; - -template -class ExpectedBaseTrivial -{ - using this_type = ExpectedBaseTrivial; - -protected: - using storage_type = expected_storage; - - constexpr ExpectedBaseTrivial() noexcept - : valid_(true) - { - } - - template - constexpr ExpectedBaseTrivial(std::in_place_t, Args&&... args) noexcept - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseTrivial(UnexpectType, Args&&... args) noexcept - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseTrivial(const ExpectedBaseTrivial& other) noexcept - { - if (other.valid_) - { - construct(std::in_place, other.value()); - } - else - { - construct(unexpect, other.error()); - } - } - - ExpectedBaseTrivial(ExpectedBaseTrivial&& other) noexcept - { - if (other.valid_) - { - construct(std::in_place, std::move(other.value())); - } - else - { - construct(unexpect, std::move(other.error())); - } - } - - ~ExpectedBaseTrivial() noexcept = default; - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept - { - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) - { - if (other.valid_) - { - construct(std::in_place, other.value()); - } - else - { - construct(unexpect, other.error()); - } - } - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) - { - if (other.valid_) - { - construct(std::in_place, std::move(other.value())); - } - else - { - construct(unexpect, std::move(other.error())); - } - } - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) = delete; - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) - { - if (other.valid_) - { - construct(std::in_place, std::move(other.value())); - } - else - { - construct(unexpect, std::move(other.error())); - } - } - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - template >> - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) - { - if (other.valid_) - { - construct(std::in_place, other.value()); - } - else - { - construct(unexpect, other.error()); - } - } - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) = delete; - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -class ExpectedBaseNonTrivial -{ - using this_type = ExpectedBaseNonTrivial; - -protected: - using storage_type = expected_storage; - - template >> - constexpr ExpectedBaseNonTrivial() noexcept(std::is_nothrow_default_constructible_v) - : valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(std::in_place, std::forward(args)...) - , valid_(true) - { - } - - template - constexpr ExpectedBaseNonTrivial(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : storage_(unexpect, std::forward(args)...) - , valid_(false) - { - } - - ExpectedBaseNonTrivial(const ExpectedBaseNonTrivial& other) = delete; - - ExpectedBaseNonTrivial(ExpectedBaseNonTrivial&& other) = delete; - - ~ExpectedBaseNonTrivial() noexcept(noexcept(std::declval().destroy())) - { - destroy(); - } - - constexpr const T& value() const noexcept - { - return storage_.value(); - } - - constexpr T& value() noexcept - { - return storage_.value(); - } - - constexpr const E& error() const noexcept - { - return storage_.error(); - } - - constexpr E& error() noexcept - { - return storage_.error(); - } - - constexpr const T* valuePtr() const noexcept - { - return std::addressof(value()); - } - - constexpr T* valuePtr() noexcept - { - return std::addressof(value()); - } - - constexpr const E* errorPtr() const noexcept - { - return std::addressof(error()); - } - - constexpr E* errorPtr() noexcept - { - return std::addressof(error()); - } - - constexpr bool valid() const noexcept - { - return valid_; - } - - template - inline T& construct(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = true; - return *detail::construct_at(valuePtr(), std::forward(args)...); - } - - template - inline E& construct(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - { - valid_ = false; - return *detail::construct_at(errorPtr(), std::forward(args)...); - } - - inline void destroy() noexcept(std::is_nothrow_destructible_v&& std::is_nothrow_destructible_v) - { - if (valid_) - { - std::destroy_at(valuePtr()); - } - else - { - std::destroy_at(errorPtr()); - } - } - -private: - storage_type storage_; - bool valid_; -}; - -template -using ExpectedBase = std::conditional_t< - (std::is_void_v || std::is_trivially_destructible_v) && std::is_trivially_destructible_v, - ExpectedBaseTrivial, - ExpectedBaseNonTrivial>; - -} // namespace detail - -template -class Unexpected -{ - static_assert(!std::is_reference_v && !std::is_void_v, "Unexpected type can't be a reference or void"); - -public: - Unexpected() = delete; - - constexpr explicit Unexpected(E&& e) noexcept(std::is_nothrow_move_constructible_v) - : error_(std::move(e)) - { - } - - constexpr explicit Unexpected(const E& e) noexcept(std::is_nothrow_copy_constructible_v) - : error_(e) - { - } - - constexpr const E& value() const& noexcept - { - return error_; - } - - constexpr E& value() & noexcept - { - return error_; - } - - constexpr const E&& value() const&& noexcept - { - return std::move(error_); - } - - constexpr E&& value() && noexcept - { - return std::move(error_); - } - -private: - E error_; -}; - -template -constexpr bool operator==(const Unexpected& lhs, const Unexpected& rhs) noexcept -{ - return lhs.value() == rhs.value(); -} - -template -constexpr bool operator!=(const Unexpected& lhs, const Unexpected& rhs) noexcept -{ - return lhs.value() != rhs.value(); -} - -template -constexpr inline Unexpected> makeUnexpected(E&& error) noexcept(std::is_nothrow_constructible_v>, E>) -{ - return Unexpected>{ std::forward(error) }; -} - -#if LUABRIDGE_HAS_EXCEPTIONS -template -class BadExpectedAccess; - -template <> -class BadExpectedAccess : public std::exception -{ -public: - explicit BadExpectedAccess() noexcept - { - } -}; -template -class BadExpectedAccess : public BadExpectedAccess -{ -public: - explicit BadExpectedAccess(E error) noexcept(std::is_nothrow_constructible_v) - : error_(std::move(error)) - { - } - - const E& error() const& noexcept - { - return error_; - } - - E& error() & noexcept - { - return error_; - } - - E&& error() && noexcept - { - return std::move(error_); - } - -private: - E error_; -}; -#endif - -template -struct is_expected : std::false_type -{ -}; - -template -struct is_expected> : std::true_type -{ -}; -template -struct is_unexpected : std::false_type -{ -}; - -template -struct is_unexpected> : std::true_type -{ -}; - -template -class Expected : public detail::ExpectedBase, std::is_move_constructible_v> -{ - static_assert(!std::is_reference_v && !std::is_void_v, "Unexpected type can't be a reference or void"); - - using base_type = detail::ExpectedBase, std::is_move_constructible_v>; - using this_type = Expected; - -public: - using value_type = T; - - using error_type = E; - - using unexpected_type = Unexpected; - - template - struct rebind - { - using type = Expected; - }; - - template >> - constexpr Expected() noexcept(std::is_nothrow_default_constructible_v) - : base_type() - { - } - - constexpr Expected(const Expected& other) noexcept(std::is_nothrow_copy_constructible_v) = default; - - constexpr Expected(Expected&& other) noexcept(std::is_nothrow_move_constructible_v) = default; - - template - Expected(const Expected& other) - { - if (other.hasValue()) - { - this->construct(std::in_place, other.value()); - } - else - { - this->construct(unexpect, other.error()); - } - } - - template - Expected(Expected&& other) - { - if (other.hasValue()) - { - this->construct(std::in_place, std::move(other.value())); - } - else - { - this->construct(unexpect, std::move(other.error())); - } - } - - template && std::is_constructible_v && !std::is_same_v, std::in_place_t> && !is_expected>::value && !is_unexpected>::value, int> = 0> - constexpr Expected(U&& value) noexcept(std::is_nothrow_constructible_v) - : base_type(std::in_place, std::forward(value)) - { - } - - template - constexpr explicit Expected(std::in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : base_type(std::in_place, std::forward(args)...) - { - } - - template - constexpr explicit Expected(std::in_place_t, std::initializer_list ilist, Args&&... args) noexcept(std::is_nothrow_constructible_v, Args...>) - : base_type(std::in_place, ilist, std::forward(args)...) - { - } - - template - constexpr Expected(const Unexpected& u) noexcept(std::is_nothrow_constructible_v) - : base_type(unexpect, u.value()) - { - } - - template - constexpr Expected(Unexpected&& u) noexcept(std::is_nothrow_constructible_v) - : base_type(unexpect, std::move(u.value())) - { - } - - template - constexpr explicit Expected(UnexpectType, Args&&... args) noexcept(std::is_nothrow_constructible_v) - : base_type(unexpect, std::forward(args)...) - { - } - - template - constexpr explicit Expected(UnexpectType, std::initializer_list ilist, Args&&... args) noexcept(std::is_nothrow_constructible_v, Args...>) - : base_type(unexpect, ilist, std::forward(args)...) - { - } - - Expected& operator=(const Expected& other) - { - if (other.hasValue()) - { - assign(std::in_place, other.value()); - } - else - { - assign(unexpect, other.error()); - } - - return *this; - } - - Expected& operator=(Expected&& other) - { - if (other.hasValue()) - { - assign(std::in_place, std::move(other.value())); - } - else - { - assign(unexpect, std::move(other.error())); - } - - return *this; - } - - template >::value && !is_unexpected>::value, int> = 0> - Expected& operator=(U&& value) - { - assign(std::in_place, std::forward(value)); - return *this; - } - - template - Expected& operator=(const Unexpected& u) - { - assign(unexpect, u.value()); - return *this; - } - - template - Expected& operator=(Unexpected&& u) - { - assign(unexpect, std::move(u.value())); - return *this; - } - - template - T& emplace(Args&&... args) noexcept(noexcept(std::declval().assign(std::in_place, std::forward(args)...))) - { - return assign(std::in_place, std::forward(args)...); - } - - template - T& emplace(std::initializer_list ilist, Args&&... args) noexcept(noexcept(std::declval().assign(std::in_place, ilist, std::forward(args)...))) - { - return assign(std::in_place, ilist, std::forward(args)...); - } - - void swap(Expected& other) noexcept(detail::is_nothrow_swappable::value && detail::is_nothrow_swappable::value) - { - using std::swap; - - if (hasValue()) - { - if (other.hasValue()) - { - swap(value(), other.value()); - } - else - { - E error = std::move(other.error()); - other.assign(std::in_place, std::move(value())); - assign(unexpect, std::move(error)); - } - } - else - { - if (other.hasValue()) - { - other.swap(*this); - } - else - { - swap(error(), other.error()); - } - } - } - - constexpr const T* operator->() const - { - return base_type::valuePtr(); - } - - constexpr T* operator->() - { - return base_type::valuePtr(); - } - - constexpr const T& operator*() const& - { - return value(); - } - - constexpr T& operator*() & - { - return value(); - } - - constexpr const T&& operator*() const&& - { - return std::move(value()); - } - - constexpr T&& operator*() && - { - return std::move(value()); - } - - constexpr explicit operator bool() const noexcept - { - return hasValue(); - } - - constexpr bool hasValue() const noexcept - { - return base_type::valid(); - } - - constexpr const T& value() const& - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - - return base_type::value(); - } - - constexpr T& value() & - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - - return base_type::value(); - } - - constexpr const T&& value() const&& noexcept - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - - return std::move(base_type::value()); - } - - constexpr T&& value() && - { -#if LUABRIDGE_HAS_EXCEPTIONS - if (!hasValue()) - throw BadExpectedAccess(error()); -#endif - return std::move(base_type::value()); - } - - constexpr const E& error() const& noexcept - { - return base_type::error(); - } - - constexpr E& error() & noexcept - { - return base_type::error(); - } - - constexpr const E&& error() const&& noexcept - { - return std::move(base_type::error()); - } - - constexpr E&& error() && noexcept - { - return std::move(base_type::error()); - } - - template - constexpr T valueOr(U&& defaultValue) const& - { - return hasValue() ? value() : static_cast(std::forward(defaultValue)); - } - - template - T valueOr(U&& defaultValue) && - { - return hasValue() ? std::move(value()) : static_cast(std::forward(defaultValue)); - } - -private: - template - auto assign(Tag tag, Args&&... args) noexcept(noexcept(std::declval().destroy()) && noexcept(std::declval().construct(tag, std::forward(args)...))) - -> decltype(std::declval().construct(tag, std::forward(args)...)) - { - this->destroy(); - - return this->construct(tag, std::forward(args)...); - } -}; - -template -class Expected : public detail::ExpectedBase, std::is_move_constructible_v> -{ - static_assert(!std::is_reference_v && !std::is_void_v, "Unexpected type can't be a reference or void"); - - using base_type = detail::ExpectedBase, std::is_move_constructible_v>; - using this_type = Expected; - -public: - using value_type = void; - - using error_type = E; - - using unexpected_type = Unexpected; - - template - struct rebind - { - using type = Expected; - }; - - constexpr Expected() = default; - - constexpr Expected(const Expected& other) = default; - - constexpr Expected(Expected&& other) = default; - - template - Expected(const Expected& other) - { - if (other.hasValue()) - { - this->valid_ = true; - } - else - { - this->construct(unexpect, other.error()); - } - } - - template - Expected(Expected&& other) - { - if (other.hasValue()) - { - this->valid_ = true; - } - else - { - this->construct(unexpect, std::move(other.error())); - } - } - - template - constexpr Expected(const Unexpected& u) - : base_type(unexpect, u.value()) - { - } - - template - constexpr Expected(Unexpected&& u) - : base_type(unexpect, std::move(u.value())) - { - } - - template - constexpr explicit Expected(UnexpectType, Args&&... args) - : base_type(unexpect, std::forward(args)...) - { - } - - template - constexpr explicit Expected(UnexpectType, std::initializer_list ilist, Args&&... args) - : base_type(unexpect, ilist, std::forward(args)...) - { - } - - Expected& operator=(const Expected& other) - { - if (other.hasValue()) - { - assign(std::in_place); - } - else - { - assign(unexpect, other.error()); - } - - return *this; - } - - Expected& operator=(Expected&& other) - { - if (other.hasValue()) - { - assign(std::in_place); - } - else - { - assign(unexpect, std::move(other.error())); - } - - return *this; - } - - template - Expected& operator=(const Unexpected& u) - { - assign(unexpect, u.value()); - return *this; - } - - template - Expected& operator=(Unexpected&& u) - { - assign(unexpect, std::move(u.value())); - return *this; - } - - void swap(Expected& other) noexcept(detail::is_nothrow_swappable::value) - { - using std::swap; - - if (hasValue()) - { - if (!other.hasValue()) - { - assign(unexpect, std::move(other.error())); - other.assign(std::in_place); - } - } - else - { - if (other.hasValue()) - { - other.swap(*this); - } - else - { - swap(error(), other.error()); - } - } - } - - constexpr explicit operator bool() const noexcept - { - return hasValue(); - } - - constexpr bool hasValue() const noexcept - { - return base_type::valid(); - } - - constexpr const E& error() const& noexcept - { - return base_type::error(); - } - - constexpr E& error() & noexcept - { - return base_type::error(); - } - - constexpr const E&& error() const&& noexcept - { - return std::move(base_type::error()); - } - - constexpr E&& error() && noexcept - { - return std::move(base_type::error()); - } - -private: - template - void assign(Tag tag, Args&&... args) noexcept(noexcept(std::declval().destroy()) && noexcept(std::declval().construct(tag, std::forward(args)...))) - { - this->destroy(); - this->construct(tag, std::forward(args)...); - } -}; - -template -constexpr bool operator==(const Expected& lhs, const Expected& rhs) -{ - return (lhs && rhs) ? *lhs == *rhs : ((!lhs && !rhs) ? lhs.error() == rhs.error() : false); -} - -template -constexpr bool operator==(const Expected& lhs, const Expected& rhs) -{ - return (lhs && rhs) ? true : ((!lhs && !rhs) ? lhs.error() == rhs.error() : false); -} - -template -constexpr bool operator!=(const Expected& lhs, const Expected& rhs) -{ - return !(lhs == rhs); -} - -template -constexpr bool operator==(const Expected& lhs, const T& rhs) -{ - return lhs ? *lhs == rhs : false; -} - -template -constexpr bool operator==(const T& lhs, const Expected& rhs) -{ - return rhs == lhs; -} - -template -constexpr bool operator!=(const Expected& lhs, const T& rhs) -{ - return !(lhs == rhs); -} - -template -constexpr bool operator!=(const T& lhs, const Expected& rhs) -{ - return rhs != lhs; -} - -template -constexpr bool operator==(const Expected& lhs, const Unexpected& rhs) -{ - return lhs ? false : lhs.error() == rhs.value(); -} - -template -constexpr bool operator==(const Unexpected& lhs, const Expected& rhs) -{ - return rhs == lhs; -} - -template -constexpr bool operator!=(const Expected& lhs, const Unexpected& rhs) -{ - return !(lhs == rhs); -} - -template -constexpr bool operator!=(const Unexpected& lhs, const Expected& rhs) -{ - return rhs != lhs; -} -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/FuncTraits.h b/LuaBridge3/Source/LuaBridge/detail/FuncTraits.h deleted file mode 100644 index b034f69..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/FuncTraits.h +++ /dev/null @@ -1,508 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// Copyright 2019, George Tokmaji -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include -#include - -namespace luabridge { -namespace detail { - -//================================================================================================= -/** - * @brief Invokes undefined behavior when an unreachable part of the code is reached. - * - * An implementation may use this to optimize impossible code branches away (typically, in optimized builds) or to trap them to prevent - * further execution (typically, in debug builds). - */ -[[noreturn]] inline void unreachable() -{ -#if __GNUC__ // GCC, Clang, ICC - __builtin_unreachable(); -#elif _MSC_VER // MSVC - __assume(false); -#endif -} - -//================================================================================================= -/** - * @brief Provides the member typedef type which is the type referred to by T with its topmost cv-qualifiers removed. - */ -template< class T > -struct remove_cvref -{ - typedef std::remove_cv_t> type; -}; - -template -using remove_cvref_t = typename remove_cvref::type; - -//================================================================================================= -/** - * @brief Generic function traits. - * - * @tparam IsMember True if the function is a member function pointer. - * @tparam IsConst True if the function is const. - * @tparam R Return type of the function. - * @tparam Args Arguments types as variadic parameter pack. - */ -template -struct function_traits_base -{ - using result_type = R; - - using argument_types = std::tuple; - - static constexpr auto arity = sizeof...(Args); - - static constexpr auto is_member = IsMember; - - static constexpr auto is_const = IsConst; -}; - -template -struct function_traits_impl; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -#if _MSC_VER && _M_IX86 // Windows: WINAPI (a.k.a. __stdcall) function pointers (32bit only). -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; - -template -struct function_traits_impl : function_traits_base -{ -}; -#endif - -template -struct functor_traits_impl : function_traits_impl -{ -}; - -//================================================================================================= -/** - * @brief Traits class for callable objects (e.g. function pointers, lambdas) - * - * @tparam F Callable object. - */ -template -struct function_traits : std::conditional_t, - detail::functor_traits_impl, - detail::function_traits_impl> -{ -}; - -//================================================================================================= -/** - * @brief Deduces the argument type of a callble object or void in case it has no argument. - * - * @tparam I Argument index. - * @tparam F Callable object. - */ -template -struct function_argument_or_void -{ - using type = void; -}; - -template -struct function_argument_or_void::argument_types>>> -{ - using type = std::tuple_element_t::argument_types>; -}; - -template -using function_argument_or_void_t = typename function_argument_or_void::type; - -//================================================================================================= -/** - * @brief Deduces the return type of a callble object. - * - * @tparam F Callable object. - */ -template -using function_result_t = typename function_traits::result_type; - -/** - * @brief Deduces the argument type of a callble object. - * - * @tparam I Argument index. - * @tparam F Callable object. - */ -template -using function_argument_t = std::tuple_element_t::argument_types>; - -/** - * @brief Deduces the arguments type of a callble object. - * - * @tparam F Callable object. - */ -template -using function_arguments_t = typename function_traits::argument_types; - -/** - * @brief An integral constant expression that gives the number of arguments accepted by the callable object. - * - * @tparam F Callable object. - */ -template -static constexpr std::size_t function_arity_v = function_traits::arity; - -/** - * @brief An boolean constant expression that checks if the callable object is a member function. - * - * @tparam F Callable object. - */ -template -static constexpr bool function_is_member_v = function_traits::is_member; - -/** - * @brief An boolean constant expression that checks if the callable object is const. - * - * @tparam F Callable object. - */ -template -static constexpr bool function_is_const_v = function_traits::is_const; - -//================================================================================================= -/** - * @brief Detect if we T is a callable object. - * - * @tparam T Potentially callable object. - */ -template -struct is_callable -{ - static constexpr bool value = false; -}; - -template -struct is_callable> -{ - static constexpr bool value = true; -}; - -template -struct is_callable && std::is_function_v>>> -{ - static constexpr bool value = true; -}; - -template -struct is_callable>> -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_callable_v = is_callable::value; - -//================================================================================================= -/** - * @brief Detect if we T is a const member function pointer. - * - * @tparam T Potentially const member function pointer. - */ -template -struct is_const_member_function_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = true; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_function_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_const_member_function_pointer_v = is_const_member_function_pointer::value; - -//================================================================================================= -/** - * @brief Detect if T is a lua cfunction pointer. - * - * @tparam T Potentially lua cfunction pointer. - */ -template -struct is_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template <> -struct is_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_cfunction_pointer_v = is_cfunction_pointer::value; - -//================================================================================================= -/** - * @brief Detect if T is a member lua cfunction pointer. - * - * @tparam T Potentially member lua cfunction pointer. - */ -template -struct is_member_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_member_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -struct is_member_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_member_cfunction_pointer_v = is_member_cfunction_pointer::value; - -/** - * @brief Detect if T is a const member lua cfunction pointer. - * - * @tparam T Potentially const member lua cfunction pointer. - */ -template -struct is_const_member_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_cfunction_pointer -{ - static constexpr bool value = false; -}; - -template -struct is_const_member_cfunction_pointer -{ - static constexpr bool value = true; -}; - -template -inline static constexpr bool is_const_member_cfunction_pointer_v = is_const_member_cfunction_pointer::value; - -//================================================================================================= -/** - * @brief Detect if T is a member or non member lua cfunction pointer. - * - * @tparam T Potentially member or non member lua cfunction pointer. - */ -template -inline static constexpr bool is_any_cfunction_pointer_v = is_cfunction_pointer_v || is_member_cfunction_pointer_v; - -//================================================================================================= -/** - * @brief A constexpr check for proxy_member functions. - * - * @tparam T Type where the callable should be able to operate. - * @tparam F Callable object. - */ -template -inline static constexpr bool is_proxy_member_function_v = - !std::is_member_function_pointer_v && - std::is_same_v>>>; - -template -inline static constexpr bool is_const_proxy_function_v = - is_proxy_member_function_v && - std::is_const_v>>; - -//================================================================================================= -/** - * @brief An integral constant expression that gives the number of arguments excluding one type (usually used with lua_State*) accepted by the callable object. - * - * @tparam F Callable object. - */ -template -struct function_arity_excluding -{ -}; - -template < class... Ts, class ExclusionType> -struct function_arity_excluding, ExclusionType> - : std::integral_constant, ExclusionType> ? 0 : 1))> -{ -}; - -template -inline static constexpr std::size_t function_arity_excluding_v = function_arity_excluding, ExclusionType>::value; - -/** - * @brief An integral constant expression that gives the number of arguments excluding one type (usually used with lua_State*) accepted by the callable object. - * - * @tparam F Callable object. - */ -template -struct member_function_arity_excluding -{ -}; - -template -struct member_function_arity_excluding, ExclusionType, std::enable_if_t>> - : std::integral_constant, ExclusionType> ? 0 : 1))> -{ -}; - -template -struct member_function_arity_excluding, ExclusionType, std::enable_if_t>> - : std::integral_constant, ExclusionType> ? 0 : 1)) - 1> -{ -}; - -template -inline static constexpr std::size_t member_function_arity_excluding_v = member_function_arity_excluding, ExclusionType>::value; - -//================================================================================================= -/** - * @brief Detectors for const and non const functions in packs and counting them. - */ -template -static constexpr bool is_const_function = - detail::is_const_member_function_pointer_v || - (detail::function_arity_v > 0 && detail::is_const_proxy_function_v); - -template -inline static constexpr std::size_t const_functions_count = (0 + ... + (is_const_function ? 1 : 0)); - -template -inline static constexpr std::size_t non_const_functions_count = (0 + ... + (is_const_function ? 0 : 1)); - -//================================================================================================= -/** - * @brief Simple make_tuple alternative that doesn't decay the types. - * - * @tparam Types Argument types that will compose the tuple. - */ -template -constexpr auto tupleize(Types&&... types) -{ - return std::tuple(std::forward(types)...); -} - -//================================================================================================= -/** - * @brief Remove first type from tuple. - */ -template -struct remove_first_type -{ -}; - -template -struct remove_first_type> -{ - using type = std::tuple; -}; - -template -using remove_first_type_t = typename remove_first_type::type; - -} // namespace detail -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Invoke.h b/LuaBridge3/Source/LuaBridge/detail/Invoke.h deleted file mode 100644 index 50155e6..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Invoke.h +++ /dev/null @@ -1,228 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2021, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Errors.h" -#include "Stack.h" -#include "LuaRef.h" -#include "LuaException.h" - -#include -#include -#include -#include -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Result of a lua invocation. - */ -class LuaResult -{ -public: - /** - * @brief Get if the result was ok and didn't raise a lua error. - */ - explicit operator bool() const noexcept - { - return !m_ec; - } - - /** - * @brief Return if the invocation was ok and didn't raise a lua error. - */ - bool wasOk() const noexcept - { - return !m_ec; - } - - /** - * @brief Return if the invocation did raise a lua error. - */ - bool hasFailed() const noexcept - { - return !!m_ec; - } - - /** - * @brief Return the error code, if any. - * - * In case the invcation didn't raise any lua error, the value returned equals to a - * default constructed std::error_code. - */ - std::error_code errorCode() const noexcept - { - return m_ec; - } - - /** - * @brief Return the error message, if any. - */ - std::string errorMessage() const noexcept - { - if (std::holds_alternative(m_data)) - { - const auto& message = std::get(m_data); - return message.empty() ? m_ec.message() : message; - } - - return {}; - } - - /** - * @brief Return the number of return values. - */ - std::size_t size() const noexcept - { - if (std::holds_alternative>(m_data)) - return std::get>(m_data).size(); - - return 0; - } - - /** - * @brief Get a return value at a specific index. - */ - LuaRef operator[](std::size_t index) const - { - assert(m_ec == std::error_code()); - - if (std::holds_alternative>(m_data)) - { - const auto& values = std::get>(m_data); - - assert(index < values.size()); - return values[index]; - } - - return LuaRef(m_L); - } - -private: - template - friend LuaResult call(const LuaRef&, Args&&...); - - static LuaResult errorFromStack(lua_State* L, std::error_code ec) - { - auto errorString = lua_tostring(L, -1); - lua_pop(L, 1); - - return LuaResult(L, ec, errorString ? errorString : ec.message()); - } - - static LuaResult valuesFromStack(lua_State* L, int stackTop) - { - std::vector values; - - const int numReturnedValues = lua_gettop(L) - stackTop; - if (numReturnedValues > 0) - { - values.reserve(numReturnedValues); - - for (int index = numReturnedValues; index > 0; --index) - values.emplace_back(LuaRef::fromStack(L, -index)); - - lua_pop(L, numReturnedValues); - } - - return LuaResult(L, std::move(values)); - } - - LuaResult(lua_State* L, std::error_code ec, std::string_view errorString) - : m_L(L) - , m_ec(ec) - , m_data(std::string(errorString)) - { - } - - explicit LuaResult(lua_State* L, std::vector values) noexcept - : m_L(L) - , m_data(std::move(values)) - { - } - - lua_State* m_L = nullptr; - std::error_code m_ec; - std::variant, std::string> m_data; -}; - -//================================================================================================= -/** - * @brief Safely call Lua code. - * - * These overloads allow Lua code to be called throught lua_pcall. The return value is provided as - * a LuaResult which will hold the return values or an error if the call failed. - * - * If an error occurs, a LuaException is thrown or if exceptions are disabled the FunctionResult will - * contain a error code and evaluate false. - * - * @note The function might throw a LuaException if the application is compiled with exceptions on - * and they are enabled globally by calling `enableExceptions` in two cases: - * - one of the arguments passed cannot be pushed in the stack, for example a unregistered C++ class - * - the lua invaction calls the panic handler, which is converted to a C++ exception - * - * @return A result object. -*/ -template -LuaResult call(const LuaRef& object, Args&&... args) -{ - lua_State* L = object.state(); - const int stackTop = lua_gettop(L); - - object.push(); - - { - const auto [result, index] = detail::push_arguments(L, std::forward_as_tuple(args...)); - if (! result) - { - lua_pop(L, static_cast(index) + 1); - return LuaResult(L, result, result.message()); - } - } - - int code = lua_pcall(L, sizeof...(Args), LUA_MULTRET, 0); - if (code != LUABRIDGE_LUA_OK) - { - auto ec = makeErrorCode(ErrorCode::LuaFunctionCallFailed); - -#if LUABRIDGE_HAS_EXCEPTIONS - if (LuaException::areExceptionsEnabled()) - LuaException::raise(L, ec); -#else - return LuaResult::errorFromStack(L, ec); -#endif - } - - return LuaResult::valuesFromStack(L, stackTop); -} - -//============================================================================================= -/** - * @brief Wrapper for lua_pcall that throws if exceptions are enabled. - */ -inline int pcall(lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0) -{ - const int code = lua_pcall(L, nargs, nresults, msgh); - -#if LUABRIDGE_HAS_EXCEPTIONS - if (code != LUABRIDGE_LUA_OK && LuaException::areExceptionsEnabled()) - LuaException::raise(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed)); -#endif - - return code; -} - -//============================================================================================= -template -template -LuaResult LuaRefBase::operator()(Args&&... args) const -{ - return call(*this, std::forward(args)...); -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Iterator.h b/LuaBridge3/Source/LuaBridge/detail/Iterator.h deleted file mode 100644 index 8d90f58..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Iterator.h +++ /dev/null @@ -1,206 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2018, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "LuaRef.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Iterator class to allow table iteration. - * - * @see Range class. - */ -class Iterator -{ -public: - explicit Iterator(const LuaRef& table, bool isEnd = false) - : m_L(table.state()) - , m_table(table) - , m_key(table.state()) // m_key is nil - , m_value(table.state()) // m_value is nil - { - if (! isEnd) - { - next(); // get the first (key, value) pair from table - } - } - - /** - * @brief Return an associated Lua state. - * - * @return A Lua state. - */ - lua_State* state() const noexcept - { - return m_L; - } - - /** - * @brief Dereference the iterator. - * - * @return A key-value pair for a current table entry. - */ - std::pair operator*() const - { - return std::make_pair(m_key, m_value); - } - - /** - * @brief Return the value referred by the iterator. - * - * @return A value for the current table entry. - */ - LuaRef operator->() const - { - return m_value; - } - - /** - * @brief Compare two iterators. - * - * @param rhs Another iterator. - * - * @return True if iterators point to the same entry of the same table, false otherwise. - */ - bool operator!=(const Iterator& rhs) const - { - assert(m_L == rhs.m_L); - - return ! m_table.rawequal(rhs.m_table) || ! m_key.rawequal(rhs.m_key); - } - - /** - * @brief Move the iterator to the next table entry. - * - * @return This iterator. - */ - Iterator& operator++() - { - if (isNil()) - { - // if the iterator reaches the end, do nothing - return *this; - } - else - { - next(); - return *this; - } - } - - /** - * @brief Check if the iterator points after the last table entry. - * - * @return True if there are no more table entries to iterate, false otherwise. - */ - bool isNil() const noexcept - { - return m_key.isNil(); - } - - /** - * @brief Return the key for the current table entry. - * - * @return A reference to the entry key. - */ - LuaRef key() const - { - return m_key; - } - - /** - * @brief Return the key for the current table entry. - * - * @return A reference to the entry value. - */ - LuaRef value() const - { - return m_value; - } - -private: - // Don't use postfix increment, it is less efficient - Iterator operator++(int); - - void next() - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - { - m_key = LuaNil(); - m_value = LuaNil(); - return; - } -#endif - - m_table.push(); - m_key.push(); - - if (lua_next(m_L, -2)) - { - m_value.pop(); - m_key.pop(); - } - else - { - m_key = LuaNil(); - m_value = LuaNil(); - } - - lua_pop(m_L, 1); - } - - lua_State* m_L = nullptr; - LuaRef m_table; - LuaRef m_key; - LuaRef m_value; -}; - -//================================================================================================= -/** - * @brief Range class taking two table iterators. - */ -class Range -{ -public: - Range(const Iterator& begin, const Iterator& end) - : m_begin(begin) - , m_end(end) - { - } - - const Iterator& begin() const noexcept - { - return m_begin; - } - - const Iterator& end() const noexcept - { - return m_end; - } - -private: - Iterator m_begin; - Iterator m_end; -}; - -//================================================================================================= -/** - * @brief Return a range for the Lua table reference. - * - * @return A range suitable for range-based for statement. - */ -inline Range pairs(const LuaRef& table) -{ - return Range{ Iterator(table, false), Iterator(table, true) }; -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/LuaException.h b/LuaBridge3/Source/LuaBridge/detail/LuaException.h deleted file mode 100644 index 99541c0..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/LuaException.h +++ /dev/null @@ -1,187 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2021, Lucio Asnaghi -// Copyright 2012, Vinnie Falco -// Copyright 2008, Nigel Atkinson -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include "LuaHelpers.h" - -#include -#include -#include -#include - -namespace luabridge { - -//================================================================================================ -class LuaException : public std::exception -{ -public: - //============================================================================================= - /** - * @brief Construct a LuaException after a lua_pcall(). - * - * Assumes the error string is on top of the stack, but provides a generic error message otherwise. - */ - LuaException(lua_State* L, std::error_code code) - : m_L(L) - , m_code(code) - { - } - - ~LuaException() noexcept override - { - } - - //============================================================================================= - /** - * @brief Return the error message. - */ - const char* what() const noexcept override - { - return m_what.c_str(); - } - - //============================================================================================= - /** - * @brief Throw an exception or raises a luaerror when exceptions are disabled. - */ - static void raise(lua_State* L, std::error_code code) - { - assert(areExceptionsEnabled()); - -#if LUABRIDGE_HAS_EXCEPTIONS - throw LuaException(L, code, FromLua{}); -#else - unused(L, code); - - std::abort(); -#endif - } - - //============================================================================================= - /** - * @brief Check if exceptions are enabled. - */ - static bool areExceptionsEnabled() noexcept - { - return exceptionsEnabled(); - } - - /** - * @brief Initializes error handling. - * - * Subsequent Lua errors are translated to C++ exceptions, or logging and abort if exceptions are disabled. - */ - static void enableExceptions(lua_State* L) noexcept - { - exceptionsEnabled() = true; - -#if LUABRIDGE_HAS_EXCEPTIONS && LUABRIDGE_ON_LUAJIT - lua_pushlightuserdata(L, (void*)luajitWrapperCallback); - luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON); - lua_pop(L, 1); -#endif - -#if LUABRIDGE_ON_LUAU - auto callbacks = lua_callbacks(L); - callbacks->panic = +[](lua_State* L, int) { panicHandlerCallback(L); }; -#else - lua_atpanic(L, panicHandlerCallback); -#endif - } - - //============================================================================================= - /** - * @brief Retrieve the lua_State associated with the exception. - * - * @return A Lua state. - */ - lua_State* state() const { return m_L; } - -private: - struct FromLua {}; - - LuaException(lua_State* L, std::error_code code, FromLua) - : m_L(L) - , m_code(code) - { - whatFromStack(); - } - - void whatFromStack() - { - std::stringstream ss; - - const char* errorText = nullptr; - - if (lua_gettop(m_L) > 0) - { - errorText = lua_tostring(m_L, -1); - lua_pop(m_L, 1); - } - - ss << (errorText ? errorText : "Unknown error") << " (code=" << m_code.message() << ")"; - - m_what = std::move(ss).str(); - } - - static int panicHandlerCallback(lua_State* L) - { -#if LUABRIDGE_HAS_EXCEPTIONS - throw LuaException(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed), FromLua{}); -#else - unused(L); - - std::abort(); -#endif - } - -#if LUABRIDGE_HAS_EXCEPTIONS && LUABRIDGE_ON_LUAJIT - static int luajitWrapperCallback(lua_State* L, lua_CFunction f) - { - try - { - return f(L); - } - catch (const std::exception& e) - { - lua_pushstring(L, e.what()); - return lua_error_x(L); - } - } -#endif - - static bool& exceptionsEnabled() - { - static bool areExceptionsEnabled = false; - return areExceptionsEnabled; - } - - lua_State* m_L = nullptr; - std::error_code m_code; - std::string m_what; -}; - -//================================================================================================= -/** - * @brief Initializes error handling using C++ exceptions. - * - * Subsequent Lua errors are translated to C++ exceptions. It aborts the application if called when no exceptions. - */ -inline void enableExceptions(lua_State* L) noexcept -{ -#if LUABRIDGE_HAS_EXCEPTIONS - LuaException::enableExceptions(L); -#else - unused(L); - - assert(false); // Never call this function when exceptions are not enabled. -#endif -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/LuaHelpers.h b/LuaBridge3/Source/LuaBridge/detail/LuaHelpers.h deleted file mode 100644 index 6df5ef3..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/LuaHelpers.h +++ /dev/null @@ -1,581 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include -#include -#include -#include -#include -#include - -namespace luabridge { - -/** - * @brief Helper for unused vars. - */ -template -constexpr void unused(Args&&...) -{ -} - -// These functions and defines are for Luau. -#if LUABRIDGE_ON_LUAU -inline int luaL_ref(lua_State* L, int idx) -{ - assert(idx == LUA_REGISTRYINDEX); - - const int ref = lua_ref(L, -1); - - lua_pop(L, 1); - - return ref; -} - -inline void luaL_unref(lua_State* L, int idx, int ref) -{ - unused(idx); - - lua_unref(L, ref); -} - -template -inline void* lua_newuserdata_x(lua_State* L, size_t sz) -{ - return lua_newuserdatadtor(L, sz, [](void* x) - { - T* object = static_cast(x); - object->~T(); - }); -} - -inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn) -{ - lua_pushcfunction(L, fn, ""); -} - -inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, int n) -{ - lua_pushcclosure(L, fn, "", n); -} - -inline int lua_error_x(lua_State* L) -{ - lua_error(L); - return 0; -} - -inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar) -{ - return lua_getinfo(L, level, what, ar); -} - -#else -using ::luaL_ref; -using ::luaL_unref; - -template -inline void* lua_newuserdata_x(lua_State* L, size_t sz) -{ - return lua_newuserdata(L, sz); -} - -inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn) -{ - lua_pushcfunction(L, fn); -} - -inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, int n) -{ - lua_pushcclosure(L, fn, n); -} - -inline int lua_error_x(lua_State* L) -{ - return lua_error(L); -} - -inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar) -{ - lua_getstack(L, level, ar); - return lua_getinfo(L, what, ar); -} - -#endif // LUABRIDGE_ON_LUAU - -// These are for Lua versions prior to 5.3.0. -#if LUA_VERSION_NUM < 503 -inline lua_Number to_numberx(lua_State* L, int idx, int* isnum) -{ - lua_Number n = lua_tonumber(L, idx); - - if (isnum) - *isnum = (n != 0 || lua_isnumber(L, idx)); - - return n; -} - -inline lua_Integer to_integerx(lua_State* L, int idx, int* isnum) -{ - int ok = 0; - lua_Number n = to_numberx(L, idx, &ok); - - if (ok) - { - const auto int_n = static_cast(n); - if (n == static_cast(int_n)) - { - if (isnum) - *isnum = 1; - - return int_n; - } - } - - if (isnum) - *isnum = 0; - - return 0; -} - -#endif // LUA_VERSION_NUM < 503 - -// These are for Lua versions prior to 5.2.0. -#if LUA_VERSION_NUM < 502 -using lua_Unsigned = std::make_unsigned_t; - -#if ! LUABRIDGE_ON_LUAU -inline int lua_absindex(lua_State* L, int idx) -{ - if (idx > LUA_REGISTRYINDEX && idx < 0) - return lua_gettop(L) + idx + 1; - else - return idx; -} -#endif - -inline void lua_rawgetp(lua_State* L, int idx, const void* p) -{ - idx = lua_absindex(L, idx); - luaL_checkstack(L, 1, "not enough stack slots"); - lua_pushlightuserdata(L, const_cast(p)); - lua_rawget(L, idx); -} - -inline void lua_rawsetp(lua_State* L, int idx, const void* p) -{ - idx = lua_absindex(L, idx); - luaL_checkstack(L, 1, "not enough stack slots"); - lua_pushlightuserdata(L, const_cast(p)); - lua_insert(L, -2); - lua_rawset(L, idx); -} - -#define LUA_OPEQ 1 -#define LUA_OPLT 2 -#define LUA_OPLE 3 - -inline int lua_compare(lua_State* L, int idx1, int idx2, int op) -{ - switch (op) - { - case LUA_OPEQ: - return lua_equal(L, idx1, idx2); - - case LUA_OPLT: - return lua_lessthan(L, idx1, idx2); - - case LUA_OPLE: - return lua_equal(L, idx1, idx2) || lua_lessthan(L, idx1, idx2); - - default: - return 0; - } -} - -inline int get_length(lua_State* L, int idx) -{ - return static_cast(lua_objlen(L, idx)); -} - -#else // LUA_VERSION_NUM >= 502 -inline int get_length(lua_State* L, int idx) -{ - lua_len(L, idx); - const int len = static_cast(luaL_checknumber(L, -1)); - lua_pop(L, 1); - return len; -} - -#endif // LUA_VERSION_NUM < 502 - -#ifndef LUA_OK -#define LUABRIDGE_LUA_OK 0 -#else -#define LUABRIDGE_LUA_OK LUA_OK -#endif - -/** - * @brief Helper to throw or return an error code. - */ -template -std::error_code throw_or_error_code(ErrorType error) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - throw T(makeErrorCode(error).message().c_str()); -#else - return makeErrorCode(error); -#endif -} - -template -std::error_code throw_or_error_code(lua_State* L, ErrorType error) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - throw T(L, makeErrorCode(error)); -#else - return unused(L), makeErrorCode(error); -#endif -} - -/** - * @brief Helper to throw or assert. - */ -template -void throw_or_assert(Args&&... args) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - throw T(std::forward(args)...); -#else - unused(std::forward(args)...); - assert(false); -#endif -} - -/** - * @brief Helper to set unsigned. - */ -template -void pushunsigned(lua_State* L, T value) -{ - static_assert(std::is_unsigned_v); - - lua_pushinteger(L, static_cast(value)); -} - -/** - * @brief Helper to convert to integer. - */ -inline lua_Number tonumber(lua_State* L, int idx, int* isnum) -{ -#if ! LUABRIDGE_ON_LUAU && LUA_VERSION_NUM > 502 - return lua_tonumberx(L, idx, isnum); -#else - return to_numberx(L, idx, isnum); -#endif -} - -/** - * @brief Helper to convert to integer. - */ -inline lua_Integer tointeger(lua_State* L, int idx, int* isnum) -{ -#if ! LUABRIDGE_ON_LUAU && LUA_VERSION_NUM > 502 - return lua_tointegerx(L, idx, isnum); -#else - return to_integerx(L, idx, isnum); -#endif -} - -/** - * @brief Register main thread, only supported on 5.1. - */ -inline constexpr char main_thread_name[] = "__luabridge_main_thread"; - -inline void register_main_thread(lua_State* threadL) -{ -#if LUA_VERSION_NUM < 502 - if (threadL == nullptr) - lua_pushnil(threadL); - else - lua_pushthread(threadL); - - lua_setglobal(threadL, main_thread_name); -#else - unused(threadL); -#endif -} - -/** - * @brief Get main thread, not supported on 5.1. - */ -inline lua_State* main_thread(lua_State* threadL) -{ -#if LUA_VERSION_NUM < 502 - lua_getglobal(threadL, main_thread_name); - if (lua_isthread(threadL, -1)) - { - auto L = lua_tothread(threadL, -1); - lua_pop(threadL, 1); - return L; - } - assert(false); // Have you forgot to call luabridge::registerMainThread ? - lua_pop(threadL, 1); - return threadL; -#else - lua_rawgeti(threadL, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); - lua_State* L = lua_tothread(threadL, -1); - lua_pop(threadL, 1); - return L; -#endif -} - -/** - * @brief Get a table value, bypassing metamethods. - */ -inline void rawgetfield(lua_State* L, int index, char const* key) -{ - assert(lua_istable(L, index)); - index = lua_absindex(L, index); - lua_pushstring(L, key); - lua_rawget(L, index); -} - -/** - * @brief Set a table value, bypassing metamethods. - */ -inline void rawsetfield(lua_State* L, int index, char const* key) -{ - assert(lua_istable(L, index)); - index = lua_absindex(L, index); - lua_pushstring(L, key); - lua_insert(L, -2); - lua_rawset(L, index); -} - -/** - * @brief Returns true if the value is a full userdata (not light). - */ -[[nodiscard]] inline bool isfulluserdata(lua_State* L, int index) -{ - return lua_isuserdata(L, index) && !lua_islightuserdata(L, index); -} - -/** - * @brief Test lua_State objects for global equality. - * - * This can determine if two different lua_State objects really point - * to the same global state, such as when using coroutines. - * - * @note This is used for assertions. - */ -[[nodiscard]] inline bool equalstates(lua_State* L1, lua_State* L2) -{ - return lua_topointer(L1, LUA_REGISTRYINDEX) == lua_topointer(L2, LUA_REGISTRYINDEX); -} - -/** - * @brief Return the size of lua table, even if not a sequence { 1=x, 2=y, 3=... }. - */ -[[nodiscard]] inline int table_length(lua_State* L, int index) -{ - assert(lua_istable(L, index)); - - int items_count = 0; - - lua_pushnil(L); - while (lua_next(L, index) != 0) - { - ++items_count; - - lua_pop(L, 1); - } - - return items_count; -} - -/** - * @brief Return an aligned pointer of type T. - */ -template -[[nodiscard]] T* align(void* ptr) noexcept -{ - const auto address = reinterpret_cast(ptr); - - const auto offset = address % alignof(T); - const auto aligned_address = (offset == 0) ? address : (address + alignof(T) - offset); - - return reinterpret_cast(aligned_address); -} - -/** - * @brief Return the space needed to align the type T on an unaligned address. - */ -template -[[nodiscard]] constexpr size_t maximum_space_needed_to_align() noexcept -{ - return sizeof(T) + alignof(T) - 1; -} - -/** - * @brief Deallocate lua userdata taking into account alignment. - */ -template -int lua_deleteuserdata_aligned(lua_State* L) -{ - assert(isfulluserdata(L, 1)); - - T* aligned = align(lua_touserdata(L, 1)); - aligned->~T(); - - return 0; -} - -/** - * @brief Allocate lua userdata taking into account alignment. - * - * Using this instead of lua_newuserdata directly prevents alignment warnings on 64bits platforms. - */ -template -void* lua_newuserdata_aligned(lua_State* L, Args&&... args) -{ -#if LUABRIDGE_ON_LUAU - void* pointer = lua_newuserdatadtor(L, maximum_space_needed_to_align(), [](void* x) - { - T* aligned = align(x); - aligned->~T(); - }); -#else - void* pointer = lua_newuserdata_x(L, maximum_space_needed_to_align()); - - lua_newtable(L); - lua_pushcfunction_x(L, &lua_deleteuserdata_aligned); - rawsetfield(L, -2, "__gc"); - lua_setmetatable(L, -2); -#endif - - T* aligned = align(pointer); - - new (aligned) T(std::forward(args)...); - - return pointer; -} - -/** - * @brief Safe error able to walk backwards for error reporting correctly. - */ -inline int raise_lua_error(lua_State *L, const char *fmt, ...) -{ - va_list argp; - va_start(argp, fmt); - - bool pushed_error = false; - for (int level = 2; level > 0; --level) - { - lua_Debug ar; - -#if LUABRIDGE_ON_LUAU - if (lua_getinfo(L, level, "sl", &ar) == 0) - continue; -#else - if (lua_getstack(L, level, &ar) == 0 || lua_getinfo(L, "Sl", &ar) == 0) - continue; -#endif - - if (ar.currentline <= 0) - continue; - - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); - pushed_error = true; - - break; - } - - if (! pushed_error) - lua_pushliteral(L, ""); - - lua_pushvfstring(L, fmt, argp); - va_end(argp); - lua_concat(L, 2); - - return lua_error_x(L); -} - -/** - * @brief Checks if the value on the stack is a number type and can fit into the corresponding c++ integral type.. - */ -template -constexpr bool is_integral_representable_by(T value) -{ - constexpr bool same_signedness = (std::is_unsigned_v && std::is_unsigned_v) - || (!std::is_unsigned_v && !std::is_unsigned_v); - - if constexpr (sizeof(T) == sizeof(U)) - { - if constexpr (same_signedness) - return true; - - if constexpr (std::is_unsigned_v) - return value <= static_cast(std::numeric_limits::max()); - - return value >= static_cast(std::numeric_limits::min()) - && static_cast(value) <= std::numeric_limits::max(); - } - - if constexpr (sizeof(T) < sizeof(U)) - { - return static_cast(value) >= std::numeric_limits::min() - && static_cast(value) <= std::numeric_limits::max(); - } - - if constexpr (std::is_unsigned_v) - return value <= static_cast(std::numeric_limits::max()); - - return value >= static_cast(std::numeric_limits::min()) - && value <= static_cast(std::numeric_limits::max()); -} - -template -bool is_integral_representable_by(lua_State* L, int index) -{ - int isValid = 0; - - const auto value = tointeger(L, index, &isValid); - - return isValid ? is_integral_representable_by(value) : false; -} - -/** - * @brief Checks if the value on the stack is a number type and can fit into the corresponding c++ numerical type.. - */ -template -constexpr bool is_floating_point_representable_by(T value) -{ - if constexpr (sizeof(T) == sizeof(U)) - return true; - - if constexpr (sizeof(T) < sizeof(U)) - return static_cast(value) >= -std::numeric_limits::max() - && static_cast(value) <= std::numeric_limits::max(); - - return value >= static_cast(-std::numeric_limits::max()) - && value <= static_cast(std::numeric_limits::max()); -} - -template -bool is_floating_point_representable_by(lua_State* L, int index) -{ - int isValid = 0; - - const auto value = tonumber(L, index, &isValid); - - return isValid ? is_floating_point_representable_by(value) : false; -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/LuaRef.h b/LuaBridge3/Source/LuaBridge/detail/LuaRef.h deleted file mode 100644 index be25301..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/LuaRef.h +++ /dev/null @@ -1,1405 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, George Tokmaji -// Copyright 2018, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2008, Nigel Atkinson -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Errors.h" -#include "Expected.h" -#include "Stack.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace luabridge { - -class LuaResult; - -//================================================================================================= -/** - * @brief Type tag for representing LUA_TNIL. - * - * Construct one of these using `LuaNil ()` to represent a Lua nil. This is faster than creating a reference in the registry to nil. - * Example: - * - * @code - * LuaRef t (LuaRef::createTable (L)); - * ... - * t ["k"] = LuaNil (); // assign nil - * @endcode - */ -struct LuaNil -{ -}; - -/** - * @brief Stack specialization for LuaNil. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const LuaNil&) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushnil(L); - return {}; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TNIL; - } -}; - -//================================================================================================= -/** - * @brief Base class for Lua variables and table item reference classes. - */ -template -class LuaRefBase -{ -protected: - friend struct Stack; - - //============================================================================================= - /** - * @brief Type tag for stack construction. - */ - struct FromStack - { - }; - - LuaRefBase(lua_State* L) - : m_L(L) - { - } - - //============================================================================================= - /** - * @brief Create a reference to this reference. - * - * @returns An index in the Lua registry. - */ - int createRef() const - { - impl().push(); - - return luaL_ref(m_L, LUA_REGISTRYINDEX); - } - -public: - //============================================================================================= - /** - * @brief Convert to a string using lua_tostring function. - * - * @returns A string representation of the referred Lua value. - */ - std::string tostring() const - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - return {}; -#endif - - const StackRestore stackRestore(m_L); - - lua_getglobal(m_L, "tostring"); - - impl().push(); - - lua_call(m_L, 1, 1); - - const char* str = lua_tostring(m_L, -1); - return str != nullptr ? str : ""; - } - - //============================================================================================= - /** - * @brief Print a text description of the value to a stream. - * - * This is used for diagnostics. - * - * @param os An output stream. - */ - void print(std::ostream& os) const - { - switch (type()) - { - case LUA_TNONE: - case LUA_TNIL: - os << "nil"; - break; - - case LUA_TNUMBER: - os << unsafe_cast(); - break; - - case LUA_TBOOLEAN: - os << (unsafe_cast() ? "true" : "false"); - break; - - case LUA_TSTRING: - os << '"' << unsafe_cast() << '"'; - break; - - case LUA_TTABLE: - case LUA_TFUNCTION: - case LUA_TTHREAD: - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - os << tostring(); - break; - - default: - os << "unknown"; - break; - } - } - - //============================================================================================= - /** - * @brief Insert a Lua value or table item reference to a stream. - * - * @param os An output stream. - * @param ref A Lua reference. - * - * @returns The output stream. - */ - friend std::ostream& operator<<(std::ostream& os, const LuaRefBase& ref) - { - ref.print(os); - return os; - } - - //============================================================================================= - /** - * @brief Retrieve the lua_State associated with the reference. - * - * @returns A Lua state. - */ - lua_State* state() const - { - return m_L; - } - - //============================================================================================= - /** - * @brief Place the object onto the Lua stack. - * - * @param L A Lua state. - */ - void push(lua_State* L) const - { - assert(equalstates(L, m_L)); - (void) L; - - impl().push(); - } - - //============================================================================================= - /** - * @brief Pop the top of Lua stack and assign it to the reference. - * - * @param L A Lua state. - */ - void pop(lua_State* L) - { - assert(equalstates(L, m_L)); - (void) L; - - impl().pop(); - } - - //============================================================================================= - /** - * @brief Return the Lua type of the referred value. - * - * This invokes lua_type(). - * - * @returns The type of the referred value. - * - * @see lua_type() - */ - int type() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - const int refType = lua_type(m_L, -1); - - return refType; - } - - /** - * @brief Indicate whether it is a nil reference. - * - * @returns True if this is a nil reference, false otherwise. - */ - bool isNil() const { return type() == LUA_TNIL; } - - /** - * @brief Indicate whether it is a reference to a boolean. - * - * @returns True if it is a reference to a boolean, false otherwise. - */ - bool isBool() const { return type() == LUA_TBOOLEAN; } - - /** - * @brief Indicate whether it is a reference to a number. - * - * @returns True if it is a reference to a number, false otherwise. - */ - bool isNumber() const { return type() == LUA_TNUMBER; } - - /** - * @brief Indicate whether it is a reference to a string. - * - * @returns True if it is a reference to a string, false otherwise. - */ - bool isString() const { return type() == LUA_TSTRING; } - - /** - * @brief Indicate whether it is a reference to a table. - * - * @returns True if it is a reference to a table, false otherwise. - */ - bool isTable() const { return type() == LUA_TTABLE; } - - /** - * @brief Indicate whether it is a reference to a function. - * - * @returns True if it is a reference to a function, false otherwise. - */ - bool isFunction() const { return type() == LUA_TFUNCTION; } - - /** - * @brief Indicate whether it is a reference to a full userdata. - * - * @returns True if it is a reference to a full userdata, false otherwise. - */ - bool isUserdata() const { return type() == LUA_TUSERDATA; } - - /** - * @brief Indicate whether it is a reference to a lua thread (coroutine). - * - * @returns True if it is a reference to a lua thread, false otherwise. - */ - bool isThread() const { return type() == LUA_TTHREAD; } - - /** - * @brief Indicate whether it is a reference to a light userdata. - * - * @returns True if it is a reference to a light userdata, false otherwise. - */ - bool isLightUserdata() const { return type() == LUA_TLIGHTUSERDATA; } - - /** - * @brief Indicate whether it is a callable. - * - * @returns True if it is a callable, false otherwise. - */ - bool isCallable() const - { - if (isFunction()) - return true; - - auto metatable = getMetatable(); - return metatable.isTable() && metatable["__call"].isFunction(); - } - - //============================================================================================= - /** - * @brief Perform a safe explicit conversion to the type T. - * - * @returns An expected holding a value of the type T converted from this reference or an error code. - */ - template - TypeResult cast() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - auto result = Stack::get(m_L, -1); - if (! result) - return result.error(); - - return static_cast(*result); - } - else - { - return Stack::get(m_L, -1); - } - } - - /** - * @brief Perform an unsafe explicit conversion to the type T. - * - * @returns A value of the type T converted from this reference. - */ - template - T unsafe_cast() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - return static_cast(*Stack::get(m_L, -1)); - } - else - { - return *Stack::get(m_L, -1); - } - } - - //============================================================================================= - /** - * @brief Indicate if this reference is convertible to the type T. - * - * @returns True if the referred value is convertible to the type T, false otherwise. - */ - template - bool isInstance() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - return Stack::isInstance(m_L, -1); - } - else - { - return Stack::isInstance(m_L, -1); - } - } - - //============================================================================================= - /** - * @brief Type cast operator. - * - * This operator calls cast and always dereference the returned expected instance, resulting in exceptions being thrown if the - * exceptions are enabled, or otherwise we'll enter the UB land (and a likely crash down the line). - * - * @returns A value of the type T converted from this reference. - */ - template - operator T() const - { - return cast().value(); - } - - //============================================================================================= - /** - * @brief Get the metatable for the LuaRef. - * - * @returns A LuaRef holding the metatable of the lua object. - */ - LuaRef getMetatable() const - { - if (isNil()) - return LuaRef(m_L); - - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! lua_getmetatable(m_L, -1)) - return LuaRef(m_L); - - return LuaRef::fromStack(m_L); - } - - //============================================================================================= - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This invokes metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is equal to the specified one. - */ - template - bool operator==(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - return lua_compare(m_L, -2, -1, LUA_OPEQ) == 1; - } - - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This invokes metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is not equal to the specified one. - */ - template - bool operator!=(const T& rhs) const - { - return !(*this == rhs); - } - - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This invokes metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is less than the specified one. - */ - template - bool operator<(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType < rhsType; - - return lua_compare(m_L, -2, -1, LUA_OPLT) == 1; - } - - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This invokes metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is less than or equal to the specified one. - */ - template - bool operator<=(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType <= rhsType; - - return lua_compare(m_L, -2, -1, LUA_OPLE) == 1; - } - - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This invokes metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is greater than the specified one. - */ - template - bool operator>(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType > rhsType; - - return lua_compare(m_L, -1, -2, LUA_OPLT) == 1; - } - - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This invokes metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is greater than or equal to the specified one. - */ - template - bool operator>=(const T& rhs) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, rhs)) - return false; - - const int lhsType = lua_type(m_L, -2); - const int rhsType = lua_type(m_L, -1); - if (lhsType != rhsType) - return lhsType >= rhsType; - - return lua_compare(m_L, -1, -2, LUA_OPLE) == 1; - } - - /** - * @brief Compare this reference with a specified value using lua_compare(). - * - * This does not invoke metamethods. - * - * @param rhs A value to compare with. - * - * @returns True if the referred value is equal to the specified one. - */ - template - bool rawequal(const T& v) const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - if (! Stack::push(m_L, v)) - return false; - - return lua_rawequal(m_L, -1, -2) == 1; - } - - //============================================================================================= - /** - * @brief Return the length of a referred array. - * - * This is identical to applying the Lua # operator. - * - * @returns The length of the referred array. - */ - int length() const - { - const StackRestore stackRestore(m_L); - - impl().push(); - - return get_length(m_L, -1); - } - - //============================================================================================= - /** - * @brief Call Lua code. - * - * The return value is provided as a LuaRef (which may be LUA_REFNIL). - * - * If an error occurs, a LuaException is thrown (only if exceptions are enabled). - * - * @returns A result of the call. - */ - template - LuaResult operator()(Args&&... args) const; - -protected: - lua_State* m_L = nullptr; - -private: - const Impl& impl() const { return static_cast(*this); } - - Impl& impl() { return static_cast(*this); } -}; - -//================================================================================================= -/** - * @brief Lightweight reference to a Lua object. - * - * The reference is maintained for the lifetime of the C++ object. - */ -class LuaRef : public LuaRefBase -{ - //============================================================================================= - /** - * @brief A proxy for representing table values. - */ - class TableItem : public LuaRefBase - { - friend class LuaRef; - - public: - //========================================================================================= - /** - * @brief Construct a TableItem from a table value. - * - * The table is in the registry, and the key is at the top of the stack. - * The key is popped off the stack. - * - * @param L A lua state. - * @param tableRef The index of a table in the Lua registry. - */ - TableItem(lua_State* L, int tableRef) - : LuaRefBase(L) - , m_keyRef(luaL_ref(L, LUA_REGISTRYINDEX)) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - luaL_checkstack(m_L, 1, detail::error_lua_stack_overflow); -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, tableRef); - m_tableRef = luaL_ref(L, LUA_REGISTRYINDEX); - } - - //========================================================================================= - /** - * @brief Create a TableItem via copy constructor. - * - * It is best to avoid code paths that invoke this, because it creates an extra temporary Lua reference. Typically this is done by - * passing the TableItem parameter as a `const` reference. - * - * @param other Another Lua table item reference. - */ - TableItem(const TableItem& other) - : LuaRefBase(other.m_L) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 1)) - return; -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, other.m_tableRef); - m_tableRef = luaL_ref(m_L, LUA_REGISTRYINDEX); - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, other.m_keyRef); - m_keyRef = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - - //========================================================================================= - /** - * @brief Destroy the proxy. - * - * This does not destroy the table value. - */ - ~TableItem() - { - if (m_keyRef != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_keyRef); - - if (m_tableRef != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_tableRef); - } - - //========================================================================================= - /** - * @brief Assign a new value to this table key. - * - * This may invoke metamethods. - * - * @tparam T The type of a value to assing. - * - * @param v A value to assign. - * - * @returns This reference. - */ - template - TableItem& operator=(const T& v) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - return *this; -#endif - - const StackRestore stackRestore(m_L); - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_tableRef); - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_keyRef); - - if (! Stack::push(m_L, v)) - return *this; - - lua_settable(m_L, -3); - return *this; - } - - //========================================================================================= - /** - * @brief Assign a new value to this table key. - * - * The assignment is raw, no metamethods are invoked. - * - * @tparam T The type of a value to assing. - * - * @param v A value to assign. - * - * @returns This reference. - */ - template - TableItem& rawset(const T& v) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 2)) - return *this; -#endif - - const StackRestore stackRestore(m_L); - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_tableRef); - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_keyRef); - - if (! Stack::push(m_L, v)) - return *this; - - lua_rawset(m_L, -3); - return *this; - } - - //========================================================================================= - /** - * @brief Push the value onto the Lua stack. - */ - using LuaRefBase::push; - - void push() const - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 3)) - return; -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_tableRef); - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_keyRef); - lua_gettable(m_L, -2); - lua_remove(m_L, -2); // remove the table - } - - //========================================================================================= - /** - * @brief Access a table value using a key. - * - * This invokes metamethods. - * - * @tparam T The type of a key. - * - * @param key A key value. - * - * @returns A Lua table item reference. - */ - template - TableItem operator[](const T& key) const - { - return LuaRef(*this)[key]; - } - - //========================================================================================= - /** - * @brief Access a table value using a key. - * - * The operation is raw, metamethods are not invoked. The result is passed by value and may not be modified. - * - * @tparam T The type of a key. - * - * @param key A key value. - * - * @returns A Lua value reference. - */ - template - LuaRef rawget(const T& key) const - { - return LuaRef(*this).rawget(key); - } - - private: - int m_tableRef = LUA_NOREF; - int m_keyRef = LUA_NOREF; - }; - - friend struct Stack; - friend struct Stack; - - //========================================================================================= - /** - * @brief Create a reference to an object at the top of the Lua stack and pop it. - * - * This constructor is private and not invoked directly. Instead, use the `fromStack` function. - * - * @param L A Lua state. - * - * @note The object is popped. - */ - LuaRef(lua_State* L, FromStack) - : LuaRefBase(L) - , m_ref(luaL_ref(m_L, LUA_REGISTRYINDEX)) - { - } - - //========================================================================================= - /** - * @brief Create a reference to an object on the Lua stack. - * - * This constructor is private and not invoked directly. Instead, use the `fromStack` function. - * - * @param L A Lua state. - * - * @param index The index of the value on the Lua stack. - * - * @note The object is not popped. - */ - LuaRef(lua_State* L, int index, FromStack) - : LuaRefBase(L) - , m_ref(LUA_NOREF) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 1)) - return; -#endif - - lua_pushvalue(m_L, index); - m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - -public: - //============================================================================================= - /** - * @brief Create an invalid reference that will be treated as nil. - * - * The Lua reference may be assigned later. - * - * @param L A Lua state. - */ - LuaRef(lua_State* L) - : LuaRefBase(L) - , m_ref(LUA_NOREF) - { - } - - //============================================================================================= - /** - * @brief Push a value onto a Lua stack and return a reference to it. - * - * @param L A Lua state. - * @param v A value to push. - */ - template - LuaRef(lua_State* L, const T& v) - : LuaRefBase(L) - , m_ref(LUA_NOREF) - { - if (! Stack::push(m_L, v)) - return; - - m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - - //============================================================================================= - /** - * @brief Create a reference to a table item. - * - * @param v A table item reference. - */ - LuaRef(const TableItem& v) - : LuaRefBase(v.state()) - , m_ref(v.createRef()) - { - } - - //============================================================================================= - /** - * @brief Create a new reference to an existing Lua value. - * - * @param other An existing reference. - */ - LuaRef(const LuaRef& other) - : LuaRefBase(other.m_L) - , m_ref(other.createRef()) - { - } - - //============================================================================================= - /** - * @brief Move a reference to an existing Lua value. - * - * @param other An existing reference. - */ - LuaRef(LuaRef&& other) - : LuaRefBase(other.m_L) - , m_ref(std::exchange(other.m_ref, LUA_NOREF)) - { - } - - //============================================================================================= - /** - * @brief Destroy a reference. - * - * The corresponding Lua registry reference will be released. - * - * @note If the state refers to a thread, it is the responsibility of the caller to ensure that the thread still exists when the LuaRef is destroyed. - */ - ~LuaRef() - { - if (m_ref != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref); - } - - //============================================================================================= - /** - * @brief Return a reference to a top Lua stack item. - * - * The stack item is not popped. - * - * @param L A Lua state. - * - * @returns A reference to a value on the top of a Lua stack. - */ - static LuaRef fromStack(lua_State* L) - { - return LuaRef(L, FromStack()); - } - - //============================================================================================= - /** - * @brief Return a reference to a Lua stack item with a specified index. - * - * The stack item is not removed. - * - * @param L A Lua state. - * @param index An index in the Lua stack. - * - * @returns A reference to a value in a Lua stack. - */ - static LuaRef fromStack(lua_State* L, int index) - { - return LuaRef(L, index, FromStack()); - } - - //============================================================================================= - /** - * @brief Create a new empty table on the top of a Lua stack and return a reference to it. - * - * @param L A Lua state. - * - * @returns A reference to the newly created table. - * - * @see luabridge::newTable() - */ - static LuaRef newTable(lua_State* L) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return { L }; -#endif - - lua_newtable(L); - return LuaRef(L, FromStack()); - } - - //============================================================================================= - /** - * @brief Return a reference to a named global Lua variable. - * - * @param L A Lua state. - * @param name The name of a global variable. - * - * @returns A reference to the Lua variable. - * - * @see luabridge::getGlobal() - */ - static LuaRef getGlobal(lua_State* L, const char* name) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return { L }; -#endif - - lua_getglobal(L, name); - return LuaRef(L, FromStack()); - } - - //============================================================================================= - /** - * @brief Indicate whether it is an invalid reference. - * - * @returns True if this is an invalid reference, false otherwise. - */ - bool isValid() const { return m_ref != LUA_NOREF; } - - //============================================================================================= - /** - * @brief Assign another LuaRef to this LuaRef. - * - * @param rhs A reference to assign from. - * - * @returns This reference. - */ - LuaRef& operator=(const LuaRef& rhs) - { - LuaRef ref(rhs); - swap(ref); - return *this; - } - - //============================================================================================= - /** - * @brief Move assign another LuaRef to this LuaRef. - * - * @param rhs A reference to assign from. - * - * @returns This reference. - */ - LuaRef& operator=(LuaRef&& rhs) - { - if (m_ref != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref); - - m_L = rhs.m_L; - m_ref = std::exchange(rhs.m_ref, LUA_NOREF); - - return *this; - } - - //============================================================================================= - /** - * @brief Assign a table item reference. - * - * @param rhs A table item reference. - * - * @returns This reference. - */ - LuaRef& operator=(const LuaRef::TableItem& rhs) - { - LuaRef ref(rhs); - swap(ref); - return *this; - } - - //============================================================================================= - /** - * @brief Assign nil to this reference. - * - * @returns This reference. - */ - LuaRef& operator=(const LuaNil&) - { - LuaRef ref(m_L); - swap(ref); - return *this; - } - - //============================================================================================= - /** - * @brief Assign a different value to this reference. - * - * @param rhs A value to assign. - * - * @returns This reference. - */ - template - LuaRef& operator=(const T& rhs) - { - LuaRef ref(m_L, rhs); - swap(ref); - return *this; - } - - //============================================================================================= - /** - * @brief Place the object onto the Lua stack. - */ - using LuaRefBase::push; - - void push() const - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(m_L, 1)) - return; -#endif - - lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_ref); - } - - //============================================================================================= - /** - * @brief Pop the top of Lua stack and assign the ref to m_ref - */ - void pop() - { - if (m_ref != LUA_NOREF) - luaL_unref(m_L, LUA_REGISTRYINDEX, m_ref); - - m_ref = luaL_ref(m_L, LUA_REGISTRYINDEX); - } - - //============================================================================================= - /** - * @brief Access a table value using a key. - * - * This invokes metamethods. - * - * @param key A key in the table. - * - * @returns A reference to the table item. - */ - template - TableItem operator[](const T& key) const - { - if (! Stack::push(m_L, key)) - return TableItem(m_L, m_ref); - - return TableItem(m_L, m_ref); - } - - //============================================================================================= - /** - * @brief Access a table value using a key. - * - * The operation is raw, metamethods are not invoked. The result is passed by value and may not be modified. - * - * @param key A key in the table. - * - * @returns A reference to the table item. - */ - template - LuaRef rawget(const T& key) const - { - const StackRestore stackRestore(m_L); - - push(m_L); - - if (! Stack::push(m_L, key)) - return LuaRef(m_L); - - lua_rawget(m_L, -2); - return LuaRef(m_L, FromStack()); - } - - //============================================================================================= - /** - * @brief Get the unique hash of a LuaRef. - */ - std::size_t hash() const - { - std::size_t value; - switch (type()) - { - case LUA_TNONE: - value = std::hash{}(nullptr); - break; - - case LUA_TBOOLEAN: - value = std::hash{}(unsafe_cast()); - break; - - case LUA_TNUMBER: - value = std::hash{}(unsafe_cast()); - break; - - case LUA_TSTRING: - value = std::hash{}(unsafe_cast()); - break; - - case LUA_TNIL: - case LUA_TTABLE: - case LUA_TFUNCTION: - case LUA_TTHREAD: - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - default: - value = static_cast(m_ref); - break; - } - - const std::size_t seed = std::hash{}(type()); - return value + 0x9e3779b9u + (seed << 6) + (seed >> 2); - } - -private: - void swap(LuaRef& other) - { - using std::swap; - - swap(m_L, other.m_L); - swap(m_ref, other.m_ref); - } - - int m_ref = LUA_NOREF; -}; - -//================================================================================================= -/** - * @brief Equality between type T and LuaRef. - */ -template -auto operator==(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return rhs == lhs; -} - -/** - * @brief Inequality between type T and LuaRef. - */ -template -auto operator!=(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs == lhs); -} - -/** - * @brief Less than between type T and LuaRef. - */ -template -auto operator<(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs >= lhs); -} - -/** - * @brief Less than equal between type T and LuaRef. - */ -template -auto operator<=(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs > lhs); -} - -/** - * @brief Greater than between type T and LuaRef. - */ -template -auto operator>(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return rhs <= lhs; -} - -/** - * @brief Greater than equal between type T and LuaRef. - */ -template -auto operator>=(const T& lhs, const LuaRef& rhs) - -> std::enable_if_t && !std::is_same_v>, bool> -{ - return !(rhs > lhs); -} - -//================================================================================================= -/** - * @brief Stack specialization for `LuaRef`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const LuaRef& v) - { - v.push(L); - - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - return LuaRef::fromStack(L, index); - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `TableItem`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const LuaRef::TableItem& v) - { - v.push(L); - - return {}; - } -}; - -//================================================================================================= -/** - * @brief Create a reference to a new, empty table. - * - * This is a syntactic abbreviation for LuaRef::newTable (). - */ -[[nodiscard]] inline LuaRef newTable(lua_State* L) -{ - return LuaRef::newTable(L); -} - -//================================================================================================= -/** - * @brief Create a reference to a value in the global table. - * - * This is a syntactic abbreviation for LuaRef::getGlobal (). - */ -[[nodiscard]] inline LuaRef getGlobal(lua_State* L, const char* name) -{ - return LuaRef::getGlobal(L, name); -} - -//================================================================================================= -/** - * @brief C++ like cast syntax, safe. - */ -template -[[nodiscard]] TypeResult cast(const LuaRef& ref) -{ - return ref.cast(); -} - -/** - * @brief C++ like cast syntax, unsafe. - */ -template -[[nodiscard]] T unsafe_cast(const LuaRef& ref) -{ - return ref.unsafe_cast(); -} -} // namespace luabridge - -namespace std { -template <> -struct hash -{ - std::size_t operator()(const luabridge::LuaRef& x) const - { - return x.hash(); - } -}; -} // namespace std diff --git a/LuaBridge3/Source/LuaBridge/detail/Namespace.h b/LuaBridge3/Source/LuaBridge/detail/Namespace.h deleted file mode 100644 index 1804c92..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Namespace.h +++ /dev/null @@ -1,1956 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "ClassInfo.h" -#include "LuaHelpers.h" -#include "LuaException.h" -#include "Security.h" -#include "TypeTraits.h" - -#include -#include -#include -#include -#include - -namespace luabridge { -namespace detail { - -//================================================================================================= -/** - * @brief Base for class and namespace registration. - * - * Maintains Lua stack in the proper state. Once beginNamespace, beginClass or deriveClass is called the parent object upon its destruction - * may no longer clear the Lua stack. - * - * Then endNamespace or endClass is called, a new parent is created and the child transfers the responsibility for clearing stack to it. - * - * So there can be maximum one "active" registrar object. - */ -class Registrar -{ -protected: - Registrar(lua_State* L) - : L(L) - , m_stackSize(0) - { - } - - Registrar(lua_State* L, int skipStackPops) - : L(L) - , m_stackSize(0) - , m_skipStackPops(skipStackPops) - { - } - - Registrar(const Registrar& rhs) - : L(rhs.L) - , m_stackSize(std::exchange(rhs.m_stackSize, 0)) - , m_skipStackPops(std::exchange(rhs.m_skipStackPops, 0)) - { - } - - Registrar& operator=(const Registrar& rhs) - { - m_stackSize = rhs.m_stackSize; - m_skipStackPops = rhs.m_skipStackPops; - - return *this; - } - - ~Registrar() - { - const int popsCount = m_stackSize - m_skipStackPops; - if (popsCount > 0) - { - assert(popsCount <= lua_gettop(L)); - - lua_pop(L, popsCount); - } - } - - void assertIsActive() const - { - if (m_stackSize == 0) - { - throw_or_assert("Unable to continue registration"); - } - } - - lua_State* const L = nullptr; - int mutable m_stackSize = 0; - int mutable m_skipStackPops = 0; -}; - -} // namespace detail - -//================================================================================================= -/** - * @brief Provides C++ to Lua registration capabilities. - * - * This class is not instantiated directly, call `getGlobalNamespace` to start the registration process. - */ -class Namespace : public detail::Registrar -{ - //============================================================================================= -#if 0 - /** - * @brief Error reporting. - * - * This function looks handy, why aren't we using it? - */ - static int luaError(lua_State* L, std::string message) - { - assert(lua_isstring(L, lua_upvalueindex(1))); - std::string s; - - // Get information on the caller's caller to format the message, - // so the error appears to originate from the Lua source. - lua_Debug ar; - - int result = lua_getstack(L, 2, &ar); - if (result != 0) - { - lua_getinfo(L, "Sl", &ar); - s = ar.short_src; - if (ar.currentline != -1) - { - // poor mans int to string to avoid . - lua_pushnumber(L, ar.currentline); - s = s + ":" + lua_tostring(L, -1) + ": "; - lua_pop(L, 1); - } - } - - s = s + message; - - luaL_error(L, "%s", s.c_str()); - - return 0; - } -#endif - - //============================================================================================= - /** - * @brief Factored base to reduce template instantiations. - */ - class ClassBase : public detail::Registrar - { - public: - explicit ClassBase(Namespace& parent) - : Registrar(parent) - { - } - - using Registrar::operator=; - - protected: - //========================================================================================= - /** - * @brief Create the const table. - */ - void createConstTable(const char* name, bool trueConst = true) - { - assert(name != nullptr); - - std::string type_name = std::string(trueConst ? "const " : "") + name; - - // Stack: namespace table (ns) - lua_newtable(L); // Stack: ns, const table (co) - lua_pushvalue(L, -1); // Stack: ns, co, co - lua_setmetatable(L, -2); // co.__metatable = co. Stack: ns, co - - lua_pushstring(L, type_name.c_str()); - lua_rawsetp(L, -2, detail::getTypeKey()); // co [typeKey] = name. Stack: ns, co - - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); - - lua_pushcfunction_x(L, &detail::newindex_object_metamethod); - rawsetfield(L, -2, "__newindex"); - - lua_newtable(L); - lua_rawsetp(L, -2, detail::getPropgetKey()); - - if (Security::hideMetatables()) - { - lua_pushnil(L); - rawsetfield(L, -2, "__metatable"); - } - } - - //========================================================================================= - /** - * @brief Create the class table. - * - * The Lua stack should have the const table on top. - */ - void createClassTable(const char* name) - { - assert(name != nullptr); - - // Stack: namespace table (ns), const table (co) - - // Class table is the same as const table except the propset table - createConstTable(name, false); // Stack: ns, co, cl - - lua_newtable(L); // Stack: ns, co, cl, propset table (ps) - lua_rawsetp(L, -2, detail::getPropsetKey()); // cl [propsetKey] = ps. Stack: ns, co, cl - - lua_pushvalue(L, -2); // Stack: ns, co, cl, co - lua_rawsetp(L, -2, detail::getConstKey()); // cl [constKey] = co. Stack: ns, co, cl - - lua_pushvalue(L, -1); // Stack: ns, co, cl, cl - lua_rawsetp(L, -3, detail::getClassKey()); // co [classKey] = cl. Stack: ns, co, cl - } - - //========================================================================================= - /** - * @brief Create the static table. - */ - void createStaticTable(const char* name) - { - assert(name != nullptr); - - // Stack: namespace table (ns), const table (co), class table (cl) - lua_newtable(L); // Stack: ns, co, cl, visible static table (vst) - lua_newtable(L); // Stack: ns, co, cl, st, static metatable (st) - lua_pushvalue(L, -1); // Stack: ns, co, cl, vst, st, st - lua_setmetatable(L, -3); // st.__metatable = mt. Stack: ns, co, cl, vst, st - lua_insert(L, -2); // Stack: ns, co, cl, st, vst - rawsetfield(L, -5, name); // ns [name] = vst. Stack: ns, co, cl, st - -#if 0 - lua_pushlightuserdata(L, this); - lua_pushcclosure_x(L, &tostringMetaMethod, 1); - rawsetfield(L, -2, "__tostring"); -#endif - - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); - - lua_pushcfunction_x(L, &detail::newindex_static_metamethod); - rawsetfield(L, -2, "__newindex"); - - lua_newtable(L); // Stack: ns, co, cl, st, proget table (pg) - lua_rawsetp(L, -2, detail::getPropgetKey()); // st [propgetKey] = pg. Stack: ns, co, cl, st - - lua_newtable(L); // Stack: ns, co, cl, st, propset table (ps) - lua_rawsetp(L, -2, detail::getPropsetKey()); // st [propsetKey] = pg. Stack: ns, co, cl, st - - lua_pushvalue(L, -2); // Stack: ns, co, cl, st, cl - lua_rawsetp(L, -2, detail::getClassKey()); // st [classKey] = cl. Stack: ns, co, cl, st - - if (Security::hideMetatables()) - { - lua_pushnil(L); - rawsetfield(L, -2, "__metatable"); - } - } - - //========================================================================================= - /** - * @brief Asserts on stack state. - */ - void assertStackState() const - { - // Stack: const table (co), class table (cl), static table (st) - assert(lua_istable(L, -3)); - assert(lua_istable(L, -2)); - assert(lua_istable(L, -1)); - } - }; - - //============================================================================================= - /** - * @brief Provides a class registration in a lua_State. - * - * After construction the Lua stack holds these objects: - * -1 static table - * -2 class table - * -3 const table - * -4 enclosing namespace table - */ - template - class Class : public ClassBase - { - public: - //========================================================================================= - - /** - * @brief Register a new class or add to an existing class registration. - * - * @param name The new class name. - * @param parent A parent namespace object. - */ - Class(const char* name, Namespace& parent) - : ClassBase(parent) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - rawgetfield(L, -1, name); // Stack: ns, static table (st) | nil - - if (lua_isnil(L, -1)) // Stack: ns, nil - { - lua_pop(L, 1); // Stack: ns - - createConstTable(name); // Stack: ns, const table (co) -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); // Stack: ns, co, function - rawsetfield(L, -2, "__gc"); // co ["__gc"] = function. Stack: ns, co -#endif - ++m_stackSize; - - createClassTable(name); // Stack: ns, co, class table (cl) -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); // Stack: ns, co, cl, function - rawsetfield(L, -2, "__gc"); // cl ["__gc"] = function. Stack: ns, co, cl -#endif - ++m_stackSize; - - createStaticTable(name); // Stack: ns, co, cl, st - ++m_stackSize; - - // Map T back to its tables. - lua_pushvalue(L, -1); // Stack: ns, co, cl, st, st - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getStaticRegistryKey()); // Stack: ns, co, cl, st - lua_pushvalue(L, -2); // Stack: ns, co, cl, st, cl - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); // Stack: ns, co, cl, st - lua_pushvalue(L, -3); // Stack: ns, co, cl, st, co - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); // Stack: ns, co, cl, st - } - else - { - assert(lua_istable(L, -1)); // Stack: ns, st - ++m_stackSize; - - // Map T back from its stored tables - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); // Stack: ns, st, co - lua_insert(L, -2); // Stack: ns, co, st - ++m_stackSize; - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); // Stack: ns, co, st, cl - lua_insert(L, -2); // Stack: ns, co, cl, st - ++m_stackSize; - } - } - - //========================================================================================= - /** - * @brief Derive a new class. - * - * @param name The class name. - * @param parent A parent namespace object. - * @param staticKey Key where the class is stored. - */ - Class(const char* name, Namespace& parent, void const* const staticKey) - : ClassBase(parent) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - createConstTable(name); // Stack: ns, const table (co) -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); // Stack: ns, co, function - rawsetfield(L, -2, "__gc"); // co ["__gc"] = function. Stack: ns, co -#endif - ++m_stackSize; - - createClassTable(name); // Stack: ns, co, class table (cl) -#if !defined(LUABRIDGE_ON_LUAU) - lua_pushcfunction_x(L, &detail::gc_metamethod); // Stack: ns, co, cl, function - rawsetfield(L, -2, "__gc"); // cl ["__gc"] = function. Stack: ns, co, cl -#endif - ++m_stackSize; - - createStaticTable(name); // Stack: ns, co, cl, st - ++m_stackSize; - - lua_rawgetp(L, LUA_REGISTRYINDEX, staticKey); // Stack: ns, co, cl, st, parent st (pst) | nil - if (lua_isnil(L, -1)) // Stack: ns, co, cl, st, nil - { - lua_pop(L, 1); - - throw_or_assert("Base class is not registered"); - return; - } - - assert(lua_istable(L, -1)); // Stack: ns, co, cl, st, pst - - lua_rawgetp(L, -1, detail::getClassKey()); // Stack: ns, co, cl, st, pst, parent cl (pcl) - assert(lua_istable(L, -1)); - - lua_rawgetp(L, -1, detail::getConstKey()); // Stack: ns, co, cl, st, pst, pcl, parent co (pco) - assert(lua_istable(L, -1)); - - lua_rawsetp(L, -6, detail::getParentKey()); // co [parentKey] = pco. Stack: ns, co, cl, st, pst, pcl - lua_rawsetp(L, -4, detail::getParentKey()); // cl [parentKey] = pcl. Stack: ns, co, cl, st, pst - lua_rawsetp(L, -2, detail::getParentKey()); // st [parentKey] = pst. Stack: ns, co, cl, st - - lua_pushvalue(L, -1); // Stack: ns, co, cl, st, st - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getStaticRegistryKey()); // Stack: ns, co, cl, st - lua_pushvalue(L, -2); // Stack: ns, co, cl, st, cl - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); // Stack: ns, co, cl, st - lua_pushvalue(L, -3); // Stack: ns, co, cl, st, co - lua_rawsetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); // Stack: ns, co, cl, st - } - - //========================================================================================= - /** - * @brief Continue registration in the enclosing namespace. - * - * @returns A parent registration object. - */ - Namespace endClass() - { - assert(m_stackSize > 3); - - m_stackSize -= 3; - lua_pop(L, 3); - return Namespace(*this); - } - - //========================================================================================= - /** - * @brief Add or replace a static property. - * - * @tparam U The type of the property. - * - * @param name The property name. - * @param value A property value pointer. - * @param isWritable True for a read-write, false for read-only property. - * - * @returns This class registration object. - */ - template >> - Class& addStaticProperty(const char* name, const U* value) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, const_cast(value)); // Stack: co, cl, st, pointer - lua_pushcclosure_x(L, &detail::property_getter::call, 1); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -2); // Stack: co, cl, st - - lua_pushstring(L, name); // Stack: co, cl, st, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: co, cl, st, function - - detail::add_property_setter(L, name, -2); // Stack: co, cl, st - - return *this; - } - - /** - * @brief Add or replace a static property. - * - * @tparam U The type of the property. - * - * @param name The property name. - * @param value A property value pointer. - * @param isWritable True for a read-write, false for read-only property. - * - * @returns This class registration object. - */ - template >> - Class& addStaticProperty(const char* name, U* value, bool isWritable = true) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, value); // Stack: co, cl, st, pointer - lua_pushcclosure_x(L, &detail::property_getter::call, 1); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -2); // Stack: co, cl, st - - if (isWritable) - { - lua_pushlightuserdata(L, value); // Stack: co, cl, st, ps, pointer - lua_pushcclosure_x(L, &detail::property_setter::call, 1); // Stack: co, cl, st, ps, setter - } - else - { - lua_pushstring(L, name); // Stack: co, cl, st, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: co, cl, st, function - } - - detail::add_property_setter(L, name, -2); // Stack: co, cl, st - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a static property member. - * - * @tparam U The type of the property. - * - * @param name The property name. - * @param get A property getter function pointer. - * @param set A property setter function pointer, optional, nullable. Omit or pass nullptr for a read-only property. - * - * @returns This class registration object. - */ - template - Class& addStaticProperty(const char* name, U (*get)(), void (*set)(U) = nullptr) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, reinterpret_cast(get)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -2); // Stack: co, cl, st - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, setter - } - else - { - lua_pushstring(L, name); // Stack: co, cl, st, ps, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: co, cl, st, function - } - - detail::add_property_setter(L, name, -2); // Stack: co, cl, st - - return *this; - } - - template - Class& addStaticProperty(const char* name, U (*get)() noexcept, void (*set)(U) noexcept = nullptr) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, reinterpret_cast(get)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -2); // Stack: co, cl, st - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, setter - } - else - { - lua_pushstring(L, name); // Stack: co, cl, st, ps, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: co, cl, st, function - } - - detail::add_property_setter(L, name, -2); // Stack: co, cl, st - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a static property, by constructible by std::function. - */ - template >> - Class& addStaticProperty(const char* name, Getter get) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - using GetType = decltype(get); - - lua_newuserdata_aligned(L, std::move(get)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, function - detail::add_property_getter(L, name, -2); // Stack: co, cl, st - - return *this; - } - - template && !std::is_pointer_v>> - Class& addStaticProperty(const char* name, Getter get, Setter set) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - using GetType = decltype(get); - using SetType = decltype(set); - - lua_newuserdata_aligned(L, std::move(get)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, function - detail::add_property_getter(L, name, -2); // Stack: co, cl, st - - lua_newuserdata_aligned(L, std::move(set)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, function - detail::add_property_setter(L, name, -2); // Stack: co, cl, st - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a single static function or multiple overloaded functions. - * - * @param name The overload name. - * @param functions A single or set of static functions that will be invoked. - * - * @returns This class registration object. - */ - template - auto addStaticFunction(const char* name, Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Class&> - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_function(L, std::move(functions)); - - } (), ...); - } - else - { - // create new closure of try_overloads with new table - lua_createtable(L, static_cast(sizeof...(Functions)), 0); // reserve space for N overloads - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, name); - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a property member. - */ - template - Class& addProperty(const char* name, U V::*mp, bool isWritable = true) - { - static_assert(std::is_base_of_v); - - using MemberPtrType = decltype(mp); - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(MemberPtrType))) MemberPtrType(mp); // Stack: co, cl, st, field ptr - lua_pushcclosure_x(L, &detail::property_getter::call, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (isWritable) - { - new (lua_newuserdata_x(L, sizeof(MemberPtrType))) MemberPtrType(mp); // Stack: co, cl, st, field ptr - lua_pushcclosure_x(L, &detail::property_setter::call, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a property member. - */ - template - Class& addProperty(const char* name, TG (T::*get)() const, void (T::*set)(TS) = nullptr) - { - using GetType = TG (T::*)() const; - using SetType = void (T::*)(TS); - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); // Stack: co, cl, st, funcion ptr - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (T::*get)() const noexcept, void (T::*set)(TS) noexcept = nullptr) - { - using GetType = TG (T::*)() const noexcept; - using SetType = void (T::*)(TS) noexcept; - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); // Stack: co, cl, st, funcion ptr - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a property member. - */ - template - Class& addProperty(const char* name, TG (T::*get)(lua_State*) const, void (T::*set)(TS, lua_State*) = nullptr) - { - using GetType = TG (T::*)(lua_State*) const; - using SetType = void (T::*)(TS, lua_State*); - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); // Stack: co, cl, st, funcion ptr - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (T::*get)(lua_State*) const noexcept, void (T::*set)(TS, lua_State*) noexcept = nullptr) - { - using GetType = TG (T::*)(lua_State*) const noexcept; - using SetType = void (T::*)(TS, lua_State*) noexcept; - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(GetType))) GetType(get); // Stack: co, cl, st, funcion ptr - lua_pushcclosure_x(L, &detail::invoke_const_member_function, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (set != nullptr) - { - new (lua_newuserdata_x(L, sizeof(SetType))) SetType(set); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a property member, by proxy. - * - * When a class is closed for modification and does not provide (or cannot provide) the function signatures necessary to implement - * get or set for a property, this will allow non-member functions act as proxies. - * - * Both the get and the set functions require a T const* and T* in the first argument respectively. - */ - template - Class& addProperty(const char* name, TG (*get)(const T*), void (*set)(T*, TS) = nullptr) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, reinterpret_cast(get)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st,, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (set != nullptr) - { - lua_pushlightuserdata( L, reinterpret_cast(set)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - template - Class& addProperty(const char* name, TG (*get)(const T*) noexcept, void (*set)(T*, TS) noexcept = nullptr) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, reinterpret_cast(get)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st,, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st - - if (set != nullptr) - { - lua_pushlightuserdata( L, reinterpret_cast(set)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - } - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a property member, by proxy C-function. - * - * When a class is closed for modification and does not provide (or cannot provide) the function signatures necessary to implement - * get or set for a property, this will allow non-member functions act as proxies. - * - * The object userdata ('this') value is at the index 1. - * The new value for set function is at the index 2. - */ - Class& addProperty(const char* name, lua_CFunction get, lua_CFunction set = nullptr) - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushcfunction_x(L, get); - lua_pushvalue(L, -1); // Stack: co, cl, st,, getter, getter - detail::add_property_getter(L, name, -5); // Stack: co, cl, st,, getter - detail::add_property_getter(L, name, -3); // Stack: co, cl, st, - - if (set != nullptr) - { - lua_pushcfunction_x(L, set); - detail::add_property_setter(L, name, -3); // Stack: co, cl, st, - } - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a property member, by constructible by std::function. - */ - template >> - Class& addProperty(const char* name, Getter get) - { - using FirstArg = detail::function_argument_t<0, Getter>; - static_assert(std::is_same_v>, T>); - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - using GetType = decltype(get); - - lua_newuserdata_aligned(L, std::move(get)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, getter - lua_pushvalue(L, -1); // Stack: co, cl, st, getter, getter - detail::add_property_getter(L, name, -4); // Stack: co, cl, st, getter - detail::add_property_getter(L, name, -4); // Stack: co, cl, st - - return *this; - } - - template && !std::is_pointer_v>> - Class& addProperty(const char* name, Getter get, Setter set) - { - addProperty(name, std::move(get)); - - using FirstArg = detail::function_argument_t<0, Setter>; - static_assert(std::is_same_v>, T>); - - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - using SetType = decltype(set); - - lua_newuserdata_aligned(L, std::move(set)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, setter - detail::add_property_setter(L, name, -3); // Stack: co, cl, st - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a function that can operate on the class. - * - * @param name The function or overloaded functions name. - * @param functions A single or set of functions that will be invoked. - * - * @returns This class registration object. - */ - template - auto addFunction(const char* name, Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Class&> - { - assert(name != nullptr); - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - if (name == std::string_view("__gc")) - { - throw_or_assert("__gc metamethod registration is forbidden"); - return *this; - } - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_member_function(L, std::move(functions)); - - } (), ...); - - if constexpr (detail::const_functions_count == 1) - { - lua_pushvalue(L, -1); // Stack: co, cl, st, function, function - rawsetfield(L, -4, name); // Stack: co, cl, st, function - rawsetfield(L, -4, name); // Stack: co, cl, st - } - else - { - rawsetfield(L, -3, name); // Stack: co, cl, st - } - } - else - { - // create new closure of const try_overload_functions with new table - if constexpr (detail::const_functions_count > 0) - { - lua_createtable(L, static_cast(detail::const_functions_count), 0); - - int idx = 1; - - ([&] - { - if (!detail::is_const_function) - return; - - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::member_function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_member_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - assert(idx > 1); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - lua_pushvalue(L, -1); // Stack: co, cl, st, function, function - rawsetfield(L, -4, name); // Stack: co, cl, st, function - rawsetfield(L, -4, name); // Stack: co, cl, st - } - - // create new closure of non const try_overload_functions with new table - if constexpr (detail::non_const_functions_count > 0) - { - lua_createtable(L, static_cast(detail::non_const_functions_count), 0); - - int idx = 1; - - ([&] - { - if (detail::is_const_function) - return; - - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::member_function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_member_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - assert(idx > 1); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - rawsetfield(L, -3, name); // Stack: co, cl, st - } - } - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a primary Constructor. - * - * The primary Constructor is invoked when calling the class type table like a function. - * - * The template parameter should be a function pointer type that matches the desired Constructor (since you can't take the - * address of a Constructor and pass it as an argument). - */ - template - auto addConstructor() - -> std::enable_if_t<(sizeof...(Functions) > 0), Class&> - { - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - lua_pushcclosure_x(L, &detail::constructor_placement_proxy>, 0); - - } (), ...); - } - else - { - // create new closure of try_overloads with new table - lua_createtable(L, static_cast(sizeof...(Functions)), 0); // reserve space for N overloads - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - lua_pushcclosure_x(L, &detail::constructor_placement_proxy>, 0); - lua_settable(L, -3); - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, "__call"); - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a primary Constructor when the type is used from an intrusive container C. - */ - template - auto addConstructorFrom() - -> std::enable_if_t<(sizeof...(Functions) > 0), Class&> - { - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - lua_pushcclosure_x(L, &detail::constructor_container_proxy>, 0); - - } (), ...); - } - else - { - // create new closure of try_overloads with new table - lua_createtable(L, static_cast(sizeof...(Functions)), 0); // reserve space for N overloads - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - lua_pushcclosure_x(L, &detail::constructor_container_proxy>, 0); - lua_settable(L, -3); - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, "__call"); - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a placement constructor. - * - * The primary placement constructor is invoked when calling the class type table like a function. - * - * The provider of the Function argument is responsible of doing placement new of the type T over the void* pointer provided to - * the method as first argument. - */ - template - auto addConstructor(Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Class&> - { - static_assert(((detail::function_arity_excluding_v >= 1) && ...)); - static_assert(((std::is_same_v, void*>) && ...)); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_function(L, detail::constructor_forwarder(std::move(functions))); // Stack: co, cl, st, function - - } (), ...); - } - else - { - // create new closure of try_overloads with new table - lua_createtable(L, static_cast(sizeof...(Functions)), 0); // reserve space for N overloads - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v) - 1); // 1: for void* ptr - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_function(L, detail::constructor_forwarder(std::move(functions))); - lua_settable(L, -3); - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, "__call"); // Stack: co, cl, st - - return *this; - } - - //========================================================================================= - /** - * @brief Add or replace a factory. - * - * The primary Constructor is invoked when calling the class type table like a function. - * - * The template parameter should be a function pointer type that matches the desired Constructor (since you can't take the - * address of a Constructor and pass it as an argument). - */ - template - Class& addFactory(Allocator allocator, Deallocator deallocator) - { - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - detail::push_function(L, detail::factory_forwarder(std::move(allocator), std::move(deallocator))); - rawsetfield(L, -2, "__call"); // Stack: co, cl, st - - return *this; - } - - //========================================================================================= - /** - * @brief Add an index metamethod function fallback that is triggered when no result is found in functions, properties or any other members. - * - * Let the user define a fallback index (__index) metamethod at its level. - */ - template - auto addIndexMetaMethod(Function function) - -> std::enable_if_t - && std::is_invocable_v, Class&> - { - using FnType = decltype(function); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_newuserdata_aligned(L, std::move(function)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, function - lua_rawsetp(L, -3, detail::getIndexFallbackKey()); - - return *this; - } - - Class& addIndexMetaMethod(LuaRef (*idxf)(T&, const LuaRef&, lua_State*)) - { - using FnType = decltype(idxf); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, reinterpret_cast(idxf)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, function - lua_rawsetp(L, -3, detail::getIndexFallbackKey()); - - return *this; - } - - Class& addIndexMetaMethod(LuaRef (T::* idxf)(const LuaRef&, lua_State*)) - { - using MemFnPtr = decltype(idxf); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(MemFnPtr))) MemFnPtr(idxf); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - lua_rawsetp(L, -3, detail::getIndexFallbackKey()); - - return *this; - } - - //========================================================================================= - /** - * @brief Add an insert index metamethod function fallback that is triggered when no result is found in functions, properties or any other members. - * - * Let the user define a fallback insert index (___newindex) metamethod at its level. - */ - template - auto addNewIndexMetaMethod(Function function) - -> std::enable_if_t - && std::is_invocable_v, Class&> - { - using FnType = decltype(function); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_newuserdata_aligned(L, std::move(function)); // Stack: co, cl, st, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: co, cl, st, function - lua_rawsetp(L, -3, detail::getNewIndexFallbackKey()); - - return *this; - } - - Class& addNewIndexMetaMethod(LuaRef (*idxf)(T&, const LuaRef&, const LuaRef&, lua_State*)) - { - using FnType = decltype(idxf); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - lua_pushlightuserdata(L, reinterpret_cast(idxf)); // Stack: co, cl, st, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: co, cl, st, function - lua_rawsetp(L, -3, detail::getNewIndexFallbackKey()); - - return *this; - } - - Class& addNewIndexMetaMethod(LuaRef (T::* idxf)(const LuaRef&, const LuaRef&, lua_State*)) - { - using MemFnPtr = decltype(idxf); - - assertStackState(); // Stack: const table (co), class table (cl), static table (st) - - new (lua_newuserdata_x(L, sizeof(MemFnPtr))) MemFnPtr(idxf); - lua_pushcclosure_x(L, &detail::invoke_member_function, 1); - lua_rawsetp(L, -3, detail::getNewIndexFallbackKey()); - - return *this; - } - }; - - class Table : public detail::Registrar - { - public: - explicit Table(const char* name, Namespace& parent) - : Registrar(parent) - { - lua_newtable(L); // Stack: ns, table (tb) - lua_pushvalue(L, -1); // Stack: ns, tb, tb - rawsetfield(L, -3, name); - ++m_stackSize; - - lua_newtable(L); // Stack: ns, table (tb) - lua_pushvalue(L, -1); // Stack: ns, tb, tb - lua_setmetatable(L, -3); // tb.__metatable = tb. Stack: ns, tb - ++m_stackSize; - } - - using Registrar::operator=; - - template - Table& addFunction(const char* name, Function function) - { - using FnType = decltype(function); - - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - lua_newuserdata_aligned(L, std::move(function)); // Stack: ns, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: ns, function - rawsetfield(L, -3, name); // Stack: ns - - return *this; - } - - template - Table& addMetaFunction(const char* name, Function function) - { - using FnType = decltype(function); - - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - lua_newuserdata_aligned(L, std::move(function)); // Stack: ns, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: ns, function - rawsetfield(L, -2, name); // Stack: ns - - return *this; - } - - Namespace endTable() - { - assert(m_stackSize > 2); - - m_stackSize -= 2; - lua_pop(L, 2); - return Namespace(*this); - } - }; - -private: - struct FromStack {}; - - //============================================================================================= - /** - * @brief Open the global namespace for registrations. - * - * @param L A Lua state. - */ - explicit Namespace(lua_State* L) - : Registrar(L) - { - lua_getglobal(L, "_G"); - - ++m_stackSize; - } - - //============================================================================================= - /** - * @brief Open the a namespace for registrations from a table on top of the stack. - * - * @param L A Lua state. - */ - Namespace(lua_State* L, FromStack) - : Registrar(L, 1) - { - assert(lua_istable(L, -1)); - - { - lua_pushvalue(L, -1); // Stack: ns, mt - - // ns.__metatable = ns - lua_setmetatable(L, -2); // Stack: ns, mt - - // ns.__index = index_metamethod - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); // Stack: ns - - lua_newtable(L); // Stack: ns, mt, propget table (pg) - lua_rawsetp(L, -2, detail::getPropgetKey()); // ns [propgetKey] = pg. Stack: ns - - lua_newtable(L); // Stack: ns, mt, propset table (ps) - lua_rawsetp(L, -2, detail::getPropsetKey()); // ns [propsetKey] = ps. Stack: ns - } - - ++m_stackSize; - } - - //============================================================================================= - /** - * @brief Open a namespace for registrations. - * - * The namespace is created if it doesn't already exist. - * - * @param name The namespace name. - * @param parent The parent namespace object. - * - * @pre The parent namespace is at the top of the Lua stack. - */ - Namespace(const char* name, Namespace& parent) - : Registrar(parent) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: parent namespace (pns) - - rawgetfield(L, -1, name); // Stack: pns, namespace (ns) | nil - - if (lua_isnil(L, -1)) // Stack: pns, nil - { - lua_pop(L, 1); // Stack: pns - - lua_newtable(L); // Stack: pns, ns - lua_pushvalue(L, -1); // Stack: pns, ns, mt - - // ns.__metatable = ns - lua_setmetatable(L, -2); // Stack: pns, ns - - // ns.__index = index_metamethod - lua_pushcfunction_x(L, &detail::index_metamethod); - rawsetfield(L, -2, "__index"); // Stack: pns, ns - - // ns.__newindex = newindex_static_metamethod - lua_pushcfunction_x(L, &detail::newindex_static_metamethod); - rawsetfield(L, -2, "__newindex"); // Stack: pns, ns - - lua_newtable(L); // Stack: pns, ns, propget table (pg) - lua_rawsetp(L, -2, detail::getPropgetKey()); // ns [propgetKey] = pg. Stack: pns, ns - - lua_newtable(L); // Stack: pns, ns, propset table (ps) - lua_rawsetp(L, -2, detail::getPropsetKey()); // ns [propsetKey] = ps. Stack: pns, ns - - // pns [name] = ns - lua_pushvalue(L, -1); // Stack: pns, ns, ns - rawsetfield(L, -3, name); // Stack: pns, ns - } - - ++m_stackSize; - } - - //============================================================================================= - /** - * @brief Close the class and continue the namespace registrations. - * - * @param child A child class registration object. - */ - explicit Namespace(ClassBase& child) - : Registrar(child) - { - } - - explicit Namespace(Table& child) - : Registrar(child) - { - } - - using Registrar::operator=; - -public: - //============================================================================================= - /** - * @brief Retrieve the global namespace. - * - * It is recommended to put your namespace inside the global namespace, and then add your classes and functions to it, rather than - * adding many classes and functions directly to the global namespace. - * - * @param L A Lua state. - * - * @returns A namespace registration object. - */ - static Namespace getGlobalNamespace(lua_State* L) - { - return Namespace(L); - } - - /** - * @brief Retrieve the namespace on top of the stack. - * - * You should have a table on top of the stack before calling this function. It will then use the table there as destination for registrations. - * - * @param L A Lua state. - * - * @returns A namespace registration object. - */ - static Namespace getNamespaceFromStack(lua_State* L) - { - return Namespace(L, FromStack{}); - } - - //============================================================================================= - /** - * @brief Open a new or existing namespace for registrations. - * - * @param name The namespace name. - * - * @returns A namespace registration object. - */ - Namespace beginNamespace(const char* name) - { - assertIsActive(); - return Namespace(name, *this); - } - - //============================================================================================= - /** - * @brief Continue namespace registration in the parent. - * - * Do not use this on the global namespace. - * - * @returns A parent namespace registration object. - */ - Namespace endNamespace() - { - if (m_stackSize == 1) - { - throw_or_assert("endNamespace() called on global namespace"); - - return Namespace(*this); - } - - assert(m_stackSize > 1); - --m_stackSize; - lua_pop(L, 1); - return Namespace(*this); - } - - //============================================================================================= - /** - * @brief Add or replace a variable, a variable will be added in the namespace by copy of the passed value. - * - * @param name The property name. - * @param value A value object. - * - * @returns This namespace registration object. - */ - template - Namespace& addVariable(const char* name, const T& value) - { - if (m_stackSize == 1) - { - throw_or_assert("addVariable() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - auto result = Stack::push(L, static_cast(value)); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - } - else - { - auto result = Stack::push(L, value); - if (! result) - luaL_error(L, "%s", result.message().c_str()); - } - - rawsetfield(L, -2, name); // Stack: ns - - return *this; - } - - //============================================================================================= - /** - * @brief Add or replace a property. - * - * @param name The property name. - * @param value A value pointer. - * @param isWritable True for a read-write, false for read-only property. - * - * @returns This namespace registration object. - */ - template - Namespace& addProperty(const char* name, T* value, bool isWritable = true) - { - if (m_stackSize == 1) - { - throw_or_assert("addProperty() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - lua_pushlightuserdata(L, value); // Stack: ns, pointer - lua_pushcclosure_x(L, &detail::property_getter::call, 1); // Stack: ns, getter - detail::add_property_getter(L, name, -2); // Stack: ns - - if (isWritable) - { - lua_pushlightuserdata(L, value); // Stack: ns, pointer - lua_pushcclosure_x(L, &detail::property_setter::call, 1); // Stack: ns, setter - } - else - { - lua_pushstring(L, name); // Stack: ns, ps, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: ns, function - } - - detail::add_property_setter(L, name, -2); // Stack: ns - - return *this; - } - - //============================================================================================= - /** - * @brief Add or replace a property. - * - * If the set function is omitted or null, the property is read-only. - * - * @param name The property name. - * @param get A pointer to a property getter function. - * @param set A pointer to a property setter function, optional. - * - * @returns This namespace registration object. - */ - template - Namespace& addProperty(const char* name, TG (*get)(), void (*set)(TS) = nullptr) - { - if (m_stackSize == 1) - { - throw_or_assert("addProperty() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - lua_pushlightuserdata(L, reinterpret_cast(get)); // Stack: ns, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: ns, getter - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); // Stack: ns, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - template - Namespace& addProperty(const char* name, TG (*get)() noexcept, void (*set)(TS) noexcept = nullptr) - { - if (m_stackSize == 1) - { - throw_or_assert("addProperty() called on global namespace"); - - return *this; - } - - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - lua_pushlightuserdata(L, reinterpret_cast(get)); // Stack: ns, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); // Stack: ns, getter - detail::add_property_getter(L, name, -2); - - if (set != nullptr) - { - lua_pushlightuserdata(L, reinterpret_cast(set)); // Stack: ns, function ptr - lua_pushcclosure_x(L, &detail::invoke_proxy_function, 1); - } - else - { - lua_pushstring(L, name); - lua_pushcclosure_x(L, &detail::read_only_error, 1); - } - - detail::add_property_setter(L, name, -2); - - return *this; - } - - //============================================================================================= - /** - * @brief Add or replace a readonly property. - * - * @param name The property name. - * @param get A pointer to a property getter function. - * - * @returns This namespace registration object. - */ - template >> - Namespace& addProperty(const char* name, Getter get) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - if constexpr (std::is_enum_v) - { - using U = std::underlying_type_t; - - auto enumGet = [get = std::move(get)] { return static_cast(get); }; - - using GetType = decltype(enumGet); - lua_newuserdata_aligned(L, std::move(enumGet)); // Stack: ns, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: ns, ud, getter - } - else - { - using GetType = decltype(get); - lua_newuserdata_aligned(L, std::move(get)); // Stack: ns, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: ns, ud, getter - } - - detail::add_property_getter(L, name, -2); // Stack: ns, ud, getter - - lua_pushstring(L, name); // Stack: ns, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: ns, name, function - detail::add_property_setter(L, name, -2); // Stack: ns - - return *this; - } - - /** - * @brief Add or replace a mutable property. - * - * @param name The property name. - * @param get A pointer to a property getter function. - * @param set A pointer to a property setter function. - * - * @returns This namespace registration object. - */ - template && !std::is_pointer_v>> - Namespace& addProperty(const char* name, Getter get, Setter set) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - addProperty(name, std::move(get)); - - using SetType = decltype(set); - - lua_newuserdata_aligned(L, std::move(set)); // Stack: ns, function userdata (ud) - lua_pushcclosure_x(L, &detail::invoke_proxy_functor, 1); // Stack: ns, ud, getter - detail::add_property_setter(L, name, -2); // Stack: ns, ud, getter - - return *this; - } - - //============================================================================================= - /** - * @brief Add or replace a property. - * - * If the set function is omitted or null, the property is read-only. - * - * @param name The property name. - * @param get A pointer to a property getter function. - * @param set A pointer to a property setter function, optional. - * - * @returns This namespace registration object. - */ - Namespace& addProperty(const char* name, lua_CFunction get, lua_CFunction set = nullptr) - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - lua_pushcfunction_x(L, get); // Stack: ns, getter - detail::add_property_getter(L, name, -2); // Stack: ns - - if (set != nullptr) - { - lua_pushcfunction_x(L, set); // Stack: ns, setter - detail::add_property_setter(L, name, -2); // Stack: ns - } - else - { - lua_pushstring(L, name); // Stack: ns, name - lua_pushcclosure_x(L, &detail::read_only_error, 1); // Stack: ns, name, function - detail::add_property_setter(L, name, -2); // Stack: ns - } - - return *this; - } - - //============================================================================================= - /** - * @brief Add or replace a single function or multiple overloaded functions. - * - * @param name The overload name. - * @param functions A single or set of functions that will be invoked. - * - * @returns This namespace registration object. - */ - template - auto addFunction(const char* name, Functions... functions) - -> std::enable_if_t<(detail::is_callable_v && ...) && (sizeof...(Functions) > 0), Namespace&> - { - assert(name != nullptr); - assert(lua_istable(L, -1)); // Stack: namespace table (ns) - - if constexpr (sizeof...(Functions) == 1) - { - ([&] - { - detail::push_function(L, std::move(functions)); - - } (), ...); - } - else - { - // create new closure of try_overloads with new table - lua_createtable(L, static_cast(sizeof...(Functions)), 0); // reserve space for N overloads - - int idx = 1; - - ([&] - { - lua_createtable(L, 2, 0); // reserve space for: function, arity - lua_pushinteger(L, 1); - if constexpr (detail::is_any_cfunction_pointer_v) - lua_pushinteger(L, -1); - else - lua_pushinteger(L, static_cast(detail::function_arity_excluding_v)); - lua_settable(L, -3); - lua_pushinteger(L, 2); - detail::push_function(L, std::move(functions)); - lua_settable(L, -3); - - lua_rawseti(L, -2, idx); - ++idx; - - } (), ...); - - lua_pushcclosure_x(L, &detail::try_overload_functions, 1); - } - - rawsetfield(L, -2, name); - - return *this; - } - - //============================================================================================= - Table beginTable(const char* name) - { - assertIsActive(); - return Table(name, *this); - } - - //============================================================================================= - /** - * @brief Open a new or existing class for registrations. - * - * @param name The class name. - * - * @returns A class registration object. - */ - template - Class beginClass(const char* name) - { - assertIsActive(); - return Class(name, *this); - } - - //============================================================================================= - /** - * @brief Derive a new class for registrations. - * - * Call deriveClass() only once. To continue registrations for the class later, use beginClass(). - * - * @param name The class name. - * - * @returns A class registration object. - */ - template - Class deriveClass(const char* name) - { - assertIsActive(); - return Class(name, *this, detail::getStaticRegistryKey()); - } -}; - -//================================================================================================= -/** - * @brief Retrieve the global namespace. - * - * It is recommended to put your namespace inside the global namespace, and then add your classes and functions to it, rather than adding - * many classes and functions directly to the global namespace. - * - * @param L A Lua state. - * - * @returns A namespace registration object. - */ -inline Namespace getGlobalNamespace(lua_State* L) -{ - return Namespace::getGlobalNamespace(L); -} - -//================================================================================================= -/** - * @brief Retrieve the namespace on top of the stack. - * - * You should have a table on top of the stack before calling this function. It will then use the table there as destination for registrations. - * - * @param L A Lua state. - * - * @returns A namespace registration object. - */ -inline Namespace getNamespaceFromStack(lua_State* L) -{ - return Namespace::getNamespaceFromStack(L); -} - -//================================================================================================= -/** - * @brief Registers main thread. - * - * This is a backward compatibility mitigation for lua 5.1 not supporting LUA_RIDX_MAINTHREAD. - * - * @param L The main Lua state that will be registered as main thread. - * - * @returns A namespace registration object. - */ -inline void registerMainThread(lua_State* L) -{ - register_main_thread(L); -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Overload.h b/LuaBridge3/Source/LuaBridge/detail/Overload.h deleted file mode 100644 index e5fc54f..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Overload.h +++ /dev/null @@ -1,65 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// Copyright 2019, George Tokmaji -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Errors.h" -#include "Stack.h" -#include "TypeTraits.h" -#include "Userdata.h" - -#include -#include - -namespace luabridge { -namespace detail { - -template -struct non_const_overload -{ - template - constexpr auto operator()(R (T::*ptr)(Args...)) const noexcept -> decltype(ptr) - { - return ptr; - } -}; - -template -struct const_overload -{ - template - constexpr auto operator()(R (T::*ptr)(Args...) const) const noexcept -> decltype(ptr) - { - return ptr; - } -}; - -template -struct overload : const_overload, non_const_overload -{ - using const_overload::operator(); - using non_const_overload::operator(); - - template - constexpr auto operator()(R (*ptr)(Args...)) const noexcept -> decltype(ptr) - { - return ptr; - } -}; - -} // namespace detail - -//================================================================================================= -/** - * @brief Overload resolution. - */ -template constexpr detail::overload overload = {}; -template constexpr detail::const_overload constOverload = {}; -template constexpr detail::non_const_overload nonConstOverload = {}; - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Result.h b/LuaBridge3/Source/LuaBridge/detail/Result.h deleted file mode 100644 index b87a329..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Result.h +++ /dev/null @@ -1,146 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Errors.h" - -namespace luabridge { - -//================================================================================================= -/** - * @brief Simple result class containing a result. - */ -struct Result -{ - Result() = default; - - Result(std::error_code ec) noexcept - : m_ec(ec) - { - } - - Result(const Result&) = default; - Result(Result&&) = default; - Result& operator=(const Result&) = default; - Result& operator=(Result&&) = default; - - explicit operator bool() const noexcept - { - return !m_ec; - } - - std::error_code error() const noexcept - { - return m_ec; - } - - operator std::error_code() const noexcept - { - return m_ec; - } - - std::string message() const - { - return m_ec.message(); - } - -private: - std::error_code m_ec; -}; - -//================================================================================================= -/** - * @brief Simple result class containing or a type T or an error code. - */ -template -struct TypeResult -{ - TypeResult() = default; - - template && !std::is_same_v, std::error_code>>> - TypeResult(U&& value) noexcept - : m_value(std::in_place, std::forward(value)) - { - } - - TypeResult(std::error_code ec) noexcept - : m_value(makeUnexpected(ec)) - { - } - - TypeResult(const TypeResult&) = default; - TypeResult(TypeResult&&) = default; - TypeResult& operator=(const TypeResult&) = default; - TypeResult& operator=(TypeResult&&) = default; - - explicit operator bool() const - { - return m_value.hasValue(); - } - - const T& value() const - { - return m_value.value(); - } - - T& operator*() & - { - return m_value.value(); - } - - T operator*() && - { - return std::move(m_value.value()); - } - - const T& operator*() const& - { - return m_value.value(); - } - - std::error_code error() const - { - return m_value.error(); - } - - operator std::error_code() const - { - return m_value.error(); - } - - std::string message() const - { - return m_value.error().message(); - } - -private: - Expected m_value; -}; - -template -inline bool operator==(const TypeResult& lhs, const U& rhs) noexcept -{ - return lhs ? *lhs == rhs : false; -} - -template -inline bool operator==(const U& lhs, const TypeResult& rhs) noexcept -{ - return rhs == lhs; -} - -template -inline bool operator!=(const TypeResult& lhs, const U& rhs) noexcept -{ - return !(lhs == rhs); -} - -template -inline bool operator!=(const U& lhs, const TypeResult& rhs) noexcept -{ - return !(rhs == lhs); -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/ScopeGuard.h b/LuaBridge3/Source/LuaBridge/detail/ScopeGuard.h deleted file mode 100644 index b44e138..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/ScopeGuard.h +++ /dev/null @@ -1,47 +0,0 @@ -// https://github.com/vinniefalco/LuaBridge -// Copyright 2021, Lucio Asnaghi -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Stack.h" - -namespace luabridge::detail { - -//================================================================================================= -/** - * @brief Scope guard. - */ -template -class ScopeGuard -{ -public: - template - ScopeGuard(V&& v) - : m_func(std::forward(v)) - , m_shouldRun(true) - { - } - - ~ScopeGuard() - { - if (m_shouldRun) - m_func(); - } - - void reset() - { - m_shouldRun = false; - } - -private: - F m_func; - bool m_shouldRun; -}; - -template -ScopeGuard(F&&) -> ScopeGuard; - -} // namespace luabridge::detail diff --git a/LuaBridge3/Source/LuaBridge/detail/Security.h b/LuaBridge3/Source/LuaBridge/detail/Security.h deleted file mode 100644 index d63d5c9..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Security.h +++ /dev/null @@ -1,93 +0,0 @@ -// https://github.com/vinniefalco/LuaBridge -// Copyright 2021, Lucio Asnaghi -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Stack.h" - -namespace luabridge { - -//================================================================================================= -/** - * @brief Security options. - */ -class Security -{ -public: - static bool hideMetatables() noexcept - { - return getSettings().hideMetatables; - } - - static void setHideMetatables(bool shouldHide) noexcept - { - getSettings().hideMetatables = shouldHide; - } - -private: - struct Settings - { - Settings() noexcept - : hideMetatables(true) - { - } - - bool hideMetatables; - }; - - static Settings& getSettings() noexcept - { - static Settings settings; - return settings; - } -}; - -//================================================================================================= -/** - * @brief Get a global value from the lua_State. - * - * @note This works on any type specialized by `Stack`, including `LuaRef` and its table proxies. -*/ -template -TypeResult getGlobal(lua_State* L, const char* name) -{ - lua_getglobal(L, name); - - auto result = luabridge::Stack::get(L, -1); - - lua_pop(L, 1); - - return result; -} - -//================================================================================================= -/** - * @brief Set a global value in the lua_State. - * - * @note This works on any type specialized by `Stack`, including `LuaRef` and its table proxies. -*/ -template -bool setGlobal(lua_State* L, T&& t, const char* name) -{ - if (auto result = push(L, std::forward(t))) - { - lua_setglobal(L, name); - return true; - } - - return false; -} - -//================================================================================================= -/** - * @brief Change whether or not metatables are hidden (on by default). - */ -inline void setHideMetatables(bool shouldHide) noexcept -{ - Security::setHideMetatables(shouldHide); -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Stack.h b/LuaBridge3/Source/LuaBridge/detail/Stack.h deleted file mode 100644 index 7bf02c2..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Stack.h +++ /dev/null @@ -1,1471 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#pragma once - -#include "LuaHelpers.h" -#include "Errors.h" -#include "Expected.h" -#include "Result.h" -#include "Userdata.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Stack restorer. - */ -class StackRestore final -{ -public: - StackRestore(lua_State* L) - : m_L(L) - , m_stackTop(lua_gettop(L)) - { - } - - ~StackRestore() - { - if (m_doRestoreStack) - lua_settop(m_L, m_stackTop); - } - - void reset() - { - m_doRestoreStack = false; - } - -private: - lua_State* const m_L = nullptr; - int m_stackTop = 0; - bool m_doRestoreStack = true; -}; - -//================================================================================================= -/** - * @brief Lua stack traits for C++ types. - * - * @tparam T A C++ type. - */ -template -struct Stack; - -//================================================================================================= -/** - * @brief Specialization for void type. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State*) - { - return {}; - } -}; - -//================================================================================================= -/** - * @brief Specialization for nullptr_t. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, std::nullptr_t) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushnil(L); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (! lua_isnil(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return nullptr; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_isnil(L, index); - } -}; - -//================================================================================================= -/** - * @brief Receive the lua_State* as an argument. - */ -template <> -struct Stack -{ - [[nodiscard]] static TypeResult get(lua_State* L, int) - { - return L; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for a lua_CFunction. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, lua_CFunction f) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushcfunction_x(L, f); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (! lua_iscfunction(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return lua_tocfunction(L, index); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_iscfunction(L, index); - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `bool`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, bool value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushboolean(L, value ? 1 : 0); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - return lua_toboolean(L, index) ? true : false; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_isboolean(L, index); - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `std::byte`. - */ -template <> -struct Stack -{ - static_assert(sizeof(std::byte) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, std::byte value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - pushunsigned(L, std::to_integer>(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `char`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, char value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, &value, 1); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TSTRING) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring(L, index, &length); - - if (str == nullptr || length != 1) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return str[0]; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TSTRING) - { - std::size_t len; - luaL_checklstring(L, index, &len); - return len == 1; - } - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `int8_t`. - */ -template <> -struct Stack -{ - static_assert(sizeof(int8_t) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, int8_t value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `unsigned char`. - */ -template <> -struct Stack -{ - static_assert(sizeof(unsigned char) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, unsigned char value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `short`. - */ -template <> -struct Stack -{ - static_assert(sizeof(short) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, short value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `unsigned short`. - */ -template <> -struct Stack -{ - static_assert(sizeof(unsigned short) < sizeof(lua_Integer)); - - [[nodiscard]] static Result push(lua_State* L, unsigned short value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `int`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, int value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `unsigned int`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, unsigned int value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `long`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `unsigned long`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, unsigned long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `long long`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, long long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `unsigned long long`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, unsigned long long value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - pushunsigned(L, value); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by(L, index); - - return false; - } -}; - -#if 0 // defined(__SIZEOF_INT128__) -//================================================================================================= -/** - * @brief Stack specialization for `__int128_t`. - */ -template <> -struct Stack<__int128_t> -{ - [[nodiscard]] static Result push(lua_State* L, __int128_t value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult<__int128_t> get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by<__int128_t>(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast<__int128_t>(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by<__int128_t>(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `__uint128_t`. - */ -template <> -struct Stack<__uint128_t> -{ - [[nodiscard]] static Result push(lua_State* L, __uint128_t value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_integral_representable_by(value)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - lua_pushinteger(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult<__uint128_t> get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_integral_representable_by<__uint128_t>(L, index)) - return makeErrorCode(ErrorCode::IntegerDoesntFitIntoLuaInteger); - - return static_cast<__uint128_t>(lua_tointeger(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_integral_representable_by<__uint128_t>(L, index); - - return false; - } -}; -#endif - -//================================================================================================= -/** - * @brief Stack specialization for `float`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, float value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_floating_point_representable_by(value)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - lua_pushnumber(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_floating_point_representable_by(L, index)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - return static_cast(lua_tonumber(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_floating_point_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `double`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, double value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_floating_point_representable_by(value)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - lua_pushnumber(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_floating_point_representable_by(L, index)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - return static_cast(lua_tonumber(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_floating_point_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `long double`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, long double value) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (! is_floating_point_representable_by(value)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - lua_pushnumber(L, static_cast(value)); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TNUMBER) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (! is_floating_point_representable_by(L, index)) - return makeErrorCode(ErrorCode::FloatingPointDoesntFitIntoLuaNumber); - - return static_cast(lua_tonumber(L, index)); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNUMBER) - return is_floating_point_representable_by(L, index); - - return false; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `const char*`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const char* str) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - if (str != nullptr) - lua_pushstring(L, str); - else - lua_pushlstring(L, "", 0); - - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TSTRING) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring(L, index, &length); - if (str == nullptr) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return str; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TSTRING; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `std::string_view`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, std::string_view str) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, str.data(), str.size()); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) != LUA_TSTRING) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::size_t length = 0; - const char* str = lua_tolstring(L, index, &length); - if (str == nullptr) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return std::string_view{ str, length }; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TSTRING; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `std::string`. - */ -template <> -struct Stack -{ - [[nodiscard]] static Result push(lua_State* L, const std::string& str) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, str.data(), str.size()); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - std::size_t length = 0; - const char* str = nullptr; - - if (lua_type(L, index) == LUA_TSTRING) - { - str = lua_tolstring(L, index, &length); - } - else - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - // Lua reference manual: - // If the value is a number, then lua_tolstring also changes the actual value in the stack - // to a string. (This change confuses lua_next when lua_tolstring is applied to keys during - // a table traversal) - lua_pushvalue(L, index); - str = lua_tolstring(L, -1, &length); - lua_pop(L, 1); - } - - if (str == nullptr) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return std::string{ str, length }; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TSTRING; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `std::optional`. - */ -template -struct Stack> -{ - using Type = std::optional; - - [[nodiscard]] static Result push(lua_State* L, const Type& value) - { - if (value) - { - StackRestore stackRestore(L); - - auto result = Stack::push(L, *value); - if (! result) - return result; - - stackRestore.reset(); - return {}; - } - -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushnil(L); - return {}; - } - - [[nodiscard]] static TypeResult get(lua_State* L, int index) - { - if (lua_type(L, index) == LUA_TNIL) - return std::nullopt; - - auto result = Stack::get(L, index); - if (! result) - return result.error(); - - return *result; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_isnil(L, index) || Stack::isInstance(L, index); - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `std::pair`. - */ -template -struct Stack> -{ - [[nodiscard]] static Result push(lua_State* L, const std::pair& t) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, 2, 0); - - auto result1 = push_element<0>(L, t); - if (! result1) - return result1; - - auto result2 = push_element<1>(L, t); - if (! result2) - return result2; - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult> get(lua_State* L, int index) - { - const StackRestore stackRestore(L); - - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (get_length(L, index) != 2) - return makeErrorCode(ErrorCode::InvalidTableSizeInCast); - - std::pair value; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - auto result1 = pop_element<0>(L, absIndex, value); - if (! result1) - return result1.error(); - - auto result2 = pop_element<1>(L, absIndex, value); - if (! result2) - return result2.error(); - - return value; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TTABLE && get_length(L, index) == 2; - } - -private: - template - static Result push_element(lua_State* L, const std::pair& p) - { - static_assert(Index < 2); - - using T = std::tuple_element_t>; - - lua_pushinteger(L, static_cast(Index + 1)); - - auto result = Stack::push(L, std::get(p)); - if (! result) - { - lua_pushnil(L); - lua_settable(L, -3); - return result; - } - - lua_settable(L, -3); - - return {}; - } - - template - static Result pop_element(lua_State* L, int absIndex, std::pair& p) - { - static_assert(Index < 2); - - using T = std::tuple_element_t>; - - if (lua_next(L, absIndex) == 0) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto result = Stack::get(L, -1); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::get(p) = std::move(*result); - lua_pop(L, 1); - - return {}; - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `std::tuple`. - */ -template -struct Stack> -{ - [[nodiscard]] static Result push(lua_State* L, const std::tuple& t) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 3)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(Size), 0); - - auto result = push_element(L, t); - if (! result) - return result; - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static TypeResult> get(lua_State* L, int index) - { - const StackRestore stackRestore(L); - - if (!lua_istable(L, index)) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - if (get_length(L, index) != static_cast(Size)) - return makeErrorCode(ErrorCode::InvalidTableSizeInCast); - - std::tuple value; - - int absIndex = lua_absindex(L, index); - lua_pushnil(L); - - auto result = pop_element(L, absIndex, value); - if (! result) - return result.error(); - - return value; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TTABLE && get_length(L, index) == static_cast(Size); - } - -private: - static constexpr std::size_t Size = std::tuple_size_v>; - - template - static auto push_element(lua_State*, const std::tuple&) - -> std::enable_if_t - { - return {}; - } - - template - static auto push_element(lua_State* L, const std::tuple& t) - -> std::enable_if_t - { - using T = std::tuple_element_t>; - - lua_pushinteger(L, static_cast(Index + 1)); - - auto result = Stack::push(L, std::get(t)); - if (! result) - { - lua_pushnil(L); - lua_settable(L, -3); - return result; - } - - lua_settable(L, -3); - - return push_element(L, t); - } - - template - static auto pop_element(lua_State*, int, std::tuple&) - -> std::enable_if_t - { - return {}; - } - - template - static auto pop_element(lua_State* L, int absIndex, std::tuple& t) - -> std::enable_if_t - { - using T = std::tuple_element_t>; - - if (lua_next(L, absIndex) == 0) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - auto result = Stack::get(L, -1); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - std::get(t) = std::move(*result); - lua_pop(L, 1); - - return pop_element(L, absIndex, t); - } -}; - -//================================================================================================= -/** - * @brief Stack specialization for `T[N]`. - */ -template -struct Stack -{ - static_assert(N > 0, "Unsupported zero sized array"); - - [[nodiscard]] static Result push(lua_State* L, const T (&value)[N]) - { - if constexpr (std::is_same_v) - { -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 1)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - lua_pushlstring(L, value, N - 1); - return {}; - } - -#if LUABRIDGE_SAFE_STACK_CHECKS - if (! lua_checkstack(L, 2)) - return makeErrorCode(ErrorCode::LuaStackOverflow); -#endif - - StackRestore stackRestore(L); - - lua_createtable(L, static_cast(N), 0); - - for (std::size_t i = 0; i < N; ++i) - { - lua_pushinteger(L, static_cast(i + 1)); - - auto result = Stack::push(L, value[i]); - if (! result) - return result; - - lua_settable(L, -3); - } - - stackRestore.reset(); - return {}; - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return lua_type(L, index) == LUA_TTABLE && get_length(L, index) == static_cast(N); - } -}; - -namespace detail { - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, T& value) { return Stack::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T& value) { return Stack::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, T* value) { return Stack::push(L, *value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T* value) { return Stack::push(L, *value); } - - static ReturnType get(lua_State* L, int index) { return Stack::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Stack::isInstance(L, index); } -}; - -} // namespace detail - -template -struct Stack>> -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, T& value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -struct Stack>> -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, const T& value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -struct Stack -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, T* value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -template -struct Stack -{ - using Helper = detail::StackOpSelector::value>; - using ReturnType = typename Helper::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, const T* value) { return Helper::push(L, value); } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) { return Helper::template isInstance(L, index); } -}; - -//================================================================================================= -/** - * @brief Push an object onto the Lua stack. - */ -template -[[nodiscard]] Result push(lua_State* L, const T& t) -{ - return Stack::push(L, t); -} - -//================================================================================================= -/** - * @brief Get an object from the Lua stack. - */ -template -[[nodiscard]] TypeResult get(lua_State* L, int index) -{ - return Stack::get(L, index); -} - -//================================================================================================= -/** - * @brief Check whether an object on the Lua stack is of type T. - */ -template -[[nodiscard]] bool isInstance(lua_State* L, int index) -{ - return Stack::isInstance(L, index); -} - -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/TypeTraits.h b/LuaBridge3/Source/LuaBridge/detail/TypeTraits.h deleted file mode 100644 index b043eaa..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/TypeTraits.h +++ /dev/null @@ -1,104 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" - -#include - -namespace luabridge { - -//================================================================================================= -/** - * @brief Container traits. - * - * Unspecialized ContainerTraits has the isNotContainer typedef for SFINAE. All user defined containers must supply an appropriate - * specialization for ContinerTraits (without the alias isNotContainer). The containers that come with LuaBridge also come with the - * appropriate ContainerTraits specialization. - * - * @note See the corresponding declaration for details. - * - * A specialization of ContainerTraits for some generic type ContainerType looks like this: - * - * @code - * - * template - * struct ContainerTraits> - * { - * using Type = T; - * - * static ContainerType construct(T* c) - * { - * return c; // Implementation-dependent on ContainerType - * } - * - * static T* get(const ContainerType& c) - * { - * return c.get(); // Implementation-dependent on ContainerType - * } - * }; - * - * @endcode - */ -template -struct ContainerTraits -{ - using IsNotContainer = bool; - - using Type = T; -}; - -/** - * @brief Register shared_ptr support as container. - * - * @tparam T Class that is hold by the shared_ptr, must inherit from std::enable_shared_from_this. - */ -template -struct ContainerTraits> -{ - static_assert(std::is_base_of_v, T>); - - using Type = T; - - static std::shared_ptr construct(T* t) - { - return t->shared_from_this(); - } - - static T* get(const std::shared_ptr& c) - { - return c.get(); - } -}; - -namespace detail { - -//================================================================================================= -/** - * @brief Determine if type T is a container. - * - * To be considered a container, there must be a specialization of ContainerTraits with the required fields. - */ -template -class IsContainer -{ -private: - typedef char yes[1]; // sizeof (yes) == 1 - typedef char no[2]; // sizeof (no) == 2 - - template - static constexpr no& test(typename C::IsNotContainer*); - - template - static constexpr yes& test(...); - -public: - static constexpr bool value = sizeof(test>(0)) == sizeof(yes); -}; - -} // namespace detail -} // namespace luabridge diff --git a/LuaBridge3/Source/LuaBridge/detail/Userdata.h b/LuaBridge3/Source/LuaBridge/detail/Userdata.h deleted file mode 100644 index 7d57629..0000000 --- a/LuaBridge3/Source/LuaBridge/detail/Userdata.h +++ /dev/null @@ -1,1010 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Config.h" -#include "Errors.h" -#include "LuaException.h" -#include "ClassInfo.h" -#include "TypeTraits.h" -#include "Result.h" -#include "Stack.h" - -#include -#include -#include - -namespace luabridge { -namespace detail { - -//================================================================================================= -/** - * @brief Return the identity pointer for our lightuserdata tokens. - * - * Because of Lua's dynamic typing and our improvised system of imposing C++ class structure, there is the possibility that executing - * scripts may knowingly or unknowingly cause invalid data to get passed to the C functions created by LuaBridge. - * - * In particular, our security model addresses the following: - * - * 1. Scripts cannot create a userdata (ignoring the debug lib). - * - * 2. Scripts cannot create a lightuserdata (ignoring the debug lib). - * - * 3. Scripts cannot set the metatable on a userdata. - */ - -/** - * @brief Interface to a class pointer retrievable from a userdata. - */ -class Userdata -{ -private: - //============================================================================================= - /** - * @brief Validate and retrieve a Userdata on the stack. - * - * The Userdata must exactly match the corresponding class table or const table, or else a Lua error is raised. This is used for the - * __gc metamethod. - */ - static Userdata* getExactClass(lua_State* L, int index, const void* classKey) - { - return (void)classKey, static_cast(lua_touserdata(L, lua_absindex(L, index))); - } - - //============================================================================================= - /** - * @brief Validate and retrieve a Userdata on the stack. - * - * The Userdata must be derived from or the same as the given base class, identified by the key. If canBeConst is false, generates - * an error if the resulting Userdata represents to a const object. We do the type check first so that the error message is informative. - */ - static Userdata* getClass(lua_State* L, - int index, - const void* registryConstKey, - const void* registryClassKey, - bool canBeConst) - { - index = lua_absindex(L, index); - - lua_getmetatable(L, index); // Stack: object metatable (ot) | nil - if (!lua_istable(L, -1)) - { - lua_rawgetp(L, LUA_REGISTRYINDEX, registryClassKey); // Stack: registry metatable (rt) | nil - return throwBadArg(L, index); - } - - lua_rawgetp(L, -1, getConstKey()); // Stack: ot | nil, const table (co) | nil - assert(lua_istable(L, -1) || lua_isnil(L, -1)); - - // If const table is NOT present, object is const. Use non-const registry table - // if object cannot be const, so constness validation is done automatically. - // E.g. nonConstFn (constObj) - // -> canBeConst = false, isConst = true - // -> 'Class' registry table, 'const Class' object table - // -> 'expected Class, got const Class' - bool isConst = lua_isnil(L, -1); // Stack: ot | nil, nil, rt - if (isConst && canBeConst) - { - lua_rawgetp(L, LUA_REGISTRYINDEX, registryConstKey); // Stack: ot, nil, rt - } - else - { - lua_rawgetp(L, LUA_REGISTRYINDEX, registryClassKey); // Stack: ot, co, rt - } - - lua_insert(L, -3); // Stack: rt, ot, co | nil - lua_pop(L, 1); // Stack: rt, ot - - for (;;) - { - if (lua_rawequal(L, -1, -2)) // Stack: rt, ot - { - lua_pop(L, 2); // Stack: - - return static_cast(lua_touserdata(L, index)); - } - - // Replace current metatable with it's base class. - lua_rawgetp(L, -1, getParentKey()); // Stack: rt, ot, parent ot (pot) | nil - - if (lua_isnil(L, -1)) // Stack: rt, ot, nil - { - // Drop the object metatable because it may be some parent metatable - lua_pop(L, 2); // Stack: rt - return throwBadArg(L, index); - } - - lua_remove(L, -2); // Stack: rt, pot - } - - // no return - } - - static bool isInstance(lua_State* L, int index, const void* registryClassKey) - { - index = lua_absindex(L, index); - - int result = lua_getmetatable(L, index); // Stack: object metatable (ot) | nothing - if (result == 0) - return false; // Nothing was pushed on the stack - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // Stack: - - return false; - } - - lua_rawgetp(L, LUA_REGISTRYINDEX, registryClassKey); // Stack: ot, rt - lua_insert(L, -2); // Stack: rt, ot - - for (;;) - { - if (lua_rawequal(L, -1, -2)) // Stack: rt, ot - { - lua_pop(L, 2); // Stack: - - return true; - } - - // Replace current metatable with it's base class. - lua_rawgetp(L, -1, getParentKey()); // Stack: rt, ot, parent ot (pot) | nil - - if (lua_isnil(L, -1)) // Stack: rt, ot, nil - { - lua_pop(L, 3); // Stack: - - return false; - } - - lua_remove(L, -2); // Stack: rt, pot - } - } - - static Userdata* throwBadArg(lua_State* L, int index) - { - assert(lua_istable(L, -1) || lua_isnil(L, -1)); // Stack: rt | nil - - const char* expected = 0; - if (lua_isnil(L, -1)) // Stack: nil - { - expected = "unregistered class"; - } - else - { - lua_rawgetp(L, -1, getTypeKey()); // Stack: rt, registry type - expected = lua_tostring(L, -1); - } - - const char* got = 0; - if (lua_isuserdata(L, index)) - { - lua_getmetatable(L, index); // Stack: ..., ot | nil - if (lua_istable(L, -1)) // Stack: ..., ot - { - lua_rawgetp(L, -1, getTypeKey()); // Stack: ..., ot, object type | nil - if (lua_isstring(L, -1)) - { - got = lua_tostring(L, -1); - } - } - } - - if (!got) - { - got = lua_typename(L, lua_type(L, index)); - } - - luaL_argerror(L, index, lua_pushfstring(L, "%s expected, got %s", expected, got)); - return nullptr; - } - -public: - virtual ~Userdata() {} - - //============================================================================================= - /** - * @brief Returns the Userdata* if the class on the Lua stack matches. - * - * If the class does not match, a Lua error is raised. - * - * @tparam T A registered user class. - * - * @param L A Lua state. - * @param index The index of an item on the Lua stack. - * - * @return A userdata pointer if the class matches. - */ - template - static Userdata* getExact(lua_State* L, int index) - { - return getExactClass(L, index, detail::getClassRegistryKey()); - } - - //============================================================================================= - /** - * @brief Get a pointer to the class from the Lua stack. - * - * If the object is not the class or a subclass, or it violates the const-ness, a Lua error is raised. - * - * @tparam T A registered user class. - * - * @param L A Lua state. - * @param index The index of an item on the Lua stack. - * @param canBeConst TBD - * - * @return A pointer if the class and constness match. - */ - template - static T* get(lua_State* L, int index, bool canBeConst) - { - if (lua_isnil(L, index)) - return nullptr; - - return static_cast(getClass(L, - index, - detail::getConstRegistryKey(), - detail::getClassRegistryKey(), - canBeConst) - ->getPointer()); - } - - template - static bool isInstance(lua_State* L, int index) - { - return isInstance(L, index, detail::getClassRegistryKey()); - } - -protected: - Userdata() = default; - - /** - * @brief Get an untyped pointer to the contained class. - */ - void* getPointer() const noexcept - { - return m_p; - } - - void* m_p = nullptr; // subclasses must set this -}; - -//================================================================================================= -/** - * @brief Wraps a class object stored in a Lua userdata. - * - * The lifetime of the object is managed by Lua. The object is constructed inside the userdata using placement new. - */ -template -class UserdataValue : public Userdata -{ -public: - UserdataValue(const UserdataValue&) = delete; - UserdataValue operator=(const UserdataValue&) = delete; - - ~UserdataValue() - { - if (getPointer() != nullptr) - { - getObject()->~T(); - } - } - - /** - * @brief Push a T via placement new. - * - * The caller is responsible for calling placement new using the returned uninitialized storage. - * - * @param L A Lua state. - * - * @return An object referring to the newly created userdata value. - */ - static UserdataValue* place(lua_State* L, std::error_code& ec) - { - auto* ud = new (lua_newuserdata_x>(L, sizeof(UserdataValue))) UserdataValue(); - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - ud->~UserdataValue(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - ec = throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - ec = makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - - return nullptr; - } - - lua_setmetatable(L, -2); - - return ud; - } - - /** - * @brief Push T via copy construction from U. - * - * @tparam U A container type. - * - * @param L A Lua state. - * @param u A container object l-value reference. - * @param ec Error code that will be set in case of failure to push on the lua stack. - */ - template - static auto push(lua_State* L, const U& u) -> std::enable_if_t, Result> - { - std::error_code ec; - auto* ud = place(L, ec); - - if (!ud) - return ec; - - new (ud->getObject()) U(u); - - ud->commit(); - - return {}; - } - - /** - * @brief Push T via move construction from U. - * - * @tparam U A container type. - * - * @param L A Lua state. - * @param u A container object r-value reference. - * @param ec Error code that will be set in case of failure to push on the lua stack. - */ - template - static auto push(lua_State* L, U&& u) -> std::enable_if_t, Result> - { - std::error_code ec; - auto* ud = place(L, ec); - - if (!ud) - return ec; - - new (ud->getObject()) U(std::move(u)); - - ud->commit(); - - return {}; - } - - /** - * @brief Confirm object construction. - */ - void commit() noexcept - { - m_p = getObject(); - } - - T* getObject() noexcept - { - // If this fails to compile it means you forgot to provide - // a Container specialization for your container! - return reinterpret_cast(&m_storage); - } - -private: - /** - * @brief Used for placement construction. - */ - UserdataValue() noexcept - : Userdata() - { - } - - std::aligned_storage_t m_storage; -}; - -//================================================================================================= -/** - * @brief Wraps a pointer to a class object inside a Lua userdata. - * - * The lifetime of the object is managed by C++. - */ -class UserdataPtr : public Userdata -{ -public: - UserdataPtr(const UserdataPtr&) = delete; - UserdataPtr operator=(const UserdataPtr&) = delete; - - /** - * @brief Push non-const pointer to object. - * - * @tparam T A user registered class. - * - * @param L A Lua state. - * @param p A pointer to the user class instance. - * @param ec Error code that will be set in case of failure to push on the lua stack. - */ - template - static Result push(lua_State* L, T* ptr) - { - if (ptr) - return push(L, ptr, getClassRegistryKey()); - - lua_pushnil(L); - return {}; - } - - /** - * @brief Push const pointer to object. - * - * @tparam T A user registered class. - * - * @param L A Lua state. - * @param p A pointer to the user class instance. - * @param ec Error code that will be set in case of failure to push on the lua stack. - */ - template - static Result push(lua_State* L, const T* ptr) - { - if (ptr) - return push(L, ptr, getConstRegistryKey()); - - lua_pushnil(L); - return {}; - } - -private: - /** - * @brief Push a pointer to object using metatable key. - */ - static Result push(lua_State* L, const void* ptr, const void* key) - { - auto* udptr = new (lua_newuserdata_x(L, sizeof(UserdataPtr))) UserdataPtr(const_cast(ptr)); - - lua_rawgetp(L, LUA_REGISTRYINDEX, key); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - udptr->~UserdataPtr(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - - return {}; - } - - explicit UserdataPtr(void* ptr) - { - // Can't construct with a null object! - assert(ptr != nullptr); - m_p = ptr; - } -}; - -//============================================================================ -/** - * @brief Wraps an external value type to a class object inside a Lua userdata. - * - * The lifetime of the object is managed by Lua. The object is constructed inside the userdata using an - * already constructed object provided externally, and it is destructed by a deallocator function provided. - */ -template -class UserdataValueExternal : public Userdata -{ -public: - UserdataValueExternal(const UserdataValueExternal&) = delete; - UserdataValueExternal operator=(const UserdataValueExternal&) = delete; - - ~UserdataValueExternal() - { - if (getObject() != nullptr) - m_dealloc(getObject()); - } - - /** - * @brief Push a T via externally allocated object. - * - * @param L A Lua state. - * @param obj The object allocated externally that need to be stored. - * @param dealloc A deallocator function that will free the passed object. - * - * @return An object referring to the newly created userdata value. - */ - template - static UserdataValueExternal* place(lua_State* L, T* obj, Dealloc dealloc, std::error_code& ec) - { - auto* ud = new (lua_newuserdata_x>(L, sizeof(UserdataValueExternal))) UserdataValueExternal(obj, dealloc); - - lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - ud->~UserdataValueExternal(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - ec = throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - ec = makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - - return nullptr; - } - - lua_setmetatable(L, -2); - - return ud; - } - - T* getObject() noexcept - { - return static_cast(m_p); - } - -private: - UserdataValueExternal(void* ptr, void (*dealloc)(T*)) noexcept - { - // Can't construct with a null object! - assert(ptr != nullptr); - m_p = ptr; - - // Can't construct with a null deallocator! - assert(dealloc != nullptr); - m_dealloc = dealloc; - } - - void (*m_dealloc)(T*) = nullptr; -}; - -//============================================================================ -/** - * @brief Wraps a container that references a class object. - * - * The template argument C is the container type, ContainerTraits must be specialized on C or else a compile error will result. - */ -template -class UserdataShared : public Userdata -{ -public: - UserdataShared(const UserdataShared&) = delete; - UserdataShared& operator=(const UserdataShared&) = delete; - - ~UserdataShared() = default; - - /** - * @brief Construct from a container to the class or a derived class. - * - * @tparam U A container type. - * - * @param u A container object reference. - */ - template - explicit UserdataShared(const U& u) : m_c(u) - { - m_p = const_cast(reinterpret_cast((ContainerTraits::get(m_c)))); - } - - /** - * @brief Construct from a pointer to the class or a derived class. - * - * @tparam U A container type. - * - * @param u A container object pointer. - */ - template - explicit UserdataShared(U* u) : m_c(u) - { - m_p = const_cast(reinterpret_cast((ContainerTraits::get(m_c)))); - } - -private: - C m_c; -}; - -//================================================================================================= -/** - * @brief SFINAE helper for non-const objects. - */ -template -struct UserdataSharedHelper -{ - using T = std::remove_const_t::Type>; - - static Result push(lua_State* L, const C& c) - { - if (ContainerTraits::get(c) != nullptr) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(c); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } - - static Result push(lua_State* L, T* t) - { - if (t) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(t); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getClassRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } -}; - -/** - * @brief SFINAE helper for const objects. - */ -template -struct UserdataSharedHelper -{ - using T = std::remove_const_t::Type>; - - static Result push(lua_State* L, const C& c) - { - if (ContainerTraits::get(c) != nullptr) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(c); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getConstRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } - - static Result push(lua_State* L, T* t) - { - if (t) - { - auto* us = new (lua_newuserdata_x>(L, sizeof(UserdataShared))) UserdataShared(t); - - lua_rawgetp(L, LUA_REGISTRYINDEX, getConstRegistryKey()); - - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); // possibly: a nil - - us->~UserdataShared(); - -#if LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE - return throw_or_error_code(L, ErrorCode::ClassNotRegistered); -#else - return makeErrorCode(ErrorCode::ClassNotRegistered); -#endif - } - - lua_setmetatable(L, -2); - } - else - { - lua_pushnil(L); - } - - return {}; - } -}; - -//================================================================================================= -/** - * @brief Pass by container. - * - * The container controls the object lifetime. Typically this will be a lifetime shared by C++ and Lua using a reference count. Because of type - * erasure, containers like std::shared_ptr will not work, unless the type hold by them is derived from std::enable_shared_from_this. - */ -template -struct StackHelper -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T& t) - { - return UserdataSharedHelper::Type>>::push(L, t); - } - - static ReturnType get(lua_State* L, int index) - { - using CastType = std::remove_const_t::Type>; - - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return ContainerTraits::construct(result); - } -}; - -/** - * @brief Pass by value. - * - * Lifetime is managed by Lua. A C++ function which accesses a pointer or reference to an object outside the activation record in which it was - * retrieved may result in undefined behavior if Lua garbage collected it. - */ -template -struct StackHelper -{ - static Result push(lua_State* L, const T& t) - { - return UserdataValue::push(L, t); - } - - static Result push(lua_State* L, T&& t) - { - return UserdataValue::push(L, std::move(t)); - } - - static TypeResult> get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); // nil passed to reference - - return std::cref(*result); - } -}; - -//================================================================================================= -/** - * @brief Lua stack conversions for pointers and references to class objects. - * - * Lifetime is managed by C++. Lua code which remembers a reference to the value may result in undefined behavior if C++ destroys the object. - * The handling of the const and volatile qualifiers happens in UserdataPtr. - */ -template -struct RefStackHelper -{ - using ReturnType = TypeResult; - using T = std::remove_const_t::Type>; - - static Result push(lua_State* L, const C& t) - { - return UserdataSharedHelper::Type>>::push(L, t); - } - - static ReturnType get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return ContainerTraits::construct(result); - } -}; - -template -struct RefStackHelper -{ - using ReturnType = TypeResult>; - - static Result push(lua_State* L, const T& t) - { - return UserdataPtr::push(L, std::addressof(t)); - } - - static ReturnType get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); // nil passed to reference - - return std::ref(*result); - } -}; - -//================================================================================================= -/** - * @brief Trait class that selects whether to return a user registered class object by value or by reference. - */ -template -struct UserdataGetter -{ - using ReturnType = TypeResult; - - static ReturnType get(lua_State* L, int index) - { - auto* result = Userdata::get(L, index, true); - if (! result) - return makeErrorCode(ErrorCode::InvalidTypeCast); - - return result; - } -}; - -template -struct UserdataGetter> -{ - using ReturnType = TypeResult; - - static ReturnType get(lua_State* L, int index) - { - auto result = StackHelper::value>::get(L, index); - if (! result) - return result.error(); - - return *result; - } -}; - -} // namespace detail - -//================================================================================================= -/** - * @brief Lua stack conversions for class objects passed by value. - */ -template -struct Stack -{ - using IsUserdata = void; - - using Getter = detail::UserdataGetter; - using ReturnType = typename Getter::ReturnType; - - [[nodiscard]] static Result push(lua_State* L, const T& value) - { - return detail::StackHelper::value>::push(L, value); - } - - [[nodiscard]] static Result push(lua_State* L, T&& value) - { - return detail::StackHelper::value>::push(L, std::move(value)); - } - - [[nodiscard]] static ReturnType get(lua_State* L, int index) - { - return Getter::get(L, index); - } - - [[nodiscard]] static bool isInstance(lua_State* L, int index) - { - return detail::Userdata::isInstance(L, index); - } -}; - -namespace detail { - -//================================================================================================= -/** - * @brief Trait class indicating whether the parameter type must be a user registered class. - * - * The trait checks the existence of member type Stack::IsUserdata specialization for detection. - */ -template -struct IsUserdata : std::false_type -{ -}; - -template -struct IsUserdata::IsUserdata>> : std::true_type -{ -}; - -//================================================================================================= -/** - * @brief Trait class that selects a specific push/get implementation for userdata. - */ -template -struct StackOpSelector; - -// pointer -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, T* value) { return UserdataPtr::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Userdata::get(L, index, false); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -// pointer to const -template -struct StackOpSelector -{ - using ReturnType = TypeResult; - - static Result push(lua_State* L, const T* value) { return UserdataPtr::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Userdata::get(L, index, true); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -// l-value reference -template -struct StackOpSelector -{ - using Helper = RefStackHelper::value>; - using ReturnType = typename Helper::ReturnType; - - static Result push(lua_State* L, T& value) { return UserdataPtr::push(L, &value); } - - static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -// l-value reference to const -template -struct StackOpSelector -{ - using Helper = RefStackHelper::value>; - using ReturnType = typename Helper::ReturnType; - - static Result push(lua_State* L, const T& value) { return Helper::push(L, value); } - - static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); } - - static bool isInstance(lua_State* L, int index) { return Userdata::isInstance(L, index); } -}; - -} // namespace detail -} // namespace luabridge diff --git a/LuaBridge3/Tests/CMakeLists.txt b/LuaBridge3/Tests/CMakeLists.txt deleted file mode 100644 index 1f235ab..0000000 --- a/LuaBridge3/Tests/CMakeLists.txt +++ /dev/null @@ -1,293 +0,0 @@ -cmake_minimum_required (VERSION 3.5) - -# ====================================================== Locations - -set (LUABRIDGE_LUAJIT_LOCATION "${CMAKE_CURRENT_LIST_DIR}/Lua/LuaJIT.2.1") -set (LUABRIDGE_LUAU_LOCATION "${CMAKE_CURRENT_LIST_DIR}/../ThirdParty/luau") - -add_subdirectory(${LUABRIDGE_LUAJIT_LOCATION}) - -# ====================================================== Unit Tests Files - -set (LUABRIDGE_TEST_SOURCE_FILES - Source/AmalgamateTests.cpp - Source/ArrayTests.cpp - Source/ClassTests.cpp - Source/ExceptionTests.cpp - Source/IssueTests.cpp - Source/IteratorTests.cpp - Source/LegacyTests.cpp - Source/LegacyTests.h - Source/ListTests.cpp - Source/LuaRefTests.cpp - Source/MapTests.cpp - Source/NamespaceTests.cpp - Source/OptionalTests.cpp - Source/OverloadTests.cpp - Source/PairTests.cpp - Source/PerformanceTests.cpp - Source/RefCountedPtrTests.cpp - Source/ScopeGuardTests.cpp - Source/StackTests.cpp - Source/Tests.cpp - Source/TestBase.h - Source/TestTypes.h - Source/TestsMain.cpp - Source/UnorderedMapTests.cpp - Source/UserdataTests.cpp - Source/VectorTests.cpp -) - -if (APPLE) - list(APPEND LUABRIDGE_TEST_SOURCE_FILES Source/ObjCTests.mm) -endif () - -source_group ("Source Files" FILES ${LUABRIDGE_TEST_SOURCE_FILES}) - -# ====================================================== Lua 5.1 - -file (GLOB_RECURSE LUABRIDGE_TEST_LUA51_FILES - Lua/LuaLibrary.h - Lua/LuaLibrary5.1.5.cpp -) - -# ====================================================== Lua 5.2 - -file (GLOB_RECURSE LUABRIDGE_TEST_LUA52_FILES - Lua/LuaLibrary.h - Lua/LuaLibrary5.2.4.cpp -) - -# ====================================================== Lua 5.3 - -file (GLOB_RECURSE LUABRIDGE_TEST_LUA53_FILES - Lua/LuaLibrary.h - Lua/LuaLibrary5.3.6.cpp -) - -# ====================================================== Lua 5.4 - -file (GLOB_RECURSE LUABRIDGE_TEST_LUA54_FILES - Lua/LuaLibrary.h - Lua/LuaLibrary5.4.4.cpp -) - -# ====================================================== Luau - -file (GLOB_RECURSE LUABRIDGE_TEST_LUAU_FILES - Lua/LuaLibrary.h - Lua/Luau.cpp - Lua/LuauSplit.cpp -) - -# ====================================================== LuaJIT - -file (GLOB_RECURSE LUABRIDGE_TEST_LUAJIT_FILES - Lua/LuaLibrary.h -) - -# ====================================================== Shared Library - -file (GLOB_RECURSE LUABRIDGE_TEST_SHARED_LIBRARY_FILES - Source/SharedCode.ch - Source/SharedCode.cpp -) - -# ====================================================== Coverage - -function (setup_target_for_coverage TARGET_NAME SOURCE_LOCATION SOURCE_PACKAGE) - if ("${CMAKE_GENERATOR}" STREQUAL "Xcode") - set_target_properties (${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS "YES") - set_target_properties (${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_INSTRUMENT_PROGRAM_FLOW_ARCS "YES") - set_target_properties (${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_GENERATE_TEST_COVERAGE_FILES "YES") - else () - target_compile_options (${TARGET_NAME} PRIVATE -fprofile-arcs -ftest-coverage) - target_link_options (${TARGET_NAME} PRIVATE -fprofile-arcs) - endif () - - add_custom_command(TARGET ${TARGET_NAME} PRE_BUILD - COMMAND ${FIND_EXECUTABLE} . -path "*/Tests/*/${TARGET_NAME}.*/*/*.gcda" -delete - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - VERBATIM) - - add_custom_command ( - OUTPUT "coverage/${TARGET_NAME}.info" - COMMAND ${FIND_EXECUTABLE} . -path "*/Tests/*/${TARGET_NAME}.*/*/*.gcda" -delete - COMMAND ${TARGET_NAME} ${ARGV3} - COMMAND ${CMAKE_COMMAND} -E make_directory coverage - COMMAND ${CMAKE_COMMAND} -E rm -f coverage/${TARGET_NAME}.info - COMMAND ${LCOV_EXECUTABLE} - -c -d "${CMAKE_BINARY_DIR}" - --include "*/${SOURCE_PACKAGE}/*" - --exclude "*/Tests/*" - --exclude "*/Distribution/*" - --exclude "*/coverage_html/*" - --rc lcov_branch_coverage=1 - -o "coverage/${TARGET_NAME}.info" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - VERBATIM) - - # Message status - message (STATUS "Luabridge3 -- Enabled code coverage reporting for ${TARGET_NAME}") -endfunction () - -function (setup_coverage_single_target) - add_custom_target (LuaBridgeTestsCoverage - COMMAND ${CMAKE_COMMAND} -E make_directory coverage_html - COMMAND ${CMAKE_COMMAND} -E rm -Rf coverage_html/* - COMMAND ${CMAKE_COMMAND} -E rm -f coverage/Merged.info - COMMAND ${LCOV_EXECUTABLE} - -a "coverage/LuaBridgeTests51.info" - -a "coverage/LuaBridgeTests51Noexcept.info" - -a "coverage/LuaBridgeTests52.info" - -a "coverage/LuaBridgeTests52Noexcept.info" - -a "coverage/LuaBridgeTests53.info" - -a "coverage/LuaBridgeTests53Noexcept.info" - -a "coverage/LuaBridgeTests54.info" - -a "coverage/LuaBridgeTests54Noexcept.info" - -a "coverage/LuaBridgeTestsLuaJIT.info" - -a "coverage/LuaBridgeTestsLuaJITNoexcept.info" - -a "coverage/LuaBridgeTestsLuau.info" - -o "coverage/Merged.info" - COMMAND ${GENHTML_EXECUTABLE} - --rc lcov_branch_coverage=1 - "coverage/Merged.info" -o "coverage_html" - DEPENDS - "coverage/LuaBridgeTests51.info" - "coverage/LuaBridgeTests51Noexcept.info" - "coverage/LuaBridgeTests52.info" - "coverage/LuaBridgeTests52Noexcept.info" - "coverage/LuaBridgeTests53.info" - "coverage/LuaBridgeTests53Noexcept.info" - "coverage/LuaBridgeTests54.info" - "coverage/LuaBridgeTests54Noexcept.info" - "coverage/LuaBridgeTestsLuaJIT.info" - "coverage/LuaBridgeTestsLuaJITNoexcept.info" - "coverage/LuaBridgeTestsLuau.info" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - VERBATIM) - -endfunction () - -# ====================================================== Macro - -macro (add_test_app LUABRIDGE_TEST_NAME LUA_VERSION LUABRIDGE_TEST_LUA_LIBRARY_FILES LUABRIDGE_EXCEPTIONS LUABRIDGE_LIBS) - get_filename_component (SOURCE_LOCATION "${CMAKE_CURRENT_LIST_DIR}/../Source" ABSOLUTE) - - if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /W3 /MP") - else () - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") - endif () - - # Dynamic library test - set(LUABRIDGE_TESTLIB_NAME ${LUABRIDGE_TEST_NAME}_DynamicLibrary) - add_library (${LUABRIDGE_TESTLIB_NAME} SHARED - ${LUABRIDGE_TEST_SHARED_LIBRARY_FILES} - ${LUABRIDGE_TEST_LUA_LIBRARY_FILES}) - - target_compile_definitions (${LUABRIDGE_TESTLIB_NAME} PRIVATE - LUABRIDGEDEMO_SHARED_EXPORT=1) - - target_include_directories (${LUABRIDGE_TESTLIB_NAME} PRIVATE - ${CMAKE_CURRENT_LIST_DIR} - ${SOURCE_LOCATION} - Source) - - list(APPEND LUABRIDGE_TEST_SOURCE_FILES Source/DynamicLibraryTests.cpp) - - # Main binary test - add_executable (${LUABRIDGE_TEST_NAME} - ${LUABRIDGE_TEST_SOURCE_FILES} - ${LUABRIDGE_TEST_LUA_LIBRARY_FILES} - ) - - if (LUABRIDGE_COVERAGE) - setup_target_for_coverage (${LUABRIDGE_TEST_NAME} ${SOURCE_LOCATION} LuaBridge) - endif () - - target_include_directories (${LUABRIDGE_TEST_NAME} PRIVATE - ${CMAKE_CURRENT_LIST_DIR} - ${SOURCE_LOCATION} - Source) - - if (${LUA_VERSION} STREQUAL "LUAU") - target_include_directories (${LUABRIDGE_TEST_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/VM/include") - target_include_directories (${LUABRIDGE_TEST_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/Ast/include") - target_include_directories (${LUABRIDGE_TEST_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/Compiler/include") - target_include_directories (${LUABRIDGE_TEST_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/Common/include") - target_compile_definitions (${LUABRIDGE_TEST_NAME} PRIVATE LUABRIDGEDEMO_LUAU=1) - - target_include_directories (${LUABRIDGE_TESTLIB_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/VM/include") - target_include_directories (${LUABRIDGE_TESTLIB_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/Ast/include") - target_include_directories (${LUABRIDGE_TESTLIB_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/Compiler/include") - target_include_directories (${LUABRIDGE_TESTLIB_NAME} PRIVATE "${LUABRIDGE_LUAU_LOCATION}/Common/include") - target_compile_definitions (${LUABRIDGE_TESTLIB_NAME} PRIVATE LUABRIDGEDEMO_LUAU=1) - elseif (${LUA_VERSION} STREQUAL "LUAJIT") - target_compile_definitions (${LUABRIDGE_TEST_NAME} PRIVATE LUABRIDGEDEMO_LUAJIT=1) - target_compile_definitions (${LUABRIDGE_TESTLIB_NAME} PRIVATE LUABRIDGEDEMO_LUAJIT=1) - else () # if(${LUA_VERSION} MATCHES "^[0-9]*") - target_compile_definitions (${LUABRIDGE_TEST_NAME} PRIVATE LUABRIDGEDEMO_LUA_VERSION=${LUA_VERSION}) - target_compile_definitions (${LUABRIDGE_TESTLIB_NAME} PRIVATE LUABRIDGEDEMO_LUA_VERSION=${LUA_VERSION}) - endif () - - if (WIN32) - set(LUABRIDGEDEMO_SHARED_LIBRARY "${LUABRIDGE_TESTLIB_NAME}.dll") - elseif (APPLE) - set(LUABRIDGEDEMO_SHARED_LIBRARY "lib${LUABRIDGE_TESTLIB_NAME}.dylib") - else () - set(LUABRIDGEDEMO_SHARED_LIBRARY "lib${LUABRIDGE_TESTLIB_NAME}.so") - endif () - target_compile_definitions (${LUABRIDGE_TEST_NAME} PRIVATE - LUABRIDGEDEMO_SHARED_LIBRARY="${LUABRIDGEDEMO_SHARED_LIBRARY}" - LUABRIDGEDEMO_SHARED_EXPORT=0) - - if (NOT ${LUABRIDGE_EXCEPTIONS}) - target_compile_definitions (${LUABRIDGE_TEST_NAME} PRIVATE LUA_USE_LONGJMP=1) - if (APPLE) - target_compile_options (${LUABRIDGE_TEST_NAME} PRIVATE -fno-exceptions) - set_target_properties (${LUABRIDGE_TEST_NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_ENABLE_CPP_EXCEPTIONS "NO") - elseif (WIN32) - target_compile_options (${LUABRIDGE_TEST_NAME} PRIVATE /EHs-c-) - target_compile_definitions (${LUABRIDGE_TEST_NAME} PRIVATE _HAS_EXCEPTIONS=0) - else () - target_compile_options (${LUABRIDGE_TEST_NAME} PRIVATE -fno-exceptions) - endif () - endif () - - target_link_libraries (${LUABRIDGE_TEST_NAME} PRIVATE LuaBridge gtest ${CMAKE_DL_LIBS}) - add_dependencies (${LUABRIDGE_TEST_NAME} ${LUABRIDGE_TESTLIB_NAME}) - - if ("${LUABRIDGE_LIBS}" STREQUAL "") - else () - target_link_libraries (${LUABRIDGE_TEST_NAME} PRIVATE ${LUABRIDGE_LIBS}) - target_link_libraries (${LUABRIDGE_TESTLIB_NAME} PRIVATE ${LUABRIDGE_LIBS}) - endif () - - add_test(NAME ${LUABRIDGE_TEST_NAME} COMMAND ${LUABRIDGE_TEST_NAME}) - -endmacro (add_test_app) - -# ====================================================== Real Unit Tests - -add_test_app (LuaBridgeTests51 501 "${LUABRIDGE_TEST_LUA51_FILES}" 1 "") -add_test_app (LuaBridgeTests51Noexcept 501 "${LUABRIDGE_TEST_LUA51_FILES}" 0 "") - -add_test_app (LuaBridgeTests52 502 "${LUABRIDGE_TEST_LUA52_FILES}" 1 "") -add_test_app (LuaBridgeTests52Noexcept 502 "${LUABRIDGE_TEST_LUA52_FILES}" 0 "") - -add_test_app (LuaBridgeTests53 503 "${LUABRIDGE_TEST_LUA53_FILES}" 1 "") -add_test_app (LuaBridgeTests53Noexcept 503 "${LUABRIDGE_TEST_LUA53_FILES}" 0 "") - -add_test_app (LuaBridgeTests54 504 "${LUABRIDGE_TEST_LUA54_FILES}" 1 "") -add_test_app (LuaBridgeTests54Noexcept 504 "${LUABRIDGE_TEST_LUA54_FILES}" 0 "") - -add_test_app (LuaBridgeTestsLuaJIT "LUAJIT" "${LUABRIDGE_TEST_LUAJIT_FILES}" 1 "liblua-static") -add_test_app (LuaBridgeTestsLuaJITNoexcept "LUAJIT" "${LUABRIDGE_TEST_LUAJIT_FILES}" 0 "liblua-static") - -add_test_app (LuaBridgeTestsLuau "LUAU" "${LUABRIDGE_TEST_LUAU_FILES}" 1 "") -#add_test_app (LuaBridgeTestsLuauNoexcept "LUAU" "${LUABRIDGE_TEST_LUAU_FILES}" 0 "") - -if (LUABRIDGE_COVERAGE) - setup_coverage_single_target () -endif () diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/COPYRIGHT b/LuaBridge3/Tests/Lua/Lua.5.1.5/COPYRIGHT deleted file mode 100644 index a860268..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/COPYRIGHT +++ /dev/null @@ -1,34 +0,0 @@ -Lua License ------------ - -Lua is licensed under the terms of the MIT license reproduced below. -This means that Lua is free software and can be used for both academic -and commercial purposes at absolutely no cost. - -For details and rationale, see http://www.lua.org/license.html . - -=============================================================================== - -Copyright (C) 1994-2012 Lua.org, PUC-Rio. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=============================================================================== - -(end of COPYRIGHT) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/HISTORY b/LuaBridge3/Tests/Lua/Lua.5.1.5/HISTORY deleted file mode 100644 index ce0c95b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/HISTORY +++ /dev/null @@ -1,183 +0,0 @@ -HISTORY for Lua 5.1 - -* Changes from version 5.0 to 5.1 - ------------------------------- - Language: - + new module system. - + new semantics for control variables of fors. - + new semantics for setn/getn. - + new syntax/semantics for varargs. - + new long strings and comments. - + new `mod' operator (`%') - + new length operator #t - + metatables for all types - API: - + new functions: lua_createtable, lua_get(set)field, lua_push(to)integer. - + user supplies memory allocator (lua_open becomes lua_newstate). - + luaopen_* functions must be called through Lua. - Implementation: - + new configuration scheme via luaconf.h. - + incremental garbage collection. - + better handling of end-of-line in the lexer. - + fully reentrant parser (new Lua function `load') - + better support for 64-bit machines. - + native loadlib support for Mac OS X. - + standard distribution in only one library (lualib.a merged into lua.a) - -* Changes from version 4.0 to 5.0 - ------------------------------- - Language: - + lexical scoping. - + Lua coroutines. - + standard libraries now packaged in tables. - + tags replaced by metatables and tag methods replaced by metamethods, - stored in metatables. - + proper tail calls. - + each function can have its own global table, which can be shared. - + new __newindex metamethod, called when we insert a new key into a table. - + new block comments: --[[ ... ]]. - + new generic for. - + new weak tables. - + new boolean type. - + new syntax "local function". - + (f()) returns the first value returned by f. - + {f()} fills a table with all values returned by f. - + \n ignored in [[\n . - + fixed and-or priorities. - + more general syntax for function definition (e.g. function a.x.y:f()...end). - + more general syntax for function calls (e.g. (print or write)(9)). - + new functions (time/date, tmpfile, unpack, require, load*, etc.). - API: - + chunks are loaded by using lua_load; new luaL_loadfile and luaL_loadbuffer. - + introduced lightweight userdata, a simple "void*" without a metatable. - + new error handling protocol: the core no longer prints error messages; - all errors are reported to the caller on the stack. - + new lua_atpanic for host cleanup. - + new, signal-safe, hook scheme. - Implementation: - + new license: MIT. - + new, faster, register-based virtual machine. - + support for external multithreading and coroutines. - + new and consistent error message format. - + the core no longer needs "stdio.h" for anything (except for a single - use of sprintf to convert numbers to strings). - + lua.c now runs the environment variable LUA_INIT, if present. It can - be "@filename", to run a file, or the chunk itself. - + support for user extensions in lua.c. - sample implementation given for command line editing. - + new dynamic loading library, active by default on several platforms. - + safe garbage-collector metamethods. - + precompiled bytecodes checked for integrity (secure binary dostring). - + strings are fully aligned. - + position capture in string.find. - + read('*l') can read lines with embedded zeros. - -* Changes from version 3.2 to 4.0 - ------------------------------- - Language: - + new "break" and "for" statements (both numerical and for tables). - + uniform treatment of globals: globals are now stored in a Lua table. - + improved error messages. - + no more '$debug': full speed *and* full debug information. - + new read form: read(N) for next N bytes. - + general read patterns now deprecated. - (still available with -DCOMPAT_READPATTERNS.) - + all return values are passed as arguments for the last function - (old semantics still available with -DLUA_COMPAT_ARGRET) - + garbage collection tag methods for tables now deprecated. - + there is now only one tag method for order. - API: - + New API: fully re-entrant, simpler, and more efficient. - + New debug API. - Implementation: - + faster than ever: cleaner virtual machine and new hashing algorithm. - + non-recursive garbage-collector algorithm. - + reduced memory usage for programs with many strings. - + improved treatment for memory allocation errors. - + improved support for 16-bit machines (we hope). - + code now compiles unmodified as both ANSI C and C++. - + numbers in bases other than 10 are converted using strtoul. - + new -f option in Lua to support #! scripts. - + luac can now combine text and binaries. - -* Changes from version 3.1 to 3.2 - ------------------------------- - + redirected all output in Lua's core to _ERRORMESSAGE and _ALERT. - + increased limit on the number of constants and globals per function - (from 2^16 to 2^24). - + debugging info (lua_debug and hooks) moved into lua_state and new API - functions provided to get and set this info. - + new debug lib gives full debugging access within Lua. - + new table functions "foreachi", "sort", "tinsert", "tremove", "getn". - + new io functions "flush", "seek". - -* Changes from version 3.0 to 3.1 - ------------------------------- - + NEW FEATURE: anonymous functions with closures (via "upvalues"). - + new syntax: - - local variables in chunks. - - better scope control with DO block END. - - constructors can now be also written: { record-part; list-part }. - - more general syntax for function calls and lvalues, e.g.: - f(x).y=1 - o:f(x,y):g(z) - f"string" is sugar for f("string") - + strings may now contain arbitrary binary data (e.g., embedded zeros). - + major code re-organization and clean-up; reduced module interdependecies. - + no arbitrary limits on the total number of constants and globals. - + support for multiple global contexts. - + better syntax error messages. - + new traversal functions "foreach" and "foreachvar". - + the default for numbers is now double. - changing it to use floats or longs is easy. - + complete debug information stored in pre-compiled chunks. - + sample interpreter now prompts user when run interactively, and also - handles control-C interruptions gracefully. - -* Changes from version 2.5 to 3.0 - ------------------------------- - + NEW CONCEPT: "tag methods". - Tag methods replace fallbacks as the meta-mechanism for extending the - semantics of Lua. Whereas fallbacks had a global nature, tag methods - work on objects having the same tag (e.g., groups of tables). - Existing code that uses fallbacks should work without change. - + new, general syntax for constructors {[exp] = exp, ... }. - + support for handling variable number of arguments in functions (varargs). - + support for conditional compilation ($if ... $else ... $end). - + cleaner semantics in API simplifies host code. - + better support for writing libraries (auxlib.h). - + better type checking and error messages in the standard library. - + luac can now also undump. - -* Changes from version 2.4 to 2.5 - ------------------------------- - + io and string libraries are now based on pattern matching; - the old libraries are still available for compatibility - + dofile and dostring can now return values (via return statement) - + better support for 16- and 64-bit machines - + expanded documentation, with more examples - -* Changes from version 2.2 to 2.4 - ------------------------------- - + external compiler creates portable binary files that can be loaded faster - + interface for debugging and profiling - + new "getglobal" fallback - + new functions for handling references to Lua objects - + new functions in standard lib - + only one copy of each string is stored - + expanded documentation, with more examples - -* Changes from version 2.1 to 2.2 - ------------------------------- - + functions now may be declared with any "lvalue" as a name - + garbage collection of functions - + support for pipes - -* Changes from version 1.1 to 2.1 - ------------------------------- - + object-oriented support - + fallbacks - + simplified syntax for tables - + many internal improvements - -(end of HISTORY) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/INSTALL b/LuaBridge3/Tests/Lua/Lua.5.1.5/INSTALL deleted file mode 100644 index 17eb8ae..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/INSTALL +++ /dev/null @@ -1,99 +0,0 @@ -INSTALL for Lua 5.1 - -* Building Lua - ------------ - Lua is built in the src directory, but the build process can be - controlled from the top-level Makefile. - - Building Lua on Unix systems should be very easy. First do "make" and - see if your platform is listed. If so, just do "make xxx", where xxx - is your platform name. The platforms currently supported are: - aix ansi bsd freebsd generic linux macosx mingw posix solaris - - If your platform is not listed, try the closest one or posix, generic, - ansi, in this order. - - See below for customization instructions and for instructions on how - to build with other Windows compilers. - - If you want to check that Lua has been built correctly, do "make test" - after building Lua. Also, have a look at the example programs in test. - -* Installing Lua - -------------- - Once you have built Lua, you may want to install it in an official - place in your system. In this case, do "make install". The official - place and the way to install files are defined in Makefile. You must - have the right permissions to install files. - - If you want to build and install Lua in one step, do "make xxx install", - where xxx is your platform name. - - If you want to install Lua locally, then do "make local". This will - create directories bin, include, lib, man, and install Lua there as - follows: - - bin: lua luac - include: lua.h luaconf.h lualib.h lauxlib.h lua.hpp - lib: liblua.a - man/man1: lua.1 luac.1 - - These are the only directories you need for development. - - There are man pages for lua and luac, in both nroff and html, and a - reference manual in html in doc, some sample code in test, and some - useful stuff in etc. You don't need these directories for development. - - If you want to install Lua locally, but in some other directory, do - "make install INSTALL_TOP=xxx", where xxx is your chosen directory. - - See below for instructions for Windows and other systems. - -* Customization - ------------- - Three things can be customized by editing a file: - - Where and how to install Lua -- edit Makefile. - - How to build Lua -- edit src/Makefile. - - Lua features -- edit src/luaconf.h. - - You don't actually need to edit the Makefiles because you may set the - relevant variables when invoking make. - - On the other hand, if you need to select some Lua features, you'll need - to edit src/luaconf.h. The edited file will be the one installed, and - it will be used by any Lua clients that you build, to ensure consistency. - - We strongly recommend that you enable dynamic loading. This is done - automatically for all platforms listed above that have this feature - (and also Windows). See src/luaconf.h and also src/Makefile. - -* Building Lua on Windows and other systems - ----------------------------------------- - If you're not using the usual Unix tools, then the instructions for - building Lua depend on the compiler you use. You'll need to create - projects (or whatever your compiler uses) for building the library, - the interpreter, and the compiler, as follows: - - library: lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c - lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c - ltable.c ltm.c lundump.c lvm.c lzio.c - lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c - ltablib.c lstrlib.c loadlib.c linit.c - - interpreter: library, lua.c - - compiler: library, luac.c print.c - - If you use Visual Studio .NET, you can use etc/luavs.bat in its - "Command Prompt". - - If all you want is to build the Lua interpreter, you may put all .c files - in a single project, except for luac.c and print.c. Or just use etc/all.c. - - To use Lua as a library in your own programs, you'll need to know how to - create and use libraries with your compiler. - - As mentioned above, you may edit luaconf.h to select some features before - building Lua. - -(end of INSTALL) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/README b/LuaBridge3/Tests/Lua/Lua.5.1.5/README deleted file mode 100644 index 11b4dff..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/README +++ /dev/null @@ -1,37 +0,0 @@ -README for Lua 5.1 - -See INSTALL for installation instructions. -See HISTORY for a summary of changes since the last released version. - -* What is Lua? - ------------ - Lua is a powerful, light-weight programming language designed for extending - applications. Lua is also frequently used as a general-purpose, stand-alone - language. Lua is free software. - - For complete information, visit Lua's web site at http://www.lua.org/ . - For an executive summary, see http://www.lua.org/about.html . - - Lua has been used in many different projects around the world. - For a short list, see http://www.lua.org/uses.html . - -* Availability - ------------ - Lua is freely available for both academic and commercial purposes. - See COPYRIGHT and http://www.lua.org/license.html for details. - Lua can be downloaded at http://www.lua.org/download.html . - -* Installation - ------------ - Lua is implemented in pure ANSI C, and compiles unmodified in all known - platforms that have an ANSI C compiler. In most Unix-like platforms, simply - do "make" with a suitable target. See INSTALL for detailed instructions. - -* Origin - ------ - Lua is developed at Lua.org, a laboratory of the Department of Computer - Science of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro - in Brazil). - For more information about the authors, see http://www.lua.org/authors.html . - -(end of README) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/contents.html b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/contents.html deleted file mode 100644 index 3d83da9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/contents.html +++ /dev/null @@ -1,497 +0,0 @@ - - - -Lua 5.1 Reference Manual - contents - - - - - - - -
-

- -Lua 5.1 Reference Manual -

- -

-The reference manual is the official definition of the Lua language. -For a complete introduction to Lua programming, see the book -Programming in Lua. - -

-This manual is also available as a book: -

- - - -Lua 5.1 Reference Manual -
by R. Ierusalimschy, L. H. de Figueiredo, W. Celes -
Lua.org, August 2006 -
ISBN 85-903798-3-3 -
-
- -

-Buy a copy -of this book and -help to support -the Lua project. - -

-start -· -contents -· -index -· -other versions -


- -Copyright © 2006–2012 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

Contents

- - -

Index

- - - - - - - -
-

Lua functions

-_G
-_VERSION
-

- -assert
-collectgarbage
-dofile
-error
-getfenv
-getmetatable
-ipairs
-load
-loadfile
-loadstring
-module
-next
-pairs
-pcall
-print
-rawequal
-rawget
-rawset
-require
-select
-setfenv
-setmetatable
-tonumber
-tostring
-type
-unpack
-xpcall
-

- -coroutine.create
-coroutine.resume
-coroutine.running
-coroutine.status
-coroutine.wrap
-coroutine.yield
-

- -debug.debug
-debug.getfenv
-debug.gethook
-debug.getinfo
-debug.getlocal
-debug.getmetatable
-debug.getregistry
-debug.getupvalue
-debug.setfenv
-debug.sethook
-debug.setlocal
-debug.setmetatable
-debug.setupvalue
-debug.traceback
- -

-

 

-file:close
-file:flush
-file:lines
-file:read
-file:seek
-file:setvbuf
-file:write
-

- -io.close
-io.flush
-io.input
-io.lines
-io.open
-io.output
-io.popen
-io.read
-io.stderr
-io.stdin
-io.stdout
-io.tmpfile
-io.type
-io.write
-

- -math.abs
-math.acos
-math.asin
-math.atan
-math.atan2
-math.ceil
-math.cos
-math.cosh
-math.deg
-math.exp
-math.floor
-math.fmod
-math.frexp
-math.huge
-math.ldexp
-math.log
-math.log10
-math.max
-math.min
-math.modf
-math.pi
-math.pow
-math.rad
-math.random
-math.randomseed
-math.sin
-math.sinh
-math.sqrt
-math.tan
-math.tanh
-

- -os.clock
-os.date
-os.difftime
-os.execute
-os.exit
-os.getenv
-os.remove
-os.rename
-os.setlocale
-os.time
-os.tmpname
-

- -package.cpath
-package.loaded
-package.loaders
-package.loadlib
-package.path
-package.preload
-package.seeall
-

- -string.byte
-string.char
-string.dump
-string.find
-string.format
-string.gmatch
-string.gsub
-string.len
-string.lower
-string.match
-string.rep
-string.reverse
-string.sub
-string.upper
-

- -table.concat
-table.insert
-table.maxn
-table.remove
-table.sort
- -

-

C API

-lua_Alloc
-lua_CFunction
-lua_Debug
-lua_Hook
-lua_Integer
-lua_Number
-lua_Reader
-lua_State
-lua_Writer
-

- -lua_atpanic
-lua_call
-lua_checkstack
-lua_close
-lua_concat
-lua_cpcall
-lua_createtable
-lua_dump
-lua_equal
-lua_error
-lua_gc
-lua_getallocf
-lua_getfenv
-lua_getfield
-lua_getglobal
-lua_gethook
-lua_gethookcount
-lua_gethookmask
-lua_getinfo
-lua_getlocal
-lua_getmetatable
-lua_getstack
-lua_gettable
-lua_gettop
-lua_getupvalue
-lua_insert
-lua_isboolean
-lua_iscfunction
-lua_isfunction
-lua_islightuserdata
-lua_isnil
-lua_isnone
-lua_isnoneornil
-lua_isnumber
-lua_isstring
-lua_istable
-lua_isthread
-lua_isuserdata
-lua_lessthan
-lua_load
-lua_newstate
-lua_newtable
-lua_newthread
-lua_newuserdata
-lua_next
-lua_objlen
-lua_pcall
-lua_pop
-lua_pushboolean
-lua_pushcclosure
-lua_pushcfunction
-lua_pushfstring
-lua_pushinteger
-lua_pushlightuserdata
-lua_pushliteral
-lua_pushlstring
-lua_pushnil
-lua_pushnumber
-lua_pushstring
-lua_pushthread
-lua_pushvalue
-lua_pushvfstring
-lua_rawequal
-lua_rawget
-lua_rawgeti
-lua_rawset
-lua_rawseti
-lua_register
-lua_remove
-lua_replace
-lua_resume
-lua_setallocf
-lua_setfenv
-lua_setfield
-lua_setglobal
-lua_sethook
-lua_setlocal
-lua_setmetatable
-lua_settable
-lua_settop
-lua_setupvalue
-lua_status
-lua_toboolean
-lua_tocfunction
-lua_tointeger
-lua_tolstring
-lua_tonumber
-lua_topointer
-lua_tostring
-lua_tothread
-lua_touserdata
-lua_type
-lua_typename
-lua_upvalueindex
-lua_xmove
-lua_yield
- -

-

auxiliary library

-luaL_Buffer
-luaL_Reg
-

- -luaL_addchar
-luaL_addlstring
-luaL_addsize
-luaL_addstring
-luaL_addvalue
-luaL_argcheck
-luaL_argerror
-luaL_buffinit
-luaL_callmeta
-luaL_checkany
-luaL_checkint
-luaL_checkinteger
-luaL_checklong
-luaL_checklstring
-luaL_checknumber
-luaL_checkoption
-luaL_checkstack
-luaL_checkstring
-luaL_checktype
-luaL_checkudata
-luaL_dofile
-luaL_dostring
-luaL_error
-luaL_getmetafield
-luaL_getmetatable
-luaL_gsub
-luaL_loadbuffer
-luaL_loadfile
-luaL_loadstring
-luaL_newmetatable
-luaL_newstate
-luaL_openlibs
-luaL_optint
-luaL_optinteger
-luaL_optlong
-luaL_optlstring
-luaL_optnumber
-luaL_optstring
-luaL_prepbuffer
-luaL_pushresult
-luaL_ref
-luaL_register
-luaL_typename
-luaL_typerror
-luaL_unref
-luaL_where
- -

-

- -


- -Last update: -Mon Feb 13 18:53:32 BRST 2012 - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/cover.png b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/cover.png deleted file mode 100644 index 2dbb198123f03a7250bfa57c8253739962d69afb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3305 zcmVNc=P)V>IGcGYOVIUw=dB=aTYv^*htKxL4xjCf9X{I|EBQ!hu?+M>5oT>TE}0ye97P9R7S8qXycMFZ&6rBQl2xq6d5z1RT?1tMWggH(oGfxZ3MRwMW* zhWcm<0o+gGDNJLnwySJIYqTbnA(cT&JjHAh%b?&;aM%-PVunbF`4oU{acLCOU~~ed z=Xys9YZpo#i8bMPc#43D)u4sMGKqI^_da6LW&~0K*cO4+ z_PRNFEtj+pK65RYy#Eh+iK_)|A>ml%LRW(G?uWEPuP@)V__gB&q{E^1Drx0`;n)|1&{JZ#-e7eMcd1S~0(ChdB8 zS0!Ap-8R#X^0X5R7@pQ0wmH~jKhYj`l%C2tznfmz5?4vXD&s9-{r%L{8o|B1n{hn> zX-7F)1C|g{Fjw^QO3xSEM8WF{nF8))ijLB@AziK0j<-dAU&NHQAw-4j8oelO%2Dg_ z37hiyuBd>qbbcrr0xb~*rLW9q2cyBcq8kgCW9j_Jd}=!9R2g|I=9{KHXtr2}hFHKH zPZ!2Bg|$47mFu;Duqg$YQfQ4vD~-}9t!+atHYg~SbM=?ElxgB&vnLeLny@Jo1@}ra zw-%pO_5&GLRc)GAp8w;^w0pr+)}6{$xN2*=h1(z&s0B5@zOQ2Cj<++EgPm6D*KdLp^Jc$%i(A&wq1mn{*M;Pu$%2I-|s;8_q`68Jd zLJ$dITeas|8_h>+9GB??ksz(jj7@SsNq-j_f;Mf@l8W*L-v0vui)W9N64OhM7aV?n zo{!IxNC9-U@zPPgc8EYtsn)ggZ<}BOc#01{#gH6*gjm!cMXYMFiJ5! z$8SI7^a#mxl?1n2Bwr+veIkV`2fdd@*by0Naq>o!4A;Y!nrTV7gj#l-OAs* zvT_zQj8DKsyvuDrVn7=m8 z&;O0T{VN_DroW5Nu5jxvQZU%ZlLv@3)#xH@icfQd{R930nH<0P?=qQ<5s3ufc;l~s z^rLTdbhJn*9LK$Q@z$Gf{__VPoYQ~*AN<{S=xOJbXHXg;Sjdpd5Nq1FU!ZP(bkV*K z5BX<_uE(!VaN&B59T#f)0@ixmc3_}Kkful!<-+AYa=bk&rr9RA^GG2#cH|o2Jo3*;M^C0Z#I`l`S@(jjq^e|^t7&J*rAXei$y>%zrcxe zzKVokW{ylvDyoN%5F8rxOC(&6ljrfOA4aT&iHZA4RiB-iOg@n)*W;YNOgdZoU&C~Q zYvZ-d>YDjzn4Be*DQQDPBE@KZ$^kz7@cjMzsnv(*TI*A%M(*BC03b*t8J+ZR_jR(6 zttGy#T|b&jH^^6g-e(O?=xBjqSdb8D)Kd$tjjQa}6Izo*l=AOHBZzP@%TWj?-Z2yYmt`$ryp=SGWT>kg8zlLgEEs(4iVm;4Q>56I~!I5E_!W;Hjvwox?Uqoq) z@&EyI&Dg6UFbzN8)tb&2Y&=@c`Y|NW9`Pe8A!)AFN8A)Nk)Urp8ZM1e+_>zsWuw3Gwz#h*<|ZTYWyBV&rD^+OOrPXFnaE_T4H3gMI7NJvIPCeSU~lbZRURtjFJ3 zOtR_n9@p1NEV@-WX*<9pdwg@TE&lANPj7A1!>6YW%k<@shB-1^pOm#iGtfhChrf42 zsVsLR)XYafILOn7Dzbrs7oH##T<@vPK}ueH!cSN`F26lfqvKnrf9<;5xmTWYf?eG_ zeX!9}PBYlclLvflOw3@&T9Q?4=KSZAi+(6#NWSqr9j%R{qzT%*cARj9+M7Z={YZ`Z zkUIHTCXWs=UG`IipsSVd{5f`@zJAseNAl`14({FT2Xbx{9&lM)RVZ}_{lVes;w@a^N+fz49V zNXZM2^W9f`Rcp=JFX(8gt1f+0`B4G4?=d#PKzC_k7?Qz0y4x6=B$uz#sndjmeCtJC zJ5DgL%uYf!d*Z&jYQX0B2)f!R6lrVmT}CPC?c~T_GI?g_YxBM}hQWc|eD9k)^C*Fe z?D1?8AQoMD2D71Pn?G+{G@(R_)@FY(T|5yQo#5loxID%}wj5$qei{Hm5DK!lj~Ach z@X#`~XwB_uPF>*Z&(R#ISEvU#FA)Nz`TQED$+JgFvs?%)ll=n>_cNbnY=Y|(+?{11 zL&3o^iG=8GW2ldzK00F6PjxbRUOh&1<7lUfP!D<@?6{2FWT>x{XIvqi2CY#FPoWf2 zVo0P!tZu2v=D9u1zJZdTwyAHS9=M*uGC8uBNRUK|GgrvwmU;C8q`)+=EkZW7g=ru~ z6RQpkqkiq>Ru+?vAkXbSVK7dSLn?*gy_ zjjN{!SUh^+iEFRr=;K9At8qQ=c=~M}HT#)sT^Fg(`nT>?C{y%_^R>wBb&6$ nh%8`n`v3p{2XskIMF-Xh6%#iZwFs;u00000NkvXXu0mjfd@Wp4 diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/logo.gif b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/logo.gif deleted file mode 100644 index 2f5e4ac2e742fbb7675e739879211553758aea9c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4232 zcmeH``9G8i;K!fm@ywWE@XWZzZ5SF?A>^uN#>^O6HI%DVL*tw8h1>$H%uPC0$QQ=txe!o}PX)Ev+jn>*nFZ-TC=<^76WcLZL(=DJm)| zEiIKwrInSHXU?3duCC_u@5try#>U2`rlw1mF15F}U%!66tE;QKyIUmcDJ<+QF77*W zFJd#&)VAl)kJ6K zi<>tmZ{3>g>+2gB7#JQNe(>OdLh;A=`1q42PbMZNCMPF*dXxhLZw3aYhlZwyhu@Bj z%#4n{d-Q1b$&i4QMce4L#8^!oMdw{PDnm4D66&3*dxX=-YIX6DQL_g`jbzkd4k zZDCoLf=%jL&vIeE zO=XcZ9fxt`f}-DQ^%H*PHMUs(JN%UWkI|Y8h9#6~I$Cw@{RqzO4&P-x;jHCPJ6Ks2 zoU%foi)nXd_sdkiuJa@@5J4RrreKfWSnz5>eMa5yTP=)16uu)TIdx~Fhho))6jZl) z($*i>QrIX4u}u3>m{WSn_ehkUGQ& zs})aUlTH1Cj1g3ZE3=MPXsSniEwJ{e6C3N#HjD=B4`8rWIsz!a7ecYpec?WuH+y?Wsm18^$cS4WmHhH3_=r zh*ILlm*X1dB^E5($KVl&zT524%l}vpHg%;Y+LezV_&TAJCmH`idhuj-n$4FZ)UE|jXLayXa-&O3Q z?Iyo!x*$5hD_HfFnDfGYj-RD|eIb7I?%>Y_kf%}Nbd`BXb4l1(Pc+}zoUR|9%_!7f zum2T;wbx&pohtI+&@~wm3nH9xLbOYkg*`phY~TK5iC#3tZNXo9s`cahx+8j2)rh5C zQgZh6D7Ekgib|hpdhxYf{r!PTJc z!vsYG@{hA}l5kL)g)0N_)(nC<*L0qdUi*3fD5<0sn58>zklX@6Tyv3*X^}m=Cqc40 zQ6GfjG@kd1mFIm`qaubWunm_?P>WUZ`9|f_z%gGHi{n|uu(N8!L=aw5(qAcDj$-QK zu;D#j6e42OXTQD>)i zlvM$LX`$n9EEjxM$_QDF&a z7cme_rat}aXmiN&7`6Q98}dh4Z@8L_uAb#nK&GQiZOOUnA9kAEVb-csuN1AWL=sXt z{z9GCN%%l0N9QvJM;tl1nf?rrhT{*sE%4WqR?{0~aIrfCcCPxf4eh_*jjQ=`$p53Y z@_|Rsx2i}|3dNFetMQQ5y8agTK-E0D&7;@3-LUxfvZ7 z7~!p@&mFe^oca2^F|CBt+4Ly?^ViUVSAhAH>JH1GN{^TQb3QnM*x0ZiZgDyNI@_c3 z@{}(WH4*e3T~}n_^0}da4ElIxAf9B!IaL7z9X0Icvj@cIkE*~W--17&WN`Ea5)Gn> z#gpfRb#44;jVTOS{FuaZgd(-ZD848=fQzgST2MxR>wSLc1P=2HDvByz$B$IsNCC6L zCM?nK*OHj6JA9gz4|b<~2%RqelN^1Y)jIqnRs!mDKV^BQTfo@hOtz7*Ug}Ee^cbsj zNNlumRgAmt`1$b5MO;&X#5-EP<}AaY;52ihIpem&MTea$?3!DrwbYa?V`NjEfWF3z zUq5JY8Ch;L{kx&J<1K&Fe_Vn;8gk{%c;n?nA2(%(f%DCRHko3uT~VI7RE^JWEqaCq z)i|%nfj(*4|V*XhY3W%M# z*yn6SN4eUOHFxAD7B&9E_PO`G5bqgs^@J{9bk>&;PlUAiqo`j3rjQDgD!}mqLUtb` zCB}ZD@m@s#pf7bV4jreOC*JVfHZ|hyHkX!rauVdd_I9FL45d{gWH!DNYu;i(|8wVx z!)eLY6YXxZ2{Coae0xuTnxo1ACb5wtED?VJAz&@114$Ao6uG9YSy*!K;m5_mj=0^j zw%?b%AOs}ql@$TGC-!^^*_#RT5+y_kTzQG9?LPPZNAtt6cJ%d2$q(I)ws21*?xF%p zN+NeGnWRQ<5w70Rc(bl|S0Xr&5@WrmdurS|IgPB|EyuZO#=tf!35)G!HJ`E1jh^lH zTBu~rL#DhQO*XAWtBt}JHH$lc>3%r0yD|maW_(W=B_J+y164F>O4dO|@&@N3Z3p=B zmVl{|^Z&#atHY|9n&la)SBo}=3AFIF=_~LDJk6MTlA73CXtX+4bnn+c!}N}IPa5pp zwyqbqIkN|I3j_3vD6$zlu{Ps(N-J|*qzEt<$5Soh;s^AuKv_ z-Tz+O1_~6*9CJh4r}`}mbUtjbf#fX58RIIkP6&@*y9kI|5fK*_eZ%jv3U$5*x<>D_ za2M(TV8?XY+9xy>0En#Te<6X4$0&dbyd(go$~eq4u(u)EA2msyF<5ssLZ zDP|I}=~Bi_q)whWv=Ri~L1TYaNrR;5cMB@s78HF1{w&r(6GJ;_2@bD?#1p&P4n_?n0#9Vx~$qjMX=Lk?*!@aKo8m&$iPO7S{g3sFUwr`*<53(68xx7?z`2xf# zGSicy_zI(PJ|%qc2VxT+6bOE--a{k&aq7$<<= zFt)C<@|TPs`+eycPGoGL1Wn9|Ed&a2JyAmjnkm3DQBECX&`bt~odH9cUPq4M{#$-q?G3!)qO-it*&YHw+j-O* zYy78V*`4Q=kQ@^Yz*b6Tal4(Me7BGeS^;phWAW8+L^5A(=D)t?k!rLIwVAKtq=f7h z&^n&VX1-T$ScvN~639QLZ^d@niMaS{C-Q)8oHHBhwD*r~-1Ze#Q)GFOFptW32a-uF z;M@ux%i%a25NwIgXt*=GHX$3~aZfwovGL!}sf?j9TsVo^cn(%&a<--0mIXYqGe>c PWz_J}_#7St0k8iB@FZjZ diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.1 b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.1 deleted file mode 100644 index 24809cc..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.1 +++ /dev/null @@ -1,163 +0,0 @@ -.\" $Id: lua.man,v 1.11 2006/01/06 16:03:34 lhf Exp $ -.TH LUA 1 "$Date: 2006/01/06 16:03:34 $" -.SH NAME -lua \- Lua interpreter -.SH SYNOPSIS -.B lua -[ -.I options -] -[ -.I script -[ -.I args -] -] -.SH DESCRIPTION -.B lua -is the stand-alone Lua interpreter. -It loads and executes Lua programs, -either in textual source form or -in precompiled binary form. -(Precompiled binaries are output by -.BR luac , -the Lua compiler.) -.B lua -can be used as a batch interpreter and also interactively. -.LP -The given -.I options -(see below) -are executed and then -the Lua program in file -.I script -is loaded and executed. -The given -.I args -are available to -.I script -as strings in a global table named -.BR arg . -If these arguments contain spaces or other characters special to the shell, -then they should be quoted -(but note that the quotes will be removed by the shell). -The arguments in -.B arg -start at 0, -which contains the string -.RI ' script '. -The index of the last argument is stored in -.BR arg.n . -The arguments given in the command line before -.IR script , -including the name of the interpreter, -are available in negative indices in -.BR arg . -.LP -At the very start, -before even handling the command line, -.B lua -executes the contents of the environment variable -.BR LUA_INIT , -if it is defined. -If the value of -.B LUA_INIT -is of the form -.RI '@ filename ', -then -.I filename -is executed. -Otherwise, the string is assumed to be a Lua statement and is executed. -.LP -Options start with -.B '\-' -and are described below. -You can use -.B "'\--'" -to signal the end of options. -.LP -If no arguments are given, -then -.B "\-v \-i" -is assumed when the standard input is a terminal; -otherwise, -.B "\-" -is assumed. -.LP -In interactive mode, -.B lua -prompts the user, -reads lines from the standard input, -and executes them as they are read. -If a line does not contain a complete statement, -then a secondary prompt is displayed and -lines are read until a complete statement is formed or -a syntax error is found. -So, one way to interrupt the reading of an incomplete statement is -to force a syntax error: -adding a -.B ';' -in the middle of a statement is a sure way of forcing a syntax error -(except inside multiline strings and comments; these must be closed explicitly). -If a line starts with -.BR '=' , -then -.B lua -displays the values of all the expressions in the remainder of the -line. The expressions must be separated by commas. -The primary prompt is the value of the global variable -.BR _PROMPT , -if this value is a string; -otherwise, the default prompt is used. -Similarly, the secondary prompt is the value of the global variable -.BR _PROMPT2 . -So, -to change the prompts, -set the corresponding variable to a string of your choice. -You can do that after calling the interpreter -or on the command line -(but in this case you have to be careful with quotes -if the prompt string contains a space; otherwise you may confuse the shell.) -The default prompts are "> " and ">> ". -.SH OPTIONS -.TP -.B \- -load and execute the standard input as a file, -that is, -not interactively, -even when the standard input is a terminal. -.TP -.BI \-e " stat" -execute statement -.IR stat . -You need to quote -.I stat -if it contains spaces, quotes, -or other characters special to the shell. -.TP -.B \-i -enter interactive mode after -.I script -is executed. -.TP -.BI \-l " name" -call -.BI require(' name ') -before executing -.IR script . -Typically used to load libraries. -.TP -.B \-v -show version information. -.SH "SEE ALSO" -.BR luac (1) -.br -http://www.lua.org/ -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -and -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.css b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.css deleted file mode 100644 index 7fafbb1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.css +++ /dev/null @@ -1,83 +0,0 @@ -body { - color: #000000 ; - background-color: #FFFFFF ; - font-family: Helvetica, Arial, sans-serif ; - text-align: justify ; - margin-right: 30px ; - margin-left: 30px ; -} - -h1, h2, h3, h4 { - font-family: Verdana, Geneva, sans-serif ; - font-weight: normal ; - font-style: italic ; -} - -h2 { - padding-top: 0.4em ; - padding-bottom: 0.4em ; - padding-left: 30px ; - padding-right: 30px ; - margin-left: -30px ; - background-color: #E0E0FF ; -} - -h3 { - padding-left: 0.5em ; - border-left: solid #E0E0FF 1em ; -} - -table h3 { - padding-left: 0px ; - border-left: none ; -} - -a:link { - color: #000080 ; - background-color: inherit ; - text-decoration: none ; -} - -a:visited { - background-color: inherit ; - text-decoration: none ; -} - -a:link:hover, a:visited:hover { - color: #000080 ; - background-color: #E0E0FF ; -} - -a:link:active, a:visited:active { - color: #FF0000 ; -} - -hr { - border: 0 ; - height: 1px ; - color: #a0a0a0 ; - background-color: #a0a0a0 ; -} - -:target { - background-color: #F8F8F8 ; - padding: 8px ; - border: solid #a0a0a0 2px ; -} - -.footer { - color: gray ; - font-size: small ; -} - -input[type=text] { - border: solid #a0a0a0 2px ; - border-radius: 2em ; - -moz-border-radius: 2em ; - background-image: url('images/search.png') ; - background-repeat: no-repeat; - background-position: 4px center ; - padding-left: 20px ; - height: 2em ; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.html b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.html deleted file mode 100644 index 1d435ab..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/lua.html +++ /dev/null @@ -1,172 +0,0 @@ - - - -LUA man page - - - - - -

NAME

-lua - Lua interpreter -

SYNOPSIS

-lua -[ -options -] -[ -script -[ -args -] -] -

DESCRIPTION

-lua -is the stand-alone Lua interpreter. -It loads and executes Lua programs, -either in textual source form or -in precompiled binary form. -(Precompiled binaries are output by -luac, -the Lua compiler.) -lua -can be used as a batch interpreter and also interactively. -

-The given -options -(see below) -are executed and then -the Lua program in file -script -is loaded and executed. -The given -args -are available to -script -as strings in a global table named -arg. -If these arguments contain spaces or other characters special to the shell, -then they should be quoted -(but note that the quotes will be removed by the shell). -The arguments in -arg -start at 0, -which contains the string -'script'. -The index of the last argument is stored in -arg.n. -The arguments given in the command line before -script, -including the name of the interpreter, -are available in negative indices in -arg. -

-At the very start, -before even handling the command line, -lua -executes the contents of the environment variable -LUA_INIT, -if it is defined. -If the value of -LUA_INIT -is of the form -'@filename', -then -filename -is executed. -Otherwise, the string is assumed to be a Lua statement and is executed. -

-Options start with -'-' -and are described below. -You can use -'--' -to signal the end of options. -

-If no arguments are given, -then -"-v -i" -is assumed when the standard input is a terminal; -otherwise, -"-" -is assumed. -

-In interactive mode, -lua -prompts the user, -reads lines from the standard input, -and executes them as they are read. -If a line does not contain a complete statement, -then a secondary prompt is displayed and -lines are read until a complete statement is formed or -a syntax error is found. -So, one way to interrupt the reading of an incomplete statement is -to force a syntax error: -adding a -';' -in the middle of a statement is a sure way of forcing a syntax error -(except inside multiline strings and comments; these must be closed explicitly). -If a line starts with -'=', -then -lua -displays the values of all the expressions in the remainder of the -line. The expressions must be separated by commas. -The primary prompt is the value of the global variable -_PROMPT, -if this value is a string; -otherwise, the default prompt is used. -Similarly, the secondary prompt is the value of the global variable -_PROMPT2. -So, -to change the prompts, -set the corresponding variable to a string of your choice. -You can do that after calling the interpreter -or on the command line -(but in this case you have to be careful with quotes -if the prompt string contains a space; otherwise you may confuse the shell.) -The default prompts are "> " and ">> ". -

OPTIONS

-

-- -load and execute the standard input as a file, -that is, -not interactively, -even when the standard input is a terminal. -

--e stat -execute statement -stat. -You need to quote -stat -if it contains spaces, quotes, -or other characters special to the shell. -

--i -enter interactive mode after -script -is executed. -

--l name -call -require('name') -before executing -script. -Typically used to load libraries. -

--v -show version information. -

SEE ALSO

-luac(1) -
-http://www.lua.org/ -

DIAGNOSTICS

-Error messages should be self explanatory. -

AUTHORS

-R. Ierusalimschy, -L. H. de Figueiredo, -and -W. Celes - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.1 b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.1 deleted file mode 100644 index d814678..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.1 +++ /dev/null @@ -1,136 +0,0 @@ -.\" $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp $ -.TH LUAC 1 "$Date: 2006/01/06 16:03:34 $" -.SH NAME -luac \- Lua compiler -.SH SYNOPSIS -.B luac -[ -.I options -] [ -.I filenames -] -.SH DESCRIPTION -.B luac -is the Lua compiler. -It translates programs written in the Lua programming language -into binary files that can be later loaded and executed. -.LP -The main advantages of precompiling chunks are: -faster loading, -protecting source code from accidental user changes, -and -off-line syntax checking. -.LP -Pre-compiling does not imply faster execution -because in Lua chunks are always compiled into bytecodes before being executed. -.B luac -simply allows those bytecodes to be saved in a file for later execution. -.LP -Pre-compiled chunks are not necessarily smaller than the corresponding source. -The main goal in pre-compiling is faster loading. -.LP -The binary files created by -.B luac -are portable only among architectures with the same word size and byte order. -.LP -.B luac -produces a single output file containing the bytecodes -for all source files given. -By default, -the output file is named -.BR luac.out , -but you can change this with the -.B \-o -option. -.LP -In the command line, -you can mix -text files containing Lua source and -binary files containing precompiled chunks. -This is useful to combine several precompiled chunks, -even from different (but compatible) platforms, -into a single precompiled chunk. -.LP -You can use -.B "'\-'" -to indicate the standard input as a source file -and -.B "'\--'" -to signal the end of options -(that is, -all remaining arguments will be treated as files even if they start with -.BR "'\-'" ). -.LP -The internal format of the binary files produced by -.B luac -is likely to change when a new version of Lua is released. -So, -save the source files of all Lua programs that you precompile. -.LP -.SH OPTIONS -Options must be separate. -.TP -.B \-l -produce a listing of the compiled bytecode for Lua's virtual machine. -Listing bytecodes is useful to learn about Lua's virtual machine. -If no files are given, then -.B luac -loads -.B luac.out -and lists its contents. -.TP -.BI \-o " file" -output to -.IR file , -instead of the default -.BR luac.out . -(You can use -.B "'\-'" -for standard output, -but not on platforms that open standard output in text mode.) -The output file may be a source file because -all files are loaded before the output file is written. -Be careful not to overwrite precious files. -.TP -.B \-p -load files but do not generate any output file. -Used mainly for syntax checking and for testing precompiled chunks: -corrupted files will probably generate errors when loaded. -Lua always performs a thorough integrity test on precompiled chunks. -Bytecode that passes this test is completely safe, -in the sense that it will not break the interpreter. -However, -there is no guarantee that such code does anything sensible. -(None can be given, because the halting problem is unsolvable.) -If no files are given, then -.B luac -loads -.B luac.out -and tests its contents. -No messages are displayed if the file passes the integrity test. -.TP -.B \-s -strip debug information before writing the output file. -This saves some space in very large chunks, -but if errors occur when running a stripped chunk, -then the error messages may not contain the full information they usually do. -For instance, -line numbers and names of local variables are lost. -.TP -.B \-v -show version information. -.SH FILES -.TP 15 -.B luac.out -default output file -.SH "SEE ALSO" -.BR lua (1) -.br -http://www.lua.org/ -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -L. H. de Figueiredo, -R. Ierusalimschy and -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.html b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.html deleted file mode 100644 index 179ffe8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/luac.html +++ /dev/null @@ -1,145 +0,0 @@ - - - -LUAC man page - - - - - -

NAME

-luac - Lua compiler -

SYNOPSIS

-luac -[ -options -] [ -filenames -] -

DESCRIPTION

-luac -is the Lua compiler. -It translates programs written in the Lua programming language -into binary files that can be later loaded and executed. -

-The main advantages of precompiling chunks are: -faster loading, -protecting source code from accidental user changes, -and -off-line syntax checking. -

-Precompiling does not imply faster execution -because in Lua chunks are always compiled into bytecodes before being executed. -luac -simply allows those bytecodes to be saved in a file for later execution. -

-Precompiled chunks are not necessarily smaller than the corresponding source. -The main goal in precompiling is faster loading. -

-The binary files created by -luac -are portable only among architectures with the same word size and byte order. -

-luac -produces a single output file containing the bytecodes -for all source files given. -By default, -the output file is named -luac.out, -but you can change this with the --o -option. -

-In the command line, -you can mix -text files containing Lua source and -binary files containing precompiled chunks. -This is useful because several precompiled chunks, -even from different (but compatible) platforms, -can be combined into a single precompiled chunk. -

-You can use -'-' -to indicate the standard input as a source file -and -'--' -to signal the end of options -(that is, -all remaining arguments will be treated as files even if they start with -'-'). -

-The internal format of the binary files produced by -luac -is likely to change when a new version of Lua is released. -So, -save the source files of all Lua programs that you precompile. -

-

OPTIONS

-Options must be separate. -

--l -produce a listing of the compiled bytecode for Lua's virtual machine. -Listing bytecodes is useful to learn about Lua's virtual machine. -If no files are given, then -luac -loads -luac.out -and lists its contents. -

--o file -output to -file, -instead of the default -luac.out. -(You can use -'-' -for standard output, -but not on platforms that open standard output in text mode.) -The output file may be a source file because -all files are loaded before the output file is written. -Be careful not to overwrite precious files. -

--p -load files but do not generate any output file. -Used mainly for syntax checking and for testing precompiled chunks: -corrupted files will probably generate errors when loaded. -Lua always performs a thorough integrity test on precompiled chunks. -Bytecode that passes this test is completely safe, -in the sense that it will not break the interpreter. -However, -there is no guarantee that such code does anything sensible. -(None can be given, because the halting problem is unsolvable.) -If no files are given, then -luac -loads -luac.out -and tests its contents. -No messages are displayed if the file passes the integrity test. -

--s -strip debug information before writing the output file. -This saves some space in very large chunks, -but if errors occur when running a stripped chunk, -then the error messages may not contain the full information they usually do. -For instance, -line numbers and names of local variables are lost. -

--v -show version information. -

FILES

-

-luac.out -default output file -

SEE ALSO

-lua(1) -
-http://www.lua.org/ -

DIAGNOSTICS

-Error messages should be self explanatory. -

AUTHORS

-L. H. de Figueiredo, -R. Ierusalimschy and -W. Celes - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.css b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.css deleted file mode 100644 index b49b362..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.css +++ /dev/null @@ -1,24 +0,0 @@ -h3 code { - font-family: inherit ; - font-size: inherit ; -} - -pre, code { - font-size: 12pt ; -} - -span.apii { - float: right ; - font-family: inherit ; - font-style: normal ; - font-size: small ; - color: gray ; -} - -p+h1, ul+h1 { - padding-top: 0.4em ; - padding-bottom: 0.4em ; - padding-left: 30px ; - margin-left: -30px ; - background-color: #E0E0FF ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.html b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.html deleted file mode 100644 index 4e41683..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/manual.html +++ /dev/null @@ -1,8804 +0,0 @@ - - - - -Lua 5.1 Reference Manual - - - - - - - -
-

- -Lua 5.1 Reference Manual -

- -by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes -

- -Copyright © 2006–2012 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - -


-

- -contents -· -index -· -other versions - - -

- - - - - - -

1 - Introduction

- -

-Lua is an extension programming language designed to support -general procedural programming with data description -facilities. -It also offers good support for object-oriented programming, -functional programming, and data-driven programming. -Lua is intended to be used as a powerful, light-weight -scripting language for any program that needs one. -Lua is implemented as a library, written in clean C -(that is, in the common subset of ANSI C and C++). - - -

-Being an extension language, Lua has no notion of a "main" program: -it only works embedded in a host client, -called the embedding program or simply the host. -This host program can invoke functions to execute a piece of Lua code, -can write and read Lua variables, -and can register C functions to be called by Lua code. -Through the use of C functions, Lua can be augmented to cope with -a wide range of different domains, -thus creating customized programming languages sharing a syntactical framework. -The Lua distribution includes a sample host program called lua, -which uses the Lua library to offer a complete, stand-alone Lua interpreter. - - -

-Lua is free software, -and is provided as usual with no guarantees, -as stated in its license. -The implementation described in this manual is available -at Lua's official web site, www.lua.org. - - -

-Like any other reference manual, -this document is dry in places. -For a discussion of the decisions behind the design of Lua, -see the technical papers available at Lua's web site. -For a detailed introduction to programming in Lua, -see Roberto's book, Programming in Lua (Second Edition). - - - -

2 - The Language

- -

-This section describes the lexis, the syntax, and the semantics of Lua. -In other words, -this section describes -which tokens are valid, -how they can be combined, -and what their combinations mean. - - -

-The language constructs will be explained using the usual extended BNF notation, -in which -{a} means 0 or more a's, and -[a] means an optional a. -Non-terminals are shown like non-terminal, -keywords are shown like kword, -and other terminal symbols are shown like `=´. -The complete syntax of Lua can be found in §8 -at the end of this manual. - - - -

2.1 - Lexical Conventions

- -

-Names -(also called identifiers) -in Lua can be any string of letters, -digits, and underscores, -not beginning with a digit. -This coincides with the definition of names in most languages. -(The definition of letter depends on the current locale: -any character considered alphabetic by the current locale -can be used in an identifier.) -Identifiers are used to name variables and table fields. - - -

-The following keywords are reserved -and cannot be used as names: - - -

-     and       break     do        else      elseif
-     end       false     for       function  if
-     in        local     nil       not       or
-     repeat    return    then      true      until     while
-
- -

-Lua is a case-sensitive language: -and is a reserved word, but And and AND -are two different, valid names. -As a convention, names starting with an underscore followed by -uppercase letters (such as _VERSION) -are reserved for internal global variables used by Lua. - - -

-The following strings denote other tokens: - -

-     +     -     *     /     %     ^     #
-     ==    ~=    <=    >=    <     >     =
-     (     )     {     }     [     ]
-     ;     :     ,     .     ..    ...
-
- -

-Literal strings -can be delimited by matching single or double quotes, -and can contain the following C-like escape sequences: -'\a' (bell), -'\b' (backspace), -'\f' (form feed), -'\n' (newline), -'\r' (carriage return), -'\t' (horizontal tab), -'\v' (vertical tab), -'\\' (backslash), -'\"' (quotation mark [double quote]), -and '\'' (apostrophe [single quote]). -Moreover, a backslash followed by a real newline -results in a newline in the string. -A character in a string can also be specified by its numerical value -using the escape sequence \ddd, -where ddd is a sequence of up to three decimal digits. -(Note that if a numerical escape is to be followed by a digit, -it must be expressed using exactly three digits.) -Strings in Lua can contain any 8-bit value, including embedded zeros, -which can be specified as '\0'. - - -

-Literal strings can also be defined using a long format -enclosed by long brackets. -We define an opening long bracket of level n as an opening -square bracket followed by n equal signs followed by another -opening square bracket. -So, an opening long bracket of level 0 is written as [[, -an opening long bracket of level 1 is written as [=[, -and so on. -A closing long bracket is defined similarly; -for instance, a closing long bracket of level 4 is written as ]====]. -A long string starts with an opening long bracket of any level and -ends at the first closing long bracket of the same level. -Literals in this bracketed form can run for several lines, -do not interpret any escape sequences, -and ignore long brackets of any other level. -They can contain anything except a closing bracket of the proper level. - - -

-For convenience, -when the opening long bracket is immediately followed by a newline, -the newline is not included in the string. -As an example, in a system using ASCII -(in which 'a' is coded as 97, -newline is coded as 10, and '1' is coded as 49), -the five literal strings below denote the same string: - -

-     a = 'alo\n123"'
-     a = "alo\n123\""
-     a = '\97lo\10\04923"'
-     a = [[alo
-     123"]]
-     a = [==[
-     alo
-     123"]==]
-
- -

-A numerical constant can be written with an optional decimal part -and an optional decimal exponent. -Lua also accepts integer hexadecimal constants, -by prefixing them with 0x. -Examples of valid numerical constants are - -

-     3   3.0   3.1416   314.16e-2   0.31416E1   0xff   0x56
-
- -

-A comment starts with a double hyphen (--) -anywhere outside a string. -If the text immediately after -- is not an opening long bracket, -the comment is a short comment, -which runs until the end of the line. -Otherwise, it is a long comment, -which runs until the corresponding closing long bracket. -Long comments are frequently used to disable code temporarily. - - - - - -

2.2 - Values and Types

- -

-Lua is a dynamically typed language. -This means that -variables do not have types; only values do. -There are no type definitions in the language. -All values carry their own type. - - -

-All values in Lua are first-class values. -This means that all values can be stored in variables, -passed as arguments to other functions, and returned as results. - - -

-There are eight basic types in Lua: -nil, boolean, number, -string, function, userdata, -thread, and table. -Nil is the type of the value nil, -whose main property is to be different from any other value; -it usually represents the absence of a useful value. -Boolean is the type of the values false and true. -Both nil and false make a condition false; -any other value makes it true. -Number represents real (double-precision floating-point) numbers. -(It is easy to build Lua interpreters that use other -internal representations for numbers, -such as single-precision float or long integers; -see file luaconf.h.) -String represents arrays of characters. - -Lua is 8-bit clean: -strings can contain any 8-bit character, -including embedded zeros ('\0') (see §2.1). - - -

-Lua can call (and manipulate) functions written in Lua and -functions written in C -(see §2.5.8). - - -

-The type userdata is provided to allow arbitrary C data to -be stored in Lua variables. -This type corresponds to a block of raw memory -and has no pre-defined operations in Lua, -except assignment and identity test. -However, by using metatables, -the programmer can define operations for userdata values -(see §2.8). -Userdata values cannot be created or modified in Lua, -only through the C API. -This guarantees the integrity of data owned by the host program. - - -

-The type thread represents independent threads of execution -and it is used to implement coroutines (see §2.11). -Do not confuse Lua threads with operating-system threads. -Lua supports coroutines on all systems, -even those that do not support threads. - - -

-The type table implements associative arrays, -that is, arrays that can be indexed not only with numbers, -but with any value (except nil). -Tables can be heterogeneous; -that is, they can contain values of all types (except nil). -Tables are the sole data structuring mechanism in Lua; -they can be used to represent ordinary arrays, -symbol tables, sets, records, graphs, trees, etc. -To represent records, Lua uses the field name as an index. -The language supports this representation by -providing a.name as syntactic sugar for a["name"]. -There are several convenient ways to create tables in Lua -(see §2.5.7). - - -

-Like indices, -the value of a table field can be of any type (except nil). -In particular, -because functions are first-class values, -table fields can contain functions. -Thus tables can also carry methods (see §2.5.9). - - -

-Tables, functions, threads, and (full) userdata values are objects: -variables do not actually contain these values, -only references to them. -Assignment, parameter passing, and function returns -always manipulate references to such values; -these operations do not imply any kind of copy. - - -

-The library function type returns a string describing the type -of a given value. - - - -

2.2.1 - Coercion

- -

-Lua provides automatic conversion between -string and number values at run time. -Any arithmetic operation applied to a string tries to convert -this string to a number, following the usual conversion rules. -Conversely, whenever a number is used where a string is expected, -the number is converted to a string, in a reasonable format. -For complete control over how numbers are converted to strings, -use the format function from the string library -(see string.format). - - - - - - - -

2.3 - Variables

- -

-Variables are places that store values. - -There are three kinds of variables in Lua: -global variables, local variables, and table fields. - - -

-A single name can denote a global variable or a local variable -(or a function's formal parameter, -which is a particular kind of local variable): - -

-	var ::= Name
-

-Name denotes identifiers, as defined in §2.1. - - -

-Any variable is assumed to be global unless explicitly declared -as a local (see §2.4.7). -Local variables are lexically scoped: -local variables can be freely accessed by functions -defined inside their scope (see §2.6). - - -

-Before the first assignment to a variable, its value is nil. - - -

-Square brackets are used to index a table: - -

-	var ::= prefixexp `[´ exp `]´
-

-The meaning of accesses to global variables -and table fields can be changed via metatables. -An access to an indexed variable t[i] is equivalent to -a call gettable_event(t,i). -(See §2.8 for a complete description of the -gettable_event function. -This function is not defined or callable in Lua. -We use it here only for explanatory purposes.) - - -

-The syntax var.Name is just syntactic sugar for -var["Name"]: - -

-	var ::= prefixexp `.´ Name
-
- -

-All global variables live as fields in ordinary Lua tables, -called environment tables or simply -environments (see §2.9). -Each function has its own reference to an environment, -so that all global variables in this function -will refer to this environment table. -When a function is created, -it inherits the environment from the function that created it. -To get the environment table of a Lua function, -you call getfenv. -To replace it, -you call setfenv. -(You can only manipulate the environment of C functions -through the debug library; (see §5.9).) - - -

-An access to a global variable x -is equivalent to _env.x, -which in turn is equivalent to - -

-     gettable_event(_env, "x")
-

-where _env is the environment of the running function. -(See §2.8 for a complete description of the -gettable_event function. -This function is not defined or callable in Lua. -Similarly, the _env variable is not defined in Lua. -We use them here only for explanatory purposes.) - - - - - -

2.4 - Statements

- -

-Lua supports an almost conventional set of statements, -similar to those in Pascal or C. -This set includes -assignments, control structures, function calls, -and variable declarations. - - - -

2.4.1 - Chunks

- -

-The unit of execution of Lua is called a chunk. -A chunk is simply a sequence of statements, -which are executed sequentially. -Each statement can be optionally followed by a semicolon: - -

-	chunk ::= {stat [`;´]}
-

-There are no empty statements and thus ';;' is not legal. - - -

-Lua handles a chunk as the body of an anonymous function -with a variable number of arguments -(see §2.5.9). -As such, chunks can define local variables, -receive arguments, and return values. - - -

-A chunk can be stored in a file or in a string inside the host program. -To execute a chunk, -Lua first pre-compiles the chunk into instructions for a virtual machine, -and then it executes the compiled code -with an interpreter for the virtual machine. - - -

-Chunks can also be pre-compiled into binary form; -see program luac for details. -Programs in source and compiled forms are interchangeable; -Lua automatically detects the file type and acts accordingly. - - - - - - -

2.4.2 - Blocks

-A block is a list of statements; -syntactically, a block is the same as a chunk: - -

-	block ::= chunk
-
- -

-A block can be explicitly delimited to produce a single statement: - -

-	stat ::= do block end
-

-Explicit blocks are useful -to control the scope of variable declarations. -Explicit blocks are also sometimes used to -add a return or break statement in the middle -of another block (see §2.4.4). - - - - - -

2.4.3 - Assignment

- -

-Lua allows multiple assignments. -Therefore, the syntax for assignment -defines a list of variables on the left side -and a list of expressions on the right side. -The elements in both lists are separated by commas: - -

-	stat ::= varlist `=´ explist
-	varlist ::= var {`,´ var}
-	explist ::= exp {`,´ exp}
-

-Expressions are discussed in §2.5. - - -

-Before the assignment, -the list of values is adjusted to the length of -the list of variables. -If there are more values than needed, -the excess values are thrown away. -If there are fewer values than needed, -the list is extended with as many nil's as needed. -If the list of expressions ends with a function call, -then all values returned by that call enter the list of values, -before the adjustment -(except when the call is enclosed in parentheses; see §2.5). - - -

-The assignment statement first evaluates all its expressions -and only then are the assignments performed. -Thus the code - -

-     i = 3
-     i, a[i] = i+1, 20
-

-sets a[3] to 20, without affecting a[4] -because the i in a[i] is evaluated (to 3) -before it is assigned 4. -Similarly, the line - -

-     x, y = y, x
-

-exchanges the values of x and y, -and - -

-     x, y, z = y, z, x
-

-cyclically permutes the values of x, y, and z. - - -

-The meaning of assignments to global variables -and table fields can be changed via metatables. -An assignment to an indexed variable t[i] = val is equivalent to -settable_event(t,i,val). -(See §2.8 for a complete description of the -settable_event function. -This function is not defined or callable in Lua. -We use it here only for explanatory purposes.) - - -

-An assignment to a global variable x = val -is equivalent to the assignment -_env.x = val, -which in turn is equivalent to - -

-     settable_event(_env, "x", val)
-

-where _env is the environment of the running function. -(The _env variable is not defined in Lua. -We use it here only for explanatory purposes.) - - - - - -

2.4.4 - Control Structures

-The control structures -if, while, and repeat have the usual meaning and -familiar syntax: - - - - -

-	stat ::= while exp do block end
-	stat ::= repeat block until exp
-	stat ::= if exp then block {elseif exp then block} [else block] end
-

-Lua also has a for statement, in two flavors (see §2.4.5). - - -

-The condition expression of a -control structure can return any value. -Both false and nil are considered false. -All values different from nil and false are considered true -(in particular, the number 0 and the empty string are also true). - - -

-In the repeatuntil loop, -the inner block does not end at the until keyword, -but only after the condition. -So, the condition can refer to local variables -declared inside the loop block. - - -

-The return statement is used to return values -from a function or a chunk (which is just a function). - -Functions and chunks can return more than one value, -and so the syntax for the return statement is - -

-	stat ::= return [explist]
-
- -

-The break statement is used to terminate the execution of a -while, repeat, or for loop, -skipping to the next statement after the loop: - - -

-	stat ::= break
-

-A break ends the innermost enclosing loop. - - -

-The return and break -statements can only be written as the last statement of a block. -If it is really necessary to return or break in the -middle of a block, -then an explicit inner block can be used, -as in the idioms -do return end and do break end, -because now return and break are the last statements in -their (inner) blocks. - - - - - -

2.4.5 - For Statement

- -

- -The for statement has two forms: -one numeric and one generic. - - -

-The numeric for loop repeats a block of code while a -control variable runs through an arithmetic progression. -It has the following syntax: - -

-	stat ::= for Name `=´ exp `,´ exp [`,´ exp] do block end
-

-The block is repeated for name starting at the value of -the first exp, until it passes the second exp by steps of the -third exp. -More precisely, a for statement like - -

-     for v = e1, e2, e3 do block end
-

-is equivalent to the code: - -

-     do
-       local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
-       if not (var and limit and step) then error() end
-       while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
-         local v = var
-         block
-         var = var + step
-       end
-     end
-

-Note the following: - -

    - -
  • -All three control expressions are evaluated only once, -before the loop starts. -They must all result in numbers. -
  • - -
  • -var, limit, and step are invisible variables. -The names shown here are for explanatory purposes only. -
  • - -
  • -If the third expression (the step) is absent, -then a step of 1 is used. -
  • - -
  • -You can use break to exit a for loop. -
  • - -
  • -The loop variable v is local to the loop; -you cannot use its value after the for ends or is broken. -If you need this value, -assign it to another variable before breaking or exiting the loop. -
  • - -
- -

-The generic for statement works over functions, -called iterators. -On each iteration, the iterator function is called to produce a new value, -stopping when this new value is nil. -The generic for loop has the following syntax: - -

-	stat ::= for namelist in explist do block end
-	namelist ::= Name {`,´ Name}
-

-A for statement like - -

-     for var_1, ···, var_n in explist do block end
-

-is equivalent to the code: - -

-     do
-       local f, s, var = explist
-       while true do
-         local var_1, ···, var_n = f(s, var)
-         var = var_1
-         if var == nil then break end
-         block
-       end
-     end
-

-Note the following: - -

    - -
  • -explist is evaluated only once. -Its results are an iterator function, -a state, -and an initial value for the first iterator variable. -
  • - -
  • -f, s, and var are invisible variables. -The names are here for explanatory purposes only. -
  • - -
  • -You can use break to exit a for loop. -
  • - -
  • -The loop variables var_i are local to the loop; -you cannot use their values after the for ends. -If you need these values, -then assign them to other variables before breaking or exiting the loop. -
  • - -
- - - - -

2.4.6 - Function Calls as Statements

-To allow possible side-effects, -function calls can be executed as statements: - -

-	stat ::= functioncall
-

-In this case, all returned values are thrown away. -Function calls are explained in §2.5.8. - - - - - -

2.4.7 - Local Declarations

-Local variables can be declared anywhere inside a block. -The declaration can include an initial assignment: - -

-	stat ::= local namelist [`=´ explist]
-

-If present, an initial assignment has the same semantics -of a multiple assignment (see §2.4.3). -Otherwise, all variables are initialized with nil. - - -

-A chunk is also a block (see §2.4.1), -and so local variables can be declared in a chunk outside any explicit block. -The scope of such local variables extends until the end of the chunk. - - -

-The visibility rules for local variables are explained in §2.6. - - - - - - - -

2.5 - Expressions

- -

-The basic expressions in Lua are the following: - -

-	exp ::= prefixexp
-	exp ::= nil | false | true
-	exp ::= Number
-	exp ::= String
-	exp ::= function
-	exp ::= tableconstructor
-	exp ::= `...´
-	exp ::= exp binop exp
-	exp ::= unop exp
-	prefixexp ::= var | functioncall | `(´ exp `)´
-
- -

-Numbers and literal strings are explained in §2.1; -variables are explained in §2.3; -function definitions are explained in §2.5.9; -function calls are explained in §2.5.8; -table constructors are explained in §2.5.7. -Vararg expressions, -denoted by three dots ('...'), can only be used when -directly inside a vararg function; -they are explained in §2.5.9. - - -

-Binary operators comprise arithmetic operators (see §2.5.1), -relational operators (see §2.5.2), logical operators (see §2.5.3), -and the concatenation operator (see §2.5.4). -Unary operators comprise the unary minus (see §2.5.1), -the unary not (see §2.5.3), -and the unary length operator (see §2.5.5). - - -

-Both function calls and vararg expressions can result in multiple values. -If an expression is used as a statement -(only possible for function calls (see §2.4.6)), -then its return list is adjusted to zero elements, -thus discarding all returned values. -If an expression is used as the last (or the only) element -of a list of expressions, -then no adjustment is made -(unless the call is enclosed in parentheses). -In all other contexts, -Lua adjusts the result list to one element, -discarding all values except the first one. - - -

-Here are some examples: - -

-     f()                -- adjusted to 0 results
-     g(f(), x)          -- f() is adjusted to 1 result
-     g(x, f())          -- g gets x plus all results from f()
-     a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)
-     a,b = ...          -- a gets the first vararg parameter, b gets
-                        -- the second (both a and b can get nil if there
-                        -- is no corresponding vararg parameter)
-     
-     a,b,c = x, f()     -- f() is adjusted to 2 results
-     a,b,c = f()        -- f() is adjusted to 3 results
-     return f()         -- returns all results from f()
-     return ...         -- returns all received vararg parameters
-     return x,y,f()     -- returns x, y, and all results from f()
-     {f()}              -- creates a list with all results from f()
-     {...}              -- creates a list with all vararg parameters
-     {f(), nil}         -- f() is adjusted to 1 result
-
- -

-Any expression enclosed in parentheses always results in only one value. -Thus, -(f(x,y,z)) is always a single value, -even if f returns several values. -(The value of (f(x,y,z)) is the first value returned by f -or nil if f does not return any values.) - - - -

2.5.1 - Arithmetic Operators

-Lua supports the usual arithmetic operators: -the binary + (addition), -- (subtraction), * (multiplication), -/ (division), % (modulo), and ^ (exponentiation); -and unary - (negation). -If the operands are numbers, or strings that can be converted to -numbers (see §2.2.1), -then all operations have the usual meaning. -Exponentiation works for any exponent. -For instance, x^(-0.5) computes the inverse of the square root of x. -Modulo is defined as - -

-     a % b == a - math.floor(a/b)*b
-

-That is, it is the remainder of a division that rounds -the quotient towards minus infinity. - - - - - -

2.5.2 - Relational Operators

-The relational operators in Lua are - -

-     ==    ~=    <     >     <=    >=
-

-These operators always result in false or true. - - -

-Equality (==) first compares the type of its operands. -If the types are different, then the result is false. -Otherwise, the values of the operands are compared. -Numbers and strings are compared in the usual way. -Objects (tables, userdata, threads, and functions) -are compared by reference: -two objects are considered equal only if they are the same object. -Every time you create a new object -(a table, userdata, thread, or function), -this new object is different from any previously existing object. - - -

-You can change the way that Lua compares tables and userdata -by using the "eq" metamethod (see §2.8). - - -

-The conversion rules of §2.2.1 -do not apply to equality comparisons. -Thus, "0"==0 evaluates to false, -and t[0] and t["0"] denote different -entries in a table. - - -

-The operator ~= is exactly the negation of equality (==). - - -

-The order operators work as follows. -If both arguments are numbers, then they are compared as such. -Otherwise, if both arguments are strings, -then their values are compared according to the current locale. -Otherwise, Lua tries to call the "lt" or the "le" -metamethod (see §2.8). -A comparison a > b is translated to b < a -and a >= b is translated to b <= a. - - - - - -

2.5.3 - Logical Operators

-The logical operators in Lua are -and, or, and not. -Like the control structures (see §2.4.4), -all logical operators consider both false and nil as false -and anything else as true. - - -

-The negation operator not always returns false or true. -The conjunction operator and returns its first argument -if this value is false or nil; -otherwise, and returns its second argument. -The disjunction operator or returns its first argument -if this value is different from nil and false; -otherwise, or returns its second argument. -Both and and or use short-cut evaluation; -that is, -the second operand is evaluated only if necessary. -Here are some examples: - -

-     10 or 20            --> 10
-     10 or error()       --> 10
-     nil or "a"          --> "a"
-     nil and 10          --> nil
-     false and error()   --> false
-     false and nil       --> false
-     false or nil        --> nil
-     10 and 20           --> 20
-

-(In this manual, ---> indicates the result of the preceding expression.) - - - - - -

2.5.4 - Concatenation

-The string concatenation operator in Lua is -denoted by two dots ('..'). -If both operands are strings or numbers, then they are converted to -strings according to the rules mentioned in §2.2.1. -Otherwise, the "concat" metamethod is called (see §2.8). - - - - - -

2.5.5 - The Length Operator

- -

-The length operator is denoted by the unary operator #. -The length of a string is its number of bytes -(that is, the usual meaning of string length when each -character is one byte). - - -

-The length of a table t is defined to be any -integer index n -such that t[n] is not nil and t[n+1] is nil; -moreover, if t[1] is nil, n can be zero. -For a regular array, with non-nil values from 1 to a given n, -its length is exactly that n, -the index of its last value. -If the array has "holes" -(that is, nil values between other non-nil values), -then #t can be any of the indices that -directly precedes a nil value -(that is, it may consider any such nil value as the end of -the array). - - - - - -

2.5.6 - Precedence

-Operator precedence in Lua follows the table below, -from lower to higher priority: - -

-     or
-     and
-     <     >     <=    >=    ~=    ==
-     ..
-     +     -
-     *     /     %
-     not   #     - (unary)
-     ^
-

-As usual, -you can use parentheses to change the precedences of an expression. -The concatenation ('..') and exponentiation ('^') -operators are right associative. -All other binary operators are left associative. - - - - - -

2.5.7 - Table Constructors

-Table constructors are expressions that create tables. -Every time a constructor is evaluated, a new table is created. -A constructor can be used to create an empty table -or to create a table and initialize some of its fields. -The general syntax for constructors is - -

-	tableconstructor ::= `{´ [fieldlist] `}´
-	fieldlist ::= field {fieldsep field} [fieldsep]
-	field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp
-	fieldsep ::= `,´ | `;´
-
- -

-Each field of the form [exp1] = exp2 adds to the new table an entry -with key exp1 and value exp2. -A field of the form name = exp is equivalent to -["name"] = exp. -Finally, fields of the form exp are equivalent to -[i] = exp, where i are consecutive numerical integers, -starting with 1. -Fields in the other formats do not affect this counting. -For example, - -

-     a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
-

-is equivalent to - -

-     do
-       local t = {}
-       t[f(1)] = g
-       t[1] = "x"         -- 1st exp
-       t[2] = "y"         -- 2nd exp
-       t.x = 1            -- t["x"] = 1
-       t[3] = f(x)        -- 3rd exp
-       t[30] = 23
-       t[4] = 45          -- 4th exp
-       a = t
-     end
-
- -

-If the last field in the list has the form exp -and the expression is a function call or a vararg expression, -then all values returned by this expression enter the list consecutively -(see §2.5.8). -To avoid this, -enclose the function call or the vararg expression -in parentheses (see §2.5). - - -

-The field list can have an optional trailing separator, -as a convenience for machine-generated code. - - - - - -

2.5.8 - Function Calls

-A function call in Lua has the following syntax: - -

-	functioncall ::= prefixexp args
-

-In a function call, -first prefixexp and args are evaluated. -If the value of prefixexp has type function, -then this function is called -with the given arguments. -Otherwise, the prefixexp "call" metamethod is called, -having as first parameter the value of prefixexp, -followed by the original call arguments -(see §2.8). - - -

-The form - -

-	functioncall ::= prefixexp `:´ Name args
-

-can be used to call "methods". -A call v:name(args) -is syntactic sugar for v.name(v,args), -except that v is evaluated only once. - - -

-Arguments have the following syntax: - -

-	args ::= `(´ [explist] `)´
-	args ::= tableconstructor
-	args ::= String
-

-All argument expressions are evaluated before the call. -A call of the form f{fields} is -syntactic sugar for f({fields}); -that is, the argument list is a single new table. -A call of the form f'string' -(or f"string" or f[[string]]) -is syntactic sugar for f('string'); -that is, the argument list is a single literal string. - - -

-As an exception to the free-format syntax of Lua, -you cannot put a line break before the '(' in a function call. -This restriction avoids some ambiguities in the language. -If you write - -

-     a = f
-     (g).x(a)
-

-Lua would see that as a single statement, a = f(g).x(a). -So, if you want two statements, you must add a semi-colon between them. -If you actually want to call f, -you must remove the line break before (g). - - -

-A call of the form return functioncall is called -a tail call. -Lua implements proper tail calls -(or proper tail recursion): -in a tail call, -the called function reuses the stack entry of the calling function. -Therefore, there is no limit on the number of nested tail calls that -a program can execute. -However, a tail call erases any debug information about the -calling function. -Note that a tail call only happens with a particular syntax, -where the return has one single function call as argument; -this syntax makes the calling function return exactly -the returns of the called function. -So, none of the following examples are tail calls: - -

-     return (f(x))        -- results adjusted to 1
-     return 2 * f(x)
-     return x, f(x)       -- additional results
-     f(x); return         -- results discarded
-     return x or f(x)     -- results adjusted to 1
-
- - - - -

2.5.9 - Function Definitions

- -

-The syntax for function definition is - -

-	function ::= function funcbody
-	funcbody ::= `(´ [parlist] `)´ block end
-
- -

-The following syntactic sugar simplifies function definitions: - -

-	stat ::= function funcname funcbody
-	stat ::= local function Name funcbody
-	funcname ::= Name {`.´ Name} [`:´ Name]
-

-The statement - -

-     function f () body end
-

-translates to - -

-     f = function () body end
-

-The statement - -

-     function t.a.b.c.f () body end
-

-translates to - -

-     t.a.b.c.f = function () body end
-

-The statement - -

-     local function f () body end
-

-translates to - -

-     local f; f = function () body end
-

-not to - -

-     local f = function () body end
-

-(This only makes a difference when the body of the function -contains references to f.) - - -

-A function definition is an executable expression, -whose value has type function. -When Lua pre-compiles a chunk, -all its function bodies are pre-compiled too. -Then, whenever Lua executes the function definition, -the function is instantiated (or closed). -This function instance (or closure) -is the final value of the expression. -Different instances of the same function -can refer to different external local variables -and can have different environment tables. - - -

-Parameters act as local variables that are -initialized with the argument values: - -

-	parlist ::= namelist [`,´ `...´] | `...´
-

-When a function is called, -the list of arguments is adjusted to -the length of the list of parameters, -unless the function is a variadic or vararg function, -which is -indicated by three dots ('...') at the end of its parameter list. -A vararg function does not adjust its argument list; -instead, it collects all extra arguments and supplies them -to the function through a vararg expression, -which is also written as three dots. -The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results. -If a vararg expression is used inside another expression -or in the middle of a list of expressions, -then its return list is adjusted to one element. -If the expression is used as the last element of a list of expressions, -then no adjustment is made -(unless that last expression is enclosed in parentheses). - - -

-As an example, consider the following definitions: - -

-     function f(a, b) end
-     function g(a, b, ...) end
-     function r() return 1,2,3 end
-

-Then, we have the following mapping from arguments to parameters and -to the vararg expression: - -

-     CALL            PARAMETERS
-     
-     f(3)             a=3, b=nil
-     f(3, 4)          a=3, b=4
-     f(3, 4, 5)       a=3, b=4
-     f(r(), 10)       a=1, b=10
-     f(r())           a=1, b=2
-     
-     g(3)             a=3, b=nil, ... -->  (nothing)
-     g(3, 4)          a=3, b=4,   ... -->  (nothing)
-     g(3, 4, 5, 8)    a=3, b=4,   ... -->  5  8
-     g(5, r())        a=5, b=1,   ... -->  2  3
-
- -

-Results are returned using the return statement (see §2.4.4). -If control reaches the end of a function -without encountering a return statement, -then the function returns with no results. - - -

-The colon syntax -is used for defining methods, -that is, functions that have an implicit extra parameter self. -Thus, the statement - -

-     function t.a.b.c:f (params) body end
-

-is syntactic sugar for - -

-     t.a.b.c.f = function (self, params) body end
-
- - - - - - -

2.6 - Visibility Rules

- -

- -Lua is a lexically scoped language. -The scope of variables begins at the first statement after -their declaration and lasts until the end of the innermost block that -includes the declaration. -Consider the following example: - -

-     x = 10                -- global variable
-     do                    -- new block
-       local x = x         -- new 'x', with value 10
-       print(x)            --> 10
-       x = x+1
-       do                  -- another block
-         local x = x+1     -- another 'x'
-         print(x)          --> 12
-       end
-       print(x)            --> 11
-     end
-     print(x)              --> 10  (the global one)
-
- -

-Notice that, in a declaration like local x = x, -the new x being declared is not in scope yet, -and so the second x refers to the outside variable. - - -

-Because of the lexical scoping rules, -local variables can be freely accessed by functions -defined inside their scope. -A local variable used by an inner function is called -an upvalue, or external local variable, -inside the inner function. - - -

-Notice that each execution of a local statement -defines new local variables. -Consider the following example: - -

-     a = {}
-     local x = 20
-     for i=1,10 do
-       local y = 0
-       a[i] = function () y=y+1; return x+y end
-     end
-

-The loop creates ten closures -(that is, ten instances of the anonymous function). -Each of these closures uses a different y variable, -while all of them share the same x. - - - - - -

2.7 - Error Handling

- -

-Because Lua is an embedded extension language, -all Lua actions start from C code in the host program -calling a function from the Lua library (see lua_pcall). -Whenever an error occurs during Lua compilation or execution, -control returns to C, -which can take appropriate measures -(such as printing an error message). - - -

-Lua code can explicitly generate an error by calling the -error function. -If you need to catch errors in Lua, -you can use the pcall function. - - - - - -

2.8 - Metatables

- -

-Every value in Lua can have a metatable. -This metatable is an ordinary Lua table -that defines the behavior of the original value -under certain special operations. -You can change several aspects of the behavior -of operations over a value by setting specific fields in its metatable. -For instance, when a non-numeric value is the operand of an addition, -Lua checks for a function in the field "__add" in its metatable. -If it finds one, -Lua calls this function to perform the addition. - - -

-We call the keys in a metatable events -and the values metamethods. -In the previous example, the event is "add" -and the metamethod is the function that performs the addition. - - -

-You can query the metatable of any value -through the getmetatable function. - - -

-You can replace the metatable of tables -through the setmetatable -function. -You cannot change the metatable of other types from Lua -(except by using the debug library); -you must use the C API for that. - - -

-Tables and full userdata have individual metatables -(although multiple tables and userdata can share their metatables). -Values of all other types share one single metatable per type; -that is, there is one single metatable for all numbers, -one for all strings, etc. - - -

-A metatable controls how an object behaves in arithmetic operations, -order comparisons, concatenation, length operation, and indexing. -A metatable also can define a function to be called when a userdata -is garbage collected. -For each of these operations Lua associates a specific key -called an event. -When Lua performs one of these operations over a value, -it checks whether this value has a metatable with the corresponding event. -If so, the value associated with that key (the metamethod) -controls how Lua will perform the operation. - - -

-Metatables control the operations listed next. -Each operation is identified by its corresponding name. -The key for each operation is a string with its name prefixed by -two underscores, '__'; -for instance, the key for operation "add" is the -string "__add". -The semantics of these operations is better explained by a Lua function -describing how the interpreter executes the operation. - - -

-The code shown here in Lua is only illustrative; -the real behavior is hard coded in the interpreter -and it is much more efficient than this simulation. -All functions used in these descriptions -(rawget, tonumber, etc.) -are described in §5.1. -In particular, to retrieve the metamethod of a given object, -we use the expression - -

-     metatable(obj)[event]
-

-This should be read as - -

-     rawget(getmetatable(obj) or {}, event)
-

- -That is, the access to a metamethod does not invoke other metamethods, -and the access to objects with no metatables does not fail -(it simply results in nil). - - - -

    - -
  • "add": -the + operation. - - - -

    -The function getbinhandler below defines how Lua chooses a handler -for a binary operation. -First, Lua tries the first operand. -If its type does not define a handler for the operation, -then Lua tries the second operand. - -

    -     function getbinhandler (op1, op2, event)
    -       return metatable(op1)[event] or metatable(op2)[event]
    -     end
    -

    -By using this function, -the behavior of the op1 + op2 is - -

    -     function add_event (op1, op2)
    -       local o1, o2 = tonumber(op1), tonumber(op2)
    -       if o1 and o2 then  -- both operands are numeric?
    -         return o1 + o2   -- '+' here is the primitive 'add'
    -       else  -- at least one of the operands is not numeric
    -         local h = getbinhandler(op1, op2, "__add")
    -         if h then
    -           -- call the handler with both operands
    -           return (h(op1, op2))
    -         else  -- no handler available: default behavior
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
  • "sub": -the - operation. - -Behavior similar to the "add" operation. -
  • - -
  • "mul": -the * operation. - -Behavior similar to the "add" operation. -
  • - -
  • "div": -the / operation. - -Behavior similar to the "add" operation. -
  • - -
  • "mod": -the % operation. - -Behavior similar to the "add" operation, -with the operation -o1 - floor(o1/o2)*o2 as the primitive operation. -
  • - -
  • "pow": -the ^ (exponentiation) operation. - -Behavior similar to the "add" operation, -with the function pow (from the C math library) -as the primitive operation. -
  • - -
  • "unm": -the unary - operation. - - -
    -     function unm_event (op)
    -       local o = tonumber(op)
    -       if o then  -- operand is numeric?
    -         return -o  -- '-' here is the primitive 'unm'
    -       else  -- the operand is not numeric.
    -         -- Try to get a handler from the operand
    -         local h = metatable(op).__unm
    -         if h then
    -           -- call the handler with the operand
    -           return (h(op))
    -         else  -- no handler available: default behavior
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
  • "concat": -the .. (concatenation) operation. - - -
    -     function concat_event (op1, op2)
    -       if (type(op1) == "string" or type(op1) == "number") and
    -          (type(op2) == "string" or type(op2) == "number") then
    -         return op1 .. op2  -- primitive string concatenation
    -       else
    -         local h = getbinhandler(op1, op2, "__concat")
    -         if h then
    -           return (h(op1, op2))
    -         else
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
  • "len": -the # operation. - - -
    -     function len_event (op)
    -       if type(op) == "string" then
    -         return strlen(op)         -- primitive string length
    -       elseif type(op) == "table" then
    -         return #op                -- primitive table length
    -       else
    -         local h = metatable(op).__len
    -         if h then
    -           -- call the handler with the operand
    -           return (h(op))
    -         else  -- no handler available: default behavior
    -           error(···)
    -         end
    -       end
    -     end
    -

    -See §2.5.5 for a description of the length of a table. -

  • - -
  • "eq": -the == operation. - -The function getcomphandler defines how Lua chooses a metamethod -for comparison operators. -A metamethod only is selected when both objects -being compared have the same type -and the same metamethod for the selected operation. - -
    -     function getcomphandler (op1, op2, event)
    -       if type(op1) ~= type(op2) then return nil end
    -       local mm1 = metatable(op1)[event]
    -       local mm2 = metatable(op2)[event]
    -       if mm1 == mm2 then return mm1 else return nil end
    -     end
    -

    -The "eq" event is defined as follows: - -

    -     function eq_event (op1, op2)
    -       if type(op1) ~= type(op2) then  -- different types?
    -         return false   -- different objects
    -       end
    -       if op1 == op2 then   -- primitive equal?
    -         return true   -- objects are equal
    -       end
    -       -- try metamethod
    -       local h = getcomphandler(op1, op2, "__eq")
    -       if h then
    -         return (h(op1, op2))
    -       else
    -         return false
    -       end
    -     end
    -

    -a ~= b is equivalent to not (a == b). -

  • - -
  • "lt": -the < operation. - - -
    -     function lt_event (op1, op2)
    -       if type(op1) == "number" and type(op2) == "number" then
    -         return op1 < op2   -- numeric comparison
    -       elseif type(op1) == "string" and type(op2) == "string" then
    -         return op1 < op2   -- lexicographic comparison
    -       else
    -         local h = getcomphandler(op1, op2, "__lt")
    -         if h then
    -           return (h(op1, op2))
    -         else
    -           error(···)
    -         end
    -       end
    -     end
    -

    -a > b is equivalent to b < a. -

  • - -
  • "le": -the <= operation. - - -
    -     function le_event (op1, op2)
    -       if type(op1) == "number" and type(op2) == "number" then
    -         return op1 <= op2   -- numeric comparison
    -       elseif type(op1) == "string" and type(op2) == "string" then
    -         return op1 <= op2   -- lexicographic comparison
    -       else
    -         local h = getcomphandler(op1, op2, "__le")
    -         if h then
    -           return (h(op1, op2))
    -         else
    -           h = getcomphandler(op1, op2, "__lt")
    -           if h then
    -             return not h(op2, op1)
    -           else
    -             error(···)
    -           end
    -         end
    -       end
    -     end
    -

    -a >= b is equivalent to b <= a. -Note that, in the absence of a "le" metamethod, -Lua tries the "lt", assuming that a <= b is -equivalent to not (b < a). -

  • - -
  • "index": -The indexing access table[key]. - - -
    -     function gettable_event (table, key)
    -       local h
    -       if type(table) == "table" then
    -         local v = rawget(table, key)
    -         if v ~= nil then return v end
    -         h = metatable(table).__index
    -         if h == nil then return nil end
    -       else
    -         h = metatable(table).__index
    -         if h == nil then
    -           error(···)
    -         end
    -       end
    -       if type(h) == "function" then
    -         return (h(table, key))     -- call the handler
    -       else return h[key]           -- or repeat operation on it
    -       end
    -     end
    -

    -

  • - -
  • "newindex": -The indexing assignment table[key] = value. - - -
    -     function settable_event (table, key, value)
    -       local h
    -       if type(table) == "table" then
    -         local v = rawget(table, key)
    -         if v ~= nil then rawset(table, key, value); return end
    -         h = metatable(table).__newindex
    -         if h == nil then rawset(table, key, value); return end
    -       else
    -         h = metatable(table).__newindex
    -         if h == nil then
    -           error(···)
    -         end
    -       end
    -       if type(h) == "function" then
    -         h(table, key,value)           -- call the handler
    -       else h[key] = value             -- or repeat operation on it
    -       end
    -     end
    -

    -

  • - -
  • "call": -called when Lua calls a value. - - -
    -     function function_event (func, ...)
    -       if type(func) == "function" then
    -         return func(...)   -- primitive call
    -       else
    -         local h = metatable(func).__call
    -         if h then
    -           return h(func, ...)
    -         else
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
- - - - -

2.9 - Environments

- -

-Besides metatables, -objects of types thread, function, and userdata -have another table associated with them, -called their environment. -Like metatables, environments are regular tables and -multiple objects can share the same environment. - - -

-Threads are created sharing the environment of the creating thread. -Userdata and C functions are created sharing the environment -of the creating C function. -Non-nested Lua functions -(created by loadfile, loadstring or load) -are created sharing the environment of the creating thread. -Nested Lua functions are created sharing the environment of -the creating Lua function. - - -

-Environments associated with userdata have no meaning for Lua. -It is only a convenience feature for programmers to associate a table to -a userdata. - - -

-Environments associated with threads are called -global environments. -They are used as the default environment for threads and -non-nested Lua functions created by the thread -and can be directly accessed by C code (see §3.3). - - -

-The environment associated with a C function can be directly -accessed by C code (see §3.3). -It is used as the default environment for other C functions -and userdata created by the function. - - -

-Environments associated with Lua functions are used to resolve -all accesses to global variables within the function (see §2.3). -They are used as the default environment for nested Lua functions -created by the function. - - -

-You can change the environment of a Lua function or the -running thread by calling setfenv. -You can get the environment of a Lua function or the running thread -by calling getfenv. -To manipulate the environment of other objects -(userdata, C functions, other threads) you must -use the C API. - - - - - -

2.10 - Garbage Collection

- -

-Lua performs automatic memory management. -This means that -you have to worry neither about allocating memory for new objects -nor about freeing it when the objects are no longer needed. -Lua manages memory automatically by running -a garbage collector from time to time -to collect all dead objects -(that is, objects that are no longer accessible from Lua). -All memory used by Lua is subject to automatic management: -tables, userdata, functions, threads, strings, etc. - - -

-Lua implements an incremental mark-and-sweep collector. -It uses two numbers to control its garbage-collection cycles: -the garbage-collector pause and -the garbage-collector step multiplier. -Both use percentage points as units -(so that a value of 100 means an internal value of 1). - - -

-The garbage-collector pause -controls how long the collector waits before starting a new cycle. -Larger values make the collector less aggressive. -Values smaller than 100 mean the collector will not wait to -start a new cycle. -A value of 200 means that the collector waits for the total memory in use -to double before starting a new cycle. - - -

-The step multiplier -controls the relative speed of the collector relative to -memory allocation. -Larger values make the collector more aggressive but also increase -the size of each incremental step. -Values smaller than 100 make the collector too slow and -can result in the collector never finishing a cycle. -The default, 200, means that the collector runs at "twice" -the speed of memory allocation. - - -

-You can change these numbers by calling lua_gc in C -or collectgarbage in Lua. -With these functions you can also control -the collector directly (e.g., stop and restart it). - - - -

2.10.1 - Garbage-Collection Metamethods

- -

-Using the C API, -you can set garbage-collector metamethods for userdata (see §2.8). -These metamethods are also called finalizers. -Finalizers allow you to coordinate Lua's garbage collection -with external resource management -(such as closing files, network or database connections, -or freeing your own memory). - - -

-Garbage userdata with a field __gc in their metatables are not -collected immediately by the garbage collector. -Instead, Lua puts them in a list. -After the collection, -Lua does the equivalent of the following function -for each userdata in that list: - -

-     function gc_event (udata)
-       local h = metatable(udata).__gc
-       if h then
-         h(udata)
-       end
-     end
-
- -

-At the end of each garbage-collection cycle, -the finalizers for userdata are called in reverse -order of their creation, -among those collected in that cycle. -That is, the first finalizer to be called is the one associated -with the userdata created last in the program. -The userdata itself is freed only in the next garbage-collection cycle. - - - - - -

2.10.2 - Weak Tables

- -

-A weak table is a table whose elements are -weak references. -A weak reference is ignored by the garbage collector. -In other words, -if the only references to an object are weak references, -then the garbage collector will collect this object. - - -

-A weak table can have weak keys, weak values, or both. -A table with weak keys allows the collection of its keys, -but prevents the collection of its values. -A table with both weak keys and weak values allows the collection of -both keys and values. -In any case, if either the key or the value is collected, -the whole pair is removed from the table. -The weakness of a table is controlled by the -__mode field of its metatable. -If the __mode field is a string containing the character 'k', -the keys in the table are weak. -If __mode contains 'v', -the values in the table are weak. - - -

-After you use a table as a metatable, -you should not change the value of its __mode field. -Otherwise, the weak behavior of the tables controlled by this -metatable is undefined. - - - - - - - -

2.11 - Coroutines

- -

-Lua supports coroutines, -also called collaborative multithreading. -A coroutine in Lua represents an independent thread of execution. -Unlike threads in multithread systems, however, -a coroutine only suspends its execution by explicitly calling -a yield function. - - -

-You create a coroutine with a call to coroutine.create. -Its sole argument is a function -that is the main function of the coroutine. -The create function only creates a new coroutine and -returns a handle to it (an object of type thread); -it does not start the coroutine execution. - - -

-When you first call coroutine.resume, -passing as its first argument -a thread returned by coroutine.create, -the coroutine starts its execution, -at the first line of its main function. -Extra arguments passed to coroutine.resume are passed on -to the coroutine main function. -After the coroutine starts running, -it runs until it terminates or yields. - - -

-A coroutine can terminate its execution in two ways: -normally, when its main function returns -(explicitly or implicitly, after the last instruction); -and abnormally, if there is an unprotected error. -In the first case, coroutine.resume returns true, -plus any values returned by the coroutine main function. -In case of errors, coroutine.resume returns false -plus an error message. - - -

-A coroutine yields by calling coroutine.yield. -When a coroutine yields, -the corresponding coroutine.resume returns immediately, -even if the yield happens inside nested function calls -(that is, not in the main function, -but in a function directly or indirectly called by the main function). -In the case of a yield, coroutine.resume also returns true, -plus any values passed to coroutine.yield. -The next time you resume the same coroutine, -it continues its execution from the point where it yielded, -with the call to coroutine.yield returning any extra -arguments passed to coroutine.resume. - - -

-Like coroutine.create, -the coroutine.wrap function also creates a coroutine, -but instead of returning the coroutine itself, -it returns a function that, when called, resumes the coroutine. -Any arguments passed to this function -go as extra arguments to coroutine.resume. -coroutine.wrap returns all the values returned by coroutine.resume, -except the first one (the boolean error code). -Unlike coroutine.resume, -coroutine.wrap does not catch errors; -any error is propagated to the caller. - - -

-As an example, -consider the following code: - -

-     function foo (a)
-       print("foo", a)
-       return coroutine.yield(2*a)
-     end
-     
-     co = coroutine.create(function (a,b)
-           print("co-body", a, b)
-           local r = foo(a+1)
-           print("co-body", r)
-           local r, s = coroutine.yield(a+b, a-b)
-           print("co-body", r, s)
-           return b, "end"
-     end)
-            
-     print("main", coroutine.resume(co, 1, 10))
-     print("main", coroutine.resume(co, "r"))
-     print("main", coroutine.resume(co, "x", "y"))
-     print("main", coroutine.resume(co, "x", "y"))
-

-When you run it, it produces the following output: - -

-     co-body 1       10
-     foo     2
-     
-     main    true    4
-     co-body r
-     main    true    11      -9
-     co-body x       y
-     main    true    10      end
-     main    false   cannot resume dead coroutine
-
- - - - -

3 - The Application Program Interface

- -

- -This section describes the C API for Lua, that is, -the set of C functions available to the host program to communicate -with Lua. -All API functions and related types and constants -are declared in the header file lua.h. - - -

-Even when we use the term "function", -any facility in the API may be provided as a macro instead. -All such macros use each of their arguments exactly once -(except for the first argument, which is always a Lua state), -and so do not generate any hidden side-effects. - - -

-As in most C libraries, -the Lua API functions do not check their arguments for validity or consistency. -However, you can change this behavior by compiling Lua -with a proper definition for the macro luai_apicheck, -in file luaconf.h. - - - -

3.1 - The Stack

- -

-Lua uses a virtual stack to pass values to and from C. -Each element in this stack represents a Lua value -(nil, number, string, etc.). - - -

-Whenever Lua calls C, the called function gets a new stack, -which is independent of previous stacks and of stacks of -C functions that are still active. -This stack initially contains any arguments to the C function -and it is where the C function pushes its results -to be returned to the caller (see lua_CFunction). - - -

-For convenience, -most query operations in the API do not follow a strict stack discipline. -Instead, they can refer to any element in the stack -by using an index: -A positive index represents an absolute stack position -(starting at 1); -a negative index represents an offset relative to the top of the stack. -More specifically, if the stack has n elements, -then index 1 represents the first element -(that is, the element that was pushed onto the stack first) -and -index n represents the last element; -index -1 also represents the last element -(that is, the element at the top) -and index -n represents the first element. -We say that an index is valid -if it lies between 1 and the stack top -(that is, if 1 ≤ abs(index) ≤ top). - - - - - - -

3.2 - Stack Size

- -

-When you interact with Lua API, -you are responsible for ensuring consistency. -In particular, -you are responsible for controlling stack overflow. -You can use the function lua_checkstack -to grow the stack size. - - -

-Whenever Lua calls C, -it ensures that at least LUA_MINSTACK stack positions are available. -LUA_MINSTACK is defined as 20, -so that usually you do not have to worry about stack space -unless your code has loops pushing elements onto the stack. - - -

-Most query functions accept as indices any value inside the -available stack space, that is, indices up to the maximum stack size -you have set through lua_checkstack. -Such indices are called acceptable indices. -More formally, we define an acceptable index -as follows: - -

-     (index < 0 && abs(index) <= top) ||
-     (index > 0 && index <= stackspace)
-

-Note that 0 is never an acceptable index. - - - - - -

3.3 - Pseudo-Indices

- -

-Unless otherwise noted, -any function that accepts valid indices can also be called with -pseudo-indices, -which represent some Lua values that are accessible to C code -but which are not in the stack. -Pseudo-indices are used to access the thread environment, -the function environment, -the registry, -and the upvalues of a C function (see §3.4). - - -

-The thread environment (where global variables live) is -always at pseudo-index LUA_GLOBALSINDEX. -The environment of the running C function is always -at pseudo-index LUA_ENVIRONINDEX. - - -

-To access and change the value of global variables, -you can use regular table operations over an environment table. -For instance, to access the value of a global variable, do - -

-     lua_getfield(L, LUA_GLOBALSINDEX, varname);
-
- - - - -

3.4 - C Closures

- -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure; -these values are called upvalues and are -accessible to the function whenever it is called -(see lua_pushcclosure). - - -

-Whenever a C function is called, -its upvalues are located at specific pseudo-indices. -These pseudo-indices are produced by the macro -lua_upvalueindex. -The first value associated with a function is at position -lua_upvalueindex(1), and so on. -Any access to lua_upvalueindex(n), -where n is greater than the number of upvalues of the -current function (but not greater than 256), -produces an acceptable (but invalid) index. - - - - - -

3.5 - Registry

- -

-Lua provides a registry, -a pre-defined table that can be used by any C code to -store whatever Lua value it needs to store. -This table is always located at pseudo-index -LUA_REGISTRYINDEX. -Any C library can store data into this table, -but it should take care to choose keys different from those used -by other libraries, to avoid collisions. -Typically, you should use as key a string containing your library name -or a light userdata with the address of a C object in your code. - - -

-The integer keys in the registry are used by the reference mechanism, -implemented by the auxiliary library, -and therefore should not be used for other purposes. - - - - - -

3.6 - Error Handling in C

- -

-Internally, Lua uses the C longjmp facility to handle errors. -(You can also choose to use exceptions if you use C++; -see file luaconf.h.) -When Lua faces any error -(such as memory allocation errors, type errors, syntax errors, -and runtime errors) -it raises an error; -that is, it does a long jump. -A protected environment uses setjmp -to set a recover point; -any error jumps to the most recent active recover point. - - -

-Most functions in the API can throw an error, -for instance due to a memory allocation error. -The documentation for each function indicates whether -it can throw errors. - - -

-Inside a C function you can throw an error by calling lua_error. - - - - - -

3.7 - Functions and Types

- -

-Here we list all functions and types from the C API in -alphabetical order. -Each function has an indicator like this: -[-o, +p, x] - - -

-The first field, o, -is how many elements the function pops from the stack. -The second field, p, -is how many elements the function pushes onto the stack. -(Any function always pushes its results after popping its arguments.) -A field in the form x|y means the function can push (or pop) -x or y elements, -depending on the situation; -an interrogation mark '?' means that -we cannot know how many elements the function pops/pushes -by looking only at its arguments -(e.g., they may depend on what is on the stack). -The third field, x, -tells whether the function may throw errors: -'-' means the function never throws any error; -'m' means the function may throw an error -only due to not enough memory; -'e' means the function may throw other kinds of errors; -'v' means the function may throw an error on purpose. - - - -


lua_Alloc

-
typedef void * (*lua_Alloc) (void *ud,
-                             void *ptr,
-                             size_t osize,
-                             size_t nsize);
- -

-The type of the memory-allocation function used by Lua states. -The allocator function must provide a -functionality similar to realloc, -but not exactly the same. -Its arguments are -ud, an opaque pointer passed to lua_newstate; -ptr, a pointer to the block being allocated/reallocated/freed; -osize, the original size of the block; -nsize, the new size of the block. -ptr is NULL if and only if osize is zero. -When nsize is zero, the allocator must return NULL; -if osize is not zero, -it should free the block pointed to by ptr. -When nsize is not zero, the allocator returns NULL -if and only if it cannot fill the request. -When nsize is not zero and osize is zero, -the allocator should behave like malloc. -When nsize and osize are not zero, -the allocator behaves like realloc. -Lua assumes that the allocator never fails when -osize >= nsize. - - -

-Here is a simple implementation for the allocator function. -It is used in the auxiliary library by luaL_newstate. - -

-     static void *l_alloc (void *ud, void *ptr, size_t osize,
-                                                size_t nsize) {
-       (void)ud;  (void)osize;  /* not used */
-       if (nsize == 0) {
-         free(ptr);
-         return NULL;
-       }
-       else
-         return realloc(ptr, nsize);
-     }
-

-This code assumes -that free(NULL) has no effect and that -realloc(NULL, size) is equivalent to malloc(size). -ANSI C ensures both behaviors. - - - - - -


lua_atpanic

-[-0, +0, -] -

lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
- -

-Sets a new panic function and returns the old one. - - -

-If an error happens outside any protected environment, -Lua calls a panic function -and then calls exit(EXIT_FAILURE), -thus exiting the host application. -Your panic function can avoid this exit by -never returning (e.g., doing a long jump). - - -

-The panic function can access the error message at the top of the stack. - - - - - -


lua_call

-[-(nargs + 1), +nresults, e] -

void lua_call (lua_State *L, int nargs, int nresults);
- -

-Calls a function. - - -

-To call a function you must use the following protocol: -first, the function to be called is pushed onto the stack; -then, the arguments to the function are pushed -in direct order; -that is, the first argument is pushed first. -Finally you call lua_call; -nargs is the number of arguments that you pushed onto the stack. -All arguments and the function value are popped from the stack -when the function is called. -The function results are pushed onto the stack when the function returns. -The number of results is adjusted to nresults, -unless nresults is LUA_MULTRET. -In this case, all results from the function are pushed. -Lua takes care that the returned values fit into the stack space. -The function results are pushed onto the stack in direct order -(the first result is pushed first), -so that after the call the last result is on the top of the stack. - - -

-Any error inside the called function is propagated upwards -(with a longjmp). - - -

-The following example shows how the host program can do the -equivalent to this Lua code: - -

-     a = f("how", t.x, 14)
-

-Here it is in C: - -

-     lua_getfield(L, LUA_GLOBALSINDEX, "f"); /* function to be called */
-     lua_pushstring(L, "how");                        /* 1st argument */
-     lua_getfield(L, LUA_GLOBALSINDEX, "t");   /* table to be indexed */
-     lua_getfield(L, -1, "x");        /* push result of t.x (2nd arg) */
-     lua_remove(L, -2);                  /* remove 't' from the stack */
-     lua_pushinteger(L, 14);                          /* 3rd argument */
-     lua_call(L, 3, 1);     /* call 'f' with 3 arguments and 1 result */
-     lua_setfield(L, LUA_GLOBALSINDEX, "a");        /* set global 'a' */
-

-Note that the code above is "balanced": -at its end, the stack is back to its original configuration. -This is considered good programming practice. - - - - - -


lua_CFunction

-
typedef int (*lua_CFunction) (lua_State *L);
- -

-Type for C functions. - - -

-In order to communicate properly with Lua, -a C function must use the following protocol, -which defines the way parameters and results are passed: -a C function receives its arguments from Lua in its stack -in direct order (the first argument is pushed first). -So, when the function starts, -lua_gettop(L) returns the number of arguments received by the function. -The first argument (if any) is at index 1 -and its last argument is at index lua_gettop(L). -To return values to Lua, a C function just pushes them onto the stack, -in direct order (the first result is pushed first), -and returns the number of results. -Any other value in the stack below the results will be properly -discarded by Lua. -Like a Lua function, a C function called by Lua can also return -many results. - - -

-As an example, the following function receives a variable number -of numerical arguments and returns their average and sum: - -

-     static int foo (lua_State *L) {
-       int n = lua_gettop(L);    /* number of arguments */
-       lua_Number sum = 0;
-       int i;
-       for (i = 1; i <= n; i++) {
-         if (!lua_isnumber(L, i)) {
-           lua_pushstring(L, "incorrect argument");
-           lua_error(L);
-         }
-         sum += lua_tonumber(L, i);
-       }
-       lua_pushnumber(L, sum/n);        /* first result */
-       lua_pushnumber(L, sum);         /* second result */
-       return 2;                   /* number of results */
-     }
-
- - - - -

lua_checkstack

-[-0, +0, m] -

int lua_checkstack (lua_State *L, int extra);
- -

-Ensures that there are at least extra free stack slots in the stack. -It returns false if it cannot grow the stack to that size. -This function never shrinks the stack; -if the stack is already larger than the new size, -it is left unchanged. - - - - - -


lua_close

-[-0, +0, -] -

void lua_close (lua_State *L);
- -

-Destroys all objects in the given Lua state -(calling the corresponding garbage-collection metamethods, if any) -and frees all dynamic memory used by this state. -On several platforms, you may not need to call this function, -because all resources are naturally released when the host program ends. -On the other hand, long-running programs, -such as a daemon or a web server, -might need to release states as soon as they are not needed, -to avoid growing too large. - - - - - -


lua_concat

-[-n, +1, e] -

void lua_concat (lua_State *L, int n);
- -

-Concatenates the n values at the top of the stack, -pops them, and leaves the result at the top. -If n is 1, the result is the single value on the stack -(that is, the function does nothing); -if n is 0, the result is the empty string. -Concatenation is performed following the usual semantics of Lua -(see §2.5.4). - - - - - -


lua_cpcall

-[-0, +(0|1), -] -

int lua_cpcall (lua_State *L, lua_CFunction func, void *ud);
- -

-Calls the C function func in protected mode. -func starts with only one element in its stack, -a light userdata containing ud. -In case of errors, -lua_cpcall returns the same error codes as lua_pcall, -plus the error object on the top of the stack; -otherwise, it returns zero, and does not change the stack. -All values returned by func are discarded. - - - - - -


lua_createtable

-[-0, +1, m] -

void lua_createtable (lua_State *L, int narr, int nrec);
- -

-Creates a new empty table and pushes it onto the stack. -The new table has space pre-allocated -for narr array elements and nrec non-array elements. -This pre-allocation is useful when you know exactly how many elements -the table will have. -Otherwise you can use the function lua_newtable. - - - - - -


lua_dump

-[-0, +0, m] -

int lua_dump (lua_State *L, lua_Writer writer, void *data);
- -

-Dumps a function as a binary chunk. -Receives a Lua function on the top of the stack -and produces a binary chunk that, -if loaded again, -results in a function equivalent to the one dumped. -As it produces parts of the chunk, -lua_dump calls function writer (see lua_Writer) -with the given data -to write them. - - -

-The value returned is the error code returned by the last -call to the writer; -0 means no errors. - - -

-This function does not pop the Lua function from the stack. - - - - - -


lua_equal

-[-0, +0, e] -

int lua_equal (lua_State *L, int index1, int index2);
- -

-Returns 1 if the two values in acceptable indices index1 and -index2 are equal, -following the semantics of the Lua == operator -(that is, may call metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices is non valid. - - - - - -


lua_error

-[-1, +0, v] -

int lua_error (lua_State *L);
- -

-Generates a Lua error. -The error message (which can actually be a Lua value of any type) -must be on the stack top. -This function does a long jump, -and therefore never returns. -(see luaL_error). - - - - - -


lua_gc

-[-0, +0, e] -

int lua_gc (lua_State *L, int what, int data);
- -

-Controls the garbage collector. - - -

-This function performs several tasks, -according to the value of the parameter what: - -

    - -
  • LUA_GCSTOP: -stops the garbage collector. -
  • - -
  • LUA_GCRESTART: -restarts the garbage collector. -
  • - -
  • LUA_GCCOLLECT: -performs a full garbage-collection cycle. -
  • - -
  • LUA_GCCOUNT: -returns the current amount of memory (in Kbytes) in use by Lua. -
  • - -
  • LUA_GCCOUNTB: -returns the remainder of dividing the current amount of bytes of -memory in use by Lua by 1024. -
  • - -
  • LUA_GCSTEP: -performs an incremental step of garbage collection. -The step "size" is controlled by data -(larger values mean more steps) in a non-specified way. -If you want to control the step size -you must experimentally tune the value of data. -The function returns 1 if the step finished a -garbage-collection cycle. -
  • - -
  • LUA_GCSETPAUSE: -sets data as the new value -for the pause of the collector (see §2.10). -The function returns the previous value of the pause. -
  • - -
  • LUA_GCSETSTEPMUL: -sets data as the new value for the step multiplier of -the collector (see §2.10). -The function returns the previous value of the step multiplier. -
  • - -
- - - - -

lua_getallocf

-[-0, +0, -] -

lua_Alloc lua_getallocf (lua_State *L, void **ud);
- -

-Returns the memory-allocation function of a given state. -If ud is not NULL, Lua stores in *ud the -opaque pointer passed to lua_newstate. - - - - - -


lua_getfenv

-[-0, +1, -] -

void lua_getfenv (lua_State *L, int index);
- -

-Pushes onto the stack the environment table of -the value at the given index. - - - - - -


lua_getfield

-[-0, +1, e] -

void lua_getfield (lua_State *L, int index, const char *k);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given valid index. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.8). - - - - - -


lua_getglobal

-[-0, +1, e] -

void lua_getglobal (lua_State *L, const char *name);
- -

-Pushes onto the stack the value of the global name. -It is defined as a macro: - -

-     #define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, s)
-
- - - - -

lua_getmetatable

-[-0, +(0|1), -] -

int lua_getmetatable (lua_State *L, int index);
- -

-Pushes onto the stack the metatable of the value at the given -acceptable index. -If the index is not valid, -or if the value does not have a metatable, -the function returns 0 and pushes nothing on the stack. - - - - - -


lua_gettable

-[-1, +1, e] -

void lua_gettable (lua_State *L, int index);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given valid index -and k is the value at the top of the stack. - - -

-This function pops the key from the stack -(putting the resulting value in its place). -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.8). - - - - - -


lua_gettop

-[-0, +0, -] -

int lua_gettop (lua_State *L);
- -

-Returns the index of the top element in the stack. -Because indices start at 1, -this result is equal to the number of elements in the stack -(and so 0 means an empty stack). - - - - - -


lua_insert

-[-1, +1, -] -

void lua_insert (lua_State *L, int index);
- -

-Moves the top element into the given valid index, -shifting up the elements above this index to open space. -Cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_Integer

-
typedef ptrdiff_t lua_Integer;
- -

-The type used by the Lua API to represent integral values. - - -

-By default it is a ptrdiff_t, -which is usually the largest signed integral type the machine handles -"comfortably". - - - - - -


lua_isboolean

-[-0, +0, -] -

int lua_isboolean (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index has type boolean, -and 0 otherwise. - - - - - -


lua_iscfunction

-[-0, +0, -] -

int lua_iscfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a C function, -and 0 otherwise. - - - - - -


lua_isfunction

-[-0, +0, -] -

int lua_isfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a function -(either C or Lua), and 0 otherwise. - - - - - -


lua_islightuserdata

-[-0, +0, -] -

int lua_islightuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a light userdata, -and 0 otherwise. - - - - - -


lua_isnil

-[-0, +0, -] -

int lua_isnil (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is nil, -and 0 otherwise. - - - - - -


lua_isnone

-[-0, +0, -] -

int lua_isnone (lua_State *L, int index);
- -

-Returns 1 if the given acceptable index is not valid -(that is, it refers to an element outside the current stack), -and 0 otherwise. - - - - - -


lua_isnoneornil

-[-0, +0, -] -

int lua_isnoneornil (lua_State *L, int index);
- -

-Returns 1 if the given acceptable index is not valid -(that is, it refers to an element outside the current stack) -or if the value at this index is nil, -and 0 otherwise. - - - - - -


lua_isnumber

-[-0, +0, -] -

int lua_isnumber (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a number -or a string convertible to a number, -and 0 otherwise. - - - - - -


lua_isstring

-[-0, +0, -] -

int lua_isstring (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a string -or a number (which is always convertible to a string), -and 0 otherwise. - - - - - -


lua_istable

-[-0, +0, -] -

int lua_istable (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a table, -and 0 otherwise. - - - - - -


lua_isthread

-[-0, +0, -] -

int lua_isthread (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a thread, -and 0 otherwise. - - - - - -


lua_isuserdata

-[-0, +0, -] -

int lua_isuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given acceptable index is a userdata -(either full or light), and 0 otherwise. - - - - - -


lua_lessthan

-[-0, +0, e] -

int lua_lessthan (lua_State *L, int index1, int index2);
- -

-Returns 1 if the value at acceptable index index1 is smaller -than the value at acceptable index index2, -following the semantics of the Lua < operator -(that is, may call metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices is non valid. - - - - - -


lua_load

-[-0, +1, -] -

int lua_load (lua_State *L,
-              lua_Reader reader,
-              void *data,
-              const char *chunkname);
- -

-Loads a Lua chunk. -If there are no errors, -lua_load pushes the compiled chunk as a Lua -function on top of the stack. -Otherwise, it pushes an error message. -The return values of lua_load are: - -

    - -
  • 0: no errors;
  • - -
  • LUA_ERRSYNTAX: -syntax error during pre-compilation;
  • - -
  • LUA_ERRMEM: -memory allocation error.
  • - -
- -

-This function only loads a chunk; -it does not run it. - - -

-lua_load automatically detects whether the chunk is text or binary, -and loads it accordingly (see program luac). - - -

-The lua_load function uses a user-supplied reader function -to read the chunk (see lua_Reader). -The data argument is an opaque value passed to the reader function. - - -

-The chunkname argument gives a name to the chunk, -which is used for error messages and in debug information (see §3.8). - - - - - -


lua_newstate

-[-0, +0, -] -

lua_State *lua_newstate (lua_Alloc f, void *ud);
- -

-Creates a new, independent state. -Returns NULL if cannot create the state -(due to lack of memory). -The argument f is the allocator function; -Lua does all memory allocation for this state through this function. -The second argument, ud, is an opaque pointer that Lua -simply passes to the allocator in every call. - - - - - -


lua_newtable

-[-0, +1, m] -

void lua_newtable (lua_State *L);
- -

-Creates a new empty table and pushes it onto the stack. -It is equivalent to lua_createtable(L, 0, 0). - - - - - -


lua_newthread

-[-0, +1, m] -

lua_State *lua_newthread (lua_State *L);
- -

-Creates a new thread, pushes it on the stack, -and returns a pointer to a lua_State that represents this new thread. -The new state returned by this function shares with the original state -all global objects (such as tables), -but has an independent execution stack. - - -

-There is no explicit function to close or to destroy a thread. -Threads are subject to garbage collection, -like any Lua object. - - - - - -


lua_newuserdata

-[-0, +1, m] -

void *lua_newuserdata (lua_State *L, size_t size);
- -

-This function allocates a new block of memory with the given size, -pushes onto the stack a new full userdata with the block address, -and returns this address. - - -

-Userdata represent C values in Lua. -A full userdata represents a block of memory. -It is an object (like a table): -you must create it, it can have its own metatable, -and you can detect when it is being collected. -A full userdata is only equal to itself (under raw equality). - - -

-When Lua collects a full userdata with a gc metamethod, -Lua calls the metamethod and marks the userdata as finalized. -When this userdata is collected again then -Lua frees its corresponding memory. - - - - - -


lua_next

-[-1, +(2|0), e] -

int lua_next (lua_State *L, int index);
- -

-Pops a key from the stack, -and pushes a key-value pair from the table at the given index -(the "next" pair after the given key). -If there are no more elements in the table, -then lua_next returns 0 (and pushes nothing). - - -

-A typical traversal looks like this: - -

-     /* table is in the stack at index 't' */
-     lua_pushnil(L);  /* first key */
-     while (lua_next(L, t) != 0) {
-       /* uses 'key' (at index -2) and 'value' (at index -1) */
-       printf("%s - %s\n",
-              lua_typename(L, lua_type(L, -2)),
-              lua_typename(L, lua_type(L, -1)));
-       /* removes 'value'; keeps 'key' for next iteration */
-       lua_pop(L, 1);
-     }
-
- -

-While traversing a table, -do not call lua_tolstring directly on a key, -unless you know that the key is actually a string. -Recall that lua_tolstring changes -the value at the given index; -this confuses the next call to lua_next. - - - - - -


lua_Number

-
typedef double lua_Number;
- -

-The type of numbers in Lua. -By default, it is double, but that can be changed in luaconf.h. - - -

-Through the configuration file you can change -Lua to operate with another type for numbers (e.g., float or long). - - - - - -


lua_objlen

-[-0, +0, -] -

size_t lua_objlen (lua_State *L, int index);
- -

-Returns the "length" of the value at the given acceptable index: -for strings, this is the string length; -for tables, this is the result of the length operator ('#'); -for userdata, this is the size of the block of memory allocated -for the userdata; -for other values, it is 0. - - - - - -


lua_pcall

-[-(nargs + 1), +(nresults|1), -] -

int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
- -

-Calls a function in protected mode. - - -

-Both nargs and nresults have the same meaning as -in lua_call. -If there are no errors during the call, -lua_pcall behaves exactly like lua_call. -However, if there is any error, -lua_pcall catches it, -pushes a single value on the stack (the error message), -and returns an error code. -Like lua_call, -lua_pcall always removes the function -and its arguments from the stack. - - -

-If errfunc is 0, -then the error message returned on the stack -is exactly the original error message. -Otherwise, errfunc is the stack index of an -error handler function. -(In the current implementation, this index cannot be a pseudo-index.) -In case of runtime errors, -this function will be called with the error message -and its return value will be the message returned on the stack by lua_pcall. - - -

-Typically, the error handler function is used to add more debug -information to the error message, such as a stack traceback. -Such information cannot be gathered after the return of lua_pcall, -since by then the stack has unwound. - - -

-The lua_pcall function returns 0 in case of success -or one of the following error codes -(defined in lua.h): - -

    - -
  • LUA_ERRRUN: -a runtime error. -
  • - -
  • LUA_ERRMEM: -memory allocation error. -For such errors, Lua does not call the error handler function. -
  • - -
  • LUA_ERRERR: -error while running the error handler function. -
  • - -
- - - - -

lua_pop

-[-n, +0, -] -

void lua_pop (lua_State *L, int n);
- -

-Pops n elements from the stack. - - - - - -


lua_pushboolean

-[-0, +1, -] -

void lua_pushboolean (lua_State *L, int b);
- -

-Pushes a boolean value with value b onto the stack. - - - - - -


lua_pushcclosure

-[-n, +1, m] -

void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
- -

-Pushes a new C closure onto the stack. - - -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure (see §3.4); -these values are then accessible to the function whenever it is called. -To associate values with a C function, -first these values should be pushed onto the stack -(when there are multiple values, the first value is pushed first). -Then lua_pushcclosure -is called to create and push the C function onto the stack, -with the argument n telling how many values should be -associated with the function. -lua_pushcclosure also pops these values from the stack. - - -

-The maximum value for n is 255. - - - - - -


lua_pushcfunction

-[-0, +1, m] -

void lua_pushcfunction (lua_State *L, lua_CFunction f);
- -

-Pushes a C function onto the stack. -This function receives a pointer to a C function -and pushes onto the stack a Lua value of type function that, -when called, invokes the corresponding C function. - - -

-Any function to be registered in Lua must -follow the correct protocol to receive its parameters -and return its results (see lua_CFunction). - - -

-lua_pushcfunction is defined as a macro: - -

-     #define lua_pushcfunction(L,f)  lua_pushcclosure(L,f,0)
-
- - - - -

lua_pushfstring

-[-0, +1, m] -

const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
- -

-Pushes onto the stack a formatted string -and returns a pointer to this string. -It is similar to the C function sprintf, -but has some important differences: - -

    - -
  • -You do not have to allocate space for the result: -the result is a Lua string and Lua takes care of memory allocation -(and deallocation, through garbage collection). -
  • - -
  • -The conversion specifiers are quite restricted. -There are no flags, widths, or precisions. -The conversion specifiers can only be -'%%' (inserts a '%' in the string), -'%s' (inserts a zero-terminated string, with no size restrictions), -'%f' (inserts a lua_Number), -'%p' (inserts a pointer as a hexadecimal numeral), -'%d' (inserts an int), and -'%c' (inserts an int as a character). -
  • - -
- - - - -

lua_pushinteger

-[-0, +1, -] -

void lua_pushinteger (lua_State *L, lua_Integer n);
- -

-Pushes a number with value n onto the stack. - - - - - -


lua_pushlightuserdata

-[-0, +1, -] -

void lua_pushlightuserdata (lua_State *L, void *p);
- -

-Pushes a light userdata onto the stack. - - -

-Userdata represent C values in Lua. -A light userdata represents a pointer. -It is a value (like a number): -you do not create it, it has no individual metatable, -and it is not collected (as it was never created). -A light userdata is equal to "any" -light userdata with the same C address. - - - - - -


lua_pushliteral

-[-0, +1, m] -

void lua_pushliteral (lua_State *L, const char *s);
- -

-This macro is equivalent to lua_pushlstring, -but can be used only when s is a literal string. -In these cases, it automatically provides the string length. - - - - - -


lua_pushlstring

-[-0, +1, m] -

void lua_pushlstring (lua_State *L, const char *s, size_t len);
- -

-Pushes the string pointed to by s with size len -onto the stack. -Lua makes (or reuses) an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. -The string can contain embedded zeros. - - - - - -


lua_pushnil

-[-0, +1, -] -

void lua_pushnil (lua_State *L);
- -

-Pushes a nil value onto the stack. - - - - - -


lua_pushnumber

-[-0, +1, -] -

void lua_pushnumber (lua_State *L, lua_Number n);
- -

-Pushes a number with value n onto the stack. - - - - - -


lua_pushstring

-[-0, +1, m] -

void lua_pushstring (lua_State *L, const char *s);
- -

-Pushes the zero-terminated string pointed to by s -onto the stack. -Lua makes (or reuses) an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. -The string cannot contain embedded zeros; -it is assumed to end at the first zero. - - - - - -


lua_pushthread

-[-0, +1, -] -

int lua_pushthread (lua_State *L);
- -

-Pushes the thread represented by L onto the stack. -Returns 1 if this thread is the main thread of its state. - - - - - -


lua_pushvalue

-[-0, +1, -] -

void lua_pushvalue (lua_State *L, int index);
- -

-Pushes a copy of the element at the given valid index -onto the stack. - - - - - -


lua_pushvfstring

-[-0, +1, m] -

const char *lua_pushvfstring (lua_State *L,
-                              const char *fmt,
-                              va_list argp);
- -

-Equivalent to lua_pushfstring, except that it receives a va_list -instead of a variable number of arguments. - - - - - -


lua_rawequal

-[-0, +0, -] -

int lua_rawequal (lua_State *L, int index1, int index2);
- -

-Returns 1 if the two values in acceptable indices index1 and -index2 are primitively equal -(that is, without calling metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices are non valid. - - - - - -


lua_rawget

-[-1, +1, -] -

void lua_rawget (lua_State *L, int index);
- -

-Similar to lua_gettable, but does a raw access -(i.e., without metamethods). - - - - - -


lua_rawgeti

-[-0, +1, -] -

void lua_rawgeti (lua_State *L, int index, int n);
- -

-Pushes onto the stack the value t[n], -where t is the value at the given valid index. -The access is raw; -that is, it does not invoke metamethods. - - - - - -


lua_rawset

-[-2, +0, m] -

void lua_rawset (lua_State *L, int index);
- -

-Similar to lua_settable, but does a raw assignment -(i.e., without metamethods). - - - - - -


lua_rawseti

-[-1, +0, m] -

void lua_rawseti (lua_State *L, int index, int n);
- -

-Does the equivalent of t[n] = v, -where t is the value at the given valid index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw; -that is, it does not invoke metamethods. - - - - - -


lua_Reader

-
typedef const char * (*lua_Reader) (lua_State *L,
-                                    void *data,
-                                    size_t *size);
- -

-The reader function used by lua_load. -Every time it needs another piece of the chunk, -lua_load calls the reader, -passing along its data parameter. -The reader must return a pointer to a block of memory -with a new piece of the chunk -and set size to the block size. -The block must exist until the reader function is called again. -To signal the end of the chunk, -the reader must return NULL or set size to zero. -The reader function may return pieces of any size greater than zero. - - - - - -


lua_register

-[-0, +0, e] -

void lua_register (lua_State *L,
-                   const char *name,
-                   lua_CFunction f);
- -

-Sets the C function f as the new value of global name. -It is defined as a macro: - -

-     #define lua_register(L,n,f) \
-            (lua_pushcfunction(L, f), lua_setglobal(L, n))
-
- - - - -

lua_remove

-[-1, +0, -] -

void lua_remove (lua_State *L, int index);
- -

-Removes the element at the given valid index, -shifting down the elements above this index to fill the gap. -Cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_replace

-[-1, +0, -] -

void lua_replace (lua_State *L, int index);
- -

-Moves the top element into the given position (and pops it), -without shifting any element -(therefore replacing the value at the given position). - - - - - -


lua_resume

-[-?, +?, -] -

int lua_resume (lua_State *L, int narg);
- -

-Starts and resumes a coroutine in a given thread. - - -

-To start a coroutine, you first create a new thread -(see lua_newthread); -then you push onto its stack the main function plus any arguments; -then you call lua_resume, -with narg being the number of arguments. -This call returns when the coroutine suspends or finishes its execution. -When it returns, the stack contains all values passed to lua_yield, -or all values returned by the body function. -lua_resume returns -LUA_YIELD if the coroutine yields, -0 if the coroutine finishes its execution -without errors, -or an error code in case of errors (see lua_pcall). -In case of errors, -the stack is not unwound, -so you can use the debug API over it. -The error message is on the top of the stack. -To restart a coroutine, you put on its stack only the values to -be passed as results from yield, -and then call lua_resume. - - - - - -


lua_setallocf

-[-0, +0, -] -

void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
- -

-Changes the allocator function of a given state to f -with user data ud. - - - - - -


lua_setfenv

-[-1, +0, -] -

int lua_setfenv (lua_State *L, int index);
- -

-Pops a table from the stack and sets it as -the new environment for the value at the given index. -If the value at the given index is -neither a function nor a thread nor a userdata, -lua_setfenv returns 0. -Otherwise it returns 1. - - - - - -


lua_setfield

-[-1, +0, e] -

void lua_setfield (lua_State *L, int index, const char *k);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given valid index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.8). - - - - - -


lua_setglobal

-[-1, +0, e] -

void lua_setglobal (lua_State *L, const char *name);
- -

-Pops a value from the stack and -sets it as the new value of global name. -It is defined as a macro: - -

-     #define lua_setglobal(L,s)   lua_setfield(L, LUA_GLOBALSINDEX, s)
-
- - - - -

lua_setmetatable

-[-1, +0, -] -

int lua_setmetatable (lua_State *L, int index);
- -

-Pops a table from the stack and -sets it as the new metatable for the value at the given -acceptable index. - - - - - -


lua_settable

-[-2, +0, e] -

void lua_settable (lua_State *L, int index);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given valid index, -v is the value at the top of the stack, -and k is the value just below the top. - - -

-This function pops both the key and the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.8). - - - - - -


lua_settop

-[-?, +?, -] -

void lua_settop (lua_State *L, int index);
- -

-Accepts any acceptable index, or 0, -and sets the stack top to this index. -If the new top is larger than the old one, -then the new elements are filled with nil. -If index is 0, then all stack elements are removed. - - - - - -


lua_State

-
typedef struct lua_State lua_State;
- -

-Opaque structure that keeps the whole state of a Lua interpreter. -The Lua library is fully reentrant: -it has no global variables. -All information about a state is kept in this structure. - - -

-A pointer to this state must be passed as the first argument to -every function in the library, except to lua_newstate, -which creates a Lua state from scratch. - - - - - -


lua_status

-[-0, +0, -] -

int lua_status (lua_State *L);
- -

-Returns the status of the thread L. - - -

-The status can be 0 for a normal thread, -an error code if the thread finished its execution with an error, -or LUA_YIELD if the thread is suspended. - - - - - -


lua_toboolean

-[-0, +0, -] -

int lua_toboolean (lua_State *L, int index);
- -

-Converts the Lua value at the given acceptable index to a C boolean -value (0 or 1). -Like all tests in Lua, -lua_toboolean returns 1 for any Lua value -different from false and nil; -otherwise it returns 0. -It also returns 0 when called with a non-valid index. -(If you want to accept only actual boolean values, -use lua_isboolean to test the value's type.) - - - - - -


lua_tocfunction

-[-0, +0, -] -

lua_CFunction lua_tocfunction (lua_State *L, int index);
- -

-Converts a value at the given acceptable index to a C function. -That value must be a C function; -otherwise, returns NULL. - - - - - -


lua_tointeger

-[-0, +0, -] -

lua_Integer lua_tointeger (lua_State *L, int index);
- -

-Converts the Lua value at the given acceptable index -to the signed integral type lua_Integer. -The Lua value must be a number or a string convertible to a number -(see §2.2.1); -otherwise, lua_tointeger returns 0. - - -

-If the number is not an integer, -it is truncated in some non-specified way. - - - - - -


lua_tolstring

-[-0, +0, m] -

const char *lua_tolstring (lua_State *L, int index, size_t *len);
- -

-Converts the Lua value at the given acceptable index to a C string. -If len is not NULL, -it also sets *len with the string length. -The Lua value must be a string or a number; -otherwise, the function returns NULL. -If the value is a number, -then lua_tolstring also -changes the actual value in the stack to a string. -(This change confuses lua_next -when lua_tolstring is applied to keys during a table traversal.) - - -

-lua_tolstring returns a fully aligned pointer -to a string inside the Lua state. -This string always has a zero ('\0') -after its last character (as in C), -but can contain other zeros in its body. -Because Lua has garbage collection, -there is no guarantee that the pointer returned by lua_tolstring -will be valid after the corresponding value is removed from the stack. - - - - - -


lua_tonumber

-[-0, +0, -] -

lua_Number lua_tonumber (lua_State *L, int index);
- -

-Converts the Lua value at the given acceptable index -to the C type lua_Number (see lua_Number). -The Lua value must be a number or a string convertible to a number -(see §2.2.1); -otherwise, lua_tonumber returns 0. - - - - - -


lua_topointer

-[-0, +0, -] -

const void *lua_topointer (lua_State *L, int index);
- -

-Converts the value at the given acceptable index to a generic -C pointer (void*). -The value can be a userdata, a table, a thread, or a function; -otherwise, lua_topointer returns NULL. -Different objects will give different pointers. -There is no way to convert the pointer back to its original value. - - -

-Typically this function is used only for debug information. - - - - - -


lua_tostring

-[-0, +0, m] -

const char *lua_tostring (lua_State *L, int index);
- -

-Equivalent to lua_tolstring with len equal to NULL. - - - - - -


lua_tothread

-[-0, +0, -] -

lua_State *lua_tothread (lua_State *L, int index);
- -

-Converts the value at the given acceptable index to a Lua thread -(represented as lua_State*). -This value must be a thread; -otherwise, the function returns NULL. - - - - - -


lua_touserdata

-[-0, +0, -] -

void *lua_touserdata (lua_State *L, int index);
- -

-If the value at the given acceptable index is a full userdata, -returns its block address. -If the value is a light userdata, -returns its pointer. -Otherwise, returns NULL. - - - - - -


lua_type

-[-0, +0, -] -

int lua_type (lua_State *L, int index);
- -

-Returns the type of the value in the given acceptable index, -or LUA_TNONE for a non-valid index -(that is, an index to an "empty" stack position). -The types returned by lua_type are coded by the following constants -defined in lua.h: -LUA_TNIL, -LUA_TNUMBER, -LUA_TBOOLEAN, -LUA_TSTRING, -LUA_TTABLE, -LUA_TFUNCTION, -LUA_TUSERDATA, -LUA_TTHREAD, -and -LUA_TLIGHTUSERDATA. - - - - - -


lua_typename

-[-0, +0, -] -

const char *lua_typename  (lua_State *L, int tp);
- -

-Returns the name of the type encoded by the value tp, -which must be one the values returned by lua_type. - - - - - -


lua_Writer

-
typedef int (*lua_Writer) (lua_State *L,
-                           const void* p,
-                           size_t sz,
-                           void* ud);
- -

-The type of the writer function used by lua_dump. -Every time it produces another piece of chunk, -lua_dump calls the writer, -passing along the buffer to be written (p), -its size (sz), -and the data parameter supplied to lua_dump. - - -

-The writer returns an error code: -0 means no errors; -any other value means an error and stops lua_dump from -calling the writer again. - - - - - -


lua_xmove

-[-?, +?, -] -

void lua_xmove (lua_State *from, lua_State *to, int n);
- -

-Exchange values between different threads of the same global state. - - -

-This function pops n values from the stack from, -and pushes them onto the stack to. - - - - - -


lua_yield

-[-?, +?, -] -

int lua_yield  (lua_State *L, int nresults);
- -

-Yields a coroutine. - - -

-This function should only be called as the -return expression of a C function, as follows: - -

-     return lua_yield (L, nresults);
-

-When a C function calls lua_yield in that way, -the running coroutine suspends its execution, -and the call to lua_resume that started this coroutine returns. -The parameter nresults is the number of values from the stack -that are passed as results to lua_resume. - - - - - - - -

3.8 - The Debug Interface

- -

-Lua has no built-in debugging facilities. -Instead, it offers a special interface -by means of functions and hooks. -This interface allows the construction of different -kinds of debuggers, profilers, and other tools -that need "inside information" from the interpreter. - - - -


lua_Debug

-
typedef struct lua_Debug {
-  int event;
-  const char *name;           /* (n) */
-  const char *namewhat;       /* (n) */
-  const char *what;           /* (S) */
-  const char *source;         /* (S) */
-  int currentline;            /* (l) */
-  int nups;                   /* (u) number of upvalues */
-  int linedefined;            /* (S) */
-  int lastlinedefined;        /* (S) */
-  char short_src[LUA_IDSIZE]; /* (S) */
-  /* private part */
-  other fields
-} lua_Debug;
- -

-A structure used to carry different pieces of -information about an active function. -lua_getstack fills only the private part -of this structure, for later use. -To fill the other fields of lua_Debug with useful information, -call lua_getinfo. - - -

-The fields of lua_Debug have the following meaning: - -

    - -
  • source: -If the function was defined in a string, -then source is that string. -If the function was defined in a file, -then source starts with a '@' followed by the file name. -
  • - -
  • short_src: -a "printable" version of source, to be used in error messages. -
  • - -
  • linedefined: -the line number where the definition of the function starts. -
  • - -
  • lastlinedefined: -the line number where the definition of the function ends. -
  • - -
  • what: -the string "Lua" if the function is a Lua function, -"C" if it is a C function, -"main" if it is the main part of a chunk, -and "tail" if it was a function that did a tail call. -In the latter case, -Lua has no other information about the function. -
  • - -
  • currentline: -the current line where the given function is executing. -When no line information is available, -currentline is set to -1. -
  • - -
  • name: -a reasonable name for the given function. -Because functions in Lua are first-class values, -they do not have a fixed name: -some functions can be the value of multiple global variables, -while others can be stored only in a table field. -The lua_getinfo function checks how the function was -called to find a suitable name. -If it cannot find a name, -then name is set to NULL. -
  • - -
  • namewhat: -explains the name field. -The value of namewhat can be -"global", "local", "method", -"field", "upvalue", or "" (the empty string), -according to how the function was called. -(Lua uses the empty string when no other option seems to apply.) -
  • - -
  • nups: -the number of upvalues of the function. -
  • - -
- - - - -

lua_gethook

-[-0, +0, -] -

lua_Hook lua_gethook (lua_State *L);
- -

-Returns the current hook function. - - - - - -


lua_gethookcount

-[-0, +0, -] -

int lua_gethookcount (lua_State *L);
- -

-Returns the current hook count. - - - - - -


lua_gethookmask

-[-0, +0, -] -

int lua_gethookmask (lua_State *L);
- -

-Returns the current hook mask. - - - - - -


lua_getinfo

-[-(0|1), +(0|1|2), m] -

int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
- -

-Returns information about a specific function or function invocation. - - -

-To get information about a function invocation, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). - - -

-To get information about a function you push it onto the stack -and start the what string with the character '>'. -(In that case, -lua_getinfo pops the function in the top of the stack.) -For instance, to know in which line a function f was defined, -you can write the following code: - -

-     lua_Debug ar;
-     lua_getfield(L, LUA_GLOBALSINDEX, "f");  /* get global 'f' */
-     lua_getinfo(L, ">S", &ar);
-     printf("%d\n", ar.linedefined);
-
- -

-Each character in the string what -selects some fields of the structure ar to be filled or -a value to be pushed on the stack: - -

    - -
  • 'n': fills in the field name and namewhat; -
  • - -
  • 'S': -fills in the fields source, short_src, -linedefined, lastlinedefined, and what; -
  • - -
  • 'l': fills in the field currentline; -
  • - -
  • 'u': fills in the field nups; -
  • - -
  • 'f': -pushes onto the stack the function that is -running at the given level; -
  • - -
  • 'L': -pushes onto the stack a table whose indices are the -numbers of the lines that are valid on the function. -(A valid line is a line with some associated code, -that is, a line where you can put a break point. -Non-valid lines include empty lines and comments.) -
  • - -
- -

-This function returns 0 on error -(for instance, an invalid option in what). - - - - - -


lua_getlocal

-[-0, +(0|1), -] -

const char *lua_getlocal (lua_State *L, lua_Debug *ar, int n);
- -

-Gets information about a local variable of a given activation record. -The parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). -The index n selects which local variable to inspect -(1 is the first parameter or active local variable, and so on, -until the last active local variable). -lua_getlocal pushes the variable's value onto the stack -and returns its name. - - -

-Variable names starting with '(' (open parentheses) -represent internal variables -(loop control variables, temporaries, and C function locals). - - -

-Returns NULL (and pushes nothing) -when the index is greater than -the number of active local variables. - - - - - -


lua_getstack

-[-0, +0, -] -

int lua_getstack (lua_State *L, int level, lua_Debug *ar);
- -

-Get information about the interpreter runtime stack. - - -

-This function fills parts of a lua_Debug structure with -an identification of the activation record -of the function executing at a given level. -Level 0 is the current running function, -whereas level n+1 is the function that has called level n. -When there are no errors, lua_getstack returns 1; -when called with a level greater than the stack depth, -it returns 0. - - - - - -


lua_getupvalue

-[-0, +(0|1), -] -

const char *lua_getupvalue (lua_State *L, int funcindex, int n);
- -

-Gets information about a closure's upvalue. -(For Lua functions, -upvalues are the external local variables that the function uses, -and that are consequently included in its closure.) -lua_getupvalue gets the index n of an upvalue, -pushes the upvalue's value onto the stack, -and returns its name. -funcindex points to the closure in the stack. -(Upvalues have no particular order, -as they are active through the whole function. -So, they are numbered in an arbitrary order.) - - -

-Returns NULL (and pushes nothing) -when the index is greater than the number of upvalues. -For C functions, this function uses the empty string "" -as a name for all upvalues. - - - - - -


lua_Hook

-
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
- -

-Type for debugging hook functions. - - -

-Whenever a hook is called, its ar argument has its field -event set to the specific event that triggered the hook. -Lua identifies these events with the following constants: -LUA_HOOKCALL, LUA_HOOKRET, -LUA_HOOKTAILRET, LUA_HOOKLINE, -and LUA_HOOKCOUNT. -Moreover, for line events, the field currentline is also set. -To get the value of any other field in ar, -the hook must call lua_getinfo. -For return events, event can be LUA_HOOKRET, -the normal value, or LUA_HOOKTAILRET. -In the latter case, Lua is simulating a return from -a function that did a tail call; -in this case, it is useless to call lua_getinfo. - - -

-While Lua is running a hook, it disables other calls to hooks. -Therefore, if a hook calls back Lua to execute a function or a chunk, -this execution occurs without any calls to hooks. - - - - - -


lua_sethook

-[-0, +0, -] -

int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
- -

-Sets the debugging hook function. - - -

-Argument f is the hook function. -mask specifies on which events the hook will be called: -it is formed by a bitwise or of the constants -LUA_MASKCALL, -LUA_MASKRET, -LUA_MASKLINE, -and LUA_MASKCOUNT. -The count argument is only meaningful when the mask -includes LUA_MASKCOUNT. -For each event, the hook is called as explained below: - -

    - -
  • The call hook: is called when the interpreter calls a function. -The hook is called just after Lua enters the new function, -before the function gets its arguments. -
  • - -
  • The return hook: is called when the interpreter returns from a function. -The hook is called just before Lua leaves the function. -You have no access to the values to be returned by the function. -
  • - -
  • The line hook: is called when the interpreter is about to -start the execution of a new line of code, -or when it jumps back in the code (even to the same line). -(This event only happens while Lua is executing a Lua function.) -
  • - -
  • The count hook: is called after the interpreter executes every -count instructions. -(This event only happens while Lua is executing a Lua function.) -
  • - -
- -

-A hook is disabled by setting mask to zero. - - - - - -


lua_setlocal

-[-(0|1), +0, -] -

const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);
- -

-Sets the value of a local variable of a given activation record. -Parameters ar and n are as in lua_getlocal -(see lua_getlocal). -lua_setlocal assigns the value at the top of the stack -to the variable and returns its name. -It also pops the value from the stack. - - -

-Returns NULL (and pops nothing) -when the index is greater than -the number of active local variables. - - - - - -


lua_setupvalue

-[-(0|1), +0, -] -

const char *lua_setupvalue (lua_State *L, int funcindex, int n);
- -

-Sets the value of a closure's upvalue. -It assigns the value at the top of the stack -to the upvalue and returns its name. -It also pops the value from the stack. -Parameters funcindex and n are as in the lua_getupvalue -(see lua_getupvalue). - - -

-Returns NULL (and pops nothing) -when the index is greater than the number of upvalues. - - - - - - - -

4 - The Auxiliary Library

- -

- -The auxiliary library provides several convenient functions -to interface C with Lua. -While the basic API provides the primitive functions for all -interactions between C and Lua, -the auxiliary library provides higher-level functions for some -common tasks. - - -

-All functions from the auxiliary library -are defined in header file lauxlib.h and -have a prefix luaL_. - - -

-All functions in the auxiliary library are built on -top of the basic API, -and so they provide nothing that cannot be done with this API. - - -

-Several functions in the auxiliary library are used to -check C function arguments. -Their names are always luaL_check* or luaL_opt*. -All of these functions throw an error if the check is not satisfied. -Because the error message is formatted for arguments -(e.g., "bad argument #1"), -you should not use these functions for other stack values. - - - -

4.1 - Functions and Types

- -

-Here we list all functions and types from the auxiliary library -in alphabetical order. - - - -


luaL_addchar

-[-0, +0, m] -

void luaL_addchar (luaL_Buffer *B, char c);
- -

-Adds the character c to the buffer B -(see luaL_Buffer). - - - - - -


luaL_addlstring

-[-0, +0, m] -

void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
- -

-Adds the string pointed to by s with length l to -the buffer B -(see luaL_Buffer). -The string may contain embedded zeros. - - - - - -


luaL_addsize

-[-0, +0, m] -

void luaL_addsize (luaL_Buffer *B, size_t n);
- -

-Adds to the buffer B (see luaL_Buffer) -a string of length n previously copied to the -buffer area (see luaL_prepbuffer). - - - - - -


luaL_addstring

-[-0, +0, m] -

void luaL_addstring (luaL_Buffer *B, const char *s);
- -

-Adds the zero-terminated string pointed to by s -to the buffer B -(see luaL_Buffer). -The string may not contain embedded zeros. - - - - - -


luaL_addvalue

-[-1, +0, m] -

void luaL_addvalue (luaL_Buffer *B);
- -

-Adds the value at the top of the stack -to the buffer B -(see luaL_Buffer). -Pops the value. - - -

-This is the only function on string buffers that can (and must) -be called with an extra element on the stack, -which is the value to be added to the buffer. - - - - - -


luaL_argcheck

-[-0, +0, v] -

void luaL_argcheck (lua_State *L,
-                    int cond,
-                    int narg,
-                    const char *extramsg);
- -

-Checks whether cond is true. -If not, raises an error with the following message, -where func is retrieved from the call stack: - -

-     bad argument #<narg> to <func> (<extramsg>)
-
- - - - -

luaL_argerror

-[-0, +0, v] -

int luaL_argerror (lua_State *L, int narg, const char *extramsg);
- -

-Raises an error with the following message, -where func is retrieved from the call stack: - -

-     bad argument #<narg> to <func> (<extramsg>)
-
- -

-This function never returns, -but it is an idiom to use it in C functions -as return luaL_argerror(args). - - - - - -


luaL_Buffer

-
typedef struct luaL_Buffer luaL_Buffer;
- -

-Type for a string buffer. - - -

-A string buffer allows C code to build Lua strings piecemeal. -Its pattern of use is as follows: - -

    - -
  • First you declare a variable b of type luaL_Buffer.
  • - -
  • Then you initialize it with a call luaL_buffinit(L, &b).
  • - -
  • -Then you add string pieces to the buffer calling any of -the luaL_add* functions. -
  • - -
  • -You finish by calling luaL_pushresult(&b). -This call leaves the final string on the top of the stack. -
  • - -
- -

-During its normal operation, -a string buffer uses a variable number of stack slots. -So, while using a buffer, you cannot assume that you know where -the top of the stack is. -You can use the stack between successive calls to buffer operations -as long as that use is balanced; -that is, -when you call a buffer operation, -the stack is at the same level -it was immediately after the previous buffer operation. -(The only exception to this rule is luaL_addvalue.) -After calling luaL_pushresult the stack is back to its -level when the buffer was initialized, -plus the final string on its top. - - - - - -


luaL_buffinit

-[-0, +0, -] -

void luaL_buffinit (lua_State *L, luaL_Buffer *B);
- -

-Initializes a buffer B. -This function does not allocate any space; -the buffer must be declared as a variable -(see luaL_Buffer). - - - - - -


luaL_callmeta

-[-0, +(0|1), e] -

int luaL_callmeta (lua_State *L, int obj, const char *e);
- -

-Calls a metamethod. - - -

-If the object at index obj has a metatable and this -metatable has a field e, -this function calls this field and passes the object as its only argument. -In this case this function returns 1 and pushes onto the -stack the value returned by the call. -If there is no metatable or no metamethod, -this function returns 0 (without pushing any value on the stack). - - - - - -


luaL_checkany

-[-0, +0, v] -

void luaL_checkany (lua_State *L, int narg);
- -

-Checks whether the function has an argument -of any type (including nil) at position narg. - - - - - -


luaL_checkint

-[-0, +0, v] -

int luaL_checkint (lua_State *L, int narg);
- -

-Checks whether the function argument narg is a number -and returns this number cast to an int. - - - - - -


luaL_checkinteger

-[-0, +0, v] -

lua_Integer luaL_checkinteger (lua_State *L, int narg);
- -

-Checks whether the function argument narg is a number -and returns this number cast to a lua_Integer. - - - - - -


luaL_checklong

-[-0, +0, v] -

long luaL_checklong (lua_State *L, int narg);
- -

-Checks whether the function argument narg is a number -and returns this number cast to a long. - - - - - -


luaL_checklstring

-[-0, +0, v] -

const char *luaL_checklstring (lua_State *L, int narg, size_t *l);
- -

-Checks whether the function argument narg is a string -and returns this string; -if l is not NULL fills *l -with the string's length. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checknumber

-[-0, +0, v] -

lua_Number luaL_checknumber (lua_State *L, int narg);
- -

-Checks whether the function argument narg is a number -and returns this number. - - - - - -


luaL_checkoption

-[-0, +0, v] -

int luaL_checkoption (lua_State *L,
-                      int narg,
-                      const char *def,
-                      const char *const lst[]);
- -

-Checks whether the function argument narg is a string and -searches for this string in the array lst -(which must be NULL-terminated). -Returns the index in the array where the string was found. -Raises an error if the argument is not a string or -if the string cannot be found. - - -

-If def is not NULL, -the function uses def as a default value when -there is no argument narg or if this argument is nil. - - -

-This is a useful function for mapping strings to C enums. -(The usual convention in Lua libraries is -to use strings instead of numbers to select options.) - - - - - -


luaL_checkstack

-[-0, +0, v] -

void luaL_checkstack (lua_State *L, int sz, const char *msg);
- -

-Grows the stack size to top + sz elements, -raising an error if the stack cannot grow to that size. -msg is an additional text to go into the error message. - - - - - -


luaL_checkstring

-[-0, +0, v] -

const char *luaL_checkstring (lua_State *L, int narg);
- -

-Checks whether the function argument narg is a string -and returns this string. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checktype

-[-0, +0, v] -

void luaL_checktype (lua_State *L, int narg, int t);
- -

-Checks whether the function argument narg has type t. -See lua_type for the encoding of types for t. - - - - - -


luaL_checkudata

-[-0, +0, v] -

void *luaL_checkudata (lua_State *L, int narg, const char *tname);
- -

-Checks whether the function argument narg is a userdata -of the type tname (see luaL_newmetatable). - - - - - -


luaL_dofile

-[-0, +?, m] -

int luaL_dofile (lua_State *L, const char *filename);
- -

-Loads and runs the given file. -It is defined as the following macro: - -

-     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns 0 if there are no errors -or 1 in case of errors. - - - - - -


luaL_dostring

-[-0, +?, m] -

int luaL_dostring (lua_State *L, const char *str);
- -

-Loads and runs the given string. -It is defined as the following macro: - -

-     (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns 0 if there are no errors -or 1 in case of errors. - - - - - -


luaL_error

-[-0, +0, v] -

int luaL_error (lua_State *L, const char *fmt, ...);
- -

-Raises an error. -The error message format is given by fmt -plus any extra arguments, -following the same rules of lua_pushfstring. -It also adds at the beginning of the message the file name and -the line number where the error occurred, -if this information is available. - - -

-This function never returns, -but it is an idiom to use it in C functions -as return luaL_error(args). - - - - - -


luaL_getmetafield

-[-0, +(0|1), m] -

int luaL_getmetafield (lua_State *L, int obj, const char *e);
- -

-Pushes onto the stack the field e from the metatable -of the object at index obj. -If the object does not have a metatable, -or if the metatable does not have this field, -returns 0 and pushes nothing. - - - - - -


luaL_getmetatable

-[-0, +1, -] -

void luaL_getmetatable (lua_State *L, const char *tname);
- -

-Pushes onto the stack the metatable associated with name tname -in the registry (see luaL_newmetatable). - - - - - -


luaL_gsub

-[-0, +1, m] -

const char *luaL_gsub (lua_State *L,
-                       const char *s,
-                       const char *p,
-                       const char *r);
- -

-Creates a copy of string s by replacing -any occurrence of the string p -with the string r. -Pushes the resulting string on the stack and returns it. - - - - - -


luaL_loadbuffer

-[-0, +1, m] -

int luaL_loadbuffer (lua_State *L,
-                     const char *buff,
-                     size_t sz,
-                     const char *name);
- -

-Loads a buffer as a Lua chunk. -This function uses lua_load to load the chunk in the -buffer pointed to by buff with size sz. - - -

-This function returns the same results as lua_load. -name is the chunk name, -used for debug information and error messages. - - - - - -


luaL_loadfile

-[-0, +1, m] -

int luaL_loadfile (lua_State *L, const char *filename);
- -

-Loads a file as a Lua chunk. -This function uses lua_load to load the chunk in the file -named filename. -If filename is NULL, -then it loads from the standard input. -The first line in the file is ignored if it starts with a #. - - -

-This function returns the same results as lua_load, -but it has an extra error code LUA_ERRFILE -if it cannot open/read the file. - - -

-As lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_loadstring

-[-0, +1, m] -

int luaL_loadstring (lua_State *L, const char *s);
- -

-Loads a string as a Lua chunk. -This function uses lua_load to load the chunk in -the zero-terminated string s. - - -

-This function returns the same results as lua_load. - - -

-Also as lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_newmetatable

-[-0, +1, m] -

int luaL_newmetatable (lua_State *L, const char *tname);
- -

-If the registry already has the key tname, -returns 0. -Otherwise, -creates a new table to be used as a metatable for userdata, -adds it to the registry with key tname, -and returns 1. - - -

-In both cases pushes onto the stack the final value associated -with tname in the registry. - - - - - -


luaL_newstate

-[-0, +0, -] -

lua_State *luaL_newstate (void);
- -

-Creates a new Lua state. -It calls lua_newstate with an -allocator based on the standard C realloc function -and then sets a panic function (see lua_atpanic) that prints -an error message to the standard error output in case of fatal -errors. - - -

-Returns the new state, -or NULL if there is a memory allocation error. - - - - - -


luaL_openlibs

-[-0, +0, m] -

void luaL_openlibs (lua_State *L);
- -

-Opens all standard Lua libraries into the given state. - - - - - -


luaL_optint

-[-0, +0, v] -

int luaL_optint (lua_State *L, int narg, int d);
- -

-If the function argument narg is a number, -returns this number cast to an int. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optinteger

-[-0, +0, v] -

lua_Integer luaL_optinteger (lua_State *L,
-                             int narg,
-                             lua_Integer d);
- -

-If the function argument narg is a number, -returns this number cast to a lua_Integer. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optlong

-[-0, +0, v] -

long luaL_optlong (lua_State *L, int narg, long d);
- -

-If the function argument narg is a number, -returns this number cast to a long. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optlstring

-[-0, +0, v] -

const char *luaL_optlstring (lua_State *L,
-                             int narg,
-                             const char *d,
-                             size_t *l);
- -

-If the function argument narg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - -

-If l is not NULL, -fills the position *l with the results's length. - - - - - -


luaL_optnumber

-[-0, +0, v] -

lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number d);
- -

-If the function argument narg is a number, -returns this number. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optstring

-[-0, +0, v] -

const char *luaL_optstring (lua_State *L,
-                            int narg,
-                            const char *d);
- -

-If the function argument narg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_prepbuffer

-[-0, +0, -] -

char *luaL_prepbuffer (luaL_Buffer *B);
- -

-Returns an address to a space of size LUAL_BUFFERSIZE -where you can copy a string to be added to buffer B -(see luaL_Buffer). -After copying the string into this space you must call -luaL_addsize with the size of the string to actually add -it to the buffer. - - - - - -


luaL_pushresult

-[-?, +1, m] -

void luaL_pushresult (luaL_Buffer *B);
- -

-Finishes the use of buffer B leaving the final string on -the top of the stack. - - - - - -


luaL_ref

-[-1, +0, m] -

int luaL_ref (lua_State *L, int t);
- -

-Creates and returns a reference, -in the table at index t, -for the object at the top of the stack (and pops the object). - - -

-A reference is a unique integer key. -As long as you do not manually add integer keys into table t, -luaL_ref ensures the uniqueness of the key it returns. -You can retrieve an object referred by reference r -by calling lua_rawgeti(L, t, r). -Function luaL_unref frees a reference and its associated object. - - -

-If the object at the top of the stack is nil, -luaL_ref returns the constant LUA_REFNIL. -The constant LUA_NOREF is guaranteed to be different -from any reference returned by luaL_ref. - - - - - -


luaL_Reg

-
typedef struct luaL_Reg {
-  const char *name;
-  lua_CFunction func;
-} luaL_Reg;
- -

-Type for arrays of functions to be registered by -luaL_register. -name is the function name and func is a pointer to -the function. -Any array of luaL_Reg must end with an sentinel entry -in which both name and func are NULL. - - - - - -


luaL_register

-[-(0|1), +1, m] -

void luaL_register (lua_State *L,
-                    const char *libname,
-                    const luaL_Reg *l);
- -

-Opens a library. - - -

-When called with libname equal to NULL, -it simply registers all functions in the list l -(see luaL_Reg) into the table on the top of the stack. - - -

-When called with a non-null libname, -luaL_register creates a new table t, -sets it as the value of the global variable libname, -sets it as the value of package.loaded[libname], -and registers on it all functions in the list l. -If there is a table in package.loaded[libname] or in -variable libname, -reuses this table instead of creating a new one. - - -

-In any case the function leaves the table -on the top of the stack. - - - - - -


luaL_typename

-[-0, +0, -] -

const char *luaL_typename (lua_State *L, int index);
- -

-Returns the name of the type of the value at the given index. - - - - - -


luaL_typerror

-[-0, +0, v] -

int luaL_typerror (lua_State *L, int narg, const char *tname);
- -

-Generates an error with a message like the following: - -

-     location: bad argument narg to 'func' (tname expected, got rt)
-

-where location is produced by luaL_where, -func is the name of the current function, -and rt is the type name of the actual argument. - - - - - -


luaL_unref

-[-0, +0, -] -

void luaL_unref (lua_State *L, int t, int ref);
- -

-Releases reference ref from the table at index t -(see luaL_ref). -The entry is removed from the table, -so that the referred object can be collected. -The reference ref is also freed to be used again. - - -

-If ref is LUA_NOREF or LUA_REFNIL, -luaL_unref does nothing. - - - - - -


luaL_where

-[-0, +1, m] -

void luaL_where (lua_State *L, int lvl);
- -

-Pushes onto the stack a string identifying the current position -of the control at level lvl in the call stack. -Typically this string has the following format: - -

-     chunkname:currentline:
-

-Level 0 is the running function, -level 1 is the function that called the running function, -etc. - - -

-This function is used to build a prefix for error messages. - - - - - - - -

5 - Standard Libraries

- -

-The standard Lua libraries provide useful functions -that are implemented directly through the C API. -Some of these functions provide essential services to the language -(e.g., type and getmetatable); -others provide access to "outside" services (e.g., I/O); -and others could be implemented in Lua itself, -but are quite useful or have critical performance requirements that -deserve an implementation in C (e.g., table.sort). - - -

-All libraries are implemented through the official C API -and are provided as separate C modules. -Currently, Lua has the following standard libraries: - -

    - -
  • basic library, which includes the coroutine sub-library;
  • - -
  • package library;
  • - -
  • string manipulation;
  • - -
  • table manipulation;
  • - -
  • mathematical functions (sin, log, etc.);
  • - -
  • input and output;
  • - -
  • operating system facilities;
  • - -
  • debug facilities.
  • - -

-Except for the basic and package libraries, -each library provides all its functions as fields of a global table -or as methods of its objects. - - -

-To have access to these libraries, -the C host program should call the luaL_openlibs function, -which opens all standard libraries. -Alternatively, -it can open them individually by calling -luaopen_base (for the basic library), -luaopen_package (for the package library), -luaopen_string (for the string library), -luaopen_table (for the table library), -luaopen_math (for the mathematical library), -luaopen_io (for the I/O library), -luaopen_os (for the Operating System library), -and luaopen_debug (for the debug library). -These functions are declared in lualib.h -and should not be called directly: -you must call them like any other Lua C function, -e.g., by using lua_call. - - - -

5.1 - Basic Functions

- -

-The basic library provides some core functions to Lua. -If you do not include this library in your application, -you should check carefully whether you need to provide -implementations for some of its facilities. - - -

-


assert (v [, message])

-Issues an error when -the value of its argument v is false (i.e., nil or false); -otherwise, returns all its arguments. -message is an error message; -when absent, it defaults to "assertion failed!" - - - - -

-


collectgarbage ([opt [, arg]])

- - -

-This function is a generic interface to the garbage collector. -It performs different functions according to its first argument, opt: - -

    - -
  • "collect": -performs a full garbage-collection cycle. -This is the default option. -
  • - -
  • "stop": -stops the garbage collector. -
  • - -
  • "restart": -restarts the garbage collector. -
  • - -
  • "count": -returns the total memory in use by Lua (in Kbytes). -
  • - -
  • "step": -performs a garbage-collection step. -The step "size" is controlled by arg -(larger values mean more steps) in a non-specified way. -If you want to control the step size -you must experimentally tune the value of arg. -Returns true if the step finished a collection cycle. -
  • - -
  • "setpause": -sets arg as the new value for the pause of -the collector (see §2.10). -Returns the previous value for pause. -
  • - -
  • "setstepmul": -sets arg as the new value for the step multiplier of -the collector (see §2.10). -Returns the previous value for step. -
  • - -
- - - -

-


dofile ([filename])

-Opens the named file and executes its contents as a Lua chunk. -When called without arguments, -dofile executes the contents of the standard input (stdin). -Returns all values returned by the chunk. -In case of errors, dofile propagates the error -to its caller (that is, dofile does not run in protected mode). - - - - -

-


error (message [, level])

-Terminates the last protected function called -and returns message as the error message. -Function error never returns. - - -

-Usually, error adds some information about the error position -at the beginning of the message. -The level argument specifies how to get the error position. -With level 1 (the default), the error position is where the -error function was called. -Level 2 points the error to where the function -that called error was called; and so on. -Passing a level 0 avoids the addition of error position information -to the message. - - - - -

-


_G

-A global variable (not a function) that -holds the global environment (that is, _G._G = _G). -Lua itself does not use this variable; -changing its value does not affect any environment, -nor vice-versa. -(Use setfenv to change environments.) - - - - -

-


getfenv ([f])

-Returns the current environment in use by the function. -f can be a Lua function or a number -that specifies the function at that stack level: -Level 1 is the function calling getfenv. -If the given function is not a Lua function, -or if f is 0, -getfenv returns the global environment. -The default for f is 1. - - - - -

-


getmetatable (object)

- - -

-If object does not have a metatable, returns nil. -Otherwise, -if the object's metatable has a "__metatable" field, -returns the associated value. -Otherwise, returns the metatable of the given object. - - - - -

-


ipairs (t)

- - -

-Returns three values: an iterator function, the table t, and 0, -so that the construction - -

-     for i,v in ipairs(t) do body end
-

-will iterate over the pairs (1,t[1]), (2,t[2]), ···, -up to the first integer key absent from the table. - - - - -

-


load (func [, chunkname])

- - -

-Loads a chunk using function func to get its pieces. -Each call to func must return a string that concatenates -with previous results. -A return of an empty string, nil, or no value signals the end of the chunk. - - -

-If there are no errors, -returns the compiled chunk as a function; -otherwise, returns nil plus the error message. -The environment of the returned function is the global environment. - - -

-chunkname is used as the chunk name for error messages -and debug information. -When absent, -it defaults to "=(load)". - - - - -

-


loadfile ([filename])

- - -

-Similar to load, -but gets the chunk from file filename -or from the standard input, -if no file name is given. - - - - -

-


loadstring (string [, chunkname])

- - -

-Similar to load, -but gets the chunk from the given string. - - -

-To load and run a given string, use the idiom - -

-     assert(loadstring(s))()
-
- -

-When absent, -chunkname defaults to the given string. - - - - -

-


next (table [, index])

- - -

-Allows a program to traverse all fields of a table. -Its first argument is a table and its second argument -is an index in this table. -next returns the next index of the table -and its associated value. -When called with nil as its second argument, -next returns an initial index -and its associated value. -When called with the last index, -or with nil in an empty table, -next returns nil. -If the second argument is absent, then it is interpreted as nil. -In particular, -you can use next(t) to check whether a table is empty. - - -

-The order in which the indices are enumerated is not specified, -even for numeric indices. -(To traverse a table in numeric order, -use a numerical for or the ipairs function.) - - -

-The behavior of next is undefined if, -during the traversal, -you assign any value to a non-existent field in the table. -You may however modify existing fields. -In particular, you may clear existing fields. - - - - -

-


pairs (t)

- - -

-Returns three values: the next function, the table t, and nil, -so that the construction - -

-     for k,v in pairs(t) do body end
-

-will iterate over all key–value pairs of table t. - - -

-See function next for the caveats of modifying -the table during its traversal. - - - - -

-


pcall (f, arg1, ···)

- - -

-Calls function f with -the given arguments in protected mode. -This means that any error inside f is not propagated; -instead, pcall catches the error -and returns a status code. -Its first result is the status code (a boolean), -which is true if the call succeeds without errors. -In such case, pcall also returns all results from the call, -after this first result. -In case of any error, pcall returns false plus the error message. - - - - -

-


print (···)

-Receives any number of arguments, -and prints their values to stdout, -using the tostring function to convert them to strings. -print is not intended for formatted output, -but only as a quick way to show a value, -typically for debugging. -For formatted output, use string.format. - - - - -

-


rawequal (v1, v2)

-Checks whether v1 is equal to v2, -without invoking any metamethod. -Returns a boolean. - - - - -

-


rawget (table, index)

-Gets the real value of table[index], -without invoking any metamethod. -table must be a table; -index may be any value. - - - - -

-


rawset (table, index, value)

-Sets the real value of table[index] to value, -without invoking any metamethod. -table must be a table, -index any value different from nil, -and value any Lua value. - - -

-This function returns table. - - - - -

-


select (index, ···)

- - -

-If index is a number, -returns all arguments after argument number index. -Otherwise, index must be the string "#", -and select returns the total number of extra arguments it received. - - - - -

-


setfenv (f, table)

- - -

-Sets the environment to be used by the given function. -f can be a Lua function or a number -that specifies the function at that stack level: -Level 1 is the function calling setfenv. -setfenv returns the given function. - - -

-As a special case, when f is 0 setfenv changes -the environment of the running thread. -In this case, setfenv returns no values. - - - - -

-


setmetatable (table, metatable)

- - -

-Sets the metatable for the given table. -(You cannot change the metatable of other types from Lua, only from C.) -If metatable is nil, -removes the metatable of the given table. -If the original metatable has a "__metatable" field, -raises an error. - - -

-This function returns table. - - - - -

-


tonumber (e [, base])

-Tries to convert its argument to a number. -If the argument is already a number or a string convertible -to a number, then tonumber returns this number; -otherwise, it returns nil. - - -

-An optional argument specifies the base to interpret the numeral. -The base may be any integer between 2 and 36, inclusive. -In bases above 10, the letter 'A' (in either upper or lower case) -represents 10, 'B' represents 11, and so forth, -with 'Z' representing 35. -In base 10 (the default), the number can have a decimal part, -as well as an optional exponent part (see §2.1). -In other bases, only unsigned integers are accepted. - - - - -

-


tostring (e)

-Receives an argument of any type and -converts it to a string in a reasonable format. -For complete control of how numbers are converted, -use string.format. - - -

-If the metatable of e has a "__tostring" field, -then tostring calls the corresponding value -with e as argument, -and uses the result of the call as its result. - - - - -

-


type (v)

-Returns the type of its only argument, coded as a string. -The possible results of this function are -"nil" (a string, not the value nil), -"number", -"string", -"boolean", -"table", -"function", -"thread", -and "userdata". - - - - -

-


unpack (list [, i [, j]])

-Returns the elements from the given table. -This function is equivalent to - -
-     return list[i], list[i+1], ···, list[j]
-

-except that the above code can be written only for a fixed number -of elements. -By default, i is 1 and j is the length of the list, -as defined by the length operator (see §2.5.5). - - - - -

-


_VERSION

-A global variable (not a function) that -holds a string containing the current interpreter version. -The current contents of this variable is "Lua 5.1". - - - - -

-


xpcall (f, err)

- - -

-This function is similar to pcall, -except that you can set a new error handler. - - -

-xpcall calls function f in protected mode, -using err as the error handler. -Any error inside f is not propagated; -instead, xpcall catches the error, -calls the err function with the original error object, -and returns a status code. -Its first result is the status code (a boolean), -which is true if the call succeeds without errors. -In this case, xpcall also returns all results from the call, -after this first result. -In case of any error, -xpcall returns false plus the result from err. - - - - - - - -

5.2 - Coroutine Manipulation

- -

-The operations related to coroutines comprise a sub-library of -the basic library and come inside the table coroutine. -See §2.11 for a general description of coroutines. - - -

-


coroutine.create (f)

- - -

-Creates a new coroutine, with body f. -f must be a Lua function. -Returns this new coroutine, -an object with type "thread". - - - - -

-


coroutine.resume (co [, val1, ···])

- - -

-Starts or continues the execution of coroutine co. -The first time you resume a coroutine, -it starts running its body. -The values val1, ··· are passed -as the arguments to the body function. -If the coroutine has yielded, -resume restarts it; -the values val1, ··· are passed -as the results from the yield. - - -

-If the coroutine runs without any errors, -resume returns true plus any values passed to yield -(if the coroutine yields) or any values returned by the body function -(if the coroutine terminates). -If there is any error, -resume returns false plus the error message. - - - - -

-


coroutine.running ()

- - -

-Returns the running coroutine, -or nil when called by the main thread. - - - - -

-


coroutine.status (co)

- - -

-Returns the status of coroutine co, as a string: -"running", -if the coroutine is running (that is, it called status); -"suspended", if the coroutine is suspended in a call to yield, -or if it has not started running yet; -"normal" if the coroutine is active but not running -(that is, it has resumed another coroutine); -and "dead" if the coroutine has finished its body function, -or if it has stopped with an error. - - - - -

-


coroutine.wrap (f)

- - -

-Creates a new coroutine, with body f. -f must be a Lua function. -Returns a function that resumes the coroutine each time it is called. -Any arguments passed to the function behave as the -extra arguments to resume. -Returns the same values returned by resume, -except the first boolean. -In case of error, propagates the error. - - - - -

-


coroutine.yield (···)

- - -

-Suspends the execution of the calling coroutine. -The coroutine cannot be running a C function, -a metamethod, or an iterator. -Any arguments to yield are passed as extra results to resume. - - - - - - - -

5.3 - Modules

- -

-The package library provides basic -facilities for loading and building modules in Lua. -It exports two of its functions directly in the global environment: -require and module. -Everything else is exported in a table package. - - -

-


module (name [, ···])

- - -

-Creates a module. -If there is a table in package.loaded[name], -this table is the module. -Otherwise, if there is a global table t with the given name, -this table is the module. -Otherwise creates a new table t and -sets it as the value of the global name and -the value of package.loaded[name]. -This function also initializes t._NAME with the given name, -t._M with the module (t itself), -and t._PACKAGE with the package name -(the full module name minus last component; see below). -Finally, module sets t as the new environment -of the current function and the new value of package.loaded[name], -so that require returns t. - - -

-If name is a compound name -(that is, one with components separated by dots), -module creates (or reuses, if they already exist) -tables for each component. -For instance, if name is a.b.c, -then module stores the module table in field c of -field b of global a. - - -

-This function can receive optional options after -the module name, -where each option is a function to be applied over the module. - - - - -

-


require (modname)

- - -

-Loads the given module. -The function starts by looking into the package.loaded table -to determine whether modname is already loaded. -If it is, then require returns the value stored -at package.loaded[modname]. -Otherwise, it tries to find a loader for the module. - - -

-To find a loader, -require is guided by the package.loaders array. -By changing this array, -we can change how require looks for a module. -The following explanation is based on the default configuration -for package.loaders. - - -

-First require queries package.preload[modname]. -If it has a value, -this value (which should be a function) is the loader. -Otherwise require searches for a Lua loader using the -path stored in package.path. -If that also fails, it searches for a C loader using the -path stored in package.cpath. -If that also fails, -it tries an all-in-one loader (see package.loaders). - - -

-Once a loader is found, -require calls the loader with a single argument, modname. -If the loader returns any value, -require assigns the returned value to package.loaded[modname]. -If the loader returns no value and -has not assigned any value to package.loaded[modname], -then require assigns true to this entry. -In any case, require returns the -final value of package.loaded[modname]. - - -

-If there is any error loading or running the module, -or if it cannot find any loader for the module, -then require signals an error. - - - - -

-


package.cpath

- - -

-The path used by require to search for a C loader. - - -

-Lua initializes the C path package.cpath in the same way -it initializes the Lua path package.path, -using the environment variable LUA_CPATH -or a default path defined in luaconf.h. - - - - -

- -


package.loaded

- - -

-A table used by require to control which -modules are already loaded. -When you require a module modname and -package.loaded[modname] is not false, -require simply returns the value stored there. - - - - -

-


package.loaders

- - -

-A table used by require to control how to load modules. - - -

-Each entry in this table is a searcher function. -When looking for a module, -require calls each of these searchers in ascending order, -with the module name (the argument given to require) as its -sole parameter. -The function can return another function (the module loader) -or a string explaining why it did not find that module -(or nil if it has nothing to say). -Lua initializes this table with four functions. - - -

-The first searcher simply looks for a loader in the -package.preload table. - - -

-The second searcher looks for a loader as a Lua library, -using the path stored at package.path. -A path is a sequence of templates separated by semicolons. -For each template, -the searcher will change each interrogation -mark in the template by filename, -which is the module name with each dot replaced by a -"directory separator" (such as "/" in Unix); -then it will try to open the resulting file name. -So, for instance, if the Lua path is the string - -

-     "./?.lua;./?.lc;/usr/local/?/init.lua"
-

-the search for a Lua file for module foo -will try to open the files -./foo.lua, ./foo.lc, and -/usr/local/foo/init.lua, in that order. - - -

-The third searcher looks for a loader as a C library, -using the path given by the variable package.cpath. -For instance, -if the C path is the string - -

-     "./?.so;./?.dll;/usr/local/?/init.so"
-

-the searcher for module foo -will try to open the files ./foo.so, ./foo.dll, -and /usr/local/foo/init.so, in that order. -Once it finds a C library, -this searcher first uses a dynamic link facility to link the -application with the library. -Then it tries to find a C function inside the library to -be used as the loader. -The name of this C function is the string "luaopen_" -concatenated with a copy of the module name where each dot -is replaced by an underscore. -Moreover, if the module name has a hyphen, -its prefix up to (and including) the first hyphen is removed. -For instance, if the module name is a.v1-b.c, -the function name will be luaopen_b_c. - - -

-The fourth searcher tries an all-in-one loader. -It searches the C path for a library for -the root name of the given module. -For instance, when requiring a.b.c, -it will search for a C library for a. -If found, it looks into it for an open function for -the submodule; -in our example, that would be luaopen_a_b_c. -With this facility, a package can pack several C submodules -into one single library, -with each submodule keeping its original open function. - - - - -

-


package.loadlib (libname, funcname)

- - -

-Dynamically links the host program with the C library libname. -Inside this library, looks for a function funcname -and returns this function as a C function. -(So, funcname must follow the protocol (see lua_CFunction)). - - -

-This is a low-level function. -It completely bypasses the package and module system. -Unlike require, -it does not perform any path searching and -does not automatically adds extensions. -libname must be the complete file name of the C library, -including if necessary a path and extension. -funcname must be the exact name exported by the C library -(which may depend on the C compiler and linker used). - - -

-This function is not supported by ANSI C. -As such, it is only available on some platforms -(Windows, Linux, Mac OS X, Solaris, BSD, -plus other Unix systems that support the dlfcn standard). - - - - -

-


package.path

- - -

-The path used by require to search for a Lua loader. - - -

-At start-up, Lua initializes this variable with -the value of the environment variable LUA_PATH or -with a default path defined in luaconf.h, -if the environment variable is not defined. -Any ";;" in the value of the environment variable -is replaced by the default path. - - - - -

-


package.preload

- - -

-A table to store loaders for specific modules -(see require). - - - - -

-


package.seeall (module)

- - -

-Sets a metatable for module with -its __index field referring to the global environment, -so that this module inherits values -from the global environment. -To be used as an option to function module. - - - - - - - -

5.4 - String Manipulation

- -

-This library provides generic functions for string manipulation, -such as finding and extracting substrings, and pattern matching. -When indexing a string in Lua, the first character is at position 1 -(not at 0, as in C). -Indices are allowed to be negative and are interpreted as indexing backwards, -from the end of the string. -Thus, the last character is at position -1, and so on. - - -

-The string library provides all its functions inside the table -string. -It also sets a metatable for strings -where the __index field points to the string table. -Therefore, you can use the string functions in object-oriented style. -For instance, string.byte(s, i) -can be written as s:byte(i). - - -

-The string library assumes one-byte character encodings. - - -

-


string.byte (s [, i [, j]])

-Returns the internal numerical codes of the characters s[i], -s[i+1], ···, s[j]. -The default value for i is 1; -the default value for j is i. - - -

-Note that numerical codes are not necessarily portable across platforms. - - - - -

-


string.char (···)

-Receives zero or more integers. -Returns a string with length equal to the number of arguments, -in which each character has the internal numerical code equal -to its corresponding argument. - - -

-Note that numerical codes are not necessarily portable across platforms. - - - - -

-


string.dump (function)

- - -

-Returns a string containing a binary representation of the given function, -so that a later loadstring on this string returns -a copy of the function. -function must be a Lua function without upvalues. - - - - -

-


string.find (s, pattern [, init [, plain]])

-Looks for the first match of -pattern in the string s. -If it finds a match, then find returns the indices of s -where this occurrence starts and ends; -otherwise, it returns nil. -A third, optional numerical argument init specifies -where to start the search; -its default value is 1 and can be negative. -A value of true as a fourth, optional argument plain -turns off the pattern matching facilities, -so the function does a plain "find substring" operation, -with no characters in pattern being considered "magic". -Note that if plain is given, then init must be given as well. - - -

-If the pattern has captures, -then in a successful match -the captured values are also returned, -after the two indices. - - - - -

-


string.format (formatstring, ···)

-Returns a formatted version of its variable number of arguments -following the description given in its first argument (which must be a string). -The format string follows the same rules as the printf family of -standard C functions. -The only differences are that the options/modifiers -*, l, L, n, p, -and h are not supported -and that there is an extra option, q. -The q option formats a string in a form suitable to be safely read -back by the Lua interpreter: -the string is written between double quotes, -and all double quotes, newlines, embedded zeros, -and backslashes in the string -are correctly escaped when written. -For instance, the call - -
-     string.format('%q', 'a string with "quotes" and \n new line')
-

-will produce the string: - -

-     "a string with \"quotes\" and \
-      new line"
-
- -

-The options c, d, E, e, f, -g, G, i, o, u, X, and x all -expect a number as argument, -whereas q and s expect a string. - - -

-This function does not accept string values -containing embedded zeros, -except as arguments to the q option. - - - - -

-


string.gmatch (s, pattern)

-Returns an iterator function that, -each time it is called, -returns the next captures from pattern over string s. -If pattern specifies no captures, -then the whole match is produced in each call. - - -

-As an example, the following loop - -

-     s = "hello world from Lua"
-     for w in string.gmatch(s, "%a+") do
-       print(w)
-     end
-

-will iterate over all the words from string s, -printing one per line. -The next example collects all pairs key=value from the -given string into a table: - -

-     t = {}
-     s = "from=world, to=Lua"
-     for k, v in string.gmatch(s, "(%w+)=(%w+)") do
-       t[k] = v
-     end
-
- -

-For this function, a '^' at the start of a pattern does not -work as an anchor, as this would prevent the iteration. - - - - -

-


string.gsub (s, pattern, repl [, n])

-Returns a copy of s -in which all (or the first n, if given) -occurrences of the pattern have been -replaced by a replacement string specified by repl, -which can be a string, a table, or a function. -gsub also returns, as its second value, -the total number of matches that occurred. - - -

-If repl is a string, then its value is used for replacement. -The character % works as an escape character: -any sequence in repl of the form %n, -with n between 1 and 9, -stands for the value of the n-th captured substring (see below). -The sequence %0 stands for the whole match. -The sequence %% stands for a single %. - - -

-If repl is a table, then the table is queried for every match, -using the first capture as the key; -if the pattern specifies no captures, -then the whole match is used as the key. - - -

-If repl is a function, then this function is called every time a -match occurs, with all captured substrings passed as arguments, -in order; -if the pattern specifies no captures, -then the whole match is passed as a sole argument. - - -

-If the value returned by the table query or by the function call -is a string or a number, -then it is used as the replacement string; -otherwise, if it is false or nil, -then there is no replacement -(that is, the original match is kept in the string). - - -

-Here are some examples: - -

-     x = string.gsub("hello world", "(%w+)", "%1 %1")
-     --> x="hello hello world world"
-     
-     x = string.gsub("hello world", "%w+", "%0 %0", 1)
-     --> x="hello hello world"
-     
-     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
-     --> x="world hello Lua from"
-     
-     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
-     --> x="home = /home/roberto, user = roberto"
-     
-     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
-           return loadstring(s)()
-         end)
-     --> x="4+5 = 9"
-     
-     local t = {name="lua", version="5.1"}
-     x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
-     --> x="lua-5.1.tar.gz"
-
- - - -

-


string.len (s)

-Receives a string and returns its length. -The empty string "" has length 0. -Embedded zeros are counted, -so "a\000bc\000" has length 5. - - - - -

-


string.lower (s)

-Receives a string and returns a copy of this string with all -uppercase letters changed to lowercase. -All other characters are left unchanged. -The definition of what an uppercase letter is depends on the current locale. - - - - -

-


string.match (s, pattern [, init])

-Looks for the first match of -pattern in the string s. -If it finds one, then match returns -the captures from the pattern; -otherwise it returns nil. -If pattern specifies no captures, -then the whole match is returned. -A third, optional numerical argument init specifies -where to start the search; -its default value is 1 and can be negative. - - - - -

-


string.rep (s, n)

-Returns a string that is the concatenation of n copies of -the string s. - - - - -

-


string.reverse (s)

-Returns a string that is the string s reversed. - - - - -

-


string.sub (s, i [, j])

-Returns the substring of s that -starts at i and continues until j; -i and j can be negative. -If j is absent, then it is assumed to be equal to -1 -(which is the same as the string length). -In particular, -the call string.sub(s,1,j) returns a prefix of s -with length j, -and string.sub(s, -i) returns a suffix of s -with length i. - - - - -

-


string.upper (s)

-Receives a string and returns a copy of this string with all -lowercase letters changed to uppercase. -All other characters are left unchanged. -The definition of what a lowercase letter is depends on the current locale. - - - -

5.4.1 - Patterns

- - -

Character Class:

-A character class is used to represent a set of characters. -The following combinations are allowed in describing a character class: - -

    - -
  • x: -(where x is not one of the magic characters -^$()%.[]*+-?) -represents the character x itself. -
  • - -
  • .: (a dot) represents all characters.
  • - -
  • %a: represents all letters.
  • - -
  • %c: represents all control characters.
  • - -
  • %d: represents all digits.
  • - -
  • %l: represents all lowercase letters.
  • - -
  • %p: represents all punctuation characters.
  • - -
  • %s: represents all space characters.
  • - -
  • %u: represents all uppercase letters.
  • - -
  • %w: represents all alphanumeric characters.
  • - -
  • %x: represents all hexadecimal digits.
  • - -
  • %z: represents the character with representation 0.
  • - -
  • %x: (where x is any non-alphanumeric character) -represents the character x. -This is the standard way to escape the magic characters. -Any punctuation character (even the non magic) -can be preceded by a '%' -when used to represent itself in a pattern. -
  • - -
  • [set]: -represents the class which is the union of all -characters in set. -A range of characters can be specified by -separating the end characters of the range with a '-'. -All classes %x described above can also be used as -components in set. -All other characters in set represent themselves. -For example, [%w_] (or [_%w]) -represents all alphanumeric characters plus the underscore, -[0-7] represents the octal digits, -and [0-7%l%-] represents the octal digits plus -the lowercase letters plus the '-' character. - - -

    -The interaction between ranges and classes is not defined. -Therefore, patterns like [%a-z] or [a-%%] -have no meaning. -

  • - -
  • [^set]: -represents the complement of set, -where set is interpreted as above. -
  • - -

-For all classes represented by single letters (%a, %c, etc.), -the corresponding uppercase letter represents the complement of the class. -For instance, %S represents all non-space characters. - - -

-The definitions of letter, space, and other character groups -depend on the current locale. -In particular, the class [a-z] may not be equivalent to %l. - - - - - -

Pattern Item:

-A pattern item can be - -

    - -
  • -a single character class, -which matches any single character in the class; -
  • - -
  • -a single character class followed by '*', -which matches 0 or more repetitions of characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '+', -which matches 1 or more repetitions of characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '-', -which also matches 0 or more repetitions of characters in the class. -Unlike '*', -these repetition items will always match the shortest possible sequence; -
  • - -
  • -a single character class followed by '?', -which matches 0 or 1 occurrence of a character in the class; -
  • - -
  • -%n, for n between 1 and 9; -such item matches a substring equal to the n-th captured string -(see below); -
  • - -
  • -%bxy, where x and y are two distinct characters; -such item matches strings that start with x, end with y, -and where the x and y are balanced. -This means that, if one reads the string from left to right, -counting +1 for an x and -1 for a y, -the ending y is the first y where the count reaches 0. -For instance, the item %b() matches expressions with -balanced parentheses. -
  • - -
- - - - -

Pattern:

-A pattern is a sequence of pattern items. -A '^' at the beginning of a pattern anchors the match at the -beginning of the subject string. -A '$' at the end of a pattern anchors the match at the -end of the subject string. -At other positions, -'^' and '$' have no special meaning and represent themselves. - - - - - -

Captures:

-A pattern can contain sub-patterns enclosed in parentheses; -they describe captures. -When a match succeeds, the substrings of the subject string -that match captures are stored (captured) for future use. -Captures are numbered according to their left parentheses. -For instance, in the pattern "(a*(.)%w(%s*))", -the part of the string matching "a*(.)%w(%s*)" is -stored as the first capture (and therefore has number 1); -the character matching "." is captured with number 2, -and the part matching "%s*" has number 3. - - -

-As a special case, the empty capture () captures -the current string position (a number). -For instance, if we apply the pattern "()aa()" on the -string "flaaap", there will be two captures: 3 and 5. - - -

-A pattern cannot contain embedded zeros. Use %z instead. - - - - - - - - - - - -

5.5 - Table Manipulation

-This library provides generic functions for table manipulation. -It provides all its functions inside the table table. - - -

-Most functions in the table library assume that the table -represents an array or a list. -For these functions, when we talk about the "length" of a table -we mean the result of the length operator. - - -

-


table.concat (table [, sep [, i [, j]]])

-Given an array where all elements are strings or numbers, -returns table[i]..sep..table[i+1] ··· sep..table[j]. -The default value for sep is the empty string, -the default for i is 1, -and the default for j is the length of the table. -If i is greater than j, returns the empty string. - - - - -

-


table.insert (table, [pos,] value)

- - -

-Inserts element value at position pos in table, -shifting up other elements to open space, if necessary. -The default value for pos is n+1, -where n is the length of the table (see §2.5.5), -so that a call table.insert(t,x) inserts x at the end -of table t. - - - - -

-


table.maxn (table)

- - -

-Returns the largest positive numerical index of the given table, -or zero if the table has no positive numerical indices. -(To do its job this function does a linear traversal of -the whole table.) - - - - -

-


table.remove (table [, pos])

- - -

-Removes from table the element at position pos, -shifting down other elements to close the space, if necessary. -Returns the value of the removed element. -The default value for pos is n, -where n is the length of the table, -so that a call table.remove(t) removes the last element -of table t. - - - - -

-


table.sort (table [, comp])

-Sorts table elements in a given order, in-place, -from table[1] to table[n], -where n is the length of the table. -If comp is given, -then it must be a function that receives two table elements, -and returns true -when the first is less than the second -(so that not comp(a[i+1],a[i]) will be true after the sort). -If comp is not given, -then the standard Lua operator < is used instead. - - -

-The sort algorithm is not stable; -that is, elements considered equal by the given order -may have their relative positions changed by the sort. - - - - - - - -

5.6 - Mathematical Functions

- -

-This library is an interface to the standard C math library. -It provides all its functions inside the table math. - - -

-


math.abs (x)

- - -

-Returns the absolute value of x. - - - - -

-


math.acos (x)

- - -

-Returns the arc cosine of x (in radians). - - - - -

-


math.asin (x)

- - -

-Returns the arc sine of x (in radians). - - - - -

-


math.atan (x)

- - -

-Returns the arc tangent of x (in radians). - - - - -

-


math.atan2 (y, x)

- - -

-Returns the arc tangent of y/x (in radians), -but uses the signs of both parameters to find the -quadrant of the result. -(It also handles correctly the case of x being zero.) - - - - -

-


math.ceil (x)

- - -

-Returns the smallest integer larger than or equal to x. - - - - -

-


math.cos (x)

- - -

-Returns the cosine of x (assumed to be in radians). - - - - -

-


math.cosh (x)

- - -

-Returns the hyperbolic cosine of x. - - - - -

-


math.deg (x)

- - -

-Returns the angle x (given in radians) in degrees. - - - - -

-


math.exp (x)

- - -

-Returns the value ex. - - - - -

-


math.floor (x)

- - -

-Returns the largest integer smaller than or equal to x. - - - - -

-


math.fmod (x, y)

- - -

-Returns the remainder of the division of x by y -that rounds the quotient towards zero. - - - - -

-


math.frexp (x)

- - -

-Returns m and e such that x = m2e, -e is an integer and the absolute value of m is -in the range [0.5, 1) -(or zero when x is zero). - - - - -

-


math.huge

- - -

-The value HUGE_VAL, -a value larger than or equal to any other numerical value. - - - - -

-


math.ldexp (m, e)

- - -

-Returns m2e (e should be an integer). - - - - -

-


math.log (x)

- - -

-Returns the natural logarithm of x. - - - - -

-


math.log10 (x)

- - -

-Returns the base-10 logarithm of x. - - - - -

-


math.max (x, ···)

- - -

-Returns the maximum value among its arguments. - - - - -

-


math.min (x, ···)

- - -

-Returns the minimum value among its arguments. - - - - -

-


math.modf (x)

- - -

-Returns two numbers, -the integral part of x and the fractional part of x. - - - - -

-


math.pi

- - -

-The value of pi. - - - - -

-


math.pow (x, y)

- - -

-Returns xy. -(You can also use the expression x^y to compute this value.) - - - - -

-


math.rad (x)

- - -

-Returns the angle x (given in degrees) in radians. - - - - -

-


math.random ([m [, n]])

- - -

-This function is an interface to the simple -pseudo-random generator function rand provided by ANSI C. -(No guarantees can be given for its statistical properties.) - - -

-When called without arguments, -returns a uniform pseudo-random real number -in the range [0,1). -When called with an integer number m, -math.random returns -a uniform pseudo-random integer in the range [1, m]. -When called with two integer numbers m and n, -math.random returns a uniform pseudo-random -integer in the range [m, n]. - - - - -

-


math.randomseed (x)

- - -

-Sets x as the "seed" -for the pseudo-random generator: -equal seeds produce equal sequences of numbers. - - - - -

-


math.sin (x)

- - -

-Returns the sine of x (assumed to be in radians). - - - - -

-


math.sinh (x)

- - -

-Returns the hyperbolic sine of x. - - - - -

-


math.sqrt (x)

- - -

-Returns the square root of x. -(You can also use the expression x^0.5 to compute this value.) - - - - -

-


math.tan (x)

- - -

-Returns the tangent of x (assumed to be in radians). - - - - -

-


math.tanh (x)

- - -

-Returns the hyperbolic tangent of x. - - - - - - - -

5.7 - Input and Output Facilities

- -

-The I/O library provides two different styles for file manipulation. -The first one uses implicit file descriptors; -that is, there are operations to set a default input file and a -default output file, -and all input/output operations are over these default files. -The second style uses explicit file descriptors. - - -

-When using implicit file descriptors, -all operations are supplied by table io. -When using explicit file descriptors, -the operation io.open returns a file descriptor -and then all operations are supplied as methods of the file descriptor. - - -

-The table io also provides -three predefined file descriptors with their usual meanings from C: -io.stdin, io.stdout, and io.stderr. -The I/O library never closes these files. - - -

-Unless otherwise stated, -all I/O functions return nil on failure -(plus an error message as a second result and -a system-dependent error code as a third result) -and some value different from nil on success. - - -

-


io.close ([file])

- - -

-Equivalent to file:close(). -Without a file, closes the default output file. - - - - -

-


io.flush ()

- - -

-Equivalent to file:flush over the default output file. - - - - -

-


io.input ([file])

- - -

-When called with a file name, it opens the named file (in text mode), -and sets its handle as the default input file. -When called with a file handle, -it simply sets this file handle as the default input file. -When called without parameters, -it returns the current default input file. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


io.lines ([filename])

- - -

-Opens the given file name in read mode -and returns an iterator function that, -each time it is called, -returns a new line from the file. -Therefore, the construction - -

-     for line in io.lines(filename) do body end
-

-will iterate over all lines of the file. -When the iterator function detects the end of file, -it returns nil (to finish the loop) and automatically closes the file. - - -

-The call io.lines() (with no file name) is equivalent -to io.input():lines(); -that is, it iterates over the lines of the default input file. -In this case it does not close the file when the loop ends. - - - - -

-


io.open (filename [, mode])

- - -

-This function opens a file, -in the mode specified in the string mode. -It returns a new file handle, -or, in case of errors, nil plus an error message. - - -

-The mode string can be any of the following: - -

    -
  • "r": read mode (the default);
  • -
  • "w": write mode;
  • -
  • "a": append mode;
  • -
  • "r+": update mode, all previous data is preserved;
  • -
  • "w+": update mode, all previous data is erased;
  • -
  • "a+": append update mode, previous data is preserved, - writing is only allowed at the end of file.
  • -

-The mode string can also have a 'b' at the end, -which is needed in some systems to open the file in binary mode. -This string is exactly what is used in the -standard C function fopen. - - - - -

-


io.output ([file])

- - -

-Similar to io.input, but operates over the default output file. - - - - -

-


io.popen (prog [, mode])

- - -

-Starts program prog in a separated process and returns -a file handle that you can use to read data from this program -(if mode is "r", the default) -or to write data to this program -(if mode is "w"). - - -

-This function is system dependent and is not available -on all platforms. - - - - -

-


io.read (···)

- - -

-Equivalent to io.input():read. - - - - -

-


io.tmpfile ()

- - -

-Returns a handle for a temporary file. -This file is opened in update mode -and it is automatically removed when the program ends. - - - - -

-


io.type (obj)

- - -

-Checks whether obj is a valid file handle. -Returns the string "file" if obj is an open file handle, -"closed file" if obj is a closed file handle, -or nil if obj is not a file handle. - - - - -

-


io.write (···)

- - -

-Equivalent to io.output():write. - - - - -

-


file:close ()

- - -

-Closes file. -Note that files are automatically closed when -their handles are garbage collected, -but that takes an unpredictable amount of time to happen. - - - - -

-


file:flush ()

- - -

-Saves any written data to file. - - - - -

-


file:lines ()

- - -

-Returns an iterator function that, -each time it is called, -returns a new line from the file. -Therefore, the construction - -

-     for line in file:lines() do body end
-

-will iterate over all lines of the file. -(Unlike io.lines, this function does not close the file -when the loop ends.) - - - - -

-


file:read (···)

- - -

-Reads the file file, -according to the given formats, which specify what to read. -For each format, -the function returns a string (or a number) with the characters read, -or nil if it cannot read data with the specified format. -When called without formats, -it uses a default format that reads the entire next line -(see below). - - -

-The available formats are - -

    - -
  • "*n": -reads a number; -this is the only format that returns a number instead of a string. -
  • - -
  • "*a": -reads the whole file, starting at the current position. -On end of file, it returns the empty string. -
  • - -
  • "*l": -reads the next line (skipping the end of line), -returning nil on end of file. -This is the default format. -
  • - -
  • number: -reads a string with up to this number of characters, -returning nil on end of file. -If number is zero, -it reads nothing and returns an empty string, -or nil on end of file. -
  • - -
- - - -

-


file:seek ([whence] [, offset])

- - -

-Sets and gets the file position, -measured from the beginning of the file, -to the position given by offset plus a base -specified by the string whence, as follows: - -

    -
  • "set": base is position 0 (beginning of the file);
  • -
  • "cur": base is current position;
  • -
  • "end": base is end of file;
  • -

-In case of success, function seek returns the final file position, -measured in bytes from the beginning of the file. -If this function fails, it returns nil, -plus a string describing the error. - - -

-The default value for whence is "cur", -and for offset is 0. -Therefore, the call file:seek() returns the current -file position, without changing it; -the call file:seek("set") sets the position to the -beginning of the file (and returns 0); -and the call file:seek("end") sets the position to the -end of the file, and returns its size. - - - - -

-


file:setvbuf (mode [, size])

- - -

-Sets the buffering mode for an output file. -There are three available modes: - -

    - -
  • "no": -no buffering; the result of any output operation appears immediately. -
  • - -
  • "full": -full buffering; output operation is performed only -when the buffer is full (or when you explicitly flush the file -(see io.flush)). -
  • - -
  • "line": -line buffering; output is buffered until a newline is output -or there is any input from some special files -(such as a terminal device). -
  • - -

-For the last two cases, size -specifies the size of the buffer, in bytes. -The default is an appropriate size. - - - - -

-


file:write (···)

- - -

-Writes the value of each of its arguments to -the file. -The arguments must be strings or numbers. -To write other values, -use tostring or string.format before write. - - - - - - - -

5.8 - Operating System Facilities

- -

-This library is implemented through table os. - - -

-


os.clock ()

- - -

-Returns an approximation of the amount in seconds of CPU time -used by the program. - - - - -

-


os.date ([format [, time]])

- - -

-Returns a string or a table containing date and time, -formatted according to the given string format. - - -

-If the time argument is present, -this is the time to be formatted -(see the os.time function for a description of this value). -Otherwise, date formats the current time. - - -

-If format starts with '!', -then the date is formatted in Coordinated Universal Time. -After this optional character, -if format is the string "*t", -then date returns a table with the following fields: -year (four digits), month (1--12), day (1--31), -hour (0--23), min (0--59), sec (0--61), -wday (weekday, Sunday is 1), -yday (day of the year), -and isdst (daylight saving flag, a boolean). - - -

-If format is not "*t", -then date returns the date as a string, -formatted according to the same rules as the C function strftime. - - -

-When called without arguments, -date returns a reasonable date and time representation that depends on -the host system and on the current locale -(that is, os.date() is equivalent to os.date("%c")). - - - - -

-


os.difftime (t2, t1)

- - -

-Returns the number of seconds from time t1 to time t2. -In POSIX, Windows, and some other systems, -this value is exactly t2-t1. - - - - -

-


os.execute ([command])

- - -

-This function is equivalent to the C function system. -It passes command to be executed by an operating system shell. -It returns a status code, which is system-dependent. -If command is absent, then it returns nonzero if a shell is available -and zero otherwise. - - - - -

-


os.exit ([code])

- - -

-Calls the C function exit, -with an optional code, -to terminate the host program. -The default value for code is the success code. - - - - -

-


os.getenv (varname)

- - -

-Returns the value of the process environment variable varname, -or nil if the variable is not defined. - - - - -

-


os.remove (filename)

- - -

-Deletes the file or directory with the given name. -Directories must be empty to be removed. -If this function fails, it returns nil, -plus a string describing the error. - - - - -

-


os.rename (oldname, newname)

- - -

-Renames file or directory named oldname to newname. -If this function fails, it returns nil, -plus a string describing the error. - - - - -

-


os.setlocale (locale [, category])

- - -

-Sets the current locale of the program. -locale is a string specifying a locale; -category is an optional string describing which category to change: -"all", "collate", "ctype", -"monetary", "numeric", or "time"; -the default category is "all". -The function returns the name of the new locale, -or nil if the request cannot be honored. - - -

-If locale is the empty string, -the current locale is set to an implementation-defined native locale. -If locale is the string "C", -the current locale is set to the standard C locale. - - -

-When called with nil as the first argument, -this function only returns the name of the current locale -for the given category. - - - - -

-


os.time ([table])

- - -

-Returns the current time when called without arguments, -or a time representing the date and time specified by the given table. -This table must have fields year, month, and day, -and may have fields hour, min, sec, and isdst -(for a description of these fields, see the os.date function). - - -

-The returned value is a number, whose meaning depends on your system. -In POSIX, Windows, and some other systems, this number counts the number -of seconds since some given start time (the "epoch"). -In other systems, the meaning is not specified, -and the number returned by time can be used only as an argument to -date and difftime. - - - - -

-


os.tmpname ()

- - -

-Returns a string with a file name that can -be used for a temporary file. -The file must be explicitly opened before its use -and explicitly removed when no longer needed. - - -

-On some systems (POSIX), -this function also creates a file with that name, -to avoid security risks. -(Someone else might create the file with wrong permissions -in the time between getting the name and creating the file.) -You still have to open the file to use it -and to remove it (even if you do not use it). - - -

-When possible, -you may prefer to use io.tmpfile, -which automatically removes the file when the program ends. - - - - - - - -

5.9 - The Debug Library

- -

-This library provides -the functionality of the debug interface to Lua programs. -You should exert care when using this library. -The functions provided here should be used exclusively for debugging -and similar tasks, such as profiling. -Please resist the temptation to use them as a -usual programming tool: -they can be very slow. -Moreover, several of these functions -violate some assumptions about Lua code -(e.g., that variables local to a function -cannot be accessed from outside or -that userdata metatables cannot be changed by Lua code) -and therefore can compromise otherwise secure code. - - -

-All functions in this library are provided -inside the debug table. -All functions that operate over a thread -have an optional first argument which is the -thread to operate over. -The default is always the current thread. - - -

-


debug.debug ()

- - -

-Enters an interactive mode with the user, -running each string that the user enters. -Using simple commands and other debug facilities, -the user can inspect global and local variables, -change their values, evaluate expressions, and so on. -A line containing only the word cont finishes this function, -so that the caller continues its execution. - - -

-Note that commands for debug.debug are not lexically nested -within any function, and so have no direct access to local variables. - - - - -

-


debug.getfenv (o)

-Returns the environment of object o. - - - - -

-


debug.gethook ([thread])

- - -

-Returns the current hook settings of the thread, as three values: -the current hook function, the current hook mask, -and the current hook count -(as set by the debug.sethook function). - - - - -

-


debug.getinfo ([thread,] function [, what])

- - -

-Returns a table with information about a function. -You can give the function directly, -or you can give a number as the value of function, -which means the function running at level function of the call stack -of the given thread: -level 0 is the current function (getinfo itself); -level 1 is the function that called getinfo; -and so on. -If function is a number larger than the number of active functions, -then getinfo returns nil. - - -

-The returned table can contain all the fields returned by lua_getinfo, -with the string what describing which fields to fill in. -The default for what is to get all information available, -except the table of valid lines. -If present, -the option 'f' -adds a field named func with the function itself. -If present, -the option 'L' -adds a field named activelines with the table of -valid lines. - - -

-For instance, the expression debug.getinfo(1,"n").name returns -a table with a name for the current function, -if a reasonable name can be found, -and the expression debug.getinfo(print) -returns a table with all available information -about the print function. - - - - -

-


debug.getlocal ([thread,] level, local)

- - -

-This function returns the name and the value of the local variable -with index local of the function at level level of the stack. -(The first parameter or local variable has index 1, and so on, -until the last active local variable.) -The function returns nil if there is no local -variable with the given index, -and raises an error when called with a level out of range. -(You can call debug.getinfo to check whether the level is valid.) - - -

-Variable names starting with '(' (open parentheses) -represent internal variables -(loop control variables, temporaries, and C function locals). - - - - -

-


debug.getmetatable (object)

- - -

-Returns the metatable of the given object -or nil if it does not have a metatable. - - - - -

-


debug.getregistry ()

- - -

-Returns the registry table (see §3.5). - - - - -

-


debug.getupvalue (func, up)

- - -

-This function returns the name and the value of the upvalue -with index up of the function func. -The function returns nil if there is no upvalue with the given index. - - - - -

-


debug.setfenv (object, table)

- - -

-Sets the environment of the given object to the given table. -Returns object. - - - - -

-


debug.sethook ([thread,] hook, mask [, count])

- - -

-Sets the given function as a hook. -The string mask and the number count describe -when the hook will be called. -The string mask may have the following characters, -with the given meaning: - -

    -
  • "c": the hook is called every time Lua calls a function;
  • -
  • "r": the hook is called every time Lua returns from a function;
  • -
  • "l": the hook is called every time Lua enters a new line of code.
  • -

-With a count different from zero, -the hook is called after every count instructions. - - -

-When called without arguments, -debug.sethook turns off the hook. - - -

-When the hook is called, its first parameter is a string -describing the event that has triggered its call: -"call", "return" (or "tail return", -when simulating a return from a tail call), -"line", and "count". -For line events, -the hook also gets the new line number as its second parameter. -Inside a hook, -you can call getinfo with level 2 to get more information about -the running function -(level 0 is the getinfo function, -and level 1 is the hook function), -unless the event is "tail return". -In this case, Lua is only simulating the return, -and a call to getinfo will return invalid data. - - - - -

-


debug.setlocal ([thread,] level, local, value)

- - -

-This function assigns the value value to the local variable -with index local of the function at level level of the stack. -The function returns nil if there is no local -variable with the given index, -and raises an error when called with a level out of range. -(You can call getinfo to check whether the level is valid.) -Otherwise, it returns the name of the local variable. - - - - -

-


debug.setmetatable (object, table)

- - -

-Sets the metatable for the given object to the given table -(which can be nil). - - - - -

-


debug.setupvalue (func, up, value)

- - -

-This function assigns the value value to the upvalue -with index up of the function func. -The function returns nil if there is no upvalue -with the given index. -Otherwise, it returns the name of the upvalue. - - - - -

-


debug.traceback ([thread,] [message [, level]])

- - -

-Returns a string with a traceback of the call stack. -An optional message string is appended -at the beginning of the traceback. -An optional level number tells at which level -to start the traceback -(default is 1, the function calling traceback). - - - - - - - -

6 - Lua Stand-alone

- -

-Although Lua has been designed as an extension language, -to be embedded in a host C program, -it is also frequently used as a stand-alone language. -An interpreter for Lua as a stand-alone language, -called simply lua, -is provided with the standard distribution. -The stand-alone interpreter includes -all standard libraries, including the debug library. -Its usage is: - -

-     lua [options] [script [args]]
-

-The options are: - -

    -
  • -e stat: executes string stat;
  • -
  • -l mod: "requires" mod;
  • -
  • -i: enters interactive mode after running script;
  • -
  • -v: prints version information;
  • -
  • --: stops handling options;
  • -
  • -: executes stdin as a file and stops handling options.
  • -

-After handling its options, lua runs the given script, -passing to it the given args as string arguments. -When called without arguments, -lua behaves as lua -v -i -when the standard input (stdin) is a terminal, -and as lua - otherwise. - - -

-Before running any argument, -the interpreter checks for an environment variable LUA_INIT. -If its format is @filename, -then lua executes the file. -Otherwise, lua executes the string itself. - - -

-All options are handled in order, except -i. -For instance, an invocation like - -

-     $ lua -e'a=1' -e 'print(a)' script.lua
-

-will first set a to 1, then print the value of a (which is '1'), -and finally run the file script.lua with no arguments. -(Here $ is the shell prompt. Your prompt may be different.) - - -

-Before starting to run the script, -lua collects all arguments in the command line -in a global table called arg. -The script name is stored at index 0, -the first argument after the script name goes to index 1, -and so on. -Any arguments before the script name -(that is, the interpreter name plus the options) -go to negative indices. -For instance, in the call - -

-     $ lua -la b.lua t1 t2
-

-the interpreter first runs the file a.lua, -then creates a table - -

-     arg = { [-2] = "lua", [-1] = "-la",
-             [0] = "b.lua",
-             [1] = "t1", [2] = "t2" }
-

-and finally runs the file b.lua. -The script is called with arg[1], arg[2], ··· -as arguments; -it can also access these arguments with the vararg expression '...'. - - -

-In interactive mode, -if you write an incomplete statement, -the interpreter waits for its completion -by issuing a different prompt. - - -

-If the global variable _PROMPT contains a string, -then its value is used as the prompt. -Similarly, if the global variable _PROMPT2 contains a string, -its value is used as the secondary prompt -(issued during incomplete statements). -Therefore, both prompts can be changed directly on the command line -or in any Lua programs by assigning to _PROMPT. -See the next example: - -

-     $ lua -e"_PROMPT='myprompt> '" -i
-

-(The outer pair of quotes is for the shell, -the inner pair is for Lua.) -Note the use of -i to enter interactive mode; -otherwise, -the program would just end silently -right after the assignment to _PROMPT. - - -

-To allow the use of Lua as a -script interpreter in Unix systems, -the stand-alone interpreter skips -the first line of a chunk if it starts with #. -Therefore, Lua scripts can be made into executable programs -by using chmod +x and the #! form, -as in - -

-     #!/usr/local/bin/lua
-

-(Of course, -the location of the Lua interpreter may be different in your machine. -If lua is in your PATH, -then - -

-     #!/usr/bin/env lua
-

-is a more portable solution.) - - - -

7 - Incompatibilities with the Previous Version

- -

-Here we list the incompatibilities that you may find when moving a program -from Lua 5.0 to Lua 5.1. -You can avoid most of the incompatibilities compiling Lua with -appropriate options (see file luaconf.h). -However, -all these compatibility options will be removed in the next version of Lua. - - - -

7.1 - Changes in the Language

-
    - -
  • -The vararg system changed from the pseudo-argument arg with a -table with the extra arguments to the vararg expression. -(See compile-time option LUA_COMPAT_VARARG in luaconf.h.) -
  • - -
  • -There was a subtle change in the scope of the implicit -variables of the for statement and for the repeat statement. -
  • - -
  • -The long string/long comment syntax ([[string]]) -does not allow nesting. -You can use the new syntax ([=[string]=]) in these cases. -(See compile-time option LUA_COMPAT_LSTR in luaconf.h.) -
  • - -
- - - - -

7.2 - Changes in the Libraries

-
    - -
  • -Function string.gfind was renamed string.gmatch. -(See compile-time option LUA_COMPAT_GFIND in luaconf.h.) -
  • - -
  • -When string.gsub is called with a function as its -third argument, -whenever this function returns nil or false the -replacement string is the whole match, -instead of the empty string. -
  • - -
  • -Function table.setn was deprecated. -Function table.getn corresponds -to the new length operator (#); -use the operator instead of the function. -(See compile-time option LUA_COMPAT_GETN in luaconf.h.) -
  • - -
  • -Function loadlib was renamed package.loadlib. -(See compile-time option LUA_COMPAT_LOADLIB in luaconf.h.) -
  • - -
  • -Function math.mod was renamed math.fmod. -(See compile-time option LUA_COMPAT_MOD in luaconf.h.) -
  • - -
  • -Functions table.foreach and table.foreachi are deprecated. -You can use a for loop with pairs or ipairs instead. -
  • - -
  • -There were substantial changes in function require due to -the new module system. -However, the new behavior is mostly compatible with the old, -but require gets the path from package.path instead -of from LUA_PATH. -
  • - -
  • -Function collectgarbage has different arguments. -Function gcinfo is deprecated; -use collectgarbage("count") instead. -
  • - -
- - - - -

7.3 - Changes in the API

-
    - -
  • -The luaopen_* functions (to open libraries) -cannot be called directly, -like a regular C function. -They must be called through Lua, -like a Lua function. -
  • - -
  • -Function lua_open was replaced by lua_newstate to -allow the user to set a memory-allocation function. -You can use luaL_newstate from the standard library to -create a state with a standard allocation function -(based on realloc). -
  • - -
  • -Functions luaL_getn and luaL_setn -(from the auxiliary library) are deprecated. -Use lua_objlen instead of luaL_getn -and nothing instead of luaL_setn. -
  • - -
  • -Function luaL_openlib was replaced by luaL_register. -
  • - -
  • -Function luaL_checkudata now throws an error when the given value -is not a userdata of the expected type. -(In Lua 5.0 it returned NULL.) -
  • - -
- - - - -

8 - The Complete Syntax of Lua

- -

-Here is the complete syntax of Lua in extended BNF. -(It does not describe operator precedences.) - - - - -

-
-	chunk ::= {stat [`;´]} [laststat [`;´]]
-
-	block ::= chunk
-
-	stat ::=  varlist `=´ explist | 
-		 functioncall | 
-		 do block end | 
-		 while exp do block end | 
-		 repeat block until exp | 
-		 if exp then block {elseif exp then block} [else block] end | 
-		 for Name `=´ exp `,´ exp [`,´ exp] do block end | 
-		 for namelist in explist do block end | 
-		 function funcname funcbody | 
-		 local function Name funcbody | 
-		 local namelist [`=´ explist] 
-
-	laststat ::= return [explist] | break
-
-	funcname ::= Name {`.´ Name} [`:´ Name]
-
-	varlist ::= var {`,´ var}
-
-	var ::=  Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name 
-
-	namelist ::= Name {`,´ Name}
-
-	explist ::= {exp `,´} exp
-
-	exp ::=  nil | false | true | Number | String | `...´ | function | 
-		 prefixexp | tableconstructor | exp binop exp | unop exp 
-
-	prefixexp ::= var | functioncall | `(´ exp `)´
-
-	functioncall ::=  prefixexp args | prefixexp `:´ Name args 
-
-	args ::=  `(´ [explist] `)´ | tableconstructor | String 
-
-	function ::= function funcbody
-
-	funcbody ::= `(´ [parlist] `)´ block end
-
-	parlist ::= namelist [`,´ `...´] | `...´
-
-	tableconstructor ::= `{´ [fieldlist] `}´
-
-	fieldlist ::= field {fieldsep field} [fieldsep]
-
-	field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp
-
-	fieldsep ::= `,´ | `;´
-
-	binop ::= `+´ | `-´ | `*´ | `/´ | `^´ | `%´ | `..´ | 
-		 `<´ | `<=´ | `>´ | `>=´ | `==´ | `~=´ | 
-		 and | or
-
-	unop ::= `-´ | not | `#´
-
-
- -

- - - - - - - -


- -Last update: -Mon Feb 13 18:54:19 BRST 2012 - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/readme.html b/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/readme.html deleted file mode 100644 index 3ed6a81..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/doc/readme.html +++ /dev/null @@ -1,40 +0,0 @@ - - -Lua documentation - - - - - -
-

-Lua -Documentation -

- -This is the documentation included in the source distribution of Lua 5.1.5. - - - -Lua's -official web site -contains updated documentation, -especially the -reference manual. -

- -


- -Last update: -Fri Feb 3 09:44:42 BRST 2012 - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/README b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/README deleted file mode 100644 index 5149fc9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/README +++ /dev/null @@ -1,37 +0,0 @@ -This directory contains some useful files and code. -Unlike the code in ../src, everything here is in the public domain. - -If any of the makes fail, you're probably not using the same libraries -used to build Lua. Set MYLIBS in Makefile accordingly. - -all.c - Full Lua interpreter in a single file. - Do "make one" for a demo. - -lua.hpp - Lua header files for C++ using 'extern "C"'. - -lua.ico - A Lua icon for Windows (and web sites: save as favicon.ico). - Drawn by hand by Markus Gritsch . - -lua.pc - pkg-config data for Lua - -luavs.bat - Script to build Lua under "Visual Studio .NET Command Prompt". - Run it from the toplevel as etc\luavs.bat. - -min.c - A minimal Lua interpreter. - Good for learning and for starting your own. - Do "make min" for a demo. - -noparser.c - Linking with noparser.o avoids loading the parsing modules in lualib.a. - Do "make noparser" for a demo. - -strict.lua - Traps uses of undeclared global variables. - Do "make strict" for a demo. - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/all.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/all.c deleted file mode 100644 index dab68fa..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/all.c +++ /dev/null @@ -1,38 +0,0 @@ -/* -* all.c -- Lua core, libraries and interpreter in a single file -*/ - -#define luaall_c - -#include "lapi.c" -#include "lcode.c" -#include "ldebug.c" -#include "ldo.c" -#include "ldump.c" -#include "lfunc.c" -#include "lgc.c" -#include "llex.c" -#include "lmem.c" -#include "lobject.c" -#include "lopcodes.c" -#include "lparser.c" -#include "lstate.c" -#include "lstring.c" -#include "ltable.c" -#include "ltm.c" -#include "lundump.c" -#include "lvm.c" -#include "lzio.c" - -#include "lauxlib.c" -#include "lbaselib.c" -#include "ldblib.c" -#include "liolib.c" -#include "linit.c" -#include "lmathlib.c" -#include "loadlib.c" -#include "loslib.c" -#include "lstrlib.c" -#include "ltablib.c" - -#include "lua.c" diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.hpp b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.hpp deleted file mode 100644 index ec417f5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// lua.hpp -// Lua header files for C++ -// <> not supplied automatically because Lua also compiles as C++ - -extern "C" { -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.ico b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.ico deleted file mode 100644 index ccbabc4e2004683f29598a991006d7caff6d837d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1078 zcma)5y>7xl4E|D3VJbX9VX7GW1~6FSw!BIQq_T0tNo32b^bxZ4H5fZGR7xh?&v!Y3 zDh3=J`#b+#Yy%W{!g4u>(a#g`Mme7+yefc~5wPOflDr`o81qe{?|t$BfABsDzNw;V z8cH*0{6W<;G9Np#7ik(qoR4aR5-A@{5)}DJ9&}FRBA#X_5+im4-kQSzMF^)-t2(Vi ztw-^|Sn8@O_lM9`oos+0wMZGt&`Bq(aK&XCv1Gfr&Jtd6%lKPdD{s=unqGWyb3%y{X9SS{jB~HMh0oKMISQrDC zJ;K?)>ElnpmN^UNE-rXxtyk{c#rCe~`P=qnFT7 bCxwx*w%~s~=?o*z_6Fk4@7l(poWF`cPpA(! diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.pc b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.pc deleted file mode 100644 index 07e2852..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/lua.pc +++ /dev/null @@ -1,31 +0,0 @@ -# lua.pc -- pkg-config data for Lua - -# vars from install Makefile - -# grep '^V=' ../Makefile -V= 5.1 -# grep '^R=' ../Makefile -R= 5.1.5 - -# grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/' -prefix= /usr/local -INSTALL_BIN= ${prefix}/bin -INSTALL_INC= ${prefix}/include -INSTALL_LIB= ${prefix}/lib -INSTALL_MAN= ${prefix}/man/man1 -INSTALL_LMOD= ${prefix}/share/lua/${V} -INSTALL_CMOD= ${prefix}/lib/lua/${V} - -# canonical vars -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -includedir=${prefix}/include - -Name: Lua -Description: An Extensible Extension Language -Version: ${R} -Requires: -Libs: -L${libdir} -llua -lm -Cflags: -I${includedir} - -# (end of lua.pc) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/luavs.bat b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/luavs.bat deleted file mode 100644 index 054b462..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/luavs.bat +++ /dev/null @@ -1,28 +0,0 @@ -@rem Script to build Lua under "Visual Studio .NET Command Prompt". -@rem Do not run from this directory; run it from the toplevel: etc\luavs.bat . -@rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src. -@rem (contributed by David Manura and Mike Pall) - -@setlocal -@set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE -@set MYLINK=link /nologo -@set MYMT=mt /nologo - -cd src -%MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c -del lua.obj luac.obj -%MYLINK% /DLL /out:lua51.dll l*.obj -if exist lua51.dll.manifest^ - %MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2 -%MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c -%MYLINK% /out:lua.exe lua.obj lua51.lib -if exist lua.exe.manifest^ - %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe -%MYCOMPILE% l*.c print.c -del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^ - loslib.obj ltablib.obj lstrlib.obj loadlib.obj -%MYLINK% /out:luac.exe *.obj -if exist luac.exe.manifest^ - %MYMT% -manifest luac.exe.manifest -outputresource:luac.exe -del *.obj *.manifest -cd .. diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/min.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/min.c deleted file mode 100644 index 6a85a4d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/min.c +++ /dev/null @@ -1,39 +0,0 @@ -/* -* min.c -- a minimal Lua interpreter -* loads stdin only with minimal error handling. -* no interaction, and no standard library, only a "print" function. -*/ - -#include - -#include "lua.h" -#include "lauxlib.h" - -static int print(lua_State *L) -{ - int n=lua_gettop(L); - int i; - for (i=1; i<=n; i++) - { - if (i>1) printf("\t"); - if (lua_isstring(L,i)) - printf("%s",lua_tostring(L,i)); - else if (lua_isnil(L,i)) - printf("%s","nil"); - else if (lua_isboolean(L,i)) - printf("%s",lua_toboolean(L,i) ? "true" : "false"); - else - printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i)); - } - printf("\n"); - return 0; -} - -int main(void) -{ - lua_State *L=lua_open(); - lua_register(L,"print",print); - if (luaL_dofile(L,NULL)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1)); - lua_close(L); - return 0; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/noparser.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/noparser.c deleted file mode 100644 index 13ba546..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/noparser.c +++ /dev/null @@ -1,50 +0,0 @@ -/* -* The code below can be used to make a Lua core that does not contain the -* parsing modules (lcode, llex, lparser), which represent 35% of the total core. -* You'll only be able to load binary files and strings, precompiled with luac. -* (Of course, you'll have to build luac with the original parsing modules!) -* -* To use this module, simply compile it ("make noparser" does that) and list -* its object file before the Lua libraries. The linker should then not load -* the parsing modules. To try it, do "make luab". -* -* If you also want to avoid the dump module (ldump.o), define NODUMP. -* #define NODUMP -*/ - -#define LUA_CORE - -#include "llex.h" -#include "lparser.h" -#include "lzio.h" - -LUAI_FUNC void luaX_init (lua_State *L) { - UNUSED(L); -} - -LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { - UNUSED(z); - UNUSED(buff); - UNUSED(name); - lua_pushliteral(L,"parser not loaded"); - lua_error(L); - return NULL; -} - -#ifdef NODUMP -#include "lundump.h" - -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) { - UNUSED(f); - UNUSED(w); - UNUSED(data); - UNUSED(strip); -#if 1 - UNUSED(L); - return 0; -#else - lua_pushliteral(L,"dumper not loaded"); - lua_error(L); -#endif -} -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/strict.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/strict.lua deleted file mode 100644 index 604619d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/etc/strict.lua +++ /dev/null @@ -1,41 +0,0 @@ --- --- strict.lua --- checks uses of undeclared global variables --- All global variables must be 'declared' through a regular assignment --- (even assigning nil will do) in a main chunk before being used --- anywhere or assigned to inside a function. --- - -local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget - -local mt = getmetatable(_G) -if mt == nil then - mt = {} - setmetatable(_G, mt) -end - -mt.__declared = {} - -local function what () - local d = getinfo(3, "S") - return d and d.what or "C" -end - -mt.__newindex = function (t, n, v) - if not mt.__declared[n] then - local w = what() - if w ~= "main" and w ~= "C" then - error("assign to undeclared variable '"..n.."'", 2) - end - mt.__declared[n] = true - end - rawset(t, n, v) -end - -mt.__index = function (t, n) - if not mt.__declared[n] and what() ~= "C" then - error("variable '"..n.."' is not declared", 2) - end - return rawget(t, n) -end - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.c deleted file mode 100644 index 5d5145d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.c +++ /dev/null @@ -1,1087 +0,0 @@ -/* -** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ -** Lua API -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include - -#define lapi_c -#define LUA_CORE - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" - - - -const char lua_ident[] = - "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" - "$Authors: " LUA_AUTHORS " $\n" - "$URL: www.lua.org $\n"; - - - -#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) - -#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) - -#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} - - - -static TValue *index2adr (lua_State *L, int idx) { - if (idx > 0) { - TValue *o = L->base + (idx - 1); - api_check(L, idx <= L->ci->top - L->base); - if (o >= L->top) return cast(TValue *, luaO_nilobject); - else return o; - } - else if (idx > LUA_REGISTRYINDEX) { - api_check(L, idx != 0 && -idx <= L->top - L->base); - return L->top + idx; - } - else switch (idx) { /* pseudo-indices */ - case LUA_REGISTRYINDEX: return registry(L); - case LUA_ENVIRONINDEX: { - Closure *func = curr_func(L); - sethvalue(L, &L->env, func->c.env); - return &L->env; - } - case LUA_GLOBALSINDEX: return gt(L); - default: { - Closure *func = curr_func(L); - idx = LUA_GLOBALSINDEX - idx; - return (idx <= func->c.nupvalues) - ? &func->c.upvalue[idx-1] - : cast(TValue *, luaO_nilobject); - } - } -} - - -static Table *getcurrenv (lua_State *L) { - if (L->ci == L->base_ci) /* no enclosing function? */ - return hvalue(gt(L)); /* use global table as environment */ - else { - Closure *func = curr_func(L); - return func->c.env; - } -} - - -void luaA_pushobject (lua_State *L, const TValue *o) { - setobj2s(L, L->top, o); - api_incr_top(L); -} - - -LUA_API int lua_checkstack (lua_State *L, int size) { - int res = 1; - lua_lock(L); - if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) - res = 0; /* stack overflow */ - else if (size > 0) { - luaD_checkstack(L, size); - if (L->ci->top < L->top + size) - L->ci->top = L->top + size; - } - lua_unlock(L); - return res; -} - - -LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { - int i; - if (from == to) return; - lua_lock(to); - api_checknelems(from, n); - api_check(from, G(from) == G(to)); - api_check(from, to->ci->top - to->top >= n); - from->top -= n; - for (i = 0; i < n; i++) { - setobj2s(to, to->top++, from->top + i); - } - lua_unlock(to); -} - - -LUA_API void lua_setlevel (lua_State *from, lua_State *to) { - to->nCcalls = from->nCcalls; -} - - -LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { - lua_CFunction old; - lua_lock(L); - old = G(L)->panic; - G(L)->panic = panicf; - lua_unlock(L); - return old; -} - - -LUA_API lua_State *lua_newthread (lua_State *L) { - lua_State *L1; - lua_lock(L); - luaC_checkGC(L); - L1 = luaE_newthread(L); - setthvalue(L, L->top, L1); - api_incr_top(L); - lua_unlock(L); - luai_userstatethread(L, L1); - return L1; -} - - - -/* -** basic stack manipulation -*/ - - -LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - L->base); -} - - -LUA_API void lua_settop (lua_State *L, int idx) { - lua_lock(L); - if (idx >= 0) { - api_check(L, idx <= L->stack_last - L->base); - while (L->top < L->base + idx) - setnilvalue(L->top++); - L->top = L->base + idx; - } - else { - api_check(L, -(idx+1) <= (L->top - L->base)); - L->top += idx+1; /* `subtract' index (index is negative) */ - } - lua_unlock(L); -} - - -LUA_API void lua_remove (lua_State *L, int idx) { - StkId p; - lua_lock(L); - p = index2adr(L, idx); - api_checkvalidindex(L, p); - while (++p < L->top) setobjs2s(L, p-1, p); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_insert (lua_State *L, int idx) { - StkId p; - StkId q; - lua_lock(L); - p = index2adr(L, idx); - api_checkvalidindex(L, p); - for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); - setobjs2s(L, p, L->top); - lua_unlock(L); -} - - -LUA_API void lua_replace (lua_State *L, int idx) { - StkId o; - lua_lock(L); - /* explicit test for incompatible code */ - if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci) - luaG_runerror(L, "no calling environment"); - api_checknelems(L, 1); - o = index2adr(L, idx); - api_checkvalidindex(L, o); - if (idx == LUA_ENVIRONINDEX) { - Closure *func = curr_func(L); - api_check(L, ttistable(L->top - 1)); - func->c.env = hvalue(L->top - 1); - luaC_barrier(L, func, L->top - 1); - } - else { - setobj(L, o, L->top - 1); - if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ - luaC_barrier(L, curr_func(L), L->top - 1); - } - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_pushvalue (lua_State *L, int idx) { - lua_lock(L); - setobj2s(L, L->top, index2adr(L, idx)); - api_incr_top(L); - lua_unlock(L); -} - - - -/* -** access functions (stack -> C) -*/ - - -LUA_API int lua_type (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); -} - - -LUA_API const char *lua_typename (lua_State *L, int t) { - UNUSED(L); - return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; -} - - -LUA_API int lua_iscfunction (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - return iscfunction(o); -} - - -LUA_API int lua_isnumber (lua_State *L, int idx) { - TValue n; - const TValue *o = index2adr(L, idx); - return tonumber(o, &n); -} - - -LUA_API int lua_isstring (lua_State *L, int idx) { - int t = lua_type(L, idx); - return (t == LUA_TSTRING || t == LUA_TNUMBER); -} - - -LUA_API int lua_isuserdata (lua_State *L, int idx) { - const TValue *o = index2adr(L, idx); - return (ttisuserdata(o) || ttislightuserdata(o)); -} - - -LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { - StkId o1 = index2adr(L, index1); - StkId o2 = index2adr(L, index2); - return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 - : luaO_rawequalObj(o1, o2); -} - - -LUA_API int lua_equal (lua_State *L, int index1, int index2) { - StkId o1, o2; - int i; - lua_lock(L); /* may call tag method */ - o1 = index2adr(L, index1); - o2 = index2adr(L, index2); - i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); - lua_unlock(L); - return i; -} - - -LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { - StkId o1, o2; - int i; - lua_lock(L); /* may call tag method */ - o1 = index2adr(L, index1); - o2 = index2adr(L, index2); - i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 - : luaV_lessthan(L, o1, o2); - lua_unlock(L); - return i; -} - - - -LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { - TValue n; - const TValue *o = index2adr(L, idx); - if (tonumber(o, &n)) - return nvalue(o); - else - return 0; -} - - -LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { - TValue n; - const TValue *o = index2adr(L, idx); - if (tonumber(o, &n)) { - lua_Integer res; - lua_Number num = nvalue(o); - lua_number2integer(res, num); - return res; - } - else - return 0; -} - - -LUA_API int lua_toboolean (lua_State *L, int idx) { - const TValue *o = index2adr(L, idx); - return !l_isfalse(o); -} - - -LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - StkId o = index2adr(L, idx); - if (!ttisstring(o)) { - lua_lock(L); /* `luaV_tostring' may create a new string */ - if (!luaV_tostring(L, o)) { /* conversion failed? */ - if (len != NULL) *len = 0; - lua_unlock(L); - return NULL; - } - luaC_checkGC(L); - o = index2adr(L, idx); /* previous call may reallocate the stack */ - lua_unlock(L); - } - if (len != NULL) *len = tsvalue(o)->len; - return svalue(o); -} - - -LUA_API size_t lua_objlen (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - switch (ttype(o)) { - case LUA_TSTRING: return tsvalue(o)->len; - case LUA_TUSERDATA: return uvalue(o)->len; - case LUA_TTABLE: return luaH_getn(hvalue(o)); - case LUA_TNUMBER: { - size_t l; - lua_lock(L); /* `luaV_tostring' may create a new string */ - l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0); - lua_unlock(L); - return l; - } - default: return 0; - } -} - - -LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; -} - - -LUA_API void *lua_touserdata (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - switch (ttype(o)) { - case LUA_TUSERDATA: return (rawuvalue(o) + 1); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; - } -} - - -LUA_API lua_State *lua_tothread (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - return (!ttisthread(o)) ? NULL : thvalue(o); -} - - -LUA_API const void *lua_topointer (lua_State *L, int idx) { - StkId o = index2adr(L, idx); - switch (ttype(o)) { - case LUA_TTABLE: return hvalue(o); - case LUA_TFUNCTION: return clvalue(o); - case LUA_TTHREAD: return thvalue(o); - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - return lua_touserdata(L, idx); - default: return NULL; - } -} - - - -/* -** push functions (C -> stack) -*/ - - -LUA_API void lua_pushnil (lua_State *L) { - lua_lock(L); - setnilvalue(L->top); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { - lua_lock(L); - setnvalue(L->top, n); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { - lua_lock(L); - setnvalue(L->top, cast_num(n)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { - lua_lock(L); - luaC_checkGC(L); - setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushstring (lua_State *L, const char *s) { - if (s == NULL) - lua_pushnil(L); - else - lua_pushlstring(L, s, strlen(s)); -} - - -LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, - va_list argp) { - const char *ret; - lua_lock(L); - luaC_checkGC(L); - ret = luaO_pushvfstring(L, fmt, argp); - lua_unlock(L); - return ret; -} - - -LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { - const char *ret; - va_list argp; - lua_lock(L); - luaC_checkGC(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - lua_unlock(L); - return ret; -} - - -LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { - Closure *cl; - lua_lock(L); - luaC_checkGC(L); - api_checknelems(L, n); - cl = luaF_newCclosure(L, n, getcurrenv(L)); - cl->c.f = fn; - L->top -= n; - while (n--) - setobj2n(L, &cl->c.upvalue[n], L->top+n); - setclvalue(L, L->top, cl); - lua_assert(iswhite(obj2gco(cl))); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushboolean (lua_State *L, int b) { - lua_lock(L); - setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { - lua_lock(L); - setpvalue(L->top, p); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API int lua_pushthread (lua_State *L) { - lua_lock(L); - setthvalue(L, L->top, L); - api_incr_top(L); - lua_unlock(L); - return (G(L)->mainthread == L); -} - - - -/* -** get functions (Lua -> stack) -*/ - - -LUA_API void lua_gettable (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2adr(L, idx); - api_checkvalidindex(L, t); - luaV_gettable(L, t, L->top - 1, L->top - 1); - lua_unlock(L); -} - - -LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { - StkId t; - TValue key; - lua_lock(L); - t = index2adr(L, idx); - api_checkvalidindex(L, t); - setsvalue(L, &key, luaS_new(L, k)); - luaV_gettable(L, t, &key, L->top); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_rawget (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2adr(L, idx); - api_check(L, ttistable(t)); - setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); - lua_unlock(L); -} - - -LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { - StkId o; - lua_lock(L); - o = index2adr(L, idx); - api_check(L, ttistable(o)); - setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { - lua_lock(L); - luaC_checkGC(L); - sethvalue(L, L->top, luaH_new(L, narray, nrec)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API int lua_getmetatable (lua_State *L, int objindex) { - const TValue *obj; - Table *mt = NULL; - int res; - lua_lock(L); - obj = index2adr(L, objindex); - switch (ttype(obj)) { - case LUA_TTABLE: - mt = hvalue(obj)->metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(obj)->metatable; - break; - default: - mt = G(L)->mt[ttype(obj)]; - break; - } - if (mt == NULL) - res = 0; - else { - sethvalue(L, L->top, mt); - api_incr_top(L); - res = 1; - } - lua_unlock(L); - return res; -} - - -LUA_API void lua_getfenv (lua_State *L, int idx) { - StkId o; - lua_lock(L); - o = index2adr(L, idx); - api_checkvalidindex(L, o); - switch (ttype(o)) { - case LUA_TFUNCTION: - sethvalue(L, L->top, clvalue(o)->c.env); - break; - case LUA_TUSERDATA: - sethvalue(L, L->top, uvalue(o)->env); - break; - case LUA_TTHREAD: - setobj2s(L, L->top, gt(thvalue(o))); - break; - default: - setnilvalue(L->top); - break; - } - api_incr_top(L); - lua_unlock(L); -} - - -/* -** set functions (stack -> Lua) -*/ - - -LUA_API void lua_settable (lua_State *L, int idx) { - StkId t; - lua_lock(L); - api_checknelems(L, 2); - t = index2adr(L, idx); - api_checkvalidindex(L, t); - luaV_settable(L, t, L->top - 2, L->top - 1); - L->top -= 2; /* pop index and value */ - lua_unlock(L); -} - - -LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { - StkId t; - TValue key; - lua_lock(L); - api_checknelems(L, 1); - t = index2adr(L, idx); - api_checkvalidindex(L, t); - setsvalue(L, &key, luaS_new(L, k)); - luaV_settable(L, t, &key, L->top - 1); - L->top--; /* pop value */ - lua_unlock(L); -} - - -LUA_API void lua_rawset (lua_State *L, int idx) { - StkId t; - lua_lock(L); - api_checknelems(L, 2); - t = index2adr(L, idx); - api_check(L, ttistable(t)); - setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); - luaC_barriert(L, hvalue(t), L->top-1); - L->top -= 2; - lua_unlock(L); -} - - -LUA_API void lua_rawseti (lua_State *L, int idx, int n) { - StkId o; - lua_lock(L); - api_checknelems(L, 1); - o = index2adr(L, idx); - api_check(L, ttistable(o)); - setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); - luaC_barriert(L, hvalue(o), L->top-1); - L->top--; - lua_unlock(L); -} - - -LUA_API int lua_setmetatable (lua_State *L, int objindex) { - TValue *obj; - Table *mt; - lua_lock(L); - api_checknelems(L, 1); - obj = index2adr(L, objindex); - api_checkvalidindex(L, obj); - if (ttisnil(L->top - 1)) - mt = NULL; - else { - api_check(L, ttistable(L->top - 1)); - mt = hvalue(L->top - 1); - } - switch (ttype(obj)) { - case LUA_TTABLE: { - hvalue(obj)->metatable = mt; - if (mt) - luaC_objbarriert(L, hvalue(obj), mt); - break; - } - case LUA_TUSERDATA: { - uvalue(obj)->metatable = mt; - if (mt) - luaC_objbarrier(L, rawuvalue(obj), mt); - break; - } - default: { - G(L)->mt[ttype(obj)] = mt; - break; - } - } - L->top--; - lua_unlock(L); - return 1; -} - - -LUA_API int lua_setfenv (lua_State *L, int idx) { - StkId o; - int res = 1; - lua_lock(L); - api_checknelems(L, 1); - o = index2adr(L, idx); - api_checkvalidindex(L, o); - api_check(L, ttistable(L->top - 1)); - switch (ttype(o)) { - case LUA_TFUNCTION: - clvalue(o)->c.env = hvalue(L->top - 1); - break; - case LUA_TUSERDATA: - uvalue(o)->env = hvalue(L->top - 1); - break; - case LUA_TTHREAD: - sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1)); - break; - default: - res = 0; - break; - } - if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); - L->top--; - lua_unlock(L); - return res; -} - - -/* -** `load' and `call' functions (run Lua code) -*/ - - -#define adjustresults(L,nres) \ - { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } - - -#define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) - - -LUA_API void lua_call (lua_State *L, int nargs, int nresults) { - StkId func; - lua_lock(L); - api_checknelems(L, nargs+1); - checkresults(L, nargs, nresults); - func = L->top - (nargs+1); - luaD_call(L, func, nresults); - adjustresults(L, nresults); - lua_unlock(L); -} - - - -/* -** Execute a protected call. -*/ -struct CallS { /* data to `f_call' */ - StkId func; - int nresults; -}; - - -static void f_call (lua_State *L, void *ud) { - struct CallS *c = cast(struct CallS *, ud); - luaD_call(L, c->func, c->nresults); -} - - - -LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { - struct CallS c; - int status; - ptrdiff_t func; - lua_lock(L); - api_checknelems(L, nargs+1); - checkresults(L, nargs, nresults); - if (errfunc == 0) - func = 0; - else { - StkId o = index2adr(L, errfunc); - api_checkvalidindex(L, o); - func = savestack(L, o); - } - c.func = L->top - (nargs+1); /* function to be called */ - c.nresults = nresults; - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); - adjustresults(L, nresults); - lua_unlock(L); - return status; -} - - -/* -** Execute a protected C call. -*/ -struct CCallS { /* data to `f_Ccall' */ - lua_CFunction func; - void *ud; -}; - - -static void f_Ccall (lua_State *L, void *ud) { - struct CCallS *c = cast(struct CCallS *, ud); - Closure *cl; - cl = luaF_newCclosure(L, 0, getcurrenv(L)); - cl->c.f = c->func; - setclvalue(L, L->top, cl); /* push function */ - api_incr_top(L); - setpvalue(L->top, c->ud); /* push only argument */ - api_incr_top(L); - luaD_call(L, L->top - 2, 0); -} - - -LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { - struct CCallS c; - int status; - lua_lock(L); - c.func = func; - c.ud = ud; - status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); - lua_unlock(L); - return status; -} - - -LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, - const char *chunkname) { - ZIO z; - int status; - lua_lock(L); - if (!chunkname) chunkname = "?"; - luaZ_init(L, &z, reader, data); - status = luaD_protectedparser(L, &z, chunkname); - lua_unlock(L); - return status; -} - - -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { - int status; - TValue *o; - lua_lock(L); - api_checknelems(L, 1); - o = L->top - 1; - if (isLfunction(o)) - status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); - else - status = 1; - lua_unlock(L); - return status; -} - - -LUA_API int lua_status (lua_State *L) { - return L->status; -} - - -/* -** Garbage-collection function -*/ - -LUA_API int lua_gc (lua_State *L, int what, int data) { - int res = 0; - global_State *g; - lua_lock(L); - g = G(L); - switch (what) { - case LUA_GCSTOP: { - g->GCthreshold = MAX_LUMEM; - break; - } - case LUA_GCRESTART: { - g->GCthreshold = g->totalbytes; - break; - } - case LUA_GCCOLLECT: { - luaC_fullgc(L); - break; - } - case LUA_GCCOUNT: { - /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(g->totalbytes >> 10); - break; - } - case LUA_GCCOUNTB: { - res = cast_int(g->totalbytes & 0x3ff); - break; - } - case LUA_GCSTEP: { - lu_mem a = (cast(lu_mem, data) << 10); - if (a <= g->totalbytes) - g->GCthreshold = g->totalbytes - a; - else - g->GCthreshold = 0; - while (g->GCthreshold <= g->totalbytes) { - luaC_step(L); - if (g->gcstate == GCSpause) { /* end of cycle? */ - res = 1; /* signal it */ - break; - } - } - break; - } - case LUA_GCSETPAUSE: { - res = g->gcpause; - g->gcpause = data; - break; - } - case LUA_GCSETSTEPMUL: { - res = g->gcstepmul; - g->gcstepmul = data; - break; - } - default: res = -1; /* invalid option */ - } - lua_unlock(L); - return res; -} - - - -/* -** miscellaneous functions -*/ - - -LUA_API int lua_error (lua_State *L) { - lua_lock(L); - api_checknelems(L, 1); - luaG_errormsg(L); - lua_unlock(L); - return 0; /* to avoid warnings */ -} - - -LUA_API int lua_next (lua_State *L, int idx) { - StkId t; - int more; - lua_lock(L); - t = index2adr(L, idx); - api_check(L, ttistable(t)); - more = luaH_next(L, hvalue(t), L->top - 1); - if (more) { - api_incr_top(L); - } - else /* no more elements */ - L->top -= 1; /* remove key */ - lua_unlock(L); - return more; -} - - -LUA_API void lua_concat (lua_State *L, int n) { - lua_lock(L); - api_checknelems(L, n); - if (n >= 2) { - luaC_checkGC(L); - luaV_concat(L, n, cast_int(L->top - L->base) - 1); - L->top -= (n-1); - } - else if (n == 0) { /* push empty string */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); - api_incr_top(L); - } - /* else n == 1; nothing to do */ - lua_unlock(L); -} - - -LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { - lua_Alloc f; - lua_lock(L); - if (ud) *ud = G(L)->ud; - f = G(L)->frealloc; - lua_unlock(L); - return f; -} - - -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { - lua_lock(L); - G(L)->ud = ud; - G(L)->frealloc = f; - lua_unlock(L); -} - - -LUA_API void *lua_newuserdata (lua_State *L, size_t size) { - Udata *u; - lua_lock(L); - luaC_checkGC(L); - u = luaS_newudata(L, size, getcurrenv(L)); - setuvalue(L, L->top, u); - api_incr_top(L); - lua_unlock(L); - return u + 1; -} - - - - -static const char *aux_upvalue (StkId fi, int n, TValue **val) { - Closure *f; - if (!ttisfunction(fi)) return NULL; - f = clvalue(fi); - if (f->c.isC) { - if (!(1 <= n && n <= f->c.nupvalues)) return NULL; - *val = &f->c.upvalue[n-1]; - return ""; - } - else { - Proto *p = f->l.p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; - *val = f->l.upvals[n-1]->v; - return getstr(p->upvalues[n-1]); - } -} - - -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val; - lua_lock(L); - name = aux_upvalue(index2adr(L, funcindex), n, &val); - if (name) { - setobj2s(L, L->top, val); - api_incr_top(L); - } - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val; - StkId fi; - lua_lock(L); - fi = index2adr(L, funcindex); - api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val); - if (name) { - L->top--; - setobj(L, val, L->top); - luaC_barrier(L, clvalue(fi), L->top); - } - lua_unlock(L); - return name; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.h deleted file mode 100644 index 2c3fab2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lapi.h +++ /dev/null @@ -1,16 +0,0 @@ -/* -** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ -** Auxiliary functions from Lua API -** See Copyright Notice in lua.h -*/ - -#ifndef lapi_h -#define lapi_h - - -#include "lobject.h" - - -LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.c deleted file mode 100644 index 10f14e2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.c +++ /dev/null @@ -1,652 +0,0 @@ -/* -** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include -#include -#include - - -/* This file uses only the official API of Lua. -** Any function declared here could be written as an application function. -*/ - -#define lauxlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" - - -#define FREELIST_REF 0 /* free list of references */ - - -/* convert a stack index to positive */ -#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ - lua_gettop(L) + (i) + 1) - - -/* -** {====================================================== -** Error-report functions -** ======================================================= -*/ - - -LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { - lua_Debug ar; - if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ - return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); - lua_getinfo(L, "n", &ar); - if (strcmp(ar.namewhat, "method") == 0) { - narg--; /* do not count `self' */ - if (narg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling " LUA_QS " on bad self (%s)", - ar.name, extramsg); - } - if (ar.name == NULL) - ar.name = "?"; - return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", - narg, ar.name, extramsg); -} - - -LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { - const char *msg = lua_pushfstring(L, "%s expected, got %s", - tname, luaL_typename(L, narg)); - return luaL_argerror(L, narg, msg); -} - - -static void tag_error (lua_State *L, int narg, int tag) { - luaL_typerror(L, narg, lua_typename(L, tag)); -} - - -LUALIB_API void luaL_where (lua_State *L, int level) { - lua_Debug ar; - if (lua_getstack(L, level, &ar)) { /* check function at level */ - lua_getinfo(L, "Sl", &ar); /* get info about it */ - if (ar.currentline > 0) { /* is there info? */ - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); - return; - } - } - lua_pushliteral(L, ""); /* else, no information available... */ -} - - -LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - luaL_where(L, 1); - lua_pushvfstring(L, fmt, argp); - va_end(argp); - lua_concat(L, 2); - return lua_error(L); -} - -/* }====================================================== */ - - -LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, - const char *const lst[]) { - const char *name = (def) ? luaL_optstring(L, narg, def) : - luaL_checkstring(L, narg); - int i; - for (i=0; lst[i]; i++) - if (strcmp(lst[i], name) == 0) - return i; - return luaL_argerror(L, narg, - lua_pushfstring(L, "invalid option " LUA_QS, name)); -} - - -LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ - if (!lua_isnil(L, -1)) /* name already in use? */ - return 0; /* leave previous value on top, but return 0 */ - lua_pop(L, 1); - lua_newtable(L); /* create metatable */ - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ - return 1; -} - - -LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); - if (p != NULL) { /* value is a userdata? */ - if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ - lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ - if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ - lua_pop(L, 2); /* remove both metatables */ - return p; - } - } - } - luaL_typerror(L, ud, tname); /* else error */ - return NULL; /* to avoid warnings */ -} - - -LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { - if (!lua_checkstack(L, space)) - luaL_error(L, "stack overflow (%s)", mes); -} - - -LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { - if (lua_type(L, narg) != t) - tag_error(L, narg, t); -} - - -LUALIB_API void luaL_checkany (lua_State *L, int narg) { - if (lua_type(L, narg) == LUA_TNONE) - luaL_argerror(L, narg, "value expected"); -} - - -LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { - const char *s = lua_tolstring(L, narg, len); - if (!s) tag_error(L, narg, LUA_TSTRING); - return s; -} - - -LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, - const char *def, size_t *len) { - if (lua_isnoneornil(L, narg)) { - if (len) - *len = (def ? strlen(def) : 0); - return def; - } - else return luaL_checklstring(L, narg, len); -} - - -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { - lua_Number d = lua_tonumber(L, narg); - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { - return luaL_opt(L, luaL_checknumber, narg, def); -} - - -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { - lua_Integer d = lua_tointeger(L, narg); - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, - lua_Integer def) { - return luaL_opt(L, luaL_checkinteger, narg, def); -} - - -LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { - if (!lua_getmetatable(L, obj)) /* no metatable? */ - return 0; - lua_pushstring(L, event); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { - lua_pop(L, 2); /* remove metatable and metafield */ - return 0; - } - else { - lua_remove(L, -2); /* remove only metatable */ - return 1; - } -} - - -LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { - obj = abs_index(L, obj); - if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ - return 0; - lua_pushvalue(L, obj); - lua_call(L, 1, 1); - return 1; -} - - -LUALIB_API void (luaL_register) (lua_State *L, const char *libname, - const luaL_Reg *l) { - luaI_openlib(L, libname, l, 0); -} - - -static int libsize (const luaL_Reg *l) { - int size = 0; - for (; l->name; l++) size++; - return size; -} - - -LUALIB_API void luaI_openlib (lua_State *L, const char *libname, - const luaL_Reg *l, int nup) { - if (libname) { - int size = libsize(l); - /* check whether lib already exists */ - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); - lua_getfield(L, -1, libname); /* get _LOADED[libname] */ - if (!lua_istable(L, -1)) { /* not found? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) - luaL_error(L, "name conflict for module " LUA_QS, libname); - lua_pushvalue(L, -1); - lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ - } - lua_remove(L, -2); /* remove _LOADED table */ - lua_insert(L, -(nup+1)); /* move library table to below upvalues */ - } - for (; l->name; l++) { - int i; - for (i=0; ifunc, nup); - lua_setfield(L, -(nup+2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - - - -/* -** {====================================================== -** getn-setn: size for arrays -** ======================================================= -*/ - -#if defined(LUA_COMPAT_GETN) - -static int checkint (lua_State *L, int topop) { - int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; - lua_pop(L, topop); - return n; -} - - -static void getsizes (lua_State *L) { - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); - if (lua_isnil(L, -1)) { /* no `size' table? */ - lua_pop(L, 1); /* remove nil */ - lua_newtable(L); /* create it */ - lua_pushvalue(L, -1); /* `size' will be its own metatable */ - lua_setmetatable(L, -2); - lua_pushliteral(L, "kv"); - lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ - } -} - - -LUALIB_API void luaL_setn (lua_State *L, int t, int n) { - t = abs_index(L, t); - lua_pushliteral(L, "n"); - lua_rawget(L, t); - if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ - lua_pushliteral(L, "n"); /* use it */ - lua_pushinteger(L, n); - lua_rawset(L, t); - } - else { /* use `sizes' */ - getsizes(L); - lua_pushvalue(L, t); - lua_pushinteger(L, n); - lua_rawset(L, -3); /* sizes[t] = n */ - lua_pop(L, 1); /* remove `sizes' */ - } -} - - -LUALIB_API int luaL_getn (lua_State *L, int t) { - int n; - t = abs_index(L, t); - lua_pushliteral(L, "n"); /* try t.n */ - lua_rawget(L, t); - if ((n = checkint(L, 1)) >= 0) return n; - getsizes(L); /* else try sizes[t] */ - lua_pushvalue(L, t); - lua_rawget(L, -2); - if ((n = checkint(L, 2)) >= 0) return n; - return (int)lua_objlen(L, t); -} - -#endif - -/* }====================================================== */ - - - -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, - const char *r) { - const char *wild; - size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); - while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, wild - s); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after `p' */ - } - luaL_addstring(&b, s); /* push last suffix */ - luaL_pushresult(&b); - return lua_tostring(L, -1); -} - - -LUALIB_API const char *luaL_findtable (lua_State *L, int idx, - const char *fname, int szhint) { - const char *e; - lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, e - fname); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { /* no such field? */ - lua_pop(L, 1); /* remove this nil */ - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ - lua_pushlstring(L, fname, e - fname); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } - else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - - -#define bufflen(B) ((B)->p - (B)->buffer) -#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) - -#define LIMIT (LUA_MINSTACK/2) - - -static int emptybuffer (luaL_Buffer *B) { - size_t l = bufflen(B); - if (l == 0) return 0; /* put nothing on stack */ - else { - lua_pushlstring(B->L, B->buffer, l); - B->p = B->buffer; - B->lvl++; - return 1; - } -} - - -static void adjuststack (luaL_Buffer *B) { - if (B->lvl > 1) { - lua_State *L = B->L; - int toget = 1; /* number of levels to concat */ - size_t toplen = lua_strlen(L, -1); - do { - size_t l = lua_strlen(L, -(toget+1)); - if (B->lvl - toget + 1 >= LIMIT || toplen > l) { - toplen += l; - toget++; - } - else break; - } while (toget < B->lvl); - lua_concat(L, toget); - B->lvl = B->lvl - toget + 1; - } -} - - -LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { - if (emptybuffer(B)) - adjuststack(B); - return B->buffer; -} - - -LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - while (l--) - luaL_addchar(B, *s++); -} - - -LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { - luaL_addlstring(B, s, strlen(s)); -} - - -LUALIB_API void luaL_pushresult (luaL_Buffer *B) { - emptybuffer(B); - lua_concat(B->L, B->lvl); - B->lvl = 1; -} - - -LUALIB_API void luaL_addvalue (luaL_Buffer *B) { - lua_State *L = B->L; - size_t vl; - const char *s = lua_tolstring(L, -1, &vl); - if (vl <= bufffree(B)) { /* fit into buffer? */ - memcpy(B->p, s, vl); /* put it there */ - B->p += vl; - lua_pop(L, 1); /* remove from stack */ - } - else { - if (emptybuffer(B)) - lua_insert(L, -2); /* put buffer before new value */ - B->lvl++; /* add new value into B stack */ - adjuststack(B); - } -} - - -LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { - B->L = L; - B->p = B->buffer; - B->lvl = 0; -} - -/* }====================================================== */ - - -LUALIB_API int luaL_ref (lua_State *L, int t) { - int ref; - t = abs_index(L, t); - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* `nil' has a unique fixed reference */ - } - lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ - lua_pop(L, 1); /* remove it from stack */ - if (ref != 0) { /* any free element? */ - lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ - } - else { /* no free elements */ - ref = (int)lua_objlen(L, t); - ref++; /* create new reference */ - } - lua_rawseti(L, t, ref); - return ref; -} - - -LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { - if (ref >= 0) { - t = abs_index(L, t); - lua_rawgeti(L, t, FREELIST_REF); - lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ - lua_pushinteger(L, ref); - lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ - } -} - - - -/* -** {====================================================== -** Load functions -** ======================================================= -*/ - -typedef struct LoadF { - int extraline; - FILE *f; - char buff[LUAL_BUFFERSIZE]; -} LoadF; - - -static const char *getF (lua_State *L, void *ud, size_t *size) { - LoadF *lf = (LoadF *)ud; - (void)L; - if (lf->extraline) { - lf->extraline = 0; - *size = 1; - return "\n"; - } - if (feof(lf->f)) return NULL; - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); - return (*size > 0) ? lf->buff : NULL; -} - - -static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); - const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); - lua_remove(L, fnameindex); - return LUA_ERRFILE; -} - - -LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { - LoadF lf; - int status, readstatus; - int c; - int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ - lf.extraline = 0; - if (filename == NULL) { - lua_pushliteral(L, "=stdin"); - lf.f = stdin; - } - else { - lua_pushfstring(L, "@%s", filename); - lf.f = fopen(filename, "r"); - if (lf.f == NULL) return errfile(L, "open", fnameindex); - } - c = getc(lf.f); - if (c == '#') { /* Unix exec. file? */ - lf.extraline = 1; - while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ - if (c == '\n') c = getc(lf.f); - } - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - /* skip eventual `#!...' */ - while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; - lf.extraline = 0; - } - ungetc(c, lf.f); - status = lua_load(L, getF, &lf, lua_tostring(L, -1)); - readstatus = ferror(lf.f); - if (filename) fclose(lf.f); /* close file (even in case of errors) */ - if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from `lua_load' */ - return errfile(L, "read", fnameindex); - } - lua_remove(L, fnameindex); - return status; -} - - -typedef struct LoadS { - const char *s; - size_t size; -} LoadS; - - -static const char *getS (lua_State *L, void *ud, size_t *size) { - LoadS *ls = (LoadS *)ud; - (void)L; - if (ls->size == 0) return NULL; - *size = ls->size; - ls->size = 0; - return ls->s; -} - - -LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, - const char *name) { - LoadS ls; - ls.s = buff; - ls.size = size; - return lua_load(L, getS, &ls, name); -} - - -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { - return luaL_loadbuffer(L, s, strlen(s), s); -} - - - -/* }====================================================== */ - - -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { - (void)ud; - (void)osize; - if (nsize == 0) { - free(ptr); - return NULL; - } - else - return realloc(ptr, nsize); -} - - -static int panic (lua_State *L) { - (void)L; /* to avoid warnings */ - fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); - return 0; -} - - -LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); - if (L) lua_atpanic(L, &panic); - return L; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.h deleted file mode 100644 index 3425823..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lauxlib.h +++ /dev/null @@ -1,174 +0,0 @@ -/* -** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lauxlib_h -#define lauxlib_h - - -#include -#include - -#include "lua.h" - - -#if defined(LUA_COMPAT_GETN) -LUALIB_API int (luaL_getn) (lua_State *L, int t); -LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); -#else -#define luaL_getn(L,i) ((int)lua_objlen(L, i)) -#define luaL_setn(L,i,j) ((void)0) /* no op! */ -#endif - -#if defined(LUA_COMPAT_OPENLIB) -#define luaI_openlib luaL_openlib -#endif - - -/* extra error code for `luaL_load' */ -#define LUA_ERRFILE (LUA_ERRERR+1) - - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - - - -LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); -LUALIB_API void (luaL_register) (lua_State *L, const char *libname, - const luaL_Reg *l); -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, - size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, - const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); - -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, - lua_Integer def); - -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); - -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); - -LUALIB_API void (luaL_where) (lua_State *L, int lvl); -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); - -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, - const char *const lst[]); - -LUALIB_API int (luaL_ref) (lua_State *L, int t); -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); - -LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); -LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, - const char *name); -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); - -LUALIB_API lua_State *(luaL_newstate) (void); - - -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); - -LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, - const char *fname, int szhint); - - - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define luaL_argcheck(L, cond,numarg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) - -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - -#define luaL_dofile(L, fn) \ - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_dostring(L, s) \ - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) - -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - - - -typedef struct luaL_Buffer { - char *p; /* current position in buffer */ - int lvl; /* number of strings in the stack (level) */ - lua_State *L; - char buffer[LUAL_BUFFERSIZE]; -} luaL_Buffer; - -#define luaL_addchar(B,c) \ - ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ - (*(B)->p++ = (char)(c))) - -/* compatibility only */ -#define luaL_putchar(B,c) luaL_addchar(B,c) - -#define luaL_addsize(B,n) ((B)->p += (n)) - -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); -LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); - - -/* }====================================================== */ - - -/* compatibility with ref system */ - -/* pre-defined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ - (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) - -#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) - -#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) - - -#define luaL_reg luaL_Reg - -#endif - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lbaselib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lbaselib.c deleted file mode 100644 index 2ab550b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lbaselib.c +++ /dev/null @@ -1,653 +0,0 @@ -/* -** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ -** Basic library -** See Copyright Notice in lua.h -*/ - - - -#include -#include -#include -#include - -#define lbaselib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - - -/* -** If your system does not support `stdout', you can just remove this function. -** If you need, you can define your own `print' function, following this -** model but changing `fputs' to put the strings at a proper place -** (a console window or a log file, for instance). -*/ -static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tostring(L, -1); /* get result */ - if (s == NULL) - return luaL_error(L, LUA_QL("tostring") " must return a string to " - LUA_QL("print")); - if (i>1) fputs("\t", stdout); - fputs(s, stdout); - lua_pop(L, 1); /* pop result */ - } - fputs("\n", stdout); - return 0; -} - - -static int luaB_tonumber (lua_State *L) { - int base = luaL_optint(L, 2, 10); - if (base == 10) { /* standard conversion */ - luaL_checkany(L, 1); - if (lua_isnumber(L, 1)) { - lua_pushnumber(L, lua_tonumber(L, 1)); - return 1; - } - } - else { - const char *s1 = luaL_checkstring(L, 1); - char *s2; - unsigned long n; - luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - n = strtoul(s1, &s2, base); - if (s1 != s2) { /* at least one valid digit? */ - while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ - if (*s2 == '\0') { /* no invalid trailing characters? */ - lua_pushnumber(L, (lua_Number)n); - return 1; - } - } - } - lua_pushnil(L); /* else not a number */ - return 1; -} - - -static int luaB_error (lua_State *L) { - int level = luaL_optint(L, 2, 1); - lua_settop(L, 1); - if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ - luaL_where(L, level); - lua_pushvalue(L, 1); - lua_concat(L, 2); - } - return lua_error(L); -} - - -static int luaB_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); - return 1; /* no metatable */ - } - luaL_getmetafield(L, 1, "__metatable"); - return 1; /* returns either __metatable field (if present) or metatable */ -} - - -static int luaB_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - if (luaL_getmetafield(L, 1, "__metatable")) - luaL_error(L, "cannot change a protected metatable"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; -} - - -static void getfunc (lua_State *L, int opt) { - if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); - else { - lua_Debug ar; - int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); - luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); - if (lua_getstack(L, level, &ar) == 0) - luaL_argerror(L, 1, "invalid level"); - lua_getinfo(L, "f", &ar); - if (lua_isnil(L, -1)) - luaL_error(L, "no function environment for tail call at level %d", - level); - } -} - - -static int luaB_getfenv (lua_State *L) { - getfunc(L, 1); - if (lua_iscfunction(L, -1)) /* is a C function? */ - lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ - else - lua_getfenv(L, -1); - return 1; -} - - -static int luaB_setfenv (lua_State *L) { - luaL_checktype(L, 2, LUA_TTABLE); - getfunc(L, 0); - lua_pushvalue(L, 2); - if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { - /* change environment of current thread */ - lua_pushthread(L); - lua_insert(L, -2); - lua_setfenv(L, -2); - return 0; - } - else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) - luaL_error(L, - LUA_QL("setfenv") " cannot change environment of given object"); - return 1; -} - - -static int luaB_rawequal (lua_State *L) { - luaL_checkany(L, 1); - luaL_checkany(L, 2); - lua_pushboolean(L, lua_rawequal(L, 1, 2)); - return 1; -} - - -static int luaB_rawget (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - lua_settop(L, 2); - lua_rawget(L, 1); - return 1; -} - -static int luaB_rawset (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - luaL_checkany(L, 3); - lua_settop(L, 3); - lua_rawset(L, 1); - return 1; -} - - -static int luaB_gcinfo (lua_State *L) { - lua_pushinteger(L, lua_getgccount(L)); - return 1; -} - - -static int luaB_collectgarbage (lua_State *L) { - static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setpause", "setstepmul", NULL}; - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; - int o = luaL_checkoption(L, 1, "collect", opts); - int ex = luaL_optint(L, 2, 0); - int res = lua_gc(L, optsnum[o], ex); - switch (optsnum[o]) { - case LUA_GCCOUNT: { - int b = lua_gc(L, LUA_GCCOUNTB, 0); - lua_pushnumber(L, res + ((lua_Number)b/1024)); - return 1; - } - case LUA_GCSTEP: { - lua_pushboolean(L, res); - return 1; - } - default: { - lua_pushnumber(L, res); - return 1; - } - } -} - - -static int luaB_type (lua_State *L) { - luaL_checkany(L, 1); - lua_pushstring(L, luaL_typename(L, 1)); - return 1; -} - - -static int luaB_next (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 2); /* create a 2nd argument if there isn't one */ - if (lua_next(L, 1)) - return 2; - else { - lua_pushnil(L); - return 1; - } -} - - -static int luaB_pairs (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ - lua_pushvalue(L, 1); /* state, */ - lua_pushnil(L); /* and initial value */ - return 3; -} - - -static int ipairsaux (lua_State *L) { - int i = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - i++; /* next value */ - lua_pushinteger(L, i); - lua_rawgeti(L, 1, i); - return (lua_isnil(L, -1)) ? 0 : 2; -} - - -static int luaB_ipairs (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ - lua_pushvalue(L, 1); /* state, */ - lua_pushinteger(L, 0); /* and initial value */ - return 3; -} - - -static int load_aux (lua_State *L, int status) { - if (status == 0) /* OK? */ - return 1; - else { - lua_pushnil(L); - lua_insert(L, -2); /* put before error message */ - return 2; /* return nil plus error message */ - } -} - - -static int luaB_loadstring (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - const char *chunkname = luaL_optstring(L, 2, s); - return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); -} - - -static int luaB_loadfile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - return load_aux(L, luaL_loadfile(L, fname)); -} - - -/* -** Reader for generic `load' function: `lua_load' uses the -** stack for internal stuff, so the reader cannot change the -** stack top. Instead, it keeps its resulting string in a -** reserved slot inside the stack. -*/ -static const char *generic_reader (lua_State *L, void *ud, size_t *size) { - (void)ud; /* to avoid warnings */ - luaL_checkstack(L, 2, "too many nested functions"); - lua_pushvalue(L, 1); /* get function */ - lua_call(L, 0, 1); /* call it */ - if (lua_isnil(L, -1)) { - *size = 0; - return NULL; - } - else if (lua_isstring(L, -1)) { - lua_replace(L, 3); /* save string in a reserved stack slot */ - return lua_tolstring(L, 3, size); - } - else luaL_error(L, "reader function must return a string"); - return NULL; /* to avoid warnings */ -} - - -static int luaB_load (lua_State *L) { - int status; - const char *cname = luaL_optstring(L, 2, "=(load)"); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ - status = lua_load(L, generic_reader, NULL, cname); - return load_aux(L, status); -} - - -static int luaB_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - int n = lua_gettop(L); - if (luaL_loadfile(L, fname) != 0) lua_error(L); - lua_call(L, 0, LUA_MULTRET); - return lua_gettop(L) - n; -} - - -static int luaB_assert (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_toboolean(L, 1)) - return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); - return lua_gettop(L); -} - - -static int luaB_unpack (lua_State *L) { - int i, e, n; - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); - if (i > e) return 0; /* empty range */ - n = e - i + 1; /* number of elements */ - if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ - return luaL_error(L, "too many results to unpack"); - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ - while (i++ < e) /* push arg[i + 1...e] */ - lua_rawgeti(L, 1, i); - return n; -} - - -static int luaB_select (lua_State *L) { - int n = lua_gettop(L); - if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { - lua_pushinteger(L, n-1); - return 1; - } - else { - int i = luaL_checkint(L, 1); - if (i < 0) i = n + i; - else if (i > n) i = n; - luaL_argcheck(L, 1 <= i, 1, "index out of range"); - return n - i; - } -} - - -static int luaB_pcall (lua_State *L) { - int status; - luaL_checkany(L, 1); - status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); - lua_pushboolean(L, (status == 0)); - lua_insert(L, 1); - return lua_gettop(L); /* return status + all results */ -} - - -static int luaB_xpcall (lua_State *L) { - int status; - luaL_checkany(L, 2); - lua_settop(L, 2); - lua_insert(L, 1); /* put error function under function to be called */ - status = lua_pcall(L, 0, LUA_MULTRET, 1); - lua_pushboolean(L, (status == 0)); - lua_replace(L, 1); - return lua_gettop(L); /* return status + all results */ -} - - -static int luaB_tostring (lua_State *L) { - luaL_checkany(L, 1); - if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ - return 1; /* use its value */ - switch (lua_type(L, 1)) { - case LUA_TNUMBER: - lua_pushstring(L, lua_tostring(L, 1)); - break; - case LUA_TSTRING: - lua_pushvalue(L, 1); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); - break; - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - default: - lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); - break; - } - return 1; -} - - -static int luaB_newproxy (lua_State *L) { - lua_settop(L, 1); - lua_newuserdata(L, 0); /* create proxy */ - if (lua_toboolean(L, 1) == 0) - return 1; /* no metatable */ - else if (lua_isboolean(L, 1)) { - lua_newtable(L); /* create a new metatable `m' ... */ - lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ - lua_pushboolean(L, 1); - lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ - } - else { - int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ - if (lua_getmetatable(L, 1)) { - lua_rawget(L, lua_upvalueindex(1)); - validproxy = lua_toboolean(L, -1); - lua_pop(L, 1); /* remove value */ - } - luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); - lua_getmetatable(L, 1); /* metatable is valid; get it */ - } - lua_setmetatable(L, 2); - return 1; -} - - -static const luaL_Reg base_funcs[] = { - {"assert", luaB_assert}, - {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, - {"error", luaB_error}, - {"gcinfo", luaB_gcinfo}, - {"getfenv", luaB_getfenv}, - {"getmetatable", luaB_getmetatable}, - {"loadfile", luaB_loadfile}, - {"load", luaB_load}, - {"loadstring", luaB_loadstring}, - {"next", luaB_next}, - {"pcall", luaB_pcall}, - {"print", luaB_print}, - {"rawequal", luaB_rawequal}, - {"rawget", luaB_rawget}, - {"rawset", luaB_rawset}, - {"select", luaB_select}, - {"setfenv", luaB_setfenv}, - {"setmetatable", luaB_setmetatable}, - {"tonumber", luaB_tonumber}, - {"tostring", luaB_tostring}, - {"type", luaB_type}, - {"unpack", luaB_unpack}, - {"xpcall", luaB_xpcall}, - {NULL, NULL} -}; - - -/* -** {====================================================== -** Coroutine library -** ======================================================= -*/ - -#define CO_RUN 0 /* running */ -#define CO_SUS 1 /* suspended */ -#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ -#define CO_DEAD 3 - -static const char *const statnames[] = - {"running", "suspended", "normal", "dead"}; - -static int costatus (lua_State *L, lua_State *co) { - if (L == co) return CO_RUN; - switch (lua_status(co)) { - case LUA_YIELD: - return CO_SUS; - case 0: { - lua_Debug ar; - if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ - return CO_NOR; /* it is running */ - else if (lua_gettop(co) == 0) - return CO_DEAD; - else - return CO_SUS; /* initial state */ - } - default: /* some error occured */ - return CO_DEAD; - } -} - - -static int luaB_costatus (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "coroutine expected"); - lua_pushstring(L, statnames[costatus(L, co)]); - return 1; -} - - -static int auxresume (lua_State *L, lua_State *co, int narg) { - int status = costatus(L, co); - if (!lua_checkstack(co, narg)) - luaL_error(L, "too many arguments to resume"); - if (status != CO_SUS) { - lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); - return -1; /* error flag */ - } - lua_xmove(L, co, narg); - lua_setlevel(L, co); - status = lua_resume(co, narg); - if (status == 0 || status == LUA_YIELD) { - int nres = lua_gettop(co); - if (!lua_checkstack(L, nres + 1)) - luaL_error(L, "too many results to resume"); - lua_xmove(co, L, nres); /* move yielded values */ - return nres; - } - else { - lua_xmove(co, L, 1); /* move error message */ - return -1; /* error flag */ - } -} - - -static int luaB_coresume (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - int r; - luaL_argcheck(L, co, 1, "coroutine expected"); - r = auxresume(L, co, lua_gettop(L) - 1); - if (r < 0) { - lua_pushboolean(L, 0); - lua_insert(L, -2); - return 2; /* return false + error message */ - } - else { - lua_pushboolean(L, 1); - lua_insert(L, -(r + 1)); - return r + 1; /* return true + `resume' returns */ - } -} - - -static int luaB_auxwrap (lua_State *L) { - lua_State *co = lua_tothread(L, lua_upvalueindex(1)); - int r = auxresume(L, co, lua_gettop(L)); - if (r < 0) { - if (lua_isstring(L, -1)) { /* error object is a string? */ - luaL_where(L, 1); /* add extra info */ - lua_insert(L, -2); - lua_concat(L, 2); - } - lua_error(L); /* propagate error */ - } - return r; -} - - -static int luaB_cocreate (lua_State *L) { - lua_State *NL = lua_newthread(L); - luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, - "Lua function expected"); - lua_pushvalue(L, 1); /* move function to top */ - lua_xmove(L, NL, 1); /* move function from L to NL */ - return 1; -} - - -static int luaB_cowrap (lua_State *L) { - luaB_cocreate(L); - lua_pushcclosure(L, luaB_auxwrap, 1); - return 1; -} - - -static int luaB_yield (lua_State *L) { - return lua_yield(L, lua_gettop(L)); -} - - -static int luaB_corunning (lua_State *L) { - if (lua_pushthread(L)) - lua_pushnil(L); /* main thread is not a coroutine */ - return 1; -} - - -static const luaL_Reg co_funcs[] = { - {"create", luaB_cocreate}, - {"resume", luaB_coresume}, - {"running", luaB_corunning}, - {"status", luaB_costatus}, - {"wrap", luaB_cowrap}, - {"yield", luaB_yield}, - {NULL, NULL} -}; - -/* }====================================================== */ - - -static void auxopen (lua_State *L, const char *name, - lua_CFunction f, lua_CFunction u) { - lua_pushcfunction(L, u); - lua_pushcclosure(L, f, 1); - lua_setfield(L, -2, name); -} - - -static void base_open (lua_State *L) { - /* set global _G */ - lua_pushvalue(L, LUA_GLOBALSINDEX); - lua_setglobal(L, "_G"); - /* open lib into global table */ - luaL_register(L, "_G", base_funcs); - lua_pushliteral(L, LUA_VERSION); - lua_setglobal(L, "_VERSION"); /* set global _VERSION */ - /* `ipairs' and `pairs' need auxiliary functions as upvalues */ - auxopen(L, "ipairs", luaB_ipairs, ipairsaux); - auxopen(L, "pairs", luaB_pairs, luaB_next); - /* `newproxy' needs a weaktable as upvalue */ - lua_createtable(L, 0, 1); /* new table `w' */ - lua_pushvalue(L, -1); /* `w' will be its own metatable */ - lua_setmetatable(L, -2); - lua_pushliteral(L, "kv"); - lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ - lua_pushcclosure(L, luaB_newproxy, 1); - lua_setglobal(L, "newproxy"); /* set global `newproxy' */ -} - - -LUALIB_API int luaopen_base (lua_State *L) { - base_open(L); - luaL_register(L, LUA_COLIBNAME, co_funcs); - return 2; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.c deleted file mode 100644 index 679cb9c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.c +++ /dev/null @@ -1,831 +0,0 @@ -/* -** $Id: lcode.c,v 2.25.1.5 2011/01/31 14:53:16 roberto Exp $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - - -#include - -#define lcode_c -#define LUA_CORE - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "ltable.h" - - -#define hasjumps(e) ((e)->t != (e)->f) - - -static int isnumeral(expdesc *e) { - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); -} - - -void luaK_nil (FuncState *fs, int from, int n) { - Instruction *previous; - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - if (fs->pc == 0) { /* function start? */ - if (from >= fs->nactvar) - return; /* positions are already clean */ - } - else { - previous = &fs->f->code[fs->pc-1]; - if (GET_OPCODE(*previous) == OP_LOADNIL) { - int pfrom = GETARG_A(*previous); - int pto = GETARG_B(*previous); - if (pfrom <= from && from <= pto+1) { /* can connect both? */ - if (from+n-1 > pto) - SETARG_B(*previous, from+n-1); - return; - } - } - } - } - luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ -} - - -int luaK_jump (FuncState *fs) { - int jpc = fs->jpc; /* save list of jumps to here */ - int j; - fs->jpc = NO_JUMP; - j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); - luaK_concat(fs, &j, jpc); /* keep them on hold */ - return j; -} - - -void luaK_ret (FuncState *fs, int first, int nret) { - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); -} - - -static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { - luaK_codeABC(fs, op, A, B, C); - return luaK_jump(fs); -} - - -static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->code[pc]; - int offset = dest-(pc+1); - lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) - luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); -} - - -/* -** returns current `pc' and marks it as a jump target (to avoid wrong -** optimizations with consecutive instructions not in the same basic block). -*/ -int luaK_getlabel (FuncState *fs) { - fs->lasttarget = fs->pc; - return fs->pc; -} - - -static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); - if (offset == NO_JUMP) /* point to itself represents end of list */ - return NO_JUMP; /* end of list */ - else - return (pc+1)+offset; /* turn offset into absolute position */ -} - - -static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->code[pc]; - if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) - return pi-1; - else - return pi; -} - - -/* -** check whether list has any jump that do not produce a value -** (or produce an inverted value) -*/ -static int need_value (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TESTSET) return 1; - } - return 0; /* not found */ -} - - -static int patchtestreg (FuncState *fs, int node, int reg) { - Instruction *i = getjumpcontrol(fs, node); - if (GET_OPCODE(*i) != OP_TESTSET) - return 0; /* cannot patch other instructions */ - if (reg != NO_REG && reg != GETARG_B(*i)) - SETARG_A(*i, reg); - else /* no register to put value or register already has the value */ - *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); - - return 1; -} - - -static void removevalues (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) - patchtestreg(fs, list, NO_REG); -} - - -static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, - int dtarget) { - while (list != NO_JUMP) { - int next = getjump(fs, list); - if (patchtestreg(fs, list, reg)) - fixjump(fs, list, vtarget); - else - fixjump(fs, list, dtarget); /* jump to default target */ - list = next; - } -} - - -static void dischargejpc (FuncState *fs) { - patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); - fs->jpc = NO_JUMP; -} - - -void luaK_patchlist (FuncState *fs, int list, int target) { - if (target == fs->pc) - luaK_patchtohere(fs, list); - else { - lua_assert(target < fs->pc); - patchlistaux(fs, list, target, NO_REG, target); - } -} - - -void luaK_patchtohere (FuncState *fs, int list) { - luaK_getlabel(fs); - luaK_concat(fs, &fs->jpc, list); -} - - -void luaK_concat (FuncState *fs, int *l1, int l2) { - if (l2 == NO_JUMP) return; - else if (*l1 == NO_JUMP) - *l1 = l2; - else { - int list = *l1; - int next; - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ - list = next; - fixjump(fs, list, l2); - } -} - - -void luaK_checkstack (FuncState *fs, int n) { - int newstack = fs->freereg + n; - if (newstack > fs->f->maxstacksize) { - if (newstack >= MAXSTACK) - luaX_syntaxerror(fs->ls, "function or expression too complex"); - fs->f->maxstacksize = cast_byte(newstack); - } -} - - -void luaK_reserveregs (FuncState *fs, int n) { - luaK_checkstack(fs, n); - fs->freereg += n; -} - - -static void freereg (FuncState *fs, int reg) { - if (!ISK(reg) && reg >= fs->nactvar) { - fs->freereg--; - lua_assert(reg == fs->freereg); - } -} - - -static void freeexp (FuncState *fs, expdesc *e) { - if (e->k == VNONRELOC) - freereg(fs, e->u.s.info); -} - - -static int addk (FuncState *fs, TValue *k, TValue *v) { - lua_State *L = fs->L; - TValue *idx = luaH_set(L, fs->h, k); - Proto *f = fs->f; - int oldsize = f->sizek; - if (ttisnumber(idx)) { - lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); - return cast_int(nvalue(idx)); - } - else { /* constant not found; create a new entry */ - setnvalue(idx, cast_num(fs->nk)); - luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, - MAXARG_Bx, "constant table overflow"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); - setobj(L, &f->k[fs->nk], v); - luaC_barrier(L, f, v); - return fs->nk++; - } -} - - -int luaK_stringK (FuncState *fs, TString *s) { - TValue o; - setsvalue(fs->L, &o, s); - return addk(fs, &o, &o); -} - - -int luaK_numberK (FuncState *fs, lua_Number r) { - TValue o; - setnvalue(&o, r); - return addk(fs, &o, &o); -} - - -static int boolK (FuncState *fs, int b) { - TValue o; - setbvalue(&o, b); - return addk(fs, &o, &o); -} - - -static int nilK (FuncState *fs) { - TValue k, v; - setnilvalue(&v); - /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->L, &k, fs->h); - return addk(fs, &k, &v); -} - - -void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { - if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getcode(fs, e), nresults+1); - } - else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), nresults+1); - SETARG_A(getcode(fs, e), fs->freereg); - luaK_reserveregs(fs, 1); - } -} - - -void luaK_setoneret (FuncState *fs, expdesc *e) { - if (e->k == VCALL) { /* expression is an open function call? */ - e->k = VNONRELOC; - e->u.s.info = GETARG_A(getcode(fs, e)); - } - else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), 2); - e->k = VRELOCABLE; /* can relocate its simple result */ - } -} - - -void luaK_dischargevars (FuncState *fs, expdesc *e) { - switch (e->k) { - case VLOCAL: { - e->k = VNONRELOC; - break; - } - case VUPVAL: { - e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0); - e->k = VRELOCABLE; - break; - } - case VGLOBAL: { - e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info); - e->k = VRELOCABLE; - break; - } - case VINDEXED: { - freereg(fs, e->u.s.aux); - freereg(fs, e->u.s.info); - e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux); - e->k = VRELOCABLE; - break; - } - case VVARARG: - case VCALL: { - luaK_setoneret(fs, e); - break; - } - default: break; /* there is one value available (somewhere) */ - } -} - - -static int code_label (FuncState *fs, int A, int b, int jump) { - luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); -} - - -static void discharge2reg (FuncState *fs, expdesc *e, int reg) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: { - luaK_nil(fs, reg, 1); - break; - } - case VFALSE: case VTRUE: { - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); - break; - } - case VK: { - luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info); - break; - } - case VKNUM: { - luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval)); - break; - } - case VRELOCABLE: { - Instruction *pc = &getcode(fs, e); - SETARG_A(*pc, reg); - break; - } - case VNONRELOC: { - if (reg != e->u.s.info) - luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0); - break; - } - default: { - lua_assert(e->k == VVOID || e->k == VJMP); - return; /* nothing to do... */ - } - } - e->u.s.info = reg; - e->k = VNONRELOC; -} - - -static void discharge2anyreg (FuncState *fs, expdesc *e) { - if (e->k != VNONRELOC) { - luaK_reserveregs(fs, 1); - discharge2reg(fs, e, fs->freereg-1); - } -} - - -static void exp2reg (FuncState *fs, expdesc *e, int reg) { - discharge2reg(fs, e, reg); - if (e->k == VJMP) - luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */ - if (hasjumps(e)) { - int final; /* position after whole expression */ - int p_f = NO_JUMP; /* position of an eventual LOAD false */ - int p_t = NO_JUMP; /* position of an eventual LOAD true */ - if (need_value(fs, e->t) || need_value(fs, e->f)) { - int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_label(fs, reg, 0, 1); - p_t = code_label(fs, reg, 1, 0); - luaK_patchtohere(fs, fj); - } - final = luaK_getlabel(fs); - patchlistaux(fs, e->f, final, reg, p_f); - patchlistaux(fs, e->t, final, reg, p_t); - } - e->f = e->t = NO_JUMP; - e->u.s.info = reg; - e->k = VNONRELOC; -} - - -void luaK_exp2nextreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - freeexp(fs, e); - luaK_reserveregs(fs, 1); - exp2reg(fs, e, fs->freereg - 1); -} - - -int luaK_exp2anyreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - if (e->k == VNONRELOC) { - if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */ - if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */ - exp2reg(fs, e, e->u.s.info); /* put value on it */ - return e->u.s.info; - } - } - luaK_exp2nextreg(fs, e); /* default */ - return e->u.s.info; -} - - -void luaK_exp2val (FuncState *fs, expdesc *e) { - if (hasjumps(e)) - luaK_exp2anyreg(fs, e); - else - luaK_dischargevars(fs, e); -} - - -int luaK_exp2RK (FuncState *fs, expdesc *e) { - luaK_exp2val(fs, e); - switch (e->k) { - case VKNUM: - case VTRUE: - case VFALSE: - case VNIL: { - if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ - e->u.s.info = (e->k == VNIL) ? nilK(fs) : - (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) : - boolK(fs, (e->k == VTRUE)); - e->k = VK; - return RKASK(e->u.s.info); - } - else break; - } - case VK: { - if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */ - return RKASK(e->u.s.info); - else break; - } - default: break; - } - /* not a constant in the right range: put it in a register */ - return luaK_exp2anyreg(fs, e); -} - - -void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { - switch (var->k) { - case VLOCAL: { - freeexp(fs, ex); - exp2reg(fs, ex, var->u.s.info); - return; - } - case VUPVAL: { - int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); - break; - } - case VGLOBAL: { - int e = luaK_exp2anyreg(fs, ex); - luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info); - break; - } - case VINDEXED: { - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); - break; - } - default: { - lua_assert(0); /* invalid var kind to store */ - break; - } - } - freeexp(fs, ex); -} - - -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { - int func; - luaK_exp2anyreg(fs, e); - freeexp(fs, e); - func = fs->freereg; - luaK_reserveregs(fs, 2); - luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); - freeexp(fs, key); - e->u.s.info = func; - e->k = VNONRELOC; -} - - -static void invertjump (FuncState *fs, expdesc *e) { - Instruction *pc = getjumpcontrol(fs, e->u.s.info); - lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && - GET_OPCODE(*pc) != OP_TEST); - SETARG_A(*pc, !(GETARG_A(*pc))); -} - - -static int jumponcond (FuncState *fs, expdesc *e, int cond) { - if (e->k == VRELOCABLE) { - Instruction ie = getcode(fs, e); - if (GET_OPCODE(ie) == OP_NOT) { - fs->pc--; /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); - } - /* else go through */ - } - discharge2anyreg(fs, e); - freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond); -} - - -void luaK_goiftrue (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VK: case VKNUM: case VTRUE: { - pc = NO_JUMP; /* always true; do nothing */ - break; - } - case VJMP: { - invertjump(fs, e); - pc = e->u.s.info; - break; - } - default: { - pc = jumponcond(fs, e, 0); - break; - } - } - luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ - luaK_patchtohere(fs, e->t); - e->t = NO_JUMP; -} - - -static void luaK_goiffalse (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: case VFALSE: { - pc = NO_JUMP; /* always false; do nothing */ - break; - } - case VJMP: { - pc = e->u.s.info; - break; - } - default: { - pc = jumponcond(fs, e, 1); - break; - } - } - luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ - luaK_patchtohere(fs, e->f); - e->f = NO_JUMP; -} - - -static void codenot (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: case VFALSE: { - e->k = VTRUE; - break; - } - case VK: case VKNUM: case VTRUE: { - e->k = VFALSE; - break; - } - case VJMP: { - invertjump(fs, e); - break; - } - case VRELOCABLE: - case VNONRELOC: { - discharge2anyreg(fs, e); - freeexp(fs, e); - e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0); - e->k = VRELOCABLE; - break; - } - default: { - lua_assert(0); /* cannot happen */ - break; - } - } - /* interchange true and false lists */ - { int temp = e->f; e->f = e->t; e->t = temp; } - removevalues(fs, e->f); - removevalues(fs, e->t); -} - - -void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - t->u.s.aux = luaK_exp2RK(fs, k); - t->k = VINDEXED; -} - - -static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { - lua_Number v1, v2, r; - if (!isnumeral(e1) || !isnumeral(e2)) return 0; - v1 = e1->u.nval; - v2 = e2->u.nval; - switch (op) { - case OP_ADD: r = luai_numadd(v1, v2); break; - case OP_SUB: r = luai_numsub(v1, v2); break; - case OP_MUL: r = luai_nummul(v1, v2); break; - case OP_DIV: - if (v2 == 0) return 0; /* do not attempt to divide by 0 */ - r = luai_numdiv(v1, v2); break; - case OP_MOD: - if (v2 == 0) return 0; /* do not attempt to divide by 0 */ - r = luai_nummod(v1, v2); break; - case OP_POW: r = luai_numpow(v1, v2); break; - case OP_UNM: r = luai_numunm(v1); break; - case OP_LEN: return 0; /* no constant folding for 'len' */ - default: lua_assert(0); r = 0; break; - } - if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ - e1->u.nval = r; - return 1; -} - - -static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { - if (constfolding(op, e1, e2)) - return; - else { - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; - int o1 = luaK_exp2RK(fs, e1); - if (o1 > o2) { - freeexp(fs, e1); - freeexp(fs, e2); - } - else { - freeexp(fs, e2); - freeexp(fs, e1); - } - e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); - e1->k = VRELOCABLE; - } -} - - -static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, - expdesc *e2) { - int o1 = luaK_exp2RK(fs, e1); - int o2 = luaK_exp2RK(fs, e2); - freeexp(fs, e2); - freeexp(fs, e1); - if (cond == 0 && op != OP_EQ) { - int temp; /* exchange args to replace by `<' or `<=' */ - temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ - cond = 1; - } - e1->u.s.info = condjump(fs, op, cond, o1, o2); - e1->k = VJMP; -} - - -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { - expdesc e2; - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; - switch (op) { - case OPR_MINUS: { - if (!isnumeral(e)) - luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ - codearith(fs, OP_UNM, e, &e2); - break; - } - case OPR_NOT: codenot(fs, e); break; - case OPR_LEN: { - luaK_exp2anyreg(fs, e); /* cannot operate on constants */ - codearith(fs, OP_LEN, e, &e2); - break; - } - default: lua_assert(0); - } -} - - -void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { - switch (op) { - case OPR_AND: { - luaK_goiftrue(fs, v); - break; - } - case OPR_OR: { - luaK_goiffalse(fs, v); - break; - } - case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ - break; - } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - if (!isnumeral(v)) luaK_exp2RK(fs, v); - break; - } - default: { - luaK_exp2RK(fs, v); - break; - } - } -} - - -void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { - switch (op) { - case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); - luaK_concat(fs, &e2->f, e1->f); - *e1 = *e2; - break; - } - case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); - luaK_concat(fs, &e2->t, e1->t); - *e1 = *e2; - break; - } - case OPR_CONCAT: { - luaK_exp2val(fs, e2); - if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { - lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1); - freeexp(fs, e1); - SETARG_B(getcode(fs, e2), e1->u.s.info); - e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info; - } - else { - luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codearith(fs, OP_CONCAT, e1, e2); - } - break; - } - case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break; - case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break; - case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break; - case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break; - case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break; - case OPR_POW: codearith(fs, OP_POW, e1, e2); break; - case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break; - case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break; - case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break; - case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break; - case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break; - case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break; - default: lua_assert(0); - } -} - - -void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; -} - - -static int luaK_code (FuncState *fs, Instruction i, int line) { - Proto *f = fs->f; - dischargejpc(fs); /* `pc' will change */ - /* put new instruction in code array */ - luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, - MAX_INT, "code size overflow"); - f->code[fs->pc] = i; - /* save corresponding line information */ - luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, - MAX_INT, "code size overflow"); - f->lineinfo[fs->pc] = line; - return fs->pc++; -} - - -int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { - lua_assert(getOpMode(o) == iABC); - lua_assert(getBMode(o) != OpArgN || b == 0); - lua_assert(getCMode(o) != OpArgN || c == 0); - return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); -} - - -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { - lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); - lua_assert(getCMode(o) == OpArgN); - return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); -} - - -void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; - int b = (tostore == LUA_MULTRET) ? 0 : tostore; - lua_assert(tostore != 0); - if (c <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, b, c); - else { - luaK_codeABC(fs, OP_SETLIST, base, b, 0); - luaK_code(fs, cast(Instruction, c), fs->ls->lastline); - } - fs->freereg = base + 1; /* free registers with list values */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.h deleted file mode 100644 index b941c60..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lcode.h +++ /dev/null @@ -1,76 +0,0 @@ -/* -** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lcode_h -#define lcode_h - -#include "llex.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" - - -/* -** Marks the end of a patch list. It is an invalid value both as an absolute -** address, and as a list link (would link an element to itself). -*/ -#define NO_JUMP (-1) - - -/* -** grep "ORDER OPR" if you change these enums -*/ -typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, - OPR_CONCAT, - OPR_NE, OPR_EQ, - OPR_LT, OPR_LE, OPR_GT, OPR_GE, - OPR_AND, OPR_OR, - OPR_NOBINOPR -} BinOpr; - - -typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; - - -#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) - -#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) - -#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) - -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); -LUAI_FUNC void luaK_fixline (FuncState *fs, int line); -LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); -LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); -LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); -LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); -LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); -LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); -LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); -LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); -LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_jump (FuncState *fs); -LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); -LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); -LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); -LUAI_FUNC int luaK_getlabel (FuncState *fs); -LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); -LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); -LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); -LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldblib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldblib.c deleted file mode 100644 index 2027eda..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldblib.c +++ /dev/null @@ -1,398 +0,0 @@ -/* -** $Id: ldblib.c,v 1.104.1.4 2009/08/04 18:50:18 roberto Exp $ -** Interface from Lua to its debug API -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define ldblib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - -static int db_getregistry (lua_State *L) { - lua_pushvalue(L, LUA_REGISTRYINDEX); - return 1; -} - - -static int db_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); /* no metatable */ - } - return 1; -} - - -static int db_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - lua_settop(L, 2); - lua_pushboolean(L, lua_setmetatable(L, 1)); - return 1; -} - - -static int db_getfenv (lua_State *L) { - luaL_checkany(L, 1); - lua_getfenv(L, 1); - return 1; -} - - -static int db_setfenv (lua_State *L) { - luaL_checktype(L, 2, LUA_TTABLE); - lua_settop(L, 2); - if (lua_setfenv(L, 1) == 0) - luaL_error(L, LUA_QL("setfenv") - " cannot change environment of given object"); - return 1; -} - - -static void settabss (lua_State *L, const char *i, const char *v) { - lua_pushstring(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsi (lua_State *L, const char *i, int v) { - lua_pushinteger(L, v); - lua_setfield(L, -2, i); -} - - -static lua_State *getthread (lua_State *L, int *arg) { - if (lua_isthread(L, 1)) { - *arg = 1; - return lua_tothread(L, 1); - } - else { - *arg = 0; - return L; - } -} - - -static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { - if (L == L1) { - lua_pushvalue(L, -2); - lua_remove(L, -3); - } - else - lua_xmove(L1, L, 1); - lua_setfield(L, -2, fname); -} - - -static int db_getinfo (lua_State *L) { - lua_Debug ar; - int arg; - lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnSu"); - if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { - lua_pushnil(L); /* level out of range */ - return 1; - } - } - else if (lua_isfunction(L, arg+1)) { - lua_pushfstring(L, ">%s", options); - options = lua_tostring(L, -1); - lua_pushvalue(L, arg+1); - lua_xmove(L, L1, 1); - } - else - return luaL_argerror(L, arg+1, "function or level expected"); - if (!lua_getinfo(L1, options, &ar)) - return luaL_argerror(L, arg+2, "invalid option"); - lua_createtable(L, 0, 2); - if (strchr(options, 'S')) { - settabss(L, "source", ar.source); - settabss(L, "short_src", ar.short_src); - settabsi(L, "linedefined", ar.linedefined); - settabsi(L, "lastlinedefined", ar.lastlinedefined); - settabss(L, "what", ar.what); - } - if (strchr(options, 'l')) - settabsi(L, "currentline", ar.currentline); - if (strchr(options, 'u')) - settabsi(L, "nups", ar.nups); - if (strchr(options, 'n')) { - settabss(L, "name", ar.name); - settabss(L, "namewhat", ar.namewhat); - } - if (strchr(options, 'L')) - treatstackoption(L, L1, "activelines"); - if (strchr(options, 'f')) - treatstackoption(L, L1, "func"); - return 1; /* return table */ -} - - -static int db_getlocal (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); - if (name) { - lua_xmove(L1, L, 1); - lua_pushstring(L, name); - lua_pushvalue(L, -2); - return 2; - } - else { - lua_pushnil(L); - return 1; - } -} - - -static int db_setlocal (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - luaL_checkany(L, arg+3); - lua_settop(L, arg+3); - lua_xmove(L, L1, 1); - lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); - return 1; -} - - -static int auxupvalue (lua_State *L, int get) { - const char *name; - int n = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); - if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); - if (name == NULL) return 0; - lua_pushstring(L, name); - lua_insert(L, -(get+1)); - return get + 1; -} - - -static int db_getupvalue (lua_State *L) { - return auxupvalue(L, 1); -} - - -static int db_setupvalue (lua_State *L) { - luaL_checkany(L, 3); - return auxupvalue(L, 0); -} - - - -static const char KEY_HOOK = 'h'; - - -static void hookf (lua_State *L, lua_Debug *ar) { - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail return"}; - lua_pushlightuserdata(L, (void *)&KEY_HOOK); - lua_rawget(L, LUA_REGISTRYINDEX); - lua_pushlightuserdata(L, L); - lua_rawget(L, -2); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); - if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); - else lua_pushnil(L); - lua_assert(lua_getinfo(L, "lS", ar)); - lua_call(L, 2, 0); - } -} - - -static int makemask (const char *smask, int count) { - int mask = 0; - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; - if (strchr(smask, 'r')) mask |= LUA_MASKRET; - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; - if (count > 0) mask |= LUA_MASKCOUNT; - return mask; -} - - -static char *unmakemask (int mask, char *smask) { - int i = 0; - if (mask & LUA_MASKCALL) smask[i++] = 'c'; - if (mask & LUA_MASKRET) smask[i++] = 'r'; - if (mask & LUA_MASKLINE) smask[i++] = 'l'; - smask[i] = '\0'; - return smask; -} - - -static void gethooktable (lua_State *L) { - lua_pushlightuserdata(L, (void *)&KEY_HOOK); - lua_rawget(L, LUA_REGISTRYINDEX); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - lua_createtable(L, 0, 1); - lua_pushlightuserdata(L, (void *)&KEY_HOOK); - lua_pushvalue(L, -2); - lua_rawset(L, LUA_REGISTRYINDEX); - } -} - - -static int db_sethook (lua_State *L) { - int arg, mask, count; - lua_Hook func; - lua_State *L1 = getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { - lua_settop(L, arg+1); - func = NULL; mask = 0; count = 0; /* turn off hooks */ - } - else { - const char *smask = luaL_checkstring(L, arg+2); - luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } - gethooktable(L); - lua_pushlightuserdata(L, L1); - lua_pushvalue(L, arg+1); - lua_rawset(L, -3); /* set new hook */ - lua_pop(L, 1); /* remove hook table */ - lua_sethook(L1, func, mask, count); /* set hooks */ - return 0; -} - - -static int db_gethook (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - char buff[5]; - int mask = lua_gethookmask(L1); - lua_Hook hook = lua_gethook(L1); - if (hook != NULL && hook != hookf) /* external hook? */ - lua_pushliteral(L, "external hook"); - else { - gethooktable(L); - lua_pushlightuserdata(L, L1); - lua_rawget(L, -2); /* get hook */ - lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L1)); - return 3; -} - - -static int db_debug (lua_State *L) { - for (;;) { - char buffer[250]; - fputs("lua_debug> ", stderr); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || - strcmp(buffer, "cont\n") == 0) - return 0; - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || - lua_pcall(L, 0, 0, 0)) { - fputs(lua_tostring(L, -1), stderr); - fputs("\n", stderr); - } - lua_settop(L, 0); /* remove eventual returns */ - } -} - - -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ - -static int db_errorfb (lua_State *L) { - int level; - int firstpart = 1; /* still before eventual `...' */ - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - if (lua_isnumber(L, arg+2)) { - level = (int)lua_tointeger(L, arg+2); - lua_pop(L, 1); - } - else - level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ - if (lua_gettop(L) == arg) - lua_pushliteral(L, ""); - else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ - else lua_pushliteral(L, "\n"); - lua_pushliteral(L, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - if (level > LEVELS1 && firstpart) { - /* no more than `LEVELS2' more levels? */ - if (!lua_getstack(L1, level+LEVELS2, &ar)) - level--; /* keep going */ - else { - lua_pushliteral(L, "\n\t..."); /* too many levels */ - while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ - level++; - } - firstpart = 0; - continue; - } - lua_pushliteral(L, "\n\t"); - lua_getinfo(L1, "Snl", &ar); - lua_pushfstring(L, "%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - if (*ar.namewhat != '\0') /* is there a name? */ - lua_pushfstring(L, " in function " LUA_QS, ar.name); - else { - if (*ar.what == 'm') /* main? */ - lua_pushfstring(L, " in main chunk"); - else if (*ar.what == 'C' || *ar.what == 't') - lua_pushliteral(L, " ?"); /* C function or tail call */ - else - lua_pushfstring(L, " in function <%s:%d>", - ar.short_src, ar.linedefined); - } - lua_concat(L, lua_gettop(L) - arg); - } - lua_concat(L, lua_gettop(L) - arg); - return 1; -} - - -static const luaL_Reg dblib[] = { - {"debug", db_debug}, - {"getfenv", db_getfenv}, - {"gethook", db_gethook}, - {"getinfo", db_getinfo}, - {"getlocal", db_getlocal}, - {"getregistry", db_getregistry}, - {"getmetatable", db_getmetatable}, - {"getupvalue", db_getupvalue}, - {"setfenv", db_setfenv}, - {"sethook", db_sethook}, - {"setlocal", db_setlocal}, - {"setmetatable", db_setmetatable}, - {"setupvalue", db_setupvalue}, - {"traceback", db_errorfb}, - {NULL, NULL} -}; - - -LUALIB_API int luaopen_debug (lua_State *L) { - luaL_register(L, LUA_DBLIBNAME, dblib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.c deleted file mode 100644 index 3d7095c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.c +++ /dev/null @@ -1,638 +0,0 @@ -/* -** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $ -** Debug Interface -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - - -#define ldebug_c -#define LUA_CORE - -#include "lua.h" - -#include "lapi.h" -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - - -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); - - -static int currentpc (lua_State *L, CallInfo *ci) { - if (!isLua(ci)) return -1; /* function is not a Lua function? */ - if (ci == L->ci) - ci->savedpc = L->savedpc; - return pcRel(ci->savedpc, ci_func(ci)->l.p); -} - - -static int currentline (lua_State *L, CallInfo *ci) { - int pc = currentpc(L, ci); - if (pc < 0) - return -1; /* only active lua functions have current-line information */ - else - return getline_(ci_func(ci)->l.p, pc); -} - - -/* -** this function can be called asynchronous (e.g. during a signal) -*/ -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { - if (func == NULL || mask == 0) { /* turn off hooks? */ - mask = 0; - func = NULL; - } - L->hook = func; - L->basehookcount = count; - resethookcount(L); - L->hookmask = cast_byte(mask); - return 1; -} - - -LUA_API lua_Hook lua_gethook (lua_State *L) { - return L->hook; -} - - -LUA_API int lua_gethookmask (lua_State *L) { - return L->hookmask; -} - - -LUA_API int lua_gethookcount (lua_State *L) { - return L->basehookcount; -} - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { - int status; - CallInfo *ci; - lua_lock(L); - for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { - level--; - if (f_isLua(ci)) /* Lua function? */ - level -= ci->tailcalls; /* skip lost tail calls */ - } - if (level == 0 && ci > L->base_ci) { /* level found? */ - status = 1; - ar->i_ci = cast_int(ci - L->base_ci); - } - else if (level < 0) { /* level is of a lost tail call? */ - status = 1; - ar->i_ci = 0; - } - else status = 0; /* no such level */ - lua_unlock(L); - return status; -} - - -static Proto *getluaproto (CallInfo *ci) { - return (isLua(ci) ? ci_func(ci)->l.p : NULL); -} - - -static const char *findlocal (lua_State *L, CallInfo *ci, int n) { - const char *name; - Proto *fp = getluaproto(ci); - if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) - return name; /* is a local variable in a Lua function */ - else { - StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; - if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ - return "(*temporary)"; - else - return NULL; - } -} - - -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { - CallInfo *ci = L->base_ci + ar->i_ci; - const char *name = findlocal(L, ci, n); - lua_lock(L); - if (name) - luaA_pushobject(L, ci->base + (n - 1)); - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - CallInfo *ci = L->base_ci + ar->i_ci; - const char *name = findlocal(L, ci, n); - lua_lock(L); - if (name) - setobjs2s(L, ci->base + (n - 1), L->top - 1); - L->top--; /* pop value */ - lua_unlock(L); - return name; -} - - -static void funcinfo (lua_Debug *ar, Closure *cl) { - if (cl->c.isC) { - ar->source = "=[C]"; - ar->linedefined = -1; - ar->lastlinedefined = -1; - ar->what = "C"; - } - else { - ar->source = getstr(cl->l.p->source); - ar->linedefined = cl->l.p->linedefined; - ar->lastlinedefined = cl->l.p->lastlinedefined; - ar->what = (ar->linedefined == 0) ? "main" : "Lua"; - } - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); -} - - -static void info_tailcall (lua_Debug *ar) { - ar->name = ar->namewhat = ""; - ar->what = "tail"; - ar->lastlinedefined = ar->linedefined = ar->currentline = -1; - ar->source = "=(tail call)"; - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); - ar->nups = 0; -} - - -static void collectvalidlines (lua_State *L, Closure *f) { - if (f == NULL || f->c.isC) { - setnilvalue(L->top); - } - else { - Table *t = luaH_new(L, 0, 0); - int *lineinfo = f->l.p->lineinfo; - int i; - for (i=0; il.p->sizelineinfo; i++) - setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); - sethvalue(L, L->top, t); - } - incr_top(L); -} - - -static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, - Closure *f, CallInfo *ci) { - int status = 1; - if (f == NULL) { - info_tailcall(ar); - return status; - } - for (; *what; what++) { - switch (*what) { - case 'S': { - funcinfo(ar, f); - break; - } - case 'l': { - ar->currentline = (ci) ? currentline(L, ci) : -1; - break; - } - case 'u': { - ar->nups = f->c.nupvalues; - break; - } - case 'n': { - ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; - if (ar->namewhat == NULL) { - ar->namewhat = ""; /* not found */ - ar->name = NULL; - } - break; - } - case 'L': - case 'f': /* handled by lua_getinfo */ - break; - default: status = 0; /* invalid option */ - } - } - return status; -} - - -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { - int status; - Closure *f = NULL; - CallInfo *ci = NULL; - lua_lock(L); - if (*what == '>') { - StkId func = L->top - 1; - luai_apicheck(L, ttisfunction(func)); - what++; /* skip the '>' */ - f = clvalue(func); - L->top--; /* pop function */ - } - else if (ar->i_ci != 0) { /* no tail call? */ - ci = L->base_ci + ar->i_ci; - lua_assert(ttisfunction(ci->func)); - f = clvalue(ci->func); - } - status = auxgetinfo(L, what, ar, f, ci); - if (strchr(what, 'f')) { - if (f == NULL) setnilvalue(L->top); - else setclvalue(L, L->top, f); - incr_top(L); - } - if (strchr(what, 'L')) - collectvalidlines(L, f); - lua_unlock(L); - return status; -} - - -/* -** {====================================================== -** Symbolic Execution and code checker -** ======================================================= -*/ - -#define check(x) if (!(x)) return 0; - -#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) - -#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) - - - -static int precheck (const Proto *pt) { - check(pt->maxstacksize <= MAXSTACK); - check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); - check(!(pt->is_vararg & VARARG_NEEDSARG) || - (pt->is_vararg & VARARG_HASARG)); - check(pt->sizeupvalues <= pt->nups); - check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); - check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); - return 1; -} - - -#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) - -int luaG_checkopenop (Instruction i) { - switch (GET_OPCODE(i)) { - case OP_CALL: - case OP_TAILCALL: - case OP_RETURN: - case OP_SETLIST: { - check(GETARG_B(i) == 0); - return 1; - } - default: return 0; /* invalid instruction after an open call */ - } -} - - -static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { - switch (mode) { - case OpArgN: check(r == 0); break; - case OpArgU: break; - case OpArgR: checkreg(pt, r); break; - case OpArgK: - check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); - break; - } - return 1; -} - - -static Instruction symbexec (const Proto *pt, int lastpc, int reg) { - int pc; - int last; /* stores position of last instruction that changed `reg' */ - last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ - check(precheck(pt)); - for (pc = 0; pc < lastpc; pc++) { - Instruction i = pt->code[pc]; - OpCode op = GET_OPCODE(i); - int a = GETARG_A(i); - int b = 0; - int c = 0; - check(op < NUM_OPCODES); - checkreg(pt, a); - switch (getOpMode(op)) { - case iABC: { - b = GETARG_B(i); - c = GETARG_C(i); - check(checkArgMode(pt, b, getBMode(op))); - check(checkArgMode(pt, c, getCMode(op))); - break; - } - case iABx: { - b = GETARG_Bx(i); - if (getBMode(op) == OpArgK) check(b < pt->sizek); - break; - } - case iAsBx: { - b = GETARG_sBx(i); - if (getBMode(op) == OpArgR) { - int dest = pc+1+b; - check(0 <= dest && dest < pt->sizecode); - if (dest > 0) { - int j; - /* check that it does not jump to a setlist count; this - is tricky, because the count from a previous setlist may - have the same value of an invalid setlist; so, we must - go all the way back to the first of them (if any) */ - for (j = 0; j < dest; j++) { - Instruction d = pt->code[dest-1-j]; - if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; - } - /* if 'j' is even, previous value is not a setlist (even if - it looks like one) */ - check((j&1) == 0); - } - } - break; - } - } - if (testAMode(op)) { - if (a == reg) last = pc; /* change register `a' */ - } - if (testTMode(op)) { - check(pc+2 < pt->sizecode); /* check skip */ - check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); - } - switch (op) { - case OP_LOADBOOL: { - if (c == 1) { /* does it jump? */ - check(pc+2 < pt->sizecode); /* check its jump */ - check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || - GETARG_C(pt->code[pc+1]) != 0); - } - break; - } - case OP_LOADNIL: { - if (a <= reg && reg <= b) - last = pc; /* set registers from `a' to `b' */ - break; - } - case OP_GETUPVAL: - case OP_SETUPVAL: { - check(b < pt->nups); - break; - } - case OP_GETGLOBAL: - case OP_SETGLOBAL: { - check(ttisstring(&pt->k[b])); - break; - } - case OP_SELF: { - checkreg(pt, a+1); - if (reg == a+1) last = pc; - break; - } - case OP_CONCAT: { - check(b < c); /* at least two operands */ - break; - } - case OP_TFORLOOP: { - check(c >= 1); /* at least one result (control variable) */ - checkreg(pt, a+2+c); /* space for results */ - if (reg >= a+2) last = pc; /* affect all regs above its base */ - break; - } - case OP_FORLOOP: - case OP_FORPREP: - checkreg(pt, a+3); - /* go through */ - case OP_JMP: { - int dest = pc+1+b; - /* not full check and jump is forward and do not skip `lastpc'? */ - if (reg != NO_REG && pc < dest && dest <= lastpc) - pc += b; /* do the jump */ - break; - } - case OP_CALL: - case OP_TAILCALL: { - if (b != 0) { - checkreg(pt, a+b-1); - } - c--; /* c = num. returns */ - if (c == LUA_MULTRET) { - check(checkopenop(pt, pc)); - } - else if (c != 0) - checkreg(pt, a+c-1); - if (reg >= a) last = pc; /* affect all registers above base */ - break; - } - case OP_RETURN: { - b--; /* b = num. returns */ - if (b > 0) checkreg(pt, a+b-1); - break; - } - case OP_SETLIST: { - if (b > 0) checkreg(pt, a + b); - if (c == 0) { - pc++; - check(pc < pt->sizecode - 1); - } - break; - } - case OP_CLOSURE: { - int nup, j; - check(b < pt->sizep); - nup = pt->p[b]->nups; - check(pc + nup < pt->sizecode); - for (j = 1; j <= nup; j++) { - OpCode op1 = GET_OPCODE(pt->code[pc + j]); - check(op1 == OP_GETUPVAL || op1 == OP_MOVE); - } - if (reg != NO_REG) /* tracing? */ - pc += nup; /* do not 'execute' these pseudo-instructions */ - break; - } - case OP_VARARG: { - check((pt->is_vararg & VARARG_ISVARARG) && - !(pt->is_vararg & VARARG_NEEDSARG)); - b--; - if (b == LUA_MULTRET) check(checkopenop(pt, pc)); - checkreg(pt, a+b-1); - break; - } - default: break; - } - } - return pt->code[last]; -} - -#undef check -#undef checkjump -#undef checkreg - -/* }====================================================== */ - - -int luaG_checkcode (const Proto *pt) { - return (symbexec(pt, pt->sizecode, NO_REG) != 0); -} - - -static const char *kname (Proto *p, int c) { - if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) - return svalue(&p->k[INDEXK(c)]); - else - return "?"; -} - - -static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, - const char **name) { - if (isLua(ci)) { /* a Lua function? */ - Proto *p = ci_func(ci)->l.p; - int pc = currentpc(L, ci); - Instruction i; - *name = luaF_getlocalname(p, stackpos+1, pc); - if (*name) /* is a local? */ - return "local"; - i = symbexec(p, pc, stackpos); /* try symbolic execution */ - lua_assert(pc != -1); - switch (GET_OPCODE(i)) { - case OP_GETGLOBAL: { - int g = GETARG_Bx(i); /* global index */ - lua_assert(ttisstring(&p->k[g])); - *name = svalue(&p->k[g]); - return "global"; - } - case OP_MOVE: { - int a = GETARG_A(i); - int b = GETARG_B(i); /* move from `b' to `a' */ - if (b < a) - return getobjname(L, ci, b, name); /* get name for `b' */ - break; - } - case OP_GETTABLE: { - int k = GETARG_C(i); /* key index */ - *name = kname(p, k); - return "field"; - } - case OP_GETUPVAL: { - int u = GETARG_B(i); /* upvalue index */ - *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; - return "upvalue"; - } - case OP_SELF: { - int k = GETARG_C(i); /* key index */ - *name = kname(p, k); - return "method"; - } - default: break; - } - } - return NULL; /* no useful name found */ -} - - -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - Instruction i; - if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) - return NULL; /* calling function is not Lua (or is unknown) */ - ci--; /* calling function */ - i = ci_func(ci)->l.p->code[currentpc(L, ci)]; - if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || - GET_OPCODE(i) == OP_TFORLOOP) - return getobjname(L, ci, GETARG_A(i), name); - else - return NULL; /* no useful name can be found */ -} - - -/* only ANSI way to check whether a pointer points to an array */ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId p; - for (p = ci->base; p < ci->top; p++) - if (o == p) return 1; - return 0; -} - - -void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { - const char *name = NULL; - const char *t = luaT_typenames[ttype(o)]; - const char *kind = (isinstack(L->ci, o)) ? - getobjname(L, L->ci, cast_int(o - L->base), &name) : - NULL; - if (kind) - luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", - op, kind, name, t); - else - luaG_runerror(L, "attempt to %s a %s value", op, t); -} - - -void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; - lua_assert(!ttisstring(p1) && !ttisnumber(p1)); - luaG_typeerror(L, p1, "concatenate"); -} - - -void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { - TValue temp; - if (luaV_tonumber(p1, &temp) == NULL) - p2 = p1; /* first operand is wrong */ - luaG_typeerror(L, p2, "perform arithmetic on"); -} - - -int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { - const char *t1 = luaT_typenames[ttype(p1)]; - const char *t2 = luaT_typenames[ttype(p2)]; - if (t1[2] == t2[2]) - luaG_runerror(L, "attempt to compare two %s values", t1); - else - luaG_runerror(L, "attempt to compare %s with %s", t1, t2); - return 0; -} - - -static void addinfo (lua_State *L, const char *msg) { - CallInfo *ci = L->ci; - if (isLua(ci)) { /* is Lua code? */ - char buff[LUA_IDSIZE]; /* add file:line information */ - int line = currentline(L, ci); - luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); - luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); - } -} - - -void luaG_errormsg (lua_State *L) { - if (L->errfunc != 0) { /* is there an error handling function? */ - StkId errfunc = restorestack(L, L->errfunc); - if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - incr_top(L); - luaD_call(L, L->top - 2, 1); /* call it */ - } - luaD_throw(L, LUA_ERRRUN); -} - - -void luaG_runerror (lua_State *L, const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - addinfo(L, luaO_pushvfstring(L, fmt, argp)); - va_end(argp); - luaG_errormsg(L); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.h deleted file mode 100644 index 6f1155f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldebug.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ -** Auxiliary functions from Debug Interface module -** See Copyright Notice in lua.h -*/ - -#ifndef ldebug_h -#define ldebug_h - - -#include "lstate.h" - - -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) - -#define getline_(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) - -#define resethookcount(L) (L->hookcount = L->basehookcount) - - -LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, - const char *opname); -LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); -LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaG_errormsg (lua_State *L); -LUAI_FUNC int luaG_checkcode (const Proto *pt); -LUAI_FUNC int luaG_checkopenop (Instruction i); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.c deleted file mode 100644 index d1bf786..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.c +++ /dev/null @@ -1,519 +0,0 @@ -/* -** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define ldo_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" -#include "lzio.h" - - - - -/* -** {====================================================== -** Error-recovery functions -** ======================================================= -*/ - - -/* chain list of long jump buffers */ -struct lua_longjmp { - struct lua_longjmp *previous; - luai_jmpbuf b; - volatile int status; /* error code */ -}; - - -void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { - setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG)); - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - case LUA_ERRSYNTAX: - case LUA_ERRRUN: { - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ - break; - } - } - L->top = oldtop + 1; -} - - -static void restore_stack_limit (lua_State *L) { - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); - if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */ - int inuse = cast_int(L->ci - L->base_ci); - if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */ - luaD_reallocCI(L, LUAI_MAXCALLS); - } -} - - -static void resetstack (lua_State *L, int status) { - L->ci = L->base_ci; - L->base = L->ci->base; - luaF_close(L, L->base); /* close eventual pending closures */ - luaD_seterrorobj(L, status, L->base); - L->nCcalls = L->baseCcalls; - L->allowhook = 1; - restore_stack_limit(L); - L->errfunc = 0; - L->errorJmp = NULL; -} - - -void luaD_throw (lua_State *L, int errcode) { - if (L->errorJmp) { - L->errorJmp->status = errcode; - LUAI_THROW(L, L->errorJmp); - } - else { - L->status = cast_byte(errcode); - if (G(L)->panic) { - resetstack(L, errcode); - lua_unlock(L); - G(L)->panic(L); - } - exit(EXIT_FAILURE); - } -} - - -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - struct lua_longjmp lj; - lj.status = 0; - lj.previous = L->errorJmp; /* chain new error handler */ - L->errorJmp = &lj; - LUAI_TRY(L, &lj, - (*f)(L, ud); - ); - L->errorJmp = lj.previous; /* restore old error handler */ - return lj.status; -} - -/* }====================================================== */ - - -static void correctstack (lua_State *L, TValue *oldstack) { - CallInfo *ci; - GCObject *up; - L->top = (L->top - oldstack) + L->stack; - for (up = L->openupval; up != NULL; up = up->gch.next) - gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; - for (ci = L->base_ci; ci <= L->ci; ci++) { - ci->top = (ci->top - oldstack) + L->stack; - ci->base = (ci->base - oldstack) + L->stack; - ci->func = (ci->func - oldstack) + L->stack; - } - L->base = (L->base - oldstack) + L->stack; -} - - -void luaD_reallocstack (lua_State *L, int newsize) { - TValue *oldstack = L->stack; - int realsize = newsize + 1 + EXTRA_STACK; - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); - luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue); - L->stacksize = realsize; - L->stack_last = L->stack+newsize; - correctstack(L, oldstack); -} - - -void luaD_reallocCI (lua_State *L, int newsize) { - CallInfo *oldci = L->base_ci; - luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo); - L->size_ci = newsize; - L->ci = (L->ci - oldci) + L->base_ci; - L->end_ci = L->base_ci + L->size_ci - 1; -} - - -void luaD_growstack (lua_State *L, int n) { - if (n <= L->stacksize) /* double size is enough? */ - luaD_reallocstack(L, 2*L->stacksize); - else - luaD_reallocstack(L, L->stacksize + n); -} - - -static CallInfo *growCI (lua_State *L) { - if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */ - luaD_throw(L, LUA_ERRERR); - else { - luaD_reallocCI(L, 2*L->size_ci); - if (L->size_ci > LUAI_MAXCALLS) - luaG_runerror(L, "stack overflow"); - } - return ++L->ci; -} - - -void luaD_callhook (lua_State *L, int event, int line) { - lua_Hook hook = L->hook; - if (hook && L->allowhook) { - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, L->ci->top); - lua_Debug ar; - ar.event = event; - ar.currentline = line; - if (event == LUA_HOOKTAILRET) - ar.i_ci = 0; /* tail call; no debug information about it */ - else - ar.i_ci = cast_int(L->ci - L->base_ci); - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - L->ci->top = L->top + LUA_MINSTACK; - lua_assert(L->ci->top <= L->stack_last); - L->allowhook = 0; /* cannot call hooks inside a hook */ - lua_unlock(L); - (*hook)(L, &ar); - lua_lock(L); - lua_assert(!L->allowhook); - L->allowhook = 1; - L->ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); - } -} - - -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { - int i; - int nfixargs = p->numparams; - Table *htab = NULL; - StkId base, fixed; - for (; actual < nfixargs; ++actual) - setnilvalue(L->top++); -#if defined(LUA_COMPAT_VARARG) - if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ - int nvar = actual - nfixargs; /* number of extra arguments */ - lua_assert(p->is_vararg & VARARG_HASARG); - luaC_checkGC(L); - luaD_checkstack(L, p->maxstacksize); - htab = luaH_new(L, nvar, 1); /* create `arg' table */ - for (i=0; itop - nvar + i); - /* store counter in field `n' */ - setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar)); - } -#endif - /* move fixed parameters to final position */ - fixed = L->top - actual; /* first fixed argument */ - base = L->top; /* final position of first argument */ - for (i=0; itop++, fixed+i); - setnilvalue(fixed+i); - } - /* add `arg' parameter */ - if (htab) { - sethvalue(L, L->top++, htab); - lua_assert(iswhite(obj2gco(htab))); - } - return base; -} - - -static StkId tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); - StkId p; - ptrdiff_t funcr = savestack(L, func); - if (!ttisfunction(tm)) - luaG_typeerror(L, func, "call"); - /* Open a hole inside the stack at `func' */ - for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); - incr_top(L); - func = restorestack(L, funcr); /* previous call may change stack */ - setobj2s(L, func, tm); /* tag method is the new function to be called */ - return func; -} - - - -#define inc_ci(L) \ - ((L->ci == L->end_ci) ? growCI(L) : \ - (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci)) - - -int luaD_precall (lua_State *L, StkId func, int nresults) { - LClosure *cl; - ptrdiff_t funcr; - if (!ttisfunction(func)) /* `func' is not a function? */ - func = tryfuncTM(L, func); /* check the `function' tag method */ - funcr = savestack(L, func); - cl = &clvalue(func)->l; - L->ci->savedpc = L->savedpc; - if (!cl->isC) { /* Lua function? prepare its call */ - CallInfo *ci; - StkId st, base; - Proto *p = cl->p; - luaD_checkstack(L, p->maxstacksize); - func = restorestack(L, funcr); - if (!p->is_vararg) { /* no varargs? */ - base = func + 1; - if (L->top > base + p->numparams) - L->top = base + p->numparams; - } - else { /* vararg function */ - int nargs = cast_int(L->top - func) - 1; - base = adjust_varargs(L, p, nargs); - func = restorestack(L, funcr); /* previous call may change the stack */ - } - ci = inc_ci(L); /* now `enter' new function */ - ci->func = func; - L->base = ci->base = base; - ci->top = L->base + p->maxstacksize; - lua_assert(ci->top <= L->stack_last); - L->savedpc = p->code; /* starting point */ - ci->tailcalls = 0; - ci->nresults = nresults; - for (st = L->top; st < ci->top; st++) - setnilvalue(st); - L->top = ci->top; - if (L->hookmask & LUA_MASKCALL) { - L->savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_callhook(L, LUA_HOOKCALL, -1); - L->savedpc--; /* correct 'pc' */ - } - return PCRLUA; - } - else { /* if is a C function, call it */ - CallInfo *ci; - int n; - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci = inc_ci(L); /* now `enter' new function */ - ci->func = restorestack(L, funcr); - L->base = ci->base = ci->func + 1; - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->nresults = nresults; - if (L->hookmask & LUA_MASKCALL) - luaD_callhook(L, LUA_HOOKCALL, -1); - lua_unlock(L); - n = (*curr_func(L)->c.f)(L); /* do the actual call */ - lua_lock(L); - if (n < 0) /* yielding? */ - return PCRYIELD; - else { - luaD_poscall(L, L->top - n); - return PCRC; - } - } -} - - -static StkId callrethooks (lua_State *L, StkId firstResult) { - ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ - luaD_callhook(L, LUA_HOOKRET, -1); - if (f_isLua(L->ci)) { /* Lua function? */ - while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ - luaD_callhook(L, LUA_HOOKTAILRET, -1); - } - return restorestack(L, fr); -} - - -int luaD_poscall (lua_State *L, StkId firstResult) { - StkId res; - int wanted, i; - CallInfo *ci; - if (L->hookmask & LUA_MASKRET) - firstResult = callrethooks(L, firstResult); - ci = L->ci--; - res = ci->func; /* res == final position of 1st result */ - wanted = ci->nresults; - L->base = (ci - 1)->base; /* restore base */ - L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ - /* move results to correct place */ - for (i = wanted; i != 0 && firstResult < L->top; i--) - setobjs2s(L, res++, firstResult++); - while (i-- > 0) - setnilvalue(res++); - L->top = res; - return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ -} - - -/* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. -*/ -void luaD_call (lua_State *L, StkId func, int nResults) { - if (++L->nCcalls >= LUAI_MAXCCALLS) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ - } - if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ - luaV_execute(L, 1); /* call it */ - L->nCcalls--; - luaC_checkGC(L); -} - - -static void resume (lua_State *L, void *ud) { - StkId firstArg = cast(StkId, ud); - CallInfo *ci = L->ci; - if (L->status == 0) { /* start coroutine? */ - lua_assert(ci == L->base_ci && firstArg > L->base); - if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) - return; - } - else { /* resuming from previous yield */ - lua_assert(L->status == LUA_YIELD); - L->status = 0; - if (!f_isLua(ci)) { /* `common' yield? */ - /* finish interrupted execution of `OP_CALL' */ - lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || - GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); - if (luaD_poscall(L, firstArg)) /* complete it... */ - L->top = L->ci->top; /* and correct top if not multiple results */ - } - else /* yielded inside a hook: just continue its execution */ - L->base = L->ci->base; - } - luaV_execute(L, cast_int(L->ci - L->base_ci)); -} - - -static int resume_error (lua_State *L, const char *msg) { - L->top = L->ci->base; - setsvalue2s(L, L->top, luaS_new(L, msg)); - incr_top(L); - lua_unlock(L); - return LUA_ERRRUN; -} - - -LUA_API int lua_resume (lua_State *L, int nargs) { - int status; - lua_lock(L); - if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci)) - return resume_error(L, "cannot resume non-suspended coroutine"); - if (L->nCcalls >= LUAI_MAXCCALLS) - return resume_error(L, "C stack overflow"); - luai_userstateresume(L, nargs); - lua_assert(L->errfunc == 0); - L->baseCcalls = ++L->nCcalls; - status = luaD_rawrunprotected(L, resume, L->top - nargs); - if (status != 0) { /* error? */ - L->status = cast_byte(status); /* mark thread as `dead' */ - luaD_seterrorobj(L, status, L->top); - L->ci->top = L->top; - } - else { - lua_assert(L->nCcalls == L->baseCcalls); - status = L->status; - } - --L->nCcalls; - lua_unlock(L); - return status; -} - - -LUA_API int lua_yield (lua_State *L, int nresults) { - luai_userstateyield(L, nresults); - lua_lock(L); - if (L->nCcalls > L->baseCcalls) - luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); - L->base = L->top - nresults; /* protect stack slots below */ - L->status = LUA_YIELD; - lua_unlock(L); - return -1; -} - - -int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t old_top, ptrdiff_t ef) { - int status; - unsigned short oldnCcalls = L->nCcalls; - ptrdiff_t old_ci = saveci(L, L->ci); - lu_byte old_allowhooks = L->allowhook; - ptrdiff_t old_errfunc = L->errfunc; - L->errfunc = ef; - status = luaD_rawrunprotected(L, func, u); - if (status != 0) { /* an error occurred? */ - StkId oldtop = restorestack(L, old_top); - luaF_close(L, oldtop); /* close eventual pending closures */ - luaD_seterrorobj(L, status, oldtop); - L->nCcalls = oldnCcalls; - L->ci = restoreci(L, old_ci); - L->base = L->ci->base; - L->savedpc = L->ci->savedpc; - L->allowhook = old_allowhooks; - restore_stack_limit(L); - } - L->errfunc = old_errfunc; - return status; -} - - - -/* -** Execute a protected parser. -*/ -struct SParser { /* data to `f_parser' */ - ZIO *z; - Mbuffer buff; /* buffer to be used by the scanner */ - const char *name; -}; - -static void f_parser (lua_State *L, void *ud) { - int i; - Proto *tf; - Closure *cl; - struct SParser *p = cast(struct SParser *, ud); - int c = luaZ_lookahead(p->z); - luaC_checkGC(L); - tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, - &p->buff, p->name); - cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); - cl->l.p = tf; - for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ - cl->l.upvals[i] = luaF_newupval(L); - setclvalue(L, L->top, cl); - incr_top(L); -} - - -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { - struct SParser p; - int status; - p.z = z; p.name = name; - luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); - luaZ_freebuffer(L, &p.buff); - return status; -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.h deleted file mode 100644 index 98fddac..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldo.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#ifndef ldo_h -#define ldo_h - - -#include "lobject.h" -#include "lstate.h" -#include "lzio.h" - - -#define luaD_checkstack(L,n) \ - if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ - luaD_growstack(L, n); \ - else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); - - -#define incr_top(L) {luaD_checkstack(L,1); L->top++;} - -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) - -#define saveci(L,p) ((char *)(p) - (char *)L->base_ci) -#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) - - -/* results from luaD_precall */ -#define PCRLUA 0 /* initiated a call to a Lua function */ -#define PCRC 1 /* did a call to a C function */ -#define PCRYIELD 2 /* C funtion yielded */ - - -/* type of protected functions, to be ran by `runprotected' */ -typedef void (*Pfunc) (lua_State *L, void *ud); - -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); -LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); -LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); -LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); -LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); -LUAI_FUNC void luaD_growstack (lua_State *L, int n); - -LUAI_FUNC void luaD_throw (lua_State *L, int errcode); -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); - -LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldump.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldump.c deleted file mode 100644 index c9d3d48..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ldump.c +++ /dev/null @@ -1,164 +0,0 @@ -/* -** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ -** save precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#include - -#define ldump_c -#define LUA_CORE - -#include "lua.h" - -#include "lobject.h" -#include "lstate.h" -#include "lundump.h" - -typedef struct { - lua_State* L; - lua_Writer writer; - void* data; - int strip; - int status; -} DumpState; - -#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) -#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) - -static void DumpBlock(const void* b, size_t size, DumpState* D) -{ - if (D->status==0) - { - lua_unlock(D->L); - D->status=(*D->writer)(D->L,b,size,D->data); - lua_lock(D->L); - } -} - -static void DumpChar(int y, DumpState* D) -{ - char x=(char)y; - DumpVar(x,D); -} - -static void DumpInt(int x, DumpState* D) -{ - DumpVar(x,D); -} - -static void DumpNumber(lua_Number x, DumpState* D) -{ - DumpVar(x,D); -} - -static void DumpVector(const void* b, int n, size_t size, DumpState* D) -{ - DumpInt(n,D); - DumpMem(b,n,size,D); -} - -static void DumpString(const TString* s, DumpState* D) -{ - if (s==NULL || getstr(s)==NULL) - { - size_t size=0; - DumpVar(size,D); - } - else - { - size_t size=s->tsv.len+1; /* include trailing '\0' */ - DumpVar(size,D); - DumpBlock(getstr(s),size,D); - } -} - -#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) - -static void DumpFunction(const Proto* f, const TString* p, DumpState* D); - -static void DumpConstants(const Proto* f, DumpState* D) -{ - int i,n=f->sizek; - DumpInt(n,D); - for (i=0; ik[i]; - DumpChar(ttype(o),D); - switch (ttype(o)) - { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpChar(bvalue(o),D); - break; - case LUA_TNUMBER: - DumpNumber(nvalue(o),D); - break; - case LUA_TSTRING: - DumpString(rawtsvalue(o),D); - break; - default: - lua_assert(0); /* cannot happen */ - break; - } - } - n=f->sizep; - DumpInt(n,D); - for (i=0; ip[i],f->source,D); -} - -static void DumpDebug(const Proto* f, DumpState* D) -{ - int i,n; - n= (D->strip) ? 0 : f->sizelineinfo; - DumpVector(f->lineinfo,n,sizeof(int),D); - n= (D->strip) ? 0 : f->sizelocvars; - DumpInt(n,D); - for (i=0; ilocvars[i].varname,D); - DumpInt(f->locvars[i].startpc,D); - DumpInt(f->locvars[i].endpc,D); - } - n= (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n,D); - for (i=0; iupvalues[i],D); -} - -static void DumpFunction(const Proto* f, const TString* p, DumpState* D) -{ - DumpString((f->source==p || D->strip) ? NULL : f->source,D); - DumpInt(f->linedefined,D); - DumpInt(f->lastlinedefined,D); - DumpChar(f->nups,D); - DumpChar(f->numparams,D); - DumpChar(f->is_vararg,D); - DumpChar(f->maxstacksize,D); - DumpCode(f,D); - DumpConstants(f,D); - DumpDebug(f,D); -} - -static void DumpHeader(DumpState* D) -{ - char h[LUAC_HEADERSIZE]; - luaU_header(h); - DumpBlock(h,LUAC_HEADERSIZE,D); -} - -/* -** dump Lua function as precompiled chunk -*/ -int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) -{ - DumpState D; - D.L=L; - D.writer=w; - D.data=data; - D.strip=strip; - D.status=0; - DumpHeader(&D); - DumpFunction(f,NULL,&D); - return D.status; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.c deleted file mode 100644 index 813e88f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.c +++ /dev/null @@ -1,174 +0,0 @@ -/* -** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - - -#include - -#define lfunc_c -#define LUA_CORE - -#include "lua.h" - -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { - Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); - luaC_link(L, obj2gco(c), LUA_TFUNCTION); - c->c.isC = 1; - c->c.env = e; - c->c.nupvalues = cast_byte(nelems); - return c; -} - - -Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { - Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); - luaC_link(L, obj2gco(c), LUA_TFUNCTION); - c->l.isC = 0; - c->l.env = e; - c->l.nupvalues = cast_byte(nelems); - while (nelems--) c->l.upvals[nelems] = NULL; - return c; -} - - -UpVal *luaF_newupval (lua_State *L) { - UpVal *uv = luaM_new(L, UpVal); - luaC_link(L, obj2gco(uv), LUA_TUPVAL); - uv->v = &uv->u.value; - setnilvalue(uv->v); - return uv; -} - - -UpVal *luaF_findupval (lua_State *L, StkId level) { - global_State *g = G(L); - GCObject **pp = &L->openupval; - UpVal *p; - UpVal *uv; - while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { - lua_assert(p->v != &p->u.value); - if (p->v == level) { /* found a corresponding upvalue? */ - if (isdead(g, obj2gco(p))) /* is it dead? */ - changewhite(obj2gco(p)); /* ressurect it */ - return p; - } - pp = &p->next; - } - uv = luaM_new(L, UpVal); /* not found: create a new one */ - uv->tt = LUA_TUPVAL; - uv->marked = luaC_white(g); - uv->v = level; /* current value lives in the stack */ - uv->next = *pp; /* chain it in the proper position */ - *pp = obj2gco(uv); - uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ - uv->u.l.next = g->uvhead.u.l.next; - uv->u.l.next->u.l.prev = uv; - g->uvhead.u.l.next = uv; - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - return uv; -} - - -static void unlinkupval (UpVal *uv) { - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ - uv->u.l.prev->u.l.next = uv->u.l.next; -} - - -void luaF_freeupval (lua_State *L, UpVal *uv) { - if (uv->v != &uv->u.value) /* is it open? */ - unlinkupval(uv); /* remove from open list */ - luaM_free(L, uv); /* free upvalue */ -} - - -void luaF_close (lua_State *L, StkId level) { - UpVal *uv; - global_State *g = G(L); - while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o) && uv->v != &uv->u.value); - L->openupval = uv->next; /* remove from `open' list */ - if (isdead(g, o)) - luaF_freeupval(L, uv); /* free upvalue */ - else { - unlinkupval(uv); - setobj(L, &uv->u.value, uv->v); - uv->v = &uv->u.value; /* now current value lives here */ - luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ - } - } -} - - -Proto *luaF_newproto (lua_State *L) { - Proto *f = luaM_new(L, Proto); - luaC_link(L, obj2gco(f), LUA_TPROTO); - f->k = NULL; - f->sizek = 0; - f->p = NULL; - f->sizep = 0; - f->code = NULL; - f->sizecode = 0; - f->sizelineinfo = 0; - f->sizeupvalues = 0; - f->nups = 0; - f->upvalues = NULL; - f->numparams = 0; - f->is_vararg = 0; - f->maxstacksize = 0; - f->lineinfo = NULL; - f->sizelocvars = 0; - f->locvars = NULL; - f->linedefined = 0; - f->lastlinedefined = 0; - f->source = NULL; - return f; -} - - -void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode, Instruction); - luaM_freearray(L, f->p, f->sizep, Proto *); - luaM_freearray(L, f->k, f->sizek, TValue); - luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); - luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); - luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); - luaM_free(L, f); -} - - -void luaF_freeclosure (lua_State *L, Closure *c) { - int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : - sizeLclosure(c->l.nupvalues); - luaM_freemem(L, c, size); -} - - -/* -** Look for n-th local variable at line `line' in function `func'. -** Returns NULL if not found. -*/ -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { - int i; - for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { - if (pc < f->locvars[i].endpc) { /* is variable active? */ - local_number--; - if (local_number == 0) - return getstr(f->locvars[i].varname); - } - } - return NULL; /* not found */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.h deleted file mode 100644 index a68cf51..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lfunc.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - -#ifndef lfunc_h -#define lfunc_h - - -#include "lobject.h" - - -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ - cast(int, sizeof(TValue)*((n)-1))) - -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ - cast(int, sizeof(TValue *)*((n)-1))) - - -LUAI_FUNC Proto *luaF_newproto (lua_State *L); -LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); -LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); -LUAI_FUNC UpVal *luaF_newupval (lua_State *L); -LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level); -LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); -LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); -LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); -LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, - int pc); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.c deleted file mode 100644 index e909c79..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.c +++ /dev/null @@ -1,710 +0,0 @@ -/* -** $Id: lgc.c,v 2.38.1.2 2011/03/18 18:05:38 roberto Exp $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#include - -#define lgc_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -#define GCSTEPSIZE 1024u -#define GCSWEEPMAX 40 -#define GCSWEEPCOST 10 -#define GCFINALIZECOST 100 - - -#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS)) - -#define makewhite(g,x) \ - ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g))) - -#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) -#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) - -#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) - - -#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) -#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT) - - -#define KEYWEAK bitmask(KEYWEAKBIT) -#define VALUEWEAK bitmask(VALUEWEAKBIT) - - - -#define markvalue(g,o) { checkconsistency(o); \ - if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); } - -#define markobject(g,t) { if (iswhite(obj2gco(t))) \ - reallymarkobject(g, obj2gco(t)); } - - -#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause) - - -static void removeentry (Node *n) { - lua_assert(ttisnil(gval(n))); - if (iscollectable(gkey(n))) - setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ -} - - -static void reallymarkobject (global_State *g, GCObject *o) { - lua_assert(iswhite(o) && !isdead(g, o)); - white2gray(o); - switch (o->gch.tt) { - case LUA_TSTRING: { - return; - } - case LUA_TUSERDATA: { - Table *mt = gco2u(o)->metatable; - gray2black(o); /* udata are never gray */ - if (mt) markobject(g, mt); - markobject(g, gco2u(o)->env); - return; - } - case LUA_TUPVAL: { - UpVal *uv = gco2uv(o); - markvalue(g, uv->v); - if (uv->v == &uv->u.value) /* closed? */ - gray2black(o); /* open upvalues are never black */ - return; - } - case LUA_TFUNCTION: { - gco2cl(o)->c.gclist = g->gray; - g->gray = o; - break; - } - case LUA_TTABLE: { - gco2h(o)->gclist = g->gray; - g->gray = o; - break; - } - case LUA_TTHREAD: { - gco2th(o)->gclist = g->gray; - g->gray = o; - break; - } - case LUA_TPROTO: { - gco2p(o)->gclist = g->gray; - g->gray = o; - break; - } - default: lua_assert(0); - } -} - - -static void marktmu (global_State *g) { - GCObject *u = g->tmudata; - if (u) { - do { - u = u->gch.next; - makewhite(g, u); /* may be marked, if left from previous GC */ - reallymarkobject(g, u); - } while (u != g->tmudata); - } -} - - -/* move `dead' udata that need finalization to list `tmudata' */ -size_t luaC_separateudata (lua_State *L, int all) { - global_State *g = G(L); - size_t deadmem = 0; - GCObject **p = &g->mainthread->next; - GCObject *curr; - while ((curr = *p) != NULL) { - if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) - p = &curr->gch.next; /* don't bother with them */ - else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { - markfinalized(gco2u(curr)); /* don't need finalization */ - p = &curr->gch.next; - } - else { /* must call its gc method */ - deadmem += sizeudata(gco2u(curr)); - markfinalized(gco2u(curr)); - *p = curr->gch.next; - /* link `curr' at the end of `tmudata' list */ - if (g->tmudata == NULL) /* list is empty? */ - g->tmudata = curr->gch.next = curr; /* creates a circular list */ - else { - curr->gch.next = g->tmudata->gch.next; - g->tmudata->gch.next = curr; - g->tmudata = curr; - } - } - } - return deadmem; -} - - -static int traversetable (global_State *g, Table *h) { - int i; - int weakkey = 0; - int weakvalue = 0; - const TValue *mode; - if (h->metatable) - markobject(g, h->metatable); - mode = gfasttm(g, h->metatable, TM_MODE); - if (mode && ttisstring(mode)) { /* is there a weak mode? */ - weakkey = (strchr(svalue(mode), 'k') != NULL); - weakvalue = (strchr(svalue(mode), 'v') != NULL); - if (weakkey || weakvalue) { /* is really weak? */ - h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ - h->marked |= cast_byte((weakkey << KEYWEAKBIT) | - (weakvalue << VALUEWEAKBIT)); - h->gclist = g->weak; /* must be cleared after GC, ... */ - g->weak = obj2gco(h); /* ... so put in the appropriate list */ - } - } - if (weakkey && weakvalue) return 1; - if (!weakvalue) { - i = h->sizearray; - while (i--) - markvalue(g, &h->array[i]); - } - i = sizenode(h); - while (i--) { - Node *n = gnode(h, i); - lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n))); - if (ttisnil(gval(n))) - removeentry(n); /* remove empty entries */ - else { - lua_assert(!ttisnil(gkey(n))); - if (!weakkey) markvalue(g, gkey(n)); - if (!weakvalue) markvalue(g, gval(n)); - } - } - return weakkey || weakvalue; -} - - -/* -** All marks are conditional because a GC may happen while the -** prototype is still being created -*/ -static void traverseproto (global_State *g, Proto *f) { - int i; - if (f->source) stringmark(f->source); - for (i=0; isizek; i++) /* mark literals */ - markvalue(g, &f->k[i]); - for (i=0; isizeupvalues; i++) { /* mark upvalue names */ - if (f->upvalues[i]) - stringmark(f->upvalues[i]); - } - for (i=0; isizep; i++) { /* mark nested protos */ - if (f->p[i]) - markobject(g, f->p[i]); - } - for (i=0; isizelocvars; i++) { /* mark local-variable names */ - if (f->locvars[i].varname) - stringmark(f->locvars[i].varname); - } -} - - - -static void traverseclosure (global_State *g, Closure *cl) { - markobject(g, cl->c.env); - if (cl->c.isC) { - int i; - for (i=0; ic.nupvalues; i++) /* mark its upvalues */ - markvalue(g, &cl->c.upvalue[i]); - } - else { - int i; - lua_assert(cl->l.nupvalues == cl->l.p->nups); - markobject(g, cl->l.p); - for (i=0; il.nupvalues; i++) /* mark its upvalues */ - markobject(g, cl->l.upvals[i]); - } -} - - -static void checkstacksizes (lua_State *L, StkId max) { - int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */ - int s_used = cast_int(max - L->stack); /* part of stack in use */ - if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */ - return; /* do not touch the stacks */ - if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) - luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ - condhardstacktests(luaD_reallocCI(L, ci_used + 1)); - if (4*s_used < L->stacksize && - 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize) - luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ - condhardstacktests(luaD_reallocstack(L, s_used)); -} - - -static void traversestack (global_State *g, lua_State *l) { - StkId o, lim; - CallInfo *ci; - markvalue(g, gt(l)); - lim = l->top; - for (ci = l->base_ci; ci <= l->ci; ci++) { - lua_assert(ci->top <= l->stack_last); - if (lim < ci->top) lim = ci->top; - } - for (o = l->stack; o < l->top; o++) - markvalue(g, o); - for (; o <= lim; o++) - setnilvalue(o); - checkstacksizes(l, lim); -} - - -/* -** traverse one gray object, turning it to black. -** Returns `quantity' traversed. -*/ -static l_mem propagatemark (global_State *g) { - GCObject *o = g->gray; - lua_assert(isgray(o)); - gray2black(o); - switch (o->gch.tt) { - case LUA_TTABLE: { - Table *h = gco2h(o); - g->gray = h->gclist; - if (traversetable(g, h)) /* table is weak? */ - black2gray(o); /* keep it gray */ - return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * sizenode(h); - } - case LUA_TFUNCTION: { - Closure *cl = gco2cl(o); - g->gray = cl->c.gclist; - traverseclosure(g, cl); - return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : - sizeLclosure(cl->l.nupvalues); - } - case LUA_TTHREAD: { - lua_State *th = gco2th(o); - g->gray = th->gclist; - th->gclist = g->grayagain; - g->grayagain = o; - black2gray(o); - traversestack(g, th); - return sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * th->size_ci; - } - case LUA_TPROTO: { - Proto *p = gco2p(o); - g->gray = p->gclist; - traverseproto(g, p); - return sizeof(Proto) + sizeof(Instruction) * p->sizecode + - sizeof(Proto *) * p->sizep + - sizeof(TValue) * p->sizek + - sizeof(int) * p->sizelineinfo + - sizeof(LocVar) * p->sizelocvars + - sizeof(TString *) * p->sizeupvalues; - } - default: lua_assert(0); return 0; - } -} - - -static size_t propagateall (global_State *g) { - size_t m = 0; - while (g->gray) m += propagatemark(g); - return m; -} - - -/* -** The next function tells whether a key or value can be cleared from -** a weak table. Non-collectable objects are never removed from weak -** tables. Strings behave as `values', so are never removed too. for -** other objects: if really collected, cannot keep them; for userdata -** being finalized, keep them in keys, but not in values -*/ -static int iscleared (const TValue *o, int iskey) { - if (!iscollectable(o)) return 0; - if (ttisstring(o)) { - stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ - return 0; - } - return iswhite(gcvalue(o)) || - (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); -} - - -/* -** clear collected entries from weaktables -*/ -static void cleartable (GCObject *l) { - while (l) { - Table *h = gco2h(l); - int i = h->sizearray; - lua_assert(testbit(h->marked, VALUEWEAKBIT) || - testbit(h->marked, KEYWEAKBIT)); - if (testbit(h->marked, VALUEWEAKBIT)) { - while (i--) { - TValue *o = &h->array[i]; - if (iscleared(o, 0)) /* value was collected? */ - setnilvalue(o); /* remove value */ - } - } - i = sizenode(h); - while (i--) { - Node *n = gnode(h, i); - if (!ttisnil(gval(n)) && /* non-empty entry? */ - (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* remove entry from table */ - } - } - l = h->gclist; - } -} - - -static void freeobj (lua_State *L, GCObject *o) { - switch (o->gch.tt) { - case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; - case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; - case LUA_TTABLE: luaH_free(L, gco2h(o)); break; - case LUA_TTHREAD: { - lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); - luaE_freethread(L, gco2th(o)); - break; - } - case LUA_TSTRING: { - G(L)->strt.nuse--; - luaM_freemem(L, o, sizestring(gco2ts(o))); - break; - } - case LUA_TUSERDATA: { - luaM_freemem(L, o, sizeudata(gco2u(o))); - break; - } - default: lua_assert(0); - } -} - - - -#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) - - -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { - GCObject *curr; - global_State *g = G(L); - int deadmask = otherwhite(g); - while ((curr = *p) != NULL && count-- > 0) { - if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */ - sweepwholelist(L, &gco2th(curr)->openupval); - if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */ - lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); - makewhite(g, curr); /* make it white (for next cycle) */ - p = &curr->gch.next; - } - else { /* must erase `curr' */ - lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); - *p = curr->gch.next; - if (curr == g->rootgc) /* is the first element of the list? */ - g->rootgc = curr->gch.next; /* adjust first */ - freeobj(L, curr); - } - } - return p; -} - - -static void checkSizes (lua_State *L) { - global_State *g = G(L); - /* check size of string hash */ - if (g->strt.nuse < cast(lu_int32, g->strt.size/4) && - g->strt.size > MINSTRTABSIZE*2) - luaS_resize(L, g->strt.size/2); /* table is too big */ - /* check size of buffer */ - if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */ - size_t newsize = luaZ_sizebuffer(&g->buff) / 2; - luaZ_resizebuffer(L, &g->buff, newsize); - } -} - - -static void GCTM (lua_State *L) { - global_State *g = G(L); - GCObject *o = g->tmudata->gch.next; /* get first element */ - Udata *udata = rawgco2u(o); - const TValue *tm; - /* remove udata from `tmudata' */ - if (o == g->tmudata) /* last element? */ - g->tmudata = NULL; - else - g->tmudata->gch.next = udata->uv.next; - udata->uv.next = g->mainthread->next; /* return it to `root' list */ - g->mainthread->next = o; - makewhite(g, o); - tm = fasttm(L, udata->uv.metatable, TM_GC); - if (tm != NULL) { - lu_byte oldah = L->allowhook; - lu_mem oldt = g->GCthreshold; - L->allowhook = 0; /* stop debug hooks during GC tag method */ - g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ - setobj2s(L, L->top, tm); - setuvalue(L, L->top+1, udata); - L->top += 2; - luaD_call(L, L->top - 2, 0); - L->allowhook = oldah; /* restore hooks */ - g->GCthreshold = oldt; /* restore threshold */ - } -} - - -/* -** Call all GC tag methods -*/ -void luaC_callGCTM (lua_State *L) { - while (G(L)->tmudata) - GCTM(L); -} - - -void luaC_freeall (lua_State *L) { - global_State *g = G(L); - int i; - g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */ - sweepwholelist(L, &g->rootgc); - for (i = 0; i < g->strt.size; i++) /* free all string lists */ - sweepwholelist(L, &g->strt.hash[i]); -} - - -static void markmt (global_State *g) { - int i; - for (i=0; imt[i]) markobject(g, g->mt[i]); -} - - -/* mark root set */ -static void markroot (lua_State *L) { - global_State *g = G(L); - g->gray = NULL; - g->grayagain = NULL; - g->weak = NULL; - markobject(g, g->mainthread); - /* make global table be traversed before main stack */ - markvalue(g, gt(g->mainthread)); - markvalue(g, registry(L)); - markmt(g); - g->gcstate = GCSpropagate; -} - - -static void remarkupvals (global_State *g) { - UpVal *uv; - for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - if (isgray(obj2gco(uv))) - markvalue(g, uv->v); - } -} - - -static void atomic (lua_State *L) { - global_State *g = G(L); - size_t udsize; /* total size of userdata to be finalized */ - /* remark occasional upvalues of (maybe) dead threads */ - remarkupvals(g); - /* traverse objects cautch by write barrier and by 'remarkupvals' */ - propagateall(g); - /* remark weak tables */ - g->gray = g->weak; - g->weak = NULL; - lua_assert(!iswhite(obj2gco(g->mainthread))); - markobject(g, L); /* mark running thread */ - markmt(g); /* mark basic metatables (again) */ - propagateall(g); - /* remark gray again */ - g->gray = g->grayagain; - g->grayagain = NULL; - propagateall(g); - udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */ - marktmu(g); /* mark `preserved' userdata */ - udsize += propagateall(g); /* remark, to propagate `preserveness' */ - cleartable(g->weak); /* remove collected objects from weak tables */ - /* flip current white */ - g->currentwhite = cast_byte(otherwhite(g)); - g->sweepstrgc = 0; - g->sweepgc = &g->rootgc; - g->gcstate = GCSsweepstring; - g->estimate = g->totalbytes - udsize; /* first estimate */ -} - - -static l_mem singlestep (lua_State *L) { - global_State *g = G(L); - /*lua_checkmemory(L);*/ - switch (g->gcstate) { - case GCSpause: { - markroot(L); /* start a new collection */ - return 0; - } - case GCSpropagate: { - if (g->gray) - return propagatemark(g); - else { /* no more `gray' objects */ - atomic(L); /* finish mark phase */ - return 0; - } - } - case GCSsweepstring: { - lu_mem old = g->totalbytes; - sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]); - if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */ - g->gcstate = GCSsweep; /* end sweep-string phase */ - lua_assert(old >= g->totalbytes); - g->estimate -= old - g->totalbytes; - return GCSWEEPCOST; - } - case GCSsweep: { - lu_mem old = g->totalbytes; - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); - if (*g->sweepgc == NULL) { /* nothing more to sweep? */ - checkSizes(L); - g->gcstate = GCSfinalize; /* end sweep phase */ - } - lua_assert(old >= g->totalbytes); - g->estimate -= old - g->totalbytes; - return GCSWEEPMAX*GCSWEEPCOST; - } - case GCSfinalize: { - if (g->tmudata) { - GCTM(L); - if (g->estimate > GCFINALIZECOST) - g->estimate -= GCFINALIZECOST; - return GCFINALIZECOST; - } - else { - g->gcstate = GCSpause; /* end collection */ - g->gcdept = 0; - return 0; - } - } - default: lua_assert(0); return 0; - } -} - - -void luaC_step (lua_State *L) { - global_State *g = G(L); - l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul; - if (lim == 0) - lim = (MAX_LUMEM-1)/2; /* no limit */ - g->gcdept += g->totalbytes - g->GCthreshold; - do { - lim -= singlestep(L); - if (g->gcstate == GCSpause) - break; - } while (lim > 0); - if (g->gcstate != GCSpause) { - if (g->gcdept < GCSTEPSIZE) - g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/ - else { - g->gcdept -= GCSTEPSIZE; - g->GCthreshold = g->totalbytes; - } - } - else { - setthreshold(g); - } -} - - -void luaC_fullgc (lua_State *L) { - global_State *g = G(L); - if (g->gcstate <= GCSpropagate) { - /* reset sweep marks to sweep all elements (returning them to white) */ - g->sweepstrgc = 0; - g->sweepgc = &g->rootgc; - /* reset other collector lists */ - g->gray = NULL; - g->grayagain = NULL; - g->weak = NULL; - g->gcstate = GCSsweepstring; - } - lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate); - /* finish any pending sweep phase */ - while (g->gcstate != GCSfinalize) { - lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); - singlestep(L); - } - markroot(L); - while (g->gcstate != GCSpause) { - singlestep(L); - } - setthreshold(g); -} - - -void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { - global_State *g = G(L); - lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); - lua_assert(ttype(&o->gch) != LUA_TTABLE); - /* must keep invariant? */ - if (g->gcstate == GCSpropagate) - reallymarkobject(g, v); /* restore invariant */ - else /* don't mind */ - makewhite(g, o); /* mark as white just to avoid other barriers */ -} - - -void luaC_barrierback (lua_State *L, Table *t) { - global_State *g = G(L); - GCObject *o = obj2gco(t); - lua_assert(isblack(o) && !isdead(g, o)); - lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); - black2gray(o); /* make table gray (again) */ - t->gclist = g->grayagain; - g->grayagain = o; -} - - -void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { - global_State *g = G(L); - o->gch.next = g->rootgc; - g->rootgc = o; - o->gch.marked = luaC_white(g); - o->gch.tt = tt; -} - - -void luaC_linkupval (lua_State *L, UpVal *uv) { - global_State *g = G(L); - GCObject *o = obj2gco(uv); - o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ - g->rootgc = o; - if (isgray(o)) { - if (g->gcstate == GCSpropagate) { - gray2black(o); /* closed upvalues need barrier */ - luaC_barrier(L, uv, uv->v); - } - else { /* sweep phase: sweep it (turning it into white) */ - makewhite(g, o); - lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); - } - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.h deleted file mode 100644 index 5a8dc60..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lgc.h +++ /dev/null @@ -1,110 +0,0 @@ -/* -** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#ifndef lgc_h -#define lgc_h - - -#include "lobject.h" - - -/* -** Possible states of the Garbage Collector -*/ -#define GCSpause 0 -#define GCSpropagate 1 -#define GCSsweepstring 2 -#define GCSsweep 3 -#define GCSfinalize 4 - - -/* -** some userful bit tricks -*/ -#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) -#define setbits(x,m) ((x) |= (m)) -#define testbits(x,m) ((x) & (m)) -#define bitmask(b) (1<<(b)) -#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) -#define l_setbit(x,b) setbits(x, bitmask(b)) -#define resetbit(x,b) resetbits(x, bitmask(b)) -#define testbit(x,b) testbits(x, bitmask(b)) -#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) -#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) -#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) - - - -/* -** Layout for bit use in `marked' field: -** bit 0 - object is white (type 0) -** bit 1 - object is white (type 1) -** bit 2 - object is black -** bit 3 - for userdata: has been finalized -** bit 3 - for tables: has weak keys -** bit 4 - for tables: has weak values -** bit 5 - object is fixed (should not be collected) -** bit 6 - object is "super" fixed (only the main thread) -*/ - - -#define WHITE0BIT 0 -#define WHITE1BIT 1 -#define BLACKBIT 2 -#define FINALIZEDBIT 3 -#define KEYWEAKBIT 3 -#define VALUEWEAKBIT 4 -#define FIXEDBIT 5 -#define SFIXEDBIT 6 -#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) - - -#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) -#define isblack(x) testbit((x)->gch.marked, BLACKBIT) -#define isgray(x) (!isblack(x) && !iswhite(x)) - -#define otherwhite(g) (g->currentwhite ^ WHITEBITS) -#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) - -#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) - -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) - -#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) - - -#define luaC_checkGC(L) { \ - condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ - if (G(L)->totalbytes >= G(L)->GCthreshold) \ - luaC_step(L); } - - -#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ - luaC_barrierf(L,obj2gco(p),gcvalue(v)); } - -#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ - luaC_barrierback(L,t); } - -#define luaC_objbarrier(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ - luaC_barrierf(L,obj2gco(p),obj2gco(o)); } - -#define luaC_objbarriert(L,t,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } - -LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); -LUAI_FUNC void luaC_callGCTM (lua_State *L); -LUAI_FUNC void luaC_freeall (lua_State *L); -LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_fullgc (lua_State *L); -LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); -LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); -LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/linit.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/linit.c deleted file mode 100644 index c1f90df..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/linit.c +++ /dev/null @@ -1,38 +0,0 @@ -/* -** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ -** Initialization of libraries for lua.c -** See Copyright Notice in lua.h -*/ - - -#define linit_c -#define LUA_LIB - -#include "lua.h" - -#include "lualib.h" -#include "lauxlib.h" - - -static const luaL_Reg lualibs[] = { - {"", luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_DBLIBNAME, luaopen_debug}, - {NULL, NULL} -}; - - -LUALIB_API void luaL_openlibs (lua_State *L) { - const luaL_Reg *lib = lualibs; - for (; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/liolib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/liolib.c deleted file mode 100644 index 649f9a5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/liolib.c +++ /dev/null @@ -1,556 +0,0 @@ -/* -** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $ -** Standard I/O (and system) library -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include - -#define liolib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - -#define IO_INPUT 1 -#define IO_OUTPUT 2 - - -static const char *const fnames[] = {"input", "output"}; - - -static int pushresult (lua_State *L, int i, const char *filename) { - int en = errno; /* calls to Lua API may change this value */ - if (i) { - lua_pushboolean(L, 1); - return 1; - } - else { - lua_pushnil(L); - if (filename) - lua_pushfstring(L, "%s: %s", filename, strerror(en)); - else - lua_pushfstring(L, "%s", strerror(en)); - lua_pushinteger(L, en); - return 3; - } -} - - -static void fileerror (lua_State *L, int arg, const char *filename) { - lua_pushfstring(L, "%s: %s", filename, strerror(errno)); - luaL_argerror(L, arg, lua_tostring(L, -1)); -} - - -#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) - - -static int io_type (lua_State *L) { - void *ud; - luaL_checkany(L, 1); - ud = lua_touserdata(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); - if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) - lua_pushnil(L); /* not a file */ - else if (*((FILE **)ud) == NULL) - lua_pushliteral(L, "closed file"); - else - lua_pushliteral(L, "file"); - return 1; -} - - -static FILE *tofile (lua_State *L) { - FILE **f = tofilep(L); - if (*f == NULL) - luaL_error(L, "attempt to use a closed file"); - return *f; -} - - - -/* -** When creating file handles, always creates a `closed' file handle -** before opening the actual file; so, if there is a memory error, the -** file is not left opened. -*/ -static FILE **newfile (lua_State *L) { - FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); - *pf = NULL; /* file handle is currently `closed' */ - luaL_getmetatable(L, LUA_FILEHANDLE); - lua_setmetatable(L, -2); - return pf; -} - - -/* -** function to (not) close the standard files stdin, stdout, and stderr -*/ -static int io_noclose (lua_State *L) { - lua_pushnil(L); - lua_pushliteral(L, "cannot close standard file"); - return 2; -} - - -/* -** function to close 'popen' files -*/ -static int io_pclose (lua_State *L) { - FILE **p = tofilep(L); - int ok = lua_pclose(L, *p); - *p = NULL; - return pushresult(L, ok, NULL); -} - - -/* -** function to close regular files -*/ -static int io_fclose (lua_State *L) { - FILE **p = tofilep(L); - int ok = (fclose(*p) == 0); - *p = NULL; - return pushresult(L, ok, NULL); -} - - -static int aux_close (lua_State *L) { - lua_getfenv(L, 1); - lua_getfield(L, -1, "__close"); - return (lua_tocfunction(L, -1))(L); -} - - -static int io_close (lua_State *L) { - if (lua_isnone(L, 1)) - lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); - tofile(L); /* make sure argument is a file */ - return aux_close(L); -} - - -static int io_gc (lua_State *L) { - FILE *f = *tofilep(L); - /* ignore closed files */ - if (f != NULL) - aux_close(L); - return 0; -} - - -static int io_tostring (lua_State *L) { - FILE *f = *tofilep(L); - if (f == NULL) - lua_pushliteral(L, "file (closed)"); - else - lua_pushfstring(L, "file (%p)", f); - return 1; -} - - -static int io_open (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - FILE **pf = newfile(L); - *pf = fopen(filename, mode); - return (*pf == NULL) ? pushresult(L, 0, filename) : 1; -} - - -/* -** this function has a separated environment, which defines the -** correct __close for 'popen' files -*/ -static int io_popen (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - FILE **pf = newfile(L); - *pf = lua_popen(L, filename, mode); - return (*pf == NULL) ? pushresult(L, 0, filename) : 1; -} - - -static int io_tmpfile (lua_State *L) { - FILE **pf = newfile(L); - *pf = tmpfile(); - return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; -} - - -static FILE *getiofile (lua_State *L, int findex) { - FILE *f; - lua_rawgeti(L, LUA_ENVIRONINDEX, findex); - f = *(FILE **)lua_touserdata(L, -1); - if (f == NULL) - luaL_error(L, "standard %s file is closed", fnames[findex - 1]); - return f; -} - - -static int g_iofile (lua_State *L, int f, const char *mode) { - if (!lua_isnoneornil(L, 1)) { - const char *filename = lua_tostring(L, 1); - if (filename) { - FILE **pf = newfile(L); - *pf = fopen(filename, mode); - if (*pf == NULL) - fileerror(L, 1, filename); - } - else { - tofile(L); /* check that it's a valid file handle */ - lua_pushvalue(L, 1); - } - lua_rawseti(L, LUA_ENVIRONINDEX, f); - } - /* return current value */ - lua_rawgeti(L, LUA_ENVIRONINDEX, f); - return 1; -} - - -static int io_input (lua_State *L) { - return g_iofile(L, IO_INPUT, "r"); -} - - -static int io_output (lua_State *L) { - return g_iofile(L, IO_OUTPUT, "w"); -} - - -static int io_readline (lua_State *L); - - -static void aux_lines (lua_State *L, int idx, int toclose) { - lua_pushvalue(L, idx); - lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_pushcclosure(L, io_readline, 2); -} - - -static int f_lines (lua_State *L) { - tofile(L); /* check that it's a valid file handle */ - aux_lines(L, 1, 0); - return 1; -} - - -static int io_lines (lua_State *L) { - if (lua_isnoneornil(L, 1)) { /* no arguments? */ - /* will iterate over default input */ - lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); - return f_lines(L); - } - else { - const char *filename = luaL_checkstring(L, 1); - FILE **pf = newfile(L); - *pf = fopen(filename, "r"); - if (*pf == NULL) - fileerror(L, 1, filename); - aux_lines(L, lua_gettop(L), 1); - return 1; - } -} - - -/* -** {====================================================== -** READ -** ======================================================= -*/ - - -static int read_number (lua_State *L, FILE *f) { - lua_Number d; - if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { - lua_pushnumber(L, d); - return 1; - } - else { - lua_pushnil(L); /* "result" to be removed */ - return 0; /* read fails */ - } -} - - -static int test_eof (lua_State *L, FILE *f) { - int c = getc(f); - ungetc(c, f); - lua_pushlstring(L, NULL, 0); - return (c != EOF); -} - - -static int read_line (lua_State *L, FILE *f) { - luaL_Buffer b; - luaL_buffinit(L, &b); - for (;;) { - size_t l; - char *p = luaL_prepbuffer(&b); - if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ - luaL_pushresult(&b); /* close buffer */ - return (lua_objlen(L, -1) > 0); /* check whether read something */ - } - l = strlen(p); - if (l == 0 || p[l-1] != '\n') - luaL_addsize(&b, l); - else { - luaL_addsize(&b, l - 1); /* do not include `eol' */ - luaL_pushresult(&b); /* close buffer */ - return 1; /* read at least an `eol' */ - } - } -} - - -static int read_chars (lua_State *L, FILE *f, size_t n) { - size_t rlen; /* how much to read */ - size_t nr; /* number of chars actually read */ - luaL_Buffer b; - luaL_buffinit(L, &b); - rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ - do { - char *p = luaL_prepbuffer(&b); - if (rlen > n) rlen = n; /* cannot read more than asked */ - nr = fread(p, sizeof(char), rlen, f); - luaL_addsize(&b, nr); - n -= nr; /* still have to read `n' chars */ - } while (n > 0 && nr == rlen); /* until end of count or eof */ - luaL_pushresult(&b); /* close buffer */ - return (n == 0 || lua_objlen(L, -1) > 0); -} - - -static int g_read (lua_State *L, FILE *f, int first) { - int nargs = lua_gettop(L) - 1; - int success; - int n; - clearerr(f); - if (nargs == 0) { /* no arguments? */ - success = read_line(L, f); - n = first+1; /* to return 1 result */ - } - else { /* ensure stack space for all results and for auxlib's buffer */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - success = 1; - for (n = first; nargs-- && success; n++) { - if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)lua_tointeger(L, n); - success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); - } - else { - const char *p = lua_tostring(L, n); - luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); - switch (p[1]) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f); - break; - case 'a': /* file */ - read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, n, "invalid format"); - } - } - } - } - if (ferror(f)) - return pushresult(L, 0, NULL); - if (!success) { - lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ - } - return n - first; -} - - -static int io_read (lua_State *L) { - return g_read(L, getiofile(L, IO_INPUT), 1); -} - - -static int f_read (lua_State *L) { - return g_read(L, tofile(L), 2); -} - - -static int io_readline (lua_State *L) { - FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); - int sucess; - if (f == NULL) /* file is already closed? */ - luaL_error(L, "file is already closed"); - sucess = read_line(L, f); - if (ferror(f)) - return luaL_error(L, "%s", strerror(errno)); - if (sucess) return 1; - else { /* EOF */ - if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); - aux_close(L); /* close it */ - } - return 0; - } -} - -/* }====================================================== */ - - -static int g_write (lua_State *L, FILE *f, int arg) { - int nargs = lua_gettop(L) - 1; - int status = 1; - for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - status = status && - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; - } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); - } - } - return pushresult(L, status, NULL); -} - - -static int io_write (lua_State *L) { - return g_write(L, getiofile(L, IO_OUTPUT), 1); -} - - -static int f_write (lua_State *L) { - return g_write(L, tofile(L), 2); -} - - -static int f_seek (lua_State *L) { - static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; - static const char *const modenames[] = {"set", "cur", "end", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, "cur", modenames); - long offset = luaL_optlong(L, 3, 0); - op = fseek(f, offset, mode[op]); - if (op) - return pushresult(L, 0, NULL); /* error */ - else { - lua_pushinteger(L, ftell(f)); - return 1; - } -} - - -static int f_setvbuf (lua_State *L) { - static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; - static const char *const modenames[] = {"no", "full", "line", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, NULL, modenames); - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], sz); - return pushresult(L, res == 0, NULL); -} - - - -static int io_flush (lua_State *L) { - return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); -} - - -static int f_flush (lua_State *L) { - return pushresult(L, fflush(tofile(L)) == 0, NULL); -} - - -static const luaL_Reg iolib[] = { - {"close", io_close}, - {"flush", io_flush}, - {"input", io_input}, - {"lines", io_lines}, - {"open", io_open}, - {"output", io_output}, - {"popen", io_popen}, - {"read", io_read}, - {"tmpfile", io_tmpfile}, - {"type", io_type}, - {"write", io_write}, - {NULL, NULL} -}; - - -static const luaL_Reg flib[] = { - {"close", io_close}, - {"flush", f_flush}, - {"lines", f_lines}, - {"read", f_read}, - {"seek", f_seek}, - {"setvbuf", f_setvbuf}, - {"write", f_write}, - {"__gc", io_gc}, - {"__tostring", io_tostring}, - {NULL, NULL} -}; - - -static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_register(L, NULL, flib); /* file methods */ -} - - -static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { - *newfile(L) = f; - if (k > 0) { - lua_pushvalue(L, -1); - lua_rawseti(L, LUA_ENVIRONINDEX, k); - } - lua_pushvalue(L, -2); /* copy environment */ - lua_setfenv(L, -2); /* set it */ - lua_setfield(L, -3, fname); -} - - -static void newfenv (lua_State *L, lua_CFunction cls) { - lua_createtable(L, 0, 1); - lua_pushcfunction(L, cls); - lua_setfield(L, -2, "__close"); -} - - -LUALIB_API int luaopen_io (lua_State *L) { - createmeta(L); - /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ - newfenv(L, io_fclose); - lua_replace(L, LUA_ENVIRONINDEX); - /* open library */ - luaL_register(L, LUA_IOLIBNAME, iolib); - /* create (and set) default files */ - newfenv(L, io_noclose); /* close function for default files */ - createstdfile(L, stdin, IO_INPUT, "stdin"); - createstdfile(L, stdout, IO_OUTPUT, "stdout"); - createstdfile(L, stderr, 0, "stderr"); - lua_pop(L, 1); /* pop environment for default files */ - lua_getfield(L, -1, "popen"); - newfenv(L, io_pclose); /* create environment for 'popen' */ - lua_setfenv(L, -2); /* set fenv for 'popen' */ - lua_pop(L, 1); /* pop 'popen' */ - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.c deleted file mode 100644 index 88c6790..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.c +++ /dev/null @@ -1,463 +0,0 @@ -/* -** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define llex_c -#define LUA_CORE - -#include "lua.h" - -#include "ldo.h" -#include "llex.h" -#include "lobject.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lzio.h" - - - -#define next(ls) (ls->current = zgetc(ls->z)) - - - - -#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') - - -/* ORDER RESERVED */ -const char *const luaX_tokens [] = { - "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", "if", - "in", "local", "nil", "not", "or", "repeat", - "return", "then", "true", "until", "while", - "..", "...", "==", ">=", "<=", "~=", - "", "", "", "", - NULL -}; - - -#define save_and_next(ls) (save(ls, ls->current), next(ls)) - - -static void save (LexState *ls, int c) { - Mbuffer *b = ls->buff; - if (b->n + 1 > b->buffsize) { - size_t newsize; - if (b->buffsize >= MAX_SIZET/2) - luaX_lexerror(ls, "lexical element too long", 0); - newsize = b->buffsize * 2; - luaZ_resizebuffer(ls->L, b, newsize); - } - b->buffer[b->n++] = cast(char, c); -} - - -void luaX_init (lua_State *L) { - int i; - for (i=0; itsv.reserved = cast_byte(i+1); /* reserved word */ - } -} - - -#define MAXSRC 80 - - -const char *luaX_token2str (LexState *ls, int token) { - if (token < FIRST_RESERVED) { - lua_assert(token == cast(unsigned char, token)); - return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) : - luaO_pushfstring(ls->L, "%c", token); - } - else - return luaX_tokens[token-FIRST_RESERVED]; -} - - -static const char *txtToken (LexState *ls, int token) { - switch (token) { - case TK_NAME: - case TK_STRING: - case TK_NUMBER: - save(ls, '\0'); - return luaZ_buffer(ls->buff); - default: - return luaX_token2str(ls, token); - } -} - - -void luaX_lexerror (LexState *ls, const char *msg, int token) { - char buff[MAXSRC]; - luaO_chunkid(buff, getstr(ls->source), MAXSRC); - msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); - if (token) - luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token)); - luaD_throw(ls->L, LUA_ERRSYNTAX); -} - - -void luaX_syntaxerror (LexState *ls, const char *msg) { - luaX_lexerror(ls, msg, ls->t.token); -} - - -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { - lua_State *L = ls->L; - TString *ts = luaS_newlstr(L, str, l); - TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ - if (ttisnil(o)) { - setbvalue(o, 1); /* make sure `str' will not be collected */ - luaC_checkGC(L); - } - return ts; -} - - -static void inclinenumber (LexState *ls) { - int old = ls->current; - lua_assert(currIsNewline(ls)); - next(ls); /* skip `\n' or `\r' */ - if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip `\n\r' or `\r\n' */ - if (++ls->linenumber >= MAX_INT) - luaX_syntaxerror(ls, "chunk has too many lines"); -} - - -void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { - ls->decpoint = '.'; - ls->L = L; - ls->lookahead.token = TK_EOS; /* no look-ahead token */ - ls->z = z; - ls->fs = NULL; - ls->linenumber = 1; - ls->lastline = 1; - ls->source = source; - luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ - next(ls); /* read first char */ -} - - - -/* -** ======================================================= -** LEXICAL ANALYZER -** ======================================================= -*/ - - - -static int check_next (LexState *ls, const char *set) { - if (!strchr(set, ls->current)) - return 0; - save_and_next(ls); - return 1; -} - - -static void buffreplace (LexState *ls, char from, char to) { - size_t n = luaZ_bufflen(ls->buff); - char *p = luaZ_buffer(ls->buff); - while (n--) - if (p[n] == from) p[n] = to; -} - - -static void trydecpoint (LexState *ls, SemInfo *seminfo) { - /* format error: try to update decimal point separator */ - struct lconv *cv = localeconv(); - char old = ls->decpoint; - ls->decpoint = (cv ? cv->decimal_point[0] : '.'); - buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */ - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { - /* format error with correct decimal point: no more options */ - buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ - luaX_lexerror(ls, "malformed number", TK_NUMBER); - } -} - - -/* LUA_NUMBER */ -static void read_numeral (LexState *ls, SemInfo *seminfo) { - lua_assert(isdigit(ls->current)); - do { - save_and_next(ls); - } while (isdigit(ls->current) || ls->current == '.'); - if (check_next(ls, "Ee")) /* `E'? */ - check_next(ls, "+-"); /* optional exponent sign */ - while (isalnum(ls->current) || ls->current == '_') - save_and_next(ls); - save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ - trydecpoint(ls, seminfo); /* try to update decimal point separator */ -} - - -static int skip_sep (LexState *ls) { - int count = 0; - int s = ls->current; - lua_assert(s == '[' || s == ']'); - save_and_next(ls); - while (ls->current == '=') { - save_and_next(ls); - count++; - } - return (ls->current == s) ? count : (-count) - 1; -} - - -static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { - int cont = 0; - (void)(cont); /* avoid warnings when `cont' is not used */ - save_and_next(ls); /* skip 2nd `[' */ - if (currIsNewline(ls)) /* string starts with a newline? */ - inclinenumber(ls); /* skip it */ - for (;;) { - switch (ls->current) { - case EOZ: - luaX_lexerror(ls, (seminfo) ? "unfinished long string" : - "unfinished long comment", TK_EOS); - break; /* to avoid warnings */ -#if defined(LUA_COMPAT_LSTR) - case '[': { - if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd `[' */ - cont++; -#if LUA_COMPAT_LSTR == 1 - if (sep == 0) - luaX_lexerror(ls, "nesting of [[...]] is deprecated", '['); -#endif - } - break; - } -#endif - case ']': { - if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd `]' */ -#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2 - cont--; - if (sep == 0 && cont >= 0) break; -#endif - goto endloop; - } - break; - } - case '\n': - case '\r': { - save(ls, '\n'); - inclinenumber(ls); - if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ - break; - } - default: { - if (seminfo) save_and_next(ls); - else next(ls); - } - } - } endloop: - if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), - luaZ_bufflen(ls->buff) - 2*(2 + sep)); -} - - -static void read_string (LexState *ls, int del, SemInfo *seminfo) { - save_and_next(ls); - while (ls->current != del) { - switch (ls->current) { - case EOZ: - luaX_lexerror(ls, "unfinished string", TK_EOS); - continue; /* to avoid warnings */ - case '\n': - case '\r': - luaX_lexerror(ls, "unfinished string", TK_STRING); - continue; /* to avoid warnings */ - case '\\': { - int c; - next(ls); /* do not save the `\' */ - switch (ls->current) { - case 'a': c = '\a'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 'r': c = '\r'; break; - case 't': c = '\t'; break; - case 'v': c = '\v'; break; - case '\n': /* go through */ - case '\r': save(ls, '\n'); inclinenumber(ls); continue; - case EOZ: continue; /* will raise an error next loop */ - default: { - if (!isdigit(ls->current)) - save_and_next(ls); /* handles \\, \", \', and \? */ - else { /* \xxx */ - int i = 0; - c = 0; - do { - c = 10*c + (ls->current-'0'); - next(ls); - } while (++i<3 && isdigit(ls->current)); - if (c > UCHAR_MAX) - luaX_lexerror(ls, "escape sequence too large", TK_STRING); - save(ls, c); - } - continue; - } - } - save(ls, c); - next(ls); - continue; - } - default: - save_and_next(ls); - } - } - save_and_next(ls); /* skip delimiter */ - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, - luaZ_bufflen(ls->buff) - 2); -} - - -static int llex (LexState *ls, SemInfo *seminfo) { - luaZ_resetbuffer(ls->buff); - for (;;) { - switch (ls->current) { - case '\n': - case '\r': { - inclinenumber(ls); - continue; - } - case '-': { - next(ls); - if (ls->current != '-') return '-'; - /* else is a comment */ - next(ls); - if (ls->current == '[') { - int sep = skip_sep(ls); - luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ - if (sep >= 0) { - read_long_string(ls, NULL, sep); /* long comment */ - luaZ_resetbuffer(ls->buff); - continue; - } - } - /* else short comment */ - while (!currIsNewline(ls) && ls->current != EOZ) - next(ls); - continue; - } - case '[': { - int sep = skip_sep(ls); - if (sep >= 0) { - read_long_string(ls, seminfo, sep); - return TK_STRING; - } - else if (sep == -1) return '['; - else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); - } - case '=': { - next(ls); - if (ls->current != '=') return '='; - else { next(ls); return TK_EQ; } - } - case '<': { - next(ls); - if (ls->current != '=') return '<'; - else { next(ls); return TK_LE; } - } - case '>': { - next(ls); - if (ls->current != '=') return '>'; - else { next(ls); return TK_GE; } - } - case '~': { - next(ls); - if (ls->current != '=') return '~'; - else { next(ls); return TK_NE; } - } - case '"': - case '\'': { - read_string(ls, ls->current, seminfo); - return TK_STRING; - } - case '.': { - save_and_next(ls); - if (check_next(ls, ".")) { - if (check_next(ls, ".")) - return TK_DOTS; /* ... */ - else return TK_CONCAT; /* .. */ - } - else if (!isdigit(ls->current)) return '.'; - else { - read_numeral(ls, seminfo); - return TK_NUMBER; - } - } - case EOZ: { - return TK_EOS; - } - default: { - if (isspace(ls->current)) { - lua_assert(!currIsNewline(ls)); - next(ls); - continue; - } - else if (isdigit(ls->current)) { - read_numeral(ls, seminfo); - return TK_NUMBER; - } - else if (isalpha(ls->current) || ls->current == '_') { - /* identifier or reserved word */ - TString *ts; - do { - save_and_next(ls); - } while (isalnum(ls->current) || ls->current == '_'); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - if (ts->tsv.reserved > 0) /* reserved word? */ - return ts->tsv.reserved - 1 + FIRST_RESERVED; - else { - seminfo->ts = ts; - return TK_NAME; - } - } - else { - int c = ls->current; - next(ls); - return c; /* single-char tokens (+ - / ...) */ - } - } - } - } -} - - -void luaX_next (LexState *ls) { - ls->lastline = ls->linenumber; - if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ - ls->t = ls->lookahead; /* use this one */ - ls->lookahead.token = TK_EOS; /* and discharge it */ - } - else - ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ -} - - -void luaX_lookahead (LexState *ls) { - lua_assert(ls->lookahead.token == TK_EOS); - ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.h deleted file mode 100644 index a9201ce..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llex.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - -#ifndef llex_h -#define llex_h - -#include "lobject.h" -#include "lzio.h" - - -#define FIRST_RESERVED 257 - -/* maximum length of a reserved word */ -#define TOKEN_LEN (sizeof("function")/sizeof(char)) - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER RESERVED" -*/ -enum RESERVED { - /* terminal symbols denoted by reserved words */ - TK_AND = FIRST_RESERVED, TK_BREAK, - TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, - TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, - /* other terminal symbols */ - TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, - TK_NAME, TK_STRING, TK_EOS -}; - -/* number of reserved words */ -#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) - - -/* array with token `names' */ -LUAI_DATA const char *const luaX_tokens []; - - -typedef union { - lua_Number r; - TString *ts; -} SemInfo; /* semantics information */ - - -typedef struct Token { - int token; - SemInfo seminfo; -} Token; - - -typedef struct LexState { - int current; /* current character (charint) */ - int linenumber; /* input line counter */ - int lastline; /* line of last token `consumed' */ - Token t; /* current token */ - Token lookahead; /* look ahead token */ - struct FuncState *fs; /* `FuncState' is private to the parser */ - struct lua_State *L; - ZIO *z; /* input stream */ - Mbuffer *buff; /* buffer for tokens */ - TString *source; /* current source name */ - char decpoint; /* locale decimal point */ -} LexState; - - -LUAI_FUNC void luaX_init (lua_State *L); -LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, - TString *source); -LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); -LUAI_FUNC void luaX_next (LexState *ls); -LUAI_FUNC void luaX_lookahead (LexState *ls); -LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); -LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); -LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llimits.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llimits.h deleted file mode 100644 index ca8dcb7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/llimits.h +++ /dev/null @@ -1,128 +0,0 @@ -/* -** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ -** Limits, basic types, and some other `installation-dependent' definitions -** See Copyright Notice in lua.h -*/ - -#ifndef llimits_h -#define llimits_h - - -#include -#include - - -#include "lua.h" - - -typedef LUAI_UINT32 lu_int32; - -typedef LUAI_UMEM lu_mem; - -typedef LUAI_MEM l_mem; - - - -/* chars used as small naturals (so that `char' is reserved for characters) */ -typedef unsigned char lu_byte; - - -#define MAX_SIZET ((size_t)(~(size_t)0)-2) - -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) - - -#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ - -/* -** conversion of pointer to integer -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value -*/ -#define IntPoint(p) ((unsigned int)(lu_mem)(p)) - - - -/* type to ensure maximum alignment */ -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; - - -/* result of a `usual argument conversion' over lua_Number */ -typedef LUAI_UACNUMBER l_uacNumber; - - -/* internal assertions for in-house debugging */ -#ifdef lua_assert - -#define check_exp(c,e) (lua_assert(c), (e)) -#define api_check(l,e) lua_assert(e) - -#else - -#define lua_assert(c) ((void)0) -#define check_exp(c,e) (e) -#define api_check luai_apicheck - -#endif - - -#ifndef UNUSED -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ -#endif - - -#ifndef cast -#define cast(t, exp) ((t)(exp)) -#endif - -#define cast_byte(i) cast(lu_byte, (i)) -#define cast_num(i) cast(lua_Number, (i)) -#define cast_int(i) cast(int, (i)) - - - -/* -** type for virtual-machine instructions -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) -*/ -typedef lu_int32 Instruction; - - - -/* maximum stack for a Lua function */ -#define MAXSTACK 250 - - - -/* minimum size for the string table (must be power of 2) */ -#ifndef MINSTRTABSIZE -#define MINSTRTABSIZE 32 -#endif - - -/* minimum size for string buffer */ -#ifndef LUA_MINBUFFER -#define LUA_MINBUFFER 32 -#endif - - -#ifndef lua_lock -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif - -#ifndef luai_threadyield -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} -#endif - - -/* -** macro to control inclusion of some hard tests on stack reallocation -*/ -#ifndef HARDSTACKTESTS -#define condhardstacktests(x) ((void)0) -#else -#define condhardstacktests(x) x -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmathlib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmathlib.c deleted file mode 100644 index 441fbf7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmathlib.c +++ /dev/null @@ -1,263 +0,0 @@ -/* -** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ -** Standard mathematical library -** See Copyright Notice in lua.h -*/ - - -#include -#include - -#define lmathlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#undef PI -#define PI (3.14159265358979323846) -#define RADIANS_PER_DEGREE (PI/180.0) - - - -static int math_abs (lua_State *L) { - lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sin (lua_State *L) { - lua_pushnumber(L, sin(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sinh (lua_State *L) { - lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); - return 1; -} - -static int math_cos (lua_State *L) { - lua_pushnumber(L, cos(luaL_checknumber(L, 1))); - return 1; -} - -static int math_cosh (lua_State *L) { - lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tan (lua_State *L) { - lua_pushnumber(L, tan(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tanh (lua_State *L) { - lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); - return 1; -} - -static int math_asin (lua_State *L) { - lua_pushnumber(L, asin(luaL_checknumber(L, 1))); - return 1; -} - -static int math_acos (lua_State *L) { - lua_pushnumber(L, acos(luaL_checknumber(L, 1))); - return 1; -} - -static int math_atan (lua_State *L) { - lua_pushnumber(L, atan(luaL_checknumber(L, 1))); - return 1; -} - -static int math_atan2 (lua_State *L) { - lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); - return 1; -} - -static int math_ceil (lua_State *L) { - lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); - return 1; -} - -static int math_floor (lua_State *L) { - lua_pushnumber(L, floor(luaL_checknumber(L, 1))); - return 1; -} - -static int math_fmod (lua_State *L) { - lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); - return 1; -} - -static int math_modf (lua_State *L) { - double ip; - double fp = modf(luaL_checknumber(L, 1), &ip); - lua_pushnumber(L, ip); - lua_pushnumber(L, fp); - return 2; -} - -static int math_sqrt (lua_State *L) { - lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); - return 1; -} - -static int math_pow (lua_State *L) { - lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); - return 1; -} - -static int math_log (lua_State *L) { - lua_pushnumber(L, log(luaL_checknumber(L, 1))); - return 1; -} - -static int math_log10 (lua_State *L) { - lua_pushnumber(L, log10(luaL_checknumber(L, 1))); - return 1; -} - -static int math_exp (lua_State *L) { - lua_pushnumber(L, exp(luaL_checknumber(L, 1))); - return 1; -} - -static int math_deg (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); - return 1; -} - -static int math_rad (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); - return 1; -} - -static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); - return 2; -} - -static int math_ldexp (lua_State *L) { - lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); - return 1; -} - - - -static int math_min (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmin = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d < dmin) - dmin = d; - } - lua_pushnumber(L, dmin); - return 1; -} - - -static int math_max (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmax = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d > dmax) - dmax = d; - } - lua_pushnumber(L, dmax); - return 1; -} - - -static int math_random (lua_State *L) { - /* the `%' avoids the (rare) case of r==1, and is needed also because on - some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ - lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; - switch (lua_gettop(L)) { /* check number of arguments */ - case 0: { /* no arguments */ - lua_pushnumber(L, r); /* Number between 0 and 1 */ - break; - } - case 1: { /* only upper limit */ - int u = luaL_checkint(L, 1); - luaL_argcheck(L, 1<=u, 1, "interval is empty"); - lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ - break; - } - case 2: { /* lower and upper limits */ - int l = luaL_checkint(L, 1); - int u = luaL_checkint(L, 2); - luaL_argcheck(L, l<=u, 2, "interval is empty"); - lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ - break; - } - default: return luaL_error(L, "wrong number of arguments"); - } - return 1; -} - - -static int math_randomseed (lua_State *L) { - srand(luaL_checkint(L, 1)); - return 0; -} - - -static const luaL_Reg mathlib[] = { - {"abs", math_abs}, - {"acos", math_acos}, - {"asin", math_asin}, - {"atan2", math_atan2}, - {"atan", math_atan}, - {"ceil", math_ceil}, - {"cosh", math_cosh}, - {"cos", math_cos}, - {"deg", math_deg}, - {"exp", math_exp}, - {"floor", math_floor}, - {"fmod", math_fmod}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, - {"log10", math_log10}, - {"log", math_log}, - {"max", math_max}, - {"min", math_min}, - {"modf", math_modf}, - {"pow", math_pow}, - {"rad", math_rad}, - {"random", math_random}, - {"randomseed", math_randomseed}, - {"sinh", math_sinh}, - {"sin", math_sin}, - {"sqrt", math_sqrt}, - {"tanh", math_tanh}, - {"tan", math_tan}, - {NULL, NULL} -}; - - -/* -** Open math library -*/ -LUALIB_API int luaopen_math (lua_State *L) { - luaL_register(L, LUA_MATHLIBNAME, mathlib); - lua_pushnumber(L, PI); - lua_setfield(L, -2, "pi"); - lua_pushnumber(L, HUGE_VAL); - lua_setfield(L, -2, "huge"); -#if defined(LUA_COMPAT_MOD) - lua_getfield(L, -1, "fmod"); - lua_setfield(L, -2, "mod"); -#endif - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.c deleted file mode 100644 index ae7d8c9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.c +++ /dev/null @@ -1,86 +0,0 @@ -/* -** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - - -#include - -#define lmem_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -/* -** About the realloc function: -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); -** (`osize' is the old size, `nsize' is the new size) -** -** Lua ensures that (ptr == NULL) iff (osize == 0). -** -** * frealloc(ud, NULL, 0, x) creates a new block of size `x' -** -** * frealloc(ud, p, x, 0) frees the block `p' -** (in this specific case, frealloc must return NULL). -** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ANSI C) -** -** frealloc returns NULL if it cannot create or reallocate the area -** (any reallocation to an equal or smaller size cannot fail!) -*/ - - - -#define MINSIZEARRAY 4 - - -void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, - int limit, const char *errormsg) { - void *newblock; - int newsize; - if (*size >= limit/2) { /* cannot double it? */ - if (*size >= limit) /* cannot grow even a little? */ - luaG_runerror(L, errormsg); - newsize = limit; /* still have at least one free place */ - } - else { - newsize = (*size)*2; - if (newsize < MINSIZEARRAY) - newsize = MINSIZEARRAY; /* minimum size */ - } - newblock = luaM_reallocv(L, block, *size, newsize, size_elems); - *size = newsize; /* update only when everything else is OK */ - return newblock; -} - - -void *luaM_toobig (lua_State *L) { - luaG_runerror(L, "memory allocation error: block too big"); - return NULL; /* to avoid warnings */ -} - - - -/* -** generic allocation routine. -*/ -void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { - global_State *g = G(L); - lua_assert((osize == 0) == (block == NULL)); - block = (*g->frealloc)(g->ud, block, osize, nsize); - if (block == NULL && nsize > 0) - luaD_throw(L, LUA_ERRMEM); - lua_assert((nsize == 0) == (block == NULL)); - g->totalbytes = (g->totalbytes - osize) + nsize; - return block; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.h deleted file mode 100644 index 7c2dcb3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lmem.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - -#ifndef lmem_h -#define lmem_h - - -#include - -#include "llimits.h" -#include "lua.h" - -#define MEMERRMSG "not enough memory" - - -#define luaM_reallocv(L,b,on,n,e) \ - ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ - luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ - luaM_toobig(L)) - -#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) -#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) -#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) - -#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) -#define luaM_newvector(L,n,t) \ - cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) - -#define luaM_growvector(L,v,nelems,size,t,limit,e) \ - if ((nelems)+1 > (size)) \ - ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) - -#define luaM_reallocvector(L, v,oldn,n,t) \ - ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) - - -LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, - size_t size); -LUAI_FUNC void *luaM_toobig (lua_State *L); -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, - size_t size_elem, int limit, - const char *errormsg); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/loadlib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/loadlib.c deleted file mode 100644 index 6158c53..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/loadlib.c +++ /dev/null @@ -1,666 +0,0 @@ -/* -** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $ -** Dynamic library loader for Lua -** See Copyright Notice in lua.h -** -** This module contains an implementation of loadlib for Unix systems -** that have dlfcn, an implementation for Darwin (Mac OS X), an -** implementation for Windows, and a stub for other systems. -*/ - - -#include -#include - - -#define loadlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* prefix for open functions in C libraries */ -#define LUA_POF "luaopen_" - -/* separator for open functions in C libraries */ -#define LUA_OFSEP "_" - - -#define LIBPREFIX "LOADLIB: " - -#define POF LUA_POF -#define LIB_FAIL "open" - - -/* error codes for ll_loadfunc */ -#define ERRLIB 1 -#define ERRFUNC 2 - -#define setprogdir(L) ((void)0) - - -static void ll_unloadlib (void *lib); -static void *ll_load (lua_State *L, const char *path); -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); - - - -#if defined(LUA_DL_DLOPEN) -/* -** {======================================================================== -** This is an implementation of loadlib based on the dlfcn interface. -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -** as an emulation layer on top of native functions. -** ========================================================================= -*/ - -#include - -static void ll_unloadlib (void *lib) { - dlclose(lib); -} - - -static void *ll_load (lua_State *L, const char *path) { - void *lib = dlopen(path, RTLD_NOW); - if (lib == NULL) lua_pushstring(L, dlerror()); - return lib; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); - if (f == NULL) lua_pushstring(L, dlerror()); - return f; -} - -/* }====================================================== */ - - - -#elif defined(LUA_DL_DLL) -/* -** {====================================================================== -** This is an implementation of loadlib for Windows using native functions. -** ======================================================================= -*/ - -#include - - -#undef setprogdir - -static void setprogdir (lua_State *L) { - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) - luaL_error(L, "unable to get ModuleFileName"); - else { - *lb = '\0'; - luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - - -static void pusherror (lua_State *L) { - int error = GetLastError(); - char buffer[128]; - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, buffer, sizeof(buffer), NULL)) - lua_pushstring(L, buffer); - else - lua_pushfstring(L, "system error %d\n", error); -} - -static void ll_unloadlib (void *lib) { - FreeLibrary((HINSTANCE)lib); -} - - -static void *ll_load (lua_State *L, const char *path) { - HINSTANCE lib = LoadLibraryA(path); - if (lib == NULL) pusherror(L); - return lib; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); - if (f == NULL) pusherror(L); - return f; -} - -/* }====================================================== */ - - - -#elif defined(LUA_DL_DYLD) -/* -** {====================================================================== -** Native Mac OS X / Darwin Implementation -** ======================================================================= -*/ - -#include - - -/* Mac appends a `_' before C function names */ -#undef POF -#define POF "_" LUA_POF - - -static void pusherror (lua_State *L) { - const char *err_str; - const char *err_file; - NSLinkEditErrors err; - int err_num; - NSLinkEditError(&err, &err_num, &err_file, &err_str); - lua_pushstring(L, err_str); -} - - -static const char *errorfromcode (NSObjectFileImageReturnCode ret) { - switch (ret) { - case NSObjectFileImageInappropriateFile: - return "file is not a bundle"; - case NSObjectFileImageArch: - return "library is for wrong CPU type"; - case NSObjectFileImageFormat: - return "bad format"; - case NSObjectFileImageAccess: - return "cannot access file"; - case NSObjectFileImageFailure: - default: - return "unable to load library"; - } -} - - -static void ll_unloadlib (void *lib) { - NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); -} - - -static void *ll_load (lua_State *L, const char *path) { - NSObjectFileImage img; - NSObjectFileImageReturnCode ret; - /* this would be a rare case, but prevents crashing if it happens */ - if(!_dyld_present()) { - lua_pushliteral(L, "dyld not present"); - return NULL; - } - ret = NSCreateObjectFileImageFromFile(path, &img); - if (ret == NSObjectFileImageSuccess) { - NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | - NSLINKMODULE_OPTION_RETURN_ON_ERROR); - NSDestroyObjectFileImage(img); - if (mod == NULL) pusherror(L); - return mod; - } - lua_pushstring(L, errorfromcode(ret)); - return NULL; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); - if (nss == NULL) { - lua_pushfstring(L, "symbol " LUA_QS " not found", sym); - return NULL; - } - return (lua_CFunction)NSAddressOfSymbol(nss); -} - -/* }====================================================== */ - - - -#else -/* -** {====================================================== -** Fallback for other systems -** ======================================================= -*/ - -#undef LIB_FAIL -#define LIB_FAIL "absent" - - -#define DLMSG "dynamic libraries not enabled; check your Lua installation" - - -static void ll_unloadlib (void *lib) { - (void)lib; /* to avoid warnings */ -} - - -static void *ll_load (lua_State *L, const char *path) { - (void)path; /* to avoid warnings */ - lua_pushliteral(L, DLMSG); - return NULL; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - (void)lib; (void)sym; /* to avoid warnings */ - lua_pushliteral(L, DLMSG); - return NULL; -} - -/* }====================================================== */ -#endif - - - -static void **ll_register (lua_State *L, const char *path) { - void **plib; - lua_pushfstring(L, "%s%s", LIBPREFIX, path); - lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ - if (!lua_isnil(L, -1)) /* is there an entry? */ - plib = (void **)lua_touserdata(L, -1); - else { /* no entry yet; create one */ - lua_pop(L, 1); - plib = (void **)lua_newuserdata(L, sizeof(const void *)); - *plib = NULL; - luaL_getmetatable(L, "_LOADLIB"); - lua_setmetatable(L, -2); - lua_pushfstring(L, "%s%s", LIBPREFIX, path); - lua_pushvalue(L, -2); - lua_settable(L, LUA_REGISTRYINDEX); - } - return plib; -} - - -/* -** __gc tag method: calls library's `ll_unloadlib' function with the lib -** handle -*/ -static int gctm (lua_State *L) { - void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); - if (*lib) ll_unloadlib(*lib); - *lib = NULL; /* mark library as closed */ - return 0; -} - - -static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { - void **reg = ll_register(L, path); - if (*reg == NULL) *reg = ll_load(L, path); - if (*reg == NULL) - return ERRLIB; /* unable to load library */ - else { - lua_CFunction f = ll_sym(L, *reg, sym); - if (f == NULL) - return ERRFUNC; /* unable to find function */ - lua_pushcfunction(L, f); - return 0; /* return function */ - } -} - - -static int ll_loadlib (lua_State *L) { - const char *path = luaL_checkstring(L, 1); - const char *init = luaL_checkstring(L, 2); - int stat = ll_loadfunc(L, path, init); - if (stat == 0) /* no errors? */ - return 1; /* return the loaded function */ - else { /* error; error message is on stack top */ - lua_pushnil(L); - lua_insert(L, -2); - lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ - } -} - - - -/* -** {====================================================== -** 'require' function -** ======================================================= -*/ - - -static int readable (const char *filename) { - FILE *f = fopen(filename, "r"); /* try to open file */ - if (f == NULL) return 0; /* open failed */ - fclose(f); - return 1; -} - - -static const char *pushnexttemplate (lua_State *L, const char *path) { - const char *l; - while (*path == *LUA_PATHSEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ - l = strchr(path, *LUA_PATHSEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, l - path); /* template */ - return l; -} - - -static const char *findfile (lua_State *L, const char *name, - const char *pname) { - const char *path; - name = luaL_gsub(L, name, ".", LUA_DIRSEP); - lua_getfield(L, LUA_ENVIRONINDEX, pname); - path = lua_tostring(L, -1); - if (path == NULL) - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); - lua_pushliteral(L, ""); /* error accumulator */ - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename; - filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ - if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); - lua_remove(L, -2); /* remove file name */ - lua_concat(L, 2); /* add entry to possible error message */ - } - return NULL; /* not found */ -} - - -static void loaderror (lua_State *L, const char *filename) { - luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", - lua_tostring(L, 1), filename, lua_tostring(L, -1)); -} - - -static int loader_Lua (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - filename = findfile(L, name, "path"); - if (filename == NULL) return 1; /* library not found in this path */ - if (luaL_loadfile(L, filename) != 0) - loaderror(L, filename); - return 1; /* library loaded successfully */ -} - - -static const char *mkfuncname (lua_State *L, const char *modname) { - const char *funcname; - const char *mark = strchr(modname, *LUA_IGMARK); - if (mark) modname = mark + 1; - funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); - funcname = lua_pushfstring(L, POF"%s", funcname); - lua_remove(L, -2); /* remove 'gsub' result */ - return funcname; -} - - -static int loader_C (lua_State *L) { - const char *funcname; - const char *name = luaL_checkstring(L, 1); - const char *filename = findfile(L, name, "cpath"); - if (filename == NULL) return 1; /* library not found in this path */ - funcname = mkfuncname(L, name); - if (ll_loadfunc(L, filename, funcname) != 0) - loaderror(L, filename); - return 1; /* library loaded successfully */ -} - - -static int loader_Croot (lua_State *L) { - const char *funcname; - const char *filename; - const char *name = luaL_checkstring(L, 1); - const char *p = strchr(name, '.'); - int stat; - if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, p - name); - filename = findfile(L, lua_tostring(L, -1), "cpath"); - if (filename == NULL) return 1; /* root not found */ - funcname = mkfuncname(L, name); - if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { - if (stat != ERRFUNC) loaderror(L, filename); /* real error */ - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, - name, filename); - return 1; /* function not found */ - } - return 1; -} - - -static int loader_preload (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_ENVIRONINDEX, "preload"); - if (!lua_istable(L, -1)) - luaL_error(L, LUA_QL("package.preload") " must be a table"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) /* not found? */ - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - return 1; -} - - -static const int sentinel_ = 0; -#define sentinel ((void *)&sentinel_) - - -static int ll_require (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - int i; - lua_settop(L, 1); /* _LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); - if (lua_toboolean(L, -1)) { /* is it there? */ - if (lua_touserdata(L, -1) == sentinel) /* check loops */ - luaL_error(L, "loop or previous error loading module " LUA_QS, name); - return 1; /* package is already loaded */ - } - /* else must load it; iterate over available loaders */ - lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); - if (!lua_istable(L, -1)) - luaL_error(L, LUA_QL("package.loaders") " must be a table"); - lua_pushliteral(L, ""); /* error message accumulator */ - for (i=1; ; i++) { - lua_rawgeti(L, -2, i); /* get a loader */ - if (lua_isnil(L, -1)) - luaL_error(L, "module " LUA_QS " not found:%s", - name, lua_tostring(L, -2)); - lua_pushstring(L, name); - lua_call(L, 1, 1); /* call it */ - if (lua_isfunction(L, -1)) /* did it find module? */ - break; /* module loaded successfully */ - else if (lua_isstring(L, -1)) /* loader returned error message? */ - lua_concat(L, 2); /* accumulate it */ - else - lua_pop(L, 1); - } - lua_pushlightuserdata(L, sentinel); - lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ - lua_pushstring(L, name); /* pass name as argument to module */ - lua_call(L, 1, 1); /* run loaded module */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ - lua_getfield(L, 2, name); - if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ - lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* _LOADED[name] = true */ - } - return 1; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** 'module' function -** ======================================================= -*/ - - -static void setfenv (lua_State *L) { - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - luaL_error(L, LUA_QL("module") " not called from a Lua function"); - lua_pushvalue(L, -2); - lua_setfenv(L, -2); - lua_pop(L, 1); -} - - -static void dooptions (lua_State *L, int n) { - int i; - for (i = 2; i <= n; i++) { - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } -} - - -static void modinit (lua_State *L, const char *modname) { - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; - else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, dot - modname); - lua_setfield(L, -2, "_PACKAGE"); -} - - -static int ll_module (lua_State *L) { - const char *modname = luaL_checkstring(L, 1); - int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ - if (!lua_istable(L, -1)) { /* not found? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) - return luaL_error(L, "name conflict for module " LUA_QS, modname); - lua_pushvalue(L, -1); - lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ - } - /* check whether table already has a _NAME field */ - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) /* is table an initialized module? */ - lua_pop(L, 1); - else { /* no; initialize it */ - lua_pop(L, 1); - modinit(L, modname); - } - lua_pushvalue(L, -1); - setfenv(L); - dooptions(L, loaded - 1); - return 0; -} - - -static int ll_seeall (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushvalue(L, LUA_GLOBALSINDEX); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - - -/* }====================================================== */ - - - -/* auxiliary mark (for internal use) */ -#define AUXMARK "\1" - -static void setpath (lua_State *L, const char *fieldname, const char *envname, - const char *def) { - const char *path = getenv(envname); - if (path == NULL) /* no environment variable? */ - lua_pushstring(L, def); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, - LUA_PATHSEP AUXMARK LUA_PATHSEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); - } - setprogdir(L); - lua_setfield(L, -2, fieldname); -} - - -static const luaL_Reg pk_funcs[] = { - {"loadlib", ll_loadlib}, - {"seeall", ll_seeall}, - {NULL, NULL} -}; - - -static const luaL_Reg ll_funcs[] = { - {"module", ll_module}, - {"require", ll_require}, - {NULL, NULL} -}; - - -static const lua_CFunction loaders[] = - {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; - - -LUALIB_API int luaopen_package (lua_State *L) { - int i; - /* create new type _LOADLIB */ - luaL_newmetatable(L, "_LOADLIB"); - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); - /* create `package' table */ - luaL_register(L, LUA_LOADLIBNAME, pk_funcs); -#if defined(LUA_COMPAT_LOADLIB) - lua_getfield(L, -1, "loadlib"); - lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); -#endif - lua_pushvalue(L, -1); - lua_replace(L, LUA_ENVIRONINDEX); - /* create `loaders' table */ - lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); - /* fill it with pre-defined loaders */ - for (i=0; loaders[i] != NULL; i++) { - lua_pushcfunction(L, loaders[i]); - lua_rawseti(L, -2, i+1); - } - lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ - setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ - setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ - /* store config information */ - lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" - LUA_EXECDIR "\n" LUA_IGMARK); - lua_setfield(L, -2, "config"); - /* set field `loaded' */ - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); - lua_setfield(L, -2, "loaded"); - /* set field `preload' */ - lua_newtable(L); - lua_setfield(L, -2, "preload"); - lua_pushvalue(L, LUA_GLOBALSINDEX); - luaL_register(L, NULL, ll_funcs); /* open lib into global table */ - lua_pop(L, 1); - return 1; /* return 'package' table */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.c deleted file mode 100644 index 4ff5073..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.c +++ /dev/null @@ -1,214 +0,0 @@ -/* -** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ -** Some generic functions over Lua objects -** See Copyright Notice in lua.h -*/ - -#include -#include -#include -#include -#include - -#define lobject_c -#define LUA_CORE - -#include "lua.h" - -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "lvm.h" - - - -const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; - - -/* -** converts an integer to a "floating point byte", represented as -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if -** eeeee != 0 and (xxx) otherwise. -*/ -int luaO_int2fb (unsigned int x) { - int e = 0; /* expoent */ - while (x >= 16) { - x = (x+1) >> 1; - e++; - } - if (x < 8) return x; - else return ((e+1) << 3) | (cast_int(x) - 8); -} - - -/* converts back */ -int luaO_fb2int (int x) { - int e = (x >> 3) & 31; - if (e == 0) return x; - else return ((x & 7)+8) << (e - 1); -} - - -int luaO_log2 (unsigned int x) { - static const lu_byte log_2[256] = { - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 - }; - int l = -1; - while (x >= 256) { l += 8; x >>= 8; } - return l + log_2[x]; - -} - - -int luaO_rawequalObj (const TValue *t1, const TValue *t2) { - if (ttype(t1) != ttype(t2)) return 0; - else switch (ttype(t1)) { - case LUA_TNIL: - return 1; - case LUA_TNUMBER: - return luai_numeq(nvalue(t1), nvalue(t2)); - case LUA_TBOOLEAN: - return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ - case LUA_TLIGHTUSERDATA: - return pvalue(t1) == pvalue(t2); - default: - lua_assert(iscollectable(t1)); - return gcvalue(t1) == gcvalue(t2); - } -} - - -int luaO_str2d (const char *s, lua_Number *result) { - char *endptr; - *result = lua_str2number(s, &endptr); - if (endptr == s) return 0; /* conversion failed */ - if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ - *result = cast_num(strtoul(s, &endptr, 16)); - if (*endptr == '\0') return 1; /* most common case */ - while (isspace(cast(unsigned char, *endptr))) endptr++; - if (*endptr != '\0') return 0; /* invalid trailing characters? */ - return 1; -} - - - -static void pushstr (lua_State *L, const char *str) { - setsvalue2s(L, L->top, luaS_new(L, str)); - incr_top(L); -} - - -/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ -const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - int n = 1; - pushstr(L, ""); - for (;;) { - const char *e = strchr(fmt, '%'); - if (e == NULL) break; - setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); - incr_top(L); - switch (*(e+1)) { - case 's': { - const char *s = va_arg(argp, char *); - if (s == NULL) s = "(null)"; - pushstr(L, s); - break; - } - case 'c': { - char buff[2]; - buff[0] = cast(char, va_arg(argp, int)); - buff[1] = '\0'; - pushstr(L, buff); - break; - } - case 'd': { - setnvalue(L->top, cast_num(va_arg(argp, int))); - incr_top(L); - break; - } - case 'f': { - setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); - incr_top(L); - break; - } - case 'p': { - char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ - sprintf(buff, "%p", va_arg(argp, void *)); - pushstr(L, buff); - break; - } - case '%': { - pushstr(L, "%"); - break; - } - default: { - char buff[3]; - buff[0] = '%'; - buff[1] = *(e+1); - buff[2] = '\0'; - pushstr(L, buff); - break; - } - } - n += 2; - fmt = e+2; - } - pushstr(L, fmt); - luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); - L->top -= n; - return svalue(L->top - 1); -} - - -const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - return msg; -} - - -void luaO_chunkid (char *out, const char *source, size_t bufflen) { - if (*source == '=') { - strncpy(out, source+1, bufflen); /* remove first char */ - out[bufflen-1] = '\0'; /* ensures null termination */ - } - else { /* out = "source", or "...source" */ - if (*source == '@') { - size_t l; - source++; /* skip the `@' */ - bufflen -= sizeof(" '...' "); - l = strlen(source); - strcpy(out, ""); - if (l > bufflen) { - source += (l-bufflen); /* get last part of file name */ - strcat(out, "..."); - } - strcat(out, source); - } - else { /* out = [string "string"] */ - size_t len = strcspn(source, "\n\r"); /* stop at first newline */ - bufflen -= sizeof(" [string \"...\"] "); - if (len > bufflen) len = bufflen; - strcpy(out, "[string \""); - if (source[len] != '\0') { /* must truncate? */ - strncat(out, source, len); - strcat(out, "..."); - } - else - strcat(out, source); - strcat(out, "\"]"); - } - } -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.h deleted file mode 100644 index f1e447e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lobject.h +++ /dev/null @@ -1,381 +0,0 @@ -/* -** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $ -** Type definitions for Lua objects -** See Copyright Notice in lua.h -*/ - - -#ifndef lobject_h -#define lobject_h - - -#include - - -#include "llimits.h" -#include "lua.h" - - -/* tags for values visible from Lua */ -#define LAST_TAG LUA_TTHREAD - -#define NUM_TAGS (LAST_TAG+1) - - -/* -** Extra tags for non-values -*/ -#define LUA_TPROTO (LAST_TAG+1) -#define LUA_TUPVAL (LAST_TAG+2) -#define LUA_TDEADKEY (LAST_TAG+3) - - -/* -** Union of all collectable objects -*/ -typedef union GCObject GCObject; - - -/* -** Common Header for all collectable objects (in macro form, to be -** included in other objects) -*/ -#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked - - -/* -** Common header in struct form -*/ -typedef struct GCheader { - CommonHeader; -} GCheader; - - - - -/* -** Union of all Lua values -*/ -typedef union { - GCObject *gc; - void *p; - lua_Number n; - int b; -} Value; - - -/* -** Tagged Values -*/ - -#define TValuefields Value value; int tt - -typedef struct lua_TValue { - TValuefields; -} TValue; - - -/* Macros to test type */ -#define ttisnil(o) (ttype(o) == LUA_TNIL) -#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) -#define ttisstring(o) (ttype(o) == LUA_TSTRING) -#define ttistable(o) (ttype(o) == LUA_TTABLE) -#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) -#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) -#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) -#define ttisthread(o) (ttype(o) == LUA_TTHREAD) -#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) - -/* Macros to access values */ -#define ttype(o) ((o)->tt) -#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) -#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) -#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) -#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) -#define tsvalue(o) (&rawtsvalue(o)->tsv) -#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) -#define uvalue(o) (&rawuvalue(o)->uv) -#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) -#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) -#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) -#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) - -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) - -/* -** for internal debug only -*/ -#define checkconsistency(obj) \ - lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) - -#define checkliveness(g,obj) \ - lua_assert(!iscollectable(obj) || \ - ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) - - -/* Macros to set values */ -#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) - -#define setnvalue(obj,x) \ - { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } - -#define setpvalue(obj,x) \ - { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } - -#define setbvalue(obj,x) \ - { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } - -#define setsvalue(L,obj,x) \ - { TValue *i_o=(obj); \ - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ - checkliveness(G(L),i_o); } - -#define setuvalue(L,obj,x) \ - { TValue *i_o=(obj); \ - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ - checkliveness(G(L),i_o); } - -#define setthvalue(L,obj,x) \ - { TValue *i_o=(obj); \ - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ - checkliveness(G(L),i_o); } - -#define setclvalue(L,obj,x) \ - { TValue *i_o=(obj); \ - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ - checkliveness(G(L),i_o); } - -#define sethvalue(L,obj,x) \ - { TValue *i_o=(obj); \ - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ - checkliveness(G(L),i_o); } - -#define setptvalue(L,obj,x) \ - { TValue *i_o=(obj); \ - i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ - checkliveness(G(L),i_o); } - - - - -#define setobj(L,obj1,obj2) \ - { const TValue *o2=(obj2); TValue *o1=(obj1); \ - o1->value = o2->value; o1->tt=o2->tt; \ - checkliveness(G(L),o1); } - - -/* -** different types of sets, according to destination -*/ - -/* from stack to (same) stack */ -#define setobjs2s setobj -/* to stack (not from same stack) */ -#define setobj2s setobj -#define setsvalue2s setsvalue -#define sethvalue2s sethvalue -#define setptvalue2s setptvalue -/* from table to same table */ -#define setobjt2t setobj -/* to table */ -#define setobj2t setobj -/* to new object */ -#define setobj2n setobj -#define setsvalue2n setsvalue - -#define setttype(obj, tt) (ttype(obj) = (tt)) - - -#define iscollectable(o) (ttype(o) >= LUA_TSTRING) - - - -typedef TValue *StkId; /* index to stack elements */ - - -/* -** String headers for string table -*/ -typedef union TString { - L_Umaxalign dummy; /* ensures maximum alignment for strings */ - struct { - CommonHeader; - lu_byte reserved; - unsigned int hash; - size_t len; - } tsv; -} TString; - - -#define getstr(ts) cast(const char *, (ts) + 1) -#define svalue(o) getstr(rawtsvalue(o)) - - - -typedef union Udata { - L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ - struct { - CommonHeader; - struct Table *metatable; - struct Table *env; - size_t len; - } uv; -} Udata; - - - - -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; - TValue *k; /* constants used by the function */ - Instruction *code; - struct Proto **p; /* functions defined inside the function */ - int *lineinfo; /* map from opcodes to source lines */ - struct LocVar *locvars; /* information about local variables */ - TString **upvalues; /* upvalue names */ - TString *source; - int sizeupvalues; - int sizek; /* size of `k' */ - int sizecode; - int sizelineinfo; - int sizep; /* size of `p' */ - int sizelocvars; - int linedefined; - int lastlinedefined; - GCObject *gclist; - lu_byte nups; /* number of upvalues */ - lu_byte numparams; - lu_byte is_vararg; - lu_byte maxstacksize; -} Proto; - - -/* masks for new-style vararg */ -#define VARARG_HASARG 1 -#define VARARG_ISVARARG 2 -#define VARARG_NEEDSARG 4 - - -typedef struct LocVar { - TString *varname; - int startpc; /* first point where variable is active */ - int endpc; /* first point where variable is dead */ -} LocVar; - - - -/* -** Upvalues -*/ - -typedef struct UpVal { - CommonHeader; - TValue *v; /* points to stack or to its own value */ - union { - TValue value; /* the value (when closed) */ - struct { /* double linked list (when open) */ - struct UpVal *prev; - struct UpVal *next; - } l; - } u; -} UpVal; - - -/* -** Closures -*/ - -#define ClosureHeader \ - CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ - struct Table *env - -typedef struct CClosure { - ClosureHeader; - lua_CFunction f; - TValue upvalue[1]; -} CClosure; - - -typedef struct LClosure { - ClosureHeader; - struct Proto *p; - UpVal *upvals[1]; -} LClosure; - - -typedef union Closure { - CClosure c; - LClosure l; -} Closure; - - -#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) -#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) - - -/* -** Tables -*/ - -typedef union TKey { - struct { - TValuefields; - struct Node *next; /* for chaining */ - } nk; - TValue tvk; -} TKey; - - -typedef struct Node { - TValue i_val; - TKey i_key; -} Node; - - -typedef struct Table { - CommonHeader; - lu_byte flags; /* 1<

lsizenode)) - - -#define luaO_nilobject (&luaO_nilobject_) - -LUAI_DATA const TValue luaO_nilobject_; - -#define ceillog2(x) (luaO_log2((x)-1) + 1) - -LUAI_FUNC int luaO_log2 (unsigned int x); -LUAI_FUNC int luaO_int2fb (unsigned int x); -LUAI_FUNC int luaO_fb2int (int x); -LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); -LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); -LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, - va_list argp); -LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.c deleted file mode 100644 index 4cc7452..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.c +++ /dev/null @@ -1,102 +0,0 @@ -/* -** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ -** See Copyright Notice in lua.h -*/ - - -#define lopcodes_c -#define LUA_CORE - - -#include "lopcodes.h" - - -/* ORDER OP */ - -const char *const luaP_opnames[NUM_OPCODES+1] = { - "MOVE", - "LOADK", - "LOADBOOL", - "LOADNIL", - "GETUPVAL", - "GETGLOBAL", - "GETTABLE", - "SETGLOBAL", - "SETUPVAL", - "SETTABLE", - "NEWTABLE", - "SELF", - "ADD", - "SUB", - "MUL", - "DIV", - "MOD", - "POW", - "UNM", - "NOT", - "LEN", - "CONCAT", - "JMP", - "EQ", - "LT", - "LE", - "TEST", - "TESTSET", - "CALL", - "TAILCALL", - "RETURN", - "FORLOOP", - "FORPREP", - "TFORLOOP", - "SETLIST", - "CLOSE", - "CLOSURE", - "VARARG", - NULL -}; - - -#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) - -const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* T A B C mode opcode */ - opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ - ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ - ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ - ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ - ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ - ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ -}; - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.h deleted file mode 100644 index 41224d6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lopcodes.h +++ /dev/null @@ -1,268 +0,0 @@ -/* -** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lopcodes_h -#define lopcodes_h - -#include "llimits.h" - - -/*=========================================================================== - We assume that instructions are unsigned numbers. - All instructions have an opcode in the first 6 bits. - Instructions can have the following fields: - `A' : 8 bits - `B' : 9 bits - `C' : 9 bits - `Bx' : 18 bits (`B' and `C' together) - `sBx' : signed Bx - - A signed argument is represented in excess K; that is, the number - value is the unsigned value minus K. K is exactly the maximum value - for that argument (so that -max is represented by 0, and +max is - represented by 2*max), which is half the maximum for the corresponding - unsigned argument. -===========================================================================*/ - - -enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ - - -/* -** size and position of opcode arguments. -*/ -#define SIZE_C 9 -#define SIZE_B 9 -#define SIZE_Bx (SIZE_C + SIZE_B) -#define SIZE_A 8 - -#define SIZE_OP 6 - -#define POS_OP 0 -#define POS_A (POS_OP + SIZE_OP) -#define POS_C (POS_A + SIZE_A) -#define POS_B (POS_C + SIZE_C) -#define POS_Bx POS_C - - -/* -** limits for opcode arguments. -** we use (signed) int to manipulate most arguments, -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) -*/ -#if SIZE_Bx < LUAI_BITSINT-1 -#define MAXARG_Bx ((1<>1) /* `sBx' is signed */ -#else -#define MAXARG_Bx MAX_INT -#define MAXARG_sBx MAX_INT -#endif - - -#define MAXARG_A ((1<>POS_OP) & MASK1(SIZE_OP,0))) -#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>POS_A) & MASK1(SIZE_A,0))) -#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ - ((cast(Instruction, u)<>POS_B) & MASK1(SIZE_B,0))) -#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ - ((cast(Instruction, b)<>POS_C) & MASK1(SIZE_C,0))) -#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ - ((cast(Instruction, b)<>POS_Bx) & MASK1(SIZE_Bx,0))) -#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ - ((cast(Instruction, b)< C) then pc++ */ -OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ - -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ - -OP_FORLOOP,/* A sBx R(A)+=R(A+2); - if R(A) =) R(A)*/ -OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ - -OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ -} OpCode; - - -#define NUM_OPCODES (cast(int, OP_VARARG) + 1) - - - -/*=========================================================================== - Notes: - (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, - and can be 0: OP_CALL then sets `top' to last_result+1, so - next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. - - (*) In OP_VARARG, if (B == 0) then use actual number of varargs and - set top (like in OP_CALL with C == 0). - - (*) In OP_RETURN, if (B == 0) then return up to `top' - - (*) In OP_SETLIST, if (B == 0) then B = `top'; - if (C == 0) then next `instruction' is real C - - (*) For comparisons, A specifies what condition the test should accept - (true or false). - - (*) All `skips' (pc++) assume that next instruction is a jump -===========================================================================*/ - - -/* -** masks for instruction properties. The format is: -** bits 0-1: op mode -** bits 2-3: C arg mode -** bits 4-5: B arg mode -** bit 6: instruction set register A -** bit 7: operator is a test -*/ - -enum OpArgMask { - OpArgN, /* argument is not used */ - OpArgU, /* argument is used */ - OpArgR, /* argument is a register or a jump offset */ - OpArgK /* argument is a constant or register/constant */ -}; - -LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; - -#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) -#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) -#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) -#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) -#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) - - -LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ - - -/* number of list items to accumulate before a SETLIST instruction */ -#define LFIELDS_PER_FLUSH 50 - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/loslib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/loslib.c deleted file mode 100644 index da06a57..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/loslib.c +++ /dev/null @@ -1,243 +0,0 @@ -/* -** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ -** Standard Operating System library -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include -#include - -#define loslib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static int os_pushresult (lua_State *L, int i, const char *filename) { - int en = errno; /* calls to Lua API may change this value */ - if (i) { - lua_pushboolean(L, 1); - return 1; - } - else { - lua_pushnil(L); - lua_pushfstring(L, "%s: %s", filename, strerror(en)); - lua_pushinteger(L, en); - return 3; - } -} - - -static int os_execute (lua_State *L) { - lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); - return 1; -} - - -static int os_remove (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - return os_pushresult(L, remove(filename) == 0, filename); -} - - -static int os_rename (lua_State *L) { - const char *fromname = luaL_checkstring(L, 1); - const char *toname = luaL_checkstring(L, 2); - return os_pushresult(L, rename(fromname, toname) == 0, fromname); -} - - -static int os_tmpname (lua_State *L) { - char buff[LUA_TMPNAMBUFSIZE]; - int err; - lua_tmpnam(buff, err); - if (err) - return luaL_error(L, "unable to generate a unique filename"); - lua_pushstring(L, buff); - return 1; -} - - -static int os_getenv (lua_State *L) { - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ - return 1; -} - - -static int os_clock (lua_State *L) { - lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); - return 1; -} - - -/* -** {====================================================== -** Time/Date operations -** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, -** wday=%w+1, yday=%j, isdst=? } -** ======================================================= -*/ - -static void setfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); - lua_setfield(L, -2, key); -} - -static void setboolfield (lua_State *L, const char *key, int value) { - if (value < 0) /* undefined? */ - return; /* does not set field */ - lua_pushboolean(L, value); - lua_setfield(L, -2, key); -} - -static int getboolfield (lua_State *L, const char *key) { - int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); - lua_pop(L, 1); - return res; -} - - -static int getfield (lua_State *L, const char *key, int d) { - int res; - lua_getfield(L, -1, key); - if (lua_isnumber(L, -1)) - res = (int)lua_tointeger(L, -1); - else { - if (d < 0) - return luaL_error(L, "field " LUA_QS " missing in date table", key); - res = d; - } - lua_pop(L, 1); - return res; -} - - -static int os_date (lua_State *L) { - const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); - struct tm *stm; - if (*s == '!') { /* UTC? */ - stm = gmtime(&t); - s++; /* skip `!' */ - } - else - stm = localtime(&t); - if (stm == NULL) /* invalid date? */ - lua_pushnil(L); - else if (strcmp(s, "*t") == 0) { - lua_createtable(L, 0, 9); /* 9 = number of fields */ - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon+1); - setfield(L, "year", stm->tm_year+1900); - setfield(L, "wday", stm->tm_wday+1); - setfield(L, "yday", stm->tm_yday+1); - setboolfield(L, "isdst", stm->tm_isdst); - } - else { - char cc[3]; - luaL_Buffer b; - cc[0] = '%'; cc[2] = '\0'; - luaL_buffinit(L, &b); - for (; *s; s++) { - if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ - luaL_addchar(&b, *s); - else { - size_t reslen; - char buff[200]; /* should be big enough for any conversion result */ - cc[1] = *(++s); - reslen = strftime(buff, sizeof(buff), cc, stm); - luaL_addlstring(&b, buff, reslen); - } - } - luaL_pushresult(&b); - } - return 1; -} - - -static int os_time (lua_State *L) { - time_t t; - if (lua_isnoneornil(L, 1)) /* called without args? */ - t = time(NULL); /* get current time */ - else { - struct tm ts; - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0); - ts.tm_min = getfield(L, "min", 0); - ts.tm_hour = getfield(L, "hour", 12); - ts.tm_mday = getfield(L, "day", -1); - ts.tm_mon = getfield(L, "month", -1) - 1; - ts.tm_year = getfield(L, "year", -1) - 1900; - ts.tm_isdst = getboolfield(L, "isdst"); - t = mktime(&ts); - } - if (t == (time_t)(-1)) - lua_pushnil(L); - else - lua_pushnumber(L, (lua_Number)t); - return 1; -} - - -static int os_difftime (lua_State *L) { - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), - (time_t)(luaL_optnumber(L, 2, 0)))); - return 1; -} - -/* }====================================================== */ - - -static int os_setlocale (lua_State *L) { - static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, - LC_NUMERIC, LC_TIME}; - static const char *const catnames[] = {"all", "collate", "ctype", "monetary", - "numeric", "time", NULL}; - const char *l = luaL_optstring(L, 1, NULL); - int op = luaL_checkoption(L, 2, "all", catnames); - lua_pushstring(L, setlocale(cat[op], l)); - return 1; -} - - -static int os_exit (lua_State *L) { - exit(luaL_optint(L, 1, EXIT_SUCCESS)); -} - -static const luaL_Reg syslib[] = { - {"clock", os_clock}, - {"date", os_date}, - {"difftime", os_difftime}, - {"execute", os_execute}, - {"exit", os_exit}, - {"getenv", os_getenv}, - {"remove", os_remove}, - {"rename", os_rename}, - {"setlocale", os_setlocale}, - {"time", os_time}, - {"tmpname", os_tmpname}, - {NULL, NULL} -}; - -/* }====================================================== */ - - - -LUALIB_API int luaopen_os (lua_State *L) { - luaL_register(L, LUA_OSLIBNAME, syslib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.c deleted file mode 100644 index dda7488..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.c +++ /dev/null @@ -1,1339 +0,0 @@ -/* -** $Id: lparser.c,v 2.42.1.4 2011/10/21 19:31:42 roberto Exp $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - - -#include - -#define lparser_c -#define LUA_CORE - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" - - - -#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) - -#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) - -#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) - - -/* -** nodes for block list (list of active blocks) -*/ -typedef struct BlockCnt { - struct BlockCnt *previous; /* chain */ - int breaklist; /* list of jumps out of this loop */ - lu_byte nactvar; /* # active locals outside the breakable structure */ - lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isbreakable; /* true if `block' is a loop */ -} BlockCnt; - - - -/* -** prototypes for recursive non-terminal functions -*/ -static void chunk (LexState *ls); -static void expr (LexState *ls, expdesc *v); - - -static void anchor_token (LexState *ls) { - if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { - TString *ts = ls->t.seminfo.ts; - luaX_newstring(ls, getstr(ts), ts->tsv.len); - } -} - - -static void error_expected (LexState *ls, int token) { - luaX_syntaxerror(ls, - luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); -} - - -static void errorlimit (FuncState *fs, int limit, const char *what) { - const char *msg = (fs->f->linedefined == 0) ? - luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : - luaO_pushfstring(fs->L, "function at line %d has more than %d %s", - fs->f->linedefined, limit, what); - luaX_lexerror(fs->ls, msg, 0); -} - - -static int testnext (LexState *ls, int c) { - if (ls->t.token == c) { - luaX_next(ls); - return 1; - } - else return 0; -} - - -static void check (LexState *ls, int c) { - if (ls->t.token != c) - error_expected(ls, c); -} - -static void checknext (LexState *ls, int c) { - check(ls, c); - luaX_next(ls); -} - - -#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } - - - -static void check_match (LexState *ls, int what, int who, int where) { - if (!testnext(ls, what)) { - if (where == ls->linenumber) - error_expected(ls, what); - else { - luaX_syntaxerror(ls, luaO_pushfstring(ls->L, - LUA_QS " expected (to close " LUA_QS " at line %d)", - luaX_token2str(ls, what), luaX_token2str(ls, who), where)); - } - } -} - - -static TString *str_checkname (LexState *ls) { - TString *ts; - check(ls, TK_NAME); - ts = ls->t.seminfo.ts; - luaX_next(ls); - return ts; -} - - -static void init_exp (expdesc *e, expkind k, int i) { - e->f = e->t = NO_JUMP; - e->k = k; - e->u.s.info = i; -} - - -static void codestring (LexState *ls, expdesc *e, TString *s) { - init_exp(e, VK, luaK_stringK(ls->fs, s)); -} - - -static void checkname(LexState *ls, expdesc *e) { - codestring(ls, e, str_checkname(ls)); -} - - -static int registerlocalvar (LexState *ls, TString *varname) { - FuncState *fs = ls->fs; - Proto *f = fs->f; - int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, - LocVar, SHRT_MAX, "too many local variables"); - while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; - f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, f, varname); - return fs->nlocvars++; -} - - -#define new_localvarliteral(ls,v,n) \ - new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) - - -static void new_localvar (LexState *ls, TString *name, int n) { - FuncState *fs = ls->fs; - luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); - fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); -} - - -static void adjustlocalvars (LexState *ls, int nvars) { - FuncState *fs = ls->fs; - fs->nactvar = cast_byte(fs->nactvar + nvars); - for (; nvars; nvars--) { - getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; - } -} - - -static void removevars (LexState *ls, int tolevel) { - FuncState *fs = ls->fs; - while (fs->nactvar > tolevel) - getlocvar(fs, --fs->nactvar).endpc = fs->pc; -} - - -static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { - int i; - Proto *f = fs->f; - int oldsize = f->sizeupvalues; - for (i=0; inups; i++) { - if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { - lua_assert(f->upvalues[i] == name); - return i; - } - } - /* new one */ - luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); - luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, - TString *, MAX_INT, ""); - while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; - f->upvalues[f->nups] = name; - luaC_objbarrier(fs->L, f, name); - lua_assert(v->k == VLOCAL || v->k == VUPVAL); - fs->upvalues[f->nups].k = cast_byte(v->k); - fs->upvalues[f->nups].info = cast_byte(v->u.s.info); - return f->nups++; -} - - -static int searchvar (FuncState *fs, TString *n) { - int i; - for (i=fs->nactvar-1; i >= 0; i--) { - if (n == getlocvar(fs, i).varname) - return i; - } - return -1; /* not found */ -} - - -static void markupval (FuncState *fs, int level) { - BlockCnt *bl = fs->bl; - while (bl && bl->nactvar > level) bl = bl->previous; - if (bl) bl->upval = 1; -} - - -static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - if (fs == NULL) { /* no more levels? */ - init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ - return VGLOBAL; - } - else { - int v = searchvar(fs, n); /* look up at current level */ - if (v >= 0) { - init_exp(var, VLOCAL, v); - if (!base) - markupval(fs, v); /* local will be used as an upval */ - return VLOCAL; - } - else { /* not found at current level; try upper one */ - if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) - return VGLOBAL; - var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ - var->k = VUPVAL; /* upvalue in this level */ - return VUPVAL; - } - } -} - - -static void singlevar (LexState *ls, expdesc *var) { - TString *varname = str_checkname(ls); - FuncState *fs = ls->fs; - if (singlevaraux(fs, varname, var, 1) == VGLOBAL) - var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ -} - - -static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { - FuncState *fs = ls->fs; - int extra = nvars - nexps; - if (hasmultret(e->k)) { - extra++; /* includes call itself */ - if (extra < 0) extra = 0; - luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ - if (extra > 1) luaK_reserveregs(fs, extra-1); - } - else { - if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ - if (extra > 0) { - int reg = fs->freereg; - luaK_reserveregs(fs, extra); - luaK_nil(fs, reg, extra); - } - } -} - - -static void enterlevel (LexState *ls) { - if (++ls->L->nCcalls > LUAI_MAXCCALLS) - luaX_lexerror(ls, "chunk has too many syntax levels", 0); -} - - -#define leavelevel(ls) ((ls)->L->nCcalls--) - - -static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { - bl->breaklist = NO_JUMP; - bl->isbreakable = isbreakable; - bl->nactvar = fs->nactvar; - bl->upval = 0; - bl->previous = fs->bl; - fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); -} - - -static void leaveblock (FuncState *fs) { - BlockCnt *bl = fs->bl; - fs->bl = bl->previous; - removevars(fs->ls, bl->nactvar); - if (bl->upval) - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); - /* a block either controls scope or breaks (never both) */ - lua_assert(!bl->isbreakable || !bl->upval); - lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = fs->nactvar; /* free registers */ - luaK_patchtohere(fs, bl->breaklist); -} - - -static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { - FuncState *fs = ls->fs; - Proto *f = fs->f; - int oldsize = f->sizep; - int i; - luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, - MAXARG_Bx, "constant table overflow"); - while (oldsize < f->sizep) f->p[oldsize++] = NULL; - f->p[fs->np++] = func->f; - luaC_objbarrier(ls->L, f, func->f); - init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); - for (i=0; if->nups; i++) { - OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; - luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); - } -} - - -static void open_func (LexState *ls, FuncState *fs) { - lua_State *L = ls->L; - Proto *f = luaF_newproto(L); - fs->f = f; - fs->prev = ls->fs; /* linked list of funcstates */ - fs->ls = ls; - fs->L = L; - ls->fs = fs; - fs->pc = 0; - fs->lasttarget = -1; - fs->jpc = NO_JUMP; - fs->freereg = 0; - fs->nk = 0; - fs->np = 0; - fs->nlocvars = 0; - fs->nactvar = 0; - fs->bl = NULL; - f->source = ls->source; - f->maxstacksize = 2; /* registers 0/1 are always valid */ - fs->h = luaH_new(L, 0, 0); - /* anchor table of constants and prototype (to avoid being collected) */ - sethvalue2s(L, L->top, fs->h); - incr_top(L); - setptvalue2s(L, L->top, f); - incr_top(L); -} - - -static void close_func (LexState *ls) { - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; - removevars(ls, 0); - luaK_ret(fs, 0, 0); /* final return */ - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); - f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); - f->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); - f->sizek = fs->nk; - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); - f->sizep = fs->np; - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); - f->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); - f->sizeupvalues = f->nups; - lua_assert(luaG_checkcode(f)); - lua_assert(fs->bl == NULL); - ls->fs = fs->prev; - /* last token read was anchored in defunct function; must reanchor it */ - if (fs) anchor_token(ls); - L->top -= 2; /* remove table and prototype from the stack */ -} - - -Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { - struct LexState lexstate; - struct FuncState funcstate; - lexstate.buff = buff; - luaX_setinput(L, &lexstate, z, luaS_new(L, name)); - open_func(&lexstate, &funcstate); - funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ - luaX_next(&lexstate); /* read first token */ - chunk(&lexstate); - check(&lexstate, TK_EOS); - close_func(&lexstate); - lua_assert(funcstate.prev == NULL); - lua_assert(funcstate.f->nups == 0); - lua_assert(lexstate.fs == NULL); - return funcstate.f; -} - - - -/*============================================================*/ -/* GRAMMAR RULES */ -/*============================================================*/ - - -static void field (LexState *ls, expdesc *v) { - /* field -> ['.' | ':'] NAME */ - FuncState *fs = ls->fs; - expdesc key; - luaK_exp2anyreg(fs, v); - luaX_next(ls); /* skip the dot or colon */ - checkname(ls, &key); - luaK_indexed(fs, v, &key); -} - - -static void yindex (LexState *ls, expdesc *v) { - /* index -> '[' expr ']' */ - luaX_next(ls); /* skip the '[' */ - expr(ls, v); - luaK_exp2val(ls->fs, v); - checknext(ls, ']'); -} - - -/* -** {====================================================================== -** Rules for Constructors -** ======================================================================= -*/ - - -struct ConsControl { - expdesc v; /* last list item read */ - expdesc *t; /* table descriptor */ - int nh; /* total number of `record' elements */ - int na; /* total number of array elements */ - int tostore; /* number of array elements pending to be stored */ -}; - - -static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | `['exp1`]') = exp1 */ - FuncState *fs = ls->fs; - int reg = ls->fs->freereg; - expdesc key, val; - int rkkey; - if (ls->t.token == TK_NAME) { - luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); - checkname(ls, &key); - } - else /* ls->t.token == '[' */ - yindex(ls, &key); - cc->nh++; - checknext(ls, '='); - rkkey = luaK_exp2RK(fs, &key); - expr(ls, &val); - luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); - fs->freereg = reg; /* free registers */ -} - - -static void closelistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->v.k == VVOID) return; /* there is no list item */ - luaK_exp2nextreg(fs, &cc->v); - cc->v.k = VVOID; - if (cc->tostore == LFIELDS_PER_FLUSH) { - luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ - cc->tostore = 0; /* no more items pending */ - } -} - - -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->tostore == 0) return; - if (hasmultret(cc->v.k)) { - luaK_setmultret(fs, &cc->v); - luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); - cc->na--; /* do not count last expression (unknown number of elements) */ - } - else { - if (cc->v.k != VVOID) - luaK_exp2nextreg(fs, &cc->v); - luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); - } -} - - -static void listfield (LexState *ls, struct ConsControl *cc) { - expr(ls, &cc->v); - luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; - cc->tostore++; -} - - -static void constructor (LexState *ls, expdesc *t) { - /* constructor -> ?? */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - struct ConsControl cc; - cc.na = cc.nh = cc.tostore = 0; - cc.t = t; - init_exp(t, VRELOCABLE, pc); - init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ - checknext(ls, '{'); - do { - lua_assert(cc.v.k == VVOID || cc.tostore > 0); - if (ls->t.token == '}') break; - closelistfield(fs, &cc); - switch(ls->t.token) { - case TK_NAME: { /* may be listfields or recfields */ - luaX_lookahead(ls); - if (ls->lookahead.token != '=') /* expression? */ - listfield(ls, &cc); - else - recfield(ls, &cc); - break; - } - case '[': { /* constructor_item -> recfield */ - recfield(ls, &cc); - break; - } - default: { /* constructor_part -> listfield */ - listfield(ls, &cc); - break; - } - } - } while (testnext(ls, ',') || testnext(ls, ';')); - check_match(ls, '}', '{', line); - lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ -} - -/* }====================================================================== */ - - - -static void parlist (LexState *ls) { - /* parlist -> [ param { `,' param } ] */ - FuncState *fs = ls->fs; - Proto *f = fs->f; - int nparams = 0; - f->is_vararg = 0; - if (ls->t.token != ')') { /* is `parlist' not empty? */ - do { - switch (ls->t.token) { - case TK_NAME: { /* param -> NAME */ - new_localvar(ls, str_checkname(ls), nparams++); - break; - } - case TK_DOTS: { /* param -> `...' */ - luaX_next(ls); -#if defined(LUA_COMPAT_VARARG) - /* use `arg' as default name */ - new_localvarliteral(ls, "arg", nparams++); - f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; -#endif - f->is_vararg |= VARARG_ISVARARG; - break; - } - default: luaX_syntaxerror(ls, " or " LUA_QL("...") " expected"); - } - } while (!f->is_vararg && testnext(ls, ',')); - } - adjustlocalvars(ls, nparams); - f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); - luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ -} - - -static void body (LexState *ls, expdesc *e, int needself, int line) { - /* body -> `(' parlist `)' chunk END */ - FuncState new_fs; - open_func(ls, &new_fs); - new_fs.f->linedefined = line; - checknext(ls, '('); - if (needself) { - new_localvarliteral(ls, "self", 0); - adjustlocalvars(ls, 1); - } - parlist(ls); - checknext(ls, ')'); - chunk(ls); - new_fs.f->lastlinedefined = ls->linenumber; - check_match(ls, TK_END, TK_FUNCTION, line); - close_func(ls); - pushclosure(ls, &new_fs, e); -} - - -static int explist1 (LexState *ls, expdesc *v) { - /* explist1 -> expr { `,' expr } */ - int n = 1; /* at least one expression */ - expr(ls, v); - while (testnext(ls, ',')) { - luaK_exp2nextreg(ls->fs, v); - expr(ls, v); - n++; - } - return n; -} - - -static void funcargs (LexState *ls, expdesc *f) { - FuncState *fs = ls->fs; - expdesc args; - int base, nparams; - int line = ls->linenumber; - switch (ls->t.token) { - case '(': { /* funcargs -> `(' [ explist1 ] `)' */ - if (line != ls->lastline) - luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); - luaX_next(ls); - if (ls->t.token == ')') /* arg list is empty? */ - args.k = VVOID; - else { - explist1(ls, &args); - luaK_setmultret(fs, &args); - } - check_match(ls, ')', '(', line); - break; - } - case '{': { /* funcargs -> constructor */ - constructor(ls, &args); - break; - } - case TK_STRING: { /* funcargs -> STRING */ - codestring(ls, &args, ls->t.seminfo.ts); - luaX_next(ls); /* must use `seminfo' before `next' */ - break; - } - default: { - luaX_syntaxerror(ls, "function arguments expected"); - return; - } - } - lua_assert(f->k == VNONRELOC); - base = f->u.s.info; /* base register for call */ - if (hasmultret(args.k)) - nparams = LUA_MULTRET; /* open call */ - else { - if (args.k != VVOID) - luaK_exp2nextreg(fs, &args); /* close last argument */ - nparams = fs->freereg - (base+1); - } - init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); - luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ -} - - - - -/* -** {====================================================================== -** Expression parsing -** ======================================================================= -*/ - - -static void prefixexp (LexState *ls, expdesc *v) { - /* prefixexp -> NAME | '(' expr ')' */ - switch (ls->t.token) { - case '(': { - int line = ls->linenumber; - luaX_next(ls); - expr(ls, v); - check_match(ls, ')', '(', line); - luaK_dischargevars(ls->fs, v); - return; - } - case TK_NAME: { - singlevar(ls, v); - return; - } - default: { - luaX_syntaxerror(ls, "unexpected symbol"); - return; - } - } -} - - -static void primaryexp (LexState *ls, expdesc *v) { - /* primaryexp -> - prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ - FuncState *fs = ls->fs; - prefixexp(ls, v); - for (;;) { - switch (ls->t.token) { - case '.': { /* field */ - field(ls, v); - break; - } - case '[': { /* `[' exp1 `]' */ - expdesc key; - luaK_exp2anyreg(fs, v); - yindex(ls, &key); - luaK_indexed(fs, v, &key); - break; - } - case ':': { /* `:' NAME funcargs */ - expdesc key; - luaX_next(ls); - checkname(ls, &key); - luaK_self(fs, v, &key); - funcargs(ls, v); - break; - } - case '(': case TK_STRING: case '{': { /* funcargs */ - luaK_exp2nextreg(fs, v); - funcargs(ls, v); - break; - } - default: return; - } - } -} - - -static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | - constructor | FUNCTION body | primaryexp */ - switch (ls->t.token) { - case TK_NUMBER: { - init_exp(v, VKNUM, 0); - v->u.nval = ls->t.seminfo.r; - break; - } - case TK_STRING: { - codestring(ls, v, ls->t.seminfo.ts); - break; - } - case TK_NIL: { - init_exp(v, VNIL, 0); - break; - } - case TK_TRUE: { - init_exp(v, VTRUE, 0); - break; - } - case TK_FALSE: { - init_exp(v, VFALSE, 0); - break; - } - case TK_DOTS: { /* vararg */ - FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, - "cannot use " LUA_QL("...") " outside a vararg function"); - fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); - break; - } - case '{': { /* constructor */ - constructor(ls, v); - return; - } - case TK_FUNCTION: { - luaX_next(ls); - body(ls, v, 0, ls->linenumber); - return; - } - default: { - primaryexp(ls, v); - return; - } - } - luaX_next(ls); -} - - -static UnOpr getunopr (int op) { - switch (op) { - case TK_NOT: return OPR_NOT; - case '-': return OPR_MINUS; - case '#': return OPR_LEN; - default: return OPR_NOUNOPR; - } -} - - -static BinOpr getbinopr (int op) { - switch (op) { - case '+': return OPR_ADD; - case '-': return OPR_SUB; - case '*': return OPR_MUL; - case '/': return OPR_DIV; - case '%': return OPR_MOD; - case '^': return OPR_POW; - case TK_CONCAT: return OPR_CONCAT; - case TK_NE: return OPR_NE; - case TK_EQ: return OPR_EQ; - case '<': return OPR_LT; - case TK_LE: return OPR_LE; - case '>': return OPR_GT; - case TK_GE: return OPR_GE; - case TK_AND: return OPR_AND; - case TK_OR: return OPR_OR; - default: return OPR_NOBINOPR; - } -} - - -static const struct { - lu_byte left; /* left priority for each binary operator */ - lu_byte right; /* right priority */ -} priority[] = { /* ORDER OPR */ - {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ - {10, 9}, {5, 4}, /* power and concat (right associative) */ - {3, 3}, {3, 3}, /* equality and inequality */ - {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ - {2, 2}, {1, 1} /* logical (and/or) */ -}; - -#define UNARY_PRIORITY 8 /* priority for unary operators */ - - -/* -** subexpr -> (simpleexp | unop subexpr) { binop subexpr } -** where `binop' is any binary operator with a priority higher than `limit' -*/ -static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { - BinOpr op; - UnOpr uop; - enterlevel(ls); - uop = getunopr(ls->t.token); - if (uop != OPR_NOUNOPR) { - luaX_next(ls); - subexpr(ls, v, UNARY_PRIORITY); - luaK_prefix(ls->fs, uop, v); - } - else simpleexp(ls, v); - /* expand while operators have priorities higher than `limit' */ - op = getbinopr(ls->t.token); - while (op != OPR_NOBINOPR && priority[op].left > limit) { - expdesc v2; - BinOpr nextop; - luaX_next(ls); - luaK_infix(ls->fs, op, v); - /* read sub-expression with higher priority */ - nextop = subexpr(ls, &v2, priority[op].right); - luaK_posfix(ls->fs, op, v, &v2); - op = nextop; - } - leavelevel(ls); - return op; /* return first untreated operator */ -} - - -static void expr (LexState *ls, expdesc *v) { - subexpr(ls, v, 0); -} - -/* }==================================================================== */ - - - -/* -** {====================================================================== -** Rules for Statements -** ======================================================================= -*/ - - -static int block_follow (int token) { - switch (token) { - case TK_ELSE: case TK_ELSEIF: case TK_END: - case TK_UNTIL: case TK_EOS: - return 1; - default: return 0; - } -} - - -static void block (LexState *ls) { - /* block -> chunk */ - FuncState *fs = ls->fs; - BlockCnt bl; - enterblock(fs, &bl, 0); - chunk(ls); - lua_assert(bl.breaklist == NO_JUMP); - leaveblock(fs); -} - - -/* -** structure to chain all variables in the left-hand side of an -** assignment -*/ -struct LHS_assign { - struct LHS_assign *prev; - expdesc v; /* variable (global, local, upvalue, or indexed) */ -}; - - -/* -** check whether, in an assignment to a local variable, the local variable -** is needed in a previous assignment (to a table). If so, save original -** local value in a safe place and use this safe copy in the previous -** assignment. -*/ -static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { - FuncState *fs = ls->fs; - int extra = fs->freereg; /* eventual position to save local variable */ - int conflict = 0; - for (; lh; lh = lh->prev) { - if (lh->v.k == VINDEXED) { - if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ - conflict = 1; - lh->v.u.s.info = extra; /* previous assignment will use safe copy */ - } - if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ - conflict = 1; - lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ - } - } - } - if (conflict) { - luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ - luaK_reserveregs(fs, 1); - } -} - - -static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { - expdesc e; - check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, - "syntax error"); - if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ - struct LHS_assign nv; - nv.prev = lh; - primaryexp(ls, &nv.v); - if (nv.v.k == VLOCAL) - check_conflict(ls, lh, &nv.v); - luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, - "variables in assignment"); - assignment(ls, &nv, nvars+1); - } - else { /* assignment -> `=' explist1 */ - int nexps; - checknext(ls, '='); - nexps = explist1(ls, &e); - if (nexps != nvars) { - adjust_assign(ls, nvars, nexps, &e); - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ - } - else { - luaK_setoneret(ls->fs, &e); /* close last expression */ - luaK_storevar(ls->fs, &lh->v, &e); - return; /* avoid default */ - } - } - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ - luaK_storevar(ls->fs, &lh->v, &e); -} - - -static int cond (LexState *ls) { - /* cond -> exp */ - expdesc v; - expr(ls, &v); /* read condition */ - if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ - luaK_goiftrue(ls->fs, &v); - return v.f; -} - - -static void breakstat (LexState *ls) { - FuncState *fs = ls->fs; - BlockCnt *bl = fs->bl; - int upval = 0; - while (bl && !bl->isbreakable) { - upval |= bl->upval; - bl = bl->previous; - } - if (!bl) - luaX_syntaxerror(ls, "no loop to break"); - if (upval) - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); - luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); -} - - -static void whilestat (LexState *ls, int line) { - /* whilestat -> WHILE cond DO block END */ - FuncState *fs = ls->fs; - int whileinit; - int condexit; - BlockCnt bl; - luaX_next(ls); /* skip WHILE */ - whileinit = luaK_getlabel(fs); - condexit = cond(ls); - enterblock(fs, &bl, 1); - checknext(ls, TK_DO); - block(ls); - luaK_patchlist(fs, luaK_jump(fs), whileinit); - check_match(ls, TK_END, TK_WHILE, line); - leaveblock(fs); - luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ -} - - -static void repeatstat (LexState *ls, int line) { - /* repeatstat -> REPEAT block UNTIL cond */ - int condexit; - FuncState *fs = ls->fs; - int repeat_init = luaK_getlabel(fs); - BlockCnt bl1, bl2; - enterblock(fs, &bl1, 1); /* loop block */ - enterblock(fs, &bl2, 0); /* scope block */ - luaX_next(ls); /* skip REPEAT */ - chunk(ls); - check_match(ls, TK_UNTIL, TK_REPEAT, line); - condexit = cond(ls); /* read condition (inside scope block) */ - if (!bl2.upval) { /* no upvalues? */ - leaveblock(fs); /* finish scope */ - luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ - } - else { /* complete semantics when there are upvalues */ - breakstat(ls); /* if condition then break */ - luaK_patchtohere(ls->fs, condexit); /* else... */ - leaveblock(fs); /* finish scope... */ - luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ - } - leaveblock(fs); /* finish loop */ -} - - -static int exp1 (LexState *ls) { - expdesc e; - int k; - expr(ls, &e); - k = e.k; - luaK_exp2nextreg(ls->fs, &e); - return k; -} - - -static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { - /* forbody -> DO block */ - BlockCnt bl; - FuncState *fs = ls->fs; - int prep, endfor; - adjustlocalvars(ls, 3); /* control variables */ - checknext(ls, TK_DO); - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); - enterblock(fs, &bl, 0); /* scope for declared variables */ - adjustlocalvars(ls, nvars); - luaK_reserveregs(fs, nvars); - block(ls); - leaveblock(fs); /* end of scope for declared variables */ - luaK_patchtohere(fs, prep); - endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : - luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); - luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ - luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); -} - - -static void fornum (LexState *ls, TString *varname, int line) { - /* fornum -> NAME = exp1,exp1[,exp1] forbody */ - FuncState *fs = ls->fs; - int base = fs->freereg; - new_localvarliteral(ls, "(for index)", 0); - new_localvarliteral(ls, "(for limit)", 1); - new_localvarliteral(ls, "(for step)", 2); - new_localvar(ls, varname, 3); - checknext(ls, '='); - exp1(ls); /* initial value */ - checknext(ls, ','); - exp1(ls); /* limit */ - if (testnext(ls, ',')) - exp1(ls); /* optional step */ - else { /* default step = 1 */ - luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); - luaK_reserveregs(fs, 1); - } - forbody(ls, base, line, 1, 1); -} - - -static void forlist (LexState *ls, TString *indexname) { - /* forlist -> NAME {,NAME} IN explist1 forbody */ - FuncState *fs = ls->fs; - expdesc e; - int nvars = 0; - int line; - int base = fs->freereg; - /* create control variables */ - new_localvarliteral(ls, "(for generator)", nvars++); - new_localvarliteral(ls, "(for state)", nvars++); - new_localvarliteral(ls, "(for control)", nvars++); - /* create declared variables */ - new_localvar(ls, indexname, nvars++); - while (testnext(ls, ',')) - new_localvar(ls, str_checkname(ls), nvars++); - checknext(ls, TK_IN); - line = ls->linenumber; - adjust_assign(ls, 3, explist1(ls, &e), &e); - luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 3, 0); -} - - -static void forstat (LexState *ls, int line) { - /* forstat -> FOR (fornum | forlist) END */ - FuncState *fs = ls->fs; - TString *varname; - BlockCnt bl; - enterblock(fs, &bl, 1); /* scope for loop and control variables */ - luaX_next(ls); /* skip `for' */ - varname = str_checkname(ls); /* first variable name */ - switch (ls->t.token) { - case '=': fornum(ls, varname, line); break; - case ',': case TK_IN: forlist(ls, varname); break; - default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); - } - check_match(ls, TK_END, TK_FOR, line); - leaveblock(fs); /* loop scope (`break' jumps to this point) */ -} - - -static int test_then_block (LexState *ls) { - /* test_then_block -> [IF | ELSEIF] cond THEN block */ - int condexit; - luaX_next(ls); /* skip IF or ELSEIF */ - condexit = cond(ls); - checknext(ls, TK_THEN); - block(ls); /* `then' part */ - return condexit; -} - - -static void ifstat (LexState *ls, int line) { - /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ - FuncState *fs = ls->fs; - int flist; - int escapelist = NO_JUMP; - flist = test_then_block(ls); /* IF cond THEN block */ - while (ls->t.token == TK_ELSEIF) { - luaK_concat(fs, &escapelist, luaK_jump(fs)); - luaK_patchtohere(fs, flist); - flist = test_then_block(ls); /* ELSEIF cond THEN block */ - } - if (ls->t.token == TK_ELSE) { - luaK_concat(fs, &escapelist, luaK_jump(fs)); - luaK_patchtohere(fs, flist); - luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ - block(ls); /* `else' part */ - } - else - luaK_concat(fs, &escapelist, flist); - luaK_patchtohere(fs, escapelist); - check_match(ls, TK_END, TK_IF, line); -} - - -static void localfunc (LexState *ls) { - expdesc v, b; - FuncState *fs = ls->fs; - new_localvar(ls, str_checkname(ls), 0); - init_exp(&v, VLOCAL, fs->freereg); - luaK_reserveregs(fs, 1); - adjustlocalvars(ls, 1); - body(ls, &b, 0, ls->linenumber); - luaK_storevar(fs, &v, &b); - /* debug information will only see the variable after this point! */ - getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; -} - - -static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ - int nvars = 0; - int nexps; - expdesc e; - do { - new_localvar(ls, str_checkname(ls), nvars++); - } while (testnext(ls, ',')); - if (testnext(ls, '=')) - nexps = explist1(ls, &e); - else { - e.k = VVOID; - nexps = 0; - } - adjust_assign(ls, nvars, nexps, &e); - adjustlocalvars(ls, nvars); -} - - -static int funcname (LexState *ls, expdesc *v) { - /* funcname -> NAME {field} [`:' NAME] */ - int needself = 0; - singlevar(ls, v); - while (ls->t.token == '.') - field(ls, v); - if (ls->t.token == ':') { - needself = 1; - field(ls, v); - } - return needself; -} - - -static void funcstat (LexState *ls, int line) { - /* funcstat -> FUNCTION funcname body */ - int needself; - expdesc v, b; - luaX_next(ls); /* skip FUNCTION */ - needself = funcname(ls, &v); - body(ls, &b, needself, line); - luaK_storevar(ls->fs, &v, &b); - luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ -} - - -static void exprstat (LexState *ls) { - /* stat -> func | assignment */ - FuncState *fs = ls->fs; - struct LHS_assign v; - primaryexp(ls, &v.v); - if (v.v.k == VCALL) /* stat -> func */ - SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ - else { /* stat -> assignment */ - v.prev = NULL; - assignment(ls, &v, 1); - } -} - - -static void retstat (LexState *ls) { - /* stat -> RETURN explist */ - FuncState *fs = ls->fs; - expdesc e; - int first, nret; /* registers with returned values */ - luaX_next(ls); /* skip RETURN */ - if (block_follow(ls->t.token) || ls->t.token == ';') - first = nret = 0; /* return no values */ - else { - nret = explist1(ls, &e); /* optional return values */ - if (hasmultret(e.k)) { - luaK_setmultret(fs, &e); - if (e.k == VCALL && nret == 1) { /* tail call? */ - SET_OPCODE(getcode(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); - } - first = fs->nactvar; - nret = LUA_MULTRET; /* return all values */ - } - else { - if (nret == 1) /* only one single value? */ - first = luaK_exp2anyreg(fs, &e); - else { - luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ - first = fs->nactvar; /* return all `active' values */ - lua_assert(nret == fs->freereg - first); - } - } - } - luaK_ret(fs, first, nret); -} - - -static int statement (LexState *ls) { - int line = ls->linenumber; /* may be needed for error messages */ - switch (ls->t.token) { - case TK_IF: { /* stat -> ifstat */ - ifstat(ls, line); - return 0; - } - case TK_WHILE: { /* stat -> whilestat */ - whilestat(ls, line); - return 0; - } - case TK_DO: { /* stat -> DO block END */ - luaX_next(ls); /* skip DO */ - block(ls); - check_match(ls, TK_END, TK_DO, line); - return 0; - } - case TK_FOR: { /* stat -> forstat */ - forstat(ls, line); - return 0; - } - case TK_REPEAT: { /* stat -> repeatstat */ - repeatstat(ls, line); - return 0; - } - case TK_FUNCTION: { - funcstat(ls, line); /* stat -> funcstat */ - return 0; - } - case TK_LOCAL: { /* stat -> localstat */ - luaX_next(ls); /* skip LOCAL */ - if (testnext(ls, TK_FUNCTION)) /* local function? */ - localfunc(ls); - else - localstat(ls); - return 0; - } - case TK_RETURN: { /* stat -> retstat */ - retstat(ls); - return 1; /* must be last statement */ - } - case TK_BREAK: { /* stat -> breakstat */ - luaX_next(ls); /* skip BREAK */ - breakstat(ls); - return 1; /* must be last statement */ - } - default: { - exprstat(ls); - return 0; /* to avoid warnings */ - } - } -} - - -static void chunk (LexState *ls) { - /* chunk -> { stat [`;'] } */ - int islast = 0; - enterlevel(ls); - while (!islast && !block_follow(ls->t.token)) { - islast = statement(ls); - testnext(ls, ';'); - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* free registers */ - } - leavelevel(ls); -} - -/* }====================================================================== */ diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.h deleted file mode 100644 index 18836af..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lparser.h +++ /dev/null @@ -1,82 +0,0 @@ -/* -** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - -#ifndef lparser_h -#define lparser_h - -#include "llimits.h" -#include "lobject.h" -#include "lzio.h" - - -/* -** Expression descriptor -*/ - -typedef enum { - VVOID, /* no value */ - VNIL, - VTRUE, - VFALSE, - VK, /* info = index of constant in `k' */ - VKNUM, /* nval = numerical value */ - VLOCAL, /* info = local register */ - VUPVAL, /* info = index of upvalue in `upvalues' */ - VGLOBAL, /* info = index of table; aux = index of global name in `k' */ - VINDEXED, /* info = table register; aux = index register (or `k') */ - VJMP, /* info = instruction pc */ - VRELOCABLE, /* info = instruction pc */ - VNONRELOC, /* info = result register */ - VCALL, /* info = instruction pc */ - VVARARG /* info = instruction pc */ -} expkind; - -typedef struct expdesc { - expkind k; - union { - struct { int info, aux; } s; - lua_Number nval; - } u; - int t; /* patch list of `exit when true' */ - int f; /* patch list of `exit when false' */ -} expdesc; - - -typedef struct upvaldesc { - lu_byte k; - lu_byte info; -} upvaldesc; - - -struct BlockCnt; /* defined in lparser.c */ - - -/* state needed to generate code for a given function */ -typedef struct FuncState { - Proto *f; /* current function header */ - Table *h; /* table to find (and reuse) elements in `k' */ - struct FuncState *prev; /* enclosing function */ - struct LexState *ls; /* lexical state */ - struct lua_State *L; /* copy of the Lua state */ - struct BlockCnt *bl; /* chain of current blocks */ - int pc; /* next position to code (equivalent to `ncode') */ - int lasttarget; /* `pc' of last `jump target' */ - int jpc; /* list of pending jumps to `pc' */ - int freereg; /* first free register */ - int nk; /* number of elements in `k' */ - int np; /* number of elements in `p' */ - short nlocvars; /* number of elements in `locvars' */ - lu_byte nactvar; /* number of active local variables */ - upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ - unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ -} FuncState; - - -LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - const char *name); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.c deleted file mode 100644 index 4313b83..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.c +++ /dev/null @@ -1,214 +0,0 @@ -/* -** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ -** Global State -** See Copyright Notice in lua.h -*/ - - -#include - -#define lstate_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) -#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) -#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) - - -/* -** Main thread combines a thread state and the global state -*/ -typedef struct LG { - lua_State l; - global_State g; -} LG; - - - -static void stack_init (lua_State *L1, lua_State *L) { - /* initialize CallInfo array */ - L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); - L1->ci = L1->base_ci; - L1->size_ci = BASIC_CI_SIZE; - L1->end_ci = L1->base_ci + L1->size_ci - 1; - /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); - L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; - L1->top = L1->stack; - L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; - /* initialize first ci */ - L1->ci->func = L1->top; - setnilvalue(L1->top++); /* `function' entry for this `ci' */ - L1->base = L1->ci->base = L1->top; - L1->ci->top = L1->top + LUA_MINSTACK; -} - - -static void freestack (lua_State *L, lua_State *L1) { - luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); - luaM_freearray(L, L1->stack, L1->stacksize, TValue); -} - - -/* -** open parts that may cause memory-allocation errors -*/ -static void f_luaopen (lua_State *L, void *ud) { - global_State *g = G(L); - UNUSED(ud); - stack_init(L, L); /* init stack */ - sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ - sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ - luaT_init(L); - luaX_init(L); - luaS_fix(luaS_newliteral(L, MEMERRMSG)); - g->GCthreshold = 4*g->totalbytes; -} - - -static void preinit_state (lua_State *L, global_State *g) { - G(L) = g; - L->stack = NULL; - L->stacksize = 0; - L->errorJmp = NULL; - L->hook = NULL; - L->hookmask = 0; - L->basehookcount = 0; - L->allowhook = 1; - resethookcount(L); - L->openupval = NULL; - L->size_ci = 0; - L->nCcalls = L->baseCcalls = 0; - L->status = 0; - L->base_ci = L->ci = NULL; - L->savedpc = NULL; - L->errfunc = 0; - setnilvalue(gt(L)); -} - - -static void close_state (lua_State *L) { - global_State *g = G(L); - luaF_close(L, L->stack); /* close all upvalues for this thread */ - luaC_freeall(L); /* collect all objects */ - lua_assert(g->rootgc == obj2gco(L)); - lua_assert(g->strt.nuse == 0); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); - luaZ_freebuffer(L, &g->buff); - freestack(L, L); - lua_assert(g->totalbytes == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); -} - - -lua_State *luaE_newthread (lua_State *L) { - lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); - luaC_link(L, obj2gco(L1), LUA_TTHREAD); - preinit_state(L1, G(L)); - stack_init(L1, L); /* init stack */ - setobj2n(L, gt(L1), gt(L)); /* share table of globals */ - L1->hookmask = L->hookmask; - L1->basehookcount = L->basehookcount; - L1->hook = L->hook; - resethookcount(L1); - lua_assert(iswhite(obj2gco(L1))); - return L1; -} - - -void luaE_freethread (lua_State *L, lua_State *L1) { - luaF_close(L1, L1->stack); /* close all upvalues for this thread */ - lua_assert(L1->openupval == NULL); - luai_userstatefree(L1); - freestack(L, L1); - luaM_freemem(L, fromstate(L1), state_size(lua_State)); -} - - -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { - int i; - lua_State *L; - global_State *g; - void *l = (*f)(ud, NULL, 0, state_size(LG)); - if (l == NULL) return NULL; - L = tostate(l); - g = &((LG *)L)->g; - L->next = NULL; - L->tt = LUA_TTHREAD; - g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); - L->marked = luaC_white(g); - set2bits(L->marked, FIXEDBIT, SFIXEDBIT); - preinit_state(L, g); - g->frealloc = f; - g->ud = ud; - g->mainthread = L; - g->uvhead.u.l.prev = &g->uvhead; - g->uvhead.u.l.next = &g->uvhead; - g->GCthreshold = 0; /* mark it as unfinished state */ - g->strt.size = 0; - g->strt.nuse = 0; - g->strt.hash = NULL; - setnilvalue(registry(L)); - luaZ_initbuffer(L, &g->buff); - g->panic = NULL; - g->gcstate = GCSpause; - g->rootgc = obj2gco(L); - g->sweepstrgc = 0; - g->sweepgc = &g->rootgc; - g->gray = NULL; - g->grayagain = NULL; - g->weak = NULL; - g->tmudata = NULL; - g->totalbytes = sizeof(LG); - g->gcpause = LUAI_GCPAUSE; - g->gcstepmul = LUAI_GCMUL; - g->gcdept = 0; - for (i=0; imt[i] = NULL; - if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { - /* memory allocation error: free partial state */ - close_state(L); - L = NULL; - } - else - luai_userstateopen(L); - return L; -} - - -static void callallgcTM (lua_State *L, void *ud) { - UNUSED(ud); - luaC_callGCTM(L); /* call GC metamethods for all udata */ -} - - -LUA_API void lua_close (lua_State *L) { - L = G(L)->mainthread; /* only the main thread can be closed */ - lua_lock(L); - luaF_close(L, L->stack); /* close all upvalues for this thread */ - luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ - L->errfunc = 0; /* no error function during GC metamethods */ - do { /* repeat until no more errors */ - L->ci = L->base_ci; - L->base = L->top = L->ci->base; - L->nCcalls = L->baseCcalls = 0; - } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); - lua_assert(G(L)->tmudata == NULL); - luai_userstateclose(L); - close_state(L); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.h deleted file mode 100644 index 3bc575b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstate.h +++ /dev/null @@ -1,169 +0,0 @@ -/* -** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ -** Global State -** See Copyright Notice in lua.h -*/ - -#ifndef lstate_h -#define lstate_h - -#include "lua.h" - -#include "lobject.h" -#include "ltm.h" -#include "lzio.h" - - - -struct lua_longjmp; /* defined in ldo.c */ - - -/* table of globals */ -#define gt(L) (&L->l_gt) - -/* registry */ -#define registry(L) (&G(L)->l_registry) - - -/* extra stack space to handle TM calls and some other extras */ -#define EXTRA_STACK 5 - - -#define BASIC_CI_SIZE 8 - -#define BASIC_STACK_SIZE (2*LUA_MINSTACK) - - - -typedef struct stringtable { - GCObject **hash; - lu_int32 nuse; /* number of elements */ - int size; -} stringtable; - - -/* -** informations about a call -*/ -typedef struct CallInfo { - StkId base; /* base for this function */ - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ - const Instruction *savedpc; - int nresults; /* expected number of results from this function */ - int tailcalls; /* number of tail calls lost under this entry */ -} CallInfo; - - - -#define curr_func(L) (clvalue(L->ci->func)) -#define ci_func(ci) (clvalue((ci)->func)) -#define f_isLua(ci) (!ci_func(ci)->c.isC) -#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) - - -/* -** `global state', shared by all threads of this state -*/ -typedef struct global_State { - stringtable strt; /* hash table for strings */ - lua_Alloc frealloc; /* function to reallocate memory */ - void *ud; /* auxiliary data to `frealloc' */ - lu_byte currentwhite; - lu_byte gcstate; /* state of garbage collector */ - int sweepstrgc; /* position of sweep in `strt' */ - GCObject *rootgc; /* list of all collectable objects */ - GCObject **sweepgc; /* position of sweep in `rootgc' */ - GCObject *gray; /* list of gray objects */ - GCObject *grayagain; /* list of objects to be traversed atomically */ - GCObject *weak; /* list of weak tables (to be cleared) */ - GCObject *tmudata; /* last element of list of userdata to be GC */ - Mbuffer buff; /* temporary buffer for string concatentation */ - lu_mem GCthreshold; - lu_mem totalbytes; /* number of bytes currently allocated */ - lu_mem estimate; /* an estimate of number of bytes actually in use */ - lu_mem gcdept; /* how much GC is `behind schedule' */ - int gcpause; /* size of pause between successive GCs */ - int gcstepmul; /* GC `granularity' */ - lua_CFunction panic; /* to be called in unprotected errors */ - TValue l_registry; - struct lua_State *mainthread; - UpVal uvhead; /* head of double-linked list of all open upvalues */ - struct Table *mt[NUM_TAGS]; /* metatables for basic types */ - TString *tmname[TM_N]; /* array with tag-method names */ -} global_State; - - -/* -** `per thread' state -*/ -struct lua_State { - CommonHeader; - lu_byte status; - StkId top; /* first free slot in the stack */ - StkId base; /* base of current function */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - const Instruction *savedpc; /* `savedpc' of current function */ - StkId stack_last; /* last free slot in the stack */ - StkId stack; /* stack base */ - CallInfo *end_ci; /* points after end of ci array*/ - CallInfo *base_ci; /* array of CallInfo's */ - int stacksize; - int size_ci; /* size of array `base_ci' */ - unsigned short nCcalls; /* number of nested C calls */ - unsigned short baseCcalls; /* nested C calls when resuming coroutine */ - lu_byte hookmask; - lu_byte allowhook; - int basehookcount; - int hookcount; - lua_Hook hook; - TValue l_gt; /* table of globals */ - TValue env; /* temporary place for environments */ - GCObject *openupval; /* list of open upvalues in this stack */ - GCObject *gclist; - struct lua_longjmp *errorJmp; /* current error recover point */ - ptrdiff_t errfunc; /* current error handling function (stack index) */ -}; - - -#define G(L) (L->l_G) - - -/* -** Union of all collectable objects -*/ -union GCObject { - GCheader gch; - union TString ts; - union Udata u; - union Closure cl; - struct Table h; - struct Proto p; - struct UpVal uv; - struct lua_State th; /* thread */ -}; - - -/* macros to convert a GCObject into a specific value */ -#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) -#define gco2ts(o) (&rawgco2ts(o)->tsv) -#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) -#define gco2u(o) (&rawgco2u(o)->uv) -#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) -#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) -#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) -#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) -#define ngcotouv(o) \ - check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) -#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) - -/* macro to convert any Lua object into a GCObject */ -#define obj2gco(v) (cast(GCObject *, (v))) - - -LUAI_FUNC lua_State *luaE_newthread (lua_State *L); -LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.c deleted file mode 100644 index 4911315..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.c +++ /dev/null @@ -1,111 +0,0 @@ -/* -** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ -** String table (keeps all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - - -#include - -#define lstring_c -#define LUA_CORE - -#include "lua.h" - -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" - - - -void luaS_resize (lua_State *L, int newsize) { - GCObject **newhash; - stringtable *tb; - int i; - if (G(L)->gcstate == GCSsweepstring) - return; /* cannot resize during GC traverse */ - newhash = luaM_newvector(L, newsize, GCObject *); - tb = &G(L)->strt; - for (i=0; isize; i++) { - GCObject *p = tb->hash[i]; - while (p) { /* for each node in the list */ - GCObject *next = p->gch.next; /* save next */ - unsigned int h = gco2ts(p)->hash; - int h1 = lmod(h, newsize); /* new position */ - lua_assert(cast_int(h%newsize) == lmod(h, newsize)); - p->gch.next = newhash[h1]; /* chain it */ - newhash[h1] = p; - p = next; - } - } - luaM_freearray(L, tb->hash, tb->size, TString *); - tb->size = newsize; - tb->hash = newhash; -} - - -static TString *newlstr (lua_State *L, const char *str, size_t l, - unsigned int h) { - TString *ts; - stringtable *tb; - if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) - luaM_toobig(L); - ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); - ts->tsv.len = l; - ts->tsv.hash = h; - ts->tsv.marked = luaC_white(G(L)); - ts->tsv.tt = LUA_TSTRING; - ts->tsv.reserved = 0; - memcpy(ts+1, str, l*sizeof(char)); - ((char *)(ts+1))[l] = '\0'; /* ending 0 */ - tb = &G(L)->strt; - h = lmod(h, tb->size); - ts->tsv.next = tb->hash[h]; /* chain new entry */ - tb->hash[h] = obj2gco(ts); - tb->nuse++; - if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) - luaS_resize(L, tb->size*2); /* too crowded */ - return ts; -} - - -TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { - GCObject *o; - unsigned int h = cast(unsigned int, l); /* seed */ - size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ - size_t l1; - for (l1=l; l1>=step; l1-=step) /* compute hash */ - h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); - for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; - o != NULL; - o = o->gch.next) { - TString *ts = rawgco2ts(o); - if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { - /* string may be dead */ - if (isdead(G(L), o)) changewhite(o); - return ts; - } - } - return newlstr(L, str, l, h); /* not found */ -} - - -Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { - Udata *u; - if (s > MAX_SIZET - sizeof(Udata)) - luaM_toobig(L); - u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); - u->uv.marked = luaC_white(G(L)); /* is not finalized */ - u->uv.tt = LUA_TUSERDATA; - u->uv.len = s; - u->uv.metatable = NULL; - u->uv.env = e; - /* chain it on udata list (after main thread) */ - u->uv.next = G(L)->mainthread->next; - G(L)->mainthread->next = obj2gco(u); - return u; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.h deleted file mode 100644 index 73a2ff8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstring.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ -** String table (keep all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - -#ifndef lstring_h -#define lstring_h - - -#include "lgc.h" -#include "lobject.h" -#include "lstate.h" - - -#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) - -#define sizeudata(u) (sizeof(union Udata)+(u)->len) - -#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) -#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ - (sizeof(s)/sizeof(char))-1)) - -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) - -LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); -LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstrlib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstrlib.c deleted file mode 100644 index 7a03489..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lstrlib.c +++ /dev/null @@ -1,871 +0,0 @@ -/* -** $Id: lstrlib.c,v 1.132.1.5 2010/05/14 15:34:19 roberto Exp $ -** Standard library for string operations and pattern-matching -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include -#include - -#define lstrlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* macro to `unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - - -static int str_len (lua_State *L) { - size_t l; - luaL_checklstring(L, 1, &l); - lua_pushinteger(L, l); - return 1; -} - - -static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { - /* relative string position: negative means back from end */ - if (pos < 0) pos += (ptrdiff_t)len + 1; - return (pos >= 0) ? pos : 0; -} - - -static int str_sub (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l); - ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l); - if (start < 1) start = 1; - if (end > (ptrdiff_t)l) end = (ptrdiff_t)l; - if (start <= end) - lua_pushlstring(L, s+start-1, end-start+1); - else lua_pushliteral(L, ""); - return 1; -} - - -static int str_reverse (lua_State *L) { - size_t l; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - luaL_buffinit(L, &b); - while (l--) luaL_addchar(&b, s[l]); - luaL_pushresult(&b); - return 1; -} - - -static int str_lower (lua_State *L) { - size_t l; - size_t i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - luaL_buffinit(L, &b); - for (i=0; i 0) - luaL_addlstring(&b, s, l); - luaL_pushresult(&b); - return 1; -} - - -static int str_byte (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l); - ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l); - int n, i; - if (posi <= 0) posi = 1; - if ((size_t)pose > l) pose = l; - if (posi > pose) return 0; /* empty interval; return no values */ - n = (int)(pose - posi + 1); - if (posi + n <= pose) /* overflow? */ - luaL_error(L, "string slice too long"); - luaL_checkstack(L, n, "string slice too long"); - for (i=0; i= ms->level || ms->capture[l].len == CAP_UNFINISHED) - return luaL_error(ms->L, "invalid capture index"); - return l; -} - - -static int capture_to_close (MatchState *ms) { - int level = ms->level; - for (level--; level>=0; level--) - if (ms->capture[level].len == CAP_UNFINISHED) return level; - return luaL_error(ms->L, "invalid pattern capture"); -} - - -static const char *classend (MatchState *ms, const char *p) { - switch (*p++) { - case L_ESC: { - if (*p == '\0') - luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); - return p+1; - } - case '[': { - if (*p == '^') p++; - do { /* look for a `]' */ - if (*p == '\0') - luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); - if (*(p++) == L_ESC && *p != '\0') - p++; /* skip escapes (e.g. `%]') */ - } while (*p != ']'); - return p+1; - } - default: { - return p; - } - } -} - - -static int match_class (int c, int cl) { - int res; - switch (tolower(cl)) { - case 'a' : res = isalpha(c); break; - case 'c' : res = iscntrl(c); break; - case 'd' : res = isdigit(c); break; - case 'l' : res = islower(c); break; - case 'p' : res = ispunct(c); break; - case 's' : res = isspace(c); break; - case 'u' : res = isupper(c); break; - case 'w' : res = isalnum(c); break; - case 'x' : res = isxdigit(c); break; - case 'z' : res = (c == 0); break; - default: return (cl == c); - } - return (islower(cl) ? res : !res); -} - - -static int matchbracketclass (int c, const char *p, const char *ec) { - int sig = 1; - if (*(p+1) == '^') { - sig = 0; - p++; /* skip the `^' */ - } - while (++p < ec) { - if (*p == L_ESC) { - p++; - if (match_class(c, uchar(*p))) - return sig; - } - else if ((*(p+1) == '-') && (p+2 < ec)) { - p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) - return sig; - } - else if (uchar(*p) == c) return sig; - } - return !sig; -} - - -static int singlematch (int c, const char *p, const char *ep) { - switch (*p) { - case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); - case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); - } -} - - -static const char *match (MatchState *ms, const char *s, const char *p); - - -static const char *matchbalance (MatchState *ms, const char *s, - const char *p) { - if (*p == 0 || *(p+1) == 0) - luaL_error(ms->L, "unbalanced pattern"); - if (*s != *p) return NULL; - else { - int b = *p; - int e = *(p+1); - int cont = 1; - while (++s < ms->src_end) { - if (*s == e) { - if (--cont == 0) return s+1; - } - else if (*s == b) cont++; - } - } - return NULL; /* string ends out of balance */ -} - - -static const char *max_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - ptrdiff_t i = 0; /* counts maximum expand for item */ - while ((s+i)src_end && singlematch(uchar(*(s+i)), p, ep)) - i++; - /* keeps trying to match with the maximum repetitions */ - while (i>=0) { - const char *res = match(ms, (s+i), ep+1); - if (res) return res; - i--; /* else didn't match; reduce 1 repetition to try again */ - } - return NULL; -} - - -static const char *min_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - for (;;) { - const char *res = match(ms, s, ep+1); - if (res != NULL) - return res; - else if (ssrc_end && singlematch(uchar(*s), p, ep)) - s++; /* try with one more repetition */ - else return NULL; - } -} - - -static const char *start_capture (MatchState *ms, const char *s, - const char *p, int what) { - const char *res; - int level = ms->level; - if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); - ms->capture[level].init = s; - ms->capture[level].len = what; - ms->level = level+1; - if ((res=match(ms, s, p)) == NULL) /* match failed? */ - ms->level--; /* undo capture */ - return res; -} - - -static const char *end_capture (MatchState *ms, const char *s, - const char *p) { - int l = capture_to_close(ms); - const char *res; - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ - if ((res = match(ms, s, p)) == NULL) /* match failed? */ - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ - return res; -} - - -static const char *match_capture (MatchState *ms, const char *s, int l) { - size_t len; - l = check_capture(ms, l); - len = ms->capture[l].len; - if ((size_t)(ms->src_end-s) >= len && - memcmp(ms->capture[l].init, s, len) == 0) - return s+len; - else return NULL; -} - - -static const char *match (MatchState *ms, const char *s, const char *p) { - init: /* using goto's to optimize tail recursion */ - switch (*p) { - case '(': { /* start capture */ - if (*(p+1) == ')') /* position capture? */ - return start_capture(ms, s, p+2, CAP_POSITION); - else - return start_capture(ms, s, p+1, CAP_UNFINISHED); - } - case ')': { /* end capture */ - return end_capture(ms, s, p+1); - } - case L_ESC: { - switch (*(p+1)) { - case 'b': { /* balanced string? */ - s = matchbalance(ms, s, p+2); - if (s == NULL) return NULL; - p+=4; goto init; /* else return match(ms, s, p+4); */ - } - case 'f': { /* frontier? */ - const char *ep; char previous; - p += 2; - if (*p != '[') - luaL_error(ms->L, "missing " LUA_QL("[") " after " - LUA_QL("%%f") " in pattern"); - ep = classend(ms, p); /* points to what is next */ - previous = (s == ms->src_init) ? '\0' : *(s-1); - if (matchbracketclass(uchar(previous), p, ep-1) || - !matchbracketclass(uchar(*s), p, ep-1)) return NULL; - p=ep; goto init; /* else return match(ms, s, ep); */ - } - default: { - if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p+1))); - if (s == NULL) return NULL; - p+=2; goto init; /* else return match(ms, s, p+2) */ - } - goto dflt; /* case default */ - } - } - } - case '\0': { /* end of pattern */ - return s; /* match succeeded */ - } - case '$': { - if (*(p+1) == '\0') /* is the `$' the last char in pattern? */ - return (s == ms->src_end) ? s : NULL; /* check end of string */ - else goto dflt; - } - default: dflt: { /* it is a pattern item */ - const char *ep = classend(ms, p); /* points to what is next */ - int m = ssrc_end && singlematch(uchar(*s), p, ep); - switch (*ep) { - case '?': { /* optional */ - const char *res; - if (m && ((res=match(ms, s+1, ep+1)) != NULL)) - return res; - p=ep+1; goto init; /* else return match(ms, s, ep+1); */ - } - case '*': { /* 0 or more repetitions */ - return max_expand(ms, s, p, ep); - } - case '+': { /* 1 or more repetitions */ - return (m ? max_expand(ms, s+1, p, ep) : NULL); - } - case '-': { /* 0 or more repetitions (minimum) */ - return min_expand(ms, s, p, ep); - } - default: { - if (!m) return NULL; - s++; p=ep; goto init; /* else return match(ms, s+1, ep); */ - } - } - } - } -} - - - -static const char *lmemfind (const char *s1, size_t l1, - const char *s2, size_t l2) { - if (l2 == 0) return s1; /* empty strings are everywhere */ - else if (l2 > l1) return NULL; /* avoids a negative `l1' */ - else { - const char *init; /* to search for a `*s2' inside `s1' */ - l2--; /* 1st char will be checked by `memchr' */ - l1 = l1-l2; /* `s2' cannot be found after that */ - while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { - init++; /* 1st char is already checked */ - if (memcmp(init, s2+1, l2) == 0) - return init-1; - else { /* correct `l1' and `s1' to try again */ - l1 -= init-s1; - s1 = init; - } - } - return NULL; /* not found */ - } -} - - -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { - if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, e - s); /* add whole match */ - else - luaL_error(ms->L, "invalid capture index"); - } - else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); - if (l == CAP_POSITION) - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, l); - } -} - - -static int push_captures (MatchState *ms, const char *s, const char *e) { - int i; - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; - luaL_checkstack(ms->L, nlevels, "too many captures"); - for (i = 0; i < nlevels; i++) - push_onecapture(ms, i, s, e); - return nlevels; /* number of strings pushed */ -} - - -static int str_find_aux (lua_State *L, int find) { - size_t l1, l2; - const char *s = luaL_checklstring(L, 1, &l1); - const char *p = luaL_checklstring(L, 2, &l2); - ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; - if (init < 0) init = 0; - else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; - if (find && (lua_toboolean(L, 4) || /* explicit request? */ - strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ - /* do a plain search */ - const char *s2 = lmemfind(s+init, l1-init, p, l2); - if (s2) { - lua_pushinteger(L, s2-s+1); - lua_pushinteger(L, s2-s+l2); - return 2; - } - } - else { - MatchState ms; - int anchor = (*p == '^') ? (p++, 1) : 0; - const char *s1=s+init; - ms.L = L; - ms.src_init = s; - ms.src_end = s+l1; - do { - const char *res; - ms.level = 0; - if ((res=match(&ms, s1, p)) != NULL) { - if (find) { - lua_pushinteger(L, s1-s+1); /* start */ - lua_pushinteger(L, res-s); /* end */ - return push_captures(&ms, NULL, 0) + 2; - } - else - return push_captures(&ms, s1, res); - } - } while (s1++ < ms.src_end && !anchor); - } - lua_pushnil(L); /* not found */ - return 1; -} - - -static int str_find (lua_State *L) { - return str_find_aux(L, 1); -} - - -static int str_match (lua_State *L) { - return str_find_aux(L, 0); -} - - -static int gmatch_aux (lua_State *L) { - MatchState ms; - size_t ls; - const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); - const char *p = lua_tostring(L, lua_upvalueindex(2)); - const char *src; - ms.L = L; - ms.src_init = s; - ms.src_end = s+ls; - for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); - src <= ms.src_end; - src++) { - const char *e; - ms.level = 0; - if ((e = match(&ms, src, p)) != NULL) { - lua_Integer newstart = e-s; - if (e == src) newstart++; /* empty match? go at least one position */ - lua_pushinteger(L, newstart); - lua_replace(L, lua_upvalueindex(3)); - return push_captures(&ms, src, e); - } - } - return 0; /* not found */ -} - - -static int gmatch (lua_State *L) { - luaL_checkstring(L, 1); - luaL_checkstring(L, 2); - lua_settop(L, 2); - lua_pushinteger(L, 0); - lua_pushcclosure(L, gmatch_aux, 3); - return 1; -} - - -static int gfind_nodef (lua_State *L) { - return luaL_error(L, LUA_QL("string.gfind") " was renamed to " - LUA_QL("string.gmatch")); -} - - -static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e) { - size_t l, i; - const char *news = lua_tolstring(ms->L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) - luaL_addchar(b, news[i]); - else { - i++; /* skip ESC */ - if (!isdigit(uchar(news[i]))) - luaL_addchar(b, news[i]); - else if (news[i] == '0') - luaL_addlstring(b, s, e - s); - else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_addvalue(b); /* add capture to accumulated result */ - } - } - } -} - - -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e) { - lua_State *L = ms->L; - switch (lua_type(L, 3)) { - case LUA_TNUMBER: - case LUA_TSTRING: { - add_s(ms, b, s, e); - return; - } - case LUA_TFUNCTION: { - int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); - break; - } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); - lua_gettable(L, 3); - break; - } - } - if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, e - s); /* keep original text */ - } - else if (!lua_isstring(L, -1)) - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); - luaL_addvalue(b); /* add result to accumulator */ -} - - -static int str_gsub (lua_State *L) { - size_t srcl; - const char *src = luaL_checklstring(L, 1, &srcl); - const char *p = luaL_checkstring(L, 2); - int tr = lua_type(L, 3); - int max_s = luaL_optint(L, 4, srcl+1); - int anchor = (*p == '^') ? (p++, 1) : 0; - int n = 0; - MatchState ms; - luaL_Buffer b; - luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || - tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, - "string/function/table expected"); - luaL_buffinit(L, &b); - ms.L = L; - ms.src_init = src; - ms.src_end = src+srcl; - while (n < max_s) { - const char *e; - ms.level = 0; - e = match(&ms, src, p); - if (e) { - n++; - add_value(&ms, &b, src, e); - } - if (e && e>src) /* non empty match? */ - src = e; /* skip it */ - else if (src < ms.src_end) - luaL_addchar(&b, *src++); - else break; - if (anchor) break; - } - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); - lua_pushinteger(L, n); /* number of substitutions */ - return 2; -} - -/* }====================================================== */ - - -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_ITEM 512 -/* valid flags in a format specification */ -#define FLAGS "-+ #0" -/* -** maximum size of each format specification (such as '%-099.99d') -** (+10 accounts for %99.99x plus margin of error) -*/ -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) - - -static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - luaL_addchar(b, '"'); - while (l--) { - switch (*s) { - case '"': case '\\': case '\n': { - luaL_addchar(b, '\\'); - luaL_addchar(b, *s); - break; - } - case '\r': { - luaL_addlstring(b, "\\r", 2); - break; - } - case '\0': { - luaL_addlstring(b, "\\000", 4); - break; - } - default: { - luaL_addchar(b, *s); - break; - } - } - s++; - } - luaL_addchar(b, '"'); -} - -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { - const char *p = strfrmt; - while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) p++; /* skip precision */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); - *(form++) = '%'; - strncpy(form, strfrmt, p - strfrmt + 1); - form += p - strfrmt + 1; - *form = '\0'; - return p; -} - - -static void addintlen (char *form) { - size_t l = strlen(form); - char spec = form[l - 1]; - strcpy(form + l - 1, LUA_INTFRMLEN); - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; -} - - -static int str_format (lua_State *L) { - int top = lua_gettop(L); - int arg = 1; - size_t sfl; - const char *strfrmt = luaL_checklstring(L, arg, &sfl); - const char *strfrmt_end = strfrmt+sfl; - luaL_Buffer b; - luaL_buffinit(L, &b); - while (strfrmt < strfrmt_end) { - if (*strfrmt != L_ESC) - luaL_addchar(&b, *strfrmt++); - else if (*++strfrmt == L_ESC) - luaL_addchar(&b, *strfrmt++); /* %% */ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ - char buff[MAX_ITEM]; /* to store the formatted item */ - if (++arg > top) - luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { - sprintf(buff, form, (int)luaL_checknumber(L, arg)); - break; - } - case 'd': case 'i': { - addintlen(form); - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); - break; - } - case 'o': case 'u': case 'x': case 'X': { - addintlen(form); - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); - break; - } - case 'e': case 'E': case 'f': - case 'g': case 'G': { - sprintf(buff, form, (double)luaL_checknumber(L, arg)); - break; - } - case 'q': { - addquoted(L, &b, arg); - continue; /* skip the 'addsize' at the end */ - } - case 's': { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - if (!strchr(form, '.') && l >= 100) { - /* no precision and string is too long to be formatted; - keep original string */ - lua_pushvalue(L, arg); - luaL_addvalue(&b); - continue; /* skip the `addsize' at the end */ - } - else { - sprintf(buff, form, s); - break; - } - } - default: { /* also treat cases `pnLlh' */ - return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " - LUA_QL("format"), *(strfrmt - 1)); - } - } - luaL_addlstring(&b, buff, strlen(buff)); - } - } - luaL_pushresult(&b); - return 1; -} - - -static const luaL_Reg strlib[] = { - {"byte", str_byte}, - {"char", str_char}, - {"dump", str_dump}, - {"find", str_find}, - {"format", str_format}, - {"gfind", gfind_nodef}, - {"gmatch", gmatch}, - {"gsub", str_gsub}, - {"len", str_len}, - {"lower", str_lower}, - {"match", str_match}, - {"rep", str_rep}, - {"reverse", str_reverse}, - {"sub", str_sub}, - {"upper", str_upper}, - {NULL, NULL} -}; - - -static void createmetatable (lua_State *L) { - lua_createtable(L, 0, 1); /* create metatable for strings */ - lua_pushliteral(L, ""); /* dummy string */ - lua_pushvalue(L, -2); - lua_setmetatable(L, -2); /* set string metatable */ - lua_pop(L, 1); /* pop dummy string */ - lua_pushvalue(L, -2); /* string library... */ - lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ - lua_pop(L, 1); /* pop metatable */ -} - - -/* -** Open string library -*/ -LUALIB_API int luaopen_string (lua_State *L) { - luaL_register(L, LUA_STRLIBNAME, strlib); -#if defined(LUA_COMPAT_GFIND) - lua_getfield(L, -1, "gmatch"); - lua_setfield(L, -2, "gfind"); -#endif - createmetatable(L); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.c deleted file mode 100644 index ec84f4f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.c +++ /dev/null @@ -1,588 +0,0 @@ -/* -** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - - -/* -** Implementation of tables (aka arrays, objects, or hash tables). -** Tables keep its elements in two parts: an array part and a hash part. -** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest `n' such that at -** least half the slots between 0 and n are in use. -** Hash uses a mix of chained scatter table with Brent's variation. -** A main invariant of these tables is that, if an element is not -** in its main position (i.e. the `original' position that its hash gives -** to it), then the colliding element is in its own main position. -** Hence even when the load factor reaches 100%, performance remains good. -*/ - -#include -#include - -#define ltable_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "ltable.h" - - -/* -** max size of array part is 2^MAXBITS -*/ -#if LUAI_BITSINT > 26 -#define MAXBITS 26 -#else -#define MAXBITS (LUAI_BITSINT-2) -#endif - -#define MAXASIZE (1 << MAXBITS) - - -#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) - -#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) -#define hashboolean(t,p) hashpow2(t, p) - - -/* -** for some types, it is better to avoid modulus by power of 2, as -** they tend to have many 2 factors. -*/ -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) - - -#define hashpointer(t,p) hashmod(t, IntPoint(p)) - - -/* -** number of ints inside a lua_Number -*/ -#define numints cast_int(sizeof(lua_Number)/sizeof(int)) - - - -#define dummynode (&dummynode_) - -static const Node dummynode_ = { - {{NULL}, LUA_TNIL}, /* value */ - {{{NULL}, LUA_TNIL, NULL}} /* key */ -}; - - -/* -** hash for lua_Numbers -*/ -static Node *hashnum (const Table *t, lua_Number n) { - unsigned int a[numints]; - int i; - if (luai_numeq(n, 0)) /* avoid problems with -0 */ - return gnode(t, 0); - memcpy(a, &n, sizeof(a)); - for (i = 1; i < numints; i++) a[0] += a[i]; - return hashmod(t, a[0]); -} - - - -/* -** returns the `main' position of an element in a table (that is, the index -** of its hash value) -*/ -static Node *mainposition (const Table *t, const TValue *key) { - switch (ttype(key)) { - case LUA_TNUMBER: - return hashnum(t, nvalue(key)); - case LUA_TSTRING: - return hashstr(t, rawtsvalue(key)); - case LUA_TBOOLEAN: - return hashboolean(t, bvalue(key)); - case LUA_TLIGHTUSERDATA: - return hashpointer(t, pvalue(key)); - default: - return hashpointer(t, gcvalue(key)); - } -} - - -/* -** returns the index for `key' if `key' is an appropriate key to live in -** the array part of the table, -1 otherwise. -*/ -static int arrayindex (const TValue *key) { - if (ttisnumber(key)) { - lua_Number n = nvalue(key); - int k; - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) - return k; - } - return -1; /* `key' did not match some condition */ -} - - -/* -** returns the index of a `key' for table traversals. First goes all -** elements in the array part, then elements in the hash part. The -** beginning of a traversal is signalled by -1. -*/ -static int findindex (lua_State *L, Table *t, StkId key) { - int i; - if (ttisnil(key)) return -1; /* first iteration */ - i = arrayindex(key); - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ - return i-1; /* yes; that's the index (corrected to C) */ - else { - Node *n = mainposition(t, key); - do { /* check whether `key' is somewhere in the chain */ - /* key may be dead already, but it is ok to use it in `next' */ - if (luaO_rawequalObj(key2tval(n), key) || - (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && - gcvalue(gkey(n)) == gcvalue(key))) { - i = cast_int(n - gnode(t, 0)); /* key index in hash table */ - /* hash elements are numbered after array ones */ - return i + t->sizearray; - } - else n = gnext(n); - } while (n); - luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ - return 0; /* to avoid warnings */ - } -} - - -int luaH_next (lua_State *L, Table *t, StkId key) { - int i = findindex(L, t, key); /* find original element */ - for (i++; i < t->sizearray; i++) { /* try first array part */ - if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setnvalue(key, cast_num(i+1)); - setobj2s(L, key+1, &t->array[i]); - return 1; - } - } - for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ - if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ - setobj2s(L, key, key2tval(gnode(t, i))); - setobj2s(L, key+1, gval(gnode(t, i))); - return 1; - } - } - return 0; /* no more elements */ -} - - -/* -** {============================================================= -** Rehash -** ============================================================== -*/ - - -static int computesizes (int nums[], int *narray) { - int i; - int twotoi; /* 2^i */ - int a = 0; /* number of elements smaller than 2^i */ - int na = 0; /* number of elements to go to array part */ - int n = 0; /* optimal size for array part */ - for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ - n = twotoi; /* optimal size (till now) */ - na = a; /* all elements smaller than n will go to array part */ - } - } - if (a == *narray) break; /* all elements already counted */ - } - *narray = n; - lua_assert(*narray/2 <= na && na <= *narray); - return na; -} - - -static int countint (const TValue *key, int *nums) { - int k = arrayindex(key); - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ - nums[ceillog2(k)]++; /* count as such */ - return 1; - } - else - return 0; -} - - -static int numusearray (const Table *t, int *nums) { - int lg; - int ttlg; /* 2^lg */ - int ause = 0; /* summation of `nums' */ - int i = 1; /* count to traverse all array keys */ - for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ - int lc = 0; /* counter */ - int lim = ttlg; - if (lim > t->sizearray) { - lim = t->sizearray; /* adjust upper limit */ - if (i > lim) - break; /* no more elements to count */ - } - /* count elements in range (2^(lg-1), 2^lg] */ - for (; i <= lim; i++) { - if (!ttisnil(&t->array[i-1])) - lc++; - } - nums[lg] += lc; - ause += lc; - } - return ause; -} - - -static int numusehash (const Table *t, int *nums, int *pnasize) { - int totaluse = 0; /* total number of elements */ - int ause = 0; /* summation of `nums' */ - int i = sizenode(t); - while (i--) { - Node *n = &t->node[i]; - if (!ttisnil(gval(n))) { - ause += countint(key2tval(n), nums); - totaluse++; - } - } - *pnasize += ause; - return totaluse; -} - - -static void setarrayvector (lua_State *L, Table *t, int size) { - int i; - luaM_reallocvector(L, t->array, t->sizearray, size, TValue); - for (i=t->sizearray; iarray[i]); - t->sizearray = size; -} - - -static void setnodevector (lua_State *L, Table *t, int size) { - int lsize; - if (size == 0) { /* no elements to hash part? */ - t->node = cast(Node *, dummynode); /* use common `dummynode' */ - lsize = 0; - } - else { - int i; - lsize = ceillog2(size); - if (lsize > MAXBITS) - luaG_runerror(L, "table overflow"); - size = twoto(lsize); - t->node = luaM_newvector(L, size, Node); - for (i=0; ilsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ -} - - -static void resize (lua_State *L, Table *t, int nasize, int nhsize) { - int i; - int oldasize = t->sizearray; - int oldhsize = t->lsizenode; - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); - /* create new hash part with appropriate size */ - setnodevector(L, t, nhsize); - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ - for (i=nasize; iarray[i])) - setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); - } - /* shrink array */ - luaM_reallocvector(L, t->array, oldasize, nasize, TValue); - } - /* re-insert elements from hash part */ - for (i = twoto(oldhsize) - 1; i >= 0; i--) { - Node *old = nold+i; - if (!ttisnil(gval(old))) - setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); - } - if (nold != dummynode) - luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ -} - - -void luaH_resizearray (lua_State *L, Table *t, int nasize) { - int nsize = (t->node == dummynode) ? 0 : sizenode(t); - resize(L, t, nasize, nsize); -} - - -static void rehash (lua_State *L, Table *t, const TValue *ek) { - int nasize, na; - int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ - int i; - int totaluse; - for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ - nasize = numusearray(t, nums); /* count keys in array part */ - totaluse = nasize; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ - /* count extra key */ - nasize += countint(ek, nums); - totaluse++; - /* compute new size for array part */ - na = computesizes(nums, &nasize); - /* resize the table to new computed sizes */ - resize(L, t, nasize, totaluse - na); -} - - - -/* -** }============================================================= -*/ - - -Table *luaH_new (lua_State *L, int narray, int nhash) { - Table *t = luaM_new(L, Table); - luaC_link(L, obj2gco(t), LUA_TTABLE); - t->metatable = NULL; - t->flags = cast_byte(~0); - /* temporary values (kept only if some malloc fails) */ - t->array = NULL; - t->sizearray = 0; - t->lsizenode = 0; - t->node = cast(Node *, dummynode); - setarrayvector(L, t, narray); - setnodevector(L, t, nhash); - return t; -} - - -void luaH_free (lua_State *L, Table *t) { - if (t->node != dummynode) - luaM_freearray(L, t->node, sizenode(t), Node); - luaM_freearray(L, t->array, t->sizearray, TValue); - luaM_free(L, t); -} - - -static Node *getfreepos (Table *t) { - while (t->lastfree-- > t->node) { - if (ttisnil(gkey(t->lastfree))) - return t->lastfree; - } - return NULL; /* could not find a free place */ -} - - - -/* -** inserts a new key into a hash table; first, check whether key's main -** position is free. If not, check whether colliding node is in its main -** position or not: if it is not, move colliding node to an empty place and -** put new key in its main position; otherwise (colliding node is in its main -** position), new key goes to an empty position. -*/ -static TValue *newkey (lua_State *L, Table *t, const TValue *key) { - Node *mp = mainposition(t, key); - if (!ttisnil(gval(mp)) || mp == dummynode) { - Node *othern; - Node *n = getfreepos(t); /* get a free place */ - if (n == NULL) { /* cannot find a free place? */ - rehash(L, t, key); /* grow table */ - return luaH_set(L, t, key); /* re-insert key into grown table */ - } - lua_assert(n != dummynode); - othern = mainposition(t, key2tval(mp)); - if (othern != mp) { /* is colliding node out of its main position? */ - /* yes; move colliding node into free position */ - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ - gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ - *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ - gnext(mp) = NULL; /* now `mp' is free */ - setnilvalue(gval(mp)); - } - else { /* colliding node is in its own main position */ - /* new node will go into free position */ - gnext(n) = gnext(mp); /* chain new position */ - gnext(mp) = n; - mp = n; - } - } - gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; - luaC_barriert(L, t, key); - lua_assert(ttisnil(gval(mp))); - return gval(mp); -} - - -/* -** search function for integers -*/ -const TValue *luaH_getnum (Table *t, int key) { - /* (1 <= key && key <= t->sizearray) */ - if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) - return &t->array[key-1]; - else { - lua_Number nk = cast_num(key); - Node *n = hashnum(t, nk); - do { /* check whether `key' is somewhere in the chain */ - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; - } -} - - -/* -** search function for strings -*/ -const TValue *luaH_getstr (Table *t, TString *key) { - Node *n = hashstr(t, key); - do { /* check whether `key' is somewhere in the chain */ - if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; -} - - -/* -** main search function -*/ -const TValue *luaH_get (Table *t, const TValue *key) { - switch (ttype(key)) { - case LUA_TNIL: return luaO_nilobject; - case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); - case LUA_TNUMBER: { - int k; - lua_Number n = nvalue(key); - lua_number2int(k, n); - if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ - return luaH_getnum(t, k); /* use specialized version */ - /* else go through */ - } - default: { - Node *n = mainposition(t, key); - do { /* check whether `key' is somewhere in the chain */ - if (luaO_rawequalObj(key2tval(n), key)) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; - } - } -} - - -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p = luaH_get(t, key); - t->flags = 0; - if (p != luaO_nilobject) - return cast(TValue *, p); - else { - if (ttisnil(key)) luaG_runerror(L, "table index is nil"); - else if (ttisnumber(key) && luai_numisnan(nvalue(key))) - luaG_runerror(L, "table index is NaN"); - return newkey(L, t, key); - } -} - - -TValue *luaH_setnum (lua_State *L, Table *t, int key) { - const TValue *p = luaH_getnum(t, key); - if (p != luaO_nilobject) - return cast(TValue *, p); - else { - TValue k; - setnvalue(&k, cast_num(key)); - return newkey(L, t, &k); - } -} - - -TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { - const TValue *p = luaH_getstr(t, key); - if (p != luaO_nilobject) - return cast(TValue *, p); - else { - TValue k; - setsvalue(L, &k, key); - return newkey(L, t, &k); - } -} - - -static int unbound_search (Table *t, unsigned int j) { - unsigned int i = j; /* i is zero or a present index */ - j++; - /* find `i' and `j' such that i is present and j is not */ - while (!ttisnil(luaH_getnum(t, j))) { - i = j; - j *= 2; - if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ - i = 1; - while (!ttisnil(luaH_getnum(t, i))) i++; - return i - 1; - } - } - /* now do a binary search between them */ - while (j - i > 1) { - unsigned int m = (i+j)/2; - if (ttisnil(luaH_getnum(t, m))) j = m; - else i = m; - } - return i; -} - - -/* -** Try to find a boundary in table `t'. A `boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). -*/ -int luaH_getn (Table *t) { - unsigned int j = t->sizearray; - if (j > 0 && ttisnil(&t->array[j - 1])) { - /* there is a boundary in the array part: (binary) search for it */ - unsigned int i = 0; - while (j - i > 1) { - unsigned int m = (i+j)/2; - if (ttisnil(&t->array[m - 1])) j = m; - else i = m; - } - return i; - } - /* else must find a boundary in hash part */ - else if (t->node == dummynode) /* hash part is empty? */ - return j; /* that is easy... */ - else return unbound_search(t, j); -} - - - -#if defined(LUA_DEBUG) - -Node *luaH_mainposition (const Table *t, const TValue *key) { - return mainposition(t, key); -} - -int luaH_isdummy (Node *n) { return n == dummynode; } - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.h deleted file mode 100644 index f5b9d5e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltable.h +++ /dev/null @@ -1,40 +0,0 @@ -/* -** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - -#ifndef ltable_h -#define ltable_h - -#include "lobject.h" - - -#define gnode(t,i) (&(t)->node[i]) -#define gkey(n) (&(n)->i_key.nk) -#define gval(n) (&(n)->i_val) -#define gnext(n) ((n)->i_key.nk.next) - -#define key2tval(n) (&(n)->i_key.tvk) - - -LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); -LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); -LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); -LUAI_FUNC void luaH_free (lua_State *L, Table *t); -LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC int luaH_getn (Table *t); - - -#if defined(LUA_DEBUG) -LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (Node *n); -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltablib.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltablib.c deleted file mode 100644 index b6d9cb4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltablib.c +++ /dev/null @@ -1,287 +0,0 @@ -/* -** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $ -** Library for Table Manipulation -** See Copyright Notice in lua.h -*/ - - -#include - -#define ltablib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) - - -static int foreachi (lua_State *L) { - int i; - int n = aux_getn(L, 1); - luaL_checktype(L, 2, LUA_TFUNCTION); - for (i=1; i <= n; i++) { - lua_pushvalue(L, 2); /* function */ - lua_pushinteger(L, i); /* 1st argument */ - lua_rawgeti(L, 1, i); /* 2nd argument */ - lua_call(L, 2, 1); - if (!lua_isnil(L, -1)) - return 1; - lua_pop(L, 1); /* remove nil result */ - } - return 0; -} - - -static int foreach (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checktype(L, 2, LUA_TFUNCTION); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pushvalue(L, 2); /* function */ - lua_pushvalue(L, -3); /* key */ - lua_pushvalue(L, -3); /* value */ - lua_call(L, 2, 1); - if (!lua_isnil(L, -1)) - return 1; - lua_pop(L, 2); /* remove value and result */ - } - return 0; -} - - -static int maxn (lua_State *L) { - lua_Number max = 0; - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pop(L, 1); /* remove value */ - if (lua_type(L, -1) == LUA_TNUMBER) { - lua_Number v = lua_tonumber(L, -1); - if (v > max) max = v; - } - } - lua_pushnumber(L, max); - return 1; -} - - -static int getn (lua_State *L) { - lua_pushinteger(L, aux_getn(L, 1)); - return 1; -} - - -static int setn (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); -#ifndef luaL_setn - luaL_setn(L, 1, luaL_checkint(L, 2)); -#else - luaL_error(L, LUA_QL("setn") " is obsolete"); -#endif - lua_pushvalue(L, 1); - return 1; -} - - -static int tinsert (lua_State *L) { - int e = aux_getn(L, 1) + 1; /* first empty element */ - int pos; /* where to insert new element */ - switch (lua_gettop(L)) { - case 2: { /* called with only 2 arguments */ - pos = e; /* insert new element at the end */ - break; - } - case 3: { - int i; - pos = luaL_checkint(L, 2); /* 2nd argument is the position */ - if (pos > e) e = pos; /* `grow' array if necessary */ - for (i = e; i > pos; i--) { /* move up elements */ - lua_rawgeti(L, 1, i-1); - lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ - } - break; - } - default: { - return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); - } - } - luaL_setn(L, 1, e); /* new size */ - lua_rawseti(L, 1, pos); /* t[pos] = v */ - return 0; -} - - -static int tremove (lua_State *L) { - int e = aux_getn(L, 1); - int pos = luaL_optint(L, 2, e); - if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ - return 0; /* nothing to remove */ - luaL_setn(L, 1, e - 1); /* t.n = n-1 */ - lua_rawgeti(L, 1, pos); /* result = t[pos] */ - for ( ;pos= P */ - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i>u) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[i] */ - } - /* repeat --j until a[j] <= P */ - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j - -#define ltm_c -#define LUA_CORE - -#include "lua.h" - -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - - -const char *const luaT_typenames[] = { - "nil", "boolean", "userdata", "number", - "string", "table", "function", "userdata", "thread", - "proto", "upval" -}; - - -void luaT_init (lua_State *L) { - static const char *const luaT_eventname[] = { /* ORDER TM */ - "__index", "__newindex", - "__gc", "__mode", "__eq", - "__add", "__sub", "__mul", "__div", "__mod", - "__pow", "__unm", "__len", "__lt", "__le", - "__concat", "__call" - }; - int i; - for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); - luaS_fix(G(L)->tmname[i]); /* never collect these names */ - } -} - - -/* -** function to be used with macro "fasttm": optimized for absence of -** tag methods -*/ -const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getstr(events, ename); - lua_assert(event <= TM_EQ); - if (ttisnil(tm)) { /* no tag method? */ - events->flags |= cast_byte(1u<metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(o)->metatable; - break; - default: - mt = G(L)->mt[ttype(o)]; - } - return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.h deleted file mode 100644 index 64343b7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/ltm.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#ifndef ltm_h -#define ltm_h - - -#include "lobject.h" - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER TM" -*/ -typedef enum { - TM_INDEX, - TM_NEWINDEX, - TM_GC, - TM_MODE, - TM_EQ, /* last tag method with `fast' access */ - TM_ADD, - TM_SUB, - TM_MUL, - TM_DIV, - TM_MOD, - TM_POW, - TM_UNM, - TM_LEN, - TM_LT, - TM_LE, - TM_CONCAT, - TM_CALL, - TM_N /* number of elements in the enum */ -} TMS; - - - -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) - -#define fasttm(l,et,e) gfasttm(G(l), et, e) - -LUAI_DATA const char *const luaT_typenames[]; - - -LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); -LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, - TMS event); -LUAI_FUNC void luaT_init (lua_State *L); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.c deleted file mode 100644 index 3a46609..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.c +++ /dev/null @@ -1,392 +0,0 @@ -/* -** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $ -** Lua stand-alone interpreter -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include - -#define lua_c - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - -static lua_State *globalL = NULL; - -static const char *progname = LUA_PROGNAME; - - - -static void lstop (lua_State *L, lua_Debug *ar) { - (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); - luaL_error(L, "interrupted!"); -} - - -static void laction (int i) { - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); -} - - -static void print_usage (void) { - fprintf(stderr, - "usage: %s [options] [script [args]].\n" - "Available options are:\n" - " -e stat execute string " LUA_QL("stat") "\n" - " -l name require library " LUA_QL("name") "\n" - " -i enter interactive mode after executing " LUA_QL("script") "\n" - " -v show version information\n" - " -- stop handling options\n" - " - execute stdin and stop handling options\n" - , - progname); - fflush(stderr); -} - - -static void l_message (const char *pname, const char *msg) { - if (pname) fprintf(stderr, "%s: ", pname); - fprintf(stderr, "%s\n", msg); - fflush(stderr); -} - - -static int report (lua_State *L, int status) { - if (status && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - } - return status; -} - - -static int traceback (lua_State *L) { - if (!lua_isstring(L, 1)) /* 'message' not a string? */ - return 1; /* keep it intact */ - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - return 1; - } - lua_getfield(L, -1, "traceback"); - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); - return 1; - } - lua_pushvalue(L, 1); /* pass error message */ - lua_pushinteger(L, 2); /* skip this function and traceback */ - lua_call(L, 2, 1); /* call debug.traceback */ - return 1; -} - - -static int docall (lua_State *L, int narg, int clear) { - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ - signal(SIGINT, laction); - status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); - signal(SIGINT, SIG_DFL); - lua_remove(L, base); /* remove traceback function */ - /* force a complete garbage collection in case of errors */ - if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); - return status; -} - - -static void print_version (void) { - l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT); -} - - -static int getargs (lua_State *L, char **argv, int n) { - int narg; - int i; - int argc = 0; - while (argv[argc]) argc++; /* count total number of arguments */ - narg = argc - (n + 1); /* number of arguments to the script */ - luaL_checkstack(L, narg + 3, "too many arguments to script"); - for (i=n+1; i < argc; i++) - lua_pushstring(L, argv[i]); - lua_createtable(L, narg, n + 1); - for (i=0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - n); - } - return narg; -} - - -static int dofile (lua_State *L, const char *name) { - int status = luaL_loadfile(L, name) || docall(L, 0, 1); - return report(L, status); -} - - -static int dostring (lua_State *L, const char *s, const char *name) { - int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); - return report(L, status); -} - - -static int dolibrary (lua_State *L, const char *name) { - lua_getglobal(L, "require"); - lua_pushstring(L, name); - return report(L, docall(L, 1, 1)); -} - - -static const char *get_prompt (lua_State *L, int firstline) { - const char *p; - lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); - lua_pop(L, 1); /* remove global */ - return p; -} - - -static int incomplete (lua_State *L, int status) { - if (status == LUA_ERRSYNTAX) { - size_t lmsg; - const char *msg = lua_tolstring(L, -1, &lmsg); - const char *tp = msg + lmsg - (sizeof(LUA_QL("")) - 1); - if (strstr(msg, LUA_QL("")) == tp) { - lua_pop(L, 1); - return 1; - } - } - return 0; /* else... */ -} - - -static int pushline (lua_State *L, int firstline) { - char buffer[LUA_MAXINPUT]; - char *b = buffer; - size_t l; - const char *prmt = get_prompt(L, firstline); - if (lua_readline(L, b, prmt) == 0) - return 0; /* no input */ - l = strlen(b); - if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[l-1] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* first line starts with `=' ? */ - lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ - else - lua_pushstring(L, b); - lua_freeline(L, b); - return 1; -} - - -static int loadline (lua_State *L) { - int status; - lua_settop(L, 0); - if (!pushline(L, 1)) - return -1; /* no input */ - for (;;) { /* repeat until gets a complete line */ - status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); - if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (!pushline(L, 0)) /* no more input? */ - return -1; - lua_pushliteral(L, "\n"); /* add a new line... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } - lua_saveline(L, 1); - lua_remove(L, 1); /* remove line */ - return status; -} - - -static void dotty (lua_State *L) { - int status; - const char *oldprogname = progname; - progname = NULL; - while ((status = loadline(L)) != -1) { - if (status == 0) status = docall(L, 0, 0); - report(L, status); - if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) - l_message(progname, lua_pushfstring(L, - "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); - } - } - lua_settop(L, 0); /* clear stack */ - fputs("\n", stdout); - fflush(stdout); - progname = oldprogname; -} - - -static int handle_script (lua_State *L, char **argv, int n) { - int status; - const char *fname; - int narg = getargs(L, argv, n); /* collect arguments */ - lua_setglobal(L, "arg"); - fname = argv[n]; - if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - lua_insert(L, -(narg+1)); - if (status == 0) - status = docall(L, narg, 0); - else - lua_pop(L, narg); - return report(L, status); -} - - -/* check that argument has no extra characters at the end */ -#define notail(x) {if ((x)[2] != '\0') return -1;} - - -static int collectargs (char **argv, int *pi, int *pv, int *pe) { - int i; - for (i = 1; argv[i] != NULL; i++) { - if (argv[i][0] != '-') /* not an option? */ - return i; - switch (argv[i][1]) { /* option */ - case '-': - notail(argv[i]); - return (argv[i+1] != NULL ? i+1 : 0); - case '\0': - return i; - case 'i': - notail(argv[i]); - *pi = 1; /* go through */ - case 'v': - notail(argv[i]); - *pv = 1; - break; - case 'e': - *pe = 1; /* go through */ - case 'l': - if (argv[i][2] == '\0') { - i++; - if (argv[i] == NULL) return -1; - } - break; - default: return -1; /* invalid option */ - } - } - return 0; -} - - -static int runargs (lua_State *L, char **argv, int n) { - int i; - for (i = 1; i < n; i++) { - if (argv[i] == NULL) continue; - lua_assert(argv[i][0] == '-'); - switch (argv[i][1]) { /* option */ - case 'e': { - const char *chunk = argv[i] + 2; - if (*chunk == '\0') chunk = argv[++i]; - lua_assert(chunk != NULL); - if (dostring(L, chunk, "=(command line)") != 0) - return 1; - break; - } - case 'l': { - const char *filename = argv[i] + 2; - if (*filename == '\0') filename = argv[++i]; - lua_assert(filename != NULL); - if (dolibrary(L, filename)) - return 1; /* stop if file fails */ - break; - } - default: break; - } - } - return 0; -} - - -static int handle_luainit (lua_State *L) { - const char *init = getenv(LUA_INIT); - if (init == NULL) return 0; /* status OK */ - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, "=" LUA_INIT); -} - - -struct Smain { - int argc; - char **argv; - int status; -}; - - -static int pmain (lua_State *L) { - struct Smain *s = (struct Smain *)lua_touserdata(L, 1); - char **argv = s->argv; - int script; - int has_i = 0, has_v = 0, has_e = 0; - globalL = L; - if (argv[0] && argv[0][0]) progname = argv[0]; - lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ - luaL_openlibs(L); /* open libraries */ - lua_gc(L, LUA_GCRESTART, 0); - s->status = handle_luainit(L); - if (s->status != 0) return 0; - script = collectargs(argv, &has_i, &has_v, &has_e); - if (script < 0) { /* invalid args? */ - print_usage(); - s->status = 1; - return 0; - } - if (has_v) print_version(); - s->status = runargs(L, argv, (script > 0) ? script : s->argc); - if (s->status != 0) return 0; - if (script) - s->status = handle_script(L, argv, script); - if (s->status != 0) return 0; - if (has_i) - dotty(L); - else if (script == 0 && !has_e && !has_v) { - if (lua_stdin_is_tty()) { - print_version(); - dotty(L); - } - else dofile(L, NULL); /* executes stdin as a file */ - } - return 0; -} - - -int main (int argc, char **argv) { - int status; - struct Smain s; - lua_State *L = lua_open(); /* create state */ - if (L == NULL) { - l_message(argv[0], "cannot create state: not enough memory"); - return EXIT_FAILURE; - } - s.argc = argc; - s.argv = argv; - status = lua_cpcall(L, &pmain, &s); - report(L, status); - lua_close(L); - return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.h deleted file mode 100644 index a4b73e7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lua.h +++ /dev/null @@ -1,388 +0,0 @@ -/* -** $Id: lua.h,v 1.218.1.7 2012/01/13 20:36:20 roberto Exp $ -** Lua - An Extensible Extension Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) -** See Copyright Notice at the end of this file -*/ - - -#ifndef lua_h -#define lua_h - -#include -#include - - -#include "luaconf.h" - - -#define LUA_VERSION "Lua 5.1" -#define LUA_RELEASE "Lua 5.1.5" -#define LUA_VERSION_NUM 501 -#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" - - -/* mark for precompiled code (`Lua') */ -#define LUA_SIGNATURE "\033Lua" - -/* option for multiple returns in `lua_pcall' and `lua_call' */ -#define LUA_MULTRET (-1) - - -/* -** pseudo-indices -*/ -#define LUA_REGISTRYINDEX (-10000) -#define LUA_ENVIRONINDEX (-10001) -#define LUA_GLOBALSINDEX (-10002) -#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) - - -/* thread status; 0 is OK */ -#define LUA_YIELD 1 -#define LUA_ERRRUN 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRERR 5 - - -typedef struct lua_State lua_State; - -typedef int (*lua_CFunction) (lua_State *L); - - -/* -** functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); - - -/* -** prototype for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - - - -/* minimum Lua stack available to a C function */ -#define LUA_MINSTACK 20 - - -/* -** generic extra include file -*/ -#if defined(LUA_USER_H) -#include LUA_USER_H -#endif - - -/* type of numbers in Lua */ -typedef LUA_NUMBER lua_Number; - - -/* type for integer functions */ -typedef LUA_INTEGER lua_Integer; - - - -/* -** state manipulation -*/ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); -LUA_API void (lua_close) (lua_State *L); -LUA_API lua_State *(lua_newthread) (lua_State *L); - -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); - - -/* -** basic stack manipulation -*/ -LUA_API int (lua_gettop) (lua_State *L); -LUA_API void (lua_settop) (lua_State *L, int idx); -LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_remove) (lua_State *L, int idx); -LUA_API void (lua_insert) (lua_State *L, int idx); -LUA_API void (lua_replace) (lua_State *L, int idx); -LUA_API int (lua_checkstack) (lua_State *L, int sz); - -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); - - -/* -** access functions (stack -> C) -*/ - -LUA_API int (lua_isnumber) (lua_State *L, int idx); -LUA_API int (lua_isstring) (lua_State *L, int idx); -LUA_API int (lua_iscfunction) (lua_State *L, int idx); -LUA_API int (lua_isuserdata) (lua_State *L, int idx); -LUA_API int (lua_type) (lua_State *L, int idx); -LUA_API const char *(lua_typename) (lua_State *L, int tp); - -LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); - -LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); -LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); -LUA_API int (lua_toboolean) (lua_State *L, int idx); -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API size_t (lua_objlen) (lua_State *L, int idx); -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); -LUA_API void *(lua_touserdata) (lua_State *L, int idx); -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); -LUA_API const void *(lua_topointer) (lua_State *L, int idx); - - -/* -** push functions (C -> stack) -*/ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); -LUA_API void (lua_pushstring) (lua_State *L, const char *s); -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, - va_list argp); -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); -LUA_API void (lua_pushboolean) (lua_State *L, int b); -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); -LUA_API int (lua_pushthread) (lua_State *L); - - -/* -** get functions (Lua -> stack) -*/ -LUA_API void (lua_gettable) (lua_State *L, int idx); -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawget) (lua_State *L, int idx); -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API void (lua_getfenv) (lua_State *L, int idx); - - -/* -** set functions (stack -> Lua) -*/ -LUA_API void (lua_settable) (lua_State *L, int idx); -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API int (lua_setfenv) (lua_State *L, int idx); - - -/* -** `load' and `call' functions (load and run Lua code) -*/ -LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); -LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); -LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname); - -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); - - -/* -** coroutine functions -*/ -LUA_API int (lua_yield) (lua_State *L, int nresults); -LUA_API int (lua_resume) (lua_State *L, int narg); -LUA_API int (lua_status) (lua_State *L); - -/* -** garbage-collection function and options -*/ - -#define LUA_GCSTOP 0 -#define LUA_GCRESTART 1 -#define LUA_GCCOLLECT 2 -#define LUA_GCCOUNT 3 -#define LUA_GCCOUNTB 4 -#define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 - -LUA_API int (lua_gc) (lua_State *L, int what, int data); - - -/* -** miscellaneous functions -*/ - -LUA_API int (lua_error) (lua_State *L); - -LUA_API int (lua_next) (lua_State *L, int idx); - -LUA_API void (lua_concat) (lua_State *L, int n); - -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); - - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_newtable(L) lua_createtable(L, 0, 0) - -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define lua_strlen(L,i) lua_objlen(L, (i)) - -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) - -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) - -#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) -#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) - -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) - - - -/* -** compatibility macros and functions -*/ - -#define lua_open() luaL_newstate() - -#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) - -#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) - -#define lua_Chunkreader lua_Reader -#define lua_Chunkwriter lua_Writer - - -/* hack */ -LUA_API void lua_setlevel (lua_State *from, lua_State *to); - - -/* -** {====================================================================== -** Debug API -** ======================================================================= -*/ - - -/* -** Event codes -*/ -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILRET 4 - - -/* -** Event masks -*/ -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debuger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); - -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); -LUA_API lua_Hook lua_gethook (lua_State *L); -LUA_API int lua_gethookmask (lua_State *L); -LUA_API int lua_gethookcount (lua_State *L); - - -struct lua_Debug { - int event; - const char *name; /* (n) */ - const char *namewhat; /* (n) `global', `local', `field', `method' */ - const char *what; /* (S) `Lua', `C', `main', `tail' */ - const char *source; /* (S) */ - int currentline; /* (l) */ - int nups; /* (u) number of upvalues */ - int linedefined; /* (S) */ - int lastlinedefined; /* (S) */ - char short_src[LUA_IDSIZE]; /* (S) */ - /* private part */ - int i_ci; /* active function */ -}; - -/* }====================================================================== */ - - -/****************************************************************************** -* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/luac.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/luac.c deleted file mode 100644 index d070173..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/luac.c +++ /dev/null @@ -1,200 +0,0 @@ -/* -** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $ -** Lua compiler (saves bytecodes to files; also list bytecodes) -** See Copyright Notice in lua.h -*/ - -#include -#include -#include -#include - -#define luac_c -#define LUA_CORE - -#include "lua.h" -#include "lauxlib.h" - -#include "ldo.h" -#include "lfunc.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstring.h" -#include "lundump.h" - -#define PROGNAME "luac" /* default program name */ -#define OUTPUT PROGNAME ".out" /* default output file */ - -static int listing=0; /* list bytecodes? */ -static int dumping=1; /* dump bytecodes? */ -static int stripping=0; /* strip debug information? */ -static char Output[]={ OUTPUT }; /* default output file name */ -static const char* output=Output; /* actual output file name */ -static const char* progname=PROGNAME; /* actual program name */ - -static void fatal(const char* message) -{ - fprintf(stderr,"%s: %s\n",progname,message); - exit(EXIT_FAILURE); -} - -static void cannot(const char* what) -{ - fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); - exit(EXIT_FAILURE); -} - -static void usage(const char* message) -{ - if (*message=='-') - fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); - else - fprintf(stderr,"%s: %s\n",progname,message); - fprintf(stderr, - "usage: %s [options] [filenames].\n" - "Available options are:\n" - " - process stdin\n" - " -l list\n" - " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" - " -p parse only\n" - " -s strip debug information\n" - " -v show version information\n" - " -- stop handling options\n", - progname,Output); - exit(EXIT_FAILURE); -} - -#define IS(s) (strcmp(argv[i],s)==0) - -static int doargs(int argc, char* argv[]) -{ - int i; - int version=0; - if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; - for (i=1; itop+(i))->l.p) - -static const Proto* combine(lua_State* L, int n) -{ - if (n==1) - return toproto(L,-1); - else - { - int i,pc; - Proto* f=luaF_newproto(L); - setptvalue2s(L,L->top,f); incr_top(L); - f->source=luaS_newliteral(L,"=(" PROGNAME ")"); - f->maxstacksize=1; - pc=2*n+1; - f->code=luaM_newvector(L,pc,Instruction); - f->sizecode=pc; - f->p=luaM_newvector(L,n,Proto*); - f->sizep=n; - pc=0; - for (i=0; ip[i]=toproto(L,i-n-1); - f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i); - f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1); - } - f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0); - return f; - } -} - -static int writer(lua_State* L, const void* p, size_t size, void* u) -{ - UNUSED(L); - return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); -} - -struct Smain { - int argc; - char** argv; -}; - -static int pmain(lua_State* L) -{ - struct Smain* s = (struct Smain*)lua_touserdata(L, 1); - int argc=s->argc; - char** argv=s->argv; - const Proto* f; - int i; - if (!lua_checkstack(L,argc)) fatal("too many input files"); - for (i=0; i1); - if (dumping) - { - FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); - if (D==NULL) cannot("open"); - lua_lock(L); - luaU_dump(L,f,writer,D,stripping); - lua_unlock(L); - if (ferror(D)) cannot("write"); - if (fclose(D)) cannot("close"); - } - return 0; -} - -int main(int argc, char* argv[]) -{ - lua_State* L; - struct Smain s; - int i=doargs(argc,argv); - argc-=i; argv+=i; - if (argc<=0) usage("no input files given"); - L=lua_open(); - if (L==NULL) fatal("not enough memory for state"); - s.argc=argc; - s.argv=argv; - if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1)); - lua_close(L); - return EXIT_SUCCESS; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/luaconf.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/luaconf.h deleted file mode 100644 index 07b9279..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/luaconf.h +++ /dev/null @@ -1,763 +0,0 @@ -/* -** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $ -** Configuration file for Lua -** See Copyright Notice in lua.h -*/ - - -#ifndef lconfig_h -#define lconfig_h - -#include -#include - - -/* -** ================================================================== -** Search for "@@" to find all configurable definitions. -** =================================================================== -*/ - - -/* -@@ LUA_ANSI controls the use of non-ansi features. -** CHANGE it (define it) if you want Lua to avoid the use of any -** non-ansi feature or library. -*/ -#if defined(__STRICT_ANSI__) -#define LUA_ANSI -#endif - - -#if !defined(LUA_ANSI) && defined(_WIN32) -#define LUA_WIN -#endif - -#if defined(LUA_USE_LINUX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ -#define LUA_USE_READLINE /* needs some extra libraries */ -#endif - -#if defined(LUA_USE_MACOSX) -#define LUA_USE_POSIX -#define LUA_DL_DYLD /* does not need extra library */ -#endif - - - -/* -@@ LUA_USE_POSIX includes all functionallity listed as X/Open System -@* Interfaces Extension (XSI). -** CHANGE it (define it) if your system is XSI compatible. -*/ -#if defined(LUA_USE_POSIX) -#define LUA_USE_MKSTEMP -#define LUA_USE_ISATTY -#define LUA_USE_POPEN -#define LUA_USE_ULONGJMP -#endif - - -/* -@@ LUA_PATH and LUA_CPATH are the names of the environment variables that -@* Lua check to set its paths. -@@ LUA_INIT is the name of the environment variable that Lua -@* checks for initialization code. -** CHANGE them if you want different names. -*/ -#define LUA_PATH "LUA_PATH" -#define LUA_CPATH "LUA_CPATH" -#define LUA_INIT "LUA_INIT" - - -/* -@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for -@* Lua libraries. -@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for -@* C libraries. -** CHANGE them if your machine has a non-conventional directory -** hierarchy or if you want to install your libraries in -** non-conventional directories. -*/ -#if defined(_WIN32) -/* -** In Windows, any exclamation mark ('!') in the path is replaced by the -** path of the directory of the executable file of the current process. -*/ -#define LUA_LDIR "!\\lua\\" -#define LUA_CDIR "!\\" -#define LUA_PATH_DEFAULT \ - ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua" -#define LUA_CPATH_DEFAULT \ - ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" - -#else -#define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/5.1/" -#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" -#define LUA_PATH_DEFAULT \ - "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua" -#define LUA_CPATH_DEFAULT \ - "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so" -#endif - - -/* -@@ LUA_DIRSEP is the directory separator (for submodules). -** CHANGE it if your machine does not use "/" as the directory separator -** and is not Windows. (On Windows Lua automatically uses "\".) -*/ -#if defined(_WIN32) -#define LUA_DIRSEP "\\" -#else -#define LUA_DIRSEP "/" -#endif - - -/* -@@ LUA_PATHSEP is the character that separates templates in a path. -@@ LUA_PATH_MARK is the string that marks the substitution points in a -@* template. -@@ LUA_EXECDIR in a Windows path is replaced by the executable's -@* directory. -@@ LUA_IGMARK is a mark to ignore all before it when bulding the -@* luaopen_ function name. -** CHANGE them if for some reason your system cannot use those -** characters. (E.g., if one of those characters is a common character -** in file/directory names.) Probably you do not need to change them. -*/ -#define LUA_PATHSEP ";" -#define LUA_PATH_MARK "?" -#define LUA_EXECDIR "!" -#define LUA_IGMARK "-" - - -/* -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most -** machines, ptrdiff_t gives a good choice between int or long.) -*/ -#define LUA_INTEGER ptrdiff_t - - -/* -@@ LUA_API is a mark for all core API functions. -@@ LUALIB_API is a mark for all standard library functions. -** CHANGE them if you need to define those functions in some special way. -** For instance, if you want to create one Windows DLL with the core and -** the libraries, you may want to use the following definition (define -** LUA_BUILD_AS_DLL to get it). -*/ -#if defined(LUA_BUILD_AS_DLL) - -#if defined(LUA_CORE) || defined(LUA_LIB) -#define LUA_API __declspec(dllexport) -#else -#define LUA_API __declspec(dllimport) -#endif - -#else - -#define LUA_API extern - -#endif - -/* more often than not the libs go together with the core */ -#define LUALIB_API LUA_API - - -/* -@@ LUAI_FUNC is a mark for all extern functions that are not to be -@* exported to outside modules. -@@ LUAI_DATA is a mark for all extern (const) variables that are not to -@* be exported to outside modules. -** CHANGE them if you need to mark them in some special way. Elf/gcc -** (versions 3.2 and later) mark them as "hidden" to optimize access -** when Lua is compiled as a shared library. -*/ -#if defined(luaall_c) -#define LUAI_FUNC static -#define LUAI_DATA /* empty */ - -#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) -#define LUAI_FUNC __attribute__((visibility("hidden"))) extern -#define LUAI_DATA LUAI_FUNC - -#else -#define LUAI_FUNC extern -#define LUAI_DATA extern -#endif - - - -/* -@@ LUA_QL describes how error messages quote program elements. -** CHANGE it if you want a different appearance. -*/ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - - -/* -@@ LUA_IDSIZE gives the maximum size for the description of the source -@* of a function in debug information. -** CHANGE it if you want a different size. -*/ -#define LUA_IDSIZE 60 - - -/* -** {================================================================== -** Stand-alone configuration -** =================================================================== -*/ - -#if defined(lua_c) || defined(luaall_c) - -/* -@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that -@* is, whether we're running lua interactively). -** CHANGE it if you have a better definition for non-POSIX/non-Windows -** systems. -*/ -#if defined(LUA_USE_ISATTY) -#include -#define lua_stdin_is_tty() isatty(0) -#elif defined(LUA_WIN) -#include -#include -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) -#else -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ -#endif - - -/* -@@ LUA_PROMPT is the default prompt used by stand-alone Lua. -@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. -** CHANGE them if you want different prompts. (You can also change the -** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) -*/ -#define LUA_PROMPT "> " -#define LUA_PROMPT2 ">> " - - -/* -@@ LUA_PROGNAME is the default name for the stand-alone Lua program. -** CHANGE it if your stand-alone interpreter has a different name and -** your system is not able to detect that name automatically. -*/ -#define LUA_PROGNAME "lua" - - -/* -@@ LUA_MAXINPUT is the maximum length for an input line in the -@* stand-alone interpreter. -** CHANGE it if you need longer lines. -*/ -#define LUA_MAXINPUT 512 - - -/* -@@ lua_readline defines how to show a prompt and then read a line from -@* the standard input. -@@ lua_saveline defines how to "save" a read line in a "history". -@@ lua_freeline defines how to free a line read by lua_readline. -** CHANGE them if you want to improve this functionality (e.g., by using -** GNU readline and history facilities). -*/ -#if defined(LUA_USE_READLINE) -#include -#include -#include -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,idx) \ - if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ - add_history(lua_tostring(L, idx)); /* add it to history */ -#define lua_freeline(L,b) ((void)L, free(b)) -#else -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,idx) { (void)L; (void)idx; } -#define lua_freeline(L,b) { (void)L; (void)b; } -#endif - -#endif - -/* }================================================================== */ - - -/* -@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles -@* as a percentage. -** CHANGE it if you want the GC to run faster or slower (higher values -** mean larger pauses which mean slower collection.) You can also change -** this value dynamically. -*/ -#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */ - - -/* -@@ LUAI_GCMUL defines the default speed of garbage collection relative to -@* memory allocation as a percentage. -** CHANGE it if you want to change the granularity of the garbage -** collection. (Higher values mean coarser collections. 0 represents -** infinity, where each step performs a full collection.) You can also -** change this value dynamically. -*/ -#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ - - - -/* -@@ LUA_COMPAT_GETN controls compatibility with old getn behavior. -** CHANGE it (define it) if you want exact compatibility with the -** behavior of setn/getn in Lua 5.0. -*/ -#undef LUA_COMPAT_GETN - -/* -@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib. -** CHANGE it to undefined as soon as you do not need a global 'loadlib' -** function (the function is still available as 'package.loadlib'). -*/ -#undef LUA_COMPAT_LOADLIB - -/* -@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature. -** CHANGE it to undefined as soon as your programs use only '...' to -** access vararg parameters (instead of the old 'arg' table). -*/ -#define LUA_COMPAT_VARARG - -/* -@@ LUA_COMPAT_MOD controls compatibility with old math.mod function. -** CHANGE it to undefined as soon as your programs use 'math.fmod' or -** the new '%' operator instead of 'math.mod'. -*/ -#define LUA_COMPAT_MOD - -/* -@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting -@* facility. -** CHANGE it to 2 if you want the old behaviour, or undefine it to turn -** off the advisory error when nesting [[...]]. -*/ -#define LUA_COMPAT_LSTR 1 - -/* -@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name. -** CHANGE it to undefined as soon as you rename 'string.gfind' to -** 'string.gmatch'. -*/ -#define LUA_COMPAT_GFIND - -/* -@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib' -@* behavior. -** CHANGE it to undefined as soon as you replace to 'luaL_register' -** your uses of 'luaL_openlib' -*/ -#define LUA_COMPAT_OPENLIB - - - -/* -@@ luai_apicheck is the assert macro used by the Lua-C API. -** CHANGE luai_apicheck if you want Lua to perform some checks in the -** parameters it gets from API calls. This may slow down the interpreter -** a bit, but may be quite useful when debugging C code that interfaces -** with Lua. A useful redefinition is to use assert.h. -*/ -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(L,o) { (void)L; assert(o); } -#else -#define luai_apicheck(L,o) { (void)L; } -#endif - - -/* -@@ LUAI_BITSINT defines the number of bits in an int. -** CHANGE here if Lua cannot automatically detect the number of bits of -** your machine. Probably you do not need to change this. -*/ -/* avoid overflows in comparison */ -#if INT_MAX-20 < 32760 -#define LUAI_BITSINT 16 -#elif INT_MAX > 2147483640L -/* int has at least 32 bits */ -#define LUAI_BITSINT 32 -#else -#error "you must define LUA_BITSINT with number of bits in an integer" -#endif - - -/* -@@ LUAI_UINT32 is an unsigned integer with at least 32 bits. -@@ LUAI_INT32 is an signed integer with at least 32 bits. -@@ LUAI_UMEM is an unsigned integer big enough to count the total -@* memory used by Lua. -@@ LUAI_MEM is a signed integer big enough to count the total memory -@* used by Lua. -** CHANGE here if for some weird reason the default definitions are not -** good enough for your machine. (The definitions in the 'else' -** part always works, but may waste space on machines with 64-bit -** longs.) Probably you do not need to change this. -*/ -#if LUAI_BITSINT >= 32 -#define LUAI_UINT32 unsigned int -#define LUAI_INT32 int -#define LUAI_MAXINT32 INT_MAX -#define LUAI_UMEM size_t -#define LUAI_MEM ptrdiff_t -#else -/* 16-bit ints */ -#define LUAI_UINT32 unsigned long -#define LUAI_INT32 long -#define LUAI_MAXINT32 LONG_MAX -#define LUAI_UMEM unsigned long -#define LUAI_MEM long -#endif - - -/* -@@ LUAI_MAXCALLS limits the number of nested calls. -** CHANGE it if you need really deep recursive calls. This limit is -** arbitrary; its only purpose is to stop infinite recursion before -** exhausting memory. -*/ -#define LUAI_MAXCALLS 20000 - - -/* -@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function -@* can use. -** CHANGE it if you need lots of (Lua) stack space for your C -** functions. This limit is arbitrary; its only purpose is to stop C -** functions to consume unlimited stack space. (must be smaller than -** -LUA_REGISTRYINDEX) -*/ -#define LUAI_MAXCSTACK 8000 - - - -/* -** {================================================================== -** CHANGE (to smaller values) the following definitions if your system -** has a small C stack. (Or you may want to change them to larger -** values if your system has a large C stack and these limits are -** too rigid for you.) Some of these constants control the size of -** stack-allocated arrays used by the compiler or the interpreter, while -** others limit the maximum number of recursive calls that the compiler -** or the interpreter can perform. Values too large may cause a C stack -** overflow for some forms of deep constructs. -** =================================================================== -*/ - - -/* -@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and -@* syntactical nested non-terminals in a program. -*/ -#define LUAI_MAXCCALLS 200 - - -/* -@@ LUAI_MAXVARS is the maximum number of local variables per function -@* (must be smaller than 250). -*/ -#define LUAI_MAXVARS 200 - - -/* -@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function -@* (must be smaller than 250). -*/ -#define LUAI_MAXUPVALUES 60 - - -/* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -*/ -#define LUAL_BUFFERSIZE BUFSIZ - -/* }================================================================== */ - - - - -/* -** {================================================================== -@@ LUA_NUMBER is the type of numbers in Lua. -** CHANGE the following definitions only if you want to build Lua -** with a number type different from double. You may also need to -** change lua_number2int & lua_number2integer. -** =================================================================== -*/ - -#define LUA_NUMBER_DOUBLE -#define LUA_NUMBER double - -/* -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' -@* over a number. -*/ -#define LUAI_UACNUMBER double - - -/* -@@ LUA_NUMBER_SCAN is the format for reading numbers. -@@ LUA_NUMBER_FMT is the format for writing numbers. -@@ lua_number2str converts a number to a string. -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. -@@ lua_str2number converts a string to a number. -*/ -#define LUA_NUMBER_SCAN "%lf" -#define LUA_NUMBER_FMT "%.14g" -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ -#define lua_str2number(s,p) strtod((s), (p)) - - -/* -@@ The luai_num* macros define the primitive operations over numbers. -*/ -#if defined(LUA_CORE) -#include -#define luai_numadd(a,b) ((a)+(b)) -#define luai_numsub(a,b) ((a)-(b)) -#define luai_nummul(a,b) ((a)*(b)) -#define luai_numdiv(a,b) ((a)/(b)) -#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) -#define luai_numpow(a,b) (pow(a,b)) -#define luai_numunm(a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(a,b) ((a)<(b)) -#define luai_numle(a,b) ((a)<=(b)) -#define luai_numisnan(a) (!luai_numeq((a), (a))) -#endif - - -/* -@@ lua_number2int is a macro to convert lua_Number to int. -@@ lua_number2integer is a macro to convert lua_Number to lua_Integer. -** CHANGE them if you know a faster way to convert a lua_Number to -** int (with any rounding method and without throwing errors) in your -** system. In Pentium machines, a naive typecast from double to int -** in C is extremely slow, so any alternative is worth trying. -*/ - -/* On a Pentium, resort to a trick */ -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ - (defined(__i386) || defined (_M_IX86) || defined(__i386__)) - -/* On a Microsoft compiler, use assembler */ -#if defined(_MSC_VER) - -#define lua_number2int(i,d) __asm fld d __asm fistp i -#define lua_number2integer(i,n) lua_number2int(i, n) - -/* the next trick should work on any Pentium, but sometimes clashes - with a DirectX idiosyncrasy */ -#else - -union luai_Cast { double l_d; long l_l; }; -#define lua_number2int(i,d) \ - { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } -#define lua_number2integer(i,n) lua_number2int(i, n) - -#endif - - -/* this option always works, but may be slow */ -#else -#define lua_number2int(i,d) ((i)=(int)(d)) -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) - -#endif - -/* }================================================================== */ - - -/* -@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment. -** CHANGE it if your system requires alignments larger than double. (For -** instance, if your system supports long doubles and they must be -** aligned in 16-byte boundaries, then you should add long double in the -** union.) Probably you do not need to change this. -*/ -#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } - - -/* -@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling. -** CHANGE them if you prefer to use longjmp/setjmp even with C++ -** or if want/don't to use _longjmp/_setjmp instead of regular -** longjmp/setjmp. By default, Lua handles errors with exceptions when -** compiling as C++ code, with _longjmp/_setjmp when asked to use them, -** and with longjmp/setjmp otherwise. -*/ -#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) -/* C++ exceptions */ -#define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,a) try { a } catch(...) \ - { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy variable */ - -#elif defined(LUA_USE_ULONGJMP) -/* in Unix, try _longjmp/_setjmp (more efficient) */ -#define LUAI_THROW(L,c) _longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#else -/* default handling with long jumps */ -#define LUAI_THROW(L,c) longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#endif - - -/* -@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern -@* can do during pattern-matching. -** CHANGE it if you need more captures. This limit is arbitrary. -*/ -#define LUA_MAXCAPTURES 32 - - -/* -@@ lua_tmpnam is the function that the OS library uses to create a -@* temporary name. -@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam. -** CHANGE them if you have an alternative to tmpnam (which is considered -** insecure) or if you want the original tmpnam anyway. By default, Lua -** uses tmpnam except when POSIX is available, where it uses mkstemp. -*/ -#if defined(loslib_c) || defined(luaall_c) - -#if defined(LUA_USE_MKSTEMP) -#include -#define LUA_TMPNAMBUFSIZE 32 -#define lua_tmpnam(b,e) { \ - strcpy(b, "/tmp/lua_XXXXXX"); \ - e = mkstemp(b); \ - if (e != -1) close(e); \ - e = (e == -1); } - -#else -#define LUA_TMPNAMBUFSIZE L_tmpnam -#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } -#endif - -#endif - - -/* -@@ lua_popen spawns a new process connected to the current one through -@* the file streams. -** CHANGE it if you have a way to implement it in your system. -*/ -#if defined(LUA_USE_POPEN) - -#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) -#define lua_pclose(L,file) ((void)L, (pclose(file) != -1)) - -#elif defined(LUA_WIN) - -#define lua_popen(L,c,m) ((void)L, _popen(c,m)) -#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1)) - -#else - -#define lua_popen(L,c,m) ((void)((void)c, m), \ - luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) -#define lua_pclose(L,file) ((void)((void)L, file), 0) - -#endif - -/* -@@ LUA_DL_* define which dynamic-library system Lua should use. -** CHANGE here if Lua has problems choosing the appropriate -** dynamic-library system for your platform (either Windows' DLL, Mac's -** dyld, or Unix's dlopen). If your system is some kind of Unix, there -** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for -** it. To use dlopen you also need to adapt the src/Makefile (probably -** adding -ldl to the linker options), so Lua does not select it -** automatically. (When you change the makefile to add -ldl, you must -** also add -DLUA_USE_DLOPEN.) -** If you do not want any kind of dynamic library, undefine all these -** options. -** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD. -*/ -#if defined(LUA_USE_DLOPEN) -#define LUA_DL_DLOPEN -#endif - -#if defined(LUA_WIN) -#define LUA_DL_DLL -#endif - - -/* -@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State -@* (the data goes just *before* the lua_State pointer). -** CHANGE (define) this if you really need that. This value must be -** a multiple of the maximum alignment required for your machine. -*/ -#define LUAI_EXTRASPACE 0 - - -/* -@@ luai_userstate* allow user-specific actions on threads. -** CHANGE them if you defined LUAI_EXTRASPACE and need to do something -** extra when a thread is created/deleted/resumed/yielded. -*/ -#define luai_userstateopen(L) ((void)L) -#define luai_userstateclose(L) ((void)L) -#define luai_userstatethread(L,L1) ((void)L) -#define luai_userstatefree(L) ((void)L) -#define luai_userstateresume(L,n) ((void)L) -#define luai_userstateyield(L,n) ((void)L) - - -/* -@@ LUA_INTFRMLEN is the length modifier for integer conversions -@* in 'string.format'. -@@ LUA_INTFRM_T is the integer type correspoding to the previous length -@* modifier. -** CHANGE them if your system supports long long or does not support long. -*/ - -#if defined(LUA_USELONGLONG) - -#define LUA_INTFRMLEN "ll" -#define LUA_INTFRM_T long long - -#else - -#define LUA_INTFRMLEN "l" -#define LUA_INTFRM_T long - -#endif - - - -/* =================================================================== */ - -/* -** Local configuration. You can use this space to add your redefinitions -** without modifying the main part of the file. -*/ - - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lualib.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lualib.h deleted file mode 100644 index 469417f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lualib.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ -** Lua standard libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lualib_h -#define lualib_h - -#include "lua.h" - - -/* Key to file-handle type */ -#define LUA_FILEHANDLE "FILE*" - - -#define LUA_COLIBNAME "coroutine" -LUALIB_API int (luaopen_base) (lua_State *L); - -#define LUA_TABLIBNAME "table" -LUALIB_API int (luaopen_table) (lua_State *L); - -#define LUA_IOLIBNAME "io" -LUALIB_API int (luaopen_io) (lua_State *L); - -#define LUA_OSLIBNAME "os" -LUALIB_API int (luaopen_os) (lua_State *L); - -#define LUA_STRLIBNAME "string" -LUALIB_API int (luaopen_string) (lua_State *L); - -#define LUA_MATHLIBNAME "math" -LUALIB_API int (luaopen_math) (lua_State *L); - -#define LUA_DBLIBNAME "debug" -LUALIB_API int (luaopen_debug) (lua_State *L); - -#define LUA_LOADLIBNAME "package" -LUALIB_API int (luaopen_package) (lua_State *L); - - -/* open all previous libraries */ -LUALIB_API void (luaL_openlibs) (lua_State *L); - - - -#ifndef lua_assert -#define lua_assert(x) ((void)0) -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.c deleted file mode 100644 index 8010a45..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.c +++ /dev/null @@ -1,227 +0,0 @@ -/* -** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#include - -#define lundump_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstring.h" -#include "lundump.h" -#include "lzio.h" - -typedef struct { - lua_State* L; - ZIO* Z; - Mbuffer* b; - const char* name; -} LoadState; - -#ifdef LUAC_TRUST_BINARIES -#define IF(c,s) -#define error(S,s) -#else -#define IF(c,s) if (c) error(S,s) - -static void error(LoadState* S, const char* why) -{ - luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); - luaD_throw(S->L,LUA_ERRSYNTAX); -} -#endif - -#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) -#define LoadByte(S) (lu_byte)LoadChar(S) -#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) -#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) - -static void LoadBlock(LoadState* S, void* b, size_t size) -{ - size_t r=luaZ_read(S->Z,b,size); - IF (r!=0, "unexpected end"); -} - -static int LoadChar(LoadState* S) -{ - char x; - LoadVar(S,x); - return x; -} - -static int LoadInt(LoadState* S) -{ - int x; - LoadVar(S,x); - IF (x<0, "bad integer"); - return x; -} - -static lua_Number LoadNumber(LoadState* S) -{ - lua_Number x; - LoadVar(S,x); - return x; -} - -static TString* LoadString(LoadState* S) -{ - size_t size; - LoadVar(S,size); - if (size==0) - return NULL; - else - { - char* s=luaZ_openspace(S->L,S->b,size); - LoadBlock(S,s,size); - return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ - } -} - -static void LoadCode(LoadState* S, Proto* f) -{ - int n=LoadInt(S); - f->code=luaM_newvector(S->L,n,Instruction); - f->sizecode=n; - LoadVector(S,f->code,n,sizeof(Instruction)); -} - -static Proto* LoadFunction(LoadState* S, TString* p); - -static void LoadConstants(LoadState* S, Proto* f) -{ - int i,n; - n=LoadInt(S); - f->k=luaM_newvector(S->L,n,TValue); - f->sizek=n; - for (i=0; ik[i]); - for (i=0; ik[i]; - int t=LoadChar(S); - switch (t) - { - case LUA_TNIL: - setnilvalue(o); - break; - case LUA_TBOOLEAN: - setbvalue(o,LoadChar(S)!=0); - break; - case LUA_TNUMBER: - setnvalue(o,LoadNumber(S)); - break; - case LUA_TSTRING: - setsvalue2n(S->L,o,LoadString(S)); - break; - default: - error(S,"bad constant"); - break; - } - } - n=LoadInt(S); - f->p=luaM_newvector(S->L,n,Proto*); - f->sizep=n; - for (i=0; ip[i]=NULL; - for (i=0; ip[i]=LoadFunction(S,f->source); -} - -static void LoadDebug(LoadState* S, Proto* f) -{ - int i,n; - n=LoadInt(S); - f->lineinfo=luaM_newvector(S->L,n,int); - f->sizelineinfo=n; - LoadVector(S,f->lineinfo,n,sizeof(int)); - n=LoadInt(S); - f->locvars=luaM_newvector(S->L,n,LocVar); - f->sizelocvars=n; - for (i=0; ilocvars[i].varname=NULL; - for (i=0; ilocvars[i].varname=LoadString(S); - f->locvars[i].startpc=LoadInt(S); - f->locvars[i].endpc=LoadInt(S); - } - n=LoadInt(S); - f->upvalues=luaM_newvector(S->L,n,TString*); - f->sizeupvalues=n; - for (i=0; iupvalues[i]=NULL; - for (i=0; iupvalues[i]=LoadString(S); -} - -static Proto* LoadFunction(LoadState* S, TString* p) -{ - Proto* f; - if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); - f=luaF_newproto(S->L); - setptvalue2s(S->L,S->L->top,f); incr_top(S->L); - f->source=LoadString(S); if (f->source==NULL) f->source=p; - f->linedefined=LoadInt(S); - f->lastlinedefined=LoadInt(S); - f->nups=LoadByte(S); - f->numparams=LoadByte(S); - f->is_vararg=LoadByte(S); - f->maxstacksize=LoadByte(S); - LoadCode(S,f); - LoadConstants(S,f); - LoadDebug(S,f); - IF (!luaG_checkcode(f), "bad code"); - S->L->top--; - S->L->nCcalls--; - return f; -} - -static void LoadHeader(LoadState* S) -{ - char h[LUAC_HEADERSIZE]; - char s[LUAC_HEADERSIZE]; - luaU_header(h); - LoadBlock(S,s,LUAC_HEADERSIZE); - IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); -} - -/* -** load precompiled chunk -*/ -Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) -{ - LoadState S; - if (*name=='@' || *name=='=') - S.name=name+1; - else if (*name==LUA_SIGNATURE[0]) - S.name="binary string"; - else - S.name=name; - S.L=L; - S.Z=Z; - S.b=buff; - LoadHeader(&S); - return LoadFunction(&S,luaS_newliteral(L,"=?")); -} - -/* -* make header -*/ -void luaU_header (char* h) -{ - int x=1; - memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); - h+=sizeof(LUA_SIGNATURE)-1; - *h++=(char)LUAC_VERSION; - *h++=(char)LUAC_FORMAT; - *h++=(char)*(char*)&x; /* endianness */ - *h++=(char)sizeof(int); - *h++=(char)sizeof(size_t); - *h++=(char)sizeof(Instruction); - *h++=(char)sizeof(lua_Number); - *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.h deleted file mode 100644 index c80189d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lundump.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#ifndef lundump_h -#define lundump_h - -#include "lobject.h" -#include "lzio.h" - -/* load one chunk; from lundump.c */ -LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); - -/* make header; from lundump.c */ -LUAI_FUNC void luaU_header (char* h); - -/* dump one chunk; from ldump.c */ -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); - -#ifdef luac_c -/* print one chunk; from print.c */ -LUAI_FUNC void luaU_print (const Proto* f, int full); -#endif - -/* for header of binary files -- this is Lua 5.1 */ -#define LUAC_VERSION 0x51 - -/* for header of binary files -- this is the official format */ -#define LUAC_FORMAT 0 - -/* size of header of binary files */ -#define LUAC_HEADERSIZE 12 - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.c deleted file mode 100644 index a6f07b2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.c +++ /dev/null @@ -1,767 +0,0 @@ -/* -** $Id: lvm.c,v 2.63.1.5 2011/08/17 20:43:11 roberto Exp $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define lvm_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - - -/* limit for table tag-method chains (to avoid loops) */ -#define MAXTAGLOOP 100 - - -const TValue *luaV_tonumber (const TValue *obj, TValue *n) { - lua_Number num; - if (ttisnumber(obj)) return obj; - if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { - setnvalue(n, num); - return n; - } - else - return NULL; -} - - -int luaV_tostring (lua_State *L, StkId obj) { - if (!ttisnumber(obj)) - return 0; - else { - char s[LUAI_MAXNUMBER2STR]; - lua_Number n = nvalue(obj); - lua_number2str(s, n); - setsvalue2s(L, obj, luaS_new(L, s)); - return 1; - } -} - - -static void traceexec (lua_State *L, const Instruction *pc) { - lu_byte mask = L->hookmask; - const Instruction *oldpc = L->savedpc; - L->savedpc = pc; - if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { - resethookcount(L); - luaD_callhook(L, LUA_HOOKCOUNT, -1); - } - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(L->ci)->l.p; - int npc = pcRel(pc, p); - int newline = getline_(p, npc); - /* call linehook when enter a new function, when jump back (loop), - or when enter a new line */ - if (npc == 0 || pc <= oldpc || newline != getline_(p, pcRel(oldpc, p))) - luaD_callhook(L, LUA_HOOKLINE, newline); - } -} - - -static void callTMres (lua_State *L, StkId res, const TValue *f, - const TValue *p1, const TValue *p2) { - ptrdiff_t result = savestack(L, res); - setobj2s(L, L->top, f); /* push function */ - setobj2s(L, L->top+1, p1); /* 1st argument */ - setobj2s(L, L->top+2, p2); /* 2nd argument */ - luaD_checkstack(L, 3); - L->top += 3; - luaD_call(L, L->top - 3, 1); - res = restorestack(L, result); - L->top--; - setobjs2s(L, res, L->top); -} - - - -static void callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, const TValue *p3) { - setobj2s(L, L->top, f); /* push function */ - setobj2s(L, L->top+1, p1); /* 1st argument */ - setobj2s(L, L->top+2, p2); /* 2nd argument */ - setobj2s(L, L->top+3, p3); /* 3th argument */ - luaD_checkstack(L, 4); - L->top += 4; - luaD_call(L, L->top - 4, 0); -} - - -void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - const TValue *res = luaH_get(h, key); /* do a primitive get */ - if (!ttisnil(res) || /* result is no nil? */ - (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ - setobj2s(L, val, res); - return; - } - /* else will try the tag method */ - } - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) - luaG_typeerror(L, t, "index"); - if (ttisfunction(tm)) { - callTMres(L, val, tm, t, key); - return; - } - t = tm; /* else repeat with `tm' */ - } - luaG_runerror(L, "loop in gettable"); -} - - -void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; - TValue temp; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ - if (!ttisnil(oldval) || /* result is no nil? */ - (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ - setobj2t(L, oldval, val); - h->flags = 0; - luaC_barriert(L, h, val); - return; - } - /* else will try the tag method */ - } - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) - luaG_typeerror(L, t, "index"); - if (ttisfunction(tm)) { - callTM(L, tm, t, key, val); - return; - } - /* else repeat with `tm' */ - setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ - t = &temp; - } - luaG_runerror(L, "loop in settable"); -} - - -static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ - if (ttisnil(tm)) - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (ttisnil(tm)) return 0; - callTMres(L, res, tm, p1, p2); - return 1; -} - - -static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2, - TMS event) { - const TValue *tm1 = fasttm(L, mt1, event); - const TValue *tm2; - if (tm1 == NULL) return NULL; /* no metamethod */ - if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ - tm2 = fasttm(L, mt2, event); - if (tm2 == NULL) return NULL; /* no metamethod */ - if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */ - return tm1; - return NULL; -} - - -static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, - TMS event) { - const TValue *tm1 = luaT_gettmbyobj(L, p1, event); - const TValue *tm2; - if (ttisnil(tm1)) return -1; /* no metamethod? */ - tm2 = luaT_gettmbyobj(L, p2, event); - if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ - return -1; - callTMres(L, L->top, tm1, p1, p2); - return !l_isfalse(L->top); -} - - -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = ls->tsv.len; - const char *r = getstr(rs); - size_t lr = rs->tsv.len; - for (;;) { - int temp = strcoll(l, r); - if (temp != 0) return temp; - else { /* strings are equal up to a `\0' */ - size_t len = strlen(l); /* index of first `\0' in both strings */ - if (len == lr) /* r is finished? */ - return (len == ll) ? 0 : 1; - else if (len == ll) /* l is finished? */ - return -1; /* l is smaller than r (because r is not finished) */ - /* both strings longer than `len'; go on comparing (after the `\0') */ - len++; - l += len; ll -= len; r += len; lr -= len; - } - } -} - - -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttype(l) != ttype(r)) - return luaG_ordererror(L, l, r); - else if (ttisnumber(l)) - return luai_numlt(nvalue(l), nvalue(r)); - else if (ttisstring(l)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; - else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) - return res; - return luaG_ordererror(L, l, r); -} - - -static int lessequal (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttype(l) != ttype(r)) - return luaG_ordererror(L, l, r); - else if (ttisnumber(l)) - return luai_numle(nvalue(l), nvalue(r)); - else if (ttisstring(l)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; - else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ - return res; - else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ - return !res; - return luaG_ordererror(L, l, r); -} - - -int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { - const TValue *tm; - lua_assert(ttype(t1) == ttype(t2)); - switch (ttype(t1)) { - case LUA_TNIL: return 1; - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); - case LUA_TUSERDATA: { - if (uvalue(t1) == uvalue(t2)) return 1; - tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, - TM_EQ); - break; /* will try TM */ - } - case LUA_TTABLE: { - if (hvalue(t1) == hvalue(t2)) return 1; - tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - default: return gcvalue(t1) == gcvalue(t2); - } - if (tm == NULL) return 0; /* no TM? */ - callTMres(L, L->top, tm, t1, t2); /* call TM */ - return !l_isfalse(L->top); -} - - -void luaV_concat (lua_State *L, int total, int last) { - do { - StkId top = L->base + last + 1; - int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) - luaG_concaterror(L, top-2, top-1); - } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ - (void)tostring(L, top - 2); /* result is first op (as string) */ - else { - /* at least two string values; get as many as possible */ - size_t tl = tsvalue(top-1)->len; - char *buffer; - int i; - /* collect total length */ - for (n = 1; n < total && tostring(L, top-n-1); n++) { - size_t l = tsvalue(top-n-1)->len; - if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); - tl += l; - } - buffer = luaZ_openspace(L, &G(L)->buff, tl); - tl = 0; - for (i=n; i>0; i--) { /* concat all strings */ - size_t l = tsvalue(top-i)->len; - memcpy(buffer+tl, svalue(top-i), l); - tl += l; - } - setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); - } - total -= n-1; /* got `n' strings to create 1 new */ - last -= n-1; - } while (total > 1); /* repeat until only 1 result left */ -} - - -static void Arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op) { - TValue tempb, tempc; - const TValue *b, *c; - if ((b = luaV_tonumber(rb, &tempb)) != NULL && - (c = luaV_tonumber(rc, &tempc)) != NULL) { - lua_Number nb = nvalue(b), nc = nvalue(c); - switch (op) { - case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; - case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; - case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; - case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; - case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; - case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; - case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; - default: lua_assert(0); break; - } - } - else if (!call_binTM(L, rb, rc, ra, op)) - luaG_aritherror(L, rb, rc); -} - - - -/* -** some macros for common tasks in `luaV_execute' -*/ - -#define runtime_check(L, c) { if (!(c)) break; } - -#define RA(i) (base+GETARG_A(i)) -/* to be used after possible stack reallocation */ -#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) -#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) -#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) -#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) -#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) - - -#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} - - -#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } - - -#define arith_op(op,tm) { \ - TValue *rb = RKB(i); \ - TValue *rc = RKC(i); \ - if (ttisnumber(rb) && ttisnumber(rc)) { \ - lua_Number nb = nvalue(rb), nc = nvalue(rc); \ - setnvalue(ra, op(nb, nc)); \ - } \ - else \ - Protect(Arith(L, ra, rb, rc, tm)); \ - } - - - -void luaV_execute (lua_State *L, int nexeccalls) { - LClosure *cl; - StkId base; - TValue *k; - const Instruction *pc; - reentry: /* entry point */ - lua_assert(isLua(L->ci)); - pc = L->savedpc; - cl = &clvalue(L->ci->func)->l; - base = L->base; - k = cl->p->k; - /* main loop of interpreter */ - for (;;) { - const Instruction i = *pc++; - StkId ra; - if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && - (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { - traceexec(L, pc); - if (L->status == LUA_YIELD) { /* did hook yield? */ - L->savedpc = pc - 1; - return; - } - base = L->base; - } - /* warning!! several calls may realloc the stack and invalidate `ra' */ - ra = RA(i); - lua_assert(base == L->base && L->base == L->ci->base); - lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); - lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); - switch (GET_OPCODE(i)) { - case OP_MOVE: { - setobjs2s(L, ra, RB(i)); - continue; - } - case OP_LOADK: { - setobj2s(L, ra, KBx(i)); - continue; - } - case OP_LOADBOOL: { - setbvalue(ra, GETARG_B(i)); - if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ - continue; - } - case OP_LOADNIL: { - TValue *rb = RB(i); - do { - setnilvalue(rb--); - } while (rb >= ra); - continue; - } - case OP_GETUPVAL: { - int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); - continue; - } - case OP_GETGLOBAL: { - TValue g; - TValue *rb = KBx(i); - sethvalue(L, &g, cl->env); - lua_assert(ttisstring(rb)); - Protect(luaV_gettable(L, &g, rb, ra)); - continue; - } - case OP_GETTABLE: { - Protect(luaV_gettable(L, RB(i), RKC(i), ra)); - continue; - } - case OP_SETGLOBAL: { - TValue g; - sethvalue(L, &g, cl->env); - lua_assert(ttisstring(KBx(i))); - Protect(luaV_settable(L, &g, KBx(i), ra)); - continue; - } - case OP_SETUPVAL: { - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, ra); - luaC_barrier(L, uv, ra); - continue; - } - case OP_SETTABLE: { - Protect(luaV_settable(L, ra, RKB(i), RKC(i))); - continue; - } - case OP_NEWTABLE: { - int b = GETARG_B(i); - int c = GETARG_C(i); - sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); - Protect(luaC_checkGC(L)); - continue; - } - case OP_SELF: { - StkId rb = RB(i); - setobjs2s(L, ra+1, rb); - Protect(luaV_gettable(L, rb, RKC(i), ra)); - continue; - } - case OP_ADD: { - arith_op(luai_numadd, TM_ADD); - continue; - } - case OP_SUB: { - arith_op(luai_numsub, TM_SUB); - continue; - } - case OP_MUL: { - arith_op(luai_nummul, TM_MUL); - continue; - } - case OP_DIV: { - arith_op(luai_numdiv, TM_DIV); - continue; - } - case OP_MOD: { - arith_op(luai_nummod, TM_MOD); - continue; - } - case OP_POW: { - arith_op(luai_numpow, TM_POW); - continue; - } - case OP_UNM: { - TValue *rb = RB(i); - if (ttisnumber(rb)) { - lua_Number nb = nvalue(rb); - setnvalue(ra, luai_numunm(nb)); - } - else { - Protect(Arith(L, ra, rb, rb, TM_UNM)); - } - continue; - } - case OP_NOT: { - int res = l_isfalse(RB(i)); /* next assignment may change this value */ - setbvalue(ra, res); - continue; - } - case OP_LEN: { - const TValue *rb = RB(i); - switch (ttype(rb)) { - case LUA_TTABLE: { - setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); - break; - } - case LUA_TSTRING: { - setnvalue(ra, cast_num(tsvalue(rb)->len)); - break; - } - default: { /* try metamethod */ - Protect( - if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) - luaG_typeerror(L, rb, "get length of"); - ) - } - } - continue; - } - case OP_CONCAT: { - int b = GETARG_B(i); - int c = GETARG_C(i); - Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); - setobjs2s(L, RA(i), base+b); - continue; - } - case OP_JMP: { - dojump(L, pc, GETARG_sBx(i)); - continue; - } - case OP_EQ: { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - Protect( - if (equalobj(L, rb, rc) == GETARG_A(i)) - dojump(L, pc, GETARG_sBx(*pc)); - ) - pc++; - continue; - } - case OP_LT: { - Protect( - if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) - dojump(L, pc, GETARG_sBx(*pc)); - ) - pc++; - continue; - } - case OP_LE: { - Protect( - if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) - dojump(L, pc, GETARG_sBx(*pc)); - ) - pc++; - continue; - } - case OP_TEST: { - if (l_isfalse(ra) != GETARG_C(i)) - dojump(L, pc, GETARG_sBx(*pc)); - pc++; - continue; - } - case OP_TESTSET: { - TValue *rb = RB(i); - if (l_isfalse(rb) != GETARG_C(i)) { - setobjs2s(L, ra, rb); - dojump(L, pc, GETARG_sBx(*pc)); - } - pc++; - continue; - } - case OP_CALL: { - int b = GETARG_B(i); - int nresults = GETARG_C(i) - 1; - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - L->savedpc = pc; - switch (luaD_precall(L, ra, nresults)) { - case PCRLUA: { - nexeccalls++; - goto reentry; /* restart luaV_execute over new Lua function */ - } - case PCRC: { - /* it was a C function (`precall' called it); adjust results */ - if (nresults >= 0) L->top = L->ci->top; - base = L->base; - continue; - } - default: { - return; /* yield */ - } - } - } - case OP_TAILCALL: { - int b = GETARG_B(i); - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - L->savedpc = pc; - lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - switch (luaD_precall(L, ra, LUA_MULTRET)) { - case PCRLUA: { - /* tail call: put new frame in place of previous one */ - CallInfo *ci = L->ci - 1; /* previous frame */ - int aux; - StkId func = ci->func; - StkId pfunc = (ci+1)->func; /* previous function index */ - if (L->openupval) luaF_close(L, ci->base); - L->base = ci->base = ci->func + ((ci+1)->base - pfunc); - for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ - setobjs2s(L, func+aux, pfunc+aux); - ci->top = L->top = func+aux; /* correct top */ - lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); - ci->savedpc = L->savedpc; - ci->tailcalls++; /* one more call lost */ - L->ci--; /* remove new frame */ - goto reentry; - } - case PCRC: { /* it was a C function (`precall' called it) */ - base = L->base; - continue; - } - default: { - return; /* yield */ - } - } - } - case OP_RETURN: { - int b = GETARG_B(i); - if (b != 0) L->top = ra+b-1; - if (L->openupval) luaF_close(L, base); - L->savedpc = pc; - b = luaD_poscall(L, ra); - if (--nexeccalls == 0) /* was previous function running `here'? */ - return; /* no: return */ - else { /* yes: continue its execution */ - if (b) L->top = L->ci->top; - lua_assert(isLua(L->ci)); - lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); - goto reentry; - } - } - case OP_FORLOOP: { - lua_Number step = nvalue(ra+2); - lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ - lua_Number limit = nvalue(ra+1); - if (luai_numlt(0, step) ? luai_numle(idx, limit) - : luai_numle(limit, idx)) { - dojump(L, pc, GETARG_sBx(i)); /* jump back */ - setnvalue(ra, idx); /* update internal index... */ - setnvalue(ra+3, idx); /* ...and external index */ - } - continue; - } - case OP_FORPREP: { - const TValue *init = ra; - const TValue *plimit = ra+1; - const TValue *pstep = ra+2; - L->savedpc = pc; /* next steps may throw errors */ - if (!tonumber(init, ra)) - luaG_runerror(L, LUA_QL("for") " initial value must be a number"); - else if (!tonumber(plimit, ra+1)) - luaG_runerror(L, LUA_QL("for") " limit must be a number"); - else if (!tonumber(pstep, ra+2)) - luaG_runerror(L, LUA_QL("for") " step must be a number"); - setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); - dojump(L, pc, GETARG_sBx(i)); - continue; - } - case OP_TFORLOOP: { - StkId cb = ra + 3; /* call base */ - setobjs2s(L, cb+2, ra+2); - setobjs2s(L, cb+1, ra+1); - setobjs2s(L, cb, ra); - L->top = cb+3; /* func. + 2 args (state and index) */ - Protect(luaD_call(L, cb, GETARG_C(i))); - L->top = L->ci->top; - cb = RA(i) + 3; /* previous call may change the stack */ - if (!ttisnil(cb)) { /* continue loop? */ - setobjs2s(L, cb-1, cb); /* save control variable */ - dojump(L, pc, GETARG_sBx(*pc)); /* jump back */ - } - pc++; - continue; - } - case OP_SETLIST: { - int n = GETARG_B(i); - int c = GETARG_C(i); - int last; - Table *h; - if (n == 0) { - n = cast_int(L->top - ra) - 1; - L->top = L->ci->top; - } - if (c == 0) c = cast_int(*pc++); - runtime_check(L, ttistable(ra)); - h = hvalue(ra); - last = ((c-1)*LFIELDS_PER_FLUSH) + n; - if (last > h->sizearray) /* needs more space? */ - luaH_resizearray(L, h, last); /* pre-alloc it at once */ - for (; n > 0; n--) { - TValue *val = ra+n; - setobj2t(L, luaH_setnum(L, h, last--), val); - luaC_barriert(L, h, val); - } - continue; - } - case OP_CLOSE: { - luaF_close(L, ra); - continue; - } - case OP_CLOSURE: { - Proto *p; - Closure *ncl; - int nup, j; - p = cl->p->p[GETARG_Bx(i)]; - nup = p->nups; - ncl = luaF_newLclosure(L, nup, cl->env); - ncl->l.p = p; - for (j=0; jl.upvals[j] = cl->upvals[GETARG_B(*pc)]; - else { - lua_assert(GET_OPCODE(*pc) == OP_MOVE); - ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); - } - } - setclvalue(L, ra, ncl); - Protect(luaC_checkGC(L)); - continue; - } - case OP_VARARG: { - int b = GETARG_B(i) - 1; - int j; - CallInfo *ci = L->ci; - int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1; - if (b == LUA_MULTRET) { - Protect(luaD_checkstack(L, n)); - ra = RA(i); /* previous call may change the stack */ - b = n; - L->top = ra + n; - } - for (j = 0; j < b; j++) { - if (j < n) { - setobjs2s(L, ra + j, ci->base - n + j); - } - else { - setnilvalue(ra + j); - } - } - continue; - } - } - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.h deleted file mode 100644 index bfe4f56..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lvm.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lvm_h -#define lvm_h - - -#include "ldo.h" -#include "lobject.h" -#include "ltm.h" - - -#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) - -#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ - (((o) = luaV_tonumber(o,n)) != NULL)) - -#define equalobj(L,o1,o2) \ - (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) - - -LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); -LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); -LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); -LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, - StkId val); -LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, - StkId val); -LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); -LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.c deleted file mode 100644 index 293edd5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.c +++ /dev/null @@ -1,82 +0,0 @@ -/* -** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ -** a generic input stream interface -** See Copyright Notice in lua.h -*/ - - -#include - -#define lzio_c -#define LUA_CORE - -#include "lua.h" - -#include "llimits.h" -#include "lmem.h" -#include "lstate.h" -#include "lzio.h" - - -int luaZ_fill (ZIO *z) { - size_t size; - lua_State *L = z->L; - const char *buff; - lua_unlock(L); - buff = z->reader(L, z->data, &size); - lua_lock(L); - if (buff == NULL || size == 0) return EOZ; - z->n = size - 1; - z->p = buff; - return char2int(*(z->p++)); -} - - -int luaZ_lookahead (ZIO *z) { - if (z->n == 0) { - if (luaZ_fill(z) == EOZ) - return EOZ; - else { - z->n++; /* luaZ_fill removed first byte; put back it */ - z->p--; - } - } - return char2int(*z->p); -} - - -void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { - z->L = L; - z->reader = reader; - z->data = data; - z->n = 0; - z->p = NULL; -} - - -/* --------------------------------------------------------------- read --- */ -size_t luaZ_read (ZIO *z, void *b, size_t n) { - while (n) { - size_t m; - if (luaZ_lookahead(z) == EOZ) - return n; /* return number of missing bytes */ - m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ - memcpy(b, z->p, m); - z->n -= m; - z->p += m; - b = (char *)b + m; - n -= m; - } - return 0; -} - -/* ------------------------------------------------------------------------ */ -char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { - if (n > buff->buffsize) { - if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; - luaZ_resizebuffer(L, buff, n); - } - return buff->buffer; -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.h b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.h deleted file mode 100644 index 51d695d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/lzio.h +++ /dev/null @@ -1,67 +0,0 @@ -/* -** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - - -#ifndef lzio_h -#define lzio_h - -#include "lua.h" - -#include "lmem.h" - - -#define EOZ (-1) /* end of stream */ - -typedef struct Zio ZIO; - -#define char2int(c) cast(int, cast(unsigned char, (c))) - -#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) - -typedef struct Mbuffer { - char *buffer; - size_t n; - size_t buffsize; -} Mbuffer; - -#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) - -#define luaZ_buffer(buff) ((buff)->buffer) -#define luaZ_sizebuffer(buff) ((buff)->buffsize) -#define luaZ_bufflen(buff) ((buff)->n) - -#define luaZ_resetbuffer(buff) ((buff)->n = 0) - - -#define luaZ_resizebuffer(L, buff, size) \ - (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ - (buff)->buffsize = size) - -#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) - - -LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); -LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, - void *data); -LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ -LUAI_FUNC int luaZ_lookahead (ZIO *z); - - - -/* --------- Private Part ------------------ */ - -struct Zio { - size_t n; /* bytes still unread */ - const char *p; /* current position in buffer */ - lua_Reader reader; - void* data; /* additional data */ - lua_State *L; /* Lua state (for reader) */ -}; - - -LUAI_FUNC int luaZ_fill (ZIO *z); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/print.c b/LuaBridge3/Tests/Lua/Lua.5.1.5/src/print.c deleted file mode 100644 index 048e3f4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/src/print.c +++ /dev/null @@ -1,227 +0,0 @@ -/* -** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ -** print bytecodes -** See Copyright Notice in lua.h -*/ - -#include -#include - -#define luac_c -#define LUA_CORE - -#include "ldebug.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lundump.h" - -#define PrintFunction luaU_print - -#define Sizeof(x) ((int)sizeof(x)) -#define VOID(p) ((const void*)(p)) - -static void PrintString(const TString* ts) -{ - const char* s=getstr(ts); - size_t i,n=ts->tsv.len; - putchar('"'); - for (i=0; ik[i]; - switch (ttype(o)) - { - case LUA_TNIL: - printf("nil"); - break; - case LUA_TBOOLEAN: - printf(bvalue(o) ? "true" : "false"); - break; - case LUA_TNUMBER: - printf(LUA_NUMBER_FMT,nvalue(o)); - break; - case LUA_TSTRING: - PrintString(rawtsvalue(o)); - break; - default: /* cannot happen */ - printf("? type=%d",ttype(o)); - break; - } -} - -static void PrintCode(const Proto* f) -{ - const Instruction* code=f->code; - int pc,n=f->sizecode; - for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); - printf("%-9s\t",luaP_opnames[o]); - switch (getOpMode(o)) - { - case iABC: - printf("%d",a); - if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); - if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); - break; - case iABx: - if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); - break; - case iAsBx: - if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); - break; - } - switch (o) - { - case OP_LOADK: - printf("\t; "); PrintConstant(f,bx); - break; - case OP_GETUPVAL: - case OP_SETUPVAL: - printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); - break; - case OP_GETGLOBAL: - case OP_SETGLOBAL: - printf("\t; %s",svalue(&f->k[bx])); - break; - case OP_GETTABLE: - case OP_SELF: - if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } - break; - case OP_SETTABLE: - case OP_ADD: - case OP_SUB: - case OP_MUL: - case OP_DIV: - case OP_POW: - case OP_EQ: - case OP_LT: - case OP_LE: - if (ISK(b) || ISK(c)) - { - printf("\t; "); - if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); - printf(" "); - if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); - } - break; - case OP_JMP: - case OP_FORLOOP: - case OP_FORPREP: - printf("\t; to %d",sbx+pc+2); - break; - case OP_CLOSURE: - printf("\t; %p",VOID(f->p[bx])); - break; - case OP_SETLIST: - if (c==0) printf("\t; %d",(int)code[++pc]); - else printf("\t; %d",c); - break; - default: - break; - } - printf("\n"); - } -} - -#define SS(x) (x==1)?"":"s" -#define S(x) x,SS(x) - -static void PrintHeader(const Proto* f) -{ - const char* s=getstr(f->source); - if (*s=='@' || *s=='=') - s++; - else if (*s==LUA_SIGNATURE[0]) - s="(bstring)"; - else - s="(string)"; - printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", - (f->linedefined==0)?"main":"function",s, - f->linedefined,f->lastlinedefined, - S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", - f->numparams,f->is_vararg?"+":"",SS(f->numparams), - S(f->maxstacksize),S(f->nups)); - printf("%d local%s, %d constant%s, %d function%s\n", - S(f->sizelocvars),S(f->sizek),S(f->sizep)); -} - -static void PrintConstants(const Proto* f) -{ - int i,n=f->sizek; - printf("constants (%d) for %p:\n",n,VOID(f)); - for (i=0; isizelocvars; - printf("locals (%d) for %p:\n",n,VOID(f)); - for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); - } -} - -static void PrintUpvalues(const Proto* f) -{ - int i,n=f->sizeupvalues; - printf("upvalues (%d) for %p:\n",n,VOID(f)); - if (f->upvalues==NULL) return; - for (i=0; iupvalues[i])); - } -} - -void PrintFunction(const Proto* f, int full) -{ - int i,n=f->sizep; - PrintHeader(f); - PrintCode(f); - if (full) - { - PrintConstants(f); - PrintLocals(f); - PrintUpvalues(f); - } - for (i=0; ip[i],full); -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/README b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/README deleted file mode 100644 index 0c7f38b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/README +++ /dev/null @@ -1,26 +0,0 @@ -These are simple tests for Lua. Some of them contain useful code. -They are meant to be run to make sure Lua is built correctly and also -to be read, to see how Lua programs look. - -Here is a one-line summary of each program: - - bisect.lua bisection method for solving non-linear equations - cf.lua temperature conversion table (celsius to farenheit) - echo.lua echo command line arguments - env.lua environment variables as automatic global variables - factorial.lua factorial without recursion - fib.lua fibonacci function with cache - fibfor.lua fibonacci numbers with coroutines and generators - globals.lua report global variable usage - hello.lua the first program in every language - life.lua Conway's Game of Life - luac.lua bare-bones luac - printf.lua an implementation of printf - readonly.lua make global variables readonly - sieve.lua the sieve of of Eratosthenes programmed with coroutines - sort.lua two implementations of a sort function - table.lua make table, grouping all data for the same item - trace-calls.lua trace calls - trace-globals.lua trace assigments to global variables - xd.lua hex dump - diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/bisect.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/bisect.lua deleted file mode 100644 index f91e69b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/bisect.lua +++ /dev/null @@ -1,27 +0,0 @@ --- bisection method for solving non-linear equations - -delta=1e-6 -- tolerance - -function bisect(f,a,b,fa,fb) - local c=(a+b)/2 - io.write(n," c=",c," a=",a," b=",b,"\n") - if c==a or c==b or math.abs(a-b) posted to lua-l --- modified to use ANSI terminal escape sequences --- modified to use for instead of while - -local write=io.write - -ALIVE="" DEAD="" -ALIVE="O" DEAD="-" - -function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary - for i=1,10000 do end - -- local i=os.clock()+1 while(os.clock() 0 do - local xm1,x,xp1,xi=self.w-1,self.w,1,self.w - while xi > 0 do - local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + - self[y][xm1] + self[y][xp1] + - self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] - next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 - xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 - end - ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 - end -end - --- output the array to screen -function _CELLS:draw() - local out="" -- accumulate to reduce flicker - for y=1,self.h do - for x=1,self.w do - out=out..(((self[y][x]>0) and ALIVE) or DEAD) - end - out=out.."\n" - end - write(out) -end - --- constructor -function CELLS(w,h) - local c = ARRAY2D(w,h) - c.spawn = _CELLS.spawn - c.evolve = _CELLS.evolve - c.draw = _CELLS.draw - return c -end - --- --- shapes suitable for use with spawn() above --- -HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } -GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } -EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } -FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } -BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } - --- the main routine -function LIFE(w,h) - -- create two arrays - local thisgen = CELLS(w,h) - local nextgen = CELLS(w,h) - - -- create some life - -- about 1000 generations of fun, then a glider steady-state - thisgen:spawn(GLIDER,5,4) - thisgen:spawn(EXPLODE,25,10) - thisgen:spawn(FISH,4,12) - - -- run until break - local gen=1 - write("\027[2J") -- ANSI clear screen - while 1 do - thisgen:evolve(nextgen) - thisgen,nextgen = nextgen,thisgen - write("\027[H") -- ANSI home cursor - thisgen:draw() - write("Life - generation ",gen,"\n") - gen=gen+1 - if gen>2000 then break end - --delay() -- no delay - end -end - -LIFE(40,20) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/luac.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/luac.lua deleted file mode 100644 index 96a0a97..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/luac.lua +++ /dev/null @@ -1,7 +0,0 @@ --- bare-bones luac in Lua --- usage: lua luac.lua file.lua - -assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua") -f=assert(io.open("luac.out","wb")) -assert(f:write(string.dump(assert(loadfile(arg[1]))))) -assert(f:close()) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/printf.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/printf.lua deleted file mode 100644 index 58c63ff..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/printf.lua +++ /dev/null @@ -1,7 +0,0 @@ --- an implementation of printf - -function printf(...) - io.write(string.format(...)) -end - -printf("Hello %s from %s on %s\n",os.getenv"USER" or "there",_VERSION,os.date()) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/readonly.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/readonly.lua deleted file mode 100644 index 85c0b4e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/readonly.lua +++ /dev/null @@ -1,12 +0,0 @@ --- make global variables readonly - -local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end -local g={} -local G=getfenv() -setmetatable(g,{__index=G,__newindex=f}) -setfenv(1,g) - --- an example -rawset(g,"x",3) -x=2 -y=1 -- cannot redefine `y' diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/sieve.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/sieve.lua deleted file mode 100644 index 0871bb2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/sieve.lua +++ /dev/null @@ -1,29 +0,0 @@ --- the sieve of of Eratosthenes programmed with coroutines --- typical usage: lua -e N=1000 sieve.lua | column - --- generate all the numbers from 2 to n -function gen (n) - return coroutine.wrap(function () - for i=2,n do coroutine.yield(i) end - end) -end - --- filter the numbers generated by `g', removing multiples of `p' -function filter (p, g) - return coroutine.wrap(function () - while 1 do - local n = g() - if n == nil then return end - if math.mod(n, p) ~= 0 then coroutine.yield(n) end - end - end) -end - -N=N or 1000 -- from command line -x = gen(N) -- generate primes up to N -while 1 do - local n = x() -- pick a number until done - if n == nil then break end - print(n) -- must be a prime number - x = filter(n, x) -- now remove its multiples -end diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/sort.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/sort.lua deleted file mode 100644 index 0bcb15f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/sort.lua +++ /dev/null @@ -1,66 +0,0 @@ --- two implementations of a sort function --- this is an example only. Lua has now a built-in function "sort" - --- extracted from Programming Pearls, page 110 -function qsort(x,l,u,f) - if ly end) - show("after reverse selection sort",x) - qsort(x,1,n,function (x,y) return x>> ",string.rep(" ",level)) - if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end - t=debug.getinfo(2) - if event=="call" then - level=level+1 - else - level=level-1 if level<0 then level=0 end - end - if t.what=="main" then - if event=="call" then - io.write("begin ",t.short_src) - else - io.write("end ",t.short_src) - end - elseif t.what=="Lua" then --- table.foreach(t,print) - io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") - else - io.write(event," ",t.name or "(C)"," [",t.what,"] ") - end - io.write("\n") -end - -debug.sethook(hook,"cr") -level=0 diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/trace-globals.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/trace-globals.lua deleted file mode 100644 index 295e670..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/trace-globals.lua +++ /dev/null @@ -1,38 +0,0 @@ --- trace assigments to global variables - -do - -- a tostring that quotes strings. note the use of the original tostring. - local _tostring=tostring - local tostring=function(a) - if type(a)=="string" then - return string.format("%q",a) - else - return _tostring(a) - end - end - - local log=function (name,old,new) - local t=debug.getinfo(3,"Sl") - local line=t.currentline - io.write(t.short_src) - if line>=0 then io.write(":",line) end - io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") - end - - local g={} - local set=function (t,name,value) - log(name,g[name],value) - g[name]=value - end - setmetatable(getfenv(),{__index=g,__newindex=set}) -end - --- an example - -a=1 -b=2 -a=10 -b=20 -b=nil -b=200 -print(a,b,c) diff --git a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/xd.lua b/LuaBridge3/Tests/Lua/Lua.5.1.5/test/xd.lua deleted file mode 100644 index ebc3eff..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.1.5/test/xd.lua +++ /dev/null @@ -1,14 +0,0 @@ --- hex dump --- usage: lua xd.lua < file - -local offset=0 -while true do - local s=io.read(16) - if s==nil then return end - io.write(string.format("%08X ",offset)) - string.gsub(s,"(.)", - function (c) io.write(string.format("%02X ",string.byte(c))) end) - io.write(string.rep(" ",3*(16-string.len(s)))) - io.write(" ",string.gsub(s,"%c","."),"\n") - offset=offset+16 -end diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/README b/LuaBridge3/Tests/Lua/Lua.5.2.4/README deleted file mode 100644 index 710e3ee..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/README +++ /dev/null @@ -1,6 +0,0 @@ - -This is Lua 5.2.4, released on 25 Feb 2015. - -For installation instructions, license details, and -further information about Lua, see doc/readme.html. - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/contents.html b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/contents.html deleted file mode 100644 index 9d5202f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/contents.html +++ /dev/null @@ -1,608 +0,0 @@ - - - -Lua 5.2 Reference Manual - contents - - - - - - - -


-

- -Lua 5.2 Reference Manual -

- -

-The reference manual is the official definition of the Lua language. -For a complete introduction to Lua programming, see the book -Programming in Lua. - -

-start -· -contents -· -index -


- -Copyright © 2011–2015 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

Contents

- - -

Index

- - - - - - - -
-

Lua functions

-

-basic
-_G
-_VERSION
- -assert
-collectgarbage
-dofile
-error
-getmetatable
-ipairs
-load
-loadfile
-next
-pairs
-pcall
-print
-rawequal
-rawget
-rawlen
-rawset
-require
-select
-setmetatable
-tonumber
-tostring
-type
-xpcall
- -

-bit32
-bit32.arshift
-bit32.band
-bit32.bnot
-bit32.bor
-bit32.btest
-bit32.bxor
-bit32.extract
-bit32.lrotate
-bit32.lshift
-bit32.replace
-bit32.rrotate
-bit32.rshift
- -

-coroutine
-coroutine.create
-coroutine.resume
-coroutine.running
-coroutine.status
-coroutine.wrap
-coroutine.yield
- -

-debug
-debug.debug
-debug.getuservalue
-debug.gethook
-debug.getinfo
-debug.getlocal
-debug.getmetatable
-debug.getregistry
-debug.getupvalue
-debug.setuservalue
-debug.sethook
-debug.setlocal
-debug.setmetatable
-debug.setupvalue
-debug.traceback
-debug.upvalueid
-debug.upvaluejoin
- -

-io
-io.close
-io.flush
-io.input
-io.lines
-io.open
-io.output
-io.popen
-io.read
-io.stderr
-io.stdin
-io.stdout
-io.tmpfile
-io.type
-io.write
-file:close
-file:flush
-file:lines
-file:read
-file:seek
-file:setvbuf
-file:write
- -

-

 

-

-math
-math.abs
-math.acos
-math.asin
-math.atan
-math.atan2
-math.ceil
-math.cos
-math.cosh
-math.deg
-math.exp
-math.floor
-math.fmod
-math.frexp
-math.huge
-math.ldexp
-math.log
-math.max
-math.min
-math.modf
-math.pi
-math.pow
-math.rad
-math.random
-math.randomseed
-math.sin
-math.sinh
-math.sqrt
-math.tan
-math.tanh
- -

-os
-os.clock
-os.date
-os.difftime
-os.execute
-os.exit
-os.getenv
-os.remove
-os.rename
-os.setlocale
-os.time
-os.tmpname
- -

-package
-package.config
-package.cpath
-package.loaded
-package.loadlib
-package.path
-package.preload
-package.searchers
-package.searchpath
- -

-string
-string.byte
-string.char
-string.dump
-string.find
-string.format
-string.gmatch
-string.gsub
-string.len
-string.lower
-string.match
-string.rep
-string.reverse
-string.sub
-string.upper
- -

-table
-table.concat
-table.insert
-table.pack
-table.remove
-table.sort
-table.unpack
- -

environment
variables

-LUA_CPATH
-LUA_CPATH_5_2
-LUA_INIT
-LUA_INIT_5_2
-LUA_PATH
-LUA_PATH_5_2
- -
-

C API

-

-lua_Alloc
-lua_CFunction
-lua_Debug
-lua_Hook
-lua_Integer
-lua_Number
-lua_Reader
-lua_State
-lua_Unsigned
-lua_Writer
- -

-lua_absindex
-lua_arith
-lua_atpanic
-lua_call
-lua_callk
-lua_checkstack
-lua_close
-lua_compare
-lua_concat
-lua_copy
-lua_createtable
-lua_dump
-lua_error
-lua_gc
-lua_getallocf
-lua_getctx
-lua_getfield
-lua_getglobal
-lua_gethook
-lua_gethookcount
-lua_gethookmask
-lua_getinfo
-lua_getlocal
-lua_getmetatable
-lua_getstack
-lua_gettable
-lua_gettop
-lua_getupvalue
-lua_getuservalue
-lua_insert
-lua_isboolean
-lua_iscfunction
-lua_isfunction
-lua_islightuserdata
-lua_isnil
-lua_isnone
-lua_isnoneornil
-lua_isnumber
-lua_isstring
-lua_istable
-lua_isthread
-lua_isuserdata
-lua_len
-lua_load
-lua_newstate
-lua_newtable
-lua_newthread
-lua_newuserdata
-lua_next
-lua_pcall
-lua_pcallk
-lua_pop
-lua_pushboolean
-lua_pushcclosure
-lua_pushcfunction
-lua_pushfstring
-lua_pushglobaltable
-lua_pushinteger
-lua_pushlightuserdata
-lua_pushliteral
-lua_pushlstring
-lua_pushnil
-lua_pushnumber
-lua_pushstring
-lua_pushthread
-lua_pushunsigned
-lua_pushvalue
-lua_pushvfstring
-lua_rawequal
-lua_rawget
-lua_rawgeti
-lua_rawgetp
-lua_rawlen
-lua_rawset
-lua_rawseti
-lua_rawsetp
-lua_register
-lua_remove
-lua_replace
-lua_resume
-lua_setallocf
-lua_setfield
-lua_setglobal
-lua_sethook
-lua_setlocal
-lua_setmetatable
-lua_settable
-lua_settop
-lua_setupvalue
-lua_setuservalue
-lua_status
-lua_toboolean
-lua_tocfunction
-lua_tointeger
-lua_tointegerx
-lua_tolstring
-lua_tonumber
-lua_tonumberx
-lua_topointer
-lua_tostring
-lua_tothread
-lua_tounsigned
-lua_tounsignedx
-lua_touserdata
-lua_type
-lua_typename
-lua_upvalueid
-lua_upvalueindex
-lua_upvaluejoin
-lua_version
-lua_xmove
-lua_yield
-lua_yieldk
- -

-

auxiliary library

-

-luaL_Buffer
-luaL_Reg
- -

-luaL_addchar
-luaL_addlstring
-luaL_addsize
-luaL_addstring
-luaL_addvalue
-luaL_argcheck
-luaL_argerror
-luaL_buffinit
-luaL_buffinitsize
-luaL_callmeta
-luaL_checkany
-luaL_checkint
-luaL_checkinteger
-luaL_checklong
-luaL_checklstring
-luaL_checknumber
-luaL_checkoption
-luaL_checkstack
-luaL_checkstring
-luaL_checktype
-luaL_checkudata
-luaL_checkunsigned
-luaL_checkversion
-luaL_dofile
-luaL_dostring
-luaL_error
-luaL_execresult
-luaL_fileresult
-luaL_getmetafield
-luaL_getmetatable
-luaL_getsubtable
-luaL_gsub
-luaL_len
-luaL_loadbuffer
-luaL_loadbufferx
-luaL_loadfile
-luaL_loadfilex
-luaL_loadstring
-luaL_newlib
-luaL_newlibtable
-luaL_newmetatable
-luaL_newstate
-luaL_openlibs
-luaL_optint
-luaL_optinteger
-luaL_optlong
-luaL_optlstring
-luaL_optnumber
-luaL_optstring
-luaL_optunsigned
-luaL_prepbuffer
-luaL_prepbuffsize
-luaL_pushresult
-luaL_pushresultsize
-luaL_ref
-luaL_requiref
-luaL_setfuncs
-luaL_setmetatable
-luaL_testudata
-luaL_tolstring
-luaL_traceback
-luaL_typename
-luaL_unref
-luaL_where
- -

standard library

-

-luaopen_base
-luaopen_bit32
-luaopen_coroutine
-luaopen_debug
-luaopen_io
-luaopen_math
-luaopen_os
-luaopen_package
-luaopen_string
-luaopen_table
- -

constants

-LUA_ERRERR
-LUA_ERRFILE
-LUA_ERRGCMM
-LUA_ERRMEM
-LUA_ERRRUN
-LUA_ERRSYNTAX
-LUA_HOOKCALL
-LUA_HOOKCOUNT
-LUA_HOOKLINE
-LUA_HOOKRET
-LUA_HOOKTAILCALL
-LUA_MASKCALL
-LUA_MASKCOUNT
-LUA_MASKLINE
-LUA_MASKRET
-LUA_MINSTACK
-LUA_MULTRET
-LUA_NOREF
-LUA_OK
-LUA_OPADD
-LUA_OPDIV
-LUA_OPEQ
-LUA_OPLE
-LUA_OPLT
-LUA_OPMOD
-LUA_OPMUL
-LUA_OPPOW
-LUA_OPSUB
-LUA_OPUNM
-LUA_REFNIL
-LUA_REGISTRYINDEX
-LUA_RIDX_GLOBALS
-LUA_RIDX_MAINTHREAD
-LUA_TBOOLEAN
-LUA_TFUNCTION
-LUA_TLIGHTUSERDATA
-LUA_TNIL
-LUA_TNONE
-LUA_TNUMBER
-LUA_TSTRING
-LUA_TTABLE
-LUA_TTHREAD
-LUA_TUSERDATA
-LUA_USE_APICHECK
-LUA_YIELD
-LUAL_BUFFERSIZE
- -
- -
- -Last update: -Mon Feb 23 22:24:36 BRT 2015 - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/logo.gif b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/logo.gif deleted file mode 100644 index 2f5e4ac2e742fbb7675e739879211553758aea9c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4232 zcmeH``9G8i;K!fm@ywWE@XWZzZ5SF?A>^uN#>^O6HI%DVL*tw8h1>$H%uPC0$QQ=txe!o}PX)Ev+jn>*nFZ-TC=<^76WcLZL(=DJm)| zEiIKwrInSHXU?3duCC_u@5try#>U2`rlw1mF15F}U%!66tE;QKyIUmcDJ<+QF77*W zFJd#&)VAl)kJ6K zi<>tmZ{3>g>+2gB7#JQNe(>OdLh;A=`1q42PbMZNCMPF*dXxhLZw3aYhlZwyhu@Bj z%#4n{d-Q1b$&i4QMce4L#8^!oMdw{PDnm4D66&3*dxX=-YIX6DQL_g`jbzkd4k zZDCoLf=%jL&vIeE zO=XcZ9fxt`f}-DQ^%H*PHMUs(JN%UWkI|Y8h9#6~I$Cw@{RqzO4&P-x;jHCPJ6Ks2 zoU%foi)nXd_sdkiuJa@@5J4RrreKfWSnz5>eMa5yTP=)16uu)TIdx~Fhho))6jZl) z($*i>QrIX4u}u3>m{WSn_ehkUGQ& zs})aUlTH1Cj1g3ZE3=MPXsSniEwJ{e6C3N#HjD=B4`8rWIsz!a7ecYpec?WuH+y?Wsm18^$cS4WmHhH3_=r zh*ILlm*X1dB^E5($KVl&zT524%l}vpHg%;Y+LezV_&TAJCmH`idhuj-n$4FZ)UE|jXLayXa-&O3Q z?Iyo!x*$5hD_HfFnDfGYj-RD|eIb7I?%>Y_kf%}Nbd`BXb4l1(Pc+}zoUR|9%_!7f zum2T;wbx&pohtI+&@~wm3nH9xLbOYkg*`phY~TK5iC#3tZNXo9s`cahx+8j2)rh5C zQgZh6D7Ekgib|hpdhxYf{r!PTJc z!vsYG@{hA}l5kL)g)0N_)(nC<*L0qdUi*3fD5<0sn58>zklX@6Tyv3*X^}m=Cqc40 zQ6GfjG@kd1mFIm`qaubWunm_?P>WUZ`9|f_z%gGHi{n|uu(N8!L=aw5(qAcDj$-QK zu;D#j6e42OXTQD>)i zlvM$LX`$n9EEjxM$_QDF&a z7cme_rat}aXmiN&7`6Q98}dh4Z@8L_uAb#nK&GQiZOOUnA9kAEVb-csuN1AWL=sXt z{z9GCN%%l0N9QvJM;tl1nf?rrhT{*sE%4WqR?{0~aIrfCcCPxf4eh_*jjQ=`$p53Y z@_|Rsx2i}|3dNFetMQQ5y8agTK-E0D&7;@3-LUxfvZ7 z7~!p@&mFe^oca2^F|CBt+4Ly?^ViUVSAhAH>JH1GN{^TQb3QnM*x0ZiZgDyNI@_c3 z@{}(WH4*e3T~}n_^0}da4ElIxAf9B!IaL7z9X0Icvj@cIkE*~W--17&WN`Ea5)Gn> z#gpfRb#44;jVTOS{FuaZgd(-ZD848=fQzgST2MxR>wSLc1P=2HDvByz$B$IsNCC6L zCM?nK*OHj6JA9gz4|b<~2%RqelN^1Y)jIqnRs!mDKV^BQTfo@hOtz7*Ug}Ee^cbsj zNNlumRgAmt`1$b5MO;&X#5-EP<}AaY;52ihIpem&MTea$?3!DrwbYa?V`NjEfWF3z zUq5JY8Ch;L{kx&J<1K&Fe_Vn;8gk{%c;n?nA2(%(f%DCRHko3uT~VI7RE^JWEqaCq z)i|%nfj(*4|V*XhY3W%M# z*yn6SN4eUOHFxAD7B&9E_PO`G5bqgs^@J{9bk>&;PlUAiqo`j3rjQDgD!}mqLUtb` zCB}ZD@m@s#pf7bV4jreOC*JVfHZ|hyHkX!rauVdd_I9FL45d{gWH!DNYu;i(|8wVx z!)eLY6YXxZ2{Coae0xuTnxo1ACb5wtED?VJAz&@114$Ao6uG9YSy*!K;m5_mj=0^j zw%?b%AOs}ql@$TGC-!^^*_#RT5+y_kTzQG9?LPPZNAtt6cJ%d2$q(I)ws21*?xF%p zN+NeGnWRQ<5w70Rc(bl|S0Xr&5@WrmdurS|IgPB|EyuZO#=tf!35)G!HJ`E1jh^lH zTBu~rL#DhQO*XAWtBt}JHH$lc>3%r0yD|maW_(W=B_J+y164F>O4dO|@&@N3Z3p=B zmVl{|^Z&#atHY|9n&la)SBo}=3AFIF=_~LDJk6MTlA73CXtX+4bnn+c!}N}IPa5pp zwyqbqIkN|I3j_3vD6$zlu{Ps(N-J|*qzEt<$5Soh;s^AuKv_ z-Tz+O1_~6*9CJh4r}`}mbUtjbf#fX58RIIkP6&@*y9kI|5fK*_eZ%jv3U$5*x<>D_ za2M(TV8?XY+9xy>0En#Te<6X4$0&dbyd(go$~eq4u(u)EA2msyF<5ssLZ zDP|I}=~Bi_q)whWv=Ri~L1TYaNrR;5cMB@s78HF1{w&r(6GJ;_2@bD?#1p&P4n_?n0#9Vx~$qjMX=Lk?*!@aKo8m&$iPO7S{g3sFUwr`*<53(68xx7?z`2xf# zGSicy_zI(PJ|%qc2VxT+6bOE--a{k&aq7$<<= zFt)C<@|TPs`+eycPGoGL1Wn9|Ed&a2JyAmjnkm3DQBECX&`bt~odH9cUPq4M{#$-q?G3!)qO-it*&YHw+j-O* zYy78V*`4Q=kQ@^Yz*b6Tal4(Me7BGeS^;phWAW8+L^5A(=D)t?k!rLIwVAKtq=f7h z&^n&VX1-T$ScvN~639QLZ^d@niMaS{C-Q)8oHHBhwD*r~-1Ze#Q)GFOFptW32a-uF z;M@ux%i%a25NwIgXt*=GHX$3~aZfwovGL!}sf?j9TsVo^cn(%&a<--0mIXYqGe>c PWz_J}_#7St0k8iB@FZjZ diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.1 b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.1 deleted file mode 100644 index 1dbf043..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.1 +++ /dev/null @@ -1,116 +0,0 @@ -.\" $Id: lua.man,v 1.13 2011/11/16 17:16:53 lhf Exp $ -.TH LUA 1 "$Date: 2011/11/16 17:16:53 $" -.SH NAME -lua \- Lua interpreter -.SH SYNOPSIS -.B lua -[ -.I options -] -[ -.I script -[ -.I args -] -] -.SH DESCRIPTION -.B lua -is the standalone Lua interpreter. -It loads and executes Lua programs, -either in textual source form or -in precompiled binary form. -(Precompiled binaries are output by -.BR luac , -the Lua compiler.) -.B lua -can be used as a batch interpreter and also interactively. -.LP -The given -.I options -are handled in order and then -the Lua program in file -.I script -is loaded and executed. -The given -.I args -are available to -.I script -as strings in a global table named -.BR arg . -If no options or arguments are given, -then -.B "\-v \-i" -is assumed when the standard input is a terminal; -otherwise, -.B "\-" -is assumed. -.LP -In interactive mode, -.B lua -prompts the user, -reads lines from the standard input, -and executes them as they are read. -If a line does not contain a complete statement, -then a secondary prompt is displayed and -lines are read until a complete statement is formed or -a syntax error is found. -If a line starts with -.BR '=' , -then -.B lua -evaluates and displays -the values of the expressions in the remainder of the line. -.LP -At the very start, -before even handling the command line, -.B lua -checks the contents of the environment variables -.B LUA_INIT_5_2 -or -.BR LUA_INIT , -in that order. -If the contents is of the form -.RI '@ filename ', -then -.I filename -is executed. -Otherwise, the string is assumed to be a Lua statement and is executed. -.SH OPTIONS -.TP -.BI \-e " stat" -execute statement -.IR stat . -.TP -.B \-i -enter interactive mode after executing -.IR script . -.TP -.BI \-l " name" -execute the equivalent of -.IB name =require(' name ') -before executing -.IR script . -.TP -.B \-v -show version information. -.TP -.B \-E -ignore environment variables. -.TP -.B \-\- -stop handling options. -.TP -.B \- -stop handling options and execute the standard input as a file. -.SH "SEE ALSO" -.BR luac (1) -.br -The documentation at lua.org, -especially section 7 of the reference manual. -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.css b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.css deleted file mode 100644 index 5dc9a8b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/lua.css +++ /dev/null @@ -1,106 +0,0 @@ -html { - background-color: #F8F8F8 ; -} - -body { - border: solid #a0a0a0 1px ; - border-radius: 20px ; - padding: 26px ; - margin: 16px ; - color: #000000 ; - background-color: #FFFFFF ; - font-family: Helvetica, Arial, sans-serif ; - text-align: justify ; - line-height: 1.25 ; -} - -h1, h2, h3, h4 { - font-family: Verdana, Geneva, sans-serif ; - font-weight: normal ; - font-style: normal ; -} - -h2 { - padding-top: 0.4em ; - padding-bottom: 0.4em ; - padding-left: 0.8em ; - padding-right: 0.8em ; - background-color: #D0D0FF ; - border-radius: 8px ; - border: solid #a0a0a0 1px ; -} - -h3 { - padding-left: 0.5em ; - border-left: solid #D0D0FF 1em ; -} - -table h3 { - padding-left: 0px ; - border-left: none ; -} - -a:link { - color: #000080 ; - background-color: inherit ; - text-decoration: none ; -} - -a:visited { - background-color: inherit ; - text-decoration: none ; -} - -a:link:hover, a:visited:hover { - color: #000080 ; - background-color: #D0D0FF ; - border-radius: 4px ; -} - -a:link:active, a:visited:active { - color: #FF0000 ; -} - -h1 a img { - vertical-align: text-bottom ; -} - -hr { - border: 0 ; - height: 1px ; - color: #a0a0a0 ; - background-color: #a0a0a0 ; - display: none ; -} - -table hr { - display: block ; -} - -:target { - background-color: #F8F8F8 ; - padding: 8px ; - border: solid #a0a0a0 2px ; - border-radius: 8px ; -} - -.footer { - color: gray ; - font-size: x-small ; -} - -input[type=text] { - border: solid #a0a0a0 2px ; - border-radius: 2em ; - background-image: url('images/search.png') ; - background-repeat: no-repeat ; - background-position: 4px center ; - padding-left: 20px ; - height: 2em ; -} - -pre.session { - background-color: #F8F8F8 ; - padding: 1em ; - border-radius: 8px ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/luac.1 b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/luac.1 deleted file mode 100644 index 33a4ed0..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/luac.1 +++ /dev/null @@ -1,118 +0,0 @@ -.\" $Id: luac.man,v 1.29 2011/11/16 13:53:40 lhf Exp $ -.TH LUAC 1 "$Date: 2011/11/16 13:53:40 $" -.SH NAME -luac \- Lua compiler -.SH SYNOPSIS -.B luac -[ -.I options -] [ -.I filenames -] -.SH DESCRIPTION -.B luac -is the Lua compiler. -It translates programs written in the Lua programming language -into binary files containing precompiled chunks -that can be later loaded and executed. -.LP -The main advantages of precompiling chunks are: -faster loading, -protecting source code from accidental user changes, -and -off-line syntax checking. -Precompiling does not imply faster execution -because in Lua chunks are always compiled into bytecodes before being executed. -.B luac -simply allows those bytecodes to be saved in a file for later execution. -Precompiled chunks are not necessarily smaller than the corresponding source. -The main goal in precompiling is faster loading. -.LP -In the command line, -you can mix -text files containing Lua source and -binary files containing precompiled chunks. -.B luac -produces a single output file containing the combined bytecodes -for all files given. -Executing the combined file is equivalent to executing the given files. -By default, -the output file is named -.BR luac.out , -but you can change this with the -.B \-o -option. -.LP -Precompiled chunks are -.I not -portable across different architectures. -Moreover, -the internal format of precompiled chunks -is likely to change when a new version of Lua is released. -Make sure you save the source files of all Lua programs that you precompile. -.LP -.SH OPTIONS -.TP -.B \-l -produce a listing of the compiled bytecode for Lua's virtual machine. -Listing bytecodes is useful to learn about Lua's virtual machine. -If no files are given, then -.B luac -loads -.B luac.out -and lists its contents. -Use -.B \-l \-l -for a full listing. -.TP -.BI \-o " file" -output to -.IR file , -instead of the default -.BR luac.out . -(You can use -.B "'\-'" -for standard output, -but not on platforms that open standard output in text mode.) -The output file may be one of the given files because -all files are loaded before the output file is written. -Be careful not to overwrite precious files. -.TP -.B \-p -load files but do not generate any output file. -Used mainly for syntax checking and for testing precompiled chunks: -corrupted files will probably generate errors when loaded. -If no files are given, then -.B luac -loads -.B luac.out -and tests its contents. -No messages are displayed if the file loads without errors. -.TP -.B \-s -strip debug information before writing the output file. -This saves some space in very large chunks, -but if errors occur when running a stripped chunk, -then the error messages may not contain the full information they usually do. -In particular, -line numbers and names of local variables are lost. -.TP -.B \-v -show version information. -.TP -.B \-\- -stop handling options. -.TP -.B \- -stop handling options and process standard input. -.SH "SEE ALSO" -.BR lua (1) -.br -The documentation at lua.org. -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.css b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.css deleted file mode 100644 index ca613cd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.css +++ /dev/null @@ -1,27 +0,0 @@ -h3 code { - font-family: inherit ; - font-size: inherit ; -} - -pre, code { - font-size: 12pt ; -} - -span.apii { - float: right ; - font-family: inherit ; - font-style: normal ; - font-size: small ; - color: gray ; -} - -p+h1, ul+h1 { - font-style: normal ; - padding-top: 0.4em ; - padding-bottom: 0.4em ; - padding-left: 16px ; - margin-left: -16px ; - background-color: #D0D0FF ; - border-radius: 8px ; - border: solid #000080 1px ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.html b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.html deleted file mode 100644 index 0a95e9e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/manual.html +++ /dev/null @@ -1,10508 +0,0 @@ - - - - -Lua 5.2 Reference Manual - - - - - - - -
-

- -Lua 5.2 Reference Manual -

- -by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes -

- -Copyright © 2011–2015 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - -


-

- -contents -· -index - - -

- - - - - - -

1 – Introduction

- -

-Lua is an extension programming language designed to support -general procedural programming with data description -facilities. -It also offers good support for object-oriented programming, -functional programming, and data-driven programming. -Lua is intended to be used as a powerful, lightweight, -embeddable scripting language for any program that needs one. -Lua is implemented as a library, written in clean C, -the common subset of Standard C and C++. - - -

-Being an extension language, Lua has no notion of a "main" program: -it only works embedded in a host client, -called the embedding program or simply the host. -The host program can invoke functions to execute a piece of Lua code, -can write and read Lua variables, -and can register C functions to be called by Lua code. -Through the use of C functions, Lua can be augmented to cope with -a wide range of different domains, -thus creating customized programming languages sharing a syntactical framework. -The Lua distribution includes a sample host program called lua, -which uses the Lua library to offer a complete, standalone Lua interpreter, -for interactive or batch use. - - -

-Lua is free software, -and is provided as usual with no guarantees, -as stated in its license. -The implementation described in this manual is available -at Lua's official web site, www.lua.org. - - -

-Like any other reference manual, -this document is dry in places. -For a discussion of the decisions behind the design of Lua, -see the technical papers available at Lua's web site. -For a detailed introduction to programming in Lua, -see Roberto's book, Programming in Lua. - - - -

2 – Basic Concepts

- -

-This section describes the basic concepts of the language. - - - -

2.1 – Values and Types

- -

-Lua is a dynamically typed language. -This means that -variables do not have types; only values do. -There are no type definitions in the language. -All values carry their own type. - - -

-All values in Lua are first-class values. -This means that all values can be stored in variables, -passed as arguments to other functions, and returned as results. - - -

-There are eight basic types in Lua: -nil, boolean, number, -string, function, userdata, -thread, and table. -Nil is the type of the value nil, -whose main property is to be different from any other value; -it usually represents the absence of a useful value. -Boolean is the type of the values false and true. -Both nil and false make a condition false; -any other value makes it true. -Number represents real (double-precision floating-point) numbers. -Operations on numbers follow the same rules of -the underlying C implementation, -which, in turn, usually follows the IEEE 754 standard. -(It is easy to build Lua interpreters that use other -internal representations for numbers, -such as single-precision floats or long integers; -see file luaconf.h.) -String represents immutable sequences of bytes. - -Lua is 8-bit clean: -strings can contain any 8-bit value, -including embedded zeros ('\0'). - - -

-Lua can call (and manipulate) functions written in Lua and -functions written in C -(see §3.4.9). - - -

-The type userdata is provided to allow arbitrary C data to -be stored in Lua variables. -A userdata value is a pointer to a block of raw memory. -There are two kinds of userdata: -full userdata, where the block of memory is managed by Lua, -and light userdata, where the block of memory is managed by the host. -Userdata has no predefined operations in Lua, -except assignment and identity test. -By using metatables, -the programmer can define operations for full userdata values -(see §2.4). -Userdata values cannot be created or modified in Lua, -only through the C API. -This guarantees the integrity of data owned by the host program. - - -

-The type thread represents independent threads of execution -and it is used to implement coroutines (see §2.6). -Do not confuse Lua threads with operating-system threads. -Lua supports coroutines on all systems, -even those that do not support threads. - - -

-The type table implements associative arrays, -that is, arrays that can be indexed not only with numbers, -but with any Lua value except nil and NaN -(Not a Number, a special numeric value used to represent -undefined or unrepresentable results, such as 0/0). -Tables can be heterogeneous; -that is, they can contain values of all types (except nil). -Any key with value nil is not considered part of the table. -Conversely, any key that is not part of a table has -an associated value nil. - - -

-Tables are the sole data structuring mechanism in Lua; -they can be used to represent ordinary arrays, sequences, -symbol tables, sets, records, graphs, trees, etc. -To represent records, Lua uses the field name as an index. -The language supports this representation by -providing a.name as syntactic sugar for a["name"]. -There are several convenient ways to create tables in Lua -(see §3.4.8). - - -

-We use the term sequence to denote a table where -the set of all positive numeric keys is equal to {1..n} -for some integer n, -which is called the length of the sequence (see §3.4.6). - - -

-Like indices, -the values of table fields can be of any type. -In particular, -because functions are first-class values, -table fields can contain functions. -Thus tables can also carry methods (see §3.4.10). - - -

-The indexing of tables follows -the definition of raw equality in the language. -The expressions a[i] and a[j] -denote the same table element -if and only if i and j are raw equal -(that is, equal without metamethods). - - -

-Tables, functions, threads, and (full) userdata values are objects: -variables do not actually contain these values, -only references to them. -Assignment, parameter passing, and function returns -always manipulate references to such values; -these operations do not imply any kind of copy. - - -

-The library function type returns a string describing the type -of a given value (see §6.1). - - - - - -

2.2 – Environments and the Global Environment

- -

-As will be discussed in §3.2 and §3.3.3, -any reference to a global name var is syntactically translated -to _ENV.var. -Moreover, every chunk is compiled in the scope of -an external local variable called _ENV (see §3.3.2), -so _ENV itself is never a global name in a chunk. - - -

-Despite the existence of this external _ENV variable and -the translation of global names, -_ENV is a completely regular name. -In particular, -you can define new variables and parameters with that name. -Each reference to a global name uses the _ENV that is -visible at that point in the program, -following the usual visibility rules of Lua (see §3.5). - - -

-Any table used as the value of _ENV is called an environment. - - -

-Lua keeps a distinguished environment called the global environment. -This value is kept at a special index in the C registry (see §4.5). -In Lua, the variable _G is initialized with this same value. - - -

-When Lua compiles a chunk, -it initializes the value of its _ENV upvalue -with the global environment (see load). -Therefore, by default, -global variables in Lua code refer to entries in the global environment. -Moreover, all standard libraries are loaded in the global environment -and several functions there operate on that environment. -You can use load (or loadfile) -to load a chunk with a different environment. -(In C, you have to load the chunk and then change the value -of its first upvalue.) - - -

-If you change the global environment in the registry -(through C code or the debug library), -all chunks loaded after the change will get the new environment. -Previously loaded chunks are not affected, however, -as each has its own reference to the environment in its _ENV variable. -Moreover, the variable _G -(which is stored in the original global environment) -is never updated by Lua. - - - - - -

2.3 – Error Handling

- -

-Because Lua is an embedded extension language, -all Lua actions start from C code in the host program -calling a function from the Lua library (see lua_pcall). -Whenever an error occurs during -the compilation or execution of a Lua chunk, -control returns to the host, -which can take appropriate measures -(such as printing an error message). - - -

-Lua code can explicitly generate an error by calling the -error function. -If you need to catch errors in Lua, -you can use pcall or xpcall -to call a given function in protected mode. - - -

-Whenever there is an error, -an error object (also called an error message) -is propagated with information about the error. -Lua itself only generates errors where the error object is a string, -but programs may generate errors with -any value for the error object. - - -

-When you use xpcall or lua_pcall, -you may give a message handler -to be called in case of errors. -This function is called with the original error message -and returns a new error message. -It is called before the error unwinds the stack, -so that it can gather more information about the error, -for instance by inspecting the stack and creating a stack traceback. -This message handler is still protected by the protected call; -so, an error inside the message handler -will call the message handler again. -If this loop goes on, Lua breaks it and returns an appropriate message. - - - - - -

2.4 – Metatables and Metamethods

- -

-Every value in Lua can have a metatable. -This metatable is an ordinary Lua table -that defines the behavior of the original value -under certain special operations. -You can change several aspects of the behavior -of operations over a value by setting specific fields in its metatable. -For instance, when a non-numeric value is the operand of an addition, -Lua checks for a function in the field "__add" of the value's metatable. -If it finds one, -Lua calls this function to perform the addition. - - -

-The keys in a metatable are derived from the event names; -the corresponding values are called metamethods. -In the previous example, the event is "add" -and the metamethod is the function that performs the addition. - - -

-You can query the metatable of any value -using the getmetatable function. - - -

-You can replace the metatable of tables -using the setmetatable function. -You cannot change the metatable of other types from Lua -(except by using the debug library); -you must use the C API for that. - - -

-Tables and full userdata have individual metatables -(although multiple tables and userdata can share their metatables). -Values of all other types share one single metatable per type; -that is, there is one single metatable for all numbers, -one for all strings, etc. -By default, a value has no metatable, -but the string library sets a metatable for the string type (see §6.4). - - -

-A metatable controls how an object behaves in arithmetic operations, -order comparisons, concatenation, length operation, and indexing. -A metatable also can define a function to be called -when a userdata or a table is garbage collected. -When Lua performs one of these operations over a value, -it checks whether this value has a metatable with the corresponding event. -If so, the value associated with that key (the metamethod) -controls how Lua will perform the operation. - - -

-Metatables control the operations listed next. -Each operation is identified by its corresponding name. -The key for each operation is a string with its name prefixed by -two underscores, '__'; -for instance, the key for operation "add" is the -string "__add". - - -

-The semantics of these operations is better explained by a Lua function -describing how the interpreter executes the operation. -The code shown here in Lua is only illustrative; -the real behavior is hard coded in the interpreter -and it is much more efficient than this simulation. -All functions used in these descriptions -(rawget, tonumber, etc.) -are described in §6.1. -In particular, to retrieve the metamethod of a given object, -we use the expression - -

-     metatable(obj)[event]
-

-This should be read as - -

-     rawget(getmetatable(obj) or {}, event)
-

-This means that the access to a metamethod does not invoke other metamethods, -and access to objects with no metatables does not fail -(it simply results in nil). - - -

-For the unary - and # operators, -the metamethod is called with a dummy second argument. -This extra argument is only to simplify Lua's internals; -it may be removed in future versions and therefore it is not present -in the following code. -(For most uses this extra argument is irrelevant.) - - - -

    - -
  • "add": -the + operation. - - - -

    -The function getbinhandler below defines how Lua chooses a handler -for a binary operation. -First, Lua tries the first operand. -If its type does not define a handler for the operation, -then Lua tries the second operand. - -

    -     function getbinhandler (op1, op2, event)
    -       return metatable(op1)[event] or metatable(op2)[event]
    -     end
    -

    -By using this function, -the behavior of the op1 + op2 is - -

    -     function add_event (op1, op2)
    -       local o1, o2 = tonumber(op1), tonumber(op2)
    -       if o1 and o2 then  -- both operands are numeric?
    -         return o1 + o2   -- '+' here is the primitive 'add'
    -       else  -- at least one of the operands is not numeric
    -         local h = getbinhandler(op1, op2, "__add")
    -         if h then
    -           -- call the handler with both operands
    -           return (h(op1, op2))
    -         else  -- no handler available: default behavior
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
  • "sub": -the - operation. - -Behavior similar to the "add" operation. -
  • - -
  • "mul": -the * operation. - -Behavior similar to the "add" operation. -
  • - -
  • "div": -the / operation. - -Behavior similar to the "add" operation. -
  • - -
  • "mod": -the % operation. - -Behavior similar to the "add" operation, -with the operation -o1 - floor(o1/o2)*o2 as the primitive operation. -
  • - -
  • "pow": -the ^ (exponentiation) operation. - -Behavior similar to the "add" operation, -with the function pow (from the C math library) -as the primitive operation. -
  • - -
  • "unm": -the unary - operation. - - -
    -     function unm_event (op)
    -       local o = tonumber(op)
    -       if o then  -- operand is numeric?
    -         return -o  -- '-' here is the primitive 'unm'
    -       else  -- the operand is not numeric.
    -         -- Try to get a handler from the operand
    -         local h = metatable(op).__unm
    -         if h then
    -           -- call the handler with the operand
    -           return (h(op))
    -         else  -- no handler available: default behavior
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
  • "concat": -the .. (concatenation) operation. - - -
    -     function concat_event (op1, op2)
    -       if (type(op1) == "string" or type(op1) == "number") and
    -          (type(op2) == "string" or type(op2) == "number") then
    -         return op1 .. op2  -- primitive string concatenation
    -       else
    -         local h = getbinhandler(op1, op2, "__concat")
    -         if h then
    -           return (h(op1, op2))
    -         else
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
  • "len": -the # operation. - - -
    -     function len_event (op)
    -       if type(op) == "string" then
    -         return strlen(op)      -- primitive string length
    -       else
    -         local h = metatable(op).__len
    -         if h then
    -           return (h(op))       -- call handler with the operand
    -         elseif type(op) == "table" then
    -           return #op              -- primitive table length
    -         else  -- no handler available: error
    -           error(···)
    -         end
    -       end
    -     end
    -

    -See §3.4.6 for a description of the length of a table. -

  • - -
  • "eq": -the == operation. - -The function getequalhandler defines how Lua chooses a metamethod -for equality. -A metamethod is selected only when both values -being compared have the same type -and the same metamethod for the selected operation, -and the values are either tables or full userdata. - -
    -     function getequalhandler (op1, op2)
    -       if type(op1) ~= type(op2) or
    -          (type(op1) ~= "table" and type(op1) ~= "userdata") then
    -         return nil     -- different values
    -       end
    -       local mm1 = metatable(op1).__eq
    -       local mm2 = metatable(op2).__eq
    -       if mm1 == mm2 then return mm1 else return nil end
    -     end
    -

    -The "eq" event is defined as follows: - -

    -     function eq_event (op1, op2)
    -       if op1 == op2 then   -- primitive equal?
    -         return true   -- values are equal
    -       end
    -       -- try metamethod
    -       local h = getequalhandler(op1, op2)
    -       if h then
    -         return not not h(op1, op2)
    -       else
    -         return false
    -       end
    -     end
    -

    -Note that the result is always a boolean. -

  • - -
  • "lt": -the < operation. - - -
    -     function lt_event (op1, op2)
    -       if type(op1) == "number" and type(op2) == "number" then
    -         return op1 < op2   -- numeric comparison
    -       elseif type(op1) == "string" and type(op2) == "string" then
    -         return op1 < op2   -- lexicographic comparison
    -       else
    -         local h = getbinhandler(op1, op2, "__lt")
    -         if h then
    -           return not not h(op1, op2)
    -         else
    -           error(···)
    -         end
    -       end
    -     end
    -

    -Note that the result is always a boolean. -

  • - -
  • "le": -the <= operation. - - -
    -     function le_event (op1, op2)
    -       if type(op1) == "number" and type(op2) == "number" then
    -         return op1 <= op2   -- numeric comparison
    -       elseif type(op1) == "string" and type(op2) == "string" then
    -         return op1 <= op2   -- lexicographic comparison
    -       else
    -         local h = getbinhandler(op1, op2, "__le")
    -         if h then
    -           return not not h(op1, op2)
    -         else
    -           h = getbinhandler(op1, op2, "__lt")
    -           if h then
    -             return not h(op2, op1)
    -           else
    -             error(···)
    -           end
    -         end
    -       end
    -     end
    -

    -Note that, in the absence of a "le" metamethod, -Lua tries the "lt", assuming that a <= b is -equivalent to not (b < a). - - -

    -As with the other comparison operators, -the result is always a boolean. -

  • - -
  • "index": -The indexing access table[key]. -Note that the metamethod is tried only -when key is not present in table. -(When table is not a table, -no key is ever present, -so the metamethod is always tried.) - - -
    -     function gettable_event (table, key)
    -       local h
    -       if type(table) == "table" then
    -         local v = rawget(table, key)
    -         -- if key is present, return raw value
    -         if v ~= nil then return v end
    -         h = metatable(table).__index
    -         if h == nil then return nil end
    -       else
    -         h = metatable(table).__index
    -         if h == nil then
    -           error(···)
    -         end
    -       end
    -       if type(h) == "function" then
    -         return (h(table, key))     -- call the handler
    -       else return h[key]           -- or repeat operation on it
    -       end
    -     end
    -

    -

  • - -
  • "newindex": -The indexing assignment table[key] = value. -Note that the metamethod is tried only -when key is not present in table. - - -
    -     function settable_event (table, key, value)
    -       local h
    -       if type(table) == "table" then
    -         local v = rawget(table, key)
    -         -- if key is present, do raw assignment
    -         if v ~= nil then rawset(table, key, value); return end
    -         h = metatable(table).__newindex
    -         if h == nil then rawset(table, key, value); return end
    -       else
    -         h = metatable(table).__newindex
    -         if h == nil then
    -           error(···)
    -         end
    -       end
    -       if type(h) == "function" then
    -         h(table, key,value)           -- call the handler
    -       else h[key] = value             -- or repeat operation on it
    -       end
    -     end
    -

    -

  • - -
  • "call": -called when Lua calls a value. - - -
    -     function function_event (func, ...)
    -       if type(func) == "function" then
    -         return func(...)   -- primitive call
    -       else
    -         local h = metatable(func).__call
    -         if h then
    -           return h(func, ...)
    -         else
    -           error(···)
    -         end
    -       end
    -     end
    -

    -

  • - -
- - - - -

2.5 – Garbage Collection

- -

-Lua performs automatic memory management. -This means that -you have to worry neither about allocating memory for new objects -nor about freeing it when the objects are no longer needed. -Lua manages memory automatically by running -a garbage collector to collect all dead objects -(that is, objects that are no longer accessible from Lua). -All memory used by Lua is subject to automatic management: -strings, tables, userdata, functions, threads, internal structures, etc. - - -

-Lua implements an incremental mark-and-sweep collector. -It uses two numbers to control its garbage-collection cycles: -the garbage-collector pause and -the garbage-collector step multiplier. -Both use percentage points as units -(e.g., a value of 100 means an internal value of 1). - - -

-The garbage-collector pause -controls how long the collector waits before starting a new cycle. -Larger values make the collector less aggressive. -Values smaller than 100 mean the collector will not wait to -start a new cycle. -A value of 200 means that the collector waits for the total memory in use -to double before starting a new cycle. - - -

-The garbage-collector step multiplier -controls the relative speed of the collector relative to -memory allocation. -Larger values make the collector more aggressive but also increase -the size of each incremental step. -Values smaller than 100 make the collector too slow and -can result in the collector never finishing a cycle. -The default is 200, -which means that the collector runs at "twice" -the speed of memory allocation. - - -

-If you set the step multiplier to a very large number -(larger than 10% of the maximum number of -bytes that the program may use), -the collector behaves like a stop-the-world collector. -If you then set the pause to 200, -the collector behaves as in old Lua versions, -doing a complete collection every time Lua doubles its -memory usage. - - -

-You can change these numbers by calling lua_gc in C -or collectgarbage in Lua. -You can also use these functions to control -the collector directly (e.g., stop and restart it). - - -

-As an experimental feature in Lua 5.2, -you can change the collector's operation mode -from incremental to generational. -A generational collector assumes that most objects die young, -and therefore it traverses only young (recently created) objects. -This behavior can reduce the time used by the collector, -but also increases memory usage (as old dead objects may accumulate). -To mitigate this second problem, -from time to time the generational collector performs a full collection. -Remember that this is an experimental feature; -you are welcome to try it, -but check your gains. - - - -

2.5.1 – Garbage-Collection Metamethods

- -

-You can set garbage-collector metamethods for tables -and, using the C API, -for full userdata (see §2.4). -These metamethods are also called finalizers. -Finalizers allow you to coordinate Lua's garbage collection -with external resource management -(such as closing files, network or database connections, -or freeing your own memory). - - -

-For an object (table or userdata) to be finalized when collected, -you must mark it for finalization. - -You mark an object for finalization when you set its metatable -and the metatable has a field indexed by the string "__gc". -Note that if you set a metatable without a __gc field -and later create that field in the metatable, -the object will not be marked for finalization. -However, after an object is marked, -you can freely change the __gc field of its metatable. - - -

-When a marked object becomes garbage, -it is not collected immediately by the garbage collector. -Instead, Lua puts it in a list. -After the collection, -Lua does the equivalent of the following function -for each object in that list: - -

-     function gc_event (obj)
-       local h = metatable(obj).__gc
-       if type(h) == "function" then
-         h(obj)
-       end
-     end
-
- -

-At the end of each garbage-collection cycle, -the finalizers for objects are called in -the reverse order that they were marked for collection, -among those collected in that cycle; -that is, the first finalizer to be called is the one associated -with the object marked last in the program. -The execution of each finalizer may occur at any point during -the execution of the regular code. - - -

-Because the object being collected must still be used by the finalizer, -it (and other objects accessible only through it) -must be resurrected by Lua. -Usually, this resurrection is transient, -and the object memory is freed in the next garbage-collection cycle. -However, if the finalizer stores the object in some global place -(e.g., a global variable), -then there is a permanent resurrection. -In any case, -the object memory is freed only when it becomes completely inaccessible; -its finalizer will never be called twice. - - -

-When you close a state (see lua_close), -Lua calls the finalizers of all objects marked for finalization, -following the reverse order that they were marked. -If any finalizer marks new objects for collection during that phase, -these new objects will not be finalized. - - - - - -

2.5.2 – Weak Tables

- -

-A weak table is a table whose elements are -weak references. -A weak reference is ignored by the garbage collector. -In other words, -if the only references to an object are weak references, -then the garbage collector will collect that object. - - -

-A weak table can have weak keys, weak values, or both. -A table with weak keys allows the collection of its keys, -but prevents the collection of its values. -A table with both weak keys and weak values allows the collection of -both keys and values. -In any case, if either the key or the value is collected, -the whole pair is removed from the table. -The weakness of a table is controlled by the -__mode field of its metatable. -If the __mode field is a string containing the character 'k', -the keys in the table are weak. -If __mode contains 'v', -the values in the table are weak. - - -

-A table with weak keys and strong values -is also called an ephemeron table. -In an ephemeron table, -a value is considered reachable only if its key is reachable. -In particular, -if the only reference to a key comes through its value, -the pair is removed. - - -

-Any change in the weakness of a table may take effect only -at the next collect cycle. -In particular, if you change the weakness to a stronger mode, -Lua may still collect some items from that table -before the change takes effect. - - -

-Only objects that have an explicit construction -are removed from weak tables. -Values, such as numbers and light C functions, -are not subject to garbage collection, -and therefore are not removed from weak tables -(unless its associated value is collected). -Although strings are subject to garbage collection, -they do not have an explicit construction, -and therefore are not removed from weak tables. - - -

-Resurrected objects -(that is, objects being finalized -and objects accessible only through objects being finalized) -have a special behavior in weak tables. -They are removed from weak values before running their finalizers, -but are removed from weak keys only in the next collection -after running their finalizers, when such objects are actually freed. -This behavior allows the finalizer to access properties -associated with the object through weak tables. - - -

-If a weak table is among the resurrected objects in a collection cycle, -it may not be properly cleared until the next cycle. - - - - - - - -

2.6 – Coroutines

- -

-Lua supports coroutines, -also called collaborative multithreading. -A coroutine in Lua represents an independent thread of execution. -Unlike threads in multithread systems, however, -a coroutine only suspends its execution by explicitly calling -a yield function. - - -

-You create a coroutine by calling coroutine.create. -Its sole argument is a function -that is the main function of the coroutine. -The create function only creates a new coroutine and -returns a handle to it (an object of type thread); -it does not start the coroutine. - - -

-You execute a coroutine by calling coroutine.resume. -When you first call coroutine.resume, -passing as its first argument -a thread returned by coroutine.create, -the coroutine starts its execution, -at the first line of its main function. -Extra arguments passed to coroutine.resume are passed on -to the coroutine main function. -After the coroutine starts running, -it runs until it terminates or yields. - - -

-A coroutine can terminate its execution in two ways: -normally, when its main function returns -(explicitly or implicitly, after the last instruction); -and abnormally, if there is an unprotected error. -In the first case, coroutine.resume returns true, -plus any values returned by the coroutine main function. -In case of errors, coroutine.resume returns false -plus an error message. - - -

-A coroutine yields by calling coroutine.yield. -When a coroutine yields, -the corresponding coroutine.resume returns immediately, -even if the yield happens inside nested function calls -(that is, not in the main function, -but in a function directly or indirectly called by the main function). -In the case of a yield, coroutine.resume also returns true, -plus any values passed to coroutine.yield. -The next time you resume the same coroutine, -it continues its execution from the point where it yielded, -with the call to coroutine.yield returning any extra -arguments passed to coroutine.resume. - - -

-Like coroutine.create, -the coroutine.wrap function also creates a coroutine, -but instead of returning the coroutine itself, -it returns a function that, when called, resumes the coroutine. -Any arguments passed to this function -go as extra arguments to coroutine.resume. -coroutine.wrap returns all the values returned by coroutine.resume, -except the first one (the boolean error code). -Unlike coroutine.resume, -coroutine.wrap does not catch errors; -any error is propagated to the caller. - - -

-As an example of how coroutines work, -consider the following code: - -

-     function foo (a)
-       print("foo", a)
-       return coroutine.yield(2*a)
-     end
-     
-     co = coroutine.create(function (a,b)
-           print("co-body", a, b)
-           local r = foo(a+1)
-           print("co-body", r)
-           local r, s = coroutine.yield(a+b, a-b)
-           print("co-body", r, s)
-           return b, "end"
-     end)
-     
-     print("main", coroutine.resume(co, 1, 10))
-     print("main", coroutine.resume(co, "r"))
-     print("main", coroutine.resume(co, "x", "y"))
-     print("main", coroutine.resume(co, "x", "y"))
-

-When you run it, it produces the following output: - -

-     co-body 1       10
-     foo     2
-     main    true    4
-     co-body r
-     main    true    11      -9
-     co-body x       y
-     main    true    10      end
-     main    false   cannot resume dead coroutine
-
- -

-You can also create and manipulate coroutines through the C API: -see functions lua_newthread, lua_resume, -and lua_yield. - - - - - -

3 – The Language

- -

-This section describes the lexis, the syntax, and the semantics of Lua. -In other words, -this section describes -which tokens are valid, -how they can be combined, -and what their combinations mean. - - -

-Language constructs will be explained using the usual extended BNF notation, -in which -{a} means 0 or more a's, and -[a] means an optional a. -Non-terminals are shown like non-terminal, -keywords are shown like kword, -and other terminal symbols are shown like ‘=’. -The complete syntax of Lua can be found in §9 -at the end of this manual. - - - -

3.1 – Lexical Conventions

- -

-Lua is a free-form language. -It ignores spaces (including new lines) and comments -between lexical elements (tokens), -except as delimiters between names and keywords. - - -

-Names -(also called identifiers) -in Lua can be any string of letters, -digits, and underscores, -not beginning with a digit. -Identifiers are used to name variables, table fields, and labels. - - -

-The following keywords are reserved -and cannot be used as names: - - -

-     and       break     do        else      elseif    end
-     false     for       function  goto      if        in
-     local     nil       not       or        repeat    return
-     then      true      until     while
-
- -

-Lua is a case-sensitive language: -and is a reserved word, but And and AND -are two different, valid names. -As a convention, names starting with an underscore followed by -uppercase letters (such as _VERSION) -are reserved for variables used by Lua. - - -

-The following strings denote other tokens: - -

-     +     -     *     /     %     ^     #
-     ==    ~=    <=    >=    <     >     =
-     (     )     {     }     [     ]     ::
-     ;     :     ,     .     ..    ...
-
- -

-Literal strings -can be delimited by matching single or double quotes, -and can contain the following C-like escape sequences: -'\a' (bell), -'\b' (backspace), -'\f' (form feed), -'\n' (newline), -'\r' (carriage return), -'\t' (horizontal tab), -'\v' (vertical tab), -'\\' (backslash), -'\"' (quotation mark [double quote]), -and '\'' (apostrophe [single quote]). -A backslash followed by a real newline -results in a newline in the string. -The escape sequence '\z' skips the following span -of white-space characters, -including line breaks; -it is particularly useful to break and indent a long literal string -into multiple lines without adding the newlines and spaces -into the string contents. - - -

-A byte in a literal string can also be specified by its numerical value. -This can be done with the escape sequence \xXX, -where XX is a sequence of exactly two hexadecimal digits, -or with the escape sequence \ddd, -where ddd is a sequence of up to three decimal digits. -(Note that if a decimal escape is to be followed by a digit, -it must be expressed using exactly three digits.) -Strings in Lua can contain any 8-bit value, including embedded zeros, -which can be specified as '\0'. - - -

-Literal strings can also be defined using a long format -enclosed by long brackets. -We define an opening long bracket of level n as an opening -square bracket followed by n equal signs followed by another -opening square bracket. -So, an opening long bracket of level 0 is written as [[, -an opening long bracket of level 1 is written as [=[, -and so on. -A closing long bracket is defined similarly; -for instance, a closing long bracket of level 4 is written as ]====]. -A long literal starts with an opening long bracket of any level and -ends at the first closing long bracket of the same level. -It can contain any text except a closing bracket of the proper level. -Literals in this bracketed form can run for several lines, -do not interpret any escape sequences, -and ignore long brackets of any other level. -Any kind of end-of-line sequence -(carriage return, newline, carriage return followed by newline, -or newline followed by carriage return) -is converted to a simple newline. - - -

-Any byte in a literal string not -explicitly affected by the previous rules represents itself. -However, Lua opens files for parsing in text mode, -and the system file functions may have problems with -some control characters. -So, it is safer to represent -non-text data as a quoted literal with -explicit escape sequences for non-text characters. - - -

-For convenience, -when the opening long bracket is immediately followed by a newline, -the newline is not included in the string. -As an example, in a system using ASCII -(in which 'a' is coded as 97, -newline is coded as 10, and '1' is coded as 49), -the five literal strings below denote the same string: - -

-     a = 'alo\n123"'
-     a = "alo\n123\""
-     a = '\97lo\10\04923"'
-     a = [[alo
-     123"]]
-     a = [==[
-     alo
-     123"]==]
-
- -

-A numerical constant can be written with an optional fractional part -and an optional decimal exponent, -marked by a letter 'e' or 'E'. -Lua also accepts hexadecimal constants, -which start with 0x or 0X. -Hexadecimal constants also accept an optional fractional part -plus an optional binary exponent, -marked by a letter 'p' or 'P'. -Examples of valid numerical constants are - -

-     3     3.0     3.1416     314.16e-2     0.31416E1
-     0xff  0x0.1E  0xA23p-4   0X1.921FB54442D18P+1
-
- -

-A comment starts with a double hyphen (--) -anywhere outside a string. -If the text immediately after -- is not an opening long bracket, -the comment is a short comment, -which runs until the end of the line. -Otherwise, it is a long comment, -which runs until the corresponding closing long bracket. -Long comments are frequently used to disable code temporarily. - - - - - -

3.2 – Variables

- -

-Variables are places that store values. -There are three kinds of variables in Lua: -global variables, local variables, and table fields. - - -

-A single name can denote a global variable or a local variable -(or a function's formal parameter, -which is a particular kind of local variable): - -

-	var ::= Name
-

-Name denotes identifiers, as defined in §3.1. - - -

-Any variable name is assumed to be global unless explicitly declared -as a local (see §3.3.7). -Local variables are lexically scoped: -local variables can be freely accessed by functions -defined inside their scope (see §3.5). - - -

-Before the first assignment to a variable, its value is nil. - - -

-Square brackets are used to index a table: - -

-	var ::= prefixexp ‘[’ exp ‘]’
-

-The meaning of accesses to table fields can be changed via metatables. -An access to an indexed variable t[i] is equivalent to -a call gettable_event(t,i). -(See §2.4 for a complete description of the -gettable_event function. -This function is not defined or callable in Lua. -We use it here only for explanatory purposes.) - - -

-The syntax var.Name is just syntactic sugar for -var["Name"]: - -

-	var ::= prefixexp ‘.’ Name
-
- -

-An access to a global variable x -is equivalent to _ENV.x. -Due to the way that chunks are compiled, -_ENV is never a global name (see §2.2). - - - - - -

3.3 – Statements

- -

-Lua supports an almost conventional set of statements, -similar to those in Pascal or C. -This set includes -assignments, control structures, function calls, -and variable declarations. - - - -

3.3.1 – Blocks

- -

-A block is a list of statements, -which are executed sequentially: - -

-	block ::= {stat}
-

-Lua has empty statements -that allow you to separate statements with semicolons, -start a block with a semicolon -or write two semicolons in sequence: - -

-	stat ::= ‘;’
-
- -

-Function calls and assignments -can start with an open parenthesis. -This possibility leads to an ambiguity in Lua's grammar. -Consider the following fragment: - -

-     a = b + c
-     (print or io.write)('done')
-

-The grammar could see it in two ways: - -

-     a = b + c(print or io.write)('done')
-     
-     a = b + c; (print or io.write)('done')
-

-The current parser always sees such constructions -in the first way, -interpreting the open parenthesis -as the start of the arguments to a call. -To avoid this ambiguity, -it is a good practice to always precede with a semicolon -statements that start with a parenthesis: - -

-     ;(print or io.write)('done')
-
- -

-A block can be explicitly delimited to produce a single statement: - -

-	stat ::= do block end
-

-Explicit blocks are useful -to control the scope of variable declarations. -Explicit blocks are also sometimes used to -add a return statement in the middle -of another block (see §3.3.4). - - - - - -

3.3.2 – Chunks

- -

-The unit of compilation of Lua is called a chunk. -Syntactically, -a chunk is simply a block: - -

-	chunk ::= block
-
- -

-Lua handles a chunk as the body of an anonymous function -with a variable number of arguments -(see §3.4.10). -As such, chunks can define local variables, -receive arguments, and return values. -Moreover, such anonymous function is compiled as in the -scope of an external local variable called _ENV (see §2.2). -The resulting function always has _ENV as its only upvalue, -even if it does not use that variable. - - -

-A chunk can be stored in a file or in a string inside the host program. -To execute a chunk, -Lua first precompiles the chunk into instructions for a virtual machine, -and then it executes the compiled code -with an interpreter for the virtual machine. - - -

-Chunks can also be precompiled into binary form; -see program luac for details. -Programs in source and compiled forms are interchangeable; -Lua automatically detects the file type and acts accordingly. - - - - - - -

3.3.3 – Assignment

- -

-Lua allows multiple assignments. -Therefore, the syntax for assignment -defines a list of variables on the left side -and a list of expressions on the right side. -The elements in both lists are separated by commas: - -

-	stat ::= varlist ‘=’ explist
-	varlist ::= var {‘,’ var}
-	explist ::= exp {‘,’ exp}
-

-Expressions are discussed in §3.4. - - -

-Before the assignment, -the list of values is adjusted to the length of -the list of variables. -If there are more values than needed, -the excess values are thrown away. -If there are fewer values than needed, -the list is extended with as many nil's as needed. -If the list of expressions ends with a function call, -then all values returned by that call enter the list of values, -before the adjustment -(except when the call is enclosed in parentheses; see §3.4). - - -

-The assignment statement first evaluates all its expressions -and only then are the assignments performed. -Thus the code - -

-     i = 3
-     i, a[i] = i+1, 20
-

-sets a[3] to 20, without affecting a[4] -because the i in a[i] is evaluated (to 3) -before it is assigned 4. -Similarly, the line - -

-     x, y = y, x
-

-exchanges the values of x and y, -and - -

-     x, y, z = y, z, x
-

-cyclically permutes the values of x, y, and z. - - -

-The meaning of assignments to global variables -and table fields can be changed via metatables. -An assignment to an indexed variable t[i] = val is equivalent to -settable_event(t,i,val). -(See §2.4 for a complete description of the -settable_event function. -This function is not defined or callable in Lua. -We use it here only for explanatory purposes.) - - -

-An assignment to a global variable x = val -is equivalent to the assignment -_ENV.x = val (see §2.2). - - - - - -

3.3.4 – Control Structures

-The control structures -if, while, and repeat have the usual meaning and -familiar syntax: - - - - -

-	stat ::= while exp do block end
-	stat ::= repeat block until exp
-	stat ::= if exp then block {elseif exp then block} [else block] end
-

-Lua also has a for statement, in two flavors (see §3.3.5). - - -

-The condition expression of a -control structure can return any value. -Both false and nil are considered false. -All values different from nil and false are considered true -(in particular, the number 0 and the empty string are also true). - - -

-In the repeatuntil loop, -the inner block does not end at the until keyword, -but only after the condition. -So, the condition can refer to local variables -declared inside the loop block. - - -

-The goto statement transfers the program control to a label. -For syntactical reasons, -labels in Lua are considered statements too: - - - -

-	stat ::= goto Name
-	stat ::= label
-	label ::= ‘::’ Name ‘::’
-
- -

-A label is visible in the entire block where it is defined, -except -inside nested blocks where a label with the same name is defined and -inside nested functions. -A goto may jump to any visible label as long as it does not -enter into the scope of a local variable. - - -

-Labels and empty statements are called void statements, -as they perform no actions. - - -

-The break statement terminates the execution of a -while, repeat, or for loop, -skipping to the next statement after the loop: - - -

-	stat ::= break
-

-A break ends the innermost enclosing loop. - - -

-The return statement is used to return values -from a function or a chunk (which is a function in disguise). - -Functions can return more than one value, -so the syntax for the return statement is - -

-	stat ::= return [explist] [‘;’]
-
- -

-The return statement can only be written -as the last statement of a block. -If it is really necessary to return in the middle of a block, -then an explicit inner block can be used, -as in the idiom do return end, -because now return is the last statement in its (inner) block. - - - - - -

3.3.5 – For Statement

- -

- -The for statement has two forms: -one numeric and one generic. - - -

-The numeric for loop repeats a block of code while a -control variable runs through an arithmetic progression. -It has the following syntax: - -

-	stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end
-

-The block is repeated for name starting at the value of -the first exp, until it passes the second exp by steps of the -third exp. -More precisely, a for statement like - -

-     for v = e1, e2, e3 do block end
-

-is equivalent to the code: - -

-     do
-       local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
-       if not (var and limit and step) then error() end
-       while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
-         local v = var
-         block
-         var = var + step
-       end
-     end
-

-Note the following: - -

    - -
  • -All three control expressions are evaluated only once, -before the loop starts. -They must all result in numbers. -
  • - -
  • -var, limit, and step are invisible variables. -The names shown here are for explanatory purposes only. -
  • - -
  • -If the third expression (the step) is absent, -then a step of 1 is used. -
  • - -
  • -You can use break to exit a for loop. -
  • - -
  • -The loop variable v is local to the loop; -you cannot use its value after the for ends or is broken. -If you need this value, -assign it to another variable before breaking or exiting the loop. -
  • - -
- -

-The generic for statement works over functions, -called iterators. -On each iteration, the iterator function is called to produce a new value, -stopping when this new value is nil. -The generic for loop has the following syntax: - -

-	stat ::= for namelist in explist do block end
-	namelist ::= Name {‘,’ Name}
-

-A for statement like - -

-     for var_1, ···, var_n in explist do block end
-

-is equivalent to the code: - -

-     do
-       local f, s, var = explist
-       while true do
-         local var_1, ···, var_n = f(s, var)
-         if var_1 == nil then break end
-         var = var_1
-         block
-       end
-     end
-

-Note the following: - -

    - -
  • -explist is evaluated only once. -Its results are an iterator function, -a state, -and an initial value for the first iterator variable. -
  • - -
  • -f, s, and var are invisible variables. -The names are here for explanatory purposes only. -
  • - -
  • -You can use break to exit a for loop. -
  • - -
  • -The loop variables var_i are local to the loop; -you cannot use their values after the for ends. -If you need these values, -then assign them to other variables before breaking or exiting the loop. -
  • - -
- - - - -

3.3.6 – Function Calls as Statements

-To allow possible side-effects, -function calls can be executed as statements: - -

-	stat ::= functioncall
-

-In this case, all returned values are thrown away. -Function calls are explained in §3.4.9. - - - - - -

3.3.7 – Local Declarations

-Local variables can be declared anywhere inside a block. -The declaration can include an initial assignment: - -

-	stat ::= local namelist [‘=’ explist]
-

-If present, an initial assignment has the same semantics -of a multiple assignment (see §3.3.3). -Otherwise, all variables are initialized with nil. - - -

-A chunk is also a block (see §3.3.2), -and so local variables can be declared in a chunk outside any explicit block. - - -

-The visibility rules for local variables are explained in §3.5. - - - - - - - -

3.4 – Expressions

- -

-The basic expressions in Lua are the following: - -

-	exp ::= prefixexp
-	exp ::= nil | false | true
-	exp ::= Number
-	exp ::= String
-	exp ::= functiondef
-	exp ::= tableconstructor
-	exp ::= ‘...’
-	exp ::= exp binop exp
-	exp ::= unop exp
-	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
-
- -

-Numbers and literal strings are explained in §3.1; -variables are explained in §3.2; -function definitions are explained in §3.4.10; -function calls are explained in §3.4.9; -table constructors are explained in §3.4.8. -Vararg expressions, -denoted by three dots ('...'), can only be used when -directly inside a vararg function; -they are explained in §3.4.10. - - -

-Binary operators comprise arithmetic operators (see §3.4.1), -relational operators (see §3.4.3), logical operators (see §3.4.4), -and the concatenation operator (see §3.4.5). -Unary operators comprise the unary minus (see §3.4.1), -the unary not (see §3.4.4), -and the unary length operator (see §3.4.6). - - -

-Both function calls and vararg expressions can result in multiple values. -If a function call is used as a statement (see §3.3.6), -then its return list is adjusted to zero elements, -thus discarding all returned values. -If an expression is used as the last (or the only) element -of a list of expressions, -then no adjustment is made -(unless the expression is enclosed in parentheses). -In all other contexts, -Lua adjusts the result list to one element, -either discarding all values except the first one -or adding a single nil if there are no values. - - -

-Here are some examples: - -

-     f()                -- adjusted to 0 results
-     g(f(), x)          -- f() is adjusted to 1 result
-     g(x, f())          -- g gets x plus all results from f()
-     a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)
-     a,b = ...          -- a gets the first vararg parameter, b gets
-                        -- the second (both a and b can get nil if there
-                        -- is no corresponding vararg parameter)
-     
-     a,b,c = x, f()     -- f() is adjusted to 2 results
-     a,b,c = f()        -- f() is adjusted to 3 results
-     return f()         -- returns all results from f()
-     return ...         -- returns all received vararg parameters
-     return x,y,f()     -- returns x, y, and all results from f()
-     {f()}              -- creates a list with all results from f()
-     {...}              -- creates a list with all vararg parameters
-     {f(), nil}         -- f() is adjusted to 1 result
-
- -

-Any expression enclosed in parentheses always results in only one value. -Thus, -(f(x,y,z)) is always a single value, -even if f returns several values. -(The value of (f(x,y,z)) is the first value returned by f -or nil if f does not return any values.) - - - -

3.4.1 – Arithmetic Operators

-Lua supports the usual arithmetic operators: -the binary + (addition), -- (subtraction), * (multiplication), -/ (division), % (modulo), and ^ (exponentiation); -and unary - (mathematical negation). -If the operands are numbers, or strings that can be converted to -numbers (see §3.4.2), -then all operations have the usual meaning. -Exponentiation works for any exponent. -For instance, x^(-0.5) computes the inverse of the square root of x. -Modulo is defined as - -

-     a % b == a - math.floor(a/b)*b
-

-That is, it is the remainder of a division that rounds -the quotient towards minus infinity. - - - - - -

3.4.2 – Coercion

- -

-Lua provides automatic conversion between -string and number values at run time. -Any arithmetic operation applied to a string tries to convert -this string to a number, following the rules of the Lua lexer. -(The string may have leading and trailing spaces and a sign.) -Conversely, whenever a number is used where a string is expected, -the number is converted to a string, in a reasonable format. -For complete control over how numbers are converted to strings, -use the format function from the string library -(see string.format). - - - - - -

3.4.3 – Relational Operators

-The relational operators in Lua are - -

-     ==    ~=    <     >     <=    >=
-

-These operators always result in false or true. - - -

-Equality (==) first compares the type of its operands. -If the types are different, then the result is false. -Otherwise, the values of the operands are compared. -Numbers and strings are compared in the usual way. -Tables, userdata, and threads -are compared by reference: -two objects are considered equal only if they are the same object. -Every time you create a new object -(a table, userdata, or thread), -this new object is different from any previously existing object. -Closures with the same reference are always equal. -Closures with any detectable difference -(different behavior, different definition) are always different. - - -

-You can change the way that Lua compares tables and userdata -by using the "eq" metamethod (see §2.4). - - -

-The conversion rules of §3.4.2 -do not apply to equality comparisons. -Thus, "0"==0 evaluates to false, -and t[0] and t["0"] denote different -entries in a table. - - -

-The operator ~= is exactly the negation of equality (==). - - -

-The order operators work as follows. -If both arguments are numbers, then they are compared as such. -Otherwise, if both arguments are strings, -then their values are compared according to the current locale. -Otherwise, Lua tries to call the "lt" or the "le" -metamethod (see §2.4). -A comparison a > b is translated to b < a -and a >= b is translated to b <= a. - - - - - -

3.4.4 – Logical Operators

-The logical operators in Lua are -and, or, and not. -Like the control structures (see §3.3.4), -all logical operators consider both false and nil as false -and anything else as true. - - -

-The negation operator not always returns false or true. -The conjunction operator and returns its first argument -if this value is false or nil; -otherwise, and returns its second argument. -The disjunction operator or returns its first argument -if this value is different from nil and false; -otherwise, or returns its second argument. -Both and and or use short-cut evaluation; -that is, -the second operand is evaluated only if necessary. -Here are some examples: - -

-     10 or 20            --> 10
-     10 or error()       --> 10
-     nil or "a"          --> "a"
-     nil and 10          --> nil
-     false and error()   --> false
-     false and nil       --> false
-     false or nil        --> nil
-     10 and 20           --> 20
-

-(In this manual, ---> indicates the result of the preceding expression.) - - - - - -

3.4.5 – Concatenation

-The string concatenation operator in Lua is -denoted by two dots ('..'). -If both operands are strings or numbers, then they are converted to -strings according to the rules mentioned in §3.4.2. -Otherwise, the __concat metamethod is called (see §2.4). - - - - - -

3.4.6 – The Length Operator

- -

-The length operator is denoted by the unary prefix operator #. -The length of a string is its number of bytes -(that is, the usual meaning of string length when each -character is one byte). - - -

-A program can modify the behavior of the length operator for -any value but strings through the __len metamethod (see §2.4). - - -

-Unless a __len metamethod is given, -the length of a table t is only defined if the -table is a sequence, -that is, -the set of its positive numeric keys is equal to {1..n} -for some non-negative integer n. -In that case, n is its length. -Note that a table like - -

-     {10, 20, nil, 40}
-

-is not a sequence, because it has the key 4 -but does not have the key 3. -(So, there is no n such that the set {1..n} is equal -to the set of positive numeric keys of that table.) -Note, however, that non-numeric keys do not interfere -with whether a table is a sequence. - - - - - -

3.4.7 – Precedence

-Operator precedence in Lua follows the table below, -from lower to higher priority: - -

-     or
-     and
-     <     >     <=    >=    ~=    ==
-     ..
-     +     -
-     *     /     %
-     not   #     - (unary)
-     ^
-

-As usual, -you can use parentheses to change the precedences of an expression. -The concatenation ('..') and exponentiation ('^') -operators are right associative. -All other binary operators are left associative. - - - - - -

3.4.8 – Table Constructors

-Table constructors are expressions that create tables. -Every time a constructor is evaluated, a new table is created. -A constructor can be used to create an empty table -or to create a table and initialize some of its fields. -The general syntax for constructors is - -

-	tableconstructor ::= ‘{’ [fieldlist] ‘}’
-	fieldlist ::= field {fieldsep field} [fieldsep]
-	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
-	fieldsep ::= ‘,’ | ‘;’
-
- -

-Each field of the form [exp1] = exp2 adds to the new table an entry -with key exp1 and value exp2. -A field of the form name = exp is equivalent to -["name"] = exp. -Finally, fields of the form exp are equivalent to -[i] = exp, where i are consecutive numerical integers, -starting with 1. -Fields in the other formats do not affect this counting. -For example, - -

-     a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
-

-is equivalent to - -

-     do
-       local t = {}
-       t[f(1)] = g
-       t[1] = "x"         -- 1st exp
-       t[2] = "y"         -- 2nd exp
-       t.x = 1            -- t["x"] = 1
-       t[3] = f(x)        -- 3rd exp
-       t[30] = 23
-       t[4] = 45          -- 4th exp
-       a = t
-     end
-
- -

-If the last field in the list has the form exp -and the expression is a function call or a vararg expression, -then all values returned by this expression enter the list consecutively -(see §3.4.9). - - -

-The field list can have an optional trailing separator, -as a convenience for machine-generated code. - - - - - -

3.4.9 – Function Calls

-A function call in Lua has the following syntax: - -

-	functioncall ::= prefixexp args
-

-In a function call, -first prefixexp and args are evaluated. -If the value of prefixexp has type function, -then this function is called -with the given arguments. -Otherwise, the prefixexp "call" metamethod is called, -having as first parameter the value of prefixexp, -followed by the original call arguments -(see §2.4). - - -

-The form - -

-	functioncall ::= prefixexp ‘:’ Name args
-

-can be used to call "methods". -A call v:name(args) -is syntactic sugar for v.name(v,args), -except that v is evaluated only once. - - -

-Arguments have the following syntax: - -

-	args ::= ‘(’ [explist] ‘)’
-	args ::= tableconstructor
-	args ::= String
-

-All argument expressions are evaluated before the call. -A call of the form f{fields} is -syntactic sugar for f({fields}); -that is, the argument list is a single new table. -A call of the form f'string' -(or f"string" or f[[string]]) -is syntactic sugar for f('string'); -that is, the argument list is a single literal string. - - -

-A call of the form return functioncall is called -a tail call. -Lua implements proper tail calls -(or proper tail recursion): -in a tail call, -the called function reuses the stack entry of the calling function. -Therefore, there is no limit on the number of nested tail calls that -a program can execute. -However, a tail call erases any debug information about the -calling function. -Note that a tail call only happens with a particular syntax, -where the return has one single function call as argument; -this syntax makes the calling function return exactly -the returns of the called function. -So, none of the following examples are tail calls: - -

-     return (f(x))        -- results adjusted to 1
-     return 2 * f(x)
-     return x, f(x)       -- additional results
-     f(x); return         -- results discarded
-     return x or f(x)     -- results adjusted to 1
-
- - - - -

3.4.10 – Function Definitions

- -

-The syntax for function definition is - -

-	functiondef ::= function funcbody
-	funcbody ::= ‘(’ [parlist] ‘)’ block end
-
- -

-The following syntactic sugar simplifies function definitions: - -

-	stat ::= function funcname funcbody
-	stat ::= local function Name funcbody
-	funcname ::= Name {‘.’ Name} [‘:’ Name]
-

-The statement - -

-     function f () body end
-

-translates to - -

-     f = function () body end
-

-The statement - -

-     function t.a.b.c.f () body end
-

-translates to - -

-     t.a.b.c.f = function () body end
-

-The statement - -

-     local function f () body end
-

-translates to - -

-     local f; f = function () body end
-

-not to - -

-     local f = function () body end
-

-(This only makes a difference when the body of the function -contains references to f.) - - -

-A function definition is an executable expression, -whose value has type function. -When Lua precompiles a chunk, -all its function bodies are precompiled too. -Then, whenever Lua executes the function definition, -the function is instantiated (or closed). -This function instance (or closure) -is the final value of the expression. - - -

-Parameters act as local variables that are -initialized with the argument values: - -

-	parlist ::= namelist [‘,’ ‘...’] | ‘...’
-

-When a function is called, -the list of arguments is adjusted to -the length of the list of parameters, -unless the function is a vararg function, -which is indicated by three dots ('...') -at the end of its parameter list. -A vararg function does not adjust its argument list; -instead, it collects all extra arguments and supplies them -to the function through a vararg expression, -which is also written as three dots. -The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results. -If a vararg expression is used inside another expression -or in the middle of a list of expressions, -then its return list is adjusted to one element. -If the expression is used as the last element of a list of expressions, -then no adjustment is made -(unless that last expression is enclosed in parentheses). - - -

-As an example, consider the following definitions: - -

-     function f(a, b) end
-     function g(a, b, ...) end
-     function r() return 1,2,3 end
-

-Then, we have the following mapping from arguments to parameters and -to the vararg expression: - -

-     CALL            PARAMETERS
-     
-     f(3)             a=3, b=nil
-     f(3, 4)          a=3, b=4
-     f(3, 4, 5)       a=3, b=4
-     f(r(), 10)       a=1, b=10
-     f(r())           a=1, b=2
-     
-     g(3)             a=3, b=nil, ... -->  (nothing)
-     g(3, 4)          a=3, b=4,   ... -->  (nothing)
-     g(3, 4, 5, 8)    a=3, b=4,   ... -->  5  8
-     g(5, r())        a=5, b=1,   ... -->  2  3
-
- -

-Results are returned using the return statement (see §3.3.4). -If control reaches the end of a function -without encountering a return statement, -then the function returns with no results. - - -

- -There is a system-dependent limit on the number of values -that a function may return. -This limit is guaranteed to be larger than 1000. - - -

-The colon syntax -is used for defining methods, -that is, functions that have an implicit extra parameter self. -Thus, the statement - -

-     function t.a.b.c:f (params) body end
-

-is syntactic sugar for - -

-     t.a.b.c.f = function (self, params) body end
-
- - - - - - -

3.5 – Visibility Rules

- -

- -Lua is a lexically scoped language. -The scope of a local variable begins at the first statement after -its declaration and lasts until the last non-void statement -of the innermost block that includes the declaration. -Consider the following example: - -

-     x = 10                -- global variable
-     do                    -- new block
-       local x = x         -- new 'x', with value 10
-       print(x)            --> 10
-       x = x+1
-       do                  -- another block
-         local x = x+1     -- another 'x'
-         print(x)          --> 12
-       end
-       print(x)            --> 11
-     end
-     print(x)              --> 10  (the global one)
-
- -

-Notice that, in a declaration like local x = x, -the new x being declared is not in scope yet, -and so the second x refers to the outside variable. - - -

-Because of the lexical scoping rules, -local variables can be freely accessed by functions -defined inside their scope. -A local variable used by an inner function is called -an upvalue, or external local variable, -inside the inner function. - - -

-Notice that each execution of a local statement -defines new local variables. -Consider the following example: - -

-     a = {}
-     local x = 20
-     for i=1,10 do
-       local y = 0
-       a[i] = function () y=y+1; return x+y end
-     end
-

-The loop creates ten closures -(that is, ten instances of the anonymous function). -Each of these closures uses a different y variable, -while all of them share the same x. - - - - - -

4 – The Application Program Interface

- -

- -This section describes the C API for Lua, that is, -the set of C functions available to the host program to communicate -with Lua. -All API functions and related types and constants -are declared in the header file lua.h. - - -

-Even when we use the term "function", -any facility in the API may be provided as a macro instead. -Except where stated otherwise, -all such macros use each of their arguments exactly once -(except for the first argument, which is always a Lua state), -and so do not generate any hidden side-effects. - - -

-As in most C libraries, -the Lua API functions do not check their arguments for validity or consistency. -However, you can change this behavior by compiling Lua -with the macro LUA_USE_APICHECK defined. - - - -

4.1 – The Stack

- -

-Lua uses a virtual stack to pass values to and from C. -Each element in this stack represents a Lua value -(nil, number, string, etc.). - - -

-Whenever Lua calls C, the called function gets a new stack, -which is independent of previous stacks and of stacks of -C functions that are still active. -This stack initially contains any arguments to the C function -and it is where the C function pushes its results -to be returned to the caller (see lua_CFunction). - - -

-For convenience, -most query operations in the API do not follow a strict stack discipline. -Instead, they can refer to any element in the stack -by using an index: -A positive index represents an absolute stack position -(starting at 1); -a negative index represents an offset relative to the top of the stack. -More specifically, if the stack has n elements, -then index 1 represents the first element -(that is, the element that was pushed onto the stack first) -and -index n represents the last element; -index -1 also represents the last element -(that is, the element at the top) -and index -n represents the first element. - - - - - -

4.2 – Stack Size

- -

-When you interact with the Lua API, -you are responsible for ensuring consistency. -In particular, -you are responsible for controlling stack overflow. -You can use the function lua_checkstack -to ensure that the stack has extra slots when pushing new elements. - - -

-Whenever Lua calls C, -it ensures that the stack has at least LUA_MINSTACK extra slots. -LUA_MINSTACK is defined as 20, -so that usually you do not have to worry about stack space -unless your code has loops pushing elements onto the stack. - - -

-When you call a Lua function -without a fixed number of results (see lua_call), -Lua ensures that the stack has enough size for all results, -but it does not ensure any extra space. -So, before pushing anything in the stack after such a call -you should use lua_checkstack. - - - - - -

4.3 – Valid and Acceptable Indices

- -

-Any function in the API that receives stack indices -works only with valid indices or acceptable indices. - - -

-A valid index is an index that refers to a -real position within the stack, that is, -its position lies between 1 and the stack top -(1 ≤ abs(index) ≤ top). - -Usually, functions that can modify the value at an index -require valid indices. - - -

-Unless otherwise noted, -any function that accepts valid indices also accepts pseudo-indices, -which represent some Lua values that are accessible to C code -but which are not in the stack. -Pseudo-indices are used to access the registry -and the upvalues of a C function (see §4.4). - - -

-Functions that do not need a specific stack position, -but only a value in the stack (e.g., query functions), -can be called with acceptable indices. -An acceptable index can be any valid index, -including the pseudo-indices, -but it also can be any positive index after the stack top -within the space allocated for the stack, -that is, indices up to the stack size. -(Note that 0 is never an acceptable index.) -Except when noted otherwise, -functions in the API work with acceptable indices. - - -

-Acceptable indices serve to avoid extra tests -against the stack top when querying the stack. -For instance, a C function can query its third argument -without the need to first check whether there is a third argument, -that is, without the need to check whether 3 is a valid index. - - -

-For functions that can be called with acceptable indices, -any non-valid index is treated as if it -contains a value of a virtual type LUA_TNONE, -which behaves like a nil value. - - - - - -

4.4 – C Closures

- -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure -(see lua_pushcclosure); -these values are called upvalues and are -accessible to the function whenever it is called. - - -

-Whenever a C function is called, -its upvalues are located at specific pseudo-indices. -These pseudo-indices are produced by the macro -lua_upvalueindex. -The first value associated with a function is at position -lua_upvalueindex(1), and so on. -Any access to lua_upvalueindex(n), -where n is greater than the number of upvalues of the -current function (but not greater than 256), -produces an acceptable but invalid index. - - - - - -

4.5 – Registry

- -

-Lua provides a registry, -a predefined table that can be used by any C code to -store whatever Lua values it needs to store. -The registry table is always located at pseudo-index -LUA_REGISTRYINDEX, -which is a valid index. -Any C library can store data into this table, -but it should take care to choose keys -that are different from those used -by other libraries, to avoid collisions. -Typically, you should use as key a string containing your library name, -or a light userdata with the address of a C object in your code, -or any Lua object created by your code. -As with global names, -string keys starting with an underscore followed by -uppercase letters are reserved for Lua. - - -

-The integer keys in the registry are used by the reference mechanism, -implemented by the auxiliary library, -and by some predefined values. -Therefore, integer keys should not be used for other purposes. - - -

-When you create a new Lua state, -its registry comes with some predefined values. -These predefined values are indexed with integer keys -defined as constants in lua.h. -The following constants are defined: - -

    -
  • LUA_RIDX_MAINTHREAD: At this index the registry has -the main thread of the state. -(The main thread is the one created together with the state.) -
  • - -
  • LUA_RIDX_GLOBALS: At this index the registry has -the global environment. -
  • -
- - - - -

4.6 – Error Handling in C

- -

-Internally, Lua uses the C longjmp facility to handle errors. -(You can also choose to use exceptions if you compile Lua as C++; -search for LUAI_THROW in the source code.) -When Lua faces any error -(such as a memory allocation error, type errors, syntax errors, -and runtime errors) -it raises an error; -that is, it does a long jump. -A protected environment uses setjmp -to set a recovery point; -any error jumps to the most recent active recovery point. - - -

-If an error happens outside any protected environment, -Lua calls a panic function (see lua_atpanic) -and then calls abort, -thus exiting the host application. -Your panic function can avoid this exit by -never returning -(e.g., doing a long jump to your own recovery point outside Lua). - - -

-The panic function runs as if it were a message handler (see §2.3); -in particular, the error message is at the top of the stack. -However, there is no guarantees about stack space. -To push anything on the stack, -the panic function should first check the available space (see §4.2). - - -

-Most functions in the API can throw an error, -for instance due to a memory allocation error. -The documentation for each function indicates whether -it can throw errors. - - -

-Inside a C function you can throw an error by calling lua_error. - - - - - -

4.7 – Handling Yields in C

- -

-Internally, Lua uses the C longjmp facility to yield a coroutine. -Therefore, if a function foo calls an API function -and this API function yields -(directly or indirectly by calling another function that yields), -Lua cannot return to foo any more, -because the longjmp removes its frame from the C stack. - - -

-To avoid this kind of problem, -Lua raises an error whenever it tries to yield across an API call, -except for three functions: -lua_yieldk, lua_callk, and lua_pcallk. -All those functions receive a continuation function -(as a parameter called k) to continue execution after a yield. - - -

-We need to set some terminology to explain continuations. -We have a C function called from Lua which we will call -the original function. -This original function then calls one of those three functions in the C API, -which we will call the callee function, -that then yields the current thread. -(This can happen when the callee function is lua_yieldk, -or when the callee function is either lua_callk or lua_pcallk -and the function called by them yields.) - - -

-Suppose the running thread yields while executing the callee function. -After the thread resumes, -it eventually will finish running the callee function. -However, -the callee function cannot return to the original function, -because its frame in the C stack was destroyed by the yield. -Instead, Lua calls a continuation function, -which was given as an argument to the callee function. -As the name implies, -the continuation function should continue the task -of the original function. - - -

-Lua treats the continuation function as if it were the original function. -The continuation function receives the same Lua stack -from the original function, -in the same state it would be if the callee function had returned. -(For instance, -after a lua_callk the function and its arguments are -removed from the stack and replaced by the results from the call.) -It also has the same upvalues. -Whatever it returns is handled by Lua as if it were the return -of the original function. - - -

-The only difference in the Lua state between the original function -and its continuation is the result of a call to lua_getctx. - - - - - -

4.8 – Functions and Types

- -

-Here we list all functions and types from the C API in -alphabetical order. -Each function has an indicator like this: -[-o, +p, x] - - -

-The first field, o, -is how many elements the function pops from the stack. -The second field, p, -is how many elements the function pushes onto the stack. -(Any function always pushes its results after popping its arguments.) -A field in the form x|y means the function can push (or pop) -x or y elements, -depending on the situation; -an interrogation mark '?' means that -we cannot know how many elements the function pops/pushes -by looking only at its arguments -(e.g., they may depend on what is on the stack). -The third field, x, -tells whether the function may throw errors: -'-' means the function never throws any error; -'e' means the function may throw errors; -'v' means the function may throw an error on purpose. - - - -


lua_absindex

-[-0, +0, –] -

int lua_absindex (lua_State *L, int idx);
- -

-Converts the acceptable index idx into an absolute index -(that is, one that does not depend on the stack top). - - - - - -


lua_Alloc

-
typedef void * (*lua_Alloc) (void *ud,
-                             void *ptr,
-                             size_t osize,
-                             size_t nsize);
- -

-The type of the memory-allocation function used by Lua states. -The allocator function must provide a -functionality similar to realloc, -but not exactly the same. -Its arguments are -ud, an opaque pointer passed to lua_newstate; -ptr, a pointer to the block being allocated/reallocated/freed; -osize, the original size of the block or some code about what -is being allocated; -nsize, the new size of the block. - - -

-When ptr is not NULL, -osize is the size of the block pointed by ptr, -that is, the size given when it was allocated or reallocated. - - -

-When ptr is NULL, -osize encodes the kind of object that Lua is allocating. -osize is any of -LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, -LUA_TUSERDATA, or LUA_TTHREAD when (and only when) -Lua is creating a new object of that type. -When osize is some other value, -Lua is allocating memory for something else. - - -

-Lua assumes the following behavior from the allocator function: - - -

-When nsize is zero, -the allocator should behave like free -and return NULL. - - -

-When nsize is not zero, -the allocator should behave like realloc. -The allocator returns NULL -if and only if it cannot fulfill the request. -Lua assumes that the allocator never fails when -osize >= nsize. - - -

-Here is a simple implementation for the allocator function. -It is used in the auxiliary library by luaL_newstate. - -

-     static void *l_alloc (void *ud, void *ptr, size_t osize,
-                                                size_t nsize) {
-       (void)ud;  (void)osize;  /* not used */
-       if (nsize == 0) {
-         free(ptr);
-         return NULL;
-       }
-       else
-         return realloc(ptr, nsize);
-     }
-

-Note that Standard C ensures -that free(NULL) has no effect and that -realloc(NULL, size) is equivalent to malloc(size). -This code assumes that realloc does not fail when shrinking a block. -(Although Standard C does not ensure this behavior, -it seems to be a safe assumption.) - - - - - -


lua_arith

-[-(2|1), +1, e] -

void lua_arith (lua_State *L, int op);
- -

-Performs an arithmetic operation over the two values -(or one, in the case of negation) -at the top of the stack, -with the value at the top being the second operand, -pops these values, and pushes the result of the operation. -The function follows the semantics of the corresponding Lua operator -(that is, it may call metamethods). - - -

-The value of op must be one of the following constants: - -

- - - - -

lua_atpanic

-[-0, +0, –] -

lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
- -

-Sets a new panic function and returns the old one (see §4.6). - - - - - -


lua_call

-[-(nargs+1), +nresults, e] -

void lua_call (lua_State *L, int nargs, int nresults);
- -

-Calls a function. - - -

-To call a function you must use the following protocol: -first, the function to be called is pushed onto the stack; -then, the arguments to the function are pushed -in direct order; -that is, the first argument is pushed first. -Finally you call lua_call; -nargs is the number of arguments that you pushed onto the stack. -All arguments and the function value are popped from the stack -when the function is called. -The function results are pushed onto the stack when the function returns. -The number of results is adjusted to nresults, -unless nresults is LUA_MULTRET. -In this case, all results from the function are pushed. -Lua takes care that the returned values fit into the stack space. -The function results are pushed onto the stack in direct order -(the first result is pushed first), -so that after the call the last result is on the top of the stack. - - -

-Any error inside the called function is propagated upwards -(with a longjmp). - - -

-The following example shows how the host program can do the -equivalent to this Lua code: - -

-     a = f("how", t.x, 14)
-

-Here it is in C: - -

-     lua_getglobal(L, "f");                  /* function to be called */
-     lua_pushstring(L, "how");                        /* 1st argument */
-     lua_getglobal(L, "t");                    /* table to be indexed */
-     lua_getfield(L, -1, "x");        /* push result of t.x (2nd arg) */
-     lua_remove(L, -2);                  /* remove 't' from the stack */
-     lua_pushinteger(L, 14);                          /* 3rd argument */
-     lua_call(L, 3, 1);     /* call 'f' with 3 arguments and 1 result */
-     lua_setglobal(L, "a");                         /* set global 'a' */
-

-Note that the code above is "balanced": -at its end, the stack is back to its original configuration. -This is considered good programming practice. - - - - - -


lua_callk

-[-(nargs + 1), +nresults, e] -

void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
-                lua_CFunction k);
- -

-This function behaves exactly like lua_call, -but allows the called function to yield (see §4.7). - - - - - -


lua_CFunction

-
typedef int (*lua_CFunction) (lua_State *L);
- -

-Type for C functions. - - -

-In order to communicate properly with Lua, -a C function must use the following protocol, -which defines the way parameters and results are passed: -a C function receives its arguments from Lua in its stack -in direct order (the first argument is pushed first). -So, when the function starts, -lua_gettop(L) returns the number of arguments received by the function. -The first argument (if any) is at index 1 -and its last argument is at index lua_gettop(L). -To return values to Lua, a C function just pushes them onto the stack, -in direct order (the first result is pushed first), -and returns the number of results. -Any other value in the stack below the results will be properly -discarded by Lua. -Like a Lua function, a C function called by Lua can also return -many results. - - -

-As an example, the following function receives a variable number -of numerical arguments and returns their average and sum: - -

-     static int foo (lua_State *L) {
-       int n = lua_gettop(L);    /* number of arguments */
-       lua_Number sum = 0;
-       int i;
-       for (i = 1; i <= n; i++) {
-         if (!lua_isnumber(L, i)) {
-           lua_pushstring(L, "incorrect argument");
-           lua_error(L);
-         }
-         sum += lua_tonumber(L, i);
-       }
-       lua_pushnumber(L, sum/n);        /* first result */
-       lua_pushnumber(L, sum);         /* second result */
-       return 2;                   /* number of results */
-     }
-
- - - - -

lua_checkstack

-[-0, +0, –] -

int lua_checkstack (lua_State *L, int extra);
- -

-Ensures that there are at least extra free stack slots in the stack. -It returns false if it cannot fulfill the request, -because it would cause the stack to be larger than a fixed maximum size -(typically at least a few thousand elements) or -because it cannot allocate memory for the new stack size. -This function never shrinks the stack; -if the stack is already larger than the new size, -it is left unchanged. - - - - - -


lua_close

-[-0, +0, –] -

void lua_close (lua_State *L);
- -

-Destroys all objects in the given Lua state -(calling the corresponding garbage-collection metamethods, if any) -and frees all dynamic memory used by this state. -On several platforms, you may not need to call this function, -because all resources are naturally released when the host program ends. -On the other hand, long-running programs that create multiple states, -such as daemons or web servers, -might need to close states as soon as they are not needed. - - - - - -


lua_compare

-[-0, +0, e] -

int lua_compare (lua_State *L, int index1, int index2, int op);
- -

-Compares two Lua values. -Returns 1 if the value at index index1 satisfies op -when compared with the value at index index2, -following the semantics of the corresponding Lua operator -(that is, it may call metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices is non valid. - - -

-The value of op must be one of the following constants: - -

    - -
  • LUA_OPEQ: compares for equality (==)
  • -
  • LUA_OPLT: compares for less than (<)
  • -
  • LUA_OPLE: compares for less or equal (<=)
  • - -
- - - - -

lua_concat

-[-n, +1, e] -

void lua_concat (lua_State *L, int n);
- -

-Concatenates the n values at the top of the stack, -pops them, and leaves the result at the top. -If n is 1, the result is the single value on the stack -(that is, the function does nothing); -if n is 0, the result is the empty string. -Concatenation is performed following the usual semantics of Lua -(see §3.4.5). - - - - - -


lua_copy

-[-0, +0, –] -

void lua_copy (lua_State *L, int fromidx, int toidx);
- -

-Moves the element at index fromidx -into the valid index toidx -without shifting any element -(therefore replacing the value at that position). - - - - - -


lua_createtable

-[-0, +1, e] -

void lua_createtable (lua_State *L, int narr, int nrec);
- -

-Creates a new empty table and pushes it onto the stack. -Parameter narr is a hint for how many elements the table -will have as a sequence; -parameter nrec is a hint for how many other elements -the table will have. -Lua may use these hints to preallocate memory for the new table. -This pre-allocation is useful for performance when you know in advance -how many elements the table will have. -Otherwise you can use the function lua_newtable. - - - - - -


lua_dump

-[-0, +0, e] -

int lua_dump (lua_State *L, lua_Writer writer, void *data);
- -

-Dumps a function as a binary chunk. -Receives a Lua function on the top of the stack -and produces a binary chunk that, -if loaded again, -results in a function equivalent to the one dumped. -As it produces parts of the chunk, -lua_dump calls function writer (see lua_Writer) -with the given data -to write them. - - -

-The value returned is the error code returned by the last -call to the writer; -0 means no errors. - - -

-This function does not pop the Lua function from the stack. - - - - - -


lua_error

-[-1, +0, v] -

int lua_error (lua_State *L);
- -

-Generates a Lua error. -The error message (which can actually be a Lua value of any type) -must be on the stack top. -This function does a long jump, -and therefore never returns -(see luaL_error). - - - - - -


lua_gc

-[-0, +0, e] -

int lua_gc (lua_State *L, int what, int data);
- -

-Controls the garbage collector. - - -

-This function performs several tasks, -according to the value of the parameter what: - -

    - -
  • LUA_GCSTOP: -stops the garbage collector. -
  • - -
  • LUA_GCRESTART: -restarts the garbage collector. -
  • - -
  • LUA_GCCOLLECT: -performs a full garbage-collection cycle. -
  • - -
  • LUA_GCCOUNT: -returns the current amount of memory (in Kbytes) in use by Lua. -
  • - -
  • LUA_GCCOUNTB: -returns the remainder of dividing the current amount of bytes of -memory in use by Lua by 1024. -
  • - -
  • LUA_GCSTEP: -performs an incremental step of garbage collection. -The step "size" is controlled by data -(larger values mean more steps) in a non-specified way. -If you want to control the step size -you must experimentally tune the value of data. -The function returns 1 if the step finished a -garbage-collection cycle. -
  • - -
  • LUA_GCSETPAUSE: -sets data as the new value -for the pause of the collector (see §2.5). -The function returns the previous value of the pause. -
  • - -
  • LUA_GCSETSTEPMUL: -sets data as the new value for the step multiplier of -the collector (see §2.5). -The function returns the previous value of the step multiplier. -
  • - -
  • LUA_GCISRUNNING: -returns a boolean that tells whether the collector is running -(i.e., not stopped). -
  • - -
  • LUA_GCGEN: -changes the collector to generational mode -(see §2.5). -
  • - -
  • LUA_GCINC: -changes the collector to incremental mode. -This is the default mode. -
  • - -
- -

-For more details about these options, -see collectgarbage. - - - - - -


lua_getallocf

-[-0, +0, –] -

lua_Alloc lua_getallocf (lua_State *L, void **ud);
- -

-Returns the memory-allocation function of a given state. -If ud is not NULL, Lua stores in *ud the -opaque pointer passed to lua_newstate. - - - - - -


lua_getctx

-[-0, +0, –] -

int lua_getctx (lua_State *L, int *ctx);
- -

-This function is called by a continuation function (see §4.7) -to retrieve the status of the thread and a context information. - - -

-When called in the original function, -lua_getctx always returns LUA_OK -and does not change the value of its argument ctx. -When called inside a continuation function, -lua_getctx returns LUA_YIELD and sets -the value of ctx to be the context information -(the value passed as the ctx argument -to the callee together with the continuation function). - - -

-When the callee is lua_pcallk, -Lua may also call its continuation function -to handle errors during the call. -That is, upon an error in the function called by lua_pcallk, -Lua may not return to the original function -but instead may call the continuation function. -In that case, a call to lua_getctx will return the error code -(the value that would be returned by lua_pcallk); -the value of ctx will be set to the context information, -as in the case of a yield. - - - - - -


lua_getfield

-[-0, +1, e] -

void lua_getfield (lua_State *L, int index, const char *k);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given index. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - - - - -


lua_getglobal

-[-0, +1, e] -

void lua_getglobal (lua_State *L, const char *name);
- -

-Pushes onto the stack the value of the global name. - - - - - -


lua_getmetatable

-[-0, +(0|1), –] -

int lua_getmetatable (lua_State *L, int index);
- -

-Pushes onto the stack the metatable of the value at the given index. -If the value does not have a metatable, -the function returns 0 and pushes nothing on the stack. - - - - - -


lua_gettable

-[-1, +1, e] -

void lua_gettable (lua_State *L, int index);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given index -and k is the value at the top of the stack. - - -

-This function pops the key from the stack -(putting the resulting value in its place). -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - - - - -


lua_gettop

-[-0, +0, –] -

int lua_gettop (lua_State *L);
- -

-Returns the index of the top element in the stack. -Because indices start at 1, -this result is equal to the number of elements in the stack -(and so 0 means an empty stack). - - - - - -


lua_getuservalue

-[-0, +1, –] -

void lua_getuservalue (lua_State *L, int index);
- -

-Pushes onto the stack the Lua value associated with the userdata -at the given index. -This Lua value must be a table or nil. - - - - - -


lua_insert

-[-1, +1, –] -

void lua_insert (lua_State *L, int index);
- -

-Moves the top element into the given valid index, -shifting up the elements above this index to open space. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_Integer

-
typedef ptrdiff_t lua_Integer;
- -

-The type used by the Lua API to represent signed integral values. - - -

-By default it is a ptrdiff_t, -which is usually the largest signed integral type the machine handles -"comfortably". - - - - - -


lua_isboolean

-[-0, +0, –] -

int lua_isboolean (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a boolean, -and 0 otherwise. - - - - - -


lua_iscfunction

-[-0, +0, –] -

int lua_iscfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a C function, -and 0 otherwise. - - - - - -


lua_isfunction

-[-0, +0, –] -

int lua_isfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a function -(either C or Lua), and 0 otherwise. - - - - - -


lua_islightuserdata

-[-0, +0, –] -

int lua_islightuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a light userdata, -and 0 otherwise. - - - - - -


lua_isnil

-[-0, +0, –] -

int lua_isnil (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is nil, -and 0 otherwise. - - - - - -


lua_isnone

-[-0, +0, –] -

int lua_isnone (lua_State *L, int index);
- -

-Returns 1 if the given index is not valid, -and 0 otherwise. - - - - - -


lua_isnoneornil

-[-0, +0, –] -

int lua_isnoneornil (lua_State *L, int index);
- -

-Returns 1 if the given index is not valid -or if the value at this index is nil, -and 0 otherwise. - - - - - -


lua_isnumber

-[-0, +0, –] -

int lua_isnumber (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a number -or a string convertible to a number, -and 0 otherwise. - - - - - -


lua_isstring

-[-0, +0, –] -

int lua_isstring (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a string -or a number (which is always convertible to a string), -and 0 otherwise. - - - - - -


lua_istable

-[-0, +0, –] -

int lua_istable (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a table, -and 0 otherwise. - - - - - -


lua_isthread

-[-0, +0, –] -

int lua_isthread (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a thread, -and 0 otherwise. - - - - - -


lua_isuserdata

-[-0, +0, –] -

int lua_isuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a userdata -(either full or light), and 0 otherwise. - - - - - -


lua_len

-[-0, +1, e] -

void lua_len (lua_State *L, int index);
- -

-Returns the "length" of the value at the given index; -it is equivalent to the '#' operator in Lua (see §3.4.6). -The result is pushed on the stack. - - - - - -


lua_load

-[-0, +1, –] -

int lua_load (lua_State *L,
-              lua_Reader reader,
-              void *data,
-              const char *source,
-              const char *mode);
- -

-Loads a Lua chunk (without running it). -If there are no errors, -lua_load pushes the compiled chunk as a Lua -function on top of the stack. -Otherwise, it pushes an error message. - - -

-The return values of lua_load are: - -

    - -
  • LUA_OK: no errors;
  • - -
  • LUA_ERRSYNTAX: -syntax error during precompilation;
  • - -
  • LUA_ERRMEM: -memory allocation error;
  • - -
  • LUA_ERRGCMM: -error while running a __gc metamethod. -(This error has no relation with the chunk being loaded. -It is generated by the garbage collector.) -
  • - -
- -

-The lua_load function uses a user-supplied reader function -to read the chunk (see lua_Reader). -The data argument is an opaque value passed to the reader function. - - -

-The source argument gives a name to the chunk, -which is used for error messages and in debug information (see §4.9). - - -

-lua_load automatically detects whether the chunk is text or binary -and loads it accordingly (see program luac). -The string mode works as in function load, -with the addition that -a NULL value is equivalent to the string "bt". - - -

-lua_load uses the stack internally, -so the reader function should always leave the stack -unmodified when returning. - - -

-If the resulting function has one upvalue, -this upvalue is set to the value of the global environment -stored at index LUA_RIDX_GLOBALS in the registry (see §4.5). -When loading main chunks, -this upvalue will be the _ENV variable (see §2.2). - - - - - -


lua_newstate

-[-0, +0, –] -

lua_State *lua_newstate (lua_Alloc f, void *ud);
- -

-Creates a new thread running in a new, independent state. -Returns NULL if cannot create the thread or the state -(due to lack of memory). -The argument f is the allocator function; -Lua does all memory allocation for this state through this function. -The second argument, ud, is an opaque pointer that Lua -passes to the allocator in every call. - - - - - -


lua_newtable

-[-0, +1, e] -

void lua_newtable (lua_State *L);
- -

-Creates a new empty table and pushes it onto the stack. -It is equivalent to lua_createtable(L, 0, 0). - - - - - -


lua_newthread

-[-0, +1, e] -

lua_State *lua_newthread (lua_State *L);
- -

-Creates a new thread, pushes it on the stack, -and returns a pointer to a lua_State that represents this new thread. -The new thread returned by this function shares with the original thread -its global environment, -but has an independent execution stack. - - -

-There is no explicit function to close or to destroy a thread. -Threads are subject to garbage collection, -like any Lua object. - - - - - -


lua_newuserdata

-[-0, +1, e] -

void *lua_newuserdata (lua_State *L, size_t size);
- -

-This function allocates a new block of memory with the given size, -pushes onto the stack a new full userdata with the block address, -and returns this address. -The host program can freely use this memory. - - - - - -


lua_next

-[-1, +(2|0), e] -

int lua_next (lua_State *L, int index);
- -

-Pops a key from the stack, -and pushes a key–value pair from the table at the given index -(the "next" pair after the given key). -If there are no more elements in the table, -then lua_next returns 0 (and pushes nothing). - - -

-A typical traversal looks like this: - -

-     /* table is in the stack at index 't' */
-     lua_pushnil(L);  /* first key */
-     while (lua_next(L, t) != 0) {
-       /* uses 'key' (at index -2) and 'value' (at index -1) */
-       printf("%s - %s\n",
-              lua_typename(L, lua_type(L, -2)),
-              lua_typename(L, lua_type(L, -1)));
-       /* removes 'value'; keeps 'key' for next iteration */
-       lua_pop(L, 1);
-     }
-
- -

-While traversing a table, -do not call lua_tolstring directly on a key, -unless you know that the key is actually a string. -Recall that lua_tolstring may change -the value at the given index; -this confuses the next call to lua_next. - - -

-See function next for the caveats of modifying -the table during its traversal. - - - - - -


lua_Number

-
typedef double lua_Number;
- -

-The type of numbers in Lua. -By default, it is double, but that can be changed in luaconf.h. -Through this configuration file you can change -Lua to operate with another type for numbers (e.g., float or long). - - - - - -


lua_pcall

-[-(nargs + 1), +(nresults|1), –] -

int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);
- -

-Calls a function in protected mode. - - -

-Both nargs and nresults have the same meaning as -in lua_call. -If there are no errors during the call, -lua_pcall behaves exactly like lua_call. -However, if there is any error, -lua_pcall catches it, -pushes a single value on the stack (the error message), -and returns an error code. -Like lua_call, -lua_pcall always removes the function -and its arguments from the stack. - - -

-If msgh is 0, -then the error message returned on the stack -is exactly the original error message. -Otherwise, msgh is the stack index of a -message handler. -(In the current implementation, this index cannot be a pseudo-index.) -In case of runtime errors, -this function will be called with the error message -and its return value will be the message -returned on the stack by lua_pcall. - - -

-Typically, the message handler is used to add more debug -information to the error message, such as a stack traceback. -Such information cannot be gathered after the return of lua_pcall, -since by then the stack has unwound. - - -

-The lua_pcall function returns one of the following codes -(defined in lua.h): - -

    - -
  • LUA_OK (0): -success.
  • - -
  • LUA_ERRRUN: -a runtime error. -
  • - -
  • LUA_ERRMEM: -memory allocation error. -For such errors, Lua does not call the message handler. -
  • - -
  • LUA_ERRERR: -error while running the message handler. -
  • - -
  • LUA_ERRGCMM: -error while running a __gc metamethod. -(This error typically has no relation with the function being called. -It is generated by the garbage collector.) -
  • - -
- - - - -

lua_pcallk

-[-(nargs + 1), +(nresults|1), –] -

int lua_pcallk (lua_State *L,
-                int nargs,
-                int nresults,
-                int errfunc,
-                int ctx,
-                lua_CFunction k);
- -

-This function behaves exactly like lua_pcall, -but allows the called function to yield (see §4.7). - - - - - -


lua_pop

-[-n, +0, –] -

void lua_pop (lua_State *L, int n);
- -

-Pops n elements from the stack. - - - - - -


lua_pushboolean

-[-0, +1, –] -

void lua_pushboolean (lua_State *L, int b);
- -

-Pushes a boolean value with value b onto the stack. - - - - - -


lua_pushcclosure

-[-n, +1, e] -

void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
- -

-Pushes a new C closure onto the stack. - - -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure (see §4.4); -these values are then accessible to the function whenever it is called. -To associate values with a C function, -first these values should be pushed onto the stack -(when there are multiple values, the first value is pushed first). -Then lua_pushcclosure -is called to create and push the C function onto the stack, -with the argument n telling how many values should be -associated with the function. -lua_pushcclosure also pops these values from the stack. - - -

-The maximum value for n is 255. - - -

-When n is zero, -this function creates a light C function, -which is just a pointer to the C function. -In that case, it never throws a memory error. - - - - - -


lua_pushcfunction

-[-0, +1, –] -

void lua_pushcfunction (lua_State *L, lua_CFunction f);
- -

-Pushes a C function onto the stack. -This function receives a pointer to a C function -and pushes onto the stack a Lua value of type function that, -when called, invokes the corresponding C function. - - -

-Any function to be registered in Lua must -follow the correct protocol to receive its parameters -and return its results (see lua_CFunction). - - -

-lua_pushcfunction is defined as a macro: - -

-     #define lua_pushcfunction(L,f)  lua_pushcclosure(L,f,0)
-

-Note that f is used twice. - - - - - -


lua_pushfstring

-[-0, +1, e] -

const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
- -

-Pushes onto the stack a formatted string -and returns a pointer to this string. -It is similar to the ISO C function sprintf, -but has some important differences: - -

    - -
  • -You do not have to allocate space for the result: -the result is a Lua string and Lua takes care of memory allocation -(and deallocation, through garbage collection). -
  • - -
  • -The conversion specifiers are quite restricted. -There are no flags, widths, or precisions. -The conversion specifiers can only be -'%%' (inserts a '%' in the string), -'%s' (inserts a zero-terminated string, with no size restrictions), -'%f' (inserts a lua_Number), -'%p' (inserts a pointer as a hexadecimal numeral), -'%d' (inserts an int), and -'%c' (inserts an int as a byte). -
  • - -
- - - - -

lua_pushglobaltable

-[-0, +1, –] -

void lua_pushglobaltable (lua_State *L);
- -

-Pushes the global environment onto the stack. - - - - - -


lua_pushinteger

-[-0, +1, –] -

void lua_pushinteger (lua_State *L, lua_Integer n);
- -

-Pushes a number with value n onto the stack. - - - - - -


lua_pushlightuserdata

-[-0, +1, –] -

void lua_pushlightuserdata (lua_State *L, void *p);
- -

-Pushes a light userdata onto the stack. - - -

-Userdata represent C values in Lua. -A light userdata represents a pointer, a void*. -It is a value (like a number): -you do not create it, it has no individual metatable, -and it is not collected (as it was never created). -A light userdata is equal to "any" -light userdata with the same C address. - - - - - -


lua_pushliteral

-[-0, +1, e] -

const char *lua_pushliteral (lua_State *L, const char *s);
- -

-This macro is equivalent to lua_pushlstring, -but can be used only when s is a literal string. -It automatically provides the string length. - - - - - -


lua_pushlstring

-[-0, +1, e] -

const char *lua_pushlstring (lua_State *L, const char *s, size_t len);
- -

-Pushes the string pointed to by s with size len -onto the stack. -Lua makes (or reuses) an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. -The string can contain any binary data, -including embedded zeros. - - -

-Returns a pointer to the internal copy of the string. - - - - - -


lua_pushnil

-[-0, +1, –] -

void lua_pushnil (lua_State *L);
- -

-Pushes a nil value onto the stack. - - - - - -


lua_pushnumber

-[-0, +1, –] -

void lua_pushnumber (lua_State *L, lua_Number n);
- -

-Pushes a number with value n onto the stack. - - - - - -


lua_pushstring

-[-0, +1, e] -

const char *lua_pushstring (lua_State *L, const char *s);
- -

-Pushes the zero-terminated string pointed to by s -onto the stack. -Lua makes (or reuses) an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. - - -

-Returns a pointer to the internal copy of the string. - - -

-If s is NULL, pushes nil and returns NULL. - - - - - -


lua_pushthread

-[-0, +1, –] -

int lua_pushthread (lua_State *L);
- -

-Pushes the thread represented by L onto the stack. -Returns 1 if this thread is the main thread of its state. - - - - - -


lua_pushunsigned

-[-0, +1, –] -

void lua_pushunsigned (lua_State *L, lua_Unsigned n);
- -

-Pushes a number with value n onto the stack. - - - - - -


lua_pushvalue

-[-0, +1, –] -

void lua_pushvalue (lua_State *L, int index);
- -

-Pushes a copy of the element at the given index -onto the stack. - - - - - -


lua_pushvfstring

-[-0, +1, e] -

const char *lua_pushvfstring (lua_State *L,
-                              const char *fmt,
-                              va_list argp);
- -

-Equivalent to lua_pushfstring, except that it receives a va_list -instead of a variable number of arguments. - - - - - -


lua_rawequal

-[-0, +0, –] -

int lua_rawequal (lua_State *L, int index1, int index2);
- -

-Returns 1 if the two values in indices index1 and -index2 are primitively equal -(that is, without calling metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices are non valid. - - - - - -


lua_rawget

-[-1, +1, –] -

void lua_rawget (lua_State *L, int index);
- -

-Similar to lua_gettable, but does a raw access -(i.e., without metamethods). - - - - - -


lua_rawgeti

-[-0, +1, –] -

void lua_rawgeti (lua_State *L, int index, int n);
- -

-Pushes onto the stack the value t[n], -where t is the table at the given index. -The access is raw; -that is, it does not invoke metamethods. - - - - - -


lua_rawgetp

-[-0, +1, –] -

void lua_rawgetp (lua_State *L, int index, const void *p);
- -

-Pushes onto the stack the value t[k], -where t is the table at the given index and -k is the pointer p represented as a light userdata. -The access is raw; -that is, it does not invoke metamethods. - - - - - -


lua_rawlen

-[-0, +0, –] -

size_t lua_rawlen (lua_State *L, int index);
- -

-Returns the raw "length" of the value at the given index: -for strings, this is the string length; -for tables, this is the result of the length operator ('#') -with no metamethods; -for userdata, this is the size of the block of memory allocated -for the userdata; -for other values, it is 0. - - - - - -


lua_rawset

-[-2, +0, e] -

void lua_rawset (lua_State *L, int index);
- -

-Similar to lua_settable, but does a raw assignment -(i.e., without metamethods). - - - - - -


lua_rawseti

-[-1, +0, e] -

void lua_rawseti (lua_State *L, int index, int n);
- -

-Does the equivalent of t[n] = v, -where t is the table at the given index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw; -that is, it does not invoke metamethods. - - - - - -


lua_rawsetp

-[-1, +0, e] -

void lua_rawsetp (lua_State *L, int index, const void *p);
- -

-Does the equivalent of t[k] = v, -where t is the table at the given index, -k is the pointer p represented as a light userdata, -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw; -that is, it does not invoke metamethods. - - - - - -


lua_Reader

-
typedef const char * (*lua_Reader) (lua_State *L,
-                                    void *data,
-                                    size_t *size);
- -

-The reader function used by lua_load. -Every time it needs another piece of the chunk, -lua_load calls the reader, -passing along its data parameter. -The reader must return a pointer to a block of memory -with a new piece of the chunk -and set size to the block size. -The block must exist until the reader function is called again. -To signal the end of the chunk, -the reader must return NULL or set size to zero. -The reader function may return pieces of any size greater than zero. - - - - - -


lua_register

-[-0, +0, e] -

void lua_register (lua_State *L, const char *name, lua_CFunction f);
- -

-Sets the C function f as the new value of global name. -It is defined as a macro: - -

-     #define lua_register(L,n,f) \
-            (lua_pushcfunction(L, f), lua_setglobal(L, n))
-
- - - - -

lua_remove

-[-1, +0, –] -

void lua_remove (lua_State *L, int index);
- -

-Removes the element at the given valid index, -shifting down the elements above this index to fill the gap. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_replace

-[-1, +0, –] -

void lua_replace (lua_State *L, int index);
- -

-Moves the top element into the given valid index -without shifting any element -(therefore replacing the value at the given index), -and then pops the top element. - - - - - -


lua_resume

-[-?, +?, –] -

int lua_resume (lua_State *L, lua_State *from, int nargs);
- -

-Starts and resumes a coroutine in a given thread. - - -

-To start a coroutine, -you push onto the thread stack the main function plus any arguments; -then you call lua_resume, -with nargs being the number of arguments. -This call returns when the coroutine suspends or finishes its execution. -When it returns, the stack contains all values passed to lua_yield, -or all values returned by the body function. -lua_resume returns -LUA_YIELD if the coroutine yields, -LUA_OK if the coroutine finishes its execution -without errors, -or an error code in case of errors (see lua_pcall). - - -

-In case of errors, -the stack is not unwound, -so you can use the debug API over it. -The error message is on the top of the stack. - - -

-To resume a coroutine, -you remove any results from the last lua_yield, -put on its stack only the values to -be passed as results from yield, -and then call lua_resume. - - -

-The parameter from represents the coroutine that is resuming L. -If there is no such coroutine, -this parameter can be NULL. - - - - - -


lua_setallocf

-[-0, +0, –] -

void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
- -

-Changes the allocator function of a given state to f -with user data ud. - - - - - -


lua_setfield

-[-1, +0, e] -

void lua_setfield (lua_State *L, int index, const char *k);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_setglobal

-[-1, +0, e] -

void lua_setglobal (lua_State *L, const char *name);
- -

-Pops a value from the stack and -sets it as the new value of global name. - - - - - -


lua_setmetatable

-[-1, +0, –] -

void lua_setmetatable (lua_State *L, int index);
- -

-Pops a table from the stack and -sets it as the new metatable for the value at the given index. - - - - - -


lua_settable

-[-2, +0, e] -

void lua_settable (lua_State *L, int index);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given index, -v is the value at the top of the stack, -and k is the value just below the top. - - -

-This function pops both the key and the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_settop

-[-?, +?, –] -

void lua_settop (lua_State *L, int index);
- -

-Accepts any index, or 0, -and sets the stack top to this index. -If the new top is larger than the old one, -then the new elements are filled with nil. -If index is 0, then all stack elements are removed. - - - - - -


lua_setuservalue

-[-1, +0, –] -

void lua_setuservalue (lua_State *L, int index);
- -

-Pops a table or nil from the stack and sets it as -the new value associated to the userdata at the given index. - - - - - -


lua_State

-
typedef struct lua_State lua_State;
- -

-An opaque structure that points to a thread and indirectly -(through the thread) to the whole state of a Lua interpreter. -The Lua library is fully reentrant: -it has no global variables. -All information about a state is accessible through this structure. - - -

-A pointer to this structure must be passed as the first argument to -every function in the library, except to lua_newstate, -which creates a Lua state from scratch. - - - - - -


lua_status

-[-0, +0, –] -

int lua_status (lua_State *L);
- -

-Returns the status of the thread L. - - -

-The status can be 0 (LUA_OK) for a normal thread, -an error code if the thread finished the execution -of a lua_resume with an error, -or LUA_YIELD if the thread is suspended. - - -

-You can only call functions in threads with status LUA_OK. -You can resume threads with status LUA_OK -(to start a new coroutine) or LUA_YIELD -(to resume a coroutine). - - - - - -


lua_toboolean

-[-0, +0, –] -

int lua_toboolean (lua_State *L, int index);
- -

-Converts the Lua value at the given index to a C boolean -value (0 or 1). -Like all tests in Lua, -lua_toboolean returns true for any Lua value -different from false and nil; -otherwise it returns false. -(If you want to accept only actual boolean values, -use lua_isboolean to test the value's type.) - - - - - -


lua_tocfunction

-[-0, +0, –] -

lua_CFunction lua_tocfunction (lua_State *L, int index);
- -

-Converts a value at the given index to a C function. -That value must be a C function; -otherwise, returns NULL. - - - - - -


lua_tointeger

-[-0, +0, –] -

lua_Integer lua_tointeger (lua_State *L, int index);
- -

-Equivalent to lua_tointegerx with isnum equal to NULL. - - - - - -


lua_tointegerx

-[-0, +0, –] -

lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the signed integral type lua_Integer. -The Lua value must be a number or a string convertible to a number -(see §3.4.2); -otherwise, lua_tointegerx returns 0. - - -

-If the number is not an integer, -it is truncated in some non-specified way. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_tolstring

-[-0, +0, e] -

const char *lua_tolstring (lua_State *L, int index, size_t *len);
- -

-Converts the Lua value at the given index to a C string. -If len is not NULL, -it also sets *len with the string length. -The Lua value must be a string or a number; -otherwise, the function returns NULL. -If the value is a number, -then lua_tolstring also -changes the actual value in the stack to a string. -(This change confuses lua_next -when lua_tolstring is applied to keys during a table traversal.) - - -

-lua_tolstring returns a fully aligned pointer -to a string inside the Lua state. -This string always has a zero ('\0') -after its last character (as in C), -but can contain other zeros in its body. -Because Lua has garbage collection, -there is no guarantee that the pointer returned by lua_tolstring -will be valid after the corresponding value is removed from the stack. - - - - - -


lua_tonumber

-[-0, +0, –] -

lua_Number lua_tonumber (lua_State *L, int index);
- -

-Equivalent to lua_tonumberx with isnum equal to NULL. - - - - - -


lua_tonumberx

-[-0, +0, –] -

lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the C type lua_Number (see lua_Number). -The Lua value must be a number or a string convertible to a number -(see §3.4.2); -otherwise, lua_tonumberx returns 0. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_topointer

-[-0, +0, –] -

const void *lua_topointer (lua_State *L, int index);
- -

-Converts the value at the given index to a generic -C pointer (void*). -The value can be a userdata, a table, a thread, or a function; -otherwise, lua_topointer returns NULL. -Different objects will give different pointers. -There is no way to convert the pointer back to its original value. - - -

-Typically this function is used only for debug information. - - - - - -


lua_tostring

-[-0, +0, e] -

const char *lua_tostring (lua_State *L, int index);
- -

-Equivalent to lua_tolstring with len equal to NULL. - - - - - -


lua_tothread

-[-0, +0, –] -

lua_State *lua_tothread (lua_State *L, int index);
- -

-Converts the value at the given index to a Lua thread -(represented as lua_State*). -This value must be a thread; -otherwise, the function returns NULL. - - - - - -


lua_tounsigned

-[-0, +0, –] -

lua_Unsigned lua_tounsigned (lua_State *L, int index);
- -

-Equivalent to lua_tounsignedx with isnum equal to NULL. - - - - - -


lua_tounsignedx

-[-0, +0, –] -

lua_Unsigned lua_tounsignedx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the unsigned integral type lua_Unsigned. -The Lua value must be a number or a string convertible to a number -(see §3.4.2); -otherwise, lua_tounsignedx returns 0. - - -

-If the number is not an integer, -it is truncated in some non-specified way. -If the number is outside the range of representable values, -it is normalized to the remainder of its division by -one more than the maximum representable value. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_touserdata

-[-0, +0, –] -

void *lua_touserdata (lua_State *L, int index);
- -

-If the value at the given index is a full userdata, -returns its block address. -If the value is a light userdata, -returns its pointer. -Otherwise, returns NULL. - - - - - -


lua_type

-[-0, +0, –] -

int lua_type (lua_State *L, int index);
- -

-Returns the type of the value in the given valid index, -or LUA_TNONE for a non-valid (but acceptable) index. -The types returned by lua_type are coded by the following constants -defined in lua.h: -LUA_TNIL, -LUA_TNUMBER, -LUA_TBOOLEAN, -LUA_TSTRING, -LUA_TTABLE, -LUA_TFUNCTION, -LUA_TUSERDATA, -LUA_TTHREAD, -and -LUA_TLIGHTUSERDATA. - - - - - -


lua_typename

-[-0, +0, –] -

const char *lua_typename (lua_State *L, int tp);
- -

-Returns the name of the type encoded by the value tp, -which must be one the values returned by lua_type. - - - - - -


lua_Unsigned

-
typedef unsigned long lua_Unsigned;
- -

-The type used by the Lua API to represent unsigned integral values. -It must have at least 32 bits. - - -

-By default it is an unsigned int or an unsigned long, -whichever can hold 32-bit values. - - - - - -


lua_upvalueindex

-[-0, +0, –] -

int lua_upvalueindex (int i);
- -

-Returns the pseudo-index that represents the i-th upvalue of -the running function (see §4.4). - - - - - -


lua_version

-[-0, +0, v] -

const lua_Number *lua_version (lua_State *L);
- -

-Returns the address of the version number stored in the Lua core. -When called with a valid lua_State, -returns the address of the version used to create that state. -When called with NULL, -returns the address of the version running the call. - - - - - -


lua_Writer

-
typedef int (*lua_Writer) (lua_State *L,
-                           const void* p,
-                           size_t sz,
-                           void* ud);
- -

-The type of the writer function used by lua_dump. -Every time it produces another piece of chunk, -lua_dump calls the writer, -passing along the buffer to be written (p), -its size (sz), -and the data parameter supplied to lua_dump. - - -

-The writer returns an error code: -0 means no errors; -any other value means an error and stops lua_dump from -calling the writer again. - - - - - -


lua_xmove

-[-?, +?, –] -

void lua_xmove (lua_State *from, lua_State *to, int n);
- -

-Exchange values between different threads of the same state. - - -

-This function pops n values from the stack from, -and pushes them onto the stack to. - - - - - -


lua_yield

-[-?, +?, –] -

int lua_yield (lua_State *L, int nresults);
- -

-This function is equivalent to lua_yieldk, -but it has no continuation (see §4.7). -Therefore, when the thread resumes, -it returns to the function that called -the function calling lua_yield. - - - - - -


lua_yieldk

-[-?, +?, –] -

int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k);
- -

-Yields a coroutine. - - -

-This function should only be called as the -return expression of a C function, as follows: - -

-     return lua_yieldk (L, n, i, k);
-

-When a C function calls lua_yieldk in that way, -the running coroutine suspends its execution, -and the call to lua_resume that started this coroutine returns. -The parameter nresults is the number of values from the stack -that are passed as results to lua_resume. - - -

-When the coroutine is resumed again, -Lua calls the given continuation function k to continue -the execution of the C function that yielded (see §4.7). -This continuation function receives the same stack -from the previous function, -with the results removed and -replaced by the arguments passed to lua_resume. -Moreover, -the continuation function may access the value ctx -by calling lua_getctx. - - - - - - - -

4.9 – The Debug Interface

- -

-Lua has no built-in debugging facilities. -Instead, it offers a special interface -by means of functions and hooks. -This interface allows the construction of different -kinds of debuggers, profilers, and other tools -that need "inside information" from the interpreter. - - - -


lua_Debug

-
typedef struct lua_Debug {
-  int event;
-  const char *name;           /* (n) */
-  const char *namewhat;       /* (n) */
-  const char *what;           /* (S) */
-  const char *source;         /* (S) */
-  int currentline;            /* (l) */
-  int linedefined;            /* (S) */
-  int lastlinedefined;        /* (S) */
-  unsigned char nups;         /* (u) number of upvalues */
-  unsigned char nparams;      /* (u) number of parameters */
-  char isvararg;              /* (u) */
-  char istailcall;            /* (t) */
-  char short_src[LUA_IDSIZE]; /* (S) */
-  /* private part */
-  other fields
-} lua_Debug;
- -

-A structure used to carry different pieces of -information about a function or an activation record. -lua_getstack fills only the private part -of this structure, for later use. -To fill the other fields of lua_Debug with useful information, -call lua_getinfo. - - -

-The fields of lua_Debug have the following meaning: - -

    - -
  • source: -the source of the chunk that created the function. -If source starts with a '@', -it means that the function was defined in a file where -the file name follows the '@'. -If source starts with a '=', -the remainder of its contents describe the source in a user-dependent manner. -Otherwise, -the function was defined in a string where -source is that string. -
  • - -
  • short_src: -a "printable" version of source, to be used in error messages. -
  • - -
  • linedefined: -the line number where the definition of the function starts. -
  • - -
  • lastlinedefined: -the line number where the definition of the function ends. -
  • - -
  • what: -the string "Lua" if the function is a Lua function, -"C" if it is a C function, -"main" if it is the main part of a chunk. -
  • - -
  • currentline: -the current line where the given function is executing. -When no line information is available, -currentline is set to -1. -
  • - -
  • name: -a reasonable name for the given function. -Because functions in Lua are first-class values, -they do not have a fixed name: -some functions can be the value of multiple global variables, -while others can be stored only in a table field. -The lua_getinfo function checks how the function was -called to find a suitable name. -If it cannot find a name, -then name is set to NULL. -
  • - -
  • namewhat: -explains the name field. -The value of namewhat can be -"global", "local", "method", -"field", "upvalue", or "" (the empty string), -according to how the function was called. -(Lua uses the empty string when no other option seems to apply.) -
  • - -
  • istailcall: -true if this function invocation was called by a tail call. -In this case, the caller of this level is not in the stack. -
  • - -
  • nups: -the number of upvalues of the function. -
  • - -
  • nparams: -the number of fixed parameters of the function -(always 0 for C functions). -
  • - -
  • isvararg: -true if the function is a vararg function -(always true for C functions). -
  • - -
- - - - -

lua_gethook

-[-0, +0, –] -

lua_Hook lua_gethook (lua_State *L);
- -

-Returns the current hook function. - - - - - -


lua_gethookcount

-[-0, +0, –] -

int lua_gethookcount (lua_State *L);
- -

-Returns the current hook count. - - - - - -


lua_gethookmask

-[-0, +0, –] -

int lua_gethookmask (lua_State *L);
- -

-Returns the current hook mask. - - - - - -


lua_getinfo

-[-(0|1), +(0|1|2), e] -

int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
- -

-Gets information about a specific function or function invocation. - - -

-To get information about a function invocation, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). - - -

-To get information about a function you push it onto the stack -and start the what string with the character '>'. -(In that case, -lua_getinfo pops the function from the top of the stack.) -For instance, to know in which line a function f was defined, -you can write the following code: - -

-     lua_Debug ar;
-     lua_getglobal(L, "f");  /* get global 'f' */
-     lua_getinfo(L, ">S", &ar);
-     printf("%d\n", ar.linedefined);
-
- -

-Each character in the string what -selects some fields of the structure ar to be filled or -a value to be pushed on the stack: - -

    - -
  • 'n': fills in the field name and namewhat; -
  • - -
  • 'S': -fills in the fields source, short_src, -linedefined, lastlinedefined, and what; -
  • - -
  • 'l': fills in the field currentline; -
  • - -
  • 't': fills in the field istailcall; -
  • - -
  • 'u': fills in the fields -nups, nparams, and isvararg; -
  • - -
  • 'f': -pushes onto the stack the function that is -running at the given level; -
  • - -
  • 'L': -pushes onto the stack a table whose indices are the -numbers of the lines that are valid on the function. -(A valid line is a line with some associated code, -that is, a line where you can put a break point. -Non-valid lines include empty lines and comments.) -
  • - -
- -

-This function returns 0 on error -(for instance, an invalid option in what). - - - - - -


lua_getlocal

-[-0, +(0|1), –] -

const char *lua_getlocal (lua_State *L, lua_Debug *ar, int n);
- -

-Gets information about a local variable of -a given activation record or a given function. - - -

-In the first case, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). -The index n selects which local variable to inspect; -see debug.getlocal for details about variable indices -and names. - - -

-lua_getlocal pushes the variable's value onto the stack -and returns its name. - - -

-In the second case, ar should be NULL and the function -to be inspected must be at the top of the stack. -In this case, only parameters of Lua functions are visible -(as there is no information about what variables are active) -and no values are pushed onto the stack. - - -

-Returns NULL (and pushes nothing) -when the index is greater than -the number of active local variables. - - - - - -


lua_getstack

-[-0, +0, –] -

int lua_getstack (lua_State *L, int level, lua_Debug *ar);
- -

-Gets information about the interpreter runtime stack. - - -

-This function fills parts of a lua_Debug structure with -an identification of the activation record -of the function executing at a given level. -Level 0 is the current running function, -whereas level n+1 is the function that has called level n -(except for tail calls, which do not count on the stack). -When there are no errors, lua_getstack returns 1; -when called with a level greater than the stack depth, -it returns 0. - - - - - -


lua_getupvalue

-[-0, +(0|1), –] -

const char *lua_getupvalue (lua_State *L, int funcindex, int n);
- -

-Gets information about a closure's upvalue. -(For Lua functions, -upvalues are the external local variables that the function uses, -and that are consequently included in its closure.) -lua_getupvalue gets the index n of an upvalue, -pushes the upvalue's value onto the stack, -and returns its name. -funcindex points to the closure in the stack. -(Upvalues have no particular order, -as they are active through the whole function. -So, they are numbered in an arbitrary order.) - - -

-Returns NULL (and pushes nothing) -when the index is greater than the number of upvalues. -For C functions, this function uses the empty string "" -as a name for all upvalues. - - - - - -


lua_Hook

-
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
- -

-Type for debugging hook functions. - - -

-Whenever a hook is called, its ar argument has its field -event set to the specific event that triggered the hook. -Lua identifies these events with the following constants: -LUA_HOOKCALL, LUA_HOOKRET, -LUA_HOOKTAILCALL, LUA_HOOKLINE, -and LUA_HOOKCOUNT. -Moreover, for line events, the field currentline is also set. -To get the value of any other field in ar, -the hook must call lua_getinfo. - - -

-For call events, event can be LUA_HOOKCALL, -the normal value, or LUA_HOOKTAILCALL, for a tail call; -in this case, there will be no corresponding return event. - - -

-While Lua is running a hook, it disables other calls to hooks. -Therefore, if a hook calls back Lua to execute a function or a chunk, -this execution occurs without any calls to hooks. - - -

-Hook functions cannot have continuations, -that is, they cannot call lua_yieldk, -lua_pcallk, or lua_callk with a non-null k. - - -

-Hook functions can yield under the following conditions: -Only count and line events can yield -and they cannot yield any value; -to yield a hook function must finish its execution -calling lua_yield with nresults equal to zero. - - - - - -


lua_sethook

-[-0, +0, –] -

int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
- -

-Sets the debugging hook function. - - -

-Argument f is the hook function. -mask specifies on which events the hook will be called: -it is formed by a bitwise or of the constants -LUA_MASKCALL, -LUA_MASKRET, -LUA_MASKLINE, -and LUA_MASKCOUNT. -The count argument is only meaningful when the mask -includes LUA_MASKCOUNT. -For each event, the hook is called as explained below: - -

    - -
  • The call hook: is called when the interpreter calls a function. -The hook is called just after Lua enters the new function, -before the function gets its arguments. -
  • - -
  • The return hook: is called when the interpreter returns from a function. -The hook is called just before Lua leaves the function. -There is no standard way to access the values -to be returned by the function. -
  • - -
  • The line hook: is called when the interpreter is about to -start the execution of a new line of code, -or when it jumps back in the code (even to the same line). -(This event only happens while Lua is executing a Lua function.) -
  • - -
  • The count hook: is called after the interpreter executes every -count instructions. -(This event only happens while Lua is executing a Lua function.) -
  • - -
- -

-A hook is disabled by setting mask to zero. - - - - - -


lua_setlocal

-[-(0|1), +0, –] -

const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);
- -

-Sets the value of a local variable of a given activation record. -Parameters ar and n are as in lua_getlocal -(see lua_getlocal). -lua_setlocal assigns the value at the top of the stack -to the variable and returns its name. -It also pops the value from the stack. - - -

-Returns NULL (and pops nothing) -when the index is greater than -the number of active local variables. - - - - - -


lua_setupvalue

-[-(0|1), +0, –] -

const char *lua_setupvalue (lua_State *L, int funcindex, int n);
- -

-Sets the value of a closure's upvalue. -It assigns the value at the top of the stack -to the upvalue and returns its name. -It also pops the value from the stack. -Parameters funcindex and n are as in the lua_getupvalue -(see lua_getupvalue). - - -

-Returns NULL (and pops nothing) -when the index is greater than the number of upvalues. - - - - - -


lua_upvalueid

-[-0, +0, –] -

void *lua_upvalueid (lua_State *L, int funcindex, int n);
- -

-Returns an unique identifier for the upvalue numbered n -from the closure at index funcindex. -Parameters funcindex and n are as in the lua_getupvalue -(see lua_getupvalue) -(but n cannot be greater than the number of upvalues). - - -

-These unique identifiers allow a program to check whether different -closures share upvalues. -Lua closures that share an upvalue -(that is, that access a same external local variable) -will return identical ids for those upvalue indices. - - - - - -


lua_upvaluejoin

-[-0, +0, –] -

void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
-                                    int funcindex2, int n2);
- -

-Make the n1-th upvalue of the Lua closure at index funcindex1 -refer to the n2-th upvalue of the Lua closure at index funcindex2. - - - - - - - -

5 – The Auxiliary Library

- -

- -The auxiliary library provides several convenient functions -to interface C with Lua. -While the basic API provides the primitive functions for all -interactions between C and Lua, -the auxiliary library provides higher-level functions for some -common tasks. - - -

-All functions and types from the auxiliary library -are defined in header file lauxlib.h and -have a prefix luaL_. - - -

-All functions in the auxiliary library are built on -top of the basic API, -and so they provide nothing that cannot be done with that API. -Nevertheless, the use of the auxiliary library ensures -more consistency to your code. - - -

-Several functions in the auxiliary library use internally some -extra stack slots. -When a function in the auxiliary library uses less than five slots, -it does not check the stack size; -it simply assumes that there are enough slots. - - -

-Several functions in the auxiliary library are used to -check C function arguments. -Because the error message is formatted for arguments -(e.g., "bad argument #1"), -you should not use these functions for other stack values. - - -

-Functions called luaL_check* -always throw an error if the check is not satisfied. - - - -

5.1 – Functions and Types

- -

-Here we list all functions and types from the auxiliary library -in alphabetical order. - - - -


luaL_addchar

-[-?, +?, e] -

void luaL_addchar (luaL_Buffer *B, char c);
- -

-Adds the byte c to the buffer B -(see luaL_Buffer). - - - - - -


luaL_addlstring

-[-?, +?, e] -

void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
- -

-Adds the string pointed to by s with length l to -the buffer B -(see luaL_Buffer). -The string can contain embedded zeros. - - - - - -


luaL_addsize

-[-?, +?, e] -

void luaL_addsize (luaL_Buffer *B, size_t n);
- -

-Adds to the buffer B (see luaL_Buffer) -a string of length n previously copied to the -buffer area (see luaL_prepbuffer). - - - - - -


luaL_addstring

-[-?, +?, e] -

void luaL_addstring (luaL_Buffer *B, const char *s);
- -

-Adds the zero-terminated string pointed to by s -to the buffer B -(see luaL_Buffer). -The string cannot contain embedded zeros. - - - - - -


luaL_addvalue

-[-1, +?, e] -

void luaL_addvalue (luaL_Buffer *B);
- -

-Adds the value at the top of the stack -to the buffer B -(see luaL_Buffer). -Pops the value. - - -

-This is the only function on string buffers that can (and must) -be called with an extra element on the stack, -which is the value to be added to the buffer. - - - - - -


luaL_argcheck

-[-0, +0, v] -

void luaL_argcheck (lua_State *L,
-                    int cond,
-                    int arg,
-                    const char *extramsg);
- -

-Checks whether cond is true. -If not, raises an error with a standard message. - - - - - -


luaL_argerror

-[-0, +0, v] -

int luaL_argerror (lua_State *L, int arg, const char *extramsg);
- -

-Raises an error with a standard message -that includes extramsg as a comment. - - -

-This function never returns, -but it is an idiom to use it in C functions -as return luaL_argerror(args). - - - - - -


luaL_Buffer

-
typedef struct luaL_Buffer luaL_Buffer;
- -

-Type for a string buffer. - - -

-A string buffer allows C code to build Lua strings piecemeal. -Its pattern of use is as follows: - -

    - -
  • First declare a variable b of type luaL_Buffer.
  • - -
  • Then initialize it with a call luaL_buffinit(L, &b).
  • - -
  • -Then add string pieces to the buffer calling any of -the luaL_add* functions. -
  • - -
  • -Finish by calling luaL_pushresult(&b). -This call leaves the final string on the top of the stack. -
  • - -
- -

-If you know beforehand the total size of the resulting string, -you can use the buffer like this: - -

    - -
  • First declare a variable b of type luaL_Buffer.
  • - -
  • Then initialize it and preallocate a space of -size sz with a call luaL_buffinitsize(L, &b, sz).
  • - -
  • Then copy the string into that space.
  • - -
  • -Finish by calling luaL_pushresultsize(&b, sz), -where sz is the total size of the resulting string -copied into that space. -
  • - -
- -

-During its normal operation, -a string buffer uses a variable number of stack slots. -So, while using a buffer, you cannot assume that you know where -the top of the stack is. -You can use the stack between successive calls to buffer operations -as long as that use is balanced; -that is, -when you call a buffer operation, -the stack is at the same level -it was immediately after the previous buffer operation. -(The only exception to this rule is luaL_addvalue.) -After calling luaL_pushresult the stack is back to its -level when the buffer was initialized, -plus the final string on its top. - - - - - -


luaL_buffinit

-[-0, +0, –] -

void luaL_buffinit (lua_State *L, luaL_Buffer *B);
- -

-Initializes a buffer B. -This function does not allocate any space; -the buffer must be declared as a variable -(see luaL_Buffer). - - - - - -


luaL_buffinitsize

-[-?, +?, e] -

char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);
- -

-Equivalent to the sequence -luaL_buffinit, luaL_prepbuffsize. - - - - - -


luaL_callmeta

-[-0, +(0|1), e] -

int luaL_callmeta (lua_State *L, int obj, const char *e);
- -

-Calls a metamethod. - - -

-If the object at index obj has a metatable and this -metatable has a field e, -this function calls this field passing the object as its only argument. -In this case this function returns true and pushes onto the -stack the value returned by the call. -If there is no metatable or no metamethod, -this function returns false (without pushing any value on the stack). - - - - - -


luaL_checkany

-[-0, +0, v] -

void luaL_checkany (lua_State *L, int arg);
- -

-Checks whether the function has an argument -of any type (including nil) at position arg. - - - - - -


luaL_checkint

-[-0, +0, v] -

int luaL_checkint (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number cast to an int. - - - - - -


luaL_checkinteger

-[-0, +0, v] -

lua_Integer luaL_checkinteger (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number cast to a lua_Integer. - - - - - -


luaL_checklong

-[-0, +0, v] -

long luaL_checklong (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number cast to a long. - - - - - -


luaL_checklstring

-[-0, +0, v] -

const char *luaL_checklstring (lua_State *L, int arg, size_t *l);
- -

-Checks whether the function argument arg is a string -and returns this string; -if l is not NULL fills *l -with the string's length. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checknumber

-[-0, +0, v] -

lua_Number luaL_checknumber (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number. - - - - - -


luaL_checkoption

-[-0, +0, v] -

int luaL_checkoption (lua_State *L,
-                      int arg,
-                      const char *def,
-                      const char *const lst[]);
- -

-Checks whether the function argument arg is a string and -searches for this string in the array lst -(which must be NULL-terminated). -Returns the index in the array where the string was found. -Raises an error if the argument is not a string or -if the string cannot be found. - - -

-If def is not NULL, -the function uses def as a default value when -there is no argument arg or when this argument is nil. - - -

-This is a useful function for mapping strings to C enums. -(The usual convention in Lua libraries is -to use strings instead of numbers to select options.) - - - - - -


luaL_checkstack

-[-0, +0, v] -

void luaL_checkstack (lua_State *L, int sz, const char *msg);
- -

-Grows the stack size to top + sz elements, -raising an error if the stack cannot grow to that size. -msg is an additional text to go into the error message -(or NULL for no additional text). - - - - - -


luaL_checkstring

-[-0, +0, v] -

const char *luaL_checkstring (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a string -and returns this string. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checktype

-[-0, +0, v] -

void luaL_checktype (lua_State *L, int arg, int t);
- -

-Checks whether the function argument arg has type t. -See lua_type for the encoding of types for t. - - - - - -


luaL_checkudata

-[-0, +0, v] -

void *luaL_checkudata (lua_State *L, int arg, const char *tname);
- -

-Checks whether the function argument arg is a userdata -of the type tname (see luaL_newmetatable) and -returns the userdata address (see lua_touserdata). - - - - - -


luaL_checkunsigned

-[-0, +0, v] -

lua_Unsigned luaL_checkunsigned (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number cast to a lua_Unsigned. - - - - - -


luaL_checkversion

-[-0, +0, –] -

void luaL_checkversion (lua_State *L);
- -

-Checks whether the core running the call, -the core that created the Lua state, -and the code making the call are all using the same version of Lua. -Also checks whether the core running the call -and the core that created the Lua state -are using the same address space. - - - - - -


luaL_dofile

-[-0, +?, e] -

int luaL_dofile (lua_State *L, const char *filename);
- -

-Loads and runs the given file. -It is defined as the following macro: - -

-     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns false if there are no errors -or true in case of errors. - - - - - -


luaL_dostring

-[-0, +?, –] -

int luaL_dostring (lua_State *L, const char *str);
- -

-Loads and runs the given string. -It is defined as the following macro: - -

-     (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns false if there are no errors -or true in case of errors. - - - - - -


luaL_error

-[-0, +0, v] -

int luaL_error (lua_State *L, const char *fmt, ...);
- -

-Raises an error. -The error message format is given by fmt -plus any extra arguments, -following the same rules of lua_pushfstring. -It also adds at the beginning of the message the file name and -the line number where the error occurred, -if this information is available. - - -

-This function never returns, -but it is an idiom to use it in C functions -as return luaL_error(args). - - - - - -


luaL_execresult

-[-0, +3, e] -

int luaL_execresult (lua_State *L, int stat);
- -

-This function produces the return values for -process-related functions in the standard library -(os.execute and io.close). - - - - - -


luaL_fileresult

-[-0, +(1|3), e] -

int luaL_fileresult (lua_State *L, int stat, const char *fname);
- -

-This function produces the return values for -file-related functions in the standard library -(io.open, os.rename, file:seek, etc.). - - - - - -


luaL_getmetafield

-[-0, +(0|1), e] -

int luaL_getmetafield (lua_State *L, int obj, const char *e);
- -

-Pushes onto the stack the field e from the metatable -of the object at index obj. -If the object does not have a metatable, -or if the metatable does not have this field, -returns false and pushes nothing. - - - - - -


luaL_getmetatable

-[-0, +1, –] -

void luaL_getmetatable (lua_State *L, const char *tname);
- -

-Pushes onto the stack the metatable associated with name tname -in the registry (see luaL_newmetatable). - - - - - -


luaL_getsubtable

-[-0, +1, e] -

int luaL_getsubtable (lua_State *L, int idx, const char *fname);
- -

-Ensures that the value t[fname], -where t is the value at index idx, -is a table, -and pushes that table onto the stack. -Returns true if it finds a previous table there -and false if it creates a new table. - - - - - -


luaL_gsub

-[-0, +1, e] -

const char *luaL_gsub (lua_State *L,
-                       const char *s,
-                       const char *p,
-                       const char *r);
- -

-Creates a copy of string s by replacing -any occurrence of the string p -with the string r. -Pushes the resulting string on the stack and returns it. - - - - - -


luaL_len

-[-0, +0, e] -

int luaL_len (lua_State *L, int index);
- -

-Returns the "length" of the value at the given index -as a number; -it is equivalent to the '#' operator in Lua (see §3.4.6). -Raises an error if the result of the operation is not a number. -(This case only can happen through metamethods.) - - - - - -


luaL_loadbuffer

-[-0, +1, –] -

int luaL_loadbuffer (lua_State *L,
-                     const char *buff,
-                     size_t sz,
-                     const char *name);
- -

-Equivalent to luaL_loadbufferx with mode equal to NULL. - - - - - -


luaL_loadbufferx

-[-0, +1, –] -

int luaL_loadbufferx (lua_State *L,
-                      const char *buff,
-                      size_t sz,
-                      const char *name,
-                      const char *mode);
- -

-Loads a buffer as a Lua chunk. -This function uses lua_load to load the chunk in the -buffer pointed to by buff with size sz. - - -

-This function returns the same results as lua_load. -name is the chunk name, -used for debug information and error messages. -The string mode works as in function lua_load. - - - - - -


luaL_loadfile

-[-0, +1, e] -

int luaL_loadfile (lua_State *L, const char *filename);
- -

-Equivalent to luaL_loadfilex with mode equal to NULL. - - - - - -


luaL_loadfilex

-[-0, +1, e] -

int luaL_loadfilex (lua_State *L, const char *filename,
-                                            const char *mode);
- -

-Loads a file as a Lua chunk. -This function uses lua_load to load the chunk in the file -named filename. -If filename is NULL, -then it loads from the standard input. -The first line in the file is ignored if it starts with a #. - - -

-The string mode works as in function lua_load. - - -

-This function returns the same results as lua_load, -but it has an extra error code LUA_ERRFILE -if it cannot open/read the file or the file has a wrong mode. - - -

-As lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_loadstring

-[-0, +1, –] -

int luaL_loadstring (lua_State *L, const char *s);
- -

-Loads a string as a Lua chunk. -This function uses lua_load to load the chunk in -the zero-terminated string s. - - -

-This function returns the same results as lua_load. - - -

-Also as lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_newlib

-[-0, +1, e] -

void luaL_newlib (lua_State *L, const luaL_Reg *l);
- -

-Creates a new table and registers there -the functions in list l. -It is implemented as the following macro: - -

-     (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
-
- - - - -

luaL_newlibtable

-[-0, +1, e] -

void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);
- -

-Creates a new table with a size optimized -to store all entries in the array l -(but does not actually store them). -It is intended to be used in conjunction with luaL_setfuncs -(see luaL_newlib). - - -

-It is implemented as a macro. -The array l must be the actual array, -not a pointer to it. - - - - - -


luaL_newmetatable

-[-0, +1, e] -

int luaL_newmetatable (lua_State *L, const char *tname);
- -

-If the registry already has the key tname, -returns 0. -Otherwise, -creates a new table to be used as a metatable for userdata, -adds it to the registry with key tname, -and returns 1. - - -

-In both cases pushes onto the stack the final value associated -with tname in the registry. - - - - - -


luaL_newstate

-[-0, +0, –] -

lua_State *luaL_newstate (void);
- -

-Creates a new Lua state. -It calls lua_newstate with an -allocator based on the standard C realloc function -and then sets a panic function (see §4.6) that prints -an error message to the standard error output in case of fatal -errors. - - -

-Returns the new state, -or NULL if there is a memory allocation error. - - - - - -


luaL_openlibs

-[-0, +0, e] -

void luaL_openlibs (lua_State *L);
- -

-Opens all standard Lua libraries into the given state. - - - - - -


luaL_optint

-[-0, +0, v] -

int luaL_optint (lua_State *L, int arg, int d);
- -

-If the function argument arg is a number, -returns this number cast to an int. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optinteger

-[-0, +0, v] -

lua_Integer luaL_optinteger (lua_State *L,
-                             int arg,
-                             lua_Integer d);
- -

-If the function argument arg is a number, -returns this number cast to a lua_Integer. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optlong

-[-0, +0, v] -

long luaL_optlong (lua_State *L, int arg, long d);
- -

-If the function argument arg is a number, -returns this number cast to a long. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optlstring

-[-0, +0, v] -

const char *luaL_optlstring (lua_State *L,
-                             int arg,
-                             const char *d,
-                             size_t *l);
- -

-If the function argument arg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - -

-If l is not NULL, -fills the position *l with the result's length. - - - - - -


luaL_optnumber

-[-0, +0, v] -

lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);
- -

-If the function argument arg is a number, -returns this number. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optstring

-[-0, +0, v] -

const char *luaL_optstring (lua_State *L,
-                            int arg,
-                            const char *d);
- -

-If the function argument arg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optunsigned

-[-0, +0, v] -

lua_Unsigned luaL_optunsigned (lua_State *L,
-                               int arg,
-                               lua_Unsigned u);
- -

-If the function argument arg is a number, -returns this number cast to a lua_Unsigned. -If this argument is absent or is nil, -returns u. -Otherwise, raises an error. - - - - - -


luaL_prepbuffer

-[-?, +?, e] -

char *luaL_prepbuffer (luaL_Buffer *B);
- -

-Equivalent to luaL_prepbuffsize -with the predefined size LUAL_BUFFERSIZE. - - - - - -


luaL_prepbuffsize

-[-?, +?, e] -

char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);
- -

-Returns an address to a space of size sz -where you can copy a string to be added to buffer B -(see luaL_Buffer). -After copying the string into this space you must call -luaL_addsize with the size of the string to actually add -it to the buffer. - - - - - -


luaL_pushresult

-[-?, +1, e] -

void luaL_pushresult (luaL_Buffer *B);
- -

-Finishes the use of buffer B leaving the final string on -the top of the stack. - - - - - -


luaL_pushresultsize

-[-?, +1, e] -

void luaL_pushresultsize (luaL_Buffer *B, size_t sz);
- -

-Equivalent to the sequence luaL_addsize, luaL_pushresult. - - - - - -


luaL_ref

-[-1, +0, e] -

int luaL_ref (lua_State *L, int t);
- -

-Creates and returns a reference, -in the table at index t, -for the object at the top of the stack (and pops the object). - - -

-A reference is a unique integer key. -As long as you do not manually add integer keys into table t, -luaL_ref ensures the uniqueness of the key it returns. -You can retrieve an object referred by reference r -by calling lua_rawgeti(L, t, r). -Function luaL_unref frees a reference and its associated object. - - -

-If the object at the top of the stack is nil, -luaL_ref returns the constant LUA_REFNIL. -The constant LUA_NOREF is guaranteed to be different -from any reference returned by luaL_ref. - - - - - -


luaL_Reg

-
typedef struct luaL_Reg {
-  const char *name;
-  lua_CFunction func;
-} luaL_Reg;
- -

-Type for arrays of functions to be registered by -luaL_setfuncs. -name is the function name and func is a pointer to -the function. -Any array of luaL_Reg must end with an sentinel entry -in which both name and func are NULL. - - - - - -


luaL_requiref

-[-0, +1, e] -

void luaL_requiref (lua_State *L, const char *modname,
-                    lua_CFunction openf, int glb);
- -

-Calls function openf with string modname as an argument -and sets the call result in package.loaded[modname], -as if that function has been called through require. - - -

-If glb is true, -also stores the result into global modname. - - -

-Leaves a copy of that result on the stack. - - - - - -


luaL_setfuncs

-[-nup, +0, e] -

void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
- -

-Registers all functions in the array l -(see luaL_Reg) into the table on the top of the stack -(below optional upvalues, see next). - - -

-When nup is not zero, -all functions are created sharing nup upvalues, -which must be previously pushed on the stack -on top of the library table. -These values are popped from the stack after the registration. - - - - - -


luaL_setmetatable

-[-0, +0, –] -

void luaL_setmetatable (lua_State *L, const char *tname);
- -

-Sets the metatable of the object at the top of the stack -as the metatable associated with name tname -in the registry (see luaL_newmetatable). - - - - - -


luaL_testudata

-[-0, +0, e] -

void *luaL_testudata (lua_State *L, int arg, const char *tname);
- -

-This function works like luaL_checkudata, -except that, when the test fails, -it returns NULL instead of throwing an error. - - - - - -


luaL_tolstring

-[-0, +1, e] -

const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
- -

-Converts any Lua value at the given index to a C string -in a reasonable format. -The resulting string is pushed onto the stack and also -returned by the function. -If len is not NULL, -the function also sets *len with the string length. - - -

-If the value has a metatable with a "__tostring" field, -then luaL_tolstring calls the corresponding metamethod -with the value as argument, -and uses the result of the call as its result. - - - - - -


luaL_traceback

-[-0, +1, e] -

void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
-                     int level);
- -

-Creates and pushes a traceback of the stack L1. -If msg is not NULL it is appended -at the beginning of the traceback. -The level parameter tells at which level -to start the traceback. - - - - - -


luaL_typename

-[-0, +0, –] -

const char *luaL_typename (lua_State *L, int index);
- -

-Returns the name of the type of the value at the given index. - - - - - -


luaL_unref

-[-0, +0, –] -

void luaL_unref (lua_State *L, int t, int ref);
- -

-Releases reference ref from the table at index t -(see luaL_ref). -The entry is removed from the table, -so that the referred object can be collected. -The reference ref is also freed to be used again. - - -

-If ref is LUA_NOREF or LUA_REFNIL, -luaL_unref does nothing. - - - - - -


luaL_where

-[-0, +1, e] -

void luaL_where (lua_State *L, int lvl);
- -

-Pushes onto the stack a string identifying the current position -of the control at level lvl in the call stack. -Typically this string has the following format: - -

-     chunkname:currentline:
-

-Level 0 is the running function, -level 1 is the function that called the running function, -etc. - - -

-This function is used to build a prefix for error messages. - - - - - - - -

6 – Standard Libraries

- -

-The standard Lua libraries provide useful functions -that are implemented directly through the C API. -Some of these functions provide essential services to the language -(e.g., type and getmetatable); -others provide access to "outside" services (e.g., I/O); -and others could be implemented in Lua itself, -but are quite useful or have critical performance requirements that -deserve an implementation in C (e.g., table.sort). - - -

-All libraries are implemented through the official C API -and are provided as separate C modules. -Currently, Lua has the following standard libraries: - -

    - -
  • basic library (§6.1);
  • - -
  • coroutine library (§6.2);
  • - -
  • package library (§6.3);
  • - -
  • string manipulation (§6.4);
  • - -
  • table manipulation (§6.5);
  • - -
  • mathematical functions (§6.6) (sin, log, etc.);
  • - -
  • bitwise operations (§6.7);
  • - -
  • input and output (§6.8);
  • - -
  • operating system facilities (§6.9);
  • - -
  • debug facilities (§6.10).
  • - -

-Except for the basic and the package libraries, -each library provides all its functions as fields of a global table -or as methods of its objects. - - -

-To have access to these libraries, -the C host program should call the luaL_openlibs function, -which opens all standard libraries. -Alternatively, -the host program can open them individually by using -luaL_requiref to call -luaopen_base (for the basic library), -luaopen_package (for the package library), -luaopen_coroutine (for the coroutine library), -luaopen_string (for the string library), -luaopen_table (for the table library), -luaopen_math (for the mathematical library), -luaopen_bit32 (for the bit library), -luaopen_io (for the I/O library), -luaopen_os (for the Operating System library), -and luaopen_debug (for the debug library). -These functions are declared in lualib.h. - - - -

6.1 – Basic Functions

- -

-The basic library provides core functions to Lua. -If you do not include this library in your application, -you should check carefully whether you need to provide -implementations for some of its facilities. - - -

-


assert (v [, message])

-Issues an error when -the value of its argument v is false (i.e., nil or false); -otherwise, returns all its arguments. -message is an error message; -when absent, it defaults to "assertion failed!" - - - - -

-


collectgarbage ([opt [, arg]])

- - -

-This function is a generic interface to the garbage collector. -It performs different functions according to its first argument, opt: - -

    - -
  • "collect": -performs a full garbage-collection cycle. -This is the default option. -
  • - -
  • "stop": -stops automatic execution of the garbage collector. -The collector will run only when explicitly invoked, -until a call to restart it. -
  • - -
  • "restart": -restarts automatic execution of the garbage collector. -
  • - -
  • "count": -returns the total memory in use by Lua (in Kbytes) and -a second value with the total memory in bytes modulo 1024. -The first value has a fractional part, -so the following equality is always true: - -
    -     k, b = collectgarbage("count")
    -     assert(k*1024 == math.floor(k)*1024 + b)
    -

    -(The second result is useful when Lua is compiled -with a non floating-point type for numbers.) -

  • - -
  • "step": -performs a garbage-collection step. -The step "size" is controlled by arg -(larger values mean more steps) in a non-specified way. -If you want to control the step size -you must experimentally tune the value of arg. -Returns true if the step finished a collection cycle. -
  • - -
  • "setpause": -sets arg as the new value for the pause of -the collector (see §2.5). -Returns the previous value for pause. -
  • - -
  • "setstepmul": -sets arg as the new value for the step multiplier of -the collector (see §2.5). -Returns the previous value for step. -
  • - -
  • "isrunning": -returns a boolean that tells whether the collector is running -(i.e., not stopped). -
  • - -
  • "generational": -changes the collector to generational mode. -This is an experimental feature (see §2.5). -
  • - -
  • "incremental": -changes the collector to incremental mode. -This is the default mode. -
  • - -
- - - -

-


dofile ([filename])

-Opens the named file and executes its contents as a Lua chunk. -When called without arguments, -dofile executes the contents of the standard input (stdin). -Returns all values returned by the chunk. -In case of errors, dofile propagates the error -to its caller (that is, dofile does not run in protected mode). - - - - -

-


error (message [, level])

-Terminates the last protected function called -and returns message as the error message. -Function error never returns. - - -

-Usually, error adds some information about the error position -at the beginning of the message, if the message is a string. -The level argument specifies how to get the error position. -With level 1 (the default), the error position is where the -error function was called. -Level 2 points the error to where the function -that called error was called; and so on. -Passing a level 0 avoids the addition of error position information -to the message. - - - - -

-


_G

-A global variable (not a function) that -holds the global environment (see §2.2). -Lua itself does not use this variable; -changing its value does not affect any environment, -nor vice-versa. - - - - -

-


getmetatable (object)

- - -

-If object does not have a metatable, returns nil. -Otherwise, -if the object's metatable has a "__metatable" field, -returns the associated value. -Otherwise, returns the metatable of the given object. - - - - -

-


ipairs (t)

- - -

-If t has a metamethod __ipairs, -calls it with t as argument and returns the first three -results from the call. - - -

-Otherwise, -returns three values: an iterator function, the table t, and 0, -so that the construction - -

-     for i,v in ipairs(t) do body end
-

-will iterate over the pairs (1,t[1]), (2,t[2]), ..., -up to the first integer key absent from the table. - - - - -

-


load (ld [, source [, mode [, env]]])

- - -

-Loads a chunk. - - -

-If ld is a string, the chunk is this string. -If ld is a function, -load calls it repeatedly to get the chunk pieces. -Each call to ld must return a string that concatenates -with previous results. -A return of an empty string, nil, or no value signals the end of the chunk. - - -

-If there are no syntactic errors, -returns the compiled chunk as a function; -otherwise, returns nil plus the error message. - - -

-If the resulting function has upvalues, -the first upvalue is set to the value of env, -if that parameter is given, -or to the value of the global environment. -(When you load a main chunk, -the resulting function will always have exactly one upvalue, -the _ENV variable (see §2.2). -When you load a binary chunk created from a function (see string.dump), -the resulting function can have arbitrary upvalues.) - - -

-source is used as the source of the chunk for error messages -and debug information (see §4.9). -When absent, -it defaults to ld, if ld is a string, -or to "=(load)" otherwise. - - -

-The string mode controls whether the chunk can be text or binary -(that is, a precompiled chunk). -It may be the string "b" (only binary chunks), -"t" (only text chunks), -or "bt" (both binary and text). -The default is "bt". - - - - -

-


loadfile ([filename [, mode [, env]]])

- - -

-Similar to load, -but gets the chunk from file filename -or from the standard input, -if no file name is given. - - - - -

-


next (table [, index])

- - -

-Allows a program to traverse all fields of a table. -Its first argument is a table and its second argument -is an index in this table. -next returns the next index of the table -and its associated value. -When called with nil as its second argument, -next returns an initial index -and its associated value. -When called with the last index, -or with nil in an empty table, -next returns nil. -If the second argument is absent, then it is interpreted as nil. -In particular, -you can use next(t) to check whether a table is empty. - - -

-The order in which the indices are enumerated is not specified, -even for numeric indices. -(To traverse a table in numeric order, -use a numerical for.) - - -

-The behavior of next is undefined if, -during the traversal, -you assign any value to a non-existent field in the table. -You may however modify existing fields. -In particular, you may clear existing fields. - - - - -

-


pairs (t)

- - -

-If t has a metamethod __pairs, -calls it with t as argument and returns the first three -results from the call. - - -

-Otherwise, -returns three values: the next function, the table t, and nil, -so that the construction - -

-     for k,v in pairs(t) do body end
-

-will iterate over all key–value pairs of table t. - - -

-See function next for the caveats of modifying -the table during its traversal. - - - - -

-


pcall (f [, arg1, ···])

- - -

-Calls function f with -the given arguments in protected mode. -This means that any error inside f is not propagated; -instead, pcall catches the error -and returns a status code. -Its first result is the status code (a boolean), -which is true if the call succeeds without errors. -In such case, pcall also returns all results from the call, -after this first result. -In case of any error, pcall returns false plus the error message. - - - - -

-


print (···)

-Receives any number of arguments -and prints their values to stdout, -using the tostring function to convert each argument to a string. -print is not intended for formatted output, -but only as a quick way to show a value, -for instance for debugging. -For complete control over the output, -use string.format and io.write. - - - - -

-


rawequal (v1, v2)

-Checks whether v1 is equal to v2, -without invoking any metamethod. -Returns a boolean. - - - - -

-


rawget (table, index)

-Gets the real value of table[index], -without invoking any metamethod. -table must be a table; -index may be any value. - - - - -

-


rawlen (v)

-Returns the length of the object v, -which must be a table or a string, -without invoking any metamethod. -Returns an integer number. - - - - -

-


rawset (table, index, value)

-Sets the real value of table[index] to value, -without invoking any metamethod. -table must be a table, -index any value different from nil and NaN, -and value any Lua value. - - -

-This function returns table. - - - - -

-


select (index, ···)

- - -

-If index is a number, -returns all arguments after argument number index; -a negative number indexes from the end (-1 is the last argument). -Otherwise, index must be the string "#", -and select returns the total number of extra arguments it received. - - - - -

-


setmetatable (table, metatable)

- - -

-Sets the metatable for the given table. -(You cannot change the metatable of other types from Lua, only from C.) -If metatable is nil, -removes the metatable of the given table. -If the original metatable has a "__metatable" field, -raises an error. - - -

-This function returns table. - - - - -

-


tonumber (e [, base])

- - -

-When called with no base, -tonumber tries to convert its argument to a number. -If the argument is already a number or -a string convertible to a number (see §3.4.2), -then tonumber returns this number; -otherwise, it returns nil. - - -

-When called with base, -then e should be a string to be interpreted as -an integer numeral in that base. -The base may be any integer between 2 and 36, inclusive. -In bases above 10, the letter 'A' (in either upper or lower case) -represents 10, 'B' represents 11, and so forth, -with 'Z' representing 35. -If the string e is not a valid numeral in the given base, -the function returns nil. - - - - -

-


tostring (v)

-Receives a value of any type and -converts it to a string in a reasonable format. -(For complete control of how numbers are converted, -use string.format.) - - -

-If the metatable of v has a "__tostring" field, -then tostring calls the corresponding value -with v as argument, -and uses the result of the call as its result. - - - - -

-


type (v)

-Returns the type of its only argument, coded as a string. -The possible results of this function are -"nil" (a string, not the value nil), -"number", -"string", -"boolean", -"table", -"function", -"thread", -and "userdata". - - - - -

-


_VERSION

-A global variable (not a function) that -holds a string containing the current interpreter version. -The current contents of this variable is "Lua 5.2". - - - - -

-


xpcall (f, msgh [, arg1, ···])

- - -

-This function is similar to pcall, -except that it sets a new message handler msgh. - - - - - - - -

6.2 – Coroutine Manipulation

- -

-The operations related to coroutines comprise a sub-library of -the basic library and come inside the table coroutine. -See §2.6 for a general description of coroutines. - - -

-


coroutine.create (f)

- - -

-Creates a new coroutine, with body f. -f must be a Lua function. -Returns this new coroutine, -an object with type "thread". - - - - -

-


coroutine.resume (co [, val1, ···])

- - -

-Starts or continues the execution of coroutine co. -The first time you resume a coroutine, -it starts running its body. -The values val1, ... are passed -as the arguments to the body function. -If the coroutine has yielded, -resume restarts it; -the values val1, ... are passed -as the results from the yield. - - -

-If the coroutine runs without any errors, -resume returns true plus any values passed to yield -(if the coroutine yields) or any values returned by the body function -(if the coroutine terminates). -If there is any error, -resume returns false plus the error message. - - - - -

-


coroutine.running ()

- - -

-Returns the running coroutine plus a boolean, -true when the running coroutine is the main one. - - - - -

-


coroutine.status (co)

- - -

-Returns the status of coroutine co, as a string: -"running", -if the coroutine is running (that is, it called status); -"suspended", if the coroutine is suspended in a call to yield, -or if it has not started running yet; -"normal" if the coroutine is active but not running -(that is, it has resumed another coroutine); -and "dead" if the coroutine has finished its body function, -or if it has stopped with an error. - - - - -

-


coroutine.wrap (f)

- - -

-Creates a new coroutine, with body f. -f must be a Lua function. -Returns a function that resumes the coroutine each time it is called. -Any arguments passed to the function behave as the -extra arguments to resume. -Returns the same values returned by resume, -except the first boolean. -In case of error, propagates the error. - - - - -

-


coroutine.yield (···)

- - -

-Suspends the execution of the calling coroutine. -Any arguments to yield are passed as extra results to resume. - - - - - - - -

6.3 – Modules

- -

-The package library provides basic -facilities for loading modules in Lua. -It exports one function directly in the global environment: -require. -Everything else is exported in a table package. - - -

-


require (modname)

- - -

-Loads the given module. -The function starts by looking into the package.loaded table -to determine whether modname is already loaded. -If it is, then require returns the value stored -at package.loaded[modname]. -Otherwise, it tries to find a loader for the module. - - -

-To find a loader, -require is guided by the package.searchers sequence. -By changing this sequence, -we can change how require looks for a module. -The following explanation is based on the default configuration -for package.searchers. - - -

-First require queries package.preload[modname]. -If it has a value, -this value (which should be a function) is the loader. -Otherwise require searches for a Lua loader using the -path stored in package.path. -If that also fails, it searches for a C loader using the -path stored in package.cpath. -If that also fails, -it tries an all-in-one loader (see package.searchers). - - -

-Once a loader is found, -require calls the loader with two arguments: -modname and an extra value dependent on how it got the loader. -(If the loader came from a file, -this extra value is the file name.) -If the loader returns any non-nil value, -require assigns the returned value to package.loaded[modname]. -If the loader does not return a non-nil value and -has not assigned any value to package.loaded[modname], -then require assigns true to this entry. -In any case, require returns the -final value of package.loaded[modname]. - - -

-If there is any error loading or running the module, -or if it cannot find any loader for the module, -then require raises an error. - - - - -

-


package.config

- - -

-A string describing some compile-time configurations for packages. -This string is a sequence of lines: - -

    - -
  • The first line is the directory separator string. -Default is '\' for Windows and '/' for all other systems.
  • - -
  • The second line is the character that separates templates in a path. -Default is ';'.
  • - -
  • The third line is the string that marks the -substitution points in a template. -Default is '?'.
  • - -
  • The fourth line is a string that, in a path in Windows, -is replaced by the executable's directory. -Default is '!'.
  • - -
  • The fifth line is a mark to ignore all text before it -when building the luaopen_ function name. -Default is '-'.
  • - -
- - - -

-


package.cpath

- - -

-The path used by require to search for a C loader. - - -

-Lua initializes the C path package.cpath in the same way -it initializes the Lua path package.path, -using the environment variable LUA_CPATH_5_2 -or the environment variable LUA_CPATH -or a default path defined in luaconf.h. - - - - -

-


package.loaded

- - -

-A table used by require to control which -modules are already loaded. -When you require a module modname and -package.loaded[modname] is not false, -require simply returns the value stored there. - - -

-This variable is only a reference to the real table; -assignments to this variable do not change the -table used by require. - - - - -

-


package.loadlib (libname, funcname)

- - -

-Dynamically links the host program with the C library libname. - - -

-If funcname is "*", -then it only links with the library, -making the symbols exported by the library -available to other dynamically linked libraries. -Otherwise, -it looks for a function funcname inside the library -and returns this function as a C function. -So, funcname must follow the lua_CFunction prototype -(see lua_CFunction). - - -

-This is a low-level function. -It completely bypasses the package and module system. -Unlike require, -it does not perform any path searching and -does not automatically adds extensions. -libname must be the complete file name of the C library, -including if necessary a path and an extension. -funcname must be the exact name exported by the C library -(which may depend on the C compiler and linker used). - - -

-This function is not supported by Standard C. -As such, it is only available on some platforms -(Windows, Linux, Mac OS X, Solaris, BSD, -plus other Unix systems that support the dlfcn standard). - - - - -

-


package.path

- - -

-The path used by require to search for a Lua loader. - - -

-At start-up, Lua initializes this variable with -the value of the environment variable LUA_PATH_5_2 or -the environment variable LUA_PATH or -with a default path defined in luaconf.h, -if those environment variables are not defined. -Any ";;" in the value of the environment variable -is replaced by the default path. - - - - -

-


package.preload

- - -

-A table to store loaders for specific modules -(see require). - - -

-This variable is only a reference to the real table; -assignments to this variable do not change the -table used by require. - - - - -

-


package.searchers

- - -

-A table used by require to control how to load modules. - - -

-Each entry in this table is a searcher function. -When looking for a module, -require calls each of these searchers in ascending order, -with the module name (the argument given to require) as its -sole parameter. -The function can return another function (the module loader) -plus an extra value that will be passed to that loader, -or a string explaining why it did not find that module -(or nil if it has nothing to say). - - -

-Lua initializes this table with four searcher functions. - - -

-The first searcher simply looks for a loader in the -package.preload table. - - -

-The second searcher looks for a loader as a Lua library, -using the path stored at package.path. -The search is done as described in function package.searchpath. - - -

-The third searcher looks for a loader as a C library, -using the path given by the variable package.cpath. -Again, -the search is done as described in function package.searchpath. -For instance, -if the C path is the string - -

-     "./?.so;./?.dll;/usr/local/?/init.so"
-

-the searcher for module foo -will try to open the files ./foo.so, ./foo.dll, -and /usr/local/foo/init.so, in that order. -Once it finds a C library, -this searcher first uses a dynamic link facility to link the -application with the library. -Then it tries to find a C function inside the library to -be used as the loader. -The name of this C function is the string "luaopen_" -concatenated with a copy of the module name where each dot -is replaced by an underscore. -Moreover, if the module name has a hyphen, -its prefix up to (and including) the first hyphen is removed. -For instance, if the module name is a.v1-b.c, -the function name will be luaopen_b_c. - - -

-The fourth searcher tries an all-in-one loader. -It searches the C path for a library for -the root name of the given module. -For instance, when requiring a.b.c, -it will search for a C library for a. -If found, it looks into it for an open function for -the submodule; -in our example, that would be luaopen_a_b_c. -With this facility, a package can pack several C submodules -into one single library, -with each submodule keeping its original open function. - - -

-All searchers except the first one (preload) return as the extra value -the file name where the module was found, -as returned by package.searchpath. -The first searcher returns no extra value. - - - - -

-


package.searchpath (name, path [, sep [, rep]])

- - -

-Searches for the given name in the given path. - - -

-A path is a string containing a sequence of -templates separated by semicolons. -For each template, -the function replaces each interrogation mark (if any) -in the template with a copy of name -wherein all occurrences of sep -(a dot, by default) -were replaced by rep -(the system's directory separator, by default), -and then tries to open the resulting file name. - - -

-For instance, if the path is the string - -

-     "./?.lua;./?.lc;/usr/local/?/init.lua"
-

-the search for the name foo.a -will try to open the files -./foo/a.lua, ./foo/a.lc, and -/usr/local/foo/a/init.lua, in that order. - - -

-Returns the resulting name of the first file that it can -open in read mode (after closing the file), -or nil plus an error message if none succeeds. -(This error message lists all file names it tried to open.) - - - - - - - -

6.4 – String Manipulation

- -

-This library provides generic functions for string manipulation, -such as finding and extracting substrings, and pattern matching. -When indexing a string in Lua, the first character is at position 1 -(not at 0, as in C). -Indices are allowed to be negative and are interpreted as indexing backwards, -from the end of the string. -Thus, the last character is at position -1, and so on. - - -

-The string library provides all its functions inside the table -string. -It also sets a metatable for strings -where the __index field points to the string table. -Therefore, you can use the string functions in object-oriented style. -For instance, string.byte(s,i) -can be written as s:byte(i). - - -

-The string library assumes one-byte character encodings. - - -

-


string.byte (s [, i [, j]])

-Returns the internal numerical codes of the characters s[i], -s[i+1], ..., s[j]. -The default value for i is 1; -the default value for j is i. -These indices are corrected -following the same rules of function string.sub. - - -

-Numerical codes are not necessarily portable across platforms. - - - - -

-


string.char (···)

-Receives zero or more integers. -Returns a string with length equal to the number of arguments, -in which each character has the internal numerical code equal -to its corresponding argument. - - -

-Numerical codes are not necessarily portable across platforms. - - - - -

-


string.dump (function)

- - -

-Returns a string containing a binary representation of the given function, -so that a later load on this string returns -a copy of the function (but with new upvalues). - - - - -

-


string.find (s, pattern [, init [, plain]])

- - -

-Looks for the first match of -pattern in the string s. -If it finds a match, then find returns the indices of s -where this occurrence starts and ends; -otherwise, it returns nil. -A third, optional numerical argument init specifies -where to start the search; -its default value is 1 and can be negative. -A value of true as a fourth, optional argument plain -turns off the pattern matching facilities, -so the function does a plain "find substring" operation, -with no characters in pattern being considered magic. -Note that if plain is given, then init must be given as well. - - -

-If the pattern has captures, -then in a successful match -the captured values are also returned, -after the two indices. - - - - -

-


string.format (formatstring, ···)

- - -

-Returns a formatted version of its variable number of arguments -following the description given in its first argument (which must be a string). -The format string follows the same rules as the ISO C function sprintf. -The only differences are that the options/modifiers -*, h, L, l, n, -and p are not supported -and that there is an extra option, q. -The q option formats a string between double quotes, -using escape sequences when necessary to ensure that -it can safely be read back by the Lua interpreter. -For instance, the call - -

-     string.format('%q', 'a string with "quotes" and \n new line')
-

-may produce the string: - -

-     "a string with \"quotes\" and \
-      new line"
-
- -

-Options -A and a (when available), -E, e, f, -G, and g all expect a number as argument. -Options c, d, -i, o, u, X, and x -also expect a number, -but the range of that number may be limited by -the underlying C implementation. -For options o, u, X, and x, -the number cannot be negative. -Option q expects a string; -option s expects a string without embedded zeros. -If the argument to option s is not a string, -it is converted to one following the same rules of tostring. - - - - -

-


string.gmatch (s, pattern)

-Returns an iterator function that, -each time it is called, -returns the next captures from pattern over the string s. -If pattern specifies no captures, -then the whole match is produced in each call. - - -

-As an example, the following loop -will iterate over all the words from string s, -printing one per line: - -

-     s = "hello world from Lua"
-     for w in string.gmatch(s, "%a+") do
-       print(w)
-     end
-

-The next example collects all pairs key=value from the -given string into a table: - -

-     t = {}
-     s = "from=world, to=Lua"
-     for k, v in string.gmatch(s, "(%w+)=(%w+)") do
-       t[k] = v
-     end
-
- -

-For this function, a caret '^' at the start of a pattern does not -work as an anchor, as this would prevent the iteration. - - - - -

-


string.gsub (s, pattern, repl [, n])

-Returns a copy of s -in which all (or the first n, if given) -occurrences of the pattern have been -replaced by a replacement string specified by repl, -which can be a string, a table, or a function. -gsub also returns, as its second value, -the total number of matches that occurred. -The name gsub comes from Global SUBstitution. - - -

-If repl is a string, then its value is used for replacement. -The character % works as an escape character: -any sequence in repl of the form %d, -with d between 1 and 9, -stands for the value of the d-th captured substring. -The sequence %0 stands for the whole match. -The sequence %% stands for a single %. - - -

-If repl is a table, then the table is queried for every match, -using the first capture as the key. - - -

-If repl is a function, then this function is called every time a -match occurs, with all captured substrings passed as arguments, -in order. - - -

-In any case, -if the pattern specifies no captures, -then it behaves as if the whole pattern was inside a capture. - - -

-If the value returned by the table query or by the function call -is a string or a number, -then it is used as the replacement string; -otherwise, if it is false or nil, -then there is no replacement -(that is, the original match is kept in the string). - - -

-Here are some examples: - -

-     x = string.gsub("hello world", "(%w+)", "%1 %1")
-     --> x="hello hello world world"
-     
-     x = string.gsub("hello world", "%w+", "%0 %0", 1)
-     --> x="hello hello world"
-     
-     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
-     --> x="world hello Lua from"
-     
-     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
-     --> x="home = /home/roberto, user = roberto"
-     
-     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
-           return load(s)()
-         end)
-     --> x="4+5 = 9"
-     
-     local t = {name="lua", version="5.2"}
-     x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
-     --> x="lua-5.2.tar.gz"
-
- - - -

-


string.len (s)

-Receives a string and returns its length. -The empty string "" has length 0. -Embedded zeros are counted, -so "a\000bc\000" has length 5. - - - - -

-


string.lower (s)

-Receives a string and returns a copy of this string with all -uppercase letters changed to lowercase. -All other characters are left unchanged. -The definition of what an uppercase letter is depends on the current locale. - - - - -

-


string.match (s, pattern [, init])

-Looks for the first match of -pattern in the string s. -If it finds one, then match returns -the captures from the pattern; -otherwise it returns nil. -If pattern specifies no captures, -then the whole match is returned. -A third, optional numerical argument init specifies -where to start the search; -its default value is 1 and can be negative. - - - - -

-


string.rep (s, n [, sep])

-Returns a string that is the concatenation of n copies of -the string s separated by the string sep. -The default value for sep is the empty string -(that is, no separator). - - - - -

-


string.reverse (s)

-Returns a string that is the string s reversed. - - - - -

-


string.sub (s, i [, j])

-Returns the substring of s that -starts at i and continues until j; -i and j can be negative. -If j is absent, then it is assumed to be equal to -1 -(which is the same as the string length). -In particular, -the call string.sub(s,1,j) returns a prefix of s -with length j, -and string.sub(s, -i) returns a suffix of s -with length i. - - -

-If, after the translation of negative indices, -i is less than 1, -it is corrected to 1. -If j is greater than the string length, -it is corrected to that length. -If, after these corrections, -i is greater than j, -the function returns the empty string. - - - - -

-


string.upper (s)

-Receives a string and returns a copy of this string with all -lowercase letters changed to uppercase. -All other characters are left unchanged. -The definition of what a lowercase letter is depends on the current locale. - - - -

6.4.1 – Patterns

- - -

Character Class:

-A character class is used to represent a set of characters. -The following combinations are allowed in describing a character class: - -

    - -
  • x: -(where x is not one of the magic characters -^$()%.[]*+-?) -represents the character x itself. -
  • - -
  • .: (a dot) represents all characters.
  • - -
  • %a: represents all letters.
  • - -
  • %c: represents all control characters.
  • - -
  • %d: represents all digits.
  • - -
  • %g: represents all printable characters except space.
  • - -
  • %l: represents all lowercase letters.
  • - -
  • %p: represents all punctuation characters.
  • - -
  • %s: represents all space characters.
  • - -
  • %u: represents all uppercase letters.
  • - -
  • %w: represents all alphanumeric characters.
  • - -
  • %x: represents all hexadecimal digits.
  • - -
  • %x: (where x is any non-alphanumeric character) -represents the character x. -This is the standard way to escape the magic characters. -Any punctuation character (even the non magic) -can be preceded by a '%' -when used to represent itself in a pattern. -
  • - -
  • [set]: -represents the class which is the union of all -characters in set. -A range of characters can be specified by -separating the end characters of the range, -in ascending order, with a '-', -All classes %x described above can also be used as -components in set. -All other characters in set represent themselves. -For example, [%w_] (or [_%w]) -represents all alphanumeric characters plus the underscore, -[0-7] represents the octal digits, -and [0-7%l%-] represents the octal digits plus -the lowercase letters plus the '-' character. - - -

    -The interaction between ranges and classes is not defined. -Therefore, patterns like [%a-z] or [a-%%] -have no meaning. -

  • - -
  • [^set]: -represents the complement of set, -where set is interpreted as above. -
  • - -

-For all classes represented by single letters (%a, %c, etc.), -the corresponding uppercase letter represents the complement of the class. -For instance, %S represents all non-space characters. - - -

-The definitions of letter, space, and other character groups -depend on the current locale. -In particular, the class [a-z] may not be equivalent to %l. - - - - - -

Pattern Item:

-A pattern item can be - -

    - -
  • -a single character class, -which matches any single character in the class; -
  • - -
  • -a single character class followed by '*', -which matches 0 or more repetitions of characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '+', -which matches 1 or more repetitions of characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '-', -which also matches 0 or more repetitions of characters in the class. -Unlike '*', -these repetition items will always match the shortest possible sequence; -
  • - -
  • -a single character class followed by '?', -which matches 0 or 1 occurrence of a character in the class; -
  • - -
  • -%n, for n between 1 and 9; -such item matches a substring equal to the n-th captured string -(see below); -
  • - -
  • -%bxy, where x and y are two distinct characters; -such item matches strings that start with x, end with y, -and where the x and y are balanced. -This means that, if one reads the string from left to right, -counting +1 for an x and -1 for a y, -the ending y is the first y where the count reaches 0. -For instance, the item %b() matches expressions with -balanced parentheses. -
  • - -
  • -%f[set], a frontier pattern; -such item matches an empty string at any position such that -the next character belongs to set -and the previous character does not belong to set. -The set set is interpreted as previously described. -The beginning and the end of the subject are handled as if -they were the character '\0'. -
  • - -
- - - - -

Pattern:

-A pattern is a sequence of pattern items. -A caret '^' at the beginning of a pattern anchors the match at the -beginning of the subject string. -A '$' at the end of a pattern anchors the match at the -end of the subject string. -At other positions, -'^' and '$' have no special meaning and represent themselves. - - - - - -

Captures:

-A pattern can contain sub-patterns enclosed in parentheses; -they describe captures. -When a match succeeds, the substrings of the subject string -that match captures are stored (captured) for future use. -Captures are numbered according to their left parentheses. -For instance, in the pattern "(a*(.)%w(%s*))", -the part of the string matching "a*(.)%w(%s*)" is -stored as the first capture (and therefore has number 1); -the character matching "." is captured with number 2, -and the part matching "%s*" has number 3. - - -

-As a special case, the empty capture () captures -the current string position (a number). -For instance, if we apply the pattern "()aa()" on the -string "flaaap", there will be two captures: 3 and 5. - - - - - - - - - - - -

6.5 – Table Manipulation

- -

-This library provides generic functions for table manipulation. -It provides all its functions inside the table table. - - -

-Remember that, whenever an operation needs the length of a table, -the table should be a proper sequence -or have a __len metamethod (see §3.4.6). -All functions ignore non-numeric keys -in tables given as arguments. - - -

-For performance reasons, -all table accesses (get/set) performed by these functions are raw. - - -

-


table.concat (list [, sep [, i [, j]]])

- - -

-Given a list where all elements are strings or numbers, -returns the string list[i]..sep..list[i+1] ··· sep..list[j]. -The default value for sep is the empty string, -the default for i is 1, -and the default for j is #list. -If i is greater than j, returns the empty string. - - - - -

-


table.insert (list, [pos,] value)

- - -

-Inserts element value at position pos in list, -shifting up the elements -list[pos], list[pos+1], ···, list[#list]. -The default value for pos is #list+1, -so that a call table.insert(t,x) inserts x at the end -of list t. - - - - -

-


table.pack (···)

- - -

-Returns a new table with all parameters stored into keys 1, 2, etc. -and with a field "n" with the total number of parameters. -Note that the resulting table may not be a sequence. - - - - -

-


table.remove (list [, pos])

- - -

-Removes from list the element at position pos, -returning the value of the removed element. -When pos is an integer between 1 and #list, -it shifts down the elements -list[pos+1], list[pos+2], ···, list[#list] -and erases element list[#list]; -The index pos can also be 0 when #list is 0, -or #list + 1; -in those cases, the function erases the element list[pos]. - - -

-The default value for pos is #list, -so that a call table.remove(t) removes the last element -of list t. - - - - -

-


table.sort (list [, comp])

- - -

-Sorts list elements in a given order, in-place, -from list[1] to list[#list]. -If comp is given, -then it must be a function that receives two list elements -and returns true when the first element must come -before the second in the final order -(so that not comp(list[i+1],list[i]) will be true after the sort). -If comp is not given, -then the standard Lua operator < is used instead. - - -

-The sort algorithm is not stable; -that is, elements considered equal by the given order -may have their relative positions changed by the sort. - - - - -

-


table.unpack (list [, i [, j]])

- - -

-Returns the elements from the given table. -This function is equivalent to - -

-     return list[i], list[i+1], ···, list[j]
-

-By default, i is 1 and j is #list. - - - - - - - -

6.6 – Mathematical Functions

- -

-This library is an interface to the standard C math library. -It provides all its functions inside the table math. - - -

-


math.abs (x)

- - -

-Returns the absolute value of x. - - - - -

-


math.acos (x)

- - -

-Returns the arc cosine of x (in radians). - - - - -

-


math.asin (x)

- - -

-Returns the arc sine of x (in radians). - - - - -

-


math.atan (x)

- - -

-Returns the arc tangent of x (in radians). - - - - -

-


math.atan2 (y, x)

- - -

-Returns the arc tangent of y/x (in radians), -but uses the signs of both parameters to find the -quadrant of the result. -(It also handles correctly the case of x being zero.) - - - - -

-


math.ceil (x)

- - -

-Returns the smallest integer larger than or equal to x. - - - - -

-


math.cos (x)

- - -

-Returns the cosine of x (assumed to be in radians). - - - - -

-


math.cosh (x)

- - -

-Returns the hyperbolic cosine of x. - - - - -

-


math.deg (x)

- - -

-Returns the angle x (given in radians) in degrees. - - - - -

-


math.exp (x)

- - -

-Returns the value ex. - - - - -

-


math.floor (x)

- - -

-Returns the largest integer smaller than or equal to x. - - - - -

-


math.fmod (x, y)

- - -

-Returns the remainder of the division of x by y -that rounds the quotient towards zero. - - - - -

-


math.frexp (x)

- - -

-Returns m and e such that x = m2e, -e is an integer and the absolute value of m is -in the range [0.5, 1) -(or zero when x is zero). - - - - -

-


math.huge

- - -

-The value HUGE_VAL, -a value larger than or equal to any other numerical value. - - - - -

-


math.ldexp (m, e)

- - -

-Returns m2e (e should be an integer). - - - - -

-


math.log (x [, base])

- - -

-Returns the logarithm of x in the given base. -The default for base is e -(so that the function returns the natural logarithm of x). - - - - -

-


math.max (x, ···)

- - -

-Returns the maximum value among its arguments. - - - - -

-


math.min (x, ···)

- - -

-Returns the minimum value among its arguments. - - - - -

-


math.modf (x)

- - -

-Returns two numbers, -the integral part of x and the fractional part of x. - - - - -

-


math.pi

- - -

-The value of π. - - - - -

-


math.pow (x, y)

- - -

-Returns xy. -(You can also use the expression x^y to compute this value.) - - - - -

-


math.rad (x)

- - -

-Returns the angle x (given in degrees) in radians. - - - - -

-


math.random ([m [, n]])

- - -

-This function is an interface to the simple -pseudo-random generator function rand provided by Standard C. -(No guarantees can be given for its statistical properties.) - - -

-When called without arguments, -returns a uniform pseudo-random real number -in the range [0,1). -When called with an integer number m, -math.random returns -a uniform pseudo-random integer in the range [1, m]. -When called with two integer numbers m and n, -math.random returns a uniform pseudo-random -integer in the range [m, n]. - - - - -

-


math.randomseed (x)

- - -

-Sets x as the "seed" -for the pseudo-random generator: -equal seeds produce equal sequences of numbers. - - - - -

-


math.sin (x)

- - -

-Returns the sine of x (assumed to be in radians). - - - - -

-


math.sinh (x)

- - -

-Returns the hyperbolic sine of x. - - - - -

-


math.sqrt (x)

- - -

-Returns the square root of x. -(You can also use the expression x^0.5 to compute this value.) - - - - -

-


math.tan (x)

- - -

-Returns the tangent of x (assumed to be in radians). - - - - -

-


math.tanh (x)

- - -

-Returns the hyperbolic tangent of x. - - - - - - - -

6.7 – Bitwise Operations

- -

-This library provides bitwise operations. -It provides all its functions inside the table bit32. - - -

-Unless otherwise stated, -all functions accept numeric arguments in the range -(-251,+251); -each argument is normalized to -the remainder of its division by 232 -and truncated to an integer (in some unspecified way), -so that its final value falls in the range [0,232 - 1]. -Similarly, all results are in the range [0,232 - 1]. -Note that bit32.bnot(0) is 0xFFFFFFFF, -which is different from -1. - - -

-


bit32.arshift (x, disp)

- - -

-Returns the number x shifted disp bits to the right. -The number disp may be any representable integer. -Negative displacements shift to the left. - - -

-This shift operation is what is called arithmetic shift. -Vacant bits on the left are filled -with copies of the higher bit of x; -vacant bits on the right are filled with zeros. -In particular, -displacements with absolute values higher than 31 -result in zero or 0xFFFFFFFF (all original bits are shifted out). - - - - -

-


bit32.band (···)

- - -

-Returns the bitwise and of its operands. - - - - -

-


bit32.bnot (x)

- - -

-Returns the bitwise negation of x. -For any integer x, -the following identity holds: - -

-     assert(bit32.bnot(x) == (-1 - x) % 2^32)
-
- - - -

-


bit32.bor (···)

- - -

-Returns the bitwise or of its operands. - - - - -

-


bit32.btest (···)

- - -

-Returns a boolean signaling -whether the bitwise and of its operands is different from zero. - - - - -

-


bit32.bxor (···)

- - -

-Returns the bitwise exclusive or of its operands. - - - - -

-


bit32.extract (n, field [, width])

- - -

-Returns the unsigned number formed by the bits -field to field + width - 1 from n. -Bits are numbered from 0 (least significant) to 31 (most significant). -All accessed bits must be in the range [0, 31]. - - -

-The default for width is 1. - - - - -

-


bit32.replace (n, v, field [, width])

- - -

-Returns a copy of n with -the bits field to field + width - 1 -replaced by the value v. -See bit32.extract for details about field and width. - - - - -

-


bit32.lrotate (x, disp)

- - -

-Returns the number x rotated disp bits to the left. -The number disp may be any representable integer. - - -

-For any valid displacement, -the following identity holds: - -

-     assert(bit32.lrotate(x, disp) == bit32.lrotate(x, disp % 32))
-

-In particular, -negative displacements rotate to the right. - - - - -

-


bit32.lshift (x, disp)

- - -

-Returns the number x shifted disp bits to the left. -The number disp may be any representable integer. -Negative displacements shift to the right. -In any direction, vacant bits are filled with zeros. -In particular, -displacements with absolute values higher than 31 -result in zero (all bits are shifted out). - - -

-For positive displacements, -the following equality holds: - -

-     assert(bit32.lshift(b, disp) == (b * 2^disp) % 2^32)
-
- - - -

-


bit32.rrotate (x, disp)

- - -

-Returns the number x rotated disp bits to the right. -The number disp may be any representable integer. - - -

-For any valid displacement, -the following identity holds: - -

-     assert(bit32.rrotate(x, disp) == bit32.rrotate(x, disp % 32))
-

-In particular, -negative displacements rotate to the left. - - - - -

-


bit32.rshift (x, disp)

- - -

-Returns the number x shifted disp bits to the right. -The number disp may be any representable integer. -Negative displacements shift to the left. -In any direction, vacant bits are filled with zeros. -In particular, -displacements with absolute values higher than 31 -result in zero (all bits are shifted out). - - -

-For positive displacements, -the following equality holds: - -

-     assert(bit32.rshift(b, disp) == math.floor(b % 2^32 / 2^disp))
-
- -

-This shift operation is what is called logical shift. - - - - - - - -

6.8 – Input and Output Facilities

- -

-The I/O library provides two different styles for file manipulation. -The first one uses implicit file descriptors; -that is, there are operations to set a default input file and a -default output file, -and all input/output operations are over these default files. -The second style uses explicit file descriptors. - - -

-When using implicit file descriptors, -all operations are supplied by table io. -When using explicit file descriptors, -the operation io.open returns a file descriptor -and then all operations are supplied as methods of the file descriptor. - - -

-The table io also provides -three predefined file descriptors with their usual meanings from C: -io.stdin, io.stdout, and io.stderr. -The I/O library never closes these files. - - -

-Unless otherwise stated, -all I/O functions return nil on failure -(plus an error message as a second result and -a system-dependent error code as a third result) -and some value different from nil on success. -On non-Posix systems, -the computation of the error message and error code -in case of errors -may be not thread safe, -because they rely on the global C variable errno. - - -

-


io.close ([file])

- - -

-Equivalent to file:close(). -Without a file, closes the default output file. - - - - -

-


io.flush ()

- - -

-Equivalent to io.output():flush(). - - - - -

-


io.input ([file])

- - -

-When called with a file name, it opens the named file (in text mode), -and sets its handle as the default input file. -When called with a file handle, -it simply sets this file handle as the default input file. -When called without parameters, -it returns the current default input file. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


io.lines ([filename ···])

- - -

-Opens the given file name in read mode -and returns an iterator function that -works like file:lines(···) over the opened file. -When the iterator function detects the end of file, -it returns nil (to finish the loop) and automatically closes the file. - - -

-The call io.lines() (with no file name) is equivalent -to io.input():lines(); -that is, it iterates over the lines of the default input file. -In this case it does not close the file when the loop ends. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


io.open (filename [, mode])

- - -

-This function opens a file, -in the mode specified in the string mode. -It returns a new file handle, -or, in case of errors, nil plus an error message. - - -

-The mode string can be any of the following: - -

    -
  • "r": read mode (the default);
  • -
  • "w": write mode;
  • -
  • "a": append mode;
  • -
  • "r+": update mode, all previous data is preserved;
  • -
  • "w+": update mode, all previous data is erased;
  • -
  • "a+": append update mode, previous data is preserved, - writing is only allowed at the end of file.
  • -

-The mode string can also have a 'b' at the end, -which is needed in some systems to open the file in binary mode. - - - - -

-


io.output ([file])

- - -

-Similar to io.input, but operates over the default output file. - - - - -

-


io.popen (prog [, mode])

- - -

-This function is system dependent and is not available -on all platforms. - - -

-Starts program prog in a separated process and returns -a file handle that you can use to read data from this program -(if mode is "r", the default) -or to write data to this program -(if mode is "w"). - - - - -

-


io.read (···)

- - -

-Equivalent to io.input():read(···). - - - - -

-


io.tmpfile ()

- - -

-Returns a handle for a temporary file. -This file is opened in update mode -and it is automatically removed when the program ends. - - - - -

-


io.type (obj)

- - -

-Checks whether obj is a valid file handle. -Returns the string "file" if obj is an open file handle, -"closed file" if obj is a closed file handle, -or nil if obj is not a file handle. - - - - -

-


io.write (···)

- - -

-Equivalent to io.output():write(···). - - - - -

-


file:close ()

- - -

-Closes file. -Note that files are automatically closed when -their handles are garbage collected, -but that takes an unpredictable amount of time to happen. - - -

-When closing a file handle created with io.popen, -file:close returns the same values -returned by os.execute. - - - - -

-


file:flush ()

- - -

-Saves any written data to file. - - - - -

-


file:lines (···)

- - -

-Returns an iterator function that, -each time it is called, -reads the file according to the given formats. -When no format is given, -uses "*l" as a default. -As an example, the construction - -

-     for c in file:lines(1) do body end
-

-will iterate over all characters of the file, -starting at the current position. -Unlike io.lines, this function does not close the file -when the loop ends. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


file:read (···)

- - -

-Reads the file file, -according to the given formats, which specify what to read. -For each format, -the function returns a string (or a number) with the characters read, -or nil if it cannot read data with the specified format. -When called without formats, -it uses a default format that reads the next line -(see below). - - -

-The available formats are - -

    - -
  • "*n": -reads a number; -this is the only format that returns a number instead of a string. -
  • - -
  • "*a": -reads the whole file, starting at the current position. -On end of file, it returns the empty string. -
  • - -
  • "*l": -reads the next line skipping the end of line, -returning nil on end of file. -This is the default format. -
  • - -
  • "*L": -reads the next line keeping the end of line (if present), -returning nil on end of file. -
  • - -
  • number: -reads a string with up to this number of bytes, -returning nil on end of file. -If number is zero, -it reads nothing and returns an empty string, -or nil on end of file. -
  • - -
- - - -

-


file:seek ([whence [, offset]])

- - -

-Sets and gets the file position, -measured from the beginning of the file, -to the position given by offset plus a base -specified by the string whence, as follows: - -

    -
  • "set": base is position 0 (beginning of the file);
  • -
  • "cur": base is current position;
  • -
  • "end": base is end of file;
  • -

-In case of success, seek returns the final file position, -measured in bytes from the beginning of the file. -If seek fails, it returns nil, -plus a string describing the error. - - -

-The default value for whence is "cur", -and for offset is 0. -Therefore, the call file:seek() returns the current -file position, without changing it; -the call file:seek("set") sets the position to the -beginning of the file (and returns 0); -and the call file:seek("end") sets the position to the -end of the file, and returns its size. - - - - -

-


file:setvbuf (mode [, size])

- - -

-Sets the buffering mode for an output file. -There are three available modes: - -

    - -
  • "no": -no buffering; the result of any output operation appears immediately. -
  • - -
  • "full": -full buffering; output operation is performed only -when the buffer is full or when -you explicitly flush the file (see io.flush). -
  • - -
  • "line": -line buffering; output is buffered until a newline is output -or there is any input from some special files -(such as a terminal device). -
  • - -

-For the last two cases, size -specifies the size of the buffer, in bytes. -The default is an appropriate size. - - - - -

-


file:write (···)

- - -

-Writes the value of each of its arguments to file. -The arguments must be strings or numbers. - - -

-In case of success, this function returns file. -Otherwise it returns nil plus a string describing the error. - - - - - - - -

6.9 – Operating System Facilities

- -

-This library is implemented through table os. - - -

-


os.clock ()

- - -

-Returns an approximation of the amount in seconds of CPU time -used by the program. - - - - -

-


os.date ([format [, time]])

- - -

-Returns a string or a table containing date and time, -formatted according to the given string format. - - -

-If the time argument is present, -this is the time to be formatted -(see the os.time function for a description of this value). -Otherwise, date formats the current time. - - -

-If format starts with '!', -then the date is formatted in Coordinated Universal Time. -After this optional character, -if format is the string "*t", -then date returns a table with the following fields: -year (four digits), month (1–12), day (1–31), -hour (0–23), min (0–59), sec (0–61), -wday (weekday, Sunday is 1), -yday (day of the year), -and isdst (daylight saving flag, a boolean). -This last field may be absent -if the information is not available. - - -

-If format is not "*t", -then date returns the date as a string, -formatted according to the same rules as the ISO C function strftime. - - -

-When called without arguments, -date returns a reasonable date and time representation that depends on -the host system and on the current locale -(that is, os.date() is equivalent to os.date("%c")). - - -

-On non-Posix systems, -this function may be not thread safe -because of its reliance on C function gmtime and C function localtime. - - - - -

-


os.difftime (t2, t1)

- - -

-Returns the number of seconds from time t1 to time t2. -In POSIX, Windows, and some other systems, -this value is exactly t2-t1. - - - - -

-


os.execute ([command])

- - -

-This function is equivalent to the ISO C function system. -It passes command to be executed by an operating system shell. -Its first result is true -if the command terminated successfully, -or nil otherwise. -After this first result -the function returns a string and a number, -as follows: - -

    - -
  • "exit": -the command terminated normally; -the following number is the exit status of the command. -
  • - -
  • "signal": -the command was terminated by a signal; -the following number is the signal that terminated the command. -
  • - -
- -

-When called without a command, -os.execute returns a boolean that is true if a shell is available. - - - - -

-


os.exit ([code [, close])

- - -

-Calls the ISO C function exit to terminate the host program. -If code is true, -the returned status is EXIT_SUCCESS; -if code is false, -the returned status is EXIT_FAILURE; -if code is a number, -the returned status is this number. -The default value for code is true. - - -

-If the optional second argument close is true, -closes the Lua state before exiting. - - - - -

-


os.getenv (varname)

- - -

-Returns the value of the process environment variable varname, -or nil if the variable is not defined. - - - - -

-


os.remove (filename)

- - -

-Deletes the file (or empty directory, on POSIX systems) -with the given name. -If this function fails, it returns nil, -plus a string describing the error and the error code. - - - - -

-


os.rename (oldname, newname)

- - -

-Renames file or directory named oldname to newname. -If this function fails, it returns nil, -plus a string describing the error and the error code. - - - - -

-


os.setlocale (locale [, category])

- - -

-Sets the current locale of the program. -locale is a system-dependent string specifying a locale; -category is an optional string describing which category to change: -"all", "collate", "ctype", -"monetary", "numeric", or "time"; -the default category is "all". -The function returns the name of the new locale, -or nil if the request cannot be honored. - - -

-If locale is the empty string, -the current locale is set to an implementation-defined native locale. -If locale is the string "C", -the current locale is set to the standard C locale. - - -

-When called with nil as the first argument, -this function only returns the name of the current locale -for the given category. - - -

-This function may be not thread safe -because of its reliance on C function setlocale. - - - - -

-


os.time ([table])

- - -

-Returns the current time when called without arguments, -or a time representing the date and time specified by the given table. -This table must have fields year, month, and day, -and may have fields -hour (default is 12), -min (default is 0), -sec (default is 0), -and isdst (default is nil). -For a description of these fields, see the os.date function. - - -

-The returned value is a number, whose meaning depends on your system. -In POSIX, Windows, and some other systems, -this number counts the number -of seconds since some given start time (the "epoch"). -In other systems, the meaning is not specified, -and the number returned by time can be used only as an argument to -os.date and os.difftime. - - - - -

-


os.tmpname ()

- - -

-Returns a string with a file name that can -be used for a temporary file. -The file must be explicitly opened before its use -and explicitly removed when no longer needed. - - -

-On POSIX systems, -this function also creates a file with that name, -to avoid security risks. -(Someone else might create the file with wrong permissions -in the time between getting the name and creating the file.) -You still have to open the file to use it -and to remove it (even if you do not use it). - - -

-When possible, -you may prefer to use io.tmpfile, -which automatically removes the file when the program ends. - - - - - - - -

6.10 – The Debug Library

- -

-This library provides -the functionality of the debug interface (§4.9) to Lua programs. -You should exert care when using this library. -Several of its functions -violate basic assumptions about Lua code -(e.g., that variables local to a function -cannot be accessed from outside; -that userdata metatables cannot be changed by Lua code; -that Lua programs do not crash) -and therefore can compromise otherwise secure code. -Moreover, some functions in this library may be slow. - - -

-All functions in this library are provided -inside the debug table. -All functions that operate over a thread -have an optional first argument which is the -thread to operate over. -The default is always the current thread. - - -

-


debug.debug ()

- - -

-Enters an interactive mode with the user, -running each string that the user enters. -Using simple commands and other debug facilities, -the user can inspect global and local variables, -change their values, evaluate expressions, and so on. -A line containing only the word cont finishes this function, -so that the caller continues its execution. - - -

-Note that commands for debug.debug are not lexically nested -within any function and so have no direct access to local variables. - - - - -

-


debug.gethook ([thread])

- - -

-Returns the current hook settings of the thread, as three values: -the current hook function, the current hook mask, -and the current hook count -(as set by the debug.sethook function). - - - - -

-


debug.getinfo ([thread,] f [, what])

- - -

-Returns a table with information about a function. -You can give the function directly -or you can give a number as the value of f, -which means the function running at level f of the call stack -of the given thread: -level 0 is the current function (getinfo itself); -level 1 is the function that called getinfo -(except for tail calls, which do not count on the stack); -and so on. -If f is a number larger than the number of active functions, -then getinfo returns nil. - - -

-The returned table can contain all the fields returned by lua_getinfo, -with the string what describing which fields to fill in. -The default for what is to get all information available, -except the table of valid lines. -If present, -the option 'f' -adds a field named func with the function itself. -If present, -the option 'L' -adds a field named activelines with the table of -valid lines. - - -

-For instance, the expression debug.getinfo(1,"n").name returns -a table with a name for the current function, -if a reasonable name can be found, -and the expression debug.getinfo(print) -returns a table with all available information -about the print function. - - - - -

-


debug.getlocal ([thread,] f, local)

- - -

-This function returns the name and the value of the local variable -with index local of the function at level f of the stack. -This function accesses not only explicit local variables, -but also parameters, temporaries, etc. - - -

-The first parameter or local variable has index 1, and so on, -until the last active variable. -Negative indices refer to vararg parameters; --1 is the first vararg parameter. -The function returns nil if there is no variable with the given index, -and raises an error when called with a level out of range. -(You can call debug.getinfo to check whether the level is valid.) - - -

-Variable names starting with '(' (open parenthesis) -represent internal variables -(loop control variables, temporaries, varargs, and C function locals). - - -

-The parameter f may also be a function. -In that case, getlocal returns only the name of function parameters. - - - - -

-


debug.getmetatable (value)

- - -

-Returns the metatable of the given value -or nil if it does not have a metatable. - - - - -

-


debug.getregistry ()

- - -

-Returns the registry table (see §4.5). - - - - -

-


debug.getupvalue (f, up)

- - -

-This function returns the name and the value of the upvalue -with index up of the function f. -The function returns nil if there is no upvalue with the given index. - - - - -

-


debug.getuservalue (u)

- - -

-Returns the Lua value associated to u. -If u is not a userdata, -returns nil. - - - - -

-


debug.sethook ([thread,] hook, mask [, count])

- - -

-Sets the given function as a hook. -The string mask and the number count describe -when the hook will be called. -The string mask may have any combination of the following characters, -with the given meaning: - -

    -
  • 'c': the hook is called every time Lua calls a function;
  • -
  • 'r': the hook is called every time Lua returns from a function;
  • -
  • 'l': the hook is called every time Lua enters a new line of code.
  • -

-Moreover, -with a count different from zero, -the hook is called also after every count instructions. - - -

-When called without arguments, -debug.sethook turns off the hook. - - -

-When the hook is called, its first parameter is a string -describing the event that has triggered its call: -"call" (or "tail call"), -"return", -"line", and "count". -For line events, -the hook also gets the new line number as its second parameter. -Inside a hook, -you can call getinfo with level 2 to get more information about -the running function -(level 0 is the getinfo function, -and level 1 is the hook function). - - - - -

-


debug.setlocal ([thread,] level, local, value)

- - -

-This function assigns the value value to the local variable -with index local of the function at level level of the stack. -The function returns nil if there is no local -variable with the given index, -and raises an error when called with a level out of range. -(You can call getinfo to check whether the level is valid.) -Otherwise, it returns the name of the local variable. - - -

-See debug.getlocal for more information about -variable indices and names. - - - - -

-


debug.setmetatable (value, table)

- - -

-Sets the metatable for the given value to the given table -(which can be nil). -Returns value. - - - - -

-


debug.setupvalue (f, up, value)

- - -

-This function assigns the value value to the upvalue -with index up of the function f. -The function returns nil if there is no upvalue -with the given index. -Otherwise, it returns the name of the upvalue. - - - - -

-


debug.setuservalue (udata, value)

- - -

-Sets the given value as -the Lua value associated to the given udata. -value must be a table or nil; -udata must be a full userdata. - - -

-Returns udata. - - - - -

-


debug.traceback ([thread,] [message [, level]])

- - -

-If message is present but is neither a string nor nil, -this function returns message without further processing. -Otherwise, -it returns a string with a traceback of the call stack. -An optional message string is appended -at the beginning of the traceback. -An optional level number tells at which level -to start the traceback -(default is 1, the function calling traceback). - - - - -

-


debug.upvalueid (f, n)

- - -

-Returns an unique identifier (as a light userdata) -for the upvalue numbered n -from the given function. - - -

-These unique identifiers allow a program to check whether different -closures share upvalues. -Lua closures that share an upvalue -(that is, that access a same external local variable) -will return identical ids for those upvalue indices. - - - - -

-


debug.upvaluejoin (f1, n1, f2, n2)

- - -

-Make the n1-th upvalue of the Lua closure f1 -refer to the n2-th upvalue of the Lua closure f2. - - - - - - - -

7 – Lua Standalone

- -

-Although Lua has been designed as an extension language, -to be embedded in a host C program, -it is also frequently used as a standalone language. -An interpreter for Lua as a standalone language, -called simply lua, -is provided with the standard distribution. -The standalone interpreter includes -all standard libraries, including the debug library. -Its usage is: - -

-     lua [options] [script [args]]
-

-The options are: - -

    -
  • -e stat: executes string stat;
  • -
  • -l mod: "requires" mod;
  • -
  • -i: enters interactive mode after running script;
  • -
  • -v: prints version information;
  • -
  • -E: ignores environment variables;
  • -
  • --: stops handling options;
  • -
  • -: executes stdin as a file and stops handling options.
  • -

-After handling its options, lua runs the given script, -passing to it the given args as string arguments. -When called without arguments, -lua behaves as lua -v -i -when the standard input (stdin) is a terminal, -and as lua - otherwise. - - -

-When called without option -E, -the interpreter checks for an environment variable LUA_INIT_5_2 -(or LUA_INIT if it is not defined) -before running any argument. -If the variable content has the format @filename, -then lua executes the file. -Otherwise, lua executes the string itself. - - -

-When called with option -E, -besides ignoring LUA_INIT, -Lua also ignores -the values of LUA_PATH and LUA_CPATH, -setting the values of -package.path and package.cpath -with the default paths defined in luaconf.h. - - -

-All options are handled in order, except -i and -E. -For instance, an invocation like - -

-     $ lua -e'a=1' -e 'print(a)' script.lua
-

-will first set a to 1, then print the value of a, -and finally run the file script.lua with no arguments. -(Here $ is the shell prompt. Your prompt may be different.) - - -

-Before starting to run the script, -lua collects all arguments in the command line -in a global table called arg. -The script name is stored at index 0, -the first argument after the script name goes to index 1, -and so on. -Any arguments before the script name -(that is, the interpreter name plus the options) -go to negative indices. -For instance, in the call - -

-     $ lua -la b.lua t1 t2
-

-the interpreter first runs the file a.lua, -then creates a table - -

-     arg = { [-2] = "lua", [-1] = "-la",
-             [0] = "b.lua",
-             [1] = "t1", [2] = "t2" }
-

-and finally runs the file b.lua. -The script is called with arg[1], arg[2], ... -as arguments; -it can also access these arguments with the vararg expression '...'. - - -

-In interactive mode, -if you write an incomplete statement, -the interpreter waits for its completion -by issuing a different prompt. - - -

-In case of unprotected errors in the script, -the interpreter reports the error to the standard error stream. -If the error object is a string, -the interpreter adds a stack traceback to it. -Otherwise, if the error object has a metamethod __tostring, -the interpreter calls this metamethod to produce the final message. -Finally, if the error object is nil, -the interpreter does not report the error. - - -

-When finishing normally, -the interpreter closes its main Lua state -(see lua_close). -The script can avoid this step by -calling os.exit to terminate. - - -

-To allow the use of Lua as a -script interpreter in Unix systems, -the standalone interpreter skips -the first line of a chunk if it starts with #. -Therefore, Lua scripts can be made into executable programs -by using chmod +x and the #! form, -as in - -

-     #!/usr/local/bin/lua
-

-(Of course, -the location of the Lua interpreter may be different in your machine. -If lua is in your PATH, -then - -

-     #!/usr/bin/env lua
-

-is a more portable solution.) - - - -

8 – Incompatibilities with the Previous Version

- -

-Here we list the incompatibilities that you may find when moving a program -from Lua 5.1 to Lua 5.2. -You can avoid some incompatibilities by compiling Lua with -appropriate options (see file luaconf.h). -However, -all these compatibility options will be removed in the next version of Lua. -Similarly, -all features marked as deprecated in Lua 5.1 -have been removed in Lua 5.2. - - - -

8.1 – Changes in the Language

-
    - -
  • -The concept of environment changed. -Only Lua functions have environments. -To set the environment of a Lua function, -use the variable _ENV or the function load. - - -

    -C functions no longer have environments. -Use an upvalue with a shared table if you need to keep -shared state among several C functions. -(You may use luaL_setfuncs to open a C library -with all functions sharing a common upvalue.) - - -

    -To manipulate the "environment" of a userdata -(which is now called user value), -use the new functions -lua_getuservalue and lua_setuservalue. -

  • - -
  • -Lua identifiers cannot use locale-dependent letters. -
  • - -
  • -Doing a step or a full collection in the garbage collector -does not restart the collector if it has been stopped. -
  • - -
  • -Weak tables with weak keys now perform like ephemeron tables. -
  • - -
  • -The event tail return in debug hooks was removed. -Instead, tail calls generate a special new event, -tail call, so that the debugger can know that -there will not be a corresponding return event. -
  • - -
  • -Equality between function values has changed. -Now, a function definition may not create a new value; -it may reuse some previous value if there is no -observable difference to the new function. -
  • - -
- - - - -

8.2 – Changes in the Libraries

-
    - -
  • -Function module is deprecated. -It is easy to set up a module with regular Lua code. -Modules are not expected to set global variables. -
  • - -
  • -Functions setfenv and getfenv were removed, -because of the changes in environments. -
  • - -
  • -Function math.log10 is deprecated. -Use math.log with 10 as its second argument, instead. -
  • - -
  • -Function loadstring is deprecated. -Use load instead; it now accepts string arguments -and are exactly equivalent to loadstring. -
  • - -
  • -Function table.maxn is deprecated. -Write it in Lua if you really need it. -
  • - -
  • -Function os.execute now returns true when command -terminates successfully and nil plus error information -otherwise. -
  • - -
  • -Function unpack was moved into the table library -and therefore must be called as table.unpack. -
  • - -
  • -Character class %z in patterns is deprecated, -as now patterns may contain '\0' as a regular character. -
  • - -
  • -The table package.loaders was renamed package.searchers. -
  • - -
  • -Lua does not have bytecode verification anymore. -So, all functions that load code -(load and loadfile) -are potentially insecure when loading untrusted binary data. -(Actually, those functions were already insecure because -of flaws in the verification algorithm.) -When in doubt, -use the mode argument of those functions -to restrict them to loading textual chunks. -
  • - -
  • -The standard paths in the official distribution may -change between versions. -
  • - -
- - - - -

8.3 – Changes in the API

-
    - -
  • -Pseudoindex LUA_GLOBALSINDEX was removed. -You must get the global environment from the registry -(see §4.5). -
  • - -
  • -Pseudoindex LUA_ENVIRONINDEX -and functions lua_getfenv/lua_setfenv -were removed, -as C functions no longer have environments. -
  • - -
  • -Function luaL_register is deprecated. -Use luaL_setfuncs so that your module does not create globals. -(Modules are not expected to set global variables anymore.) -
  • - -
  • -The osize argument to the allocation function -may not be zero when creating a new block, -that is, when ptr is NULL -(see lua_Alloc). -Use only the test ptr == NULL to check whether -the block is new. -
  • - -
  • -Finalizers (__gc metamethods) for userdata are called in the -reverse order that they were marked for finalization, -not that they were created (see §2.5.1). -(Most userdata are marked immediately after they are created.) -Moreover, -if the metatable does not have a __gc field when set, -the finalizer will not be called, -even if it is set later. -
  • - -
  • -luaL_typerror was removed. -Write your own version if you need it. -
  • - -
  • -Function lua_cpcall is deprecated. -You can simply push the function with lua_pushcfunction -and call it with lua_pcall. -
  • - -
  • -Functions lua_equal and lua_lessthan are deprecated. -Use the new lua_compare with appropriate options instead. -
  • - -
  • -Function lua_objlen was renamed lua_rawlen. -
  • - -
  • -Function lua_load has an extra parameter, mode. -Pass NULL to simulate the old behavior. -
  • - -
  • -Function lua_resume has an extra parameter, from. -Pass NULL or the thread doing the call. -
  • - -
- - - - -

9 – The Complete Syntax of Lua

- -

-Here is the complete syntax of Lua in extended BNF. -(It does not describe operator precedences.) - - - - -

-
-	chunk ::= block
-
-	block ::= {stat} [retstat]
-
-	stat ::=  ‘;’ | 
-		 varlist ‘=’ explist | 
-		 functioncall | 
-		 label | 
-		 break | 
-		 goto Name | 
-		 do block end | 
-		 while exp do block end | 
-		 repeat block until exp | 
-		 if exp then block {elseif exp then block} [else block] end | 
-		 for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end | 
-		 for namelist in explist do block end | 
-		 function funcname funcbody | 
-		 local function Name funcbody | 
-		 local namelist [‘=’ explist] 
-
-	retstat ::= return [explist] [‘;’]
-
-	label ::= ‘::’ Name ‘::’
-
-	funcname ::= Name {‘.’ Name} [‘:’ Name]
-
-	varlist ::= var {‘,’ var}
-
-	var ::=  Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name 
-
-	namelist ::= Name {‘,’ Name}
-
-	explist ::= exp {‘,’ exp}
-
-	exp ::=  nil | false | true | Number | String | ‘...’ | functiondef | 
-		 prefixexp | tableconstructor | exp binop exp | unop exp 
-
-	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
-
-	functioncall ::=  prefixexp args | prefixexp ‘:’ Name args 
-
-	args ::=  ‘(’ [explist] ‘)’ | tableconstructor | String 
-
-	functiondef ::= function funcbody
-
-	funcbody ::= ‘(’ [parlist] ‘)’ block end
-
-	parlist ::= namelist [‘,’ ‘...’] | ‘...’
-
-	tableconstructor ::= ‘{’ [fieldlist] ‘}’
-
-	fieldlist ::= field {fieldsep field} [fieldsep]
-
-	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
-
-	fieldsep ::= ‘,’ | ‘;’
-
-	binop ::= ‘+’ | ‘-’ | ‘*’ | ‘/’ | ‘^’ | ‘%’ | ‘..’ | 
-		 ‘<’ | ‘<=’ | ‘>’ | ‘>=’ | ‘==’ | ‘~=’ | 
-		 and | or
-
-	unop ::= ‘-’ | not | ‘#’
-
-
- -

- - - - - - - -


- -Last update: -Mon Feb 23 22:24:44 BRT 2015 - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/osi-certified-72x60.png b/LuaBridge3/Tests/Lua/Lua.5.2.4/doc/osi-certified-72x60.png deleted file mode 100644 index 07df5f6ee7a7a8b2108025dcd815f73f145a83af..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3774 zcmV;v4ngsWP)$kl5 zqcT7g&?zu8?ezWYz4zUB-|zR9d+&Qy2xAN{qY(ew0A7^*gV^7jytKqPFV3{hZfovn zs%x!l>(m&Gdb8C+5XeR7>h0kj=o=X3A39;2KLYfEMt>p1YMW~dt`rpAC{lN~P>5pq zH1L4nAdCT17}*hN=LnEsvMl=5Ij^QArAa&_V~zoht-Ei~)E~(Ivhe0#jik{t$isEK znCH$TxCB8EKmcF>3@pRaHpbR%Gqm*dsZA4H{j(NjZFp^iNFW+RBx6R*X19J*`0XG5 z^Y>cR=^Hi9#ovYGlbFSr#Q*^PgCGC^gb*SC5TcBfzQLe-r2m!Quik&_g9XzTj0qSR zD`FkG_RYWDa^+#UUxL&t+!K+&(ion@Fd`5l5p7{Qsva9vegC|4^NzJUMvn)^gqWsF zvu^j=%FfCVg^cgbXDRl1DE$lsfe;BjjmFmRHER~E-MeWoNsyyNHCpG%Y}igd_(Md;&9La8_B075NDRX9gTD zIHY`}9E~aGi9Kk1@P~rmPna=*=gz~UTdTpsQmjX)J23%v9NliQS)8`xJh6Qz_nE~e z&tP|!dcJdo;JMNa3>afSx$lko8>fp-I}OiCVz(dOF1u6e8$IrsSP?=5mp~lkaFqm? zAUMxRq%ecIu3WE)Uf=%p8g z+RSY?G=VO%wAfdICj?Uzb+5jr{8m|)i#{M}JjaDIoXf#1=DYLwX;1EW&sijPvm6EkBGuOx6r~lKv`g`yH?)|&PRUr$5Ibw2HBM7C74XvE@gaPjN+@;j$J)AgYhnT-U5m+wj|Wz8K630AfO8PUoGD^^Mcq zY9C<~%wUm^u%ox5P21)KNN0$(v^OI$A~?iwsS_fRu1+`EH|CRdpA4zsk8Z#|?x@^vVEAL+2JxH%&^{JUU%B=?EU7`Ar*Q|JvqPofcBt765(*f5JI$>=3{<%K)4ei zogo$)5XP}_X$y^pIYyWTt}EAnhTq}u4sAdBvC(WC{I#x4^>$vCvQ0UDs^18sAQG9o zEaP0qjrSSv1W0FyO%9&y$@em~n@8}}EXBG6x%ew49J_q%l@As_XnNpi|MTTPr~ca_ zW%uon6dBKL*pvzYFvf<~p6K8hK9BDNNN0$7xp^hWC3n^7FoQ?P(=m(6!Pj&S2f1fqH=`(w)KcPl5aEi2}~4hF*f*g}vaS-=c7v>N8c z{yNM*%+azq=@prWtgpi~^3?^AsJqS(>=pb=6PrGH#=O{Hcho$_F#MtsK$$3e2fZvg zy}!-V%`+uFMOW87LIgu3vKuMgqwY0}*Sd;aokQp(F#-{}Ss(Iy1iekY1ZQX?1WEL? z7=zq`lH-#Hw=bHRio3yPun%`c5rI1Hb|wTSWTs|12Mg#QkkwTmy zAYul0H*_b(BnkP#!R_&p@d54uz0JKthGv3C^fdKS%~alookE`QX@%#MQN2=SFWrOha7Ij7ImStNaWsy~? zsylUeT02_-z-G4s0L!v=+Wx|cxr$tmY&$a1by8z#6HBp!*9{@mU9XQ0h@L%V_R}4g z&s#2{MCOj4`5ux-SUautC5@{U895o-biKMWWoQ09{|jx8wz}@_(ep%Yk4{90C#s6-sa}fU5{}m>#>VtE_b#5bn8O+3k{&6GoEkB;yGie;A_5Uy zqPN*tU()pE+_&~``5XX({el-xT_}%`%fsc>_0@m5{+FhXru>rpyLESe31R>cK^FFrCm+#WL$-D{Z3*9>Lg{wi}xEYn_`@Hy`-d z1N}kIY%@Eu&Bpe|Rr6N;%Yk>6&RI$lgpIO26BYT%C!dU-o4bqqQpGY?p6lPru6Hzc z@WuSDI^BYaDH*>R)~)$V1J0Edn4r(9vo>E<2XjOJr2*G124;t^U+p{iUnZN5oapCpCk(F}}<#3ZZli!Nk z^UWT;Q9qm-i`i$kJS}5P%puBJ<&krTO;*#$Y7d$o96EbQ{aF1XFpTj}wf}eI|IOba z%w}_CWu?JjkV>U-ad9L$@Mu$CU;pUQBZgt5QmI@n=W@9K(A(SF-rnxzy|_!5ekKqCQTad`sa|&&Q6jfy}iAEst?|mH*emIjg9SB zRVWlHl?r3bvh2qnf6V6(+>4TulB%kzFveeh{k1?K*t&J=m>dk9P8SjqQdn4sF;*&- z(b3VFnVH$y*$Rb%rs zefJ#z#KpyZ_0?C$jvY%)O?7a?7#}%u1OT>d*)keF*REZ=c=4j6tkr5MilS*cB_$;< zFArmEv)Oby-7}4>TD9uE_ulKT4s6Bp@^Y0*rBEo&o;?cy8#Zi^%jH+DTv4f1SFc_L zfc5LwXJ=;vKt@K!?%liR&!6Almmq$2R@G|tg$oyGnpP+jQBhF<(9qCOR8%AuiBtJCSc zyu1LQw6wIQre^Zw$^E0N)#}R1%J}$rkw`Qc#z0A{)dIkjDN`I(PfyS2=x9f~R4N64 zPe1*1=gytQ#l=RWao4V0bLY-=?Bpl*dQDA@LZMJ9l{Gar$;rvzfB$`Tb#+==T0=ua zSy@?1N{UXWyL9Q&#*G`Zv$GE#JXljxBauj2T3VD!rO9N<%F3#*uP-Sn(P%W=w{Jgx z{(NC!VNOmC0OaN6ZQHg@tJQw^;fGtdZUulVSFX&NGv~~iGoO9-nNq0~2n78w23E{L zmth7T3|W>10ISuSm6cUgRCMXmr5!tV0D!x@`?6)rcI?<8lgZ#IIehqVOiYYpi@x#3 z8xau^+1c4ER;th&( zVHk--A`l3|!os9dsYatANm8TH96x@%qM{-&FmUtc&2qVX-MV%A_U(J~%{TY#*<&ym zX3Ur|c$No?u%e>k#EBDaZEY7XUVLH`0zh|n zw_~XRz;RH!y1MS)zn_X$Km70mNs@ZKo~G$z$BuD09F}FpVzEY}F&d2ug#rLPJUpgPpKh}a^y$-i zJl@%}XHT6vRaaNHckf=MQYn>6Fk&*D<+ja0B z5C{a#&CQN-V`HPyXe3EeAP~gH#>U3RayT5ZSd1}tbaaSNDAZ^)j%n&QHMoE=7KubA zlWEeVNpiV7Dk=&gzM|0Dz(>0HA5Q-_F}_znz(xxqbU~E|+`a#EH|V zPjA|^DJLg~rs?+f_6rv-T)upnAP7fChoq;cFJHcV=gyt)zWXjs(+gZ<%kMDTlOd1+TFW%&z(D`)oKF*0@Bmd zLqkIy?RvewprGK+ojWv5%Ve?@D^>&r1p$CcrMhuv}x1&joiO~|IC>)G) - - -Lua 5.2 readme - - - - - - - -
-

-Lua -Welcome to Lua 5.2 -

- -

-about -· -installation -· -changes -· -license -· -reference manual - -

About Lua

- -

-Lua is a powerful, fast, lightweight, embeddable scripting language -developed by a -team -at -PUC-Rio, -the Pontifical Catholic University of Rio de Janeiro in Brazil. -Lua is -free software -used in many products and projects around the world. - -

-Lua's -official web site -provides complete information -about Lua, -including -an -executive summary -and -updated -documentation, -especially the -reference manual, -which may differ slightly from the -local copy -distributed in this package. - -

Installing Lua

- -

-Lua is distributed in -source -form. -You need to build it before using it. -Building Lua should be straightforward -because -Lua is implemented in pure ANSI C and compiles unmodified in all known -platforms that have an ANSI C compiler. -Lua also compiles unmodified as C++. -The instructions given below for building Lua are for Unix-like platforms. -See also -instructions for other systems -and -customization options. - -

-If you don't have the time or the inclination to compile Lua yourself, -get a binary from -LuaBinaries. -Try also -LuaDist, -a multi-platform distribution of Lua that includes batteries. - -

Building Lua

- -

-In most Unix-like platforms, simply do "make" with a suitable target. -Here are the details. - -

    -
  1. -Open a terminal window and move to -the top-level directory, which is named lua-5.2.x. -The Makefile there controls both the build process and the installation process. -

    -

  2. - Do "make" and see if your platform is listed. - The platforms currently supported are: -

    -

    - aix ansi bsd freebsd generic linux macosx mingw posix solaris -

    -

    - If your platform is listed, just do "make xxx", where xxx - is your platform name. -

    - If your platform is not listed, try the closest one or posix, generic, - ansi, in this order. -

    -

  3. -The compilation takes only a few moments -and produces three files in the src directory: -lua (the interpreter), -luac (the compiler), -and liblua.a (the library). -

    -

  4. - To check that Lua has been built correctly, do "make test" - after building Lua. This will run the interpreter and print its version. -
-

-If you're running Linux and get compilation errors, -make sure you have installed the readline development package -(which is probably named libreadline-dev or readline-devel). -If you get link errors after that, -then try "make linux MYLIBS=-ltermcap". - -

Installing Lua

-

- Once you have built Lua, you may want to install it in an official - place in your system. In this case, do "make install". The official - place and the way to install files are defined in the Makefile. You'll - probably need the right permissions to install files. - -

- To build and install Lua in one step, do "make xxx install", - where xxx is your platform name. - -

- To install Lua locally, do "make local". - This will create a directory install with subdirectories - bin, include, lib, man, share, - and install Lua as listed below. - - To install Lua locally, but in some other directory, do - "make install INSTALL_TOP=xxx", where xxx is your chosen directory. - The installation starts in the src and doc directories, - so take care if INSTALL_TOP is not an absolute path. - -

-
- bin: -
- lua luac -
- include: -
- lua.h luaconf.h lualib.h lauxlib.h lua.hpp -
- lib: -
- liblua.a -
- man/man1: -
- lua.1 luac.1 -
- -

- These are the only directories you need for development. - If you only want to run Lua programs, - you only need the files in bin and man. - The files in include and lib are needed for - embedding Lua in C or C++ programs. - -

Customization

-

- Three kinds of things can be customized by editing a file: -

    -
  • Where and how to install Lua — edit Makefile. -
  • How to build Lua — edit src/Makefile. -
  • Lua features — edit src/luaconf.h. -
- -

- You don't actually need to edit the Makefiles because you may set the - relevant variables in the command line when invoking make. - Nevertheless, it's probably best to edit and save the Makefiles to - record the changes you've made. - -

- On the other hand, if you need to customize some Lua features, you'll need - to edit src/luaconf.h before building and installing Lua. - The edited file will be the one installed, and - it will be used by any Lua clients that you build, to ensure consistency. - Further customization is available to experts by editing the Lua sources. - -

- We strongly recommend that you enable dynamic loading in src/luaconf.h. - This is done automatically for all platforms listed above that have - this feature and also for Windows. - -

Building Lua on other systems

- -

- If you're not using the usual Unix tools, then the instructions for - building Lua depend on the compiler you use. You'll need to create - projects (or whatever your compiler uses) for building the library, - the interpreter, and the compiler, as follows: - -

-
-library: -
-lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c -lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c -ltm.c lundump.c lvm.c lzio.c -lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c -lmathlib.c loslib.c lstrlib.c ltablib.c loadlib.c linit.c -
-interpreter: -
- library, lua.c -
-compiler: -
- library, luac.c -
- -

- To use Lua as a library in your own programs you'll need to know how to - create and use libraries with your compiler. Moreover, to dynamically load - C libraries for Lua you'll need to know how to create dynamic libraries - and you'll need to make sure that the Lua API functions are accessible to - those dynamic libraries — but don't link the Lua library - into each dynamic library. For Unix, we recommend that the Lua library - be linked statically into the host program and its symbols exported for - dynamic linking; src/Makefile does this for the Lua interpreter. - For Windows, we recommend that the Lua library be a DLL. - In all cases, the compiler luac should be linked statically. - -

- As mentioned above, you may edit src/luaconf.h to customize - some features before building Lua. - -

Changes since Lua 5.1

- -

-Here are the main changes introduced in Lua 5.2. -The -reference manual -lists the -incompatibilities that had to be introduced. - -

Main changes

-
    -
  • yieldable pcall and metamethods -
  • new lexical scheme for globals -
  • ephemeron tables -
  • new library for bitwise operations -
  • light C functions -
  • emergency garbage collector -
  • goto statement -
  • finalizers for tables -
- -Here are the other changes introduced in Lua 5.2: -

Language

-
    -
  • no more fenv for threads or functions -
  • tables honor the __len metamethod -
  • hex and \z escapes in strings -
  • support for hexadecimal floats -
  • order metamethods work for different types -
  • no more verification of opcode consistency -
  • hook event "tail return" replaced by "tail call" -
  • empty statement -
  • break statement may appear in the middle of a block -
- -

Libraries

-
    -
  • arguments for function called through xpcall -
  • optional 'mode' argument to load and loadfile (to control binary x text) -
  • optional 'env' argument to load and loadfile (environment for loaded chunk) -
  • loadlib may load libraries with global names (RTLD_GLOBAL) -
  • new function package.searchpath -
  • modules receive their paths when loaded -
  • optional base in math.log -
  • optional separator in string.rep -
  • file:write returns file -
  • closing a pipe returns exit status -
  • os.exit may close state -
  • new metamethods __pairs and __ipairs -
  • new option 'isrunning' for collectgarbage and lua_gc -
  • frontier patterns -
  • \0 in patterns -
  • new option *L for io.read -
  • options for io.lines -
  • debug.getlocal can access function varargs -
- -

C API

-
    -
  • main thread predefined in the registry -
  • new functions -lua_absindex, -lua_arith, -lua_compare, -lua_copy, -lua_len, -lua_rawgetp, -lua_rawsetp, -lua_upvalueid, -lua_upvaluejoin, -lua_version. -
  • new functions -luaL_checkversion, -luaL_setmetatable, -luaL_testudata, -luaL_tolstring. -
  • lua_pushstring and pushlstring return string -
  • nparams and isvararg available in debug API -
  • new lua_Unsigned -
- -

Implementation

-
    -
  • max constants per function raised to 226 -
  • generational mode for garbage collection (experimental) -
  • NaN trick (experimental) -
  • internal (immutable) version of ctypes -
  • simpler implementation for string buffers -
  • parser uses much less C-stack space (no more auto arrays) -
- -

Lua standalone interpreter

-
    -
  • new -E option to avoid environment variables -
  • handling of non-string error messages -
- -

License

- -[osi certified] - - -

-Lua is free software distributed under the terms of the -MIT license -reproduced below; -it may be used for any purpose, including commercial purposes, -at absolutely no cost without having to ask us. - -The only requirement is that if you do use Lua, -then you should give us credit by including the appropriate copyright notice somewhere in your product or its documentation. - -For details, see -this. - -

-Copyright © 1994–2015 Lua.org, PUC-Rio. - -

-Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -

-The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -

-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -

-

- -


- -Last update: -Mon Feb 23 22:25:08 BRT 2015 - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.c deleted file mode 100644 index d011431..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.c +++ /dev/null @@ -1,1284 +0,0 @@ -/* -** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua API -** See Copyright Notice in lua.h -*/ - - -#include -#include - -#define lapi_c -#define LUA_CORE - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" - - - -const char lua_ident[] = - "$LuaVersion: " LUA_COPYRIGHT " $" - "$LuaAuthors: " LUA_AUTHORS " $"; - - -/* value at a non-valid index */ -#define NONVALIDVALUE cast(TValue *, luaO_nilobject) - -/* corresponding test */ -#define isvalid(o) ((o) != luaO_nilobject) - -/* test for pseudo index */ -#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) - -/* test for valid but not pseudo index */ -#define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) - -#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index") - -#define api_checkstackindex(L, i, o) \ - api_check(L, isstackindex(i, o), "index not in the stack") - - -static TValue *index2addr (lua_State *L, int idx) { - CallInfo *ci = L->ci; - if (idx > 0) { - TValue *o = ci->func + idx; - api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return NONVALIDVALUE; - else return o; - } - else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return L->top + idx; - } - else if (idx == LUA_REGISTRYINDEX) - return &G(L)->l_registry; - else { /* upvalues */ - idx = LUA_REGISTRYINDEX - idx; - api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(ci->func)) /* light C function? */ - return NONVALIDVALUE; /* it has no upvalues */ - else { - CClosure *func = clCvalue(ci->func); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; - } - } -} - - -/* -** to be called by 'lua_checkstack' in protected mode, to grow stack -** capturing memory errors -*/ -static void growstack (lua_State *L, void *ud) { - int size = *(int *)ud; - luaD_growstack(L, size); -} - - -LUA_API int lua_checkstack (lua_State *L, int size) { - int res; - CallInfo *ci = L->ci; - lua_lock(L); - if (L->stack_last - L->top > size) /* stack large enough? */ - res = 1; /* yes; check is OK */ - else { /* no; need to grow stack */ - int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */ - res = 0; /* no */ - else /* try to grow stack */ - res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK); - } - if (res && ci->top < L->top + size) - ci->top = L->top + size; /* adjust frame top */ - lua_unlock(L); - return res; -} - - -LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { - int i; - if (from == to) return; - lua_lock(to); - api_checknelems(from, n); - api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); - from->top -= n; - for (i = 0; i < n; i++) { - setobj2s(to, to->top++, from->top + i); - } - lua_unlock(to); -} - - -LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { - lua_CFunction old; - lua_lock(L); - old = G(L)->panic; - G(L)->panic = panicf; - lua_unlock(L); - return old; -} - - -LUA_API const lua_Number *lua_version (lua_State *L) { - static const lua_Number version = LUA_VERSION_NUM; - if (L == NULL) return &version; - else return G(L)->version; -} - - - -/* -** basic stack manipulation -*/ - - -/* -** convert an acceptable stack index into an absolute index -*/ -LUA_API int lua_absindex (lua_State *L, int idx) { - return (idx > 0 || ispseudo(idx)) - ? idx - : cast_int(L->top - L->ci->func + idx); -} - - -LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); -} - - -LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->ci->func; - lua_lock(L); - if (idx >= 0) { - api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); - while (L->top < (func + 1) + idx) - setnilvalue(L->top++); - L->top = (func + 1) + idx; - } - else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* `subtract' index (index is negative) */ - } - lua_unlock(L); -} - - -LUA_API void lua_remove (lua_State *L, int idx) { - StkId p; - lua_lock(L); - p = index2addr(L, idx); - api_checkstackindex(L, idx, p); - while (++p < L->top) setobjs2s(L, p-1, p); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_insert (lua_State *L, int idx) { - StkId p; - StkId q; - lua_lock(L); - p = index2addr(L, idx); - api_checkstackindex(L, idx, p); - for (q = L->top; q > p; q--) /* use L->top as a temporary */ - setobjs2s(L, q, q - 1); - setobjs2s(L, p, L->top); - lua_unlock(L); -} - - -static void moveto (lua_State *L, TValue *fr, int idx) { - TValue *to = index2addr(L, idx); - api_checkvalidindex(L, to); - setobj(L, to, fr); - if (idx < LUA_REGISTRYINDEX) /* function upvalue? */ - luaC_barrier(L, clCvalue(L->ci->func), fr); - /* LUA_REGISTRYINDEX does not need gc barrier - (collector revisits it before finishing collection) */ -} - - -LUA_API void lua_replace (lua_State *L, int idx) { - lua_lock(L); - api_checknelems(L, 1); - moveto(L, L->top - 1, idx); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { - TValue *fr; - lua_lock(L); - fr = index2addr(L, fromidx); - moveto(L, fr, toidx); - lua_unlock(L); -} - - -LUA_API void lua_pushvalue (lua_State *L, int idx) { - lua_lock(L); - setobj2s(L, L->top, index2addr(L, idx)); - api_incr_top(L); - lua_unlock(L); -} - - - -/* -** access functions (stack -> C) -*/ - - -LUA_API int lua_type (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return (isvalid(o) ? ttypenv(o) : LUA_TNONE); -} - - -LUA_API const char *lua_typename (lua_State *L, int t) { - UNUSED(L); - return ttypename(t); -} - - -LUA_API int lua_iscfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return (ttislcf(o) || (ttisCclosure(o))); -} - - -LUA_API int lua_isnumber (lua_State *L, int idx) { - TValue n; - const TValue *o = index2addr(L, idx); - return tonumber(o, &n); -} - - -LUA_API int lua_isstring (lua_State *L, int idx) { - int t = lua_type(L, idx); - return (t == LUA_TSTRING || t == LUA_TNUMBER); -} - - -LUA_API int lua_isuserdata (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); - return (ttisuserdata(o) || ttislightuserdata(o)); -} - - -LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { - StkId o1 = index2addr(L, index1); - StkId o2 = index2addr(L, index2); - return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; -} - - -LUA_API void lua_arith (lua_State *L, int op) { - StkId o1; /* 1st operand */ - StkId o2; /* 2nd operand */ - lua_lock(L); - if (op != LUA_OPUNM) /* all other operations expect two operands */ - api_checknelems(L, 2); - else { /* for unary minus, add fake 2nd operand */ - api_checknelems(L, 1); - setobjs2s(L, L->top, L->top - 1); - L->top++; - } - o1 = L->top - 2; - o2 = L->top - 1; - if (ttisnumber(o1) && ttisnumber(o2)) { - setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2))); - } - else - luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD)); - L->top--; - lua_unlock(L); -} - - -LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { - StkId o1, o2; - int i = 0; - lua_lock(L); /* may call tag method */ - o1 = index2addr(L, index1); - o2 = index2addr(L, index2); - if (isvalid(o1) && isvalid(o2)) { - switch (op) { - case LUA_OPEQ: i = equalobj(L, o1, o2); break; - case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; - case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; - default: api_check(L, 0, "invalid option"); - } - } - lua_unlock(L); - return i; -} - - -LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) { - TValue n; - const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - if (isnum) *isnum = 1; - return nvalue(o); - } - else { - if (isnum) *isnum = 0; - return 0; - } -} - - -LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { - TValue n; - const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - lua_Integer res; - lua_Number num = nvalue(o); - lua_number2integer(res, num); - if (isnum) *isnum = 1; - return res; - } - else { - if (isnum) *isnum = 0; - return 0; - } -} - - -LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { - TValue n; - const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) { - lua_Unsigned res; - lua_Number num = nvalue(o); - lua_number2unsigned(res, num); - if (isnum) *isnum = 1; - return res; - } - else { - if (isnum) *isnum = 0; - return 0; - } -} - - -LUA_API int lua_toboolean (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); - return !l_isfalse(o); -} - - -LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - StkId o = index2addr(L, idx); - if (!ttisstring(o)) { - lua_lock(L); /* `luaV_tostring' may create a new string */ - if (!luaV_tostring(L, o)) { /* conversion failed? */ - if (len != NULL) *len = 0; - lua_unlock(L); - return NULL; - } - luaC_checkGC(L); - o = index2addr(L, idx); /* previous call may reallocate the stack */ - lua_unlock(L); - } - if (len != NULL) *len = tsvalue(o)->len; - return svalue(o); -} - - -LUA_API size_t lua_rawlen (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttypenv(o)) { - case LUA_TSTRING: return tsvalue(o)->len; - case LUA_TUSERDATA: return uvalue(o)->len; - case LUA_TTABLE: return luaH_getn(hvalue(o)); - default: return 0; - } -} - - -LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - if (ttislcf(o)) return fvalue(o); - else if (ttisCclosure(o)) - return clCvalue(o)->f; - else return NULL; /* not a C function */ -} - - -LUA_API void *lua_touserdata (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttypenv(o)) { - case LUA_TUSERDATA: return (rawuvalue(o) + 1); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; - } -} - - -LUA_API lua_State *lua_tothread (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return (!ttisthread(o)) ? NULL : thvalue(o); -} - - -LUA_API const void *lua_topointer (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttype(o)) { - case LUA_TTABLE: return hvalue(o); - case LUA_TLCL: return clLvalue(o); - case LUA_TCCL: return clCvalue(o); - case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); - case LUA_TTHREAD: return thvalue(o); - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - return lua_touserdata(L, idx); - default: return NULL; - } -} - - - -/* -** push functions (C -> stack) -*/ - - -LUA_API void lua_pushnil (lua_State *L) { - lua_lock(L); - setnilvalue(L->top); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { - lua_lock(L); - setnvalue(L->top, n); - luai_checknum(L, L->top, - luaG_runerror(L, "C API - attempt to push a signaling NaN")); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { - lua_lock(L); - setnvalue(L->top, cast_num(n)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { - lua_Number n; - lua_lock(L); - n = lua_unsigned2number(u); - setnvalue(L->top, n); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { - TString *ts; - lua_lock(L); - luaC_checkGC(L); - ts = luaS_newlstr(L, s, len); - setsvalue2s(L, L->top, ts); - api_incr_top(L); - lua_unlock(L); - return getstr(ts); -} - - -LUA_API const char *lua_pushstring (lua_State *L, const char *s) { - if (s == NULL) { - lua_pushnil(L); - return NULL; - } - else { - TString *ts; - lua_lock(L); - luaC_checkGC(L); - ts = luaS_new(L, s); - setsvalue2s(L, L->top, ts); - api_incr_top(L); - lua_unlock(L); - return getstr(ts); - } -} - - -LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, - va_list argp) { - const char *ret; - lua_lock(L); - luaC_checkGC(L); - ret = luaO_pushvfstring(L, fmt, argp); - lua_unlock(L); - return ret; -} - - -LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { - const char *ret; - va_list argp; - lua_lock(L); - luaC_checkGC(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - lua_unlock(L); - return ret; -} - - -LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { - lua_lock(L); - if (n == 0) { - setfvalue(L->top, fn); - } - else { - Closure *cl; - api_checknelems(L, n); - api_check(L, n <= MAXUPVAL, "upvalue index too large"); - luaC_checkGC(L); - cl = luaF_newCclosure(L, n); - cl->c.f = fn; - L->top -= n; - while (n--) - setobj2n(L, &cl->c.upvalue[n], L->top + n); - setclCvalue(L, L->top, cl); - } - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushboolean (lua_State *L, int b) { - lua_lock(L); - setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { - lua_lock(L); - setpvalue(L->top, p); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API int lua_pushthread (lua_State *L) { - lua_lock(L); - setthvalue(L, L->top, L); - api_incr_top(L); - lua_unlock(L); - return (G(L)->mainthread == L); -} - - - -/* -** get functions (Lua -> stack) -*/ - - -LUA_API void lua_getglobal (lua_State *L, const char *var) { - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt; /* global table */ - lua_lock(L); - gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, var)); - luaV_gettable(L, gt, L->top - 1, L->top - 1); - lua_unlock(L); -} - - -LUA_API void lua_gettable (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - luaV_gettable(L, t, L->top - 1, L->top - 1); - lua_unlock(L); -} - - -LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - setsvalue2s(L, L->top, luaS_new(L, k)); - api_incr_top(L); - luaV_gettable(L, t, L->top - 1, L->top - 1); - lua_unlock(L); -} - - -LUA_API void lua_rawget (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); - lua_unlock(L); -} - - -LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top, luaH_getint(hvalue(t), n)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) { - StkId t; - TValue k; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setpvalue(&k, cast(void *, p)); - setobj2s(L, L->top, luaH_get(hvalue(t), &k)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { - Table *t; - lua_lock(L); - luaC_checkGC(L); - t = luaH_new(L); - sethvalue(L, L->top, t); - api_incr_top(L); - if (narray > 0 || nrec > 0) - luaH_resize(L, t, narray, nrec); - lua_unlock(L); -} - - -LUA_API int lua_getmetatable (lua_State *L, int objindex) { - const TValue *obj; - Table *mt = NULL; - int res; - lua_lock(L); - obj = index2addr(L, objindex); - switch (ttypenv(obj)) { - case LUA_TTABLE: - mt = hvalue(obj)->metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(obj)->metatable; - break; - default: - mt = G(L)->mt[ttypenv(obj)]; - break; - } - if (mt == NULL) - res = 0; - else { - sethvalue(L, L->top, mt); - api_incr_top(L); - res = 1; - } - lua_unlock(L); - return res; -} - - -LUA_API void lua_getuservalue (lua_State *L, int idx) { - StkId o; - lua_lock(L); - o = index2addr(L, idx); - api_check(L, ttisuserdata(o), "userdata expected"); - if (uvalue(o)->env) { - sethvalue(L, L->top, uvalue(o)->env); - } else - setnilvalue(L->top); - api_incr_top(L); - lua_unlock(L); -} - - -/* -** set functions (stack -> Lua) -*/ - - -LUA_API void lua_setglobal (lua_State *L, const char *var) { - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt; /* global table */ - lua_lock(L); - api_checknelems(L, 1); - gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - setsvalue2s(L, L->top++, luaS_new(L, var)); - luaV_settable(L, gt, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ - lua_unlock(L); -} - - -LUA_API void lua_settable (lua_State *L, int idx) { - StkId t; - lua_lock(L); - api_checknelems(L, 2); - t = index2addr(L, idx); - luaV_settable(L, t, L->top - 2, L->top - 1); - L->top -= 2; /* pop index and value */ - lua_unlock(L); -} - - -LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { - StkId t; - lua_lock(L); - api_checknelems(L, 1); - t = index2addr(L, idx); - setsvalue2s(L, L->top++, luaS_new(L, k)); - luaV_settable(L, t, L->top - 1, L->top - 2); - L->top -= 2; /* pop value and key */ - lua_unlock(L); -} - - -LUA_API void lua_rawset (lua_State *L, int idx) { - StkId t; - lua_lock(L); - api_checknelems(L, 2); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); - invalidateTMcache(hvalue(t)); - luaC_barrierback(L, gcvalue(t), L->top-1); - L->top -= 2; - lua_unlock(L); -} - - -LUA_API void lua_rawseti (lua_State *L, int idx, int n) { - StkId t; - lua_lock(L); - api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - luaH_setint(L, hvalue(t), n, L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top-1); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - StkId t; - TValue k; - lua_lock(L); - api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setpvalue(&k, cast(void *, p)); - setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top - 1); - L->top--; - lua_unlock(L); -} - - -LUA_API int lua_setmetatable (lua_State *L, int objindex) { - TValue *obj; - Table *mt; - lua_lock(L); - api_checknelems(L, 1); - obj = index2addr(L, objindex); - if (ttisnil(L->top - 1)) - mt = NULL; - else { - api_check(L, ttistable(L->top - 1), "table expected"); - mt = hvalue(L->top - 1); - } - switch (ttypenv(obj)) { - case LUA_TTABLE: { - hvalue(obj)->metatable = mt; - if (mt) { - luaC_objbarrierback(L, gcvalue(obj), mt); - luaC_checkfinalizer(L, gcvalue(obj), mt); - } - break; - } - case LUA_TUSERDATA: { - uvalue(obj)->metatable = mt; - if (mt) { - luaC_objbarrier(L, rawuvalue(obj), mt); - luaC_checkfinalizer(L, gcvalue(obj), mt); - } - break; - } - default: { - G(L)->mt[ttypenv(obj)] = mt; - break; - } - } - L->top--; - lua_unlock(L); - return 1; -} - - -LUA_API void lua_setuservalue (lua_State *L, int idx) { - StkId o; - lua_lock(L); - api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttisuserdata(o), "userdata expected"); - if (ttisnil(L->top - 1)) - uvalue(o)->env = NULL; - else { - api_check(L, ttistable(L->top - 1), "table expected"); - uvalue(o)->env = hvalue(L->top - 1); - luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); - } - L->top--; - lua_unlock(L); -} - - -/* -** `load' and `call' functions (run Lua code) -*/ - - -#define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ - "results from function overflow current stack size") - - -LUA_API int lua_getctx (lua_State *L, int *ctx) { - if (L->ci->callstatus & CIST_YIELDED) { - if (ctx) *ctx = L->ci->u.c.ctx; - return L->ci->u.c.status; - } - else return LUA_OK; -} - - -LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, - lua_CFunction k) { - StkId func; - lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), - "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); - checkresults(L, nargs, nresults); - func = L->top - (nargs+1); - if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ - L->ci->u.c.k = k; /* save continuation */ - L->ci->u.c.ctx = ctx; /* save context */ - luaD_call(L, func, nresults, 1); /* do the call */ - } - else /* no continuation or no yieldable */ - luaD_call(L, func, nresults, 0); /* just do the call */ - adjustresults(L, nresults); - lua_unlock(L); -} - - - -/* -** Execute a protected call. -*/ -struct CallS { /* data to `f_call' */ - StkId func; - int nresults; -}; - - -static void f_call (lua_State *L, void *ud) { - struct CallS *c = cast(struct CallS *, ud); - luaD_call(L, c->func, c->nresults, 0); -} - - - -LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_CFunction k) { - struct CallS c; - int status; - ptrdiff_t func; - lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), - "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); - checkresults(L, nargs, nresults); - if (errfunc == 0) - func = 0; - else { - StkId o = index2addr(L, errfunc); - api_checkstackindex(L, errfunc, o); - func = savestack(L, o); - } - c.func = L->top - (nargs+1); /* function to be called */ - if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ - c.nresults = nresults; /* do a 'conventional' protected call */ - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); - } - else { /* prepare continuation (call is already protected by 'resume') */ - CallInfo *ci = L->ci; - ci->u.c.k = k; /* save continuation */ - ci->u.c.ctx = ctx; /* save context */ - /* save information for error recovery */ - ci->extra = savestack(L, c.func); - ci->u.c.old_allowhook = L->allowhook; - ci->u.c.old_errfunc = L->errfunc; - L->errfunc = func; - /* mark that function may do error recovery */ - ci->callstatus |= CIST_YPCALL; - luaD_call(L, c.func, nresults, 1); /* do the call */ - ci->callstatus &= ~CIST_YPCALL; - L->errfunc = ci->u.c.old_errfunc; - status = LUA_OK; /* if it is here, there were no errors */ - } - adjustresults(L, nresults); - lua_unlock(L); - return status; -} - - -LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, - const char *chunkname, const char *mode) { - ZIO z; - int status; - lua_lock(L); - if (!chunkname) chunkname = "?"; - luaZ_init(L, &z, reader, data); - status = luaD_protectedparser(L, &z, chunkname, mode); - if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(L->top - 1); /* get newly created function */ - if (f->nupvalues == 1) { /* does it have one upvalue? */ - /* get global table from registry */ - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); - luaC_barrier(L, f->upvals[0], gt); - } - } - lua_unlock(L); - return status; -} - - -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { - int status; - TValue *o; - lua_lock(L); - api_checknelems(L, 1); - o = L->top - 1; - if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, 0); - else - status = 1; - lua_unlock(L); - return status; -} - - -LUA_API int lua_status (lua_State *L) { - return L->status; -} - - -/* -** Garbage-collection function -*/ - -LUA_API int lua_gc (lua_State *L, int what, int data) { - int res = 0; - global_State *g; - lua_lock(L); - g = G(L); - switch (what) { - case LUA_GCSTOP: { - g->gcrunning = 0; - break; - } - case LUA_GCRESTART: { - luaE_setdebt(g, 0); - g->gcrunning = 1; - break; - } - case LUA_GCCOLLECT: { - luaC_fullgc(L, 0); - break; - } - case LUA_GCCOUNT: { - /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(gettotalbytes(g) >> 10); - break; - } - case LUA_GCCOUNTB: { - res = cast_int(gettotalbytes(g) & 0x3ff); - break; - } - case LUA_GCSTEP: { - if (g->gckind == KGC_GEN) { /* generational mode? */ - res = (g->GCestimate == 0); /* true if it will do major collection */ - luaC_forcestep(L); /* do a single step */ - } - else { - lu_mem debt = cast(lu_mem, data) * 1024 - GCSTEPSIZE; - if (g->gcrunning) - debt += g->GCdebt; /* include current debt */ - luaE_setdebt(g, debt); - luaC_forcestep(L); - if (g->gcstate == GCSpause) /* end of cycle? */ - res = 1; /* signal it */ - } - break; - } - case LUA_GCSETPAUSE: { - res = g->gcpause; - g->gcpause = data; - break; - } - case LUA_GCSETMAJORINC: { - res = g->gcmajorinc; - g->gcmajorinc = data; - break; - } - case LUA_GCSETSTEPMUL: { - res = g->gcstepmul; - g->gcstepmul = data; - break; - } - case LUA_GCISRUNNING: { - res = g->gcrunning; - break; - } - case LUA_GCGEN: { /* change collector to generational mode */ - luaC_changemode(L, KGC_GEN); - break; - } - case LUA_GCINC: { /* change collector to incremental mode */ - luaC_changemode(L, KGC_NORMAL); - break; - } - default: res = -1; /* invalid option */ - } - lua_unlock(L); - return res; -} - - - -/* -** miscellaneous functions -*/ - - -LUA_API int lua_error (lua_State *L) { - lua_lock(L); - api_checknelems(L, 1); - luaG_errormsg(L); - /* code unreachable; will unlock when control actually leaves the kernel */ - return 0; /* to avoid warnings */ -} - - -LUA_API int lua_next (lua_State *L, int idx) { - StkId t; - int more; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - more = luaH_next(L, hvalue(t), L->top - 1); - if (more) { - api_incr_top(L); - } - else /* no more elements */ - L->top -= 1; /* remove key */ - lua_unlock(L); - return more; -} - - -LUA_API void lua_concat (lua_State *L, int n) { - lua_lock(L); - api_checknelems(L, n); - if (n >= 2) { - luaC_checkGC(L); - luaV_concat(L, n); - } - else if (n == 0) { /* push empty string */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); - api_incr_top(L); - } - /* else n == 1; nothing to do */ - lua_unlock(L); -} - - -LUA_API void lua_len (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - luaV_objlen(L, L->top, t); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { - lua_Alloc f; - lua_lock(L); - if (ud) *ud = G(L)->ud; - f = G(L)->frealloc; - lua_unlock(L); - return f; -} - - -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { - lua_lock(L); - G(L)->ud = ud; - G(L)->frealloc = f; - lua_unlock(L); -} - - -LUA_API void *lua_newuserdata (lua_State *L, size_t size) { - Udata *u; - lua_lock(L); - luaC_checkGC(L); - u = luaS_newudata(L, size, NULL); - setuvalue(L, L->top, u); - api_incr_top(L); - lua_unlock(L); - return u + 1; -} - - - -static const char *aux_upvalue (StkId fi, int n, TValue **val, - GCObject **owner) { - switch (ttype(fi)) { - case LUA_TCCL: { /* C closure */ - CClosure *f = clCvalue(fi); - if (!(1 <= n && n <= f->nupvalues)) return NULL; - *val = &f->upvalue[n-1]; - if (owner) *owner = obj2gco(f); - return ""; - } - case LUA_TLCL: { /* Lua closure */ - LClosure *f = clLvalue(fi); - TString *name; - Proto *p = f->p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; - *val = f->upvals[n-1]->v; - if (owner) *owner = obj2gco(f->upvals[n - 1]); - name = p->upvalues[n-1].name; - return (name == NULL) ? "" : getstr(name); - } - default: return NULL; /* not a closure */ - } -} - - -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val = NULL; /* to avoid warnings */ - lua_lock(L); - name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL); - if (name) { - setobj2s(L, L->top, val); - api_incr_top(L); - } - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val = NULL; /* to avoid warnings */ - GCObject *owner = NULL; /* to avoid warnings */ - StkId fi; - lua_lock(L); - fi = index2addr(L, funcindex); - api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner); - if (name) { - L->top--; - setobj(L, val, L->top); - luaC_barrier(L, owner, L->top); - } - lua_unlock(L); - return name; -} - - -static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { - LClosure *f; - StkId fi = index2addr(L, fidx); - api_check(L, ttisLclosure(fi), "Lua function expected"); - f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); - if (pf) *pf = f; - return &f->upvals[n - 1]; /* get its upvalue pointer */ -} - - -LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { - StkId fi = index2addr(L, fidx); - switch (ttype(fi)) { - case LUA_TLCL: { /* lua closure */ - return *getupvalref(L, fidx, n, NULL); - } - case LUA_TCCL: { /* C closure */ - CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); - return &f->upvalue[n - 1]; - } - default: { - api_check(L, 0, "closure expected"); - return NULL; - } - } -} - - -LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, - int fidx2, int n2) { - LClosure *f1; - UpVal **up1 = getupvalref(L, fidx1, n1, &f1); - UpVal **up2 = getupvalref(L, fidx2, n2, NULL); - *up1 = *up2; - luaC_objbarrier(L, f1, *up2); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.h deleted file mode 100644 index c7d34ad..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lapi.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ -** Auxiliary functions from Lua API -** See Copyright Notice in lua.h -*/ - -#ifndef lapi_h -#define lapi_h - - -#include "llimits.h" -#include "lstate.h" - -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ - "stack overflow");} - -#define adjustresults(L,nres) \ - { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } - -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ - "not enough elements in the stack") - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.c deleted file mode 100644 index b00f8c7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.c +++ /dev/null @@ -1,959 +0,0 @@ -/* -** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include -#include - - -/* This file uses only the official API of Lua. -** Any function declared here could be written as an application function. -*/ - -#define lauxlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" - - -/* -** {====================================================== -** Traceback -** ======================================================= -*/ - - -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ - - - -/* -** search for 'objidx' in table at index -1. -** return 1 + string at top if find a good name. -*/ -static int findfield (lua_State *L, int objidx, int level) { - if (level == 0 || !lua_istable(L, -1)) - return 0; /* not found */ - lua_pushnil(L); /* start 'next' loop */ - while (lua_next(L, -2)) { /* for each pair in table */ - if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ - if (lua_rawequal(L, objidx, -1)) { /* found object? */ - lua_pop(L, 1); /* remove value (but keep name) */ - return 1; - } - else if (findfield(L, objidx, level - 1)) { /* try recursively */ - lua_remove(L, -2); /* remove table (but keep name) */ - lua_pushliteral(L, "."); - lua_insert(L, -2); /* place '.' between the two names */ - lua_concat(L, 3); - return 1; - } - } - lua_pop(L, 1); /* remove value */ - } - return 0; /* not found */ -} - - -static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { - int top = lua_gettop(L); - lua_getinfo(L, "f", ar); /* push function */ - lua_pushglobaltable(L); - if (findfield(L, top + 1, 2)) { - lua_copy(L, -1, top + 1); /* move name to proper place */ - lua_pop(L, 2); /* remove pushed values */ - return 1; - } - else { - lua_settop(L, top); /* remove function and global table */ - return 0; - } -} - - -static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (*ar->namewhat != '\0') /* is there a name? */ - lua_pushfstring(L, "function " LUA_QS, ar->name); - else if (*ar->what == 'm') /* main? */ - lua_pushliteral(L, "main chunk"); - else if (*ar->what == 'C') { - if (pushglobalfuncname(L, ar)) { - lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else - lua_pushliteral(L, "?"); - } - else - lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); -} - - -static int countlevels (lua_State *L) { - lua_Debug ar; - int li = 1, le = 1; - /* find an upper bound */ - while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } - /* do a binary search */ - while (li < le) { - int m = (li + le)/2; - if (lua_getstack(L, m, &ar)) li = m + 1; - else le = m; - } - return le - 1; -} - - -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, - const char *msg, int level) { - lua_Debug ar; - int top = lua_gettop(L); - int numlevels = countlevels(L1); - int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0; - if (msg) lua_pushfstring(L, "%s\n", msg); - lua_pushliteral(L, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - if (level == mark) { /* too many levels? */ - lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = numlevels - LEVELS2; /* and skip to last ones */ - } - else { - lua_getinfo(L1, "Slnt", &ar); - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - lua_pushliteral(L, " in "); - pushfuncname(L, &ar); - if (ar.istailcall) - lua_pushliteral(L, "\n\t(...tail calls...)"); - lua_concat(L, lua_gettop(L) - top); - } - } - lua_concat(L, lua_gettop(L) - top); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Error-report functions -** ======================================================= -*/ - -LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { - lua_Debug ar; - if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ - return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); - lua_getinfo(L, "n", &ar); - if (strcmp(ar.namewhat, "method") == 0) { - narg--; /* do not count `self' */ - if (narg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling " LUA_QS " on bad self (%s)", - ar.name, extramsg); - } - if (ar.name == NULL) - ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", - narg, ar.name, extramsg); -} - - -static int typeerror (lua_State *L, int narg, const char *tname) { - const char *msg = lua_pushfstring(L, "%s expected, got %s", - tname, luaL_typename(L, narg)); - return luaL_argerror(L, narg, msg); -} - - -static void tag_error (lua_State *L, int narg, int tag) { - typeerror(L, narg, lua_typename(L, tag)); -} - - -LUALIB_API void luaL_where (lua_State *L, int level) { - lua_Debug ar; - if (lua_getstack(L, level, &ar)) { /* check function at level */ - lua_getinfo(L, "Sl", &ar); /* get info about it */ - if (ar.currentline > 0) { /* is there info? */ - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); - return; - } - } - lua_pushliteral(L, ""); /* else, no information available... */ -} - - -LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - luaL_where(L, 1); - lua_pushvfstring(L, fmt, argp); - va_end(argp); - lua_concat(L, 2); - return lua_error(L); -} - - -LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { - int en = errno; /* calls to Lua API may change this value */ - if (stat) { - lua_pushboolean(L, 1); - return 1; - } - else { - lua_pushnil(L); - if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); - else - lua_pushstring(L, strerror(en)); - lua_pushinteger(L, en); - return 3; - } -} - - -#if !defined(inspectstat) /* { */ - -#if defined(LUA_USE_POSIX) - -#include - -/* -** use appropriate macros to interpret 'pclose' return status -*/ -#define inspectstat(stat,what) \ - if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ - else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } - -#else - -#define inspectstat(stat,what) /* no op */ - -#endif - -#endif /* } */ - - -LUALIB_API int luaL_execresult (lua_State *L, int stat) { - const char *what = "exit"; /* type of termination */ - if (stat == -1) /* error? */ - return luaL_fileresult(L, 0, NULL); - else { - inspectstat(stat, what); /* interpret result */ - if (*what == 'e' && stat == 0) /* successful termination? */ - lua_pushboolean(L, 1); - else - lua_pushnil(L); - lua_pushstring(L, what); - lua_pushinteger(L, stat); - return 3; /* return true/nil,what,code */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Userdata's metatable manipulation -** ======================================================= -*/ - -LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - luaL_getmetatable(L, tname); /* try to get metatable */ - if (!lua_isnil(L, -1)) /* name already in use? */ - return 0; /* leave previous value on top, but return 0 */ - lua_pop(L, 1); - lua_newtable(L); /* create metatable */ - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ - return 1; -} - - -LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { - luaL_getmetatable(L, tname); - lua_setmetatable(L, -2); -} - - -LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); - if (p != NULL) { /* value is a userdata? */ - if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ - luaL_getmetatable(L, tname); /* get correct metatable */ - if (!lua_rawequal(L, -1, -2)) /* not the same? */ - p = NULL; /* value is a userdata with wrong metatable */ - lua_pop(L, 2); /* remove both metatables */ - return p; - } - } - return NULL; /* value is not a userdata with a metatable */ -} - - -LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - void *p = luaL_testudata(L, ud, tname); - if (p == NULL) typeerror(L, ud, tname); - return p; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Argument check functions -** ======================================================= -*/ - -LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, - const char *const lst[]) { - const char *name = (def) ? luaL_optstring(L, narg, def) : - luaL_checkstring(L, narg); - int i; - for (i=0; lst[i]; i++) - if (strcmp(lst[i], name) == 0) - return i; - return luaL_argerror(L, narg, - lua_pushfstring(L, "invalid option " LUA_QS, name)); -} - - -LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - /* keep some extra space to run error routines, if needed */ - const int extra = LUA_MINSTACK; - if (!lua_checkstack(L, space + extra)) { - if (msg) - luaL_error(L, "stack overflow (%s)", msg); - else - luaL_error(L, "stack overflow"); - } -} - - -LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { - if (lua_type(L, narg) != t) - tag_error(L, narg, t); -} - - -LUALIB_API void luaL_checkany (lua_State *L, int narg) { - if (lua_type(L, narg) == LUA_TNONE) - luaL_argerror(L, narg, "value expected"); -} - - -LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { - const char *s = lua_tolstring(L, narg, len); - if (!s) tag_error(L, narg, LUA_TSTRING); - return s; -} - - -LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, - const char *def, size_t *len) { - if (lua_isnoneornil(L, narg)) { - if (len) - *len = (def ? strlen(def) : 0); - return def; - } - else return luaL_checklstring(L, narg, len); -} - - -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { - int isnum; - lua_Number d = lua_tonumberx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { - return luaL_opt(L, luaL_checknumber, narg, def); -} - - -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { - int isnum; - lua_Integer d = lua_tointegerx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) { - int isnum; - lua_Unsigned d = lua_tounsignedx(L, narg, &isnum); - if (!isnum) - tag_error(L, narg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, - lua_Integer def) { - return luaL_opt(L, luaL_checkinteger, narg, def); -} - - -LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, - lua_Unsigned def) { - return luaL_opt(L, luaL_checkunsigned, narg, def); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - -/* -** check whether buffer is using a userdata on the stack as a temporary -** buffer -*/ -#define buffonstack(B) ((B)->b != (B)->initb) - - -/* -** returns a pointer to a free area with at least 'sz' bytes -*/ -LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { - lua_State *L = B->L; - if (B->size - B->n < sz) { /* not enough space? */ - char *newbuff; - size_t newsize = B->size * 2; /* double buffer size */ - if (newsize - B->n < sz) /* not big enough? */ - newsize = B->n + sz; - if (newsize < B->n || newsize - B->n < sz) - luaL_error(L, "buffer too large"); - /* create larger buffer */ - newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char)); - /* move content to new buffer */ - memcpy(newbuff, B->b, B->n * sizeof(char)); - if (buffonstack(B)) - lua_remove(L, -2); /* remove old buffer */ - B->b = newbuff; - B->size = newsize; - } - return &B->b[B->n]; -} - - -LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - char *b = luaL_prepbuffsize(B, l); - memcpy(b, s, l * sizeof(char)); - luaL_addsize(B, l); -} - - -LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { - luaL_addlstring(B, s, strlen(s)); -} - - -LUALIB_API void luaL_pushresult (luaL_Buffer *B) { - lua_State *L = B->L; - lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) - lua_remove(L, -2); /* remove old buffer */ -} - - -LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { - luaL_addsize(B, sz); - luaL_pushresult(B); -} - - -LUALIB_API void luaL_addvalue (luaL_Buffer *B) { - lua_State *L = B->L; - size_t l; - const char *s = lua_tolstring(L, -1, &l); - if (buffonstack(B)) - lua_insert(L, -2); /* put value below buffer */ - luaL_addlstring(B, s, l); - lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */ -} - - -LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { - B->L = L; - B->b = B->initb; - B->n = 0; - B->size = LUAL_BUFFERSIZE; -} - - -LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { - luaL_buffinit(L, B); - return luaL_prepbuffsize(B, sz); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Reference system -** ======================================================= -*/ - -/* index of free-list header */ -#define freelist 0 - - -LUALIB_API int luaL_ref (lua_State *L, int t) { - int ref; - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* `nil' has a unique fixed reference */ - } - t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ - lua_pop(L, 1); /* remove it from stack */ - if (ref != 0) { /* any free element? */ - lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ - } - else /* no free elements */ - ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ - lua_rawseti(L, t, ref); - return ref; -} - - -LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { - if (ref >= 0) { - t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); - lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ - lua_pushinteger(L, ref); - lua_rawseti(L, t, freelist); /* t[freelist] = ref */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Load functions -** ======================================================= -*/ - -typedef struct LoadF { - int n; /* number of pre-read characters */ - FILE *f; /* file being read */ - char buff[LUAL_BUFFERSIZE]; /* area for reading file */ -} LoadF; - - -static const char *getF (lua_State *L, void *ud, size_t *size) { - LoadF *lf = (LoadF *)ud; - (void)L; /* not used */ - if (lf->n > 0) { /* are there pre-read characters to be read? */ - *size = lf->n; /* return them (chars already in buffer) */ - lf->n = 0; /* no more pre-read characters */ - } - else { /* read a block from file */ - /* 'fread' can return > 0 *and* set the EOF flag. If next call to - 'getF' called 'fread', it might still wait for user input. - The next check avoids this problem. */ - if (feof(lf->f)) return NULL; - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ - } - return lf->buff; -} - - -static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); - const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); - lua_remove(L, fnameindex); - return LUA_ERRFILE; -} - - -static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ -} - - -/* -** reads the first character of file 'f' and skips an optional BOM mark -** in its beginning plus its first line if it starts with '#'. Returns -** true if it skipped the first line. In any case, '*cp' has the -** first "valid" character of the file (after the optional BOM and -** a first-line comment). -*/ -static int skipcomment (LoadF *lf, int *cp) { - int c = *cp = skipBOM(lf); - if (c == '#') { /* first line is a comment (Unix exec. file)? */ - do { /* skip first line */ - c = getc(lf->f); - } while (c != EOF && c != '\n') ; - *cp = getc(lf->f); /* skip end-of-line, if present */ - return 1; /* there was a comment */ - } - else return 0; /* no comment */ -} - - -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, - const char *mode) { - LoadF lf; - int status, readstatus; - int c; - int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ - if (filename == NULL) { - lua_pushliteral(L, "=stdin"); - lf.f = stdin; - } - else { - lua_pushfstring(L, "@%s", filename); - lf.f = fopen(filename, "r"); - if (lf.f == NULL) return errfile(L, "open", fnameindex); - } - if (skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(&lf, &c); /* re-read initial portion */ - } - if (c != EOF) - lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ - status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); - readstatus = ferror(lf.f); - if (filename) fclose(lf.f); /* close file (even in case of errors) */ - if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from `lua_load' */ - return errfile(L, "read", fnameindex); - } - lua_remove(L, fnameindex); - return status; -} - - -typedef struct LoadS { - const char *s; - size_t size; -} LoadS; - - -static const char *getS (lua_State *L, void *ud, size_t *size) { - LoadS *ls = (LoadS *)ud; - (void)L; /* not used */ - if (ls->size == 0) return NULL; - *size = ls->size; - ls->size = 0; - return ls->s; -} - - -LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, - const char *name, const char *mode) { - LoadS ls; - ls.s = buff; - ls.size = size; - return lua_load(L, getS, &ls, name, mode); -} - - -LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { - return luaL_loadbuffer(L, s, strlen(s), s); -} - -/* }====================================================== */ - - - -LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { - if (!lua_getmetatable(L, obj)) /* no metatable? */ - return 0; - lua_pushstring(L, event); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { - lua_pop(L, 2); /* remove metatable and metafield */ - return 0; - } - else { - lua_remove(L, -2); /* remove only metatable */ - return 1; - } -} - - -LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { - obj = lua_absindex(L, obj); - if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ - return 0; - lua_pushvalue(L, obj); - lua_call(L, 1, 1); - return 1; -} - - -LUALIB_API int luaL_len (lua_State *L, int idx) { - int l; - int isnum; - lua_len(L, idx); - l = (int)lua_tointegerx(L, -1, &isnum); - if (!isnum) - luaL_error(L, "object length is not a number"); - lua_pop(L, 1); /* remove object */ - return l; -} - - -LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { - if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ - switch (lua_type(L, idx)) { - case LUA_TNUMBER: - case LUA_TSTRING: - lua_pushvalue(L, idx); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); - break; - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - default: - lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), - lua_topointer(L, idx)); - break; - } - } - return lua_tolstring(L, -1, len); -} - - -/* -** {====================================================== -** Compatibility with 5.1 module functions -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -static const char *luaL_findtable (lua_State *L, int idx, - const char *fname, int szhint) { - const char *e; - if (idx) lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, e - fname); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { /* no such field? */ - lua_pop(L, 1); /* remove this nil */ - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ - lua_pushlstring(L, fname, e - fname); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } - else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - - -/* -** Count number of elements in a luaL_Reg list. -*/ -static int libsize (const luaL_Reg *l) { - int size = 0; - for (; l && l->name; l++) size++; - return size; -} - - -/* -** Find or create a module table with a given name. The function -** first looks at the _LOADED table and, if that fails, try a -** global variable with that name. In any case, leaves on the stack -** the module table. -*/ -LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, - int sizehint) { - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ - lua_getfield(L, -1, modname); /* get _LOADED[modname] */ - if (!lua_istable(L, -1)) { /* not found? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - lua_pushglobaltable(L); - if (luaL_findtable(L, 0, modname, sizehint) != NULL) - luaL_error(L, "name conflict for module " LUA_QS, modname); - lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ - } - lua_remove(L, -2); /* remove _LOADED table */ -} - - -LUALIB_API void luaL_openlib (lua_State *L, const char *libname, - const luaL_Reg *l, int nup) { - luaL_checkversion(L); - if (libname) { - luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ - lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ - } - if (l) - luaL_setfuncs(L, l, nup); - else - lua_pop(L, nup); /* remove upvalues */ -} - -#endif -/* }====================================================== */ - -/* -** set functions from list 'l' into table at top - 'nup'; each -** function gets the 'nup' elements at the top as upvalues. -** Returns with only the table at the stack. -*/ -LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkversion(L); - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - - -/* -** ensure that stack[idx][fname] has a table and push that table -** into the stack -*/ -LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { - lua_getfield(L, idx, fname); - if (lua_istable(L, -1)) return 1; /* table already there */ - else { - lua_pop(L, 1); /* remove previous result */ - idx = lua_absindex(L, idx); - lua_newtable(L); - lua_pushvalue(L, -1); /* copy to be left at top */ - lua_setfield(L, idx, fname); /* assign new table to field */ - return 0; /* false, because did not find table there */ - } -} - - -/* -** stripped-down 'require'. Calls 'openf' to open a module, -** registers the result in 'package.loaded' table and, if 'glb' -** is true, also registers the result in the global table. -** Leaves resulting module on the top. -*/ -LUALIB_API void luaL_requiref (lua_State *L, const char *modname, - lua_CFunction openf, int glb) { - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); /* argument to open function */ - lua_call(L, 1, 1); /* open module */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_pushvalue(L, -2); /* make copy of module (call result) */ - lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ - lua_pop(L, 1); /* remove _LOADED table */ - if (glb) { - lua_pushvalue(L, -1); /* copy of 'mod' */ - lua_setglobal(L, modname); /* _G[modname] = module */ - } -} - - -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, - const char *r) { - const char *wild; - size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); - while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, wild - s); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after `p' */ - } - luaL_addstring(&b, s); /* push last suffix */ - luaL_pushresult(&b); - return lua_tostring(L, -1); -} - - -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { - (void)ud; (void)osize; /* not used */ - if (nsize == 0) { - free(ptr); - return NULL; - } - else - return realloc(ptr, nsize); -} - - -static int panic (lua_State *L) { - luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); - return 0; /* return to Lua to abort */ -} - - -LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); - if (L) lua_atpanic(L, &panic); - return L; -} - - -LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) { - const lua_Number *v = lua_version(L); - if (v != lua_version(NULL)) - luaL_error(L, "multiple Lua VMs detected"); - else if (*v != ver) - luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - ver, *v); - /* check conversions number -> integer types */ - lua_pushnumber(L, -(lua_Number)0x1234); - if (lua_tointeger(L, -1) != -0x1234 || - lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234) - luaL_error(L, "bad conversion number->int;" - " must recompile Lua with proper settings"); - lua_pop(L, 1); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.h deleted file mode 100644 index 0fb023b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lauxlib.h +++ /dev/null @@ -1,212 +0,0 @@ -/* -** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lauxlib_h -#define lauxlib_h - - -#include -#include - -#include "lua.h" - - - -/* extra error code for `luaL_load' */ -#define LUA_ERRFILE (LUA_ERRERR+1) - - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - - -LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); -#define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) - -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); -LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, - size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, - const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); - -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, - lua_Integer def); -LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); -LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, - lua_Unsigned def); - -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); - -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); -LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); - -LUALIB_API void (luaL_where) (lua_State *L, int lvl); -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); - -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, - const char *const lst[]); - -LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); -LUALIB_API int (luaL_execresult) (lua_State *L, int stat); - -/* pre-defined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -LUALIB_API int (luaL_ref) (lua_State *L, int t); -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); - -LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, - const char *mode); - -#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) - -LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, - const char *name, const char *mode); -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); - -LUALIB_API lua_State *(luaL_newstate) (void); - -LUALIB_API int (luaL_len) (lua_State *L, int idx); - -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); - -LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); - -LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); - -LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, - const char *msg, int level); - -LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, - lua_CFunction openf, int glb); - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - - -#define luaL_newlibtable(L,l) \ - lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) - -#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) - -#define luaL_argcheck(L, cond,numarg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) - -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - -#define luaL_dofile(L, fn) \ - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_dostring(L, s) \ - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) - -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) - -#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - -typedef struct luaL_Buffer { - char *b; /* buffer address */ - size_t size; /* buffer size */ - size_t n; /* number of characters in buffer */ - lua_State *L; - char initb[LUAL_BUFFERSIZE]; /* initial buffer */ -} luaL_Buffer; - - -#define luaL_addchar(B,c) \ - ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ - ((B)->b[(B)->n++] = (c))) - -#define luaL_addsize(B,s) ((B)->n += (s)) - -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); -LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); -LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); - -#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) - -/* }====================================================== */ - - - -/* -** {====================================================== -** File handles for IO library -** ======================================================= -*/ - -/* -** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and -** initial structure 'luaL_Stream' (it may contain other fields -** after that initial structure). -*/ - -#define LUA_FILEHANDLE "FILE*" - - -typedef struct luaL_Stream { - FILE *f; /* stream (NULL for incompletely created streams) */ - lua_CFunction closef; /* to close stream (NULL for closed streams) */ -} luaL_Stream; - -/* }====================================================== */ - - - -/* compatibility with old module system */ -#if defined(LUA_COMPAT_MODULE) - -LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, - int sizehint); -LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); - -#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) - -#endif - - -#endif - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbaselib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbaselib.c deleted file mode 100644 index 5255b3c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbaselib.c +++ /dev/null @@ -1,458 +0,0 @@ -/* -** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ -** Basic library -** See Copyright Notice in lua.h -*/ - - - -#include -#include -#include -#include - -#define lbaselib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, - LUA_QL("tostring") " must return a string to " LUA_QL("print")); - if (i>1) luai_writestring("\t", 1); - luai_writestring(s, l); - lua_pop(L, 1); /* pop result */ - } - luai_writeline(); - return 0; -} - - -#define SPACECHARS " \f\n\r\t\v" - -static int luaB_tonumber (lua_State *L) { - if (lua_isnoneornil(L, 2)) { /* standard conversion */ - int isnum; - lua_Number n = lua_tonumberx(L, 1, &isnum); - if (isnum) { - lua_pushnumber(L, n); - return 1; - } /* else not a number; must be something */ - luaL_checkany(L, 1); - } - else { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - const char *e = s + l; /* end point for 's' */ - int base = luaL_checkint(L, 2); - int neg = 0; - luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - s += strspn(s, SPACECHARS); /* skip initial spaces */ - if (*s == '-') { s++; neg = 1; } /* handle signal */ - else if (*s == '+') s++; - if (isalnum((unsigned char)*s)) { - lua_Number n = 0; - do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : toupper((unsigned char)*s) - 'A' + 10; - if (digit >= base) break; /* invalid numeral; force a fail */ - n = n * (lua_Number)base + (lua_Number)digit; - s++; - } while (isalnum((unsigned char)*s)); - s += strspn(s, SPACECHARS); /* skip trailing spaces */ - if (s == e) { /* no invalid trailing characters? */ - lua_pushnumber(L, (neg) ? -n : n); - return 1; - } /* else not a number */ - } /* else not a number */ - } - lua_pushnil(L); /* not a number */ - return 1; -} - - -static int luaB_error (lua_State *L) { - int level = luaL_optint(L, 2, 1); - lua_settop(L, 1); - if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ - luaL_where(L, level); - lua_pushvalue(L, 1); - lua_concat(L, 2); - } - return lua_error(L); -} - - -static int luaB_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); - return 1; /* no metatable */ - } - luaL_getmetafield(L, 1, "__metatable"); - return 1; /* returns either __metatable field (if present) or metatable */ -} - - -static int luaB_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - if (luaL_getmetafield(L, 1, "__metatable")) - return luaL_error(L, "cannot change a protected metatable"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; -} - - -static int luaB_rawequal (lua_State *L) { - luaL_checkany(L, 1); - luaL_checkany(L, 2); - lua_pushboolean(L, lua_rawequal(L, 1, 2)); - return 1; -} - - -static int luaB_rawlen (lua_State *L) { - int t = lua_type(L, 1); - luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, - "table or string expected"); - lua_pushinteger(L, lua_rawlen(L, 1)); - return 1; -} - - -static int luaB_rawget (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - lua_settop(L, 2); - lua_rawget(L, 1); - return 1; -} - -static int luaB_rawset (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - luaL_checkany(L, 3); - lua_settop(L, 3); - lua_rawset(L, 1); - return 1; -} - - -static int luaB_collectgarbage (lua_State *L) { - static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setpause", "setstepmul", - "setmajorinc", "isrunning", "generational", "incremental", NULL}; - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; - int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - int ex = luaL_optint(L, 2, 0); - int res = lua_gc(L, o, ex); - switch (o) { - case LUA_GCCOUNT: { - int b = lua_gc(L, LUA_GCCOUNTB, 0); - lua_pushnumber(L, res + ((lua_Number)b/1024)); - lua_pushinteger(L, b); - return 2; - } - case LUA_GCSTEP: case LUA_GCISRUNNING: { - lua_pushboolean(L, res); - return 1; - } - default: { - lua_pushinteger(L, res); - return 1; - } - } -} - - -static int luaB_type (lua_State *L) { - luaL_checkany(L, 1); - lua_pushstring(L, luaL_typename(L, 1)); - return 1; -} - - -static int pairsmeta (lua_State *L, const char *method, int iszero, - lua_CFunction iter) { - if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ - luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ - lua_pushcfunction(L, iter); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - if (iszero) lua_pushinteger(L, 0); /* and initial value */ - else lua_pushnil(L); - } - else { - lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ - } - return 3; -} - - -static int luaB_next (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 2); /* create a 2nd argument if there isn't one */ - if (lua_next(L, 1)) - return 2; - else { - lua_pushnil(L); - return 1; - } -} - - -static int luaB_pairs (lua_State *L) { - return pairsmeta(L, "__pairs", 0, luaB_next); -} - - -static int ipairsaux (lua_State *L) { - int i = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - i++; /* next value */ - lua_pushinteger(L, i); - lua_rawgeti(L, 1, i); - return (lua_isnil(L, -1)) ? 1 : 2; -} - - -static int luaB_ipairs (lua_State *L) { - return pairsmeta(L, "__ipairs", 1, ipairsaux); -} - - -static int load_aux (lua_State *L, int status, int envidx) { - if (status == LUA_OK) { - if (envidx != 0) { /* 'env' parameter? */ - lua_pushvalue(L, envidx); /* environment for loaded function */ - if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ - lua_pop(L, 1); /* remove 'env' if not used by previous call */ - } - return 1; - } - else { /* error (message is on top of the stack) */ - lua_pushnil(L); - lua_insert(L, -2); /* put before error message */ - return 2; /* return nil plus error message */ - } -} - - -static int luaB_loadfile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); - int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); - return load_aux(L, status, env); -} - - -/* -** {====================================================== -** Generic Read function -** ======================================================= -*/ - - -/* -** reserved slot, above all arguments, to hold a copy of the returned -** string to avoid it being collected while parsed. 'load' has four -** optional arguments (chunk, source name, mode, and environment). -*/ -#define RESERVEDSLOT 5 - - -/* -** Reader for generic `load' function: `lua_load' uses the -** stack for internal stuff, so the reader cannot change the -** stack top. Instead, it keeps its resulting string in a -** reserved slot inside the stack. -*/ -static const char *generic_reader (lua_State *L, void *ud, size_t *size) { - (void)(ud); /* not used */ - luaL_checkstack(L, 2, "too many nested functions"); - lua_pushvalue(L, 1); /* get function */ - lua_call(L, 0, 1); /* call it */ - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* pop result */ - *size = 0; - return NULL; - } - else if (!lua_isstring(L, -1)) - luaL_error(L, "reader function must return a string"); - lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ - return lua_tolstring(L, RESERVEDSLOT, size); -} - - -static int luaB_load (lua_State *L) { - int status; - size_t l; - const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); - int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ - if (s != NULL) { /* loading a string? */ - const char *chunkname = luaL_optstring(L, 2, s); - status = luaL_loadbufferx(L, s, l, chunkname, mode); - } - else { /* loading from a reader function */ - const char *chunkname = luaL_optstring(L, 2, "=(load)"); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, RESERVEDSLOT); /* create reserved slot */ - status = lua_load(L, generic_reader, NULL, chunkname, mode); - } - return load_aux(L, status, env); -} - -/* }====================================================== */ - - -static int dofilecont (lua_State *L) { - return lua_gettop(L) - 1; -} - - -static int luaB_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) - return lua_error(L); - lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L); -} - - -static int luaB_assert (lua_State *L) { - if (!lua_toboolean(L, 1)) - return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); - return lua_gettop(L); -} - - -static int luaB_select (lua_State *L) { - int n = lua_gettop(L); - if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { - lua_pushinteger(L, n-1); - return 1; - } - else { - int i = luaL_checkint(L, 1); - if (i < 0) i = n + i; - else if (i > n) i = n; - luaL_argcheck(L, 1 <= i, 1, "index out of range"); - return n - i; - } -} - - -static int finishpcall (lua_State *L, int status) { - if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ - lua_settop(L, 0); /* create space for return values */ - lua_pushboolean(L, 0); - lua_pushstring(L, "stack overflow"); - return 2; /* return false, msg */ - } - lua_pushboolean(L, status); /* first result (status) */ - lua_replace(L, 1); /* put first result in first slot */ - return lua_gettop(L); -} - - -static int pcallcont (lua_State *L) { - int status = lua_getctx(L, NULL); - return finishpcall(L, (status == LUA_YIELD)); -} - - -static int luaB_pcall (lua_State *L) { - int status; - luaL_checkany(L, 1); - lua_pushnil(L); - lua_insert(L, 1); /* create space for status result */ - status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); - return finishpcall(L, (status == LUA_OK)); -} - - -static int luaB_xpcall (lua_State *L) { - int status; - int n = lua_gettop(L); - luaL_argcheck(L, n >= 2, 2, "value expected"); - lua_pushvalue(L, 1); /* exchange function... */ - lua_copy(L, 2, 1); /* ...and error handler */ - lua_replace(L, 2); - status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont); - return finishpcall(L, (status == LUA_OK)); -} - - -static int luaB_tostring (lua_State *L) { - luaL_checkany(L, 1); - luaL_tolstring(L, 1, NULL); - return 1; -} - - -static const luaL_Reg base_funcs[] = { - {"assert", luaB_assert}, - {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, - {"error", luaB_error}, - {"getmetatable", luaB_getmetatable}, - {"ipairs", luaB_ipairs}, - {"loadfile", luaB_loadfile}, - {"load", luaB_load}, -#if defined(LUA_COMPAT_LOADSTRING) - {"loadstring", luaB_load}, -#endif - {"next", luaB_next}, - {"pairs", luaB_pairs}, - {"pcall", luaB_pcall}, - {"print", luaB_print}, - {"rawequal", luaB_rawequal}, - {"rawlen", luaB_rawlen}, - {"rawget", luaB_rawget}, - {"rawset", luaB_rawset}, - {"select", luaB_select}, - {"setmetatable", luaB_setmetatable}, - {"tonumber", luaB_tonumber}, - {"tostring", luaB_tostring}, - {"type", luaB_type}, - {"xpcall", luaB_xpcall}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_base (lua_State *L) { - /* set global _G */ - lua_pushglobaltable(L); - lua_pushglobaltable(L); - lua_setfield(L, -2, "_G"); - /* open lib into global table */ - luaL_setfuncs(L, base_funcs, 0); - lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbitlib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbitlib.c deleted file mode 100644 index 31c7b66..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lbitlib.c +++ /dev/null @@ -1,212 +0,0 @@ -/* -** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ -** Standard library for bitwise operations -** See Copyright Notice in lua.h -*/ - -#define lbitlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* number of bits to consider in a number */ -#if !defined(LUA_NBITS) -#define LUA_NBITS 32 -#endif - - -#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) - -/* macro to trim extra bits */ -#define trim(x) ((x) & ALLONES) - - -/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ -#define mask(n) (~((ALLONES << 1) << ((n) - 1))) - - -typedef lua_Unsigned b_uint; - - - -static b_uint andaux (lua_State *L) { - int i, n = lua_gettop(L); - b_uint r = ~(b_uint)0; - for (i = 1; i <= n; i++) - r &= luaL_checkunsigned(L, i); - return trim(r); -} - - -static int b_and (lua_State *L) { - b_uint r = andaux(L); - lua_pushunsigned(L, r); - return 1; -} - - -static int b_test (lua_State *L) { - b_uint r = andaux(L); - lua_pushboolean(L, r != 0); - return 1; -} - - -static int b_or (lua_State *L) { - int i, n = lua_gettop(L); - b_uint r = 0; - for (i = 1; i <= n; i++) - r |= luaL_checkunsigned(L, i); - lua_pushunsigned(L, trim(r)); - return 1; -} - - -static int b_xor (lua_State *L) { - int i, n = lua_gettop(L); - b_uint r = 0; - for (i = 1; i <= n; i++) - r ^= luaL_checkunsigned(L, i); - lua_pushunsigned(L, trim(r)); - return 1; -} - - -static int b_not (lua_State *L) { - b_uint r = ~luaL_checkunsigned(L, 1); - lua_pushunsigned(L, trim(r)); - return 1; -} - - -static int b_shift (lua_State *L, b_uint r, int i) { - if (i < 0) { /* shift right? */ - i = -i; - r = trim(r); - if (i >= LUA_NBITS) r = 0; - else r >>= i; - } - else { /* shift left */ - if (i >= LUA_NBITS) r = 0; - else r <<= i; - r = trim(r); - } - lua_pushunsigned(L, r); - return 1; -} - - -static int b_lshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); -} - - -static int b_rshift (lua_State *L) { - return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); -} - - -static int b_arshift (lua_State *L) { - b_uint r = luaL_checkunsigned(L, 1); - int i = luaL_checkint(L, 2); - if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) - return b_shift(L, r, -i); - else { /* arithmetic shift for 'negative' number */ - if (i >= LUA_NBITS) r = ALLONES; - else - r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ - lua_pushunsigned(L, r); - return 1; - } -} - - -static int b_rot (lua_State *L, int i) { - b_uint r = luaL_checkunsigned(L, 1); - i &= (LUA_NBITS - 1); /* i = i % NBITS */ - r = trim(r); - if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ - r = (r << i) | (r >> (LUA_NBITS - i)); - lua_pushunsigned(L, trim(r)); - return 1; -} - - -static int b_lrot (lua_State *L) { - return b_rot(L, luaL_checkint(L, 2)); -} - - -static int b_rrot (lua_State *L) { - return b_rot(L, -luaL_checkint(L, 2)); -} - - -/* -** get field and width arguments for field-manipulation functions, -** checking whether they are valid. -** ('luaL_error' called without 'return' to avoid later warnings about -** 'width' being used uninitialized.) -*/ -static int fieldargs (lua_State *L, int farg, int *width) { - int f = luaL_checkint(L, farg); - int w = luaL_optint(L, farg + 1, 1); - luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); - luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); - if (f + w > LUA_NBITS) - luaL_error(L, "trying to access non-existent bits"); - *width = w; - return f; -} - - -static int b_extract (lua_State *L) { - int w; - b_uint r = luaL_checkunsigned(L, 1); - int f = fieldargs(L, 2, &w); - r = (r >> f) & mask(w); - lua_pushunsigned(L, r); - return 1; -} - - -static int b_replace (lua_State *L) { - int w; - b_uint r = luaL_checkunsigned(L, 1); - b_uint v = luaL_checkunsigned(L, 2); - int f = fieldargs(L, 3, &w); - int m = mask(w); - v &= m; /* erase bits outside given width */ - r = (r & ~(m << f)) | (v << f); - lua_pushunsigned(L, r); - return 1; -} - - -static const luaL_Reg bitlib[] = { - {"arshift", b_arshift}, - {"band", b_and}, - {"bnot", b_not}, - {"bor", b_or}, - {"bxor", b_xor}, - {"btest", b_test}, - {"extract", b_extract}, - {"lrotate", b_lrot}, - {"lshift", b_lshift}, - {"replace", b_replace}, - {"rrotate", b_rrot}, - {"rshift", b_rshift}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_bit32 (lua_State *L) { - luaL_newlib(L, bitlib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.c deleted file mode 100644 index 820b95c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.c +++ /dev/null @@ -1,881 +0,0 @@ -/* -** $Id: lcode.c,v 2.62.1.1 2013/04/12 18:48:47 roberto Exp $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - - -#include - -#define lcode_c -#define LUA_CORE - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstring.h" -#include "ltable.h" -#include "lvm.h" - - -#define hasjumps(e) ((e)->t != (e)->f) - - -static int isnumeral(expdesc *e) { - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); -} - - -void luaK_nil (FuncState *fs, int from, int n) { - Instruction *previous; - int l = from + n - 1; /* last register to set nil */ - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->code[fs->pc-1]; - if (GET_OPCODE(*previous) == OP_LOADNIL) { - int pfrom = GETARG_A(*previous); - int pl = pfrom + GETARG_B(*previous); - if ((pfrom <= from && from <= pl + 1) || - (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ - if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ - if (pl > l) l = pl; /* l = max(l, pl) */ - SETARG_A(*previous, from); - SETARG_B(*previous, l - from); - return; - } - } /* else go through */ - } - luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ -} - - -int luaK_jump (FuncState *fs) { - int jpc = fs->jpc; /* save list of jumps to here */ - int j; - fs->jpc = NO_JUMP; - j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); - luaK_concat(fs, &j, jpc); /* keep them on hold */ - return j; -} - - -void luaK_ret (FuncState *fs, int first, int nret) { - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); -} - - -static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { - luaK_codeABC(fs, op, A, B, C); - return luaK_jump(fs); -} - - -static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->code[pc]; - int offset = dest-(pc+1); - lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) - luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); -} - - -/* -** returns current `pc' and marks it as a jump target (to avoid wrong -** optimizations with consecutive instructions not in the same basic block). -*/ -int luaK_getlabel (FuncState *fs) { - fs->lasttarget = fs->pc; - return fs->pc; -} - - -static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); - if (offset == NO_JUMP) /* point to itself represents end of list */ - return NO_JUMP; /* end of list */ - else - return (pc+1)+offset; /* turn offset into absolute position */ -} - - -static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->code[pc]; - if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) - return pi-1; - else - return pi; -} - - -/* -** check whether list has any jump that do not produce a value -** (or produce an inverted value) -*/ -static int need_value (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TESTSET) return 1; - } - return 0; /* not found */ -} - - -static int patchtestreg (FuncState *fs, int node, int reg) { - Instruction *i = getjumpcontrol(fs, node); - if (GET_OPCODE(*i) != OP_TESTSET) - return 0; /* cannot patch other instructions */ - if (reg != NO_REG && reg != GETARG_B(*i)) - SETARG_A(*i, reg); - else /* no register to put value or register already has the value */ - *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); - - return 1; -} - - -static void removevalues (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) - patchtestreg(fs, list, NO_REG); -} - - -static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, - int dtarget) { - while (list != NO_JUMP) { - int next = getjump(fs, list); - if (patchtestreg(fs, list, reg)) - fixjump(fs, list, vtarget); - else - fixjump(fs, list, dtarget); /* jump to default target */ - list = next; - } -} - - -static void dischargejpc (FuncState *fs) { - patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); - fs->jpc = NO_JUMP; -} - - -void luaK_patchlist (FuncState *fs, int list, int target) { - if (target == fs->pc) - luaK_patchtohere(fs, list); - else { - lua_assert(target < fs->pc); - patchlistaux(fs, list, target, NO_REG, target); - } -} - - -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) { - level++; /* argument is +1 to reserve 0 as non-op */ - while (list != NO_JUMP) { - int next = getjump(fs, list); - lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && - (GETARG_A(fs->f->code[list]) == 0 || - GETARG_A(fs->f->code[list]) >= level)); - SETARG_A(fs->f->code[list], level); - list = next; - } -} - - -void luaK_patchtohere (FuncState *fs, int list) { - luaK_getlabel(fs); - luaK_concat(fs, &fs->jpc, list); -} - - -void luaK_concat (FuncState *fs, int *l1, int l2) { - if (l2 == NO_JUMP) return; - else if (*l1 == NO_JUMP) - *l1 = l2; - else { - int list = *l1; - int next; - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ - list = next; - fixjump(fs, list, l2); - } -} - - -static int luaK_code (FuncState *fs, Instruction i) { - Proto *f = fs->f; - dischargejpc(fs); /* `pc' will change */ - /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, - MAX_INT, "opcodes"); - f->code[fs->pc] = i; - /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, - MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; - return fs->pc++; -} - - -int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { - lua_assert(getOpMode(o) == iABC); - lua_assert(getBMode(o) != OpArgN || b == 0); - lua_assert(getCMode(o) != OpArgN || c == 0); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); - return luaK_code(fs, CREATE_ABC(o, a, b, c)); -} - - -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { - lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); - lua_assert(getCMode(o) == OpArgN); - lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, bc)); -} - - -static int codeextraarg (FuncState *fs, int a) { - lua_assert(a <= MAXARG_Ax); - return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); -} - - -int luaK_codek (FuncState *fs, int reg, int k) { - if (k <= MAXARG_Bx) - return luaK_codeABx(fs, OP_LOADK, reg, k); - else { - int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); - codeextraarg(fs, k); - return p; - } -} - - -void luaK_checkstack (FuncState *fs, int n) { - int newstack = fs->freereg + n; - if (newstack > fs->f->maxstacksize) { - if (newstack >= MAXSTACK) - luaX_syntaxerror(fs->ls, "function or expression too complex"); - fs->f->maxstacksize = cast_byte(newstack); - } -} - - -void luaK_reserveregs (FuncState *fs, int n) { - luaK_checkstack(fs, n); - fs->freereg += n; -} - - -static void freereg (FuncState *fs, int reg) { - if (!ISK(reg) && reg >= fs->nactvar) { - fs->freereg--; - lua_assert(reg == fs->freereg); - } -} - - -static void freeexp (FuncState *fs, expdesc *e) { - if (e->k == VNONRELOC) - freereg(fs, e->u.info); -} - - -static int addk (FuncState *fs, TValue *key, TValue *v) { - lua_State *L = fs->ls->L; - TValue *idx = luaH_set(L, fs->h, key); - Proto *f = fs->f; - int k, oldsize; - if (ttisnumber(idx)) { - lua_Number n = nvalue(idx); - lua_number2int(k, n); - if (luaV_rawequalobj(&f->k[k], v)) - return k; - /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0"); - go through and create a new entry for this value */ - } - /* constant not found; create a new entry */ - oldsize = f->sizek; - k = fs->nk; - /* numerical value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ - setnvalue(idx, cast_num(k)); - luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); - setobj(L, &f->k[k], v); - fs->nk++; - luaC_barrier(L, f, v); - return k; -} - - -int luaK_stringK (FuncState *fs, TString *s) { - TValue o; - setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); -} - - -int luaK_numberK (FuncState *fs, lua_Number r) { - int n; - lua_State *L = fs->ls->L; - TValue o; - setnvalue(&o, r); - if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ - /* use raw representation as key to avoid numeric problems */ - setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r))); - n = addk(fs, L->top - 1, &o); - L->top--; - } - else - n = addk(fs, &o, &o); /* regular case */ - return n; -} - - -static int boolK (FuncState *fs, int b) { - TValue o; - setbvalue(&o, b); - return addk(fs, &o, &o); -} - - -static int nilK (FuncState *fs) { - TValue k, v; - setnilvalue(&v); - /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->h); - return addk(fs, &k, &v); -} - - -void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { - if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getcode(fs, e), nresults+1); - } - else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), nresults+1); - SETARG_A(getcode(fs, e), fs->freereg); - luaK_reserveregs(fs, 1); - } -} - - -void luaK_setoneret (FuncState *fs, expdesc *e) { - if (e->k == VCALL) { /* expression is an open function call? */ - e->k = VNONRELOC; - e->u.info = GETARG_A(getcode(fs, e)); - } - else if (e->k == VVARARG) { - SETARG_B(getcode(fs, e), 2); - e->k = VRELOCABLE; /* can relocate its simple result */ - } -} - - -void luaK_dischargevars (FuncState *fs, expdesc *e) { - switch (e->k) { - case VLOCAL: { - e->k = VNONRELOC; - break; - } - case VUPVAL: { - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); - e->k = VRELOCABLE; - break; - } - case VINDEXED: { - OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */ - freereg(fs, e->u.ind.idx); - if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */ - freereg(fs, e->u.ind.t); - op = OP_GETTABLE; - } - e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOCABLE; - break; - } - case VVARARG: - case VCALL: { - luaK_setoneret(fs, e); - break; - } - default: break; /* there is one value available (somewhere) */ - } -} - - -static int code_label (FuncState *fs, int A, int b, int jump) { - luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); -} - - -static void discharge2reg (FuncState *fs, expdesc *e, int reg) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: { - luaK_nil(fs, reg, 1); - break; - } - case VFALSE: case VTRUE: { - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); - break; - } - case VK: { - luaK_codek(fs, reg, e->u.info); - break; - } - case VKNUM: { - luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); - break; - } - case VRELOCABLE: { - Instruction *pc = &getcode(fs, e); - SETARG_A(*pc, reg); - break; - } - case VNONRELOC: { - if (reg != e->u.info) - luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); - break; - } - default: { - lua_assert(e->k == VVOID || e->k == VJMP); - return; /* nothing to do... */ - } - } - e->u.info = reg; - e->k = VNONRELOC; -} - - -static void discharge2anyreg (FuncState *fs, expdesc *e) { - if (e->k != VNONRELOC) { - luaK_reserveregs(fs, 1); - discharge2reg(fs, e, fs->freereg-1); - } -} - - -static void exp2reg (FuncState *fs, expdesc *e, int reg) { - discharge2reg(fs, e, reg); - if (e->k == VJMP) - luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */ - if (hasjumps(e)) { - int final; /* position after whole expression */ - int p_f = NO_JUMP; /* position of an eventual LOAD false */ - int p_t = NO_JUMP; /* position of an eventual LOAD true */ - if (need_value(fs, e->t) || need_value(fs, e->f)) { - int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_label(fs, reg, 0, 1); - p_t = code_label(fs, reg, 1, 0); - luaK_patchtohere(fs, fj); - } - final = luaK_getlabel(fs); - patchlistaux(fs, e->f, final, reg, p_f); - patchlistaux(fs, e->t, final, reg, p_t); - } - e->f = e->t = NO_JUMP; - e->u.info = reg; - e->k = VNONRELOC; -} - - -void luaK_exp2nextreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - freeexp(fs, e); - luaK_reserveregs(fs, 1); - exp2reg(fs, e, fs->freereg - 1); -} - - -int luaK_exp2anyreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - if (e->k == VNONRELOC) { - if (!hasjumps(e)) return e->u.info; /* exp is already in a register */ - if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ - exp2reg(fs, e, e->u.info); /* put value on it */ - return e->u.info; - } - } - luaK_exp2nextreg(fs, e); /* default */ - return e->u.info; -} - - -void luaK_exp2anyregup (FuncState *fs, expdesc *e) { - if (e->k != VUPVAL || hasjumps(e)) - luaK_exp2anyreg(fs, e); -} - - -void luaK_exp2val (FuncState *fs, expdesc *e) { - if (hasjumps(e)) - luaK_exp2anyreg(fs, e); - else - luaK_dischargevars(fs, e); -} - - -int luaK_exp2RK (FuncState *fs, expdesc *e) { - luaK_exp2val(fs, e); - switch (e->k) { - case VTRUE: - case VFALSE: - case VNIL: { - if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */ - e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE)); - e->k = VK; - return RKASK(e->u.info); - } - else break; - } - case VKNUM: { - e->u.info = luaK_numberK(fs, e->u.nval); - e->k = VK; - /* go through */ - } - case VK: { - if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */ - return RKASK(e->u.info); - else break; - } - default: break; - } - /* not a constant in the right range: put it in a register */ - return luaK_exp2anyreg(fs, e); -} - - -void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { - switch (var->k) { - case VLOCAL: { - freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); - return; - } - case VUPVAL: { - int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); - break; - } - case VINDEXED: { - OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP; - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); - break; - } - default: { - lua_assert(0); /* invalid var kind to store */ - break; - } - } - freeexp(fs, ex); -} - - -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { - int ereg; - luaK_exp2anyreg(fs, e); - ereg = e->u.info; /* register where 'e' was placed */ - freeexp(fs, e); - e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; - luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ - luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); - freeexp(fs, key); -} - - -static void invertjump (FuncState *fs, expdesc *e) { - Instruction *pc = getjumpcontrol(fs, e->u.info); - lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && - GET_OPCODE(*pc) != OP_TEST); - SETARG_A(*pc, !(GETARG_A(*pc))); -} - - -static int jumponcond (FuncState *fs, expdesc *e, int cond) { - if (e->k == VRELOCABLE) { - Instruction ie = getcode(fs, e); - if (GET_OPCODE(ie) == OP_NOT) { - fs->pc--; /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); - } - /* else go through */ - } - discharge2anyreg(fs, e); - freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); -} - - -void luaK_goiftrue (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VJMP: { - invertjump(fs, e); - pc = e->u.info; - break; - } - case VK: case VKNUM: case VTRUE: { - pc = NO_JUMP; /* always true; do nothing */ - break; - } - default: { - pc = jumponcond(fs, e, 0); - break; - } - } - luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ - luaK_patchtohere(fs, e->t); - e->t = NO_JUMP; -} - - -void luaK_goiffalse (FuncState *fs, expdesc *e) { - int pc; /* pc of last jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VJMP: { - pc = e->u.info; - break; - } - case VNIL: case VFALSE: { - pc = NO_JUMP; /* always false; do nothing */ - break; - } - default: { - pc = jumponcond(fs, e, 1); - break; - } - } - luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ - luaK_patchtohere(fs, e->f); - e->f = NO_JUMP; -} - - -static void codenot (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: case VFALSE: { - e->k = VTRUE; - break; - } - case VK: case VKNUM: case VTRUE: { - e->k = VFALSE; - break; - } - case VJMP: { - invertjump(fs, e); - break; - } - case VRELOCABLE: - case VNONRELOC: { - discharge2anyreg(fs, e); - freeexp(fs, e); - e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); - e->k = VRELOCABLE; - break; - } - default: { - lua_assert(0); /* cannot happen */ - break; - } - } - /* interchange true and false lists */ - { int temp = e->f; e->f = e->t; e->t = temp; } - removevalues(fs, e->f); - removevalues(fs, e->t); -} - - -void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t)); - t->u.ind.t = t->u.info; - t->u.ind.idx = luaK_exp2RK(fs, k); - t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL - : check_exp(vkisinreg(t->k), VLOCAL); - t->k = VINDEXED; -} - - -static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { - lua_Number r; - if (!isnumeral(e1) || !isnumeral(e2)) return 0; - if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0) - return 0; /* do not attempt to divide by 0 */ - r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval); - e1->u.nval = r; - return 1; -} - - -static void codearith (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - if (constfolding(op, e1, e2)) - return; - else { - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; - int o1 = luaK_exp2RK(fs, e1); - if (o1 > o2) { - freeexp(fs, e1); - freeexp(fs, e2); - } - else { - freeexp(fs, e2); - freeexp(fs, e1); - } - e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); - e1->k = VRELOCABLE; - luaK_fixline(fs, line); - } -} - - -static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, - expdesc *e2) { - int o1 = luaK_exp2RK(fs, e1); - int o2 = luaK_exp2RK(fs, e2); - freeexp(fs, e2); - freeexp(fs, e1); - if (cond == 0 && op != OP_EQ) { - int temp; /* exchange args to replace by `<' or `<=' */ - temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ - cond = 1; - } - e1->u.info = condjump(fs, op, cond, o1, o2); - e1->k = VJMP; -} - - -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { - expdesc e2; - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; - switch (op) { - case OPR_MINUS: { - if (isnumeral(e)) /* minus constant? */ - e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */ - else { - luaK_exp2anyreg(fs, e); - codearith(fs, OP_UNM, e, &e2, line); - } - break; - } - case OPR_NOT: codenot(fs, e); break; - case OPR_LEN: { - luaK_exp2anyreg(fs, e); /* cannot operate on constants */ - codearith(fs, OP_LEN, e, &e2, line); - break; - } - default: lua_assert(0); - } -} - - -void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { - switch (op) { - case OPR_AND: { - luaK_goiftrue(fs, v); - break; - } - case OPR_OR: { - luaK_goiffalse(fs, v); - break; - } - case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ - break; - } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - if (!isnumeral(v)) luaK_exp2RK(fs, v); - break; - } - default: { - luaK_exp2RK(fs, v); - break; - } - } -} - - -void luaK_posfix (FuncState *fs, BinOpr op, - expdesc *e1, expdesc *e2, int line) { - switch (op) { - case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); - luaK_concat(fs, &e2->f, e1->f); - *e1 = *e2; - break; - } - case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); - luaK_concat(fs, &e2->t, e1->t); - *e1 = *e2; - break; - } - case OPR_CONCAT: { - luaK_exp2val(fs, e2); - if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { - lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1); - freeexp(fs, e1); - SETARG_B(getcode(fs, e2), e1->u.info); - e1->k = VRELOCABLE; e1->u.info = e2->u.info; - } - else { - luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codearith(fs, OP_CONCAT, e1, e2, line); - } - break; - } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_MOD: case OPR_POW: { - codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line); - break; - } - case OPR_EQ: case OPR_LT: case OPR_LE: { - codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2); - break; - } - case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2); - break; - } - default: lua_assert(0); - } -} - - -void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; -} - - -void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; - int b = (tostore == LUA_MULTRET) ? 0 : tostore; - lua_assert(tostore != 0); - if (c <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, b, c); - else if (c <= MAXARG_Ax) { - luaK_codeABC(fs, OP_SETLIST, base, b, 0); - codeextraarg(fs, c); - } - else - luaX_syntaxerror(fs->ls, "constructor too long"); - fs->freereg = base + 1; /* free registers with list values */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.h deleted file mode 100644 index 6a1424c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcode.h +++ /dev/null @@ -1,83 +0,0 @@ -/* -** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lcode_h -#define lcode_h - -#include "llex.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" - - -/* -** Marks the end of a patch list. It is an invalid value both as an absolute -** address, and as a list link (would link an element to itself). -*/ -#define NO_JUMP (-1) - - -/* -** grep "ORDER OPR" if you change these enums (ORDER OP) -*/ -typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, - OPR_CONCAT, - OPR_EQ, OPR_LT, OPR_LE, - OPR_NE, OPR_GT, OPR_GE, - OPR_AND, OPR_OR, - OPR_NOBINOPR -} BinOpr; - - -typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; - - -#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) - -#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) - -#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) - -#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) - -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); -LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); -LUAI_FUNC void luaK_fixline (FuncState *fs, int line); -LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); -LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); -LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); -LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); -LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); -LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); -LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); -LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); -LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_jump (FuncState *fs); -LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); -LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); -LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); -LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); -LUAI_FUNC int luaK_getlabel (FuncState *fs); -LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); -LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); -LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, - expdesc *v2, int line); -LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcorolib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcorolib.c deleted file mode 100644 index ce4f6ad..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lcorolib.c +++ /dev/null @@ -1,155 +0,0 @@ -/* -** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ -** Coroutine Library -** See Copyright Notice in lua.h -*/ - - -#include - - -#define lcorolib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static int auxresume (lua_State *L, lua_State *co, int narg) { - int status; - if (!lua_checkstack(co, narg)) { - lua_pushliteral(L, "too many arguments to resume"); - return -1; /* error flag */ - } - if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { - lua_pushliteral(L, "cannot resume dead coroutine"); - return -1; /* error flag */ - } - lua_xmove(L, co, narg); - status = lua_resume(co, L, narg); - if (status == LUA_OK || status == LUA_YIELD) { - int nres = lua_gettop(co); - if (!lua_checkstack(L, nres + 1)) { - lua_pop(co, nres); /* remove results anyway */ - lua_pushliteral(L, "too many results to resume"); - return -1; /* error flag */ - } - lua_xmove(co, L, nres); /* move yielded values */ - return nres; - } - else { - lua_xmove(co, L, 1); /* move error message */ - return -1; /* error flag */ - } -} - - -static int luaB_coresume (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - int r; - luaL_argcheck(L, co, 1, "coroutine expected"); - r = auxresume(L, co, lua_gettop(L) - 1); - if (r < 0) { - lua_pushboolean(L, 0); - lua_insert(L, -2); - return 2; /* return false + error message */ - } - else { - lua_pushboolean(L, 1); - lua_insert(L, -(r + 1)); - return r + 1; /* return true + `resume' returns */ - } -} - - -static int luaB_auxwrap (lua_State *L) { - lua_State *co = lua_tothread(L, lua_upvalueindex(1)); - int r = auxresume(L, co, lua_gettop(L)); - if (r < 0) { - if (lua_isstring(L, -1)) { /* error object is a string? */ - luaL_where(L, 1); /* add extra info */ - lua_insert(L, -2); - lua_concat(L, 2); - } - return lua_error(L); /* propagate error */ - } - return r; -} - - -static int luaB_cocreate (lua_State *L) { - lua_State *NL; - luaL_checktype(L, 1, LUA_TFUNCTION); - NL = lua_newthread(L); - lua_pushvalue(L, 1); /* move function to top */ - lua_xmove(L, NL, 1); /* move function from L to NL */ - return 1; -} - - -static int luaB_cowrap (lua_State *L) { - luaB_cocreate(L); - lua_pushcclosure(L, luaB_auxwrap, 1); - return 1; -} - - -static int luaB_yield (lua_State *L) { - return lua_yield(L, lua_gettop(L)); -} - - -static int luaB_costatus (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "coroutine expected"); - if (L == co) lua_pushliteral(L, "running"); - else { - switch (lua_status(co)) { - case LUA_YIELD: - lua_pushliteral(L, "suspended"); - break; - case LUA_OK: { - lua_Debug ar; - if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ - lua_pushliteral(L, "normal"); /* it is running */ - else if (lua_gettop(co) == 0) - lua_pushliteral(L, "dead"); - else - lua_pushliteral(L, "suspended"); /* initial state */ - break; - } - default: /* some error occurred */ - lua_pushliteral(L, "dead"); - break; - } - } - return 1; -} - - -static int luaB_corunning (lua_State *L) { - int ismain = lua_pushthread(L); - lua_pushboolean(L, ismain); - return 2; -} - - -static const luaL_Reg co_funcs[] = { - {"create", luaB_cocreate}, - {"resume", luaB_coresume}, - {"running", luaB_corunning}, - {"status", luaB_costatus}, - {"wrap", luaB_cowrap}, - {"yield", luaB_yield}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_coroutine (lua_State *L) { - luaL_newlib(L, co_funcs); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.c deleted file mode 100644 index 93f8cad..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.c +++ /dev/null @@ -1,52 +0,0 @@ -/* -** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ -** 'ctype' functions for Lua -** See Copyright Notice in lua.h -*/ - -#define lctype_c -#define LUA_CORE - -#include "lctype.h" - -#if !LUA_USE_CTYPE /* { */ - -#include - -LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { - 0x00, /* EOZ */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ - 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ - 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ - 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, - 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ - 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -}; - -#endif /* } */ diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.h deleted file mode 100644 index b09b21a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lctype.h +++ /dev/null @@ -1,95 +0,0 @@ -/* -** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ -** 'ctype' functions for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lctype_h -#define lctype_h - -#include "lua.h" - - -/* -** WARNING: the functions defined here do not necessarily correspond -** to the similar functions in the standard C ctype.h. They are -** optimized for the specific needs of Lua -*/ - -#if !defined(LUA_USE_CTYPE) - -#if 'A' == 65 && '0' == 48 -/* ASCII case: can use its own tables; faster and fixed */ -#define LUA_USE_CTYPE 0 -#else -/* must use standard C ctype */ -#define LUA_USE_CTYPE 1 -#endif - -#endif - - -#if !LUA_USE_CTYPE /* { */ - -#include - -#include "llimits.h" - - -#define ALPHABIT 0 -#define DIGITBIT 1 -#define PRINTBIT 2 -#define SPACEBIT 3 -#define XDIGITBIT 4 - - -#define MASK(B) (1 << (B)) - - -/* -** add 1 to char to allow index -1 (EOZ) -*/ -#define testprop(c,p) (luai_ctype_[(c)+1] & (p)) - -/* -** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' -*/ -#define lislalpha(c) testprop(c, MASK(ALPHABIT)) -#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) -#define lisdigit(c) testprop(c, MASK(DIGITBIT)) -#define lisspace(c) testprop(c, MASK(SPACEBIT)) -#define lisprint(c) testprop(c, MASK(PRINTBIT)) -#define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) - -/* -** this 'ltolower' only works for alphabetic characters -*/ -#define ltolower(c) ((c) | ('A' ^ 'a')) - - -/* two more entries for 0 and -1 (EOZ) */ -LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; - - -#else /* }{ */ - -/* -** use standard C ctypes -*/ - -#include - - -#define lislalpha(c) (isalpha(c) || (c) == '_') -#define lislalnum(c) (isalnum(c) || (c) == '_') -#define lisdigit(c) (isdigit(c)) -#define lisspace(c) (isspace(c)) -#define lisprint(c) (isprint(c)) -#define lisxdigit(c) (isxdigit(c)) - -#define ltolower(c) (tolower(c)) - -#endif /* } */ - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldblib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldblib.c deleted file mode 100644 index bd8df9d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldblib.c +++ /dev/null @@ -1,408 +0,0 @@ -/* -** $Id: ldblib.c,v 1.132.1.2 2015/02/19 17:16:55 roberto Exp $ -** Interface from Lua to its debug API -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define ldblib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#define HOOKKEY "_HKEY" - - -static void checkstack (lua_State *L, lua_State *L1, int n) { - if (L != L1 && !lua_checkstack(L1, n)) - luaL_error(L, "stack overflow"); -} - - -static int db_getregistry (lua_State *L) { - lua_pushvalue(L, LUA_REGISTRYINDEX); - return 1; -} - - -static int db_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); /* no metatable */ - } - return 1; -} - - -static int db_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; /* return 1st argument */ -} - - -static int db_getuservalue (lua_State *L) { - if (lua_type(L, 1) != LUA_TUSERDATA) - lua_pushnil(L); - else - lua_getuservalue(L, 1); - return 1; -} - - -static int db_setuservalue (lua_State *L) { - if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) - luaL_argerror(L, 1, "full userdata expected, got light userdata"); - luaL_checktype(L, 1, LUA_TUSERDATA); - if (!lua_isnoneornil(L, 2)) - luaL_checktype(L, 2, LUA_TTABLE); - lua_settop(L, 2); - lua_setuservalue(L, 1); - return 1; -} - - -static void settabss (lua_State *L, const char *i, const char *v) { - lua_pushstring(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsi (lua_State *L, const char *i, int v) { - lua_pushinteger(L, v); - lua_setfield(L, -2, i); -} - - -static void settabsb (lua_State *L, const char *i, int v) { - lua_pushboolean(L, v); - lua_setfield(L, -2, i); -} - - -static lua_State *getthread (lua_State *L, int *arg) { - if (lua_isthread(L, 1)) { - *arg = 1; - return lua_tothread(L, 1); - } - else { - *arg = 0; - return L; - } -} - - -static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { - if (L == L1) { - lua_pushvalue(L, -2); - lua_remove(L, -3); - } - else - lua_xmove(L1, L, 1); - lua_setfield(L, -2, fname); -} - - -static int db_getinfo (lua_State *L) { - lua_Debug ar; - int arg; - lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnStu"); - checkstack(L, L1, 3); - if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { - lua_pushnil(L); /* level out of range */ - return 1; - } - } - else if (lua_isfunction(L, arg+1)) { - lua_pushfstring(L, ">%s", options); - options = lua_tostring(L, -1); - lua_pushvalue(L, arg+1); - lua_xmove(L, L1, 1); - } - else - return luaL_argerror(L, arg+1, "function or level expected"); - if (!lua_getinfo(L1, options, &ar)) - return luaL_argerror(L, arg+2, "invalid option"); - lua_createtable(L, 0, 2); - if (strchr(options, 'S')) { - settabss(L, "source", ar.source); - settabss(L, "short_src", ar.short_src); - settabsi(L, "linedefined", ar.linedefined); - settabsi(L, "lastlinedefined", ar.lastlinedefined); - settabss(L, "what", ar.what); - } - if (strchr(options, 'l')) - settabsi(L, "currentline", ar.currentline); - if (strchr(options, 'u')) { - settabsi(L, "nups", ar.nups); - settabsi(L, "nparams", ar.nparams); - settabsb(L, "isvararg", ar.isvararg); - } - if (strchr(options, 'n')) { - settabss(L, "name", ar.name); - settabss(L, "namewhat", ar.namewhat); - } - if (strchr(options, 't')) - settabsb(L, "istailcall", ar.istailcall); - if (strchr(options, 'L')) - treatstackoption(L, L1, "activelines"); - if (strchr(options, 'f')) - treatstackoption(L, L1, "func"); - return 1; /* return table */ -} - - -static int db_getlocal (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; - int nvar = luaL_checkint(L, arg+2); /* local-variable index */ - if (lua_isfunction(L, arg + 1)) { /* function argument? */ - lua_pushvalue(L, arg + 1); /* push function */ - lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ - return 1; - } - else { /* stack-level argument */ - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - checkstack(L, L1, 1); - name = lua_getlocal(L1, &ar, nvar); - if (name) { - lua_xmove(L1, L, 1); /* push local value */ - lua_pushstring(L, name); /* push name */ - lua_pushvalue(L, -2); /* re-order */ - return 2; - } - else { - lua_pushnil(L); /* no name (nor value) */ - return 1; - } - } -} - - -static int db_setlocal (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - luaL_checkany(L, arg+3); - lua_settop(L, arg+3); - checkstack(L, L1, 1); - lua_xmove(L, L1, 1); - lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); - return 1; -} - - -static int auxupvalue (lua_State *L, int get) { - const char *name; - int n = luaL_checkint(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); - if (name == NULL) return 0; - lua_pushstring(L, name); - lua_insert(L, -(get+1)); - return get + 1; -} - - -static int db_getupvalue (lua_State *L) { - return auxupvalue(L, 1); -} - - -static int db_setupvalue (lua_State *L) { - luaL_checkany(L, 3); - return auxupvalue(L, 0); -} - - -static int checkupval (lua_State *L, int argf, int argnup) { - lua_Debug ar; - int nup = luaL_checkint(L, argnup); - luaL_checktype(L, argf, LUA_TFUNCTION); - lua_pushvalue(L, argf); - lua_getinfo(L, ">u", &ar); - luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index"); - return nup; -} - - -static int db_upvalueid (lua_State *L) { - int n = checkupval(L, 1, 2); - lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); - return 1; -} - - -static int db_upvaluejoin (lua_State *L) { - int n1 = checkupval(L, 1, 2); - int n2 = checkupval(L, 3, 4); - luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); - luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); - lua_upvaluejoin(L, 1, n1, 3, n2); - return 0; -} - - -#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) - - -static void hookf (lua_State *L, lua_Debug *ar) { - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail call"}; - gethooktable(L); - lua_pushthread(L); - lua_rawget(L, -2); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); - if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); - else lua_pushnil(L); - lua_assert(lua_getinfo(L, "lS", ar)); - lua_call(L, 2, 0); - } -} - - -static int makemask (const char *smask, int count) { - int mask = 0; - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; - if (strchr(smask, 'r')) mask |= LUA_MASKRET; - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; - if (count > 0) mask |= LUA_MASKCOUNT; - return mask; -} - - -static char *unmakemask (int mask, char *smask) { - int i = 0; - if (mask & LUA_MASKCALL) smask[i++] = 'c'; - if (mask & LUA_MASKRET) smask[i++] = 'r'; - if (mask & LUA_MASKLINE) smask[i++] = 'l'; - smask[i] = '\0'; - return smask; -} - - -static int db_sethook (lua_State *L) { - int arg, mask, count; - lua_Hook func; - lua_State *L1 = getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { - lua_settop(L, arg+1); - func = NULL; mask = 0; count = 0; /* turn off hooks */ - } - else { - const char *smask = luaL_checkstring(L, arg+2); - luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } - if (gethooktable(L) == 0) { /* creating hook table? */ - lua_pushstring(L, "k"); - lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ - lua_pushvalue(L, -1); - lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ - } - checkstack(L, L1, 1); - lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_pushvalue(L, arg+1); - lua_rawset(L, -3); /* set new hook */ - lua_sethook(L1, func, mask, count); /* set hooks */ - return 0; -} - - -static int db_gethook (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - char buff[5]; - int mask = lua_gethookmask(L1); - lua_Hook hook = lua_gethook(L1); - if (hook != NULL && hook != hookf) /* external hook? */ - lua_pushliteral(L, "external hook"); - else { - gethooktable(L); - checkstack(L, L1, 1); - lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_rawget(L, -2); /* get hook */ - lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L1)); - return 3; -} - - -static int db_debug (lua_State *L) { - for (;;) { - char buffer[250]; - luai_writestringerror("%s", "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || - strcmp(buffer, "cont\n") == 0) - return 0; - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || - lua_pcall(L, 0, 0, 0)) - luai_writestringerror("%s\n", lua_tostring(L, -1)); - lua_settop(L, 0); /* remove eventual returns */ - } -} - - -static int db_traceback (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - const char *msg = lua_tostring(L, arg + 1); - if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ - lua_pushvalue(L, arg + 1); /* return it untouched */ - else { - int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); - luaL_traceback(L, L1, msg, level); - } - return 1; -} - - -static const luaL_Reg dblib[] = { - {"debug", db_debug}, - {"getuservalue", db_getuservalue}, - {"gethook", db_gethook}, - {"getinfo", db_getinfo}, - {"getlocal", db_getlocal}, - {"getregistry", db_getregistry}, - {"getmetatable", db_getmetatable}, - {"getupvalue", db_getupvalue}, - {"upvaluejoin", db_upvaluejoin}, - {"upvalueid", db_upvalueid}, - {"setuservalue", db_setuservalue}, - {"sethook", db_sethook}, - {"setlocal", db_setlocal}, - {"setmetatable", db_setmetatable}, - {"setupvalue", db_setupvalue}, - {"traceback", db_traceback}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_debug (lua_State *L) { - luaL_newlib(L, dblib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.c deleted file mode 100644 index 9611846..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.c +++ /dev/null @@ -1,610 +0,0 @@ -/* -** $Id: ldebug.c,v 2.90.1.4 2015/02/19 17:05:13 roberto Exp $ -** Debug Interface -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - - -#define ldebug_c -#define LUA_CORE - -#include "lua.h" - -#include "lapi.h" -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - - -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) - - -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); - - -static int currentpc (CallInfo *ci) { - lua_assert(isLua(ci)); - return pcRel(ci->u.l.savedpc, ci_func(ci)->p); -} - - -static int currentline (CallInfo *ci) { - return getfuncline(ci_func(ci)->p, currentpc(ci)); -} - - -static void swapextra (lua_State *L) { - if (L->status == LUA_YIELD) { - CallInfo *ci = L->ci; /* get function that yielded */ - StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ - ci->func = restorestack(L, ci->extra); - ci->extra = savestack(L, temp); - } -} - - -/* -** this function can be called asynchronous (e.g. during a signal) -*/ -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { - if (func == NULL || mask == 0) { /* turn off hooks? */ - mask = 0; - func = NULL; - } - if (isLua(L->ci)) - L->oldpc = L->ci->u.l.savedpc; - L->hook = func; - L->basehookcount = count; - resethookcount(L); - L->hookmask = cast_byte(mask); - return 1; -} - - -LUA_API lua_Hook lua_gethook (lua_State *L) { - return L->hook; -} - - -LUA_API int lua_gethookmask (lua_State *L) { - return L->hookmask; -} - - -LUA_API int lua_gethookcount (lua_State *L) { - return L->basehookcount; -} - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { - int status; - CallInfo *ci; - if (level < 0) return 0; /* invalid (negative) level */ - lua_lock(L); - for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) - level--; - if (level == 0 && ci != &L->base_ci) { /* level found? */ - status = 1; - ar->i_ci = ci; - } - else status = 0; /* no such level */ - lua_unlock(L); - return status; -} - - -static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); - if (s == NULL) return "?"; - else return getstr(s); -} - - -static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->numparams; - if (n >= ci->u.l.base - ci->func - nparams) - return NULL; /* no such vararg */ - else { - *pos = ci->func + nparams + n; - return "(*vararg)"; /* generic name for any vararg */ - } -} - - -static const char *findlocal (lua_State *L, CallInfo *ci, int n, - StkId *pos) { - const char *name = NULL; - StkId base; - if (isLua(ci)) { - if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); - else { - base = ci->u.l.base; - name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); - } - } - else - base = ci->func + 1; - if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; - if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ - name = "(*temporary)"; /* generic name for any valid slot */ - else - return NULL; /* no name */ - } - *pos = base + (n - 1); - return name; -} - - -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { - const char *name; - lua_lock(L); - swapextra(L); - if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(L->top - 1)) /* not a Lua function? */ - name = NULL; - else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); - } - else { /* active function; get information through 'ar' */ - StkId pos = 0; /* to avoid warnings */ - name = findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobj2s(L, L->top, pos); - api_incr_top(L); - } - } - swapextra(L); - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = 0; /* to avoid warnings */ - const char *name; - lua_lock(L); - swapextra(L); - name = findlocal(L, ar->i_ci, n, &pos); - if (name) - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ - swapextra(L); - lua_unlock(L); - return name; -} - - -static void funcinfo (lua_Debug *ar, Closure *cl) { - if (noLuaClosure(cl)) { - ar->source = "=[C]"; - ar->linedefined = -1; - ar->lastlinedefined = -1; - ar->what = "C"; - } - else { - Proto *p = cl->l.p; - ar->source = p->source ? getstr(p->source) : "=?"; - ar->linedefined = p->linedefined; - ar->lastlinedefined = p->lastlinedefined; - ar->what = (ar->linedefined == 0) ? "main" : "Lua"; - } - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); -} - - -static void collectvalidlines (lua_State *L, Closure *f) { - if (noLuaClosure(f)) { - setnilvalue(L->top); - api_incr_top(L); - } - else { - int i; - TValue v; - int *lineinfo = f->l.p->lineinfo; - Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue(L, L->top, t); /* push it on stack */ - api_incr_top(L); - setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ - luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ - } -} - - -static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, - Closure *f, CallInfo *ci) { - int status = 1; - for (; *what; what++) { - switch (*what) { - case 'S': { - funcinfo(ar, f); - break; - } - case 'l': { - ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; - break; - } - case 'u': { - ar->nups = (f == NULL) ? 0 : f->c.nupvalues; - if (noLuaClosure(f)) { - ar->isvararg = 1; - ar->nparams = 0; - } - else { - ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams; - } - break; - } - case 't': { - ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; - break; - } - case 'n': { - /* calling function is a known Lua function? */ - if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) - ar->namewhat = getfuncname(L, ci->previous, &ar->name); - else - ar->namewhat = NULL; - if (ar->namewhat == NULL) { - ar->namewhat = ""; /* not found */ - ar->name = NULL; - } - break; - } - case 'L': - case 'f': /* handled by lua_getinfo */ - break; - default: status = 0; /* invalid option */ - } - } - return status; -} - - -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { - int status; - Closure *cl; - CallInfo *ci; - StkId func; - lua_lock(L); - swapextra(L); - if (*what == '>') { - ci = NULL; - func = L->top - 1; - api_check(L, ttisfunction(func), "function expected"); - what++; /* skip the '>' */ - L->top--; /* pop function */ - } - else { - ci = ar->i_ci; - func = ci->func; - lua_assert(ttisfunction(ci->func)); - } - cl = ttisclosure(func) ? clvalue(func) : NULL; - status = auxgetinfo(L, what, ar, cl, ci); - if (strchr(what, 'f')) { - setobjs2s(L, L->top, func); - api_incr_top(L); - } - swapextra(L); - if (strchr(what, 'L')) - collectvalidlines(L, cl); - lua_unlock(L); - return status; -} - - -/* -** {====================================================== -** Symbolic Execution -** ======================================================= -*/ - -static const char *getobjname (Proto *p, int lastpc, int reg, - const char **name); - - -/* -** find a "name" for the RK value 'c' -*/ -static void kname (Proto *p, int pc, int c, const char **name) { - if (ISK(c)) { /* is 'c' a constant? */ - TValue *kvalue = &p->k[INDEXK(c)]; - if (ttisstring(kvalue)) { /* literal constant? */ - *name = svalue(kvalue); /* it is its own name */ - return; - } - /* else no reasonable name found */ - } - else { /* 'c' is a register */ - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (what && *what == 'c') { /* found a constant name? */ - return; /* 'name' already filled */ - } - /* else no reasonable name found */ - } - *name = "?"; /* no reasonable name found */ -} - - -static int filterpc (int pc, int jmptarget) { - if (pc < jmptarget) /* is code conditional (inside a jump)? */ - return -1; /* cannot know who sets that register */ - else return pc; /* current position sets that register */ -} - - -/* -** try to find last instruction before 'lastpc' that modified register 'reg' -*/ -static int findsetreg (Proto *p, int lastpc, int reg) { - int pc; - int setreg = -1; /* keep last instruction that changed 'reg' */ - int jmptarget = 0; /* any code before this address is conditional */ - for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); - int a = GETARG_A(i); - switch (op) { - case OP_LOADNIL: { - int b = GETARG_B(i); - if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ - setreg = filterpc(pc, jmptarget); - break; - } - case OP_TFORCALL: { - if (reg >= a + 2) /* affect all regs above its base */ - setreg = filterpc(pc, jmptarget); - break; - } - case OP_CALL: - case OP_TAILCALL: { - if (reg >= a) /* affect all registers above base */ - setreg = filterpc(pc, jmptarget); - break; - } - case OP_JMP: { - int b = GETARG_sBx(i); - int dest = pc + 1 + b; - /* jump is forward and do not skip `lastpc'? */ - if (pc < dest && dest <= lastpc) { - if (dest > jmptarget) - jmptarget = dest; /* update 'jmptarget' */ - } - break; - } - case OP_TEST: { - if (reg == a) /* jumped code can change 'a' */ - setreg = filterpc(pc, jmptarget); - break; - } - default: - if (testAMode(op) && reg == a) /* any instruction that set A */ - setreg = filterpc(pc, jmptarget); - break; - } - } - return setreg; -} - - -static const char *getobjname (Proto *p, int lastpc, int reg, - const char **name) { - int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); - if (*name) /* is a local? */ - return "local"; - /* else try symbolic execution */ - pc = findsetreg(p, lastpc, reg); - if (pc != -1) { /* could find instruction? */ - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); - switch (op) { - case OP_MOVE: { - int b = GETARG_B(i); /* move from 'b' to 'a' */ - if (b < GETARG_A(i)) - return getobjname(p, pc, b, name); /* get name for 'b' */ - break; - } - case OP_GETTABUP: - case OP_GETTABLE: { - int k = GETARG_C(i); /* key index */ - int t = GETARG_B(i); /* table index */ - const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ - ? luaF_getlocalname(p, t + 1, pc) - : upvalname(p, t); - kname(p, pc, k, name); - return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; - } - case OP_GETUPVAL: { - *name = upvalname(p, GETARG_B(i)); - return "upvalue"; - } - case OP_LOADK: - case OP_LOADKX: { - int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); - if (ttisstring(&p->k[b])) { - *name = svalue(&p->k[b]); - return "constant"; - } - break; - } - case OP_SELF: { - int k = GETARG_C(i); /* key index */ - kname(p, pc, k, name); - return "method"; - } - default: break; /* go through to return NULL */ - } - } - return NULL; /* could not find reasonable name */ -} - - -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - TMS tm; - Proto *p = ci_func(ci)->p; /* calling function */ - int pc = currentpc(ci); /* calling instruction index */ - Instruction i = p->code[pc]; /* calling instruction */ - switch (GET_OPCODE(i)) { - case OP_CALL: - case OP_TAILCALL: /* get function name */ - return getobjname(p, pc, GETARG_A(i), name); - case OP_TFORCALL: { /* for iterator */ - *name = "for iterator"; - return "for iterator"; - } - /* all other instructions can call only through metamethods */ - case OP_SELF: - case OP_GETTABUP: - case OP_GETTABLE: tm = TM_INDEX; break; - case OP_SETTABUP: - case OP_SETTABLE: tm = TM_NEWINDEX; break; - case OP_EQ: tm = TM_EQ; break; - case OP_ADD: tm = TM_ADD; break; - case OP_SUB: tm = TM_SUB; break; - case OP_MUL: tm = TM_MUL; break; - case OP_DIV: tm = TM_DIV; break; - case OP_MOD: tm = TM_MOD; break; - case OP_POW: tm = TM_POW; break; - case OP_UNM: tm = TM_UNM; break; - case OP_LEN: tm = TM_LEN; break; - case OP_LT: tm = TM_LT; break; - case OP_LE: tm = TM_LE; break; - case OP_CONCAT: tm = TM_CONCAT; break; - default: - return NULL; /* else no useful name can be found */ - } - *name = getstr(G(L)->tmname[tm]); - return "metamethod"; -} - -/* }====================================================== */ - - - -/* -** only ANSI way to check whether a pointer points to an array -** (used only for error messages, so efficiency is not a big concern) -*/ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId p; - for (p = ci->u.l.base; p < ci->top; p++) - if (o == p) return 1; - return 0; -} - - -static const char *getupvalname (CallInfo *ci, const TValue *o, - const char **name) { - LClosure *c = ci_func(ci); - int i; - for (i = 0; i < c->nupvalues; i++) { - if (c->upvals[i]->v == o) { - *name = upvalname(c->p, i); - return "upvalue"; - } - } - return NULL; -} - - -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { - CallInfo *ci = L->ci; - const char *name = NULL; - const char *t = objtypename(o); - const char *kind = NULL; - if (isLua(ci)) { - kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(o - ci->u.l.base), &name); - } - if (kind) - luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", - op, kind, name, t); - else - luaG_runerror(L, "attempt to %s a %s value", op, t); -} - - -l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; - lua_assert(!ttisstring(p1) && !ttisnumber(p1)); - luaG_typeerror(L, p1, "concatenate"); -} - - -l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { - TValue temp; - if (luaV_tonumber(p1, &temp) == NULL) - p2 = p1; /* first operand is wrong */ - luaG_typeerror(L, p2, "perform arithmetic on"); -} - - -l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { - const char *t1 = objtypename(p1); - const char *t2 = objtypename(p2); - if (t1 == t2) - luaG_runerror(L, "attempt to compare two %s values", t1); - else - luaG_runerror(L, "attempt to compare %s with %s", t1, t2); -} - - -static void addinfo (lua_State *L, const char *msg) { - CallInfo *ci = L->ci; - if (isLua(ci)) { /* is Lua code? */ - char buff[LUA_IDSIZE]; /* add file:line information */ - int line = currentline(ci); - TString *src = ci_func(ci)->p->source; - if (src) - luaO_chunkid(buff, getstr(src), LUA_IDSIZE); - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; - } - luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); - } -} - - -l_noret luaG_errormsg (lua_State *L) { - if (L->errfunc != 0) { /* is there an error handling function? */ - StkId errfunc = restorestack(L, L->errfunc); - if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; - luaD_call(L, L->top - 2, 1, 0); /* call it */ - } - luaD_throw(L, LUA_ERRRUN); -} - - -l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - addinfo(L, luaO_pushvfstring(L, fmt, argp)); - va_end(argp); - luaG_errormsg(L); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.h deleted file mode 100644 index 6445c76..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldebug.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ -** Auxiliary functions from Debug Interface module -** See Copyright Notice in lua.h -*/ - -#ifndef ldebug_h -#define ldebug_h - - -#include "lstate.h" - - -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) - -#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) - -#define resethookcount(L) (L->hookcount = L->basehookcount) - -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue((ci)->func)) - - -LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, - const char *opname); -LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); -LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); -LUAI_FUNC l_noret luaG_errormsg (lua_State *L); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.c deleted file mode 100644 index e9dd5fa..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.c +++ /dev/null @@ -1,681 +0,0 @@ -/* -** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define ldo_c -#define LUA_CORE - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" -#include "lzio.h" - - - - -/* -** {====================================================== -** Error-recovery functions -** ======================================================= -*/ - -/* -** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By -** default, Lua handles errors with exceptions when compiling as -** C++ code, with _longjmp/_setjmp when asked to use them, and with -** longjmp/setjmp otherwise. -*/ -#if !defined(LUAI_THROW) - -#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) -/* C++ exceptions */ -#define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,a) \ - try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy variable */ - -#elif defined(LUA_USE_ULONGJMP) -/* in Unix, try _longjmp/_setjmp (more efficient) */ -#define LUAI_THROW(L,c) _longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#else -/* default handling with long jumps */ -#define LUAI_THROW(L,c) longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#endif - -#endif - - - -/* chain list of long jump buffers */ -struct lua_longjmp { - struct lua_longjmp *previous; - luai_jmpbuf b; - volatile int status; /* error code */ -}; - - -static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - default: { - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ - break; - } - } - L->top = oldtop + 1; -} - - -l_noret luaD_throw (lua_State *L, int errcode) { - if (L->errorJmp) { /* thread has an error handler? */ - L->errorJmp->status = errcode; /* set status */ - LUAI_THROW(L, L->errorJmp); /* jump to it */ - } - else { /* thread has no error handler */ - L->status = cast_byte(errcode); /* mark it as dead */ - if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ - luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ - } - else { /* no handler at all; abort */ - if (G(L)->panic) { /* panic function? */ - lua_unlock(L); - G(L)->panic(L); /* call it (last chance to jump out) */ - } - abort(); - } - } -} - - -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - unsigned short oldnCcalls = L->nCcalls; - struct lua_longjmp lj; - lj.status = LUA_OK; - lj.previous = L->errorJmp; /* chain new error handler */ - L->errorJmp = &lj; - LUAI_TRY(L, &lj, - (*f)(L, ud); - ); - L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls; - return lj.status; -} - -/* }====================================================== */ - - -static void correctstack (lua_State *L, TValue *oldstack) { - CallInfo *ci; - GCObject *up; - L->top = (L->top - oldstack) + L->stack; - for (up = L->openupval; up != NULL; up = up->gch.next) - gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + L->stack; - ci->func = (ci->func - oldstack) + L->stack; - if (isLua(ci)) - ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; - } -} - - -/* some space for error handling */ -#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) - - -void luaD_reallocstack (lua_State *L, int newsize) { - TValue *oldstack = L->stack; - int lim = L->stacksize; - lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); - for (; lim < newsize; lim++) - setnilvalue(L->stack + lim); /* erase new segment */ - L->stacksize = newsize; - L->stack_last = L->stack + newsize - EXTRA_STACK; - correctstack(L, oldstack); -} - - -void luaD_growstack (lua_State *L, int n) { - int size = L->stacksize; - if (size > LUAI_MAXSTACK) /* error after extra size? */ - luaD_throw(L, LUA_ERRERR); - else { - int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; - int newsize = 2 * size; - if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; - if (newsize < needed) newsize = needed; - if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ - luaD_reallocstack(L, ERRORSTACKSIZE); - luaG_runerror(L, "stack overflow"); - } - else - luaD_reallocstack(L, newsize); - } -} - - -static int stackinuse (lua_State *L) { - CallInfo *ci; - StkId lim = L->top; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - lua_assert(ci->top <= L->stack_last); - if (lim < ci->top) lim = ci->top; - } - return cast_int(lim - L->stack) + 1; /* part of stack in use */ -} - - -void luaD_shrinkstack (lua_State *L) { - int inuse = stackinuse(L); - int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; - if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; - if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */ - goodsize >= L->stacksize) /* would grow instead of shrink? */ - condmovestack(L); /* don't change stack (change only for debugging) */ - else - luaD_reallocstack(L, goodsize); /* shrink it */ -} - - -void luaD_hook (lua_State *L, int event, int line) { - lua_Hook hook = L->hook; - if (hook && L->allowhook) { - CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); - lua_Debug ar; - ar.event = event; - ar.currentline = line; - ar.i_ci = ci; - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= CIST_HOOKED; - lua_unlock(L); - (*hook)(L, &ar); - lua_lock(L); - lua_assert(!L->allowhook); - L->allowhook = 1; - ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); - ci->callstatus &= ~CIST_HOOKED; - } -} - - -static void callhook (lua_State *L, CallInfo *ci) { - int hook = LUA_HOOKCALL; - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (isLua(ci->previous) && - GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { - ci->callstatus |= CIST_TAIL; - hook = LUA_HOOKTAILCALL; - } - luaD_hook(L, hook, -1); - ci->u.l.savedpc--; /* correct 'pc' */ -} - - -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { - int i; - int nfixargs = p->numparams; - StkId base, fixed; - lua_assert(actual >= nfixargs); - /* move fixed parameters to final position */ - luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ - fixed = L->top - actual; /* first fixed argument */ - base = L->top; /* final position of first argument */ - for (i=0; itop++, fixed + i); - setnilvalue(fixed + i); - } - return base; -} - - -static StkId tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); - StkId p; - ptrdiff_t funcr = savestack(L, func); - if (!ttisfunction(tm)) - luaG_typeerror(L, func, "call"); - /* Open a hole inside the stack at `func' */ - for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); - incr_top(L); - func = restorestack(L, funcr); /* previous call may change stack */ - setobj2s(L, func, tm); /* tag method is the new function to be called */ - return func; -} - - - -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) - - -/* -** returns true if function has been executed (C function) -*/ -int luaD_precall (lua_State *L, StkId func, int nresults) { - lua_CFunction f; - CallInfo *ci; - int n; /* number of arguments (Lua) or returns (C) */ - ptrdiff_t funcr = savestack(L, func); - switch (ttype(func)) { - case LUA_TLCF: /* light C function */ - f = fvalue(func); - goto Cfunc; - case LUA_TCCL: { /* C closure */ - f = clCvalue(func)->f; - Cfunc: - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = restorestack(L, funcr); - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; - luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, L->top - n); - return 1; - } - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - Proto *p = clLvalue(func)->p; - n = cast_int(L->top - func) - 1; /* number of real arguments */ - luaD_checkstack(L, p->maxstacksize); - for (; n < p->numparams; n++) - setnilvalue(L->top++); /* complete missing arguments */ - if (!p->is_vararg) { - func = restorestack(L, funcr); - base = func + 1; - } - else { - base = adjust_varargs(L, p, n); - func = restorestack(L, funcr); /* previous call can change stack */ - } - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; - ci->u.l.base = base; - ci->top = base + p->maxstacksize; - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; - L->top = ci->top; - luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); - return 0; - } - default: { /* not a function */ - func = tryfuncTM(L, func); /* retry with 'function' tag method */ - return luaD_precall(L, func, nresults); /* now it must be a function */ - } - } -} - - -int luaD_poscall (lua_State *L, StkId firstResult) { - StkId res; - int wanted, i; - CallInfo *ci = L->ci; - if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { - if (L->hookmask & LUA_MASKRET) { - ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - luaD_hook(L, LUA_HOOKRET, -1); - firstResult = restorestack(L, fr); - } - L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ - } - res = ci->func; /* res == final position of 1st result */ - wanted = ci->nresults; - L->ci = ci = ci->previous; /* back to caller */ - /* move results to correct place */ - for (i = wanted; i != 0 && firstResult < L->top; i--) - setobjs2s(L, res++, firstResult++); - while (i-- > 0) - setnilvalue(res++); - L->top = res; - return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ -} - - -/* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. -*/ -void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { - if (++L->nCcalls >= LUAI_MAXCCALLS) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ - } - if (!allowyield) L->nny++; - if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ - luaV_execute(L); /* call it */ - if (!allowyield) L->nny--; - L->nCcalls--; -} - - -static void finishCcall (lua_State *L) { - CallInfo *ci = L->ci; - int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); - if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ - L->errfunc = ci->u.c.old_errfunc; - } - /* finish 'lua_callk'/'lua_pcall' */ - adjustresults(L, ci->nresults); - /* call continuation function */ - if (!(ci->callstatus & CIST_STAT)) /* no call status? */ - ci->u.c.status = LUA_YIELD; /* 'default' status */ - lua_assert(ci->u.c.status != LUA_OK); - ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; - lua_unlock(L); - n = (*ci->u.c.k)(L); - lua_lock(L); - api_checknelems(L, n); - /* finish 'luaD_precall' */ - luaD_poscall(L, L->top - n); -} - - -static void unroll (lua_State *L, void *ud) { - UNUSED(ud); - for (;;) { - if (L->ci == &L->base_ci) /* stack is empty? */ - return; /* coroutine finished normally */ - if (!isLua(L->ci)) /* C function? */ - finishCcall(L); - else { /* Lua function */ - luaV_finishOp(L); /* finish interrupted instruction */ - luaV_execute(L); /* execute down to higher C 'boundary' */ - } - } -} - - -/* -** check whether thread has a suspended protected call -*/ -static CallInfo *findpcall (lua_State *L) { - CallInfo *ci; - for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ - if (ci->callstatus & CIST_YPCALL) - return ci; - } - return NULL; /* no pending pcall */ -} - - -static int recover (lua_State *L, int status) { - StkId oldtop; - CallInfo *ci = findpcall(L); - if (ci == NULL) return 0; /* no recovery point */ - /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->extra); - luaF_close(L, oldtop); - seterrorobj(L, status, oldtop); - L->ci = ci; - L->allowhook = ci->u.c.old_allowhook; - L->nny = 0; /* should be zero to be yieldable */ - luaD_shrinkstack(L); - L->errfunc = ci->u.c.old_errfunc; - ci->callstatus |= CIST_STAT; /* call has error status */ - ci->u.c.status = status; /* (here it is) */ - return 1; /* continue running the coroutine */ -} - - -/* -** signal an error in the call to 'resume', not in the execution of the -** coroutine itself. (Such errors should not be handled by any coroutine -** error handler and should not kill the coroutine.) -*/ -static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { - L->top = firstArg; /* remove args from the stack */ - setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ - api_incr_top(L); - luaD_throw(L, -1); /* jump back to 'lua_resume' */ -} - - -/* -** do the work for 'lua_resume' in protected mode -*/ -static void resume (lua_State *L, void *ud) { - int nCcalls = L->nCcalls; - StkId firstArg = cast(StkId, ud); - CallInfo *ci = L->ci; - if (nCcalls >= LUAI_MAXCCALLS) - resume_error(L, "C stack overflow", firstArg); - if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (ci != &L->base_ci) /* not in base level? */ - resume_error(L, "cannot resume non-suspended coroutine", firstArg); - /* coroutine is in base level; start running it */ - if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ - luaV_execute(L); /* call it */ - } - else if (L->status != LUA_YIELD) - resume_error(L, "cannot resume dead coroutine", firstArg); - else { /* resuming from previous yield */ - L->status = LUA_OK; - ci->func = restorestack(L, ci->extra); - if (isLua(ci)) /* yielded inside a hook? */ - luaV_execute(L); /* just continue running Lua code */ - else { /* 'common' yield */ - if (ci->u.c.k != NULL) { /* does it have a continuation? */ - int n; - ci->u.c.status = LUA_YIELD; /* 'default' status */ - ci->callstatus |= CIST_YIELDED; - lua_unlock(L); - n = (*ci->u.c.k)(L); /* call continuation */ - lua_lock(L); - api_checknelems(L, n); - firstArg = L->top - n; /* yield results come from continuation */ - } - luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ - } - unroll(L, NULL); - } - lua_assert(nCcalls == L->nCcalls); -} - - -LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { - int status; - int oldnny = L->nny; /* save 'nny' */ - lua_lock(L); - luai_userstateresume(L, nargs); - L->nCcalls = (from) ? from->nCcalls + 1 : 1; - L->nny = 0; /* allow yields */ - api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); - status = luaD_rawrunprotected(L, resume, L->top - nargs); - if (status == -1) /* error calling 'lua_resume'? */ - status = LUA_ERRRUN; - else { /* yield or regular error */ - while (status != LUA_OK && status != LUA_YIELD) { /* error? */ - if (recover(L, status)) /* recover point? */ - status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ - else { /* unrecoverable error */ - L->status = cast_byte(status); /* mark thread as `dead' */ - seterrorobj(L, status, L->top); - L->ci->top = L->top; - break; - } - } - lua_assert(status == L->status); - } - L->nny = oldnny; /* restore 'nny' */ - L->nCcalls--; - lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); - lua_unlock(L); - return status; -} - - -LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { - CallInfo *ci = L->ci; - luai_userstateyield(L, nresults); - lua_lock(L); - api_checknelems(L, nresults); - if (L->nny > 0) { - if (L != G(L)->mainthread) - luaG_runerror(L, "attempt to yield across a C-call boundary"); - else - luaG_runerror(L, "attempt to yield from outside a coroutine"); - } - L->status = LUA_YIELD; - ci->extra = savestack(L, ci->func); /* save current 'func' */ - if (isLua(ci)) { /* inside a hook? */ - api_check(L, k == NULL, "hooks cannot continue after yielding"); - } - else { - if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ - ci->u.c.ctx = ctx; /* save context */ - ci->func = L->top - nresults - 1; /* protect stack below results */ - luaD_throw(L, LUA_YIELD); - } - lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ - lua_unlock(L); - return 0; /* return to 'luaD_hook' */ -} - - -int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t old_top, ptrdiff_t ef) { - int status; - CallInfo *old_ci = L->ci; - lu_byte old_allowhooks = L->allowhook; - unsigned short old_nny = L->nny; - ptrdiff_t old_errfunc = L->errfunc; - L->errfunc = ef; - status = luaD_rawrunprotected(L, func, u); - if (status != LUA_OK) { /* an error occurred? */ - StkId oldtop = restorestack(L, old_top); - luaF_close(L, oldtop); /* close possible pending closures */ - seterrorobj(L, status, oldtop); - L->ci = old_ci; - L->allowhook = old_allowhooks; - L->nny = old_nny; - luaD_shrinkstack(L); - } - L->errfunc = old_errfunc; - return status; -} - - - -/* -** Execute a protected parser. -*/ -struct SParser { /* data to `f_parser' */ - ZIO *z; - Mbuffer buff; /* dynamic structure used by the scanner */ - Dyndata dyd; /* dynamic structures used by the parser */ - const char *mode; - const char *name; -}; - - -static void checkmode (lua_State *L, const char *mode, const char *x) { - if (mode && strchr(mode, x[0]) == NULL) { - luaO_pushfstring(L, - "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); - luaD_throw(L, LUA_ERRSYNTAX); - } -} - - -static void f_parser (lua_State *L, void *ud) { - int i; - Closure *cl; - struct SParser *p = cast(struct SParser *, ud); - int c = zgetc(p->z); /* read first character */ - if (c == LUA_SIGNATURE[0]) { - checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, &p->buff, p->name); - } - else { - checkmode(L, p->mode, "text"); - cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); - } - lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues); - for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */ - UpVal *up = luaF_newupval(L); - cl->l.upvals[i] = up; - luaC_objbarrier(L, cl, up); - } -} - - -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode) { - struct SParser p; - int status; - L->nny++; /* cannot yield during parsing */ - p.z = z; p.name = name; p.mode = mode; - p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; - p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; - p.dyd.label.arr = NULL; p.dyd.label.size = 0; - luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); - luaZ_freebuffer(L, &p.buff); - luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); - luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); - luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); - L->nny--; - return status; -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.h deleted file mode 100644 index d3d3082..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldo.h +++ /dev/null @@ -1,46 +0,0 @@ -/* -** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#ifndef ldo_h -#define ldo_h - - -#include "lobject.h" -#include "lstate.h" -#include "lzio.h" - - -#define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ - luaD_growstack(L, n); else condmovestack(L); - - -#define incr_top(L) {L->top++; luaD_checkstack(L,0);} - -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) - - -/* type of protected functions, to be ran by `runprotected' */ -typedef void (*Pfunc) (lua_State *L, void *ud); - -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode); -LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); -LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, - int allowyield); -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); -LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); -LUAI_FUNC void luaD_growstack (lua_State *L, int n); -LUAI_FUNC void luaD_shrinkstack (lua_State *L); - -LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldump.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldump.c deleted file mode 100644 index 61fa2cd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ldump.c +++ /dev/null @@ -1,173 +0,0 @@ -/* -** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ -** save precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#include - -#define ldump_c -#define LUA_CORE - -#include "lua.h" - -#include "lobject.h" -#include "lstate.h" -#include "lundump.h" - -typedef struct { - lua_State* L; - lua_Writer writer; - void* data; - int strip; - int status; -} DumpState; - -#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) -#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) - -static void DumpBlock(const void* b, size_t size, DumpState* D) -{ - if (D->status==0) - { - lua_unlock(D->L); - D->status=(*D->writer)(D->L,b,size,D->data); - lua_lock(D->L); - } -} - -static void DumpChar(int y, DumpState* D) -{ - char x=(char)y; - DumpVar(x,D); -} - -static void DumpInt(int x, DumpState* D) -{ - DumpVar(x,D); -} - -static void DumpNumber(lua_Number x, DumpState* D) -{ - DumpVar(x,D); -} - -static void DumpVector(const void* b, int n, size_t size, DumpState* D) -{ - DumpInt(n,D); - DumpMem(b,n,size,D); -} - -static void DumpString(const TString* s, DumpState* D) -{ - if (s==NULL) - { - size_t size=0; - DumpVar(size,D); - } - else - { - size_t size=s->tsv.len+1; /* include trailing '\0' */ - DumpVar(size,D); - DumpBlock(getstr(s),size*sizeof(char),D); - } -} - -#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) - -static void DumpFunction(const Proto* f, DumpState* D); - -static void DumpConstants(const Proto* f, DumpState* D) -{ - int i,n=f->sizek; - DumpInt(n,D); - for (i=0; ik[i]; - DumpChar(ttypenv(o),D); - switch (ttypenv(o)) - { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpChar(bvalue(o),D); - break; - case LUA_TNUMBER: - DumpNumber(nvalue(o),D); - break; - case LUA_TSTRING: - DumpString(rawtsvalue(o),D); - break; - default: lua_assert(0); - } - } - n=f->sizep; - DumpInt(n,D); - for (i=0; ip[i],D); -} - -static void DumpUpvalues(const Proto* f, DumpState* D) -{ - int i,n=f->sizeupvalues; - DumpInt(n,D); - for (i=0; iupvalues[i].instack,D); - DumpChar(f->upvalues[i].idx,D); - } -} - -static void DumpDebug(const Proto* f, DumpState* D) -{ - int i,n; - DumpString((D->strip) ? NULL : f->source,D); - n= (D->strip) ? 0 : f->sizelineinfo; - DumpVector(f->lineinfo,n,sizeof(int),D); - n= (D->strip) ? 0 : f->sizelocvars; - DumpInt(n,D); - for (i=0; ilocvars[i].varname,D); - DumpInt(f->locvars[i].startpc,D); - DumpInt(f->locvars[i].endpc,D); - } - n= (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n,D); - for (i=0; iupvalues[i].name,D); -} - -static void DumpFunction(const Proto* f, DumpState* D) -{ - DumpInt(f->linedefined,D); - DumpInt(f->lastlinedefined,D); - DumpChar(f->numparams,D); - DumpChar(f->is_vararg,D); - DumpChar(f->maxstacksize,D); - DumpCode(f,D); - DumpConstants(f,D); - DumpUpvalues(f,D); - DumpDebug(f,D); -} - -static void DumpHeader(DumpState* D) -{ - lu_byte h[LUAC_HEADERSIZE]; - luaU_header(h); - DumpBlock(h,LUAC_HEADERSIZE,D); -} - -/* -** dump Lua function as precompiled chunk -*/ -int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) -{ - DumpState D; - D.L=L; - D.writer=w; - D.data=data; - D.strip=strip; - D.status=0; - DumpHeader(&D); - DumpFunction(f,&D); - return D.status; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.c deleted file mode 100644 index e90e152..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.c +++ /dev/null @@ -1,161 +0,0 @@ -/* -** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - - -#include - -#define lfunc_c -#define LUA_CORE - -#include "lua.h" - -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -Closure *luaF_newCclosure (lua_State *L, int n) { - Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; - c->c.nupvalues = cast_byte(n); - return c; -} - - -Closure *luaF_newLclosure (lua_State *L, int n) { - Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; - c->l.p = NULL; - c->l.nupvalues = cast_byte(n); - while (n--) c->l.upvals[n] = NULL; - return c; -} - - -UpVal *luaF_newupval (lua_State *L) { - UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; - uv->v = &uv->u.value; - setnilvalue(uv->v); - return uv; -} - - -UpVal *luaF_findupval (lua_State *L, StkId level) { - global_State *g = G(L); - GCObject **pp = &L->openupval; - UpVal *p; - UpVal *uv; - while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { - GCObject *o = obj2gco(p); - lua_assert(p->v != &p->u.value); - lua_assert(!isold(o) || isold(obj2gco(L))); - if (p->v == level) { /* found a corresponding upvalue? */ - if (isdead(g, o)) /* is it dead? */ - changewhite(o); /* resurrect it */ - return p; - } - pp = &p->next; - } - /* not found: create a new one */ - uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; - uv->v = level; /* current value lives in the stack */ - uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ - uv->u.l.next = g->uvhead.u.l.next; - uv->u.l.next->u.l.prev = uv; - g->uvhead.u.l.next = uv; - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - return uv; -} - - -static void unlinkupval (UpVal *uv) { - lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); - uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ - uv->u.l.prev->u.l.next = uv->u.l.next; -} - - -void luaF_freeupval (lua_State *L, UpVal *uv) { - if (uv->v != &uv->u.value) /* is it open? */ - unlinkupval(uv); /* remove from open list */ - luaM_free(L, uv); /* free upvalue */ -} - - -void luaF_close (lua_State *L, StkId level) { - UpVal *uv; - global_State *g = G(L); - while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o) && uv->v != &uv->u.value); - L->openupval = uv->next; /* remove from `open' list */ - if (isdead(g, o)) - luaF_freeupval(L, uv); /* free upvalue */ - else { - unlinkupval(uv); /* remove upvalue from 'uvhead' list */ - setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ - uv->v = &uv->u.value; /* now current value lives here */ - gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ - g->allgc = o; - luaC_checkupvalcolor(g, uv); - } - } -} - - -Proto *luaF_newproto (lua_State *L) { - Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; - f->k = NULL; - f->sizek = 0; - f->p = NULL; - f->sizep = 0; - f->code = NULL; - f->cache = NULL; - f->sizecode = 0; - f->lineinfo = NULL; - f->sizelineinfo = 0; - f->upvalues = NULL; - f->sizeupvalues = 0; - f->numparams = 0; - f->is_vararg = 0; - f->maxstacksize = 0; - f->locvars = NULL; - f->sizelocvars = 0; - f->linedefined = 0; - f->lastlinedefined = 0; - f->source = NULL; - return f; -} - - -void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); - luaM_free(L, f); -} - - -/* -** Look for n-th local variable at line `line' in function `func'. -** Returns NULL if not found. -*/ -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { - int i; - for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { - if (pc < f->locvars[i].endpc) { /* is variable active? */ - local_number--; - if (local_number == 0) - return getstr(f->locvars[i].varname); - } - } - return NULL; /* not found */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.h deleted file mode 100644 index ca0d3a3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lfunc.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - -#ifndef lfunc_h -#define lfunc_h - - -#include "lobject.h" - - -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ - cast(int, sizeof(TValue)*((n)-1))) - -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ - cast(int, sizeof(TValue *)*((n)-1))) - - -LUAI_FUNC Proto *luaF_newproto (lua_State *L); -LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); -LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); -LUAI_FUNC UpVal *luaF_newupval (lua_State *L); -LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level); -LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); -LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); -LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, - int pc); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.c deleted file mode 100644 index 553fd17..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.c +++ /dev/null @@ -1,1220 +0,0 @@ -/* -** $Id: lgc.c,v 2.140.1.3 2014/09/01 16:55:08 roberto Exp $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#include - -#define lgc_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - - -/* -** cost of sweeping one element (the size of a small object divided -** by some adjust for the sweep speed) -*/ -#define GCSWEEPCOST ((sizeof(TString) + 4) / 4) - -/* maximum number of elements to sweep in each single step */ -#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) - -/* maximum number of finalizers to call in each GC step */ -#define GCFINALIZENUM 4 - - -/* -** macro to adjust 'stepmul': 'stepmul' is actually used like -** 'stepmul / STEPMULADJ' (value chosen by tests) -*/ -#define STEPMULADJ 200 - - -/* -** macro to adjust 'pause': 'pause' is actually used like -** 'pause / PAUSEADJ' (value chosen by tests) -*/ -#define PAUSEADJ 100 - - -/* -** 'makewhite' erases all color bits plus the old bit and then -** sets only the current white bit -*/ -#define maskcolors (~(bit2mask(BLACKBIT, OLDBIT) | WHITEBITS)) -#define makewhite(g,x) \ - (gch(x)->marked = cast_byte((gch(x)->marked & maskcolors) | luaC_white(g))) - -#define white2gray(x) resetbits(gch(x)->marked, WHITEBITS) -#define black2gray(x) resetbit(gch(x)->marked, BLACKBIT) - - -#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT) - -#define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n))) - - -#define checkconsistency(obj) \ - lua_longassert(!iscollectable(obj) || righttt(obj)) - - -#define markvalue(g,o) { checkconsistency(o); \ - if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } - -#define markobject(g,t) { if ((t) && iswhite(obj2gco(t))) \ - reallymarkobject(g, obj2gco(t)); } - -static void reallymarkobject (global_State *g, GCObject *o); - - -/* -** {====================================================== -** Generic functions -** ======================================================= -*/ - - -/* -** one after last element in a hash array -*/ -#define gnodelast(h) gnode(h, cast(size_t, sizenode(h))) - - -/* -** link table 'h' into list pointed by 'p' -*/ -#define linktable(h,p) ((h)->gclist = *(p), *(p) = obj2gco(h)) - - -/* -** if key is not marked, mark its entry as dead (therefore removing it -** from the table) -*/ -static void removeentry (Node *n) { - lua_assert(ttisnil(gval(n))); - if (valiswhite(gkey(n))) - setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */ -} - - -/* -** tells whether a key or value can be cleared from a weak -** table. Non-collectable objects are never removed from weak -** tables. Strings behave as `values', so are never removed too. for -** other objects: if really collected, cannot keep them; for objects -** being finalized, keep them in keys, but not in values -*/ -static int iscleared (global_State *g, const TValue *o) { - if (!iscollectable(o)) return 0; - else if (ttisstring(o)) { - markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */ - return 0; - } - else return iswhite(gcvalue(o)); -} - - -/* -** barrier that moves collector forward, that is, mark the white object -** being pointed by a black object. -*/ -void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { - global_State *g = G(L); - lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - lua_assert(g->gcstate != GCSpause); - lua_assert(gch(o)->tt != LUA_TTABLE); - if (keepinvariantout(g)) /* must keep invariant? */ - reallymarkobject(g, v); /* restore invariant */ - else { /* sweep phase */ - lua_assert(issweepphase(g)); - makewhite(g, o); /* mark main obj. as white to avoid other barriers */ - } -} - - -/* -** barrier that moves collector backward, that is, mark the black object -** pointing to a white object as gray again. (Current implementation -** only works for tables; access to 'gclist' is not uniform across -** different types.) -*/ -void luaC_barrierback_ (lua_State *L, GCObject *o) { - global_State *g = G(L); - lua_assert(isblack(o) && !isdead(g, o) && gch(o)->tt == LUA_TTABLE); - black2gray(o); /* make object gray (again) */ - gco2t(o)->gclist = g->grayagain; - g->grayagain = o; -} - - -/* -** barrier for prototypes. When creating first closure (cache is -** NULL), use a forward barrier; this may be the only closure of the -** prototype (if it is a "regular" function, with a single instance) -** and the prototype may be big, so it is better to avoid traversing -** it again. Otherwise, use a backward barrier, to avoid marking all -** possible instances. -*/ -LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c) { - global_State *g = G(L); - lua_assert(isblack(obj2gco(p))); - if (p->cache == NULL) { /* first time? */ - luaC_objbarrier(L, p, c); - } - else { /* use a backward barrier */ - black2gray(obj2gco(p)); /* make prototype gray (again) */ - p->gclist = g->grayagain; - g->grayagain = obj2gco(p); - } -} - - -/* -** check color (and invariants) for an upvalue that was closed, -** i.e., moved into the 'allgc' list -*/ -void luaC_checkupvalcolor (global_State *g, UpVal *uv) { - GCObject *o = obj2gco(uv); - lua_assert(!isblack(o)); /* open upvalues are never black */ - if (isgray(o)) { - if (keepinvariant(g)) { - resetoldbit(o); /* see MOVE OLD rule */ - gray2black(o); /* it is being visited now */ - markvalue(g, uv->v); - } - else { - lua_assert(issweepphase(g)); - makewhite(g, o); - } - } -} - - -/* -** create a new collectable object (with given type and size) and link -** it to '*list'. 'offset' tells how many bytes to allocate before the -** object itself (used only by states). -*/ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list, - int offset) { - global_State *g = G(L); - char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz)); - GCObject *o = obj2gco(raw + offset); - if (list == NULL) - list = &g->allgc; /* standard list for collectable objects */ - gch(o)->marked = luaC_white(g); - gch(o)->tt = tt; - gch(o)->next = *list; - *list = o; - return o; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Mark functions -** ======================================================= -*/ - - -/* -** mark an object. Userdata, strings, and closed upvalues are visited -** and turned black here. Other objects are marked gray and added -** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list.) -*/ -static void reallymarkobject (global_State *g, GCObject *o) { - lu_mem size; - white2gray(o); - switch (gch(o)->tt) { - case LUA_TSHRSTR: - case LUA_TLNGSTR: { - size = sizestring(gco2ts(o)); - break; /* nothing else to mark; make it black */ - } - case LUA_TUSERDATA: { - Table *mt = gco2u(o)->metatable; - markobject(g, mt); - markobject(g, gco2u(o)->env); - size = sizeudata(gco2u(o)); - break; - } - case LUA_TUPVAL: { - UpVal *uv = gco2uv(o); - markvalue(g, uv->v); - if (uv->v != &uv->u.value) /* open? */ - return; /* open upvalues remain gray */ - size = sizeof(UpVal); - break; - } - case LUA_TLCL: { - gco2lcl(o)->gclist = g->gray; - g->gray = o; - return; - } - case LUA_TCCL: { - gco2ccl(o)->gclist = g->gray; - g->gray = o; - return; - } - case LUA_TTABLE: { - linktable(gco2t(o), &g->gray); - return; - } - case LUA_TTHREAD: { - gco2th(o)->gclist = g->gray; - g->gray = o; - return; - } - case LUA_TPROTO: { - gco2p(o)->gclist = g->gray; - g->gray = o; - return; - } - default: lua_assert(0); return; - } - gray2black(o); - g->GCmemtrav += size; -} - - -/* -** mark metamethods for basic types -*/ -static void markmt (global_State *g) { - int i; - for (i=0; i < LUA_NUMTAGS; i++) - markobject(g, g->mt[i]); -} - - -/* -** mark all objects in list of being-finalized -*/ -static void markbeingfnz (global_State *g) { - GCObject *o; - for (o = g->tobefnz; o != NULL; o = gch(o)->next) { - makewhite(g, o); - reallymarkobject(g, o); - } -} - - -/* -** mark all values stored in marked open upvalues. (See comment in -** 'lstate.h'.) -*/ -static void remarkupvals (global_State *g) { - UpVal *uv; - for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { - if (isgray(obj2gco(uv))) - markvalue(g, uv->v); - } -} - - -/* -** mark root set and reset all gray lists, to start a new -** incremental (or full) collection -*/ -static void restartcollection (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; - markobject(g, g->mainthread); - markvalue(g, &g->l_registry); - markmt(g); - markbeingfnz(g); /* mark any finalizing object left from previous cycle */ -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Traverse functions -** ======================================================= -*/ - -static void traverseweakvalue (global_State *g, Table *h) { - Node *n, *limit = gnodelast(h); - /* if there is array part, assume it may have white values (do not - traverse it just to check) */ - int hasclears = (h->sizearray > 0); - for (n = gnode(h, 0); n < limit; n++) { - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */ - hasclears = 1; /* table will have to be cleared */ - } - } - if (hasclears) - linktable(h, &g->weak); /* has to be cleared later */ - else /* no white values */ - linktable(h, &g->grayagain); /* no need to clean */ -} - - -static int traverseephemeron (global_State *g, Table *h) { - int marked = 0; /* true if an object is marked in this traversal */ - int hasclears = 0; /* true if table has white keys */ - int prop = 0; /* true if table has entry "white-key -> white-value" */ - Node *n, *limit = gnodelast(h); - int i; - /* traverse array part (numeric keys are 'strong') */ - for (i = 0; i < h->sizearray; i++) { - if (valiswhite(&h->array[i])) { - marked = 1; - reallymarkobject(g, gcvalue(&h->array[i])); - } - } - /* traverse hash part */ - for (n = gnode(h, 0); n < limit; n++) { - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */ - hasclears = 1; /* table must be cleared */ - if (valiswhite(gval(n))) /* value not marked yet? */ - prop = 1; /* must propagate again */ - } - else if (valiswhite(gval(n))) { /* value not marked yet? */ - marked = 1; - reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ - } - } - if (g->gcstate != GCSatomic || prop) - linktable(h, &g->ephemeron); /* have to propagate again */ - else if (hasclears) /* does table have white keys? */ - linktable(h, &g->allweak); /* may have to clean white keys */ - else /* no white keys */ - linktable(h, &g->grayagain); /* no need to clean */ - return marked; -} - - -static void traversestrongtable (global_State *g, Table *h) { - Node *n, *limit = gnodelast(h); - int i; - for (i = 0; i < h->sizearray; i++) /* traverse array part */ - markvalue(g, &h->array[i]); - for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - markvalue(g, gval(n)); /* mark value */ - } - } -} - - -static lu_mem traversetable (global_State *g, Table *h) { - const char *weakkey, *weakvalue; - const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - markobject(g, h->metatable); - if (mode && ttisstring(mode) && /* is there a weak mode? */ - ((weakkey = strchr(svalue(mode), 'k')), - (weakvalue = strchr(svalue(mode), 'v')), - (weakkey || weakvalue))) { /* is really weak? */ - black2gray(obj2gco(h)); /* keep table gray */ - if (!weakkey) /* strong keys? */ - traverseweakvalue(g, h); - else if (!weakvalue) /* strong values? */ - traverseephemeron(g, h); - else /* all weak */ - linktable(h, &g->allweak); /* nothing to traverse now */ - } - else /* not weak */ - traversestrongtable(g, h); - return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * cast(size_t, sizenode(h)); -} - - -static int traverseproto (global_State *g, Proto *f) { - int i; - if (f->cache && iswhite(obj2gco(f->cache))) - f->cache = NULL; /* allow cache to be collected */ - markobject(g, f->source); - for (i = 0; i < f->sizek; i++) /* mark literals */ - markvalue(g, &f->k[i]); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobject(g, f->upvalues[i].name); - for (i = 0; i < f->sizep; i++) /* mark nested protos */ - markobject(g, f->p[i]); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobject(g, f->locvars[i].varname); - return sizeof(Proto) + sizeof(Instruction) * f->sizecode + - sizeof(Proto *) * f->sizep + - sizeof(TValue) * f->sizek + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; -} - - -static lu_mem traverseCclosure (global_State *g, CClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ - markvalue(g, &cl->upvalue[i]); - return sizeCclosure(cl->nupvalues); -} - -static lu_mem traverseLclosure (global_State *g, LClosure *cl) { - int i; - markobject(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ - markobject(g, cl->upvals[i]); - return sizeLclosure(cl->nupvalues); -} - - -static lu_mem traversestack (global_State *g, lua_State *th) { - int n = 0; - StkId o = th->stack; - if (o == NULL) - return 1; /* stack not completely built yet */ - for (; o < th->top; o++) /* mark live elements in the stack */ - markvalue(g, o); - if (g->gcstate == GCSatomic) { /* final traversal? */ - StkId lim = th->stack + th->stacksize; /* real end of stack */ - for (; o < lim; o++) /* clear not-marked stack slice */ - setnilvalue(o); - } - else { /* count call infos to compute size */ - CallInfo *ci; - for (ci = &th->base_ci; ci != th->ci; ci = ci->next) - n++; - } - return sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * n; -} - - -/* -** traverse one gray object, turning it to black (except for threads, -** which are always gray). -*/ -static void propagatemark (global_State *g) { - lu_mem size; - GCObject *o = g->gray; - lua_assert(isgray(o)); - gray2black(o); - switch (gch(o)->tt) { - case LUA_TTABLE: { - Table *h = gco2t(o); - g->gray = h->gclist; /* remove from 'gray' list */ - size = traversetable(g, h); - break; - } - case LUA_TLCL: { - LClosure *cl = gco2lcl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseLclosure(g, cl); - break; - } - case LUA_TCCL: { - CClosure *cl = gco2ccl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseCclosure(g, cl); - break; - } - case LUA_TTHREAD: { - lua_State *th = gco2th(o); - g->gray = th->gclist; /* remove from 'gray' list */ - th->gclist = g->grayagain; - g->grayagain = o; /* insert into 'grayagain' list */ - black2gray(o); - size = traversestack(g, th); - break; - } - case LUA_TPROTO: { - Proto *p = gco2p(o); - g->gray = p->gclist; /* remove from 'gray' list */ - size = traverseproto(g, p); - break; - } - default: lua_assert(0); return; - } - g->GCmemtrav += size; -} - - -static void propagateall (global_State *g) { - while (g->gray) propagatemark(g); -} - - -static void propagatelist (global_State *g, GCObject *l) { - lua_assert(g->gray == NULL); /* no grays left */ - g->gray = l; - propagateall(g); /* traverse all elements from 'l' */ -} - -/* -** retraverse all gray lists. Because tables may be reinserted in other -** lists when traversed, traverse the original lists to avoid traversing -** twice the same table (which is not wrong, but inefficient) -*/ -static void retraversegrays (global_State *g) { - GCObject *weak = g->weak; /* save original lists */ - GCObject *grayagain = g->grayagain; - GCObject *ephemeron = g->ephemeron; - g->weak = g->grayagain = g->ephemeron = NULL; - propagateall(g); /* traverse main gray list */ - propagatelist(g, grayagain); - propagatelist(g, weak); - propagatelist(g, ephemeron); -} - - -static void convergeephemerons (global_State *g) { - int changed; - do { - GCObject *w; - GCObject *next = g->ephemeron; /* get ephemeron list */ - g->ephemeron = NULL; /* tables will return to this list when traversed */ - changed = 0; - while ((w = next) != NULL) { - next = gco2t(w)->gclist; - if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */ - propagateall(g); /* propagate changes */ - changed = 1; /* will have to revisit all ephemeron tables */ - } - } - } while (changed); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Sweep Functions -** ======================================================= -*/ - - -/* -** clear entries with unmarked keys from all weaktables in list 'l' up -** to element 'f' -*/ -static void clearkeys (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { - Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); - for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* and remove entry from table */ - } - } - } -} - - -/* -** clear entries with unmarked values from all weaktables in list 'l' up -** to element 'f' -*/ -static void clearvalues (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { - Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); - int i; - for (i = 0; i < h->sizearray; i++) { - TValue *o = &h->array[i]; - if (iscleared(g, o)) /* value was collected? */ - setnilvalue(o); /* remove value */ - } - for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && iscleared(g, gval(n))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* and remove entry from table */ - } - } - } -} - - -static void freeobj (lua_State *L, GCObject *o) { - switch (gch(o)->tt) { - case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TLCL: { - luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); - break; - } - case LUA_TCCL: { - luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); - break; - } - case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; - case LUA_TTABLE: luaH_free(L, gco2t(o)); break; - case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; - case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; - case LUA_TSHRSTR: - G(L)->strt.nuse--; - /* go through */ - case LUA_TLNGSTR: { - luaM_freemem(L, o, sizestring(gco2ts(o))); - break; - } - default: lua_assert(0); - } -} - - -#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); - - -/* -** sweep the (open) upvalues of a thread and resize its stack and -** list of call-info structures. -*/ -static void sweepthread (lua_State *L, lua_State *L1) { - if (L1->stack == NULL) return; /* stack not completely built yet */ - sweepwholelist(L, &L1->openupval); /* sweep open upvalues */ - luaE_freeCI(L1); /* free extra CallInfo slots */ - /* should not change the stack during an emergency gc cycle */ - if (G(L)->gckind != KGC_EMERGENCY) - luaD_shrinkstack(L1); -} - - -/* -** sweep at most 'count' elements from a list of GCObjects erasing dead -** objects, where a dead (not alive) object is one marked with the "old" -** (non current) white and not fixed. -** In non-generational mode, change all non-dead objects back to white, -** preparing for next collection cycle. -** In generational mode, keep black objects black, and also mark them as -** old; stop when hitting an old object, as all objects after that -** one will be old too. -** When object is a thread, sweep its list of open upvalues too. -*/ -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { - global_State *g = G(L); - int ow = otherwhite(g); - int toclear, toset; /* bits to clear and to set in all live objects */ - int tostop; /* stop sweep when this is true */ - if (isgenerational(g)) { /* generational mode? */ - toclear = ~0; /* clear nothing */ - toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */ - tostop = bitmask(OLDBIT); /* do not sweep old generation */ - } - else { /* normal mode */ - toclear = maskcolors; /* clear all color bits + old bit */ - toset = luaC_white(g); /* make object white */ - tostop = 0; /* do not stop */ - } - while (*p != NULL && count-- > 0) { - GCObject *curr = *p; - int marked = gch(curr)->marked; - if (isdeadm(ow, marked)) { /* is 'curr' dead? */ - *p = gch(curr)->next; /* remove 'curr' from list */ - freeobj(L, curr); /* erase 'curr' */ - } - else { - if (testbits(marked, tostop)) - return NULL; /* stop sweeping this list */ - if (gch(curr)->tt == LUA_TTHREAD) - sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */ - /* update marks */ - gch(curr)->marked = cast_byte((marked & toclear) | toset); - p = &gch(curr)->next; /* go to next element */ - } - } - return (*p == NULL) ? NULL : p; -} - - -/* -** sweep a list until a live object (or end of list) -*/ -static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { - GCObject ** old = p; - int i = 0; - do { - i++; - p = sweeplist(L, p, 1); - } while (p == old); - if (n) *n += i; - return p; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Finalization -** ======================================================= -*/ - -static void checkSizes (lua_State *L) { - global_State *g = G(L); - if (g->gckind != KGC_EMERGENCY) { /* do not change sizes in emergency */ - int hs = g->strt.size / 2; /* half the size of the string table */ - if (g->strt.nuse < cast(lu_int32, hs)) /* using less than that half? */ - luaS_resize(L, hs); /* halve its size */ - luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */ - } -} - - -static GCObject *udata2finalize (global_State *g) { - GCObject *o = g->tobefnz; /* get first element */ - lua_assert(isfinalized(o)); - g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */ - gch(o)->next = g->allgc; /* return it to 'allgc' list */ - g->allgc = o; - resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */ - lua_assert(!isold(o)); /* see MOVE OLD rule */ - if (!keepinvariantout(g)) /* not keeping invariant? */ - makewhite(g, o); /* "sweep" object */ - return o; -} - - -static void dothecall (lua_State *L, void *ud) { - UNUSED(ud); - luaD_call(L, L->top - 2, 0, 0); -} - - -static void GCTM (lua_State *L, int propagateerrors) { - global_State *g = G(L); - const TValue *tm; - TValue v; - setgcovalue(L, &v, udata2finalize(g)); - tm = luaT_gettmbyobj(L, &v, TM_GC); - if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */ - int status; - lu_byte oldah = L->allowhook; - int running = g->gcrunning; - L->allowhook = 0; /* stop debug hooks during GC metamethod */ - g->gcrunning = 0; /* avoid GC steps */ - setobj2s(L, L->top, tm); /* push finalizer... */ - setobj2s(L, L->top + 1, &v); /* ... and its argument */ - L->top += 2; /* and (next line) call the finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); - L->allowhook = oldah; /* restore hooks */ - g->gcrunning = running; /* restore state */ - if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ - if (status == LUA_ERRRUN) { /* is there an error object? */ - const char *msg = (ttisstring(L->top - 1)) - ? svalue(L->top - 1) - : "no message"; - luaO_pushfstring(L, "error in __gc metamethod (%s)", msg); - status = LUA_ERRGCMM; /* error in __gc metamethod */ - } - luaD_throw(L, status); /* re-throw error */ - } - } -} - - -/* -** move all unreachable objects (or 'all' objects) that need -** finalization from list 'finobj' to list 'tobefnz' (to be finalized) -*/ -static void separatetobefnz (lua_State *L, int all) { - global_State *g = G(L); - GCObject **p = &g->finobj; - GCObject *curr; - GCObject **lastnext = &g->tobefnz; - /* find last 'next' field in 'tobefnz' list (to add elements in its end) */ - while (*lastnext != NULL) - lastnext = &gch(*lastnext)->next; - while ((curr = *p) != NULL) { /* traverse all finalizable objects */ - lua_assert(!isfinalized(curr)); - lua_assert(testbit(gch(curr)->marked, SEPARATED)); - if (!(iswhite(curr) || all)) /* not being collected? */ - p = &gch(curr)->next; /* don't bother with it */ - else { - l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */ - *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */ - gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */ - *lastnext = curr; - lastnext = &gch(curr)->next; - } - } -} - - -/* -** if object 'o' has a finalizer, remove it from 'allgc' list (must -** search the list to find it) and link it in 'finobj' list. -*/ -void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { - global_State *g = G(L); - if (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */ - isfinalized(o) || /* ... or is finalized... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ - return; /* nothing to be done */ - else { /* move 'o' to 'finobj' list */ - GCObject **p; - GCheader *ho = gch(o); - if (g->sweepgc == &ho->next) { /* avoid removing current sweep object */ - lua_assert(issweepphase(g)); - g->sweepgc = sweeptolive(L, g->sweepgc, NULL); - } - /* search for pointer pointing to 'o' */ - for (p = &g->allgc; *p != o; p = &gch(*p)->next) { /* empty */ } - *p = ho->next; /* remove 'o' from root list */ - ho->next = g->finobj; /* link it in list 'finobj' */ - g->finobj = o; - l_setbit(ho->marked, SEPARATED); /* mark it as such */ - if (!keepinvariantout(g)) /* not keeping invariant? */ - makewhite(g, o); /* "sweep" object */ - else - resetoldbit(o); /* see MOVE OLD rule */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** GC control -** ======================================================= -*/ - - -/* -** set a reasonable "time" to wait before starting a new GC cycle; -** cycle will start when memory use hits threshold -*/ -static void setpause (global_State *g, l_mem estimate) { - l_mem debt, threshold; - estimate = estimate / PAUSEADJ; /* adjust 'estimate' */ - threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * g->gcpause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = -cast(l_mem, threshold - gettotalbytes(g)); - luaE_setdebt(g, debt); -} - - -#define sweepphases \ - (bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep)) - - -/* -** enter first sweep phase (strings) and prepare pointers for other -** sweep phases. The calls to 'sweeptolive' make pointers point to an -** object inside the list (instead of to the header), so that the real -** sweep do not need to skip objects created between "now" and the start -** of the real sweep. -** Returns how many objects it swept. -*/ -static int entersweep (lua_State *L) { - global_State *g = G(L); - int n = 0; - g->gcstate = GCSsweepstring; - lua_assert(g->sweepgc == NULL && g->sweepfin == NULL); - /* prepare to sweep strings, finalizable objects, and regular objects */ - g->sweepstrgc = 0; - g->sweepfin = sweeptolive(L, &g->finobj, &n); - g->sweepgc = sweeptolive(L, &g->allgc, &n); - return n; -} - - -/* -** change GC mode -*/ -void luaC_changemode (lua_State *L, int mode) { - global_State *g = G(L); - if (mode == g->gckind) return; /* nothing to change */ - if (mode == KGC_GEN) { /* change to generational mode */ - /* make sure gray lists are consistent */ - luaC_runtilstate(L, bitmask(GCSpropagate)); - g->GCestimate = gettotalbytes(g); - g->gckind = KGC_GEN; - } - else { /* change to incremental mode */ - /* sweep all objects to turn them back to white - (as white has not changed, nothing extra will be collected) */ - g->gckind = KGC_NORMAL; - entersweep(L); - luaC_runtilstate(L, ~sweepphases); - } -} - - -/* -** call all pending finalizers -*/ -static void callallpendingfinalizers (lua_State *L, int propagateerrors) { - global_State *g = G(L); - while (g->tobefnz) { - resetoldbit(g->tobefnz); - GCTM(L, propagateerrors); - } -} - - -void luaC_freeallobjects (lua_State *L) { - global_State *g = G(L); - int i; - separatetobefnz(L, 1); /* separate all objects with finalizers */ - lua_assert(g->finobj == NULL); - callallpendingfinalizers(L, 0); - g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ - g->gckind = KGC_NORMAL; - sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */ - sweepwholelist(L, &g->allgc); - for (i = 0; i < g->strt.size; i++) /* free all string lists */ - sweepwholelist(L, &g->strt.hash[i]); - lua_assert(g->strt.nuse == 0); -} - - -static l_mem atomic (lua_State *L) { - global_State *g = G(L); - l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */ - GCObject *origweak, *origall; - lua_assert(!iswhite(obj2gco(g->mainthread))); - markobject(g, L); /* mark running thread */ - /* registry and global metatables may be changed by API */ - markvalue(g, &g->l_registry); - markmt(g); /* mark basic metatables */ - /* remark occasional upvalues of (maybe) dead threads */ - remarkupvals(g); - propagateall(g); /* propagate changes */ - work += g->GCmemtrav; /* stop counting (do not (re)count grays) */ - /* traverse objects caught by write barrier and by 'remarkupvals' */ - retraversegrays(g); - work -= g->GCmemtrav; /* restart counting */ - convergeephemerons(g); - /* at this point, all strongly accessible objects are marked. */ - /* clear values from weak tables, before checking finalizers */ - clearvalues(g, g->weak, NULL); - clearvalues(g, g->allweak, NULL); - origweak = g->weak; origall = g->allweak; - work += g->GCmemtrav; /* stop counting (objects being finalized) */ - separatetobefnz(L, 0); /* separate objects to be finalized */ - markbeingfnz(g); /* mark objects that will be finalized */ - propagateall(g); /* remark, to propagate `preserveness' */ - work -= g->GCmemtrav; /* restart counting */ - convergeephemerons(g); - /* at this point, all resurrected objects are marked. */ - /* remove dead objects from weak tables */ - clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */ - clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */ - /* clear values from resurrected weak tables */ - clearvalues(g, g->weak, origweak); - clearvalues(g, g->allweak, origall); - g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ - work += g->GCmemtrav; /* complete counting */ - return work; /* estimate of memory marked by 'atomic' */ -} - - -static lu_mem singlestep (lua_State *L) { - global_State *g = G(L); - switch (g->gcstate) { - case GCSpause: { - /* start to count memory traversed */ - g->GCmemtrav = g->strt.size * sizeof(GCObject*); - lua_assert(!isgenerational(g)); - restartcollection(g); - g->gcstate = GCSpropagate; - return g->GCmemtrav; - } - case GCSpropagate: { - if (g->gray) { - lu_mem oldtrav = g->GCmemtrav; - propagatemark(g); - return g->GCmemtrav - oldtrav; /* memory traversed in this step */ - } - else { /* no more `gray' objects */ - lu_mem work; - int sw; - g->gcstate = GCSatomic; /* finish mark phase */ - g->GCestimate = g->GCmemtrav; /* save what was counted */; - work = atomic(L); /* add what was traversed by 'atomic' */ - g->GCestimate += work; /* estimate of total memory traversed */ - sw = entersweep(L); - return work + sw * GCSWEEPCOST; - } - } - case GCSsweepstring: { - int i; - for (i = 0; i < GCSWEEPMAX && g->sweepstrgc + i < g->strt.size; i++) - sweepwholelist(L, &g->strt.hash[g->sweepstrgc + i]); - g->sweepstrgc += i; - if (g->sweepstrgc >= g->strt.size) /* no more strings to sweep? */ - g->gcstate = GCSsweepudata; - return i * GCSWEEPCOST; - } - case GCSsweepudata: { - if (g->sweepfin) { - g->sweepfin = sweeplist(L, g->sweepfin, GCSWEEPMAX); - return GCSWEEPMAX*GCSWEEPCOST; - } - else { - g->gcstate = GCSsweep; - return 0; - } - } - case GCSsweep: { - if (g->sweepgc) { - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); - return GCSWEEPMAX*GCSWEEPCOST; - } - else { - /* sweep main thread */ - GCObject *mt = obj2gco(g->mainthread); - sweeplist(L, &mt, 1); - checkSizes(L); - g->gcstate = GCSpause; /* finish collection */ - return GCSWEEPCOST; - } - } - default: lua_assert(0); return 0; - } -} - - -/* -** advances the garbage collector until it reaches a state allowed -** by 'statemask' -*/ -void luaC_runtilstate (lua_State *L, int statesmask) { - global_State *g = G(L); - while (!testbit(statesmask, g->gcstate)) - singlestep(L); -} - - -static void generationalcollection (lua_State *L) { - global_State *g = G(L); - lua_assert(g->gcstate == GCSpropagate); - if (g->GCestimate == 0) { /* signal for another major collection? */ - luaC_fullgc(L, 0); /* perform a full regular collection */ - g->GCestimate = gettotalbytes(g); /* update control */ - } - else { - lu_mem estimate = g->GCestimate; - luaC_runtilstate(L, bitmask(GCSpause)); /* run complete (minor) cycle */ - g->gcstate = GCSpropagate; /* skip restart */ - if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc) - g->GCestimate = 0; /* signal for a major collection */ - else - g->GCestimate = estimate; /* keep estimate from last major coll. */ - - } - setpause(g, gettotalbytes(g)); - lua_assert(g->gcstate == GCSpropagate); -} - - -static void incstep (lua_State *L) { - global_State *g = G(L); - l_mem debt = g->GCdebt; - int stepmul = g->gcstepmul; - if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values (and 0) */ - /* convert debt from Kb to 'work units' (avoid zero debt and overflows) */ - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - do { /* always perform at least one single step */ - lu_mem work = singlestep(L); /* do some work */ - debt -= work; - } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); - if (g->gcstate == GCSpause) - setpause(g, g->GCestimate); /* pause until next cycle */ - else { - debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */ - luaE_setdebt(g, debt); - } -} - - -/* -** performs a basic GC step -*/ -void luaC_forcestep (lua_State *L) { - global_State *g = G(L); - int i; - if (isgenerational(g)) generationalcollection(L); - else incstep(L); - /* run a few finalizers (or all of them at the end of a collect cycle) */ - for (i = 0; g->tobefnz && (i < GCFINALIZENUM || g->gcstate == GCSpause); i++) - GCTM(L, 1); /* call one finalizer */ -} - - -/* -** performs a basic GC step only if collector is running -*/ -void luaC_step (lua_State *L) { - global_State *g = G(L); - if (g->gcrunning) luaC_forcestep(L); - else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */ -} - - - -/* -** performs a full GC cycle; if "isemergency", does not call -** finalizers (which could change stack positions) -*/ -void luaC_fullgc (lua_State *L, int isemergency) { - global_State *g = G(L); - int origkind = g->gckind; - lua_assert(origkind != KGC_EMERGENCY); - if (isemergency) /* do not run finalizers during emergency GC */ - g->gckind = KGC_EMERGENCY; - else { - g->gckind = KGC_NORMAL; - callallpendingfinalizers(L, 1); - } - if (keepinvariant(g)) { /* may there be some black objects? */ - /* must sweep all objects to turn them back to white - (as white has not changed, nothing will be collected) */ - entersweep(L); - } - /* finish any pending sweep phase to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpause)); - luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */ - luaC_runtilstate(L, bitmask(GCSpause)); /* run entire collection */ - if (origkind == KGC_GEN) { /* generational mode? */ - /* generational mode must be kept in propagate phase */ - luaC_runtilstate(L, bitmask(GCSpropagate)); - } - g->gckind = origkind; - setpause(g, gettotalbytes(g)); - if (!isemergency) /* do not run finalizers during emergency GC */ - callallpendingfinalizers(L, 1); -} - -/* }====================================================== */ - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.h deleted file mode 100644 index 84bb1cd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lgc.h +++ /dev/null @@ -1,157 +0,0 @@ -/* -** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#ifndef lgc_h -#define lgc_h - - -#include "lobject.h" -#include "lstate.h" - -/* -** Collectable objects may have one of three colors: white, which -** means the object is not marked; gray, which means the -** object is marked, but its references may be not marked; and -** black, which means that the object and all its references are marked. -** The main invariant of the garbage collector, while marking objects, -** is that a black object can never point to a white one. Moreover, -** any gray object must be in a "gray list" (gray, grayagain, weak, -** allweak, ephemeron) so that it can be visited again before finishing -** the collection cycle. These lists have no meaning when the invariant -** is not being enforced (e.g., sweep phase). -*/ - - - -/* how much to allocate before next GC step */ -#if !defined(GCSTEPSIZE) -/* ~100 small strings */ -#define GCSTEPSIZE (cast_int(100 * sizeof(TString))) -#endif - - -/* -** Possible states of the Garbage Collector -*/ -#define GCSpropagate 0 -#define GCSatomic 1 -#define GCSsweepstring 2 -#define GCSsweepudata 3 -#define GCSsweep 4 -#define GCSpause 5 - - -#define issweepphase(g) \ - (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) - -#define isgenerational(g) ((g)->gckind == KGC_GEN) - -/* -** macros to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a non-generational collection, the sweep -** phase may break the invariant, as objects turned white may point to -** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. During a generational collection, the -** invariant must be kept all times. -*/ - -#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) - - -/* -** Outside the collector, the state in generational mode is kept in -** 'propagate', so 'keepinvariant' is always true. -*/ -#define keepinvariantout(g) \ - check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \ - g->gcstate <= GCSatomic) - - -/* -** some useful bit tricks -*/ -#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) -#define setbits(x,m) ((x) |= (m)) -#define testbits(x,m) ((x) & (m)) -#define bitmask(b) (1<<(b)) -#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) -#define l_setbit(x,b) setbits(x, bitmask(b)) -#define resetbit(x,b) resetbits(x, bitmask(b)) -#define testbit(x,b) testbits(x, bitmask(b)) - - -/* Layout for bit use in `marked' field: */ -#define WHITE0BIT 0 /* object is white (type 0) */ -#define WHITE1BIT 1 /* object is white (type 1) */ -#define BLACKBIT 2 /* object is black */ -#define FINALIZEDBIT 3 /* object has been separated for finalization */ -#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ -#define FIXEDBIT 5 /* object is fixed (should not be collected) */ -#define OLDBIT 6 /* object is old (only in generational mode) */ -/* bit 7 is currently used by tests (luaL_checkmemory) */ - -#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) - - -#define iswhite(x) testbits((x)->gch.marked, WHITEBITS) -#define isblack(x) testbit((x)->gch.marked, BLACKBIT) -#define isgray(x) /* neither white nor black */ \ - (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) - -#define isold(x) testbit((x)->gch.marked, OLDBIT) - -/* MOVE OLD rule: whenever an object is moved to the beginning of - a GC list, its old bit must be cleared */ -#define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) - -#define otherwhite(g) (g->currentwhite ^ WHITEBITS) -#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) -#define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) - -#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) - -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) - -#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) - - -#define luaC_condGC(L,c) \ - {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} -#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) - - -#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)); } - -#define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ - luaC_barrierback_(L,p); } - -#define luaC_objbarrier(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ - luaC_barrier_(L,obj2gco(p),obj2gco(o)); } - -#define luaC_objbarrierback(L,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } - -#define luaC_barrierproto(L,p,c) \ - { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } - -LUAI_FUNC void luaC_freeallobjects (lua_State *L); -LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_forcestep (lua_State *L); -LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); -LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, - GCObject **list, int offset); -LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); -LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); -LUAI_FUNC void luaC_changemode (lua_State *L, int mode); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/linit.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/linit.c deleted file mode 100644 index c1a3830..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/linit.c +++ /dev/null @@ -1,67 +0,0 @@ -/* -** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ -** Initialization of libraries for lua.c and other clients -** See Copyright Notice in lua.h -*/ - - -/* -** If you embed Lua in your program and need to open the standard -** libraries, call luaL_openlibs in your program. If you need a -** different set of libraries, copy this file to your project and edit -** it to suit your needs. -*/ - - -#define linit_c -#define LUA_LIB - -#include "lua.h" - -#include "lualib.h" -#include "lauxlib.h" - - -/* -** these libs are loaded by lua.c and are readily available to any Lua -** program -*/ -static const luaL_Reg loadedlibs[] = { - {"_G", luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_COLIBNAME, luaopen_coroutine}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_BITLIBNAME, luaopen_bit32}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_DBLIBNAME, luaopen_debug}, - {NULL, NULL} -}; - - -/* -** these libs are preloaded and must be required before used -*/ -static const luaL_Reg preloadedlibs[] = { - {NULL, NULL} -}; - - -LUALIB_API void luaL_openlibs (lua_State *L) { - const luaL_Reg *lib; - /* call open functions from 'loadedlibs' and set results to global table */ - for (lib = loadedlibs; lib->func; lib++) { - luaL_requiref(L, lib->name, lib->func, 1); - lua_pop(L, 1); /* remove lib */ - } - /* add open functions from 'preloadedlibs' into 'package.preload' table */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); - for (lib = preloadedlibs; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); - } - lua_pop(L, 1); /* remove _PRELOAD table */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/liolib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/liolib.c deleted file mode 100644 index 2a4ec4a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/liolib.c +++ /dev/null @@ -1,666 +0,0 @@ -/* -** $Id: liolib.c,v 2.112.1.1 2013/04/12 18:48:47 roberto Exp $ -** Standard I/O (and system) library -** See Copyright Notice in lua.h -*/ - - -/* -** This definition must come before the inclusion of 'stdio.h'; it -** should not affect non-POSIX systems -*/ -#if !defined(_FILE_OFFSET_BITS) -#define _LARGEFILE_SOURCE 1 -#define _FILE_OFFSET_BITS 64 -#endif - - -#include -#include -#include -#include - -#define liolib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#if !defined(lua_checkmode) - -/* -** Check whether 'mode' matches '[rwa]%+?b?'. -** Change this macro to accept other modes for 'fopen' besides -** the standard ones. -*/ -#define lua_checkmode(mode) \ - (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ - (*mode != '+' || ++mode) && /* skip if char is '+' */ \ - (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ - (*mode == '\0')) - -#endif - -/* -** {====================================================== -** lua_popen spawns a new process connected to the current -** one through the file streams. -** ======================================================= -*/ - -#if !defined(lua_popen) /* { */ - -#if defined(LUA_USE_POPEN) /* { */ - -#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) -#define lua_pclose(L,file) ((void)L, pclose(file)) - -#elif defined(LUA_WIN) /* }{ */ - -#define lua_popen(L,c,m) ((void)L, _popen(c,m)) -#define lua_pclose(L,file) ((void)L, _pclose(file)) - - -#else /* }{ */ - -#define lua_popen(L,c,m) ((void)((void)c, m), \ - luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) -#define lua_pclose(L,file) ((void)((void)L, file), -1) - - -#endif /* } */ - -#endif /* } */ - -/* }====================================================== */ - - -/* -** {====================================================== -** lua_fseek: configuration for longer offsets -** ======================================================= -*/ - -#if !defined(lua_fseek) && !defined(LUA_ANSI) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#define l_fseek(f,o,w) fseeko(f,o,w) -#define l_ftell(f) ftello(f) -#define l_seeknum off_t - -#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \ - && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ -/* Windows (but not DDK) and Visual C++ 2005 or higher */ - -#define l_fseek(f,o,w) _fseeki64(f,o,w) -#define l_ftell(f) _ftelli64(f) -#define l_seeknum __int64 - -#endif /* } */ - -#endif /* } */ - - -#if !defined(l_fseek) /* default definitions */ -#define l_fseek(f,o,w) fseek(f,o,w) -#define l_ftell(f) ftell(f) -#define l_seeknum long -#endif - -/* }====================================================== */ - - -#define IO_PREFIX "_IO_" -#define IO_INPUT (IO_PREFIX "input") -#define IO_OUTPUT (IO_PREFIX "output") - - -typedef luaL_Stream LStream; - - -#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) - -#define isclosed(p) ((p)->closef == NULL) - - -static int io_type (lua_State *L) { - LStream *p; - luaL_checkany(L, 1); - p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); - if (p == NULL) - lua_pushnil(L); /* not a file */ - else if (isclosed(p)) - lua_pushliteral(L, "closed file"); - else - lua_pushliteral(L, "file"); - return 1; -} - - -static int f_tostring (lua_State *L) { - LStream *p = tolstream(L); - if (isclosed(p)) - lua_pushliteral(L, "file (closed)"); - else - lua_pushfstring(L, "file (%p)", p->f); - return 1; -} - - -static FILE *tofile (lua_State *L) { - LStream *p = tolstream(L); - if (isclosed(p)) - luaL_error(L, "attempt to use a closed file"); - lua_assert(p->f); - return p->f; -} - - -/* -** When creating file handles, always creates a `closed' file handle -** before opening the actual file; so, if there is a memory error, the -** file is not left opened. -*/ -static LStream *newprefile (lua_State *L) { - LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); - p->closef = NULL; /* mark file handle as 'closed' */ - luaL_setmetatable(L, LUA_FILEHANDLE); - return p; -} - - -static int aux_close (lua_State *L) { - LStream *p = tolstream(L); - lua_CFunction cf = p->closef; - p->closef = NULL; /* mark stream as closed */ - return (*cf)(L); /* close it */ -} - - -static int io_close (lua_State *L) { - if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); -} - - -static int f_gc (lua_State *L) { - LStream *p = tolstream(L); - if (!isclosed(p) && p->f != NULL) - aux_close(L); /* ignore closed and incompletely open files */ - return 0; -} - - -/* -** function to close regular files -*/ -static int io_fclose (lua_State *L) { - LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); -} - - -static LStream *newfile (lua_State *L) { - LStream *p = newprefile(L); - p->f = NULL; - p->closef = &io_fclose; - return p; -} - - -static void opencheck (lua_State *L, const char *fname, const char *mode) { - LStream *p = newfile(L); - p->f = fopen(fname, mode); - if (p->f == NULL) - luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno)); -} - - -static int io_open (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newfile(L); - const char *md = mode; /* to traverse/check mode */ - luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); - p->f = fopen(filename, mode); - return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; -} - - -/* -** function to close 'popen' files -*/ -static int io_pclose (lua_State *L) { - LStream *p = tolstream(L); - return luaL_execresult(L, lua_pclose(L, p->f)); -} - - -static int io_popen (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newprefile(L); - p->f = lua_popen(L, filename, mode); - p->closef = &io_pclose; - return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; -} - - -static int io_tmpfile (lua_State *L) { - LStream *p = newfile(L); - p->f = tmpfile(); - return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; -} - - -static FILE *getiofile (lua_State *L, const char *findex) { - LStream *p; - lua_getfield(L, LUA_REGISTRYINDEX, findex); - p = (LStream *)lua_touserdata(L, -1); - if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX)); - return p->f; -} - - -static int g_iofile (lua_State *L, const char *f, const char *mode) { - if (!lua_isnoneornil(L, 1)) { - const char *filename = lua_tostring(L, 1); - if (filename) - opencheck(L, filename, mode); - else { - tofile(L); /* check that it's a valid file handle */ - lua_pushvalue(L, 1); - } - lua_setfield(L, LUA_REGISTRYINDEX, f); - } - /* return current value */ - lua_getfield(L, LUA_REGISTRYINDEX, f); - return 1; -} - - -static int io_input (lua_State *L) { - return g_iofile(L, IO_INPUT, "r"); -} - - -static int io_output (lua_State *L) { - return g_iofile(L, IO_OUTPUT, "w"); -} - - -static int io_readline (lua_State *L); - - -static void aux_lines (lua_State *L, int toclose) { - int i; - int n = lua_gettop(L) - 1; /* number of arguments to read */ - /* ensure that arguments will fit here and into 'io_readline' stack */ - luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); - lua_pushvalue(L, 1); /* file handle */ - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ - lua_pushcclosure(L, io_readline, 3 + n); -} - - -static int f_lines (lua_State *L) { - tofile(L); /* check that it's a valid file handle */ - aux_lines(L, 0); - return 1; -} - - -static int io_lines (lua_State *L) { - int toclose; - if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ - if (lua_isnil(L, 1)) { /* no file name? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ - lua_replace(L, 1); /* put it at index 1 */ - tofile(L); /* check that it's a valid file handle */ - toclose = 0; /* do not close it after iteration */ - } - else { /* open a new file */ - const char *filename = luaL_checkstring(L, 1); - opencheck(L, filename, "r"); - lua_replace(L, 1); /* put file at index 1 */ - toclose = 1; /* close it after iteration */ - } - aux_lines(L, toclose); - return 1; -} - - -/* -** {====================================================== -** READ -** ======================================================= -*/ - - -static int read_number (lua_State *L, FILE *f) { - lua_Number d; - if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { - lua_pushnumber(L, d); - return 1; - } - else { - lua_pushnil(L); /* "result" to be removed */ - return 0; /* read fails */ - } -} - - -static int test_eof (lua_State *L, FILE *f) { - int c = getc(f); - ungetc(c, f); - lua_pushlstring(L, NULL, 0); - return (c != EOF); -} - - -static int read_line (lua_State *L, FILE *f, int chop) { - luaL_Buffer b; - luaL_buffinit(L, &b); - for (;;) { - size_t l; - char *p = luaL_prepbuffer(&b); - if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ - luaL_pushresult(&b); /* close buffer */ - return (lua_rawlen(L, -1) > 0); /* check whether read something */ - } - l = strlen(p); - if (l == 0 || p[l-1] != '\n') - luaL_addsize(&b, l); - else { - luaL_addsize(&b, l - chop); /* chop 'eol' if needed */ - luaL_pushresult(&b); /* close buffer */ - return 1; /* read at least an `eol' */ - } - } -} - - -#define MAX_SIZE_T (~(size_t)0) - -static void read_all (lua_State *L, FILE *f) { - size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ - luaL_Buffer b; - luaL_buffinit(L, &b); - for (;;) { - char *p = luaL_prepbuffsize(&b, rlen); - size_t nr = fread(p, sizeof(char), rlen, f); - luaL_addsize(&b, nr); - if (nr < rlen) break; /* eof? */ - else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ - rlen *= 2; /* double buffer size at each iteration */ - } - luaL_pushresult(&b); /* close buffer */ -} - - -static int read_chars (lua_State *L, FILE *f, size_t n) { - size_t nr; /* number of chars actually read */ - char *p; - luaL_Buffer b; - luaL_buffinit(L, &b); - p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ - nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ - luaL_addsize(&b, nr); - luaL_pushresult(&b); /* close buffer */ - return (nr > 0); /* true iff read something */ -} - - -static int g_read (lua_State *L, FILE *f, int first) { - int nargs = lua_gettop(L) - 1; - int success; - int n; - clearerr(f); - if (nargs == 0) { /* no arguments? */ - success = read_line(L, f, 1); - n = first+1; /* to return 1 result */ - } - else { /* ensure stack space for all results and for auxlib's buffer */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - success = 1; - for (n = first; nargs-- && success; n++) { - if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)lua_tointeger(L, n); - success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); - } - else { - const char *p = lua_tostring(L, n); - luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); - switch (p[1]) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f, 1); - break; - case 'L': /* line with end-of-line */ - success = read_line(L, f, 0); - break; - case 'a': /* file */ - read_all(L, f); /* read entire file */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, n, "invalid format"); - } - } - } - } - if (ferror(f)) - return luaL_fileresult(L, 0, NULL); - if (!success) { - lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ - } - return n - first; -} - - -static int io_read (lua_State *L) { - return g_read(L, getiofile(L, IO_INPUT), 1); -} - - -static int f_read (lua_State *L) { - return g_read(L, tofile(L), 2); -} - - -static int io_readline (lua_State *L) { - LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); - int i; - int n = (int)lua_tointeger(L, lua_upvalueindex(2)); - if (isclosed(p)) /* file is already closed? */ - return luaL_error(L, "file is already closed"); - lua_settop(L , 1); - for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ - lua_pushvalue(L, lua_upvalueindex(3 + i)); - n = g_read(L, p->f, 2); /* 'n' is number of results */ - lua_assert(n > 0); /* should return at least a nil */ - if (!lua_isnil(L, -n)) /* read at least one value? */ - return n; /* return them */ - else { /* first result is nil: EOF or error */ - if (n > 1) { /* is there error information? */ - /* 2nd result is error message */ - return luaL_error(L, "%s", lua_tostring(L, -n + 1)); - } - if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); - aux_close(L); /* close it */ - } - return 0; - } -} - -/* }====================================================== */ - - -static int g_write (lua_State *L, FILE *f, int arg) { - int nargs = lua_gettop(L) - arg; - int status = 1; - for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - status = status && - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; - } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); - } - } - if (status) return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); -} - - -static int io_write (lua_State *L) { - return g_write(L, getiofile(L, IO_OUTPUT), 1); -} - - -static int f_write (lua_State *L) { - FILE *f = tofile(L); - lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ - return g_write(L, f, 2); -} - - -static int f_seek (lua_State *L) { - static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; - static const char *const modenames[] = {"set", "cur", "end", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Number p3 = luaL_optnumber(L, 3, 0); - l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Number)offset == p3, 3, - "not an integer in proper range"); - op = l_fseek(f, offset, mode[op]); - if (op) - return luaL_fileresult(L, 0, NULL); /* error */ - else { - lua_pushnumber(L, (lua_Number)l_ftell(f)); - return 1; - } -} - - -static int f_setvbuf (lua_State *L) { - static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; - static const char *const modenames[] = {"no", "full", "line", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, NULL, modenames); - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], sz); - return luaL_fileresult(L, res == 0, NULL); -} - - - -static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); -} - - -static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); -} - - -/* -** functions for 'io' library -*/ -static const luaL_Reg iolib[] = { - {"close", io_close}, - {"flush", io_flush}, - {"input", io_input}, - {"lines", io_lines}, - {"open", io_open}, - {"output", io_output}, - {"popen", io_popen}, - {"read", io_read}, - {"tmpfile", io_tmpfile}, - {"type", io_type}, - {"write", io_write}, - {NULL, NULL} -}; - - -/* -** methods for file handles -*/ -static const luaL_Reg flib[] = { - {"close", io_close}, - {"flush", f_flush}, - {"lines", f_lines}, - {"read", f_read}, - {"seek", f_seek}, - {"setvbuf", f_setvbuf}, - {"write", f_write}, - {"__gc", f_gc}, - {"__tostring", f_tostring}, - {NULL, NULL} -}; - - -static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ - lua_pop(L, 1); /* pop new metatable */ -} - - -/* -** function to (not) close the standard files stdin, stdout, and stderr -*/ -static int io_noclose (lua_State *L) { - LStream *p = tolstream(L); - p->closef = &io_noclose; /* keep file opened */ - lua_pushnil(L); - lua_pushliteral(L, "cannot close standard file"); - return 2; -} - - -static void createstdfile (lua_State *L, FILE *f, const char *k, - const char *fname) { - LStream *p = newprefile(L); - p->f = f; - p->closef = &io_noclose; - if (k != NULL) { - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ - } - lua_setfield(L, -2, fname); /* add file to module */ -} - - -LUAMOD_API int luaopen_io (lua_State *L) { - luaL_newlib(L, iolib); /* new module */ - createmeta(L); - /* create (and set) default files */ - createstdfile(L, stdin, IO_INPUT, "stdin"); - createstdfile(L, stdout, IO_OUTPUT, "stdout"); - createstdfile(L, stderr, NULL, "stderr"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.c deleted file mode 100644 index 32cdcf1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.c +++ /dev/null @@ -1,530 +0,0 @@ -/* -** $Id: llex.c,v 2.63.1.3 2015/02/09 17:56:34 roberto Exp $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - - -#include -#include - -#define llex_c -#define LUA_CORE - -#include "lua.h" - -#include "lctype.h" -#include "ldo.h" -#include "llex.h" -#include "lobject.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lzio.h" - - - -#define next(ls) (ls->current = zgetc(ls->z)) - - - -#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') - - -/* ORDER RESERVED */ -static const char *const luaX_tokens [] = { - "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", "goto", "if", - "in", "local", "nil", "not", "or", "repeat", - "return", "then", "true", "until", "while", - "..", "...", "==", ">=", "<=", "~=", "::", "", - "", "", "" -}; - - -#define save_and_next(ls) (save(ls, ls->current), next(ls)) - - -static l_noret lexerror (LexState *ls, const char *msg, int token); - - -static void save (LexState *ls, int c) { - Mbuffer *b = ls->buff; - if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { - size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZET/2) - lexerror(ls, "lexical element too long", 0); - newsize = luaZ_sizebuffer(b) * 2; - luaZ_resizebuffer(ls->L, b, newsize); - } - b->buffer[luaZ_bufflen(b)++] = cast(char, c); -} - - -void luaX_init (lua_State *L) { - int i; - for (i=0; itsv.extra = cast_byte(i+1); /* reserved word */ - } -} - - -const char *luaX_token2str (LexState *ls, int token) { - if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast(unsigned char, token)); - return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) : - luaO_pushfstring(ls->L, "char(%d)", token); - } - else { - const char *s = luaX_tokens[token - FIRST_RESERVED]; - if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ - return luaO_pushfstring(ls->L, LUA_QS, s); - else /* names, strings, and numerals */ - return s; - } -} - - -static const char *txtToken (LexState *ls, int token) { - switch (token) { - case TK_NAME: - case TK_STRING: - case TK_NUMBER: - save(ls, '\0'); - return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); - default: - return luaX_token2str(ls, token); - } -} - - -static l_noret lexerror (LexState *ls, const char *msg, int token) { - char buff[LUA_IDSIZE]; - luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE); - msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); - if (token) - luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); - luaD_throw(ls->L, LUA_ERRSYNTAX); -} - - -l_noret luaX_syntaxerror (LexState *ls, const char *msg) { - lexerror(ls, msg, ls->t.token); -} - - -/* -** creates a new string and anchors it in function's table so that -** it will not be collected until the end of the function's compilation -** (by that time it should be anchored in function's prototype) -*/ -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { - lua_State *L = ls->L; - TValue *o; /* entry for `str' */ - TString *ts = luaS_newlstr(L, str, l); /* create new string */ - setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->fs->h, L->top - 1); - if (ttisnil(o)) { /* not in use yet? (see 'addK') */ - /* boolean value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ - setbvalue(o, 1); /* t[string] = true */ - luaC_checkGC(L); - } - else { /* string already present */ - ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ - } - L->top--; /* remove string from stack */ - return ts; -} - - -/* -** increment line number and skips newline sequence (any of -** \n, \r, \n\r, or \r\n) -*/ -static void inclinenumber (LexState *ls) { - int old = ls->current; - lua_assert(currIsNewline(ls)); - next(ls); /* skip `\n' or `\r' */ - if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip `\n\r' or `\r\n' */ - if (++ls->linenumber >= MAX_INT) - lexerror(ls, "chunk has too many lines", 0); -} - - -void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, - int firstchar) { - ls->decpoint = '.'; - ls->L = L; - ls->current = firstchar; - ls->lookahead.token = TK_EOS; /* no look-ahead token */ - ls->z = z; - ls->fs = NULL; - ls->linenumber = 1; - ls->lastline = 1; - ls->source = source; - ls->envn = luaS_new(L, LUA_ENV); /* create env name */ - luaS_fix(ls->envn); /* never collect this name */ - luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ -} - - - -/* -** ======================================================= -** LEXICAL ANALYZER -** ======================================================= -*/ - - - -static int check_next (LexState *ls, const char *set) { - if (ls->current == '\0' || !strchr(set, ls->current)) - return 0; - save_and_next(ls); - return 1; -} - - -/* -** change all characters 'from' in buffer to 'to' -*/ -static void buffreplace (LexState *ls, char from, char to) { - size_t n = luaZ_bufflen(ls->buff); - char *p = luaZ_buffer(ls->buff); - while (n--) - if (p[n] == from) p[n] = to; -} - - -#if !defined(getlocaledecpoint) -#define getlocaledecpoint() (localeconv()->decimal_point[0]) -#endif - - -#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e) - -/* -** in case of format error, try to change decimal point separator to -** the one defined in the current locale and check again -*/ -static void trydecpoint (LexState *ls, SemInfo *seminfo) { - char old = ls->decpoint; - ls->decpoint = getlocaledecpoint(); - buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ - if (!buff2d(ls->buff, &seminfo->r)) { - /* format error with correct decimal point: no more options */ - buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ - lexerror(ls, "malformed number", TK_NUMBER); - } -} - - -/* LUA_NUMBER */ -/* -** this function is quite liberal in what it accepts, as 'luaO_str2d' -** will reject ill-formed numerals. -*/ -static void read_numeral (LexState *ls, SemInfo *seminfo) { - const char *expo = "Ee"; - int first = ls->current; - lua_assert(lisdigit(ls->current)); - save_and_next(ls); - if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ - expo = "Pp"; - for (;;) { - if (check_next(ls, expo)) /* exponent part? */ - check_next(ls, "+-"); /* optional exponent sign */ - if (lisxdigit(ls->current) || ls->current == '.') - save_and_next(ls); - else break; - } - save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ - trydecpoint(ls, seminfo); /* try to update decimal point separator */ -} - - -/* -** skip a sequence '[=*[' or ']=*]' and return its number of '='s or -** -1 if sequence is malformed -*/ -static int skip_sep (LexState *ls) { - int count = 0; - int s = ls->current; - lua_assert(s == '[' || s == ']'); - save_and_next(ls); - while (ls->current == '=') { - save_and_next(ls); - count++; - } - return (ls->current == s) ? count : (-count) - 1; -} - - -static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { - save_and_next(ls); /* skip 2nd `[' */ - if (currIsNewline(ls)) /* string starts with a newline? */ - inclinenumber(ls); /* skip it */ - for (;;) { - switch (ls->current) { - case EOZ: - lexerror(ls, (seminfo) ? "unfinished long string" : - "unfinished long comment", TK_EOS); - break; /* to avoid warnings */ - case ']': { - if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd `]' */ - goto endloop; - } - break; - } - case '\n': case '\r': { - save(ls, '\n'); - inclinenumber(ls); - if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ - break; - } - default: { - if (seminfo) save_and_next(ls); - else next(ls); - } - } - } endloop: - if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), - luaZ_bufflen(ls->buff) - 2*(2 + sep)); -} - - -static void escerror (LexState *ls, int *c, int n, const char *msg) { - int i; - luaZ_resetbuffer(ls->buff); /* prepare error message */ - save(ls, '\\'); - for (i = 0; i < n && c[i] != EOZ; i++) - save(ls, c[i]); - lexerror(ls, msg, TK_STRING); -} - - -static int readhexaesc (LexState *ls) { - int c[3], i; /* keep input for error message */ - int r = 0; /* result accumulator */ - c[0] = 'x'; /* for error message */ - for (i = 1; i < 3; i++) { /* read two hexadecimal digits */ - c[i] = next(ls); - if (!lisxdigit(c[i])) - escerror(ls, c, i + 1, "hexadecimal digit expected"); - r = (r << 4) + luaO_hexavalue(c[i]); - } - return r; -} - - -static int readdecesc (LexState *ls) { - int c[3], i; - int r = 0; /* result accumulator */ - for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ - c[i] = ls->current; - r = 10*r + c[i] - '0'; - next(ls); - } - if (r > UCHAR_MAX) - escerror(ls, c, i, "decimal escape too large"); - return r; -} - - -static void read_string (LexState *ls, int del, SemInfo *seminfo) { - save_and_next(ls); /* keep delimiter (for error messages) */ - while (ls->current != del) { - switch (ls->current) { - case EOZ: - lexerror(ls, "unfinished string", TK_EOS); - break; /* to avoid warnings */ - case '\n': - case '\r': - lexerror(ls, "unfinished string", TK_STRING); - break; /* to avoid warnings */ - case '\\': { /* escape sequences */ - int c; /* final character to be saved */ - next(ls); /* do not save the `\' */ - switch (ls->current) { - case 'a': c = '\a'; goto read_save; - case 'b': c = '\b'; goto read_save; - case 'f': c = '\f'; goto read_save; - case 'n': c = '\n'; goto read_save; - case 'r': c = '\r'; goto read_save; - case 't': c = '\t'; goto read_save; - case 'v': c = '\v'; goto read_save; - case 'x': c = readhexaesc(ls); goto read_save; - case '\n': case '\r': - inclinenumber(ls); c = '\n'; goto only_save; - case '\\': case '\"': case '\'': - c = ls->current; goto read_save; - case EOZ: goto no_save; /* will raise an error next loop */ - case 'z': { /* zap following span of spaces */ - next(ls); /* skip the 'z' */ - while (lisspace(ls->current)) { - if (currIsNewline(ls)) inclinenumber(ls); - else next(ls); - } - goto no_save; - } - default: { - if (!lisdigit(ls->current)) - escerror(ls, &ls->current, 1, "invalid escape sequence"); - /* digital escape \ddd */ - c = readdecesc(ls); - goto only_save; - } - } - read_save: next(ls); /* read next character */ - only_save: save(ls, c); /* save 'c' */ - no_save: break; - } - default: - save_and_next(ls); - } - } - save_and_next(ls); /* skip delimiter */ - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, - luaZ_bufflen(ls->buff) - 2); -} - - -static int llex (LexState *ls, SemInfo *seminfo) { - luaZ_resetbuffer(ls->buff); - for (;;) { - switch (ls->current) { - case '\n': case '\r': { /* line breaks */ - inclinenumber(ls); - break; - } - case ' ': case '\f': case '\t': case '\v': { /* spaces */ - next(ls); - break; - } - case '-': { /* '-' or '--' (comment) */ - next(ls); - if (ls->current != '-') return '-'; - /* else is a comment */ - next(ls); - if (ls->current == '[') { /* long comment? */ - int sep = skip_sep(ls); - luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ - if (sep >= 0) { - read_long_string(ls, NULL, sep); /* skip long comment */ - luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ - break; - } - } - /* else short comment */ - while (!currIsNewline(ls) && ls->current != EOZ) - next(ls); /* skip until end of line (or end of file) */ - break; - } - case '[': { /* long string or simply '[' */ - int sep = skip_sep(ls); - if (sep >= 0) { - read_long_string(ls, seminfo, sep); - return TK_STRING; - } - else if (sep == -1) return '['; - else lexerror(ls, "invalid long string delimiter", TK_STRING); - } - case '=': { - next(ls); - if (ls->current != '=') return '='; - else { next(ls); return TK_EQ; } - } - case '<': { - next(ls); - if (ls->current != '=') return '<'; - else { next(ls); return TK_LE; } - } - case '>': { - next(ls); - if (ls->current != '=') return '>'; - else { next(ls); return TK_GE; } - } - case '~': { - next(ls); - if (ls->current != '=') return '~'; - else { next(ls); return TK_NE; } - } - case ':': { - next(ls); - if (ls->current != ':') return ':'; - else { next(ls); return TK_DBCOLON; } - } - case '"': case '\'': { /* short literal strings */ - read_string(ls, ls->current, seminfo); - return TK_STRING; - } - case '.': { /* '.', '..', '...', or number */ - save_and_next(ls); - if (check_next(ls, ".")) { - if (check_next(ls, ".")) - return TK_DOTS; /* '...' */ - else return TK_CONCAT; /* '..' */ - } - else if (!lisdigit(ls->current)) return '.'; - /* else go through */ - } - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': { - read_numeral(ls, seminfo); - return TK_NUMBER; - } - case EOZ: { - return TK_EOS; - } - default: { - if (lislalpha(ls->current)) { /* identifier or reserved word? */ - TString *ts; - do { - save_and_next(ls); - } while (lislalnum(ls->current)); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - seminfo->ts = ts; - if (isreserved(ts)) /* reserved word? */ - return ts->tsv.extra - 1 + FIRST_RESERVED; - else { - return TK_NAME; - } - } - else { /* single-char tokens (+ - / ...) */ - int c = ls->current; - next(ls); - return c; - } - } - } - } -} - - -void luaX_next (LexState *ls) { - ls->lastline = ls->linenumber; - if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ - ls->t = ls->lookahead; /* use this one */ - ls->lookahead.token = TK_EOS; /* and discharge it */ - } - else - ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ -} - - -int luaX_lookahead (LexState *ls) { - lua_assert(ls->lookahead.token == TK_EOS); - ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); - return ls->lookahead.token; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.h deleted file mode 100644 index a4acdd3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llex.h +++ /dev/null @@ -1,78 +0,0 @@ -/* -** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - -#ifndef llex_h -#define llex_h - -#include "lobject.h" -#include "lzio.h" - - -#define FIRST_RESERVED 257 - - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER RESERVED" -*/ -enum RESERVED { - /* terminal symbols denoted by reserved words */ - TK_AND = FIRST_RESERVED, TK_BREAK, - TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, - TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, - /* other terminal symbols */ - TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, - TK_NUMBER, TK_NAME, TK_STRING -}; - -/* number of reserved words */ -#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) - - -typedef union { - lua_Number r; - TString *ts; -} SemInfo; /* semantics information */ - - -typedef struct Token { - int token; - SemInfo seminfo; -} Token; - - -/* state of the lexer plus state of the parser when shared by all - functions */ -typedef struct LexState { - int current; /* current character (charint) */ - int linenumber; /* input line counter */ - int lastline; /* line of last token `consumed' */ - Token t; /* current token */ - Token lookahead; /* look ahead token */ - struct FuncState *fs; /* current function (parser) */ - struct lua_State *L; - ZIO *z; /* input stream */ - Mbuffer *buff; /* buffer for tokens */ - struct Dyndata *dyd; /* dynamic structures used by the parser */ - TString *source; /* current source name */ - TString *envn; /* environment variable name */ - char decpoint; /* locale decimal point */ -} LexState; - - -LUAI_FUNC void luaX_init (lua_State *L); -LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, - TString *source, int firstchar); -LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); -LUAI_FUNC void luaX_next (LexState *ls); -LUAI_FUNC int luaX_lookahead (LexState *ls); -LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); -LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llimits.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llimits.h deleted file mode 100644 index 152dd05..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/llimits.h +++ /dev/null @@ -1,309 +0,0 @@ -/* -** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $ -** Limits, basic types, and some other `installation-dependent' definitions -** See Copyright Notice in lua.h -*/ - -#ifndef llimits_h -#define llimits_h - - -#include -#include - - -#include "lua.h" - - -typedef unsigned LUA_INT32 lu_int32; - -typedef LUAI_UMEM lu_mem; - -typedef LUAI_MEM l_mem; - - - -/* chars used as small naturals (so that `char' is reserved for characters) */ -typedef unsigned char lu_byte; - - -#define MAX_SIZET ((size_t)(~(size_t)0)-2) - -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) - -#define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) - - -#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ - -/* -** conversion of pointer to integer -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value -*/ -#define IntPoint(p) ((unsigned int)(lu_mem)(p)) - - - -/* type to ensure maximum alignment */ -#if !defined(LUAI_USER_ALIGNMENT_T) -#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } -#endif - -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; - - -/* result of a `usual argument conversion' over lua_Number */ -typedef LUAI_UACNUMBER l_uacNumber; - - -/* internal assertions for in-house debugging */ -#if defined(lua_assert) -#define check_exp(c,e) (lua_assert(c), (e)) -/* to avoid problems with conditions too long */ -#define lua_longassert(c) { if (!(c)) lua_assert(0); } -#else -#define lua_assert(c) ((void)0) -#define check_exp(c,e) (e) -#define lua_longassert(c) ((void)0) -#endif - -/* -** assertion for checking API calls -*/ -#if !defined(luai_apicheck) - -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(L,e) assert(e) -#else -#define luai_apicheck(L,e) lua_assert(e) -#endif - -#endif - -#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) - - -#if !defined(UNUSED) -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ -#endif - - -#define cast(t, exp) ((t)(exp)) - -#define cast_byte(i) cast(lu_byte, (i)) -#define cast_num(i) cast(lua_Number, (i)) -#define cast_int(i) cast(int, (i)) -#define cast_uchar(i) cast(unsigned char, (i)) - - -/* -** non-return type -*/ -#if defined(__GNUC__) -#define l_noret void __attribute__((noreturn)) -#elif defined(_MSC_VER) -#define l_noret void __declspec(noreturn) -#else -#define l_noret void -#endif - - - -/* -** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 -#endif - -/* -** maximum number of upvalues in a closure (both C and Lua). (Value -** must fit in an unsigned char.) -*/ -#define MAXUPVAL UCHAR_MAX - - -/* -** type for virtual-machine instructions -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) -*/ -typedef lu_int32 Instruction; - - - -/* maximum stack for a Lua function */ -#define MAXSTACK 250 - - - -/* minimum size for the string table (must be power of 2) */ -#if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 32 -#endif - - -/* minimum size for string buffer */ -#if !defined(LUA_MINBUFFER) -#define LUA_MINBUFFER 32 -#endif - - -#if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif - -#if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} -#endif - - -/* -** these macros allow user-specific actions on threads when you defined -** LUAI_EXTRASPACE and need to do something extra when a thread is -** created/deleted/resumed/yielded. -*/ -#if !defined(luai_userstateopen) -#define luai_userstateopen(L) ((void)L) -#endif - -#if !defined(luai_userstateclose) -#define luai_userstateclose(L) ((void)L) -#endif - -#if !defined(luai_userstatethread) -#define luai_userstatethread(L,L1) ((void)L) -#endif - -#if !defined(luai_userstatefree) -#define luai_userstatefree(L,L1) ((void)L) -#endif - -#if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) -#endif - -#if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) -#endif - -/* -** lua_number2int is a macro to convert lua_Number to int. -** lua_number2integer is a macro to convert lua_Number to lua_Integer. -** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. -** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. -** luai_hashnum is a macro to hash a lua_Number value into an integer. -** The hash must be deterministic and give reasonable values for -** both small and large values (outside the range of integers). -*/ - -#if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ -/* trick with Microsoft assembler for X86 */ - -#define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} -#define lua_number2integer(i,n) lua_number2int(i, n) -#define lua_number2unsigned(i,n) \ - {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} - - -#elif defined(LUA_IEEE754TRICK) /* }{ */ -/* the next trick should work on any machine using IEEE754 with - a 32-bit int type */ - -union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; - -#if !defined(LUA_IEEEENDIAN) /* { */ -#define LUAI_EXTRAIEEE \ - static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; -#define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) -#else -#define LUA_IEEEENDIANLOC LUA_IEEEENDIAN -#define LUAI_EXTRAIEEE /* empty */ -#endif /* } */ - -#define lua_number2int32(i,n,t) \ - { LUAI_EXTRAIEEE \ - volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ - (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } - -#define luai_hashnum(i,n) \ - { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ - (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ - -#define lua_number2int(i,n) lua_number2int32(i, n, int) -#define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) - -/* the trick can be expanded to lua_Integer when it is a 32-bit value */ -#if defined(LUA_IEEELL) -#define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) -#endif - -#endif /* } */ - - -/* the following definitions always work, but may be slow */ - -#if !defined(lua_number2int) -#define lua_number2int(i,n) ((i)=(int)(n)) -#endif - -#if !defined(lua_number2integer) -#define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) -#endif - -#if !defined(lua_number2unsigned) /* { */ -/* the following definition assures proper modulo behavior */ -#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) -#include -#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) -#define lua_number2unsigned(i,n) \ - ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) -#else -#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) -#endif -#endif /* } */ - - -#if !defined(lua_unsigned2number) -/* on several machines, coercion from unsigned to double is slow, - so it may be worth to avoid */ -#define lua_unsigned2number(u) \ - (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) -#endif - - - -#if defined(ltable_c) && !defined(luai_hashnum) - -#include -#include - -#define luai_hashnum(i,n) { int e; \ - n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ - lua_number2int(i, n); i += e; } - -#endif - - - -/* -** macro to control inclusion of some hard tests on stack reallocation -*/ -#if !defined(HARDSTACKTESTS) -#define condmovestack(L) ((void)0) -#else -/* realloc stack keeping its size */ -#define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) -#endif - -#if !defined(HARDMEMTESTS) -#define condchangemem(L) condmovestack(L) -#else -#define condchangemem(L) \ - ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmathlib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmathlib.c deleted file mode 100644 index fe9fc54..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmathlib.c +++ /dev/null @@ -1,279 +0,0 @@ -/* -** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $ -** Standard mathematical library -** See Copyright Notice in lua.h -*/ - - -#include -#include - -#define lmathlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#undef PI -#define PI ((lua_Number)(3.1415926535897932384626433832795)) -#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) - - - -static int math_abs (lua_State *L) { - lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sin (lua_State *L) { - lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sinh (lua_State *L) { - lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_cos (lua_State *L) { - lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_cosh (lua_State *L) { - lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tan (lua_State *L) { - lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tanh (lua_State *L) { - lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_asin (lua_State *L) { - lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_acos (lua_State *L) { - lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_atan (lua_State *L) { - lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_atan2 (lua_State *L) { - lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); - return 1; -} - -static int math_ceil (lua_State *L) { - lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_floor (lua_State *L) { - lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_fmod (lua_State *L) { - lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); - return 1; -} - -static int math_modf (lua_State *L) { - lua_Number ip; - lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); - lua_pushnumber(L, ip); - lua_pushnumber(L, fp); - return 2; -} - -static int math_sqrt (lua_State *L) { - lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_pow (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number y = luaL_checknumber(L, 2); - lua_pushnumber(L, l_mathop(pow)(x, y)); - return 1; -} - -static int math_log (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number res; - if (lua_isnoneornil(L, 2)) - res = l_mathop(log)(x); - else { - lua_Number base = luaL_checknumber(L, 2); - if (base == (lua_Number)10.0) res = l_mathop(log10)(x); - else res = l_mathop(log)(x)/l_mathop(log)(base); - } - lua_pushnumber(L, res); - return 1; -} - -#if defined(LUA_COMPAT_LOG10) -static int math_log10 (lua_State *L) { - lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); - return 1; -} -#endif - -static int math_exp (lua_State *L) { - lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_deg (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); - return 1; -} - -static int math_rad (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); - return 1; -} - -static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); - return 2; -} - -static int math_ldexp (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - int ep = luaL_checkint(L, 2); - lua_pushnumber(L, l_mathop(ldexp)(x, ep)); - return 1; -} - - - -static int math_min (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmin = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d < dmin) - dmin = d; - } - lua_pushnumber(L, dmin); - return 1; -} - - -static int math_max (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - lua_Number dmax = luaL_checknumber(L, 1); - int i; - for (i=2; i<=n; i++) { - lua_Number d = luaL_checknumber(L, i); - if (d > dmax) - dmax = d; - } - lua_pushnumber(L, dmax); - return 1; -} - - -static int math_random (lua_State *L) { - /* the `%' avoids the (rare) case of r==1, and is needed also because on - some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ - lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; - switch (lua_gettop(L)) { /* check number of arguments */ - case 0: { /* no arguments */ - lua_pushnumber(L, r); /* Number between 0 and 1 */ - break; - } - case 1: { /* only upper limit */ - lua_Number u = luaL_checknumber(L, 1); - luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); - lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ - break; - } - case 2: { /* lower and upper limits */ - lua_Number l = luaL_checknumber(L, 1); - lua_Number u = luaL_checknumber(L, 2); - luaL_argcheck(L, l <= u, 2, "interval is empty"); - lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ - break; - } - default: return luaL_error(L, "wrong number of arguments"); - } - return 1; -} - - -static int math_randomseed (lua_State *L) { - srand(luaL_checkunsigned(L, 1)); - (void)rand(); /* discard first value to avoid undesirable correlations */ - return 0; -} - - -static const luaL_Reg mathlib[] = { - {"abs", math_abs}, - {"acos", math_acos}, - {"asin", math_asin}, - {"atan2", math_atan2}, - {"atan", math_atan}, - {"ceil", math_ceil}, - {"cosh", math_cosh}, - {"cos", math_cos}, - {"deg", math_deg}, - {"exp", math_exp}, - {"floor", math_floor}, - {"fmod", math_fmod}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, -#if defined(LUA_COMPAT_LOG10) - {"log10", math_log10}, -#endif - {"log", math_log}, - {"max", math_max}, - {"min", math_min}, - {"modf", math_modf}, - {"pow", math_pow}, - {"rad", math_rad}, - {"random", math_random}, - {"randomseed", math_randomseed}, - {"sinh", math_sinh}, - {"sin", math_sin}, - {"sqrt", math_sqrt}, - {"tanh", math_tanh}, - {"tan", math_tan}, - {NULL, NULL} -}; - - -/* -** Open math library -*/ -LUAMOD_API int luaopen_math (lua_State *L) { - luaL_newlib(L, mathlib); - lua_pushnumber(L, PI); - lua_setfield(L, -2, "pi"); - lua_pushnumber(L, HUGE_VAL); - lua_setfield(L, -2, "huge"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.c deleted file mode 100644 index ee343e3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.c +++ /dev/null @@ -1,99 +0,0 @@ -/* -** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - - -#include - -#define lmem_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -/* -** About the realloc function: -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); -** (`osize' is the old size, `nsize' is the new size) -** -** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no -** matter 'x'). -** -** * frealloc(ud, p, x, 0) frees the block `p' -** (in this specific case, frealloc must return NULL); -** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ANSI C) -** -** frealloc returns NULL if it cannot create or reallocate the area -** (any reallocation to an equal or smaller size cannot fail!) -*/ - - - -#define MINSIZEARRAY 4 - - -void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, - int limit, const char *what) { - void *newblock; - int newsize; - if (*size >= limit/2) { /* cannot double it? */ - if (*size >= limit) /* cannot grow even a little? */ - luaG_runerror(L, "too many %s (limit is %d)", what, limit); - newsize = limit; /* still have at least one free place */ - } - else { - newsize = (*size)*2; - if (newsize < MINSIZEARRAY) - newsize = MINSIZEARRAY; /* minimum size */ - } - newblock = luaM_reallocv(L, block, *size, newsize, size_elems); - *size = newsize; /* update only when everything else is OK */ - return newblock; -} - - -l_noret luaM_toobig (lua_State *L) { - luaG_runerror(L, "memory allocation error: block too big"); -} - - - -/* -** generic allocation routine. -*/ -void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { - void *newblock; - global_State *g = G(L); - size_t realosize = (block) ? osize : 0; - lua_assert((realosize == 0) == (block == NULL)); -#if defined(HARDMEMTESTS) - if (nsize > realosize && g->gcrunning) - luaC_fullgc(L, 1); /* force a GC whenever possible */ -#endif - newblock = (*g->frealloc)(g->ud, block, osize, nsize); - if (newblock == NULL && nsize > 0) { - api_check(L, nsize > realosize, - "realloc cannot fail when shrinking a block"); - if (g->gcrunning) { - luaC_fullgc(L, 1); /* try to free some memory... */ - newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ - } - if (newblock == NULL) - luaD_throw(L, LUA_ERRMEM); - } - lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - realosize; - return newblock; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.h deleted file mode 100644 index bd4f4e0..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lmem.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - -#ifndef lmem_h -#define lmem_h - - -#include - -#include "llimits.h" -#include "lua.h" - - -/* -** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is -** always constant. -** The macro is somewhat complex to avoid warnings: -** +1 avoids warnings of "comparison has constant result"; -** cast to 'void' avoids warnings of "value unused". -*/ -#define luaM_reallocv(L,b,on,n,e) \ - (cast(void, \ - (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ - luaM_realloc_(L, (b), (on)*(e), (n)*(e))) - -#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) -#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) -#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) - -#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) -#define luaM_newvector(L,n,t) \ - cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) - -#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) - -#define luaM_growvector(L,v,nelems,size,t,limit,e) \ - if ((nelems)+1 > (size)) \ - ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) - -#define luaM_reallocvector(L, v,oldn,n,t) \ - ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) - -LUAI_FUNC l_noret luaM_toobig (lua_State *L); - -/* not to be called directly */ -LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, - size_t size); -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, - size_t size_elem, int limit, - const char *what); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/loadlib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/loadlib.c deleted file mode 100644 index bedbea3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/loadlib.c +++ /dev/null @@ -1,725 +0,0 @@ -/* -** $Id: loadlib.c,v 1.111.1.1 2013/04/12 18:48:47 roberto Exp $ -** Dynamic library loader for Lua -** See Copyright Notice in lua.h -** -** This module contains an implementation of loadlib for Unix systems -** that have dlfcn, an implementation for Windows, and a stub for other -** systems. -*/ - - -/* -** if needed, includes windows header before everything else -*/ -#if defined(_WIN32) -#include -#endif - - -#include -#include - - -#define loadlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** LUA_PATH and LUA_CPATH are the names of the environment -** variables that Lua check to set its paths. -*/ -#if !defined(LUA_PATH) -#define LUA_PATH "LUA_PATH" -#endif - -#if !defined(LUA_CPATH) -#define LUA_CPATH "LUA_CPATH" -#endif - -#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - -#define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX -#define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX - -/* -** LUA_PATH_SEP is the character that separates templates in a path. -** LUA_PATH_MARK is the string that marks the substitution points in a -** template. -** LUA_EXEC_DIR in a Windows path is replaced by the executable's -** directory. -** LUA_IGMARK is a mark to ignore all before it when building the -** luaopen_ function name. -*/ -#if !defined (LUA_PATH_SEP) -#define LUA_PATH_SEP ";" -#endif -#if !defined (LUA_PATH_MARK) -#define LUA_PATH_MARK "?" -#endif -#if !defined (LUA_EXEC_DIR) -#define LUA_EXEC_DIR "!" -#endif -#if !defined (LUA_IGMARK) -#define LUA_IGMARK "-" -#endif - - -/* -** LUA_CSUBSEP is the character that replaces dots in submodule names -** when searching for a C loader. -** LUA_LSUBSEP is the character that replaces dots in submodule names -** when searching for a Lua loader. -*/ -#if !defined(LUA_CSUBSEP) -#define LUA_CSUBSEP LUA_DIRSEP -#endif - -#if !defined(LUA_LSUBSEP) -#define LUA_LSUBSEP LUA_DIRSEP -#endif - - -/* prefix for open functions in C libraries */ -#define LUA_POF "luaopen_" - -/* separator for open functions in C libraries */ -#define LUA_OFSEP "_" - - -/* table (in the registry) that keeps handles for all loaded C libraries */ -#define CLIBS "_CLIBS" - -#define LIB_FAIL "open" - - -/* error codes for ll_loadfunc */ -#define ERRLIB 1 -#define ERRFUNC 2 - -#define setprogdir(L) ((void)0) - - -/* -** system-dependent functions -*/ -static void ll_unloadlib (void *lib); -static void *ll_load (lua_State *L, const char *path, int seeglb); -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); - - - -#if defined(LUA_USE_DLOPEN) -/* -** {======================================================================== -** This is an implementation of loadlib based on the dlfcn interface. -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -** as an emulation layer on top of native functions. -** ========================================================================= -*/ - -#include - -static void ll_unloadlib (void *lib) { - dlclose(lib); -} - - -static void *ll_load (lua_State *L, const char *path, int seeglb) { - void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); - if (lib == NULL) lua_pushstring(L, dlerror()); - return lib; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); - if (f == NULL) lua_pushstring(L, dlerror()); - return f; -} - -/* }====================================================== */ - - - -#elif defined(LUA_DL_DLL) -/* -** {====================================================================== -** This is an implementation of loadlib for Windows using native functions. -** ======================================================================= -*/ - -#undef setprogdir - -/* -** optional flags for LoadLibraryEx -*/ -#if !defined(LUA_LLE_FLAGS) -#define LUA_LLE_FLAGS 0 -#endif - - -static void setprogdir (lua_State *L) { - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) - luaL_error(L, "unable to get ModuleFileName"); - else { - *lb = '\0'; - luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - - -static void pusherror (lua_State *L) { - int error = GetLastError(); - char buffer[128]; - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL)) - lua_pushstring(L, buffer); - else - lua_pushfstring(L, "system error %d\n", error); -} - -static void ll_unloadlib (void *lib) { - FreeLibrary((HMODULE)lib); -} - - -static void *ll_load (lua_State *L, const char *path, int seeglb) { - HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS); - (void)(seeglb); /* not used: symbols are 'global' by default */ - if (lib == NULL) pusherror(L); - return lib; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym); - if (f == NULL) pusherror(L); - return f; -} - -/* }====================================================== */ - - -#else -/* -** {====================================================== -** Fallback for other systems -** ======================================================= -*/ - -#undef LIB_FAIL -#define LIB_FAIL "absent" - - -#define DLMSG "dynamic libraries not enabled; check your Lua installation" - - -static void ll_unloadlib (void *lib) { - (void)(lib); /* not used */ -} - - -static void *ll_load (lua_State *L, const char *path, int seeglb) { - (void)(path); (void)(seeglb); /* not used */ - lua_pushliteral(L, DLMSG); - return NULL; -} - - -static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { - (void)(lib); (void)(sym); /* not used */ - lua_pushliteral(L, DLMSG); - return NULL; -} - -/* }====================================================== */ -#endif - - -static void *ll_checkclib (lua_State *L, const char *path) { - void *plib; - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); - lua_getfield(L, -1, path); - plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ - lua_pop(L, 2); /* pop CLIBS table and 'plib' */ - return plib; -} - - -static void ll_addtoclib (lua_State *L, const char *path, void *plib) { - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); - lua_pushlightuserdata(L, plib); - lua_pushvalue(L, -1); - lua_setfield(L, -3, path); /* CLIBS[path] = plib */ - lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ - lua_pop(L, 1); /* pop CLIBS table */ -} - - -/* -** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib -** handles in list CLIBS -*/ -static int gctm (lua_State *L) { - int n = luaL_len(L, 1); - for (; n >= 1; n--) { /* for each handle, in reverse order */ - lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - ll_unloadlib(lua_touserdata(L, -1)); - lua_pop(L, 1); /* pop handle */ - } - return 0; -} - - -static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { - void *reg = ll_checkclib(L, path); /* check loaded C libraries */ - if (reg == NULL) { /* must load library? */ - reg = ll_load(L, path, *sym == '*'); - if (reg == NULL) return ERRLIB; /* unable to load library */ - ll_addtoclib(L, path, reg); - } - if (*sym == '*') { /* loading only library (no function)? */ - lua_pushboolean(L, 1); /* return 'true' */ - return 0; /* no errors */ - } - else { - lua_CFunction f = ll_sym(L, reg, sym); - if (f == NULL) - return ERRFUNC; /* unable to find function */ - lua_pushcfunction(L, f); /* else create new function */ - return 0; /* no errors */ - } -} - - -static int ll_loadlib (lua_State *L) { - const char *path = luaL_checkstring(L, 1); - const char *init = luaL_checkstring(L, 2); - int stat = ll_loadfunc(L, path, init); - if (stat == 0) /* no errors? */ - return 1; /* return the loaded function */ - else { /* error; error message is on stack top */ - lua_pushnil(L); - lua_insert(L, -2); - lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ - } -} - - - -/* -** {====================================================== -** 'require' function -** ======================================================= -*/ - - -static int readable (const char *filename) { - FILE *f = fopen(filename, "r"); /* try to open file */ - if (f == NULL) return 0; /* open failed */ - fclose(f); - return 1; -} - - -static const char *pushnexttemplate (lua_State *L, const char *path) { - const char *l; - while (*path == *LUA_PATH_SEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ - l = strchr(path, *LUA_PATH_SEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, l - path); /* template */ - return l; -} - - -static const char *searchpath (lua_State *L, const char *name, - const char *path, - const char *sep, - const char *dirsep) { - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - if (*sep != '\0') /* non-empty separator? */ - name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename = luaL_gsub(L, lua_tostring(L, -1), - LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ - if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); - lua_remove(L, -2); /* remove file name */ - luaL_addvalue(&msg); /* concatenate error msg. entry */ - } - luaL_pushresult(&msg); /* create error message */ - return NULL; /* not found */ -} - - -static int ll_searchpath (lua_State *L) { - const char *f = searchpath(L, luaL_checkstring(L, 1), - luaL_checkstring(L, 2), - luaL_optstring(L, 3, "."), - luaL_optstring(L, 4, LUA_DIRSEP)); - if (f != NULL) return 1; - else { /* error message is on top of the stack */ - lua_pushnil(L); - lua_insert(L, -2); - return 2; /* return nil + error message */ - } -} - - -static const char *findfile (lua_State *L, const char *name, - const char *pname, - const char *dirsep) { - const char *path; - lua_getfield(L, lua_upvalueindex(1), pname); - path = lua_tostring(L, -1); - if (path == NULL) - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); - return searchpath(L, name, path, ".", dirsep); -} - - -static int checkload (lua_State *L, int stat, const char *filename) { - if (stat) { /* module loaded successfully? */ - lua_pushstring(L, filename); /* will be 2nd argument to module */ - return 2; /* return open function and file name */ - } - else - return luaL_error(L, "error loading module " LUA_QS - " from file " LUA_QS ":\n\t%s", - lua_tostring(L, 1), filename, lua_tostring(L, -1)); -} - - -static int searcher_Lua (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - filename = findfile(L, name, "path", LUA_LSUBSEP); - if (filename == NULL) return 1; /* module not found in this path */ - return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); -} - - -static int loadfunc (lua_State *L, const char *filename, const char *modname) { - const char *funcname; - const char *mark; - modname = luaL_gsub(L, modname, ".", LUA_OFSEP); - mark = strchr(modname, *LUA_IGMARK); - if (mark) { - int stat; - funcname = lua_pushlstring(L, modname, mark - modname); - funcname = lua_pushfstring(L, LUA_POF"%s", funcname); - stat = ll_loadfunc(L, filename, funcname); - if (stat != ERRFUNC) return stat; - modname = mark + 1; /* else go ahead and try old-style name */ - } - funcname = lua_pushfstring(L, LUA_POF"%s", modname); - return ll_loadfunc(L, filename, funcname); -} - - -static int searcher_C (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); - if (filename == NULL) return 1; /* module not found in this path */ - return checkload(L, (loadfunc(L, filename, name) == 0), filename); -} - - -static int searcher_Croot (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - const char *p = strchr(name, '.'); - int stat; - if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, p - name); - filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); - if (filename == NULL) return 1; /* root not found */ - if ((stat = loadfunc(L, filename, name)) != 0) { - if (stat != ERRFUNC) - return checkload(L, 0, filename); /* real error */ - else { /* open function not found */ - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, - name, filename); - return 1; - } - } - lua_pushstring(L, filename); /* will be 2nd argument to module */ - return 2; -} - - -static int searcher_preload (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) /* not found? */ - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - return 1; -} - - -static void findloader (lua_State *L, const char *name) { - int i; - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */ - if (!lua_istable(L, 3)) - luaL_error(L, LUA_QL("package.searchers") " must be a table"); - /* iterate over available searchers to find a loader */ - for (i = 1; ; i++) { - lua_rawgeti(L, 3, i); /* get a searcher */ - if (lua_isnil(L, -1)) { /* no more searchers? */ - lua_pop(L, 1); /* remove nil */ - luaL_pushresult(&msg); /* create error message */ - luaL_error(L, "module " LUA_QS " not found:%s", - name, lua_tostring(L, -1)); - } - lua_pushstring(L, name); - lua_call(L, 1, 2); /* call it */ - if (lua_isfunction(L, -2)) /* did it find a loader? */ - return; /* module loader found */ - else if (lua_isstring(L, -2)) { /* searcher returned error message? */ - lua_pop(L, 1); /* remove extra return */ - luaL_addvalue(&msg); /* concatenate error message */ - } - else - lua_pop(L, 2); /* remove both returns */ - } -} - - -static int ll_require (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_settop(L, 1); /* _LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); /* _LOADED[name] */ - if (lua_toboolean(L, -1)) /* is it there? */ - return 1; /* package is already loaded */ - /* else must load package */ - lua_pop(L, 1); /* remove 'getfield' result */ - findloader(L, name); - lua_pushstring(L, name); /* pass name as argument to module loader */ - lua_insert(L, -2); /* name is 1st argument (before search data) */ - lua_call(L, 2, 1); /* run loader to load module */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ - lua_getfield(L, 2, name); - if (lua_isnil(L, -1)) { /* module did not set a value? */ - lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* _LOADED[name] = true */ - } - return 1; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** 'module' function -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -/* -** changes the environment variable of calling function -*/ -static void set_env (lua_State *L) { - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - luaL_error(L, LUA_QL("module") " not called from a Lua function"); - lua_pushvalue(L, -2); /* copy new environment table to top */ - lua_setupvalue(L, -2, 1); - lua_pop(L, 1); /* remove function */ -} - - -static void dooptions (lua_State *L, int n) { - int i; - for (i = 2; i <= n; i++) { - if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */ - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } - } -} - - -static void modinit (lua_State *L, const char *modname) { - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; - else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, dot - modname); - lua_setfield(L, -2, "_PACKAGE"); -} - - -static int ll_module (lua_State *L) { - const char *modname = luaL_checkstring(L, 1); - int lastarg = lua_gettop(L); /* last parameter */ - luaL_pushmodule(L, modname, 1); /* get/create module table */ - /* check whether table already has a _NAME field */ - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) /* is table an initialized module? */ - lua_pop(L, 1); - else { /* no; initialize it */ - lua_pop(L, 1); - modinit(L, modname); - } - lua_pushvalue(L, -1); - set_env(L); - dooptions(L, lastarg); - return 1; -} - - -static int ll_seeall (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushglobaltable(L); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - -#endif -/* }====================================================== */ - - - -/* auxiliary mark (for internal use) */ -#define AUXMARK "\1" - - -/* -** return registry.LUA_NOENV as a boolean -*/ -static int noenv (lua_State *L) { - int b; - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - b = lua_toboolean(L, -1); - lua_pop(L, 1); /* remove value */ - return b; -} - - -static void setpath (lua_State *L, const char *fieldname, const char *envname1, - const char *envname2, const char *def) { - const char *path = getenv(envname1); - if (path == NULL) /* no environment variable? */ - path = getenv(envname2); /* try alternative name */ - if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, def); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); - } - setprogdir(L); - lua_setfield(L, -2, fieldname); -} - - -static const luaL_Reg pk_funcs[] = { - {"loadlib", ll_loadlib}, - {"searchpath", ll_searchpath}, -#if defined(LUA_COMPAT_MODULE) - {"seeall", ll_seeall}, -#endif - {NULL, NULL} -}; - - -static const luaL_Reg ll_funcs[] = { -#if defined(LUA_COMPAT_MODULE) - {"module", ll_module}, -#endif - {"require", ll_require}, - {NULL, NULL} -}; - - -static void createsearcherstable (lua_State *L) { - static const lua_CFunction searchers[] = - {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; - int i; - /* create 'searchers' table */ - lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); - /* fill it with pre-defined searchers */ - for (i=0; searchers[i] != NULL; i++) { - lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ - lua_pushcclosure(L, searchers[i], 1); - lua_rawseti(L, -2, i+1); - } -} - - -LUAMOD_API int luaopen_package (lua_State *L) { - /* create table CLIBS to keep track of loaded C libraries */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); - lua_createtable(L, 0, 1); /* metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); - /* create `package' table */ - luaL_newlib(L, pk_funcs); - createsearcherstable(L); -#if defined(LUA_COMPAT_LOADERS) - lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ - lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ -#endif - lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ - /* set field 'path' */ - setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT); - /* set field 'cpath' */ - setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT); - /* store config information */ - lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" - LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); - lua_setfield(L, -2, "config"); - /* set field `loaded' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_setfield(L, -2, "loaded"); - /* set field `preload' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); - lua_setfield(L, -2, "preload"); - lua_pushglobaltable(L); - lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ - luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ - lua_pop(L, 1); /* pop global table */ - return 1; /* return 'package' table */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.c deleted file mode 100644 index 882d994..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.c +++ /dev/null @@ -1,287 +0,0 @@ -/* -** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ -** Some generic functions over Lua objects -** See Copyright Notice in lua.h -*/ - -#include -#include -#include -#include - -#define lobject_c -#define LUA_CORE - -#include "lua.h" - -#include "lctype.h" -#include "ldebug.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "lvm.h" - - - -LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; - - -/* -** converts an integer to a "floating point byte", represented as -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if -** eeeee != 0 and (xxx) otherwise. -*/ -int luaO_int2fb (unsigned int x) { - int e = 0; /* exponent */ - if (x < 8) return x; - while (x >= 0x10) { - x = (x+1) >> 1; - e++; - } - return ((e+1) << 3) | (cast_int(x) - 8); -} - - -/* converts back */ -int luaO_fb2int (int x) { - int e = (x >> 3) & 0x1f; - if (e == 0) return x; - else return ((x & 7) + 8) << (e - 1); -} - - -int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 - }; - int l = 0; - x--; - while (x >= 256) { l += 8; x >>= 8; } - return l + log_2[x]; -} - - -lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { - switch (op) { - case LUA_OPADD: return luai_numadd(NULL, v1, v2); - case LUA_OPSUB: return luai_numsub(NULL, v1, v2); - case LUA_OPMUL: return luai_nummul(NULL, v1, v2); - case LUA_OPDIV: return luai_numdiv(NULL, v1, v2); - case LUA_OPMOD: return luai_nummod(NULL, v1, v2); - case LUA_OPPOW: return luai_numpow(NULL, v1, v2); - case LUA_OPUNM: return luai_numunm(NULL, v1); - default: lua_assert(0); return 0; - } -} - - -int luaO_hexavalue (int c) { - if (lisdigit(c)) return c - '0'; - else return ltolower(c) - 'a' + 10; -} - - -#if !defined(lua_strx2number) - -#include - - -static int isneg (const char **s) { - if (**s == '-') { (*s)++; return 1; } - else if (**s == '+') (*s)++; - return 0; -} - - -static lua_Number readhexa (const char **s, lua_Number r, int *count) { - for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ - r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); - (*count)++; - } - return r; -} - - -/* -** convert an hexadecimal numeric string to a number, following -** C99 specification for 'strtod' -*/ -static lua_Number lua_strx2number (const char *s, char **endptr) { - lua_Number r = 0.0; - int e = 0, i = 0; - int neg = 0; /* 1 if number is negative */ - *endptr = cast(char *, s); /* nothing is valid yet */ - while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); /* check signal */ - if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ - return 0.0; /* invalid format (no '0x') */ - s += 2; /* skip '0x' */ - r = readhexa(&s, r, &i); /* read integer part */ - if (*s == '.') { - s++; /* skip dot */ - r = readhexa(&s, r, &e); /* read fractional part */ - } - if (i == 0 && e == 0) - return 0.0; /* invalid format (no digit) */ - e *= -4; /* each fractional digit divides value by 2^-4 */ - *endptr = cast(char *, s); /* valid up to here */ - if (*s == 'p' || *s == 'P') { /* exponent part? */ - int exp1 = 0; - int neg1; - s++; /* skip 'p' */ - neg1 = isneg(&s); /* signal */ - if (!lisdigit(cast_uchar(*s))) - goto ret; /* must have at least one digit */ - while (lisdigit(cast_uchar(*s))) /* read exponent */ - exp1 = exp1 * 10 + *(s++) - '0'; - if (neg1) exp1 = -exp1; - e += exp1; - } - *endptr = cast(char *, s); /* valid up to here */ - ret: - if (neg) r = -r; - return l_mathop(ldexp)(r, e); -} - -#endif - - -int luaO_str2d (const char *s, size_t len, lua_Number *result) { - char *endptr; - if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ - return 0; - else if (strpbrk(s, "xX")) /* hexa? */ - *result = lua_strx2number(s, &endptr); - else - *result = lua_str2number(s, &endptr); - if (endptr == s) return 0; /* nothing recognized */ - while (lisspace(cast_uchar(*endptr))) endptr++; - return (endptr == s + len); /* OK if no trailing characters */ -} - - - -static void pushstr (lua_State *L, const char *str, size_t l) { - setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); -} - - -/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ -const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - int n = 0; - for (;;) { - const char *e = strchr(fmt, '%'); - if (e == NULL) break; - luaD_checkstack(L, 2); /* fmt + item */ - pushstr(L, fmt, e - fmt); - switch (*(e+1)) { - case 's': { - const char *s = va_arg(argp, char *); - if (s == NULL) s = "(null)"; - pushstr(L, s, strlen(s)); - break; - } - case 'c': { - char buff; - buff = cast(char, va_arg(argp, int)); - pushstr(L, &buff, 1); - break; - } - case 'd': { - setnvalue(L->top++, cast_num(va_arg(argp, int))); - break; - } - case 'f': { - setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); - break; - } - case 'p': { - char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ - int l = sprintf(buff, "%p", va_arg(argp, void *)); - pushstr(L, buff, l); - break; - } - case '%': { - pushstr(L, "%", 1); - break; - } - default: { - luaG_runerror(L, - "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"), - *(e + 1)); - } - } - n += 2; - fmt = e+2; - } - luaD_checkstack(L, 1); - pushstr(L, fmt, strlen(fmt)); - if (n > 0) luaV_concat(L, n + 1); - return svalue(L->top - 1); -} - - -const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - return msg; -} - - -/* number of chars of a literal string without the ending \0 */ -#define LL(x) (sizeof(x)/sizeof(char) - 1) - -#define RETS "..." -#define PRE "[string \"" -#define POS "\"]" - -#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) - -void luaO_chunkid (char *out, const char *source, size_t bufflen) { - size_t l = strlen(source); - if (*source == '=') { /* 'literal' source */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); - else { /* truncate it */ - addstr(out, source + 1, bufflen - 1); - *out = '\0'; - } - } - else if (*source == '@') { /* file name */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); - else { /* add '...' before rest of name */ - addstr(out, RETS, LL(RETS)); - bufflen -= LL(RETS); - memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); - } - } - else { /* string; format as [string "source"] */ - const char *nl = strchr(source, '\n'); /* find first new line (if any) */ - addstr(out, PRE, LL(PRE)); /* add prefix */ - bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ - if (l < bufflen && nl == NULL) { /* small one-line source? */ - addstr(out, source, l); /* keep it */ - } - else { - if (nl != NULL) l = nl - source; /* stop at first newline */ - if (l > bufflen) l = bufflen; - addstr(out, source, l); - addstr(out, RETS, LL(RETS)); - } - memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.h deleted file mode 100644 index bc0bb69..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lobject.h +++ /dev/null @@ -1,607 +0,0 @@ -/* -** $Id: lobject.h,v 2.71.1.2 2014/05/07 14:14:58 roberto Exp $ -** Type definitions for Lua objects -** See Copyright Notice in lua.h -*/ - - -#ifndef lobject_h -#define lobject_h - - -#include - - -#include "llimits.h" -#include "lua.h" - - -/* -** Extra tags for non-values -*/ -#define LUA_TPROTO LUA_NUMTAGS -#define LUA_TUPVAL (LUA_NUMTAGS+1) -#define LUA_TDEADKEY (LUA_NUMTAGS+2) - -/* -** number of all possible tags (including LUA_TNONE but excluding DEADKEY) -*/ -#define LUA_TOTALTAGS (LUA_TUPVAL+2) - - -/* -** tags for Tagged Values have the following use of bits: -** bits 0-3: actual tag (a LUA_T* value) -** bits 4-5: variant bits -** bit 6: whether value is collectable -*/ - -#define VARBITS (3 << 4) - - -/* -** LUA_TFUNCTION variants: -** 0 - Lua function -** 1 - light C function -** 2 - regular C function (closure) -*/ - -/* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ - - -/* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ - - -/* Bit mark for collectable types */ -#define BIT_ISCOLLECTABLE (1 << 6) - -/* mark a tag as collectable */ -#define ctb(t) ((t) | BIT_ISCOLLECTABLE) - - -/* -** Union of all collectable objects -*/ -typedef union GCObject GCObject; - - -/* -** Common Header for all collectable objects (in macro form, to be -** included in other objects) -*/ -#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked - - -/* -** Common header in struct form -*/ -typedef struct GCheader { - CommonHeader; -} GCheader; - - - -/* -** Union of all Lua values -*/ -typedef union Value Value; - - -#define numfield lua_Number n; /* numbers */ - - - -/* -** Tagged Values. This is the basic representation of values in Lua, -** an actual value plus a tag with its type. -*/ - -#define TValuefields Value value_; int tt_ - -typedef struct lua_TValue TValue; - - -/* macro defining a nil value */ -#define NILCONSTANT {NULL}, LUA_TNIL - - -#define val_(o) ((o)->value_) -#define num_(o) (val_(o).n) - - -/* raw type tag of a TValue */ -#define rttype(o) ((o)->tt_) - -/* tag with no variants (bits 0-3) */ -#define novariant(x) ((x) & 0x0F) - -/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ -#define ttype(o) (rttype(o) & 0x3F) - -/* type tag of a TValue with no variants (bits 0-3) */ -#define ttypenv(o) (novariant(rttype(o))) - - -/* Macros to test type */ -#define checktag(o,t) (rttype(o) == (t)) -#define checktype(o,t) (ttypenv(o) == (t)) -#define ttisnumber(o) checktag((o), LUA_TNUMBER) -#define ttisnil(o) checktag((o), LUA_TNIL) -#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) -#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) -#define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) -#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) -#define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) -#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) -#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) -#define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA)) -#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) -#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) - -#define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) - -/* Macros to access values */ -#define nvalue(o) check_exp(ttisnumber(o), num_(o)) -#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) -#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) -#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) -#define tsvalue(o) (&rawtsvalue(o)->tsv) -#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u) -#define uvalue(o) (&rawuvalue(o)->uv) -#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl) -#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l) -#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c) -#define fvalue(o) check_exp(ttislcf(o), val_(o).f) -#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h) -#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th) -/* a dead value may get the 'gc' field, but cannot access its contents */ -#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc)) - -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) - - -#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) - - -/* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt) - -#define checkliveness(g,obj) \ - lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && !isdead(g,gcvalue(obj)))) - - -/* Macros to set values */ -#define settt_(o,t) ((o)->tt_=(t)) - -#define setnvalue(obj,x) \ - { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); } - -#define setnilvalue(obj) settt_(obj, LUA_TNIL) - -#define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } - -#define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } - -#define setbvalue(obj,x) \ - { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } - -#define setgcovalue(L,obj,x) \ - { TValue *io=(obj); GCObject *i_g=(x); \ - val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); } - -#define setsvalue(L,obj,x) \ - { TValue *io=(obj); \ - TString *x_ = (x); \ - val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \ - checkliveness(G(L),io); } - -#define setuvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \ - checkliveness(G(L),io); } - -#define setthvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \ - checkliveness(G(L),io); } - -#define setclLvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \ - checkliveness(G(L),io); } - -#define setclCvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \ - checkliveness(G(L),io); } - -#define sethvalue(L,obj,x) \ - { TValue *io=(obj); \ - val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \ - checkliveness(G(L),io); } - -#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) - - - -#define setobj(L,obj1,obj2) \ - { const TValue *io2=(obj2); TValue *io1=(obj1); \ - io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - checkliveness(G(L),io1); } - - -/* -** different types of assignments, according to destination -*/ - -/* from stack to (same) stack */ -#define setobjs2s setobj -/* to stack (not from same stack) */ -#define setobj2s setobj -#define setsvalue2s setsvalue -#define sethvalue2s sethvalue -#define setptvalue2s setptvalue -/* from table to same table */ -#define setobjt2t setobj -/* to table */ -#define setobj2t setobj -/* to new object */ -#define setobj2n setobj -#define setsvalue2n setsvalue - - -/* check whether a number is valid (useful only for NaN trick) */ -#define luai_checknum(L,o,c) { /* empty */ } - - -/* -** {====================================================== -** NaN Trick -** ======================================================= -*/ -#if defined(LUA_NANTRICK) - -/* -** numbers are represented in the 'd_' field. All other values have the -** value (NNMARK | tag) in 'tt__'. A number with such pattern would be -** a "signaled NaN", which is never generated by regular operations by -** the CPU (nor by 'strtod') -*/ - -/* allows for external implementation for part of the trick */ -#if !defined(NNMARK) /* { */ - - -#if !defined(LUA_IEEEENDIAN) -#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN' -#endif - - -#define NNMARK 0x7FF7A500 -#define NNMASK 0x7FFFFF00 - -#undef TValuefields -#undef NILCONSTANT - -#if (LUA_IEEEENDIAN == 0) /* { */ - -/* little endian */ -#define TValuefields \ - union { struct { Value v__; int tt__; } i; double d__; } u -#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}} -/* field-access macros */ -#define v_(o) ((o)->u.i.v__) -#define d_(o) ((o)->u.d__) -#define tt_(o) ((o)->u.i.tt__) - -#else /* }{ */ - -/* big endian */ -#define TValuefields \ - union { struct { int tt__; Value v__; } i; double d__; } u -#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}} -/* field-access macros */ -#define v_(o) ((o)->u.i.v__) -#define d_(o) ((o)->u.d__) -#define tt_(o) ((o)->u.i.tt__) - -#endif /* } */ - -#endif /* } */ - - -/* correspondence with standard representation */ -#undef val_ -#define val_(o) v_(o) -#undef num_ -#define num_(o) d_(o) - - -#undef numfield -#define numfield /* no such field; numbers are the entire struct */ - -/* basic check to distinguish numbers from non-numbers */ -#undef ttisnumber -#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK) - -#define tag2tt(t) (NNMARK | (t)) - -#undef rttype -#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff) - -#undef settt_ -#define settt_(o,t) (tt_(o) = tag2tt(t)) - -#undef setnvalue -#define setnvalue(obj,x) \ - { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); } - -#undef setobj -#define setobj(L,obj1,obj2) \ - { const TValue *o2_=(obj2); TValue *o1_=(obj1); \ - o1_->u = o2_->u; \ - checkliveness(G(L),o1_); } - - -/* -** these redefinitions are not mandatory, but these forms are more efficient -*/ - -#undef checktag -#undef checktype -#define checktag(o,t) (tt_(o) == tag2tt(t)) -#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS)) - -#undef ttisequal -#define ttisequal(o1,o2) \ - (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2))) - - -#undef luai_checknum -#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; } - -#endif -/* }====================================================== */ - - - -/* -** {====================================================== -** types and prototypes -** ======================================================= -*/ - - -union Value { - GCObject *gc; /* collectable objects */ - void *p; /* light userdata */ - int b; /* booleans */ - lua_CFunction f; /* light C functions */ - numfield /* numbers */ -}; - - -struct lua_TValue { - TValuefields; -}; - - -typedef TValue *StkId; /* index to stack elements */ - - - - -/* -** Header for string value; string bytes follow the end of this structure -*/ -typedef union TString { - L_Umaxalign dummy; /* ensures maximum alignment for strings */ - struct { - CommonHeader; - lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - unsigned int hash; - size_t len; /* number of characters in string */ - } tsv; -} TString; - - -/* get the actual string (array of bytes) from a TString */ -#define getstr(ts) cast(const char *, (ts) + 1) - -/* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(rawtsvalue(o)) - - -/* -** Header for userdata; memory area follows the end of this structure -*/ -typedef union Udata { - L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ - struct { - CommonHeader; - struct Table *metatable; - struct Table *env; - size_t len; /* number of bytes */ - } uv; -} Udata; - - - -/* -** Description of an upvalue for function prototypes -*/ -typedef struct Upvaldesc { - TString *name; /* upvalue name (for debug information) */ - lu_byte instack; /* whether it is in stack */ - lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ -} Upvaldesc; - - -/* -** Description of a local variable for function prototypes -** (used for debug information) -*/ -typedef struct LocVar { - TString *varname; - int startpc; /* first point where variable is active */ - int endpc; /* first point where variable is dead */ -} LocVar; - - -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; - TValue *k; /* constants used by the function */ - Instruction *code; - struct Proto **p; /* functions defined inside the function */ - int *lineinfo; /* map from opcodes to source lines (debug information) */ - LocVar *locvars; /* information about local variables (debug information) */ - Upvaldesc *upvalues; /* upvalue information */ - union Closure *cache; /* last created closure with this prototype */ - TString *source; /* used for debug information */ - int sizeupvalues; /* size of 'upvalues' */ - int sizek; /* size of `k' */ - int sizecode; - int sizelineinfo; - int sizep; /* size of `p' */ - int sizelocvars; - int linedefined; - int lastlinedefined; - GCObject *gclist; - lu_byte numparams; /* number of fixed parameters */ - lu_byte is_vararg; - lu_byte maxstacksize; /* maximum stack used by this function */ -} Proto; - - - -/* -** Lua Upvalues -*/ -typedef struct UpVal { - CommonHeader; - TValue *v; /* points to stack or to its own value */ - union { - TValue value; /* the value (when closed) */ - struct { /* double linked list (when open) */ - struct UpVal *prev; - struct UpVal *next; - } l; - } u; -} UpVal; - - -/* -** Closures -*/ - -#define ClosureHeader \ - CommonHeader; lu_byte nupvalues; GCObject *gclist - -typedef struct CClosure { - ClosureHeader; - lua_CFunction f; - TValue upvalue[1]; /* list of upvalues */ -} CClosure; - - -typedef struct LClosure { - ClosureHeader; - struct Proto *p; - UpVal *upvals[1]; /* list of upvalues */ -} LClosure; - - -typedef union Closure { - CClosure c; - LClosure l; -} Closure; - - -#define isLfunction(o) ttisLclosure(o) - -#define getproto(o) (clLvalue(o)->p) - - -/* -** Tables -*/ - -typedef union TKey { - struct { - TValuefields; - struct Node *next; /* for chaining */ - } nk; - TValue tvk; -} TKey; - - -typedef struct Node { - TValue i_val; - TKey i_key; -} Node; - - -typedef struct Table { - CommonHeader; - lu_byte flags; /* 1<

lsizenode)) - - -/* -** (address of) a fixed nil value -*/ -#define luaO_nilobject (&luaO_nilobject_) - - -LUAI_DDEC const TValue luaO_nilobject_; - - -LUAI_FUNC int luaO_int2fb (unsigned int x); -LUAI_FUNC int luaO_fb2int (int x); -LUAI_FUNC int luaO_ceillog2 (unsigned int x); -LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); -LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); -LUAI_FUNC int luaO_hexavalue (int c); -LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, - va_list argp); -LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.c deleted file mode 100644 index 4190dc7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.c +++ /dev/null @@ -1,107 +0,0 @@ -/* -** $Id: lopcodes.c,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - - -#define lopcodes_c -#define LUA_CORE - - -#include "lopcodes.h" - - -/* ORDER OP */ - -LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { - "MOVE", - "LOADK", - "LOADKX", - "LOADBOOL", - "LOADNIL", - "GETUPVAL", - "GETTABUP", - "GETTABLE", - "SETTABUP", - "SETUPVAL", - "SETTABLE", - "NEWTABLE", - "SELF", - "ADD", - "SUB", - "MUL", - "DIV", - "MOD", - "POW", - "UNM", - "NOT", - "LEN", - "CONCAT", - "JMP", - "EQ", - "LT", - "LE", - "TEST", - "TESTSET", - "CALL", - "TAILCALL", - "RETURN", - "FORLOOP", - "FORPREP", - "TFORCALL", - "TFORLOOP", - "SETLIST", - "CLOSURE", - "VARARG", - "EXTRAARG", - NULL -}; - - -#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) - -LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* T A B C mode opcode */ - opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ - ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ - ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ - ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ - ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ - ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ - ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ - ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ - ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ -}; - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.h deleted file mode 100644 index 8e2f80a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lopcodes.h +++ /dev/null @@ -1,288 +0,0 @@ -/* -** $Id: lopcodes.h,v 1.142.1.2 2014/10/20 18:32:09 roberto Exp $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lopcodes_h -#define lopcodes_h - -#include "llimits.h" - - -/*=========================================================================== - We assume that instructions are unsigned numbers. - All instructions have an opcode in the first 6 bits. - Instructions can have the following fields: - `A' : 8 bits - `B' : 9 bits - `C' : 9 bits - 'Ax' : 26 bits ('A', 'B', and 'C' together) - `Bx' : 18 bits (`B' and `C' together) - `sBx' : signed Bx - - A signed argument is represented in excess K; that is, the number - value is the unsigned value minus K. K is exactly the maximum value - for that argument (so that -max is represented by 0, and +max is - represented by 2*max), which is half the maximum for the corresponding - unsigned argument. -===========================================================================*/ - - -enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ - - -/* -** size and position of opcode arguments. -*/ -#define SIZE_C 9 -#define SIZE_B 9 -#define SIZE_Bx (SIZE_C + SIZE_B) -#define SIZE_A 8 -#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) - -#define SIZE_OP 6 - -#define POS_OP 0 -#define POS_A (POS_OP + SIZE_OP) -#define POS_C (POS_A + SIZE_A) -#define POS_B (POS_C + SIZE_C) -#define POS_Bx POS_C -#define POS_Ax POS_A - - -/* -** limits for opcode arguments. -** we use (signed) int to manipulate most arguments, -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) -*/ -#if SIZE_Bx < LUAI_BITSINT-1 -#define MAXARG_Bx ((1<>1) /* `sBx' is signed */ -#else -#define MAXARG_Bx MAX_INT -#define MAXARG_sBx MAX_INT -#endif - -#if SIZE_Ax < LUAI_BITSINT-1 -#define MAXARG_Ax ((1<>POS_OP) & MASK1(SIZE_OP,0))) -#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>pos) & MASK1(size,0))) -#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ - ((cast(Instruction, v)<= R(A - 1) */ -OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ -OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ -OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ - -OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ -OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ - -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ - -OP_FORLOOP,/* A sBx R(A)+=R(A+2); - if R(A) > 4) & 3)) -#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) -#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) -#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) - - -LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ - - -/* number of list items to accumulate before a SETLIST instruction */ -#define LFIELDS_PER_FLUSH 50 - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/loslib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/loslib.c deleted file mode 100644 index 052ba17..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/loslib.c +++ /dev/null @@ -1,323 +0,0 @@ -/* -** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ -** Standard Operating System library -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include -#include - -#define loslib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** list of valid conversion specifiers for the 'strftime' function -*/ -#if !defined(LUA_STRFTIMEOPTIONS) - -#if !defined(LUA_USE_POSIX) -#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } -#else -#define LUA_STRFTIMEOPTIONS \ - { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \ - "", "E", "cCxXyY", \ - "O", "deHImMSuUVwWy" } -#endif - -#endif - - - -/* -** By default, Lua uses tmpnam except when POSIX is available, where it -** uses mkstemp. -*/ -#if defined(LUA_USE_MKSTEMP) -#include -#define LUA_TMPNAMBUFSIZE 32 -#define lua_tmpnam(b,e) { \ - strcpy(b, "/tmp/lua_XXXXXX"); \ - e = mkstemp(b); \ - if (e != -1) close(e); \ - e = (e == -1); } - -#elif !defined(lua_tmpnam) - -#define LUA_TMPNAMBUFSIZE L_tmpnam -#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } - -#endif - - -/* -** By default, Lua uses gmtime/localtime, except when POSIX is available, -** where it uses gmtime_r/localtime_r -*/ -#if defined(LUA_USE_GMTIME_R) - -#define l_gmtime(t,r) gmtime_r(t,r) -#define l_localtime(t,r) localtime_r(t,r) - -#elif !defined(l_gmtime) - -#define l_gmtime(t,r) ((void)r, gmtime(t)) -#define l_localtime(t,r) ((void)r, localtime(t)) - -#endif - - - -static int os_execute (lua_State *L) { - const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); - if (cmd != NULL) - return luaL_execresult(L, stat); - else { - lua_pushboolean(L, stat); /* true if there is a shell */ - return 1; - } -} - - -static int os_remove (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - return luaL_fileresult(L, remove(filename) == 0, filename); -} - - -static int os_rename (lua_State *L) { - const char *fromname = luaL_checkstring(L, 1); - const char *toname = luaL_checkstring(L, 2); - return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); -} - - -static int os_tmpname (lua_State *L) { - char buff[LUA_TMPNAMBUFSIZE]; - int err; - lua_tmpnam(buff, err); - if (err) - return luaL_error(L, "unable to generate a unique filename"); - lua_pushstring(L, buff); - return 1; -} - - -static int os_getenv (lua_State *L) { - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ - return 1; -} - - -static int os_clock (lua_State *L) { - lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); - return 1; -} - - -/* -** {====================================================== -** Time/Date operations -** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, -** wday=%w+1, yday=%j, isdst=? } -** ======================================================= -*/ - -static void setfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); - lua_setfield(L, -2, key); -} - -static void setboolfield (lua_State *L, const char *key, int value) { - if (value < 0) /* undefined? */ - return; /* does not set field */ - lua_pushboolean(L, value); - lua_setfield(L, -2, key); -} - -static int getboolfield (lua_State *L, const char *key) { - int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); - lua_pop(L, 1); - return res; -} - - -static int getfield (lua_State *L, const char *key, int d) { - int res, isnum; - lua_getfield(L, -1, key); - res = (int)lua_tointegerx(L, -1, &isnum); - if (!isnum) { - if (d < 0) - return luaL_error(L, "field " LUA_QS " missing in date table", key); - res = d; - } - lua_pop(L, 1); - return res; -} - - -static const char *checkoption (lua_State *L, const char *conv, char *buff) { - static const char *const options[] = LUA_STRFTIMEOPTIONS; - unsigned int i; - for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { - if (*conv != '\0' && strchr(options[i], *conv) != NULL) { - buff[1] = *conv; - if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ - buff[2] = '\0'; /* end buffer */ - return conv + 1; - } - else if (*(conv + 1) != '\0' && - strchr(options[i + 1], *(conv + 1)) != NULL) { - buff[2] = *(conv + 1); /* valid two-char conversion specifier */ - buff[3] = '\0'; /* end buffer */ - return conv + 2; - } - } - } - luaL_argerror(L, 1, - lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); - return conv; /* to avoid warnings */ -} - - -static int os_date (lua_State *L) { - const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); - struct tm tmr, *stm; - if (*s == '!') { /* UTC? */ - stm = l_gmtime(&t, &tmr); - s++; /* skip `!' */ - } - else - stm = l_localtime(&t, &tmr); - if (stm == NULL) /* invalid date? */ - lua_pushnil(L); - else if (strcmp(s, "*t") == 0) { - lua_createtable(L, 0, 9); /* 9 = number of fields */ - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon+1); - setfield(L, "year", stm->tm_year+1900); - setfield(L, "wday", stm->tm_wday+1); - setfield(L, "yday", stm->tm_yday+1); - setboolfield(L, "isdst", stm->tm_isdst); - } - else { - char cc[4]; - luaL_Buffer b; - cc[0] = '%'; - luaL_buffinit(L, &b); - while (*s) { - if (*s != '%') /* no conversion specifier? */ - luaL_addchar(&b, *s++); - else { - size_t reslen; - char buff[200]; /* should be big enough for any conversion result */ - s = checkoption(L, s + 1, cc); - reslen = strftime(buff, sizeof(buff), cc, stm); - luaL_addlstring(&b, buff, reslen); - } - } - luaL_pushresult(&b); - } - return 1; -} - - -static int os_time (lua_State *L) { - time_t t; - if (lua_isnoneornil(L, 1)) /* called without args? */ - t = time(NULL); /* get current time */ - else { - struct tm ts; - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0); - ts.tm_min = getfield(L, "min", 0); - ts.tm_hour = getfield(L, "hour", 12); - ts.tm_mday = getfield(L, "day", -1); - ts.tm_mon = getfield(L, "month", -1) - 1; - ts.tm_year = getfield(L, "year", -1) - 1900; - ts.tm_isdst = getboolfield(L, "isdst"); - t = mktime(&ts); - } - if (t == (time_t)(-1)) - lua_pushnil(L); - else - lua_pushnumber(L, (lua_Number)t); - return 1; -} - - -static int os_difftime (lua_State *L) { - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), - (time_t)(luaL_optnumber(L, 2, 0)))); - return 1; -} - -/* }====================================================== */ - - -static int os_setlocale (lua_State *L) { - static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, - LC_NUMERIC, LC_TIME}; - static const char *const catnames[] = {"all", "collate", "ctype", "monetary", - "numeric", "time", NULL}; - const char *l = luaL_optstring(L, 1, NULL); - int op = luaL_checkoption(L, 2, "all", catnames); - lua_pushstring(L, setlocale(cat[op], l)); - return 1; -} - - -static int os_exit (lua_State *L) { - int status; - if (lua_isboolean(L, 1)) - status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); - else - status = luaL_optint(L, 1, EXIT_SUCCESS); - if (lua_toboolean(L, 2)) - lua_close(L); - if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ - return 0; -} - - -static const luaL_Reg syslib[] = { - {"clock", os_clock}, - {"date", os_date}, - {"difftime", os_difftime}, - {"execute", os_execute}, - {"exit", os_exit}, - {"getenv", os_getenv}, - {"remove", os_remove}, - {"rename", os_rename}, - {"setlocale", os_setlocale}, - {"time", os_time}, - {"tmpname", os_tmpname}, - {NULL, NULL} -}; - -/* }====================================================== */ - - - -LUAMOD_API int luaopen_os (lua_State *L) { - luaL_newlib(L, syslib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.c deleted file mode 100644 index 9e1a9ca..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.c +++ /dev/null @@ -1,1638 +0,0 @@ -/* -** $Id: lparser.c,v 2.130.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - - -#include - -#define lparser_c -#define LUA_CORE - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" - - - -/* maximum number of local variables per function (must be smaller - than 250, due to the bytecode format) */ -#define MAXVARS 200 - - -#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) - - - -/* -** nodes for block list (list of active blocks) -*/ -typedef struct BlockCnt { - struct BlockCnt *previous; /* chain */ - short firstlabel; /* index of first label in this block */ - short firstgoto; /* index of first pending goto in this block */ - lu_byte nactvar; /* # active locals outside the block */ - lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if `block' is a loop */ -} BlockCnt; - - - -/* -** prototypes for recursive non-terminal functions -*/ -static void statement (LexState *ls); -static void expr (LexState *ls, expdesc *v); - - -static void anchor_token (LexState *ls) { - /* last token from outer function must be EOS */ - lua_assert(ls->fs != NULL || ls->t.token == TK_EOS); - if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { - TString *ts = ls->t.seminfo.ts; - luaX_newstring(ls, getstr(ts), ts->tsv.len); - } -} - - -/* semantic error */ -static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove 'near to' from final message */ - luaX_syntaxerror(ls, msg); -} - - -static l_noret error_expected (LexState *ls, int token) { - luaX_syntaxerror(ls, - luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); -} - - -static l_noret errorlimit (FuncState *fs, int limit, const char *what) { - lua_State *L = fs->ls->L; - const char *msg; - int line = fs->f->linedefined; - const char *where = (line == 0) - ? "main function" - : luaO_pushfstring(L, "function at line %d", line); - msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", - what, limit, where); - luaX_syntaxerror(fs->ls, msg); -} - - -static void checklimit (FuncState *fs, int v, int l, const char *what) { - if (v > l) errorlimit(fs, l, what); -} - - -static int testnext (LexState *ls, int c) { - if (ls->t.token == c) { - luaX_next(ls); - return 1; - } - else return 0; -} - - -static void check (LexState *ls, int c) { - if (ls->t.token != c) - error_expected(ls, c); -} - - -static void checknext (LexState *ls, int c) { - check(ls, c); - luaX_next(ls); -} - - -#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } - - - -static void check_match (LexState *ls, int what, int who, int where) { - if (!testnext(ls, what)) { - if (where == ls->linenumber) - error_expected(ls, what); - else { - luaX_syntaxerror(ls, luaO_pushfstring(ls->L, - "%s expected (to close %s at line %d)", - luaX_token2str(ls, what), luaX_token2str(ls, who), where)); - } - } -} - - -static TString *str_checkname (LexState *ls) { - TString *ts; - check(ls, TK_NAME); - ts = ls->t.seminfo.ts; - luaX_next(ls); - return ts; -} - - -static void init_exp (expdesc *e, expkind k, int i) { - e->f = e->t = NO_JUMP; - e->k = k; - e->u.info = i; -} - - -static void codestring (LexState *ls, expdesc *e, TString *s) { - init_exp(e, VK, luaK_stringK(ls->fs, s)); -} - - -static void checkname (LexState *ls, expdesc *e) { - codestring(ls, e, str_checkname(ls)); -} - - -static int registerlocalvar (LexState *ls, TString *varname) { - FuncState *fs = ls->fs; - Proto *f = fs->f; - int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, - LocVar, SHRT_MAX, "local variables"); - while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; - f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, f, varname); - return fs->nlocvars++; -} - - -static void new_localvar (LexState *ls, TString *name) { - FuncState *fs = ls->fs; - Dyndata *dyd = ls->dyd; - int reg = registerlocalvar(ls, name); - checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); - luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, MAX_INT, "local variables"); - dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); -} - - -static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { - new_localvar(ls, luaX_newstring(ls, name, sz)); -} - -#define new_localvarliteral(ls,v) \ - new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1) - - -static LocVar *getlocvar (FuncState *fs, int i) { - int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; - lua_assert(idx < fs->nlocvars); - return &fs->f->locvars[idx]; -} - - -static void adjustlocalvars (LexState *ls, int nvars) { - FuncState *fs = ls->fs; - fs->nactvar = cast_byte(fs->nactvar + nvars); - for (; nvars; nvars--) { - getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; - } -} - - -static void removevars (FuncState *fs, int tolevel) { - fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); - while (fs->nactvar > tolevel) - getlocvar(fs, --fs->nactvar)->endpc = fs->pc; -} - - -static int searchupvalue (FuncState *fs, TString *name) { - int i; - Upvaldesc *up = fs->f->upvalues; - for (i = 0; i < fs->nups; i++) { - if (luaS_eqstr(up[i].name, name)) return i; - } - return -1; /* not found */ -} - - -static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Proto *f = fs->f; - int oldsize = f->sizeupvalues; - checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); - luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, - Upvaldesc, MAXUPVAL, "upvalues"); - while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; - f->upvalues[fs->nups].instack = (v->k == VLOCAL); - f->upvalues[fs->nups].idx = cast_byte(v->u.info); - f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, f, name); - return fs->nups++; -} - - -static int searchvar (FuncState *fs, TString *n) { - int i; - for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (luaS_eqstr(n, getlocvar(fs, i)->varname)) - return i; - } - return -1; /* not found */ -} - - -/* - Mark block where variable at given level was defined - (to emit close instructions later). -*/ -static void markupval (FuncState *fs, int level) { - BlockCnt *bl = fs->bl; - while (bl->nactvar > level) bl = bl->previous; - bl->upval = 1; -} - - -/* - Find variable with given name 'n'. If it is an upvalue, add this - upvalue into all intermediate functions. -*/ -static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - if (fs == NULL) /* no more levels? */ - return VVOID; /* default is global */ - else { - int v = searchvar(fs, n); /* look up locals at current level */ - if (v >= 0) { /* found? */ - init_exp(var, VLOCAL, v); /* variable is local */ - if (!base) - markupval(fs, v); /* local will be used as an upval */ - return VLOCAL; - } - else { /* not found as local at current level; try upvalues */ - int idx = searchupvalue(fs, n); /* try existing upvalues */ - if (idx < 0) { /* not found? */ - if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */ - return VVOID; /* not found; is a global */ - /* else was LOCAL or UPVAL */ - idx = newupvalue(fs, n, var); /* will be a new upvalue */ - } - init_exp(var, VUPVAL, idx); - return VUPVAL; - } - } -} - - -static void singlevar (LexState *ls, expdesc *var) { - TString *varname = str_checkname(ls); - FuncState *fs = ls->fs; - if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */ - expdesc key; - singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k == VLOCAL || var->k == VUPVAL); - codestring(ls, &key, varname); /* key is variable name */ - luaK_indexed(fs, var, &key); /* env[varname] */ - } -} - - -static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { - FuncState *fs = ls->fs; - int extra = nvars - nexps; - if (hasmultret(e->k)) { - extra++; /* includes call itself */ - if (extra < 0) extra = 0; - luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ - if (extra > 1) luaK_reserveregs(fs, extra-1); - } - else { - if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ - if (extra > 0) { - int reg = fs->freereg; - luaK_reserveregs(fs, extra); - luaK_nil(fs, reg, extra); - } - } -} - - -static void enterlevel (LexState *ls) { - lua_State *L = ls->L; - ++L->nCcalls; - checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels"); -} - - -#define leavelevel(ls) ((ls)->L->nCcalls--) - - -static void closegoto (LexState *ls, int g, Labeldesc *label) { - int i; - FuncState *fs = ls->fs; - Labellist *gl = &ls->dyd->gt; - Labeldesc *gt = &gl->arr[g]; - lua_assert(luaS_eqstr(gt->name, label->name)); - if (gt->nactvar < label->nactvar) { - TString *vname = getlocvar(fs, gt->nactvar)->varname; - const char *msg = luaO_pushfstring(ls->L, - " at line %d jumps into the scope of local " LUA_QS, - getstr(gt->name), gt->line, getstr(vname)); - semerror(ls, msg); - } - luaK_patchlist(fs, gt->pc, label->pc); - /* remove goto from pending list */ - for (i = g; i < gl->n - 1; i++) - gl->arr[i] = gl->arr[i + 1]; - gl->n--; -} - - -/* -** try to close a goto with existing labels; this solves backward jumps -*/ -static int findlabel (LexState *ls, int g) { - int i; - BlockCnt *bl = ls->fs->bl; - Dyndata *dyd = ls->dyd; - Labeldesc *gt = &dyd->gt.arr[g]; - /* check labels in current block for a match */ - for (i = bl->firstlabel; i < dyd->label.n; i++) { - Labeldesc *lb = &dyd->label.arr[i]; - if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */ - if (gt->nactvar > lb->nactvar && - (bl->upval || dyd->label.n > bl->firstlabel)) - luaK_patchclose(ls->fs, gt->pc, lb->nactvar); - closegoto(ls, g, lb); /* close it */ - return 1; - } - } - return 0; /* label not found; cannot close goto */ -} - - -static int newlabelentry (LexState *ls, Labellist *l, TString *name, - int line, int pc) { - int n = l->n; - luaM_growvector(ls->L, l->arr, n, l->size, - Labeldesc, SHRT_MAX, "labels/gotos"); - l->arr[n].name = name; - l->arr[n].line = line; - l->arr[n].nactvar = ls->fs->nactvar; - l->arr[n].pc = pc; - l->n++; - return n; -} - - -/* -** check whether new label 'lb' matches any pending gotos in current -** block; solves forward jumps -*/ -static void findgotos (LexState *ls, Labeldesc *lb) { - Labellist *gl = &ls->dyd->gt; - int i = ls->fs->bl->firstgoto; - while (i < gl->n) { - if (luaS_eqstr(gl->arr[i].name, lb->name)) - closegoto(ls, i, lb); - else - i++; - } -} - - -/* -** "export" pending gotos to outer level, to check them against -** outer labels; if the block being exited has upvalues, and -** the goto exits the scope of any variable (which can be the -** upvalue), close those variables being exited. -*/ -static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i = bl->firstgoto; - Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block and try to close it - with visible labels */ - while (i < gl->n) { - Labeldesc *gt = &gl->arr[i]; - if (gt->nactvar > bl->nactvar) { - if (bl->upval) - luaK_patchclose(fs, gt->pc, bl->nactvar); - gt->nactvar = bl->nactvar; - } - if (!findlabel(fs->ls, i)) - i++; /* move to next one */ - } -} - - -static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { - bl->isloop = isloop; - bl->nactvar = fs->nactvar; - bl->firstlabel = fs->ls->dyd->label.n; - bl->firstgoto = fs->ls->dyd->gt.n; - bl->upval = 0; - bl->previous = fs->bl; - fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); -} - - -/* -** create a label named "break" to resolve break statements -*/ -static void breaklabel (LexState *ls) { - TString *n = luaS_new(ls->L, "break"); - int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc); - findgotos(ls, &ls->dyd->label.arr[l]); -} - -/* -** generates an error for an undefined 'goto'; choose appropriate -** message when label name is a reserved word (which can only be 'break') -*/ -static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg = isreserved(gt->name) - ? "<%s> at line %d not inside a loop" - : "no visible label " LUA_QS " for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - semerror(ls, msg); -} - - -static void leaveblock (FuncState *fs) { - BlockCnt *bl = fs->bl; - LexState *ls = fs->ls; - if (bl->previous && bl->upval) { - /* create a 'jump to here' to close upvalues */ - int j = luaK_jump(fs); - luaK_patchclose(fs, j, bl->nactvar); - luaK_patchtohere(fs, j); - } - if (bl->isloop) - breaklabel(ls); /* close pending breaks */ - fs->bl = bl->previous; - removevars(fs, bl->nactvar); - lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = fs->nactvar; /* free registers */ - ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - if (bl->previous) /* inner block? */ - movegotosout(fs, bl); /* update pending gotos to outer block */ - else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ - undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ -} - - -/* -** adds a new prototype into list of prototypes -*/ -static Proto *addprototype (LexState *ls) { - Proto *clp; - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sizep) { - int oldsize = f->sizep; - luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sizep) f->p[oldsize++] = NULL; - } - f->p[fs->np++] = clp = luaF_newproto(L); - luaC_objbarrier(L, f, clp); - return clp; -} - - -/* -** codes instruction to create new closure in parent function. -** The OP_CLOSURE instruction must use the last available register, -** so that, if it invokes the GC, the GC knows which registers -** are in use at that time. -*/ -static void codeclosure (LexState *ls, expdesc *v) { - FuncState *fs = ls->fs->prev; - init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); - luaK_exp2nextreg(fs, v); /* fix it at the last register */ -} - - -static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - lua_State *L = ls->L; - Proto *f; - fs->prev = ls->fs; /* linked list of funcstates */ - fs->ls = ls; - ls->fs = fs; - fs->pc = 0; - fs->lasttarget = 0; - fs->jpc = NO_JUMP; - fs->freereg = 0; - fs->nk = 0; - fs->np = 0; - fs->nups = 0; - fs->nlocvars = 0; - fs->nactvar = 0; - fs->firstlocal = ls->dyd->actvar.n; - fs->bl = NULL; - f = fs->f; - f->source = ls->source; - f->maxstacksize = 2; /* registers 0/1 are always valid */ - fs->h = luaH_new(L); - /* anchor table of constants (to avoid being collected) */ - sethvalue2s(L, L->top, fs->h); - incr_top(L); - enterblock(fs, bl, 0); -} - - -static void close_func (LexState *ls) { - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; - luaK_ret(fs, 0, 0); /* final return */ - leaveblock(fs); - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); - f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); - f->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); - f->sizek = fs->nk; - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); - f->sizep = fs->np; - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); - f->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - f->sizeupvalues = fs->nups; - lua_assert(fs->bl == NULL); - ls->fs = fs->prev; - /* last token read was anchored in defunct function; must re-anchor it */ - anchor_token(ls); - L->top--; /* pop table of constants */ - luaC_checkGC(L); -} - - - -/*============================================================*/ -/* GRAMMAR RULES */ -/*============================================================*/ - - -/* -** check whether current token is in the follow set of a block. -** 'until' closes syntactical blocks, but do not close scope, -** so it handled in separate. -*/ -static int block_follow (LexState *ls, int withuntil) { - switch (ls->t.token) { - case TK_ELSE: case TK_ELSEIF: - case TK_END: case TK_EOS: - return 1; - case TK_UNTIL: return withuntil; - default: return 0; - } -} - - -static void statlist (LexState *ls) { - /* statlist -> { stat [`;'] } */ - while (!block_follow(ls, 1)) { - if (ls->t.token == TK_RETURN) { - statement(ls); - return; /* 'return' must be last statement */ - } - statement(ls); - } -} - - -static void fieldsel (LexState *ls, expdesc *v) { - /* fieldsel -> ['.' | ':'] NAME */ - FuncState *fs = ls->fs; - expdesc key; - luaK_exp2anyregup(fs, v); - luaX_next(ls); /* skip the dot or colon */ - checkname(ls, &key); - luaK_indexed(fs, v, &key); -} - - -static void yindex (LexState *ls, expdesc *v) { - /* index -> '[' expr ']' */ - luaX_next(ls); /* skip the '[' */ - expr(ls, v); - luaK_exp2val(ls->fs, v); - checknext(ls, ']'); -} - - -/* -** {====================================================================== -** Rules for Constructors -** ======================================================================= -*/ - - -struct ConsControl { - expdesc v; /* last list item read */ - expdesc *t; /* table descriptor */ - int nh; /* total number of `record' elements */ - int na; /* total number of array elements */ - int tostore; /* number of array elements pending to be stored */ -}; - - -static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | `['exp1`]') = exp1 */ - FuncState *fs = ls->fs; - int reg = ls->fs->freereg; - expdesc key, val; - int rkkey; - if (ls->t.token == TK_NAME) { - checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); - checkname(ls, &key); - } - else /* ls->t.token == '[' */ - yindex(ls, &key); - cc->nh++; - checknext(ls, '='); - rkkey = luaK_exp2RK(fs, &key); - expr(ls, &val); - luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val)); - fs->freereg = reg; /* free registers */ -} - - -static void closelistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->v.k == VVOID) return; /* there is no list item */ - luaK_exp2nextreg(fs, &cc->v); - cc->v.k = VVOID; - if (cc->tostore == LFIELDS_PER_FLUSH) { - luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ - cc->tostore = 0; /* no more items pending */ - } -} - - -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->tostore == 0) return; - if (hasmultret(cc->v.k)) { - luaK_setmultret(fs, &cc->v); - luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); - cc->na--; /* do not count last expression (unknown number of elements) */ - } - else { - if (cc->v.k != VVOID) - luaK_exp2nextreg(fs, &cc->v); - luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); - } -} - - -static void listfield (LexState *ls, struct ConsControl *cc) { - /* listfield -> exp */ - expr(ls, &cc->v); - checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; - cc->tostore++; -} - - -static void field (LexState *ls, struct ConsControl *cc) { - /* field -> listfield | recfield */ - switch(ls->t.token) { - case TK_NAME: { /* may be 'listfield' or 'recfield' */ - if (luaX_lookahead(ls) != '=') /* expression? */ - listfield(ls, cc); - else - recfield(ls, cc); - break; - } - case '[': { - recfield(ls, cc); - break; - } - default: { - listfield(ls, cc); - break; - } - } -} - - -static void constructor (LexState *ls, expdesc *t) { - /* constructor -> '{' [ field { sep field } [sep] ] '}' - sep -> ',' | ';' */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - struct ConsControl cc; - cc.na = cc.nh = cc.tostore = 0; - cc.t = t; - init_exp(t, VRELOCABLE, pc); - init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ - checknext(ls, '{'); - do { - lua_assert(cc.v.k == VVOID || cc.tostore > 0); - if (ls->t.token == '}') break; - closelistfield(fs, &cc); - field(ls, &cc); - } while (testnext(ls, ',') || testnext(ls, ';')); - check_match(ls, '}', '{', line); - lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ -} - -/* }====================================================================== */ - - - -static void parlist (LexState *ls) { - /* parlist -> [ param { `,' param } ] */ - FuncState *fs = ls->fs; - Proto *f = fs->f; - int nparams = 0; - f->is_vararg = 0; - if (ls->t.token != ')') { /* is `parlist' not empty? */ - do { - switch (ls->t.token) { - case TK_NAME: { /* param -> NAME */ - new_localvar(ls, str_checkname(ls)); - nparams++; - break; - } - case TK_DOTS: { /* param -> `...' */ - luaX_next(ls); - f->is_vararg = 1; - break; - } - default: luaX_syntaxerror(ls, " or " LUA_QL("...") " expected"); - } - } while (!f->is_vararg && testnext(ls, ',')); - } - adjustlocalvars(ls, nparams); - f->numparams = cast_byte(fs->nactvar); - luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ -} - - -static void body (LexState *ls, expdesc *e, int ismethod, int line) { - /* body -> `(' parlist `)' block END */ - FuncState new_fs; - BlockCnt bl; - new_fs.f = addprototype(ls); - new_fs.f->linedefined = line; - open_func(ls, &new_fs, &bl); - checknext(ls, '('); - if (ismethod) { - new_localvarliteral(ls, "self"); /* create 'self' parameter */ - adjustlocalvars(ls, 1); - } - parlist(ls); - checknext(ls, ')'); - statlist(ls); - new_fs.f->lastlinedefined = ls->linenumber; - check_match(ls, TK_END, TK_FUNCTION, line); - codeclosure(ls, e); - close_func(ls); -} - - -static int explist (LexState *ls, expdesc *v) { - /* explist -> expr { `,' expr } */ - int n = 1; /* at least one expression */ - expr(ls, v); - while (testnext(ls, ',')) { - luaK_exp2nextreg(ls->fs, v); - expr(ls, v); - n++; - } - return n; -} - - -static void funcargs (LexState *ls, expdesc *f, int line) { - FuncState *fs = ls->fs; - expdesc args; - int base, nparams; - switch (ls->t.token) { - case '(': { /* funcargs -> `(' [ explist ] `)' */ - luaX_next(ls); - if (ls->t.token == ')') /* arg list is empty? */ - args.k = VVOID; - else { - explist(ls, &args); - luaK_setmultret(fs, &args); - } - check_match(ls, ')', '(', line); - break; - } - case '{': { /* funcargs -> constructor */ - constructor(ls, &args); - break; - } - case TK_STRING: { /* funcargs -> STRING */ - codestring(ls, &args, ls->t.seminfo.ts); - luaX_next(ls); /* must use `seminfo' before `next' */ - break; - } - default: { - luaX_syntaxerror(ls, "function arguments expected"); - } - } - lua_assert(f->k == VNONRELOC); - base = f->u.info; /* base register for call */ - if (hasmultret(args.k)) - nparams = LUA_MULTRET; /* open call */ - else { - if (args.k != VVOID) - luaK_exp2nextreg(fs, &args); /* close last argument */ - nparams = fs->freereg - (base+1); - } - init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); - luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ -} - - - - -/* -** {====================================================================== -** Expression parsing -** ======================================================================= -*/ - - -static void primaryexp (LexState *ls, expdesc *v) { - /* primaryexp -> NAME | '(' expr ')' */ - switch (ls->t.token) { - case '(': { - int line = ls->linenumber; - luaX_next(ls); - expr(ls, v); - check_match(ls, ')', '(', line); - luaK_dischargevars(ls->fs, v); - return; - } - case TK_NAME: { - singlevar(ls, v); - return; - } - default: { - luaX_syntaxerror(ls, "unexpected symbol"); - } - } -} - - -static void suffixedexp (LexState *ls, expdesc *v) { - /* suffixedexp -> - primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - primaryexp(ls, v); - for (;;) { - switch (ls->t.token) { - case '.': { /* fieldsel */ - fieldsel(ls, v); - break; - } - case '[': { /* `[' exp1 `]' */ - expdesc key; - luaK_exp2anyregup(fs, v); - yindex(ls, &key); - luaK_indexed(fs, v, &key); - break; - } - case ':': { /* `:' NAME funcargs */ - expdesc key; - luaX_next(ls); - checkname(ls, &key); - luaK_self(fs, v, &key); - funcargs(ls, v, line); - break; - } - case '(': case TK_STRING: case '{': { /* funcargs */ - luaK_exp2nextreg(fs, v); - funcargs(ls, v, line); - break; - } - default: return; - } - } -} - - -static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | - constructor | FUNCTION body | suffixedexp */ - switch (ls->t.token) { - case TK_NUMBER: { - init_exp(v, VKNUM, 0); - v->u.nval = ls->t.seminfo.r; - break; - } - case TK_STRING: { - codestring(ls, v, ls->t.seminfo.ts); - break; - } - case TK_NIL: { - init_exp(v, VNIL, 0); - break; - } - case TK_TRUE: { - init_exp(v, VTRUE, 0); - break; - } - case TK_FALSE: { - init_exp(v, VFALSE, 0); - break; - } - case TK_DOTS: { /* vararg */ - FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, - "cannot use " LUA_QL("...") " outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); - break; - } - case '{': { /* constructor */ - constructor(ls, v); - return; - } - case TK_FUNCTION: { - luaX_next(ls); - body(ls, v, 0, ls->linenumber); - return; - } - default: { - suffixedexp(ls, v); - return; - } - } - luaX_next(ls); -} - - -static UnOpr getunopr (int op) { - switch (op) { - case TK_NOT: return OPR_NOT; - case '-': return OPR_MINUS; - case '#': return OPR_LEN; - default: return OPR_NOUNOPR; - } -} - - -static BinOpr getbinopr (int op) { - switch (op) { - case '+': return OPR_ADD; - case '-': return OPR_SUB; - case '*': return OPR_MUL; - case '/': return OPR_DIV; - case '%': return OPR_MOD; - case '^': return OPR_POW; - case TK_CONCAT: return OPR_CONCAT; - case TK_NE: return OPR_NE; - case TK_EQ: return OPR_EQ; - case '<': return OPR_LT; - case TK_LE: return OPR_LE; - case '>': return OPR_GT; - case TK_GE: return OPR_GE; - case TK_AND: return OPR_AND; - case TK_OR: return OPR_OR; - default: return OPR_NOBINOPR; - } -} - - -static const struct { - lu_byte left; /* left priority for each binary operator */ - lu_byte right; /* right priority */ -} priority[] = { /* ORDER OPR */ - {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `*' `/' `%' */ - {10, 9}, {5, 4}, /* ^, .. (right associative) */ - {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ - {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ - {2, 2}, {1, 1} /* and, or */ -}; - -#define UNARY_PRIORITY 8 /* priority for unary operators */ - - -/* -** subexpr -> (simpleexp | unop subexpr) { binop subexpr } -** where `binop' is any binary operator with a priority higher than `limit' -*/ -static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { - BinOpr op; - UnOpr uop; - enterlevel(ls); - uop = getunopr(ls->t.token); - if (uop != OPR_NOUNOPR) { - int line = ls->linenumber; - luaX_next(ls); - subexpr(ls, v, UNARY_PRIORITY); - luaK_prefix(ls->fs, uop, v, line); - } - else simpleexp(ls, v); - /* expand while operators have priorities higher than `limit' */ - op = getbinopr(ls->t.token); - while (op != OPR_NOBINOPR && priority[op].left > limit) { - expdesc v2; - BinOpr nextop; - int line = ls->linenumber; - luaX_next(ls); - luaK_infix(ls->fs, op, v); - /* read sub-expression with higher priority */ - nextop = subexpr(ls, &v2, priority[op].right); - luaK_posfix(ls->fs, op, v, &v2, line); - op = nextop; - } - leavelevel(ls); - return op; /* return first untreated operator */ -} - - -static void expr (LexState *ls, expdesc *v) { - subexpr(ls, v, 0); -} - -/* }==================================================================== */ - - - -/* -** {====================================================================== -** Rules for Statements -** ======================================================================= -*/ - - -static void block (LexState *ls) { - /* block -> statlist */ - FuncState *fs = ls->fs; - BlockCnt bl; - enterblock(fs, &bl, 0); - statlist(ls); - leaveblock(fs); -} - - -/* -** structure to chain all variables in the left-hand side of an -** assignment -*/ -struct LHS_assign { - struct LHS_assign *prev; - expdesc v; /* variable (global, local, upvalue, or indexed) */ -}; - - -/* -** check whether, in an assignment to an upvalue/local variable, the -** upvalue/local variable is begin used in a previous assignment to a -** table. If so, save original upvalue/local value in a safe place and -** use this safe copy in the previous assignment. -*/ -static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { - FuncState *fs = ls->fs; - int extra = fs->freereg; /* eventual position to save local variable */ - int conflict = 0; - for (; lh; lh = lh->prev) { /* check all previous assignments */ - if (lh->v.k == VINDEXED) { /* assigning to a table? */ - /* table is the upvalue/local being assigned now? */ - if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) { - conflict = 1; - lh->v.u.ind.vt = VLOCAL; - lh->v.u.ind.t = extra; /* previous assignment will use safe copy */ - } - /* index is the local being assigned? (index cannot be upvalue) */ - if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) { - conflict = 1; - lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ - } - } - } - if (conflict) { - /* copy upvalue/local value to a temporary (in position 'extra') */ - OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; - luaK_codeABC(fs, op, extra, v->u.info, 0); - luaK_reserveregs(fs, 1); - } -} - - -static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { - expdesc e; - check_condition(ls, vkisvar(lh->v.k), "syntax error"); - if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */ - struct LHS_assign nv; - nv.prev = lh; - suffixedexp(ls, &nv.v); - if (nv.v.k != VINDEXED) - check_conflict(ls, lh, &nv.v); - checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, - "C levels"); - assignment(ls, &nv, nvars+1); - } - else { /* assignment -> `=' explist */ - int nexps; - checknext(ls, '='); - nexps = explist(ls, &e); - if (nexps != nvars) { - adjust_assign(ls, nvars, nexps, &e); - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ - } - else { - luaK_setoneret(ls->fs, &e); /* close last expression */ - luaK_storevar(ls->fs, &lh->v, &e); - return; /* avoid default */ - } - } - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ - luaK_storevar(ls->fs, &lh->v, &e); -} - - -static int cond (LexState *ls) { - /* cond -> exp */ - expdesc v; - expr(ls, &v); /* read condition */ - if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ - luaK_goiftrue(ls->fs, &v); - return v.f; -} - - -static void gotostat (LexState *ls, int pc) { - int line = ls->linenumber; - TString *label; - int g; - if (testnext(ls, TK_GOTO)) - label = str_checkname(ls); - else { - luaX_next(ls); /* skip break */ - label = luaS_new(ls->L, "break"); - } - g = newlabelentry(ls, &ls->dyd->gt, label, line, pc); - findlabel(ls, g); /* close it if label already defined */ -} - - -/* check for repeated labels on the same block */ -static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { - int i; - for (i = fs->bl->firstlabel; i < ll->n; i++) { - if (luaS_eqstr(label, ll->arr[i].name)) { - const char *msg = luaO_pushfstring(fs->ls->L, - "label " LUA_QS " already defined on line %d", - getstr(label), ll->arr[i].line); - semerror(fs->ls, msg); - } - } -} - - -/* skip no-op statements */ -static void skipnoopstat (LexState *ls) { - while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) - statement(ls); -} - - -static void labelstat (LexState *ls, TString *label, int line) { - /* label -> '::' NAME '::' */ - FuncState *fs = ls->fs; - Labellist *ll = &ls->dyd->label; - int l; /* index of new label being created */ - checkrepeated(fs, ll, label); /* check for repeated labels */ - checknext(ls, TK_DBCOLON); /* skip double colon */ - /* create new entry for this label */ - l = newlabelentry(ls, ll, label, line, fs->pc); - skipnoopstat(ls); /* skip other no-op statements */ - if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ - /* assume that locals are already out of scope */ - ll->arr[l].nactvar = fs->bl->nactvar; - } - findgotos(ls, &ll->arr[l]); -} - - -static void whilestat (LexState *ls, int line) { - /* whilestat -> WHILE cond DO block END */ - FuncState *fs = ls->fs; - int whileinit; - int condexit; - BlockCnt bl; - luaX_next(ls); /* skip WHILE */ - whileinit = luaK_getlabel(fs); - condexit = cond(ls); - enterblock(fs, &bl, 1); - checknext(ls, TK_DO); - block(ls); - luaK_jumpto(fs, whileinit); - check_match(ls, TK_END, TK_WHILE, line); - leaveblock(fs); - luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ -} - - -static void repeatstat (LexState *ls, int line) { - /* repeatstat -> REPEAT block UNTIL cond */ - int condexit; - FuncState *fs = ls->fs; - int repeat_init = luaK_getlabel(fs); - BlockCnt bl1, bl2; - enterblock(fs, &bl1, 1); /* loop block */ - enterblock(fs, &bl2, 0); /* scope block */ - luaX_next(ls); /* skip REPEAT */ - statlist(ls); - check_match(ls, TK_UNTIL, TK_REPEAT, line); - condexit = cond(ls); /* read condition (inside scope block) */ - if (bl2.upval) /* upvalues? */ - luaK_patchclose(fs, condexit, bl2.nactvar); - leaveblock(fs); /* finish scope */ - luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ - leaveblock(fs); /* finish loop */ -} - - -static int exp1 (LexState *ls) { - expdesc e; - int reg; - expr(ls, &e); - luaK_exp2nextreg(ls->fs, &e); - lua_assert(e.k == VNONRELOC); - reg = e.u.info; - return reg; -} - - -static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { - /* forbody -> DO block */ - BlockCnt bl; - FuncState *fs = ls->fs; - int prep, endfor; - adjustlocalvars(ls, 3); /* control variables */ - checknext(ls, TK_DO); - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); - enterblock(fs, &bl, 0); /* scope for declared variables */ - adjustlocalvars(ls, nvars); - luaK_reserveregs(fs, nvars); - block(ls); - leaveblock(fs); /* end of scope for declared variables */ - luaK_patchtohere(fs, prep); - if (isnum) /* numeric for? */ - endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); - else { /* generic for */ - luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); - luaK_fixline(fs, line); - endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); - } - luaK_patchlist(fs, endfor, prep + 1); - luaK_fixline(fs, line); -} - - -static void fornum (LexState *ls, TString *varname, int line) { - /* fornum -> NAME = exp1,exp1[,exp1] forbody */ - FuncState *fs = ls->fs; - int base = fs->freereg; - new_localvarliteral(ls, "(for index)"); - new_localvarliteral(ls, "(for limit)"); - new_localvarliteral(ls, "(for step)"); - new_localvar(ls, varname); - checknext(ls, '='); - exp1(ls); /* initial value */ - checknext(ls, ','); - exp1(ls); /* limit */ - if (testnext(ls, ',')) - exp1(ls); /* optional step */ - else { /* default step = 1 */ - luaK_codek(fs, fs->freereg, luaK_numberK(fs, 1)); - luaK_reserveregs(fs, 1); - } - forbody(ls, base, line, 1, 1); -} - - -static void forlist (LexState *ls, TString *indexname) { - /* forlist -> NAME {,NAME} IN explist forbody */ - FuncState *fs = ls->fs; - expdesc e; - int nvars = 4; /* gen, state, control, plus at least one declared var */ - int line; - int base = fs->freereg; - /* create control variables */ - new_localvarliteral(ls, "(for generator)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for control)"); - /* create declared variables */ - new_localvar(ls, indexname); - while (testnext(ls, ',')) { - new_localvar(ls, str_checkname(ls)); - nvars++; - } - checknext(ls, TK_IN); - line = ls->linenumber; - adjust_assign(ls, 3, explist(ls, &e), &e); - luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 3, 0); -} - - -static void forstat (LexState *ls, int line) { - /* forstat -> FOR (fornum | forlist) END */ - FuncState *fs = ls->fs; - TString *varname; - BlockCnt bl; - enterblock(fs, &bl, 1); /* scope for loop and control variables */ - luaX_next(ls); /* skip `for' */ - varname = str_checkname(ls); /* first variable name */ - switch (ls->t.token) { - case '=': fornum(ls, varname, line); break; - case ',': case TK_IN: forlist(ls, varname); break; - default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); - } - check_match(ls, TK_END, TK_FOR, line); - leaveblock(fs); /* loop scope (`break' jumps to this point) */ -} - - -static void test_then_block (LexState *ls, int *escapelist) { - /* test_then_block -> [IF | ELSEIF] cond THEN block */ - BlockCnt bl; - FuncState *fs = ls->fs; - expdesc v; - int jf; /* instruction to skip 'then' code (if condition is false) */ - luaX_next(ls); /* skip IF or ELSEIF */ - expr(ls, &v); /* read condition */ - checknext(ls, TK_THEN); - if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - gotostat(ls, v.t); /* handle goto/break */ - skipnoopstat(ls); /* skip other no-op statements */ - if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ - leaveblock(fs); - return; /* and that is it */ - } - else /* must skip over 'then' part if condition is false */ - jf = luaK_jump(fs); - } - else { /* regular case (not goto/break) */ - luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ - enterblock(fs, &bl, 0); - jf = v.f; - } - statlist(ls); /* `then' part */ - leaveblock(fs); - if (ls->t.token == TK_ELSE || - ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ - luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ - luaK_patchtohere(fs, jf); -} - - -static void ifstat (LexState *ls, int line) { - /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ - FuncState *fs = ls->fs; - int escapelist = NO_JUMP; /* exit list for finished parts */ - test_then_block(ls, &escapelist); /* IF cond THEN block */ - while (ls->t.token == TK_ELSEIF) - test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ - if (testnext(ls, TK_ELSE)) - block(ls); /* `else' part */ - check_match(ls, TK_END, TK_IF, line); - luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ -} - - -static void localfunc (LexState *ls) { - expdesc b; - FuncState *fs = ls->fs; - new_localvar(ls, str_checkname(ls)); /* new local variable */ - adjustlocalvars(ls, 1); /* enter its scope */ - body(ls, &b, 0, ls->linenumber); /* function created in next register */ - /* debug information will only see the variable after this point! */ - getlocvar(fs, b.u.info)->startpc = fs->pc; -} - - -static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */ - int nvars = 0; - int nexps; - expdesc e; - do { - new_localvar(ls, str_checkname(ls)); - nvars++; - } while (testnext(ls, ',')); - if (testnext(ls, '=')) - nexps = explist(ls, &e); - else { - e.k = VVOID; - nexps = 0; - } - adjust_assign(ls, nvars, nexps, &e); - adjustlocalvars(ls, nvars); -} - - -static int funcname (LexState *ls, expdesc *v) { - /* funcname -> NAME {fieldsel} [`:' NAME] */ - int ismethod = 0; - singlevar(ls, v); - while (ls->t.token == '.') - fieldsel(ls, v); - if (ls->t.token == ':') { - ismethod = 1; - fieldsel(ls, v); - } - return ismethod; -} - - -static void funcstat (LexState *ls, int line) { - /* funcstat -> FUNCTION funcname body */ - int ismethod; - expdesc v, b; - luaX_next(ls); /* skip FUNCTION */ - ismethod = funcname(ls, &v); - body(ls, &b, ismethod, line); - luaK_storevar(ls->fs, &v, &b); - luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ -} - - -static void exprstat (LexState *ls) { - /* stat -> func | assignment */ - FuncState *fs = ls->fs; - struct LHS_assign v; - suffixedexp(ls, &v.v); - if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ - v.prev = NULL; - assignment(ls, &v, 1); - } - else { /* stat -> func */ - check_condition(ls, v.v.k == VCALL, "syntax error"); - SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ - } -} - - -static void retstat (LexState *ls) { - /* stat -> RETURN [explist] [';'] */ - FuncState *fs = ls->fs; - expdesc e; - int first, nret; /* registers with returned values */ - if (block_follow(ls, 1) || ls->t.token == ';') - first = nret = 0; /* return no values */ - else { - nret = explist(ls, &e); /* optional return values */ - if (hasmultret(e.k)) { - luaK_setmultret(fs, &e); - if (e.k == VCALL && nret == 1) { /* tail call? */ - SET_OPCODE(getcode(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); - } - first = fs->nactvar; - nret = LUA_MULTRET; /* return all values */ - } - else { - if (nret == 1) /* only one single value? */ - first = luaK_exp2anyreg(fs, &e); - else { - luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ - first = fs->nactvar; /* return all `active' values */ - lua_assert(nret == fs->freereg - first); - } - } - } - luaK_ret(fs, first, nret); - testnext(ls, ';'); /* skip optional semicolon */ -} - - -static void statement (LexState *ls) { - int line = ls->linenumber; /* may be needed for error messages */ - enterlevel(ls); - switch (ls->t.token) { - case ';': { /* stat -> ';' (empty statement) */ - luaX_next(ls); /* skip ';' */ - break; - } - case TK_IF: { /* stat -> ifstat */ - ifstat(ls, line); - break; - } - case TK_WHILE: { /* stat -> whilestat */ - whilestat(ls, line); - break; - } - case TK_DO: { /* stat -> DO block END */ - luaX_next(ls); /* skip DO */ - block(ls); - check_match(ls, TK_END, TK_DO, line); - break; - } - case TK_FOR: { /* stat -> forstat */ - forstat(ls, line); - break; - } - case TK_REPEAT: { /* stat -> repeatstat */ - repeatstat(ls, line); - break; - } - case TK_FUNCTION: { /* stat -> funcstat */ - funcstat(ls, line); - break; - } - case TK_LOCAL: { /* stat -> localstat */ - luaX_next(ls); /* skip LOCAL */ - if (testnext(ls, TK_FUNCTION)) /* local function? */ - localfunc(ls); - else - localstat(ls); - break; - } - case TK_DBCOLON: { /* stat -> label */ - luaX_next(ls); /* skip double colon */ - labelstat(ls, str_checkname(ls), line); - break; - } - case TK_RETURN: { /* stat -> retstat */ - luaX_next(ls); /* skip RETURN */ - retstat(ls); - break; - } - case TK_BREAK: /* stat -> breakstat */ - case TK_GOTO: { /* stat -> 'goto' NAME */ - gotostat(ls, luaK_jump(ls->fs)); - break; - } - default: { /* stat -> func | assignment */ - exprstat(ls); - break; - } - } - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* free registers */ - leavelevel(ls); -} - -/* }====================================================================== */ - - -/* -** compiles the main function, which is a regular vararg function with an -** upvalue named LUA_ENV -*/ -static void mainfunc (LexState *ls, FuncState *fs) { - BlockCnt bl; - expdesc v; - open_func(ls, fs, &bl); - fs->f->is_vararg = 1; /* main function is always vararg */ - init_exp(&v, VLOCAL, 0); /* create and... */ - newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ - luaX_next(ls); /* read first token */ - statlist(ls); /* parse main body */ - check(ls, TK_EOS); - close_func(ls); -} - - -Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar) { - LexState lexstate; - FuncState funcstate; - Closure *cl = luaF_newLclosure(L, 1); /* create main closure */ - /* anchor closure (to avoid being collected) */ - setclLvalue(L, L->top, cl); - incr_top(L); - funcstate.f = cl->l.p = luaF_newproto(L); - funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ - lexstate.buff = buff; - lexstate.dyd = dyd; - dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); - mainfunc(&lexstate, &funcstate); - lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); - /* all scopes should be correctly finished */ - lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - return cl; /* it's on the stack too */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.h deleted file mode 100644 index 0346e3c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lparser.h +++ /dev/null @@ -1,119 +0,0 @@ -/* -** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - -#ifndef lparser_h -#define lparser_h - -#include "llimits.h" -#include "lobject.h" -#include "lzio.h" - - -/* -** Expression descriptor -*/ - -typedef enum { - VVOID, /* no value */ - VNIL, - VTRUE, - VFALSE, - VK, /* info = index of constant in `k' */ - VKNUM, /* nval = numerical value */ - VNONRELOC, /* info = result register */ - VLOCAL, /* info = local register */ - VUPVAL, /* info = index of upvalue in 'upvalues' */ - VINDEXED, /* t = table register/upvalue; idx = index R/K */ - VJMP, /* info = instruction pc */ - VRELOCABLE, /* info = instruction pc */ - VCALL, /* info = instruction pc */ - VVARARG /* info = instruction pc */ -} expkind; - - -#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) -#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) - -typedef struct expdesc { - expkind k; - union { - struct { /* for indexed variables (VINDEXED) */ - short idx; /* index (R/K) */ - lu_byte t; /* table (register or upvalue) */ - lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ - } ind; - int info; /* for generic use */ - lua_Number nval; /* for VKNUM */ - } u; - int t; /* patch list of `exit when true' */ - int f; /* patch list of `exit when false' */ -} expdesc; - - -/* description of active local variable */ -typedef struct Vardesc { - short idx; /* variable index in stack */ -} Vardesc; - - -/* description of pending goto statements and label statements */ -typedef struct Labeldesc { - TString *name; /* label identifier */ - int pc; /* position in code */ - int line; /* line where it appeared */ - lu_byte nactvar; /* local level where it appears in current block */ -} Labeldesc; - - -/* list of labels or gotos */ -typedef struct Labellist { - Labeldesc *arr; /* array */ - int n; /* number of entries in use */ - int size; /* array size */ -} Labellist; - - -/* dynamic structures used by the parser */ -typedef struct Dyndata { - struct { /* list of active local variables */ - Vardesc *arr; - int n; - int size; - } actvar; - Labellist gt; /* list of pending gotos */ - Labellist label; /* list of active labels */ -} Dyndata; - - -/* control of blocks */ -struct BlockCnt; /* defined in lparser.c */ - - -/* state needed to generate code for a given function */ -typedef struct FuncState { - Proto *f; /* current function header */ - Table *h; /* table to find (and reuse) elements in `k' */ - struct FuncState *prev; /* enclosing function */ - struct LexState *ls; /* lexical state */ - struct BlockCnt *bl; /* chain of current blocks */ - int pc; /* next position to code (equivalent to `ncode') */ - int lasttarget; /* 'label' of last 'jump label' */ - int jpc; /* list of pending jumps to `pc' */ - int nk; /* number of elements in `k' */ - int np; /* number of elements in `p' */ - int firstlocal; /* index of first local var (in Dyndata array) */ - short nlocvars; /* number of elements in 'f->locvars' */ - lu_byte nactvar; /* number of active local variables */ - lu_byte nups; /* number of upvalues */ - lu_byte freereg; /* first free register */ -} FuncState; - - -LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.c deleted file mode 100644 index c7f2672..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.c +++ /dev/null @@ -1,323 +0,0 @@ -/* -** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $ -** Global State -** See Copyright Notice in lua.h -*/ - - -#include -#include - -#define lstate_c -#define LUA_CORE - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -#if !defined(LUAI_GCPAUSE) -#define LUAI_GCPAUSE 200 /* 200% */ -#endif - -#if !defined(LUAI_GCMAJOR) -#define LUAI_GCMAJOR 200 /* 200% */ -#endif - -#if !defined(LUAI_GCMUL) -#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ -#endif - - -#define MEMERRMSG "not enough memory" - - -/* -** a macro to help the creation of a unique random seed when a state is -** created; the seed is used to randomize hashes. -*/ -#if !defined(luai_makeseed) -#include -#define luai_makeseed() cast(unsigned int, time(NULL)) -#endif - - - -/* -** thread state + extra space -*/ -typedef struct LX { -#if defined(LUAI_EXTRASPACE) - char buff[LUAI_EXTRASPACE]; -#endif - lua_State l; -} LX; - - -/* -** Main thread combines a thread state and the global state -*/ -typedef struct LG { - LX l; - global_State g; -} LG; - - - -#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) - - -/* -** Compute an initial seed as random as possible. In ANSI, rely on -** Address Space Layout Randomization (if present) to increase -** randomness.. -*/ -#define addbuff(b,p,e) \ - { size_t t = cast(size_t, e); \ - memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int makeseed (lua_State *L) { - char buff[4 * sizeof(size_t)]; - unsigned int h = luai_makeseed(); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, luaO_nilobject); /* global variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} - - -/* -** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant -*/ -void luaE_setdebt (global_State *g, l_mem debt) { - g->totalbytes -= (debt - g->GCdebt); - g->GCdebt = debt; -} - - -CallInfo *luaE_extendCI (lua_State *L) { - CallInfo *ci = luaM_new(L, CallInfo); - lua_assert(L->ci->next == NULL); - L->ci->next = ci; - ci->previous = L->ci; - ci->next = NULL; - return ci; -} - - -void luaE_freeCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next = ci->next; - ci->next = NULL; - while ((ci = next) != NULL) { - next = ci->next; - luaM_free(L, ci); - } -} - - -static void stack_init (lua_State *L1, lua_State *L) { - int i; CallInfo *ci; - /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); - L1->stacksize = BASIC_STACK_SIZE; - for (i = 0; i < BASIC_STACK_SIZE; i++) - setnilvalue(L1->stack + i); /* erase new stack */ - L1->top = L1->stack; - L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; - /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - ci->callstatus = 0; - ci->func = L1->top; - setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ - ci->top = L1->top + LUA_MINSTACK; - L1->ci = ci; -} - - -static void freestack (lua_State *L) { - if (L->stack == NULL) - return; /* stack not completely built yet */ - L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); - luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ -} - - -/* -** Create registry table and its predefined values -*/ -static void init_registry (lua_State *L, global_State *g) { - TValue mt; - /* create registry */ - Table *registry = luaH_new(L); - sethvalue(L, &g->l_registry, registry); - luaH_resize(L, registry, LUA_RIDX_LAST, 0); - /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, &mt, L); - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); - /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &mt, luaH_new(L)); - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); -} - - -/* -** open parts of the state that may cause memory-allocation errors -*/ -static void f_luaopen (lua_State *L, void *ud) { - global_State *g = G(L); - UNUSED(ud); - stack_init(L, L); /* init stack */ - init_registry(L, g); - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ - luaT_init(L); - luaX_init(L); - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaS_fix(g->memerrmsg); /* it should never be collected */ - g->gcrunning = 1; /* allow gc */ - g->version = lua_version(NULL); - luai_userstateopen(L); -} - - -/* -** preinitialize a state with consistent values without allocating -** any memory (to avoid errors) -*/ -static void preinit_state (lua_State *L, global_State *g) { - G(L) = g; - L->stack = NULL; - L->ci = NULL; - L->stacksize = 0; - L->errorJmp = NULL; - L->nCcalls = 0; - L->hook = NULL; - L->hookmask = 0; - L->basehookcount = 0; - L->allowhook = 1; - resethookcount(L); - L->openupval = NULL; - L->nny = 1; - L->status = LUA_OK; - L->errfunc = 0; -} - - -static void close_state (lua_State *L) { - global_State *g = G(L); - luaF_close(L, L->stack); /* close all upvalues for this thread */ - luaC_freeallobjects(L); /* collect all objects */ - if (g->version) /* closing a fully built state? */ - luai_userstateclose(L); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); - luaZ_freebuffer(L, &g->buff); - freestack(L); - lua_assert(gettotalbytes(g) == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ -} - - -LUA_API lua_State *lua_newthread (lua_State *L) { - lua_State *L1; - lua_lock(L); - luaC_checkGC(L); - L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; - setthvalue(L, L->top, L1); - api_incr_top(L); - preinit_state(L1, G(L)); - L1->hookmask = L->hookmask; - L1->basehookcount = L->basehookcount; - L1->hook = L->hook; - resethookcount(L1); - luai_userstatethread(L, L1); - stack_init(L1, L); /* init stack */ - lua_unlock(L); - return L1; -} - - -void luaE_freethread (lua_State *L, lua_State *L1) { - LX *l = fromstate(L1); - luaF_close(L1, L1->stack); /* close all upvalues for this thread */ - lua_assert(L1->openupval == NULL); - luai_userstatefree(L, L1); - freestack(L1); - luaM_free(L, l); -} - - -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { - int i; - lua_State *L; - global_State *g; - LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); - if (l == NULL) return NULL; - L = &l->l.l; - g = &l->g; - L->next = NULL; - L->tt = LUA_TTHREAD; - g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); - L->marked = luaC_white(g); - g->gckind = KGC_NORMAL; - preinit_state(L, g); - g->frealloc = f; - g->ud = ud; - g->mainthread = L; - g->seed = makeseed(L); - g->uvhead.u.l.prev = &g->uvhead; - g->uvhead.u.l.next = &g->uvhead; - g->gcrunning = 0; /* no GC while building state */ - g->GCestimate = 0; - g->strt.size = 0; - g->strt.nuse = 0; - g->strt.hash = NULL; - setnilvalue(&g->l_registry); - luaZ_initbuffer(L, &g->buff); - g->panic = NULL; - g->version = NULL; - g->gcstate = GCSpause; - g->allgc = NULL; - g->finobj = NULL; - g->tobefnz = NULL; - g->sweepgc = g->sweepfin = NULL; - g->gray = g->grayagain = NULL; - g->weak = g->ephemeron = g->allweak = NULL; - g->totalbytes = sizeof(LG); - g->GCdebt = 0; - g->gcpause = LUAI_GCPAUSE; - g->gcmajorinc = LUAI_GCMAJOR; - g->gcstepmul = LUAI_GCMUL; - for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; - if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { - /* memory allocation error: free partial state */ - close_state(L); - L = NULL; - } - return L; -} - - -LUA_API void lua_close (lua_State *L) { - L = G(L)->mainthread; /* only the main thread can be closed */ - lua_lock(L); - close_state(L); -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.h deleted file mode 100644 index daffd9a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstate.h +++ /dev/null @@ -1,228 +0,0 @@ -/* -** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ -** Global State -** See Copyright Notice in lua.h -*/ - -#ifndef lstate_h -#define lstate_h - -#include "lua.h" - -#include "lobject.h" -#include "ltm.h" -#include "lzio.h" - - -/* - -** Some notes about garbage-collected objects: All objects in Lua must -** be kept somehow accessible until being freed. -** -** Lua keeps most objects linked in list g->allgc. The link uses field -** 'next' of the CommonHeader. -** -** Strings are kept in several lists headed by the array g->strt.hash. -** -** Open upvalues are not subject to independent garbage collection. They -** are collected together with their respective threads. Lua keeps a -** double-linked list with all open upvalues (g->uvhead) so that it can -** mark objects referred by them. (They are always gray, so they must -** be remarked in the atomic step. Usually their contents would be marked -** when traversing the respective threads, but the thread may already be -** dead, while the upvalue is still accessible through closures.) -** -** Objects with finalizers are kept in the list g->finobj. -** -** The list g->tobefnz links all objects being finalized. - -*/ - - -struct lua_longjmp; /* defined in ldo.c */ - - - -/* extra stack space to handle TM calls and some other extras */ -#define EXTRA_STACK 5 - - -#define BASIC_STACK_SIZE (2*LUA_MINSTACK) - - -/* kinds of Garbage Collection */ -#define KGC_NORMAL 0 -#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ -#define KGC_GEN 2 /* generational collection */ - - -typedef struct stringtable { - GCObject **hash; - lu_int32 nuse; /* number of elements */ - int size; -} stringtable; - - -/* -** information about a call -*/ -typedef struct CallInfo { - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ - struct CallInfo *previous, *next; /* dynamic call link */ - short nresults; /* expected number of results from this function */ - lu_byte callstatus; - ptrdiff_t extra; - union { - struct { /* only for Lua functions */ - StkId base; /* base for this function */ - const Instruction *savedpc; - } l; - struct { /* only for C functions */ - int ctx; /* context info. in case of yields */ - lua_CFunction k; /* continuation in case of yields */ - ptrdiff_t old_errfunc; - lu_byte old_allowhook; - lu_byte status; - } c; - } u; -} CallInfo; - - -/* -** Bits in CallInfo status -*/ -#define CIST_LUA (1<<0) /* call is running a Lua function */ -#define CIST_HOOKED (1<<1) /* call is running a debug hook */ -#define CIST_REENTRY (1<<2) /* call is running on same invocation of - luaV_execute of previous call */ -#define CIST_YIELDED (1<<3) /* call reentered after suspension */ -#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ -#define CIST_STAT (1<<5) /* call has an error status (pcall) */ -#define CIST_TAIL (1<<6) /* call was tail called */ -#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ - - -#define isLua(ci) ((ci)->callstatus & CIST_LUA) - - -/* -** `global state', shared by all threads of this state -*/ -typedef struct global_State { - lua_Alloc frealloc; /* function to reallocate memory */ - void *ud; /* auxiliary data to `frealloc' */ - lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ - l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ - lu_mem GCmemtrav; /* memory traversed by the GC */ - lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ - stringtable strt; /* hash table for strings */ - TValue l_registry; - unsigned int seed; /* randomized seed for hashes */ - lu_byte currentwhite; - lu_byte gcstate; /* state of garbage collector */ - lu_byte gckind; /* kind of GC running */ - lu_byte gcrunning; /* true if GC is running */ - int sweepstrgc; /* position of sweep in `strt' */ - GCObject *allgc; /* list of all collectable objects */ - GCObject *finobj; /* list of collectable objects with finalizers */ - GCObject **sweepgc; /* current position of sweep in list 'allgc' */ - GCObject **sweepfin; /* current position of sweep in list 'finobj' */ - GCObject *gray; /* list of gray objects */ - GCObject *grayagain; /* list of objects to be traversed atomically */ - GCObject *weak; /* list of tables with weak values */ - GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ - GCObject *allweak; /* list of all-weak tables */ - GCObject *tobefnz; /* list of userdata to be GC */ - UpVal uvhead; /* head of double-linked list of all open upvalues */ - Mbuffer buff; /* temporary buffer for string concatenation */ - int gcpause; /* size of pause between successive GCs */ - int gcmajorinc; /* pause between major collections (only in gen. mode) */ - int gcstepmul; /* GC `granularity' */ - lua_CFunction panic; /* to be called in unprotected errors */ - struct lua_State *mainthread; - const lua_Number *version; /* pointer to version number */ - TString *memerrmsg; /* memory-error message */ - TString *tmname[TM_N]; /* array with tag-method names */ - struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ -} global_State; - - -/* -** `per thread' state -*/ -struct lua_State { - CommonHeader; - lu_byte status; - StkId top; /* first free slot in the stack */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - const Instruction *oldpc; /* last pc traced */ - StkId stack_last; /* last free slot in the stack */ - StkId stack; /* stack base */ - int stacksize; - unsigned short nny; /* number of non-yieldable calls in stack */ - unsigned short nCcalls; /* number of nested C calls */ - lu_byte hookmask; - lu_byte allowhook; - int basehookcount; - int hookcount; - lua_Hook hook; - GCObject *openupval; /* list of open upvalues in this stack */ - GCObject *gclist; - struct lua_longjmp *errorJmp; /* current error recover point */ - ptrdiff_t errfunc; /* current error handling function (stack index) */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ -}; - - -#define G(L) (L->l_G) - - -/* -** Union of all collectable objects -*/ -union GCObject { - GCheader gch; /* common header */ - union TString ts; - union Udata u; - union Closure cl; - struct Table h; - struct Proto p; - struct UpVal uv; - struct lua_State th; /* thread */ -}; - - -#define gch(o) (&(o)->gch) - -/* macros to convert a GCObject into a specific value */ -#define rawgco2ts(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) -#define gco2ts(o) (&rawgco2ts(o)->tsv) -#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) -#define gco2u(o) (&rawgco2u(o)->uv) -#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) -#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) -#define gco2cl(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) -#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) -#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) -#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) -#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) - -/* macro to convert any Lua object into a GCObject */ -#define obj2gco(v) (cast(GCObject *, (v))) - - -/* actual number of total bytes allocated */ -#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) - -LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); -LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); -LUAI_FUNC void luaE_freeCI (lua_State *L); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.c deleted file mode 100644 index af96c89..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.c +++ /dev/null @@ -1,185 +0,0 @@ -/* -** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $ -** String table (keeps all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - - -#include - -#define lstring_c -#define LUA_CORE - -#include "lua.h" - -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" - - -/* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to -** compute its hash -*/ -#if !defined(LUAI_HASHLIMIT) -#define LUAI_HASHLIMIT 5 -#endif - - -/* -** equality for long strings -*/ -int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->tsv.len; - lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); - return (a == b) || /* same instance or... */ - ((len == b->tsv.len) && /* equal length and ... */ - (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ -} - - -/* -** equality for strings -*/ -int luaS_eqstr (TString *a, TString *b) { - return (a->tsv.tt == b->tsv.tt) && - (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); -} - - -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { - unsigned int h = seed ^ cast(unsigned int, l); - size_t l1; - size_t step = (l >> LUAI_HASHLIMIT) + 1; - for (l1 = l; l1 >= step; l1 -= step) - h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); - return h; -} - - -/* -** resizes the string table -*/ -void luaS_resize (lua_State *L, int newsize) { - int i; - stringtable *tb = &G(L)->strt; - /* cannot resize while GC is traversing strings */ - luaC_runtilstate(L, ~bitmask(GCSsweepstring)); - if (newsize > tb->size) { - luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); - for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; - } - /* rehash */ - for (i=0; isize; i++) { - GCObject *p = tb->hash[i]; - tb->hash[i] = NULL; - while (p) { /* for each node in the list */ - GCObject *next = gch(p)->next; /* save next */ - unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ - gch(p)->next = tb->hash[h]; /* chain it */ - tb->hash[h] = p; - resetoldbit(p); /* see MOVE OLD rule */ - p = next; - } - } - if (newsize < tb->size) { - /* shrinking slice must be empty */ - lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); - luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); - } - tb->size = newsize; -} - - -/* -** creates a new string object -*/ -static TString *createstrobj (lua_State *L, const char *str, size_t l, - int tag, unsigned int h, GCObject **list) { - TString *ts; - size_t totalsize; /* total size of TString object */ - totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); - ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; - ts->tsv.len = l; - ts->tsv.hash = h; - ts->tsv.extra = 0; - memcpy(ts+1, str, l*sizeof(char)); - ((char *)(ts+1))[l] = '\0'; /* ending 0 */ - return ts; -} - - -/* -** creates a new short string, inserting it into string table -*/ -static TString *newshrstr (lua_State *L, const char *str, size_t l, - unsigned int h) { - GCObject **list; /* (pointer to) list where it will be inserted */ - stringtable *tb = &G(L)->strt; - TString *s; - if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) - luaS_resize(L, tb->size*2); /* too crowded */ - list = &tb->hash[lmod(h, tb->size)]; - s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); - tb->nuse++; - return s; -} - - -/* -** checks whether short string exists and reuses it or creates a new one -*/ -static TString *internshrstr (lua_State *L, const char *str, size_t l) { - GCObject *o; - global_State *g = G(L); - unsigned int h = luaS_hash(str, l, g->seed); - for (o = g->strt.hash[lmod(h, g->strt.size)]; - o != NULL; - o = gch(o)->next) { - TString *ts = rawgco2ts(o); - if (h == ts->tsv.hash && - l == ts->tsv.len && - (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { - if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ - changewhite(o); /* resurrect it */ - return ts; - } - } - return newshrstr(L, str, l, h); /* not found; create a new string */ -} - - -/* -** new string (with explicit length) -*/ -TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { - if (l <= LUAI_MAXSHORTLEN) /* short string? */ - return internshrstr(L, str, l); - else { - if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) - luaM_toobig(L); - return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); - } -} - - -/* -** new zero-terminated string -*/ -TString *luaS_new (lua_State *L, const char *str) { - return luaS_newlstr(L, str, strlen(str)); -} - - -Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { - Udata *u; - if (s > MAX_SIZET - sizeof(Udata)) - luaM_toobig(L); - u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; - u->uv.len = s; - u->uv.metatable = NULL; - u->uv.env = e; - return u; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.h deleted file mode 100644 index 260e7f1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstring.h +++ /dev/null @@ -1,46 +0,0 @@ -/* -** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ -** String table (keep all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - -#ifndef lstring_h -#define lstring_h - -#include "lgc.h" -#include "lobject.h" -#include "lstate.h" - - -#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) - -#define sizeudata(u) (sizeof(union Udata)+(u)->len) - -#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ - (sizeof(s)/sizeof(char))-1)) - -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) - - -/* -** test whether a string is a reserved word -*/ -#define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) - - -/* -** equality for short strings, which are always internalized -*/ -#define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) - - -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); -LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); -LUAI_FUNC int luaS_eqstr (TString *a, TString *b); -LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); -LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); -LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstrlib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstrlib.c deleted file mode 100644 index 9261fd2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lstrlib.c +++ /dev/null @@ -1,1019 +0,0 @@ -/* -** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $ -** Standard library for string operations and pattern-matching -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include -#include - -#define lstrlib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** maximum number of captures that a pattern can do during -** pattern-matching. This limit is arbitrary. -*/ -#if !defined(LUA_MAXCAPTURES) -#define LUA_MAXCAPTURES 32 -#endif - - -/* macro to `unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - - -static int str_len (lua_State *L) { - size_t l; - luaL_checklstring(L, 1, &l); - lua_pushinteger(L, (lua_Integer)l); - return 1; -} - - -/* translate a relative string position: negative means back from end */ -static size_t posrelat (ptrdiff_t pos, size_t len) { - if (pos >= 0) return (size_t)pos; - else if (0u - (size_t)pos > len) return 0; - else return len - ((size_t)-pos) + 1; -} - - -static int str_sub (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - size_t start = posrelat(luaL_checkinteger(L, 2), l); - size_t end = posrelat(luaL_optinteger(L, 3, -1), l); - if (start < 1) start = 1; - if (end > l) end = l; - if (start <= end) - lua_pushlstring(L, s + start - 1, end - start + 1); - else lua_pushliteral(L, ""); - return 1; -} - - -static int str_reverse (lua_State *L) { - size_t l, i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - char *p = luaL_buffinitsize(L, &b, l); - for (i = 0; i < l; i++) - p[i] = s[l - i - 1]; - luaL_pushresultsize(&b, l); - return 1; -} - - -static int str_lower (lua_State *L) { - size_t l; - size_t i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - char *p = luaL_buffinitsize(L, &b, l); - for (i=0; i> 1) - -static int str_rep (lua_State *L) { - size_t l, lsep; - const char *s = luaL_checklstring(L, 1, &l); - int n = luaL_checkint(L, 2); - const char *sep = luaL_optlstring(L, 3, "", &lsep); - if (n <= 0) lua_pushliteral(L, ""); - else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ - return luaL_error(L, "resulting string too large"); - else { - size_t totallen = n * l + (n - 1) * lsep; - luaL_Buffer b; - char *p = luaL_buffinitsize(L, &b, totallen); - while (n-- > 1) { /* first n-1 copies (followed by separator) */ - memcpy(p, s, l * sizeof(char)); p += l; - if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */ - memcpy(p, sep, lsep * sizeof(char)); p += lsep; - } - } - memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ - luaL_pushresultsize(&b, totallen); - } - return 1; -} - - -static int str_byte (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - size_t posi = posrelat(luaL_optinteger(L, 2, 1), l); - size_t pose = posrelat(luaL_optinteger(L, 3, posi), l); - int n, i; - if (posi < 1) posi = 1; - if (pose > l) pose = l; - if (posi > pose) return 0; /* empty interval; return no values */ - n = (int)(pose - posi + 1); - if (posi + n <= pose) /* (size_t -> int) overflow? */ - return luaL_error(L, "string slice too long"); - luaL_checkstack(L, n, "string slice too long"); - for (i=0; i= ms->level || ms->capture[l].len == CAP_UNFINISHED) - return luaL_error(ms->L, "invalid capture index %%%d", l + 1); - return l; -} - - -static int capture_to_close (MatchState *ms) { - int level = ms->level; - for (level--; level>=0; level--) - if (ms->capture[level].len == CAP_UNFINISHED) return level; - return luaL_error(ms->L, "invalid pattern capture"); -} - - -static const char *classend (MatchState *ms, const char *p) { - switch (*p++) { - case L_ESC: { - if (p == ms->p_end) - luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); - return p+1; - } - case '[': { - if (*p == '^') p++; - do { /* look for a `]' */ - if (p == ms->p_end) - luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); - if (*(p++) == L_ESC && p < ms->p_end) - p++; /* skip escapes (e.g. `%]') */ - } while (*p != ']'); - return p+1; - } - default: { - return p; - } - } -} - - -static int match_class (int c, int cl) { - int res; - switch (tolower(cl)) { - case 'a' : res = isalpha(c); break; - case 'c' : res = iscntrl(c); break; - case 'd' : res = isdigit(c); break; - case 'g' : res = isgraph(c); break; - case 'l' : res = islower(c); break; - case 'p' : res = ispunct(c); break; - case 's' : res = isspace(c); break; - case 'u' : res = isupper(c); break; - case 'w' : res = isalnum(c); break; - case 'x' : res = isxdigit(c); break; - case 'z' : res = (c == 0); break; /* deprecated option */ - default: return (cl == c); - } - return (islower(cl) ? res : !res); -} - - -static int matchbracketclass (int c, const char *p, const char *ec) { - int sig = 1; - if (*(p+1) == '^') { - sig = 0; - p++; /* skip the `^' */ - } - while (++p < ec) { - if (*p == L_ESC) { - p++; - if (match_class(c, uchar(*p))) - return sig; - } - else if ((*(p+1) == '-') && (p+2 < ec)) { - p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) - return sig; - } - else if (uchar(*p) == c) return sig; - } - return !sig; -} - - -static int singlematch (MatchState *ms, const char *s, const char *p, - const char *ep) { - if (s >= ms->src_end) - return 0; - else { - int c = uchar(*s); - switch (*p) { - case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); - case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); - } - } -} - - -static const char *matchbalance (MatchState *ms, const char *s, - const char *p) { - if (p >= ms->p_end - 1) - luaL_error(ms->L, "malformed pattern " - "(missing arguments to " LUA_QL("%%b") ")"); - if (*s != *p) return NULL; - else { - int b = *p; - int e = *(p+1); - int cont = 1; - while (++s < ms->src_end) { - if (*s == e) { - if (--cont == 0) return s+1; - } - else if (*s == b) cont++; - } - } - return NULL; /* string ends out of balance */ -} - - -static const char *max_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - ptrdiff_t i = 0; /* counts maximum expand for item */ - while (singlematch(ms, s + i, p, ep)) - i++; - /* keeps trying to match with the maximum repetitions */ - while (i>=0) { - const char *res = match(ms, (s+i), ep+1); - if (res) return res; - i--; /* else didn't match; reduce 1 repetition to try again */ - } - return NULL; -} - - -static const char *min_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - for (;;) { - const char *res = match(ms, s, ep+1); - if (res != NULL) - return res; - else if (singlematch(ms, s, p, ep)) - s++; /* try with one more repetition */ - else return NULL; - } -} - - -static const char *start_capture (MatchState *ms, const char *s, - const char *p, int what) { - const char *res; - int level = ms->level; - if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); - ms->capture[level].init = s; - ms->capture[level].len = what; - ms->level = level+1; - if ((res=match(ms, s, p)) == NULL) /* match failed? */ - ms->level--; /* undo capture */ - return res; -} - - -static const char *end_capture (MatchState *ms, const char *s, - const char *p) { - int l = capture_to_close(ms); - const char *res; - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ - if ((res = match(ms, s, p)) == NULL) /* match failed? */ - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ - return res; -} - - -static const char *match_capture (MatchState *ms, const char *s, int l) { - size_t len; - l = check_capture(ms, l); - len = ms->capture[l].len; - if ((size_t)(ms->src_end-s) >= len && - memcmp(ms->capture[l].init, s, len) == 0) - return s+len; - else return NULL; -} - - -static const char *match (MatchState *ms, const char *s, const char *p) { - if (ms->matchdepth-- == 0) - luaL_error(ms->L, "pattern too complex"); - init: /* using goto's to optimize tail recursion */ - if (p != ms->p_end) { /* end of pattern? */ - switch (*p) { - case '(': { /* start capture */ - if (*(p + 1) == ')') /* position capture? */ - s = start_capture(ms, s, p + 2, CAP_POSITION); - else - s = start_capture(ms, s, p + 1, CAP_UNFINISHED); - break; - } - case ')': { /* end capture */ - s = end_capture(ms, s, p + 1); - break; - } - case '$': { - if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ - goto dflt; /* no; go to default */ - s = (s == ms->src_end) ? s : NULL; /* check end of string */ - break; - } - case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ - switch (*(p + 1)) { - case 'b': { /* balanced string? */ - s = matchbalance(ms, s, p + 2); - if (s != NULL) { - p += 4; goto init; /* return match(ms, s, p + 4); */ - } /* else fail (s == NULL) */ - break; - } - case 'f': { /* frontier? */ - const char *ep; char previous; - p += 2; - if (*p != '[') - luaL_error(ms->L, "missing " LUA_QL("[") " after " - LUA_QL("%%f") " in pattern"); - ep = classend(ms, p); /* points to what is next */ - previous = (s == ms->src_init) ? '\0' : *(s - 1); - if (!matchbracketclass(uchar(previous), p, ep - 1) && - matchbracketclass(uchar(*s), p, ep - 1)) { - p = ep; goto init; /* return match(ms, s, ep); */ - } - s = NULL; /* match failed */ - break; - } - case '0': case '1': case '2': case '3': - case '4': case '5': case '6': case '7': - case '8': case '9': { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p + 1))); - if (s != NULL) { - p += 2; goto init; /* return match(ms, s, p + 2) */ - } - break; - } - default: goto dflt; - } - break; - } - default: dflt: { /* pattern class plus optional suffix */ - const char *ep = classend(ms, p); /* points to optional suffix */ - /* does not match at least once? */ - if (!singlematch(ms, s, p, ep)) { - if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */ - p = ep + 1; goto init; /* return match(ms, s, ep + 1); */ - } - else /* '+' or no suffix */ - s = NULL; /* fail */ - } - else { /* matched once */ - switch (*ep) { /* handle optional suffix */ - case '?': { /* optional */ - const char *res; - if ((res = match(ms, s + 1, ep + 1)) != NULL) - s = res; - else { - p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */ - } - break; - } - case '+': /* 1 or more repetitions */ - s++; /* 1 match already done */ - /* go through */ - case '*': /* 0 or more repetitions */ - s = max_expand(ms, s, p, ep); - break; - case '-': /* 0 or more repetitions (minimum) */ - s = min_expand(ms, s, p, ep); - break; - default: /* no suffix */ - s++; p = ep; goto init; /* return match(ms, s + 1, ep); */ - } - } - break; - } - } - } - ms->matchdepth++; - return s; -} - - - -static const char *lmemfind (const char *s1, size_t l1, - const char *s2, size_t l2) { - if (l2 == 0) return s1; /* empty strings are everywhere */ - else if (l2 > l1) return NULL; /* avoids a negative `l1' */ - else { - const char *init; /* to search for a `*s2' inside `s1' */ - l2--; /* 1st char will be checked by `memchr' */ - l1 = l1-l2; /* `s2' cannot be found after that */ - while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { - init++; /* 1st char is already checked */ - if (memcmp(init, s2+1, l2) == 0) - return init-1; - else { /* correct `l1' and `s1' to try again */ - l1 -= init-s1; - s1 = init; - } - } - return NULL; /* not found */ - } -} - - -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { - if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, e - s); /* add whole match */ - else - luaL_error(ms->L, "invalid capture index"); - } - else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); - if (l == CAP_POSITION) - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, l); - } -} - - -static int push_captures (MatchState *ms, const char *s, const char *e) { - int i; - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; - luaL_checkstack(ms->L, nlevels, "too many captures"); - for (i = 0; i < nlevels; i++) - push_onecapture(ms, i, s, e); - return nlevels; /* number of strings pushed */ -} - - -/* check whether pattern has no special characters */ -static int nospecials (const char *p, size_t l) { - size_t upto = 0; - do { - if (strpbrk(p + upto, SPECIALS)) - return 0; /* pattern has a special character */ - upto += strlen(p + upto) + 1; /* may have more after \0 */ - } while (upto <= l); - return 1; /* no special chars found */ -} - - -static int str_find_aux (lua_State *L, int find) { - size_t ls, lp; - const char *s = luaL_checklstring(L, 1, &ls); - const char *p = luaL_checklstring(L, 2, &lp); - size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); - if (init < 1) init = 1; - else if (init > ls + 1) { /* start after string's end? */ - lua_pushnil(L); /* cannot find anything */ - return 1; - } - /* explicit request or no special characters? */ - if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { - /* do a plain search */ - const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); - if (s2) { - lua_pushinteger(L, s2 - s + 1); - lua_pushinteger(L, s2 - s + lp); - return 2; - } - } - else { - MatchState ms; - const char *s1 = s + init - 1; - int anchor = (*p == '^'); - if (anchor) { - p++; lp--; /* skip anchor character */ - } - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = s; - ms.src_end = s + ls; - ms.p_end = p + lp; - do { - const char *res; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - if ((res=match(&ms, s1, p)) != NULL) { - if (find) { - lua_pushinteger(L, s1 - s + 1); /* start */ - lua_pushinteger(L, res - s); /* end */ - return push_captures(&ms, NULL, 0) + 2; - } - else - return push_captures(&ms, s1, res); - } - } while (s1++ < ms.src_end && !anchor); - } - lua_pushnil(L); /* not found */ - return 1; -} - - -static int str_find (lua_State *L) { - return str_find_aux(L, 1); -} - - -static int str_match (lua_State *L) { - return str_find_aux(L, 0); -} - - -static int gmatch_aux (lua_State *L) { - MatchState ms; - size_t ls, lp; - const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); - const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp); - const char *src; - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = s; - ms.src_end = s+ls; - ms.p_end = p + lp; - for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); - src <= ms.src_end; - src++) { - const char *e; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - if ((e = match(&ms, src, p)) != NULL) { - lua_Integer newstart = e-s; - if (e == src) newstart++; /* empty match? go at least one position */ - lua_pushinteger(L, newstart); - lua_replace(L, lua_upvalueindex(3)); - return push_captures(&ms, src, e); - } - } - return 0; /* not found */ -} - - -static int gmatch (lua_State *L) { - luaL_checkstring(L, 1); - luaL_checkstring(L, 2); - lua_settop(L, 2); - lua_pushinteger(L, 0); - lua_pushcclosure(L, gmatch_aux, 3); - return 1; -} - - -static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e) { - size_t l, i; - const char *news = lua_tolstring(ms->L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) - luaL_addchar(b, news[i]); - else { - i++; /* skip ESC */ - if (!isdigit(uchar(news[i]))) { - if (news[i] != L_ESC) - luaL_error(ms->L, "invalid use of " LUA_QL("%c") - " in replacement string", L_ESC); - luaL_addchar(b, news[i]); - } - else if (news[i] == '0') - luaL_addlstring(b, s, e - s); - else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_addvalue(b); /* add capture to accumulated result */ - } - } - } -} - - -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e, int tr) { - lua_State *L = ms->L; - switch (tr) { - case LUA_TFUNCTION: { - int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); - break; - } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); - lua_gettable(L, 3); - break; - } - default: { /* LUA_TNUMBER or LUA_TSTRING */ - add_s(ms, b, s, e); - return; - } - } - if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, e - s); /* keep original text */ - } - else if (!lua_isstring(L, -1)) - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); - luaL_addvalue(b); /* add result to accumulator */ -} - - -static int str_gsub (lua_State *L) { - size_t srcl, lp; - const char *src = luaL_checklstring(L, 1, &srcl); - const char *p = luaL_checklstring(L, 2, &lp); - int tr = lua_type(L, 3); - size_t max_s = luaL_optinteger(L, 4, srcl+1); - int anchor = (*p == '^'); - size_t n = 0; - MatchState ms; - luaL_Buffer b; - luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || - tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, - "string/function/table expected"); - luaL_buffinit(L, &b); - if (anchor) { - p++; lp--; /* skip anchor character */ - } - ms.L = L; - ms.matchdepth = MAXCCALLS; - ms.src_init = src; - ms.src_end = src+srcl; - ms.p_end = p + lp; - while (n < max_s) { - const char *e; - ms.level = 0; - lua_assert(ms.matchdepth == MAXCCALLS); - e = match(&ms, src, p); - if (e) { - n++; - add_value(&ms, &b, src, e, tr); - } - if (e && e>src) /* non empty match? */ - src = e; /* skip it */ - else if (src < ms.src_end) - luaL_addchar(&b, *src++); - else break; - if (anchor) break; - } - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); - lua_pushinteger(L, n); /* number of substitutions */ - return 2; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** STRING FORMAT -** ======================================================= -*/ - -/* -** LUA_INTFRMLEN is the length modifier for integer conversions in -** 'string.format'; LUA_INTFRM_T is the integer type corresponding to -** the previous length -*/ -#if !defined(LUA_INTFRMLEN) /* { */ -#if defined(LUA_USE_LONGLONG) - -#define LUA_INTFRMLEN "ll" -#define LUA_INTFRM_T long long - -#else - -#define LUA_INTFRMLEN "l" -#define LUA_INTFRM_T long - -#endif -#endif /* } */ - - -/* -** LUA_FLTFRMLEN is the length modifier for float conversions in -** 'string.format'; LUA_FLTFRM_T is the float type corresponding to -** the previous length -*/ -#if !defined(LUA_FLTFRMLEN) - -#define LUA_FLTFRMLEN "" -#define LUA_FLTFRM_T double - -#endif - - -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_ITEM 512 -/* valid flags in a format specification */ -#define FLAGS "-+ #0" -/* -** maximum size of each format specification (such as '%-099.99d') -** (+10 accounts for %99.99x plus margin of error) -*/ -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) - - -static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - luaL_addchar(b, '"'); - while (l--) { - if (*s == '"' || *s == '\\' || *s == '\n') { - luaL_addchar(b, '\\'); - luaL_addchar(b, *s); - } - else if (*s == '\0' || iscntrl(uchar(*s))) { - char buff[10]; - if (!isdigit(uchar(*(s+1)))) - sprintf(buff, "\\%d", (int)uchar(*s)); - else - sprintf(buff, "\\%03d", (int)uchar(*s)); - luaL_addstring(b, buff); - } - else - luaL_addchar(b, *s); - s++; - } - luaL_addchar(b, '"'); -} - -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { - const char *p = strfrmt; - while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) p++; /* skip precision */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); - *(form++) = '%'; - memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); - form += p - strfrmt + 1; - *form = '\0'; - return p; -} - - -/* -** add length modifier into formats -*/ -static void addlenmod (char *form, const char *lenmod) { - size_t l = strlen(form); - size_t lm = strlen(lenmod); - char spec = form[l - 1]; - strcpy(form + l - 1, lenmod); - form[l + lm - 1] = spec; - form[l + lm] = '\0'; -} - - -static int str_format (lua_State *L) { - int top = lua_gettop(L); - int arg = 1; - size_t sfl; - const char *strfrmt = luaL_checklstring(L, arg, &sfl); - const char *strfrmt_end = strfrmt+sfl; - luaL_Buffer b; - luaL_buffinit(L, &b); - while (strfrmt < strfrmt_end) { - if (*strfrmt != L_ESC) - luaL_addchar(&b, *strfrmt++); - else if (*++strfrmt == L_ESC) - luaL_addchar(&b, *strfrmt++); /* %% */ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ - char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ - int nb = 0; /* number of bytes in added item */ - if (++arg > top) - luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { - nb = sprintf(buff, form, luaL_checkint(L, arg)); - break; - } - case 'd': case 'i': { - lua_Number n = luaL_checknumber(L, arg); - LUA_INTFRM_T ni = (LUA_INTFRM_T)n; - lua_Number diff = n - (lua_Number)ni; - luaL_argcheck(L, -1 < diff && diff < 1, arg, - "not a number in proper range"); - addlenmod(form, LUA_INTFRMLEN); - nb = sprintf(buff, form, ni); - break; - } - case 'o': case 'u': case 'x': case 'X': { - lua_Number n = luaL_checknumber(L, arg); - unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; - lua_Number diff = n - (lua_Number)ni; - luaL_argcheck(L, -1 < diff && diff < 1, arg, - "not a non-negative number in proper range"); - addlenmod(form, LUA_INTFRMLEN); - nb = sprintf(buff, form, ni); - break; - } - case 'e': case 'E': case 'f': -#if defined(LUA_USE_AFORMAT) - case 'a': case 'A': -#endif - case 'g': case 'G': { - addlenmod(form, LUA_FLTFRMLEN); - nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); - break; - } - case 'q': { - addquoted(L, &b, arg); - break; - } - case 's': { - size_t l; - const char *s = luaL_tolstring(L, arg, &l); - if (!strchr(form, '.') && l >= 100) { - /* no precision and string is too long to be formatted; - keep original string */ - luaL_addvalue(&b); - break; - } - else { - nb = sprintf(buff, form, s); - lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ - break; - } - } - default: { /* also treat cases `pnLlh' */ - return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " - LUA_QL("format"), *(strfrmt - 1)); - } - } - luaL_addsize(&b, nb); - } - } - luaL_pushresult(&b); - return 1; -} - -/* }====================================================== */ - - -static const luaL_Reg strlib[] = { - {"byte", str_byte}, - {"char", str_char}, - {"dump", str_dump}, - {"find", str_find}, - {"format", str_format}, - {"gmatch", gmatch}, - {"gsub", str_gsub}, - {"len", str_len}, - {"lower", str_lower}, - {"match", str_match}, - {"rep", str_rep}, - {"reverse", str_reverse}, - {"sub", str_sub}, - {"upper", str_upper}, - {NULL, NULL} -}; - - -static void createmetatable (lua_State *L) { - lua_createtable(L, 0, 1); /* table to be metatable for strings */ - lua_pushliteral(L, ""); /* dummy string */ - lua_pushvalue(L, -2); /* copy table */ - lua_setmetatable(L, -2); /* set table as metatable for strings */ - lua_pop(L, 1); /* pop dummy string */ - lua_pushvalue(L, -2); /* get string library */ - lua_setfield(L, -2, "__index"); /* metatable.__index = string */ - lua_pop(L, 1); /* pop metatable */ -} - - -/* -** Open string library -*/ -LUAMOD_API int luaopen_string (lua_State *L) { - luaL_newlib(L, strlib); - createmetatable(L); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.c deleted file mode 100644 index 5d76f97..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.c +++ /dev/null @@ -1,588 +0,0 @@ -/* -** $Id: ltable.c,v 2.72.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - - -/* -** Implementation of tables (aka arrays, objects, or hash tables). -** Tables keep its elements in two parts: an array part and a hash part. -** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest `n' such that at -** least half the slots between 0 and n are in use. -** Hash uses a mix of chained scatter table with Brent's variation. -** A main invariant of these tables is that, if an element is not -** in its main position (i.e. the `original' position that its hash gives -** to it), then the colliding element is in its own main position. -** Hence even when the load factor reaches 100%, performance remains good. -*/ - -#include - -#define ltable_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lvm.h" - - -/* -** max size of array part is 2^MAXBITS -*/ -#if LUAI_BITSINT >= 32 -#define MAXBITS 30 -#else -#define MAXBITS (LUAI_BITSINT-2) -#endif - -#define MAXASIZE (1 << MAXBITS) - - -#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) - -#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) -#define hashboolean(t,p) hashpow2(t, p) - - -/* -** for some types, it is better to avoid modulus by power of 2, as -** they tend to have many 2 factors. -*/ -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) - - -#define hashpointer(t,p) hashmod(t, IntPoint(p)) - - -#define dummynode (&dummynode_) - -#define isdummy(n) ((n) == dummynode) - -static const Node dummynode_ = { - {NILCONSTANT}, /* value */ - {{NILCONSTANT, NULL}} /* key */ -}; - - -/* -** hash for lua_Numbers -*/ -static Node *hashnum (const Table *t, lua_Number n) { - int i; - luai_hashnum(i, n); - if (i < 0) { - if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */ - i = 0; /* handle INT_MIN */ - i = -i; /* must be a positive value */ - } - return hashmod(t, i); -} - - - -/* -** returns the `main' position of an element in a table (that is, the index -** of its hash value) -*/ -static Node *mainposition (const Table *t, const TValue *key) { - switch (ttype(key)) { - case LUA_TNUMBER: - return hashnum(t, nvalue(key)); - case LUA_TLNGSTR: { - TString *s = rawtsvalue(key); - if (s->tsv.extra == 0) { /* no hash? */ - s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash); - s->tsv.extra = 1; /* now it has its hash */ - } - return hashstr(t, rawtsvalue(key)); - } - case LUA_TSHRSTR: - return hashstr(t, rawtsvalue(key)); - case LUA_TBOOLEAN: - return hashboolean(t, bvalue(key)); - case LUA_TLIGHTUSERDATA: - return hashpointer(t, pvalue(key)); - case LUA_TLCF: - return hashpointer(t, fvalue(key)); - default: - return hashpointer(t, gcvalue(key)); - } -} - - -/* -** returns the index for `key' if `key' is an appropriate key to live in -** the array part of the table, -1 otherwise. -*/ -static int arrayindex (const TValue *key) { - if (ttisnumber(key)) { - lua_Number n = nvalue(key); - int k; - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) - return k; - } - return -1; /* `key' did not match some condition */ -} - - -/* -** returns the index of a `key' for table traversals. First goes all -** elements in the array part, then elements in the hash part. The -** beginning of a traversal is signaled by -1. -*/ -static int findindex (lua_State *L, Table *t, StkId key) { - int i; - if (ttisnil(key)) return -1; /* first iteration */ - i = arrayindex(key); - if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ - return i-1; /* yes; that's the index (corrected to C) */ - else { - Node *n = mainposition(t, key); - for (;;) { /* check whether `key' is somewhere in the chain */ - /* key may be dead already, but it is ok to use it in `next' */ - if (luaV_rawequalobj(gkey(n), key) || - (ttisdeadkey(gkey(n)) && iscollectable(key) && - deadvalue(gkey(n)) == gcvalue(key))) { - i = cast_int(n - gnode(t, 0)); /* key index in hash table */ - /* hash elements are numbered after array ones */ - return i + t->sizearray; - } - else n = gnext(n); - if (n == NULL) - luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ - } - } -} - - -int luaH_next (lua_State *L, Table *t, StkId key) { - int i = findindex(L, t, key); /* find original element */ - for (i++; i < t->sizearray; i++) { /* try first array part */ - if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setnvalue(key, cast_num(i+1)); - setobj2s(L, key+1, &t->array[i]); - return 1; - } - } - for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ - if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ - setobj2s(L, key, gkey(gnode(t, i))); - setobj2s(L, key+1, gval(gnode(t, i))); - return 1; - } - } - return 0; /* no more elements */ -} - - -/* -** {============================================================= -** Rehash -** ============================================================== -*/ - - -static int computesizes (int nums[], int *narray) { - int i; - int twotoi; /* 2^i */ - int a = 0; /* number of elements smaller than 2^i */ - int na = 0; /* number of elements to go to array part */ - int n = 0; /* optimal size for array part */ - for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ - n = twotoi; /* optimal size (till now) */ - na = a; /* all elements smaller than n will go to array part */ - } - } - if (a == *narray) break; /* all elements already counted */ - } - *narray = n; - lua_assert(*narray/2 <= na && na <= *narray); - return na; -} - - -static int countint (const TValue *key, int *nums) { - int k = arrayindex(key); - if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ - nums[luaO_ceillog2(k)]++; /* count as such */ - return 1; - } - else - return 0; -} - - -static int numusearray (const Table *t, int *nums) { - int lg; - int ttlg; /* 2^lg */ - int ause = 0; /* summation of `nums' */ - int i = 1; /* count to traverse all array keys */ - for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ - int lc = 0; /* counter */ - int lim = ttlg; - if (lim > t->sizearray) { - lim = t->sizearray; /* adjust upper limit */ - if (i > lim) - break; /* no more elements to count */ - } - /* count elements in range (2^(lg-1), 2^lg] */ - for (; i <= lim; i++) { - if (!ttisnil(&t->array[i-1])) - lc++; - } - nums[lg] += lc; - ause += lc; - } - return ause; -} - - -static int numusehash (const Table *t, int *nums, int *pnasize) { - int totaluse = 0; /* total number of elements */ - int ause = 0; /* summation of `nums' */ - int i = sizenode(t); - while (i--) { - Node *n = &t->node[i]; - if (!ttisnil(gval(n))) { - ause += countint(gkey(n), nums); - totaluse++; - } - } - *pnasize += ause; - return totaluse; -} - - -static void setarrayvector (lua_State *L, Table *t, int size) { - int i; - luaM_reallocvector(L, t->array, t->sizearray, size, TValue); - for (i=t->sizearray; iarray[i]); - t->sizearray = size; -} - - -static void setnodevector (lua_State *L, Table *t, int size) { - int lsize; - if (size == 0) { /* no elements to hash part? */ - t->node = cast(Node *, dummynode); /* use common `dummynode' */ - lsize = 0; - } - else { - int i; - lsize = luaO_ceillog2(size); - if (lsize > MAXBITS) - luaG_runerror(L, "table overflow"); - size = twoto(lsize); - t->node = luaM_newvector(L, size, Node); - for (i=0; ilsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ -} - - -void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { - int i; - int oldasize = t->sizearray; - int oldhsize = t->lsizenode; - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); - /* create new hash part with appropriate size */ - setnodevector(L, t, nhsize); - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ - for (i=nasize; iarray[i])) - luaH_setint(L, t, i + 1, &t->array[i]); - } - /* shrink array */ - luaM_reallocvector(L, t->array, oldasize, nasize, TValue); - } - /* re-insert elements from hash part */ - for (i = twoto(oldhsize) - 1; i >= 0; i--) { - Node *old = nold+i; - if (!ttisnil(gval(old))) { - /* doesn't need barrier/invalidate cache, as entry was - already present in the table */ - setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); - } - } - if (!isdummy(nold)) - luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */ -} - - -void luaH_resizearray (lua_State *L, Table *t, int nasize) { - int nsize = isdummy(t->node) ? 0 : sizenode(t); - luaH_resize(L, t, nasize, nsize); -} - - -static void rehash (lua_State *L, Table *t, const TValue *ek) { - int nasize, na; - int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */ - int i; - int totaluse; - for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ - nasize = numusearray(t, nums); /* count keys in array part */ - totaluse = nasize; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ - /* count extra key */ - nasize += countint(ek, nums); - totaluse++; - /* compute new size for array part */ - na = computesizes(nums, &nasize); - /* resize the table to new computed sizes */ - luaH_resize(L, t, nasize, totaluse - na); -} - - - -/* -** }============================================================= -*/ - - -Table *luaH_new (lua_State *L) { - Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h; - t->metatable = NULL; - t->flags = cast_byte(~0); - t->array = NULL; - t->sizearray = 0; - setnodevector(L, t, 0); - return t; -} - - -void luaH_free (lua_State *L, Table *t) { - if (!isdummy(t->node)) - luaM_freearray(L, t->node, cast(size_t, sizenode(t))); - luaM_freearray(L, t->array, t->sizearray); - luaM_free(L, t); -} - - -static Node *getfreepos (Table *t) { - while (t->lastfree > t->node) { - t->lastfree--; - if (ttisnil(gkey(t->lastfree))) - return t->lastfree; - } - return NULL; /* could not find a free place */ -} - - - -/* -** inserts a new key into a hash table; first, check whether key's main -** position is free. If not, check whether colliding node is in its main -** position or not: if it is not, move colliding node to an empty place and -** put new key in its main position; otherwise (colliding node is in its main -** position), new key goes to an empty position. -*/ -TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { - Node *mp; - if (ttisnil(key)) luaG_runerror(L, "table index is nil"); - else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) - luaG_runerror(L, "table index is NaN"); - mp = mainposition(t, key); - if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ - Node *othern; - Node *n = getfreepos(t); /* get a free place */ - if (n == NULL) { /* cannot find a free place? */ - rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' take care of TM cache and GC barrier */ - return luaH_set(L, t, key); /* insert key into grown table */ - } - lua_assert(!isdummy(n)); - othern = mainposition(t, gkey(mp)); - if (othern != mp) { /* is colliding node out of its main position? */ - /* yes; move colliding node into free position */ - while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ - gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ - *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ - gnext(mp) = NULL; /* now `mp' is free */ - setnilvalue(gval(mp)); - } - else { /* colliding node is in its own main position */ - /* new node will go into free position */ - gnext(n) = gnext(mp); /* chain new position */ - gnext(mp) = n; - mp = n; - } - } - setobj2t(L, gkey(mp), key); - luaC_barrierback(L, obj2gco(t), key); - lua_assert(ttisnil(gval(mp))); - return gval(mp); -} - - -/* -** search function for integers -*/ -const TValue *luaH_getint (Table *t, int key) { - /* (1 <= key && key <= t->sizearray) */ - if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) - return &t->array[key-1]; - else { - lua_Number nk = cast_num(key); - Node *n = hashnum(t, nk); - do { /* check whether `key' is somewhere in the chain */ - if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; - } -} - - -/* -** search function for short strings -*/ -const TValue *luaH_getstr (Table *t, TString *key) { - Node *n = hashstr(t, key); - lua_assert(key->tsv.tt == LUA_TSHRSTR); - do { /* check whether `key' is somewhere in the chain */ - if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key)) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; -} - - -/* -** main search function -*/ -const TValue *luaH_get (Table *t, const TValue *key) { - switch (ttype(key)) { - case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); - case LUA_TNIL: return luaO_nilobject; - case LUA_TNUMBER: { - int k; - lua_Number n = nvalue(key); - lua_number2int(k, n); - if (luai_numeq(cast_num(k), n)) /* index is int? */ - return luaH_getint(t, k); /* use specialized version */ - /* else go through */ - } - default: { - Node *n = mainposition(t, key); - do { /* check whether `key' is somewhere in the chain */ - if (luaV_rawequalobj(gkey(n), key)) - return gval(n); /* that's it */ - else n = gnext(n); - } while (n); - return luaO_nilobject; - } - } -} - - -/* -** beware: when using this function you probably need to check a GC -** barrier and invalidate the TM cache. -*/ -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p = luaH_get(t, key); - if (p != luaO_nilobject) - return cast(TValue *, p); - else return luaH_newkey(L, t, key); -} - - -void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { - const TValue *p = luaH_getint(t, key); - TValue *cell; - if (p != luaO_nilobject) - cell = cast(TValue *, p); - else { - TValue k; - setnvalue(&k, cast_num(key)); - cell = luaH_newkey(L, t, &k); - } - setobj2t(L, cell, value); -} - - -static int unbound_search (Table *t, unsigned int j) { - unsigned int i = j; /* i is zero or a present index */ - j++; - /* find `i' and `j' such that i is present and j is not */ - while (!ttisnil(luaH_getint(t, j))) { - i = j; - j *= 2; - if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ - i = 1; - while (!ttisnil(luaH_getint(t, i))) i++; - return i - 1; - } - } - /* now do a binary search between them */ - while (j - i > 1) { - unsigned int m = (i+j)/2; - if (ttisnil(luaH_getint(t, m))) j = m; - else i = m; - } - return i; -} - - -/* -** Try to find a boundary in table `t'. A `boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). -*/ -int luaH_getn (Table *t) { - unsigned int j = t->sizearray; - if (j > 0 && ttisnil(&t->array[j - 1])) { - /* there is a boundary in the array part: (binary) search for it */ - unsigned int i = 0; - while (j - i > 1) { - unsigned int m = (i+j)/2; - if (ttisnil(&t->array[m - 1])) j = m; - else i = m; - } - return i; - } - /* else must find a boundary in hash part */ - else if (isdummy(t->node)) /* hash part is empty? */ - return j; /* that is easy... */ - else return unbound_search(t, j); -} - - - -#if defined(LUA_DEBUG) - -Node *luaH_mainposition (const Table *t, const TValue *key) { - return mainposition(t, key); -} - -int luaH_isdummy (Node *n) { return isdummy(n); } - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.h deleted file mode 100644 index d69449b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltable.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - -#ifndef ltable_h -#define ltable_h - -#include "lobject.h" - - -#define gnode(t,i) (&(t)->node[i]) -#define gkey(n) (&(n)->i_key.tvk) -#define gval(n) (&(n)->i_val) -#define gnext(n) ((n)->i_key.nk.next) - -#define invalidateTMcache(t) ((t)->flags = 0) - -/* returns the key, given the value of a table entry */ -#define keyfromval(v) \ - (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) - - -LUAI_FUNC const TValue *luaH_getint (Table *t, int key); -LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); -LUAI_FUNC void luaH_free (lua_State *L, Table *t); -LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC int luaH_getn (Table *t); - - -#if defined(LUA_DEBUG) -LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (Node *n); -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltablib.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltablib.c deleted file mode 100644 index 99764d2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltablib.c +++ /dev/null @@ -1,285 +0,0 @@ -/* -** $Id: ltablib.c,v 1.65.1.2 2014/05/07 16:32:55 roberto Exp $ -** Library for Table Manipulation -** See Copyright Notice in lua.h -*/ - - -#include -#include - -#define ltablib_c -#define LUA_LIB - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) - - - -#if defined(LUA_COMPAT_MAXN) -static int maxn (lua_State *L) { - lua_Number max = 0; - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pop(L, 1); /* remove value */ - if (lua_type(L, -1) == LUA_TNUMBER) { - lua_Number v = lua_tonumber(L, -1); - if (v > max) max = v; - } - } - lua_pushnumber(L, max); - return 1; -} -#endif - - -static int tinsert (lua_State *L) { - int e = aux_getn(L, 1) + 1; /* first empty element */ - int pos; /* where to insert new element */ - switch (lua_gettop(L)) { - case 2: { /* called with only 2 arguments */ - pos = e; /* insert new element at the end */ - break; - } - case 3: { - int i; - pos = luaL_checkint(L, 2); /* 2nd argument is the position */ - luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); - for (i = e; i > pos; i--) { /* move up elements */ - lua_rawgeti(L, 1, i-1); - lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ - } - break; - } - default: { - return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); - } - } - lua_rawseti(L, 1, pos); /* t[pos] = v */ - return 0; -} - - -static int tremove (lua_State *L) { - int size = aux_getn(L, 1); - int pos = luaL_optint(L, 2, size); - if (pos != size) /* validate 'pos' if given */ - luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); - lua_rawgeti(L, 1, pos); /* result = t[pos] */ - for ( ; pos < size; pos++) { - lua_rawgeti(L, 1, pos+1); - lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ - } - lua_pushnil(L); - lua_rawseti(L, 1, pos); /* t[pos] = nil */ - return 1; -} - - -static void addfield (lua_State *L, luaL_Buffer *b, int i) { - lua_rawgeti(L, 1, i); - if (!lua_isstring(L, -1)) - luaL_error(L, "invalid value (%s) at index %d in table for " - LUA_QL("concat"), luaL_typename(L, -1), i); - luaL_addvalue(b); -} - - -static int tconcat (lua_State *L) { - luaL_Buffer b; - size_t lsep; - int i, last; - const char *sep = luaL_optlstring(L, 2, "", &lsep); - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 3, 1); - last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); - luaL_buffinit(L, &b); - for (; i < last; i++) { - addfield(L, &b, i); - luaL_addlstring(&b, sep, lsep); - } - if (i == last) /* add last value (if interval was not empty) */ - addfield(L, &b, i); - luaL_pushresult(&b); - return 1; -} - - -/* -** {====================================================== -** Pack/unpack -** ======================================================= -*/ - -static int pack (lua_State *L) { - int n = lua_gettop(L); /* number of elements to pack */ - lua_createtable(L, n, 1); /* create result table */ - lua_pushinteger(L, n); - lua_setfield(L, -2, "n"); /* t.n = number of elements */ - if (n > 0) { /* at least one element? */ - int i; - lua_pushvalue(L, 1); - lua_rawseti(L, -2, 1); /* insert first element */ - lua_replace(L, 1); /* move table into index 1 */ - for (i = n; i >= 2; i--) /* assign other elements */ - lua_rawseti(L, 1, i); - } - return 1; /* return table */ -} - - -static int unpack (lua_State *L) { - int i, e; - unsigned int n; - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); - if (i > e) return 0; /* empty range */ - n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */ - if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) - return luaL_error(L, "too many results to unpack"); - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ - while (i++ < e) /* push arg[i + 1...e] */ - lua_rawgeti(L, 1, i); - return n; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Quicksort -** (based on `Algorithms in MODULA-3', Robert Sedgewick; -** Addison-Wesley, 1993.) -** ======================================================= -*/ - - -static void set2 (lua_State *L, int i, int j) { - lua_rawseti(L, 1, i); - lua_rawseti(L, 1, j); -} - -static int sort_comp (lua_State *L, int a, int b) { - if (!lua_isnil(L, 2)) { /* function? */ - int res; - lua_pushvalue(L, 2); - lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ - lua_call(L, 2, 1); - res = lua_toboolean(L, -1); - lua_pop(L, 1); - return res; - } - else /* a < b? */ - return lua_compare(L, a, b, LUA_OPLT); -} - -static void auxsort (lua_State *L, int l, int u) { - while (l < u) { /* for tail recursion */ - int i, j; - /* sort elements a[l], a[(l+u)/2] and a[u] */ - lua_rawgeti(L, 1, l); - lua_rawgeti(L, 1, u); - if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ - set2(L, l, u); /* swap a[l] - a[u] */ - else - lua_pop(L, 2); - if (u-l == 1) break; /* only 2 elements */ - i = (l+u)/2; - lua_rawgeti(L, 1, i); - lua_rawgeti(L, 1, l); - if (sort_comp(L, -2, -1)) /* a[i]= P */ - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i>=u) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[i] */ - } - /* repeat --j until a[j] <= P */ - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j<=l) luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[j] */ - } - if (j - -#define ltm_c -#define LUA_CORE - -#include "lua.h" - -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -static const char udatatypename[] = "userdata"; - -LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { - "no value", - "nil", "boolean", udatatypename, "number", - "string", "table", "function", udatatypename, "thread", - "proto", "upval" /* these last two cases are used for tests only */ -}; - - -void luaT_init (lua_State *L) { - static const char *const luaT_eventname[] = { /* ORDER TM */ - "__index", "__newindex", - "__gc", "__mode", "__len", "__eq", - "__add", "__sub", "__mul", "__div", "__mod", - "__pow", "__unm", "__lt", "__le", - "__concat", "__call" - }; - int i; - for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); - luaS_fix(G(L)->tmname[i]); /* never collect these names */ - } -} - - -/* -** function to be used with macro "fasttm": optimized for absence of -** tag methods -*/ -const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getstr(events, ename); - lua_assert(event <= TM_EQ); - if (ttisnil(tm)) { /* no tag method? */ - events->flags |= cast_byte(1u<metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(o)->metatable; - break; - default: - mt = G(L)->mt[ttypenv(o)]; - } - return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.h deleted file mode 100644 index 7f89c84..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/ltm.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#ifndef ltm_h -#define ltm_h - - -#include "lobject.h" - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER TM" -*/ -typedef enum { - TM_INDEX, - TM_NEWINDEX, - TM_GC, - TM_MODE, - TM_LEN, - TM_EQ, /* last tag method with `fast' access */ - TM_ADD, - TM_SUB, - TM_MUL, - TM_DIV, - TM_MOD, - TM_POW, - TM_UNM, - TM_LT, - TM_LE, - TM_CONCAT, - TM_CALL, - TM_N /* number of elements in the enum */ -} TMS; - - - -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) - -#define fasttm(l,et,e) gfasttm(G(l), et, e) - -#define ttypename(x) luaT_typenames_[(x) + 1] -#define objtypename(x) ttypename(ttypenv(x)) - -LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; - - -LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); -LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, - TMS event); -LUAI_FUNC void luaT_init (lua_State *L); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.c deleted file mode 100644 index 4345e55..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.c +++ /dev/null @@ -1,497 +0,0 @@ -/* -** $Id: lua.c,v 1.206.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua stand-alone interpreter -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include -#include - -#define lua_c - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#if !defined(LUA_PROMPT) -#define LUA_PROMPT "> " -#define LUA_PROMPT2 ">> " -#endif - -#if !defined(LUA_PROGNAME) -#define LUA_PROGNAME "lua" -#endif - -#if !defined(LUA_MAXINPUT) -#define LUA_MAXINPUT 512 -#endif - -#if !defined(LUA_INIT) -#define LUA_INIT "LUA_INIT" -#endif - -#define LUA_INITVERSION \ - LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - - -/* -** lua_stdin_is_tty detects whether the standard input is a 'tty' (that -** is, whether we're running lua interactively). -*/ -#if defined(LUA_USE_ISATTY) -#include -#define lua_stdin_is_tty() isatty(0) -#elif defined(LUA_WIN) -#include -#include -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) -#else -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ -#endif - - -/* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. -*/ -#if defined(LUA_USE_READLINE) - -#include -#include -#include -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,idx) \ - if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ - add_history(lua_tostring(L, idx)); /* add it to history */ -#define lua_freeline(L,b) ((void)L, free(b)) - -#elif !defined(lua_readline) - -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,idx) { (void)L; (void)idx; } -#define lua_freeline(L,b) { (void)L; (void)b; } - -#endif - - - - -static lua_State *globalL = NULL; - -static const char *progname = LUA_PROGNAME; - - - -static void lstop (lua_State *L, lua_Debug *ar) { - (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); - luaL_error(L, "interrupted!"); -} - - -static void laction (int i) { - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); -} - - -static void print_usage (const char *badoption) { - luai_writestringerror("%s: ", progname); - if (badoption[1] == 'e' || badoption[1] == 'l') - luai_writestringerror("'%s' needs argument\n", badoption); - else - luai_writestringerror("unrecognized option '%s'\n", badoption); - luai_writestringerror( - "usage: %s [options] [script [args]]\n" - "Available options are:\n" - " -e stat execute string " LUA_QL("stat") "\n" - " -i enter interactive mode after executing " LUA_QL("script") "\n" - " -l name require library " LUA_QL("name") "\n" - " -v show version information\n" - " -E ignore environment variables\n" - " -- stop handling options\n" - " - stop handling options and execute stdin\n" - , - progname); -} - - -static void l_message (const char *pname, const char *msg) { - if (pname) luai_writestringerror("%s: ", pname); - luai_writestringerror("%s\n", msg); -} - - -static int report (lua_State *L, int status) { - if (status != LUA_OK && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - /* force a complete garbage collection in case of errors */ - lua_gc(L, LUA_GCCOLLECT, 0); - } - return status; -} - - -/* the next function is called unprotected, so it must avoid errors */ -static void finalreport (lua_State *L, int status) { - if (status != LUA_OK) { - const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) - : NULL; - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - } -} - - -static int traceback (lua_State *L) { - const char *msg = lua_tostring(L, 1); - if (msg) - luaL_traceback(L, L, msg, 1); - else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */ - if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ - lua_pushliteral(L, "(no error message)"); - } - return 1; -} - - -static int docall (lua_State *L, int narg, int nres) { - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ - globalL = L; /* to be available to 'laction' */ - signal(SIGINT, laction); - status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); - lua_remove(L, base); /* remove traceback function */ - return status; -} - - -static void print_version (void) { - luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); - luai_writeline(); -} - - -static int getargs (lua_State *L, char **argv, int n) { - int narg; - int i; - int argc = 0; - while (argv[argc]) argc++; /* count total number of arguments */ - narg = argc - (n + 1); /* number of arguments to the script */ - luaL_checkstack(L, narg + 3, "too many arguments to script"); - for (i=n+1; i < argc; i++) - lua_pushstring(L, argv[i]); - lua_createtable(L, narg, n + 1); - for (i=0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - n); - } - return narg; -} - - -static int dofile (lua_State *L, const char *name) { - int status = luaL_loadfile(L, name); - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); -} - - -static int dostring (lua_State *L, const char *s, const char *name) { - int status = luaL_loadbuffer(L, s, strlen(s), name); - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); -} - - -static int dolibrary (lua_State *L, const char *name) { - int status; - lua_getglobal(L, "require"); - lua_pushstring(L, name); - status = docall(L, 1, 1); /* call 'require(name)' */ - if (status == LUA_OK) - lua_setglobal(L, name); /* global[name] = require return */ - return report(L, status); -} - - -static const char *get_prompt (lua_State *L, int firstline) { - const char *p; - lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); - return p; -} - -/* mark in error messages for incomplete statements */ -#define EOFMARK "" -#define marklen (sizeof(EOFMARK)/sizeof(char) - 1) - -static int incomplete (lua_State *L, int status) { - if (status == LUA_ERRSYNTAX) { - size_t lmsg; - const char *msg = lua_tolstring(L, -1, &lmsg); - if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { - lua_pop(L, 1); - return 1; - } - } - return 0; /* else... */ -} - - -static int pushline (lua_State *L, int firstline) { - char buffer[LUA_MAXINPUT]; - char *b = buffer; - size_t l; - const char *prmt = get_prompt(L, firstline); - int readstatus = lua_readline(L, b, prmt); - lua_pop(L, 1); /* remove result from 'get_prompt' */ - if (readstatus == 0) - return 0; /* no input */ - l = strlen(b); - if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[l-1] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* first line starts with `=' ? */ - lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ - else - lua_pushstring(L, b); - lua_freeline(L, b); - return 1; -} - - -static int loadline (lua_State *L) { - int status; - lua_settop(L, 0); - if (!pushline(L, 1)) - return -1; /* no input */ - for (;;) { /* repeat until gets a complete line */ - size_t l; - const char *line = lua_tolstring(L, 1, &l); - status = luaL_loadbuffer(L, line, l, "=stdin"); - if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (!pushline(L, 0)) /* no more input? */ - return -1; - lua_pushliteral(L, "\n"); /* add a new line... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } - lua_saveline(L, 1); - lua_remove(L, 1); /* remove line */ - return status; -} - - -static void dotty (lua_State *L) { - int status; - const char *oldprogname = progname; - progname = NULL; - while ((status = loadline(L)) != -1) { - if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); - report(L, status); - if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ - luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) - l_message(progname, lua_pushfstring(L, - "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); - } - } - lua_settop(L, 0); /* clear stack */ - luai_writeline(); - progname = oldprogname; -} - - -static int handle_script (lua_State *L, char **argv, int n) { - int status; - const char *fname; - int narg = getargs(L, argv, n); /* collect arguments */ - lua_setglobal(L, "arg"); - fname = argv[n]; - if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - lua_insert(L, -(narg+1)); - if (status == LUA_OK) - status = docall(L, narg, LUA_MULTRET); - else - lua_pop(L, narg); - return report(L, status); -} - - -/* check that argument has no extra characters at the end */ -#define noextrachars(x) {if ((x)[2] != '\0') return -1;} - - -/* indices of various argument indicators in array args */ -#define has_i 0 /* -i */ -#define has_v 1 /* -v */ -#define has_e 2 /* -e */ -#define has_E 3 /* -E */ - -#define num_has 4 /* number of 'has_*' */ - - -static int collectargs (char **argv, int *args) { - int i; - for (i = 1; argv[i] != NULL; i++) { - if (argv[i][0] != '-') /* not an option? */ - return i; - switch (argv[i][1]) { /* option */ - case '-': - noextrachars(argv[i]); - return (argv[i+1] != NULL ? i+1 : 0); - case '\0': - return i; - case 'E': - args[has_E] = 1; - break; - case 'i': - noextrachars(argv[i]); - args[has_i] = 1; /* go through */ - case 'v': - noextrachars(argv[i]); - args[has_v] = 1; - break; - case 'e': - args[has_e] = 1; /* go through */ - case 'l': /* both options need an argument */ - if (argv[i][2] == '\0') { /* no concatenated argument? */ - i++; /* try next 'argv' */ - if (argv[i] == NULL || argv[i][0] == '-') - return -(i - 1); /* no next argument or it is another option */ - } - break; - default: /* invalid option; return its index... */ - return -i; /* ...as a negative value */ - } - } - return 0; -} - - -static int runargs (lua_State *L, char **argv, int n) { - int i; - for (i = 1; i < n; i++) { - lua_assert(argv[i][0] == '-'); - switch (argv[i][1]) { /* option */ - case 'e': { - const char *chunk = argv[i] + 2; - if (*chunk == '\0') chunk = argv[++i]; - lua_assert(chunk != NULL); - if (dostring(L, chunk, "=(command line)") != LUA_OK) - return 0; - break; - } - case 'l': { - const char *filename = argv[i] + 2; - if (*filename == '\0') filename = argv[++i]; - lua_assert(filename != NULL); - if (dolibrary(L, filename) != LUA_OK) - return 0; /* stop if file fails */ - break; - } - default: break; - } - } - return 1; -} - - -static int handle_luainit (lua_State *L) { - const char *name = "=" LUA_INITVERSION; - const char *init = getenv(name + 1); - if (init == NULL) { - name = "=" LUA_INIT; - init = getenv(name + 1); /* try alternative name */ - } - if (init == NULL) return LUA_OK; - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, name); -} - - -static int pmain (lua_State *L) { - int argc = (int)lua_tointeger(L, 1); - char **argv = (char **)lua_touserdata(L, 2); - int script; - int args[num_has]; - args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; - if (argv[0] && argv[0][0]) progname = argv[0]; - script = collectargs(argv, args); - if (script < 0) { /* invalid arg? */ - print_usage(argv[-script]); - return 0; - } - if (args[has_v]) print_version(); - if (args[has_E]) { /* option '-E'? */ - lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } - /* open standard libraries */ - luaL_checkversion(L); - lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ - luaL_openlibs(L); /* open libraries */ - lua_gc(L, LUA_GCRESTART, 0); - if (!args[has_E] && handle_luainit(L) != LUA_OK) - return 0; /* error running LUA_INIT */ - /* execute arguments -e and -l */ - if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; - /* execute main script (if there is one) */ - if (script && handle_script(L, argv, script) != LUA_OK) return 0; - if (args[has_i]) /* -i option? */ - dotty(L); - else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ - if (lua_stdin_is_tty()) { - print_version(); - dotty(L); - } - else dofile(L, NULL); /* executes stdin as a file */ - } - lua_pushboolean(L, 1); /* signal no errors */ - return 1; -} - - -int main (int argc, char **argv) { - int status, result; - lua_State *L = luaL_newstate(); /* create state */ - if (L == NULL) { - l_message(argv[0], "cannot create state: not enough memory"); - return EXIT_FAILURE; - } - /* call 'pmain' in protected mode */ - lua_pushcfunction(L, &pmain); - lua_pushinteger(L, argc); /* 1st argument */ - lua_pushlightuserdata(L, argv); /* 2nd argument */ - status = lua_pcall(L, 2, 1, 0); - result = lua_toboolean(L, -1); /* get result */ - finalreport(L, status); - lua_close(L); - return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.h deleted file mode 100644 index ff4a108..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.h +++ /dev/null @@ -1,444 +0,0 @@ -/* -** $Id: lua.h,v 1.285.1.4 2015/02/21 14:04:50 roberto Exp $ -** Lua - A Scripting Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) -** See Copyright Notice at the end of this file -*/ - - -#ifndef lua_h -#define lua_h - -#include -#include - - -#include "luaconf.h" - - -#define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "2" -#define LUA_VERSION_NUM 502 -#define LUA_VERSION_RELEASE "4" - -#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" - - -/* mark for precompiled code ('Lua') */ -#define LUA_SIGNATURE "\033Lua" - -/* option for multiple returns in 'lua_pcall' and 'lua_call' */ -#define LUA_MULTRET (-1) - - -/* -** pseudo-indices -*/ -#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX -#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) - - -/* thread status */ -#define LUA_OK 0 -#define LUA_YIELD 1 -#define LUA_ERRRUN 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRGCMM 5 -#define LUA_ERRERR 6 - - -typedef struct lua_State lua_State; - -typedef int (*lua_CFunction) (lua_State *L); - - -/* -** functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); - - -/* -** prototype for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - -#define LUA_NUMTAGS 9 - - - -/* minimum Lua stack available to a C function */ -#define LUA_MINSTACK 20 - - -/* predefined values in the registry */ -#define LUA_RIDX_MAINTHREAD 1 -#define LUA_RIDX_GLOBALS 2 -#define LUA_RIDX_LAST LUA_RIDX_GLOBALS - - -/* type of numbers in Lua */ -typedef LUA_NUMBER lua_Number; - - -/* type for integer functions */ -typedef LUA_INTEGER lua_Integer; - -/* unsigned integer type */ -typedef LUA_UNSIGNED lua_Unsigned; - - - -/* -** generic extra include file -*/ -#if defined(LUA_USER_H) -#include LUA_USER_H -#endif - - -/* -** RCS ident string -*/ -extern const char lua_ident[]; - - -/* -** state manipulation -*/ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); -LUA_API void (lua_close) (lua_State *L); -LUA_API lua_State *(lua_newthread) (lua_State *L); - -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); - - -LUA_API const lua_Number *(lua_version) (lua_State *L); - - -/* -** basic stack manipulation -*/ -LUA_API int (lua_absindex) (lua_State *L, int idx); -LUA_API int (lua_gettop) (lua_State *L); -LUA_API void (lua_settop) (lua_State *L, int idx); -LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_remove) (lua_State *L, int idx); -LUA_API void (lua_insert) (lua_State *L, int idx); -LUA_API void (lua_replace) (lua_State *L, int idx); -LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); -LUA_API int (lua_checkstack) (lua_State *L, int sz); - -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); - - -/* -** access functions (stack -> C) -*/ - -LUA_API int (lua_isnumber) (lua_State *L, int idx); -LUA_API int (lua_isstring) (lua_State *L, int idx); -LUA_API int (lua_iscfunction) (lua_State *L, int idx); -LUA_API int (lua_isuserdata) (lua_State *L, int idx); -LUA_API int (lua_type) (lua_State *L, int idx); -LUA_API const char *(lua_typename) (lua_State *L, int tp); - -LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); -LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); -LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum); -LUA_API int (lua_toboolean) (lua_State *L, int idx); -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API size_t (lua_rawlen) (lua_State *L, int idx); -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); -LUA_API void *(lua_touserdata) (lua_State *L, int idx); -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); -LUA_API const void *(lua_topointer) (lua_State *L, int idx); - - -/* -** Comparison and arithmetic functions -*/ - -#define LUA_OPADD 0 /* ORDER TM */ -#define LUA_OPSUB 1 -#define LUA_OPMUL 2 -#define LUA_OPDIV 3 -#define LUA_OPMOD 4 -#define LUA_OPPOW 5 -#define LUA_OPUNM 6 - -LUA_API void (lua_arith) (lua_State *L, int op); - -#define LUA_OPEQ 0 -#define LUA_OPLT 1 -#define LUA_OPLE 2 - -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); - - -/* -** push functions (C -> stack) -*/ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n); -LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); -LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, - va_list argp); -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); -LUA_API void (lua_pushboolean) (lua_State *L, int b); -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); -LUA_API int (lua_pushthread) (lua_State *L); - - -/* -** get functions (Lua -> stack) -*/ -LUA_API void (lua_getglobal) (lua_State *L, const char *var); -LUA_API void (lua_gettable) (lua_State *L, int idx); -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawget) (lua_State *L, int idx); -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); -LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p); -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API void (lua_getuservalue) (lua_State *L, int idx); - - -/* -** set functions (stack -> Lua) -*/ -LUA_API void (lua_setglobal) (lua_State *L, const char *var); -LUA_API void (lua_settable) (lua_State *L, int idx); -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); -LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API void (lua_setuservalue) (lua_State *L, int idx); - - -/* -** 'load' and 'call' functions (load and run Lua code) -*/ -LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, - lua_CFunction k); -#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) - -LUA_API int (lua_getctx) (lua_State *L, int *ctx); - -LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_CFunction k); -#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) - -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, - const char *mode); - -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); - - -/* -** coroutine functions -*/ -LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, - lua_CFunction k); -#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) -LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); -LUA_API int (lua_status) (lua_State *L); - -/* -** garbage-collection function and options -*/ - -#define LUA_GCSTOP 0 -#define LUA_GCRESTART 1 -#define LUA_GCCOLLECT 2 -#define LUA_GCCOUNT 3 -#define LUA_GCCOUNTB 4 -#define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 -#define LUA_GCSETMAJORINC 8 -#define LUA_GCISRUNNING 9 -#define LUA_GCGEN 10 -#define LUA_GCINC 11 - -LUA_API int (lua_gc) (lua_State *L, int what, int data); - - -/* -** miscellaneous functions -*/ - -LUA_API int (lua_error) (lua_State *L); - -LUA_API int (lua_next) (lua_State *L, int idx); - -LUA_API void (lua_concat) (lua_State *L, int n); -LUA_API void (lua_len) (lua_State *L, int idx); - -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); -LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); - - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) -#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) -#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_newtable(L) lua_createtable(L, 0, 0) - -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) - -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) - -#define lua_pushglobaltable(L) \ - lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) - -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) - - - -/* -** {====================================================================== -** Debug API -** ======================================================================= -*/ - - -/* -** Event codes -*/ -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILCALL 4 - - -/* -** Event masks -*/ -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debugger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); -LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); -LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); -LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); - -LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); -LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, - int fidx2, int n2); - -LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); -LUA_API lua_Hook (lua_gethook) (lua_State *L); -LUA_API int (lua_gethookmask) (lua_State *L); -LUA_API int (lua_gethookcount) (lua_State *L); - - -struct lua_Debug { - int event; - const char *name; /* (n) */ - const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ - const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ - const char *source; /* (S) */ - int currentline; /* (l) */ - int linedefined; /* (S) */ - int lastlinedefined; /* (S) */ - unsigned char nups; /* (u) number of upvalues */ - unsigned char nparams;/* (u) number of parameters */ - char isvararg; /* (u) */ - char istailcall; /* (t) */ - char short_src[LUA_IDSIZE]; /* (S) */ - /* private part */ - struct CallInfo *i_ci; /* active function */ -}; - -/* }====================================================================== */ - - -/****************************************************************************** -* Copyright (C) 1994-2015 Lua.org, PUC-Rio. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.hpp b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.hpp deleted file mode 100644 index ec417f5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lua.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// lua.hpp -// Lua header files for C++ -// <> not supplied automatically because Lua also compiles as C++ - -extern "C" { -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/luac.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/luac.c deleted file mode 100644 index 7409706..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/luac.c +++ /dev/null @@ -1,432 +0,0 @@ -/* -** $Id: luac.c,v 1.69 2011/11/29 17:46:33 lhf Exp $ -** Lua compiler (saves bytecodes to files; also list bytecodes) -** See Copyright Notice in lua.h -*/ - -#include -#include -#include -#include - -#define luac_c -#define LUA_CORE - -#include "lua.h" -#include "lauxlib.h" - -#include "lobject.h" -#include "lstate.h" -#include "lundump.h" - -static void PrintFunction(const Proto* f, int full); -#define luaU_print PrintFunction - -#define PROGNAME "luac" /* default program name */ -#define OUTPUT PROGNAME ".out" /* default output file */ - -static int listing=0; /* list bytecodes? */ -static int dumping=1; /* dump bytecodes? */ -static int stripping=0; /* strip debug information? */ -static char Output[]={ OUTPUT }; /* default output file name */ -static const char* output=Output; /* actual output file name */ -static const char* progname=PROGNAME; /* actual program name */ - -static void fatal(const char* message) -{ - fprintf(stderr,"%s: %s\n",progname,message); - exit(EXIT_FAILURE); -} - -static void cannot(const char* what) -{ - fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); - exit(EXIT_FAILURE); -} - -static void usage(const char* message) -{ - if (*message=='-') - fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); - else - fprintf(stderr,"%s: %s\n",progname,message); - fprintf(stderr, - "usage: %s [options] [filenames]\n" - "Available options are:\n" - " -l list (use -l -l for full listing)\n" - " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" - " -p parse only\n" - " -s strip debug information\n" - " -v show version information\n" - " -- stop handling options\n" - " - stop handling options and process stdin\n" - ,progname,Output); - exit(EXIT_FAILURE); -} - -#define IS(s) (strcmp(argv[i],s)==0) - -static int doargs(int argc, char* argv[]) -{ - int i; - int version=0; - if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; - for (i=1; itop+(i)) - -static const Proto* combine(lua_State* L, int n) -{ - if (n==1) - return toproto(L,-1); - else - { - Proto* f; - int i=n; - if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); - f=toproto(L,-1); - for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; - } - f->sizelineinfo=0; - return f; - } -} - -static int writer(lua_State* L, const void* p, size_t size, void* u) -{ - UNUSED(L); - return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); -} - -static int pmain(lua_State* L) -{ - int argc=(int)lua_tointeger(L,1); - char** argv=(char**)lua_touserdata(L,2); - const Proto* f; - int i; - if (!lua_checkstack(L,argc)) fatal("too many input files"); - for (i=0; i1); - if (dumping) - { - FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); - if (D==NULL) cannot("open"); - lua_lock(L); - luaU_dump(L,f,writer,D,stripping); - lua_unlock(L); - if (ferror(D)) cannot("write"); - if (fclose(D)) cannot("close"); - } - return 0; -} - -int main(int argc, char* argv[]) -{ - lua_State* L; - int i=doargs(argc,argv); - argc-=i; argv+=i; - if (argc<=0) usage("no input files given"); - L=luaL_newstate(); - if (L==NULL) fatal("cannot create state: not enough memory"); - lua_pushcfunction(L,&pmain); - lua_pushinteger(L,argc); - lua_pushlightuserdata(L,argv); - if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); - lua_close(L); - return EXIT_SUCCESS; -} - -/* -** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $ -** print bytecodes -** See Copyright Notice in lua.h -*/ - -#include -#include - -#define luac_c -#define LUA_CORE - -#include "ldebug.h" -#include "lobject.h" -#include "lopcodes.h" - -#define VOID(p) ((const void*)(p)) - -static void PrintString(const TString* ts) -{ - const char* s=getstr(ts); - size_t i,n=ts->tsv.len; - printf("%c",'"'); - for (i=0; ik[i]; - switch (ttypenv(o)) - { - case LUA_TNIL: - printf("nil"); - break; - case LUA_TBOOLEAN: - printf(bvalue(o) ? "true" : "false"); - break; - case LUA_TNUMBER: - printf(LUA_NUMBER_FMT,nvalue(o)); - break; - case LUA_TSTRING: - PrintString(rawtsvalue(o)); - break; - default: /* cannot happen */ - printf("? type=%d",ttype(o)); - break; - } -} - -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") -#define MYK(x) (-1-(x)) - -static void PrintCode(const Proto* f) -{ - const Instruction* code=f->code; - int pc,n=f->sizecode; - for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); - printf("%-9s\t",luaP_opnames[o]); - switch (getOpMode(o)) - { - case iABC: - printf("%d",a); - if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); - if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); - break; - case iABx: - printf("%d",a); - if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); - if (getBMode(o)==OpArgU) printf(" %d",bx); - break; - case iAsBx: - printf("%d %d",a,sbx); - break; - case iAx: - printf("%d",MYK(ax)); - break; - } - switch (o) - { - case OP_LOADK: - printf("\t; "); PrintConstant(f,bx); - break; - case OP_GETUPVAL: - case OP_SETUPVAL: - printf("\t; %s",UPVALNAME(b)); - break; - case OP_GETTABUP: - printf("\t; %s",UPVALNAME(b)); - if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } - break; - case OP_SETTABUP: - printf("\t; %s",UPVALNAME(a)); - if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } - if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } - break; - case OP_GETTABLE: - case OP_SELF: - if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } - break; - case OP_SETTABLE: - case OP_ADD: - case OP_SUB: - case OP_MUL: - case OP_DIV: - case OP_POW: - case OP_EQ: - case OP_LT: - case OP_LE: - if (ISK(b) || ISK(c)) - { - printf("\t; "); - if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); - printf(" "); - if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); - } - break; - case OP_JMP: - case OP_FORLOOP: - case OP_FORPREP: - case OP_TFORLOOP: - printf("\t; to %d",sbx+pc+2); - break; - case OP_CLOSURE: - printf("\t; %p",VOID(f->p[bx])); - break; - case OP_SETLIST: - if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); - break; - case OP_EXTRAARG: - printf("\t; "); PrintConstant(f,ax); - break; - default: - break; - } - printf("\n"); - } -} - -#define SS(x) ((x==1)?"":"s") -#define S(x) (int)(x),SS(x) - -static void PrintHeader(const Proto* f) -{ - const char* s=f->source ? getstr(f->source) : "=?"; - if (*s=='@' || *s=='=') - s++; - else if (*s==LUA_SIGNATURE[0]) - s="(bstring)"; - else - s="(string)"; - printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", - (f->linedefined==0)?"main":"function",s, - f->linedefined,f->lastlinedefined, - S(f->sizecode),VOID(f)); - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", - (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), - S(f->maxstacksize),S(f->sizeupvalues)); - printf("%d local%s, %d constant%s, %d function%s\n", - S(f->sizelocvars),S(f->sizek),S(f->sizep)); -} - -static void PrintDebug(const Proto* f) -{ - int i,n; - n=f->sizek; - printf("constants (%d) for %p:\n",n,VOID(f)); - for (i=0; isizelocvars; - printf("locals (%d) for %p:\n",n,VOID(f)); - for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); - } - n=f->sizeupvalues; - printf("upvalues (%d) for %p:\n",n,VOID(f)); - for (i=0; iupvalues[i].instack,f->upvalues[i].idx); - } -} - -static void PrintFunction(const Proto* f, int full) -{ - int i,n=f->sizep; - PrintHeader(f); - PrintCode(f); - if (full) PrintDebug(f); - for (i=0; ip[i],full); -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/luaconf.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/luaconf.h deleted file mode 100644 index 1b0ff59..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/luaconf.h +++ /dev/null @@ -1,551 +0,0 @@ -/* -** $Id: luaconf.h,v 1.176.1.2 2013/11/21 17:26:16 roberto Exp $ -** Configuration file for Lua -** See Copyright Notice in lua.h -*/ - - -#ifndef lconfig_h -#define lconfig_h - -#include -#include - - -/* -** ================================================================== -** Search for "@@" to find all configurable definitions. -** =================================================================== -*/ - - -/* -@@ LUA_ANSI controls the use of non-ansi features. -** CHANGE it (define it) if you want Lua to avoid the use of any -** non-ansi feature or library. -*/ -#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__) -#define LUA_ANSI -#endif - - -#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE) -#define LUA_WIN /* enable goodies for regular Windows platforms */ -#endif - -#if defined(LUA_WIN) -#define LUA_DL_DLL -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#endif - - - -#if defined(LUA_USE_LINUX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ -#define LUA_USE_READLINE /* needs some extra libraries */ -#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#define LUA_USE_LONGLONG /* assume support for long long */ -#endif - -#if defined(LUA_USE_MACOSX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* does not need -ldl */ -#define LUA_USE_READLINE /* needs an extra library: -lreadline */ -#define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */ -#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ -#define LUA_USE_LONGLONG /* assume support for long long */ -#endif - - - -/* -@@ LUA_USE_POSIX includes all functionality listed as X/Open System -@* Interfaces Extension (XSI). -** CHANGE it (define it) if your system is XSI compatible. -*/ -#if defined(LUA_USE_POSIX) -#define LUA_USE_MKSTEMP -#define LUA_USE_ISATTY -#define LUA_USE_POPEN -#define LUA_USE_ULONGJMP -#define LUA_USE_GMTIME_R -#endif - - - -/* -@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for -@* Lua libraries. -@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for -@* C libraries. -** CHANGE them if your machine has a non-conventional directory -** hierarchy or if you want to install your libraries in -** non-conventional directories. -*/ -#if defined(_WIN32) /* { */ -/* -** In Windows, any exclamation mark ('!') in the path is replaced by the -** path of the directory of the executable file of the current process. -*/ -#define LUA_LDIR "!\\lua\\" -#define LUA_CDIR "!\\" -#define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" ".\\?.lua" -#define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll" - -#else /* }{ */ - -#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/" -#define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR -#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR -#define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" "./?.lua" -#define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" -#endif /* } */ - - -/* -@@ LUA_DIRSEP is the directory separator (for submodules). -** CHANGE it if your machine does not use "/" as the directory separator -** and is not Windows. (On Windows Lua automatically uses "\".) -*/ -#if defined(_WIN32) -#define LUA_DIRSEP "\\" -#else -#define LUA_DIRSEP "/" -#endif - - -/* -@@ LUA_ENV is the name of the variable that holds the current -@@ environment, used to access global names. -** CHANGE it if you do not like this name. -*/ -#define LUA_ENV "_ENV" - - -/* -@@ LUA_API is a mark for all core API functions. -@@ LUALIB_API is a mark for all auxiliary library functions. -@@ LUAMOD_API is a mark for all standard library opening functions. -** CHANGE them if you need to define those functions in some special way. -** For instance, if you want to create one Windows DLL with the core and -** the libraries, you may want to use the following definition (define -** LUA_BUILD_AS_DLL to get it). -*/ -#if defined(LUA_BUILD_AS_DLL) /* { */ - -#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ -#define LUA_API __declspec(dllexport) -#else /* }{ */ -#define LUA_API __declspec(dllimport) -#endif /* } */ - -#else /* }{ */ - -#define LUA_API extern - -#endif /* } */ - - -/* more often than not the libs go together with the core */ -#define LUALIB_API LUA_API -#define LUAMOD_API LUALIB_API - - -/* -@@ LUAI_FUNC is a mark for all extern functions that are not to be -@* exported to outside modules. -@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables -@* that are not to be exported to outside modules (LUAI_DDEF for -@* definitions and LUAI_DDEC for declarations). -** CHANGE them if you need to mark them in some special way. Elf/gcc -** (versions 3.2 and later) mark them as "hidden" to optimize access -** when Lua is compiled as a shared library. Not all elf targets support -** this attribute. Unfortunately, gcc does not offer a way to check -** whether the target offers that support, and those without support -** give a warning about it. To avoid these warnings, change to the -** default definition. -*/ -#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("hidden"))) extern -#define LUAI_DDEC LUAI_FUNC -#define LUAI_DDEF /* empty */ - -#else /* }{ */ -#define LUAI_FUNC extern -#define LUAI_DDEC extern -#define LUAI_DDEF /* empty */ -#endif /* } */ - - - -/* -@@ LUA_QL describes how error messages quote program elements. -** CHANGE it if you want a different appearance. -*/ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - - -/* -@@ LUA_IDSIZE gives the maximum size for the description of the source -@* of a function in debug information. -** CHANGE it if you want a different size. -*/ -#define LUA_IDSIZE 60 - - -/* -@@ luai_writestring/luai_writeline define how 'print' prints its results. -** They are only used in libraries and the stand-alone program. (The #if -** avoids including 'stdio.h' everywhere.) -*/ -#if defined(LUA_LIB) || defined(lua_c) -#include -#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout)) -#endif - -/* -@@ luai_writestringerror defines how to print error messages. -** (A format string with one argument is enough for Lua...) -*/ -#define luai_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) - - -/* -@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is, -** strings that are internalized. (Cannot be smaller than reserved words -** or tags for metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#define LUAI_MAXSHORTLEN 40 - - - -/* -** {================================================================== -** Compatibility with previous versions -** =================================================================== -*/ - -/* -@@ LUA_COMPAT_ALL controls all compatibility options. -** You can define it to get all options, or change specific options -** to fit your specific needs. -*/ -#if defined(LUA_COMPAT_ALL) /* { */ - -/* -@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. -** You can replace it with 'table.unpack'. -*/ -#define LUA_COMPAT_UNPACK - -/* -@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. -** You can replace it with 'package.searchers'. -*/ -#define LUA_COMPAT_LOADERS - -/* -@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. -** You can call your C function directly (with light C functions). -*/ -#define lua_cpcall(L,f,u) \ - (lua_pushcfunction(L, (f)), \ - lua_pushlightuserdata(L,(u)), \ - lua_pcall(L,1,0,0)) - - -/* -@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. -** You can rewrite 'log10(x)' as 'log(x, 10)'. -*/ -#define LUA_COMPAT_LOG10 - -/* -@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base -** library. You can rewrite 'loadstring(s)' as 'load(s)'. -*/ -#define LUA_COMPAT_LOADSTRING - -/* -@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. -*/ -#define LUA_COMPAT_MAXN - -/* -@@ The following macros supply trivial compatibility for some -** changes in the API. The macros themselves document how to -** change your code to avoid using them. -*/ -#define lua_strlen(L,i) lua_rawlen(L, (i)) - -#define lua_objlen(L,i) lua_rawlen(L, (i)) - -#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) -#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) - -/* -@@ LUA_COMPAT_MODULE controls compatibility with previous -** module functions 'module' (Lua) and 'luaL_register' (C). -*/ -#define LUA_COMPAT_MODULE - -#endif /* } */ - -/* }================================================================== */ - - - -/* -@@ LUAI_BITSINT defines the number of bits in an int. -** CHANGE here if Lua cannot automatically detect the number of bits of -** your machine. Probably you do not need to change this. -*/ -/* avoid overflows in comparison */ -#if INT_MAX-20 < 32760 /* { */ -#define LUAI_BITSINT 16 -#elif INT_MAX > 2147483640L /* }{ */ -/* int has at least 32 bits */ -#define LUAI_BITSINT 32 -#else /* }{ */ -#error "you must define LUA_BITSINT with number of bits in an integer" -#endif /* } */ - - -/* -@@ LUA_INT32 is a signed integer with exactly 32 bits. -@@ LUAI_UMEM is an unsigned integer big enough to count the total -@* memory used by Lua. -@@ LUAI_MEM is a signed integer big enough to count the total memory -@* used by Lua. -** CHANGE here if for some weird reason the default definitions are not -** good enough for your machine. Probably you do not need to change -** this. -*/ -#if LUAI_BITSINT >= 32 /* { */ -#define LUA_INT32 int -#define LUAI_UMEM size_t -#define LUAI_MEM ptrdiff_t -#else /* }{ */ -/* 16-bit ints */ -#define LUA_INT32 long -#define LUAI_UMEM unsigned long -#define LUAI_MEM long -#endif /* } */ - - -/* -@@ LUAI_MAXSTACK limits the size of the Lua stack. -** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua from consuming unlimited stack -** space (and to reserve some numbers for pseudo-indices). -*/ -#if LUAI_BITSINT >= 32 -#define LUAI_MAXSTACK 1000000 -#else -#define LUAI_MAXSTACK 15000 -#endif - -/* reserve some space for error handling */ -#define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000) - - - - -/* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -** CHANGE it if it uses too much C-stack space. -*/ -#define LUAL_BUFFERSIZE BUFSIZ - - - - -/* -** {================================================================== -@@ LUA_NUMBER is the type of numbers in Lua. -** CHANGE the following definitions only if you want to build Lua -** with a number type different from double. You may also need to -** change lua_number2int & lua_number2integer. -** =================================================================== -*/ - -#define LUA_NUMBER_DOUBLE -#define LUA_NUMBER double - -/* -@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' -@* over a number. -*/ -#define LUAI_UACNUMBER double - - -/* -@@ LUA_NUMBER_SCAN is the format for reading numbers. -@@ LUA_NUMBER_FMT is the format for writing numbers. -@@ lua_number2str converts a number to a string. -@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. -*/ -#define LUA_NUMBER_SCAN "%lf" -#define LUA_NUMBER_FMT "%.14g" -#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) -#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ - - -/* -@@ l_mathop allows the addition of an 'l' or 'f' to all math operations -*/ -#define l_mathop(x) (x) - - -/* -@@ lua_str2number converts a decimal numeric string to a number. -@@ lua_strx2number converts an hexadecimal numeric string to a number. -** In C99, 'strtod' does both conversions. C89, however, has no function -** to convert floating hexadecimal strings to numbers. For these -** systems, you can leave 'lua_strx2number' undefined and Lua will -** provide its own implementation. -*/ -#define lua_str2number(s,p) strtod((s), (p)) - -#if defined(LUA_USE_STRTODHEX) -#define lua_strx2number(s,p) strtod((s), (p)) -#endif - - -/* -@@ The luai_num* macros define the primitive operations over numbers. -*/ - -/* the following operations need the math library */ -#if defined(lobject_c) || defined(lvm_c) -#include -#define luai_nummod(L,a,b) ((a) - l_mathop(floor)((a)/(b))*(b)) -#define luai_numpow(L,a,b) (l_mathop(pow)(a,b)) -#endif - -/* these are quite standard operations */ -#if defined(LUA_CORE) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numdiv(L,a,b) ((a)/(b)) -#define luai_numunm(L,a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(L,a,b) ((a)<(b)) -#define luai_numle(L,a,b) ((a)<=(b)) -#define luai_numisnan(L,a) (!luai_numeq((a), (a))) -#endif - - - -/* -@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. -** CHANGE that if ptrdiff_t is not adequate on your machine. (On most -** machines, ptrdiff_t gives a good choice between int or long.) -*/ -#define LUA_INTEGER ptrdiff_t - -/* -@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned. -** It must have at least 32 bits. -*/ -#define LUA_UNSIGNED unsigned LUA_INT32 - - - -/* -** Some tricks with doubles -*/ - -#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ -/* -** The next definitions activate some tricks to speed up the -** conversion from doubles to integer types, mainly to LUA_UNSIGNED. -** -@@ LUA_MSASMTRICK uses Microsoft assembler to avoid clashes with a -** DirectX idiosyncrasy. -** -@@ LUA_IEEE754TRICK uses a trick that should work on any machine -** using IEEE754 with a 32-bit integer type. -** -@@ LUA_IEEELL extends the trick to LUA_INTEGER; should only be -** defined when LUA_INTEGER is a 32-bit integer. -** -@@ LUA_IEEEENDIAN is the endianness of doubles in your machine -** (0 for little endian, 1 for big endian); if not defined, Lua will -** check it dynamically for LUA_IEEE754TRICK (but not for LUA_NANTRICK). -** -@@ LUA_NANTRICK controls the use of a trick to pack all types into -** a single double value, using NaN values to represent non-number -** values. The trick only works on 32-bit machines (ints and pointers -** are 32-bit values) with numbers represented as IEEE 754-2008 doubles -** with conventional endianess (12345678 or 87654321), in CPUs that do -** not produce signaling NaN values (all NaNs are quiet). -*/ - -/* Microsoft compiler on a Pentium (32 bit) ? */ -#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */ - -#define LUA_MSASMTRICK -#define LUA_IEEEENDIAN 0 -#define LUA_NANTRICK - - -/* pentium 32 bits? */ -#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEELL -#define LUA_IEEEENDIAN 0 -#define LUA_NANTRICK - -/* pentium 64 bits? */ -#elif defined(__x86_64) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEEENDIAN 0 - -#elif defined(__POWERPC__) || defined(__ppc__) /* }{ */ - -#define LUA_IEEE754TRICK -#define LUA_IEEEENDIAN 1 - -#else /* }{ */ - -/* assume IEEE754 and a 32-bit integer type */ -#define LUA_IEEE754TRICK - -#endif /* } */ - -#endif /* } */ - -/* }================================================================== */ - - - - -/* =================================================================== */ - -/* -** Local configuration. You can use this space to add your redefinitions -** without modifying the main part of the file. -*/ - - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lualib.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lualib.h deleted file mode 100644 index da82005..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lualib.h +++ /dev/null @@ -1,55 +0,0 @@ -/* -** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua standard libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lualib_h -#define lualib_h - -#include "lua.h" - - - -LUAMOD_API int (luaopen_base) (lua_State *L); - -#define LUA_COLIBNAME "coroutine" -LUAMOD_API int (luaopen_coroutine) (lua_State *L); - -#define LUA_TABLIBNAME "table" -LUAMOD_API int (luaopen_table) (lua_State *L); - -#define LUA_IOLIBNAME "io" -LUAMOD_API int (luaopen_io) (lua_State *L); - -#define LUA_OSLIBNAME "os" -LUAMOD_API int (luaopen_os) (lua_State *L); - -#define LUA_STRLIBNAME "string" -LUAMOD_API int (luaopen_string) (lua_State *L); - -#define LUA_BITLIBNAME "bit32" -LUAMOD_API int (luaopen_bit32) (lua_State *L); - -#define LUA_MATHLIBNAME "math" -LUAMOD_API int (luaopen_math) (lua_State *L); - -#define LUA_DBLIBNAME "debug" -LUAMOD_API int (luaopen_debug) (lua_State *L); - -#define LUA_LOADLIBNAME "package" -LUAMOD_API int (luaopen_package) (lua_State *L); - - -/* open all previous libraries */ -LUALIB_API void (luaL_openlibs) (lua_State *L); - - - -#if !defined(lua_assert) -#define lua_assert(x) ((void)0) -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.c deleted file mode 100644 index 4163cb5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.c +++ /dev/null @@ -1,258 +0,0 @@ -/* -** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#include - -#define lundump_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstring.h" -#include "lundump.h" -#include "lzio.h" - -typedef struct { - lua_State* L; - ZIO* Z; - Mbuffer* b; - const char* name; -} LoadState; - -static l_noret error(LoadState* S, const char* why) -{ - luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); - luaD_throw(S->L,LUA_ERRSYNTAX); -} - -#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) -#define LoadByte(S) (lu_byte)LoadChar(S) -#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) -#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) - -#if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ -#endif - -static void LoadBlock(LoadState* S, void* b, size_t size) -{ - if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); -} - -static int LoadChar(LoadState* S) -{ - char x; - LoadVar(S,x); - return x; -} - -static int LoadInt(LoadState* S) -{ - int x; - LoadVar(S,x); - if (x<0) error(S,"corrupted"); - return x; -} - -static lua_Number LoadNumber(LoadState* S) -{ - lua_Number x; - LoadVar(S,x); - return x; -} - -static TString* LoadString(LoadState* S) -{ - size_t size; - LoadVar(S,size); - if (size==0) - return NULL; - else - { - char* s=luaZ_openspace(S->L,S->b,size); - LoadBlock(S,s,size*sizeof(char)); - return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ - } -} - -static void LoadCode(LoadState* S, Proto* f) -{ - int n=LoadInt(S); - f->code=luaM_newvector(S->L,n,Instruction); - f->sizecode=n; - LoadVector(S,f->code,n,sizeof(Instruction)); -} - -static void LoadFunction(LoadState* S, Proto* f); - -static void LoadConstants(LoadState* S, Proto* f) -{ - int i,n; - n=LoadInt(S); - f->k=luaM_newvector(S->L,n,TValue); - f->sizek=n; - for (i=0; ik[i]); - for (i=0; ik[i]; - int t=LoadChar(S); - switch (t) - { - case LUA_TNIL: - setnilvalue(o); - break; - case LUA_TBOOLEAN: - setbvalue(o,LoadChar(S)); - break; - case LUA_TNUMBER: - setnvalue(o,LoadNumber(S)); - break; - case LUA_TSTRING: - setsvalue2n(S->L,o,LoadString(S)); - break; - default: lua_assert(0); - } - } - n=LoadInt(S); - f->p=luaM_newvector(S->L,n,Proto*); - f->sizep=n; - for (i=0; ip[i]=NULL; - for (i=0; ip[i]=luaF_newproto(S->L); - LoadFunction(S,f->p[i]); - } -} - -static void LoadUpvalues(LoadState* S, Proto* f) -{ - int i,n; - n=LoadInt(S); - f->upvalues=luaM_newvector(S->L,n,Upvaldesc); - f->sizeupvalues=n; - for (i=0; iupvalues[i].name=NULL; - for (i=0; iupvalues[i].instack=LoadByte(S); - f->upvalues[i].idx=LoadByte(S); - } -} - -static void LoadDebug(LoadState* S, Proto* f) -{ - int i,n; - f->source=LoadString(S); - n=LoadInt(S); - f->lineinfo=luaM_newvector(S->L,n,int); - f->sizelineinfo=n; - LoadVector(S,f->lineinfo,n,sizeof(int)); - n=LoadInt(S); - f->locvars=luaM_newvector(S->L,n,LocVar); - f->sizelocvars=n; - for (i=0; ilocvars[i].varname=NULL; - for (i=0; ilocvars[i].varname=LoadString(S); - f->locvars[i].startpc=LoadInt(S); - f->locvars[i].endpc=LoadInt(S); - } - n=LoadInt(S); - for (i=0; iupvalues[i].name=LoadString(S); -} - -static void LoadFunction(LoadState* S, Proto* f) -{ - f->linedefined=LoadInt(S); - f->lastlinedefined=LoadInt(S); - f->numparams=LoadByte(S); - f->is_vararg=LoadByte(S); - f->maxstacksize=LoadByte(S); - LoadCode(S,f); - LoadConstants(S,f); - LoadUpvalues(S,f); - LoadDebug(S,f); -} - -/* the code below must be consistent with the code in luaU_header */ -#define N0 LUAC_HEADERSIZE -#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) -#define N2 N1+2 -#define N3 N2+6 - -static void LoadHeader(LoadState* S) -{ - lu_byte h[LUAC_HEADERSIZE]; - lu_byte s[LUAC_HEADERSIZE]; - luaU_header(h); - memcpy(s,h,sizeof(char)); /* first char already read */ - LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); - if (memcmp(h,s,N0)==0) return; - if (memcmp(h,s,N1)!=0) error(S,"not a"); - if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); - if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); -} - -/* -** load precompiled chunk -*/ -Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) -{ - LoadState S; - Closure* cl; - if (*name=='@' || *name=='=') - S.name=name+1; - else if (*name==LUA_SIGNATURE[0]) - S.name="binary string"; - else - S.name=name; - S.L=L; - S.Z=Z; - S.b=buff; - LoadHeader(&S); - cl=luaF_newLclosure(L,1); - setclLvalue(L,L->top,cl); incr_top(L); - cl->l.p=luaF_newproto(L); - LoadFunction(&S,cl->l.p); - if (cl->l.p->sizeupvalues != 1) - { - Proto* p=cl->l.p; - cl=luaF_newLclosure(L,cl->l.p->sizeupvalues); - cl->l.p=p; - setclLvalue(L,L->top-1,cl); - } - luai_verifycode(L,buff,cl->l.p); - return cl; -} - -#define MYINT(s) (s[0]-'0') -#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) -#define FORMAT 0 /* this is the official format */ - -/* -* make header for precompiled chunks -* if you change the code below be sure to update LoadHeader and FORMAT above -* and LUAC_HEADERSIZE in lundump.h -*/ -void luaU_header (lu_byte* h) -{ - int x=1; - memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); - h+=sizeof(LUA_SIGNATURE)-sizeof(char); - *h++=cast_byte(VERSION); - *h++=cast_byte(FORMAT); - *h++=cast_byte(*(char*)&x); /* endianness */ - *h++=cast_byte(sizeof(int)); - *h++=cast_byte(sizeof(size_t)); - *h++=cast_byte(sizeof(Instruction)); - *h++=cast_byte(sizeof(lua_Number)); - *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ - memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.h deleted file mode 100644 index 5255db2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lundump.h +++ /dev/null @@ -1,28 +0,0 @@ -/* -** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#ifndef lundump_h -#define lundump_h - -#include "lobject.h" -#include "lzio.h" - -/* load one chunk; from lundump.c */ -LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); - -/* make header; from lundump.c */ -LUAI_FUNC void luaU_header (lu_byte* h); - -/* dump one chunk; from ldump.c */ -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); - -/* data to catch conversion errors */ -#define LUAC_TAIL "\x19\x93\r\n\x1a\n" - -/* size in bytes of header of binary files */ -#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.c deleted file mode 100644 index 141b9fd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.c +++ /dev/null @@ -1,867 +0,0 @@ -/* -** $Id: lvm.c,v 2.155.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - - -#include -#include -#include - -#define lvm_c -#define LUA_CORE - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - - -/* limit for table tag-method chains (to avoid loops) */ -#define MAXTAGLOOP 100 - - -const TValue *luaV_tonumber (const TValue *obj, TValue *n) { - lua_Number num; - if (ttisnumber(obj)) return obj; - if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { - setnvalue(n, num); - return n; - } - else - return NULL; -} - - -int luaV_tostring (lua_State *L, StkId obj) { - if (!ttisnumber(obj)) - return 0; - else { - char s[LUAI_MAXNUMBER2STR]; - lua_Number n = nvalue(obj); - int l = lua_number2str(s, n); - setsvalue2s(L, obj, luaS_newlstr(L, s, l)); - return 1; - } -} - - -static void traceexec (lua_State *L) { - CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; - int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); - if (counthook) - resethookcount(L); /* reset count */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ - ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ - return; /* do not call hook again (VM yielded, so it did not move) */ - } - if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(ci)->p; - int npc = pcRel(ci->u.l.savedpc, p); - int newline = getfuncline(p, npc); - if (npc == 0 || /* call linehook when enter a new function, */ - ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ - luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ - } - L->oldpc = ci->u.l.savedpc; - if (L->status == LUA_YIELD) { /* did hook yield? */ - if (counthook) - L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ - ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - ci->func = L->top - 1; /* protect stack below results */ - luaD_throw(L, LUA_YIELD); - } -} - - -static void callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres) { - ptrdiff_t result = savestack(L, p3); - setobj2s(L, L->top++, f); /* push function */ - setobj2s(L, L->top++, p1); /* 1st argument */ - setobj2s(L, L->top++, p2); /* 2nd argument */ - if (!hasres) /* no result? 'p3' is third argument */ - setobj2s(L, L->top++, p3); /* 3rd argument */ - /* metamethod may yield only when called from Lua code */ - luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); - if (hasres) { /* if has result, move it to its place */ - p3 = restorestack(L, result); - setobjs2s(L, p3, --L->top); - } -} - - -void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - const TValue *res = luaH_get(h, key); /* do a primitive get */ - if (!ttisnil(res) || /* result is not nil? */ - (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ - setobj2s(L, val, res); - return; - } - /* else will try the tag method */ - } - else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) - luaG_typeerror(L, t, "index"); - if (ttisfunction(tm)) { - callTM(L, tm, t, key, val, 1); - return; - } - t = tm; /* else repeat with 'tm' */ - } - luaG_runerror(L, "loop in gettable"); -} - - -void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - TValue *oldval = cast(TValue *, luaH_get(h, key)); - /* if previous value is not nil, there must be a previous entry - in the table; moreover, a metamethod has no relevance */ - if (!ttisnil(oldval) || - /* previous value is nil; must check the metamethod */ - ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && - /* no metamethod; is there a previous entry in the table? */ - (oldval != luaO_nilobject || - /* no previous entry; must create one. (The next test is - always true; we only need the assignment.) */ - (oldval = luaH_newkey(L, h, key), 1)))) { - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, oldval, val); /* assign new value to that entry */ - invalidateTMcache(h); - luaC_barrierback(L, obj2gco(h), val); - return; - } - /* else will try the metamethod */ - } - else /* not a table; check metamethod */ - if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) - luaG_typeerror(L, t, "index"); - /* there is a metamethod */ - if (ttisfunction(tm)) { - callTM(L, tm, t, key, val, 0); - return; - } - t = tm; /* else repeat with 'tm' */ - } - luaG_runerror(L, "loop in settable"); -} - - -static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ - if (ttisnil(tm)) - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (ttisnil(tm)) return 0; - callTM(L, tm, p1, p2, res, 1); - return 1; -} - - -static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, - TMS event) { - const TValue *tm1 = fasttm(L, mt1, event); - const TValue *tm2; - if (tm1 == NULL) return NULL; /* no metamethod */ - if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ - tm2 = fasttm(L, mt2, event); - if (tm2 == NULL) return NULL; /* no metamethod */ - if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */ - return tm1; - return NULL; -} - - -static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, - TMS event) { - if (!call_binTM(L, p1, p2, L->top, event)) - return -1; /* no metamethod */ - else - return !l_isfalse(L->top); -} - - -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = ls->tsv.len; - const char *r = getstr(rs); - size_t lr = rs->tsv.len; - for (;;) { - int temp = strcoll(l, r); - if (temp != 0) return temp; - else { /* strings are equal up to a `\0' */ - size_t len = strlen(l); /* index of first `\0' in both strings */ - if (len == lr) /* r is finished? */ - return (len == ll) ? 0 : 1; - else if (len == ll) /* l is finished? */ - return -1; /* l is smaller than r (because r is not finished) */ - /* both strings longer than `len'; go on comparing (after the `\0') */ - len++; - l += len; ll -= len; r += len; lr -= len; - } - } -} - - -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttisnumber(l) && ttisnumber(r)) - return luai_numlt(L, nvalue(l), nvalue(r)); - else if (ttisstring(l) && ttisstring(r)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; - else if ((res = call_orderTM(L, l, r, TM_LT)) < 0) - luaG_ordererror(L, l, r); - return res; -} - - -int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttisnumber(l) && ttisnumber(r)) - return luai_numle(L, nvalue(l), nvalue(r)); - else if (ttisstring(l) && ttisstring(r)) - return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; - else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */ - return res; - else if ((res = call_orderTM(L, r, l, TM_LT)) < 0) /* else try `lt' */ - luaG_ordererror(L, l, r); - return !res; -} - - -/* -** equality of Lua values. L == NULL means raw equality (no metamethods) -*/ -int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) { - const TValue *tm; - lua_assert(ttisequal(t1, t2)); - switch (ttype(t1)) { - case LUA_TNIL: return 1; - case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); - case LUA_TLCF: return fvalue(t1) == fvalue(t2); - case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2)); - case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2)); - case LUA_TUSERDATA: { - if (uvalue(t1) == uvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - case LUA_TTABLE: { - if (hvalue(t1) == hvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - default: - lua_assert(iscollectable(t1)); - return gcvalue(t1) == gcvalue(t2); - } - if (tm == NULL) return 0; /* no TM? */ - callTM(L, tm, t1, t2, L->top, 1); /* call TM */ - return !l_isfalse(L->top); -} - - -void luaV_concat (lua_State *L, int total) { - lua_assert(total >= 2); - do { - StkId top = L->top; - int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) - luaG_concaterror(L, top-2, top-1); - } - else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ - (void)tostring(L, top - 2); /* result is first operand */ - else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) { - setobjs2s(L, top - 2, top - 1); /* result is second op. */ - } - else { - /* at least two non-empty string values; get as many as possible */ - size_t tl = tsvalue(top-1)->len; - char *buffer; - int i; - /* collect total length */ - for (i = 1; i < total && tostring(L, top-i-1); i++) { - size_t l = tsvalue(top-i-1)->len; - if (l >= (MAX_SIZET/sizeof(char)) - tl) - luaG_runerror(L, "string length overflow"); - tl += l; - } - buffer = luaZ_openspace(L, &G(L)->buff, tl); - tl = 0; - n = i; - do { /* concat all strings */ - size_t l = tsvalue(top-i)->len; - memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); - tl += l; - } while (--i > 0); - setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); - } - total -= n-1; /* got 'n' strings to create 1 new */ - L->top -= n-1; /* popped 'n' strings and pushed one */ - } while (total > 1); /* repeat until only 1 result left */ -} - - -void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { - const TValue *tm; - switch (ttypenv(rb)) { - case LUA_TTABLE: { - Table *h = hvalue(rb); - tm = fasttm(L, h->metatable, TM_LEN); - if (tm) break; /* metamethod? break switch to call it */ - setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */ - return; - } - case LUA_TSTRING: { - setnvalue(ra, cast_num(tsvalue(rb)->len)); - return; - } - default: { /* try metamethod */ - tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (ttisnil(tm)) /* no metamethod? */ - luaG_typeerror(L, rb, "get length of"); - break; - } - } - callTM(L, tm, rb, rb, ra, 1); -} - - -void luaV_arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op) { - TValue tempb, tempc; - const TValue *b, *c; - if ((b = luaV_tonumber(rb, &tempb)) != NULL && - (c = luaV_tonumber(rc, &tempc)) != NULL) { - lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c)); - setnvalue(ra, res); - } - else if (!call_binTM(L, rb, rc, ra, op)) - luaG_aritherror(L, rb, rc); -} - - -/* -** check whether cached closure in prototype 'p' may be reused, that is, -** whether there is a cached closure with the same upvalues needed by -** new closure to be created. -*/ -static Closure *getcached (Proto *p, UpVal **encup, StkId base) { - Closure *c = p->cache; - if (c != NULL) { /* is there a cached closure? */ - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; - int i; - for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ - TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; - if (c->l.upvals[i]->v != v) - return NULL; /* wrong upvalue; cannot reuse closure */ - } - } - return c; /* return cached closure (or NULL if no cached closure) */ -} - - -/* -** create a new Lua closure, push it in the stack, and initialize -** its upvalues. Note that the call to 'luaC_barrierproto' must come -** before the assignment to 'p->cache', as the function needs the -** original value of that field. -*/ -static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, - StkId ra) { - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; - int i; - Closure *ncl = luaF_newLclosure(L, nup); - ncl->l.p = p; - setclLvalue(L, ra, ncl); /* anchor new closure in stack */ - for (i = 0; i < nup; i++) { /* fill in its upvalues */ - if (uv[i].instack) /* upvalue refers to local variable? */ - ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx); - else /* get upvalue from enclosing function */ - ncl->l.upvals[i] = encup[uv[i].idx]; - } - luaC_barrierproto(L, p, ncl); - p->cache = ncl; /* save it on cache for reuse */ -} - - -/* -** finish execution of an opcode interrupted by an yield -*/ -void luaV_finishOp (lua_State *L) { - CallInfo *ci = L->ci; - StkId base = ci->u.l.base; - Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ - OpCode op = GET_OPCODE(inst); - switch (op) { /* finish its execution */ - case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: - case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN: - case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { - setobjs2s(L, base + GETARG_A(inst), --L->top); - break; - } - case OP_LE: case OP_LT: case OP_EQ: { - int res = !l_isfalse(L->top - 1); - L->top--; - /* metamethod should not be called when operand is K */ - lua_assert(!ISK(GETARG_B(inst))); - if (op == OP_LE && /* "<=" using "<" instead? */ - ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE))) - res = !res; /* invert result */ - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_A(inst)) /* condition failed? */ - ci->u.l.savedpc++; /* skip jump instruction */ - break; - } - case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'call_binTM' was called */ - int b = GETARG_B(inst); /* first element to concatenate */ - int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ - setobj2s(L, top - 2, top); /* put TM result in proper position */ - if (total > 1) { /* are there elements to concat? */ - L->top = top - 1; /* top is one after last element (at top-2) */ - luaV_concat(L, total); /* concat them (may yield again) */ - } - /* move final result to final position */ - setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1); - L->top = ci->top; /* restore top */ - break; - } - case OP_TFORCALL: { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); - L->top = ci->top; /* correct top */ - break; - } - case OP_CALL: { - if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ - L->top = ci->top; /* adjust results */ - break; - } - case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: - break; - default: lua_assert(0); - } -} - - - -/* -** some macros for common tasks in `luaV_execute' -*/ - -#if !defined luai_runtimecheck -#define luai_runtimecheck(L, c) /* void */ -#endif - - -#define RA(i) (base+GETARG_A(i)) -/* to be used after possible stack reallocation */ -#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) -#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) -#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) -#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) -#define KBx(i) \ - (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++))) - - -/* execute a jump instruction */ -#define dojump(ci,i,e) \ - { int a = GETARG_A(i); \ - if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \ - ci->u.l.savedpc += GETARG_sBx(i) + e; } - -/* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); } - - -#define Protect(x) { {x;}; base = ci->u.l.base; } - -#define checkGC(L,c) \ - Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \ - luaC_step(L); \ - L->top = ci->top;}) /* restore top */ \ - luai_threadyield(L); ) - - -#define arith_op(op,tm) { \ - TValue *rb = RKB(i); \ - TValue *rc = RKC(i); \ - if (ttisnumber(rb) && ttisnumber(rc)) { \ - lua_Number nb = nvalue(rb), nc = nvalue(rc); \ - setnvalue(ra, op(L, nb, nc)); \ - } \ - else { Protect(luaV_arith(L, ra, rb, rc, tm)); } } - - -#define vmdispatch(o) switch(o) -#define vmcase(l,b) case l: {b} break; -#define vmcasenb(l,b) case l: {b} /* nb = no break */ - -void luaV_execute (lua_State *L) { - CallInfo *ci = L->ci; - LClosure *cl; - TValue *k; - StkId base; - newframe: /* reentry point when frame changes (call/return) */ - lua_assert(ci == L->ci); - cl = clLvalue(ci->func); - k = cl->p->k; - base = ci->u.l.base; - /* main loop of interpreter */ - for (;;) { - Instruction i = *(ci->u.l.savedpc++); - StkId ra; - if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && - (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { - Protect(traceexec(L)); - } - /* WARNING: several calls may realloc the stack and invalidate `ra' */ - ra = RA(i); - lua_assert(base == ci->u.l.base); - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); - vmdispatch (GET_OPCODE(i)) { - vmcase(OP_MOVE, - setobjs2s(L, ra, RB(i)); - ) - vmcase(OP_LOADK, - TValue *rb = k + GETARG_Bx(i); - setobj2s(L, ra, rb); - ) - vmcase(OP_LOADKX, - TValue *rb; - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - rb = k + GETARG_Ax(*ci->u.l.savedpc++); - setobj2s(L, ra, rb); - ) - vmcase(OP_LOADBOOL, - setbvalue(ra, GETARG_B(i)); - if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ - ) - vmcase(OP_LOADNIL, - int b = GETARG_B(i); - do { - setnilvalue(ra++); - } while (b--); - ) - vmcase(OP_GETUPVAL, - int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); - ) - vmcase(OP_GETTABUP, - int b = GETARG_B(i); - Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); - ) - vmcase(OP_GETTABLE, - Protect(luaV_gettable(L, RB(i), RKC(i), ra)); - ) - vmcase(OP_SETTABUP, - int a = GETARG_A(i); - Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); - ) - vmcase(OP_SETUPVAL, - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, ra); - luaC_barrier(L, uv, ra); - ) - vmcase(OP_SETTABLE, - Protect(luaV_settable(L, ra, RKB(i), RKC(i))); - ) - vmcase(OP_NEWTABLE, - int b = GETARG_B(i); - int c = GETARG_C(i); - Table *t = luaH_new(L); - sethvalue(L, ra, t); - if (b != 0 || c != 0) - luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); - checkGC(L, ra + 1); - ) - vmcase(OP_SELF, - StkId rb = RB(i); - setobjs2s(L, ra+1, rb); - Protect(luaV_gettable(L, rb, RKC(i), ra)); - ) - vmcase(OP_ADD, - arith_op(luai_numadd, TM_ADD); - ) - vmcase(OP_SUB, - arith_op(luai_numsub, TM_SUB); - ) - vmcase(OP_MUL, - arith_op(luai_nummul, TM_MUL); - ) - vmcase(OP_DIV, - arith_op(luai_numdiv, TM_DIV); - ) - vmcase(OP_MOD, - arith_op(luai_nummod, TM_MOD); - ) - vmcase(OP_POW, - arith_op(luai_numpow, TM_POW); - ) - vmcase(OP_UNM, - TValue *rb = RB(i); - if (ttisnumber(rb)) { - lua_Number nb = nvalue(rb); - setnvalue(ra, luai_numunm(L, nb)); - } - else { - Protect(luaV_arith(L, ra, rb, rb, TM_UNM)); - } - ) - vmcase(OP_NOT, - TValue *rb = RB(i); - int res = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(ra, res); - ) - vmcase(OP_LEN, - Protect(luaV_objlen(L, ra, RB(i))); - ) - vmcase(OP_CONCAT, - int b = GETARG_B(i); - int c = GETARG_C(i); - StkId rb; - L->top = base + c + 1; /* mark the end of concat operands */ - Protect(luaV_concat(L, c - b + 1)); - ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */ - rb = b + base; - setobjs2s(L, ra, rb); - checkGC(L, (ra >= rb ? ra + 1 : rb)); - L->top = ci->top; /* restore top */ - ) - vmcase(OP_JMP, - dojump(ci, i, 0); - ) - vmcase(OP_EQ, - TValue *rb = RKB(i); - TValue *rc = RKC(i); - Protect( - if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - ) - vmcase(OP_LT, - Protect( - if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - ) - vmcase(OP_LE, - Protect( - if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - ) - vmcase(OP_TEST, - if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - vmcase(OP_TESTSET, - TValue *rb = RB(i); - if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) - ci->u.l.savedpc++; - else { - setobjs2s(L, ra, rb); - donextjump(ci); - } - ) - vmcase(OP_CALL, - int b = GETARG_B(i); - int nresults = GETARG_C(i) - 1; - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - if (luaD_precall(L, ra, nresults)) { /* C function? */ - if (nresults >= 0) L->top = ci->top; /* adjust results */ - base = ci->u.l.base; - } - else { /* Lua function */ - ci = L->ci; - ci->callstatus |= CIST_REENTRY; - goto newframe; /* restart luaV_execute over new Lua function */ - } - ) - vmcase(OP_TAILCALL, - int b = GETARG_B(i); - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ - base = ci->u.l.base; - else { - /* tail call: put called frame (n) in place of caller one (o) */ - CallInfo *nci = L->ci; /* called frame */ - CallInfo *oci = nci->previous; /* caller frame */ - StkId nfunc = nci->func; /* called function */ - StkId ofunc = oci->func; /* caller function */ - /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->numparams; - int aux; - /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); - /* move new frame into old one */ - for (aux = 0; nfunc + aux < lim; aux++) - setobjs2s(L, ofunc + aux, nfunc + aux); - oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */ - oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ - oci->u.l.savedpc = nci->u.l.savedpc; - oci->callstatus |= CIST_TAIL; /* function was tail called */ - ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); - goto newframe; /* restart luaV_execute over new Lua function */ - } - ) - vmcasenb(OP_RETURN, - int b = GETARG_B(i); - if (b != 0) L->top = ra+b-1; - if (cl->p->sizep > 0) luaF_close(L, base); - b = luaD_poscall(L, ra); - if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ - return; /* external invocation: return */ - else { /* invocation via reentry: continue execution */ - ci = L->ci; - if (b) L->top = ci->top; - lua_assert(isLua(ci)); - lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); - goto newframe; /* restart luaV_execute over new Lua function */ - } - ) - vmcase(OP_FORLOOP, - lua_Number step = nvalue(ra+2); - lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */ - lua_Number limit = nvalue(ra+1); - if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit) - : luai_numle(L, limit, idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - setnvalue(ra, idx); /* update internal index... */ - setnvalue(ra+3, idx); /* ...and external index */ - } - ) - vmcase(OP_FORPREP, - const TValue *init = ra; - const TValue *plimit = ra+1; - const TValue *pstep = ra+2; - if (!tonumber(init, ra)) - luaG_runerror(L, LUA_QL("for") " initial value must be a number"); - else if (!tonumber(plimit, ra+1)) - luaG_runerror(L, LUA_QL("for") " limit must be a number"); - else if (!tonumber(pstep, ra+2)) - luaG_runerror(L, LUA_QL("for") " step must be a number"); - setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep))); - ci->u.l.savedpc += GETARG_sBx(i); - ) - vmcasenb(OP_TFORCALL, - StkId cb = ra + 3; /* call base */ - setobjs2s(L, cb+2, ra+2); - setobjs2s(L, cb+1, ra+1); - setobjs2s(L, cb, ra); - L->top = cb + 3; /* func. + 2 args (state and index) */ - Protect(luaD_call(L, cb, GETARG_C(i), 1)); - L->top = ci->top; - i = *(ci->u.l.savedpc++); /* go to next instruction */ - ra = RA(i); - lua_assert(GET_OPCODE(i) == OP_TFORLOOP); - goto l_tforloop; - ) - vmcase(OP_TFORLOOP, - l_tforloop: - if (!ttisnil(ra + 1)) { /* continue loop? */ - setobjs2s(L, ra, ra + 1); /* save control variable */ - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - } - ) - vmcase(OP_SETLIST, - int n = GETARG_B(i); - int c = GETARG_C(i); - int last; - Table *h; - if (n == 0) n = cast_int(L->top - ra) - 1; - if (c == 0) { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - c = GETARG_Ax(*ci->u.l.savedpc++); - } - luai_runtimecheck(L, ttistable(ra)); - h = hvalue(ra); - last = ((c-1)*LFIELDS_PER_FLUSH) + n; - if (last > h->sizearray) /* needs more space? */ - luaH_resizearray(L, h, last); /* pre-allocate it at once */ - for (; n > 0; n--) { - TValue *val = ra+n; - luaH_setint(L, h, last--, val); - luaC_barrierback(L, obj2gco(h), val); - } - L->top = ci->top; /* correct top (in case of previous open call) */ - ) - vmcase(OP_CLOSURE, - Proto *p = cl->p->p[GETARG_Bx(i)]; - Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */ - if (ncl == NULL) /* no match? */ - pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ - else - setclLvalue(L, ra, ncl); /* push cashed closure */ - checkGC(L, ra + 1); - ) - vmcase(OP_VARARG, - int b = GETARG_B(i) - 1; - int j; - int n = cast_int(base - ci->func) - cl->p->numparams - 1; - if (b < 0) { /* B == 0? */ - b = n; /* get all var. arguments */ - Protect(luaD_checkstack(L, n)); - ra = RA(i); /* previous call may change the stack */ - L->top = ra + n; - } - for (j = 0; j < b; j++) { - if (j < n) { - setobjs2s(L, ra + j, base - n + j); - } - else { - setnilvalue(ra + j); - } - } - ) - vmcase(OP_EXTRAARG, - lua_assert(0); - ) - } - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.h deleted file mode 100644 index 5380270..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lvm.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lvm_h -#define lvm_h - - -#include "ldo.h" -#include "lobject.h" -#include "ltm.h" - - -#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) - -#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) - -#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) - -#define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) - - -/* not to called directly */ -LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); - - -LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); -LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); -LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, - StkId val); -LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, - StkId val); -LUAI_FUNC void luaV_finishOp (lua_State *L); -LUAI_FUNC void luaV_execute (lua_State *L); -LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, - const TValue *rc, TMS op); -LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.c b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.c deleted file mode 100644 index 20efea9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.c +++ /dev/null @@ -1,76 +0,0 @@ -/* -** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - - -#include - -#define lzio_c -#define LUA_CORE - -#include "lua.h" - -#include "llimits.h" -#include "lmem.h" -#include "lstate.h" -#include "lzio.h" - - -int luaZ_fill (ZIO *z) { - size_t size; - lua_State *L = z->L; - const char *buff; - lua_unlock(L); - buff = z->reader(L, z->data, &size); - lua_lock(L); - if (buff == NULL || size == 0) - return EOZ; - z->n = size - 1; /* discount char being returned */ - z->p = buff; - return cast_uchar(*(z->p++)); -} - - -void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { - z->L = L; - z->reader = reader; - z->data = data; - z->n = 0; - z->p = NULL; -} - - -/* --------------------------------------------------------------- read --- */ -size_t luaZ_read (ZIO *z, void *b, size_t n) { - while (n) { - size_t m; - if (z->n == 0) { /* no bytes in buffer? */ - if (luaZ_fill(z) == EOZ) /* try to read more */ - return n; /* no more input; return number of missing bytes */ - else { - z->n++; /* luaZ_fill consumed first byte; put it back */ - z->p--; - } - } - m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ - memcpy(b, z->p, m); - z->n -= m; - z->p += m; - b = (char *)b + m; - n -= m; - } - return 0; -} - -/* ------------------------------------------------------------------------ */ -char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { - if (n > buff->buffsize) { - if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; - luaZ_resizebuffer(L, buff, n); - } - return buff->buffer; -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.h b/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.h deleted file mode 100644 index 441f747..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.2.4/src/lzio.h +++ /dev/null @@ -1,65 +0,0 @@ -/* -** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - - -#ifndef lzio_h -#define lzio_h - -#include "lua.h" - -#include "lmem.h" - - -#define EOZ (-1) /* end of stream */ - -typedef struct Zio ZIO; - -#define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) - - -typedef struct Mbuffer { - char *buffer; - size_t n; - size_t buffsize; -} Mbuffer; - -#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) - -#define luaZ_buffer(buff) ((buff)->buffer) -#define luaZ_sizebuffer(buff) ((buff)->buffsize) -#define luaZ_bufflen(buff) ((buff)->n) - -#define luaZ_resetbuffer(buff) ((buff)->n = 0) - - -#define luaZ_resizebuffer(L, buff, size) \ - (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ - (buff)->buffsize = size) - -#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) - - -LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); -LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, - void *data); -LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ - - - -/* --------- Private Part ------------------ */ - -struct Zio { - size_t n; /* bytes still unread */ - const char *p; /* current position in buffer */ - lua_Reader reader; /* reader function */ - void* data; /* additional data */ - lua_State *L; /* Lua state (for reader) */ -}; - - -LUAI_FUNC int luaZ_fill (ZIO *z); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/README b/LuaBridge3/Tests/Lua/Lua.5.3.6/README deleted file mode 100644 index f8bdb6f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/README +++ /dev/null @@ -1,6 +0,0 @@ - -This is Lua 5.3.6, released on 14 Sep 2020. - -For installation instructions, license details, and -further information about Lua, see doc/readme.html. - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/contents.html b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/contents.html deleted file mode 100644 index 3a357b1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/contents.html +++ /dev/null @@ -1,650 +0,0 @@ - - - -Lua 5.3 Reference Manual - contents - - - - - - - -

-Lua -Lua 5.3 Reference Manual -

- -

-The reference manual is the official definition of the Lua language. -
-For a complete introduction to Lua programming, see the book -Programming in Lua. - -

- -

- -Copyright © 2015–2020 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

Contents

- - -

Index

- - - - - - - - - - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/index.css b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/index.css deleted file mode 100644 index c961835..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/index.css +++ /dev/null @@ -1,21 +0,0 @@ -ul { - list-style-type: none ; -} - -ul.contents { - padding: 0 ; -} - -table { - border: none ; - border-spacing: 0 ; - border-collapse: collapse ; -} - -td { - vertical-align: top ; - padding: 0 ; - text-align: left ; - line-height: 1.25 ; - width: 15% ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/logo.gif b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/logo.gif deleted file mode 100644 index 5c77eacc3b8f397fb2e87d2aaecd89128e53a117..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9893 zcmZ`JAZZ!6FBhF{g-pRY z?mq`C!p1xV^4?ZTR7k~j;V9G1HDS87U+{!}P$o&rgc4zX0NDT~riS`~QEcMXc4?@W z^+STR)$~fm_`ABgqdI|BbFIpHF{r}hP+etgD8-NQs7W75NaJI?Ql(hqjVakK+HRty zUXfx9(Bq-+-?*K3uw9ID@9?*;-eo#?myt-t^)SGPba%#*OB5Fu=j7)HeEWuc=*-K) z!oqjj0S)u!&5gi;Bahobc>^^a9XUOHT z2|mFhODfM>_&5x-TrvTHjN4n=t}cJHz|Oj&#(Ac#A<;QYj?%w(I{6NHetKEy88L^C$rQ3l)#a6 zoT#z!d-$MNnQ|f0h4|+;?CDm7g1UnqCpv3AVbZ0g!y5D)ht6oJG9OD4(C|vg-ir+_ zH4QGgnPSh+=ffel34{g3QbJKkb?GxJXlu)W&M4!QB^7H4b450C&zK>m4Sy@4- z6QwcXU;C4g#1AgjGlY}HQLNi?RV^MlIy=8Y#lo5nG5DcI$LoBU)7F-?yK5#MO(ZKV z4S#la_H=)P#gQKG^HdgSn#J9BwwdVYIM>{c*8F@wEO$d}R+sxjn>$#7D0Sp=?`**6 zSgb455aPSEq?K#BY9czOB~w zbadnT{PaZ|6MyUb3JQ8>r#+FkD@XhNG?}o-!{}_4A*5_Nyi-4?sw$?YhV}1AdjC?B zgUxkSi}=^CG*t3gMjJh01>e6T@3$!ESeZ^i-|I!u zS;Y;*lPMP-5pj1|J68PThiJLljogBy_@_?@umOG-J7a9_muB|%_5%Y0xG`uvyho24 zIB%XLJt0uPbhhSAqhnKw*C!-wew~ll#zHvKqtx}hu;K?rot-)2DP`g3vIG)P#bW#V z#78r@yHnXrnbSui-|;3*m#OEgl|Ar3-?ZbLj*ECy&1Z-(e4BAv822YtIM?~_>A{XCM0gm<2F`yi=~k^Qpkaj;z9R!JMds*m*#io0jdc7= z_32qZaeQ{RyoLtu;NW0GM#|0W?QI53?3wlUfYmFzOS9J|wG7ON3R+r2E3FBfX*p?^~m1 z6V|U~sT|y#!d_|FC~juNn3R-(U?c)K1cYB-XL+RJsusn5sJ+RfCJjfoveJPB2E8VY ze>-6GiZ=08p%V7#QAzD2(fGeZ=h&GUh!qrTD!o3Hx0HW92SHt(m5Rzw1vQg>ESIRL zceqjS=8oh<74zz*;XjH7mM-Q|N>rkd^6&&UHrgsF*A*1*Ny>erU}PL7XDcg0?d{O* z1OHq$wC4kBW@%Z_)RY<-8R=VoJu1hW%$PbbR=GYYPsi<1pLKq2BJ*9&lH*Mr1@h?- zk$H2rr%((U;g63+%JFe|?|D5i*-R){z1;cs_IMilux#weYs=fwFeyZelpm6zO3TXT zYfVu=SRoN`fR)c~#H3%xLrrVG){XLs%W+C*j!X)3XXS=o9L4Y7Y4c4Nn3e6iqIP!B$P!~=+%bVCorwZbea_d%{9_@+x)gMD z!A=L1w$D$>lAk8s9b;tVXJZqwMyRqA-fVf9jLHaCqMT)d%(`(nU#mfn-S)W6#P; z=3~G1aSyeny@xz{sqe(QAtQ%m7HZiLcGoU!#=I^tB;>)$tUp@4#&}2onV7?oPE35i zlo$@3HMJv8Y>)4(2i;^?lJIo!@e!ZH+B3jmXa9^CL-NPx#NG&|6hO2b&vPRZ#5%&5>oeqS2brR1*C$NtWqMvwVDOx5Y@S#^ z&_6%4yRdnC(v!H)L}^Ka+LW}D-Cm1w)+quD5+xy*Ed+psxoShNndF8Bm$M#>koguj z6f7);EM(I_VR11rgmli6^)M>r$(5Du>ykxlf{`JnO5?K)MC~1O8|-vR+p3I8AN~*= zT@6_!CDC>&*w)uvnR50H4tI|iW17v5G!4tni~8OiB$0i{-skaNkDJ7=p?>-!QAtuK zGH&qwyZv(MezW5r9PF&?$r6pDH(b=RG{xXlS@d8_Q?g~x9Zwmx@oP1+_x>s#?yX&A zJ<$i~41SNKh86dSYQui}G5(udqd)BUytgYi|Dx4(X$bQst#fSOqxZjt63ThVELa(< z=wI@Opzy_vOv)kuWxt8eUN7D5>|Apl8>nubM5%c9f7!7;r$k%CGhCKXT2 z-k!qzYR%Czzyy-azx++9a_n;OQf4Lr3KX3NpT!_=3b$18MH`>vz-i^}tMTVUZ z>>6?`x}LOt@qT{D&)`uV`Y3Z+ZZooW)2=6EW~SdY0o?^b4jI6ZC)@y&ZP@Uz`5D7Z zl7dqFba9n&vrI2DRB5j@vG&K0)N-wxvoW8ny|(k~ASD{ZgPGTs%RJA)pGADun)w|a z9dCPO-ENNA_?|AQmW!TZVZ#avB>&ydPA~a9V`hh<#3X&+zS}w3vd~Kp=B*6_p}bcV zUE^_+>f5sHi>P1s`6V5{K_R*fd;9HOnbE7L83#ush~4-^y8hd8)pE5F z;(t~CR|ixR&)!V>9E-Q}W7qM?>PHTG9O>l`kbNR(){jzBZ*ds4KT^iK9nC2MFeUHP z9338RB2IKBlpAx=O+0EmU0UsJqgAF_Yqf}8Q@d%~4+9$z#-MfW^efyWWMSd6uGf_+ zqjn?n8k;pT-XZ&lpw~#O~n_8?Twhc9ed6YFyb#LI=KCJ zvxB&XLT&i`?-Sp}FiYifPlEId6de`PfSdd$>PIn)jd(&d4rO4 z)`gJ$(5q|9mZSEj+U-Jyj68(nd8|~`FqMr8&xOZFD(-ejPhfMcleKc;TX^`F^Q}P@ zH8mx*zim<8h~pUTMM@?2#=mgs!BWd|-=d!OJHi|z39ur+V?Et3D{3|8<={wSG3*04*38Uo_r^=G9s|a2cvz-V z?>h*Y_J3Cw*8EqJ+E0vUf*#IF9rqE@D5hP3xQMXi$A5?SFx~#$VJzA8YVz(7Q})(U6RbGxnUHX$I?6)oBrzO(9Gijguu~Q)mrI3TvYr5V zlecf*E;%n+iHt!C3&ov}`Pd8MvETwj-e6#3A9{!ob!->gJ&Dyi?9fPmit;{cNQQD} zRloJu`6cIqVp~w*ZEHNPfMO=t?uJUuz+PjK>|*NYH$DV0sZDM5xKUD4+E5i?XUAS~ z&ENey8k;>nVL)7Oe!#gxo%g+OLzzHbZ^Q#Hr?fQO-~Uyl_=%{Dwe=v}>VpS~e)Zfd zq6(`zp2q6M3EN{ptaCKVO!w*q^{zTx5U{s93}a&p(^r#9_g$4P^C zhOAD1#@AZmX9yCK(qOMLy4SZxI3t?z`q%(R0dXh+nA(Xct8Ixl9UWcRw{PM_lWt44 zii-UG-&9%8)deq&#<(253+VH6aJtz4m08Z|cR8Fh9?iD7Y$R^gm6wr0OlHzMRz(`> zc#of!mL@4Fxf?IyQ+0dVt(2A~UOH1LC^|9d2W<@XX~FiNC;9vL0sMRjA2{kA)_ivs zeWeS@SXj{VCnOyl*v3ad<_MUZ(*T3SXZzS^K0w(^57NRwPnw6=RAub}_Hv4CRcFz|73xTIY87ZN%Eu-R<#d?Y0Mk9Z}Bv zYr7mCuc_(%=s#ii#emwHHkH8m#t`<|2Vzt7Ll=jtqy z)$u#R!;`EfUY`u5UOo|I+42}XT)zUCA%0A-^7`I=ZfR-o#M>pI(&x#FQO~-wA&6X` z)yeVAwEKme93k?X@U%31d;)%TT8MV@)M27H{Ks-Nz!g>w@Q)m&r)ECy?&D|eyj2WD z5o~z@y`E18lT9aWgGMI%u|qa35ieo1_7QK4-^D*)V(2O9s-SjK2ozF~_xSI1JUn=b zy>CfBmtzObTZ)&fd8j^&zW*o2Cny~%la4)hePrQ&alp>0b+@msxA}OtZYdw5sBHN6 ztY2{B#7A&&qxUPlZbskj8B0%J@YVO!y3g-G)<+CB0VRNfvmr2^%A)G?(XzvKx_J8c@z3|&{Jf#Q6hf-12#XCn z7r*I(05aTlBn=FvRV={-z0+e8P%4{pPQ2F+ylIcnR!zi{>dI$k4-)qKT;6A1muF^S zK@RG@;X6RE3#zLPC5zl$yRohr!qFKRGMCf%c85Adviqy5tKWrm?BiqK zJlr6{$2}y}emwh4R~Zu<+v)!rAvhG56B@nNgK8~TG-+s&BVCBA0)MH6Qz1MY zE!g%GI++q<31}P~DeNho66M2rTY);Fw)_xu*nZscis(d#~c9jyj zlO_C&>f}=Bs;a6})6=1f$k$?UuQo zZj|=1ToL2BBQGZkFdJ#5n4sGI_w=9=6GwLu$gdFr zh@3YGgbkL}(_R~PjdJ9F=gAn_f;2xJN7|XcD)GfrMW1)WQ8+fEHZ(*N2vyEHta{ym zJ9?aozqKTFj7fv5GVqnYQ;U7M9$e-XJy*c9ovSeEAX* z_qjGGA8%scLJA^GI>}8 z0u=`bV!4{EnHef=;?l|Mk|G&AF^v?P#5t_BW^7QU-Bp)8{tk;~`REdzZuECUqksL% z({@#k_d|!*Zz7-EdY61|W+q6R@mAYKgEF4Y!6Z-K>LVxg*3OQyfuyN0BkoWHzR ze@`Hb)#sM}c1JEkE<$Ag03DiREQpYh&<73>=auitEdPKE5Zio;M8>8#N3>mg$0#?fpooPYP3zvz$nr7G*V7M zL14Z_bdBXaYg~N14k9WJj)Z~&Du{QYWN+27M0$-FY3mA?660V3@hEr#80d}w6uBc2 z&xqbW5PdYDPRl#h-K81fsXxHpZz5AyS!#Y)UzBun<3>V4@)GI(`I&(y^V|Nuxg|$> ze-xu$mXxC-bvJjTp)D$GT`3va6usUGXWYHK56@_&k5|l4vR1Rs&x#osT?6V=nbSGf zNgVWviClW^l>IX%D-Hsnz=(>8T~WS@?M&=}0=Gz!$qFHiCcqgvoX^J_MPyEqLP0_T zb4;*2p#vi+Gj-|xXMBA0c)93JEa7Bg!0Ez*S5~L7_vr6)zKF6xBOtNG78De$aK-4l zeXw5Q`YxaS?ekGnkbI$1mB|Q9bQHQsm_-1i4t)GUm|kp&@+NF2q~fM^;guaSW5drtx9X1l{5mNq(_-&+XiTn`hG7xNubGh-OWvXylx2D+uyhNHEuTL`TXFZQESqr z@1Eb*h7^$7wv@4yyalP)=k5 ztp@8qNET4Yfq*ppnVftWU#Mi{zggx?1(g1hR?AF7gL#a<0p2DqkruOp>mR#??h!I0 zR-@x!50K!O9L8=}OuaAjnD3C{Cg-mEz^5}70Z{c=dST~_(OVbQUEHQ(>p!wd^mKqDerqMHD0f3E%r6}(tKvECnk z>(}yvt*>g`NS`kR``z|Sx)B}Knx6mB7#X>$cNmgf9t?h|w!Pd{EG=yrimJ8SROySp zxuRb~X=L@Xny;x`ErtJobBCTDE_E2|(CyLn{pxKWFOZv&h2JqMsGF|GO01kDe+PLy zCZ(8^n>%hm63f2HmqH2?iyi3eCughAUJ3jj;WSJ(drz_eRlI$h2K7!_=*#0RE{Ao- z{*C5-|KIZRazbu<7{D8Zh5h?Y13)cK=N#LbkXY?s#|?c8LrbUScHD&FT@po=jF7=Q zJI5p{jk#{)GgMSu>}TW67Q*eI6dHvD9)d)Qv9+aheE8Q%){c!2T~2dz&_sUSJl^?GmyDOdBCvz1``m^V5AmdwV#F{P!Yxje4v0fRqp zR0c8$iZ^pI5Eo^>9~8zQBpiJV?j}{#S(bKkib!XHvlbwrhY>}irA;d-W^Q^~(ea4e zl0Mr=%l4D*`m}qM3wX^%3z*^8)%&}$Pbt3P> zOI`DB@Oua*(rNKvFMnQaQ+!Zge6uu-jy?DDE9}LNN5%DPX8foO0k5=z0*XJA%Lm9y ztbj*;>5IhMb>xX|4e|g(z<0lEnM5zuuOs!7$(B5nUNdeqKYH|f!C65=1I!#H6_sfR z=11rir@n9RK8cG5s}JUTpuT(eJ6@i#@aGiQ8q^L#V}Pi6={Kru?FEmZD@?f}3`I;l z;N&?eL*5j)-;QP$&9%6V!Icvenn?mi!1w&9^B-mEvpr8HKt003u*hp~LzSO_-gw)I z^7mfb?O_#4x-f(IQQz9d;Oi<$@=)a`&r@G0chPW81!^O{>$xM(O#*HriRwpQA=EX3 z)q(Zueoq!$w_u^GFC6J^H_I;EZhw9?J3(@DrR%MAF1?v>rYjb&#x|B z5wUP^;**nilr?XOC|@QARsjTzb= zPGt)L{w+KLLbZnHC*_LSkkb&sA8R{;dV%7T@fN$QuPe${#(dgB1s@H9#(J0-7`DM} zDJUqIO-BjG&iP#b#ERz|OqZ(89XU4yh4PD;6~_@jyE2xP#6BLHKCs}*EZwjFB`Yc6 z0b|q!M&DE5=u=!zZv>7aH731o3vN^&n@_hdwLfe?ON)8E<$S*%bh7($vo%BGkKhQL zqKmEgPhOFT`v?)e!oXPs(1~bKHy8;T@`2sy6zKR1nfzs$S`zr7AgVnd7He-uT};rh z*gcgFZigXx)L&RrLn9+EOZVsMY`H{Vbge06VuxE>%?2;e0%_=3A)8MB4?w@Z8_rB8 zWPUxSzs?4&-QpUHB5(^FJ`A7AS5mo6Q4Msp|kLZhOt3Y&C` zRp8q$3q{o`2t|wv(n@LB7#R4k-Uic=H^OCSRqXJtrjERL4#q>QE$|z-aHeBf+eZzn zebx;(4rkY}-rUeC>gvF8IvJmt>HD2Xm&$D5ze)ksDA%0K$X@GlgXerA&<#W>E<(ne zlk_pfilr|2pbP*qG}^t<4`$`b_;n7{O>q@5X=rIT0#F1DMDS_WA zWNXXpvh0++8j2!fi*)&6r%G^q{%gO!S#C~FFa{MZ4gHU9eX~BJ{SI>{5~-Ec>eoUu zndqE>Qz{H#Q$Y$hTi6TFm5sXO9vRXn4JRusEHtb8oR`lZ4c~Um3oDF*D)~KzLLEj< zM~46lYXldOAt9MlS=IP^Nl8Fbk{l1u(gSyJAaz)z3I-OI@Rz*c1WX1H^%PVVz|8~O zE^@R+ec87V^{X{*Z#Sdy04dRvD_;?gqaZ{^OCOonX{;6mwFubrnDFzm1Vz;0KkMq; z603#?5Z&5tl9I4TGkN>LmWx!d{JFWgiExTe-vE`N*l06rN#-yV{Z zmKLUr_Y!)v29ArE*lXIvaO0sQ7J!kX5AWK(%xOSp%2Bb-kMMdf%cP8$K%%Yg{&iI8 zLy9H`I|JUt4jRd1@?e$Ew3|<@L!{zB=>UmiV`Ib9W?UW@TQ$46xfxv3ZnX6Sw`T)& z4^$^fkmWiOrT+y}hvP=Q><17rFdzl&!Q-TLwkU?z=Zl6U#8$PM8sw&nF~Opm8uUcz z-aN4gpf)$ni(KFXa2}Cp8usb85tYDcFb-k3J!vSsEeNWgkcO046OYr z{Pf~Ng@%Tv^V4T5C@KmmDxv|ZYGi5}pO(g>pKv_VRTT@F1+IT{xw)>AlK+Gm1vfWP z`h6yCF!&dfr3;12j?%k&zf_MeEc`B3%zxZqV8_JG%74{BrW8l<;yDQkL3MU^ib_bt z$HaUzH>XWaO@%@${PAKDC@6e%a$={Uq4C1a*_lGBI1(#S$y1B5Vy~sbIHg zXlS_TBd{4CAO8qA92mL7LrYe+$|)KmDcpLkj;L`AxUa0Kwp@fju)Rw4ESvsaq4Nva z^FaB+AS7H$$Mjh53i3+nShux3oMWM-qqF151Vs4H#DtK&J!_eIpscPg0R-yyDh@@= zCfjvk8=@y5_kgd#`0twjR-;WEPGdhXX>VISeU@4^LTa0+cn3CNy>}GTa5OS-H0Ck1 zHwGsND>DlR0}Cqy^9L0cW*$~{9#(D!W>y|%X0efPKmV(Nm5tF?6Sx1};6n@t9B2TM M5|b0H5Z3qqKj-ldMF0Q* diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.1 b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.1 deleted file mode 100644 index d728d0b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.1 +++ /dev/null @@ -1,112 +0,0 @@ -.\" $Id: lua.man,v 1.14 2016/10/17 15:43:50 lhf Exp $ -.TH LUA 1 "$Date: 2016/10/17 15:43:50 $" -.SH NAME -lua \- Lua interpreter -.SH SYNOPSIS -.B lua -[ -.I options -] -[ -.I script -[ -.I args -] -] -.SH DESCRIPTION -.B lua -is the standalone Lua interpreter. -It loads and executes Lua programs, -either in textual source form or -in precompiled binary form. -(Precompiled binaries are output by -.BR luac , -the Lua compiler.) -.B lua -can be used as a batch interpreter and also interactively. -.LP -The given -.I options -are handled in order and then -the Lua program in file -.I script -is loaded and executed. -The given -.I args -are available to -.I script -as strings in a global table named -.BR arg . -If no options or arguments are given, -then -.B "\-v \-i" -is assumed when the standard input is a terminal; -otherwise, -.B "\-" -is assumed. -.LP -In interactive mode, -.B lua -prompts the user, -reads lines from the standard input, -and executes them as they are read. -If the line contains an expression or list of expressions, -then the line is evaluated and the results are printed. -If a line does not contain a complete statement, -then a secondary prompt is displayed and -lines are read until a complete statement is formed or -a syntax error is found. -.LP -At the very start, -before even handling the command line, -.B lua -checks the contents of the environment variables -.B LUA_INIT_5_3 -or -.BR LUA_INIT , -in that order. -If the contents is of the form -.RI '@ filename ', -then -.I filename -is executed. -Otherwise, the string is assumed to be a Lua statement and is executed. -.SH OPTIONS -.TP -.BI \-e " stat" -execute statement -.IR stat . -.TP -.B \-i -enter interactive mode after executing -.IR script . -.TP -.BI \-l " name" -execute the equivalent of -.IB name =require(' name ') -before executing -.IR script . -.TP -.B \-v -show version information. -.TP -.B \-E -ignore environment variables. -.TP -.B \-\- -stop handling options. -.TP -.B \- -stop handling options and execute the standard input as a file. -.SH "SEE ALSO" -.BR luac (1) -.br -The documentation at lua.org, -especially section 7 of the reference manual. -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.css b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.css deleted file mode 100644 index cbd0799..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/lua.css +++ /dev/null @@ -1,161 +0,0 @@ -html { - background-color: #F8F8F8 ; -} - -body { - background-color: #FFFFFF ; - color: #000000 ; - font-family: Helvetica, Arial, sans-serif ; - text-align: justify ; - line-height: 1.25 ; - margin: 16px auto ; - padding: 32px ; - border: solid #ccc 1px ; - border-radius: 20px ; - max-width: 70em ; - width: 90% ; -} - -h1, h2, h3, h4 { - color: #000080 ; - font-family: Verdana, Geneva, sans-serif ; - font-weight: normal ; - font-style: normal ; - text-align: left ; -} - -h1 { - font-size: 28pt ; -} - -h1 img { - vertical-align: text-bottom ; -} - -h2:before { - content: "\2756" ; - padding-right: 0.5em ; -} - -a { - text-decoration: none ; -} - -a:link { - color: #000080 ; -} - -a:link:hover, a:visited:hover { - background-color: #D0D0FF ; - color: #000080 ; - border-radius: 4px ; -} - -a:link:active, a:visited:active { - color: #FF0000 ; -} - -div.menubar { - padding-bottom: 0.5em ; -} - -p.menubar { - margin-left: 2.5em ; -} - -.menubar a:hover { - margin: -3px -3px -3px -3px ; - padding: 3px 3px 3px 3px ; - border-radius: 4px ; -} - -:target { - background-color: #F0F0F0 ; - margin: -8px ; - padding: 8px ; - border-radius: 8px ; - outline: none ; -} - -hr { - display: none ; -} - -table hr { - background-color: #a0a0a0 ; - color: #a0a0a0 ; - border: 0 ; - height: 1px ; - display: block ; -} - -.footer { - color: gray ; - font-size: x-small ; - text-transform: lowercase ; -} - -input[type=text] { - border: solid #a0a0a0 2px ; - border-radius: 2em ; - background-image: url('images/search.png') ; - background-repeat: no-repeat ; - background-position: 4px center ; - padding-left: 20px ; - height: 2em ; -} - -pre.session { - background-color: #F8F8F8 ; - padding: 1em ; - border-radius: 8px ; -} - -table { - border: none ; - border-spacing: 0 ; - border-collapse: collapse ; -} - -td { - padding: 0 ; - margin: 0 ; -} - -td.gutter { - width: 4% ; -} - -table.columns td { - vertical-align: top ; - padding-bottom: 1em ; - text-align: justify ; - line-height: 1.25 ; -} - -table.book td { - vertical-align: top ; -} - -table.book td.cover { - padding-right: 1em ; -} - -table.book img { - border: solid #000080 1px ; -} - -table.book span { - font-size: small ; - text-align: left ; - display: block ; - margin-top: 0.25em ; -} - -p.logos a:link:hover, p.logos a:visited:hover { - background-color: inherit ; -} - -img { - background-color: white ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/luac.1 b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/luac.1 deleted file mode 100644 index 33a4ed0..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/luac.1 +++ /dev/null @@ -1,118 +0,0 @@ -.\" $Id: luac.man,v 1.29 2011/11/16 13:53:40 lhf Exp $ -.TH LUAC 1 "$Date: 2011/11/16 13:53:40 $" -.SH NAME -luac \- Lua compiler -.SH SYNOPSIS -.B luac -[ -.I options -] [ -.I filenames -] -.SH DESCRIPTION -.B luac -is the Lua compiler. -It translates programs written in the Lua programming language -into binary files containing precompiled chunks -that can be later loaded and executed. -.LP -The main advantages of precompiling chunks are: -faster loading, -protecting source code from accidental user changes, -and -off-line syntax checking. -Precompiling does not imply faster execution -because in Lua chunks are always compiled into bytecodes before being executed. -.B luac -simply allows those bytecodes to be saved in a file for later execution. -Precompiled chunks are not necessarily smaller than the corresponding source. -The main goal in precompiling is faster loading. -.LP -In the command line, -you can mix -text files containing Lua source and -binary files containing precompiled chunks. -.B luac -produces a single output file containing the combined bytecodes -for all files given. -Executing the combined file is equivalent to executing the given files. -By default, -the output file is named -.BR luac.out , -but you can change this with the -.B \-o -option. -.LP -Precompiled chunks are -.I not -portable across different architectures. -Moreover, -the internal format of precompiled chunks -is likely to change when a new version of Lua is released. -Make sure you save the source files of all Lua programs that you precompile. -.LP -.SH OPTIONS -.TP -.B \-l -produce a listing of the compiled bytecode for Lua's virtual machine. -Listing bytecodes is useful to learn about Lua's virtual machine. -If no files are given, then -.B luac -loads -.B luac.out -and lists its contents. -Use -.B \-l \-l -for a full listing. -.TP -.BI \-o " file" -output to -.IR file , -instead of the default -.BR luac.out . -(You can use -.B "'\-'" -for standard output, -but not on platforms that open standard output in text mode.) -The output file may be one of the given files because -all files are loaded before the output file is written. -Be careful not to overwrite precious files. -.TP -.B \-p -load files but do not generate any output file. -Used mainly for syntax checking and for testing precompiled chunks: -corrupted files will probably generate errors when loaded. -If no files are given, then -.B luac -loads -.B luac.out -and tests its contents. -No messages are displayed if the file loads without errors. -.TP -.B \-s -strip debug information before writing the output file. -This saves some space in very large chunks, -but if errors occur when running a stripped chunk, -then the error messages may not contain the full information they usually do. -In particular, -line numbers and names of local variables are lost. -.TP -.B \-v -show version information. -.TP -.B \-\- -stop handling options. -.TP -.B \- -stop handling options and process standard input. -.SH "SEE ALSO" -.BR lua (1) -.br -The documentation at lua.org. -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.css b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.css deleted file mode 100644 index aa0e677..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.css +++ /dev/null @@ -1,21 +0,0 @@ -h3 code { - font-family: inherit ; - font-size: inherit ; -} - -pre, code { - font-size: 12pt ; -} - -span.apii { - color: gray ; - float: right ; - font-family: inherit ; - font-style: normal ; - font-size: small ; -} - -h2:before { - content: "" ; - padding-right: 0em ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.html b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.html deleted file mode 100644 index 57c7787..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/manual.html +++ /dev/null @@ -1,10982 +0,0 @@ - - - -Lua 5.3 Reference Manual - - - - - - - -

-Lua -Lua 5.3 Reference Manual -

- -

-by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes - -

- -Copyright © 2015–2020 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

- - -

- - - - - - -

1 – Introduction

- -

-Lua is a powerful, efficient, lightweight, embeddable scripting language. -It supports procedural programming, -object-oriented programming, functional programming, -data-driven programming, and data description. - - -

-Lua combines simple procedural syntax with powerful data description -constructs based on associative arrays and extensible semantics. -Lua is dynamically typed, -runs by interpreting bytecode with a register-based -virtual machine, -and has automatic memory management with -incremental garbage collection, -making it ideal for configuration, scripting, -and rapid prototyping. - - -

-Lua is implemented as a library, written in clean C, -the common subset of Standard C and C++. -The Lua distribution includes a host program called lua, -which uses the Lua library to offer a complete, -standalone Lua interpreter, -for interactive or batch use. -Lua is intended to be used both as a powerful, lightweight, -embeddable scripting language for any program that needs one, -and as a powerful but lightweight and efficient stand-alone language. - - -

-As an extension language, Lua has no notion of a "main" program: -it works embedded in a host client, -called the embedding program or simply the host. -(Frequently, this host is the stand-alone lua program.) -The host program can invoke functions to execute a piece of Lua code, -can write and read Lua variables, -and can register C functions to be called by Lua code. -Through the use of C functions, Lua can be augmented to cope with -a wide range of different domains, -thus creating customized programming languages sharing a syntactical framework. - - -

-Lua is free software, -and is provided as usual with no guarantees, -as stated in its license. -The implementation described in this manual is available -at Lua's official web site, www.lua.org. - - -

-Like any other reference manual, -this document is dry in places. -For a discussion of the decisions behind the design of Lua, -see the technical papers available at Lua's web site. -For a detailed introduction to programming in Lua, -see Roberto's book, Programming in Lua. - - - -

2 – Basic Concepts

- -

-This section describes the basic concepts of the language. - - - -

2.1 – Values and Types

- -

-Lua is a dynamically typed language. -This means that -variables do not have types; only values do. -There are no type definitions in the language. -All values carry their own type. - - -

-All values in Lua are first-class values. -This means that all values can be stored in variables, -passed as arguments to other functions, and returned as results. - - -

-There are eight basic types in Lua: -nil, boolean, number, -string, function, userdata, -thread, and table. -The type nil has one single value, nil, -whose main property is to be different from any other value; -it usually represents the absence of a useful value. -The type boolean has two values, false and true. -Both nil and false make a condition false; -any other value makes it true. -The type number represents both -integer numbers and real (floating-point) numbers. -The type string represents immutable sequences of bytes. - -Lua is 8-bit clean: -strings can contain any 8-bit value, -including embedded zeros ('\0'). -Lua is also encoding-agnostic; -it makes no assumptions about the contents of a string. - - -

-The type number uses two internal representations, -or two subtypes, -one called integer and the other called float. -Lua has explicit rules about when each representation is used, -but it also converts between them automatically as needed (see §3.4.3). -Therefore, -the programmer may choose to mostly ignore the difference -between integers and floats -or to assume complete control over the representation of each number. -Standard Lua uses 64-bit integers and double-precision (64-bit) floats, -but you can also compile Lua so that it -uses 32-bit integers and/or single-precision (32-bit) floats. -The option with 32 bits for both integers and floats -is particularly attractive -for small machines and embedded systems. -(See macro LUA_32BITS in file luaconf.h.) - - -

-Lua can call (and manipulate) functions written in Lua and -functions written in C (see §3.4.10). -Both are represented by the type function. - - -

-The type userdata is provided to allow arbitrary C data to -be stored in Lua variables. -A userdata value represents a block of raw memory. -There are two kinds of userdata: -full userdata, -which is an object with a block of memory managed by Lua, -and light userdata, -which is simply a C pointer value. -Userdata has no predefined operations in Lua, -except assignment and identity test. -By using metatables, -the programmer can define operations for full userdata values -(see §2.4). -Userdata values cannot be created or modified in Lua, -only through the C API. -This guarantees the integrity of data owned by the host program. - - -

-The type thread represents independent threads of execution -and it is used to implement coroutines (see §2.6). -Lua threads are not related to operating-system threads. -Lua supports coroutines on all systems, -even those that do not support threads natively. - - -

-The type table implements associative arrays, -that is, arrays that can have as indices not only numbers, -but any Lua value except nil and NaN. -(Not a Number is a special value used to represent -undefined or unrepresentable numerical results, such as 0/0.) -Tables can be heterogeneous; -that is, they can contain values of all types (except nil). -Any key with value nil is not considered part of the table. -Conversely, any key that is not part of a table has -an associated value nil. - - -

-Tables are the sole data-structuring mechanism in Lua; -they can be used to represent ordinary arrays, lists, -symbol tables, sets, records, graphs, trees, etc. -To represent records, Lua uses the field name as an index. -The language supports this representation by -providing a.name as syntactic sugar for a["name"]. -There are several convenient ways to create tables in Lua -(see §3.4.9). - - -

-Like indices, -the values of table fields can be of any type. -In particular, -because functions are first-class values, -table fields can contain functions. -Thus tables can also carry methods (see §3.4.11). - - -

-The indexing of tables follows -the definition of raw equality in the language. -The expressions a[i] and a[j] -denote the same table element -if and only if i and j are raw equal -(that is, equal without metamethods). -In particular, floats with integral values -are equal to their respective integers -(e.g., 1.0 == 1). -To avoid ambiguities, -any float with integral value used as a key -is converted to its respective integer. -For instance, if you write a[2.0] = true, -the actual key inserted into the table will be the -integer 2. -(On the other hand, -2 and "2" are different Lua values and therefore -denote different table entries.) - - -

-Tables, functions, threads, and (full) userdata values are objects: -variables do not actually contain these values, -only references to them. -Assignment, parameter passing, and function returns -always manipulate references to such values; -these operations do not imply any kind of copy. - - -

-The library function type returns a string describing the type -of a given value (see §6.1). - - - - - -

2.2 – Environments and the Global Environment

- -

-As will be discussed in §3.2 and §3.3.3, -any reference to a free name -(that is, a name not bound to any declaration) var -is syntactically translated to _ENV.var. -Moreover, every chunk is compiled in the scope of -an external local variable named _ENV (see §3.3.2), -so _ENV itself is never a free name in a chunk. - - -

-Despite the existence of this external _ENV variable and -the translation of free names, -_ENV is a completely regular name. -In particular, -you can define new variables and parameters with that name. -Each reference to a free name uses the _ENV that is -visible at that point in the program, -following the usual visibility rules of Lua (see §3.5). - - -

-Any table used as the value of _ENV is called an environment. - - -

-Lua keeps a distinguished environment called the global environment. -This value is kept at a special index in the C registry (see §4.5). -In Lua, the global variable _G is initialized with this same value. -(_G is never used internally.) - - -

-When Lua loads a chunk, -the default value for its _ENV upvalue -is the global environment (see load). -Therefore, by default, -free names in Lua code refer to entries in the global environment -(and, therefore, they are also called global variables). -Moreover, all standard libraries are loaded in the global environment -and some functions there operate on that environment. -You can use load (or loadfile) -to load a chunk with a different environment. -(In C, you have to load the chunk and then change the value -of its first upvalue.) - - - - - -

2.3 – Error Handling

- -

-Because Lua is an embedded extension language, -all Lua actions start from C code in the host program -calling a function from the Lua library. -(When you use Lua standalone, -the lua application is the host program.) -Whenever an error occurs during -the compilation or execution of a Lua chunk, -control returns to the host, -which can take appropriate measures -(such as printing an error message). - - -

-Lua code can explicitly generate an error by calling the -error function. -If you need to catch errors in Lua, -you can use pcall or xpcall -to call a given function in protected mode. - - -

-Whenever there is an error, -an error object (also called an error message) -is propagated with information about the error. -Lua itself only generates errors whose error object is a string, -but programs may generate errors with -any value as the error object. -It is up to the Lua program or its host to handle such error objects. - - -

-When you use xpcall or lua_pcall, -you may give a message handler -to be called in case of errors. -This function is called with the original error object -and returns a new error object. -It is called before the error unwinds the stack, -so that it can gather more information about the error, -for instance by inspecting the stack and creating a stack traceback. -This message handler is still protected by the protected call; -so, an error inside the message handler -will call the message handler again. -If this loop goes on for too long, -Lua breaks it and returns an appropriate message. -(The message handler is called only for regular runtime errors. -It is not called for memory-allocation errors -nor for errors while running finalizers.) - - - - - -

2.4 – Metatables and Metamethods

- -

-Every value in Lua can have a metatable. -This metatable is an ordinary Lua table -that defines the behavior of the original value -under certain special operations. -You can change several aspects of the behavior -of operations over a value by setting specific fields in its metatable. -For instance, when a non-numeric value is the operand of an addition, -Lua checks for a function in the field "__add" of the value's metatable. -If it finds one, -Lua calls this function to perform the addition. - - -

-The key for each event in a metatable is a string -with the event name prefixed by two underscores; -the corresponding values are called metamethods. -In the previous example, the key is "__add" -and the metamethod is the function that performs the addition. -Unless stated otherwise, -metamethods should be function values. - - -

-You can query the metatable of any value -using the getmetatable function. -Lua queries metamethods in metatables using a raw access (see rawget). -So, to retrieve the metamethod for event ev in object o, -Lua does the equivalent to the following code: - -

-     rawget(getmetatable(o) or {}, "__ev")
-
- -

-You can replace the metatable of tables -using the setmetatable function. -You cannot change the metatable of other types from Lua code -(except by using the debug library (§6.10)); -you should use the C API for that. - - -

-Tables and full userdata have individual metatables -(although multiple tables and userdata can share their metatables). -Values of all other types share one single metatable per type; -that is, there is one single metatable for all numbers, -one for all strings, etc. -By default, a value has no metatable, -but the string library sets a metatable for the string type (see §6.4). - - -

-A metatable controls how an object behaves in -arithmetic operations, bitwise operations, -order comparisons, concatenation, length operation, calls, and indexing. -A metatable also can define a function to be called -when a userdata or a table is garbage collected (§2.5). - - -

-For the unary operators (negation, length, and bitwise NOT), -the metamethod is computed and called with a dummy second operand, -equal to the first one. -This extra operand is only to simplify Lua's internals -(by making these operators behave like a binary operation) -and may be removed in future versions. -(For most uses this extra operand is irrelevant.) - - -

-A detailed list of events controlled by metatables is given next. -Each operation is identified by its corresponding key. - - - -

    - -
  • __add: -the addition (+) operation. -If any operand for an addition is not a number -(nor a string coercible to a number), -Lua will try to call a metamethod. -First, Lua will check the first operand (even if it is valid). -If that operand does not define a metamethod for __add, -then Lua will check the second operand. -If Lua can find a metamethod, -it calls the metamethod with the two operands as arguments, -and the result of the call -(adjusted to one value) -is the result of the operation. -Otherwise, -it raises an error. -
  • - -
  • __sub: -the subtraction (-) operation. -Behavior similar to the addition operation. -
  • - -
  • __mul: -the multiplication (*) operation. -Behavior similar to the addition operation. -
  • - -
  • __div: -the division (/) operation. -Behavior similar to the addition operation. -
  • - -
  • __mod: -the modulo (%) operation. -Behavior similar to the addition operation. -
  • - -
  • __pow: -the exponentiation (^) operation. -Behavior similar to the addition operation. -
  • - -
  • __unm: -the negation (unary -) operation. -Behavior similar to the addition operation. -
  • - -
  • __idiv: -the floor division (//) operation. -Behavior similar to the addition operation. -
  • - -
  • __band: -the bitwise AND (&) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod -if any operand is neither an integer -nor a value coercible to an integer (see §3.4.3). -
  • - -
  • __bor: -the bitwise OR (|) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __bxor: -the bitwise exclusive OR (binary ~) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __bnot: -the bitwise NOT (unary ~) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __shl: -the bitwise left shift (<<) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __shr: -the bitwise right shift (>>) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __concat: -the concatenation (..) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod -if any operand is neither a string nor a number -(which is always coercible to a string). -
  • - -
  • __len: -the length (#) operation. -If the object is not a string, -Lua will try its metamethod. -If there is a metamethod, -Lua calls it with the object as argument, -and the result of the call -(always adjusted to one value) -is the result of the operation. -If there is no metamethod but the object is a table, -then Lua uses the table length operation (see §3.4.7). -Otherwise, Lua raises an error. -
  • - -
  • __eq: -the equal (==) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod only when the values -being compared are either both tables or both full userdata -and they are not primitively equal. -The result of the call is always converted to a boolean. -
  • - -
  • __lt: -the less than (<) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod only when the values -being compared are neither both numbers nor both strings. -The result of the call is always converted to a boolean. -
  • - -
  • __le: -the less equal (<=) operation. -Unlike other operations, -the less-equal operation can use two different events. -First, Lua looks for the __le metamethod in both operands, -like in the less than operation. -If it cannot find such a metamethod, -then it will try the __lt metamethod, -assuming that a <= b is equivalent to not (b < a). -As with the other comparison operators, -the result is always a boolean. -(This use of the __lt event can be removed in future versions; -it is also slower than a real __le metamethod.) -
  • - -
  • __index: -The indexing access operation table[key]. -This event happens when table is not a table or -when key is not present in table. -The metamethod is looked up in table. - - -

    -Despite the name, -the metamethod for this event can be either a function or a table. -If it is a function, -it is called with table and key as arguments, -and the result of the call -(adjusted to one value) -is the result of the operation. -If it is a table, -the final result is the result of indexing this table with key. -(This indexing is regular, not raw, -and therefore can trigger another metamethod.) -

  • - -
  • __newindex: -The indexing assignment table[key] = value. -Like the index event, -this event happens when table is not a table or -when key is not present in table. -The metamethod is looked up in table. - - -

    -Like with indexing, -the metamethod for this event can be either a function or a table. -If it is a function, -it is called with table, key, and value as arguments. -If it is a table, -Lua does an indexing assignment to this table with the same key and value. -(This assignment is regular, not raw, -and therefore can trigger another metamethod.) - - -

    -Whenever there is a __newindex metamethod, -Lua does not perform the primitive assignment. -(If necessary, -the metamethod itself can call rawset -to do the assignment.) -

  • - -
  • __call: -The call operation func(args). -This event happens when Lua tries to call a non-function value -(that is, func is not a function). -The metamethod is looked up in func. -If present, -the metamethod is called with func as its first argument, -followed by the arguments of the original call (args). -All results of the call -are the result of the operation. -(This is the only metamethod that allows multiple results.) -
  • - -
- -

-It is a good practice to add all needed metamethods to a table -before setting it as a metatable of some object. -In particular, the __gc metamethod works only when this order -is followed (see §2.5.1). - - -

-Because metatables are regular tables, -they can contain arbitrary fields, -not only the event names defined above. -Some functions in the standard library -(e.g., tostring) -use other fields in metatables for their own purposes. - - - - - -

2.5 – Garbage Collection

- -

-Lua performs automatic memory management. -This means that -you do not have to worry about allocating memory for new objects -or freeing it when the objects are no longer needed. -Lua manages memory automatically by running -a garbage collector to collect all dead objects -(that is, objects that are no longer accessible from Lua). -All memory used by Lua is subject to automatic management: -strings, tables, userdata, functions, threads, internal structures, etc. - - -

-Lua implements an incremental mark-and-sweep collector. -It uses two numbers to control its garbage-collection cycles: -the garbage-collector pause and -the garbage-collector step multiplier. -Both use percentage points as units -(e.g., a value of 100 means an internal value of 1). - - -

-The garbage-collector pause -controls how long the collector waits before starting a new cycle. -Larger values make the collector less aggressive. -Values smaller than 100 mean the collector will not wait to -start a new cycle. -A value of 200 means that the collector waits for the total memory in use -to double before starting a new cycle. - - -

-The garbage-collector step multiplier -controls the relative speed of the collector relative to -memory allocation. -Larger values make the collector more aggressive but also increase -the size of each incremental step. -You should not use values smaller than 100, -because they make the collector too slow and -can result in the collector never finishing a cycle. -The default is 200, -which means that the collector runs at "twice" -the speed of memory allocation. - - -

-If you set the step multiplier to a very large number -(larger than 10% of the maximum number of -bytes that the program may use), -the collector behaves like a stop-the-world collector. -If you then set the pause to 200, -the collector behaves as in old Lua versions, -doing a complete collection every time Lua doubles its -memory usage. - - -

-You can change these numbers by calling lua_gc in C -or collectgarbage in Lua. -You can also use these functions to control -the collector directly (e.g., stop and restart it). - - - -

2.5.1 – Garbage-Collection Metamethods

- -

-You can set garbage-collector metamethods for tables -and, using the C API, -for full userdata (see §2.4). -These metamethods are also called finalizers. -Finalizers allow you to coordinate Lua's garbage collection -with external resource management -(such as closing files, network or database connections, -or freeing your own memory). - - -

-For an object (table or userdata) to be finalized when collected, -you must mark it for finalization. - -You mark an object for finalization when you set its metatable -and the metatable has a field indexed by the string "__gc". -Note that if you set a metatable without a __gc field -and later create that field in the metatable, -the object will not be marked for finalization. - - -

-When a marked object becomes garbage, -it is not collected immediately by the garbage collector. -Instead, Lua puts it in a list. -After the collection, -Lua goes through that list. -For each object in the list, -it checks the object's __gc metamethod: -If it is a function, -Lua calls it with the object as its single argument; -if the metamethod is not a function, -Lua simply ignores it. - - -

-At the end of each garbage-collection cycle, -the finalizers for objects are called in -the reverse order that the objects were marked for finalization, -among those collected in that cycle; -that is, the first finalizer to be called is the one associated -with the object marked last in the program. -The execution of each finalizer may occur at any point during -the execution of the regular code. - - -

-Because the object being collected must still be used by the finalizer, -that object (and other objects accessible only through it) -must be resurrected by Lua. -Usually, this resurrection is transient, -and the object memory is freed in the next garbage-collection cycle. -However, if the finalizer stores the object in some global place -(e.g., a global variable), -then the resurrection is permanent. -Moreover, if the finalizer marks a finalizing object for finalization again, -its finalizer will be called again in the next cycle where the -object is unreachable. -In any case, -the object memory is freed only in a GC cycle where -the object is unreachable and not marked for finalization. - - -

-When you close a state (see lua_close), -Lua calls the finalizers of all objects marked for finalization, -following the reverse order that they were marked. -If any finalizer marks objects for collection during that phase, -these marks have no effect. - - - - - -

2.5.2 – Weak Tables

- -

-A weak table is a table whose elements are -weak references. -A weak reference is ignored by the garbage collector. -In other words, -if the only references to an object are weak references, -then the garbage collector will collect that object. - - -

-A weak table can have weak keys, weak values, or both. -A table with weak values allows the collection of its values, -but prevents the collection of its keys. -A table with both weak keys and weak values allows the collection of -both keys and values. -In any case, if either the key or the value is collected, -the whole pair is removed from the table. -The weakness of a table is controlled by the -__mode field of its metatable. -If the __mode field is a string containing the character 'k', -the keys in the table are weak. -If __mode contains 'v', -the values in the table are weak. - - -

-A table with weak keys and strong values -is also called an ephemeron table. -In an ephemeron table, -a value is considered reachable only if its key is reachable. -In particular, -if the only reference to a key comes through its value, -the pair is removed. - - -

-Any change in the weakness of a table may take effect only -at the next collect cycle. -In particular, if you change the weakness to a stronger mode, -Lua may still collect some items from that table -before the change takes effect. - - -

-Only objects that have an explicit construction -are removed from weak tables. -Values, such as numbers and light C functions, -are not subject to garbage collection, -and therefore are not removed from weak tables -(unless their associated values are collected). -Although strings are subject to garbage collection, -they do not have an explicit construction, -and therefore are not removed from weak tables. - - -

-Resurrected objects -(that is, objects being finalized -and objects accessible only through objects being finalized) -have a special behavior in weak tables. -They are removed from weak values before running their finalizers, -but are removed from weak keys only in the next collection -after running their finalizers, when such objects are actually freed. -This behavior allows the finalizer to access properties -associated with the object through weak tables. - - -

-If a weak table is among the resurrected objects in a collection cycle, -it may not be properly cleared until the next cycle. - - - - - - - -

2.6 – Coroutines

- -

-Lua supports coroutines, -also called collaborative multithreading. -A coroutine in Lua represents an independent thread of execution. -Unlike threads in multithread systems, however, -a coroutine only suspends its execution by explicitly calling -a yield function. - - -

-You create a coroutine by calling coroutine.create. -Its sole argument is a function -that is the main function of the coroutine. -The create function only creates a new coroutine and -returns a handle to it (an object of type thread); -it does not start the coroutine. - - -

-You execute a coroutine by calling coroutine.resume. -When you first call coroutine.resume, -passing as its first argument -a thread returned by coroutine.create, -the coroutine starts its execution by -calling its main function. -Extra arguments passed to coroutine.resume are passed -as arguments to that function. -After the coroutine starts running, -it runs until it terminates or yields. - - -

-A coroutine can terminate its execution in two ways: -normally, when its main function returns -(explicitly or implicitly, after the last instruction); -and abnormally, if there is an unprotected error. -In case of normal termination, -coroutine.resume returns true, -plus any values returned by the coroutine main function. -In case of errors, coroutine.resume returns false -plus an error object. - - -

-A coroutine yields by calling coroutine.yield. -When a coroutine yields, -the corresponding coroutine.resume returns immediately, -even if the yield happens inside nested function calls -(that is, not in the main function, -but in a function directly or indirectly called by the main function). -In the case of a yield, coroutine.resume also returns true, -plus any values passed to coroutine.yield. -The next time you resume the same coroutine, -it continues its execution from the point where it yielded, -with the call to coroutine.yield returning any extra -arguments passed to coroutine.resume. - - -

-Like coroutine.create, -the coroutine.wrap function also creates a coroutine, -but instead of returning the coroutine itself, -it returns a function that, when called, resumes the coroutine. -Any arguments passed to this function -go as extra arguments to coroutine.resume. -coroutine.wrap returns all the values returned by coroutine.resume, -except the first one (the boolean error code). -Unlike coroutine.resume, -coroutine.wrap does not catch errors; -any error is propagated to the caller. - - -

-As an example of how coroutines work, -consider the following code: - -

-     function foo (a)
-       print("foo", a)
-       return coroutine.yield(2*a)
-     end
-     
-     co = coroutine.create(function (a,b)
-           print("co-body", a, b)
-           local r = foo(a+1)
-           print("co-body", r)
-           local r, s = coroutine.yield(a+b, a-b)
-           print("co-body", r, s)
-           return b, "end"
-     end)
-     
-     print("main", coroutine.resume(co, 1, 10))
-     print("main", coroutine.resume(co, "r"))
-     print("main", coroutine.resume(co, "x", "y"))
-     print("main", coroutine.resume(co, "x", "y"))
-

-When you run it, it produces the following output: - -

-     co-body 1       10
-     foo     2
-     main    true    4
-     co-body r
-     main    true    11      -9
-     co-body x       y
-     main    true    10      end
-     main    false   cannot resume dead coroutine
-
- -

-You can also create and manipulate coroutines through the C API: -see functions lua_newthread, lua_resume, -and lua_yield. - - - - - -

3 – The Language

- -

-This section describes the lexis, the syntax, and the semantics of Lua. -In other words, -this section describes -which tokens are valid, -how they can be combined, -and what their combinations mean. - - -

-Language constructs will be explained using the usual extended BNF notation, -in which -{a} means 0 or more a's, and -[a] means an optional a. -Non-terminals are shown like non-terminal, -keywords are shown like kword, -and other terminal symbols are shown like ‘=’. -The complete syntax of Lua can be found in §9 -at the end of this manual. - - - -

3.1 – Lexical Conventions

- -

-Lua is a free-form language. -It ignores spaces (including new lines) and comments -between lexical elements (tokens), -except as delimiters between names and keywords. - - -

-Names -(also called identifiers) -in Lua can be any string of letters, -digits, and underscores, -not beginning with a digit and -not being a reserved word. -Identifiers are used to name variables, table fields, and labels. - - -

-The following keywords are reserved -and cannot be used as names: - - -

-     and       break     do        else      elseif    end
-     false     for       function  goto      if        in
-     local     nil       not       or        repeat    return
-     then      true      until     while
-
- -

-Lua is a case-sensitive language: -and is a reserved word, but And and AND -are two different, valid names. -As a convention, -programs should avoid creating -names that start with an underscore followed by -one or more uppercase letters (such as _VERSION). - - -

-The following strings denote other tokens: - -

-     +     -     *     /     %     ^     #
-     &     ~     |     <<    >>    //
-     ==    ~=    <=    >=    <     >     =
-     (     )     {     }     [     ]     ::
-     ;     :     ,     .     ..    ...
-
- -

-A short literal string -can be delimited by matching single or double quotes, -and can contain the following C-like escape sequences: -'\a' (bell), -'\b' (backspace), -'\f' (form feed), -'\n' (newline), -'\r' (carriage return), -'\t' (horizontal tab), -'\v' (vertical tab), -'\\' (backslash), -'\"' (quotation mark [double quote]), -and '\'' (apostrophe [single quote]). -A backslash followed by a line break -results in a newline in the string. -The escape sequence '\z' skips the following span -of white-space characters, -including line breaks; -it is particularly useful to break and indent a long literal string -into multiple lines without adding the newlines and spaces -into the string contents. -A short literal string cannot contain unescaped line breaks -nor escapes not forming a valid escape sequence. - - -

-We can specify any byte in a short literal string by its numeric value -(including embedded zeros). -This can be done -with the escape sequence \xXX, -where XX is a sequence of exactly two hexadecimal digits, -or with the escape sequence \ddd, -where ddd is a sequence of up to three decimal digits. -(Note that if a decimal escape sequence is to be followed by a digit, -it must be expressed using exactly three digits.) - - -

-The UTF-8 encoding of a Unicode character -can be inserted in a literal string with -the escape sequence \u{XXX} -(note the mandatory enclosing brackets), -where XXX is a sequence of one or more hexadecimal digits -representing the character code point. - - -

-Literal strings can also be defined using a long format -enclosed by long brackets. -We define an opening long bracket of level n as an opening -square bracket followed by n equal signs followed by another -opening square bracket. -So, an opening long bracket of level 0 is written as [[, -an opening long bracket of level 1 is written as [=[, -and so on. -A closing long bracket is defined similarly; -for instance, -a closing long bracket of level 4 is written as ]====]. -A long literal starts with an opening long bracket of any level and -ends at the first closing long bracket of the same level. -It can contain any text except a closing bracket of the same level. -Literals in this bracketed form can run for several lines, -do not interpret any escape sequences, -and ignore long brackets of any other level. -Any kind of end-of-line sequence -(carriage return, newline, carriage return followed by newline, -or newline followed by carriage return) -is converted to a simple newline. - - -

-For convenience, -when the opening long bracket is immediately followed by a newline, -the newline is not included in the string. -As an example, in a system using ASCII -(in which 'a' is coded as 97, -newline is coded as 10, and '1' is coded as 49), -the five literal strings below denote the same string: - -

-     a = 'alo\n123"'
-     a = "alo\n123\""
-     a = '\97lo\10\04923"'
-     a = [[alo
-     123"]]
-     a = [==[
-     alo
-     123"]==]
-
- -

-Any byte in a literal string not -explicitly affected by the previous rules represents itself. -However, Lua opens files for parsing in text mode, -and the system file functions may have problems with -some control characters. -So, it is safer to represent -non-text data as a quoted literal with -explicit escape sequences for the non-text characters. - - -

-A numeric constant (or numeral) -can be written with an optional fractional part -and an optional decimal exponent, -marked by a letter 'e' or 'E'. -Lua also accepts hexadecimal constants, -which start with 0x or 0X. -Hexadecimal constants also accept an optional fractional part -plus an optional binary exponent, -marked by a letter 'p' or 'P'. -A numeric constant with a radix point or an exponent -denotes a float; -otherwise, -if its value fits in an integer, -it denotes an integer. -Examples of valid integer constants are - -

-     3   345   0xff   0xBEBADA
-

-Examples of valid float constants are - -

-     3.0     3.1416     314.16e-2     0.31416E1     34e1
-     0x0.1E  0xA23p-4   0X1.921FB54442D18P+1
-
- -

-A comment starts with a double hyphen (--) -anywhere outside a string. -If the text immediately after -- is not an opening long bracket, -the comment is a short comment, -which runs until the end of the line. -Otherwise, it is a long comment, -which runs until the corresponding closing long bracket. -Long comments are frequently used to disable code temporarily. - - - - - -

3.2 – Variables

- -

-Variables are places that store values. -There are three kinds of variables in Lua: -global variables, local variables, and table fields. - - -

-A single name can denote a global variable or a local variable -(or a function's formal parameter, -which is a particular kind of local variable): - -

-	var ::= Name
-

-Name denotes identifiers, as defined in §3.1. - - -

-Any variable name is assumed to be global unless explicitly declared -as a local (see §3.3.7). -Local variables are lexically scoped: -local variables can be freely accessed by functions -defined inside their scope (see §3.5). - - -

-Before the first assignment to a variable, its value is nil. - - -

-Square brackets are used to index a table: - -

-	var ::= prefixexp ‘[’ exp ‘]’
-

-The meaning of accesses to table fields can be changed via metatables -(see §2.4). - - -

-The syntax var.Name is just syntactic sugar for -var["Name"]: - -

-	var ::= prefixexp ‘.’ Name
-
- -

-An access to a global variable x -is equivalent to _ENV.x. -Due to the way that chunks are compiled, -_ENV is never a global name (see §2.2). - - - - - -

3.3 – Statements

- -

-Lua supports an almost conventional set of statements, -similar to those in Pascal or C. -This set includes -assignments, control structures, function calls, -and variable declarations. - - - -

3.3.1 – Blocks

- -

-A block is a list of statements, -which are executed sequentially: - -

-	block ::= {stat}
-

-Lua has empty statements -that allow you to separate statements with semicolons, -start a block with a semicolon -or write two semicolons in sequence: - -

-	stat ::= ‘;’
-
- -

-Function calls and assignments -can start with an open parenthesis. -This possibility leads to an ambiguity in Lua's grammar. -Consider the following fragment: - -

-     a = b + c
-     (print or io.write)('done')
-

-The grammar could see it in two ways: - -

-     a = b + c(print or io.write)('done')
-     
-     a = b + c; (print or io.write)('done')
-

-The current parser always sees such constructions -in the first way, -interpreting the open parenthesis -as the start of the arguments to a call. -To avoid this ambiguity, -it is a good practice to always precede with a semicolon -statements that start with a parenthesis: - -

-     ;(print or io.write)('done')
-
- -

-A block can be explicitly delimited to produce a single statement: - -

-	stat ::= do block end
-

-Explicit blocks are useful -to control the scope of variable declarations. -Explicit blocks are also sometimes used to -add a return statement in the middle -of another block (see §3.3.4). - - - - - -

3.3.2 – Chunks

- -

-The unit of compilation of Lua is called a chunk. -Syntactically, -a chunk is simply a block: - -

-	chunk ::= block
-
- -

-Lua handles a chunk as the body of an anonymous function -with a variable number of arguments -(see §3.4.11). -As such, chunks can define local variables, -receive arguments, and return values. -Moreover, such anonymous function is compiled as in the -scope of an external local variable called _ENV (see §2.2). -The resulting function always has _ENV as its only upvalue, -even if it does not use that variable. - - -

-A chunk can be stored in a file or in a string inside the host program. -To execute a chunk, -Lua first loads it, -precompiling the chunk's code into instructions for a virtual machine, -and then Lua executes the compiled code -with an interpreter for the virtual machine. - - -

-Chunks can also be precompiled into binary form; -see program luac and function string.dump for details. -Programs in source and compiled forms are interchangeable; -Lua automatically detects the file type and acts accordingly (see load). - - - - - -

3.3.3 – Assignment

- -

-Lua allows multiple assignments. -Therefore, the syntax for assignment -defines a list of variables on the left side -and a list of expressions on the right side. -The elements in both lists are separated by commas: - -

-	stat ::= varlist ‘=’ explist
-	varlist ::= var {‘,’ var}
-	explist ::= exp {‘,’ exp}
-

-Expressions are discussed in §3.4. - - -

-Before the assignment, -the list of values is adjusted to the length of -the list of variables. -If there are more values than needed, -the excess values are thrown away. -If there are fewer values than needed, -the list is extended with as many nil's as needed. -If the list of expressions ends with a function call, -then all values returned by that call enter the list of values, -before the adjustment -(except when the call is enclosed in parentheses; see §3.4). - - -

-The assignment statement first evaluates all its expressions -and only then the assignments are performed. -Thus the code - -

-     i = 3
-     i, a[i] = i+1, 20
-

-sets a[3] to 20, without affecting a[4] -because the i in a[i] is evaluated (to 3) -before it is assigned 4. -Similarly, the line - -

-     x, y = y, x
-

-exchanges the values of x and y, -and - -

-     x, y, z = y, z, x
-

-cyclically permutes the values of x, y, and z. - - -

-An assignment to a global name x = val -is equivalent to the assignment -_ENV.x = val (see §2.2). - - -

-The meaning of assignments to table fields and -global variables (which are actually table fields, too) -can be changed via metatables (see §2.4). - - - - - -

3.3.4 – Control Structures

-The control structures -if, while, and repeat have the usual meaning and -familiar syntax: - - - - -

-	stat ::= while exp do block end
-	stat ::= repeat block until exp
-	stat ::= if exp then block {elseif exp then block} [else block] end
-

-Lua also has a for statement, in two flavors (see §3.3.5). - - -

-The condition expression of a -control structure can return any value. -Both false and nil are considered false. -All values different from nil and false are considered true -(in particular, the number 0 and the empty string are also true). - - -

-In the repeatuntil loop, -the inner block does not end at the until keyword, -but only after the condition. -So, the condition can refer to local variables -declared inside the loop block. - - -

-The goto statement transfers the program control to a label. -For syntactical reasons, -labels in Lua are considered statements too: - - - -

-	stat ::= goto Name
-	stat ::= label
-	label ::= ‘::’ Name ‘::’
-
- -

-A label is visible in the entire block where it is defined, -except -inside nested blocks where a label with the same name is defined and -inside nested functions. -A goto may jump to any visible label as long as it does not -enter into the scope of a local variable. - - -

-Labels and empty statements are called void statements, -as they perform no actions. - - -

-The break statement terminates the execution of a -while, repeat, or for loop, -skipping to the next statement after the loop: - - -

-	stat ::= break
-

-A break ends the innermost enclosing loop. - - -

-The return statement is used to return values -from a function or a chunk -(which is an anonymous function). - -Functions can return more than one value, -so the syntax for the return statement is - -

-	stat ::= return [explist] [‘;’]
-
- -

-The return statement can only be written -as the last statement of a block. -If it is really necessary to return in the middle of a block, -then an explicit inner block can be used, -as in the idiom do return end, -because now return is the last statement in its (inner) block. - - - - - -

3.3.5 – For Statement

- -

- -The for statement has two forms: -one numerical and one generic. - - -

-The numerical for loop repeats a block of code while a -control variable runs through an arithmetic progression. -It has the following syntax: - -

-	stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end
-

-The block is repeated for name starting at the value of -the first exp, until it passes the second exp by steps of the -third exp. -More precisely, a for statement like - -

-     for v = e1, e2, e3 do block end
-

-is equivalent to the code: - -

-     do
-       local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
-       if not (var and limit and step) then error() end
-       var = var - step
-       while true do
-         var = var + step
-         if (step >= 0 and var > limit) or (step < 0 and var < limit) then
-           break
-         end
-         local v = var
-         block
-       end
-     end
-
- -

-Note the following: - -

    - -
  • -All three control expressions are evaluated only once, -before the loop starts. -They must all result in numbers. -
  • - -
  • -var, limit, and step are invisible variables. -The names shown here are for explanatory purposes only. -
  • - -
  • -If the third expression (the step) is absent, -then a step of 1 is used. -
  • - -
  • -You can use break and goto to exit a for loop. -
  • - -
  • -The loop variable v is local to the loop body. -If you need its value after the loop, -assign it to another variable before exiting the loop. -
  • - -
- -

-The generic for statement works over functions, -called iterators. -On each iteration, the iterator function is called to produce a new value, -stopping when this new value is nil. -The generic for loop has the following syntax: - -

-	stat ::= for namelist in explist do block end
-	namelist ::= Name {‘,’ Name}
-

-A for statement like - -

-     for var_1, ···, var_n in explist do block end
-

-is equivalent to the code: - -

-     do
-       local f, s, var = explist
-       while true do
-         local var_1, ···, var_n = f(s, var)
-         if var_1 == nil then break end
-         var = var_1
-         block
-       end
-     end
-

-Note the following: - -

    - -
  • -explist is evaluated only once. -Its results are an iterator function, -a state, -and an initial value for the first iterator variable. -
  • - -
  • -f, s, and var are invisible variables. -The names are here for explanatory purposes only. -
  • - -
  • -You can use break to exit a for loop. -
  • - -
  • -The loop variables var_i are local to the loop; -you cannot use their values after the for ends. -If you need these values, -then assign them to other variables before breaking or exiting the loop. -
  • - -
- - - - -

3.3.6 – Function Calls as Statements

-To allow possible side-effects, -function calls can be executed as statements: - -

-	stat ::= functioncall
-

-In this case, all returned values are thrown away. -Function calls are explained in §3.4.10. - - - - - -

3.3.7 – Local Declarations

-Local variables can be declared anywhere inside a block. -The declaration can include an initial assignment: - -

-	stat ::= local namelist [‘=’ explist]
-

-If present, an initial assignment has the same semantics -of a multiple assignment (see §3.3.3). -Otherwise, all variables are initialized with nil. - - -

-A chunk is also a block (see §3.3.2), -and so local variables can be declared in a chunk outside any explicit block. - - -

-The visibility rules for local variables are explained in §3.5. - - - - - - - -

3.4 – Expressions

- -

-The basic expressions in Lua are the following: - -

-	exp ::= prefixexp
-	exp ::= nil | false | true
-	exp ::= Numeral
-	exp ::= LiteralString
-	exp ::= functiondef
-	exp ::= tableconstructor
-	exp ::= ‘...’
-	exp ::= exp binop exp
-	exp ::= unop exp
-	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
-
- -

-Numerals and literal strings are explained in §3.1; -variables are explained in §3.2; -function definitions are explained in §3.4.11; -function calls are explained in §3.4.10; -table constructors are explained in §3.4.9. -Vararg expressions, -denoted by three dots ('...'), can only be used when -directly inside a vararg function; -they are explained in §3.4.11. - - -

-Binary operators comprise arithmetic operators (see §3.4.1), -bitwise operators (see §3.4.2), -relational operators (see §3.4.4), logical operators (see §3.4.5), -and the concatenation operator (see §3.4.6). -Unary operators comprise the unary minus (see §3.4.1), -the unary bitwise NOT (see §3.4.2), -the unary logical not (see §3.4.5), -and the unary length operator (see §3.4.7). - - -

-Both function calls and vararg expressions can result in multiple values. -If a function call is used as a statement (see §3.3.6), -then its return list is adjusted to zero elements, -thus discarding all returned values. -If an expression is used as the last (or the only) element -of a list of expressions, -then no adjustment is made -(unless the expression is enclosed in parentheses). -In all other contexts, -Lua adjusts the result list to one element, -either discarding all values except the first one -or adding a single nil if there are no values. - - -

-Here are some examples: - -

-     f()                -- adjusted to 0 results
-     g(f(), x)          -- f() is adjusted to 1 result
-     g(x, f())          -- g gets x plus all results from f()
-     a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)
-     a,b = ...          -- a gets the first vararg argument, b gets
-                        -- the second (both a and b can get nil if there
-                        -- is no corresponding vararg argument)
-     
-     a,b,c = x, f()     -- f() is adjusted to 2 results
-     a,b,c = f()        -- f() is adjusted to 3 results
-     return f()         -- returns all results from f()
-     return ...         -- returns all received vararg arguments
-     return x,y,f()     -- returns x, y, and all results from f()
-     {f()}              -- creates a list with all results from f()
-     {...}              -- creates a list with all vararg arguments
-     {f(), nil}         -- f() is adjusted to 1 result
-
- -

-Any expression enclosed in parentheses always results in only one value. -Thus, -(f(x,y,z)) is always a single value, -even if f returns several values. -(The value of (f(x,y,z)) is the first value returned by f -or nil if f does not return any values.) - - - -

3.4.1 – Arithmetic Operators

-Lua supports the following arithmetic operators: - -

    -
  • +: addition
  • -
  • -: subtraction
  • -
  • *: multiplication
  • -
  • /: float division
  • -
  • //: floor division
  • -
  • %: modulo
  • -
  • ^: exponentiation
  • -
  • -: unary minus
  • -
- -

-With the exception of exponentiation and float division, -the arithmetic operators work as follows: -If both operands are integers, -the operation is performed over integers and the result is an integer. -Otherwise, if both operands are numbers -or strings that can be converted to -numbers (see §3.4.3), -then they are converted to floats, -the operation is performed following the usual rules -for floating-point arithmetic -(usually the IEEE 754 standard), -and the result is a float. - - -

-Exponentiation and float division (/) -always convert their operands to floats -and the result is always a float. -Exponentiation uses the ISO C function pow, -so that it works for non-integer exponents too. - - -

-Floor division (//) is a division -that rounds the quotient towards minus infinity, -that is, the floor of the division of its operands. - - -

-Modulo is defined as the remainder of a division -that rounds the quotient towards minus infinity (floor division). - - -

-In case of overflows in integer arithmetic, -all operations wrap around, -according to the usual rules of two-complement arithmetic. -(In other words, -they return the unique representable integer -that is equal modulo 264 to the mathematical result.) - - - -

3.4.2 – Bitwise Operators

-Lua supports the following bitwise operators: - -

    -
  • &: bitwise AND
  • -
  • |: bitwise OR
  • -
  • ~: bitwise exclusive OR
  • -
  • >>: right shift
  • -
  • <<: left shift
  • -
  • ~: unary bitwise NOT
  • -
- -

-All bitwise operations convert its operands to integers -(see §3.4.3), -operate on all bits of those integers, -and result in an integer. - - -

-Both right and left shifts fill the vacant bits with zeros. -Negative displacements shift to the other direction; -displacements with absolute values equal to or higher than -the number of bits in an integer -result in zero (as all bits are shifted out). - - - - - -

3.4.3 – Coercions and Conversions

-Lua provides some automatic conversions between some -types and representations at run time. -Bitwise operators always convert float operands to integers. -Exponentiation and float division -always convert integer operands to floats. -All other arithmetic operations applied to mixed numbers -(integers and floats) convert the integer operand to a float; -this is called the usual rule. -The C API also converts both integers to floats and -floats to integers, as needed. -Moreover, string concatenation accepts numbers as arguments, -besides strings. - - -

-Lua also converts strings to numbers, -whenever a number is expected. - - -

-In a conversion from integer to float, -if the integer value has an exact representation as a float, -that is the result. -Otherwise, -the conversion gets the nearest higher or -the nearest lower representable value. -This kind of conversion never fails. - - -

-The conversion from float to integer -checks whether the float has an exact representation as an integer -(that is, the float has an integral value and -it is in the range of integer representation). -If it does, that representation is the result. -Otherwise, the conversion fails. - - -

-The conversion from strings to numbers goes as follows: -First, the string is converted to an integer or a float, -following its syntax and the rules of the Lua lexer. -(The string may have also leading and trailing spaces and a sign.) -Then, the resulting number (float or integer) -is converted to the type (float or integer) required by the context -(e.g., the operation that forced the conversion). - - -

-All conversions from strings to numbers -accept both a dot and the current locale mark -as the radix character. -(The Lua lexer, however, accepts only a dot.) - - -

-The conversion from numbers to strings uses a -non-specified human-readable format. -For complete control over how numbers are converted to strings, -use the format function from the string library -(see string.format). - - - - - -

3.4.4 – Relational Operators

-Lua supports the following relational operators: - -

    -
  • ==: equality
  • -
  • ~=: inequality
  • -
  • <: less than
  • -
  • >: greater than
  • -
  • <=: less or equal
  • -
  • >=: greater or equal
  • -

-These operators always result in false or true. - - -

-Equality (==) first compares the type of its operands. -If the types are different, then the result is false. -Otherwise, the values of the operands are compared. -Strings are compared in the obvious way. -Numbers are equal if they denote the same mathematical value. - - -

-Tables, userdata, and threads -are compared by reference: -two objects are considered equal only if they are the same object. -Every time you create a new object -(a table, userdata, or thread), -this new object is different from any previously existing object. -A closure is always equal to itself. -Closures with any detectable difference -(different behavior, different definition) are always different. -Closures created at different times but with no detectable differences -may be classified as equal or not -(depending on internal caching details). - - -

-You can change the way that Lua compares tables and userdata -by using the "eq" metamethod (see §2.4). - - -

-Equality comparisons do not convert strings to numbers -or vice versa. -Thus, "0"==0 evaluates to false, -and t[0] and t["0"] denote different -entries in a table. - - -

-The operator ~= is exactly the negation of equality (==). - - -

-The order operators work as follows. -If both arguments are numbers, -then they are compared according to their mathematical values -(regardless of their subtypes). -Otherwise, if both arguments are strings, -then their values are compared according to the current locale. -Otherwise, Lua tries to call the "lt" or the "le" -metamethod (see §2.4). -A comparison a > b is translated to b < a -and a >= b is translated to b <= a. - - -

-Following the IEEE 754 standard, -NaN is considered neither smaller than, -nor equal to, nor greater than any value (including itself). - - - - - -

3.4.5 – Logical Operators

-The logical operators in Lua are -and, or, and not. -Like the control structures (see §3.3.4), -all logical operators consider both false and nil as false -and anything else as true. - - -

-The negation operator not always returns false or true. -The conjunction operator and returns its first argument -if this value is false or nil; -otherwise, and returns its second argument. -The disjunction operator or returns its first argument -if this value is different from nil and false; -otherwise, or returns its second argument. -Both and and or use short-circuit evaluation; -that is, -the second operand is evaluated only if necessary. -Here are some examples: - -

-     10 or 20            --> 10
-     10 or error()       --> 10
-     nil or "a"          --> "a"
-     nil and 10          --> nil
-     false and error()   --> false
-     false and nil       --> false
-     false or nil        --> nil
-     10 and 20           --> 20
-

-(In this manual, ---> indicates the result of the preceding expression.) - - - - - -

3.4.6 – Concatenation

-The string concatenation operator in Lua is -denoted by two dots ('..'). -If both operands are strings or numbers, then they are converted to -strings according to the rules described in §3.4.3. -Otherwise, the __concat metamethod is called (see §2.4). - - - - - -

3.4.7 – The Length Operator

- -

-The length operator is denoted by the unary prefix operator #. - - -

-The length of a string is its number of bytes -(that is, the usual meaning of string length when each -character is one byte). - - -

-The length operator applied on a table -returns a border in that table. -A border in a table t is any natural number -that satisfies the following condition: - -

-     (border == 0 or t[border] ~= nil) and t[border + 1] == nil
-

-In words, -a border is any (natural) index in a table -where a non-nil value is followed by a nil value -(or zero, when index 1 is nil). - - -

-A table with exactly one border is called a sequence. -For instance, the table {10, 20, 30, 40, 50} is a sequence, -as it has only one border (5). -The table {10, 20, 30, nil, 50} has two borders (3 and 5), -and therefore it is not a sequence. -The table {nil, 20, 30, nil, nil, 60, nil} -has three borders (0, 3, and 6), -so it is not a sequence, too. -The table {} is a sequence with border 0. -Note that non-natural keys do not interfere -with whether a table is a sequence. - - -

-When t is a sequence, -#t returns its only border, -which corresponds to the intuitive notion of the length of the sequence. -When t is not a sequence, -#t can return any of its borders. -(The exact one depends on details of -the internal representation of the table, -which in turn can depend on how the table was populated and -the memory addresses of its non-numeric keys.) - - -

-The computation of the length of a table -has a guaranteed worst time of O(log n), -where n is the largest natural key in the table. - - -

-A program can modify the behavior of the length operator for -any value but strings through the __len metamethod (see §2.4). - - - - - -

3.4.8 – Precedence

-Operator precedence in Lua follows the table below, -from lower to higher priority: - -

-     or
-     and
-     <     >     <=    >=    ~=    ==
-     |
-     ~
-     &
-     <<    >>
-     ..
-     +     -
-     *     /     //    %
-     unary operators (not   #     -     ~)
-     ^
-

-As usual, -you can use parentheses to change the precedences of an expression. -The concatenation ('..') and exponentiation ('^') -operators are right associative. -All other binary operators are left associative. - - - - - -

3.4.9 – Table Constructors

-Table constructors are expressions that create tables. -Every time a constructor is evaluated, a new table is created. -A constructor can be used to create an empty table -or to create a table and initialize some of its fields. -The general syntax for constructors is - -

-	tableconstructor ::= ‘{’ [fieldlist] ‘}’
-	fieldlist ::= field {fieldsep field} [fieldsep]
-	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
-	fieldsep ::= ‘,’ | ‘;’
-
- -

-Each field of the form [exp1] = exp2 adds to the new table an entry -with key exp1 and value exp2. -A field of the form name = exp is equivalent to -["name"] = exp. -Finally, fields of the form exp are equivalent to -[i] = exp, where i are consecutive integers -starting with 1. -Fields in the other formats do not affect this counting. -For example, - -

-     a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
-

-is equivalent to - -

-     do
-       local t = {}
-       t[f(1)] = g
-       t[1] = "x"         -- 1st exp
-       t[2] = "y"         -- 2nd exp
-       t.x = 1            -- t["x"] = 1
-       t[3] = f(x)        -- 3rd exp
-       t[30] = 23
-       t[4] = 45          -- 4th exp
-       a = t
-     end
-
- -

-The order of the assignments in a constructor is undefined. -(This order would be relevant only when there are repeated keys.) - - -

-If the last field in the list has the form exp -and the expression is a function call or a vararg expression, -then all values returned by this expression enter the list consecutively -(see §3.4.10). - - -

-The field list can have an optional trailing separator, -as a convenience for machine-generated code. - - - - - -

3.4.10 – Function Calls

-A function call in Lua has the following syntax: - -

-	functioncall ::= prefixexp args
-

-In a function call, -first prefixexp and args are evaluated. -If the value of prefixexp has type function, -then this function is called -with the given arguments. -Otherwise, the prefixexp "call" metamethod is called, -having as first argument the value of prefixexp, -followed by the original call arguments -(see §2.4). - - -

-The form - -

-	functioncall ::= prefixexp ‘:’ Name args
-

-can be used to call "methods". -A call v:name(args) -is syntactic sugar for v.name(v,args), -except that v is evaluated only once. - - -

-Arguments have the following syntax: - -

-	args ::= ‘(’ [explist] ‘)’
-	args ::= tableconstructor
-	args ::= LiteralString
-

-All argument expressions are evaluated before the call. -A call of the form f{fields} is -syntactic sugar for f({fields}); -that is, the argument list is a single new table. -A call of the form f'string' -(or f"string" or f[[string]]) -is syntactic sugar for f('string'); -that is, the argument list is a single literal string. - - -

-A call of the form return functioncall is called -a tail call. -Lua implements proper tail calls -(or proper tail recursion): -in a tail call, -the called function reuses the stack entry of the calling function. -Therefore, there is no limit on the number of nested tail calls that -a program can execute. -However, a tail call erases any debug information about the -calling function. -Note that a tail call only happens with a particular syntax, -where the return has one single function call as argument; -this syntax makes the calling function return exactly -the returns of the called function. -So, none of the following examples are tail calls: - -

-     return (f(x))        -- results adjusted to 1
-     return 2 * f(x)
-     return x, f(x)       -- additional results
-     f(x); return         -- results discarded
-     return x or f(x)     -- results adjusted to 1
-
- - - - -

3.4.11 – Function Definitions

- -

-The syntax for function definition is - -

-	functiondef ::= function funcbody
-	funcbody ::= ‘(’ [parlist] ‘)’ block end
-
- -

-The following syntactic sugar simplifies function definitions: - -

-	stat ::= function funcname funcbody
-	stat ::= local function Name funcbody
-	funcname ::= Name {‘.’ Name} [‘:’ Name]
-

-The statement - -

-     function f () body end
-

-translates to - -

-     f = function () body end
-

-The statement - -

-     function t.a.b.c.f () body end
-

-translates to - -

-     t.a.b.c.f = function () body end
-

-The statement - -

-     local function f () body end
-

-translates to - -

-     local f; f = function () body end
-

-not to - -

-     local f = function () body end
-

-(This only makes a difference when the body of the function -contains references to f.) - - -

-A function definition is an executable expression, -whose value has type function. -When Lua precompiles a chunk, -all its function bodies are precompiled too. -Then, whenever Lua executes the function definition, -the function is instantiated (or closed). -This function instance (or closure) -is the final value of the expression. - - -

-Parameters act as local variables that are -initialized with the argument values: - -

-	parlist ::= namelist [‘,’ ‘...’] | ‘...’
-

-When a function is called, -the list of arguments is adjusted to -the length of the list of parameters, -unless the function is a vararg function, -which is indicated by three dots ('...') -at the end of its parameter list. -A vararg function does not adjust its argument list; -instead, it collects all extra arguments and supplies them -to the function through a vararg expression, -which is also written as three dots. -The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results. -If a vararg expression is used inside another expression -or in the middle of a list of expressions, -then its return list is adjusted to one element. -If the expression is used as the last element of a list of expressions, -then no adjustment is made -(unless that last expression is enclosed in parentheses). - - -

-As an example, consider the following definitions: - -

-     function f(a, b) end
-     function g(a, b, ...) end
-     function r() return 1,2,3 end
-

-Then, we have the following mapping from arguments to parameters and -to the vararg expression: - -

-     CALL            PARAMETERS
-     
-     f(3)             a=3, b=nil
-     f(3, 4)          a=3, b=4
-     f(3, 4, 5)       a=3, b=4
-     f(r(), 10)       a=1, b=10
-     f(r())           a=1, b=2
-     
-     g(3)             a=3, b=nil, ... -->  (nothing)
-     g(3, 4)          a=3, b=4,   ... -->  (nothing)
-     g(3, 4, 5, 8)    a=3, b=4,   ... -->  5  8
-     g(5, r())        a=5, b=1,   ... -->  2  3
-
- -

-Results are returned using the return statement (see §3.3.4). -If control reaches the end of a function -without encountering a return statement, -then the function returns with no results. - - -

- -There is a system-dependent limit on the number of values -that a function may return. -This limit is guaranteed to be larger than 1000. - - -

-The colon syntax -is used for defining methods, -that is, functions that have an implicit extra parameter self. -Thus, the statement - -

-     function t.a.b.c:f (params) body end
-

-is syntactic sugar for - -

-     t.a.b.c.f = function (self, params) body end
-
- - - - - - -

3.5 – Visibility Rules

- -

- -Lua is a lexically scoped language. -The scope of a local variable begins at the first statement after -its declaration and lasts until the last non-void statement -of the innermost block that includes the declaration. -Consider the following example: - -

-     x = 10                -- global variable
-     do                    -- new block
-       local x = x         -- new 'x', with value 10
-       print(x)            --> 10
-       x = x+1
-       do                  -- another block
-         local x = x+1     -- another 'x'
-         print(x)          --> 12
-       end
-       print(x)            --> 11
-     end
-     print(x)              --> 10  (the global one)
-
- -

-Notice that, in a declaration like local x = x, -the new x being declared is not in scope yet, -and so the second x refers to the outside variable. - - -

-Because of the lexical scoping rules, -local variables can be freely accessed by functions -defined inside their scope. -A local variable used by an inner function is called -an upvalue, or external local variable, -inside the inner function. - - -

-Notice that each execution of a local statement -defines new local variables. -Consider the following example: - -

-     a = {}
-     local x = 20
-     for i=1,10 do
-       local y = 0
-       a[i] = function () y=y+1; return x+y end
-     end
-

-The loop creates ten closures -(that is, ten instances of the anonymous function). -Each of these closures uses a different y variable, -while all of them share the same x. - - - - - -

4 – The Application Program Interface

- -

- -This section describes the C API for Lua, that is, -the set of C functions available to the host program to communicate -with Lua. -All API functions and related types and constants -are declared in the header file lua.h. - - -

-Even when we use the term "function", -any facility in the API may be provided as a macro instead. -Except where stated otherwise, -all such macros use each of their arguments exactly once -(except for the first argument, which is always a Lua state), -and so do not generate any hidden side-effects. - - -

-As in most C libraries, -the Lua API functions do not check their arguments for validity or consistency. -However, you can change this behavior by compiling Lua -with the macro LUA_USE_APICHECK defined. - - -

-The Lua library is fully reentrant: -it has no global variables. -It keeps all information it needs in a dynamic structure, -called the Lua state. - - -

-Each Lua state has one or more threads, -which correspond to independent, cooperative lines of execution. -The type lua_State (despite its name) refers to a thread. -(Indirectly, through the thread, it also refers to the -Lua state associated to the thread.) - - -

-A pointer to a thread must be passed as the first argument to -every function in the library, except to lua_newstate, -which creates a Lua state from scratch and returns a pointer -to the main thread in the new state. - - - -

4.1 – The Stack

- -

-Lua uses a virtual stack to pass values to and from C. -Each element in this stack represents a Lua value -(nil, number, string, etc.). -Functions in the API can access this stack through the -Lua state parameter that they receive. - - -

-Whenever Lua calls C, the called function gets a new stack, -which is independent of previous stacks and of stacks of -C functions that are still active. -This stack initially contains any arguments to the C function -and it is where the C function can store temporary -Lua values and must push its results -to be returned to the caller (see lua_CFunction). - - -

-For convenience, -most query operations in the API do not follow a strict stack discipline. -Instead, they can refer to any element in the stack -by using an index: -A positive index represents an absolute stack position -(starting at 1); -a negative index represents an offset relative to the top of the stack. -More specifically, if the stack has n elements, -then index 1 represents the first element -(that is, the element that was pushed onto the stack first) -and -index n represents the last element; -index -1 also represents the last element -(that is, the element at the top) -and index -n represents the first element. - - - - - -

4.2 – Stack Size

- -

-When you interact with the Lua API, -you are responsible for ensuring consistency. -In particular, -you are responsible for controlling stack overflow. -You can use the function lua_checkstack -to ensure that the stack has enough space for pushing new elements. - - -

-Whenever Lua calls C, -it ensures that the stack has space for -at least LUA_MINSTACK extra slots. -LUA_MINSTACK is defined as 20, -so that usually you do not have to worry about stack space -unless your code has loops pushing elements onto the stack. - - -

-When you call a Lua function -without a fixed number of results (see lua_call), -Lua ensures that the stack has enough space for all results, -but it does not ensure any extra space. -So, before pushing anything in the stack after such a call -you should use lua_checkstack. - - - - - -

4.3 – Valid and Acceptable Indices

- -

-Any function in the API that receives stack indices -works only with valid indices or acceptable indices. - - -

-A valid index is an index that refers to a -position that stores a modifiable Lua value. -It comprises stack indices between 1 and the stack top -(1 ≤ abs(index) ≤ top) - -plus pseudo-indices, -which represent some positions that are accessible to C code -but that are not in the stack. -Pseudo-indices are used to access the registry (see §4.5) -and the upvalues of a C function (see §4.4). - - -

-Functions that do not need a specific mutable position, -but only a value (e.g., query functions), -can be called with acceptable indices. -An acceptable index can be any valid index, -but it also can be any positive index after the stack top -within the space allocated for the stack, -that is, indices up to the stack size. -(Note that 0 is never an acceptable index.) -Except when noted otherwise, -functions in the API work with acceptable indices. - - -

-Acceptable indices serve to avoid extra tests -against the stack top when querying the stack. -For instance, a C function can query its third argument -without the need to first check whether there is a third argument, -that is, without the need to check whether 3 is a valid index. - - -

-For functions that can be called with acceptable indices, -any non-valid index is treated as if it -contains a value of a virtual type LUA_TNONE, -which behaves like a nil value. - - - - - -

4.4 – C Closures

- -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure -(see lua_pushcclosure); -these values are called upvalues and are -accessible to the function whenever it is called. - - -

-Whenever a C function is called, -its upvalues are located at specific pseudo-indices. -These pseudo-indices are produced by the macro -lua_upvalueindex. -The first upvalue associated with a function is at index -lua_upvalueindex(1), and so on. -Any access to lua_upvalueindex(n), -where n is greater than the number of upvalues of the -current function -(but not greater than 256, -which is one plus the maximum number of upvalues in a closure), -produces an acceptable but invalid index. - - - - - -

4.5 – Registry

- -

-Lua provides a registry, -a predefined table that can be used by any C code to -store whatever Lua values it needs to store. -The registry table is always located at pseudo-index -LUA_REGISTRYINDEX. -Any C library can store data into this table, -but it must take care to choose keys -that are different from those used -by other libraries, to avoid collisions. -Typically, you should use as key a string containing your library name, -or a light userdata with the address of a C object in your code, -or any Lua object created by your code. -As with variable names, -string keys starting with an underscore followed by -uppercase letters are reserved for Lua. - - -

-The integer keys in the registry are used -by the reference mechanism (see luaL_ref) -and by some predefined values. -Therefore, integer keys must not be used for other purposes. - - -

-When you create a new Lua state, -its registry comes with some predefined values. -These predefined values are indexed with integer keys -defined as constants in lua.h. -The following constants are defined: - -

    -
  • LUA_RIDX_MAINTHREAD: At this index the registry has -the main thread of the state. -(The main thread is the one created together with the state.) -
  • - -
  • LUA_RIDX_GLOBALS: At this index the registry has -the global environment. -
  • -
- - - - -

4.6 – Error Handling in C

- -

-Internally, Lua uses the C longjmp facility to handle errors. -(Lua will use exceptions if you compile it as C++; -search for LUAI_THROW in the source code for details.) -When Lua faces any error -(such as a memory allocation error or a type error) -it raises an error; -that is, it does a long jump. -A protected environment uses setjmp -to set a recovery point; -any error jumps to the most recent active recovery point. - - -

-Inside a C function you can raise an error by calling lua_error. - - -

-Most functions in the API can raise an error, -for instance due to a memory allocation error. -The documentation for each function indicates whether -it can raise errors. - - -

-If an error happens outside any protected environment, -Lua calls a panic function (see lua_atpanic) -and then calls abort, -thus exiting the host application. -Your panic function can avoid this exit by -never returning -(e.g., doing a long jump to your own recovery point outside Lua). - - -

-The panic function, -as its name implies, -is a mechanism of last resort. -Programs should avoid it. -As a general rule, -when a C function is called by Lua with a Lua state, -it can do whatever it wants on that Lua state, -as it should be already protected. -However, -when C code operates on other Lua states -(e.g., a Lua argument to the function, -a Lua state stored in the registry, or -the result of lua_newthread), -it should use them only in API calls that cannot raise errors. - - -

-The panic function runs as if it were a message handler (see §2.3); -in particular, the error object is at the top of the stack. -However, there is no guarantee about stack space. -To push anything on the stack, -the panic function must first check the available space (see §4.2). - - - - - -

4.7 – Handling Yields in C

- -

-Internally, Lua uses the C longjmp facility to yield a coroutine. -Therefore, if a C function foo calls an API function -and this API function yields -(directly or indirectly by calling another function that yields), -Lua cannot return to foo any more, -because the longjmp removes its frame from the C stack. - - -

-To avoid this kind of problem, -Lua raises an error whenever it tries to yield across an API call, -except for three functions: -lua_yieldk, lua_callk, and lua_pcallk. -All those functions receive a continuation function -(as a parameter named k) to continue execution after a yield. - - -

-We need to set some terminology to explain continuations. -We have a C function called from Lua which we will call -the original function. -This original function then calls one of those three functions in the C API, -which we will call the callee function, -that then yields the current thread. -(This can happen when the callee function is lua_yieldk, -or when the callee function is either lua_callk or lua_pcallk -and the function called by them yields.) - - -

-Suppose the running thread yields while executing the callee function. -After the thread resumes, -it eventually will finish running the callee function. -However, -the callee function cannot return to the original function, -because its frame in the C stack was destroyed by the yield. -Instead, Lua calls a continuation function, -which was given as an argument to the callee function. -As the name implies, -the continuation function should continue the task -of the original function. - - -

-As an illustration, consider the following function: - -

-     int original_function (lua_State *L) {
-       ...     /* code 1 */
-       status = lua_pcall(L, n, m, h);  /* calls Lua */
-       ...     /* code 2 */
-     }
-

-Now we want to allow -the Lua code being run by lua_pcall to yield. -First, we can rewrite our function like here: - -

-     int k (lua_State *L, int status, lua_KContext ctx) {
-       ...  /* code 2 */
-     }
-     
-     int original_function (lua_State *L) {
-       ...     /* code 1 */
-       return k(L, lua_pcall(L, n, m, h), ctx);
-     }
-

-In the above code, -the new function k is a -continuation function (with type lua_KFunction), -which should do all the work that the original function -was doing after calling lua_pcall. -Now, we must inform Lua that it must call k if the Lua code -being executed by lua_pcall gets interrupted in some way -(errors or yielding), -so we rewrite the code as here, -replacing lua_pcall by lua_pcallk: - -

-     int original_function (lua_State *L) {
-       ...     /* code 1 */
-       return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
-     }
-

-Note the external, explicit call to the continuation: -Lua will call the continuation only if needed, that is, -in case of errors or resuming after a yield. -If the called function returns normally without ever yielding, -lua_pcallk (and lua_callk) will also return normally. -(Of course, instead of calling the continuation in that case, -you can do the equivalent work directly inside the original function.) - - -

-Besides the Lua state, -the continuation function has two other parameters: -the final status of the call plus the context value (ctx) that -was passed originally to lua_pcallk. -(Lua does not use this context value; -it only passes this value from the original function to the -continuation function.) -For lua_pcallk, -the status is the same value that would be returned by lua_pcallk, -except that it is LUA_YIELD when being executed after a yield -(instead of LUA_OK). -For lua_yieldk and lua_callk, -the status is always LUA_YIELD when Lua calls the continuation. -(For these two functions, -Lua will not call the continuation in case of errors, -because they do not handle errors.) -Similarly, when using lua_callk, -you should call the continuation function -with LUA_OK as the status. -(For lua_yieldk, there is not much point in calling -directly the continuation function, -because lua_yieldk usually does not return.) - - -

-Lua treats the continuation function as if it were the original function. -The continuation function receives the same Lua stack -from the original function, -in the same state it would be if the callee function had returned. -(For instance, -after a lua_callk the function and its arguments are -removed from the stack and replaced by the results from the call.) -It also has the same upvalues. -Whatever it returns is handled by Lua as if it were the return -of the original function. - - - - - -

4.8 – Functions and Types

- -

-Here we list all functions and types from the C API in -alphabetical order. -Each function has an indicator like this: -[-o, +p, x] - - -

-The first field, o, -is how many elements the function pops from the stack. -The second field, p, -is how many elements the function pushes onto the stack. -(Any function always pushes its results after popping its arguments.) -A field in the form x|y means the function can push (or pop) -x or y elements, -depending on the situation; -an interrogation mark '?' means that -we cannot know how many elements the function pops/pushes -by looking only at its arguments -(e.g., they may depend on what is on the stack). -The third field, x, -tells whether the function may raise errors: -'-' means the function never raises any error; -'m' means the function may raise out-of-memory errors -and errors running a __gc metamethod; -'e' means the function may raise any errors -(it can run arbitrary Lua code, -either directly or through metamethods); -'v' means the function may raise an error on purpose. - - - -


lua_absindex

-[-0, +0, –] -

int lua_absindex (lua_State *L, int idx);
- -

-Converts the acceptable index idx -into an equivalent absolute index -(that is, one that does not depend on the stack top). - - - - - -


lua_Alloc

-
typedef void * (*lua_Alloc) (void *ud,
-                             void *ptr,
-                             size_t osize,
-                             size_t nsize);
- -

-The type of the memory-allocation function used by Lua states. -The allocator function must provide a -functionality similar to realloc, -but not exactly the same. -Its arguments are -ud, an opaque pointer passed to lua_newstate; -ptr, a pointer to the block being allocated/reallocated/freed; -osize, the original size of the block or some code about what -is being allocated; -and nsize, the new size of the block. - - -

-When ptr is not NULL, -osize is the size of the block pointed by ptr, -that is, the size given when it was allocated or reallocated. - - -

-When ptr is NULL, -osize encodes the kind of object that Lua is allocating. -osize is any of -LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, -LUA_TUSERDATA, or LUA_TTHREAD when (and only when) -Lua is creating a new object of that type. -When osize is some other value, -Lua is allocating memory for something else. - - -

-Lua assumes the following behavior from the allocator function: - - -

-When nsize is zero, -the allocator must behave like free -and return NULL. - - -

-When nsize is not zero, -the allocator must behave like realloc. -The allocator returns NULL -if and only if it cannot fulfill the request. -Lua assumes that the allocator never fails when -osize >= nsize. - - -

-Here is a simple implementation for the allocator function. -It is used in the auxiliary library by luaL_newstate. - -

-     static void *l_alloc (void *ud, void *ptr, size_t osize,
-                                                size_t nsize) {
-       (void)ud;  (void)osize;  /* not used */
-       if (nsize == 0) {
-         free(ptr);
-         return NULL;
-       }
-       else
-         return realloc(ptr, nsize);
-     }
-

-Note that Standard C ensures -that free(NULL) has no effect and that -realloc(NULL,size) is equivalent to malloc(size). -This code assumes that realloc does not fail when shrinking a block. -(Although Standard C does not ensure this behavior, -it seems to be a safe assumption.) - - - - - -


lua_arith

-[-(2|1), +1, e] -

void lua_arith (lua_State *L, int op);
- -

-Performs an arithmetic or bitwise operation over the two values -(or one, in the case of negations) -at the top of the stack, -with the value at the top being the second operand, -pops these values, and pushes the result of the operation. -The function follows the semantics of the corresponding Lua operator -(that is, it may call metamethods). - - -

-The value of op must be one of the following constants: - -

- - - - -

lua_atpanic

-[-0, +0, –] -

lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
- -

-Sets a new panic function and returns the old one (see §4.6). - - - - - -


lua_call

-[-(nargs+1), +nresults, e] -

void lua_call (lua_State *L, int nargs, int nresults);
- -

-Calls a function. - - -

-To call a function you must use the following protocol: -first, the function to be called is pushed onto the stack; -then, the arguments to the function are pushed -in direct order; -that is, the first argument is pushed first. -Finally you call lua_call; -nargs is the number of arguments that you pushed onto the stack. -All arguments and the function value are popped from the stack -when the function is called. -The function results are pushed onto the stack when the function returns. -The number of results is adjusted to nresults, -unless nresults is LUA_MULTRET. -In this case, all results from the function are pushed; -Lua takes care that the returned values fit into the stack space, -but it does not ensure any extra space in the stack. -The function results are pushed onto the stack in direct order -(the first result is pushed first), -so that after the call the last result is on the top of the stack. - - -

-Any error inside the called function is propagated upwards -(with a longjmp). - - -

-The following example shows how the host program can do the -equivalent to this Lua code: - -

-     a = f("how", t.x, 14)
-

-Here it is in C: - -

-     lua_getglobal(L, "f");                  /* function to be called */
-     lua_pushliteral(L, "how");                       /* 1st argument */
-     lua_getglobal(L, "t");                    /* table to be indexed */
-     lua_getfield(L, -1, "x");        /* push result of t.x (2nd arg) */
-     lua_remove(L, -2);                  /* remove 't' from the stack */
-     lua_pushinteger(L, 14);                          /* 3rd argument */
-     lua_call(L, 3, 1);     /* call 'f' with 3 arguments and 1 result */
-     lua_setglobal(L, "a");                         /* set global 'a' */
-

-Note that the code above is balanced: -at its end, the stack is back to its original configuration. -This is considered good programming practice. - - - - - -


lua_callk

-[-(nargs + 1), +nresults, e] -

void lua_callk (lua_State *L,
-                int nargs,
-                int nresults,
-                lua_KContext ctx,
-                lua_KFunction k);
- -

-This function behaves exactly like lua_call, -but allows the called function to yield (see §4.7). - - - - - -


lua_CFunction

-
typedef int (*lua_CFunction) (lua_State *L);
- -

-Type for C functions. - - -

-In order to communicate properly with Lua, -a C function must use the following protocol, -which defines the way parameters and results are passed: -a C function receives its arguments from Lua in its stack -in direct order (the first argument is pushed first). -So, when the function starts, -lua_gettop(L) returns the number of arguments received by the function. -The first argument (if any) is at index 1 -and its last argument is at index lua_gettop(L). -To return values to Lua, a C function just pushes them onto the stack, -in direct order (the first result is pushed first), -and returns the number of results. -Any other value in the stack below the results will be properly -discarded by Lua. -Like a Lua function, a C function called by Lua can also return -many results. - - -

-As an example, the following function receives a variable number -of numeric arguments and returns their average and their sum: - -

-     static int foo (lua_State *L) {
-       int n = lua_gettop(L);    /* number of arguments */
-       lua_Number sum = 0.0;
-       int i;
-       for (i = 1; i <= n; i++) {
-         if (!lua_isnumber(L, i)) {
-           lua_pushliteral(L, "incorrect argument");
-           lua_error(L);
-         }
-         sum += lua_tonumber(L, i);
-       }
-       lua_pushnumber(L, sum/n);        /* first result */
-       lua_pushnumber(L, sum);         /* second result */
-       return 2;                   /* number of results */
-     }
-
- - - - -

lua_checkstack

-[-0, +0, –] -

int lua_checkstack (lua_State *L, int n);
- -

-Ensures that the stack has space for at least n extra slots -(that is, that you can safely push up to n values into it). -It returns false if it cannot fulfill the request, -either because it would cause the stack -to be larger than a fixed maximum size -(typically at least several thousand elements) or -because it cannot allocate memory for the extra space. -This function never shrinks the stack; -if the stack already has space for the extra slots, -it is left unchanged. - - - - - -


lua_close

-[-0, +0, –] -

void lua_close (lua_State *L);
- -

-Destroys all objects in the given Lua state -(calling the corresponding garbage-collection metamethods, if any) -and frees all dynamic memory used by this state. -In several platforms, you may not need to call this function, -because all resources are naturally released when the host program ends. -On the other hand, long-running programs that create multiple states, -such as daemons or web servers, -will probably need to close states as soon as they are not needed. - - - - - -


lua_compare

-[-0, +0, e] -

int lua_compare (lua_State *L, int index1, int index2, int op);
- -

-Compares two Lua values. -Returns 1 if the value at index index1 satisfies op -when compared with the value at index index2, -following the semantics of the corresponding Lua operator -(that is, it may call metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices is not valid. - - -

-The value of op must be one of the following constants: - -

    - -
  • LUA_OPEQ: compares for equality (==)
  • -
  • LUA_OPLT: compares for less than (<)
  • -
  • LUA_OPLE: compares for less or equal (<=)
  • - -
- - - - -

lua_concat

-[-n, +1, e] -

void lua_concat (lua_State *L, int n);
- -

-Concatenates the n values at the top of the stack, -pops them, and leaves the result at the top. -If n is 1, the result is the single value on the stack -(that is, the function does nothing); -if n is 0, the result is the empty string. -Concatenation is performed following the usual semantics of Lua -(see §3.4.6). - - - - - -


lua_copy

-[-0, +0, –] -

void lua_copy (lua_State *L, int fromidx, int toidx);
- -

-Copies the element at index fromidx -into the valid index toidx, -replacing the value at that position. -Values at other positions are not affected. - - - - - -


lua_createtable

-[-0, +1, m] -

void lua_createtable (lua_State *L, int narr, int nrec);
- -

-Creates a new empty table and pushes it onto the stack. -Parameter narr is a hint for how many elements the table -will have as a sequence; -parameter nrec is a hint for how many other elements -the table will have. -Lua may use these hints to preallocate memory for the new table. -This preallocation is useful for performance when you know in advance -how many elements the table will have. -Otherwise you can use the function lua_newtable. - - - - - -


lua_dump

-[-0, +0, –] -

int lua_dump (lua_State *L,
-                        lua_Writer writer,
-                        void *data,
-                        int strip);
- -

-Dumps a function as a binary chunk. -Receives a Lua function on the top of the stack -and produces a binary chunk that, -if loaded again, -results in a function equivalent to the one dumped. -As it produces parts of the chunk, -lua_dump calls function writer (see lua_Writer) -with the given data -to write them. - - -

-If strip is true, -the binary representation may not include all debug information -about the function, -to save space. - - -

-The value returned is the error code returned by the last -call to the writer; -0 means no errors. - - -

-This function does not pop the Lua function from the stack. - - - - - -


lua_error

-[-1, +0, v] -

int lua_error (lua_State *L);
- -

-Generates a Lua error, -using the value at the top of the stack as the error object. -This function does a long jump, -and therefore never returns -(see luaL_error). - - - - - -


lua_gc

-[-0, +0, m] -

int lua_gc (lua_State *L, int what, int data);
- -

-Controls the garbage collector. - - -

-This function performs several tasks, -according to the value of the parameter what: - -

    - -
  • LUA_GCSTOP: -stops the garbage collector. -
  • - -
  • LUA_GCRESTART: -restarts the garbage collector. -
  • - -
  • LUA_GCCOLLECT: -performs a full garbage-collection cycle. -
  • - -
  • LUA_GCCOUNT: -returns the current amount of memory (in Kbytes) in use by Lua. -
  • - -
  • LUA_GCCOUNTB: -returns the remainder of dividing the current amount of bytes of -memory in use by Lua by 1024. -
  • - -
  • LUA_GCSTEP: -performs an incremental step of garbage collection. -
  • - -
  • LUA_GCSETPAUSE: -sets data as the new value -for the pause of the collector (see §2.5) -and returns the previous value of the pause. -
  • - -
  • LUA_GCSETSTEPMUL: -sets data as the new value for the step multiplier of -the collector (see §2.5) -and returns the previous value of the step multiplier. -
  • - -
  • LUA_GCISRUNNING: -returns a boolean that tells whether the collector is running -(i.e., not stopped). -
  • - -
- -

-For more details about these options, -see collectgarbage. - - - - - -


lua_getallocf

-[-0, +0, –] -

lua_Alloc lua_getallocf (lua_State *L, void **ud);
- -

-Returns the memory-allocation function of a given state. -If ud is not NULL, Lua stores in *ud the -opaque pointer given when the memory-allocator function was set. - - - - - -


lua_getfield

-[-0, +1, e] -

int lua_getfield (lua_State *L, int index, const char *k);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given index. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - -

-Returns the type of the pushed value. - - - - - -


lua_getextraspace

-[-0, +0, –] -

void *lua_getextraspace (lua_State *L);
- -

-Returns a pointer to a raw memory area associated with the -given Lua state. -The application can use this area for any purpose; -Lua does not use it for anything. - - -

-Each new thread has this area initialized with a copy -of the area of the main thread. - - -

-By default, this area has the size of a pointer to void, -but you can recompile Lua with a different size for this area. -(See LUA_EXTRASPACE in luaconf.h.) - - - - - -


lua_getglobal

-[-0, +1, e] -

int lua_getglobal (lua_State *L, const char *name);
- -

-Pushes onto the stack the value of the global name. -Returns the type of that value. - - - - - -


lua_geti

-[-0, +1, e] -

int lua_geti (lua_State *L, int index, lua_Integer i);
- -

-Pushes onto the stack the value t[i], -where t is the value at the given index. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - -

-Returns the type of the pushed value. - - - - - -


lua_getmetatable

-[-0, +(0|1), –] -

int lua_getmetatable (lua_State *L, int index);
- -

-If the value at the given index has a metatable, -the function pushes that metatable onto the stack and returns 1. -Otherwise, -the function returns 0 and pushes nothing on the stack. - - - - - -


lua_gettable

-[-1, +1, e] -

int lua_gettable (lua_State *L, int index);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given index -and k is the value at the top of the stack. - - -

-This function pops the key from the stack, -pushing the resulting value in its place. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - -

-Returns the type of the pushed value. - - - - - -


lua_gettop

-[-0, +0, –] -

int lua_gettop (lua_State *L);
- -

-Returns the index of the top element in the stack. -Because indices start at 1, -this result is equal to the number of elements in the stack; -in particular, 0 means an empty stack. - - - - - -


lua_getuservalue

-[-0, +1, –] -

int lua_getuservalue (lua_State *L, int index);
- -

-Pushes onto the stack the Lua value associated with the full userdata -at the given index. - - -

-Returns the type of the pushed value. - - - - - -


lua_insert

-[-1, +1, –] -

void lua_insert (lua_State *L, int index);
- -

-Moves the top element into the given valid index, -shifting up the elements above this index to open space. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_Integer

-
typedef ... lua_Integer;
- -

-The type of integers in Lua. - - -

-By default this type is long long, -(usually a 64-bit two-complement integer), -but that can be changed to long or int -(usually a 32-bit two-complement integer). -(See LUA_INT_TYPE in luaconf.h.) - - -

-Lua also defines the constants -LUA_MININTEGER and LUA_MAXINTEGER, -with the minimum and the maximum values that fit in this type. - - - - - -


lua_isboolean

-[-0, +0, –] -

int lua_isboolean (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a boolean, -and 0 otherwise. - - - - - -


lua_iscfunction

-[-0, +0, –] -

int lua_iscfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a C function, -and 0 otherwise. - - - - - -


lua_isfunction

-[-0, +0, –] -

int lua_isfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a function -(either C or Lua), and 0 otherwise. - - - - - -


lua_isinteger

-[-0, +0, –] -

int lua_isinteger (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is an integer -(that is, the value is a number and is represented as an integer), -and 0 otherwise. - - - - - -


lua_islightuserdata

-[-0, +0, –] -

int lua_islightuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a light userdata, -and 0 otherwise. - - - - - -


lua_isnil

-[-0, +0, –] -

int lua_isnil (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is nil, -and 0 otherwise. - - - - - -


lua_isnone

-[-0, +0, –] -

int lua_isnone (lua_State *L, int index);
- -

-Returns 1 if the given index is not valid, -and 0 otherwise. - - - - - -


lua_isnoneornil

-[-0, +0, –] -

int lua_isnoneornil (lua_State *L, int index);
- -

-Returns 1 if the given index is not valid -or if the value at this index is nil, -and 0 otherwise. - - - - - -


lua_isnumber

-[-0, +0, –] -

int lua_isnumber (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a number -or a string convertible to a number, -and 0 otherwise. - - - - - -


lua_isstring

-[-0, +0, –] -

int lua_isstring (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a string -or a number (which is always convertible to a string), -and 0 otherwise. - - - - - -


lua_istable

-[-0, +0, –] -

int lua_istable (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a table, -and 0 otherwise. - - - - - -


lua_isthread

-[-0, +0, –] -

int lua_isthread (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a thread, -and 0 otherwise. - - - - - -


lua_isuserdata

-[-0, +0, –] -

int lua_isuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a userdata -(either full or light), and 0 otherwise. - - - - - -


lua_isyieldable

-[-0, +0, –] -

int lua_isyieldable (lua_State *L);
- -

-Returns 1 if the given coroutine can yield, -and 0 otherwise. - - - - - -


lua_KContext

-
typedef ... lua_KContext;
- -

-The type for continuation-function contexts. -It must be a numeric type. -This type is defined as intptr_t -when intptr_t is available, -so that it can store pointers too. -Otherwise, it is defined as ptrdiff_t. - - - - - -


lua_KFunction

-
typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
- -

-Type for continuation functions (see §4.7). - - - - - -


lua_len

-[-0, +1, e] -

void lua_len (lua_State *L, int index);
- -

-Returns the length of the value at the given index. -It is equivalent to the '#' operator in Lua (see §3.4.7) and -may trigger a metamethod for the "length" event (see §2.4). -The result is pushed on the stack. - - - - - -


lua_load

-[-0, +1, –] -

int lua_load (lua_State *L,
-              lua_Reader reader,
-              void *data,
-              const char *chunkname,
-              const char *mode);
- -

-Loads a Lua chunk without running it. -If there are no errors, -lua_load pushes the compiled chunk as a Lua -function on top of the stack. -Otherwise, it pushes an error message. - - -

-The return values of lua_load are: - -

    - -
  • LUA_OK: no errors;
  • - -
  • LUA_ERRSYNTAX: -syntax error during precompilation;
  • - -
  • LUA_ERRMEM: -memory allocation (out-of-memory) error;
  • - -
  • LUA_ERRGCMM: -error while running a __gc metamethod. -(This error has no relation with the chunk being loaded. -It is generated by the garbage collector.) -
  • - -
- -

-The lua_load function uses a user-supplied reader function -to read the chunk (see lua_Reader). -The data argument is an opaque value passed to the reader function. - - -

-The chunkname argument gives a name to the chunk, -which is used for error messages and in debug information (see §4.9). - - -

-lua_load automatically detects whether the chunk is text or binary -and loads it accordingly (see program luac). -The string mode works as in function load, -with the addition that -a NULL value is equivalent to the string "bt". - - -

-lua_load uses the stack internally, -so the reader function must always leave the stack -unmodified when returning. - - -

-If the resulting function has upvalues, -its first upvalue is set to the value of the global environment -stored at index LUA_RIDX_GLOBALS in the registry (see §4.5). -When loading main chunks, -this upvalue will be the _ENV variable (see §2.2). -Other upvalues are initialized with nil. - - - - - -


lua_newstate

-[-0, +0, –] -

lua_State *lua_newstate (lua_Alloc f, void *ud);
- -

-Creates a new thread running in a new, independent state. -Returns NULL if it cannot create the thread or the state -(due to lack of memory). -The argument f is the allocator function; -Lua does all memory allocation for this state -through this function (see lua_Alloc). -The second argument, ud, is an opaque pointer that Lua -passes to the allocator in every call. - - - - - -


lua_newtable

-[-0, +1, m] -

void lua_newtable (lua_State *L);
- -

-Creates a new empty table and pushes it onto the stack. -It is equivalent to lua_createtable(L, 0, 0). - - - - - -


lua_newthread

-[-0, +1, m] -

lua_State *lua_newthread (lua_State *L);
- -

-Creates a new thread, pushes it on the stack, -and returns a pointer to a lua_State that represents this new thread. -The new thread returned by this function shares with the original thread -its global environment, -but has an independent execution stack. - - -

-There is no explicit function to close or to destroy a thread. -Threads are subject to garbage collection, -like any Lua object. - - - - - -


lua_newuserdata

-[-0, +1, m] -

void *lua_newuserdata (lua_State *L, size_t size);
- -

-This function allocates a new block of memory with the given size, -pushes onto the stack a new full userdata with the block address, -and returns this address. -The host program can freely use this memory. - - - - - -


lua_next

-[-1, +(2|0), e] -

int lua_next (lua_State *L, int index);
- -

-Pops a key from the stack, -and pushes a key–value pair from the table at the given index -(the "next" pair after the given key). -If there are no more elements in the table, -then lua_next returns 0 (and pushes nothing). - - -

-A typical traversal looks like this: - -

-     /* table is in the stack at index 't' */
-     lua_pushnil(L);  /* first key */
-     while (lua_next(L, t) != 0) {
-       /* uses 'key' (at index -2) and 'value' (at index -1) */
-       printf("%s - %s\n",
-              lua_typename(L, lua_type(L, -2)),
-              lua_typename(L, lua_type(L, -1)));
-       /* removes 'value'; keeps 'key' for next iteration */
-       lua_pop(L, 1);
-     }
-
- -

-While traversing a table, -do not call lua_tolstring directly on a key, -unless you know that the key is actually a string. -Recall that lua_tolstring may change -the value at the given index; -this confuses the next call to lua_next. - - -

-See function next for the caveats of modifying -the table during its traversal. - - - - - -


lua_Number

-
typedef ... lua_Number;
- -

-The type of floats in Lua. - - -

-By default this type is double, -but that can be changed to a single float or a long double. -(See LUA_FLOAT_TYPE in luaconf.h.) - - - - - -


lua_numbertointeger

-
int lua_numbertointeger (lua_Number n, lua_Integer *p);
- -

-Converts a Lua float to a Lua integer. -This macro assumes that n has an integral value. -If that value is within the range of Lua integers, -it is converted to an integer and assigned to *p. -The macro results in a boolean indicating whether the -conversion was successful. -(Note that this range test can be tricky to do -correctly without this macro, -due to roundings.) - - -

-This macro may evaluate its arguments more than once. - - - - - -


lua_pcall

-[-(nargs + 1), +(nresults|1), –] -

int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);
- -

-Calls a function in protected mode. - - -

-Both nargs and nresults have the same meaning as -in lua_call. -If there are no errors during the call, -lua_pcall behaves exactly like lua_call. -However, if there is any error, -lua_pcall catches it, -pushes a single value on the stack (the error object), -and returns an error code. -Like lua_call, -lua_pcall always removes the function -and its arguments from the stack. - - -

-If msgh is 0, -then the error object returned on the stack -is exactly the original error object. -Otherwise, msgh is the stack index of a -message handler. -(This index cannot be a pseudo-index.) -In case of runtime errors, -this function will be called with the error object -and its return value will be the object -returned on the stack by lua_pcall. - - -

-Typically, the message handler is used to add more debug -information to the error object, such as a stack traceback. -Such information cannot be gathered after the return of lua_pcall, -since by then the stack has unwound. - - -

-The lua_pcall function returns one of the following constants -(defined in lua.h): - -

    - -
  • LUA_OK (0): -success.
  • - -
  • LUA_ERRRUN: -a runtime error. -
  • - -
  • LUA_ERRMEM: -memory allocation error. -For such errors, Lua does not call the message handler. -
  • - -
  • LUA_ERRERR: -error while running the message handler. -
  • - -
  • LUA_ERRGCMM: -error while running a __gc metamethod. -For such errors, Lua does not call the message handler -(as this kind of error typically has no relation -with the function being called). -
  • - -
- - - - -

lua_pcallk

-[-(nargs + 1), +(nresults|1), –] -

int lua_pcallk (lua_State *L,
-                int nargs,
-                int nresults,
-                int msgh,
-                lua_KContext ctx,
-                lua_KFunction k);
- -

-This function behaves exactly like lua_pcall, -but allows the called function to yield (see §4.7). - - - - - -


lua_pop

-[-n, +0, –] -

void lua_pop (lua_State *L, int n);
- -

-Pops n elements from the stack. - - - - - -


lua_pushboolean

-[-0, +1, –] -

void lua_pushboolean (lua_State *L, int b);
- -

-Pushes a boolean value with value b onto the stack. - - - - - -


lua_pushcclosure

-[-n, +1, m] -

void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
- -

-Pushes a new C closure onto the stack. - - -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure (see §4.4); -these values are then accessible to the function whenever it is called. -To associate values with a C function, -first these values must be pushed onto the stack -(when there are multiple values, the first value is pushed first). -Then lua_pushcclosure -is called to create and push the C function onto the stack, -with the argument n telling how many values will be -associated with the function. -lua_pushcclosure also pops these values from the stack. - - -

-The maximum value for n is 255. - - -

-When n is zero, -this function creates a light C function, -which is just a pointer to the C function. -In that case, it never raises a memory error. - - - - - -


lua_pushcfunction

-[-0, +1, –] -

void lua_pushcfunction (lua_State *L, lua_CFunction f);
- -

-Pushes a C function onto the stack. -This function receives a pointer to a C function -and pushes onto the stack a Lua value of type function that, -when called, invokes the corresponding C function. - - -

-Any function to be callable by Lua must -follow the correct protocol to receive its parameters -and return its results (see lua_CFunction). - - - - - -


lua_pushfstring

-[-0, +1, e] -

const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
- -

-Pushes onto the stack a formatted string -and returns a pointer to this string. -It is similar to the ISO C function sprintf, -but has some important differences: - -

    - -
  • -You do not have to allocate space for the result: -the result is a Lua string and Lua takes care of memory allocation -(and deallocation, through garbage collection). -
  • - -
  • -The conversion specifiers are quite restricted. -There are no flags, widths, or precisions. -The conversion specifiers can only be -'%%' (inserts the character '%'), -'%s' (inserts a zero-terminated string, with no size restrictions), -'%f' (inserts a lua_Number), -'%I' (inserts a lua_Integer), -'%p' (inserts a pointer as a hexadecimal numeral), -'%d' (inserts an int), -'%c' (inserts an int as a one-byte character), and -'%U' (inserts a long int as a UTF-8 byte sequence). -
  • - -
- -

-Unlike other push functions, -this function checks for the stack space it needs, -including the slot for its result. - - - - - -


lua_pushglobaltable

-[-0, +1, –] -

void lua_pushglobaltable (lua_State *L);
- -

-Pushes the global environment onto the stack. - - - - - -


lua_pushinteger

-[-0, +1, –] -

void lua_pushinteger (lua_State *L, lua_Integer n);
- -

-Pushes an integer with value n onto the stack. - - - - - -


lua_pushlightuserdata

-[-0, +1, –] -

void lua_pushlightuserdata (lua_State *L, void *p);
- -

-Pushes a light userdata onto the stack. - - -

-Userdata represent C values in Lua. -A light userdata represents a pointer, a void*. -It is a value (like a number): -you do not create it, it has no individual metatable, -and it is not collected (as it was never created). -A light userdata is equal to "any" -light userdata with the same C address. - - - - - -


lua_pushliteral

-[-0, +1, m] -

const char *lua_pushliteral (lua_State *L, const char *s);
- -

-This macro is equivalent to lua_pushstring, -but should be used only when s is a literal string. - - - - - -


lua_pushlstring

-[-0, +1, m] -

const char *lua_pushlstring (lua_State *L, const char *s, size_t len);
- -

-Pushes the string pointed to by s with size len -onto the stack. -Lua makes (or reuses) an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. -The string can contain any binary data, -including embedded zeros. - - -

-Returns a pointer to the internal copy of the string. - - - - - -


lua_pushnil

-[-0, +1, –] -

void lua_pushnil (lua_State *L);
- -

-Pushes a nil value onto the stack. - - - - - -


lua_pushnumber

-[-0, +1, –] -

void lua_pushnumber (lua_State *L, lua_Number n);
- -

-Pushes a float with value n onto the stack. - - - - - -


lua_pushstring

-[-0, +1, m] -

const char *lua_pushstring (lua_State *L, const char *s);
- -

-Pushes the zero-terminated string pointed to by s -onto the stack. -Lua makes (or reuses) an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. - - -

-Returns a pointer to the internal copy of the string. - - -

-If s is NULL, pushes nil and returns NULL. - - - - - -


lua_pushthread

-[-0, +1, –] -

int lua_pushthread (lua_State *L);
- -

-Pushes the thread represented by L onto the stack. -Returns 1 if this thread is the main thread of its state. - - - - - -


lua_pushvalue

-[-0, +1, –] -

void lua_pushvalue (lua_State *L, int index);
- -

-Pushes a copy of the element at the given index -onto the stack. - - - - - -


lua_pushvfstring

-[-0, +1, m] -

const char *lua_pushvfstring (lua_State *L,
-                              const char *fmt,
-                              va_list argp);
- -

-Equivalent to lua_pushfstring, except that it receives a va_list -instead of a variable number of arguments. - - - - - -


lua_rawequal

-[-0, +0, –] -

int lua_rawequal (lua_State *L, int index1, int index2);
- -

-Returns 1 if the two values in indices index1 and -index2 are primitively equal -(that is, without calling the __eq metamethod). -Otherwise returns 0. -Also returns 0 if any of the indices are not valid. - - - - - -


lua_rawget

-[-1, +1, –] -

int lua_rawget (lua_State *L, int index);
- -

-Similar to lua_gettable, but does a raw access -(i.e., without metamethods). - - - - - -


lua_rawgeti

-[-0, +1, –] -

int lua_rawgeti (lua_State *L, int index, lua_Integer n);
- -

-Pushes onto the stack the value t[n], -where t is the table at the given index. -The access is raw, -that is, it does not invoke the __index metamethod. - - -

-Returns the type of the pushed value. - - - - - -


lua_rawgetp

-[-0, +1, –] -

int lua_rawgetp (lua_State *L, int index, const void *p);
- -

-Pushes onto the stack the value t[k], -where t is the table at the given index and -k is the pointer p represented as a light userdata. -The access is raw; -that is, it does not invoke the __index metamethod. - - -

-Returns the type of the pushed value. - - - - - -


lua_rawlen

-[-0, +0, –] -

size_t lua_rawlen (lua_State *L, int index);
- -

-Returns the raw "length" of the value at the given index: -for strings, this is the string length; -for tables, this is the result of the length operator ('#') -with no metamethods; -for userdata, this is the size of the block of memory allocated -for the userdata; -for other values, it is 0. - - - - - -


lua_rawset

-[-2, +0, m] -

void lua_rawset (lua_State *L, int index);
- -

-Similar to lua_settable, but does a raw assignment -(i.e., without metamethods). - - - - - -


lua_rawseti

-[-1, +0, m] -

void lua_rawseti (lua_State *L, int index, lua_Integer i);
- -

-Does the equivalent of t[i] = v, -where t is the table at the given index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw, -that is, it does not invoke the __newindex metamethod. - - - - - -


lua_rawsetp

-[-1, +0, m] -

void lua_rawsetp (lua_State *L, int index, const void *p);
- -

-Does the equivalent of t[p] = v, -where t is the table at the given index, -p is encoded as a light userdata, -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw, -that is, it does not invoke __newindex metamethod. - - - - - -


lua_Reader

-
typedef const char * (*lua_Reader) (lua_State *L,
-                                    void *data,
-                                    size_t *size);
- -

-The reader function used by lua_load. -Every time it needs another piece of the chunk, -lua_load calls the reader, -passing along its data parameter. -The reader must return a pointer to a block of memory -with a new piece of the chunk -and set size to the block size. -The block must exist until the reader function is called again. -To signal the end of the chunk, -the reader must return NULL or set size to zero. -The reader function may return pieces of any size greater than zero. - - - - - -


lua_register

-[-0, +0, e] -

void lua_register (lua_State *L, const char *name, lua_CFunction f);
- -

-Sets the C function f as the new value of global name. -It is defined as a macro: - -

-     #define lua_register(L,n,f) \
-            (lua_pushcfunction(L, f), lua_setglobal(L, n))
-
- - - - -

lua_remove

-[-1, +0, –] -

void lua_remove (lua_State *L, int index);
- -

-Removes the element at the given valid index, -shifting down the elements above this index to fill the gap. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_replace

-[-1, +0, –] -

void lua_replace (lua_State *L, int index);
- -

-Moves the top element into the given valid index -without shifting any element -(therefore replacing the value at that given index), -and then pops the top element. - - - - - -


lua_resume

-[-?, +?, –] -

int lua_resume (lua_State *L, lua_State *from, int nargs);
- -

-Starts and resumes a coroutine in the given thread L. - - -

-To start a coroutine, -you push onto the thread stack the main function plus any arguments; -then you call lua_resume, -with nargs being the number of arguments. -This call returns when the coroutine suspends or finishes its execution. -When it returns, the stack contains all values passed to lua_yield, -or all values returned by the body function. -lua_resume returns -LUA_YIELD if the coroutine yields, -LUA_OK if the coroutine finishes its execution -without errors, -or an error code in case of errors (see lua_pcall). - - -

-In case of errors, -the stack is not unwound, -so you can use the debug API over it. -The error object is on the top of the stack. - - -

-To resume a coroutine, -you remove any results from the last lua_yield, -put on its stack only the values to -be passed as results from yield, -and then call lua_resume. - - -

-The parameter from represents the coroutine that is resuming L. -If there is no such coroutine, -this parameter can be NULL. - - - - - -


lua_rotate

-[-0, +0, –] -

void lua_rotate (lua_State *L, int idx, int n);
- -

-Rotates the stack elements between the valid index idx -and the top of the stack. -The elements are rotated n positions in the direction of the top, -for a positive n, -or -n positions in the direction of the bottom, -for a negative n. -The absolute value of n must not be greater than the size -of the slice being rotated. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_setallocf

-[-0, +0, –] -

void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
- -

-Changes the allocator function of a given state to f -with user data ud. - - - - - -


lua_setfield

-[-1, +0, e] -

void lua_setfield (lua_State *L, int index, const char *k);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_setglobal

-[-1, +0, e] -

void lua_setglobal (lua_State *L, const char *name);
- -

-Pops a value from the stack and -sets it as the new value of global name. - - - - - -


lua_seti

-[-1, +0, e] -

void lua_seti (lua_State *L, int index, lua_Integer n);
- -

-Does the equivalent to t[n] = v, -where t is the value at the given index -and v is the value at the top of the stack. - - -

-This function pops the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_setmetatable

-[-1, +0, –] -

void lua_setmetatable (lua_State *L, int index);
- -

-Pops a table from the stack and -sets it as the new metatable for the value at the given index. - - - - - -


lua_settable

-[-2, +0, e] -

void lua_settable (lua_State *L, int index);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given index, -v is the value at the top of the stack, -and k is the value just below the top. - - -

-This function pops both the key and the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_settop

-[-?, +?, –] -

void lua_settop (lua_State *L, int index);
- -

-Accepts any index, or 0, -and sets the stack top to this index. -If the new top is larger than the old one, -then the new elements are filled with nil. -If index is 0, then all stack elements are removed. - - - - - -


lua_setuservalue

-[-1, +0, –] -

void lua_setuservalue (lua_State *L, int index);
- -

-Pops a value from the stack and sets it as -the new value associated to the full userdata at the given index. - - - - - -


lua_State

-
typedef struct lua_State lua_State;
- -

-An opaque structure that points to a thread and indirectly -(through the thread) to the whole state of a Lua interpreter. -The Lua library is fully reentrant: -it has no global variables. -All information about a state is accessible through this structure. - - -

-A pointer to this structure must be passed as the first argument to -every function in the library, except to lua_newstate, -which creates a Lua state from scratch. - - - - - -


lua_status

-[-0, +0, –] -

int lua_status (lua_State *L);
- -

-Returns the status of the thread L. - - -

-The status can be 0 (LUA_OK) for a normal thread, -an error code if the thread finished the execution -of a lua_resume with an error, -or LUA_YIELD if the thread is suspended. - - -

-You can only call functions in threads with status LUA_OK. -You can resume threads with status LUA_OK -(to start a new coroutine) or LUA_YIELD -(to resume a coroutine). - - - - - -


lua_stringtonumber

-[-0, +1, –] -

size_t lua_stringtonumber (lua_State *L, const char *s);
- -

-Converts the zero-terminated string s to a number, -pushes that number into the stack, -and returns the total size of the string, -that is, its length plus one. -The conversion can result in an integer or a float, -according to the lexical conventions of Lua (see §3.1). -The string may have leading and trailing spaces and a sign. -If the string is not a valid numeral, -returns 0 and pushes nothing. -(Note that the result can be used as a boolean, -true if the conversion succeeds.) - - - - - -


lua_toboolean

-[-0, +0, –] -

int lua_toboolean (lua_State *L, int index);
- -

-Converts the Lua value at the given index to a C boolean -value (0 or 1). -Like all tests in Lua, -lua_toboolean returns true for any Lua value -different from false and nil; -otherwise it returns false. -(If you want to accept only actual boolean values, -use lua_isboolean to test the value's type.) - - - - - -


lua_tocfunction

-[-0, +0, –] -

lua_CFunction lua_tocfunction (lua_State *L, int index);
- -

-Converts a value at the given index to a C function. -That value must be a C function; -otherwise, returns NULL. - - - - - -


lua_tointeger

-[-0, +0, –] -

lua_Integer lua_tointeger (lua_State *L, int index);
- -

-Equivalent to lua_tointegerx with isnum equal to NULL. - - - - - -


lua_tointegerx

-[-0, +0, –] -

lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the signed integral type lua_Integer. -The Lua value must be an integer, -or a number or string convertible to an integer (see §3.4.3); -otherwise, lua_tointegerx returns 0. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_tolstring

-[-0, +0, m] -

const char *lua_tolstring (lua_State *L, int index, size_t *len);
- -

-Converts the Lua value at the given index to a C string. -If len is not NULL, -it sets *len with the string length. -The Lua value must be a string or a number; -otherwise, the function returns NULL. -If the value is a number, -then lua_tolstring also -changes the actual value in the stack to a string. -(This change confuses lua_next -when lua_tolstring is applied to keys during a table traversal.) - - -

-lua_tolstring returns a pointer -to a string inside the Lua state. -This string always has a zero ('\0') -after its last character (as in C), -but can contain other zeros in its body. - - -

-Because Lua has garbage collection, -there is no guarantee that the pointer returned by lua_tolstring -will be valid after the corresponding Lua value is removed from the stack. - - - - - -


lua_tonumber

-[-0, +0, –] -

lua_Number lua_tonumber (lua_State *L, int index);
- -

-Equivalent to lua_tonumberx with isnum equal to NULL. - - - - - -


lua_tonumberx

-[-0, +0, –] -

lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the C type lua_Number (see lua_Number). -The Lua value must be a number or a string convertible to a number -(see §3.4.3); -otherwise, lua_tonumberx returns 0. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_topointer

-[-0, +0, –] -

const void *lua_topointer (lua_State *L, int index);
- -

-Converts the value at the given index to a generic -C pointer (void*). -The value can be a userdata, a table, a thread, or a function; -otherwise, lua_topointer returns NULL. -Different objects will give different pointers. -There is no way to convert the pointer back to its original value. - - -

-Typically this function is used only for hashing and debug information. - - - - - -


lua_tostring

-[-0, +0, m] -

const char *lua_tostring (lua_State *L, int index);
- -

-Equivalent to lua_tolstring with len equal to NULL. - - - - - -


lua_tothread

-[-0, +0, –] -

lua_State *lua_tothread (lua_State *L, int index);
- -

-Converts the value at the given index to a Lua thread -(represented as lua_State*). -This value must be a thread; -otherwise, the function returns NULL. - - - - - -


lua_touserdata

-[-0, +0, –] -

void *lua_touserdata (lua_State *L, int index);
- -

-If the value at the given index is a full userdata, -returns its block address. -If the value is a light userdata, -returns its pointer. -Otherwise, returns NULL. - - - - - -


lua_type

-[-0, +0, –] -

int lua_type (lua_State *L, int index);
- -

-Returns the type of the value in the given valid index, -or LUA_TNONE for a non-valid (but acceptable) index. -The types returned by lua_type are coded by the following constants -defined in lua.h: -LUA_TNIL (0), -LUA_TNUMBER, -LUA_TBOOLEAN, -LUA_TSTRING, -LUA_TTABLE, -LUA_TFUNCTION, -LUA_TUSERDATA, -LUA_TTHREAD, -and -LUA_TLIGHTUSERDATA. - - - - - -


lua_typename

-[-0, +0, –] -

const char *lua_typename (lua_State *L, int tp);
- -

-Returns the name of the type encoded by the value tp, -which must be one the values returned by lua_type. - - - - - -


lua_Unsigned

-
typedef ... lua_Unsigned;
- -

-The unsigned version of lua_Integer. - - - - - -


lua_upvalueindex

-[-0, +0, –] -

int lua_upvalueindex (int i);
- -

-Returns the pseudo-index that represents the i-th upvalue of -the running function (see §4.4). - - - - - -


lua_version

-[-0, +0, –] -

const lua_Number *lua_version (lua_State *L);
- -

-Returns the address of the version number -(a C static variable) -stored in the Lua core. -When called with a valid lua_State, -returns the address of the version used to create that state. -When called with NULL, -returns the address of the version running the call. - - - - - -


lua_Writer

-
typedef int (*lua_Writer) (lua_State *L,
-                           const void* p,
-                           size_t sz,
-                           void* ud);
- -

-The type of the writer function used by lua_dump. -Every time it produces another piece of chunk, -lua_dump calls the writer, -passing along the buffer to be written (p), -its size (sz), -and the data parameter supplied to lua_dump. - - -

-The writer returns an error code: -0 means no errors; -any other value means an error and stops lua_dump from -calling the writer again. - - - - - -


lua_xmove

-[-?, +?, –] -

void lua_xmove (lua_State *from, lua_State *to, int n);
- -

-Exchange values between different threads of the same state. - - -

-This function pops n values from the stack from, -and pushes them onto the stack to. - - - - - -


lua_yield

-[-?, +?, e] -

int lua_yield (lua_State *L, int nresults);
- -

-This function is equivalent to lua_yieldk, -but it has no continuation (see §4.7). -Therefore, when the thread resumes, -it continues the function that called -the function calling lua_yield. - - - - - -


lua_yieldk

-[-?, +?, e] -

int lua_yieldk (lua_State *L,
-                int nresults,
-                lua_KContext ctx,
-                lua_KFunction k);
- -

-Yields a coroutine (thread). - - -

-When a C function calls lua_yieldk, -the running coroutine suspends its execution, -and the call to lua_resume that started this coroutine returns. -The parameter nresults is the number of values from the stack -that will be passed as results to lua_resume. - - -

-When the coroutine is resumed again, -Lua calls the given continuation function k to continue -the execution of the C function that yielded (see §4.7). -This continuation function receives the same stack -from the previous function, -with the n results removed and -replaced by the arguments passed to lua_resume. -Moreover, -the continuation function receives the value ctx -that was passed to lua_yieldk. - - -

-Usually, this function does not return; -when the coroutine eventually resumes, -it continues executing the continuation function. -However, there is one special case, -which is when this function is called -from inside a line or a count hook (see §4.9). -In that case, lua_yieldk should be called with no continuation -(probably in the form of lua_yield) and no results, -and the hook should return immediately after the call. -Lua will yield and, -when the coroutine resumes again, -it will continue the normal execution -of the (Lua) function that triggered the hook. - - -

-This function can raise an error if it is called from a thread -with a pending C call with no continuation function, -or it is called from a thread that is not running inside a resume -(e.g., the main thread). - - - - - - - -

4.9 – The Debug Interface

- -

-Lua has no built-in debugging facilities. -Instead, it offers a special interface -by means of functions and hooks. -This interface allows the construction of different -kinds of debuggers, profilers, and other tools -that need "inside information" from the interpreter. - - - -


lua_Debug

-
typedef struct lua_Debug {
-  int event;
-  const char *name;           /* (n) */
-  const char *namewhat;       /* (n) */
-  const char *what;           /* (S) */
-  const char *source;         /* (S) */
-  int currentline;            /* (l) */
-  int linedefined;            /* (S) */
-  int lastlinedefined;        /* (S) */
-  unsigned char nups;         /* (u) number of upvalues */
-  unsigned char nparams;      /* (u) number of parameters */
-  char isvararg;              /* (u) */
-  char istailcall;            /* (t) */
-  char short_src[LUA_IDSIZE]; /* (S) */
-  /* private part */
-  other fields
-} lua_Debug;
- -

-A structure used to carry different pieces of -information about a function or an activation record. -lua_getstack fills only the private part -of this structure, for later use. -To fill the other fields of lua_Debug with useful information, -call lua_getinfo. - - -

-The fields of lua_Debug have the following meaning: - -

    - -
  • source: -the name of the chunk that created the function. -If source starts with a '@', -it means that the function was defined in a file where -the file name follows the '@'. -If source starts with a '=', -the remainder of its contents describe the source in a user-dependent manner. -Otherwise, -the function was defined in a string where -source is that string. -
  • - -
  • short_src: -a "printable" version of source, to be used in error messages. -
  • - -
  • linedefined: -the line number where the definition of the function starts. -
  • - -
  • lastlinedefined: -the line number where the definition of the function ends. -
  • - -
  • what: -the string "Lua" if the function is a Lua function, -"C" if it is a C function, -"main" if it is the main part of a chunk. -
  • - -
  • currentline: -the current line where the given function is executing. -When no line information is available, -currentline is set to -1. -
  • - -
  • name: -a reasonable name for the given function. -Because functions in Lua are first-class values, -they do not have a fixed name: -some functions can be the value of multiple global variables, -while others can be stored only in a table field. -The lua_getinfo function checks how the function was -called to find a suitable name. -If it cannot find a name, -then name is set to NULL. -
  • - -
  • namewhat: -explains the name field. -The value of namewhat can be -"global", "local", "method", -"field", "upvalue", or "" (the empty string), -according to how the function was called. -(Lua uses the empty string when no other option seems to apply.) -
  • - -
  • istailcall: -true if this function invocation was called by a tail call. -In this case, the caller of this level is not in the stack. -
  • - -
  • nups: -the number of upvalues of the function. -
  • - -
  • nparams: -the number of fixed parameters of the function -(always 0 for C functions). -
  • - -
  • isvararg: -true if the function is a vararg function -(always true for C functions). -
  • - -
- - - - -

lua_gethook

-[-0, +0, –] -

lua_Hook lua_gethook (lua_State *L);
- -

-Returns the current hook function. - - - - - -


lua_gethookcount

-[-0, +0, –] -

int lua_gethookcount (lua_State *L);
- -

-Returns the current hook count. - - - - - -


lua_gethookmask

-[-0, +0, –] -

int lua_gethookmask (lua_State *L);
- -

-Returns the current hook mask. - - - - - -


lua_getinfo

-[-(0|1), +(0|1|2), e] -

int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
- -

-Gets information about a specific function or function invocation. - - -

-To get information about a function invocation, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). - - -

-To get information about a function, you push it onto the stack -and start the what string with the character '>'. -(In that case, -lua_getinfo pops the function from the top of the stack.) -For instance, to know in which line a function f was defined, -you can write the following code: - -

-     lua_Debug ar;
-     lua_getglobal(L, "f");  /* get global 'f' */
-     lua_getinfo(L, ">S", &ar);
-     printf("%d\n", ar.linedefined);
-
- -

-Each character in the string what -selects some fields of the structure ar to be filled or -a value to be pushed on the stack: - -

    - -
  • 'n': fills in the field name and namewhat; -
  • - -
  • 'S': -fills in the fields source, short_src, -linedefined, lastlinedefined, and what; -
  • - -
  • 'l': fills in the field currentline; -
  • - -
  • 't': fills in the field istailcall; -
  • - -
  • 'u': fills in the fields -nups, nparams, and isvararg; -
  • - -
  • 'f': -pushes onto the stack the function that is -running at the given level; -
  • - -
  • 'L': -pushes onto the stack a table whose indices are the -numbers of the lines that are valid on the function. -(A valid line is a line with some associated code, -that is, a line where you can put a break point. -Non-valid lines include empty lines and comments.) - - -

    -If this option is given together with option 'f', -its table is pushed after the function. -

  • - -
- -

-This function returns 0 on error -(for instance, an invalid option in what). - - - - - -


lua_getlocal

-[-0, +(0|1), –] -

const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
- -

-Gets information about a local variable of -a given activation record or a given function. - - -

-In the first case, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). -The index n selects which local variable to inspect; -see debug.getlocal for details about variable indices -and names. - - -

-lua_getlocal pushes the variable's value onto the stack -and returns its name. - - -

-In the second case, ar must be NULL and the function -to be inspected must be at the top of the stack. -In this case, only parameters of Lua functions are visible -(as there is no information about what variables are active) -and no values are pushed onto the stack. - - -

-Returns NULL (and pushes nothing) -when the index is greater than -the number of active local variables. - - - - - -


lua_getstack

-[-0, +0, –] -

int lua_getstack (lua_State *L, int level, lua_Debug *ar);
- -

-Gets information about the interpreter runtime stack. - - -

-This function fills parts of a lua_Debug structure with -an identification of the activation record -of the function executing at a given level. -Level 0 is the current running function, -whereas level n+1 is the function that has called level n -(except for tail calls, which do not count on the stack). -When there are no errors, lua_getstack returns 1; -when called with a level greater than the stack depth, -it returns 0. - - - - - -


lua_getupvalue

-[-0, +(0|1), –] -

const char *lua_getupvalue (lua_State *L, int funcindex, int n);
- -

-Gets information about the n-th upvalue -of the closure at index funcindex. -It pushes the upvalue's value onto the stack -and returns its name. -Returns NULL (and pushes nothing) -when the index n is greater than the number of upvalues. - - -

-For C functions, this function uses the empty string "" -as a name for all upvalues. -(For Lua functions, -upvalues are the external local variables that the function uses, -and that are consequently included in its closure.) - - -

-Upvalues have no particular order, -as they are active through the whole function. -They are numbered in an arbitrary order. - - - - - -


lua_Hook

-
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
- -

-Type for debugging hook functions. - - -

-Whenever a hook is called, its ar argument has its field -event set to the specific event that triggered the hook. -Lua identifies these events with the following constants: -LUA_HOOKCALL, LUA_HOOKRET, -LUA_HOOKTAILCALL, LUA_HOOKLINE, -and LUA_HOOKCOUNT. -Moreover, for line events, the field currentline is also set. -To get the value of any other field in ar, -the hook must call lua_getinfo. - - -

-For call events, event can be LUA_HOOKCALL, -the normal value, or LUA_HOOKTAILCALL, for a tail call; -in this case, there will be no corresponding return event. - - -

-While Lua is running a hook, it disables other calls to hooks. -Therefore, if a hook calls back Lua to execute a function or a chunk, -this execution occurs without any calls to hooks. - - -

-Hook functions cannot have continuations, -that is, they cannot call lua_yieldk, -lua_pcallk, or lua_callk with a non-null k. - - -

-Hook functions can yield under the following conditions: -Only count and line events can yield; -to yield, a hook function must finish its execution -calling lua_yield with nresults equal to zero -(that is, with no values). - - - - - -


lua_sethook

-[-0, +0, –] -

void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
- -

-Sets the debugging hook function. - - -

-Argument f is the hook function. -mask specifies on which events the hook will be called: -it is formed by a bitwise OR of the constants -LUA_MASKCALL, -LUA_MASKRET, -LUA_MASKLINE, -and LUA_MASKCOUNT. -The count argument is only meaningful when the mask -includes LUA_MASKCOUNT. -For each event, the hook is called as explained below: - -

    - -
  • The call hook: is called when the interpreter calls a function. -The hook is called just after Lua enters the new function, -before the function gets its arguments. -
  • - -
  • The return hook: is called when the interpreter returns from a function. -The hook is called just before Lua leaves the function. -There is no standard way to access the values -to be returned by the function. -
  • - -
  • The line hook: is called when the interpreter is about to -start the execution of a new line of code, -or when it jumps back in the code (even to the same line). -(This event only happens while Lua is executing a Lua function.) -
  • - -
  • The count hook: is called after the interpreter executes every -count instructions. -(This event only happens while Lua is executing a Lua function.) -
  • - -
- -

-A hook is disabled by setting mask to zero. - - - - - -


lua_setlocal

-[-(0|1), +0, –] -

const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
- -

-Sets the value of a local variable of a given activation record. -It assigns the value at the top of the stack -to the variable and returns its name. -It also pops the value from the stack. - - -

-Returns NULL (and pops nothing) -when the index is greater than -the number of active local variables. - - -

-Parameters ar and n are as in function lua_getlocal. - - - - - -


lua_setupvalue

-[-(0|1), +0, –] -

const char *lua_setupvalue (lua_State *L, int funcindex, int n);
- -

-Sets the value of a closure's upvalue. -It assigns the value at the top of the stack -to the upvalue and returns its name. -It also pops the value from the stack. - - -

-Returns NULL (and pops nothing) -when the index n is greater than the number of upvalues. - - -

-Parameters funcindex and n are as in function lua_getupvalue. - - - - - -


lua_upvalueid

-[-0, +0, –] -

void *lua_upvalueid (lua_State *L, int funcindex, int n);
- -

-Returns a unique identifier for the upvalue numbered n -from the closure at index funcindex. - - -

-These unique identifiers allow a program to check whether different -closures share upvalues. -Lua closures that share an upvalue -(that is, that access a same external local variable) -will return identical ids for those upvalue indices. - - -

-Parameters funcindex and n are as in function lua_getupvalue, -but n cannot be greater than the number of upvalues. - - - - - -


lua_upvaluejoin

-[-0, +0, –] -

void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
-                                    int funcindex2, int n2);
- -

-Make the n1-th upvalue of the Lua closure at index funcindex1 -refer to the n2-th upvalue of the Lua closure at index funcindex2. - - - - - - - -

5 – The Auxiliary Library

- -

- -The auxiliary library provides several convenient functions -to interface C with Lua. -While the basic API provides the primitive functions for all -interactions between C and Lua, -the auxiliary library provides higher-level functions for some -common tasks. - - -

-All functions and types from the auxiliary library -are defined in header file lauxlib.h and -have a prefix luaL_. - - -

-All functions in the auxiliary library are built on -top of the basic API, -and so they provide nothing that cannot be done with that API. -Nevertheless, the use of the auxiliary library ensures -more consistency to your code. - - -

-Several functions in the auxiliary library use internally some -extra stack slots. -When a function in the auxiliary library uses less than five slots, -it does not check the stack size; -it simply assumes that there are enough slots. - - -

-Several functions in the auxiliary library are used to -check C function arguments. -Because the error message is formatted for arguments -(e.g., "bad argument #1"), -you should not use these functions for other stack values. - - -

-Functions called luaL_check* -always raise an error if the check is not satisfied. - - - -

5.1 – Functions and Types

- -

-Here we list all functions and types from the auxiliary library -in alphabetical order. - - - -


luaL_addchar

-[-?, +?, m] -

void luaL_addchar (luaL_Buffer *B, char c);
- -

-Adds the byte c to the buffer B -(see luaL_Buffer). - - - - - -


luaL_addlstring

-[-?, +?, m] -

void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
- -

-Adds the string pointed to by s with length l to -the buffer B -(see luaL_Buffer). -The string can contain embedded zeros. - - - - - -


luaL_addsize

-[-?, +?, –] -

void luaL_addsize (luaL_Buffer *B, size_t n);
- -

-Adds to the buffer B (see luaL_Buffer) -a string of length n previously copied to the -buffer area (see luaL_prepbuffer). - - - - - -


luaL_addstring

-[-?, +?, m] -

void luaL_addstring (luaL_Buffer *B, const char *s);
- -

-Adds the zero-terminated string pointed to by s -to the buffer B -(see luaL_Buffer). - - - - - -


luaL_addvalue

-[-1, +?, m] -

void luaL_addvalue (luaL_Buffer *B);
- -

-Adds the value at the top of the stack -to the buffer B -(see luaL_Buffer). -Pops the value. - - -

-This is the only function on string buffers that can (and must) -be called with an extra element on the stack, -which is the value to be added to the buffer. - - - - - -


luaL_argcheck

-[-0, +0, v] -

void luaL_argcheck (lua_State *L,
-                    int cond,
-                    int arg,
-                    const char *extramsg);
- -

-Checks whether cond is true. -If it is not, raises an error with a standard message (see luaL_argerror). - - - - - -


luaL_argerror

-[-0, +0, v] -

int luaL_argerror (lua_State *L, int arg, const char *extramsg);
- -

-Raises an error reporting a problem with argument arg -of the C function that called it, -using a standard message -that includes extramsg as a comment: - -

-     bad argument #arg to 'funcname' (extramsg)
-

-This function never returns. - - - - - -


luaL_Buffer

-
typedef struct luaL_Buffer luaL_Buffer;
- -

-Type for a string buffer. - - -

-A string buffer allows C code to build Lua strings piecemeal. -Its pattern of use is as follows: - -

    - -
  • First declare a variable b of type luaL_Buffer.
  • - -
  • Then initialize it with a call luaL_buffinit(L, &b).
  • - -
  • -Then add string pieces to the buffer calling any of -the luaL_add* functions. -
  • - -
  • -Finish by calling luaL_pushresult(&b). -This call leaves the final string on the top of the stack. -
  • - -
- -

-If you know beforehand the total size of the resulting string, -you can use the buffer like this: - -

    - -
  • First declare a variable b of type luaL_Buffer.
  • - -
  • Then initialize it and preallocate a space of -size sz with a call luaL_buffinitsize(L, &b, sz).
  • - -
  • Then copy the string into that space.
  • - -
  • -Finish by calling luaL_pushresultsize(&b, sz), -where sz is the total size of the resulting string -copied into that space. -
  • - -
- -

-During its normal operation, -a string buffer uses a variable number of stack slots. -So, while using a buffer, you cannot assume that you know where -the top of the stack is. -You can use the stack between successive calls to buffer operations -as long as that use is balanced; -that is, -when you call a buffer operation, -the stack is at the same level -it was immediately after the previous buffer operation. -(The only exception to this rule is luaL_addvalue.) -After calling luaL_pushresult the stack is back to its -level when the buffer was initialized, -plus the final string on its top. - - - - - -


luaL_buffinit

-[-0, +0, –] -

void luaL_buffinit (lua_State *L, luaL_Buffer *B);
- -

-Initializes a buffer B. -This function does not allocate any space; -the buffer must be declared as a variable -(see luaL_Buffer). - - - - - -


luaL_buffinitsize

-[-?, +?, m] -

char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);
- -

-Equivalent to the sequence -luaL_buffinit, luaL_prepbuffsize. - - - - - -


luaL_callmeta

-[-0, +(0|1), e] -

int luaL_callmeta (lua_State *L, int obj, const char *e);
- -

-Calls a metamethod. - - -

-If the object at index obj has a metatable and this -metatable has a field e, -this function calls this field passing the object as its only argument. -In this case this function returns true and pushes onto the -stack the value returned by the call. -If there is no metatable or no metamethod, -this function returns false (without pushing any value on the stack). - - - - - -


luaL_checkany

-[-0, +0, v] -

void luaL_checkany (lua_State *L, int arg);
- -

-Checks whether the function has an argument -of any type (including nil) at position arg. - - - - - -


luaL_checkinteger

-[-0, +0, v] -

lua_Integer luaL_checkinteger (lua_State *L, int arg);
- -

-Checks whether the function argument arg is an integer -(or can be converted to an integer) -and returns this integer cast to a lua_Integer. - - - - - -


luaL_checklstring

-[-0, +0, v] -

const char *luaL_checklstring (lua_State *L, int arg, size_t *l);
- -

-Checks whether the function argument arg is a string -and returns this string; -if l is not NULL fills *l -with the string's length. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checknumber

-[-0, +0, v] -

lua_Number luaL_checknumber (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number. - - - - - -


luaL_checkoption

-[-0, +0, v] -

int luaL_checkoption (lua_State *L,
-                      int arg,
-                      const char *def,
-                      const char *const lst[]);
- -

-Checks whether the function argument arg is a string and -searches for this string in the array lst -(which must be NULL-terminated). -Returns the index in the array where the string was found. -Raises an error if the argument is not a string or -if the string cannot be found. - - -

-If def is not NULL, -the function uses def as a default value when -there is no argument arg or when this argument is nil. - - -

-This is a useful function for mapping strings to C enums. -(The usual convention in Lua libraries is -to use strings instead of numbers to select options.) - - - - - -


luaL_checkstack

-[-0, +0, v] -

void luaL_checkstack (lua_State *L, int sz, const char *msg);
- -

-Grows the stack size to top + sz elements, -raising an error if the stack cannot grow to that size. -msg is an additional text to go into the error message -(or NULL for no additional text). - - - - - -


luaL_checkstring

-[-0, +0, v] -

const char *luaL_checkstring (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a string -and returns this string. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checktype

-[-0, +0, v] -

void luaL_checktype (lua_State *L, int arg, int t);
- -

-Checks whether the function argument arg has type t. -See lua_type for the encoding of types for t. - - - - - -


luaL_checkudata

-[-0, +0, v] -

void *luaL_checkudata (lua_State *L, int arg, const char *tname);
- -

-Checks whether the function argument arg is a userdata -of the type tname (see luaL_newmetatable) and -returns the userdata address (see lua_touserdata). - - - - - -


luaL_checkversion

-[-0, +0, v] -

void luaL_checkversion (lua_State *L);
- -

-Checks whether the core running the call, -the core that created the Lua state, -and the code making the call are all using the same version of Lua. -Also checks whether the core running the call -and the core that created the Lua state -are using the same address space. - - - - - -


luaL_dofile

-[-0, +?, e] -

int luaL_dofile (lua_State *L, const char *filename);
- -

-Loads and runs the given file. -It is defined as the following macro: - -

-     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns false if there are no errors -or true in case of errors. - - - - - -


luaL_dostring

-[-0, +?, –] -

int luaL_dostring (lua_State *L, const char *str);
- -

-Loads and runs the given string. -It is defined as the following macro: - -

-     (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns false if there are no errors -or true in case of errors. - - - - - -


luaL_error

-[-0, +0, v] -

int luaL_error (lua_State *L, const char *fmt, ...);
- -

-Raises an error. -The error message format is given by fmt -plus any extra arguments, -following the same rules of lua_pushfstring. -It also adds at the beginning of the message the file name and -the line number where the error occurred, -if this information is available. - - -

-This function never returns, -but it is an idiom to use it in C functions -as return luaL_error(args). - - - - - -


luaL_execresult

-[-0, +3, m] -

int luaL_execresult (lua_State *L, int stat);
- -

-This function produces the return values for -process-related functions in the standard library -(os.execute and io.close). - - - - - -


luaL_fileresult

-[-0, +(1|3), m] -

int luaL_fileresult (lua_State *L, int stat, const char *fname);
- -

-This function produces the return values for -file-related functions in the standard library -(io.open, os.rename, file:seek, etc.). - - - - - -


luaL_getmetafield

-[-0, +(0|1), m] -

int luaL_getmetafield (lua_State *L, int obj, const char *e);
- -

-Pushes onto the stack the field e from the metatable -of the object at index obj and returns the type of the pushed value. -If the object does not have a metatable, -or if the metatable does not have this field, -pushes nothing and returns LUA_TNIL. - - - - - -


luaL_getmetatable

-[-0, +1, m] -

int luaL_getmetatable (lua_State *L, const char *tname);
- -

-Pushes onto the stack the metatable associated with name tname -in the registry (see luaL_newmetatable) -(nil if there is no metatable associated with that name). -Returns the type of the pushed value. - - - - - -


luaL_getsubtable

-[-0, +1, e] -

int luaL_getsubtable (lua_State *L, int idx, const char *fname);
- -

-Ensures that the value t[fname], -where t is the value at index idx, -is a table, -and pushes that table onto the stack. -Returns true if it finds a previous table there -and false if it creates a new table. - - - - - -


luaL_gsub

-[-0, +1, m] -

const char *luaL_gsub (lua_State *L,
-                       const char *s,
-                       const char *p,
-                       const char *r);
- -

-Creates a copy of string s by replacing -any occurrence of the string p -with the string r. -Pushes the resulting string on the stack and returns it. - - - - - -


luaL_len

-[-0, +0, e] -

lua_Integer luaL_len (lua_State *L, int index);
- -

-Returns the "length" of the value at the given index -as a number; -it is equivalent to the '#' operator in Lua (see §3.4.7). -Raises an error if the result of the operation is not an integer. -(This case only can happen through metamethods.) - - - - - -


luaL_loadbuffer

-[-0, +1, –] -

int luaL_loadbuffer (lua_State *L,
-                     const char *buff,
-                     size_t sz,
-                     const char *name);
- -

-Equivalent to luaL_loadbufferx with mode equal to NULL. - - - - - -


luaL_loadbufferx

-[-0, +1, –] -

int luaL_loadbufferx (lua_State *L,
-                      const char *buff,
-                      size_t sz,
-                      const char *name,
-                      const char *mode);
- -

-Loads a buffer as a Lua chunk. -This function uses lua_load to load the chunk in the -buffer pointed to by buff with size sz. - - -

-This function returns the same results as lua_load. -name is the chunk name, -used for debug information and error messages. -The string mode works as in function lua_load. - - - - - -


luaL_loadfile

-[-0, +1, m] -

int luaL_loadfile (lua_State *L, const char *filename);
- -

-Equivalent to luaL_loadfilex with mode equal to NULL. - - - - - -


luaL_loadfilex

-[-0, +1, m] -

int luaL_loadfilex (lua_State *L, const char *filename,
-                                            const char *mode);
- -

-Loads a file as a Lua chunk. -This function uses lua_load to load the chunk in the file -named filename. -If filename is NULL, -then it loads from the standard input. -The first line in the file is ignored if it starts with a #. - - -

-The string mode works as in function lua_load. - - -

-This function returns the same results as lua_load, -but it has an extra error code LUA_ERRFILE -for file-related errors -(e.g., it cannot open or read the file). - - -

-As lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_loadstring

-[-0, +1, –] -

int luaL_loadstring (lua_State *L, const char *s);
- -

-Loads a string as a Lua chunk. -This function uses lua_load to load the chunk in -the zero-terminated string s. - - -

-This function returns the same results as lua_load. - - -

-Also as lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_newlib

-[-0, +1, m] -

void luaL_newlib (lua_State *L, const luaL_Reg l[]);
- -

-Creates a new table and registers there -the functions in list l. - - -

-It is implemented as the following macro: - -

-     (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
-

-The array l must be the actual array, -not a pointer to it. - - - - - -


luaL_newlibtable

-[-0, +1, m] -

void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);
- -

-Creates a new table with a size optimized -to store all entries in the array l -(but does not actually store them). -It is intended to be used in conjunction with luaL_setfuncs -(see luaL_newlib). - - -

-It is implemented as a macro. -The array l must be the actual array, -not a pointer to it. - - - - - -


luaL_newmetatable

-[-0, +1, m] -

int luaL_newmetatable (lua_State *L, const char *tname);
- -

-If the registry already has the key tname, -returns 0. -Otherwise, -creates a new table to be used as a metatable for userdata, -adds to this new table the pair __name = tname, -adds to the registry the pair [tname] = new table, -and returns 1. -(The entry __name is used by some error-reporting functions.) - - -

-In both cases pushes onto the stack the final value associated -with tname in the registry. - - - - - -


luaL_newstate

-[-0, +0, –] -

lua_State *luaL_newstate (void);
- -

-Creates a new Lua state. -It calls lua_newstate with an -allocator based on the standard C realloc function -and then sets a panic function (see §4.6) that prints -an error message to the standard error output in case of fatal -errors. - - -

-Returns the new state, -or NULL if there is a memory allocation error. - - - - - -


luaL_openlibs

-[-0, +0, e] -

void luaL_openlibs (lua_State *L);
- -

-Opens all standard Lua libraries into the given state. - - - - - -


luaL_opt

-[-0, +0, e] -

T luaL_opt (L, func, arg, dflt);
- -

-This macro is defined as follows: - -

-     (lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg)))
-

-In words, if the argument arg is nil or absent, -the macro results in the default dflt. -Otherwise, it results in the result of calling func -with the state L and the argument index arg as -arguments. -Note that it evaluates the expression dflt only if needed. - - - - - -


luaL_optinteger

-[-0, +0, v] -

lua_Integer luaL_optinteger (lua_State *L,
-                             int arg,
-                             lua_Integer d);
- -

-If the function argument arg is an integer -(or convertible to an integer), -returns this integer. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optlstring

-[-0, +0, v] -

const char *luaL_optlstring (lua_State *L,
-                             int arg,
-                             const char *d,
-                             size_t *l);
- -

-If the function argument arg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - -

-If l is not NULL, -fills the position *l with the result's length. -If the result is NULL -(only possible when returning d and d == NULL), -its length is considered zero. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_optnumber

-[-0, +0, v] -

lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);
- -

-If the function argument arg is a number, -returns this number. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optstring

-[-0, +0, v] -

const char *luaL_optstring (lua_State *L,
-                            int arg,
-                            const char *d);
- -

-If the function argument arg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_prepbuffer

-[-?, +?, m] -

char *luaL_prepbuffer (luaL_Buffer *B);
- -

-Equivalent to luaL_prepbuffsize -with the predefined size LUAL_BUFFERSIZE. - - - - - -


luaL_prepbuffsize

-[-?, +?, m] -

char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);
- -

-Returns an address to a space of size sz -where you can copy a string to be added to buffer B -(see luaL_Buffer). -After copying the string into this space you must call -luaL_addsize with the size of the string to actually add -it to the buffer. - - - - - -


luaL_pushresult

-[-?, +1, m] -

void luaL_pushresult (luaL_Buffer *B);
- -

-Finishes the use of buffer B leaving the final string on -the top of the stack. - - - - - -


luaL_pushresultsize

-[-?, +1, m] -

void luaL_pushresultsize (luaL_Buffer *B, size_t sz);
- -

-Equivalent to the sequence luaL_addsize, luaL_pushresult. - - - - - -


luaL_ref

-[-1, +0, m] -

int luaL_ref (lua_State *L, int t);
- -

-Creates and returns a reference, -in the table at index t, -for the object at the top of the stack (and pops the object). - - -

-A reference is a unique integer key. -As long as you do not manually add integer keys into table t, -luaL_ref ensures the uniqueness of the key it returns. -You can retrieve an object referred by reference r -by calling lua_rawgeti(L, t, r). -Function luaL_unref frees a reference and its associated object. - - -

-If the object at the top of the stack is nil, -luaL_ref returns the constant LUA_REFNIL. -The constant LUA_NOREF is guaranteed to be different -from any reference returned by luaL_ref. - - - - - -


luaL_Reg

-
typedef struct luaL_Reg {
-  const char *name;
-  lua_CFunction func;
-} luaL_Reg;
- -

-Type for arrays of functions to be registered by -luaL_setfuncs. -name is the function name and func is a pointer to -the function. -Any array of luaL_Reg must end with a sentinel entry -in which both name and func are NULL. - - - - - -


luaL_requiref

-[-0, +1, e] -

void luaL_requiref (lua_State *L, const char *modname,
-                    lua_CFunction openf, int glb);
- -

-If modname is not already present in package.loaded, -calls function openf with string modname as an argument -and sets the call result in package.loaded[modname], -as if that function has been called through require. - - -

-If glb is true, -also stores the module into global modname. - - -

-Leaves a copy of the module on the stack. - - - - - -


luaL_setfuncs

-[-nup, +0, m] -

void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
- -

-Registers all functions in the array l -(see luaL_Reg) into the table on the top of the stack -(below optional upvalues, see next). - - -

-When nup is not zero, -all functions are created sharing nup upvalues, -which must be previously pushed on the stack -on top of the library table. -These values are popped from the stack after the registration. - - - - - -


luaL_setmetatable

-[-0, +0, –] -

void luaL_setmetatable (lua_State *L, const char *tname);
- -

-Sets the metatable of the object at the top of the stack -as the metatable associated with name tname -in the registry (see luaL_newmetatable). - - - - - -


luaL_Stream

-
typedef struct luaL_Stream {
-  FILE *f;
-  lua_CFunction closef;
-} luaL_Stream;
- -

-The standard representation for file handles, -which is used by the standard I/O library. - - -

-A file handle is implemented as a full userdata, -with a metatable called LUA_FILEHANDLE -(where LUA_FILEHANDLE is a macro with the actual metatable's name). -The metatable is created by the I/O library -(see luaL_newmetatable). - - -

-This userdata must start with the structure luaL_Stream; -it can contain other data after this initial structure. -Field f points to the corresponding C stream -(or it can be NULL to indicate an incompletely created handle). -Field closef points to a Lua function -that will be called to close the stream -when the handle is closed or collected; -this function receives the file handle as its sole argument and -must return either true (in case of success) -or nil plus an error message (in case of error). -Once Lua calls this field, -it changes the field value to NULL -to signal that the handle is closed. - - - - - -


luaL_testudata

-[-0, +0, m] -

void *luaL_testudata (lua_State *L, int arg, const char *tname);
- -

-This function works like luaL_checkudata, -except that, when the test fails, -it returns NULL instead of raising an error. - - - - - -


luaL_tolstring

-[-0, +1, e] -

const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
- -

-Converts any Lua value at the given index to a C string -in a reasonable format. -The resulting string is pushed onto the stack and also -returned by the function. -If len is not NULL, -the function also sets *len with the string length. - - -

-If the value has a metatable with a __tostring field, -then luaL_tolstring calls the corresponding metamethod -with the value as argument, -and uses the result of the call as its result. - - - - - -


luaL_traceback

-[-0, +1, m] -

void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
-                     int level);
- -

-Creates and pushes a traceback of the stack L1. -If msg is not NULL it is appended -at the beginning of the traceback. -The level parameter tells at which level -to start the traceback. - - - - - -


luaL_typename

-[-0, +0, –] -

const char *luaL_typename (lua_State *L, int index);
- -

-Returns the name of the type of the value at the given index. - - - - - -


luaL_unref

-[-0, +0, –] -

void luaL_unref (lua_State *L, int t, int ref);
- -

-Releases reference ref from the table at index t -(see luaL_ref). -The entry is removed from the table, -so that the referred object can be collected. -The reference ref is also freed to be used again. - - -

-If ref is LUA_NOREF or LUA_REFNIL, -luaL_unref does nothing. - - - - - -


luaL_where

-[-0, +1, m] -

void luaL_where (lua_State *L, int lvl);
- -

-Pushes onto the stack a string identifying the current position -of the control at level lvl in the call stack. -Typically this string has the following format: - -

-     chunkname:currentline:
-

-Level 0 is the running function, -level 1 is the function that called the running function, -etc. - - -

-This function is used to build a prefix for error messages. - - - - - - - -

6 – Standard Libraries

- -

-The standard Lua libraries provide useful functions -that are implemented directly through the C API. -Some of these functions provide essential services to the language -(e.g., type and getmetatable); -others provide access to "outside" services (e.g., I/O); -and others could be implemented in Lua itself, -but are quite useful or have critical performance requirements that -deserve an implementation in C (e.g., table.sort). - - -

-All libraries are implemented through the official C API -and are provided as separate C modules. -Currently, Lua has the following standard libraries: - -

    - -
  • basic library (§6.1);
  • - -
  • coroutine library (§6.2);
  • - -
  • package library (§6.3);
  • - -
  • string manipulation (§6.4);
  • - -
  • basic UTF-8 support (§6.5);
  • - -
  • table manipulation (§6.6);
  • - -
  • mathematical functions (§6.7) (sin, log, etc.);
  • - -
  • input and output (§6.8);
  • - -
  • operating system facilities (§6.9);
  • - -
  • debug facilities (§6.10).
  • - -

-Except for the basic and the package libraries, -each library provides all its functions as fields of a global table -or as methods of its objects. - - -

-To have access to these libraries, -the C host program should call the luaL_openlibs function, -which opens all standard libraries. -Alternatively, -the host program can open them individually by using -luaL_requiref to call -luaopen_base (for the basic library), -luaopen_package (for the package library), -luaopen_coroutine (for the coroutine library), -luaopen_string (for the string library), -luaopen_utf8 (for the UTF8 library), -luaopen_table (for the table library), -luaopen_math (for the mathematical library), -luaopen_io (for the I/O library), -luaopen_os (for the operating system library), -and luaopen_debug (for the debug library). -These functions are declared in lualib.h. - - - -

6.1 – Basic Functions

- -

-The basic library provides core functions to Lua. -If you do not include this library in your application, -you should check carefully whether you need to provide -implementations for some of its facilities. - - -

-


assert (v [, message])

- - -

-Calls error if -the value of its argument v is false (i.e., nil or false); -otherwise, returns all its arguments. -In case of error, -message is the error object; -when absent, it defaults to "assertion failed!" - - - - -

-


collectgarbage ([opt [, arg]])

- - -

-This function is a generic interface to the garbage collector. -It performs different functions according to its first argument, opt: - -

    - -
  • "collect": -performs a full garbage-collection cycle. -This is the default option. -
  • - -
  • "stop": -stops automatic execution of the garbage collector. -The collector will run only when explicitly invoked, -until a call to restart it. -
  • - -
  • "restart": -restarts automatic execution of the garbage collector. -
  • - -
  • "count": -returns the total memory in use by Lua in Kbytes. -The value has a fractional part, -so that it multiplied by 1024 -gives the exact number of bytes in use by Lua -(except for overflows). -
  • - -
  • "step": -performs a garbage-collection step. -The step "size" is controlled by arg. -With a zero value, -the collector will perform one basic (indivisible) step. -For non-zero values, -the collector will perform as if that amount of memory -(in KBytes) had been allocated by Lua. -Returns true if the step finished a collection cycle. -
  • - -
  • "setpause": -sets arg as the new value for the pause of -the collector (see §2.5). -Returns the previous value for pause. -
  • - -
  • "setstepmul": -sets arg as the new value for the step multiplier of -the collector (see §2.5). -Returns the previous value for step. -
  • - -
  • "isrunning": -returns a boolean that tells whether the collector is running -(i.e., not stopped). -
  • - -
- - - -

-


dofile ([filename])

-Opens the named file and executes its contents as a Lua chunk. -When called without arguments, -dofile executes the contents of the standard input (stdin). -Returns all values returned by the chunk. -In case of errors, dofile propagates the error -to its caller (that is, dofile does not run in protected mode). - - - - -

-


error (message [, level])

-Terminates the last protected function called -and returns message as the error object. -Function error never returns. - - -

-Usually, error adds some information about the error position -at the beginning of the message, if the message is a string. -The level argument specifies how to get the error position. -With level 1 (the default), the error position is where the -error function was called. -Level 2 points the error to where the function -that called error was called; and so on. -Passing a level 0 avoids the addition of error position information -to the message. - - - - -

-


_G

-A global variable (not a function) that -holds the global environment (see §2.2). -Lua itself does not use this variable; -changing its value does not affect any environment, -nor vice versa. - - - - -

-


getmetatable (object)

- - -

-If object does not have a metatable, returns nil. -Otherwise, -if the object's metatable has a __metatable field, -returns the associated value. -Otherwise, returns the metatable of the given object. - - - - -

-


ipairs (t)

- - -

-Returns three values (an iterator function, the table t, and 0) -so that the construction - -

-     for i,v in ipairs(t) do body end
-

-will iterate over the key–value pairs -(1,t[1]), (2,t[2]), ..., -up to the first nil value. - - - - -

-


load (chunk [, chunkname [, mode [, env]]])

- - -

-Loads a chunk. - - -

-If chunk is a string, the chunk is this string. -If chunk is a function, -load calls it repeatedly to get the chunk pieces. -Each call to chunk must return a string that concatenates -with previous results. -A return of an empty string, nil, or no value signals the end of the chunk. - - -

-If there are no syntactic errors, -returns the compiled chunk as a function; -otherwise, returns nil plus the error message. - - -

-If the resulting function has upvalues, -the first upvalue is set to the value of env, -if that parameter is given, -or to the value of the global environment. -Other upvalues are initialized with nil. -(When you load a main chunk, -the resulting function will always have exactly one upvalue, -the _ENV variable (see §2.2). -However, -when you load a binary chunk created from a function (see string.dump), -the resulting function can have an arbitrary number of upvalues.) -All upvalues are fresh, that is, -they are not shared with any other function. - - -

-chunkname is used as the name of the chunk for error messages -and debug information (see §4.9). -When absent, -it defaults to chunk, if chunk is a string, -or to "=(load)" otherwise. - - -

-The string mode controls whether the chunk can be text or binary -(that is, a precompiled chunk). -It may be the string "b" (only binary chunks), -"t" (only text chunks), -or "bt" (both binary and text). -The default is "bt". - - -

-Lua does not check the consistency of binary chunks. -Maliciously crafted binary chunks can crash -the interpreter. - - - - -

-


loadfile ([filename [, mode [, env]]])

- - -

-Similar to load, -but gets the chunk from file filename -or from the standard input, -if no file name is given. - - - - -

-


next (table [, index])

- - -

-Allows a program to traverse all fields of a table. -Its first argument is a table and its second argument -is an index in this table. -next returns the next index of the table -and its associated value. -When called with nil as its second argument, -next returns an initial index -and its associated value. -When called with the last index, -or with nil in an empty table, -next returns nil. -If the second argument is absent, then it is interpreted as nil. -In particular, -you can use next(t) to check whether a table is empty. - - -

-The order in which the indices are enumerated is not specified, -even for numeric indices. -(To traverse a table in numerical order, -use a numerical for.) - - -

-The behavior of next is undefined if, -during the traversal, -you assign any value to a non-existent field in the table. -You may however modify existing fields. -In particular, you may clear existing fields. - - - - -

-


pairs (t)

- - -

-If t has a metamethod __pairs, -calls it with t as argument and returns the first three -results from the call. - - -

-Otherwise, -returns three values: the next function, the table t, and nil, -so that the construction - -

-     for k,v in pairs(t) do body end
-

-will iterate over all key–value pairs of table t. - - -

-See function next for the caveats of modifying -the table during its traversal. - - - - -

-


pcall (f [, arg1, ···])

- - -

-Calls function f with -the given arguments in protected mode. -This means that any error inside f is not propagated; -instead, pcall catches the error -and returns a status code. -Its first result is the status code (a boolean), -which is true if the call succeeds without errors. -In such case, pcall also returns all results from the call, -after this first result. -In case of any error, pcall returns false plus the error message. - - - - -

-


print (···)

-Receives any number of arguments -and prints their values to stdout, -using the tostring function to convert each argument to a string. -print is not intended for formatted output, -but only as a quick way to show a value, -for instance for debugging. -For complete control over the output, -use string.format and io.write. - - - - -

-


rawequal (v1, v2)

-Checks whether v1 is equal to v2, -without invoking the __eq metamethod. -Returns a boolean. - - - - -

-


rawget (table, index)

-Gets the real value of table[index], -without invoking the __index metamethod. -table must be a table; -index may be any value. - - - - -

-


rawlen (v)

-Returns the length of the object v, -which must be a table or a string, -without invoking the __len metamethod. -Returns an integer. - - - - -

-


rawset (table, index, value)

-Sets the real value of table[index] to value, -without invoking the __newindex metamethod. -table must be a table, -index any value different from nil and NaN, -and value any Lua value. - - -

-This function returns table. - - - - -

-


select (index, ···)

- - -

-If index is a number, -returns all arguments after argument number index; -a negative number indexes from the end (-1 is the last argument). -Otherwise, index must be the string "#", -and select returns the total number of extra arguments it received. - - - - -

-


setmetatable (table, metatable)

- - -

-Sets the metatable for the given table. -(To change the metatable of other types from Lua code, -you must use the debug library (§6.10).) -If metatable is nil, -removes the metatable of the given table. -If the original metatable has a __metatable field, -raises an error. - - -

-This function returns table. - - - - -

-


tonumber (e [, base])

- - -

-When called with no base, -tonumber tries to convert its argument to a number. -If the argument is already a number or -a string convertible to a number, -then tonumber returns this number; -otherwise, it returns nil. - - -

-The conversion of strings can result in integers or floats, -according to the lexical conventions of Lua (see §3.1). -(The string may have leading and trailing spaces and a sign.) - - -

-When called with base, -then e must be a string to be interpreted as -an integer numeral in that base. -The base may be any integer between 2 and 36, inclusive. -In bases above 10, the letter 'A' (in either upper or lower case) -represents 10, 'B' represents 11, and so forth, -with 'Z' representing 35. -If the string e is not a valid numeral in the given base, -the function returns nil. - - - - -

-


tostring (v)

-Receives a value of any type and -converts it to a string in a human-readable format. -(For complete control of how numbers are converted, -use string.format.) - - -

-If the metatable of v has a __tostring field, -then tostring calls the corresponding value -with v as argument, -and uses the result of the call as its result. - - - - -

-


type (v)

-Returns the type of its only argument, coded as a string. -The possible results of this function are -"nil" (a string, not the value nil), -"number", -"string", -"boolean", -"table", -"function", -"thread", -and "userdata". - - - - -

-


_VERSION

- - -

-A global variable (not a function) that -holds a string containing the running Lua version. -The current value of this variable is "Lua 5.3". - - - - -

-


xpcall (f, msgh [, arg1, ···])

- - -

-This function is similar to pcall, -except that it sets a new message handler msgh. - - - - - - - -

6.2 – Coroutine Manipulation

- -

-This library comprises the operations to manipulate coroutines, -which come inside the table coroutine. -See §2.6 for a general description of coroutines. - - -

-


coroutine.create (f)

- - -

-Creates a new coroutine, with body f. -f must be a function. -Returns this new coroutine, -an object with type "thread". - - - - -

-


coroutine.isyieldable ()

- - -

-Returns true when the running coroutine can yield. - - -

-A running coroutine is yieldable if it is not the main thread and -it is not inside a non-yieldable C function. - - - - -

-


coroutine.resume (co [, val1, ···])

- - -

-Starts or continues the execution of coroutine co. -The first time you resume a coroutine, -it starts running its body. -The values val1, ... are passed -as the arguments to the body function. -If the coroutine has yielded, -resume restarts it; -the values val1, ... are passed -as the results from the yield. - - -

-If the coroutine runs without any errors, -resume returns true plus any values passed to yield -(when the coroutine yields) or any values returned by the body function -(when the coroutine terminates). -If there is any error, -resume returns false plus the error message. - - - - -

-


coroutine.running ()

- - -

-Returns the running coroutine plus a boolean, -true when the running coroutine is the main one. - - - - -

-


coroutine.status (co)

- - -

-Returns the status of coroutine co, as a string: -"running", -if the coroutine is running (that is, it called status); -"suspended", if the coroutine is suspended in a call to yield, -or if it has not started running yet; -"normal" if the coroutine is active but not running -(that is, it has resumed another coroutine); -and "dead" if the coroutine has finished its body function, -or if it has stopped with an error. - - - - -

-


coroutine.wrap (f)

- - -

-Creates a new coroutine, with body f. -f must be a function. -Returns a function that resumes the coroutine each time it is called. -Any arguments passed to the function behave as the -extra arguments to resume. -Returns the same values returned by resume, -except the first boolean. -In case of error, propagates the error. - - - - -

-


coroutine.yield (···)

- - -

-Suspends the execution of the calling coroutine. -Any arguments to yield are passed as extra results to resume. - - - - - - - -

6.3 – Modules

- -

-The package library provides basic -facilities for loading modules in Lua. -It exports one function directly in the global environment: -require. -Everything else is exported in a table package. - - -

-


require (modname)

- - -

-Loads the given module. -The function starts by looking into the package.loaded table -to determine whether modname is already loaded. -If it is, then require returns the value stored -at package.loaded[modname]. -Otherwise, it tries to find a loader for the module. - - -

-To find a loader, -require is guided by the package.searchers sequence. -By changing this sequence, -we can change how require looks for a module. -The following explanation is based on the default configuration -for package.searchers. - - -

-First require queries package.preload[modname]. -If it has a value, -this value (which must be a function) is the loader. -Otherwise require searches for a Lua loader using the -path stored in package.path. -If that also fails, it searches for a C loader using the -path stored in package.cpath. -If that also fails, -it tries an all-in-one loader (see package.searchers). - - -

-Once a loader is found, -require calls the loader with two arguments: -modname and an extra value dependent on how it got the loader. -(If the loader came from a file, -this extra value is the file name.) -If the loader returns any non-nil value, -require assigns the returned value to package.loaded[modname]. -If the loader does not return a non-nil value and -has not assigned any value to package.loaded[modname], -then require assigns true to this entry. -In any case, require returns the -final value of package.loaded[modname]. - - -

-If there is any error loading or running the module, -or if it cannot find any loader for the module, -then require raises an error. - - - - -

-


package.config

- - -

-A string describing some compile-time configurations for packages. -This string is a sequence of lines: - -

    - -
  • The first line is the directory separator string. -Default is '\' for Windows and '/' for all other systems.
  • - -
  • The second line is the character that separates templates in a path. -Default is ';'.
  • - -
  • The third line is the string that marks the -substitution points in a template. -Default is '?'.
  • - -
  • The fourth line is a string that, in a path in Windows, -is replaced by the executable's directory. -Default is '!'.
  • - -
  • The fifth line is a mark to ignore all text after it -when building the luaopen_ function name. -Default is '-'.
  • - -
- - - -

-


package.cpath

- - -

-The path used by require to search for a C loader. - - -

-Lua initializes the C path package.cpath in the same way -it initializes the Lua path package.path, -using the environment variable LUA_CPATH_5_3, -or the environment variable LUA_CPATH, -or a default path defined in luaconf.h. - - - - -

-


package.loaded

- - -

-A table used by require to control which -modules are already loaded. -When you require a module modname and -package.loaded[modname] is not false, -require simply returns the value stored there. - - -

-This variable is only a reference to the real table; -assignments to this variable do not change the -table used by require. - - - - -

-


package.loadlib (libname, funcname)

- - -

-Dynamically links the host program with the C library libname. - - -

-If funcname is "*", -then it only links with the library, -making the symbols exported by the library -available to other dynamically linked libraries. -Otherwise, -it looks for a function funcname inside the library -and returns this function as a C function. -So, funcname must follow the lua_CFunction prototype -(see lua_CFunction). - - -

-This is a low-level function. -It completely bypasses the package and module system. -Unlike require, -it does not perform any path searching and -does not automatically adds extensions. -libname must be the complete file name of the C library, -including if necessary a path and an extension. -funcname must be the exact name exported by the C library -(which may depend on the C compiler and linker used). - - -

-This function is not supported by Standard C. -As such, it is only available on some platforms -(Windows, Linux, Mac OS X, Solaris, BSD, -plus other Unix systems that support the dlfcn standard). - - - - -

-


package.path

- - -

-The path used by require to search for a Lua loader. - - -

-At start-up, Lua initializes this variable with -the value of the environment variable LUA_PATH_5_3 or -the environment variable LUA_PATH or -with a default path defined in luaconf.h, -if those environment variables are not defined. -Any ";;" in the value of the environment variable -is replaced by the default path. - - - - -

-


package.preload

- - -

-A table to store loaders for specific modules -(see require). - - -

-This variable is only a reference to the real table; -assignments to this variable do not change the -table used by require. - - - - -

-


package.searchers

- - -

-A table used by require to control how to load modules. - - -

-Each entry in this table is a searcher function. -When looking for a module, -require calls each of these searchers in ascending order, -with the module name (the argument given to require) as its -sole parameter. -The function can return another function (the module loader) -plus an extra value that will be passed to that loader, -or a string explaining why it did not find that module -(or nil if it has nothing to say). - - -

-Lua initializes this table with four searcher functions. - - -

-The first searcher simply looks for a loader in the -package.preload table. - - -

-The second searcher looks for a loader as a Lua library, -using the path stored at package.path. -The search is done as described in function package.searchpath. - - -

-The third searcher looks for a loader as a C library, -using the path given by the variable package.cpath. -Again, -the search is done as described in function package.searchpath. -For instance, -if the C path is the string - -

-     "./?.so;./?.dll;/usr/local/?/init.so"
-

-the searcher for module foo -will try to open the files ./foo.so, ./foo.dll, -and /usr/local/foo/init.so, in that order. -Once it finds a C library, -this searcher first uses a dynamic link facility to link the -application with the library. -Then it tries to find a C function inside the library to -be used as the loader. -The name of this C function is the string "luaopen_" -concatenated with a copy of the module name where each dot -is replaced by an underscore. -Moreover, if the module name has a hyphen, -its suffix after (and including) the first hyphen is removed. -For instance, if the module name is a.b.c-v2.1, -the function name will be luaopen_a_b_c. - - -

-The fourth searcher tries an all-in-one loader. -It searches the C path for a library for -the root name of the given module. -For instance, when requiring a.b.c, -it will search for a C library for a. -If found, it looks into it for an open function for -the submodule; -in our example, that would be luaopen_a_b_c. -With this facility, a package can pack several C submodules -into one single library, -with each submodule keeping its original open function. - - -

-All searchers except the first one (preload) return as the extra value -the file name where the module was found, -as returned by package.searchpath. -The first searcher returns no extra value. - - - - -

-


package.searchpath (name, path [, sep [, rep]])

- - -

-Searches for the given name in the given path. - - -

-A path is a string containing a sequence of -templates separated by semicolons. -For each template, -the function replaces each interrogation mark (if any) -in the template with a copy of name -wherein all occurrences of sep -(a dot, by default) -were replaced by rep -(the system's directory separator, by default), -and then tries to open the resulting file name. - - -

-For instance, if the path is the string - -

-     "./?.lua;./?.lc;/usr/local/?/init.lua"
-

-the search for the name foo.a -will try to open the files -./foo/a.lua, ./foo/a.lc, and -/usr/local/foo/a/init.lua, in that order. - - -

-Returns the resulting name of the first file that it can -open in read mode (after closing the file), -or nil plus an error message if none succeeds. -(This error message lists all file names it tried to open.) - - - - - - - -

6.4 – String Manipulation

- -

-This library provides generic functions for string manipulation, -such as finding and extracting substrings, and pattern matching. -When indexing a string in Lua, the first character is at position 1 -(not at 0, as in C). -Indices are allowed to be negative and are interpreted as indexing backwards, -from the end of the string. -Thus, the last character is at position -1, and so on. - - -

-The string library provides all its functions inside the table -string. -It also sets a metatable for strings -where the __index field points to the string table. -Therefore, you can use the string functions in object-oriented style. -For instance, string.byte(s,i) -can be written as s:byte(i). - - -

-The string library assumes one-byte character encodings. - - -

-


string.byte (s [, i [, j]])

-Returns the internal numeric codes of the characters s[i], -s[i+1], ..., s[j]. -The default value for i is 1; -the default value for j is i. -These indices are corrected -following the same rules of function string.sub. - - -

-Numeric codes are not necessarily portable across platforms. - - - - -

-


string.char (···)

-Receives zero or more integers. -Returns a string with length equal to the number of arguments, -in which each character has the internal numeric code equal -to its corresponding argument. - - -

-Numeric codes are not necessarily portable across platforms. - - - - -

-


string.dump (function [, strip])

- - -

-Returns a string containing a binary representation -(a binary chunk) -of the given function, -so that a later load on this string returns -a copy of the function (but with new upvalues). -If strip is a true value, -the binary representation may not include all debug information -about the function, -to save space. - - -

-Functions with upvalues have only their number of upvalues saved. -When (re)loaded, -those upvalues receive fresh instances containing nil. -(You can use the debug library to serialize -and reload the upvalues of a function -in a way adequate to your needs.) - - - - -

-


string.find (s, pattern [, init [, plain]])

- - -

-Looks for the first match of -pattern (see §6.4.1) in the string s. -If it finds a match, then find returns the indices of s -where this occurrence starts and ends; -otherwise, it returns nil. -A third, optional numeric argument init specifies -where to start the search; -its default value is 1 and can be negative. -A value of true as a fourth, optional argument plain -turns off the pattern matching facilities, -so the function does a plain "find substring" operation, -with no characters in pattern being considered magic. -Note that if plain is given, then init must be given as well. - - -

-If the pattern has captures, -then in a successful match -the captured values are also returned, -after the two indices. - - - - -

-


string.format (formatstring, ···)

- - -

-Returns a formatted version of its variable number of arguments -following the description given in its first argument (which must be a string). -The format string follows the same rules as the ISO C function sprintf. -The only differences are that the options/modifiers -*, h, L, l, n, -and p are not supported -and that there is an extra option, q. - - -

-The q option formats a string between double quotes, -using escape sequences when necessary to ensure that -it can safely be read back by the Lua interpreter. -For instance, the call - -

-     string.format('%q', 'a string with "quotes" and \n new line')
-

-may produce the string: - -

-     "a string with \"quotes\" and \
-      new line"
-
- -

-Options -A, a, E, e, f, -G, and g all expect a number as argument. -Options c, d, -i, o, u, X, and x -expect an integer. -When Lua is compiled with a C89 compiler, -options A and a (hexadecimal floats) -do not support any modifier (flags, width, length). - - -

-Option s expects a string; -if its argument is not a string, -it is converted to one following the same rules of tostring. -If the option has any modifier (flags, width, length), -the string argument should not contain embedded zeros. - - - - -

-


string.gmatch (s, pattern)

-Returns an iterator function that, -each time it is called, -returns the next captures from pattern (see §6.4.1) -over the string s. -If pattern specifies no captures, -then the whole match is produced in each call. - - -

-As an example, the following loop -will iterate over all the words from string s, -printing one per line: - -

-     s = "hello world from Lua"
-     for w in string.gmatch(s, "%a+") do
-       print(w)
-     end
-

-The next example collects all pairs key=value from the -given string into a table: - -

-     t = {}
-     s = "from=world, to=Lua"
-     for k, v in string.gmatch(s, "(%w+)=(%w+)") do
-       t[k] = v
-     end
-
- -

-For this function, a caret '^' at the start of a pattern does not -work as an anchor, as this would prevent the iteration. - - - - -

-


string.gsub (s, pattern, repl [, n])

-Returns a copy of s -in which all (or the first n, if given) -occurrences of the pattern (see §6.4.1) have been -replaced by a replacement string specified by repl, -which can be a string, a table, or a function. -gsub also returns, as its second value, -the total number of matches that occurred. -The name gsub comes from Global SUBstitution. - - -

-If repl is a string, then its value is used for replacement. -The character % works as an escape character: -any sequence in repl of the form %d, -with d between 1 and 9, -stands for the value of the d-th captured substring. -The sequence %0 stands for the whole match. -The sequence %% stands for a single %. - - -

-If repl is a table, then the table is queried for every match, -using the first capture as the key. - - -

-If repl is a function, then this function is called every time a -match occurs, with all captured substrings passed as arguments, -in order. - - -

-In any case, -if the pattern specifies no captures, -then it behaves as if the whole pattern was inside a capture. - - -

-If the value returned by the table query or by the function call -is a string or a number, -then it is used as the replacement string; -otherwise, if it is false or nil, -then there is no replacement -(that is, the original match is kept in the string). - - -

-Here are some examples: - -

-     x = string.gsub("hello world", "(%w+)", "%1 %1")
-     --> x="hello hello world world"
-     
-     x = string.gsub("hello world", "%w+", "%0 %0", 1)
-     --> x="hello hello world"
-     
-     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
-     --> x="world hello Lua from"
-     
-     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
-     --> x="home = /home/roberto, user = roberto"
-     
-     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
-           return load(s)()
-         end)
-     --> x="4+5 = 9"
-     
-     local t = {name="lua", version="5.3"}
-     x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
-     --> x="lua-5.3.tar.gz"
-
- - - -

-


string.len (s)

-Receives a string and returns its length. -The empty string "" has length 0. -Embedded zeros are counted, -so "a\000bc\000" has length 5. - - - - -

-


string.lower (s)

-Receives a string and returns a copy of this string with all -uppercase letters changed to lowercase. -All other characters are left unchanged. -The definition of what an uppercase letter is depends on the current locale. - - - - -

-


string.match (s, pattern [, init])

-Looks for the first match of -pattern (see §6.4.1) in the string s. -If it finds one, then match returns -the captures from the pattern; -otherwise it returns nil. -If pattern specifies no captures, -then the whole match is returned. -A third, optional numeric argument init specifies -where to start the search; -its default value is 1 and can be negative. - - - - -

-


string.pack (fmt, v1, v2, ···)

- - -

-Returns a binary string containing the values v1, v2, etc. -packed (that is, serialized in binary form) -according to the format string fmt (see §6.4.2). - - - - -

-


string.packsize (fmt)

- - -

-Returns the size of a string resulting from string.pack -with the given format. -The format string cannot have the variable-length options -'s' or 'z' (see §6.4.2). - - - - -

-


string.rep (s, n [, sep])

-Returns a string that is the concatenation of n copies of -the string s separated by the string sep. -The default value for sep is the empty string -(that is, no separator). -Returns the empty string if n is not positive. - - -

-(Note that it is very easy to exhaust the memory of your machine -with a single call to this function.) - - - - -

-


string.reverse (s)

-Returns a string that is the string s reversed. - - - - -

-


string.sub (s, i [, j])

-Returns the substring of s that -starts at i and continues until j; -i and j can be negative. -If j is absent, then it is assumed to be equal to -1 -(which is the same as the string length). -In particular, -the call string.sub(s,1,j) returns a prefix of s -with length j, -and string.sub(s, -i) (for a positive i) -returns a suffix of s -with length i. - - -

-If, after the translation of negative indices, -i is less than 1, -it is corrected to 1. -If j is greater than the string length, -it is corrected to that length. -If, after these corrections, -i is greater than j, -the function returns the empty string. - - - - -

-


string.unpack (fmt, s [, pos])

- - -

-Returns the values packed in string s (see string.pack) -according to the format string fmt (see §6.4.2). -An optional pos marks where -to start reading in s (default is 1). -After the read values, -this function also returns the index of the first unread byte in s. - - - - -

-


string.upper (s)

-Receives a string and returns a copy of this string with all -lowercase letters changed to uppercase. -All other characters are left unchanged. -The definition of what a lowercase letter is depends on the current locale. - - - - - -

6.4.1 – Patterns

- -

-Patterns in Lua are described by regular strings, -which are interpreted as patterns by the pattern-matching functions -string.find, -string.gmatch, -string.gsub, -and string.match. -This section describes the syntax and the meaning -(that is, what they match) of these strings. - - - -

Character Class:

-A character class is used to represent a set of characters. -The following combinations are allowed in describing a character class: - -

    - -
  • x: -(where x is not one of the magic characters -^$()%.[]*+-?) -represents the character x itself. -
  • - -
  • .: (a dot) represents all characters.
  • - -
  • %a: represents all letters.
  • - -
  • %c: represents all control characters.
  • - -
  • %d: represents all digits.
  • - -
  • %g: represents all printable characters except space.
  • - -
  • %l: represents all lowercase letters.
  • - -
  • %p: represents all punctuation characters.
  • - -
  • %s: represents all space characters.
  • - -
  • %u: represents all uppercase letters.
  • - -
  • %w: represents all alphanumeric characters.
  • - -
  • %x: represents all hexadecimal digits.
  • - -
  • %x: (where x is any non-alphanumeric character) -represents the character x. -This is the standard way to escape the magic characters. -Any non-alphanumeric character -(including all punctuation characters, even the non-magical) -can be preceded by a '%' -when used to represent itself in a pattern. -
  • - -
  • [set]: -represents the class which is the union of all -characters in set. -A range of characters can be specified by -separating the end characters of the range, -in ascending order, with a '-'. -All classes %x described above can also be used as -components in set. -All other characters in set represent themselves. -For example, [%w_] (or [_%w]) -represents all alphanumeric characters plus the underscore, -[0-7] represents the octal digits, -and [0-7%l%-] represents the octal digits plus -the lowercase letters plus the '-' character. - - -

    -You can put a closing square bracket in a set -by positioning it as the first character in the set. -You can put a hyphen in a set -by positioning it as the first or the last character in the set. -(You can also use an escape for both cases.) - - -

    -The interaction between ranges and classes is not defined. -Therefore, patterns like [%a-z] or [a-%%] -have no meaning. -

  • - -
  • [^set]: -represents the complement of set, -where set is interpreted as above. -
  • - -

-For all classes represented by single letters (%a, %c, etc.), -the corresponding uppercase letter represents the complement of the class. -For instance, %S represents all non-space characters. - - -

-The definitions of letter, space, and other character groups -depend on the current locale. -In particular, the class [a-z] may not be equivalent to %l. - - - - - -

Pattern Item:

-A pattern item can be - -

    - -
  • -a single character class, -which matches any single character in the class; -
  • - -
  • -a single character class followed by '*', -which matches zero or more repetitions of characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '+', -which matches one or more repetitions of characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '-', -which also matches zero or more repetitions of characters in the class. -Unlike '*', -these repetition items will always match the shortest possible sequence; -
  • - -
  • -a single character class followed by '?', -which matches zero or one occurrence of a character in the class. -It always matches one occurrence if possible; -
  • - -
  • -%n, for n between 1 and 9; -such item matches a substring equal to the n-th captured string -(see below); -
  • - -
  • -%bxy, where x and y are two distinct characters; -such item matches strings that start with x, end with y, -and where the x and y are balanced. -This means that, if one reads the string from left to right, -counting +1 for an x and -1 for a y, -the ending y is the first y where the count reaches 0. -For instance, the item %b() matches expressions with -balanced parentheses. -
  • - -
  • -%f[set], a frontier pattern; -such item matches an empty string at any position such that -the next character belongs to set -and the previous character does not belong to set. -The set set is interpreted as previously described. -The beginning and the end of the subject are handled as if -they were the character '\0'. -
  • - -
- - - - -

Pattern:

-A pattern is a sequence of pattern items. -A caret '^' at the beginning of a pattern anchors the match at the -beginning of the subject string. -A '$' at the end of a pattern anchors the match at the -end of the subject string. -At other positions, -'^' and '$' have no special meaning and represent themselves. - - - - - -

Captures:

-A pattern can contain sub-patterns enclosed in parentheses; -they describe captures. -When a match succeeds, the substrings of the subject string -that match captures are stored (captured) for future use. -Captures are numbered according to their left parentheses. -For instance, in the pattern "(a*(.)%w(%s*))", -the part of the string matching "a*(.)%w(%s*)" is -stored as the first capture (and therefore has number 1); -the character matching "." is captured with number 2, -and the part matching "%s*" has number 3. - - -

-As a special case, the empty capture () captures -the current string position (a number). -For instance, if we apply the pattern "()aa()" on the -string "flaaap", there will be two captures: 3 and 5. - - - - - - - -

6.4.2 – Format Strings for Pack and Unpack

- -

-The first argument to string.pack, -string.packsize, and string.unpack -is a format string, -which describes the layout of the structure being created or read. - - -

-A format string is a sequence of conversion options. -The conversion options are as follows: - -

    -
  • <: sets little endian
  • -
  • >: sets big endian
  • -
  • =: sets native endian
  • -
  • ![n]: sets maximum alignment to n -(default is native alignment)
  • -
  • b: a signed byte (char)
  • -
  • B: an unsigned byte (char)
  • -
  • h: a signed short (native size)
  • -
  • H: an unsigned short (native size)
  • -
  • l: a signed long (native size)
  • -
  • L: an unsigned long (native size)
  • -
  • j: a lua_Integer
  • -
  • J: a lua_Unsigned
  • -
  • T: a size_t (native size)
  • -
  • i[n]: a signed int with n bytes -(default is native size)
  • -
  • I[n]: an unsigned int with n bytes -(default is native size)
  • -
  • f: a float (native size)
  • -
  • d: a double (native size)
  • -
  • n: a lua_Number
  • -
  • cn: a fixed-sized string with n bytes
  • -
  • z: a zero-terminated string
  • -
  • s[n]: a string preceded by its length -coded as an unsigned integer with n bytes -(default is a size_t)
  • -
  • x: one byte of padding
  • -
  • Xop: an empty item that aligns -according to option op -(which is otherwise ignored)
  • -
  • ' ': (empty space) ignored
  • -

-(A "[n]" means an optional integral numeral.) -Except for padding, spaces, and configurations -(options "xX <=>!"), -each option corresponds to an argument (in string.pack) -or a result (in string.unpack). - - -

-For options "!n", "sn", "in", and "In", -n can be any integer between 1 and 16. -All integral options check overflows; -string.pack checks whether the given value fits in the given size; -string.unpack checks whether the read value fits in a Lua integer. - - -

-Any format string starts as if prefixed by "!1=", -that is, -with maximum alignment of 1 (no alignment) -and native endianness. - - -

-Alignment works as follows: -For each option, -the format gets extra padding until the data starts -at an offset that is a multiple of the minimum between the -option size and the maximum alignment; -this minimum must be a power of 2. -Options "c" and "z" are not aligned; -option "s" follows the alignment of its starting integer. - - -

-All padding is filled with zeros by string.pack -(and ignored by string.unpack). - - - - - - - -

6.5 – UTF-8 Support

- -

-This library provides basic support for UTF-8 encoding. -It provides all its functions inside the table utf8. -This library does not provide any support for Unicode other -than the handling of the encoding. -Any operation that needs the meaning of a character, -such as character classification, is outside its scope. - - -

-Unless stated otherwise, -all functions that expect a byte position as a parameter -assume that the given position is either the start of a byte sequence -or one plus the length of the subject string. -As in the string library, -negative indices count from the end of the string. - - -

-


utf8.char (···)

-Receives zero or more integers, -converts each one to its corresponding UTF-8 byte sequence -and returns a string with the concatenation of all these sequences. - - - - -

-


utf8.charpattern

-The pattern (a string, not a function) "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" -(see §6.4.1), -which matches exactly one UTF-8 byte sequence, -assuming that the subject is a valid UTF-8 string. - - - - -

-


utf8.codes (s)

- - -

-Returns values so that the construction - -

-     for p, c in utf8.codes(s) do body end
-

-will iterate over all characters in string s, -with p being the position (in bytes) and c the code point -of each character. -It raises an error if it meets any invalid byte sequence. - - - - -

-


utf8.codepoint (s [, i [, j]])

-Returns the codepoints (as integers) from all characters in s -that start between byte position i and j (both included). -The default for i is 1 and for j is i. -It raises an error if it meets any invalid byte sequence. - - - - -

-


utf8.len (s [, i [, j]])

-Returns the number of UTF-8 characters in string s -that start between positions i and j (both inclusive). -The default for i is 1 and for j is -1. -If it finds any invalid byte sequence, -returns a false value plus the position of the first invalid byte. - - - - -

-


utf8.offset (s, n [, i])

-Returns the position (in bytes) where the encoding of the -n-th character of s -(counting from position i) starts. -A negative n gets characters before position i. -The default for i is 1 when n is non-negative -and #s + 1 otherwise, -so that utf8.offset(s, -n) gets the offset of the -n-th character from the end of the string. -If the specified character is neither in the subject -nor right after its end, -the function returns nil. - - -

-As a special case, -when n is 0 the function returns the start of the encoding -of the character that contains the i-th byte of s. - - -

-This function assumes that s is a valid UTF-8 string. - - - - - - - -

6.6 – Table Manipulation

- -

-This library provides generic functions for table manipulation. -It provides all its functions inside the table table. - - -

-Remember that, whenever an operation needs the length of a table, -all caveats about the length operator apply (see §3.4.7). -All functions ignore non-numeric keys -in the tables given as arguments. - - -

-


table.concat (list [, sep [, i [, j]]])

- - -

-Given a list where all elements are strings or numbers, -returns the string list[i]..sep..list[i+1] ··· sep..list[j]. -The default value for sep is the empty string, -the default for i is 1, -and the default for j is #list. -If i is greater than j, returns the empty string. - - - - -

-


table.insert (list, [pos,] value)

- - -

-Inserts element value at position pos in list, -shifting up the elements -list[pos], list[pos+1], ···, list[#list]. -The default value for pos is #list+1, -so that a call table.insert(t,x) inserts x at the end -of list t. - - - - -

-


table.move (a1, f, e, t [,a2])

- - -

-Moves elements from table a1 to table a2, -performing the equivalent to the following -multiple assignment: -a2[t],··· = a1[f],···,a1[e]. -The default for a2 is a1. -The destination range can overlap with the source range. -The number of elements to be moved must fit in a Lua integer. - - -

-Returns the destination table a2. - - - - -

-


table.pack (···)

- - -

-Returns a new table with all arguments stored into keys 1, 2, etc. -and with a field "n" with the total number of arguments. -Note that the resulting table may not be a sequence. - - - - -

-


table.remove (list [, pos])

- - -

-Removes from list the element at position pos, -returning the value of the removed element. -When pos is an integer between 1 and #list, -it shifts down the elements -list[pos+1], list[pos+2], ···, list[#list] -and erases element list[#list]; -The index pos can also be 0 when #list is 0, -or #list + 1; -in those cases, the function erases the element list[pos]. - - -

-The default value for pos is #list, -so that a call table.remove(l) removes the last element -of list l. - - - - -

-


table.sort (list [, comp])

- - -

-Sorts list elements in a given order, in-place, -from list[1] to list[#list]. -If comp is given, -then it must be a function that receives two list elements -and returns true when the first element must come -before the second in the final order -(so that, after the sort, -i < j implies not comp(list[j],list[i])). -If comp is not given, -then the standard Lua operator < is used instead. - - -

-Note that the comp function must define -a strict partial order over the elements in the list; -that is, it must be asymmetric and transitive. -Otherwise, no valid sort may be possible. - - -

-The sort algorithm is not stable: -elements considered equal by the given order -may have their relative positions changed by the sort. - - - - -

-


table.unpack (list [, i [, j]])

- - -

-Returns the elements from the given list. -This function is equivalent to - -

-     return list[i], list[i+1], ···, list[j]
-

-By default, i is 1 and j is #list. - - - - - - - -

6.7 – Mathematical Functions

- -

-This library provides basic mathematical functions. -It provides all its functions and constants inside the table math. -Functions with the annotation "integer/float" give -integer results for integer arguments -and float results for float (or mixed) arguments. -Rounding functions -(math.ceil, math.floor, and math.modf) -return an integer when the result fits in the range of an integer, -or a float otherwise. - - -

-


math.abs (x)

- - -

-Returns the absolute value of x. (integer/float) - - - - -

-


math.acos (x)

- - -

-Returns the arc cosine of x (in radians). - - - - -

-


math.asin (x)

- - -

-Returns the arc sine of x (in radians). - - - - -

-


math.atan (y [, x])

- - -

- -Returns the arc tangent of y/x (in radians), -but uses the signs of both arguments to find the -quadrant of the result. -(It also handles correctly the case of x being zero.) - - -

-The default value for x is 1, -so that the call math.atan(y) -returns the arc tangent of y. - - - - -

-


math.ceil (x)

- - -

-Returns the smallest integral value larger than or equal to x. - - - - -

-


math.cos (x)

- - -

-Returns the cosine of x (assumed to be in radians). - - - - -

-


math.deg (x)

- - -

-Converts the angle x from radians to degrees. - - - - -

-


math.exp (x)

- - -

-Returns the value ex -(where e is the base of natural logarithms). - - - - -

-


math.floor (x)

- - -

-Returns the largest integral value smaller than or equal to x. - - - - -

-


math.fmod (x, y)

- - -

-Returns the remainder of the division of x by y -that rounds the quotient towards zero. (integer/float) - - - - -

-


math.huge

- - -

-The float value HUGE_VAL, -a value larger than any other numeric value. - - - - -

-


math.log (x [, base])

- - -

-Returns the logarithm of x in the given base. -The default for base is e -(so that the function returns the natural logarithm of x). - - - - -

-


math.max (x, ···)

- - -

-Returns the argument with the maximum value, -according to the Lua operator <. (integer/float) - - - - -

-


math.maxinteger

-An integer with the maximum value for an integer. - - - - -

-


math.min (x, ···)

- - -

-Returns the argument with the minimum value, -according to the Lua operator <. (integer/float) - - - - -

-


math.mininteger

-An integer with the minimum value for an integer. - - - - -

-


math.modf (x)

- - -

-Returns the integral part of x and the fractional part of x. -Its second result is always a float. - - - - -

-


math.pi

- - -

-The value of π. - - - - -

-


math.rad (x)

- - -

-Converts the angle x from degrees to radians. - - - - -

-


math.random ([m [, n]])

- - -

-When called without arguments, -returns a pseudo-random float with uniform distribution -in the range [0,1). -When called with two integers m and n, -math.random returns a pseudo-random integer -with uniform distribution in the range [m, n]. -(The value n-m cannot be negative and must fit in a Lua integer.) -The call math.random(n) is equivalent to math.random(1,n). - - -

-This function is an interface to the underling -pseudo-random generator function provided by C. - - - - -

-


math.randomseed (x)

- - -

-Sets x as the "seed" -for the pseudo-random generator: -equal seeds produce equal sequences of numbers. - - - - -

-


math.sin (x)

- - -

-Returns the sine of x (assumed to be in radians). - - - - -

-


math.sqrt (x)

- - -

-Returns the square root of x. -(You can also use the expression x^0.5 to compute this value.) - - - - -

-


math.tan (x)

- - -

-Returns the tangent of x (assumed to be in radians). - - - - -

-


math.tointeger (x)

- - -

-If the value x is convertible to an integer, -returns that integer. -Otherwise, returns nil. - - - - -

-


math.type (x)

- - -

-Returns "integer" if x is an integer, -"float" if it is a float, -or nil if x is not a number. - - - - -

-


math.ult (m, n)

- - -

-Returns a boolean, -true if and only if integer m is below integer n when -they are compared as unsigned integers. - - - - - - - -

6.8 – Input and Output Facilities

- -

-The I/O library provides two different styles for file manipulation. -The first one uses implicit file handles; -that is, there are operations to set a default input file and a -default output file, -and all input/output operations are over these default files. -The second style uses explicit file handles. - - -

-When using implicit file handles, -all operations are supplied by table io. -When using explicit file handles, -the operation io.open returns a file handle -and then all operations are supplied as methods of the file handle. - - -

-The table io also provides -three predefined file handles with their usual meanings from C: -io.stdin, io.stdout, and io.stderr. -The I/O library never closes these files. - - -

-Unless otherwise stated, -all I/O functions return nil on failure -(plus an error message as a second result and -a system-dependent error code as a third result) -and some value different from nil on success. -In non-POSIX systems, -the computation of the error message and error code -in case of errors -may be not thread safe, -because they rely on the global C variable errno. - - -

-


io.close ([file])

- - -

-Equivalent to file:close(). -Without a file, closes the default output file. - - - - -

-


io.flush ()

- - -

-Equivalent to io.output():flush(). - - - - -

-


io.input ([file])

- - -

-When called with a file name, it opens the named file (in text mode), -and sets its handle as the default input file. -When called with a file handle, -it simply sets this file handle as the default input file. -When called without arguments, -it returns the current default input file. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


io.lines ([filename, ···])

- - -

-Opens the given file name in read mode -and returns an iterator function that -works like file:lines(···) over the opened file. -When the iterator function detects the end of file, -it returns no values (to finish the loop) and automatically closes the file. - - -

-The call io.lines() (with no file name) is equivalent -to io.input():lines("*l"); -that is, it iterates over the lines of the default input file. -In this case, the iterator does not close the file when the loop ends. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


io.open (filename [, mode])

- - -

-This function opens a file, -in the mode specified in the string mode. -In case of success, -it returns a new file handle. - - -

-The mode string can be any of the following: - -

    -
  • "r": read mode (the default);
  • -
  • "w": write mode;
  • -
  • "a": append mode;
  • -
  • "r+": update mode, all previous data is preserved;
  • -
  • "w+": update mode, all previous data is erased;
  • -
  • "a+": append update mode, previous data is preserved, - writing is only allowed at the end of file.
  • -

-The mode string can also have a 'b' at the end, -which is needed in some systems to open the file in binary mode. - - - - -

-


io.output ([file])

- - -

-Similar to io.input, but operates over the default output file. - - - - -

-


io.popen (prog [, mode])

- - -

-This function is system dependent and is not available -on all platforms. - - -

-Starts program prog in a separated process and returns -a file handle that you can use to read data from this program -(if mode is "r", the default) -or to write data to this program -(if mode is "w"). - - - - -

-


io.read (···)

- - -

-Equivalent to io.input():read(···). - - - - -

-


io.tmpfile ()

- - -

-In case of success, -returns a handle for a temporary file. -This file is opened in update mode -and it is automatically removed when the program ends. - - - - -

-


io.type (obj)

- - -

-Checks whether obj is a valid file handle. -Returns the string "file" if obj is an open file handle, -"closed file" if obj is a closed file handle, -or nil if obj is not a file handle. - - - - -

-


io.write (···)

- - -

-Equivalent to io.output():write(···). - - - - -

-


file:close ()

- - -

-Closes file. -Note that files are automatically closed when -their handles are garbage collected, -but that takes an unpredictable amount of time to happen. - - -

-When closing a file handle created with io.popen, -file:close returns the same values -returned by os.execute. - - - - -

-


file:flush ()

- - -

-Saves any written data to file. - - - - -

-


file:lines (···)

- - -

-Returns an iterator function that, -each time it is called, -reads the file according to the given formats. -When no format is given, -uses "l" as a default. -As an example, the construction - -

-     for c in file:lines(1) do body end
-

-will iterate over all characters of the file, -starting at the current position. -Unlike io.lines, this function does not close the file -when the loop ends. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


file:read (···)

- - -

-Reads the file file, -according to the given formats, which specify what to read. -For each format, -the function returns a string or a number with the characters read, -or nil if it cannot read data with the specified format. -(In this latter case, -the function does not read subsequent formats.) -When called without formats, -it uses a default format that reads the next line -(see below). - - -

-The available formats are - -

    - -
  • "n": -reads a numeral and returns it as a float or an integer, -following the lexical conventions of Lua. -(The numeral may have leading spaces and a sign.) -This format always reads the longest input sequence that -is a valid prefix for a numeral; -if that prefix does not form a valid numeral -(e.g., an empty string, "0x", or "3.4e-"), -it is discarded and the function returns nil. -
  • - -
  • "a": -reads the whole file, starting at the current position. -On end of file, it returns the empty string. -
  • - -
  • "l": -reads the next line skipping the end of line, -returning nil on end of file. -This is the default format. -
  • - -
  • "L": -reads the next line keeping the end-of-line character (if present), -returning nil on end of file. -
  • - -
  • number: -reads a string with up to this number of bytes, -returning nil on end of file. -If number is zero, -it reads nothing and returns an empty string, -or nil on end of file. -
  • - -

-The formats "l" and "L" should be used only for text files. - - - - -

-


file:seek ([whence [, offset]])

- - -

-Sets and gets the file position, -measured from the beginning of the file, -to the position given by offset plus a base -specified by the string whence, as follows: - -

    -
  • "set": base is position 0 (beginning of the file);
  • -
  • "cur": base is current position;
  • -
  • "end": base is end of file;
  • -

-In case of success, seek returns the final file position, -measured in bytes from the beginning of the file. -If seek fails, it returns nil, -plus a string describing the error. - - -

-The default value for whence is "cur", -and for offset is 0. -Therefore, the call file:seek() returns the current -file position, without changing it; -the call file:seek("set") sets the position to the -beginning of the file (and returns 0); -and the call file:seek("end") sets the position to the -end of the file, and returns its size. - - - - -

-


file:setvbuf (mode [, size])

- - -

-Sets the buffering mode for an output file. -There are three available modes: - -

    - -
  • "no": -no buffering; the result of any output operation appears immediately. -
  • - -
  • "full": -full buffering; output operation is performed only -when the buffer is full or when -you explicitly flush the file (see io.flush). -
  • - -
  • "line": -line buffering; output is buffered until a newline is output -or there is any input from some special files -(such as a terminal device). -
  • - -

-For the last two cases, size -specifies the size of the buffer, in bytes. -The default is an appropriate size. - - - - -

-


file:write (···)

- - -

-Writes the value of each of its arguments to file. -The arguments must be strings or numbers. - - -

-In case of success, this function returns file. -Otherwise it returns nil plus a string describing the error. - - - - - - - -

6.9 – Operating System Facilities

- -

-This library is implemented through table os. - - -

-


os.clock ()

- - -

-Returns an approximation of the amount in seconds of CPU time -used by the program. - - - - -

-


os.date ([format [, time]])

- - -

-Returns a string or a table containing date and time, -formatted according to the given string format. - - -

-If the time argument is present, -this is the time to be formatted -(see the os.time function for a description of this value). -Otherwise, date formats the current time. - - -

-If format starts with '!', -then the date is formatted in Coordinated Universal Time. -After this optional character, -if format is the string "*t", -then date returns a table with the following fields: -year, month (1–12), day (1–31), -hour (0–23), min (0–59), sec (0–61), -wday (weekday, 1–7, Sunday is 1), -yday (day of the year, 1–366), -and isdst (daylight saving flag, a boolean). -This last field may be absent -if the information is not available. - - -

-If format is not "*t", -then date returns the date as a string, -formatted according to the same rules as the ISO C function strftime. - - -

-When called without arguments, -date returns a reasonable date and time representation that depends on -the host system and on the current locale. -(More specifically, os.date() is equivalent to os.date("%c").) - - -

-In non-POSIX systems, -this function may be not thread safe -because of its reliance on C function gmtime and C function localtime. - - - - -

-


os.difftime (t2, t1)

- - -

-Returns the difference, in seconds, -from time t1 to time t2 -(where the times are values returned by os.time). -In POSIX, Windows, and some other systems, -this value is exactly t2-t1. - - - - -

-


os.execute ([command])

- - -

-This function is equivalent to the ISO C function system. -It passes command to be executed by an operating system shell. -Its first result is true -if the command terminated successfully, -or nil otherwise. -After this first result -the function returns a string plus a number, -as follows: - -

    - -
  • "exit": -the command terminated normally; -the following number is the exit status of the command. -
  • - -
  • "signal": -the command was terminated by a signal; -the following number is the signal that terminated the command. -
  • - -
- -

-When called without a command, -os.execute returns a boolean that is true if a shell is available. - - - - -

-


os.exit ([code [, close]])

- - -

-Calls the ISO C function exit to terminate the host program. -If code is true, -the returned status is EXIT_SUCCESS; -if code is false, -the returned status is EXIT_FAILURE; -if code is a number, -the returned status is this number. -The default value for code is true. - - -

-If the optional second argument close is true, -closes the Lua state before exiting. - - - - -

-


os.getenv (varname)

- - -

-Returns the value of the process environment variable varname, -or nil if the variable is not defined. - - - - -

-


os.remove (filename)

- - -

-Deletes the file (or empty directory, on POSIX systems) -with the given name. -If this function fails, it returns nil, -plus a string describing the error and the error code. -Otherwise, it returns true. - - - - -

-


os.rename (oldname, newname)

- - -

-Renames the file or directory named oldname to newname. -If this function fails, it returns nil, -plus a string describing the error and the error code. -Otherwise, it returns true. - - - - -

-


os.setlocale (locale [, category])

- - -

-Sets the current locale of the program. -locale is a system-dependent string specifying a locale; -category is an optional string describing which category to change: -"all", "collate", "ctype", -"monetary", "numeric", or "time"; -the default category is "all". -The function returns the name of the new locale, -or nil if the request cannot be honored. - - -

-If locale is the empty string, -the current locale is set to an implementation-defined native locale. -If locale is the string "C", -the current locale is set to the standard C locale. - - -

-When called with nil as the first argument, -this function only returns the name of the current locale -for the given category. - - -

-This function may be not thread safe -because of its reliance on C function setlocale. - - - - -

-


os.time ([table])

- - -

-Returns the current time when called without arguments, -or a time representing the local date and time specified by the given table. -This table must have fields year, month, and day, -and may have fields -hour (default is 12), -min (default is 0), -sec (default is 0), -and isdst (default is nil). -Other fields are ignored. -For a description of these fields, see the os.date function. - - -

-The values in these fields do not need to be inside their valid ranges. -For instance, if sec is -10, -it means -10 seconds from the time specified by the other fields; -if hour is 1000, -it means +1000 hours from the time specified by the other fields. - - -

-The returned value is a number, whose meaning depends on your system. -In POSIX, Windows, and some other systems, -this number counts the number -of seconds since some given start time (the "epoch"). -In other systems, the meaning is not specified, -and the number returned by time can be used only as an argument to -os.date and os.difftime. - - - - -

-


os.tmpname ()

- - -

-Returns a string with a file name that can -be used for a temporary file. -The file must be explicitly opened before its use -and explicitly removed when no longer needed. - - -

-In POSIX systems, -this function also creates a file with that name, -to avoid security risks. -(Someone else might create the file with wrong permissions -in the time between getting the name and creating the file.) -You still have to open the file to use it -and to remove it (even if you do not use it). - - -

-When possible, -you may prefer to use io.tmpfile, -which automatically removes the file when the program ends. - - - - - - - -

6.10 – The Debug Library

- -

-This library provides -the functionality of the debug interface (§4.9) to Lua programs. -You should exert care when using this library. -Several of its functions -violate basic assumptions about Lua code -(e.g., that variables local to a function -cannot be accessed from outside; -that userdata metatables cannot be changed by Lua code; -that Lua programs do not crash) -and therefore can compromise otherwise secure code. -Moreover, some functions in this library may be slow. - - -

-All functions in this library are provided -inside the debug table. -All functions that operate over a thread -have an optional first argument which is the -thread to operate over. -The default is always the current thread. - - -

-


debug.debug ()

- - -

-Enters an interactive mode with the user, -running each string that the user enters. -Using simple commands and other debug facilities, -the user can inspect global and local variables, -change their values, evaluate expressions, and so on. -A line containing only the word cont finishes this function, -so that the caller continues its execution. - - -

-Note that commands for debug.debug are not lexically nested -within any function and so have no direct access to local variables. - - - - -

-


debug.gethook ([thread])

- - -

-Returns the current hook settings of the thread, as three values: -the current hook function, the current hook mask, -and the current hook count -(as set by the debug.sethook function). - - - - -

-


debug.getinfo ([thread,] f [, what])

- - -

-Returns a table with information about a function. -You can give the function directly -or you can give a number as the value of f, -which means the function running at level f of the call stack -of the given thread: -level 0 is the current function (getinfo itself); -level 1 is the function that called getinfo -(except for tail calls, which do not count on the stack); -and so on. -If f is a number larger than the number of active functions, -then getinfo returns nil. - - -

-The returned table can contain all the fields returned by lua_getinfo, -with the string what describing which fields to fill in. -The default for what is to get all information available, -except the table of valid lines. -If present, -the option 'f' -adds a field named func with the function itself. -If present, -the option 'L' -adds a field named activelines with the table of -valid lines. - - -

-For instance, the expression debug.getinfo(1,"n").name returns -a name for the current function, -if a reasonable name can be found, -and the expression debug.getinfo(print) -returns a table with all available information -about the print function. - - - - -

-


debug.getlocal ([thread,] f, local)

- - -

-This function returns the name and the value of the local variable -with index local of the function at level f of the stack. -This function accesses not only explicit local variables, -but also parameters, temporaries, etc. - - -

-The first parameter or local variable has index 1, and so on, -following the order that they are declared in the code, -counting only the variables that are active -in the current scope of the function. -Negative indices refer to vararg arguments; --1 is the first vararg argument. -The function returns nil if there is no variable with the given index, -and raises an error when called with a level out of range. -(You can call debug.getinfo to check whether the level is valid.) - - -

-Variable names starting with '(' (open parenthesis) -represent variables with no known names -(internal variables such as loop control variables, -and variables from chunks saved without debug information). - - -

-The parameter f may also be a function. -In that case, getlocal returns only the name of function parameters. - - - - -

-


debug.getmetatable (value)

- - -

-Returns the metatable of the given value -or nil if it does not have a metatable. - - - - -

-


debug.getregistry ()

- - -

-Returns the registry table (see §4.5). - - - - -

-


debug.getupvalue (f, up)

- - -

-This function returns the name and the value of the upvalue -with index up of the function f. -The function returns nil if there is no upvalue with the given index. - - -

-Variable names starting with '(' (open parenthesis) -represent variables with no known names -(variables from chunks saved without debug information). - - - - -

-


debug.getuservalue (u)

- - -

-Returns the Lua value associated to u. -If u is not a full userdata, -returns nil. - - - - -

-


debug.sethook ([thread,] hook, mask [, count])

- - -

-Sets the given function as a hook. -The string mask and the number count describe -when the hook will be called. -The string mask may have any combination of the following characters, -with the given meaning: - -

    -
  • 'c': the hook is called every time Lua calls a function;
  • -
  • 'r': the hook is called every time Lua returns from a function;
  • -
  • 'l': the hook is called every time Lua enters a new line of code.
  • -

-Moreover, -with a count different from zero, -the hook is called also after every count instructions. - - -

-When called without arguments, -debug.sethook turns off the hook. - - -

-When the hook is called, its first argument is a string -describing the event that has triggered its call: -"call" (or "tail call"), -"return", -"line", and "count". -For line events, -the hook also gets the new line number as its second parameter. -Inside a hook, -you can call getinfo with level 2 to get more information about -the running function -(level 0 is the getinfo function, -and level 1 is the hook function). - - - - -

-


debug.setlocal ([thread,] level, local, value)

- - -

-This function assigns the value value to the local variable -with index local of the function at level level of the stack. -The function returns nil if there is no local -variable with the given index, -and raises an error when called with a level out of range. -(You can call getinfo to check whether the level is valid.) -Otherwise, it returns the name of the local variable. - - -

-See debug.getlocal for more information about -variable indices and names. - - - - -

-


debug.setmetatable (value, table)

- - -

-Sets the metatable for the given value to the given table -(which can be nil). -Returns value. - - - - -

-


debug.setupvalue (f, up, value)

- - -

-This function assigns the value value to the upvalue -with index up of the function f. -The function returns nil if there is no upvalue -with the given index. -Otherwise, it returns the name of the upvalue. - - - - -

-


debug.setuservalue (udata, value)

- - -

-Sets the given value as -the Lua value associated to the given udata. -udata must be a full userdata. - - -

-Returns udata. - - - - -

-


debug.traceback ([thread,] [message [, level]])

- - -

-If message is present but is neither a string nor nil, -this function returns message without further processing. -Otherwise, -it returns a string with a traceback of the call stack. -The optional message string is appended -at the beginning of the traceback. -An optional level number tells at which level -to start the traceback -(default is 1, the function calling traceback). - - - - -

-


debug.upvalueid (f, n)

- - -

-Returns a unique identifier (as a light userdata) -for the upvalue numbered n -from the given function. - - -

-These unique identifiers allow a program to check whether different -closures share upvalues. -Lua closures that share an upvalue -(that is, that access a same external local variable) -will return identical ids for those upvalue indices. - - - - -

-


debug.upvaluejoin (f1, n1, f2, n2)

- - -

-Make the n1-th upvalue of the Lua closure f1 -refer to the n2-th upvalue of the Lua closure f2. - - - - - - - -

7 – Lua Standalone

- -

-Although Lua has been designed as an extension language, -to be embedded in a host C program, -it is also frequently used as a standalone language. -An interpreter for Lua as a standalone language, -called simply lua, -is provided with the standard distribution. -The standalone interpreter includes -all standard libraries, including the debug library. -Its usage is: - -

-     lua [options] [script [args]]
-

-The options are: - -

    -
  • -e stat: executes string stat;
  • -
  • -l mod: "requires" mod and assigns the - result to global @mod;
  • -
  • -i: enters interactive mode after running script;
  • -
  • -v: prints version information;
  • -
  • -E: ignores environment variables;
  • -
  • --: stops handling options;
  • -
  • -: executes stdin as a file and stops handling options.
  • -

-After handling its options, lua runs the given script. -When called without arguments, -lua behaves as lua -v -i -when the standard input (stdin) is a terminal, -and as lua - otherwise. - - -

-When called without option -E, -the interpreter checks for an environment variable LUA_INIT_5_3 -(or LUA_INIT if the versioned name is not defined) -before running any argument. -If the variable content has the format @filename, -then lua executes the file. -Otherwise, lua executes the string itself. - - -

-When called with option -E, -besides ignoring LUA_INIT, -Lua also ignores -the values of LUA_PATH and LUA_CPATH, -setting the values of -package.path and package.cpath -with the default paths defined in luaconf.h. - - -

-All options are handled in order, except -i and -E. -For instance, an invocation like - -

-     $ lua -e'a=1' -e 'print(a)' script.lua
-

-will first set a to 1, then print the value of a, -and finally run the file script.lua with no arguments. -(Here $ is the shell prompt. Your prompt may be different.) - - -

-Before running any code, -lua collects all command-line arguments -in a global table called arg. -The script name goes to index 0, -the first argument after the script name goes to index 1, -and so on. -Any arguments before the script name -(that is, the interpreter name plus its options) -go to negative indices. -For instance, in the call - -

-     $ lua -la b.lua t1 t2
-

-the table is like this: - -

-     arg = { [-2] = "lua", [-1] = "-la",
-             [0] = "b.lua",
-             [1] = "t1", [2] = "t2" }
-

-If there is no script in the call, -the interpreter name goes to index 0, -followed by the other arguments. -For instance, the call - -

-     $ lua -e "print(arg[1])"
-

-will print "-e". -If there is a script, -the script is called with arguments -arg[1], ···, arg[#arg]. -(Like all chunks in Lua, -the script is compiled as a vararg function.) - - -

-In interactive mode, -Lua repeatedly prompts and waits for a line. -After reading a line, -Lua first try to interpret the line as an expression. -If it succeeds, it prints its value. -Otherwise, it interprets the line as a statement. -If you write an incomplete statement, -the interpreter waits for its completion -by issuing a different prompt. - - -

-If the global variable _PROMPT contains a string, -then its value is used as the prompt. -Similarly, if the global variable _PROMPT2 contains a string, -its value is used as the secondary prompt -(issued during incomplete statements). - - -

-In case of unprotected errors in the script, -the interpreter reports the error to the standard error stream. -If the error object is not a string but -has a metamethod __tostring, -the interpreter calls this metamethod to produce the final message. -Otherwise, the interpreter converts the error object to a string -and adds a stack traceback to it. - - -

-When finishing normally, -the interpreter closes its main Lua state -(see lua_close). -The script can avoid this step by -calling os.exit to terminate. - - -

-To allow the use of Lua as a -script interpreter in Unix systems, -the standalone interpreter skips -the first line of a chunk if it starts with #. -Therefore, Lua scripts can be made into executable programs -by using chmod +x and the #! form, -as in - -

-     #!/usr/local/bin/lua
-

-(Of course, -the location of the Lua interpreter may be different in your machine. -If lua is in your PATH, -then - -

-     #!/usr/bin/env lua
-

-is a more portable solution.) - - - -

8 – Incompatibilities with the Previous Version

- -

-Here we list the incompatibilities that you may find when moving a program -from Lua 5.2 to Lua 5.3. -You can avoid some incompatibilities by compiling Lua with -appropriate options (see file luaconf.h). -However, -all these compatibility options will be removed in the future. - - -

-Lua versions can always change the C API in ways that -do not imply source-code changes in a program, -such as the numeric values for constants -or the implementation of functions as macros. -Therefore, -you should not assume that binaries are compatible between -different Lua versions. -Always recompile clients of the Lua API when -using a new version. - - -

-Similarly, Lua versions can always change the internal representation -of precompiled chunks; -precompiled chunks are not compatible between different Lua versions. - - -

-The standard paths in the official distribution may -change between versions. - - - -

8.1 – Changes in the Language

-
    - -
  • -The main difference between Lua 5.2 and Lua 5.3 is the -introduction of an integer subtype for numbers. -Although this change should not affect "normal" computations, -some computations -(mainly those that involve some kind of overflow) -can give different results. - - -

    -You can fix these differences by forcing a number to be a float -(in Lua 5.2 all numbers were float), -in particular writing constants with an ending .0 -or using x = x + 0.0 to convert a variable. -(This recommendation is only for a quick fix -for an occasional incompatibility; -it is not a general guideline for good programming. -For good programming, -use floats where you need floats -and integers where you need integers.) -

  • - -
  • -The conversion of a float to a string now adds a .0 suffix -to the result if it looks like an integer. -(For instance, the float 2.0 will be printed as 2.0, -not as 2.) -You should always use an explicit format -when you need a specific format for numbers. - - -

    -(Formally this is not an incompatibility, -because Lua does not specify how numbers are formatted as strings, -but some programs assumed a specific format.) -

  • - -
  • -The generational mode for the garbage collector was removed. -(It was an experimental feature in Lua 5.2.) -
  • - -
- - - - -

8.2 – Changes in the Libraries

-
    - -
  • -The bit32 library has been deprecated. -It is easy to require a compatible external library or, -better yet, to replace its functions with appropriate bitwise operations. -(Keep in mind that bit32 operates on 32-bit integers, -while the bitwise operators in Lua 5.3 operate on Lua integers, -which by default have 64 bits.) -
  • - -
  • -The Table library now respects metamethods -for setting and getting elements. -
  • - -
  • -The ipairs iterator now respects metamethods and -its __ipairs metamethod has been deprecated. -
  • - -
  • -Option names in io.read do not have a starting '*' anymore. -For compatibility, Lua will continue to accept (and ignore) this character. -
  • - -
  • -The following functions were deprecated in the mathematical library: -atan2, cosh, sinh, tanh, pow, -frexp, and ldexp. -You can replace math.pow(x,y) with x^y; -you can replace math.atan2 with math.atan, -which now accepts one or two arguments; -you can replace math.ldexp(x,exp) with x * 2.0^exp. -For the other operations, -you can either use an external library or -implement them in Lua. -
  • - -
  • -The searcher for C loaders used by require -changed the way it handles versioned names. -Now, the version should come after the module name -(as is usual in most other tools). -For compatibility, that searcher still tries the old format -if it cannot find an open function according to the new style. -(Lua 5.2 already worked that way, -but it did not document the change.) -
  • - -
  • -The call collectgarbage("count") now returns only one result. -(You can compute that second result from the fractional part -of the first result.) -
  • - -
- - - - -

8.3 – Changes in the API

- - -
    - -
  • -Continuation functions now receive as arguments what they needed -to get through lua_getctx, -so lua_getctx has been removed. -Adapt your code accordingly. -
  • - -
  • -Function lua_dump has an extra parameter, strip. -Use 0 as the value of this parameter to get the old behavior. -
  • - -
  • -Functions to inject/project unsigned integers -(lua_pushunsigned, lua_tounsigned, lua_tounsignedx, -luaL_checkunsigned, luaL_optunsigned) -were deprecated. -Use their signed equivalents with a type cast. -
  • - -
  • -Macros to project non-default integer types -(luaL_checkint, luaL_optint, luaL_checklong, luaL_optlong) -were deprecated. -Use their equivalent over lua_Integer with a type cast -(or, when possible, use lua_Integer in your code). -
  • - -
- - - - -

9 – The Complete Syntax of Lua

- -

-Here is the complete syntax of Lua in extended BNF. -As usual in extended BNF, -{A} means 0 or more As, -and [A] means an optional A. -(For operator precedences, see §3.4.8; -for a description of the terminals -Name, Numeral, -and LiteralString, see §3.1.) - - - - -

-
-	chunk ::= block
-
-	block ::= {stat} [retstat]
-
-	stat ::=  ‘;’ | 
-		 varlist ‘=’ explist | 
-		 functioncall | 
-		 label | 
-		 break | 
-		 goto Name | 
-		 do block end | 
-		 while exp do block end | 
-		 repeat block until exp | 
-		 if exp then block {elseif exp then block} [else block] end | 
-		 for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end | 
-		 for namelist in explist do block end | 
-		 function funcname funcbody | 
-		 local function Name funcbody | 
-		 local namelist [‘=’ explist] 
-
-	retstat ::= return [explist] [‘;’]
-
-	label ::= ‘::’ Name ‘::’
-
-	funcname ::= Name {‘.’ Name} [‘:’ Name]
-
-	varlist ::= var {‘,’ var}
-
-	var ::=  Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name 
-
-	namelist ::= Name {‘,’ Name}
-
-	explist ::= exp {‘,’ exp}
-
-	exp ::=  nil | false | true | Numeral | LiteralString | ‘...’ | functiondef | 
-		 prefixexp | tableconstructor | exp binop exp | unop exp 
-
-	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
-
-	functioncall ::=  prefixexp args | prefixexp ‘:’ Name args 
-
-	args ::=  ‘(’ [explist] ‘)’ | tableconstructor | LiteralString 
-
-	functiondef ::= function funcbody
-
-	funcbody ::= ‘(’ [parlist] ‘)’ block end
-
-	parlist ::= namelist [‘,’ ‘...’] | ‘...’
-
-	tableconstructor ::= ‘{’ [fieldlist] ‘}’
-
-	fieldlist ::= field {fieldsep field} [fieldsep]
-
-	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
-
-	fieldsep ::= ‘,’ | ‘;’
-
-	binop ::=  ‘+’ | ‘-’ | ‘*’ | ‘/’ | ‘//’ | ‘^’ | ‘%’ | 
-		 ‘&’ | ‘~’ | ‘|’ | ‘>>’ | ‘<<’ | ‘..’ | 
-		 ‘<’ | ‘<=’ | ‘>’ | ‘>=’ | ‘==’ | ‘~=’ | 
-		 and | or
-
-	unop ::= ‘-’ | not | ‘#’ | ‘~’
-
-
- -

- - - - - - - - -

- - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/osi-certified-72x60.png b/LuaBridge3/Tests/Lua/Lua.5.3.6/doc/osi-certified-72x60.png deleted file mode 100644 index 07df5f6ee7a7a8b2108025dcd815f73f145a83af..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3774 zcmV;v4ngsWP)$kl5 zqcT7g&?zu8?ezWYz4zUB-|zR9d+&Qy2xAN{qY(ew0A7^*gV^7jytKqPFV3{hZfovn zs%x!l>(m&Gdb8C+5XeR7>h0kj=o=X3A39;2KLYfEMt>p1YMW~dt`rpAC{lN~P>5pq zH1L4nAdCT17}*hN=LnEsvMl=5Ij^QArAa&_V~zoht-Ei~)E~(Ivhe0#jik{t$isEK znCH$TxCB8EKmcF>3@pRaHpbR%Gqm*dsZA4H{j(NjZFp^iNFW+RBx6R*X19J*`0XG5 z^Y>cR=^Hi9#ovYGlbFSr#Q*^PgCGC^gb*SC5TcBfzQLe-r2m!Quik&_g9XzTj0qSR zD`FkG_RYWDa^+#UUxL&t+!K+&(ion@Fd`5l5p7{Qsva9vegC|4^NzJUMvn)^gqWsF zvu^j=%FfCVg^cgbXDRl1DE$lsfe;BjjmFmRHER~E-MeWoNsyyNHCpG%Y}igd_(Md;&9La8_B075NDRX9gTD zIHY`}9E~aGi9Kk1@P~rmPna=*=gz~UTdTpsQmjX)J23%v9NliQS)8`xJh6Qz_nE~e z&tP|!dcJdo;JMNa3>afSx$lko8>fp-I}OiCVz(dOF1u6e8$IrsSP?=5mp~lkaFqm? zAUMxRq%ecIu3WE)Uf=%p8g z+RSY?G=VO%wAfdICj?Uzb+5jr{8m|)i#{M}JjaDIoXf#1=DYLwX;1EW&sijPvm6EkBGuOx6r~lKv`g`yH?)|&PRUr$5Ibw2HBM7C74XvE@gaPjN+@;j$J)AgYhnT-U5m+wj|Wz8K630AfO8PUoGD^^Mcq zY9C<~%wUm^u%ox5P21)KNN0$(v^OI$A~?iwsS_fRu1+`EH|CRdpA4zsk8Z#|?x@^vVEAL+2JxH%&^{JUU%B=?EU7`Ar*Q|JvqPofcBt765(*f5JI$>=3{<%K)4ei zogo$)5XP}_X$y^pIYyWTt}EAnhTq}u4sAdBvC(WC{I#x4^>$vCvQ0UDs^18sAQG9o zEaP0qjrSSv1W0FyO%9&y$@em~n@8}}EXBG6x%ew49J_q%l@As_XnNpi|MTTPr~ca_ zW%uon6dBKL*pvzYFvf<~p6K8hK9BDNNN0$7xp^hWC3n^7FoQ?P(=m(6!Pj&S2f1fqH=`(w)KcPl5aEi2}~4hF*f*g}vaS-=c7v>N8c z{yNM*%+azq=@prWtgpi~^3?^AsJqS(>=pb=6PrGH#=O{Hcho$_F#MtsK$$3e2fZvg zy}!-V%`+uFMOW87LIgu3vKuMgqwY0}*Sd;aokQp(F#-{}Ss(Iy1iekY1ZQX?1WEL? z7=zq`lH-#Hw=bHRio3yPun%`c5rI1Hb|wTSWTs|12Mg#QkkwTmy zAYul0H*_b(BnkP#!R_&p@d54uz0JKthGv3C^fdKS%~alookE`QX@%#MQN2=SFWrOha7Ij7ImStNaWsy~? zsylUeT02_-z-G4s0L!v=+Wx|cxr$tmY&$a1by8z#6HBp!*9{@mU9XQ0h@L%V_R}4g z&s#2{MCOj4`5ux-SUautC5@{U895o-biKMWWoQ09{|jx8wz}@_(ep%Yk4{90C#s6-sa}fU5{}m>#>VtE_b#5bn8O+3k{&6GoEkB;yGie;A_5Uy zqPN*tU()pE+_&~``5XX({el-xT_}%`%fsc>_0@m5{+FhXru>rpyLESe31R>cK^FFrCm+#WL$-D{Z3*9>Lg{wi}xEYn_`@Hy`-d z1N}kIY%@Eu&Bpe|Rr6N;%Yk>6&RI$lgpIO26BYT%C!dU-o4bqqQpGY?p6lPru6Hzc z@WuSDI^BYaDH*>R)~)$V1J0Edn4r(9vo>E<2XjOJr2*G124;t^U+p{iUnZN5oapCpCk(F}}<#3ZZli!Nk z^UWT;Q9qm-i`i$kJS}5P%puBJ<&krTO;*#$Y7d$o96EbQ{aF1XFpTj}wf}eI|IOba z%w}_CWu?JjkV>U-ad9L$@Mu$CU;pUQBZgt5QmI@n=W@9K(A(SF-rnxzy|_!5ekKqCQTad`sa|&&Q6jfy}iAEst?|mH*emIjg9SB zRVWlHl?r3bvh2qnf6V6(+>4TulB%kzFveeh{k1?K*t&J=m>dk9P8SjqQdn4sF;*&- z(b3VFnVH$y*$Rb%rs zefJ#z#KpyZ_0?C$jvY%)O?7a?7#}%u1OT>d*)keF*REZ=c=4j6tkr5MilS*cB_$;< zFArmEv)Oby-7}4>TD9uE_ulKT4s6Bp@^Y0*rBEo&o;?cy8#Zi^%jH+DTv4f1SFc_L zfc5LwXJ=;vKt@K!?%liR&!6Almmq$2R@G|tg$oyGnpP+jQBhF<(9qCOR8%AuiBtJCSc zyu1LQw6wIQre^Zw$^E0N)#}R1%J}$rkw`Qc#z0A{)dIkjDN`I(PfyS2=x9f~R4N64 zPe1*1=gytQ#l=RWao4V0bLY-=?Bpl*dQDA@LZMJ9l{Gar$;rvzfB$`Tb#+==T0=ua zSy@?1N{UXWyL9Q&#*G`Zv$GE#JXljxBauj2T3VD!rO9N<%F3#*uP-Sn(P%W=w{Jgx z{(NC!VNOmC0OaN6ZQHg@tJQw^;fGtdZUulVSFX&NGv~~iGoO9-nNq0~2n78w23E{L zmth7T3|W>10ISuSm6cUgRCMXmr5!tV0D!x@`?6)rcI?<8lgZ#IIehqVOiYYpi@x#3 z8xau^+1c4ER;th&( zVHk--A`l3|!os9dsYatANm8TH96x@%qM{-&FmUtc&2qVX-MV%A_U(J~%{TY#*<&ym zX3Ur|c$No?u%e>k#EBDaZEY7XUVLH`0zh|n zw_~XRz;RH!y1MS)zn_X$Km70mNs@ZKo~G$z$BuD09F}FpVzEY}F&d2ug#rLPJUpgPpKh}a^y$-i zJl@%}XHT6vRaaNHckf=MQYn>6Fk&*D<+ja0B z5C{a#&CQN-V`HPyXe3EeAP~gH#>U3RayT5ZSd1}tbaaSNDAZ^)j%n&QHMoE=7KubA zlWEeVNpiV7Dk=&gzM|0Dz(>0HA5Q-_F}_znz(xxqbU~E|+`a#EH|V zPjA|^DJLg~rs?+f_6rv-T)upnAP7fChoq;cFJHcV=gyt)zWXjs(+gZ<%kMDTlOd1+TFW%&z(D`)oKF*0@Bmd zLqkIy?RvewprGK+ojWv5%Ve?@D^>&r1p$CcrMhuv}x1&joiO~|IC>)G) - - -Lua 5.3 readme - - - - - - - -

-Lua -Welcome to Lua 5.3 -

- - - -

About Lua

-

-Lua is a powerful, fast, lightweight, embeddable scripting language -developed by a -team -at -PUC-Rio, -the Pontifical Catholic University of Rio de Janeiro in Brazil. -Lua is -free software -used in many products and projects around the world. - -

-Lua's -official web site -provides complete information -about Lua, -including -an -executive summary -and -updated -documentation, -especially the -reference manual, -which may differ slightly from the -local copy -distributed in this package. - -

Installing Lua

-

-Lua is distributed in -source -form. -You need to build it before using it. -Building Lua should be straightforward -because -Lua is implemented in pure ANSI C and compiles unmodified in all known -platforms that have an ANSI C compiler. -Lua also compiles unmodified as C++. -The instructions given below for building Lua are for Unix-like platforms. -See also -instructions for other systems -and -customization options. - -

-If you don't have the time or the inclination to compile Lua yourself, -get a binary from -LuaBinaries. -Try also -LuaDist, -a multi-platform distribution of Lua that includes batteries. - -

Building Lua

-

-In most Unix-like platforms, simply do "make" with a suitable target. -Here are the details. - -

    -
  1. -Open a terminal window and move to -the top-level directory, which is named lua-5.3.6. -The Makefile there controls both the build process and the installation process. -

    -

  2. - Do "make" and see if your platform is listed. - The platforms currently supported are: -

    -

    - aix bsd c89 freebsd generic linux macosx mingw posix solaris -

    -

    - If your platform is listed, just do "make xxx", where xxx - is your platform name. -

    - If your platform is not listed, try the closest one or posix, generic, - c89, in this order. -

    -

  3. -The compilation takes only a few moments -and produces three files in the src directory: -lua (the interpreter), -luac (the compiler), -and liblua.a (the library). -

    -

  4. - To check that Lua has been built correctly, do "make test" - after building Lua. This will run the interpreter and print its version. -
-

-If you're running Linux and get compilation errors, -make sure you have installed the readline development package -(which is probably named libreadline-dev or readline-devel). -If you get link errors after that, -then try "make linux MYLIBS=-ltermcap". - -

Installing Lua

-

- Once you have built Lua, you may want to install it in an official - place in your system. In this case, do "make install". The official - place and the way to install files are defined in the Makefile. You'll - probably need the right permissions to install files. - -

- To build and install Lua in one step, do "make xxx install", - where xxx is your platform name. - -

- To install Lua locally, do "make local". - This will create a directory install with subdirectories - bin, include, lib, man, share, - and install Lua as listed below. - - To install Lua locally, but in some other directory, do - "make install INSTALL_TOP=xxx", where xxx is your chosen directory. - The installation starts in the src and doc directories, - so take care if INSTALL_TOP is not an absolute path. - -

-
- bin: -
- lua luac -
- include: -
- lua.h luaconf.h lualib.h lauxlib.h lua.hpp -
- lib: -
- liblua.a -
- man/man1: -
- lua.1 luac.1 -
- -

- These are the only directories you need for development. - If you only want to run Lua programs, - you only need the files in bin and man. - The files in include and lib are needed for - embedding Lua in C or C++ programs. - -

Customization

-

- Three kinds of things can be customized by editing a file: -

    -
  • Where and how to install Lua — edit Makefile. -
  • How to build Lua — edit src/Makefile. -
  • Lua features — edit src/luaconf.h. -
- -

- You don't actually need to edit the Makefiles because you may set the - relevant variables in the command line when invoking make. - Nevertheless, it's probably best to edit and save the Makefiles to - record the changes you've made. - -

- On the other hand, if you need to customize some Lua features, you'll need - to edit src/luaconf.h before building and installing Lua. - The edited file will be the one installed, and - it will be used by any Lua clients that you build, to ensure consistency. - Further customization is available to experts by editing the Lua sources. - -

Building Lua on other systems

-

- If you're not using the usual Unix tools, then the instructions for - building Lua depend on the compiler you use. You'll need to create - projects (or whatever your compiler uses) for building the library, - the interpreter, and the compiler, as follows: - -

-
-library: -
-lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c -lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c -ltm.c lundump.c lvm.c lzio.c -lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c -lmathlib.c loslib.c lstrlib.c ltablib.c lutf8lib.c loadlib.c linit.c -
-interpreter: -
- library, lua.c -
-compiler: -
- library, luac.c -
- -

- To use Lua as a library in your own programs you'll need to know how to - create and use libraries with your compiler. Moreover, to dynamically load - C libraries for Lua you'll need to know how to create dynamic libraries - and you'll need to make sure that the Lua API functions are accessible to - those dynamic libraries — but don't link the Lua library - into each dynamic library. For Unix, we recommend that the Lua library - be linked statically into the host program and its symbols exported for - dynamic linking; src/Makefile does this for the Lua interpreter. - For Windows, we recommend that the Lua library be a DLL. - In all cases, the compiler luac should be linked statically. - -

- As mentioned above, you may edit src/luaconf.h to customize - some features before building Lua. - -

Changes since Lua 5.2

-

-Here are the main changes introduced in Lua 5.3. -The -reference manual -lists the -incompatibilities that had to be introduced. - -

Main changes

-
    -
  • integers (64-bit by default) -
  • official support for 32-bit numbers -
  • bitwise operators -
  • basic utf-8 support -
  • functions for packing and unpacking values - -
- -Here are the other changes introduced in Lua 5.3: -

Language

-
    -
  • userdata can have any Lua value as uservalue -
  • floor division -
  • more flexible rules for some metamethods -
- -

Libraries

-
    -
  • ipairs and the table library respect metamethods -
  • strip option in string.dump -
  • table library respects metamethods -
  • new function table.move -
  • new function string.pack -
  • new function string.unpack -
  • new function string.packsize -
- -

C API

-
    -
  • simpler API for continuation functions in C -
  • lua_gettable and similar functions return type of resulted value -
  • strip option in lua_dump -
  • new function: lua_geti -
  • new function: lua_seti -
  • new function: lua_isyieldable -
  • new function: lua_numbertointeger -
  • new function: lua_rotate -
  • new function: lua_stringtonumber -
- -

Lua standalone interpreter

-
    -
  • can be used as calculator; no need to prefix with '=' -
  • arg table available to all code -
- -

License

-

- -[osi certified] - -Lua is free software distributed under the terms of the -MIT license -reproduced below; -it may be used for any purpose, including commercial purposes, -at absolutely no cost without having to ask us. - -The only requirement is that if you do use Lua, -then you should give us credit by including the appropriate copyright notice somewhere in your product or its documentation. - -For details, see -this. - -

-Copyright © 1994–2020 Lua.org, PUC-Rio. - -

-Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -

-The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -

-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -

-

- -

- - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.c deleted file mode 100644 index 711895b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.c +++ /dev/null @@ -1,1299 +0,0 @@ -/* -** $Id: lapi.c,v 2.259.1.2 2017/12/06 18:35:12 roberto Exp $ -** Lua API -** See Copyright Notice in lua.h -*/ - -#define lapi_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" - - - -const char lua_ident[] = - "$LuaVersion: " LUA_COPYRIGHT " $" - "$LuaAuthors: " LUA_AUTHORS " $"; - - -/* value at a non-valid index */ -#define NONVALIDVALUE cast(TValue *, luaO_nilobject) - -/* corresponding test */ -#define isvalid(o) ((o) != luaO_nilobject) - -/* test for pseudo index */ -#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) - -/* test for upvalue */ -#define isupvalue(i) ((i) < LUA_REGISTRYINDEX) - -/* test for valid but not pseudo index */ -#define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) - -#define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index") - -#define api_checkstackindex(l, i, o) \ - api_check(l, isstackindex(i, o), "index not in the stack") - - -static TValue *index2addr (lua_State *L, int idx) { - CallInfo *ci = L->ci; - if (idx > 0) { - TValue *o = ci->func + idx; - api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return NONVALIDVALUE; - else return o; - } - else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return L->top + idx; - } - else if (idx == LUA_REGISTRYINDEX) - return &G(L)->l_registry; - else { /* upvalues */ - idx = LUA_REGISTRYINDEX - idx; - api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(ci->func)) /* light C function? */ - return NONVALIDVALUE; /* it has no upvalues */ - else { - CClosure *func = clCvalue(ci->func); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; - } - } -} - - -/* -** to be called by 'lua_checkstack' in protected mode, to grow stack -** capturing memory errors -*/ -static void growstack (lua_State *L, void *ud) { - int size = *(int *)ud; - luaD_growstack(L, size); -} - - -LUA_API int lua_checkstack (lua_State *L, int n) { - int res; - CallInfo *ci = L->ci; - lua_lock(L); - api_check(L, n >= 0, "negative 'n'"); - if (L->stack_last - L->top > n) /* stack large enough? */ - res = 1; /* yes; check is OK */ - else { /* no; need to grow stack */ - int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ - res = 0; /* no */ - else /* try to grow stack */ - res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); - } - if (res && ci->top < L->top + n) - ci->top = L->top + n; /* adjust frame top */ - lua_unlock(L); - return res; -} - - -LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { - int i; - if (from == to) return; - lua_lock(to); - api_checknelems(from, n); - api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "stack overflow"); - from->top -= n; - for (i = 0; i < n; i++) { - setobj2s(to, to->top, from->top + i); - to->top++; /* stack already checked by previous 'api_check' */ - } - lua_unlock(to); -} - - -LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { - lua_CFunction old; - lua_lock(L); - old = G(L)->panic; - G(L)->panic = panicf; - lua_unlock(L); - return old; -} - - -LUA_API const lua_Number *lua_version (lua_State *L) { - static const lua_Number version = LUA_VERSION_NUM; - if (L == NULL) return &version; - else return G(L)->version; -} - - - -/* -** basic stack manipulation -*/ - - -/* -** convert an acceptable stack index into an absolute index -*/ -LUA_API int lua_absindex (lua_State *L, int idx) { - return (idx > 0 || ispseudo(idx)) - ? idx - : cast_int(L->top - L->ci->func) + idx; -} - - -LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); -} - - -LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->ci->func; - lua_lock(L); - if (idx >= 0) { - api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); - while (L->top < (func + 1) + idx) - setnilvalue(L->top++); - L->top = (func + 1) + idx; - } - else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* 'subtract' index (index is negative) */ - } - lua_unlock(L); -} - - -/* -** Reverse the stack segment from 'from' to 'to' -** (auxiliary to 'lua_rotate') -*/ -static void reverse (lua_State *L, StkId from, StkId to) { - for (; from < to; from++, to--) { - TValue temp; - setobj(L, &temp, from); - setobjs2s(L, from, to); - setobj2s(L, to, &temp); - } -} - - -/* -** Let x = AB, where A is a prefix of length 'n'. Then, -** rotate x n == BA. But BA == (A^r . B^r)^r. -*/ -LUA_API void lua_rotate (lua_State *L, int idx, int n) { - StkId p, t, m; - lua_lock(L); - t = L->top - 1; /* end of stack segment being rotated */ - p = index2addr(L, idx); /* start of segment */ - api_checkstackindex(L, idx, p); - api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); - m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ - reverse(L, p, m); /* reverse the prefix with length 'n' */ - reverse(L, m + 1, t); /* reverse the suffix */ - reverse(L, p, t); /* reverse the entire segment */ - lua_unlock(L); -} - - -LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { - TValue *fr, *to; - lua_lock(L); - fr = index2addr(L, fromidx); - to = index2addr(L, toidx); - api_checkvalidindex(L, to); - setobj(L, to, fr); - if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(L->ci->func), fr); - /* LUA_REGISTRYINDEX does not need gc barrier - (collector revisits it before finishing collection) */ - lua_unlock(L); -} - - -LUA_API void lua_pushvalue (lua_State *L, int idx) { - lua_lock(L); - setobj2s(L, L->top, index2addr(L, idx)); - api_incr_top(L); - lua_unlock(L); -} - - - -/* -** access functions (stack -> C) -*/ - - -LUA_API int lua_type (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return (isvalid(o) ? ttnov(o) : LUA_TNONE); -} - - -LUA_API const char *lua_typename (lua_State *L, int t) { - UNUSED(L); - api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); - return ttypename(t); -} - - -LUA_API int lua_iscfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return (ttislcf(o) || (ttisCclosure(o))); -} - - -LUA_API int lua_isinteger (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return ttisinteger(o); -} - - -LUA_API int lua_isnumber (lua_State *L, int idx) { - lua_Number n; - const TValue *o = index2addr(L, idx); - return tonumber(o, &n); -} - - -LUA_API int lua_isstring (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); - return (ttisstring(o) || cvt2str(o)); -} - - -LUA_API int lua_isuserdata (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); - return (ttisfulluserdata(o) || ttislightuserdata(o)); -} - - -LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { - StkId o1 = index2addr(L, index1); - StkId o2 = index2addr(L, index2); - return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; -} - - -LUA_API void lua_arith (lua_State *L, int op) { - lua_lock(L); - if (op != LUA_OPUNM && op != LUA_OPBNOT) - api_checknelems(L, 2); /* all other operations expect two operands */ - else { /* for unary operations, add fake 2nd operand */ - api_checknelems(L, 1); - setobjs2s(L, L->top, L->top - 1); - api_incr_top(L); - } - /* first operand at top - 2, second at top - 1; result go to top - 2 */ - luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); - L->top--; /* remove second operand */ - lua_unlock(L); -} - - -LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { - StkId o1, o2; - int i = 0; - lua_lock(L); /* may call tag method */ - o1 = index2addr(L, index1); - o2 = index2addr(L, index2); - if (isvalid(o1) && isvalid(o2)) { - switch (op) { - case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; - case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; - case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; - default: api_check(L, 0, "invalid option"); - } - } - lua_unlock(L); - return i; -} - - -LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, L->top); - if (sz != 0) - api_incr_top(L); - return sz; -} - - -LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { - lua_Number n; - const TValue *o = index2addr(L, idx); - int isnum = tonumber(o, &n); - if (!isnum) - n = 0; /* call to 'tonumber' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; - return n; -} - - -LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { - lua_Integer res; - const TValue *o = index2addr(L, idx); - int isnum = tointeger(o, &res); - if (!isnum) - res = 0; /* call to 'tointeger' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; - return res; -} - - -LUA_API int lua_toboolean (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); - return !l_isfalse(o); -} - - -LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - StkId o = index2addr(L, idx); - if (!ttisstring(o)) { - if (!cvt2str(o)) { /* not convertible? */ - if (len != NULL) *len = 0; - return NULL; - } - lua_lock(L); /* 'luaO_tostring' may create a new string */ - luaO_tostring(L, o); - luaC_checkGC(L); - o = index2addr(L, idx); /* previous call may reallocate the stack */ - lua_unlock(L); - } - if (len != NULL) - *len = vslen(o); - return svalue(o); -} - - -LUA_API size_t lua_rawlen (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttype(o)) { - case LUA_TSHRSTR: return tsvalue(o)->shrlen; - case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; - case LUA_TUSERDATA: return uvalue(o)->len; - case LUA_TTABLE: return luaH_getn(hvalue(o)); - default: return 0; - } -} - - -LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - if (ttislcf(o)) return fvalue(o); - else if (ttisCclosure(o)) - return clCvalue(o)->f; - else return NULL; /* not a C function */ -} - - -LUA_API void *lua_touserdata (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttnov(o)) { - case LUA_TUSERDATA: return getudatamem(uvalue(o)); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; - } -} - - -LUA_API lua_State *lua_tothread (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - return (!ttisthread(o)) ? NULL : thvalue(o); -} - - -LUA_API const void *lua_topointer (lua_State *L, int idx) { - StkId o = index2addr(L, idx); - switch (ttype(o)) { - case LUA_TTABLE: return hvalue(o); - case LUA_TLCL: return clLvalue(o); - case LUA_TCCL: return clCvalue(o); - case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); - case LUA_TTHREAD: return thvalue(o); - case LUA_TUSERDATA: return getudatamem(uvalue(o)); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; - } -} - - - -/* -** push functions (C -> stack) -*/ - - -LUA_API void lua_pushnil (lua_State *L) { - lua_lock(L); - setnilvalue(L->top); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { - lua_lock(L); - setfltvalue(L->top, n); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { - lua_lock(L); - setivalue(L->top, n); - api_incr_top(L); - lua_unlock(L); -} - - -/* -** Pushes on the stack a string with given length. Avoid using 's' when -** 'len' == 0 (as 's' can be NULL in that case), due to later use of -** 'memcmp' and 'memcpy'. -*/ -LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { - TString *ts; - lua_lock(L); - ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); - setsvalue2s(L, L->top, ts); - api_incr_top(L); - luaC_checkGC(L); - lua_unlock(L); - return getstr(ts); -} - - -LUA_API const char *lua_pushstring (lua_State *L, const char *s) { - lua_lock(L); - if (s == NULL) - setnilvalue(L->top); - else { - TString *ts; - ts = luaS_new(L, s); - setsvalue2s(L, L->top, ts); - s = getstr(ts); /* internal copy's address */ - } - api_incr_top(L); - luaC_checkGC(L); - lua_unlock(L); - return s; -} - - -LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, - va_list argp) { - const char *ret; - lua_lock(L); - ret = luaO_pushvfstring(L, fmt, argp); - luaC_checkGC(L); - lua_unlock(L); - return ret; -} - - -LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { - const char *ret; - va_list argp; - lua_lock(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - luaC_checkGC(L); - lua_unlock(L); - return ret; -} - - -LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { - lua_lock(L); - if (n == 0) { - setfvalue(L->top, fn); - api_incr_top(L); - } - else { - CClosure *cl; - api_checknelems(L, n); - api_check(L, n <= MAXUPVAL, "upvalue index too large"); - cl = luaF_newCclosure(L, n); - cl->f = fn; - L->top -= n; - while (n--) { - setobj2n(L, &cl->upvalue[n], L->top + n); - /* does not need barrier because closure is white */ - } - setclCvalue(L, L->top, cl); - api_incr_top(L); - luaC_checkGC(L); - } - lua_unlock(L); -} - - -LUA_API void lua_pushboolean (lua_State *L, int b) { - lua_lock(L); - setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { - lua_lock(L); - setpvalue(L->top, p); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API int lua_pushthread (lua_State *L) { - lua_lock(L); - setthvalue(L, L->top, L); - api_incr_top(L); - lua_unlock(L); - return (G(L)->mainthread == L); -} - - - -/* -** get functions (Lua -> stack) -*/ - - -static int auxgetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; - TString *str = luaS_new(L, k); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); - api_incr_top(L); - } - else { - setsvalue2s(L, L->top, str); - api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, slot); - } - lua_unlock(L); - return ttnov(L->top - 1); -} - - -LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); - lua_lock(L); - return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); -} - - -LUA_API int lua_gettable (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - luaV_gettable(L, t, L->top - 1, L->top - 1); - lua_unlock(L); - return ttnov(L->top - 1); -} - - -LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { - lua_lock(L); - return auxgetstr(L, index2addr(L, idx), k); -} - - -LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { - StkId t; - const TValue *slot; - lua_lock(L); - t = index2addr(L, idx); - if (luaV_fastget(L, t, n, slot, luaH_getint)) { - setobj2s(L, L->top, slot); - api_incr_top(L); - } - else { - setivalue(L->top, n); - api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, slot); - } - lua_unlock(L); - return ttnov(L->top - 1); -} - - -LUA_API int lua_rawget (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); - lua_unlock(L); - return ttnov(L->top - 1); -} - - -LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top, luaH_getint(hvalue(t), n)); - api_incr_top(L); - lua_unlock(L); - return ttnov(L->top - 1); -} - - -LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { - StkId t; - TValue k; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setpvalue(&k, cast(void *, p)); - setobj2s(L, L->top, luaH_get(hvalue(t), &k)); - api_incr_top(L); - lua_unlock(L); - return ttnov(L->top - 1); -} - - -LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { - Table *t; - lua_lock(L); - t = luaH_new(L); - sethvalue(L, L->top, t); - api_incr_top(L); - if (narray > 0 || nrec > 0) - luaH_resize(L, t, narray, nrec); - luaC_checkGC(L); - lua_unlock(L); -} - - -LUA_API int lua_getmetatable (lua_State *L, int objindex) { - const TValue *obj; - Table *mt; - int res = 0; - lua_lock(L); - obj = index2addr(L, objindex); - switch (ttnov(obj)) { - case LUA_TTABLE: - mt = hvalue(obj)->metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(obj)->metatable; - break; - default: - mt = G(L)->mt[ttnov(obj)]; - break; - } - if (mt != NULL) { - sethvalue(L, L->top, mt); - api_incr_top(L); - res = 1; - } - lua_unlock(L); - return res; -} - - -LUA_API int lua_getuservalue (lua_State *L, int idx) { - StkId o; - lua_lock(L); - o = index2addr(L, idx); - api_check(L, ttisfulluserdata(o), "full userdata expected"); - getuservalue(L, uvalue(o), L->top); - api_incr_top(L); - lua_unlock(L); - return ttnov(L->top - 1); -} - - -/* -** set functions (stack -> Lua) -*/ - -/* -** t[k] = value at the top of the stack (where 'k' is a string) -*/ -static void auxsetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; - TString *str = luaS_new(L, k); - api_checknelems(L, 1); - if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) - L->top--; /* pop value */ - else { - setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ - api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, slot); - L->top -= 2; /* pop value and key */ - } - lua_unlock(L); /* lock done by caller */ -} - - -LUA_API void lua_setglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); - lua_lock(L); /* unlock done in 'auxsetstr' */ - auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); -} - - -LUA_API void lua_settable (lua_State *L, int idx) { - StkId t; - lua_lock(L); - api_checknelems(L, 2); - t = index2addr(L, idx); - luaV_settable(L, t, L->top - 2, L->top - 1); - L->top -= 2; /* pop index and value */ - lua_unlock(L); -} - - -LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { - lua_lock(L); /* unlock done in 'auxsetstr' */ - auxsetstr(L, index2addr(L, idx), k); -} - - -LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { - StkId t; - const TValue *slot; - lua_lock(L); - api_checknelems(L, 1); - t = index2addr(L, idx); - if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) - L->top--; /* pop value */ - else { - setivalue(L->top, n); - api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, slot); - L->top -= 2; /* pop value and key */ - } - lua_unlock(L); -} - - -LUA_API void lua_rawset (lua_State *L, int idx) { - StkId o; - TValue *slot; - lua_lock(L); - api_checknelems(L, 2); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - slot = luaH_set(L, hvalue(o), L->top - 2); - setobj2t(L, slot, L->top - 1); - invalidateTMcache(hvalue(o)); - luaC_barrierback(L, hvalue(o), L->top-1); - L->top -= 2; - lua_unlock(L); -} - - -LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - StkId o; - lua_lock(L); - api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - luaH_setint(L, hvalue(o), n, L->top - 1); - luaC_barrierback(L, hvalue(o), L->top-1); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - StkId o; - TValue k, *slot; - lua_lock(L); - api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttistable(o), "table expected"); - setpvalue(&k, cast(void *, p)); - slot = luaH_set(L, hvalue(o), &k); - setobj2t(L, slot, L->top - 1); - luaC_barrierback(L, hvalue(o), L->top - 1); - L->top--; - lua_unlock(L); -} - - -LUA_API int lua_setmetatable (lua_State *L, int objindex) { - TValue *obj; - Table *mt; - lua_lock(L); - api_checknelems(L, 1); - obj = index2addr(L, objindex); - if (ttisnil(L->top - 1)) - mt = NULL; - else { - api_check(L, ttistable(L->top - 1), "table expected"); - mt = hvalue(L->top - 1); - } - switch (ttnov(obj)) { - case LUA_TTABLE: { - hvalue(obj)->metatable = mt; - if (mt) { - luaC_objbarrier(L, gcvalue(obj), mt); - luaC_checkfinalizer(L, gcvalue(obj), mt); - } - break; - } - case LUA_TUSERDATA: { - uvalue(obj)->metatable = mt; - if (mt) { - luaC_objbarrier(L, uvalue(obj), mt); - luaC_checkfinalizer(L, gcvalue(obj), mt); - } - break; - } - default: { - G(L)->mt[ttnov(obj)] = mt; - break; - } - } - L->top--; - lua_unlock(L); - return 1; -} - - -LUA_API void lua_setuservalue (lua_State *L, int idx) { - StkId o; - lua_lock(L); - api_checknelems(L, 1); - o = index2addr(L, idx); - api_check(L, ttisfulluserdata(o), "full userdata expected"); - setuservalue(L, uvalue(o), L->top - 1); - luaC_barrier(L, gcvalue(o), L->top - 1); - L->top--; - lua_unlock(L); -} - - -/* -** 'load' and 'call' functions (run Lua code) -*/ - - -#define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ - "results from function overflow current stack size") - - -LUA_API void lua_callk (lua_State *L, int nargs, int nresults, - lua_KContext ctx, lua_KFunction k) { - StkId func; - lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), - "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); - checkresults(L, nargs, nresults); - func = L->top - (nargs+1); - if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ - L->ci->u.c.k = k; /* save continuation */ - L->ci->u.c.ctx = ctx; /* save context */ - luaD_call(L, func, nresults); /* do the call */ - } - else /* no continuation or no yieldable */ - luaD_callnoyield(L, func, nresults); /* just do the call */ - adjustresults(L, nresults); - lua_unlock(L); -} - - - -/* -** Execute a protected call. -*/ -struct CallS { /* data to 'f_call' */ - StkId func; - int nresults; -}; - - -static void f_call (lua_State *L, void *ud) { - struct CallS *c = cast(struct CallS *, ud); - luaD_callnoyield(L, c->func, c->nresults); -} - - - -LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, - lua_KContext ctx, lua_KFunction k) { - struct CallS c; - int status; - ptrdiff_t func; - lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), - "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); - checkresults(L, nargs, nresults); - if (errfunc == 0) - func = 0; - else { - StkId o = index2addr(L, errfunc); - api_checkstackindex(L, errfunc, o); - func = savestack(L, o); - } - c.func = L->top - (nargs+1); /* function to be called */ - if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ - c.nresults = nresults; /* do a 'conventional' protected call */ - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); - } - else { /* prepare continuation (call is already protected by 'resume') */ - CallInfo *ci = L->ci; - ci->u.c.k = k; /* save continuation */ - ci->u.c.ctx = ctx; /* save context */ - /* save information for error recovery */ - ci->extra = savestack(L, c.func); - ci->u.c.old_errfunc = L->errfunc; - L->errfunc = func; - setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ - ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ - luaD_call(L, c.func, nresults); /* do the call */ - ci->callstatus &= ~CIST_YPCALL; - L->errfunc = ci->u.c.old_errfunc; - status = LUA_OK; /* if it is here, there were no errors */ - } - adjustresults(L, nresults); - lua_unlock(L); - return status; -} - - -LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, - const char *chunkname, const char *mode) { - ZIO z; - int status; - lua_lock(L); - if (!chunkname) chunkname = "?"; - luaZ_init(L, &z, reader, data); - status = luaD_protectedparser(L, &z, chunkname, mode); - if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(L->top - 1); /* get newly created function */ - if (f->nupvalues >= 1) { /* does it have an upvalue? */ - /* get global table from registry */ - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); - luaC_upvalbarrier(L, f->upvals[0]); - } - } - lua_unlock(L); - return status; -} - - -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { - int status; - TValue *o; - lua_lock(L); - api_checknelems(L, 1); - o = L->top - 1; - if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, strip); - else - status = 1; - lua_unlock(L); - return status; -} - - -LUA_API int lua_status (lua_State *L) { - return L->status; -} - - -/* -** Garbage-collection function -*/ - -LUA_API int lua_gc (lua_State *L, int what, int data) { - int res = 0; - global_State *g; - lua_lock(L); - g = G(L); - switch (what) { - case LUA_GCSTOP: { - g->gcrunning = 0; - break; - } - case LUA_GCRESTART: { - luaE_setdebt(g, 0); - g->gcrunning = 1; - break; - } - case LUA_GCCOLLECT: { - luaC_fullgc(L, 0); - break; - } - case LUA_GCCOUNT: { - /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(gettotalbytes(g) >> 10); - break; - } - case LUA_GCCOUNTB: { - res = cast_int(gettotalbytes(g) & 0x3ff); - break; - } - case LUA_GCSTEP: { - l_mem debt = 1; /* =1 to signal that it did an actual step */ - lu_byte oldrunning = g->gcrunning; - g->gcrunning = 1; /* allow GC to run */ - if (data == 0) { - luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ - luaC_step(L); - } - else { /* add 'data' to total debt */ - debt = cast(l_mem, data) * 1024 + g->GCdebt; - luaE_setdebt(g, debt); - luaC_checkGC(L); - } - g->gcrunning = oldrunning; /* restore previous state */ - if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ - res = 1; /* signal it */ - break; - } - case LUA_GCSETPAUSE: { - res = g->gcpause; - g->gcpause = data; - break; - } - case LUA_GCSETSTEPMUL: { - res = g->gcstepmul; - if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ - g->gcstepmul = data; - break; - } - case LUA_GCISRUNNING: { - res = g->gcrunning; - break; - } - default: res = -1; /* invalid option */ - } - lua_unlock(L); - return res; -} - - - -/* -** miscellaneous functions -*/ - - -LUA_API int lua_error (lua_State *L) { - lua_lock(L); - api_checknelems(L, 1); - luaG_errormsg(L); - /* code unreachable; will unlock when control actually leaves the kernel */ - return 0; /* to avoid warnings */ -} - - -LUA_API int lua_next (lua_State *L, int idx) { - StkId t; - int more; - lua_lock(L); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - more = luaH_next(L, hvalue(t), L->top - 1); - if (more) { - api_incr_top(L); - } - else /* no more elements */ - L->top -= 1; /* remove key */ - lua_unlock(L); - return more; -} - - -LUA_API void lua_concat (lua_State *L, int n) { - lua_lock(L); - api_checknelems(L, n); - if (n >= 2) { - luaV_concat(L, n); - } - else if (n == 0) { /* push empty string */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); - api_incr_top(L); - } - /* else n == 1; nothing to do */ - luaC_checkGC(L); - lua_unlock(L); -} - - -LUA_API void lua_len (lua_State *L, int idx) { - StkId t; - lua_lock(L); - t = index2addr(L, idx); - luaV_objlen(L, L->top, t); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { - lua_Alloc f; - lua_lock(L); - if (ud) *ud = G(L)->ud; - f = G(L)->frealloc; - lua_unlock(L); - return f; -} - - -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { - lua_lock(L); - G(L)->ud = ud; - G(L)->frealloc = f; - lua_unlock(L); -} - - -LUA_API void *lua_newuserdata (lua_State *L, size_t size) { - Udata *u; - lua_lock(L); - u = luaS_newudata(L, size); - setuvalue(L, L->top, u); - api_incr_top(L); - luaC_checkGC(L); - lua_unlock(L); - return getudatamem(u); -} - - - -static const char *aux_upvalue (StkId fi, int n, TValue **val, - CClosure **owner, UpVal **uv) { - switch (ttype(fi)) { - case LUA_TCCL: { /* C closure */ - CClosure *f = clCvalue(fi); - if (!(1 <= n && n <= f->nupvalues)) return NULL; - *val = &f->upvalue[n-1]; - if (owner) *owner = f; - return ""; - } - case LUA_TLCL: { /* Lua closure */ - LClosure *f = clLvalue(fi); - TString *name; - Proto *p = f->p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; - *val = f->upvals[n-1]->v; - if (uv) *uv = f->upvals[n - 1]; - name = p->upvalues[n-1].name; - return (name == NULL) ? "(*no name)" : getstr(name); - } - default: return NULL; /* not a closure */ - } -} - - -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val = NULL; /* to avoid warnings */ - lua_lock(L); - name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL); - if (name) { - setobj2s(L, L->top, val); - api_incr_top(L); - } - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val = NULL; /* to avoid warnings */ - CClosure *owner = NULL; - UpVal *uv = NULL; - StkId fi; - lua_lock(L); - fi = index2addr(L, funcindex); - api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner, &uv); - if (name) { - L->top--; - setobj(L, val, L->top); - if (owner) { luaC_barrier(L, owner, L->top); } - else if (uv) { luaC_upvalbarrier(L, uv); } - } - lua_unlock(L); - return name; -} - - -static UpVal **getupvalref (lua_State *L, int fidx, int n) { - LClosure *f; - StkId fi = index2addr(L, fidx); - api_check(L, ttisLclosure(fi), "Lua function expected"); - f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); - return &f->upvals[n - 1]; /* get its upvalue pointer */ -} - - -LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { - StkId fi = index2addr(L, fidx); - switch (ttype(fi)) { - case LUA_TLCL: { /* lua closure */ - return *getupvalref(L, fidx, n); - } - case LUA_TCCL: { /* C closure */ - CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); - return &f->upvalue[n - 1]; - } - default: { - api_check(L, 0, "closure expected"); - return NULL; - } - } -} - - -LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, - int fidx2, int n2) { - UpVal **up1 = getupvalref(L, fidx1, n1); - UpVal **up2 = getupvalref(L, fidx2, n2); - if (*up1 == *up2) - return; - luaC_upvdeccount(L, *up1); - *up1 = *up2; - (*up1)->refcount++; - if (upisopen(*up1)) (*up1)->u.open.touched = 1; - luaC_upvalbarrier(L, *up1); -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.h deleted file mode 100644 index 8e16ad5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lapi.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $ -** Auxiliary functions from Lua API -** See Copyright Notice in lua.h -*/ - -#ifndef lapi_h -#define lapi_h - - -#include "llimits.h" -#include "lstate.h" - -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ - "stack overflow");} - -#define adjustresults(L,nres) \ - { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } - -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ - "not enough elements in the stack") - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.c deleted file mode 100644 index ac68bd3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.c +++ /dev/null @@ -1,1048 +0,0 @@ -/* -** $Id: lauxlib.c,v 1.289.1.1 2017/04/19 17:20:42 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - -#define lauxlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include - - -/* -** This file uses only the official API of Lua. -** Any function declared here could be written as an application function. -*/ - -#include "lua.h" - -#include "lauxlib.h" - - -/* -** {====================================================== -** Traceback -** ======================================================= -*/ - - -#define LEVELS1 10 /* size of the first part of the stack */ -#define LEVELS2 11 /* size of the second part of the stack */ - - - -/* -** search for 'objidx' in table at index -1. -** return 1 + string at top if find a good name. -*/ -static int findfield (lua_State *L, int objidx, int level) { - if (level == 0 || !lua_istable(L, -1)) - return 0; /* not found */ - lua_pushnil(L); /* start 'next' loop */ - while (lua_next(L, -2)) { /* for each pair in table */ - if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ - if (lua_rawequal(L, objidx, -1)) { /* found object? */ - lua_pop(L, 1); /* remove value (but keep name) */ - return 1; - } - else if (findfield(L, objidx, level - 1)) { /* try recursively */ - lua_remove(L, -2); /* remove table (but keep name) */ - lua_pushliteral(L, "."); - lua_insert(L, -2); /* place '.' between the two names */ - lua_concat(L, 3); - return 1; - } - } - lua_pop(L, 1); /* remove value */ - } - return 0; /* not found */ -} - - -/* -** Search for a name for a function in all loaded modules -*/ -static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { - int top = lua_gettop(L); - lua_getinfo(L, "f", ar); /* push function */ - lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - if (findfield(L, top + 1, 2)) { - const char *name = lua_tostring(L, -1); - if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ - lua_pushstring(L, name + 3); /* push name without prefix */ - lua_remove(L, -2); /* remove original name */ - } - lua_copy(L, -1, top + 1); /* move name to proper place */ - lua_pop(L, 2); /* remove pushed values */ - return 1; - } - else { - lua_settop(L, top); /* remove function and global table */ - return 0; - } -} - - -static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (pushglobalfuncname(L, ar)) { /* try first a global name */ - lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else if (*ar->namewhat != '\0') /* is there a name from code? */ - lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ - else if (*ar->what == 'm') /* main? */ - lua_pushliteral(L, "main chunk"); - else if (*ar->what != 'C') /* for Lua functions, use */ - lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); - else /* nothing left... */ - lua_pushliteral(L, "?"); -} - - -static int lastlevel (lua_State *L) { - lua_Debug ar; - int li = 1, le = 1; - /* find an upper bound */ - while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } - /* do a binary search */ - while (li < le) { - int m = (li + le)/2; - if (lua_getstack(L, m, &ar)) li = m + 1; - else le = m; - } - return le - 1; -} - - -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, - const char *msg, int level) { - lua_Debug ar; - int top = lua_gettop(L); - int last = lastlevel(L1); - int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; - if (msg) - lua_pushfstring(L, "%s\n", msg); - luaL_checkstack(L, 10, NULL); - lua_pushliteral(L, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - if (n1-- == 0) { /* too many levels? */ - lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = last - LEVELS2 + 1; /* and skip to last ones */ - } - else { - lua_getinfo(L1, "Slnt", &ar); - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - lua_pushliteral(L, " in "); - pushfuncname(L, &ar); - if (ar.istailcall) - lua_pushliteral(L, "\n\t(...tail calls...)"); - lua_concat(L, lua_gettop(L) - top); - } - } - lua_concat(L, lua_gettop(L) - top); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Error-report functions -** ======================================================= -*/ - -LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { - lua_Debug ar; - if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ - return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); - lua_getinfo(L, "n", &ar); - if (strcmp(ar.namewhat, "method") == 0) { - arg--; /* do not count 'self' */ - if (arg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling '%s' on bad self (%s)", - ar.name, extramsg); - } - if (ar.name == NULL) - ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to '%s' (%s)", - arg, ar.name, extramsg); -} - - -static int typeerror (lua_State *L, int arg, const char *tname) { - const char *msg; - const char *typearg; /* name for the type of the actual argument */ - if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) - typearg = lua_tostring(L, -1); /* use the given type name */ - else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) - typearg = "light userdata"; /* special name for messages */ - else - typearg = luaL_typename(L, arg); /* standard name */ - msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); - return luaL_argerror(L, arg, msg); -} - - -static void tag_error (lua_State *L, int arg, int tag) { - typeerror(L, arg, lua_typename(L, tag)); -} - - -/* -** The use of 'lua_pushfstring' ensures this function does not -** need reserved stack space when called. -*/ -LUALIB_API void luaL_where (lua_State *L, int level) { - lua_Debug ar; - if (lua_getstack(L, level, &ar)) { /* check function at level */ - lua_getinfo(L, "Sl", &ar); /* get info about it */ - if (ar.currentline > 0) { /* is there info? */ - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); - return; - } - } - lua_pushfstring(L, ""); /* else, no information available... */ -} - - -/* -** Again, the use of 'lua_pushvfstring' ensures this function does -** not need reserved stack space when called. (At worst, it generates -** an error with "stack overflow" instead of the given message.) -*/ -LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - luaL_where(L, 1); - lua_pushvfstring(L, fmt, argp); - va_end(argp); - lua_concat(L, 2); - return lua_error(L); -} - - -LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { - int en = errno; /* calls to Lua API may change this value */ - if (stat) { - lua_pushboolean(L, 1); - return 1; - } - else { - lua_pushnil(L); - if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); - else - lua_pushstring(L, strerror(en)); - lua_pushinteger(L, en); - return 3; - } -} - - -#if !defined(l_inspectstat) /* { */ - -#if defined(LUA_USE_POSIX) - -#include - -/* -** use appropriate macros to interpret 'pclose' return status -*/ -#define l_inspectstat(stat,what) \ - if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ - else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } - -#else - -#define l_inspectstat(stat,what) /* no op */ - -#endif - -#endif /* } */ - - -LUALIB_API int luaL_execresult (lua_State *L, int stat) { - const char *what = "exit"; /* type of termination */ - if (stat == -1) /* error? */ - return luaL_fileresult(L, 0, NULL); - else { - l_inspectstat(stat, what); /* interpret result */ - if (*what == 'e' && stat == 0) /* successful termination? */ - lua_pushboolean(L, 1); - else - lua_pushnil(L); - lua_pushstring(L, what); - lua_pushinteger(L, stat); - return 3; /* return true/nil,what,code */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Userdata's metatable manipulation -** ======================================================= -*/ - -LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ - return 0; /* leave previous value on top, but return 0 */ - lua_pop(L, 1); - lua_createtable(L, 0, 2); /* create metatable */ - lua_pushstring(L, tname); - lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ - return 1; -} - - -LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { - luaL_getmetatable(L, tname); - lua_setmetatable(L, -2); -} - - -LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); - if (p != NULL) { /* value is a userdata? */ - if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ - luaL_getmetatable(L, tname); /* get correct metatable */ - if (!lua_rawequal(L, -1, -2)) /* not the same? */ - p = NULL; /* value is a userdata with wrong metatable */ - lua_pop(L, 2); /* remove both metatables */ - return p; - } - } - return NULL; /* value is not a userdata with a metatable */ -} - - -LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - void *p = luaL_testudata(L, ud, tname); - if (p == NULL) typeerror(L, ud, tname); - return p; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Argument check functions -** ======================================================= -*/ - -LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, - const char *const lst[]) { - const char *name = (def) ? luaL_optstring(L, arg, def) : - luaL_checkstring(L, arg); - int i; - for (i=0; lst[i]; i++) - if (strcmp(lst[i], name) == 0) - return i; - return luaL_argerror(L, arg, - lua_pushfstring(L, "invalid option '%s'", name)); -} - - -/* -** Ensures the stack has at least 'space' extra slots, raising an error -** if it cannot fulfill the request. (The error handling needs a few -** extra slots to format the error message. In case of an error without -** this extra space, Lua will generate the same 'stack overflow' error, -** but without 'msg'.) -*/ -LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - if (!lua_checkstack(L, space)) { - if (msg) - luaL_error(L, "stack overflow (%s)", msg); - else - luaL_error(L, "stack overflow"); - } -} - - -LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { - if (lua_type(L, arg) != t) - tag_error(L, arg, t); -} - - -LUALIB_API void luaL_checkany (lua_State *L, int arg) { - if (lua_type(L, arg) == LUA_TNONE) - luaL_argerror(L, arg, "value expected"); -} - - -LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { - const char *s = lua_tolstring(L, arg, len); - if (!s) tag_error(L, arg, LUA_TSTRING); - return s; -} - - -LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, - const char *def, size_t *len) { - if (lua_isnoneornil(L, arg)) { - if (len) - *len = (def ? strlen(def) : 0); - return def; - } - else return luaL_checklstring(L, arg, len); -} - - -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { - int isnum; - lua_Number d = lua_tonumberx(L, arg, &isnum); - if (!isnum) - tag_error(L, arg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { - return luaL_opt(L, luaL_checknumber, arg, def); -} - - -static void interror (lua_State *L, int arg) { - if (lua_isnumber(L, arg)) - luaL_argerror(L, arg, "number has no integer representation"); - else - tag_error(L, arg, LUA_TNUMBER); -} - - -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { - int isnum; - lua_Integer d = lua_tointegerx(L, arg, &isnum); - if (!isnum) { - interror(L, arg); - } - return d; -} - - -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, - lua_Integer def) { - return luaL_opt(L, luaL_checkinteger, arg, def); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - -/* userdata to box arbitrary data */ -typedef struct UBox { - void *box; - size_t bsize; -} UBox; - - -static void *resizebox (lua_State *L, int idx, size_t newsize) { - void *ud; - lua_Alloc allocf = lua_getallocf(L, &ud); - UBox *box = (UBox *)lua_touserdata(L, idx); - void *temp = allocf(ud, box->box, box->bsize, newsize); - if (temp == NULL && newsize > 0) { /* allocation error? */ - resizebox(L, idx, 0); /* free buffer */ - luaL_error(L, "not enough memory for buffer allocation"); - } - box->box = temp; - box->bsize = newsize; - return temp; -} - - -static int boxgc (lua_State *L) { - resizebox(L, 1, 0); - return 0; -} - - -static void *newbox (lua_State *L, size_t newsize) { - UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox)); - box->box = NULL; - box->bsize = 0; - if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */ - lua_pushcfunction(L, boxgc); - lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */ - } - lua_setmetatable(L, -2); - return resizebox(L, -1, newsize); -} - - -/* -** check whether buffer is using a userdata on the stack as a temporary -** buffer -*/ -#define buffonstack(B) ((B)->b != (B)->initb) - - -/* -** returns a pointer to a free area with at least 'sz' bytes -*/ -LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { - lua_State *L = B->L; - if (B->size - B->n < sz) { /* not enough space? */ - char *newbuff; - size_t newsize = B->size * 2; /* double buffer size */ - if (newsize - B->n < sz) /* not big enough? */ - newsize = B->n + sz; - if (newsize < B->n || newsize - B->n < sz) - luaL_error(L, "buffer too large"); - /* create larger buffer */ - if (buffonstack(B)) - newbuff = (char *)resizebox(L, -1, newsize); - else { /* no buffer yet */ - newbuff = (char *)newbox(L, newsize); - memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ - } - B->b = newbuff; - B->size = newsize; - } - return &B->b[B->n]; -} - - -LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ - char *b = luaL_prepbuffsize(B, l); - memcpy(b, s, l * sizeof(char)); - luaL_addsize(B, l); - } -} - - -LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { - luaL_addlstring(B, s, strlen(s)); -} - - -LUALIB_API void luaL_pushresult (luaL_Buffer *B) { - lua_State *L = B->L; - lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) { - resizebox(L, -2, 0); /* delete old buffer */ - lua_remove(L, -2); /* remove its header from the stack */ - } -} - - -LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { - luaL_addsize(B, sz); - luaL_pushresult(B); -} - - -LUALIB_API void luaL_addvalue (luaL_Buffer *B) { - lua_State *L = B->L; - size_t l; - const char *s = lua_tolstring(L, -1, &l); - if (buffonstack(B)) - lua_insert(L, -2); /* put value below buffer */ - luaL_addlstring(B, s, l); - lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */ -} - - -LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { - B->L = L; - B->b = B->initb; - B->n = 0; - B->size = LUAL_BUFFERSIZE; -} - - -LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { - luaL_buffinit(L, B); - return luaL_prepbuffsize(B, sz); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Reference system -** ======================================================= -*/ - -/* index of free-list header */ -#define freelist 0 - - -LUALIB_API int luaL_ref (lua_State *L, int t) { - int ref; - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* 'nil' has a unique fixed reference */ - } - t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ - lua_pop(L, 1); /* remove it from stack */ - if (ref != 0) { /* any free element? */ - lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ - } - else /* no free elements */ - ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ - lua_rawseti(L, t, ref); - return ref; -} - - -LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { - if (ref >= 0) { - t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); - lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ - lua_pushinteger(L, ref); - lua_rawseti(L, t, freelist); /* t[freelist] = ref */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Load functions -** ======================================================= -*/ - -typedef struct LoadF { - int n; /* number of pre-read characters */ - FILE *f; /* file being read */ - char buff[BUFSIZ]; /* area for reading file */ -} LoadF; - - -static const char *getF (lua_State *L, void *ud, size_t *size) { - LoadF *lf = (LoadF *)ud; - (void)L; /* not used */ - if (lf->n > 0) { /* are there pre-read characters to be read? */ - *size = lf->n; /* return them (chars already in buffer) */ - lf->n = 0; /* no more pre-read characters */ - } - else { /* read a block from file */ - /* 'fread' can return > 0 *and* set the EOF flag. If next call to - 'getF' called 'fread', it might still wait for user input. - The next check avoids this problem. */ - if (feof(lf->f)) return NULL; - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ - } - return lf->buff; -} - - -static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); - const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); - lua_remove(L, fnameindex); - return LUA_ERRFILE; -} - - -static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ -} - - -/* -** reads the first character of file 'f' and skips an optional BOM mark -** in its beginning plus its first line if it starts with '#'. Returns -** true if it skipped the first line. In any case, '*cp' has the -** first "valid" character of the file (after the optional BOM and -** a first-line comment). -*/ -static int skipcomment (LoadF *lf, int *cp) { - int c = *cp = skipBOM(lf); - if (c == '#') { /* first line is a comment (Unix exec. file)? */ - do { /* skip first line */ - c = getc(lf->f); - } while (c != EOF && c != '\n'); - *cp = getc(lf->f); /* skip end-of-line, if present */ - return 1; /* there was a comment */ - } - else return 0; /* no comment */ -} - - -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, - const char *mode) { - LoadF lf; - int status, readstatus; - int c; - int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ - if (filename == NULL) { - lua_pushliteral(L, "=stdin"); - lf.f = stdin; - } - else { - lua_pushfstring(L, "@%s", filename); - lf.f = fopen(filename, "r"); - if (lf.f == NULL) return errfile(L, "open", fnameindex); - } - if (skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(&lf, &c); /* re-read initial portion */ - } - if (c != EOF) - lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ - status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); - readstatus = ferror(lf.f); - if (filename) fclose(lf.f); /* close file (even in case of errors) */ - if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ - return errfile(L, "read", fnameindex); - } - lua_remove(L, fnameindex); - return status; -} - - -typedef struct LoadS { - const char *s; - size_t size; -} LoadS; - - -static const char *getS (lua_State *L, void *ud, size_t *size) { - LoadS *ls = (LoadS *)ud; - (void)L; /* not used */ - if (ls->size == 0) return NULL; - *size = ls->size; - ls->size = 0; - return ls->s; -} - - -LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, - const char *name, const char *mode) { - LoadS ls; - ls.s = buff; - ls.size = size; - return lua_load(L, getS, &ls, name, mode); -} - - -LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { - return luaL_loadbuffer(L, s, strlen(s), s); -} - -/* }====================================================== */ - - - -LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { - if (!lua_getmetatable(L, obj)) /* no metatable? */ - return LUA_TNIL; - else { - int tt; - lua_pushstring(L, event); - tt = lua_rawget(L, -2); - if (tt == LUA_TNIL) /* is metafield nil? */ - lua_pop(L, 2); /* remove metatable and metafield */ - else - lua_remove(L, -2); /* remove only metatable */ - return tt; /* return metafield type */ - } -} - - -LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { - obj = lua_absindex(L, obj); - if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ - return 0; - lua_pushvalue(L, obj); - lua_call(L, 1, 1); - return 1; -} - - -LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { - lua_Integer l; - int isnum; - lua_len(L, idx); - l = lua_tointegerx(L, -1, &isnum); - if (!isnum) - luaL_error(L, "object length is not an integer"); - lua_pop(L, 1); /* remove object */ - return l; -} - - -LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { - if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ - if (!lua_isstring(L, -1)) - luaL_error(L, "'__tostring' must return a string"); - } - else { - switch (lua_type(L, idx)) { - case LUA_TNUMBER: { - if (lua_isinteger(L, idx)) - lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); - else - lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); - break; - } - case LUA_TSTRING: - lua_pushvalue(L, idx); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); - break; - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - default: { - int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ - const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : - luaL_typename(L, idx); - lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); - if (tt != LUA_TNIL) - lua_remove(L, -2); /* remove '__name' */ - break; - } - } - } - return lua_tolstring(L, -1, len); -} - - -/* -** {====================================================== -** Compatibility with 5.1 module functions -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -static const char *luaL_findtable (lua_State *L, int idx, - const char *fname, int szhint) { - const char *e; - if (idx) lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, e - fname); - if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */ - lua_pop(L, 1); /* remove this nil */ - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ - lua_pushlstring(L, fname, e - fname); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } - else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - - -/* -** Count number of elements in a luaL_Reg list. -*/ -static int libsize (const luaL_Reg *l) { - int size = 0; - for (; l && l->name; l++) size++; - return size; -} - - -/* -** Find or create a module table with a given name. The function -** first looks at the LOADED table and, if that fails, try a -** global variable with that name. In any case, leaves on the stack -** the module table. -*/ -LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, - int sizehint) { - luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1); - if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - lua_pushglobaltable(L); - if (luaL_findtable(L, 0, modname, sizehint) != NULL) - luaL_error(L, "name conflict for module '%s'", modname); - lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); /* LOADED[modname] = new table */ - } - lua_remove(L, -2); /* remove LOADED table */ -} - - -LUALIB_API void luaL_openlib (lua_State *L, const char *libname, - const luaL_Reg *l, int nup) { - luaL_checkversion(L); - if (libname) { - luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ - lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ - } - if (l) - luaL_setfuncs(L, l, nup); - else - lua_pop(L, nup); /* remove upvalues */ -} - -#endif -/* }====================================================== */ - -/* -** set functions from list 'l' into table at top - 'nup'; each -** function gets the 'nup' elements at the top as upvalues. -** Returns with only the table at the stack. -*/ -LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - - -/* -** ensure that stack[idx][fname] has a table and push that table -** into the stack -*/ -LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { - if (lua_getfield(L, idx, fname) == LUA_TTABLE) - return 1; /* table already there */ - else { - lua_pop(L, 1); /* remove previous result */ - idx = lua_absindex(L, idx); - lua_newtable(L); - lua_pushvalue(L, -1); /* copy to be left at top */ - lua_setfield(L, idx, fname); /* assign new table to field */ - return 0; /* false, because did not find table there */ - } -} - - -/* -** Stripped-down 'require': After checking "loaded" table, calls 'openf' -** to open a module, registers the result in 'package.loaded' table and, -** if 'glb' is true, also registers the result in the global table. -** Leaves resulting module on the top. -*/ -LUALIB_API void luaL_requiref (lua_State *L, const char *modname, - lua_CFunction openf, int glb) { - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - lua_getfield(L, -1, modname); /* LOADED[modname] */ - if (!lua_toboolean(L, -1)) { /* package not already loaded? */ - lua_pop(L, 1); /* remove field */ - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); /* argument to open function */ - lua_call(L, 1, 1); /* call 'openf' to open module */ - lua_pushvalue(L, -1); /* make copy of module (call result) */ - lua_setfield(L, -3, modname); /* LOADED[modname] = module */ - } - lua_remove(L, -2); /* remove LOADED table */ - if (glb) { - lua_pushvalue(L, -1); /* copy of module */ - lua_setglobal(L, modname); /* _G[modname] = module */ - } -} - - -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, - const char *r) { - const char *wild; - size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); - while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, wild - s); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after 'p' */ - } - luaL_addstring(&b, s); /* push last suffix */ - luaL_pushresult(&b); - return lua_tostring(L, -1); -} - - -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { - (void)ud; (void)osize; /* not used */ - if (nsize == 0) { - free(ptr); - return NULL; - } - else { /* cannot fail when shrinking a block */ - void *newptr = realloc(ptr, nsize); - if (newptr == NULL && ptr != NULL && nsize <= osize) - return ptr; /* keep the original block */ - else /* no fail or not shrinking */ - return newptr; /* use the new block */ - } -} - - -static int panic (lua_State *L) { - lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); - return 0; /* return to Lua to abort */ -} - - -LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); - if (L) lua_atpanic(L, &panic); - return L; -} - - -LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { - const lua_Number *v = lua_version(L); - if (sz != LUAL_NUMSIZES) /* check numeric types */ - luaL_error(L, "core and library have incompatible numeric types"); - if (v != lua_version(NULL)) - luaL_error(L, "multiple Lua VMs detected"); - else if (*v != ver) - luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.h deleted file mode 100644 index 9857d3a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lauxlib.h +++ /dev/null @@ -1,264 +0,0 @@ -/* -** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lauxlib_h -#define lauxlib_h - - -#include -#include - -#include "lua.h" - - - -/* extra error code for 'luaL_loadfilex' */ -#define LUA_ERRFILE (LUA_ERRERR+1) - - -/* key, in the registry, for table of loaded modules */ -#define LUA_LOADED_TABLE "_LOADED" - - -/* key, in the registry, for table of preloaded loaders */ -#define LUA_PRELOAD_TABLE "_PRELOAD" - - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - - -#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) - -LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); -#define luaL_checkversion(L) \ - luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) - -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); -LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); -LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, - size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, - const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); - -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, - lua_Integer def); - -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int arg); - -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); -LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); - -LUALIB_API void (luaL_where) (lua_State *L, int lvl); -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); - -LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, - const char *const lst[]); - -LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); -LUALIB_API int (luaL_execresult) (lua_State *L, int stat); - -/* predefined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -LUALIB_API int (luaL_ref) (lua_State *L, int t); -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); - -LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, - const char *mode); - -#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) - -LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, - const char *name, const char *mode); -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); - -LUALIB_API lua_State *(luaL_newstate) (void); - -LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); - -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); - -LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); - -LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); - -LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, - const char *msg, int level); - -LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, - lua_CFunction openf, int glb); - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - - -#define luaL_newlibtable(L,l) \ - lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) - -#define luaL_newlib(L,l) \ - (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) - -#define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) - -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - -#define luaL_dofile(L, fn) \ - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_dostring(L, s) \ - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) - -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) - -#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - -typedef struct luaL_Buffer { - char *b; /* buffer address */ - size_t size; /* buffer size */ - size_t n; /* number of characters in buffer */ - lua_State *L; - char initb[LUAL_BUFFERSIZE]; /* initial buffer */ -} luaL_Buffer; - - -#define luaL_addchar(B,c) \ - ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ - ((B)->b[(B)->n++] = (c))) - -#define luaL_addsize(B,s) ((B)->n += (s)) - -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); -LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); -LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); - -#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) - -/* }====================================================== */ - - - -/* -** {====================================================== -** File handles for IO library -** ======================================================= -*/ - -/* -** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and -** initial structure 'luaL_Stream' (it may contain other fields -** after that initial structure). -*/ - -#define LUA_FILEHANDLE "FILE*" - - -typedef struct luaL_Stream { - FILE *f; /* stream (NULL for incompletely created streams) */ - lua_CFunction closef; /* to close stream (NULL for closed streams) */ -} luaL_Stream; - -/* }====================================================== */ - - - -/* compatibility with old module system */ -#if defined(LUA_COMPAT_MODULE) - -LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, - int sizehint); -LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); - -#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) - -#endif - - -/* -** {================================================================== -** "Abstraction Layer" for basic report of messages and errors -** =================================================================== -*/ - -/* print a string */ -#if !defined(lua_writestring) -#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#endif - -/* print a newline and flush the output */ -#if !defined(lua_writeline) -#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) -#endif - -/* print an error message */ -#if !defined(lua_writestringerror) -#define lua_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) -#endif - -/* }================================================================== */ - - -/* -** {============================================================ -** Compatibility with deprecated conversions -** ============================================================= -*/ -#if defined(LUA_COMPAT_APIINTCASTS) - -#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) -#define luaL_optunsigned(L,a,d) \ - ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) - -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) - -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) - -#endif -/* }============================================================ */ - - - -#endif - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbaselib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbaselib.c deleted file mode 100644 index 6460e4f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbaselib.c +++ /dev/null @@ -1,498 +0,0 @@ -/* -** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $ -** Basic library -** See Copyright Notice in lua.h -*/ - -#define lbaselib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, "'tostring' must return a string to 'print'"); - if (i>1) lua_writestring("\t", 1); - lua_writestring(s, l); - lua_pop(L, 1); /* pop result */ - } - lua_writeline(); - return 0; -} - - -#define SPACECHARS " \f\n\r\t\v" - -static const char *b_str2int (const char *s, int base, lua_Integer *pn) { - lua_Unsigned n = 0; - int neg = 0; - s += strspn(s, SPACECHARS); /* skip initial spaces */ - if (*s == '-') { s++; neg = 1; } /* handle signal */ - else if (*s == '+') s++; - if (!isalnum((unsigned char)*s)) /* no digit? */ - return NULL; - do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : (toupper((unsigned char)*s) - 'A') + 10; - if (digit >= base) return NULL; /* invalid numeral */ - n = n * base + digit; - s++; - } while (isalnum((unsigned char)*s)); - s += strspn(s, SPACECHARS); /* skip trailing spaces */ - *pn = (lua_Integer)((neg) ? (0u - n) : n); - return s; -} - - -static int luaB_tonumber (lua_State *L) { - if (lua_isnoneornil(L, 2)) { /* standard conversion? */ - luaL_checkany(L, 1); - if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ - lua_settop(L, 1); /* yes; return it */ - return 1; - } - else { - size_t l; - const char *s = lua_tolstring(L, 1, &l); - if (s != NULL && lua_stringtonumber(L, s) == l + 1) - return 1; /* successful conversion to number */ - /* else not a number */ - } - } - else { - size_t l; - const char *s; - lua_Integer n = 0; /* to avoid warnings */ - lua_Integer base = luaL_checkinteger(L, 2); - luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ - s = lua_tolstring(L, 1, &l); - luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - if (b_str2int(s, (int)base, &n) == s + l) { - lua_pushinteger(L, n); - return 1; - } /* else not a number */ - } /* else not a number */ - lua_pushnil(L); /* not a number */ - return 1; -} - - -static int luaB_error (lua_State *L) { - int level = (int)luaL_optinteger(L, 2, 1); - lua_settop(L, 1); - if (lua_type(L, 1) == LUA_TSTRING && level > 0) { - luaL_where(L, level); /* add extra information */ - lua_pushvalue(L, 1); - lua_concat(L, 2); - } - return lua_error(L); -} - - -static int luaB_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); - return 1; /* no metatable */ - } - luaL_getmetafield(L, 1, "__metatable"); - return 1; /* returns either __metatable field (if present) or metatable */ -} - - -static int luaB_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) - return luaL_error(L, "cannot change a protected metatable"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; -} - - -static int luaB_rawequal (lua_State *L) { - luaL_checkany(L, 1); - luaL_checkany(L, 2); - lua_pushboolean(L, lua_rawequal(L, 1, 2)); - return 1; -} - - -static int luaB_rawlen (lua_State *L) { - int t = lua_type(L, 1); - luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, - "table or string expected"); - lua_pushinteger(L, lua_rawlen(L, 1)); - return 1; -} - - -static int luaB_rawget (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - lua_settop(L, 2); - lua_rawget(L, 1); - return 1; -} - -static int luaB_rawset (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - luaL_checkany(L, 3); - lua_settop(L, 3); - lua_rawset(L, 1); - return 1; -} - - -static int luaB_collectgarbage (lua_State *L) { - static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setpause", "setstepmul", - "isrunning", NULL}; - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCISRUNNING}; - int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - int ex = (int)luaL_optinteger(L, 2, 0); - int res = lua_gc(L, o, ex); - switch (o) { - case LUA_GCCOUNT: { - int b = lua_gc(L, LUA_GCCOUNTB, 0); - lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024)); - return 1; - } - case LUA_GCSTEP: case LUA_GCISRUNNING: { - lua_pushboolean(L, res); - return 1; - } - default: { - lua_pushinteger(L, res); - return 1; - } - } -} - - -static int luaB_type (lua_State *L) { - int t = lua_type(L, 1); - luaL_argcheck(L, t != LUA_TNONE, 1, "value expected"); - lua_pushstring(L, lua_typename(L, t)); - return 1; -} - - -static int pairsmeta (lua_State *L, const char *method, int iszero, - lua_CFunction iter) { - luaL_checkany(L, 1); - if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ - lua_pushcfunction(L, iter); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - if (iszero) lua_pushinteger(L, 0); /* and initial value */ - else lua_pushnil(L); - } - else { - lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ - } - return 3; -} - - -static int luaB_next (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 2); /* create a 2nd argument if there isn't one */ - if (lua_next(L, 1)) - return 2; - else { - lua_pushnil(L); - return 1; - } -} - - -static int luaB_pairs (lua_State *L) { - return pairsmeta(L, "__pairs", 0, luaB_next); -} - - -/* -** Traversal function for 'ipairs' -*/ -static int ipairsaux (lua_State *L) { - lua_Integer i = luaL_checkinteger(L, 2) + 1; - lua_pushinteger(L, i); - return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; -} - - -/* -** 'ipairs' function. Returns 'ipairsaux', given "table", 0. -** (The given "table" may not be a table.) -*/ -static int luaB_ipairs (lua_State *L) { -#if defined(LUA_COMPAT_IPAIRS) - return pairsmeta(L, "__ipairs", 1, ipairsaux); -#else - luaL_checkany(L, 1); - lua_pushcfunction(L, ipairsaux); /* iteration function */ - lua_pushvalue(L, 1); /* state */ - lua_pushinteger(L, 0); /* initial value */ - return 3; -#endif -} - - -static int load_aux (lua_State *L, int status, int envidx) { - if (status == LUA_OK) { - if (envidx != 0) { /* 'env' parameter? */ - lua_pushvalue(L, envidx); /* environment for loaded function */ - if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ - lua_pop(L, 1); /* remove 'env' if not used by previous call */ - } - return 1; - } - else { /* error (message is on top of the stack) */ - lua_pushnil(L); - lua_insert(L, -2); /* put before error message */ - return 2; /* return nil plus error message */ - } -} - - -static int luaB_loadfile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); - int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); - return load_aux(L, status, env); -} - - -/* -** {====================================================== -** Generic Read function -** ======================================================= -*/ - - -/* -** reserved slot, above all arguments, to hold a copy of the returned -** string to avoid it being collected while parsed. 'load' has four -** optional arguments (chunk, source name, mode, and environment). -*/ -#define RESERVEDSLOT 5 - - -/* -** Reader for generic 'load' function: 'lua_load' uses the -** stack for internal stuff, so the reader cannot change the -** stack top. Instead, it keeps its resulting string in a -** reserved slot inside the stack. -*/ -static const char *generic_reader (lua_State *L, void *ud, size_t *size) { - (void)(ud); /* not used */ - luaL_checkstack(L, 2, "too many nested functions"); - lua_pushvalue(L, 1); /* get function */ - lua_call(L, 0, 1); /* call it */ - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* pop result */ - *size = 0; - return NULL; - } - else if (!lua_isstring(L, -1)) - luaL_error(L, "reader function must return a string"); - lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ - return lua_tolstring(L, RESERVEDSLOT, size); -} - - -static int luaB_load (lua_State *L) { - int status; - size_t l; - const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); - int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ - if (s != NULL) { /* loading a string? */ - const char *chunkname = luaL_optstring(L, 2, s); - status = luaL_loadbufferx(L, s, l, chunkname, mode); - } - else { /* loading from a reader function */ - const char *chunkname = luaL_optstring(L, 2, "=(load)"); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, RESERVEDSLOT); /* create reserved slot */ - status = lua_load(L, generic_reader, NULL, chunkname, mode); - } - return load_aux(L, status, env); -} - -/* }====================================================== */ - - -static int dofilecont (lua_State *L, int d1, lua_KContext d2) { - (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ - return lua_gettop(L) - 1; -} - - -static int luaB_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) - return lua_error(L); - lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L, 0, 0); -} - - -static int luaB_assert (lua_State *L) { - if (lua_toboolean(L, 1)) /* condition is true? */ - return lua_gettop(L); /* return all arguments */ - else { /* error */ - luaL_checkany(L, 1); /* there must be a condition */ - lua_remove(L, 1); /* remove it */ - lua_pushliteral(L, "assertion failed!"); /* default message */ - lua_settop(L, 1); /* leave only message (default if no other one) */ - return luaB_error(L); /* call 'error' */ - } -} - - -static int luaB_select (lua_State *L) { - int n = lua_gettop(L); - if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { - lua_pushinteger(L, n-1); - return 1; - } - else { - lua_Integer i = luaL_checkinteger(L, 1); - if (i < 0) i = n + i; - else if (i > n) i = n; - luaL_argcheck(L, 1 <= i, 1, "index out of range"); - return n - (int)i; - } -} - - -/* -** Continuation function for 'pcall' and 'xpcall'. Both functions -** already pushed a 'true' before doing the call, so in case of success -** 'finishpcall' only has to return everything in the stack minus -** 'extra' values (where 'extra' is exactly the number of items to be -** ignored). -*/ -static int finishpcall (lua_State *L, int status, lua_KContext extra) { - if (status != LUA_OK && status != LUA_YIELD) { /* error? */ - lua_pushboolean(L, 0); /* first result (false) */ - lua_pushvalue(L, -2); /* error message */ - return 2; /* return false, msg */ - } - else - return lua_gettop(L) - (int)extra; /* return all results */ -} - - -static int luaB_pcall (lua_State *L) { - int status; - luaL_checkany(L, 1); - lua_pushboolean(L, 1); /* first result if no errors */ - lua_insert(L, 1); /* put it in place */ - status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); - return finishpcall(L, status, 0); -} - - -/* -** Do a protected call with error handling. After 'lua_rotate', the -** stack will have ; so, the function passes -** 2 to 'finishpcall' to skip the 2 first values when returning results. -*/ -static int luaB_xpcall (lua_State *L) { - int status; - int n = lua_gettop(L); - luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ - lua_pushboolean(L, 1); /* first result */ - lua_pushvalue(L, 1); /* function */ - lua_rotate(L, 3, 2); /* move them below function's arguments */ - status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); - return finishpcall(L, status, 2); -} - - -static int luaB_tostring (lua_State *L) { - luaL_checkany(L, 1); - luaL_tolstring(L, 1, NULL); - return 1; -} - - -static const luaL_Reg base_funcs[] = { - {"assert", luaB_assert}, - {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, - {"error", luaB_error}, - {"getmetatable", luaB_getmetatable}, - {"ipairs", luaB_ipairs}, - {"loadfile", luaB_loadfile}, - {"load", luaB_load}, -#if defined(LUA_COMPAT_LOADSTRING) - {"loadstring", luaB_load}, -#endif - {"next", luaB_next}, - {"pairs", luaB_pairs}, - {"pcall", luaB_pcall}, - {"print", luaB_print}, - {"rawequal", luaB_rawequal}, - {"rawlen", luaB_rawlen}, - {"rawget", luaB_rawget}, - {"rawset", luaB_rawset}, - {"select", luaB_select}, - {"setmetatable", luaB_setmetatable}, - {"tonumber", luaB_tonumber}, - {"tostring", luaB_tostring}, - {"type", luaB_type}, - {"xpcall", luaB_xpcall}, - /* placeholders */ - {"_G", NULL}, - {"_VERSION", NULL}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_base (lua_State *L) { - /* open lib into global table */ - lua_pushglobaltable(L); - luaL_setfuncs(L, base_funcs, 0); - /* set global _G */ - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_G"); - /* set global _VERSION */ - lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, -2, "_VERSION"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbitlib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbitlib.c deleted file mode 100644 index 4786c0d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lbitlib.c +++ /dev/null @@ -1,233 +0,0 @@ -/* -** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ -** Standard library for bitwise operations -** See Copyright Notice in lua.h -*/ - -#define lbitlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#if defined(LUA_COMPAT_BITLIB) /* { */ - - -#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) -#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) - - -/* number of bits to consider in a number */ -#if !defined(LUA_NBITS) -#define LUA_NBITS 32 -#endif - - -/* -** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must -** be made in two parts to avoid problems when LUA_NBITS is equal to the -** number of bits in a lua_Unsigned.) -*/ -#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) - - -/* macro to trim extra bits */ -#define trim(x) ((x) & ALLONES) - - -/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ -#define mask(n) (~((ALLONES << 1) << ((n) - 1))) - - - -static lua_Unsigned andaux (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = ~(lua_Unsigned)0; - for (i = 1; i <= n; i++) - r &= checkunsigned(L, i); - return trim(r); -} - - -static int b_and (lua_State *L) { - lua_Unsigned r = andaux(L); - pushunsigned(L, r); - return 1; -} - - -static int b_test (lua_State *L) { - lua_Unsigned r = andaux(L); - lua_pushboolean(L, r != 0); - return 1; -} - - -static int b_or (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = 0; - for (i = 1; i <= n; i++) - r |= checkunsigned(L, i); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_xor (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = 0; - for (i = 1; i <= n; i++) - r ^= checkunsigned(L, i); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_not (lua_State *L) { - lua_Unsigned r = ~checkunsigned(L, 1); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { - if (i < 0) { /* shift right? */ - i = -i; - r = trim(r); - if (i >= LUA_NBITS) r = 0; - else r >>= i; - } - else { /* shift left */ - if (i >= LUA_NBITS) r = 0; - else r <<= i; - r = trim(r); - } - pushunsigned(L, r); - return 1; -} - - -static int b_lshift (lua_State *L) { - return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); -} - - -static int b_rshift (lua_State *L) { - return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); -} - - -static int b_arshift (lua_State *L) { - lua_Unsigned r = checkunsigned(L, 1); - lua_Integer i = luaL_checkinteger(L, 2); - if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) - return b_shift(L, r, -i); - else { /* arithmetic shift for 'negative' number */ - if (i >= LUA_NBITS) r = ALLONES; - else - r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ - pushunsigned(L, r); - return 1; - } -} - - -static int b_rot (lua_State *L, lua_Integer d) { - lua_Unsigned r = checkunsigned(L, 1); - int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ - r = trim(r); - if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ - r = (r << i) | (r >> (LUA_NBITS - i)); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_lrot (lua_State *L) { - return b_rot(L, luaL_checkinteger(L, 2)); -} - - -static int b_rrot (lua_State *L) { - return b_rot(L, -luaL_checkinteger(L, 2)); -} - - -/* -** get field and width arguments for field-manipulation functions, -** checking whether they are valid. -** ('luaL_error' called without 'return' to avoid later warnings about -** 'width' being used uninitialized.) -*/ -static int fieldargs (lua_State *L, int farg, int *width) { - lua_Integer f = luaL_checkinteger(L, farg); - lua_Integer w = luaL_optinteger(L, farg + 1, 1); - luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); - luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); - if (f + w > LUA_NBITS) - luaL_error(L, "trying to access non-existent bits"); - *width = (int)w; - return (int)f; -} - - -static int b_extract (lua_State *L) { - int w; - lua_Unsigned r = trim(checkunsigned(L, 1)); - int f = fieldargs(L, 2, &w); - r = (r >> f) & mask(w); - pushunsigned(L, r); - return 1; -} - - -static int b_replace (lua_State *L) { - int w; - lua_Unsigned r = trim(checkunsigned(L, 1)); - lua_Unsigned v = trim(checkunsigned(L, 2)); - int f = fieldargs(L, 3, &w); - lua_Unsigned m = mask(w); - r = (r & ~(m << f)) | ((v & m) << f); - pushunsigned(L, r); - return 1; -} - - -static const luaL_Reg bitlib[] = { - {"arshift", b_arshift}, - {"band", b_and}, - {"bnot", b_not}, - {"bor", b_or}, - {"bxor", b_xor}, - {"btest", b_test}, - {"extract", b_extract}, - {"lrotate", b_lrot}, - {"lshift", b_lshift}, - {"replace", b_replace}, - {"rrotate", b_rrot}, - {"rshift", b_rshift}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_bit32 (lua_State *L) { - luaL_newlib(L, bitlib); - return 1; -} - - -#else /* }{ */ - - -LUAMOD_API int luaopen_bit32 (lua_State *L) { - return luaL_error(L, "library 'bit32' has been deprecated"); -} - -#endif /* } */ diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.c deleted file mode 100644 index dc7271d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.c +++ /dev/null @@ -1,1203 +0,0 @@ -/* -** $Id: lcode.c,v 2.112.1.1 2017/04/19 17:20:42 roberto Exp $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - -#define lcode_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstring.h" -#include "ltable.h" -#include "lvm.h" - - -/* Maximum number of registers in a Lua function (must fit in 8 bits) */ -#define MAXREGS 255 - - -#define hasjumps(e) ((e)->t != (e)->f) - - -/* -** If expression is a numeric constant, fills 'v' with its value -** and returns 1. Otherwise, returns 0. -*/ -static int tonumeral(const expdesc *e, TValue *v) { - if (hasjumps(e)) - return 0; /* not a numeral */ - switch (e->k) { - case VKINT: - if (v) setivalue(v, e->u.ival); - return 1; - case VKFLT: - if (v) setfltvalue(v, e->u.nval); - return 1; - default: return 0; - } -} - - -/* -** Create a OP_LOADNIL instruction, but try to optimize: if the previous -** instruction is also OP_LOADNIL and ranges are compatible, adjust -** range of previous instruction instead of emitting a new one. (For -** instance, 'local a; local b' will generate a single opcode.) -*/ -void luaK_nil (FuncState *fs, int from, int n) { - Instruction *previous; - int l = from + n - 1; /* last register to set nil */ - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->code[fs->pc-1]; - if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ - int pfrom = GETARG_A(*previous); /* get previous range */ - int pl = pfrom + GETARG_B(*previous); - if ((pfrom <= from && from <= pl + 1) || - (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ - if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ - if (pl > l) l = pl; /* l = max(l, pl) */ - SETARG_A(*previous, from); - SETARG_B(*previous, l - from); - return; - } - } /* else go through */ - } - luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ -} - - -/* -** Gets the destination address of a jump instruction. Used to traverse -** a list of jumps. -*/ -static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); - if (offset == NO_JUMP) /* point to itself represents end of list */ - return NO_JUMP; /* end of list */ - else - return (pc+1)+offset; /* turn offset into absolute position */ -} - - -/* -** Fix jump instruction at position 'pc' to jump to 'dest'. -** (Jump addresses are relative in Lua) -*/ -static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->code[pc]; - int offset = dest - (pc + 1); - lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) - luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); -} - - -/* -** Concatenate jump-list 'l2' into jump-list 'l1' -*/ -void luaK_concat (FuncState *fs, int *l1, int l2) { - if (l2 == NO_JUMP) return; /* nothing to concatenate? */ - else if (*l1 == NO_JUMP) /* no original list? */ - *l1 = l2; /* 'l1' points to 'l2' */ - else { - int list = *l1; - int next; - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ - list = next; - fixjump(fs, list, l2); /* last element links to 'l2' */ - } -} - - -/* -** Create a jump instruction and return its position, so its destination -** can be fixed later (with 'fixjump'). If there are jumps to -** this position (kept in 'jpc'), link them all together so that -** 'patchlistaux' will fix all them directly to the final destination. -*/ -int luaK_jump (FuncState *fs) { - int jpc = fs->jpc; /* save list of jumps to here */ - int j; - fs->jpc = NO_JUMP; /* no more jumps to here */ - j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); - luaK_concat(fs, &j, jpc); /* keep them on hold */ - return j; -} - - -/* -** Code a 'return' instruction -*/ -void luaK_ret (FuncState *fs, int first, int nret) { - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); -} - - -/* -** Code a "conditional jump", that is, a test or comparison opcode -** followed by a jump. Return jump position. -*/ -static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { - luaK_codeABC(fs, op, A, B, C); - return luaK_jump(fs); -} - - -/* -** returns current 'pc' and marks it as a jump target (to avoid wrong -** optimizations with consecutive instructions not in the same basic block). -*/ -int luaK_getlabel (FuncState *fs) { - fs->lasttarget = fs->pc; - return fs->pc; -} - - -/* -** Returns the position of the instruction "controlling" a given -** jump (that is, its condition), or the jump itself if it is -** unconditional. -*/ -static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->code[pc]; - if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) - return pi-1; - else - return pi; -} - - -/* -** Patch destination register for a TESTSET instruction. -** If instruction in position 'node' is not a TESTSET, return 0 ("fails"). -** Otherwise, if 'reg' is not 'NO_REG', set it as the destination -** register. Otherwise, change instruction to a simple 'TEST' (produces -** no register value) -*/ -static int patchtestreg (FuncState *fs, int node, int reg) { - Instruction *i = getjumpcontrol(fs, node); - if (GET_OPCODE(*i) != OP_TESTSET) - return 0; /* cannot patch other instructions */ - if (reg != NO_REG && reg != GETARG_B(*i)) - SETARG_A(*i, reg); - else { - /* no register to put value or register already has the value; - change instruction to simple test */ - *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); - } - return 1; -} - - -/* -** Traverse a list of tests ensuring no one produces a value -*/ -static void removevalues (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) - patchtestreg(fs, list, NO_REG); -} - - -/* -** Traverse a list of tests, patching their destination address and -** registers: tests producing values jump to 'vtarget' (and put their -** values in 'reg'), other tests jump to 'dtarget'. -*/ -static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, - int dtarget) { - while (list != NO_JUMP) { - int next = getjump(fs, list); - if (patchtestreg(fs, list, reg)) - fixjump(fs, list, vtarget); - else - fixjump(fs, list, dtarget); /* jump to default target */ - list = next; - } -} - - -/* -** Ensure all pending jumps to current position are fixed (jumping -** to current position with no values) and reset list of pending -** jumps -*/ -static void dischargejpc (FuncState *fs) { - patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); - fs->jpc = NO_JUMP; -} - - -/* -** Add elements in 'list' to list of pending jumps to "here" -** (current position) -*/ -void luaK_patchtohere (FuncState *fs, int list) { - luaK_getlabel(fs); /* mark "here" as a jump target */ - luaK_concat(fs, &fs->jpc, list); -} - - -/* -** Path all jumps in 'list' to jump to 'target'. -** (The assert means that we cannot fix a jump to a forward address -** because we only know addresses once code is generated.) -*/ -void luaK_patchlist (FuncState *fs, int list, int target) { - if (target == fs->pc) /* 'target' is current position? */ - luaK_patchtohere(fs, list); /* add list to pending jumps */ - else { - lua_assert(target < fs->pc); - patchlistaux(fs, list, target, NO_REG, target); - } -} - - -/* -** Path all jumps in 'list' to close upvalues up to given 'level' -** (The assertion checks that jumps either were closing nothing -** or were closing higher levels, from inner blocks.) -*/ -void luaK_patchclose (FuncState *fs, int list, int level) { - level++; /* argument is +1 to reserve 0 as non-op */ - for (; list != NO_JUMP; list = getjump(fs, list)) { - lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && - (GETARG_A(fs->f->code[list]) == 0 || - GETARG_A(fs->f->code[list]) >= level)); - SETARG_A(fs->f->code[list], level); - } -} - - -/* -** Emit instruction 'i', checking for array sizes and saving also its -** line information. Return 'i' position. -*/ -static int luaK_code (FuncState *fs, Instruction i) { - Proto *f = fs->f; - dischargejpc(fs); /* 'pc' will change */ - /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, - MAX_INT, "opcodes"); - f->code[fs->pc] = i; - /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, - MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; - return fs->pc++; -} - - -/* -** Format and emit an 'iABC' instruction. (Assertions check consistency -** of parameters versus opcode.) -*/ -int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { - lua_assert(getOpMode(o) == iABC); - lua_assert(getBMode(o) != OpArgN || b == 0); - lua_assert(getCMode(o) != OpArgN || c == 0); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); - return luaK_code(fs, CREATE_ABC(o, a, b, c)); -} - - -/* -** Format and emit an 'iABx' instruction. -*/ -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { - lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); - lua_assert(getCMode(o) == OpArgN); - lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, bc)); -} - - -/* -** Emit an "extra argument" instruction (format 'iAx') -*/ -static int codeextraarg (FuncState *fs, int a) { - lua_assert(a <= MAXARG_Ax); - return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); -} - - -/* -** Emit a "load constant" instruction, using either 'OP_LOADK' -** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' -** instruction with "extra argument". -*/ -int luaK_codek (FuncState *fs, int reg, int k) { - if (k <= MAXARG_Bx) - return luaK_codeABx(fs, OP_LOADK, reg, k); - else { - int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); - codeextraarg(fs, k); - return p; - } -} - - -/* -** Check register-stack level, keeping track of its maximum size -** in field 'maxstacksize' -*/ -void luaK_checkstack (FuncState *fs, int n) { - int newstack = fs->freereg + n; - if (newstack > fs->f->maxstacksize) { - if (newstack >= MAXREGS) - luaX_syntaxerror(fs->ls, - "function or expression needs too many registers"); - fs->f->maxstacksize = cast_byte(newstack); - } -} - - -/* -** Reserve 'n' registers in register stack -*/ -void luaK_reserveregs (FuncState *fs, int n) { - luaK_checkstack(fs, n); - fs->freereg += n; -} - - -/* -** Free register 'reg', if it is neither a constant index nor -** a local variable. -) -*/ -static void freereg (FuncState *fs, int reg) { - if (!ISK(reg) && reg >= fs->nactvar) { - fs->freereg--; - lua_assert(reg == fs->freereg); - } -} - - -/* -** Free register used by expression 'e' (if any) -*/ -static void freeexp (FuncState *fs, expdesc *e) { - if (e->k == VNONRELOC) - freereg(fs, e->u.info); -} - - -/* -** Free registers used by expressions 'e1' and 'e2' (if any) in proper -** order. -*/ -static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { - int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; - int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; - if (r1 > r2) { - freereg(fs, r1); - freereg(fs, r2); - } - else { - freereg(fs, r2); - freereg(fs, r1); - } -} - - -/* -** Add constant 'v' to prototype's list of constants (field 'k'). -** Use scanner's table to cache position of constants in constant list -** and try to reuse constants. Because some values should not be used -** as keys (nil cannot be a key, integer keys can collapse with float -** keys), the caller must provide a useful 'key' for indexing the cache. -*/ -static int addk (FuncState *fs, TValue *key, TValue *v) { - lua_State *L = fs->ls->L; - Proto *f = fs->f; - TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ - int k, oldsize; - if (ttisinteger(idx)) { /* is there an index there? */ - k = cast_int(ivalue(idx)); - /* correct value? (warning: must distinguish floats from integers!) */ - if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && - luaV_rawequalobj(&f->k[k], v)) - return k; /* reuse index */ - } - /* constant not found; create a new entry */ - oldsize = f->sizek; - k = fs->nk; - /* numerical value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ - setivalue(idx, k); - luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); - setobj(L, &f->k[k], v); - fs->nk++; - luaC_barrier(L, f, v); - return k; -} - - -/* -** Add a string to list of constants and return its index. -*/ -int luaK_stringK (FuncState *fs, TString *s) { - TValue o; - setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); /* use string itself as key */ -} - - -/* -** Add an integer to list of constants and return its index. -** Integers use userdata as keys to avoid collision with floats with -** same value; conversion to 'void*' is used only for hashing, so there -** are no "precision" problems. -*/ -int luaK_intK (FuncState *fs, lua_Integer n) { - TValue k, o; - setpvalue(&k, cast(void*, cast(size_t, n))); - setivalue(&o, n); - return addk(fs, &k, &o); -} - -/* -** Add a float to list of constants and return its index. -*/ -static int luaK_numberK (FuncState *fs, lua_Number r) { - TValue o; - setfltvalue(&o, r); - return addk(fs, &o, &o); /* use number itself as key */ -} - - -/* -** Add a boolean to list of constants and return its index. -*/ -static int boolK (FuncState *fs, int b) { - TValue o; - setbvalue(&o, b); - return addk(fs, &o, &o); /* use boolean itself as key */ -} - - -/* -** Add nil to list of constants and return its index. -*/ -static int nilK (FuncState *fs) { - TValue k, v; - setnilvalue(&v); - /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->ls->h); - return addk(fs, &k, &v); -} - - -/* -** Fix an expression to return the number of results 'nresults'. -** Either 'e' is a multi-ret expression (function call or vararg) -** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). -*/ -void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { - if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getinstruction(fs, e), nresults + 1); - } - else if (e->k == VVARARG) { - Instruction *pc = &getinstruction(fs, e); - SETARG_B(*pc, nresults + 1); - SETARG_A(*pc, fs->freereg); - luaK_reserveregs(fs, 1); - } - else lua_assert(nresults == LUA_MULTRET); -} - - -/* -** Fix an expression to return one result. -** If expression is not a multi-ret expression (function call or -** vararg), it already returns one result, so nothing needs to be done. -** Function calls become VNONRELOC expressions (as its result comes -** fixed in the base register of the call), while vararg expressions -** become VRELOCABLE (as OP_VARARG puts its results where it wants). -** (Calls are created returning one result, so that does not need -** to be fixed.) -*/ -void luaK_setoneret (FuncState *fs, expdesc *e) { - if (e->k == VCALL) { /* expression is an open function call? */ - /* already returns 1 value */ - lua_assert(GETARG_C(getinstruction(fs, e)) == 2); - e->k = VNONRELOC; /* result has fixed position */ - e->u.info = GETARG_A(getinstruction(fs, e)); - } - else if (e->k == VVARARG) { - SETARG_B(getinstruction(fs, e), 2); - e->k = VRELOCABLE; /* can relocate its simple result */ - } -} - - -/* -** Ensure that expression 'e' is not a variable. -*/ -void luaK_dischargevars (FuncState *fs, expdesc *e) { - switch (e->k) { - case VLOCAL: { /* already in a register */ - e->k = VNONRELOC; /* becomes a non-relocatable value */ - break; - } - case VUPVAL: { /* move value to some (pending) register */ - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); - e->k = VRELOCABLE; - break; - } - case VINDEXED: { - OpCode op; - freereg(fs, e->u.ind.idx); - if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */ - freereg(fs, e->u.ind.t); - op = OP_GETTABLE; - } - else { - lua_assert(e->u.ind.vt == VUPVAL); - op = OP_GETTABUP; /* 't' is in an upvalue */ - } - e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOCABLE; - break; - } - case VVARARG: case VCALL: { - luaK_setoneret(fs, e); - break; - } - default: break; /* there is one value available (somewhere) */ - } -} - - -/* -** Ensures expression value is in register 'reg' (and therefore -** 'e' will become a non-relocatable expression). -*/ -static void discharge2reg (FuncState *fs, expdesc *e, int reg) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: { - luaK_nil(fs, reg, 1); - break; - } - case VFALSE: case VTRUE: { - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); - break; - } - case VK: { - luaK_codek(fs, reg, e->u.info); - break; - } - case VKFLT: { - luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); - break; - } - case VKINT: { - luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); - break; - } - case VRELOCABLE: { - Instruction *pc = &getinstruction(fs, e); - SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ - break; - } - case VNONRELOC: { - if (reg != e->u.info) - luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); - break; - } - default: { - lua_assert(e->k == VJMP); - return; /* nothing to do... */ - } - } - e->u.info = reg; - e->k = VNONRELOC; -} - - -/* -** Ensures expression value is in any register. -*/ -static void discharge2anyreg (FuncState *fs, expdesc *e) { - if (e->k != VNONRELOC) { /* no fixed register yet? */ - luaK_reserveregs(fs, 1); /* get a register */ - discharge2reg(fs, e, fs->freereg-1); /* put value there */ - } -} - - -static int code_loadbool (FuncState *fs, int A, int b, int jump) { - luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); -} - - -/* -** check whether list has any jump that do not produce a value -** or produce an inverted value -*/ -static int need_value (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TESTSET) return 1; - } - return 0; /* not found */ -} - - -/* -** Ensures final expression result (including results from its jump -** lists) is in register 'reg'. -** If expression has jumps, need to patch these jumps either to -** its final position or to "load" instructions (for those tests -** that do not produce values). -*/ -static void exp2reg (FuncState *fs, expdesc *e, int reg) { - discharge2reg(fs, e, reg); - if (e->k == VJMP) /* expression itself is a test? */ - luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ - if (hasjumps(e)) { - int final; /* position after whole expression */ - int p_f = NO_JUMP; /* position of an eventual LOAD false */ - int p_t = NO_JUMP; /* position of an eventual LOAD true */ - if (need_value(fs, e->t) || need_value(fs, e->f)) { - int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_loadbool(fs, reg, 0, 1); - p_t = code_loadbool(fs, reg, 1, 0); - luaK_patchtohere(fs, fj); - } - final = luaK_getlabel(fs); - patchlistaux(fs, e->f, final, reg, p_f); - patchlistaux(fs, e->t, final, reg, p_t); - } - e->f = e->t = NO_JUMP; - e->u.info = reg; - e->k = VNONRELOC; -} - - -/* -** Ensures final expression result (including results from its jump -** lists) is in next available register. -*/ -void luaK_exp2nextreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - freeexp(fs, e); - luaK_reserveregs(fs, 1); - exp2reg(fs, e, fs->freereg - 1); -} - - -/* -** Ensures final expression result (including results from its jump -** lists) is in some (any) register and return that register. -*/ -int luaK_exp2anyreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - if (e->k == VNONRELOC) { /* expression already has a register? */ - if (!hasjumps(e)) /* no jumps? */ - return e->u.info; /* result is already in a register */ - if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ - exp2reg(fs, e, e->u.info); /* put final result in it */ - return e->u.info; - } - } - luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ - return e->u.info; -} - - -/* -** Ensures final expression result is either in a register or in an -** upvalue. -*/ -void luaK_exp2anyregup (FuncState *fs, expdesc *e) { - if (e->k != VUPVAL || hasjumps(e)) - luaK_exp2anyreg(fs, e); -} - - -/* -** Ensures final expression result is either in a register or it is -** a constant. -*/ -void luaK_exp2val (FuncState *fs, expdesc *e) { - if (hasjumps(e)) - luaK_exp2anyreg(fs, e); - else - luaK_dischargevars(fs, e); -} - - -/* -** Ensures final expression result is in a valid R/K index -** (that is, it is either in a register or in 'k' with an index -** in the range of R/K indices). -** Returns R/K index. -*/ -int luaK_exp2RK (FuncState *fs, expdesc *e) { - luaK_exp2val(fs, e); - switch (e->k) { /* move constants to 'k' */ - case VTRUE: e->u.info = boolK(fs, 1); goto vk; - case VFALSE: e->u.info = boolK(fs, 0); goto vk; - case VNIL: e->u.info = nilK(fs); goto vk; - case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk; - case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk; - case VK: - vk: - e->k = VK; - if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ - return RKASK(e->u.info); - else break; - default: break; - } - /* not a constant in the right range: put it in a register */ - return luaK_exp2anyreg(fs, e); -} - - -/* -** Generate code to store result of expression 'ex' into variable 'var'. -*/ -void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { - switch (var->k) { - case VLOCAL: { - freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ - return; - } - case VUPVAL: { - int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); - break; - } - case VINDEXED: { - OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP; - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); - break; - } - default: lua_assert(0); /* invalid var kind to store */ - } - freeexp(fs, ex); -} - - -/* -** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). -*/ -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { - int ereg; - luaK_exp2anyreg(fs, e); - ereg = e->u.info; /* register where 'e' was placed */ - freeexp(fs, e); - e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; /* self expression has a fixed register */ - luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ - luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); - freeexp(fs, key); -} - - -/* -** Negate condition 'e' (where 'e' is a comparison). -*/ -static void negatecondition (FuncState *fs, expdesc *e) { - Instruction *pc = getjumpcontrol(fs, e->u.info); - lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && - GET_OPCODE(*pc) != OP_TEST); - SETARG_A(*pc, !(GETARG_A(*pc))); -} - - -/* -** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' -** is true, code will jump if 'e' is true.) Return jump position. -** Optimize when 'e' is 'not' something, inverting the condition -** and removing the 'not'. -*/ -static int jumponcond (FuncState *fs, expdesc *e, int cond) { - if (e->k == VRELOCABLE) { - Instruction ie = getinstruction(fs, e); - if (GET_OPCODE(ie) == OP_NOT) { - fs->pc--; /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); - } - /* else go through */ - } - discharge2anyreg(fs, e); - freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); -} - - -/* -** Emit code to go through if 'e' is true, jump otherwise. -*/ -void luaK_goiftrue (FuncState *fs, expdesc *e) { - int pc; /* pc of new jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VJMP: { /* condition? */ - negatecondition(fs, e); /* jump when it is false */ - pc = e->u.info; /* save jump position */ - break; - } - case VK: case VKFLT: case VKINT: case VTRUE: { - pc = NO_JUMP; /* always true; do nothing */ - break; - } - default: { - pc = jumponcond(fs, e, 0); /* jump when false */ - break; - } - } - luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ - luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ - e->t = NO_JUMP; -} - - -/* -** Emit code to go through if 'e' is false, jump otherwise. -*/ -void luaK_goiffalse (FuncState *fs, expdesc *e) { - int pc; /* pc of new jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VJMP: { - pc = e->u.info; /* already jump if true */ - break; - } - case VNIL: case VFALSE: { - pc = NO_JUMP; /* always false; do nothing */ - break; - } - default: { - pc = jumponcond(fs, e, 1); /* jump if true */ - break; - } - } - luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ - luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ - e->f = NO_JUMP; -} - - -/* -** Code 'not e', doing constant folding. -*/ -static void codenot (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: case VFALSE: { - e->k = VTRUE; /* true == not nil == not false */ - break; - } - case VK: case VKFLT: case VKINT: case VTRUE: { - e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ - break; - } - case VJMP: { - negatecondition(fs, e); - break; - } - case VRELOCABLE: - case VNONRELOC: { - discharge2anyreg(fs, e); - freeexp(fs, e); - e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); - e->k = VRELOCABLE; - break; - } - default: lua_assert(0); /* cannot happen */ - } - /* interchange true and false lists */ - { int temp = e->f; e->f = e->t; e->t = temp; } - removevalues(fs, e->f); /* values are useless when negated */ - removevalues(fs, e->t); -} - - -/* -** Create expression 't[k]'. 't' must have its final result already in a -** register or upvalue. -*/ -void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); - t->u.ind.t = t->u.info; /* register or upvalue index */ - t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */ - t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL; - t->k = VINDEXED; -} - - -/* -** Return false if folding can raise an error. -** Bitwise operations need operands convertible to integers; division -** operations cannot have 0 as divisor. -*/ -static int validop (int op, TValue *v1, TValue *v2) { - switch (op) { - case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: - case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ - lua_Integer i; - return (tointeger(v1, &i) && tointeger(v2, &i)); - } - case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ - return (nvalue(v2) != 0); - default: return 1; /* everything else is valid */ - } -} - - -/* -** Try to "constant-fold" an operation; return 1 iff successful. -** (In this case, 'e1' has the final result.) -*/ -static int constfolding (FuncState *fs, int op, expdesc *e1, - const expdesc *e2) { - TValue v1, v2, res; - if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) - return 0; /* non-numeric operands or not safe to fold */ - luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ - if (ttisinteger(&res)) { - e1->k = VKINT; - e1->u.ival = ivalue(&res); - } - else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ - lua_Number n = fltvalue(&res); - if (luai_numisnan(n) || n == 0) - return 0; - e1->k = VKFLT; - e1->u.nval = n; - } - return 1; -} - - -/* -** Emit code for unary expressions that "produce values" -** (everything but 'not'). -** Expression to produce final result will be encoded in 'e'. -*/ -static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { - int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ - freeexp(fs, e); - e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ - e->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); -} - - -/* -** Emit code for binary expressions that "produce values" -** (everything but logical operators 'and'/'or' and comparison -** operators). -** Expression to produce final result will be encoded in 'e1'. -** Because 'luaK_exp2RK' can free registers, its calls must be -** in "stack order" (that is, first on 'e2', which may have more -** recent registers to be released). -*/ -static void codebinexpval (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ - int rk1 = luaK_exp2RK(fs, e1); - freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); -} - - -/* -** Emit code for comparisons. -** 'e1' was already put in R/K form by 'luaK_infix'. -*/ -static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { - int rk1 = (e1->k == VK) ? RKASK(e1->u.info) - : check_exp(e1->k == VNONRELOC, e1->u.info); - int rk2 = luaK_exp2RK(fs, e2); - freeexps(fs, e1, e2); - switch (opr) { - case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */ - e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2); - break; - } - case OPR_GT: case OPR_GE: { - /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ - OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); - e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */ - break; - } - default: { /* '==', '<', '<=' use their own opcodes */ - OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - e1->u.info = condjump(fs, op, 1, rk1, rk2); - break; - } - } - e1->k = VJMP; -} - - -/* -** Apply prefix operation 'op' to expression 'e'. -*/ -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { - static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; - switch (op) { - case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, op + LUA_OPUNM, e, &ef)) - break; - /* FALLTHROUGH */ - case OPR_LEN: - codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); - break; - case OPR_NOT: codenot(fs, e); break; - default: lua_assert(0); - } -} - - -/* -** Process 1st operand 'v' of binary operation 'op' before reading -** 2nd operand. -*/ -void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { - switch (op) { - case OPR_AND: { - luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ - break; - } - case OPR_OR: { - luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ - break; - } - case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ - break; - } - case OPR_ADD: case OPR_SUB: - case OPR_MUL: case OPR_DIV: case OPR_IDIV: - case OPR_MOD: case OPR_POW: - case OPR_BAND: case OPR_BOR: case OPR_BXOR: - case OPR_SHL: case OPR_SHR: { - if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); - /* else keep numeral, which may be folded with 2nd operand */ - break; - } - default: { - luaK_exp2RK(fs, v); - break; - } - } -} - - -/* -** Finalize code for binary operation, after reading 2nd operand. -** For '(a .. b .. c)' (which is '(a .. (b .. c))', because -** concatenation is right associative), merge second CONCAT into first -** one. -*/ -void luaK_posfix (FuncState *fs, BinOpr op, - expdesc *e1, expdesc *e2, int line) { - switch (op) { - case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); - luaK_concat(fs, &e2->f, e1->f); - *e1 = *e2; - break; - } - case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); - luaK_concat(fs, &e2->t, e1->t); - *e1 = *e2; - break; - } - case OPR_CONCAT: { - luaK_exp2val(fs, e2); - if (e2->k == VRELOCABLE && - GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) { - lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1); - freeexp(fs, e1); - SETARG_B(getinstruction(fs, e2), e1->u.info); - e1->k = VRELOCABLE; e1->u.info = e2->u.info; - } - else { - luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codebinexpval(fs, OP_CONCAT, e1, e2, line); - } - break; - } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_IDIV: case OPR_MOD: case OPR_POW: - case OPR_BAND: case OPR_BOR: case OPR_BXOR: - case OPR_SHL: case OPR_SHR: { - if (!constfolding(fs, op + LUA_OPADD, e1, e2)) - codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line); - break; - } - case OPR_EQ: case OPR_LT: case OPR_LE: - case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, op, e1, e2); - break; - } - default: lua_assert(0); - } -} - - -/* -** Change line information associated with current position. -*/ -void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; -} - - -/* -** Emit a SETLIST instruction. -** 'base' is register that keeps table; -** 'nelems' is #table plus those to be stored now; -** 'tostore' is number of values (in registers 'base + 1',...) to add to -** table (or LUA_MULTRET to add up to stack top). -*/ -void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; - int b = (tostore == LUA_MULTRET) ? 0 : tostore; - lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); - if (c <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, b, c); - else if (c <= MAXARG_Ax) { - luaK_codeABC(fs, OP_SETLIST, base, b, 0); - codeextraarg(fs, c); - } - else - luaX_syntaxerror(fs->ls, "constructor too long"); - fs->freereg = base + 1; /* free registers with list values */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.h deleted file mode 100644 index 882dc9c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcode.h +++ /dev/null @@ -1,88 +0,0 @@ -/* -** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lcode_h -#define lcode_h - -#include "llex.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" - - -/* -** Marks the end of a patch list. It is an invalid value both as an absolute -** address, and as a list link (would link an element to itself). -*/ -#define NO_JUMP (-1) - - -/* -** grep "ORDER OPR" if you change these enums (ORDER OP) -*/ -typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, - OPR_DIV, - OPR_IDIV, - OPR_BAND, OPR_BOR, OPR_BXOR, - OPR_SHL, OPR_SHR, - OPR_CONCAT, - OPR_EQ, OPR_LT, OPR_LE, - OPR_NE, OPR_GT, OPR_GE, - OPR_AND, OPR_OR, - OPR_NOBINOPR -} BinOpr; - - -typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; - - -/* get (pointer to) instruction of given 'expdesc' */ -#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) - -#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) - -#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) - -#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) - -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); -LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); -LUAI_FUNC void luaK_fixline (FuncState *fs, int line); -LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); -LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); -LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); -LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); -LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); -LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); -LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); -LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); -LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_jump (FuncState *fs); -LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); -LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); -LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); -LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); -LUAI_FUNC int luaK_getlabel (FuncState *fs); -LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); -LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); -LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, - expdesc *v2, int line); -LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcorolib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcorolib.c deleted file mode 100644 index 0b17af9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lcorolib.c +++ /dev/null @@ -1,168 +0,0 @@ -/* -** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $ -** Coroutine Library -** See Copyright Notice in lua.h -*/ - -#define lcorolib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static lua_State *getco (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "thread expected"); - return co; -} - - -static int auxresume (lua_State *L, lua_State *co, int narg) { - int status; - if (!lua_checkstack(co, narg)) { - lua_pushliteral(L, "too many arguments to resume"); - return -1; /* error flag */ - } - if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { - lua_pushliteral(L, "cannot resume dead coroutine"); - return -1; /* error flag */ - } - lua_xmove(L, co, narg); - status = lua_resume(co, L, narg); - if (status == LUA_OK || status == LUA_YIELD) { - int nres = lua_gettop(co); - if (!lua_checkstack(L, nres + 1)) { - lua_pop(co, nres); /* remove results anyway */ - lua_pushliteral(L, "too many results to resume"); - return -1; /* error flag */ - } - lua_xmove(co, L, nres); /* move yielded values */ - return nres; - } - else { - lua_xmove(co, L, 1); /* move error message */ - return -1; /* error flag */ - } -} - - -static int luaB_coresume (lua_State *L) { - lua_State *co = getco(L); - int r; - r = auxresume(L, co, lua_gettop(L) - 1); - if (r < 0) { - lua_pushboolean(L, 0); - lua_insert(L, -2); - return 2; /* return false + error message */ - } - else { - lua_pushboolean(L, 1); - lua_insert(L, -(r + 1)); - return r + 1; /* return true + 'resume' returns */ - } -} - - -static int luaB_auxwrap (lua_State *L) { - lua_State *co = lua_tothread(L, lua_upvalueindex(1)); - int r = auxresume(L, co, lua_gettop(L)); - if (r < 0) { - if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ - luaL_where(L, 1); /* add extra info */ - lua_insert(L, -2); - lua_concat(L, 2); - } - return lua_error(L); /* propagate error */ - } - return r; -} - - -static int luaB_cocreate (lua_State *L) { - lua_State *NL; - luaL_checktype(L, 1, LUA_TFUNCTION); - NL = lua_newthread(L); - lua_pushvalue(L, 1); /* move function to top */ - lua_xmove(L, NL, 1); /* move function from L to NL */ - return 1; -} - - -static int luaB_cowrap (lua_State *L) { - luaB_cocreate(L); - lua_pushcclosure(L, luaB_auxwrap, 1); - return 1; -} - - -static int luaB_yield (lua_State *L) { - return lua_yield(L, lua_gettop(L)); -} - - -static int luaB_costatus (lua_State *L) { - lua_State *co = getco(L); - if (L == co) lua_pushliteral(L, "running"); - else { - switch (lua_status(co)) { - case LUA_YIELD: - lua_pushliteral(L, "suspended"); - break; - case LUA_OK: { - lua_Debug ar; - if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ - lua_pushliteral(L, "normal"); /* it is running */ - else if (lua_gettop(co) == 0) - lua_pushliteral(L, "dead"); - else - lua_pushliteral(L, "suspended"); /* initial state */ - break; - } - default: /* some error occurred */ - lua_pushliteral(L, "dead"); - break; - } - } - return 1; -} - - -static int luaB_yieldable (lua_State *L) { - lua_pushboolean(L, lua_isyieldable(L)); - return 1; -} - - -static int luaB_corunning (lua_State *L) { - int ismain = lua_pushthread(L); - lua_pushboolean(L, ismain); - return 2; -} - - -static const luaL_Reg co_funcs[] = { - {"create", luaB_cocreate}, - {"resume", luaB_coresume}, - {"running", luaB_corunning}, - {"status", luaB_costatus}, - {"wrap", luaB_cowrap}, - {"yield", luaB_yield}, - {"isyieldable", luaB_yieldable}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_coroutine (lua_State *L) { - luaL_newlib(L, co_funcs); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.c deleted file mode 100644 index f8ad7a2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.c +++ /dev/null @@ -1,55 +0,0 @@ -/* -** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $ -** 'ctype' functions for Lua -** See Copyright Notice in lua.h -*/ - -#define lctype_c -#define LUA_CORE - -#include "lprefix.h" - - -#include "lctype.h" - -#if !LUA_USE_CTYPE /* { */ - -#include - -LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { - 0x00, /* EOZ */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ - 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ - 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ - 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, - 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ - 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -}; - -#endif /* } */ diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.h deleted file mode 100644 index b09b21a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lctype.h +++ /dev/null @@ -1,95 +0,0 @@ -/* -** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ -** 'ctype' functions for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lctype_h -#define lctype_h - -#include "lua.h" - - -/* -** WARNING: the functions defined here do not necessarily correspond -** to the similar functions in the standard C ctype.h. They are -** optimized for the specific needs of Lua -*/ - -#if !defined(LUA_USE_CTYPE) - -#if 'A' == 65 && '0' == 48 -/* ASCII case: can use its own tables; faster and fixed */ -#define LUA_USE_CTYPE 0 -#else -/* must use standard C ctype */ -#define LUA_USE_CTYPE 1 -#endif - -#endif - - -#if !LUA_USE_CTYPE /* { */ - -#include - -#include "llimits.h" - - -#define ALPHABIT 0 -#define DIGITBIT 1 -#define PRINTBIT 2 -#define SPACEBIT 3 -#define XDIGITBIT 4 - - -#define MASK(B) (1 << (B)) - - -/* -** add 1 to char to allow index -1 (EOZ) -*/ -#define testprop(c,p) (luai_ctype_[(c)+1] & (p)) - -/* -** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' -*/ -#define lislalpha(c) testprop(c, MASK(ALPHABIT)) -#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) -#define lisdigit(c) testprop(c, MASK(DIGITBIT)) -#define lisspace(c) testprop(c, MASK(SPACEBIT)) -#define lisprint(c) testprop(c, MASK(PRINTBIT)) -#define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) - -/* -** this 'ltolower' only works for alphabetic characters -*/ -#define ltolower(c) ((c) | ('A' ^ 'a')) - - -/* two more entries for 0 and -1 (EOZ) */ -LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; - - -#else /* }{ */ - -/* -** use standard C ctypes -*/ - -#include - - -#define lislalpha(c) (isalpha(c) || (c) == '_') -#define lislalnum(c) (isalnum(c) || (c) == '_') -#define lisdigit(c) (isdigit(c)) -#define lisspace(c) (isspace(c)) -#define lisprint(c) (isprint(c)) -#define lisxdigit(c) (isxdigit(c)) - -#define ltolower(c) (tolower(c)) - -#endif /* } */ - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldblib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldblib.c deleted file mode 100644 index 9d29afb..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldblib.c +++ /dev/null @@ -1,456 +0,0 @@ -/* -** $Id: ldblib.c,v 1.151.1.1 2017/04/19 17:20:42 roberto Exp $ -** Interface from Lua to its debug API -** See Copyright Notice in lua.h -*/ - -#define ldblib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** The hook table at registry[&HOOKKEY] maps threads to their current -** hook function. (We only need the unique address of 'HOOKKEY'.) -*/ -static const int HOOKKEY = 0; - - -/* -** If L1 != L, L1 can be in any state, and therefore there are no -** guarantees about its stack space; any push in L1 must be -** checked. -*/ -static void checkstack (lua_State *L, lua_State *L1, int n) { - if (L != L1 && !lua_checkstack(L1, n)) - luaL_error(L, "stack overflow"); -} - - -static int db_getregistry (lua_State *L) { - lua_pushvalue(L, LUA_REGISTRYINDEX); - return 1; -} - - -static int db_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); /* no metatable */ - } - return 1; -} - - -static int db_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; /* return 1st argument */ -} - - -static int db_getuservalue (lua_State *L) { - if (lua_type(L, 1) != LUA_TUSERDATA) - lua_pushnil(L); - else - lua_getuservalue(L, 1); - return 1; -} - - -static int db_setuservalue (lua_State *L) { - luaL_checktype(L, 1, LUA_TUSERDATA); - luaL_checkany(L, 2); - lua_settop(L, 2); - lua_setuservalue(L, 1); - return 1; -} - - -/* -** Auxiliary function used by several library functions: check for -** an optional thread as function's first argument and set 'arg' with -** 1 if this argument is present (so that functions can skip it to -** access their other arguments) -*/ -static lua_State *getthread (lua_State *L, int *arg) { - if (lua_isthread(L, 1)) { - *arg = 1; - return lua_tothread(L, 1); - } - else { - *arg = 0; - return L; /* function will operate over current thread */ - } -} - - -/* -** Variations of 'lua_settable', used by 'db_getinfo' to put results -** from 'lua_getinfo' into result table. Key is always a string; -** value can be a string, an int, or a boolean. -*/ -static void settabss (lua_State *L, const char *k, const char *v) { - lua_pushstring(L, v); - lua_setfield(L, -2, k); -} - -static void settabsi (lua_State *L, const char *k, int v) { - lua_pushinteger(L, v); - lua_setfield(L, -2, k); -} - -static void settabsb (lua_State *L, const char *k, int v) { - lua_pushboolean(L, v); - lua_setfield(L, -2, k); -} - - -/* -** In function 'db_getinfo', the call to 'lua_getinfo' may push -** results on the stack; later it creates the result table to put -** these objects. Function 'treatstackoption' puts the result from -** 'lua_getinfo' on top of the result table so that it can call -** 'lua_setfield'. -*/ -static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { - if (L == L1) - lua_rotate(L, -2, 1); /* exchange object and table */ - else - lua_xmove(L1, L, 1); /* move object to the "main" stack */ - lua_setfield(L, -2, fname); /* put object into table */ -} - - -/* -** Calls 'lua_getinfo' and collects all results in a new table. -** L1 needs stack space for an optional input (function) plus -** two optional outputs (function and line table) from function -** 'lua_getinfo'. -*/ -static int db_getinfo (lua_State *L) { - lua_Debug ar; - int arg; - lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnStu"); - checkstack(L, L1, 3); - if (lua_isfunction(L, arg + 1)) { /* info about a function? */ - options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ - lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ - lua_xmove(L, L1, 1); - } - else { /* stack level */ - if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { - lua_pushnil(L); /* level out of range */ - return 1; - } - } - if (!lua_getinfo(L1, options, &ar)) - return luaL_argerror(L, arg+2, "invalid option"); - lua_newtable(L); /* table to collect results */ - if (strchr(options, 'S')) { - settabss(L, "source", ar.source); - settabss(L, "short_src", ar.short_src); - settabsi(L, "linedefined", ar.linedefined); - settabsi(L, "lastlinedefined", ar.lastlinedefined); - settabss(L, "what", ar.what); - } - if (strchr(options, 'l')) - settabsi(L, "currentline", ar.currentline); - if (strchr(options, 'u')) { - settabsi(L, "nups", ar.nups); - settabsi(L, "nparams", ar.nparams); - settabsb(L, "isvararg", ar.isvararg); - } - if (strchr(options, 'n')) { - settabss(L, "name", ar.name); - settabss(L, "namewhat", ar.namewhat); - } - if (strchr(options, 't')) - settabsb(L, "istailcall", ar.istailcall); - if (strchr(options, 'L')) - treatstackoption(L, L1, "activelines"); - if (strchr(options, 'f')) - treatstackoption(L, L1, "func"); - return 1; /* return table */ -} - - -static int db_getlocal (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; - int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ - if (lua_isfunction(L, arg + 1)) { /* function argument? */ - lua_pushvalue(L, arg + 1); /* push function */ - lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ - return 1; /* return only name (there is no value) */ - } - else { /* stack-level argument */ - int level = (int)luaL_checkinteger(L, arg + 1); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - checkstack(L, L1, 1); - name = lua_getlocal(L1, &ar, nvar); - if (name) { - lua_xmove(L1, L, 1); /* move local value */ - lua_pushstring(L, name); /* push name */ - lua_rotate(L, -2, 1); /* re-order */ - return 2; - } - else { - lua_pushnil(L); /* no name (nor value) */ - return 1; - } - } -} - - -static int db_setlocal (lua_State *L) { - int arg; - const char *name; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - int level = (int)luaL_checkinteger(L, arg + 1); - int nvar = (int)luaL_checkinteger(L, arg + 2); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - luaL_checkany(L, arg+3); - lua_settop(L, arg+3); - checkstack(L, L1, 1); - lua_xmove(L, L1, 1); - name = lua_setlocal(L1, &ar, nvar); - if (name == NULL) - lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ - lua_pushstring(L, name); - return 1; -} - - -/* -** get (if 'get' is true) or set an upvalue from a closure -*/ -static int auxupvalue (lua_State *L, int get) { - const char *name; - int n = (int)luaL_checkinteger(L, 2); /* upvalue index */ - luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); - if (name == NULL) return 0; - lua_pushstring(L, name); - lua_insert(L, -(get+1)); /* no-op if get is false */ - return get + 1; -} - - -static int db_getupvalue (lua_State *L) { - return auxupvalue(L, 1); -} - - -static int db_setupvalue (lua_State *L) { - luaL_checkany(L, 3); - return auxupvalue(L, 0); -} - - -/* -** Check whether a given upvalue from a given closure exists and -** returns its index -*/ -static int checkupval (lua_State *L, int argf, int argnup) { - int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ - luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ - luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, - "invalid upvalue index"); - return nup; -} - - -static int db_upvalueid (lua_State *L) { - int n = checkupval(L, 1, 2); - lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); - return 1; -} - - -static int db_upvaluejoin (lua_State *L) { - int n1 = checkupval(L, 1, 2); - int n2 = checkupval(L, 3, 4); - luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); - luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); - lua_upvaluejoin(L, 1, n1, 3, n2); - return 0; -} - - -/* -** Call hook function registered at hook table for the current -** thread (if there is one) -*/ -static void hookf (lua_State *L, lua_Debug *ar) { - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail call"}; - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); - lua_pushthread(L); - if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ - lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ - if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); /* push current line */ - else lua_pushnil(L); - lua_assert(lua_getinfo(L, "lS", ar)); - lua_call(L, 2, 0); /* call hook function */ - } -} - - -/* -** Convert a string mask (for 'sethook') into a bit mask -*/ -static int makemask (const char *smask, int count) { - int mask = 0; - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; - if (strchr(smask, 'r')) mask |= LUA_MASKRET; - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; - if (count > 0) mask |= LUA_MASKCOUNT; - return mask; -} - - -/* -** Convert a bit mask (for 'gethook') into a string mask -*/ -static char *unmakemask (int mask, char *smask) { - int i = 0; - if (mask & LUA_MASKCALL) smask[i++] = 'c'; - if (mask & LUA_MASKRET) smask[i++] = 'r'; - if (mask & LUA_MASKLINE) smask[i++] = 'l'; - smask[i] = '\0'; - return smask; -} - - -static int db_sethook (lua_State *L) { - int arg, mask, count; - lua_Hook func; - lua_State *L1 = getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { /* no hook? */ - lua_settop(L, arg+1); - func = NULL; mask = 0; count = 0; /* turn off hooks */ - } - else { - const char *smask = luaL_checkstring(L, arg+2); - luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = (int)luaL_optinteger(L, arg + 3, 0); - func = hookf; mask = makemask(smask, count); - } - if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { - lua_createtable(L, 0, 2); /* create a hook table */ - lua_pushvalue(L, -1); - lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ - lua_pushstring(L, "k"); - lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ - lua_pushvalue(L, -1); - lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ - } - checkstack(L, L1, 1); - lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ - lua_pushvalue(L, arg + 1); /* value (hook function) */ - lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ - lua_sethook(L1, func, mask, count); - return 0; -} - - -static int db_gethook (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - char buff[5]; - int mask = lua_gethookmask(L1); - lua_Hook hook = lua_gethook(L1); - if (hook == NULL) /* no hook? */ - lua_pushnil(L); - else if (hook != hookf) /* external hook? */ - lua_pushliteral(L, "external hook"); - else { /* hook table must exist */ - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); - checkstack(L, L1, 1); - lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_rawget(L, -2); /* 1st result = hooktable[L1] */ - lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ - lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ - return 3; -} - - -static int db_debug (lua_State *L) { - for (;;) { - char buffer[250]; - lua_writestringerror("%s", "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || - strcmp(buffer, "cont\n") == 0) - return 0; - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || - lua_pcall(L, 0, 0, 0)) - lua_writestringerror("%s\n", lua_tostring(L, -1)); - lua_settop(L, 0); /* remove eventual returns */ - } -} - - -static int db_traceback (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - const char *msg = lua_tostring(L, arg + 1); - if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ - lua_pushvalue(L, arg + 1); /* return it untouched */ - else { - int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0); - luaL_traceback(L, L1, msg, level); - } - return 1; -} - - -static const luaL_Reg dblib[] = { - {"debug", db_debug}, - {"getuservalue", db_getuservalue}, - {"gethook", db_gethook}, - {"getinfo", db_getinfo}, - {"getlocal", db_getlocal}, - {"getregistry", db_getregistry}, - {"getmetatable", db_getmetatable}, - {"getupvalue", db_getupvalue}, - {"upvaluejoin", db_upvaluejoin}, - {"upvalueid", db_upvalueid}, - {"setuservalue", db_setuservalue}, - {"sethook", db_sethook}, - {"setlocal", db_setlocal}, - {"setmetatable", db_setmetatable}, - {"setupvalue", db_setupvalue}, - {"traceback", db_traceback}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_debug (lua_State *L) { - luaL_newlib(L, dblib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.c deleted file mode 100644 index bb0e1d4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.c +++ /dev/null @@ -1,700 +0,0 @@ -/* -** $Id: ldebug.c,v 2.121.1.2 2017/07/10 17:21:50 roberto Exp $ -** Debug Interface -** See Copyright Notice in lua.h -*/ - -#define ldebug_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - - -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) - - -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue((ci)->func)) - - -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name); - - -static int currentpc (CallInfo *ci) { - lua_assert(isLua(ci)); - return pcRel(ci->u.l.savedpc, ci_func(ci)->p); -} - - -static int currentline (CallInfo *ci) { - return getfuncline(ci_func(ci)->p, currentpc(ci)); -} - - -/* -** If function yielded, its 'func' can be in the 'extra' field. The -** next function restores 'func' to its correct value for debugging -** purposes. (It exchanges 'func' and 'extra'; so, when called again, -** after debugging, it also "re-restores" ** 'func' to its altered value. -*/ -static void swapextra (lua_State *L) { - if (L->status == LUA_YIELD) { - CallInfo *ci = L->ci; /* get function that yielded */ - StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ - ci->func = restorestack(L, ci->extra); - ci->extra = savestack(L, temp); - } -} - - -/* -** This function can be called asynchronously (e.g. during a signal). -** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by -** 'resethookcount') are for debug only, and it is no problem if they -** get arbitrary values (causes at most one wrong hook call). 'hookmask' -** is an atomic value. We assume that pointers are atomic too (e.g., gcc -** ensures that for all platforms where it runs). Moreover, 'hook' is -** always checked before being called (see 'luaD_hook'). -*/ -LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { - if (func == NULL || mask == 0) { /* turn off hooks? */ - mask = 0; - func = NULL; - } - if (isLua(L->ci)) - L->oldpc = L->ci->u.l.savedpc; - L->hook = func; - L->basehookcount = count; - resethookcount(L); - L->hookmask = cast_byte(mask); -} - - -LUA_API lua_Hook lua_gethook (lua_State *L) { - return L->hook; -} - - -LUA_API int lua_gethookmask (lua_State *L) { - return L->hookmask; -} - - -LUA_API int lua_gethookcount (lua_State *L) { - return L->basehookcount; -} - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { - int status; - CallInfo *ci; - if (level < 0) return 0; /* invalid (negative) level */ - lua_lock(L); - for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) - level--; - if (level == 0 && ci != &L->base_ci) { /* level found? */ - status = 1; - ar->i_ci = ci; - } - else status = 0; /* no such level */ - lua_unlock(L); - return status; -} - - -static const char *upvalname (Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); - if (s == NULL) return "?"; - else return getstr(s); -} - - -static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->numparams; - int nvararg = cast_int(ci->u.l.base - ci->func) - nparams; - if (n <= -nvararg) - return NULL; /* no such vararg */ - else { - *pos = ci->func + nparams - n; - return "(*vararg)"; /* generic name for any vararg */ - } -} - - -static const char *findlocal (lua_State *L, CallInfo *ci, int n, - StkId *pos) { - const char *name = NULL; - StkId base; - if (isLua(ci)) { - if (n < 0) /* access to vararg values? */ - return findvararg(ci, n, pos); - else { - base = ci->u.l.base; - name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); - } - } - else - base = ci->func + 1; - if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; - if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ - name = "(*temporary)"; /* generic name for any valid slot */ - else - return NULL; /* no name */ - } - *pos = base + (n - 1); - return name; -} - - -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { - const char *name; - lua_lock(L); - swapextra(L); - if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(L->top - 1)) /* not a Lua function? */ - name = NULL; - else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); - } - else { /* active function; get information through 'ar' */ - StkId pos = NULL; /* to avoid warnings */ - name = findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobj2s(L, L->top, pos); - api_incr_top(L); - } - } - swapextra(L); - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = NULL; /* to avoid warnings */ - const char *name; - lua_lock(L); - swapextra(L); - name = findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ - } - swapextra(L); - lua_unlock(L); - return name; -} - - -static void funcinfo (lua_Debug *ar, Closure *cl) { - if (noLuaClosure(cl)) { - ar->source = "=[C]"; - ar->linedefined = -1; - ar->lastlinedefined = -1; - ar->what = "C"; - } - else { - Proto *p = cl->l.p; - ar->source = p->source ? getstr(p->source) : "=?"; - ar->linedefined = p->linedefined; - ar->lastlinedefined = p->lastlinedefined; - ar->what = (ar->linedefined == 0) ? "main" : "Lua"; - } - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); -} - - -static void collectvalidlines (lua_State *L, Closure *f) { - if (noLuaClosure(f)) { - setnilvalue(L->top); - api_incr_top(L); - } - else { - int i; - TValue v; - int *lineinfo = f->l.p->lineinfo; - Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue(L, L->top, t); /* push it on stack */ - api_incr_top(L); - setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ - luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ - } -} - - -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - if (ci == NULL) /* no 'ci'? */ - return NULL; /* no info */ - else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ - *name = "__gc"; - return "metamethod"; /* report it as such */ - } - /* calling function is a known Lua function? */ - else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) - return funcnamefromcode(L, ci->previous, name); - else return NULL; /* no way to find a name */ -} - - -static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, - Closure *f, CallInfo *ci) { - int status = 1; - for (; *what; what++) { - switch (*what) { - case 'S': { - funcinfo(ar, f); - break; - } - case 'l': { - ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; - break; - } - case 'u': { - ar->nups = (f == NULL) ? 0 : f->c.nupvalues; - if (noLuaClosure(f)) { - ar->isvararg = 1; - ar->nparams = 0; - } - else { - ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams; - } - break; - } - case 't': { - ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; - break; - } - case 'n': { - ar->namewhat = getfuncname(L, ci, &ar->name); - if (ar->namewhat == NULL) { - ar->namewhat = ""; /* not found */ - ar->name = NULL; - } - break; - } - case 'L': - case 'f': /* handled by lua_getinfo */ - break; - default: status = 0; /* invalid option */ - } - } - return status; -} - - -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { - int status; - Closure *cl; - CallInfo *ci; - StkId func; - lua_lock(L); - swapextra(L); - if (*what == '>') { - ci = NULL; - func = L->top - 1; - api_check(L, ttisfunction(func), "function expected"); - what++; /* skip the '>' */ - L->top--; /* pop function */ - } - else { - ci = ar->i_ci; - func = ci->func; - lua_assert(ttisfunction(ci->func)); - } - cl = ttisclosure(func) ? clvalue(func) : NULL; - status = auxgetinfo(L, what, ar, cl, ci); - if (strchr(what, 'f')) { - setobjs2s(L, L->top, func); - api_incr_top(L); - } - swapextra(L); /* correct before option 'L', which can raise a mem. error */ - if (strchr(what, 'L')) - collectvalidlines(L, cl); - lua_unlock(L); - return status; -} - - -/* -** {====================================================== -** Symbolic Execution -** ======================================================= -*/ - -static const char *getobjname (Proto *p, int lastpc, int reg, - const char **name); - - -/* -** find a "name" for the RK value 'c' -*/ -static void kname (Proto *p, int pc, int c, const char **name) { - if (ISK(c)) { /* is 'c' a constant? */ - TValue *kvalue = &p->k[INDEXK(c)]; - if (ttisstring(kvalue)) { /* literal constant? */ - *name = svalue(kvalue); /* it is its own name */ - return; - } - /* else no reasonable name found */ - } - else { /* 'c' is a register */ - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (what && *what == 'c') { /* found a constant name? */ - return; /* 'name' already filled */ - } - /* else no reasonable name found */ - } - *name = "?"; /* no reasonable name found */ -} - - -static int filterpc (int pc, int jmptarget) { - if (pc < jmptarget) /* is code conditional (inside a jump)? */ - return -1; /* cannot know who sets that register */ - else return pc; /* current position sets that register */ -} - - -/* -** try to find last instruction before 'lastpc' that modified register 'reg' -*/ -static int findsetreg (Proto *p, int lastpc, int reg) { - int pc; - int setreg = -1; /* keep last instruction that changed 'reg' */ - int jmptarget = 0; /* any code before this address is conditional */ - for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); - int a = GETARG_A(i); - switch (op) { - case OP_LOADNIL: { - int b = GETARG_B(i); - if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ - setreg = filterpc(pc, jmptarget); - break; - } - case OP_TFORCALL: { - if (reg >= a + 2) /* affect all regs above its base */ - setreg = filterpc(pc, jmptarget); - break; - } - case OP_CALL: - case OP_TAILCALL: { - if (reg >= a) /* affect all registers above base */ - setreg = filterpc(pc, jmptarget); - break; - } - case OP_JMP: { - int b = GETARG_sBx(i); - int dest = pc + 1 + b; - /* jump is forward and do not skip 'lastpc'? */ - if (pc < dest && dest <= lastpc) { - if (dest > jmptarget) - jmptarget = dest; /* update 'jmptarget' */ - } - break; - } - default: - if (testAMode(op) && reg == a) /* any instruction that set A */ - setreg = filterpc(pc, jmptarget); - break; - } - } - return setreg; -} - - -static const char *getobjname (Proto *p, int lastpc, int reg, - const char **name) { - int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); - if (*name) /* is a local? */ - return "local"; - /* else try symbolic execution */ - pc = findsetreg(p, lastpc, reg); - if (pc != -1) { /* could find instruction? */ - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); - switch (op) { - case OP_MOVE: { - int b = GETARG_B(i); /* move from 'b' to 'a' */ - if (b < GETARG_A(i)) - return getobjname(p, pc, b, name); /* get name for 'b' */ - break; - } - case OP_GETTABUP: - case OP_GETTABLE: { - int k = GETARG_C(i); /* key index */ - int t = GETARG_B(i); /* table index */ - const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ - ? luaF_getlocalname(p, t + 1, pc) - : upvalname(p, t); - kname(p, pc, k, name); - return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; - } - case OP_GETUPVAL: { - *name = upvalname(p, GETARG_B(i)); - return "upvalue"; - } - case OP_LOADK: - case OP_LOADKX: { - int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); - if (ttisstring(&p->k[b])) { - *name = svalue(&p->k[b]); - return "constant"; - } - break; - } - case OP_SELF: { - int k = GETARG_C(i); /* key index */ - kname(p, pc, k, name); - return "method"; - } - default: break; /* go through to return NULL */ - } - } - return NULL; /* could not find reasonable name */ -} - - -/* -** Try to find a name for a function based on the code that called it. -** (Only works when function was called by a Lua function.) -** Returns what the name is (e.g., "for iterator", "method", -** "metamethod") and sets '*name' to point to the name. -*/ -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name) { - TMS tm = (TMS)0; /* (initial value avoids warnings) */ - Proto *p = ci_func(ci)->p; /* calling function */ - int pc = currentpc(ci); /* calling instruction index */ - Instruction i = p->code[pc]; /* calling instruction */ - if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ - *name = "?"; - return "hook"; - } - switch (GET_OPCODE(i)) { - case OP_CALL: - case OP_TAILCALL: - return getobjname(p, pc, GETARG_A(i), name); /* get function name */ - case OP_TFORCALL: { /* for iterator */ - *name = "for iterator"; - return "for iterator"; - } - /* other instructions can do calls through metamethods */ - case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: - tm = TM_INDEX; - break; - case OP_SETTABUP: case OP_SETTABLE: - tm = TM_NEWINDEX; - break; - case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: - case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: - case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { - int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */ - tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */ - break; - } - case OP_UNM: tm = TM_UNM; break; - case OP_BNOT: tm = TM_BNOT; break; - case OP_LEN: tm = TM_LEN; break; - case OP_CONCAT: tm = TM_CONCAT; break; - case OP_EQ: tm = TM_EQ; break; - case OP_LT: tm = TM_LT; break; - case OP_LE: tm = TM_LE; break; - default: - return NULL; /* cannot find a reasonable name */ - } - *name = getstr(G(L)->tmname[tm]); - return "metamethod"; -} - -/* }====================================================== */ - - - -/* -** The subtraction of two potentially unrelated pointers is -** not ISO C, but it should not crash a program; the subsequent -** checks are ISO C and ensure a correct result. -*/ -static int isinstack (CallInfo *ci, const TValue *o) { - ptrdiff_t i = o - ci->u.l.base; - return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o); -} - - -/* -** Checks whether value 'o' came from an upvalue. (That can only happen -** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on -** upvalues.) -*/ -static const char *getupvalname (CallInfo *ci, const TValue *o, - const char **name) { - LClosure *c = ci_func(ci); - int i; - for (i = 0; i < c->nupvalues; i++) { - if (c->upvals[i]->v == o) { - *name = upvalname(c->p, i); - return "upvalue"; - } - } - return NULL; -} - - -static const char *varinfo (lua_State *L, const TValue *o) { - const char *name = NULL; /* to avoid warnings */ - CallInfo *ci = L->ci; - const char *kind = NULL; - if (isLua(ci)) { - kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(o - ci->u.l.base), &name); - } - return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; -} - - -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { - const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); -} - - -l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { - if (ttisstring(p1) || cvt2str(p1)) p1 = p2; - luaG_typeerror(L, p1, "concatenate"); -} - - -l_noret luaG_opinterror (lua_State *L, const TValue *p1, - const TValue *p2, const char *msg) { - lua_Number temp; - if (!tonumber(p1, &temp)) /* first operand is wrong? */ - p2 = p1; /* now second is wrong */ - luaG_typeerror(L, p2, msg); -} - - -/* -** Error when both values are convertible to numbers, but not to integers -*/ -l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { - lua_Integer temp; - if (!tointeger(p1, &temp)) - p2 = p1; - luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); -} - - -l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { - const char *t1 = luaT_objtypename(L, p1); - const char *t2 = luaT_objtypename(L, p2); - if (strcmp(t1, t2) == 0) - luaG_runerror(L, "attempt to compare two %s values", t1); - else - luaG_runerror(L, "attempt to compare %s with %s", t1, t2); -} - - -/* add src:line information to 'msg' */ -const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, - int line) { - char buff[LUA_IDSIZE]; - if (src) - luaO_chunkid(buff, getstr(src), LUA_IDSIZE); - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; - } - return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); -} - - -l_noret luaG_errormsg (lua_State *L) { - if (L->errfunc != 0) { /* is there an error handling function? */ - StkId errfunc = restorestack(L, L->errfunc); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; /* assume EXTRA_STACK */ - luaD_callnoyield(L, L->top - 2, 1); /* call it */ - } - luaD_throw(L, LUA_ERRRUN); -} - - -l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { - CallInfo *ci = L->ci; - const char *msg; - va_list argp; - luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); - luaG_errormsg(L); -} - - -void luaG_traceexec (lua_State *L) { - CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; - int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); - if (counthook) - resethookcount(L); /* reset count */ - else if (!(mask & LUA_MASKLINE)) - return; /* no line hook and count != 0; nothing to be done */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ - ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ - return; /* do not call hook again (VM yielded, so it did not move) */ - } - if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(ci)->p; - int npc = pcRel(ci->u.l.savedpc, p); - int newline = getfuncline(p, npc); - if (npc == 0 || /* call linehook when enter a new function, */ - ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ - luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ - } - L->oldpc = ci->u.l.savedpc; - if (L->status == LUA_YIELD) { /* did hook yield? */ - if (counthook) - L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ - ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - ci->func = L->top - 1; /* protect stack below results */ - luaD_throw(L, LUA_YIELD); - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.h deleted file mode 100644 index 8cea0ee..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldebug.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $ -** Auxiliary functions from Debug Interface module -** See Copyright Notice in lua.h -*/ - -#ifndef ldebug_h -#define ldebug_h - - -#include "lstate.h" - - -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) - -#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) - -#define resethookcount(L) (L->hookcount = L->basehookcount) - - -LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, - const char *opname); -LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, - const TValue *p2, - const char *msg); -LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); -LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, - TString *src, int line); -LUAI_FUNC l_noret luaG_errormsg (lua_State *L); -LUAI_FUNC void luaG_traceexec (lua_State *L); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.c deleted file mode 100644 index 316e45c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.c +++ /dev/null @@ -1,802 +0,0 @@ -/* -** $Id: ldo.c,v 2.157.1.1 2017/04/19 17:20:42 roberto Exp $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#define ldo_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" -#include "lzio.h" - - - -#define errorstatus(s) ((s) > LUA_YIELD) - - -/* -** {====================================================== -** Error-recovery functions -** ======================================================= -*/ - -/* -** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By -** default, Lua handles errors with exceptions when compiling as -** C++ code, with _longjmp/_setjmp when asked to use them, and with -** longjmp/setjmp otherwise. -*/ -#if !defined(LUAI_THROW) /* { */ - -#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ - -/* C++ exceptions */ -#define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,a) \ - try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy variable */ - -#elif defined(LUA_USE_POSIX) /* }{ */ - -/* in POSIX, try _longjmp/_setjmp (more efficient) */ -#define LUAI_THROW(L,c) _longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#else /* }{ */ - -/* ISO C handling with long jumps */ -#define LUAI_THROW(L,c) longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#endif /* } */ - -#endif /* } */ - - - -/* chain list of long jump buffers */ -struct lua_longjmp { - struct lua_longjmp *previous; - luai_jmpbuf b; - volatile int status; /* error code */ -}; - - -static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - default: { - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ - break; - } - } - L->top = oldtop + 1; -} - - -l_noret luaD_throw (lua_State *L, int errcode) { - if (L->errorJmp) { /* thread has an error handler? */ - L->errorJmp->status = errcode; /* set status */ - LUAI_THROW(L, L->errorJmp); /* jump to it */ - } - else { /* thread has no error handler */ - global_State *g = G(L); - L->status = cast_byte(errcode); /* mark it as dead */ - if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ - luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ - } - else { /* no handler at all; abort */ - if (g->panic) { /* panic function? */ - seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ - if (L->ci->top < L->top) - L->ci->top = L->top; /* pushing msg. can break this invariant */ - lua_unlock(L); - g->panic(L); /* call panic function (last chance to jump out) */ - } - abort(); - } - } -} - - -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - unsigned short oldnCcalls = L->nCcalls; - struct lua_longjmp lj; - lj.status = LUA_OK; - lj.previous = L->errorJmp; /* chain new error handler */ - L->errorJmp = &lj; - LUAI_TRY(L, &lj, - (*f)(L, ud); - ); - L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls; - return lj.status; -} - -/* }====================================================== */ - - -/* -** {================================================================== -** Stack reallocation -** =================================================================== -*/ -static void correctstack (lua_State *L, TValue *oldstack) { - CallInfo *ci; - UpVal *up; - L->top = (L->top - oldstack) + L->stack; - for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = (up->v - oldstack) + L->stack; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + L->stack; - ci->func = (ci->func - oldstack) + L->stack; - if (isLua(ci)) - ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; - } -} - - -/* some space for error handling */ -#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) - - -void luaD_reallocstack (lua_State *L, int newsize) { - TValue *oldstack = L->stack; - int lim = L->stacksize; - lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); - for (; lim < newsize; lim++) - setnilvalue(L->stack + lim); /* erase new segment */ - L->stacksize = newsize; - L->stack_last = L->stack + newsize - EXTRA_STACK; - correctstack(L, oldstack); -} - - -void luaD_growstack (lua_State *L, int n) { - int size = L->stacksize; - if (size > LUAI_MAXSTACK) /* error after extra size? */ - luaD_throw(L, LUA_ERRERR); - else { - int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; - int newsize = 2 * size; - if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; - if (newsize < needed) newsize = needed; - if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ - luaD_reallocstack(L, ERRORSTACKSIZE); - luaG_runerror(L, "stack overflow"); - } - else - luaD_reallocstack(L, newsize); - } -} - - -static int stackinuse (lua_State *L) { - CallInfo *ci; - StkId lim = L->top; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top) lim = ci->top; - } - lua_assert(lim <= L->stack_last); - return cast_int(lim - L->stack) + 1; /* part of stack in use */ -} - - -void luaD_shrinkstack (lua_State *L) { - int inuse = stackinuse(L); - int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; - if (goodsize > LUAI_MAXSTACK) - goodsize = LUAI_MAXSTACK; /* respect stack limit */ - if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */ - luaE_freeCI(L); /* free all CIs (list grew because of an error) */ - else - luaE_shrinkCI(L); /* shrink list */ - /* if thread is currently not handling a stack overflow and its - good size is smaller than current size, shrink its stack */ - if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && - goodsize < L->stacksize) - luaD_reallocstack(L, goodsize); - else /* don't change stack */ - condmovestack(L,{},{}); /* (change only for debugging) */ -} - - -void luaD_inctop (lua_State *L) { - luaD_checkstack(L, 1); - L->top++; -} - -/* }================================================================== */ - - -/* -** Call a hook for the given event. Make sure there is a hook to be -** called. (Both 'L->hook' and 'L->hookmask', which triggers this -** function, can be changed asynchronously by signals.) -*/ -void luaD_hook (lua_State *L, int event, int line) { - lua_Hook hook = L->hook; - if (hook && L->allowhook) { /* make sure there is a hook */ - CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); - lua_Debug ar; - ar.event = event; - ar.currentline = line; - ar.i_ci = ci; - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= CIST_HOOKED; - lua_unlock(L); - (*hook)(L, &ar); - lua_lock(L); - lua_assert(!L->allowhook); - L->allowhook = 1; - ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); - ci->callstatus &= ~CIST_HOOKED; - } -} - - -static void callhook (lua_State *L, CallInfo *ci) { - int hook = LUA_HOOKCALL; - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (isLua(ci->previous) && - GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { - ci->callstatus |= CIST_TAIL; - hook = LUA_HOOKTAILCALL; - } - luaD_hook(L, hook, -1); - ci->u.l.savedpc--; /* correct 'pc' */ -} - - -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { - int i; - int nfixargs = p->numparams; - StkId base, fixed; - /* move fixed parameters to final position */ - fixed = L->top - actual; /* first fixed argument */ - base = L->top; /* final position of first argument */ - for (i = 0; i < nfixargs && i < actual; i++) { - setobjs2s(L, L->top++, fixed + i); - setnilvalue(fixed + i); /* erase original copy (for GC) */ - } - for (; i < nfixargs; i++) - setnilvalue(L->top++); /* complete missing arguments */ - return base; -} - - -/* -** Check whether __call metafield of 'func' is a function. If so, put -** it in stack below original 'func' so that 'luaD_precall' can call -** it. Raise an error if __call metafield is not a function. -*/ -static void tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); - StkId p; - if (!ttisfunction(tm)) - luaG_typeerror(L, func, "call"); - /* Open a hole inside the stack at 'func' */ - for (p = L->top; p > func; p--) - setobjs2s(L, p, p-1); - L->top++; /* slot ensured by caller */ - setobj2s(L, func, tm); /* tag method is the new function to be called */ -} - - -/* -** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. -** Handle most typical cases (zero results for commands, one result for -** expressions, multiple results for tail calls/single parameters) -** separated. -*/ -static int moveresults (lua_State *L, const TValue *firstResult, StkId res, - int nres, int wanted) { - switch (wanted) { /* handle typical cases separately */ - case 0: break; /* nothing to move */ - case 1: { /* one result needed */ - if (nres == 0) /* no results? */ - firstResult = luaO_nilobject; /* adjust with nil */ - setobjs2s(L, res, firstResult); /* move it to proper place */ - break; - } - case LUA_MULTRET: { - int i; - for (i = 0; i < nres; i++) /* move all results to correct place */ - setobjs2s(L, res + i, firstResult + i); - L->top = res + nres; - return 0; /* wanted == LUA_MULTRET */ - } - default: { - int i; - if (wanted <= nres) { /* enough results? */ - for (i = 0; i < wanted; i++) /* move wanted results to correct place */ - setobjs2s(L, res + i, firstResult + i); - } - else { /* not enough results; use all of them plus nils */ - for (i = 0; i < nres; i++) /* move all results to correct place */ - setobjs2s(L, res + i, firstResult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(res + i); - } - break; - } - } - L->top = res + wanted; /* top points after the last result */ - return 1; -} - - -/* -** Finishes a function call: calls hook if necessary, removes CallInfo, -** moves current number of results to proper place; returns 0 iff call -** wanted multiple (variable number of) results. -*/ -int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { - StkId res; - int wanted = ci->nresults; - if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { - if (L->hookmask & LUA_MASKRET) { - ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - luaD_hook(L, LUA_HOOKRET, -1); - firstResult = restorestack(L, fr); - } - L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ - } - res = ci->func; /* res == final position of 1st result */ - L->ci = ci->previous; /* back to caller */ - /* move results to proper place */ - return moveresults(L, firstResult, res, nres, wanted); -} - - - -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) - - -/* macro to check stack size, preserving 'p' */ -#define checkstackp(L,n,p) \ - luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ - luaC_checkGC(L), /* stack grow uses memory */ \ - p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ - - -/* -** Prepares a function call: checks the stack, creates a new CallInfo -** entry, fills in the relevant information, calls hook if needed. -** If function is a C function, does the call, too. (Otherwise, leave -** the execution ('luaV_execute') to the caller, to allow stackless -** calls.) Returns true iff function has been executed (C function). -*/ -int luaD_precall (lua_State *L, StkId func, int nresults) { - lua_CFunction f; - CallInfo *ci; - switch (ttype(func)) { - case LUA_TCCL: /* C closure */ - f = clCvalue(func)->f; - goto Cfunc; - case LUA_TLCF: /* light C function */ - f = fvalue(func); - Cfunc: { - int n; /* number of returns */ - checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); - return 1; - } - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - Proto *p = clLvalue(func)->p; - int n = cast_int(L->top - func) - 1; /* number of real arguments */ - int fsize = p->maxstacksize; /* frame size */ - checkstackp(L, fsize, func); - if (p->is_vararg) - base = adjust_varargs(L, p, n); - else { /* non vararg function */ - for (; n < p->numparams; n++) - setnilvalue(L->top++); /* complete missing arguments */ - base = func + 1; - } - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; - ci->u.l.base = base; - L->top = ci->top = base + fsize; - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; - if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); - return 0; - } - default: { /* not a function */ - checkstackp(L, 1, func); /* ensure space for metamethod */ - tryfuncTM(L, func); /* try to get '__call' metamethod */ - return luaD_precall(L, func, nresults); /* now it must be a function */ - } - } -} - - -/* -** Check appropriate error for stack overflow ("regular" overflow or -** overflow while handling stack overflow). If 'nCalls' is larger than -** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but -** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to -** allow overflow handling to work) -*/ -static void stackerror (lua_State *L) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ -} - - -/* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. -*/ -void luaD_call (lua_State *L, StkId func, int nResults) { - if (++L->nCcalls >= LUAI_MAXCCALLS) - stackerror(L); - if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ - luaV_execute(L); /* call it */ - L->nCcalls--; -} - - -/* -** Similar to 'luaD_call', but does not allow yields during the call -*/ -void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - L->nny++; - luaD_call(L, func, nResults); - L->nny--; -} - - -/* -** Completes the execution of an interrupted C function, calling its -** continuation function. -*/ -static void finishCcall (lua_State *L, int status) { - CallInfo *ci = L->ci; - int n; - /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && L->nny == 0); - /* error status can only happen in a protected call */ - lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); - if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */ - L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ - } - /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already - handled */ - adjustresults(L, ci->nresults); - lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ -} - - -/* -** Executes "full continuation" (everything in the stack) of a -** previously interrupted coroutine until the stack is empty (or another -** interruption long-jumps out of the loop). If the coroutine is -** recovering from an error, 'ud' points to the error status, which must -** be passed to the first continuation function (otherwise the default -** status is LUA_YIELD). -*/ -static void unroll (lua_State *L, void *ud) { - if (ud != NULL) /* error status? */ - finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ - while (L->ci != &L->base_ci) { /* something in the stack */ - if (!isLua(L->ci)) /* C function? */ - finishCcall(L, LUA_YIELD); /* complete its execution */ - else { /* Lua function */ - luaV_finishOp(L); /* finish interrupted instruction */ - luaV_execute(L); /* execute down to higher C 'boundary' */ - } - } -} - - -/* -** Try to find a suspended protected call (a "recover point") for the -** given thread. -*/ -static CallInfo *findpcall (lua_State *L) { - CallInfo *ci; - for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ - if (ci->callstatus & CIST_YPCALL) - return ci; - } - return NULL; /* no pending pcall */ -} - - -/* -** Recovers from an error in a coroutine. Finds a recover point (if -** there is one) and completes the execution of the interrupted -** 'luaD_pcall'. If there is no recover point, returns zero. -*/ -static int recover (lua_State *L, int status) { - StkId oldtop; - CallInfo *ci = findpcall(L); - if (ci == NULL) return 0; /* no recovery point */ - /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->extra); - luaF_close(L, oldtop); - seterrorobj(L, status, oldtop); - L->ci = ci; - L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - L->nny = 0; /* should be zero to be yieldable */ - luaD_shrinkstack(L); - L->errfunc = ci->u.c.old_errfunc; - return 1; /* continue running the coroutine */ -} - - -/* -** Signal an error in the call to 'lua_resume', not in the execution -** of the coroutine itself. (Such errors should not be handled by any -** coroutine error handler and should not kill the coroutine.) -*/ -static int resume_error (lua_State *L, const char *msg, int narg) { - L->top -= narg; /* remove args from the stack */ - setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ - api_incr_top(L); - lua_unlock(L); - return LUA_ERRRUN; -} - - -/* -** Do the work for 'lua_resume' in protected mode. Most of the work -** depends on the status of the coroutine: initial state, suspended -** inside a hook, or regularly suspended (optionally with a continuation -** function), plus erroneous cases: non-suspended coroutine or dead -** coroutine. -*/ -static void resume (lua_State *L, void *ud) { - int n = *(cast(int*, ud)); /* number of arguments */ - StkId firstArg = L->top - n; /* first argument */ - CallInfo *ci = L->ci; - if (L->status == LUA_OK) { /* starting a coroutine? */ - if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ - luaV_execute(L); /* call it */ - } - else { /* resuming from previous yield */ - lua_assert(L->status == LUA_YIELD); - L->status = LUA_OK; /* mark that it is running (again) */ - ci->func = restorestack(L, ci->extra); - if (isLua(ci)) /* yielded inside a hook? */ - luaV_execute(L); /* just continue running Lua code */ - else { /* 'common' yield */ - if (ci->u.c.k != NULL) { /* does it have a continuation function? */ - lua_unlock(L); - n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ - lua_lock(L); - api_checknelems(L, n); - firstArg = L->top - n; /* yield results come from continuation */ - } - luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ - } - unroll(L, NULL); /* run continuation */ - } -} - - -LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { - int status; - unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ - lua_lock(L); - if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (L->ci != &L->base_ci) /* not in base level? */ - return resume_error(L, "cannot resume non-suspended coroutine", nargs); - } - else if (L->status != LUA_YIELD) - return resume_error(L, "cannot resume dead coroutine", nargs); - L->nCcalls = (from) ? from->nCcalls + 1 : 1; - if (L->nCcalls >= LUAI_MAXCCALLS) - return resume_error(L, "C stack overflow", nargs); - luai_userstateresume(L, nargs); - L->nny = 0; /* allow yields */ - api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); - status = luaD_rawrunprotected(L, resume, &nargs); - if (status == -1) /* error calling 'lua_resume'? */ - status = LUA_ERRRUN; - else { /* continue running after recoverable errors */ - while (errorstatus(status) && recover(L, status)) { - /* unroll continuation */ - status = luaD_rawrunprotected(L, unroll, &status); - } - if (errorstatus(status)) { /* unrecoverable error? */ - L->status = cast_byte(status); /* mark thread as 'dead' */ - seterrorobj(L, status, L->top); /* push error message */ - L->ci->top = L->top; - } - else lua_assert(status == L->status); /* normal end or yield */ - } - L->nny = oldnny; /* restore 'nny' */ - L->nCcalls--; - lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); - lua_unlock(L); - return status; -} - - -LUA_API int lua_isyieldable (lua_State *L) { - return (L->nny == 0); -} - - -LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, - lua_KFunction k) { - CallInfo *ci = L->ci; - luai_userstateyield(L, nresults); - lua_lock(L); - api_checknelems(L, nresults); - if (L->nny > 0) { - if (L != G(L)->mainthread) - luaG_runerror(L, "attempt to yield across a C-call boundary"); - else - luaG_runerror(L, "attempt to yield from outside a coroutine"); - } - L->status = LUA_YIELD; - ci->extra = savestack(L, ci->func); /* save current 'func' */ - if (isLua(ci)) { /* inside a hook? */ - api_check(L, k == NULL, "hooks cannot continue after yielding"); - } - else { - if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ - ci->u.c.ctx = ctx; /* save context */ - ci->func = L->top - nresults - 1; /* protect stack below results */ - luaD_throw(L, LUA_YIELD); - } - lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ - lua_unlock(L); - return 0; /* return to 'luaD_hook' */ -} - - -int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t old_top, ptrdiff_t ef) { - int status; - CallInfo *old_ci = L->ci; - lu_byte old_allowhooks = L->allowhook; - unsigned short old_nny = L->nny; - ptrdiff_t old_errfunc = L->errfunc; - L->errfunc = ef; - status = luaD_rawrunprotected(L, func, u); - if (status != LUA_OK) { /* an error occurred? */ - StkId oldtop = restorestack(L, old_top); - luaF_close(L, oldtop); /* close possible pending closures */ - seterrorobj(L, status, oldtop); - L->ci = old_ci; - L->allowhook = old_allowhooks; - L->nny = old_nny; - luaD_shrinkstack(L); - } - L->errfunc = old_errfunc; - return status; -} - - - -/* -** Execute a protected parser. -*/ -struct SParser { /* data to 'f_parser' */ - ZIO *z; - Mbuffer buff; /* dynamic structure used by the scanner */ - Dyndata dyd; /* dynamic structures used by the parser */ - const char *mode; - const char *name; -}; - - -static void checkmode (lua_State *L, const char *mode, const char *x) { - if (mode && strchr(mode, x[0]) == NULL) { - luaO_pushfstring(L, - "attempt to load a %s chunk (mode is '%s')", x, mode); - luaD_throw(L, LUA_ERRSYNTAX); - } -} - - -static void f_parser (lua_State *L, void *ud) { - LClosure *cl; - struct SParser *p = cast(struct SParser *, ud); - int c = zgetc(p->z); /* read first character */ - if (c == LUA_SIGNATURE[0]) { - checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, p->name); - } - else { - checkmode(L, p->mode, "text"); - cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); - } - lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luaF_initupvals(L, cl); -} - - -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode) { - struct SParser p; - int status; - L->nny++; /* cannot yield during parsing */ - p.z = z; p.name = name; p.mode = mode; - p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; - p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; - p.dyd.label.arr = NULL; p.dyd.label.size = 0; - luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); - luaZ_freebuffer(L, &p.buff); - luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); - luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); - luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); - L->nny--; - return status; -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.h deleted file mode 100644 index 3b2983a..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldo.h +++ /dev/null @@ -1,58 +0,0 @@ -/* -** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#ifndef ldo_h -#define ldo_h - - -#include "lobject.h" -#include "lstate.h" -#include "lzio.h" - - -/* -** Macro to check stack size and grow stack if needed. Parameters -** 'pre'/'pos' allow the macro to preserve a pointer into the -** stack across reallocations, doing the work only when needed. -** 'condmovestack' is used in heavy tests to force a stack reallocation -** at every check. -*/ -#define luaD_checkstackaux(L,n,pre,pos) \ - if (L->stack_last - L->top <= (n)) \ - { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } - -/* In general, 'pre'/'pos' are empty (nothing to save) */ -#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) - - - -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) - - -/* type of protected functions, to be ran by 'runprotected' */ -typedef void (*Pfunc) (lua_State *L, void *ud); - -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode); -LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); -LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); -LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, - int nres); -LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); -LUAI_FUNC void luaD_growstack (lua_State *L, int n); -LUAI_FUNC void luaD_shrinkstack (lua_State *L); -LUAI_FUNC void luaD_inctop (lua_State *L); - -LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldump.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldump.c deleted file mode 100644 index f025aca..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ldump.c +++ /dev/null @@ -1,215 +0,0 @@ -/* -** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $ -** save precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#define ldump_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lobject.h" -#include "lstate.h" -#include "lundump.h" - - -typedef struct { - lua_State *L; - lua_Writer writer; - void *data; - int strip; - int status; -} DumpState; - - -/* -** All high-level dumps go through DumpVector; you can change it to -** change the endianness of the result -*/ -#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) - -#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) - - -static void DumpBlock (const void *b, size_t size, DumpState *D) { - if (D->status == 0 && size > 0) { - lua_unlock(D->L); - D->status = (*D->writer)(D->L, b, size, D->data); - lua_lock(D->L); - } -} - - -#define DumpVar(x,D) DumpVector(&x,1,D) - - -static void DumpByte (int y, DumpState *D) { - lu_byte x = (lu_byte)y; - DumpVar(x, D); -} - - -static void DumpInt (int x, DumpState *D) { - DumpVar(x, D); -} - - -static void DumpNumber (lua_Number x, DumpState *D) { - DumpVar(x, D); -} - - -static void DumpInteger (lua_Integer x, DumpState *D) { - DumpVar(x, D); -} - - -static void DumpString (const TString *s, DumpState *D) { - if (s == NULL) - DumpByte(0, D); - else { - size_t size = tsslen(s) + 1; /* include trailing '\0' */ - const char *str = getstr(s); - if (size < 0xFF) - DumpByte(cast_int(size), D); - else { - DumpByte(0xFF, D); - DumpVar(size, D); - } - DumpVector(str, size - 1, D); /* no need to save '\0' */ - } -} - - -static void DumpCode (const Proto *f, DumpState *D) { - DumpInt(f->sizecode, D); - DumpVector(f->code, f->sizecode, D); -} - - -static void DumpFunction(const Proto *f, TString *psource, DumpState *D); - -static void DumpConstants (const Proto *f, DumpState *D) { - int i; - int n = f->sizek; - DumpInt(n, D); - for (i = 0; i < n; i++) { - const TValue *o = &f->k[i]; - DumpByte(ttype(o), D); - switch (ttype(o)) { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpByte(bvalue(o), D); - break; - case LUA_TNUMFLT: - DumpNumber(fltvalue(o), D); - break; - case LUA_TNUMINT: - DumpInteger(ivalue(o), D); - break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: - DumpString(tsvalue(o), D); - break; - default: - lua_assert(0); - } - } -} - - -static void DumpProtos (const Proto *f, DumpState *D) { - int i; - int n = f->sizep; - DumpInt(n, D); - for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->source, D); -} - - -static void DumpUpvalues (const Proto *f, DumpState *D) { - int i, n = f->sizeupvalues; - DumpInt(n, D); - for (i = 0; i < n; i++) { - DumpByte(f->upvalues[i].instack, D); - DumpByte(f->upvalues[i].idx, D); - } -} - - -static void DumpDebug (const Proto *f, DumpState *D) { - int i, n; - n = (D->strip) ? 0 : f->sizelineinfo; - DumpInt(n, D); - DumpVector(f->lineinfo, n, D); - n = (D->strip) ? 0 : f->sizelocvars; - DumpInt(n, D); - for (i = 0; i < n; i++) { - DumpString(f->locvars[i].varname, D); - DumpInt(f->locvars[i].startpc, D); - DumpInt(f->locvars[i].endpc, D); - } - n = (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n, D); - for (i = 0; i < n; i++) - DumpString(f->upvalues[i].name, D); -} - - -static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { - if (D->strip || f->source == psource) - DumpString(NULL, D); /* no debug info or same source as its parent */ - else - DumpString(f->source, D); - DumpInt(f->linedefined, D); - DumpInt(f->lastlinedefined, D); - DumpByte(f->numparams, D); - DumpByte(f->is_vararg, D); - DumpByte(f->maxstacksize, D); - DumpCode(f, D); - DumpConstants(f, D); - DumpUpvalues(f, D); - DumpProtos(f, D); - DumpDebug(f, D); -} - - -static void DumpHeader (DumpState *D) { - DumpLiteral(LUA_SIGNATURE, D); - DumpByte(LUAC_VERSION, D); - DumpByte(LUAC_FORMAT, D); - DumpLiteral(LUAC_DATA, D); - DumpByte(sizeof(int), D); - DumpByte(sizeof(size_t), D); - DumpByte(sizeof(Instruction), D); - DumpByte(sizeof(lua_Integer), D); - DumpByte(sizeof(lua_Number), D); - DumpInteger(LUAC_INT, D); - DumpNumber(LUAC_NUM, D); -} - - -/* -** dump Lua function as precompiled chunk -*/ -int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, - int strip) { - DumpState D; - D.L = L; - D.writer = w; - D.data = data; - D.strip = strip; - D.status = 0; - DumpHeader(&D); - DumpByte(f->sizeupvalues, &D); - DumpFunction(f, NULL, &D); - return D.status; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.c deleted file mode 100644 index ccafbb8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.c +++ /dev/null @@ -1,151 +0,0 @@ -/* -** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - -#define lfunc_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -CClosure *luaF_newCclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); - CClosure *c = gco2ccl(o); - c->nupvalues = cast_byte(n); - return c; -} - - -LClosure *luaF_newLclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); - LClosure *c = gco2lcl(o); - c->p = NULL; - c->nupvalues = cast_byte(n); - while (n--) c->upvals[n] = NULL; - return c; -} - -/* -** fill a closure with new closed upvalues -*/ -void luaF_initupvals (lua_State *L, LClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) { - UpVal *uv = luaM_new(L, UpVal); - uv->refcount = 1; - uv->v = &uv->u.value; /* make it closed */ - setnilvalue(uv->v); - cl->upvals[i] = uv; - } -} - - -UpVal *luaF_findupval (lua_State *L, StkId level) { - UpVal **pp = &L->openupval; - UpVal *p; - UpVal *uv; - lua_assert(isintwups(L) || L->openupval == NULL); - while (*pp != NULL && (p = *pp)->v >= level) { - lua_assert(upisopen(p)); - if (p->v == level) /* found a corresponding upvalue? */ - return p; /* return it */ - pp = &p->u.open.next; - } - /* not found: create a new upvalue */ - uv = luaM_new(L, UpVal); - uv->refcount = 0; - uv->u.open.next = *pp; /* link it to list of open upvalues */ - uv->u.open.touched = 1; - *pp = uv; - uv->v = level; /* current value lives in the stack */ - if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ - L->twups = G(L)->twups; /* link it to the list */ - G(L)->twups = L; - } - return uv; -} - - -void luaF_close (lua_State *L, StkId level) { - UpVal *uv; - while (L->openupval != NULL && (uv = L->openupval)->v >= level) { - lua_assert(upisopen(uv)); - L->openupval = uv->u.open.next; /* remove from 'open' list */ - if (uv->refcount == 0) /* no references? */ - luaM_free(L, uv); /* free upvalue */ - else { - setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ - uv->v = &uv->u.value; /* now current value lives here */ - luaC_upvalbarrier(L, uv); - } - } -} - - -Proto *luaF_newproto (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); - Proto *f = gco2p(o); - f->k = NULL; - f->sizek = 0; - f->p = NULL; - f->sizep = 0; - f->code = NULL; - f->cache = NULL; - f->sizecode = 0; - f->lineinfo = NULL; - f->sizelineinfo = 0; - f->upvalues = NULL; - f->sizeupvalues = 0; - f->numparams = 0; - f->is_vararg = 0; - f->maxstacksize = 0; - f->locvars = NULL; - f->sizelocvars = 0; - f->linedefined = 0; - f->lastlinedefined = 0; - f->source = NULL; - return f; -} - - -void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); - luaM_free(L, f); -} - - -/* -** Look for n-th local variable at line 'line' in function 'func'. -** Returns NULL if not found. -*/ -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { - int i; - for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { - if (pc < f->locvars[i].endpc) { /* is variable active? */ - local_number--; - if (local_number == 0) - return getstr(f->locvars[i].varname); - } - } - return NULL; /* not found */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.h deleted file mode 100644 index c916e98..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lfunc.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - -#ifndef lfunc_h -#define lfunc_h - - -#include "lobject.h" - - -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ - cast(int, sizeof(TValue)*((n)-1))) - -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ - cast(int, sizeof(TValue *)*((n)-1))) - - -/* test whether thread is in 'twups' list */ -#define isintwups(L) (L->twups != L) - - -/* -** maximum number of upvalues in a closure (both C and Lua). (Value -** must fit in a VM register.) -*/ -#define MAXUPVAL 255 - - -/* -** Upvalues for Lua closures -*/ -struct UpVal { - TValue *v; /* points to stack or to its own value */ - lu_mem refcount; /* reference counter */ - union { - struct { /* (when open) */ - UpVal *next; /* linked list */ - int touched; /* mark to avoid cycles with dead threads */ - } open; - TValue value; /* the value (when closed) */ - } u; -}; - -#define upisopen(up) ((up)->v != &(up)->u.value) - - -LUAI_FUNC Proto *luaF_newproto (lua_State *L); -LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); -LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); -LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); -LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level); -LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); -LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, - int pc); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.c deleted file mode 100644 index db4df82..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.c +++ /dev/null @@ -1,1179 +0,0 @@ -/* -** $Id: lgc.c,v 2.215.1.2 2017/08/31 16:15:27 roberto Exp $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#define lgc_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -/* -** internal state for collector while inside the atomic phase. The -** collector should never be in this state while running regular code. -*/ -#define GCSinsideatomic (GCSpause + 1) - -/* -** cost of sweeping one element (the size of a small object divided -** by some adjust for the sweep speed) -*/ -#define GCSWEEPCOST ((sizeof(TString) + 4) / 4) - -/* maximum number of elements to sweep in each single step */ -#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) - -/* cost of calling one finalizer */ -#define GCFINALIZECOST GCSWEEPCOST - - -/* -** macro to adjust 'stepmul': 'stepmul' is actually used like -** 'stepmul / STEPMULADJ' (value chosen by tests) -*/ -#define STEPMULADJ 200 - - -/* -** macro to adjust 'pause': 'pause' is actually used like -** 'pause / PAUSEADJ' (value chosen by tests) -*/ -#define PAUSEADJ 100 - - -/* -** 'makewhite' erases all color bits then sets only the current white -** bit -*/ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) -#define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) - -#define white2gray(x) resetbits(x->marked, WHITEBITS) -#define black2gray(x) resetbit(x->marked, BLACKBIT) - - -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) - -#define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n))) - - -#define checkconsistency(obj) \ - lua_longassert(!iscollectable(obj) || righttt(obj)) - - -#define markvalue(g,o) { checkconsistency(o); \ - if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } - -#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } - -/* -** mark an object that can be NULL (either because it is really optional, -** or it was stripped as debug info, or inside an uncompleted structure) -*/ -#define markobjectN(g,t) { if (t) markobject(g,t); } - -static void reallymarkobject (global_State *g, GCObject *o); - - -/* -** {====================================================== -** Generic functions -** ======================================================= -*/ - - -/* -** one after last element in a hash array -*/ -#define gnodelast(h) gnode(h, cast(size_t, sizenode(h))) - - -/* -** link collectable object 'o' into list pointed by 'p' -*/ -#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) - - -/* -** If key is not marked, mark its entry as dead. This allows key to be -** collected, but keeps its entry in the table. A dead node is needed -** when Lua looks up for a key (it may be part of a chain) and when -** traversing a weak table (key might be removed from the table during -** traversal). Other places never manipulate dead keys, because its -** associated nil value is enough to signal that the entry is logically -** empty. -*/ -static void removeentry (Node *n) { - lua_assert(ttisnil(gval(n))); - if (valiswhite(gkey(n))) - setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */ -} - - -/* -** tells whether a key or value can be cleared from a weak -** table. Non-collectable objects are never removed from weak -** tables. Strings behave as 'values', so are never removed too. for -** other objects: if really collected, cannot keep them; for objects -** being finalized, keep them in keys, but not in values -*/ -static int iscleared (global_State *g, const TValue *o) { - if (!iscollectable(o)) return 0; - else if (ttisstring(o)) { - markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */ - return 0; - } - else return iswhite(gcvalue(o)); -} - - -/* -** barrier that moves collector forward, that is, mark the white object -** being pointed by a black object. (If in sweep phase, clear the black -** object to white [sweep it] to avoid other barrier calls for this -** same object.) -*/ -void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { - global_State *g = G(L); - lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - if (keepinvariant(g)) /* must keep invariant? */ - reallymarkobject(g, v); /* restore invariant */ - else { /* sweep phase */ - lua_assert(issweepphase(g)); - makewhite(g, o); /* mark main obj. as white to avoid other barriers */ - } -} - - -/* -** barrier that moves collector backward, that is, mark the black object -** pointing to a white object as gray again. -*/ -void luaC_barrierback_ (lua_State *L, Table *t) { - global_State *g = G(L); - lua_assert(isblack(t) && !isdead(g, t)); - black2gray(t); /* make table gray (again) */ - linkgclist(t, g->grayagain); -} - - -/* -** barrier for assignments to closed upvalues. Because upvalues are -** shared among closures, it is impossible to know the color of all -** closures pointing to it. So, we assume that the object being assigned -** must be marked. -*/ -void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { - global_State *g = G(L); - GCObject *o = gcvalue(uv->v); - lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */ - if (keepinvariant(g)) - markobject(g, o); -} - - -void luaC_fix (lua_State *L, GCObject *o) { - global_State *g = G(L); - lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ - white2gray(o); /* they will be gray forever */ - g->allgc = o->next; /* remove object from 'allgc' list */ - o->next = g->fixedgc; /* link it to 'fixedgc' list */ - g->fixedgc = o; -} - - -/* -** create a new collectable object (with given type and size) and link -** it to 'allgc' list. -*/ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { - global_State *g = G(L); - GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); - o->marked = luaC_white(g); - o->tt = tt; - o->next = g->allgc; - g->allgc = o; - return o; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Mark functions -** ======================================================= -*/ - - -/* -** mark an object. Userdata, strings, and closed upvalues are visited -** and turned black here. Other objects are marked gray and added -** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list.) -*/ -static void reallymarkobject (global_State *g, GCObject *o) { - reentry: - white2gray(o); - switch (o->tt) { - case LUA_TSHRSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); - break; - } - case LUA_TLNGSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); - break; - } - case LUA_TUSERDATA: { - TValue uvalue; - markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ - gray2black(o); - g->GCmemtrav += sizeudata(gco2u(o)); - getuservalue(g->mainthread, gco2u(o), &uvalue); - if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */ - o = gcvalue(&uvalue); - goto reentry; - } - break; - } - case LUA_TLCL: { - linkgclist(gco2lcl(o), g->gray); - break; - } - case LUA_TCCL: { - linkgclist(gco2ccl(o), g->gray); - break; - } - case LUA_TTABLE: { - linkgclist(gco2t(o), g->gray); - break; - } - case LUA_TTHREAD: { - linkgclist(gco2th(o), g->gray); - break; - } - case LUA_TPROTO: { - linkgclist(gco2p(o), g->gray); - break; - } - default: lua_assert(0); break; - } -} - - -/* -** mark metamethods for basic types -*/ -static void markmt (global_State *g) { - int i; - for (i=0; i < LUA_NUMTAGS; i++) - markobjectN(g, g->mt[i]); -} - - -/* -** mark all objects in list of being-finalized -*/ -static void markbeingfnz (global_State *g) { - GCObject *o; - for (o = g->tobefnz; o != NULL; o = o->next) - markobject(g, o); -} - - -/* -** Mark all values stored in marked open upvalues from non-marked threads. -** (Values from marked threads were already marked when traversing the -** thread.) Remove from the list threads that no longer have upvalues and -** not-marked threads. -*/ -static void remarkupvals (global_State *g) { - lua_State *thread; - lua_State **p = &g->twups; - while ((thread = *p) != NULL) { - lua_assert(!isblack(thread)); /* threads are never black */ - if (isgray(thread) && thread->openupval != NULL) - p = &thread->twups; /* keep marked thread with upvalues in the list */ - else { /* thread is not marked or without upvalues */ - UpVal *uv; - *p = thread->twups; /* remove thread from the list */ - thread->twups = thread; /* mark that it is out of list */ - for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { - if (uv->u.open.touched) { - markvalue(g, uv->v); /* remark upvalue's value */ - uv->u.open.touched = 0; - } - } - } - } -} - - -/* -** mark root set and reset all gray lists, to start a new collection -*/ -static void restartcollection (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; - markobject(g, g->mainthread); - markvalue(g, &g->l_registry); - markmt(g); - markbeingfnz(g); /* mark any finalizing object left from previous cycle */ -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Traverse functions -** ======================================================= -*/ - -/* -** Traverse a table with weak values and link it to proper list. During -** propagate phase, keep it in 'grayagain' list, to be revisited in the -** atomic phase. In the atomic phase, if table has any white value, -** put it in 'weak' list, to be cleared. -*/ -static void traverseweakvalue (global_State *g, Table *h) { - Node *n, *limit = gnodelast(h); - /* if there is array part, assume it may have white values (it is not - worth traversing it now just to check) */ - int hasclears = (h->sizearray > 0); - for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */ - hasclears = 1; /* table will have to be cleared */ - } - } - if (g->gcstate == GCSpropagate) - linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ - else if (hasclears) - linkgclist(h, g->weak); /* has to be cleared later */ -} - - -/* -** Traverse an ephemeron table and link it to proper list. Returns true -** iff any object was marked during this traversal (which implies that -** convergence has to continue). During propagation phase, keep table -** in 'grayagain' list, to be visited again in the atomic phase. In -** the atomic phase, if table has any white->white entry, it has to -** be revisited during ephemeron convergence (as that key may turn -** black). Otherwise, if it has any white key, table has to be cleared -** (in the atomic phase). -*/ -static int traverseephemeron (global_State *g, Table *h) { - int marked = 0; /* true if an object is marked in this traversal */ - int hasclears = 0; /* true if table has white keys */ - int hasww = 0; /* true if table has entry "white-key -> white-value" */ - Node *n, *limit = gnodelast(h); - unsigned int i; - /* traverse array part */ - for (i = 0; i < h->sizearray; i++) { - if (valiswhite(&h->array[i])) { - marked = 1; - reallymarkobject(g, gcvalue(&h->array[i])); - } - } - /* traverse hash part */ - for (n = gnode(h, 0); n < limit; n++) { - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */ - hasclears = 1; /* table must be cleared */ - if (valiswhite(gval(n))) /* value not marked yet? */ - hasww = 1; /* white-white entry */ - } - else if (valiswhite(gval(n))) { /* value not marked yet? */ - marked = 1; - reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ - } - } - /* link table into proper list */ - if (g->gcstate == GCSpropagate) - linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ - else if (hasww) /* table has white->white entries? */ - linkgclist(h, g->ephemeron); /* have to propagate again */ - else if (hasclears) /* table has white keys? */ - linkgclist(h, g->allweak); /* may have to clean white keys */ - return marked; -} - - -static void traversestrongtable (global_State *g, Table *h) { - Node *n, *limit = gnodelast(h); - unsigned int i; - for (i = 0; i < h->sizearray; i++) /* traverse array part */ - markvalue(g, &h->array[i]); - for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ - else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - markvalue(g, gval(n)); /* mark value */ - } - } -} - - -static lu_mem traversetable (global_State *g, Table *h) { - const char *weakkey, *weakvalue; - const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - markobjectN(g, h->metatable); - if (mode && ttisstring(mode) && /* is there a weak mode? */ - ((weakkey = strchr(svalue(mode), 'k')), - (weakvalue = strchr(svalue(mode), 'v')), - (weakkey || weakvalue))) { /* is really weak? */ - black2gray(h); /* keep table gray */ - if (!weakkey) /* strong keys? */ - traverseweakvalue(g, h); - else if (!weakvalue) /* strong values? */ - traverseephemeron(g, h); - else /* all weak */ - linkgclist(h, g->allweak); /* nothing to traverse now */ - } - else /* not weak */ - traversestrongtable(g, h); - return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * cast(size_t, allocsizenode(h)); -} - - -/* -** Traverse a prototype. (While a prototype is being build, its -** arrays can be larger than needed; the extra slots are filled with -** NULL, so the use of 'markobjectN') -*/ -static int traverseproto (global_State *g, Proto *f) { - int i; - if (f->cache && iswhite(f->cache)) - f->cache = NULL; /* allow cache to be collected */ - markobjectN(g, f->source); - for (i = 0; i < f->sizek; i++) /* mark literals */ - markvalue(g, &f->k[i]); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobjectN(g, f->upvalues[i].name); - for (i = 0; i < f->sizep; i++) /* mark nested protos */ - markobjectN(g, f->p[i]); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobjectN(g, f->locvars[i].varname); - return sizeof(Proto) + sizeof(Instruction) * f->sizecode + - sizeof(Proto *) * f->sizep + - sizeof(TValue) * f->sizek + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; -} - - -static lu_mem traverseCclosure (global_State *g, CClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ - markvalue(g, &cl->upvalue[i]); - return sizeCclosure(cl->nupvalues); -} - -/* -** open upvalues point to values in a thread, so those values should -** be marked when the thread is traversed except in the atomic phase -** (because then the value cannot be changed by the thread and the -** thread may not be traversed again) -*/ -static lu_mem traverseLclosure (global_State *g, LClosure *cl) { - int i; - markobjectN(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ - UpVal *uv = cl->upvals[i]; - if (uv != NULL) { - if (upisopen(uv) && g->gcstate != GCSinsideatomic) - uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ - else - markvalue(g, uv->v); - } - } - return sizeLclosure(cl->nupvalues); -} - - -static lu_mem traversethread (global_State *g, lua_State *th) { - StkId o = th->stack; - if (o == NULL) - return 1; /* stack not completely built yet */ - lua_assert(g->gcstate == GCSinsideatomic || - th->openupval == NULL || isintwups(th)); - for (; o < th->top; o++) /* mark live elements in the stack */ - markvalue(g, o); - if (g->gcstate == GCSinsideatomic) { /* final traversal? */ - StkId lim = th->stack + th->stacksize; /* real end of stack */ - for (; o < lim; o++) /* clear not-marked stack slice */ - setnilvalue(o); - /* 'remarkupvals' may have removed thread from 'twups' list */ - if (!isintwups(th) && th->openupval != NULL) { - th->twups = g->twups; /* link it back to the list */ - g->twups = th; - } - } - else if (g->gckind != KGC_EMERGENCY) - luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * th->nci); -} - - -/* -** traverse one gray object, turning it to black (except for threads, -** which are always gray). -*/ -static void propagatemark (global_State *g) { - lu_mem size; - GCObject *o = g->gray; - lua_assert(isgray(o)); - gray2black(o); - switch (o->tt) { - case LUA_TTABLE: { - Table *h = gco2t(o); - g->gray = h->gclist; /* remove from 'gray' list */ - size = traversetable(g, h); - break; - } - case LUA_TLCL: { - LClosure *cl = gco2lcl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseLclosure(g, cl); - break; - } - case LUA_TCCL: { - CClosure *cl = gco2ccl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseCclosure(g, cl); - break; - } - case LUA_TTHREAD: { - lua_State *th = gco2th(o); - g->gray = th->gclist; /* remove from 'gray' list */ - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(o); - size = traversethread(g, th); - break; - } - case LUA_TPROTO: { - Proto *p = gco2p(o); - g->gray = p->gclist; /* remove from 'gray' list */ - size = traverseproto(g, p); - break; - } - default: lua_assert(0); return; - } - g->GCmemtrav += size; -} - - -static void propagateall (global_State *g) { - while (g->gray) propagatemark(g); -} - - -static void convergeephemerons (global_State *g) { - int changed; - do { - GCObject *w; - GCObject *next = g->ephemeron; /* get ephemeron list */ - g->ephemeron = NULL; /* tables may return to this list when traversed */ - changed = 0; - while ((w = next) != NULL) { - next = gco2t(w)->gclist; - if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */ - propagateall(g); /* propagate changes */ - changed = 1; /* will have to revisit all ephemeron tables */ - } - } - } while (changed); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Sweep Functions -** ======================================================= -*/ - - -/* -** clear entries with unmarked keys from all weaktables in list 'l' up -** to element 'f' -*/ -static void clearkeys (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { - Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); - for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { - setnilvalue(gval(n)); /* remove value ... */ - } - if (ttisnil(gval(n))) /* is entry empty? */ - removeentry(n); /* remove entry from table */ - } - } -} - - -/* -** clear entries with unmarked values from all weaktables in list 'l' up -** to element 'f' -*/ -static void clearvalues (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { - Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); - unsigned int i; - for (i = 0; i < h->sizearray; i++) { - TValue *o = &h->array[i]; - if (iscleared(g, o)) /* value was collected? */ - setnilvalue(o); /* remove value */ - } - for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && iscleared(g, gval(n))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* and remove entry from table */ - } - } - } -} - - -void luaC_upvdeccount (lua_State *L, UpVal *uv) { - lua_assert(uv->refcount > 0); - uv->refcount--; - if (uv->refcount == 0 && !upisopen(uv)) - luaM_free(L, uv); -} - - -static void freeLclosure (lua_State *L, LClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) { - UpVal *uv = cl->upvals[i]; - if (uv) - luaC_upvdeccount(L, uv); - } - luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); -} - - -static void freeobj (lua_State *L, GCObject *o) { - switch (o->tt) { - case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TLCL: { - freeLclosure(L, gco2lcl(o)); - break; - } - case LUA_TCCL: { - luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); - break; - } - case LUA_TTABLE: luaH_free(L, gco2t(o)); break; - case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; - case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; - case LUA_TSHRSTR: - luaS_remove(L, gco2ts(o)); /* remove it from hash table */ - luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); - break; - case LUA_TLNGSTR: { - luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); - break; - } - default: lua_assert(0); - } -} - - -#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); - - -/* -** sweep at most 'count' elements from a list of GCObjects erasing dead -** objects, where a dead object is one marked with the old (non current) -** white; change all non-dead objects back to white, preparing for next -** collection cycle. Return where to continue the traversal or NULL if -** list is finished. -*/ -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { - global_State *g = G(L); - int ow = otherwhite(g); - int white = luaC_white(g); /* current white */ - while (*p != NULL && count-- > 0) { - GCObject *curr = *p; - int marked = curr->marked; - if (isdeadm(ow, marked)) { /* is 'curr' dead? */ - *p = curr->next; /* remove 'curr' from list */ - freeobj(L, curr); /* erase 'curr' */ - } - else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskcolors) | white); - p = &curr->next; /* go to next element */ - } - } - return (*p == NULL) ? NULL : p; -} - - -/* -** sweep a list until a live object (or end of list) -*/ -static GCObject **sweeptolive (lua_State *L, GCObject **p) { - GCObject **old = p; - do { - p = sweeplist(L, p, 1); - } while (p == old); - return p; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Finalization -** ======================================================= -*/ - -/* -** If possible, shrink string table -*/ -static void checkSizes (lua_State *L, global_State *g) { - if (g->gckind != KGC_EMERGENCY) { - l_mem olddebt = g->GCdebt; - if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ - luaS_resize(L, g->strt.size / 2); /* shrink it a little */ - g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - } -} - - -static GCObject *udata2finalize (global_State *g) { - GCObject *o = g->tobefnz; /* get first element */ - lua_assert(tofinalize(o)); - g->tobefnz = o->next; /* remove it from 'tobefnz' list */ - o->next = g->allgc; /* return it to 'allgc' list */ - g->allgc = o; - resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ - if (issweepphase(g)) - makewhite(g, o); /* "sweep" object */ - return o; -} - - -static void dothecall (lua_State *L, void *ud) { - UNUSED(ud); - luaD_callnoyield(L, L->top - 2, 0); -} - - -static void GCTM (lua_State *L, int propagateerrors) { - global_State *g = G(L); - const TValue *tm; - TValue v; - setgcovalue(L, &v, udata2finalize(g)); - tm = luaT_gettmbyobj(L, &v, TM_GC); - if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */ - int status; - lu_byte oldah = L->allowhook; - int running = g->gcrunning; - L->allowhook = 0; /* stop debug hooks during GC metamethod */ - g->gcrunning = 0; /* avoid GC steps */ - setobj2s(L, L->top, tm); /* push finalizer... */ - setobj2s(L, L->top + 1, &v); /* ... and its argument */ - L->top += 2; /* and (next line) call the finalizer */ - L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); - L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ - L->allowhook = oldah; /* restore hooks */ - g->gcrunning = running; /* restore state */ - if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ - if (status == LUA_ERRRUN) { /* is there an error object? */ - const char *msg = (ttisstring(L->top - 1)) - ? svalue(L->top - 1) - : "no message"; - luaO_pushfstring(L, "error in __gc metamethod (%s)", msg); - status = LUA_ERRGCMM; /* error in __gc metamethod */ - } - luaD_throw(L, status); /* re-throw error */ - } - } -} - - -/* -** call a few (up to 'g->gcfinnum') finalizers -*/ -static int runafewfinalizers (lua_State *L) { - global_State *g = G(L); - unsigned int i; - lua_assert(!g->tobefnz || g->gcfinnum > 0); - for (i = 0; g->tobefnz && i < g->gcfinnum; i++) - GCTM(L, 1); /* call one finalizer */ - g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */ - : g->gcfinnum * 2; /* else call a few more next time */ - return i; -} - - -/* -** call all pending finalizers -*/ -static void callallpendingfinalizers (lua_State *L) { - global_State *g = G(L); - while (g->tobefnz) - GCTM(L, 0); -} - - -/* -** find last 'next' field in list 'p' list (to add elements in its end) -*/ -static GCObject **findlast (GCObject **p) { - while (*p != NULL) - p = &(*p)->next; - return p; -} - - -/* -** move all unreachable objects (or 'all' objects) that need -** finalization from list 'finobj' to list 'tobefnz' (to be finalized) -*/ -static void separatetobefnz (global_State *g, int all) { - GCObject *curr; - GCObject **p = &g->finobj; - GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != NULL) { /* traverse all finalizable objects */ - lua_assert(tofinalize(curr)); - if (!(iswhite(curr) || all)) /* not being collected? */ - p = &curr->next; /* don't bother with it */ - else { - *p = curr->next; /* remove 'curr' from 'finobj' list */ - curr->next = *lastnext; /* link at the end of 'tobefnz' list */ - *lastnext = curr; - lastnext = &curr->next; - } - } -} - - -/* -** if object 'o' has a finalizer, remove it from 'allgc' list (must -** search the list to find it) and link it in 'finobj' list. -*/ -void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { - global_State *g = G(L); - if (tofinalize(o) || /* obj. is already marked... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ - return; /* nothing to be done */ - else { /* move 'o' to 'finobj' list */ - GCObject **p; - if (issweepphase(g)) { - makewhite(g, o); /* "sweep" object 'o' */ - if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ - g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ - } - /* search for pointer pointing to 'o' */ - for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } - *p = o->next; /* remove 'o' from 'allgc' list */ - o->next = g->finobj; /* link it in 'finobj' list */ - g->finobj = o; - l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */ - } -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** GC control -** ======================================================= -*/ - - -/* -** Set a reasonable "time" to wait before starting a new GC cycle; cycle -** will start when memory use hits threshold. (Division by 'estimate' -** should be OK: it cannot be zero (because Lua cannot even start with -** less than PAUSEADJ bytes). -*/ -static void setpause (global_State *g) { - l_mem threshold, debt; - l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ - lua_assert(estimate > 0); - threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * g->gcpause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = gettotalbytes(g) - threshold; - luaE_setdebt(g, debt); -} - - -/* -** Enter first sweep phase. -** The call to 'sweeplist' tries to make pointer point to an object -** inside the list (instead of to the header), so that the real sweep do -** not need to skip objects created between "now" and the start of the -** real sweep. -*/ -static void entersweep (lua_State *L) { - global_State *g = G(L); - g->gcstate = GCSswpallgc; - lua_assert(g->sweepgc == NULL); - g->sweepgc = sweeplist(L, &g->allgc, 1); -} - - -void luaC_freeallobjects (lua_State *L) { - global_State *g = G(L); - separatetobefnz(g, 1); /* separate all objects with finalizers */ - lua_assert(g->finobj == NULL); - callallpendingfinalizers(L); - lua_assert(g->tobefnz == NULL); - g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ - g->gckind = KGC_NORMAL; - sweepwholelist(L, &g->finobj); - sweepwholelist(L, &g->allgc); - sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ - lua_assert(g->strt.nuse == 0); -} - - -static l_mem atomic (lua_State *L) { - global_State *g = G(L); - l_mem work; - GCObject *origweak, *origall; - GCObject *grayagain = g->grayagain; /* save original list */ - lua_assert(g->ephemeron == NULL && g->weak == NULL); - lua_assert(!iswhite(g->mainthread)); - g->gcstate = GCSinsideatomic; - g->GCmemtrav = 0; /* start counting work */ - markobject(g, L); /* mark running thread */ - /* registry and global metatables may be changed by API */ - markvalue(g, &g->l_registry); - markmt(g); /* mark global metatables */ - /* remark occasional upvalues of (maybe) dead threads */ - remarkupvals(g); - propagateall(g); /* propagate changes */ - work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ - g->gray = grayagain; - propagateall(g); /* traverse 'grayagain' list */ - g->GCmemtrav = 0; /* restart counting */ - convergeephemerons(g); - /* at this point, all strongly accessible objects are marked. */ - /* Clear values from weak tables, before checking finalizers */ - clearvalues(g, g->weak, NULL); - clearvalues(g, g->allweak, NULL); - origweak = g->weak; origall = g->allweak; - work += g->GCmemtrav; /* stop counting (objects being finalized) */ - separatetobefnz(g, 0); /* separate objects to be finalized */ - g->gcfinnum = 1; /* there may be objects to be finalized */ - markbeingfnz(g); /* mark objects that will be finalized */ - propagateall(g); /* remark, to propagate 'resurrection' */ - g->GCmemtrav = 0; /* restart counting */ - convergeephemerons(g); - /* at this point, all resurrected objects are marked. */ - /* remove dead objects from weak tables */ - clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */ - clearkeys(g, g->allweak, NULL); /* clear keys from all 'allweak' tables */ - /* clear values from resurrected weak tables */ - clearvalues(g, g->weak, origweak); - clearvalues(g, g->allweak, origall); - luaS_clearcache(g); - g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ - work += g->GCmemtrav; /* complete counting */ - return work; /* estimate of memory marked by 'atomic' */ -} - - -static lu_mem sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { - if (g->sweepgc) { - l_mem olddebt = g->GCdebt; - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); - g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - if (g->sweepgc) /* is there still something to sweep? */ - return (GCSWEEPMAX * GCSWEEPCOST); - } - /* else enter next state */ - g->gcstate = nextstate; - g->sweepgc = nextlist; - return 0; -} - - -static lu_mem singlestep (lua_State *L) { - global_State *g = G(L); - switch (g->gcstate) { - case GCSpause: { - g->GCmemtrav = g->strt.size * sizeof(GCObject*); - restartcollection(g); - g->gcstate = GCSpropagate; - return g->GCmemtrav; - } - case GCSpropagate: { - g->GCmemtrav = 0; - lua_assert(g->gray); - propagatemark(g); - if (g->gray == NULL) /* no more gray objects? */ - g->gcstate = GCSatomic; /* finish propagate phase */ - return g->GCmemtrav; /* memory traversed in this step */ - } - case GCSatomic: { - lu_mem work; - propagateall(g); /* make sure gray list is empty */ - work = atomic(L); /* work is what was traversed by 'atomic' */ - entersweep(L); - g->GCestimate = gettotalbytes(g); /* first estimate */; - return work; - } - case GCSswpallgc: { /* sweep "regular" objects */ - return sweepstep(L, g, GCSswpfinobj, &g->finobj); - } - case GCSswpfinobj: { /* sweep objects with finalizers */ - return sweepstep(L, g, GCSswptobefnz, &g->tobefnz); - } - case GCSswptobefnz: { /* sweep objects to be finalized */ - return sweepstep(L, g, GCSswpend, NULL); - } - case GCSswpend: { /* finish sweeps */ - makewhite(g, g->mainthread); /* sweep main thread */ - checkSizes(L, g); - g->gcstate = GCScallfin; - return 0; - } - case GCScallfin: { /* call remaining finalizers */ - if (g->tobefnz && g->gckind != KGC_EMERGENCY) { - int n = runafewfinalizers(L); - return (n * GCFINALIZECOST); - } - else { /* emergency mode or no more finalizers */ - g->gcstate = GCSpause; /* finish collection */ - return 0; - } - } - default: lua_assert(0); return 0; - } -} - - -/* -** advances the garbage collector until it reaches a state allowed -** by 'statemask' -*/ -void luaC_runtilstate (lua_State *L, int statesmask) { - global_State *g = G(L); - while (!testbit(statesmask, g->gcstate)) - singlestep(L); -} - - -/* -** get GC debt and convert it from Kb to 'work units' (avoid zero debt -** and overflows) -*/ -static l_mem getdebt (global_State *g) { - l_mem debt = g->GCdebt; - int stepmul = g->gcstepmul; - if (debt <= 0) return 0; /* minimal debt */ - else { - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - return debt; - } -} - -/* -** performs a basic GC step when collector is running -*/ -void luaC_step (lua_State *L) { - global_State *g = G(L); - l_mem debt = getdebt(g); /* GC deficit (be paid now) */ - if (!g->gcrunning) { /* not running? */ - luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ - return; - } - do { /* repeat until pause or enough "credit" (negative debt) */ - lu_mem work = singlestep(L); /* perform one single step */ - debt -= work; - } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); - if (g->gcstate == GCSpause) - setpause(g); /* pause until next cycle */ - else { - debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ - luaE_setdebt(g, debt); - runafewfinalizers(L); - } -} - - -/* -** Performs a full GC cycle; if 'isemergency', set a flag to avoid -** some operations which could change the interpreter state in some -** unexpected ways (running finalizers and shrinking some structures). -** Before running the collection, check 'keepinvariant'; if it is true, -** there may be some objects marked as black, so the collector has -** to sweep all objects to turn them back to white (as white has not -** changed, nothing will be collected). -*/ -void luaC_fullgc (lua_State *L, int isemergency) { - global_State *g = G(L); - lua_assert(g->gckind == KGC_NORMAL); - if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */ - if (keepinvariant(g)) { /* black objects? */ - entersweep(L); /* sweep everything to turn them back to white */ - } - /* finish any pending sweep phase to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpause)); - luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */ - luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ - /* estimate must be correct after a full GC cycle */ - lua_assert(g->GCestimate == gettotalbytes(g)); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ - g->gckind = KGC_NORMAL; - setpause(g); -} - -/* }====================================================== */ - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.h deleted file mode 100644 index 425cd7c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lgc.h +++ /dev/null @@ -1,147 +0,0 @@ -/* -** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#ifndef lgc_h -#define lgc_h - - -#include "lobject.h" -#include "lstate.h" - -/* -** Collectable objects may have one of three colors: white, which -** means the object is not marked; gray, which means the -** object is marked, but its references may be not marked; and -** black, which means that the object and all its references are marked. -** The main invariant of the garbage collector, while marking objects, -** is that a black object can never point to a white one. Moreover, -** any gray object must be in a "gray list" (gray, grayagain, weak, -** allweak, ephemeron) so that it can be visited again before finishing -** the collection cycle. These lists have no meaning when the invariant -** is not being enforced (e.g., sweep phase). -*/ - - - -/* how much to allocate before next GC step */ -#if !defined(GCSTEPSIZE) -/* ~100 small strings */ -#define GCSTEPSIZE (cast_int(100 * sizeof(TString))) -#endif - - -/* -** Possible states of the Garbage Collector -*/ -#define GCSpropagate 0 -#define GCSatomic 1 -#define GCSswpallgc 2 -#define GCSswpfinobj 3 -#define GCSswptobefnz 4 -#define GCSswpend 5 -#define GCScallfin 6 -#define GCSpause 7 - - -#define issweepphase(g) \ - (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) - - -/* -** macro to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a collection, the sweep -** phase may break the invariant, as objects turned white may point to -** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. -*/ - -#define keepinvariant(g) ((g)->gcstate <= GCSatomic) - - -/* -** some useful bit tricks -*/ -#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) -#define setbits(x,m) ((x) |= (m)) -#define testbits(x,m) ((x) & (m)) -#define bitmask(b) (1<<(b)) -#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) -#define l_setbit(x,b) setbits(x, bitmask(b)) -#define resetbit(x,b) resetbits(x, bitmask(b)) -#define testbit(x,b) testbits(x, bitmask(b)) - - -/* Layout for bit use in 'marked' field: */ -#define WHITE0BIT 0 /* object is white (type 0) */ -#define WHITE1BIT 1 /* object is white (type 1) */ -#define BLACKBIT 2 /* object is black */ -#define FINALIZEDBIT 3 /* object has been marked for finalization */ -/* bit 7 is currently used by tests (luaL_checkmemory) */ - -#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) - - -#define iswhite(x) testbits((x)->marked, WHITEBITS) -#define isblack(x) testbit((x)->marked, BLACKBIT) -#define isgray(x) /* neither white nor black */ \ - (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) - -#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) - -#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) -#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) -#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) - -#define changewhite(x) ((x)->marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->marked, BLACKBIT) - -#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) - - -/* -** Does one step of collection when debt becomes positive. 'pre'/'pos' -** allows some adjustments to be done only when needed. macro -** 'condchangemem' is used only for heavy tests (forcing a full -** GC cycle on every opportunity) -*/ -#define luaC_condGC(L,pre,pos) \ - { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ - condchangemem(L,pre,pos); } - -/* more often than not, 'pre'/'pos' are empty */ -#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) - - -#define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) - -#define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrierback_(L,p) : cast_void(0)) - -#define luaC_objbarrier(L,p,o) ( \ - (isblack(p) && iswhite(o)) ? \ - luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) - -#define luaC_upvalbarrier(L,uv) ( \ - (iscollectable((uv)->v) && !upisopen(uv)) ? \ - luaC_upvalbarrier_(L,uv) : cast_void(0)) - -LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_freeallobjects (lua_State *L); -LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); -LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); -LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); -LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); -LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/linit.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/linit.c deleted file mode 100644 index 480da52..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/linit.c +++ /dev/null @@ -1,68 +0,0 @@ -/* -** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ -** Initialization of libraries for lua.c and other clients -** See Copyright Notice in lua.h -*/ - - -#define linit_c -#define LUA_LIB - -/* -** If you embed Lua in your program and need to open the standard -** libraries, call luaL_openlibs in your program. If you need a -** different set of libraries, copy this file to your project and edit -** it to suit your needs. -** -** You can also *preload* libraries, so that a later 'require' can -** open the library, which is already linked to the application. -** For that, do the following code: -** -** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); -** lua_pushcfunction(L, luaopen_modname); -** lua_setfield(L, -2, modname); -** lua_pop(L, 1); // remove PRELOAD table -*/ - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lualib.h" -#include "lauxlib.h" - - -/* -** these libs are loaded by lua.c and are readily available to any Lua -** program -*/ -static const luaL_Reg loadedlibs[] = { - {"_G", luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_COLIBNAME, luaopen_coroutine}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_UTF8LIBNAME, luaopen_utf8}, - {LUA_DBLIBNAME, luaopen_debug}, -#if defined(LUA_COMPAT_BITLIB) - {LUA_BITLIBNAME, luaopen_bit32}, -#endif - {NULL, NULL} -}; - - -LUALIB_API void luaL_openlibs (lua_State *L) { - const luaL_Reg *lib; - /* "require" functions from 'loadedlibs' and set results to global table */ - for (lib = loadedlibs; lib->func; lib++) { - luaL_requiref(L, lib->name, lib->func, 1); - lua_pop(L, 1); /* remove lib */ - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/liolib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/liolib.c deleted file mode 100644 index 027d4bd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/liolib.c +++ /dev/null @@ -1,778 +0,0 @@ -/* -** $Id: liolib.c,v 2.151.1.1 2017/04/19 17:29:57 roberto Exp $ -** Standard I/O (and system) library -** See Copyright Notice in lua.h -*/ - -#define liolib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - - -/* -** Change this macro to accept other modes for 'fopen' besides -** the standard ones. -*/ -#if !defined(l_checkmode) - -/* accepted extensions to 'mode' in 'fopen' */ -#if !defined(L_MODEEXT) -#define L_MODEEXT "b" -#endif - -/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ -static int l_checkmode (const char *mode) { - return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && - (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ - (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ -} - -#endif - -/* -** {====================================================== -** l_popen spawns a new process connected to the current -** one through the file streams. -** ======================================================= -*/ - -#if !defined(l_popen) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#define l_popen(L,c,m) (fflush(NULL), popen(c,m)) -#define l_pclose(L,file) (pclose(file)) - -#elif defined(LUA_USE_WINDOWS) /* }{ */ - -#define l_popen(L,c,m) (_popen(c,m)) -#define l_pclose(L,file) (_pclose(file)) - -#else /* }{ */ - -/* ISO C definitions */ -#define l_popen(L,c,m) \ - ((void)((void)c, m), \ - luaL_error(L, "'popen' not supported"), \ - (FILE*)0) -#define l_pclose(L,file) ((void)L, (void)file, -1) - -#endif /* } */ - -#endif /* } */ - -/* }====================================================== */ - - -#if !defined(l_getc) /* { */ - -#if defined(LUA_USE_POSIX) -#define l_getc(f) getc_unlocked(f) -#define l_lockfile(f) flockfile(f) -#define l_unlockfile(f) funlockfile(f) -#else -#define l_getc(f) getc(f) -#define l_lockfile(f) ((void)0) -#define l_unlockfile(f) ((void)0) -#endif - -#endif /* } */ - - -/* -** {====================================================== -** l_fseek: configuration for longer offsets -** ======================================================= -*/ - -#if !defined(l_fseek) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include - -#define l_fseek(f,o,w) fseeko(f,o,w) -#define l_ftell(f) ftello(f) -#define l_seeknum off_t - -#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ - && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ - -/* Windows (but not DDK) and Visual C++ 2005 or higher */ -#define l_fseek(f,o,w) _fseeki64(f,o,w) -#define l_ftell(f) _ftelli64(f) -#define l_seeknum __int64 - -#else /* }{ */ - -/* ISO C definitions */ -#define l_fseek(f,o,w) fseek(f,o,w) -#define l_ftell(f) ftell(f) -#define l_seeknum long - -#endif /* } */ - -#endif /* } */ - -/* }====================================================== */ - - -#define IO_PREFIX "_IO_" -#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) -#define IO_INPUT (IO_PREFIX "input") -#define IO_OUTPUT (IO_PREFIX "output") - - -typedef luaL_Stream LStream; - - -#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) - -#define isclosed(p) ((p)->closef == NULL) - - -static int io_type (lua_State *L) { - LStream *p; - luaL_checkany(L, 1); - p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); - if (p == NULL) - lua_pushnil(L); /* not a file */ - else if (isclosed(p)) - lua_pushliteral(L, "closed file"); - else - lua_pushliteral(L, "file"); - return 1; -} - - -static int f_tostring (lua_State *L) { - LStream *p = tolstream(L); - if (isclosed(p)) - lua_pushliteral(L, "file (closed)"); - else - lua_pushfstring(L, "file (%p)", p->f); - return 1; -} - - -static FILE *tofile (lua_State *L) { - LStream *p = tolstream(L); - if (isclosed(p)) - luaL_error(L, "attempt to use a closed file"); - lua_assert(p->f); - return p->f; -} - - -/* -** When creating file handles, always creates a 'closed' file handle -** before opening the actual file; so, if there is a memory error, the -** handle is in a consistent state. -*/ -static LStream *newprefile (lua_State *L) { - LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); - p->closef = NULL; /* mark file handle as 'closed' */ - luaL_setmetatable(L, LUA_FILEHANDLE); - return p; -} - - -/* -** Calls the 'close' function from a file handle. The 'volatile' avoids -** a bug in some versions of the Clang compiler (e.g., clang 3.0 for -** 32 bits). -*/ -static int aux_close (lua_State *L) { - LStream *p = tolstream(L); - volatile lua_CFunction cf = p->closef; - p->closef = NULL; /* mark stream as closed */ - return (*cf)(L); /* close it */ -} - - -static int f_close (lua_State *L) { - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); -} - - -static int io_close (lua_State *L) { - if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ - return f_close(L); -} - - -static int f_gc (lua_State *L) { - LStream *p = tolstream(L); - if (!isclosed(p) && p->f != NULL) - aux_close(L); /* ignore closed and incompletely open files */ - return 0; -} - - -/* -** function to close regular files -*/ -static int io_fclose (lua_State *L) { - LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); -} - - -static LStream *newfile (lua_State *L) { - LStream *p = newprefile(L); - p->f = NULL; - p->closef = &io_fclose; - return p; -} - - -static void opencheck (lua_State *L, const char *fname, const char *mode) { - LStream *p = newfile(L); - p->f = fopen(fname, mode); - if (p->f == NULL) - luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); -} - - -static int io_open (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newfile(L); - const char *md = mode; /* to traverse/check mode */ - luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); - p->f = fopen(filename, mode); - return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; -} - - -/* -** function to close 'popen' files -*/ -static int io_pclose (lua_State *L) { - LStream *p = tolstream(L); - return luaL_execresult(L, l_pclose(L, p->f)); -} - - -static int io_popen (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newprefile(L); - luaL_argcheck(L, ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0'), - 2, "invalid mode"); - p->f = l_popen(L, filename, mode); - p->closef = &io_pclose; - return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; -} - - -static int io_tmpfile (lua_State *L) { - LStream *p = newfile(L); - p->f = tmpfile(); - return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; -} - - -static FILE *getiofile (lua_State *L, const char *findex) { - LStream *p; - lua_getfield(L, LUA_REGISTRYINDEX, findex); - p = (LStream *)lua_touserdata(L, -1); - if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN); - return p->f; -} - - -static int g_iofile (lua_State *L, const char *f, const char *mode) { - if (!lua_isnoneornil(L, 1)) { - const char *filename = lua_tostring(L, 1); - if (filename) - opencheck(L, filename, mode); - else { - tofile(L); /* check that it's a valid file handle */ - lua_pushvalue(L, 1); - } - lua_setfield(L, LUA_REGISTRYINDEX, f); - } - /* return current value */ - lua_getfield(L, LUA_REGISTRYINDEX, f); - return 1; -} - - -static int io_input (lua_State *L) { - return g_iofile(L, IO_INPUT, "r"); -} - - -static int io_output (lua_State *L) { - return g_iofile(L, IO_OUTPUT, "w"); -} - - -static int io_readline (lua_State *L); - - -/* -** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit -** in the limit for upvalues of a closure) -*/ -#define MAXARGLINE 250 - -static void aux_lines (lua_State *L, int toclose) { - int n = lua_gettop(L) - 1; /* number of arguments to read */ - luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ - lua_pushcclosure(L, io_readline, 3 + n); -} - - -static int f_lines (lua_State *L) { - tofile(L); /* check that it's a valid file handle */ - aux_lines(L, 0); - return 1; -} - - -static int io_lines (lua_State *L) { - int toclose; - if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ - if (lua_isnil(L, 1)) { /* no file name? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ - lua_replace(L, 1); /* put it at index 1 */ - tofile(L); /* check that it's a valid file handle */ - toclose = 0; /* do not close it after iteration */ - } - else { /* open a new file */ - const char *filename = luaL_checkstring(L, 1); - opencheck(L, filename, "r"); - lua_replace(L, 1); /* put file at index 1 */ - toclose = 1; /* close it after iteration */ - } - aux_lines(L, toclose); - return 1; -} - - -/* -** {====================================================== -** READ -** ======================================================= -*/ - - -/* maximum length of a numeral */ -#if !defined (L_MAXLENNUM) -#define L_MAXLENNUM 200 -#endif - - -/* auxiliary structure used by 'read_number' */ -typedef struct { - FILE *f; /* file being read */ - int c; /* current character (look ahead) */ - int n; /* number of elements in buffer 'buff' */ - char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ -} RN; - - -/* -** Add current char to buffer (if not out of space) and read next one -*/ -static int nextc (RN *rn) { - if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ - rn->buff[0] = '\0'; /* invalidate result */ - return 0; /* fail */ - } - else { - rn->buff[rn->n++] = rn->c; /* save current char */ - rn->c = l_getc(rn->f); /* read next one */ - return 1; - } -} - - -/* -** Accept current char if it is in 'set' (of size 2) -*/ -static int test2 (RN *rn, const char *set) { - if (rn->c == set[0] || rn->c == set[1]) - return nextc(rn); - else return 0; -} - - -/* -** Read a sequence of (hex)digits -*/ -static int readdigits (RN *rn, int hex) { - int count = 0; - while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) - count++; - return count; -} - - -/* -** Read a number: first reads a valid prefix of a numeral into a buffer. -** Then it calls 'lua_stringtonumber' to check whether the format is -** correct and to convert it to a Lua number -*/ -static int read_number (lua_State *L, FILE *f) { - RN rn; - int count = 0; - int hex = 0; - char decp[2]; - rn.f = f; rn.n = 0; - decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ - decp[1] = '.'; /* always accept a dot */ - l_lockfile(rn.f); - do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ - test2(&rn, "-+"); /* optional signal */ - if (test2(&rn, "00")) { - if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ - else count = 1; /* count initial '0' as a valid digit */ - } - count += readdigits(&rn, hex); /* integral part */ - if (test2(&rn, decp)) /* decimal point? */ - count += readdigits(&rn, hex); /* fractional part */ - if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ - test2(&rn, "-+"); /* exponent signal */ - readdigits(&rn, 0); /* exponent digits */ - } - ungetc(rn.c, rn.f); /* unread look-ahead char */ - l_unlockfile(rn.f); - rn.buff[rn.n] = '\0'; /* finish string */ - if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ - return 1; /* ok */ - else { /* invalid format */ - lua_pushnil(L); /* "result" to be removed */ - return 0; /* read fails */ - } -} - - -static int test_eof (lua_State *L, FILE *f) { - int c = getc(f); - ungetc(c, f); /* no-op when c == EOF */ - lua_pushliteral(L, ""); - return (c != EOF); -} - - -static int read_line (lua_State *L, FILE *f, int chop) { - luaL_Buffer b; - int c = '\0'; - luaL_buffinit(L, &b); - while (c != EOF && c != '\n') { /* repeat until end of line */ - char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ - int i = 0; - l_lockfile(f); /* no memory errors can happen inside the lock */ - while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') - buff[i++] = c; - l_unlockfile(f); - luaL_addsize(&b, i); - } - if (!chop && c == '\n') /* want a newline and have one? */ - luaL_addchar(&b, c); /* add ending newline to result */ - luaL_pushresult(&b); /* close buffer */ - /* return ok if read something (either a newline or something else) */ - return (c == '\n' || lua_rawlen(L, -1) > 0); -} - - -static void read_all (lua_State *L, FILE *f) { - size_t nr; - luaL_Buffer b; - luaL_buffinit(L, &b); - do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ - char *p = luaL_prepbuffer(&b); - nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); - luaL_addsize(&b, nr); - } while (nr == LUAL_BUFFERSIZE); - luaL_pushresult(&b); /* close buffer */ -} - - -static int read_chars (lua_State *L, FILE *f, size_t n) { - size_t nr; /* number of chars actually read */ - char *p; - luaL_Buffer b; - luaL_buffinit(L, &b); - p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ - nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ - luaL_addsize(&b, nr); - luaL_pushresult(&b); /* close buffer */ - return (nr > 0); /* true iff read something */ -} - - -static int g_read (lua_State *L, FILE *f, int first) { - int nargs = lua_gettop(L) - 1; - int success; - int n; - clearerr(f); - if (nargs == 0) { /* no arguments? */ - success = read_line(L, f, 1); - n = first+1; /* to return 1 result */ - } - else { /* ensure stack space for all results and for auxlib's buffer */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - success = 1; - for (n = first; nargs-- && success; n++) { - if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)luaL_checkinteger(L, n); - success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); - } - else { - const char *p = luaL_checkstring(L, n); - if (*p == '*') p++; /* skip optional '*' (for compatibility) */ - switch (*p) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f, 1); - break; - case 'L': /* line with end-of-line */ - success = read_line(L, f, 0); - break; - case 'a': /* file */ - read_all(L, f); /* read entire file */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, n, "invalid format"); - } - } - } - } - if (ferror(f)) - return luaL_fileresult(L, 0, NULL); - if (!success) { - lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ - } - return n - first; -} - - -static int io_read (lua_State *L) { - return g_read(L, getiofile(L, IO_INPUT), 1); -} - - -static int f_read (lua_State *L) { - return g_read(L, tofile(L), 2); -} - - -static int io_readline (lua_State *L) { - LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); - int i; - int n = (int)lua_tointeger(L, lua_upvalueindex(2)); - if (isclosed(p)) /* file is already closed? */ - return luaL_error(L, "file is already closed"); - lua_settop(L , 1); - luaL_checkstack(L, n, "too many arguments"); - for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ - lua_pushvalue(L, lua_upvalueindex(3 + i)); - n = g_read(L, p->f, 2); /* 'n' is number of results */ - lua_assert(n > 0); /* should return at least a nil */ - if (lua_toboolean(L, -n)) /* read at least one value? */ - return n; /* return them */ - else { /* first result is nil: EOF or error */ - if (n > 1) { /* is there error information? */ - /* 2nd result is error message */ - return luaL_error(L, "%s", lua_tostring(L, -n + 1)); - } - if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); - aux_close(L); /* close it */ - } - return 0; - } -} - -/* }====================================================== */ - - -static int g_write (lua_State *L, FILE *f, int arg) { - int nargs = lua_gettop(L) - arg; - int status = 1; - for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - int len = lua_isinteger(L, arg) - ? fprintf(f, LUA_INTEGER_FMT, - (LUAI_UACINT)lua_tointeger(L, arg)) - : fprintf(f, LUA_NUMBER_FMT, - (LUAI_UACNUMBER)lua_tonumber(L, arg)); - status = status && (len > 0); - } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); - } - } - if (status) return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); -} - - -static int io_write (lua_State *L) { - return g_write(L, getiofile(L, IO_OUTPUT), 1); -} - - -static int f_write (lua_State *L) { - FILE *f = tofile(L); - lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ - return g_write(L, f, 2); -} - - -static int f_seek (lua_State *L) { - static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; - static const char *const modenames[] = {"set", "cur", "end", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Integer p3 = luaL_optinteger(L, 3, 0); - l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Integer)offset == p3, 3, - "not an integer in proper range"); - op = l_fseek(f, offset, mode[op]); - if (op) - return luaL_fileresult(L, 0, NULL); /* error */ - else { - lua_pushinteger(L, (lua_Integer)l_ftell(f)); - return 1; - } -} - - -static int f_setvbuf (lua_State *L) { - static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; - static const char *const modenames[] = {"no", "full", "line", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, NULL, modenames); - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], (size_t)sz); - return luaL_fileresult(L, res == 0, NULL); -} - - - -static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); -} - - -static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); -} - - -/* -** functions for 'io' library -*/ -static const luaL_Reg iolib[] = { - {"close", io_close}, - {"flush", io_flush}, - {"input", io_input}, - {"lines", io_lines}, - {"open", io_open}, - {"output", io_output}, - {"popen", io_popen}, - {"read", io_read}, - {"tmpfile", io_tmpfile}, - {"type", io_type}, - {"write", io_write}, - {NULL, NULL} -}; - - -/* -** methods for file handles -*/ -static const luaL_Reg flib[] = { - {"close", f_close}, - {"flush", f_flush}, - {"lines", f_lines}, - {"read", f_read}, - {"seek", f_seek}, - {"setvbuf", f_setvbuf}, - {"write", f_write}, - {"__gc", f_gc}, - {"__tostring", f_tostring}, - {NULL, NULL} -}; - - -static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ - lua_pop(L, 1); /* pop new metatable */ -} - - -/* -** function to (not) close the standard files stdin, stdout, and stderr -*/ -static int io_noclose (lua_State *L) { - LStream *p = tolstream(L); - p->closef = &io_noclose; /* keep file opened */ - lua_pushnil(L); - lua_pushliteral(L, "cannot close standard file"); - return 2; -} - - -static void createstdfile (lua_State *L, FILE *f, const char *k, - const char *fname) { - LStream *p = newprefile(L); - p->f = f; - p->closef = &io_noclose; - if (k != NULL) { - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ - } - lua_setfield(L, -2, fname); /* add file to module */ -} - - -LUAMOD_API int luaopen_io (lua_State *L) { - luaL_newlib(L, iolib); /* new module */ - createmeta(L); - /* create (and set) default files */ - createstdfile(L, stdin, IO_INPUT, "stdin"); - createstdfile(L, stdout, IO_OUTPUT, "stdout"); - createstdfile(L, stderr, NULL, "stderr"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.c deleted file mode 100644 index b6d9a46..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.c +++ /dev/null @@ -1,568 +0,0 @@ -/* -** $Id: llex.c,v 2.96.1.1 2017/04/19 17:20:42 roberto Exp $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - -#define llex_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lctype.h" -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "llex.h" -#include "lobject.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lzio.h" - - - -#define next(ls) (ls->current = zgetc(ls->z)) - - - -#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') - - -/* ORDER RESERVED */ -static const char *const luaX_tokens [] = { - "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", "goto", "if", - "in", "local", "nil", "not", "or", "repeat", - "return", "then", "true", "until", "while", - "//", "..", "...", "==", ">=", "<=", "~=", - "<<", ">>", "::", "", - "", "", "", "" -}; - - -#define save_and_next(ls) (save(ls, ls->current), next(ls)) - - -static l_noret lexerror (LexState *ls, const char *msg, int token); - - -static void save (LexState *ls, int c) { - Mbuffer *b = ls->buff; - if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { - size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZE/2) - lexerror(ls, "lexical element too long", 0); - newsize = luaZ_sizebuffer(b) * 2; - luaZ_resizebuffer(ls->L, b, newsize); - } - b->buffer[luaZ_bufflen(b)++] = cast(char, c); -} - - -void luaX_init (lua_State *L) { - int i; - TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ - luaC_fix(L, obj2gco(e)); /* never collect this name */ - for (i=0; iextra = cast_byte(i+1); /* reserved word */ - } -} - - -const char *luaX_token2str (LexState *ls, int token) { - if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast_uchar(token)); - return luaO_pushfstring(ls->L, "'%c'", token); - } - else { - const char *s = luaX_tokens[token - FIRST_RESERVED]; - if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ - return luaO_pushfstring(ls->L, "'%s'", s); - else /* names, strings, and numerals */ - return s; - } -} - - -static const char *txtToken (LexState *ls, int token) { - switch (token) { - case TK_NAME: case TK_STRING: - case TK_FLT: case TK_INT: - save(ls, '\0'); - return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); - default: - return luaX_token2str(ls, token); - } -} - - -static l_noret lexerror (LexState *ls, const char *msg, int token) { - msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); - if (token) - luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); - luaD_throw(ls->L, LUA_ERRSYNTAX); -} - - -l_noret luaX_syntaxerror (LexState *ls, const char *msg) { - lexerror(ls, msg, ls->t.token); -} - - -/* -** creates a new string and anchors it in scanner's table so that -** it will not be collected until the end of the compilation -** (by that time it should be anchored somewhere) -*/ -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { - lua_State *L = ls->L; - TValue *o; /* entry for 'str' */ - TString *ts = luaS_newlstr(L, str, l); /* create new string */ - setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->h, L->top - 1); - if (ttisnil(o)) { /* not in use yet? */ - /* boolean value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ - setbvalue(o, 1); /* t[string] = true */ - luaC_checkGC(L); - } - else { /* string already present */ - ts = tsvalue(keyfromval(o)); /* re-use value previously stored */ - } - L->top--; /* remove string from stack */ - return ts; -} - - -/* -** increment line number and skips newline sequence (any of -** \n, \r, \n\r, or \r\n) -*/ -static void inclinenumber (LexState *ls) { - int old = ls->current; - lua_assert(currIsNewline(ls)); - next(ls); /* skip '\n' or '\r' */ - if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip '\n\r' or '\r\n' */ - if (++ls->linenumber >= MAX_INT) - lexerror(ls, "chunk has too many lines", 0); -} - - -void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, - int firstchar) { - ls->t.token = 0; - ls->L = L; - ls->current = firstchar; - ls->lookahead.token = TK_EOS; /* no look-ahead token */ - ls->z = z; - ls->fs = NULL; - ls->linenumber = 1; - ls->lastline = 1; - ls->source = source; - ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ - luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ -} - - - -/* -** ======================================================= -** LEXICAL ANALYZER -** ======================================================= -*/ - - -static int check_next1 (LexState *ls, int c) { - if (ls->current == c) { - next(ls); - return 1; - } - else return 0; -} - - -/* -** Check whether current char is in set 'set' (with two chars) and -** saves it -*/ -static int check_next2 (LexState *ls, const char *set) { - lua_assert(set[2] == '\0'); - if (ls->current == set[0] || ls->current == set[1]) { - save_and_next(ls); - return 1; - } - else return 0; -} - - -/* LUA_NUMBER */ -/* -** this function is quite liberal in what it accepts, as 'luaO_str2num' -** will reject ill-formed numerals. -*/ -static int read_numeral (LexState *ls, SemInfo *seminfo) { - TValue obj; - const char *expo = "Ee"; - int first = ls->current; - lua_assert(lisdigit(ls->current)); - save_and_next(ls); - if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ - expo = "Pp"; - for (;;) { - if (check_next2(ls, expo)) /* exponent part? */ - check_next2(ls, "-+"); /* optional exponent sign */ - if (lisxdigit(ls->current)) - save_and_next(ls); - else if (ls->current == '.') - save_and_next(ls); - else break; - } - save(ls, '\0'); - if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ - lexerror(ls, "malformed number", TK_FLT); - if (ttisinteger(&obj)) { - seminfo->i = ivalue(&obj); - return TK_INT; - } - else { - lua_assert(ttisfloat(&obj)); - seminfo->r = fltvalue(&obj); - return TK_FLT; - } -} - - -/* -** reads a sequence '[=*[' or ']=*]', leaving the last bracket. -** If sequence is well formed, return its number of '='s + 2; otherwise, -** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). -*/ -static size_t skip_sep (LexState *ls) { - size_t count = 0; - int s = ls->current; - lua_assert(s == '[' || s == ']'); - save_and_next(ls); - while (ls->current == '=') { - save_and_next(ls); - count++; - } - return (ls->current == s) ? count + 2 - : (count == 0) ? 1 - : 0; - -} - - -static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { - int line = ls->linenumber; /* initial line (for error message) */ - save_and_next(ls); /* skip 2nd '[' */ - if (currIsNewline(ls)) /* string starts with a newline? */ - inclinenumber(ls); /* skip it */ - for (;;) { - switch (ls->current) { - case EOZ: { /* error */ - const char *what = (seminfo ? "string" : "comment"); - const char *msg = luaO_pushfstring(ls->L, - "unfinished long %s (starting at line %d)", what, line); - lexerror(ls, msg, TK_EOS); - break; /* to avoid warnings */ - } - case ']': { - if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd ']' */ - goto endloop; - } - break; - } - case '\n': case '\r': { - save(ls, '\n'); - inclinenumber(ls); - if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ - break; - } - default: { - if (seminfo) save_and_next(ls); - else next(ls); - } - } - } endloop: - if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, - luaZ_bufflen(ls->buff) - 2 * sep); -} - - -static void esccheck (LexState *ls, int c, const char *msg) { - if (!c) { - if (ls->current != EOZ) - save_and_next(ls); /* add current to buffer for error message */ - lexerror(ls, msg, TK_STRING); - } -} - - -static int gethexa (LexState *ls) { - save_and_next(ls); - esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); - return luaO_hexavalue(ls->current); -} - - -static int readhexaesc (LexState *ls) { - int r = gethexa(ls); - r = (r << 4) + gethexa(ls); - luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ - return r; -} - - -static unsigned long readutf8esc (LexState *ls) { - unsigned long r; - int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ - save_and_next(ls); /* skip 'u' */ - esccheck(ls, ls->current == '{', "missing '{'"); - r = gethexa(ls); /* must have at least one digit */ - while ((save_and_next(ls), lisxdigit(ls->current))) { - i++; - r = (r << 4) + luaO_hexavalue(ls->current); - esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large"); - } - esccheck(ls, ls->current == '}', "missing '}'"); - next(ls); /* skip '}' */ - luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ - return r; -} - - -static void utf8esc (LexState *ls) { - char buff[UTF8BUFFSZ]; - int n = luaO_utf8esc(buff, readutf8esc(ls)); - for (; n > 0; n--) /* add 'buff' to string */ - save(ls, buff[UTF8BUFFSZ - n]); -} - - -static int readdecesc (LexState *ls) { - int i; - int r = 0; /* result accumulator */ - for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ - r = 10*r + ls->current - '0'; - save_and_next(ls); - } - esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); - luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ - return r; -} - - -static void read_string (LexState *ls, int del, SemInfo *seminfo) { - save_and_next(ls); /* keep delimiter (for error messages) */ - while (ls->current != del) { - switch (ls->current) { - case EOZ: - lexerror(ls, "unfinished string", TK_EOS); - break; /* to avoid warnings */ - case '\n': - case '\r': - lexerror(ls, "unfinished string", TK_STRING); - break; /* to avoid warnings */ - case '\\': { /* escape sequences */ - int c; /* final character to be saved */ - save_and_next(ls); /* keep '\\' for error messages */ - switch (ls->current) { - case 'a': c = '\a'; goto read_save; - case 'b': c = '\b'; goto read_save; - case 'f': c = '\f'; goto read_save; - case 'n': c = '\n'; goto read_save; - case 'r': c = '\r'; goto read_save; - case 't': c = '\t'; goto read_save; - case 'v': c = '\v'; goto read_save; - case 'x': c = readhexaesc(ls); goto read_save; - case 'u': utf8esc(ls); goto no_save; - case '\n': case '\r': - inclinenumber(ls); c = '\n'; goto only_save; - case '\\': case '\"': case '\'': - c = ls->current; goto read_save; - case EOZ: goto no_save; /* will raise an error next loop */ - case 'z': { /* zap following span of spaces */ - luaZ_buffremove(ls->buff, 1); /* remove '\\' */ - next(ls); /* skip the 'z' */ - while (lisspace(ls->current)) { - if (currIsNewline(ls)) inclinenumber(ls); - else next(ls); - } - goto no_save; - } - default: { - esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); - c = readdecesc(ls); /* digital escape '\ddd' */ - goto only_save; - } - } - read_save: - next(ls); - /* go through */ - only_save: - luaZ_buffremove(ls->buff, 1); /* remove '\\' */ - save(ls, c); - /* go through */ - no_save: break; - } - default: - save_and_next(ls); - } - } - save_and_next(ls); /* skip delimiter */ - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, - luaZ_bufflen(ls->buff) - 2); -} - - -static int llex (LexState *ls, SemInfo *seminfo) { - luaZ_resetbuffer(ls->buff); - for (;;) { - switch (ls->current) { - case '\n': case '\r': { /* line breaks */ - inclinenumber(ls); - break; - } - case ' ': case '\f': case '\t': case '\v': { /* spaces */ - next(ls); - break; - } - case '-': { /* '-' or '--' (comment) */ - next(ls); - if (ls->current != '-') return '-'; - /* else is a comment */ - next(ls); - if (ls->current == '[') { /* long comment? */ - size_t sep = skip_sep(ls); - luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ - if (sep >= 2) { - read_long_string(ls, NULL, sep); /* skip long comment */ - luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ - break; - } - } - /* else short comment */ - while (!currIsNewline(ls) && ls->current != EOZ) - next(ls); /* skip until end of line (or end of file) */ - break; - } - case '[': { /* long string or simply '[' */ - size_t sep = skip_sep(ls); - if (sep >= 2) { - read_long_string(ls, seminfo, sep); - return TK_STRING; - } - else if (sep == 0) /* '[=...' missing second bracket */ - lexerror(ls, "invalid long string delimiter", TK_STRING); - return '['; - } - case '=': { - next(ls); - if (check_next1(ls, '=')) return TK_EQ; - else return '='; - } - case '<': { - next(ls); - if (check_next1(ls, '=')) return TK_LE; - else if (check_next1(ls, '<')) return TK_SHL; - else return '<'; - } - case '>': { - next(ls); - if (check_next1(ls, '=')) return TK_GE; - else if (check_next1(ls, '>')) return TK_SHR; - else return '>'; - } - case '/': { - next(ls); - if (check_next1(ls, '/')) return TK_IDIV; - else return '/'; - } - case '~': { - next(ls); - if (check_next1(ls, '=')) return TK_NE; - else return '~'; - } - case ':': { - next(ls); - if (check_next1(ls, ':')) return TK_DBCOLON; - else return ':'; - } - case '"': case '\'': { /* short literal strings */ - read_string(ls, ls->current, seminfo); - return TK_STRING; - } - case '.': { /* '.', '..', '...', or number */ - save_and_next(ls); - if (check_next1(ls, '.')) { - if (check_next1(ls, '.')) - return TK_DOTS; /* '...' */ - else return TK_CONCAT; /* '..' */ - } - else if (!lisdigit(ls->current)) return '.'; - else return read_numeral(ls, seminfo); - } - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': { - return read_numeral(ls, seminfo); - } - case EOZ: { - return TK_EOS; - } - default: { - if (lislalpha(ls->current)) { /* identifier or reserved word? */ - TString *ts; - do { - save_and_next(ls); - } while (lislalnum(ls->current)); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - seminfo->ts = ts; - if (isreserved(ts)) /* reserved word? */ - return ts->extra - 1 + FIRST_RESERVED; - else { - return TK_NAME; - } - } - else { /* single-char tokens (+ - / ...) */ - int c = ls->current; - next(ls); - return c; - } - } - } - } -} - - -void luaX_next (LexState *ls) { - ls->lastline = ls->linenumber; - if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ - ls->t = ls->lookahead; /* use this one */ - ls->lookahead.token = TK_EOS; /* and discharge it */ - } - else - ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ -} - - -int luaX_lookahead (LexState *ls) { - lua_assert(ls->lookahead.token == TK_EOS); - ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); - return ls->lookahead.token; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.h deleted file mode 100644 index 2ed0af6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llex.h +++ /dev/null @@ -1,85 +0,0 @@ -/* -** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - -#ifndef llex_h -#define llex_h - -#include "lobject.h" -#include "lzio.h" - - -#define FIRST_RESERVED 257 - - -#if !defined(LUA_ENV) -#define LUA_ENV "_ENV" -#endif - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER RESERVED" -*/ -enum RESERVED { - /* terminal symbols denoted by reserved words */ - TK_AND = FIRST_RESERVED, TK_BREAK, - TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, - TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, - /* other terminal symbols */ - TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, - TK_SHL, TK_SHR, - TK_DBCOLON, TK_EOS, - TK_FLT, TK_INT, TK_NAME, TK_STRING -}; - -/* number of reserved words */ -#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) - - -typedef union { - lua_Number r; - lua_Integer i; - TString *ts; -} SemInfo; /* semantics information */ - - -typedef struct Token { - int token; - SemInfo seminfo; -} Token; - - -/* state of the lexer plus state of the parser when shared by all - functions */ -typedef struct LexState { - int current; /* current character (charint) */ - int linenumber; /* input line counter */ - int lastline; /* line of last token 'consumed' */ - Token t; /* current token */ - Token lookahead; /* look ahead token */ - struct FuncState *fs; /* current function (parser) */ - struct lua_State *L; - ZIO *z; /* input stream */ - Mbuffer *buff; /* buffer for tokens */ - Table *h; /* to avoid collection/reuse strings */ - struct Dyndata *dyd; /* dynamic structures used by the parser */ - TString *source; /* current source name */ - TString *envn; /* environment variable name */ -} LexState; - - -LUAI_FUNC void luaX_init (lua_State *L); -LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, - TString *source, int firstchar); -LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); -LUAI_FUNC void luaX_next (LexState *ls); -LUAI_FUNC int luaX_lookahead (LexState *ls); -LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); -LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llimits.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llimits.h deleted file mode 100644 index d1036f6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/llimits.h +++ /dev/null @@ -1,323 +0,0 @@ -/* -** $Id: llimits.h,v 1.141.1.1 2017/04/19 17:20:42 roberto Exp $ -** Limits, basic types, and some other 'installation-dependent' definitions -** See Copyright Notice in lua.h -*/ - -#ifndef llimits_h -#define llimits_h - - -#include -#include - - -#include "lua.h" - -/* -** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count -** the total memory used by Lua (in bytes). Usually, 'size_t' and -** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. -*/ -#if defined(LUAI_MEM) /* { external definitions? */ -typedef LUAI_UMEM lu_mem; -typedef LUAI_MEM l_mem; -#elif LUAI_BITSINT >= 32 /* }{ */ -typedef size_t lu_mem; -typedef ptrdiff_t l_mem; -#else /* 16-bit ints */ /* }{ */ -typedef unsigned long lu_mem; -typedef long l_mem; -#endif /* } */ - - -/* chars used as small naturals (so that 'char' is reserved for characters) */ -typedef unsigned char lu_byte; - - -/* maximum value for size_t */ -#define MAX_SIZET ((size_t)(~(size_t)0)) - -/* maximum size visible for Lua (must be representable in a lua_Integer */ -#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ - : (size_t)(LUA_MAXINTEGER)) - - -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) - -#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) - - -#define MAX_INT INT_MAX /* maximum value of an int */ - - -/* -** conversion of pointer to unsigned integer: -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value -*/ -#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) - - - -/* type to ensure maximum alignment */ -#if defined(LUAI_USER_ALIGNMENT_T) -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; -#else -typedef union { - lua_Number n; - double u; - void *s; - lua_Integer i; - long l; -} L_Umaxalign; -#endif - - - -/* types of 'usual argument conversions' for lua_Number and lua_Integer */ -typedef LUAI_UACNUMBER l_uacNumber; -typedef LUAI_UACINT l_uacInt; - - -/* internal assertions for in-house debugging */ -#if defined(lua_assert) -#define check_exp(c,e) (lua_assert(c), (e)) -/* to avoid problems with conditions too long */ -#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) -#else -#define lua_assert(c) ((void)0) -#define check_exp(c,e) (e) -#define lua_longassert(c) ((void)0) -#endif - -/* -** assertion for checking API calls -*/ -#if !defined(luai_apicheck) -#define luai_apicheck(l,e) lua_assert(e) -#endif - -#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) - - -/* macro to avoid warnings about unused variables */ -#if !defined(UNUSED) -#define UNUSED(x) ((void)(x)) -#endif - - -/* type casts (a macro highlights casts in the code) */ -#define cast(t, exp) ((t)(exp)) - -#define cast_void(i) cast(void, (i)) -#define cast_byte(i) cast(lu_byte, (i)) -#define cast_num(i) cast(lua_Number, (i)) -#define cast_int(i) cast(int, (i)) -#define cast_uchar(i) cast(unsigned char, (i)) - - -/* cast a signed lua_Integer to lua_Unsigned */ -#if !defined(l_castS2U) -#define l_castS2U(i) ((lua_Unsigned)(i)) -#endif - -/* -** cast a lua_Unsigned to a signed lua_Integer; this cast is -** not strict ISO C, but two-complement architectures should -** work fine. -*/ -#if !defined(l_castU2S) -#define l_castU2S(i) ((lua_Integer)(i)) -#endif - - -/* -** non-return type -*/ -#if defined(__GNUC__) -#define l_noret void __attribute__((noreturn)) -#elif defined(_MSC_VER) && _MSC_VER >= 1200 -#define l_noret void __declspec(noreturn) -#else -#define l_noret void -#endif - - - -/* -** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 -#endif - - - -/* -** type for virtual-machine instructions; -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) -*/ -#if LUAI_BITSINT >= 32 -typedef unsigned int Instruction; -#else -typedef unsigned long Instruction; -#endif - - - -/* -** Maximum length for short strings, that is, strings that are -** internalized. (Cannot be smaller than reserved words or tags for -** metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#if !defined(LUAI_MAXSHORTLEN) -#define LUAI_MAXSHORTLEN 40 -#endif - - -/* -** Initial size for the string table (must be power of 2). -** The Lua core alone registers ~50 strings (reserved words + -** metaevent keys + a few others). Libraries would typically add -** a few dozens more. -*/ -#if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 128 -#endif - - -/* -** Size of cache for strings in the API. 'N' is the number of -** sets (better be a prime) and "M" is the size of each set (M == 1 -** makes a direct cache.) -*/ -#if !defined(STRCACHE_N) -#define STRCACHE_N 53 -#define STRCACHE_M 2 -#endif - - -/* minimum size for string buffer */ -#if !defined(LUA_MINBUFFER) -#define LUA_MINBUFFER 32 -#endif - - -/* -** macros that are executed whenever program enters the Lua core -** ('lua_lock') and leaves the core ('lua_unlock') -*/ -#if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif - -/* -** macro executed during Lua functions at points where the -** function can yield. -*/ -#if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} -#endif - - -/* -** these macros allow user-specific actions on threads when you defined -** LUAI_EXTRASPACE and need to do something extra when a thread is -** created/deleted/resumed/yielded. -*/ -#if !defined(luai_userstateopen) -#define luai_userstateopen(L) ((void)L) -#endif - -#if !defined(luai_userstateclose) -#define luai_userstateclose(L) ((void)L) -#endif - -#if !defined(luai_userstatethread) -#define luai_userstatethread(L,L1) ((void)L) -#endif - -#if !defined(luai_userstatefree) -#define luai_userstatefree(L,L1) ((void)L) -#endif - -#if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) -#endif - -#if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) -#endif - - - -/* -** The luai_num* macros define the primitive operations over numbers. -*/ - -/* floor division (defined as 'floor(a/b)') */ -#if !defined(luai_numidiv) -#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) -#endif - -/* float division */ -#if !defined(luai_numdiv) -#define luai_numdiv(L,a,b) ((a)/(b)) -#endif - -/* -** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when -** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of -** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) -** ~= floor(a/b)'. That happens when the division has a non-integer -** negative result, which is equivalent to the test below. -*/ -#if !defined(luai_nummod) -#define luai_nummod(L,a,b,m) \ - { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } -#endif - -/* exponentiation */ -#if !defined(luai_numpow) -#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) -#endif - -/* the others are quite standard operations */ -#if !defined(luai_numadd) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numunm(L,a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(a,b) ((a)<(b)) -#define luai_numle(a,b) ((a)<=(b)) -#define luai_numisnan(a) (!luai_numeq((a), (a))) -#endif - - - - - -/* -** macro to control inclusion of some hard tests on stack reallocation -*/ -#if !defined(HARDSTACKTESTS) -#define condmovestack(L,pre,pos) ((void)0) -#else -/* realloc stack keeping its size */ -#define condmovestack(L,pre,pos) \ - { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; } -#endif - -#if !defined(HARDMEMTESTS) -#define condchangemem(L,pre,pos) ((void)0) -#else -#define condchangemem(L,pre,pos) \ - { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmathlib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmathlib.c deleted file mode 100644 index 7ef7e59..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmathlib.c +++ /dev/null @@ -1,410 +0,0 @@ -/* -** $Id: lmathlib.c,v 1.119.1.1 2017/04/19 17:20:42 roberto Exp $ -** Standard mathematical library -** See Copyright Notice in lua.h -*/ - -#define lmathlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#undef PI -#define PI (l_mathop(3.141592653589793238462643383279502884)) - - -#if !defined(l_rand) /* { */ -#if defined(LUA_USE_POSIX) -#define l_rand() random() -#define l_srand(x) srandom(x) -#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */ -#else -#define l_rand() rand() -#define l_srand(x) srand(x) -#define L_RANDMAX RAND_MAX -#endif -#endif /* } */ - - -static int math_abs (lua_State *L) { - if (lua_isinteger(L, 1)) { - lua_Integer n = lua_tointeger(L, 1); - if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n); - lua_pushinteger(L, n); - } - else - lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sin (lua_State *L) { - lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_cos (lua_State *L) { - lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tan (lua_State *L) { - lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_asin (lua_State *L) { - lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_acos (lua_State *L) { - lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_atan (lua_State *L) { - lua_Number y = luaL_checknumber(L, 1); - lua_Number x = luaL_optnumber(L, 2, 1); - lua_pushnumber(L, l_mathop(atan2)(y, x)); - return 1; -} - - -static int math_toint (lua_State *L) { - int valid; - lua_Integer n = lua_tointegerx(L, 1, &valid); - if (valid) - lua_pushinteger(L, n); - else { - luaL_checkany(L, 1); - lua_pushnil(L); /* value is not convertible to integer */ - } - return 1; -} - - -static void pushnumint (lua_State *L, lua_Number d) { - lua_Integer n; - if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */ - lua_pushinteger(L, n); /* result is integer */ - else - lua_pushnumber(L, d); /* result is float */ -} - - -static int math_floor (lua_State *L) { - if (lua_isinteger(L, 1)) - lua_settop(L, 1); /* integer is its own floor */ - else { - lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1)); - pushnumint(L, d); - } - return 1; -} - - -static int math_ceil (lua_State *L) { - if (lua_isinteger(L, 1)) - lua_settop(L, 1); /* integer is its own ceil */ - else { - lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); - pushnumint(L, d); - } - return 1; -} - - -static int math_fmod (lua_State *L) { - if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { - lua_Integer d = lua_tointeger(L, 2); - if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ - luaL_argcheck(L, d != 0, 2, "zero"); - lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ - } - else - lua_pushinteger(L, lua_tointeger(L, 1) % d); - } - else - lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); - return 1; -} - - -/* -** next function does not use 'modf', avoiding problems with 'double*' -** (which is not compatible with 'float*') when lua_Number is not -** 'double'. -*/ -static int math_modf (lua_State *L) { - if (lua_isinteger(L ,1)) { - lua_settop(L, 1); /* number is its own integer part */ - lua_pushnumber(L, 0); /* no fractional part */ - } - else { - lua_Number n = luaL_checknumber(L, 1); - /* integer part (rounds toward zero) */ - lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); - pushnumint(L, ip); - /* fractional part (test needed for inf/-inf) */ - lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip)); - } - return 2; -} - - -static int math_sqrt (lua_State *L) { - lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); - return 1; -} - - -static int math_ult (lua_State *L) { - lua_Integer a = luaL_checkinteger(L, 1); - lua_Integer b = luaL_checkinteger(L, 2); - lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b); - return 1; -} - -static int math_log (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number res; - if (lua_isnoneornil(L, 2)) - res = l_mathop(log)(x); - else { - lua_Number base = luaL_checknumber(L, 2); -#if !defined(LUA_USE_C89) - if (base == l_mathop(2.0)) - res = l_mathop(log2)(x); else -#endif - if (base == l_mathop(10.0)) - res = l_mathop(log10)(x); - else - res = l_mathop(log)(x)/l_mathop(log)(base); - } - lua_pushnumber(L, res); - return 1; -} - -static int math_exp (lua_State *L) { - lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_deg (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); - return 1; -} - -static int math_rad (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); - return 1; -} - - -static int math_min (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int imin = 1; /* index of current minimum value */ - int i; - luaL_argcheck(L, n >= 1, 1, "value expected"); - for (i = 2; i <= n; i++) { - if (lua_compare(L, i, imin, LUA_OPLT)) - imin = i; - } - lua_pushvalue(L, imin); - return 1; -} - - -static int math_max (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int imax = 1; /* index of current maximum value */ - int i; - luaL_argcheck(L, n >= 1, 1, "value expected"); - for (i = 2; i <= n; i++) { - if (lua_compare(L, imax, i, LUA_OPLT)) - imax = i; - } - lua_pushvalue(L, imax); - return 1; -} - -/* -** This function uses 'double' (instead of 'lua_Number') to ensure that -** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0' -** will keep full precision (ensuring that 'r' is always less than 1.0.) -*/ -static int math_random (lua_State *L) { - lua_Integer low, up; - double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0)); - switch (lua_gettop(L)) { /* check number of arguments */ - case 0: { /* no arguments */ - lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */ - return 1; - } - case 1: { /* only upper limit */ - low = 1; - up = luaL_checkinteger(L, 1); - break; - } - case 2: { /* lower and upper limits */ - low = luaL_checkinteger(L, 1); - up = luaL_checkinteger(L, 2); - break; - } - default: return luaL_error(L, "wrong number of arguments"); - } - /* random integer in the interval [low, up] */ - luaL_argcheck(L, low <= up, 1, "interval is empty"); - luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, - "interval too large"); - r *= (double)(up - low) + 1.0; - lua_pushinteger(L, (lua_Integer)r + low); - return 1; -} - - -static int math_randomseed (lua_State *L) { - l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); - (void)l_rand(); /* discard first value to avoid undesirable correlations */ - return 0; -} - - -static int math_type (lua_State *L) { - if (lua_type(L, 1) == LUA_TNUMBER) { - if (lua_isinteger(L, 1)) - lua_pushliteral(L, "integer"); - else - lua_pushliteral(L, "float"); - } - else { - luaL_checkany(L, 1); - lua_pushnil(L); - } - return 1; -} - - -/* -** {================================================================== -** Deprecated functions (for compatibility only) -** =================================================================== -*/ -#if defined(LUA_COMPAT_MATHLIB) - -static int math_cosh (lua_State *L) { - lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sinh (lua_State *L) { - lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tanh (lua_State *L) { - lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_pow (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number y = luaL_checknumber(L, 2); - lua_pushnumber(L, l_mathop(pow)(x, y)); - return 1; -} - -static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); - return 2; -} - -static int math_ldexp (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - int ep = (int)luaL_checkinteger(L, 2); - lua_pushnumber(L, l_mathop(ldexp)(x, ep)); - return 1; -} - -static int math_log10 (lua_State *L) { - lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); - return 1; -} - -#endif -/* }================================================================== */ - - - -static const luaL_Reg mathlib[] = { - {"abs", math_abs}, - {"acos", math_acos}, - {"asin", math_asin}, - {"atan", math_atan}, - {"ceil", math_ceil}, - {"cos", math_cos}, - {"deg", math_deg}, - {"exp", math_exp}, - {"tointeger", math_toint}, - {"floor", math_floor}, - {"fmod", math_fmod}, - {"ult", math_ult}, - {"log", math_log}, - {"max", math_max}, - {"min", math_min}, - {"modf", math_modf}, - {"rad", math_rad}, - {"random", math_random}, - {"randomseed", math_randomseed}, - {"sin", math_sin}, - {"sqrt", math_sqrt}, - {"tan", math_tan}, - {"type", math_type}, -#if defined(LUA_COMPAT_MATHLIB) - {"atan2", math_atan}, - {"cosh", math_cosh}, - {"sinh", math_sinh}, - {"tanh", math_tanh}, - {"pow", math_pow}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, - {"log10", math_log10}, -#endif - /* placeholders */ - {"pi", NULL}, - {"huge", NULL}, - {"maxinteger", NULL}, - {"mininteger", NULL}, - {NULL, NULL} -}; - - -/* -** Open math library -*/ -LUAMOD_API int luaopen_math (lua_State *L) { - luaL_newlib(L, mathlib); - lua_pushnumber(L, PI); - lua_setfield(L, -2, "pi"); - lua_pushnumber(L, (lua_Number)HUGE_VAL); - lua_setfield(L, -2, "huge"); - lua_pushinteger(L, LUA_MAXINTEGER); - lua_setfield(L, -2, "maxinteger"); - lua_pushinteger(L, LUA_MININTEGER); - lua_setfield(L, -2, "mininteger"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.c deleted file mode 100644 index 0241cc3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.c +++ /dev/null @@ -1,100 +0,0 @@ -/* -** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - -#define lmem_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -/* -** About the realloc function: -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); -** ('osize' is the old size, 'nsize' is the new size) -** -** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no -** matter 'x'). -** -** * frealloc(ud, p, x, 0) frees the block 'p' -** (in this specific case, frealloc must return NULL); -** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ISO C) -** -** frealloc returns NULL if it cannot create or reallocate the area -** (any reallocation to an equal or smaller size cannot fail!) -*/ - - - -#define MINSIZEARRAY 4 - - -void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, - int limit, const char *what) { - void *newblock; - int newsize; - if (*size >= limit/2) { /* cannot double it? */ - if (*size >= limit) /* cannot grow even a little? */ - luaG_runerror(L, "too many %s (limit is %d)", what, limit); - newsize = limit; /* still have at least one free place */ - } - else { - newsize = (*size)*2; - if (newsize < MINSIZEARRAY) - newsize = MINSIZEARRAY; /* minimum size */ - } - newblock = luaM_reallocv(L, block, *size, newsize, size_elems); - *size = newsize; /* update only when everything else is OK */ - return newblock; -} - - -l_noret luaM_toobig (lua_State *L) { - luaG_runerror(L, "memory allocation error: block too big"); -} - - - -/* -** generic allocation routine. -*/ -void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { - void *newblock; - global_State *g = G(L); - size_t realosize = (block) ? osize : 0; - lua_assert((realosize == 0) == (block == NULL)); -#if defined(HARDMEMTESTS) - if (nsize > realosize && g->gcrunning) - luaC_fullgc(L, 1); /* force a GC whenever possible */ -#endif - newblock = (*g->frealloc)(g->ud, block, osize, nsize); - if (newblock == NULL && nsize > 0) { - lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ - if (g->version) { /* is state fully built? */ - luaC_fullgc(L, 1); /* try to free some memory... */ - newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ - } - if (newblock == NULL) - luaD_throw(L, LUA_ERRMEM); - } - lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - realosize; - return newblock; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.h deleted file mode 100644 index 357b1e4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lmem.h +++ /dev/null @@ -1,69 +0,0 @@ -/* -** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - -#ifndef lmem_h -#define lmem_h - - -#include - -#include "llimits.h" -#include "lua.h" - - -/* -** This macro reallocs a vector 'b' from 'on' to 'n' elements, where -** each element has size 'e'. In case of arithmetic overflow of the -** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because -** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). -** -** (The macro is somewhat complex to avoid warnings: The 'sizeof' -** comparison avoids a runtime comparison when overflow cannot occur. -** The compiler should be able to optimize the real test by itself, but -** when it does it, it may give a warning about "comparison is always -** false due to limited range of data type"; the +1 tricks the compiler, -** avoiding this warning but also this optimization.) -*/ -#define luaM_reallocv(L,b,on,n,e) \ - (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ - ? luaM_toobig(L) : cast_void(0)) , \ - luaM_realloc_(L, (b), (on)*(e), (n)*(e))) - -/* -** Arrays of chars do not need any test -*/ -#define luaM_reallocvchar(L,b,on,n) \ - cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) - -#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) -#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) -#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) - -#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) -#define luaM_newvector(L,n,t) \ - cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) - -#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) - -#define luaM_growvector(L,v,nelems,size,t,limit,e) \ - if ((nelems)+1 > (size)) \ - ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) - -#define luaM_reallocvector(L, v,oldn,n,t) \ - ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) - -LUAI_FUNC l_noret luaM_toobig (lua_State *L); - -/* not to be called directly */ -LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, - size_t size); -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, - size_t size_elem, int limit, - const char *what); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/loadlib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/loadlib.c deleted file mode 100644 index 45f44d3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/loadlib.c +++ /dev/null @@ -1,790 +0,0 @@ -/* -** $Id: loadlib.c,v 1.130.1.1 2017/04/19 17:20:42 roberto Exp $ -** Dynamic library loader for Lua -** See Copyright Notice in lua.h -** -** This module contains an implementation of loadlib for Unix systems -** that have dlfcn, an implementation for Windows, and a stub for other -** systems. -*/ - -#define loadlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** LUA_IGMARK is a mark to ignore all before it when building the -** luaopen_ function name. -*/ -#if !defined (LUA_IGMARK) -#define LUA_IGMARK "-" -#endif - - -/* -** LUA_CSUBSEP is the character that replaces dots in submodule names -** when searching for a C loader. -** LUA_LSUBSEP is the character that replaces dots in submodule names -** when searching for a Lua loader. -*/ -#if !defined(LUA_CSUBSEP) -#define LUA_CSUBSEP LUA_DIRSEP -#endif - -#if !defined(LUA_LSUBSEP) -#define LUA_LSUBSEP LUA_DIRSEP -#endif - - -/* prefix for open functions in C libraries */ -#define LUA_POF "luaopen_" - -/* separator for open functions in C libraries */ -#define LUA_OFSEP "_" - - -/* -** unique key for table in the registry that keeps handles -** for all loaded C libraries -*/ -static const int CLIBS = 0; - -#define LIB_FAIL "open" - - -#define setprogdir(L) ((void)0) - - -/* -** system-dependent functions -*/ - -/* -** unload library 'lib' -*/ -static void lsys_unloadlib (void *lib); - -/* -** load C library in file 'path'. If 'seeglb', load with all names in -** the library global. -** Returns the library; in case of error, returns NULL plus an -** error string in the stack. -*/ -static void *lsys_load (lua_State *L, const char *path, int seeglb); - -/* -** Try to find a function named 'sym' in library 'lib'. -** Returns the function; in case of error, returns NULL plus an -** error string in the stack. -*/ -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); - - - - -#if defined(LUA_USE_DLOPEN) /* { */ -/* -** {======================================================================== -** This is an implementation of loadlib based on the dlfcn interface. -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -** as an emulation layer on top of native functions. -** ========================================================================= -*/ - -#include - -/* -** Macro to convert pointer-to-void* to pointer-to-function. This cast -** is undefined according to ISO C, but POSIX assumes that it works. -** (The '__extension__' in gnu compilers is only to avoid warnings.) -*/ -#if defined(__GNUC__) -#define cast_func(p) (__extension__ (lua_CFunction)(p)) -#else -#define cast_func(p) ((lua_CFunction)(p)) -#endif - - -static void lsys_unloadlib (void *lib) { - dlclose(lib); -} - - -static void *lsys_load (lua_State *L, const char *path, int seeglb) { - void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); - if (lib == NULL) lua_pushstring(L, dlerror()); - return lib; -} - - -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = cast_func(dlsym(lib, sym)); - if (f == NULL) lua_pushstring(L, dlerror()); - return f; -} - -/* }====================================================== */ - - - -#elif defined(LUA_DL_DLL) /* }{ */ -/* -** {====================================================================== -** This is an implementation of loadlib for Windows using native functions. -** ======================================================================= -*/ - -#include - - -/* -** optional flags for LoadLibraryEx -*/ -#if !defined(LUA_LLE_FLAGS) -#define LUA_LLE_FLAGS 0 -#endif - - -#undef setprogdir - - -/* -** Replace in the path (on the top of the stack) any occurrence -** of LUA_EXEC_DIR with the executable's path. -*/ -static void setprogdir (lua_State *L) { - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) - luaL_error(L, "unable to get ModuleFileName"); - else { - *lb = '\0'; /* cut name on the last '\\' to get the path */ - luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - - - - -static void pusherror (lua_State *L) { - int error = GetLastError(); - char buffer[128]; - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL)) - lua_pushstring(L, buffer); - else - lua_pushfstring(L, "system error %d\n", error); -} - -static void lsys_unloadlib (void *lib) { - FreeLibrary((HMODULE)lib); -} - - -static void *lsys_load (lua_State *L, const char *path, int seeglb) { - HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS); - (void)(seeglb); /* not used: symbols are 'global' by default */ - if (lib == NULL) pusherror(L); - return lib; -} - - -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym); - if (f == NULL) pusherror(L); - return f; -} - -/* }====================================================== */ - - -#else /* }{ */ -/* -** {====================================================== -** Fallback for other systems -** ======================================================= -*/ - -#undef LIB_FAIL -#define LIB_FAIL "absent" - - -#define DLMSG "dynamic libraries not enabled; check your Lua installation" - - -static void lsys_unloadlib (void *lib) { - (void)(lib); /* not used */ -} - - -static void *lsys_load (lua_State *L, const char *path, int seeglb) { - (void)(path); (void)(seeglb); /* not used */ - lua_pushliteral(L, DLMSG); - return NULL; -} - - -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - (void)(lib); (void)(sym); /* not used */ - lua_pushliteral(L, DLMSG); - return NULL; -} - -/* }====================================================== */ -#endif /* } */ - - -/* -** {================================================================== -** Set Paths -** =================================================================== -*/ - -/* -** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment -** variables that Lua check to set its paths. -*/ -#if !defined(LUA_PATH_VAR) -#define LUA_PATH_VAR "LUA_PATH" -#endif - -#if !defined(LUA_CPATH_VAR) -#define LUA_CPATH_VAR "LUA_CPATH" -#endif - - -#define AUXMARK "\1" /* auxiliary mark */ - - -/* -** return registry.LUA_NOENV as a boolean -*/ -static int noenv (lua_State *L) { - int b; - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - b = lua_toboolean(L, -1); - lua_pop(L, 1); /* remove value */ - return b; -} - - -/* -** Set a path -*/ -static void setpath (lua_State *L, const char *fieldname, - const char *envname, - const char *dft) { - const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); - const char *path = getenv(nver); /* use versioned name */ - if (path == NULL) /* no environment variable? */ - path = getenv(envname); /* try unversioned name */ - if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, dft); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, dft); - lua_remove(L, -2); /* remove result from 1st 'gsub' */ - } - setprogdir(L); - lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ - lua_pop(L, 1); /* pop versioned variable name */ -} - -/* }================================================================== */ - - -/* -** return registry.CLIBS[path] -*/ -static void *checkclib (lua_State *L, const char *path) { - void *plib; - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); - lua_getfield(L, -1, path); - plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ - lua_pop(L, 2); /* pop CLIBS table and 'plib' */ - return plib; -} - - -/* -** registry.CLIBS[path] = plib -- for queries -** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries -*/ -static void addtoclib (lua_State *L, const char *path, void *plib) { - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); - lua_pushlightuserdata(L, plib); - lua_pushvalue(L, -1); - lua_setfield(L, -3, path); /* CLIBS[path] = plib */ - lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ - lua_pop(L, 1); /* pop CLIBS table */ -} - - -/* -** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib -** handles in list CLIBS -*/ -static int gctm (lua_State *L) { - lua_Integer n = luaL_len(L, 1); - for (; n >= 1; n--) { /* for each handle, in reverse order */ - lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - lsys_unloadlib(lua_touserdata(L, -1)); - lua_pop(L, 1); /* pop handle */ - } - return 0; -} - - - -/* error codes for 'lookforfunc' */ -#define ERRLIB 1 -#define ERRFUNC 2 - -/* -** Look for a C function named 'sym' in a dynamically loaded library -** 'path'. -** First, check whether the library is already loaded; if not, try -** to load it. -** Then, if 'sym' is '*', return true (as library has been loaded). -** Otherwise, look for symbol 'sym' in the library and push a -** C function with that symbol. -** Return 0 and 'true' or a function in the stack; in case of -** errors, return an error code and an error message in the stack. -*/ -static int lookforfunc (lua_State *L, const char *path, const char *sym) { - void *reg = checkclib(L, path); /* check loaded C libraries */ - if (reg == NULL) { /* must load library? */ - reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ - if (reg == NULL) return ERRLIB; /* unable to load library */ - addtoclib(L, path, reg); - } - if (*sym == '*') { /* loading only library (no function)? */ - lua_pushboolean(L, 1); /* return 'true' */ - return 0; /* no errors */ - } - else { - lua_CFunction f = lsys_sym(L, reg, sym); - if (f == NULL) - return ERRFUNC; /* unable to find function */ - lua_pushcfunction(L, f); /* else create new function */ - return 0; /* no errors */ - } -} - - -static int ll_loadlib (lua_State *L) { - const char *path = luaL_checkstring(L, 1); - const char *init = luaL_checkstring(L, 2); - int stat = lookforfunc(L, path, init); - if (stat == 0) /* no errors? */ - return 1; /* return the loaded function */ - else { /* error; error message is on stack top */ - lua_pushnil(L); - lua_insert(L, -2); - lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ - } -} - - - -/* -** {====================================================== -** 'require' function -** ======================================================= -*/ - - -static int readable (const char *filename) { - FILE *f = fopen(filename, "r"); /* try to open file */ - if (f == NULL) return 0; /* open failed */ - fclose(f); - return 1; -} - - -static const char *pushnexttemplate (lua_State *L, const char *path) { - const char *l; - while (*path == *LUA_PATH_SEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ - l = strchr(path, *LUA_PATH_SEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, l - path); /* template */ - return l; -} - - -static const char *searchpath (lua_State *L, const char *name, - const char *path, - const char *sep, - const char *dirsep) { - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - if (*sep != '\0') /* non-empty separator? */ - name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename = luaL_gsub(L, lua_tostring(L, -1), - LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ - if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file '%s'", filename); - lua_remove(L, -2); /* remove file name */ - luaL_addvalue(&msg); /* concatenate error msg. entry */ - } - luaL_pushresult(&msg); /* create error message */ - return NULL; /* not found */ -} - - -static int ll_searchpath (lua_State *L) { - const char *f = searchpath(L, luaL_checkstring(L, 1), - luaL_checkstring(L, 2), - luaL_optstring(L, 3, "."), - luaL_optstring(L, 4, LUA_DIRSEP)); - if (f != NULL) return 1; - else { /* error message is on top of the stack */ - lua_pushnil(L); - lua_insert(L, -2); - return 2; /* return nil + error message */ - } -} - - -static const char *findfile (lua_State *L, const char *name, - const char *pname, - const char *dirsep) { - const char *path; - lua_getfield(L, lua_upvalueindex(1), pname); - path = lua_tostring(L, -1); - if (path == NULL) - luaL_error(L, "'package.%s' must be a string", pname); - return searchpath(L, name, path, ".", dirsep); -} - - -static int checkload (lua_State *L, int stat, const char *filename) { - if (stat) { /* module loaded successfully? */ - lua_pushstring(L, filename); /* will be 2nd argument to module */ - return 2; /* return open function and file name */ - } - else - return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", - lua_tostring(L, 1), filename, lua_tostring(L, -1)); -} - - -static int searcher_Lua (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - filename = findfile(L, name, "path", LUA_LSUBSEP); - if (filename == NULL) return 1; /* module not found in this path */ - return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); -} - - -/* -** Try to find a load function for module 'modname' at file 'filename'. -** First, change '.' to '_' in 'modname'; then, if 'modname' has -** the form X-Y (that is, it has an "ignore mark"), build a function -** name "luaopen_X" and look for it. (For compatibility, if that -** fails, it also tries "luaopen_Y".) If there is no ignore mark, -** look for a function named "luaopen_modname". -*/ -static int loadfunc (lua_State *L, const char *filename, const char *modname) { - const char *openfunc; - const char *mark; - modname = luaL_gsub(L, modname, ".", LUA_OFSEP); - mark = strchr(modname, *LUA_IGMARK); - if (mark) { - int stat; - openfunc = lua_pushlstring(L, modname, mark - modname); - openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); - stat = lookforfunc(L, filename, openfunc); - if (stat != ERRFUNC) return stat; - modname = mark + 1; /* else go ahead and try old-style name */ - } - openfunc = lua_pushfstring(L, LUA_POF"%s", modname); - return lookforfunc(L, filename, openfunc); -} - - -static int searcher_C (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); - if (filename == NULL) return 1; /* module not found in this path */ - return checkload(L, (loadfunc(L, filename, name) == 0), filename); -} - - -static int searcher_Croot (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - const char *p = strchr(name, '.'); - int stat; - if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, p - name); - filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); - if (filename == NULL) return 1; /* root not found */ - if ((stat = loadfunc(L, filename, name)) != 0) { - if (stat != ERRFUNC) - return checkload(L, 0, filename); /* real error */ - else { /* open function not found */ - lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename); - return 1; - } - } - lua_pushstring(L, filename); /* will be 2nd argument to module */ - return 2; -} - - -static int searcher_preload (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - return 1; -} - - -static void findloader (lua_State *L, const char *name) { - int i; - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - /* push 'package.searchers' to index 3 in the stack */ - if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) - luaL_error(L, "'package.searchers' must be a table"); - /* iterate over available searchers to find a loader */ - for (i = 1; ; i++) { - if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ - lua_pop(L, 1); /* remove nil */ - luaL_pushresult(&msg); /* create error message */ - luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); - } - lua_pushstring(L, name); - lua_call(L, 1, 2); /* call it */ - if (lua_isfunction(L, -2)) /* did it find a loader? */ - return; /* module loader found */ - else if (lua_isstring(L, -2)) { /* searcher returned error message? */ - lua_pop(L, 1); /* remove extra return */ - luaL_addvalue(&msg); /* concatenate error message */ - } - else - lua_pop(L, 2); /* remove both returns */ - } -} - - -static int ll_require (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_settop(L, 1); /* LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - lua_getfield(L, 2, name); /* LOADED[name] */ - if (lua_toboolean(L, -1)) /* is it there? */ - return 1; /* package is already loaded */ - /* else must load package */ - lua_pop(L, 1); /* remove 'getfield' result */ - findloader(L, name); - lua_pushstring(L, name); /* pass name as argument to module loader */ - lua_insert(L, -2); /* name is 1st argument (before search data) */ - lua_call(L, 2, 1); /* run loader to load module */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* LOADED[name] = returned value */ - if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ - lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* LOADED[name] = true */ - } - return 1; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** 'module' function -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -/* -** changes the environment variable of calling function -*/ -static void set_env (lua_State *L) { - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - luaL_error(L, "'module' not called from a Lua function"); - lua_pushvalue(L, -2); /* copy new environment table to top */ - lua_setupvalue(L, -2, 1); - lua_pop(L, 1); /* remove function */ -} - - -static void dooptions (lua_State *L, int n) { - int i; - for (i = 2; i <= n; i++) { - if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */ - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } - } -} - - -static void modinit (lua_State *L, const char *modname) { - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; - else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, dot - modname); - lua_setfield(L, -2, "_PACKAGE"); -} - - -static int ll_module (lua_State *L) { - const char *modname = luaL_checkstring(L, 1); - int lastarg = lua_gettop(L); /* last parameter */ - luaL_pushmodule(L, modname, 1); /* get/create module table */ - /* check whether table already has a _NAME field */ - if (lua_getfield(L, -1, "_NAME") != LUA_TNIL) - lua_pop(L, 1); /* table is an initialized module */ - else { /* no; initialize it */ - lua_pop(L, 1); - modinit(L, modname); - } - lua_pushvalue(L, -1); - set_env(L); - dooptions(L, lastarg); - return 1; -} - - -static int ll_seeall (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushglobaltable(L); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - -#endif -/* }====================================================== */ - - - -static const luaL_Reg pk_funcs[] = { - {"loadlib", ll_loadlib}, - {"searchpath", ll_searchpath}, -#if defined(LUA_COMPAT_MODULE) - {"seeall", ll_seeall}, -#endif - /* placeholders */ - {"preload", NULL}, - {"cpath", NULL}, - {"path", NULL}, - {"searchers", NULL}, - {"loaded", NULL}, - {NULL, NULL} -}; - - -static const luaL_Reg ll_funcs[] = { -#if defined(LUA_COMPAT_MODULE) - {"module", ll_module}, -#endif - {"require", ll_require}, - {NULL, NULL} -}; - - -static void createsearcherstable (lua_State *L) { - static const lua_CFunction searchers[] = - {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; - int i; - /* create 'searchers' table */ - lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); - /* fill it with predefined searchers */ - for (i=0; searchers[i] != NULL; i++) { - lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ - lua_pushcclosure(L, searchers[i], 1); - lua_rawseti(L, -2, i+1); - } -#if defined(LUA_COMPAT_LOADERS) - lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ - lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */ -#endif - lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ -} - - -/* -** create table CLIBS to keep track of loaded C libraries, -** setting a finalizer to close all libraries when closing state. -*/ -static void createclibstable (lua_State *L) { - lua_newtable(L); /* create CLIBS table */ - lua_createtable(L, 0, 1); /* create metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); - lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */ -} - - -LUAMOD_API int luaopen_package (lua_State *L) { - createclibstable(L); - luaL_newlib(L, pk_funcs); /* create 'package' table */ - createsearcherstable(L); - /* set paths */ - setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); - setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); - /* store config information */ - lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" - LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); - lua_setfield(L, -2, "config"); - /* set field 'loaded' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - lua_setfield(L, -2, "loaded"); - /* set field 'preload' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - lua_setfield(L, -2, "preload"); - lua_pushglobaltable(L); - lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ - luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ - lua_pop(L, 1); /* pop global table */ - return 1; /* return 'package' table */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.c deleted file mode 100644 index 355bf58..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.c +++ /dev/null @@ -1,522 +0,0 @@ -/* -** $Id: lobject.c,v 2.113.1.1 2017/04/19 17:29:57 roberto Exp $ -** Some generic functions over Lua objects -** See Copyright Notice in lua.h -*/ - -#define lobject_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lctype.h" -#include "ldebug.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "lvm.h" - - - -LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; - - -/* -** converts an integer to a "floating point byte", represented as -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if -** eeeee != 0 and (xxx) otherwise. -*/ -int luaO_int2fb (unsigned int x) { - int e = 0; /* exponent */ - if (x < 8) return x; - while (x >= (8 << 4)) { /* coarse steps */ - x = (x + 0xf) >> 4; /* x = ceil(x / 16) */ - e += 4; - } - while (x >= (8 << 1)) { /* fine steps */ - x = (x + 1) >> 1; /* x = ceil(x / 2) */ - e++; - } - return ((e+1) << 3) | (cast_int(x) - 8); -} - - -/* converts back */ -int luaO_fb2int (int x) { - return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); -} - - -/* -** Computes ceil(log2(x)) -*/ -int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 - }; - int l = 0; - x--; - while (x >= 256) { l += 8; x >>= 8; } - return l + log_2[x]; -} - - -static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, - lua_Integer v2) { - switch (op) { - case LUA_OPADD: return intop(+, v1, v2); - case LUA_OPSUB:return intop(-, v1, v2); - case LUA_OPMUL:return intop(*, v1, v2); - case LUA_OPMOD: return luaV_mod(L, v1, v2); - case LUA_OPIDIV: return luaV_div(L, v1, v2); - case LUA_OPBAND: return intop(&, v1, v2); - case LUA_OPBOR: return intop(|, v1, v2); - case LUA_OPBXOR: return intop(^, v1, v2); - case LUA_OPSHL: return luaV_shiftl(v1, v2); - case LUA_OPSHR: return luaV_shiftl(v1, -v2); - case LUA_OPUNM: return intop(-, 0, v1); - case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); - default: lua_assert(0); return 0; - } -} - - -static lua_Number numarith (lua_State *L, int op, lua_Number v1, - lua_Number v2) { - switch (op) { - case LUA_OPADD: return luai_numadd(L, v1, v2); - case LUA_OPSUB: return luai_numsub(L, v1, v2); - case LUA_OPMUL: return luai_nummul(L, v1, v2); - case LUA_OPDIV: return luai_numdiv(L, v1, v2); - case LUA_OPPOW: return luai_numpow(L, v1, v2); - case LUA_OPIDIV: return luai_numidiv(L, v1, v2); - case LUA_OPUNM: return luai_numunm(L, v1); - case LUA_OPMOD: { - lua_Number m; - luai_nummod(L, v1, v2, m); - return m; - } - default: lua_assert(0); return 0; - } -} - - -void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, - TValue *res) { - switch (op) { - case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: - case LUA_OPSHL: case LUA_OPSHR: - case LUA_OPBNOT: { /* operate only on integers */ - lua_Integer i1; lua_Integer i2; - if (tointeger(p1, &i1) && tointeger(p2, &i2)) { - setivalue(res, intarith(L, op, i1, i2)); - return; - } - else break; /* go to the end */ - } - case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ - lua_Number n1; lua_Number n2; - if (tonumber(p1, &n1) && tonumber(p2, &n2)) { - setfltvalue(res, numarith(L, op, n1, n2)); - return; - } - else break; /* go to the end */ - } - default: { /* other operations */ - lua_Number n1; lua_Number n2; - if (ttisinteger(p1) && ttisinteger(p2)) { - setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); - return; - } - else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { - setfltvalue(res, numarith(L, op, n1, n2)); - return; - } - else break; /* go to the end */ - } - } - /* could not perform raw operation; try metamethod */ - lua_assert(L != NULL); /* should not fail when folding (compile time) */ - luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); -} - - -int luaO_hexavalue (int c) { - if (lisdigit(c)) return c - '0'; - else return (ltolower(c) - 'a') + 10; -} - - -static int isneg (const char **s) { - if (**s == '-') { (*s)++; return 1; } - else if (**s == '+') (*s)++; - return 0; -} - - - -/* -** {================================================================== -** Lua's implementation for 'lua_strx2number' -** =================================================================== -*/ - -#if !defined(lua_strx2number) - -/* maximum number of significant digits to read (to avoid overflows - even with single floats) */ -#define MAXSIGDIG 30 - -/* -** convert an hexadecimal numeric string to a number, following -** C99 specification for 'strtod' -*/ -static lua_Number lua_strx2number (const char *s, char **endptr) { - int dot = lua_getlocaledecpoint(); - lua_Number r = 0.0; /* result (accumulator) */ - int sigdig = 0; /* number of significant digits */ - int nosigdig = 0; /* number of non-significant digits */ - int e = 0; /* exponent correction */ - int neg; /* 1 if number is negative */ - int hasdot = 0; /* true after seen a dot */ - *endptr = cast(char *, s); /* nothing is valid yet */ - while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); /* check signal */ - if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ - return 0.0; /* invalid format (no '0x') */ - for (s += 2; ; s++) { /* skip '0x' and read numeral */ - if (*s == dot) { - if (hasdot) break; /* second dot? stop loop */ - else hasdot = 1; - } - else if (lisxdigit(cast_uchar(*s))) { - if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ - nosigdig++; - else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ - r = (r * cast_num(16.0)) + luaO_hexavalue(*s); - else e++; /* too many digits; ignore, but still count for exponent */ - if (hasdot) e--; /* decimal digit? correct exponent */ - } - else break; /* neither a dot nor a digit */ - } - if (nosigdig + sigdig == 0) /* no digits? */ - return 0.0; /* invalid format */ - *endptr = cast(char *, s); /* valid up to here */ - e *= 4; /* each digit multiplies/divides value by 2^4 */ - if (*s == 'p' || *s == 'P') { /* exponent part? */ - int exp1 = 0; /* exponent value */ - int neg1; /* exponent signal */ - s++; /* skip 'p' */ - neg1 = isneg(&s); /* signal */ - if (!lisdigit(cast_uchar(*s))) - return 0.0; /* invalid; must have at least one digit */ - while (lisdigit(cast_uchar(*s))) /* read exponent */ - exp1 = exp1 * 10 + *(s++) - '0'; - if (neg1) exp1 = -exp1; - e += exp1; - *endptr = cast(char *, s); /* valid up to here */ - } - if (neg) r = -r; - return l_mathop(ldexp)(r, e); -} - -#endif -/* }====================================================== */ - - -/* maximum length of a numeral */ -#if !defined (L_MAXLENNUM) -#define L_MAXLENNUM 200 -#endif - -static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { - char *endptr; - *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ - : lua_str2number(s, &endptr); - if (endptr == s) return NULL; /* nothing recognized? */ - while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ - return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */ -} - - -/* -** Convert string 's' to a Lua number (put in 'result'). Return NULL -** on fail or the address of the ending '\0' on success. -** 'pmode' points to (and 'mode' contains) special things in the string: -** - 'x'/'X' means an hexadecimal numeral -** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) -** - '.' just optimizes the search for the common case (nothing special) -** This function accepts both the current locale or a dot as the radix -** mark. If the conversion fails, it may mean number has a dot but -** locale accepts something else. In that case, the code copies 's' -** to a buffer (because 's' is read-only), changes the dot to the -** current locale radix mark, and tries to convert again. -*/ -static const char *l_str2d (const char *s, lua_Number *result) { - const char *endptr; - const char *pmode = strpbrk(s, ".xXnN"); - int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; - if (mode == 'n') /* reject 'inf' and 'nan' */ - return NULL; - endptr = l_str2dloc(s, result, mode); /* try to convert */ - if (endptr == NULL) { /* failed? may be a different locale */ - char buff[L_MAXLENNUM + 1]; - const char *pdot = strchr(s, '.'); - if (strlen(s) > L_MAXLENNUM || pdot == NULL) - return NULL; /* string too long or no dot; fail */ - strcpy(buff, s); /* copy string to buffer */ - buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ - endptr = l_str2dloc(buff, result, mode); /* try again */ - if (endptr != NULL) - endptr = s + (endptr - buff); /* make relative to 's' */ - } - return endptr; -} - - -#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) -#define MAXLASTD cast_int(LUA_MAXINTEGER % 10) - -static const char *l_str2int (const char *s, lua_Integer *result) { - lua_Unsigned a = 0; - int empty = 1; - int neg; - while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); - if (s[0] == '0' && - (s[1] == 'x' || s[1] == 'X')) { /* hex? */ - s += 2; /* skip '0x' */ - for (; lisxdigit(cast_uchar(*s)); s++) { - a = a * 16 + luaO_hexavalue(*s); - empty = 0; - } - } - else { /* decimal */ - for (; lisdigit(cast_uchar(*s)); s++) { - int d = *s - '0'; - if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ - return NULL; /* do not accept it (as integer) */ - a = a * 10 + d; - empty = 0; - } - } - while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ - if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ - else { - *result = l_castU2S((neg) ? 0u - a : a); - return s; - } -} - - -size_t luaO_str2num (const char *s, TValue *o) { - lua_Integer i; lua_Number n; - const char *e; - if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ - setivalue(o, i); - } - else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ - setfltvalue(o, n); - } - else - return 0; /* conversion failed */ - return (e - s) + 1; /* success; return string size */ -} - - -int luaO_utf8esc (char *buff, unsigned long x) { - int n = 1; /* number of bytes put in buffer (backwards) */ - lua_assert(x <= 0x10FFFF); - if (x < 0x80) /* ascii? */ - buff[UTF8BUFFSZ - 1] = cast(char, x); - else { /* need continuation bytes */ - unsigned int mfb = 0x3f; /* maximum that fits in first byte */ - do { /* add continuation bytes */ - buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f)); - x >>= 6; /* remove added bits */ - mfb >>= 1; /* now there is one less bit available in first byte */ - } while (x > mfb); /* still needs continuation byte? */ - buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */ - } - return n; -} - - -/* maximum length of the conversion of a number to a string */ -#define MAXNUMBER2STR 50 - - -/* -** Convert a number object to a string -*/ -void luaO_tostring (lua_State *L, StkId obj) { - char buff[MAXNUMBER2STR]; - size_t len; - lua_assert(ttisnumber(obj)); - if (ttisinteger(obj)) - len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); - else { - len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); -#if !defined(LUA_COMPAT_FLOATSTRING) - if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ - buff[len++] = lua_getlocaledecpoint(); - buff[len++] = '0'; /* adds '.0' to result */ - } -#endif - } - setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); -} - - -static void pushstr (lua_State *L, const char *str, size_t l) { - setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - luaD_inctop(L); -} - - -/* -** this function handles only '%d', '%c', '%f', '%p', and '%s' - conventional formats, plus Lua-specific '%I' and '%U' -*/ -const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - int n = 0; - for (;;) { - const char *e = strchr(fmt, '%'); - if (e == NULL) break; - pushstr(L, fmt, e - fmt); - switch (*(e+1)) { - case 's': { /* zero-terminated string */ - const char *s = va_arg(argp, char *); - if (s == NULL) s = "(null)"; - pushstr(L, s, strlen(s)); - break; - } - case 'c': { /* an 'int' as a character */ - char buff = cast(char, va_arg(argp, int)); - if (lisprint(cast_uchar(buff))) - pushstr(L, &buff, 1); - else /* non-printable character; print its code */ - luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); - break; - } - case 'd': { /* an 'int' */ - setivalue(L->top, va_arg(argp, int)); - goto top2str; - } - case 'I': { /* a 'lua_Integer' */ - setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); - goto top2str; - } - case 'f': { /* a 'lua_Number' */ - setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); - top2str: /* convert the top element to a string */ - luaD_inctop(L); - luaO_tostring(L, L->top - 1); - break; - } - case 'p': { /* a pointer */ - char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ - void *p = va_arg(argp, void *); - int l = lua_pointer2str(buff, sizeof(buff), p); - pushstr(L, buff, l); - break; - } - case 'U': { /* an 'int' as a UTF-8 sequence */ - char buff[UTF8BUFFSZ]; - int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); - pushstr(L, buff + UTF8BUFFSZ - l, l); - break; - } - case '%': { - pushstr(L, "%", 1); - break; - } - default: { - luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", - *(e + 1)); - } - } - n += 2; - fmt = e+2; - } - luaD_checkstack(L, 1); - pushstr(L, fmt, strlen(fmt)); - if (n > 0) luaV_concat(L, n + 1); - return svalue(L->top - 1); -} - - -const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - return msg; -} - - -/* number of chars of a literal string without the ending \0 */ -#define LL(x) (sizeof(x)/sizeof(char) - 1) - -#define RETS "..." -#define PRE "[string \"" -#define POS "\"]" - -#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) - -void luaO_chunkid (char *out, const char *source, size_t bufflen) { - size_t l = strlen(source); - if (*source == '=') { /* 'literal' source */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); - else { /* truncate it */ - addstr(out, source + 1, bufflen - 1); - *out = '\0'; - } - } - else if (*source == '@') { /* file name */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); - else { /* add '...' before rest of name */ - addstr(out, RETS, LL(RETS)); - bufflen -= LL(RETS); - memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); - } - } - else { /* string; format as [string "source"] */ - const char *nl = strchr(source, '\n'); /* find first new line (if any) */ - addstr(out, PRE, LL(PRE)); /* add prefix */ - bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ - if (l < bufflen && nl == NULL) { /* small one-line source? */ - addstr(out, source, l); /* keep it */ - } - else { - if (nl != NULL) l = nl - source; /* stop at first newline */ - if (l > bufflen) l = bufflen; - addstr(out, source, l); - addstr(out, RETS, LL(RETS)); - } - memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.h deleted file mode 100644 index 2408861..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lobject.h +++ /dev/null @@ -1,549 +0,0 @@ -/* -** $Id: lobject.h,v 2.117.1.1 2017/04/19 17:39:34 roberto Exp $ -** Type definitions for Lua objects -** See Copyright Notice in lua.h -*/ - - -#ifndef lobject_h -#define lobject_h - - -#include - - -#include "llimits.h" -#include "lua.h" - - -/* -** Extra tags for non-values -*/ -#define LUA_TPROTO LUA_NUMTAGS /* function prototypes */ -#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */ - -/* -** number of all possible tags (including LUA_TNONE but excluding DEADKEY) -*/ -#define LUA_TOTALTAGS (LUA_TPROTO + 2) - - -/* -** tags for Tagged Values have the following use of bits: -** bits 0-3: actual tag (a LUA_T* value) -** bits 4-5: variant bits -** bit 6: whether value is collectable -*/ - - -/* -** LUA_TFUNCTION variants: -** 0 - Lua function -** 1 - light C function -** 2 - regular C function (closure) -*/ - -/* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ - - -/* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ - - -/* Variant tags for numbers */ -#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ -#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ - - -/* Bit mark for collectable types */ -#define BIT_ISCOLLECTABLE (1 << 6) - -/* mark a tag as collectable */ -#define ctb(t) ((t) | BIT_ISCOLLECTABLE) - - -/* -** Common type for all collectable objects -*/ -typedef struct GCObject GCObject; - - -/* -** Common Header for all collectable objects (in macro form, to be -** included in other objects) -*/ -#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked - - -/* -** Common type has only the common header -*/ -struct GCObject { - CommonHeader; -}; - - - - -/* -** Tagged Values. This is the basic representation of values in Lua, -** an actual value plus a tag with its type. -*/ - -/* -** Union of all Lua values -*/ -typedef union Value { - GCObject *gc; /* collectable objects */ - void *p; /* light userdata */ - int b; /* booleans */ - lua_CFunction f; /* light C functions */ - lua_Integer i; /* integer numbers */ - lua_Number n; /* float numbers */ -} Value; - - -#define TValuefields Value value_; int tt_ - - -typedef struct lua_TValue { - TValuefields; -} TValue; - - - -/* macro defining a nil value */ -#define NILCONSTANT {NULL}, LUA_TNIL - - -#define val_(o) ((o)->value_) - - -/* raw type tag of a TValue */ -#define rttype(o) ((o)->tt_) - -/* tag with no variants (bits 0-3) */ -#define novariant(x) ((x) & 0x0F) - -/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ -#define ttype(o) (rttype(o) & 0x3F) - -/* type tag of a TValue with no variants (bits 0-3) */ -#define ttnov(o) (novariant(rttype(o))) - - -/* Macros to test type */ -#define checktag(o,t) (rttype(o) == (t)) -#define checktype(o,t) (ttnov(o) == (t)) -#define ttisnumber(o) checktype((o), LUA_TNUMBER) -#define ttisfloat(o) checktag((o), LUA_TNUMFLT) -#define ttisinteger(o) checktag((o), LUA_TNUMINT) -#define ttisnil(o) checktag((o), LUA_TNIL) -#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) -#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) -#define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) -#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) -#define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) -#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) -#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) -#define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) -#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) -#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) - - -/* Macros to access values */ -#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) -#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) -#define nvalue(o) check_exp(ttisnumber(o), \ - (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) -#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) -#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) -#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) -#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) -#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) -#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) -#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) -#define fvalue(o) check_exp(ttislcf(o), val_(o).f) -#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) -#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) -/* a dead value may get the 'gc' field, but cannot access its contents */ -#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc)) - -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) - - -#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) - - -/* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) - -#define checkliveness(L,obj) \ - lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) - - -/* Macros to set values */ -#define settt_(o,t) ((o)->tt_=(t)) - -#define setfltvalue(obj,x) \ - { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } - -#define chgfltvalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } - -#define setivalue(obj,x) \ - { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } - -#define chgivalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } - -#define setnilvalue(obj) settt_(obj, LUA_TNIL) - -#define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } - -#define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } - -#define setbvalue(obj,x) \ - { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } - -#define setgcovalue(L,obj,x) \ - { TValue *io = (obj); GCObject *i_g=(x); \ - val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } - -#define setsvalue(L,obj,x) \ - { TValue *io = (obj); TString *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ - checkliveness(L,io); } - -#define setuvalue(L,obj,x) \ - { TValue *io = (obj); Udata *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ - checkliveness(L,io); } - -#define setthvalue(L,obj,x) \ - { TValue *io = (obj); lua_State *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ - checkliveness(L,io); } - -#define setclLvalue(L,obj,x) \ - { TValue *io = (obj); LClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ - checkliveness(L,io); } - -#define setclCvalue(L,obj,x) \ - { TValue *io = (obj); CClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ - checkliveness(L,io); } - -#define sethvalue(L,obj,x) \ - { TValue *io = (obj); Table *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ - checkliveness(L,io); } - -#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) - - - -#define setobj(L,obj1,obj2) \ - { TValue *io1=(obj1); *io1 = *(obj2); \ - (void)L; checkliveness(L,io1); } - - -/* -** different types of assignments, according to destination -*/ - -/* from stack to (same) stack */ -#define setobjs2s setobj -/* to stack (not from same stack) */ -#define setobj2s setobj -#define setsvalue2s setsvalue -#define sethvalue2s sethvalue -#define setptvalue2s setptvalue -/* from table to same table */ -#define setobjt2t setobj -/* to new object */ -#define setobj2n setobj -#define setsvalue2n setsvalue - -/* to table (define it as an expression to be used in macros) */ -#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1))) - - - - -/* -** {====================================================== -** types and prototypes -** ======================================================= -*/ - - -typedef TValue *StkId; /* index to stack elements */ - - - - -/* -** Header for string value; string bytes follow the end of this structure -** (aligned according to 'UTString'; see next). -*/ -typedef struct TString { - CommonHeader; - lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - lu_byte shrlen; /* length for short strings */ - unsigned int hash; - union { - size_t lnglen; /* length for long strings */ - struct TString *hnext; /* linked list for hash table */ - } u; -} TString; - - -/* -** Ensures that address after this type is always fully aligned. -*/ -typedef union UTString { - L_Umaxalign dummy; /* ensures maximum alignment for strings */ - TString tsv; -} UTString; - - -/* -** Get the actual string (array of bytes) from a 'TString'. -** (Access to 'extra' ensures that value is really a 'TString'.) -*/ -#define getstr(ts) \ - check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString)) - - -/* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(tsvalue(o)) - -/* get string length from 'TString *s' */ -#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen) - -/* get string length from 'TValue *o' */ -#define vslen(o) tsslen(tsvalue(o)) - - -/* -** Header for userdata; memory area follows the end of this structure -** (aligned according to 'UUdata'; see next). -*/ -typedef struct Udata { - CommonHeader; - lu_byte ttuv_; /* user value's tag */ - struct Table *metatable; - size_t len; /* number of bytes */ - union Value user_; /* user value */ -} Udata; - - -/* -** Ensures that address after this type is always fully aligned. -*/ -typedef union UUdata { - L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */ - Udata uv; -} UUdata; - - -/* -** Get the address of memory block inside 'Udata'. -** (Access to 'ttuv_' ensures that value is really a 'Udata'.) -*/ -#define getudatamem(u) \ - check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata))) - -#define setuservalue(L,u,o) \ - { const TValue *io=(o); Udata *iu = (u); \ - iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ - checkliveness(L,io); } - - -#define getuservalue(L,u,o) \ - { TValue *io=(o); const Udata *iu = (u); \ - io->value_ = iu->user_; settt_(io, iu->ttuv_); \ - checkliveness(L,io); } - - -/* -** Description of an upvalue for function prototypes -*/ -typedef struct Upvaldesc { - TString *name; /* upvalue name (for debug information) */ - lu_byte instack; /* whether it is in stack (register) */ - lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ -} Upvaldesc; - - -/* -** Description of a local variable for function prototypes -** (used for debug information) -*/ -typedef struct LocVar { - TString *varname; - int startpc; /* first point where variable is active */ - int endpc; /* first point where variable is dead */ -} LocVar; - - -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; - lu_byte numparams; /* number of fixed parameters */ - lu_byte is_vararg; - lu_byte maxstacksize; /* number of registers needed by this function */ - int sizeupvalues; /* size of 'upvalues' */ - int sizek; /* size of 'k' */ - int sizecode; - int sizelineinfo; - int sizep; /* size of 'p' */ - int sizelocvars; - int linedefined; /* debug information */ - int lastlinedefined; /* debug information */ - TValue *k; /* constants used by the function */ - Instruction *code; /* opcodes */ - struct Proto **p; /* functions defined inside the function */ - int *lineinfo; /* map from opcodes to source lines (debug information) */ - LocVar *locvars; /* information about local variables (debug information) */ - Upvaldesc *upvalues; /* upvalue information */ - struct LClosure *cache; /* last-created closure with this prototype */ - TString *source; /* used for debug information */ - GCObject *gclist; -} Proto; - - - -/* -** Lua Upvalues -*/ -typedef struct UpVal UpVal; - - -/* -** Closures -*/ - -#define ClosureHeader \ - CommonHeader; lu_byte nupvalues; GCObject *gclist - -typedef struct CClosure { - ClosureHeader; - lua_CFunction f; - TValue upvalue[1]; /* list of upvalues */ -} CClosure; - - -typedef struct LClosure { - ClosureHeader; - struct Proto *p; - UpVal *upvals[1]; /* list of upvalues */ -} LClosure; - - -typedef union Closure { - CClosure c; - LClosure l; -} Closure; - - -#define isLfunction(o) ttisLclosure(o) - -#define getproto(o) (clLvalue(o)->p) - - -/* -** Tables -*/ - -typedef union TKey { - struct { - TValuefields; - int next; /* for chaining (offset for next node) */ - } nk; - TValue tvk; -} TKey; - - -/* copy a value into a key without messing up field 'next' */ -#define setnodekey(L,key,obj) \ - { TKey *k_=(key); const TValue *io_=(obj); \ - k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \ - (void)L; checkliveness(L,io_); } - - -typedef struct Node { - TValue i_val; - TKey i_key; -} Node; - - -typedef struct Table { - CommonHeader; - lu_byte flags; /* 1<

lsizenode)) - - -/* -** (address of) a fixed nil value -*/ -#define luaO_nilobject (&luaO_nilobject_) - - -LUAI_DDEC const TValue luaO_nilobject_; - -/* size of buffer for 'luaO_utf8esc' function */ -#define UTF8BUFFSZ 8 - -LUAI_FUNC int luaO_int2fb (unsigned int x); -LUAI_FUNC int luaO_fb2int (int x); -LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); -LUAI_FUNC int luaO_ceillog2 (unsigned int x); -LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, - const TValue *p2, TValue *res); -LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); -LUAI_FUNC int luaO_hexavalue (int c); -LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj); -LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, - va_list argp); -LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.c deleted file mode 100644 index 5ca3eb2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.c +++ /dev/null @@ -1,124 +0,0 @@ -/* -** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#define lopcodes_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lopcodes.h" - - -/* ORDER OP */ - -LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { - "MOVE", - "LOADK", - "LOADKX", - "LOADBOOL", - "LOADNIL", - "GETUPVAL", - "GETTABUP", - "GETTABLE", - "SETTABUP", - "SETUPVAL", - "SETTABLE", - "NEWTABLE", - "SELF", - "ADD", - "SUB", - "MUL", - "MOD", - "POW", - "DIV", - "IDIV", - "BAND", - "BOR", - "BXOR", - "SHL", - "SHR", - "UNM", - "BNOT", - "NOT", - "LEN", - "CONCAT", - "JMP", - "EQ", - "LT", - "LE", - "TEST", - "TESTSET", - "CALL", - "TAILCALL", - "RETURN", - "FORLOOP", - "FORPREP", - "TFORCALL", - "TFORLOOP", - "SETLIST", - "CLOSURE", - "VARARG", - "EXTRAARG", - NULL -}; - - -#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) - -LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* T A B C mode opcode */ - opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ - ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ - ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ - ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ - ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ - ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ - ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ - ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ - ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ -}; - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.h deleted file mode 100644 index 6feaa1c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lopcodes.h +++ /dev/null @@ -1,297 +0,0 @@ -/* -** $Id: lopcodes.h,v 1.149.1.1 2017/04/19 17:20:42 roberto Exp $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lopcodes_h -#define lopcodes_h - -#include "llimits.h" - - -/*=========================================================================== - We assume that instructions are unsigned numbers. - All instructions have an opcode in the first 6 bits. - Instructions can have the following fields: - 'A' : 8 bits - 'B' : 9 bits - 'C' : 9 bits - 'Ax' : 26 bits ('A', 'B', and 'C' together) - 'Bx' : 18 bits ('B' and 'C' together) - 'sBx' : signed Bx - - A signed argument is represented in excess K; that is, the number - value is the unsigned value minus K. K is exactly the maximum value - for that argument (so that -max is represented by 0, and +max is - represented by 2*max), which is half the maximum for the corresponding - unsigned argument. -===========================================================================*/ - - -enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ - - -/* -** size and position of opcode arguments. -*/ -#define SIZE_C 9 -#define SIZE_B 9 -#define SIZE_Bx (SIZE_C + SIZE_B) -#define SIZE_A 8 -#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) - -#define SIZE_OP 6 - -#define POS_OP 0 -#define POS_A (POS_OP + SIZE_OP) -#define POS_C (POS_A + SIZE_A) -#define POS_B (POS_C + SIZE_C) -#define POS_Bx POS_C -#define POS_Ax POS_A - - -/* -** limits for opcode arguments. -** we use (signed) int to manipulate most arguments, -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) -*/ -#if SIZE_Bx < LUAI_BITSINT-1 -#define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ -#else -#define MAXARG_Bx MAX_INT -#define MAXARG_sBx MAX_INT -#endif - -#if SIZE_Ax < LUAI_BITSINT-1 -#define MAXARG_Ax ((1<>POS_OP) & MASK1(SIZE_OP,0))) -#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>pos) & MASK1(size,0))) -#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ - ((cast(Instruction, v)<> RK(C) */ -OP_UNM,/* A B R(A) := -R(B) */ -OP_BNOT,/* A B R(A) := ~R(B) */ -OP_NOT,/* A B R(A) := not R(B) */ -OP_LEN,/* A B R(A) := length of R(B) */ - -OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ - -OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */ -OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ -OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ -OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ - -OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ -OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ - -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ - -OP_FORLOOP,/* A sBx R(A)+=R(A+2); - if R(A) > 4) & 3)) -#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) -#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) -#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) - - -LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ - - -/* number of list items to accumulate before a SETLIST instruction */ -#define LFIELDS_PER_FLUSH 50 - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/loslib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/loslib.c deleted file mode 100644 index de590c6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/loslib.c +++ /dev/null @@ -1,409 +0,0 @@ -/* -** $Id: loslib.c,v 1.65.1.1 2017/04/19 17:29:57 roberto Exp $ -** Standard Operating System library -** See Copyright Notice in lua.h -*/ - -#define loslib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** {================================================================== -** List of valid conversion specifiers for the 'strftime' function; -** options are grouped by length; group of length 2 start with '||'. -** =================================================================== -*/ -#if !defined(LUA_STRFTIMEOPTIONS) /* { */ - -/* options for ANSI C 89 (only 1-char options) */ -#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" - -/* options for ISO C 99 and POSIX */ -#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ - "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ - -/* options for Windows */ -#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ - -#if defined(LUA_USE_WINDOWS) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN -#elif defined(LUA_USE_C89) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 -#else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 -#endif - -#endif /* } */ -/* }================================================================== */ - - -/* -** {================================================================== -** Configuration for time-related stuff -** =================================================================== -*/ - -#if !defined(l_time_t) /* { */ -/* -** type to represent time_t in Lua -*/ -#define l_timet lua_Integer -#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) - -static time_t l_checktime (lua_State *L, int arg) { - lua_Integer t = luaL_checkinteger(L, arg); - luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); - return (time_t)t; -} - -#endif /* } */ - - -#if !defined(l_gmtime) /* { */ -/* -** By default, Lua uses gmtime/localtime, except when POSIX is available, -** where it uses gmtime_r/localtime_r -*/ - -#if defined(LUA_USE_POSIX) /* { */ - -#define l_gmtime(t,r) gmtime_r(t,r) -#define l_localtime(t,r) localtime_r(t,r) - -#else /* }{ */ - -/* ISO C definitions */ -#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) -#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) - -#endif /* } */ - -#endif /* } */ - -/* }================================================================== */ - - -/* -** {================================================================== -** Configuration for 'tmpnam': -** By default, Lua uses tmpnam except when POSIX is available, where -** it uses mkstemp. -** =================================================================== -*/ -#if !defined(lua_tmpnam) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include - -#define LUA_TMPNAMBUFSIZE 32 - -#if !defined(LUA_TMPNAMTEMPLATE) -#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" -#endif - -#define lua_tmpnam(b,e) { \ - strcpy(b, LUA_TMPNAMTEMPLATE); \ - e = mkstemp(b); \ - if (e != -1) close(e); \ - e = (e == -1); } - -#else /* }{ */ - -/* ISO C definitions */ -#define LUA_TMPNAMBUFSIZE L_tmpnam -#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } - -#endif /* } */ - -#endif /* } */ -/* }================================================================== */ - - - - -static int os_execute (lua_State *L) { - const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); - if (cmd != NULL) - return luaL_execresult(L, stat); - else { - lua_pushboolean(L, stat); /* true if there is a shell */ - return 1; - } -} - - -static int os_remove (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - return luaL_fileresult(L, remove(filename) == 0, filename); -} - - -static int os_rename (lua_State *L) { - const char *fromname = luaL_checkstring(L, 1); - const char *toname = luaL_checkstring(L, 2); - return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); -} - - -static int os_tmpname (lua_State *L) { - char buff[LUA_TMPNAMBUFSIZE]; - int err; - lua_tmpnam(buff, err); - if (err) - return luaL_error(L, "unable to generate a unique filename"); - lua_pushstring(L, buff); - return 1; -} - - -static int os_getenv (lua_State *L) { - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ - return 1; -} - - -static int os_clock (lua_State *L) { - lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); - return 1; -} - - -/* -** {====================================================== -** Time/Date operations -** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, -** wday=%w+1, yday=%j, isdst=? } -** ======================================================= -*/ - -static void setfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); - lua_setfield(L, -2, key); -} - -static void setboolfield (lua_State *L, const char *key, int value) { - if (value < 0) /* undefined? */ - return; /* does not set field */ - lua_pushboolean(L, value); - lua_setfield(L, -2, key); -} - - -/* -** Set all fields from structure 'tm' in the table on top of the stack -*/ -static void setallfields (lua_State *L, struct tm *stm) { - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon + 1); - setfield(L, "year", stm->tm_year + 1900); - setfield(L, "wday", stm->tm_wday + 1); - setfield(L, "yday", stm->tm_yday + 1); - setboolfield(L, "isdst", stm->tm_isdst); -} - - -static int getboolfield (lua_State *L, const char *key) { - int res; - res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); - lua_pop(L, 1); - return res; -} - - -/* maximum value for date fields (to avoid arithmetic overflows with 'int') */ -#if !defined(L_MAXDATEFIELD) -#define L_MAXDATEFIELD (INT_MAX / 2) -#endif - -static int getfield (lua_State *L, const char *key, int d, int delta) { - int isnum; - int t = lua_getfield(L, -1, key); /* get field and its type */ - lua_Integer res = lua_tointegerx(L, -1, &isnum); - if (!isnum) { /* field is not an integer? */ - if (t != LUA_TNIL) /* some other value? */ - return luaL_error(L, "field '%s' is not an integer", key); - else if (d < 0) /* absent field; no default? */ - return luaL_error(L, "field '%s' missing in date table", key); - res = d; - } - else { - if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) - return luaL_error(L, "field '%s' is out-of-bound", key); - res -= delta; - } - lua_pop(L, 1); - return (int)res; -} - - -static const char *checkoption (lua_State *L, const char *conv, - ptrdiff_t convlen, char *buff) { - const char *option = LUA_STRFTIMEOPTIONS; - int oplen = 1; /* length of options being checked */ - for (; *option != '\0' && oplen <= convlen; option += oplen) { - if (*option == '|') /* next block? */ - oplen++; /* will check options with next length (+1) */ - else if (memcmp(conv, option, oplen) == 0) { /* match? */ - memcpy(buff, conv, oplen); /* copy valid option to buffer */ - buff[oplen] = '\0'; - return conv + oplen; /* return next item */ - } - } - luaL_argerror(L, 1, - lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); - return conv; /* to avoid warnings */ -} - - -/* maximum size for an individual 'strftime' item */ -#define SIZETIMEFMT 250 - - -static int os_date (lua_State *L) { - size_t slen; - const char *s = luaL_optlstring(L, 1, "%c", &slen); - time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); - const char *se = s + slen; /* 's' end */ - struct tm tmr, *stm; - if (*s == '!') { /* UTC? */ - stm = l_gmtime(&t, &tmr); - s++; /* skip '!' */ - } - else - stm = l_localtime(&t, &tmr); - if (stm == NULL) /* invalid date? */ - return luaL_error(L, - "time result cannot be represented in this installation"); - if (strcmp(s, "*t") == 0) { - lua_createtable(L, 0, 9); /* 9 = number of fields */ - setallfields(L, stm); - } - else { - char cc[4]; /* buffer for individual conversion specifiers */ - luaL_Buffer b; - cc[0] = '%'; - luaL_buffinit(L, &b); - while (s < se) { - if (*s != '%') /* not a conversion specifier? */ - luaL_addchar(&b, *s++); - else { - size_t reslen; - char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); - s++; /* skip '%' */ - s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ - reslen = strftime(buff, SIZETIMEFMT, cc, stm); - luaL_addsize(&b, reslen); - } - } - luaL_pushresult(&b); - } - return 1; -} - - -static int os_time (lua_State *L) { - time_t t; - if (lua_isnoneornil(L, 1)) /* called without args? */ - t = time(NULL); /* get current time */ - else { - struct tm ts; - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0, 0); - ts.tm_min = getfield(L, "min", 0, 0); - ts.tm_hour = getfield(L, "hour", 12, 0); - ts.tm_mday = getfield(L, "day", -1, 0); - ts.tm_mon = getfield(L, "month", -1, 1); - ts.tm_year = getfield(L, "year", -1, 1900); - ts.tm_isdst = getboolfield(L, "isdst"); - t = mktime(&ts); - setallfields(L, &ts); /* update fields with normalized values */ - } - if (t != (time_t)(l_timet)t || t == (time_t)(-1)) - return luaL_error(L, - "time result cannot be represented in this installation"); - l_pushtime(L, t); - return 1; -} - - -static int os_difftime (lua_State *L) { - time_t t1 = l_checktime(L, 1); - time_t t2 = l_checktime(L, 2); - lua_pushnumber(L, (lua_Number)difftime(t1, t2)); - return 1; -} - -/* }====================================================== */ - - -static int os_setlocale (lua_State *L) { - static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, - LC_NUMERIC, LC_TIME}; - static const char *const catnames[] = {"all", "collate", "ctype", "monetary", - "numeric", "time", NULL}; - const char *l = luaL_optstring(L, 1, NULL); - int op = luaL_checkoption(L, 2, "all", catnames); - lua_pushstring(L, setlocale(cat[op], l)); - return 1; -} - - -static int os_exit (lua_State *L) { - int status; - if (lua_isboolean(L, 1)) - status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); - else - status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); - if (lua_toboolean(L, 2)) - lua_close(L); - if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ - return 0; -} - - -static const luaL_Reg syslib[] = { - {"clock", os_clock}, - {"date", os_date}, - {"difftime", os_difftime}, - {"execute", os_execute}, - {"exit", os_exit}, - {"getenv", os_getenv}, - {"remove", os_remove}, - {"rename", os_rename}, - {"setlocale", os_setlocale}, - {"time", os_time}, - {"tmpname", os_tmpname}, - {NULL, NULL} -}; - -/* }====================================================== */ - - - -LUAMOD_API int luaopen_os (lua_State *L) { - luaL_newlib(L, syslib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.c deleted file mode 100644 index 2f41e00..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.c +++ /dev/null @@ -1,1653 +0,0 @@ -/* -** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - -#define lparser_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" - - - -/* maximum number of local variables per function (must be smaller - than 250, due to the bytecode format) */ -#define MAXVARS 200 - - -#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) - - -/* because all strings are unified by the scanner, the parser - can use pointer equality for string equality */ -#define eqstr(a,b) ((a) == (b)) - - -/* -** nodes for block list (list of active blocks) -*/ -typedef struct BlockCnt { - struct BlockCnt *previous; /* chain */ - int firstlabel; /* index of first label in this block */ - int firstgoto; /* index of first pending goto in this block */ - lu_byte nactvar; /* # active locals outside the block */ - lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if 'block' is a loop */ -} BlockCnt; - - - -/* -** prototypes for recursive non-terminal functions -*/ -static void statement (LexState *ls); -static void expr (LexState *ls, expdesc *v); - - -/* semantic error */ -static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove "near " from final message */ - luaX_syntaxerror(ls, msg); -} - - -static l_noret error_expected (LexState *ls, int token) { - luaX_syntaxerror(ls, - luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); -} - - -static l_noret errorlimit (FuncState *fs, int limit, const char *what) { - lua_State *L = fs->ls->L; - const char *msg; - int line = fs->f->linedefined; - const char *where = (line == 0) - ? "main function" - : luaO_pushfstring(L, "function at line %d", line); - msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", - what, limit, where); - luaX_syntaxerror(fs->ls, msg); -} - - -static void checklimit (FuncState *fs, int v, int l, const char *what) { - if (v > l) errorlimit(fs, l, what); -} - - -static int testnext (LexState *ls, int c) { - if (ls->t.token == c) { - luaX_next(ls); - return 1; - } - else return 0; -} - - -static void check (LexState *ls, int c) { - if (ls->t.token != c) - error_expected(ls, c); -} - - -static void checknext (LexState *ls, int c) { - check(ls, c); - luaX_next(ls); -} - - -#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } - - - -static void check_match (LexState *ls, int what, int who, int where) { - if (!testnext(ls, what)) { - if (where == ls->linenumber) - error_expected(ls, what); - else { - luaX_syntaxerror(ls, luaO_pushfstring(ls->L, - "%s expected (to close %s at line %d)", - luaX_token2str(ls, what), luaX_token2str(ls, who), where)); - } - } -} - - -static TString *str_checkname (LexState *ls) { - TString *ts; - check(ls, TK_NAME); - ts = ls->t.seminfo.ts; - luaX_next(ls); - return ts; -} - - -static void init_exp (expdesc *e, expkind k, int i) { - e->f = e->t = NO_JUMP; - e->k = k; - e->u.info = i; -} - - -static void codestring (LexState *ls, expdesc *e, TString *s) { - init_exp(e, VK, luaK_stringK(ls->fs, s)); -} - - -static void checkname (LexState *ls, expdesc *e) { - codestring(ls, e, str_checkname(ls)); -} - - -static int registerlocalvar (LexState *ls, TString *varname) { - FuncState *fs = ls->fs; - Proto *f = fs->f; - int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, - LocVar, SHRT_MAX, "local variables"); - while (oldsize < f->sizelocvars) - f->locvars[oldsize++].varname = NULL; - f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, f, varname); - return fs->nlocvars++; -} - - -static void new_localvar (LexState *ls, TString *name) { - FuncState *fs = ls->fs; - Dyndata *dyd = ls->dyd; - int reg = registerlocalvar(ls, name); - checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); - luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, MAX_INT, "local variables"); - dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); -} - - -static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { - new_localvar(ls, luaX_newstring(ls, name, sz)); -} - -#define new_localvarliteral(ls,v) \ - new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1) - - -static LocVar *getlocvar (FuncState *fs, int i) { - int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; - lua_assert(idx < fs->nlocvars); - return &fs->f->locvars[idx]; -} - - -static void adjustlocalvars (LexState *ls, int nvars) { - FuncState *fs = ls->fs; - fs->nactvar = cast_byte(fs->nactvar + nvars); - for (; nvars; nvars--) { - getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; - } -} - - -static void removevars (FuncState *fs, int tolevel) { - fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); - while (fs->nactvar > tolevel) - getlocvar(fs, --fs->nactvar)->endpc = fs->pc; -} - - -static int searchupvalue (FuncState *fs, TString *name) { - int i; - Upvaldesc *up = fs->f->upvalues; - for (i = 0; i < fs->nups; i++) { - if (eqstr(up[i].name, name)) return i; - } - return -1; /* not found */ -} - - -static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Proto *f = fs->f; - int oldsize = f->sizeupvalues; - checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); - luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, - Upvaldesc, MAXUPVAL, "upvalues"); - while (oldsize < f->sizeupvalues) - f->upvalues[oldsize++].name = NULL; - f->upvalues[fs->nups].instack = (v->k == VLOCAL); - f->upvalues[fs->nups].idx = cast_byte(v->u.info); - f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, f, name); - return fs->nups++; -} - - -static int searchvar (FuncState *fs, TString *n) { - int i; - for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (eqstr(n, getlocvar(fs, i)->varname)) - return i; - } - return -1; /* not found */ -} - - -/* - Mark block where variable at given level was defined - (to emit close instructions later). -*/ -static void markupval (FuncState *fs, int level) { - BlockCnt *bl = fs->bl; - while (bl->nactvar > level) - bl = bl->previous; - bl->upval = 1; -} - - -/* - Find variable with given name 'n'. If it is an upvalue, add this - upvalue into all intermediate functions. -*/ -static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - if (fs == NULL) /* no more levels? */ - init_exp(var, VVOID, 0); /* default is global */ - else { - int v = searchvar(fs, n); /* look up locals at current level */ - if (v >= 0) { /* found? */ - init_exp(var, VLOCAL, v); /* variable is local */ - if (!base) - markupval(fs, v); /* local will be used as an upval */ - } - else { /* not found as local at current level; try upvalues */ - int idx = searchupvalue(fs, n); /* try existing upvalues */ - if (idx < 0) { /* not found? */ - singlevaraux(fs->prev, n, var, 0); /* try upper levels */ - if (var->k == VVOID) /* not found? */ - return; /* it is a global */ - /* else was LOCAL or UPVAL */ - idx = newupvalue(fs, n, var); /* will be a new upvalue */ - } - init_exp(var, VUPVAL, idx); /* new or old upvalue */ - } - } -} - - -static void singlevar (LexState *ls, expdesc *var) { - TString *varname = str_checkname(ls); - FuncState *fs = ls->fs; - singlevaraux(fs, varname, var, 1); - if (var->k == VVOID) { /* global name? */ - expdesc key; - singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k != VVOID); /* this one must exist */ - codestring(ls, &key, varname); /* key is variable name */ - luaK_indexed(fs, var, &key); /* env[varname] */ - } -} - - -static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { - FuncState *fs = ls->fs; - int extra = nvars - nexps; - if (hasmultret(e->k)) { - extra++; /* includes call itself */ - if (extra < 0) extra = 0; - luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ - if (extra > 1) luaK_reserveregs(fs, extra-1); - } - else { - if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ - if (extra > 0) { - int reg = fs->freereg; - luaK_reserveregs(fs, extra); - luaK_nil(fs, reg, extra); - } - } - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ -} - - -static void enterlevel (LexState *ls) { - lua_State *L = ls->L; - ++L->nCcalls; - checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels"); -} - - -#define leavelevel(ls) ((ls)->L->nCcalls--) - - -static void closegoto (LexState *ls, int g, Labeldesc *label) { - int i; - FuncState *fs = ls->fs; - Labellist *gl = &ls->dyd->gt; - Labeldesc *gt = &gl->arr[g]; - lua_assert(eqstr(gt->name, label->name)); - if (gt->nactvar < label->nactvar) { - TString *vname = getlocvar(fs, gt->nactvar)->varname; - const char *msg = luaO_pushfstring(ls->L, - " at line %d jumps into the scope of local '%s'", - getstr(gt->name), gt->line, getstr(vname)); - semerror(ls, msg); - } - luaK_patchlist(fs, gt->pc, label->pc); - /* remove goto from pending list */ - for (i = g; i < gl->n - 1; i++) - gl->arr[i] = gl->arr[i + 1]; - gl->n--; -} - - -/* -** try to close a goto with existing labels; this solves backward jumps -*/ -static int findlabel (LexState *ls, int g) { - int i; - BlockCnt *bl = ls->fs->bl; - Dyndata *dyd = ls->dyd; - Labeldesc *gt = &dyd->gt.arr[g]; - /* check labels in current block for a match */ - for (i = bl->firstlabel; i < dyd->label.n; i++) { - Labeldesc *lb = &dyd->label.arr[i]; - if (eqstr(lb->name, gt->name)) { /* correct label? */ - if (gt->nactvar > lb->nactvar && - (bl->upval || dyd->label.n > bl->firstlabel)) - luaK_patchclose(ls->fs, gt->pc, lb->nactvar); - closegoto(ls, g, lb); /* close it */ - return 1; - } - } - return 0; /* label not found; cannot close goto */ -} - - -static int newlabelentry (LexState *ls, Labellist *l, TString *name, - int line, int pc) { - int n = l->n; - luaM_growvector(ls->L, l->arr, n, l->size, - Labeldesc, SHRT_MAX, "labels/gotos"); - l->arr[n].name = name; - l->arr[n].line = line; - l->arr[n].nactvar = ls->fs->nactvar; - l->arr[n].pc = pc; - l->n = n + 1; - return n; -} - - -/* -** check whether new label 'lb' matches any pending gotos in current -** block; solves forward jumps -*/ -static void findgotos (LexState *ls, Labeldesc *lb) { - Labellist *gl = &ls->dyd->gt; - int i = ls->fs->bl->firstgoto; - while (i < gl->n) { - if (eqstr(gl->arr[i].name, lb->name)) - closegoto(ls, i, lb); - else - i++; - } -} - - -/* -** export pending gotos to outer level, to check them against -** outer labels; if the block being exited has upvalues, and -** the goto exits the scope of any variable (which can be the -** upvalue), close those variables being exited. -*/ -static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i = bl->firstgoto; - Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block and try to close it - with visible labels */ - while (i < gl->n) { - Labeldesc *gt = &gl->arr[i]; - if (gt->nactvar > bl->nactvar) { - if (bl->upval) - luaK_patchclose(fs, gt->pc, bl->nactvar); - gt->nactvar = bl->nactvar; - } - if (!findlabel(fs->ls, i)) - i++; /* move to next one */ - } -} - - -static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { - bl->isloop = isloop; - bl->nactvar = fs->nactvar; - bl->firstlabel = fs->ls->dyd->label.n; - bl->firstgoto = fs->ls->dyd->gt.n; - bl->upval = 0; - bl->previous = fs->bl; - fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); -} - - -/* -** create a label named 'break' to resolve break statements -*/ -static void breaklabel (LexState *ls) { - TString *n = luaS_new(ls->L, "break"); - int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc); - findgotos(ls, &ls->dyd->label.arr[l]); -} - -/* -** generates an error for an undefined 'goto'; choose appropriate -** message when label name is a reserved word (which can only be 'break') -*/ -static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg = isreserved(gt->name) - ? "<%s> at line %d not inside a loop" - : "no visible label '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - semerror(ls, msg); -} - - -static void leaveblock (FuncState *fs) { - BlockCnt *bl = fs->bl; - LexState *ls = fs->ls; - if (bl->previous && bl->upval) { - /* create a 'jump to here' to close upvalues */ - int j = luaK_jump(fs); - luaK_patchclose(fs, j, bl->nactvar); - luaK_patchtohere(fs, j); - } - if (bl->isloop) - breaklabel(ls); /* close pending breaks */ - fs->bl = bl->previous; - removevars(fs, bl->nactvar); - lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = fs->nactvar; /* free registers */ - ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - if (bl->previous) /* inner block? */ - movegotosout(fs, bl); /* update pending gotos to outer block */ - else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ - undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ -} - - -/* -** adds a new prototype into list of prototypes -*/ -static Proto *addprototype (LexState *ls) { - Proto *clp; - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sizep) { - int oldsize = f->sizep; - luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sizep) - f->p[oldsize++] = NULL; - } - f->p[fs->np++] = clp = luaF_newproto(L); - luaC_objbarrier(L, f, clp); - return clp; -} - - -/* -** codes instruction to create new closure in parent function. -** The OP_CLOSURE instruction must use the last available register, -** so that, if it invokes the GC, the GC knows which registers -** are in use at that time. -*/ -static void codeclosure (LexState *ls, expdesc *v) { - FuncState *fs = ls->fs->prev; - init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); - luaK_exp2nextreg(fs, v); /* fix it at the last register */ -} - - -static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - Proto *f; - fs->prev = ls->fs; /* linked list of funcstates */ - fs->ls = ls; - ls->fs = fs; - fs->pc = 0; - fs->lasttarget = 0; - fs->jpc = NO_JUMP; - fs->freereg = 0; - fs->nk = 0; - fs->np = 0; - fs->nups = 0; - fs->nlocvars = 0; - fs->nactvar = 0; - fs->firstlocal = ls->dyd->actvar.n; - fs->bl = NULL; - f = fs->f; - f->source = ls->source; - luaC_objbarrier(ls->L, f, f->source); - f->maxstacksize = 2; /* registers 0/1 are always valid */ - enterblock(fs, bl, 0); -} - - -static void close_func (LexState *ls) { - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; - luaK_ret(fs, 0, 0); /* final return */ - leaveblock(fs); - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); - f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); - f->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); - f->sizek = fs->nk; - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); - f->sizep = fs->np; - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); - f->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - f->sizeupvalues = fs->nups; - lua_assert(fs->bl == NULL); - ls->fs = fs->prev; - luaC_checkGC(L); -} - - - -/*============================================================*/ -/* GRAMMAR RULES */ -/*============================================================*/ - - -/* -** check whether current token is in the follow set of a block. -** 'until' closes syntactical blocks, but do not close scope, -** so it is handled in separate. -*/ -static int block_follow (LexState *ls, int withuntil) { - switch (ls->t.token) { - case TK_ELSE: case TK_ELSEIF: - case TK_END: case TK_EOS: - return 1; - case TK_UNTIL: return withuntil; - default: return 0; - } -} - - -static void statlist (LexState *ls) { - /* statlist -> { stat [';'] } */ - while (!block_follow(ls, 1)) { - if (ls->t.token == TK_RETURN) { - statement(ls); - return; /* 'return' must be last statement */ - } - statement(ls); - } -} - - -static void fieldsel (LexState *ls, expdesc *v) { - /* fieldsel -> ['.' | ':'] NAME */ - FuncState *fs = ls->fs; - expdesc key; - luaK_exp2anyregup(fs, v); - luaX_next(ls); /* skip the dot or colon */ - checkname(ls, &key); - luaK_indexed(fs, v, &key); -} - - -static void yindex (LexState *ls, expdesc *v) { - /* index -> '[' expr ']' */ - luaX_next(ls); /* skip the '[' */ - expr(ls, v); - luaK_exp2val(ls->fs, v); - checknext(ls, ']'); -} - - -/* -** {====================================================================== -** Rules for Constructors -** ======================================================================= -*/ - - -struct ConsControl { - expdesc v; /* last list item read */ - expdesc *t; /* table descriptor */ - int nh; /* total number of 'record' elements */ - int na; /* total number of array elements */ - int tostore; /* number of array elements pending to be stored */ -}; - - -static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | '['exp1']') = exp1 */ - FuncState *fs = ls->fs; - int reg = ls->fs->freereg; - expdesc key, val; - int rkkey; - if (ls->t.token == TK_NAME) { - checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); - checkname(ls, &key); - } - else /* ls->t.token == '[' */ - yindex(ls, &key); - cc->nh++; - checknext(ls, '='); - rkkey = luaK_exp2RK(fs, &key); - expr(ls, &val); - luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val)); - fs->freereg = reg; /* free registers */ -} - - -static void closelistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->v.k == VVOID) return; /* there is no list item */ - luaK_exp2nextreg(fs, &cc->v); - cc->v.k = VVOID; - if (cc->tostore == LFIELDS_PER_FLUSH) { - luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ - cc->tostore = 0; /* no more items pending */ - } -} - - -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { - if (cc->tostore == 0) return; - if (hasmultret(cc->v.k)) { - luaK_setmultret(fs, &cc->v); - luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); - cc->na--; /* do not count last expression (unknown number of elements) */ - } - else { - if (cc->v.k != VVOID) - luaK_exp2nextreg(fs, &cc->v); - luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); - } -} - - -static void listfield (LexState *ls, struct ConsControl *cc) { - /* listfield -> exp */ - expr(ls, &cc->v); - checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; - cc->tostore++; -} - - -static void field (LexState *ls, struct ConsControl *cc) { - /* field -> listfield | recfield */ - switch(ls->t.token) { - case TK_NAME: { /* may be 'listfield' or 'recfield' */ - if (luaX_lookahead(ls) != '=') /* expression? */ - listfield(ls, cc); - else - recfield(ls, cc); - break; - } - case '[': { - recfield(ls, cc); - break; - } - default: { - listfield(ls, cc); - break; - } - } -} - - -static void constructor (LexState *ls, expdesc *t) { - /* constructor -> '{' [ field { sep field } [sep] ] '}' - sep -> ',' | ';' */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - struct ConsControl cc; - cc.na = cc.nh = cc.tostore = 0; - cc.t = t; - init_exp(t, VRELOCABLE, pc); - init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ - checknext(ls, '{'); - do { - lua_assert(cc.v.k == VVOID || cc.tostore > 0); - if (ls->t.token == '}') break; - closelistfield(fs, &cc); - field(ls, &cc); - } while (testnext(ls, ',') || testnext(ls, ';')); - check_match(ls, '}', '{', line); - lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ -} - -/* }====================================================================== */ - - - -static void parlist (LexState *ls) { - /* parlist -> [ param { ',' param } ] */ - FuncState *fs = ls->fs; - Proto *f = fs->f; - int nparams = 0; - f->is_vararg = 0; - if (ls->t.token != ')') { /* is 'parlist' not empty? */ - do { - switch (ls->t.token) { - case TK_NAME: { /* param -> NAME */ - new_localvar(ls, str_checkname(ls)); - nparams++; - break; - } - case TK_DOTS: { /* param -> '...' */ - luaX_next(ls); - f->is_vararg = 1; /* declared vararg */ - break; - } - default: luaX_syntaxerror(ls, " or '...' expected"); - } - } while (!f->is_vararg && testnext(ls, ',')); - } - adjustlocalvars(ls, nparams); - f->numparams = cast_byte(fs->nactvar); - luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ -} - - -static void body (LexState *ls, expdesc *e, int ismethod, int line) { - /* body -> '(' parlist ')' block END */ - FuncState new_fs; - BlockCnt bl; - new_fs.f = addprototype(ls); - new_fs.f->linedefined = line; - open_func(ls, &new_fs, &bl); - checknext(ls, '('); - if (ismethod) { - new_localvarliteral(ls, "self"); /* create 'self' parameter */ - adjustlocalvars(ls, 1); - } - parlist(ls); - checknext(ls, ')'); - statlist(ls); - new_fs.f->lastlinedefined = ls->linenumber; - check_match(ls, TK_END, TK_FUNCTION, line); - codeclosure(ls, e); - close_func(ls); -} - - -static int explist (LexState *ls, expdesc *v) { - /* explist -> expr { ',' expr } */ - int n = 1; /* at least one expression */ - expr(ls, v); - while (testnext(ls, ',')) { - luaK_exp2nextreg(ls->fs, v); - expr(ls, v); - n++; - } - return n; -} - - -static void funcargs (LexState *ls, expdesc *f, int line) { - FuncState *fs = ls->fs; - expdesc args; - int base, nparams; - switch (ls->t.token) { - case '(': { /* funcargs -> '(' [ explist ] ')' */ - luaX_next(ls); - if (ls->t.token == ')') /* arg list is empty? */ - args.k = VVOID; - else { - explist(ls, &args); - luaK_setmultret(fs, &args); - } - check_match(ls, ')', '(', line); - break; - } - case '{': { /* funcargs -> constructor */ - constructor(ls, &args); - break; - } - case TK_STRING: { /* funcargs -> STRING */ - codestring(ls, &args, ls->t.seminfo.ts); - luaX_next(ls); /* must use 'seminfo' before 'next' */ - break; - } - default: { - luaX_syntaxerror(ls, "function arguments expected"); - } - } - lua_assert(f->k == VNONRELOC); - base = f->u.info; /* base register for call */ - if (hasmultret(args.k)) - nparams = LUA_MULTRET; /* open call */ - else { - if (args.k != VVOID) - luaK_exp2nextreg(fs, &args); /* close last argument */ - nparams = fs->freereg - (base+1); - } - init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); - luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ -} - - - - -/* -** {====================================================================== -** Expression parsing -** ======================================================================= -*/ - - -static void primaryexp (LexState *ls, expdesc *v) { - /* primaryexp -> NAME | '(' expr ')' */ - switch (ls->t.token) { - case '(': { - int line = ls->linenumber; - luaX_next(ls); - expr(ls, v); - check_match(ls, ')', '(', line); - luaK_dischargevars(ls->fs, v); - return; - } - case TK_NAME: { - singlevar(ls, v); - return; - } - default: { - luaX_syntaxerror(ls, "unexpected symbol"); - } - } -} - - -static void suffixedexp (LexState *ls, expdesc *v) { - /* suffixedexp -> - primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - primaryexp(ls, v); - for (;;) { - switch (ls->t.token) { - case '.': { /* fieldsel */ - fieldsel(ls, v); - break; - } - case '[': { /* '[' exp1 ']' */ - expdesc key; - luaK_exp2anyregup(fs, v); - yindex(ls, &key); - luaK_indexed(fs, v, &key); - break; - } - case ':': { /* ':' NAME funcargs */ - expdesc key; - luaX_next(ls); - checkname(ls, &key); - luaK_self(fs, v, &key); - funcargs(ls, v, line); - break; - } - case '(': case TK_STRING: case '{': { /* funcargs */ - luaK_exp2nextreg(fs, v); - funcargs(ls, v, line); - break; - } - default: return; - } - } -} - - -static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | - constructor | FUNCTION body | suffixedexp */ - switch (ls->t.token) { - case TK_FLT: { - init_exp(v, VKFLT, 0); - v->u.nval = ls->t.seminfo.r; - break; - } - case TK_INT: { - init_exp(v, VKINT, 0); - v->u.ival = ls->t.seminfo.i; - break; - } - case TK_STRING: { - codestring(ls, v, ls->t.seminfo.ts); - break; - } - case TK_NIL: { - init_exp(v, VNIL, 0); - break; - } - case TK_TRUE: { - init_exp(v, VTRUE, 0); - break; - } - case TK_FALSE: { - init_exp(v, VFALSE, 0); - break; - } - case TK_DOTS: { /* vararg */ - FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, - "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); - break; - } - case '{': { /* constructor */ - constructor(ls, v); - return; - } - case TK_FUNCTION: { - luaX_next(ls); - body(ls, v, 0, ls->linenumber); - return; - } - default: { - suffixedexp(ls, v); - return; - } - } - luaX_next(ls); -} - - -static UnOpr getunopr (int op) { - switch (op) { - case TK_NOT: return OPR_NOT; - case '-': return OPR_MINUS; - case '~': return OPR_BNOT; - case '#': return OPR_LEN; - default: return OPR_NOUNOPR; - } -} - - -static BinOpr getbinopr (int op) { - switch (op) { - case '+': return OPR_ADD; - case '-': return OPR_SUB; - case '*': return OPR_MUL; - case '%': return OPR_MOD; - case '^': return OPR_POW; - case '/': return OPR_DIV; - case TK_IDIV: return OPR_IDIV; - case '&': return OPR_BAND; - case '|': return OPR_BOR; - case '~': return OPR_BXOR; - case TK_SHL: return OPR_SHL; - case TK_SHR: return OPR_SHR; - case TK_CONCAT: return OPR_CONCAT; - case TK_NE: return OPR_NE; - case TK_EQ: return OPR_EQ; - case '<': return OPR_LT; - case TK_LE: return OPR_LE; - case '>': return OPR_GT; - case TK_GE: return OPR_GE; - case TK_AND: return OPR_AND; - case TK_OR: return OPR_OR; - default: return OPR_NOBINOPR; - } -} - - -static const struct { - lu_byte left; /* left priority for each binary operator */ - lu_byte right; /* right priority */ -} priority[] = { /* ORDER OPR */ - {10, 10}, {10, 10}, /* '+' '-' */ - {11, 11}, {11, 11}, /* '*' '%' */ - {14, 13}, /* '^' (right associative) */ - {11, 11}, {11, 11}, /* '/' '//' */ - {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ - {7, 7}, {7, 7}, /* '<<' '>>' */ - {9, 8}, /* '..' (right associative) */ - {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ - {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ - {2, 2}, {1, 1} /* and, or */ -}; - -#define UNARY_PRIORITY 12 /* priority for unary operators */ - - -/* -** subexpr -> (simpleexp | unop subexpr) { binop subexpr } -** where 'binop' is any binary operator with a priority higher than 'limit' -*/ -static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { - BinOpr op; - UnOpr uop; - enterlevel(ls); - uop = getunopr(ls->t.token); - if (uop != OPR_NOUNOPR) { - int line = ls->linenumber; - luaX_next(ls); - subexpr(ls, v, UNARY_PRIORITY); - luaK_prefix(ls->fs, uop, v, line); - } - else simpleexp(ls, v); - /* expand while operators have priorities higher than 'limit' */ - op = getbinopr(ls->t.token); - while (op != OPR_NOBINOPR && priority[op].left > limit) { - expdesc v2; - BinOpr nextop; - int line = ls->linenumber; - luaX_next(ls); - luaK_infix(ls->fs, op, v); - /* read sub-expression with higher priority */ - nextop = subexpr(ls, &v2, priority[op].right); - luaK_posfix(ls->fs, op, v, &v2, line); - op = nextop; - } - leavelevel(ls); - return op; /* return first untreated operator */ -} - - -static void expr (LexState *ls, expdesc *v) { - subexpr(ls, v, 0); -} - -/* }==================================================================== */ - - - -/* -** {====================================================================== -** Rules for Statements -** ======================================================================= -*/ - - -static void block (LexState *ls) { - /* block -> statlist */ - FuncState *fs = ls->fs; - BlockCnt bl; - enterblock(fs, &bl, 0); - statlist(ls); - leaveblock(fs); -} - - -/* -** structure to chain all variables in the left-hand side of an -** assignment -*/ -struct LHS_assign { - struct LHS_assign *prev; - expdesc v; /* variable (global, local, upvalue, or indexed) */ -}; - - -/* -** check whether, in an assignment to an upvalue/local variable, the -** upvalue/local variable is begin used in a previous assignment to a -** table. If so, save original upvalue/local value in a safe place and -** use this safe copy in the previous assignment. -*/ -static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { - FuncState *fs = ls->fs; - int extra = fs->freereg; /* eventual position to save local variable */ - int conflict = 0; - for (; lh; lh = lh->prev) { /* check all previous assignments */ - if (lh->v.k == VINDEXED) { /* assigning to a table? */ - /* table is the upvalue/local being assigned now? */ - if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) { - conflict = 1; - lh->v.u.ind.vt = VLOCAL; - lh->v.u.ind.t = extra; /* previous assignment will use safe copy */ - } - /* index is the local being assigned? (index cannot be upvalue) */ - if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) { - conflict = 1; - lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ - } - } - } - if (conflict) { - /* copy upvalue/local value to a temporary (in position 'extra') */ - OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; - luaK_codeABC(fs, op, extra, v->u.info, 0); - luaK_reserveregs(fs, 1); - } -} - - -static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { - expdesc e; - check_condition(ls, vkisvar(lh->v.k), "syntax error"); - if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */ - struct LHS_assign nv; - nv.prev = lh; - suffixedexp(ls, &nv.v); - if (nv.v.k != VINDEXED) - check_conflict(ls, lh, &nv.v); - checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, - "C levels"); - assignment(ls, &nv, nvars+1); - } - else { /* assignment -> '=' explist */ - int nexps; - checknext(ls, '='); - nexps = explist(ls, &e); - if (nexps != nvars) - adjust_assign(ls, nvars, nexps, &e); - else { - luaK_setoneret(ls->fs, &e); /* close last expression */ - luaK_storevar(ls->fs, &lh->v, &e); - return; /* avoid default */ - } - } - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ - luaK_storevar(ls->fs, &lh->v, &e); -} - - -static int cond (LexState *ls) { - /* cond -> exp */ - expdesc v; - expr(ls, &v); /* read condition */ - if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ - luaK_goiftrue(ls->fs, &v); - return v.f; -} - - -static void gotostat (LexState *ls, int pc) { - int line = ls->linenumber; - TString *label; - int g; - if (testnext(ls, TK_GOTO)) - label = str_checkname(ls); - else { - luaX_next(ls); /* skip break */ - label = luaS_new(ls->L, "break"); - } - g = newlabelentry(ls, &ls->dyd->gt, label, line, pc); - findlabel(ls, g); /* close it if label already defined */ -} - - -/* check for repeated labels on the same block */ -static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { - int i; - for (i = fs->bl->firstlabel; i < ll->n; i++) { - if (eqstr(label, ll->arr[i].name)) { - const char *msg = luaO_pushfstring(fs->ls->L, - "label '%s' already defined on line %d", - getstr(label), ll->arr[i].line); - semerror(fs->ls, msg); - } - } -} - - -/* skip no-op statements */ -static void skipnoopstat (LexState *ls) { - while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) - statement(ls); -} - - -static void labelstat (LexState *ls, TString *label, int line) { - /* label -> '::' NAME '::' */ - FuncState *fs = ls->fs; - Labellist *ll = &ls->dyd->label; - int l; /* index of new label being created */ - checkrepeated(fs, ll, label); /* check for repeated labels */ - checknext(ls, TK_DBCOLON); /* skip double colon */ - /* create new entry for this label */ - l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); - skipnoopstat(ls); /* skip other no-op statements */ - if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ - /* assume that locals are already out of scope */ - ll->arr[l].nactvar = fs->bl->nactvar; - } - findgotos(ls, &ll->arr[l]); -} - - -static void whilestat (LexState *ls, int line) { - /* whilestat -> WHILE cond DO block END */ - FuncState *fs = ls->fs; - int whileinit; - int condexit; - BlockCnt bl; - luaX_next(ls); /* skip WHILE */ - whileinit = luaK_getlabel(fs); - condexit = cond(ls); - enterblock(fs, &bl, 1); - checknext(ls, TK_DO); - block(ls); - luaK_jumpto(fs, whileinit); - check_match(ls, TK_END, TK_WHILE, line); - leaveblock(fs); - luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ -} - - -static void repeatstat (LexState *ls, int line) { - /* repeatstat -> REPEAT block UNTIL cond */ - int condexit; - FuncState *fs = ls->fs; - int repeat_init = luaK_getlabel(fs); - BlockCnt bl1, bl2; - enterblock(fs, &bl1, 1); /* loop block */ - enterblock(fs, &bl2, 0); /* scope block */ - luaX_next(ls); /* skip REPEAT */ - statlist(ls); - check_match(ls, TK_UNTIL, TK_REPEAT, line); - condexit = cond(ls); /* read condition (inside scope block) */ - if (bl2.upval) /* upvalues? */ - luaK_patchclose(fs, condexit, bl2.nactvar); - leaveblock(fs); /* finish scope */ - luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ - leaveblock(fs); /* finish loop */ -} - - -static int exp1 (LexState *ls) { - expdesc e; - int reg; - expr(ls, &e); - luaK_exp2nextreg(ls->fs, &e); - lua_assert(e.k == VNONRELOC); - reg = e.u.info; - return reg; -} - - -static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { - /* forbody -> DO block */ - BlockCnt bl; - FuncState *fs = ls->fs; - int prep, endfor; - adjustlocalvars(ls, 3); /* control variables */ - checknext(ls, TK_DO); - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); - enterblock(fs, &bl, 0); /* scope for declared variables */ - adjustlocalvars(ls, nvars); - luaK_reserveregs(fs, nvars); - block(ls); - leaveblock(fs); /* end of scope for declared variables */ - luaK_patchtohere(fs, prep); - if (isnum) /* numeric for? */ - endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); - else { /* generic for */ - luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); - luaK_fixline(fs, line); - endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); - } - luaK_patchlist(fs, endfor, prep + 1); - luaK_fixline(fs, line); -} - - -static void fornum (LexState *ls, TString *varname, int line) { - /* fornum -> NAME = exp1,exp1[,exp1] forbody */ - FuncState *fs = ls->fs; - int base = fs->freereg; - new_localvarliteral(ls, "(for index)"); - new_localvarliteral(ls, "(for limit)"); - new_localvarliteral(ls, "(for step)"); - new_localvar(ls, varname); - checknext(ls, '='); - exp1(ls); /* initial value */ - checknext(ls, ','); - exp1(ls); /* limit */ - if (testnext(ls, ',')) - exp1(ls); /* optional step */ - else { /* default step = 1 */ - luaK_codek(fs, fs->freereg, luaK_intK(fs, 1)); - luaK_reserveregs(fs, 1); - } - forbody(ls, base, line, 1, 1); -} - - -static void forlist (LexState *ls, TString *indexname) { - /* forlist -> NAME {,NAME} IN explist forbody */ - FuncState *fs = ls->fs; - expdesc e; - int nvars = 4; /* gen, state, control, plus at least one declared var */ - int line; - int base = fs->freereg; - /* create control variables */ - new_localvarliteral(ls, "(for generator)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for control)"); - /* create declared variables */ - new_localvar(ls, indexname); - while (testnext(ls, ',')) { - new_localvar(ls, str_checkname(ls)); - nvars++; - } - checknext(ls, TK_IN); - line = ls->linenumber; - adjust_assign(ls, 3, explist(ls, &e), &e); - luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 3, 0); -} - - -static void forstat (LexState *ls, int line) { - /* forstat -> FOR (fornum | forlist) END */ - FuncState *fs = ls->fs; - TString *varname; - BlockCnt bl; - enterblock(fs, &bl, 1); /* scope for loop and control variables */ - luaX_next(ls); /* skip 'for' */ - varname = str_checkname(ls); /* first variable name */ - switch (ls->t.token) { - case '=': fornum(ls, varname, line); break; - case ',': case TK_IN: forlist(ls, varname); break; - default: luaX_syntaxerror(ls, "'=' or 'in' expected"); - } - check_match(ls, TK_END, TK_FOR, line); - leaveblock(fs); /* loop scope ('break' jumps to this point) */ -} - - -static void test_then_block (LexState *ls, int *escapelist) { - /* test_then_block -> [IF | ELSEIF] cond THEN block */ - BlockCnt bl; - FuncState *fs = ls->fs; - expdesc v; - int jf; /* instruction to skip 'then' code (if condition is false) */ - luaX_next(ls); /* skip IF or ELSEIF */ - expr(ls, &v); /* read condition */ - checknext(ls, TK_THEN); - if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - gotostat(ls, v.t); /* handle goto/break */ - while (testnext(ls, ';')) {} /* skip colons */ - if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ - leaveblock(fs); - return; /* and that is it */ - } - else /* must skip over 'then' part if condition is false */ - jf = luaK_jump(fs); - } - else { /* regular case (not goto/break) */ - luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ - enterblock(fs, &bl, 0); - jf = v.f; - } - statlist(ls); /* 'then' part */ - leaveblock(fs); - if (ls->t.token == TK_ELSE || - ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ - luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ - luaK_patchtohere(fs, jf); -} - - -static void ifstat (LexState *ls, int line) { - /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ - FuncState *fs = ls->fs; - int escapelist = NO_JUMP; /* exit list for finished parts */ - test_then_block(ls, &escapelist); /* IF cond THEN block */ - while (ls->t.token == TK_ELSEIF) - test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ - if (testnext(ls, TK_ELSE)) - block(ls); /* 'else' part */ - check_match(ls, TK_END, TK_IF, line); - luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ -} - - -static void localfunc (LexState *ls) { - expdesc b; - FuncState *fs = ls->fs; - new_localvar(ls, str_checkname(ls)); /* new local variable */ - adjustlocalvars(ls, 1); /* enter its scope */ - body(ls, &b, 0, ls->linenumber); /* function created in next register */ - /* debug information will only see the variable after this point! */ - getlocvar(fs, b.u.info)->startpc = fs->pc; -} - - -static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ - int nvars = 0; - int nexps; - expdesc e; - do { - new_localvar(ls, str_checkname(ls)); - nvars++; - } while (testnext(ls, ',')); - if (testnext(ls, '=')) - nexps = explist(ls, &e); - else { - e.k = VVOID; - nexps = 0; - } - adjust_assign(ls, nvars, nexps, &e); - adjustlocalvars(ls, nvars); -} - - -static int funcname (LexState *ls, expdesc *v) { - /* funcname -> NAME {fieldsel} [':' NAME] */ - int ismethod = 0; - singlevar(ls, v); - while (ls->t.token == '.') - fieldsel(ls, v); - if (ls->t.token == ':') { - ismethod = 1; - fieldsel(ls, v); - } - return ismethod; -} - - -static void funcstat (LexState *ls, int line) { - /* funcstat -> FUNCTION funcname body */ - int ismethod; - expdesc v, b; - luaX_next(ls); /* skip FUNCTION */ - ismethod = funcname(ls, &v); - body(ls, &b, ismethod, line); - luaK_storevar(ls->fs, &v, &b); - luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ -} - - -static void exprstat (LexState *ls) { - /* stat -> func | assignment */ - FuncState *fs = ls->fs; - struct LHS_assign v; - suffixedexp(ls, &v.v); - if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ - v.prev = NULL; - assignment(ls, &v, 1); - } - else { /* stat -> func */ - check_condition(ls, v.v.k == VCALL, "syntax error"); - SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */ - } -} - - -static void retstat (LexState *ls) { - /* stat -> RETURN [explist] [';'] */ - FuncState *fs = ls->fs; - expdesc e; - int first, nret; /* registers with returned values */ - if (block_follow(ls, 1) || ls->t.token == ';') - first = nret = 0; /* return no values */ - else { - nret = explist(ls, &e); /* optional return values */ - if (hasmultret(e.k)) { - luaK_setmultret(fs, &e); - if (e.k == VCALL && nret == 1) { /* tail call? */ - SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); - } - first = fs->nactvar; - nret = LUA_MULTRET; /* return all values */ - } - else { - if (nret == 1) /* only one single value? */ - first = luaK_exp2anyreg(fs, &e); - else { - luaK_exp2nextreg(fs, &e); /* values must go to the stack */ - first = fs->nactvar; /* return all active values */ - lua_assert(nret == fs->freereg - first); - } - } - } - luaK_ret(fs, first, nret); - testnext(ls, ';'); /* skip optional semicolon */ -} - - -static void statement (LexState *ls) { - int line = ls->linenumber; /* may be needed for error messages */ - enterlevel(ls); - switch (ls->t.token) { - case ';': { /* stat -> ';' (empty statement) */ - luaX_next(ls); /* skip ';' */ - break; - } - case TK_IF: { /* stat -> ifstat */ - ifstat(ls, line); - break; - } - case TK_WHILE: { /* stat -> whilestat */ - whilestat(ls, line); - break; - } - case TK_DO: { /* stat -> DO block END */ - luaX_next(ls); /* skip DO */ - block(ls); - check_match(ls, TK_END, TK_DO, line); - break; - } - case TK_FOR: { /* stat -> forstat */ - forstat(ls, line); - break; - } - case TK_REPEAT: { /* stat -> repeatstat */ - repeatstat(ls, line); - break; - } - case TK_FUNCTION: { /* stat -> funcstat */ - funcstat(ls, line); - break; - } - case TK_LOCAL: { /* stat -> localstat */ - luaX_next(ls); /* skip LOCAL */ - if (testnext(ls, TK_FUNCTION)) /* local function? */ - localfunc(ls); - else - localstat(ls); - break; - } - case TK_DBCOLON: { /* stat -> label */ - luaX_next(ls); /* skip double colon */ - labelstat(ls, str_checkname(ls), line); - break; - } - case TK_RETURN: { /* stat -> retstat */ - luaX_next(ls); /* skip RETURN */ - retstat(ls); - break; - } - case TK_BREAK: /* stat -> breakstat */ - case TK_GOTO: { /* stat -> 'goto' NAME */ - gotostat(ls, luaK_jump(ls->fs)); - break; - } - default: { /* stat -> func | assignment */ - exprstat(ls); - break; - } - } - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* free registers */ - leavelevel(ls); -} - -/* }====================================================================== */ - - -/* -** compiles the main function, which is a regular vararg function with an -** upvalue named LUA_ENV -*/ -static void mainfunc (LexState *ls, FuncState *fs) { - BlockCnt bl; - expdesc v; - open_func(ls, fs, &bl); - fs->f->is_vararg = 1; /* main function is always declared vararg */ - init_exp(&v, VLOCAL, 0); /* create and... */ - newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ - luaC_objbarrier(ls->L, fs->f, ls->envn); - luaX_next(ls); /* read first token */ - statlist(ls); /* parse main body */ - check(ls, TK_EOS); - close_func(ls); -} - - -LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar) { - LexState lexstate; - FuncState funcstate; - LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ - luaD_inctop(L); - lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue(L, L->top, lexstate.h); /* anchor it */ - luaD_inctop(L); - funcstate.f = cl->p = luaF_newproto(L); - luaC_objbarrier(L, cl, cl->p); - funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ - lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ - lexstate.buff = buff; - lexstate.dyd = dyd; - dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); - mainfunc(&lexstate, &funcstate); - lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); - /* all scopes should be correctly finished */ - lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - L->top--; /* remove scanner's table */ - return cl; /* closure is on the stack, too */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.h deleted file mode 100644 index f45b23c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lparser.h +++ /dev/null @@ -1,133 +0,0 @@ -/* -** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - -#ifndef lparser_h -#define lparser_h - -#include "llimits.h" -#include "lobject.h" -#include "lzio.h" - - -/* -** Expression and variable descriptor. -** Code generation for variables and expressions can be delayed to allow -** optimizations; An 'expdesc' structure describes a potentially-delayed -** variable/expression. It has a description of its "main" value plus a -** list of conditional jumps that can also produce its value (generated -** by short-circuit operators 'and'/'or'). -*/ - -/* kinds of variables/expressions */ -typedef enum { - VVOID, /* when 'expdesc' describes the last expression a list, - this kind means an empty list (so, no expression) */ - VNIL, /* constant nil */ - VTRUE, /* constant true */ - VFALSE, /* constant false */ - VK, /* constant in 'k'; info = index of constant in 'k' */ - VKFLT, /* floating constant; nval = numerical float value */ - VKINT, /* integer constant; nval = numerical integer value */ - VNONRELOC, /* expression has its value in a fixed register; - info = result register */ - VLOCAL, /* local variable; info = local register */ - VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ - VINDEXED, /* indexed variable; - ind.vt = whether 't' is register or upvalue; - ind.t = table register or upvalue; - ind.idx = key's R/K index */ - VJMP, /* expression is a test/comparison; - info = pc of corresponding jump instruction */ - VRELOCABLE, /* expression can put result in any register; - info = instruction pc */ - VCALL, /* expression is a function call; info = instruction pc */ - VVARARG /* vararg expression; info = instruction pc */ -} expkind; - - -#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) -#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) - -typedef struct expdesc { - expkind k; - union { - lua_Integer ival; /* for VKINT */ - lua_Number nval; /* for VKFLT */ - int info; /* for generic use */ - struct { /* for indexed variables (VINDEXED) */ - short idx; /* index (R/K) */ - lu_byte t; /* table (register or upvalue) */ - lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ - } ind; - } u; - int t; /* patch list of 'exit when true' */ - int f; /* patch list of 'exit when false' */ -} expdesc; - - -/* description of active local variable */ -typedef struct Vardesc { - short idx; /* variable index in stack */ -} Vardesc; - - -/* description of pending goto statements and label statements */ -typedef struct Labeldesc { - TString *name; /* label identifier */ - int pc; /* position in code */ - int line; /* line where it appeared */ - lu_byte nactvar; /* local level where it appears in current block */ -} Labeldesc; - - -/* list of labels or gotos */ -typedef struct Labellist { - Labeldesc *arr; /* array */ - int n; /* number of entries in use */ - int size; /* array size */ -} Labellist; - - -/* dynamic structures used by the parser */ -typedef struct Dyndata { - struct { /* list of active local variables */ - Vardesc *arr; - int n; - int size; - } actvar; - Labellist gt; /* list of pending gotos */ - Labellist label; /* list of active labels */ -} Dyndata; - - -/* control of blocks */ -struct BlockCnt; /* defined in lparser.c */ - - -/* state needed to generate code for a given function */ -typedef struct FuncState { - Proto *f; /* current function header */ - struct FuncState *prev; /* enclosing function */ - struct LexState *ls; /* lexical state */ - struct BlockCnt *bl; /* chain of current blocks */ - int pc; /* next position to code (equivalent to 'ncode') */ - int lasttarget; /* 'label' of last 'jump label' */ - int jpc; /* list of pending jumps to 'pc' */ - int nk; /* number of elements in 'k' */ - int np; /* number of elements in 'p' */ - int firstlocal; /* index of first local var (in Dyndata array) */ - short nlocvars; /* number of elements in 'f->locvars' */ - lu_byte nactvar; /* number of active local variables */ - lu_byte nups; /* number of upvalues */ - lu_byte freereg; /* first free register */ -} FuncState; - - -LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lprefix.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lprefix.h deleted file mode 100644 index 9a749a3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lprefix.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $ -** Definitions for Lua code that must come before any other header file -** See Copyright Notice in lua.h -*/ - -#ifndef lprefix_h -#define lprefix_h - - -/* -** Allows POSIX/XSI stuff -*/ -#if !defined(LUA_USE_C89) /* { */ - -#if !defined(_XOPEN_SOURCE) -#define _XOPEN_SOURCE 600 -#elif _XOPEN_SOURCE == 0 -#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ -#endif - -/* -** Allows manipulation of large files in gcc and some other compilers -*/ -#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) -#define _LARGEFILE_SOURCE 1 -#define _FILE_OFFSET_BITS 64 -#endif - -#endif /* } */ - - -/* -** Windows stuff -*/ -#if defined(_WIN32) /* { */ - -#if !defined(_CRT_SECURE_NO_WARNINGS) -#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ -#endif - -#endif /* } */ - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.c deleted file mode 100644 index c1a7664..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.c +++ /dev/null @@ -1,347 +0,0 @@ -/* -** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ -** Global State -** See Copyright Notice in lua.h -*/ - -#define lstate_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -#if !defined(LUAI_GCPAUSE) -#define LUAI_GCPAUSE 200 /* 200% */ -#endif - -#if !defined(LUAI_GCMUL) -#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ -#endif - - -/* -** a macro to help the creation of a unique random seed when a state is -** created; the seed is used to randomize hashes. -*/ -#if !defined(luai_makeseed) -#include -#define luai_makeseed() cast(unsigned int, time(NULL)) -#endif - - - -/* -** thread state + extra space -*/ -typedef struct LX { - lu_byte extra_[LUA_EXTRASPACE]; - lua_State l; -} LX; - - -/* -** Main thread combines a thread state and the global state -*/ -typedef struct LG { - LX l; - global_State g; -} LG; - - - -#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) - - -/* -** Compute an initial seed as random as possible. Rely on Address Space -** Layout Randomization (if present) to increase randomness.. -*/ -#define addbuff(b,p,e) \ - { size_t t = cast(size_t, e); \ - memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int makeseed (lua_State *L) { - char buff[4 * sizeof(size_t)]; - unsigned int h = luai_makeseed(); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, luaO_nilobject); /* global variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} - - -/* -** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant (and avoiding underflows in 'totalbytes') -*/ -void luaE_setdebt (global_State *g, l_mem debt) { - l_mem tb = gettotalbytes(g); - lua_assert(tb > 0); - if (debt < tb - MAX_LMEM) - debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ - g->totalbytes = tb - debt; - g->GCdebt = debt; -} - - -CallInfo *luaE_extendCI (lua_State *L) { - CallInfo *ci = luaM_new(L, CallInfo); - lua_assert(L->ci->next == NULL); - L->ci->next = ci; - ci->previous = L->ci; - ci->next = NULL; - L->nci++; - return ci; -} - - -/* -** free all CallInfo structures not in use by a thread -*/ -void luaE_freeCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next = ci->next; - ci->next = NULL; - while ((ci = next) != NULL) { - next = ci->next; - luaM_free(L, ci); - L->nci--; - } -} - - -/* -** free half of the CallInfo structures not in use by a thread -*/ -void luaE_shrinkCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next2; /* next's next */ - /* while there are two nexts */ - while (ci->next != NULL && (next2 = ci->next->next) != NULL) { - luaM_free(L, ci->next); /* free next */ - L->nci--; - ci->next = next2; /* remove 'next' from the list */ - next2->previous = ci; - ci = next2; /* keep next's next */ - } -} - - -static void stack_init (lua_State *L1, lua_State *L) { - int i; CallInfo *ci; - /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); - L1->stacksize = BASIC_STACK_SIZE; - for (i = 0; i < BASIC_STACK_SIZE; i++) - setnilvalue(L1->stack + i); /* erase new stack */ - L1->top = L1->stack; - L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; - /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - ci->callstatus = 0; - ci->func = L1->top; - setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ - ci->top = L1->top + LUA_MINSTACK; - L1->ci = ci; -} - - -static void freestack (lua_State *L) { - if (L->stack == NULL) - return; /* stack not completely built yet */ - L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); - lua_assert(L->nci == 0); - luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ -} - - -/* -** Create registry table and its predefined values -*/ -static void init_registry (lua_State *L, global_State *g) { - TValue temp; - /* create registry */ - Table *registry = luaH_new(L); - sethvalue(L, &g->l_registry, registry); - luaH_resize(L, registry, LUA_RIDX_LAST, 0); - /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, &temp, L); /* temp = L */ - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); - /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); -} - - -/* -** open parts of the state that may cause memory-allocation errors. -** ('g->version' != NULL flags that the state was completely build) -*/ -static void f_luaopen (lua_State *L, void *ud) { - global_State *g = G(L); - UNUSED(ud); - stack_init(L, L); /* init stack */ - init_registry(L, g); - luaS_init(L); - luaT_init(L); - luaX_init(L); - g->gcrunning = 1; /* allow gc */ - g->version = lua_version(NULL); - luai_userstateopen(L); -} - - -/* -** preinitialize a thread with consistent values without allocating -** any memory (to avoid errors) -*/ -static void preinit_thread (lua_State *L, global_State *g) { - G(L) = g; - L->stack = NULL; - L->ci = NULL; - L->nci = 0; - L->stacksize = 0; - L->twups = L; /* thread has no upvalues */ - L->errorJmp = NULL; - L->nCcalls = 0; - L->hook = NULL; - L->hookmask = 0; - L->basehookcount = 0; - L->allowhook = 1; - resethookcount(L); - L->openupval = NULL; - L->nny = 1; - L->status = LUA_OK; - L->errfunc = 0; -} - - -static void close_state (lua_State *L) { - global_State *g = G(L); - luaF_close(L, L->stack); /* close all upvalues for this thread */ - luaC_freeallobjects(L); /* collect all objects */ - if (g->version) /* closing a fully built state? */ - luai_userstateclose(L); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); - freestack(L); - lua_assert(gettotalbytes(g) == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ -} - - -LUA_API lua_State *lua_newthread (lua_State *L) { - global_State *g = G(L); - lua_State *L1; - lua_lock(L); - luaC_checkGC(L); - /* create new thread */ - L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; - L1->marked = luaC_white(g); - L1->tt = LUA_TTHREAD; - /* link it on list 'allgc' */ - L1->next = g->allgc; - g->allgc = obj2gco(L1); - /* anchor it on L stack */ - setthvalue(L, L->top, L1); - api_incr_top(L); - preinit_thread(L1, g); - L1->hookmask = L->hookmask; - L1->basehookcount = L->basehookcount; - L1->hook = L->hook; - resethookcount(L1); - /* initialize L1 extra space */ - memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), - LUA_EXTRASPACE); - luai_userstatethread(L, L1); - stack_init(L1, L); /* init stack */ - lua_unlock(L); - return L1; -} - - -void luaE_freethread (lua_State *L, lua_State *L1) { - LX *l = fromstate(L1); - luaF_close(L1, L1->stack); /* close all upvalues for this thread */ - lua_assert(L1->openupval == NULL); - luai_userstatefree(L, L1); - freestack(L1); - luaM_free(L, l); -} - - -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { - int i; - lua_State *L; - global_State *g; - LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); - if (l == NULL) return NULL; - L = &l->l.l; - g = &l->g; - L->next = NULL; - L->tt = LUA_TTHREAD; - g->currentwhite = bitmask(WHITE0BIT); - L->marked = luaC_white(g); - preinit_thread(L, g); - g->frealloc = f; - g->ud = ud; - g->mainthread = L; - g->seed = makeseed(L); - g->gcrunning = 0; /* no GC while building state */ - g->GCestimate = 0; - g->strt.size = g->strt.nuse = 0; - g->strt.hash = NULL; - setnilvalue(&g->l_registry); - g->panic = NULL; - g->version = NULL; - g->gcstate = GCSpause; - g->gckind = KGC_NORMAL; - g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; - g->sweepgc = NULL; - g->gray = g->grayagain = NULL; - g->weak = g->ephemeron = g->allweak = NULL; - g->twups = NULL; - g->totalbytes = sizeof(LG); - g->GCdebt = 0; - g->gcfinnum = 0; - g->gcpause = LUAI_GCPAUSE; - g->gcstepmul = LUAI_GCMUL; - for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; - if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { - /* memory allocation error: free partial state */ - close_state(L); - L = NULL; - } - return L; -} - - -LUA_API void lua_close (lua_State *L) { - L = G(L)->mainthread; /* only the main thread can be closed */ - lua_lock(L); - close_state(L); -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.h deleted file mode 100644 index 56b3741..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstate.h +++ /dev/null @@ -1,253 +0,0 @@ -/* -** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ -** Global State -** See Copyright Notice in lua.h -*/ - -#ifndef lstate_h -#define lstate_h - -#include "lua.h" - -#include "lobject.h" -#include "ltm.h" -#include "lzio.h" - - -/* - -** Some notes about garbage-collected objects: All objects in Lua must -** be kept somehow accessible until being freed, so all objects always -** belong to one (and only one) of these lists, using field 'next' of -** the 'CommonHeader' for the link: -** -** 'allgc': all objects not marked for finalization; -** 'finobj': all objects marked for finalization; -** 'tobefnz': all objects ready to be finalized; -** 'fixedgc': all objects that are not to be collected (currently -** only small strings, such as reserved words). -** -** Moreover, there is another set of lists that control gray objects. -** These lists are linked by fields 'gclist'. (All objects that -** can become gray have such a field. The field is not the same -** in all objects, but it always has this name.) Any gray object -** must belong to one of these lists, and all objects in these lists -** must be gray: -** -** 'gray': regular gray objects, still waiting to be visited. -** 'grayagain': objects that must be revisited at the atomic phase. -** That includes -** - black objects got in a write barrier; -** - all kinds of weak tables during propagation phase; -** - all threads. -** 'weak': tables with weak values to be cleared; -** 'ephemeron': ephemeron tables with white->white entries; -** 'allweak': tables with weak keys and/or weak values to be cleared. -** The last three lists are used only during the atomic phase. - -*/ - - -struct lua_longjmp; /* defined in ldo.c */ - - -/* -** Atomic type (relative to signals) to better ensure that 'lua_sethook' -** is thread safe -*/ -#if !defined(l_signalT) -#include -#define l_signalT sig_atomic_t -#endif - - -/* extra stack space to handle TM calls and some other extras */ -#define EXTRA_STACK 5 - - -#define BASIC_STACK_SIZE (2*LUA_MINSTACK) - - -/* kinds of Garbage Collection */ -#define KGC_NORMAL 0 -#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ - - -typedef struct stringtable { - TString **hash; - int nuse; /* number of elements */ - int size; -} stringtable; - - -/* -** Information about a call. -** When a thread yields, 'func' is adjusted to pretend that the -** top function has only the yielded values in its stack; in that -** case, the actual 'func' value is saved in field 'extra'. -** When a function calls another with a continuation, 'extra' keeps -** the function index so that, in case of errors, the continuation -** function can be called with the correct top. -*/ -typedef struct CallInfo { - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ - struct CallInfo *previous, *next; /* dynamic call link */ - union { - struct { /* only for Lua functions */ - StkId base; /* base for this function */ - const Instruction *savedpc; - } l; - struct { /* only for C functions */ - lua_KFunction k; /* continuation in case of yields */ - ptrdiff_t old_errfunc; - lua_KContext ctx; /* context info. in case of yields */ - } c; - } u; - ptrdiff_t extra; - short nresults; /* expected number of results from this function */ - unsigned short callstatus; -} CallInfo; - - -/* -** Bits in CallInfo status -*/ -#define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_LUA (1<<1) /* call is running a Lua function */ -#define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_FRESH (1<<3) /* call is running on a fresh invocation - of luaV_execute */ -#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<5) /* call was tail called */ -#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_LEQ (1<<7) /* using __lt for __le */ -#define CIST_FIN (1<<8) /* call is running a finalizer */ - -#define isLua(ci) ((ci)->callstatus & CIST_LUA) - -/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ -#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) -#define getoah(st) ((st) & CIST_OAH) - - -/* -** 'global state', shared by all threads of this state -*/ -typedef struct global_State { - lua_Alloc frealloc; /* function to reallocate memory */ - void *ud; /* auxiliary data to 'frealloc' */ - l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ - l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ - lu_mem GCmemtrav; /* memory traversed by the GC */ - lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ - stringtable strt; /* hash table for strings */ - TValue l_registry; - unsigned int seed; /* randomized seed for hashes */ - lu_byte currentwhite; - lu_byte gcstate; /* state of garbage collector */ - lu_byte gckind; /* kind of GC running */ - lu_byte gcrunning; /* true if GC is running */ - GCObject *allgc; /* list of all collectable objects */ - GCObject **sweepgc; /* current position of sweep in list */ - GCObject *finobj; /* list of collectable objects with finalizers */ - GCObject *gray; /* list of gray objects */ - GCObject *grayagain; /* list of objects to be traversed atomically */ - GCObject *weak; /* list of tables with weak values */ - GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ - GCObject *allweak; /* list of all-weak tables */ - GCObject *tobefnz; /* list of userdata to be GC */ - GCObject *fixedgc; /* list of objects not to be collected */ - struct lua_State *twups; /* list of threads with open upvalues */ - unsigned int gcfinnum; /* number of finalizers to call in each GC step */ - int gcpause; /* size of pause between successive GCs */ - int gcstepmul; /* GC 'granularity' */ - lua_CFunction panic; /* to be called in unprotected errors */ - struct lua_State *mainthread; - const lua_Number *version; /* pointer to version number */ - TString *memerrmsg; /* memory-error message */ - TString *tmname[TM_N]; /* array with tag-method names */ - struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ - TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ -} global_State; - - -/* -** 'per thread' state -*/ -struct lua_State { - CommonHeader; - unsigned short nci; /* number of items in 'ci' list */ - lu_byte status; - StkId top; /* first free slot in the stack */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - const Instruction *oldpc; /* last pc traced */ - StkId stack_last; /* last free slot in the stack */ - StkId stack; /* stack base */ - UpVal *openupval; /* list of open upvalues in this stack */ - GCObject *gclist; - struct lua_State *twups; /* list of threads with open upvalues */ - struct lua_longjmp *errorJmp; /* current error recover point */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ - volatile lua_Hook hook; - ptrdiff_t errfunc; /* current error handling function (stack index) */ - int stacksize; - int basehookcount; - int hookcount; - unsigned short nny; /* number of non-yieldable calls in stack */ - unsigned short nCcalls; /* number of nested C calls */ - l_signalT hookmask; - lu_byte allowhook; -}; - - -#define G(L) (L->l_G) - - -/* -** Union of all collectable objects (only for conversions) -*/ -union GCUnion { - GCObject gc; /* common header */ - struct TString ts; - struct Udata u; - union Closure cl; - struct Table h; - struct Proto p; - struct lua_State th; /* thread */ -}; - - -#define cast_u(o) cast(union GCUnion *, (o)) - -/* macros to convert a GCObject into a specific value */ -#define gco2ts(o) \ - check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) -#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) -#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) -#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) -#define gco2cl(o) \ - check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) -#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) -#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) -#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) - - -/* macro to convert a Lua object into a GCObject */ -#define obj2gco(v) \ - check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) - - -/* actual number of total bytes allocated */ -#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) - -LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); -LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); -LUAI_FUNC void luaE_freeCI (lua_State *L); -LUAI_FUNC void luaE_shrinkCI (lua_State *L); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.c deleted file mode 100644 index 6257f21..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.c +++ /dev/null @@ -1,248 +0,0 @@ -/* -** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $ -** String table (keeps all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - -#define lstring_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" - - -#define MEMERRMSG "not enough memory" - - -/* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to -** compute its hash -*/ -#if !defined(LUAI_HASHLIMIT) -#define LUAI_HASHLIMIT 5 -#endif - - -/* -** equality for long strings -*/ -int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->u.lnglen; - lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); - return (a == b) || /* same instance or... */ - ((len == b->u.lnglen) && /* equal length and ... */ - (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ -} - - -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { - unsigned int h = seed ^ cast(unsigned int, l); - size_t step = (l >> LUAI_HASHLIMIT) + 1; - for (; l >= step; l -= step) - h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); - return h; -} - - -unsigned int luaS_hashlongstr (TString *ts) { - lua_assert(ts->tt == LUA_TLNGSTR); - if (ts->extra == 0) { /* no hash? */ - ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); - ts->extra = 1; /* now it has its hash */ - } - return ts->hash; -} - - -/* -** resizes the string table -*/ -void luaS_resize (lua_State *L, int newsize) { - int i; - stringtable *tb = &G(L)->strt; - if (newsize > tb->size) { /* grow table if needed */ - luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); - for (i = tb->size; i < newsize; i++) - tb->hash[i] = NULL; - } - for (i = 0; i < tb->size; i++) { /* rehash */ - TString *p = tb->hash[i]; - tb->hash[i] = NULL; - while (p) { /* for each node in the list */ - TString *hnext = p->u.hnext; /* save next */ - unsigned int h = lmod(p->hash, newsize); /* new position */ - p->u.hnext = tb->hash[h]; /* chain it */ - tb->hash[h] = p; - p = hnext; - } - } - if (newsize < tb->size) { /* shrink table if needed */ - /* vanishing slice should be empty */ - lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); - luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); - } - tb->size = newsize; -} - - -/* -** Clear API string cache. (Entries cannot be empty, so fill them with -** a non-collectable string.) -*/ -void luaS_clearcache (global_State *g) { - int i, j; - for (i = 0; i < STRCACHE_N; i++) - for (j = 0; j < STRCACHE_M; j++) { - if (iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ - } -} - - -/* -** Initialize the string table and the string cache -*/ -void luaS_init (lua_State *L) { - global_State *g = G(L); - int i, j; - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ - for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ - for (j = 0; j < STRCACHE_M; j++) - g->strcache[i][j] = g->memerrmsg; -} - - - -/* -** creates a new string object -*/ -static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { - TString *ts; - GCObject *o; - size_t totalsize; /* total size of TString object */ - totalsize = sizelstring(l); - o = luaC_newobj(L, tag, totalsize); - ts = gco2ts(o); - ts->hash = h; - ts->extra = 0; - getstr(ts)[l] = '\0'; /* ending 0 */ - return ts; -} - - -TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); - ts->u.lnglen = l; - return ts; -} - - -void luaS_remove (lua_State *L, TString *ts) { - stringtable *tb = &G(L)->strt; - TString **p = &tb->hash[lmod(ts->hash, tb->size)]; - while (*p != ts) /* find previous element */ - p = &(*p)->u.hnext; - *p = (*p)->u.hnext; /* remove element from its list */ - tb->nuse--; -} - - -/* -** checks whether short string exists and reuses it or creates a new one -*/ -static TString *internshrstr (lua_State *L, const char *str, size_t l) { - TString *ts; - global_State *g = G(L); - unsigned int h = luaS_hash(str, l, g->seed); - TString **list = &g->strt.hash[lmod(h, g->strt.size)]; - lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ - for (ts = *list; ts != NULL; ts = ts->u.hnext) { - if (l == ts->shrlen && - (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { - /* found! */ - if (isdead(g, ts)) /* dead (but not collected yet)? */ - changewhite(ts); /* resurrect it */ - return ts; - } - } - if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { - luaS_resize(L, g->strt.size * 2); - list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ - } - ts = createstrobj(L, l, LUA_TSHRSTR, h); - memcpy(getstr(ts), str, l * sizeof(char)); - ts->shrlen = cast_byte(l); - ts->u.hnext = *list; - *list = ts; - g->strt.nuse++; - return ts; -} - - -/* -** new string (with explicit length) -*/ -TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { - if (l <= LUAI_MAXSHORTLEN) /* short string? */ - return internshrstr(L, str, l); - else { - TString *ts; - if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) - luaM_toobig(L); - ts = luaS_createlngstrobj(L, l); - memcpy(getstr(ts), str, l * sizeof(char)); - return ts; - } -} - - -/* -** Create or reuse a zero-terminated string, first checking in the -** cache (using the string address as a key). The cache can contain -** only zero-terminated strings, so it is safe to use 'strcmp' to -** check hits. -*/ -TString *luaS_new (lua_State *L, const char *str) { - unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ - int j; - TString **p = G(L)->strcache[i]; - for (j = 0; j < STRCACHE_M; j++) { - if (strcmp(str, getstr(p[j])) == 0) /* hit? */ - return p[j]; /* that is it */ - } - /* normal route */ - for (j = STRCACHE_M - 1; j > 0; j--) - p[j] = p[j - 1]; /* move out last element */ - /* new element is first in the list */ - p[0] = luaS_newlstr(L, str, strlen(str)); - return p[0]; -} - - -Udata *luaS_newudata (lua_State *L, size_t s) { - Udata *u; - GCObject *o; - if (s > MAX_SIZE - sizeof(Udata)) - luaM_toobig(L); - o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); - u = gco2u(o); - u->len = s; - u->metatable = NULL; - setuservalue(L, u, luaO_nilobject); - return u; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.h deleted file mode 100644 index d612abd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstring.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $ -** String table (keep all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - -#ifndef lstring_h -#define lstring_h - -#include "lgc.h" -#include "lobject.h" -#include "lstate.h" - - -#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) - -#define sizeludata(l) (sizeof(union UUdata) + (l)) -#define sizeudata(u) sizeludata((u)->len) - -#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ - (sizeof(s)/sizeof(char))-1)) - - -/* -** test whether a string is a reserved word -*/ -#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) - - -/* -** equality for short strings, which are always internalized -*/ -#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) - - -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); -LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); -LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); -LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC void luaS_clearcache (global_State *g); -LUAI_FUNC void luaS_init (lua_State *L); -LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); -LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); -LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); -LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstrlib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstrlib.c deleted file mode 100644 index b4bed7e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lstrlib.c +++ /dev/null @@ -1,1584 +0,0 @@ -/* -** $Id: lstrlib.c,v 1.254.1.1 2017/04/19 17:29:57 roberto Exp $ -** Standard library for string operations and pattern-matching -** See Copyright Notice in lua.h -*/ - -#define lstrlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** maximum number of captures that a pattern can do during -** pattern-matching. This limit is arbitrary, but must fit in -** an unsigned char. -*/ -#if !defined(LUA_MAXCAPTURES) -#define LUA_MAXCAPTURES 32 -#endif - - -/* macro to 'unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - -/* -** Some sizes are better limited to fit in 'int', but must also fit in -** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) -*/ -#define MAX_SIZET ((size_t)(~(size_t)0)) - -#define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) - - - - -static int str_len (lua_State *L) { - size_t l; - luaL_checklstring(L, 1, &l); - lua_pushinteger(L, (lua_Integer)l); - return 1; -} - - -/* translate a relative string position: negative means back from end */ -static lua_Integer posrelat (lua_Integer pos, size_t len) { - if (pos >= 0) return pos; - else if (0u - (size_t)pos > len) return 0; - else return (lua_Integer)len + pos + 1; -} - - -static int str_sub (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - lua_Integer start = posrelat(luaL_checkinteger(L, 2), l); - lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l); - if (start < 1) start = 1; - if (end > (lua_Integer)l) end = l; - if (start <= end) - lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1); - else lua_pushliteral(L, ""); - return 1; -} - - -static int str_reverse (lua_State *L) { - size_t l, i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - char *p = luaL_buffinitsize(L, &b, l); - for (i = 0; i < l; i++) - p[i] = s[l - i - 1]; - luaL_pushresultsize(&b, l); - return 1; -} - - -static int str_lower (lua_State *L) { - size_t l; - size_t i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - char *p = luaL_buffinitsize(L, &b, l); - for (i=0; i MAXSIZE / n) /* may overflow? */ - return luaL_error(L, "resulting string too large"); - else { - size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; - luaL_Buffer b; - char *p = luaL_buffinitsize(L, &b, totallen); - while (n-- > 1) { /* first n-1 copies (followed by separator) */ - memcpy(p, s, l * sizeof(char)); p += l; - if (lsep > 0) { /* empty 'memcpy' is not that cheap */ - memcpy(p, sep, lsep * sizeof(char)); - p += lsep; - } - } - memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ - luaL_pushresultsize(&b, totallen); - } - return 1; -} - - -static int str_byte (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); - lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l); - int n, i; - if (posi < 1) posi = 1; - if (pose > (lua_Integer)l) pose = l; - if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= INT_MAX) /* arithmetic overflow? */ - return luaL_error(L, "string slice too long"); - n = (int)(pose - posi) + 1; - luaL_checkstack(L, n, "string slice too long"); - for (i=0; i= ms->level || ms->capture[l].len == CAP_UNFINISHED) - return luaL_error(ms->L, "invalid capture index %%%d", l + 1); - return l; -} - - -static int capture_to_close (MatchState *ms) { - int level = ms->level; - for (level--; level>=0; level--) - if (ms->capture[level].len == CAP_UNFINISHED) return level; - return luaL_error(ms->L, "invalid pattern capture"); -} - - -static const char *classend (MatchState *ms, const char *p) { - switch (*p++) { - case L_ESC: { - if (p == ms->p_end) - luaL_error(ms->L, "malformed pattern (ends with '%%')"); - return p+1; - } - case '[': { - if (*p == '^') p++; - do { /* look for a ']' */ - if (p == ms->p_end) - luaL_error(ms->L, "malformed pattern (missing ']')"); - if (*(p++) == L_ESC && p < ms->p_end) - p++; /* skip escapes (e.g. '%]') */ - } while (*p != ']'); - return p+1; - } - default: { - return p; - } - } -} - - -static int match_class (int c, int cl) { - int res; - switch (tolower(cl)) { - case 'a' : res = isalpha(c); break; - case 'c' : res = iscntrl(c); break; - case 'd' : res = isdigit(c); break; - case 'g' : res = isgraph(c); break; - case 'l' : res = islower(c); break; - case 'p' : res = ispunct(c); break; - case 's' : res = isspace(c); break; - case 'u' : res = isupper(c); break; - case 'w' : res = isalnum(c); break; - case 'x' : res = isxdigit(c); break; - case 'z' : res = (c == 0); break; /* deprecated option */ - default: return (cl == c); - } - return (islower(cl) ? res : !res); -} - - -static int matchbracketclass (int c, const char *p, const char *ec) { - int sig = 1; - if (*(p+1) == '^') { - sig = 0; - p++; /* skip the '^' */ - } - while (++p < ec) { - if (*p == L_ESC) { - p++; - if (match_class(c, uchar(*p))) - return sig; - } - else if ((*(p+1) == '-') && (p+2 < ec)) { - p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) - return sig; - } - else if (uchar(*p) == c) return sig; - } - return !sig; -} - - -static int singlematch (MatchState *ms, const char *s, const char *p, - const char *ep) { - if (s >= ms->src_end) - return 0; - else { - int c = uchar(*s); - switch (*p) { - case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); - case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); - } - } -} - - -static const char *matchbalance (MatchState *ms, const char *s, - const char *p) { - if (p >= ms->p_end - 1) - luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); - if (*s != *p) return NULL; - else { - int b = *p; - int e = *(p+1); - int cont = 1; - while (++s < ms->src_end) { - if (*s == e) { - if (--cont == 0) return s+1; - } - else if (*s == b) cont++; - } - } - return NULL; /* string ends out of balance */ -} - - -static const char *max_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - ptrdiff_t i = 0; /* counts maximum expand for item */ - while (singlematch(ms, s + i, p, ep)) - i++; - /* keeps trying to match with the maximum repetitions */ - while (i>=0) { - const char *res = match(ms, (s+i), ep+1); - if (res) return res; - i--; /* else didn't match; reduce 1 repetition to try again */ - } - return NULL; -} - - -static const char *min_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - for (;;) { - const char *res = match(ms, s, ep+1); - if (res != NULL) - return res; - else if (singlematch(ms, s, p, ep)) - s++; /* try with one more repetition */ - else return NULL; - } -} - - -static const char *start_capture (MatchState *ms, const char *s, - const char *p, int what) { - const char *res; - int level = ms->level; - if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); - ms->capture[level].init = s; - ms->capture[level].len = what; - ms->level = level+1; - if ((res=match(ms, s, p)) == NULL) /* match failed? */ - ms->level--; /* undo capture */ - return res; -} - - -static const char *end_capture (MatchState *ms, const char *s, - const char *p) { - int l = capture_to_close(ms); - const char *res; - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ - if ((res = match(ms, s, p)) == NULL) /* match failed? */ - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ - return res; -} - - -static const char *match_capture (MatchState *ms, const char *s, int l) { - size_t len; - l = check_capture(ms, l); - len = ms->capture[l].len; - if ((size_t)(ms->src_end-s) >= len && - memcmp(ms->capture[l].init, s, len) == 0) - return s+len; - else return NULL; -} - - -static const char *match (MatchState *ms, const char *s, const char *p) { - if (ms->matchdepth-- == 0) - luaL_error(ms->L, "pattern too complex"); - init: /* using goto's to optimize tail recursion */ - if (p != ms->p_end) { /* end of pattern? */ - switch (*p) { - case '(': { /* start capture */ - if (*(p + 1) == ')') /* position capture? */ - s = start_capture(ms, s, p + 2, CAP_POSITION); - else - s = start_capture(ms, s, p + 1, CAP_UNFINISHED); - break; - } - case ')': { /* end capture */ - s = end_capture(ms, s, p + 1); - break; - } - case '$': { - if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ - goto dflt; /* no; go to default */ - s = (s == ms->src_end) ? s : NULL; /* check end of string */ - break; - } - case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ - switch (*(p + 1)) { - case 'b': { /* balanced string? */ - s = matchbalance(ms, s, p + 2); - if (s != NULL) { - p += 4; goto init; /* return match(ms, s, p + 4); */ - } /* else fail (s == NULL) */ - break; - } - case 'f': { /* frontier? */ - const char *ep; char previous; - p += 2; - if (*p != '[') - luaL_error(ms->L, "missing '[' after '%%f' in pattern"); - ep = classend(ms, p); /* points to what is next */ - previous = (s == ms->src_init) ? '\0' : *(s - 1); - if (!matchbracketclass(uchar(previous), p, ep - 1) && - matchbracketclass(uchar(*s), p, ep - 1)) { - p = ep; goto init; /* return match(ms, s, ep); */ - } - s = NULL; /* match failed */ - break; - } - case '0': case '1': case '2': case '3': - case '4': case '5': case '6': case '7': - case '8': case '9': { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p + 1))); - if (s != NULL) { - p += 2; goto init; /* return match(ms, s, p + 2) */ - } - break; - } - default: goto dflt; - } - break; - } - default: dflt: { /* pattern class plus optional suffix */ - const char *ep = classend(ms, p); /* points to optional suffix */ - /* does not match at least once? */ - if (!singlematch(ms, s, p, ep)) { - if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */ - p = ep + 1; goto init; /* return match(ms, s, ep + 1); */ - } - else /* '+' or no suffix */ - s = NULL; /* fail */ - } - else { /* matched once */ - switch (*ep) { /* handle optional suffix */ - case '?': { /* optional */ - const char *res; - if ((res = match(ms, s + 1, ep + 1)) != NULL) - s = res; - else { - p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */ - } - break; - } - case '+': /* 1 or more repetitions */ - s++; /* 1 match already done */ - /* FALLTHROUGH */ - case '*': /* 0 or more repetitions */ - s = max_expand(ms, s, p, ep); - break; - case '-': /* 0 or more repetitions (minimum) */ - s = min_expand(ms, s, p, ep); - break; - default: /* no suffix */ - s++; p = ep; goto init; /* return match(ms, s + 1, ep); */ - } - } - break; - } - } - } - ms->matchdepth++; - return s; -} - - - -static const char *lmemfind (const char *s1, size_t l1, - const char *s2, size_t l2) { - if (l2 == 0) return s1; /* empty strings are everywhere */ - else if (l2 > l1) return NULL; /* avoids a negative 'l1' */ - else { - const char *init; /* to search for a '*s2' inside 's1' */ - l2--; /* 1st char will be checked by 'memchr' */ - l1 = l1-l2; /* 's2' cannot be found after that */ - while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { - init++; /* 1st char is already checked */ - if (memcmp(init, s2+1, l2) == 0) - return init-1; - else { /* correct 'l1' and 's1' to try again */ - l1 -= init-s1; - s1 = init; - } - } - return NULL; /* not found */ - } -} - - -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { - if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, e - s); /* add whole match */ - else - luaL_error(ms->L, "invalid capture index %%%d", i + 1); - } - else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); - if (l == CAP_POSITION) - lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, l); - } -} - - -static int push_captures (MatchState *ms, const char *s, const char *e) { - int i; - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; - luaL_checkstack(ms->L, nlevels, "too many captures"); - for (i = 0; i < nlevels; i++) - push_onecapture(ms, i, s, e); - return nlevels; /* number of strings pushed */ -} - - -/* check whether pattern has no special characters */ -static int nospecials (const char *p, size_t l) { - size_t upto = 0; - do { - if (strpbrk(p + upto, SPECIALS)) - return 0; /* pattern has a special character */ - upto += strlen(p + upto) + 1; /* may have more after \0 */ - } while (upto <= l); - return 1; /* no special chars found */ -} - - -static void prepstate (MatchState *ms, lua_State *L, - const char *s, size_t ls, const char *p, size_t lp) { - ms->L = L; - ms->matchdepth = MAXCCALLS; - ms->src_init = s; - ms->src_end = s + ls; - ms->p_end = p + lp; -} - - -static void reprepstate (MatchState *ms) { - ms->level = 0; - lua_assert(ms->matchdepth == MAXCCALLS); -} - - -static int str_find_aux (lua_State *L, int find) { - size_t ls, lp; - const char *s = luaL_checklstring(L, 1, &ls); - const char *p = luaL_checklstring(L, 2, &lp); - lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); - if (init < 1) init = 1; - else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ - lua_pushnil(L); /* cannot find anything */ - return 1; - } - /* explicit request or no special characters? */ - if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { - /* do a plain search */ - const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); - if (s2) { - lua_pushinteger(L, (s2 - s) + 1); - lua_pushinteger(L, (s2 - s) + lp); - return 2; - } - } - else { - MatchState ms; - const char *s1 = s + init - 1; - int anchor = (*p == '^'); - if (anchor) { - p++; lp--; /* skip anchor character */ - } - prepstate(&ms, L, s, ls, p, lp); - do { - const char *res; - reprepstate(&ms); - if ((res=match(&ms, s1, p)) != NULL) { - if (find) { - lua_pushinteger(L, (s1 - s) + 1); /* start */ - lua_pushinteger(L, res - s); /* end */ - return push_captures(&ms, NULL, 0) + 2; - } - else - return push_captures(&ms, s1, res); - } - } while (s1++ < ms.src_end && !anchor); - } - lua_pushnil(L); /* not found */ - return 1; -} - - -static int str_find (lua_State *L) { - return str_find_aux(L, 1); -} - - -static int str_match (lua_State *L) { - return str_find_aux(L, 0); -} - - -/* state for 'gmatch' */ -typedef struct GMatchState { - const char *src; /* current position */ - const char *p; /* pattern */ - const char *lastmatch; /* end of last match */ - MatchState ms; /* match state */ -} GMatchState; - - -static int gmatch_aux (lua_State *L) { - GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); - const char *src; - gm->ms.L = L; - for (src = gm->src; src <= gm->ms.src_end; src++) { - const char *e; - reprepstate(&gm->ms); - if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) { - gm->src = gm->lastmatch = e; - return push_captures(&gm->ms, src, e); - } - } - return 0; /* not found */ -} - - -static int gmatch (lua_State *L) { - size_t ls, lp; - const char *s = luaL_checklstring(L, 1, &ls); - const char *p = luaL_checklstring(L, 2, &lp); - GMatchState *gm; - lua_settop(L, 2); /* keep them on closure to avoid being collected */ - gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState)); - prepstate(&gm->ms, L, s, ls, p, lp); - gm->src = s; gm->p = p; gm->lastmatch = NULL; - lua_pushcclosure(L, gmatch_aux, 3); - return 1; -} - - -static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e) { - size_t l, i; - lua_State *L = ms->L; - const char *news = lua_tolstring(L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) - luaL_addchar(b, news[i]); - else { - i++; /* skip ESC */ - if (!isdigit(uchar(news[i]))) { - if (news[i] != L_ESC) - luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); - luaL_addchar(b, news[i]); - } - else if (news[i] == '0') - luaL_addlstring(b, s, e - s); - else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ - lua_remove(L, -2); /* remove original value */ - luaL_addvalue(b); /* add capture to accumulated result */ - } - } - } -} - - -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e, int tr) { - lua_State *L = ms->L; - switch (tr) { - case LUA_TFUNCTION: { - int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); - break; - } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); - lua_gettable(L, 3); - break; - } - default: { /* LUA_TNUMBER or LUA_TSTRING */ - add_s(ms, b, s, e); - return; - } - } - if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, e - s); /* keep original text */ - } - else if (!lua_isstring(L, -1)) - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); - luaL_addvalue(b); /* add result to accumulator */ -} - - -static int str_gsub (lua_State *L) { - size_t srcl, lp; - const char *src = luaL_checklstring(L, 1, &srcl); /* subject */ - const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ - const char *lastmatch = NULL; /* end of last match */ - int tr = lua_type(L, 3); /* replacement type */ - lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ - int anchor = (*p == '^'); - lua_Integer n = 0; /* replacement count */ - MatchState ms; - luaL_Buffer b; - luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || - tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, - "string/function/table expected"); - luaL_buffinit(L, &b); - if (anchor) { - p++; lp--; /* skip anchor character */ - } - prepstate(&ms, L, src, srcl, p, lp); - while (n < max_s) { - const char *e; - reprepstate(&ms); /* (re)prepare state for new match */ - if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ - n++; - add_value(&ms, &b, src, e, tr); /* add replacement to buffer */ - src = lastmatch = e; - } - else if (src < ms.src_end) /* otherwise, skip one character */ - luaL_addchar(&b, *src++); - else break; /* end of subject */ - if (anchor) break; - } - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); - lua_pushinteger(L, n); /* number of substitutions */ - return 2; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** STRING FORMAT -** ======================================================= -*/ - -#if !defined(lua_number2strx) /* { */ - -/* -** Hexadecimal floating-point formatter -*/ - -#include - -#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) - - -/* -** Number of bits that goes into the first digit. It can be any value -** between 1 and 4; the following definition tries to align the number -** to nibble boundaries by making what is left after that first digit a -** multiple of 4. -*/ -#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1) - - -/* -** Add integer part of 'x' to buffer and return new 'x' -*/ -static lua_Number adddigit (char *buff, int n, lua_Number x) { - lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ - int d = (int)dd; - buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ - return x - dd; /* return what is left */ -} - - -static int num2straux (char *buff, int sz, lua_Number x) { - /* if 'inf' or 'NaN', format it like '%g' */ - if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) - return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); - else if (x == 0) { /* can be -0... */ - /* create "0" or "-0" followed by exponent */ - return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x); - } - else { - int e; - lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ - int n = 0; /* character count */ - if (m < 0) { /* is number negative? */ - buff[n++] = '-'; /* add signal */ - m = -m; /* make it positive */ - } - buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ - m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */ - e -= L_NBFD; /* this digit goes before the radix point */ - if (m > 0) { /* more digits? */ - buff[n++] = lua_getlocaledecpoint(); /* add radix point */ - do { /* add as many digits as needed */ - m = adddigit(buff, n++, m * 16); - } while (m > 0); - } - n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ - lua_assert(n < sz); - return n; - } -} - - -static int lua_number2strx (lua_State *L, char *buff, int sz, - const char *fmt, lua_Number x) { - int n = num2straux(buff, sz, x); - if (fmt[SIZELENMOD] == 'A') { - int i; - for (i = 0; i < n; i++) - buff[i] = toupper(uchar(buff[i])); - } - else if (fmt[SIZELENMOD] != 'a') - return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); - return n; -} - -#endif /* } */ - - -/* -** Maximum size of each formatted item. This maximum size is produced -** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.', -** and '\0') + number of decimal digits to represent maxfloat (which -** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra -** expenses", such as locale-dependent stuff) -*/ -#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) - - -/* valid flags in a format specification */ -#define FLAGS "-+ #0" - -/* -** maximum size of each format specification (such as "%-099.99d") -*/ -#define MAX_FORMAT 32 - - -static void addquoted (luaL_Buffer *b, const char *s, size_t len) { - luaL_addchar(b, '"'); - while (len--) { - if (*s == '"' || *s == '\\' || *s == '\n') { - luaL_addchar(b, '\\'); - luaL_addchar(b, *s); - } - else if (iscntrl(uchar(*s))) { - char buff[10]; - if (!isdigit(uchar(*(s+1)))) - l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); - else - l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); - luaL_addstring(b, buff); - } - else - luaL_addchar(b, *s); - s++; - } - luaL_addchar(b, '"'); -} - - -/* -** Ensures the 'buff' string uses a dot as the radix character. -*/ -static void checkdp (char *buff, int nb) { - if (memchr(buff, '.', nb) == NULL) { /* no dot? */ - char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = (char *)memchr(buff, point, nb); - if (ppoint) *ppoint = '.'; /* change it to a dot */ - } -} - - -static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { - switch (lua_type(L, arg)) { - case LUA_TSTRING: { - size_t len; - const char *s = lua_tolstring(L, arg, &len); - addquoted(b, s, len); - break; - } - case LUA_TNUMBER: { - char *buff = luaL_prepbuffsize(b, MAX_ITEM); - int nb; - if (!lua_isinteger(L, arg)) { /* float? */ - lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */ - nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); - checkdp(buff, nb); /* ensure it uses a dot */ - } - else { /* integers */ - lua_Integer n = lua_tointeger(L, arg); - const char *format = (n == LUA_MININTEGER) /* corner case? */ - ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ - : LUA_INTEGER_FMT; /* else use default format */ - nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); - } - luaL_addsize(b, nb); - break; - } - case LUA_TNIL: case LUA_TBOOLEAN: { - luaL_tolstring(L, arg, NULL); - luaL_addvalue(b); - break; - } - default: { - luaL_argerror(L, arg, "value has no literal form"); - } - } -} - - -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { - const char *p = strfrmt; - while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) p++; /* skip precision */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); - *(form++) = '%'; - memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char)); - form += (p - strfrmt) + 1; - *form = '\0'; - return p; -} - - -/* -** add length modifier into formats -*/ -static void addlenmod (char *form, const char *lenmod) { - size_t l = strlen(form); - size_t lm = strlen(lenmod); - char spec = form[l - 1]; - strcpy(form + l - 1, lenmod); - form[l + lm - 1] = spec; - form[l + lm] = '\0'; -} - - -static int str_format (lua_State *L) { - int top = lua_gettop(L); - int arg = 1; - size_t sfl; - const char *strfrmt = luaL_checklstring(L, arg, &sfl); - const char *strfrmt_end = strfrmt+sfl; - luaL_Buffer b; - luaL_buffinit(L, &b); - while (strfrmt < strfrmt_end) { - if (*strfrmt != L_ESC) - luaL_addchar(&b, *strfrmt++); - else if (*++strfrmt == L_ESC) - luaL_addchar(&b, *strfrmt++); /* %% */ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format ('%...') */ - char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ - int nb = 0; /* number of bytes in added item */ - if (++arg > top) - luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { - nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); - break; - } - case 'd': case 'i': - case 'o': case 'u': case 'x': case 'X': { - lua_Integer n = luaL_checkinteger(L, arg); - addlenmod(form, LUA_INTEGER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n); - break; - } - case 'a': case 'A': - addlenmod(form, LUA_NUMBER_FRMLEN); - nb = lua_number2strx(L, buff, MAX_ITEM, form, - luaL_checknumber(L, arg)); - break; - case 'e': case 'E': case 'f': - case 'g': case 'G': { - lua_Number n = luaL_checknumber(L, arg); - addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); - break; - } - case 'q': { - addliteral(L, &b, arg); - break; - } - case 's': { - size_t l; - const char *s = luaL_tolstring(L, arg, &l); - if (form[2] == '\0') /* no modifiers? */ - luaL_addvalue(&b); /* keep entire string */ - else { - luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); - if (!strchr(form, '.') && l >= 100) { - /* no precision and string is too long to be formatted */ - luaL_addvalue(&b); /* keep entire string */ - } - else { /* format the string into 'buff' */ - nb = l_sprintf(buff, MAX_ITEM, form, s); - lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ - } - } - break; - } - default: { /* also treat cases 'pnLlh' */ - return luaL_error(L, "invalid option '%%%c' to 'format'", - *(strfrmt - 1)); - } - } - lua_assert(nb < MAX_ITEM); - luaL_addsize(&b, nb); - } - } - luaL_pushresult(&b); - return 1; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** PACK/UNPACK -** ======================================================= -*/ - - -/* value used for padding */ -#if !defined(LUAL_PACKPADBYTE) -#define LUAL_PACKPADBYTE 0x00 -#endif - -/* maximum size for the binary representation of an integer */ -#define MAXINTSIZE 16 - -/* number of bits in a character */ -#define NB CHAR_BIT - -/* mask for one character (NB 1's) */ -#define MC ((1 << NB) - 1) - -/* size of a lua_Integer */ -#define SZINT ((int)sizeof(lua_Integer)) - - -/* dummy union to get native endianness */ -static const union { - int dummy; - char little; /* true iff machine is little endian */ -} nativeendian = {1}; - - -/* dummy structure to get native alignment requirements */ -struct cD { - char c; - union { double d; void *p; lua_Integer i; lua_Number n; } u; -}; - -#define MAXALIGN (offsetof(struct cD, u)) - - -/* -** Union for serializing floats -*/ -typedef union Ftypes { - float f; - double d; - lua_Number n; - char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ -} Ftypes; - - -/* -** information to pack/unpack stuff -*/ -typedef struct Header { - lua_State *L; - int islittle; - int maxalign; -} Header; - - -/* -** options for pack/unpack -*/ -typedef enum KOption { - Kint, /* signed integers */ - Kuint, /* unsigned integers */ - Kfloat, /* floating-point numbers */ - Kchar, /* fixed-length strings */ - Kstring, /* strings with prefixed length */ - Kzstr, /* zero-terminated strings */ - Kpadding, /* padding */ - Kpaddalign, /* padding for alignment */ - Knop /* no-op (configuration or spaces) */ -} KOption; - - -/* -** Read an integer numeral from string 'fmt' or return 'df' if -** there is no numeral -*/ -static int digit (int c) { return '0' <= c && c <= '9'; } - -static int getnum (const char **fmt, int df) { - if (!digit(**fmt)) /* no number? */ - return df; /* return default value */ - else { - int a = 0; - do { - a = a*10 + (*((*fmt)++) - '0'); - } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10); - return a; - } -} - - -/* -** Read an integer numeral and raises an error if it is larger -** than the maximum size for integers. -*/ -static int getnumlimit (Header *h, const char **fmt, int df) { - int sz = getnum(fmt, df); - if (sz > MAXINTSIZE || sz <= 0) - return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); - return sz; -} - - -/* -** Initialize Header -*/ -static void initheader (lua_State *L, Header *h) { - h->L = L; - h->islittle = nativeendian.little; - h->maxalign = 1; -} - - -/* -** Read and classify next option. 'size' is filled with option's size. -*/ -static KOption getoption (Header *h, const char **fmt, int *size) { - int opt = *((*fmt)++); - *size = 0; /* default */ - switch (opt) { - case 'b': *size = sizeof(char); return Kint; - case 'B': *size = sizeof(char); return Kuint; - case 'h': *size = sizeof(short); return Kint; - case 'H': *size = sizeof(short); return Kuint; - case 'l': *size = sizeof(long); return Kint; - case 'L': *size = sizeof(long); return Kuint; - case 'j': *size = sizeof(lua_Integer); return Kint; - case 'J': *size = sizeof(lua_Integer); return Kuint; - case 'T': *size = sizeof(size_t); return Kuint; - case 'f': *size = sizeof(float); return Kfloat; - case 'd': *size = sizeof(double); return Kfloat; - case 'n': *size = sizeof(lua_Number); return Kfloat; - case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; - case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; - case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; - case 'c': - *size = getnum(fmt, -1); - if (*size == -1) - luaL_error(h->L, "missing size for format option 'c'"); - return Kchar; - case 'z': return Kzstr; - case 'x': *size = 1; return Kpadding; - case 'X': return Kpaddalign; - case ' ': break; - case '<': h->islittle = 1; break; - case '>': h->islittle = 0; break; - case '=': h->islittle = nativeendian.little; break; - case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; - default: luaL_error(h->L, "invalid format option '%c'", opt); - } - return Knop; -} - - -/* -** Read, classify, and fill other details about the next option. -** 'psize' is filled with option's size, 'notoalign' with its -** alignment requirements. -** Local variable 'size' gets the size to be aligned. (Kpadal option -** always gets its full alignment, other options are limited by -** the maximum alignment ('maxalign'). Kchar option needs no alignment -** despite its size. -*/ -static KOption getdetails (Header *h, size_t totalsize, - const char **fmt, int *psize, int *ntoalign) { - KOption opt = getoption(h, fmt, psize); - int align = *psize; /* usually, alignment follows size */ - if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ - if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) - luaL_argerror(h->L, 1, "invalid next option for option 'X'"); - } - if (align <= 1 || opt == Kchar) /* need no alignment? */ - *ntoalign = 0; - else { - if (align > h->maxalign) /* enforce maximum alignment */ - align = h->maxalign; - if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ - luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); - *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); - } - return opt; -} - - -/* -** Pack integer 'n' with 'size' bytes and 'islittle' endianness. -** The final 'if' handles the case when 'size' is larger than -** the size of a Lua integer, correcting the extra sign-extension -** bytes if necessary (by default they would be zeros). -*/ -static void packint (luaL_Buffer *b, lua_Unsigned n, - int islittle, int size, int neg) { - char *buff = luaL_prepbuffsize(b, size); - int i; - buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ - for (i = 1; i < size; i++) { - n >>= NB; - buff[islittle ? i : size - 1 - i] = (char)(n & MC); - } - if (neg && size > SZINT) { /* negative number need sign extension? */ - for (i = SZINT; i < size; i++) /* correct extra bytes */ - buff[islittle ? i : size - 1 - i] = (char)MC; - } - luaL_addsize(b, size); /* add result to buffer */ -} - - -/* -** Copy 'size' bytes from 'src' to 'dest', correcting endianness if -** given 'islittle' is different from native endianness. -*/ -static void copywithendian (volatile char *dest, volatile const char *src, - int size, int islittle) { - if (islittle == nativeendian.little) { - while (size-- != 0) - *(dest++) = *(src++); - } - else { - dest += size - 1; - while (size-- != 0) - *(dest--) = *(src++); - } -} - - -static int str_pack (lua_State *L) { - luaL_Buffer b; - Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - int arg = 1; /* current argument to pack */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader(L, &h); - lua_pushnil(L); /* mark to separate arguments from string buffer */ - luaL_buffinit(L, &b); - while (*fmt != '\0') { - int size, ntoalign; - KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); - totalsize += ntoalign + size; - while (ntoalign-- > 0) - luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ - arg++; - switch (opt) { - case Kint: { /* signed integers */ - lua_Integer n = luaL_checkinteger(L, arg); - if (size < SZINT) { /* need overflow check? */ - lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); - luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); - } - packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); - break; - } - case Kuint: { /* unsigned integers */ - lua_Integer n = luaL_checkinteger(L, arg); - if (size < SZINT) /* need overflow check? */ - luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), - arg, "unsigned overflow"); - packint(&b, (lua_Unsigned)n, h.islittle, size, 0); - break; - } - case Kfloat: { /* floating-point options */ - volatile Ftypes u; - char *buff = luaL_prepbuffsize(&b, size); - lua_Number n = luaL_checknumber(L, arg); /* get argument */ - if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ - else if (size == sizeof(u.d)) u.d = (double)n; - else u.n = n; - /* move 'u' to final result, correcting endianness if needed */ - copywithendian(buff, u.buff, size, h.islittle); - luaL_addsize(&b, size); - break; - } - case Kchar: { /* fixed-size string */ - size_t len; - const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, len <= (size_t)size, arg, - "string longer than given size"); - luaL_addlstring(&b, s, len); /* add string */ - while (len++ < (size_t)size) /* pad extra space */ - luaL_addchar(&b, LUAL_PACKPADBYTE); - break; - } - case Kstring: { /* strings with length count */ - size_t len; - const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, size >= (int)sizeof(size_t) || - len < ((size_t)1 << (size * NB)), - arg, "string length does not fit in given size"); - packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ - luaL_addlstring(&b, s, len); - totalsize += len; - break; - } - case Kzstr: { /* zero-terminated string */ - size_t len; - const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); - luaL_addlstring(&b, s, len); - luaL_addchar(&b, '\0'); /* add zero at the end */ - totalsize += len + 1; - break; - } - case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */ - case Kpaddalign: case Knop: - arg--; /* undo increment */ - break; - } - } - luaL_pushresult(&b); - return 1; -} - - -static int str_packsize (lua_State *L) { - Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader(L, &h); - while (*fmt != '\0') { - int size, ntoalign; - KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); - size += ntoalign; /* total space used by option */ - luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, - "format result too large"); - totalsize += size; - switch (opt) { - case Kstring: /* strings with length count */ - case Kzstr: /* zero-terminated string */ - luaL_argerror(L, 1, "variable-length format"); - /* call never return, but to avoid warnings: *//* FALLTHROUGH */ - default: break; - } - } - lua_pushinteger(L, (lua_Integer)totalsize); - return 1; -} - - -/* -** Unpack an integer with 'size' bytes and 'islittle' endianness. -** If size is smaller than the size of a Lua integer and integer -** is signed, must do sign extension (propagating the sign to the -** higher bits); if size is larger than the size of a Lua integer, -** it must check the unread bytes to see whether they do not cause an -** overflow. -*/ -static lua_Integer unpackint (lua_State *L, const char *str, - int islittle, int size, int issigned) { - lua_Unsigned res = 0; - int i; - int limit = (size <= SZINT) ? size : SZINT; - for (i = limit - 1; i >= 0; i--) { - res <<= NB; - res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; - } - if (size < SZINT) { /* real size smaller than lua_Integer? */ - if (issigned) { /* needs sign extension? */ - lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); - res = ((res ^ mask) - mask); /* do sign extension */ - } - } - else if (size > SZINT) { /* must check unread bytes */ - int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; - for (i = limit; i < size; i++) { - if ((unsigned char)str[islittle ? i : size - 1 - i] != mask) - luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); - } - } - return (lua_Integer)res; -} - - -static int str_unpack (lua_State *L) { - Header h; - const char *fmt = luaL_checkstring(L, 1); - size_t ld; - const char *data = luaL_checklstring(L, 2, &ld); - size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1; - int n = 0; /* number of results */ - luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); - initheader(L, &h); - while (*fmt != '\0') { - int size, ntoalign; - KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); - if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld) - luaL_argerror(L, 2, "data string too short"); - pos += ntoalign; /* skip alignment */ - /* stack space for item + next position */ - luaL_checkstack(L, 2, "too many results"); - n++; - switch (opt) { - case Kint: - case Kuint: { - lua_Integer res = unpackint(L, data + pos, h.islittle, size, - (opt == Kint)); - lua_pushinteger(L, res); - break; - } - case Kfloat: { - volatile Ftypes u; - lua_Number num; - copywithendian(u.buff, data + pos, size, h.islittle); - if (size == sizeof(u.f)) num = (lua_Number)u.f; - else if (size == sizeof(u.d)) num = (lua_Number)u.d; - else num = u.n; - lua_pushnumber(L, num); - break; - } - case Kchar: { - lua_pushlstring(L, data + pos, size); - break; - } - case Kstring: { - size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); - luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); - lua_pushlstring(L, data + pos + size, len); - pos += len; /* skip string */ - break; - } - case Kzstr: { - size_t len = (int)strlen(data + pos); - lua_pushlstring(L, data + pos, len); - pos += len + 1; /* skip string plus final '\0' */ - break; - } - case Kpaddalign: case Kpadding: case Knop: - n--; /* undo increment */ - break; - } - pos += size; - } - lua_pushinteger(L, pos + 1); /* next position */ - return n + 1; -} - -/* }====================================================== */ - - -static const luaL_Reg strlib[] = { - {"byte", str_byte}, - {"char", str_char}, - {"dump", str_dump}, - {"find", str_find}, - {"format", str_format}, - {"gmatch", gmatch}, - {"gsub", str_gsub}, - {"len", str_len}, - {"lower", str_lower}, - {"match", str_match}, - {"rep", str_rep}, - {"reverse", str_reverse}, - {"sub", str_sub}, - {"upper", str_upper}, - {"pack", str_pack}, - {"packsize", str_packsize}, - {"unpack", str_unpack}, - {NULL, NULL} -}; - - -static void createmetatable (lua_State *L) { - lua_createtable(L, 0, 1); /* table to be metatable for strings */ - lua_pushliteral(L, ""); /* dummy string */ - lua_pushvalue(L, -2); /* copy table */ - lua_setmetatable(L, -2); /* set table as metatable for strings */ - lua_pop(L, 1); /* pop dummy string */ - lua_pushvalue(L, -2); /* get string library */ - lua_setfield(L, -2, "__index"); /* metatable.__index = string */ - lua_pop(L, 1); /* pop metatable */ -} - - -/* -** Open string library -*/ -LUAMOD_API int luaopen_string (lua_State *L) { - luaL_newlib(L, strlib); - createmetatable(L); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.c deleted file mode 100644 index ea4fe7f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.c +++ /dev/null @@ -1,688 +0,0 @@ -/* -** $Id: ltable.c,v 2.118.1.4 2018/06/08 16:22:51 roberto Exp $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - -#define ltable_c -#define LUA_CORE - -#include "lprefix.h" - - -/* -** Implementation of tables (aka arrays, objects, or hash tables). -** Tables keep its elements in two parts: an array part and a hash part. -** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest 'n' such that -** more than half the slots between 1 and n are in use. -** Hash uses a mix of chained scatter table with Brent's variation. -** A main invariant of these tables is that, if an element is not -** in its main position (i.e. the 'original' position that its hash gives -** to it), then the colliding element is in its own main position. -** Hence even when the load factor reaches 100%, performance remains good. -*/ - -#include -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lvm.h" - - -/* -** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is -** the largest integer such that MAXASIZE fits in an unsigned int. -*/ -#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) -#define MAXASIZE (1u << MAXABITS) - -/* -** Maximum size of hash part is 2^MAXHBITS. MAXHBITS is the largest -** integer such that 2^MAXHBITS fits in a signed int. (Note that the -** maximum number of elements in a table, 2^MAXABITS + 2^MAXHBITS, still -** fits comfortably in an unsigned int.) -*/ -#define MAXHBITS (MAXABITS - 1) - - -#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) - -#define hashstr(t,str) hashpow2(t, (str)->hash) -#define hashboolean(t,p) hashpow2(t, p) -#define hashint(t,i) hashpow2(t, i) - - -/* -** for some types, it is better to avoid modulus by power of 2, as -** they tend to have many 2 factors. -*/ -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) - - -#define hashpointer(t,p) hashmod(t, point2uint(p)) - - -#define dummynode (&dummynode_) - -static const Node dummynode_ = { - {NILCONSTANT}, /* value */ - {{NILCONSTANT, 0}} /* key */ -}; - - -/* -** Hash for floating-point numbers. -** The main computation should be just -** n = frexp(n, &i); return (n * INT_MAX) + i -** but there are some numerical subtleties. -** In a two-complement representation, INT_MAX does not has an exact -** representation as a float, but INT_MIN does; because the absolute -** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the -** absolute value of the product 'frexp * -INT_MIN' is smaller or equal -** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when -** adding 'i'; the use of '~u' (instead of '-u') avoids problems with -** INT_MIN. -*/ -#if !defined(l_hashfloat) -static int l_hashfloat (lua_Number n) { - int i; - lua_Integer ni; - n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); - if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ - lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); - return 0; - } - else { /* normal case */ - unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni); - return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u); - } -} -#endif - - -/* -** returns the 'main' position of an element in a table (that is, the index -** of its hash value) -*/ -static Node *mainposition (const Table *t, const TValue *key) { - switch (ttype(key)) { - case LUA_TNUMINT: - return hashint(t, ivalue(key)); - case LUA_TNUMFLT: - return hashmod(t, l_hashfloat(fltvalue(key))); - case LUA_TSHRSTR: - return hashstr(t, tsvalue(key)); - case LUA_TLNGSTR: - return hashpow2(t, luaS_hashlongstr(tsvalue(key))); - case LUA_TBOOLEAN: - return hashboolean(t, bvalue(key)); - case LUA_TLIGHTUSERDATA: - return hashpointer(t, pvalue(key)); - case LUA_TLCF: - return hashpointer(t, fvalue(key)); - default: - lua_assert(!ttisdeadkey(key)); - return hashpointer(t, gcvalue(key)); - } -} - - -/* -** returns the index for 'key' if 'key' is an appropriate key to live in -** the array part of the table, 0 otherwise. -*/ -static unsigned int arrayindex (const TValue *key) { - if (ttisinteger(key)) { - lua_Integer k = ivalue(key); - if (0 < k && (lua_Unsigned)k <= MAXASIZE) - return cast(unsigned int, k); /* 'key' is an appropriate array index */ - } - return 0; /* 'key' did not match some condition */ -} - - -/* -** returns the index of a 'key' for table traversals. First goes all -** elements in the array part, then elements in the hash part. The -** beginning of a traversal is signaled by 0. -*/ -static unsigned int findindex (lua_State *L, Table *t, StkId key) { - unsigned int i; - if (ttisnil(key)) return 0; /* first iteration */ - i = arrayindex(key); - if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ - return i; /* yes; that's the index */ - else { - int nx; - Node *n = mainposition(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - /* key may be dead already, but it is ok to use it in 'next' */ - if (luaV_rawequalobj(gkey(n), key) || - (ttisdeadkey(gkey(n)) && iscollectable(key) && - deadvalue(gkey(n)) == gcvalue(key))) { - i = cast_int(n - gnode(t, 0)); /* key index in hash table */ - /* hash elements are numbered after array ones */ - return (i + 1) + t->sizearray; - } - nx = gnext(n); - if (nx == 0) - luaG_runerror(L, "invalid key to 'next'"); /* key not found */ - else n += nx; - } - } -} - - -int luaH_next (lua_State *L, Table *t, StkId key) { - unsigned int i = findindex(L, t, key); /* find original element */ - for (; i < t->sizearray; i++) { /* try first array part */ - if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setivalue(key, i + 1); - setobj2s(L, key+1, &t->array[i]); - return 1; - } - } - for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ - if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ - setobj2s(L, key, gkey(gnode(t, i))); - setobj2s(L, key+1, gval(gnode(t, i))); - return 1; - } - } - return 0; /* no more elements */ -} - - -/* -** {============================================================= -** Rehash -** ============================================================== -*/ - -/* -** Compute the optimal size for the array part of table 't'. 'nums' is a -** "count array" where 'nums[i]' is the number of integers in the table -** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of -** integer keys in the table and leaves with the number of keys that -** will go to the array part; return the optimal size. -*/ -static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { - int i; - unsigned int twotoi; /* 2^i (candidate for optimal size) */ - unsigned int a = 0; /* number of elements smaller than 2^i */ - unsigned int na = 0; /* number of elements to go to array part */ - unsigned int optimal = 0; /* optimal size for array part */ - /* loop while keys can fill more than half of total size */ - for (i = 0, twotoi = 1; - twotoi > 0 && *pna > twotoi / 2; - i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ - optimal = twotoi; /* optimal size (till now) */ - na = a; /* all elements up to 'optimal' will go to array part */ - } - } - } - lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); - *pna = na; - return optimal; -} - - -static int countint (const TValue *key, unsigned int *nums) { - unsigned int k = arrayindex(key); - if (k != 0) { /* is 'key' an appropriate array index? */ - nums[luaO_ceillog2(k)]++; /* count as such */ - return 1; - } - else - return 0; -} - - -/* -** Count keys in array part of table 't': Fill 'nums[i]' with -** number of keys that will go into corresponding slice and return -** total number of non-nil keys. -*/ -static unsigned int numusearray (const Table *t, unsigned int *nums) { - int lg; - unsigned int ttlg; /* 2^lg */ - unsigned int ause = 0; /* summation of 'nums' */ - unsigned int i = 1; /* count to traverse all array keys */ - /* traverse each slice */ - for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { - unsigned int lc = 0; /* counter */ - unsigned int lim = ttlg; - if (lim > t->sizearray) { - lim = t->sizearray; /* adjust upper limit */ - if (i > lim) - break; /* no more elements to count */ - } - /* count elements in range (2^(lg - 1), 2^lg] */ - for (; i <= lim; i++) { - if (!ttisnil(&t->array[i-1])) - lc++; - } - nums[lg] += lc; - ause += lc; - } - return ause; -} - - -static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { - int totaluse = 0; /* total number of elements */ - int ause = 0; /* elements added to 'nums' (can go to array part) */ - int i = sizenode(t); - while (i--) { - Node *n = &t->node[i]; - if (!ttisnil(gval(n))) { - ause += countint(gkey(n), nums); - totaluse++; - } - } - *pna += ause; - return totaluse; -} - - -static void setarrayvector (lua_State *L, Table *t, unsigned int size) { - unsigned int i; - luaM_reallocvector(L, t->array, t->sizearray, size, TValue); - for (i=t->sizearray; iarray[i]); - t->sizearray = size; -} - - -static void setnodevector (lua_State *L, Table *t, unsigned int size) { - if (size == 0) { /* no elements to hash part? */ - t->node = cast(Node *, dummynode); /* use common 'dummynode' */ - t->lsizenode = 0; - t->lastfree = NULL; /* signal that it is using dummy node */ - } - else { - int i; - int lsize = luaO_ceillog2(size); - if (lsize > MAXHBITS) - luaG_runerror(L, "table overflow"); - size = twoto(lsize); - t->node = luaM_newvector(L, size, Node); - for (i = 0; i < (int)size; i++) { - Node *n = gnode(t, i); - gnext(n) = 0; - setnilvalue(wgkey(n)); - setnilvalue(gval(n)); - } - t->lsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ - } -} - - -typedef struct { - Table *t; - unsigned int nhsize; -} AuxsetnodeT; - - -static void auxsetnode (lua_State *L, void *ud) { - AuxsetnodeT *asn = cast(AuxsetnodeT *, ud); - setnodevector(L, asn->t, asn->nhsize); -} - - -void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize) { - unsigned int i; - int j; - AuxsetnodeT asn; - unsigned int oldasize = t->sizearray; - int oldhsize = allocsizenode(t); - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); - /* create new hash part with appropriate size */ - asn.t = t; asn.nhsize = nhsize; - if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ - setarrayvector(L, t, oldasize); /* array back to its original size */ - luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ - } - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ - for (i=nasize; iarray[i])) - luaH_setint(L, t, i + 1, &t->array[i]); - } - /* shrink array */ - luaM_reallocvector(L, t->array, oldasize, nasize, TValue); - } - /* re-insert elements from hash part */ - for (j = oldhsize - 1; j >= 0; j--) { - Node *old = nold + j; - if (!ttisnil(gval(old))) { - /* doesn't need barrier/invalidate cache, as entry was - already present in the table */ - setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); - } - } - if (oldhsize > 0) /* not the dummy node? */ - luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */ -} - - -void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { - int nsize = allocsizenode(t); - luaH_resize(L, t, nasize, nsize); -} - -/* -** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i -*/ -static void rehash (lua_State *L, Table *t, const TValue *ek) { - unsigned int asize; /* optimal size for array part */ - unsigned int na; /* number of keys in the array part */ - unsigned int nums[MAXABITS + 1]; - int i; - int totaluse; - for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ - na = numusearray(t, nums); /* count keys in array part */ - totaluse = na; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &na); /* count keys in hash part */ - /* count extra key */ - na += countint(ek, nums); - totaluse++; - /* compute new size for array part */ - asize = computesizes(nums, &na); - /* resize the table to new computed sizes */ - luaH_resize(L, t, asize, totaluse - na); -} - - - -/* -** }============================================================= -*/ - - -Table *luaH_new (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); - Table *t = gco2t(o); - t->metatable = NULL; - t->flags = cast_byte(~0); - t->array = NULL; - t->sizearray = 0; - setnodevector(L, t, 0); - return t; -} - - -void luaH_free (lua_State *L, Table *t) { - if (!isdummy(t)) - luaM_freearray(L, t->node, cast(size_t, sizenode(t))); - luaM_freearray(L, t->array, t->sizearray); - luaM_free(L, t); -} - - -static Node *getfreepos (Table *t) { - if (!isdummy(t)) { - while (t->lastfree > t->node) { - t->lastfree--; - if (ttisnil(gkey(t->lastfree))) - return t->lastfree; - } - } - return NULL; /* could not find a free place */ -} - - - -/* -** inserts a new key into a hash table; first, check whether key's main -** position is free. If not, check whether colliding node is in its main -** position or not: if it is not, move colliding node to an empty place and -** put new key in its main position; otherwise (colliding node is in its main -** position), new key goes to an empty position. -*/ -TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { - Node *mp; - TValue aux; - if (ttisnil(key)) luaG_runerror(L, "table index is nil"); - else if (ttisfloat(key)) { - lua_Integer k; - if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */ - setivalue(&aux, k); - key = &aux; /* insert it as an integer */ - } - else if (luai_numisnan(fltvalue(key))) - luaG_runerror(L, "table index is NaN"); - } - mp = mainposition(t, key); - if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */ - Node *othern; - Node *f = getfreepos(t); /* get a free place */ - if (f == NULL) { /* cannot find a free place? */ - rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' takes care of TM cache */ - return luaH_set(L, t, key); /* insert key into grown table */ - } - lua_assert(!isdummy(t)); - othern = mainposition(t, gkey(mp)); - if (othern != mp) { /* is colliding node out of its main position? */ - /* yes; move colliding node into free position */ - while (othern + gnext(othern) != mp) /* find previous */ - othern += gnext(othern); - gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ - *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ - if (gnext(mp) != 0) { - gnext(f) += cast_int(mp - f); /* correct 'next' */ - gnext(mp) = 0; /* now 'mp' is free */ - } - setnilvalue(gval(mp)); - } - else { /* colliding node is in its own main position */ - /* new node will go into free position */ - if (gnext(mp) != 0) - gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ - else lua_assert(gnext(f) == 0); - gnext(mp) = cast_int(f - mp); - mp = f; - } - } - setnodekey(L, &mp->i_key, key); - luaC_barrierback(L, t, key); - lua_assert(ttisnil(gval(mp))); - return gval(mp); -} - - -/* -** search function for integers -*/ -const TValue *luaH_getint (Table *t, lua_Integer key) { - /* (1 <= key && key <= t->sizearray) */ - if (l_castS2U(key) - 1 < t->sizearray) - return &t->array[key - 1]; - else { - Node *n = hashint(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) break; - n += nx; - } - } - return luaO_nilobject; - } -} - - -/* -** search function for short strings -*/ -const TValue *luaH_getshortstr (Table *t, TString *key) { - Node *n = hashstr(t, key); - lua_assert(key->tt == LUA_TSHRSTR); - for (;;) { /* check whether 'key' is somewhere in the chain */ - const TValue *k = gkey(n); - if (ttisshrstring(k) && eqshrstr(tsvalue(k), key)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) - return luaO_nilobject; /* not found */ - n += nx; - } - } -} - - -/* -** "Generic" get version. (Not that generic: not valid for integers, -** which may be in array part, nor for floats with integral values.) -*/ -static const TValue *getgeneric (Table *t, const TValue *key) { - Node *n = mainposition(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (luaV_rawequalobj(gkey(n), key)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) - return luaO_nilobject; /* not found */ - n += nx; - } - } -} - - -const TValue *luaH_getstr (Table *t, TString *key) { - if (key->tt == LUA_TSHRSTR) - return luaH_getshortstr(t, key); - else { /* for long strings, use generic case */ - TValue ko; - setsvalue(cast(lua_State *, NULL), &ko, key); - return getgeneric(t, &ko); - } -} - - -/* -** main search function -*/ -const TValue *luaH_get (Table *t, const TValue *key) { - switch (ttype(key)) { - case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); - case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); - case LUA_TNIL: return luaO_nilobject; - case LUA_TNUMFLT: { - lua_Integer k; - if (luaV_tointeger(key, &k, 0)) /* index is int? */ - return luaH_getint(t, k); /* use specialized version */ - /* else... */ - } /* FALLTHROUGH */ - default: - return getgeneric(t, key); - } -} - - -/* -** beware: when using this function you probably need to check a GC -** barrier and invalidate the TM cache. -*/ -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p = luaH_get(t, key); - if (p != luaO_nilobject) - return cast(TValue *, p); - else return luaH_newkey(L, t, key); -} - - -void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - const TValue *p = luaH_getint(t, key); - TValue *cell; - if (p != luaO_nilobject) - cell = cast(TValue *, p); - else { - TValue k; - setivalue(&k, key); - cell = luaH_newkey(L, t, &k); - } - setobj2t(L, cell, value); -} - - -static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { - lua_Unsigned i = j; /* i is zero or a present index */ - j++; - /* find 'i' and 'j' such that i is present and j is not */ - while (!ttisnil(luaH_getint(t, j))) { - i = j; - if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ - i = 1; - while (!ttisnil(luaH_getint(t, i))) i++; - return i - 1; - } - j *= 2; - } - /* now do a binary search between them */ - while (j - i > 1) { - lua_Unsigned m = (i+j)/2; - if (ttisnil(luaH_getint(t, m))) j = m; - else i = m; - } - return i; -} - - -/* -** Try to find a boundary in table 't'. A 'boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). -*/ -lua_Unsigned luaH_getn (Table *t) { - unsigned int j = t->sizearray; - if (j > 0 && ttisnil(&t->array[j - 1])) { - /* there is a boundary in the array part: (binary) search for it */ - unsigned int i = 0; - while (j - i > 1) { - unsigned int m = (i+j)/2; - if (ttisnil(&t->array[m - 1])) j = m; - else i = m; - } - return i; - } - /* else must find a boundary in hash part */ - else if (isdummy(t)) /* hash part is empty? */ - return j; /* that is easy... */ - else return unbound_search(t, j); -} - - - -#if defined(LUA_DEBUG) - -Node *luaH_mainposition (const Table *t, const TValue *key) { - return mainposition(t, key); -} - -int luaH_isdummy (const Table *t) { return isdummy(t); } - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.h deleted file mode 100644 index 92db0ac..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltable.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - -#ifndef ltable_h -#define ltable_h - -#include "lobject.h" - - -#define gnode(t,i) (&(t)->node[i]) -#define gval(n) (&(n)->i_val) -#define gnext(n) ((n)->i_key.nk.next) - - -/* 'const' to avoid wrong writings that can mess up field 'next' */ -#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) - -/* -** writable version of 'gkey'; allows updates to individual fields, -** but not to the whole (which has incompatible type) -*/ -#define wgkey(n) (&(n)->i_key.nk) - -#define invalidateTMcache(t) ((t)->flags = 0) - - -/* true when 't' is using 'dummynode' as its hash part */ -#define isdummy(t) ((t)->lastfree == NULL) - - -/* allocated size for hash nodes */ -#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) - - -/* returns the key, given the value of a table entry */ -#define keyfromval(v) \ - (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) - - -LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); -LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, - TValue *value); -LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); -LUAI_FUNC void luaH_free (lua_State *L, Table *t); -LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC lua_Unsigned luaH_getn (Table *t); - - -#if defined(LUA_DEBUG) -LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (const Table *t); -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltablib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltablib.c deleted file mode 100644 index c534957..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltablib.c +++ /dev/null @@ -1,450 +0,0 @@ -/* -** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $ -** Library for Table Manipulation -** See Copyright Notice in lua.h -*/ - -#define ltablib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** Operations that an object must define to mimic a table -** (some functions only need some of them) -*/ -#define TAB_R 1 /* read */ -#define TAB_W 2 /* write */ -#define TAB_L 4 /* length */ -#define TAB_RW (TAB_R | TAB_W) /* read/write */ - - -#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) - - -static int checkfield (lua_State *L, const char *key, int n) { - lua_pushstring(L, key); - return (lua_rawget(L, -n) != LUA_TNIL); -} - - -/* -** Check that 'arg' either is a table or can behave like one (that is, -** has a metatable with the required metamethods) -*/ -static void checktab (lua_State *L, int arg, int what) { - if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ - int n = 1; /* number of elements to pop */ - if (lua_getmetatable(L, arg) && /* must have metatable */ - (!(what & TAB_R) || checkfield(L, "__index", ++n)) && - (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && - (!(what & TAB_L) || checkfield(L, "__len", ++n))) { - lua_pop(L, n); /* pop metatable and tested metamethods */ - } - else - luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ - } -} - - -#if defined(LUA_COMPAT_MAXN) -static int maxn (lua_State *L) { - lua_Number max = 0; - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pop(L, 1); /* remove value */ - if (lua_type(L, -1) == LUA_TNUMBER) { - lua_Number v = lua_tonumber(L, -1); - if (v > max) max = v; - } - } - lua_pushnumber(L, max); - return 1; -} -#endif - - -static int tinsert (lua_State *L) { - lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ - lua_Integer pos; /* where to insert new element */ - switch (lua_gettop(L)) { - case 2: { /* called with only 2 arguments */ - pos = e; /* insert new element at the end */ - break; - } - case 3: { - lua_Integer i; - pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ - luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); - for (i = e; i > pos; i--) { /* move up elements */ - lua_geti(L, 1, i - 1); - lua_seti(L, 1, i); /* t[i] = t[i - 1] */ - } - break; - } - default: { - return luaL_error(L, "wrong number of arguments to 'insert'"); - } - } - lua_seti(L, 1, pos); /* t[pos] = v */ - return 0; -} - - -static int tremove (lua_State *L) { - lua_Integer size = aux_getn(L, 1, TAB_RW); - lua_Integer pos = luaL_optinteger(L, 2, size); - if (pos != size) /* validate 'pos' if given */ - luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); - lua_geti(L, 1, pos); /* result = t[pos] */ - for ( ; pos < size; pos++) { - lua_geti(L, 1, pos + 1); - lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ - } - lua_pushnil(L); - lua_seti(L, 1, pos); /* t[pos] = nil */ - return 1; -} - - -/* -** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever -** possible, copy in increasing order, which is better for rehashing. -** "possible" means destination after original range, or smaller -** than origin, or copying to another table. -*/ -static int tmove (lua_State *L) { - lua_Integer f = luaL_checkinteger(L, 2); - lua_Integer e = luaL_checkinteger(L, 3); - lua_Integer t = luaL_checkinteger(L, 4); - int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ - checktab(L, 1, TAB_R); - checktab(L, tt, TAB_W); - if (e >= f) { /* otherwise, nothing to move */ - lua_Integer n, i; - luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, - "too many elements to move"); - n = e - f + 1; /* number of elements to move */ - luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, - "destination wrap around"); - if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { - for (i = 0; i < n; i++) { - lua_geti(L, 1, f + i); - lua_seti(L, tt, t + i); - } - } - else { - for (i = n - 1; i >= 0; i--) { - lua_geti(L, 1, f + i); - lua_seti(L, tt, t + i); - } - } - } - lua_pushvalue(L, tt); /* return destination table */ - return 1; -} - - -static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { - lua_geti(L, 1, i); - if (!lua_isstring(L, -1)) - luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", - luaL_typename(L, -1), i); - luaL_addvalue(b); -} - - -static int tconcat (lua_State *L) { - luaL_Buffer b; - lua_Integer last = aux_getn(L, 1, TAB_R); - size_t lsep; - const char *sep = luaL_optlstring(L, 2, "", &lsep); - lua_Integer i = luaL_optinteger(L, 3, 1); - last = luaL_optinteger(L, 4, last); - luaL_buffinit(L, &b); - for (; i < last; i++) { - addfield(L, &b, i); - luaL_addlstring(&b, sep, lsep); - } - if (i == last) /* add last value (if interval was not empty) */ - addfield(L, &b, i); - luaL_pushresult(&b); - return 1; -} - - -/* -** {====================================================== -** Pack/unpack -** ======================================================= -*/ - -static int pack (lua_State *L) { - int i; - int n = lua_gettop(L); /* number of elements to pack */ - lua_createtable(L, n, 1); /* create result table */ - lua_insert(L, 1); /* put it at index 1 */ - for (i = n; i >= 1; i--) /* assign elements */ - lua_seti(L, 1, i); - lua_pushinteger(L, n); - lua_setfield(L, 1, "n"); /* t.n = number of elements */ - return 1; /* return table */ -} - - -static int unpack (lua_State *L) { - lua_Unsigned n; - lua_Integer i = luaL_optinteger(L, 2, 1); - lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); - if (i > e) return 0; /* empty range */ - n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ - if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) - return luaL_error(L, "too many results to unpack"); - for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ - lua_geti(L, 1, i); - } - lua_geti(L, 1, e); /* push last element */ - return (int)n; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Quicksort -** (based on 'Algorithms in MODULA-3', Robert Sedgewick; -** Addison-Wesley, 1993.) -** ======================================================= -*/ - - -/* type for array indices */ -typedef unsigned int IdxT; - - -/* -** Produce a "random" 'unsigned int' to randomize pivot choice. This -** macro is used only when 'sort' detects a big imbalance in the result -** of a partition. (If you don't want/need this "randomness", ~0 is a -** good choice.) -*/ -#if !defined(l_randomizePivot) /* { */ - -#include - -/* size of 'e' measured in number of 'unsigned int's */ -#define sof(e) (sizeof(e) / sizeof(unsigned int)) - -/* -** Use 'time' and 'clock' as sources of "randomness". Because we don't -** know the types 'clock_t' and 'time_t', we cannot cast them to -** anything without risking overflows. A safe way to use their values -** is to copy them to an array of a known type and use the array values. -*/ -static unsigned int l_randomizePivot (void) { - clock_t c = clock(); - time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; - unsigned int i, rnd = 0; - memcpy(buff, &c, sof(c) * sizeof(unsigned int)); - memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); - for (i = 0; i < sof(buff); i++) - rnd += buff[i]; - return rnd; -} - -#endif /* } */ - - -/* arrays larger than 'RANLIMIT' may use randomized pivots */ -#define RANLIMIT 100u - - -static void set2 (lua_State *L, IdxT i, IdxT j) { - lua_seti(L, 1, i); - lua_seti(L, 1, j); -} - - -/* -** Return true iff value at stack index 'a' is less than the value at -** index 'b' (according to the order of the sort). -*/ -static int sort_comp (lua_State *L, int a, int b) { - if (lua_isnil(L, 2)) /* no function? */ - return lua_compare(L, a, b, LUA_OPLT); /* a < b */ - else { /* function */ - int res; - lua_pushvalue(L, 2); /* push function */ - lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ - lua_call(L, 2, 1); /* call function */ - res = lua_toboolean(L, -1); /* get result */ - lua_pop(L, 1); /* pop result */ - return res; - } -} - - -/* -** Does the partition: Pivot P is at the top of the stack. -** precondition: a[lo] <= P == a[up-1] <= a[up], -** so it only needs to do the partition from lo + 1 to up - 2. -** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] -** returns 'i'. -*/ -static IdxT partition (lua_State *L, IdxT lo, IdxT up) { - IdxT i = lo; /* will be incremented before first use */ - IdxT j = up - 1; /* will be decremented before first use */ - /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ - for (;;) { - /* next loop: repeat ++i while a[i] < P */ - while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ - luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[i] */ - } - /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ - /* next loop: repeat --j while P < a[j] */ - while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j < i) /* j < i but a[j] > P ?? */ - luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[j] */ - } - /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ - if (j < i) { /* no elements out of place? */ - /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ - lua_pop(L, 1); /* pop a[j] */ - /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ - set2(L, up - 1, i); - return i; - } - /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ - set2(L, i, j); - } -} - - -/* -** Choose an element in the middle (2nd-3th quarters) of [lo,up] -** "randomized" by 'rnd' -*/ -static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { - IdxT r4 = (up - lo) / 4; /* range/4 */ - IdxT p = rnd % (r4 * 2) + (lo + r4); - lua_assert(lo + r4 <= p && p <= up - r4); - return p; -} - - -/* -** QuickSort algorithm (recursive function) -*/ -static void auxsort (lua_State *L, IdxT lo, IdxT up, - unsigned int rnd) { - while (lo < up) { /* loop for tail recursion */ - IdxT p; /* Pivot index */ - IdxT n; /* to be used later */ - /* sort elements 'lo', 'p', and 'up' */ - lua_geti(L, 1, lo); - lua_geti(L, 1, up); - if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ - set2(L, lo, up); /* swap a[lo] - a[up] */ - else - lua_pop(L, 2); /* remove both values */ - if (up - lo == 1) /* only 2 elements? */ - return; /* already sorted */ - if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */ - p = (lo + up)/2; /* middle element is a good pivot */ - else /* for larger intervals, it is worth a random pivot */ - p = choosePivot(lo, up, rnd); - lua_geti(L, 1, p); - lua_geti(L, 1, lo); - if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ - set2(L, p, lo); /* swap a[p] - a[lo] */ - else { - lua_pop(L, 1); /* remove a[lo] */ - lua_geti(L, 1, up); - if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ - set2(L, p, up); /* swap a[up] - a[p] */ - else - lua_pop(L, 2); - } - if (up - lo == 2) /* only 3 elements? */ - return; /* already sorted */ - lua_geti(L, 1, p); /* get middle element (Pivot) */ - lua_pushvalue(L, -1); /* push Pivot */ - lua_geti(L, 1, up - 1); /* push a[up - 1] */ - set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ - p = partition(L, lo, up); - /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ - if (p - lo < up - p) { /* lower interval is smaller? */ - auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ - n = p - lo; /* size of smaller interval */ - lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ - } - else { - auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ - n = up - p; /* size of smaller interval */ - up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ - } - if ((up - lo) / 128 > n) /* partition too imbalanced? */ - rnd = l_randomizePivot(); /* try a new randomization */ - } /* tail call auxsort(L, lo, up, rnd) */ -} - - -static int sort (lua_State *L) { - lua_Integer n = aux_getn(L, 1, TAB_RW); - if (n > 1) { /* non-trivial interval? */ - luaL_argcheck(L, n < INT_MAX, 1, "array too big"); - if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ - luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ - lua_settop(L, 2); /* make sure there are two arguments */ - auxsort(L, 1, (IdxT)n, 0); - } - return 0; -} - -/* }====================================================== */ - - -static const luaL_Reg tab_funcs[] = { - {"concat", tconcat}, -#if defined(LUA_COMPAT_MAXN) - {"maxn", maxn}, -#endif - {"insert", tinsert}, - {"pack", pack}, - {"unpack", unpack}, - {"remove", tremove}, - {"move", tmove}, - {"sort", sort}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_table (lua_State *L) { - luaL_newlib(L, tab_funcs); -#if defined(LUA_COMPAT_UNPACK) - /* _G.unpack = table.unpack */ - lua_getfield(L, -1, "unpack"); - lua_setglobal(L, "unpack"); -#endif - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.c deleted file mode 100644 index 0e7c713..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.c +++ /dev/null @@ -1,165 +0,0 @@ -/* -** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#define ltm_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - -static const char udatatypename[] = "userdata"; - -LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { - "no value", - "nil", "boolean", udatatypename, "number", - "string", "table", "function", udatatypename, "thread", - "proto" /* this last case is used for tests only */ -}; - - -void luaT_init (lua_State *L) { - static const char *const luaT_eventname[] = { /* ORDER TM */ - "__index", "__newindex", - "__gc", "__mode", "__len", "__eq", - "__add", "__sub", "__mul", "__mod", "__pow", - "__div", "__idiv", - "__band", "__bor", "__bxor", "__shl", "__shr", - "__unm", "__bnot", "__lt", "__le", - "__concat", "__call" - }; - int i; - for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); - luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ - } -} - - -/* -** function to be used with macro "fasttm": optimized for absence of -** tag methods -*/ -const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getshortstr(events, ename); - lua_assert(event <= TM_EQ); - if (ttisnil(tm)) { /* no tag method? */ - events->flags |= cast_byte(1u<metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(o)->metatable; - break; - default: - mt = G(L)->mt[ttnov(o)]; - } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); -} - - -/* -** Return the name of the type of an object. For tables and userdata -** with metatable, use their '__name' metafield, if present. -*/ -const char *luaT_objtypename (lua_State *L, const TValue *o) { - Table *mt; - if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || - (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { - const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); - if (ttisstring(name)) /* is '__name' a string? */ - return getstr(tsvalue(name)); /* use it as type name */ - } - return ttypename(ttnov(o)); /* else use standard type name */ -} - - -void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres) { - ptrdiff_t result = savestack(L, p3); - StkId func = L->top; - setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ - setobj2s(L, func + 1, p1); /* 1st argument */ - setobj2s(L, func + 2, p2); /* 2nd argument */ - L->top += 3; - if (!hasres) /* no result? 'p3' is third argument */ - setobj2s(L, L->top++, p3); /* 3rd argument */ - /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) - luaD_call(L, func, hasres); - else - luaD_callnoyield(L, func, hasres); - if (hasres) { /* if has result, move it to its place */ - p3 = restorestack(L, result); - setobjs2s(L, p3, --L->top); - } -} - - -int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ - if (ttisnil(tm)) - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (ttisnil(tm)) return 0; - luaT_callTM(L, tm, p1, p2, res, 1); - return 1; -} - - -void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - if (!luaT_callbinTM(L, p1, p2, res, event)) { - switch (event) { - case TM_CONCAT: - luaG_concaterror(L, p1, p2); - /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ - case TM_BAND: case TM_BOR: case TM_BXOR: - case TM_SHL: case TM_SHR: case TM_BNOT: { - lua_Number dummy; - if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) - luaG_tointerror(L, p1, p2); - else - luaG_opinterror(L, p1, p2, "perform bitwise operation on"); - } - /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ - default: - luaG_opinterror(L, p1, p2, "perform arithmetic on"); - } - } -} - - -int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, - TMS event) { - if (!luaT_callbinTM(L, p1, p2, L->top, event)) - return -1; /* no metamethod */ - else - return !l_isfalse(L->top); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.h deleted file mode 100644 index 8170688..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/ltm.h +++ /dev/null @@ -1,76 +0,0 @@ -/* -** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#ifndef ltm_h -#define ltm_h - - -#include "lobject.h" - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER TM" and "ORDER OP" -*/ -typedef enum { - TM_INDEX, - TM_NEWINDEX, - TM_GC, - TM_MODE, - TM_LEN, - TM_EQ, /* last tag method with fast access */ - TM_ADD, - TM_SUB, - TM_MUL, - TM_MOD, - TM_POW, - TM_DIV, - TM_IDIV, - TM_BAND, - TM_BOR, - TM_BXOR, - TM_SHL, - TM_SHR, - TM_UNM, - TM_BNOT, - TM_LT, - TM_LE, - TM_CONCAT, - TM_CALL, - TM_N /* number of elements in the enum */ -} TMS; - - - -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) - -#define fasttm(l,et,e) gfasttm(G(l), et, e) - -#define ttypename(x) luaT_typenames_[(x) + 1] - -LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; - - -LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); - -LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); -LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, - TMS event); -LUAI_FUNC void luaT_init (lua_State *L); - -LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres); -LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event); -LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event); -LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, - const TValue *p2, TMS event); - - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.c deleted file mode 100644 index ca5b298..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.c +++ /dev/null @@ -1,612 +0,0 @@ -/* -** $Id: lua.c,v 1.230.1.1 2017/04/19 17:29:57 roberto Exp $ -** Lua stand-alone interpreter -** See Copyright Notice in lua.h -*/ - -#define lua_c - -#include "lprefix.h" - - -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - -#if !defined(LUA_PROMPT) -#define LUA_PROMPT "> " -#define LUA_PROMPT2 ">> " -#endif - -#if !defined(LUA_PROGNAME) -#define LUA_PROGNAME "lua" -#endif - -#if !defined(LUA_MAXINPUT) -#define LUA_MAXINPUT 512 -#endif - -#if !defined(LUA_INIT_VAR) -#define LUA_INIT_VAR "LUA_INIT" -#endif - -#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX - - -/* -** lua_stdin_is_tty detects whether the standard input is a 'tty' (that -** is, whether we're running lua interactively). -*/ -#if !defined(lua_stdin_is_tty) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include -#define lua_stdin_is_tty() isatty(0) - -#elif defined(LUA_USE_WINDOWS) /* }{ */ - -#include -#include - -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) - -#else /* }{ */ - -/* ISO C definition */ -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ - -#endif /* } */ - -#endif /* } */ - - -/* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. -*/ -#if !defined(lua_readline) /* { */ - -#if defined(LUA_USE_READLINE) /* { */ - -#include -#include -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,line) ((void)L, add_history(line)) -#define lua_freeline(L,b) ((void)L, free(b)) - -#else /* }{ */ - -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,line) { (void)L; (void)line; } -#define lua_freeline(L,b) { (void)L; (void)b; } - -#endif /* } */ - -#endif /* } */ - - - - -static lua_State *globalL = NULL; - -static const char *progname = LUA_PROGNAME; - - -/* -** Hook set by signal function to stop the interpreter. -*/ -static void lstop (lua_State *L, lua_Debug *ar) { - (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); /* reset hook */ - luaL_error(L, "interrupted!"); -} - - -/* -** Function to be called at a C signal. Because a C signal cannot -** just change a Lua state (as there is no proper synchronization), -** this function only sets a hook that, when called, will stop the -** interpreter. -*/ -static void laction (int i) { - signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); -} - - -static void print_usage (const char *badoption) { - lua_writestringerror("%s: ", progname); - if (badoption[1] == 'e' || badoption[1] == 'l') - lua_writestringerror("'%s' needs argument\n", badoption); - else - lua_writestringerror("unrecognized option '%s'\n", badoption); - lua_writestringerror( - "usage: %s [options] [script [args]]\n" - "Available options are:\n" - " -e stat execute string 'stat'\n" - " -i enter interactive mode after executing 'script'\n" - " -l name require library 'name' into global 'name'\n" - " -v show version information\n" - " -E ignore environment variables\n" - " -- stop handling options\n" - " - stop handling options and execute stdin\n" - , - progname); -} - - -/* -** Prints an error message, adding the program name in front of it -** (if present) -*/ -static void l_message (const char *pname, const char *msg) { - if (pname) lua_writestringerror("%s: ", pname); - lua_writestringerror("%s\n", msg); -} - - -/* -** Check whether 'status' is not OK and, if so, prints the error -** message on the top of the stack. It assumes that the error object -** is a string, as it was either generated by Lua or by 'msghandler'. -*/ -static int report (lua_State *L, int status) { - if (status != LUA_OK) { - const char *msg = lua_tostring(L, -1); - l_message(progname, msg); - lua_pop(L, 1); /* remove message */ - } - return status; -} - - -/* -** Message handler used to run all chunks -*/ -static int msghandler (lua_State *L) { - const char *msg = lua_tostring(L, 1); - if (msg == NULL) { /* is error object not a string? */ - if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ - lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ - return 1; /* that is the message */ - else - msg = lua_pushfstring(L, "(error object is a %s value)", - luaL_typename(L, 1)); - } - luaL_traceback(L, L, msg, 1); /* append a standard traceback */ - return 1; /* return the traceback */ -} - - -/* -** Interface to 'lua_pcall', which sets appropriate message function -** and C-signal handler. Used to run all chunks. -*/ -static int docall (lua_State *L, int narg, int nres) { - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, msghandler); /* push message handler */ - lua_insert(L, base); /* put it under function and args */ - globalL = L; /* to be available to 'laction' */ - signal(SIGINT, laction); /* set C-signal handler */ - status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); /* reset C-signal handler */ - lua_remove(L, base); /* remove message handler from the stack */ - return status; -} - - -static void print_version (void) { - lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); - lua_writeline(); -} - - -/* -** Create the 'arg' table, which stores all arguments from the -** command line ('argv'). It should be aligned so that, at index 0, -** it has 'argv[script]', which is the script name. The arguments -** to the script (everything after 'script') go to positive indices; -** other arguments (before the script name) go to negative indices. -** If there is no script name, assume interpreter's name as base. -*/ -static void createargtable (lua_State *L, char **argv, int argc, int script) { - int i, narg; - if (script == argc) script = 0; /* no script name? */ - narg = argc - (script + 1); /* number of positive indices */ - lua_createtable(L, narg, script + 1); - for (i = 0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - script); - } - lua_setglobal(L, "arg"); -} - - -static int dochunk (lua_State *L, int status) { - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); -} - - -static int dofile (lua_State *L, const char *name) { - return dochunk(L, luaL_loadfile(L, name)); -} - - -static int dostring (lua_State *L, const char *s, const char *name) { - return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); -} - - -/* -** Calls 'require(name)' and stores the result in a global variable -** with the given name. -*/ -static int dolibrary (lua_State *L, const char *name) { - int status; - lua_getglobal(L, "require"); - lua_pushstring(L, name); - status = docall(L, 1, 1); /* call 'require(name)' */ - if (status == LUA_OK) - lua_setglobal(L, name); /* global[name] = require return */ - return report(L, status); -} - - -/* -** Returns the string to be used as a prompt by the interpreter. -*/ -static const char *get_prompt (lua_State *L, int firstline) { - const char *p; - lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); - return p; -} - -/* mark in error messages for incomplete statements */ -#define EOFMARK "" -#define marklen (sizeof(EOFMARK)/sizeof(char) - 1) - - -/* -** Check whether 'status' signals a syntax error and the error -** message at the top of the stack ends with the above mark for -** incomplete statements. -*/ -static int incomplete (lua_State *L, int status) { - if (status == LUA_ERRSYNTAX) { - size_t lmsg; - const char *msg = lua_tolstring(L, -1, &lmsg); - if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { - lua_pop(L, 1); - return 1; - } - } - return 0; /* else... */ -} - - -/* -** Prompt the user, read a line, and push it into the Lua stack. -*/ -static int pushline (lua_State *L, int firstline) { - char buffer[LUA_MAXINPUT]; - char *b = buffer; - size_t l; - const char *prmt = get_prompt(L, firstline); - int readstatus = lua_readline(L, b, prmt); - if (readstatus == 0) - return 0; /* no input (prompt will be popped by caller) */ - lua_pop(L, 1); /* remove prompt */ - l = strlen(b); - if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[--l] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ - lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ - else - lua_pushlstring(L, b, l); - lua_freeline(L, b); - return 1; -} - - -/* -** Try to compile line on the stack as 'return ;'; on return, stack -** has either compiled chunk or original line (if compilation failed). -*/ -static int addreturn (lua_State *L) { - const char *line = lua_tostring(L, -1); /* original line */ - const char *retline = lua_pushfstring(L, "return %s;", line); - int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); - if (status == LUA_OK) { - lua_remove(L, -2); /* remove modified line */ - if (line[0] != '\0') /* non empty? */ - lua_saveline(L, line); /* keep history */ - } - else - lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ - return status; -} - - -/* -** Read multiple lines until a complete Lua statement -*/ -static int multiline (lua_State *L) { - for (;;) { /* repeat until gets a complete statement */ - size_t len; - const char *line = lua_tolstring(L, 1, &len); /* get what it has */ - int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ - if (!incomplete(L, status) || !pushline(L, 0)) { - lua_saveline(L, line); /* keep history */ - return status; /* cannot or should not try to add continuation line */ - } - lua_pushliteral(L, "\n"); /* add newline... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } -} - - -/* -** Read a line and try to load (compile) it first as an expression (by -** adding "return " in front of it) and second as a statement. Return -** the final status of load/call with the resulting function (if any) -** in the top of the stack. -*/ -static int loadline (lua_State *L) { - int status; - lua_settop(L, 0); - if (!pushline(L, 1)) - return -1; /* no input */ - if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ - status = multiline(L); /* try as command, maybe with continuation lines */ - lua_remove(L, 1); /* remove line from the stack */ - lua_assert(lua_gettop(L) == 1); - return status; -} - - -/* -** Prints (calling the Lua 'print' function) any values on the stack -*/ -static void l_print (lua_State *L) { - int n = lua_gettop(L); - if (n > 0) { /* any result to be printed? */ - luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, n, 0, 0) != LUA_OK) - l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", - lua_tostring(L, -1))); - } -} - - -/* -** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and -** print any results. -*/ -static void doREPL (lua_State *L) { - int status; - const char *oldprogname = progname; - progname = NULL; /* no 'progname' on errors in interactive mode */ - while ((status = loadline(L)) != -1) { - if (status == LUA_OK) - status = docall(L, 0, LUA_MULTRET); - if (status == LUA_OK) l_print(L); - else report(L, status); - } - lua_settop(L, 0); /* clear stack */ - lua_writeline(); - progname = oldprogname; -} - - -/* -** Push on the stack the contents of table 'arg' from 1 to #arg -*/ -static int pushargs (lua_State *L) { - int i, n; - if (lua_getglobal(L, "arg") != LUA_TTABLE) - luaL_error(L, "'arg' is not a table"); - n = (int)luaL_len(L, -1); - luaL_checkstack(L, n + 3, "too many arguments to script"); - for (i = 1; i <= n; i++) - lua_rawgeti(L, -i, i); - lua_remove(L, -i); /* remove table from the stack */ - return n; -} - - -static int handle_script (lua_State *L, char **argv) { - int status; - const char *fname = argv[0]; - if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - if (status == LUA_OK) { - int n = pushargs(L); /* push arguments to script */ - status = docall(L, n, LUA_MULTRET); - } - return report(L, status); -} - - - -/* bits of various argument indicators in 'args' */ -#define has_error 1 /* bad option */ -#define has_i 2 /* -i */ -#define has_v 4 /* -v */ -#define has_e 8 /* -e */ -#define has_E 16 /* -E */ - -/* -** Traverses all arguments from 'argv', returning a mask with those -** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument -** (either the script name or a bad argument in case of error). -*/ -static int collectargs (char **argv, int *first) { - int args = 0; - int i; - for (i = 1; argv[i] != NULL; i++) { - *first = i; - if (argv[i][0] != '-') /* not an option? */ - return args; /* stop handling options */ - switch (argv[i][1]) { /* else check option */ - case '-': /* '--' */ - if (argv[i][2] != '\0') /* extra characters after '--'? */ - return has_error; /* invalid option */ - *first = i + 1; - return args; - case '\0': /* '-' */ - return args; /* script "name" is '-' */ - case 'E': - if (argv[i][2] != '\0') /* extra characters after 1st? */ - return has_error; /* invalid option */ - args |= has_E; - break; - case 'i': - args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ - case 'v': - if (argv[i][2] != '\0') /* extra characters after 1st? */ - return has_error; /* invalid option */ - args |= has_v; - break; - case 'e': - args |= has_e; /* FALLTHROUGH */ - case 'l': /* both options need an argument */ - if (argv[i][2] == '\0') { /* no concatenated argument? */ - i++; /* try next 'argv' */ - if (argv[i] == NULL || argv[i][0] == '-') - return has_error; /* no next argument or it is another option */ - } - break; - default: /* invalid option */ - return has_error; - } - } - *first = i; /* no script name */ - return args; -} - - -/* -** Processes options 'e' and 'l', which involve running Lua code. -** Returns 0 if some code raises an error. -*/ -static int runargs (lua_State *L, char **argv, int n) { - int i; - for (i = 1; i < n; i++) { - int option = argv[i][1]; - lua_assert(argv[i][0] == '-'); /* already checked */ - if (option == 'e' || option == 'l') { - int status; - const char *extra = argv[i] + 2; /* both options need an argument */ - if (*extra == '\0') extra = argv[++i]; - lua_assert(extra != NULL); - status = (option == 'e') - ? dostring(L, extra, "=(command line)") - : dolibrary(L, extra); - if (status != LUA_OK) return 0; - } - } - return 1; -} - - - -static int handle_luainit (lua_State *L) { - const char *name = "=" LUA_INITVARVERSION; - const char *init = getenv(name + 1); - if (init == NULL) { - name = "=" LUA_INIT_VAR; - init = getenv(name + 1); /* try alternative name */ - } - if (init == NULL) return LUA_OK; - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, name); -} - - -/* -** Main body of stand-alone interpreter (to be called in protected mode). -** Reads the options and handles them all. -*/ -static int pmain (lua_State *L) { - int argc = (int)lua_tointeger(L, 1); - char **argv = (char **)lua_touserdata(L, 2); - int script; - int args = collectargs(argv, &script); - luaL_checkversion(L); /* check that interpreter has correct version */ - if (argv[0] && argv[0][0]) progname = argv[0]; - if (args == has_error) { /* bad arg? */ - print_usage(argv[script]); /* 'script' has index of bad arg. */ - return 0; - } - if (args & has_v) /* option '-v'? */ - print_version(); - if (args & has_E) { /* option '-E'? */ - lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } - luaL_openlibs(L); /* open standard libraries */ - createargtable(L, argv, argc, script); /* create table 'arg' */ - if (!(args & has_E)) { /* no option '-E'? */ - if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ - return 0; /* error running LUA_INIT */ - } - if (!runargs(L, argv, script)) /* execute arguments -e and -l */ - return 0; /* something failed */ - if (script < argc && /* execute main script (if there is one) */ - handle_script(L, argv + script) != LUA_OK) - return 0; - if (args & has_i) /* -i option? */ - doREPL(L); /* do read-eval-print loop */ - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ - if (lua_stdin_is_tty()) { /* running in interactive mode? */ - print_version(); - doREPL(L); /* do read-eval-print loop */ - } - else dofile(L, NULL); /* executes stdin as a file */ - } - lua_pushboolean(L, 1); /* signal no errors */ - return 1; -} - - -int main (int argc, char **argv) { - int status, result; - lua_State *L = luaL_newstate(); /* create state */ - if (L == NULL) { - l_message(argv[0], "cannot create state: not enough memory"); - return EXIT_FAILURE; - } - lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ - lua_pushinteger(L, argc); /* 1st argument */ - lua_pushlightuserdata(L, argv); /* 2nd argument */ - status = lua_pcall(L, 2, 1, 0); /* do the call */ - result = lua_toboolean(L, -1); /* get result */ - report(L, status); - lua_close(L); - return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.h deleted file mode 100644 index 9394c5e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.h +++ /dev/null @@ -1,485 +0,0 @@ -/* -** Lua - A Scripting Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) -** See Copyright Notice at the end of this file -*/ - - -#ifndef lua_h -#define lua_h - -#include -#include - - -#include "luaconf.h" - - -#define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "3" -#define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "6" - -#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" - - -/* mark for precompiled code ('Lua') */ -#define LUA_SIGNATURE "\x1bLua" - -/* option for multiple returns in 'lua_pcall' and 'lua_call' */ -#define LUA_MULTRET (-1) - - -/* -** Pseudo-indices -** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty -** space after that to help overflow detection) -*/ -#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) -#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) - - -/* thread status */ -#define LUA_OK 0 -#define LUA_YIELD 1 -#define LUA_ERRRUN 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRGCMM 5 -#define LUA_ERRERR 6 - - -typedef struct lua_State lua_State; - - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - -#define LUA_NUMTAGS 9 - - - -/* minimum Lua stack available to a C function */ -#define LUA_MINSTACK 20 - - -/* predefined values in the registry */ -#define LUA_RIDX_MAINTHREAD 1 -#define LUA_RIDX_GLOBALS 2 -#define LUA_RIDX_LAST LUA_RIDX_GLOBALS - - -/* type of numbers in Lua */ -typedef LUA_NUMBER lua_Number; - - -/* type for integer functions */ -typedef LUA_INTEGER lua_Integer; - -/* unsigned integer type */ -typedef LUA_UNSIGNED lua_Unsigned; - -/* type for continuation-function contexts */ -typedef LUA_KCONTEXT lua_KContext; - - -/* -** Type for C functions registered with Lua -*/ -typedef int (*lua_CFunction) (lua_State *L); - -/* -** Type for continuation functions -*/ -typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); - - -/* -** Type for functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); - - -/* -** Type for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - - - -/* -** generic extra include file -*/ -#if defined(LUA_USER_H) -#include LUA_USER_H -#endif - - -/* -** RCS ident string -*/ -extern const char lua_ident[]; - - -/* -** state manipulation -*/ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); -LUA_API void (lua_close) (lua_State *L); -LUA_API lua_State *(lua_newthread) (lua_State *L); - -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); - - -LUA_API const lua_Number *(lua_version) (lua_State *L); - - -/* -** basic stack manipulation -*/ -LUA_API int (lua_absindex) (lua_State *L, int idx); -LUA_API int (lua_gettop) (lua_State *L); -LUA_API void (lua_settop) (lua_State *L, int idx); -LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_rotate) (lua_State *L, int idx, int n); -LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); -LUA_API int (lua_checkstack) (lua_State *L, int n); - -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); - - -/* -** access functions (stack -> C) -*/ - -LUA_API int (lua_isnumber) (lua_State *L, int idx); -LUA_API int (lua_isstring) (lua_State *L, int idx); -LUA_API int (lua_iscfunction) (lua_State *L, int idx); -LUA_API int (lua_isinteger) (lua_State *L, int idx); -LUA_API int (lua_isuserdata) (lua_State *L, int idx); -LUA_API int (lua_type) (lua_State *L, int idx); -LUA_API const char *(lua_typename) (lua_State *L, int tp); - -LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); -LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); -LUA_API int (lua_toboolean) (lua_State *L, int idx); -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API size_t (lua_rawlen) (lua_State *L, int idx); -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); -LUA_API void *(lua_touserdata) (lua_State *L, int idx); -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); -LUA_API const void *(lua_topointer) (lua_State *L, int idx); - - -/* -** Comparison and arithmetic functions -*/ - -#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ -#define LUA_OPSUB 1 -#define LUA_OPMUL 2 -#define LUA_OPMOD 3 -#define LUA_OPPOW 4 -#define LUA_OPDIV 5 -#define LUA_OPIDIV 6 -#define LUA_OPBAND 7 -#define LUA_OPBOR 8 -#define LUA_OPBXOR 9 -#define LUA_OPSHL 10 -#define LUA_OPSHR 11 -#define LUA_OPUNM 12 -#define LUA_OPBNOT 13 - -LUA_API void (lua_arith) (lua_State *L, int op); - -#define LUA_OPEQ 0 -#define LUA_OPLT 1 -#define LUA_OPLE 2 - -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); - - -/* -** push functions (C -> stack) -*/ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); -LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, - va_list argp); -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); -LUA_API void (lua_pushboolean) (lua_State *L, int b); -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); -LUA_API int (lua_pushthread) (lua_State *L); - - -/* -** get functions (Lua -> stack) -*/ -LUA_API int (lua_getglobal) (lua_State *L, const char *name); -LUA_API int (lua_gettable) (lua_State *L, int idx); -LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); -LUA_API int (lua_rawget) (lua_State *L, int idx); -LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); -LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); - -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API int (lua_getuservalue) (lua_State *L, int idx); - - -/* -** set functions (stack -> Lua) -*/ -LUA_API void (lua_setglobal) (lua_State *L, const char *name); -LUA_API void (lua_settable) (lua_State *L, int idx); -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); -LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); -LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API void (lua_setuservalue) (lua_State *L, int idx); - - -/* -** 'load' and 'call' functions (load and run Lua code) -*/ -LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, - lua_KContext ctx, lua_KFunction k); -#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) - -LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, - lua_KContext ctx, lua_KFunction k); -#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) - -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, const char *mode); - -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); - - -/* -** coroutine functions -*/ -LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, - lua_KFunction k); -LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); -LUA_API int (lua_status) (lua_State *L); -LUA_API int (lua_isyieldable) (lua_State *L); - -#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) - - -/* -** garbage-collection function and options -*/ - -#define LUA_GCSTOP 0 -#define LUA_GCRESTART 1 -#define LUA_GCCOLLECT 2 -#define LUA_GCCOUNT 3 -#define LUA_GCCOUNTB 4 -#define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 -#define LUA_GCISRUNNING 9 - -LUA_API int (lua_gc) (lua_State *L, int what, int data); - - -/* -** miscellaneous functions -*/ - -LUA_API int (lua_error) (lua_State *L); - -LUA_API int (lua_next) (lua_State *L, int idx); - -LUA_API void (lua_concat) (lua_State *L, int n); -LUA_API void (lua_len) (lua_State *L, int idx); - -LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); - -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); -LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); - - - -/* -** {============================================================== -** some useful macros -** =============================================================== -*/ - -#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) - -#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) -#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_newtable(L) lua_createtable(L, 0, 0) - -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) - -#define lua_pushliteral(L, s) lua_pushstring(L, "" s) - -#define lua_pushglobaltable(L) \ - ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) - -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) - - -#define lua_insert(L,idx) lua_rotate(L, (idx), 1) - -#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) - -#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) - -/* }============================================================== */ - - -/* -** {============================================================== -** compatibility macros for unsigned conversions -** =============================================================== -*/ -#if defined(LUA_COMPAT_APIINTCASTS) - -#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) -#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) -#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) - -#endif -/* }============================================================== */ - -/* -** {====================================================================== -** Debug API -** ======================================================================= -*/ - - -/* -** Event codes -*/ -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILCALL 4 - - -/* -** Event masks -*/ -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debugger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); -LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); -LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); -LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); - -LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); -LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, - int fidx2, int n2); - -LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); -LUA_API lua_Hook (lua_gethook) (lua_State *L); -LUA_API int (lua_gethookmask) (lua_State *L); -LUA_API int (lua_gethookcount) (lua_State *L); - - -struct lua_Debug { - int event; - const char *name; /* (n) */ - const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ - const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ - const char *source; /* (S) */ - int currentline; /* (l) */ - int linedefined; /* (S) */ - int lastlinedefined; /* (S) */ - unsigned char nups; /* (u) number of upvalues */ - unsigned char nparams;/* (u) number of parameters */ - char isvararg; /* (u) */ - char istailcall; /* (t) */ - char short_src[LUA_IDSIZE]; /* (S) */ - /* private part */ - struct CallInfo *i_ci; /* active function */ -}; - -/* }====================================================================== */ - - -/****************************************************************************** -* Copyright (C) 1994-2020 Lua.org, PUC-Rio. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.hpp b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.hpp deleted file mode 100644 index ec417f5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lua.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// lua.hpp -// Lua header files for C++ -// <> not supplied automatically because Lua also compiles as C++ - -extern "C" { -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/luac.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/luac.c deleted file mode 100644 index 549ad39..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/luac.c +++ /dev/null @@ -1,450 +0,0 @@ -/* -** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $ -** Lua compiler (saves bytecodes to files; also lists bytecodes) -** See Copyright Notice in lua.h -*/ - -#define luac_c -#define LUA_CORE - -#include "lprefix.h" - -#include -#include -#include -#include -#include - -#include "lua.h" -#include "lauxlib.h" - -#include "lobject.h" -#include "lstate.h" -#include "lundump.h" - -static void PrintFunction(const Proto* f, int full); -#define luaU_print PrintFunction - -#define PROGNAME "luac" /* default program name */ -#define OUTPUT PROGNAME ".out" /* default output file */ - -static int listing=0; /* list bytecodes? */ -static int dumping=1; /* dump bytecodes? */ -static int stripping=0; /* strip debug information? */ -static char Output[]={ OUTPUT }; /* default output file name */ -static const char* output=Output; /* actual output file name */ -static const char* progname=PROGNAME; /* actual program name */ - -static void fatal(const char* message) -{ - fprintf(stderr,"%s: %s\n",progname,message); - exit(EXIT_FAILURE); -} - -static void cannot(const char* what) -{ - fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); - exit(EXIT_FAILURE); -} - -static void usage(const char* message) -{ - if (*message=='-') - fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); - else - fprintf(stderr,"%s: %s\n",progname,message); - fprintf(stderr, - "usage: %s [options] [filenames]\n" - "Available options are:\n" - " -l list (use -l -l for full listing)\n" - " -o name output to file 'name' (default is \"%s\")\n" - " -p parse only\n" - " -s strip debug information\n" - " -v show version information\n" - " -- stop handling options\n" - " - stop handling options and process stdin\n" - ,progname,Output); - exit(EXIT_FAILURE); -} - -#define IS(s) (strcmp(argv[i],s)==0) - -static int doargs(int argc, char* argv[]) -{ - int i; - int version=0; - if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; - for (i=1; itop+(i)) - -static const Proto* combine(lua_State* L, int n) -{ - if (n==1) - return toproto(L,-1); - else - { - Proto* f; - int i=n; - if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); - f=toproto(L,-1); - for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; - } - f->sizelineinfo=0; - return f; - } -} - -static int writer(lua_State* L, const void* p, size_t size, void* u) -{ - UNUSED(L); - return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); -} - -static int pmain(lua_State* L) -{ - int argc=(int)lua_tointeger(L,1); - char** argv=(char**)lua_touserdata(L,2); - const Proto* f; - int i; - if (!lua_checkstack(L,argc)) fatal("too many input files"); - for (i=0; i1); - if (dumping) - { - FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); - if (D==NULL) cannot("open"); - lua_lock(L); - luaU_dump(L,f,writer,D,stripping); - lua_unlock(L); - if (ferror(D)) cannot("write"); - if (fclose(D)) cannot("close"); - } - return 0; -} - -int main(int argc, char* argv[]) -{ - lua_State* L; - int i=doargs(argc,argv); - argc-=i; argv+=i; - if (argc<=0) usage("no input files given"); - L=luaL_newstate(); - if (L==NULL) fatal("cannot create state: not enough memory"); - lua_pushcfunction(L,&pmain); - lua_pushinteger(L,argc); - lua_pushlightuserdata(L,argv); - if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); - lua_close(L); - return EXIT_SUCCESS; -} - -/* -** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $ -** print bytecodes -** See Copyright Notice in lua.h -*/ - -#include -#include - -#define luac_c -#define LUA_CORE - -#include "ldebug.h" -#include "lobject.h" -#include "lopcodes.h" - -#define VOID(p) ((const void*)(p)) - -static void PrintString(const TString* ts) -{ - const char* s=getstr(ts); - size_t i,n=tsslen(ts); - printf("%c",'"'); - for (i=0; ik[i]; - switch (ttype(o)) - { - case LUA_TNIL: - printf("nil"); - break; - case LUA_TBOOLEAN: - printf(bvalue(o) ? "true" : "false"); - break; - case LUA_TNUMFLT: - { - char buff[100]; - sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); - printf("%s",buff); - if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); - break; - } - case LUA_TNUMINT: - printf(LUA_INTEGER_FMT,ivalue(o)); - break; - case LUA_TSHRSTR: case LUA_TLNGSTR: - PrintString(tsvalue(o)); - break; - default: /* cannot happen */ - printf("? type=%d",ttype(o)); - break; - } -} - -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") -#define MYK(x) (-1-(x)) - -static void PrintCode(const Proto* f) -{ - const Instruction* code=f->code; - int pc,n=f->sizecode; - for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); - printf("%-9s\t",luaP_opnames[o]); - switch (getOpMode(o)) - { - case iABC: - printf("%d",a); - if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); - if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); - break; - case iABx: - printf("%d",a); - if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); - if (getBMode(o)==OpArgU) printf(" %d",bx); - break; - case iAsBx: - printf("%d %d",a,sbx); - break; - case iAx: - printf("%d",MYK(ax)); - break; - } - switch (o) - { - case OP_LOADK: - printf("\t; "); PrintConstant(f,bx); - break; - case OP_GETUPVAL: - case OP_SETUPVAL: - printf("\t; %s",UPVALNAME(b)); - break; - case OP_GETTABUP: - printf("\t; %s",UPVALNAME(b)); - if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } - break; - case OP_SETTABUP: - printf("\t; %s",UPVALNAME(a)); - if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } - if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } - break; - case OP_GETTABLE: - case OP_SELF: - if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } - break; - case OP_SETTABLE: - case OP_ADD: - case OP_SUB: - case OP_MUL: - case OP_MOD: - case OP_POW: - case OP_DIV: - case OP_IDIV: - case OP_BAND: - case OP_BOR: - case OP_BXOR: - case OP_SHL: - case OP_SHR: - case OP_EQ: - case OP_LT: - case OP_LE: - if (ISK(b) || ISK(c)) - { - printf("\t; "); - if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); - printf(" "); - if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); - } - break; - case OP_JMP: - case OP_FORLOOP: - case OP_FORPREP: - case OP_TFORLOOP: - printf("\t; to %d",sbx+pc+2); - break; - case OP_CLOSURE: - printf("\t; %p",VOID(f->p[bx])); - break; - case OP_SETLIST: - if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); - break; - case OP_EXTRAARG: - printf("\t; "); PrintConstant(f,ax); - break; - default: - break; - } - printf("\n"); - } -} - -#define SS(x) ((x==1)?"":"s") -#define S(x) (int)(x),SS(x) - -static void PrintHeader(const Proto* f) -{ - const char* s=f->source ? getstr(f->source) : "=?"; - if (*s=='@' || *s=='=') - s++; - else if (*s==LUA_SIGNATURE[0]) - s="(bstring)"; - else - s="(string)"; - printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", - (f->linedefined==0)?"main":"function",s, - f->linedefined,f->lastlinedefined, - S(f->sizecode),VOID(f)); - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", - (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), - S(f->maxstacksize),S(f->sizeupvalues)); - printf("%d local%s, %d constant%s, %d function%s\n", - S(f->sizelocvars),S(f->sizek),S(f->sizep)); -} - -static void PrintDebug(const Proto* f) -{ - int i,n; - n=f->sizek; - printf("constants (%d) for %p:\n",n,VOID(f)); - for (i=0; isizelocvars; - printf("locals (%d) for %p:\n",n,VOID(f)); - for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); - } - n=f->sizeupvalues; - printf("upvalues (%d) for %p:\n",n,VOID(f)); - for (i=0; iupvalues[i].instack,f->upvalues[i].idx); - } -} - -static void PrintFunction(const Proto* f, int full) -{ - int i,n=f->sizep; - PrintHeader(f); - PrintCode(f); - if (full) PrintDebug(f); - for (i=0; ip[i],full); -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/luaconf.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/luaconf.h deleted file mode 100644 index 9eeeea6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/luaconf.h +++ /dev/null @@ -1,790 +0,0 @@ -/* -** $Id: luaconf.h,v 1.259.1.1 2017/04/19 17:29:57 roberto Exp $ -** Configuration file for Lua -** See Copyright Notice in lua.h -*/ - - -#ifndef luaconf_h -#define luaconf_h - -#include -#include - - -/* -** =================================================================== -** Search for "@@" to find all configurable definitions. -** =================================================================== -*/ - - -/* -** {==================================================================== -** System Configuration: macros to adapt (if needed) Lua to some -** particular platform, for instance compiling it with 32-bit numbers or -** restricting it to C89. -** ===================================================================== -*/ - -/* -@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You -** can also define LUA_32BITS in the make file, but changing here you -** ensure that all software connected to Lua will be compiled with the -** same configuration. -*/ -/* #define LUA_32BITS */ - - -/* -@@ LUA_USE_C89 controls the use of non-ISO-C89 features. -** Define it if you want Lua to avoid the use of a few C99 features -** or Windows-specific features on Windows. -*/ -/* #define LUA_USE_C89 */ - - -/* -** By default, Lua on Windows use (some) specific Windows features -*/ -#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) -#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ -#endif - - -#if defined(LUA_USE_WINDOWS) -#define LUA_DL_DLL /* enable support for DLL */ -#define LUA_USE_C89 /* broadly, Windows is C89 */ -#endif - - -#if defined(LUA_USE_LINUX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ -#define LUA_USE_READLINE /* needs some extra libraries */ -#endif - - -#if defined(LUA_USE_MACOSX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ -#define LUA_USE_READLINE /* needs an extra library: -lreadline */ -#endif - - -/* -@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for -** C89 ('long' and 'double'); Windows always has '__int64', so it does -** not need to use this case. -*/ -#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) -#define LUA_C89_NUMBERS -#endif - - - -/* -@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. -*/ -/* avoid undefined shifts */ -#if ((INT_MAX >> 15) >> 15) >= 1 -#define LUAI_BITSINT 32 -#else -/* 'int' always must have at least 16 bits */ -#define LUAI_BITSINT 16 -#endif - - -/* -@@ LUA_INT_TYPE defines the type for Lua integers. -@@ LUA_FLOAT_TYPE defines the type for Lua floats. -** Lua should work fine with any mix of these options (if supported -** by your C compiler). The usual configurations are 64-bit integers -** and 'double' (the default), 32-bit integers and 'float' (for -** restricted platforms), and 'long'/'double' (for C compilers not -** compliant with C99, which may not have support for 'long long'). -*/ - -/* predefined options for LUA_INT_TYPE */ -#define LUA_INT_INT 1 -#define LUA_INT_LONG 2 -#define LUA_INT_LONGLONG 3 - -/* predefined options for LUA_FLOAT_TYPE */ -#define LUA_FLOAT_FLOAT 1 -#define LUA_FLOAT_DOUBLE 2 -#define LUA_FLOAT_LONGDOUBLE 3 - -#if defined(LUA_32BITS) /* { */ -/* -** 32-bit integers and 'float' -*/ -#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ -#define LUA_INT_TYPE LUA_INT_INT -#else /* otherwise use 'long' */ -#define LUA_INT_TYPE LUA_INT_LONG -#endif -#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT - -#elif defined(LUA_C89_NUMBERS) /* }{ */ -/* -** largest types available for C89 ('long' and 'double') -*/ -#define LUA_INT_TYPE LUA_INT_LONG -#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE - -#endif /* } */ - - -/* -** default configuration for 64-bit Lua ('long long' and 'double') -*/ -#if !defined(LUA_INT_TYPE) -#define LUA_INT_TYPE LUA_INT_LONGLONG -#endif - -#if !defined(LUA_FLOAT_TYPE) -#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif - -/* }================================================================== */ - - - - -/* -** {================================================================== -** Configuration for Paths. -** =================================================================== -*/ - -/* -** LUA_PATH_SEP is the character that separates templates in a path. -** LUA_PATH_MARK is the string that marks the substitution points in a -** template. -** LUA_EXEC_DIR in a Windows path is replaced by the executable's -** directory. -*/ -#define LUA_PATH_SEP ";" -#define LUA_PATH_MARK "?" -#define LUA_EXEC_DIR "!" - - -/* -@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for -** Lua libraries. -@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for -** C libraries. -** CHANGE them if your machine has a non-conventional directory -** hierarchy or if you want to install your libraries in -** non-conventional directories. -*/ -#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#if defined(_WIN32) /* { */ -/* -** In Windows, any exclamation mark ('!') in the path is replaced by the -** path of the directory of the executable file of the current process. -*/ -#define LUA_LDIR "!\\lua\\" -#define LUA_CDIR "!\\" -#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" -#define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ - LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ - ".\\?.lua;" ".\\?\\init.lua" -#define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.dll;" \ - LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ - LUA_CDIR"loadall.dll;" ".\\?.dll" - -#else /* }{ */ - -#define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" -#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" -#define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ - "./?.lua;" "./?/init.lua" -#define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" -#endif /* } */ - - -/* -@@ LUA_DIRSEP is the directory separator (for submodules). -** CHANGE it if your machine does not use "/" as the directory separator -** and is not Windows. (On Windows Lua automatically uses "\".) -*/ -#if defined(_WIN32) -#define LUA_DIRSEP "\\" -#else -#define LUA_DIRSEP "/" -#endif - -/* }================================================================== */ - - -/* -** {================================================================== -** Marks for exported symbols in the C code -** =================================================================== -*/ - -/* -@@ LUA_API is a mark for all core API functions. -@@ LUALIB_API is a mark for all auxiliary library functions. -@@ LUAMOD_API is a mark for all standard library opening functions. -** CHANGE them if you need to define those functions in some special way. -** For instance, if you want to create one Windows DLL with the core and -** the libraries, you may want to use the following definition (define -** LUA_BUILD_AS_DLL to get it). -*/ -#if defined(LUA_BUILD_AS_DLL) /* { */ - -#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ -#define LUA_API __declspec(dllexport) -#else /* }{ */ -#define LUA_API __declspec(dllimport) -#endif /* } */ - -#else /* }{ */ - -#define LUA_API extern - -#endif /* } */ - - -/* more often than not the libs go together with the core */ -#define LUALIB_API LUA_API -#define LUAMOD_API LUALIB_API - - -/* -@@ LUAI_FUNC is a mark for all extern functions that are not to be -** exported to outside modules. -@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables -** that are not to be exported to outside modules (LUAI_DDEF for -** definitions and LUAI_DDEC for declarations). -** CHANGE them if you need to mark them in some special way. Elf/gcc -** (versions 3.2 and later) mark them as "hidden" to optimize access -** when Lua is compiled as a shared library. Not all elf targets support -** this attribute. Unfortunately, gcc does not offer a way to check -** whether the target offers that support, and those without support -** give a warning about it. To avoid these warnings, change to the -** default definition. -*/ -#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("hidden"))) extern -#else /* }{ */ -#define LUAI_FUNC extern -#endif /* } */ - -#define LUAI_DDEC LUAI_FUNC -#define LUAI_DDEF /* empty */ - -/* }================================================================== */ - - -/* -** {================================================================== -** Compatibility with previous versions -** =================================================================== -*/ - -/* -@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. -@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. -** You can define it to get all options, or change specific options -** to fit your specific needs. -*/ -#if defined(LUA_COMPAT_5_2) /* { */ - -/* -@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated -** functions in the mathematical library. -*/ -#define LUA_COMPAT_MATHLIB - -/* -@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. -*/ -#define LUA_COMPAT_BITLIB - -/* -@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. -*/ -#define LUA_COMPAT_IPAIRS - -/* -@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for -** manipulating other integer types (lua_pushunsigned, lua_tounsigned, -** luaL_checkint, luaL_checklong, etc.) -*/ -#define LUA_COMPAT_APIINTCASTS - -#endif /* } */ - - -#if defined(LUA_COMPAT_5_1) /* { */ - -/* Incompatibilities from 5.2 -> 5.3 */ -#define LUA_COMPAT_MATHLIB -#define LUA_COMPAT_APIINTCASTS - -/* -@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. -** You can replace it with 'table.unpack'. -*/ -#define LUA_COMPAT_UNPACK - -/* -@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. -** You can replace it with 'package.searchers'. -*/ -#define LUA_COMPAT_LOADERS - -/* -@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. -** You can call your C function directly (with light C functions). -*/ -#define lua_cpcall(L,f,u) \ - (lua_pushcfunction(L, (f)), \ - lua_pushlightuserdata(L,(u)), \ - lua_pcall(L,1,0,0)) - - -/* -@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. -** You can rewrite 'log10(x)' as 'log(x, 10)'. -*/ -#define LUA_COMPAT_LOG10 - -/* -@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base -** library. You can rewrite 'loadstring(s)' as 'load(s)'. -*/ -#define LUA_COMPAT_LOADSTRING - -/* -@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. -*/ -#define LUA_COMPAT_MAXN - -/* -@@ The following macros supply trivial compatibility for some -** changes in the API. The macros themselves document how to -** change your code to avoid using them. -*/ -#define lua_strlen(L,i) lua_rawlen(L, (i)) - -#define lua_objlen(L,i) lua_rawlen(L, (i)) - -#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) -#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) - -/* -@@ LUA_COMPAT_MODULE controls compatibility with previous -** module functions 'module' (Lua) and 'luaL_register' (C). -*/ -#define LUA_COMPAT_MODULE - -#endif /* } */ - - -/* -@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a -@@ a float mark ('.0'). -** This macro is not on by default even in compatibility mode, -** because this is not really an incompatibility. -*/ -/* #define LUA_COMPAT_FLOATSTRING */ - -/* }================================================================== */ - - - -/* -** {================================================================== -** Configuration for Numbers. -** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* -** satisfy your needs. -** =================================================================== -*/ - -/* -@@ LUA_NUMBER is the floating-point type used by Lua. -@@ LUAI_UACNUMBER is the result of a 'default argument promotion' -@@ over a floating number. -@@ l_mathlim(x) corrects limit name 'x' to the proper float type -** by prefixing it with one of FLT/DBL/LDBL. -@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. -@@ LUA_NUMBER_FMT is the format for writing floats. -@@ lua_number2str converts a float to a string. -@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. -@@ l_floor takes the floor of a float. -@@ lua_str2number converts a decimal numeric string to a number. -*/ - - -/* The following definitions are good for most cases here */ - -#define l_floor(x) (l_mathop(floor)(x)) - -#define lua_number2str(s,sz,n) \ - l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) - -/* -@@ lua_numbertointeger converts a float number to an integer, or -** returns 0 if float is not within the range of a lua_Integer. -** (The range comparisons are tricky because of rounding. The tests -** here assume a two-complement representation, where MININTEGER always -** has an exact representation as a float; MAXINTEGER may not have one, -** and therefore its conversion to float may have an ill-defined value.) -*/ -#define lua_numbertointeger(n,p) \ - ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ - (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ - (*(p) = (LUA_INTEGER)(n), 1)) - - -/* now the variable definitions */ - -#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ - -#define LUA_NUMBER float - -#define l_mathlim(n) (FLT_##n) - -#define LUAI_UACNUMBER double - -#define LUA_NUMBER_FRMLEN "" -#define LUA_NUMBER_FMT "%.7g" - -#define l_mathop(op) op##f - -#define lua_str2number(s,p) strtof((s), (p)) - - -#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ - -#define LUA_NUMBER long double - -#define l_mathlim(n) (LDBL_##n) - -#define LUAI_UACNUMBER long double - -#define LUA_NUMBER_FRMLEN "L" -#define LUA_NUMBER_FMT "%.19Lg" - -#define l_mathop(op) op##l - -#define lua_str2number(s,p) strtold((s), (p)) - -#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ - -#define LUA_NUMBER double - -#define l_mathlim(n) (DBL_##n) - -#define LUAI_UACNUMBER double - -#define LUA_NUMBER_FRMLEN "" -#define LUA_NUMBER_FMT "%.14g" - -#define l_mathop(op) op - -#define lua_str2number(s,p) strtod((s), (p)) - -#else /* }{ */ - -#error "numeric float type not defined" - -#endif /* } */ - - - -/* -@@ LUA_INTEGER is the integer type used by Lua. -** -@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. -** -@@ LUAI_UACINT is the result of a 'default argument promotion' -@@ over a lUA_INTEGER. -@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. -@@ LUA_INTEGER_FMT is the format for writing integers. -@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. -@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. -@@ lua_integer2str converts an integer to a string. -*/ - - -/* The following definitions are good for most cases here */ - -#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" - -#define LUAI_UACINT LUA_INTEGER - -#define lua_integer2str(s,sz,n) \ - l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) - -/* -** use LUAI_UACINT here to avoid problems with promotions (which -** can turn a comparison between unsigneds into a signed comparison) -*/ -#define LUA_UNSIGNED unsigned LUAI_UACINT - - -/* now the variable definitions */ - -#if LUA_INT_TYPE == LUA_INT_INT /* { int */ - -#define LUA_INTEGER int -#define LUA_INTEGER_FRMLEN "" - -#define LUA_MAXINTEGER INT_MAX -#define LUA_MININTEGER INT_MIN - -#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ - -#define LUA_INTEGER long -#define LUA_INTEGER_FRMLEN "l" - -#define LUA_MAXINTEGER LONG_MAX -#define LUA_MININTEGER LONG_MIN - -#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ - -/* use presence of macro LLONG_MAX as proxy for C99 compliance */ -#if defined(LLONG_MAX) /* { */ -/* use ISO C99 stuff */ - -#define LUA_INTEGER long long -#define LUA_INTEGER_FRMLEN "ll" - -#define LUA_MAXINTEGER LLONG_MAX -#define LUA_MININTEGER LLONG_MIN - -#elif defined(LUA_USE_WINDOWS) /* }{ */ -/* in Windows, can use specific Windows types */ - -#define LUA_INTEGER __int64 -#define LUA_INTEGER_FRMLEN "I64" - -#define LUA_MAXINTEGER _I64_MAX -#define LUA_MININTEGER _I64_MIN - -#else /* }{ */ - -#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ - or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" - -#endif /* } */ - -#else /* }{ */ - -#error "numeric integer type not defined" - -#endif /* } */ - -/* }================================================================== */ - - -/* -** {================================================================== -** Dependencies with C99 and other C details -** =================================================================== -*/ - -/* -@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. -** (All uses in Lua have only one format item.) -*/ -#if !defined(LUA_USE_C89) -#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) -#else -#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) -#endif - - -/* -@@ lua_strx2number converts an hexadecimal numeric string to a number. -** In C99, 'strtod' does that conversion. Otherwise, you can -** leave 'lua_strx2number' undefined and Lua will provide its own -** implementation. -*/ -#if !defined(LUA_USE_C89) -#define lua_strx2number(s,p) lua_str2number(s,p) -#endif - - -/* -@@ lua_pointer2str converts a pointer to a readable string in a -** non-specified way. -*/ -#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) - - -/* -@@ lua_number2strx converts a float to an hexadecimal numeric string. -** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. -** Otherwise, you can leave 'lua_number2strx' undefined and Lua will -** provide its own implementation. -*/ -#if !defined(LUA_USE_C89) -#define lua_number2strx(L,b,sz,f,n) \ - ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) -#endif - - -/* -** 'strtof' and 'opf' variants for math functions are not valid in -** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the -** availability of these variants. ('math.h' is already included in -** all files that use these macros.) -*/ -#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) -#undef l_mathop /* variants not available */ -#undef lua_str2number -#define l_mathop(op) (lua_Number)op /* no variant */ -#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) -#endif - - -/* -@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation -** functions. It must be a numerical type; Lua will use 'intptr_t' if -** available, otherwise it will use 'ptrdiff_t' (the nearest thing to -** 'intptr_t' in C89) -*/ -#define LUA_KCONTEXT ptrdiff_t - -#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ - __STDC_VERSION__ >= 199901L -#include -#if defined(INTPTR_MAX) /* even in C99 this type is optional */ -#undef LUA_KCONTEXT -#define LUA_KCONTEXT intptr_t -#endif -#endif - - -/* -@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). -** Change that if you do not want to use C locales. (Code using this -** macro must include header 'locale.h'.) -*/ -#if !defined(lua_getlocaledecpoint) -#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) -#endif - -/* }================================================================== */ - - -/* -** {================================================================== -** Language Variations -** ===================================================================== -*/ - -/* -@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some -** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from -** numbers to strings. Define LUA_NOCVTS2N to turn off automatic -** coercion from strings to numbers. -*/ -/* #define LUA_NOCVTN2S */ -/* #define LUA_NOCVTS2N */ - - -/* -@@ LUA_USE_APICHECK turns on several consistency checks on the C API. -** Define it as a help when debugging C code. -*/ -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(l,e) assert(e) -#endif - -/* }================================================================== */ - - -/* -** {================================================================== -** Macros that affect the API and must be stable (that is, must be the -** same when you compile Lua and when you compile code that links to -** Lua). You probably do not want/need to change them. -** ===================================================================== -*/ - -/* -@@ LUAI_MAXSTACK limits the size of the Lua stack. -** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua from consuming unlimited stack -** space (and to reserve some numbers for pseudo-indices). -*/ -#if LUAI_BITSINT >= 32 -#define LUAI_MAXSTACK 1000000 -#else -#define LUAI_MAXSTACK 15000 -#endif - - -/* -@@ LUA_EXTRASPACE defines the size of a raw memory area associated with -** a Lua state with very fast access. -** CHANGE it if you need a different size. -*/ -#define LUA_EXTRASPACE (sizeof(void *)) - - -/* -@@ LUA_IDSIZE gives the maximum size for the description of the source -@@ of a function in debug information. -** CHANGE it if you want a different size. -*/ -#define LUA_IDSIZE 60 - - -/* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -** CHANGE it if it uses too much C-stack space. (For long double, -** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a -** smaller buffer would force a memory allocation for each call to -** 'string.format'.) -*/ -#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE -#define LUAL_BUFFERSIZE 8192 -#else -#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) -#endif - -/* }================================================================== */ - - -/* -@@ LUA_QL describes how error messages quote program elements. -** Lua does not use these macros anymore; they are here for -** compatibility only. -*/ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - - - - -/* =================================================================== */ - -/* -** Local configuration. You can use this space to add your redefinitions -** without modifying the main part of the file. -*/ - - - - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lualib.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lualib.h deleted file mode 100644 index f5304aa..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lualib.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ -** Lua standard libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lualib_h -#define lualib_h - -#include "lua.h" - - -/* version suffix for environment variable names */ -#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - - -LUAMOD_API int (luaopen_base) (lua_State *L); - -#define LUA_COLIBNAME "coroutine" -LUAMOD_API int (luaopen_coroutine) (lua_State *L); - -#define LUA_TABLIBNAME "table" -LUAMOD_API int (luaopen_table) (lua_State *L); - -#define LUA_IOLIBNAME "io" -LUAMOD_API int (luaopen_io) (lua_State *L); - -#define LUA_OSLIBNAME "os" -LUAMOD_API int (luaopen_os) (lua_State *L); - -#define LUA_STRLIBNAME "string" -LUAMOD_API int (luaopen_string) (lua_State *L); - -#define LUA_UTF8LIBNAME "utf8" -LUAMOD_API int (luaopen_utf8) (lua_State *L); - -#define LUA_BITLIBNAME "bit32" -LUAMOD_API int (luaopen_bit32) (lua_State *L); - -#define LUA_MATHLIBNAME "math" -LUAMOD_API int (luaopen_math) (lua_State *L); - -#define LUA_DBLIBNAME "debug" -LUAMOD_API int (luaopen_debug) (lua_State *L); - -#define LUA_LOADLIBNAME "package" -LUAMOD_API int (luaopen_package) (lua_State *L); - - -/* open all previous libraries */ -LUALIB_API void (luaL_openlibs) (lua_State *L); - - - -#if !defined(lua_assert) -#define lua_assert(x) ((void)0) -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.c deleted file mode 100644 index edf9eb8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.c +++ /dev/null @@ -1,287 +0,0 @@ -/* -** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#define lundump_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstring.h" -#include "lundump.h" -#include "lzio.h" - - -#if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ -#endif - - -typedef struct { - lua_State *L; - ZIO *Z; - const char *name; -} LoadState; - - -static l_noret error(LoadState *S, const char *why) { - luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); - luaD_throw(S->L, LUA_ERRSYNTAX); -} - - -/* -** All high-level loads go through LoadVector; you can change it to -** adapt to the endianness of the input -*/ -#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) - -static void LoadBlock (LoadState *S, void *b, size_t size) { - if (luaZ_read(S->Z, b, size) != 0) - error(S, "truncated"); -} - - -#define LoadVar(S,x) LoadVector(S,&x,1) - - -static lu_byte LoadByte (LoadState *S) { - lu_byte x; - LoadVar(S, x); - return x; -} - - -static int LoadInt (LoadState *S) { - int x; - LoadVar(S, x); - return x; -} - - -static lua_Number LoadNumber (LoadState *S) { - lua_Number x; - LoadVar(S, x); - return x; -} - - -static lua_Integer LoadInteger (LoadState *S) { - lua_Integer x; - LoadVar(S, x); - return x; -} - - -static TString *LoadString (LoadState *S, Proto *p) { - lua_State *L = S->L; - size_t size = LoadByte(S); - TString *ts; - if (size == 0xFF) - LoadVar(S, size); - if (size == 0) - return NULL; - else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ - char buff[LUAI_MAXSHORTLEN]; - LoadVector(S, buff, size); - ts = luaS_newlstr(L, buff, size); - } - else { /* long string */ - ts = luaS_createlngstrobj(L, size); - setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ - luaD_inctop(L); - LoadVector(S, getstr(ts), size); /* load directly in final place */ - L->top--; /* pop string */ - } - luaC_objbarrier(L, p, ts); - return ts; -} - - -static void LoadCode (LoadState *S, Proto *f) { - int n = LoadInt(S); - f->code = luaM_newvector(S->L, n, Instruction); - f->sizecode = n; - LoadVector(S, f->code, n); -} - - -static void LoadFunction(LoadState *S, Proto *f, TString *psource); - - -static void LoadConstants (LoadState *S, Proto *f) { - int i; - int n = LoadInt(S); - f->k = luaM_newvector(S->L, n, TValue); - f->sizek = n; - for (i = 0; i < n; i++) - setnilvalue(&f->k[i]); - for (i = 0; i < n; i++) { - TValue *o = &f->k[i]; - int t = LoadByte(S); - switch (t) { - case LUA_TNIL: - setnilvalue(o); - break; - case LUA_TBOOLEAN: - setbvalue(o, LoadByte(S)); - break; - case LUA_TNUMFLT: - setfltvalue(o, LoadNumber(S)); - break; - case LUA_TNUMINT: - setivalue(o, LoadInteger(S)); - break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: - setsvalue2n(S->L, o, LoadString(S, f)); - break; - default: - lua_assert(0); - } - } -} - - -static void LoadProtos (LoadState *S, Proto *f) { - int i; - int n = LoadInt(S); - f->p = luaM_newvector(S->L, n, Proto *); - f->sizep = n; - for (i = 0; i < n; i++) - f->p[i] = NULL; - for (i = 0; i < n; i++) { - f->p[i] = luaF_newproto(S->L); - luaC_objbarrier(S->L, f, f->p[i]); - LoadFunction(S, f->p[i], f->source); - } -} - - -static void LoadUpvalues (LoadState *S, Proto *f) { - int i, n; - n = LoadInt(S); - f->upvalues = luaM_newvector(S->L, n, Upvaldesc); - f->sizeupvalues = n; - for (i = 0; i < n; i++) - f->upvalues[i].name = NULL; - for (i = 0; i < n; i++) { - f->upvalues[i].instack = LoadByte(S); - f->upvalues[i].idx = LoadByte(S); - } -} - - -static void LoadDebug (LoadState *S, Proto *f) { - int i, n; - n = LoadInt(S); - f->lineinfo = luaM_newvector(S->L, n, int); - f->sizelineinfo = n; - LoadVector(S, f->lineinfo, n); - n = LoadInt(S); - f->locvars = luaM_newvector(S->L, n, LocVar); - f->sizelocvars = n; - for (i = 0; i < n; i++) - f->locvars[i].varname = NULL; - for (i = 0; i < n; i++) { - f->locvars[i].varname = LoadString(S, f); - f->locvars[i].startpc = LoadInt(S); - f->locvars[i].endpc = LoadInt(S); - } - n = LoadInt(S); - for (i = 0; i < n; i++) - f->upvalues[i].name = LoadString(S, f); -} - - -static void LoadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = LoadString(S, f); - if (f->source == NULL) /* no source in dump? */ - f->source = psource; /* reuse parent's source */ - f->linedefined = LoadInt(S); - f->lastlinedefined = LoadInt(S); - f->numparams = LoadByte(S); - f->is_vararg = LoadByte(S); - f->maxstacksize = LoadByte(S); - LoadCode(S, f); - LoadConstants(S, f); - LoadUpvalues(S, f); - LoadProtos(S, f); - LoadDebug(S, f); -} - - -static void checkliteral (LoadState *S, const char *s, const char *msg) { - char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ - size_t len = strlen(s); - LoadVector(S, buff, len); - if (memcmp(s, buff, len) != 0) - error(S, msg); -} - - -static void fchecksize (LoadState *S, size_t size, const char *tname) { - if (LoadByte(S) != size) - error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); -} - - -#define checksize(S,t) fchecksize(S,sizeof(t),#t) - -static void checkHeader (LoadState *S) { - checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ - if (LoadByte(S) != LUAC_VERSION) - error(S, "version mismatch in"); - if (LoadByte(S) != LUAC_FORMAT) - error(S, "format mismatch in"); - checkliteral(S, LUAC_DATA, "corrupted"); - checksize(S, int); - checksize(S, size_t); - checksize(S, Instruction); - checksize(S, lua_Integer); - checksize(S, lua_Number); - if (LoadInteger(S) != LUAC_INT) - error(S, "endianness mismatch in"); - if (LoadNumber(S) != LUAC_NUM) - error(S, "float format mismatch in"); -} - - -/* -** load precompiled chunk -*/ -LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { - LoadState S; - LClosure *cl; - if (*name == '@' || *name == '=') - S.name = name + 1; - else if (*name == LUA_SIGNATURE[0]) - S.name = "binary string"; - else - S.name = name; - S.L = L; - S.Z = Z; - checkHeader(&S); - cl = luaF_newLclosure(L, LoadByte(&S)); - setclLvalue(L, L->top, cl); - luaD_inctop(L); - cl->p = luaF_newproto(L); - luaC_objbarrier(L, cl, cl->p); - LoadFunction(&S, cl->p, NULL); - lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luai_verifycode(L, buff, cl->p); - return cl; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.h deleted file mode 100644 index ce492d6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lundump.h +++ /dev/null @@ -1,32 +0,0 @@ -/* -** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#ifndef lundump_h -#define lundump_h - -#include "llimits.h" -#include "lobject.h" -#include "lzio.h" - - -/* data to catch conversion errors */ -#define LUAC_DATA "\x19\x93\r\n\x1a\n" - -#define LUAC_INT 0x5678 -#define LUAC_NUM cast_num(370.5) - -#define MYINT(s) (s[0]-'0') -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) -#define LUAC_FORMAT 0 /* this is the official format */ - -/* load one chunk; from lundump.c */ -LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); - -/* dump one chunk; from ldump.c */ -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, - void* data, int strip); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lutf8lib.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lutf8lib.c deleted file mode 100644 index 10bd238..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lutf8lib.c +++ /dev/null @@ -1,256 +0,0 @@ -/* -** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $ -** Standard library for UTF-8 manipulation -** See Copyright Notice in lua.h -*/ - -#define lutf8lib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - -#define MAXUNICODE 0x10FFFF - -#define iscont(p) ((*(p) & 0xC0) == 0x80) - - -/* from strlib */ -/* translate a relative string position: negative means back from end */ -static lua_Integer u_posrelat (lua_Integer pos, size_t len) { - if (pos >= 0) return pos; - else if (0u - (size_t)pos > len) return 0; - else return (lua_Integer)len + pos + 1; -} - - -/* -** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. -*/ -static const char *utf8_decode (const char *o, int *val) { - static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; - const unsigned char *s = (const unsigned char *)o; - unsigned int c = s[0]; - unsigned int res = 0; /* final result */ - if (c < 0x80) /* ascii? */ - res = c; - else { - int count = 0; /* to count number of continuation bytes */ - while (c & 0x40) { /* still have continuation bytes? */ - int cc = s[++count]; /* read next byte */ - if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ - return NULL; /* invalid byte sequence */ - res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ - c <<= 1; /* to test next bit */ - } - res |= ((c & 0x7F) << (count * 5)); /* add first byte */ - if (count > 3 || res > MAXUNICODE || res <= limits[count]) - return NULL; /* invalid byte sequence */ - s += count; /* skip continuation bytes read */ - } - if (val) *val = res; - return (const char *)s + 1; /* +1 to include first byte */ -} - - -/* -** utf8len(s [, i [, j]]) --> number of characters that start in the -** range [i,j], or nil + current position if 's' is not well formed in -** that interval -*/ -static int utflen (lua_State *L) { - int n = 0; - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); - lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); - luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, - "initial position out of string"); - luaL_argcheck(L, --posj < (lua_Integer)len, 3, - "final position out of string"); - while (posi <= posj) { - const char *s1 = utf8_decode(s + posi, NULL); - if (s1 == NULL) { /* conversion error? */ - lua_pushnil(L); /* return nil ... */ - lua_pushinteger(L, posi + 1); /* ... and current position */ - return 2; - } - posi = s1 - s; - n++; - } - lua_pushinteger(L, n); - return 1; -} - - -/* -** codepoint(s, [i, [j]]) -> returns codepoints for all characters -** that start in the range [i,j] -*/ -static int codepoint (lua_State *L) { - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); - lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); - int n; - const char *se; - luaL_argcheck(L, posi >= 1, 2, "out of range"); - luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); - if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ - return luaL_error(L, "string slice too long"); - n = (int)(pose - posi) + 1; - luaL_checkstack(L, n, "string slice too long"); - n = 0; - se = s + pose; - for (s += posi - 1; s < se;) { - int code; - s = utf8_decode(s, &code); - if (s == NULL) - return luaL_error(L, "invalid UTF-8 code"); - lua_pushinteger(L, code); - n++; - } - return n; -} - - -static void pushutfchar (lua_State *L, int arg) { - lua_Integer code = luaL_checkinteger(L, arg); - luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); - lua_pushfstring(L, "%U", (long)code); -} - - -/* -** utfchar(n1, n2, ...) -> char(n1)..char(n2)... -*/ -static int utfchar (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - if (n == 1) /* optimize common case of single char */ - pushutfchar(L, 1); - else { - int i; - luaL_Buffer b; - luaL_buffinit(L, &b); - for (i = 1; i <= n; i++) { - pushutfchar(L, i); - luaL_addvalue(&b); - } - luaL_pushresult(&b); - } - return 1; -} - - -/* -** offset(s, n, [i]) -> index where n-th character counting from -** position 'i' starts; 0 means character at 'i'. -*/ -static int byteoffset (lua_State *L) { - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer n = luaL_checkinteger(L, 2); - lua_Integer posi = (n >= 0) ? 1 : len + 1; - posi = u_posrelat(luaL_optinteger(L, 3, posi), len); - luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, - "position out of range"); - if (n == 0) { - /* find beginning of current byte sequence */ - while (posi > 0 && iscont(s + posi)) posi--; - } - else { - if (iscont(s + posi)) - return luaL_error(L, "initial position is a continuation byte"); - if (n < 0) { - while (n < 0 && posi > 0) { /* move back */ - do { /* find beginning of previous character */ - posi--; - } while (posi > 0 && iscont(s + posi)); - n++; - } - } - else { - n--; /* do not move for 1st character */ - while (n > 0 && posi < (lua_Integer)len) { - do { /* find beginning of next character */ - posi++; - } while (iscont(s + posi)); /* (cannot pass final '\0') */ - n--; - } - } - } - if (n == 0) /* did it find given character? */ - lua_pushinteger(L, posi + 1); - else /* no such character */ - lua_pushnil(L); - return 1; -} - - -static int iter_aux (lua_State *L) { - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer n = lua_tointeger(L, 2) - 1; - if (n < 0) /* first iteration? */ - n = 0; /* start from here */ - else if (n < (lua_Integer)len) { - n++; /* skip current byte */ - while (iscont(s + n)) n++; /* and its continuations */ - } - if (n >= (lua_Integer)len) - return 0; /* no more codepoints */ - else { - int code; - const char *next = utf8_decode(s + n, &code); - if (next == NULL || iscont(next)) - return luaL_error(L, "invalid UTF-8 code"); - lua_pushinteger(L, n + 1); - lua_pushinteger(L, code); - return 2; - } -} - - -static int iter_codes (lua_State *L) { - luaL_checkstring(L, 1); - lua_pushcfunction(L, iter_aux); - lua_pushvalue(L, 1); - lua_pushinteger(L, 0); - return 3; -} - - -/* pattern to match a single UTF-8 character */ -#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" - - -static const luaL_Reg funcs[] = { - {"offset", byteoffset}, - {"codepoint", codepoint}, - {"char", utfchar}, - {"len", utflen}, - {"codes", iter_codes}, - /* placeholders */ - {"charpattern", NULL}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_utf8 (lua_State *L) { - luaL_newlib(L, funcs); - lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); - lua_setfield(L, -2, "charpattern"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.c deleted file mode 100644 index cc43d87..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.c +++ /dev/null @@ -1,1322 +0,0 @@ -/* -** $Id: lvm.c,v 2.268.1.1 2017/04/19 17:39:34 roberto Exp $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#define lvm_c -#define LUA_CORE - -#include "lprefix.h" - -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - -/* limit for table tag-method chains (to avoid loops) */ -#define MAXTAGLOOP 2000 - - - -/* -** 'l_intfitsf' checks whether a given integer can be converted to a -** float without rounding. Used in comparisons. Left undefined if -** all integers fit in a float precisely. -*/ -#if !defined(l_intfitsf) - -/* number of bits in the mantissa of a float */ -#define NBM (l_mathlim(MANT_DIG)) - -/* -** Check whether some integers may not fit in a float, that is, whether -** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). -** (The shifts are done in parts to avoid shifting by more than the size -** of an integer. In a worst case, NBM == 113 for long double and -** sizeof(integer) == 32.) -*/ -#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ - >> (NBM - (3 * (NBM / 4)))) > 0 - -#define l_intfitsf(i) \ - (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) - -#endif - -#endif - - - -/* -** Try to convert a value to a float. The float case is already handled -** by the macro 'tonumber'. -*/ -int luaV_tonumber_ (const TValue *obj, lua_Number *n) { - TValue v; - if (ttisinteger(obj)) { - *n = cast_num(ivalue(obj)); - return 1; - } - else if (cvt2num(obj) && /* string convertible to number? */ - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { - *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ - return 1; - } - else - return 0; /* conversion failed */ -} - - -/* -** try to convert a value to an integer, rounding according to 'mode': -** mode == 0: accepts only integral values -** mode == 1: takes the floor of the number -** mode == 2: takes the ceil of the number -*/ -int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { - TValue v; - again: - if (ttisfloat(obj)) { - lua_Number n = fltvalue(obj); - lua_Number f = l_floor(n); - if (n != f) { /* not an integral value? */ - if (mode == 0) return 0; /* fails if mode demands integral value */ - else if (mode > 1) /* needs ceil? */ - f += 1; /* convert floor to ceil (remember: n != f) */ - } - return lua_numbertointeger(f, p); - } - else if (ttisinteger(obj)) { - *p = ivalue(obj); - return 1; - } - else if (cvt2num(obj) && - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { - obj = &v; - goto again; /* convert result from 'luaO_str2num' to an integer */ - } - return 0; /* conversion failed */ -} - - -/* -** Try to convert a 'for' limit to an integer, preserving the -** semantics of the loop. -** (The following explanation assumes a non-negative step; it is valid -** for negative steps mutatis mutandis.) -** If the limit can be converted to an integer, rounding down, that is -** it. -** Otherwise, check whether the limit can be converted to a number. If -** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, -** which means no limit. If the number is too negative, the loop -** should not run, because any initial integer value is larger than the -** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects -** the extreme case when the initial value is LUA_MININTEGER, in which -** case the LUA_MININTEGER limit would still run the loop once. -*/ -static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, - int *stopnow) { - *stopnow = 0; /* usually, let loops run */ - if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ - lua_Number n; /* try to convert to float */ - if (!tonumber(obj, &n)) /* cannot convert to float? */ - return 0; /* not a number */ - if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ - *p = LUA_MAXINTEGER; - if (step < 0) *stopnow = 1; - } - else { /* float is smaller than min integer */ - *p = LUA_MININTEGER; - if (step >= 0) *stopnow = 1; - } - } - return 1; -} - - -/* -** Finish the table access 'val = t[key]'. -** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be nil). -*/ -void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - const TValue *slot) { - int loop; /* counter to avoid infinite loops */ - const TValue *tm; /* metamethod */ - for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (slot == NULL) { /* 't' is not a table? */ - lua_assert(!ttistable(t)); - tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (ttisnil(tm)) - luaG_typeerror(L, t, "index"); /* no metamethod */ - /* else will try the metamethod */ - } - else { /* 't' is a table */ - lua_assert(ttisnil(slot)); - tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ - if (tm == NULL) { /* no metamethod? */ - setnilvalue(val); /* result is nil */ - return; - } - /* else will try the metamethod */ - } - if (ttisfunction(tm)) { /* is metamethod a function? */ - luaT_callTM(L, tm, t, key, val, 1); /* call it */ - return; - } - t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ - setobj2s(L, val, slot); /* done */ - return; - } - /* else repeat (tail call 'luaV_finishget') */ - } - luaG_runerror(L, "'__index' chain too long; possible loop"); -} - - -/* -** Finish a table assignment 't[key] = val'. -** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to 'luaO_nilobject' if there is no such -** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' -** would have done the job.) -*/ -void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot) { - int loop; /* counter to avoid infinite loops */ - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; /* '__newindex' metamethod */ - if (slot != NULL) { /* is 't' a table? */ - Table *h = hvalue(t); /* save 't' table */ - lua_assert(ttisnil(slot)); /* old value must be nil */ - tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ - if (tm == NULL) { /* no metamethod? */ - if (slot == luaO_nilobject) /* no previous entry? */ - slot = luaH_newkey(L, h, key); /* create one */ - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, slot), val); /* set its new value */ - invalidateTMcache(h); - luaC_barrierback(L, h, val); - return; - } - /* else will try the metamethod */ - } - else { /* not a table; check metamethod */ - if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) - luaG_typeerror(L, t, "index"); - } - /* try the metamethod */ - if (ttisfunction(tm)) { - luaT_callTM(L, tm, t, key, val, 0); - return; - } - t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastset(L, t, key, slot, luaH_get, val)) - return; /* done */ - /* else loop */ - } - luaG_runerror(L, "'__newindex' chain too long; possible loop"); -} - - -/* -** Compare two strings 'ls' x 'rs', returning an integer smaller-equal- -** -larger than zero if 'ls' is smaller-equal-larger than 'rs'. -** The code is a little tricky because it allows '\0' in the strings -** and it uses 'strcoll' (to respect locales) for each segments -** of the strings. -*/ -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = tsslen(ls); - const char *r = getstr(rs); - size_t lr = tsslen(rs); - for (;;) { /* for each segment */ - int temp = strcoll(l, r); - if (temp != 0) /* not equal? */ - return temp; /* done */ - else { /* strings are equal up to a '\0' */ - size_t len = strlen(l); /* index of first '\0' in both strings */ - if (len == lr) /* 'rs' is finished? */ - return (len == ll) ? 0 : 1; /* check 'ls' */ - else if (len == ll) /* 'ls' is finished? */ - return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ - /* both strings longer than 'len'; go on comparing after the '\0' */ - len++; - l += len; ll -= len; r += len; lr -= len; - } - } -} - - -/* -** Check whether integer 'i' is less than float 'f'. If 'i' has an -** exact representation as a float ('l_intfitsf'), compare numbers as -** floats. Otherwise, if 'f' is outside the range for integers, result -** is trivial. Otherwise, compare them as integers. (When 'i' has no -** float representation, either 'f' is "far away" from 'i' or 'f' has -** no precision left for a fractional part; either way, how 'f' is -** truncated is irrelevant.) When 'f' is NaN, comparisons must result -** in false. -*/ -static int LTintfloat (lua_Integer i, lua_Number f) { -#if defined(l_intfitsf) - if (!l_intfitsf(i)) { - if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ - return 1; /* f >= maxint + 1 > i */ - else if (f > cast_num(LUA_MININTEGER)) /* minint < f <= maxint ? */ - return (i < cast(lua_Integer, f)); /* compare them as integers */ - else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */ - return 0; - } -#endif - return luai_numlt(cast_num(i), f); /* compare them as floats */ -} - - -/* -** Check whether integer 'i' is less than or equal to float 'f'. -** See comments on previous function. -*/ -static int LEintfloat (lua_Integer i, lua_Number f) { -#if defined(l_intfitsf) - if (!l_intfitsf(i)) { - if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ - return 1; /* f >= maxint + 1 > i */ - else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */ - return (i <= cast(lua_Integer, f)); /* compare them as integers */ - else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */ - return 0; - } -#endif - return luai_numle(cast_num(i), f); /* compare them as floats */ -} - - -/* -** Return 'l < r', for numbers. -*/ -static int LTnum (const TValue *l, const TValue *r) { - if (ttisinteger(l)) { - lua_Integer li = ivalue(l); - if (ttisinteger(r)) - return li < ivalue(r); /* both are integers */ - else /* 'l' is int and 'r' is float */ - return LTintfloat(li, fltvalue(r)); /* l < r ? */ - } - else { - lua_Number lf = fltvalue(l); /* 'l' must be float */ - if (ttisfloat(r)) - return luai_numlt(lf, fltvalue(r)); /* both are float */ - else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ - return 0; /* NaN < i is always false */ - else /* without NaN, (l < r) <--> not(r <= l) */ - return !LEintfloat(ivalue(r), lf); /* not (r <= l) ? */ - } -} - - -/* -** Return 'l <= r', for numbers. -*/ -static int LEnum (const TValue *l, const TValue *r) { - if (ttisinteger(l)) { - lua_Integer li = ivalue(l); - if (ttisinteger(r)) - return li <= ivalue(r); /* both are integers */ - else /* 'l' is int and 'r' is float */ - return LEintfloat(li, fltvalue(r)); /* l <= r ? */ - } - else { - lua_Number lf = fltvalue(l); /* 'l' must be float */ - if (ttisfloat(r)) - return luai_numle(lf, fltvalue(r)); /* both are float */ - else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ - return 0; /* NaN <= i is always false */ - else /* without NaN, (l <= r) <--> not(r < l) */ - return !LTintfloat(ivalue(r), lf); /* not (r < l) ? */ - } -} - - -/* -** Main operation less than; return 'l < r'. -*/ -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { - int res; - if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ - return LTnum(l, r); - else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ - return l_strcmp(tsvalue(l), tsvalue(r)) < 0; - else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ - luaG_ordererror(L, l, r); /* error */ - return res; -} - - -/* -** Main operation less than or equal to; return 'l <= r'. If it needs -** a metamethod and there is no '__le', try '__lt', based on -** l <= r iff !(r < l) (assuming a total order). If the metamethod -** yields during this substitution, the continuation has to know -** about it (to negate the result of r= 0) /* try 'le' */ - return res; - else { /* try 'lt': */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - res = luaT_callorderTM(L, r, l, TM_LT); - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - if (res < 0) - luaG_ordererror(L, l, r); - return !res; /* result is negated */ - } -} - - -/* -** Main operation for equality of Lua values; return 't1 == t2'. -** L == NULL means raw equality (no metamethods) -*/ -int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { - const TValue *tm; - if (ttype(t1) != ttype(t2)) { /* not the same variant? */ - if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) - return 0; /* only numbers can be equal with different variants */ - else { /* two numbers with different variants */ - lua_Integer i1, i2; /* compare them as integers */ - return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); - } - } - /* values have same type and same variant */ - switch (ttype(t1)) { - case LUA_TNIL: return 1; - case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); - case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); - case LUA_TLCF: return fvalue(t1) == fvalue(t2); - case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); - case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); - case LUA_TUSERDATA: { - if (uvalue(t1) == uvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - case LUA_TTABLE: { - if (hvalue(t1) == hvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - default: - return gcvalue(t1) == gcvalue(t2); - } - if (tm == NULL) /* no TM? */ - return 0; /* objects are different */ - luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ - return !l_isfalse(L->top); -} - - -/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ -#define tostring(L,o) \ - (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) - -#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) - -/* copy strings in stack from top - n up to top - 1 to buffer */ -static void copy2buff (StkId top, int n, char *buff) { - size_t tl = 0; /* size already copied */ - do { - size_t l = vslen(top - n); /* length of string being copied */ - memcpy(buff + tl, svalue(top - n), l * sizeof(char)); - tl += l; - } while (--n > 0); -} - - -/* -** Main operation for concatenation: concat 'total' values in the stack, -** from 'L->top - total' up to 'L->top - 1'. -*/ -void luaV_concat (lua_State *L, int total) { - lua_assert(total >= 2); - do { - StkId top = L->top; - int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) - luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); - else if (isemptystr(top - 1)) /* second operand is empty? */ - cast_void(tostring(L, top - 2)); /* result is first operand */ - else if (isemptystr(top - 2)) { /* first operand is an empty string? */ - setobjs2s(L, top - 2, top - 1); /* result is second op. */ - } - else { - /* at least two non-empty string values; get as many as possible */ - size_t tl = vslen(top - 1); - TString *ts; - /* collect total length and number of strings */ - for (n = 1; n < total && tostring(L, top - n - 1); n++) { - size_t l = vslen(top - n - 1); - if (l >= (MAX_SIZE/sizeof(char)) - tl) - luaG_runerror(L, "string length overflow"); - tl += l; - } - if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ - char buff[LUAI_MAXSHORTLEN]; - copy2buff(top, n, buff); /* copy strings to buffer */ - ts = luaS_newlstr(L, buff, tl); - } - else { /* long string; copy strings directly to final result */ - ts = luaS_createlngstrobj(L, tl); - copy2buff(top, n, getstr(ts)); - } - setsvalue2s(L, top - n, ts); /* create result */ - } - total -= n-1; /* got 'n' strings to create 1 new */ - L->top -= n-1; /* popped 'n' strings and pushed one */ - } while (total > 1); /* repeat until only 1 result left */ -} - - -/* -** Main operation 'ra' = #rb'. -*/ -void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { - const TValue *tm; - switch (ttype(rb)) { - case LUA_TTABLE: { - Table *h = hvalue(rb); - tm = fasttm(L, h->metatable, TM_LEN); - if (tm) break; /* metamethod? break switch to call it */ - setivalue(ra, luaH_getn(h)); /* else primitive len */ - return; - } - case LUA_TSHRSTR: { - setivalue(ra, tsvalue(rb)->shrlen); - return; - } - case LUA_TLNGSTR: { - setivalue(ra, tsvalue(rb)->u.lnglen); - return; - } - default: { /* try metamethod */ - tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (ttisnil(tm)) /* no metamethod? */ - luaG_typeerror(L, rb, "get length of"); - break; - } - } - luaT_callTM(L, tm, rb, rb, ra, 1); -} - - -/* -** Integer division; return 'm // n', that is, floor(m/n). -** C division truncates its result (rounds towards zero). -** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, -** otherwise 'floor(q) == trunc(q) - 1'. -*/ -lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { - if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ - if (n == 0) - luaG_runerror(L, "attempt to divide by zero"); - return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ - } - else { - lua_Integer q = m / n; /* perform C division */ - if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ - q -= 1; /* correct result for different rounding */ - return q; - } -} - - -/* -** Integer modulus; return 'm % n'. (Assume that C '%' with -** negative operands follows C99 behavior. See previous comment -** about luaV_div.) -*/ -lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { - if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ - if (n == 0) - luaG_runerror(L, "attempt to perform 'n%%0'"); - return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ - } - else { - lua_Integer r = m % n; - if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ - r += n; /* correct result for different rounding */ - return r; - } -} - - -/* number of bits in an integer */ -#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) - -/* -** Shift left operation. (Shift right just negates 'y'.) -*/ -lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { - if (y < 0) { /* shift right? */ - if (y <= -NBITS) return 0; - else return intop(>>, x, -y); - } - else { /* shift left */ - if (y >= NBITS) return 0; - else return intop(<<, x, y); - } -} - - -/* -** check whether cached closure in prototype 'p' may be reused, that is, -** whether there is a cached closure with the same upvalues needed by -** new closure to be created. -*/ -static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { - LClosure *c = p->cache; - if (c != NULL) { /* is there a cached closure? */ - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; - int i; - for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ - TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; - if (c->upvals[i]->v != v) - return NULL; /* wrong upvalue; cannot reuse closure */ - } - } - return c; /* return cached closure (or NULL if no cached closure) */ -} - - -/* -** create a new Lua closure, push it in the stack, and initialize -** its upvalues. Note that the closure is not cached if prototype is -** already black (which means that 'cache' was already cleared by the -** GC). -*/ -static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, - StkId ra) { - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; - int i; - LClosure *ncl = luaF_newLclosure(L, nup); - ncl->p = p; - setclLvalue(L, ra, ncl); /* anchor new closure in stack */ - for (i = 0; i < nup; i++) { /* fill in its upvalues */ - if (uv[i].instack) /* upvalue refers to local variable? */ - ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); - else /* get upvalue from enclosing function */ - ncl->upvals[i] = encup[uv[i].idx]; - ncl->upvals[i]->refcount++; - /* new closure is white, so we do not need a barrier here */ - } - if (!isblack(p)) /* cache will not break GC invariant? */ - p->cache = ncl; /* save it on cache for reuse */ -} - - -/* -** finish execution of an opcode interrupted by an yield -*/ -void luaV_finishOp (lua_State *L) { - CallInfo *ci = L->ci; - StkId base = ci->u.l.base; - Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ - OpCode op = GET_OPCODE(inst); - switch (op) { /* finish its execution */ - case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: - case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: - case OP_MOD: case OP_POW: - case OP_UNM: case OP_BNOT: case OP_LEN: - case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { - setobjs2s(L, base + GETARG_A(inst), --L->top); - break; - } - case OP_LE: case OP_LT: case OP_EQ: { - int res = !l_isfalse(L->top - 1); - L->top--; - if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - lua_assert(op == OP_LE); - ci->callstatus ^= CIST_LEQ; /* clear mark */ - res = !res; /* negate result */ - } - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_A(inst)) /* condition failed? */ - ci->u.l.savedpc++; /* skip jump instruction */ - break; - } - case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ - int b = GETARG_B(inst); /* first element to concatenate */ - int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ - setobj2s(L, top - 2, top); /* put TM result in proper position */ - if (total > 1) { /* are there elements to concat? */ - L->top = top - 1; /* top is one after last element (at top-2) */ - luaV_concat(L, total); /* concat them (may yield again) */ - } - /* move final result to final position */ - setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1); - L->top = ci->top; /* restore top */ - break; - } - case OP_TFORCALL: { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); - L->top = ci->top; /* correct top */ - break; - } - case OP_CALL: { - if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ - L->top = ci->top; /* adjust results */ - break; - } - case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: - break; - default: lua_assert(0); - } -} - - - - -/* -** {================================================================== -** Function 'luaV_execute': main interpreter loop -** =================================================================== -*/ - - -/* -** some macros for common tasks in 'luaV_execute' -*/ - - -#define RA(i) (base+GETARG_A(i)) -#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) -#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) -#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) -#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) - - -/* execute a jump instruction */ -#define dojump(ci,i,e) \ - { int a = GETARG_A(i); \ - if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \ - ci->u.l.savedpc += GETARG_sBx(i) + e; } - -/* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); } - - -#define Protect(x) { {x;}; base = ci->u.l.base; } - -#define checkGC(L,c) \ - { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - Protect(L->top = ci->top)); /* restore top */ \ - luai_threadyield(L); } - - -/* fetch an instruction and prepare its execution */ -#define vmfetch() { \ - i = *(ci->u.l.savedpc++); \ - if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) \ - Protect(luaG_traceexec(L)); \ - ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ - lua_assert(base == ci->u.l.base); \ - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); \ -} - -#define vmdispatch(o) switch(o) -#define vmcase(l) case l: -#define vmbreak break - - -/* -** copy of 'luaV_gettable', but protecting the call to potential -** metamethod (which can reallocate the stack) -*/ -#define gettableProtected(L,t,k,v) { const TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else Protect(luaV_finishget(L,t,k,v,slot)); } - - -/* same for 'luaV_settable' */ -#define settableProtected(L,t,k,v) { const TValue *slot; \ - if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ - Protect(luaV_finishset(L,t,k,v,slot)); } - - - -void luaV_execute (lua_State *L) { - CallInfo *ci = L->ci; - LClosure *cl; - TValue *k; - StkId base; - ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ - newframe: /* reentry point when frame changes (call/return) */ - lua_assert(ci == L->ci); - cl = clLvalue(ci->func); /* local reference to function's closure */ - k = cl->p->k; /* local reference to function's constant table */ - base = ci->u.l.base; /* local copy of function's base */ - /* main loop of interpreter */ - for (;;) { - Instruction i; - StkId ra; - vmfetch(); - vmdispatch (GET_OPCODE(i)) { - vmcase(OP_MOVE) { - setobjs2s(L, ra, RB(i)); - vmbreak; - } - vmcase(OP_LOADK) { - TValue *rb = k + GETARG_Bx(i); - setobj2s(L, ra, rb); - vmbreak; - } - vmcase(OP_LOADKX) { - TValue *rb; - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - rb = k + GETARG_Ax(*ci->u.l.savedpc++); - setobj2s(L, ra, rb); - vmbreak; - } - vmcase(OP_LOADBOOL) { - setbvalue(ra, GETARG_B(i)); - if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ - vmbreak; - } - vmcase(OP_LOADNIL) { - int b = GETARG_B(i); - do { - setnilvalue(ra++); - } while (b--); - vmbreak; - } - vmcase(OP_GETUPVAL) { - int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); - vmbreak; - } - vmcase(OP_GETTABUP) { - TValue *upval = cl->upvals[GETARG_B(i)]->v; - TValue *rc = RKC(i); - gettableProtected(L, upval, rc, ra); - vmbreak; - } - vmcase(OP_GETTABLE) { - StkId rb = RB(i); - TValue *rc = RKC(i); - gettableProtected(L, rb, rc, ra); - vmbreak; - } - vmcase(OP_SETTABUP) { - TValue *upval = cl->upvals[GETARG_A(i)]->v; - TValue *rb = RKB(i); - TValue *rc = RKC(i); - settableProtected(L, upval, rb, rc); - vmbreak; - } - vmcase(OP_SETUPVAL) { - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, ra); - luaC_upvalbarrier(L, uv); - vmbreak; - } - vmcase(OP_SETTABLE) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - settableProtected(L, ra, rb, rc); - vmbreak; - } - vmcase(OP_NEWTABLE) { - int b = GETARG_B(i); - int c = GETARG_C(i); - Table *t = luaH_new(L); - sethvalue(L, ra, t); - if (b != 0 || c != 0) - luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); - checkGC(L, ra + 1); - vmbreak; - } - vmcase(OP_SELF) { - const TValue *aux; - StkId rb = RB(i); - TValue *rc = RKC(i); - TString *key = tsvalue(rc); /* key must be a string */ - setobjs2s(L, ra + 1, rb); - if (luaV_fastget(L, rb, key, aux, luaH_getstr)) { - setobj2s(L, ra, aux); - } - else Protect(luaV_finishget(L, rb, rc, ra, aux)); - vmbreak; - } - vmcase(OP_ADD) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, intop(+, ib, ic)); - } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numadd(L, nb, nc)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } - vmbreak; - } - vmcase(OP_SUB) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, intop(-, ib, ic)); - } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numsub(L, nb, nc)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } - vmbreak; - } - vmcase(OP_MUL) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, intop(*, ib, ic)); - } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_nummul(L, nb, nc)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } - vmbreak; - } - vmcase(OP_DIV) { /* float division (always with floats) */ - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numdiv(L, nb, nc)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } - vmbreak; - } - vmcase(OP_BAND) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, intop(&, ib, ic)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } - vmbreak; - } - vmcase(OP_BOR) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, intop(|, ib, ic)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } - vmbreak; - } - vmcase(OP_BXOR) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, intop(^, ib, ic)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } - vmbreak; - } - vmcase(OP_SHL) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, luaV_shiftl(ib, ic)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } - vmbreak; - } - vmcase(OP_SHR) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, luaV_shiftl(ib, -ic)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } - vmbreak; - } - vmcase(OP_MOD) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, luaV_mod(L, ib, ic)); - } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - lua_Number m; - luai_nummod(L, nb, nc, m); - setfltvalue(ra, m); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } - vmbreak; - } - vmcase(OP_IDIV) { /* floor division */ - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, luaV_div(L, ib, ic)); - } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numidiv(L, nb, nc)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } - vmbreak; - } - vmcase(OP_POW) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - lua_Number nb; lua_Number nc; - if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numpow(L, nb, nc)); - } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } - vmbreak; - } - vmcase(OP_UNM) { - TValue *rb = RB(i); - lua_Number nb; - if (ttisinteger(rb)) { - lua_Integer ib = ivalue(rb); - setivalue(ra, intop(-, 0, ib)); - } - else if (tonumber(rb, &nb)) { - setfltvalue(ra, luai_numunm(L, nb)); - } - else { - Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); - } - vmbreak; - } - vmcase(OP_BNOT) { - TValue *rb = RB(i); - lua_Integer ib; - if (tointeger(rb, &ib)) { - setivalue(ra, intop(^, ~l_castS2U(0), ib)); - } - else { - Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); - } - vmbreak; - } - vmcase(OP_NOT) { - TValue *rb = RB(i); - int res = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(ra, res); - vmbreak; - } - vmcase(OP_LEN) { - Protect(luaV_objlen(L, ra, RB(i))); - vmbreak; - } - vmcase(OP_CONCAT) { - int b = GETARG_B(i); - int c = GETARG_C(i); - StkId rb; - L->top = base + c + 1; /* mark the end of concat operands */ - Protect(luaV_concat(L, c - b + 1)); - ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */ - rb = base + b; - setobjs2s(L, ra, rb); - checkGC(L, (ra >= rb ? ra + 1 : rb)); - L->top = ci->top; /* restore top */ - vmbreak; - } - vmcase(OP_JMP) { - dojump(ci, i, 0); - vmbreak; - } - vmcase(OP_EQ) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); - Protect( - if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - vmbreak; - } - vmcase(OP_LT) { - Protect( - if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - vmbreak; - } - vmcase(OP_LE) { - Protect( - if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; - else - donextjump(ci); - ) - vmbreak; - } - vmcase(OP_TEST) { - if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) - ci->u.l.savedpc++; - else - donextjump(ci); - vmbreak; - } - vmcase(OP_TESTSET) { - TValue *rb = RB(i); - if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) - ci->u.l.savedpc++; - else { - setobjs2s(L, ra, rb); - donextjump(ci); - } - vmbreak; - } - vmcase(OP_CALL) { - int b = GETARG_B(i); - int nresults = GETARG_C(i) - 1; - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - if (luaD_precall(L, ra, nresults)) { /* C function? */ - if (nresults >= 0) - L->top = ci->top; /* adjust results */ - Protect((void)0); /* update 'base' */ - } - else { /* Lua function */ - ci = L->ci; - goto newframe; /* restart luaV_execute over new Lua function */ - } - vmbreak; - } - vmcase(OP_TAILCALL) { - int b = GETARG_B(i); - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ - Protect((void)0); /* update 'base' */ - } - else { - /* tail call: put called frame (n) in place of caller one (o) */ - CallInfo *nci = L->ci; /* called frame */ - CallInfo *oci = nci->previous; /* caller frame */ - StkId nfunc = nci->func; /* called function */ - StkId ofunc = oci->func; /* caller function */ - /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->numparams; - int aux; - /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); - /* move new frame into old one */ - for (aux = 0; nfunc + aux < lim; aux++) - setobjs2s(L, ofunc + aux, nfunc + aux); - oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */ - oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ - oci->u.l.savedpc = nci->u.l.savedpc; - oci->callstatus |= CIST_TAIL; /* function was tail called */ - ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); - goto newframe; /* restart luaV_execute over new Lua function */ - } - vmbreak; - } - vmcase(OP_RETURN) { - int b = GETARG_B(i); - if (cl->p->sizep > 0) luaF_close(L, base); - b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); - if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ - return; /* external invocation: return */ - else { /* invocation via reentry: continue execution */ - ci = L->ci; - if (b) L->top = ci->top; - lua_assert(isLua(ci)); - lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); - goto newframe; /* restart luaV_execute over new Lua function */ - } - } - vmcase(OP_FORLOOP) { - if (ttisinteger(ra)) { /* integer loop? */ - lua_Integer step = ivalue(ra + 2); - lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */ - lua_Integer limit = ivalue(ra + 1); - if ((0 < step) ? (idx <= limit) : (limit <= idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - chgivalue(ra, idx); /* update internal index... */ - setivalue(ra + 3, idx); /* ...and external index */ - } - } - else { /* floating loop */ - lua_Number step = fltvalue(ra + 2); - lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */ - lua_Number limit = fltvalue(ra + 1); - if (luai_numlt(0, step) ? luai_numle(idx, limit) - : luai_numle(limit, idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - chgfltvalue(ra, idx); /* update internal index... */ - setfltvalue(ra + 3, idx); /* ...and external index */ - } - } - vmbreak; - } - vmcase(OP_FORPREP) { - TValue *init = ra; - TValue *plimit = ra + 1; - TValue *pstep = ra + 2; - lua_Integer ilimit; - int stopnow; - if (ttisinteger(init) && ttisinteger(pstep) && - forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { - /* all values are integer */ - lua_Integer initv = (stopnow ? 0 : ivalue(init)); - setivalue(plimit, ilimit); - setivalue(init, intop(-, initv, ivalue(pstep))); - } - else { /* try making all values floats */ - lua_Number ninit; lua_Number nlimit; lua_Number nstep; - if (!tonumber(plimit, &nlimit)) - luaG_runerror(L, "'for' limit must be a number"); - setfltvalue(plimit, nlimit); - if (!tonumber(pstep, &nstep)) - luaG_runerror(L, "'for' step must be a number"); - setfltvalue(pstep, nstep); - if (!tonumber(init, &ninit)) - luaG_runerror(L, "'for' initial value must be a number"); - setfltvalue(init, luai_numsub(L, ninit, nstep)); - } - ci->u.l.savedpc += GETARG_sBx(i); - vmbreak; - } - vmcase(OP_TFORCALL) { - StkId cb = ra + 3; /* call base */ - setobjs2s(L, cb+2, ra+2); - setobjs2s(L, cb+1, ra+1); - setobjs2s(L, cb, ra); - L->top = cb + 3; /* func. + 2 args (state and index) */ - Protect(luaD_call(L, cb, GETARG_C(i))); - L->top = ci->top; - i = *(ci->u.l.savedpc++); /* go to next instruction */ - ra = RA(i); - lua_assert(GET_OPCODE(i) == OP_TFORLOOP); - goto l_tforloop; - } - vmcase(OP_TFORLOOP) { - l_tforloop: - if (!ttisnil(ra + 1)) { /* continue loop? */ - setobjs2s(L, ra, ra + 1); /* save control variable */ - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ - } - vmbreak; - } - vmcase(OP_SETLIST) { - int n = GETARG_B(i); - int c = GETARG_C(i); - unsigned int last; - Table *h; - if (n == 0) n = cast_int(L->top - ra) - 1; - if (c == 0) { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - c = GETARG_Ax(*ci->u.l.savedpc++); - } - h = hvalue(ra); - last = ((c-1)*LFIELDS_PER_FLUSH) + n; - if (last > h->sizearray) /* needs more space? */ - luaH_resizearray(L, h, last); /* preallocate it at once */ - for (; n > 0; n--) { - TValue *val = ra+n; - luaH_setint(L, h, last--, val); - luaC_barrierback(L, h, val); - } - L->top = ci->top; /* correct top (in case of previous open call) */ - vmbreak; - } - vmcase(OP_CLOSURE) { - Proto *p = cl->p->p[GETARG_Bx(i)]; - LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ - if (ncl == NULL) /* no match? */ - pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ - else - setclLvalue(L, ra, ncl); /* push cashed closure */ - checkGC(L, ra + 1); - vmbreak; - } - vmcase(OP_VARARG) { - int b = GETARG_B(i) - 1; /* required results */ - int j; - int n = cast_int(base - ci->func) - cl->p->numparams - 1; - if (n < 0) /* less arguments than parameters? */ - n = 0; /* no vararg arguments */ - if (b < 0) { /* B == 0? */ - b = n; /* get all var. arguments */ - Protect(luaD_checkstack(L, n)); - ra = RA(i); /* previous call may change the stack */ - L->top = ra + n; - } - for (j = 0; j < b && j < n; j++) - setobjs2s(L, ra + j, base - n + j); - for (; j < b; j++) /* complete required results with nil */ - setnilvalue(ra + j); - vmbreak; - } - vmcase(OP_EXTRAARG) { - lua_assert(0); - vmbreak; - } - } - } -} - -/* }================================================================== */ - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.h deleted file mode 100644 index a8f954f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lvm.h +++ /dev/null @@ -1,113 +0,0 @@ -/* -** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lvm_h -#define lvm_h - - -#include "ldo.h" -#include "lobject.h" -#include "ltm.h" - - -#if !defined(LUA_NOCVTN2S) -#define cvt2str(o) ttisnumber(o) -#else -#define cvt2str(o) 0 /* no conversion from numbers to strings */ -#endif - - -#if !defined(LUA_NOCVTS2N) -#define cvt2num(o) ttisstring(o) -#else -#define cvt2num(o) 0 /* no conversion from strings to numbers */ -#endif - - -/* -** You can define LUA_FLOORN2I if you want to convert floats to integers -** by flooring them (instead of raising an error if they are not -** integral values) -*/ -#if !defined(LUA_FLOORN2I) -#define LUA_FLOORN2I 0 -#endif - - -#define tonumber(o,n) \ - (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) - -#define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) - -#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) - -#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) - - -/* -** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, -** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, -** return 0 (meaning it will have to check metamethod) with 'slot' -** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). -** 'f' is the raw get function to use. -*/ -#define luaV_fastget(L,t,k,slot,f) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !ttisnil(slot))) /* result not nil? */ - -/* -** standard implementation for 'gettable' -*/ -#define luaV_gettable(L,t,k,v) { const TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else luaV_finishget(L,t,k,v,slot); } - - -/* -** Fast track for set table. If 't' is a table and 't[k]' is not nil, -** call GC barrier, do a raw 't[k]=v', and return true; otherwise, -** return false with 'slot' equal to NULL (if 't' is not a table) or -** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro -** returns true, there is no need to 'invalidateTMcache', because the -** call is not creating a new entry. -*/ -#define luaV_fastset(L,t,k,slot,f,v) \ - (!ttistable(t) \ - ? (slot = NULL, 0) \ - : (slot = f(hvalue(t), k), \ - ttisnil(slot) ? 0 \ - : (luaC_barrierback(L, hvalue(t), v), \ - setobj2t(L, cast(TValue *,slot), v), \ - 1))) - - -#define luaV_settable(L,t,k,v) { const TValue *slot; \ - if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ - luaV_finishset(L,t,k,v,slot); } - - - -LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); -LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); -LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); -LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); -LUAI_FUNC void luaV_finishOp (lua_State *L); -LUAI_FUNC void luaV_execute (lua_State *L); -LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); -LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); -LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); -LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.c b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.c deleted file mode 100644 index 6f79094..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.c +++ /dev/null @@ -1,68 +0,0 @@ -/* -** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - -#define lzio_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "llimits.h" -#include "lmem.h" -#include "lstate.h" -#include "lzio.h" - - -int luaZ_fill (ZIO *z) { - size_t size; - lua_State *L = z->L; - const char *buff; - lua_unlock(L); - buff = z->reader(L, z->data, &size); - lua_lock(L); - if (buff == NULL || size == 0) - return EOZ; - z->n = size - 1; /* discount char being returned */ - z->p = buff; - return cast_uchar(*(z->p++)); -} - - -void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { - z->L = L; - z->reader = reader; - z->data = data; - z->n = 0; - z->p = NULL; -} - - -/* --------------------------------------------------------------- read --- */ -size_t luaZ_read (ZIO *z, void *b, size_t n) { - while (n) { - size_t m; - if (z->n == 0) { /* no bytes in buffer? */ - if (luaZ_fill(z) == EOZ) /* try to read more */ - return n; /* no more input; return number of missing bytes */ - else { - z->n++; /* luaZ_fill consumed first byte; put it back */ - z->p--; - } - } - m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ - memcpy(b, z->p, m); - z->n -= m; - z->p += m; - b = (char *)b + m; - n -= m; - } - return 0; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.h b/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.h deleted file mode 100644 index d897870..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.3.6/src/lzio.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - - -#ifndef lzio_h -#define lzio_h - -#include "lua.h" - -#include "lmem.h" - - -#define EOZ (-1) /* end of stream */ - -typedef struct Zio ZIO; - -#define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) - - -typedef struct Mbuffer { - char *buffer; - size_t n; - size_t buffsize; -} Mbuffer; - -#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) - -#define luaZ_buffer(buff) ((buff)->buffer) -#define luaZ_sizebuffer(buff) ((buff)->buffsize) -#define luaZ_bufflen(buff) ((buff)->n) - -#define luaZ_buffremove(buff,i) ((buff)->n -= (i)) -#define luaZ_resetbuffer(buff) ((buff)->n = 0) - - -#define luaZ_resizebuffer(L, buff, size) \ - ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ - (buff)->buffsize, size), \ - (buff)->buffsize = size) - -#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) - - -LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, - void *data); -LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ - - - -/* --------- Private Part ------------------ */ - -struct Zio { - size_t n; /* bytes still unread */ - const char *p; /* current position in buffer */ - lua_Reader reader; /* reader function */ - void *data; /* additional data */ - lua_State *L; /* Lua state (for reader) */ -}; - - -LUAI_FUNC int luaZ_fill (ZIO *z); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/README b/LuaBridge3/Tests/Lua/Lua.5.4.4/README deleted file mode 100644 index c394c69..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/README +++ /dev/null @@ -1,6 +0,0 @@ - -This is Lua 5.4.4, released on 13 Jan 2022. - -For installation instructions, license details, and -further information about Lua, see doc/readme.html. - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/contents.html b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/contents.html deleted file mode 100644 index ab82eb4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/contents.html +++ /dev/null @@ -1,674 +0,0 @@ - - - -Lua 5.4 Reference Manual - contents - - - - - - - -

-Lua -Lua 5.4 Reference Manual -

- -

-The reference manual is the official definition of the Lua language. -
-For a complete introduction to Lua programming, see the book -Programming in Lua. - -

- -

- -Copyright © 2020–2022 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

Contents

- - -

Index

- - - - - - - - - - - - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/index.css b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/index.css deleted file mode 100644 index c961835..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/index.css +++ /dev/null @@ -1,21 +0,0 @@ -ul { - list-style-type: none ; -} - -ul.contents { - padding: 0 ; -} - -table { - border: none ; - border-spacing: 0 ; - border-collapse: collapse ; -} - -td { - vertical-align: top ; - padding: 0 ; - text-align: left ; - line-height: 1.25 ; - width: 15% ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/logo.gif b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/logo.gif deleted file mode 100644 index 5c77eacc3b8f397fb2e87d2aaecd89128e53a117..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9893 zcmZ`JAZZ!6FBhF{g-pRY z?mq`C!p1xV^4?ZTR7k~j;V9G1HDS87U+{!}P$o&rgc4zX0NDT~riS`~QEcMXc4?@W z^+STR)$~fm_`ABgqdI|BbFIpHF{r}hP+etgD8-NQs7W75NaJI?Ql(hqjVakK+HRty zUXfx9(Bq-+-?*K3uw9ID@9?*;-eo#?myt-t^)SGPba%#*OB5Fu=j7)HeEWuc=*-K) z!oqjj0S)u!&5gi;Bahobc>^^a9XUOHT z2|mFhODfM>_&5x-TrvTHjN4n=t}cJHz|Oj&#(Ac#A<;QYj?%w(I{6NHetKEy88L^C$rQ3l)#a6 zoT#z!d-$MNnQ|f0h4|+;?CDm7g1UnqCpv3AVbZ0g!y5D)ht6oJG9OD4(C|vg-ir+_ zH4QGgnPSh+=ffel34{g3QbJKkb?GxJXlu)W&M4!QB^7H4b450C&zK>m4Sy@4- z6QwcXU;C4g#1AgjGlY}HQLNi?RV^MlIy=8Y#lo5nG5DcI$LoBU)7F-?yK5#MO(ZKV z4S#la_H=)P#gQKG^HdgSn#J9BwwdVYIM>{c*8F@wEO$d}R+sxjn>$#7D0Sp=?`**6 zSgb455aPSEq?K#BY9czOB~w zbadnT{PaZ|6MyUb3JQ8>r#+FkD@XhNG?}o-!{}_4A*5_Nyi-4?sw$?YhV}1AdjC?B zgUxkSi}=^CG*t3gMjJh01>e6T@3$!ESeZ^i-|I!u zS;Y;*lPMP-5pj1|J68PThiJLljogBy_@_?@umOG-J7a9_muB|%_5%Y0xG`uvyho24 zIB%XLJt0uPbhhSAqhnKw*C!-wew~ll#zHvKqtx}hu;K?rot-)2DP`g3vIG)P#bW#V z#78r@yHnXrnbSui-|;3*m#OEgl|Ar3-?ZbLj*ECy&1Z-(e4BAv822YtIM?~_>A{XCM0gm<2F`yi=~k^Qpkaj;z9R!JMds*m*#io0jdc7= z_32qZaeQ{RyoLtu;NW0GM#|0W?QI53?3wlUfYmFzOS9J|wG7ON3R+r2E3FBfX*p?^~m1 z6V|U~sT|y#!d_|FC~juNn3R-(U?c)K1cYB-XL+RJsusn5sJ+RfCJjfoveJPB2E8VY ze>-6GiZ=08p%V7#QAzD2(fGeZ=h&GUh!qrTD!o3Hx0HW92SHt(m5Rzw1vQg>ESIRL zceqjS=8oh<74zz*;XjH7mM-Q|N>rkd^6&&UHrgsF*A*1*Ny>erU}PL7XDcg0?d{O* z1OHq$wC4kBW@%Z_)RY<-8R=VoJu1hW%$PbbR=GYYPsi<1pLKq2BJ*9&lH*Mr1@h?- zk$H2rr%((U;g63+%JFe|?|D5i*-R){z1;cs_IMilux#weYs=fwFeyZelpm6zO3TXT zYfVu=SRoN`fR)c~#H3%xLrrVG){XLs%W+C*j!X)3XXS=o9L4Y7Y4c4Nn3e6iqIP!B$P!~=+%bVCorwZbea_d%{9_@+x)gMD z!A=L1w$D$>lAk8s9b;tVXJZqwMyRqA-fVf9jLHaCqMT)d%(`(nU#mfn-S)W6#P; z=3~G1aSyeny@xz{sqe(QAtQ%m7HZiLcGoU!#=I^tB;>)$tUp@4#&}2onV7?oPE35i zlo$@3HMJv8Y>)4(2i;^?lJIo!@e!ZH+B3jmXa9^CL-NPx#NG&|6hO2b&vPRZ#5%&5>oeqS2brR1*C$NtWqMvwVDOx5Y@S#^ z&_6%4yRdnC(v!H)L}^Ka+LW}D-Cm1w)+quD5+xy*Ed+psxoShNndF8Bm$M#>koguj z6f7);EM(I_VR11rgmli6^)M>r$(5Du>ykxlf{`JnO5?K)MC~1O8|-vR+p3I8AN~*= zT@6_!CDC>&*w)uvnR50H4tI|iW17v5G!4tni~8OiB$0i{-skaNkDJ7=p?>-!QAtuK zGH&qwyZv(MezW5r9PF&?$r6pDH(b=RG{xXlS@d8_Q?g~x9Zwmx@oP1+_x>s#?yX&A zJ<$i~41SNKh86dSYQui}G5(udqd)BUytgYi|Dx4(X$bQst#fSOqxZjt63ThVELa(< z=wI@Opzy_vOv)kuWxt8eUN7D5>|Apl8>nubM5%c9f7!7;r$k%CGhCKXT2 z-k!qzYR%Czzyy-azx++9a_n;OQf4Lr3KX3NpT!_=3b$18MH`>vz-i^}tMTVUZ z>>6?`x}LOt@qT{D&)`uV`Y3Z+ZZooW)2=6EW~SdY0o?^b4jI6ZC)@y&ZP@Uz`5D7Z zl7dqFba9n&vrI2DRB5j@vG&K0)N-wxvoW8ny|(k~ASD{ZgPGTs%RJA)pGADun)w|a z9dCPO-ENNA_?|AQmW!TZVZ#avB>&ydPA~a9V`hh<#3X&+zS}w3vd~Kp=B*6_p}bcV zUE^_+>f5sHi>P1s`6V5{K_R*fd;9HOnbE7L83#ush~4-^y8hd8)pE5F z;(t~CR|ixR&)!V>9E-Q}W7qM?>PHTG9O>l`kbNR(){jzBZ*ds4KT^iK9nC2MFeUHP z9338RB2IKBlpAx=O+0EmU0UsJqgAF_Yqf}8Q@d%~4+9$z#-MfW^efyWWMSd6uGf_+ zqjn?n8k;pT-XZ&lpw~#O~n_8?Twhc9ed6YFyb#LI=KCJ zvxB&XLT&i`?-Sp}FiYifPlEId6de`PfSdd$>PIn)jd(&d4rO4 z)`gJ$(5q|9mZSEj+U-Jyj68(nd8|~`FqMr8&xOZFD(-ejPhfMcleKc;TX^`F^Q}P@ zH8mx*zim<8h~pUTMM@?2#=mgs!BWd|-=d!OJHi|z39ur+V?Et3D{3|8<={wSG3*04*38Uo_r^=G9s|a2cvz-V z?>h*Y_J3Cw*8EqJ+E0vUf*#IF9rqE@D5hP3xQMXi$A5?SFx~#$VJzA8YVz(7Q})(U6RbGxnUHX$I?6)oBrzO(9Gijguu~Q)mrI3TvYr5V zlecf*E;%n+iHt!C3&ov}`Pd8MvETwj-e6#3A9{!ob!->gJ&Dyi?9fPmit;{cNQQD} zRloJu`6cIqVp~w*ZEHNPfMO=t?uJUuz+PjK>|*NYH$DV0sZDM5xKUD4+E5i?XUAS~ z&ENey8k;>nVL)7Oe!#gxo%g+OLzzHbZ^Q#Hr?fQO-~Uyl_=%{Dwe=v}>VpS~e)Zfd zq6(`zp2q6M3EN{ptaCKVO!w*q^{zTx5U{s93}a&p(^r#9_g$4P^C zhOAD1#@AZmX9yCK(qOMLy4SZxI3t?z`q%(R0dXh+nA(Xct8Ixl9UWcRw{PM_lWt44 zii-UG-&9%8)deq&#<(253+VH6aJtz4m08Z|cR8Fh9?iD7Y$R^gm6wr0OlHzMRz(`> zc#of!mL@4Fxf?IyQ+0dVt(2A~UOH1LC^|9d2W<@XX~FiNC;9vL0sMRjA2{kA)_ivs zeWeS@SXj{VCnOyl*v3ad<_MUZ(*T3SXZzS^K0w(^57NRwPnw6=RAub}_Hv4CRcFz|73xTIY87ZN%Eu-R<#d?Y0Mk9Z}Bv zYr7mCuc_(%=s#ii#emwHHkH8m#t`<|2Vzt7Ll=jtqy z)$u#R!;`EfUY`u5UOo|I+42}XT)zUCA%0A-^7`I=ZfR-o#M>pI(&x#FQO~-wA&6X` z)yeVAwEKme93k?X@U%31d;)%TT8MV@)M27H{Ks-Nz!g>w@Q)m&r)ECy?&D|eyj2WD z5o~z@y`E18lT9aWgGMI%u|qa35ieo1_7QK4-^D*)V(2O9s-SjK2ozF~_xSI1JUn=b zy>CfBmtzObTZ)&fd8j^&zW*o2Cny~%la4)hePrQ&alp>0b+@msxA}OtZYdw5sBHN6 ztY2{B#7A&&qxUPlZbskj8B0%J@YVO!y3g-G)<+CB0VRNfvmr2^%A)G?(XzvKx_J8c@z3|&{Jf#Q6hf-12#XCn z7r*I(05aTlBn=FvRV={-z0+e8P%4{pPQ2F+ylIcnR!zi{>dI$k4-)qKT;6A1muF^S zK@RG@;X6RE3#zLPC5zl$yRohr!qFKRGMCf%c85Adviqy5tKWrm?BiqK zJlr6{$2}y}emwh4R~Zu<+v)!rAvhG56B@nNgK8~TG-+s&BVCBA0)MH6Qz1MY zE!g%GI++q<31}P~DeNho66M2rTY);Fw)_xu*nZscis(d#~c9jyj zlO_C&>f}=Bs;a6})6=1f$k$?UuQo zZj|=1ToL2BBQGZkFdJ#5n4sGI_w=9=6GwLu$gdFr zh@3YGgbkL}(_R~PjdJ9F=gAn_f;2xJN7|XcD)GfrMW1)WQ8+fEHZ(*N2vyEHta{ym zJ9?aozqKTFj7fv5GVqnYQ;U7M9$e-XJy*c9ovSeEAX* z_qjGGA8%scLJA^GI>}8 z0u=`bV!4{EnHef=;?l|Mk|G&AF^v?P#5t_BW^7QU-Bp)8{tk;~`REdzZuECUqksL% z({@#k_d|!*Zz7-EdY61|W+q6R@mAYKgEF4Y!6Z-K>LVxg*3OQyfuyN0BkoWHzR ze@`Hb)#sM}c1JEkE<$Ag03DiREQpYh&<73>=auitEdPKE5Zio;M8>8#N3>mg$0#?fpooPYP3zvz$nr7G*V7M zL14Z_bdBXaYg~N14k9WJj)Z~&Du{QYWN+27M0$-FY3mA?660V3@hEr#80d}w6uBc2 z&xqbW5PdYDPRl#h-K81fsXxHpZz5AyS!#Y)UzBun<3>V4@)GI(`I&(y^V|Nuxg|$> ze-xu$mXxC-bvJjTp)D$GT`3va6usUGXWYHK56@_&k5|l4vR1Rs&x#osT?6V=nbSGf zNgVWviClW^l>IX%D-Hsnz=(>8T~WS@?M&=}0=Gz!$qFHiCcqgvoX^J_MPyEqLP0_T zb4;*2p#vi+Gj-|xXMBA0c)93JEa7Bg!0Ez*S5~L7_vr6)zKF6xBOtNG78De$aK-4l zeXw5Q`YxaS?ekGnkbI$1mB|Q9bQHQsm_-1i4t)GUm|kp&@+NF2q~fM^;guaSW5drtx9X1l{5mNq(_-&+XiTn`hG7xNubGh-OWvXylx2D+uyhNHEuTL`TXFZQESqr z@1Eb*h7^$7wv@4yyalP)=k5 ztp@8qNET4Yfq*ppnVftWU#Mi{zggx?1(g1hR?AF7gL#a<0p2DqkruOp>mR#??h!I0 zR-@x!50K!O9L8=}OuaAjnD3C{Cg-mEz^5}70Z{c=dST~_(OVbQUEHQ(>p!wd^mKqDerqMHD0f3E%r6}(tKvECnk z>(}yvt*>g`NS`kR``z|Sx)B}Knx6mB7#X>$cNmgf9t?h|w!Pd{EG=yrimJ8SROySp zxuRb~X=L@Xny;x`ErtJobBCTDE_E2|(CyLn{pxKWFOZv&h2JqMsGF|GO01kDe+PLy zCZ(8^n>%hm63f2HmqH2?iyi3eCughAUJ3jj;WSJ(drz_eRlI$h2K7!_=*#0RE{Ao- z{*C5-|KIZRazbu<7{D8Zh5h?Y13)cK=N#LbkXY?s#|?c8LrbUScHD&FT@po=jF7=Q zJI5p{jk#{)GgMSu>}TW67Q*eI6dHvD9)d)Qv9+aheE8Q%){c!2T~2dz&_sUSJl^?GmyDOdBCvz1``m^V5AmdwV#F{P!Yxje4v0fRqp zR0c8$iZ^pI5Eo^>9~8zQBpiJV?j}{#S(bKkib!XHvlbwrhY>}irA;d-W^Q^~(ea4e zl0Mr=%l4D*`m}qM3wX^%3z*^8)%&}$Pbt3P> zOI`DB@Oua*(rNKvFMnQaQ+!Zge6uu-jy?DDE9}LNN5%DPX8foO0k5=z0*XJA%Lm9y ztbj*;>5IhMb>xX|4e|g(z<0lEnM5zuuOs!7$(B5nUNdeqKYH|f!C65=1I!#H6_sfR z=11rir@n9RK8cG5s}JUTpuT(eJ6@i#@aGiQ8q^L#V}Pi6={Kru?FEmZD@?f}3`I;l z;N&?eL*5j)-;QP$&9%6V!Icvenn?mi!1w&9^B-mEvpr8HKt003u*hp~LzSO_-gw)I z^7mfb?O_#4x-f(IQQz9d;Oi<$@=)a`&r@G0chPW81!^O{>$xM(O#*HriRwpQA=EX3 z)q(Zueoq!$w_u^GFC6J^H_I;EZhw9?J3(@DrR%MAF1?v>rYjb&#x|B z5wUP^;**nilr?XOC|@QARsjTzb= zPGt)L{w+KLLbZnHC*_LSkkb&sA8R{;dV%7T@fN$QuPe${#(dgB1s@H9#(J0-7`DM} zDJUqIO-BjG&iP#b#ERz|OqZ(89XU4yh4PD;6~_@jyE2xP#6BLHKCs}*EZwjFB`Yc6 z0b|q!M&DE5=u=!zZv>7aH731o3vN^&n@_hdwLfe?ON)8E<$S*%bh7($vo%BGkKhQL zqKmEgPhOFT`v?)e!oXPs(1~bKHy8;T@`2sy6zKR1nfzs$S`zr7AgVnd7He-uT};rh z*gcgFZigXx)L&RrLn9+EOZVsMY`H{Vbge06VuxE>%?2;e0%_=3A)8MB4?w@Z8_rB8 zWPUxSzs?4&-QpUHB5(^FJ`A7AS5mo6Q4Msp|kLZhOt3Y&C` zRp8q$3q{o`2t|wv(n@LB7#R4k-Uic=H^OCSRqXJtrjERL4#q>QE$|z-aHeBf+eZzn zebx;(4rkY}-rUeC>gvF8IvJmt>HD2Xm&$D5ze)ksDA%0K$X@GlgXerA&<#W>E<(ne zlk_pfilr|2pbP*qG}^t<4`$`b_;n7{O>q@5X=rIT0#F1DMDS_WA zWNXXpvh0++8j2!fi*)&6r%G^q{%gO!S#C~FFa{MZ4gHU9eX~BJ{SI>{5~-Ec>eoUu zndqE>Qz{H#Q$Y$hTi6TFm5sXO9vRXn4JRusEHtb8oR`lZ4c~Um3oDF*D)~KzLLEj< zM~46lYXldOAt9MlS=IP^Nl8Fbk{l1u(gSyJAaz)z3I-OI@Rz*c1WX1H^%PVVz|8~O zE^@R+ec87V^{X{*Z#Sdy04dRvD_;?gqaZ{^OCOonX{;6mwFubrnDFzm1Vz;0KkMq; z603#?5Z&5tl9I4TGkN>LmWx!d{JFWgiExTe-vE`N*l06rN#-yV{Z zmKLUr_Y!)v29ArE*lXIvaO0sQ7J!kX5AWK(%xOSp%2Bb-kMMdf%cP8$K%%Yg{&iI8 zLy9H`I|JUt4jRd1@?e$Ew3|<@L!{zB=>UmiV`Ib9W?UW@TQ$46xfxv3ZnX6Sw`T)& z4^$^fkmWiOrT+y}hvP=Q><17rFdzl&!Q-TLwkU?z=Zl6U#8$PM8sw&nF~Opm8uUcz z-aN4gpf)$ni(KFXa2}Cp8usb85tYDcFb-k3J!vSsEeNWgkcO046OYr z{Pf~Ng@%Tv^V4T5C@KmmDxv|ZYGi5}pO(g>pKv_VRTT@F1+IT{xw)>AlK+Gm1vfWP z`h6yCF!&dfr3;12j?%k&zf_MeEc`B3%zxZqV8_JG%74{BrW8l<;yDQkL3MU^ib_bt z$HaUzH>XWaO@%@${PAKDC@6e%a$={Uq4C1a*_lGBI1(#S$y1B5Vy~sbIHg zXlS_TBd{4CAO8qA92mL7LrYe+$|)KmDcpLkj;L`AxUa0Kwp@fju)Rw4ESvsaq4Nva z^FaB+AS7H$$Mjh53i3+nShux3oMWM-qqF151Vs4H#DtK&J!_eIpscPg0R-yyDh@@= zCfjvk8=@y5_kgd#`0twjR-;WEPGdhXX>VISeU@4^LTa0+cn3CNy>}GTa5OS-H0Ck1 zHwGsND>DlR0}Cqy^9L0cW*$~{9#(D!W>y|%X0efPKmV(Nm5tF?6Sx1};6n@t9B2TM M5|b0H5Z3qqKj-ldMF0Q* diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.1 b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.1 deleted file mode 100644 index a46a1a6..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.1 +++ /dev/null @@ -1,149 +0,0 @@ -.\" $Id: lua.man,v 1.14 2020/05/21 19:31:21 lhf Exp $ -.TH LUA 1 "$Date: 2020/05/21 19:31:21 $" -.SH NAME -lua \- Lua interpreter -.SH SYNOPSIS -.B lua -[ -.I options -] -[ -.I script -[ -.I args -] -] -.SH DESCRIPTION -.B lua -is the standalone Lua interpreter. -It loads and executes Lua programs, -either in textual source form or -in precompiled binary form. -(Precompiled binaries are output by -.BR luac , -the Lua compiler.) -.B lua -can be used as a batch interpreter and also interactively. -.LP -After handling the -.IR options , -the Lua program in file -.I script -is loaded and executed. -The -.I args -are available to -.I script -as strings in a global table named -.B arg -and also as arguments to its main function. -When called without arguments, -.B lua -behaves as -.B "lua \-v \-i" -if the standard input is a terminal, -and as -.B "lua \-" -otherwise. -.LP -In interactive mode, -.B lua -prompts the user, -reads lines from the standard input, -and executes them as they are read. -If the line contains an expression, -then the line is evaluated and the result is printed. -If a line does not contain a complete statement, -then a secondary prompt is displayed and -lines are read until a complete statement is formed or -a syntax error is found. -.LP -Before handling command line options and scripts, -.B lua -checks the contents of the environment variables -.B LUA_INIT_5_4 -and -.BR LUA_INIT , -in that order. -If the contents are of the form -.RI '@ filename ', -then -.I filename -is executed. -Otherwise, the contents are assumed to be a Lua statement and is executed. -When -.B LUA_INIT_5_4 -is defined, -.B LUA_INIT -is ignored. -.SH OPTIONS -.TP -.BI \-e " stat" -execute statement -.IR stat . -.TP -.B \-i -enter interactive mode after executing -.IR script . -.TP -.BI \-l " name" -require library -.I name -into global -.IR name . -.TP -.B \-v -show version information. -.TP -.B \-E -ignore environment variables. -.TP -.B \-W -turn warnings on. -.TP -.B \-\- -stop handling options. -.TP -.B \- -stop handling options and execute the standard input as a file. -.SH ENVIRONMENT VARIABLES -The following environment variables affect the execution of -.BR lua . -When defined, -the version-specific variants take priority -and the version-neutral variants are ignored. -.TP -.B LUA_INIT, LUA_INIT_5_4 -Code to be executed before command line options and scripts. -.TP -.B LUA_PATH, LUA_PATH_5_4 -Initial value of package.cpath, -the path used by require to search for Lua loaders. -.TP -.B LUA_CPATH, LUA_CPATH_5_4 -Initial value of package.cpath, -the path used by require to search for C loaders. -.SH EXIT STATUS -If a script calls os.exit, -then -.B lua -exits with the given exit status. -Otherwise, -.B lua -exits -with EXIT_SUCCESS (0 on POSIX systems) if there were no errors -and -with EXIT_FAILURE (1 on POSIX systems) if there were errors. -Errors raised in interactive mode do not cause exits. -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH "SEE ALSO" -.BR luac (1) -.br -The documentation at lua.org, -especially section 7 of the reference manual. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.css b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.css deleted file mode 100644 index cbd0799..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/lua.css +++ /dev/null @@ -1,161 +0,0 @@ -html { - background-color: #F8F8F8 ; -} - -body { - background-color: #FFFFFF ; - color: #000000 ; - font-family: Helvetica, Arial, sans-serif ; - text-align: justify ; - line-height: 1.25 ; - margin: 16px auto ; - padding: 32px ; - border: solid #ccc 1px ; - border-radius: 20px ; - max-width: 70em ; - width: 90% ; -} - -h1, h2, h3, h4 { - color: #000080 ; - font-family: Verdana, Geneva, sans-serif ; - font-weight: normal ; - font-style: normal ; - text-align: left ; -} - -h1 { - font-size: 28pt ; -} - -h1 img { - vertical-align: text-bottom ; -} - -h2:before { - content: "\2756" ; - padding-right: 0.5em ; -} - -a { - text-decoration: none ; -} - -a:link { - color: #000080 ; -} - -a:link:hover, a:visited:hover { - background-color: #D0D0FF ; - color: #000080 ; - border-radius: 4px ; -} - -a:link:active, a:visited:active { - color: #FF0000 ; -} - -div.menubar { - padding-bottom: 0.5em ; -} - -p.menubar { - margin-left: 2.5em ; -} - -.menubar a:hover { - margin: -3px -3px -3px -3px ; - padding: 3px 3px 3px 3px ; - border-radius: 4px ; -} - -:target { - background-color: #F0F0F0 ; - margin: -8px ; - padding: 8px ; - border-radius: 8px ; - outline: none ; -} - -hr { - display: none ; -} - -table hr { - background-color: #a0a0a0 ; - color: #a0a0a0 ; - border: 0 ; - height: 1px ; - display: block ; -} - -.footer { - color: gray ; - font-size: x-small ; - text-transform: lowercase ; -} - -input[type=text] { - border: solid #a0a0a0 2px ; - border-radius: 2em ; - background-image: url('images/search.png') ; - background-repeat: no-repeat ; - background-position: 4px center ; - padding-left: 20px ; - height: 2em ; -} - -pre.session { - background-color: #F8F8F8 ; - padding: 1em ; - border-radius: 8px ; -} - -table { - border: none ; - border-spacing: 0 ; - border-collapse: collapse ; -} - -td { - padding: 0 ; - margin: 0 ; -} - -td.gutter { - width: 4% ; -} - -table.columns td { - vertical-align: top ; - padding-bottom: 1em ; - text-align: justify ; - line-height: 1.25 ; -} - -table.book td { - vertical-align: top ; -} - -table.book td.cover { - padding-right: 1em ; -} - -table.book img { - border: solid #000080 1px ; -} - -table.book span { - font-size: small ; - text-align: left ; - display: block ; - margin-top: 0.25em ; -} - -p.logos a:link:hover, p.logos a:visited:hover { - background-color: inherit ; -} - -img { - background-color: white ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/luac.1 b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/luac.1 deleted file mode 100644 index 33a4ed0..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/luac.1 +++ /dev/null @@ -1,118 +0,0 @@ -.\" $Id: luac.man,v 1.29 2011/11/16 13:53:40 lhf Exp $ -.TH LUAC 1 "$Date: 2011/11/16 13:53:40 $" -.SH NAME -luac \- Lua compiler -.SH SYNOPSIS -.B luac -[ -.I options -] [ -.I filenames -] -.SH DESCRIPTION -.B luac -is the Lua compiler. -It translates programs written in the Lua programming language -into binary files containing precompiled chunks -that can be later loaded and executed. -.LP -The main advantages of precompiling chunks are: -faster loading, -protecting source code from accidental user changes, -and -off-line syntax checking. -Precompiling does not imply faster execution -because in Lua chunks are always compiled into bytecodes before being executed. -.B luac -simply allows those bytecodes to be saved in a file for later execution. -Precompiled chunks are not necessarily smaller than the corresponding source. -The main goal in precompiling is faster loading. -.LP -In the command line, -you can mix -text files containing Lua source and -binary files containing precompiled chunks. -.B luac -produces a single output file containing the combined bytecodes -for all files given. -Executing the combined file is equivalent to executing the given files. -By default, -the output file is named -.BR luac.out , -but you can change this with the -.B \-o -option. -.LP -Precompiled chunks are -.I not -portable across different architectures. -Moreover, -the internal format of precompiled chunks -is likely to change when a new version of Lua is released. -Make sure you save the source files of all Lua programs that you precompile. -.LP -.SH OPTIONS -.TP -.B \-l -produce a listing of the compiled bytecode for Lua's virtual machine. -Listing bytecodes is useful to learn about Lua's virtual machine. -If no files are given, then -.B luac -loads -.B luac.out -and lists its contents. -Use -.B \-l \-l -for a full listing. -.TP -.BI \-o " file" -output to -.IR file , -instead of the default -.BR luac.out . -(You can use -.B "'\-'" -for standard output, -but not on platforms that open standard output in text mode.) -The output file may be one of the given files because -all files are loaded before the output file is written. -Be careful not to overwrite precious files. -.TP -.B \-p -load files but do not generate any output file. -Used mainly for syntax checking and for testing precompiled chunks: -corrupted files will probably generate errors when loaded. -If no files are given, then -.B luac -loads -.B luac.out -and tests its contents. -No messages are displayed if the file loads without errors. -.TP -.B \-s -strip debug information before writing the output file. -This saves some space in very large chunks, -but if errors occur when running a stripped chunk, -then the error messages may not contain the full information they usually do. -In particular, -line numbers and names of local variables are lost. -.TP -.B \-v -show version information. -.TP -.B \-\- -stop handling options. -.TP -.B \- -stop handling options and process standard input. -.SH "SEE ALSO" -.BR lua (1) -.br -The documentation at lua.org. -.SH DIAGNOSTICS -Error messages should be self explanatory. -.SH AUTHORS -R. Ierusalimschy, -L. H. de Figueiredo, -W. Celes -.\" EOF diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.css b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.css deleted file mode 100644 index aa0e677..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.css +++ /dev/null @@ -1,21 +0,0 @@ -h3 code { - font-family: inherit ; - font-size: inherit ; -} - -pre, code { - font-size: 12pt ; -} - -span.apii { - color: gray ; - float: right ; - font-family: inherit ; - font-style: normal ; - font-size: small ; -} - -h2:before { - content: "" ; - padding-right: 0em ; -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.html b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.html deleted file mode 100644 index 61a8220..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/manual.html +++ /dev/null @@ -1,11959 +0,0 @@ - - - -Lua 5.4 Reference Manual - - - - - - - -

-Lua -Lua 5.4 Reference Manual -

- -

-by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes - -

- -Copyright © 2020–2022 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

- - -

- - - - - - -

1 – Introduction

- -

-Lua is a powerful, efficient, lightweight, embeddable scripting language. -It supports procedural programming, -object-oriented programming, functional programming, -data-driven programming, and data description. - - -

-Lua combines simple procedural syntax with powerful data description -constructs based on associative arrays and extensible semantics. -Lua is dynamically typed, -runs by interpreting bytecode with a register-based -virtual machine, -and has automatic memory management with -a generational garbage collection, -making it ideal for configuration, scripting, -and rapid prototyping. - - -

-Lua is implemented as a library, written in clean C, -the common subset of Standard C and C++. -The Lua distribution includes a host program called lua, -which uses the Lua library to offer a complete, -standalone Lua interpreter, -for interactive or batch use. -Lua is intended to be used both as a powerful, lightweight, -embeddable scripting language for any program that needs one, -and as a powerful but lightweight and efficient stand-alone language. - - -

-As an extension language, Lua has no notion of a "main" program: -it works embedded in a host client, -called the embedding program or simply the host. -(Frequently, this host is the stand-alone lua program.) -The host program can invoke functions to execute a piece of Lua code, -can write and read Lua variables, -and can register C functions to be called by Lua code. -Through the use of C functions, Lua can be augmented to cope with -a wide range of different domains, -thus creating customized programming languages sharing a syntactical framework. - - -

-Lua is free software, -and is provided as usual with no guarantees, -as stated in its license. -The implementation described in this manual is available -at Lua's official web site, www.lua.org. - - -

-Like any other reference manual, -this document is dry in places. -For a discussion of the decisions behind the design of Lua, -see the technical papers available at Lua's web site. -For a detailed introduction to programming in Lua, -see Roberto's book, Programming in Lua. - - - -

2 – Basic Concepts

- - - -

-This section describes the basic concepts of the language. - - - - - -

2.1 – Values and Types

- -

-Lua is a dynamically typed language. -This means that -variables do not have types; only values do. -There are no type definitions in the language. -All values carry their own type. - - -

-All values in Lua are first-class values. -This means that all values can be stored in variables, -passed as arguments to other functions, and returned as results. - - -

-There are eight basic types in Lua: -nil, boolean, number, -string, function, userdata, -thread, and table. -The type nil has one single value, nil, -whose main property is to be different from any other value; -it often represents the absence of a useful value. -The type boolean has two values, false and true. -Both nil and false make a condition false; -they are collectively called false values. -Any other value makes a condition true. -Despite its name, -false is frequently used as an alternative to nil, -with the key difference that false behaves -like a regular value in a table, -while a nil in a table represents an absent key. - - -

-The type number represents both -integer numbers and real (floating-point) numbers, -using two subtypes: integer and float. -Standard Lua uses 64-bit integers and double-precision (64-bit) floats, -but you can also compile Lua so that it -uses 32-bit integers and/or single-precision (32-bit) floats. -The option with 32 bits for both integers and floats -is particularly attractive -for small machines and embedded systems. -(See macro LUA_32BITS in file luaconf.h.) - - -

-Unless stated otherwise, -any overflow when manipulating integer values wrap around, -according to the usual rules of two-complement arithmetic. -(In other words, -the actual result is the unique representable integer -that is equal modulo 2n to the mathematical result, -where n is the number of bits of the integer type.) - - -

-Lua has explicit rules about when each subtype is used, -but it also converts between them automatically as needed (see §3.4.3). -Therefore, -the programmer may choose to mostly ignore the difference -between integers and floats -or to assume complete control over the representation of each number. - - -

-The type string represents immutable sequences of bytes. - -Lua is 8-bit clean: -strings can contain any 8-bit value, -including embedded zeros ('\0'). -Lua is also encoding-agnostic; -it makes no assumptions about the contents of a string. -The length of any string in Lua must fit in a Lua integer. - - -

-Lua can call (and manipulate) functions written in Lua and -functions written in C (see §3.4.10). -Both are represented by the type function. - - -

-The type userdata is provided to allow arbitrary C data to -be stored in Lua variables. -A userdata value represents a block of raw memory. -There are two kinds of userdata: -full userdata, -which is an object with a block of memory managed by Lua, -and light userdata, -which is simply a C pointer value. -Userdata has no predefined operations in Lua, -except assignment and identity test. -By using metatables, -the programmer can define operations for full userdata values -(see §2.4). -Userdata values cannot be created or modified in Lua, -only through the C API. -This guarantees the integrity of data owned by -the host program and C libraries. - - -

-The type thread represents independent threads of execution -and it is used to implement coroutines (see §2.6). -Lua threads are not related to operating-system threads. -Lua supports coroutines on all systems, -even those that do not support threads natively. - - -

-The type table implements associative arrays, -that is, arrays that can have as indices not only numbers, -but any Lua value except nil and NaN. -(Not a Number is a special floating-point value -used by the IEEE 754 standard to represent -undefined numerical results, such as 0/0.) -Tables can be heterogeneous; -that is, they can contain values of all types (except nil). -Any key associated to the value nil is not considered part of the table. -Conversely, any key that is not part of a table has -an associated value nil. - - -

-Tables are the sole data-structuring mechanism in Lua; -they can be used to represent ordinary arrays, lists, -symbol tables, sets, records, graphs, trees, etc. -To represent records, Lua uses the field name as an index. -The language supports this representation by -providing a.name as syntactic sugar for a["name"]. -There are several convenient ways to create tables in Lua -(see §3.4.9). - - -

-Like indices, -the values of table fields can be of any type. -In particular, -because functions are first-class values, -table fields can contain functions. -Thus tables can also carry methods (see §3.4.11). - - -

-The indexing of tables follows -the definition of raw equality in the language. -The expressions a[i] and a[j] -denote the same table element -if and only if i and j are raw equal -(that is, equal without metamethods). -In particular, floats with integral values -are equal to their respective integers -(e.g., 1.0 == 1). -To avoid ambiguities, -any float used as a key that is equal to an integer -is converted to that integer. -For instance, if you write a[2.0] = true, -the actual key inserted into the table will be the integer 2. - - -

-Tables, functions, threads, and (full) userdata values are objects: -variables do not actually contain these values, -only references to them. -Assignment, parameter passing, and function returns -always manipulate references to such values; -these operations do not imply any kind of copy. - - -

-The library function type returns a string describing the type -of a given value (see type). - - - - - -

2.2 – Environments and the Global Environment

- -

-As we will discuss further in §3.2 and §3.3.3, -any reference to a free name -(that is, a name not bound to any declaration) var -is syntactically translated to _ENV.var. -Moreover, every chunk is compiled in the scope of -an external local variable named _ENV (see §3.3.2), -so _ENV itself is never a free name in a chunk. - - -

-Despite the existence of this external _ENV variable and -the translation of free names, -_ENV is a completely regular name. -In particular, -you can define new variables and parameters with that name. -Each reference to a free name uses the _ENV that is -visible at that point in the program, -following the usual visibility rules of Lua (see §3.5). - - -

-Any table used as the value of _ENV is called an environment. - - -

-Lua keeps a distinguished environment called the global environment. -This value is kept at a special index in the C registry (see §4.3). -In Lua, the global variable _G is initialized with this same value. -(_G is never used internally, -so changing its value will affect only your own code.) - - -

-When Lua loads a chunk, -the default value for its _ENV variable -is the global environment (see load). -Therefore, by default, -free names in Lua code refer to entries in the global environment -and, therefore, they are also called global variables. -Moreover, all standard libraries are loaded in the global environment -and some functions there operate on that environment. -You can use load (or loadfile) -to load a chunk with a different environment. -(In C, you have to load the chunk and then change the value -of its first upvalue; see lua_setupvalue.) - - - - - -

2.3 – Error Handling

- -

-Several operations in Lua can raise an error. -An error interrupts the normal flow of the program, -which can continue by catching the error. - - -

-Lua code can explicitly raise an error by calling the -error function. -(This function never returns.) - - -

-To catch errors in Lua, -you can do a protected call, -using pcall (or xpcall). -The function pcall calls a given function in protected mode. -Any error while running the function stops its execution, -and control returns immediately to pcall, -which returns a status code. - - -

-Because Lua is an embedded extension language, -Lua code starts running by a call -from C code in the host program. -(When you use Lua standalone, -the lua application is the host program.) -Usually, this call is protected; -so, when an otherwise unprotected error occurs during -the compilation or execution of a Lua chunk, -control returns to the host, -which can take appropriate measures, -such as printing an error message. - - -

-Whenever there is an error, -an error object -is propagated with information about the error. -Lua itself only generates errors whose error object is a string, -but programs may generate errors with -any value as the error object. -It is up to the Lua program or its host to handle such error objects. -For historical reasons, -an error object is often called an error message, -even though it does not have to be a string. - - -

-When you use xpcall (or lua_pcall, in C) -you may give a message handler -to be called in case of errors. -This function is called with the original error object -and returns a new error object. -It is called before the error unwinds the stack, -so that it can gather more information about the error, -for instance by inspecting the stack and creating a stack traceback. -This message handler is still protected by the protected call; -so, an error inside the message handler -will call the message handler again. -If this loop goes on for too long, -Lua breaks it and returns an appropriate message. -The message handler is called only for regular runtime errors. -It is not called for memory-allocation errors -nor for errors while running finalizers or other message handlers. - - -

-Lua also offers a system of warnings (see warn). -Unlike errors, warnings do not interfere -in any way with program execution. -They typically only generate a message to the user, -although this behavior can be adapted from C (see lua_setwarnf). - - - - - -

2.4 – Metatables and Metamethods

- -

-Every value in Lua can have a metatable. -This metatable is an ordinary Lua table -that defines the behavior of the original value -under certain events. -You can change several aspects of the behavior -of a value by setting specific fields in its metatable. -For instance, when a non-numeric value is the operand of an addition, -Lua checks for a function in the field __add of the value's metatable. -If it finds one, -Lua calls this function to perform the addition. - - -

-The key for each event in a metatable is a string -with the event name prefixed by two underscores; -the corresponding value is called a metavalue. -For most events, the metavalue must be a function, -which is then called a metamethod. -In the previous example, the key is the string "__add" -and the metamethod is the function that performs the addition. -Unless stated otherwise, -a metamethod may in fact be any callable value, -which is either a function or a value with a __call metamethod. - - -

-You can query the metatable of any value -using the getmetatable function. -Lua queries metamethods in metatables using a raw access (see rawget). - - -

-You can replace the metatable of tables -using the setmetatable function. -You cannot change the metatable of other types from Lua code, -except by using the debug library (§6.10). - - -

-Tables and full userdata have individual metatables, -although multiple tables and userdata can share their metatables. -Values of all other types share one single metatable per type; -that is, there is one single metatable for all numbers, -one for all strings, etc. -By default, a value has no metatable, -but the string library sets a metatable for the string type (see §6.4). - - -

-A detailed list of operations controlled by metatables is given next. -Each event is identified by its corresponding key. -By convention, all metatable keys used by Lua are composed by -two underscores followed by lowercase Latin letters. - - - -

    - -
  • __add: -the addition (+) operation. -If any operand for an addition is not a number, -Lua will try to call a metamethod. -It starts by checking the first operand (even if it is a number); -if that operand does not define a metamethod for __add, -then Lua will check the second operand. -If Lua can find a metamethod, -it calls the metamethod with the two operands as arguments, -and the result of the call -(adjusted to one value) -is the result of the operation. -Otherwise, if no metamethod is found, -Lua raises an error. -
  • - -
  • __sub: -the subtraction (-) operation. -Behavior similar to the addition operation. -
  • - -
  • __mul: -the multiplication (*) operation. -Behavior similar to the addition operation. -
  • - -
  • __div: -the division (/) operation. -Behavior similar to the addition operation. -
  • - -
  • __mod: -the modulo (%) operation. -Behavior similar to the addition operation. -
  • - -
  • __pow: -the exponentiation (^) operation. -Behavior similar to the addition operation. -
  • - -
  • __unm: -the negation (unary -) operation. -Behavior similar to the addition operation. -
  • - -
  • __idiv: -the floor division (//) operation. -Behavior similar to the addition operation. -
  • - -
  • __band: -the bitwise AND (&) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod -if any operand is neither an integer -nor a float coercible to an integer (see §3.4.3). -
  • - -
  • __bor: -the bitwise OR (|) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __bxor: -the bitwise exclusive OR (binary ~) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __bnot: -the bitwise NOT (unary ~) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __shl: -the bitwise left shift (<<) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __shr: -the bitwise right shift (>>) operation. -Behavior similar to the bitwise AND operation. -
  • - -
  • __concat: -the concatenation (..) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod -if any operand is neither a string nor a number -(which is always coercible to a string). -
  • - -
  • __len: -the length (#) operation. -If the object is not a string, -Lua will try its metamethod. -If there is a metamethod, -Lua calls it with the object as argument, -and the result of the call -(always adjusted to one value) -is the result of the operation. -If there is no metamethod but the object is a table, -then Lua uses the table length operation (see §3.4.7). -Otherwise, Lua raises an error. -
  • - -
  • __eq: -the equal (==) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod only when the values -being compared are either both tables or both full userdata -and they are not primitively equal. -The result of the call is always converted to a boolean. -
  • - -
  • __lt: -the less than (<) operation. -Behavior similar to the addition operation, -except that Lua will try a metamethod only when the values -being compared are neither both numbers nor both strings. -Moreover, the result of the call is always converted to a boolean. -
  • - -
  • __le: -the less equal (<=) operation. -Behavior similar to the less than operation. -
  • - -
  • __index: -The indexing access operation table[key]. -This event happens when table is not a table or -when key is not present in table. -The metavalue is looked up in the metatable of table. - - -

    -The metavalue for this event can be either a function, a table, -or any value with an __index metavalue. -If it is a function, -it is called with table and key as arguments, -and the result of the call -(adjusted to one value) -is the result of the operation. -Otherwise, -the final result is the result of indexing this metavalue with key. -This indexing is regular, not raw, -and therefore can trigger another __index metavalue. -

  • - -
  • __newindex: -The indexing assignment table[key] = value. -Like the index event, -this event happens when table is not a table or -when key is not present in table. -The metavalue is looked up in the metatable of table. - - -

    -Like with indexing, -the metavalue for this event can be either a function, a table, -or any value with an __newindex metavalue. -If it is a function, -it is called with table, key, and value as arguments. -Otherwise, -Lua repeats the indexing assignment over this metavalue -with the same key and value. -This assignment is regular, not raw, -and therefore can trigger another __newindex metavalue. - - -

    -Whenever a __newindex metavalue is invoked, -Lua does not perform the primitive assignment. -If needed, -the metamethod itself can call rawset -to do the assignment. -

  • - -
  • __call: -The call operation func(args). -This event happens when Lua tries to call a non-function value -(that is, func is not a function). -The metamethod is looked up in func. -If present, -the metamethod is called with func as its first argument, -followed by the arguments of the original call (args). -All results of the call -are the results of the operation. -This is the only metamethod that allows multiple results. -
  • - -
- -

-In addition to the previous list, -the interpreter also respects the following keys in metatables: -__gc (see §2.5.3), -__close (see §3.3.8), -__mode (see §2.5.4), -and __name. -(The entry __name, -when it contains a string, -may be used by tostring and in error messages.) - - -

-For the unary operators (negation, length, and bitwise NOT), -the metamethod is computed and called with a dummy second operand, -equal to the first one. -This extra operand is only to simplify Lua's internals -(by making these operators behave like a binary operation) -and may be removed in future versions. -For most uses this extra operand is irrelevant. - - -

-Because metatables are regular tables, -they can contain arbitrary fields, -not only the event names defined above. -Some functions in the standard library -(e.g., tostring) -use other fields in metatables for their own purposes. - - -

-It is a good practice to add all needed metamethods to a table -before setting it as a metatable of some object. -In particular, the __gc metamethod works only when this order -is followed (see §2.5.3). -It is also a good practice to set the metatable of an object -right after its creation. - - - - - -

2.5 – Garbage Collection

- - - -

-Lua performs automatic memory management. -This means that -you do not have to worry about allocating memory for new objects -or freeing it when the objects are no longer needed. -Lua manages memory automatically by running -a garbage collector to collect all dead objects. -All memory used by Lua is subject to automatic management: -strings, tables, userdata, functions, threads, internal structures, etc. - - -

-An object is considered dead -as soon as the collector can be sure the object -will not be accessed again in the normal execution of the program. -("Normal execution" here excludes finalizers, -which can resurrect dead objects (see §2.5.3), -and excludes also operations using the debug library.) -Note that the time when the collector can be sure that an object -is dead may not coincide with the programmer's expectations. -The only guarantees are that Lua will not collect an object -that may still be accessed in the normal execution of the program, -and it will eventually collect an object -that is inaccessible from Lua. -(Here, -inaccessible from Lua means that neither a variable nor -another live object refer to the object.) -Because Lua has no knowledge about C code, -it never collects objects accessible through the registry (see §4.3), -which includes the global environment (see §2.2). - - -

-The garbage collector (GC) in Lua can work in two modes: -incremental and generational. - - -

-The default GC mode with the default parameters -are adequate for most uses. -However, programs that waste a large proportion of their time -allocating and freeing memory can benefit from other settings. -Keep in mind that the GC behavior is non-portable -both across platforms and across different Lua releases; -therefore, optimal settings are also non-portable. - - -

-You can change the GC mode and parameters by calling -lua_gc in C -or collectgarbage in Lua. -You can also use these functions to control -the collector directly (e.g., to stop and restart it). - - - - - -

2.5.1 – Incremental Garbage Collection

- -

-In incremental mode, -each GC cycle performs a mark-and-sweep collection in small steps -interleaved with the program's execution. -In this mode, -the collector uses three numbers to control its garbage-collection cycles: -the garbage-collector pause, -the garbage-collector step multiplier, -and the garbage-collector step size. - - -

-The garbage-collector pause -controls how long the collector waits before starting a new cycle. -The collector starts a new cycle when the use of memory -hits n% of the use after the previous collection. -Larger values make the collector less aggressive. -Values equal to or less than 100 mean the collector will not wait to -start a new cycle. -A value of 200 means that the collector waits for the total memory in use -to double before starting a new cycle. -The default value is 200; the maximum value is 1000. - - -

-The garbage-collector step multiplier -controls the speed of the collector relative to -memory allocation, -that is, -how many elements it marks or sweeps for each -kilobyte of memory allocated. -Larger values make the collector more aggressive but also increase -the size of each incremental step. -You should not use values less than 100, -because they make the collector too slow and -can result in the collector never finishing a cycle. -The default value is 100; the maximum value is 1000. - - -

-The garbage-collector step size controls the -size of each incremental step, -specifically how many bytes the interpreter allocates -before performing a step. -This parameter is logarithmic: -A value of n means the interpreter will allocate 2n -bytes between steps and perform equivalent work during the step. -A large value (e.g., 60) makes the collector a stop-the-world -(non-incremental) collector. -The default value is 13, -which means steps of approximately 8 Kbytes. - - - - - -

2.5.2 – Generational Garbage Collection

- -

-In generational mode, -the collector does frequent minor collections, -which traverses only objects recently created. -If after a minor collection the use of memory is still above a limit, -the collector does a stop-the-world major collection, -which traverses all objects. -The generational mode uses two parameters: -the minor multiplier and the the major multiplier. - - -

-The minor multiplier controls the frequency of minor collections. -For a minor multiplier x, -a new minor collection will be done when memory -grows x% larger than the memory in use after the previous major -collection. -For instance, for a multiplier of 20, -the collector will do a minor collection when the use of memory -gets 20% larger than the use after the previous major collection. -The default value is 20; the maximum value is 200. - - -

-The major multiplier controls the frequency of major collections. -For a major multiplier x, -a new major collection will be done when memory -grows x% larger than the memory in use after the previous major -collection. -For instance, for a multiplier of 100, -the collector will do a major collection when the use of memory -gets larger than twice the use after the previous collection. -The default value is 100; the maximum value is 1000. - - - - - -

2.5.3 – Garbage-Collection Metamethods

- -

-You can set garbage-collector metamethods for tables -and, using the C API, -for full userdata (see §2.4). -These metamethods, called finalizers, -are called when the garbage collector detects that the -corresponding table or userdata is dead. -Finalizers allow you to coordinate Lua's garbage collection -with external resource management such as closing files, -network or database connections, -or freeing your own memory. - - -

-For an object (table or userdata) to be finalized when collected, -you must mark it for finalization. - -You mark an object for finalization when you set its metatable -and the metatable has a __gc metamethod. -Note that if you set a metatable without a __gc field -and later create that field in the metatable, -the object will not be marked for finalization. - - -

-When a marked object becomes dead, -it is not collected immediately by the garbage collector. -Instead, Lua puts it in a list. -After the collection, -Lua goes through that list. -For each object in the list, -it checks the object's __gc metamethod: -If it is present, -Lua calls it with the object as its single argument. - - -

-At the end of each garbage-collection cycle, -the finalizers are called in -the reverse order that the objects were marked for finalization, -among those collected in that cycle; -that is, the first finalizer to be called is the one associated -with the object marked last in the program. -The execution of each finalizer may occur at any point during -the execution of the regular code. - - -

-Because the object being collected must still be used by the finalizer, -that object (and other objects accessible only through it) -must be resurrected by Lua. -Usually, this resurrection is transient, -and the object memory is freed in the next garbage-collection cycle. -However, if the finalizer stores the object in some global place -(e.g., a global variable), -then the resurrection is permanent. -Moreover, if the finalizer marks a finalizing object for finalization again, -its finalizer will be called again in the next cycle where the -object is dead. -In any case, -the object memory is freed only in a GC cycle where -the object is dead and not marked for finalization. - - -

-When you close a state (see lua_close), -Lua calls the finalizers of all objects marked for finalization, -following the reverse order that they were marked. -If any finalizer marks objects for collection during that phase, -these marks have no effect. - - -

-Finalizers cannot yield nor run the garbage collector. -Because they can run in unpredictable times, -it is good practice to restrict each finalizer -to the minimum necessary to properly release -its associated resource. - - -

-Any error while running a finalizer generates a warning; -the error is not propagated. - - - - - -

2.5.4 – Weak Tables

- -

-A weak table is a table whose elements are -weak references. -A weak reference is ignored by the garbage collector. -In other words, -if the only references to an object are weak references, -then the garbage collector will collect that object. - - -

-A weak table can have weak keys, weak values, or both. -A table with weak values allows the collection of its values, -but prevents the collection of its keys. -A table with both weak keys and weak values allows the collection of -both keys and values. -In any case, if either the key or the value is collected, -the whole pair is removed from the table. -The weakness of a table is controlled by the -__mode field of its metatable. -This metavalue, if present, must be one of the following strings: -"k", for a table with weak keys; -"v", for a table with weak values; -or "kv", for a table with both weak keys and values. - - -

-A table with weak keys and strong values -is also called an ephemeron table. -In an ephemeron table, -a value is considered reachable only if its key is reachable. -In particular, -if the only reference to a key comes through its value, -the pair is removed. - - -

-Any change in the weakness of a table may take effect only -at the next collect cycle. -In particular, if you change the weakness to a stronger mode, -Lua may still collect some items from that table -before the change takes effect. - - -

-Only objects that have an explicit construction -are removed from weak tables. -Values, such as numbers and light C functions, -are not subject to garbage collection, -and therefore are not removed from weak tables -(unless their associated values are collected). -Although strings are subject to garbage collection, -they do not have an explicit construction and -their equality is by value; -they behave more like values than like objects. -Therefore, they are not removed from weak tables. - - -

-Resurrected objects -(that is, objects being finalized -and objects accessible only through objects being finalized) -have a special behavior in weak tables. -They are removed from weak values before running their finalizers, -but are removed from weak keys only in the next collection -after running their finalizers, when such objects are actually freed. -This behavior allows the finalizer to access properties -associated with the object through weak tables. - - -

-If a weak table is among the resurrected objects in a collection cycle, -it may not be properly cleared until the next cycle. - - - - - - - -

2.6 – Coroutines

- -

-Lua supports coroutines, -also called collaborative multithreading. -A coroutine in Lua represents an independent thread of execution. -Unlike threads in multithread systems, however, -a coroutine only suspends its execution by explicitly calling -a yield function. - - -

-You create a coroutine by calling coroutine.create. -Its sole argument is a function -that is the main function of the coroutine. -The create function only creates a new coroutine and -returns a handle to it (an object of type thread); -it does not start the coroutine. - - -

-You execute a coroutine by calling coroutine.resume. -When you first call coroutine.resume, -passing as its first argument -a thread returned by coroutine.create, -the coroutine starts its execution by -calling its main function. -Extra arguments passed to coroutine.resume are passed -as arguments to that function. -After the coroutine starts running, -it runs until it terminates or yields. - - -

-A coroutine can terminate its execution in two ways: -normally, when its main function returns -(explicitly or implicitly, after the last instruction); -and abnormally, if there is an unprotected error. -In case of normal termination, -coroutine.resume returns true, -plus any values returned by the coroutine main function. -In case of errors, coroutine.resume returns false -plus the error object. -In this case, the coroutine does not unwind its stack, -so that it is possible to inspect it after the error -with the debug API. - - -

-A coroutine yields by calling coroutine.yield. -When a coroutine yields, -the corresponding coroutine.resume returns immediately, -even if the yield happens inside nested function calls -(that is, not in the main function, -but in a function directly or indirectly called by the main function). -In the case of a yield, coroutine.resume also returns true, -plus any values passed to coroutine.yield. -The next time you resume the same coroutine, -it continues its execution from the point where it yielded, -with the call to coroutine.yield returning any extra -arguments passed to coroutine.resume. - - -

-Like coroutine.create, -the coroutine.wrap function also creates a coroutine, -but instead of returning the coroutine itself, -it returns a function that, when called, resumes the coroutine. -Any arguments passed to this function -go as extra arguments to coroutine.resume. -coroutine.wrap returns all the values returned by coroutine.resume, -except the first one (the boolean error code). -Unlike coroutine.resume, -the function created by coroutine.wrap -propagates any error to the caller. -In this case, -the function also closes the coroutine (see coroutine.close). - - -

-As an example of how coroutines work, -consider the following code: - -

-     function foo (a)
-       print("foo", a)
-       return coroutine.yield(2*a)
-     end
-     
-     co = coroutine.create(function (a,b)
-           print("co-body", a, b)
-           local r = foo(a+1)
-           print("co-body", r)
-           local r, s = coroutine.yield(a+b, a-b)
-           print("co-body", r, s)
-           return b, "end"
-     end)
-     
-     print("main", coroutine.resume(co, 1, 10))
-     print("main", coroutine.resume(co, "r"))
-     print("main", coroutine.resume(co, "x", "y"))
-     print("main", coroutine.resume(co, "x", "y"))
-

-When you run it, it produces the following output: - -

-     co-body 1       10
-     foo     2
-     main    true    4
-     co-body r
-     main    true    11      -9
-     co-body x       y
-     main    true    10      end
-     main    false   cannot resume dead coroutine
-
- -

-You can also create and manipulate coroutines through the C API: -see functions lua_newthread, lua_resume, -and lua_yield. - - - - - -

3 – The Language

- - - -

-This section describes the lexis, the syntax, and the semantics of Lua. -In other words, -this section describes -which tokens are valid, -how they can be combined, -and what their combinations mean. - - -

-Language constructs will be explained using the usual extended BNF notation, -in which -{a} means 0 or more a's, and -[a] means an optional a. -Non-terminals are shown like non-terminal, -keywords are shown like kword, -and other terminal symbols are shown like ‘=’. -The complete syntax of Lua can be found in §9 -at the end of this manual. - - - - - -

3.1 – Lexical Conventions

- -

-Lua is a free-form language. -It ignores spaces and comments between lexical elements (tokens), -except as delimiters between two tokens. -In source code, -Lua recognizes as spaces the standard ASCII whitespace -characters space, form feed, newline, -carriage return, horizontal tab, and vertical tab. - - -

-Names -(also called identifiers) -in Lua can be any string of Latin letters, -Arabic-Indic digits, and underscores, -not beginning with a digit and -not being a reserved word. -Identifiers are used to name variables, table fields, and labels. - - -

-The following keywords are reserved -and cannot be used as names: - - -

-     and       break     do        else      elseif    end
-     false     for       function  goto      if        in
-     local     nil       not       or        repeat    return
-     then      true      until     while
-
- -

-Lua is a case-sensitive language: -and is a reserved word, but And and AND -are two different, valid names. -As a convention, -programs should avoid creating -names that start with an underscore followed by -one or more uppercase letters (such as _VERSION). - - -

-The following strings denote other tokens: - -

-     +     -     *     /     %     ^     #
-     &     ~     |     <<    >>    //
-     ==    ~=    <=    >=    <     >     =
-     (     )     {     }     [     ]     ::
-     ;     :     ,     .     ..    ...
-
- -

-A short literal string -can be delimited by matching single or double quotes, -and can contain the following C-like escape sequences: -'\a' (bell), -'\b' (backspace), -'\f' (form feed), -'\n' (newline), -'\r' (carriage return), -'\t' (horizontal tab), -'\v' (vertical tab), -'\\' (backslash), -'\"' (quotation mark [double quote]), -and '\'' (apostrophe [single quote]). -A backslash followed by a line break -results in a newline in the string. -The escape sequence '\z' skips the following span -of whitespace characters, -including line breaks; -it is particularly useful to break and indent a long literal string -into multiple lines without adding the newlines and spaces -into the string contents. -A short literal string cannot contain unescaped line breaks -nor escapes not forming a valid escape sequence. - - -

-We can specify any byte in a short literal string, -including embedded zeros, -by its numeric value. -This can be done -with the escape sequence \xXX, -where XX is a sequence of exactly two hexadecimal digits, -or with the escape sequence \ddd, -where ddd is a sequence of up to three decimal digits. -(Note that if a decimal escape sequence is to be followed by a digit, -it must be expressed using exactly three digits.) - - -

-The UTF-8 encoding of a Unicode character -can be inserted in a literal string with -the escape sequence \u{XXX} -(with mandatory enclosing braces), -where XXX is a sequence of one or more hexadecimal digits -representing the character code point. -This code point can be any value less than 231. -(Lua uses the original UTF-8 specification here, -which is not restricted to valid Unicode code points.) - - -

-Literal strings can also be defined using a long format -enclosed by long brackets. -We define an opening long bracket of level n as an opening -square bracket followed by n equal signs followed by another -opening square bracket. -So, an opening long bracket of level 0 is written as [[, -an opening long bracket of level 1 is written as [=[, -and so on. -A closing long bracket is defined similarly; -for instance, -a closing long bracket of level 4 is written as ]====]. -A long literal starts with an opening long bracket of any level and -ends at the first closing long bracket of the same level. -It can contain any text except a closing bracket of the same level. -Literals in this bracketed form can run for several lines, -do not interpret any escape sequences, -and ignore long brackets of any other level. -Any kind of end-of-line sequence -(carriage return, newline, carriage return followed by newline, -or newline followed by carriage return) -is converted to a simple newline. -When the opening long bracket is immediately followed by a newline, -the newline is not included in the string. - - -

-As an example, in a system using ASCII -(in which 'a' is coded as 97, -newline is coded as 10, and '1' is coded as 49), -the five literal strings below denote the same string: - -

-     a = 'alo\n123"'
-     a = "alo\n123\""
-     a = '\97lo\10\04923"'
-     a = [[alo
-     123"]]
-     a = [==[
-     alo
-     123"]==]
-
- -

-Any byte in a literal string not -explicitly affected by the previous rules represents itself. -However, Lua opens files for parsing in text mode, -and the system's file functions may have problems with -some control characters. -So, it is safer to represent -binary data as a quoted literal with -explicit escape sequences for the non-text characters. - - -

-A numeric constant (or numeral) -can be written with an optional fractional part -and an optional decimal exponent, -marked by a letter 'e' or 'E'. -Lua also accepts hexadecimal constants, -which start with 0x or 0X. -Hexadecimal constants also accept an optional fractional part -plus an optional binary exponent, -marked by a letter 'p' or 'P'. - - -

-A numeric constant with a radix point or an exponent -denotes a float; -otherwise, -if its value fits in an integer or it is a hexadecimal constant, -it denotes an integer; -otherwise (that is, a decimal integer numeral that overflows), -it denotes a float. -Hexadecimal numerals with neither a radix point nor an exponent -always denote an integer value; -if the value overflows, it wraps around -to fit into a valid integer. - - -

-Examples of valid integer constants are - -

-     3   345   0xff   0xBEBADA
-

-Examples of valid float constants are - -

-     3.0     3.1416     314.16e-2     0.31416E1     34e1
-     0x0.1E  0xA23p-4   0X1.921FB54442D18P+1
-
- -

-A comment starts with a double hyphen (--) -anywhere outside a string. -If the text immediately after -- is not an opening long bracket, -the comment is a short comment, -which runs until the end of the line. -Otherwise, it is a long comment, -which runs until the corresponding closing long bracket. - - - - - -

3.2 – Variables

- -

-Variables are places that store values. -There are three kinds of variables in Lua: -global variables, local variables, and table fields. - - -

-A single name can denote a global variable or a local variable -(or a function's formal parameter, -which is a particular kind of local variable): - -

-	var ::= Name
-

-Name denotes identifiers (see §3.1). - - -

-Any variable name is assumed to be global unless explicitly declared -as a local (see §3.3.7). -Local variables are lexically scoped: -local variables can be freely accessed by functions -defined inside their scope (see §3.5). - - -

-Before the first assignment to a variable, its value is nil. - - -

-Square brackets are used to index a table: - -

-	var ::= prefixexp ‘[’ exp ‘]’
-

-The meaning of accesses to table fields can be changed via metatables -(see §2.4). - - -

-The syntax var.Name is just syntactic sugar for -var["Name"]: - -

-	var ::= prefixexp ‘.’ Name
-
- -

-An access to a global variable x -is equivalent to _ENV.x. -Due to the way that chunks are compiled, -the variable _ENV itself is never global (see §2.2). - - - - - -

3.3 – Statements

- - - -

-Lua supports an almost conventional set of statements, -similar to those in other conventional languages. -This set includes -blocks, assignments, control structures, function calls, -and variable declarations. - - - - - -

3.3.1 – Blocks

- -

-A block is a list of statements, -which are executed sequentially: - -

-	block ::= {stat}
-

-Lua has empty statements -that allow you to separate statements with semicolons, -start a block with a semicolon -or write two semicolons in sequence: - -

-	stat ::= ‘;’
-
- -

-Both function calls and assignments -can start with an open parenthesis. -This possibility leads to an ambiguity in Lua's grammar. -Consider the following fragment: - -

-     a = b + c
-     (print or io.write)('done')
-

-The grammar could see this fragment in two ways: - -

-     a = b + c(print or io.write)('done')
-     
-     a = b + c; (print or io.write)('done')
-

-The current parser always sees such constructions -in the first way, -interpreting the open parenthesis -as the start of the arguments to a call. -To avoid this ambiguity, -it is a good practice to always precede with a semicolon -statements that start with a parenthesis: - -

-     ;(print or io.write)('done')
-
- -

-A block can be explicitly delimited to produce a single statement: - -

-	stat ::= do block end
-

-Explicit blocks are useful -to control the scope of variable declarations. -Explicit blocks are also sometimes used to -add a return statement in the middle -of another block (see §3.3.4). - - - - - -

3.3.2 – Chunks

- -

-The unit of compilation of Lua is called a chunk. -Syntactically, -a chunk is simply a block: - -

-	chunk ::= block
-
- -

-Lua handles a chunk as the body of an anonymous function -with a variable number of arguments -(see §3.4.11). -As such, chunks can define local variables, -receive arguments, and return values. -Moreover, such anonymous function is compiled as in the -scope of an external local variable called _ENV (see §2.2). -The resulting function always has _ENV as its only external variable, -even if it does not use that variable. - - -

-A chunk can be stored in a file or in a string inside the host program. -To execute a chunk, -Lua first loads it, -precompiling the chunk's code into instructions for a virtual machine, -and then Lua executes the compiled code -with an interpreter for the virtual machine. - - -

-Chunks can also be precompiled into binary form; -see the program luac and the function string.dump for details. -Programs in source and compiled forms are interchangeable; -Lua automatically detects the file type and acts accordingly (see load). - - - - - -

3.3.3 – Assignment

- -

-Lua allows multiple assignments. -Therefore, the syntax for assignment -defines a list of variables on the left side -and a list of expressions on the right side. -The elements in both lists are separated by commas: - -

-	stat ::= varlist ‘=’ explist
-	varlist ::= var {‘,’ var}
-	explist ::= exp {‘,’ exp}
-

-Expressions are discussed in §3.4. - - -

-Before the assignment, -the list of values is adjusted to the length of -the list of variables. -If there are more values than needed, -the excess values are thrown away. -If there are fewer values than needed, -the list is extended with nil's. -If the list of expressions ends with a function call, -then all values returned by that call enter the list of values, -before the adjustment -(except when the call is enclosed in parentheses; see §3.4). - - -

-If a variable is both assigned and read -inside a multiple assignment, -Lua ensures all reads get the value of the variable -before the assignment. -Thus the code - -

-     i = 3
-     i, a[i] = i+1, 20
-

-sets a[3] to 20, without affecting a[4] -because the i in a[i] is evaluated (to 3) -before it is assigned 4. -Similarly, the line - -

-     x, y = y, x
-

-exchanges the values of x and y, -and - -

-     x, y, z = y, z, x
-

-cyclically permutes the values of x, y, and z. - - -

-Note that this guarantee covers only accesses -syntactically inside the assignment statement. -If a function or a metamethod called during the assignment -changes the value of a variable, -Lua gives no guarantees about the order of that access. - - -

-An assignment to a global name x = val -is equivalent to the assignment -_ENV.x = val (see §2.2). - - -

-The meaning of assignments to table fields and -global variables (which are actually table fields, too) -can be changed via metatables (see §2.4). - - - - - -

3.3.4 – Control Structures

-The control structures -if, while, and repeat have the usual meaning and -familiar syntax: - - - - -

-	stat ::= while exp do block end
-	stat ::= repeat block until exp
-	stat ::= if exp then block {elseif exp then block} [else block] end
-

-Lua also has a for statement, in two flavors (see §3.3.5). - - -

-The condition expression of a -control structure can return any value. -Both false and nil test false. -All values different from nil and false test true. -In particular, the number 0 and the empty string also test true. - - -

-In the repeatuntil loop, -the inner block does not end at the until keyword, -but only after the condition. -So, the condition can refer to local variables -declared inside the loop block. - - -

-The goto statement transfers the program control to a label. -For syntactical reasons, -labels in Lua are considered statements too: - - - -

-	stat ::= goto Name
-	stat ::= label
-	label ::= ‘::’ Name ‘::’
-
- -

-A label is visible in the entire block where it is defined, -except inside nested functions. -A goto may jump to any visible label as long as it does not -enter into the scope of a local variable. -A label should not be declared -where a label with the same name is visible, -even if this other label has been declared in an enclosing block. - - -

-Labels and empty statements are called void statements, -as they perform no actions. - - -

-The break statement terminates the execution of a -while, repeat, or for loop, -skipping to the next statement after the loop: - - -

-	stat ::= break
-

-A break ends the innermost enclosing loop. - - -

-The return statement is used to return values -from a function or a chunk -(which is handled as an anonymous function). - -Functions can return more than one value, -so the syntax for the return statement is - -

-	stat ::= return [explist] [‘;’]
-
- -

-The return statement can only be written -as the last statement of a block. -If it is necessary to return in the middle of a block, -then an explicit inner block can be used, -as in the idiom do return end, -because now return is the last statement in its (inner) block. - - - - - -

3.3.5 – For Statement

- -

- -The for statement has two forms: -one numerical and one generic. - - - -

The numerical for loop

- -

-The numerical for loop repeats a block of code while a -control variable goes through an arithmetic progression. -It has the following syntax: - -

-	stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end
-

-The given identifier (Name) defines the control variable, -which is a new variable local to the loop body (block). - - -

-The loop starts by evaluating once the three control expressions. -Their values are called respectively -the initial value, the limit, and the step. -If the step is absent, it defaults to 1. - - -

-If both the initial value and the step are integers, -the loop is done with integers; -note that the limit may not be an integer. -Otherwise, the three values are converted to -floats and the loop is done with floats. -Beware of floating-point accuracy in this case. - - -

-After that initialization, -the loop body is repeated with the value of the control variable -going through an arithmetic progression, -starting at the initial value, -with a common difference given by the step. -A negative step makes a decreasing sequence; -a step equal to zero raises an error. -The loop continues while the value is less than -or equal to the limit -(greater than or equal to for a negative step). -If the initial value is already greater than the limit -(or less than, if the step is negative), -the body is not executed. - - -

-For integer loops, -the control variable never wraps around; -instead, the loop ends in case of an overflow. - - -

-You should not change the value of the control variable -during the loop. -If you need its value after the loop, -assign it to another variable before exiting the loop. - - - - - -

The generic for loop

- -

-The generic for statement works over functions, -called iterators. -On each iteration, the iterator function is called to produce a new value, -stopping when this new value is nil. -The generic for loop has the following syntax: - -

-	stat ::= for namelist in explist do block end
-	namelist ::= Name {‘,’ Name}
-

-A for statement like - -

-     for var_1, ···, var_n in explist do body end
-

-works as follows. - - -

-The names var_i declare loop variables local to the loop body. -The first of these variables is the control variable. - - -

-The loop starts by evaluating explist -to produce four values: -an iterator function, -a state, -an initial value for the control variable, -and a closing value. - - -

-Then, at each iteration, -Lua calls the iterator function with two arguments: -the state and the control variable. -The results from this call are then assigned to the loop variables, -following the rules of multiple assignments (see §3.3.3). -If the control variable becomes nil, -the loop terminates. -Otherwise, the body is executed and the loop goes -to the next iteration. - - -

-The closing value behaves like a -to-be-closed variable (see §3.3.8), -which can be used to release resources when the loop ends. -Otherwise, it does not interfere with the loop. - - -

-You should not change the value of the control variable -during the loop. - - - - - - - -

3.3.6 – Function Calls as Statements

-To allow possible side-effects, -function calls can be executed as statements: - -

-	stat ::= functioncall
-

-In this case, all returned values are thrown away. -Function calls are explained in §3.4.10. - - - - - -

3.3.7 – Local Declarations

-Local variables can be declared anywhere inside a block. -The declaration can include an initialization: - -

-	stat ::= local attnamelist [‘=’ explist]
-	attnamelist ::=  Name attrib {‘,’ Name attrib}
-

-If present, an initial assignment has the same semantics -of a multiple assignment (see §3.3.3). -Otherwise, all variables are initialized with nil. - - -

-Each variable name may be postfixed by an attribute -(a name between angle brackets): - -

-	attrib ::= [‘<’ Name ‘>’]
-

-There are two possible attributes: -const, which declares a constant variable, -that is, a variable that cannot be assigned to -after its initialization; -and close, which declares a to-be-closed variable (see §3.3.8). -A list of variables can contain at most one to-be-closed variable. - - -

-A chunk is also a block (see §3.3.2), -and so local variables can be declared in a chunk outside any explicit block. - - -

-The visibility rules for local variables are explained in §3.5. - - - - - -

3.3.8 – To-be-closed Variables

- -

-A to-be-closed variable behaves like a constant local variable, -except that its value is closed whenever the variable -goes out of scope, including normal block termination, -exiting its block by break/goto/return, -or exiting by an error. - - -

-Here, to close a value means -to call its __close metamethod. -When calling the metamethod, -the value itself is passed as the first argument -and the error object that caused the exit (if any) -is passed as a second argument; -if there was no error, the second argument is nil. - - -

-The value assigned to a to-be-closed variable -must have a __close metamethod -or be a false value. -(nil and false are ignored as to-be-closed values.) - - -

-If several to-be-closed variables go out of scope at the same event, -they are closed in the reverse order that they were declared. - - -

-If there is any error while running a closing method, -that error is handled like an error in the regular code -where the variable was defined. -After an error, -the other pending closing methods will still be called. - - -

-If a coroutine yields and is never resumed again, -some variables may never go out of scope, -and therefore they will never be closed. -(These variables are the ones created inside the coroutine -and in scope at the point where the coroutine yielded.) -Similarly, if a coroutine ends with an error, -it does not unwind its stack, -so it does not close any variable. -In both cases, -you can either use finalizers -or call coroutine.close to close the variables. -However, if the coroutine was created -through coroutine.wrap, -then its corresponding function will close the coroutine -in case of errors. - - - - - - - -

3.4 – Expressions

- - - -

-The basic expressions in Lua are the following: - -

-	exp ::= prefixexp
-	exp ::= nil | false | true
-	exp ::= Numeral
-	exp ::= LiteralString
-	exp ::= functiondef
-	exp ::= tableconstructor
-	exp ::= ‘...’
-	exp ::= exp binop exp
-	exp ::= unop exp
-	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
-
- -

-Numerals and literal strings are explained in §3.1; -variables are explained in §3.2; -function definitions are explained in §3.4.11; -function calls are explained in §3.4.10; -table constructors are explained in §3.4.9. -Vararg expressions, -denoted by three dots ('...'), can only be used when -directly inside a vararg function; -they are explained in §3.4.11. - - -

-Binary operators comprise arithmetic operators (see §3.4.1), -bitwise operators (see §3.4.2), -relational operators (see §3.4.4), logical operators (see §3.4.5), -and the concatenation operator (see §3.4.6). -Unary operators comprise the unary minus (see §3.4.1), -the unary bitwise NOT (see §3.4.2), -the unary logical not (see §3.4.5), -and the unary length operator (see §3.4.7). - - -

-Both function calls and vararg expressions can result in multiple values. -If a function call is used as a statement (see §3.3.6), -then its return list is adjusted to zero elements, -thus discarding all returned values. -If an expression is used as the last (or the only) element -of a list of expressions, -then no adjustment is made -(unless the expression is enclosed in parentheses). -In all other contexts, -Lua adjusts the result list to one element, -either discarding all values except the first one -or adding a single nil if there are no values. - - -

-Here are some examples: - -

-     f()                -- adjusted to 0 results
-     g(f(), x)          -- f() is adjusted to 1 result
-     g(x, f())          -- g gets x plus all results from f()
-     a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)
-     a,b = ...          -- a gets the first vararg argument, b gets
-                        -- the second (both a and b can get nil if there
-                        -- is no corresponding vararg argument)
-     
-     a,b,c = x, f()     -- f() is adjusted to 2 results
-     a,b,c = f()        -- f() is adjusted to 3 results
-     return f()         -- returns all results from f()
-     return ...         -- returns all received vararg arguments
-     return x,y,f()     -- returns x, y, and all results from f()
-     {f()}              -- creates a list with all results from f()
-     {...}              -- creates a list with all vararg arguments
-     {f(), nil}         -- f() is adjusted to 1 result
-
- -

-Any expression enclosed in parentheses always results in only one value. -Thus, -(f(x,y,z)) is always a single value, -even if f returns several values. -(The value of (f(x,y,z)) is the first value returned by f -or nil if f does not return any values.) - - - - - -

3.4.1 – Arithmetic Operators

-Lua supports the following arithmetic operators: - -

    -
  • +: addition
  • -
  • -: subtraction
  • -
  • *: multiplication
  • -
  • /: float division
  • -
  • //: floor division
  • -
  • %: modulo
  • -
  • ^: exponentiation
  • -
  • -: unary minus
  • -
- -

-With the exception of exponentiation and float division, -the arithmetic operators work as follows: -If both operands are integers, -the operation is performed over integers and the result is an integer. -Otherwise, if both operands are numbers, -then they are converted to floats, -the operation is performed following the machine's rules -for floating-point arithmetic -(usually the IEEE 754 standard), -and the result is a float. -(The string library coerces strings to numbers in -arithmetic operations; see §3.4.3 for details.) - - -

-Exponentiation and float division (/) -always convert their operands to floats -and the result is always a float. -Exponentiation uses the ISO C function pow, -so that it works for non-integer exponents too. - - -

-Floor division (//) is a division -that rounds the quotient towards minus infinity, -resulting in the floor of the division of its operands. - - -

-Modulo is defined as the remainder of a division -that rounds the quotient towards minus infinity (floor division). - - -

-In case of overflows in integer arithmetic, -all operations wrap around. - - - -

3.4.2 – Bitwise Operators

-Lua supports the following bitwise operators: - -

    -
  • &: bitwise AND
  • -
  • |: bitwise OR
  • -
  • ~: bitwise exclusive OR
  • -
  • >>: right shift
  • -
  • <<: left shift
  • -
  • ~: unary bitwise NOT
  • -
- -

-All bitwise operations convert its operands to integers -(see §3.4.3), -operate on all bits of those integers, -and result in an integer. - - -

-Both right and left shifts fill the vacant bits with zeros. -Negative displacements shift to the other direction; -displacements with absolute values equal to or higher than -the number of bits in an integer -result in zero (as all bits are shifted out). - - - - - -

3.4.3 – Coercions and Conversions

-Lua provides some automatic conversions between some -types and representations at run time. -Bitwise operators always convert float operands to integers. -Exponentiation and float division -always convert integer operands to floats. -All other arithmetic operations applied to mixed numbers -(integers and floats) convert the integer operand to a float. -The C API also converts both integers to floats and -floats to integers, as needed. -Moreover, string concatenation accepts numbers as arguments, -besides strings. - - -

-In a conversion from integer to float, -if the integer value has an exact representation as a float, -that is the result. -Otherwise, -the conversion gets the nearest higher or -the nearest lower representable value. -This kind of conversion never fails. - - -

-The conversion from float to integer -checks whether the float has an exact representation as an integer -(that is, the float has an integral value and -it is in the range of integer representation). -If it does, that representation is the result. -Otherwise, the conversion fails. - - -

-Several places in Lua coerce strings to numbers when necessary. -In particular, -the string library sets metamethods that try to coerce -strings to numbers in all arithmetic operations. -If the conversion fails, -the library calls the metamethod of the other operand -(if present) or it raises an error. -Note that bitwise operators do not do this coercion. - - -

-Nonetheless, it is always a good practice not to rely on these -implicit coercions, as they are not always applied; -in particular, "1"==1 is false and "1"<1 raises an error -(see §3.4.4). -These coercions exist mainly for compatibility and may be removed -in future versions of the language. - - -

-A string is converted to an integer or a float -following its syntax and the rules of the Lua lexer. -The string may have also leading and trailing whitespaces and a sign. -All conversions from strings to numbers -accept both a dot and the current locale mark -as the radix character. -(The Lua lexer, however, accepts only a dot.) -If the string is not a valid numeral, -the conversion fails. -If necessary, the result of this first step is then converted -to a specific number subtype following the previous rules -for conversions between floats and integers. - - -

-The conversion from numbers to strings uses a -non-specified human-readable format. -To convert numbers to strings in any specific way, -use the function string.format. - - - - - -

3.4.4 – Relational Operators

-Lua supports the following relational operators: - -

    -
  • ==: equality
  • -
  • ~=: inequality
  • -
  • <: less than
  • -
  • >: greater than
  • -
  • <=: less or equal
  • -
  • >=: greater or equal
  • -

-These operators always result in false or true. - - -

-Equality (==) first compares the type of its operands. -If the types are different, then the result is false. -Otherwise, the values of the operands are compared. -Strings are equal if they have the same byte content. -Numbers are equal if they denote the same mathematical value. - - -

-Tables, userdata, and threads -are compared by reference: -two objects are considered equal only if they are the same object. -Every time you create a new object -(a table, a userdata, or a thread), -this new object is different from any previously existing object. -A function is always equal to itself. -Functions with any detectable difference -(different behavior, different definition) are always different. -Functions created at different times but with no detectable differences -may be classified as equal or not -(depending on internal caching details). - - -

-You can change the way that Lua compares tables and userdata -by using the __eq metamethod (see §2.4). - - -

-Equality comparisons do not convert strings to numbers -or vice versa. -Thus, "0"==0 evaluates to false, -and t[0] and t["0"] denote different -entries in a table. - - -

-The operator ~= is exactly the negation of equality (==). - - -

-The order operators work as follows. -If both arguments are numbers, -then they are compared according to their mathematical values, -regardless of their subtypes. -Otherwise, if both arguments are strings, -then their values are compared according to the current locale. -Otherwise, Lua tries to call the __lt or the __le -metamethod (see §2.4). -A comparison a > b is translated to b < a -and a >= b is translated to b <= a. - - -

-Following the IEEE 754 standard, -the special value NaN is considered neither less than, -nor equal to, nor greater than any value, including itself. - - - - - -

3.4.5 – Logical Operators

-The logical operators in Lua are -and, or, and not. -Like the control structures (see §3.3.4), -all logical operators consider both false and nil as false -and anything else as true. - - -

-The negation operator not always returns false or true. -The conjunction operator and returns its first argument -if this value is false or nil; -otherwise, and returns its second argument. -The disjunction operator or returns its first argument -if this value is different from nil and false; -otherwise, or returns its second argument. -Both and and or use short-circuit evaluation; -that is, -the second operand is evaluated only if necessary. -Here are some examples: - -

-     10 or 20            --> 10
-     10 or error()       --> 10
-     nil or "a"          --> "a"
-     nil and 10          --> nil
-     false and error()   --> false
-     false and nil       --> false
-     false or nil        --> nil
-     10 and 20           --> 20
-
- - - - -

3.4.6 – Concatenation

-The string concatenation operator in Lua is -denoted by two dots ('..'). -If both operands are strings or numbers, -then the numbers are converted to strings -in a non-specified format (see §3.4.3). -Otherwise, the __concat metamethod is called (see §2.4). - - - - - -

3.4.7 – The Length Operator

- -

-The length operator is denoted by the unary prefix operator #. - - -

-The length of a string is its number of bytes. -(That is the usual meaning of string length when each -character is one byte.) - - -

-The length operator applied on a table -returns a border in that table. -A border in a table t is any non-negative integer -that satisfies the following condition: - -

-     (border == 0 or t[border] ~= nil) and
-     (t[border + 1] == nil or border == math.maxinteger)
-

-In words, -a border is any positive integer index present in the table -that is followed by an absent index, -plus two limit cases: -zero, when index 1 is absent; -and the maximum value for an integer, when that index is present. -Note that keys that are not positive integers -do not interfere with borders. - - -

-A table with exactly one border is called a sequence. -For instance, the table {10, 20, 30, 40, 50} is a sequence, -as it has only one border (5). -The table {10, 20, 30, nil, 50} has two borders (3 and 5), -and therefore it is not a sequence. -(The nil at index 4 is called a hole.) -The table {nil, 20, 30, nil, nil, 60, nil} -has three borders (0, 3, and 6), -so it is not a sequence, too. -The table {} is a sequence with border 0. - - -

-When t is a sequence, -#t returns its only border, -which corresponds to the intuitive notion of the length of the sequence. -When t is not a sequence, -#t can return any of its borders. -(The exact one depends on details of -the internal representation of the table, -which in turn can depend on how the table was populated and -the memory addresses of its non-numeric keys.) - - -

-The computation of the length of a table -has a guaranteed worst time of O(log n), -where n is the largest integer key in the table. - - -

-A program can modify the behavior of the length operator for -any value but strings through the __len metamethod (see §2.4). - - - - - -

3.4.8 – Precedence

-Operator precedence in Lua follows the table below, -from lower to higher priority: - -

-     or
-     and
-     <     >     <=    >=    ~=    ==
-     |
-     ~
-     &
-     <<    >>
-     ..
-     +     -
-     *     /     //    %
-     unary operators (not   #     -     ~)
-     ^
-

-As usual, -you can use parentheses to change the precedences of an expression. -The concatenation ('..') and exponentiation ('^') -operators are right associative. -All other binary operators are left associative. - - - - - -

3.4.9 – Table Constructors

-Table constructors are expressions that create tables. -Every time a constructor is evaluated, a new table is created. -A constructor can be used to create an empty table -or to create a table and initialize some of its fields. -The general syntax for constructors is - -

-	tableconstructor ::= ‘{’ [fieldlist] ‘}’
-	fieldlist ::= field {fieldsep field} [fieldsep]
-	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
-	fieldsep ::= ‘,’ | ‘;’
-
- -

-Each field of the form [exp1] = exp2 adds to the new table an entry -with key exp1 and value exp2. -A field of the form name = exp is equivalent to -["name"] = exp. -Fields of the form exp are equivalent to -[i] = exp, where i are consecutive integers -starting with 1; -fields in the other formats do not affect this counting. -For example, - -

-     a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
-

-is equivalent to - -

-     do
-       local t = {}
-       t[f(1)] = g
-       t[1] = "x"         -- 1st exp
-       t[2] = "y"         -- 2nd exp
-       t.x = 1            -- t["x"] = 1
-       t[3] = f(x)        -- 3rd exp
-       t[30] = 23
-       t[4] = 45          -- 4th exp
-       a = t
-     end
-
- -

-The order of the assignments in a constructor is undefined. -(This order would be relevant only when there are repeated keys.) - - -

-If the last field in the list has the form exp -and the expression is a function call or a vararg expression, -then all values returned by this expression enter the list consecutively -(see §3.4.10). - - -

-The field list can have an optional trailing separator, -as a convenience for machine-generated code. - - - - - -

3.4.10 – Function Calls

-A function call in Lua has the following syntax: - -

-	functioncall ::= prefixexp args
-

-In a function call, -first prefixexp and args are evaluated. -If the value of prefixexp has type function, -then this function is called -with the given arguments. -Otherwise, if present, -the prefixexp __call metamethod is called: -its first argument is the value of prefixexp, -followed by the original call arguments -(see §2.4). - - -

-The form - -

-	functioncall ::= prefixexp ‘:’ Name args
-

-can be used to emulate methods. -A call v:name(args) -is syntactic sugar for v.name(v,args), -except that v is evaluated only once. - - -

-Arguments have the following syntax: - -

-	args ::= ‘(’ [explist] ‘)’
-	args ::= tableconstructor
-	args ::= LiteralString
-

-All argument expressions are evaluated before the call. -A call of the form f{fields} is -syntactic sugar for f({fields}); -that is, the argument list is a single new table. -A call of the form f'string' -(or f"string" or f[[string]]) -is syntactic sugar for f('string'); -that is, the argument list is a single literal string. - - -

-A call of the form return functioncall not in the -scope of a to-be-closed variable is called a tail call. -Lua implements proper tail calls -(or proper tail recursion): -in a tail call, -the called function reuses the stack entry of the calling function. -Therefore, there is no limit on the number of nested tail calls that -a program can execute. -However, a tail call erases any debug information about the -calling function. -Note that a tail call only happens with a particular syntax, -where the return has one single function call as argument, -and it is outside the scope of any to-be-closed variable. -This syntax makes the calling function return exactly -the returns of the called function, -without any intervening action. -So, none of the following examples are tail calls: - -

-     return (f(x))        -- results adjusted to 1
-     return 2 * f(x)      -- result multiplied by 2
-     return x, f(x)       -- additional results
-     f(x); return         -- results discarded
-     return x or f(x)     -- results adjusted to 1
-
- - - - -

3.4.11 – Function Definitions

- -

-The syntax for function definition is - -

-	functiondef ::= function funcbody
-	funcbody ::= ‘(’ [parlist] ‘)’ block end
-
- -

-The following syntactic sugar simplifies function definitions: - -

-	stat ::= function funcname funcbody
-	stat ::= local function Name funcbody
-	funcname ::= Name {‘.’ Name} [‘:’ Name]
-

-The statement - -

-     function f () body end
-

-translates to - -

-     f = function () body end
-

-The statement - -

-     function t.a.b.c.f () body end
-

-translates to - -

-     t.a.b.c.f = function () body end
-

-The statement - -

-     local function f () body end
-

-translates to - -

-     local f; f = function () body end
-

-not to - -

-     local f = function () body end
-

-(This only makes a difference when the body of the function -contains references to f.) - - -

-A function definition is an executable expression, -whose value has type function. -When Lua precompiles a chunk, -all its function bodies are precompiled too, -but they are not created yet. -Then, whenever Lua executes the function definition, -the function is instantiated (or closed). -This function instance, or closure, -is the final value of the expression. - - -

-Parameters act as local variables that are -initialized with the argument values: - -

-	parlist ::= namelist [‘,’ ‘...’] | ‘...’
-

-When a Lua function is called, -it adjusts its list of arguments to -the length of its list of parameters, -unless the function is a vararg function, -which is indicated by three dots ('...') -at the end of its parameter list. -A vararg function does not adjust its argument list; -instead, it collects all extra arguments and supplies them -to the function through a vararg expression, -which is also written as three dots. -The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results. -If a vararg expression is used inside another expression -or in the middle of a list of expressions, -then its return list is adjusted to one element. -If the expression is used as the last element of a list of expressions, -then no adjustment is made -(unless that last expression is enclosed in parentheses). - - -

-As an example, consider the following definitions: - -

-     function f(a, b) end
-     function g(a, b, ...) end
-     function r() return 1,2,3 end
-

-Then, we have the following mapping from arguments to parameters and -to the vararg expression: - -

-     CALL             PARAMETERS
-     
-     f(3)             a=3, b=nil
-     f(3, 4)          a=3, b=4
-     f(3, 4, 5)       a=3, b=4
-     f(r(), 10)       a=1, b=10
-     f(r())           a=1, b=2
-     
-     g(3)             a=3, b=nil, ... -->  (nothing)
-     g(3, 4)          a=3, b=4,   ... -->  (nothing)
-     g(3, 4, 5, 8)    a=3, b=4,   ... -->  5  8
-     g(5, r())        a=5, b=1,   ... -->  2  3
-
- -

-Results are returned using the return statement (see §3.3.4). -If control reaches the end of a function -without encountering a return statement, -then the function returns with no results. - - -

- -There is a system-dependent limit on the number of values -that a function may return. -This limit is guaranteed to be greater than 1000. - - -

-The colon syntax -is used to emulate methods, -adding an implicit extra parameter self to the function. -Thus, the statement - -

-     function t.a.b.c:f (params) body end
-

-is syntactic sugar for - -

-     t.a.b.c.f = function (self, params) body end
-
- - - - - - -

3.5 – Visibility Rules

- -

- -Lua is a lexically scoped language. -The scope of a local variable begins at the first statement after -its declaration and lasts until the last non-void statement -of the innermost block that includes the declaration. -Consider the following example: - -

-     x = 10                -- global variable
-     do                    -- new block
-       local x = x         -- new 'x', with value 10
-       print(x)            --> 10
-       x = x+1
-       do                  -- another block
-         local x = x+1     -- another 'x'
-         print(x)          --> 12
-       end
-       print(x)            --> 11
-     end
-     print(x)              --> 10  (the global one)
-
- -

-Notice that, in a declaration like local x = x, -the new x being declared is not in scope yet, -and so the second x refers to the outside variable. - - -

-Because of the lexical scoping rules, -local variables can be freely accessed by functions -defined inside their scope. -A local variable used by an inner function is called an upvalue -(or external local variable, or simply external variable) -inside the inner function. - - -

-Notice that each execution of a local statement -defines new local variables. -Consider the following example: - -

-     a = {}
-     local x = 20
-     for i = 1, 10 do
-       local y = 0
-       a[i] = function () y = y + 1; return x + y end
-     end
-

-The loop creates ten closures -(that is, ten instances of the anonymous function). -Each of these closures uses a different y variable, -while all of them share the same x. - - - - - -

4 – The Application Program Interface

- - - -

- -This section describes the C API for Lua, that is, -the set of C functions available to the host program to communicate -with Lua. -All API functions and related types and constants -are declared in the header file lua.h. - - -

-Even when we use the term "function", -any facility in the API may be provided as a macro instead. -Except where stated otherwise, -all such macros use each of their arguments exactly once -(except for the first argument, which is always a Lua state), -and so do not generate any hidden side-effects. - - -

-As in most C libraries, -the Lua API functions do not check their arguments -for validity or consistency. -However, you can change this behavior by compiling Lua -with the macro LUA_USE_APICHECK defined. - - -

-The Lua library is fully reentrant: -it has no global variables. -It keeps all information it needs in a dynamic structure, -called the Lua state. - - -

-Each Lua state has one or more threads, -which correspond to independent, cooperative lines of execution. -The type lua_State (despite its name) refers to a thread. -(Indirectly, through the thread, it also refers to the -Lua state associated to the thread.) - - -

-A pointer to a thread must be passed as the first argument to -every function in the library, except to lua_newstate, -which creates a Lua state from scratch and returns a pointer -to the main thread in the new state. - - - - - -

4.1 – The Stack

- - - -

-Lua uses a virtual stack to pass values to and from C. -Each element in this stack represents a Lua value -(nil, number, string, etc.). -Functions in the API can access this stack through the -Lua state parameter that they receive. - - -

-Whenever Lua calls C, the called function gets a new stack, -which is independent of previous stacks and of stacks of -C functions that are still active. -This stack initially contains any arguments to the C function -and it is where the C function can store temporary -Lua values and must push its results -to be returned to the caller (see lua_CFunction). - - -

-For convenience, -most query operations in the API do not follow a strict stack discipline. -Instead, they can refer to any element in the stack -by using an index: -A positive index represents an absolute stack position, -starting at 1 as the bottom of the stack; -a negative index represents an offset relative to the top of the stack. -More specifically, if the stack has n elements, -then index 1 represents the first element -(that is, the element that was pushed onto the stack first) -and -index n represents the last element; -index -1 also represents the last element -(that is, the element at the top) -and index -n represents the first element. - - - - - -

4.1.1 – Stack Size

- -

-When you interact with the Lua API, -you are responsible for ensuring consistency. -In particular, -you are responsible for controlling stack overflow. -When you call any API function, -you must ensure the stack has enough room to accommodate the results. - - -

-There is one exception to the above rule: -When you call a Lua function -without a fixed number of results (see lua_call), -Lua ensures that the stack has enough space for all results. -However, it does not ensure any extra space. -So, before pushing anything on the stack after such a call -you should use lua_checkstack. - - -

-Whenever Lua calls C, -it ensures that the stack has space for -at least LUA_MINSTACK extra elements; -that is, you can safely push up to LUA_MINSTACK values into it. -LUA_MINSTACK is defined as 20, -so that usually you do not have to worry about stack space -unless your code has loops pushing elements onto the stack. -Whenever necessary, -you can use the function lua_checkstack -to ensure that the stack has enough space for pushing new elements. - - - - - -

4.1.2 – Valid and Acceptable Indices

- -

-Any function in the API that receives stack indices -works only with valid indices or acceptable indices. - - -

-A valid index is an index that refers to a -position that stores a modifiable Lua value. -It comprises stack indices between 1 and the stack top -(1 ≤ abs(index) ≤ top) - -plus pseudo-indices, -which represent some positions that are accessible to C code -but that are not in the stack. -Pseudo-indices are used to access the registry (see §4.3) -and the upvalues of a C function (see §4.2). - - -

-Functions that do not need a specific mutable position, -but only a value (e.g., query functions), -can be called with acceptable indices. -An acceptable index can be any valid index, -but it also can be any positive index after the stack top -within the space allocated for the stack, -that is, indices up to the stack size. -(Note that 0 is never an acceptable index.) -Indices to upvalues (see §4.2) greater than the real number -of upvalues in the current C function are also acceptable (but invalid). -Except when noted otherwise, -functions in the API work with acceptable indices. - - -

-Acceptable indices serve to avoid extra tests -against the stack top when querying the stack. -For instance, a C function can query its third argument -without the need to check whether there is a third argument, -that is, without the need to check whether 3 is a valid index. - - -

-For functions that can be called with acceptable indices, -any non-valid index is treated as if it -contains a value of a virtual type LUA_TNONE, -which behaves like a nil value. - - - - - -

4.1.3 – Pointers to strings

- -

-Several functions in the API return pointers (const char*) -to Lua strings in the stack. -(See lua_pushfstring, lua_pushlstring, -lua_pushstring, and lua_tolstring. -See also luaL_checklstring, luaL_checkstring, -and luaL_tolstring in the auxiliary library.) - - -

-In general, -Lua's garbage collection can free or move internal memory -and then invalidate pointers to internal strings. -To allow a safe use of these pointers, -The API guarantees that any pointer to a string in a stack index -is valid while the string value at that index is not removed from the stack. -(It can be moved to another index, though.) -When the index is a pseudo-index (referring to an upvalue), -the pointer is valid while the corresponding call is active and -the corresponding upvalue is not modified. - - -

-Some functions in the debug interface -also return pointers to strings, -namely lua_getlocal, lua_getupvalue, -lua_setlocal, and lua_setupvalue. -For these functions, the pointer is guaranteed to -be valid while the caller function is active and -the given closure (if one was given) is in the stack. - - -

-Except for these guarantees, -the garbage collector is free to invalidate -any pointer to internal strings. - - - - - - - -

4.2 – C Closures

- -

-When a C function is created, -it is possible to associate some values with it, -thus creating a C closure -(see lua_pushcclosure); -these values are called upvalues and are -accessible to the function whenever it is called. - - -

-Whenever a C function is called, -its upvalues are located at specific pseudo-indices. -These pseudo-indices are produced by the macro -lua_upvalueindex. -The first upvalue associated with a function is at index -lua_upvalueindex(1), and so on. -Any access to lua_upvalueindex(n), -where n is greater than the number of upvalues of the -current function -(but not greater than 256, -which is one plus the maximum number of upvalues in a closure), -produces an acceptable but invalid index. - - -

-A C closure can also change the values -of its corresponding upvalues. - - - - - -

4.3 – Registry

- -

-Lua provides a registry, -a predefined table that can be used by any C code to -store whatever Lua values it needs to store. -The registry table is always accessible at pseudo-index -LUA_REGISTRYINDEX. -Any C library can store data into this table, -but it must take care to choose keys -that are different from those used -by other libraries, to avoid collisions. -Typically, you should use as key a string containing your library name, -or a light userdata with the address of a C object in your code, -or any Lua object created by your code. -As with variable names, -string keys starting with an underscore followed by -uppercase letters are reserved for Lua. - - -

-The integer keys in the registry are used -by the reference mechanism (see luaL_ref) -and by some predefined values. -Therefore, integer keys in the registry -must not be used for other purposes. - - -

-When you create a new Lua state, -its registry comes with some predefined values. -These predefined values are indexed with integer keys -defined as constants in lua.h. -The following constants are defined: - -

    -
  • LUA_RIDX_MAINTHREAD: At this index the registry has -the main thread of the state. -(The main thread is the one created together with the state.) -
  • - -
  • LUA_RIDX_GLOBALS: At this index the registry has -the global environment. -
  • -
- - - - -

4.4 – Error Handling in C

- - - -

-Internally, Lua uses the C longjmp facility to handle errors. -(Lua will use exceptions if you compile it as C++; -search for LUAI_THROW in the source code for details.) -When Lua faces any error, -such as a memory allocation error or a type error, -it raises an error; -that is, it does a long jump. -A protected environment uses setjmp -to set a recovery point; -any error jumps to the most recent active recovery point. - - -

-Inside a C function you can raise an error explicitly -by calling lua_error. - - -

-Most functions in the API can raise an error, -for instance due to a memory allocation error. -The documentation for each function indicates whether -it can raise errors. - - -

-If an error happens outside any protected environment, -Lua calls a panic function (see lua_atpanic) -and then calls abort, -thus exiting the host application. -Your panic function can avoid this exit by -never returning -(e.g., doing a long jump to your own recovery point outside Lua). - - -

-The panic function, -as its name implies, -is a mechanism of last resort. -Programs should avoid it. -As a general rule, -when a C function is called by Lua with a Lua state, -it can do whatever it wants on that Lua state, -as it should be already protected. -However, -when C code operates on other Lua states -(e.g., a Lua-state argument to the function, -a Lua state stored in the registry, or -the result of lua_newthread), -it should use them only in API calls that cannot raise errors. - - -

-The panic function runs as if it were a message handler (see §2.3); -in particular, the error object is on the top of the stack. -However, there is no guarantee about stack space. -To push anything on the stack, -the panic function must first check the available space (see §4.1.1). - - - - - -

4.4.1 – Status Codes

- -

-Several functions that report errors in the API use the following -status codes to indicate different kinds of errors or other conditions: - -

    - -
  • LUA_OK (0): no errors.
  • - -
  • LUA_ERRRUN: a runtime error.
  • - -
  • LUA_ERRMEM: -memory allocation error. -For such errors, Lua does not call the message handler. -
  • - -
  • LUA_ERRERR: error while running the message handler.
  • - -
  • LUA_ERRSYNTAX: syntax error during precompilation.
  • - -
  • LUA_YIELD: the thread (coroutine) yields.
  • - -
  • LUA_ERRFILE: a file-related error; -e.g., it cannot open or read the file.
  • - -

-These constants are defined in the header file lua.h. - - - - - - - -

4.5 – Handling Yields in C

- -

-Internally, Lua uses the C longjmp facility to yield a coroutine. -Therefore, if a C function foo calls an API function -and this API function yields -(directly or indirectly by calling another function that yields), -Lua cannot return to foo any more, -because the longjmp removes its frame from the C stack. - - -

-To avoid this kind of problem, -Lua raises an error whenever it tries to yield across an API call, -except for three functions: -lua_yieldk, lua_callk, and lua_pcallk. -All those functions receive a continuation function -(as a parameter named k) to continue execution after a yield. - - -

-We need to set some terminology to explain continuations. -We have a C function called from Lua which we will call -the original function. -This original function then calls one of those three functions in the C API, -which we will call the callee function, -that then yields the current thread. -This can happen when the callee function is lua_yieldk, -or when the callee function is either lua_callk or lua_pcallk -and the function called by them yields. - - -

-Suppose the running thread yields while executing the callee function. -After the thread resumes, -it eventually will finish running the callee function. -However, -the callee function cannot return to the original function, -because its frame in the C stack was destroyed by the yield. -Instead, Lua calls a continuation function, -which was given as an argument to the callee function. -As the name implies, -the continuation function should continue the task -of the original function. - - -

-As an illustration, consider the following function: - -

-     int original_function (lua_State *L) {
-       ...     /* code 1 */
-       status = lua_pcall(L, n, m, h);  /* calls Lua */
-       ...     /* code 2 */
-     }
-

-Now we want to allow -the Lua code being run by lua_pcall to yield. -First, we can rewrite our function like here: - -

-     int k (lua_State *L, int status, lua_KContext ctx) {
-       ...  /* code 2 */
-     }
-     
-     int original_function (lua_State *L) {
-       ...     /* code 1 */
-       return k(L, lua_pcall(L, n, m, h), ctx);
-     }
-

-In the above code, -the new function k is a -continuation function (with type lua_KFunction), -which should do all the work that the original function -was doing after calling lua_pcall. -Now, we must inform Lua that it must call k if the Lua code -being executed by lua_pcall gets interrupted in some way -(errors or yielding), -so we rewrite the code as here, -replacing lua_pcall by lua_pcallk: - -

-     int original_function (lua_State *L) {
-       ...     /* code 1 */
-       return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
-     }
-

-Note the external, explicit call to the continuation: -Lua will call the continuation only if needed, that is, -in case of errors or resuming after a yield. -If the called function returns normally without ever yielding, -lua_pcallk (and lua_callk) will also return normally. -(Of course, instead of calling the continuation in that case, -you can do the equivalent work directly inside the original function.) - - -

-Besides the Lua state, -the continuation function has two other parameters: -the final status of the call and the context value (ctx) that -was passed originally to lua_pcallk. -Lua does not use this context value; -it only passes this value from the original function to the -continuation function. -For lua_pcallk, -the status is the same value that would be returned by lua_pcallk, -except that it is LUA_YIELD when being executed after a yield -(instead of LUA_OK). -For lua_yieldk and lua_callk, -the status is always LUA_YIELD when Lua calls the continuation. -(For these two functions, -Lua will not call the continuation in case of errors, -because they do not handle errors.) -Similarly, when using lua_callk, -you should call the continuation function -with LUA_OK as the status. -(For lua_yieldk, there is not much point in calling -directly the continuation function, -because lua_yieldk usually does not return.) - - -

-Lua treats the continuation function as if it were the original function. -The continuation function receives the same Lua stack -from the original function, -in the same state it would be if the callee function had returned. -(For instance, -after a lua_callk the function and its arguments are -removed from the stack and replaced by the results from the call.) -It also has the same upvalues. -Whatever it returns is handled by Lua as if it were the return -of the original function. - - - - - -

4.6 – Functions and Types

- -

-Here we list all functions and types from the C API in -alphabetical order. -Each function has an indicator like this: -[-o, +p, x] - - -

-The first field, o, -is how many elements the function pops from the stack. -The second field, p, -is how many elements the function pushes onto the stack. -(Any function always pushes its results after popping its arguments.) -A field in the form x|y means the function can push (or pop) -x or y elements, -depending on the situation; -an interrogation mark '?' means that -we cannot know how many elements the function pops/pushes -by looking only at its arguments. -(For instance, they may depend on what is in the stack.) -The third field, x, -tells whether the function may raise errors: -'-' means the function never raises any error; -'m' means the function may raise only out-of-memory errors; -'v' means the function may raise the errors explained in the text; -'e' means the function can run arbitrary Lua code, -either directly or through metamethods, -and therefore may raise any errors. - - - -


lua_absindex

-[-0, +0, –] -

int lua_absindex (lua_State *L, int idx);
- -

-Converts the acceptable index idx -into an equivalent absolute index -(that is, one that does not depend on the stack size). - - - - - -


lua_Alloc

-
typedef void * (*lua_Alloc) (void *ud,
-                             void *ptr,
-                             size_t osize,
-                             size_t nsize);
- -

-The type of the memory-allocation function used by Lua states. -The allocator function must provide a -functionality similar to realloc, -but not exactly the same. -Its arguments are -ud, an opaque pointer passed to lua_newstate; -ptr, a pointer to the block being allocated/reallocated/freed; -osize, the original size of the block or some code about what -is being allocated; -and nsize, the new size of the block. - - -

-When ptr is not NULL, -osize is the size of the block pointed by ptr, -that is, the size given when it was allocated or reallocated. - - -

-When ptr is NULL, -osize encodes the kind of object that Lua is allocating. -osize is any of -LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, -LUA_TUSERDATA, or LUA_TTHREAD when (and only when) -Lua is creating a new object of that type. -When osize is some other value, -Lua is allocating memory for something else. - - -

-Lua assumes the following behavior from the allocator function: - - -

-When nsize is zero, -the allocator must behave like free -and then return NULL. - - -

-When nsize is not zero, -the allocator must behave like realloc. -In particular, the allocator returns NULL -if and only if it cannot fulfill the request. - - -

-Here is a simple implementation for the allocator function. -It is used in the auxiliary library by luaL_newstate. - -

-     static void *l_alloc (void *ud, void *ptr, size_t osize,
-                                                size_t nsize) {
-       (void)ud;  (void)osize;  /* not used */
-       if (nsize == 0) {
-         free(ptr);
-         return NULL;
-       }
-       else
-         return realloc(ptr, nsize);
-     }
-

-Note that Standard C ensures -that free(NULL) has no effect and that -realloc(NULL,size) is equivalent to malloc(size). - - - - - -


lua_arith

-[-(2|1), +1, e] -

void lua_arith (lua_State *L, int op);
- -

-Performs an arithmetic or bitwise operation over the two values -(or one, in the case of negations) -at the top of the stack, -with the value on the top being the second operand, -pops these values, and pushes the result of the operation. -The function follows the semantics of the corresponding Lua operator -(that is, it may call metamethods). - - -

-The value of op must be one of the following constants: - -

- - - - -

lua_atpanic

-[-0, +0, –] -

lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
- -

-Sets a new panic function and returns the old one (see §4.4). - - - - - -


lua_call

-[-(nargs+1), +nresults, e] -

void lua_call (lua_State *L, int nargs, int nresults);
- -

-Calls a function. -Like regular Lua calls, -lua_call respects the __call metamethod. -So, here the word "function" -means any callable value. - - -

-To do a call you must use the following protocol: -first, the function to be called is pushed onto the stack; -then, the arguments to the call are pushed -in direct order; -that is, the first argument is pushed first. -Finally you call lua_call; -nargs is the number of arguments that you pushed onto the stack. -When the function returns, -all arguments and the function value are popped -and the call results are pushed onto the stack. -The number of results is adjusted to nresults, -unless nresults is LUA_MULTRET. -In this case, all results from the function are pushed; -Lua takes care that the returned values fit into the stack space, -but it does not ensure any extra space in the stack. -The function results are pushed onto the stack in direct order -(the first result is pushed first), -so that after the call the last result is on the top of the stack. - - -

-Any error while calling and running the function is propagated upwards -(with a longjmp). - - -

-The following example shows how the host program can do the -equivalent to this Lua code: - -

-     a = f("how", t.x, 14)
-

-Here it is in C: - -

-     lua_getglobal(L, "f");                  /* function to be called */
-     lua_pushliteral(L, "how");                       /* 1st argument */
-     lua_getglobal(L, "t");                    /* table to be indexed */
-     lua_getfield(L, -1, "x");        /* push result of t.x (2nd arg) */
-     lua_remove(L, -2);                  /* remove 't' from the stack */
-     lua_pushinteger(L, 14);                          /* 3rd argument */
-     lua_call(L, 3, 1);     /* call 'f' with 3 arguments and 1 result */
-     lua_setglobal(L, "a");                         /* set global 'a' */
-

-Note that the code above is balanced: -at its end, the stack is back to its original configuration. -This is considered good programming practice. - - - - - -


lua_callk

-[-(nargs + 1), +nresults, e] -

void lua_callk (lua_State *L,
-                int nargs,
-                int nresults,
-                lua_KContext ctx,
-                lua_KFunction k);
- -

-This function behaves exactly like lua_call, -but allows the called function to yield (see §4.5). - - - - - -


lua_CFunction

-
typedef int (*lua_CFunction) (lua_State *L);
- -

-Type for C functions. - - -

-In order to communicate properly with Lua, -a C function must use the following protocol, -which defines the way parameters and results are passed: -a C function receives its arguments from Lua in its stack -in direct order (the first argument is pushed first). -So, when the function starts, -lua_gettop(L) returns the number of arguments received by the function. -The first argument (if any) is at index 1 -and its last argument is at index lua_gettop(L). -To return values to Lua, a C function just pushes them onto the stack, -in direct order (the first result is pushed first), -and returns in C the number of results. -Any other value in the stack below the results will be properly -discarded by Lua. -Like a Lua function, a C function called by Lua can also return -many results. - - -

-As an example, the following function receives a variable number -of numeric arguments and returns their average and their sum: - -

-     static int foo (lua_State *L) {
-       int n = lua_gettop(L);    /* number of arguments */
-       lua_Number sum = 0.0;
-       int i;
-       for (i = 1; i <= n; i++) {
-         if (!lua_isnumber(L, i)) {
-           lua_pushliteral(L, "incorrect argument");
-           lua_error(L);
-         }
-         sum += lua_tonumber(L, i);
-       }
-       lua_pushnumber(L, sum/n);        /* first result */
-       lua_pushnumber(L, sum);         /* second result */
-       return 2;                   /* number of results */
-     }
-
- - - - -

lua_checkstack

-[-0, +0, –] -

int lua_checkstack (lua_State *L, int n);
- -

-Ensures that the stack has space for at least n extra elements, -that is, that you can safely push up to n values into it. -It returns false if it cannot fulfill the request, -either because it would cause the stack -to be greater than a fixed maximum size -(typically at least several thousand elements) or -because it cannot allocate memory for the extra space. -This function never shrinks the stack; -if the stack already has space for the extra elements, -it is left unchanged. - - - - - -


lua_close

-[-0, +0, –] -

void lua_close (lua_State *L);
- -

-Close all active to-be-closed variables in the main thread, -release all objects in the given Lua state -(calling the corresponding garbage-collection metamethods, if any), -and frees all dynamic memory used by this state. - - -

-On several platforms, you may not need to call this function, -because all resources are naturally released when the host program ends. -On the other hand, long-running programs that create multiple states, -such as daemons or web servers, -will probably need to close states as soon as they are not needed. - - - - - -


lua_closeslot

-[-0, +0, e] -

void lua_closeslot (lua_State *L, int index);
- -

-Close the to-be-closed slot at the given index and set its value to nil. -The index must be the last index previously marked to be closed -(see lua_toclose) that is still active (that is, not closed yet). - - -

-A __close metamethod cannot yield -when called through this function. - - -

-(Exceptionally, this function was introduced in release 5.4.3. -It is not present in previous 5.4 releases.) - - - - - -


lua_compare

-[-0, +0, e] -

int lua_compare (lua_State *L, int index1, int index2, int op);
- -

-Compares two Lua values. -Returns 1 if the value at index index1 satisfies op -when compared with the value at index index2, -following the semantics of the corresponding Lua operator -(that is, it may call metamethods). -Otherwise returns 0. -Also returns 0 if any of the indices is not valid. - - -

-The value of op must be one of the following constants: - -

    - -
  • LUA_OPEQ: compares for equality (==)
  • -
  • LUA_OPLT: compares for less than (<)
  • -
  • LUA_OPLE: compares for less or equal (<=)
  • - -
- - - - -

lua_concat

-[-n, +1, e] -

void lua_concat (lua_State *L, int n);
- -

-Concatenates the n values at the top of the stack, -pops them, and leaves the result on the top. -If n is 1, the result is the single value on the stack -(that is, the function does nothing); -if n is 0, the result is the empty string. -Concatenation is performed following the usual semantics of Lua -(see §3.4.6). - - - - - -


lua_copy

-[-0, +0, –] -

void lua_copy (lua_State *L, int fromidx, int toidx);
- -

-Copies the element at index fromidx -into the valid index toidx, -replacing the value at that position. -Values at other positions are not affected. - - - - - -


lua_createtable

-[-0, +1, m] -

void lua_createtable (lua_State *L, int narr, int nrec);
- -

-Creates a new empty table and pushes it onto the stack. -Parameter narr is a hint for how many elements the table -will have as a sequence; -parameter nrec is a hint for how many other elements -the table will have. -Lua may use these hints to preallocate memory for the new table. -This preallocation may help performance when you know in advance -how many elements the table will have. -Otherwise you can use the function lua_newtable. - - - - - -


lua_dump

-[-0, +0, –] -

int lua_dump (lua_State *L,
-                        lua_Writer writer,
-                        void *data,
-                        int strip);
- -

-Dumps a function as a binary chunk. -Receives a Lua function on the top of the stack -and produces a binary chunk that, -if loaded again, -results in a function equivalent to the one dumped. -As it produces parts of the chunk, -lua_dump calls function writer (see lua_Writer) -with the given data -to write them. - - -

-If strip is true, -the binary representation may not include all debug information -about the function, -to save space. - - -

-The value returned is the error code returned by the last -call to the writer; -0 means no errors. - - -

-This function does not pop the Lua function from the stack. - - - - - -


lua_error

-[-1, +0, v] -

int lua_error (lua_State *L);
- -

-Raises a Lua error, -using the value on the top of the stack as the error object. -This function does a long jump, -and therefore never returns -(see luaL_error). - - - - - -


lua_gc

-[-0, +0, –] -

int lua_gc (lua_State *L, int what, ...);
- -

-Controls the garbage collector. - - -

-This function performs several tasks, -according to the value of the parameter what. -For options that need extra arguments, -they are listed after the option. - -

    - -
  • LUA_GCCOLLECT: -Performs a full garbage-collection cycle. -
  • - -
  • LUA_GCSTOP: -Stops the garbage collector. -
  • - -
  • LUA_GCRESTART: -Restarts the garbage collector. -
  • - -
  • LUA_GCCOUNT: -Returns the current amount of memory (in Kbytes) in use by Lua. -
  • - -
  • LUA_GCCOUNTB: -Returns the remainder of dividing the current amount of bytes of -memory in use by Lua by 1024. -
  • - -
  • LUA_GCSTEP (int stepsize): -Performs an incremental step of garbage collection, -corresponding to the allocation of stepsize Kbytes. -
  • - -
  • LUA_GCISRUNNING: -Returns a boolean that tells whether the collector is running -(i.e., not stopped). -
  • - -
  • LUA_GCINC (int pause, int stepmul, stepsize): -Changes the collector to incremental mode -with the given parameters (see §2.5.1). -Returns the previous mode (LUA_GCGEN or LUA_GCINC). -
  • - -
  • LUA_GCGEN (int minormul, int majormul): -Changes the collector to generational mode -with the given parameters (see §2.5.2). -Returns the previous mode (LUA_GCGEN or LUA_GCINC). -
  • - -

-For more details about these options, -see collectgarbage. - - -

-This function should not be called by a finalizer. - - - - - -


lua_getallocf

-[-0, +0, –] -

lua_Alloc lua_getallocf (lua_State *L, void **ud);
- -

-Returns the memory-allocation function of a given state. -If ud is not NULL, Lua stores in *ud the -opaque pointer given when the memory-allocator function was set. - - - - - -


lua_getfield

-[-0, +1, e] -

int lua_getfield (lua_State *L, int index, const char *k);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given index. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - -

-Returns the type of the pushed value. - - - - - -


lua_getextraspace

-[-0, +0, –] -

void *lua_getextraspace (lua_State *L);
- -

-Returns a pointer to a raw memory area associated with the -given Lua state. -The application can use this area for any purpose; -Lua does not use it for anything. - - -

-Each new thread has this area initialized with a copy -of the area of the main thread. - - -

-By default, this area has the size of a pointer to void, -but you can recompile Lua with a different size for this area. -(See LUA_EXTRASPACE in luaconf.h.) - - - - - -


lua_getglobal

-[-0, +1, e] -

int lua_getglobal (lua_State *L, const char *name);
- -

-Pushes onto the stack the value of the global name. -Returns the type of that value. - - - - - -


lua_geti

-[-0, +1, e] -

int lua_geti (lua_State *L, int index, lua_Integer i);
- -

-Pushes onto the stack the value t[i], -where t is the value at the given index. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - -

-Returns the type of the pushed value. - - - - - -


lua_getmetatable

-[-0, +(0|1), –] -

int lua_getmetatable (lua_State *L, int index);
- -

-If the value at the given index has a metatable, -the function pushes that metatable onto the stack and returns 1. -Otherwise, -the function returns 0 and pushes nothing on the stack. - - - - - -


lua_gettable

-[-1, +1, e] -

int lua_gettable (lua_State *L, int index);
- -

-Pushes onto the stack the value t[k], -where t is the value at the given index -and k is the value on the top of the stack. - - -

-This function pops the key from the stack, -pushing the resulting value in its place. -As in Lua, this function may trigger a metamethod -for the "index" event (see §2.4). - - -

-Returns the type of the pushed value. - - - - - -


lua_gettop

-[-0, +0, –] -

int lua_gettop (lua_State *L);
- -

-Returns the index of the top element in the stack. -Because indices start at 1, -this result is equal to the number of elements in the stack; -in particular, 0 means an empty stack. - - - - - -


lua_getiuservalue

-[-0, +1, –] -

int lua_getiuservalue (lua_State *L, int index, int n);
- -

-Pushes onto the stack the n-th user value associated with the -full userdata at the given index and -returns the type of the pushed value. - - -

-If the userdata does not have that value, -pushes nil and returns LUA_TNONE. - - - - - -


lua_insert

-[-1, +1, –] -

void lua_insert (lua_State *L, int index);
- -

-Moves the top element into the given valid index, -shifting up the elements above this index to open space. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_Integer

-
typedef ... lua_Integer;
- -

-The type of integers in Lua. - - -

-By default this type is long long, -(usually a 64-bit two-complement integer), -but that can be changed to long or int -(usually a 32-bit two-complement integer). -(See LUA_INT_TYPE in luaconf.h.) - - -

-Lua also defines the constants -LUA_MININTEGER and LUA_MAXINTEGER, -with the minimum and the maximum values that fit in this type. - - - - - -


lua_isboolean

-[-0, +0, –] -

int lua_isboolean (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a boolean, -and 0 otherwise. - - - - - -


lua_iscfunction

-[-0, +0, –] -

int lua_iscfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a C function, -and 0 otherwise. - - - - - -


lua_isfunction

-[-0, +0, –] -

int lua_isfunction (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a function -(either C or Lua), and 0 otherwise. - - - - - -


lua_isinteger

-[-0, +0, –] -

int lua_isinteger (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is an integer -(that is, the value is a number and is represented as an integer), -and 0 otherwise. - - - - - -


lua_islightuserdata

-[-0, +0, –] -

int lua_islightuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a light userdata, -and 0 otherwise. - - - - - -


lua_isnil

-[-0, +0, –] -

int lua_isnil (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is nil, -and 0 otherwise. - - - - - -


lua_isnone

-[-0, +0, –] -

int lua_isnone (lua_State *L, int index);
- -

-Returns 1 if the given index is not valid, -and 0 otherwise. - - - - - -


lua_isnoneornil

-[-0, +0, –] -

int lua_isnoneornil (lua_State *L, int index);
- -

-Returns 1 if the given index is not valid -or if the value at this index is nil, -and 0 otherwise. - - - - - -


lua_isnumber

-[-0, +0, –] -

int lua_isnumber (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a number -or a string convertible to a number, -and 0 otherwise. - - - - - -


lua_isstring

-[-0, +0, –] -

int lua_isstring (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a string -or a number (which is always convertible to a string), -and 0 otherwise. - - - - - -


lua_istable

-[-0, +0, –] -

int lua_istable (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a table, -and 0 otherwise. - - - - - -


lua_isthread

-[-0, +0, –] -

int lua_isthread (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a thread, -and 0 otherwise. - - - - - -


lua_isuserdata

-[-0, +0, –] -

int lua_isuserdata (lua_State *L, int index);
- -

-Returns 1 if the value at the given index is a userdata -(either full or light), and 0 otherwise. - - - - - -


lua_isyieldable

-[-0, +0, –] -

int lua_isyieldable (lua_State *L);
- -

-Returns 1 if the given coroutine can yield, -and 0 otherwise. - - - - - -


lua_KContext

-
typedef ... lua_KContext;
- -

-The type for continuation-function contexts. -It must be a numeric type. -This type is defined as intptr_t -when intptr_t is available, -so that it can store pointers too. -Otherwise, it is defined as ptrdiff_t. - - - - - -


lua_KFunction

-
typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
- -

-Type for continuation functions (see §4.5). - - - - - -


lua_len

-[-0, +1, e] -

void lua_len (lua_State *L, int index);
- -

-Returns the length of the value at the given index. -It is equivalent to the '#' operator in Lua (see §3.4.7) and -may trigger a metamethod for the "length" event (see §2.4). -The result is pushed on the stack. - - - - - -


lua_load

-[-0, +1, –] -

int lua_load (lua_State *L,
-              lua_Reader reader,
-              void *data,
-              const char *chunkname,
-              const char *mode);
- -

-Loads a Lua chunk without running it. -If there are no errors, -lua_load pushes the compiled chunk as a Lua -function on top of the stack. -Otherwise, it pushes an error message. - - -

-The lua_load function uses a user-supplied reader function -to read the chunk (see lua_Reader). -The data argument is an opaque value passed to the reader function. - - -

-The chunkname argument gives a name to the chunk, -which is used for error messages and in debug information (see §4.7). - - -

-lua_load automatically detects whether the chunk is text or binary -and loads it accordingly (see program luac). -The string mode works as in function load, -with the addition that -a NULL value is equivalent to the string "bt". - - -

-lua_load uses the stack internally, -so the reader function must always leave the stack -unmodified when returning. - - -

-lua_load can return -LUA_OK, LUA_ERRSYNTAX, or LUA_ERRMEM. -The function may also return other values corresponding to -errors raised by the read function (see §4.4.1). - - -

-If the resulting function has upvalues, -its first upvalue is set to the value of the global environment -stored at index LUA_RIDX_GLOBALS in the registry (see §4.3). -When loading main chunks, -this upvalue will be the _ENV variable (see §2.2). -Other upvalues are initialized with nil. - - - - - -


lua_newstate

-[-0, +0, –] -

lua_State *lua_newstate (lua_Alloc f, void *ud);
- -

-Creates a new independent state and returns its main thread. -Returns NULL if it cannot create the state -(due to lack of memory). -The argument f is the allocator function; -Lua will do all memory allocation for this state -through this function (see lua_Alloc). -The second argument, ud, is an opaque pointer that Lua -passes to the allocator in every call. - - - - - -


lua_newtable

-[-0, +1, m] -

void lua_newtable (lua_State *L);
- -

-Creates a new empty table and pushes it onto the stack. -It is equivalent to lua_createtable(L, 0, 0). - - - - - -


lua_newthread

-[-0, +1, m] -

lua_State *lua_newthread (lua_State *L);
- -

-Creates a new thread, pushes it on the stack, -and returns a pointer to a lua_State that represents this new thread. -The new thread returned by this function shares with the original thread -its global environment, -but has an independent execution stack. - - -

-Threads are subject to garbage collection, -like any Lua object. - - - - - -


lua_newuserdatauv

-[-0, +1, m] -

void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue);
- -

-This function creates and pushes on the stack a new full userdata, -with nuvalue associated Lua values, called user values, -plus an associated block of raw memory with size bytes. -(The user values can be set and read with the functions -lua_setiuservalue and lua_getiuservalue.) - - -

-The function returns the address of the block of memory. -Lua ensures that this address is valid as long as -the corresponding userdata is alive (see §2.5). -Moreover, if the userdata is marked for finalization (see §2.5.3), -its address is valid at least until the call to its finalizer. - - - - - -


lua_next

-[-1, +(2|0), v] -

int lua_next (lua_State *L, int index);
- -

-Pops a key from the stack, -and pushes a key–value pair from the table at the given index, -the "next" pair after the given key. -If there are no more elements in the table, -then lua_next returns 0 and pushes nothing. - - -

-A typical table traversal looks like this: - -

-     /* table is in the stack at index 't' */
-     lua_pushnil(L);  /* first key */
-     while (lua_next(L, t) != 0) {
-       /* uses 'key' (at index -2) and 'value' (at index -1) */
-       printf("%s - %s\n",
-              lua_typename(L, lua_type(L, -2)),
-              lua_typename(L, lua_type(L, -1)));
-       /* removes 'value'; keeps 'key' for next iteration */
-       lua_pop(L, 1);
-     }
-
- -

-While traversing a table, -avoid calling lua_tolstring directly on a key, -unless you know that the key is actually a string. -Recall that lua_tolstring may change -the value at the given index; -this confuses the next call to lua_next. - - -

-This function may raise an error if the given key -is neither nil nor present in the table. -See function next for the caveats of modifying -the table during its traversal. - - - - - -


lua_Number

-
typedef ... lua_Number;
- -

-The type of floats in Lua. - - -

-By default this type is double, -but that can be changed to a single float or a long double. -(See LUA_FLOAT_TYPE in luaconf.h.) - - - - - -


lua_numbertointeger

-
int lua_numbertointeger (lua_Number n, lua_Integer *p);
- -

-Tries to convert a Lua float to a Lua integer; -the float n must have an integral value. -If that value is within the range of Lua integers, -it is converted to an integer and assigned to *p. -The macro results in a boolean indicating whether the -conversion was successful. -(Note that this range test can be tricky to do -correctly without this macro, due to rounding.) - - -

-This macro may evaluate its arguments more than once. - - - - - -


lua_pcall

-[-(nargs + 1), +(nresults|1), –] -

int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);
- -

-Calls a function (or a callable object) in protected mode. - - -

-Both nargs and nresults have the same meaning as -in lua_call. -If there are no errors during the call, -lua_pcall behaves exactly like lua_call. -However, if there is any error, -lua_pcall catches it, -pushes a single value on the stack (the error object), -and returns an error code. -Like lua_call, -lua_pcall always removes the function -and its arguments from the stack. - - -

-If msgh is 0, -then the error object returned on the stack -is exactly the original error object. -Otherwise, msgh is the stack index of a -message handler. -(This index cannot be a pseudo-index.) -In case of runtime errors, -this handler will be called with the error object -and its return value will be the object -returned on the stack by lua_pcall. - - -

-Typically, the message handler is used to add more debug -information to the error object, such as a stack traceback. -Such information cannot be gathered after the return of lua_pcall, -since by then the stack has unwound. - - -

-The lua_pcall function returns one of the following status codes: -LUA_OK, LUA_ERRRUN, LUA_ERRMEM, or LUA_ERRERR. - - - - - -


lua_pcallk

-[-(nargs + 1), +(nresults|1), –] -

int lua_pcallk (lua_State *L,
-                int nargs,
-                int nresults,
-                int msgh,
-                lua_KContext ctx,
-                lua_KFunction k);
- -

-This function behaves exactly like lua_pcall, -except that it allows the called function to yield (see §4.5). - - - - - -


lua_pop

-[-n, +0, e] -

void lua_pop (lua_State *L, int n);
- -

-Pops n elements from the stack. -It is implemented as a macro over lua_settop. - - - - - -


lua_pushboolean

-[-0, +1, –] -

void lua_pushboolean (lua_State *L, int b);
- -

-Pushes a boolean value with value b onto the stack. - - - - - -


lua_pushcclosure

-[-n, +1, m] -

void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
- -

-Pushes a new C closure onto the stack. -This function receives a pointer to a C function -and pushes onto the stack a Lua value of type function that, -when called, invokes the corresponding C function. -The parameter n tells how many upvalues this function will have -(see §4.2). - - -

-Any function to be callable by Lua must -follow the correct protocol to receive its parameters -and return its results (see lua_CFunction). - - -

-When a C function is created, -it is possible to associate some values with it, -the so called upvalues; -these upvalues are then accessible to the function whenever it is called. -This association is called a C closure (see §4.2). -To create a C closure, -first the initial values for its upvalues must be pushed onto the stack. -(When there are multiple upvalues, the first value is pushed first.) -Then lua_pushcclosure -is called to create and push the C function onto the stack, -with the argument n telling how many values will be -associated with the function. -lua_pushcclosure also pops these values from the stack. - - -

-The maximum value for n is 255. - - -

-When n is zero, -this function creates a light C function, -which is just a pointer to the C function. -In that case, it never raises a memory error. - - - - - -


lua_pushcfunction

-[-0, +1, –] -

void lua_pushcfunction (lua_State *L, lua_CFunction f);
- -

-Pushes a C function onto the stack. -This function is equivalent to lua_pushcclosure with no upvalues. - - - - - -


lua_pushfstring

-[-0, +1, v] -

const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
- -

-Pushes onto the stack a formatted string -and returns a pointer to this string (see §4.1.3). -It is similar to the ISO C function sprintf, -but has two important differences. -First, -you do not have to allocate space for the result; -the result is a Lua string and Lua takes care of memory allocation -(and deallocation, through garbage collection). -Second, -the conversion specifiers are quite restricted. -There are no flags, widths, or precisions. -The conversion specifiers can only be -'%%' (inserts the character '%'), -'%s' (inserts a zero-terminated string, with no size restrictions), -'%f' (inserts a lua_Number), -'%I' (inserts a lua_Integer), -'%p' (inserts a pointer), -'%d' (inserts an int), -'%c' (inserts an int as a one-byte character), and -'%U' (inserts a long int as a UTF-8 byte sequence). - - -

-This function may raise errors due to memory overflow -or an invalid conversion specifier. - - - - - -


lua_pushglobaltable

-[-0, +1, –] -

void lua_pushglobaltable (lua_State *L);
- -

-Pushes the global environment onto the stack. - - - - - -


lua_pushinteger

-[-0, +1, –] -

void lua_pushinteger (lua_State *L, lua_Integer n);
- -

-Pushes an integer with value n onto the stack. - - - - - -


lua_pushlightuserdata

-[-0, +1, –] -

void lua_pushlightuserdata (lua_State *L, void *p);
- -

-Pushes a light userdata onto the stack. - - -

-Userdata represent C values in Lua. -A light userdata represents a pointer, a void*. -It is a value (like a number): -you do not create it, it has no individual metatable, -and it is not collected (as it was never created). -A light userdata is equal to "any" -light userdata with the same C address. - - - - - -


lua_pushliteral

-[-0, +1, m] -

const char *lua_pushliteral (lua_State *L, const char *s);
- -

-This macro is equivalent to lua_pushstring, -but should be used only when s is a literal string. -(Lua may optimize this case.) - - - - - -


lua_pushlstring

-[-0, +1, m] -

const char *lua_pushlstring (lua_State *L, const char *s, size_t len);
- -

-Pushes the string pointed to by s with size len -onto the stack. -Lua will make or reuse an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. -The string can contain any binary data, -including embedded zeros. - - -

-Returns a pointer to the internal copy of the string (see §4.1.3). - - - - - -


lua_pushnil

-[-0, +1, –] -

void lua_pushnil (lua_State *L);
- -

-Pushes a nil value onto the stack. - - - - - -


lua_pushnumber

-[-0, +1, –] -

void lua_pushnumber (lua_State *L, lua_Number n);
- -

-Pushes a float with value n onto the stack. - - - - - -


lua_pushstring

-[-0, +1, m] -

const char *lua_pushstring (lua_State *L, const char *s);
- -

-Pushes the zero-terminated string pointed to by s -onto the stack. -Lua will make or reuse an internal copy of the given string, -so the memory at s can be freed or reused immediately after -the function returns. - - -

-Returns a pointer to the internal copy of the string (see §4.1.3). - - -

-If s is NULL, pushes nil and returns NULL. - - - - - -


lua_pushthread

-[-0, +1, –] -

int lua_pushthread (lua_State *L);
- -

-Pushes the thread represented by L onto the stack. -Returns 1 if this thread is the main thread of its state. - - - - - -


lua_pushvalue

-[-0, +1, –] -

void lua_pushvalue (lua_State *L, int index);
- -

-Pushes a copy of the element at the given index -onto the stack. - - - - - -


lua_pushvfstring

-[-0, +1, v] -

const char *lua_pushvfstring (lua_State *L,
-                              const char *fmt,
-                              va_list argp);
- -

-Equivalent to lua_pushfstring, except that it receives a va_list -instead of a variable number of arguments. - - - - - -


lua_rawequal

-[-0, +0, –] -

int lua_rawequal (lua_State *L, int index1, int index2);
- -

-Returns 1 if the two values in indices index1 and -index2 are primitively equal -(that is, equal without calling the __eq metamethod). -Otherwise returns 0. -Also returns 0 if any of the indices are not valid. - - - - - -


lua_rawget

-[-1, +1, –] -

int lua_rawget (lua_State *L, int index);
- -

-Similar to lua_gettable, but does a raw access -(i.e., without metamethods). - - - - - -


lua_rawgeti

-[-0, +1, –] -

int lua_rawgeti (lua_State *L, int index, lua_Integer n);
- -

-Pushes onto the stack the value t[n], -where t is the table at the given index. -The access is raw, -that is, it does not use the __index metavalue. - - -

-Returns the type of the pushed value. - - - - - -


lua_rawgetp

-[-0, +1, –] -

int lua_rawgetp (lua_State *L, int index, const void *p);
- -

-Pushes onto the stack the value t[k], -where t is the table at the given index and -k is the pointer p represented as a light userdata. -The access is raw; -that is, it does not use the __index metavalue. - - -

-Returns the type of the pushed value. - - - - - -


lua_rawlen

-[-0, +0, –] -

lua_Unsigned lua_rawlen (lua_State *L, int index);
- -

-Returns the raw "length" of the value at the given index: -for strings, this is the string length; -for tables, this is the result of the length operator ('#') -with no metamethods; -for userdata, this is the size of the block of memory allocated -for the userdata. -For other values, this call returns 0. - - - - - -


lua_rawset

-[-2, +0, m] -

void lua_rawset (lua_State *L, int index);
- -

-Similar to lua_settable, but does a raw assignment -(i.e., without metamethods). - - - - - -


lua_rawseti

-[-1, +0, m] -

void lua_rawseti (lua_State *L, int index, lua_Integer i);
- -

-Does the equivalent of t[i] = v, -where t is the table at the given index -and v is the value on the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw, -that is, it does not use the __newindex metavalue. - - - - - -


lua_rawsetp

-[-1, +0, m] -

void lua_rawsetp (lua_State *L, int index, const void *p);
- -

-Does the equivalent of t[p] = v, -where t is the table at the given index, -p is encoded as a light userdata, -and v is the value on the top of the stack. - - -

-This function pops the value from the stack. -The assignment is raw, -that is, it does not use the __newindex metavalue. - - - - - -


lua_Reader

-
typedef const char * (*lua_Reader) (lua_State *L,
-                                    void *data,
-                                    size_t *size);
- -

-The reader function used by lua_load. -Every time lua_load needs another piece of the chunk, -it calls the reader, -passing along its data parameter. -The reader must return a pointer to a block of memory -with a new piece of the chunk -and set size to the block size. -The block must exist until the reader function is called again. -To signal the end of the chunk, -the reader must return NULL or set size to zero. -The reader function may return pieces of any size greater than zero. - - - - - -


lua_register

-[-0, +0, e] -

void lua_register (lua_State *L, const char *name, lua_CFunction f);
- -

-Sets the C function f as the new value of global name. -It is defined as a macro: - -

-     #define lua_register(L,n,f) \
-            (lua_pushcfunction(L, f), lua_setglobal(L, n))
-
- - - - -

lua_remove

-[-1, +0, –] -

void lua_remove (lua_State *L, int index);
- -

-Removes the element at the given valid index, -shifting down the elements above this index to fill the gap. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_replace

-[-1, +0, –] -

void lua_replace (lua_State *L, int index);
- -

-Moves the top element into the given valid index -without shifting any element -(therefore replacing the value at that given index), -and then pops the top element. - - - - - -


lua_resetthread

-[-0, +?, –] -

int lua_resetthread (lua_State *L);
- -

-Resets a thread, cleaning its call stack and closing all pending -to-be-closed variables. -Returns a status code: -LUA_OK for no errors in the thread -(either the original error that stopped the thread or -errors in closing methods), -or an error status otherwise. -In case of error, -leaves the error object on the top of the stack. - - - - - -


lua_resume

-[-?, +?, –] -

int lua_resume (lua_State *L, lua_State *from, int nargs,
-                          int *nresults);
- -

-Starts and resumes a coroutine in the given thread L. - - -

-To start a coroutine, -you push the main function plus any arguments -onto the empty stack of the thread. -then you call lua_resume, -with nargs being the number of arguments. -This call returns when the coroutine suspends or finishes its execution. -When it returns, -*nresults is updated and -the top of the stack contains -the *nresults values passed to lua_yield -or returned by the body function. -lua_resume returns -LUA_YIELD if the coroutine yields, -LUA_OK if the coroutine finishes its execution -without errors, -or an error code in case of errors (see §4.4.1). -In case of errors, -the error object is on the top of the stack. - - -

-To resume a coroutine, -you remove the *nresults yielded values from its stack, -push the values to be passed as results from yield, -and then call lua_resume. - - -

-The parameter from represents the coroutine that is resuming L. -If there is no such coroutine, -this parameter can be NULL. - - - - - -


lua_rotate

-[-0, +0, –] -

void lua_rotate (lua_State *L, int idx, int n);
- -

-Rotates the stack elements between the valid index idx -and the top of the stack. -The elements are rotated n positions in the direction of the top, -for a positive n, -or -n positions in the direction of the bottom, -for a negative n. -The absolute value of n must not be greater than the size -of the slice being rotated. -This function cannot be called with a pseudo-index, -because a pseudo-index is not an actual stack position. - - - - - -


lua_setallocf

-[-0, +0, –] -

void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
- -

-Changes the allocator function of a given state to f -with user data ud. - - - - - -


lua_setfield

-[-1, +0, e] -

void lua_setfield (lua_State *L, int index, const char *k);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given index -and v is the value on the top of the stack. - - -

-This function pops the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_setglobal

-[-1, +0, e] -

void lua_setglobal (lua_State *L, const char *name);
- -

-Pops a value from the stack and -sets it as the new value of global name. - - - - - -


lua_seti

-[-1, +0, e] -

void lua_seti (lua_State *L, int index, lua_Integer n);
- -

-Does the equivalent to t[n] = v, -where t is the value at the given index -and v is the value on the top of the stack. - - -

-This function pops the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_setiuservalue

-[-1, +0, –] -

int lua_setiuservalue (lua_State *L, int index, int n);
- -

-Pops a value from the stack and sets it as -the new n-th user value associated to the -full userdata at the given index. -Returns 0 if the userdata does not have that value. - - - - - -


lua_setmetatable

-[-1, +0, –] -

int lua_setmetatable (lua_State *L, int index);
- -

-Pops a table or nil from the stack and -sets that value as the new metatable for the value at the given index. -(nil means no metatable.) - - -

-(For historical reasons, this function returns an int, -which now is always 1.) - - - - - -


lua_settable

-[-2, +0, e] -

void lua_settable (lua_State *L, int index);
- -

-Does the equivalent to t[k] = v, -where t is the value at the given index, -v is the value on the top of the stack, -and k is the value just below the top. - - -

-This function pops both the key and the value from the stack. -As in Lua, this function may trigger a metamethod -for the "newindex" event (see §2.4). - - - - - -


lua_settop

-[-?, +?, e] -

void lua_settop (lua_State *L, int index);
- -

-Accepts any index, or 0, -and sets the stack top to this index. -If the new top is greater than the old one, -then the new elements are filled with nil. -If index is 0, then all stack elements are removed. - - -

-This function can run arbitrary code when removing an index -marked as to-be-closed from the stack. - - - - - -


lua_setwarnf

-[-0, +0, –] -

void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);
- -

-Sets the warning function to be used by Lua to emit warnings -(see lua_WarnFunction). -The ud parameter sets the value ud passed to -the warning function. - - - - - -


lua_State

-
typedef struct lua_State lua_State;
- -

-An opaque structure that points to a thread and indirectly -(through the thread) to the whole state of a Lua interpreter. -The Lua library is fully reentrant: -it has no global variables. -All information about a state is accessible through this structure. - - -

-A pointer to this structure must be passed as the first argument to -every function in the library, except to lua_newstate, -which creates a Lua state from scratch. - - - - - -


lua_status

-[-0, +0, –] -

int lua_status (lua_State *L);
- -

-Returns the status of the thread L. - - -

-The status can be LUA_OK for a normal thread, -an error code if the thread finished the execution -of a lua_resume with an error, -or LUA_YIELD if the thread is suspended. - - -

-You can call functions only in threads with status LUA_OK. -You can resume threads with status LUA_OK -(to start a new coroutine) or LUA_YIELD -(to resume a coroutine). - - - - - -


lua_stringtonumber

-[-0, +1, –] -

size_t lua_stringtonumber (lua_State *L, const char *s);
- -

-Converts the zero-terminated string s to a number, -pushes that number into the stack, -and returns the total size of the string, -that is, its length plus one. -The conversion can result in an integer or a float, -according to the lexical conventions of Lua (see §3.1). -The string may have leading and trailing whitespaces and a sign. -If the string is not a valid numeral, -returns 0 and pushes nothing. -(Note that the result can be used as a boolean, -true if the conversion succeeds.) - - - - - -


lua_toboolean

-[-0, +0, –] -

int lua_toboolean (lua_State *L, int index);
- -

-Converts the Lua value at the given index to a C boolean -value (0 or 1). -Like all tests in Lua, -lua_toboolean returns true for any Lua value -different from false and nil; -otherwise it returns false. -(If you want to accept only actual boolean values, -use lua_isboolean to test the value's type.) - - - - - -


lua_tocfunction

-[-0, +0, –] -

lua_CFunction lua_tocfunction (lua_State *L, int index);
- -

-Converts a value at the given index to a C function. -That value must be a C function; -otherwise, returns NULL. - - - - - -


lua_toclose

-[-0, +0, m] -

void lua_toclose (lua_State *L, int index);
- -

-Marks the given index in the stack as a -to-be-closed slot (see §3.3.8). -Like a to-be-closed variable in Lua, -the value at that slot in the stack will be closed -when it goes out of scope. -Here, in the context of a C function, -to go out of scope means that the running function returns to Lua, -or there is an error, -or the slot is removed from the stack through -lua_settop or lua_pop, -or there is a call to lua_closeslot. -A slot marked as to-be-closed should not be removed from the stack -by any other function in the API except lua_settop or lua_pop, -unless previously deactivated by lua_closeslot. - - -

-This function should not be called for an index -that is equal to or below an active to-be-closed slot. - - -

-Note that, both in case of errors and of a regular return, -by the time the __close metamethod runs, -the C stack was already unwound, -so that any automatic C variable declared in the calling function -(e.g., a buffer) will be out of scope. - - - - - -


lua_tointeger

-[-0, +0, –] -

lua_Integer lua_tointeger (lua_State *L, int index);
- -

-Equivalent to lua_tointegerx with isnum equal to NULL. - - - - - -


lua_tointegerx

-[-0, +0, –] -

lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the signed integral type lua_Integer. -The Lua value must be an integer, -or a number or string convertible to an integer (see §3.4.3); -otherwise, lua_tointegerx returns 0. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_tolstring

-[-0, +0, m] -

const char *lua_tolstring (lua_State *L, int index, size_t *len);
- -

-Converts the Lua value at the given index to a C string. -If len is not NULL, -it sets *len with the string length. -The Lua value must be a string or a number; -otherwise, the function returns NULL. -If the value is a number, -then lua_tolstring also -changes the actual value in the stack to a string. -(This change confuses lua_next -when lua_tolstring is applied to keys during a table traversal.) - - -

-lua_tolstring returns a pointer -to a string inside the Lua state (see §4.1.3). -This string always has a zero ('\0') -after its last character (as in C), -but can contain other zeros in its body. - - - - - -


lua_tonumber

-[-0, +0, –] -

lua_Number lua_tonumber (lua_State *L, int index);
- -

-Equivalent to lua_tonumberx with isnum equal to NULL. - - - - - -


lua_tonumberx

-[-0, +0, –] -

lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);
- -

-Converts the Lua value at the given index -to the C type lua_Number (see lua_Number). -The Lua value must be a number or a string convertible to a number -(see §3.4.3); -otherwise, lua_tonumberx returns 0. - - -

-If isnum is not NULL, -its referent is assigned a boolean value that -indicates whether the operation succeeded. - - - - - -


lua_topointer

-[-0, +0, –] -

const void *lua_topointer (lua_State *L, int index);
- -

-Converts the value at the given index to a generic -C pointer (void*). -The value can be a userdata, a table, a thread, a string, or a function; -otherwise, lua_topointer returns NULL. -Different objects will give different pointers. -There is no way to convert the pointer back to its original value. - - -

-Typically this function is used only for hashing and debug information. - - - - - -


lua_tostring

-[-0, +0, m] -

const char *lua_tostring (lua_State *L, int index);
- -

-Equivalent to lua_tolstring with len equal to NULL. - - - - - -


lua_tothread

-[-0, +0, –] -

lua_State *lua_tothread (lua_State *L, int index);
- -

-Converts the value at the given index to a Lua thread -(represented as lua_State*). -This value must be a thread; -otherwise, the function returns NULL. - - - - - -


lua_touserdata

-[-0, +0, –] -

void *lua_touserdata (lua_State *L, int index);
- -

-If the value at the given index is a full userdata, -returns its memory-block address. -If the value is a light userdata, -returns its value (a pointer). -Otherwise, returns NULL. - - - - - -


lua_type

-[-0, +0, –] -

int lua_type (lua_State *L, int index);
- -

-Returns the type of the value in the given valid index, -or LUA_TNONE for a non-valid but acceptable index. -The types returned by lua_type are coded by the following constants -defined in lua.h: -LUA_TNIL, -LUA_TNUMBER, -LUA_TBOOLEAN, -LUA_TSTRING, -LUA_TTABLE, -LUA_TFUNCTION, -LUA_TUSERDATA, -LUA_TTHREAD, -and -LUA_TLIGHTUSERDATA. - - - - - -


lua_typename

-[-0, +0, –] -

const char *lua_typename (lua_State *L, int tp);
- -

-Returns the name of the type encoded by the value tp, -which must be one the values returned by lua_type. - - - - - -


lua_Unsigned

-
typedef ... lua_Unsigned;
- -

-The unsigned version of lua_Integer. - - - - - -


lua_upvalueindex

-[-0, +0, –] -

int lua_upvalueindex (int i);
- -

-Returns the pseudo-index that represents the i-th upvalue of -the running function (see §4.2). -i must be in the range [1,256]. - - - - - -


lua_version

-[-0, +0, –] -

lua_Number lua_version (lua_State *L);
- -

-Returns the version number of this core. - - - - - -


lua_WarnFunction

-
typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
- -

-The type of warning functions, called by Lua to emit warnings. -The first parameter is an opaque pointer -set by lua_setwarnf. -The second parameter is the warning message. -The third parameter is a boolean that -indicates whether the message is -to be continued by the message in the next call. - - -

-See warn for more details about warnings. - - - - - -


lua_warning

-[-0, +0, –] -

void lua_warning (lua_State *L, const char *msg, int tocont);
- -

-Emits a warning with the given message. -A message in a call with tocont true should be -continued in another call to this function. - - -

-See warn for more details about warnings. - - - - - -


lua_Writer

-
typedef int (*lua_Writer) (lua_State *L,
-                           const void* p,
-                           size_t sz,
-                           void* ud);
- -

-The type of the writer function used by lua_dump. -Every time lua_dump produces another piece of chunk, -it calls the writer, -passing along the buffer to be written (p), -its size (sz), -and the ud parameter supplied to lua_dump. - - -

-The writer returns an error code: -0 means no errors; -any other value means an error and stops lua_dump from -calling the writer again. - - - - - -


lua_xmove

-[-?, +?, –] -

void lua_xmove (lua_State *from, lua_State *to, int n);
- -

-Exchange values between different threads of the same state. - - -

-This function pops n values from the stack from, -and pushes them onto the stack to. - - - - - -


lua_yield

-[-?, +?, v] -

int lua_yield (lua_State *L, int nresults);
- -

-This function is equivalent to lua_yieldk, -but it has no continuation (see §4.5). -Therefore, when the thread resumes, -it continues the function that called -the function calling lua_yield. -To avoid surprises, -this function should be called only in a tail call. - - - - - -


lua_yieldk

-[-?, +?, v] -

int lua_yieldk (lua_State *L,
-                int nresults,
-                lua_KContext ctx,
-                lua_KFunction k);
- -

-Yields a coroutine (thread). - - -

-When a C function calls lua_yieldk, -the running coroutine suspends its execution, -and the call to lua_resume that started this coroutine returns. -The parameter nresults is the number of values from the stack -that will be passed as results to lua_resume. - - -

-When the coroutine is resumed again, -Lua calls the given continuation function k to continue -the execution of the C function that yielded (see §4.5). -This continuation function receives the same stack -from the previous function, -with the n results removed and -replaced by the arguments passed to lua_resume. -Moreover, -the continuation function receives the value ctx -that was passed to lua_yieldk. - - -

-Usually, this function does not return; -when the coroutine eventually resumes, -it continues executing the continuation function. -However, there is one special case, -which is when this function is called -from inside a line or a count hook (see §4.7). -In that case, lua_yieldk should be called with no continuation -(probably in the form of lua_yield) and no results, -and the hook should return immediately after the call. -Lua will yield and, -when the coroutine resumes again, -it will continue the normal execution -of the (Lua) function that triggered the hook. - - -

-This function can raise an error if it is called from a thread -with a pending C call with no continuation function -(what is called a C-call boundary), -or it is called from a thread that is not running inside a resume -(typically the main thread). - - - - - - - -

4.7 – The Debug Interface

- -

-Lua has no built-in debugging facilities. -Instead, it offers a special interface -by means of functions and hooks. -This interface allows the construction of different -kinds of debuggers, profilers, and other tools -that need "inside information" from the interpreter. - - - -


lua_Debug

-
typedef struct lua_Debug {
-  int event;
-  const char *name;           /* (n) */
-  const char *namewhat;       /* (n) */
-  const char *what;           /* (S) */
-  const char *source;         /* (S) */
-  size_t srclen;              /* (S) */
-  int currentline;            /* (l) */
-  int linedefined;            /* (S) */
-  int lastlinedefined;        /* (S) */
-  unsigned char nups;         /* (u) number of upvalues */
-  unsigned char nparams;      /* (u) number of parameters */
-  char isvararg;              /* (u) */
-  char istailcall;            /* (t) */
-  unsigned short ftransfer;   /* (r) index of first value transferred */
-  unsigned short ntransfer;   /* (r) number of transferred values */
-  char short_src[LUA_IDSIZE]; /* (S) */
-  /* private part */
-  other fields
-} lua_Debug;
- -

-A structure used to carry different pieces of -information about a function or an activation record. -lua_getstack fills only the private part -of this structure, for later use. -To fill the other fields of lua_Debug with useful information, -you must call lua_getinfo with an appropriate parameter. -(Specifically, to get a field, -you must add the letter between parentheses in the field's comment -to the parameter what of lua_getinfo.) - - -

-The fields of lua_Debug have the following meaning: - -

    - -
  • source: -the source of the chunk that created the function. -If source starts with a '@', -it means that the function was defined in a file where -the file name follows the '@'. -If source starts with a '=', -the remainder of its contents describes the source in a user-dependent manner. -Otherwise, -the function was defined in a string where -source is that string. -
  • - -
  • srclen: -The length of the string source. -
  • - -
  • short_src: -a "printable" version of source, to be used in error messages. -
  • - -
  • linedefined: -the line number where the definition of the function starts. -
  • - -
  • lastlinedefined: -the line number where the definition of the function ends. -
  • - -
  • what: -the string "Lua" if the function is a Lua function, -"C" if it is a C function, -"main" if it is the main part of a chunk. -
  • - -
  • currentline: -the current line where the given function is executing. -When no line information is available, -currentline is set to -1. -
  • - -
  • name: -a reasonable name for the given function. -Because functions in Lua are first-class values, -they do not have a fixed name: -some functions can be the value of multiple global variables, -while others can be stored only in a table field. -The lua_getinfo function checks how the function was -called to find a suitable name. -If it cannot find a name, -then name is set to NULL. -
  • - -
  • namewhat: -explains the name field. -The value of namewhat can be -"global", "local", "method", -"field", "upvalue", or "" (the empty string), -according to how the function was called. -(Lua uses the empty string when no other option seems to apply.) -
  • - -
  • istailcall: -true if this function invocation was called by a tail call. -In this case, the caller of this level is not in the stack. -
  • - -
  • nups: -the number of upvalues of the function. -
  • - -
  • nparams: -the number of parameters of the function -(always 0 for C functions). -
  • - -
  • isvararg: -true if the function is a vararg function -(always true for C functions). -
  • - -
  • ftransfer: -the index in the stack of the first value being "transferred", -that is, parameters in a call or return values in a return. -(The other values are in consecutive indices.) -Using this index, you can access and modify these values -through lua_getlocal and lua_setlocal. -This field is only meaningful during a -call hook, denoting the first parameter, -or a return hook, denoting the first value being returned. -(For call hooks, this value is always 1.) -
  • - -
  • ntransfer: -The number of values being transferred (see previous item). -(For calls of Lua functions, -this value is always equal to nparams.) -
  • - -
- - - - -

lua_gethook

-[-0, +0, –] -

lua_Hook lua_gethook (lua_State *L);
- -

-Returns the current hook function. - - - - - -


lua_gethookcount

-[-0, +0, –] -

int lua_gethookcount (lua_State *L);
- -

-Returns the current hook count. - - - - - -


lua_gethookmask

-[-0, +0, –] -

int lua_gethookmask (lua_State *L);
- -

-Returns the current hook mask. - - - - - -


lua_getinfo

-[-(0|1), +(0|1|2), m] -

int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
- -

-Gets information about a specific function or function invocation. - - -

-To get information about a function invocation, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). - - -

-To get information about a function, you push it onto the stack -and start the what string with the character '>'. -(In that case, -lua_getinfo pops the function from the top of the stack.) -For instance, to know in which line a function f was defined, -you can write the following code: - -

-     lua_Debug ar;
-     lua_getglobal(L, "f");  /* get global 'f' */
-     lua_getinfo(L, ">S", &ar);
-     printf("%d\n", ar.linedefined);
-
- -

-Each character in the string what -selects some fields of the structure ar to be filled or -a value to be pushed on the stack. -(These characters are also documented in the declaration of -the structure lua_Debug, -between parentheses in the comments following each field.) - -

    - -
  • 'f': -pushes onto the stack the function that is -running at the given level; -
  • - -
  • 'l': fills in the field currentline; -
  • - -
  • 'n': fills in the fields name and namewhat; -
  • - -
  • 'r': fills in the fields ftransfer and ntransfer; -
  • - -
  • 'S': -fills in the fields source, short_src, -linedefined, lastlinedefined, and what; -
  • - -
  • 't': fills in the field istailcall; -
  • - -
  • 'u': fills in the fields -nups, nparams, and isvararg; -
  • - -
  • 'L': -pushes onto the stack a table whose indices are -the lines on the function with some associated code, -that is, the lines where you can put a break point. -(Lines with no code include empty lines and comments.) -If this option is given together with option 'f', -its table is pushed after the function. -This is the only option that can raise a memory error. -
  • - -
- -

-This function returns 0 to signal an invalid option in what; -even then the valid options are handled correctly. - - - - - -


lua_getlocal

-[-0, +(0|1), –] -

const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
- -

-Gets information about a local variable or a temporary value -of a given activation record or a given function. - - -

-In the first case, -the parameter ar must be a valid activation record that was -filled by a previous call to lua_getstack or -given as argument to a hook (see lua_Hook). -The index n selects which local variable to inspect; -see debug.getlocal for details about variable indices -and names. - - -

-lua_getlocal pushes the variable's value onto the stack -and returns its name. - - -

-In the second case, ar must be NULL and the function -to be inspected must be on the top of the stack. -In this case, only parameters of Lua functions are visible -(as there is no information about what variables are active) -and no values are pushed onto the stack. - - -

-Returns NULL (and pushes nothing) -when the index is greater than -the number of active local variables. - - - - - -


lua_getstack

-[-0, +0, –] -

int lua_getstack (lua_State *L, int level, lua_Debug *ar);
- -

-Gets information about the interpreter runtime stack. - - -

-This function fills parts of a lua_Debug structure with -an identification of the activation record -of the function executing at a given level. -Level 0 is the current running function, -whereas level n+1 is the function that has called level n -(except for tail calls, which do not count in the stack). -When called with a level greater than the stack depth, -lua_getstack returns 0; -otherwise it returns 1. - - - - - -


lua_getupvalue

-[-0, +(0|1), –] -

const char *lua_getupvalue (lua_State *L, int funcindex, int n);
- -

-Gets information about the n-th upvalue -of the closure at index funcindex. -It pushes the upvalue's value onto the stack -and returns its name. -Returns NULL (and pushes nothing) -when the index n is greater than the number of upvalues. - - -

-See debug.getupvalue for more information about upvalues. - - - - - -


lua_Hook

-
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
- -

-Type for debugging hook functions. - - -

-Whenever a hook is called, its ar argument has its field -event set to the specific event that triggered the hook. -Lua identifies these events with the following constants: -LUA_HOOKCALL, LUA_HOOKRET, -LUA_HOOKTAILCALL, LUA_HOOKLINE, -and LUA_HOOKCOUNT. -Moreover, for line events, the field currentline is also set. -To get the value of any other field in ar, -the hook must call lua_getinfo. - - -

-For call events, event can be LUA_HOOKCALL, -the normal value, or LUA_HOOKTAILCALL, for a tail call; -in this case, there will be no corresponding return event. - - -

-While Lua is running a hook, it disables other calls to hooks. -Therefore, if a hook calls back Lua to execute a function or a chunk, -this execution occurs without any calls to hooks. - - -

-Hook functions cannot have continuations, -that is, they cannot call lua_yieldk, -lua_pcallk, or lua_callk with a non-null k. - - -

-Hook functions can yield under the following conditions: -Only count and line events can yield; -to yield, a hook function must finish its execution -calling lua_yield with nresults equal to zero -(that is, with no values). - - - - - -


lua_sethook

-[-0, +0, –] -

void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
- -

-Sets the debugging hook function. - - -

-Argument f is the hook function. -mask specifies on which events the hook will be called: -it is formed by a bitwise OR of the constants -LUA_MASKCALL, -LUA_MASKRET, -LUA_MASKLINE, -and LUA_MASKCOUNT. -The count argument is only meaningful when the mask -includes LUA_MASKCOUNT. -For each event, the hook is called as explained below: - -

    - -
  • The call hook: is called when the interpreter calls a function. -The hook is called just after Lua enters the new function. -
  • - -
  • The return hook: is called when the interpreter returns from a function. -The hook is called just before Lua leaves the function. -
  • - -
  • The line hook: is called when the interpreter is about to -start the execution of a new line of code, -or when it jumps back in the code (even to the same line). -This event only happens while Lua is executing a Lua function. -
  • - -
  • The count hook: is called after the interpreter executes every -count instructions. -This event only happens while Lua is executing a Lua function. -
  • - -
- -

-Hooks are disabled by setting mask to zero. - - - - - -


lua_setlocal

-[-(0|1), +0, –] -

const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
- -

-Sets the value of a local variable of a given activation record. -It assigns the value on the top of the stack -to the variable and returns its name. -It also pops the value from the stack. - - -

-Returns NULL (and pops nothing) -when the index is greater than -the number of active local variables. - - -

-Parameters ar and n are as in the function lua_getlocal. - - - - - -


lua_setupvalue

-[-(0|1), +0, –] -

const char *lua_setupvalue (lua_State *L, int funcindex, int n);
- -

-Sets the value of a closure's upvalue. -It assigns the value on the top of the stack -to the upvalue and returns its name. -It also pops the value from the stack. - - -

-Returns NULL (and pops nothing) -when the index n is greater than the number of upvalues. - - -

-Parameters funcindex and n are as in -the function lua_getupvalue. - - - - - -


lua_upvalueid

-[-0, +0, –] -

void *lua_upvalueid (lua_State *L, int funcindex, int n);
- -

-Returns a unique identifier for the upvalue numbered n -from the closure at index funcindex. - - -

-These unique identifiers allow a program to check whether different -closures share upvalues. -Lua closures that share an upvalue -(that is, that access a same external local variable) -will return identical ids for those upvalue indices. - - -

-Parameters funcindex and n are as in -the function lua_getupvalue, -but n cannot be greater than the number of upvalues. - - - - - -


lua_upvaluejoin

-[-0, +0, –] -

void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
-                                    int funcindex2, int n2);
- -

-Make the n1-th upvalue of the Lua closure at index funcindex1 -refer to the n2-th upvalue of the Lua closure at index funcindex2. - - - - - - - -

5 – The Auxiliary Library

- - - -

- -The auxiliary library provides several convenient functions -to interface C with Lua. -While the basic API provides the primitive functions for all -interactions between C and Lua, -the auxiliary library provides higher-level functions for some -common tasks. - - -

-All functions and types from the auxiliary library -are defined in header file lauxlib.h and -have a prefix luaL_. - - -

-All functions in the auxiliary library are built on -top of the basic API, -and so they provide nothing that cannot be done with that API. -Nevertheless, the use of the auxiliary library ensures -more consistency to your code. - - -

-Several functions in the auxiliary library use internally some -extra stack slots. -When a function in the auxiliary library uses less than five slots, -it does not check the stack size; -it simply assumes that there are enough slots. - - -

-Several functions in the auxiliary library are used to -check C function arguments. -Because the error message is formatted for arguments -(e.g., "bad argument #1"), -you should not use these functions for other stack values. - - -

-Functions called luaL_check* -always raise an error if the check is not satisfied. - - - - - -

5.1 – Functions and Types

- -

-Here we list all functions and types from the auxiliary library -in alphabetical order. - - - -


luaL_addchar

-[-?, +?, m] -

void luaL_addchar (luaL_Buffer *B, char c);
- -

-Adds the byte c to the buffer B -(see luaL_Buffer). - - - - - -


luaL_addgsub

-[-?, +?, m] -

const void luaL_addgsub (luaL_Buffer *B, const char *s,
-                         const char *p, const char *r);
- -

-Adds a copy of the string s to the buffer B (see luaL_Buffer), -replacing any occurrence of the string p -with the string r. - - - - - -


luaL_addlstring

-[-?, +?, m] -

void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
- -

-Adds the string pointed to by s with length l to -the buffer B -(see luaL_Buffer). -The string can contain embedded zeros. - - - - - -


luaL_addsize

-[-?, +?, –] -

void luaL_addsize (luaL_Buffer *B, size_t n);
- -

-Adds to the buffer B -a string of length n previously copied to the -buffer area (see luaL_prepbuffer). - - - - - -


luaL_addstring

-[-?, +?, m] -

void luaL_addstring (luaL_Buffer *B, const char *s);
- -

-Adds the zero-terminated string pointed to by s -to the buffer B -(see luaL_Buffer). - - - - - -


luaL_addvalue

-[-?, +?, m] -

void luaL_addvalue (luaL_Buffer *B);
- -

-Adds the value on the top of the stack -to the buffer B -(see luaL_Buffer). -Pops the value. - - -

-This is the only function on string buffers that can (and must) -be called with an extra element on the stack, -which is the value to be added to the buffer. - - - - - -


luaL_argcheck

-[-0, +0, v] -

void luaL_argcheck (lua_State *L,
-                    int cond,
-                    int arg,
-                    const char *extramsg);
- -

-Checks whether cond is true. -If it is not, raises an error with a standard message (see luaL_argerror). - - - - - -


luaL_argerror

-[-0, +0, v] -

int luaL_argerror (lua_State *L, int arg, const char *extramsg);
- -

-Raises an error reporting a problem with argument arg -of the C function that called it, -using a standard message -that includes extramsg as a comment: - -

-     bad argument #arg to 'funcname' (extramsg)
-

-This function never returns. - - - - - -


luaL_argexpected

-[-0, +0, v] -

void luaL_argexpected (lua_State *L,
-                       int cond,
-                       int arg,
-                       const char *tname);
- -

-Checks whether cond is true. -If it is not, raises an error about the type of the argument arg -with a standard message (see luaL_typeerror). - - - - - -


luaL_Buffer

-
typedef struct luaL_Buffer luaL_Buffer;
- -

-Type for a string buffer. - - -

-A string buffer allows C code to build Lua strings piecemeal. -Its pattern of use is as follows: - -

    - -
  • First declare a variable b of type luaL_Buffer.
  • - -
  • Then initialize it with a call luaL_buffinit(L, &b).
  • - -
  • -Then add string pieces to the buffer calling any of -the luaL_add* functions. -
  • - -
  • -Finish by calling luaL_pushresult(&b). -This call leaves the final string on the top of the stack. -
  • - -
- -

-If you know beforehand the maximum size of the resulting string, -you can use the buffer like this: - -

    - -
  • First declare a variable b of type luaL_Buffer.
  • - -
  • Then initialize it and preallocate a space of -size sz with a call luaL_buffinitsize(L, &b, sz).
  • - -
  • Then produce the string into that space.
  • - -
  • -Finish by calling luaL_pushresultsize(&b, sz), -where sz is the total size of the resulting string -copied into that space (which may be less than or -equal to the preallocated size). -
  • - -
- -

-During its normal operation, -a string buffer uses a variable number of stack slots. -So, while using a buffer, you cannot assume that you know where -the top of the stack is. -You can use the stack between successive calls to buffer operations -as long as that use is balanced; -that is, -when you call a buffer operation, -the stack is at the same level -it was immediately after the previous buffer operation. -(The only exception to this rule is luaL_addvalue.) -After calling luaL_pushresult, -the stack is back to its level when the buffer was initialized, -plus the final string on its top. - - - - - -


luaL_buffaddr

-[-0, +0, –] -

char *luaL_buffaddr (luaL_Buffer *B);
- -

-Returns the address of the current content of buffer B -(see luaL_Buffer). -Note that any addition to the buffer may invalidate this address. - - - - - -


luaL_buffinit

-[-0, +?, –] -

void luaL_buffinit (lua_State *L, luaL_Buffer *B);
- -

-Initializes a buffer B -(see luaL_Buffer). -This function does not allocate any space; -the buffer must be declared as a variable. - - - - - -


luaL_bufflen

-[-0, +0, –] -

size_t luaL_bufflen (luaL_Buffer *B);
- -

-Returns the length of the current content of buffer B -(see luaL_Buffer). - - - - - -


luaL_buffinitsize

-[-?, +?, m] -

char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);
- -

-Equivalent to the sequence -luaL_buffinit, luaL_prepbuffsize. - - - - - -


luaL_buffsub

-[-?, +?, –] -

void luaL_buffsub (luaL_Buffer *B, int n);
- -

-Removes n bytes from the the buffer B -(see luaL_Buffer). -The buffer must have at least that many bytes. - - - - - -


luaL_callmeta

-[-0, +(0|1), e] -

int luaL_callmeta (lua_State *L, int obj, const char *e);
- -

-Calls a metamethod. - - -

-If the object at index obj has a metatable and this -metatable has a field e, -this function calls this field passing the object as its only argument. -In this case this function returns true and pushes onto the -stack the value returned by the call. -If there is no metatable or no metamethod, -this function returns false without pushing any value on the stack. - - - - - -


luaL_checkany

-[-0, +0, v] -

void luaL_checkany (lua_State *L, int arg);
- -

-Checks whether the function has an argument -of any type (including nil) at position arg. - - - - - -


luaL_checkinteger

-[-0, +0, v] -

lua_Integer luaL_checkinteger (lua_State *L, int arg);
- -

-Checks whether the function argument arg is an integer -(or can be converted to an integer) -and returns this integer. - - - - - -


luaL_checklstring

-[-0, +0, v] -

const char *luaL_checklstring (lua_State *L, int arg, size_t *l);
- -

-Checks whether the function argument arg is a string -and returns this string; -if l is not NULL fills its referent -with the string's length. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checknumber

-[-0, +0, v] -

lua_Number luaL_checknumber (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a number -and returns this number converted to a lua_Number. - - - - - -


luaL_checkoption

-[-0, +0, v] -

int luaL_checkoption (lua_State *L,
-                      int arg,
-                      const char *def,
-                      const char *const lst[]);
- -

-Checks whether the function argument arg is a string and -searches for this string in the array lst -(which must be NULL-terminated). -Returns the index in the array where the string was found. -Raises an error if the argument is not a string or -if the string cannot be found. - - -

-If def is not NULL, -the function uses def as a default value when -there is no argument arg or when this argument is nil. - - -

-This is a useful function for mapping strings to C enums. -(The usual convention in Lua libraries is -to use strings instead of numbers to select options.) - - - - - -


luaL_checkstack

-[-0, +0, v] -

void luaL_checkstack (lua_State *L, int sz, const char *msg);
- -

-Grows the stack size to top + sz elements, -raising an error if the stack cannot grow to that size. -msg is an additional text to go into the error message -(or NULL for no additional text). - - - - - -


luaL_checkstring

-[-0, +0, v] -

const char *luaL_checkstring (lua_State *L, int arg);
- -

-Checks whether the function argument arg is a string -and returns this string. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_checktype

-[-0, +0, v] -

void luaL_checktype (lua_State *L, int arg, int t);
- -

-Checks whether the function argument arg has type t. -See lua_type for the encoding of types for t. - - - - - -


luaL_checkudata

-[-0, +0, v] -

void *luaL_checkudata (lua_State *L, int arg, const char *tname);
- -

-Checks whether the function argument arg is a userdata -of the type tname (see luaL_newmetatable) and -returns the userdata's memory-block address (see lua_touserdata). - - - - - -


luaL_checkversion

-[-0, +0, v] -

void luaL_checkversion (lua_State *L);
- -

-Checks whether the code making the call and the Lua library being called -are using the same version of Lua and the same numeric types. - - - - - -


luaL_dofile

-[-0, +?, m] -

int luaL_dofile (lua_State *L, const char *filename);
- -

-Loads and runs the given file. -It is defined as the following macro: - -

-     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns LUA_OK if there are no errors, -or an error code in case of errors (see §4.4.1). - - - - - -


luaL_dostring

-[-0, +?, –] -

int luaL_dostring (lua_State *L, const char *str);
- -

-Loads and runs the given string. -It is defined as the following macro: - -

-     (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
-

-It returns LUA_OK if there are no errors, -or an error code in case of errors (see §4.4.1). - - - - - -


luaL_error

-[-0, +0, v] -

int luaL_error (lua_State *L, const char *fmt, ...);
- -

-Raises an error. -The error message format is given by fmt -plus any extra arguments, -following the same rules of lua_pushfstring. -It also adds at the beginning of the message the file name and -the line number where the error occurred, -if this information is available. - - -

-This function never returns, -but it is an idiom to use it in C functions -as return luaL_error(args). - - - - - -


luaL_execresult

-[-0, +3, m] -

int luaL_execresult (lua_State *L, int stat);
- -

-This function produces the return values for -process-related functions in the standard library -(os.execute and io.close). - - - - - -


luaL_fileresult

-[-0, +(1|3), m] -

int luaL_fileresult (lua_State *L, int stat, const char *fname);
- -

-This function produces the return values for -file-related functions in the standard library -(io.open, os.rename, file:seek, etc.). - - - - - -


luaL_getmetafield

-[-0, +(0|1), m] -

int luaL_getmetafield (lua_State *L, int obj, const char *e);
- -

-Pushes onto the stack the field e from the metatable -of the object at index obj and returns the type of the pushed value. -If the object does not have a metatable, -or if the metatable does not have this field, -pushes nothing and returns LUA_TNIL. - - - - - -


luaL_getmetatable

-[-0, +1, m] -

int luaL_getmetatable (lua_State *L, const char *tname);
- -

-Pushes onto the stack the metatable associated with the name tname -in the registry (see luaL_newmetatable), -or nil if there is no metatable associated with that name. -Returns the type of the pushed value. - - - - - -


luaL_getsubtable

-[-0, +1, e] -

int luaL_getsubtable (lua_State *L, int idx, const char *fname);
- -

-Ensures that the value t[fname], -where t is the value at index idx, -is a table, -and pushes that table onto the stack. -Returns true if it finds a previous table there -and false if it creates a new table. - - - - - -


luaL_gsub

-[-0, +1, m] -

const char *luaL_gsub (lua_State *L,
-                       const char *s,
-                       const char *p,
-                       const char *r);
- -

-Creates a copy of string s, -replacing any occurrence of the string p -with the string r. -Pushes the resulting string on the stack and returns it. - - - - - -


luaL_len

-[-0, +0, e] -

lua_Integer luaL_len (lua_State *L, int index);
- -

-Returns the "length" of the value at the given index -as a number; -it is equivalent to the '#' operator in Lua (see §3.4.7). -Raises an error if the result of the operation is not an integer. -(This case can only happen through metamethods.) - - - - - -


luaL_loadbuffer

-[-0, +1, –] -

int luaL_loadbuffer (lua_State *L,
-                     const char *buff,
-                     size_t sz,
-                     const char *name);
- -

-Equivalent to luaL_loadbufferx with mode equal to NULL. - - - - - -


luaL_loadbufferx

-[-0, +1, –] -

int luaL_loadbufferx (lua_State *L,
-                      const char *buff,
-                      size_t sz,
-                      const char *name,
-                      const char *mode);
- -

-Loads a buffer as a Lua chunk. -This function uses lua_load to load the chunk in the -buffer pointed to by buff with size sz. - - -

-This function returns the same results as lua_load. -name is the chunk name, -used for debug information and error messages. -The string mode works as in the function lua_load. - - - - - -


luaL_loadfile

-[-0, +1, m] -

int luaL_loadfile (lua_State *L, const char *filename);
- -

-Equivalent to luaL_loadfilex with mode equal to NULL. - - - - - -


luaL_loadfilex

-[-0, +1, m] -

int luaL_loadfilex (lua_State *L, const char *filename,
-                                            const char *mode);
- -

-Loads a file as a Lua chunk. -This function uses lua_load to load the chunk in the file -named filename. -If filename is NULL, -then it loads from the standard input. -The first line in the file is ignored if it starts with a #. - - -

-The string mode works as in the function lua_load. - - -

-This function returns the same results as lua_load -or LUA_ERRFILE for file-related errors. - - -

-As lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_loadstring

-[-0, +1, –] -

int luaL_loadstring (lua_State *L, const char *s);
- -

-Loads a string as a Lua chunk. -This function uses lua_load to load the chunk in -the zero-terminated string s. - - -

-This function returns the same results as lua_load. - - -

-Also as lua_load, this function only loads the chunk; -it does not run it. - - - - - -


luaL_newlib

-[-0, +1, m] -

void luaL_newlib (lua_State *L, const luaL_Reg l[]);
- -

-Creates a new table and registers there -the functions in the list l. - - -

-It is implemented as the following macro: - -

-     (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
-

-The array l must be the actual array, -not a pointer to it. - - - - - -


luaL_newlibtable

-[-0, +1, m] -

void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);
- -

-Creates a new table with a size optimized -to store all entries in the array l -(but does not actually store them). -It is intended to be used in conjunction with luaL_setfuncs -(see luaL_newlib). - - -

-It is implemented as a macro. -The array l must be the actual array, -not a pointer to it. - - - - - -


luaL_newmetatable

-[-0, +1, m] -

int luaL_newmetatable (lua_State *L, const char *tname);
- -

-If the registry already has the key tname, -returns 0. -Otherwise, -creates a new table to be used as a metatable for userdata, -adds to this new table the pair __name = tname, -adds to the registry the pair [tname] = new table, -and returns 1. - - -

-In both cases, -the function pushes onto the stack the final value associated -with tname in the registry. - - - - - -


luaL_newstate

-[-0, +0, –] -

lua_State *luaL_newstate (void);
- -

-Creates a new Lua state. -It calls lua_newstate with an -allocator based on the standard C allocation functions -and then sets a warning function and a panic function (see §4.4) -that print messages to the standard error output. - - -

-Returns the new state, -or NULL if there is a memory allocation error. - - - - - -


luaL_openlibs

-[-0, +0, e] -

void luaL_openlibs (lua_State *L);
- -

-Opens all standard Lua libraries into the given state. - - - - - -


luaL_opt

-[-0, +0, –] -

T luaL_opt (L, func, arg, dflt);
- -

-This macro is defined as follows: - -

-     (lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg)))
-

-In words, if the argument arg is nil or absent, -the macro results in the default dflt. -Otherwise, it results in the result of calling func -with the state L and the argument index arg as -arguments. -Note that it evaluates the expression dflt only if needed. - - - - - -


luaL_optinteger

-[-0, +0, v] -

lua_Integer luaL_optinteger (lua_State *L,
-                             int arg,
-                             lua_Integer d);
- -

-If the function argument arg is an integer -(or it is convertible to an integer), -returns this integer. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optlstring

-[-0, +0, v] -

const char *luaL_optlstring (lua_State *L,
-                             int arg,
-                             const char *d,
-                             size_t *l);
- -

-If the function argument arg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - -

-If l is not NULL, -fills its referent with the result's length. -If the result is NULL -(only possible when returning d and d == NULL), -its length is considered zero. - - -

-This function uses lua_tolstring to get its result, -so all conversions and caveats of that function apply here. - - - - - -


luaL_optnumber

-[-0, +0, v] -

lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);
- -

-If the function argument arg is a number, -returns this number as a lua_Number. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_optstring

-[-0, +0, v] -

const char *luaL_optstring (lua_State *L,
-                            int arg,
-                            const char *d);
- -

-If the function argument arg is a string, -returns this string. -If this argument is absent or is nil, -returns d. -Otherwise, raises an error. - - - - - -


luaL_prepbuffer

-[-?, +?, m] -

char *luaL_prepbuffer (luaL_Buffer *B);
- -

-Equivalent to luaL_prepbuffsize -with the predefined size LUAL_BUFFERSIZE. - - - - - -


luaL_prepbuffsize

-[-?, +?, m] -

char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);
- -

-Returns an address to a space of size sz -where you can copy a string to be added to buffer B -(see luaL_Buffer). -After copying the string into this space you must call -luaL_addsize with the size of the string to actually add -it to the buffer. - - - - - -


luaL_pushfail

-[-0, +1, –] -

void luaL_pushfail (lua_State *L);
- -

-Pushes the fail value onto the stack (see §6). - - - - - -


luaL_pushresult

-[-?, +1, m] -

void luaL_pushresult (luaL_Buffer *B);
- -

-Finishes the use of buffer B leaving the final string on -the top of the stack. - - - - - -


luaL_pushresultsize

-[-?, +1, m] -

void luaL_pushresultsize (luaL_Buffer *B, size_t sz);
- -

-Equivalent to the sequence luaL_addsize, luaL_pushresult. - - - - - -


luaL_ref

-[-1, +0, m] -

int luaL_ref (lua_State *L, int t);
- -

-Creates and returns a reference, -in the table at index t, -for the object on the top of the stack (and pops the object). - - -

-A reference is a unique integer key. -As long as you do not manually add integer keys into the table t, -luaL_ref ensures the uniqueness of the key it returns. -You can retrieve an object referred by the reference r -by calling lua_rawgeti(L, t, r). -The function luaL_unref frees a reference. - - -

-If the object on the top of the stack is nil, -luaL_ref returns the constant LUA_REFNIL. -The constant LUA_NOREF is guaranteed to be different -from any reference returned by luaL_ref. - - - - - -


luaL_Reg

-
typedef struct luaL_Reg {
-  const char *name;
-  lua_CFunction func;
-} luaL_Reg;
- -

-Type for arrays of functions to be registered by -luaL_setfuncs. -name is the function name and func is a pointer to -the function. -Any array of luaL_Reg must end with a sentinel entry -in which both name and func are NULL. - - - - - -


luaL_requiref

-[-0, +1, e] -

void luaL_requiref (lua_State *L, const char *modname,
-                    lua_CFunction openf, int glb);
- -

-If package.loaded[modname] is not true, -calls the function openf with the string modname as an argument -and sets the call result to package.loaded[modname], -as if that function has been called through require. - - -

-If glb is true, -also stores the module into the global modname. - - -

-Leaves a copy of the module on the stack. - - - - - -


luaL_setfuncs

-[-nup, +0, m] -

void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
- -

-Registers all functions in the array l -(see luaL_Reg) into the table on the top of the stack -(below optional upvalues, see next). - - -

-When nup is not zero, -all functions are created with nup upvalues, -initialized with copies of the nup values -previously pushed on the stack -on top of the library table. -These values are popped from the stack after the registration. - - -

-A function with a NULL value represents a placeholder, -which is filled with false. - - - - - -


luaL_setmetatable

-[-0, +0, –] -

void luaL_setmetatable (lua_State *L, const char *tname);
- -

-Sets the metatable of the object on the top of the stack -as the metatable associated with name tname -in the registry (see luaL_newmetatable). - - - - - -


luaL_Stream

-
typedef struct luaL_Stream {
-  FILE *f;
-  lua_CFunction closef;
-} luaL_Stream;
- -

-The standard representation for file handles -used by the standard I/O library. - - -

-A file handle is implemented as a full userdata, -with a metatable called LUA_FILEHANDLE -(where LUA_FILEHANDLE is a macro with the actual metatable's name). -The metatable is created by the I/O library -(see luaL_newmetatable). - - -

-This userdata must start with the structure luaL_Stream; -it can contain other data after this initial structure. -The field f points to the corresponding C stream -(or it can be NULL to indicate an incompletely created handle). -The field closef points to a Lua function -that will be called to close the stream -when the handle is closed or collected; -this function receives the file handle as its sole argument and -must return either a true value, in case of success, -or a false value plus an error message, in case of error. -Once Lua calls this field, -it changes the field value to NULL -to signal that the handle is closed. - - - - - -


luaL_testudata

-[-0, +0, m] -

void *luaL_testudata (lua_State *L, int arg, const char *tname);
- -

-This function works like luaL_checkudata, -except that, when the test fails, -it returns NULL instead of raising an error. - - - - - -


luaL_tolstring

-[-0, +1, e] -

const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
- -

-Converts any Lua value at the given index to a C string -in a reasonable format. -The resulting string is pushed onto the stack and also -returned by the function (see §4.1.3). -If len is not NULL, -the function also sets *len with the string length. - - -

-If the value has a metatable with a __tostring field, -then luaL_tolstring calls the corresponding metamethod -with the value as argument, -and uses the result of the call as its result. - - - - - -


luaL_traceback

-[-0, +1, m] -

void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
-                     int level);
- -

-Creates and pushes a traceback of the stack L1. -If msg is not NULL, it is appended -at the beginning of the traceback. -The level parameter tells at which level -to start the traceback. - - - - - -


luaL_typeerror

-[-0, +0, v] -

const char *luaL_typeerror (lua_State *L,
-                                      int arg,
-                                      const char *tname);
- -

-Raises a type error for the argument arg -of the C function that called it, -using a standard message; -tname is a "name" for the expected type. -This function never returns. - - - - - -


luaL_typename

-[-0, +0, –] -

const char *luaL_typename (lua_State *L, int index);
- -

-Returns the name of the type of the value at the given index. - - - - - -


luaL_unref

-[-0, +0, –] -

void luaL_unref (lua_State *L, int t, int ref);
- -

-Releases the reference ref from the table at index t -(see luaL_ref). -The entry is removed from the table, -so that the referred object can be collected. -The reference ref is also freed to be used again. - - -

-If ref is LUA_NOREF or LUA_REFNIL, -luaL_unref does nothing. - - - - - -


luaL_where

-[-0, +1, m] -

void luaL_where (lua_State *L, int lvl);
- -

-Pushes onto the stack a string identifying the current position -of the control at level lvl in the call stack. -Typically this string has the following format: - -

-     chunkname:currentline:
-

-Level 0 is the running function, -level 1 is the function that called the running function, -etc. - - -

-This function is used to build a prefix for error messages. - - - - - - - -

6 – The Standard Libraries

- - - -

-The standard Lua libraries provide useful functions -that are implemented in C through the C API. -Some of these functions provide essential services to the language -(e.g., type and getmetatable); -others provide access to outside services (e.g., I/O); -and others could be implemented in Lua itself, -but that for different reasons -deserve an implementation in C (e.g., table.sort). - - -

-All libraries are implemented through the official C API -and are provided as separate C modules. -Unless otherwise noted, -these library functions do not adjust its number of arguments -to its expected parameters. -For instance, a function documented as foo(arg) -should not be called without an argument. - - -

-The notation fail means a false value representing -some kind of failure. -(Currently, fail is equal to nil, -but that may change in future versions. -The recommendation is to always test the success of these functions -with (not status), instead of (status == nil).) - - -

-Currently, Lua has the following standard libraries: - -

    - -
  • basic library (§6.1);
  • - -
  • coroutine library (§6.2);
  • - -
  • package library (§6.3);
  • - -
  • string manipulation (§6.4);
  • - -
  • basic UTF-8 support (§6.5);
  • - -
  • table manipulation (§6.6);
  • - -
  • mathematical functions (§6.7) (sin, log, etc.);
  • - -
  • input and output (§6.8);
  • - -
  • operating system facilities (§6.9);
  • - -
  • debug facilities (§6.10).
  • - -

-Except for the basic and the package libraries, -each library provides all its functions as fields of a global table -or as methods of its objects. - - -

-To have access to these libraries, -the C host program should call the luaL_openlibs function, -which opens all standard libraries. -Alternatively, -the host program can open them individually by using -luaL_requiref to call -luaopen_base (for the basic library), -luaopen_package (for the package library), -luaopen_coroutine (for the coroutine library), -luaopen_string (for the string library), -luaopen_utf8 (for the UTF-8 library), -luaopen_table (for the table library), -luaopen_math (for the mathematical library), -luaopen_io (for the I/O library), -luaopen_os (for the operating system library), -and luaopen_debug (for the debug library). -These functions are declared in lualib.h. - - - - - -

6.1 – Basic Functions

- -

-The basic library provides core functions to Lua. -If you do not include this library in your application, -you should check carefully whether you need to provide -implementations for some of its facilities. - - -

-


assert (v [, message])

- - -

-Raises an error if -the value of its argument v is false (i.e., nil or false); -otherwise, returns all its arguments. -In case of error, -message is the error object; -when absent, it defaults to "assertion failed!" - - - - -

-


collectgarbage ([opt [, arg]])

- - -

-This function is a generic interface to the garbage collector. -It performs different functions according to its first argument, opt: - -

    - -
  • "collect": -Performs a full garbage-collection cycle. -This is the default option. -
  • - -
  • "stop": -Stops automatic execution of the garbage collector. -The collector will run only when explicitly invoked, -until a call to restart it. -
  • - -
  • "restart": -Restarts automatic execution of the garbage collector. -
  • - -
  • "count": -Returns the total memory in use by Lua in Kbytes. -The value has a fractional part, -so that it multiplied by 1024 -gives the exact number of bytes in use by Lua. -
  • - -
  • "step": -Performs a garbage-collection step. -The step "size" is controlled by arg. -With a zero value, -the collector will perform one basic (indivisible) step. -For non-zero values, -the collector will perform as if that amount of memory -(in Kbytes) had been allocated by Lua. -Returns true if the step finished a collection cycle. -
  • - -
  • "isrunning": -Returns a boolean that tells whether the collector is running -(i.e., not stopped). -
  • - -
  • "incremental": -Change the collector mode to incremental. -This option can be followed by three numbers: -the garbage-collector pause, -the step multiplier, -and the step size (see §2.5.1). -A zero means to not change that value. -
  • - -
  • "generational": -Change the collector mode to generational. -This option can be followed by two numbers: -the garbage-collector minor multiplier -and the major multiplier (see §2.5.2). -A zero means to not change that value. -
  • - -

-See §2.5 for more details about garbage collection -and some of these options. - - -

-This function should not be called by a finalizer. - - - - -

-


dofile ([filename])

-Opens the named file and executes its content as a Lua chunk. -When called without arguments, -dofile executes the content of the standard input (stdin). -Returns all values returned by the chunk. -In case of errors, dofile propagates the error -to its caller. -(That is, dofile does not run in protected mode.) - - - - -

-


error (message [, level])

-Raises an error (see §2.3) with message as the error object. -This function never returns. - - -

-Usually, error adds some information about the error position -at the beginning of the message, if the message is a string. -The level argument specifies how to get the error position. -With level 1 (the default), the error position is where the -error function was called. -Level 2 points the error to where the function -that called error was called; and so on. -Passing a level 0 avoids the addition of error position information -to the message. - - - - -

-


_G

-A global variable (not a function) that -holds the global environment (see §2.2). -Lua itself does not use this variable; -changing its value does not affect any environment, -nor vice versa. - - - - -

-


getmetatable (object)

- - -

-If object does not have a metatable, returns nil. -Otherwise, -if the object's metatable has a __metatable field, -returns the associated value. -Otherwise, returns the metatable of the given object. - - - - -

-


ipairs (t)

- - -

-Returns three values (an iterator function, the table t, and 0) -so that the construction - -

-     for i,v in ipairs(t) do body end
-

-will iterate over the key–value pairs -(1,t[1]), (2,t[2]), ..., -up to the first absent index. - - - - -

-


load (chunk [, chunkname [, mode [, env]]])

- - -

-Loads a chunk. - - -

-If chunk is a string, the chunk is this string. -If chunk is a function, -load calls it repeatedly to get the chunk pieces. -Each call to chunk must return a string that concatenates -with previous results. -A return of an empty string, nil, or no value signals the end of the chunk. - - -

-If there are no syntactic errors, -load returns the compiled chunk as a function; -otherwise, it returns fail plus the error message. - - -

-When you load a main chunk, -the resulting function will always have exactly one upvalue, -the _ENV variable (see §2.2). -However, -when you load a binary chunk created from a function (see string.dump), -the resulting function can have an arbitrary number of upvalues, -and there is no guarantee that its first upvalue will be -the _ENV variable. -(A non-main function may not even have an _ENV upvalue.) - - -

-Regardless, if the resulting function has any upvalues, -its first upvalue is set to the value of env, -if that parameter is given, -or to the value of the global environment. -Other upvalues are initialized with nil. -All upvalues are fresh, that is, -they are not shared with any other function. - - -

-chunkname is used as the name of the chunk for error messages -and debug information (see §4.7). -When absent, -it defaults to chunk, if chunk is a string, -or to "=(load)" otherwise. - - -

-The string mode controls whether the chunk can be text or binary -(that is, a precompiled chunk). -It may be the string "b" (only binary chunks), -"t" (only text chunks), -or "bt" (both binary and text). -The default is "bt". - - -

-It is safe to load malformed binary chunks; -load signals an appropriate error. -However, -Lua does not check the consistency of the code inside binary chunks; -running maliciously crafted bytecode can crash the interpreter. - - - - -

-


loadfile ([filename [, mode [, env]]])

- - -

-Similar to load, -but gets the chunk from file filename -or from the standard input, -if no file name is given. - - - - -

-


next (table [, index])

- - -

-Allows a program to traverse all fields of a table. -Its first argument is a table and its second argument -is an index in this table. -A call to next returns the next index of the table -and its associated value. -When called with nil as its second argument, -next returns an initial index -and its associated value. -When called with the last index, -or with nil in an empty table, -next returns nil. -If the second argument is absent, then it is interpreted as nil. -In particular, -you can use next(t) to check whether a table is empty. - - -

-The order in which the indices are enumerated is not specified, -even for numeric indices. -(To traverse a table in numerical order, -use a numerical for.) - - -

-You should not assign any value to a non-existent field in a table -during its traversal. -You may however modify existing fields. -In particular, you may set existing fields to nil. - - - - -

-


pairs (t)

- - -

-If t has a metamethod __pairs, -calls it with t as argument and returns the first three -results from the call. - - -

-Otherwise, -returns three values: the next function, the table t, and nil, -so that the construction - -

-     for k,v in pairs(t) do body end
-

-will iterate over all key–value pairs of table t. - - -

-See function next for the caveats of modifying -the table during its traversal. - - - - -

-


pcall (f [, arg1, ···])

- - -

-Calls the function f with -the given arguments in protected mode. -This means that any error inside f is not propagated; -instead, pcall catches the error -and returns a status code. -Its first result is the status code (a boolean), -which is true if the call succeeds without errors. -In such case, pcall also returns all results from the call, -after this first result. -In case of any error, pcall returns false plus the error object. -Note that errors caught by pcall do not call a message handler. - - - - -

-


print (···)

-Receives any number of arguments -and prints their values to stdout, -converting each argument to a string -following the same rules of tostring. - - -

-The function print is not intended for formatted output, -but only as a quick way to show a value, -for instance for debugging. -For complete control over the output, -use string.format and io.write. - - - - -

-


rawequal (v1, v2)

-Checks whether v1 is equal to v2, -without invoking the __eq metamethod. -Returns a boolean. - - - - -

-


rawget (table, index)

-Gets the real value of table[index], -without using the __index metavalue. -table must be a table; -index may be any value. - - - - -

-


rawlen (v)

-Returns the length of the object v, -which must be a table or a string, -without invoking the __len metamethod. -Returns an integer. - - - - -

-


rawset (table, index, value)

-Sets the real value of table[index] to value, -without using the __newindex metavalue. -table must be a table, -index any value different from nil and NaN, -and value any Lua value. - - -

-This function returns table. - - - - -

-


select (index, ···)

- - -

-If index is a number, -returns all arguments after argument number index; -a negative number indexes from the end (-1 is the last argument). -Otherwise, index must be the string "#", -and select returns the total number of extra arguments it received. - - - - -

-


setmetatable (table, metatable)

- - -

-Sets the metatable for the given table. -If metatable is nil, -removes the metatable of the given table. -If the original metatable has a __metatable field, -raises an error. - - -

-This function returns table. - - -

-To change the metatable of other types from Lua code, -you must use the debug library (§6.10). - - - - -

-


tonumber (e [, base])

- - -

-When called with no base, -tonumber tries to convert its argument to a number. -If the argument is already a number or -a string convertible to a number, -then tonumber returns this number; -otherwise, it returns fail. - - -

-The conversion of strings can result in integers or floats, -according to the lexical conventions of Lua (see §3.1). -The string may have leading and trailing spaces and a sign. - - -

-When called with base, -then e must be a string to be interpreted as -an integer numeral in that base. -The base may be any integer between 2 and 36, inclusive. -In bases above 10, the letter 'A' (in either upper or lower case) -represents 10, 'B' represents 11, and so forth, -with 'Z' representing 35. -If the string e is not a valid numeral in the given base, -the function returns fail. - - - - -

-


tostring (v)

- - -

-Receives a value of any type and -converts it to a string in a human-readable format. - - -

-If the metatable of v has a __tostring field, -then tostring calls the corresponding value -with v as argument, -and uses the result of the call as its result. -Otherwise, if the metatable of v has a __name field -with a string value, -tostring may use that string in its final result. - - -

-For complete control of how numbers are converted, -use string.format. - - - - -

-


type (v)

- - -

-Returns the type of its only argument, coded as a string. -The possible results of this function are -"nil" (a string, not the value nil), -"number", -"string", -"boolean", -"table", -"function", -"thread", -and "userdata". - - - - -

-


_VERSION

- - -

-A global variable (not a function) that -holds a string containing the running Lua version. -The current value of this variable is "Lua 5.4". - - - - -

-


warn (msg1, ···)

- - -

-Emits a warning with a message composed by the concatenation -of all its arguments (which should be strings). - - -

-By convention, -a one-piece message starting with '@' -is intended to be a control message, -which is a message to the warning system itself. -In particular, the standard warning function in Lua -recognizes the control messages "@off", -to stop the emission of warnings, -and "@on", to (re)start the emission; -it ignores unknown control messages. - - - - -

-


xpcall (f, msgh [, arg1, ···])

- - -

-This function is similar to pcall, -except that it sets a new message handler msgh. - - - - - - - -

6.2 – Coroutine Manipulation

- -

-This library comprises the operations to manipulate coroutines, -which come inside the table coroutine. -See §2.6 for a general description of coroutines. - - -

-


coroutine.close (co)

- - -

-Closes coroutine co, -that is, -closes all its pending to-be-closed variables -and puts the coroutine in a dead state. -The given coroutine must be dead or suspended. -In case of error -(either the original error that stopped the coroutine or -errors in closing methods), -returns false plus the error object; -otherwise returns true. - - - - -

-


coroutine.create (f)

- - -

-Creates a new coroutine, with body f. -f must be a function. -Returns this new coroutine, -an object with type "thread". - - - - -

-


coroutine.isyieldable ([co])

- - -

-Returns true when the coroutine co can yield. -The default for co is the running coroutine. - - -

-A coroutine is yieldable if it is not the main thread and -it is not inside a non-yieldable C function. - - - - -

-


coroutine.resume (co [, val1, ···])

- - -

-Starts or continues the execution of coroutine co. -The first time you resume a coroutine, -it starts running its body. -The values val1, ... are passed -as the arguments to the body function. -If the coroutine has yielded, -resume restarts it; -the values val1, ... are passed -as the results from the yield. - - -

-If the coroutine runs without any errors, -resume returns true plus any values passed to yield -(when the coroutine yields) or any values returned by the body function -(when the coroutine terminates). -If there is any error, -resume returns false plus the error message. - - - - -

-


coroutine.running ()

- - -

-Returns the running coroutine plus a boolean, -true when the running coroutine is the main one. - - - - -

-


coroutine.status (co)

- - -

-Returns the status of the coroutine co, as a string: -"running", -if the coroutine is running -(that is, it is the one that called status); -"suspended", if the coroutine is suspended in a call to yield, -or if it has not started running yet; -"normal" if the coroutine is active but not running -(that is, it has resumed another coroutine); -and "dead" if the coroutine has finished its body function, -or if it has stopped with an error. - - - - -

-


coroutine.wrap (f)

- - -

-Creates a new coroutine, with body f; -f must be a function. -Returns a function that resumes the coroutine each time it is called. -Any arguments passed to this function behave as the -extra arguments to resume. -The function returns the same values returned by resume, -except the first boolean. -In case of error, -the function closes the coroutine and propagates the error. - - - - -

-


coroutine.yield (···)

- - -

-Suspends the execution of the calling coroutine. -Any arguments to yield are passed as extra results to resume. - - - - - - - -

6.3 – Modules

- -

-The package library provides basic -facilities for loading modules in Lua. -It exports one function directly in the global environment: -require. -Everything else is exported in the table package. - - -

-


require (modname)

- - -

-Loads the given module. -The function starts by looking into the package.loaded table -to determine whether modname is already loaded. -If it is, then require returns the value stored -at package.loaded[modname]. -(The absence of a second result in this case -signals that this call did not have to load the module.) -Otherwise, it tries to find a loader for the module. - - -

-To find a loader, -require is guided by the table package.searchers. -Each item in this table is a search function, -that searches for the module in a particular way. -By changing this table, -we can change how require looks for a module. -The following explanation is based on the default configuration -for package.searchers. - - -

-First require queries package.preload[modname]. -If it has a value, -this value (which must be a function) is the loader. -Otherwise require searches for a Lua loader using the -path stored in package.path. -If that also fails, it searches for a C loader using the -path stored in package.cpath. -If that also fails, -it tries an all-in-one loader (see package.searchers). - - -

-Once a loader is found, -require calls the loader with two arguments: -modname and an extra value, -a loader data, -also returned by the searcher. -The loader data can be any value useful to the module; -for the default searchers, -it indicates where the loader was found. -(For instance, if the loader came from a file, -this extra value is the file path.) -If the loader returns any non-nil value, -require assigns the returned value to package.loaded[modname]. -If the loader does not return a non-nil value and -has not assigned any value to package.loaded[modname], -then require assigns true to this entry. -In any case, require returns the -final value of package.loaded[modname]. -Besides that value, require also returns as a second result -the loader data returned by the searcher, -which indicates how require found the module. - - -

-If there is any error loading or running the module, -or if it cannot find any loader for the module, -then require raises an error. - - - - -

-


package.config

- - -

-A string describing some compile-time configurations for packages. -This string is a sequence of lines: - -

    - -
  • The first line is the directory separator string. -Default is '\' for Windows and '/' for all other systems.
  • - -
  • The second line is the character that separates templates in a path. -Default is ';'.
  • - -
  • The third line is the string that marks the -substitution points in a template. -Default is '?'.
  • - -
  • The fourth line is a string that, in a path in Windows, -is replaced by the executable's directory. -Default is '!'.
  • - -
  • The fifth line is a mark to ignore all text after it -when building the luaopen_ function name. -Default is '-'.
  • - -
- - - -

-


package.cpath

- - -

-A string with the path used by require -to search for a C loader. - - -

-Lua initializes the C path package.cpath in the same way -it initializes the Lua path package.path, -using the environment variable LUA_CPATH_5_4, -or the environment variable LUA_CPATH, -or a default path defined in luaconf.h. - - - - -

-


package.loaded

- - -

-A table used by require to control which -modules are already loaded. -When you require a module modname and -package.loaded[modname] is not false, -require simply returns the value stored there. - - -

-This variable is only a reference to the real table; -assignments to this variable do not change the -table used by require. - - - - -

-


package.loadlib (libname, funcname)

- - -

-Dynamically links the host program with the C library libname. - - -

-If funcname is "*", -then it only links with the library, -making the symbols exported by the library -available to other dynamically linked libraries. -Otherwise, -it looks for a function funcname inside the library -and returns this function as a C function. -So, funcname must follow the lua_CFunction prototype -(see lua_CFunction). - - -

-This is a low-level function. -It completely bypasses the package and module system. -Unlike require, -it does not perform any path searching and -does not automatically adds extensions. -libname must be the complete file name of the C library, -including if necessary a path and an extension. -funcname must be the exact name exported by the C library -(which may depend on the C compiler and linker used). - - -

-This function is not supported by Standard C. -As such, it is only available on some platforms -(Windows, Linux, Mac OS X, Solaris, BSD, -plus other Unix systems that support the dlfcn standard). - - -

-This function is inherently insecure, -as it allows Lua to call any function in any readable dynamic -library in the system. -(Lua calls any function assuming the function -has a proper prototype and respects a proper protocol -(see lua_CFunction). -Therefore, -calling an arbitrary function in an arbitrary dynamic library -more often than not results in an access violation.) - - - - -

-


package.path

- - -

-A string with the path used by require -to search for a Lua loader. - - -

-At start-up, Lua initializes this variable with -the value of the environment variable LUA_PATH_5_4 or -the environment variable LUA_PATH or -with a default path defined in luaconf.h, -if those environment variables are not defined. -A ";;" in the value of the environment variable -is replaced by the default path. - - - - -

-


package.preload

- - -

-A table to store loaders for specific modules -(see require). - - -

-This variable is only a reference to the real table; -assignments to this variable do not change the -table used by require. - - - - -

-


package.searchers

- - -

-A table used by require to control how to find modules. - - -

-Each entry in this table is a searcher function. -When looking for a module, -require calls each of these searchers in ascending order, -with the module name (the argument given to require) as its -sole argument. -If the searcher finds the module, -it returns another function, the module loader, -plus an extra value, a loader data, -that will be passed to that loader and -returned as a second result by require. -If it cannot find the module, -it returns a string explaining why -(or nil if it has nothing to say). - - -

-Lua initializes this table with four searcher functions. - - -

-The first searcher simply looks for a loader in the -package.preload table. - - -

-The second searcher looks for a loader as a Lua library, -using the path stored at package.path. -The search is done as described in function package.searchpath. - - -

-The third searcher looks for a loader as a C library, -using the path given by the variable package.cpath. -Again, -the search is done as described in function package.searchpath. -For instance, -if the C path is the string - -

-     "./?.so;./?.dll;/usr/local/?/init.so"
-

-the searcher for module foo -will try to open the files ./foo.so, ./foo.dll, -and /usr/local/foo/init.so, in that order. -Once it finds a C library, -this searcher first uses a dynamic link facility to link the -application with the library. -Then it tries to find a C function inside the library to -be used as the loader. -The name of this C function is the string "luaopen_" -concatenated with a copy of the module name where each dot -is replaced by an underscore. -Moreover, if the module name has a hyphen, -its suffix after (and including) the first hyphen is removed. -For instance, if the module name is a.b.c-v2.1, -the function name will be luaopen_a_b_c. - - -

-The fourth searcher tries an all-in-one loader. -It searches the C path for a library for -the root name of the given module. -For instance, when requiring a.b.c, -it will search for a C library for a. -If found, it looks into it for an open function for -the submodule; -in our example, that would be luaopen_a_b_c. -With this facility, a package can pack several C submodules -into one single library, -with each submodule keeping its original open function. - - -

-All searchers except the first one (preload) return as the extra value -the file path where the module was found, -as returned by package.searchpath. -The first searcher always returns the string ":preload:". - - -

-Searchers should raise no errors and have no side effects in Lua. -(They may have side effects in C, -for instance by linking the application with a library.) - - - - -

-


package.searchpath (name, path [, sep [, rep]])

- - -

-Searches for the given name in the given path. - - -

-A path is a string containing a sequence of -templates separated by semicolons. -For each template, -the function replaces each interrogation mark (if any) -in the template with a copy of name -wherein all occurrences of sep -(a dot, by default) -were replaced by rep -(the system's directory separator, by default), -and then tries to open the resulting file name. - - -

-For instance, if the path is the string - -

-     "./?.lua;./?.lc;/usr/local/?/init.lua"
-

-the search for the name foo.a -will try to open the files -./foo/a.lua, ./foo/a.lc, and -/usr/local/foo/a/init.lua, in that order. - - -

-Returns the resulting name of the first file that it can -open in read mode (after closing the file), -or fail plus an error message if none succeeds. -(This error message lists all file names it tried to open.) - - - - - - - -

6.4 – String Manipulation

- - - -

-This library provides generic functions for string manipulation, -such as finding and extracting substrings, and pattern matching. -When indexing a string in Lua, the first character is at position 1 -(not at 0, as in C). -Indices are allowed to be negative and are interpreted as indexing backwards, -from the end of the string. -Thus, the last character is at position -1, and so on. - - -

-The string library provides all its functions inside the table -string. -It also sets a metatable for strings -where the __index field points to the string table. -Therefore, you can use the string functions in object-oriented style. -For instance, string.byte(s,i) -can be written as s:byte(i). - - -

-The string library assumes one-byte character encodings. - - -

-


string.byte (s [, i [, j]])

-Returns the internal numeric codes of the characters s[i], -s[i+1], ..., s[j]. -The default value for i is 1; -the default value for j is i. -These indices are corrected -following the same rules of function string.sub. - - -

-Numeric codes are not necessarily portable across platforms. - - - - -

-


string.char (···)

-Receives zero or more integers. -Returns a string with length equal to the number of arguments, -in which each character has the internal numeric code equal -to its corresponding argument. - - -

-Numeric codes are not necessarily portable across platforms. - - - - -

-


string.dump (function [, strip])

- - -

-Returns a string containing a binary representation -(a binary chunk) -of the given function, -so that a later load on this string returns -a copy of the function (but with new upvalues). -If strip is a true value, -the binary representation may not include all debug information -about the function, -to save space. - - -

-Functions with upvalues have only their number of upvalues saved. -When (re)loaded, -those upvalues receive fresh instances. -(See the load function for details about -how these upvalues are initialized. -You can use the debug library to serialize -and reload the upvalues of a function -in a way adequate to your needs.) - - - - -

-


string.find (s, pattern [, init [, plain]])

- - -

-Looks for the first match of -pattern (see §6.4.1) in the string s. -If it finds a match, then find returns the indices of s -where this occurrence starts and ends; -otherwise, it returns fail. -A third, optional numeric argument init specifies -where to start the search; -its default value is 1 and can be negative. -A true as a fourth, optional argument plain -turns off the pattern matching facilities, -so the function does a plain "find substring" operation, -with no characters in pattern being considered magic. - - -

-If the pattern has captures, -then in a successful match -the captured values are also returned, -after the two indices. - - - - -

-


string.format (formatstring, ···)

- - -

-Returns a formatted version of its variable number of arguments -following the description given in its first argument, -which must be a string. -The format string follows the same rules as the ISO C function sprintf. -The only differences are that the conversion specifiers and modifiers -F, n, *, h, L, and l are not supported -and that there is an extra specifier, q. -Both width and precision, when present, -are limited to two digits. - - -

-The specifier q formats booleans, nil, numbers, and strings -in a way that the result is a valid constant in Lua source code. -Booleans and nil are written in the obvious way -(true, false, nil). -Floats are written in hexadecimal, -to preserve full precision. -A string is written between double quotes, -using escape sequences when necessary to ensure that -it can safely be read back by the Lua interpreter. -For instance, the call - -

-     string.format('%q', 'a string with "quotes" and \n new line')
-

-may produce the string: - -

-     "a string with \"quotes\" and \
-      new line"
-

-This specifier does not support modifiers (flags, width, precision). - - -

-The conversion specifiers -A, a, E, e, f, -G, and g all expect a number as argument. -The specifiers c, d, -i, o, u, X, and x -expect an integer. -When Lua is compiled with a C89 compiler, -the specifiers A and a (hexadecimal floats) -do not support modifiers. - - -

-The specifier s expects a string; -if its argument is not a string, -it is converted to one following the same rules of tostring. -If the specifier has any modifier, -the corresponding string argument should not contain embedded zeros. - - -

-The specifier p formats the pointer -returned by lua_topointer. -That gives a unique string identifier for tables, userdata, -threads, strings, and functions. -For other values (numbers, nil, booleans), -this specifier results in a string representing -the pointer NULL. - - - - -

-


string.gmatch (s, pattern [, init])

-Returns an iterator function that, -each time it is called, -returns the next captures from pattern (see §6.4.1) -over the string s. -If pattern specifies no captures, -then the whole match is produced in each call. -A third, optional numeric argument init specifies -where to start the search; -its default value is 1 and can be negative. - - -

-As an example, the following loop -will iterate over all the words from string s, -printing one per line: - -

-     s = "hello world from Lua"
-     for w in string.gmatch(s, "%a+") do
-       print(w)
-     end
-

-The next example collects all pairs key=value from the -given string into a table: - -

-     t = {}
-     s = "from=world, to=Lua"
-     for k, v in string.gmatch(s, "(%w+)=(%w+)") do
-       t[k] = v
-     end
-
- -

-For this function, a caret '^' at the start of a pattern does not -work as an anchor, as this would prevent the iteration. - - - - -

-


string.gsub (s, pattern, repl [, n])

-Returns a copy of s -in which all (or the first n, if given) -occurrences of the pattern (see §6.4.1) have been -replaced by a replacement string specified by repl, -which can be a string, a table, or a function. -gsub also returns, as its second value, -the total number of matches that occurred. -The name gsub comes from Global SUBstitution. - - -

-If repl is a string, then its value is used for replacement. -The character % works as an escape character: -any sequence in repl of the form %d, -with d between 1 and 9, -stands for the value of the d-th captured substring; -the sequence %0 stands for the whole match; -the sequence %% stands for a single %. - - -

-If repl is a table, then the table is queried for every match, -using the first capture as the key. - - -

-If repl is a function, then this function is called every time a -match occurs, with all captured substrings passed as arguments, -in order. - - -

-In any case, -if the pattern specifies no captures, -then it behaves as if the whole pattern was inside a capture. - - -

-If the value returned by the table query or by the function call -is a string or a number, -then it is used as the replacement string; -otherwise, if it is false or nil, -then there is no replacement -(that is, the original match is kept in the string). - - -

-Here are some examples: - -

-     x = string.gsub("hello world", "(%w+)", "%1 %1")
-     --> x="hello hello world world"
-     
-     x = string.gsub("hello world", "%w+", "%0 %0", 1)
-     --> x="hello hello world"
-     
-     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
-     --> x="world hello Lua from"
-     
-     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
-     --> x="home = /home/roberto, user = roberto"
-     
-     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
-           return load(s)()
-         end)
-     --> x="4+5 = 9"
-     
-     local t = {name="lua", version="5.4"}
-     x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
-     --> x="lua-5.4.tar.gz"
-
- - - -

-


string.len (s)

- - -

-Receives a string and returns its length. -The empty string "" has length 0. -Embedded zeros are counted, -so "a\000bc\000" has length 5. - - - - -

-


string.lower (s)

- - -

-Receives a string and returns a copy of this string with all -uppercase letters changed to lowercase. -All other characters are left unchanged. -The definition of what an uppercase letter is depends on the current locale. - - - - -

-


string.match (s, pattern [, init])

- - -

-Looks for the first match of -the pattern (see §6.4.1) in the string s. -If it finds one, then match returns -the captures from the pattern; -otherwise it returns fail. -If pattern specifies no captures, -then the whole match is returned. -A third, optional numeric argument init specifies -where to start the search; -its default value is 1 and can be negative. - - - - -

-


string.pack (fmt, v1, v2, ···)

- - -

-Returns a binary string containing the values v1, v2, etc. -serialized in binary form (packed) -according to the format string fmt (see §6.4.2). - - - - -

-


string.packsize (fmt)

- - -

-Returns the size of a string resulting from string.pack -with the given format. -The format string cannot have the variable-length options -'s' or 'z' (see §6.4.2). - - - - -

-


string.rep (s, n [, sep])

- - -

-Returns a string that is the concatenation of n copies of -the string s separated by the string sep. -The default value for sep is the empty string -(that is, no separator). -Returns the empty string if n is not positive. - - -

-(Note that it is very easy to exhaust the memory of your machine -with a single call to this function.) - - - - -

-


string.reverse (s)

- - -

-Returns a string that is the string s reversed. - - - - -

-


string.sub (s, i [, j])

- - -

-Returns the substring of s that -starts at i and continues until j; -i and j can be negative. -If j is absent, then it is assumed to be equal to -1 -(which is the same as the string length). -In particular, -the call string.sub(s,1,j) returns a prefix of s -with length j, -and string.sub(s, -i) (for a positive i) -returns a suffix of s -with length i. - - -

-If, after the translation of negative indices, -i is less than 1, -it is corrected to 1. -If j is greater than the string length, -it is corrected to that length. -If, after these corrections, -i is greater than j, -the function returns the empty string. - - - - -

-


string.unpack (fmt, s [, pos])

- - -

-Returns the values packed in string s (see string.pack) -according to the format string fmt (see §6.4.2). -An optional pos marks where -to start reading in s (default is 1). -After the read values, -this function also returns the index of the first unread byte in s. - - - - -

-


string.upper (s)

- - -

-Receives a string and returns a copy of this string with all -lowercase letters changed to uppercase. -All other characters are left unchanged. -The definition of what a lowercase letter is depends on the current locale. - - - - - - - -

6.4.1 – Patterns

- - - -

-Patterns in Lua are described by regular strings, -which are interpreted as patterns by the pattern-matching functions -string.find, -string.gmatch, -string.gsub, -and string.match. -This section describes the syntax and the meaning -(that is, what they match) of these strings. - - - - - -

Character Class:

-A character class is used to represent a set of characters. -The following combinations are allowed in describing a character class: - -

    - -
  • x: -(where x is not one of the magic characters -^$()%.[]*+-?) -represents the character x itself. -
  • - -
  • .: (a dot) represents all characters.
  • - -
  • %a: represents all letters.
  • - -
  • %c: represents all control characters.
  • - -
  • %d: represents all digits.
  • - -
  • %g: represents all printable characters except space.
  • - -
  • %l: represents all lowercase letters.
  • - -
  • %p: represents all punctuation characters.
  • - -
  • %s: represents all space characters.
  • - -
  • %u: represents all uppercase letters.
  • - -
  • %w: represents all alphanumeric characters.
  • - -
  • %x: represents all hexadecimal digits.
  • - -
  • %x: (where x is any non-alphanumeric character) -represents the character x. -This is the standard way to escape the magic characters. -Any non-alphanumeric character -(including all punctuation characters, even the non-magical) -can be preceded by a '%' to represent itself in a pattern. -
  • - -
  • [set]: -represents the class which is the union of all -characters in set. -A range of characters can be specified by -separating the end characters of the range, -in ascending order, with a '-'. -All classes %x described above can also be used as -components in set. -All other characters in set represent themselves. -For example, [%w_] (or [_%w]) -represents all alphanumeric characters plus the underscore, -[0-7] represents the octal digits, -and [0-7%l%-] represents the octal digits plus -the lowercase letters plus the '-' character. - - -

    -You can put a closing square bracket in a set -by positioning it as the first character in the set. -You can put a hyphen in a set -by positioning it as the first or the last character in the set. -(You can also use an escape for both cases.) - - -

    -The interaction between ranges and classes is not defined. -Therefore, patterns like [%a-z] or [a-%%] -have no meaning. -

  • - -
  • [^set]: -represents the complement of set, -where set is interpreted as above. -
  • - -

-For all classes represented by single letters (%a, %c, etc.), -the corresponding uppercase letter represents the complement of the class. -For instance, %S represents all non-space characters. - - -

-The definitions of letter, space, and other character groups -depend on the current locale. -In particular, the class [a-z] may not be equivalent to %l. - - - - - -

Pattern Item:

-A pattern item can be - -

    - -
  • -a single character class, -which matches any single character in the class; -
  • - -
  • -a single character class followed by '*', -which matches sequences of zero or more characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '+', -which matches sequences of one or more characters in the class. -These repetition items will always match the longest possible sequence; -
  • - -
  • -a single character class followed by '-', -which also matches sequences of zero or more characters in the class. -Unlike '*', -these repetition items will always match the shortest possible sequence; -
  • - -
  • -a single character class followed by '?', -which matches zero or one occurrence of a character in the class. -It always matches one occurrence if possible; -
  • - -
  • -%n, for n between 1 and 9; -such item matches a substring equal to the n-th captured string -(see below); -
  • - -
  • -%bxy, where x and y are two distinct characters; -such item matches strings that start with x, end with y, -and where the x and y are balanced. -This means that, if one reads the string from left to right, -counting +1 for an x and -1 for a y, -the ending y is the first y where the count reaches 0. -For instance, the item %b() matches expressions with -balanced parentheses. -
  • - -
  • -%f[set], a frontier pattern; -such item matches an empty string at any position such that -the next character belongs to set -and the previous character does not belong to set. -The set set is interpreted as previously described. -The beginning and the end of the subject are handled as if -they were the character '\0'. -
  • - -
- - - - -

Pattern:

-A pattern is a sequence of pattern items. -A caret '^' at the beginning of a pattern anchors the match at the -beginning of the subject string. -A '$' at the end of a pattern anchors the match at the -end of the subject string. -At other positions, -'^' and '$' have no special meaning and represent themselves. - - - - - -

Captures:

-A pattern can contain sub-patterns enclosed in parentheses; -they describe captures. -When a match succeeds, the substrings of the subject string -that match captures are stored (captured) for future use. -Captures are numbered according to their left parentheses. -For instance, in the pattern "(a*(.)%w(%s*))", -the part of the string matching "a*(.)%w(%s*)" is -stored as the first capture, and therefore has number 1; -the character matching "." is captured with number 2, -and the part matching "%s*" has number 3. - - -

-As a special case, the capture () captures -the current string position (a number). -For instance, if we apply the pattern "()aa()" on the -string "flaaap", there will be two captures: 3 and 5. - - - - - -

Multiple matches:

-The function string.gsub and the iterator string.gmatch -match multiple occurrences of the given pattern in the subject. -For these functions, -a new match is considered valid only -if it ends at least one byte after the end of the previous match. -In other words, the pattern machine never accepts the -empty string as a match immediately after another match. -As an example, -consider the results of the following code: - -

-     > string.gsub("abc", "()a*()", print);
-     --> 1   2
-     --> 3   3
-     --> 4   4
-

-The second and third results come from Lua matching an empty -string after 'b' and another one after 'c'. -Lua does not match an empty string after 'a', -because it would end at the same position of the previous match. - - - - - - - -

6.4.2 – Format Strings for Pack and Unpack

- -

-The first argument to string.pack, -string.packsize, and string.unpack -is a format string, -which describes the layout of the structure being created or read. - - -

-A format string is a sequence of conversion options. -The conversion options are as follows: - -

    -
  • <: sets little endian
  • -
  • >: sets big endian
  • -
  • =: sets native endian
  • -
  • ![n]: sets maximum alignment to n -(default is native alignment)
  • -
  • b: a signed byte (char)
  • -
  • B: an unsigned byte (char)
  • -
  • h: a signed short (native size)
  • -
  • H: an unsigned short (native size)
  • -
  • l: a signed long (native size)
  • -
  • L: an unsigned long (native size)
  • -
  • j: a lua_Integer
  • -
  • J: a lua_Unsigned
  • -
  • T: a size_t (native size)
  • -
  • i[n]: a signed int with n bytes -(default is native size)
  • -
  • I[n]: an unsigned int with n bytes -(default is native size)
  • -
  • f: a float (native size)
  • -
  • d: a double (native size)
  • -
  • n: a lua_Number
  • -
  • cn: a fixed-sized string with n bytes
  • -
  • z: a zero-terminated string
  • -
  • s[n]: a string preceded by its length -coded as an unsigned integer with n bytes -(default is a size_t)
  • -
  • x: one byte of padding
  • -
  • Xop: an empty item that aligns -according to option op -(which is otherwise ignored)
  • -
  • ' ': (space) ignored
  • -

-(A "[n]" means an optional integral numeral.) -Except for padding, spaces, and configurations -(options "xX <=>!"), -each option corresponds to an argument in string.pack -or a result in string.unpack. - - -

-For options "!n", "sn", "in", and "In", -n can be any integer between 1 and 16. -All integral options check overflows; -string.pack checks whether the given value fits in the given size; -string.unpack checks whether the read value fits in a Lua integer. -For the unsigned options, -Lua integers are treated as unsigned values too. - - -

-Any format string starts as if prefixed by "!1=", -that is, -with maximum alignment of 1 (no alignment) -and native endianness. - - -

-Native endianness assumes that the whole system is -either big or little endian. -The packing functions will not emulate correctly the behavior -of mixed-endian formats. - - -

-Alignment works as follows: -For each option, -the format gets extra padding until the data starts -at an offset that is a multiple of the minimum between the -option size and the maximum alignment; -this minimum must be a power of 2. -Options "c" and "z" are not aligned; -option "s" follows the alignment of its starting integer. - - -

-All padding is filled with zeros by string.pack -and ignored by string.unpack. - - - - - - - -

6.5 – UTF-8 Support

- -

-This library provides basic support for UTF-8 encoding. -It provides all its functions inside the table utf8. -This library does not provide any support for Unicode other -than the handling of the encoding. -Any operation that needs the meaning of a character, -such as character classification, is outside its scope. - - -

-Unless stated otherwise, -all functions that expect a byte position as a parameter -assume that the given position is either the start of a byte sequence -or one plus the length of the subject string. -As in the string library, -negative indices count from the end of the string. - - -

-Functions that create byte sequences -accept all values up to 0x7FFFFFFF, -as defined in the original UTF-8 specification; -that implies byte sequences of up to six bytes. - - -

-Functions that interpret byte sequences only accept -valid sequences (well formed and not overlong). -By default, they only accept byte sequences -that result in valid Unicode code points, -rejecting values greater than 10FFFF and surrogates. -A boolean argument lax, when available, -lifts these checks, -so that all values up to 0x7FFFFFFF are accepted. -(Not well formed and overlong sequences are still rejected.) - - -

-


utf8.char (···)

- - -

-Receives zero or more integers, -converts each one to its corresponding UTF-8 byte sequence -and returns a string with the concatenation of all these sequences. - - - - -

-


utf8.charpattern

- - -

-The pattern (a string, not a function) "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" -(see §6.4.1), -which matches exactly one UTF-8 byte sequence, -assuming that the subject is a valid UTF-8 string. - - - - -

-


utf8.codes (s [, lax])

- - -

-Returns values so that the construction - -

-     for p, c in utf8.codes(s) do body end
-

-will iterate over all UTF-8 characters in string s, -with p being the position (in bytes) and c the code point -of each character. -It raises an error if it meets any invalid byte sequence. - - - - -

-


utf8.codepoint (s [, i [, j [, lax]]])

- - -

-Returns the code points (as integers) from all characters in s -that start between byte position i and j (both included). -The default for i is 1 and for j is i. -It raises an error if it meets any invalid byte sequence. - - - - -

-


utf8.len (s [, i [, j [, lax]]])

- - -

-Returns the number of UTF-8 characters in string s -that start between positions i and j (both inclusive). -The default for i is 1 and for j is -1. -If it finds any invalid byte sequence, -returns fail plus the position of the first invalid byte. - - - - -

-


utf8.offset (s, n [, i])

- - -

-Returns the position (in bytes) where the encoding of the -n-th character of s -(counting from position i) starts. -A negative n gets characters before position i. -The default for i is 1 when n is non-negative -and #s + 1 otherwise, -so that utf8.offset(s, -n) gets the offset of the -n-th character from the end of the string. -If the specified character is neither in the subject -nor right after its end, -the function returns fail. - - -

-As a special case, -when n is 0 the function returns the start of the encoding -of the character that contains the i-th byte of s. - - -

-This function assumes that s is a valid UTF-8 string. - - - - - - - -

6.6 – Table Manipulation

- -

-This library provides generic functions for table manipulation. -It provides all its functions inside the table table. - - -

-Remember that, whenever an operation needs the length of a table, -all caveats about the length operator apply (see §3.4.7). -All functions ignore non-numeric keys -in the tables given as arguments. - - -

-


table.concat (list [, sep [, i [, j]]])

- - -

-Given a list where all elements are strings or numbers, -returns the string list[i]..sep..list[i+1] ··· sep..list[j]. -The default value for sep is the empty string, -the default for i is 1, -and the default for j is #list. -If i is greater than j, returns the empty string. - - - - -

-


table.insert (list, [pos,] value)

- - -

-Inserts element value at position pos in list, -shifting up the elements -list[pos], list[pos+1], ···, list[#list]. -The default value for pos is #list+1, -so that a call table.insert(t,x) inserts x at the end -of the list t. - - - - -

-


table.move (a1, f, e, t [,a2])

- - -

-Moves elements from the table a1 to the table a2, -performing the equivalent to the following -multiple assignment: -a2[t],··· = a1[f],···,a1[e]. -The default for a2 is a1. -The destination range can overlap with the source range. -The number of elements to be moved must fit in a Lua integer. - - -

-Returns the destination table a2. - - - - -

-


table.pack (···)

- - -

-Returns a new table with all arguments stored into keys 1, 2, etc. -and with a field "n" with the total number of arguments. -Note that the resulting table may not be a sequence, -if some arguments are nil. - - - - -

-


table.remove (list [, pos])

- - -

-Removes from list the element at position pos, -returning the value of the removed element. -When pos is an integer between 1 and #list, -it shifts down the elements -list[pos+1], list[pos+2], ···, list[#list] -and erases element list[#list]; -The index pos can also be 0 when #list is 0, -or #list + 1. - - -

-The default value for pos is #list, -so that a call table.remove(l) removes the last element -of the list l. - - - - -

-


table.sort (list [, comp])

- - -

-Sorts the list elements in a given order, in-place, -from list[1] to list[#list]. -If comp is given, -then it must be a function that receives two list elements -and returns true when the first element must come -before the second in the final order, -so that, after the sort, -i <= j implies not comp(list[j],list[i]). -If comp is not given, -then the standard Lua operator < is used instead. - - -

-The comp function must define a consistent order; -more formally, the function must define a strict weak order. -(A weak order is similar to a total order, -but it can equate different elements for comparison purposes.) - - -

-The sort algorithm is not stable: -Different elements considered equal by the given order -may have their relative positions changed by the sort. - - - - -

-


table.unpack (list [, i [, j]])

- - -

-Returns the elements from the given list. -This function is equivalent to - -

-     return list[i], list[i+1], ···, list[j]
-

-By default, i is 1 and j is #list. - - - - - - - -

6.7 – Mathematical Functions

- -

-This library provides basic mathematical functions. -It provides all its functions and constants inside the table math. -Functions with the annotation "integer/float" give -integer results for integer arguments -and float results for non-integer arguments. -The rounding functions -math.ceil, math.floor, and math.modf -return an integer when the result fits in the range of an integer, -or a float otherwise. - - -

-


math.abs (x)

- - -

-Returns the maximum value between x and -x. (integer/float) - - - - -

-


math.acos (x)

- - -

-Returns the arc cosine of x (in radians). - - - - -

-


math.asin (x)

- - -

-Returns the arc sine of x (in radians). - - - - -

-


math.atan (y [, x])

- - -

- -Returns the arc tangent of y/x (in radians), -but uses the signs of both arguments to find the -quadrant of the result. -It also handles correctly the case of x being zero. - - -

-The default value for x is 1, -so that the call math.atan(y) -returns the arc tangent of y. - - - - -

-


math.ceil (x)

- - -

-Returns the smallest integral value greater than or equal to x. - - - - -

-


math.cos (x)

- - -

-Returns the cosine of x (assumed to be in radians). - - - - -

-


math.deg (x)

- - -

-Converts the angle x from radians to degrees. - - - - -

-


math.exp (x)

- - -

-Returns the value ex -(where e is the base of natural logarithms). - - - - -

-


math.floor (x)

- - -

-Returns the largest integral value less than or equal to x. - - - - -

-


math.fmod (x, y)

- - -

-Returns the remainder of the division of x by y -that rounds the quotient towards zero. (integer/float) - - - - -

-


math.huge

- - -

-The float value HUGE_VAL, -a value greater than any other numeric value. - - - - -

-


math.log (x [, base])

- - -

-Returns the logarithm of x in the given base. -The default for base is e -(so that the function returns the natural logarithm of x). - - - - -

-


math.max (x, ···)

- - -

-Returns the argument with the maximum value, -according to the Lua operator <. - - - - -

-


math.maxinteger

-An integer with the maximum value for an integer. - - - - -

-


math.min (x, ···)

- - -

-Returns the argument with the minimum value, -according to the Lua operator <. - - - - -

-


math.mininteger

-An integer with the minimum value for an integer. - - - - -

-


math.modf (x)

- - -

-Returns the integral part of x and the fractional part of x. -Its second result is always a float. - - - - -

-


math.pi

- - -

-The value of π. - - - - -

-


math.rad (x)

- - -

-Converts the angle x from degrees to radians. - - - - -

-


math.random ([m [, n]])

- - -

-When called without arguments, -returns a pseudo-random float with uniform distribution -in the range [0,1). -When called with two integers m and n, -math.random returns a pseudo-random integer -with uniform distribution in the range [m, n]. -The call math.random(n), for a positive n, -is equivalent to math.random(1,n). -The call math.random(0) produces an integer with -all bits (pseudo)random. - - -

-This function uses the xoshiro256** algorithm to produce -pseudo-random 64-bit integers, -which are the results of calls with argument 0. -Other results (ranges and floats) -are unbiased extracted from these integers. - - -

-Lua initializes its pseudo-random generator with the equivalent of -a call to math.randomseed with no arguments, -so that math.random should generate -different sequences of results each time the program runs. - - - - -

-


math.randomseed ([x [, y]])

- - -

-When called with at least one argument, -the integer parameters x and y are -joined into a 128-bit seed that -is used to reinitialize the pseudo-random generator; -equal seeds produce equal sequences of numbers. -The default for y is zero. - - -

-When called with no arguments, -Lua generates a seed with -a weak attempt for randomness. - - -

-This function returns the two seed components -that were effectively used, -so that setting them again repeats the sequence. - - -

-To ensure a required level of randomness to the initial state -(or contrarily, to have a deterministic sequence, -for instance when debugging a program), -you should call math.randomseed with explicit arguments. - - - - -

-


math.sin (x)

- - -

-Returns the sine of x (assumed to be in radians). - - - - -

-


math.sqrt (x)

- - -

-Returns the square root of x. -(You can also use the expression x^0.5 to compute this value.) - - - - -

-


math.tan (x)

- - -

-Returns the tangent of x (assumed to be in radians). - - - - -

-


math.tointeger (x)

- - -

-If the value x is convertible to an integer, -returns that integer. -Otherwise, returns fail. - - - - -

-


math.type (x)

- - -

-Returns "integer" if x is an integer, -"float" if it is a float, -or fail if x is not a number. - - - - -

-


math.ult (m, n)

- - -

-Returns a boolean, -true if and only if integer m is below integer n when -they are compared as unsigned integers. - - - - - - - -

6.8 – Input and Output Facilities

- -

-The I/O library provides two different styles for file manipulation. -The first one uses implicit file handles; -that is, there are operations to set a default input file and a -default output file, -and all input/output operations are done over these default files. -The second style uses explicit file handles. - - -

-When using implicit file handles, -all operations are supplied by table io. -When using explicit file handles, -the operation io.open returns a file handle -and then all operations are supplied as methods of the file handle. - - -

-The metatable for file handles provides metamethods -for __gc and __close that try -to close the file when called. - - -

-The table io also provides -three predefined file handles with their usual meanings from C: -io.stdin, io.stdout, and io.stderr. -The I/O library never closes these files. - - -

-Unless otherwise stated, -all I/O functions return fail on failure, -plus an error message as a second result and -a system-dependent error code as a third result, -and some non-false value on success. -On non-POSIX systems, -the computation of the error message and error code -in case of errors -may be not thread safe, -because they rely on the global C variable errno. - - -

-


io.close ([file])

- - -

-Equivalent to file:close(). -Without a file, closes the default output file. - - - - -

-


io.flush ()

- - -

-Equivalent to io.output():flush(). - - - - -

-


io.input ([file])

- - -

-When called with a file name, it opens the named file (in text mode), -and sets its handle as the default input file. -When called with a file handle, -it simply sets this file handle as the default input file. -When called without arguments, -it returns the current default input file. - - -

-In case of errors this function raises the error, -instead of returning an error code. - - - - -

-


io.lines ([filename, ···])

- - -

-Opens the given file name in read mode -and returns an iterator function that -works like file:lines(···) over the opened file. -When the iterator function fails to read any value, -it automatically closes the file. -Besides the iterator function, -io.lines returns three other values: -two nil values as placeholders, -plus the created file handle. -Therefore, when used in a generic for loop, -the file is closed also if the loop is interrupted by an -error or a break. - - -

-The call io.lines() (with no file name) is equivalent -to io.input():lines("l"); -that is, it iterates over the lines of the default input file. -In this case, the iterator does not close the file when the loop ends. - - -

-In case of errors opening the file, -this function raises the error, -instead of returning an error code. - - - - -

-


io.open (filename [, mode])

- - -

-This function opens a file, -in the mode specified in the string mode. -In case of success, -it returns a new file handle. - - -

-The mode string can be any of the following: - -

    -
  • "r": read mode (the default);
  • -
  • "w": write mode;
  • -
  • "a": append mode;
  • -
  • "r+": update mode, all previous data is preserved;
  • -
  • "w+": update mode, all previous data is erased;
  • -
  • "a+": append update mode, previous data is preserved, - writing is only allowed at the end of file.
  • -

-The mode string can also have a 'b' at the end, -which is needed in some systems to open the file in binary mode. - - - - -

-


io.output ([file])

- - -

-Similar to io.input, but operates over the default output file. - - - - -

-


io.popen (prog [, mode])

- - -

-This function is system dependent and is not available -on all platforms. - - -

-Starts the program prog in a separated process and returns -a file handle that you can use to read data from this program -(if mode is "r", the default) -or to write data to this program -(if mode is "w"). - - - - -

-


io.read (···)

- - -

-Equivalent to io.input():read(···). - - - - -

-


io.tmpfile ()

- - -

-In case of success, -returns a handle for a temporary file. -This file is opened in update mode -and it is automatically removed when the program ends. - - - - -

-


io.type (obj)

- - -

-Checks whether obj is a valid file handle. -Returns the string "file" if obj is an open file handle, -"closed file" if obj is a closed file handle, -or fail if obj is not a file handle. - - - - -

-


io.write (···)

- - -

-Equivalent to io.output():write(···). - - - - -

-


file:close ()

- - -

-Closes file. -Note that files are automatically closed when -their handles are garbage collected, -but that takes an unpredictable amount of time to happen. - - -

-When closing a file handle created with io.popen, -file:close returns the same values -returned by os.execute. - - - - -

-


file:flush ()

- - -

-Saves any written data to file. - - - - -

-


file:lines (···)

- - -

-Returns an iterator function that, -each time it is called, -reads the file according to the given formats. -When no format is given, -uses "l" as a default. -As an example, the construction - -

-     for c in file:lines(1) do body end
-

-will iterate over all characters of the file, -starting at the current position. -Unlike io.lines, this function does not close the file -when the loop ends. - - - - -

-


file:read (···)

- - -

-Reads the file file, -according to the given formats, which specify what to read. -For each format, -the function returns a string or a number with the characters read, -or fail if it cannot read data with the specified format. -(In this latter case, -the function does not read subsequent formats.) -When called without arguments, -it uses a default format that reads the next line -(see below). - - -

-The available formats are - -

    - -
  • "n": -reads a numeral and returns it as a float or an integer, -following the lexical conventions of Lua. -(The numeral may have leading whitespaces and a sign.) -This format always reads the longest input sequence that -is a valid prefix for a numeral; -if that prefix does not form a valid numeral -(e.g., an empty string, "0x", or "3.4e-") -or it is too long (more than 200 characters), -it is discarded and the format returns fail. -
  • - -
  • "a": -reads the whole file, starting at the current position. -On end of file, it returns the empty string; -this format never fails. -
  • - -
  • "l": -reads the next line skipping the end of line, -returning fail on end of file. -This is the default format. -
  • - -
  • "L": -reads the next line keeping the end-of-line character (if present), -returning fail on end of file. -
  • - -
  • number: -reads a string with up to this number of bytes, -returning fail on end of file. -If number is zero, -it reads nothing and returns an empty string, -or fail on end of file. -
  • - -

-The formats "l" and "L" should be used only for text files. - - - - -

-


file:seek ([whence [, offset]])

- - -

-Sets and gets the file position, -measured from the beginning of the file, -to the position given by offset plus a base -specified by the string whence, as follows: - -

    -
  • "set": base is position 0 (beginning of the file);
  • -
  • "cur": base is current position;
  • -
  • "end": base is end of file;
  • -

-In case of success, seek returns the final file position, -measured in bytes from the beginning of the file. -If seek fails, it returns fail, -plus a string describing the error. - - -

-The default value for whence is "cur", -and for offset is 0. -Therefore, the call file:seek() returns the current -file position, without changing it; -the call file:seek("set") sets the position to the -beginning of the file (and returns 0); -and the call file:seek("end") sets the position to the -end of the file, and returns its size. - - - - -

-


file:setvbuf (mode [, size])

- - -

-Sets the buffering mode for a file. -There are three available modes: - -

    -
  • "no": no buffering.
  • -
  • "full": full buffering.
  • -
  • "line": line buffering.
  • -
- -

-For the last two cases, -size is a hint for the size of the buffer, in bytes. -The default is an appropriate size. - - -

-The specific behavior of each mode is non portable; -check the underlying ISO C function setvbuf in your platform for -more details. - - - - -

-


file:write (···)

- - -

-Writes the value of each of its arguments to file. -The arguments must be strings or numbers. - - -

-In case of success, this function returns file. - - - - - - - -

6.9 – Operating System Facilities

- -

-This library is implemented through table os. - - -

-


os.clock ()

- - -

-Returns an approximation of the amount in seconds of CPU time -used by the program, -as returned by the underlying ISO C function clock. - - - - -

-


os.date ([format [, time]])

- - -

-Returns a string or a table containing date and time, -formatted according to the given string format. - - -

-If the time argument is present, -this is the time to be formatted -(see the os.time function for a description of this value). -Otherwise, date formats the current time. - - -

-If format starts with '!', -then the date is formatted in Coordinated Universal Time. -After this optional character, -if format is the string "*t", -then date returns a table with the following fields: -year, month (1–12), day (1–31), -hour (0–23), min (0–59), -sec (0–61, due to leap seconds), -wday (weekday, 1–7, Sunday is 1), -yday (day of the year, 1–366), -and isdst (daylight saving flag, a boolean). -This last field may be absent -if the information is not available. - - -

-If format is not "*t", -then date returns the date as a string, -formatted according to the same rules as the ISO C function strftime. - - -

-If format is absent, it defaults to "%c", -which gives a human-readable date and time representation -using the current locale. - - -

-On non-POSIX systems, -this function may be not thread safe -because of its reliance on C function gmtime and C function localtime. - - - - -

-


os.difftime (t2, t1)

- - -

-Returns the difference, in seconds, -from time t1 to time t2 -(where the times are values returned by os.time). -In POSIX, Windows, and some other systems, -this value is exactly t2-t1. - - - - -

-


os.execute ([command])

- - -

-This function is equivalent to the ISO C function system. -It passes command to be executed by an operating system shell. -Its first result is true -if the command terminated successfully, -or fail otherwise. -After this first result -the function returns a string plus a number, -as follows: - -

    - -
  • "exit": -the command terminated normally; -the following number is the exit status of the command. -
  • - -
  • "signal": -the command was terminated by a signal; -the following number is the signal that terminated the command. -
  • - -
- -

-When called without a command, -os.execute returns a boolean that is true if a shell is available. - - - - -

-


os.exit ([code [, close]])

- - -

-Calls the ISO C function exit to terminate the host program. -If code is true, -the returned status is EXIT_SUCCESS; -if code is false, -the returned status is EXIT_FAILURE; -if code is a number, -the returned status is this number. -The default value for code is true. - - -

-If the optional second argument close is true, -closes the Lua state before exiting. - - - - -

-


os.getenv (varname)

- - -

-Returns the value of the process environment variable varname -or fail if the variable is not defined. - - - - -

-


os.remove (filename)

- - -

-Deletes the file (or empty directory, on POSIX systems) -with the given name. -If this function fails, it returns fail -plus a string describing the error and the error code. -Otherwise, it returns true. - - - - -

-


os.rename (oldname, newname)

- - -

-Renames the file or directory named oldname to newname. -If this function fails, it returns fail, -plus a string describing the error and the error code. -Otherwise, it returns true. - - - - -

-


os.setlocale (locale [, category])

- - -

-Sets the current locale of the program. -locale is a system-dependent string specifying a locale; -category is an optional string describing which category to change: -"all", "collate", "ctype", -"monetary", "numeric", or "time"; -the default category is "all". -The function returns the name of the new locale, -or fail if the request cannot be honored. - - -

-If locale is the empty string, -the current locale is set to an implementation-defined native locale. -If locale is the string "C", -the current locale is set to the standard C locale. - - -

-When called with nil as the first argument, -this function only returns the name of the current locale -for the given category. - - -

-This function may be not thread safe -because of its reliance on C function setlocale. - - - - -

-


os.time ([table])

- - -

-Returns the current time when called without arguments, -or a time representing the local date and time specified by the given table. -This table must have fields year, month, and day, -and may have fields -hour (default is 12), -min (default is 0), -sec (default is 0), -and isdst (default is nil). -Other fields are ignored. -For a description of these fields, see the os.date function. - - -

-When the function is called, -the values in these fields do not need to be inside their valid ranges. -For instance, if sec is -10, -it means 10 seconds before the time specified by the other fields; -if hour is 1000, -it means 1000 hours after the time specified by the other fields. - - -

-The returned value is a number, whose meaning depends on your system. -In POSIX, Windows, and some other systems, -this number counts the number -of seconds since some given start time (the "epoch"). -In other systems, the meaning is not specified, -and the number returned by time can be used only as an argument to -os.date and os.difftime. - - -

-When called with a table, -os.time also normalizes all the fields -documented in the os.date function, -so that they represent the same time as before the call -but with values inside their valid ranges. - - - - -

-


os.tmpname ()

- - -

-Returns a string with a file name that can -be used for a temporary file. -The file must be explicitly opened before its use -and explicitly removed when no longer needed. - - -

-In POSIX systems, -this function also creates a file with that name, -to avoid security risks. -(Someone else might create the file with wrong permissions -in the time between getting the name and creating the file.) -You still have to open the file to use it -and to remove it (even if you do not use it). - - -

-When possible, -you may prefer to use io.tmpfile, -which automatically removes the file when the program ends. - - - - - - - -

6.10 – The Debug Library

- -

-This library provides -the functionality of the debug interface (§4.7) to Lua programs. -You should exert care when using this library. -Several of its functions -violate basic assumptions about Lua code -(e.g., that variables local to a function -cannot be accessed from outside; -that userdata metatables cannot be changed by Lua code; -that Lua programs do not crash) -and therefore can compromise otherwise secure code. -Moreover, some functions in this library may be slow. - - -

-All functions in this library are provided -inside the debug table. -All functions that operate over a thread -have an optional first argument which is the -thread to operate over. -The default is always the current thread. - - -

-


debug.debug ()

- - -

-Enters an interactive mode with the user, -running each string that the user enters. -Using simple commands and other debug facilities, -the user can inspect global and local variables, -change their values, evaluate expressions, and so on. -A line containing only the word cont finishes this function, -so that the caller continues its execution. - - -

-Note that commands for debug.debug are not lexically nested -within any function and so have no direct access to local variables. - - - - -

-


debug.gethook ([thread])

- - -

-Returns the current hook settings of the thread, as three values: -the current hook function, the current hook mask, -and the current hook count, -as set by the debug.sethook function. - - -

-Returns fail if there is no active hook. - - - - -

-


debug.getinfo ([thread,] f [, what])

- - -

-Returns a table with information about a function. -You can give the function directly -or you can give a number as the value of f, -which means the function running at level f of the call stack -of the given thread: -level 0 is the current function (getinfo itself); -level 1 is the function that called getinfo -(except for tail calls, which do not count in the stack); -and so on. -If f is a number greater than the number of active functions, -then getinfo returns fail. - - -

-The returned table can contain all the fields returned by lua_getinfo, -with the string what describing which fields to fill in. -The default for what is to get all information available, -except the table of valid lines. -If present, -the option 'f' -adds a field named func with the function itself. -If present, -the option 'L' -adds a field named activelines with the table of -valid lines. - - -

-For instance, the expression debug.getinfo(1,"n").name returns -a name for the current function, -if a reasonable name can be found, -and the expression debug.getinfo(print) -returns a table with all available information -about the print function. - - - - -

-


debug.getlocal ([thread,] f, local)

- - -

-This function returns the name and the value of the local variable -with index local of the function at level f of the stack. -This function accesses not only explicit local variables, -but also parameters and temporary values. - - -

-The first parameter or local variable has index 1, and so on, -following the order that they are declared in the code, -counting only the variables that are active -in the current scope of the function. -Compile-time constants may not appear in this listing, -if they were optimized away by the compiler. -Negative indices refer to vararg arguments; --1 is the first vararg argument. -The function returns fail -if there is no variable with the given index, -and raises an error when called with a level out of range. -(You can call debug.getinfo to check whether the level is valid.) - - -

-Variable names starting with '(' (open parenthesis) -represent variables with no known names -(internal variables such as loop control variables, -and variables from chunks saved without debug information). - - -

-The parameter f may also be a function. -In that case, getlocal returns only the name of function parameters. - - - - -

-


debug.getmetatable (value)

- - -

-Returns the metatable of the given value -or nil if it does not have a metatable. - - - - -

-


debug.getregistry ()

- - -

-Returns the registry table (see §4.3). - - - - -

-


debug.getupvalue (f, up)

- - -

-This function returns the name and the value of the upvalue -with index up of the function f. -The function returns fail -if there is no upvalue with the given index. - - -

-(For Lua functions, -upvalues are the external local variables that the function uses, -and that are consequently included in its closure.) - - -

-For C functions, this function uses the empty string "" -as a name for all upvalues. - - -

-Variable name '?' (interrogation mark) -represents variables with no known names -(variables from chunks saved without debug information). - - - - -

-


debug.getuservalue (u, n)

- - -

-Returns the n-th user value associated -to the userdata u plus a boolean, -false if the userdata does not have that value. - - - - -

-


debug.sethook ([thread,] hook, mask [, count])

- - -

-Sets the given function as the debug hook. -The string mask and the number count describe -when the hook will be called. -The string mask may have any combination of the following characters, -with the given meaning: - -

    -
  • 'c': the hook is called every time Lua calls a function;
  • -
  • 'r': the hook is called every time Lua returns from a function;
  • -
  • 'l': the hook is called every time Lua enters a new line of code.
  • -

-Moreover, -with a count different from zero, -the hook is called also after every count instructions. - - -

-When called without arguments, -debug.sethook turns off the hook. - - -

-When the hook is called, its first parameter is a string -describing the event that has triggered its call: -"call", "tail call", "return", -"line", and "count". -For line events, -the hook also gets the new line number as its second parameter. -Inside a hook, -you can call getinfo with level 2 to get more information about -the running function. -(Level 0 is the getinfo function, -and level 1 is the hook function.) - - - - -

-


debug.setlocal ([thread,] level, local, value)

- - -

-This function assigns the value value to the local variable -with index local of the function at level level of the stack. -The function returns fail if there is no local -variable with the given index, -and raises an error when called with a level out of range. -(You can call getinfo to check whether the level is valid.) -Otherwise, it returns the name of the local variable. - - -

-See debug.getlocal for more information about -variable indices and names. - - - - -

-


debug.setmetatable (value, table)

- - -

-Sets the metatable for the given value to the given table -(which can be nil). -Returns value. - - - - -

-


debug.setupvalue (f, up, value)

- - -

-This function assigns the value value to the upvalue -with index up of the function f. -The function returns fail if there is no upvalue -with the given index. -Otherwise, it returns the name of the upvalue. - - -

-See debug.getupvalue for more information about upvalues. - - - - -

-


debug.setuservalue (udata, value, n)

- - -

-Sets the given value as -the n-th user value associated to the given udata. -udata must be a full userdata. - - -

-Returns udata, -or fail if the userdata does not have that value. - - - - -

-


debug.traceback ([thread,] [message [, level]])

- - -

-If message is present but is neither a string nor nil, -this function returns message without further processing. -Otherwise, -it returns a string with a traceback of the call stack. -The optional message string is appended -at the beginning of the traceback. -An optional level number tells at which level -to start the traceback -(default is 1, the function calling traceback). - - - - -

-


debug.upvalueid (f, n)

- - -

-Returns a unique identifier (as a light userdata) -for the upvalue numbered n -from the given function. - - -

-These unique identifiers allow a program to check whether different -closures share upvalues. -Lua closures that share an upvalue -(that is, that access a same external local variable) -will return identical ids for those upvalue indices. - - - - -

-


debug.upvaluejoin (f1, n1, f2, n2)

- - -

-Make the n1-th upvalue of the Lua closure f1 -refer to the n2-th upvalue of the Lua closure f2. - - - - - - - -

7 – Lua Standalone

- -

-Although Lua has been designed as an extension language, -to be embedded in a host C program, -it is also frequently used as a standalone language. -An interpreter for Lua as a standalone language, -called simply lua, -is provided with the standard distribution. -The standalone interpreter includes -all standard libraries. -Its usage is: - -

-     lua [options] [script [args]]
-

-The options are: - -

    -
  • -e stat: execute string stat;
  • -
  • -i: enter interactive mode after running script;
  • -
  • -l mod: "require" mod and assign the - result to global mod;
  • -
  • -v: print version information;
  • -
  • -E: ignore environment variables;
  • -
  • -W: turn warnings on;
  • -
  • --: stop handling options;
  • -
  • -: execute stdin as a file and stop handling options.
  • -

-After handling its options, lua runs the given script. -When called without arguments, -lua behaves as lua -v -i -when the standard input (stdin) is a terminal, -and as lua - otherwise. - - -

-When called without the option -E, -the interpreter checks for an environment variable LUA_INIT_5_4 -(or LUA_INIT if the versioned name is not defined) -before running any argument. -If the variable content has the format @filename, -then lua executes the file. -Otherwise, lua executes the string itself. - - -

-When called with the option -E, -Lua does not consult any environment variables. -In particular, -the values of package.path and package.cpath -are set with the default paths defined in luaconf.h. - - -

-The options -e, -l, and -W are handled in -the order they appear. -For instance, an invocation like - -

-     $ lua -e 'a=1' -llib1 script.lua
-

-will first set a to 1, then require the library lib1, -and finally run the file script.lua with no arguments. -(Here $ is the shell prompt. Your prompt may be different.) - - -

-Before running any code, -lua collects all command-line arguments -in a global table called arg. -The script name goes to index 0, -the first argument after the script name goes to index 1, -and so on. -Any arguments before the script name -(that is, the interpreter name plus its options) -go to negative indices. -For instance, in the call - -

-     $ lua -la b.lua t1 t2
-

-the table is like this: - -

-     arg = { [-2] = "lua", [-1] = "-la",
-             [0] = "b.lua",
-             [1] = "t1", [2] = "t2" }
-

-If there is no script in the call, -the interpreter name goes to index 0, -followed by the other arguments. -For instance, the call - -

-     $ lua -e "print(arg[1])"
-

-will print "-e". -If there is a script, -the script is called with arguments -arg[1], ···, arg[#arg]. -Like all chunks in Lua, -the script is compiled as a vararg function. - - -

-In interactive mode, -Lua repeatedly prompts and waits for a line. -After reading a line, -Lua first try to interpret the line as an expression. -If it succeeds, it prints its value. -Otherwise, it interprets the line as a statement. -If you write an incomplete statement, -the interpreter waits for its completion -by issuing a different prompt. - - -

-If the global variable _PROMPT contains a string, -then its value is used as the prompt. -Similarly, if the global variable _PROMPT2 contains a string, -its value is used as the secondary prompt -(issued during incomplete statements). - - -

-In case of unprotected errors in the script, -the interpreter reports the error to the standard error stream. -If the error object is not a string but -has a metamethod __tostring, -the interpreter calls this metamethod to produce the final message. -Otherwise, the interpreter converts the error object to a string -and adds a stack traceback to it. -When warnings are on, -they are simply printed in the standard error output. - - -

-When finishing normally, -the interpreter closes its main Lua state -(see lua_close). -The script can avoid this step by -calling os.exit to terminate. - - -

-To allow the use of Lua as a -script interpreter in Unix systems, -Lua skips the first line of a file chunk if it starts with #. -Therefore, Lua scripts can be made into executable programs -by using chmod +x and the #! form, -as in - -

-     #!/usr/local/bin/lua
-

-Of course, -the location of the Lua interpreter may be different in your machine. -If lua is in your PATH, -then - -

-     #!/usr/bin/env lua
-

-is a more portable solution. - - - -

8 – Incompatibilities with the Previous Version

- - - -

-Here we list the incompatibilities that you may find when moving a program -from Lua 5.3 to Lua 5.4. - - -

-You can avoid some incompatibilities by compiling Lua with -appropriate options (see file luaconf.h). -However, -all these compatibility options will be removed in the future. -More often than not, -compatibility issues arise when these compatibility options -are removed. -So, whenever you have the chance, -you should try to test your code with a version of Lua compiled -with all compatibility options turned off. -That will ease transitions to newer versions of Lua. - - -

-Lua versions can always change the C API in ways that -do not imply source-code changes in a program, -such as the numeric values for constants -or the implementation of functions as macros. -Therefore, -you should never assume that binaries are compatible between -different Lua versions. -Always recompile clients of the Lua API when -using a new version. - - -

-Similarly, Lua versions can always change the internal representation -of precompiled chunks; -precompiled chunks are not compatible between different Lua versions. - - -

-The standard paths in the official distribution may -change between versions. - - - - - -

8.1 – Incompatibilities in the Language

-
    - -
  • -The coercion of strings to numbers in -arithmetic and bitwise operations -has been removed from the core language. -The string library does a similar job -for arithmetic (but not for bitwise) operations -using the string metamethods. -However, unlike in previous versions, -the new implementation preserves the implicit type of the numeral -in the string. -For instance, the result of "1" + "2" now is an integer, -not a float. -
  • - -
  • -Literal decimal integer constants that overflow are read as floats, -instead of wrapping around. -You can use hexadecimal notation for such constants if you -want the old behavior -(reading them as integers with wrap around). -
  • - -
  • -The use of the __lt metamethod to emulate __le -has been removed. -When needed, this metamethod must be explicitly defined. -
  • - -
  • -The semantics of the numerical for loop -over integers changed in some details. -In particular, the control variable never wraps around. -
  • - -
  • -A label for a goto cannot be declared where a label with the same -name is visible, even if this other label is declared in an enclosing -block. -
  • - -
  • -When finalizing an object, -Lua does not ignore __gc metamethods that are not functions. -Any value will be called, if present. -(Non-callable values will generate a warning, -like any other error when calling a finalizer.) -
  • - -
- - - - -

8.2 – Incompatibilities in the Libraries

-
    - -
  • -The function print does not call tostring -to format its arguments; -instead, it has this functionality hardwired. -You should use __tostring to modify how values are printed. -
  • - -
  • -The pseudo-random number generator used by the function math.random -now starts with a somewhat random seed. -Moreover, it uses a different algorithm. -
  • - -
  • -By default, the decoding functions in the utf8 library -do not accept surrogates as valid code points. -An extra parameter in these functions makes them more permissive. -
  • - -
  • -The options "setpause" and "setstepmul" -of the function collectgarbage are deprecated. -You should use the new option "incremental" to set them. -
  • - -
  • -The function io.lines now returns four values, -instead of just one. -That can be a problem when it is used as the sole -argument to another function that has optional parameters, -such as in load(io.lines(filename, "L")). -To fix that issue, -you can wrap the call into parentheses, -to adjust its number of results to one. -
  • - -
- - - - -

8.3 – Incompatibilities in the API

- - -
    - -
  • -Full userdata now has an arbitrary number of associated user values. -Therefore, the functions lua_newuserdata, -lua_setuservalue, and lua_getuservalue were -replaced by lua_newuserdatauv, -lua_setiuservalue, and lua_getiuservalue, -which have an extra argument. - - -

    -For compatibility, the old names still work as macros assuming -one single user value. -Note, however, that userdata with zero user values -are more efficient memory-wise. -

  • - -
  • -The function lua_resume has an extra parameter. -This out parameter returns the number of values on -the top of the stack that were yielded or returned by the coroutine. -(In previous versions, -those values were the entire stack.) -
  • - -
  • -The function lua_version returns the version number, -instead of an address of the version number. -The Lua core should work correctly with libraries using their -own static copies of the same core, -so there is no need to check whether they are using the same -address space. -
  • - -
  • -The constant LUA_ERRGCMM was removed. -Errors in finalizers are never propagated; -instead, they generate a warning. -
  • - -
  • -The options LUA_GCSETPAUSE and LUA_GCSETSTEPMUL -of the function lua_gc are deprecated. -You should use the new option LUA_GCINC to set them. -
  • - -
- - - - -

9 – The Complete Syntax of Lua

- -

-Here is the complete syntax of Lua in extended BNF. -As usual in extended BNF, -{A} means 0 or more As, -and [A] means an optional A. -(For operator precedences, see §3.4.8; -for a description of the terminals -Name, Numeral, -and LiteralString, see §3.1.) - - - - -

-
-	chunk ::= block
-
-	block ::= {stat} [retstat]
-
-	stat ::=  ‘;’ | 
-		 varlist ‘=’ explist | 
-		 functioncall | 
-		 label | 
-		 break | 
-		 goto Name | 
-		 do block end | 
-		 while exp do block end | 
-		 repeat block until exp | 
-		 if exp then block {elseif exp then block} [else block] end | 
-		 for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end | 
-		 for namelist in explist do block end | 
-		 function funcname funcbody | 
-		 local function Name funcbody | 
-		 local attnamelist [‘=’ explist] 
-
-	attnamelist ::=  Name attrib {‘,’ Name attrib}
-
-	attrib ::= [‘<’ Name ‘>’]
-
-	retstat ::= return [explist] [‘;’]
-
-	label ::= ‘::’ Name ‘::’
-
-	funcname ::= Name {‘.’ Name} [‘:’ Name]
-
-	varlist ::= var {‘,’ var}
-
-	var ::=  Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name 
-
-	namelist ::= Name {‘,’ Name}
-
-	explist ::= exp {‘,’ exp}
-
-	exp ::=  nil | false | true | Numeral | LiteralString | ‘...’ | functiondef | 
-		 prefixexp | tableconstructor | exp binop exp | unop exp 
-
-	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
-
-	functioncall ::=  prefixexp args | prefixexp ‘:’ Name args 
-
-	args ::=  ‘(’ [explist] ‘)’ | tableconstructor | LiteralString 
-
-	functiondef ::= function funcbody
-
-	funcbody ::= ‘(’ [parlist] ‘)’ block end
-
-	parlist ::= namelist [‘,’ ‘...’] | ‘...’
-
-	tableconstructor ::= ‘{’ [fieldlist] ‘}’
-
-	fieldlist ::= field {fieldsep field} [fieldsep]
-
-	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
-
-	fieldsep ::= ‘,’ | ‘;’
-
-	binop ::=  ‘+’ | ‘-’ | ‘*’ | ‘/’ | ‘//’ | ‘^’ | ‘%’ | 
-		 ‘&’ | ‘~’ | ‘|’ | ‘>>’ | ‘<<’ | ‘..’ | 
-		 ‘<’ | ‘<=’ | ‘>’ | ‘>=’ | ‘==’ | ‘~=’ | 
-		 and | or
-
-	unop ::= ‘-’ | not | ‘#’ | ‘~’
-
-
- -

- - - - - - - -

- - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/osi-certified-72x60.png b/LuaBridge3/Tests/Lua/Lua.5.4.4/doc/osi-certified-72x60.png deleted file mode 100644 index 07df5f6ee7a7a8b2108025dcd815f73f145a83af..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3774 zcmV;v4ngsWP)$kl5 zqcT7g&?zu8?ezWYz4zUB-|zR9d+&Qy2xAN{qY(ew0A7^*gV^7jytKqPFV3{hZfovn zs%x!l>(m&Gdb8C+5XeR7>h0kj=o=X3A39;2KLYfEMt>p1YMW~dt`rpAC{lN~P>5pq zH1L4nAdCT17}*hN=LnEsvMl=5Ij^QArAa&_V~zoht-Ei~)E~(Ivhe0#jik{t$isEK znCH$TxCB8EKmcF>3@pRaHpbR%Gqm*dsZA4H{j(NjZFp^iNFW+RBx6R*X19J*`0XG5 z^Y>cR=^Hi9#ovYGlbFSr#Q*^PgCGC^gb*SC5TcBfzQLe-r2m!Quik&_g9XzTj0qSR zD`FkG_RYWDa^+#UUxL&t+!K+&(ion@Fd`5l5p7{Qsva9vegC|4^NzJUMvn)^gqWsF zvu^j=%FfCVg^cgbXDRl1DE$lsfe;BjjmFmRHER~E-MeWoNsyyNHCpG%Y}igd_(Md;&9La8_B075NDRX9gTD zIHY`}9E~aGi9Kk1@P~rmPna=*=gz~UTdTpsQmjX)J23%v9NliQS)8`xJh6Qz_nE~e z&tP|!dcJdo;JMNa3>afSx$lko8>fp-I}OiCVz(dOF1u6e8$IrsSP?=5mp~lkaFqm? zAUMxRq%ecIu3WE)Uf=%p8g z+RSY?G=VO%wAfdICj?Uzb+5jr{8m|)i#{M}JjaDIoXf#1=DYLwX;1EW&sijPvm6EkBGuOx6r~lKv`g`yH?)|&PRUr$5Ibw2HBM7C74XvE@gaPjN+@;j$J)AgYhnT-U5m+wj|Wz8K630AfO8PUoGD^^Mcq zY9C<~%wUm^u%ox5P21)KNN0$(v^OI$A~?iwsS_fRu1+`EH|CRdpA4zsk8Z#|?x@^vVEAL+2JxH%&^{JUU%B=?EU7`Ar*Q|JvqPofcBt765(*f5JI$>=3{<%K)4ei zogo$)5XP}_X$y^pIYyWTt}EAnhTq}u4sAdBvC(WC{I#x4^>$vCvQ0UDs^18sAQG9o zEaP0qjrSSv1W0FyO%9&y$@em~n@8}}EXBG6x%ew49J_q%l@As_XnNpi|MTTPr~ca_ zW%uon6dBKL*pvzYFvf<~p6K8hK9BDNNN0$7xp^hWC3n^7FoQ?P(=m(6!Pj&S2f1fqH=`(w)KcPl5aEi2}~4hF*f*g}vaS-=c7v>N8c z{yNM*%+azq=@prWtgpi~^3?^AsJqS(>=pb=6PrGH#=O{Hcho$_F#MtsK$$3e2fZvg zy}!-V%`+uFMOW87LIgu3vKuMgqwY0}*Sd;aokQp(F#-{}Ss(Iy1iekY1ZQX?1WEL? z7=zq`lH-#Hw=bHRio3yPun%`c5rI1Hb|wTSWTs|12Mg#QkkwTmy zAYul0H*_b(BnkP#!R_&p@d54uz0JKthGv3C^fdKS%~alookE`QX@%#MQN2=SFWrOha7Ij7ImStNaWsy~? zsylUeT02_-z-G4s0L!v=+Wx|cxr$tmY&$a1by8z#6HBp!*9{@mU9XQ0h@L%V_R}4g z&s#2{MCOj4`5ux-SUautC5@{U895o-biKMWWoQ09{|jx8wz}@_(ep%Yk4{90C#s6-sa}fU5{}m>#>VtE_b#5bn8O+3k{&6GoEkB;yGie;A_5Uy zqPN*tU()pE+_&~``5XX({el-xT_}%`%fsc>_0@m5{+FhXru>rpyLESe31R>cK^FFrCm+#WL$-D{Z3*9>Lg{wi}xEYn_`@Hy`-d z1N}kIY%@Eu&Bpe|Rr6N;%Yk>6&RI$lgpIO26BYT%C!dU-o4bqqQpGY?p6lPru6Hzc z@WuSDI^BYaDH*>R)~)$V1J0Edn4r(9vo>E<2XjOJr2*G124;t^U+p{iUnZN5oapCpCk(F}}<#3ZZli!Nk z^UWT;Q9qm-i`i$kJS}5P%puBJ<&krTO;*#$Y7d$o96EbQ{aF1XFpTj}wf}eI|IOba z%w}_CWu?JjkV>U-ad9L$@Mu$CU;pUQBZgt5QmI@n=W@9K(A(SF-rnxzy|_!5ekKqCQTad`sa|&&Q6jfy}iAEst?|mH*emIjg9SB zRVWlHl?r3bvh2qnf6V6(+>4TulB%kzFveeh{k1?K*t&J=m>dk9P8SjqQdn4sF;*&- z(b3VFnVH$y*$Rb%rs zefJ#z#KpyZ_0?C$jvY%)O?7a?7#}%u1OT>d*)keF*REZ=c=4j6tkr5MilS*cB_$;< zFArmEv)Oby-7}4>TD9uE_ulKT4s6Bp@^Y0*rBEo&o;?cy8#Zi^%jH+DTv4f1SFc_L zfc5LwXJ=;vKt@K!?%liR&!6Almmq$2R@G|tg$oyGnpP+jQBhF<(9qCOR8%AuiBtJCSc zyu1LQw6wIQre^Zw$^E0N)#}R1%J}$rkw`Qc#z0A{)dIkjDN`I(PfyS2=x9f~R4N64 zPe1*1=gytQ#l=RWao4V0bLY-=?Bpl*dQDA@LZMJ9l{Gar$;rvzfB$`Tb#+==T0=ua zSy@?1N{UXWyL9Q&#*G`Zv$GE#JXljxBauj2T3VD!rO9N<%F3#*uP-Sn(P%W=w{Jgx z{(NC!VNOmC0OaN6ZQHg@tJQw^;fGtdZUulVSFX&NGv~~iGoO9-nNq0~2n78w23E{L zmth7T3|W>10ISuSm6cUgRCMXmr5!tV0D!x@`?6)rcI?<8lgZ#IIehqVOiYYpi@x#3 z8xau^+1c4ER;th&( zVHk--A`l3|!os9dsYatANm8TH96x@%qM{-&FmUtc&2qVX-MV%A_U(J~%{TY#*<&ym zX3Ur|c$No?u%e>k#EBDaZEY7XUVLH`0zh|n zw_~XRz;RH!y1MS)zn_X$Km70mNs@ZKo~G$z$BuD09F}FpVzEY}F&d2ug#rLPJUpgPpKh}a^y$-i zJl@%}XHT6vRaaNHckf=MQYn>6Fk&*D<+ja0B z5C{a#&CQN-V`HPyXe3EeAP~gH#>U3RayT5ZSd1}tbaaSNDAZ^)j%n&QHMoE=7KubA zlWEeVNpiV7Dk=&gzM|0Dz(>0HA5Q-_F}_znz(xxqbU~E|+`a#EH|V zPjA|^DJLg~rs?+f_6rv-T)upnAP7fChoq;cFJHcV=gyt)zWXjs(+gZ<%kMDTlOd1+TFW%&z(D`)oKF*0@Bmd zLqkIy?RvewprGK+ojWv5%Ve?@D^>&r1p$CcrMhuv}x1&joiO~|IC>)G) - - -Lua 5.4 readme - - - - - - - -

-Lua -Welcome to Lua 5.4 -

- - - -

About Lua

-

-Lua is a powerful, efficient, lightweight, embeddable scripting language -developed by a -team -at -PUC-Rio, -the Pontifical Catholic University of Rio de Janeiro in Brazil. -Lua is -free software -used in -many products and projects -around the world. - -

-Lua's -official web site -provides complete information -about Lua, -including -an -executive summary -and -updated -documentation, -especially the -reference manual, -which may differ slightly from the -local copy -distributed in this package. - -

Installing Lua

-

-Lua is distributed in -source -form. -You need to build it before using it. -Building Lua should be straightforward -because -Lua is implemented in pure ANSI C and compiles unmodified in all known -platforms that have an ANSI C compiler. -Lua also compiles unmodified as C++. -The instructions given below for building Lua are for Unix-like platforms, -such as Linux and Mac OS X. -See also -instructions for other systems -and -customization options. - -

-If you don't have the time or the inclination to compile Lua yourself, -get a binary from -LuaBinaries. -Try also -LuaDist, -a multi-platform distribution of Lua that includes batteries. - -

Building Lua

-

-In most common Unix-like platforms, simply do "make". -Here are the details. - -

    -
  1. -Open a terminal window and move to -the top-level directory, which is named lua-5.4.4. -The Makefile there controls both the build process and the installation process. -

    -

  2. - Do "make". The Makefile will guess your platform and build Lua for it. -

    -

  3. - If the guess failed, do "make help" and see if your platform is listed. - The platforms currently supported are: -

    -

    - guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris -

    -

    - If your platform is listed, just do "make xxx", where xxx - is your platform name. -

    - If your platform is not listed, try the closest one or posix, generic, - c89, in this order. -

    -

  4. -The compilation takes only a few moments -and produces three files in the src directory: -lua (the interpreter), -luac (the compiler), -and liblua.a (the library). -

    -

  5. - To check that Lua has been built correctly, do "make test" - after building Lua. This will run the interpreter and print its version. -
-

-If you're running Linux, try "make linux-readline" to build the interactive Lua interpreter with handy line-editing and history capabilities. -If you get compilation errors, -make sure you have installed the readline development package -(which is probably named libreadline-dev or readline-devel). -If you get link errors after that, -then try "make linux-readline MYLIBS=-ltermcap". - -

Installing Lua

-

- Once you have built Lua, you may want to install it in an official - place in your system. In this case, do "make install". The official - place and the way to install files are defined in the Makefile. You'll - probably need the right permissions to install files, and so may need to do "sudo make install". - -

- To build and install Lua in one step, do "make all install", - or "make xxx install", - where xxx is your platform name. - -

- To install Lua locally after building it, do "make local". - This will create a directory install with subdirectories - bin, include, lib, man, share, - and install Lua as listed below. - - To install Lua locally, but in some other directory, do - "make install INSTALL_TOP=xxx", where xxx is your chosen directory. - The installation starts in the src and doc directories, - so take care if INSTALL_TOP is not an absolute path. - -

-
- bin: -
- lua luac -
- include: -
- lua.h luaconf.h lualib.h lauxlib.h lua.hpp -
- lib: -
- liblua.a -
- man/man1: -
- lua.1 luac.1 -
- -

- These are the only directories you need for development. - If you only want to run Lua programs, - you only need the files in bin and man. - The files in include and lib are needed for - embedding Lua in C or C++ programs. - -

Customization

-

- Three kinds of things can be customized by editing a file: -

    -
  • Where and how to install Lua — edit Makefile. -
  • How to build Lua — edit src/Makefile. -
  • Lua features — edit src/luaconf.h. -
- -

- You don't actually need to edit the Makefiles because you may set the - relevant variables in the command line when invoking make. - Nevertheless, it's probably best to edit and save the Makefiles to - record the changes you've made. - -

- On the other hand, if you need to customize some Lua features, you'll need - to edit src/luaconf.h before building and installing Lua. - The edited file will be the one installed, and - it will be used by any Lua clients that you build, to ensure consistency. - Further customization is available to experts by editing the Lua sources. - -

Building Lua on other systems

-

- If you're not using the usual Unix tools, then the instructions for - building Lua depend on the compiler you use. You'll need to create - projects (or whatever your compiler uses) for building the library, - the interpreter, and the compiler, as follows: - -

-
-library: -
-lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c -lauxlib.c lbaselib.c lcorolib.c ldblib.c liolib.c lmathlib.c loadlib.c loslib.c lstrlib.c ltablib.c lutf8lib.c linit.c -
-interpreter: -
- library, lua.c -
-compiler: -
- library, luac.c -
- -

- To use Lua as a library in your own programs, you'll need to know how to - create and use libraries with your compiler. Moreover, to dynamically load - C libraries for Lua, you'll need to know how to create dynamic libraries - and you'll need to make sure that the Lua API functions are accessible to - those dynamic libraries — but don't link the Lua library - into each dynamic library. For Unix, we recommend that the Lua library - be linked statically into the host program and its symbols exported for - dynamic linking; src/Makefile does this for the Lua interpreter. - For Windows, we recommend that the Lua library be a DLL. - In all cases, the compiler luac should be linked statically. - -

- As mentioned above, you may edit src/luaconf.h to customize - some features before building Lua. - -

Changes since Lua 5.3

-

-Here are the main changes introduced in Lua 5.4. -The -reference manual -lists the -incompatibilities that had to be introduced. - -

Main changes

-
    -
  • new generational mode for garbage collection -
  • to-be-closed variables -
  • const variables -
  • userdata can have multiple user values -
  • new implementation for math.random -
  • warning system -
  • debug information about function arguments and returns -
  • new semantics for the integer 'for' loop -
  • optional 'init' argument to 'string.gmatch' -
  • new functions 'lua_resetthread' and 'coroutine.close' -
  • string-to-number coercions moved to the string library -
  • allocation function allowed to fail when shrinking a memory block -
  • new format '%p' in 'string.format' -
  • utf8 library accepts codepoints up to 2^31 -
- -

License

-

- -[osi certified] - -Lua is free software distributed under the terms of the -MIT license -reproduced below; -it may be used for any purpose, including commercial purposes, -at absolutely no cost without having to ask us. - -The only requirement is that if you do use Lua, -then you should give us credit by including the appropriate copyright notice somewhere in your product or its documentation. - -For details, see -this. - -

-Copyright © 1994–2022 Lua.org, PUC-Rio. - -

-Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -

-The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -

-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -

-

- -

- - - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.c deleted file mode 100644 index 5ee6579..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.c +++ /dev/null @@ -1,1466 +0,0 @@ -/* -** $Id: lapi.c $ -** Lua API -** See Copyright Notice in lua.h -*/ - -#define lapi_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" - - - -const char lua_ident[] = - "$LuaVersion: " LUA_COPYRIGHT " $" - "$LuaAuthors: " LUA_AUTHORS " $"; - - - -/* -** Test for a valid index (one that is not the 'nilvalue'). -** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. -** However, it covers the most common cases in a faster way. -*/ -#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) - - -/* test for pseudo index */ -#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) - -/* test for upvalue */ -#define isupvalue(i) ((i) < LUA_REGISTRYINDEX) - - -/* -** Convert an acceptable index to a pointer to its respective value. -** Non-valid indices return the special nil value 'G(L)->nilvalue'. -*/ -static TValue *index2value (lua_State *L, int idx) { - CallInfo *ci = L->ci; - if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return &G(L)->nilvalue; - else return s2v(o); - } - else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return s2v(L->top + idx); - } - else if (idx == LUA_REGISTRYINDEX) - return &G(L)->l_registry; - else { /* upvalues */ - idx = LUA_REGISTRYINDEX - idx; - api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttisCclosure(s2v(ci->func))) { /* C closure? */ - CClosure *func = clCvalue(s2v(ci->func)); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] - : &G(L)->nilvalue; - } - else { /* light C function or Lua function (through a hook)?) */ - api_check(L, ttislcf(s2v(ci->func)), "caller not a C function"); - return &G(L)->nilvalue; /* no upvalues */ - } - } -} - - - -/* -** Convert a valid actual index (not a pseudo-index) to its address. -*/ -l_sinline StkId index2stack (lua_State *L, int idx) { - CallInfo *ci = L->ci; - if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, o < L->top, "invalid index"); - return o; - } - else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - api_check(L, !ispseudo(idx), "invalid index"); - return L->top + idx; - } -} - - -LUA_API int lua_checkstack (lua_State *L, int n) { - int res; - CallInfo *ci; - lua_lock(L); - ci = L->ci; - api_check(L, n >= 0, "negative 'n'"); - if (L->stack_last - L->top > n) /* stack large enough? */ - res = 1; /* yes; check is OK */ - else { /* no; need to grow stack */ - int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ - res = 0; /* no */ - else /* try to grow stack */ - res = luaD_growstack(L, n, 0); - } - if (res && ci->top < L->top + n) - ci->top = L->top + n; /* adjust frame top */ - lua_unlock(L); - return res; -} - - -LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { - int i; - if (from == to) return; - lua_lock(to); - api_checknelems(from, n); - api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "stack overflow"); - from->top -= n; - for (i = 0; i < n; i++) { - setobjs2s(to, to->top, from->top + i); - to->top++; /* stack already checked by previous 'api_check' */ - } - lua_unlock(to); -} - - -LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { - lua_CFunction old; - lua_lock(L); - old = G(L)->panic; - G(L)->panic = panicf; - lua_unlock(L); - return old; -} - - -LUA_API lua_Number lua_version (lua_State *L) { - UNUSED(L); - return LUA_VERSION_NUM; -} - - - -/* -** basic stack manipulation -*/ - - -/* -** convert an acceptable stack index into an absolute index -*/ -LUA_API int lua_absindex (lua_State *L, int idx) { - return (idx > 0 || ispseudo(idx)) - ? idx - : cast_int(L->top - L->ci->func) + idx; -} - - -LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); -} - - -LUA_API void lua_settop (lua_State *L, int idx) { - CallInfo *ci; - StkId func, newtop; - ptrdiff_t diff; /* difference for new top */ - lua_lock(L); - ci = L->ci; - func = ci->func; - if (idx >= 0) { - api_check(L, idx <= ci->top - (func + 1), "new top too large"); - diff = ((func + 1) + idx) - L->top; - for (; diff > 0; diff--) - setnilvalue(s2v(L->top++)); /* clear new slots */ - } - else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - diff = idx + 1; /* will "subtract" index (as it is negative) */ - } - api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot"); - newtop = L->top + diff; - if (diff < 0 && L->tbclist >= newtop) { - lua_assert(hastocloseCfunc(ci->nresults)); - luaF_close(L, newtop, CLOSEKTOP, 0); - } - L->top = newtop; /* correct top only after closing any upvalue */ - lua_unlock(L); -} - - -LUA_API void lua_closeslot (lua_State *L, int idx) { - StkId level; - lua_lock(L); - level = index2stack(L, idx); - api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, - "no variable to close at given level"); - luaF_close(L, level, CLOSEKTOP, 0); - level = index2stack(L, idx); /* stack may be moved */ - setnilvalue(s2v(level)); - lua_unlock(L); -} - - -/* -** Reverse the stack segment from 'from' to 'to' -** (auxiliary to 'lua_rotate') -** Note that we move(copy) only the value inside the stack. -** (We do not move additional fields that may exist.) -*/ -l_sinline void reverse (lua_State *L, StkId from, StkId to) { - for (; from < to; from++, to--) { - TValue temp; - setobj(L, &temp, s2v(from)); - setobjs2s(L, from, to); - setobj2s(L, to, &temp); - } -} - - -/* -** Let x = AB, where A is a prefix of length 'n'. Then, -** rotate x n == BA. But BA == (A^r . B^r)^r. -*/ -LUA_API void lua_rotate (lua_State *L, int idx, int n) { - StkId p, t, m; - lua_lock(L); - t = L->top - 1; /* end of stack segment being rotated */ - p = index2stack(L, idx); /* start of segment */ - api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); - m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ - reverse(L, p, m); /* reverse the prefix with length 'n' */ - reverse(L, m + 1, t); /* reverse the suffix */ - reverse(L, p, t); /* reverse the entire segment */ - lua_unlock(L); -} - - -LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { - TValue *fr, *to; - lua_lock(L); - fr = index2value(L, fromidx); - to = index2value(L, toidx); - api_check(L, isvalid(L, to), "invalid index"); - setobj(L, to, fr); - if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); - /* LUA_REGISTRYINDEX does not need gc barrier - (collector revisits it before finishing collection) */ - lua_unlock(L); -} - - -LUA_API void lua_pushvalue (lua_State *L, int idx) { - lua_lock(L); - setobj2s(L, L->top, index2value(L, idx)); - api_incr_top(L); - lua_unlock(L); -} - - - -/* -** access functions (stack -> C) -*/ - - -LUA_API int lua_type (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return (isvalid(L, o) ? ttype(o) : LUA_TNONE); -} - - -LUA_API const char *lua_typename (lua_State *L, int t) { - UNUSED(L); - api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); - return ttypename(t); -} - - -LUA_API int lua_iscfunction (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return (ttislcf(o) || (ttisCclosure(o))); -} - - -LUA_API int lua_isinteger (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return ttisinteger(o); -} - - -LUA_API int lua_isnumber (lua_State *L, int idx) { - lua_Number n; - const TValue *o = index2value(L, idx); - return tonumber(o, &n); -} - - -LUA_API int lua_isstring (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return (ttisstring(o) || cvt2str(o)); -} - - -LUA_API int lua_isuserdata (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return (ttisfulluserdata(o) || ttislightuserdata(o)); -} - - -LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { - const TValue *o1 = index2value(L, index1); - const TValue *o2 = index2value(L, index2); - return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; -} - - -LUA_API void lua_arith (lua_State *L, int op) { - lua_lock(L); - if (op != LUA_OPUNM && op != LUA_OPBNOT) - api_checknelems(L, 2); /* all other operations expect two operands */ - else { /* for unary operations, add fake 2nd operand */ - api_checknelems(L, 1); - setobjs2s(L, L->top, L->top - 1); - api_incr_top(L); - } - /* first operand at top - 2, second at top - 1; result go to top - 2 */ - luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); - L->top--; /* remove second operand */ - lua_unlock(L); -} - - -LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { - const TValue *o1; - const TValue *o2; - int i = 0; - lua_lock(L); /* may call tag method */ - o1 = index2value(L, index1); - o2 = index2value(L, index2); - if (isvalid(L, o1) && isvalid(L, o2)) { - switch (op) { - case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; - case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; - case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; - default: api_check(L, 0, "invalid option"); - } - } - lua_unlock(L); - return i; -} - - -LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, s2v(L->top)); - if (sz != 0) - api_incr_top(L); - return sz; -} - - -LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { - lua_Number n = 0; - const TValue *o = index2value(L, idx); - int isnum = tonumber(o, &n); - if (pisnum) - *pisnum = isnum; - return n; -} - - -LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { - lua_Integer res = 0; - const TValue *o = index2value(L, idx); - int isnum = tointeger(o, &res); - if (pisnum) - *pisnum = isnum; - return res; -} - - -LUA_API int lua_toboolean (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return !l_isfalse(o); -} - - -LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - TValue *o; - lua_lock(L); - o = index2value(L, idx); - if (!ttisstring(o)) { - if (!cvt2str(o)) { /* not convertible? */ - if (len != NULL) *len = 0; - lua_unlock(L); - return NULL; - } - luaO_tostring(L, o); - luaC_checkGC(L); - o = index2value(L, idx); /* previous call may reallocate the stack */ - } - if (len != NULL) - *len = vslen(o); - lua_unlock(L); - return svalue(o); -} - - -LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - switch (ttypetag(o)) { - case LUA_VSHRSTR: return tsvalue(o)->shrlen; - case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; - case LUA_VUSERDATA: return uvalue(o)->len; - case LUA_VTABLE: return luaH_getn(hvalue(o)); - default: return 0; - } -} - - -LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - if (ttislcf(o)) return fvalue(o); - else if (ttisCclosure(o)) - return clCvalue(o)->f; - else return NULL; /* not a C function */ -} - - -l_sinline void *touserdata (const TValue *o) { - switch (ttype(o)) { - case LUA_TUSERDATA: return getudatamem(uvalue(o)); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; - } -} - - -LUA_API void *lua_touserdata (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return touserdata(o); -} - - -LUA_API lua_State *lua_tothread (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - return (!ttisthread(o)) ? NULL : thvalue(o); -} - - -/* -** Returns a pointer to the internal representation of an object. -** Note that ANSI C does not allow the conversion of a pointer to -** function to a 'void*', so the conversion here goes through -** a 'size_t'. (As the returned pointer is only informative, this -** conversion should not be a problem.) -*/ -LUA_API const void *lua_topointer (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); - switch (ttypetag(o)) { - case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); - case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: - return touserdata(o); - default: { - if (iscollectable(o)) - return gcvalue(o); - else - return NULL; - } - } -} - - - -/* -** push functions (C -> stack) -*/ - - -LUA_API void lua_pushnil (lua_State *L) { - lua_lock(L); - setnilvalue(s2v(L->top)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { - lua_lock(L); - setfltvalue(s2v(L->top), n); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { - lua_lock(L); - setivalue(s2v(L->top), n); - api_incr_top(L); - lua_unlock(L); -} - - -/* -** Pushes on the stack a string with given length. Avoid using 's' when -** 'len' == 0 (as 's' can be NULL in that case), due to later use of -** 'memcmp' and 'memcpy'. -*/ -LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { - TString *ts; - lua_lock(L); - ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); - setsvalue2s(L, L->top, ts); - api_incr_top(L); - luaC_checkGC(L); - lua_unlock(L); - return getstr(ts); -} - - -LUA_API const char *lua_pushstring (lua_State *L, const char *s) { - lua_lock(L); - if (s == NULL) - setnilvalue(s2v(L->top)); - else { - TString *ts; - ts = luaS_new(L, s); - setsvalue2s(L, L->top, ts); - s = getstr(ts); /* internal copy's address */ - } - api_incr_top(L); - luaC_checkGC(L); - lua_unlock(L); - return s; -} - - -LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, - va_list argp) { - const char *ret; - lua_lock(L); - ret = luaO_pushvfstring(L, fmt, argp); - luaC_checkGC(L); - lua_unlock(L); - return ret; -} - - -LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { - const char *ret; - va_list argp; - lua_lock(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - luaC_checkGC(L); - lua_unlock(L); - return ret; -} - - -LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { - lua_lock(L); - if (n == 0) { - setfvalue(s2v(L->top), fn); - api_incr_top(L); - } - else { - CClosure *cl; - api_checknelems(L, n); - api_check(L, n <= MAXUPVAL, "upvalue index too large"); - cl = luaF_newCclosure(L, n); - cl->f = fn; - L->top -= n; - while (n--) { - setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); - /* does not need barrier because closure is white */ - lua_assert(iswhite(cl)); - } - setclCvalue(L, s2v(L->top), cl); - api_incr_top(L); - luaC_checkGC(L); - } - lua_unlock(L); -} - - -LUA_API void lua_pushboolean (lua_State *L, int b) { - lua_lock(L); - if (b) - setbtvalue(s2v(L->top)); - else - setbfvalue(s2v(L->top)); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { - lua_lock(L); - setpvalue(s2v(L->top), p); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API int lua_pushthread (lua_State *L) { - lua_lock(L); - setthvalue(L, s2v(L->top), L); - api_incr_top(L); - lua_unlock(L); - return (G(L)->mainthread == L); -} - - - -/* -** get functions (Lua -> stack) -*/ - - -l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; - TString *str = luaS_new(L, k); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); - api_incr_top(L); - } - else { - setsvalue2s(L, L->top, str); - api_incr_top(L); - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); - } - lua_unlock(L); - return ttype(s2v(L->top - 1)); -} - - -/* -** Get the global table in the registry. Since all predefined -** indices in the registry were inserted right when the registry -** was created and never removed, they must always be in the array -** part of the registry. -*/ -#define getGtable(L) \ - (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) - - -LUA_API int lua_getglobal (lua_State *L, const char *name) { - const TValue *G; - lua_lock(L); - G = getGtable(L); - return auxgetstr(L, G, name); -} - - -LUA_API int lua_gettable (lua_State *L, int idx) { - const TValue *slot; - TValue *t; - lua_lock(L); - t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { - setobj2s(L, L->top - 1, slot); - } - else - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); - lua_unlock(L); - return ttype(s2v(L->top - 1)); -} - - -LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { - lua_lock(L); - return auxgetstr(L, index2value(L, idx), k); -} - - -LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { - TValue *t; - const TValue *slot; - lua_lock(L); - t = index2value(L, idx); - if (luaV_fastgeti(L, t, n, slot)) { - setobj2s(L, L->top, slot); - } - else { - TValue aux; - setivalue(&aux, n); - luaV_finishget(L, t, &aux, L->top, slot); - } - api_incr_top(L); - lua_unlock(L); - return ttype(s2v(L->top - 1)); -} - - -l_sinline int finishrawget (lua_State *L, const TValue *val) { - if (isempty(val)) /* avoid copying empty items to the stack */ - setnilvalue(s2v(L->top)); - else - setobj2s(L, L->top, val); - api_incr_top(L); - lua_unlock(L); - return ttype(s2v(L->top - 1)); -} - - -static Table *gettable (lua_State *L, int idx) { - TValue *t = index2value(L, idx); - api_check(L, ttistable(t), "table expected"); - return hvalue(t); -} - - -LUA_API int lua_rawget (lua_State *L, int idx) { - Table *t; - const TValue *val; - lua_lock(L); - api_checknelems(L, 1); - t = gettable(L, idx); - val = luaH_get(t, s2v(L->top - 1)); - L->top--; /* remove key */ - return finishrawget(L, val); -} - - -LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { - Table *t; - lua_lock(L); - t = gettable(L, idx); - return finishrawget(L, luaH_getint(t, n)); -} - - -LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { - Table *t; - TValue k; - lua_lock(L); - t = gettable(L, idx); - setpvalue(&k, cast_voidp(p)); - return finishrawget(L, luaH_get(t, &k)); -} - - -LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { - Table *t; - lua_lock(L); - t = luaH_new(L); - sethvalue2s(L, L->top, t); - api_incr_top(L); - if (narray > 0 || nrec > 0) - luaH_resize(L, t, narray, nrec); - luaC_checkGC(L); - lua_unlock(L); -} - - -LUA_API int lua_getmetatable (lua_State *L, int objindex) { - const TValue *obj; - Table *mt; - int res = 0; - lua_lock(L); - obj = index2value(L, objindex); - switch (ttype(obj)) { - case LUA_TTABLE: - mt = hvalue(obj)->metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(obj)->metatable; - break; - default: - mt = G(L)->mt[ttype(obj)]; - break; - } - if (mt != NULL) { - sethvalue2s(L, L->top, mt); - api_incr_top(L); - res = 1; - } - lua_unlock(L); - return res; -} - - -LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { - TValue *o; - int t; - lua_lock(L); - o = index2value(L, idx); - api_check(L, ttisfulluserdata(o), "full userdata expected"); - if (n <= 0 || n > uvalue(o)->nuvalue) { - setnilvalue(s2v(L->top)); - t = LUA_TNONE; - } - else { - setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); - t = ttype(s2v(L->top)); - } - api_incr_top(L); - lua_unlock(L); - return t; -} - - -/* -** set functions (stack -> Lua) -*/ - -/* -** t[k] = value at the top of the stack (where 'k' is a string) -*/ -static void auxsetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; - TString *str = luaS_new(L, k); - api_checknelems(L, 1); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - L->top--; /* pop value */ - } - else { - setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ - api_incr_top(L); - luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); - L->top -= 2; /* pop value and key */ - } - lua_unlock(L); /* lock done by caller */ -} - - -LUA_API void lua_setglobal (lua_State *L, const char *name) { - const TValue *G; - lua_lock(L); /* unlock done in 'auxsetstr' */ - G = getGtable(L); - auxsetstr(L, G, name); -} - - -LUA_API void lua_settable (lua_State *L, int idx) { - TValue *t; - const TValue *slot; - lua_lock(L); - api_checknelems(L, 2); - t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - } - else - luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); - L->top -= 2; /* pop index and value */ - lua_unlock(L); -} - - -LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { - lua_lock(L); /* unlock done in 'auxsetstr' */ - auxsetstr(L, index2value(L, idx), k); -} - - -LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { - TValue *t; - const TValue *slot; - lua_lock(L); - api_checknelems(L, 1); - t = index2value(L, idx); - if (luaV_fastgeti(L, t, n, slot)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - } - else { - TValue aux; - setivalue(&aux, n); - luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); - } - L->top--; /* pop value */ - lua_unlock(L); -} - - -static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { - Table *t; - lua_lock(L); - api_checknelems(L, n); - t = gettable(L, idx); - luaH_set(L, t, key, s2v(L->top - 1)); - invalidateTMcache(t); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top -= n; - lua_unlock(L); -} - - -LUA_API void lua_rawset (lua_State *L, int idx) { - aux_rawset(L, idx, s2v(L->top - 2), 2); -} - - -LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - TValue k; - setpvalue(&k, cast_voidp(p)); - aux_rawset(L, idx, &k, 1); -} - - -LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - Table *t; - lua_lock(L); - api_checknelems(L, 1); - t = gettable(L, idx); - luaH_setint(L, t, n, s2v(L->top - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top--; - lua_unlock(L); -} - - -LUA_API int lua_setmetatable (lua_State *L, int objindex) { - TValue *obj; - Table *mt; - lua_lock(L); - api_checknelems(L, 1); - obj = index2value(L, objindex); - if (ttisnil(s2v(L->top - 1))) - mt = NULL; - else { - api_check(L, ttistable(s2v(L->top - 1)), "table expected"); - mt = hvalue(s2v(L->top - 1)); - } - switch (ttype(obj)) { - case LUA_TTABLE: { - hvalue(obj)->metatable = mt; - if (mt) { - luaC_objbarrier(L, gcvalue(obj), mt); - luaC_checkfinalizer(L, gcvalue(obj), mt); - } - break; - } - case LUA_TUSERDATA: { - uvalue(obj)->metatable = mt; - if (mt) { - luaC_objbarrier(L, uvalue(obj), mt); - luaC_checkfinalizer(L, gcvalue(obj), mt); - } - break; - } - default: { - G(L)->mt[ttype(obj)] = mt; - break; - } - } - L->top--; - lua_unlock(L); - return 1; -} - - -LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { - TValue *o; - int res; - lua_lock(L); - api_checknelems(L, 1); - o = index2value(L, idx); - api_check(L, ttisfulluserdata(o), "full userdata expected"); - if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) - res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ - else { - setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); - res = 1; - } - L->top--; - lua_unlock(L); - return res; -} - - -/* -** 'load' and 'call' functions (run Lua code) -*/ - - -#define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ - "results from function overflow current stack size") - - -LUA_API void lua_callk (lua_State *L, int nargs, int nresults, - lua_KContext ctx, lua_KFunction k) { - StkId func; - lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), - "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); - checkresults(L, nargs, nresults); - func = L->top - (nargs+1); - if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ - L->ci->u.c.k = k; /* save continuation */ - L->ci->u.c.ctx = ctx; /* save context */ - luaD_call(L, func, nresults); /* do the call */ - } - else /* no continuation or no yieldable */ - luaD_callnoyield(L, func, nresults); /* just do the call */ - adjustresults(L, nresults); - lua_unlock(L); -} - - - -/* -** Execute a protected call. -*/ -struct CallS { /* data to 'f_call' */ - StkId func; - int nresults; -}; - - -static void f_call (lua_State *L, void *ud) { - struct CallS *c = cast(struct CallS *, ud); - luaD_callnoyield(L, c->func, c->nresults); -} - - - -LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, - lua_KContext ctx, lua_KFunction k) { - struct CallS c; - int status; - ptrdiff_t func; - lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), - "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); - checkresults(L, nargs, nresults); - if (errfunc == 0) - func = 0; - else { - StkId o = index2stack(L, errfunc); - api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); - func = savestack(L, o); - } - c.func = L->top - (nargs+1); /* function to be called */ - if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ - c.nresults = nresults; /* do a 'conventional' protected call */ - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); - } - else { /* prepare continuation (call is already protected by 'resume') */ - CallInfo *ci = L->ci; - ci->u.c.k = k; /* save continuation */ - ci->u.c.ctx = ctx; /* save context */ - /* save information for error recovery */ - ci->u2.funcidx = cast_int(savestack(L, c.func)); - ci->u.c.old_errfunc = L->errfunc; - L->errfunc = func; - setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ - ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ - luaD_call(L, c.func, nresults); /* do the call */ - ci->callstatus &= ~CIST_YPCALL; - L->errfunc = ci->u.c.old_errfunc; - status = LUA_OK; /* if it is here, there were no errors */ - } - adjustresults(L, nresults); - lua_unlock(L); - return status; -} - - -LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, - const char *chunkname, const char *mode) { - ZIO z; - int status; - lua_lock(L); - if (!chunkname) chunkname = "?"; - luaZ_init(L, &z, reader, data); - status = luaD_protectedparser(L, &z, chunkname, mode); - if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ - if (f->nupvalues >= 1) { /* does it have an upvalue? */ - /* get global table from registry */ - const TValue *gt = getGtable(L); - /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); - luaC_barrier(L, f->upvals[0], gt); - } - } - lua_unlock(L); - return status; -} - - -LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { - int status; - TValue *o; - lua_lock(L); - api_checknelems(L, 1); - o = s2v(L->top - 1); - if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, strip); - else - status = 1; - lua_unlock(L); - return status; -} - - -LUA_API int lua_status (lua_State *L) { - return L->status; -} - - -/* -** Garbage-collection function -*/ -LUA_API int lua_gc (lua_State *L, int what, ...) { - va_list argp; - int res = 0; - global_State *g = G(L); - if (g->gcstp & GCSTPGC) /* internal stop? */ - return -1; /* all options are invalid when stopped */ - lua_lock(L); - va_start(argp, what); - switch (what) { - case LUA_GCSTOP: { - g->gcstp = GCSTPUSR; /* stopped by the user */ - break; - } - case LUA_GCRESTART: { - luaE_setdebt(g, 0); - g->gcstp = 0; /* (GCSTPGC must be already zero here) */ - break; - } - case LUA_GCCOLLECT: { - luaC_fullgc(L, 0); - break; - } - case LUA_GCCOUNT: { - /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(gettotalbytes(g) >> 10); - break; - } - case LUA_GCCOUNTB: { - res = cast_int(gettotalbytes(g) & 0x3ff); - break; - } - case LUA_GCSTEP: { - int data = va_arg(argp, int); - l_mem debt = 1; /* =1 to signal that it did an actual step */ - lu_byte oldstp = g->gcstp; - g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ - if (data == 0) { - luaE_setdebt(g, 0); /* do a basic step */ - luaC_step(L); - } - else { /* add 'data' to total debt */ - debt = cast(l_mem, data) * 1024 + g->GCdebt; - luaE_setdebt(g, debt); - luaC_checkGC(L); - } - g->gcstp = oldstp; /* restore previous state */ - if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ - res = 1; /* signal it */ - break; - } - case LUA_GCSETPAUSE: { - int data = va_arg(argp, int); - res = getgcparam(g->gcpause); - setgcparam(g->gcpause, data); - break; - } - case LUA_GCSETSTEPMUL: { - int data = va_arg(argp, int); - res = getgcparam(g->gcstepmul); - setgcparam(g->gcstepmul, data); - break; - } - case LUA_GCISRUNNING: { - res = gcrunning(g); - break; - } - case LUA_GCGEN: { - int minormul = va_arg(argp, int); - int majormul = va_arg(argp, int); - res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; - if (minormul != 0) - g->genminormul = minormul; - if (majormul != 0) - setgcparam(g->genmajormul, majormul); - luaC_changemode(L, KGC_GEN); - break; - } - case LUA_GCINC: { - int pause = va_arg(argp, int); - int stepmul = va_arg(argp, int); - int stepsize = va_arg(argp, int); - res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; - if (pause != 0) - setgcparam(g->gcpause, pause); - if (stepmul != 0) - setgcparam(g->gcstepmul, stepmul); - if (stepsize != 0) - g->gcstepsize = stepsize; - luaC_changemode(L, KGC_INC); - break; - } - default: res = -1; /* invalid option */ - } - va_end(argp); - lua_unlock(L); - return res; -} - - - -/* -** miscellaneous functions -*/ - - -LUA_API int lua_error (lua_State *L) { - TValue *errobj; - lua_lock(L); - errobj = s2v(L->top - 1); - api_checknelems(L, 1); - /* error object is the memory error message? */ - if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) - luaM_error(L); /* raise a memory error */ - else - luaG_errormsg(L); /* raise a regular error */ - /* code unreachable; will unlock when control actually leaves the kernel */ - return 0; /* to avoid warnings */ -} - - -LUA_API int lua_next (lua_State *L, int idx) { - Table *t; - int more; - lua_lock(L); - api_checknelems(L, 1); - t = gettable(L, idx); - more = luaH_next(L, t, L->top - 1); - if (more) { - api_incr_top(L); - } - else /* no more elements */ - L->top -= 1; /* remove key */ - lua_unlock(L); - return more; -} - - -LUA_API void lua_toclose (lua_State *L, int idx) { - int nresults; - StkId o; - lua_lock(L); - o = index2stack(L, idx); - nresults = L->ci->nresults; - api_check(L, L->tbclist < o, "given index below or equal a marked one"); - luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ - if (!hastocloseCfunc(nresults)) /* function not marked yet? */ - L->ci->nresults = codeNresults(nresults); /* mark it */ - lua_assert(hastocloseCfunc(L->ci->nresults)); - lua_unlock(L); -} - - -LUA_API void lua_concat (lua_State *L, int n) { - lua_lock(L); - api_checknelems(L, n); - if (n > 0) - luaV_concat(L, n); - else { /* nothing to concatenate */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */ - api_incr_top(L); - } - luaC_checkGC(L); - lua_unlock(L); -} - - -LUA_API void lua_len (lua_State *L, int idx) { - TValue *t; - lua_lock(L); - t = index2value(L, idx); - luaV_objlen(L, L->top, t); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { - lua_Alloc f; - lua_lock(L); - if (ud) *ud = G(L)->ud; - f = G(L)->frealloc; - lua_unlock(L); - return f; -} - - -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { - lua_lock(L); - G(L)->ud = ud; - G(L)->frealloc = f; - lua_unlock(L); -} - - -void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { - lua_lock(L); - G(L)->ud_warn = ud; - G(L)->warnf = f; - lua_unlock(L); -} - - -void lua_warning (lua_State *L, const char *msg, int tocont) { - lua_lock(L); - luaE_warning(L, msg, tocont); - lua_unlock(L); -} - - - -LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { - Udata *u; - lua_lock(L); - api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); - u = luaS_newudata(L, size, nuvalue); - setuvalue(L, s2v(L->top), u); - api_incr_top(L); - luaC_checkGC(L); - lua_unlock(L); - return getudatamem(u); -} - - - -static const char *aux_upvalue (TValue *fi, int n, TValue **val, - GCObject **owner) { - switch (ttypetag(fi)) { - case LUA_VCCL: { /* C closure */ - CClosure *f = clCvalue(fi); - if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) - return NULL; /* 'n' not in [1, f->nupvalues] */ - *val = &f->upvalue[n-1]; - if (owner) *owner = obj2gco(f); - return ""; - } - case LUA_VLCL: { /* Lua closure */ - LClosure *f = clLvalue(fi); - TString *name; - Proto *p = f->p; - if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) - return NULL; /* 'n' not in [1, p->sizeupvalues] */ - *val = f->upvals[n-1]->v; - if (owner) *owner = obj2gco(f->upvals[n - 1]); - name = p->upvalues[n-1].name; - return (name == NULL) ? "(no name)" : getstr(name); - } - default: return NULL; /* not a closure */ - } -} - - -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val = NULL; /* to avoid warnings */ - lua_lock(L); - name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); - if (name) { - setobj2s(L, L->top, val); - api_incr_top(L); - } - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { - const char *name; - TValue *val = NULL; /* to avoid warnings */ - GCObject *owner = NULL; /* to avoid warnings */ - TValue *fi; - lua_lock(L); - fi = index2value(L, funcindex); - api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner); - if (name) { - L->top--; - setobj(L, val, s2v(L->top)); - luaC_barrier(L, owner, val); - } - lua_unlock(L); - return name; -} - - -static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { - static const UpVal *const nullup = NULL; - LClosure *f; - TValue *fi = index2value(L, fidx); - api_check(L, ttisLclosure(fi), "Lua function expected"); - f = clLvalue(fi); - if (pf) *pf = f; - if (1 <= n && n <= f->p->sizeupvalues) - return &f->upvals[n - 1]; /* get its upvalue pointer */ - else - return (UpVal**)&nullup; -} - - -LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { - TValue *fi = index2value(L, fidx); - switch (ttypetag(fi)) { - case LUA_VLCL: { /* lua closure */ - return *getupvalref(L, fidx, n, NULL); - } - case LUA_VCCL: { /* C closure */ - CClosure *f = clCvalue(fi); - if (1 <= n && n <= f->nupvalues) - return &f->upvalue[n - 1]; - /* else */ - } /* FALLTHROUGH */ - case LUA_VLCF: - return NULL; /* light C functions have no upvalues */ - default: { - api_check(L, 0, "function expected"); - return NULL; - } - } -} - - -LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, - int fidx2, int n2) { - LClosure *f1; - UpVal **up1 = getupvalref(L, fidx1, n1, &f1); - UpVal **up2 = getupvalref(L, fidx2, n2, NULL); - api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); - *up1 = *up2; - luaC_objbarrier(L, f1, *up1); -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.h deleted file mode 100644 index 9e99cc4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lapi.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -** $Id: lapi.h $ -** Auxiliary functions from Lua API -** See Copyright Notice in lua.h -*/ - -#ifndef lapi_h -#define lapi_h - - -#include "llimits.h" -#include "lstate.h" - - -/* Increments 'L->top', checking for stack overflows */ -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ - "stack overflow");} - - -/* -** If a call returns too many multiple returns, the callee may not have -** stack space to accommodate all results. In this case, this macro -** increases its stack space ('L->ci->top'). -*/ -#define adjustresults(L,nres) \ - { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } - - -/* Ensure the stack has at least 'n' elements */ -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ - "not enough elements in the stack") - - -/* -** To reduce the overhead of returning from C functions, the presence of -** to-be-closed variables in these functions is coded in the CallInfo's -** field 'nresults', in a way that functions with no to-be-closed variables -** with zero, one, or "all" wanted results have no overhead. Functions -** with other number of wanted results, as well as functions with -** variables to be closed, have an extra check. -*/ - -#define hastocloseCfunc(n) ((n) < LUA_MULTRET) - -/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */ -#define codeNresults(n) (-(n) - 3) -#define decodeNresults(n) (-(n) - 3) - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.c deleted file mode 100644 index 8ed1da1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.c +++ /dev/null @@ -1,1106 +0,0 @@ -/* -** $Id: lauxlib.c $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - -#define lauxlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include - - -/* -** This file uses only the official API of Lua. -** Any function declared here could be written as an application function. -*/ - -#include "lua.h" - -#include "lauxlib.h" - - -#if !defined(MAX_SIZET) -/* maximum value for size_t */ -#define MAX_SIZET ((size_t)(~(size_t)0)) -#endif - - -/* -** {====================================================== -** Traceback -** ======================================================= -*/ - - -#define LEVELS1 10 /* size of the first part of the stack */ -#define LEVELS2 11 /* size of the second part of the stack */ - - - -/* -** Search for 'objidx' in table at index -1. ('objidx' must be an -** absolute index.) Return 1 + string at top if it found a good name. -*/ -static int findfield (lua_State *L, int objidx, int level) { - if (level == 0 || !lua_istable(L, -1)) - return 0; /* not found */ - lua_pushnil(L); /* start 'next' loop */ - while (lua_next(L, -2)) { /* for each pair in table */ - if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ - if (lua_rawequal(L, objidx, -1)) { /* found object? */ - lua_pop(L, 1); /* remove value (but keep name) */ - return 1; - } - else if (findfield(L, objidx, level - 1)) { /* try recursively */ - /* stack: lib_name, lib_table, field_name (top) */ - lua_pushliteral(L, "."); /* place '.' between the two names */ - lua_replace(L, -3); /* (in the slot occupied by table) */ - lua_concat(L, 3); /* lib_name.field_name */ - return 1; - } - } - lua_pop(L, 1); /* remove value */ - } - return 0; /* not found */ -} - - -/* -** Search for a name for a function in all loaded modules -*/ -static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { - int top = lua_gettop(L); - lua_getinfo(L, "f", ar); /* push function */ - lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - if (findfield(L, top + 1, 2)) { - const char *name = lua_tostring(L, -1); - if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ - lua_pushstring(L, name + 3); /* push name without prefix */ - lua_remove(L, -2); /* remove original name */ - } - lua_copy(L, -1, top + 1); /* copy name to proper place */ - lua_settop(L, top + 1); /* remove table "loaded" and name copy */ - return 1; - } - else { - lua_settop(L, top); /* remove function and global table */ - return 0; - } -} - - -static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (pushglobalfuncname(L, ar)) { /* try first a global name */ - lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else if (*ar->namewhat != '\0') /* is there a name from code? */ - lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ - else if (*ar->what == 'm') /* main? */ - lua_pushliteral(L, "main chunk"); - else if (*ar->what != 'C') /* for Lua functions, use */ - lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); - else /* nothing left... */ - lua_pushliteral(L, "?"); -} - - -static int lastlevel (lua_State *L) { - lua_Debug ar; - int li = 1, le = 1; - /* find an upper bound */ - while (lua_getstack(L, le, &ar)) { li = le; le *= 2; } - /* do a binary search */ - while (li < le) { - int m = (li + le)/2; - if (lua_getstack(L, m, &ar)) li = m + 1; - else le = m; - } - return le - 1; -} - - -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, - const char *msg, int level) { - luaL_Buffer b; - lua_Debug ar; - int last = lastlevel(L1); - int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; - luaL_buffinit(L, &b); - if (msg) { - luaL_addstring(&b, msg); - luaL_addchar(&b, '\n'); - } - luaL_addstring(&b, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - if (limit2show-- == 0) { /* too many levels? */ - int n = last - level - LEVELS2 + 1; /* number of levels to skip */ - lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); - luaL_addvalue(&b); /* add warning about skip */ - level += n; /* and skip to last levels */ - } - else { - lua_getinfo(L1, "Slnt", &ar); - if (ar.currentline <= 0) - lua_pushfstring(L, "\n\t%s: in ", ar.short_src); - else - lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); - luaL_addvalue(&b); - pushfuncname(L, &ar); - luaL_addvalue(&b); - if (ar.istailcall) - luaL_addstring(&b, "\n\t(...tail calls...)"); - } - } - luaL_pushresult(&b); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Error-report functions -** ======================================================= -*/ - -LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { - lua_Debug ar; - if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ - return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); - lua_getinfo(L, "n", &ar); - if (strcmp(ar.namewhat, "method") == 0) { - arg--; /* do not count 'self' */ - if (arg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling '%s' on bad self (%s)", - ar.name, extramsg); - } - if (ar.name == NULL) - ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to '%s' (%s)", - arg, ar.name, extramsg); -} - - -LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) { - const char *msg; - const char *typearg; /* name for the type of the actual argument */ - if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) - typearg = lua_tostring(L, -1); /* use the given type name */ - else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) - typearg = "light userdata"; /* special name for messages */ - else - typearg = luaL_typename(L, arg); /* standard name */ - msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); - return luaL_argerror(L, arg, msg); -} - - -static void tag_error (lua_State *L, int arg, int tag) { - luaL_typeerror(L, arg, lua_typename(L, tag)); -} - - -/* -** The use of 'lua_pushfstring' ensures this function does not -** need reserved stack space when called. -*/ -LUALIB_API void luaL_where (lua_State *L, int level) { - lua_Debug ar; - if (lua_getstack(L, level, &ar)) { /* check function at level */ - lua_getinfo(L, "Sl", &ar); /* get info about it */ - if (ar.currentline > 0) { /* is there info? */ - lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); - return; - } - } - lua_pushfstring(L, ""); /* else, no information available... */ -} - - -/* -** Again, the use of 'lua_pushvfstring' ensures this function does -** not need reserved stack space when called. (At worst, it generates -** an error with "stack overflow" instead of the given message.) -*/ -LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - luaL_where(L, 1); - lua_pushvfstring(L, fmt, argp); - va_end(argp); - lua_concat(L, 2); - return lua_error(L); -} - - -LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { - int en = errno; /* calls to Lua API may change this value */ - if (stat) { - lua_pushboolean(L, 1); - return 1; - } - else { - luaL_pushfail(L); - if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); - else - lua_pushstring(L, strerror(en)); - lua_pushinteger(L, en); - return 3; - } -} - - -#if !defined(l_inspectstat) /* { */ - -#if defined(LUA_USE_POSIX) - -#include - -/* -** use appropriate macros to interpret 'pclose' return status -*/ -#define l_inspectstat(stat,what) \ - if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ - else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } - -#else - -#define l_inspectstat(stat,what) /* no op */ - -#endif - -#endif /* } */ - - -LUALIB_API int luaL_execresult (lua_State *L, int stat) { - if (stat != 0 && errno != 0) /* error with an 'errno'? */ - return luaL_fileresult(L, 0, NULL); - else { - const char *what = "exit"; /* type of termination */ - l_inspectstat(stat, what); /* interpret result */ - if (*what == 'e' && stat == 0) /* successful termination? */ - lua_pushboolean(L, 1); - else - luaL_pushfail(L); - lua_pushstring(L, what); - lua_pushinteger(L, stat); - return 3; /* return true/fail,what,code */ - } -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Userdata's metatable manipulation -** ======================================================= -*/ - -LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { - if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ - return 0; /* leave previous value on top, but return 0 */ - lua_pop(L, 1); - lua_createtable(L, 0, 2); /* create metatable */ - lua_pushstring(L, tname); - lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ - return 1; -} - - -LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) { - luaL_getmetatable(L, tname); - lua_setmetatable(L, -2); -} - - -LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); - if (p != NULL) { /* value is a userdata? */ - if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ - luaL_getmetatable(L, tname); /* get correct metatable */ - if (!lua_rawequal(L, -1, -2)) /* not the same? */ - p = NULL; /* value is a userdata with wrong metatable */ - lua_pop(L, 2); /* remove both metatables */ - return p; - } - } - return NULL; /* value is not a userdata with a metatable */ -} - - -LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - void *p = luaL_testudata(L, ud, tname); - luaL_argexpected(L, p != NULL, ud, tname); - return p; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Argument check functions -** ======================================================= -*/ - -LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, - const char *const lst[]) { - const char *name = (def) ? luaL_optstring(L, arg, def) : - luaL_checkstring(L, arg); - int i; - for (i=0; lst[i]; i++) - if (strcmp(lst[i], name) == 0) - return i; - return luaL_argerror(L, arg, - lua_pushfstring(L, "invalid option '%s'", name)); -} - - -/* -** Ensures the stack has at least 'space' extra slots, raising an error -** if it cannot fulfill the request. (The error handling needs a few -** extra slots to format the error message. In case of an error without -** this extra space, Lua will generate the same 'stack overflow' error, -** but without 'msg'.) -*/ -LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - if (l_unlikely(!lua_checkstack(L, space))) { - if (msg) - luaL_error(L, "stack overflow (%s)", msg); - else - luaL_error(L, "stack overflow"); - } -} - - -LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { - if (l_unlikely(lua_type(L, arg) != t)) - tag_error(L, arg, t); -} - - -LUALIB_API void luaL_checkany (lua_State *L, int arg) { - if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) - luaL_argerror(L, arg, "value expected"); -} - - -LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { - const char *s = lua_tolstring(L, arg, len); - if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); - return s; -} - - -LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, - const char *def, size_t *len) { - if (lua_isnoneornil(L, arg)) { - if (len) - *len = (def ? strlen(def) : 0); - return def; - } - else return luaL_checklstring(L, arg, len); -} - - -LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { - int isnum; - lua_Number d = lua_tonumberx(L, arg, &isnum); - if (l_unlikely(!isnum)) - tag_error(L, arg, LUA_TNUMBER); - return d; -} - - -LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) { - return luaL_opt(L, luaL_checknumber, arg, def); -} - - -static void interror (lua_State *L, int arg) { - if (lua_isnumber(L, arg)) - luaL_argerror(L, arg, "number has no integer representation"); - else - tag_error(L, arg, LUA_TNUMBER); -} - - -LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { - int isnum; - lua_Integer d = lua_tointegerx(L, arg, &isnum); - if (l_unlikely(!isnum)) { - interror(L, arg); - } - return d; -} - - -LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg, - lua_Integer def) { - return luaL_opt(L, luaL_checkinteger, arg, def); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - -/* userdata to box arbitrary data */ -typedef struct UBox { - void *box; - size_t bsize; -} UBox; - - -static void *resizebox (lua_State *L, int idx, size_t newsize) { - void *ud; - lua_Alloc allocf = lua_getallocf(L, &ud); - UBox *box = (UBox *)lua_touserdata(L, idx); - void *temp = allocf(ud, box->box, box->bsize, newsize); - if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ - lua_pushliteral(L, "not enough memory"); - lua_error(L); /* raise a memory error */ - } - box->box = temp; - box->bsize = newsize; - return temp; -} - - -static int boxgc (lua_State *L) { - resizebox(L, 1, 0); - return 0; -} - - -static const luaL_Reg boxmt[] = { /* box metamethods */ - {"__gc", boxgc}, - {"__close", boxgc}, - {NULL, NULL} -}; - - -static void newbox (lua_State *L) { - UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); - box->box = NULL; - box->bsize = 0; - if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ - luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ - lua_setmetatable(L, -2); -} - - -/* -** check whether buffer is using a userdata on the stack as a temporary -** buffer -*/ -#define buffonstack(B) ((B)->b != (B)->init.b) - - -/* -** Whenever buffer is accessed, slot 'idx' must either be a box (which -** cannot be NULL) or it is a placeholder for the buffer. -*/ -#define checkbufferlevel(B,idx) \ - lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ - : lua_touserdata(B->L, idx) == (void*)B) - - -/* -** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. -*/ -static size_t newbuffsize (luaL_Buffer *B, size_t sz) { - size_t newsize = B->size * 2; /* double buffer size */ - if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ - return luaL_error(B->L, "buffer too large"); - if (newsize < B->n + sz) /* double is not big enough? */ - newsize = B->n + sz; - return newsize; -} - - -/* -** Returns a pointer to a free area with at least 'sz' bytes in buffer -** 'B'. 'boxidx' is the relative position in the stack where is the -** buffer's box or its placeholder. -*/ -static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { - checkbufferlevel(B, boxidx); - if (B->size - B->n >= sz) /* enough space? */ - return B->b + B->n; - else { - lua_State *L = B->L; - char *newbuff; - size_t newsize = newbuffsize(B, sz); - /* create larger buffer */ - if (buffonstack(B)) /* buffer already has a box? */ - newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ - else { /* no box yet */ - lua_remove(L, boxidx); /* remove placeholder */ - newbox(L); /* create a new box */ - lua_insert(L, boxidx); /* move box to its intended position */ - lua_toclose(L, boxidx); - newbuff = (char *)resizebox(L, boxidx, newsize); - memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ - } - B->b = newbuff; - B->size = newsize; - return newbuff + B->n; - } -} - -/* -** returns a pointer to a free area with at least 'sz' bytes -*/ -LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { - return prepbuffsize(B, sz, -1); -} - - -LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ - char *b = prepbuffsize(B, l, -1); - memcpy(b, s, l * sizeof(char)); - luaL_addsize(B, l); - } -} - - -LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { - luaL_addlstring(B, s, strlen(s)); -} - - -LUALIB_API void luaL_pushresult (luaL_Buffer *B) { - lua_State *L = B->L; - checkbufferlevel(B, -1); - lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) - lua_closeslot(L, -2); /* close the box */ - lua_remove(L, -2); /* remove box or placeholder from the stack */ -} - - -LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { - luaL_addsize(B, sz); - luaL_pushresult(B); -} - - -/* -** 'luaL_addvalue' is the only function in the Buffer system where the -** box (if existent) is not on the top of the stack. So, instead of -** calling 'luaL_addlstring', it replicates the code using -2 as the -** last argument to 'prepbuffsize', signaling that the box is (or will -** be) bellow the string being added to the buffer. (Box creation can -** trigger an emergency GC, so we should not remove the string from the -** stack before we have the space guaranteed.) -*/ -LUALIB_API void luaL_addvalue (luaL_Buffer *B) { - lua_State *L = B->L; - size_t len; - const char *s = lua_tolstring(L, -1, &len); - char *b = prepbuffsize(B, len, -2); - memcpy(b, s, len * sizeof(char)); - luaL_addsize(B, len); - lua_pop(L, 1); /* pop string */ -} - - -LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { - B->L = L; - B->b = B->init.b; - B->n = 0; - B->size = LUAL_BUFFERSIZE; - lua_pushlightuserdata(L, (void*)B); /* push placeholder */ -} - - -LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { - luaL_buffinit(L, B); - return prepbuffsize(B, sz, -1); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Reference system -** ======================================================= -*/ - -/* index of free-list header (after the predefined values) */ -#define freelist (LUA_RIDX_LAST + 1) - -/* -** The previously freed references form a linked list: -** t[freelist] is the index of a first free index, or zero if list is -** empty; t[t[freelist]] is the index of the second element; etc. -*/ -LUALIB_API int luaL_ref (lua_State *L, int t) { - int ref; - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* 'nil' has a unique fixed reference */ - } - t = lua_absindex(L, t); - if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */ - ref = 0; /* list is empty */ - lua_pushinteger(L, 0); /* initialize as an empty list */ - lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */ - } - else { /* already initialized */ - lua_assert(lua_isinteger(L, -1)); - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ - } - lua_pop(L, 1); /* remove element from stack */ - if (ref != 0) { /* any free element? */ - lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ - } - else /* no free elements */ - ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ - lua_rawseti(L, t, ref); - return ref; -} - - -LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { - if (ref >= 0) { - t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); - lua_assert(lua_isinteger(L, -1)); - lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ - lua_pushinteger(L, ref); - lua_rawseti(L, t, freelist); /* t[freelist] = ref */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Load functions -** ======================================================= -*/ - -typedef struct LoadF { - int n; /* number of pre-read characters */ - FILE *f; /* file being read */ - char buff[BUFSIZ]; /* area for reading file */ -} LoadF; - - -static const char *getF (lua_State *L, void *ud, size_t *size) { - LoadF *lf = (LoadF *)ud; - (void)L; /* not used */ - if (lf->n > 0) { /* are there pre-read characters to be read? */ - *size = lf->n; /* return them (chars already in buffer) */ - lf->n = 0; /* no more pre-read characters */ - } - else { /* read a block from file */ - /* 'fread' can return > 0 *and* set the EOF flag. If next call to - 'getF' called 'fread', it might still wait for user input. - The next check avoids this problem. */ - if (feof(lf->f)) return NULL; - *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ - } - return lf->buff; -} - - -static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); - const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); - lua_remove(L, fnameindex); - return LUA_ERRFILE; -} - - -static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ -} - - -/* -** reads the first character of file 'f' and skips an optional BOM mark -** in its beginning plus its first line if it starts with '#'. Returns -** true if it skipped the first line. In any case, '*cp' has the -** first "valid" character of the file (after the optional BOM and -** a first-line comment). -*/ -static int skipcomment (LoadF *lf, int *cp) { - int c = *cp = skipBOM(lf); - if (c == '#') { /* first line is a comment (Unix exec. file)? */ - do { /* skip first line */ - c = getc(lf->f); - } while (c != EOF && c != '\n'); - *cp = getc(lf->f); /* skip end-of-line, if present */ - return 1; /* there was a comment */ - } - else return 0; /* no comment */ -} - - -LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, - const char *mode) { - LoadF lf; - int status, readstatus; - int c; - int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ - if (filename == NULL) { - lua_pushliteral(L, "=stdin"); - lf.f = stdin; - } - else { - lua_pushfstring(L, "@%s", filename); - lf.f = fopen(filename, "r"); - if (lf.f == NULL) return errfile(L, "open", fnameindex); - } - if (skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(&lf, &c); /* re-read initial portion */ - } - if (c != EOF) - lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ - status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); - readstatus = ferror(lf.f); - if (filename) fclose(lf.f); /* close file (even in case of errors) */ - if (readstatus) { - lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ - return errfile(L, "read", fnameindex); - } - lua_remove(L, fnameindex); - return status; -} - - -typedef struct LoadS { - const char *s; - size_t size; -} LoadS; - - -static const char *getS (lua_State *L, void *ud, size_t *size) { - LoadS *ls = (LoadS *)ud; - (void)L; /* not used */ - if (ls->size == 0) return NULL; - *size = ls->size; - ls->size = 0; - return ls->s; -} - - -LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, - const char *name, const char *mode) { - LoadS ls; - ls.s = buff; - ls.size = size; - return lua_load(L, getS, &ls, name, mode); -} - - -LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { - return luaL_loadbuffer(L, s, strlen(s), s); -} - -/* }====================================================== */ - - - -LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { - if (!lua_getmetatable(L, obj)) /* no metatable? */ - return LUA_TNIL; - else { - int tt; - lua_pushstring(L, event); - tt = lua_rawget(L, -2); - if (tt == LUA_TNIL) /* is metafield nil? */ - lua_pop(L, 2); /* remove metatable and metafield */ - else - lua_remove(L, -2); /* remove only metatable */ - return tt; /* return metafield type */ - } -} - - -LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { - obj = lua_absindex(L, obj); - if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ - return 0; - lua_pushvalue(L, obj); - lua_call(L, 1, 1); - return 1; -} - - -LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { - lua_Integer l; - int isnum; - lua_len(L, idx); - l = lua_tointegerx(L, -1, &isnum); - if (l_unlikely(!isnum)) - luaL_error(L, "object length is not an integer"); - lua_pop(L, 1); /* remove object */ - return l; -} - - -LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { - idx = lua_absindex(L,idx); - if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ - if (!lua_isstring(L, -1)) - luaL_error(L, "'__tostring' must return a string"); - } - else { - switch (lua_type(L, idx)) { - case LUA_TNUMBER: { - if (lua_isinteger(L, idx)) - lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); - else - lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); - break; - } - case LUA_TSTRING: - lua_pushvalue(L, idx); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); - break; - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - default: { - int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ - const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : - luaL_typename(L, idx); - lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); - if (tt != LUA_TNIL) - lua_remove(L, -2); /* remove '__name' */ - break; - } - } - } - return lua_tolstring(L, -1, len); -} - - -/* -** set functions from list 'l' into table at top - 'nup'; each -** function gets the 'nup' elements at the top as upvalues. -** Returns with only the table at the stack. -*/ -LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - if (l->func == NULL) /* place holder? */ - lua_pushboolean(L, 0); - else { - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - } - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - - -/* -** ensure that stack[idx][fname] has a table and push that table -** into the stack -*/ -LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) { - if (lua_getfield(L, idx, fname) == LUA_TTABLE) - return 1; /* table already there */ - else { - lua_pop(L, 1); /* remove previous result */ - idx = lua_absindex(L, idx); - lua_newtable(L); - lua_pushvalue(L, -1); /* copy to be left at top */ - lua_setfield(L, idx, fname); /* assign new table to field */ - return 0; /* false, because did not find table there */ - } -} - - -/* -** Stripped-down 'require': After checking "loaded" table, calls 'openf' -** to open a module, registers the result in 'package.loaded' table and, -** if 'glb' is true, also registers the result in the global table. -** Leaves resulting module on the top. -*/ -LUALIB_API void luaL_requiref (lua_State *L, const char *modname, - lua_CFunction openf, int glb) { - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - lua_getfield(L, -1, modname); /* LOADED[modname] */ - if (!lua_toboolean(L, -1)) { /* package not already loaded? */ - lua_pop(L, 1); /* remove field */ - lua_pushcfunction(L, openf); - lua_pushstring(L, modname); /* argument to open function */ - lua_call(L, 1, 1); /* call 'openf' to open module */ - lua_pushvalue(L, -1); /* make copy of module (call result) */ - lua_setfield(L, -3, modname); /* LOADED[modname] = module */ - } - lua_remove(L, -2); /* remove LOADED table */ - if (glb) { - lua_pushvalue(L, -1); /* copy of module */ - lua_setglobal(L, modname); /* _G[modname] = module */ - } -} - - -LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, - const char *p, const char *r) { - const char *wild; - size_t l = strlen(p); - while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(b, s, wild - s); /* push prefix */ - luaL_addstring(b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after 'p' */ - } - luaL_addstring(b, s); /* push last suffix */ -} - - -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, - const char *p, const char *r) { - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addgsub(&b, s, p, r); - luaL_pushresult(&b); - return lua_tostring(L, -1); -} - - -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { - (void)ud; (void)osize; /* not used */ - if (nsize == 0) { - free(ptr); - return NULL; - } - else - return realloc(ptr, nsize); -} - - -static int panic (lua_State *L) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "error object is not a string"; - lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - msg); - return 0; /* return to Lua to abort */ -} - - -/* -** Warning functions: -** warnfoff: warning system is off -** warnfon: ready to start a new message -** warnfcont: previous message is to be continued -*/ -static void warnfoff (void *ud, const char *message, int tocont); -static void warnfon (void *ud, const char *message, int tocont); -static void warnfcont (void *ud, const char *message, int tocont); - - -/* -** Check whether message is a control message. If so, execute the -** control or ignore it if unknown. -*/ -static int checkcontrol (lua_State *L, const char *message, int tocont) { - if (tocont || *(message++) != '@') /* not a control message? */ - return 0; - else { - if (strcmp(message, "off") == 0) - lua_setwarnf(L, warnfoff, L); /* turn warnings off */ - else if (strcmp(message, "on") == 0) - lua_setwarnf(L, warnfon, L); /* turn warnings on */ - return 1; /* it was a control message */ - } -} - - -static void warnfoff (void *ud, const char *message, int tocont) { - checkcontrol((lua_State *)ud, message, tocont); -} - - -/* -** Writes the message and handle 'tocont', finishing the message -** if needed and setting the next warn function. -*/ -static void warnfcont (void *ud, const char *message, int tocont) { - lua_State *L = (lua_State *)ud; - lua_writestringerror("%s", message); /* write message */ - if (tocont) /* not the last part? */ - lua_setwarnf(L, warnfcont, L); /* to be continued */ - else { /* last part */ - lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ - lua_setwarnf(L, warnfon, L); /* next call is a new message */ - } -} - - -static void warnfon (void *ud, const char *message, int tocont) { - if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ - return; /* nothing else to be done */ - lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ - warnfcont(ud, message, tocont); /* finish processing */ -} - - -LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); - if (l_likely(L)) { - lua_atpanic(L, &panic); - lua_setwarnf(L, warnfoff, L); /* default is warnings off */ - } - return L; -} - - -LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { - lua_Number v = lua_version(L); - if (sz != LUAL_NUMSIZES) /* check numeric types */ - luaL_error(L, "core and library have incompatible numeric types"); - else if (v != ver) - luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.h deleted file mode 100644 index 5b977e2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lauxlib.h +++ /dev/null @@ -1,301 +0,0 @@ -/* -** $Id: lauxlib.h $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lauxlib_h -#define lauxlib_h - - -#include -#include - -#include "luaconf.h" -#include "lua.h" - - -/* global table */ -#define LUA_GNAME "_G" - - -typedef struct luaL_Buffer luaL_Buffer; - - -/* extra error code for 'luaL_loadfilex' */ -#define LUA_ERRFILE (LUA_ERRERR+1) - - -/* key, in the registry, for table of loaded modules */ -#define LUA_LOADED_TABLE "_LOADED" - - -/* key, in the registry, for table of preloaded loaders */ -#define LUA_PRELOAD_TABLE "_PRELOAD" - - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - - -#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number)) - -LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz); -#define luaL_checkversion(L) \ - luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES) - -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); -LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); -LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); -LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, - size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, - const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def); - -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg, - lua_Integer def); - -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int arg); - -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); -LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); - -LUALIB_API void (luaL_where) (lua_State *L, int lvl); -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); - -LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, - const char *const lst[]); - -LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); -LUALIB_API int (luaL_execresult) (lua_State *L, int stat); - - -/* predefined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -LUALIB_API int (luaL_ref) (lua_State *L, int t); -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); - -LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, - const char *mode); - -#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) - -LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, - const char *name, const char *mode); -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); - -LUALIB_API lua_State *(luaL_newstate) (void); - -LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); - -LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, - const char *p, const char *r); -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, - const char *p, const char *r); - -LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); - -LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); - -LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, - const char *msg, int level); - -LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, - lua_CFunction openf, int glb); - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - - -#define luaL_newlibtable(L,l) \ - lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) - -#define luaL_newlib(L,l) \ - (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) - -#define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) - -#define luaL_argexpected(L,cond,arg,tname) \ - ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) - -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) - -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - -#define luaL_dofile(L, fn) \ - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_dostring(L, s) \ - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) - -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) - -#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) - - -/* -** Perform arithmetic operations on lua_Integer values with wrap-around -** semantics, as the Lua core does. -*/ -#define luaL_intop(op,v1,v2) \ - ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) - - -/* push the value used to represent failure/error */ -#define luaL_pushfail(L) lua_pushnil(L) - - -/* -** Internal assertions for in-house debugging -*/ -#if !defined(lua_assert) - -#if defined LUAI_ASSERT - #include - #define lua_assert(c) assert(c) -#else - #define lua_assert(c) ((void)0) -#endif - -#endif - - - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - -struct luaL_Buffer { - char *b; /* buffer address */ - size_t size; /* buffer size */ - size_t n; /* number of characters in buffer */ - lua_State *L; - union { - LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ - char b[LUAL_BUFFERSIZE]; /* initial buffer */ - } init; -}; - - -#define luaL_bufflen(bf) ((bf)->n) -#define luaL_buffaddr(bf) ((bf)->b) - - -#define luaL_addchar(B,c) \ - ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ - ((B)->b[(B)->n++] = (c))) - -#define luaL_addsize(B,s) ((B)->n += (s)) - -#define luaL_buffsub(B,s) ((B)->n -= (s)) - -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); -LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); -LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); - -#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) - -/* }====================================================== */ - - - -/* -** {====================================================== -** File handles for IO library -** ======================================================= -*/ - -/* -** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and -** initial structure 'luaL_Stream' (it may contain other fields -** after that initial structure). -*/ - -#define LUA_FILEHANDLE "FILE*" - - -typedef struct luaL_Stream { - FILE *f; /* stream (NULL for incompletely created streams) */ - lua_CFunction closef; /* to close stream (NULL for closed streams) */ -} luaL_Stream; - -/* }====================================================== */ - -/* -** {================================================================== -** "Abstraction Layer" for basic report of messages and errors -** =================================================================== -*/ - -/* print a string */ -#if !defined(lua_writestring) -#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#endif - -/* print a newline and flush the output */ -#if !defined(lua_writeline) -#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) -#endif - -/* print an error message */ -#if !defined(lua_writestringerror) -#define lua_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) -#endif - -/* }================================================================== */ - - -/* -** {============================================================ -** Compatibility with deprecated conversions -** ============================================================= -*/ -#if defined(LUA_COMPAT_APIINTCASTS) - -#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) -#define luaL_optunsigned(L,a,d) \ - ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) - -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) - -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) - -#endif -/* }============================================================ */ - - - -#endif - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lbaselib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lbaselib.c deleted file mode 100644 index 1d60c9d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lbaselib.c +++ /dev/null @@ -1,549 +0,0 @@ -/* -** $Id: lbaselib.c $ -** Basic library -** See Copyright Notice in lua.h -*/ - -#define lbaselib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - for (i = 1; i <= n; i++) { /* for each argument */ - size_t l; - const char *s = luaL_tolstring(L, i, &l); /* convert it to string */ - if (i > 1) /* not the first element? */ - lua_writestring("\t", 1); /* add a tab before it */ - lua_writestring(s, l); /* print it */ - lua_pop(L, 1); /* pop result */ - } - lua_writeline(); - return 0; -} - - -/* -** Creates a warning with all given arguments. -** Check first for errors; otherwise an error may interrupt -** the composition of a warning, leaving it unfinished. -*/ -static int luaB_warn (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - luaL_checkstring(L, 1); /* at least one argument */ - for (i = 2; i <= n; i++) - luaL_checkstring(L, i); /* make sure all arguments are strings */ - for (i = 1; i < n; i++) /* compose warning */ - lua_warning(L, lua_tostring(L, i), 1); - lua_warning(L, lua_tostring(L, n), 0); /* close warning */ - return 0; -} - - -#define SPACECHARS " \f\n\r\t\v" - -static const char *b_str2int (const char *s, int base, lua_Integer *pn) { - lua_Unsigned n = 0; - int neg = 0; - s += strspn(s, SPACECHARS); /* skip initial spaces */ - if (*s == '-') { s++; neg = 1; } /* handle sign */ - else if (*s == '+') s++; - if (!isalnum((unsigned char)*s)) /* no digit? */ - return NULL; - do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : (toupper((unsigned char)*s) - 'A') + 10; - if (digit >= base) return NULL; /* invalid numeral */ - n = n * base + digit; - s++; - } while (isalnum((unsigned char)*s)); - s += strspn(s, SPACECHARS); /* skip trailing spaces */ - *pn = (lua_Integer)((neg) ? (0u - n) : n); - return s; -} - - -static int luaB_tonumber (lua_State *L) { - if (lua_isnoneornil(L, 2)) { /* standard conversion? */ - if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ - lua_settop(L, 1); /* yes; return it */ - return 1; - } - else { - size_t l; - const char *s = lua_tolstring(L, 1, &l); - if (s != NULL && lua_stringtonumber(L, s) == l + 1) - return 1; /* successful conversion to number */ - /* else not a number */ - luaL_checkany(L, 1); /* (but there must be some parameter) */ - } - } - else { - size_t l; - const char *s; - lua_Integer n = 0; /* to avoid warnings */ - lua_Integer base = luaL_checkinteger(L, 2); - luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ - s = lua_tolstring(L, 1, &l); - luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - if (b_str2int(s, (int)base, &n) == s + l) { - lua_pushinteger(L, n); - return 1; - } /* else not a number */ - } /* else not a number */ - luaL_pushfail(L); /* not a number */ - return 1; -} - - -static int luaB_error (lua_State *L) { - int level = (int)luaL_optinteger(L, 2, 1); - lua_settop(L, 1); - if (lua_type(L, 1) == LUA_TSTRING && level > 0) { - luaL_where(L, level); /* add extra information */ - lua_pushvalue(L, 1); - lua_concat(L, 2); - } - return lua_error(L); -} - - -static int luaB_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); - return 1; /* no metatable */ - } - luaL_getmetafield(L, 1, "__metatable"); - return 1; /* returns either __metatable field (if present) or metatable */ -} - - -static int luaB_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); - if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)) - return luaL_error(L, "cannot change a protected metatable"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; -} - - -static int luaB_rawequal (lua_State *L) { - luaL_checkany(L, 1); - luaL_checkany(L, 2); - lua_pushboolean(L, lua_rawequal(L, 1, 2)); - return 1; -} - - -static int luaB_rawlen (lua_State *L) { - int t = lua_type(L, 1); - luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, - "table or string"); - lua_pushinteger(L, lua_rawlen(L, 1)); - return 1; -} - - -static int luaB_rawget (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - lua_settop(L, 2); - lua_rawget(L, 1); - return 1; -} - -static int luaB_rawset (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); - luaL_checkany(L, 3); - lua_settop(L, 3); - lua_rawset(L, 1); - return 1; -} - - -static int pushmode (lua_State *L, int oldmode) { - if (oldmode == -1) - luaL_pushfail(L); /* invalid call to 'lua_gc' */ - else - lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" - : "generational"); - return 1; -} - - -/* -** check whether call to 'lua_gc' was valid (not inside a finalizer) -*/ -#define checkvalres(res) { if (res == -1) break; } - -static int luaB_collectgarbage (lua_State *L) { - static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setpause", "setstepmul", - "isrunning", "generational", "incremental", NULL}; - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; - int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - switch (o) { - case LUA_GCCOUNT: { - int k = lua_gc(L, o); - int b = lua_gc(L, LUA_GCCOUNTB); - checkvalres(k); - lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); - return 1; - } - case LUA_GCSTEP: { - int step = (int)luaL_optinteger(L, 2, 0); - int res = lua_gc(L, o, step); - checkvalres(res); - lua_pushboolean(L, res); - return 1; - } - case LUA_GCSETPAUSE: - case LUA_GCSETSTEPMUL: { - int p = (int)luaL_optinteger(L, 2, 0); - int previous = lua_gc(L, o, p); - checkvalres(previous); - lua_pushinteger(L, previous); - return 1; - } - case LUA_GCISRUNNING: { - int res = lua_gc(L, o); - checkvalres(res); - lua_pushboolean(L, res); - return 1; - } - case LUA_GCGEN: { - int minormul = (int)luaL_optinteger(L, 2, 0); - int majormul = (int)luaL_optinteger(L, 3, 0); - return pushmode(L, lua_gc(L, o, minormul, majormul)); - } - case LUA_GCINC: { - int pause = (int)luaL_optinteger(L, 2, 0); - int stepmul = (int)luaL_optinteger(L, 3, 0); - int stepsize = (int)luaL_optinteger(L, 4, 0); - return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); - } - default: { - int res = lua_gc(L, o); - checkvalres(res); - lua_pushinteger(L, res); - return 1; - } - } - luaL_pushfail(L); /* invalid call (inside a finalizer) */ - return 1; -} - - -static int luaB_type (lua_State *L) { - int t = lua_type(L, 1); - luaL_argcheck(L, t != LUA_TNONE, 1, "value expected"); - lua_pushstring(L, lua_typename(L, t)); - return 1; -} - - -static int luaB_next (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 2); /* create a 2nd argument if there isn't one */ - if (lua_next(L, 1)) - return 2; - else { - lua_pushnil(L); - return 1; - } -} - - -static int pairscont (lua_State *L, int status, lua_KContext k) { - (void)L; (void)status; (void)k; /* unused */ - return 3; -} - -static int luaB_pairs (lua_State *L) { - luaL_checkany(L, 1); - if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ - lua_pushcfunction(L, luaB_next); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - lua_pushnil(L); /* and initial value */ - } - else { - lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */ - } - return 3; -} - - -/* -** Traversal function for 'ipairs' -*/ -static int ipairsaux (lua_State *L) { - lua_Integer i = luaL_checkinteger(L, 2); - i = luaL_intop(+, i, 1); - lua_pushinteger(L, i); - return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; -} - - -/* -** 'ipairs' function. Returns 'ipairsaux', given "table", 0. -** (The given "table" may not be a table.) -*/ -static int luaB_ipairs (lua_State *L) { - luaL_checkany(L, 1); - lua_pushcfunction(L, ipairsaux); /* iteration function */ - lua_pushvalue(L, 1); /* state */ - lua_pushinteger(L, 0); /* initial value */ - return 3; -} - - -static int load_aux (lua_State *L, int status, int envidx) { - if (l_likely(status == LUA_OK)) { - if (envidx != 0) { /* 'env' parameter? */ - lua_pushvalue(L, envidx); /* environment for loaded function */ - if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ - lua_pop(L, 1); /* remove 'env' if not used by previous call */ - } - return 1; - } - else { /* error (message is on top of the stack) */ - luaL_pushfail(L); - lua_insert(L, -2); /* put before error message */ - return 2; /* return fail plus error message */ - } -} - - -static int luaB_loadfile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); - int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); - return load_aux(L, status, env); -} - - -/* -** {====================================================== -** Generic Read function -** ======================================================= -*/ - - -/* -** reserved slot, above all arguments, to hold a copy of the returned -** string to avoid it being collected while parsed. 'load' has four -** optional arguments (chunk, source name, mode, and environment). -*/ -#define RESERVEDSLOT 5 - - -/* -** Reader for generic 'load' function: 'lua_load' uses the -** stack for internal stuff, so the reader cannot change the -** stack top. Instead, it keeps its resulting string in a -** reserved slot inside the stack. -*/ -static const char *generic_reader (lua_State *L, void *ud, size_t *size) { - (void)(ud); /* not used */ - luaL_checkstack(L, 2, "too many nested functions"); - lua_pushvalue(L, 1); /* get function */ - lua_call(L, 0, 1); /* call it */ - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* pop result */ - *size = 0; - return NULL; - } - else if (l_unlikely(!lua_isstring(L, -1))) - luaL_error(L, "reader function must return a string"); - lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ - return lua_tolstring(L, RESERVEDSLOT, size); -} - - -static int luaB_load (lua_State *L) { - int status; - size_t l; - const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); - int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ - if (s != NULL) { /* loading a string? */ - const char *chunkname = luaL_optstring(L, 2, s); - status = luaL_loadbufferx(L, s, l, chunkname, mode); - } - else { /* loading from a reader function */ - const char *chunkname = luaL_optstring(L, 2, "=(load)"); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, RESERVEDSLOT); /* create reserved slot */ - status = lua_load(L, generic_reader, NULL, chunkname, mode); - } - return load_aux(L, status, env); -} - -/* }====================================================== */ - - -static int dofilecont (lua_State *L, int d1, lua_KContext d2) { - (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ - return lua_gettop(L) - 1; -} - - -static int luaB_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - lua_settop(L, 1); - if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) - return lua_error(L); - lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L, 0, 0); -} - - -static int luaB_assert (lua_State *L) { - if (l_likely(lua_toboolean(L, 1))) /* condition is true? */ - return lua_gettop(L); /* return all arguments */ - else { /* error */ - luaL_checkany(L, 1); /* there must be a condition */ - lua_remove(L, 1); /* remove it */ - lua_pushliteral(L, "assertion failed!"); /* default message */ - lua_settop(L, 1); /* leave only message (default if no other one) */ - return luaB_error(L); /* call 'error' */ - } -} - - -static int luaB_select (lua_State *L) { - int n = lua_gettop(L); - if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { - lua_pushinteger(L, n-1); - return 1; - } - else { - lua_Integer i = luaL_checkinteger(L, 1); - if (i < 0) i = n + i; - else if (i > n) i = n; - luaL_argcheck(L, 1 <= i, 1, "index out of range"); - return n - (int)i; - } -} - - -/* -** Continuation function for 'pcall' and 'xpcall'. Both functions -** already pushed a 'true' before doing the call, so in case of success -** 'finishpcall' only has to return everything in the stack minus -** 'extra' values (where 'extra' is exactly the number of items to be -** ignored). -*/ -static int finishpcall (lua_State *L, int status, lua_KContext extra) { - if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */ - lua_pushboolean(L, 0); /* first result (false) */ - lua_pushvalue(L, -2); /* error message */ - return 2; /* return false, msg */ - } - else - return lua_gettop(L) - (int)extra; /* return all results */ -} - - -static int luaB_pcall (lua_State *L) { - int status; - luaL_checkany(L, 1); - lua_pushboolean(L, 1); /* first result if no errors */ - lua_insert(L, 1); /* put it in place */ - status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall); - return finishpcall(L, status, 0); -} - - -/* -** Do a protected call with error handling. After 'lua_rotate', the -** stack will have ; so, the function passes -** 2 to 'finishpcall' to skip the 2 first values when returning results. -*/ -static int luaB_xpcall (lua_State *L) { - int status; - int n = lua_gettop(L); - luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */ - lua_pushboolean(L, 1); /* first result */ - lua_pushvalue(L, 1); /* function */ - lua_rotate(L, 3, 2); /* move them below function's arguments */ - status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall); - return finishpcall(L, status, 2); -} - - -static int luaB_tostring (lua_State *L) { - luaL_checkany(L, 1); - luaL_tolstring(L, 1, NULL); - return 1; -} - - -static const luaL_Reg base_funcs[] = { - {"assert", luaB_assert}, - {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, - {"error", luaB_error}, - {"getmetatable", luaB_getmetatable}, - {"ipairs", luaB_ipairs}, - {"loadfile", luaB_loadfile}, - {"load", luaB_load}, - {"next", luaB_next}, - {"pairs", luaB_pairs}, - {"pcall", luaB_pcall}, - {"print", luaB_print}, - {"warn", luaB_warn}, - {"rawequal", luaB_rawequal}, - {"rawlen", luaB_rawlen}, - {"rawget", luaB_rawget}, - {"rawset", luaB_rawset}, - {"select", luaB_select}, - {"setmetatable", luaB_setmetatable}, - {"tonumber", luaB_tonumber}, - {"tostring", luaB_tostring}, - {"type", luaB_type}, - {"xpcall", luaB_xpcall}, - /* placeholders */ - {LUA_GNAME, NULL}, - {"_VERSION", NULL}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_base (lua_State *L) { - /* open lib into global table */ - lua_pushglobaltable(L); - luaL_setfuncs(L, base_funcs, 0); - /* set global _G */ - lua_pushvalue(L, -1); - lua_setfield(L, -2, LUA_GNAME); - /* set global _VERSION */ - lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, -2, "_VERSION"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.c deleted file mode 100644 index 06425a1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.c +++ /dev/null @@ -1,1832 +0,0 @@ -/* -** $Id: lcode.c $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - -#define lcode_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include -#include - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstring.h" -#include "ltable.h" -#include "lvm.h" - - -/* Maximum number of registers in a Lua function (must fit in 8 bits) */ -#define MAXREGS 255 - - -#define hasjumps(e) ((e)->t != (e)->f) - - -static int codesJ (FuncState *fs, OpCode o, int sj, int k); - - - -/* semantic error */ -l_noret luaK_semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove "near " from final message */ - luaX_syntaxerror(ls, msg); -} - - -/* -** If expression is a numeric constant, fills 'v' with its value -** and returns 1. Otherwise, returns 0. -*/ -static int tonumeral (const expdesc *e, TValue *v) { - if (hasjumps(e)) - return 0; /* not a numeral */ - switch (e->k) { - case VKINT: - if (v) setivalue(v, e->u.ival); - return 1; - case VKFLT: - if (v) setfltvalue(v, e->u.nval); - return 1; - default: return 0; - } -} - - -/* -** Get the constant value from a constant expression -*/ -static TValue *const2val (FuncState *fs, const expdesc *e) { - lua_assert(e->k == VCONST); - return &fs->ls->dyd->actvar.arr[e->u.info].k; -} - - -/* -** If expression is a constant, fills 'v' with its value -** and returns 1. Otherwise, returns 0. -*/ -int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { - if (hasjumps(e)) - return 0; /* not a constant */ - switch (e->k) { - case VFALSE: - setbfvalue(v); - return 1; - case VTRUE: - setbtvalue(v); - return 1; - case VNIL: - setnilvalue(v); - return 1; - case VKSTR: { - setsvalue(fs->ls->L, v, e->u.strval); - return 1; - } - case VCONST: { - setobj(fs->ls->L, v, const2val(fs, e)); - return 1; - } - default: return tonumeral(e, v); - } -} - - -/* -** Return the previous instruction of the current code. If there -** may be a jump target between the current instruction and the -** previous one, return an invalid instruction (to avoid wrong -** optimizations). -*/ -static Instruction *previousinstruction (FuncState *fs) { - static const Instruction invalidinstruction = ~(Instruction)0; - if (fs->pc > fs->lasttarget) - return &fs->f->code[fs->pc - 1]; /* previous instruction */ - else - return cast(Instruction*, &invalidinstruction); -} - - -/* -** Create a OP_LOADNIL instruction, but try to optimize: if the previous -** instruction is also OP_LOADNIL and ranges are compatible, adjust -** range of previous instruction instead of emitting a new one. (For -** instance, 'local a; local b' will generate a single opcode.) -*/ -void luaK_nil (FuncState *fs, int from, int n) { - int l = from + n - 1; /* last register to set nil */ - Instruction *previous = previousinstruction(fs); - if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ - int pfrom = GETARG_A(*previous); /* get previous range */ - int pl = pfrom + GETARG_B(*previous); - if ((pfrom <= from && from <= pl + 1) || - (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ - if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ - if (pl > l) l = pl; /* l = max(l, pl) */ - SETARG_A(*previous, from); - SETARG_B(*previous, l - from); - return; - } /* else go through */ - } - luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ -} - - -/* -** Gets the destination address of a jump instruction. Used to traverse -** a list of jumps. -*/ -static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sJ(fs->f->code[pc]); - if (offset == NO_JUMP) /* point to itself represents end of list */ - return NO_JUMP; /* end of list */ - else - return (pc+1)+offset; /* turn offset into absolute position */ -} - - -/* -** Fix jump instruction at position 'pc' to jump to 'dest'. -** (Jump addresses are relative in Lua) -*/ -static void fixjump (FuncState *fs, int pc, int dest) { - Instruction *jmp = &fs->f->code[pc]; - int offset = dest - (pc + 1); - lua_assert(dest != NO_JUMP); - if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ)) - luaX_syntaxerror(fs->ls, "control structure too long"); - lua_assert(GET_OPCODE(*jmp) == OP_JMP); - SETARG_sJ(*jmp, offset); -} - - -/* -** Concatenate jump-list 'l2' into jump-list 'l1' -*/ -void luaK_concat (FuncState *fs, int *l1, int l2) { - if (l2 == NO_JUMP) return; /* nothing to concatenate? */ - else if (*l1 == NO_JUMP) /* no original list? */ - *l1 = l2; /* 'l1' points to 'l2' */ - else { - int list = *l1; - int next; - while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ - list = next; - fixjump(fs, list, l2); /* last element links to 'l2' */ - } -} - - -/* -** Create a jump instruction and return its position, so its destination -** can be fixed later (with 'fixjump'). -*/ -int luaK_jump (FuncState *fs) { - return codesJ(fs, OP_JMP, NO_JUMP, 0); -} - - -/* -** Code a 'return' instruction -*/ -void luaK_ret (FuncState *fs, int first, int nret) { - OpCode op; - switch (nret) { - case 0: op = OP_RETURN0; break; - case 1: op = OP_RETURN1; break; - default: op = OP_RETURN; break; - } - luaK_codeABC(fs, op, first, nret + 1, 0); -} - - -/* -** Code a "conditional jump", that is, a test or comparison opcode -** followed by a jump. Return jump position. -*/ -static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { - luaK_codeABCk(fs, op, A, B, C, k); - return luaK_jump(fs); -} - - -/* -** returns current 'pc' and marks it as a jump target (to avoid wrong -** optimizations with consecutive instructions not in the same basic block). -*/ -int luaK_getlabel (FuncState *fs) { - fs->lasttarget = fs->pc; - return fs->pc; -} - - -/* -** Returns the position of the instruction "controlling" a given -** jump (that is, its condition), or the jump itself if it is -** unconditional. -*/ -static Instruction *getjumpcontrol (FuncState *fs, int pc) { - Instruction *pi = &fs->f->code[pc]; - if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) - return pi-1; - else - return pi; -} - - -/* -** Patch destination register for a TESTSET instruction. -** If instruction in position 'node' is not a TESTSET, return 0 ("fails"). -** Otherwise, if 'reg' is not 'NO_REG', set it as the destination -** register. Otherwise, change instruction to a simple 'TEST' (produces -** no register value) -*/ -static int patchtestreg (FuncState *fs, int node, int reg) { - Instruction *i = getjumpcontrol(fs, node); - if (GET_OPCODE(*i) != OP_TESTSET) - return 0; /* cannot patch other instructions */ - if (reg != NO_REG && reg != GETARG_B(*i)) - SETARG_A(*i, reg); - else { - /* no register to put value or register already has the value; - change instruction to simple test */ - *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i)); - } - return 1; -} - - -/* -** Traverse a list of tests ensuring no one produces a value -*/ -static void removevalues (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) - patchtestreg(fs, list, NO_REG); -} - - -/* -** Traverse a list of tests, patching their destination address and -** registers: tests producing values jump to 'vtarget' (and put their -** values in 'reg'), other tests jump to 'dtarget'. -*/ -static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, - int dtarget) { - while (list != NO_JUMP) { - int next = getjump(fs, list); - if (patchtestreg(fs, list, reg)) - fixjump(fs, list, vtarget); - else - fixjump(fs, list, dtarget); /* jump to default target */ - list = next; - } -} - - -/* -** Path all jumps in 'list' to jump to 'target'. -** (The assert means that we cannot fix a jump to a forward address -** because we only know addresses once code is generated.) -*/ -void luaK_patchlist (FuncState *fs, int list, int target) { - lua_assert(target <= fs->pc); - patchlistaux(fs, list, target, NO_REG, target); -} - - -void luaK_patchtohere (FuncState *fs, int list) { - int hr = luaK_getlabel(fs); /* mark "here" as a jump target */ - luaK_patchlist(fs, list, hr); -} - - -/* limit for difference between lines in relative line info. */ -#define LIMLINEDIFF 0x80 - - -/* -** Save line info for a new instruction. If difference from last line -** does not fit in a byte, of after that many instructions, save a new -** absolute line info; (in that case, the special value 'ABSLINEINFO' -** in 'lineinfo' signals the existence of this absolute information.) -** Otherwise, store the difference from last line in 'lineinfo'. -*/ -static void savelineinfo (FuncState *fs, Proto *f, int line) { - int linedif = line - fs->previousline; - int pc = fs->pc - 1; /* last instruction coded */ - if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { - luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, - f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); - f->abslineinfo[fs->nabslineinfo].pc = pc; - f->abslineinfo[fs->nabslineinfo++].line = line; - linedif = ABSLINEINFO; /* signal that there is absolute information */ - fs->iwthabs = 1; /* restart counter */ - } - luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, - MAX_INT, "opcodes"); - f->lineinfo[pc] = linedif; - fs->previousline = line; /* last line saved */ -} - - -/* -** Remove line information from the last instruction. -** If line information for that instruction is absolute, set 'iwthabs' -** above its max to force the new (replacing) instruction to have -** absolute line info, too. -*/ -static void removelastlineinfo (FuncState *fs) { - Proto *f = fs->f; - int pc = fs->pc - 1; /* last instruction coded */ - if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ - fs->previousline -= f->lineinfo[pc]; /* correct last line saved */ - fs->iwthabs--; /* undo previous increment */ - } - else { /* absolute line information */ - lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc); - fs->nabslineinfo--; /* remove it */ - fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ - } -} - - -/* -** Remove the last instruction created, correcting line information -** accordingly. -*/ -static void removelastinstruction (FuncState *fs) { - removelastlineinfo(fs); - fs->pc--; -} - - -/* -** Emit instruction 'i', checking for array sizes and saving also its -** line information. Return 'i' position. -*/ -int luaK_code (FuncState *fs, Instruction i) { - Proto *f = fs->f; - /* put new instruction in code array */ - luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, - MAX_INT, "opcodes"); - f->code[fs->pc++] = i; - savelineinfo(fs, f, fs->ls->lastline); - return fs->pc - 1; /* index of new instruction */ -} - - -/* -** Format and emit an 'iABC' instruction. (Assertions check consistency -** of parameters versus opcode.) -*/ -int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { - lua_assert(getOpMode(o) == iABC); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && - c <= MAXARG_C && (k & ~1) == 0); - return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); -} - - -/* -** Format and emit an 'iABx' instruction. -*/ -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { - lua_assert(getOpMode(o) == iABx); - lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, bc)); -} - - -/* -** Format and emit an 'iAsBx' instruction. -*/ -int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { - unsigned int b = bc + OFFSET_sBx; - lua_assert(getOpMode(o) == iAsBx); - lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, b)); -} - - -/* -** Format and emit an 'isJ' instruction. -*/ -static int codesJ (FuncState *fs, OpCode o, int sj, int k) { - unsigned int j = sj + OFFSET_sJ; - lua_assert(getOpMode(o) == isJ); - lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); - return luaK_code(fs, CREATE_sJ(o, j, k)); -} - - -/* -** Emit an "extra argument" instruction (format 'iAx') -*/ -static int codeextraarg (FuncState *fs, int a) { - lua_assert(a <= MAXARG_Ax); - return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); -} - - -/* -** Emit a "load constant" instruction, using either 'OP_LOADK' -** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' -** instruction with "extra argument". -*/ -static int luaK_codek (FuncState *fs, int reg, int k) { - if (k <= MAXARG_Bx) - return luaK_codeABx(fs, OP_LOADK, reg, k); - else { - int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); - codeextraarg(fs, k); - return p; - } -} - - -/* -** Check register-stack level, keeping track of its maximum size -** in field 'maxstacksize' -*/ -void luaK_checkstack (FuncState *fs, int n) { - int newstack = fs->freereg + n; - if (newstack > fs->f->maxstacksize) { - if (newstack >= MAXREGS) - luaX_syntaxerror(fs->ls, - "function or expression needs too many registers"); - fs->f->maxstacksize = cast_byte(newstack); - } -} - - -/* -** Reserve 'n' registers in register stack -*/ -void luaK_reserveregs (FuncState *fs, int n) { - luaK_checkstack(fs, n); - fs->freereg += n; -} - - -/* -** Free register 'reg', if it is neither a constant index nor -** a local variable. -) -*/ -static void freereg (FuncState *fs, int reg) { - if (reg >= luaY_nvarstack(fs)) { - fs->freereg--; - lua_assert(reg == fs->freereg); - } -} - - -/* -** Free two registers in proper order -*/ -static void freeregs (FuncState *fs, int r1, int r2) { - if (r1 > r2) { - freereg(fs, r1); - freereg(fs, r2); - } - else { - freereg(fs, r2); - freereg(fs, r1); - } -} - - -/* -** Free register used by expression 'e' (if any) -*/ -static void freeexp (FuncState *fs, expdesc *e) { - if (e->k == VNONRELOC) - freereg(fs, e->u.info); -} - - -/* -** Free registers used by expressions 'e1' and 'e2' (if any) in proper -** order. -*/ -static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { - int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; - int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; - freeregs(fs, r1, r2); -} - - -/* -** Add constant 'v' to prototype's list of constants (field 'k'). -** Use scanner's table to cache position of constants in constant list -** and try to reuse constants. Because some values should not be used -** as keys (nil cannot be a key, integer keys can collapse with float -** keys), the caller must provide a useful 'key' for indexing the cache. -** Note that all functions share the same table, so entering or exiting -** a function can make some indices wrong. -*/ -static int addk (FuncState *fs, TValue *key, TValue *v) { - TValue val; - lua_State *L = fs->ls->L; - Proto *f = fs->f; - const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */ - int k, oldsize; - if (ttisinteger(idx)) { /* is there an index there? */ - k = cast_int(ivalue(idx)); - /* correct value? (warning: must distinguish floats from integers!) */ - if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && - luaV_rawequalobj(&f->k[k], v)) - return k; /* reuse index */ - } - /* constant not found; create a new entry */ - oldsize = f->sizek; - k = fs->nk; - /* numerical value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ - setivalue(&val, k); - luaH_finishset(L, fs->ls->h, key, idx, &val); - luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); - setobj(L, &f->k[k], v); - fs->nk++; - luaC_barrier(L, f, v); - return k; -} - - -/* -** Add a string to list of constants and return its index. -*/ -static int stringK (FuncState *fs, TString *s) { - TValue o; - setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); /* use string itself as key */ -} - - -/* -** Add an integer to list of constants and return its index. -*/ -static int luaK_intK (FuncState *fs, lua_Integer n) { - TValue o; - setivalue(&o, n); - return addk(fs, &o, &o); /* use integer itself as key */ -} - -/* -** Add a float to list of constants and return its index. Floats -** with integral values need a different key, to avoid collision -** with actual integers. To that, we add to the number its smaller -** power-of-two fraction that is still significant in its scale. -** For doubles, that would be 1/2^52. -** (This method is not bulletproof: there may be another float -** with that value, and for floats larger than 2^53 the result is -** still an integer. At worst, this only wastes an entry with -** a duplicate.) -*/ -static int luaK_numberK (FuncState *fs, lua_Number r) { - TValue o; - lua_Integer ik; - setfltvalue(&o, r); - if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ - return addk(fs, &o, &o); /* use number itself as key */ - else { /* must build an alternative key */ - const int nbm = l_floatatt(MANT_DIG); - const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); - const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ - TValue kv; - setfltvalue(&kv, k); - /* result is not an integral value, unless value is too large */ - lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || - l_mathop(fabs)(r) >= l_mathop(1e6)); - return addk(fs, &kv, &o); - } -} - - -/* -** Add a false to list of constants and return its index. -*/ -static int boolF (FuncState *fs) { - TValue o; - setbfvalue(&o); - return addk(fs, &o, &o); /* use boolean itself as key */ -} - - -/* -** Add a true to list of constants and return its index. -*/ -static int boolT (FuncState *fs) { - TValue o; - setbtvalue(&o); - return addk(fs, &o, &o); /* use boolean itself as key */ -} - - -/* -** Add nil to list of constants and return its index. -*/ -static int nilK (FuncState *fs) { - TValue k, v; - setnilvalue(&v); - /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->ls->h); - return addk(fs, &k, &v); -} - - -/* -** Check whether 'i' can be stored in an 'sC' operand. Equivalent to -** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of -** overflows in the hidden addition inside 'int2sC'. -*/ -static int fitsC (lua_Integer i) { - return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C)); -} - - -/* -** Check whether 'i' can be stored in an 'sBx' operand. -*/ -static int fitsBx (lua_Integer i) { - return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx); -} - - -void luaK_int (FuncState *fs, int reg, lua_Integer i) { - if (fitsBx(i)) - luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); - else - luaK_codek(fs, reg, luaK_intK(fs, i)); -} - - -static void luaK_float (FuncState *fs, int reg, lua_Number f) { - lua_Integer fi; - if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) - luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); - else - luaK_codek(fs, reg, luaK_numberK(fs, f)); -} - - -/* -** Convert a constant in 'v' into an expression description 'e' -*/ -static void const2exp (TValue *v, expdesc *e) { - switch (ttypetag(v)) { - case LUA_VNUMINT: - e->k = VKINT; e->u.ival = ivalue(v); - break; - case LUA_VNUMFLT: - e->k = VKFLT; e->u.nval = fltvalue(v); - break; - case LUA_VFALSE: - e->k = VFALSE; - break; - case LUA_VTRUE: - e->k = VTRUE; - break; - case LUA_VNIL: - e->k = VNIL; - break; - case LUA_VSHRSTR: case LUA_VLNGSTR: - e->k = VKSTR; e->u.strval = tsvalue(v); - break; - default: lua_assert(0); - } -} - - -/* -** Fix an expression to return the number of results 'nresults'. -** 'e' must be a multi-ret expression (function call or vararg). -*/ -void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { - Instruction *pc = &getinstruction(fs, e); - if (e->k == VCALL) /* expression is an open function call? */ - SETARG_C(*pc, nresults + 1); - else { - lua_assert(e->k == VVARARG); - SETARG_C(*pc, nresults + 1); - SETARG_A(*pc, fs->freereg); - luaK_reserveregs(fs, 1); - } -} - - -/* -** Convert a VKSTR to a VK -*/ -static void str2K (FuncState *fs, expdesc *e) { - lua_assert(e->k == VKSTR); - e->u.info = stringK(fs, e->u.strval); - e->k = VK; -} - - -/* -** Fix an expression to return one result. -** If expression is not a multi-ret expression (function call or -** vararg), it already returns one result, so nothing needs to be done. -** Function calls become VNONRELOC expressions (as its result comes -** fixed in the base register of the call), while vararg expressions -** become VRELOC (as OP_VARARG puts its results where it wants). -** (Calls are created returning one result, so that does not need -** to be fixed.) -*/ -void luaK_setoneret (FuncState *fs, expdesc *e) { - if (e->k == VCALL) { /* expression is an open function call? */ - /* already returns 1 value */ - lua_assert(GETARG_C(getinstruction(fs, e)) == 2); - e->k = VNONRELOC; /* result has fixed position */ - e->u.info = GETARG_A(getinstruction(fs, e)); - } - else if (e->k == VVARARG) { - SETARG_C(getinstruction(fs, e), 2); - e->k = VRELOC; /* can relocate its simple result */ - } -} - - -/* -** Ensure that expression 'e' is not a variable (nor a ). -** (Expression still may have jump lists.) -*/ -void luaK_dischargevars (FuncState *fs, expdesc *e) { - switch (e->k) { - case VCONST: { - const2exp(const2val(fs, e), e); - break; - } - case VLOCAL: { /* already in a register */ - e->u.info = e->u.var.ridx; - e->k = VNONRELOC; /* becomes a non-relocatable value */ - break; - } - case VUPVAL: { /* move value to some (pending) register */ - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); - e->k = VRELOC; - break; - } - case VINDEXUP: { - e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOC; - break; - } - case VINDEXI: { - freereg(fs, e->u.ind.t); - e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOC; - break; - } - case VINDEXSTR: { - freereg(fs, e->u.ind.t); - e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOC; - break; - } - case VINDEXED: { - freeregs(fs, e->u.ind.t, e->u.ind.idx); - e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOC; - break; - } - case VVARARG: case VCALL: { - luaK_setoneret(fs, e); - break; - } - default: break; /* there is one value available (somewhere) */ - } -} - - -/* -** Ensure expression value is in register 'reg', making 'e' a -** non-relocatable expression. -** (Expression still may have jump lists.) -*/ -static void discharge2reg (FuncState *fs, expdesc *e, int reg) { - luaK_dischargevars(fs, e); - switch (e->k) { - case VNIL: { - luaK_nil(fs, reg, 1); - break; - } - case VFALSE: { - luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); - break; - } - case VTRUE: { - luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); - break; - } - case VKSTR: { - str2K(fs, e); - } /* FALLTHROUGH */ - case VK: { - luaK_codek(fs, reg, e->u.info); - break; - } - case VKFLT: { - luaK_float(fs, reg, e->u.nval); - break; - } - case VKINT: { - luaK_int(fs, reg, e->u.ival); - break; - } - case VRELOC: { - Instruction *pc = &getinstruction(fs, e); - SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ - break; - } - case VNONRELOC: { - if (reg != e->u.info) - luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); - break; - } - default: { - lua_assert(e->k == VJMP); - return; /* nothing to do... */ - } - } - e->u.info = reg; - e->k = VNONRELOC; -} - - -/* -** Ensure expression value is in a register, making 'e' a -** non-relocatable expression. -** (Expression still may have jump lists.) -*/ -static void discharge2anyreg (FuncState *fs, expdesc *e) { - if (e->k != VNONRELOC) { /* no fixed register yet? */ - luaK_reserveregs(fs, 1); /* get a register */ - discharge2reg(fs, e, fs->freereg-1); /* put value there */ - } -} - - -static int code_loadbool (FuncState *fs, int A, OpCode op) { - luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, op, A, 0, 0); -} - - -/* -** check whether list has any jump that do not produce a value -** or produce an inverted value -*/ -static int need_value (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TESTSET) return 1; - } - return 0; /* not found */ -} - - -/* -** Ensures final expression result (which includes results from its -** jump lists) is in register 'reg'. -** If expression has jumps, need to patch these jumps either to -** its final position or to "load" instructions (for those tests -** that do not produce values). -*/ -static void exp2reg (FuncState *fs, expdesc *e, int reg) { - discharge2reg(fs, e, reg); - if (e->k == VJMP) /* expression itself is a test? */ - luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ - if (hasjumps(e)) { - int final; /* position after whole expression */ - int p_f = NO_JUMP; /* position of an eventual LOAD false */ - int p_t = NO_JUMP; /* position of an eventual LOAD true */ - if (need_value(fs, e->t) || need_value(fs, e->f)) { - int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */ - p_t = code_loadbool(fs, reg, OP_LOADTRUE); - /* jump around these booleans if 'e' is not a test */ - luaK_patchtohere(fs, fj); - } - final = luaK_getlabel(fs); - patchlistaux(fs, e->f, final, reg, p_f); - patchlistaux(fs, e->t, final, reg, p_t); - } - e->f = e->t = NO_JUMP; - e->u.info = reg; - e->k = VNONRELOC; -} - - -/* -** Ensures final expression result is in next available register. -*/ -void luaK_exp2nextreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - freeexp(fs, e); - luaK_reserveregs(fs, 1); - exp2reg(fs, e, fs->freereg - 1); -} - - -/* -** Ensures final expression result is in some (any) register -** and return that register. -*/ -int luaK_exp2anyreg (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); - if (e->k == VNONRELOC) { /* expression already has a register? */ - if (!hasjumps(e)) /* no jumps? */ - return e->u.info; /* result is already in a register */ - if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */ - exp2reg(fs, e, e->u.info); /* put final result in it */ - return e->u.info; - } - /* else expression has jumps and cannot change its register - to hold the jump values, because it is a local variable. - Go through to the default case. */ - } - luaK_exp2nextreg(fs, e); /* default: use next available register */ - return e->u.info; -} - - -/* -** Ensures final expression result is either in a register -** or in an upvalue. -*/ -void luaK_exp2anyregup (FuncState *fs, expdesc *e) { - if (e->k != VUPVAL || hasjumps(e)) - luaK_exp2anyreg(fs, e); -} - - -/* -** Ensures final expression result is either in a register -** or it is a constant. -*/ -void luaK_exp2val (FuncState *fs, expdesc *e) { - if (hasjumps(e)) - luaK_exp2anyreg(fs, e); - else - luaK_dischargevars(fs, e); -} - - -/* -** Try to make 'e' a K expression with an index in the range of R/K -** indices. Return true iff succeeded. -*/ -static int luaK_exp2K (FuncState *fs, expdesc *e) { - if (!hasjumps(e)) { - int info; - switch (e->k) { /* move constants to 'k' */ - case VTRUE: info = boolT(fs); break; - case VFALSE: info = boolF(fs); break; - case VNIL: info = nilK(fs); break; - case VKINT: info = luaK_intK(fs, e->u.ival); break; - case VKFLT: info = luaK_numberK(fs, e->u.nval); break; - case VKSTR: info = stringK(fs, e->u.strval); break; - case VK: info = e->u.info; break; - default: return 0; /* not a constant */ - } - if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */ - e->k = VK; /* make expression a 'K' expression */ - e->u.info = info; - return 1; - } - } - /* else, expression doesn't fit; leave it unchanged */ - return 0; -} - - -/* -** Ensures final expression result is in a valid R/K index -** (that is, it is either in a register or in 'k' with an index -** in the range of R/K indices). -** Returns 1 iff expression is K. -*/ -int luaK_exp2RK (FuncState *fs, expdesc *e) { - if (luaK_exp2K(fs, e)) - return 1; - else { /* not a constant in the right range: put it in a register */ - luaK_exp2anyreg(fs, e); - return 0; - } -} - - -static void codeABRK (FuncState *fs, OpCode o, int a, int b, - expdesc *ec) { - int k = luaK_exp2RK(fs, ec); - luaK_codeABCk(fs, o, a, b, ec->u.info, k); -} - - -/* -** Generate code to store result of expression 'ex' into variable 'var'. -*/ -void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { - switch (var->k) { - case VLOCAL: { - freeexp(fs, ex); - exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */ - return; - } - case VUPVAL: { - int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); - break; - } - case VINDEXUP: { - codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex); - break; - } - case VINDEXI: { - codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex); - break; - } - case VINDEXSTR: { - codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); - break; - } - case VINDEXED: { - codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); - break; - } - default: lua_assert(0); /* invalid var kind to store */ - } - freeexp(fs, ex); -} - - -/* -** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). -*/ -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { - int ereg; - luaK_exp2anyreg(fs, e); - ereg = e->u.info; /* register where 'e' was placed */ - freeexp(fs, e); - e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; /* self expression has a fixed register */ - luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ - codeABRK(fs, OP_SELF, e->u.info, ereg, key); - freeexp(fs, key); -} - - -/* -** Negate condition 'e' (where 'e' is a comparison). -*/ -static void negatecondition (FuncState *fs, expdesc *e) { - Instruction *pc = getjumpcontrol(fs, e->u.info); - lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && - GET_OPCODE(*pc) != OP_TEST); - SETARG_k(*pc, (GETARG_k(*pc) ^ 1)); -} - - -/* -** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond' -** is true, code will jump if 'e' is true.) Return jump position. -** Optimize when 'e' is 'not' something, inverting the condition -** and removing the 'not'. -*/ -static int jumponcond (FuncState *fs, expdesc *e, int cond) { - if (e->k == VRELOC) { - Instruction ie = getinstruction(fs, e); - if (GET_OPCODE(ie) == OP_NOT) { - removelastinstruction(fs); /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); - } - /* else go through */ - } - discharge2anyreg(fs, e); - freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); -} - - -/* -** Emit code to go through if 'e' is true, jump otherwise. -*/ -void luaK_goiftrue (FuncState *fs, expdesc *e) { - int pc; /* pc of new jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VJMP: { /* condition? */ - negatecondition(fs, e); /* jump when it is false */ - pc = e->u.info; /* save jump position */ - break; - } - case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { - pc = NO_JUMP; /* always true; do nothing */ - break; - } - default: { - pc = jumponcond(fs, e, 0); /* jump when false */ - break; - } - } - luaK_concat(fs, &e->f, pc); /* insert new jump in false list */ - luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */ - e->t = NO_JUMP; -} - - -/* -** Emit code to go through if 'e' is false, jump otherwise. -*/ -void luaK_goiffalse (FuncState *fs, expdesc *e) { - int pc; /* pc of new jump */ - luaK_dischargevars(fs, e); - switch (e->k) { - case VJMP: { - pc = e->u.info; /* already jump if true */ - break; - } - case VNIL: case VFALSE: { - pc = NO_JUMP; /* always false; do nothing */ - break; - } - default: { - pc = jumponcond(fs, e, 1); /* jump if true */ - break; - } - } - luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */ - luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */ - e->f = NO_JUMP; -} - - -/* -** Code 'not e', doing constant folding. -*/ -static void codenot (FuncState *fs, expdesc *e) { - switch (e->k) { - case VNIL: case VFALSE: { - e->k = VTRUE; /* true == not nil == not false */ - break; - } - case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { - e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ - break; - } - case VJMP: { - negatecondition(fs, e); - break; - } - case VRELOC: - case VNONRELOC: { - discharge2anyreg(fs, e); - freeexp(fs, e); - e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); - e->k = VRELOC; - break; - } - default: lua_assert(0); /* cannot happen */ - } - /* interchange true and false lists */ - { int temp = e->f; e->f = e->t; e->t = temp; } - removevalues(fs, e->f); /* values are useless when negated */ - removevalues(fs, e->t); -} - - -/* -** Check whether expression 'e' is a small literal string -*/ -static int isKstr (FuncState *fs, expdesc *e) { - return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && - ttisshrstring(&fs->f->k[e->u.info])); -} - -/* -** Check whether expression 'e' is a literal integer. -*/ -int luaK_isKint (expdesc *e) { - return (e->k == VKINT && !hasjumps(e)); -} - - -/* -** Check whether expression 'e' is a literal integer in -** proper range to fit in register C -*/ -static int isCint (expdesc *e) { - return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); -} - - -/* -** Check whether expression 'e' is a literal integer in -** proper range to fit in register sC -*/ -static int isSCint (expdesc *e) { - return luaK_isKint(e) && fitsC(e->u.ival); -} - - -/* -** Check whether expression 'e' is a literal integer or float in -** proper range to fit in a register (sB or sC). -*/ -static int isSCnumber (expdesc *e, int *pi, int *isfloat) { - lua_Integer i; - if (e->k == VKINT) - i = e->u.ival; - else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq)) - *isfloat = 1; - else - return 0; /* not a number */ - if (!hasjumps(e) && fitsC(i)) { - *pi = int2sC(cast_int(i)); - return 1; - } - else - return 0; -} - - -/* -** Create expression 't[k]'. 't' must have its final result already in a -** register or upvalue. Upvalues can only be indexed by literal strings. -** Keys can be literal strings in the constant table or arbitrary -** values in registers. -*/ -void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - if (k->k == VKSTR) - str2K(fs, k); - lua_assert(!hasjumps(t) && - (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); - if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ - luaK_exp2anyreg(fs, t); /* put it in a register */ - if (t->k == VUPVAL) { - t->u.ind.t = t->u.info; /* upvalue index */ - t->u.ind.idx = k->u.info; /* literal string */ - t->k = VINDEXUP; - } - else { - /* register index of the table */ - t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; - if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal string */ - t->k = VINDEXSTR; - } - else if (isCint(k)) { - t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */ - t->k = VINDEXI; - } - else { - t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ - t->k = VINDEXED; - } - } -} - - -/* -** Return false if folding can raise an error. -** Bitwise operations need operands convertible to integers; division -** operations cannot have 0 as divisor. -*/ -static int validop (int op, TValue *v1, TValue *v2) { - switch (op) { - case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: - case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ - lua_Integer i; - return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) && - luaV_tointegerns(v2, &i, LUA_FLOORN2I)); - } - case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ - return (nvalue(v2) != 0); - default: return 1; /* everything else is valid */ - } -} - - -/* -** Try to "constant-fold" an operation; return 1 iff successful. -** (In this case, 'e1' has the final result.) -*/ -static int constfolding (FuncState *fs, int op, expdesc *e1, - const expdesc *e2) { - TValue v1, v2, res; - if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) - return 0; /* non-numeric operands or not safe to fold */ - luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ - if (ttisinteger(&res)) { - e1->k = VKINT; - e1->u.ival = ivalue(&res); - } - else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */ - lua_Number n = fltvalue(&res); - if (luai_numisnan(n) || n == 0) - return 0; - e1->k = VKFLT; - e1->u.nval = n; - } - return 1; -} - - -/* -** Emit code for unary expressions that "produce values" -** (everything but 'not'). -** Expression to produce final result will be encoded in 'e'. -*/ -static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { - int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ - freeexp(fs, e); - e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ - e->k = VRELOC; /* all those operations are relocatable */ - luaK_fixline(fs, line); -} - - -/* -** Emit code for binary expressions that "produce values" -** (everything but logical operators 'and'/'or' and comparison -** operators). -** Expression to produce final result will be encoded in 'e1'. -*/ -static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, - OpCode op, int v2, int flip, int line, - OpCode mmop, TMS event) { - int v1 = luaK_exp2anyreg(fs, e1); - int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0); - freeexps(fs, e1, e2); - e1->u.info = pc; - e1->k = VRELOC; /* all those operations are relocatable */ - luaK_fixline(fs, line); - luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */ - luaK_fixline(fs, line); -} - - -/* -** Emit code for binary expressions that "produce values" over -** two registers. -*/ -static void codebinexpval (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ - lua_assert(OP_ADD <= op && op <= OP_SHR); - finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, - cast(TMS, (op - OP_ADD) + TM_ADD)); -} - - -/* -** Code binary operators with immediate operands. -*/ -static void codebini (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int flip, int line, - TMS event) { - int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */ - lua_assert(e2->k == VKINT); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); -} - - -/* Try to code a binary operator negating its second operand. -** For the metamethod, 2nd operand must keep its original value. -*/ -static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, - OpCode op, int line, TMS event) { - if (!luaK_isKint(e2)) - return 0; /* not an integer constant */ - else { - lua_Integer i2 = e2->u.ival; - if (!(fitsC(i2) && fitsC(-i2))) - return 0; /* not in the proper range */ - else { /* operating a small integer constant */ - int v2 = cast_int(i2); - finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); - /* correct metamethod argument */ - SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); - return 1; /* successfully coded */ - } - } -} - - -static void swapexps (expdesc *e1, expdesc *e2) { - expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ -} - - -/* -** Code arithmetic operators ('+', '-', ...). If second operand is a -** constant in the proper range, use variant opcodes with K operands. -*/ -static void codearith (FuncState *fs, BinOpr opr, - expdesc *e1, expdesc *e2, int flip, int line) { - TMS event = cast(TMS, opr + TM_ADD); - if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ - int v2 = e2->u.info; /* K index */ - OpCode op = cast(OpCode, opr + OP_ADDK); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); - } - else { /* 'e2' is neither an immediate nor a K operand */ - OpCode op = cast(OpCode, opr + OP_ADD); - if (flip) - swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ - } -} - - -/* -** Code commutative operators ('+', '*'). If first operand is a -** numeric constant, change order of operands to try to use an -** immediate or K operator. -*/ -static void codecommutative (FuncState *fs, BinOpr op, - expdesc *e1, expdesc *e2, int line) { - int flip = 0; - if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ - swapexps(e1, e2); /* change order */ - flip = 1; - } - if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ - codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD); - else - codearith(fs, op, e1, e2, flip, line); -} - - -/* -** Code bitwise operations; they are all associative, so the function -** tries to put an integer constant as the 2nd operand (a K operand). -*/ -static void codebitwise (FuncState *fs, BinOpr opr, - expdesc *e1, expdesc *e2, int line) { - int flip = 0; - int v2; - OpCode op; - if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { - swapexps(e1, e2); /* 'e2' will be the constant operand */ - flip = 1; - } - else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ - op = cast(OpCode, opr + OP_ADD); - codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ - return; - } - v2 = e2->u.info; /* index in K array */ - op = cast(OpCode, opr + OP_ADDK); - lua_assert(ttisinteger(&fs->f->k[v2])); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, - cast(TMS, opr + TM_ADD)); -} - - -/* -** Emit code for order comparisons. When using an immediate operand, -** 'isfloat' tells whether the original value was a float. -*/ -static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { - int r1, r2; - int im; - int isfloat = 0; - if (isSCnumber(e2, &im, &isfloat)) { - /* use immediate operand */ - r1 = luaK_exp2anyreg(fs, e1); - r2 = im; - op = cast(OpCode, (op - OP_LT) + OP_LTI); - } - else if (isSCnumber(e1, &im, &isfloat)) { - /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ - r1 = luaK_exp2anyreg(fs, e2); - r2 = im; - op = (op == OP_LT) ? OP_GTI : OP_GEI; - } - else { /* regular case, compare two registers */ - r1 = luaK_exp2anyreg(fs, e1); - r2 = luaK_exp2anyreg(fs, e2); - } - freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); - e1->k = VJMP; -} - - -/* -** Emit code for equality comparisons ('==', '~='). -** 'e1' was already put as RK by 'luaK_infix'. -*/ -static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { - int r1, r2; - int im; - int isfloat = 0; /* not needed here, but kept for symmetry */ - OpCode op; - if (e1->k != VNONRELOC) { - lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); - swapexps(e1, e2); - } - r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */ - if (isSCnumber(e2, &im, &isfloat)) { - op = OP_EQI; - r2 = im; /* immediate operand */ - } - else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ - op = OP_EQK; - r2 = e2->u.info; /* constant index */ - } - else { - op = OP_EQ; /* will compare two registers */ - r2 = luaK_exp2anyreg(fs, e2); - } - freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ)); - e1->k = VJMP; -} - - -/* -** Apply prefix operation 'op' to expression 'e'. -*/ -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { - static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; - luaK_dischargevars(fs, e); - switch (op) { - case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, op + LUA_OPUNM, e, &ef)) - break; - /* else */ /* FALLTHROUGH */ - case OPR_LEN: - codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); - break; - case OPR_NOT: codenot(fs, e); break; - default: lua_assert(0); - } -} - - -/* -** Process 1st operand 'v' of binary operation 'op' before reading -** 2nd operand. -*/ -void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { - luaK_dischargevars(fs, v); - switch (op) { - case OPR_AND: { - luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ - break; - } - case OPR_OR: { - luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */ - break; - } - case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the stack */ - break; - } - case OPR_ADD: case OPR_SUB: - case OPR_MUL: case OPR_DIV: case OPR_IDIV: - case OPR_MOD: case OPR_POW: - case OPR_BAND: case OPR_BOR: case OPR_BXOR: - case OPR_SHL: case OPR_SHR: { - if (!tonumeral(v, NULL)) - luaK_exp2anyreg(fs, v); - /* else keep numeral, which may be folded with 2nd operand */ - break; - } - case OPR_EQ: case OPR_NE: { - if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); - /* else keep numeral, which may be an immediate operand */ - break; - } - case OPR_LT: case OPR_LE: - case OPR_GT: case OPR_GE: { - int dummy, dummy2; - if (!isSCnumber(v, &dummy, &dummy2)) - luaK_exp2anyreg(fs, v); - /* else keep numeral, which may be an immediate operand */ - break; - } - default: lua_assert(0); - } -} - -/* -** Create code for '(e1 .. e2)'. -** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))', -** because concatenation is right associative), merge both CONCATs. -*/ -static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { - Instruction *ie2 = previousinstruction(fs); - if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */ - int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */ - lua_assert(e1->u.info + 1 == GETARG_A(*ie2)); - freeexp(fs, e2); - SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */ - SETARG_B(*ie2, n + 1); /* will concatenate one more element */ - } - else { /* 'e2' is not a concatenation */ - luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */ - freeexp(fs, e2); - luaK_fixline(fs, line); - } -} - - -/* -** Finalize code for binary operation, after reading 2nd operand. -*/ -void luaK_posfix (FuncState *fs, BinOpr opr, - expdesc *e1, expdesc *e2, int line) { - luaK_dischargevars(fs, e2); - if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2)) - return; /* done by folding */ - switch (opr) { - case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ - luaK_concat(fs, &e2->f, e1->f); - *e1 = *e2; - break; - } - case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */ - luaK_concat(fs, &e2->t, e1->t); - *e1 = *e2; - break; - } - case OPR_CONCAT: { /* e1 .. e2 */ - luaK_exp2nextreg(fs, e2); - codeconcat(fs, e1, e2, line); - break; - } - case OPR_ADD: case OPR_MUL: { - codecommutative(fs, opr, e1, e2, line); - break; - } - case OPR_SUB: { - if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) - break; /* coded as (r1 + -I) */ - /* ELSE */ - } /* FALLTHROUGH */ - case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { - codearith(fs, opr, e1, e2, 0, line); - break; - } - case OPR_BAND: case OPR_BOR: case OPR_BXOR: { - codebitwise(fs, opr, e1, e2, line); - break; - } - case OPR_SHL: { - if (isSCint(e1)) { - swapexps(e1, e2); - codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ - } - else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) { - /* coded as (r1 >> -I) */; - } - else /* regular case (two registers) */ - codebinexpval(fs, OP_SHL, e1, e2, line); - break; - } - case OPR_SHR: { - if (isSCint(e2)) - codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ - else /* regular case (two registers) */ - codebinexpval(fs, OP_SHR, e1, e2, line); - break; - } - case OPR_EQ: case OPR_NE: { - codeeq(fs, opr, e1, e2); - break; - } - case OPR_LT: case OPR_LE: { - OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - codeorder(fs, op, e1, e2); - break; - } - case OPR_GT: case OPR_GE: { - /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ - OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); - swapexps(e1, e2); - codeorder(fs, op, e1, e2); - break; - } - default: lua_assert(0); - } -} - - -/* -** Change line information associated with current position, by removing -** previous info and adding it again with new line. -*/ -void luaK_fixline (FuncState *fs, int line) { - removelastlineinfo(fs); - savelineinfo(fs, fs->f, line); -} - - -void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { - Instruction *inst = &fs->f->code[pc]; - int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ - int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ - int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ - int k = (extra > 0); /* true iff needs extra argument */ - *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); - *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); -} - - -/* -** Emit a SETLIST instruction. -** 'base' is register that keeps table; -** 'nelems' is #table plus those to be stored now; -** 'tostore' is number of values (in registers 'base + 1',...) to add to -** table (or LUA_MULTRET to add up to stack top). -*/ -void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); - if (tostore == LUA_MULTRET) - tostore = 0; - if (nelems <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems); - else { - int extra = nelems / (MAXARG_C + 1); - nelems %= (MAXARG_C + 1); - luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1); - codeextraarg(fs, extra); - } - fs->freereg = base + 1; /* free registers with list values */ -} - - -/* -** return the final target of a jump (skipping jumps to jumps) -*/ -static int finaltarget (Instruction *code, int i) { - int count; - for (count = 0; count < 100; count++) { /* avoid infinite loops */ - Instruction pc = code[i]; - if (GET_OPCODE(pc) != OP_JMP) - break; - else - i += GETARG_sJ(pc) + 1; - } - return i; -} - - -/* -** Do a final pass over the code of a function, doing small peephole -** optimizations and adjustments. -*/ -void luaK_finish (FuncState *fs) { - int i; - Proto *p = fs->f; - for (i = 0; i < fs->pc; i++) { - Instruction *pc = &p->code[i]; - lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); - switch (GET_OPCODE(*pc)) { - case OP_RETURN0: case OP_RETURN1: { - if (!(fs->needclose || p->is_vararg)) - break; /* no extra work */ - /* else use OP_RETURN to do the extra work */ - SET_OPCODE(*pc, OP_RETURN); - } /* FALLTHROUGH */ - case OP_RETURN: case OP_TAILCALL: { - if (fs->needclose) - SETARG_k(*pc, 1); /* signal that it needs to close */ - if (p->is_vararg) - SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ - break; - } - case OP_JMP: { - int target = finaltarget(p->code, i); - fixjump(fs, i, target); - break; - } - default: break; - } - } -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.h deleted file mode 100644 index 3265824..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcode.h +++ /dev/null @@ -1,104 +0,0 @@ -/* -** $Id: lcode.h $ -** Code generator for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lcode_h -#define lcode_h - -#include "llex.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" - - -/* -** Marks the end of a patch list. It is an invalid value both as an absolute -** address, and as a list link (would link an element to itself). -*/ -#define NO_JUMP (-1) - - -/* -** grep "ORDER OPR" if you change these enums (ORDER OP) -*/ -typedef enum BinOpr { - /* arithmetic operators */ - OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, - OPR_DIV, OPR_IDIV, - /* bitwise operators */ - OPR_BAND, OPR_BOR, OPR_BXOR, - OPR_SHL, OPR_SHR, - /* string operator */ - OPR_CONCAT, - /* comparison operators */ - OPR_EQ, OPR_LT, OPR_LE, - OPR_NE, OPR_GT, OPR_GE, - /* logical operators */ - OPR_AND, OPR_OR, - OPR_NOBINOPR -} BinOpr; - - -/* true if operation is foldable (that is, it is arithmetic or bitwise) */ -#define foldbinop(op) ((op) <= OPR_SHR) - - -#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) - - -typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; - - -/* get (pointer to) instruction of given 'expdesc' */ -#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) - - -#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) - -#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) - -LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); -LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, - int B, int C, int k); -LUAI_FUNC int luaK_isKint (expdesc *e); -LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); -LUAI_FUNC void luaK_fixline (FuncState *fs, int line); -LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); -LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); -LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); -LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); -LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); -LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); -LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); -LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); -LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_jump (FuncState *fs); -LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); -LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); -LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); -LUAI_FUNC int luaK_getlabel (FuncState *fs); -LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); -LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); -LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, - expdesc *v2, int line); -LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, - int ra, int asize, int hsize); -LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); -LUAI_FUNC void luaK_finish (FuncState *fs); -LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcorolib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcorolib.c deleted file mode 100644 index 785a1e8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lcorolib.c +++ /dev/null @@ -1,210 +0,0 @@ -/* -** $Id: lcorolib.c $ -** Coroutine Library -** See Copyright Notice in lua.h -*/ - -#define lcorolib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -static lua_State *getco (lua_State *L) { - lua_State *co = lua_tothread(L, 1); - luaL_argexpected(L, co, 1, "thread"); - return co; -} - - -/* -** Resumes a coroutine. Returns the number of results for non-error -** cases or -1 for errors. -*/ -static int auxresume (lua_State *L, lua_State *co, int narg) { - int status, nres; - if (l_unlikely(!lua_checkstack(co, narg))) { - lua_pushliteral(L, "too many arguments to resume"); - return -1; /* error flag */ - } - lua_xmove(L, co, narg); - status = lua_resume(co, L, narg, &nres); - if (l_likely(status == LUA_OK || status == LUA_YIELD)) { - if (l_unlikely(!lua_checkstack(L, nres + 1))) { - lua_pop(co, nres); /* remove results anyway */ - lua_pushliteral(L, "too many results to resume"); - return -1; /* error flag */ - } - lua_xmove(co, L, nres); /* move yielded values */ - return nres; - } - else { - lua_xmove(co, L, 1); /* move error message */ - return -1; /* error flag */ - } -} - - -static int luaB_coresume (lua_State *L) { - lua_State *co = getco(L); - int r; - r = auxresume(L, co, lua_gettop(L) - 1); - if (l_unlikely(r < 0)) { - lua_pushboolean(L, 0); - lua_insert(L, -2); - return 2; /* return false + error message */ - } - else { - lua_pushboolean(L, 1); - lua_insert(L, -(r + 1)); - return r + 1; /* return true + 'resume' returns */ - } -} - - -static int luaB_auxwrap (lua_State *L) { - lua_State *co = lua_tothread(L, lua_upvalueindex(1)); - int r = auxresume(L, co, lua_gettop(L)); - if (l_unlikely(r < 0)) { /* error? */ - int stat = lua_status(co); - if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ - stat = lua_resetthread(co); /* close its tbc variables */ - lua_assert(stat != LUA_OK); - lua_xmove(co, L, 1); /* move error message to the caller */ - } - if (stat != LUA_ERRMEM && /* not a memory error and ... */ - lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ - luaL_where(L, 1); /* add extra info, if available */ - lua_insert(L, -2); - lua_concat(L, 2); - } - return lua_error(L); /* propagate error */ - } - return r; -} - - -static int luaB_cocreate (lua_State *L) { - lua_State *NL; - luaL_checktype(L, 1, LUA_TFUNCTION); - NL = lua_newthread(L); - lua_pushvalue(L, 1); /* move function to top */ - lua_xmove(L, NL, 1); /* move function from L to NL */ - return 1; -} - - -static int luaB_cowrap (lua_State *L) { - luaB_cocreate(L); - lua_pushcclosure(L, luaB_auxwrap, 1); - return 1; -} - - -static int luaB_yield (lua_State *L) { - return lua_yield(L, lua_gettop(L)); -} - - -#define COS_RUN 0 -#define COS_DEAD 1 -#define COS_YIELD 2 -#define COS_NORM 3 - - -static const char *const statname[] = - {"running", "dead", "suspended", "normal"}; - - -static int auxstatus (lua_State *L, lua_State *co) { - if (L == co) return COS_RUN; - else { - switch (lua_status(co)) { - case LUA_YIELD: - return COS_YIELD; - case LUA_OK: { - lua_Debug ar; - if (lua_getstack(co, 0, &ar)) /* does it have frames? */ - return COS_NORM; /* it is running */ - else if (lua_gettop(co) == 0) - return COS_DEAD; - else - return COS_YIELD; /* initial state */ - } - default: /* some error occurred */ - return COS_DEAD; - } - } -} - - -static int luaB_costatus (lua_State *L) { - lua_State *co = getco(L); - lua_pushstring(L, statname[auxstatus(L, co)]); - return 1; -} - - -static int luaB_yieldable (lua_State *L) { - lua_State *co = lua_isnone(L, 1) ? L : getco(L); - lua_pushboolean(L, lua_isyieldable(co)); - return 1; -} - - -static int luaB_corunning (lua_State *L) { - int ismain = lua_pushthread(L); - lua_pushboolean(L, ismain); - return 2; -} - - -static int luaB_close (lua_State *L) { - lua_State *co = getco(L); - int status = auxstatus(L, co); - switch (status) { - case COS_DEAD: case COS_YIELD: { - status = lua_resetthread(co); - if (status == LUA_OK) { - lua_pushboolean(L, 1); - return 1; - } - else { - lua_pushboolean(L, 0); - lua_xmove(co, L, 1); /* move error message */ - return 2; - } - } - default: /* normal or running coroutine */ - return luaL_error(L, "cannot close a %s coroutine", statname[status]); - } -} - - -static const luaL_Reg co_funcs[] = { - {"create", luaB_cocreate}, - {"resume", luaB_coresume}, - {"running", luaB_corunning}, - {"status", luaB_costatus}, - {"wrap", luaB_cowrap}, - {"yield", luaB_yield}, - {"isyieldable", luaB_yieldable}, - {"close", luaB_close}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_coroutine (lua_State *L) { - luaL_newlib(L, co_funcs); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.c deleted file mode 100644 index 9542280..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.c +++ /dev/null @@ -1,64 +0,0 @@ -/* -** $Id: lctype.c $ -** 'ctype' functions for Lua -** See Copyright Notice in lua.h -*/ - -#define lctype_c -#define LUA_CORE - -#include "lprefix.h" - - -#include "lctype.h" - -#if !LUA_USE_CTYPE /* { */ - -#include - - -#if defined (LUA_UCID) /* accept UniCode IDentifiers? */ -/* consider all non-ascii codepoints to be alphabetic */ -#define NONA 0x01 -#else -#define NONA 0x00 /* default */ -#endif - - -LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { - 0x00, /* EOZ */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ - 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ - 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ - 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, - 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ - 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */ - NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, - NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; - -#endif /* } */ diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.h deleted file mode 100644 index 864e190..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lctype.h +++ /dev/null @@ -1,101 +0,0 @@ -/* -** $Id: lctype.h $ -** 'ctype' functions for Lua -** See Copyright Notice in lua.h -*/ - -#ifndef lctype_h -#define lctype_h - -#include "lua.h" - - -/* -** WARNING: the functions defined here do not necessarily correspond -** to the similar functions in the standard C ctype.h. They are -** optimized for the specific needs of Lua. -*/ - -#if !defined(LUA_USE_CTYPE) - -#if 'A' == 65 && '0' == 48 -/* ASCII case: can use its own tables; faster and fixed */ -#define LUA_USE_CTYPE 0 -#else -/* must use standard C ctype */ -#define LUA_USE_CTYPE 1 -#endif - -#endif - - -#if !LUA_USE_CTYPE /* { */ - -#include - -#include "llimits.h" - - -#define ALPHABIT 0 -#define DIGITBIT 1 -#define PRINTBIT 2 -#define SPACEBIT 3 -#define XDIGITBIT 4 - - -#define MASK(B) (1 << (B)) - - -/* -** add 1 to char to allow index -1 (EOZ) -*/ -#define testprop(c,p) (luai_ctype_[(c)+1] & (p)) - -/* -** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' -*/ -#define lislalpha(c) testprop(c, MASK(ALPHABIT)) -#define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) -#define lisdigit(c) testprop(c, MASK(DIGITBIT)) -#define lisspace(c) testprop(c, MASK(SPACEBIT)) -#define lisprint(c) testprop(c, MASK(PRINTBIT)) -#define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) - - -/* -** In ASCII, this 'ltolower' is correct for alphabetic characters and -** for '.'. That is enough for Lua needs. ('check_exp' ensures that -** the character either is an upper-case letter or is unchanged by -** the transformation, which holds for lower-case letters and '.'.) -*/ -#define ltolower(c) \ - check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ - (c) | ('A' ^ 'a')) - - -/* one entry for each character and for -1 (EOZ) */ -LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) - - -#else /* }{ */ - -/* -** use standard C ctypes -*/ - -#include - - -#define lislalpha(c) (isalpha(c) || (c) == '_') -#define lislalnum(c) (isalnum(c) || (c) == '_') -#define lisdigit(c) (isdigit(c)) -#define lisspace(c) (isspace(c)) -#define lisprint(c) (isprint(c)) -#define lisxdigit(c) (isxdigit(c)) - -#define ltolower(c) (tolower(c)) - -#endif /* } */ - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldblib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldblib.c deleted file mode 100644 index 6dcbaa9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldblib.c +++ /dev/null @@ -1,483 +0,0 @@ -/* -** $Id: ldblib.c $ -** Interface from Lua to its debug API -** See Copyright Notice in lua.h -*/ - -#define ldblib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** The hook table at registry[HOOKKEY] maps threads to their current -** hook function. -*/ -static const char *const HOOKKEY = "_HOOKKEY"; - - -/* -** If L1 != L, L1 can be in any state, and therefore there are no -** guarantees about its stack space; any push in L1 must be -** checked. -*/ -static void checkstack (lua_State *L, lua_State *L1, int n) { - if (l_unlikely(L != L1 && !lua_checkstack(L1, n))) - luaL_error(L, "stack overflow"); -} - - -static int db_getregistry (lua_State *L) { - lua_pushvalue(L, LUA_REGISTRYINDEX); - return 1; -} - - -static int db_getmetatable (lua_State *L) { - luaL_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - lua_pushnil(L); /* no metatable */ - } - return 1; -} - - -static int db_setmetatable (lua_State *L) { - int t = lua_type(L, 2); - luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); - lua_settop(L, 2); - lua_setmetatable(L, 1); - return 1; /* return 1st argument */ -} - - -static int db_getuservalue (lua_State *L) { - int n = (int)luaL_optinteger(L, 2, 1); - if (lua_type(L, 1) != LUA_TUSERDATA) - luaL_pushfail(L); - else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { - lua_pushboolean(L, 1); - return 2; - } - return 1; -} - - -static int db_setuservalue (lua_State *L) { - int n = (int)luaL_optinteger(L, 3, 1); - luaL_checktype(L, 1, LUA_TUSERDATA); - luaL_checkany(L, 2); - lua_settop(L, 2); - if (!lua_setiuservalue(L, 1, n)) - luaL_pushfail(L); - return 1; -} - - -/* -** Auxiliary function used by several library functions: check for -** an optional thread as function's first argument and set 'arg' with -** 1 if this argument is present (so that functions can skip it to -** access their other arguments) -*/ -static lua_State *getthread (lua_State *L, int *arg) { - if (lua_isthread(L, 1)) { - *arg = 1; - return lua_tothread(L, 1); - } - else { - *arg = 0; - return L; /* function will operate over current thread */ - } -} - - -/* -** Variations of 'lua_settable', used by 'db_getinfo' to put results -** from 'lua_getinfo' into result table. Key is always a string; -** value can be a string, an int, or a boolean. -*/ -static void settabss (lua_State *L, const char *k, const char *v) { - lua_pushstring(L, v); - lua_setfield(L, -2, k); -} - -static void settabsi (lua_State *L, const char *k, int v) { - lua_pushinteger(L, v); - lua_setfield(L, -2, k); -} - -static void settabsb (lua_State *L, const char *k, int v) { - lua_pushboolean(L, v); - lua_setfield(L, -2, k); -} - - -/* -** In function 'db_getinfo', the call to 'lua_getinfo' may push -** results on the stack; later it creates the result table to put -** these objects. Function 'treatstackoption' puts the result from -** 'lua_getinfo' on top of the result table so that it can call -** 'lua_setfield'. -*/ -static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { - if (L == L1) - lua_rotate(L, -2, 1); /* exchange object and table */ - else - lua_xmove(L1, L, 1); /* move object to the "main" stack */ - lua_setfield(L, -2, fname); /* put object into table */ -} - - -/* -** Calls 'lua_getinfo' and collects all results in a new table. -** L1 needs stack space for an optional input (function) plus -** two optional outputs (function and line table) from function -** 'lua_getinfo'. -*/ -static int db_getinfo (lua_State *L) { - lua_Debug ar; - int arg; - lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnSrtu"); - checkstack(L, L1, 3); - luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'"); - if (lua_isfunction(L, arg + 1)) { /* info about a function? */ - options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ - lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ - lua_xmove(L, L1, 1); - } - else { /* stack level */ - if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { - luaL_pushfail(L); /* level out of range */ - return 1; - } - } - if (!lua_getinfo(L1, options, &ar)) - return luaL_argerror(L, arg+2, "invalid option"); - lua_newtable(L); /* table to collect results */ - if (strchr(options, 'S')) { - lua_pushlstring(L, ar.source, ar.srclen); - lua_setfield(L, -2, "source"); - settabss(L, "short_src", ar.short_src); - settabsi(L, "linedefined", ar.linedefined); - settabsi(L, "lastlinedefined", ar.lastlinedefined); - settabss(L, "what", ar.what); - } - if (strchr(options, 'l')) - settabsi(L, "currentline", ar.currentline); - if (strchr(options, 'u')) { - settabsi(L, "nups", ar.nups); - settabsi(L, "nparams", ar.nparams); - settabsb(L, "isvararg", ar.isvararg); - } - if (strchr(options, 'n')) { - settabss(L, "name", ar.name); - settabss(L, "namewhat", ar.namewhat); - } - if (strchr(options, 'r')) { - settabsi(L, "ftransfer", ar.ftransfer); - settabsi(L, "ntransfer", ar.ntransfer); - } - if (strchr(options, 't')) - settabsb(L, "istailcall", ar.istailcall); - if (strchr(options, 'L')) - treatstackoption(L, L1, "activelines"); - if (strchr(options, 'f')) - treatstackoption(L, L1, "func"); - return 1; /* return table */ -} - - -static int db_getlocal (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ - if (lua_isfunction(L, arg + 1)) { /* function argument? */ - lua_pushvalue(L, arg + 1); /* push function */ - lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ - return 1; /* return only name (there is no value) */ - } - else { /* stack-level argument */ - lua_Debug ar; - const char *name; - int level = (int)luaL_checkinteger(L, arg + 1); - if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - checkstack(L, L1, 1); - name = lua_getlocal(L1, &ar, nvar); - if (name) { - lua_xmove(L1, L, 1); /* move local value */ - lua_pushstring(L, name); /* push name */ - lua_rotate(L, -2, 1); /* re-order */ - return 2; - } - else { - luaL_pushfail(L); /* no name (nor value) */ - return 1; - } - } -} - - -static int db_setlocal (lua_State *L) { - int arg; - const char *name; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - int level = (int)luaL_checkinteger(L, arg + 1); - int nvar = (int)luaL_checkinteger(L, arg + 2); - if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ - return luaL_argerror(L, arg+1, "level out of range"); - luaL_checkany(L, arg+3); - lua_settop(L, arg+3); - checkstack(L, L1, 1); - lua_xmove(L, L1, 1); - name = lua_setlocal(L1, &ar, nvar); - if (name == NULL) - lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ - lua_pushstring(L, name); - return 1; -} - - -/* -** get (if 'get' is true) or set an upvalue from a closure -*/ -static int auxupvalue (lua_State *L, int get) { - const char *name; - int n = (int)luaL_checkinteger(L, 2); /* upvalue index */ - luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */ - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); - if (name == NULL) return 0; - lua_pushstring(L, name); - lua_insert(L, -(get+1)); /* no-op if get is false */ - return get + 1; -} - - -static int db_getupvalue (lua_State *L) { - return auxupvalue(L, 1); -} - - -static int db_setupvalue (lua_State *L) { - luaL_checkany(L, 3); - return auxupvalue(L, 0); -} - - -/* -** Check whether a given upvalue from a given closure exists and -** returns its index -*/ -static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) { - void *id; - int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ - luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ - id = lua_upvalueid(L, argf, nup); - if (pnup) { - luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index"); - *pnup = nup; - } - return id; -} - - -static int db_upvalueid (lua_State *L) { - void *id = checkupval(L, 1, 2, NULL); - if (id != NULL) - lua_pushlightuserdata(L, id); - else - luaL_pushfail(L); - return 1; -} - - -static int db_upvaluejoin (lua_State *L) { - int n1, n2; - checkupval(L, 1, 2, &n1); - checkupval(L, 3, 4, &n2); - luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); - luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); - lua_upvaluejoin(L, 1, n1, 3, n2); - return 0; -} - - -/* -** Call hook function registered at hook table for the current -** thread (if there is one) -*/ -static void hookf (lua_State *L, lua_Debug *ar) { - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail call"}; - lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); - lua_pushthread(L); - if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ - lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ - if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); /* push current line */ - else lua_pushnil(L); - lua_assert(lua_getinfo(L, "lS", ar)); - lua_call(L, 2, 0); /* call hook function */ - } -} - - -/* -** Convert a string mask (for 'sethook') into a bit mask -*/ -static int makemask (const char *smask, int count) { - int mask = 0; - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; - if (strchr(smask, 'r')) mask |= LUA_MASKRET; - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; - if (count > 0) mask |= LUA_MASKCOUNT; - return mask; -} - - -/* -** Convert a bit mask (for 'gethook') into a string mask -*/ -static char *unmakemask (int mask, char *smask) { - int i = 0; - if (mask & LUA_MASKCALL) smask[i++] = 'c'; - if (mask & LUA_MASKRET) smask[i++] = 'r'; - if (mask & LUA_MASKLINE) smask[i++] = 'l'; - smask[i] = '\0'; - return smask; -} - - -static int db_sethook (lua_State *L) { - int arg, mask, count; - lua_Hook func; - lua_State *L1 = getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { /* no hook? */ - lua_settop(L, arg+1); - func = NULL; mask = 0; count = 0; /* turn off hooks */ - } - else { - const char *smask = luaL_checkstring(L, arg+2); - luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = (int)luaL_optinteger(L, arg + 3, 0); - func = hookf; mask = makemask(smask, count); - } - if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { - /* table just created; initialize it */ - lua_pushliteral(L, "k"); - lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ - lua_pushvalue(L, -1); - lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ - } - checkstack(L, L1, 1); - lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ - lua_pushvalue(L, arg + 1); /* value (hook function) */ - lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ - lua_sethook(L1, func, mask, count); - return 0; -} - - -static int db_gethook (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - char buff[5]; - int mask = lua_gethookmask(L1); - lua_Hook hook = lua_gethook(L1); - if (hook == NULL) { /* no hook? */ - luaL_pushfail(L); - return 1; - } - else if (hook != hookf) /* external hook? */ - lua_pushliteral(L, "external hook"); - else { /* hook table must exist */ - lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); - checkstack(L, L1, 1); - lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_rawget(L, -2); /* 1st result = hooktable[L1] */ - lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ - lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */ - return 3; -} - - -static int db_debug (lua_State *L) { - for (;;) { - char buffer[250]; - lua_writestringerror("%s", "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == NULL || - strcmp(buffer, "cont\n") == 0) - return 0; - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || - lua_pcall(L, 0, 0, 0)) - lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); - lua_settop(L, 0); /* remove eventual returns */ - } -} - - -static int db_traceback (lua_State *L) { - int arg; - lua_State *L1 = getthread(L, &arg); - const char *msg = lua_tostring(L, arg + 1); - if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ - lua_pushvalue(L, arg + 1); /* return it untouched */ - else { - int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0); - luaL_traceback(L, L1, msg, level); - } - return 1; -} - - -static int db_setcstacklimit (lua_State *L) { - int limit = (int)luaL_checkinteger(L, 1); - int res = lua_setcstacklimit(L, limit); - lua_pushinteger(L, res); - return 1; -} - - -static const luaL_Reg dblib[] = { - {"debug", db_debug}, - {"getuservalue", db_getuservalue}, - {"gethook", db_gethook}, - {"getinfo", db_getinfo}, - {"getlocal", db_getlocal}, - {"getregistry", db_getregistry}, - {"getmetatable", db_getmetatable}, - {"getupvalue", db_getupvalue}, - {"upvaluejoin", db_upvaluejoin}, - {"upvalueid", db_upvalueid}, - {"setuservalue", db_setuservalue}, - {"sethook", db_sethook}, - {"setlocal", db_setlocal}, - {"setmetatable", db_setmetatable}, - {"setupvalue", db_setupvalue}, - {"traceback", db_traceback}, - {"setcstacklimit", db_setcstacklimit}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_debug (lua_State *L) { - luaL_newlib(L, dblib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.c deleted file mode 100644 index a716d95..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.c +++ /dev/null @@ -1,918 +0,0 @@ -/* -** $Id: ldebug.c $ -** Debug Interface -** See Copyright Notice in lua.h -*/ - -#define ldebug_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - - -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) - - -static const char *funcnamefromcall (lua_State *L, CallInfo *ci, - const char **name); - - -static int currentpc (CallInfo *ci) { - lua_assert(isLua(ci)); - return pcRel(ci->u.l.savedpc, ci_func(ci)->p); -} - - -/* -** Get a "base line" to find the line corresponding to an instruction. -** Base lines are regularly placed at MAXIWTHABS intervals, so usually -** an integer division gets the right place. When the source file has -** large sequences of empty/comment lines, it may need extra entries, -** so the original estimate needs a correction. -** If the original estimate is -1, the initial 'if' ensures that the -** 'while' will run at least once. -** The assertion that the estimate is a lower bound for the correct base -** is valid as long as the debug info has been generated with the same -** value for MAXIWTHABS or smaller. (Previous releases use a little -** smaller value.) -*/ -static int getbaseline (const Proto *f, int pc, int *basepc) { - if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { - *basepc = -1; /* start from the beginning */ - return f->linedefined; - } - else { - int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ - /* estimate must be a lower bound of the correct base */ - lua_assert(i < 0 || - (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); - while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) - i++; /* low estimate; adjust it */ - *basepc = f->abslineinfo[i].pc; - return f->abslineinfo[i].line; - } -} - - -/* -** Get the line corresponding to instruction 'pc' in function 'f'; -** first gets a base line and from there does the increments until -** the desired instruction. -*/ -int luaG_getfuncline (const Proto *f, int pc) { - if (f->lineinfo == NULL) /* no debug information? */ - return -1; - else { - int basepc; - int baseline = getbaseline(f, pc, &basepc); - while (basepc++ < pc) { /* walk until given instruction */ - lua_assert(f->lineinfo[basepc] != ABSLINEINFO); - baseline += f->lineinfo[basepc]; /* correct line */ - } - return baseline; - } -} - - -static int getcurrentline (CallInfo *ci) { - return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); -} - - -/* -** Set 'trap' for all active Lua frames. -** This function can be called during a signal, under "reasonable" -** assumptions. A new 'ci' is completely linked in the list before it -** becomes part of the "active" list, and we assume that pointers are -** atomic; see comment in next function. -** (A compiler doing interprocedural optimizations could, theoretically, -** reorder memory writes in such a way that the list could be -** temporarily broken while inserting a new element. We simply assume it -** has no good reasons to do that.) -*/ -static void settraps (CallInfo *ci) { - for (; ci != NULL; ci = ci->previous) - if (isLua(ci)) - ci->u.l.trap = 1; -} - - -/* -** This function can be called during a signal, under "reasonable" -** assumptions. -** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount') -** are for debug only, and it is no problem if they get arbitrary -** values (causes at most one wrong hook call). 'hookmask' is an atomic -** value. We assume that pointers are atomic too (e.g., gcc ensures that -** for all platforms where it runs). Moreover, 'hook' is always checked -** before being called (see 'luaD_hook'). -*/ -LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { - if (func == NULL || mask == 0) { /* turn off hooks? */ - mask = 0; - func = NULL; - } - L->hook = func; - L->basehookcount = count; - resethookcount(L); - L->hookmask = cast_byte(mask); - if (mask) - settraps(L->ci); /* to trace inside 'luaV_execute' */ -} - - -LUA_API lua_Hook lua_gethook (lua_State *L) { - return L->hook; -} - - -LUA_API int lua_gethookmask (lua_State *L) { - return L->hookmask; -} - - -LUA_API int lua_gethookcount (lua_State *L) { - return L->basehookcount; -} - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { - int status; - CallInfo *ci; - if (level < 0) return 0; /* invalid (negative) level */ - lua_lock(L); - for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) - level--; - if (level == 0 && ci != &L->base_ci) { /* level found? */ - status = 1; - ar->i_ci = ci; - } - else status = 0; /* no such level */ - lua_unlock(L); - return status; -} - - -static const char *upvalname (const Proto *p, int uv) { - TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); - if (s == NULL) return "?"; - else return getstr(s); -} - - -static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - if (clLvalue(s2v(ci->func))->p->is_vararg) { - int nextra = ci->u.l.nextraargs; - if (n >= -nextra) { /* 'n' is negative */ - *pos = ci->func - nextra - (n + 1); - return "(vararg)"; /* generic name for any vararg */ - } - } - return NULL; /* no such vararg */ -} - - -const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { - StkId base = ci->func + 1; - const char *name = NULL; - if (isLua(ci)) { - if (n < 0) /* access to vararg values? */ - return findvararg(ci, n, pos); - else - name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); - } - if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; - if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ - /* generic name for any valid slot */ - name = isLua(ci) ? "(temporary)" : "(C temporary)"; - } - else - return NULL; /* no name */ - } - if (pos) - *pos = base + (n - 1); - return name; -} - - -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { - const char *name; - lua_lock(L); - if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ - name = NULL; - else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); - } - else { /* active function; get information through 'ar' */ - StkId pos = NULL; /* to avoid warnings */ - name = luaG_findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobjs2s(L, L->top, pos); - api_incr_top(L); - } - } - lua_unlock(L); - return name; -} - - -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = NULL; /* to avoid warnings */ - const char *name; - lua_lock(L); - name = luaG_findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ - } - lua_unlock(L); - return name; -} - - -static void funcinfo (lua_Debug *ar, Closure *cl) { - if (noLuaClosure(cl)) { - ar->source = "=[C]"; - ar->srclen = LL("=[C]"); - ar->linedefined = -1; - ar->lastlinedefined = -1; - ar->what = "C"; - } - else { - const Proto *p = cl->l.p; - if (p->source) { - ar->source = getstr(p->source); - ar->srclen = tsslen(p->source); - } - else { - ar->source = "=?"; - ar->srclen = LL("=?"); - } - ar->linedefined = p->linedefined; - ar->lastlinedefined = p->lastlinedefined; - ar->what = (ar->linedefined == 0) ? "main" : "Lua"; - } - luaO_chunkid(ar->short_src, ar->source, ar->srclen); -} - - -static int nextline (const Proto *p, int currentline, int pc) { - if (p->lineinfo[pc] != ABSLINEINFO) - return currentline + p->lineinfo[pc]; - else - return luaG_getfuncline(p, pc); -} - - -static void collectvalidlines (lua_State *L, Closure *f) { - if (noLuaClosure(f)) { - setnilvalue(s2v(L->top)); - api_incr_top(L); - } - else { - int i; - TValue v; - const Proto *p = f->l.p; - int currentline = p->linedefined; - Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue2s(L, L->top, t); /* push it on stack */ - api_incr_top(L); - setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - if (!p->is_vararg) /* regular function? */ - i = 0; /* consider all instructions */ - else { /* vararg function */ - lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); - currentline = nextline(p, currentline, 0); - i = 1; /* skip first instruction (OP_VARARGPREP) */ - } - for (; i < p->sizelineinfo; i++) { /* for each instruction */ - currentline = nextline(p, currentline, i); /* get its line */ - luaH_setint(L, t, currentline, &v); /* table[line] = true */ - } - } -} - - -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - /* calling function is a known function? */ - if (ci != NULL && !(ci->callstatus & CIST_TAIL)) - return funcnamefromcall(L, ci->previous, name); - else return NULL; /* no way to find a name */ -} - - -static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, - Closure *f, CallInfo *ci) { - int status = 1; - for (; *what; what++) { - switch (*what) { - case 'S': { - funcinfo(ar, f); - break; - } - case 'l': { - ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; - break; - } - case 'u': { - ar->nups = (f == NULL) ? 0 : f->c.nupvalues; - if (noLuaClosure(f)) { - ar->isvararg = 1; - ar->nparams = 0; - } - else { - ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams; - } - break; - } - case 't': { - ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; - break; - } - case 'n': { - ar->namewhat = getfuncname(L, ci, &ar->name); - if (ar->namewhat == NULL) { - ar->namewhat = ""; /* not found */ - ar->name = NULL; - } - break; - } - case 'r': { - if (ci == NULL || !(ci->callstatus & CIST_TRAN)) - ar->ftransfer = ar->ntransfer = 0; - else { - ar->ftransfer = ci->u2.transferinfo.ftransfer; - ar->ntransfer = ci->u2.transferinfo.ntransfer; - } - break; - } - case 'L': - case 'f': /* handled by lua_getinfo */ - break; - default: status = 0; /* invalid option */ - } - } - return status; -} - - -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { - int status; - Closure *cl; - CallInfo *ci; - TValue *func; - lua_lock(L); - if (*what == '>') { - ci = NULL; - func = s2v(L->top - 1); - api_check(L, ttisfunction(func), "function expected"); - what++; /* skip the '>' */ - L->top--; /* pop function */ - } - else { - ci = ar->i_ci; - func = s2v(ci->func); - lua_assert(ttisfunction(func)); - } - cl = ttisclosure(func) ? clvalue(func) : NULL; - status = auxgetinfo(L, what, ar, cl, ci); - if (strchr(what, 'f')) { - setobj2s(L, L->top, func); - api_incr_top(L); - } - if (strchr(what, 'L')) - collectvalidlines(L, cl); - lua_unlock(L); - return status; -} - - -/* -** {====================================================== -** Symbolic Execution -** ======================================================= -*/ - -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name); - - -/* -** Find a "name" for the constant 'c'. -*/ -static void kname (const Proto *p, int c, const char **name) { - TValue *kvalue = &p->k[c]; - *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; -} - - -/* -** Find a "name" for the register 'c'. -*/ -static void rname (const Proto *p, int pc, int c, const char **name) { - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (!(what && *what == 'c')) /* did not find a constant name? */ - *name = "?"; -} - - -/* -** Find a "name" for a 'C' value in an RK instruction. -*/ -static void rkname (const Proto *p, int pc, Instruction i, const char **name) { - int c = GETARG_C(i); /* key index */ - if (GETARG_k(i)) /* is 'c' a constant? */ - kname(p, c, name); - else /* 'c' is a register */ - rname(p, pc, c, name); -} - - -static int filterpc (int pc, int jmptarget) { - if (pc < jmptarget) /* is code conditional (inside a jump)? */ - return -1; /* cannot know who sets that register */ - else return pc; /* current position sets that register */ -} - - -/* -** Try to find last instruction before 'lastpc' that modified register 'reg'. -*/ -static int findsetreg (const Proto *p, int lastpc, int reg) { - int pc; - int setreg = -1; /* keep last instruction that changed 'reg' */ - int jmptarget = 0; /* any code before this address is conditional */ - if (testMMMode(GET_OPCODE(p->code[lastpc]))) - lastpc--; /* previous instruction was not actually executed */ - for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); - int a = GETARG_A(i); - int change; /* true if current instruction changed 'reg' */ - switch (op) { - case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */ - int b = GETARG_B(i); - change = (a <= reg && reg <= a + b); - break; - } - case OP_TFORCALL: { /* affect all regs above its base */ - change = (reg >= a + 2); - break; - } - case OP_CALL: - case OP_TAILCALL: { /* affect all registers above base */ - change = (reg >= a); - break; - } - case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */ - int b = GETARG_sJ(i); - int dest = pc + 1 + b; - /* jump does not skip 'lastpc' and is larger than current one? */ - if (dest <= lastpc && dest > jmptarget) - jmptarget = dest; /* update 'jmptarget' */ - change = 0; - break; - } - default: /* any instruction that sets A */ - change = (testAMode(op) && reg == a); - break; - } - if (change) - setreg = filterpc(pc, jmptarget); - } - return setreg; -} - - -/* -** Check whether table being indexed by instruction 'i' is the -** environment '_ENV' -*/ -static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { - int t = GETARG_B(i); /* table index */ - const char *name; /* name of indexed variable */ - if (isup) /* is an upvalue? */ - name = upvalname(p, t); - else - getobjname(p, pc, t, &name); - return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; -} - - -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name) { - int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); - if (*name) /* is a local? */ - return "local"; - /* else try symbolic execution */ - pc = findsetreg(p, lastpc, reg); - if (pc != -1) { /* could find instruction? */ - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); - switch (op) { - case OP_MOVE: { - int b = GETARG_B(i); /* move from 'b' to 'a' */ - if (b < GETARG_A(i)) - return getobjname(p, pc, b, name); /* get name for 'b' */ - break; - } - case OP_GETTABUP: { - int k = GETARG_C(i); /* key index */ - kname(p, k, name); - return gxf(p, pc, i, 1); - } - case OP_GETTABLE: { - int k = GETARG_C(i); /* key index */ - rname(p, pc, k, name); - return gxf(p, pc, i, 0); - } - case OP_GETI: { - *name = "integer index"; - return "field"; - } - case OP_GETFIELD: { - int k = GETARG_C(i); /* key index */ - kname(p, k, name); - return gxf(p, pc, i, 0); - } - case OP_GETUPVAL: { - *name = upvalname(p, GETARG_B(i)); - return "upvalue"; - } - case OP_LOADK: - case OP_LOADKX: { - int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); - if (ttisstring(&p->k[b])) { - *name = svalue(&p->k[b]); - return "constant"; - } - break; - } - case OP_SELF: { - rkname(p, pc, i, name); - return "method"; - } - default: break; /* go through to return NULL */ - } - } - return NULL; /* could not find reasonable name */ -} - - -/* -** Try to find a name for a function based on the code that called it. -** (Only works when function was called by a Lua function.) -** Returns what the name is (e.g., "for iterator", "method", -** "metamethod") and sets '*name' to point to the name. -*/ -static const char *funcnamefromcode (lua_State *L, const Proto *p, - int pc, const char **name) { - TMS tm = (TMS)0; /* (initial value avoids warnings) */ - Instruction i = p->code[pc]; /* calling instruction */ - switch (GET_OPCODE(i)) { - case OP_CALL: - case OP_TAILCALL: - return getobjname(p, pc, GETARG_A(i), name); /* get function name */ - case OP_TFORCALL: { /* for iterator */ - *name = "for iterator"; - return "for iterator"; - } - /* other instructions can do calls through metamethods */ - case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: - case OP_GETI: case OP_GETFIELD: - tm = TM_INDEX; - break; - case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: - tm = TM_NEWINDEX; - break; - case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { - tm = cast(TMS, GETARG_C(i)); - break; - } - case OP_UNM: tm = TM_UNM; break; - case OP_BNOT: tm = TM_BNOT; break; - case OP_LEN: tm = TM_LEN; break; - case OP_CONCAT: tm = TM_CONCAT; break; - case OP_EQ: tm = TM_EQ; break; - /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */ - case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break; - case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break; - case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break; - default: - return NULL; /* cannot find a reasonable name */ - } - *name = getstr(G(L)->tmname[tm]) + 2; - return "metamethod"; -} - - -/* -** Try to find a name for a function based on how it was called. -*/ -static const char *funcnamefromcall (lua_State *L, CallInfo *ci, - const char **name) { - if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ - *name = "?"; - return "hook"; - } - else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */ - *name = "__gc"; - return "metamethod"; /* report it as such */ - } - else if (isLua(ci)) - return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name); - else - return NULL; -} - -/* }====================================================== */ - - - -/* -** Check whether pointer 'o' points to some value in the stack -** frame of the current function. Because 'o' may not point to a -** value in this stack, we cannot compare it with the region -** boundaries (undefined behaviour in ISO C). -*/ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId pos; - for (pos = ci->func + 1; pos < ci->top; pos++) { - if (o == s2v(pos)) - return 1; - } - return 0; /* not found */ -} - - -/* -** Checks whether value 'o' came from an upvalue. (That can only happen -** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on -** upvalues.) -*/ -static const char *getupvalname (CallInfo *ci, const TValue *o, - const char **name) { - LClosure *c = ci_func(ci); - int i; - for (i = 0; i < c->nupvalues; i++) { - if (c->upvals[i]->v == o) { - *name = upvalname(c->p, i); - return "upvalue"; - } - } - return NULL; -} - - -static const char *formatvarinfo (lua_State *L, const char *kind, - const char *name) { - if (kind == NULL) - return ""; /* no information */ - else - return luaO_pushfstring(L, " (%s '%s')", kind, name); -} - -/* -** Build a string with a "description" for the value 'o', such as -** "variable 'x'" or "upvalue 'y'". -*/ -static const char *varinfo (lua_State *L, const TValue *o) { - CallInfo *ci = L->ci; - const char *name = NULL; /* to avoid warnings */ - const char *kind = NULL; - if (isLua(ci)) { - kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func + 1)), &name); - } - return formatvarinfo(L, kind, name); -} - - -/* -** Raise a type error -*/ -static l_noret typeerror (lua_State *L, const TValue *o, const char *op, - const char *extra) { - const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra); -} - - -/* -** Raise a type error with "standard" information about the faulty -** object 'o' (using 'varinfo'). -*/ -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { - typeerror(L, o, op, varinfo(L, o)); -} - - -/* -** Raise an error for calling a non-callable object. Try to find a name -** for the object based on how it was called ('funcnamefromcall'); if it -** cannot get a name there, try 'varinfo'. -*/ -l_noret luaG_callerror (lua_State *L, const TValue *o) { - CallInfo *ci = L->ci; - const char *name = NULL; /* to avoid warnings */ - const char *kind = funcnamefromcall(L, ci, &name); - const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); - typeerror(L, o, "call", extra); -} - - -l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { - luaG_runerror(L, "bad 'for' %s (number expected, got %s)", - what, luaT_objtypename(L, o)); -} - - -l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { - if (ttisstring(p1) || cvt2str(p1)) p1 = p2; - luaG_typeerror(L, p1, "concatenate"); -} - - -l_noret luaG_opinterror (lua_State *L, const TValue *p1, - const TValue *p2, const char *msg) { - if (!ttisnumber(p1)) /* first operand is wrong? */ - p2 = p1; /* now second is wrong */ - luaG_typeerror(L, p2, msg); -} - - -/* -** Error when both values are convertible to numbers, but not to integers -*/ -l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { - lua_Integer temp; - if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I)) - p2 = p1; - luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); -} - - -l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { - const char *t1 = luaT_objtypename(L, p1); - const char *t2 = luaT_objtypename(L, p2); - if (strcmp(t1, t2) == 0) - luaG_runerror(L, "attempt to compare two %s values", t1); - else - luaG_runerror(L, "attempt to compare %s with %s", t1, t2); -} - - -/* add src:line information to 'msg' */ -const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, - int line) { - char buff[LUA_IDSIZE]; - if (src) - luaO_chunkid(buff, getstr(src), tsslen(src)); - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; - } - return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); -} - - -l_noret luaG_errormsg (lua_State *L) { - if (L->errfunc != 0) { /* is there an error handling function? */ - StkId errfunc = restorestack(L, L->errfunc); - lua_assert(ttisfunction(s2v(errfunc))); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; /* assume EXTRA_STACK */ - luaD_callnoyield(L, L->top - 2, 1); /* call it */ - } - luaD_throw(L, LUA_ERRRUN); -} - - -l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { - CallInfo *ci = L->ci; - const char *msg; - va_list argp; - luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); - luaG_errormsg(L); -} - - -/* -** Check whether new instruction 'newpc' is in a different line from -** previous instruction 'oldpc'. More often than not, 'newpc' is only -** one or a few instructions after 'oldpc' (it must be after, see -** caller), so try to avoid calling 'luaG_getfuncline'. If they are -** too far apart, there is a good chance of a ABSLINEINFO in the way, -** so it goes directly to 'luaG_getfuncline'. -*/ -static int changedline (const Proto *p, int oldpc, int newpc) { - if (p->lineinfo == NULL) /* no debug information? */ - return 0; - if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ - int delta = 0; /* line diference */ - int pc = oldpc; - for (;;) { - int lineinfo = p->lineinfo[++pc]; - if (lineinfo == ABSLINEINFO) - break; /* cannot compute delta; fall through */ - delta += lineinfo; - if (pc == newpc) - return (delta != 0); /* delta computed successfully */ - } - } - /* either instructions are too far apart or there is an absolute line - info in the way; compute line difference explicitly */ - return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc)); -} - - -/* -** Traces the execution of a Lua function. Called before the execution -** of each opcode, when debug is on. 'L->oldpc' stores the last -** instruction traced, to detect line changes. When entering a new -** function, 'npci' will be zero and will test as a new line whatever -** the value of 'oldpc'. Some exceptional conditions may return to -** a function without setting 'oldpc'. In that case, 'oldpc' may be -** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' -** at most causes an extra call to a line hook.) -** This function is not "Protected" when called, so it should correct -** 'L->top' before calling anything that can run the GC. -*/ -int luaG_traceexec (lua_State *L, const Instruction *pc) { - CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; - const Proto *p = ci_func(ci)->p; - int counthook; - if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ - ci->u.l.trap = 0; /* don't need to stop again */ - return 0; /* turn off 'trap' */ - } - pc++; /* reference is always next instruction */ - ci->u.l.savedpc = pc; /* save 'pc' */ - counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); - if (counthook) - resethookcount(L); /* reset count */ - else if (!(mask & LUA_MASKLINE)) - return 1; /* no line hook and count != 0; nothing to be done now */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ - ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ - return 1; /* do not call hook again (VM yielded, so it did not move) */ - } - if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ - L->top = ci->top; /* correct top */ - if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ - if (mask & LUA_MASKLINE) { - /* 'L->oldpc' may be invalid; use zero in this case */ - int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; - int npci = pcRel(pc, p); - if (npci <= oldpc || /* call hook when jump back (loop), */ - changedline(p, oldpc, npci)) { /* or when enter new line */ - int newline = luaG_getfuncline(p, npci); - luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ - } - L->oldpc = npci; /* 'pc' of last call to line hook */ - } - if (L->status == LUA_YIELD) { /* did hook yield? */ - if (counthook) - L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ - ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - luaD_throw(L, LUA_YIELD); - } - return 1; /* keep 'trap' on */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.h deleted file mode 100644 index 974960e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldebug.h +++ /dev/null @@ -1,63 +0,0 @@ -/* -** $Id: ldebug.h $ -** Auxiliary functions from Debug Interface module -** See Copyright Notice in lua.h -*/ - -#ifndef ldebug_h -#define ldebug_h - - -#include "lstate.h" - - -#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) - - -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) - - -#define resethookcount(L) (L->hookcount = L->basehookcount) - -/* -** mark for entries in 'lineinfo' array that has absolute information in -** 'abslineinfo' array -*/ -#define ABSLINEINFO (-0x80) - - -/* -** MAXimum number of successive Instructions WiTHout ABSolute line -** information. (A power of two allows fast divisions.) -*/ -#if !defined(MAXIWTHABS) -#define MAXIWTHABS 128 -#endif - - -LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); -LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, - StkId *pos); -LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, - const char *opname); -LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); -LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, - const char *what); -LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, - const TValue *p2, - const char *msg); -LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, - const TValue *p2); -LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); -LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, - TString *src, int line); -LUAI_FUNC l_noret luaG_errormsg (lua_State *L); -LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.c deleted file mode 100644 index a48e35f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.c +++ /dev/null @@ -1,997 +0,0 @@ -/* -** $Id: ldo.c $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#define ldo_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lundump.h" -#include "lvm.h" -#include "lzio.h" - - - -#define errorstatus(s) ((s) > LUA_YIELD) - - -/* -** {====================================================== -** Error-recovery functions -** ======================================================= -*/ - -/* -** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By -** default, Lua handles errors with exceptions when compiling as -** C++ code, with _longjmp/_setjmp when asked to use them, and with -** longjmp/setjmp otherwise. -*/ -#if !defined(LUAI_THROW) /* { */ - -#if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ - -/* C++ exceptions */ -#define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,a) \ - try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy variable */ - -#elif defined(LUA_USE_POSIX) /* }{ */ - -/* in POSIX, try _longjmp/_setjmp (more efficient) */ -#define LUAI_THROW(L,c) _longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#else /* }{ */ - -/* ISO C handling with long jumps */ -#define LUAI_THROW(L,c) longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } -#define luai_jmpbuf jmp_buf - -#endif /* } */ - -#endif /* } */ - - - -/* chain list of long jump buffers */ -struct lua_longjmp { - struct lua_longjmp *previous; - luai_jmpbuf b; - volatile int status; /* error code */ -}; - - -void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - case LUA_OK: { /* special case only for closing upvalues */ - setnilvalue(s2v(oldtop)); /* no error message */ - break; - } - default: { - lua_assert(errorstatus(errcode)); /* real error */ - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ - break; - } - } - L->top = oldtop + 1; -} - - -l_noret luaD_throw (lua_State *L, int errcode) { - if (L->errorJmp) { /* thread has an error handler? */ - L->errorJmp->status = errcode; /* set status */ - LUAI_THROW(L, L->errorJmp); /* jump to it */ - } - else { /* thread has no error handler */ - global_State *g = G(L); - errcode = luaE_resetthread(L, errcode); /* close all upvalues */ - if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ - luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ - } - else { /* no handler at all; abort */ - if (g->panic) { /* panic function? */ - lua_unlock(L); - g->panic(L); /* call panic function (last chance to jump out) */ - } - abort(); - } - } -} - - -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - l_uint32 oldnCcalls = L->nCcalls; - struct lua_longjmp lj; - lj.status = LUA_OK; - lj.previous = L->errorJmp; /* chain new error handler */ - L->errorJmp = &lj; - LUAI_TRY(L, &lj, - (*f)(L, ud); - ); - L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls; - return lj.status; -} - -/* }====================================================== */ - - -/* -** {================================================================== -** Stack reallocation -** =================================================================== -*/ -static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { - CallInfo *ci; - UpVal *up; - L->top = (L->top - oldstack) + newstack; - L->tbclist = (L->tbclist - oldstack) + newstack; - for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = s2v((uplevel(up) - oldstack) + newstack); - for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + newstack; - ci->func = (ci->func - oldstack) + newstack; - if (isLua(ci)) - ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ - } -} - - -/* some space for error handling */ -#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) - - -/* -** Reallocate the stack to a new size, correcting all pointers into -** it. (There are pointers to a stack from its upvalues, from its list -** of call infos, plus a few individual pointers.) The reallocation is -** done in two steps (allocation + free) because the correction must be -** done while both addresses (the old stack and the new one) are valid. -** (In ISO C, any pointer use after the pointer has been deallocated is -** undefined behavior.) -** In case of allocation error, raise an error or return false according -** to 'raiseerror'. -*/ -int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { - int oldsize = stacksize(L); - int i; - StkId newstack = luaM_reallocvector(L, NULL, 0, - newsize + EXTRA_STACK, StackValue); - lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ - if (raiseerror) - luaM_error(L); - else return 0; /* do not raise an error */ - } - /* number of elements to be copied to the new stack */ - i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; - memcpy(newstack, L->stack, i * sizeof(StackValue)); - for (; i < newsize + EXTRA_STACK; i++) - setnilvalue(s2v(newstack + i)); /* erase new segment */ - correctstack(L, L->stack, newstack); - luaM_freearray(L, L->stack, oldsize + EXTRA_STACK); - L->stack = newstack; - L->stack_last = L->stack + newsize; - return 1; -} - - -/* -** Try to grow the stack by at least 'n' elements. when 'raiseerror' -** is true, raises any error; otherwise, return 0 in case of errors. -*/ -int luaD_growstack (lua_State *L, int n, int raiseerror) { - int size = stacksize(L); - if (l_unlikely(size > LUAI_MAXSTACK)) { - /* if stack is larger than maximum, thread is already using the - extra space reserved for errors, that is, thread is handling - a stack error; cannot grow further than that. */ - lua_assert(stacksize(L) == ERRORSTACKSIZE); - if (raiseerror) - luaD_throw(L, LUA_ERRERR); /* error inside message handler */ - return 0; /* if not 'raiseerror', just signal it */ - } - else { - int newsize = 2 * size; /* tentative new size */ - int needed = cast_int(L->top - L->stack) + n; - if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ - newsize = LUAI_MAXSTACK; - if (newsize < needed) /* but must respect what was asked for */ - newsize = needed; - if (l_likely(newsize <= LUAI_MAXSTACK)) - return luaD_reallocstack(L, newsize, raiseerror); - else { /* stack overflow */ - /* add extra size to be able to handle the error message */ - luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); - if (raiseerror) - luaG_runerror(L, "stack overflow"); - return 0; - } - } -} - - -static int stackinuse (lua_State *L) { - CallInfo *ci; - int res; - StkId lim = L->top; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top) lim = ci->top; - } - lua_assert(lim <= L->stack_last); - res = cast_int(lim - L->stack) + 1; /* part of stack in use */ - if (res < LUA_MINSTACK) - res = LUA_MINSTACK; /* ensure a minimum size */ - return res; -} - - -/* -** If stack size is more than 3 times the current use, reduce that size -** to twice the current use. (So, the final stack size is at most 2/3 the -** previous size, and half of its entries are empty.) -** As a particular case, if stack was handling a stack overflow and now -** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than -** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack -** will be reduced to a "regular" size. -*/ -void luaD_shrinkstack (lua_State *L) { - int inuse = stackinuse(L); - int nsize = inuse * 2; /* proposed new size */ - int max = inuse * 3; /* maximum "reasonable" size */ - if (max > LUAI_MAXSTACK) { - max = LUAI_MAXSTACK; /* respect stack limit */ - if (nsize > LUAI_MAXSTACK) - nsize = LUAI_MAXSTACK; - } - /* if thread is currently not handling a stack overflow and its - size is larger than maximum "reasonable" size, shrink it */ - if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) - luaD_reallocstack(L, nsize, 0); /* ok if that fails */ - else /* don't change stack */ - condmovestack(L,{},{}); /* (change only for debugging) */ - luaE_shrinkCI(L); /* shrink CI list */ -} - - -void luaD_inctop (lua_State *L) { - luaD_checkstack(L, 1); - L->top++; -} - -/* }================================================================== */ - - -/* -** Call a hook for the given event. Make sure there is a hook to be -** called. (Both 'L->hook' and 'L->hookmask', which trigger this -** function, can be changed asynchronously by signals.) -*/ -void luaD_hook (lua_State *L, int event, int line, - int ftransfer, int ntransfer) { - lua_Hook hook = L->hook; - if (hook && L->allowhook) { /* make sure there is a hook */ - int mask = CIST_HOOKED; - CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ - ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ - lua_Debug ar; - ar.event = event; - ar.currentline = line; - ar.i_ci = ci; - if (ntransfer != 0) { - mask |= CIST_TRAN; /* 'ci' has transfer information */ - ci->u2.transferinfo.ftransfer = ftransfer; - ci->u2.transferinfo.ntransfer = ntransfer; - } - if (isLua(ci) && L->top < ci->top) - L->top = ci->top; /* protect entire activation register */ - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (ci->top < L->top + LUA_MINSTACK) - ci->top = L->top + LUA_MINSTACK; - L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= mask; - lua_unlock(L); - (*hook)(L, &ar); - lua_lock(L); - lua_assert(!L->allowhook); - L->allowhook = 1; - ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); - ci->callstatus &= ~mask; - } -} - - -/* -** Executes a call hook for Lua functions. This function is called -** whenever 'hookmask' is not zero, so it checks whether call hooks are -** active. -*/ -void luaD_hookcall (lua_State *L, CallInfo *ci) { - L->oldpc = 0; /* set 'oldpc' for new function */ - if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ - int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL - : LUA_HOOKCALL; - Proto *p = ci_func(ci)->p; - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_hook(L, event, -1, 1, p->numparams); - ci->u.l.savedpc--; /* correct 'pc' */ - } -} - - -/* -** Executes a return hook for Lua and C functions and sets/corrects -** 'oldpc'. (Note that this correction is needed by the line hook, so it -** is done even when return hooks are off.) -*/ -static void rethook (lua_State *L, CallInfo *ci, int nres) { - if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ - StkId firstres = L->top - nres; /* index of first result */ - int delta = 0; /* correction for vararg functions */ - int ftransfer; - if (isLua(ci)) { - Proto *p = ci_func(ci)->p; - if (p->is_vararg) - delta = ci->u.l.nextraargs + p->numparams + 1; - } - ci->func += delta; /* if vararg, back to virtual 'func' */ - ftransfer = cast(unsigned short, firstres - ci->func); - luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ - ci->func -= delta; - } - if (isLua(ci = ci->previous)) - L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ -} - - -/* -** Check whether 'func' has a '__call' metafield. If so, put it in the -** stack, below original 'func', so that 'luaD_precall' can call it. Raise -** an error if there is no '__call' metafield. -*/ -StkId luaD_tryfuncTM (lua_State *L, StkId func) { - const TValue *tm; - StkId p; - checkstackGCp(L, 1, func); /* space for metamethod */ - tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ - if (l_unlikely(ttisnil(tm))) - luaG_callerror(L, s2v(func)); /* nothing to call */ - for (p = L->top; p > func; p--) /* open space for metamethod */ - setobjs2s(L, p, p-1); - L->top++; /* stack space pre-allocated by the caller */ - setobj2s(L, func, tm); /* metamethod is the new function to be called */ - return func; -} - - -/* -** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. -** Handle most typical cases (zero results for commands, one result for -** expressions, multiple results for tail calls/single parameters) -** separated. -*/ -l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { - StkId firstresult; - int i; - switch (wanted) { /* handle typical cases separately */ - case 0: /* no values needed */ - L->top = res; - return; - case 1: /* one value needed */ - if (nres == 0) /* no results? */ - setnilvalue(s2v(res)); /* adjust with nil */ - else /* at least one result */ - setobjs2s(L, res, L->top - nres); /* move it to proper place */ - L->top = res + 1; - return; - case LUA_MULTRET: - wanted = nres; /* we want all results */ - break; - default: /* two/more results and/or to-be-closed variables */ - if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ - ptrdiff_t savedres = savestack(L, res); - L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ - L->ci->u2.nres = nres; - luaF_close(L, res, CLOSEKTOP, 1); - L->ci->callstatus &= ~CIST_CLSRET; - if (L->hookmask) /* if needed, call hook after '__close's */ - rethook(L, L->ci, nres); - res = restorestack(L, savedres); /* close and hook can move stack */ - wanted = decodeNresults(wanted); - if (wanted == LUA_MULTRET) - wanted = nres; /* we want all results */ - } - break; - } - /* generic case */ - firstresult = L->top - nres; /* index of first result */ - if (nres > wanted) /* extra results? */ - nres = wanted; /* don't need them */ - for (i = 0; i < nres; i++) /* move all results to correct place */ - setobjs2s(L, res + i, firstresult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(s2v(res + i)); - L->top = res + wanted; /* top points after the last result */ -} - - -/* -** Finishes a function call: calls hook if necessary, moves current -** number of results to proper place, and returns to previous call -** info. If function has to close variables, hook must be called after -** that. -*/ -void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - int wanted = ci->nresults; - if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) - rethook(L, ci, nres); - /* move results to proper place */ - moveresults(L, ci->func, nres, wanted); - /* function cannot be in any of these cases when returning */ - lua_assert(!(ci->callstatus & - (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); - L->ci = ci->previous; /* back to caller (after closing variables) */ -} - - - -#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) - - -l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, - int mask, StkId top) { - CallInfo *ci = L->ci = next_ci(L); /* new frame */ - ci->func = func; - ci->nresults = nret; - ci->callstatus = mask; - ci->top = top; - return ci; -} - - -/* -** precall for C functions -*/ -l_sinline int precallC (lua_State *L, StkId func, int nresults, - lua_CFunction f) { - int n; /* number of returns */ - CallInfo *ci; - checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, - L->top + LUA_MINSTACK); - lua_assert(ci->top <= L->stack_last); - if (l_unlikely(L->hookmask & LUA_MASKCALL)) { - int narg = cast_int(L->top - func) - 1; - luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); - } - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, n); - return n; -} - - -/* -** Prepare a function for a tail call, building its call info on top -** of the current call info. 'narg1' is the number of arguments plus 1 -** (so that it includes the function itself). Return the number of -** results, if it was a C function, or -1 for a Lua function. -*/ -int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, - int narg1, int delta) { - retry: - switch (ttypetag(s2v(func))) { - case LUA_VCCL: /* C closure */ - return precallC(L, func, LUA_MULTRET, clCvalue(s2v(func))->f); - case LUA_VLCF: /* light C function */ - return precallC(L, func, LUA_MULTRET, fvalue(s2v(func))); - case LUA_VLCL: { /* Lua function */ - Proto *p = clLvalue(s2v(func))->p; - int fsize = p->maxstacksize; /* frame size */ - int nfixparams = p->numparams; - int i; - checkstackGCp(L, fsize - delta, func); - ci->func -= delta; /* restore 'func' (if vararg) */ - for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - func = ci->func; /* moved-down function */ - for (; narg1 <= nfixparams; narg1++) - setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ - return -1; - } - default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - /* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */ - narg1++; - goto retry; /* try again */ - } - } -} - - -/* -** Prepares the call to a function (C or Lua). For C functions, also do -** the call. The function to be called is at '*func'. The arguments -** are on the stack, right after the function. Returns the CallInfo -** to be executed, if it was a Lua function. Otherwise (a C function) -** returns NULL, with all the results on the stack, starting at the -** original function position. -*/ -CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { - retry: - switch (ttypetag(s2v(func))) { - case LUA_VCCL: /* C closure */ - precallC(L, func, nresults, clCvalue(s2v(func))->f); - return NULL; - case LUA_VLCF: /* light C function */ - precallC(L, func, nresults, fvalue(s2v(func))); - return NULL; - case LUA_VLCL: { /* Lua function */ - CallInfo *ci; - Proto *p = clLvalue(s2v(func))->p; - int narg = cast_int(L->top - func) - 1; /* number of real arguments */ - int nfixparams = p->numparams; - int fsize = p->maxstacksize; /* frame size */ - checkstackGCp(L, fsize, func); - L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); - ci->u.l.savedpc = p->code; /* starting point */ - for (; narg < nfixparams; narg++) - setnilvalue(s2v(L->top++)); /* complete missing arguments */ - lua_assert(ci->top <= L->stack_last); - return ci; - } - default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - /* return luaD_precall(L, func, nresults); */ - goto retry; /* try again with metamethod */ - } - } -} - - -/* -** Call a function (C or Lua) through C. 'inc' can be 1 (increment -** number of recursive invocations in the C stack) or nyci (the same -** plus increment number of non-yieldable calls). -*/ -l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { - CallInfo *ci; - L->nCcalls += inc; - if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) - luaE_checkcstack(L); - if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ - ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ - luaV_execute(L, ci); /* call it */ - } - L->nCcalls -= inc; -} - - -/* -** External interface for 'ccall' -*/ -void luaD_call (lua_State *L, StkId func, int nResults) { - ccall(L, func, nResults, 1); -} - - -/* -** Similar to 'luaD_call', but does not allow yields during the call. -*/ -void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - ccall(L, func, nResults, nyci); -} - - -/* -** Finish the job of 'lua_pcallk' after it was interrupted by an yield. -** (The caller, 'finishCcall', does the final call to 'adjustresults'.) -** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. -** If a '__close' method yields here, eventually control will be back -** to 'finishCcall' (when that '__close' method finally returns) and -** 'finishpcallk' will run again and close any still pending '__close' -** methods. Similarly, if a '__close' method errs, 'precover' calls -** 'unroll' which calls ''finishCcall' and we are back here again, to -** close any pending '__close' methods. -** Note that, up to the call to 'luaF_close', the corresponding -** 'CallInfo' is not modified, so that this repeated run works like the -** first one (except that it has at least one less '__close' to do). In -** particular, field CIST_RECST preserves the error status across these -** multiple runs, changing only if there is a new error. -*/ -static int finishpcallk (lua_State *L, CallInfo *ci) { - int status = getcistrecst(ci); /* get original status */ - if (l_likely(status == LUA_OK)) /* no error? */ - status = LUA_YIELD; /* was interrupted by an yield */ - else { /* error */ - StkId func = restorestack(L, ci->u2.funcidx); - L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ - luaF_close(L, func, status, 1); /* can yield or raise an error */ - func = restorestack(L, ci->u2.funcidx); /* stack may be moved */ - luaD_seterrorobj(L, status, func); - luaD_shrinkstack(L); /* restore stack size in case of overflow */ - setcistrecst(ci, LUA_OK); /* clear original status */ - } - ci->callstatus &= ~CIST_YPCALL; - L->errfunc = ci->u.c.old_errfunc; - /* if it is here, there were errors or yields; unlike 'lua_pcallk', - do not change status */ - return status; -} - - -/* -** Completes the execution of a C function interrupted by an yield. -** The interruption must have happened while the function was either -** closing its tbc variables in 'moveresults' or executing -** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes -** 'luaD_poscall'. In the second case, the call to 'finishpcallk' -** finishes the interrupted execution of 'lua_pcallk'. After that, it -** calls the continuation of the interrupted function and finally it -** completes the job of the 'luaD_call' that called the function. In -** the call to 'adjustresults', we do not know the number of results -** of the function called by 'lua_callk'/'lua_pcallk', so we are -** conservative and use LUA_MULTRET (always adjust). -*/ -static void finishCcall (lua_State *L, CallInfo *ci) { - int n; /* actual number of results from C function */ - if (ci->callstatus & CIST_CLSRET) { /* was returning? */ - lua_assert(hastocloseCfunc(ci->nresults)); - n = ci->u2.nres; /* just redo 'luaD_poscall' */ - /* don't need to reset CIST_CLSRET, as it will be set again anyway */ - } - else { - int status = LUA_YIELD; /* default if there were no errors */ - /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && yieldable(L)); - if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ - status = finishpcallk(L, ci); /* finish it */ - adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ - lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ - lua_lock(L); - api_checknelems(L, n); - } - luaD_poscall(L, ci, n); /* finish 'luaD_call' */ -} - - -/* -** Executes "full continuation" (everything in the stack) of a -** previously interrupted coroutine until the stack is empty (or another -** interruption long-jumps out of the loop). -*/ -static void unroll (lua_State *L, void *ud) { - CallInfo *ci; - UNUSED(ud); - while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ - if (!isLua(ci)) /* C function? */ - finishCcall(L, ci); /* complete its execution */ - else { /* Lua function */ - luaV_finishOp(L); /* finish interrupted instruction */ - luaV_execute(L, ci); /* execute down to higher C 'boundary' */ - } - } -} - - -/* -** Try to find a suspended protected call (a "recover point") for the -** given thread. -*/ -static CallInfo *findpcall (lua_State *L) { - CallInfo *ci; - for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ - if (ci->callstatus & CIST_YPCALL) - return ci; - } - return NULL; /* no pending pcall */ -} - - -/* -** Signal an error in the call to 'lua_resume', not in the execution -** of the coroutine itself. (Such errors should not be handled by any -** coroutine error handler and should not kill the coroutine.) -*/ -static int resume_error (lua_State *L, const char *msg, int narg) { - L->top -= narg; /* remove args from the stack */ - setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ - api_incr_top(L); - lua_unlock(L); - return LUA_ERRRUN; -} - - -/* -** Do the work for 'lua_resume' in protected mode. Most of the work -** depends on the status of the coroutine: initial state, suspended -** inside a hook, or regularly suspended (optionally with a continuation -** function), plus erroneous cases: non-suspended coroutine or dead -** coroutine. -*/ -static void resume (lua_State *L, void *ud) { - int n = *(cast(int*, ud)); /* number of arguments */ - StkId firstArg = L->top - n; /* first argument */ - CallInfo *ci = L->ci; - if (L->status == LUA_OK) /* starting a coroutine? */ - ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ - else { /* resuming from previous yield */ - lua_assert(L->status == LUA_YIELD); - L->status = LUA_OK; /* mark that it is running (again) */ - if (isLua(ci)) { /* yielded inside a hook? */ - L->top = firstArg; /* discard arguments */ - luaV_execute(L, ci); /* just continue running Lua code */ - } - else { /* 'common' yield */ - if (ci->u.c.k != NULL) { /* does it have a continuation function? */ - lua_unlock(L); - n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ - lua_lock(L); - api_checknelems(L, n); - } - luaD_poscall(L, ci, n); /* finish 'luaD_call' */ - } - unroll(L, NULL); /* run continuation */ - } -} - - -/* -** Unrolls a coroutine in protected mode while there are recoverable -** errors, that is, errors inside a protected call. (Any error -** interrupts 'unroll', and this loop protects it again so it can -** continue.) Stops with a normal end (status == LUA_OK), an yield -** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't -** find a recover point). -*/ -static int precover (lua_State *L, int status) { - CallInfo *ci; - while (errorstatus(status) && (ci = findpcall(L)) != NULL) { - L->ci = ci; /* go down to recovery functions */ - setcistrecst(ci, status); /* status to finish 'pcall' */ - status = luaD_rawrunprotected(L, unroll, NULL); - } - return status; -} - - -LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, - int *nresults) { - int status; - lua_lock(L); - if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (L->ci != &L->base_ci) /* not in base level? */ - return resume_error(L, "cannot resume non-suspended coroutine", nargs); - else if (L->top - (L->ci->func + 1) == nargs) /* no function? */ - return resume_error(L, "cannot resume dead coroutine", nargs); - } - else if (L->status != LUA_YIELD) /* ended with errors? */ - return resume_error(L, "cannot resume dead coroutine", nargs); - L->nCcalls = (from) ? getCcalls(from) : 0; - if (getCcalls(L) >= LUAI_MAXCCALLS) - return resume_error(L, "C stack overflow", nargs); - L->nCcalls++; - luai_userstateresume(L, nargs); - api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); - status = luaD_rawrunprotected(L, resume, &nargs); - /* continue running after recoverable errors */ - status = precover(L, status); - if (l_likely(!errorstatus(status))) - lua_assert(status == L->status); /* normal end or yield */ - else { /* unrecoverable error */ - L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->top); /* push error message */ - L->ci->top = L->top; - } - *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield - : cast_int(L->top - (L->ci->func + 1)); - lua_unlock(L); - return status; -} - - -LUA_API int lua_isyieldable (lua_State *L) { - return yieldable(L); -} - - -LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, - lua_KFunction k) { - CallInfo *ci; - luai_userstateyield(L, nresults); - lua_lock(L); - ci = L->ci; - api_checknelems(L, nresults); - if (l_unlikely(!yieldable(L))) { - if (L != G(L)->mainthread) - luaG_runerror(L, "attempt to yield across a C-call boundary"); - else - luaG_runerror(L, "attempt to yield from outside a coroutine"); - } - L->status = LUA_YIELD; - ci->u2.nyield = nresults; /* save number of results */ - if (isLua(ci)) { /* inside a hook? */ - lua_assert(!isLuacode(ci)); - api_check(L, nresults == 0, "hooks cannot yield values"); - api_check(L, k == NULL, "hooks cannot continue after yielding"); - } - else { - if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ - ci->u.c.ctx = ctx; /* save context */ - luaD_throw(L, LUA_YIELD); - } - lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ - lua_unlock(L); - return 0; /* return to 'luaD_hook' */ -} - - -/* -** Auxiliary structure to call 'luaF_close' in protected mode. -*/ -struct CloseP { - StkId level; - int status; -}; - - -/* -** Auxiliary function to call 'luaF_close' in protected mode. -*/ -static void closepaux (lua_State *L, void *ud) { - struct CloseP *pcl = cast(struct CloseP *, ud); - luaF_close(L, pcl->level, pcl->status, 0); -} - - -/* -** Calls 'luaF_close' in protected mode. Return the original status -** or, in case of errors, the new status. -*/ -int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { - CallInfo *old_ci = L->ci; - lu_byte old_allowhooks = L->allowhook; - for (;;) { /* keep closing upvalues until no more errors */ - struct CloseP pcl; - pcl.level = restorestack(L, level); pcl.status = status; - status = luaD_rawrunprotected(L, &closepaux, &pcl); - if (l_likely(status == LUA_OK)) /* no more errors? */ - return pcl.status; - else { /* an error occurred; restore saved state and repeat */ - L->ci = old_ci; - L->allowhook = old_allowhooks; - } - } -} - - -/* -** Call the C function 'func' in protected mode, restoring basic -** thread information ('allowhook', etc.) and in particular -** its stack level in case of errors. -*/ -int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t old_top, ptrdiff_t ef) { - int status; - CallInfo *old_ci = L->ci; - lu_byte old_allowhooks = L->allowhook; - ptrdiff_t old_errfunc = L->errfunc; - L->errfunc = ef; - status = luaD_rawrunprotected(L, func, u); - if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ - L->ci = old_ci; - L->allowhook = old_allowhooks; - status = luaD_closeprotected(L, old_top, status); - luaD_seterrorobj(L, status, restorestack(L, old_top)); - luaD_shrinkstack(L); /* restore stack size in case of overflow */ - } - L->errfunc = old_errfunc; - return status; -} - - - -/* -** Execute a protected parser. -*/ -struct SParser { /* data to 'f_parser' */ - ZIO *z; - Mbuffer buff; /* dynamic structure used by the scanner */ - Dyndata dyd; /* dynamic structures used by the parser */ - const char *mode; - const char *name; -}; - - -static void checkmode (lua_State *L, const char *mode, const char *x) { - if (mode && strchr(mode, x[0]) == NULL) { - luaO_pushfstring(L, - "attempt to load a %s chunk (mode is '%s')", x, mode); - luaD_throw(L, LUA_ERRSYNTAX); - } -} - - -static void f_parser (lua_State *L, void *ud) { - LClosure *cl; - struct SParser *p = cast(struct SParser *, ud); - int c = zgetc(p->z); /* read first character */ - if (c == LUA_SIGNATURE[0]) { - checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, p->name); - } - else { - checkmode(L, p->mode, "text"); - cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); - } - lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luaF_initupvals(L, cl); -} - - -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode) { - struct SParser p; - int status; - incnny(L); /* cannot yield during parsing */ - p.z = z; p.name = name; p.mode = mode; - p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; - p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; - p.dyd.label.arr = NULL; p.dyd.label.size = 0; - luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); - luaZ_freebuffer(L, &p.buff); - luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); - luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); - luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); - decnny(L); - return status; -} - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.h deleted file mode 100644 index 911e67f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldo.h +++ /dev/null @@ -1,79 +0,0 @@ -/* -** $Id: ldo.h $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#ifndef ldo_h -#define ldo_h - - -#include "lobject.h" -#include "lstate.h" -#include "lzio.h" - - -/* -** Macro to check stack size and grow stack if needed. Parameters -** 'pre'/'pos' allow the macro to preserve a pointer into the -** stack across reallocations, doing the work only when needed. -** It also allows the running of one GC step when the stack is -** reallocated. -** 'condmovestack' is used in heavy tests to force a stack reallocation -** at every check. -*/ -#define luaD_checkstackaux(L,n,pre,pos) \ - if (l_unlikely(L->stack_last - L->top <= (n))) \ - { pre; luaD_growstack(L, n, 1); pos; } \ - else { condmovestack(L,pre,pos); } - -/* In general, 'pre'/'pos' are empty (nothing to save) */ -#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) - - - -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((StkId)((char *)L->stack + (n))) - - -/* macro to check stack size, preserving 'p' */ -#define checkstackGCp(L,n,p) \ - luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ - luaC_checkGC(L), /* stack grow uses memory */ \ - p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ - - -/* macro to check stack size and GC */ -#define checkstackGC(L,fsize) \ - luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) - - -/* type of protected functions, to be ran by 'runprotected' */ -typedef void (*Pfunc) (lua_State *L, void *ud); - -LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode); -LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, - int fTransfer, int nTransfer); -LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); -LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); -LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); -LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); -LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); -LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); -LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); -LUAI_FUNC void luaD_shrinkstack (lua_State *L); -LUAI_FUNC void luaD_inctop (lua_State *L); - -LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldump.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldump.c deleted file mode 100644 index f848b66..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ldump.c +++ /dev/null @@ -1,226 +0,0 @@ -/* -** $Id: ldump.c $ -** save precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#define ldump_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lobject.h" -#include "lstate.h" -#include "lundump.h" - - -typedef struct { - lua_State *L; - lua_Writer writer; - void *data; - int strip; - int status; -} DumpState; - - -/* -** All high-level dumps go through dumpVector; you can change it to -** change the endianness of the result -*/ -#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) - -#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) - - -static void dumpBlock (DumpState *D, const void *b, size_t size) { - if (D->status == 0 && size > 0) { - lua_unlock(D->L); - D->status = (*D->writer)(D->L, b, size, D->data); - lua_lock(D->L); - } -} - - -#define dumpVar(D,x) dumpVector(D,&x,1) - - -static void dumpByte (DumpState *D, int y) { - lu_byte x = (lu_byte)y; - dumpVar(D, x); -} - - -/* dumpInt Buff Size */ -#define DIBS ((sizeof(size_t) * 8 / 7) + 1) - -static void dumpSize (DumpState *D, size_t x) { - lu_byte buff[DIBS]; - int n = 0; - do { - buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ - x >>= 7; - } while (x != 0); - buff[DIBS - 1] |= 0x80; /* mark last byte */ - dumpVector(D, buff + DIBS - n, n); -} - - -static void dumpInt (DumpState *D, int x) { - dumpSize(D, x); -} - - -static void dumpNumber (DumpState *D, lua_Number x) { - dumpVar(D, x); -} - - -static void dumpInteger (DumpState *D, lua_Integer x) { - dumpVar(D, x); -} - - -static void dumpString (DumpState *D, const TString *s) { - if (s == NULL) - dumpSize(D, 0); - else { - size_t size = tsslen(s); - const char *str = getstr(s); - dumpSize(D, size + 1); - dumpVector(D, str, size); - } -} - - -static void dumpCode (DumpState *D, const Proto *f) { - dumpInt(D, f->sizecode); - dumpVector(D, f->code, f->sizecode); -} - - -static void dumpFunction(DumpState *D, const Proto *f, TString *psource); - -static void dumpConstants (DumpState *D, const Proto *f) { - int i; - int n = f->sizek; - dumpInt(D, n); - for (i = 0; i < n; i++) { - const TValue *o = &f->k[i]; - int tt = ttypetag(o); - dumpByte(D, tt); - switch (tt) { - case LUA_VNUMFLT: - dumpNumber(D, fltvalue(o)); - break; - case LUA_VNUMINT: - dumpInteger(D, ivalue(o)); - break; - case LUA_VSHRSTR: - case LUA_VLNGSTR: - dumpString(D, tsvalue(o)); - break; - default: - lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); - } - } -} - - -static void dumpProtos (DumpState *D, const Proto *f) { - int i; - int n = f->sizep; - dumpInt(D, n); - for (i = 0; i < n; i++) - dumpFunction(D, f->p[i], f->source); -} - - -static void dumpUpvalues (DumpState *D, const Proto *f) { - int i, n = f->sizeupvalues; - dumpInt(D, n); - for (i = 0; i < n; i++) { - dumpByte(D, f->upvalues[i].instack); - dumpByte(D, f->upvalues[i].idx); - dumpByte(D, f->upvalues[i].kind); - } -} - - -static void dumpDebug (DumpState *D, const Proto *f) { - int i, n; - n = (D->strip) ? 0 : f->sizelineinfo; - dumpInt(D, n); - dumpVector(D, f->lineinfo, n); - n = (D->strip) ? 0 : f->sizeabslineinfo; - dumpInt(D, n); - for (i = 0; i < n; i++) { - dumpInt(D, f->abslineinfo[i].pc); - dumpInt(D, f->abslineinfo[i].line); - } - n = (D->strip) ? 0 : f->sizelocvars; - dumpInt(D, n); - for (i = 0; i < n; i++) { - dumpString(D, f->locvars[i].varname); - dumpInt(D, f->locvars[i].startpc); - dumpInt(D, f->locvars[i].endpc); - } - n = (D->strip) ? 0 : f->sizeupvalues; - dumpInt(D, n); - for (i = 0; i < n; i++) - dumpString(D, f->upvalues[i].name); -} - - -static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { - if (D->strip || f->source == psource) - dumpString(D, NULL); /* no debug info or same source as its parent */ - else - dumpString(D, f->source); - dumpInt(D, f->linedefined); - dumpInt(D, f->lastlinedefined); - dumpByte(D, f->numparams); - dumpByte(D, f->is_vararg); - dumpByte(D, f->maxstacksize); - dumpCode(D, f); - dumpConstants(D, f); - dumpUpvalues(D, f); - dumpProtos(D, f); - dumpDebug(D, f); -} - - -static void dumpHeader (DumpState *D) { - dumpLiteral(D, LUA_SIGNATURE); - dumpByte(D, LUAC_VERSION); - dumpByte(D, LUAC_FORMAT); - dumpLiteral(D, LUAC_DATA); - dumpByte(D, sizeof(Instruction)); - dumpByte(D, sizeof(lua_Integer)); - dumpByte(D, sizeof(lua_Number)); - dumpInteger(D, LUAC_INT); - dumpNumber(D, LUAC_NUM); -} - - -/* -** dump Lua function as precompiled chunk -*/ -int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, - int strip) { - DumpState D; - D.L = L; - D.writer = w; - D.data = data; - D.strip = strip; - D.status = 0; - dumpHeader(&D); - dumpByte(&D, f->sizeupvalues); - dumpFunction(&D, f, NULL); - return D.status; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.c deleted file mode 100644 index f5889a2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.c +++ /dev/null @@ -1,294 +0,0 @@ -/* -** $Id: lfunc.c $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - -#define lfunc_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - - -CClosure *luaF_newCclosure (lua_State *L, int nupvals) { - GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); - CClosure *c = gco2ccl(o); - c->nupvalues = cast_byte(nupvals); - return c; -} - - -LClosure *luaF_newLclosure (lua_State *L, int nupvals) { - GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); - LClosure *c = gco2lcl(o); - c->p = NULL; - c->nupvalues = cast_byte(nupvals); - while (nupvals--) c->upvals[nupvals] = NULL; - return c; -} - - -/* -** fill a closure with new closed upvalues -*/ -void luaF_initupvals (lua_State *L, LClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) { - GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); - UpVal *uv = gco2upv(o); - uv->v = &uv->u.value; /* make it closed */ - setnilvalue(uv->v); - cl->upvals[i] = uv; - luaC_objbarrier(L, cl, uv); - } -} - - -/* -** Create a new upvalue at the given level, and link it to the list of -** open upvalues of 'L' after entry 'prev'. -**/ -static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { - GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); - UpVal *uv = gco2upv(o); - UpVal *next = *prev; - uv->v = s2v(level); /* current value lives in the stack */ - uv->tbc = tbc; - uv->u.open.next = next; /* link it to list of open upvalues */ - uv->u.open.previous = prev; - if (next) - next->u.open.previous = &uv->u.open.next; - *prev = uv; - if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ - L->twups = G(L)->twups; /* link it to the list */ - G(L)->twups = L; - } - return uv; -} - - -/* -** Find and reuse, or create if it does not exist, an upvalue -** at the given level. -*/ -UpVal *luaF_findupval (lua_State *L, StkId level) { - UpVal **pp = &L->openupval; - UpVal *p; - lua_assert(isintwups(L) || L->openupval == NULL); - while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ - lua_assert(!isdead(G(L), p)); - if (uplevel(p) == level) /* corresponding upvalue? */ - return p; /* return it */ - pp = &p->u.open.next; - } - /* not found: create a new upvalue after 'pp' */ - return newupval(L, 0, level, pp); -} - - -/* -** Call closing method for object 'obj' with error message 'err'. The -** boolean 'yy' controls whether the call is yieldable. -** (This function assumes EXTRA_STACK.) -*/ -static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { - StkId top = L->top; - const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - setobj2s(L, top, tm); /* will call metamethod... */ - setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ - setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ - L->top = top + 3; /* add function and arguments */ - if (yy) - luaD_call(L, top, 0); - else - luaD_callnoyield(L, top, 0); -} - - -/* -** Check whether object at given level has a close metamethod and raise -** an error if not. -*/ -static void checkclosemth (lua_State *L, StkId level) { - const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); - if (ttisnil(tm)) { /* no metamethod? */ - int idx = cast_int(level - L->ci->func); /* variable index */ - const char *vname = luaG_findlocal(L, L->ci, idx, NULL); - if (vname == NULL) vname = "?"; - luaG_runerror(L, "variable '%s' got a non-closable value", vname); - } -} - - -/* -** Prepare and call a closing method. -** If status is CLOSEKTOP, the call to the closing method will be pushed -** at the top of the stack. Otherwise, values can be pushed right after -** the 'level' of the upvalue being closed, as everything after that -** won't be used again. -*/ -static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { - TValue *uv = s2v(level); /* value being closed */ - TValue *errobj; - if (status == CLOSEKTOP) - errobj = &G(L)->nilvalue; /* error object is nil */ - else { /* 'luaD_seterrorobj' will set top to level + 2 */ - errobj = s2v(level + 1); /* error object goes after 'uv' */ - luaD_seterrorobj(L, status, level + 1); /* set error object */ - } - callclosemethod(L, uv, errobj, yy); -} - - -/* -** Maximum value for deltas in 'tbclist', dependent on the type -** of delta. (This macro assumes that an 'L' is in scope where it -** is used.) -*/ -#define MAXDELTA \ - ((256ul << ((sizeof(L->stack->tbclist.delta) - 1) * 8)) - 1) - - -/* -** Insert a variable in the list of to-be-closed variables. -*/ -void luaF_newtbcupval (lua_State *L, StkId level) { - lua_assert(level > L->tbclist); - if (l_isfalse(s2v(level))) - return; /* false doesn't need to be closed */ - checkclosemth(L, level); /* value must have a close method */ - while (cast_uint(level - L->tbclist) > MAXDELTA) { - L->tbclist += MAXDELTA; /* create a dummy node at maximum delta */ - L->tbclist->tbclist.delta = 0; - } - level->tbclist.delta = cast(unsigned short, level - L->tbclist); - L->tbclist = level; -} - - -void luaF_unlinkupval (UpVal *uv) { - lua_assert(upisopen(uv)); - *uv->u.open.previous = uv->u.open.next; - if (uv->u.open.next) - uv->u.open.next->u.open.previous = uv->u.open.previous; -} - - -/* -** Close all upvalues up to the given stack level. -*/ -void luaF_closeupval (lua_State *L, StkId level) { - UpVal *uv; - StkId upl; /* stack index pointed by 'uv' */ - while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { - TValue *slot = &uv->u.value; /* new position for value */ - lua_assert(uplevel(uv) < L->top); - luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ - setobj(L, slot, uv->v); /* move value to upvalue slot */ - uv->v = slot; /* now current value lives here */ - if (!iswhite(uv)) { /* neither white nor dead? */ - nw2black(uv); /* closed upvalues cannot be gray */ - luaC_barrier(L, uv, slot); - } - } -} - - -/* -** Remove firt element from the tbclist plus its dummy nodes. -*/ -static void poptbclist (lua_State *L) { - StkId tbc = L->tbclist; - lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ - tbc -= tbc->tbclist.delta; - while (tbc > L->stack && tbc->tbclist.delta == 0) - tbc -= MAXDELTA; /* remove dummy nodes */ - L->tbclist = tbc; -} - - -/* -** Close all upvalues and to-be-closed variables up to the given stack -** level. -*/ -void luaF_close (lua_State *L, StkId level, int status, int yy) { - ptrdiff_t levelrel = savestack(L, level); - luaF_closeupval(L, level); /* first, close the upvalues */ - while (L->tbclist >= level) { /* traverse tbc's down to that level */ - StkId tbc = L->tbclist; /* get variable index */ - poptbclist(L); /* remove it from list */ - prepcallclosemth(L, tbc, status, yy); /* close variable */ - level = restorestack(L, levelrel); - } -} - - -Proto *luaF_newproto (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); - Proto *f = gco2p(o); - f->k = NULL; - f->sizek = 0; - f->p = NULL; - f->sizep = 0; - f->code = NULL; - f->sizecode = 0; - f->lineinfo = NULL; - f->sizelineinfo = 0; - f->abslineinfo = NULL; - f->sizeabslineinfo = 0; - f->upvalues = NULL; - f->sizeupvalues = 0; - f->numparams = 0; - f->is_vararg = 0; - f->maxstacksize = 0; - f->locvars = NULL; - f->sizelocvars = 0; - f->linedefined = 0; - f->lastlinedefined = 0; - f->source = NULL; - return f; -} - - -void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); - luaM_free(L, f); -} - - -/* -** Look for n-th local variable at line 'line' in function 'func'. -** Returns NULL if not found. -*/ -const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { - int i; - for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { - if (pc < f->locvars[i].endpc) { /* is variable active? */ - local_number--; - if (local_number == 0) - return getstr(f->locvars[i].varname); - } - } - return NULL; /* not found */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.h deleted file mode 100644 index dc1cebc..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lfunc.h +++ /dev/null @@ -1,64 +0,0 @@ -/* -** $Id: lfunc.h $ -** Auxiliary functions to manipulate prototypes and closures -** See Copyright Notice in lua.h -*/ - -#ifndef lfunc_h -#define lfunc_h - - -#include "lobject.h" - - -#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ - cast_int(sizeof(TValue)) * (n)) - -#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ - cast_int(sizeof(TValue *)) * (n)) - - -/* test whether thread is in 'twups' list */ -#define isintwups(L) (L->twups != L) - - -/* -** maximum number of upvalues in a closure (both C and Lua). (Value -** must fit in a VM register.) -*/ -#define MAXUPVAL 255 - - -#define upisopen(up) ((up)->v != &(up)->u.value) - - -#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) - - -/* -** maximum number of misses before giving up the cache of closures -** in prototypes -*/ -#define MAXMISS 10 - - - -/* special status to close upvalues preserving the top of the stack */ -#define CLOSEKTOP (-1) - - -LUAI_FUNC Proto *luaF_newproto (lua_State *L); -LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); -LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); -LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); -LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); -LUAI_FUNC void luaF_unlinkupval (UpVal *uv); -LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); -LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, - int pc); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.c deleted file mode 100644 index 42a73d8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.c +++ /dev/null @@ -1,1731 +0,0 @@ -/* -** $Id: lgc.c $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#define lgc_c -#define LUA_CORE - -#include "lprefix.h" - -#include -#include - - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - -/* -** Maximum number of elements to sweep in each single step. -** (Large enough to dissipate fixed overheads but small enough -** to allow small steps for the collector.) -*/ -#define GCSWEEPMAX 100 - -/* -** Maximum number of finalizers to call in each single step. -*/ -#define GCFINMAX 10 - - -/* -** Cost of calling one finalizer. -*/ -#define GCFINALIZECOST 50 - - -/* -** The equivalent, in bytes, of one unit of "work" (visiting a slot, -** sweeping an object, etc.) -*/ -#define WORK2MEM sizeof(TValue) - - -/* -** macro to adjust 'pause': 'pause' is actually used like -** 'pause / PAUSEADJ' (value chosen by tests) -*/ -#define PAUSEADJ 100 - - -/* mask with all color bits */ -#define maskcolors (bitmask(BLACKBIT) | WHITEBITS) - -/* mask with all GC bits */ -#define maskgcbits (maskcolors | AGEBITS) - - -/* macro to erase all color bits then set only the current white bit */ -#define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g))) - -/* make an object gray (neither white nor black) */ -#define set2gray(x) resetbits(x->marked, maskcolors) - - -/* make an object black (coming from any color) */ -#define set2black(x) \ - (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) - - -#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) - -#define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) - - -/* -** Protected access to objects in values -*/ -#define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) - - -#define markvalue(g,o) { checkliveness(g->mainthread,o); \ - if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } - -#define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } - -#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } - -/* -** mark an object that can be NULL (either because it is really optional, -** or it was stripped as debug info, or inside an uncompleted structure) -*/ -#define markobjectN(g,t) { if (t) markobject(g,t); } - -static void reallymarkobject (global_State *g, GCObject *o); -static lu_mem atomic (lua_State *L); -static void entersweep (lua_State *L); - - -/* -** {====================================================== -** Generic functions -** ======================================================= -*/ - - -/* -** one after last element in a hash array -*/ -#define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) - - -static GCObject **getgclist (GCObject *o) { - switch (o->tt) { - case LUA_VTABLE: return &gco2t(o)->gclist; - case LUA_VLCL: return &gco2lcl(o)->gclist; - case LUA_VCCL: return &gco2ccl(o)->gclist; - case LUA_VTHREAD: return &gco2th(o)->gclist; - case LUA_VPROTO: return &gco2p(o)->gclist; - case LUA_VUSERDATA: { - Udata *u = gco2u(o); - lua_assert(u->nuvalue > 0); - return &u->gclist; - } - default: lua_assert(0); return 0; - } -} - - -/* -** Link a collectable object 'o' with a known type into the list 'p'. -** (Must be a macro to access the 'gclist' field in different types.) -*/ -#define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p)) - -static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { - lua_assert(!isgray(o)); /* cannot be in a gray list */ - *pnext = *list; - *list = o; - set2gray(o); /* now it is */ -} - - -/* -** Link a generic collectable object 'o' into the list 'p'. -*/ -#define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p)) - - - -/* -** Clear keys for empty entries in tables. If entry is empty, mark its -** entry as dead. This allows the collection of the key, but keeps its -** entry in the table: its removal could break a chain and could break -** a table traversal. Other places never manipulate dead keys, because -** its associated empty value is enough to signal that the entry is -** logically empty. -*/ -static void clearkey (Node *n) { - lua_assert(isempty(gval(n))); - if (keyiscollectable(n)) - setdeadkey(n); /* unused key; remove it */ -} - - -/* -** tells whether a key or value can be cleared from a weak -** table. Non-collectable objects are never removed from weak -** tables. Strings behave as 'values', so are never removed too. for -** other objects: if really collected, cannot keep them; for objects -** being finalized, keep them in keys, but not in values -*/ -static int iscleared (global_State *g, const GCObject *o) { - if (o == NULL) return 0; /* non-collectable value */ - else if (novariant(o->tt) == LUA_TSTRING) { - markobject(g, o); /* strings are 'values', so are never weak */ - return 0; - } - else return iswhite(o); -} - - -/* -** Barrier that moves collector forward, that is, marks the white object -** 'v' being pointed by the black object 'o'. In the generational -** mode, 'v' must also become old, if 'o' is old; however, it cannot -** be changed directly to OLD, because it may still point to non-old -** objects. So, it is marked as OLD0. In the next cycle it will become -** OLD1, and in the next it will finally become OLD (regular old). By -** then, any object it points to will also be old. If called in the -** incremental sweep phase, it clears the black object to white (sweep -** it) to avoid other barrier calls for this same object. (That cannot -** be done is generational mode, as its sweep does not distinguish -** whites from deads.) -*/ -void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { - global_State *g = G(L); - lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - if (keepinvariant(g)) { /* must keep invariant? */ - reallymarkobject(g, v); /* restore invariant */ - if (isold(o)) { - lua_assert(!isold(v)); /* white object could not be old */ - setage(v, G_OLD0); /* restore generational invariant */ - } - } - else { /* sweep phase */ - lua_assert(issweepphase(g)); - if (g->gckind == KGC_INC) /* incremental mode? */ - makewhite(g, o); /* mark 'o' as white to avoid other barriers */ - } -} - - -/* -** barrier that moves collector backward, that is, mark the black object -** pointing to a white object as gray again. -*/ -void luaC_barrierback_ (lua_State *L, GCObject *o) { - global_State *g = G(L); - lua_assert(isblack(o) && !isdead(g, o)); - lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); - if (getage(o) == G_TOUCHED2) /* already in gray list? */ - set2gray(o); /* make it gray to become touched1 */ - else /* link it in 'grayagain' and paint it gray */ - linkobjgclist(o, g->grayagain); - if (isold(o)) /* generational mode? */ - setage(o, G_TOUCHED1); /* touched in current cycle */ -} - - -void luaC_fix (lua_State *L, GCObject *o) { - global_State *g = G(L); - lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ - set2gray(o); /* they will be gray forever */ - setage(o, G_OLD); /* and old forever */ - g->allgc = o->next; /* remove object from 'allgc' list */ - o->next = g->fixedgc; /* link it to 'fixedgc' list */ - g->fixedgc = o; -} - - -/* -** create a new collectable object (with given type and size) and link -** it to 'allgc' list. -*/ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { - global_State *g = G(L); - GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); - o->marked = luaC_white(g); - o->tt = tt; - o->next = g->allgc; - g->allgc = o; - return o; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Mark functions -** ======================================================= -*/ - - -/* -** Mark an object. Userdata with no user values, strings, and closed -** upvalues are visited and turned black here. Open upvalues are -** already indirectly linked through their respective threads in the -** 'twups' list, so they don't go to the gray list; nevertheless, they -** are kept gray to avoid barriers, as their values will be revisited -** by the thread or by 'remarkupvals'. Other objects are added to the -** gray list to be visited (and turned black) later. Both userdata and -** upvalues can call this function recursively, but this recursion goes -** for at most two levels: An upvalue cannot refer to another upvalue -** (only closures can), and a userdata's metatable must be a table. -*/ -static void reallymarkobject (global_State *g, GCObject *o) { - switch (o->tt) { - case LUA_VSHRSTR: - case LUA_VLNGSTR: { - set2black(o); /* nothing to visit */ - break; - } - case LUA_VUPVAL: { - UpVal *uv = gco2upv(o); - if (upisopen(uv)) - set2gray(uv); /* open upvalues are kept gray */ - else - set2black(uv); /* closed upvalues are visited here */ - markvalue(g, uv->v); /* mark its content */ - break; - } - case LUA_VUSERDATA: { - Udata *u = gco2u(o); - if (u->nuvalue == 0) { /* no user values? */ - markobjectN(g, u->metatable); /* mark its metatable */ - set2black(u); /* nothing else to mark */ - break; - } - /* else... */ - } /* FALLTHROUGH */ - case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: - case LUA_VTHREAD: case LUA_VPROTO: { - linkobjgclist(o, g->gray); /* to be visited later */ - break; - } - default: lua_assert(0); break; - } -} - - -/* -** mark metamethods for basic types -*/ -static void markmt (global_State *g) { - int i; - for (i=0; i < LUA_NUMTAGS; i++) - markobjectN(g, g->mt[i]); -} - - -/* -** mark all objects in list of being-finalized -*/ -static lu_mem markbeingfnz (global_State *g) { - GCObject *o; - lu_mem count = 0; - for (o = g->tobefnz; o != NULL; o = o->next) { - count++; - markobject(g, o); - } - return count; -} - - -/* -** For each non-marked thread, simulates a barrier between each open -** upvalue and its value. (If the thread is collected, the value will be -** assigned to the upvalue, but then it can be too late for the barrier -** to act. The "barrier" does not need to check colors: A non-marked -** thread must be young; upvalues cannot be older than their threads; so -** any visited upvalue must be young too.) Also removes the thread from -** the list, as it was already visited. Removes also threads with no -** upvalues, as they have nothing to be checked. (If the thread gets an -** upvalue later, it will be linked in the list again.) -*/ -static int remarkupvals (global_State *g) { - lua_State *thread; - lua_State **p = &g->twups; - int work = 0; /* estimate of how much work was done here */ - while ((thread = *p) != NULL) { - work++; - if (!iswhite(thread) && thread->openupval != NULL) - p = &thread->twups; /* keep marked thread with upvalues in the list */ - else { /* thread is not marked or without upvalues */ - UpVal *uv; - lua_assert(!isold(thread) || thread->openupval == NULL); - *p = thread->twups; /* remove thread from the list */ - thread->twups = thread; /* mark that it is out of list */ - for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { - lua_assert(getage(uv) <= getage(thread)); - work++; - if (!iswhite(uv)) { /* upvalue already visited? */ - lua_assert(upisopen(uv) && isgray(uv)); - markvalue(g, uv->v); /* mark its value */ - } - } - } - } - return work; -} - - -static void cleargraylists (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; -} - - -/* -** mark root set and reset all gray lists, to start a new collection -*/ -static void restartcollection (global_State *g) { - cleargraylists(g); - markobject(g, g->mainthread); - markvalue(g, &g->l_registry); - markmt(g); - markbeingfnz(g); /* mark any finalizing object left from previous cycle */ -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Traverse functions -** ======================================================= -*/ - - -/* -** Check whether object 'o' should be kept in the 'grayagain' list for -** post-processing by 'correctgraylist'. (It could put all old objects -** in the list and leave all the work to 'correctgraylist', but it is -** more efficient to avoid adding elements that will be removed.) Only -** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go -** back to a gray list, but then it must become OLD. (That is what -** 'correctgraylist' does when it finds a TOUCHED2 object.) -*/ -static void genlink (global_State *g, GCObject *o) { - lua_assert(isblack(o)); - if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ - linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ - } /* everything else do not need to be linked back */ - else if (getage(o) == G_TOUCHED2) - changeage(o, G_TOUCHED2, G_OLD); /* advance age */ -} - - -/* -** Traverse a table with weak values and link it to proper list. During -** propagate phase, keep it in 'grayagain' list, to be revisited in the -** atomic phase. In the atomic phase, if table has any white value, -** put it in 'weak' list, to be cleared. -*/ -static void traverseweakvalue (global_State *g, Table *h) { - Node *n, *limit = gnodelast(h); - /* if there is array part, assume it may have white values (it is not - worth traversing it now just to check) */ - int hasclears = (h->alimit > 0); - for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - if (isempty(gval(n))) /* entry is empty? */ - clearkey(n); /* clear its key */ - else { - lua_assert(!keyisnil(n)); - markkey(g, n); - if (!hasclears && iscleared(g, gcvalueN(gval(n)))) /* a white value? */ - hasclears = 1; /* table will have to be cleared */ - } - } - if (g->gcstate == GCSatomic && hasclears) - linkgclist(h, g->weak); /* has to be cleared later */ - else - linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ -} - - -/* -** Traverse an ephemeron table and link it to proper list. Returns true -** iff any object was marked during this traversal (which implies that -** convergence has to continue). During propagation phase, keep table -** in 'grayagain' list, to be visited again in the atomic phase. In -** the atomic phase, if table has any white->white entry, it has to -** be revisited during ephemeron convergence (as that key may turn -** black). Otherwise, if it has any white key, table has to be cleared -** (in the atomic phase). In generational mode, some tables -** must be kept in some gray list for post-processing; this is done -** by 'genlink'. -*/ -static int traverseephemeron (global_State *g, Table *h, int inv) { - int marked = 0; /* true if an object is marked in this traversal */ - int hasclears = 0; /* true if table has white keys */ - int hasww = 0; /* true if table has entry "white-key -> white-value" */ - unsigned int i; - unsigned int asize = luaH_realasize(h); - unsigned int nsize = sizenode(h); - /* traverse array part */ - for (i = 0; i < asize; i++) { - if (valiswhite(&h->array[i])) { - marked = 1; - reallymarkobject(g, gcvalue(&h->array[i])); - } - } - /* traverse hash part; if 'inv', traverse descending - (see 'convergeephemerons') */ - for (i = 0; i < nsize; i++) { - Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i); - if (isempty(gval(n))) /* entry is empty? */ - clearkey(n); /* clear its key */ - else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ - hasclears = 1; /* table must be cleared */ - if (valiswhite(gval(n))) /* value not marked yet? */ - hasww = 1; /* white-white entry */ - } - else if (valiswhite(gval(n))) { /* value not marked yet? */ - marked = 1; - reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ - } - } - /* link table into proper list */ - if (g->gcstate == GCSpropagate) - linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ - else if (hasww) /* table has white->white entries? */ - linkgclist(h, g->ephemeron); /* have to propagate again */ - else if (hasclears) /* table has white keys? */ - linkgclist(h, g->allweak); /* may have to clean white keys */ - else - genlink(g, obj2gco(h)); /* check whether collector still needs to see it */ - return marked; -} - - -static void traversestrongtable (global_State *g, Table *h) { - Node *n, *limit = gnodelast(h); - unsigned int i; - unsigned int asize = luaH_realasize(h); - for (i = 0; i < asize; i++) /* traverse array part */ - markvalue(g, &h->array[i]); - for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - if (isempty(gval(n))) /* entry is empty? */ - clearkey(n); /* clear its key */ - else { - lua_assert(!keyisnil(n)); - markkey(g, n); - markvalue(g, gval(n)); - } - } - genlink(g, obj2gco(h)); -} - - -static lu_mem traversetable (global_State *g, Table *h) { - const char *weakkey, *weakvalue; - const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - markobjectN(g, h->metatable); - if (mode && ttisstring(mode) && /* is there a weak mode? */ - (cast_void(weakkey = strchr(svalue(mode), 'k')), - cast_void(weakvalue = strchr(svalue(mode), 'v')), - (weakkey || weakvalue))) { /* is really weak? */ - if (!weakkey) /* strong keys? */ - traverseweakvalue(g, h); - else if (!weakvalue) /* strong values? */ - traverseephemeron(g, h, 0); - else /* all weak */ - linkgclist(h, g->allweak); /* nothing to traverse now */ - } - else /* not weak */ - traversestrongtable(g, h); - return 1 + h->alimit + 2 * allocsizenode(h); -} - - -static int traverseudata (global_State *g, Udata *u) { - int i; - markobjectN(g, u->metatable); /* mark its metatable */ - for (i = 0; i < u->nuvalue; i++) - markvalue(g, &u->uv[i].uv); - genlink(g, obj2gco(u)); - return 1 + u->nuvalue; -} - - -/* -** Traverse a prototype. (While a prototype is being build, its -** arrays can be larger than needed; the extra slots are filled with -** NULL, so the use of 'markobjectN') -*/ -static int traverseproto (global_State *g, Proto *f) { - int i; - markobjectN(g, f->source); - for (i = 0; i < f->sizek; i++) /* mark literals */ - markvalue(g, &f->k[i]); - for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */ - markobjectN(g, f->upvalues[i].name); - for (i = 0; i < f->sizep; i++) /* mark nested protos */ - markobjectN(g, f->p[i]); - for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ - markobjectN(g, f->locvars[i].varname); - return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars; -} - - -static int traverseCclosure (global_State *g, CClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ - markvalue(g, &cl->upvalue[i]); - return 1 + cl->nupvalues; -} - -/* -** Traverse a Lua closure, marking its prototype and its upvalues. -** (Both can be NULL while closure is being created.) -*/ -static int traverseLclosure (global_State *g, LClosure *cl) { - int i; - markobjectN(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ - UpVal *uv = cl->upvals[i]; - markobjectN(g, uv); /* mark upvalue */ - } - return 1 + cl->nupvalues; -} - - -/* -** Traverse a thread, marking the elements in the stack up to its top -** and cleaning the rest of the stack in the final traversal. That -** ensures that the entire stack have valid (non-dead) objects. -** Threads have no barriers. In gen. mode, old threads must be visited -** at every cycle, because they might point to young objects. In inc. -** mode, the thread can still be modified before the end of the cycle, -** and therefore it must be visited again in the atomic phase. To ensure -** these visits, threads must return to a gray list if they are not new -** (which can only happen in generational mode) or if the traverse is in -** the propagate phase (which can only happen in incremental mode). -*/ -static int traversethread (global_State *g, lua_State *th) { - UpVal *uv; - StkId o = th->stack; - if (isold(th) || g->gcstate == GCSpropagate) - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - if (o == NULL) - return 1; /* stack not completely built yet */ - lua_assert(g->gcstate == GCSatomic || - th->openupval == NULL || isintwups(th)); - for (; o < th->top; o++) /* mark live elements in the stack */ - markvalue(g, s2v(o)); - for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) - markobject(g, uv); /* open upvalues cannot be collected */ - if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last + EXTRA_STACK; o++) - setnilvalue(s2v(o)); /* clear dead stack slice */ - /* 'remarkupvals' may have removed thread from 'twups' list */ - if (!isintwups(th) && th->openupval != NULL) { - th->twups = g->twups; /* link it back to the list */ - g->twups = th; - } - } - else if (!g->gcemergency) - luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return 1 + stacksize(th); -} - - -/* -** traverse one gray object, turning it to black. -*/ -static lu_mem propagatemark (global_State *g) { - GCObject *o = g->gray; - nw2black(o); - g->gray = *getgclist(o); /* remove from 'gray' list */ - switch (o->tt) { - case LUA_VTABLE: return traversetable(g, gco2t(o)); - case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); - case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); - case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); - case LUA_VPROTO: return traverseproto(g, gco2p(o)); - case LUA_VTHREAD: return traversethread(g, gco2th(o)); - default: lua_assert(0); return 0; - } -} - - -static lu_mem propagateall (global_State *g) { - lu_mem tot = 0; - while (g->gray) - tot += propagatemark(g); - return tot; -} - - -/* -** Traverse all ephemeron tables propagating marks from keys to values. -** Repeat until it converges, that is, nothing new is marked. 'dir' -** inverts the direction of the traversals, trying to speed up -** convergence on chains in the same table. -** -*/ -static void convergeephemerons (global_State *g) { - int changed; - int dir = 0; - do { - GCObject *w; - GCObject *next = g->ephemeron; /* get ephemeron list */ - g->ephemeron = NULL; /* tables may return to this list when traversed */ - changed = 0; - while ((w = next) != NULL) { /* for each ephemeron table */ - Table *h = gco2t(w); - next = h->gclist; /* list is rebuilt during loop */ - nw2black(h); /* out of the list (for now) */ - if (traverseephemeron(g, h, dir)) { /* marked some value? */ - propagateall(g); /* propagate changes */ - changed = 1; /* will have to revisit all ephemeron tables */ - } - } - dir = !dir; /* invert direction next time */ - } while (changed); /* repeat until no more changes */ -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Sweep Functions -** ======================================================= -*/ - - -/* -** clear entries with unmarked keys from all weaktables in list 'l' -*/ -static void clearbykeys (global_State *g, GCObject *l) { - for (; l; l = gco2t(l)->gclist) { - Table *h = gco2t(l); - Node *limit = gnodelast(h); - Node *n; - for (n = gnode(h, 0); n < limit; n++) { - if (iscleared(g, gckeyN(n))) /* unmarked key? */ - setempty(gval(n)); /* remove entry */ - if (isempty(gval(n))) /* is entry empty? */ - clearkey(n); /* clear its key */ - } - } -} - - -/* -** clear entries with unmarked values from all weaktables in list 'l' up -** to element 'f' -*/ -static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { - Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); - unsigned int i; - unsigned int asize = luaH_realasize(h); - for (i = 0; i < asize; i++) { - TValue *o = &h->array[i]; - if (iscleared(g, gcvalueN(o))) /* value was collected? */ - setempty(o); /* remove entry */ - } - for (n = gnode(h, 0); n < limit; n++) { - if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ - setempty(gval(n)); /* remove entry */ - if (isempty(gval(n))) /* is entry empty? */ - clearkey(n); /* clear its key */ - } - } -} - - -static void freeupval (lua_State *L, UpVal *uv) { - if (upisopen(uv)) - luaF_unlinkupval(uv); - luaM_free(L, uv); -} - - -static void freeobj (lua_State *L, GCObject *o) { - switch (o->tt) { - case LUA_VPROTO: - luaF_freeproto(L, gco2p(o)); - break; - case LUA_VUPVAL: - freeupval(L, gco2upv(o)); - break; - case LUA_VLCL: { - LClosure *cl = gco2lcl(o); - luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); - break; - } - case LUA_VCCL: { - CClosure *cl = gco2ccl(o); - luaM_freemem(L, cl, sizeCclosure(cl->nupvalues)); - break; - } - case LUA_VTABLE: - luaH_free(L, gco2t(o)); - break; - case LUA_VTHREAD: - luaE_freethread(L, gco2th(o)); - break; - case LUA_VUSERDATA: { - Udata *u = gco2u(o); - luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); - break; - } - case LUA_VSHRSTR: { - TString *ts = gco2ts(o); - luaS_remove(L, ts); /* remove it from hash table */ - luaM_freemem(L, ts, sizelstring(ts->shrlen)); - break; - } - case LUA_VLNGSTR: { - TString *ts = gco2ts(o); - luaM_freemem(L, ts, sizelstring(ts->u.lnglen)); - break; - } - default: lua_assert(0); - } -} - - -/* -** sweep at most 'countin' elements from a list of GCObjects erasing dead -** objects, where a dead object is one marked with the old (non current) -** white; change all non-dead objects back to white, preparing for next -** collection cycle. Return where to continue the traversal or NULL if -** list is finished. ('*countout' gets the number of elements traversed.) -*/ -static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, - int *countout) { - global_State *g = G(L); - int ow = otherwhite(g); - int i; - int white = luaC_white(g); /* current white */ - for (i = 0; *p != NULL && i < countin; i++) { - GCObject *curr = *p; - int marked = curr->marked; - if (isdeadm(ow, marked)) { /* is 'curr' dead? */ - *p = curr->next; /* remove 'curr' from list */ - freeobj(L, curr); /* erase 'curr' */ - } - else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & ~maskgcbits) | white); - p = &curr->next; /* go to next element */ - } - } - if (countout) - *countout = i; /* number of elements traversed */ - return (*p == NULL) ? NULL : p; -} - - -/* -** sweep a list until a live object (or end of list) -*/ -static GCObject **sweeptolive (lua_State *L, GCObject **p) { - GCObject **old = p; - do { - p = sweeplist(L, p, 1, NULL); - } while (p == old); - return p; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Finalization -** ======================================================= -*/ - -/* -** If possible, shrink string table. -*/ -static void checkSizes (lua_State *L, global_State *g) { - if (!g->gcemergency) { - if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */ - l_mem olddebt = g->GCdebt; - luaS_resize(L, g->strt.size / 2); - g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ - } - } -} - - -/* -** Get the next udata to be finalized from the 'tobefnz' list, and -** link it back into the 'allgc' list. -*/ -static GCObject *udata2finalize (global_State *g) { - GCObject *o = g->tobefnz; /* get first element */ - lua_assert(tofinalize(o)); - g->tobefnz = o->next; /* remove it from 'tobefnz' list */ - o->next = g->allgc; /* return it to 'allgc' list */ - g->allgc = o; - resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ - if (issweepphase(g)) - makewhite(g, o); /* "sweep" object */ - else if (getage(o) == G_OLD1) - g->firstold1 = o; /* it is the first OLD1 object in the list */ - return o; -} - - -static void dothecall (lua_State *L, void *ud) { - UNUSED(ud); - luaD_callnoyield(L, L->top - 2, 0); -} - - -static void GCTM (lua_State *L) { - global_State *g = G(L); - const TValue *tm; - TValue v; - lua_assert(!g->gcemergency); - setgcovalue(L, &v, udata2finalize(g)); - tm = luaT_gettmbyobj(L, &v, TM_GC); - if (!notm(tm)) { /* is there a finalizer? */ - int status; - lu_byte oldah = L->allowhook; - int oldgcstp = g->gcstp; - g->gcstp |= GCSTPGC; /* avoid GC steps */ - L->allowhook = 0; /* stop debug hooks during GC metamethod */ - setobj2s(L, L->top++, tm); /* push finalizer... */ - setobj2s(L, L->top++, &v); /* ... and its argument */ - L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); - L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ - L->allowhook = oldah; /* restore hooks */ - g->gcstp = oldgcstp; /* restore state */ - if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ - luaE_warnerror(L, "__gc"); - L->top--; /* pops error object */ - } - } -} - - -/* -** Call a few finalizers -*/ -static int runafewfinalizers (lua_State *L, int n) { - global_State *g = G(L); - int i; - for (i = 0; i < n && g->tobefnz; i++) - GCTM(L); /* call one finalizer */ - return i; -} - - -/* -** call all pending finalizers -*/ -static void callallpendingfinalizers (lua_State *L) { - global_State *g = G(L); - while (g->tobefnz) - GCTM(L); -} - - -/* -** find last 'next' field in list 'p' list (to add elements in its end) -*/ -static GCObject **findlast (GCObject **p) { - while (*p != NULL) - p = &(*p)->next; - return p; -} - - -/* -** Move all unreachable objects (or 'all' objects) that need -** finalization from list 'finobj' to list 'tobefnz' (to be finalized). -** (Note that objects after 'finobjold1' cannot be white, so they -** don't need to be traversed. In incremental mode, 'finobjold1' is NULL, -** so the whole list is traversed.) -*/ -static void separatetobefnz (global_State *g, int all) { - GCObject *curr; - GCObject **p = &g->finobj; - GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */ - lua_assert(tofinalize(curr)); - if (!(iswhite(curr) || all)) /* not being collected? */ - p = &curr->next; /* don't bother with it */ - else { - if (curr == g->finobjsur) /* removing 'finobjsur'? */ - g->finobjsur = curr->next; /* correct it */ - *p = curr->next; /* remove 'curr' from 'finobj' list */ - curr->next = *lastnext; /* link at the end of 'tobefnz' list */ - *lastnext = curr; - lastnext = &curr->next; - } - } -} - - -/* -** If pointer 'p' points to 'o', move it to the next element. -*/ -static void checkpointer (GCObject **p, GCObject *o) { - if (o == *p) - *p = o->next; -} - - -/* -** Correct pointers to objects inside 'allgc' list when -** object 'o' is being removed from the list. -*/ -static void correctpointers (global_State *g, GCObject *o) { - checkpointer(&g->survival, o); - checkpointer(&g->old1, o); - checkpointer(&g->reallyold, o); - checkpointer(&g->firstold1, o); -} - - -/* -** if object 'o' has a finalizer, remove it from 'allgc' list (must -** search the list to find it) and link it in 'finobj' list. -*/ -void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { - global_State *g = G(L); - if (tofinalize(o) || /* obj. is already marked... */ - gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */ - (g->gcstp & GCSTPCLS)) /* or closing state? */ - return; /* nothing to be done */ - else { /* move 'o' to 'finobj' list */ - GCObject **p; - if (issweepphase(g)) { - makewhite(g, o); /* "sweep" object 'o' */ - if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ - g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ - } - else - correctpointers(g, o); - /* search for pointer pointing to 'o' */ - for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } - *p = o->next; /* remove 'o' from 'allgc' list */ - o->next = g->finobj; /* link it in 'finobj' list */ - g->finobj = o; - l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */ - } -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Generational Collector -** ======================================================= -*/ - -static void setpause (global_State *g); - - -/* -** Sweep a list of objects to enter generational mode. Deletes dead -** objects and turns the non dead to old. All non-dead threads---which -** are now old---must be in a gray list. Everything else is not in a -** gray list. Open upvalues are also kept gray. -*/ -static void sweep2old (lua_State *L, GCObject **p) { - GCObject *curr; - global_State *g = G(L); - while ((curr = *p) != NULL) { - if (iswhite(curr)) { /* is 'curr' dead? */ - lua_assert(isdead(g, curr)); - *p = curr->next; /* remove 'curr' from list */ - freeobj(L, curr); /* erase 'curr' */ - } - else { /* all surviving objects become old */ - setage(curr, G_OLD); - if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ - lua_State *th = gco2th(curr); - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - } - else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) - set2gray(curr); /* open upvalues are always gray */ - else /* everything else is black */ - nw2black(curr); - p = &curr->next; /* go to next element */ - } - } -} - - -/* -** Sweep for generational mode. Delete dead objects. (Because the -** collection is not incremental, there are no "new white" objects -** during the sweep. So, any white object must be dead.) For -** non-dead objects, advance their ages and clear the color of -** new objects. (Old objects keep their colors.) -** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced -** here, because these old-generation objects are usually not swept -** here. They will all be advanced in 'correctgraylist'. That function -** will also remove objects turned white here from any gray list. -*/ -static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, - GCObject *limit, GCObject **pfirstold1) { - static const lu_byte nextage[] = { - G_SURVIVAL, /* from G_NEW */ - G_OLD1, /* from G_SURVIVAL */ - G_OLD1, /* from G_OLD0 */ - G_OLD, /* from G_OLD1 */ - G_OLD, /* from G_OLD (do not change) */ - G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ - G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ - }; - int white = luaC_white(g); - GCObject *curr; - while ((curr = *p) != limit) { - if (iswhite(curr)) { /* is 'curr' dead? */ - lua_assert(!isold(curr) && isdead(g, curr)); - *p = curr->next; /* remove 'curr' from list */ - freeobj(L, curr); /* erase 'curr' */ - } - else { /* correct mark and age */ - if (getage(curr) == G_NEW) { /* new objects go back to white */ - int marked = curr->marked & ~maskgcbits; /* erase GC bits */ - curr->marked = cast_byte(marked | G_SURVIVAL | white); - } - else { /* all other objects will be old, and so keep their color */ - setage(curr, nextage[getage(curr)]); - if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) - *pfirstold1 = curr; /* first OLD1 object in the list */ - } - p = &curr->next; /* go to next element */ - } - } - return p; -} - - -/* -** Traverse a list making all its elements white and clearing their -** age. In incremental mode, all objects are 'new' all the time, -** except for fixed strings (which are always old). -*/ -static void whitelist (global_State *g, GCObject *p) { - int white = luaC_white(g); - for (; p != NULL; p = p->next) - p->marked = cast_byte((p->marked & ~maskgcbits) | white); -} - - -/* -** Correct a list of gray objects. Return pointer to where rest of the -** list should be linked. -** Because this correction is done after sweeping, young objects might -** be turned white and still be in the list. They are only removed. -** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; -** Non-white threads also remain on the list; 'TOUCHED2' objects become -** regular old; they and anything else are removed from the list. -*/ -static GCObject **correctgraylist (GCObject **p) { - GCObject *curr; - while ((curr = *p) != NULL) { - GCObject **next = getgclist(curr); - if (iswhite(curr)) - goto remove; /* remove all white objects */ - else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ - lua_assert(isgray(curr)); - nw2black(curr); /* make it black, for next barrier */ - changeage(curr, G_TOUCHED1, G_TOUCHED2); - goto remain; /* keep it in the list and go to next element */ - } - else if (curr->tt == LUA_VTHREAD) { - lua_assert(isgray(curr)); - goto remain; /* keep non-white threads on the list */ - } - else { /* everything else is removed */ - lua_assert(isold(curr)); /* young objects should be white here */ - if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ - changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ - nw2black(curr); /* make object black (to be removed) */ - goto remove; - } - remove: *p = *next; continue; - remain: p = next; continue; - } - return p; -} - - -/* -** Correct all gray lists, coalescing them into 'grayagain'. -*/ -static void correctgraylists (global_State *g) { - GCObject **list = correctgraylist(&g->grayagain); - *list = g->weak; g->weak = NULL; - list = correctgraylist(list); - *list = g->allweak; g->allweak = NULL; - list = correctgraylist(list); - *list = g->ephemeron; g->ephemeron = NULL; - correctgraylist(list); -} - - -/* -** Mark black 'OLD1' objects when starting a new young collection. -** Gray objects are already in some gray list, and so will be visited -** in the atomic step. -*/ -static void markold (global_State *g, GCObject *from, GCObject *to) { - GCObject *p; - for (p = from; p != to; p = p->next) { - if (getage(p) == G_OLD1) { - lua_assert(!iswhite(p)); - changeage(p, G_OLD1, G_OLD); /* now they are old */ - if (isblack(p)) - reallymarkobject(g, p); - } - } -} - - -/* -** Finish a young-generation collection. -*/ -static void finishgencycle (lua_State *L, global_State *g) { - correctgraylists(g); - checkSizes(L, g); - g->gcstate = GCSpropagate; /* skip restart */ - if (!g->gcemergency) - callallpendingfinalizers(L); -} - - -/* -** Does a young collection. First, mark 'OLD1' objects. Then does the -** atomic step. Then, sweep all lists and advance pointers. Finally, -** finish the collection. -*/ -static void youngcollection (lua_State *L, global_State *g) { - GCObject **psurvival; /* to point to first non-dead survival object */ - GCObject *dummy; /* dummy out parameter to 'sweepgen' */ - lua_assert(g->gcstate == GCSpropagate); - if (g->firstold1) { /* are there regular OLD1 objects? */ - markold(g, g->firstold1, g->reallyold); /* mark them */ - g->firstold1 = NULL; /* no more OLD1 objects (for now) */ - } - markold(g, g->finobj, g->finobjrold); - markold(g, g->tobefnz, NULL); - atomic(L); - - /* sweep nursery and get a pointer to its last live element */ - g->gcstate = GCSswpallgc; - psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1); - /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->old1, &g->firstold1); - g->reallyold = g->old1; - g->old1 = *psurvival; /* 'survival' survivals are old now */ - g->survival = g->allgc; /* all news are survivals */ - - /* repeat for 'finobj' lists */ - dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ - psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy); - /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->finobjold1, &dummy); - g->finobjrold = g->finobjold1; - g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ - g->finobjsur = g->finobj; /* all news are survivals */ - - sweepgen(L, g, &g->tobefnz, NULL, &dummy); - finishgencycle(L, g); -} - - -/* -** Clears all gray lists, sweeps objects, and prepare sublists to enter -** generational mode. The sweeps remove dead objects and turn all -** surviving objects to old. Threads go back to 'grayagain'; everything -** else is turned black (not in any gray list). -*/ -static void atomic2gen (lua_State *L, global_State *g) { - cleargraylists(g); - /* sweep all elements making them old */ - g->gcstate = GCSswpallgc; - sweep2old(L, &g->allgc); - /* everything alive now is old */ - g->reallyold = g->old1 = g->survival = g->allgc; - g->firstold1 = NULL; /* there are no OLD1 objects anywhere */ - - /* repeat for 'finobj' lists */ - sweep2old(L, &g->finobj); - g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj; - - sweep2old(L, &g->tobefnz); - - g->gckind = KGC_GEN; - g->lastatomic = 0; - g->GCestimate = gettotalbytes(g); /* base for memory control */ - finishgencycle(L, g); -} - - -/* -** Enter generational mode. Must go until the end of an atomic cycle -** to ensure that all objects are correctly marked and weak tables -** are cleared. Then, turn all objects into old and finishes the -** collection. -*/ -static lu_mem entergen (lua_State *L, global_State *g) { - lu_mem numobjs; - luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - numobjs = atomic(L); /* propagates all and then do the atomic stuff */ - atomic2gen(L, g); - return numobjs; -} - - -/* -** Enter incremental mode. Turn all objects white, make all -** intermediate lists point to NULL (to avoid invalid pointers), -** and go to the pause state. -*/ -static void enterinc (global_State *g) { - whitelist(g, g->allgc); - g->reallyold = g->old1 = g->survival = NULL; - whitelist(g, g->finobj); - whitelist(g, g->tobefnz); - g->finobjrold = g->finobjold1 = g->finobjsur = NULL; - g->gcstate = GCSpause; - g->gckind = KGC_INC; - g->lastatomic = 0; -} - - -/* -** Change collector mode to 'newmode'. -*/ -void luaC_changemode (lua_State *L, int newmode) { - global_State *g = G(L); - if (newmode != g->gckind) { - if (newmode == KGC_GEN) /* entering generational mode? */ - entergen(L, g); - else - enterinc(g); /* entering incremental mode */ - } - g->lastatomic = 0; -} - - -/* -** Does a full collection in generational mode. -*/ -static lu_mem fullgen (lua_State *L, global_State *g) { - enterinc(g); - return entergen(L, g); -} - - -/* -** Set debt for the next minor collection, which will happen when -** memory grows 'genminormul'%. -*/ -static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); -} - - -/* -** Does a major collection after last collection was a "bad collection". -** -** When the program is building a big structure, it allocates lots of -** memory but generates very little garbage. In those scenarios, -** the generational mode just wastes time doing small collections, and -** major collections are frequently what we call a "bad collection", a -** collection that frees too few objects. To avoid the cost of switching -** between generational mode and the incremental mode needed for full -** (major) collections, the collector tries to stay in incremental mode -** after a bad collection, and to switch back to generational mode only -** after a "good" collection (one that traverses less than 9/8 objects -** of the previous one). -** The collector must choose whether to stay in incremental mode or to -** switch back to generational mode before sweeping. At this point, it -** does not know the real memory in use, so it cannot use memory to -** decide whether to return to generational mode. Instead, it uses the -** number of objects traversed (returned by 'atomic') as a proxy. The -** field 'g->lastatomic' keeps this count from the last collection. -** ('g->lastatomic != 0' also means that the last collection was bad.) -*/ -static void stepgenfull (lua_State *L, global_State *g) { - lu_mem newatomic; /* count of traversed objects */ - lu_mem lastatomic = g->lastatomic; /* count from last collection */ - if (g->gckind == KGC_GEN) /* still in generational mode? */ - enterinc(g); /* enter incremental mode */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - newatomic = atomic(L); /* mark everybody */ - if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */ - atomic2gen(L, g); /* return to generational mode */ - setminordebt(g); - } - else { /* another bad collection; stay in incremental mode */ - g->GCestimate = gettotalbytes(g); /* first estimate */; - entersweep(L); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ - setpause(g); - g->lastatomic = newatomic; - } -} - - -/* -** Does a generational "step". -** Usually, this means doing a minor collection and setting the debt to -** make another collection when memory grows 'genminormul'% larger. -** -** However, there are exceptions. If memory grows 'genmajormul'% -** larger than it was at the end of the last major collection (kept -** in 'g->GCestimate'), the function does a major collection. At the -** end, it checks whether the major collection was able to free a -** decent amount of memory (at least half the growth in memory since -** previous major collection). If so, the collector keeps its state, -** and the next collection will probably be minor again. Otherwise, -** we have what we call a "bad collection". In that case, set the field -** 'g->lastatomic' to signal that fact, so that the next collection will -** go to 'stepgenfull'. -** -** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; -** in that case, do a minor collection. -*/ -static void genstep (lua_State *L, global_State *g) { - if (g->lastatomic != 0) /* last collection was a bad one? */ - stepgenfull(L, g); /* do a full step */ - else { - lu_mem majorbase = g->GCestimate; /* memory after last major collection */ - lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); - if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) { - lu_mem numobjs = fullgen(L, g); /* do a major collection */ - if (gettotalbytes(g) < majorbase + (majorinc / 2)) { - /* collected at least half of memory growth since last major - collection; keep doing minor collections */ - setminordebt(g); - } - else { /* bad collection */ - g->lastatomic = numobjs; /* signal that last collection was bad */ - setpause(g); /* do a long wait for next (major) collection */ - } - } - else { /* regular case; do a minor collection */ - youngcollection(L, g); - setminordebt(g); - g->GCestimate = majorbase; /* preserve base value */ - } - } - lua_assert(isdecGCmodegen(g)); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** GC control -** ======================================================= -*/ - - -/* -** Set the "time" to wait before starting a new GC cycle; cycle will -** start when memory use hits the threshold of ('estimate' * pause / -** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, -** because Lua cannot even start with less than PAUSEADJ bytes). -*/ -static void setpause (global_State *g) { - l_mem threshold, debt; - int pause = getgcparam(g->gcpause); - l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ - lua_assert(estimate > 0); - threshold = (pause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * pause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = gettotalbytes(g) - threshold; - if (debt > 0) debt = 0; - luaE_setdebt(g, debt); -} - - -/* -** Enter first sweep phase. -** The call to 'sweeptolive' makes the pointer point to an object -** inside the list (instead of to the header), so that the real sweep do -** not need to skip objects created between "now" and the start of the -** real sweep. -*/ -static void entersweep (lua_State *L) { - global_State *g = G(L); - g->gcstate = GCSswpallgc; - lua_assert(g->sweepgc == NULL); - g->sweepgc = sweeptolive(L, &g->allgc); -} - - -/* -** Delete all objects in list 'p' until (but not including) object -** 'limit'. -*/ -static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { - while (p != limit) { - GCObject *next = p->next; - freeobj(L, p); - p = next; - } -} - - -/* -** Call all finalizers of the objects in the given Lua state, and -** then free all objects, except for the main thread. -*/ -void luaC_freeallobjects (lua_State *L) { - global_State *g = G(L); - g->gcstp = GCSTPCLS; /* no extra finalizers after here */ - luaC_changemode(L, KGC_INC); - separatetobefnz(g, 1); /* separate all objects with finalizers */ - lua_assert(g->finobj == NULL); - callallpendingfinalizers(L); - deletelist(L, g->allgc, obj2gco(g->mainthread)); - lua_assert(g->finobj == NULL); /* no new finalizers */ - deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ - lua_assert(g->strt.nuse == 0); -} - - -static lu_mem atomic (lua_State *L) { - global_State *g = G(L); - lu_mem work = 0; - GCObject *origweak, *origall; - GCObject *grayagain = g->grayagain; /* save original list */ - g->grayagain = NULL; - lua_assert(g->ephemeron == NULL && g->weak == NULL); - lua_assert(!iswhite(g->mainthread)); - g->gcstate = GCSatomic; - markobject(g, L); /* mark running thread */ - /* registry and global metatables may be changed by API */ - markvalue(g, &g->l_registry); - markmt(g); /* mark global metatables */ - work += propagateall(g); /* empties 'gray' list */ - /* remark occasional upvalues of (maybe) dead threads */ - work += remarkupvals(g); - work += propagateall(g); /* propagate changes */ - g->gray = grayagain; - work += propagateall(g); /* traverse 'grayagain' list */ - convergeephemerons(g); - /* at this point, all strongly accessible objects are marked. */ - /* Clear values from weak tables, before checking finalizers */ - clearbyvalues(g, g->weak, NULL); - clearbyvalues(g, g->allweak, NULL); - origweak = g->weak; origall = g->allweak; - separatetobefnz(g, 0); /* separate objects to be finalized */ - work += markbeingfnz(g); /* mark objects that will be finalized */ - work += propagateall(g); /* remark, to propagate 'resurrection' */ - convergeephemerons(g); - /* at this point, all resurrected objects are marked. */ - /* remove dead objects from weak tables */ - clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ - clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */ - /* clear values from resurrected weak tables */ - clearbyvalues(g, g->weak, origweak); - clearbyvalues(g, g->allweak, origall); - luaS_clearcache(g); - g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ - lua_assert(g->gray == NULL); - return work; /* estimate of slots marked by 'atomic' */ -} - - -static int sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { - if (g->sweepgc) { - l_mem olddebt = g->GCdebt; - int count; - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count); - g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - return count; - } - else { /* enter next state */ - g->gcstate = nextstate; - g->sweepgc = nextlist; - return 0; /* no work done */ - } -} - - -static lu_mem singlestep (lua_State *L) { - global_State *g = G(L); - lu_mem work; - lua_assert(!g->gcstopem); /* collector is not reentrant */ - g->gcstopem = 1; /* no emergency collections while collecting */ - switch (g->gcstate) { - case GCSpause: { - restartcollection(g); - g->gcstate = GCSpropagate; - work = 1; - break; - } - case GCSpropagate: { - if (g->gray == NULL) { /* no more gray objects? */ - g->gcstate = GCSenteratomic; /* finish propagate phase */ - work = 0; - } - else - work = propagatemark(g); /* traverse one gray object */ - break; - } - case GCSenteratomic: { - work = atomic(L); /* work is what was traversed by 'atomic' */ - entersweep(L); - g->GCestimate = gettotalbytes(g); /* first estimate */; - break; - } - case GCSswpallgc: { /* sweep "regular" objects */ - work = sweepstep(L, g, GCSswpfinobj, &g->finobj); - break; - } - case GCSswpfinobj: { /* sweep objects with finalizers */ - work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz); - break; - } - case GCSswptobefnz: { /* sweep objects to be finalized */ - work = sweepstep(L, g, GCSswpend, NULL); - break; - } - case GCSswpend: { /* finish sweeps */ - checkSizes(L, g); - g->gcstate = GCScallfin; - work = 0; - break; - } - case GCScallfin: { /* call remaining finalizers */ - if (g->tobefnz && !g->gcemergency) { - g->gcstopem = 0; /* ok collections during finalizers */ - work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST; - } - else { /* emergency mode or no more finalizers */ - g->gcstate = GCSpause; /* finish collection */ - work = 0; - } - break; - } - default: lua_assert(0); return 0; - } - g->gcstopem = 0; - return work; -} - - -/* -** advances the garbage collector until it reaches a state allowed -** by 'statemask' -*/ -void luaC_runtilstate (lua_State *L, int statesmask) { - global_State *g = G(L); - while (!testbit(statesmask, g->gcstate)) - singlestep(L); -} - - - -/* -** Performs a basic incremental step. The debt and step size are -** converted from bytes to "units of work"; then the function loops -** running single steps until adding that many units of work or -** finishing a cycle (pause state). Finally, it sets the debt that -** controls when next step will be performed. -*/ -static void incstep (lua_State *L, global_State *g) { - int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ - l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; - l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) - ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul - : MAX_LMEM; /* overflow; keep maximum value */ - do { /* repeat until pause or enough "credit" (negative debt) */ - lu_mem work = singlestep(L); /* perform one single step */ - debt -= work; - } while (debt > -stepsize && g->gcstate != GCSpause); - if (g->gcstate == GCSpause) - setpause(g); /* pause until next cycle */ - else { - debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */ - luaE_setdebt(g, debt); - } -} - -/* -** performs a basic GC step if collector is running -*/ -void luaC_step (lua_State *L) { - global_State *g = G(L); - lua_assert(!g->gcemergency); - if (gcrunning(g)) { /* running? */ - if(isdecGCmodegen(g)) - genstep(L, g); - else - incstep(L, g); - } -} - - -/* -** Perform a full collection in incremental mode. -** Before running the collection, check 'keepinvariant'; if it is true, -** there may be some objects marked as black, so the collector has -** to sweep all objects to turn them back to white (as white has not -** changed, nothing will be collected). -*/ -static void fullinc (lua_State *L, global_State *g) { - if (keepinvariant(g)) /* black objects? */ - entersweep(L); /* sweep everything to turn them back to white */ - /* finish any pending sweep phase to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpause)); - luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ - /* estimate must be correct after a full GC cycle */ - lua_assert(g->GCestimate == gettotalbytes(g)); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ - setpause(g); -} - - -/* -** Performs a full GC cycle; if 'isemergency', set a flag to avoid -** some operations which could change the interpreter state in some -** unexpected ways (running finalizers and shrinking some structures). -*/ -void luaC_fullgc (lua_State *L, int isemergency) { - global_State *g = G(L); - lua_assert(!g->gcemergency); - g->gcemergency = isemergency; /* set flag */ - if (g->gckind == KGC_INC) - fullinc(L, g); - else - fullgen(L, g); - g->gcemergency = 0; -} - -/* }====================================================== */ - - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.h deleted file mode 100644 index 4a12563..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lgc.h +++ /dev/null @@ -1,199 +0,0 @@ -/* -** $Id: lgc.h $ -** Garbage Collector -** See Copyright Notice in lua.h -*/ - -#ifndef lgc_h -#define lgc_h - - -#include "lobject.h" -#include "lstate.h" - -/* -** Collectable objects may have one of three colors: white, which means -** the object is not marked; gray, which means the object is marked, but -** its references may be not marked; and black, which means that the -** object and all its references are marked. The main invariant of the -** garbage collector, while marking objects, is that a black object can -** never point to a white one. Moreover, any gray object must be in a -** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it -** can be visited again before finishing the collection cycle. (Open -** upvalues are an exception to this rule.) These lists have no meaning -** when the invariant is not being enforced (e.g., sweep phase). -*/ - - -/* -** Possible states of the Garbage Collector -*/ -#define GCSpropagate 0 -#define GCSenteratomic 1 -#define GCSatomic 2 -#define GCSswpallgc 3 -#define GCSswpfinobj 4 -#define GCSswptobefnz 5 -#define GCSswpend 6 -#define GCScallfin 7 -#define GCSpause 8 - - -#define issweepphase(g) \ - (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) - - -/* -** macro to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a collection, the sweep -** phase may break the invariant, as objects turned white may point to -** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. -*/ - -#define keepinvariant(g) ((g)->gcstate <= GCSatomic) - - -/* -** some useful bit tricks -*/ -#define resetbits(x,m) ((x) &= cast_byte(~(m))) -#define setbits(x,m) ((x) |= (m)) -#define testbits(x,m) ((x) & (m)) -#define bitmask(b) (1<<(b)) -#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) -#define l_setbit(x,b) setbits(x, bitmask(b)) -#define resetbit(x,b) resetbits(x, bitmask(b)) -#define testbit(x,b) testbits(x, bitmask(b)) - - -/* -** Layout for bit use in 'marked' field. First three bits are -** used for object "age" in generational mode. Last bit is used -** by tests. -*/ -#define WHITE0BIT 3 /* object is white (type 0) */ -#define WHITE1BIT 4 /* object is white (type 1) */ -#define BLACKBIT 5 /* object is black */ -#define FINALIZEDBIT 6 /* object has been marked for finalization */ - -#define TESTBIT 7 - - - -#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) - - -#define iswhite(x) testbits((x)->marked, WHITEBITS) -#define isblack(x) testbit((x)->marked, BLACKBIT) -#define isgray(x) /* neither white nor black */ \ - (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) - -#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) - -#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) -#define isdeadm(ow,m) ((m) & (ow)) -#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) - -#define changewhite(x) ((x)->marked ^= WHITEBITS) -#define nw2black(x) \ - check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) - -#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) - - -/* object age in generational mode */ -#define G_NEW 0 /* created in current cycle */ -#define G_SURVIVAL 1 /* created in previous cycle */ -#define G_OLD0 2 /* marked old by frw. barrier in this cycle */ -#define G_OLD1 3 /* first full cycle as old */ -#define G_OLD 4 /* really old object (not to be visited) */ -#define G_TOUCHED1 5 /* old object touched this cycle */ -#define G_TOUCHED2 6 /* old object touched in previous cycle */ - -#define AGEBITS 7 /* all age bits (111) */ - -#define getage(o) ((o)->marked & AGEBITS) -#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) -#define isold(o) (getage(o) > G_SURVIVAL) - -#define changeage(o,f,t) \ - check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) - - -/* Default Values for GC parameters */ -#define LUAI_GENMAJORMUL 100 -#define LUAI_GENMINORMUL 20 - -/* wait memory to double before starting new cycle */ -#define LUAI_GCPAUSE 200 - -/* -** some gc parameters are stored divided by 4 to allow a maximum value -** up to 1023 in a 'lu_byte'. -*/ -#define getgcparam(p) ((p) * 4) -#define setgcparam(p,v) ((p) = (v) / 4) - -#define LUAI_GCMUL 100 - -/* how much to allocate before next GC step (log2) */ -#define LUAI_GCSTEPSIZE 13 /* 8 KB */ - - -/* -** Check whether the declared GC mode is generational. While in -** generational mode, the collector can go temporarily to incremental -** mode to improve performance. This is signaled by 'g->lastatomic != 0'. -*/ -#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) - - -/* -** Control when GC is running: -*/ -#define GCSTPUSR 1 /* bit true when GC stopped by user */ -#define GCSTPGC 2 /* bit true when GC stopped by itself */ -#define GCSTPCLS 4 /* bit true when closing Lua state */ -#define gcrunning(g) ((g)->gcstp == 0) - - -/* -** Does one step of collection when debt becomes positive. 'pre'/'pos' -** allows some adjustments to be done only when needed. macro -** 'condchangemem' is used only for heavy tests (forcing a full -** GC cycle on every opportunity) -*/ -#define luaC_condGC(L,pre,pos) \ - { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ - condchangemem(L,pre,pos); } - -/* more often than not, 'pre'/'pos' are empty */ -#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) - - -#define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) - -#define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrierback_(L,p) : cast_void(0)) - -#define luaC_objbarrier(L,p,o) ( \ - (isblack(p) && iswhite(o)) ? \ - luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) - -LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_freeallobjects (lua_State *L); -LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); -LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); -LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/linit.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/linit.c deleted file mode 100644 index 69808f8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/linit.c +++ /dev/null @@ -1,65 +0,0 @@ -/* -** $Id: linit.c $ -** Initialization of libraries for lua.c and other clients -** See Copyright Notice in lua.h -*/ - - -#define linit_c -#define LUA_LIB - -/* -** If you embed Lua in your program and need to open the standard -** libraries, call luaL_openlibs in your program. If you need a -** different set of libraries, copy this file to your project and edit -** it to suit your needs. -** -** You can also *preload* libraries, so that a later 'require' can -** open the library, which is already linked to the application. -** For that, do the following code: -** -** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); -** lua_pushcfunction(L, luaopen_modname); -** lua_setfield(L, -2, modname); -** lua_pop(L, 1); // remove PRELOAD table -*/ - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "lualib.h" -#include "lauxlib.h" - - -/* -** these libs are loaded by lua.c and are readily available to any Lua -** program -*/ -static const luaL_Reg loadedlibs[] = { - {LUA_GNAME, luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_COLIBNAME, luaopen_coroutine}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_UTF8LIBNAME, luaopen_utf8}, - {LUA_DBLIBNAME, luaopen_debug}, - {NULL, NULL} -}; - - -LUALIB_API void luaL_openlibs (lua_State *L) { - const luaL_Reg *lib; - /* "require" functions from 'loadedlibs' and set results to global table */ - for (lib = loadedlibs; lib->func; lib++) { - luaL_requiref(L, lib->name, lib->func, 1); - lua_pop(L, 1); /* remove lib */ - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/liolib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/liolib.c deleted file mode 100644 index b08397d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/liolib.c +++ /dev/null @@ -1,828 +0,0 @@ -/* -** $Id: liolib.c $ -** Standard I/O (and system) library -** See Copyright Notice in lua.h -*/ - -#define liolib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - - - -/* -** Change this macro to accept other modes for 'fopen' besides -** the standard ones. -*/ -#if !defined(l_checkmode) - -/* accepted extensions to 'mode' in 'fopen' */ -#if !defined(L_MODEEXT) -#define L_MODEEXT "b" -#endif - -/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ -static int l_checkmode (const char *mode) { - return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && - (*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */ - (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ -} - -#endif - -/* -** {====================================================== -** l_popen spawns a new process connected to the current -** one through the file streams. -** ======================================================= -*/ - -#if !defined(l_popen) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#define l_popen(L,c,m) (fflush(NULL), popen(c,m)) -#define l_pclose(L,file) (pclose(file)) - -#elif defined(LUA_USE_WINDOWS) /* }{ */ - -#define l_popen(L,c,m) (_popen(c,m)) -#define l_pclose(L,file) (_pclose(file)) - -#if !defined(l_checkmodep) -/* Windows accepts "[rw][bt]?" as valid modes */ -#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \ - (m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0'))) -#endif - -#else /* }{ */ - -/* ISO C definitions */ -#define l_popen(L,c,m) \ - ((void)c, (void)m, \ - luaL_error(L, "'popen' not supported"), \ - (FILE*)0) -#define l_pclose(L,file) ((void)L, (void)file, -1) - -#endif /* } */ - -#endif /* } */ - - -#if !defined(l_checkmodep) -/* By default, Lua accepts only "r" or "w" as valid modes */ -#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') -#endif - -/* }====================================================== */ - - -#if !defined(l_getc) /* { */ - -#if defined(LUA_USE_POSIX) -#define l_getc(f) getc_unlocked(f) -#define l_lockfile(f) flockfile(f) -#define l_unlockfile(f) funlockfile(f) -#else -#define l_getc(f) getc(f) -#define l_lockfile(f) ((void)0) -#define l_unlockfile(f) ((void)0) -#endif - -#endif /* } */ - - -/* -** {====================================================== -** l_fseek: configuration for longer offsets -** ======================================================= -*/ - -#if !defined(l_fseek) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include - -#define l_fseek(f,o,w) fseeko(f,o,w) -#define l_ftell(f) ftello(f) -#define l_seeknum off_t - -#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \ - && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */ - -/* Windows (but not DDK) and Visual C++ 2005 or higher */ -#define l_fseek(f,o,w) _fseeki64(f,o,w) -#define l_ftell(f) _ftelli64(f) -#define l_seeknum __int64 - -#else /* }{ */ - -/* ISO C definitions */ -#define l_fseek(f,o,w) fseek(f,o,w) -#define l_ftell(f) ftell(f) -#define l_seeknum long - -#endif /* } */ - -#endif /* } */ - -/* }====================================================== */ - - - -#define IO_PREFIX "_IO_" -#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) -#define IO_INPUT (IO_PREFIX "input") -#define IO_OUTPUT (IO_PREFIX "output") - - -typedef luaL_Stream LStream; - - -#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) - -#define isclosed(p) ((p)->closef == NULL) - - -static int io_type (lua_State *L) { - LStream *p; - luaL_checkany(L, 1); - p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); - if (p == NULL) - luaL_pushfail(L); /* not a file */ - else if (isclosed(p)) - lua_pushliteral(L, "closed file"); - else - lua_pushliteral(L, "file"); - return 1; -} - - -static int f_tostring (lua_State *L) { - LStream *p = tolstream(L); - if (isclosed(p)) - lua_pushliteral(L, "file (closed)"); - else - lua_pushfstring(L, "file (%p)", p->f); - return 1; -} - - -static FILE *tofile (lua_State *L) { - LStream *p = tolstream(L); - if (l_unlikely(isclosed(p))) - luaL_error(L, "attempt to use a closed file"); - lua_assert(p->f); - return p->f; -} - - -/* -** When creating file handles, always creates a 'closed' file handle -** before opening the actual file; so, if there is a memory error, the -** handle is in a consistent state. -*/ -static LStream *newprefile (lua_State *L) { - LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); - p->closef = NULL; /* mark file handle as 'closed' */ - luaL_setmetatable(L, LUA_FILEHANDLE); - return p; -} - - -/* -** Calls the 'close' function from a file handle. The 'volatile' avoids -** a bug in some versions of the Clang compiler (e.g., clang 3.0 for -** 32 bits). -*/ -static int aux_close (lua_State *L) { - LStream *p = tolstream(L); - volatile lua_CFunction cf = p->closef; - p->closef = NULL; /* mark stream as closed */ - return (*cf)(L); /* close it */ -} - - -static int f_close (lua_State *L) { - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); -} - - -static int io_close (lua_State *L) { - if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ - return f_close(L); -} - - -static int f_gc (lua_State *L) { - LStream *p = tolstream(L); - if (!isclosed(p) && p->f != NULL) - aux_close(L); /* ignore closed and incompletely open files */ - return 0; -} - - -/* -** function to close regular files -*/ -static int io_fclose (lua_State *L) { - LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); -} - - -static LStream *newfile (lua_State *L) { - LStream *p = newprefile(L); - p->f = NULL; - p->closef = &io_fclose; - return p; -} - - -static void opencheck (lua_State *L, const char *fname, const char *mode) { - LStream *p = newfile(L); - p->f = fopen(fname, mode); - if (l_unlikely(p->f == NULL)) - luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); -} - - -static int io_open (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newfile(L); - const char *md = mode; /* to traverse/check mode */ - luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); - p->f = fopen(filename, mode); - return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; -} - - -/* -** function to close 'popen' files -*/ -static int io_pclose (lua_State *L) { - LStream *p = tolstream(L); - errno = 0; - return luaL_execresult(L, l_pclose(L, p->f)); -} - - -static int io_popen (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newprefile(L); - luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); - p->f = l_popen(L, filename, mode); - p->closef = &io_pclose; - return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; -} - - -static int io_tmpfile (lua_State *L) { - LStream *p = newfile(L); - p->f = tmpfile(); - return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; -} - - -static FILE *getiofile (lua_State *L, const char *findex) { - LStream *p; - lua_getfield(L, LUA_REGISTRYINDEX, findex); - p = (LStream *)lua_touserdata(L, -1); - if (l_unlikely(isclosed(p))) - luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); - return p->f; -} - - -static int g_iofile (lua_State *L, const char *f, const char *mode) { - if (!lua_isnoneornil(L, 1)) { - const char *filename = lua_tostring(L, 1); - if (filename) - opencheck(L, filename, mode); - else { - tofile(L); /* check that it's a valid file handle */ - lua_pushvalue(L, 1); - } - lua_setfield(L, LUA_REGISTRYINDEX, f); - } - /* return current value */ - lua_getfield(L, LUA_REGISTRYINDEX, f); - return 1; -} - - -static int io_input (lua_State *L) { - return g_iofile(L, IO_INPUT, "r"); -} - - -static int io_output (lua_State *L) { - return g_iofile(L, IO_OUTPUT, "w"); -} - - -static int io_readline (lua_State *L); - - -/* -** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit -** in the limit for upvalues of a closure) -*/ -#define MAXARGLINE 250 - -/* -** Auxiliary function to create the iteration function for 'lines'. -** The iteration function is a closure over 'io_readline', with -** the following upvalues: -** 1) The file being read (first value in the stack) -** 2) the number of arguments to read -** 3) a boolean, true iff file has to be closed when finished ('toclose') -** *) a variable number of format arguments (rest of the stack) -*/ -static void aux_lines (lua_State *L, int toclose) { - int n = lua_gettop(L) - 1; /* number of arguments to read */ - luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); - lua_pushvalue(L, 1); /* file */ - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_rotate(L, 2, 3); /* move the three values to their positions */ - lua_pushcclosure(L, io_readline, 3 + n); -} - - -static int f_lines (lua_State *L) { - tofile(L); /* check that it's a valid file handle */ - aux_lines(L, 0); - return 1; -} - - -/* -** Return an iteration function for 'io.lines'. If file has to be -** closed, also returns the file itself as a second result (to be -** closed as the state at the exit of a generic for). -*/ -static int io_lines (lua_State *L) { - int toclose; - if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ - if (lua_isnil(L, 1)) { /* no file name? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ - lua_replace(L, 1); /* put it at index 1 */ - tofile(L); /* check that it's a valid file handle */ - toclose = 0; /* do not close it after iteration */ - } - else { /* open a new file */ - const char *filename = luaL_checkstring(L, 1); - opencheck(L, filename, "r"); - lua_replace(L, 1); /* put file at index 1 */ - toclose = 1; /* close it after iteration */ - } - aux_lines(L, toclose); /* push iteration function */ - if (toclose) { - lua_pushnil(L); /* state */ - lua_pushnil(L); /* control */ - lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ - return 4; - } - else - return 1; -} - - -/* -** {====================================================== -** READ -** ======================================================= -*/ - - -/* maximum length of a numeral */ -#if !defined (L_MAXLENNUM) -#define L_MAXLENNUM 200 -#endif - - -/* auxiliary structure used by 'read_number' */ -typedef struct { - FILE *f; /* file being read */ - int c; /* current character (look ahead) */ - int n; /* number of elements in buffer 'buff' */ - char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ -} RN; - - -/* -** Add current char to buffer (if not out of space) and read next one -*/ -static int nextc (RN *rn) { - if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ - rn->buff[0] = '\0'; /* invalidate result */ - return 0; /* fail */ - } - else { - rn->buff[rn->n++] = rn->c; /* save current char */ - rn->c = l_getc(rn->f); /* read next one */ - return 1; - } -} - - -/* -** Accept current char if it is in 'set' (of size 2) -*/ -static int test2 (RN *rn, const char *set) { - if (rn->c == set[0] || rn->c == set[1]) - return nextc(rn); - else return 0; -} - - -/* -** Read a sequence of (hex)digits -*/ -static int readdigits (RN *rn, int hex) { - int count = 0; - while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn)) - count++; - return count; -} - - -/* -** Read a number: first reads a valid prefix of a numeral into a buffer. -** Then it calls 'lua_stringtonumber' to check whether the format is -** correct and to convert it to a Lua number. -*/ -static int read_number (lua_State *L, FILE *f) { - RN rn; - int count = 0; - int hex = 0; - char decp[2]; - rn.f = f; rn.n = 0; - decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ - decp[1] = '.'; /* always accept a dot */ - l_lockfile(rn.f); - do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ - test2(&rn, "-+"); /* optional sign */ - if (test2(&rn, "00")) { - if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ - else count = 1; /* count initial '0' as a valid digit */ - } - count += readdigits(&rn, hex); /* integral part */ - if (test2(&rn, decp)) /* decimal point? */ - count += readdigits(&rn, hex); /* fractional part */ - if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ - test2(&rn, "-+"); /* exponent sign */ - readdigits(&rn, 0); /* exponent digits */ - } - ungetc(rn.c, rn.f); /* unread look-ahead char */ - l_unlockfile(rn.f); - rn.buff[rn.n] = '\0'; /* finish string */ - if (l_likely(lua_stringtonumber(L, rn.buff))) - return 1; /* ok, it is a valid number */ - else { /* invalid format */ - lua_pushnil(L); /* "result" to be removed */ - return 0; /* read fails */ - } -} - - -static int test_eof (lua_State *L, FILE *f) { - int c = getc(f); - ungetc(c, f); /* no-op when c == EOF */ - lua_pushliteral(L, ""); - return (c != EOF); -} - - -static int read_line (lua_State *L, FILE *f, int chop) { - luaL_Buffer b; - int c; - luaL_buffinit(L, &b); - do { /* may need to read several chunks to get whole line */ - char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ - int i = 0; - l_lockfile(f); /* no memory errors can happen inside the lock */ - while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') - buff[i++] = c; /* read up to end of line or buffer limit */ - l_unlockfile(f); - luaL_addsize(&b, i); - } while (c != EOF && c != '\n'); /* repeat until end of line */ - if (!chop && c == '\n') /* want a newline and have one? */ - luaL_addchar(&b, c); /* add ending newline to result */ - luaL_pushresult(&b); /* close buffer */ - /* return ok if read something (either a newline or something else) */ - return (c == '\n' || lua_rawlen(L, -1) > 0); -} - - -static void read_all (lua_State *L, FILE *f) { - size_t nr; - luaL_Buffer b; - luaL_buffinit(L, &b); - do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ - char *p = luaL_prepbuffer(&b); - nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); - luaL_addsize(&b, nr); - } while (nr == LUAL_BUFFERSIZE); - luaL_pushresult(&b); /* close buffer */ -} - - -static int read_chars (lua_State *L, FILE *f, size_t n) { - size_t nr; /* number of chars actually read */ - char *p; - luaL_Buffer b; - luaL_buffinit(L, &b); - p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ - nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ - luaL_addsize(&b, nr); - luaL_pushresult(&b); /* close buffer */ - return (nr > 0); /* true iff read something */ -} - - -static int g_read (lua_State *L, FILE *f, int first) { - int nargs = lua_gettop(L) - 1; - int n, success; - clearerr(f); - if (nargs == 0) { /* no arguments? */ - success = read_line(L, f, 1); - n = first + 1; /* to return 1 result */ - } - else { - /* ensure stack space for all results and for auxlib's buffer */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - success = 1; - for (n = first; nargs-- && success; n++) { - if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)luaL_checkinteger(L, n); - success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); - } - else { - const char *p = luaL_checkstring(L, n); - if (*p == '*') p++; /* skip optional '*' (for compatibility) */ - switch (*p) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f, 1); - break; - case 'L': /* line with end-of-line */ - success = read_line(L, f, 0); - break; - case 'a': /* file */ - read_all(L, f); /* read entire file */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, n, "invalid format"); - } - } - } - } - if (ferror(f)) - return luaL_fileresult(L, 0, NULL); - if (!success) { - lua_pop(L, 1); /* remove last result */ - luaL_pushfail(L); /* push nil instead */ - } - return n - first; -} - - -static int io_read (lua_State *L) { - return g_read(L, getiofile(L, IO_INPUT), 1); -} - - -static int f_read (lua_State *L) { - return g_read(L, tofile(L), 2); -} - - -/* -** Iteration function for 'lines'. -*/ -static int io_readline (lua_State *L) { - LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); - int i; - int n = (int)lua_tointeger(L, lua_upvalueindex(2)); - if (isclosed(p)) /* file is already closed? */ - return luaL_error(L, "file is already closed"); - lua_settop(L , 1); - luaL_checkstack(L, n, "too many arguments"); - for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ - lua_pushvalue(L, lua_upvalueindex(3 + i)); - n = g_read(L, p->f, 2); /* 'n' is number of results */ - lua_assert(n > 0); /* should return at least a nil */ - if (lua_toboolean(L, -n)) /* read at least one value? */ - return n; /* return them */ - else { /* first result is false: EOF or error */ - if (n > 1) { /* is there error information? */ - /* 2nd result is error message */ - return luaL_error(L, "%s", lua_tostring(L, -n + 1)); - } - if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); /* clear stack */ - lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ - aux_close(L); /* close it */ - } - return 0; - } -} - -/* }====================================================== */ - - -static int g_write (lua_State *L, FILE *f, int arg) { - int nargs = lua_gettop(L) - arg; - int status = 1; - for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - int len = lua_isinteger(L, arg) - ? fprintf(f, LUA_INTEGER_FMT, - (LUAI_UACINT)lua_tointeger(L, arg)) - : fprintf(f, LUA_NUMBER_FMT, - (LUAI_UACNUMBER)lua_tonumber(L, arg)); - status = status && (len > 0); - } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); - } - } - if (l_likely(status)) - return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); -} - - -static int io_write (lua_State *L) { - return g_write(L, getiofile(L, IO_OUTPUT), 1); -} - - -static int f_write (lua_State *L) { - FILE *f = tofile(L); - lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ - return g_write(L, f, 2); -} - - -static int f_seek (lua_State *L) { - static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; - static const char *const modenames[] = {"set", "cur", "end", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Integer p3 = luaL_optinteger(L, 3, 0); - l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Integer)offset == p3, 3, - "not an integer in proper range"); - op = l_fseek(f, offset, mode[op]); - if (l_unlikely(op)) - return luaL_fileresult(L, 0, NULL); /* error */ - else { - lua_pushinteger(L, (lua_Integer)l_ftell(f)); - return 1; - } -} - - -static int f_setvbuf (lua_State *L) { - static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; - static const char *const modenames[] = {"no", "full", "line", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, NULL, modenames); - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], (size_t)sz); - return luaL_fileresult(L, res == 0, NULL); -} - - - -static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); -} - - -static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); -} - - -/* -** functions for 'io' library -*/ -static const luaL_Reg iolib[] = { - {"close", io_close}, - {"flush", io_flush}, - {"input", io_input}, - {"lines", io_lines}, - {"open", io_open}, - {"output", io_output}, - {"popen", io_popen}, - {"read", io_read}, - {"tmpfile", io_tmpfile}, - {"type", io_type}, - {"write", io_write}, - {NULL, NULL} -}; - - -/* -** methods for file handles -*/ -static const luaL_Reg meth[] = { - {"read", f_read}, - {"write", f_write}, - {"lines", f_lines}, - {"flush", f_flush}, - {"seek", f_seek}, - {"close", f_close}, - {"setvbuf", f_setvbuf}, - {NULL, NULL} -}; - - -/* -** metamethods for file handles -*/ -static const luaL_Reg metameth[] = { - {"__index", NULL}, /* place holder */ - {"__gc", f_gc}, - {"__close", f_gc}, - {"__tostring", f_tostring}, - {NULL, NULL} -}; - - -static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ - luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ - luaL_newlibtable(L, meth); /* create method table */ - luaL_setfuncs(L, meth, 0); /* add file methods to method table */ - lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ - lua_pop(L, 1); /* pop metatable */ -} - - -/* -** function to (not) close the standard files stdin, stdout, and stderr -*/ -static int io_noclose (lua_State *L) { - LStream *p = tolstream(L); - p->closef = &io_noclose; /* keep file opened */ - luaL_pushfail(L); - lua_pushliteral(L, "cannot close standard file"); - return 2; -} - - -static void createstdfile (lua_State *L, FILE *f, const char *k, - const char *fname) { - LStream *p = newprefile(L); - p->f = f; - p->closef = &io_noclose; - if (k != NULL) { - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ - } - lua_setfield(L, -2, fname); /* add file to module */ -} - - -LUAMOD_API int luaopen_io (lua_State *L) { - luaL_newlib(L, iolib); /* new module */ - createmeta(L); - /* create (and set) default files */ - createstdfile(L, stdin, IO_INPUT, "stdin"); - createstdfile(L, stdout, IO_OUTPUT, "stdout"); - createstdfile(L, stderr, NULL, "stderr"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ljumptab.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ljumptab.h deleted file mode 100644 index 8306f25..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ljumptab.h +++ /dev/null @@ -1,112 +0,0 @@ -/* -** $Id: ljumptab.h $ -** Jump Table for the Lua interpreter -** See Copyright Notice in lua.h -*/ - - -#undef vmdispatch -#undef vmcase -#undef vmbreak - -#define vmdispatch(x) goto *disptab[x]; - -#define vmcase(l) L_##l: - -#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); - - -static const void *const disptab[NUM_OPCODES] = { - -#if 0 -** you can update the following list with this command: -** -** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h -** -#endif - -&&L_OP_MOVE, -&&L_OP_LOADI, -&&L_OP_LOADF, -&&L_OP_LOADK, -&&L_OP_LOADKX, -&&L_OP_LOADFALSE, -&&L_OP_LFALSESKIP, -&&L_OP_LOADTRUE, -&&L_OP_LOADNIL, -&&L_OP_GETUPVAL, -&&L_OP_SETUPVAL, -&&L_OP_GETTABUP, -&&L_OP_GETTABLE, -&&L_OP_GETI, -&&L_OP_GETFIELD, -&&L_OP_SETTABUP, -&&L_OP_SETTABLE, -&&L_OP_SETI, -&&L_OP_SETFIELD, -&&L_OP_NEWTABLE, -&&L_OP_SELF, -&&L_OP_ADDI, -&&L_OP_ADDK, -&&L_OP_SUBK, -&&L_OP_MULK, -&&L_OP_MODK, -&&L_OP_POWK, -&&L_OP_DIVK, -&&L_OP_IDIVK, -&&L_OP_BANDK, -&&L_OP_BORK, -&&L_OP_BXORK, -&&L_OP_SHRI, -&&L_OP_SHLI, -&&L_OP_ADD, -&&L_OP_SUB, -&&L_OP_MUL, -&&L_OP_MOD, -&&L_OP_POW, -&&L_OP_DIV, -&&L_OP_IDIV, -&&L_OP_BAND, -&&L_OP_BOR, -&&L_OP_BXOR, -&&L_OP_SHL, -&&L_OP_SHR, -&&L_OP_MMBIN, -&&L_OP_MMBINI, -&&L_OP_MMBINK, -&&L_OP_UNM, -&&L_OP_BNOT, -&&L_OP_NOT, -&&L_OP_LEN, -&&L_OP_CONCAT, -&&L_OP_CLOSE, -&&L_OP_TBC, -&&L_OP_JMP, -&&L_OP_EQ, -&&L_OP_LT, -&&L_OP_LE, -&&L_OP_EQK, -&&L_OP_EQI, -&&L_OP_LTI, -&&L_OP_LEI, -&&L_OP_GTI, -&&L_OP_GEI, -&&L_OP_TEST, -&&L_OP_TESTSET, -&&L_OP_CALL, -&&L_OP_TAILCALL, -&&L_OP_RETURN, -&&L_OP_RETURN0, -&&L_OP_RETURN1, -&&L_OP_FORLOOP, -&&L_OP_FORPREP, -&&L_OP_TFORPREP, -&&L_OP_TFORCALL, -&&L_OP_TFORLOOP, -&&L_OP_SETLIST, -&&L_OP_CLOSURE, -&&L_OP_VARARG, -&&L_OP_VARARGPREP, -&&L_OP_EXTRAARG - -}; diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.c deleted file mode 100644 index e991517..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.c +++ /dev/null @@ -1,581 +0,0 @@ -/* -** $Id: llex.c $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - -#define llex_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lctype.h" -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "llex.h" -#include "lobject.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lzio.h" - - - -#define next(ls) (ls->current = zgetc(ls->z)) - - - -#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') - - -/* ORDER RESERVED */ -static const char *const luaX_tokens [] = { - "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", "goto", "if", - "in", "local", "nil", "not", "or", "repeat", - "return", "then", "true", "until", "while", - "//", "..", "...", "==", ">=", "<=", "~=", - "<<", ">>", "::", "", - "", "", "", "" -}; - - -#define save_and_next(ls) (save(ls, ls->current), next(ls)) - - -static l_noret lexerror (LexState *ls, const char *msg, int token); - - -static void save (LexState *ls, int c) { - Mbuffer *b = ls->buff; - if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { - size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZE/2) - lexerror(ls, "lexical element too long", 0); - newsize = luaZ_sizebuffer(b) * 2; - luaZ_resizebuffer(ls->L, b, newsize); - } - b->buffer[luaZ_bufflen(b)++] = cast_char(c); -} - - -void luaX_init (lua_State *L) { - int i; - TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ - luaC_fix(L, obj2gco(e)); /* never collect this name */ - for (i=0; iextra = cast_byte(i+1); /* reserved word */ - } -} - - -const char *luaX_token2str (LexState *ls, int token) { - if (token < FIRST_RESERVED) { /* single-byte symbols? */ - if (lisprint(token)) - return luaO_pushfstring(ls->L, "'%c'", token); - else /* control character */ - return luaO_pushfstring(ls->L, "'<\\%d>'", token); - } - else { - const char *s = luaX_tokens[token - FIRST_RESERVED]; - if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ - return luaO_pushfstring(ls->L, "'%s'", s); - else /* names, strings, and numerals */ - return s; - } -} - - -static const char *txtToken (LexState *ls, int token) { - switch (token) { - case TK_NAME: case TK_STRING: - case TK_FLT: case TK_INT: - save(ls, '\0'); - return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); - default: - return luaX_token2str(ls, token); - } -} - - -static l_noret lexerror (LexState *ls, const char *msg, int token) { - msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); - if (token) - luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); - luaD_throw(ls->L, LUA_ERRSYNTAX); -} - - -l_noret luaX_syntaxerror (LexState *ls, const char *msg) { - lexerror(ls, msg, ls->t.token); -} - - -/* -** Creates a new string and anchors it in scanner's table so that it -** will not be collected until the end of the compilation; by that time -** it should be anchored somewhere. It also internalizes long strings, -** ensuring there is only one copy of each unique string. The table -** here is used as a set: the string enters as the key, while its value -** is irrelevant. We use the string itself as the value only because it -** is a TValue readly available. Later, the code generation can change -** this value. -*/ -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { - lua_State *L = ls->L; - TString *ts = luaS_newlstr(L, str, l); /* create new string */ - const TValue *o = luaH_getstr(ls->h, ts); - if (!ttisnil(o)) /* string already present? */ - ts = keystrval(nodefromval(o)); /* get saved copy */ - else { /* not in use yet */ - TValue *stv = s2v(L->top++); /* reserve stack space for string */ - setsvalue(L, stv, ts); /* temporarily anchor the string */ - luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */ - /* table is not a metatable, so it does not need to invalidate cache */ - luaC_checkGC(L); - L->top--; /* remove string from stack */ - } - return ts; -} - - -/* -** increment line number and skips newline sequence (any of -** \n, \r, \n\r, or \r\n) -*/ -static void inclinenumber (LexState *ls) { - int old = ls->current; - lua_assert(currIsNewline(ls)); - next(ls); /* skip '\n' or '\r' */ - if (currIsNewline(ls) && ls->current != old) - next(ls); /* skip '\n\r' or '\r\n' */ - if (++ls->linenumber >= MAX_INT) - lexerror(ls, "chunk has too many lines", 0); -} - - -void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, - int firstchar) { - ls->t.token = 0; - ls->L = L; - ls->current = firstchar; - ls->lookahead.token = TK_EOS; /* no look-ahead token */ - ls->z = z; - ls->fs = NULL; - ls->linenumber = 1; - ls->lastline = 1; - ls->source = source; - ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ - luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ -} - - - -/* -** ======================================================= -** LEXICAL ANALYZER -** ======================================================= -*/ - - -static int check_next1 (LexState *ls, int c) { - if (ls->current == c) { - next(ls); - return 1; - } - else return 0; -} - - -/* -** Check whether current char is in set 'set' (with two chars) and -** saves it -*/ -static int check_next2 (LexState *ls, const char *set) { - lua_assert(set[2] == '\0'); - if (ls->current == set[0] || ls->current == set[1]) { - save_and_next(ls); - return 1; - } - else return 0; -} - - -/* LUA_NUMBER */ -/* -** This function is quite liberal in what it accepts, as 'luaO_str2num' -** will reject ill-formed numerals. Roughly, it accepts the following -** pattern: -** -** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))* -** -** The only tricky part is to accept [+-] only after a valid exponent -** mark, to avoid reading '3-4' or '0xe+1' as a single number. -** -** The caller might have already read an initial dot. -*/ -static int read_numeral (LexState *ls, SemInfo *seminfo) { - TValue obj; - const char *expo = "Ee"; - int first = ls->current; - lua_assert(lisdigit(ls->current)); - save_and_next(ls); - if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ - expo = "Pp"; - for (;;) { - if (check_next2(ls, expo)) /* exponent mark? */ - check_next2(ls, "-+"); /* optional exponent sign */ - else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ - save_and_next(ls); - else break; - } - if (lislalpha(ls->current)) /* is numeral touching a letter? */ - save_and_next(ls); /* force an error */ - save(ls, '\0'); - if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ - lexerror(ls, "malformed number", TK_FLT); - if (ttisinteger(&obj)) { - seminfo->i = ivalue(&obj); - return TK_INT; - } - else { - lua_assert(ttisfloat(&obj)); - seminfo->r = fltvalue(&obj); - return TK_FLT; - } -} - - -/* -** read a sequence '[=*[' or ']=*]', leaving the last bracket. If -** sequence is well formed, return its number of '='s + 2; otherwise, -** return 1 if it is a single bracket (no '='s and no 2nd bracket); -** otherwise (an unfinished '[==...') return 0. -*/ -static size_t skip_sep (LexState *ls) { - size_t count = 0; - int s = ls->current; - lua_assert(s == '[' || s == ']'); - save_and_next(ls); - while (ls->current == '=') { - save_and_next(ls); - count++; - } - return (ls->current == s) ? count + 2 - : (count == 0) ? 1 - : 0; -} - - -static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { - int line = ls->linenumber; /* initial line (for error message) */ - save_and_next(ls); /* skip 2nd '[' */ - if (currIsNewline(ls)) /* string starts with a newline? */ - inclinenumber(ls); /* skip it */ - for (;;) { - switch (ls->current) { - case EOZ: { /* error */ - const char *what = (seminfo ? "string" : "comment"); - const char *msg = luaO_pushfstring(ls->L, - "unfinished long %s (starting at line %d)", what, line); - lexerror(ls, msg, TK_EOS); - break; /* to avoid warnings */ - } - case ']': { - if (skip_sep(ls) == sep) { - save_and_next(ls); /* skip 2nd ']' */ - goto endloop; - } - break; - } - case '\n': case '\r': { - save(ls, '\n'); - inclinenumber(ls); - if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ - break; - } - default: { - if (seminfo) save_and_next(ls); - else next(ls); - } - } - } endloop: - if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, - luaZ_bufflen(ls->buff) - 2 * sep); -} - - -static void esccheck (LexState *ls, int c, const char *msg) { - if (!c) { - if (ls->current != EOZ) - save_and_next(ls); /* add current to buffer for error message */ - lexerror(ls, msg, TK_STRING); - } -} - - -static int gethexa (LexState *ls) { - save_and_next(ls); - esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); - return luaO_hexavalue(ls->current); -} - - -static int readhexaesc (LexState *ls) { - int r = gethexa(ls); - r = (r << 4) + gethexa(ls); - luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ - return r; -} - - -static unsigned long readutf8esc (LexState *ls) { - unsigned long r; - int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ - save_and_next(ls); /* skip 'u' */ - esccheck(ls, ls->current == '{', "missing '{'"); - r = gethexa(ls); /* must have at least one digit */ - while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { - i++; - esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); - r = (r << 4) + luaO_hexavalue(ls->current); - } - esccheck(ls, ls->current == '}', "missing '}'"); - next(ls); /* skip '}' */ - luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ - return r; -} - - -static void utf8esc (LexState *ls) { - char buff[UTF8BUFFSZ]; - int n = luaO_utf8esc(buff, readutf8esc(ls)); - for (; n > 0; n--) /* add 'buff' to string */ - save(ls, buff[UTF8BUFFSZ - n]); -} - - -static int readdecesc (LexState *ls) { - int i; - int r = 0; /* result accumulator */ - for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ - r = 10*r + ls->current - '0'; - save_and_next(ls); - } - esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); - luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ - return r; -} - - -static void read_string (LexState *ls, int del, SemInfo *seminfo) { - save_and_next(ls); /* keep delimiter (for error messages) */ - while (ls->current != del) { - switch (ls->current) { - case EOZ: - lexerror(ls, "unfinished string", TK_EOS); - break; /* to avoid warnings */ - case '\n': - case '\r': - lexerror(ls, "unfinished string", TK_STRING); - break; /* to avoid warnings */ - case '\\': { /* escape sequences */ - int c; /* final character to be saved */ - save_and_next(ls); /* keep '\\' for error messages */ - switch (ls->current) { - case 'a': c = '\a'; goto read_save; - case 'b': c = '\b'; goto read_save; - case 'f': c = '\f'; goto read_save; - case 'n': c = '\n'; goto read_save; - case 'r': c = '\r'; goto read_save; - case 't': c = '\t'; goto read_save; - case 'v': c = '\v'; goto read_save; - case 'x': c = readhexaesc(ls); goto read_save; - case 'u': utf8esc(ls); goto no_save; - case '\n': case '\r': - inclinenumber(ls); c = '\n'; goto only_save; - case '\\': case '\"': case '\'': - c = ls->current; goto read_save; - case EOZ: goto no_save; /* will raise an error next loop */ - case 'z': { /* zap following span of spaces */ - luaZ_buffremove(ls->buff, 1); /* remove '\\' */ - next(ls); /* skip the 'z' */ - while (lisspace(ls->current)) { - if (currIsNewline(ls)) inclinenumber(ls); - else next(ls); - } - goto no_save; - } - default: { - esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); - c = readdecesc(ls); /* digital escape '\ddd' */ - goto only_save; - } - } - read_save: - next(ls); - /* go through */ - only_save: - luaZ_buffremove(ls->buff, 1); /* remove '\\' */ - save(ls, c); - /* go through */ - no_save: break; - } - default: - save_and_next(ls); - } - } - save_and_next(ls); /* skip delimiter */ - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, - luaZ_bufflen(ls->buff) - 2); -} - - -static int llex (LexState *ls, SemInfo *seminfo) { - luaZ_resetbuffer(ls->buff); - for (;;) { - switch (ls->current) { - case '\n': case '\r': { /* line breaks */ - inclinenumber(ls); - break; - } - case ' ': case '\f': case '\t': case '\v': { /* spaces */ - next(ls); - break; - } - case '-': { /* '-' or '--' (comment) */ - next(ls); - if (ls->current != '-') return '-'; - /* else is a comment */ - next(ls); - if (ls->current == '[') { /* long comment? */ - size_t sep = skip_sep(ls); - luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ - if (sep >= 2) { - read_long_string(ls, NULL, sep); /* skip long comment */ - luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ - break; - } - } - /* else short comment */ - while (!currIsNewline(ls) && ls->current != EOZ) - next(ls); /* skip until end of line (or end of file) */ - break; - } - case '[': { /* long string or simply '[' */ - size_t sep = skip_sep(ls); - if (sep >= 2) { - read_long_string(ls, seminfo, sep); - return TK_STRING; - } - else if (sep == 0) /* '[=...' missing second bracket? */ - lexerror(ls, "invalid long string delimiter", TK_STRING); - return '['; - } - case '=': { - next(ls); - if (check_next1(ls, '=')) return TK_EQ; /* '==' */ - else return '='; - } - case '<': { - next(ls); - if (check_next1(ls, '=')) return TK_LE; /* '<=' */ - else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */ - else return '<'; - } - case '>': { - next(ls); - if (check_next1(ls, '=')) return TK_GE; /* '>=' */ - else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */ - else return '>'; - } - case '/': { - next(ls); - if (check_next1(ls, '/')) return TK_IDIV; /* '//' */ - else return '/'; - } - case '~': { - next(ls); - if (check_next1(ls, '=')) return TK_NE; /* '~=' */ - else return '~'; - } - case ':': { - next(ls); - if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */ - else return ':'; - } - case '"': case '\'': { /* short literal strings */ - read_string(ls, ls->current, seminfo); - return TK_STRING; - } - case '.': { /* '.', '..', '...', or number */ - save_and_next(ls); - if (check_next1(ls, '.')) { - if (check_next1(ls, '.')) - return TK_DOTS; /* '...' */ - else return TK_CONCAT; /* '..' */ - } - else if (!lisdigit(ls->current)) return '.'; - else return read_numeral(ls, seminfo); - } - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': { - return read_numeral(ls, seminfo); - } - case EOZ: { - return TK_EOS; - } - default: { - if (lislalpha(ls->current)) { /* identifier or reserved word? */ - TString *ts; - do { - save_and_next(ls); - } while (lislalnum(ls->current)); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - seminfo->ts = ts; - if (isreserved(ts)) /* reserved word? */ - return ts->extra - 1 + FIRST_RESERVED; - else { - return TK_NAME; - } - } - else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */ - int c = ls->current; - next(ls); - return c; - } - } - } - } -} - - -void luaX_next (LexState *ls) { - ls->lastline = ls->linenumber; - if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ - ls->t = ls->lookahead; /* use this one */ - ls->lookahead.token = TK_EOS; /* and discharge it */ - } - else - ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ -} - - -int luaX_lookahead (LexState *ls) { - lua_assert(ls->lookahead.token == TK_EOS); - ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); - return ls->lookahead.token; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.h deleted file mode 100644 index 389d2f8..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llex.h +++ /dev/null @@ -1,91 +0,0 @@ -/* -** $Id: llex.h $ -** Lexical Analyzer -** See Copyright Notice in lua.h -*/ - -#ifndef llex_h -#define llex_h - -#include - -#include "lobject.h" -#include "lzio.h" - - -/* -** Single-char tokens (terminal symbols) are represented by their own -** numeric code. Other tokens start at the following value. -*/ -#define FIRST_RESERVED (UCHAR_MAX + 1) - - -#if !defined(LUA_ENV) -#define LUA_ENV "_ENV" -#endif - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER RESERVED" -*/ -enum RESERVED { - /* terminal symbols denoted by reserved words */ - TK_AND = FIRST_RESERVED, TK_BREAK, - TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, - TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, - /* other terminal symbols */ - TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, - TK_SHL, TK_SHR, - TK_DBCOLON, TK_EOS, - TK_FLT, TK_INT, TK_NAME, TK_STRING -}; - -/* number of reserved words */ -#define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) - - -typedef union { - lua_Number r; - lua_Integer i; - TString *ts; -} SemInfo; /* semantics information */ - - -typedef struct Token { - int token; - SemInfo seminfo; -} Token; - - -/* state of the lexer plus state of the parser when shared by all - functions */ -typedef struct LexState { - int current; /* current character (charint) */ - int linenumber; /* input line counter */ - int lastline; /* line of last token 'consumed' */ - Token t; /* current token */ - Token lookahead; /* look ahead token */ - struct FuncState *fs; /* current function (parser) */ - struct lua_State *L; - ZIO *z; /* input stream */ - Mbuffer *buff; /* buffer for tokens */ - Table *h; /* to avoid collection/reuse strings */ - struct Dyndata *dyd; /* dynamic structures used by the parser */ - TString *source; /* current source name */ - TString *envn; /* environment variable name */ -} LexState; - - -LUAI_FUNC void luaX_init (lua_State *L); -LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, - TString *source, int firstchar); -LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); -LUAI_FUNC void luaX_next (LexState *ls); -LUAI_FUNC int luaX_lookahead (LexState *ls); -LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); -LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llimits.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llimits.h deleted file mode 100644 index 52a32f9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/llimits.h +++ /dev/null @@ -1,367 +0,0 @@ -/* -** $Id: llimits.h $ -** Limits, basic types, and some other 'installation-dependent' definitions -** See Copyright Notice in lua.h -*/ - -#ifndef llimits_h -#define llimits_h - - -#include -#include - - -#include "lua.h" - - -/* -** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count -** the total memory used by Lua (in bytes). Usually, 'size_t' and -** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. -*/ -#if defined(LUAI_MEM) /* { external definitions? */ -typedef LUAI_UMEM lu_mem; -typedef LUAI_MEM l_mem; -#elif LUAI_IS32INT /* }{ */ -typedef size_t lu_mem; -typedef ptrdiff_t l_mem; -#else /* 16-bit ints */ /* }{ */ -typedef unsigned long lu_mem; -typedef long l_mem; -#endif /* } */ - - -/* chars used as small naturals (so that 'char' is reserved for characters) */ -typedef unsigned char lu_byte; -typedef signed char ls_byte; - - -/* maximum value for size_t */ -#define MAX_SIZET ((size_t)(~(size_t)0)) - -/* maximum size visible for Lua (must be representable in a lua_Integer) */ -#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ - : (size_t)(LUA_MAXINTEGER)) - - -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) - -#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) - - -#define MAX_INT INT_MAX /* maximum value of an int */ - - -/* -** floor of the log2 of the maximum signed value for integral type 't'. -** (That is, maximum 'n' such that '2^n' fits in the given signed type.) -*/ -#define log2maxs(t) (sizeof(t) * 8 - 2) - - -/* -** test whether an unsigned value is a power of 2 (or zero) -*/ -#define ispow2(x) (((x) & ((x) - 1)) == 0) - - -/* number of chars of a literal string without the ending \0 */ -#define LL(x) (sizeof(x)/sizeof(char) - 1) - - -/* -** conversion of pointer to unsigned integer: -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value -*/ -#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) - - - -/* types of 'usual argument conversions' for lua_Number and lua_Integer */ -typedef LUAI_UACNUMBER l_uacNumber; -typedef LUAI_UACINT l_uacInt; - - -/* -** Internal assertions for in-house debugging -*/ -#if defined LUAI_ASSERT -#undef NDEBUG -#include -#define lua_assert(c) assert(c) -#endif - -#if defined(lua_assert) -#define check_exp(c,e) (lua_assert(c), (e)) -/* to avoid problems with conditions too long */ -#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) -#else -#define lua_assert(c) ((void)0) -#define check_exp(c,e) (e) -#define lua_longassert(c) ((void)0) -#endif - -/* -** assertion for checking API calls -*/ -#if !defined(luai_apicheck) -#define luai_apicheck(l,e) ((void)l, lua_assert(e)) -#endif - -#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) - - -/* macro to avoid warnings about unused variables */ -#if !defined(UNUSED) -#define UNUSED(x) ((void)(x)) -#endif - - -/* type casts (a macro highlights casts in the code) */ -#define cast(t, exp) ((t)(exp)) - -#define cast_void(i) cast(void, (i)) -#define cast_voidp(i) cast(void *, (i)) -#define cast_num(i) cast(lua_Number, (i)) -#define cast_int(i) cast(int, (i)) -#define cast_uint(i) cast(unsigned int, (i)) -#define cast_byte(i) cast(lu_byte, (i)) -#define cast_uchar(i) cast(unsigned char, (i)) -#define cast_char(i) cast(char, (i)) -#define cast_charp(i) cast(char *, (i)) -#define cast_sizet(i) cast(size_t, (i)) - - -/* cast a signed lua_Integer to lua_Unsigned */ -#if !defined(l_castS2U) -#define l_castS2U(i) ((lua_Unsigned)(i)) -#endif - -/* -** cast a lua_Unsigned to a signed lua_Integer; this cast is -** not strict ISO C, but two-complement architectures should -** work fine. -*/ -#if !defined(l_castU2S) -#define l_castU2S(i) ((lua_Integer)(i)) -#endif - - -/* -** non-return type -*/ -#if !defined(l_noret) - -#if defined(__GNUC__) -#define l_noret void __attribute__((noreturn)) -#elif defined(_MSC_VER) && _MSC_VER >= 1200 -#define l_noret void __declspec(noreturn) -#else -#define l_noret void -#endif - -#endif - - -/* -** Inline functions -*/ -#if !defined(LUA_USE_C89) -#define l_inline inline -#elif defined(__GNUC__) -#define l_inline __inline__ -#else -#define l_inline /* empty */ -#endif - -#define l_sinline static l_inline - - -/* -** type for virtual-machine instructions; -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) -*/ -#if LUAI_IS32INT -typedef unsigned int l_uint32; -#else -typedef unsigned long l_uint32; -#endif - -typedef l_uint32 Instruction; - - - -/* -** Maximum length for short strings, that is, strings that are -** internalized. (Cannot be smaller than reserved words or tags for -** metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#if !defined(LUAI_MAXSHORTLEN) -#define LUAI_MAXSHORTLEN 40 -#endif - - -/* -** Initial size for the string table (must be power of 2). -** The Lua core alone registers ~50 strings (reserved words + -** metaevent keys + a few others). Libraries would typically add -** a few dozens more. -*/ -#if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 128 -#endif - - -/* -** Size of cache for strings in the API. 'N' is the number of -** sets (better be a prime) and "M" is the size of each set (M == 1 -** makes a direct cache.) -*/ -#if !defined(STRCACHE_N) -#define STRCACHE_N 53 -#define STRCACHE_M 2 -#endif - - -/* minimum size for string buffer */ -#if !defined(LUA_MINBUFFER) -#define LUA_MINBUFFER 32 -#endif - - -/* -** Maximum depth for nested C calls, syntactical nested non-terminals, -** and other features implemented through recursion in C. (Value must -** fit in a 16-bit unsigned integer. It must also be compatible with -** the size of the C stack.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 -#endif - - -/* -** macros that are executed whenever program enters the Lua core -** ('lua_lock') and leaves the core ('lua_unlock') -*/ -#if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif - -/* -** macro executed during Lua functions at points where the -** function can yield. -*/ -#if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} -#endif - - -/* -** these macros allow user-specific actions when a thread is -** created/deleted/resumed/yielded. -*/ -#if !defined(luai_userstateopen) -#define luai_userstateopen(L) ((void)L) -#endif - -#if !defined(luai_userstateclose) -#define luai_userstateclose(L) ((void)L) -#endif - -#if !defined(luai_userstatethread) -#define luai_userstatethread(L,L1) ((void)L) -#endif - -#if !defined(luai_userstatefree) -#define luai_userstatefree(L,L1) ((void)L) -#endif - -#if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) -#endif - -#if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) -#endif - - - -/* -** The luai_num* macros define the primitive operations over numbers. -*/ - -/* floor division (defined as 'floor(a/b)') */ -#if !defined(luai_numidiv) -#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) -#endif - -/* float division */ -#if !defined(luai_numdiv) -#define luai_numdiv(L,a,b) ((a)/(b)) -#endif - -/* -** modulo: defined as 'a - floor(a/b)*b'; the direct computation -** using this definition has several problems with rounding errors, -** so it is better to use 'fmod'. 'fmod' gives the result of -** 'a - trunc(a/b)*b', and therefore must be corrected when -** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a -** non-integer negative result: non-integer result is equivalent to -** a non-zero remainder 'm'; negative result is equivalent to 'a' and -** 'b' with different signs, or 'm' and 'b' with different signs -** (as the result 'm' of 'fmod' has the same sign of 'a'). -*/ -#if !defined(luai_nummod) -#define luai_nummod(L,a,b,m) \ - { (void)L; (m) = l_mathop(fmod)(a,b); \ - if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } -#endif - -/* exponentiation */ -#if !defined(luai_numpow) -#define luai_numpow(L,a,b) \ - ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) -#endif - -/* the others are quite standard operations */ -#if !defined(luai_numadd) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numunm(L,a) (-(a)) -#define luai_numeq(a,b) ((a)==(b)) -#define luai_numlt(a,b) ((a)<(b)) -#define luai_numle(a,b) ((a)<=(b)) -#define luai_numgt(a,b) ((a)>(b)) -#define luai_numge(a,b) ((a)>=(b)) -#define luai_numisnan(a) (!luai_numeq((a), (a))) -#endif - - - - - -/* -** macro to control inclusion of some hard tests on stack reallocation -*/ -#if !defined(HARDSTACKTESTS) -#define condmovestack(L,pre,pos) ((void)0) -#else -/* realloc stack keeping its size */ -#define condmovestack(L,pre,pos) \ - { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } -#endif - -#if !defined(HARDMEMTESTS) -#define condchangemem(L,pre,pos) ((void)0) -#else -#define condchangemem(L,pre,pos) \ - { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmathlib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmathlib.c deleted file mode 100644 index e0c61a1..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmathlib.c +++ /dev/null @@ -1,764 +0,0 @@ -/* -** $Id: lmathlib.c $ -** Standard mathematical library -** See Copyright Notice in lua.h -*/ - -#define lmathlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#undef PI -#define PI (l_mathop(3.141592653589793238462643383279502884)) - - -static int math_abs (lua_State *L) { - if (lua_isinteger(L, 1)) { - lua_Integer n = lua_tointeger(L, 1); - if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n); - lua_pushinteger(L, n); - } - else - lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sin (lua_State *L) { - lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_cos (lua_State *L) { - lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tan (lua_State *L) { - lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_asin (lua_State *L) { - lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_acos (lua_State *L) { - lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_atan (lua_State *L) { - lua_Number y = luaL_checknumber(L, 1); - lua_Number x = luaL_optnumber(L, 2, 1); - lua_pushnumber(L, l_mathop(atan2)(y, x)); - return 1; -} - - -static int math_toint (lua_State *L) { - int valid; - lua_Integer n = lua_tointegerx(L, 1, &valid); - if (l_likely(valid)) - lua_pushinteger(L, n); - else { - luaL_checkany(L, 1); - luaL_pushfail(L); /* value is not convertible to integer */ - } - return 1; -} - - -static void pushnumint (lua_State *L, lua_Number d) { - lua_Integer n; - if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */ - lua_pushinteger(L, n); /* result is integer */ - else - lua_pushnumber(L, d); /* result is float */ -} - - -static int math_floor (lua_State *L) { - if (lua_isinteger(L, 1)) - lua_settop(L, 1); /* integer is its own floor */ - else { - lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1)); - pushnumint(L, d); - } - return 1; -} - - -static int math_ceil (lua_State *L) { - if (lua_isinteger(L, 1)) - lua_settop(L, 1); /* integer is its own ceil */ - else { - lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); - pushnumint(L, d); - } - return 1; -} - - -static int math_fmod (lua_State *L) { - if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) { - lua_Integer d = lua_tointeger(L, 2); - if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */ - luaL_argcheck(L, d != 0, 2, "zero"); - lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */ - } - else - lua_pushinteger(L, lua_tointeger(L, 1) % d); - } - else - lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), - luaL_checknumber(L, 2))); - return 1; -} - - -/* -** next function does not use 'modf', avoiding problems with 'double*' -** (which is not compatible with 'float*') when lua_Number is not -** 'double'. -*/ -static int math_modf (lua_State *L) { - if (lua_isinteger(L ,1)) { - lua_settop(L, 1); /* number is its own integer part */ - lua_pushnumber(L, 0); /* no fractional part */ - } - else { - lua_Number n = luaL_checknumber(L, 1); - /* integer part (rounds toward zero) */ - lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); - pushnumint(L, ip); - /* fractional part (test needed for inf/-inf) */ - lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip)); - } - return 2; -} - - -static int math_sqrt (lua_State *L) { - lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); - return 1; -} - - -static int math_ult (lua_State *L) { - lua_Integer a = luaL_checkinteger(L, 1); - lua_Integer b = luaL_checkinteger(L, 2); - lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b); - return 1; -} - -static int math_log (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number res; - if (lua_isnoneornil(L, 2)) - res = l_mathop(log)(x); - else { - lua_Number base = luaL_checknumber(L, 2); -#if !defined(LUA_USE_C89) - if (base == l_mathop(2.0)) - res = l_mathop(log2)(x); - else -#endif - if (base == l_mathop(10.0)) - res = l_mathop(log10)(x); - else - res = l_mathop(log)(x)/l_mathop(log)(base); - } - lua_pushnumber(L, res); - return 1; -} - -static int math_exp (lua_State *L) { - lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_deg (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); - return 1; -} - -static int math_rad (lua_State *L) { - lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); - return 1; -} - - -static int math_min (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int imin = 1; /* index of current minimum value */ - int i; - luaL_argcheck(L, n >= 1, 1, "value expected"); - for (i = 2; i <= n; i++) { - if (lua_compare(L, i, imin, LUA_OPLT)) - imin = i; - } - lua_pushvalue(L, imin); - return 1; -} - - -static int math_max (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int imax = 1; /* index of current maximum value */ - int i; - luaL_argcheck(L, n >= 1, 1, "value expected"); - for (i = 2; i <= n; i++) { - if (lua_compare(L, imax, i, LUA_OPLT)) - imax = i; - } - lua_pushvalue(L, imax); - return 1; -} - - -static int math_type (lua_State *L) { - if (lua_type(L, 1) == LUA_TNUMBER) - lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); - else { - luaL_checkany(L, 1); - luaL_pushfail(L); - } - return 1; -} - - - -/* -** {================================================================== -** Pseudo-Random Number Generator based on 'xoshiro256**'. -** =================================================================== -*/ - -/* number of binary digits in the mantissa of a float */ -#define FIGS l_floatatt(MANT_DIG) - -#if FIGS > 64 -/* there are only 64 random bits; use them all */ -#undef FIGS -#define FIGS 64 -#endif - - -/* -** LUA_RAND32 forces the use of 32-bit integers in the implementation -** of the PRN generator (mainly for testing). -*/ -#if !defined(LUA_RAND32) && !defined(Rand64) - -/* try to find an integer type with at least 64 bits */ - -#if (ULONG_MAX >> 31 >> 31) >= 3 - -/* 'long' has at least 64 bits */ -#define Rand64 unsigned long - -#elif !defined(LUA_USE_C89) && defined(LLONG_MAX) - -/* there is a 'long long' type (which must have at least 64 bits) */ -#define Rand64 unsigned long long - -#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3 - -/* 'lua_Integer' has at least 64 bits */ -#define Rand64 lua_Unsigned - -#endif - -#endif - - -#if defined(Rand64) /* { */ - -/* -** Standard implementation, using 64-bit integers. -** If 'Rand64' has more than 64 bits, the extra bits do not interfere -** with the 64 initial bits, except in a right shift. Moreover, the -** final result has to discard the extra bits. -*/ - -/* avoid using extra bits when needed */ -#define trim64(x) ((x) & 0xffffffffffffffffu) - - -/* rotate left 'x' by 'n' bits */ -static Rand64 rotl (Rand64 x, int n) { - return (x << n) | (trim64(x) >> (64 - n)); -} - -static Rand64 nextrand (Rand64 *state) { - Rand64 state0 = state[0]; - Rand64 state1 = state[1]; - Rand64 state2 = state[2] ^ state0; - Rand64 state3 = state[3] ^ state1; - Rand64 res = rotl(state1 * 5, 7) * 9; - state[0] = state0 ^ state3; - state[1] = state1 ^ state2; - state[2] = state2 ^ (state1 << 17); - state[3] = rotl(state3, 45); - return res; -} - - -/* must take care to not shift stuff by more than 63 slots */ - - -/* -** Convert bits from a random integer into a float in the -** interval [0,1), getting the higher FIG bits from the -** random unsigned integer and converting that to a float. -*/ - -/* must throw out the extra (64 - FIGS) bits */ -#define shift64_FIG (64 - FIGS) - -/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ -#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) - -static lua_Number I2d (Rand64 x) { - return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG; -} - -/* convert a 'Rand64' to a 'lua_Unsigned' */ -#define I2UInt(x) ((lua_Unsigned)trim64(x)) - -/* convert a 'lua_Unsigned' to a 'Rand64' */ -#define Int2I(x) ((Rand64)(x)) - - -#else /* no 'Rand64' }{ */ - -/* get an integer with at least 32 bits */ -#if LUAI_IS32INT -typedef unsigned int lu_int32; -#else -typedef unsigned long lu_int32; -#endif - - -/* -** Use two 32-bit integers to represent a 64-bit quantity. -*/ -typedef struct Rand64 { - lu_int32 h; /* higher half */ - lu_int32 l; /* lower half */ -} Rand64; - - -/* -** If 'lu_int32' has more than 32 bits, the extra bits do not interfere -** with the 32 initial bits, except in a right shift and comparisons. -** Moreover, the final result has to discard the extra bits. -*/ - -/* avoid using extra bits when needed */ -#define trim32(x) ((x) & 0xffffffffu) - - -/* -** basic operations on 'Rand64' values -*/ - -/* build a new Rand64 value */ -static Rand64 packI (lu_int32 h, lu_int32 l) { - Rand64 result; - result.h = h; - result.l = l; - return result; -} - -/* return i << n */ -static Rand64 Ishl (Rand64 i, int n) { - lua_assert(n > 0 && n < 32); - return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n); -} - -/* i1 ^= i2 */ -static void Ixor (Rand64 *i1, Rand64 i2) { - i1->h ^= i2.h; - i1->l ^= i2.l; -} - -/* return i1 + i2 */ -static Rand64 Iadd (Rand64 i1, Rand64 i2) { - Rand64 result = packI(i1.h + i2.h, i1.l + i2.l); - if (trim32(result.l) < trim32(i1.l)) /* carry? */ - result.h++; - return result; -} - -/* return i * 5 */ -static Rand64 times5 (Rand64 i) { - return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */ -} - -/* return i * 9 */ -static Rand64 times9 (Rand64 i) { - return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */ -} - -/* return 'i' rotated left 'n' bits */ -static Rand64 rotl (Rand64 i, int n) { - lua_assert(n > 0 && n < 32); - return packI((i.h << n) | (trim32(i.l) >> (32 - n)), - (trim32(i.h) >> (32 - n)) | (i.l << n)); -} - -/* for offsets larger than 32, rotate right by 64 - offset */ -static Rand64 rotl1 (Rand64 i, int n) { - lua_assert(n > 32 && n < 64); - n = 64 - n; - return packI((trim32(i.h) >> n) | (i.l << (32 - n)), - (i.h << (32 - n)) | (trim32(i.l) >> n)); -} - -/* -** implementation of 'xoshiro256**' algorithm on 'Rand64' values -*/ -static Rand64 nextrand (Rand64 *state) { - Rand64 res = times9(rotl(times5(state[1]), 7)); - Rand64 t = Ishl(state[1], 17); - Ixor(&state[2], state[0]); - Ixor(&state[3], state[1]); - Ixor(&state[1], state[2]); - Ixor(&state[0], state[3]); - Ixor(&state[2], t); - state[3] = rotl1(state[3], 45); - return res; -} - - -/* -** Converts a 'Rand64' into a float. -*/ - -/* an unsigned 1 with proper type */ -#define UONE ((lu_int32)1) - - -#if FIGS <= 32 - -/* 2^(-FIGS) */ -#define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) - -/* -** get up to 32 bits from higher half, shifting right to -** throw out the extra bits. -*/ -static lua_Number I2d (Rand64 x) { - lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS)); - return h * scaleFIG; -} - -#else /* 32 < FIGS <= 64 */ - -/* must take care to not shift stuff by more than 31 slots */ - -/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ -#define scaleFIG \ - (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) - -/* -** use FIGS - 32 bits from lower half, throwing out the other -** (32 - (FIGS - 32)) = (64 - FIGS) bits -*/ -#define shiftLOW (64 - FIGS) - -/* -** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) -*/ -#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0)) - - -static lua_Number I2d (Rand64 x) { - lua_Number h = (lua_Number)trim32(x.h) * shiftHI; - lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW); - return (h + l) * scaleFIG; -} - -#endif - - -/* convert a 'Rand64' to a 'lua_Unsigned' */ -static lua_Unsigned I2UInt (Rand64 x) { - return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l); -} - -/* convert a 'lua_Unsigned' to a 'Rand64' */ -static Rand64 Int2I (lua_Unsigned n) { - return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); -} - -#endif /* } */ - - -/* -** A state uses four 'Rand64' values. -*/ -typedef struct { - Rand64 s[4]; -} RanState; - - -/* -** Project the random integer 'ran' into the interval [0, n]. -** Because 'ran' has 2^B possible values, the projection can only be -** uniform when the size of the interval is a power of 2 (exact -** division). Otherwise, to get a uniform projection into [0, n], we -** first compute 'lim', the smallest Mersenne number not smaller than -** 'n'. We then project 'ran' into the interval [0, lim]. If the result -** is inside [0, n], we are done. Otherwise, we try with another 'ran', -** until we have a result inside the interval. -*/ -static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, - RanState *state) { - if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */ - return ran & n; /* no bias */ - else { - lua_Unsigned lim = n; - /* compute the smallest (2^b - 1) not smaller than 'n' */ - lim |= (lim >> 1); - lim |= (lim >> 2); - lim |= (lim >> 4); - lim |= (lim >> 8); - lim |= (lim >> 16); -#if (LUA_MAXUNSIGNED >> 31) >= 3 - lim |= (lim >> 32); /* integer type has more than 32 bits */ -#endif - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ - && lim >= n /* not smaller than 'n', */ - && (lim >> 1) < n); /* and it is the smallest one */ - while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ - ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ - return ran; - } -} - - -static int math_random (lua_State *L) { - lua_Integer low, up; - lua_Unsigned p; - RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - Rand64 rv = nextrand(state->s); /* next pseudo-random value */ - switch (lua_gettop(L)) { /* check number of arguments */ - case 0: { /* no arguments */ - lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ - return 1; - } - case 1: { /* only upper limit */ - low = 1; - up = luaL_checkinteger(L, 1); - if (up == 0) { /* single 0 as argument? */ - lua_pushinteger(L, I2UInt(rv)); /* full random integer */ - return 1; - } - break; - } - case 2: { /* lower and upper limits */ - low = luaL_checkinteger(L, 1); - up = luaL_checkinteger(L, 2); - break; - } - default: return luaL_error(L, "wrong number of arguments"); - } - /* random integer in the interval [low, up] */ - luaL_argcheck(L, low <= up, 1, "interval is empty"); - /* project random integer into the interval [0, up - low] */ - p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); - lua_pushinteger(L, p + (lua_Unsigned)low); - return 1; -} - - -static void setseed (lua_State *L, Rand64 *state, - lua_Unsigned n1, lua_Unsigned n2) { - int i; - state[0] = Int2I(n1); - state[1] = Int2I(0xff); /* avoid a zero state */ - state[2] = Int2I(n2); - state[3] = Int2I(0); - for (i = 0; i < 16; i++) - nextrand(state); /* discard initial values to "spread" seed */ - lua_pushinteger(L, n1); - lua_pushinteger(L, n2); -} - - -/* -** Set a "random" seed. To get some randomness, use the current time -** and the address of 'L' (in case the machine does address space layout -** randomization). -*/ -static void randseed (lua_State *L, RanState *state) { - lua_Unsigned seed1 = (lua_Unsigned)time(NULL); - lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; - setseed(L, state->s, seed1, seed2); -} - - -static int math_randomseed (lua_State *L) { - RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - if (lua_isnone(L, 1)) { - randseed(L, state); - } - else { - lua_Integer n1 = luaL_checkinteger(L, 1); - lua_Integer n2 = luaL_optinteger(L, 2, 0); - setseed(L, state->s, n1, n2); - } - return 2; /* return seeds */ -} - - -static const luaL_Reg randfuncs[] = { - {"random", math_random}, - {"randomseed", math_randomseed}, - {NULL, NULL} -}; - - -/* -** Register the random functions and initialize their state. -*/ -static void setrandfunc (lua_State *L) { - RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); - randseed(L, state); /* initialize with a "random" seed */ - lua_pop(L, 2); /* remove pushed seeds */ - luaL_setfuncs(L, randfuncs, 1); -} - -/* }================================================================== */ - - -/* -** {================================================================== -** Deprecated functions (for compatibility only) -** =================================================================== -*/ -#if defined(LUA_COMPAT_MATHLIB) - -static int math_cosh (lua_State *L) { - lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_sinh (lua_State *L) { - lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_tanh (lua_State *L) { - lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); - return 1; -} - -static int math_pow (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - lua_Number y = luaL_checknumber(L, 2); - lua_pushnumber(L, l_mathop(pow)(x, y)); - return 1; -} - -static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); - return 2; -} - -static int math_ldexp (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - int ep = (int)luaL_checkinteger(L, 2); - lua_pushnumber(L, l_mathop(ldexp)(x, ep)); - return 1; -} - -static int math_log10 (lua_State *L) { - lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); - return 1; -} - -#endif -/* }================================================================== */ - - - -static const luaL_Reg mathlib[] = { - {"abs", math_abs}, - {"acos", math_acos}, - {"asin", math_asin}, - {"atan", math_atan}, - {"ceil", math_ceil}, - {"cos", math_cos}, - {"deg", math_deg}, - {"exp", math_exp}, - {"tointeger", math_toint}, - {"floor", math_floor}, - {"fmod", math_fmod}, - {"ult", math_ult}, - {"log", math_log}, - {"max", math_max}, - {"min", math_min}, - {"modf", math_modf}, - {"rad", math_rad}, - {"sin", math_sin}, - {"sqrt", math_sqrt}, - {"tan", math_tan}, - {"type", math_type}, -#if defined(LUA_COMPAT_MATHLIB) - {"atan2", math_atan}, - {"cosh", math_cosh}, - {"sinh", math_sinh}, - {"tanh", math_tanh}, - {"pow", math_pow}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, - {"log10", math_log10}, -#endif - /* placeholders */ - {"random", NULL}, - {"randomseed", NULL}, - {"pi", NULL}, - {"huge", NULL}, - {"maxinteger", NULL}, - {"mininteger", NULL}, - {NULL, NULL} -}; - - -/* -** Open math library -*/ -LUAMOD_API int luaopen_math (lua_State *L) { - luaL_newlib(L, mathlib); - lua_pushnumber(L, PI); - lua_setfield(L, -2, "pi"); - lua_pushnumber(L, (lua_Number)HUGE_VAL); - lua_setfield(L, -2, "huge"); - lua_pushinteger(L, LUA_MAXINTEGER); - lua_setfield(L, -2, "maxinteger"); - lua_pushinteger(L, LUA_MININTEGER); - lua_setfield(L, -2, "mininteger"); - setrandfunc(L); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.c deleted file mode 100644 index 9029d58..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.c +++ /dev/null @@ -1,201 +0,0 @@ -/* -** $Id: lmem.c $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - -#define lmem_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" - - -#if defined(EMERGENCYGCTESTS) -/* -** First allocation will fail whenever not building initial state. -** (This fail will trigger 'tryagain' and a full GC cycle at every -** allocation.) -*/ -static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { - if (completestate(g) && ns > 0) /* frees never fail */ - return NULL; /* fail */ - else /* normal allocation */ - return (*g->frealloc)(g->ud, block, os, ns); -} -#else -#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) -#endif - - - - - -/* -** About the realloc function: -** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); -** ('osize' is the old size, 'nsize' is the new size) -** -** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. -** Particularly, frealloc(ud, NULL, 0, 0) does nothing, -** which is equivalent to free(NULL) in ISO C. -** -** - frealloc(ud, NULL, x, s) creates a new block of size 's' -** (no matter 'x'). Returns NULL if it cannot create the new block. -** -** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from -** size 'x' to size 'y'. Returns NULL if it cannot reallocate the -** block to the new size. -*/ - - - - -/* -** {================================================================== -** Functions to allocate/deallocate arrays for the Parser -** =================================================================== -*/ - -/* -** Minimum size for arrays during parsing, to avoid overhead of -** reallocating to size 1, then 2, and then 4. All these arrays -** will be reallocated to exact sizes or erased when parsing ends. -*/ -#define MINSIZEARRAY 4 - - -void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, - int size_elems, int limit, const char *what) { - void *newblock; - int size = *psize; - if (nelems + 1 <= size) /* does one extra element still fit? */ - return block; /* nothing to be done */ - if (size >= limit / 2) { /* cannot double it? */ - if (l_unlikely(size >= limit)) /* cannot grow even a little? */ - luaG_runerror(L, "too many %s (limit is %d)", what, limit); - size = limit; /* still have at least one free place */ - } - else { - size *= 2; - if (size < MINSIZEARRAY) - size = MINSIZEARRAY; /* minimum size */ - } - lua_assert(nelems + 1 <= size && size <= limit); - /* 'limit' ensures that multiplication will not overflow */ - newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, - cast_sizet(size) * size_elems); - *psize = size; /* update only when everything else is OK */ - return newblock; -} - - -/* -** In prototypes, the size of the array is also its number of -** elements (to save memory). So, if it cannot shrink an array -** to its number of elements, the only option is to raise an -** error. -*/ -void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, - int final_n, int size_elem) { - void *newblock; - size_t oldsize = cast_sizet((*size) * size_elem); - size_t newsize = cast_sizet(final_n * size_elem); - lua_assert(newsize <= oldsize); - newblock = luaM_saferealloc_(L, block, oldsize, newsize); - *size = final_n; - return newblock; -} - -/* }================================================================== */ - - -l_noret luaM_toobig (lua_State *L) { - luaG_runerror(L, "memory allocation error: block too big"); -} - - -/* -** Free memory -*/ -void luaM_free_ (lua_State *L, void *block, size_t osize) { - global_State *g = G(L); - lua_assert((osize == 0) == (block == NULL)); - (*g->frealloc)(g->ud, block, osize, 0); - g->GCdebt -= osize; -} - - -/* -** In case of allocation fail, this function will do an emergency -** collection to free some memory and then try the allocation again. -** The GC should not be called while state is not fully built, as the -** collector is not yet fully initialized. Also, it should not be called -** when 'gcstopem' is true, because then the interpreter is in the -** middle of a collection step. -*/ -static void *tryagain (lua_State *L, void *block, - size_t osize, size_t nsize) { - global_State *g = G(L); - if (completestate(g) && !g->gcstopem) { - luaC_fullgc(L, 1); /* try to free some memory... */ - return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ - } - else return NULL; /* cannot free any memory without a full state */ -} - - -/* -** Generic allocation routine. -*/ -void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { - void *newblock; - global_State *g = G(L); - lua_assert((osize == 0) == (block == NULL)); - newblock = firsttry(g, block, osize, nsize); - if (l_unlikely(newblock == NULL && nsize > 0)) { - newblock = tryagain(L, block, osize, nsize); - if (newblock == NULL) /* still no memory? */ - return NULL; /* do not update 'GCdebt' */ - } - lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - osize; - return newblock; -} - - -void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, - size_t nsize) { - void *newblock = luaM_realloc_(L, block, osize, nsize); - if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ - luaM_error(L); - return newblock; -} - - -void *luaM_malloc_ (lua_State *L, size_t size, int tag) { - if (size == 0) - return NULL; /* that's all */ - else { - global_State *g = G(L); - void *newblock = firsttry(g, NULL, tag, size); - if (l_unlikely(newblock == NULL)) { - newblock = tryagain(L, NULL, tag, size); - if (newblock == NULL) - luaM_error(L); - } - g->GCdebt += size; - return newblock; - } -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.h deleted file mode 100644 index 8c75a44..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lmem.h +++ /dev/null @@ -1,93 +0,0 @@ -/* -** $Id: lmem.h $ -** Interface to Memory Manager -** See Copyright Notice in lua.h -*/ - -#ifndef lmem_h -#define lmem_h - - -#include - -#include "llimits.h" -#include "lua.h" - - -#define luaM_error(L) luaD_throw(L, LUA_ERRMEM) - - -/* -** This macro tests whether it is safe to multiply 'n' by the size of -** type 't' without overflows. Because 'e' is always constant, it avoids -** the runtime division MAX_SIZET/(e). -** (The macro is somewhat complex to avoid warnings: The 'sizeof' -** comparison avoids a runtime comparison when overflow cannot occur. -** The compiler should be able to optimize the real test by itself, but -** when it does it, it may give a warning about "comparison is always -** false due to limited range of data type"; the +1 tricks the compiler, -** avoiding this warning but also this optimization.) -*/ -#define luaM_testsize(n,e) \ - (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) - -#define luaM_checksize(L,n,e) \ - (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) - - -/* -** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that -** the result is not larger than 'n' and cannot overflow a 'size_t' -** when multiplied by the size of type 't'. (Assumes that 'n' is an -** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) -*/ -#define luaM_limitN(n,t) \ - ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ - cast_uint((MAX_SIZET/sizeof(t)))) - - -/* -** Arrays of chars do not need any test -*/ -#define luaM_reallocvchar(L,b,on,n) \ - cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) - -#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) -#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) -#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) - -#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) -#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) -#define luaM_newvectorchecked(L,n,t) \ - (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) - -#define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) - -#define luaM_growvector(L,v,nelems,size,t,limit,e) \ - ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ - luaM_limitN(limit,t),e))) - -#define luaM_reallocvector(L, v,oldn,n,t) \ - (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ - cast_sizet(n) * sizeof(t)))) - -#define luaM_shrinkvector(L,v,size,fs,t) \ - ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) - -LUAI_FUNC l_noret luaM_toobig (lua_State *L); - -/* not to be called directly */ -LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, - size_t size); -LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, - size_t size); -LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, - int *size, int size_elem, int limit, - const char *what); -LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, - int final_n, int size_elem); -LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/loadlib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/loadlib.c deleted file mode 100644 index 6f9fa37..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/loadlib.c +++ /dev/null @@ -1,762 +0,0 @@ -/* -** $Id: loadlib.c $ -** Dynamic library loader for Lua -** See Copyright Notice in lua.h -** -** This module contains an implementation of loadlib for Unix systems -** that have dlfcn, an implementation for Windows, and a stub for other -** systems. -*/ - -#define loadlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** LUA_IGMARK is a mark to ignore all before it when building the -** luaopen_ function name. -*/ -#if !defined (LUA_IGMARK) -#define LUA_IGMARK "-" -#endif - - -/* -** LUA_CSUBSEP is the character that replaces dots in submodule names -** when searching for a C loader. -** LUA_LSUBSEP is the character that replaces dots in submodule names -** when searching for a Lua loader. -*/ -#if !defined(LUA_CSUBSEP) -#define LUA_CSUBSEP LUA_DIRSEP -#endif - -#if !defined(LUA_LSUBSEP) -#define LUA_LSUBSEP LUA_DIRSEP -#endif - - -/* prefix for open functions in C libraries */ -#define LUA_POF "luaopen_" - -/* separator for open functions in C libraries */ -#define LUA_OFSEP "_" - - -/* -** key for table in the registry that keeps handles -** for all loaded C libraries -*/ -static const char *const CLIBS = "_CLIBS"; - -#define LIB_FAIL "open" - - -#define setprogdir(L) ((void)0) - - -/* -** Special type equivalent to '(void*)' for functions in gcc -** (to suppress warnings when converting function pointers) -*/ -typedef void (*voidf)(void); - - -/* -** system-dependent functions -*/ - -/* -** unload library 'lib' -*/ -static void lsys_unloadlib (void *lib); - -/* -** load C library in file 'path'. If 'seeglb', load with all names in -** the library global. -** Returns the library; in case of error, returns NULL plus an -** error string in the stack. -*/ -static void *lsys_load (lua_State *L, const char *path, int seeglb); - -/* -** Try to find a function named 'sym' in library 'lib'. -** Returns the function; in case of error, returns NULL plus an -** error string in the stack. -*/ -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); - - - - -#if defined(LUA_USE_DLOPEN) /* { */ -/* -** {======================================================================== -** This is an implementation of loadlib based on the dlfcn interface. -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -** as an emulation layer on top of native functions. -** ========================================================================= -*/ - -#include - -/* -** Macro to convert pointer-to-void* to pointer-to-function. This cast -** is undefined according to ISO C, but POSIX assumes that it works. -** (The '__extension__' in gnu compilers is only to avoid warnings.) -*/ -#if defined(__GNUC__) -#define cast_func(p) (__extension__ (lua_CFunction)(p)) -#else -#define cast_func(p) ((lua_CFunction)(p)) -#endif - - -static void lsys_unloadlib (void *lib) { - dlclose(lib); -} - - -static void *lsys_load (lua_State *L, const char *path, int seeglb) { - void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); - if (l_unlikely(lib == NULL)) - lua_pushstring(L, dlerror()); - return lib; -} - - -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = cast_func(dlsym(lib, sym)); - if (l_unlikely(f == NULL)) - lua_pushstring(L, dlerror()); - return f; -} - -/* }====================================================== */ - - - -#elif defined(LUA_DL_DLL) /* }{ */ -/* -** {====================================================================== -** This is an implementation of loadlib for Windows using native functions. -** ======================================================================= -*/ - -#include - - -/* -** optional flags for LoadLibraryEx -*/ -#if !defined(LUA_LLE_FLAGS) -#define LUA_LLE_FLAGS 0 -#endif - - -#undef setprogdir - - -/* -** Replace in the path (on the top of the stack) any occurrence -** of LUA_EXEC_DIR with the executable's path. -*/ -static void setprogdir (lua_State *L) { - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff)/sizeof(char); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) - luaL_error(L, "unable to get ModuleFileName"); - else { - *lb = '\0'; /* cut name on the last '\\' to get the path */ - luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - - - - -static void pusherror (lua_State *L) { - int error = GetLastError(); - char buffer[128]; - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL)) - lua_pushstring(L, buffer); - else - lua_pushfstring(L, "system error %d\n", error); -} - -static void lsys_unloadlib (void *lib) { - FreeLibrary((HMODULE)lib); -} - - -static void *lsys_load (lua_State *L, const char *path, int seeglb) { - HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS); - (void)(seeglb); /* not used: symbols are 'global' by default */ - if (lib == NULL) pusherror(L); - return lib; -} - - -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym); - if (f == NULL) pusherror(L); - return f; -} - -/* }====================================================== */ - - -#else /* }{ */ -/* -** {====================================================== -** Fallback for other systems -** ======================================================= -*/ - -#undef LIB_FAIL -#define LIB_FAIL "absent" - - -#define DLMSG "dynamic libraries not enabled; check your Lua installation" - - -static void lsys_unloadlib (void *lib) { - (void)(lib); /* not used */ -} - - -static void *lsys_load (lua_State *L, const char *path, int seeglb) { - (void)(path); (void)(seeglb); /* not used */ - lua_pushliteral(L, DLMSG); - return NULL; -} - - -static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - (void)(lib); (void)(sym); /* not used */ - lua_pushliteral(L, DLMSG); - return NULL; -} - -/* }====================================================== */ -#endif /* } */ - - -/* -** {================================================================== -** Set Paths -** =================================================================== -*/ - -/* -** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment -** variables that Lua check to set its paths. -*/ -#if !defined(LUA_PATH_VAR) -#define LUA_PATH_VAR "LUA_PATH" -#endif - -#if !defined(LUA_CPATH_VAR) -#define LUA_CPATH_VAR "LUA_CPATH" -#endif - - - -/* -** return registry.LUA_NOENV as a boolean -*/ -static int noenv (lua_State *L) { - int b; - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - b = lua_toboolean(L, -1); - lua_pop(L, 1); /* remove value */ - return b; -} - - -/* -** Set a path -*/ -static void setpath (lua_State *L, const char *fieldname, - const char *envname, - const char *dft) { - const char *dftmark; - const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); - const char *path = getenv(nver); /* try versioned name */ - if (path == NULL) /* no versioned environment variable? */ - path = getenv(envname); /* try unversioned name */ - if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, dft); /* use default */ - else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) - lua_pushstring(L, path); /* nothing to change */ - else { /* path contains a ";;": insert default path in its place */ - size_t len = strlen(path); - luaL_Buffer b; - luaL_buffinit(L, &b); - if (path < dftmark) { /* is there a prefix before ';;'? */ - luaL_addlstring(&b, path, dftmark - path); /* add it */ - luaL_addchar(&b, *LUA_PATH_SEP); - } - luaL_addstring(&b, dft); /* add default */ - if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ - luaL_addchar(&b, *LUA_PATH_SEP); - luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark); - } - luaL_pushresult(&b); - } - setprogdir(L); - lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ - lua_pop(L, 1); /* pop versioned variable name ('nver') */ -} - -/* }================================================================== */ - - -/* -** return registry.CLIBS[path] -*/ -static void *checkclib (lua_State *L, const char *path) { - void *plib; - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); - lua_getfield(L, -1, path); - plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ - lua_pop(L, 2); /* pop CLIBS table and 'plib' */ - return plib; -} - - -/* -** registry.CLIBS[path] = plib -- for queries -** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries -*/ -static void addtoclib (lua_State *L, const char *path, void *plib) { - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); - lua_pushlightuserdata(L, plib); - lua_pushvalue(L, -1); - lua_setfield(L, -3, path); /* CLIBS[path] = plib */ - lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ - lua_pop(L, 1); /* pop CLIBS table */ -} - - -/* -** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib -** handles in list CLIBS -*/ -static int gctm (lua_State *L) { - lua_Integer n = luaL_len(L, 1); - for (; n >= 1; n--) { /* for each handle, in reverse order */ - lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - lsys_unloadlib(lua_touserdata(L, -1)); - lua_pop(L, 1); /* pop handle */ - } - return 0; -} - - - -/* error codes for 'lookforfunc' */ -#define ERRLIB 1 -#define ERRFUNC 2 - -/* -** Look for a C function named 'sym' in a dynamically loaded library -** 'path'. -** First, check whether the library is already loaded; if not, try -** to load it. -** Then, if 'sym' is '*', return true (as library has been loaded). -** Otherwise, look for symbol 'sym' in the library and push a -** C function with that symbol. -** Return 0 and 'true' or a function in the stack; in case of -** errors, return an error code and an error message in the stack. -*/ -static int lookforfunc (lua_State *L, const char *path, const char *sym) { - void *reg = checkclib(L, path); /* check loaded C libraries */ - if (reg == NULL) { /* must load library? */ - reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ - if (reg == NULL) return ERRLIB; /* unable to load library */ - addtoclib(L, path, reg); - } - if (*sym == '*') { /* loading only library (no function)? */ - lua_pushboolean(L, 1); /* return 'true' */ - return 0; /* no errors */ - } - else { - lua_CFunction f = lsys_sym(L, reg, sym); - if (f == NULL) - return ERRFUNC; /* unable to find function */ - lua_pushcfunction(L, f); /* else create new function */ - return 0; /* no errors */ - } -} - - -static int ll_loadlib (lua_State *L) { - const char *path = luaL_checkstring(L, 1); - const char *init = luaL_checkstring(L, 2); - int stat = lookforfunc(L, path, init); - if (l_likely(stat == 0)) /* no errors? */ - return 1; /* return the loaded function */ - else { /* error; error message is on stack top */ - luaL_pushfail(L); - lua_insert(L, -2); - lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return fail, error message, and where */ - } -} - - - -/* -** {====================================================== -** 'require' function -** ======================================================= -*/ - - -static int readable (const char *filename) { - FILE *f = fopen(filename, "r"); /* try to open file */ - if (f == NULL) return 0; /* open failed */ - fclose(f); - return 1; -} - - -/* -** Get the next name in '*path' = 'name1;name2;name3;...', changing -** the ending ';' to '\0' to create a zero-terminated string. Return -** NULL when list ends. -*/ -static const char *getnextfilename (char **path, char *end) { - char *sep; - char *name = *path; - if (name == end) - return NULL; /* no more names */ - else if (*name == '\0') { /* from previous iteration? */ - *name = *LUA_PATH_SEP; /* restore separator */ - name++; /* skip it */ - } - sep = strchr(name, *LUA_PATH_SEP); /* find next separator */ - if (sep == NULL) /* separator not found? */ - sep = end; /* name goes until the end */ - *sep = '\0'; /* finish file name */ - *path = sep; /* will start next search from here */ - return name; -} - - -/* -** Given a path such as ";blabla.so;blublu.so", pushes the string -** -** no file 'blabla.so' -** no file 'blublu.so' -*/ -static void pusherrornotfound (lua_State *L, const char *path) { - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addstring(&b, "no file '"); - luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); - luaL_addstring(&b, "'"); - luaL_pushresult(&b); -} - - -static const char *searchpath (lua_State *L, const char *name, - const char *path, - const char *sep, - const char *dirsep) { - luaL_Buffer buff; - char *pathname; /* path with name inserted */ - char *endpathname; /* its end */ - const char *filename; - /* separator is non-empty and appears in 'name'? */ - if (*sep != '\0' && strchr(name, *sep) != NULL) - name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - luaL_buffinit(L, &buff); - /* add path to the buffer, replacing marks ('?') with the file name */ - luaL_addgsub(&buff, path, LUA_PATH_MARK, name); - luaL_addchar(&buff, '\0'); - pathname = luaL_buffaddr(&buff); /* writable list of file names */ - endpathname = pathname + luaL_bufflen(&buff) - 1; - while ((filename = getnextfilename(&pathname, endpathname)) != NULL) { - if (readable(filename)) /* does file exist and is readable? */ - return lua_pushstring(L, filename); /* save and return name */ - } - luaL_pushresult(&buff); /* push path to create error message */ - pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ - return NULL; /* not found */ -} - - -static int ll_searchpath (lua_State *L) { - const char *f = searchpath(L, luaL_checkstring(L, 1), - luaL_checkstring(L, 2), - luaL_optstring(L, 3, "."), - luaL_optstring(L, 4, LUA_DIRSEP)); - if (f != NULL) return 1; - else { /* error message is on top of the stack */ - luaL_pushfail(L); - lua_insert(L, -2); - return 2; /* return fail + error message */ - } -} - - -static const char *findfile (lua_State *L, const char *name, - const char *pname, - const char *dirsep) { - const char *path; - lua_getfield(L, lua_upvalueindex(1), pname); - path = lua_tostring(L, -1); - if (l_unlikely(path == NULL)) - luaL_error(L, "'package.%s' must be a string", pname); - return searchpath(L, name, path, ".", dirsep); -} - - -static int checkload (lua_State *L, int stat, const char *filename) { - if (l_likely(stat)) { /* module loaded successfully? */ - lua_pushstring(L, filename); /* will be 2nd argument to module */ - return 2; /* return open function and file name */ - } - else - return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", - lua_tostring(L, 1), filename, lua_tostring(L, -1)); -} - - -static int searcher_Lua (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - filename = findfile(L, name, "path", LUA_LSUBSEP); - if (filename == NULL) return 1; /* module not found in this path */ - return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); -} - - -/* -** Try to find a load function for module 'modname' at file 'filename'. -** First, change '.' to '_' in 'modname'; then, if 'modname' has -** the form X-Y (that is, it has an "ignore mark"), build a function -** name "luaopen_X" and look for it. (For compatibility, if that -** fails, it also tries "luaopen_Y".) If there is no ignore mark, -** look for a function named "luaopen_modname". -*/ -static int loadfunc (lua_State *L, const char *filename, const char *modname) { - const char *openfunc; - const char *mark; - modname = luaL_gsub(L, modname, ".", LUA_OFSEP); - mark = strchr(modname, *LUA_IGMARK); - if (mark) { - int stat; - openfunc = lua_pushlstring(L, modname, mark - modname); - openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); - stat = lookforfunc(L, filename, openfunc); - if (stat != ERRFUNC) return stat; - modname = mark + 1; /* else go ahead and try old-style name */ - } - openfunc = lua_pushfstring(L, LUA_POF"%s", modname); - return lookforfunc(L, filename, openfunc); -} - - -static int searcher_C (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); - if (filename == NULL) return 1; /* module not found in this path */ - return checkload(L, (loadfunc(L, filename, name) == 0), filename); -} - - -static int searcher_Croot (lua_State *L) { - const char *filename; - const char *name = luaL_checkstring(L, 1); - const char *p = strchr(name, '.'); - int stat; - if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, p - name); - filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); - if (filename == NULL) return 1; /* root not found */ - if ((stat = loadfunc(L, filename, name)) != 0) { - if (stat != ERRFUNC) - return checkload(L, 0, filename); /* real error */ - else { /* open function not found */ - lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); - return 1; - } - } - lua_pushstring(L, filename); /* will be 2nd argument to module */ - return 2; -} - - -static int searcher_preload (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ - lua_pushfstring(L, "no field package.preload['%s']", name); - return 1; - } - else { - lua_pushliteral(L, ":preload:"); - return 2; - } -} - - -static void findloader (lua_State *L, const char *name) { - int i; - luaL_Buffer msg; /* to build error message */ - /* push 'package.searchers' to index 3 in the stack */ - if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") - != LUA_TTABLE)) - luaL_error(L, "'package.searchers' must be a table"); - luaL_buffinit(L, &msg); - /* iterate over available searchers to find a loader */ - for (i = 1; ; i++) { - luaL_addstring(&msg, "\n\t"); /* error-message prefix */ - if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ - lua_pop(L, 1); /* remove nil */ - luaL_buffsub(&msg, 2); /* remove prefix */ - luaL_pushresult(&msg); /* create error message */ - luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); - } - lua_pushstring(L, name); - lua_call(L, 1, 2); /* call it */ - if (lua_isfunction(L, -2)) /* did it find a loader? */ - return; /* module loader found */ - else if (lua_isstring(L, -2)) { /* searcher returned error message? */ - lua_pop(L, 1); /* remove extra return */ - luaL_addvalue(&msg); /* concatenate error message */ - } - else { /* no error message */ - lua_pop(L, 2); /* remove both returns */ - luaL_buffsub(&msg, 2); /* remove prefix */ - } - } -} - - -static int ll_require (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_settop(L, 1); /* LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - lua_getfield(L, 2, name); /* LOADED[name] */ - if (lua_toboolean(L, -1)) /* is it there? */ - return 1; /* package is already loaded */ - /* else must load package */ - lua_pop(L, 1); /* remove 'getfield' result */ - findloader(L, name); - lua_rotate(L, -2, 1); /* function <-> loader data */ - lua_pushvalue(L, 1); /* name is 1st argument to module loader */ - lua_pushvalue(L, -3); /* loader data is 2nd argument */ - /* stack: ...; loader data; loader function; mod. name; loader data */ - lua_call(L, 2, 1); /* run loader to load module */ - /* stack: ...; loader data; result from loader */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* LOADED[name] = returned value */ - else - lua_pop(L, 1); /* pop nil */ - if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ - lua_pushboolean(L, 1); /* use true as result */ - lua_copy(L, -1, -2); /* replace loader result */ - lua_setfield(L, 2, name); /* LOADED[name] = true */ - } - lua_rotate(L, -2, 1); /* loader data <-> module result */ - return 2; /* return module result and loader data */ -} - -/* }====================================================== */ - - - - -static const luaL_Reg pk_funcs[] = { - {"loadlib", ll_loadlib}, - {"searchpath", ll_searchpath}, - /* placeholders */ - {"preload", NULL}, - {"cpath", NULL}, - {"path", NULL}, - {"searchers", NULL}, - {"loaded", NULL}, - {NULL, NULL} -}; - - -static const luaL_Reg ll_funcs[] = { - {"require", ll_require}, - {NULL, NULL} -}; - - -static void createsearcherstable (lua_State *L) { - static const lua_CFunction searchers[] = - {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; - int i; - /* create 'searchers' table */ - lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); - /* fill it with predefined searchers */ - for (i=0; searchers[i] != NULL; i++) { - lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ - lua_pushcclosure(L, searchers[i], 1); - lua_rawseti(L, -2, i+1); - } - lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ -} - - -/* -** create table CLIBS to keep track of loaded C libraries, -** setting a finalizer to close all libraries when closing state. -*/ -static void createclibstable (lua_State *L) { - luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ - lua_createtable(L, 0, 1); /* create metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); -} - - -LUAMOD_API int luaopen_package (lua_State *L) { - createclibstable(L); - luaL_newlib(L, pk_funcs); /* create 'package' table */ - createsearcherstable(L); - /* set paths */ - setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); - setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); - /* store config information */ - lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" - LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); - lua_setfield(L, -2, "config"); - /* set field 'loaded' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); - lua_setfield(L, -2, "loaded"); - /* set field 'preload' */ - luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - lua_setfield(L, -2, "preload"); - lua_pushglobaltable(L); - lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ - luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ - lua_pop(L, 1); /* pop global table */ - return 1; /* return 'package' table */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.c deleted file mode 100644 index 301aa90..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.c +++ /dev/null @@ -1,592 +0,0 @@ -/* -** $Id: lobject.c $ -** Some generic functions over Lua objects -** See Copyright Notice in lua.h -*/ - -#define lobject_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lctype.h" -#include "ldebug.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "lvm.h" - - -/* -** Computes ceil(log2(x)) -*/ -int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 - }; - int l = 0; - x--; - while (x >= 256) { l += 8; x >>= 8; } - return l + log_2[x]; -} - - -static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, - lua_Integer v2) { - switch (op) { - case LUA_OPADD: return intop(+, v1, v2); - case LUA_OPSUB:return intop(-, v1, v2); - case LUA_OPMUL:return intop(*, v1, v2); - case LUA_OPMOD: return luaV_mod(L, v1, v2); - case LUA_OPIDIV: return luaV_idiv(L, v1, v2); - case LUA_OPBAND: return intop(&, v1, v2); - case LUA_OPBOR: return intop(|, v1, v2); - case LUA_OPBXOR: return intop(^, v1, v2); - case LUA_OPSHL: return luaV_shiftl(v1, v2); - case LUA_OPSHR: return luaV_shiftl(v1, -v2); - case LUA_OPUNM: return intop(-, 0, v1); - case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); - default: lua_assert(0); return 0; - } -} - - -static lua_Number numarith (lua_State *L, int op, lua_Number v1, - lua_Number v2) { - switch (op) { - case LUA_OPADD: return luai_numadd(L, v1, v2); - case LUA_OPSUB: return luai_numsub(L, v1, v2); - case LUA_OPMUL: return luai_nummul(L, v1, v2); - case LUA_OPDIV: return luai_numdiv(L, v1, v2); - case LUA_OPPOW: return luai_numpow(L, v1, v2); - case LUA_OPIDIV: return luai_numidiv(L, v1, v2); - case LUA_OPUNM: return luai_numunm(L, v1); - case LUA_OPMOD: return luaV_modf(L, v1, v2); - default: lua_assert(0); return 0; - } -} - - -int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, - TValue *res) { - switch (op) { - case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: - case LUA_OPSHL: case LUA_OPSHR: - case LUA_OPBNOT: { /* operate only on integers */ - lua_Integer i1; lua_Integer i2; - if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) { - setivalue(res, intarith(L, op, i1, i2)); - return 1; - } - else return 0; /* fail */ - } - case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ - lua_Number n1; lua_Number n2; - if (tonumberns(p1, n1) && tonumberns(p2, n2)) { - setfltvalue(res, numarith(L, op, n1, n2)); - return 1; - } - else return 0; /* fail */ - } - default: { /* other operations */ - lua_Number n1; lua_Number n2; - if (ttisinteger(p1) && ttisinteger(p2)) { - setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); - return 1; - } - else if (tonumberns(p1, n1) && tonumberns(p2, n2)) { - setfltvalue(res, numarith(L, op, n1, n2)); - return 1; - } - else return 0; /* fail */ - } - } -} - - -void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, - StkId res) { - if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { - /* could not perform raw operation; try metamethod */ - luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); - } -} - - -int luaO_hexavalue (int c) { - if (lisdigit(c)) return c - '0'; - else return (ltolower(c) - 'a') + 10; -} - - -static int isneg (const char **s) { - if (**s == '-') { (*s)++; return 1; } - else if (**s == '+') (*s)++; - return 0; -} - - - -/* -** {================================================================== -** Lua's implementation for 'lua_strx2number' -** =================================================================== -*/ - -#if !defined(lua_strx2number) - -/* maximum number of significant digits to read (to avoid overflows - even with single floats) */ -#define MAXSIGDIG 30 - -/* -** convert a hexadecimal numeric string to a number, following -** C99 specification for 'strtod' -*/ -static lua_Number lua_strx2number (const char *s, char **endptr) { - int dot = lua_getlocaledecpoint(); - lua_Number r = l_mathop(0.0); /* result (accumulator) */ - int sigdig = 0; /* number of significant digits */ - int nosigdig = 0; /* number of non-significant digits */ - int e = 0; /* exponent correction */ - int neg; /* 1 if number is negative */ - int hasdot = 0; /* true after seen a dot */ - *endptr = cast_charp(s); /* nothing is valid yet */ - while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); /* check sign */ - if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ - return l_mathop(0.0); /* invalid format (no '0x') */ - for (s += 2; ; s++) { /* skip '0x' and read numeral */ - if (*s == dot) { - if (hasdot) break; /* second dot? stop loop */ - else hasdot = 1; - } - else if (lisxdigit(cast_uchar(*s))) { - if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ - nosigdig++; - else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ - r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); - else e++; /* too many digits; ignore, but still count for exponent */ - if (hasdot) e--; /* decimal digit? correct exponent */ - } - else break; /* neither a dot nor a digit */ - } - if (nosigdig + sigdig == 0) /* no digits? */ - return l_mathop(0.0); /* invalid format */ - *endptr = cast_charp(s); /* valid up to here */ - e *= 4; /* each digit multiplies/divides value by 2^4 */ - if (*s == 'p' || *s == 'P') { /* exponent part? */ - int exp1 = 0; /* exponent value */ - int neg1; /* exponent sign */ - s++; /* skip 'p' */ - neg1 = isneg(&s); /* sign */ - if (!lisdigit(cast_uchar(*s))) - return l_mathop(0.0); /* invalid; must have at least one digit */ - while (lisdigit(cast_uchar(*s))) /* read exponent */ - exp1 = exp1 * 10 + *(s++) - '0'; - if (neg1) exp1 = -exp1; - e += exp1; - *endptr = cast_charp(s); /* valid up to here */ - } - if (neg) r = -r; - return l_mathop(ldexp)(r, e); -} - -#endif -/* }====================================================== */ - - -/* maximum length of a numeral to be converted to a number */ -#if !defined (L_MAXLENNUM) -#define L_MAXLENNUM 200 -#endif - -/* -** Convert string 's' to a Lua number (put in 'result'). Return NULL on -** fail or the address of the ending '\0' on success. ('mode' == 'x') -** means a hexadecimal numeral. -*/ -static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { - char *endptr; - *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ - : lua_str2number(s, &endptr); - if (endptr == s) return NULL; /* nothing recognized? */ - while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ - return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ -} - - -/* -** Convert string 's' to a Lua number (put in 'result') handling the -** current locale. -** This function accepts both the current locale or a dot as the radix -** mark. If the conversion fails, it may mean number has a dot but -** locale accepts something else. In that case, the code copies 's' -** to a buffer (because 's' is read-only), changes the dot to the -** current locale radix mark, and tries to convert again. -** The variable 'mode' checks for special characters in the string: -** - 'n' means 'inf' or 'nan' (which should be rejected) -** - 'x' means a hexadecimal numeral -** - '.' just optimizes the search for the common case (no special chars) -*/ -static const char *l_str2d (const char *s, lua_Number *result) { - const char *endptr; - const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ - int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; - if (mode == 'n') /* reject 'inf' and 'nan' */ - return NULL; - endptr = l_str2dloc(s, result, mode); /* try to convert */ - if (endptr == NULL) { /* failed? may be a different locale */ - char buff[L_MAXLENNUM + 1]; - const char *pdot = strchr(s, '.'); - if (pdot == NULL || strlen(s) > L_MAXLENNUM) - return NULL; /* string too long or no dot; fail */ - strcpy(buff, s); /* copy string to buffer */ - buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ - endptr = l_str2dloc(buff, result, mode); /* try again */ - if (endptr != NULL) - endptr = s + (endptr - buff); /* make relative to 's' */ - } - return endptr; -} - - -#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10) -#define MAXLASTD cast_int(LUA_MAXINTEGER % 10) - -static const char *l_str2int (const char *s, lua_Integer *result) { - lua_Unsigned a = 0; - int empty = 1; - int neg; - while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); - if (s[0] == '0' && - (s[1] == 'x' || s[1] == 'X')) { /* hex? */ - s += 2; /* skip '0x' */ - for (; lisxdigit(cast_uchar(*s)); s++) { - a = a * 16 + luaO_hexavalue(*s); - empty = 0; - } - } - else { /* decimal */ - for (; lisdigit(cast_uchar(*s)); s++) { - int d = *s - '0'; - if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ - return NULL; /* do not accept it (as integer) */ - a = a * 10 + d; - empty = 0; - } - } - while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ - if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ - else { - *result = l_castU2S((neg) ? 0u - a : a); - return s; - } -} - - -size_t luaO_str2num (const char *s, TValue *o) { - lua_Integer i; lua_Number n; - const char *e; - if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ - setivalue(o, i); - } - else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ - setfltvalue(o, n); - } - else - return 0; /* conversion failed */ - return (e - s) + 1; /* success; return string size */ -} - - -int luaO_utf8esc (char *buff, unsigned long x) { - int n = 1; /* number of bytes put in buffer (backwards) */ - lua_assert(x <= 0x7FFFFFFFu); - if (x < 0x80) /* ascii? */ - buff[UTF8BUFFSZ - 1] = cast_char(x); - else { /* need continuation bytes */ - unsigned int mfb = 0x3f; /* maximum that fits in first byte */ - do { /* add continuation bytes */ - buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f)); - x >>= 6; /* remove added bits */ - mfb >>= 1; /* now there is one less bit available in first byte */ - } while (x > mfb); /* still needs continuation byte? */ - buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */ - } - return n; -} - - -/* -** Maximum length of the conversion of a number to a string. Must be -** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT. -** (For a long long int, this is 19 digits plus a sign and a final '\0', -** adding to 21. For a long double, it can go to a sign, 33 digits, -** the dot, an exponent letter, an exponent sign, 5 exponent digits, -** and a final '\0', adding to 43.) -*/ -#define MAXNUMBER2STR 44 - - -/* -** Convert a number object to a string, adding it to a buffer -*/ -static int tostringbuff (TValue *obj, char *buff) { - int len; - lua_assert(ttisnumber(obj)); - if (ttisinteger(obj)) - len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); - else { - len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj)); - if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ - buff[len++] = lua_getlocaledecpoint(); - buff[len++] = '0'; /* adds '.0' to result */ - } - } - return len; -} - - -/* -** Convert a number object to a Lua string, replacing the value at 'obj' -*/ -void luaO_tostring (lua_State *L, TValue *obj) { - char buff[MAXNUMBER2STR]; - int len = tostringbuff(obj, buff); - setsvalue(L, obj, luaS_newlstr(L, buff, len)); -} - - - - -/* -** {================================================================== -** 'luaO_pushvfstring' -** =================================================================== -*/ - -/* size for buffer space used by 'luaO_pushvfstring' */ -#define BUFVFS 200 - -/* buffer used by 'luaO_pushvfstring' */ -typedef struct BuffFS { - lua_State *L; - int pushed; /* number of string pieces already on the stack */ - int blen; /* length of partial string in 'space' */ - char space[BUFVFS]; /* holds last part of the result */ -} BuffFS; - - -/* -** Push given string to the stack, as part of the buffer, and -** join the partial strings in the stack into one. -*/ -static void pushstr (BuffFS *buff, const char *str, size_t l) { - lua_State *L = buff->L; - setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - L->top++; /* may use one extra slot */ - buff->pushed++; - luaV_concat(L, buff->pushed); /* join partial results into one */ - buff->pushed = 1; -} - - -/* -** empty the buffer space into the stack -*/ -static void clearbuff (BuffFS *buff) { - pushstr(buff, buff->space, buff->blen); /* push buffer contents */ - buff->blen = 0; /* space now is empty */ -} - - -/* -** Get a space of size 'sz' in the buffer. If buffer has not enough -** space, empty it. 'sz' must fit in an empty buffer. -*/ -static char *getbuff (BuffFS *buff, int sz) { - lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); - if (sz > BUFVFS - buff->blen) /* not enough space? */ - clearbuff(buff); - return buff->space + buff->blen; -} - - -#define addsize(b,sz) ((b)->blen += (sz)) - - -/* -** Add 'str' to the buffer. If string is larger than the buffer space, -** push the string directly to the stack. -*/ -static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { - if (slen <= BUFVFS) { /* does string fit into buffer? */ - char *bf = getbuff(buff, cast_int(slen)); - memcpy(bf, str, slen); /* add string to buffer */ - addsize(buff, cast_int(slen)); - } - else { /* string larger than buffer */ - clearbuff(buff); /* string comes after buffer's content */ - pushstr(buff, str, slen); /* push string */ - } -} - - -/* -** Add a number to the buffer. -*/ -static void addnum2buff (BuffFS *buff, TValue *num) { - char *numbuff = getbuff(buff, MAXNUMBER2STR); - int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ - addsize(buff, len); -} - - -/* -** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' - conventional formats, plus Lua-specific '%I' and '%U' -*/ -const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - BuffFS buff; /* holds last part of the result */ - const char *e; /* points to next '%' */ - buff.pushed = buff.blen = 0; - buff.L = L; - while ((e = strchr(fmt, '%')) != NULL) { - addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ - switch (*(e + 1)) { /* conversion specifier */ - case 's': { /* zero-terminated string */ - const char *s = va_arg(argp, char *); - if (s == NULL) s = "(null)"; - addstr2buff(&buff, s, strlen(s)); - break; - } - case 'c': { /* an 'int' as a character */ - char c = cast_uchar(va_arg(argp, int)); - addstr2buff(&buff, &c, sizeof(char)); - break; - } - case 'd': { /* an 'int' */ - TValue num; - setivalue(&num, va_arg(argp, int)); - addnum2buff(&buff, &num); - break; - } - case 'I': { /* a 'lua_Integer' */ - TValue num; - setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); - addnum2buff(&buff, &num); - break; - } - case 'f': { /* a 'lua_Number' */ - TValue num; - setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); - addnum2buff(&buff, &num); - break; - } - case 'p': { /* a pointer */ - const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ - char *bf = getbuff(&buff, sz); - void *p = va_arg(argp, void *); - int len = lua_pointer2str(bf, sz, p); - addsize(&buff, len); - break; - } - case 'U': { /* a 'long' as a UTF-8 sequence */ - char bf[UTF8BUFFSZ]; - int len = luaO_utf8esc(bf, va_arg(argp, long)); - addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); - break; - } - case '%': { - addstr2buff(&buff, "%", 1); - break; - } - default: { - luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", - *(e + 1)); - } - } - fmt = e + 2; /* skip '%' and the specifier */ - } - addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ - clearbuff(&buff); /* empty buffer into the stack */ - lua_assert(buff.pushed == 1); - return svalue(s2v(L->top - 1)); -} - - -const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); - va_end(argp); - return msg; -} - -/* }================================================================== */ - - -#define RETS "..." -#define PRE "[string \"" -#define POS "\"]" - -#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) - -void luaO_chunkid (char *out, const char *source, size_t srclen) { - size_t bufflen = LUA_IDSIZE; /* free space in buffer */ - if (*source == '=') { /* 'literal' source */ - if (srclen <= bufflen) /* small enough? */ - memcpy(out, source + 1, srclen * sizeof(char)); - else { /* truncate it */ - addstr(out, source + 1, bufflen - 1); - *out = '\0'; - } - } - else if (*source == '@') { /* file name */ - if (srclen <= bufflen) /* small enough? */ - memcpy(out, source + 1, srclen * sizeof(char)); - else { /* add '...' before rest of name */ - addstr(out, RETS, LL(RETS)); - bufflen -= LL(RETS); - memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); - } - } - else { /* string; format as [string "source"] */ - const char *nl = strchr(source, '\n'); /* find first new line (if any) */ - addstr(out, PRE, LL(PRE)); /* add prefix */ - bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ - if (srclen < bufflen && nl == NULL) { /* small one-line source? */ - addstr(out, source, srclen); /* keep it */ - } - else { - if (nl != NULL) srclen = nl - source; /* stop at first newline */ - if (srclen > bufflen) srclen = bufflen; - addstr(out, source, srclen); - addstr(out, RETS, LL(RETS)); - } - memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); - } -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.h deleted file mode 100644 index 0e05b3e..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lobject.h +++ /dev/null @@ -1,800 +0,0 @@ -/* -** $Id: lobject.h $ -** Type definitions for Lua objects -** See Copyright Notice in lua.h -*/ - - -#ifndef lobject_h -#define lobject_h - - -#include - - -#include "llimits.h" -#include "lua.h" - - -/* -** Extra types for collectable non-values -*/ -#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ -#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ -#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ - - - -/* -** number of all possible types (including LUA_TNONE but excluding DEADKEY) -*/ -#define LUA_TOTALTYPES (LUA_TPROTO + 2) - - -/* -** tags for Tagged Values have the following use of bits: -** bits 0-3: actual tag (a LUA_T* constant) -** bits 4-5: variant bits -** bit 6: whether value is collectable -*/ - -/* add variant bits to a type */ -#define makevariant(t,v) ((t) | ((v) << 4)) - - - -/* -** Union of all Lua values -*/ -typedef union Value { - struct GCObject *gc; /* collectable objects */ - void *p; /* light userdata */ - lua_CFunction f; /* light C functions */ - lua_Integer i; /* integer numbers */ - lua_Number n; /* float numbers */ -} Value; - - -/* -** Tagged Values. This is the basic representation of values in Lua: -** an actual value plus a tag with its type. -*/ - -#define TValuefields Value value_; lu_byte tt_ - -typedef struct TValue { - TValuefields; -} TValue; - - -#define val_(o) ((o)->value_) -#define valraw(o) (val_(o)) - - -/* raw type tag of a TValue */ -#define rawtt(o) ((o)->tt_) - -/* tag with no variants (bits 0-3) */ -#define novariant(t) ((t) & 0x0F) - -/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ -#define withvariant(t) ((t) & 0x3F) -#define ttypetag(o) withvariant(rawtt(o)) - -/* type of a TValue */ -#define ttype(o) (novariant(rawtt(o))) - - -/* Macros to test type */ -#define checktag(o,t) (rawtt(o) == (t)) -#define checktype(o,t) (ttype(o) == (t)) - - -/* Macros for internal tests */ - -/* collectable object has the same tag as the original value */ -#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) - -/* -** Any value being manipulated by the program either is non -** collectable, or the collectable object has the right tag -** and it is not dead. The option 'L == NULL' allows other -** macros using this one to be used where L is not available. -*/ -#define checkliveness(L,obj) \ - ((void)L, lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) - - -/* Macros to set values */ - -/* set a value's tag */ -#define settt_(o,t) ((o)->tt_=(t)) - - -/* main macro to copy values (from 'obj2' to 'obj1') */ -#define setobj(L,obj1,obj2) \ - { TValue *io1=(obj1); const TValue *io2=(obj2); \ - io1->value_ = io2->value_; settt_(io1, io2->tt_); \ - checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } - -/* -** Different types of assignments, according to source and destination. -** (They are mostly equal now, but may be different in the future.) -*/ - -/* from stack to stack */ -#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) -/* to stack (not from same stack) */ -#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) -/* from table to same table */ -#define setobjt2t setobj -/* to new object */ -#define setobj2n setobj -/* to table */ -#define setobj2t setobj - - -/* -** Entries in a Lua stack. Field 'tbclist' forms a list of all -** to-be-closed variables active in this stack. Dummy entries are -** used when the distance between two tbc variables does not fit -** in an unsigned short. They are represented by delta==0, and -** their real delta is always the maximum value that fits in -** that field. -*/ -typedef union StackValue { - TValue val; - struct { - TValuefields; - unsigned short delta; - } tbclist; -} StackValue; - - -/* index to stack elements */ -typedef StackValue *StkId; - -/* convert a 'StackValue' to a 'TValue' */ -#define s2v(o) (&(o)->val) - - - -/* -** {================================================================== -** Nil -** =================================================================== -*/ - -/* Standard nil */ -#define LUA_VNIL makevariant(LUA_TNIL, 0) - -/* Empty slot (which might be different from a slot containing nil) */ -#define LUA_VEMPTY makevariant(LUA_TNIL, 1) - -/* Value returned for a key not found in a table (absent key) */ -#define LUA_VABSTKEY makevariant(LUA_TNIL, 2) - - -/* macro to test for (any kind of) nil */ -#define ttisnil(v) checktype((v), LUA_TNIL) - - -/* macro to test for a standard nil */ -#define ttisstrictnil(o) checktag((o), LUA_VNIL) - - -#define setnilvalue(obj) settt_(obj, LUA_VNIL) - - -#define isabstkey(v) checktag((v), LUA_VABSTKEY) - - -/* -** macro to detect non-standard nils (used only in assertions) -*/ -#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) - - -/* -** By default, entries with any kind of nil are considered empty. -** (In any definition, values associated with absent keys must also -** be accepted as empty.) -*/ -#define isempty(v) ttisnil(v) - - -/* macro defining a value corresponding to an absent key */ -#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY - - -/* mark an entry as empty */ -#define setempty(v) settt_(v, LUA_VEMPTY) - - - -/* }================================================================== */ - - -/* -** {================================================================== -** Booleans -** =================================================================== -*/ - - -#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) -#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) - -#define ttisboolean(o) checktype((o), LUA_TBOOLEAN) -#define ttisfalse(o) checktag((o), LUA_VFALSE) -#define ttistrue(o) checktag((o), LUA_VTRUE) - - -#define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) - - -#define setbfvalue(obj) settt_(obj, LUA_VFALSE) -#define setbtvalue(obj) settt_(obj, LUA_VTRUE) - -/* }================================================================== */ - - -/* -** {================================================================== -** Threads -** =================================================================== -*/ - -#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) - -#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) - -#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) - -#define setthvalue(L,obj,x) \ - { TValue *io = (obj); lua_State *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ - checkliveness(L,io); } - -#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) - -/* }================================================================== */ - - -/* -** {================================================================== -** Collectable Objects -** =================================================================== -*/ - -/* -** Common Header for all collectable objects (in macro form, to be -** included in other objects) -*/ -#define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked - - -/* Common type for all collectable objects */ -typedef struct GCObject { - CommonHeader; -} GCObject; - - -/* Bit mark for collectable types */ -#define BIT_ISCOLLECTABLE (1 << 6) - -#define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) - -/* mark a tag as collectable */ -#define ctb(t) ((t) | BIT_ISCOLLECTABLE) - -#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) - -#define gcvalueraw(v) ((v).gc) - -#define setgcovalue(L,obj,x) \ - { TValue *io = (obj); GCObject *i_g=(x); \ - val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } - -/* }================================================================== */ - - -/* -** {================================================================== -** Numbers -** =================================================================== -*/ - -/* Variant tags for numbers */ -#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ -#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ - -#define ttisnumber(o) checktype((o), LUA_TNUMBER) -#define ttisfloat(o) checktag((o), LUA_VNUMFLT) -#define ttisinteger(o) checktag((o), LUA_VNUMINT) - -#define nvalue(o) check_exp(ttisnumber(o), \ - (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) -#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) -#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) - -#define fltvalueraw(v) ((v).n) -#define ivalueraw(v) ((v).i) - -#define setfltvalue(obj,x) \ - { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } - -#define chgfltvalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } - -#define setivalue(obj,x) \ - { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } - -#define chgivalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } - -/* }================================================================== */ - - -/* -** {================================================================== -** Strings -** =================================================================== -*/ - -/* Variant tags for strings */ -#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ -#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ - -#define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) - -#define tsvalueraw(v) (gco2ts((v).gc)) - -#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) - -#define setsvalue(L,obj,x) \ - { TValue *io = (obj); TString *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ - checkliveness(L,io); } - -/* set a string to the stack */ -#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) - -/* set a string to a new object */ -#define setsvalue2n setsvalue - - -/* -** Header for a string value. -*/ -typedef struct TString { - CommonHeader; - lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - lu_byte shrlen; /* length for short strings */ - unsigned int hash; - union { - size_t lnglen; /* length for long strings */ - struct TString *hnext; /* linked list for hash table */ - } u; - char contents[1]; -} TString; - - - -/* -** Get the actual string (array of bytes) from a 'TString'. -*/ -#define getstr(ts) ((ts)->contents) - - -/* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(tsvalue(o)) - -/* get string length from 'TString *s' */ -#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) - -/* get string length from 'TValue *o' */ -#define vslen(o) tsslen(tsvalue(o)) - -/* }================================================================== */ - - -/* -** {================================================================== -** Userdata -** =================================================================== -*/ - - -/* -** Light userdata should be a variant of userdata, but for compatibility -** reasons they are also different types. -*/ -#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) - -#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) - -#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) -#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) - -#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) -#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) - -#define pvalueraw(v) ((v).p) - -#define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } - -#define setuvalue(L,obj,x) \ - { TValue *io = (obj); Udata *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ - checkliveness(L,io); } - - -/* Ensures that addresses after this type are always fully aligned. */ -typedef union UValue { - TValue uv; - LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ -} UValue; - - -/* -** Header for userdata with user values; -** memory area follows the end of this structure. -*/ -typedef struct Udata { - CommonHeader; - unsigned short nuvalue; /* number of user values */ - size_t len; /* number of bytes */ - struct Table *metatable; - GCObject *gclist; - UValue uv[1]; /* user values */ -} Udata; - - -/* -** Header for userdata with no user values. These userdata do not need -** to be gray during GC, and therefore do not need a 'gclist' field. -** To simplify, the code always use 'Udata' for both kinds of userdata, -** making sure it never accesses 'gclist' on userdata with no user values. -** This structure here is used only to compute the correct size for -** this representation. (The 'bindata' field in its end ensures correct -** alignment for binary data following this header.) -*/ -typedef struct Udata0 { - CommonHeader; - unsigned short nuvalue; /* number of user values */ - size_t len; /* number of bytes */ - struct Table *metatable; - union {LUAI_MAXALIGN;} bindata; -} Udata0; - - -/* compute the offset of the memory area of a userdata */ -#define udatamemoffset(nuv) \ - ((nuv) == 0 ? offsetof(Udata0, bindata) \ - : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) - -/* get the address of the memory block inside 'Udata' */ -#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) - -/* compute the size of a userdata */ -#define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) - -/* }================================================================== */ - - -/* -** {================================================================== -** Prototypes -** =================================================================== -*/ - -#define LUA_VPROTO makevariant(LUA_TPROTO, 0) - - -/* -** Description of an upvalue for function prototypes -*/ -typedef struct Upvaldesc { - TString *name; /* upvalue name (for debug information) */ - lu_byte instack; /* whether it is in stack (register) */ - lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ - lu_byte kind; /* kind of corresponding variable */ -} Upvaldesc; - - -/* -** Description of a local variable for function prototypes -** (used for debug information) -*/ -typedef struct LocVar { - TString *varname; - int startpc; /* first point where variable is active */ - int endpc; /* first point where variable is dead */ -} LocVar; - - -/* -** Associates the absolute line source for a given instruction ('pc'). -** The array 'lineinfo' gives, for each instruction, the difference in -** lines from the previous instruction. When that difference does not -** fit into a byte, Lua saves the absolute line for that instruction. -** (Lua also saves the absolute line periodically, to speed up the -** computation of a line number: we can use binary search in the -** absolute-line array, but we must traverse the 'lineinfo' array -** linearly to compute a line.) -*/ -typedef struct AbsLineInfo { - int pc; - int line; -} AbsLineInfo; - -/* -** Function Prototypes -*/ -typedef struct Proto { - CommonHeader; - lu_byte numparams; /* number of fixed (named) parameters */ - lu_byte is_vararg; - lu_byte maxstacksize; /* number of registers needed by this function */ - int sizeupvalues; /* size of 'upvalues' */ - int sizek; /* size of 'k' */ - int sizecode; - int sizelineinfo; - int sizep; /* size of 'p' */ - int sizelocvars; - int sizeabslineinfo; /* size of 'abslineinfo' */ - int linedefined; /* debug information */ - int lastlinedefined; /* debug information */ - TValue *k; /* constants used by the function */ - Instruction *code; /* opcodes */ - struct Proto **p; /* functions defined inside the function */ - Upvaldesc *upvalues; /* upvalue information */ - ls_byte *lineinfo; /* information about source lines (debug information) */ - AbsLineInfo *abslineinfo; /* idem */ - LocVar *locvars; /* information about local variables (debug information) */ - TString *source; /* used for debug information */ - GCObject *gclist; -} Proto; - -/* }================================================================== */ - - -/* -** {================================================================== -** Functions -** =================================================================== -*/ - -#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) - - -/* Variant tags for functions */ -#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ -#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ -#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ - -#define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) -#define ttislcf(o) checktag((o), LUA_VLCF) -#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) -#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) - - -#define isLfunction(o) ttisLclosure(o) - -#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) -#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) -#define fvalue(o) check_exp(ttislcf(o), val_(o).f) -#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) - -#define fvalueraw(v) ((v).f) - -#define setclLvalue(L,obj,x) \ - { TValue *io = (obj); LClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ - checkliveness(L,io); } - -#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) - -#define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } - -#define setclCvalue(L,obj,x) \ - { TValue *io = (obj); CClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ - checkliveness(L,io); } - - -/* -** Upvalues for Lua closures -*/ -typedef struct UpVal { - CommonHeader; - lu_byte tbc; /* true if it represents a to-be-closed variable */ - TValue *v; /* points to stack or to its own value */ - union { - struct { /* (when open) */ - struct UpVal *next; /* linked list */ - struct UpVal **previous; - } open; - TValue value; /* the value (when closed) */ - } u; -} UpVal; - - - -#define ClosureHeader \ - CommonHeader; lu_byte nupvalues; GCObject *gclist - -typedef struct CClosure { - ClosureHeader; - lua_CFunction f; - TValue upvalue[1]; /* list of upvalues */ -} CClosure; - - -typedef struct LClosure { - ClosureHeader; - struct Proto *p; - UpVal *upvals[1]; /* list of upvalues */ -} LClosure; - - -typedef union Closure { - CClosure c; - LClosure l; -} Closure; - - -#define getproto(o) (clLvalue(o)->p) - -/* }================================================================== */ - - -/* -** {================================================================== -** Tables -** =================================================================== -*/ - -#define LUA_VTABLE makevariant(LUA_TTABLE, 0) - -#define ttistable(o) checktag((o), ctb(LUA_VTABLE)) - -#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) - -#define sethvalue(L,obj,x) \ - { TValue *io = (obj); Table *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ - checkliveness(L,io); } - -#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) - - -/* -** Nodes for Hash tables: A pack of two TValue's (key-value pairs) -** plus a 'next' field to link colliding entries. The distribution -** of the key's fields ('key_tt' and 'key_val') not forming a proper -** 'TValue' allows for a smaller size for 'Node' both in 4-byte -** and 8-byte alignments. -*/ -typedef union Node { - struct NodeKey { - TValuefields; /* fields for value */ - lu_byte key_tt; /* key type */ - int next; /* for chaining */ - Value key_val; /* key value */ - } u; - TValue i_val; /* direct access to node's value as a proper 'TValue' */ -} Node; - - -/* copy a value into a key */ -#define setnodekey(L,node,obj) \ - { Node *n_=(node); const TValue *io_=(obj); \ - n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ - checkliveness(L,io_); } - - -/* copy a value from a key */ -#define getnodekey(L,obj,node) \ - { TValue *io_=(obj); const Node *n_=(node); \ - io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ - checkliveness(L,io_); } - - -/* -** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the -** real size of 'array'. Otherwise, the real size of 'array' is the -** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' -** is zero); 'alimit' is then used as a hint for #t. -*/ - -#define BITRAS (1 << 7) -#define isrealasize(t) (!((t)->flags & BITRAS)) -#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) -#define setnorealasize(t) ((t)->flags |= BITRAS) - - -typedef struct Table { - CommonHeader; - lu_byte flags; /* 1<

u.key_tt) -#define keyval(node) ((node)->u.key_val) - -#define keyisnil(node) (keytt(node) == LUA_TNIL) -#define keyisinteger(node) (keytt(node) == LUA_VNUMINT) -#define keyival(node) (keyval(node).i) -#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) -#define keystrval(node) (gco2ts(keyval(node).gc)) - -#define setnilkey(node) (keytt(node) = LUA_TNIL) - -#define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) - -#define gckey(n) (keyval(n).gc) -#define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) - - -/* -** Dead keys in tables have the tag DEADKEY but keep their original -** gcvalue. This distinguishes them from regular keys but allows them to -** be found when searched in a special way. ('next' needs that to find -** keys removed from a table during a traversal.) -*/ -#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) -#define keyisdead(node) (keytt(node) == LUA_TDEADKEY) - -/* }================================================================== */ - - - -/* -** 'module' operation for hashing (size is always a power of 2) -*/ -#define lmod(s,size) \ - (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) - - -#define twoto(x) (1<<(x)) -#define sizenode(t) (twoto((t)->lsizenode)) - - -/* size of buffer for 'luaO_utf8esc' function */ -#define UTF8BUFFSZ 8 - -LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); -LUAI_FUNC int luaO_ceillog2 (unsigned int x); -LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, - const TValue *p2, TValue *res); -LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, - const TValue *p2, StkId res); -LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); -LUAI_FUNC int luaO_hexavalue (int c); -LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); -LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, - va_list argp); -LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.c deleted file mode 100644 index c67aa22..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.c +++ /dev/null @@ -1,104 +0,0 @@ -/* -** $Id: lopcodes.c $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#define lopcodes_c -#define LUA_CORE - -#include "lprefix.h" - - -#include "lopcodes.h" - - -/* ORDER OP */ - -LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* MM OT IT T A mode opcode */ - opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */ - ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */ - ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ - ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ - ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ - ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ - ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/ - ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */ - ,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */ - ,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */ - ,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */ - ,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */ - ,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */ - ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */ - ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */ - ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ - ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ - ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ - ,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */ - ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ - ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ - ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ -}; - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.h deleted file mode 100644 index 7c27451..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopcodes.h +++ /dev/null @@ -1,405 +0,0 @@ -/* -** $Id: lopcodes.h $ -** Opcodes for Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lopcodes_h -#define lopcodes_h - -#include "llimits.h" - - -/*=========================================================================== - We assume that instructions are unsigned 32-bit integers. - All instructions have an opcode in the first 7 bits. - Instructions can have the following formats: - - 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 - 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 -iABC C(8) | B(8) |k| A(8) | Op(7) | -iABx Bx(17) | A(8) | Op(7) | -iAsBx sBx (signed)(17) | A(8) | Op(7) | -iAx Ax(25) | Op(7) | -isJ sJ(25) | Op(7) | - - A signed argument is represented in excess K: the represented value is - the written unsigned value minus K, where K is half the maximum for the - corresponding unsigned argument. -===========================================================================*/ - - -enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ - - -/* -** size and position of opcode arguments. -*/ -#define SIZE_C 8 -#define SIZE_B 8 -#define SIZE_Bx (SIZE_C + SIZE_B + 1) -#define SIZE_A 8 -#define SIZE_Ax (SIZE_Bx + SIZE_A) -#define SIZE_sJ (SIZE_Bx + SIZE_A) - -#define SIZE_OP 7 - -#define POS_OP 0 - -#define POS_A (POS_OP + SIZE_OP) -#define POS_k (POS_A + SIZE_A) -#define POS_B (POS_k + 1) -#define POS_C (POS_B + SIZE_B) - -#define POS_Bx POS_k - -#define POS_Ax POS_A - -#define POS_sJ POS_A - - -/* -** limits for opcode arguments. -** we use (signed) 'int' to manipulate most arguments, -** so they must fit in ints. -*/ - -/* Check whether type 'int' has at least 'b' bits ('b' < 32) */ -#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1) - - -#if L_INTHASBITS(SIZE_Bx) -#define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ - - -#if L_INTHASBITS(SIZE_Ax) -#define MAXARG_Ax ((1<> 1) - - -#define MAXARG_A ((1<> 1) - -#define int2sC(i) ((i) + OFFSET_sC) -#define sC2int(i) ((i) - OFFSET_sC) - - -/* creates a mask with 'n' 1 bits at position 'p' */ -#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p)) - -/* creates a mask with 'n' 0 bits at position 'p' */ -#define MASK0(n,p) (~MASK1(n,p)) - -/* -** the following macros help to manipulate instructions -*/ - -#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) -#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>(pos)) & MASK1(size,0))) -#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ - ((cast(Instruction, v)<> sC */ -OP_SHLI,/* A B sC R[A] := sC << R[B] */ - -OP_ADD,/* A B C R[A] := R[B] + R[C] */ -OP_SUB,/* A B C R[A] := R[B] - R[C] */ -OP_MUL,/* A B C R[A] := R[B] * R[C] */ -OP_MOD,/* A B C R[A] := R[B] % R[C] */ -OP_POW,/* A B C R[A] := R[B] ^ R[C] */ -OP_DIV,/* A B C R[A] := R[B] / R[C] */ -OP_IDIV,/* A B C R[A] := R[B] // R[C] */ - -OP_BAND,/* A B C R[A] := R[B] & R[C] */ -OP_BOR,/* A B C R[A] := R[B] | R[C] */ -OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ -OP_SHL,/* A B C R[A] := R[B] << R[C] */ -OP_SHR,/* A B C R[A] := R[B] >> R[C] */ - -OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */ -OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ -OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ - -OP_UNM,/* A B R[A] := -R[B] */ -OP_BNOT,/* A B R[A] := ~R[B] */ -OP_NOT,/* A B R[A] := not R[B] */ -OP_LEN,/* A B R[A] := #R[B] (length operator) */ - -OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ - -OP_CLOSE,/* A close all upvalues >= R[A] */ -OP_TBC,/* A mark variable A "to be closed" */ -OP_JMP,/* sJ pc += sJ */ -OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ -OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ -OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ - -OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */ -OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */ -OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */ -OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ -OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ -OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ - -OP_TEST,/* A k if (not R[A] == k) then pc++ */ -OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */ - -OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ -OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ - -OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */ -OP_RETURN0,/* return */ -OP_RETURN1,/* A return R[A] */ - -OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ -OP_FORPREP,/* A Bx ; - if not to run then pc+=Bx+1; */ - -OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ -OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ -OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ - -OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */ - -OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ - -OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ - -OP_VARARGPREP,/*A (adjust vararg parameters) */ - -OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ -} OpCode; - - -#define NUM_OPCODES ((int)(OP_EXTRAARG) + 1) - - - -/*=========================================================================== - Notes: - - (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean - value, in a code equivalent to (not cond ? false : true). (It - produces false and skips the next instruction producing true.) - - (*) Opcodes OP_MMBIN and variants follow each arithmetic and - bitwise opcode. If the operation succeeds, it skips this next - opcode. Otherwise, this opcode calls the corresponding metamethod. - - (*) Opcode OP_TESTSET is used in short-circuit expressions that need - both to jump and to produce a value, such as (a = b or c). - - (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then - 'top' is set to last_result+1, so next open instruction (OP_CALL, - OP_RETURN*, OP_SETLIST) may use 'top'. - - (*) In OP_VARARG, if (C == 0) then use actual number of varargs and - set top (like in OP_CALL with C == 0). - - (*) In OP_RETURN, if (B == 0) then return up to 'top'. - - (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always - OP_EXTRAARG. - - (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then - real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the - bits of C). - - (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a - power of 2) plus 1, or zero for size zero. If not k, the array size - is C. Otherwise, the array size is EXTRAARG _ C. - - (*) For comparisons, k specifies what condition the test should accept - (true or false). - - (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped - (the constant is the first operand). - - (*) All 'skips' (pc++) assume that next instruction is a jump. - - (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the - function builds upvalues, which may need to be closed. C > 0 means - the function is vararg, so that its 'func' must be corrected before - returning; in this case, (C - 1) is its number of fixed parameters. - - (*) In comparisons with an immediate operand, C signals whether the - original operand was a float. (It must be corrected in case of - metamethods.) - -===========================================================================*/ - - -/* -** masks for instruction properties. The format is: -** bits 0-2: op mode -** bit 3: instruction set register A -** bit 4: operator is a test (next instruction must be a jump) -** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) -** bit 6: instruction sets 'L->top' for next instruction (when C == 0) -** bit 7: instruction is an MM instruction (call a metamethod) -*/ - -LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) - -#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) -#define testAMode(m) (luaP_opmodes[m] & (1 << 3)) -#define testTMode(m) (luaP_opmodes[m] & (1 << 4)) -#define testITMode(m) (luaP_opmodes[m] & (1 << 5)) -#define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) -#define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) - -/* "out top" (set top for next instruction) */ -#define isOT(i) \ - ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \ - GET_OPCODE(i) == OP_TAILCALL) - -/* "in top" (uses top from previous instruction) */ -#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) - -#define opmode(mm,ot,it,t,a,m) \ - (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) - - -/* number of list items to accumulate before a SETLIST instruction */ -#define LFIELDS_PER_FLUSH 50 - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopnames.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopnames.h deleted file mode 100644 index 965cec9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lopnames.h +++ /dev/null @@ -1,103 +0,0 @@ -/* -** $Id: lopnames.h $ -** Opcode names -** See Copyright Notice in lua.h -*/ - -#if !defined(lopnames_h) -#define lopnames_h - -#include - - -/* ORDER OP */ - -static const char *const opnames[] = { - "MOVE", - "LOADI", - "LOADF", - "LOADK", - "LOADKX", - "LOADFALSE", - "LFALSESKIP", - "LOADTRUE", - "LOADNIL", - "GETUPVAL", - "SETUPVAL", - "GETTABUP", - "GETTABLE", - "GETI", - "GETFIELD", - "SETTABUP", - "SETTABLE", - "SETI", - "SETFIELD", - "NEWTABLE", - "SELF", - "ADDI", - "ADDK", - "SUBK", - "MULK", - "MODK", - "POWK", - "DIVK", - "IDIVK", - "BANDK", - "BORK", - "BXORK", - "SHRI", - "SHLI", - "ADD", - "SUB", - "MUL", - "MOD", - "POW", - "DIV", - "IDIV", - "BAND", - "BOR", - "BXOR", - "SHL", - "SHR", - "MMBIN", - "MMBINI", - "MMBINK", - "UNM", - "BNOT", - "NOT", - "LEN", - "CONCAT", - "CLOSE", - "TBC", - "JMP", - "EQ", - "LT", - "LE", - "EQK", - "EQI", - "LTI", - "LEI", - "GTI", - "GEI", - "TEST", - "TESTSET", - "CALL", - "TAILCALL", - "RETURN", - "RETURN0", - "RETURN1", - "FORLOOP", - "FORPREP", - "TFORPREP", - "TFORCALL", - "TFORLOOP", - "SETLIST", - "CLOSURE", - "VARARG", - "VARARGPREP", - "EXTRAARG", - NULL -}; - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/loslib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/loslib.c deleted file mode 100644 index 3e20d62..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/loslib.c +++ /dev/null @@ -1,430 +0,0 @@ -/* -** $Id: loslib.c $ -** Standard Operating System library -** See Copyright Notice in lua.h -*/ - -#define loslib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** {================================================================== -** List of valid conversion specifiers for the 'strftime' function; -** options are grouped by length; group of length 2 start with '||'. -** =================================================================== -*/ -#if !defined(LUA_STRFTIMEOPTIONS) /* { */ - -/* options for ANSI C 89 (only 1-char options) */ -#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" - -/* options for ISO C 99 and POSIX */ -#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ - "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ - -/* options for Windows */ -#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ - -#if defined(LUA_USE_WINDOWS) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN -#elif defined(LUA_USE_C89) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 -#else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 -#endif - -#endif /* } */ -/* }================================================================== */ - - -/* -** {================================================================== -** Configuration for time-related stuff -** =================================================================== -*/ - -/* -** type to represent time_t in Lua -*/ -#if !defined(LUA_NUMTIME) /* { */ - -#define l_timet lua_Integer -#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) -#define l_gettime(L,arg) luaL_checkinteger(L, arg) - -#else /* }{ */ - -#define l_timet lua_Number -#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t)) -#define l_gettime(L,arg) luaL_checknumber(L, arg) - -#endif /* } */ - - -#if !defined(l_gmtime) /* { */ -/* -** By default, Lua uses gmtime/localtime, except when POSIX is available, -** where it uses gmtime_r/localtime_r -*/ - -#if defined(LUA_USE_POSIX) /* { */ - -#define l_gmtime(t,r) gmtime_r(t,r) -#define l_localtime(t,r) localtime_r(t,r) - -#else /* }{ */ - -/* ISO C definitions */ -#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) -#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) - -#endif /* } */ - -#endif /* } */ - -/* }================================================================== */ - - -/* -** {================================================================== -** Configuration for 'tmpnam': -** By default, Lua uses tmpnam except when POSIX is available, where -** it uses mkstemp. -** =================================================================== -*/ -#if !defined(lua_tmpnam) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include - -#define LUA_TMPNAMBUFSIZE 32 - -#if !defined(LUA_TMPNAMTEMPLATE) -#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" -#endif - -#define lua_tmpnam(b,e) { \ - strcpy(b, LUA_TMPNAMTEMPLATE); \ - e = mkstemp(b); \ - if (e != -1) close(e); \ - e = (e == -1); } - -#else /* }{ */ - -/* ISO C definitions */ -#define LUA_TMPNAMBUFSIZE L_tmpnam -#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } - -#endif /* } */ - -#endif /* } */ -/* }================================================================== */ - - - -static int os_execute (lua_State *L) { - const char *cmd = luaL_optstring(L, 1, NULL); - int stat; - errno = 0; - stat = system(cmd); - if (cmd != NULL) - return luaL_execresult(L, stat); - else { - lua_pushboolean(L, stat); /* true if there is a shell */ - return 1; - } -} - - -static int os_remove (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - return luaL_fileresult(L, remove(filename) == 0, filename); -} - - -static int os_rename (lua_State *L) { - const char *fromname = luaL_checkstring(L, 1); - const char *toname = luaL_checkstring(L, 2); - return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); -} - - -static int os_tmpname (lua_State *L) { - char buff[LUA_TMPNAMBUFSIZE]; - int err; - lua_tmpnam(buff, err); - if (l_unlikely(err)) - return luaL_error(L, "unable to generate a unique filename"); - lua_pushstring(L, buff); - return 1; -} - - -static int os_getenv (lua_State *L) { - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ - return 1; -} - - -static int os_clock (lua_State *L) { - lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); - return 1; -} - - -/* -** {====================================================== -** Time/Date operations -** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, -** wday=%w+1, yday=%j, isdst=? } -** ======================================================= -*/ - -/* -** About the overflow check: an overflow cannot occur when time -** is represented by a lua_Integer, because either lua_Integer is -** large enough to represent all int fields or it is not large enough -** to represent a time that cause a field to overflow. However, if -** times are represented as doubles and lua_Integer is int, then the -** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900 -** to compute the year. -*/ -static void setfield (lua_State *L, const char *key, int value, int delta) { - #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) - if (l_unlikely(value > LUA_MAXINTEGER - delta)) - luaL_error(L, "field '%s' is out-of-bound", key); - #endif - lua_pushinteger(L, (lua_Integer)value + delta); - lua_setfield(L, -2, key); -} - - -static void setboolfield (lua_State *L, const char *key, int value) { - if (value < 0) /* undefined? */ - return; /* does not set field */ - lua_pushboolean(L, value); - lua_setfield(L, -2, key); -} - - -/* -** Set all fields from structure 'tm' in the table on top of the stack -*/ -static void setallfields (lua_State *L, struct tm *stm) { - setfield(L, "year", stm->tm_year, 1900); - setfield(L, "month", stm->tm_mon, 1); - setfield(L, "day", stm->tm_mday, 0); - setfield(L, "hour", stm->tm_hour, 0); - setfield(L, "min", stm->tm_min, 0); - setfield(L, "sec", stm->tm_sec, 0); - setfield(L, "yday", stm->tm_yday, 1); - setfield(L, "wday", stm->tm_wday, 1); - setboolfield(L, "isdst", stm->tm_isdst); -} - - -static int getboolfield (lua_State *L, const char *key) { - int res; - res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); - lua_pop(L, 1); - return res; -} - - -static int getfield (lua_State *L, const char *key, int d, int delta) { - int isnum; - int t = lua_getfield(L, -1, key); /* get field and its type */ - lua_Integer res = lua_tointegerx(L, -1, &isnum); - if (!isnum) { /* field is not an integer? */ - if (l_unlikely(t != LUA_TNIL)) /* some other value? */ - return luaL_error(L, "field '%s' is not an integer", key); - else if (l_unlikely(d < 0)) /* absent field; no default? */ - return luaL_error(L, "field '%s' missing in date table", key); - res = d; - } - else { - /* unsigned avoids overflow when lua_Integer has 32 bits */ - if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta - : (lua_Integer)INT_MIN + delta <= res)) - return luaL_error(L, "field '%s' is out-of-bound", key); - res -= delta; - } - lua_pop(L, 1); - return (int)res; -} - - -static const char *checkoption (lua_State *L, const char *conv, - ptrdiff_t convlen, char *buff) { - const char *option = LUA_STRFTIMEOPTIONS; - int oplen = 1; /* length of options being checked */ - for (; *option != '\0' && oplen <= convlen; option += oplen) { - if (*option == '|') /* next block? */ - oplen++; /* will check options with next length (+1) */ - else if (memcmp(conv, option, oplen) == 0) { /* match? */ - memcpy(buff, conv, oplen); /* copy valid option to buffer */ - buff[oplen] = '\0'; - return conv + oplen; /* return next item */ - } - } - luaL_argerror(L, 1, - lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); - return conv; /* to avoid warnings */ -} - - -static time_t l_checktime (lua_State *L, int arg) { - l_timet t = l_gettime(L, arg); - luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); - return (time_t)t; -} - - -/* maximum size for an individual 'strftime' item */ -#define SIZETIMEFMT 250 - - -static int os_date (lua_State *L) { - size_t slen; - const char *s = luaL_optlstring(L, 1, "%c", &slen); - time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); - const char *se = s + slen; /* 's' end */ - struct tm tmr, *stm; - if (*s == '!') { /* UTC? */ - stm = l_gmtime(&t, &tmr); - s++; /* skip '!' */ - } - else - stm = l_localtime(&t, &tmr); - if (stm == NULL) /* invalid date? */ - return luaL_error(L, - "date result cannot be represented in this installation"); - if (strcmp(s, "*t") == 0) { - lua_createtable(L, 0, 9); /* 9 = number of fields */ - setallfields(L, stm); - } - else { - char cc[4]; /* buffer for individual conversion specifiers */ - luaL_Buffer b; - cc[0] = '%'; - luaL_buffinit(L, &b); - while (s < se) { - if (*s != '%') /* not a conversion specifier? */ - luaL_addchar(&b, *s++); - else { - size_t reslen; - char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); - s++; /* skip '%' */ - s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ - reslen = strftime(buff, SIZETIMEFMT, cc, stm); - luaL_addsize(&b, reslen); - } - } - luaL_pushresult(&b); - } - return 1; -} - - -static int os_time (lua_State *L) { - time_t t; - if (lua_isnoneornil(L, 1)) /* called without args? */ - t = time(NULL); /* get current time */ - else { - struct tm ts; - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_year = getfield(L, "year", -1, 1900); - ts.tm_mon = getfield(L, "month", -1, 1); - ts.tm_mday = getfield(L, "day", -1, 0); - ts.tm_hour = getfield(L, "hour", 12, 0); - ts.tm_min = getfield(L, "min", 0, 0); - ts.tm_sec = getfield(L, "sec", 0, 0); - ts.tm_isdst = getboolfield(L, "isdst"); - t = mktime(&ts); - setallfields(L, &ts); /* update fields with normalized values */ - } - if (t != (time_t)(l_timet)t || t == (time_t)(-1)) - return luaL_error(L, - "time result cannot be represented in this installation"); - l_pushtime(L, t); - return 1; -} - - -static int os_difftime (lua_State *L) { - time_t t1 = l_checktime(L, 1); - time_t t2 = l_checktime(L, 2); - lua_pushnumber(L, (lua_Number)difftime(t1, t2)); - return 1; -} - -/* }====================================================== */ - - -static int os_setlocale (lua_State *L) { - static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, - LC_NUMERIC, LC_TIME}; - static const char *const catnames[] = {"all", "collate", "ctype", "monetary", - "numeric", "time", NULL}; - const char *l = luaL_optstring(L, 1, NULL); - int op = luaL_checkoption(L, 2, "all", catnames); - lua_pushstring(L, setlocale(cat[op], l)); - return 1; -} - - -static int os_exit (lua_State *L) { - int status; - if (lua_isboolean(L, 1)) - status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); - else - status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); - if (lua_toboolean(L, 2)) - lua_close(L); - if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ - return 0; -} - - -static const luaL_Reg syslib[] = { - {"clock", os_clock}, - {"date", os_date}, - {"difftime", os_difftime}, - {"execute", os_execute}, - {"exit", os_exit}, - {"getenv", os_getenv}, - {"remove", os_remove}, - {"rename", os_rename}, - {"setlocale", os_setlocale}, - {"time", os_time}, - {"tmpname", os_tmpname}, - {NULL, NULL} -}; - -/* }====================================================== */ - - - -LUAMOD_API int luaopen_os (lua_State *L) { - luaL_newlib(L, syslib); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.c deleted file mode 100644 index 3abe3d7..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.c +++ /dev/null @@ -1,1966 +0,0 @@ -/* -** $Id: lparser.c $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - -#define lparser_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lcode.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "llex.h" -#include "lmem.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lparser.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" - - - -/* maximum number of local variables per function (must be smaller - than 250, due to the bytecode format) */ -#define MAXVARS 200 - - -#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) - - -/* because all strings are unified by the scanner, the parser - can use pointer equality for string equality */ -#define eqstr(a,b) ((a) == (b)) - - -/* -** nodes for block list (list of active blocks) -*/ -typedef struct BlockCnt { - struct BlockCnt *previous; /* chain */ - int firstlabel; /* index of first label in this block */ - int firstgoto; /* index of first pending goto in this block */ - lu_byte nactvar; /* # active locals outside the block */ - lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if 'block' is a loop */ - lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ -} BlockCnt; - - - -/* -** prototypes for recursive non-terminal functions -*/ -static void statement (LexState *ls); -static void expr (LexState *ls, expdesc *v); - - -static l_noret error_expected (LexState *ls, int token) { - luaX_syntaxerror(ls, - luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); -} - - -static l_noret errorlimit (FuncState *fs, int limit, const char *what) { - lua_State *L = fs->ls->L; - const char *msg; - int line = fs->f->linedefined; - const char *where = (line == 0) - ? "main function" - : luaO_pushfstring(L, "function at line %d", line); - msg = luaO_pushfstring(L, "too many %s (limit is %d) in %s", - what, limit, where); - luaX_syntaxerror(fs->ls, msg); -} - - -static void checklimit (FuncState *fs, int v, int l, const char *what) { - if (v > l) errorlimit(fs, l, what); -} - - -/* -** Test whether next token is 'c'; if so, skip it. -*/ -static int testnext (LexState *ls, int c) { - if (ls->t.token == c) { - luaX_next(ls); - return 1; - } - else return 0; -} - - -/* -** Check that next token is 'c'. -*/ -static void check (LexState *ls, int c) { - if (ls->t.token != c) - error_expected(ls, c); -} - - -/* -** Check that next token is 'c' and skip it. -*/ -static void checknext (LexState *ls, int c) { - check(ls, c); - luaX_next(ls); -} - - -#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } - - -/* -** Check that next token is 'what' and skip it. In case of error, -** raise an error that the expected 'what' should match a 'who' -** in line 'where' (if that is not the current line). -*/ -static void check_match (LexState *ls, int what, int who, int where) { - if (l_unlikely(!testnext(ls, what))) { - if (where == ls->linenumber) /* all in the same line? */ - error_expected(ls, what); /* do not need a complex message */ - else { - luaX_syntaxerror(ls, luaO_pushfstring(ls->L, - "%s expected (to close %s at line %d)", - luaX_token2str(ls, what), luaX_token2str(ls, who), where)); - } - } -} - - -static TString *str_checkname (LexState *ls) { - TString *ts; - check(ls, TK_NAME); - ts = ls->t.seminfo.ts; - luaX_next(ls); - return ts; -} - - -static void init_exp (expdesc *e, expkind k, int i) { - e->f = e->t = NO_JUMP; - e->k = k; - e->u.info = i; -} - - -static void codestring (expdesc *e, TString *s) { - e->f = e->t = NO_JUMP; - e->k = VKSTR; - e->u.strval = s; -} - - -static void codename (LexState *ls, expdesc *e) { - codestring(e, str_checkname(ls)); -} - - -/* -** Register a new local variable in the active 'Proto' (for debug -** information). -*/ -static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { - Proto *f = fs->f; - int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, - LocVar, SHRT_MAX, "local variables"); - while (oldsize < f->sizelocvars) - f->locvars[oldsize++].varname = NULL; - f->locvars[fs->ndebugvars].varname = varname; - f->locvars[fs->ndebugvars].startpc = fs->pc; - luaC_objbarrier(ls->L, f, varname); - return fs->ndebugvars++; -} - - -/* -** Create a new local variable with the given 'name'. Return its index -** in the function. -*/ -static int new_localvar (LexState *ls, TString *name) { - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Dyndata *dyd = ls->dyd; - Vardesc *var; - checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); - luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); - var = &dyd->actvar.arr[dyd->actvar.n++]; - var->vd.kind = VDKREG; /* default */ - var->vd.name = name; - return dyd->actvar.n - 1 - fs->firstlocal; -} - -#define new_localvarliteral(ls,v) \ - new_localvar(ls, \ - luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); - - - -/* -** Return the "variable description" (Vardesc) of a given variable. -** (Unless noted otherwise, all variables are referred to by their -** compiler indices.) -*/ -static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { - return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; -} - - -/* -** Convert 'nvar', a compiler index level, to its corresponding -** register. For that, search for the highest variable below that level -** that is in a register and uses its register index ('ridx') plus one. -*/ -static int reglevel (FuncState *fs, int nvar) { - while (nvar-- > 0) { - Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ - if (vd->vd.kind != RDKCTC) /* is in a register? */ - return vd->vd.ridx + 1; - } - return 0; /* no variables in registers */ -} - - -/* -** Return the number of variables in the register stack for the given -** function. -*/ -int luaY_nvarstack (FuncState *fs) { - return reglevel(fs, fs->nactvar); -} - - -/* -** Get the debug-information entry for current variable 'vidx'. -*/ -static LocVar *localdebuginfo (FuncState *fs, int vidx) { - Vardesc *vd = getlocalvardesc(fs, vidx); - if (vd->vd.kind == RDKCTC) - return NULL; /* no debug info. for constants */ - else { - int idx = vd->vd.pidx; - lua_assert(idx < fs->ndebugvars); - return &fs->f->locvars[idx]; - } -} - - -/* -** Create an expression representing variable 'vidx' -*/ -static void init_var (FuncState *fs, expdesc *e, int vidx) { - e->f = e->t = NO_JUMP; - e->k = VLOCAL; - e->u.var.vidx = vidx; - e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; -} - - -/* -** Raises an error if variable described by 'e' is read only -*/ -static void check_readonly (LexState *ls, expdesc *e) { - FuncState *fs = ls->fs; - TString *varname = NULL; /* to be set if variable is const */ - switch (e->k) { - case VCONST: { - varname = ls->dyd->actvar.arr[e->u.info].vd.name; - break; - } - case VLOCAL: { - Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); - if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ - varname = vardesc->vd.name; - break; - } - case VUPVAL: { - Upvaldesc *up = &fs->f->upvalues[e->u.info]; - if (up->kind != VDKREG) - varname = up->name; - break; - } - default: - return; /* other cases cannot be read-only */ - } - if (varname) { - const char *msg = luaO_pushfstring(ls->L, - "attempt to assign to const variable '%s'", getstr(varname)); - luaK_semerror(ls, msg); /* error */ - } -} - - -/* -** Start the scope for the last 'nvars' created variables. -*/ -static void adjustlocalvars (LexState *ls, int nvars) { - FuncState *fs = ls->fs; - int reglevel = luaY_nvarstack(fs); - int i; - for (i = 0; i < nvars; i++) { - int vidx = fs->nactvar++; - Vardesc *var = getlocalvardesc(fs, vidx); - var->vd.ridx = reglevel++; - var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); - } -} - - -/* -** Close the scope for all variables up to level 'tolevel'. -** (debug info.) -*/ -static void removevars (FuncState *fs, int tolevel) { - fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); - while (fs->nactvar > tolevel) { - LocVar *var = localdebuginfo(fs, --fs->nactvar); - if (var) /* does it have debug information? */ - var->endpc = fs->pc; - } -} - - -/* -** Search the upvalues of the function 'fs' for one -** with the given 'name'. -*/ -static int searchupvalue (FuncState *fs, TString *name) { - int i; - Upvaldesc *up = fs->f->upvalues; - for (i = 0; i < fs->nups; i++) { - if (eqstr(up[i].name, name)) return i; - } - return -1; /* not found */ -} - - -static Upvaldesc *allocupvalue (FuncState *fs) { - Proto *f = fs->f; - int oldsize = f->sizeupvalues; - checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); - luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, - Upvaldesc, MAXUPVAL, "upvalues"); - while (oldsize < f->sizeupvalues) - f->upvalues[oldsize++].name = NULL; - return &f->upvalues[fs->nups++]; -} - - -static int newupvalue (FuncState *fs, TString *name, expdesc *v) { - Upvaldesc *up = allocupvalue(fs); - FuncState *prev = fs->prev; - if (v->k == VLOCAL) { - up->instack = 1; - up->idx = v->u.var.ridx; - up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; - lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); - } - else { - up->instack = 0; - up->idx = cast_byte(v->u.info); - up->kind = prev->f->upvalues[v->u.info].kind; - lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); - } - up->name = name; - luaC_objbarrier(fs->ls->L, fs->f, name); - return fs->nups - 1; -} - - -/* -** Look for an active local variable with the name 'n' in the -** function 'fs'. If found, initialize 'var' with it and return -** its expression kind; otherwise return -1. -*/ -static int searchvar (FuncState *fs, TString *n, expdesc *var) { - int i; - for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - Vardesc *vd = getlocalvardesc(fs, i); - if (eqstr(n, vd->vd.name)) { /* found? */ - if (vd->vd.kind == RDKCTC) /* compile-time constant? */ - init_exp(var, VCONST, fs->firstlocal + i); - else /* real variable */ - init_var(fs, var, i); - return var->k; - } - } - return -1; /* not found */ -} - - -/* -** Mark block where variable at given level was defined -** (to emit close instructions later). -*/ -static void markupval (FuncState *fs, int level) { - BlockCnt *bl = fs->bl; - while (bl->nactvar > level) - bl = bl->previous; - bl->upval = 1; - fs->needclose = 1; -} - - -/* -** Mark that current block has a to-be-closed variable. -*/ -static void marktobeclosed (FuncState *fs) { - BlockCnt *bl = fs->bl; - bl->upval = 1; - bl->insidetbc = 1; - fs->needclose = 1; -} - - -/* -** Find a variable with the given name 'n'. If it is an upvalue, add -** this upvalue into all intermediate functions. If it is a global, set -** 'var' as 'void' as a flag. -*/ -static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - if (fs == NULL) /* no more levels? */ - init_exp(var, VVOID, 0); /* default is global */ - else { - int v = searchvar(fs, n, var); /* look up locals at current level */ - if (v >= 0) { /* found? */ - if (v == VLOCAL && !base) - markupval(fs, var->u.var.vidx); /* local will be used as an upval */ - } - else { /* not found as local at current level; try upvalues */ - int idx = searchupvalue(fs, n); /* try existing upvalues */ - if (idx < 0) { /* not found? */ - singlevaraux(fs->prev, n, var, 0); /* try upper levels */ - if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ - idx = newupvalue(fs, n, var); /* will be a new upvalue */ - else /* it is a global or a constant */ - return; /* don't need to do anything at this level */ - } - init_exp(var, VUPVAL, idx); /* new or old upvalue */ - } - } -} - - -/* -** Find a variable with the given name 'n', handling global variables -** too. -*/ -static void singlevar (LexState *ls, expdesc *var) { - TString *varname = str_checkname(ls); - FuncState *fs = ls->fs; - singlevaraux(fs, varname, var, 1); - if (var->k == VVOID) { /* global name? */ - expdesc key; - singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k != VVOID); /* this one must exist */ - codestring(&key, varname); /* key is variable name */ - luaK_indexed(fs, var, &key); /* env[varname] */ - } -} - - -/* -** Adjust the number of results from an expression list 'e' with 'nexps' -** expressions to 'nvars' values. -*/ -static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { - FuncState *fs = ls->fs; - int needed = nvars - nexps; /* extra values needed */ - if (hasmultret(e->k)) { /* last expression has multiple returns? */ - int extra = needed + 1; /* discount last expression itself */ - if (extra < 0) - extra = 0; - luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ - } - else { - if (e->k != VVOID) /* at least one expression? */ - luaK_exp2nextreg(fs, e); /* close last expression */ - if (needed > 0) /* missing values? */ - luaK_nil(fs, fs->freereg, needed); /* complete with nils */ - } - if (needed > 0) - luaK_reserveregs(fs, needed); /* registers for extra values */ - else /* adding 'needed' is actually a subtraction */ - fs->freereg += needed; /* remove extra values */ -} - - -#define enterlevel(ls) luaE_incCstack(ls->L) - - -#define leavelevel(ls) ((ls)->L->nCcalls--) - - -/* -** Generates an error that a goto jumps into the scope of some -** local variable. -*/ -static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); - const char *msg = " at line %d jumps into the scope of local '%s'"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); - luaK_semerror(ls, msg); /* raise the error */ -} - - -/* -** Solves the goto at index 'g' to given 'label' and removes it -** from the list of pending goto's. -** If it jumps into the scope of some variable, raises an error. -*/ -static void solvegoto (LexState *ls, int g, Labeldesc *label) { - int i; - Labellist *gl = &ls->dyd->gt; /* list of goto's */ - Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ - lua_assert(eqstr(gt->name, label->name)); - if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ - jumpscopeerror(ls, gt); - luaK_patchlist(ls->fs, gt->pc, label->pc); - for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ - gl->arr[i] = gl->arr[i + 1]; - gl->n--; -} - - -/* -** Search for an active label with the given name. -*/ -static Labeldesc *findlabel (LexState *ls, TString *name) { - int i; - Dyndata *dyd = ls->dyd; - /* check labels in current function for a match */ - for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { - Labeldesc *lb = &dyd->label.arr[i]; - if (eqstr(lb->name, name)) /* correct label? */ - return lb; - } - return NULL; /* label not found */ -} - - -/* -** Adds a new label/goto in the corresponding list. -*/ -static int newlabelentry (LexState *ls, Labellist *l, TString *name, - int line, int pc) { - int n = l->n; - luaM_growvector(ls->L, l->arr, n, l->size, - Labeldesc, SHRT_MAX, "labels/gotos"); - l->arr[n].name = name; - l->arr[n].line = line; - l->arr[n].nactvar = ls->fs->nactvar; - l->arr[n].close = 0; - l->arr[n].pc = pc; - l->n = n + 1; - return n; -} - - -static int newgotoentry (LexState *ls, TString *name, int line, int pc) { - return newlabelentry(ls, &ls->dyd->gt, name, line, pc); -} - - -/* -** Solves forward jumps. Check whether new label 'lb' matches any -** pending gotos in current block and solves them. Return true -** if any of the goto's need to close upvalues. -*/ -static int solvegotos (LexState *ls, Labeldesc *lb) { - Labellist *gl = &ls->dyd->gt; - int i = ls->fs->bl->firstgoto; - int needsclose = 0; - while (i < gl->n) { - if (eqstr(gl->arr[i].name, lb->name)) { - needsclose |= gl->arr[i].close; - solvegoto(ls, i, lb); /* will remove 'i' from the list */ - } - else - i++; - } - return needsclose; -} - - -/* -** Create a new label with the given 'name' at the given 'line'. -** 'last' tells whether label is the last non-op statement in its -** block. Solves all pending goto's to this new label and adds -** a close instruction if necessary. -** Returns true iff it added a close instruction. -*/ -static int createlabel (LexState *ls, TString *name, int line, - int last) { - FuncState *fs = ls->fs; - Labellist *ll = &ls->dyd->label; - int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); - if (last) { /* label is last no-op statement in the block? */ - /* assume that locals are already out of scope */ - ll->arr[l].nactvar = fs->bl->nactvar; - } - if (solvegotos(ls, &ll->arr[l])) { /* need close? */ - luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); - return 1; - } - return 0; -} - - -/* -** Adjust pending gotos to outer level of a block. -*/ -static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i; - Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block */ - for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ - Labeldesc *gt = &gl->arr[i]; - /* leaving a variable scope? */ - if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar)) - gt->close |= bl->upval; /* jump may need a close */ - gt->nactvar = bl->nactvar; /* update goto level */ - } -} - - -static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { - bl->isloop = isloop; - bl->nactvar = fs->nactvar; - bl->firstlabel = fs->ls->dyd->label.n; - bl->firstgoto = fs->ls->dyd->gt.n; - bl->upval = 0; - bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); - bl->previous = fs->bl; - fs->bl = bl; - lua_assert(fs->freereg == luaY_nvarstack(fs)); -} - - -/* -** generates an error for an undefined 'goto'. -*/ -static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg; - if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { - msg = "break outside loop at line %d"; - msg = luaO_pushfstring(ls->L, msg, gt->line); - } - else { - msg = "no visible label '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - } - luaK_semerror(ls, msg); -} - - -static void leaveblock (FuncState *fs) { - BlockCnt *bl = fs->bl; - LexState *ls = fs->ls; - int hasclose = 0; - int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ - if (bl->isloop) /* fix pending breaks? */ - hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); - if (!hasclose && bl->previous && bl->upval) - luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); - fs->bl = bl->previous; - removevars(fs, bl->nactvar); - lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = stklevel; /* free registers */ - ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - if (bl->previous) /* inner block? */ - movegotosout(fs, bl); /* update pending gotos to outer block */ - else { - if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ - undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ - } -} - - -/* -** adds a new prototype into list of prototypes -*/ -static Proto *addprototype (LexState *ls) { - Proto *clp; - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; /* prototype of current function */ - if (fs->np >= f->sizep) { - int oldsize = f->sizep; - luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); - while (oldsize < f->sizep) - f->p[oldsize++] = NULL; - } - f->p[fs->np++] = clp = luaF_newproto(L); - luaC_objbarrier(L, f, clp); - return clp; -} - - -/* -** codes instruction to create new closure in parent function. -** The OP_CLOSURE instruction uses the last available register, -** so that, if it invokes the GC, the GC knows which registers -** are in use at that time. - -*/ -static void codeclosure (LexState *ls, expdesc *v) { - FuncState *fs = ls->fs->prev; - init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); - luaK_exp2nextreg(fs, v); /* fix it at the last register */ -} - - -static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - Proto *f = fs->f; - fs->prev = ls->fs; /* linked list of funcstates */ - fs->ls = ls; - ls->fs = fs; - fs->pc = 0; - fs->previousline = f->linedefined; - fs->iwthabs = 0; - fs->lasttarget = 0; - fs->freereg = 0; - fs->nk = 0; - fs->nabslineinfo = 0; - fs->np = 0; - fs->nups = 0; - fs->ndebugvars = 0; - fs->nactvar = 0; - fs->needclose = 0; - fs->firstlocal = ls->dyd->actvar.n; - fs->firstlabel = ls->dyd->label.n; - fs->bl = NULL; - f->source = ls->source; - luaC_objbarrier(ls->L, f, f->source); - f->maxstacksize = 2; /* registers 0/1 are always valid */ - enterblock(fs, bl, 0); -} - - -static void close_func (LexState *ls) { - lua_State *L = ls->L; - FuncState *fs = ls->fs; - Proto *f = fs->f; - luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ - leaveblock(fs); - lua_assert(fs->bl == NULL); - luaK_finish(fs); - luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); - luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); - luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, - fs->nabslineinfo, AbsLineInfo); - luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); - luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); - luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); - luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - ls->fs = fs->prev; - luaC_checkGC(L); -} - - - -/*============================================================*/ -/* GRAMMAR RULES */ -/*============================================================*/ - - -/* -** check whether current token is in the follow set of a block. -** 'until' closes syntactical blocks, but do not close scope, -** so it is handled in separate. -*/ -static int block_follow (LexState *ls, int withuntil) { - switch (ls->t.token) { - case TK_ELSE: case TK_ELSEIF: - case TK_END: case TK_EOS: - return 1; - case TK_UNTIL: return withuntil; - default: return 0; - } -} - - -static void statlist (LexState *ls) { - /* statlist -> { stat [';'] } */ - while (!block_follow(ls, 1)) { - if (ls->t.token == TK_RETURN) { - statement(ls); - return; /* 'return' must be last statement */ - } - statement(ls); - } -} - - -static void fieldsel (LexState *ls, expdesc *v) { - /* fieldsel -> ['.' | ':'] NAME */ - FuncState *fs = ls->fs; - expdesc key; - luaK_exp2anyregup(fs, v); - luaX_next(ls); /* skip the dot or colon */ - codename(ls, &key); - luaK_indexed(fs, v, &key); -} - - -static void yindex (LexState *ls, expdesc *v) { - /* index -> '[' expr ']' */ - luaX_next(ls); /* skip the '[' */ - expr(ls, v); - luaK_exp2val(ls->fs, v); - checknext(ls, ']'); -} - - -/* -** {====================================================================== -** Rules for Constructors -** ======================================================================= -*/ - - -typedef struct ConsControl { - expdesc v; /* last list item read */ - expdesc *t; /* table descriptor */ - int nh; /* total number of 'record' elements */ - int na; /* number of array elements already stored */ - int tostore; /* number of array elements pending to be stored */ -} ConsControl; - - -static void recfield (LexState *ls, ConsControl *cc) { - /* recfield -> (NAME | '['exp']') = exp */ - FuncState *fs = ls->fs; - int reg = ls->fs->freereg; - expdesc tab, key, val; - if (ls->t.token == TK_NAME) { - checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); - codename(ls, &key); - } - else /* ls->t.token == '[' */ - yindex(ls, &key); - cc->nh++; - checknext(ls, '='); - tab = *cc->t; - luaK_indexed(fs, &tab, &key); - expr(ls, &val); - luaK_storevar(fs, &tab, &val); - fs->freereg = reg; /* free registers */ -} - - -static void closelistfield (FuncState *fs, ConsControl *cc) { - if (cc->v.k == VVOID) return; /* there is no list item */ - luaK_exp2nextreg(fs, &cc->v); - cc->v.k = VVOID; - if (cc->tostore == LFIELDS_PER_FLUSH) { - luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ - cc->na += cc->tostore; - cc->tostore = 0; /* no more items pending */ - } -} - - -static void lastlistfield (FuncState *fs, ConsControl *cc) { - if (cc->tostore == 0) return; - if (hasmultret(cc->v.k)) { - luaK_setmultret(fs, &cc->v); - luaK_setlist(fs, cc->t->u.info, cc->na, LUA_MULTRET); - cc->na--; /* do not count last expression (unknown number of elements) */ - } - else { - if (cc->v.k != VVOID) - luaK_exp2nextreg(fs, &cc->v); - luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); - } - cc->na += cc->tostore; -} - - -static void listfield (LexState *ls, ConsControl *cc) { - /* listfield -> exp */ - expr(ls, &cc->v); - cc->tostore++; -} - - -static void field (LexState *ls, ConsControl *cc) { - /* field -> listfield | recfield */ - switch(ls->t.token) { - case TK_NAME: { /* may be 'listfield' or 'recfield' */ - if (luaX_lookahead(ls) != '=') /* expression? */ - listfield(ls, cc); - else - recfield(ls, cc); - break; - } - case '[': { - recfield(ls, cc); - break; - } - default: { - listfield(ls, cc); - break; - } - } -} - - -static void constructor (LexState *ls, expdesc *t) { - /* constructor -> '{' [ field { sep field } [sep] ] '}' - sep -> ',' | ';' */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - ConsControl cc; - luaK_code(fs, 0); /* space for extra arg. */ - cc.na = cc.nh = cc.tostore = 0; - cc.t = t; - init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ - luaK_reserveregs(fs, 1); - init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - checknext(ls, '{'); - do { - lua_assert(cc.v.k == VVOID || cc.tostore > 0); - if (ls->t.token == '}') break; - closelistfield(fs, &cc); - field(ls, &cc); - } while (testnext(ls, ',') || testnext(ls, ';')); - check_match(ls, '}', '{', line); - lastlistfield(fs, &cc); - luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); -} - -/* }====================================================================== */ - - -static void setvararg (FuncState *fs, int nparams) { - fs->f->is_vararg = 1; - luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); -} - - -static void parlist (LexState *ls) { - /* parlist -> [ {NAME ','} (NAME | '...') ] */ - FuncState *fs = ls->fs; - Proto *f = fs->f; - int nparams = 0; - int isvararg = 0; - if (ls->t.token != ')') { /* is 'parlist' not empty? */ - do { - switch (ls->t.token) { - case TK_NAME: { - new_localvar(ls, str_checkname(ls)); - nparams++; - break; - } - case TK_DOTS: { - luaX_next(ls); - isvararg = 1; - break; - } - default: luaX_syntaxerror(ls, " or '...' expected"); - } - } while (!isvararg && testnext(ls, ',')); - } - adjustlocalvars(ls, nparams); - f->numparams = cast_byte(fs->nactvar); - if (isvararg) - setvararg(fs, f->numparams); /* declared vararg */ - luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ -} - - -static void body (LexState *ls, expdesc *e, int ismethod, int line) { - /* body -> '(' parlist ')' block END */ - FuncState new_fs; - BlockCnt bl; - new_fs.f = addprototype(ls); - new_fs.f->linedefined = line; - open_func(ls, &new_fs, &bl); - checknext(ls, '('); - if (ismethod) { - new_localvarliteral(ls, "self"); /* create 'self' parameter */ - adjustlocalvars(ls, 1); - } - parlist(ls); - checknext(ls, ')'); - statlist(ls); - new_fs.f->lastlinedefined = ls->linenumber; - check_match(ls, TK_END, TK_FUNCTION, line); - codeclosure(ls, e); - close_func(ls); -} - - -static int explist (LexState *ls, expdesc *v) { - /* explist -> expr { ',' expr } */ - int n = 1; /* at least one expression */ - expr(ls, v); - while (testnext(ls, ',')) { - luaK_exp2nextreg(ls->fs, v); - expr(ls, v); - n++; - } - return n; -} - - -static void funcargs (LexState *ls, expdesc *f, int line) { - FuncState *fs = ls->fs; - expdesc args; - int base, nparams; - switch (ls->t.token) { - case '(': { /* funcargs -> '(' [ explist ] ')' */ - luaX_next(ls); - if (ls->t.token == ')') /* arg list is empty? */ - args.k = VVOID; - else { - explist(ls, &args); - if (hasmultret(args.k)) - luaK_setmultret(fs, &args); - } - check_match(ls, ')', '(', line); - break; - } - case '{': { /* funcargs -> constructor */ - constructor(ls, &args); - break; - } - case TK_STRING: { /* funcargs -> STRING */ - codestring(&args, ls->t.seminfo.ts); - luaX_next(ls); /* must use 'seminfo' before 'next' */ - break; - } - default: { - luaX_syntaxerror(ls, "function arguments expected"); - } - } - lua_assert(f->k == VNONRELOC); - base = f->u.info; /* base register for call */ - if (hasmultret(args.k)) - nparams = LUA_MULTRET; /* open call */ - else { - if (args.k != VVOID) - luaK_exp2nextreg(fs, &args); /* close last argument */ - nparams = fs->freereg - (base+1); - } - init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); - luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ -} - - - - -/* -** {====================================================================== -** Expression parsing -** ======================================================================= -*/ - - -static void primaryexp (LexState *ls, expdesc *v) { - /* primaryexp -> NAME | '(' expr ')' */ - switch (ls->t.token) { - case '(': { - int line = ls->linenumber; - luaX_next(ls); - expr(ls, v); - check_match(ls, ')', '(', line); - luaK_dischargevars(ls->fs, v); - return; - } - case TK_NAME: { - singlevar(ls, v); - return; - } - default: { - luaX_syntaxerror(ls, "unexpected symbol"); - } - } -} - - -static void suffixedexp (LexState *ls, expdesc *v) { - /* suffixedexp -> - primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ - FuncState *fs = ls->fs; - int line = ls->linenumber; - primaryexp(ls, v); - for (;;) { - switch (ls->t.token) { - case '.': { /* fieldsel */ - fieldsel(ls, v); - break; - } - case '[': { /* '[' exp ']' */ - expdesc key; - luaK_exp2anyregup(fs, v); - yindex(ls, &key); - luaK_indexed(fs, v, &key); - break; - } - case ':': { /* ':' NAME funcargs */ - expdesc key; - luaX_next(ls); - codename(ls, &key); - luaK_self(fs, v, &key); - funcargs(ls, v, line); - break; - } - case '(': case TK_STRING: case '{': { /* funcargs */ - luaK_exp2nextreg(fs, v); - funcargs(ls, v, line); - break; - } - default: return; - } - } -} - - -static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | - constructor | FUNCTION body | suffixedexp */ - switch (ls->t.token) { - case TK_FLT: { - init_exp(v, VKFLT, 0); - v->u.nval = ls->t.seminfo.r; - break; - } - case TK_INT: { - init_exp(v, VKINT, 0); - v->u.ival = ls->t.seminfo.i; - break; - } - case TK_STRING: { - codestring(v, ls->t.seminfo.ts); - break; - } - case TK_NIL: { - init_exp(v, VNIL, 0); - break; - } - case TK_TRUE: { - init_exp(v, VTRUE, 0); - break; - } - case TK_FALSE: { - init_exp(v, VFALSE, 0); - break; - } - case TK_DOTS: { /* vararg */ - FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, - "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); - break; - } - case '{': { /* constructor */ - constructor(ls, v); - return; - } - case TK_FUNCTION: { - luaX_next(ls); - body(ls, v, 0, ls->linenumber); - return; - } - default: { - suffixedexp(ls, v); - return; - } - } - luaX_next(ls); -} - - -static UnOpr getunopr (int op) { - switch (op) { - case TK_NOT: return OPR_NOT; - case '-': return OPR_MINUS; - case '~': return OPR_BNOT; - case '#': return OPR_LEN; - default: return OPR_NOUNOPR; - } -} - - -static BinOpr getbinopr (int op) { - switch (op) { - case '+': return OPR_ADD; - case '-': return OPR_SUB; - case '*': return OPR_MUL; - case '%': return OPR_MOD; - case '^': return OPR_POW; - case '/': return OPR_DIV; - case TK_IDIV: return OPR_IDIV; - case '&': return OPR_BAND; - case '|': return OPR_BOR; - case '~': return OPR_BXOR; - case TK_SHL: return OPR_SHL; - case TK_SHR: return OPR_SHR; - case TK_CONCAT: return OPR_CONCAT; - case TK_NE: return OPR_NE; - case TK_EQ: return OPR_EQ; - case '<': return OPR_LT; - case TK_LE: return OPR_LE; - case '>': return OPR_GT; - case TK_GE: return OPR_GE; - case TK_AND: return OPR_AND; - case TK_OR: return OPR_OR; - default: return OPR_NOBINOPR; - } -} - - -/* -** Priority table for binary operators. -*/ -static const struct { - lu_byte left; /* left priority for each binary operator */ - lu_byte right; /* right priority */ -} priority[] = { /* ORDER OPR */ - {10, 10}, {10, 10}, /* '+' '-' */ - {11, 11}, {11, 11}, /* '*' '%' */ - {14, 13}, /* '^' (right associative) */ - {11, 11}, {11, 11}, /* '/' '//' */ - {6, 6}, {4, 4}, {5, 5}, /* '&' '|' '~' */ - {7, 7}, {7, 7}, /* '<<' '>>' */ - {9, 8}, /* '..' (right associative) */ - {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ - {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ - {2, 2}, {1, 1} /* and, or */ -}; - -#define UNARY_PRIORITY 12 /* priority for unary operators */ - - -/* -** subexpr -> (simpleexp | unop subexpr) { binop subexpr } -** where 'binop' is any binary operator with a priority higher than 'limit' -*/ -static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { - BinOpr op; - UnOpr uop; - enterlevel(ls); - uop = getunopr(ls->t.token); - if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ - int line = ls->linenumber; - luaX_next(ls); /* skip operator */ - subexpr(ls, v, UNARY_PRIORITY); - luaK_prefix(ls->fs, uop, v, line); - } - else simpleexp(ls, v); - /* expand while operators have priorities higher than 'limit' */ - op = getbinopr(ls->t.token); - while (op != OPR_NOBINOPR && priority[op].left > limit) { - expdesc v2; - BinOpr nextop; - int line = ls->linenumber; - luaX_next(ls); /* skip operator */ - luaK_infix(ls->fs, op, v); - /* read sub-expression with higher priority */ - nextop = subexpr(ls, &v2, priority[op].right); - luaK_posfix(ls->fs, op, v, &v2, line); - op = nextop; - } - leavelevel(ls); - return op; /* return first untreated operator */ -} - - -static void expr (LexState *ls, expdesc *v) { - subexpr(ls, v, 0); -} - -/* }==================================================================== */ - - - -/* -** {====================================================================== -** Rules for Statements -** ======================================================================= -*/ - - -static void block (LexState *ls) { - /* block -> statlist */ - FuncState *fs = ls->fs; - BlockCnt bl; - enterblock(fs, &bl, 0); - statlist(ls); - leaveblock(fs); -} - - -/* -** structure to chain all variables in the left-hand side of an -** assignment -*/ -struct LHS_assign { - struct LHS_assign *prev; - expdesc v; /* variable (global, local, upvalue, or indexed) */ -}; - - -/* -** check whether, in an assignment to an upvalue/local variable, the -** upvalue/local variable is begin used in a previous assignment to a -** table. If so, save original upvalue/local value in a safe place and -** use this safe copy in the previous assignment. -*/ -static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { - FuncState *fs = ls->fs; - int extra = fs->freereg; /* eventual position to save local variable */ - int conflict = 0; - for (; lh; lh = lh->prev) { /* check all previous assignments */ - if (vkisindexed(lh->v.k)) { /* assignment to table field? */ - if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ - if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { - conflict = 1; /* table is the upvalue being assigned now */ - lh->v.k = VINDEXSTR; - lh->v.u.ind.t = extra; /* assignment will use safe copy */ - } - } - else { /* table is a register */ - if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { - conflict = 1; /* table is the local being assigned now */ - lh->v.u.ind.t = extra; /* assignment will use safe copy */ - } - /* is index the local being assigned? */ - if (lh->v.k == VINDEXED && v->k == VLOCAL && - lh->v.u.ind.idx == v->u.var.ridx) { - conflict = 1; - lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ - } - } - } - } - if (conflict) { - /* copy upvalue/local value to a temporary (in position 'extra') */ - if (v->k == VLOCAL) - luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); - else - luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); - luaK_reserveregs(fs, 1); - } -} - -/* -** Parse and compile a multiple assignment. The first "variable" -** (a 'suffixedexp') was already read by the caller. -** -** assignment -> suffixedexp restassign -** restassign -> ',' suffixedexp restassign | '=' explist -*/ -static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { - expdesc e; - check_condition(ls, vkisvar(lh->v.k), "syntax error"); - check_readonly(ls, &lh->v); - if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ - struct LHS_assign nv; - nv.prev = lh; - suffixedexp(ls, &nv.v); - if (!vkisindexed(nv.v.k)) - check_conflict(ls, lh, &nv.v); - enterlevel(ls); /* control recursion depth */ - restassign(ls, &nv, nvars+1); - leavelevel(ls); - } - else { /* restassign -> '=' explist */ - int nexps; - checknext(ls, '='); - nexps = explist(ls, &e); - if (nexps != nvars) - adjust_assign(ls, nvars, nexps, &e); - else { - luaK_setoneret(ls->fs, &e); /* close last expression */ - luaK_storevar(ls->fs, &lh->v, &e); - return; /* avoid default */ - } - } - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ - luaK_storevar(ls->fs, &lh->v, &e); -} - - -static int cond (LexState *ls) { - /* cond -> exp */ - expdesc v; - expr(ls, &v); /* read condition */ - if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */ - luaK_goiftrue(ls->fs, &v); - return v.f; -} - - -static void gotostat (LexState *ls) { - FuncState *fs = ls->fs; - int line = ls->linenumber; - TString *name = str_checkname(ls); /* label's name */ - Labeldesc *lb = findlabel(ls, name); - if (lb == NULL) /* no label? */ - /* forward jump; will be resolved when the label is declared */ - newgotoentry(ls, name, line, luaK_jump(fs)); - else { /* found a label */ - /* backward jump; will be resolved here */ - int lblevel = reglevel(fs, lb->nactvar); /* label level */ - if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ - luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); - /* create jump and link it to the label */ - luaK_patchlist(fs, luaK_jump(fs), lb->pc); - } -} - - -/* -** Break statement. Semantically equivalent to "goto break". -*/ -static void breakstat (LexState *ls) { - int line = ls->linenumber; - luaX_next(ls); /* skip break */ - newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); -} - - -/* -** Check whether there is already a label with the given 'name'. -*/ -static void checkrepeated (LexState *ls, TString *name) { - Labeldesc *lb = findlabel(ls, name); - if (l_unlikely(lb != NULL)) { /* already defined? */ - const char *msg = "label '%s' already defined on line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); - luaK_semerror(ls, msg); /* error */ - } -} - - -static void labelstat (LexState *ls, TString *name, int line) { - /* label -> '::' NAME '::' */ - checknext(ls, TK_DBCOLON); /* skip double colon */ - while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) - statement(ls); /* skip other no-op statements */ - checkrepeated(ls, name); /* check for repeated labels */ - createlabel(ls, name, line, block_follow(ls, 0)); -} - - -static void whilestat (LexState *ls, int line) { - /* whilestat -> WHILE cond DO block END */ - FuncState *fs = ls->fs; - int whileinit; - int condexit; - BlockCnt bl; - luaX_next(ls); /* skip WHILE */ - whileinit = luaK_getlabel(fs); - condexit = cond(ls); - enterblock(fs, &bl, 1); - checknext(ls, TK_DO); - block(ls); - luaK_jumpto(fs, whileinit); - check_match(ls, TK_END, TK_WHILE, line); - leaveblock(fs); - luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ -} - - -static void repeatstat (LexState *ls, int line) { - /* repeatstat -> REPEAT block UNTIL cond */ - int condexit; - FuncState *fs = ls->fs; - int repeat_init = luaK_getlabel(fs); - BlockCnt bl1, bl2; - enterblock(fs, &bl1, 1); /* loop block */ - enterblock(fs, &bl2, 0); /* scope block */ - luaX_next(ls); /* skip REPEAT */ - statlist(ls); - check_match(ls, TK_UNTIL, TK_REPEAT, line); - condexit = cond(ls); /* read condition (inside scope block) */ - leaveblock(fs); /* finish scope */ - if (bl2.upval) { /* upvalues? */ - int exit = luaK_jump(fs); /* normal exit must jump over fix */ - luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ - luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); - condexit = luaK_jump(fs); /* repeat after closing upvalues */ - luaK_patchtohere(fs, exit); /* normal exit comes to here */ - } - luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ - leaveblock(fs); /* finish loop */ -} - - -/* -** Read an expression and generate code to put its results in next -** stack slot. -** -*/ -static void exp1 (LexState *ls) { - expdesc e; - expr(ls, &e); - luaK_exp2nextreg(ls->fs, &e); - lua_assert(e.k == VNONRELOC); -} - - -/* -** Fix for instruction at position 'pc' to jump to 'dest'. -** (Jump addresses are relative in Lua). 'back' true means -** a back jump. -*/ -static void fixforjump (FuncState *fs, int pc, int dest, int back) { - Instruction *jmp = &fs->f->code[pc]; - int offset = dest - (pc + 1); - if (back) - offset = -offset; - if (l_unlikely(offset > MAXARG_Bx)) - luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_Bx(*jmp, offset); -} - - -/* -** Generate code for a 'for' loop. -*/ -static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { - /* forbody -> DO block */ - static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; - static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; - BlockCnt bl; - FuncState *fs = ls->fs; - int prep, endfor; - checknext(ls, TK_DO); - prep = luaK_codeABx(fs, forprep[isgen], base, 0); - enterblock(fs, &bl, 0); /* scope for declared variables */ - adjustlocalvars(ls, nvars); - luaK_reserveregs(fs, nvars); - block(ls); - leaveblock(fs); /* end of scope for declared variables */ - fixforjump(fs, prep, luaK_getlabel(fs), 0); - if (isgen) { /* generic for? */ - luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); - luaK_fixline(fs, line); - } - endfor = luaK_codeABx(fs, forloop[isgen], base, 0); - fixforjump(fs, endfor, prep + 1, 1); - luaK_fixline(fs, line); -} - - -static void fornum (LexState *ls, TString *varname, int line) { - /* fornum -> NAME = exp,exp[,exp] forbody */ - FuncState *fs = ls->fs; - int base = fs->freereg; - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - new_localvar(ls, varname); - checknext(ls, '='); - exp1(ls); /* initial value */ - checknext(ls, ','); - exp1(ls); /* limit */ - if (testnext(ls, ',')) - exp1(ls); /* optional step */ - else { /* default step = 1 */ - luaK_int(fs, fs->freereg, 1); - luaK_reserveregs(fs, 1); - } - adjustlocalvars(ls, 3); /* control variables */ - forbody(ls, base, line, 1, 0); -} - - -static void forlist (LexState *ls, TString *indexname) { - /* forlist -> NAME {,NAME} IN explist forbody */ - FuncState *fs = ls->fs; - expdesc e; - int nvars = 5; /* gen, state, control, toclose, 'indexname' */ - int line; - int base = fs->freereg; - /* create control variables */ - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - /* create declared variables */ - new_localvar(ls, indexname); - while (testnext(ls, ',')) { - new_localvar(ls, str_checkname(ls)); - nvars++; - } - checknext(ls, TK_IN); - line = ls->linenumber; - adjust_assign(ls, 4, explist(ls, &e), &e); - adjustlocalvars(ls, 4); /* control variables */ - marktobeclosed(fs); /* last control var. must be closed */ - luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 4, 1); -} - - -static void forstat (LexState *ls, int line) { - /* forstat -> FOR (fornum | forlist) END */ - FuncState *fs = ls->fs; - TString *varname; - BlockCnt bl; - enterblock(fs, &bl, 1); /* scope for loop and control variables */ - luaX_next(ls); /* skip 'for' */ - varname = str_checkname(ls); /* first variable name */ - switch (ls->t.token) { - case '=': fornum(ls, varname, line); break; - case ',': case TK_IN: forlist(ls, varname); break; - default: luaX_syntaxerror(ls, "'=' or 'in' expected"); - } - check_match(ls, TK_END, TK_FOR, line); - leaveblock(fs); /* loop scope ('break' jumps to this point) */ -} - - -static void test_then_block (LexState *ls, int *escapelist) { - /* test_then_block -> [IF | ELSEIF] cond THEN block */ - BlockCnt bl; - FuncState *fs = ls->fs; - expdesc v; - int jf; /* instruction to skip 'then' code (if condition is false) */ - luaX_next(ls); /* skip IF or ELSEIF */ - expr(ls, &v); /* read condition */ - checknext(ls, TK_THEN); - if (ls->t.token == TK_BREAK) { /* 'if x then break' ? */ - int line = ls->linenumber; - luaK_goiffalse(ls->fs, &v); /* will jump if condition is true */ - luaX_next(ls); /* skip 'break' */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t); - while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* jump is the entire block? */ - leaveblock(fs); - return; /* and that is it */ - } - else /* must skip over 'then' part if condition is false */ - jf = luaK_jump(fs); - } - else { /* regular case (not a break) */ - luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ - enterblock(fs, &bl, 0); - jf = v.f; - } - statlist(ls); /* 'then' part */ - leaveblock(fs); - if (ls->t.token == TK_ELSE || - ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ - luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ - luaK_patchtohere(fs, jf); -} - - -static void ifstat (LexState *ls, int line) { - /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ - FuncState *fs = ls->fs; - int escapelist = NO_JUMP; /* exit list for finished parts */ - test_then_block(ls, &escapelist); /* IF cond THEN block */ - while (ls->t.token == TK_ELSEIF) - test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */ - if (testnext(ls, TK_ELSE)) - block(ls); /* 'else' part */ - check_match(ls, TK_END, TK_IF, line); - luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */ -} - - -static void localfunc (LexState *ls) { - expdesc b; - FuncState *fs = ls->fs; - int fvar = fs->nactvar; /* function's variable index */ - new_localvar(ls, str_checkname(ls)); /* new local variable */ - adjustlocalvars(ls, 1); /* enter its scope */ - body(ls, &b, 0, ls->linenumber); /* function created in next register */ - /* debug information will only see the variable after this point! */ - localdebuginfo(fs, fvar)->startpc = fs->pc; -} - - -static int getlocalattribute (LexState *ls) { - /* ATTRIB -> ['<' Name '>'] */ - if (testnext(ls, '<')) { - const char *attr = getstr(str_checkname(ls)); - checknext(ls, '>'); - if (strcmp(attr, "const") == 0) - return RDKCONST; /* read-only variable */ - else if (strcmp(attr, "close") == 0) - return RDKTOCLOSE; /* to-be-closed variable */ - else - luaK_semerror(ls, - luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); - } - return VDKREG; /* regular variable */ -} - - -static void checktoclose (FuncState *fs, int level) { - if (level != -1) { /* is there a to-be-closed variable? */ - marktobeclosed(fs); - luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); - } -} - - -static void localstat (LexState *ls) { - /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */ - FuncState *fs = ls->fs; - int toclose = -1; /* index of to-be-closed variable (if any) */ - Vardesc *var; /* last variable */ - int vidx, kind; /* index and kind of last variable */ - int nvars = 0; - int nexps; - expdesc e; - do { - vidx = new_localvar(ls, str_checkname(ls)); - kind = getlocalattribute(ls); - getlocalvardesc(fs, vidx)->vd.kind = kind; - if (kind == RDKTOCLOSE) { /* to-be-closed? */ - if (toclose != -1) /* one already present? */ - luaK_semerror(ls, "multiple to-be-closed variables in local list"); - toclose = fs->nactvar + nvars; - } - nvars++; - } while (testnext(ls, ',')); - if (testnext(ls, '=')) - nexps = explist(ls, &e); - else { - e.k = VVOID; - nexps = 0; - } - var = getlocalvardesc(fs, vidx); /* get last variable */ - if (nvars == nexps && /* no adjustments? */ - var->vd.kind == RDKCONST && /* last variable is const? */ - luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ - var->vd.kind = RDKCTC; /* variable is a compile-time constant */ - adjustlocalvars(ls, nvars - 1); /* exclude last variable */ - fs->nactvar++; /* but count it */ - } - else { - adjust_assign(ls, nvars, nexps, &e); - adjustlocalvars(ls, nvars); - } - checktoclose(fs, toclose); -} - - -static int funcname (LexState *ls, expdesc *v) { - /* funcname -> NAME {fieldsel} [':' NAME] */ - int ismethod = 0; - singlevar(ls, v); - while (ls->t.token == '.') - fieldsel(ls, v); - if (ls->t.token == ':') { - ismethod = 1; - fieldsel(ls, v); - } - return ismethod; -} - - -static void funcstat (LexState *ls, int line) { - /* funcstat -> FUNCTION funcname body */ - int ismethod; - expdesc v, b; - luaX_next(ls); /* skip FUNCTION */ - ismethod = funcname(ls, &v); - body(ls, &b, ismethod, line); - check_readonly(ls, &v); - luaK_storevar(ls->fs, &v, &b); - luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ -} - - -static void exprstat (LexState *ls) { - /* stat -> func | assignment */ - FuncState *fs = ls->fs; - struct LHS_assign v; - suffixedexp(ls, &v.v); - if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ - v.prev = NULL; - restassign(ls, &v, 1); - } - else { /* stat -> func */ - Instruction *inst; - check_condition(ls, v.v.k == VCALL, "syntax error"); - inst = &getinstruction(fs, &v.v); - SETARG_C(*inst, 1); /* call statement uses no results */ - } -} - - -static void retstat (LexState *ls) { - /* stat -> RETURN [explist] [';'] */ - FuncState *fs = ls->fs; - expdesc e; - int nret; /* number of values being returned */ - int first = luaY_nvarstack(fs); /* first slot to be returned */ - if (block_follow(ls, 1) || ls->t.token == ';') - nret = 0; /* return no values */ - else { - nret = explist(ls, &e); /* optional return values */ - if (hasmultret(e.k)) { - luaK_setmultret(fs, &e); - if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ - SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); - } - nret = LUA_MULTRET; /* return all values */ - } - else { - if (nret == 1) /* only one single value? */ - first = luaK_exp2anyreg(fs, &e); /* can use original slot */ - else { /* values must go to the top of the stack */ - luaK_exp2nextreg(fs, &e); - lua_assert(nret == fs->freereg - first); - } - } - } - luaK_ret(fs, first, nret); - testnext(ls, ';'); /* skip optional semicolon */ -} - - -static void statement (LexState *ls) { - int line = ls->linenumber; /* may be needed for error messages */ - enterlevel(ls); - switch (ls->t.token) { - case ';': { /* stat -> ';' (empty statement) */ - luaX_next(ls); /* skip ';' */ - break; - } - case TK_IF: { /* stat -> ifstat */ - ifstat(ls, line); - break; - } - case TK_WHILE: { /* stat -> whilestat */ - whilestat(ls, line); - break; - } - case TK_DO: { /* stat -> DO block END */ - luaX_next(ls); /* skip DO */ - block(ls); - check_match(ls, TK_END, TK_DO, line); - break; - } - case TK_FOR: { /* stat -> forstat */ - forstat(ls, line); - break; - } - case TK_REPEAT: { /* stat -> repeatstat */ - repeatstat(ls, line); - break; - } - case TK_FUNCTION: { /* stat -> funcstat */ - funcstat(ls, line); - break; - } - case TK_LOCAL: { /* stat -> localstat */ - luaX_next(ls); /* skip LOCAL */ - if (testnext(ls, TK_FUNCTION)) /* local function? */ - localfunc(ls); - else - localstat(ls); - break; - } - case TK_DBCOLON: { /* stat -> label */ - luaX_next(ls); /* skip double colon */ - labelstat(ls, str_checkname(ls), line); - break; - } - case TK_RETURN: { /* stat -> retstat */ - luaX_next(ls); /* skip RETURN */ - retstat(ls); - break; - } - case TK_BREAK: { /* stat -> breakstat */ - breakstat(ls); - break; - } - case TK_GOTO: { /* stat -> 'goto' NAME */ - luaX_next(ls); /* skip 'goto' */ - gotostat(ls); - break; - } - default: { /* stat -> func | assignment */ - exprstat(ls); - break; - } - } - lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= luaY_nvarstack(ls->fs)); - ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ - leavelevel(ls); -} - -/* }====================================================================== */ - - -/* -** compiles the main function, which is a regular vararg function with an -** upvalue named LUA_ENV -*/ -static void mainfunc (LexState *ls, FuncState *fs) { - BlockCnt bl; - Upvaldesc *env; - open_func(ls, fs, &bl); - setvararg(fs, 0); /* main function is always declared vararg */ - env = allocupvalue(fs); /* ...set environment upvalue */ - env->instack = 1; - env->idx = 0; - env->kind = VDKREG; - env->name = ls->envn; - luaC_objbarrier(ls->L, fs->f, env->name); - luaX_next(ls); /* read first token */ - statlist(ls); /* parse main body */ - check(ls, TK_EOS); - close_func(ls); -} - - -LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar) { - LexState lexstate; - FuncState funcstate; - LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ - luaD_inctop(L); - lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue2s(L, L->top, lexstate.h); /* anchor it */ - luaD_inctop(L); - funcstate.f = cl->p = luaF_newproto(L); - luaC_objbarrier(L, cl, cl->p); - funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ - luaC_objbarrier(L, funcstate.f, funcstate.f->source); - lexstate.buff = buff; - lexstate.dyd = dyd; - dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; - luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar); - mainfunc(&lexstate, &funcstate); - lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); - /* all scopes should be correctly finished */ - lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - L->top--; /* remove scanner's table */ - return cl; /* closure is on the stack, too */ -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.h deleted file mode 100644 index 5e4500f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lparser.h +++ /dev/null @@ -1,171 +0,0 @@ -/* -** $Id: lparser.h $ -** Lua Parser -** See Copyright Notice in lua.h -*/ - -#ifndef lparser_h -#define lparser_h - -#include "llimits.h" -#include "lobject.h" -#include "lzio.h" - - -/* -** Expression and variable descriptor. -** Code generation for variables and expressions can be delayed to allow -** optimizations; An 'expdesc' structure describes a potentially-delayed -** variable/expression. It has a description of its "main" value plus a -** list of conditional jumps that can also produce its value (generated -** by short-circuit operators 'and'/'or'). -*/ - -/* kinds of variables/expressions */ -typedef enum { - VVOID, /* when 'expdesc' describes the last expression of a list, - this kind means an empty list (so, no expression) */ - VNIL, /* constant nil */ - VTRUE, /* constant true */ - VFALSE, /* constant false */ - VK, /* constant in 'k'; info = index of constant in 'k' */ - VKFLT, /* floating constant; nval = numerical float value */ - VKINT, /* integer constant; ival = numerical integer value */ - VKSTR, /* string constant; strval = TString address; - (string is fixed by the lexer) */ - VNONRELOC, /* expression has its value in a fixed register; - info = result register */ - VLOCAL, /* local variable; var.ridx = register index; - var.vidx = relative index in 'actvar.arr' */ - VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ - VCONST, /* compile-time variable; - info = absolute index in 'actvar.arr' */ - VINDEXED, /* indexed variable; - ind.t = table register; - ind.idx = key's R index */ - VINDEXUP, /* indexed upvalue; - ind.t = table upvalue; - ind.idx = key's K index */ - VINDEXI, /* indexed variable with constant integer; - ind.t = table register; - ind.idx = key's value */ - VINDEXSTR, /* indexed variable with literal string; - ind.t = table register; - ind.idx = key's K index */ - VJMP, /* expression is a test/comparison; - info = pc of corresponding jump instruction */ - VRELOC, /* expression can put result in any register; - info = instruction pc */ - VCALL, /* expression is a function call; info = instruction pc */ - VVARARG /* vararg expression; info = instruction pc */ -} expkind; - - -#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) -#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) - - -typedef struct expdesc { - expkind k; - union { - lua_Integer ival; /* for VKINT */ - lua_Number nval; /* for VKFLT */ - TString *strval; /* for VKSTR */ - int info; /* for generic use */ - struct { /* for indexed variables */ - short idx; /* index (R or "long" K) */ - lu_byte t; /* table (register or upvalue) */ - } ind; - struct { /* for local variables */ - lu_byte ridx; /* register holding the variable */ - unsigned short vidx; /* compiler index (in 'actvar.arr') */ - } var; - } u; - int t; /* patch list of 'exit when true' */ - int f; /* patch list of 'exit when false' */ -} expdesc; - - -/* kinds of variables */ -#define VDKREG 0 /* regular */ -#define RDKCONST 1 /* constant */ -#define RDKTOCLOSE 2 /* to-be-closed */ -#define RDKCTC 3 /* compile-time constant */ - -/* description of an active local variable */ -typedef union Vardesc { - struct { - TValuefields; /* constant value (if it is a compile-time constant) */ - lu_byte kind; - lu_byte ridx; /* register holding the variable */ - short pidx; /* index of the variable in the Proto's 'locvars' array */ - TString *name; /* variable name */ - } vd; - TValue k; /* constant value (if any) */ -} Vardesc; - - - -/* description of pending goto statements and label statements */ -typedef struct Labeldesc { - TString *name; /* label identifier */ - int pc; /* position in code */ - int line; /* line where it appeared */ - lu_byte nactvar; /* number of active variables in that position */ - lu_byte close; /* goto that escapes upvalues */ -} Labeldesc; - - -/* list of labels or gotos */ -typedef struct Labellist { - Labeldesc *arr; /* array */ - int n; /* number of entries in use */ - int size; /* array size */ -} Labellist; - - -/* dynamic structures used by the parser */ -typedef struct Dyndata { - struct { /* list of all active local variables */ - Vardesc *arr; - int n; - int size; - } actvar; - Labellist gt; /* list of pending gotos */ - Labellist label; /* list of active labels */ -} Dyndata; - - -/* control of blocks */ -struct BlockCnt; /* defined in lparser.c */ - - -/* state needed to generate code for a given function */ -typedef struct FuncState { - Proto *f; /* current function header */ - struct FuncState *prev; /* enclosing function */ - struct LexState *ls; /* lexical state */ - struct BlockCnt *bl; /* chain of current blocks */ - int pc; /* next position to code (equivalent to 'ncode') */ - int lasttarget; /* 'label' of last 'jump label' */ - int previousline; /* last line that was saved in 'lineinfo' */ - int nk; /* number of elements in 'k' */ - int np; /* number of elements in 'p' */ - int nabslineinfo; /* number of elements in 'abslineinfo' */ - int firstlocal; /* index of first local var (in Dyndata array) */ - int firstlabel; /* index of first label (in 'dyd->label->arr') */ - short ndebugvars; /* number of elements in 'f->locvars' */ - lu_byte nactvar; /* number of active local variables */ - lu_byte nups; /* number of upvalues */ - lu_byte freereg; /* first free register */ - lu_byte iwthabs; /* instructions issued since last absolute line info */ - lu_byte needclose; /* function needs to close upvalues when returning */ -} FuncState; - - -LUAI_FUNC int luaY_nvarstack (FuncState *fs); -LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, - Dyndata *dyd, const char *name, int firstchar); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lprefix.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lprefix.h deleted file mode 100644 index 484f2ad..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lprefix.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -** $Id: lprefix.h $ -** Definitions for Lua code that must come before any other header file -** See Copyright Notice in lua.h -*/ - -#ifndef lprefix_h -#define lprefix_h - - -/* -** Allows POSIX/XSI stuff -*/ -#if !defined(LUA_USE_C89) /* { */ - -#if !defined(_XOPEN_SOURCE) -#define _XOPEN_SOURCE 600 -#elif _XOPEN_SOURCE == 0 -#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ -#endif - -/* -** Allows manipulation of large files in gcc and some other compilers -*/ -#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) -#define _LARGEFILE_SOURCE 1 -#define _FILE_OFFSET_BITS 64 -#endif - -#endif /* } */ - - -/* -** Windows stuff -*/ -#if defined(_WIN32) /* { */ - -#if !defined(_CRT_SECURE_NO_WARNINGS) -#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ -#endif - -#endif /* } */ - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.c deleted file mode 100644 index 1ffe1a0..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.c +++ /dev/null @@ -1,440 +0,0 @@ -/* -** $Id: lstate.c $ -** Global State -** See Copyright Notice in lua.h -*/ - -#define lstate_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "lapi.h" -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "llex.h" -#include "lmem.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" - - - -/* -** thread state + extra space -*/ -typedef struct LX { - lu_byte extra_[LUA_EXTRASPACE]; - lua_State l; -} LX; - - -/* -** Main thread combines a thread state and the global state -*/ -typedef struct LG { - LX l; - global_State g; -} LG; - - - -#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) - - -/* -** A macro to create a "random" seed when a state is created; -** the seed is used to randomize string hashes. -*/ -#if !defined(luai_makeseed) - -#include - -/* -** Compute an initial seed with some level of randomness. -** Rely on Address Space Layout Randomization (if present) and -** current time. -*/ -#define addbuff(b,p,e) \ - { size_t t = cast_sizet(e); \ - memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int luai_makeseed (lua_State *L) { - char buff[3 * sizeof(size_t)]; - unsigned int h = cast_uint(time(NULL)); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} - -#endif - - -/* -** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant (and avoiding underflows in 'totalbytes') -*/ -void luaE_setdebt (global_State *g, l_mem debt) { - l_mem tb = gettotalbytes(g); - lua_assert(tb > 0); - if (debt < tb - MAX_LMEM) - debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ - g->totalbytes = tb - debt; - g->GCdebt = debt; -} - - -LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { - UNUSED(L); UNUSED(limit); - return LUAI_MAXCCALLS; /* warning?? */ -} - - -CallInfo *luaE_extendCI (lua_State *L) { - CallInfo *ci; - lua_assert(L->ci->next == NULL); - ci = luaM_new(L, CallInfo); - lua_assert(L->ci->next == NULL); - L->ci->next = ci; - ci->previous = L->ci; - ci->next = NULL; - ci->u.l.trap = 0; - L->nci++; - return ci; -} - - -/* -** free all CallInfo structures not in use by a thread -*/ -void luaE_freeCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next = ci->next; - ci->next = NULL; - while ((ci = next) != NULL) { - next = ci->next; - luaM_free(L, ci); - L->nci--; - } -} - - -/* -** free half of the CallInfo structures not in use by a thread, -** keeping the first one. -*/ -void luaE_shrinkCI (lua_State *L) { - CallInfo *ci = L->ci->next; /* first free CallInfo */ - CallInfo *next; - if (ci == NULL) - return; /* no extra elements */ - while ((next = ci->next) != NULL) { /* two extra elements? */ - CallInfo *next2 = next->next; /* next's next */ - ci->next = next2; /* remove next from the list */ - L->nci--; - luaM_free(L, next); /* free next */ - if (next2 == NULL) - break; /* no more elements */ - else { - next2->previous = ci; - ci = next2; /* continue */ - } - } -} - - -/* -** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. -** If equal, raises an overflow error. If value is larger than -** LUAI_MAXCCALLS (which means it is handling an overflow) but -** not much larger, does not report an error (to allow overflow -** handling to work). -*/ -void luaE_checkcstack (lua_State *L) { - if (getCcalls(L) == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) - luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ -} - - -LUAI_FUNC void luaE_incCstack (lua_State *L) { - L->nCcalls++; - if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) - luaE_checkcstack(L); -} - - -static void stack_init (lua_State *L1, lua_State *L) { - int i; CallInfo *ci; - /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - L1->tbclist = L1->stack; - for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) - setnilvalue(s2v(L1->stack + i)); /* erase new stack */ - L1->top = L1->stack; - L1->stack_last = L1->stack + BASIC_STACK_SIZE; - /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - ci->callstatus = CIST_C; - ci->func = L1->top; - ci->u.c.k = NULL; - ci->nresults = 0; - setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ - L1->top++; - ci->top = L1->top + LUA_MINSTACK; - L1->ci = ci; -} - - -static void freestack (lua_State *L) { - if (L->stack == NULL) - return; /* stack not completely built yet */ - L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); - lua_assert(L->nci == 0); - luaM_freearray(L, L->stack, stacksize(L) + EXTRA_STACK); /* free stack */ -} - - -/* -** Create registry table and its predefined values -*/ -static void init_registry (lua_State *L, global_State *g) { - /* create registry */ - Table *registry = luaH_new(L); - sethvalue(L, &g->l_registry, registry); - luaH_resize(L, registry, LUA_RIDX_LAST, 0); - /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L); - /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ - sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); -} - - -/* -** open parts of the state that may cause memory-allocation errors. -*/ -static void f_luaopen (lua_State *L, void *ud) { - global_State *g = G(L); - UNUSED(ud); - stack_init(L, L); /* init stack */ - init_registry(L, g); - luaS_init(L); - luaT_init(L); - luaX_init(L); - g->gcstp = 0; /* allow gc */ - setnilvalue(&g->nilvalue); /* now state is complete */ - luai_userstateopen(L); -} - - -/* -** preinitialize a thread with consistent values without allocating -** any memory (to avoid errors) -*/ -static void preinit_thread (lua_State *L, global_State *g) { - G(L) = g; - L->stack = NULL; - L->ci = NULL; - L->nci = 0; - L->twups = L; /* thread has no upvalues */ - L->nCcalls = 0; - L->errorJmp = NULL; - L->hook = NULL; - L->hookmask = 0; - L->basehookcount = 0; - L->allowhook = 1; - resethookcount(L); - L->openupval = NULL; - L->status = LUA_OK; - L->errfunc = 0; - L->oldpc = 0; -} - - -static void close_state (lua_State *L) { - global_State *g = G(L); - if (!completestate(g)) /* closing a partially built state? */ - luaC_freeallobjects(L); /* just collect its objects */ - else { /* closing a fully built state */ - L->ci = &L->base_ci; /* unwind CallInfo list */ - luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ - luaC_freeallobjects(L); /* collect all objects */ - luai_userstateclose(L); - } - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); - freestack(L); - lua_assert(gettotalbytes(g) == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ -} - - -LUA_API lua_State *lua_newthread (lua_State *L) { - global_State *g; - lua_State *L1; - lua_lock(L); - g = G(L); - luaC_checkGC(L); - /* create new thread */ - L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; - L1->marked = luaC_white(g); - L1->tt = LUA_VTHREAD; - /* link it on list 'allgc' */ - L1->next = g->allgc; - g->allgc = obj2gco(L1); - /* anchor it on L stack */ - setthvalue2s(L, L->top, L1); - api_incr_top(L); - preinit_thread(L1, g); - L1->hookmask = L->hookmask; - L1->basehookcount = L->basehookcount; - L1->hook = L->hook; - resethookcount(L1); - /* initialize L1 extra space */ - memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), - LUA_EXTRASPACE); - luai_userstatethread(L, L1); - stack_init(L1, L); /* init stack */ - lua_unlock(L); - return L1; -} - - -void luaE_freethread (lua_State *L, lua_State *L1) { - LX *l = fromstate(L1); - luaF_closeupval(L1, L1->stack); /* close all upvalues */ - lua_assert(L1->openupval == NULL); - luai_userstatefree(L, L1); - freestack(L1); - luaM_free(L, l); -} - - -int luaE_resetthread (lua_State *L, int status) { - CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ - setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ - ci->func = L->stack; - ci->callstatus = CIST_C; - if (status == LUA_YIELD) - status = LUA_OK; - L->status = LUA_OK; /* so it can run __close metamethods */ - status = luaD_closeprotected(L, 1, status); - if (status != LUA_OK) /* errors? */ - luaD_seterrorobj(L, status, L->stack + 1); - else - L->top = L->stack + 1; - ci->top = L->top + LUA_MINSTACK; - luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); - return status; -} - - -LUA_API int lua_resetthread (lua_State *L) { - int status; - lua_lock(L); - status = luaE_resetthread(L, L->status); - lua_unlock(L); - return status; -} - - -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { - int i; - lua_State *L; - global_State *g; - LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); - if (l == NULL) return NULL; - L = &l->l.l; - g = &l->g; - L->tt = LUA_VTHREAD; - g->currentwhite = bitmask(WHITE0BIT); - L->marked = luaC_white(g); - preinit_thread(L, g); - g->allgc = obj2gco(L); /* by now, only object is the main thread */ - L->next = NULL; - incnny(L); /* main thread is always non yieldable */ - g->frealloc = f; - g->ud = ud; - g->warnf = NULL; - g->ud_warn = NULL; - g->mainthread = L; - g->seed = luai_makeseed(L); - g->gcstp = GCSTPGC; /* no GC while building state */ - g->strt.size = g->strt.nuse = 0; - g->strt.hash = NULL; - setnilvalue(&g->l_registry); - g->panic = NULL; - g->gcstate = GCSpause; - g->gckind = KGC_INC; - g->gcstopem = 0; - g->gcemergency = 0; - g->finobj = g->tobefnz = g->fixedgc = NULL; - g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; - g->finobjsur = g->finobjold1 = g->finobjrold = NULL; - g->sweepgc = NULL; - g->gray = g->grayagain = NULL; - g->weak = g->ephemeron = g->allweak = NULL; - g->twups = NULL; - g->totalbytes = sizeof(LG); - g->GCdebt = 0; - g->lastatomic = 0; - setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ - setgcparam(g->gcpause, LUAI_GCPAUSE); - setgcparam(g->gcstepmul, LUAI_GCMUL); - g->gcstepsize = LUAI_GCSTEPSIZE; - setgcparam(g->genmajormul, LUAI_GENMAJORMUL); - g->genminormul = LUAI_GENMINORMUL; - for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; - if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { - /* memory allocation error: free partial state */ - close_state(L); - L = NULL; - } - return L; -} - - -LUA_API void lua_close (lua_State *L) { - lua_lock(L); - L = G(L)->mainthread; /* only the main thread can be closed */ - close_state(L); -} - - -void luaE_warning (lua_State *L, const char *msg, int tocont) { - lua_WarnFunction wf = G(L)->warnf; - if (wf != NULL) - wf(G(L)->ud_warn, msg, tocont); -} - - -/* -** Generate a warning from an error message -*/ -void luaE_warnerror (lua_State *L, const char *where) { - TValue *errobj = s2v(L->top - 1); /* error object */ - const char *msg = (ttisstring(errobj)) - ? svalue(errobj) - : "error object is not a string"; - /* produce warning "error in %s (%s)" (where, msg) */ - luaE_warning(L, "error in ", 1); - luaE_warning(L, where, 1); - luaE_warning(L, " (", 1); - luaE_warning(L, msg, 1); - luaE_warning(L, ")", 0); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.h deleted file mode 100644 index 61e82cd..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstate.h +++ /dev/null @@ -1,404 +0,0 @@ -/* -** $Id: lstate.h $ -** Global State -** See Copyright Notice in lua.h -*/ - -#ifndef lstate_h -#define lstate_h - -#include "lua.h" - -#include "lobject.h" -#include "ltm.h" -#include "lzio.h" - - -/* -** Some notes about garbage-collected objects: All objects in Lua must -** be kept somehow accessible until being freed, so all objects always -** belong to one (and only one) of these lists, using field 'next' of -** the 'CommonHeader' for the link: -** -** 'allgc': all objects not marked for finalization; -** 'finobj': all objects marked for finalization; -** 'tobefnz': all objects ready to be finalized; -** 'fixedgc': all objects that are not to be collected (currently -** only small strings, such as reserved words). -** -** For the generational collector, some of these lists have marks for -** generations. Each mark points to the first element in the list for -** that particular generation; that generation goes until the next mark. -** -** 'allgc' -> 'survival': new objects; -** 'survival' -> 'old': objects that survived one collection; -** 'old1' -> 'reallyold': objects that became old in last collection; -** 'reallyold' -> NULL: objects old for more than one cycle. -** -** 'finobj' -> 'finobjsur': new objects marked for finalization; -** 'finobjsur' -> 'finobjold1': survived """"; -** 'finobjold1' -> 'finobjrold': just old """"; -** 'finobjrold' -> NULL: really old """". -** -** All lists can contain elements older than their main ages, due -** to 'luaC_checkfinalizer' and 'udata2finalize', which move -** objects between the normal lists and the "marked for finalization" -** lists. Moreover, barriers can age young objects in young lists as -** OLD0, which then become OLD1. However, a list never contains -** elements younger than their main ages. -** -** The generational collector also uses a pointer 'firstold1', which -** points to the first OLD1 object in the list. It is used to optimize -** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' -** and 'reallyold', but often the list has no OLD1 objects or they are -** after 'old1'.) Note the difference between it and 'old1': -** 'firstold1': no OLD1 objects before this point; there can be all -** ages after it. -** 'old1': no objects younger than OLD1 after this point. -*/ - -/* -** Moreover, there is another set of lists that control gray objects. -** These lists are linked by fields 'gclist'. (All objects that -** can become gray have such a field. The field is not the same -** in all objects, but it always has this name.) Any gray object -** must belong to one of these lists, and all objects in these lists -** must be gray (with two exceptions explained below): -** -** 'gray': regular gray objects, still waiting to be visited. -** 'grayagain': objects that must be revisited at the atomic phase. -** That includes -** - black objects got in a write barrier; -** - all kinds of weak tables during propagation phase; -** - all threads. -** 'weak': tables with weak values to be cleared; -** 'ephemeron': ephemeron tables with white->white entries; -** 'allweak': tables with weak keys and/or weak values to be cleared. -** -** The exceptions to that "gray rule" are: -** - TOUCHED2 objects in generational mode stay in a gray list (because -** they must be visited again at the end of the cycle), but they are -** marked black because assignments to them must activate barriers (to -** move them back to TOUCHED1). -** - Open upvales are kept gray to avoid barriers, but they stay out -** of gray lists. (They don't even have a 'gclist' field.) -*/ - - - -/* -** About 'nCcalls': This count has two parts: the lower 16 bits counts -** the number of recursive invocations in the C stack; the higher -** 16 bits counts the number of non-yieldable calls in the stack. -** (They are together so that we can change and save both with one -** instruction.) -*/ - - -/* true if this thread does not have non-yieldable calls in the stack */ -#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) - -/* real number of C calls */ -#define getCcalls(L) ((L)->nCcalls & 0xffff) - - -/* Increment the number of non-yieldable calls */ -#define incnny(L) ((L)->nCcalls += 0x10000) - -/* Decrement the number of non-yieldable calls */ -#define decnny(L) ((L)->nCcalls -= 0x10000) - -/* Non-yieldable call increment */ -#define nyci (0x10000 | 1) - - - - -struct lua_longjmp; /* defined in ldo.c */ - - -/* -** Atomic type (relative to signals) to better ensure that 'lua_sethook' -** is thread safe -*/ -#if !defined(l_signalT) -#include -#define l_signalT sig_atomic_t -#endif - - -/* -** Extra stack space to handle TM calls and some other extras. This -** space is not included in 'stack_last'. It is used only to avoid stack -** checks, either because the element will be promptly popped or because -** there will be a stack check soon after the push. Function frames -** never use this extra space, so it does not need to be kept clean. -*/ -#define EXTRA_STACK 5 - - -#define BASIC_STACK_SIZE (2*LUA_MINSTACK) - -#define stacksize(th) cast_int((th)->stack_last - (th)->stack) - - -/* kinds of Garbage Collection */ -#define KGC_INC 0 /* incremental gc */ -#define KGC_GEN 1 /* generational gc */ - - -typedef struct stringtable { - TString **hash; - int nuse; /* number of elements */ - int size; -} stringtable; - - -/* -** Information about a call. -** About union 'u': -** - field 'l' is used only for Lua functions; -** - field 'c' is used only for C functions. -** About union 'u2': -** - field 'funcidx' is used only by C functions while doing a -** protected call; -** - field 'nyield' is used only while a function is "doing" an -** yield (from the yield until the next resume); -** - field 'nres' is used only while closing tbc variables when -** returning from a function; -** - field 'transferinfo' is used only during call/returnhooks, -** before the function starts or after it ends. -*/ -typedef struct CallInfo { - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ - struct CallInfo *previous, *next; /* dynamic call link */ - union { - struct { /* only for Lua functions */ - const Instruction *savedpc; - volatile l_signalT trap; - int nextraargs; /* # of extra arguments in vararg functions */ - } l; - struct { /* only for C functions */ - lua_KFunction k; /* continuation in case of yields */ - ptrdiff_t old_errfunc; - lua_KContext ctx; /* context info. in case of yields */ - } c; - } u; - union { - int funcidx; /* called-function index */ - int nyield; /* number of values yielded */ - int nres; /* number of values returned */ - struct { /* info about transferred values (for call/return hooks) */ - unsigned short ftransfer; /* offset of first value transferred */ - unsigned short ntransfer; /* number of values transferred */ - } transferinfo; - } u2; - short nresults; /* expected number of results from this function */ - unsigned short callstatus; -} CallInfo; - - -/* -** Bits in CallInfo status -*/ -#define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_C (1<<1) /* call is running a C function */ -#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ -#define CIST_HOOKED (1<<3) /* call is running a debug hook */ -#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ -#define CIST_TAIL (1<<5) /* call was tail called */ -#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_FIN (1<<7) /* function "called" a finalizer */ -#define CIST_TRAN (1<<8) /* 'ci' has transfer information */ -#define CIST_CLSRET (1<<9) /* function is closing tbc variables */ -/* Bits 10-12 are used for CIST_RECST (see below) */ -#define CIST_RECST 10 -#if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<13) /* using __lt for __le */ -#endif - - -/* -** Field CIST_RECST stores the "recover status", used to keep the error -** status while closing to-be-closed variables in coroutines, so that -** Lua can correctly resume after an yield from a __close method called -** because of an error. (Three bits are enough for error status.) -*/ -#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) -#define setcistrecst(ci,st) \ - check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ - ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \ - | ((st) << CIST_RECST))) - - -/* active function is a Lua function */ -#define isLua(ci) (!((ci)->callstatus & CIST_C)) - -/* call is running Lua code (not a hook) */ -#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) - -/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ -#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) -#define getoah(st) ((st) & CIST_OAH) - - -/* -** 'global state', shared by all threads of this state -*/ -typedef struct global_State { - lua_Alloc frealloc; /* function to reallocate memory */ - void *ud; /* auxiliary data to 'frealloc' */ - l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ - l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ - lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ - lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ - stringtable strt; /* hash table for strings */ - TValue l_registry; - TValue nilvalue; /* a nil value */ - unsigned int seed; /* randomized seed for hashes */ - lu_byte currentwhite; - lu_byte gcstate; /* state of garbage collector */ - lu_byte gckind; /* kind of GC running */ - lu_byte gcstopem; /* stops emergency collections */ - lu_byte genminormul; /* control for minor generational collections */ - lu_byte genmajormul; /* control for major generational collections */ - lu_byte gcstp; /* control whether GC is running */ - lu_byte gcemergency; /* true if this is an emergency collection */ - lu_byte gcpause; /* size of pause between successive GCs */ - lu_byte gcstepmul; /* GC "speed" */ - lu_byte gcstepsize; /* (log2 of) GC granularity */ - GCObject *allgc; /* list of all collectable objects */ - GCObject **sweepgc; /* current position of sweep in list */ - GCObject *finobj; /* list of collectable objects with finalizers */ - GCObject *gray; /* list of gray objects */ - GCObject *grayagain; /* list of objects to be traversed atomically */ - GCObject *weak; /* list of tables with weak values */ - GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ - GCObject *allweak; /* list of all-weak tables */ - GCObject *tobefnz; /* list of userdata to be GC */ - GCObject *fixedgc; /* list of objects not to be collected */ - /* fields for generational collector */ - GCObject *survival; /* start of objects that survived one GC cycle */ - GCObject *old1; /* start of old1 objects */ - GCObject *reallyold; /* objects more than one cycle old ("really old") */ - GCObject *firstold1; /* first OLD1 object in the list (if any) */ - GCObject *finobjsur; /* list of survival objects with finalizers */ - GCObject *finobjold1; /* list of old1 objects with finalizers */ - GCObject *finobjrold; /* list of really old objects with finalizers */ - struct lua_State *twups; /* list of threads with open upvalues */ - lua_CFunction panic; /* to be called in unprotected errors */ - struct lua_State *mainthread; - TString *memerrmsg; /* message for memory-allocation errors */ - TString *tmname[TM_N]; /* array with tag-method names */ - struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ - TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ - lua_WarnFunction warnf; /* warning function */ - void *ud_warn; /* auxiliary data to 'warnf' */ -} global_State; - - -/* -** 'per thread' state -*/ -struct lua_State { - CommonHeader; - lu_byte status; - lu_byte allowhook; - unsigned short nci; /* number of items in 'ci' list */ - StkId top; /* first free slot in the stack */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - StkId stack_last; /* end of stack (last element + 1) */ - StkId stack; /* stack base */ - UpVal *openupval; /* list of open upvalues in this stack */ - StkId tbclist; /* list of to-be-closed variables */ - GCObject *gclist; - struct lua_State *twups; /* list of threads with open upvalues */ - struct lua_longjmp *errorJmp; /* current error recover point */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ - volatile lua_Hook hook; - ptrdiff_t errfunc; /* current error handling function (stack index) */ - l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ - int oldpc; /* last pc traced */ - int basehookcount; - int hookcount; - volatile l_signalT hookmask; -}; - - -#define G(L) (L->l_G) - -/* -** 'g->nilvalue' being a nil value flags that the state was completely -** build. -*/ -#define completestate(g) ttisnil(&g->nilvalue) - - -/* -** Union of all collectable objects (only for conversions) -** ISO C99, 6.5.2.3 p.5: -** "if a union contains several structures that share a common initial -** sequence [...], and if the union object currently contains one -** of these structures, it is permitted to inspect the common initial -** part of any of them anywhere that a declaration of the complete type -** of the union is visible." -*/ -union GCUnion { - GCObject gc; /* common header */ - struct TString ts; - struct Udata u; - union Closure cl; - struct Table h; - struct Proto p; - struct lua_State th; /* thread */ - struct UpVal upv; -}; - - -/* -** ISO C99, 6.7.2.1 p.14: -** "A pointer to a union object, suitably converted, points to each of -** its members [...], and vice versa." -*/ -#define cast_u(o) cast(union GCUnion *, (o)) - -/* macros to convert a GCObject into a specific value */ -#define gco2ts(o) \ - check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) -#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) -#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) -#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) -#define gco2cl(o) \ - check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) -#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) -#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) -#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) -#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) - - -/* -** macro to convert a Lua object into a GCObject -** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) -*/ -#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) - - -/* actual number of total bytes allocated */ -#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) - -LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); -LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); -LUAI_FUNC void luaE_freeCI (lua_State *L); -LUAI_FUNC void luaE_shrinkCI (lua_State *L); -LUAI_FUNC void luaE_checkcstack (lua_State *L); -LUAI_FUNC void luaE_incCstack (lua_State *L); -LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); -LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); -LUAI_FUNC int luaE_resetthread (lua_State *L, int status); - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.c deleted file mode 100644 index 13dcaf4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.c +++ /dev/null @@ -1,273 +0,0 @@ -/* -** $Id: lstring.c $ -** String table (keeps all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - -#define lstring_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" - - -/* -** Maximum size for string table. -*/ -#define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*)) - - -/* -** equality for long strings -*/ -int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->u.lnglen; - lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); - return (a == b) || /* same instance or... */ - ((len == b->u.lnglen) && /* equal length and ... */ - (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ -} - - -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { - unsigned int h = seed ^ cast_uint(l); - for (; l > 0; l--) - h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); - return h; -} - - -unsigned int luaS_hashlongstr (TString *ts) { - lua_assert(ts->tt == LUA_VLNGSTR); - if (ts->extra == 0) { /* no hash? */ - size_t len = ts->u.lnglen; - ts->hash = luaS_hash(getstr(ts), len, ts->hash); - ts->extra = 1; /* now it has its hash */ - } - return ts->hash; -} - - -static void tablerehash (TString **vect, int osize, int nsize) { - int i; - for (i = osize; i < nsize; i++) /* clear new elements */ - vect[i] = NULL; - for (i = 0; i < osize; i++) { /* rehash old part of the array */ - TString *p = vect[i]; - vect[i] = NULL; - while (p) { /* for each string in the list */ - TString *hnext = p->u.hnext; /* save next */ - unsigned int h = lmod(p->hash, nsize); /* new position */ - p->u.hnext = vect[h]; /* chain it into array */ - vect[h] = p; - p = hnext; - } - } -} - - -/* -** Resize the string table. If allocation fails, keep the current size. -** (This can degrade performance, but any non-zero size should work -** correctly.) -*/ -void luaS_resize (lua_State *L, int nsize) { - stringtable *tb = &G(L)->strt; - int osize = tb->size; - TString **newvect; - if (nsize < osize) /* shrinking table? */ - tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ - newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); - if (l_unlikely(newvect == NULL)) { /* reallocation failed? */ - if (nsize < osize) /* was it shrinking table? */ - tablerehash(tb->hash, nsize, osize); /* restore to original size */ - /* leave table as it was */ - } - else { /* allocation succeeded */ - tb->hash = newvect; - tb->size = nsize; - if (nsize > osize) - tablerehash(newvect, osize, nsize); /* rehash for new size */ - } -} - - -/* -** Clear API string cache. (Entries cannot be empty, so fill them with -** a non-collectable string.) -*/ -void luaS_clearcache (global_State *g) { - int i, j; - for (i = 0; i < STRCACHE_N; i++) - for (j = 0; j < STRCACHE_M; j++) { - if (iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ - } -} - - -/* -** Initialize the string table and the string cache -*/ -void luaS_init (lua_State *L) { - global_State *g = G(L); - int i, j; - stringtable *tb = &G(L)->strt; - tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); - tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ - tb->size = MINSTRTABSIZE; - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ - for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ - for (j = 0; j < STRCACHE_M; j++) - g->strcache[i][j] = g->memerrmsg; -} - - - -/* -** creates a new string object -*/ -static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { - TString *ts; - GCObject *o; - size_t totalsize; /* total size of TString object */ - totalsize = sizelstring(l); - o = luaC_newobj(L, tag, totalsize); - ts = gco2ts(o); - ts->hash = h; - ts->extra = 0; - getstr(ts)[l] = '\0'; /* ending 0 */ - return ts; -} - - -TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed); - ts->u.lnglen = l; - return ts; -} - - -void luaS_remove (lua_State *L, TString *ts) { - stringtable *tb = &G(L)->strt; - TString **p = &tb->hash[lmod(ts->hash, tb->size)]; - while (*p != ts) /* find previous element */ - p = &(*p)->u.hnext; - *p = (*p)->u.hnext; /* remove element from its list */ - tb->nuse--; -} - - -static void growstrtab (lua_State *L, stringtable *tb) { - if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ - luaC_fullgc(L, 1); /* try to free some... */ - if (tb->nuse == MAX_INT) /* still too many? */ - luaM_error(L); /* cannot even create a message... */ - } - if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ - luaS_resize(L, tb->size * 2); -} - - -/* -** Checks whether short string exists and reuses it or creates a new one. -*/ -static TString *internshrstr (lua_State *L, const char *str, size_t l) { - TString *ts; - global_State *g = G(L); - stringtable *tb = &g->strt; - unsigned int h = luaS_hash(str, l, g->seed); - TString **list = &tb->hash[lmod(h, tb->size)]; - lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ - for (ts = *list; ts != NULL; ts = ts->u.hnext) { - if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { - /* found! */ - if (isdead(g, ts)) /* dead (but not collected yet)? */ - changewhite(ts); /* resurrect it */ - return ts; - } - } - /* else must create a new string */ - if (tb->nuse >= tb->size) { /* need to grow string table? */ - growstrtab(L, tb); - list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ - } - ts = createstrobj(L, l, LUA_VSHRSTR, h); - memcpy(getstr(ts), str, l * sizeof(char)); - ts->shrlen = cast_byte(l); - ts->u.hnext = *list; - *list = ts; - tb->nuse++; - return ts; -} - - -/* -** new string (with explicit length) -*/ -TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { - if (l <= LUAI_MAXSHORTLEN) /* short string? */ - return internshrstr(L, str, l); - else { - TString *ts; - if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) - luaM_toobig(L); - ts = luaS_createlngstrobj(L, l); - memcpy(getstr(ts), str, l * sizeof(char)); - return ts; - } -} - - -/* -** Create or reuse a zero-terminated string, first checking in the -** cache (using the string address as a key). The cache can contain -** only zero-terminated strings, so it is safe to use 'strcmp' to -** check hits. -*/ -TString *luaS_new (lua_State *L, const char *str) { - unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ - int j; - TString **p = G(L)->strcache[i]; - for (j = 0; j < STRCACHE_M; j++) { - if (strcmp(str, getstr(p[j])) == 0) /* hit? */ - return p[j]; /* that is it */ - } - /* normal route */ - for (j = STRCACHE_M - 1; j > 0; j--) - p[j] = p[j - 1]; /* move out last element */ - /* new element is first in the list */ - p[0] = luaS_newlstr(L, str, strlen(str)); - return p[0]; -} - - -Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { - Udata *u; - int i; - GCObject *o; - if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) - luaM_toobig(L); - o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); - u = gco2u(o); - u->len = s; - u->nuvalue = nuvalue; - u->metatable = NULL; - for (i = 0; i < nuvalue; i++) - setnilvalue(&u->uv[i].uv); - return u; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.h deleted file mode 100644 index 450c239..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstring.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -** $Id: lstring.h $ -** String table (keep all strings handled by Lua) -** See Copyright Notice in lua.h -*/ - -#ifndef lstring_h -#define lstring_h - -#include "lgc.h" -#include "lobject.h" -#include "lstate.h" - - -/* -** Memory-allocation error message must be preallocated (it cannot -** be created after memory is exhausted) -*/ -#define MEMERRMSG "not enough memory" - - -/* -** Size of a TString: Size of the header plus space for the string -** itself (including final '\0'). -*/ -#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) - -#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ - (sizeof(s)/sizeof(char))-1)) - - -/* -** test whether a string is a reserved word -*/ -#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) - - -/* -** equality for short strings, which are always internalized -*/ -#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) - - -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); -LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); -LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); -LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC void luaS_clearcache (global_State *g); -LUAI_FUNC void luaS_init (lua_State *L); -LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); -LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); -LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); -LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstrlib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstrlib.c deleted file mode 100644 index 0b4fdbb..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lstrlib.c +++ /dev/null @@ -1,1874 +0,0 @@ -/* -** $Id: lstrlib.c $ -** Standard library for string operations and pattern-matching -** See Copyright Notice in lua.h -*/ - -#define lstrlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** maximum number of captures that a pattern can do during -** pattern-matching. This limit is arbitrary, but must fit in -** an unsigned char. -*/ -#if !defined(LUA_MAXCAPTURES) -#define LUA_MAXCAPTURES 32 -#endif - - -/* macro to 'unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - -/* -** Some sizes are better limited to fit in 'int', but must also fit in -** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) -*/ -#define MAX_SIZET ((size_t)(~(size_t)0)) - -#define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) - - - - -static int str_len (lua_State *L) { - size_t l; - luaL_checklstring(L, 1, &l); - lua_pushinteger(L, (lua_Integer)l); - return 1; -} - - -/* -** translate a relative initial string position -** (negative means back from end): clip result to [1, inf). -** The length of any string in Lua must fit in a lua_Integer, -** so there are no overflows in the casts. -** The inverted comparison avoids a possible overflow -** computing '-pos'. -*/ -static size_t posrelatI (lua_Integer pos, size_t len) { - if (pos > 0) - return (size_t)pos; - else if (pos == 0) - return 1; - else if (pos < -(lua_Integer)len) /* inverted comparison */ - return 1; /* clip to 1 */ - else return len + (size_t)pos + 1; -} - - -/* -** Gets an optional ending string position from argument 'arg', -** with default value 'def'. -** Negative means back from end: clip result to [0, len] -*/ -static size_t getendpos (lua_State *L, int arg, lua_Integer def, - size_t len) { - lua_Integer pos = luaL_optinteger(L, arg, def); - if (pos > (lua_Integer)len) - return len; - else if (pos >= 0) - return (size_t)pos; - else if (pos < -(lua_Integer)len) - return 0; - else return len + (size_t)pos + 1; -} - - -static int str_sub (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - size_t start = posrelatI(luaL_checkinteger(L, 2), l); - size_t end = getendpos(L, 3, -1, l); - if (start <= end) - lua_pushlstring(L, s + start - 1, (end - start) + 1); - else lua_pushliteral(L, ""); - return 1; -} - - -static int str_reverse (lua_State *L) { - size_t l, i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - char *p = luaL_buffinitsize(L, &b, l); - for (i = 0; i < l; i++) - p[i] = s[l - i - 1]; - luaL_pushresultsize(&b, l); - return 1; -} - - -static int str_lower (lua_State *L) { - size_t l; - size_t i; - luaL_Buffer b; - const char *s = luaL_checklstring(L, 1, &l); - char *p = luaL_buffinitsize(L, &b, l); - for (i=0; i MAXSIZE / n)) - return luaL_error(L, "resulting string too large"); - else { - size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; - luaL_Buffer b; - char *p = luaL_buffinitsize(L, &b, totallen); - while (n-- > 1) { /* first n-1 copies (followed by separator) */ - memcpy(p, s, l * sizeof(char)); p += l; - if (lsep > 0) { /* empty 'memcpy' is not that cheap */ - memcpy(p, sep, lsep * sizeof(char)); - p += lsep; - } - } - memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ - luaL_pushresultsize(&b, totallen); - } - return 1; -} - - -static int str_byte (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - lua_Integer pi = luaL_optinteger(L, 2, 1); - size_t posi = posrelatI(pi, l); - size_t pose = getendpos(L, 3, pi, l); - int n, i; - if (posi > pose) return 0; /* empty interval; return no values */ - if (l_unlikely(pose - posi >= (size_t)INT_MAX)) /* arithmetic overflow? */ - return luaL_error(L, "string slice too long"); - n = (int)(pose - posi) + 1; - luaL_checkstack(L, n, "string slice too long"); - for (i=0; iinit) { - state->init = 1; - luaL_buffinit(L, &state->B); - } - luaL_addlstring(&state->B, (const char *)b, size); - return 0; -} - - -static int str_dump (lua_State *L) { - struct str_Writer state; - int strip = lua_toboolean(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 1); /* ensure function is on the top of the stack */ - state.init = 0; - if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) - return luaL_error(L, "unable to dump given function"); - luaL_pushresult(&state.B); - return 1; -} - - - -/* -** {====================================================== -** METAMETHODS -** ======================================================= -*/ - -#if defined(LUA_NOCVTS2N) /* { */ - -/* no coercion from strings to numbers */ - -static const luaL_Reg stringmetamethods[] = { - {"__index", NULL}, /* placeholder */ - {NULL, NULL} -}; - -#else /* }{ */ - -static int tonum (lua_State *L, int arg) { - if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */ - lua_pushvalue(L, arg); - return 1; - } - else { /* check whether it is a numerical string */ - size_t len; - const char *s = lua_tolstring(L, arg, &len); - return (s != NULL && lua_stringtonumber(L, s) == len + 1); - } -} - - -static void trymt (lua_State *L, const char *mtname) { - lua_settop(L, 2); /* back to the original arguments */ - if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || - !luaL_getmetafield(L, 2, mtname))) - luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, - luaL_typename(L, -2), luaL_typename(L, -1)); - lua_insert(L, -3); /* put metamethod before arguments */ - lua_call(L, 2, 1); /* call metamethod */ -} - - -static int arith (lua_State *L, int op, const char *mtname) { - if (tonum(L, 1) && tonum(L, 2)) - lua_arith(L, op); /* result will be on the top */ - else - trymt(L, mtname); - return 1; -} - - -static int arith_add (lua_State *L) { - return arith(L, LUA_OPADD, "__add"); -} - -static int arith_sub (lua_State *L) { - return arith(L, LUA_OPSUB, "__sub"); -} - -static int arith_mul (lua_State *L) { - return arith(L, LUA_OPMUL, "__mul"); -} - -static int arith_mod (lua_State *L) { - return arith(L, LUA_OPMOD, "__mod"); -} - -static int arith_pow (lua_State *L) { - return arith(L, LUA_OPPOW, "__pow"); -} - -static int arith_div (lua_State *L) { - return arith(L, LUA_OPDIV, "__div"); -} - -static int arith_idiv (lua_State *L) { - return arith(L, LUA_OPIDIV, "__idiv"); -} - -static int arith_unm (lua_State *L) { - return arith(L, LUA_OPUNM, "__unm"); -} - - -static const luaL_Reg stringmetamethods[] = { - {"__add", arith_add}, - {"__sub", arith_sub}, - {"__mul", arith_mul}, - {"__mod", arith_mod}, - {"__pow", arith_pow}, - {"__div", arith_div}, - {"__idiv", arith_idiv}, - {"__unm", arith_unm}, - {"__index", NULL}, /* placeholder */ - {NULL, NULL} -}; - -#endif /* } */ - -/* }====================================================== */ - -/* -** {====================================================== -** PATTERN MATCHING -** ======================================================= -*/ - - -#define CAP_UNFINISHED (-1) -#define CAP_POSITION (-2) - - -typedef struct MatchState { - const char *src_init; /* init of source string */ - const char *src_end; /* end ('\0') of source string */ - const char *p_end; /* end ('\0') of pattern */ - lua_State *L; - int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ - unsigned char level; /* total number of captures (finished or unfinished) */ - struct { - const char *init; - ptrdiff_t len; - } capture[LUA_MAXCAPTURES]; -} MatchState; - - -/* recursive function */ -static const char *match (MatchState *ms, const char *s, const char *p); - - -/* maximum recursion depth for 'match' */ -#if !defined(MAXCCALLS) -#define MAXCCALLS 200 -#endif - - -#define L_ESC '%' -#define SPECIALS "^$*+?.([%-" - - -static int check_capture (MatchState *ms, int l) { - l -= '1'; - if (l_unlikely(l < 0 || l >= ms->level || - ms->capture[l].len == CAP_UNFINISHED)) - return luaL_error(ms->L, "invalid capture index %%%d", l + 1); - return l; -} - - -static int capture_to_close (MatchState *ms) { - int level = ms->level; - for (level--; level>=0; level--) - if (ms->capture[level].len == CAP_UNFINISHED) return level; - return luaL_error(ms->L, "invalid pattern capture"); -} - - -static const char *classend (MatchState *ms, const char *p) { - switch (*p++) { - case L_ESC: { - if (l_unlikely(p == ms->p_end)) - luaL_error(ms->L, "malformed pattern (ends with '%%')"); - return p+1; - } - case '[': { - if (*p == '^') p++; - do { /* look for a ']' */ - if (l_unlikely(p == ms->p_end)) - luaL_error(ms->L, "malformed pattern (missing ']')"); - if (*(p++) == L_ESC && p < ms->p_end) - p++; /* skip escapes (e.g. '%]') */ - } while (*p != ']'); - return p+1; - } - default: { - return p; - } - } -} - - -static int match_class (int c, int cl) { - int res; - switch (tolower(cl)) { - case 'a' : res = isalpha(c); break; - case 'c' : res = iscntrl(c); break; - case 'd' : res = isdigit(c); break; - case 'g' : res = isgraph(c); break; - case 'l' : res = islower(c); break; - case 'p' : res = ispunct(c); break; - case 's' : res = isspace(c); break; - case 'u' : res = isupper(c); break; - case 'w' : res = isalnum(c); break; - case 'x' : res = isxdigit(c); break; - case 'z' : res = (c == 0); break; /* deprecated option */ - default: return (cl == c); - } - return (islower(cl) ? res : !res); -} - - -static int matchbracketclass (int c, const char *p, const char *ec) { - int sig = 1; - if (*(p+1) == '^') { - sig = 0; - p++; /* skip the '^' */ - } - while (++p < ec) { - if (*p == L_ESC) { - p++; - if (match_class(c, uchar(*p))) - return sig; - } - else if ((*(p+1) == '-') && (p+2 < ec)) { - p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) - return sig; - } - else if (uchar(*p) == c) return sig; - } - return !sig; -} - - -static int singlematch (MatchState *ms, const char *s, const char *p, - const char *ep) { - if (s >= ms->src_end) - return 0; - else { - int c = uchar(*s); - switch (*p) { - case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); - case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); - } - } -} - - -static const char *matchbalance (MatchState *ms, const char *s, - const char *p) { - if (l_unlikely(p >= ms->p_end - 1)) - luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); - if (*s != *p) return NULL; - else { - int b = *p; - int e = *(p+1); - int cont = 1; - while (++s < ms->src_end) { - if (*s == e) { - if (--cont == 0) return s+1; - } - else if (*s == b) cont++; - } - } - return NULL; /* string ends out of balance */ -} - - -static const char *max_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - ptrdiff_t i = 0; /* counts maximum expand for item */ - while (singlematch(ms, s + i, p, ep)) - i++; - /* keeps trying to match with the maximum repetitions */ - while (i>=0) { - const char *res = match(ms, (s+i), ep+1); - if (res) return res; - i--; /* else didn't match; reduce 1 repetition to try again */ - } - return NULL; -} - - -static const char *min_expand (MatchState *ms, const char *s, - const char *p, const char *ep) { - for (;;) { - const char *res = match(ms, s, ep+1); - if (res != NULL) - return res; - else if (singlematch(ms, s, p, ep)) - s++; /* try with one more repetition */ - else return NULL; - } -} - - -static const char *start_capture (MatchState *ms, const char *s, - const char *p, int what) { - const char *res; - int level = ms->level; - if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); - ms->capture[level].init = s; - ms->capture[level].len = what; - ms->level = level+1; - if ((res=match(ms, s, p)) == NULL) /* match failed? */ - ms->level--; /* undo capture */ - return res; -} - - -static const char *end_capture (MatchState *ms, const char *s, - const char *p) { - int l = capture_to_close(ms); - const char *res; - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ - if ((res = match(ms, s, p)) == NULL) /* match failed? */ - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ - return res; -} - - -static const char *match_capture (MatchState *ms, const char *s, int l) { - size_t len; - l = check_capture(ms, l); - len = ms->capture[l].len; - if ((size_t)(ms->src_end-s) >= len && - memcmp(ms->capture[l].init, s, len) == 0) - return s+len; - else return NULL; -} - - -static const char *match (MatchState *ms, const char *s, const char *p) { - if (l_unlikely(ms->matchdepth-- == 0)) - luaL_error(ms->L, "pattern too complex"); - init: /* using goto's to optimize tail recursion */ - if (p != ms->p_end) { /* end of pattern? */ - switch (*p) { - case '(': { /* start capture */ - if (*(p + 1) == ')') /* position capture? */ - s = start_capture(ms, s, p + 2, CAP_POSITION); - else - s = start_capture(ms, s, p + 1, CAP_UNFINISHED); - break; - } - case ')': { /* end capture */ - s = end_capture(ms, s, p + 1); - break; - } - case '$': { - if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ - goto dflt; /* no; go to default */ - s = (s == ms->src_end) ? s : NULL; /* check end of string */ - break; - } - case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ - switch (*(p + 1)) { - case 'b': { /* balanced string? */ - s = matchbalance(ms, s, p + 2); - if (s != NULL) { - p += 4; goto init; /* return match(ms, s, p + 4); */ - } /* else fail (s == NULL) */ - break; - } - case 'f': { /* frontier? */ - const char *ep; char previous; - p += 2; - if (l_unlikely(*p != '[')) - luaL_error(ms->L, "missing '[' after '%%f' in pattern"); - ep = classend(ms, p); /* points to what is next */ - previous = (s == ms->src_init) ? '\0' : *(s - 1); - if (!matchbracketclass(uchar(previous), p, ep - 1) && - matchbracketclass(uchar(*s), p, ep - 1)) { - p = ep; goto init; /* return match(ms, s, ep); */ - } - s = NULL; /* match failed */ - break; - } - case '0': case '1': case '2': case '3': - case '4': case '5': case '6': case '7': - case '8': case '9': { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p + 1))); - if (s != NULL) { - p += 2; goto init; /* return match(ms, s, p + 2) */ - } - break; - } - default: goto dflt; - } - break; - } - default: dflt: { /* pattern class plus optional suffix */ - const char *ep = classend(ms, p); /* points to optional suffix */ - /* does not match at least once? */ - if (!singlematch(ms, s, p, ep)) { - if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */ - p = ep + 1; goto init; /* return match(ms, s, ep + 1); */ - } - else /* '+' or no suffix */ - s = NULL; /* fail */ - } - else { /* matched once */ - switch (*ep) { /* handle optional suffix */ - case '?': { /* optional */ - const char *res; - if ((res = match(ms, s + 1, ep + 1)) != NULL) - s = res; - else { - p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */ - } - break; - } - case '+': /* 1 or more repetitions */ - s++; /* 1 match already done */ - /* FALLTHROUGH */ - case '*': /* 0 or more repetitions */ - s = max_expand(ms, s, p, ep); - break; - case '-': /* 0 or more repetitions (minimum) */ - s = min_expand(ms, s, p, ep); - break; - default: /* no suffix */ - s++; p = ep; goto init; /* return match(ms, s + 1, ep); */ - } - } - break; - } - } - } - ms->matchdepth++; - return s; -} - - - -static const char *lmemfind (const char *s1, size_t l1, - const char *s2, size_t l2) { - if (l2 == 0) return s1; /* empty strings are everywhere */ - else if (l2 > l1) return NULL; /* avoids a negative 'l1' */ - else { - const char *init; /* to search for a '*s2' inside 's1' */ - l2--; /* 1st char will be checked by 'memchr' */ - l1 = l1-l2; /* 's2' cannot be found after that */ - while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { - init++; /* 1st char is already checked */ - if (memcmp(init, s2+1, l2) == 0) - return init-1; - else { /* correct 'l1' and 's1' to try again */ - l1 -= init-s1; - s1 = init; - } - } - return NULL; /* not found */ - } -} - - -/* -** get information about the i-th capture. If there are no captures -** and 'i==0', return information about the whole match, which -** is the range 's'..'e'. If the capture is a string, return -** its length and put its address in '*cap'. If it is an integer -** (a position), push it on the stack and return CAP_POSITION. -*/ -static size_t get_onecapture (MatchState *ms, int i, const char *s, - const char *e, const char **cap) { - if (i >= ms->level) { - if (l_unlikely(i != 0)) - luaL_error(ms->L, "invalid capture index %%%d", i + 1); - *cap = s; - return e - s; - } - else { - ptrdiff_t capl = ms->capture[i].len; - *cap = ms->capture[i].init; - if (l_unlikely(capl == CAP_UNFINISHED)) - luaL_error(ms->L, "unfinished capture"); - else if (capl == CAP_POSITION) - lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); - return capl; - } -} - - -/* -** Push the i-th capture on the stack. -*/ -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { - const char *cap; - ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); - if (l != CAP_POSITION) - lua_pushlstring(ms->L, cap, l); - /* else position was already pushed */ -} - - -static int push_captures (MatchState *ms, const char *s, const char *e) { - int i; - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; - luaL_checkstack(ms->L, nlevels, "too many captures"); - for (i = 0; i < nlevels; i++) - push_onecapture(ms, i, s, e); - return nlevels; /* number of strings pushed */ -} - - -/* check whether pattern has no special characters */ -static int nospecials (const char *p, size_t l) { - size_t upto = 0; - do { - if (strpbrk(p + upto, SPECIALS)) - return 0; /* pattern has a special character */ - upto += strlen(p + upto) + 1; /* may have more after \0 */ - } while (upto <= l); - return 1; /* no special chars found */ -} - - -static void prepstate (MatchState *ms, lua_State *L, - const char *s, size_t ls, const char *p, size_t lp) { - ms->L = L; - ms->matchdepth = MAXCCALLS; - ms->src_init = s; - ms->src_end = s + ls; - ms->p_end = p + lp; -} - - -static void reprepstate (MatchState *ms) { - ms->level = 0; - lua_assert(ms->matchdepth == MAXCCALLS); -} - - -static int str_find_aux (lua_State *L, int find) { - size_t ls, lp; - const char *s = luaL_checklstring(L, 1, &ls); - const char *p = luaL_checklstring(L, 2, &lp); - size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; - if (init > ls) { /* start after string's end? */ - luaL_pushfail(L); /* cannot find anything */ - return 1; - } - /* explicit request or no special characters? */ - if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { - /* do a plain search */ - const char *s2 = lmemfind(s + init, ls - init, p, lp); - if (s2) { - lua_pushinteger(L, (s2 - s) + 1); - lua_pushinteger(L, (s2 - s) + lp); - return 2; - } - } - else { - MatchState ms; - const char *s1 = s + init; - int anchor = (*p == '^'); - if (anchor) { - p++; lp--; /* skip anchor character */ - } - prepstate(&ms, L, s, ls, p, lp); - do { - const char *res; - reprepstate(&ms); - if ((res=match(&ms, s1, p)) != NULL) { - if (find) { - lua_pushinteger(L, (s1 - s) + 1); /* start */ - lua_pushinteger(L, res - s); /* end */ - return push_captures(&ms, NULL, 0) + 2; - } - else - return push_captures(&ms, s1, res); - } - } while (s1++ < ms.src_end && !anchor); - } - luaL_pushfail(L); /* not found */ - return 1; -} - - -static int str_find (lua_State *L) { - return str_find_aux(L, 1); -} - - -static int str_match (lua_State *L) { - return str_find_aux(L, 0); -} - - -/* state for 'gmatch' */ -typedef struct GMatchState { - const char *src; /* current position */ - const char *p; /* pattern */ - const char *lastmatch; /* end of last match */ - MatchState ms; /* match state */ -} GMatchState; - - -static int gmatch_aux (lua_State *L) { - GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); - const char *src; - gm->ms.L = L; - for (src = gm->src; src <= gm->ms.src_end; src++) { - const char *e; - reprepstate(&gm->ms); - if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) { - gm->src = gm->lastmatch = e; - return push_captures(&gm->ms, src, e); - } - } - return 0; /* not found */ -} - - -static int gmatch (lua_State *L) { - size_t ls, lp; - const char *s = luaL_checklstring(L, 1, &ls); - const char *p = luaL_checklstring(L, 2, &lp); - size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; - GMatchState *gm; - lua_settop(L, 2); /* keep strings on closure to avoid being collected */ - gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0); - if (init > ls) /* start after string's end? */ - init = ls + 1; /* avoid overflows in 's + init' */ - prepstate(&gm->ms, L, s, ls, p, lp); - gm->src = s + init; gm->p = p; gm->lastmatch = NULL; - lua_pushcclosure(L, gmatch_aux, 3); - return 1; -} - - -static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e) { - size_t l; - lua_State *L = ms->L; - const char *news = lua_tolstring(L, 3, &l); - const char *p; - while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { - luaL_addlstring(b, news, p - news); - p++; /* skip ESC */ - if (*p == L_ESC) /* '%%' */ - luaL_addchar(b, *p); - else if (*p == '0') /* '%0' */ - luaL_addlstring(b, s, e - s); - else if (isdigit(uchar(*p))) { /* '%n' */ - const char *cap; - ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); - if (resl == CAP_POSITION) - luaL_addvalue(b); /* add position to accumulated result */ - else - luaL_addlstring(b, cap, resl); - } - else - luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); - l -= p + 1 - news; - news = p + 1; - } - luaL_addlstring(b, news, l); -} - - -/* -** Add the replacement value to the string buffer 'b'. -** Return true if the original string was changed. (Function calls and -** table indexing resulting in nil or false do not change the subject.) -*/ -static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e, int tr) { - lua_State *L = ms->L; - switch (tr) { - case LUA_TFUNCTION: { /* call the function */ - int n; - lua_pushvalue(L, 3); /* push the function */ - n = push_captures(ms, s, e); /* all captures as arguments */ - lua_call(L, n, 1); /* call it */ - break; - } - case LUA_TTABLE: { /* index the table */ - push_onecapture(ms, 0, s, e); /* first capture is the index */ - lua_gettable(L, 3); - break; - } - default: { /* LUA_TNUMBER or LUA_TSTRING */ - add_s(ms, b, s, e); /* add value to the buffer */ - return 1; /* something changed */ - } - } - if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); /* remove value */ - luaL_addlstring(b, s, e - s); /* keep original text */ - return 0; /* no changes */ - } - else if (l_unlikely(!lua_isstring(L, -1))) - return luaL_error(L, "invalid replacement value (a %s)", - luaL_typename(L, -1)); - else { - luaL_addvalue(b); /* add result to accumulator */ - return 1; /* something changed */ - } -} - - -static int str_gsub (lua_State *L) { - size_t srcl, lp; - const char *src = luaL_checklstring(L, 1, &srcl); /* subject */ - const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ - const char *lastmatch = NULL; /* end of last match */ - int tr = lua_type(L, 3); /* replacement type */ - lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ - int anchor = (*p == '^'); - lua_Integer n = 0; /* replacement count */ - int changed = 0; /* change flag */ - MatchState ms; - luaL_Buffer b; - luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || - tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, - "string/function/table"); - luaL_buffinit(L, &b); - if (anchor) { - p++; lp--; /* skip anchor character */ - } - prepstate(&ms, L, src, srcl, p, lp); - while (n < max_s) { - const char *e; - reprepstate(&ms); /* (re)prepare state for new match */ - if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ - n++; - changed = add_value(&ms, &b, src, e, tr) | changed; - src = lastmatch = e; - } - else if (src < ms.src_end) /* otherwise, skip one character */ - luaL_addchar(&b, *src++); - else break; /* end of subject */ - if (anchor) break; - } - if (!changed) /* no changes? */ - lua_pushvalue(L, 1); /* return original string */ - else { /* something changed */ - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); /* create and return new string */ - } - lua_pushinteger(L, n); /* number of substitutions */ - return 2; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** STRING FORMAT -** ======================================================= -*/ - -#if !defined(lua_number2strx) /* { */ - -/* -** Hexadecimal floating-point formatter -*/ - -#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) - - -/* -** Number of bits that goes into the first digit. It can be any value -** between 1 and 4; the following definition tries to align the number -** to nibble boundaries by making what is left after that first digit a -** multiple of 4. -*/ -#define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1) - - -/* -** Add integer part of 'x' to buffer and return new 'x' -*/ -static lua_Number adddigit (char *buff, int n, lua_Number x) { - lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ - int d = (int)dd; - buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ - return x - dd; /* return what is left */ -} - - -static int num2straux (char *buff, int sz, lua_Number x) { - /* if 'inf' or 'NaN', format it like '%g' */ - if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) - return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); - else if (x == 0) { /* can be -0... */ - /* create "0" or "-0" followed by exponent */ - return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x); - } - else { - int e; - lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ - int n = 0; /* character count */ - if (m < 0) { /* is number negative? */ - buff[n++] = '-'; /* add sign */ - m = -m; /* make it positive */ - } - buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ - m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */ - e -= L_NBFD; /* this digit goes before the radix point */ - if (m > 0) { /* more digits? */ - buff[n++] = lua_getlocaledecpoint(); /* add radix point */ - do { /* add as many digits as needed */ - m = adddigit(buff, n++, m * 16); - } while (m > 0); - } - n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ - lua_assert(n < sz); - return n; - } -} - - -static int lua_number2strx (lua_State *L, char *buff, int sz, - const char *fmt, lua_Number x) { - int n = num2straux(buff, sz, x); - if (fmt[SIZELENMOD] == 'A') { - int i; - for (i = 0; i < n; i++) - buff[i] = toupper(uchar(buff[i])); - } - else if (l_unlikely(fmt[SIZELENMOD] != 'a')) - return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); - return n; -} - -#endif /* } */ - - -/* -** Maximum size for items formatted with '%f'. This size is produced -** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.', -** and '\0') + number of decimal digits to represent maxfloat (which -** is maximum exponent + 1). (99+3+1, adding some extra, 110) -*/ -#define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP)) - - -/* -** All formats except '%f' do not need that large limit. The other -** float formats use exponents, so that they fit in the 99 limit for -** significant digits; 's' for large strings and 'q' add items directly -** to the buffer; all integer formats also fit in the 99 limit. The -** worst case are floats: they may need 99 significant digits, plus -** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120. -*/ -#define MAX_ITEM 120 - - -/* valid flags in a format specification */ -#if !defined(L_FMTFLAGSF) - -/* valid flags for a, A, e, E, f, F, g, and G conversions */ -#define L_FMTFLAGSF "-+#0 " - -/* valid flags for o, x, and X conversions */ -#define L_FMTFLAGSX "-#0" - -/* valid flags for d and i conversions */ -#define L_FMTFLAGSI "-+0 " - -/* valid flags for u conversions */ -#define L_FMTFLAGSU "-0" - -/* valid flags for c, p, and s conversions */ -#define L_FMTFLAGSC "-" - -#endif - - -/* -** Maximum size of each format specification (such as "%-099.99d"): -** Initial '%', flags (up to 5), width (2), period, precision (2), -** length modifier (8), conversion specifier, and final '\0', plus some -** extra. -*/ -#define MAX_FORMAT 32 - - -static void addquoted (luaL_Buffer *b, const char *s, size_t len) { - luaL_addchar(b, '"'); - while (len--) { - if (*s == '"' || *s == '\\' || *s == '\n') { - luaL_addchar(b, '\\'); - luaL_addchar(b, *s); - } - else if (iscntrl(uchar(*s))) { - char buff[10]; - if (!isdigit(uchar(*(s+1)))) - l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); - else - l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); - luaL_addstring(b, buff); - } - else - luaL_addchar(b, *s); - s++; - } - luaL_addchar(b, '"'); -} - - -/* -** Serialize a floating-point number in such a way that it can be -** scanned back by Lua. Use hexadecimal format for "common" numbers -** (to preserve precision); inf, -inf, and NaN are handled separately. -** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.) -*/ -static int quotefloat (lua_State *L, char *buff, lua_Number n) { - const char *s; /* for the fixed representations */ - if (n == (lua_Number)HUGE_VAL) /* inf? */ - s = "1e9999"; - else if (n == -(lua_Number)HUGE_VAL) /* -inf? */ - s = "-1e9999"; - else if (n != n) /* NaN? */ - s = "(0/0)"; - else { /* format number as hexadecimal */ - int nb = lua_number2strx(L, buff, MAX_ITEM, - "%" LUA_NUMBER_FRMLEN "a", n); - /* ensures that 'buff' string uses a dot as the radix character */ - if (memchr(buff, '.', nb) == NULL) { /* no dot? */ - char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = (char *)memchr(buff, point, nb); - if (ppoint) *ppoint = '.'; /* change it to a dot */ - } - return nb; - } - /* for the fixed representations */ - return l_sprintf(buff, MAX_ITEM, "%s", s); -} - - -static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { - switch (lua_type(L, arg)) { - case LUA_TSTRING: { - size_t len; - const char *s = lua_tolstring(L, arg, &len); - addquoted(b, s, len); - break; - } - case LUA_TNUMBER: { - char *buff = luaL_prepbuffsize(b, MAX_ITEM); - int nb; - if (!lua_isinteger(L, arg)) /* float? */ - nb = quotefloat(L, buff, lua_tonumber(L, arg)); - else { /* integers */ - lua_Integer n = lua_tointeger(L, arg); - const char *format = (n == LUA_MININTEGER) /* corner case? */ - ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */ - : LUA_INTEGER_FMT; /* else use default format */ - nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); - } - luaL_addsize(b, nb); - break; - } - case LUA_TNIL: case LUA_TBOOLEAN: { - luaL_tolstring(L, arg, NULL); - luaL_addvalue(b); - break; - } - default: { - luaL_argerror(L, arg, "value has no literal form"); - } - } -} - - -static const char *get2digits (const char *s) { - if (isdigit(uchar(*s))) { - s++; - if (isdigit(uchar(*s))) s++; /* (2 digits at most) */ - } - return s; -} - - -/* -** Check whether a conversion specification is valid. When called, -** first character in 'form' must be '%' and last character must -** be a valid conversion specifier. 'flags' are the accepted flags; -** 'precision' signals whether to accept a precision. -*/ -static void checkformat (lua_State *L, const char *form, const char *flags, - int precision) { - const char *spec = form + 1; /* skip '%' */ - spec += strspn(spec, flags); /* skip flags */ - if (*spec != '0') { /* a width cannot start with '0' */ - spec = get2digits(spec); /* skip width */ - if (*spec == '.' && precision) { - spec++; - spec = get2digits(spec); /* skip precision */ - } - } - if (!isalpha(uchar(*spec))) /* did not go to the end? */ - luaL_error(L, "invalid conversion specification: '%s'", form); -} - - -/* -** Get a conversion specification and copy it to 'form'. -** Return the address of its last character. -*/ -static const char *getformat (lua_State *L, const char *strfrmt, - char *form) { - /* spans flags, width, and precision ('0' is included as a flag) */ - size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789."); - len++; /* adds following character (should be the specifier) */ - /* still needs space for '%', '\0', plus a length modifier */ - if (len >= MAX_FORMAT - 10) - luaL_error(L, "invalid format (too long)"); - *(form++) = '%'; - memcpy(form, strfrmt, len * sizeof(char)); - *(form + len) = '\0'; - return strfrmt + len - 1; -} - - -/* -** add length modifier into formats -*/ -static void addlenmod (char *form, const char *lenmod) { - size_t l = strlen(form); - size_t lm = strlen(lenmod); - char spec = form[l - 1]; - strcpy(form + l - 1, lenmod); - form[l + lm - 1] = spec; - form[l + lm] = '\0'; -} - - -static int str_format (lua_State *L) { - int top = lua_gettop(L); - int arg = 1; - size_t sfl; - const char *strfrmt = luaL_checklstring(L, arg, &sfl); - const char *strfrmt_end = strfrmt+sfl; - const char *flags; - luaL_Buffer b; - luaL_buffinit(L, &b); - while (strfrmt < strfrmt_end) { - if (*strfrmt != L_ESC) - luaL_addchar(&b, *strfrmt++); - else if (*++strfrmt == L_ESC) - luaL_addchar(&b, *strfrmt++); /* %% */ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format ('%...') */ - int maxitem = MAX_ITEM; /* maximum length for the result */ - char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ - int nb = 0; /* number of bytes in result */ - if (++arg > top) - return luaL_argerror(L, arg, "no value"); - strfrmt = getformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { - checkformat(L, form, L_FMTFLAGSC, 0); - nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg)); - break; - } - case 'd': case 'i': - flags = L_FMTFLAGSI; - goto intcase; - case 'u': - flags = L_FMTFLAGSU; - goto intcase; - case 'o': case 'x': case 'X': - flags = L_FMTFLAGSX; - intcase: { - lua_Integer n = luaL_checkinteger(L, arg); - checkformat(L, form, flags, 1); - addlenmod(form, LUA_INTEGER_FRMLEN); - nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); - break; - } - case 'a': case 'A': - checkformat(L, form, L_FMTFLAGSF, 1); - addlenmod(form, LUA_NUMBER_FRMLEN); - nb = lua_number2strx(L, buff, maxitem, form, - luaL_checknumber(L, arg)); - break; - case 'f': - maxitem = MAX_ITEMF; /* extra space for '%f' */ - buff = luaL_prepbuffsize(&b, maxitem); - /* FALLTHROUGH */ - case 'e': case 'E': case 'g': case 'G': { - lua_Number n = luaL_checknumber(L, arg); - checkformat(L, form, L_FMTFLAGSF, 1); - addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); - break; - } - case 'p': { - const void *p = lua_topointer(L, arg); - checkformat(L, form, L_FMTFLAGSC, 0); - if (p == NULL) { /* avoid calling 'printf' with argument NULL */ - p = "(null)"; /* result */ - form[strlen(form) - 1] = 's'; /* format it as a string */ - } - nb = l_sprintf(buff, maxitem, form, p); - break; - } - case 'q': { - if (form[2] != '\0') /* modifiers? */ - return luaL_error(L, "specifier '%%q' cannot have modifiers"); - addliteral(L, &b, arg); - break; - } - case 's': { - size_t l; - const char *s = luaL_tolstring(L, arg, &l); - if (form[2] == '\0') /* no modifiers? */ - luaL_addvalue(&b); /* keep entire string */ - else { - luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); - checkformat(L, form, L_FMTFLAGSC, 1); - if (strchr(form, '.') == NULL && l >= 100) { - /* no precision and string is too long to be formatted */ - luaL_addvalue(&b); /* keep entire string */ - } - else { /* format the string into 'buff' */ - nb = l_sprintf(buff, maxitem, form, s); - lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ - } - } - break; - } - default: { /* also treat cases 'pnLlh' */ - return luaL_error(L, "invalid conversion '%s' to 'format'", form); - } - } - lua_assert(nb < maxitem); - luaL_addsize(&b, nb); - } - } - luaL_pushresult(&b); - return 1; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** PACK/UNPACK -** ======================================================= -*/ - - -/* value used for padding */ -#if !defined(LUAL_PACKPADBYTE) -#define LUAL_PACKPADBYTE 0x00 -#endif - -/* maximum size for the binary representation of an integer */ -#define MAXINTSIZE 16 - -/* number of bits in a character */ -#define NB CHAR_BIT - -/* mask for one character (NB 1's) */ -#define MC ((1 << NB) - 1) - -/* size of a lua_Integer */ -#define SZINT ((int)sizeof(lua_Integer)) - - -/* dummy union to get native endianness */ -static const union { - int dummy; - char little; /* true iff machine is little endian */ -} nativeendian = {1}; - - -/* -** information to pack/unpack stuff -*/ -typedef struct Header { - lua_State *L; - int islittle; - int maxalign; -} Header; - - -/* -** options for pack/unpack -*/ -typedef enum KOption { - Kint, /* signed integers */ - Kuint, /* unsigned integers */ - Kfloat, /* single-precision floating-point numbers */ - Knumber, /* Lua "native" floating-point numbers */ - Kdouble, /* double-precision floating-point numbers */ - Kchar, /* fixed-length strings */ - Kstring, /* strings with prefixed length */ - Kzstr, /* zero-terminated strings */ - Kpadding, /* padding */ - Kpaddalign, /* padding for alignment */ - Knop /* no-op (configuration or spaces) */ -} KOption; - - -/* -** Read an integer numeral from string 'fmt' or return 'df' if -** there is no numeral -*/ -static int digit (int c) { return '0' <= c && c <= '9'; } - -static int getnum (const char **fmt, int df) { - if (!digit(**fmt)) /* no number? */ - return df; /* return default value */ - else { - int a = 0; - do { - a = a*10 + (*((*fmt)++) - '0'); - } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10); - return a; - } -} - - -/* -** Read an integer numeral and raises an error if it is larger -** than the maximum size for integers. -*/ -static int getnumlimit (Header *h, const char **fmt, int df) { - int sz = getnum(fmt, df); - if (l_unlikely(sz > MAXINTSIZE || sz <= 0)) - return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); - return sz; -} - - -/* -** Initialize Header -*/ -static void initheader (lua_State *L, Header *h) { - h->L = L; - h->islittle = nativeendian.little; - h->maxalign = 1; -} - - -/* -** Read and classify next option. 'size' is filled with option's size. -*/ -static KOption getoption (Header *h, const char **fmt, int *size) { - /* dummy structure to get native alignment requirements */ - struct cD { char c; union { LUAI_MAXALIGN; } u; }; - int opt = *((*fmt)++); - *size = 0; /* default */ - switch (opt) { - case 'b': *size = sizeof(char); return Kint; - case 'B': *size = sizeof(char); return Kuint; - case 'h': *size = sizeof(short); return Kint; - case 'H': *size = sizeof(short); return Kuint; - case 'l': *size = sizeof(long); return Kint; - case 'L': *size = sizeof(long); return Kuint; - case 'j': *size = sizeof(lua_Integer); return Kint; - case 'J': *size = sizeof(lua_Integer); return Kuint; - case 'T': *size = sizeof(size_t); return Kuint; - case 'f': *size = sizeof(float); return Kfloat; - case 'n': *size = sizeof(lua_Number); return Knumber; - case 'd': *size = sizeof(double); return Kdouble; - case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; - case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; - case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; - case 'c': - *size = getnum(fmt, -1); - if (l_unlikely(*size == -1)) - luaL_error(h->L, "missing size for format option 'c'"); - return Kchar; - case 'z': return Kzstr; - case 'x': *size = 1; return Kpadding; - case 'X': return Kpaddalign; - case ' ': break; - case '<': h->islittle = 1; break; - case '>': h->islittle = 0; break; - case '=': h->islittle = nativeendian.little; break; - case '!': { - const int maxalign = offsetof(struct cD, u); - h->maxalign = getnumlimit(h, fmt, maxalign); - break; - } - default: luaL_error(h->L, "invalid format option '%c'", opt); - } - return Knop; -} - - -/* -** Read, classify, and fill other details about the next option. -** 'psize' is filled with option's size, 'notoalign' with its -** alignment requirements. -** Local variable 'size' gets the size to be aligned. (Kpadal option -** always gets its full alignment, other options are limited by -** the maximum alignment ('maxalign'). Kchar option needs no alignment -** despite its size. -*/ -static KOption getdetails (Header *h, size_t totalsize, - const char **fmt, int *psize, int *ntoalign) { - KOption opt = getoption(h, fmt, psize); - int align = *psize; /* usually, alignment follows size */ - if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ - if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) - luaL_argerror(h->L, 1, "invalid next option for option 'X'"); - } - if (align <= 1 || opt == Kchar) /* need no alignment? */ - *ntoalign = 0; - else { - if (align > h->maxalign) /* enforce maximum alignment */ - align = h->maxalign; - if (l_unlikely((align & (align - 1)) != 0)) /* not a power of 2? */ - luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); - *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); - } - return opt; -} - - -/* -** Pack integer 'n' with 'size' bytes and 'islittle' endianness. -** The final 'if' handles the case when 'size' is larger than -** the size of a Lua integer, correcting the extra sign-extension -** bytes if necessary (by default they would be zeros). -*/ -static void packint (luaL_Buffer *b, lua_Unsigned n, - int islittle, int size, int neg) { - char *buff = luaL_prepbuffsize(b, size); - int i; - buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ - for (i = 1; i < size; i++) { - n >>= NB; - buff[islittle ? i : size - 1 - i] = (char)(n & MC); - } - if (neg && size > SZINT) { /* negative number need sign extension? */ - for (i = SZINT; i < size; i++) /* correct extra bytes */ - buff[islittle ? i : size - 1 - i] = (char)MC; - } - luaL_addsize(b, size); /* add result to buffer */ -} - - -/* -** Copy 'size' bytes from 'src' to 'dest', correcting endianness if -** given 'islittle' is different from native endianness. -*/ -static void copywithendian (char *dest, const char *src, - int size, int islittle) { - if (islittle == nativeendian.little) - memcpy(dest, src, size); - else { - dest += size - 1; - while (size-- != 0) - *(dest--) = *(src++); - } -} - - -static int str_pack (lua_State *L) { - luaL_Buffer b; - Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - int arg = 1; /* current argument to pack */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader(L, &h); - lua_pushnil(L); /* mark to separate arguments from string buffer */ - luaL_buffinit(L, &b); - while (*fmt != '\0') { - int size, ntoalign; - KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); - totalsize += ntoalign + size; - while (ntoalign-- > 0) - luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ - arg++; - switch (opt) { - case Kint: { /* signed integers */ - lua_Integer n = luaL_checkinteger(L, arg); - if (size < SZINT) { /* need overflow check? */ - lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); - luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); - } - packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); - break; - } - case Kuint: { /* unsigned integers */ - lua_Integer n = luaL_checkinteger(L, arg); - if (size < SZINT) /* need overflow check? */ - luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), - arg, "unsigned overflow"); - packint(&b, (lua_Unsigned)n, h.islittle, size, 0); - break; - } - case Kfloat: { /* C float */ - float f = (float)luaL_checknumber(L, arg); /* get argument */ - char *buff = luaL_prepbuffsize(&b, sizeof(f)); - /* move 'f' to final result, correcting endianness if needed */ - copywithendian(buff, (char *)&f, sizeof(f), h.islittle); - luaL_addsize(&b, size); - break; - } - case Knumber: { /* Lua float */ - lua_Number f = luaL_checknumber(L, arg); /* get argument */ - char *buff = luaL_prepbuffsize(&b, sizeof(f)); - /* move 'f' to final result, correcting endianness if needed */ - copywithendian(buff, (char *)&f, sizeof(f), h.islittle); - luaL_addsize(&b, size); - break; - } - case Kdouble: { /* C double */ - double f = (double)luaL_checknumber(L, arg); /* get argument */ - char *buff = luaL_prepbuffsize(&b, sizeof(f)); - /* move 'f' to final result, correcting endianness if needed */ - copywithendian(buff, (char *)&f, sizeof(f), h.islittle); - luaL_addsize(&b, size); - break; - } - case Kchar: { /* fixed-size string */ - size_t len; - const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, len <= (size_t)size, arg, - "string longer than given size"); - luaL_addlstring(&b, s, len); /* add string */ - while (len++ < (size_t)size) /* pad extra space */ - luaL_addchar(&b, LUAL_PACKPADBYTE); - break; - } - case Kstring: { /* strings with length count */ - size_t len; - const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, size >= (int)sizeof(size_t) || - len < ((size_t)1 << (size * NB)), - arg, "string length does not fit in given size"); - packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ - luaL_addlstring(&b, s, len); - totalsize += len; - break; - } - case Kzstr: { /* zero-terminated string */ - size_t len; - const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros"); - luaL_addlstring(&b, s, len); - luaL_addchar(&b, '\0'); /* add zero at the end */ - totalsize += len + 1; - break; - } - case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */ - case Kpaddalign: case Knop: - arg--; /* undo increment */ - break; - } - } - luaL_pushresult(&b); - return 1; -} - - -static int str_packsize (lua_State *L) { - Header h; - const char *fmt = luaL_checkstring(L, 1); /* format string */ - size_t totalsize = 0; /* accumulate total size of result */ - initheader(L, &h); - while (*fmt != '\0') { - int size, ntoalign; - KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); - luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, - "variable-length format"); - size += ntoalign; /* total space used by option */ - luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, - "format result too large"); - totalsize += size; - } - lua_pushinteger(L, (lua_Integer)totalsize); - return 1; -} - - -/* -** Unpack an integer with 'size' bytes and 'islittle' endianness. -** If size is smaller than the size of a Lua integer and integer -** is signed, must do sign extension (propagating the sign to the -** higher bits); if size is larger than the size of a Lua integer, -** it must check the unread bytes to see whether they do not cause an -** overflow. -*/ -static lua_Integer unpackint (lua_State *L, const char *str, - int islittle, int size, int issigned) { - lua_Unsigned res = 0; - int i; - int limit = (size <= SZINT) ? size : SZINT; - for (i = limit - 1; i >= 0; i--) { - res <<= NB; - res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; - } - if (size < SZINT) { /* real size smaller than lua_Integer? */ - if (issigned) { /* needs sign extension? */ - lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); - res = ((res ^ mask) - mask); /* do sign extension */ - } - } - else if (size > SZINT) { /* must check unread bytes */ - int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; - for (i = limit; i < size; i++) { - if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) - luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); - } - } - return (lua_Integer)res; -} - - -static int str_unpack (lua_State *L) { - Header h; - const char *fmt = luaL_checkstring(L, 1); - size_t ld; - const char *data = luaL_checklstring(L, 2, &ld); - size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1; - int n = 0; /* number of results */ - luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); - initheader(L, &h); - while (*fmt != '\0') { - int size, ntoalign; - KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); - luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2, - "data string too short"); - pos += ntoalign; /* skip alignment */ - /* stack space for item + next position */ - luaL_checkstack(L, 2, "too many results"); - n++; - switch (opt) { - case Kint: - case Kuint: { - lua_Integer res = unpackint(L, data + pos, h.islittle, size, - (opt == Kint)); - lua_pushinteger(L, res); - break; - } - case Kfloat: { - float f; - copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); - lua_pushnumber(L, (lua_Number)f); - break; - } - case Knumber: { - lua_Number f; - copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); - lua_pushnumber(L, f); - break; - } - case Kdouble: { - double f; - copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); - lua_pushnumber(L, (lua_Number)f); - break; - } - case Kchar: { - lua_pushlstring(L, data + pos, size); - break; - } - case Kstring: { - size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); - luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); - lua_pushlstring(L, data + pos + size, len); - pos += len; /* skip string */ - break; - } - case Kzstr: { - size_t len = strlen(data + pos); - luaL_argcheck(L, pos + len < ld, 2, - "unfinished string for format 'z'"); - lua_pushlstring(L, data + pos, len); - pos += len + 1; /* skip string plus final '\0' */ - break; - } - case Kpaddalign: case Kpadding: case Knop: - n--; /* undo increment */ - break; - } - pos += size; - } - lua_pushinteger(L, pos + 1); /* next position */ - return n + 1; -} - -/* }====================================================== */ - - -static const luaL_Reg strlib[] = { - {"byte", str_byte}, - {"char", str_char}, - {"dump", str_dump}, - {"find", str_find}, - {"format", str_format}, - {"gmatch", gmatch}, - {"gsub", str_gsub}, - {"len", str_len}, - {"lower", str_lower}, - {"match", str_match}, - {"rep", str_rep}, - {"reverse", str_reverse}, - {"sub", str_sub}, - {"upper", str_upper}, - {"pack", str_pack}, - {"packsize", str_packsize}, - {"unpack", str_unpack}, - {NULL, NULL} -}; - - -static void createmetatable (lua_State *L) { - /* table to be metatable for strings */ - luaL_newlibtable(L, stringmetamethods); - luaL_setfuncs(L, stringmetamethods, 0); - lua_pushliteral(L, ""); /* dummy string */ - lua_pushvalue(L, -2); /* copy table */ - lua_setmetatable(L, -2); /* set table as metatable for strings */ - lua_pop(L, 1); /* pop dummy string */ - lua_pushvalue(L, -2); /* get string library */ - lua_setfield(L, -2, "__index"); /* metatable.__index = string */ - lua_pop(L, 1); /* pop metatable */ -} - - -/* -** Open string library -*/ -LUAMOD_API int luaopen_string (lua_State *L) { - luaL_newlib(L, strlib); - createmetatable(L); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.c deleted file mode 100644 index 1b1cd24..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.c +++ /dev/null @@ -1,980 +0,0 @@ -/* -** $Id: ltable.c $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - -#define ltable_c -#define LUA_CORE - -#include "lprefix.h" - - -/* -** Implementation of tables (aka arrays, objects, or hash tables). -** Tables keep its elements in two parts: an array part and a hash part. -** Non-negative integer keys are all candidates to be kept in the array -** part. The actual size of the array is the largest 'n' such that -** more than half the slots between 1 and n are in use. -** Hash uses a mix of chained scatter table with Brent's variation. -** A main invariant of these tables is that, if an element is not -** in its main position (i.e. the 'original' position that its hash gives -** to it), then the colliding element is in its own main position. -** Hence even when the load factor reaches 100%, performance remains good. -*/ - -#include -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "lvm.h" - - -/* -** MAXABITS is the largest integer such that MAXASIZE fits in an -** unsigned int. -*/ -#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) - - -/* -** MAXASIZE is the maximum size of the array part. It is the minimum -** between 2^MAXABITS and the maximum size that, measured in bytes, -** fits in a 'size_t'. -*/ -#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) - -/* -** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a -** signed int. -*/ -#define MAXHBITS (MAXABITS - 1) - - -/* -** MAXHSIZE is the maximum size of the hash part. It is the minimum -** between 2^MAXHBITS and the maximum size such that, measured in bytes, -** it fits in a 'size_t'. -*/ -#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) - - -/* -** When the original hash value is good, hashing by a power of 2 -** avoids the cost of '%'. -*/ -#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) - -/* -** for other types, it is better to avoid modulo by power of 2, as -** they can have many 2 factors. -*/ -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) - - -#define hashstr(t,str) hashpow2(t, (str)->hash) -#define hashboolean(t,p) hashpow2(t, p) - - -#define hashpointer(t,p) hashmod(t, point2uint(p)) - - -#define dummynode (&dummynode_) - -static const Node dummynode_ = { - {{NULL}, LUA_VEMPTY, /* value's value and type */ - LUA_VNIL, 0, {NULL}} /* key type, next, and key value */ -}; - - -static const TValue absentkey = {ABSTKEYCONSTANT}; - - -/* -** Hash for integers. To allow a good hash, use the remainder operator -** ('%'). If integer fits as a non-negative int, compute an int -** remainder, which is faster. Otherwise, use an unsigned-integer -** remainder, which uses all bits and ensures a non-negative result. -*/ -static Node *hashint (const Table *t, lua_Integer i) { - lua_Unsigned ui = l_castS2U(i); - if (ui <= (unsigned int)INT_MAX) - return hashmod(t, cast_int(ui)); - else - return hashmod(t, ui); -} - - -/* -** Hash for floating-point numbers. -** The main computation should be just -** n = frexp(n, &i); return (n * INT_MAX) + i -** but there are some numerical subtleties. -** In a two-complement representation, INT_MAX does not has an exact -** representation as a float, but INT_MIN does; because the absolute -** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the -** absolute value of the product 'frexp * -INT_MIN' is smaller or equal -** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when -** adding 'i'; the use of '~u' (instead of '-u') avoids problems with -** INT_MIN. -*/ -#if !defined(l_hashfloat) -static int l_hashfloat (lua_Number n) { - int i; - lua_Integer ni; - n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); - if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ - lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); - return 0; - } - else { /* normal case */ - unsigned int u = cast_uint(i) + cast_uint(ni); - return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); - } -} -#endif - - -/* -** returns the 'main' position of an element in a table (that is, -** the index of its hash value). -*/ -static Node *mainpositionTV (const Table *t, const TValue *key) { - switch (ttypetag(key)) { - case LUA_VNUMINT: { - lua_Integer i = ivalue(key); - return hashint(t, i); - } - case LUA_VNUMFLT: { - lua_Number n = fltvalue(key); - return hashmod(t, l_hashfloat(n)); - } - case LUA_VSHRSTR: { - TString *ts = tsvalue(key); - return hashstr(t, ts); - } - case LUA_VLNGSTR: { - TString *ts = tsvalue(key); - return hashpow2(t, luaS_hashlongstr(ts)); - } - case LUA_VFALSE: - return hashboolean(t, 0); - case LUA_VTRUE: - return hashboolean(t, 1); - case LUA_VLIGHTUSERDATA: { - void *p = pvalue(key); - return hashpointer(t, p); - } - case LUA_VLCF: { - lua_CFunction f = fvalue(key); - return hashpointer(t, f); - } - default: { - GCObject *o = gcvalue(key); - return hashpointer(t, o); - } - } -} - - -l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { - TValue key; - getnodekey(cast(lua_State *, NULL), &key, nd); - return mainpositionTV(t, &key); -} - - -/* -** Check whether key 'k1' is equal to the key in node 'n2'. This -** equality is raw, so there are no metamethods. Floats with integer -** values have been normalized, so integers cannot be equal to -** floats. It is assumed that 'eqshrstr' is simply pointer equality, so -** that short strings are handled in the default case. -** A true 'deadok' means to accept dead keys as equal to their original -** values. All dead keys are compared in the default case, by pointer -** identity. (Only collectable objects can produce dead keys.) Note that -** dead long strings are also compared by identity. -** Once a key is dead, its corresponding value may be collected, and -** then another value can be created with the same address. If this -** other value is given to 'next', 'equalkey' will signal a false -** positive. In a regular traversal, this situation should never happen, -** as all keys given to 'next' came from the table itself, and therefore -** could not have been collected. Outside a regular traversal, we -** have garbage in, garbage out. What is relevant is that this false -** positive does not break anything. (In particular, 'next' will return -** some other valid item on the table or nil.) -*/ -static int equalkey (const TValue *k1, const Node *n2, int deadok) { - if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ - !(deadok && keyisdead(n2) && iscollectable(k1))) - return 0; /* cannot be same key */ - switch (keytt(n2)) { - case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: - return 1; - case LUA_VNUMINT: - return (ivalue(k1) == keyival(n2)); - case LUA_VNUMFLT: - return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); - case LUA_VLIGHTUSERDATA: - return pvalue(k1) == pvalueraw(keyval(n2)); - case LUA_VLCF: - return fvalue(k1) == fvalueraw(keyval(n2)); - case ctb(LUA_VLNGSTR): - return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); - default: - return gcvalue(k1) == gcvalueraw(keyval(n2)); - } -} - - -/* -** True if value of 'alimit' is equal to the real size of the array -** part of table 't'. (Otherwise, the array part must be larger than -** 'alimit'.) -*/ -#define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit)) - - -/* -** Returns the real size of the 'array' array -*/ -LUAI_FUNC unsigned int luaH_realasize (const Table *t) { - if (limitequalsasize(t)) - return t->alimit; /* this is the size */ - else { - unsigned int size = t->alimit; - /* compute the smallest power of 2 not smaller than 'n' */ - size |= (size >> 1); - size |= (size >> 2); - size |= (size >> 4); - size |= (size >> 8); - size |= (size >> 16); -#if (UINT_MAX >> 30) > 3 - size |= (size >> 32); /* unsigned int has more than 32 bits */ -#endif - size++; - lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); - return size; - } -} - - -/* -** Check whether real size of the array is a power of 2. -** (If it is not, 'alimit' cannot be changed to any other value -** without changing the real size.) -*/ -static int ispow2realasize (const Table *t) { - return (!isrealasize(t) || ispow2(t->alimit)); -} - - -static unsigned int setlimittosize (Table *t) { - t->alimit = luaH_realasize(t); - setrealasize(t); - return t->alimit; -} - - -#define limitasasize(t) check_exp(isrealasize(t), t->alimit) - - - -/* -** "Generic" get version. (Not that generic: not valid for integers, -** which may be in array part, nor for floats with integral values.) -** See explanation about 'deadok' in function 'equalkey'. -*/ -static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { - Node *n = mainpositionTV(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (equalkey(key, n, deadok)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) - return &absentkey; /* not found */ - n += nx; - } - } -} - - -/* -** returns the index for 'k' if 'k' is an appropriate key to live in -** the array part of a table, 0 otherwise. -*/ -static unsigned int arrayindex (lua_Integer k) { - if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */ - return cast_uint(k); /* 'key' is an appropriate array index */ - else - return 0; -} - - -/* -** returns the index of a 'key' for table traversals. First goes all -** elements in the array part, then elements in the hash part. The -** beginning of a traversal is signaled by 0. -*/ -static unsigned int findindex (lua_State *L, Table *t, TValue *key, - unsigned int asize) { - unsigned int i; - if (ttisnil(key)) return 0; /* first iteration */ - i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; - if (i - 1u < asize) /* is 'key' inside array part? */ - return i; /* yes; that's the index */ - else { - const TValue *n = getgeneric(t, key, 1); - if (l_unlikely(isabstkey(n))) - luaG_runerror(L, "invalid key to 'next'"); /* key not found */ - i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ - /* hash elements are numbered after array ones */ - return (i + 1) + asize; - } -} - - -int luaH_next (lua_State *L, Table *t, StkId key) { - unsigned int asize = luaH_realasize(t); - unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ - for (; i < asize; i++) { /* try first array part */ - if (!isempty(&t->array[i])) { /* a non-empty entry? */ - setivalue(s2v(key), i + 1); - setobj2s(L, key + 1, &t->array[i]); - return 1; - } - } - for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ - if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ - Node *n = gnode(t, i); - getnodekey(L, s2v(key), n); - setobj2s(L, key + 1, gval(n)); - return 1; - } - } - return 0; /* no more elements */ -} - - -static void freehash (lua_State *L, Table *t) { - if (!isdummy(t)) - luaM_freearray(L, t->node, cast_sizet(sizenode(t))); -} - - -/* -** {============================================================= -** Rehash -** ============================================================== -*/ - -/* -** Compute the optimal size for the array part of table 't'. 'nums' is a -** "count array" where 'nums[i]' is the number of integers in the table -** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of -** integer keys in the table and leaves with the number of keys that -** will go to the array part; return the optimal size. (The condition -** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) -*/ -static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { - int i; - unsigned int twotoi; /* 2^i (candidate for optimal size) */ - unsigned int a = 0; /* number of elements smaller than 2^i */ - unsigned int na = 0; /* number of elements to go to array part */ - unsigned int optimal = 0; /* optimal size for array part */ - /* loop while keys can fill more than half of total size */ - for (i = 0, twotoi = 1; - twotoi > 0 && *pna > twotoi / 2; - i++, twotoi *= 2) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ - optimal = twotoi; /* optimal size (till now) */ - na = a; /* all elements up to 'optimal' will go to array part */ - } - } - lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); - *pna = na; - return optimal; -} - - -static int countint (lua_Integer key, unsigned int *nums) { - unsigned int k = arrayindex(key); - if (k != 0) { /* is 'key' an appropriate array index? */ - nums[luaO_ceillog2(k)]++; /* count as such */ - return 1; - } - else - return 0; -} - - -/* -** Count keys in array part of table 't': Fill 'nums[i]' with -** number of keys that will go into corresponding slice and return -** total number of non-nil keys. -*/ -static unsigned int numusearray (const Table *t, unsigned int *nums) { - int lg; - unsigned int ttlg; /* 2^lg */ - unsigned int ause = 0; /* summation of 'nums' */ - unsigned int i = 1; /* count to traverse all array keys */ - unsigned int asize = limitasasize(t); /* real array size */ - /* traverse each slice */ - for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { - unsigned int lc = 0; /* counter */ - unsigned int lim = ttlg; - if (lim > asize) { - lim = asize; /* adjust upper limit */ - if (i > lim) - break; /* no more elements to count */ - } - /* count elements in range (2^(lg - 1), 2^lg] */ - for (; i <= lim; i++) { - if (!isempty(&t->array[i-1])) - lc++; - } - nums[lg] += lc; - ause += lc; - } - return ause; -} - - -static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { - int totaluse = 0; /* total number of elements */ - int ause = 0; /* elements added to 'nums' (can go to array part) */ - int i = sizenode(t); - while (i--) { - Node *n = &t->node[i]; - if (!isempty(gval(n))) { - if (keyisinteger(n)) - ause += countint(keyival(n), nums); - totaluse++; - } - } - *pna += ause; - return totaluse; -} - - -/* -** Creates an array for the hash part of a table with the given -** size, or reuses the dummy node if size is zero. -** The computation for size overflow is in two steps: the first -** comparison ensures that the shift in the second one does not -** overflow. -*/ -static void setnodevector (lua_State *L, Table *t, unsigned int size) { - if (size == 0) { /* no elements to hash part? */ - t->node = cast(Node *, dummynode); /* use common 'dummynode' */ - t->lsizenode = 0; - t->lastfree = NULL; /* signal that it is using dummy node */ - } - else { - int i; - int lsize = luaO_ceillog2(size); - if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) - luaG_runerror(L, "table overflow"); - size = twoto(lsize); - t->node = luaM_newvector(L, size, Node); - for (i = 0; i < (int)size; i++) { - Node *n = gnode(t, i); - gnext(n) = 0; - setnilkey(n); - setempty(gval(n)); - } - t->lsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ - } -} - - -/* -** (Re)insert all elements from the hash part of 'ot' into table 't'. -*/ -static void reinsert (lua_State *L, Table *ot, Table *t) { - int j; - int size = sizenode(ot); - for (j = 0; j < size; j++) { - Node *old = gnode(ot, j); - if (!isempty(gval(old))) { - /* doesn't need barrier/invalidate cache, as entry was - already present in the table */ - TValue k; - getnodekey(L, &k, old); - luaH_set(L, t, &k, gval(old)); - } - } -} - - -/* -** Exchange the hash part of 't1' and 't2'. -*/ -static void exchangehashpart (Table *t1, Table *t2) { - lu_byte lsizenode = t1->lsizenode; - Node *node = t1->node; - Node *lastfree = t1->lastfree; - t1->lsizenode = t2->lsizenode; - t1->node = t2->node; - t1->lastfree = t2->lastfree; - t2->lsizenode = lsizenode; - t2->node = node; - t2->lastfree = lastfree; -} - - -/* -** Resize table 't' for the new given sizes. Both allocations (for -** the hash part and for the array part) can fail, which creates some -** subtleties. If the first allocation, for the hash part, fails, an -** error is raised and that is it. Otherwise, it copies the elements from -** the shrinking part of the array (if it is shrinking) into the new -** hash. Then it reallocates the array part. If that fails, the table -** is in its original state; the function frees the new hash part and then -** raises the allocation error. Otherwise, it sets the new hash part -** into the table, initializes the new part of the array (if any) with -** nils and reinserts the elements of the old hash back into the new -** parts of the table. -*/ -void luaH_resize (lua_State *L, Table *t, unsigned int newasize, - unsigned int nhsize) { - unsigned int i; - Table newt; /* to keep the new hash part */ - unsigned int oldasize = setlimittosize(t); - TValue *newarray; - /* create new hash part with appropriate size into 'newt' */ - setnodevector(L, &newt, nhsize); - if (newasize < oldasize) { /* will array shrink? */ - t->alimit = newasize; /* pretend array has new size... */ - exchangehashpart(t, &newt); /* and new hash */ - /* re-insert into the new hash the elements from vanishing slice */ - for (i = newasize; i < oldasize; i++) { - if (!isempty(&t->array[i])) - luaH_setint(L, t, i + 1, &t->array[i]); - } - t->alimit = oldasize; /* restore current size... */ - exchangehashpart(t, &newt); /* and hash (in case of errors) */ - } - /* allocate new array */ - newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); - if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ - freehash(L, &newt); /* release new hash part */ - luaM_error(L); /* raise error (with array unchanged) */ - } - /* allocation ok; initialize new part of the array */ - exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ - t->array = newarray; /* set new array part */ - t->alimit = newasize; - for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ - setempty(&t->array[i]); - /* re-insert elements from old hash part into new parts */ - reinsert(L, &newt, t); /* 'newt' now has the old hash */ - freehash(L, &newt); /* free old hash part */ -} - - -void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { - int nsize = allocsizenode(t); - luaH_resize(L, t, nasize, nsize); -} - -/* -** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i -*/ -static void rehash (lua_State *L, Table *t, const TValue *ek) { - unsigned int asize; /* optimal size for array part */ - unsigned int na; /* number of keys in the array part */ - unsigned int nums[MAXABITS + 1]; - int i; - int totaluse; - for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ - setlimittosize(t); - na = numusearray(t, nums); /* count keys in array part */ - totaluse = na; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &na); /* count keys in hash part */ - /* count extra key */ - if (ttisinteger(ek)) - na += countint(ivalue(ek), nums); - totaluse++; - /* compute new size for array part */ - asize = computesizes(nums, &na); - /* resize the table to new computed sizes */ - luaH_resize(L, t, asize, totaluse - na); -} - - - -/* -** }============================================================= -*/ - - -Table *luaH_new (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); - Table *t = gco2t(o); - t->metatable = NULL; - t->flags = cast_byte(maskflags); /* table has no metamethod fields */ - t->array = NULL; - t->alimit = 0; - setnodevector(L, t, 0); - return t; -} - - -void luaH_free (lua_State *L, Table *t) { - freehash(L, t); - luaM_freearray(L, t->array, luaH_realasize(t)); - luaM_free(L, t); -} - - -static Node *getfreepos (Table *t) { - if (!isdummy(t)) { - while (t->lastfree > t->node) { - t->lastfree--; - if (keyisnil(t->lastfree)) - return t->lastfree; - } - } - return NULL; /* could not find a free place */ -} - - - -/* -** inserts a new key into a hash table; first, check whether key's main -** position is free. If not, check whether colliding node is in its main -** position or not: if it is not, move colliding node to an empty place and -** put new key in its main position; otherwise (colliding node is in its main -** position), new key goes to an empty position. -*/ -void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { - Node *mp; - TValue aux; - if (l_unlikely(ttisnil(key))) - luaG_runerror(L, "table index is nil"); - else if (ttisfloat(key)) { - lua_Number f = fltvalue(key); - lua_Integer k; - if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */ - setivalue(&aux, k); - key = &aux; /* insert it as an integer */ - } - else if (l_unlikely(luai_numisnan(f))) - luaG_runerror(L, "table index is NaN"); - } - if (ttisnil(value)) - return; /* do not insert nil values */ - mp = mainpositionTV(t, key); - if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ - Node *othern; - Node *f = getfreepos(t); /* get a free place */ - if (f == NULL) { /* cannot find a free place? */ - rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' takes care of TM cache */ - luaH_set(L, t, key, value); /* insert key into grown table */ - return; - } - lua_assert(!isdummy(t)); - othern = mainpositionfromnode(t, mp); - if (othern != mp) { /* is colliding node out of its main position? */ - /* yes; move colliding node into free position */ - while (othern + gnext(othern) != mp) /* find previous */ - othern += gnext(othern); - gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ - *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ - if (gnext(mp) != 0) { - gnext(f) += cast_int(mp - f); /* correct 'next' */ - gnext(mp) = 0; /* now 'mp' is free */ - } - setempty(gval(mp)); - } - else { /* colliding node is in its own main position */ - /* new node will go into free position */ - if (gnext(mp) != 0) - gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ - else lua_assert(gnext(f) == 0); - gnext(mp) = cast_int(f - mp); - mp = f; - } - } - setnodekey(L, mp, key); - luaC_barrierback(L, obj2gco(t), key); - lua_assert(isempty(gval(mp))); - setobj2t(L, gval(mp), value); -} - - -/* -** Search function for integers. If integer is inside 'alimit', get it -** directly from the array part. Otherwise, if 'alimit' is not equal to -** the real size of the array, key still can be in the array part. In -** this case, try to avoid a call to 'luaH_realasize' when key is just -** one more than the limit (so that it can be incremented without -** changing the real size of the array). -*/ -const TValue *luaH_getint (Table *t, lua_Integer key) { - if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ - return &t->array[key - 1]; - else if (!limitequalsasize(t) && /* key still may be in the array part? */ - (l_castS2U(key) == t->alimit + 1 || - l_castS2U(key) - 1u < luaH_realasize(t))) { - t->alimit = cast_uint(key); /* probably '#t' is here now */ - return &t->array[key - 1]; - } - else { - Node *n = hashint(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (keyisinteger(n) && keyival(n) == key) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) break; - n += nx; - } - } - return &absentkey; - } -} - - -/* -** search function for short strings -*/ -const TValue *luaH_getshortstr (Table *t, TString *key) { - Node *n = hashstr(t, key); - lua_assert(key->tt == LUA_VSHRSTR); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) - return &absentkey; /* not found */ - n += nx; - } - } -} - - -const TValue *luaH_getstr (Table *t, TString *key) { - if (key->tt == LUA_VSHRSTR) - return luaH_getshortstr(t, key); - else { /* for long strings, use generic case */ - TValue ko; - setsvalue(cast(lua_State *, NULL), &ko, key); - return getgeneric(t, &ko, 0); - } -} - - -/* -** main search function -*/ -const TValue *luaH_get (Table *t, const TValue *key) { - switch (ttypetag(key)) { - case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key)); - case LUA_VNUMINT: return luaH_getint(t, ivalue(key)); - case LUA_VNIL: return &absentkey; - case LUA_VNUMFLT: { - lua_Integer k; - if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_getint(t, k); /* use specialized version */ - /* else... */ - } /* FALLTHROUGH */ - default: - return getgeneric(t, key, 0); - } -} - - -/* -** Finish a raw "set table" operation, where 'slot' is where the value -** should have been (the result of a previous "get table"). -** Beware: when using this function you probably need to check a GC -** barrier and invalidate the TM cache. -*/ -void luaH_finishset (lua_State *L, Table *t, const TValue *key, - const TValue *slot, TValue *value) { - if (isabstkey(slot)) - luaH_newkey(L, t, key, value); - else - setobj2t(L, cast(TValue *, slot), value); -} - - -/* -** beware: when using this function you probably need to check a GC -** barrier and invalidate the TM cache. -*/ -void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { - const TValue *slot = luaH_get(t, key); - luaH_finishset(L, t, key, slot, value); -} - - -void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - const TValue *p = luaH_getint(t, key); - if (isabstkey(p)) { - TValue k; - setivalue(&k, key); - luaH_newkey(L, t, &k, value); - } - else - setobj2t(L, cast(TValue *, p), value); -} - - -/* -** Try to find a boundary in the hash part of table 't'. From the -** caller, we know that 'j' is zero or present and that 'j + 1' is -** present. We want to find a larger key that is absent from the -** table, so that we can do a binary search between the two keys to -** find a boundary. We keep doubling 'j' until we get an absent index. -** If the doubling would overflow, we try LUA_MAXINTEGER. If it is -** absent, we are ready for the binary search. ('j', being max integer, -** is larger or equal to 'i', but it cannot be equal because it is -** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a -** boundary. ('j + 1' cannot be a present integer key because it is -** not a valid integer in Lua.) -*/ -static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { - lua_Unsigned i; - if (j == 0) j++; /* the caller ensures 'j + 1' is present */ - do { - i = j; /* 'i' is a present index */ - if (j <= l_castS2U(LUA_MAXINTEGER) / 2) - j *= 2; - else { - j = LUA_MAXINTEGER; - if (isempty(luaH_getint(t, j))) /* t[j] not present? */ - break; /* 'j' now is an absent index */ - else /* weird case */ - return j; /* well, max integer is a boundary... */ - } - } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */ - /* i < j && t[i] present && t[j] absent */ - while (j - i > 1u) { /* do a binary search between them */ - lua_Unsigned m = (i + j) / 2; - if (isempty(luaH_getint(t, m))) j = m; - else i = m; - } - return i; -} - - -static unsigned int binsearch (const TValue *array, unsigned int i, - unsigned int j) { - while (j - i > 1u) { /* binary search */ - unsigned int m = (i + j) / 2; - if (isempty(&array[m - 1])) j = m; - else i = m; - } - return i; -} - - -/* -** Try to find a boundary in table 't'. (A 'boundary' is an integer index -** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent -** and 'maxinteger' if t[maxinteger] is present.) -** (In the next explanation, we use Lua indices, that is, with base 1. -** The code itself uses base 0 when indexing the array part of the table.) -** The code starts with 'limit = t->alimit', a position in the array -** part that may be a boundary. -** -** (1) If 't[limit]' is empty, there must be a boundary before it. -** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1' -** is present. If so, it is a boundary. Otherwise, do a binary search -** between 0 and limit to find a boundary. In both cases, try to -** use this boundary as the new 'alimit', as a hint for the next call. -** -** (2) If 't[limit]' is not empty and the array has more elements -** after 'limit', try to find a boundary there. Again, try first -** the special case (which should be quite frequent) where 'limit+1' -** is empty, so that 'limit' is a boundary. Otherwise, check the -** last element of the array part. If it is empty, there must be a -** boundary between the old limit (present) and the last element -** (absent), which is found with a binary search. (This boundary always -** can be a new limit.) -** -** (3) The last case is when there are no elements in the array part -** (limit == 0) or its last element (the new limit) is present. -** In this case, must check the hash part. If there is no hash part -** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call -** 'hash_search' to find a boundary in the hash part of the table. -** (In those cases, the boundary is not inside the array part, and -** therefore cannot be used as a new limit.) -*/ -lua_Unsigned luaH_getn (Table *t) { - unsigned int limit = t->alimit; - if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ - /* there must be a boundary before 'limit' */ - if (limit >= 2 && !isempty(&t->array[limit - 2])) { - /* 'limit - 1' is a boundary; can it be a new limit? */ - if (ispow2realasize(t) && !ispow2(limit - 1)) { - t->alimit = limit - 1; - setnorealasize(t); /* now 'alimit' is not the real size */ - } - return limit - 1; - } - else { /* must search for a boundary in [0, limit] */ - unsigned int boundary = binsearch(t->array, 0, limit); - /* can this boundary represent the real size of the array? */ - if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { - t->alimit = boundary; /* use it as the new limit */ - setnorealasize(t); - } - return boundary; - } - } - /* 'limit' is zero or present in table */ - if (!limitequalsasize(t)) { /* (2)? */ - /* 'limit' > 0 and array has more elements after 'limit' */ - if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ - return limit; /* this is the boundary */ - /* else, try last element in the array */ - limit = luaH_realasize(t); - if (isempty(&t->array[limit - 1])) { /* empty? */ - /* there must be a boundary in the array after old limit, - and it must be a valid new limit */ - unsigned int boundary = binsearch(t->array, t->alimit, limit); - t->alimit = boundary; - return boundary; - } - /* else, new limit is present in the table; check the hash part */ - } - /* (3) 'limit' is the last element and either is zero or present in table */ - lua_assert(limit == luaH_realasize(t) && - (limit == 0 || !isempty(&t->array[limit - 1]))); - if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) - return limit; /* 'limit + 1' is absent */ - else /* 'limit + 1' is also present */ - return hash_search(t, limit); -} - - - -#if defined(LUA_DEBUG) - -/* export these functions for the test library */ - -Node *luaH_mainposition (const Table *t, const TValue *key) { - return mainpositionTV(t, key); -} - -int luaH_isdummy (const Table *t) { return isdummy(t); } - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.h deleted file mode 100644 index 7bbbcb2..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltable.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -** $Id: ltable.h $ -** Lua tables (hash) -** See Copyright Notice in lua.h -*/ - -#ifndef ltable_h -#define ltable_h - -#include "lobject.h" - - -#define gnode(t,i) (&(t)->node[i]) -#define gval(n) (&(n)->i_val) -#define gnext(n) ((n)->u.next) - - -/* -** Clear all bits of fast-access metamethods, which means that the table -** may have any of these metamethods. (First access that fails after the -** clearing will set the bit again.) -*/ -#define invalidateTMcache(t) ((t)->flags &= ~maskflags) - - -/* true when 't' is using 'dummynode' as its hash part */ -#define isdummy(t) ((t)->lastfree == NULL) - - -/* allocated size for hash nodes */ -#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) - - -/* returns the Node, given the value of a table entry */ -#define nodefromval(v) cast(Node *, (v)) - - -LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); -LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, - TValue *value); -LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, - TValue *value); -LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, - TValue *value); -LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, - const TValue *slot, TValue *value); -LUAI_FUNC Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); -LUAI_FUNC void luaH_free (lua_State *L, Table *t); -LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC lua_Unsigned luaH_getn (Table *t); -LUAI_FUNC unsigned int luaH_realasize (const Table *t); - - -#if defined(LUA_DEBUG) -LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (const Table *t); -#endif - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltablib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltablib.c deleted file mode 100644 index 868d78f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltablib.c +++ /dev/null @@ -1,430 +0,0 @@ -/* -** $Id: ltablib.c $ -** Library for Table Manipulation -** See Copyright Notice in lua.h -*/ - -#define ltablib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -/* -** Operations that an object must define to mimic a table -** (some functions only need some of them) -*/ -#define TAB_R 1 /* read */ -#define TAB_W 2 /* write */ -#define TAB_L 4 /* length */ -#define TAB_RW (TAB_R | TAB_W) /* read/write */ - - -#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) - - -static int checkfield (lua_State *L, const char *key, int n) { - lua_pushstring(L, key); - return (lua_rawget(L, -n) != LUA_TNIL); -} - - -/* -** Check that 'arg' either is a table or can behave like one (that is, -** has a metatable with the required metamethods) -*/ -static void checktab (lua_State *L, int arg, int what) { - if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ - int n = 1; /* number of elements to pop */ - if (lua_getmetatable(L, arg) && /* must have metatable */ - (!(what & TAB_R) || checkfield(L, "__index", ++n)) && - (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && - (!(what & TAB_L) || checkfield(L, "__len", ++n))) { - lua_pop(L, n); /* pop metatable and tested metamethods */ - } - else - luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ - } -} - - -static int tinsert (lua_State *L) { - lua_Integer pos; /* where to insert new element */ - lua_Integer e = aux_getn(L, 1, TAB_RW); - e = luaL_intop(+, e, 1); /* first empty element */ - switch (lua_gettop(L)) { - case 2: { /* called with only 2 arguments */ - pos = e; /* insert new element at the end */ - break; - } - case 3: { - lua_Integer i; - pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ - /* check whether 'pos' is in [1, e] */ - luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, - "position out of bounds"); - for (i = e; i > pos; i--) { /* move up elements */ - lua_geti(L, 1, i - 1); - lua_seti(L, 1, i); /* t[i] = t[i - 1] */ - } - break; - } - default: { - return luaL_error(L, "wrong number of arguments to 'insert'"); - } - } - lua_seti(L, 1, pos); /* t[pos] = v */ - return 0; -} - - -static int tremove (lua_State *L) { - lua_Integer size = aux_getn(L, 1, TAB_RW); - lua_Integer pos = luaL_optinteger(L, 2, size); - if (pos != size) /* validate 'pos' if given */ - /* check whether 'pos' is in [1, size + 1] */ - luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1, - "position out of bounds"); - lua_geti(L, 1, pos); /* result = t[pos] */ - for ( ; pos < size; pos++) { - lua_geti(L, 1, pos + 1); - lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ - } - lua_pushnil(L); - lua_seti(L, 1, pos); /* remove entry t[pos] */ - return 1; -} - - -/* -** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever -** possible, copy in increasing order, which is better for rehashing. -** "possible" means destination after original range, or smaller -** than origin, or copying to another table. -*/ -static int tmove (lua_State *L) { - lua_Integer f = luaL_checkinteger(L, 2); - lua_Integer e = luaL_checkinteger(L, 3); - lua_Integer t = luaL_checkinteger(L, 4); - int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ - checktab(L, 1, TAB_R); - checktab(L, tt, TAB_W); - if (e >= f) { /* otherwise, nothing to move */ - lua_Integer n, i; - luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, - "too many elements to move"); - n = e - f + 1; /* number of elements to move */ - luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, - "destination wrap around"); - if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { - for (i = 0; i < n; i++) { - lua_geti(L, 1, f + i); - lua_seti(L, tt, t + i); - } - } - else { - for (i = n - 1; i >= 0; i--) { - lua_geti(L, 1, f + i); - lua_seti(L, tt, t + i); - } - } - } - lua_pushvalue(L, tt); /* return destination table */ - return 1; -} - - -static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { - lua_geti(L, 1, i); - if (l_unlikely(!lua_isstring(L, -1))) - luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", - luaL_typename(L, -1), (LUAI_UACINT)i); - luaL_addvalue(b); -} - - -static int tconcat (lua_State *L) { - luaL_Buffer b; - lua_Integer last = aux_getn(L, 1, TAB_R); - size_t lsep; - const char *sep = luaL_optlstring(L, 2, "", &lsep); - lua_Integer i = luaL_optinteger(L, 3, 1); - last = luaL_optinteger(L, 4, last); - luaL_buffinit(L, &b); - for (; i < last; i++) { - addfield(L, &b, i); - luaL_addlstring(&b, sep, lsep); - } - if (i == last) /* add last value (if interval was not empty) */ - addfield(L, &b, i); - luaL_pushresult(&b); - return 1; -} - - -/* -** {====================================================== -** Pack/unpack -** ======================================================= -*/ - -static int tpack (lua_State *L) { - int i; - int n = lua_gettop(L); /* number of elements to pack */ - lua_createtable(L, n, 1); /* create result table */ - lua_insert(L, 1); /* put it at index 1 */ - for (i = n; i >= 1; i--) /* assign elements */ - lua_seti(L, 1, i); - lua_pushinteger(L, n); - lua_setfield(L, 1, "n"); /* t.n = number of elements */ - return 1; /* return table */ -} - - -static int tunpack (lua_State *L) { - lua_Unsigned n; - lua_Integer i = luaL_optinteger(L, 2, 1); - lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); - if (i > e) return 0; /* empty range */ - n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ - if (l_unlikely(n >= (unsigned int)INT_MAX || - !lua_checkstack(L, (int)(++n)))) - return luaL_error(L, "too many results to unpack"); - for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ - lua_geti(L, 1, i); - } - lua_geti(L, 1, e); /* push last element */ - return (int)n; -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Quicksort -** (based on 'Algorithms in MODULA-3', Robert Sedgewick; -** Addison-Wesley, 1993.) -** ======================================================= -*/ - - -/* type for array indices */ -typedef unsigned int IdxT; - - -/* -** Produce a "random" 'unsigned int' to randomize pivot choice. This -** macro is used only when 'sort' detects a big imbalance in the result -** of a partition. (If you don't want/need this "randomness", ~0 is a -** good choice.) -*/ -#if !defined(l_randomizePivot) /* { */ - -#include - -/* size of 'e' measured in number of 'unsigned int's */ -#define sof(e) (sizeof(e) / sizeof(unsigned int)) - -/* -** Use 'time' and 'clock' as sources of "randomness". Because we don't -** know the types 'clock_t' and 'time_t', we cannot cast them to -** anything without risking overflows. A safe way to use their values -** is to copy them to an array of a known type and use the array values. -*/ -static unsigned int l_randomizePivot (void) { - clock_t c = clock(); - time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; - unsigned int i, rnd = 0; - memcpy(buff, &c, sof(c) * sizeof(unsigned int)); - memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); - for (i = 0; i < sof(buff); i++) - rnd += buff[i]; - return rnd; -} - -#endif /* } */ - - -/* arrays larger than 'RANLIMIT' may use randomized pivots */ -#define RANLIMIT 100u - - -static void set2 (lua_State *L, IdxT i, IdxT j) { - lua_seti(L, 1, i); - lua_seti(L, 1, j); -} - - -/* -** Return true iff value at stack index 'a' is less than the value at -** index 'b' (according to the order of the sort). -*/ -static int sort_comp (lua_State *L, int a, int b) { - if (lua_isnil(L, 2)) /* no function? */ - return lua_compare(L, a, b, LUA_OPLT); /* a < b */ - else { /* function */ - int res; - lua_pushvalue(L, 2); /* push function */ - lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ - lua_call(L, 2, 1); /* call function */ - res = lua_toboolean(L, -1); /* get result */ - lua_pop(L, 1); /* pop result */ - return res; - } -} - - -/* -** Does the partition: Pivot P is at the top of the stack. -** precondition: a[lo] <= P == a[up-1] <= a[up], -** so it only needs to do the partition from lo + 1 to up - 2. -** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] -** returns 'i'. -*/ -static IdxT partition (lua_State *L, IdxT lo, IdxT up) { - IdxT i = lo; /* will be incremented before first use */ - IdxT j = up - 1; /* will be decremented before first use */ - /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ - for (;;) { - /* next loop: repeat ++i while a[i] < P */ - while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ - luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[i] */ - } - /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ - /* next loop: repeat --j while P < a[j] */ - while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { - if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ - luaL_error(L, "invalid order function for sorting"); - lua_pop(L, 1); /* remove a[j] */ - } - /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ - if (j < i) { /* no elements out of place? */ - /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ - lua_pop(L, 1); /* pop a[j] */ - /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ - set2(L, up - 1, i); - return i; - } - /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ - set2(L, i, j); - } -} - - -/* -** Choose an element in the middle (2nd-3th quarters) of [lo,up] -** "randomized" by 'rnd' -*/ -static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { - IdxT r4 = (up - lo) / 4; /* range/4 */ - IdxT p = rnd % (r4 * 2) + (lo + r4); - lua_assert(lo + r4 <= p && p <= up - r4); - return p; -} - - -/* -** Quicksort algorithm (recursive function) -*/ -static void auxsort (lua_State *L, IdxT lo, IdxT up, - unsigned int rnd) { - while (lo < up) { /* loop for tail recursion */ - IdxT p; /* Pivot index */ - IdxT n; /* to be used later */ - /* sort elements 'lo', 'p', and 'up' */ - lua_geti(L, 1, lo); - lua_geti(L, 1, up); - if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ - set2(L, lo, up); /* swap a[lo] - a[up] */ - else - lua_pop(L, 2); /* remove both values */ - if (up - lo == 1) /* only 2 elements? */ - return; /* already sorted */ - if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */ - p = (lo + up)/2; /* middle element is a good pivot */ - else /* for larger intervals, it is worth a random pivot */ - p = choosePivot(lo, up, rnd); - lua_geti(L, 1, p); - lua_geti(L, 1, lo); - if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ - set2(L, p, lo); /* swap a[p] - a[lo] */ - else { - lua_pop(L, 1); /* remove a[lo] */ - lua_geti(L, 1, up); - if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ - set2(L, p, up); /* swap a[up] - a[p] */ - else - lua_pop(L, 2); - } - if (up - lo == 2) /* only 3 elements? */ - return; /* already sorted */ - lua_geti(L, 1, p); /* get middle element (Pivot) */ - lua_pushvalue(L, -1); /* push Pivot */ - lua_geti(L, 1, up - 1); /* push a[up - 1] */ - set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ - p = partition(L, lo, up); - /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ - if (p - lo < up - p) { /* lower interval is smaller? */ - auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ - n = p - lo; /* size of smaller interval */ - lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ - } - else { - auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ - n = up - p; /* size of smaller interval */ - up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ - } - if ((up - lo) / 128 > n) /* partition too imbalanced? */ - rnd = l_randomizePivot(); /* try a new randomization */ - } /* tail call auxsort(L, lo, up, rnd) */ -} - - -static int sort (lua_State *L) { - lua_Integer n = aux_getn(L, 1, TAB_RW); - if (n > 1) { /* non-trivial interval? */ - luaL_argcheck(L, n < INT_MAX, 1, "array too big"); - if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ - luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ - lua_settop(L, 2); /* make sure there are two arguments */ - auxsort(L, 1, (IdxT)n, 0); - } - return 0; -} - -/* }====================================================== */ - - -static const luaL_Reg tab_funcs[] = { - {"concat", tconcat}, - {"insert", tinsert}, - {"pack", tpack}, - {"unpack", tunpack}, - {"remove", tremove}, - {"move", tmove}, - {"sort", sort}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_table (lua_State *L) { - luaL_newlib(L, tab_funcs); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.c deleted file mode 100644 index b657b78..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.c +++ /dev/null @@ -1,271 +0,0 @@ -/* -** $Id: ltm.c $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#define ltm_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lgc.h" -#include "lobject.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - -static const char udatatypename[] = "userdata"; - -LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = { - "no value", - "nil", "boolean", udatatypename, "number", - "string", "table", "function", udatatypename, "thread", - "upvalue", "proto" /* these last cases are used for tests only */ -}; - - -void luaT_init (lua_State *L) { - static const char *const luaT_eventname[] = { /* ORDER TM */ - "__index", "__newindex", - "__gc", "__mode", "__len", "__eq", - "__add", "__sub", "__mul", "__mod", "__pow", - "__div", "__idiv", - "__band", "__bor", "__bxor", "__shl", "__shr", - "__unm", "__bnot", "__lt", "__le", - "__concat", "__call", "__close" - }; - int i; - for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); - luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ - } -} - - -/* -** function to be used with macro "fasttm": optimized for absence of -** tag methods -*/ -const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getshortstr(events, ename); - lua_assert(event <= TM_EQ); - if (notm(tm)) { /* no tag method? */ - events->flags |= cast_byte(1u<metatable; - break; - case LUA_TUSERDATA: - mt = uvalue(o)->metatable; - break; - default: - mt = G(L)->mt[ttype(o)]; - } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); -} - - -/* -** Return the name of the type of an object. For tables and userdata -** with metatable, use their '__name' metafield, if present. -*/ -const char *luaT_objtypename (lua_State *L, const TValue *o) { - Table *mt; - if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || - (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { - const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); - if (ttisstring(name)) /* is '__name' a string? */ - return getstr(tsvalue(name)); /* use it as type name */ - } - return ttypename(ttype(o)); /* else use standard type name */ -} - - -void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, const TValue *p3) { - StkId func = L->top; - setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ - setobj2s(L, func + 1, p1); /* 1st argument */ - setobj2s(L, func + 2, p2); /* 2nd argument */ - setobj2s(L, func + 3, p3); /* 3rd argument */ - L->top = func + 4; - /* metamethod may yield only when called from Lua code */ - if (isLuacode(L->ci)) - luaD_call(L, func, 0); - else - luaD_callnoyield(L, func, 0); -} - - -void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { - ptrdiff_t result = savestack(L, res); - StkId func = L->top; - setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ - setobj2s(L, func + 1, p1); /* 1st argument */ - setobj2s(L, func + 2, p2); /* 2nd argument */ - L->top += 3; - /* metamethod may yield only when called from Lua code */ - if (isLuacode(L->ci)) - luaD_call(L, func, 1); - else - luaD_callnoyield(L, func, 1); - res = restorestack(L, result); - setobjs2s(L, res, --L->top); /* move result to its place */ -} - - -static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ - if (notm(tm)) - tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (notm(tm)) return 0; - luaT_callTMres(L, tm, p1, p2, res); - return 1; -} - - -void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { - if (l_unlikely(!callbinTM(L, p1, p2, res, event))) { - switch (event) { - case TM_BAND: case TM_BOR: case TM_BXOR: - case TM_SHL: case TM_SHR: case TM_BNOT: { - if (ttisnumber(p1) && ttisnumber(p2)) - luaG_tointerror(L, p1, p2); - else - luaG_opinterror(L, p1, p2, "perform bitwise operation on"); - } - /* calls never return, but to avoid warnings: *//* FALLTHROUGH */ - default: - luaG_opinterror(L, p1, p2, "perform arithmetic on"); - } - } -} - - -void luaT_tryconcatTM (lua_State *L) { - StkId top = L->top; - if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, - TM_CONCAT))) - luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); -} - - -void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, - int flip, StkId res, TMS event) { - if (flip) - luaT_trybinTM(L, p2, p1, res, event); - else - luaT_trybinTM(L, p1, p2, res, event); -} - - -void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, - int flip, StkId res, TMS event) { - TValue aux; - setivalue(&aux, i2); - luaT_trybinassocTM(L, p1, &aux, flip, res, event); -} - - -/* -** Calls an order tag method. -** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old -** behavior: if there is no '__le', try '__lt', based on l <= r iff -** !(r < l) (assuming a total order). If the metamethod yields during -** this substitution, the continuation has to know about it (to negate -** the result of rtop, event)) /* try original event */ - return !l_isfalse(s2v(L->top)); -#if defined(LUA_COMPAT_LT_LE) - else if (event == TM_LE) { - /* try '!(p2 < p1)' for '(p1 <= p2)' */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - if (callbinTM(L, p2, p1, L->top, TM_LT)) { - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - return l_isfalse(s2v(L->top)); - } - /* else error will remove this 'ci'; no need to clear mark */ - } -#endif - luaG_ordererror(L, p1, p2); /* no metamethod found */ - return 0; /* to avoid warnings */ -} - - -int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int flip, int isfloat, TMS event) { - TValue aux; const TValue *p2; - if (isfloat) { - setfltvalue(&aux, cast_num(v2)); - } - else - setivalue(&aux, v2); - if (flip) { /* arguments were exchanged? */ - p2 = p1; p1 = &aux; /* correct them */ - } - else - p2 = &aux; - return luaT_callorderTM(L, p1, p2, event); -} - - -void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, - const Proto *p) { - int i; - int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ - int nextra = actual - nfixparams; /* number of extra arguments */ - ci->u.l.nextraargs = nextra; - luaD_checkstack(L, p->maxstacksize + 1); - /* copy function to the top of the stack */ - setobjs2s(L, L->top++, ci->func); - /* move fixed parameters to the top of the stack */ - for (i = 1; i <= nfixparams; i++) { - setobjs2s(L, L->top++, ci->func + i); - setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ - } - ci->func += actual + 1; - ci->top += actual + 1; - lua_assert(L->top <= ci->top && ci->top <= L->stack_last); -} - - -void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { - int i; - int nextra = ci->u.l.nextraargs; - if (wanted < 0) { - wanted = nextra; /* get all extra arguments available */ - checkstackGCp(L, nextra, where); /* ensure stack space */ - L->top = where + nextra; /* next instruction will need top */ - } - for (i = 0; i < wanted && i < nextra; i++) - setobjs2s(L, where + i, ci->func - nextra + i); - for (; i < wanted; i++) /* complete required results with nil */ - setnilvalue(s2v(where + i)); -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.h deleted file mode 100644 index 73b833c..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/ltm.h +++ /dev/null @@ -1,103 +0,0 @@ -/* -** $Id: ltm.h $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#ifndef ltm_h -#define ltm_h - - -#include "lobject.h" - - -/* -* WARNING: if you change the order of this enumeration, -* grep "ORDER TM" and "ORDER OP" -*/ -typedef enum { - TM_INDEX, - TM_NEWINDEX, - TM_GC, - TM_MODE, - TM_LEN, - TM_EQ, /* last tag method with fast access */ - TM_ADD, - TM_SUB, - TM_MUL, - TM_MOD, - TM_POW, - TM_DIV, - TM_IDIV, - TM_BAND, - TM_BOR, - TM_BXOR, - TM_SHL, - TM_SHR, - TM_UNM, - TM_BNOT, - TM_LT, - TM_LE, - TM_CONCAT, - TM_CALL, - TM_CLOSE, - TM_N /* number of elements in the enum */ -} TMS; - - -/* -** Mask with 1 in all fast-access methods. A 1 in any of these bits -** in the flag of a (meta)table means the metatable does not have the -** corresponding metamethod field. (Bit 7 of the flag is used for -** 'isrealasize'.) -*/ -#define maskflags (~(~0u << (TM_EQ + 1))) - - -/* -** Test whether there is no tagmethod. -** (Because tagmethods use raw accesses, the result may be an "empty" nil.) -*/ -#define notm(tm) ttisnil(tm) - - -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) - -#define fasttm(l,et,e) gfasttm(G(l), et, e) - -#define ttypename(x) luaT_typenames_[(x) + 1] - -LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) - - -LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); - -LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); -LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, - TMS event); -LUAI_FUNC void luaT_init (lua_State *L); - -LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, const TValue *p3); -LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, - const TValue *p1, const TValue *p2, StkId p3); -LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event); -LUAI_FUNC void luaT_tryconcatTM (lua_State *L); -LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, - const TValue *p2, int inv, StkId res, TMS event); -LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, - int inv, StkId res, TMS event); -LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, - const TValue *p2, TMS event); -LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int inv, int isfloat, TMS event); - -LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, - StkId where, int wanted); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.c deleted file mode 100644 index 0f19004..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.c +++ /dev/null @@ -1,666 +0,0 @@ -/* -** $Id: lua.c $ -** Lua stand-alone interpreter -** See Copyright Notice in lua.h -*/ - -#define lua_c - -#include "lprefix.h" - - -#include -#include -#include - -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#if !defined(LUA_PROGNAME) -#define LUA_PROGNAME "lua" -#endif - -#if !defined(LUA_INIT_VAR) -#define LUA_INIT_VAR "LUA_INIT" -#endif - -#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX - - -static lua_State *globalL = NULL; - -static const char *progname = LUA_PROGNAME; - - -#if defined(LUA_USE_POSIX) /* { */ - -/* -** Use 'sigaction' when available. -*/ -static void setsignal (int sig, void (*handler)(int)) { - struct sigaction sa; - sa.sa_handler = handler; - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); /* do not mask any signal */ - sigaction(sig, &sa, NULL); -} - -#else /* }{ */ - -#define setsignal signal - -#endif /* } */ - - -/* -** Hook set by signal function to stop the interpreter. -*/ -static void lstop (lua_State *L, lua_Debug *ar) { - (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); /* reset hook */ - luaL_error(L, "interrupted!"); -} - - -/* -** Function to be called at a C signal. Because a C signal cannot -** just change a Lua state (as there is no proper synchronization), -** this function only sets a hook that, when called, will stop the -** interpreter. -*/ -static void laction (int i) { - int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; - setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ - lua_sethook(globalL, lstop, flag, 1); -} - - -static void print_usage (const char *badoption) { - lua_writestringerror("%s: ", progname); - if (badoption[1] == 'e' || badoption[1] == 'l') - lua_writestringerror("'%s' needs argument\n", badoption); - else - lua_writestringerror("unrecognized option '%s'\n", badoption); - lua_writestringerror( - "usage: %s [options] [script [args]]\n" - "Available options are:\n" - " -e stat execute string 'stat'\n" - " -i enter interactive mode after executing 'script'\n" - " -l mod require library 'mod' into global 'mod'\n" - " -l g=mod require library 'mod' into global 'g'\n" - " -v show version information\n" - " -E ignore environment variables\n" - " -W turn warnings on\n" - " -- stop handling options\n" - " - stop handling options and execute stdin\n" - , - progname); -} - - -/* -** Prints an error message, adding the program name in front of it -** (if present) -*/ -static void l_message (const char *pname, const char *msg) { - if (pname) lua_writestringerror("%s: ", pname); - lua_writestringerror("%s\n", msg); -} - - -/* -** Check whether 'status' is not OK and, if so, prints the error -** message on the top of the stack. It assumes that the error object -** is a string, as it was either generated by Lua or by 'msghandler'. -*/ -static int report (lua_State *L, int status) { - if (status != LUA_OK) { - const char *msg = lua_tostring(L, -1); - l_message(progname, msg); - lua_pop(L, 1); /* remove message */ - } - return status; -} - - -/* -** Message handler used to run all chunks -*/ -static int msghandler (lua_State *L) { - const char *msg = lua_tostring(L, 1); - if (msg == NULL) { /* is error object not a string? */ - if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */ - lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */ - return 1; /* that is the message */ - else - msg = lua_pushfstring(L, "(error object is a %s value)", - luaL_typename(L, 1)); - } - luaL_traceback(L, L, msg, 1); /* append a standard traceback */ - return 1; /* return the traceback */ -} - - -/* -** Interface to 'lua_pcall', which sets appropriate message function -** and C-signal handler. Used to run all chunks. -*/ -static int docall (lua_State *L, int narg, int nres) { - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, msghandler); /* push message handler */ - lua_insert(L, base); /* put it under function and args */ - globalL = L; /* to be available to 'laction' */ - setsignal(SIGINT, laction); /* set C-signal handler */ - status = lua_pcall(L, narg, nres, base); - setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ - lua_remove(L, base); /* remove message handler from the stack */ - return status; -} - - -static void print_version (void) { - lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); - lua_writeline(); -} - - -/* -** Create the 'arg' table, which stores all arguments from the -** command line ('argv'). It should be aligned so that, at index 0, -** it has 'argv[script]', which is the script name. The arguments -** to the script (everything after 'script') go to positive indices; -** other arguments (before the script name) go to negative indices. -** If there is no script name, assume interpreter's name as base. -*/ -static void createargtable (lua_State *L, char **argv, int argc, int script) { - int i, narg; - if (script == argc) script = 0; /* no script name? */ - narg = argc - (script + 1); /* number of positive indices */ - lua_createtable(L, narg, script + 1); - for (i = 0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - script); - } - lua_setglobal(L, "arg"); -} - - -static int dochunk (lua_State *L, int status) { - if (status == LUA_OK) status = docall(L, 0, 0); - return report(L, status); -} - - -static int dofile (lua_State *L, const char *name) { - return dochunk(L, luaL_loadfile(L, name)); -} - - -static int dostring (lua_State *L, const char *s, const char *name) { - return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); -} - - -/* -** Receives 'globname[=modname]' and runs 'globname = require(modname)'. -*/ -static int dolibrary (lua_State *L, char *globname) { - int status; - char *modname = strchr(globname, '='); - if (modname == NULL) /* no explicit name? */ - modname = globname; /* module name is equal to global name */ - else { - *modname = '\0'; /* global name ends here */ - modname++; /* module name starts after the '=' */ - } - lua_getglobal(L, "require"); - lua_pushstring(L, modname); - status = docall(L, 1, 1); /* call 'require(modname)' */ - if (status == LUA_OK) - lua_setglobal(L, globname); /* globname = require(modname) */ - return report(L, status); -} - - -/* -** Push on the stack the contents of table 'arg' from 1 to #arg -*/ -static int pushargs (lua_State *L) { - int i, n; - if (lua_getglobal(L, "arg") != LUA_TTABLE) - luaL_error(L, "'arg' is not a table"); - n = (int)luaL_len(L, -1); - luaL_checkstack(L, n + 3, "too many arguments to script"); - for (i = 1; i <= n; i++) - lua_rawgeti(L, -i, i); - lua_remove(L, -i); /* remove table from the stack */ - return n; -} - - -static int handle_script (lua_State *L, char **argv) { - int status; - const char *fname = argv[0]; - if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - if (status == LUA_OK) { - int n = pushargs(L); /* push arguments to script */ - status = docall(L, n, LUA_MULTRET); - } - return report(L, status); -} - - -/* bits of various argument indicators in 'args' */ -#define has_error 1 /* bad option */ -#define has_i 2 /* -i */ -#define has_v 4 /* -v */ -#define has_e 8 /* -e */ -#define has_E 16 /* -E */ - - -/* -** Traverses all arguments from 'argv', returning a mask with those -** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument -** (either the script name or a bad argument in case of error). -*/ -static int collectargs (char **argv, int *first) { - int args = 0; - int i; - for (i = 1; argv[i] != NULL; i++) { - *first = i; - if (argv[i][0] != '-') /* not an option? */ - return args; /* stop handling options */ - switch (argv[i][1]) { /* else check option */ - case '-': /* '--' */ - if (argv[i][2] != '\0') /* extra characters after '--'? */ - return has_error; /* invalid option */ - *first = i + 1; - return args; - case '\0': /* '-' */ - return args; /* script "name" is '-' */ - case 'E': - if (argv[i][2] != '\0') /* extra characters? */ - return has_error; /* invalid option */ - args |= has_E; - break; - case 'W': - if (argv[i][2] != '\0') /* extra characters? */ - return has_error; /* invalid option */ - break; - case 'i': - args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ - case 'v': - if (argv[i][2] != '\0') /* extra characters? */ - return has_error; /* invalid option */ - args |= has_v; - break; - case 'e': - args |= has_e; /* FALLTHROUGH */ - case 'l': /* both options need an argument */ - if (argv[i][2] == '\0') { /* no concatenated argument? */ - i++; /* try next 'argv' */ - if (argv[i] == NULL || argv[i][0] == '-') - return has_error; /* no next argument or it is another option */ - } - break; - default: /* invalid option */ - return has_error; - } - } - *first = i; /* no script name */ - return args; -} - - -/* -** Processes options 'e' and 'l', which involve running Lua code, and -** 'W', which also affects the state. -** Returns 0 if some code raises an error. -*/ -static int runargs (lua_State *L, char **argv, int n) { - int i; - for (i = 1; i < n; i++) { - int option = argv[i][1]; - lua_assert(argv[i][0] == '-'); /* already checked */ - switch (option) { - case 'e': case 'l': { - int status; - char *extra = argv[i] + 2; /* both options need an argument */ - if (*extra == '\0') extra = argv[++i]; - lua_assert(extra != NULL); - status = (option == 'e') - ? dostring(L, extra, "=(command line)") - : dolibrary(L, extra); - if (status != LUA_OK) return 0; - break; - } - case 'W': - lua_warning(L, "@on", 0); /* warnings on */ - break; - } - } - return 1; -} - - -static int handle_luainit (lua_State *L) { - const char *name = "=" LUA_INITVARVERSION; - const char *init = getenv(name + 1); - if (init == NULL) { - name = "=" LUA_INIT_VAR; - init = getenv(name + 1); /* try alternative name */ - } - if (init == NULL) return LUA_OK; - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, name); -} - - -/* -** {================================================================== -** Read-Eval-Print Loop (REPL) -** =================================================================== -*/ - -#if !defined(LUA_PROMPT) -#define LUA_PROMPT "> " -#define LUA_PROMPT2 ">> " -#endif - -#if !defined(LUA_MAXINPUT) -#define LUA_MAXINPUT 512 -#endif - - -/* -** lua_stdin_is_tty detects whether the standard input is a 'tty' (that -** is, whether we're running lua interactively). -*/ -#if !defined(lua_stdin_is_tty) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include -#define lua_stdin_is_tty() isatty(0) - -#elif defined(LUA_USE_WINDOWS) /* }{ */ - -#include -#include - -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) - -#else /* }{ */ - -/* ISO C definition */ -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ - -#endif /* } */ - -#endif /* } */ - - -/* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. -*/ -#if !defined(lua_readline) /* { */ - -#if defined(LUA_USE_READLINE) /* { */ - -#include -#include -#define lua_initreadline(L) ((void)L, rl_readline_name="lua") -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,line) ((void)L, add_history(line)) -#define lua_freeline(L,b) ((void)L, free(b)) - -#else /* }{ */ - -#define lua_initreadline(L) ((void)L) -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,line) { (void)L; (void)line; } -#define lua_freeline(L,b) { (void)L; (void)b; } - -#endif /* } */ - -#endif /* } */ - - -/* -** Return the string to be used as a prompt by the interpreter. Leave -** the string (or nil, if using the default value) on the stack, to keep -** it anchored. -*/ -static const char *get_prompt (lua_State *L, int firstline) { - if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) - return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ - else { /* apply 'tostring' over the value */ - const char *p = luaL_tolstring(L, -1, NULL); - lua_remove(L, -2); /* remove original value */ - return p; - } -} - -/* mark in error messages for incomplete statements */ -#define EOFMARK "" -#define marklen (sizeof(EOFMARK)/sizeof(char) - 1) - - -/* -** Check whether 'status' signals a syntax error and the error -** message at the top of the stack ends with the above mark for -** incomplete statements. -*/ -static int incomplete (lua_State *L, int status) { - if (status == LUA_ERRSYNTAX) { - size_t lmsg; - const char *msg = lua_tolstring(L, -1, &lmsg); - if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { - lua_pop(L, 1); - return 1; - } - } - return 0; /* else... */ -} - - -/* -** Prompt the user, read a line, and push it into the Lua stack. -*/ -static int pushline (lua_State *L, int firstline) { - char buffer[LUA_MAXINPUT]; - char *b = buffer; - size_t l; - const char *prmt = get_prompt(L, firstline); - int readstatus = lua_readline(L, b, prmt); - if (readstatus == 0) - return 0; /* no input (prompt will be popped by caller) */ - lua_pop(L, 1); /* remove prompt */ - l = strlen(b); - if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[--l] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ - lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ - else - lua_pushlstring(L, b, l); - lua_freeline(L, b); - return 1; -} - - -/* -** Try to compile line on the stack as 'return ;'; on return, stack -** has either compiled chunk or original line (if compilation failed). -*/ -static int addreturn (lua_State *L) { - const char *line = lua_tostring(L, -1); /* original line */ - const char *retline = lua_pushfstring(L, "return %s;", line); - int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); - if (status == LUA_OK) { - lua_remove(L, -2); /* remove modified line */ - if (line[0] != '\0') /* non empty? */ - lua_saveline(L, line); /* keep history */ - } - else - lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ - return status; -} - - -/* -** Read multiple lines until a complete Lua statement -*/ -static int multiline (lua_State *L) { - for (;;) { /* repeat until gets a complete statement */ - size_t len; - const char *line = lua_tolstring(L, 1, &len); /* get what it has */ - int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ - if (!incomplete(L, status) || !pushline(L, 0)) { - lua_saveline(L, line); /* keep history */ - return status; /* cannot or should not try to add continuation line */ - } - lua_pushliteral(L, "\n"); /* add newline... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } -} - - -/* -** Read a line and try to load (compile) it first as an expression (by -** adding "return " in front of it) and second as a statement. Return -** the final status of load/call with the resulting function (if any) -** in the top of the stack. -*/ -static int loadline (lua_State *L) { - int status; - lua_settop(L, 0); - if (!pushline(L, 1)) - return -1; /* no input */ - if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ - status = multiline(L); /* try as command, maybe with continuation lines */ - lua_remove(L, 1); /* remove line from the stack */ - lua_assert(lua_gettop(L) == 1); - return status; -} - - -/* -** Prints (calling the Lua 'print' function) any values on the stack -*/ -static void l_print (lua_State *L) { - int n = lua_gettop(L); - if (n > 0) { /* any result to be printed? */ - luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, n, 0, 0) != LUA_OK) - l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)", - lua_tostring(L, -1))); - } -} - - -/* -** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and -** print any results. -*/ -static void doREPL (lua_State *L) { - int status; - const char *oldprogname = progname; - progname = NULL; /* no 'progname' on errors in interactive mode */ - lua_initreadline(L); - while ((status = loadline(L)) != -1) { - if (status == LUA_OK) - status = docall(L, 0, LUA_MULTRET); - if (status == LUA_OK) l_print(L); - else report(L, status); - } - lua_settop(L, 0); /* clear stack */ - lua_writeline(); - progname = oldprogname; -} - -/* }================================================================== */ - - -/* -** Main body of stand-alone interpreter (to be called in protected mode). -** Reads the options and handles them all. -*/ -static int pmain (lua_State *L) { - int argc = (int)lua_tointeger(L, 1); - char **argv = (char **)lua_touserdata(L, 2); - int script; - int args = collectargs(argv, &script); - luaL_checkversion(L); /* check that interpreter has correct version */ - if (argv[0] && argv[0][0]) progname = argv[0]; - if (args == has_error) { /* bad arg? */ - print_usage(argv[script]); /* 'script' has index of bad arg. */ - return 0; - } - if (args & has_v) /* option '-v'? */ - print_version(); - if (args & has_E) { /* option '-E'? */ - lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } - luaL_openlibs(L); /* open standard libraries */ - createargtable(L, argv, argc, script); /* create table 'arg' */ - lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ - if (!(args & has_E)) { /* no option '-E'? */ - if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ - return 0; /* error running LUA_INIT */ - } - if (!runargs(L, argv, script)) /* execute arguments -e and -l */ - return 0; /* something failed */ - if (script < argc && /* execute main script (if there is one) */ - handle_script(L, argv + script) != LUA_OK) - return 0; - if (args & has_i) /* -i option? */ - doREPL(L); /* do read-eval-print loop */ - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ - if (lua_stdin_is_tty()) { /* running in interactive mode? */ - print_version(); - doREPL(L); /* do read-eval-print loop */ - } - else dofile(L, NULL); /* executes stdin as a file */ - } - lua_pushboolean(L, 1); /* signal no errors */ - return 1; -} - - -int main (int argc, char **argv) { - int status, result; - lua_State *L = luaL_newstate(); /* create state */ - if (L == NULL) { - l_message(argv[0], "cannot create state: not enough memory"); - return EXIT_FAILURE; - } - lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ - lua_pushinteger(L, argc); /* 1st argument */ - lua_pushlightuserdata(L, argv); /* 2nd argument */ - status = lua_pcall(L, 2, 1, 0); /* do the call */ - result = lua_toboolean(L, -1); /* get result */ - report(L, status); - lua_close(L); - return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.h deleted file mode 100644 index e661839..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.h +++ /dev/null @@ -1,518 +0,0 @@ -/* -** $Id: lua.h $ -** Lua - A Scripting Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) -** See Copyright Notice at the end of this file -*/ - - -#ifndef lua_h -#define lua_h - -#include -#include - - -#include "luaconf.h" - - -#define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "4" - -#define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) - -#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" - - -/* mark for precompiled code ('Lua') */ -#define LUA_SIGNATURE "\x1bLua" - -/* option for multiple returns in 'lua_pcall' and 'lua_call' */ -#define LUA_MULTRET (-1) - - -/* -** Pseudo-indices -** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty -** space after that to help overflow detection) -*/ -#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) -#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) - - -/* thread status */ -#define LUA_OK 0 -#define LUA_YIELD 1 -#define LUA_ERRRUN 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRERR 5 - - -typedef struct lua_State lua_State; - - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - -#define LUA_NUMTYPES 9 - - - -/* minimum Lua stack available to a C function */ -#define LUA_MINSTACK 20 - - -/* predefined values in the registry */ -#define LUA_RIDX_MAINTHREAD 1 -#define LUA_RIDX_GLOBALS 2 -#define LUA_RIDX_LAST LUA_RIDX_GLOBALS - - -/* type of numbers in Lua */ -typedef LUA_NUMBER lua_Number; - - -/* type for integer functions */ -typedef LUA_INTEGER lua_Integer; - -/* unsigned integer type */ -typedef LUA_UNSIGNED lua_Unsigned; - -/* type for continuation-function contexts */ -typedef LUA_KCONTEXT lua_KContext; - - -/* -** Type for C functions registered with Lua -*/ -typedef int (*lua_CFunction) (lua_State *L); - -/* -** Type for continuation functions -*/ -typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx); - - -/* -** Type for functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); - - -/* -** Type for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - - -/* -** Type for warning functions -*/ -typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); - - - - -/* -** generic extra include file -*/ -#if defined(LUA_USER_H) -#include LUA_USER_H -#endif - - -/* -** RCS ident string -*/ -extern const char lua_ident[]; - - -/* -** state manipulation -*/ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); -LUA_API void (lua_close) (lua_State *L); -LUA_API lua_State *(lua_newthread) (lua_State *L); -LUA_API int (lua_resetthread) (lua_State *L); - -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); - - -LUA_API lua_Number (lua_version) (lua_State *L); - - -/* -** basic stack manipulation -*/ -LUA_API int (lua_absindex) (lua_State *L, int idx); -LUA_API int (lua_gettop) (lua_State *L); -LUA_API void (lua_settop) (lua_State *L, int idx); -LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_rotate) (lua_State *L, int idx, int n); -LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); -LUA_API int (lua_checkstack) (lua_State *L, int n); - -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); - - -/* -** access functions (stack -> C) -*/ - -LUA_API int (lua_isnumber) (lua_State *L, int idx); -LUA_API int (lua_isstring) (lua_State *L, int idx); -LUA_API int (lua_iscfunction) (lua_State *L, int idx); -LUA_API int (lua_isinteger) (lua_State *L, int idx); -LUA_API int (lua_isuserdata) (lua_State *L, int idx); -LUA_API int (lua_type) (lua_State *L, int idx); -LUA_API const char *(lua_typename) (lua_State *L, int tp); - -LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); -LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); -LUA_API int (lua_toboolean) (lua_State *L, int idx); -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); -LUA_API void *(lua_touserdata) (lua_State *L, int idx); -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); -LUA_API const void *(lua_topointer) (lua_State *L, int idx); - - -/* -** Comparison and arithmetic functions -*/ - -#define LUA_OPADD 0 /* ORDER TM, ORDER OP */ -#define LUA_OPSUB 1 -#define LUA_OPMUL 2 -#define LUA_OPMOD 3 -#define LUA_OPPOW 4 -#define LUA_OPDIV 5 -#define LUA_OPIDIV 6 -#define LUA_OPBAND 7 -#define LUA_OPBOR 8 -#define LUA_OPBXOR 9 -#define LUA_OPSHL 10 -#define LUA_OPSHR 11 -#define LUA_OPUNM 12 -#define LUA_OPBNOT 13 - -LUA_API void (lua_arith) (lua_State *L, int op); - -#define LUA_OPEQ 0 -#define LUA_OPLT 1 -#define LUA_OPLE 2 - -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op); - - -/* -** push functions (C -> stack) -*/ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); -LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, - va_list argp); -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); -LUA_API void (lua_pushboolean) (lua_State *L, int b); -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); -LUA_API int (lua_pushthread) (lua_State *L); - - -/* -** get functions (Lua -> stack) -*/ -LUA_API int (lua_getglobal) (lua_State *L, const char *name); -LUA_API int (lua_gettable) (lua_State *L, int idx); -LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n); -LUA_API int (lua_rawget) (lua_State *L, int idx); -LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); -LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); - -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); - - -/* -** set functions (stack -> Lua) -*/ -LUA_API void (lua_setglobal) (lua_State *L, const char *name); -LUA_API void (lua_settable) (lua_State *L, int idx); -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n); -LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); -LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); - - -/* -** 'load' and 'call' functions (load and run Lua code) -*/ -LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, - lua_KContext ctx, lua_KFunction k); -#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) - -LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, - lua_KContext ctx, lua_KFunction k); -#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) - -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, const char *mode); - -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); - - -/* -** coroutine functions -*/ -LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, - lua_KFunction k); -LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg, - int *nres); -LUA_API int (lua_status) (lua_State *L); -LUA_API int (lua_isyieldable) (lua_State *L); - -#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) - - -/* -** Warning-related functions -*/ -LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); -LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); - - -/* -** garbage-collection function and options -*/ - -#define LUA_GCSTOP 0 -#define LUA_GCRESTART 1 -#define LUA_GCCOLLECT 2 -#define LUA_GCCOUNT 3 -#define LUA_GCCOUNTB 4 -#define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 -#define LUA_GCISRUNNING 9 -#define LUA_GCGEN 10 -#define LUA_GCINC 11 - -LUA_API int (lua_gc) (lua_State *L, int what, ...); - - -/* -** miscellaneous functions -*/ - -LUA_API int (lua_error) (lua_State *L); - -LUA_API int (lua_next) (lua_State *L, int idx); - -LUA_API void (lua_concat) (lua_State *L, int n); -LUA_API void (lua_len) (lua_State *L, int idx); - -LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); - -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); -LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); - -LUA_API void (lua_toclose) (lua_State *L, int idx); -LUA_API void (lua_closeslot) (lua_State *L, int idx); - - -/* -** {============================================================== -** some useful macros -** =============================================================== -*/ - -#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) - -#define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) -#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_newtable(L) lua_createtable(L, 0, 0) - -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) - -#define lua_pushliteral(L, s) lua_pushstring(L, "" s) - -#define lua_pushglobaltable(L) \ - ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) - -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) - - -#define lua_insert(L,idx) lua_rotate(L, (idx), 1) - -#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) - -#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1)) - -/* }============================================================== */ - - -/* -** {============================================================== -** compatibility macros -** =============================================================== -*/ -#if defined(LUA_COMPAT_APIINTCASTS) - -#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) -#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) -#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) - -#endif - -#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) -#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) -#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) - -#define LUA_NUMTAGS LUA_NUMTYPES - -/* }============================================================== */ - -/* -** {====================================================================== -** Debug API -** ======================================================================= -*/ - - -/* -** Event codes -*/ -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILCALL 4 - - -/* -** Event masks -*/ -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debugger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); -LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); -LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n); -LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); - -LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n); -LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1, - int fidx2, int n2); - -LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); -LUA_API lua_Hook (lua_gethook) (lua_State *L); -LUA_API int (lua_gethookmask) (lua_State *L); -LUA_API int (lua_gethookcount) (lua_State *L); - -LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); - -struct lua_Debug { - int event; - const char *name; /* (n) */ - const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ - const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ - const char *source; /* (S) */ - size_t srclen; /* (S) */ - int currentline; /* (l) */ - int linedefined; /* (S) */ - int lastlinedefined; /* (S) */ - unsigned char nups; /* (u) number of upvalues */ - unsigned char nparams;/* (u) number of parameters */ - char isvararg; /* (u) */ - char istailcall; /* (t) */ - unsigned short ftransfer; /* (r) index of first value transferred */ - unsigned short ntransfer; /* (r) number of transferred values */ - char short_src[LUA_IDSIZE]; /* (S) */ - /* private part */ - struct CallInfo *i_ci; /* active function */ -}; - -/* }====================================================================== */ - - -/****************************************************************************** -* Copyright (C) 1994-2022 Lua.org, PUC-Rio. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.hpp b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.hpp deleted file mode 100644 index ec417f5..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lua.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// lua.hpp -// Lua header files for C++ -// <> not supplied automatically because Lua also compiles as C++ - -extern "C" { -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/luac.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/luac.c deleted file mode 100644 index f6db9cf..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/luac.c +++ /dev/null @@ -1,725 +0,0 @@ -/* -** $Id: luac.c $ -** Lua compiler (saves bytecodes to files; also lists bytecodes) -** See Copyright Notice in lua.h -*/ - -#define luac_c -#define LUA_CORE - -#include "lprefix.h" - -#include -#include -#include -#include -#include - -#include "lua.h" -#include "lauxlib.h" - -#include "ldebug.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lopnames.h" -#include "lstate.h" -#include "lundump.h" - -static void PrintFunction(const Proto* f, int full); -#define luaU_print PrintFunction - -#define PROGNAME "luac" /* default program name */ -#define OUTPUT PROGNAME ".out" /* default output file */ - -static int listing=0; /* list bytecodes? */ -static int dumping=1; /* dump bytecodes? */ -static int stripping=0; /* strip debug information? */ -static char Output[]={ OUTPUT }; /* default output file name */ -static const char* output=Output; /* actual output file name */ -static const char* progname=PROGNAME; /* actual program name */ -static TString **tmname; - -static void fatal(const char* message) -{ - fprintf(stderr,"%s: %s\n",progname,message); - exit(EXIT_FAILURE); -} - -static void cannot(const char* what) -{ - fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); - exit(EXIT_FAILURE); -} - -static void usage(const char* message) -{ - if (*message=='-') - fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message); - else - fprintf(stderr,"%s: %s\n",progname,message); - fprintf(stderr, - "usage: %s [options] [filenames]\n" - "Available options are:\n" - " -l list (use -l -l for full listing)\n" - " -o name output to file 'name' (default is \"%s\")\n" - " -p parse only\n" - " -s strip debug information\n" - " -v show version information\n" - " -- stop handling options\n" - " - stop handling options and process stdin\n" - ,progname,Output); - exit(EXIT_FAILURE); -} - -#define IS(s) (strcmp(argv[i],s)==0) - -static int doargs(int argc, char* argv[]) -{ - int i; - int version=0; - if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; - for (i=1; itop+(i))) - -static const Proto* combine(lua_State* L, int n) -{ - if (n==1) - return toproto(L,-1); - else - { - Proto* f; - int i=n; - if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); - f=toproto(L,-1); - for (i=0; ip[i]=toproto(L,i-n-1); - if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; - } - luaM_freearray(L,f->lineinfo,f->sizelineinfo); - f->sizelineinfo=0; - return f; - } -} - -static int writer(lua_State* L, const void* p, size_t size, void* u) -{ - UNUSED(L); - return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); -} - -static int pmain(lua_State* L) -{ - int argc=(int)lua_tointeger(L,1); - char** argv=(char**)lua_touserdata(L,2); - const Proto* f; - int i; - tmname=G(L)->tmname; - if (!lua_checkstack(L,argc)) fatal("too many input files"); - for (i=0; i1); - if (dumping) - { - FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); - if (D==NULL) cannot("open"); - lua_lock(L); - luaU_dump(L,f,writer,D,stripping); - lua_unlock(L); - if (ferror(D)) cannot("write"); - if (fclose(D)) cannot("close"); - } - return 0; -} - -int main(int argc, char* argv[]) -{ - lua_State* L; - int i=doargs(argc,argv); - argc-=i; argv+=i; - if (argc<=0) usage("no input files given"); - L=luaL_newstate(); - if (L==NULL) fatal("cannot create state: not enough memory"); - lua_pushcfunction(L,&pmain); - lua_pushinteger(L,argc); - lua_pushlightuserdata(L,argv); - if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); - lua_close(L); - return EXIT_SUCCESS; -} - -/* -** print bytecodes -*/ - -#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") -#define VOID(p) ((const void*)(p)) -#define eventname(i) (getstr(tmname[i])) - -static void PrintString(const TString* ts) -{ - const char* s=getstr(ts); - size_t i,n=tsslen(ts); - printf("\""); - for (i=0; ik[i]; - switch (ttypetag(o)) - { - case LUA_VNIL: - printf("N"); - break; - case LUA_VFALSE: - case LUA_VTRUE: - printf("B"); - break; - case LUA_VNUMFLT: - printf("F"); - break; - case LUA_VNUMINT: - printf("I"); - break; - case LUA_VSHRSTR: - case LUA_VLNGSTR: - printf("S"); - break; - default: /* cannot happen */ - printf("?%d",ttypetag(o)); - break; - } - printf("\t"); -} - -static void PrintConstant(const Proto* f, int i) -{ - const TValue* o=&f->k[i]; - switch (ttypetag(o)) - { - case LUA_VNIL: - printf("nil"); - break; - case LUA_VFALSE: - printf("false"); - break; - case LUA_VTRUE: - printf("true"); - break; - case LUA_VNUMFLT: - { - char buff[100]; - sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); - printf("%s",buff); - if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); - break; - } - case LUA_VNUMINT: - printf(LUA_INTEGER_FMT,ivalue(o)); - break; - case LUA_VSHRSTR: - case LUA_VLNGSTR: - PrintString(tsvalue(o)); - break; - default: /* cannot happen */ - printf("?%d",ttypetag(o)); - break; - } -} - -#define COMMENT "\t; " -#define EXTRAARG GETARG_Ax(code[pc+1]) -#define EXTRAARGC (EXTRAARG*(MAXARG_C+1)) -#define ISK (isk ? "k" : "") - -static void PrintCode(const Proto* f) -{ - const Instruction* code=f->code; - int pc,n=f->sizecode; - for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); - printf("%-9s\t",opnames[o]); - switch (o) - { - case OP_MOVE: - printf("%d %d",a,b); - break; - case OP_LOADI: - printf("%d %d",a,sbx); - break; - case OP_LOADF: - printf("%d %d",a,sbx); - break; - case OP_LOADK: - printf("%d %d",a,bx); - printf(COMMENT); PrintConstant(f,bx); - break; - case OP_LOADKX: - printf("%d",a); - printf(COMMENT); PrintConstant(f,EXTRAARG); - break; - case OP_LOADFALSE: - printf("%d",a); - break; - case OP_LFALSESKIP: - printf("%d",a); - break; - case OP_LOADTRUE: - printf("%d",a); - break; - case OP_LOADNIL: - printf("%d %d",a,b); - printf(COMMENT "%d out",b+1); - break; - case OP_GETUPVAL: - printf("%d %d",a,b); - printf(COMMENT "%s",UPVALNAME(b)); - break; - case OP_SETUPVAL: - printf("%d %d",a,b); - printf(COMMENT "%s",UPVALNAME(b)); - break; - case OP_GETTABUP: - printf("%d %d %d",a,b,c); - printf(COMMENT "%s",UPVALNAME(b)); - printf(" "); PrintConstant(f,c); - break; - case OP_GETTABLE: - printf("%d %d %d",a,b,c); - break; - case OP_GETI: - printf("%d %d %d",a,b,c); - break; - case OP_GETFIELD: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_SETTABUP: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT "%s",UPVALNAME(a)); - printf(" "); PrintConstant(f,b); - if (isk) { printf(" "); PrintConstant(f,c); } - break; - case OP_SETTABLE: - printf("%d %d %d%s",a,b,c,ISK); - if (isk) { printf(COMMENT); PrintConstant(f,c); } - break; - case OP_SETI: - printf("%d %d %d%s",a,b,c,ISK); - if (isk) { printf(COMMENT); PrintConstant(f,c); } - break; - case OP_SETFIELD: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT); PrintConstant(f,b); - if (isk) { printf(" "); PrintConstant(f,c); } - break; - case OP_NEWTABLE: - printf("%d %d %d",a,b,c); - printf(COMMENT "%d",c+EXTRAARGC); - break; - case OP_SELF: - printf("%d %d %d%s",a,b,c,ISK); - if (isk) { printf(COMMENT); PrintConstant(f,c); } - break; - case OP_ADDI: - printf("%d %d %d",a,b,sc); - break; - case OP_ADDK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_SUBK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_MULK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_MODK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_POWK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_DIVK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_IDIVK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_BANDK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_BORK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_BXORK: - printf("%d %d %d",a,b,c); - printf(COMMENT); PrintConstant(f,c); - break; - case OP_SHRI: - printf("%d %d %d",a,b,sc); - break; - case OP_SHLI: - printf("%d %d %d",a,b,sc); - break; - case OP_ADD: - printf("%d %d %d",a,b,c); - break; - case OP_SUB: - printf("%d %d %d",a,b,c); - break; - case OP_MUL: - printf("%d %d %d",a,b,c); - break; - case OP_MOD: - printf("%d %d %d",a,b,c); - break; - case OP_POW: - printf("%d %d %d",a,b,c); - break; - case OP_DIV: - printf("%d %d %d",a,b,c); - break; - case OP_IDIV: - printf("%d %d %d",a,b,c); - break; - case OP_BAND: - printf("%d %d %d",a,b,c); - break; - case OP_BOR: - printf("%d %d %d",a,b,c); - break; - case OP_BXOR: - printf("%d %d %d",a,b,c); - break; - case OP_SHL: - printf("%d %d %d",a,b,c); - break; - case OP_SHR: - printf("%d %d %d",a,b,c); - break; - case OP_MMBIN: - printf("%d %d %d",a,b,c); - printf(COMMENT "%s",eventname(c)); - break; - case OP_MMBINI: - printf("%d %d %d %d",a,sb,c,isk); - printf(COMMENT "%s",eventname(c)); - if (isk) printf(" flip"); - break; - case OP_MMBINK: - printf("%d %d %d %d",a,b,c,isk); - printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b); - if (isk) printf(" flip"); - break; - case OP_UNM: - printf("%d %d",a,b); - break; - case OP_BNOT: - printf("%d %d",a,b); - break; - case OP_NOT: - printf("%d %d",a,b); - break; - case OP_LEN: - printf("%d %d",a,b); - break; - case OP_CONCAT: - printf("%d %d",a,b); - break; - case OP_CLOSE: - printf("%d",a); - break; - case OP_TBC: - printf("%d",a); - break; - case OP_JMP: - printf("%d",GETARG_sJ(i)); - printf(COMMENT "to %d",GETARG_sJ(i)+pc+2); - break; - case OP_EQ: - printf("%d %d %d",a,b,isk); - break; - case OP_LT: - printf("%d %d %d",a,b,isk); - break; - case OP_LE: - printf("%d %d %d",a,b,isk); - break; - case OP_EQK: - printf("%d %d %d",a,b,isk); - printf(COMMENT); PrintConstant(f,b); - break; - case OP_EQI: - printf("%d %d %d",a,sb,isk); - break; - case OP_LTI: - printf("%d %d %d",a,sb,isk); - break; - case OP_LEI: - printf("%d %d %d",a,sb,isk); - break; - case OP_GTI: - printf("%d %d %d",a,sb,isk); - break; - case OP_GEI: - printf("%d %d %d",a,sb,isk); - break; - case OP_TEST: - printf("%d %d",a,isk); - break; - case OP_TESTSET: - printf("%d %d %d",a,b,isk); - break; - case OP_CALL: - printf("%d %d %d",a,b,c); - printf(COMMENT); - if (b==0) printf("all in "); else printf("%d in ",b-1); - if (c==0) printf("all out"); else printf("%d out",c-1); - break; - case OP_TAILCALL: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT "%d in",b-1); - break; - case OP_RETURN: - printf("%d %d %d%s",a,b,c,ISK); - printf(COMMENT); - if (b==0) printf("all out"); else printf("%d out",b-1); - break; - case OP_RETURN0: - break; - case OP_RETURN1: - printf("%d",a); - break; - case OP_FORLOOP: - printf("%d %d",a,bx); - printf(COMMENT "to %d",pc-bx+2); - break; - case OP_FORPREP: - printf("%d %d",a,bx); - printf(COMMENT "exit to %d",pc+bx+3); - break; - case OP_TFORPREP: - printf("%d %d",a,bx); - printf(COMMENT "to %d",pc+bx+2); - break; - case OP_TFORCALL: - printf("%d %d",a,c); - break; - case OP_TFORLOOP: - printf("%d %d",a,bx); - printf(COMMENT "to %d",pc-bx+2); - break; - case OP_SETLIST: - printf("%d %d %d",a,b,c); - if (isk) printf(COMMENT "%d",c+EXTRAARGC); - break; - case OP_CLOSURE: - printf("%d %d",a,bx); - printf(COMMENT "%p",VOID(f->p[bx])); - break; - case OP_VARARG: - printf("%d %d",a,c); - printf(COMMENT); - if (c==0) printf("all out"); else printf("%d out",c-1); - break; - case OP_VARARGPREP: - printf("%d",a); - break; - case OP_EXTRAARG: - printf("%d",ax); - break; -#if 0 - default: - printf("%d %d %d",a,b,c); - printf(COMMENT "not handled"); - break; -#endif - } - printf("\n"); - } -} - - -#define SS(x) ((x==1)?"":"s") -#define S(x) (int)(x),SS(x) - -static void PrintHeader(const Proto* f) -{ - const char* s=f->source ? getstr(f->source) : "=?"; - if (*s=='@' || *s=='=') - s++; - else if (*s==LUA_SIGNATURE[0]) - s="(bstring)"; - else - s="(string)"; - printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", - (f->linedefined==0)?"main":"function",s, - f->linedefined,f->lastlinedefined, - S(f->sizecode),VOID(f)); - printf("%d%s param%s, %d slot%s, %d upvalue%s, ", - (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), - S(f->maxstacksize),S(f->sizeupvalues)); - printf("%d local%s, %d constant%s, %d function%s\n", - S(f->sizelocvars),S(f->sizek),S(f->sizep)); -} - -static void PrintDebug(const Proto* f) -{ - int i,n; - n=f->sizek; - printf("constants (%d) for %p:\n",n,VOID(f)); - for (i=0; isizelocvars; - printf("locals (%d) for %p:\n",n,VOID(f)); - for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); - } - n=f->sizeupvalues; - printf("upvalues (%d) for %p:\n",n,VOID(f)); - for (i=0; iupvalues[i].instack,f->upvalues[i].idx); - } -} - -static void PrintFunction(const Proto* f, int full) -{ - int i,n=f->sizep; - PrintHeader(f); - PrintCode(f); - if (full) PrintDebug(f); - for (i=0; ip[i],full); -} diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/luaconf.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/luaconf.h deleted file mode 100644 index d42d14b..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/luaconf.h +++ /dev/null @@ -1,786 +0,0 @@ -/* -** $Id: luaconf.h $ -** Configuration file for Lua -** See Copyright Notice in lua.h -*/ - - -#ifndef luaconf_h -#define luaconf_h - -#include -#include - - -/* -** =================================================================== -** General Configuration File for Lua -** -** Some definitions here can be changed externally, through the compiler -** (e.g., with '-D' options): They are commented out or protected -** by '#if !defined' guards. However, several other definitions -** should be changed directly here, either because they affect the -** Lua ABI (by making the changes here, you ensure that all software -** connected to Lua, such as C libraries, will be compiled with the same -** configuration); or because they are seldom changed. -** -** Search for "@@" to find all configurable definitions. -** =================================================================== -*/ - - -/* -** {==================================================================== -** System Configuration: macros to adapt (if needed) Lua to some -** particular platform, for instance restricting it to C89. -** ===================================================================== -*/ - -/* -@@ LUA_USE_C89 controls the use of non-ISO-C89 features. -** Define it if you want Lua to avoid the use of a few C99 features -** or Windows-specific features on Windows. -*/ -/* #define LUA_USE_C89 */ - - -/* -** By default, Lua on Windows use (some) specific Windows features -*/ -#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) -#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ -#endif - - -#if defined(LUA_USE_WINDOWS) -#define LUA_DL_DLL /* enable support for DLL */ -#define LUA_USE_C89 /* broadly, Windows is C89 */ -#endif - - -#if defined(LUA_USE_LINUX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ -#endif - - -#if defined(LUA_USE_MACOSX) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ -#endif - - -/* -@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. -*/ -#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) - -/* }================================================================== */ - - - -/* -** {================================================================== -** Configuration for Number types. These options should not be -** set externally, because any other code connected to Lua must -** use the same configuration. -** =================================================================== -*/ - -/* -@@ LUA_INT_TYPE defines the type for Lua integers. -@@ LUA_FLOAT_TYPE defines the type for Lua floats. -** Lua should work fine with any mix of these options supported -** by your C compiler. The usual configurations are 64-bit integers -** and 'double' (the default), 32-bit integers and 'float' (for -** restricted platforms), and 'long'/'double' (for C compilers not -** compliant with C99, which may not have support for 'long long'). -*/ - -/* predefined options for LUA_INT_TYPE */ -#define LUA_INT_INT 1 -#define LUA_INT_LONG 2 -#define LUA_INT_LONGLONG 3 - -/* predefined options for LUA_FLOAT_TYPE */ -#define LUA_FLOAT_FLOAT 1 -#define LUA_FLOAT_DOUBLE 2 -#define LUA_FLOAT_LONGDOUBLE 3 - - -/* Default configuration ('long long' and 'double', for 64-bit Lua) */ -#define LUA_INT_DEFAULT LUA_INT_LONGLONG -#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE - - -/* -@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. -*/ -#define LUA_32BITS 0 - - -/* -@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for -** C89 ('long' and 'double'); Windows always has '__int64', so it does -** not need to use this case. -*/ -#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) -#define LUA_C89_NUMBERS 1 -#else -#define LUA_C89_NUMBERS 0 -#endif - - -#if LUA_32BITS /* { */ -/* -** 32-bit integers and 'float' -*/ -#if LUAI_IS32INT /* use 'int' if big enough */ -#define LUA_INT_TYPE LUA_INT_INT -#else /* otherwise use 'long' */ -#define LUA_INT_TYPE LUA_INT_LONG -#endif -#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT - -#elif LUA_C89_NUMBERS /* }{ */ -/* -** largest types available for C89 ('long' and 'double') -*/ -#define LUA_INT_TYPE LUA_INT_LONG -#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE - -#else /* }{ */ -/* use defaults */ - -#define LUA_INT_TYPE LUA_INT_DEFAULT -#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT - -#endif /* } */ - - -/* }================================================================== */ - - - -/* -** {================================================================== -** Configuration for Paths. -** =================================================================== -*/ - -/* -** LUA_PATH_SEP is the character that separates templates in a path. -** LUA_PATH_MARK is the string that marks the substitution points in a -** template. -** LUA_EXEC_DIR in a Windows path is replaced by the executable's -** directory. -*/ -#define LUA_PATH_SEP ";" -#define LUA_PATH_MARK "?" -#define LUA_EXEC_DIR "!" - - -/* -@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for -** Lua libraries. -@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for -** C libraries. -** CHANGE them if your machine has a non-conventional directory -** hierarchy or if you want to install your libraries in -** non-conventional directories. -*/ - -#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#if defined(_WIN32) /* { */ -/* -** In Windows, any exclamation mark ('!') in the path is replaced by the -** path of the directory of the executable file of the current process. -*/ -#define LUA_LDIR "!\\lua\\" -#define LUA_CDIR "!\\" -#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" - -#if !defined(LUA_PATH_DEFAULT) -#define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ - LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ - ".\\?.lua;" ".\\?\\init.lua" -#endif - -#if !defined(LUA_CPATH_DEFAULT) -#define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.dll;" \ - LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ - LUA_CDIR"loadall.dll;" ".\\?.dll" -#endif - -#else /* }{ */ - -#define LUA_ROOT "/usr/local/" -#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" -#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" - -#if !defined(LUA_PATH_DEFAULT) -#define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ - "./?.lua;" "./?/init.lua" -#endif - -#if !defined(LUA_CPATH_DEFAULT) -#define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" -#endif - -#endif /* } */ - - -/* -@@ LUA_DIRSEP is the directory separator (for submodules). -** CHANGE it if your machine does not use "/" as the directory separator -** and is not Windows. (On Windows Lua automatically uses "\".) -*/ -#if !defined(LUA_DIRSEP) - -#if defined(_WIN32) -#define LUA_DIRSEP "\\" -#else -#define LUA_DIRSEP "/" -#endif - -#endif - -/* }================================================================== */ - - -/* -** {================================================================== -** Marks for exported symbols in the C code -** =================================================================== -*/ - -/* -@@ LUA_API is a mark for all core API functions. -@@ LUALIB_API is a mark for all auxiliary library functions. -@@ LUAMOD_API is a mark for all standard library opening functions. -** CHANGE them if you need to define those functions in some special way. -** For instance, if you want to create one Windows DLL with the core and -** the libraries, you may want to use the following definition (define -** LUA_BUILD_AS_DLL to get it). -*/ -#if defined(LUA_BUILD_AS_DLL) /* { */ - -#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ -#define LUA_API __declspec(dllexport) -#else /* }{ */ -#define LUA_API __declspec(dllimport) -#endif /* } */ - -#else /* }{ */ - -#define LUA_API extern - -#endif /* } */ - - -/* -** More often than not the libs go together with the core. -*/ -#define LUALIB_API LUA_API -#define LUAMOD_API LUA_API - - -/* -@@ LUAI_FUNC is a mark for all extern functions that are not to be -** exported to outside modules. -@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, -** none of which to be exported to outside modules (LUAI_DDEF for -** definitions and LUAI_DDEC for declarations). -** CHANGE them if you need to mark them in some special way. Elf/gcc -** (versions 3.2 and later) mark them as "hidden" to optimize access -** when Lua is compiled as a shared library. Not all elf targets support -** this attribute. Unfortunately, gcc does not offer a way to check -** whether the target offers that support, and those without support -** give a warning about it. To avoid these warnings, change to the -** default definition. -*/ -#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("internal"))) extern -#else /* }{ */ -#define LUAI_FUNC extern -#endif /* } */ - -#define LUAI_DDEC(dec) LUAI_FUNC dec -#define LUAI_DDEF /* empty */ - -/* }================================================================== */ - - -/* -** {================================================================== -** Compatibility with previous versions -** =================================================================== -*/ - -/* -@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. -** You can define it to get all options, or change specific options -** to fit your specific needs. -*/ -#if defined(LUA_COMPAT_5_3) /* { */ - -/* -@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated -** functions in the mathematical library. -** (These functions were already officially removed in 5.3; -** nevertheless they are still available here.) -*/ -#define LUA_COMPAT_MATHLIB - -/* -@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for -** manipulating other integer types (lua_pushunsigned, lua_tounsigned, -** luaL_checkint, luaL_checklong, etc.) -** (These macros were also officially removed in 5.3, but they are still -** available here.) -*/ -#define LUA_COMPAT_APIINTCASTS - - -/* -@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod -** using '__lt'. -*/ -#define LUA_COMPAT_LT_LE - - -/* -@@ The following macros supply trivial compatibility for some -** changes in the API. The macros themselves document how to -** change your code to avoid using them. -** (Once more, these macros were officially removed in 5.3, but they are -** still available here.) -*/ -#define lua_strlen(L,i) lua_rawlen(L, (i)) - -#define lua_objlen(L,i) lua_rawlen(L, (i)) - -#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) -#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) - -#endif /* } */ - -/* }================================================================== */ - - - -/* -** {================================================================== -** Configuration for Numbers (low-level part). -** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* -** satisfy your needs. -** =================================================================== -*/ - -/* -@@ LUAI_UACNUMBER is the result of a 'default argument promotion' -@@ over a floating number. -@@ l_floatatt(x) corrects float attribute 'x' to the proper float type -** by prefixing it with one of FLT/DBL/LDBL. -@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. -@@ LUA_NUMBER_FMT is the format for writing floats. -@@ lua_number2str converts a float to a string. -@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. -@@ l_floor takes the floor of a float. -@@ lua_str2number converts a decimal numeral to a number. -*/ - - -/* The following definitions are good for most cases here */ - -#define l_floor(x) (l_mathop(floor)(x)) - -#define lua_number2str(s,sz,n) \ - l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) - -/* -@@ lua_numbertointeger converts a float number with an integral value -** to an integer, or returns 0 if float is not within the range of -** a lua_Integer. (The range comparisons are tricky because of -** rounding. The tests here assume a two-complement representation, -** where MININTEGER always has an exact representation as a float; -** MAXINTEGER may not have one, and therefore its conversion to float -** may have an ill-defined value.) -*/ -#define lua_numbertointeger(n,p) \ - ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ - (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ - (*(p) = (LUA_INTEGER)(n), 1)) - - -/* now the variable definitions */ - -#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ - -#define LUA_NUMBER float - -#define l_floatatt(n) (FLT_##n) - -#define LUAI_UACNUMBER double - -#define LUA_NUMBER_FRMLEN "" -#define LUA_NUMBER_FMT "%.7g" - -#define l_mathop(op) op##f - -#define lua_str2number(s,p) strtof((s), (p)) - - -#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ - -#define LUA_NUMBER long double - -#define l_floatatt(n) (LDBL_##n) - -#define LUAI_UACNUMBER long double - -#define LUA_NUMBER_FRMLEN "L" -#define LUA_NUMBER_FMT "%.19Lg" - -#define l_mathop(op) op##l - -#define lua_str2number(s,p) strtold((s), (p)) - -#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ - -#define LUA_NUMBER double - -#define l_floatatt(n) (DBL_##n) - -#define LUAI_UACNUMBER double - -#define LUA_NUMBER_FRMLEN "" -#define LUA_NUMBER_FMT "%.14g" - -#define l_mathop(op) op - -#define lua_str2number(s,p) strtod((s), (p)) - -#else /* }{ */ - -#error "numeric float type not defined" - -#endif /* } */ - - - -/* -@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. -@@ LUAI_UACINT is the result of a 'default argument promotion' -@@ over a LUA_INTEGER. -@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. -@@ LUA_INTEGER_FMT is the format for writing integers. -@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. -@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. -@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. -@@ lua_integer2str converts an integer to a string. -*/ - - -/* The following definitions are good for most cases here */ - -#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" - -#define LUAI_UACINT LUA_INTEGER - -#define lua_integer2str(s,sz,n) \ - l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) - -/* -** use LUAI_UACINT here to avoid problems with promotions (which -** can turn a comparison between unsigneds into a signed comparison) -*/ -#define LUA_UNSIGNED unsigned LUAI_UACINT - - -/* now the variable definitions */ - -#if LUA_INT_TYPE == LUA_INT_INT /* { int */ - -#define LUA_INTEGER int -#define LUA_INTEGER_FRMLEN "" - -#define LUA_MAXINTEGER INT_MAX -#define LUA_MININTEGER INT_MIN - -#define LUA_MAXUNSIGNED UINT_MAX - -#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ - -#define LUA_INTEGER long -#define LUA_INTEGER_FRMLEN "l" - -#define LUA_MAXINTEGER LONG_MAX -#define LUA_MININTEGER LONG_MIN - -#define LUA_MAXUNSIGNED ULONG_MAX - -#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ - -/* use presence of macro LLONG_MAX as proxy for C99 compliance */ -#if defined(LLONG_MAX) /* { */ -/* use ISO C99 stuff */ - -#define LUA_INTEGER long long -#define LUA_INTEGER_FRMLEN "ll" - -#define LUA_MAXINTEGER LLONG_MAX -#define LUA_MININTEGER LLONG_MIN - -#define LUA_MAXUNSIGNED ULLONG_MAX - -#elif defined(LUA_USE_WINDOWS) /* }{ */ -/* in Windows, can use specific Windows types */ - -#define LUA_INTEGER __int64 -#define LUA_INTEGER_FRMLEN "I64" - -#define LUA_MAXINTEGER _I64_MAX -#define LUA_MININTEGER _I64_MIN - -#define LUA_MAXUNSIGNED _UI64_MAX - -#else /* }{ */ - -#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ - or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" - -#endif /* } */ - -#else /* }{ */ - -#error "numeric integer type not defined" - -#endif /* } */ - -/* }================================================================== */ - - -/* -** {================================================================== -** Dependencies with C99 and other C details -** =================================================================== -*/ - -/* -@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. -** (All uses in Lua have only one format item.) -*/ -#if !defined(LUA_USE_C89) -#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) -#else -#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) -#endif - - -/* -@@ lua_strx2number converts a hexadecimal numeral to a number. -** In C99, 'strtod' does that conversion. Otherwise, you can -** leave 'lua_strx2number' undefined and Lua will provide its own -** implementation. -*/ -#if !defined(LUA_USE_C89) -#define lua_strx2number(s,p) lua_str2number(s,p) -#endif - - -/* -@@ lua_pointer2str converts a pointer to a readable string in a -** non-specified way. -*/ -#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) - - -/* -@@ lua_number2strx converts a float to a hexadecimal numeral. -** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. -** Otherwise, you can leave 'lua_number2strx' undefined and Lua will -** provide its own implementation. -*/ -#if !defined(LUA_USE_C89) -#define lua_number2strx(L,b,sz,f,n) \ - ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) -#endif - - -/* -** 'strtof' and 'opf' variants for math functions are not valid in -** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the -** availability of these variants. ('math.h' is already included in -** all files that use these macros.) -*/ -#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) -#undef l_mathop /* variants not available */ -#undef lua_str2number -#define l_mathop(op) (lua_Number)op /* no variant */ -#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) -#endif - - -/* -@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation -** functions. It must be a numerical type; Lua will use 'intptr_t' if -** available, otherwise it will use 'ptrdiff_t' (the nearest thing to -** 'intptr_t' in C89) -*/ -#define LUA_KCONTEXT ptrdiff_t - -#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ - __STDC_VERSION__ >= 199901L -#include -#if defined(INTPTR_MAX) /* even in C99 this type is optional */ -#undef LUA_KCONTEXT -#define LUA_KCONTEXT intptr_t -#endif -#endif - - -/* -@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). -** Change that if you do not want to use C locales. (Code using this -** macro must include the header 'locale.h'.) -*/ -#if !defined(lua_getlocaledecpoint) -#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) -#endif - - -/* -** macros to improve jump prediction, used mostly for error handling -** and debug facilities. (Some macros in the Lua API use these macros. -** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your -** code.) -*/ -#if !defined(luai_likely) - -#if defined(__GNUC__) && !defined(LUA_NOBUILTIN) -#define luai_likely(x) (__builtin_expect(((x) != 0), 1)) -#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) -#else -#define luai_likely(x) (x) -#define luai_unlikely(x) (x) -#endif - -#endif - - -#if defined(LUA_CORE) || defined(LUA_LIB) -/* shorter names for Lua's own use */ -#define l_likely(x) luai_likely(x) -#define l_unlikely(x) luai_unlikely(x) -#endif - - - -/* }================================================================== */ - - -/* -** {================================================================== -** Language Variations -** ===================================================================== -*/ - -/* -@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some -** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from -** numbers to strings. Define LUA_NOCVTS2N to turn off automatic -** coercion from strings to numbers. -*/ -/* #define LUA_NOCVTN2S */ -/* #define LUA_NOCVTS2N */ - - -/* -@@ LUA_USE_APICHECK turns on several consistency checks on the C API. -** Define it as a help when debugging C code. -*/ -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(l,e) assert(e) -#endif - -/* }================================================================== */ - - -/* -** {================================================================== -** Macros that affect the API and must be stable (that is, must be the -** same when you compile Lua and when you compile code that links to -** Lua). -** ===================================================================== -*/ - -/* -@@ LUAI_MAXSTACK limits the size of the Lua stack. -** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua from consuming unlimited stack -** space (and to reserve some numbers for pseudo-indices). -** (It must fit into max(size_t)/32.) -*/ -#if LUAI_IS32INT -#define LUAI_MAXSTACK 1000000 -#else -#define LUAI_MAXSTACK 15000 -#endif - - -/* -@@ LUA_EXTRASPACE defines the size of a raw memory area associated with -** a Lua state with very fast access. -** CHANGE it if you need a different size. -*/ -#define LUA_EXTRASPACE (sizeof(void *)) - - -/* -@@ LUA_IDSIZE gives the maximum size for the description of the source -@@ of a function in debug information. -** CHANGE it if you want a different size. -*/ -#define LUA_IDSIZE 60 - - -/* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -*/ -#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) - - -/* -@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure -** maximum alignment for the other items in that union. -*/ -#define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l - -/* }================================================================== */ - - - - - -/* =================================================================== */ - -/* -** Local configuration. You can use this space to add your redefinitions -** without modifying the main part of the file. -*/ - - - - - -#endif - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lualib.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lualib.h deleted file mode 100644 index 2625529..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lualib.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -** $Id: lualib.h $ -** Lua standard libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lualib_h -#define lualib_h - -#include "lua.h" - - -/* version suffix for environment variable names */ -#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - - -LUAMOD_API int (luaopen_base) (lua_State *L); - -#define LUA_COLIBNAME "coroutine" -LUAMOD_API int (luaopen_coroutine) (lua_State *L); - -#define LUA_TABLIBNAME "table" -LUAMOD_API int (luaopen_table) (lua_State *L); - -#define LUA_IOLIBNAME "io" -LUAMOD_API int (luaopen_io) (lua_State *L); - -#define LUA_OSLIBNAME "os" -LUAMOD_API int (luaopen_os) (lua_State *L); - -#define LUA_STRLIBNAME "string" -LUAMOD_API int (luaopen_string) (lua_State *L); - -#define LUA_UTF8LIBNAME "utf8" -LUAMOD_API int (luaopen_utf8) (lua_State *L); - -#define LUA_MATHLIBNAME "math" -LUAMOD_API int (luaopen_math) (lua_State *L); - -#define LUA_DBLIBNAME "debug" -LUAMOD_API int (luaopen_debug) (lua_State *L); - -#define LUA_LOADLIBNAME "package" -LUAMOD_API int (luaopen_package) (lua_State *L); - - -/* open all previous libraries */ -LUALIB_API void (luaL_openlibs) (lua_State *L); - - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.c deleted file mode 100644 index 5aa55c4..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.c +++ /dev/null @@ -1,333 +0,0 @@ -/* -** $Id: lundump.c $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#define lundump_c -#define LUA_CORE - -#include "lprefix.h" - - -#include -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lmem.h" -#include "lobject.h" -#include "lstring.h" -#include "lundump.h" -#include "lzio.h" - - -#if !defined(luai_verifycode) -#define luai_verifycode(L,f) /* empty */ -#endif - - -typedef struct { - lua_State *L; - ZIO *Z; - const char *name; -} LoadState; - - -static l_noret error (LoadState *S, const char *why) { - luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); - luaD_throw(S->L, LUA_ERRSYNTAX); -} - - -/* -** All high-level loads go through loadVector; you can change it to -** adapt to the endianness of the input -*/ -#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) - -static void loadBlock (LoadState *S, void *b, size_t size) { - if (luaZ_read(S->Z, b, size) != 0) - error(S, "truncated chunk"); -} - - -#define loadVar(S,x) loadVector(S,&x,1) - - -static lu_byte loadByte (LoadState *S) { - int b = zgetc(S->Z); - if (b == EOZ) - error(S, "truncated chunk"); - return cast_byte(b); -} - - -static size_t loadUnsigned (LoadState *S, size_t limit) { - size_t x = 0; - int b; - limit >>= 7; - do { - b = loadByte(S); - if (x >= limit) - error(S, "integer overflow"); - x = (x << 7) | (b & 0x7f); - } while ((b & 0x80) == 0); - return x; -} - - -static size_t loadSize (LoadState *S) { - return loadUnsigned(S, ~(size_t)0); -} - - -static int loadInt (LoadState *S) { - return cast_int(loadUnsigned(S, INT_MAX)); -} - - -static lua_Number loadNumber (LoadState *S) { - lua_Number x; - loadVar(S, x); - return x; -} - - -static lua_Integer loadInteger (LoadState *S) { - lua_Integer x; - loadVar(S, x); - return x; -} - - -/* -** Load a nullable string into prototype 'p'. -*/ -static TString *loadStringN (LoadState *S, Proto *p) { - lua_State *L = S->L; - TString *ts; - size_t size = loadSize(S); - if (size == 0) /* no string? */ - return NULL; - else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ - char buff[LUAI_MAXSHORTLEN]; - loadVector(S, buff, size); /* load string into buffer */ - ts = luaS_newlstr(L, buff, size); /* create string */ - } - else { /* long string */ - ts = luaS_createlngstrobj(L, size); /* create string */ - setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ - luaD_inctop(L); - loadVector(S, getstr(ts), size); /* load directly in final place */ - L->top--; /* pop string */ - } - luaC_objbarrier(L, p, ts); - return ts; -} - - -/* -** Load a non-nullable string into prototype 'p'. -*/ -static TString *loadString (LoadState *S, Proto *p) { - TString *st = loadStringN(S, p); - if (st == NULL) - error(S, "bad format for constant string"); - return st; -} - - -static void loadCode (LoadState *S, Proto *f) { - int n = loadInt(S); - f->code = luaM_newvectorchecked(S->L, n, Instruction); - f->sizecode = n; - loadVector(S, f->code, n); -} - - -static void loadFunction(LoadState *S, Proto *f, TString *psource); - - -static void loadConstants (LoadState *S, Proto *f) { - int i; - int n = loadInt(S); - f->k = luaM_newvectorchecked(S->L, n, TValue); - f->sizek = n; - for (i = 0; i < n; i++) - setnilvalue(&f->k[i]); - for (i = 0; i < n; i++) { - TValue *o = &f->k[i]; - int t = loadByte(S); - switch (t) { - case LUA_VNIL: - setnilvalue(o); - break; - case LUA_VFALSE: - setbfvalue(o); - break; - case LUA_VTRUE: - setbtvalue(o); - break; - case LUA_VNUMFLT: - setfltvalue(o, loadNumber(S)); - break; - case LUA_VNUMINT: - setivalue(o, loadInteger(S)); - break; - case LUA_VSHRSTR: - case LUA_VLNGSTR: - setsvalue2n(S->L, o, loadString(S, f)); - break; - default: lua_assert(0); - } - } -} - - -static void loadProtos (LoadState *S, Proto *f) { - int i; - int n = loadInt(S); - f->p = luaM_newvectorchecked(S->L, n, Proto *); - f->sizep = n; - for (i = 0; i < n; i++) - f->p[i] = NULL; - for (i = 0; i < n; i++) { - f->p[i] = luaF_newproto(S->L); - luaC_objbarrier(S->L, f, f->p[i]); - loadFunction(S, f->p[i], f->source); - } -} - - -/* -** Load the upvalues for a function. The names must be filled first, -** because the filling of the other fields can raise read errors and -** the creation of the error message can call an emergency collection; -** in that case all prototypes must be consistent for the GC. -*/ -static void loadUpvalues (LoadState *S, Proto *f) { - int i, n; - n = loadInt(S); - f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); - f->sizeupvalues = n; - for (i = 0; i < n; i++) /* make array valid for GC */ - f->upvalues[i].name = NULL; - for (i = 0; i < n; i++) { /* following calls can raise errors */ - f->upvalues[i].instack = loadByte(S); - f->upvalues[i].idx = loadByte(S); - f->upvalues[i].kind = loadByte(S); - } -} - - -static void loadDebug (LoadState *S, Proto *f) { - int i, n; - n = loadInt(S); - f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); - f->sizelineinfo = n; - loadVector(S, f->lineinfo, n); - n = loadInt(S); - f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); - f->sizeabslineinfo = n; - for (i = 0; i < n; i++) { - f->abslineinfo[i].pc = loadInt(S); - f->abslineinfo[i].line = loadInt(S); - } - n = loadInt(S); - f->locvars = luaM_newvectorchecked(S->L, n, LocVar); - f->sizelocvars = n; - for (i = 0; i < n; i++) - f->locvars[i].varname = NULL; - for (i = 0; i < n; i++) { - f->locvars[i].varname = loadStringN(S, f); - f->locvars[i].startpc = loadInt(S); - f->locvars[i].endpc = loadInt(S); - } - n = loadInt(S); - for (i = 0; i < n; i++) - f->upvalues[i].name = loadStringN(S, f); -} - - -static void loadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = loadStringN(S, f); - if (f->source == NULL) /* no source in dump? */ - f->source = psource; /* reuse parent's source */ - f->linedefined = loadInt(S); - f->lastlinedefined = loadInt(S); - f->numparams = loadByte(S); - f->is_vararg = loadByte(S); - f->maxstacksize = loadByte(S); - loadCode(S, f); - loadConstants(S, f); - loadUpvalues(S, f); - loadProtos(S, f); - loadDebug(S, f); -} - - -static void checkliteral (LoadState *S, const char *s, const char *msg) { - char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ - size_t len = strlen(s); - loadVector(S, buff, len); - if (memcmp(s, buff, len) != 0) - error(S, msg); -} - - -static void fchecksize (LoadState *S, size_t size, const char *tname) { - if (loadByte(S) != size) - error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); -} - - -#define checksize(S,t) fchecksize(S,sizeof(t),#t) - -static void checkHeader (LoadState *S) { - /* skip 1st char (already read and checked) */ - checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); - if (loadByte(S) != LUAC_VERSION) - error(S, "version mismatch"); - if (loadByte(S) != LUAC_FORMAT) - error(S, "format mismatch"); - checkliteral(S, LUAC_DATA, "corrupted chunk"); - checksize(S, Instruction); - checksize(S, lua_Integer); - checksize(S, lua_Number); - if (loadInteger(S) != LUAC_INT) - error(S, "integer format mismatch"); - if (loadNumber(S) != LUAC_NUM) - error(S, "float format mismatch"); -} - - -/* -** Load precompiled chunk. -*/ -LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { - LoadState S; - LClosure *cl; - if (*name == '@' || *name == '=') - S.name = name + 1; - else if (*name == LUA_SIGNATURE[0]) - S.name = "binary string"; - else - S.name = name; - S.L = L; - S.Z = Z; - checkHeader(&S); - cl = luaF_newLclosure(L, loadByte(&S)); - setclLvalue2s(L, L->top, cl); - luaD_inctop(L); - cl->p = luaF_newproto(L); - luaC_objbarrier(L, cl, cl->p); - loadFunction(&S, cl->p, NULL); - lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luai_verifycode(L, cl->p); - return cl; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.h deleted file mode 100644 index f3748a9..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lundump.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -** $Id: lundump.h $ -** load precompiled Lua chunks -** See Copyright Notice in lua.h -*/ - -#ifndef lundump_h -#define lundump_h - -#include "llimits.h" -#include "lobject.h" -#include "lzio.h" - - -/* data to catch conversion errors */ -#define LUAC_DATA "\x19\x93\r\n\x1a\n" - -#define LUAC_INT 0x5678 -#define LUAC_NUM cast_num(370.5) - -/* -** Encode major-minor version in one byte, one nibble for each -*/ -#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) - -#define LUAC_FORMAT 0 /* this is the official format */ - -/* load one chunk; from lundump.c */ -LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); - -/* dump one chunk; from ldump.c */ -LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, - void* data, int strip); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lutf8lib.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lutf8lib.c deleted file mode 100644 index e7bf098..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lutf8lib.c +++ /dev/null @@ -1,286 +0,0 @@ -/* -** $Id: lutf8lib.c $ -** Standard library for UTF-8 manipulation -** See Copyright Notice in lua.h -*/ - -#define lutf8lib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include -#include -#include -#include - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#define MAXUNICODE 0x10FFFFu - -#define MAXUTF 0x7FFFFFFFu - -/* -** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. -*/ -#if (UINT_MAX >> 30) >= 1 -typedef unsigned int utfint; -#else -typedef unsigned long utfint; -#endif - - -#define iscont(p) ((*(p) & 0xC0) == 0x80) - - -/* from strlib */ -/* translate a relative string position: negative means back from end */ -static lua_Integer u_posrelat (lua_Integer pos, size_t len) { - if (pos >= 0) return pos; - else if (0u - (size_t)pos > len) return 0; - else return (lua_Integer)len + pos + 1; -} - - -/* -** Decode one UTF-8 sequence, returning NULL if byte sequence is -** invalid. The array 'limits' stores the minimum value for each -** sequence length, to check for overlong representations. Its first -** entry forces an error for non-ascii bytes with no continuation -** bytes (count == 0). -*/ -static const char *utf8_decode (const char *s, utfint *val, int strict) { - static const utfint limits[] = - {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; - unsigned int c = (unsigned char)s[0]; - utfint res = 0; /* final result */ - if (c < 0x80) /* ascii? */ - res = c; - else { - int count = 0; /* to count number of continuation bytes */ - for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ - unsigned int cc = (unsigned char)s[++count]; /* read next byte */ - if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ - return NULL; /* invalid byte sequence */ - res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ - } - res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ - if (count > 5 || res > MAXUTF || res < limits[count]) - return NULL; /* invalid byte sequence */ - s += count; /* skip continuation bytes read */ - } - if (strict) { - /* check for invalid code points; too large or surrogates */ - if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) - return NULL; - } - if (val) *val = res; - return s + 1; /* +1 to include first byte */ -} - - -/* -** utf8len(s [, i [, j [, lax]]]) --> number of characters that -** start in the range [i,j], or nil + current position if 's' is not -** well formed in that interval -*/ -static int utflen (lua_State *L) { - lua_Integer n = 0; /* counter for the number of characters */ - size_t len; /* string length in bytes */ - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); - lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); - int lax = lua_toboolean(L, 4); - luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, - "initial position out of bounds"); - luaL_argcheck(L, --posj < (lua_Integer)len, 3, - "final position out of bounds"); - while (posi <= posj) { - const char *s1 = utf8_decode(s + posi, NULL, !lax); - if (s1 == NULL) { /* conversion error? */ - luaL_pushfail(L); /* return fail ... */ - lua_pushinteger(L, posi + 1); /* ... and current position */ - return 2; - } - posi = s1 - s; - n++; - } - lua_pushinteger(L, n); - return 1; -} - - -/* -** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all -** characters that start in the range [i,j] -*/ -static int codepoint (lua_State *L) { - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); - lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); - int lax = lua_toboolean(L, 4); - int n; - const char *se; - luaL_argcheck(L, posi >= 1, 2, "out of bounds"); - luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); - if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ - return luaL_error(L, "string slice too long"); - n = (int)(pose - posi) + 1; /* upper bound for number of returns */ - luaL_checkstack(L, n, "string slice too long"); - n = 0; /* count the number of returns */ - se = s + pose; /* string end */ - for (s += posi - 1; s < se;) { - utfint code; - s = utf8_decode(s, &code, !lax); - if (s == NULL) - return luaL_error(L, "invalid UTF-8 code"); - lua_pushinteger(L, code); - n++; - } - return n; -} - - -static void pushutfchar (lua_State *L, int arg) { - lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); - luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); - lua_pushfstring(L, "%U", (long)code); -} - - -/* -** utfchar(n1, n2, ...) -> char(n1)..char(n2)... -*/ -static int utfchar (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - if (n == 1) /* optimize common case of single char */ - pushutfchar(L, 1); - else { - int i; - luaL_Buffer b; - luaL_buffinit(L, &b); - for (i = 1; i <= n; i++) { - pushutfchar(L, i); - luaL_addvalue(&b); - } - luaL_pushresult(&b); - } - return 1; -} - - -/* -** offset(s, n, [i]) -> index where n-th character counting from -** position 'i' starts; 0 means character at 'i'. -*/ -static int byteoffset (lua_State *L) { - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Integer n = luaL_checkinteger(L, 2); - lua_Integer posi = (n >= 0) ? 1 : len + 1; - posi = u_posrelat(luaL_optinteger(L, 3, posi), len); - luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, - "position out of bounds"); - if (n == 0) { - /* find beginning of current byte sequence */ - while (posi > 0 && iscont(s + posi)) posi--; - } - else { - if (iscont(s + posi)) - return luaL_error(L, "initial position is a continuation byte"); - if (n < 0) { - while (n < 0 && posi > 0) { /* move back */ - do { /* find beginning of previous character */ - posi--; - } while (posi > 0 && iscont(s + posi)); - n++; - } - } - else { - n--; /* do not move for 1st character */ - while (n > 0 && posi < (lua_Integer)len) { - do { /* find beginning of next character */ - posi++; - } while (iscont(s + posi)); /* (cannot pass final '\0') */ - n--; - } - } - } - if (n == 0) /* did it find given character? */ - lua_pushinteger(L, posi + 1); - else /* no such character */ - luaL_pushfail(L); - return 1; -} - - -static int iter_aux (lua_State *L, int strict) { - size_t len; - const char *s = luaL_checklstring(L, 1, &len); - lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); - if (n < len) { - while (iscont(s + n)) n++; /* skip continuation bytes */ - } - if (n >= len) /* (also handles original 'n' being negative) */ - return 0; /* no more codepoints */ - else { - utfint code; - const char *next = utf8_decode(s + n, &code, strict); - if (next == NULL) - return luaL_error(L, "invalid UTF-8 code"); - lua_pushinteger(L, n + 1); - lua_pushinteger(L, code); - return 2; - } -} - - -static int iter_auxstrict (lua_State *L) { - return iter_aux(L, 1); -} - -static int iter_auxlax (lua_State *L) { - return iter_aux(L, 0); -} - - -static int iter_codes (lua_State *L) { - int lax = lua_toboolean(L, 2); - luaL_checkstring(L, 1); - lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); - lua_pushvalue(L, 1); - lua_pushinteger(L, 0); - return 3; -} - - -/* pattern to match a single UTF-8 character */ -#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" - - -static const luaL_Reg funcs[] = { - {"offset", byteoffset}, - {"codepoint", codepoint}, - {"char", utfchar}, - {"len", utflen}, - {"codes", iter_codes}, - /* placeholders */ - {"charpattern", NULL}, - {NULL, NULL} -}; - - -LUAMOD_API int luaopen_utf8 (lua_State *L) { - luaL_newlib(L, funcs); - lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); - lua_setfield(L, -2, "charpattern"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.c deleted file mode 100644 index 2ec3440..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.c +++ /dev/null @@ -1,1840 +0,0 @@ -/* -** $Id: lvm.c $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#define lvm_c -#define LUA_CORE - -#include "lprefix.h" - -#include -#include -#include -#include -#include -#include - -#include "lua.h" - -#include "ldebug.h" -#include "ldo.h" -#include "lfunc.h" -#include "lgc.h" -#include "lobject.h" -#include "lopcodes.h" -#include "lstate.h" -#include "lstring.h" -#include "ltable.h" -#include "ltm.h" -#include "lvm.h" - - -/* -** By default, use jump tables in the main interpreter loop on gcc -** and compatible compilers. -*/ -#if !defined(LUA_USE_JUMPTABLE) -#if defined(__GNUC__) -#define LUA_USE_JUMPTABLE 1 -#else -#define LUA_USE_JUMPTABLE 0 -#endif -#endif - - - -/* limit for table tag-method chains (to avoid infinite loops) */ -#define MAXTAGLOOP 2000 - - -/* -** 'l_intfitsf' checks whether a given integer is in the range that -** can be converted to a float without rounding. Used in comparisons. -*/ - -/* number of bits in the mantissa of a float */ -#define NBM (l_floatatt(MANT_DIG)) - -/* -** Check whether some integers may not fit in a float, testing whether -** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.) -** (The shifts are done in parts, to avoid shifting by more than the size -** of an integer. In a worst case, NBM == 113 for long double and -** sizeof(long) == 32.) -*/ -#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ - >> (NBM - (3 * (NBM / 4)))) > 0 - -/* limit for integers that fit in a float */ -#define MAXINTFITSF ((lua_Unsigned)1 << NBM) - -/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ -#define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) - -#else /* all integers fit in a float precisely */ - -#define l_intfitsf(i) 1 - -#endif - - -/* -** Try to convert a value from string to a number value. -** If the value is not a string or is a string not representing -** a valid numeral (or if coercions from strings to numbers -** are disabled via macro 'cvt2num'), do not modify 'result' -** and return 0. -*/ -static int l_strton (const TValue *obj, TValue *result) { - lua_assert(obj != result); - if (!cvt2num(obj)) /* is object not a string? */ - return 0; - else - return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); -} - - -/* -** Try to convert a value to a float. The float case is already handled -** by the macro 'tonumber'. -*/ -int luaV_tonumber_ (const TValue *obj, lua_Number *n) { - TValue v; - if (ttisinteger(obj)) { - *n = cast_num(ivalue(obj)); - return 1; - } - else if (l_strton(obj, &v)) { /* string coercible to number? */ - *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ - return 1; - } - else - return 0; /* conversion failed */ -} - - -/* -** try to convert a float to an integer, rounding according to 'mode'. -*/ -int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { - lua_Number f = l_floor(n); - if (n != f) { /* not an integral value? */ - if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ - else if (mode == F2Iceil) /* needs ceil? */ - f += 1; /* convert floor to ceil (remember: n != f) */ - } - return lua_numbertointeger(f, p); -} - - -/* -** try to convert a value to an integer, rounding according to 'mode', -** without string coercion. -** ("Fast track" handled by macro 'tointegerns'.) -*/ -int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { - if (ttisfloat(obj)) - return luaV_flttointeger(fltvalue(obj), p, mode); - else if (ttisinteger(obj)) { - *p = ivalue(obj); - return 1; - } - else - return 0; -} - - -/* -** try to convert a value to an integer. -*/ -int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { - TValue v; - if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ - obj = &v; /* change it to point to its corresponding number */ - return luaV_tointegerns(obj, p, mode); -} - - -/* -** Try to convert a 'for' limit to an integer, preserving the semantics -** of the loop. Return true if the loop must not run; otherwise, '*p' -** gets the integer limit. -** (The following explanation assumes a positive step; it is valid for -** negative steps mutatis mutandis.) -** If the limit is an integer or can be converted to an integer, -** rounding down, that is the limit. -** Otherwise, check whether the limit can be converted to a float. If -** the float is too large, clip it to LUA_MAXINTEGER. If the float -** is too negative, the loop should not run, because any initial -** integer value is greater than such limit; so, the function returns -** true to signal that. (For this latter case, no integer limit would be -** correct; even a limit of LUA_MININTEGER would run the loop once for -** an initial value equal to LUA_MININTEGER.) -*/ -static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, - lua_Integer *p, lua_Integer step) { - if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { - /* not coercible to in integer */ - lua_Number flim; /* try to convert to float */ - if (!tonumber(lim, &flim)) /* cannot convert to float? */ - luaG_forerror(L, lim, "limit"); - /* else 'flim' is a float out of integer bounds */ - if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ - if (step < 0) return 1; /* initial value must be less than it */ - *p = LUA_MAXINTEGER; /* truncate */ - } - else { /* it is less than min integer */ - if (step > 0) return 1; /* initial value must be greater than it */ - *p = LUA_MININTEGER; /* truncate */ - } - } - return (step > 0 ? init > *p : init < *p); /* not to run? */ -} - - -/* -** Prepare a numerical for loop (opcode OP_FORPREP). -** Return true to skip the loop. Otherwise, -** after preparation, stack will be as follows: -** ra : internal index (safe copy of the control variable) -** ra + 1 : loop counter (integer loops) or limit (float loops) -** ra + 2 : step -** ra + 3 : control variable -*/ -static int forprep (lua_State *L, StkId ra) { - TValue *pinit = s2v(ra); - TValue *plimit = s2v(ra + 1); - TValue *pstep = s2v(ra + 2); - if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ - lua_Integer init = ivalue(pinit); - lua_Integer step = ivalue(pstep); - lua_Integer limit; - if (step == 0) - luaG_runerror(L, "'for' step is zero"); - setivalue(s2v(ra + 3), init); /* control variable */ - if (forlimit(L, init, plimit, &limit, step)) - return 1; /* skip the loop */ - else { /* prepare loop counter */ - lua_Unsigned count; - if (step > 0) { /* ascending loop? */ - count = l_castS2U(limit) - l_castS2U(init); - if (step != 1) /* avoid division in the too common case */ - count /= l_castS2U(step); - } - else { /* step < 0; descending loop */ - count = l_castS2U(init) - l_castS2U(limit); - /* 'step+1' avoids negating 'mininteger' */ - count /= l_castS2U(-(step + 1)) + 1u; - } - /* store the counter in place of the limit (which won't be - needed anymore) */ - setivalue(plimit, l_castU2S(count)); - } - } - else { /* try making all values floats */ - lua_Number init; lua_Number limit; lua_Number step; - if (l_unlikely(!tonumber(plimit, &limit))) - luaG_forerror(L, plimit, "limit"); - if (l_unlikely(!tonumber(pstep, &step))) - luaG_forerror(L, pstep, "step"); - if (l_unlikely(!tonumber(pinit, &init))) - luaG_forerror(L, pinit, "initial value"); - if (step == 0) - luaG_runerror(L, "'for' step is zero"); - if (luai_numlt(0, step) ? luai_numlt(limit, init) - : luai_numlt(init, limit)) - return 1; /* skip the loop */ - else { - /* make sure internal values are all floats */ - setfltvalue(plimit, limit); - setfltvalue(pstep, step); - setfltvalue(s2v(ra), init); /* internal index */ - setfltvalue(s2v(ra + 3), init); /* control variable */ - } - } - return 0; -} - - -/* -** Execute a step of a float numerical for loop, returning -** true iff the loop must continue. (The integer case is -** written online with opcode OP_FORLOOP, for performance.) -*/ -static int floatforloop (StkId ra) { - lua_Number step = fltvalue(s2v(ra + 2)); - lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra)); /* internal index */ - idx = luai_numadd(L, idx, step); /* increment index */ - if (luai_numlt(0, step) ? luai_numle(idx, limit) - : luai_numle(limit, idx)) { - chgfltvalue(s2v(ra), idx); /* update internal index */ - setfltvalue(s2v(ra + 3), idx); /* and control variable */ - return 1; /* jump back */ - } - else - return 0; /* finish the loop */ -} - - -/* -** Finish the table access 'val = t[key]'. -** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be empty). -*/ -void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - const TValue *slot) { - int loop; /* counter to avoid infinite loops */ - const TValue *tm; /* metamethod */ - for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (slot == NULL) { /* 't' is not a table? */ - lua_assert(!ttistable(t)); - tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (l_unlikely(notm(tm))) - luaG_typeerror(L, t, "index"); /* no metamethod */ - /* else will try the metamethod */ - } - else { /* 't' is a table */ - lua_assert(isempty(slot)); - tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ - if (tm == NULL) { /* no metamethod? */ - setnilvalue(s2v(val)); /* result is nil */ - return; - } - /* else will try the metamethod */ - } - if (ttisfunction(tm)) { /* is metamethod a function? */ - luaT_callTMres(L, tm, t, key, val); /* call it */ - return; - } - t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ - setobj2s(L, val, slot); /* done */ - return; - } - /* else repeat (tail call 'luaV_finishget') */ - } - luaG_runerror(L, "'__index' chain too long; possible loop"); -} - - -/* -** Finish a table assignment 't[key] = val'. -** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to a value with an absent key if there -** is no such entry. (The value at 'slot' must be empty, otherwise -** 'luaV_fastget' would have done the job.) -*/ -void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - TValue *val, const TValue *slot) { - int loop; /* counter to avoid infinite loops */ - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; /* '__newindex' metamethod */ - if (slot != NULL) { /* is 't' a table? */ - Table *h = hvalue(t); /* save 't' table */ - lua_assert(isempty(slot)); /* slot must be empty */ - tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ - if (tm == NULL) { /* no metamethod? */ - luaH_finishset(L, h, key, slot, val); /* set new value */ - invalidateTMcache(h); - luaC_barrierback(L, obj2gco(h), val); - return; - } - /* else will try the metamethod */ - } - else { /* not a table; check metamethod */ - tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); - if (l_unlikely(notm(tm))) - luaG_typeerror(L, t, "index"); - } - /* try the metamethod */ - if (ttisfunction(tm)) { - luaT_callTM(L, tm, t, key, val); - return; - } - t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { - luaV_finishfastset(L, t, slot, val); - return; /* done */ - } - /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ - } - luaG_runerror(L, "'__newindex' chain too long; possible loop"); -} - - -/* -** Compare two strings 'ls' x 'rs', returning an integer less-equal- -** -greater than zero if 'ls' is less-equal-greater than 'rs'. -** The code is a little tricky because it allows '\0' in the strings -** and it uses 'strcoll' (to respect locales) for each segments -** of the strings. -*/ -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = tsslen(ls); - const char *r = getstr(rs); - size_t lr = tsslen(rs); - for (;;) { /* for each segment */ - int temp = strcoll(l, r); - if (temp != 0) /* not equal? */ - return temp; /* done */ - else { /* strings are equal up to a '\0' */ - size_t len = strlen(l); /* index of first '\0' in both strings */ - if (len == lr) /* 'rs' is finished? */ - return (len == ll) ? 0 : 1; /* check 'ls' */ - else if (len == ll) /* 'ls' is finished? */ - return -1; /* 'ls' is less than 'rs' ('rs' is not finished) */ - /* both strings longer than 'len'; go on comparing after the '\0' */ - len++; - l += len; ll -= len; r += len; lr -= len; - } - } -} - - -/* -** Check whether integer 'i' is less than float 'f'. If 'i' has an -** exact representation as a float ('l_intfitsf'), compare numbers as -** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'. -** If 'ceil(f)' is out of integer range, either 'f' is greater than -** all integers or less than all integers. -** (The test with 'l_intfitsf' is only for performance; the else -** case is correct for all values, but it is slow due to the conversion -** from float to int.) -** When 'f' is NaN, comparisons must result in false. -*/ -l_sinline int LTintfloat (lua_Integer i, lua_Number f) { - if (l_intfitsf(i)) - return luai_numlt(cast_num(i), f); /* compare them as floats */ - else { /* i < f <=> i < ceil(f) */ - lua_Integer fi; - if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ - return i < fi; /* compare them as integers */ - else /* 'f' is either greater or less than all integers */ - return f > 0; /* greater? */ - } -} - - -/* -** Check whether integer 'i' is less than or equal to float 'f'. -** See comments on previous function. -*/ -l_sinline int LEintfloat (lua_Integer i, lua_Number f) { - if (l_intfitsf(i)) - return luai_numle(cast_num(i), f); /* compare them as floats */ - else { /* i <= f <=> i <= floor(f) */ - lua_Integer fi; - if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ - return i <= fi; /* compare them as integers */ - else /* 'f' is either greater or less than all integers */ - return f > 0; /* greater? */ - } -} - - -/* -** Check whether float 'f' is less than integer 'i'. -** See comments on previous function. -*/ -l_sinline int LTfloatint (lua_Number f, lua_Integer i) { - if (l_intfitsf(i)) - return luai_numlt(f, cast_num(i)); /* compare them as floats */ - else { /* f < i <=> floor(f) < i */ - lua_Integer fi; - if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ - return fi < i; /* compare them as integers */ - else /* 'f' is either greater or less than all integers */ - return f < 0; /* less? */ - } -} - - -/* -** Check whether float 'f' is less than or equal to integer 'i'. -** See comments on previous function. -*/ -l_sinline int LEfloatint (lua_Number f, lua_Integer i) { - if (l_intfitsf(i)) - return luai_numle(f, cast_num(i)); /* compare them as floats */ - else { /* f <= i <=> ceil(f) <= i */ - lua_Integer fi; - if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ - return fi <= i; /* compare them as integers */ - else /* 'f' is either greater or less than all integers */ - return f < 0; /* less? */ - } -} - - -/* -** Return 'l < r', for numbers. -*/ -l_sinline int LTnum (const TValue *l, const TValue *r) { - lua_assert(ttisnumber(l) && ttisnumber(r)); - if (ttisinteger(l)) { - lua_Integer li = ivalue(l); - if (ttisinteger(r)) - return li < ivalue(r); /* both are integers */ - else /* 'l' is int and 'r' is float */ - return LTintfloat(li, fltvalue(r)); /* l < r ? */ - } - else { - lua_Number lf = fltvalue(l); /* 'l' must be float */ - if (ttisfloat(r)) - return luai_numlt(lf, fltvalue(r)); /* both are float */ - else /* 'l' is float and 'r' is int */ - return LTfloatint(lf, ivalue(r)); - } -} - - -/* -** Return 'l <= r', for numbers. -*/ -l_sinline int LEnum (const TValue *l, const TValue *r) { - lua_assert(ttisnumber(l) && ttisnumber(r)); - if (ttisinteger(l)) { - lua_Integer li = ivalue(l); - if (ttisinteger(r)) - return li <= ivalue(r); /* both are integers */ - else /* 'l' is int and 'r' is float */ - return LEintfloat(li, fltvalue(r)); /* l <= r ? */ - } - else { - lua_Number lf = fltvalue(l); /* 'l' must be float */ - if (ttisfloat(r)) - return luai_numle(lf, fltvalue(r)); /* both are float */ - else /* 'l' is float and 'r' is int */ - return LEfloatint(lf, ivalue(r)); - } -} - - -/* -** return 'l < r' for non-numbers. -*/ -static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { - lua_assert(!ttisnumber(l) || !ttisnumber(r)); - if (ttisstring(l) && ttisstring(r)) /* both are strings? */ - return l_strcmp(tsvalue(l), tsvalue(r)) < 0; - else - return luaT_callorderTM(L, l, r, TM_LT); -} - - -/* -** Main operation less than; return 'l < r'. -*/ -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { - if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ - return LTnum(l, r); - else return lessthanothers(L, l, r); -} - - -/* -** return 'l <= r' for non-numbers. -*/ -static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { - lua_assert(!ttisnumber(l) || !ttisnumber(r)); - if (ttisstring(l) && ttisstring(r)) /* both are strings? */ - return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; - else - return luaT_callorderTM(L, l, r, TM_LE); -} - - -/* -** Main operation less than or equal to; return 'l <= r'. -*/ -int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { - if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ - return LEnum(l, r); - else return lessequalothers(L, l, r); -} - - -/* -** Main operation for equality of Lua values; return 't1 == t2'. -** L == NULL means raw equality (no metamethods) -*/ -int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { - const TValue *tm; - if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */ - if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) - return 0; /* only numbers can be equal with different variants */ - else { /* two numbers with different variants */ - /* One of them is an integer. If the other does not have an - integer value, they cannot be equal; otherwise, compare their - integer values. */ - lua_Integer i1, i2; - return (luaV_tointegerns(t1, &i1, F2Ieq) && - luaV_tointegerns(t2, &i2, F2Ieq) && - i1 == i2); - } - } - /* values have same type and same variant */ - switch (ttypetag(t1)) { - case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; - case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2)); - case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); - case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); - case LUA_VLCF: return fvalue(t1) == fvalue(t2); - case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); - case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); - case LUA_VUSERDATA: { - if (uvalue(t1) == uvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - case LUA_VTABLE: { - if (hvalue(t1) == hvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ - } - default: - return gcvalue(t1) == gcvalue(t2); - } - if (tm == NULL) /* no TM? */ - return 0; /* objects are different */ - else { - luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ - return !l_isfalse(s2v(L->top)); - } -} - - -/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ -#define tostring(L,o) \ - (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) - -#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) - -/* copy strings in stack from top - n up to top - 1 to buffer */ -static void copy2buff (StkId top, int n, char *buff) { - size_t tl = 0; /* size already copied */ - do { - size_t l = vslen(s2v(top - n)); /* length of string being copied */ - memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); - tl += l; - } while (--n > 0); -} - - -/* -** Main operation for concatenation: concat 'total' values in the stack, -** from 'L->top - total' up to 'L->top - 1'. -*/ -void luaV_concat (lua_State *L, int total) { - if (total == 1) - return; /* "all" values already concatenated */ - do { - StkId top = L->top; - int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || - !tostring(L, s2v(top - 1))) - luaT_tryconcatTM(L); - else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ - cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ - else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ - setobjs2s(L, top - 2, top - 1); /* result is second op. */ - } - else { - /* at least two non-empty string values; get as many as possible */ - size_t tl = vslen(s2v(top - 1)); - TString *ts; - /* collect total length and number of strings */ - for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { - size_t l = vslen(s2v(top - n - 1)); - if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) - luaG_runerror(L, "string length overflow"); - tl += l; - } - if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ - char buff[LUAI_MAXSHORTLEN]; - copy2buff(top, n, buff); /* copy strings to buffer */ - ts = luaS_newlstr(L, buff, tl); - } - else { /* long string; copy strings directly to final result */ - ts = luaS_createlngstrobj(L, tl); - copy2buff(top, n, getstr(ts)); - } - setsvalue2s(L, top - n, ts); /* create result */ - } - total -= n-1; /* got 'n' strings to create 1 new */ - L->top -= n-1; /* popped 'n' strings and pushed one */ - } while (total > 1); /* repeat until only 1 result left */ -} - - -/* -** Main operation 'ra = #rb'. -*/ -void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { - const TValue *tm; - switch (ttypetag(rb)) { - case LUA_VTABLE: { - Table *h = hvalue(rb); - tm = fasttm(L, h->metatable, TM_LEN); - if (tm) break; /* metamethod? break switch to call it */ - setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ - return; - } - case LUA_VSHRSTR: { - setivalue(s2v(ra), tsvalue(rb)->shrlen); - return; - } - case LUA_VLNGSTR: { - setivalue(s2v(ra), tsvalue(rb)->u.lnglen); - return; - } - default: { /* try metamethod */ - tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (l_unlikely(notm(tm))) /* no metamethod? */ - luaG_typeerror(L, rb, "get length of"); - break; - } - } - luaT_callTMres(L, tm, rb, rb, ra); -} - - -/* -** Integer division; return 'm // n', that is, floor(m/n). -** C division truncates its result (rounds towards zero). -** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, -** otherwise 'floor(q) == trunc(q) - 1'. -*/ -lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { - if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ - if (n == 0) - luaG_runerror(L, "attempt to divide by zero"); - return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ - } - else { - lua_Integer q = m / n; /* perform C division */ - if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ - q -= 1; /* correct result for different rounding */ - return q; - } -} - - -/* -** Integer modulus; return 'm % n'. (Assume that C '%' with -** negative operands follows C99 behavior. See previous comment -** about luaV_idiv.) -*/ -lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { - if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ - if (n == 0) - luaG_runerror(L, "attempt to perform 'n%%0'"); - return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ - } - else { - lua_Integer r = m % n; - if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ - r += n; /* correct result for different rounding */ - return r; - } -} - - -/* -** Float modulus -*/ -lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { - lua_Number r; - luai_nummod(L, m, n, r); - return r; -} - - -/* number of bits in an integer */ -#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) - -/* -** Shift left operation. (Shift right just negates 'y'.) -*/ -#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) - - -lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { - if (y < 0) { /* shift right? */ - if (y <= -NBITS) return 0; - else return intop(>>, x, -y); - } - else { /* shift left */ - if (y >= NBITS) return 0; - else return intop(<<, x, y); - } -} - - -/* -** create a new Lua closure, push it in the stack, and initialize -** its upvalues. -*/ -static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, - StkId ra) { - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; - int i; - LClosure *ncl = luaF_newLclosure(L, nup); - ncl->p = p; - setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ - for (i = 0; i < nup; i++) { /* fill in its upvalues */ - if (uv[i].instack) /* upvalue refers to local variable? */ - ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); - else /* get upvalue from enclosing function */ - ncl->upvals[i] = encup[uv[i].idx]; - luaC_objbarrier(L, ncl, ncl->upvals[i]); - } -} - - -/* -** finish execution of an opcode interrupted by a yield -*/ -void luaV_finishOp (lua_State *L) { - CallInfo *ci = L->ci; - StkId base = ci->func + 1; - Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ - OpCode op = GET_OPCODE(inst); - switch (op) { /* finish its execution */ - case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { - setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top); - break; - } - case OP_UNM: case OP_BNOT: case OP_LEN: - case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: - case OP_GETFIELD: case OP_SELF: { - setobjs2s(L, base + GETARG_A(inst), --L->top); - break; - } - case OP_LT: case OP_LE: - case OP_LTI: case OP_LEI: - case OP_GTI: case OP_GEI: - case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ - int res = !l_isfalse(s2v(L->top - 1)); - L->top--; -#if defined(LUA_COMPAT_LT_LE) - if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - ci->callstatus ^= CIST_LEQ; /* clear mark */ - res = !res; /* negate result */ - } -#endif - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_k(inst)) /* condition failed? */ - ci->u.l.savedpc++; /* skip jump instruction */ - break; - } - case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */ - int a = GETARG_A(inst); /* first element to concatenate */ - int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ - setobjs2s(L, top - 2, top); /* put TM result in proper position */ - L->top = top - 1; /* top is one after last element (at top-2) */ - luaV_concat(L, total); /* concat them (may yield again) */ - break; - } - case OP_CLOSE: { /* yielded closing variables */ - ci->u.l.savedpc--; /* repeat instruction to close other vars. */ - break; - } - case OP_RETURN: { /* yielded closing variables */ - StkId ra = base + GETARG_A(inst); - /* adjust top to signal correct number of returns, in case the - return is "up to top" ('isIT') */ - L->top = ra + ci->u2.nres; - /* repeat instruction to close other vars. and complete the return */ - ci->u.l.savedpc--; - break; - } - default: { - /* only these other opcodes can yield */ - lua_assert(op == OP_TFORCALL || op == OP_CALL || - op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || - op == OP_SETI || op == OP_SETFIELD); - break; - } - } -} - - - - -/* -** {================================================================== -** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' -** =================================================================== -*/ - -#define l_addi(L,a,b) intop(+, a, b) -#define l_subi(L,a,b) intop(-, a, b) -#define l_muli(L,a,b) intop(*, a, b) -#define l_band(a,b) intop(&, a, b) -#define l_bor(a,b) intop(|, a, b) -#define l_bxor(a,b) intop(^, a, b) - -#define l_lti(a,b) (a < b) -#define l_lei(a,b) (a <= b) -#define l_gti(a,b) (a > b) -#define l_gei(a,b) (a >= b) - - -/* -** Arithmetic operations with immediate operands. 'iop' is the integer -** operation, 'fop' is the float operation. -*/ -#define op_arithI(L,iop,fop) { \ - TValue *v1 = vRB(i); \ - int imm = GETARG_sC(i); \ - if (ttisinteger(v1)) { \ - lua_Integer iv1 = ivalue(v1); \ - pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ - } \ - else if (ttisfloat(v1)) { \ - lua_Number nb = fltvalue(v1); \ - lua_Number fimm = cast_num(imm); \ - pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ - }} - - -/* -** Auxiliary function for arithmetic operations over floats and others -** with two register operands. -*/ -#define op_arithf_aux(L,v1,v2,fop) { \ - lua_Number n1; lua_Number n2; \ - if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ - }} - - -/* -** Arithmetic operations over floats and others with register operands. -*/ -#define op_arithf(L,fop) { \ - TValue *v1 = vRB(i); \ - TValue *v2 = vRC(i); \ - op_arithf_aux(L, v1, v2, fop); } - - -/* -** Arithmetic operations with K operands for floats. -*/ -#define op_arithfK(L,fop) { \ - TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ - op_arithf_aux(L, v1, v2, fop); } - - -/* -** Arithmetic operations over integers and floats. -*/ -#define op_arith_aux(L,v1,v2,iop,fop) { \ - if (ttisinteger(v1) && ttisinteger(v2)) { \ - lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ - pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ - } \ - else op_arithf_aux(L, v1, v2, fop); } - - -/* -** Arithmetic operations with register operands. -*/ -#define op_arith(L,iop,fop) { \ - TValue *v1 = vRB(i); \ - TValue *v2 = vRC(i); \ - op_arith_aux(L, v1, v2, iop, fop); } - - -/* -** Arithmetic operations with K operands. -*/ -#define op_arithK(L,iop,fop) { \ - TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ - op_arith_aux(L, v1, v2, iop, fop); } - - -/* -** Bitwise operations with constant operand. -*/ -#define op_bitwiseK(L,op) { \ - TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ - lua_Integer i1; \ - lua_Integer i2 = ivalue(v2); \ - if (tointegerns(v1, &i1)) { \ - pc++; setivalue(s2v(ra), op(i1, i2)); \ - }} - - -/* -** Bitwise operations with register operands. -*/ -#define op_bitwise(L,op) { \ - TValue *v1 = vRB(i); \ - TValue *v2 = vRC(i); \ - lua_Integer i1; lua_Integer i2; \ - if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ - pc++; setivalue(s2v(ra), op(i1, i2)); \ - }} - - -/* -** Order operations with register operands. 'opn' actually works -** for all numbers, but the fast track improves performance for -** integers. -*/ -#define op_order(L,opi,opn,other) { \ - int cond; \ - TValue *rb = vRB(i); \ - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ - lua_Integer ia = ivalue(s2v(ra)); \ - lua_Integer ib = ivalue(rb); \ - cond = opi(ia, ib); \ - } \ - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ - cond = opn(s2v(ra), rb); \ - else \ - Protect(cond = other(L, s2v(ra), rb)); \ - docondjump(); } - - -/* -** Order operations with immediate operand. (Immediate operand is -** always small enough to have an exact representation as a float.) -*/ -#define op_orderI(L,opi,opf,inv,tm) { \ - int cond; \ - int im = GETARG_sB(i); \ - if (ttisinteger(s2v(ra))) \ - cond = opi(ivalue(s2v(ra)), im); \ - else if (ttisfloat(s2v(ra))) { \ - lua_Number fa = fltvalue(s2v(ra)); \ - lua_Number fim = cast_num(im); \ - cond = opf(fa, fim); \ - } \ - else { \ - int isf = GETARG_C(i); \ - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ - } \ - docondjump(); } - -/* }================================================================== */ - - -/* -** {================================================================== -** Function 'luaV_execute': main interpreter loop -** =================================================================== -*/ - -/* -** some macros for common tasks in 'luaV_execute' -*/ - - -#define RA(i) (base+GETARG_A(i)) -#define RB(i) (base+GETARG_B(i)) -#define vRB(i) s2v(RB(i)) -#define KB(i) (k+GETARG_B(i)) -#define RC(i) (base+GETARG_C(i)) -#define vRC(i) s2v(RC(i)) -#define KC(i) (k+GETARG_C(i)) -#define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) - - - -#define updatetrap(ci) (trap = ci->u.l.trap) - -#define updatebase(ci) (base = ci->func + 1) - - -#define updatestack(ci) \ - { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } - - -/* -** Execute a jump instruction. The 'updatetrap' allows signals to stop -** tight loops. (Without it, the local copy of 'trap' could never change.) -*/ -#define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } - - -/* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } - -/* -** do a conditional jump: skip next instruction if 'cond' is not what -** was expected (parameter 'k'), else do next instruction, which must -** be a jump. -*/ -#define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); - - -/* -** Correct global 'pc'. -*/ -#define savepc(L) (ci->u.l.savedpc = pc) - - -/* -** Whenever code can raise errors, the global 'pc' and the global -** 'top' must be correct to report occasional errors. -*/ -#define savestate(L,ci) (savepc(L), L->top = ci->top) - - -/* -** Protect code that, in general, can raise errors, reallocate the -** stack, and change the hooks. -*/ -#define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) - -/* special version that does not change the top */ -#define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) - -/* -** Protect code that can only raise errors. (That is, it cannot change -** the stack or hooks.) -*/ -#define halfProtect(exp) (savestate(L,ci), (exp)) - -/* 'c' is the limit of live values in the stack */ -#define checkGC(L,c) \ - { luaC_condGC(L, (savepc(L), L->top = (c)), \ - updatetrap(ci)); \ - luai_threadyield(L); } - - -/* fetch an instruction and prepare its execution */ -#define vmfetch() { \ - if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ - trap = luaG_traceexec(L, pc); /* handle hooks */ \ - updatebase(ci); /* correct stack */ \ - } \ - i = *(pc++); \ - ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ -} - -#define vmdispatch(o) switch(o) -#define vmcase(l) case l: -#define vmbreak break - - -void luaV_execute (lua_State *L, CallInfo *ci) { - LClosure *cl; - TValue *k; - StkId base; - const Instruction *pc; - int trap; -#if LUA_USE_JUMPTABLE -#include "ljumptab.h" -#endif - startfunc: - trap = L->hookmask; - returning: /* trap already set */ - cl = clLvalue(s2v(ci->func)); - k = cl->p->k; - pc = ci->u.l.savedpc; - if (l_unlikely(trap)) { - if (pc == cl->p->code) { /* first instruction (not resuming)? */ - if (cl->p->is_vararg) - trap = 0; /* hooks will start after VARARGPREP instruction */ - else /* check 'call' hook */ - luaD_hookcall(L, ci); - } - ci->u.l.trap = 1; /* assume trap is on, for now */ - } - base = ci->func + 1; - /* main loop of interpreter */ - for (;;) { - Instruction i; /* instruction being executed */ - StkId ra; /* instruction's A register */ - vmfetch(); - #if 0 - /* low-level line tracing for debugging Lua */ - printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); - #endif - lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top < L->stack_last); - /* invalidate top for instructions not expecting it */ - lua_assert(isIT(i) || (cast_void(L->top = base), 1)); - vmdispatch (GET_OPCODE(i)) { - vmcase(OP_MOVE) { - setobjs2s(L, ra, RB(i)); - vmbreak; - } - vmcase(OP_LOADI) { - lua_Integer b = GETARG_sBx(i); - setivalue(s2v(ra), b); - vmbreak; - } - vmcase(OP_LOADF) { - int b = GETARG_sBx(i); - setfltvalue(s2v(ra), cast_num(b)); - vmbreak; - } - vmcase(OP_LOADK) { - TValue *rb = k + GETARG_Bx(i); - setobj2s(L, ra, rb); - vmbreak; - } - vmcase(OP_LOADKX) { - TValue *rb; - rb = k + GETARG_Ax(*pc); pc++; - setobj2s(L, ra, rb); - vmbreak; - } - vmcase(OP_LOADFALSE) { - setbfvalue(s2v(ra)); - vmbreak; - } - vmcase(OP_LFALSESKIP) { - setbfvalue(s2v(ra)); - pc++; /* skip next instruction */ - vmbreak; - } - vmcase(OP_LOADTRUE) { - setbtvalue(s2v(ra)); - vmbreak; - } - vmcase(OP_LOADNIL) { - int b = GETARG_B(i); - do { - setnilvalue(s2v(ra++)); - } while (b--); - vmbreak; - } - vmcase(OP_GETUPVAL) { - int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); - vmbreak; - } - vmcase(OP_SETUPVAL) { - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, s2v(ra)); - luaC_barrier(L, uv, s2v(ra)); - vmbreak; - } - vmcase(OP_GETTABUP) { - const TValue *slot; - TValue *upval = cl->upvals[GETARG_B(i)]->v; - TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, upval, rc, ra, slot)); - vmbreak; - } - vmcase(OP_GETTABLE) { - const TValue *slot; - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Unsigned n; - if (ttisinteger(rc) /* fast track for integers? */ - ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot)) - : luaV_fastget(L, rb, rc, slot, luaH_get)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); - vmbreak; - } - vmcase(OP_GETI) { - const TValue *slot; - TValue *rb = vRB(i); - int c = GETARG_C(i); - if (luaV_fastgeti(L, rb, c, slot)) { - setobj2s(L, ra, slot); - } - else { - TValue key; - setivalue(&key, c); - Protect(luaV_finishget(L, rb, &key, ra, slot)); - } - vmbreak; - } - vmcase(OP_GETFIELD) { - const TValue *slot; - TValue *rb = vRB(i); - TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); - vmbreak; - } - vmcase(OP_SETTABUP) { - const TValue *slot; - TValue *upval = cl->upvals[GETARG_A(i)]->v; - TValue *rb = KB(i); - TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, upval, slot, rc); - } - else - Protect(luaV_finishset(L, upval, rb, rc, slot)); - vmbreak; - } - vmcase(OP_SETTABLE) { - const TValue *slot; - TValue *rb = vRB(i); /* key (table is in 'ra') */ - TValue *rc = RKC(i); /* value */ - lua_Unsigned n; - if (ttisinteger(rb) /* fast track for integers? */ - ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot)) - : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } - else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); - vmbreak; - } - vmcase(OP_SETI) { - const TValue *slot; - int c = GETARG_B(i); - TValue *rc = RKC(i); - if (luaV_fastgeti(L, s2v(ra), c, slot)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } - else { - TValue key; - setivalue(&key, c); - Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); - } - vmbreak; - } - vmcase(OP_SETFIELD) { - const TValue *slot; - TValue *rb = KB(i); - TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } - else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); - vmbreak; - } - vmcase(OP_NEWTABLE) { - int b = GETARG_B(i); /* log2(hash size) + 1 */ - int c = GETARG_C(i); /* array size */ - Table *t; - if (b > 0) - b = 1 << (b - 1); /* size is 2^(b - 1) */ - lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0)); - if (TESTARG_k(i)) /* non-zero extra argument? */ - c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ - pc++; /* skip extra argument */ - L->top = ra + 1; /* correct top in case of emergency GC */ - t = luaH_new(L); /* memory allocation */ - sethvalue2s(L, ra, t); - if (b != 0 || c != 0) - luaH_resize(L, t, c, b); /* idem */ - checkGC(L, ra + 1); - vmbreak; - } - vmcase(OP_SELF) { - const TValue *slot; - TValue *rb = vRB(i); - TValue *rc = RKC(i); - TString *key = tsvalue(rc); /* key must be a string */ - setobj2s(L, ra + 1, rb); - if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); - vmbreak; - } - vmcase(OP_ADDI) { - op_arithI(L, l_addi, luai_numadd); - vmbreak; - } - vmcase(OP_ADDK) { - op_arithK(L, l_addi, luai_numadd); - vmbreak; - } - vmcase(OP_SUBK) { - op_arithK(L, l_subi, luai_numsub); - vmbreak; - } - vmcase(OP_MULK) { - op_arithK(L, l_muli, luai_nummul); - vmbreak; - } - vmcase(OP_MODK) { - op_arithK(L, luaV_mod, luaV_modf); - vmbreak; - } - vmcase(OP_POWK) { - op_arithfK(L, luai_numpow); - vmbreak; - } - vmcase(OP_DIVK) { - op_arithfK(L, luai_numdiv); - vmbreak; - } - vmcase(OP_IDIVK) { - op_arithK(L, luaV_idiv, luai_numidiv); - vmbreak; - } - vmcase(OP_BANDK) { - op_bitwiseK(L, l_band); - vmbreak; - } - vmcase(OP_BORK) { - op_bitwiseK(L, l_bor); - vmbreak; - } - vmcase(OP_BXORK) { - op_bitwiseK(L, l_bxor); - vmbreak; - } - vmcase(OP_SHRI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); - } - vmbreak; - } - vmcase(OP_SHLI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); - } - vmbreak; - } - vmcase(OP_ADD) { - op_arith(L, l_addi, luai_numadd); - vmbreak; - } - vmcase(OP_SUB) { - op_arith(L, l_subi, luai_numsub); - vmbreak; - } - vmcase(OP_MUL) { - op_arith(L, l_muli, luai_nummul); - vmbreak; - } - vmcase(OP_MOD) { - op_arith(L, luaV_mod, luaV_modf); - vmbreak; - } - vmcase(OP_POW) { - op_arithf(L, luai_numpow); - vmbreak; - } - vmcase(OP_DIV) { /* float division (always with floats) */ - op_arithf(L, luai_numdiv); - vmbreak; - } - vmcase(OP_IDIV) { /* floor division */ - op_arith(L, luaV_idiv, luai_numidiv); - vmbreak; - } - vmcase(OP_BAND) { - op_bitwise(L, l_band); - vmbreak; - } - vmcase(OP_BOR) { - op_bitwise(L, l_bor); - vmbreak; - } - vmcase(OP_BXOR) { - op_bitwise(L, l_bxor); - vmbreak; - } - vmcase(OP_SHR) { - op_bitwise(L, luaV_shiftr); - vmbreak; - } - vmcase(OP_SHL) { - op_bitwise(L, luaV_shiftl); - vmbreak; - } - vmcase(OP_MMBIN) { - Instruction pi = *(pc - 2); /* original arith. expression */ - TValue *rb = vRB(i); - TMS tm = (TMS)GETARG_C(i); - StkId result = RA(pi); - lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); - Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); - vmbreak; - } - vmcase(OP_MMBINI) { - Instruction pi = *(pc - 2); /* original arith. expression */ - int imm = GETARG_sB(i); - TMS tm = (TMS)GETARG_C(i); - int flip = GETARG_k(i); - StkId result = RA(pi); - Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); - vmbreak; - } - vmcase(OP_MMBINK) { - Instruction pi = *(pc - 2); /* original arith. expression */ - TValue *imm = KB(i); - TMS tm = (TMS)GETARG_C(i); - int flip = GETARG_k(i); - StkId result = RA(pi); - Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); - vmbreak; - } - vmcase(OP_UNM) { - TValue *rb = vRB(i); - lua_Number nb; - if (ttisinteger(rb)) { - lua_Integer ib = ivalue(rb); - setivalue(s2v(ra), intop(-, 0, ib)); - } - else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_numunm(L, nb)); - } - else - Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); - vmbreak; - } - vmcase(OP_BNOT) { - TValue *rb = vRB(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); - } - else - Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); - vmbreak; - } - vmcase(OP_NOT) { - TValue *rb = vRB(i); - if (l_isfalse(rb)) - setbtvalue(s2v(ra)); - else - setbfvalue(s2v(ra)); - vmbreak; - } - vmcase(OP_LEN) { - Protect(luaV_objlen(L, ra, vRB(i))); - vmbreak; - } - vmcase(OP_CONCAT) { - int n = GETARG_B(i); /* number of elements to concatenate */ - L->top = ra + n; /* mark the end of concat operands */ - ProtectNT(luaV_concat(L, n)); - checkGC(L, L->top); /* 'luaV_concat' ensures correct top */ - vmbreak; - } - vmcase(OP_CLOSE) { - Protect(luaF_close(L, ra, LUA_OK, 1)); - vmbreak; - } - vmcase(OP_TBC) { - /* create new to-be-closed upvalue */ - halfProtect(luaF_newtbcupval(L, ra)); - vmbreak; - } - vmcase(OP_JMP) { - dojump(ci, i, 0); - vmbreak; - } - vmcase(OP_EQ) { - int cond; - TValue *rb = vRB(i); - Protect(cond = luaV_equalobj(L, s2v(ra), rb)); - docondjump(); - vmbreak; - } - vmcase(OP_LT) { - op_order(L, l_lti, LTnum, lessthanothers); - vmbreak; - } - vmcase(OP_LE) { - op_order(L, l_lei, LEnum, lessequalothers); - vmbreak; - } - vmcase(OP_EQK) { - TValue *rb = KB(i); - /* basic types do not use '__eq'; we can use raw equality */ - int cond = luaV_rawequalobj(s2v(ra), rb); - docondjump(); - vmbreak; - } - vmcase(OP_EQI) { - int cond; - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (ivalue(s2v(ra)) == im); - else if (ttisfloat(s2v(ra))) - cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); - else - cond = 0; /* other types cannot be equal to a number */ - docondjump(); - vmbreak; - } - vmcase(OP_LTI) { - op_orderI(L, l_lti, luai_numlt, 0, TM_LT); - vmbreak; - } - vmcase(OP_LEI) { - op_orderI(L, l_lei, luai_numle, 0, TM_LE); - vmbreak; - } - vmcase(OP_GTI) { - op_orderI(L, l_gti, luai_numgt, 1, TM_LT); - vmbreak; - } - vmcase(OP_GEI) { - op_orderI(L, l_gei, luai_numge, 1, TM_LE); - vmbreak; - } - vmcase(OP_TEST) { - int cond = !l_isfalse(s2v(ra)); - docondjump(); - vmbreak; - } - vmcase(OP_TESTSET) { - TValue *rb = vRB(i); - if (l_isfalse(rb) == GETARG_k(i)) - pc++; - else { - setobj2s(L, ra, rb); - donextjump(ci); - } - vmbreak; - } - vmcase(OP_CALL) { - CallInfo *newci; - int b = GETARG_B(i); - int nresults = GETARG_C(i) - 1; - if (b != 0) /* fixed number of arguments? */ - L->top = ra + b; /* top signals number of arguments */ - /* else previous instruction set top */ - savepc(L); /* in case of errors */ - if ((newci = luaD_precall(L, ra, nresults)) == NULL) - updatetrap(ci); /* C call; nothing else to be done */ - else { /* Lua call: run function in this same C frame */ - ci = newci; - goto startfunc; - } - vmbreak; - } - vmcase(OP_TAILCALL) { - int b = GETARG_B(i); /* number of arguments + 1 (function) */ - int n; /* number of results when calling a C function */ - int nparams1 = GETARG_C(i); - /* delta is virtual 'func' - real 'func' (vararg functions) */ - int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; - if (b != 0) - L->top = ra + b; - else /* previous instruction set top */ - b = cast_int(L->top - ra); - savepc(ci); /* several calls here can raise errors */ - if (TESTARG_k(i)) { - luaF_closeupval(L, base); /* close upvalues from current call */ - lua_assert(L->tbclist < base); /* no pending tbc variables */ - lua_assert(base == ci->func + 1); - } - if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ - goto startfunc; /* execute the callee */ - else { /* C function? */ - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_poscall(L, ci, n); /* finish caller */ - updatetrap(ci); /* 'luaD_poscall' can change hooks */ - goto ret; /* caller returns after the tail call */ - } - } - vmcase(OP_RETURN) { - int n = GETARG_B(i) - 1; /* number of results */ - int nparams1 = GETARG_C(i); - if (n < 0) /* not fixed? */ - n = cast_int(L->top - ra); /* get what is available */ - savepc(ci); - if (TESTARG_k(i)) { /* may there be open upvalues? */ - ci->u2.nres = n; /* save number of returns */ - if (L->top < ci->top) - L->top = ci->top; - luaF_close(L, base, CLOSEKTOP, 1); - updatetrap(ci); - updatestack(ci); - } - if (nparams1) /* vararg function? */ - ci->func -= ci->u.l.nextraargs + nparams1; - L->top = ra + n; /* set call for 'luaD_poscall' */ - luaD_poscall(L, ci, n); - updatetrap(ci); /* 'luaD_poscall' can change hooks */ - goto ret; - } - vmcase(OP_RETURN0) { - if (l_unlikely(L->hookmask)) { - L->top = ra; - savepc(ci); - luaD_poscall(L, ci, 0); /* no hurry... */ - trap = 1; - } - else { /* do the 'poscall' here */ - int nres; - L->ci = ci->previous; /* back to caller */ - L->top = base - 1; - for (nres = ci->nresults; l_unlikely(nres > 0); nres--) - setnilvalue(s2v(L->top++)); /* all results are nil */ - } - goto ret; - } - vmcase(OP_RETURN1) { - if (l_unlikely(L->hookmask)) { - L->top = ra + 1; - savepc(ci); - luaD_poscall(L, ci, 1); /* no hurry... */ - trap = 1; - } - else { /* do the 'poscall' here */ - int nres = ci->nresults; - L->ci = ci->previous; /* back to caller */ - if (nres == 0) - L->top = base - 1; /* asked for no results */ - else { - setobjs2s(L, base - 1, ra); /* at least this result */ - L->top = base; - for (; l_unlikely(nres > 1); nres--) - setnilvalue(s2v(L->top++)); /* complete missing results */ - } - } - ret: /* return from a Lua function */ - if (ci->callstatus & CIST_FRESH) - return; /* end this frame */ - else { - ci = ci->previous; - goto returning; /* continue running caller in this frame */ - } - } - vmcase(OP_FORLOOP) { - if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ - lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); - if (count > 0) { /* still more iterations? */ - lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = ivalue(s2v(ra)); /* internal index */ - chgivalue(s2v(ra + 1), count - 1); /* update counter */ - idx = intop(+, idx, step); /* add step to index */ - chgivalue(s2v(ra), idx); /* update internal index */ - setivalue(s2v(ra + 3), idx); /* and control variable */ - pc -= GETARG_Bx(i); /* jump back */ - } - } - else if (floatforloop(ra)) /* float loop */ - pc -= GETARG_Bx(i); /* jump back */ - updatetrap(ci); /* allows a signal to break the loop */ - vmbreak; - } - vmcase(OP_FORPREP) { - savestate(L, ci); /* in case of errors */ - if (forprep(L, ra)) - pc += GETARG_Bx(i) + 1; /* skip the loop */ - vmbreak; - } - vmcase(OP_TFORPREP) { - /* create to-be-closed upvalue (if needed) */ - halfProtect(luaF_newtbcupval(L, ra + 3)); - pc += GETARG_Bx(i); - i = *(pc++); /* go to next instruction */ - lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); - goto l_tforcall; - } - vmcase(OP_TFORCALL) { - l_tforcall: - /* 'ra' has the iterator function, 'ra + 1' has the state, - 'ra + 2' has the control variable, and 'ra + 3' has the - to-be-closed variable. The call will use the stack after - these values (starting at 'ra + 4') - */ - /* push function, state, and control variable */ - memcpy(ra + 4, ra, 3 * sizeof(*ra)); - L->top = ra + 4 + 3; - ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ - updatestack(ci); /* stack may have changed */ - i = *(pc++); /* go to next instruction */ - lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); - goto l_tforloop; - } - vmcase(OP_TFORLOOP) { - l_tforloop: - if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ - setobjs2s(L, ra + 2, ra + 4); /* save control variable */ - pc -= GETARG_Bx(i); /* jump back */ - } - vmbreak; - } - vmcase(OP_SETLIST) { - int n = GETARG_B(i); - unsigned int last = GETARG_C(i); - Table *h = hvalue(s2v(ra)); - if (n == 0) - n = cast_int(L->top - ra) - 1; /* get up to the top */ - else - L->top = ci->top; /* correct top in case of emergency GC */ - last += n; - if (TESTARG_k(i)) { - last += GETARG_Ax(*pc) * (MAXARG_C + 1); - pc++; - } - if (last > luaH_realasize(h)) /* needs more space? */ - luaH_resizearray(L, h, last); /* preallocate it at once */ - for (; n > 0; n--) { - TValue *val = s2v(ra + n); - setobj2t(L, &h->array[last - 1], val); - last--; - luaC_barrierback(L, obj2gco(h), val); - } - vmbreak; - } - vmcase(OP_CLOSURE) { - Proto *p = cl->p->p[GETARG_Bx(i)]; - halfProtect(pushclosure(L, p, cl->upvals, base, ra)); - checkGC(L, ra + 1); - vmbreak; - } - vmcase(OP_VARARG) { - int n = GETARG_C(i) - 1; /* required results */ - Protect(luaT_getvarargs(L, ci, ra, n)); - vmbreak; - } - vmcase(OP_VARARGPREP) { - ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); - if (l_unlikely(trap)) { /* previous "Protect" updated trap */ - luaD_hookcall(L, ci); - L->oldpc = 1; /* next opcode will be seen as a "new" line */ - } - updatebase(ci); /* function has new base after adjustment */ - vmbreak; - } - vmcase(OP_EXTRAARG) { - lua_assert(0); - vmbreak; - } - } - } -} - -/* }================================================================== */ diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.h deleted file mode 100644 index 1bc16f3..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lvm.h +++ /dev/null @@ -1,136 +0,0 @@ -/* -** $Id: lvm.h $ -** Lua virtual machine -** See Copyright Notice in lua.h -*/ - -#ifndef lvm_h -#define lvm_h - - -#include "ldo.h" -#include "lobject.h" -#include "ltm.h" - - -#if !defined(LUA_NOCVTN2S) -#define cvt2str(o) ttisnumber(o) -#else -#define cvt2str(o) 0 /* no conversion from numbers to strings */ -#endif - - -#if !defined(LUA_NOCVTS2N) -#define cvt2num(o) ttisstring(o) -#else -#define cvt2num(o) 0 /* no conversion from strings to numbers */ -#endif - - -/* -** You can define LUA_FLOORN2I if you want to convert floats to integers -** by flooring them (instead of raising an error if they are not -** integral values) -*/ -#if !defined(LUA_FLOORN2I) -#define LUA_FLOORN2I F2Ieq -#endif - - -/* -** Rounding modes for float->integer coercion - */ -typedef enum { - F2Ieq, /* no rounding; accepts only integral values */ - F2Ifloor, /* takes the floor of the number */ - F2Iceil /* takes the ceil of the number */ -} F2Imod; - - -/* convert an object to a float (including string coercion) */ -#define tonumber(o,n) \ - (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) - - -/* convert an object to a float (without string coercion) */ -#define tonumberns(o,n) \ - (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ - (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) - - -/* convert an object to an integer (including string coercion) */ -#define tointeger(o,i) \ - (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ - : luaV_tointeger(o,i,LUA_FLOORN2I)) - - -/* convert an object to an integer (without string coercion) */ -#define tointegerns(o,i) \ - (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ - : luaV_tointegerns(o,i,LUA_FLOORN2I)) - - -#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) - -#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) - - -/* -** fast track for 'gettable': if 't' is a table and 't[k]' is present, -** return 1 with 'slot' pointing to 't[k]' (position of final result). -** Otherwise, return 0 (meaning it will have to check metamethod) -** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL -** (otherwise). 'f' is the raw get function to use. -*/ -#define luaV_fastget(L,t,k,slot,f) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !isempty(slot))) /* result not empty? */ - - -/* -** Special case of 'luaV_fastget' for integers, inlining the fast case -** of 'luaH_getint'. -*/ -#define luaV_fastgeti(L,t,k,slot) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ - ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ - !isempty(slot))) /* result not empty? */ - - -/* -** Finish a fast set operation (when fast get succeeds). In that case, -** 'slot' points to the place to put the value. -*/ -#define luaV_finishfastset(L,t,slot,v) \ - { setobj2t(L, cast(TValue *,slot), v); \ - luaC_barrierback(L, gcvalue(t), v); } - - - - -LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); -LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); -LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); -LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); -LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, - F2Imod mode); -LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); -LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); -LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - TValue *val, const TValue *slot); -LUAI_FUNC void luaV_finishOp (lua_State *L); -LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); -LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); -LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); -LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); -LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); -LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); - -#endif diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.c b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.c deleted file mode 100644 index cd0a02d..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.c +++ /dev/null @@ -1,68 +0,0 @@ -/* -** $Id: lzio.c $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - -#define lzio_c -#define LUA_CORE - -#include "lprefix.h" - - -#include - -#include "lua.h" - -#include "llimits.h" -#include "lmem.h" -#include "lstate.h" -#include "lzio.h" - - -int luaZ_fill (ZIO *z) { - size_t size; - lua_State *L = z->L; - const char *buff; - lua_unlock(L); - buff = z->reader(L, z->data, &size); - lua_lock(L); - if (buff == NULL || size == 0) - return EOZ; - z->n = size - 1; /* discount char being returned */ - z->p = buff; - return cast_uchar(*(z->p++)); -} - - -void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { - z->L = L; - z->reader = reader; - z->data = data; - z->n = 0; - z->p = NULL; -} - - -/* --------------------------------------------------------------- read --- */ -size_t luaZ_read (ZIO *z, void *b, size_t n) { - while (n) { - size_t m; - if (z->n == 0) { /* no bytes in buffer? */ - if (luaZ_fill(z) == EOZ) /* try to read more */ - return n; /* no more input; return number of missing bytes */ - else { - z->n++; /* luaZ_fill consumed first byte; put it back */ - z->p--; - } - } - m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ - memcpy(b, z->p, m); - z->n -= m; - z->p += m; - b = (char *)b + m; - n -= m; - } - return 0; -} - diff --git a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.h b/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.h deleted file mode 100644 index 38f397f..0000000 --- a/LuaBridge3/Tests/Lua/Lua.5.4.4/src/lzio.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -** $Id: lzio.h $ -** Buffered streams -** See Copyright Notice in lua.h -*/ - - -#ifndef lzio_h -#define lzio_h - -#include "lua.h" - -#include "lmem.h" - - -#define EOZ (-1) /* end of stream */ - -typedef struct Zio ZIO; - -#define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) - - -typedef struct Mbuffer { - char *buffer; - size_t n; - size_t buffsize; -} Mbuffer; - -#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) - -#define luaZ_buffer(buff) ((buff)->buffer) -#define luaZ_sizebuffer(buff) ((buff)->buffsize) -#define luaZ_bufflen(buff) ((buff)->n) - -#define luaZ_buffremove(buff,i) ((buff)->n -= (i)) -#define luaZ_resetbuffer(buff) ((buff)->n = 0) - - -#define luaZ_resizebuffer(L, buff, size) \ - ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ - (buff)->buffsize, size), \ - (buff)->buffsize = size) - -#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) - - -LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, - void *data); -LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ - - - -/* --------- Private Part ------------------ */ - -struct Zio { - size_t n; /* bytes still unread */ - const char *p; /* current position in buffer */ - lua_Reader reader; /* reader function */ - void *data; /* additional data */ - lua_State *L; /* Lua state (for reader) */ -}; - - -LUAI_FUNC int luaZ_fill (ZIO *z); - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/.gitignore b/LuaBridge3/Tests/Lua/LuaJIT.2.1/.gitignore deleted file mode 100644 index 2e82193..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/.gitignore +++ /dev/null @@ -1,17 +0,0 @@ -*.[oa] -*.so -*.obj -*.lib -*.exp -*.dll -*.exe -*.manifest -*.dmp -*.swp -.tags -*~ -tags -*.swo -.idea/ -build/ -cmake-*-build/ diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/CMakeLists.txt b/LuaBridge3/Tests/Lua/LuaJIT.2.1/CMakeLists.txt deleted file mode 100644 index 690e50c..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/CMakeLists.txt +++ /dev/null @@ -1,444 +0,0 @@ -# This CMakeLists.txt has been first taken from LuaDist -# Copyright (C) 2007-2011 LuaDist. -# Created by Peter Drahoš -# Redistribution and use of this file is allowed according to the terms of the MIT license. -# Debugged and (now seriously) modified by Ronan Collobert, for Torch7 -# Upgrade to luajit-2.1 by 9chu -# Modified 2022 by kunitoki - -CMAKE_MINIMUM_REQUIRED(VERSION 3.6 FATAL_ERROR) - -project(Lua C ASM) - -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) - -# If you want to include Lua in a main project, -# you might want to define those variables yourself -IF(NOT Lua_IS_SUBPROJECT) - INCLUDE(LuaPaths) -ENDIF() - -INCLUDE(CheckLibraryExists) -INCLUDE(CheckFunctionExists) -INCLUDE(CheckCSourceCompiles) -INCLUDE(CheckTypeSize) -INCLUDE(CheckCCompilerFlag) - -# LuaJIT specific -option ( LUAJIT_DISABLE_FFI "Disable FFI." OFF ) -option ( LUAJIT_ENABLE_LUA52COMPAT "Enable Lua 5.2 compatibility." OFF ) -option ( LUAJIT_DISABLE_JIT "Disable JIT." OFF ) -option ( LUAJIT_CPU_SSE2 "Use SSE2 instead of x87 instructions." ON ) -option ( LUAJIT_CPU_NOCMOV "Disable NOCMOV." OFF ) -option ( LUAJIT_DISABLE_GC64 "Disable LJ_GC64 mode for x64." OFF ) - -# Debug purpose -option ( LUAJIT_USE_SYSMALLOC "Use the system provided memory allocator (realloc)." OFF ) -option ( LUAJIT_USE_VALGRIND "This define is required to run LuaJIT under Valgrind." OFF ) -option ( LUAJIT_USE_GDBJIT "This is the client for the GDB JIT API." OFF ) -option ( LUA_USE_APICHECK "Turn on assertions for the Lua/C API to debug problems with lua_* calls." OFF ) -option ( LUA_USE_ASSERT "Turn on assertions for the whole LuaJIT VM." OFF ) -MARK_AS_ADVANCED(LUAJIT_DISABLE_FFI LUAJIT_ENABLE_LUA52COMPAT LUAJIT_DISABLE_JIT LUAJIT_CPU_SSE2 LUAJIT_CPU_NOCMOV LUAJIT_RUN_DYNASM LUAJIT_DISABLE_GC64 - LUAJIT_USE_SYSMALLOC LUAJIT_USE_VALGRIND LUAJIT_USE_GDBJIT LUA_USE_APICHECK LUA_USE_ASSERT ) - -CHECK_TYPE_SIZE("void*" SIZEOF_VOID_P) -IF(SIZEOF_VOID_P EQUAL 8) - ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE) -ENDIF() - -IF(NOT WIN32) - FIND_LIBRARY(DL_LIBRARY "dl") - IF(DL_LIBRARY) - SET(CMAKE_REQUIRED_LIBRARIES ${DL_LIBRARY}) - LIST(APPEND LIBS ${DL_LIBRARY}) - ENDIF(DL_LIBRARY) - CHECK_FUNCTION_EXISTS(dlopen LUA_USE_DLOPEN) - IF(NOT LUA_USE_DLOPEN) - MESSAGE(FATAL_ERROR "Cannot compile a useful lua. -Function dlopen() seems not to be supported on your platform. -Apparently you are not on a Windows platform as well. -So lua has no way to deal with shared libraries!") - ENDIF(NOT LUA_USE_DLOPEN) -ENDIF(NOT WIN32) - -CHECK_LIBRARY_EXISTS(m sin "" LUA_USE_LIBM) -if ( LUA_USE_LIBM ) - list ( APPEND LIBS m ) -endif () - -#FIND_PACKAGE(Readline) -#IF(READLINE_FOUND) -# INCLUDE_DIRECTORIES(${READLINE_INCLUDE_DIR}) -# list(APPEND LIBS ${READLINE_LIBRARIES}) -# SET(LUAJIT_USE_READLINE 1) -#ENDIF(READLINE_FOUND) -SET(LUAJIT_USE_READLINE 0) - -IF (CMAKE_SHARED_LIBRARY_SUFFIX STREQUAL CMAKE_SHARED_MODULE_SUFFIX) - SET(LUA_USE_MODULE_AND_LIBRARY 0) -ELSE (CMAKE_SHARED_LIBRARY_SUFFIX STREQUAL CMAKE_SHARED_MODULE_SUFFIX) - SET(LUA_USE_MODULE_AND_LIBRARY 1) -ENDIF (CMAKE_SHARED_LIBRARY_SUFFIX STREQUAL CMAKE_SHARED_MODULE_SUFFIX) - -## SOURCES -INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/luaconf.h src/lua.h src/lauxlib.h src/lualib.h src/lua.hpp src/luajit.h - DESTINATION "${Lua_INSTALL_INCLUDE_SUBDIR}") - -MACRO(LJ_TEST_ARCH stuff) - CHECK_C_SOURCE_COMPILES(" -#undef ${stuff} -#include \"lj_arch.h\" -#if ${stuff} -int main() { return 0; } -#else -#error \"not defined\" -#endif -" ${stuff}) -ENDMACRO() - -MACRO(LJ_TEST_ARCH_VALUE stuff value) - CHECK_C_SOURCE_COMPILES(" -#undef ${stuff} -#include \"lj_arch.h\" -#if ${stuff} == ${value} -int main() { return 0; } -#else -#error \"not defined\" -#endif -" ${stuff}_${value}) -ENDMACRO() - -SET(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src) -FOREACH(arch X64 X86 ARM ARM64 PPC PPCSPE MIPS MIPS64 S390X) - LJ_TEST_ARCH(LJ_TARGET_${arch}) - if(LJ_TARGET_${arch}) - STRING(TOLOWER ${arch} TARGET_LJARCH) - MESSAGE(STATUS "LuaJIT Target ${TARGET_LJARCH}") - BREAK() - ENDIF() -ENDFOREACH() - -IF(NOT TARGET_LJARCH) - MESSAGE(FATAL_ERROR "architecture not supported") -ENDIF() - -SET(DASM_ARCH ${TARGET_LJARCH}) -SET(DASM_FLAGS) -SET(TARGET_ARCH) -LIST(APPEND TARGET_ARCH "LUAJIT_TARGET=LUAJIT_ARCH_${TARGET_LJARCH}") - -LJ_TEST_ARCH(LJ_TARGET_ARM64) -IF(LJ_TARGET_ARM64) - LJ_TEST_ARCH(__AARCH64EB__) - IF(__AARCH64EB__) - LIST(APPEND TARGET_ARCH "__AARCH64EB__=1") - ENDIF() -ENDIF() - -LJ_TEST_ARCH(LJ_TARGET_PPC) -IF(LJ_TARGET_PPC) - LJ_TEST_ARCH_VALUE(LJ_LE 1) - IF(LJ_LE_1) - LIST(APPEND TARGET_ARCH "LJ_ARCH_ENDIAN=LUAJIT_LE") - ELSE() - LIST(APPEND TARGET_ARCH "LJ_ARCH_ENDIAN=LUAJIT_BE") - ENDIF() -ENDIF() - -LJ_TEST_ARCH(LJ_TARGET_MIPS) -IF(LJ_TARGET_MIPS) - LJ_TEST_ARCH(MIPSEL) - IF(NOT MIPSEL) - LIST(APPEND TARGET_ARCH "__MIPSEL__=1") - ENDIF() -ENDIF() - -LJ_TEST_ARCH(LJ_TARGET_PS3) -IF(LJ_TARGET_PS3) - LIST(APPEND TARGET_ARCH "__CELLOS_LV2__=1") - set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLUAJIT_USE_SYSMALLOC" ) -ENDIF() - -LJ_TEST_ARCH_VALUE(LJ_LE 1) -IF(LJ_LE_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D ENDIAN_LE) -ELSE() - SET(DASM_FLAGS ${DASM_FLAGS} -D ENDIAN_BE) -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_ARCH_BITS 64) -IF(LJ_ARCH_BITS_64) - SET(DASM_FLAGS ${DASM_FLAGS} -D P64) -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_HASJIT 1) -IF(LJ_HASJIT_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D JIT) -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_HASFFI 1) -IF(LJ_HASFFI_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D FFI) -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_DUALNUM 1) -IF(LJ_DUALNUM_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D DUALNUM) -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_ARCH_HASFPU 1) -IF(LJ_ARCH_HASFPU_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D HASFPU) - LIST(APPEND TARGET_ARCH "LJ_ARCH_HASFPU=1") -ELSE() - LIST(APPEND TARGET_ARCH "LJ_ARCH_HASFPU=0") -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_ABI_SOFTFP 1) -IF(NOT LJ_ABI_SOFTFP_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D HFABI) - LIST(APPEND TARGET_ARCH "LJ_ABI_SOFTFP=0") -ELSE() - LIST(APPEND TARGET_ARCH "LJ_ABI_SOFTFP=1") -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_NO_UNWIND 1) -IF(LJ_NO_UNWIND_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D NO_UNWIND) - LIST(APPEND TARGET_ARCH "LUAJIT_NO_UNWIND=1") -ENDIF() -IF(WIN32) - SET(DASM_FLAGS ${DASM_FLAGS} -D WIN) -ENDIF() - -#IF(TARGET_LJARCH STREQUAL "x86") -# LJ_TEST_ARCH_VALUE(__SSE2__ 1) -# IF(__SSE2__1) -# SET(DASM_FLAGS ${DASM_FLAGS} -D SSE) -# ENDIF() -#ENDIF() -IF(TARGET_LJARCH STREQUAL "x64") - LJ_TEST_ARCH_VALUE(LJ_FR2 1) - IF (NOT LJ_FR2_1) - SET(DASM_ARCH "x86") - ENDIF () -ENDIF() -IF(TARGET_LJARCH STREQUAL "arm") - if(${CMAKE_SYSTEM_NAME} MATCHES "iOS") - SET(DASM_AFLAGS ${DASM_AFLAGS} -D IOS) - ENDIF() -ENDIF() -LJ_TEST_ARCH_VALUE(LJ_TARGET_MIPSR6 1) -IF(LJ_TARGET_MIPSR6_1) - SET(DASM_AFLAGS ${DASM_AFLAGS} -D MIPSR6) -ENDIF() -IF(TARGET_LJARCH STREQUAL "ppc") - LJ_TEST_ARCH_VALUE(LJ_ARCH_SQRT 1) - IF(LJ_ARCH_SQRT_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D SQRT) - ENDIF() - LJ_TEST_ARCH_VALUE(LJ_ARCH_ROUND 1) - IF(LJ_ARCH_ROUND_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D ROUND) - ENDIF() - LJ_TEST_ARCH_VALUE(LJ_ARCH_PPC32ON64 1) - IF(LJ_ARCH_PPC32ON64_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D GPR64) - ENDIF() - if(LJ_TARGET_PS3) - SET(DASM_FLAGS ${DASM_FLAGS} -D PPE) - ENDIF() - LJ_TEST_ARCH_VALUE(LJ_ARCH_PPC_OPD 1) - IF(LJ_ARCH_PPC_OPD_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D OPD) - ENDIF() - LJ_TEST_ARCH_VALUE(LJ_ARCH_PPC_OPDENV 1) - IF(LJ_ARCH_PPC_OPDENV_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D OPDENV) - ENDIF() - LJ_TEST_ARCH_VALUE(LJ_ARCH_PPC_ELFV2 1) - IF(LJ_ARCH_PPC_ELFV2_1) - SET(DASM_FLAGS ${DASM_FLAGS} -D ELFV2) - ENDIF() -ENDIF() -IF(TARGET_LJARCH STREQUAL "s390x") # untested - SET(DASM_ARCH "s390x") -ENDIF() - -check_c_compiler_flag("-fno-stack-protector" HAS_NO_STACK_PROTECTOR) -IF(HAS_NO_STACK_PROTECTOR) - set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-stack-protector") -ENDIF() - -check_c_compiler_flag("-fomit-frame-pointer" HAS_OMIT_FRAME_POINTER) -IF(HAS_NO_STACK_PROTECTOR) - set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer") -ENDIF() - -check_c_compiler_flag("-fPIC" HAS_POSITION_INDEPENDENT_CODE) -IF(HAS_POSITION_INDEPENDENT_CODE) - set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") -ENDIF() - -if ( WIN32 ) - add_definitions ( -DLUAJIT_OS=LUAJIT_OS_WINDOWS ) - IF(NOT MSVC) - set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -malign-double" ) - ENDIF() - set ( LJVM_MODE peobj ) -elseif ( APPLE ) - set ( LJVM_MODE machasm ) - add_definitions ( -DLUAJIT_OS=LUAJIT_OS_OSX ) - if ( ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" ) - add_definitions ( -DLUAJIT_UNWIND_EXTERNAL ) - endif () - if ( ${CMAKE_SYSTEM_NAME} MATCHES "iOS" ) # untested - add_definitions ( -DTARGET_OS_IPHONE=1 ) - IF (TARGET_LJARCH STREQUAL "arm64") - set ( CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-fno-omit-frame-pointer" ) - endif () - else () - add_definitions ( -DTARGET_OS_IPHONE=0 ) - endif () -else () - if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") - add_definitions ( -DLUAJIT_OS=LUAJIT_OS_LINUX ) - else() - add_definitions ( -DLUAJIT_OS=LUAJIT_OS_OTHER ) - endif () - - if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS") - set ( LJVM_MODE static ) - elseif (LJ_TARGET_PS3) - set ( LJVM_MODE static ) - else() - set ( LJVM_MODE elfasm ) - endif() - - if (NOT LJ_NO_UNWIND_1) - # Find out whether the target toolchain always generates unwind tables. - execute_process(COMMAND bash "-c" - "exec 2>/dev/null; echo 'extern void b(void);int a(void){b();return 0;}' | ${CMAKE_C_COMPILER} -c -x c - -o tmpunwind.o && { grep -qa -e eh_frame -e __unwind_info tmpunwind.o || grep -qU -e eh_frame -e __unwind_info tmpunwind.o; } && echo E" - OUTPUT_VARIABLE TARGET_TESTUNWIND) - IF (${TARGET_TESTUNWIND} MATCHES "E") - message(STATUS "Use -DLUAJIT_UNWIND_EXTERNAL") - add_definitions ( -DLUAJIT_UNWIND_EXTERNAL ) - endif () - endif () -endif () - -# lj_str_hash requirement -LJ_TEST_ARCH_VALUE(LJ_HAS_OPTIMISED_HASH 1) -IF (TARGET_LJARCH STREQUAL "x64" AND LJ_HAS_OPTIMISED_HASH_1) - set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2") -endif () - -add_executable(minilua src/host/minilua.c) -SET_TARGET_PROPERTIES(minilua PROPERTIES COMPILE_DEFINITIONS "${TARGET_ARCH}") -CHECK_LIBRARY_EXISTS(m pow "" MINILUA_USE_LIBM) -if(MINILUA_USE_LIBM) - message(STATUS "Use libm to build minilua") - TARGET_LINK_LIBRARIES(minilua m) -endif() - -add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/buildvm_arch.h - COMMAND minilua ${CMAKE_CURRENT_SOURCE_DIR}/dynasm/dynasm.lua ${DASM_FLAGS} -o ${CMAKE_CURRENT_BINARY_DIR}/buildvm_arch.h ${CMAKE_CURRENT_SOURCE_DIR}/src/vm_${DASM_ARCH}.dasc - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/dynasm/dynasm.lua minilua -) - -SET(SRC_LJLIB src/lib_base.c src/lib_math.c src/lib_bit.c src/lib_string.c src/lib_table.c - src/lib_io.c src/lib_os.c src/lib_package.c src/lib_debug.c src/lib_jit.c src/lib_ffi.c src/lib_buffer.c) - -SET(SRC_LJCORE src/lj_gc.c src/lj_err.c src/lj_char.c src/lj_bc.c src/lj_obj.c src/lj_buf.c - src/lj_str.c src/lj_tab.c src/lj_func.c src/lj_udata.c src/lj_meta.c src/lj_debug.c - src/lj_state.c src/lj_dispatch.c src/lj_vmevent.c src/lj_vmmath.c src/lj_strscan.c src/lj_strfmt.c src/lj_strfmt_num.c - src/lj_api.c src/lj_profile.c src/lj_lex.c src/lj_parse.c src/lj_bcread.c src/lj_bcwrite.c src/lj_load.c - src/lj_ir.c src/lj_opt_mem.c src/lj_opt_fold.c src/lj_opt_narrow.c - src/lj_opt_dce.c src/lj_opt_loop.c src/lj_opt_split.c src/lj_opt_sink.c - src/lj_mcode.c src/lj_snap.c src/lj_record.c src/lj_crecord.c src/lj_ffrecord.c - src/lj_asm.c src/lj_trace.c src/lj_gdbjit.c - src/lj_ctype.c src/lj_cdata.c src/lj_cconv.c src/lj_ccall.c src/lj_ccallback.c - src/lj_carith.c src/lj_clib.c src/lj_cparse.c - src/lj_lib.c src/lj_alloc.c src/lib_aux.c - src/lj_prng.c src/lj_serialize.c - ${SRC_LJLIB} src/lib_init.c) - -SET(SRC_BUILDVM src/host/buildvm.c src/host/buildvm_asm.c -src/host/buildvm_peobj.c src/host/buildvm_lib.c src/host/buildvm_fold.c -${CMAKE_CURRENT_BINARY_DIR}/buildvm_arch.h) - -## GENERATE -ADD_EXECUTABLE(buildvm ${SRC_BUILDVM}) -SET_TARGET_PROPERTIES(buildvm PROPERTIES COMPILE_DEFINITIONS "${TARGET_ARCH}") - -macro(add_buildvm_target _target _mode) - add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_target} - COMMAND buildvm ARGS -m ${_mode} -o ${CMAKE_CURRENT_BINARY_DIR}/${_target} ${ARGN} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS buildvm ${ARGN} - ) -endmacro(add_buildvm_target) - -if (MSVC) - add_buildvm_target ( lj_vm.obj peobj ) - set (LJ_VM_SRC ${CMAKE_CURRENT_BINARY_DIR}/lj_vm.obj) -else () - add_buildvm_target ( lj_vm.s ${LJVM_MODE} ) - set (LJ_VM_SRC ${CMAKE_CURRENT_BINARY_DIR}/lj_vm.s) -endif () -add_buildvm_target ( lj_ffdef.h ffdef ${SRC_LJLIB} ) -add_buildvm_target ( lj_bcdef.h bcdef ${SRC_LJLIB} ) -add_buildvm_target ( lj_folddef.h folddef src/lj_opt_fold.c ) -add_buildvm_target ( lj_recdef.h recdef ${SRC_LJLIB} ) -add_buildvm_target ( lj_libdef.h libdef ${SRC_LJLIB} ) -add_buildvm_target ( vmdef.lua vmdef ${SRC_LJLIB} ) - -SET(DEPS - ${LJ_VM_SRC} - ${CMAKE_CURRENT_BINARY_DIR}/lj_ffdef.h - ${CMAKE_CURRENT_BINARY_DIR}/lj_bcdef.h - ${CMAKE_CURRENT_BINARY_DIR}/lj_libdef.h - ${CMAKE_CURRENT_BINARY_DIR}/lj_recdef.h - ${CMAKE_CURRENT_BINARY_DIR}/lj_folddef.h - ${CMAKE_CURRENT_BINARY_DIR}/vmdef.lua - ) - -## COMPILE -include_directories (BEFORE ${CMAKE_CURRENT_BINARY_DIR} dynasm src ) - -#add_library ( liblua-shared SHARED ${SRC_LJCORE} ${DEPS} ) -#target_link_libraries ( liblua-shared ${LIBS} ) -#target_include_directories( liblua-shared INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src ) -#SET_TARGET_PROPERTIES(liblua-shared PROPERTIES -# PREFIX "lib" IMPORT_PREFIX "lib" OUTPUT_NAME "lua") -#if ( WIN32 AND NOT CYGWIN ) -# target_compile_definitions ( liblua-shared PRIVATE -DLUA_BUILD_AS_DLL ) -#endif () - -add_library ( liblua-static STATIC ${SRC_LJCORE} ${DEPS} ) -target_link_libraries ( liblua-static ${LIBS} ) -target_include_directories( liblua-static INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src ) -SET_TARGET_PROPERTIES(liblua-static PROPERTIES - COMPILE_DEFINITIONS "liblua_STATIC" - OUTPUT_NAME "lua-static") - -#add_executable ( lua src/luajit.c) -#target_link_libraries ( lua liblua-shared ) - -# add_executable ( lua-static src/luajit.c) -# target_link_libraries ( lua-static liblua-static ) - -#INSTALL(TARGETS liblua-shared lua -# EXPORT torch-exports -# RUNTIME DESTINATION "${Lua_INSTALL_BIN_SUBDIR}" -# LIBRARY DESTINATION "${Lua_INSTALL_LIB_SUBDIR}" -# ARCHIVE DESTINATION "${Lua_INSTALL_LIB_SUBDIR}") - -# Create internal FindLua.cmake -#SET(LUA_LIBRARIES liblua-shared) -SET(LUA_EXECUTABLE lua-static) -SET(LUAC_EXECUTABLE torch-luac) -SET(LUA_INCLUDE_DIR - ${CMAKE_CURRENT_SOURCE_DIR}/src - ${CMAKE_CURRENT_BINARY_DIR} - ${READLINE_INCLUDE_DIR}) -#CONFIGURE_FILE(cmake/LuaConfig.cmake.in "${Lua_INSTALL_FINDLUA_DIR}/FindLua.cmake") - -INSTALL(FILES - src/jit/bc.lua src/jit/v.lua src/jit/dump.lua src/jit/dis_x86.lua src/jit/dis_x64.lua src/jit/dis_arm.lua - src/jit/dis_ppc.lua src/jit/dis_mips.lua src/jit/dis_mipsel.lua src/jit/bcsave.lua ${CMAKE_CURRENT_BINARY_DIR}/vmdef.lua - src/jit/p.lua src/jit/zone.lua src/jit/dis_arm64.lua src/jit/dis_arm64be.lua src/jit/dis_mips64.lua src/jit/dis_mips64el.lua - DESTINATION "${Lua_INSTALL_LUA_PATH_SUBDIR}/jit") diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/COPYRIGHT b/LuaBridge3/Tests/Lua/LuaJIT.2.1/COPYRIGHT deleted file mode 100644 index c74216c..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/COPYRIGHT +++ /dev/null @@ -1,56 +0,0 @@ -=============================================================================== -LuaJIT -- a Just-In-Time Compiler for Lua. https://luajit.org/ - -Copyright (C) 2005-2022 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -[ MIT license: https://www.opensource.org/licenses/mit-license.php ] - -=============================================================================== -[ LuaJIT includes code from Lua 5.1/5.2, which has this license statement: ] - -Copyright (C) 1994-2012 Lua.org, PUC-Rio. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=============================================================================== -[ LuaJIT includes code from dlmalloc, which has this license statement: ] - -This is a version (aka dlmalloc) of malloc/free/realloc written by -Doug Lea and released to the public domain, as explained at -https://creativecommons.org/licenses/publicdomain - -=============================================================================== diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/README b/LuaBridge3/Tests/Lua/LuaJIT.2.1/README deleted file mode 100644 index 1faef25..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/README +++ /dev/null @@ -1,16 +0,0 @@ -README for LuaJIT 2.1.0-beta3 ------------------------------ - -LuaJIT is a Just-In-Time (JIT) compiler for the Lua programming language. - -Project Homepage: https://luajit.org/ - -LuaJIT is Copyright (C) 2005-2022 Mike Pall. -LuaJIT is free software, released under the MIT license. -See full Copyright Notice in the COPYRIGHT file or in luajit.h. - -Documentation for LuaJIT is available in HTML format. -Please point your favorite browser to: - - doc/luajit.html - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/FindReadline.cmake b/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/FindReadline.cmake deleted file mode 100644 index 95bb942..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/FindReadline.cmake +++ /dev/null @@ -1,80 +0,0 @@ -# - Find the readline library -# This module defines -# READLINE_INCLUDE_DIR, path to readline/readline.h, etc. -# READLINE_LIBRARIES, the libraries required to use READLINE. -# READLINE_FOUND, If false, do not try to use READLINE. -# also defined, but not for general use are -# READLINE_readline_LIBRARY, where to find the READLINE library. -# READLINE_ncurses_LIBRARY, where to find the ncurses library [might not be defined] - -# Apple readline does not support readline hooks -# So we look for another one by default -IF(APPLE) - FIND_PATH(READLINE_INCLUDE_DIR NAMES readline/readline.h PATHS - /sw/include - /opt/local/include - /opt/include - /usr/local/include - /usr/include/ - NO_DEFAULT_PATH - ) -ENDIF(APPLE) -FIND_PATH(READLINE_INCLUDE_DIR NAMES readline/readline.h) - - -# Apple readline does not support readline hooks -# So we look for another one by default -IF(APPLE) - FIND_LIBRARY(READLINE_readline_LIBRARY NAMES readline PATHS - /sw/lib - /opt/local/lib - /opt/lib - /usr/local/lib - /usr/lib - NO_DEFAULT_PATH - ) -ENDIF(APPLE) -FIND_LIBRARY(READLINE_readline_LIBRARY NAMES readline) - -# Sometimes readline really needs ncurses -IF(APPLE) - FIND_LIBRARY(READLINE_ncurses_LIBRARY NAMES ncurses PATHS - /sw/lib - /opt/local/lib - /opt/lib - /usr/local/lib - /usr/lib - NO_DEFAULT_PATH - ) -ENDIF(APPLE) -FIND_LIBRARY(READLINE_ncurses_LIBRARY NAMES ncurses) - -MARK_AS_ADVANCED( - READLINE_INCLUDE_DIR - READLINE_readline_LIBRARY - READLINE_ncurses_LIBRARY - ) - -SET( READLINE_FOUND "NO" ) -IF(READLINE_INCLUDE_DIR) - IF(READLINE_readline_LIBRARY) - SET( READLINE_FOUND "YES" ) - SET( READLINE_LIBRARIES - ${READLINE_readline_LIBRARY} - ) - - # some readline libraries depend on ncurses - IF(READLINE_ncurses_LIBRARY) - SET(READLINE_LIBRARIES ${READLINE_LIBRARIES} ${READLINE_ncurses_LIBRARY}) - ENDIF(READLINE_ncurses_LIBRARY) - - ENDIF(READLINE_readline_LIBRARY) -ENDIF(READLINE_INCLUDE_DIR) - -IF(READLINE_FOUND) - MESSAGE(STATUS "Found readline library") -ELSE(READLINE_FOUND) - IF(READLINE_FIND_REQUIRED) - MESSAGE(FATAL_ERROR "Could not find readline -- please give some paths to CMake") - ENDIF(READLINE_FIND_REQUIRED) -ENDIF(READLINE_FOUND) diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaConfig.cmake.in b/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaConfig.cmake.in deleted file mode 100644 index ca6b346..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaConfig.cmake.in +++ /dev/null @@ -1,9 +0,0 @@ -# Find the Lua includes and library -# - -SET(LUA_FOUND 1) -SET(LUA_JIT 1) -SET(LUA_COMPILED_WITH_CXX 1) -SET(LUA_INCLUDE_DIR "@LUA_INCLUDE_DIR@") -SET(LUA_LIBRARIES "@LUA_LIBRARIES@") -SET(LUA_EXECUTABLE "@LUA_EXECUTABLE@") diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaPaths.cmake b/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaPaths.cmake deleted file mode 100644 index 1ba435c..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/cmake/LuaPaths.cmake +++ /dev/null @@ -1,63 +0,0 @@ -SET(Lua_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/install") - -SET(Lua_INSTALL_BIN_SUBDIR "bin" CACHE PATH - "Install dir for binaries (relative to Lua_INSTALL_PREFIX)") - -SET(Lua_INSTALL_LIB_SUBDIR "lib" CACHE PATH - "Install dir for archives (relative to Lua_INSTALL_PREFIX)") - -SET(Lua_INSTALL_INCLUDE_SUBDIR "include" CACHE PATH - "Install dir for include (relative to Lua_INSTALL_PREFIX)") - -SET(Lua_INSTALL_CMAKE_SUBDIR "share/lua/cmake" CACHE PATH - "Install dir for .cmake files (relative to Lua_INSTALL_PREFIX)") - -SET(Lua_INSTALL_LUA_PATH_SUBDIR "share/lua/5.1" CACHE PATH - "Install dir for Lua packages files (relative to Lua_INSTALL_PREFIX)") - -SET(Lua_INSTALL_LUA_CPATH_SUBDIR "lib/lua/5.1" CACHE PATH - "Install dir for Lua C packages files (relative to Lua_INSTALL_PREFIX)") - -SET(Lua_INSTALL_FINDLUA_DIR "${Lua_BINARY_DIR}/cmake") -SET(Lua_INSTALL_BIN "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_BIN_SUBDIR}") -SET(Lua_INSTALL_LIB "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_LIB_SUBDIR}") -SET(Lua_INSTALL_INCLUDE "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_INCLUDE_SUBDIR}") -SET(Lua_INSTALL_CMAKE "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_CMAKE_SUBDIR}") -SET(Lua_INSTALL_LUA_PATH "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_LUA_PATH_SUBDIR}") -SET(Lua_INSTALL_LUA_CPATH "${Lua_INSTALL_PREFIX}/${Lua_INSTALL_LUA_CPATH_SUBDIR}") - -# reverse relative path to prefix (ridbus is the palindrom of subdir) -FILE(RELATIVE_PATH Lua_INSTALL_BIN_RIDBUS "${Lua_INSTALL_BIN}" "${Lua_INSTALL_PREFIX}/.") -FILE(RELATIVE_PATH Lua_INSTALL_CMAKE_RIDBUS "${Lua_INSTALL_CMAKE}" "${Lua_INSTALL_PREFIX}/.") -GET_FILENAME_COMPONENT(Lua_INSTALL_BIN_RIDBUS "${Lua_INSTALL_BIN_RIDBUS}" PATH) -GET_FILENAME_COMPONENT(Lua_INSTALL_CMAKE_RIDBUS "${Lua_INSTALL_CMAKE_RIDBUS}" PATH) - -IF(UNIX) - OPTION(Lua_BUILD_WITH_RPATH "Build libraries with rpaths" ON) - - IF(Lua_BUILD_WITH_RPATH) - SET(CMAKE_SKIP_BUILD_RPATH FALSE) - SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) - SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) - FILE(RELATIVE_PATH Lua_INSTALL_BIN2LIB - "${Lua_INSTALL_BIN}" "${Lua_INSTALL_LIB}") - FILE(RELATIVE_PATH Lua_INSTALL_BIN2CPATH - "${Lua_INSTALL_BIN}" "${Lua_INSTALL_LUA_CPATH}") - IF(NOT APPLE) - OPTION(WITH_DYNAMIC_RPATH - "Build libraries with executable relative rpaths (\$ORIGIN)" ON ) - ENDIF(NOT APPLE) - IF (WITH_DYNAMIC_RPATH OR APPLE) - SET(CMAKE_INSTALL_RPATH "\$ORIGIN/${Lua_INSTALL_BIN2LIB}") - ELSE (WITH_DYNAMIC_RPATH OR APPLE) - SET(CMAKE_INSTALL_RPATH "${Lua_INSTALL_LIB}") - ENDIF (WITH_DYNAMIC_RPATH OR APPLE) - SET(CMAKE_INSTALL_NAME_DIR "@executable_path/${Lua_INSTALL_BIN2LIB}") - ENDIF(Lua_BUILD_WITH_RPATH) - -ENDIF(UNIX) - -IF (WIN32) - SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") - SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") -ENDIF (WIN32) diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad-print.css b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad-print.css deleted file mode 100644 index a49d309..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad-print.css +++ /dev/null @@ -1,166 +0,0 @@ -/* Copyright (C) 2004-2022 Mike Pall. - * - * You are welcome to use the general ideas of this design for your own sites. - * But please do not steal the stylesheet, the layout or the color scheme. - */ -body { - font-family: serif; - font-size: 11pt; - margin: 0 3em; - padding: 0; - border: none; -} -a:link, a:visited, a:hover, a:active { - text-decoration: none; - background: transparent; - color: #0000ff; -} -h1, h2, h3 { - font-family: sans-serif; - font-weight: bold; - text-align: left; - margin: 0.5em 0; - padding: 0; -} -h1 { - font-size: 200%; -} -h2 { - font-size: 150%; -} -h3 { - font-size: 125%; -} -p { - margin: 0 0 0.5em 0; - padding: 0; -} -ul, ol { - margin: 0.5em 0; - padding: 0 0 0 2em; -} -ul { - list-style: outside square; -} -ol { - list-style: outside decimal; -} -li { - margin: 0; - padding: 0; -} -dl { - margin: 1em 0; - padding: 1em; - border: 1px solid black; -} -dt { - font-weight: bold; - margin: 0; - padding: 0; -} -dt sup { - float: right; - margin-left: 1em; -} -dd { - margin: 0.5em 0 0 2em; - padding: 0; -} -table { - table-layout: fixed; - width: 100%; - margin: 1em 0; - padding: 0; - border: 1px solid black; - border-spacing: 0; - border-collapse: collapse; -} -tr { - margin: 0; - padding: 0; - border: none; -} -td { - text-align: left; - margin: 0; - padding: 0.2em 0.5em; - border-top: 1px solid black; - border-bottom: 1px solid black; -} -tr.separate td { - border-top: double; -} -tt, pre, code, kbd, samp { - font-family: monospace; - font-size: 75%; -} -kbd { - font-weight: bolder; -} -blockquote, pre { - margin: 1em 2em; - padding: 0; -} -img { - border: none; - vertical-align: baseline; - margin: 0; - padding: 0; -} -img.left { - float: left; - margin: 0.5em 1em 0.5em 0; -} -img.right { - float: right; - margin: 0.5em 0 0.5em 1em; -} -.flush { - clear: both; - visibility: hidden; -} -.hide, .noprint, #nav { - display: none !important; -} -.pagebreak { - page-break-before: always; -} -#site { - text-align: right; - font-family: sans-serif; - font-weight: bold; - margin: 0 1em; - border-bottom: 1pt solid black; -} -#site a { - font-size: 1.2em; -} -#site a:link, #site a:visited { - text-decoration: none; - font-weight: bold; - background: transparent; - color: #ffffff; -} -#logo { - color: #ff8000; -} -#head { - clear: both; - margin: 0 1em; -} -#main { - line-height: 1.3; - text-align: justify; - margin: 1em; -} -#foot { - clear: both; - font-size: 80%; - text-align: center; - margin: 0 1.25em; - padding: 0.5em 0 0 0; - border-top: 1pt solid black; - page-break-before: avoid; - page-break-after: avoid; -} diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad.css b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad.css deleted file mode 100644 index 4c1a908..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/bluequad.css +++ /dev/null @@ -1,325 +0,0 @@ -/* Copyright (C) 2004-2022 Mike Pall. - * - * You are welcome to use the general ideas of this design for your own sites. - * But please do not steal the stylesheet, the layout or the color scheme. - */ -/* colorscheme: - * - * site | head #4162bf/white | #6078bf/#e6ecff - * ------+------ ----------------+------------------- - * nav | main #bfcfff | #e6ecff/black - * - * nav: hiback loback #c5d5ff #b9c9f9 - * hiborder loborder #e6ecff #97a7d7 - * link hover #2142bf #ff0000 - * - * link: link visited hover #2142bf #8122bf #ff0000 - * - * main: boxback boxborder #f0f4ff #bfcfff - */ -body { - font-family: Verdana, Arial, Helvetica, sans-serif; - font-size: 10pt; - margin: 0; - padding: 0; - border: none; - background: #e0e0e0; - color: #000000; -} -a:link { - text-decoration: none; - background: transparent; - color: #2142bf; -} -a:visited { - text-decoration: none; - background: transparent; - color: #8122bf; -} -a:hover, a:active { - text-decoration: underline; - background: transparent; - color: #ff0000; -} -h1, h2, h3 { - font-weight: bold; - text-align: left; - margin: 0.5em 0; - padding: 0; - background: transparent; -} -h1 { - font-size: 200%; - line-height: 3em; /* really 6em relative to body, match #site span */ - margin: 0; -} -h2 { - font-size: 150%; - color: #606060; -} -h3 { - font-size: 125%; - color: #404040; -} -p { - max-width: 600px; - margin: 0 0 0.5em 0; - padding: 0; -} -b { - color: #404040; -} -ul, ol { - max-width: 600px; - margin: 0.5em 0; - padding: 0 0 0 2em; -} -ul { - list-style: outside square; -} -ol { - list-style: outside decimal; -} -li { - margin: 0; - padding: 0; -} -dl { - max-width: 600px; - margin: 1em 0; - padding: 1em; - border: 1px solid #bfcfff; - background: #f0f4ff; -} -dt { - font-weight: bold; - margin: 0; - padding: 0; -} -dt sup { - float: right; - margin-left: 1em; - color: #808080; -} -dt a:visited { - text-decoration: none; - color: #2142bf; -} -dt a:hover, dt a:active { - text-decoration: none; - color: #ff0000; -} -dd { - margin: 0.5em 0 0 2em; - padding: 0; -} -div.tablewrap { /* for IE *sigh* */ - max-width: 600px; -} -table { - table-layout: fixed; - border-spacing: 0; - border-collapse: collapse; - max-width: 600px; - width: 100%; - margin: 1em 0; - padding: 0; - border: 1px solid #bfcfff; -} -tr { - margin: 0; - padding: 0; - border: none; -} -tr.odd { - background: #f0f4ff; -} -tr.separate td { - border-top: 1px solid #bfcfff; -} -td { - text-align: left; - margin: 0; - padding: 0.2em 0.5em; - border: none; -} -tt, code, kbd, samp { - font-family: Courier New, Courier, monospace; - line-height: 1.2; - font-size: 110%; -} -kbd { - font-weight: bolder; -} -blockquote, pre { - max-width: 600px; - margin: 1em 2em; - padding: 0; -} -pre { - line-height: 1.1; -} -pre.code { - line-height: 1.4; - margin: 0.5em 0 1em 0.5em; - padding: 0.5em 1em; - border: 1px solid #bfcfff; - background: #f0f4ff; -} -pre.mark { - padding-left: 2em; -} -span.codemark { - position:absolute; - left: 16em; - color: #4040c0; -} -span.mark { - color: #4040c0; - font-family: Courier New, Courier, monospace; - line-height: 1.1; -} -img { - border: none; - vertical-align: baseline; - margin: 0; - padding: 0; -} -img.left { - float: left; - margin: 0.5em 1em 0.5em 0; -} -img.right { - float: right; - margin: 0.5em 0 0.5em 1em; -} -.indent { - padding-left: 1em; -} -.flush { - clear: both; - visibility: hidden; -} -.hide, .noscreen { - display: none !important; -} -.ext { - color: #ff8000; -} -.new { - font-size: 6pt; - vertical-align: middle; - background: #ff8000; - color: #ffffff; -} -#site { - clear: both; - float: left; - width: 13em; - text-align: center; - font-weight: bold; - margin: 0; - padding: 0; - background: transparent; - color: #ffffff; -} -#site a { - font-size: 200%; -} -#site a:link, #site a:visited { - text-decoration: none; - font-weight: bold; - background: transparent; - color: #ffffff; -} -#site span { - line-height: 3em; /* really 6em relative to body, match h1 */ -} -#logo { - color: #ffb380; -} -#head { - margin: 0; - padding: 0 0 0 2em; - border-left: solid 13em #4162bf; - border-right: solid 3em #6078bf; - background: #6078bf; - color: #e6ecff; -} -#nav { - clear: both; - float: left; - overflow: hidden; - text-align: left; - line-height: 1.5; - width: 13em; - padding-top: 1em; - background: transparent; -} -#nav ul { - list-style: none outside; - margin: 0; - padding: 0; -} -#nav li { - margin: 0; - padding: 0; -} -#nav a { - display: block; - text-decoration: none; - font-weight: bold; - margin: 0; - padding: 2px 1em; - border-top: 1px solid transparent; - border-bottom: 1px solid transparent; - background: transparent; - color: #2142bf; -} -#nav a:hover, #nav a:active { - text-decoration: none; - border-top: 1px solid #97a7d7; - border-bottom: 1px solid #e6ecff; - background: #b9c9f9; - color: #ff0000; -} -#nav a.current, #nav a.current:hover, #nav a.current:active { - border-top: 1px solid #e6ecff; - border-bottom: 1px solid #97a7d7; - background: #c5d5ff; - color: #2142bf; -} -#nav ul ul a { - padding: 0 1em 0 1.7em; -} -#nav ul ul ul a { - padding: 0 0.5em 0 2.4em; -} -#main { - line-height: 1.5; - text-align: left; - margin: 0; - padding: 1em 2em; - border-left: solid 13em #bfcfff; - border-right: solid 3em #e6ecff; - background: #e6ecff; -} -#foot { - clear: both; - font-size: 80%; - text-align: center; - margin: 0; - padding: 0.5em; - background: #6078bf; - color: #ffffff; -} -#foot a:link, #foot a:visited { - text-decoration: underline; - background: transparent; - color: #ffffff; -} -#foot a:hover, #foot a:active { - text-decoration: underline; - background: transparent; - color: #bfcfff; -} diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/contact.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/contact.html deleted file mode 100644 index 6d60928..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/contact.html +++ /dev/null @@ -1,112 +0,0 @@ - - - -Contact - - - - - - - -

-Lua -
- - -
-

-If you want to report bugs, propose fixes or suggest enhancements, -please use the -» GitHub issue tracker. -

-

-Please send general questions to the -» LuaJIT mailing list. -

-

-You can also send any questions you have directly to me: -

- - - - - -

-Note: I cannot reply to GMail, Google Workplace, Outlook or Office365 -mail addresses, since they prefer to mindlessly filter out mails sent -from small domains using independent mail servers, such as mine. If you -don't like that, please complain to Google or Microsoft, not me. -

- -

Copyright

-

-All documentation is -Copyright © 2005-2022 Mike Pall. -

- - -
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_buffer.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_buffer.html deleted file mode 100644 index 2a82aa9..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_buffer.html +++ /dev/null @@ -1,695 +0,0 @@ - - - -String Buffer Library - - - - - - - - -
-Lua -
- - -
-

-The string buffer library allows high-performance manipulation of -string-like data. -

-

-Unlike Lua strings, which are constants, string buffers are -mutable sequences of 8-bit (binary-transparent) characters. Data -can be stored, formatted and encoded into a string buffer and later -converted, extracted or decoded. -

-

-The convenient string buffer API simplifies common string manipulation -tasks, that would otherwise require creating many intermediate strings. -String buffers improve performance by eliminating redundant memory -copies, object creation, string interning and garbage collection -overhead. In conjunction with the FFI library, they allow zero-copy -operations. -

-

-The string buffer library also includes a high-performance -serializer for Lua objects. -

- -

Work in Progress

-

-This library is a work in progress. More -functionality will be added soon. -

- -

Using the String Buffer Library

-

-The string buffer library is built into LuaJIT by default, but it's not -loaded by default. Add this to the start of every Lua file that needs -one of its functions: -

-
-local buffer = require("string.buffer")
-
-

-The convention for the syntax shown on this page is that buffer -refers to the buffer library and buf refers to an individual -buffer object. -

-

-Please note the difference between a Lua function call, e.g. -buffer.new() (with a dot) and a Lua method call, e.g. -buf:reset() (with a colon). -

- -

Buffer Objects

-

-A buffer object is a garbage-collected Lua object. After creation with -buffer.new(), it can (and should) be reused for many operations. -When the last reference to a buffer object is gone, it will eventually -be freed by the garbage collector, along with the allocated buffer -space. -

-

-Buffers operate like a FIFO (first-in first-out) data structure. Data -can be appended (written) to the end of the buffer and consumed (read) -from the front of the buffer. These operations may be freely mixed. -

-

-The buffer space that holds the characters is managed automatically -— it grows as needed and already consumed space is recycled. Use -buffer.new(size) and buf:free(), if you need more -control. -

-

-The maximum size of a single buffer is the same as the maximum size of a -Lua string, which is slightly below two gigabytes. For huge data sizes, -neither strings nor buffers are the right data structure — use the -FFI library to directly map memory or files up to the virtual memory -limit of your OS. -

- -

Buffer Method Overview

-
    -
  • -The buf:put*()-like methods append (write) characters to the -end of the buffer. -
  • -
  • -The buf:get*()-like methods consume (read) characters from the -front of the buffer. -
  • -
  • -Other methods, like buf:tostring() only read the buffer -contents, but don't change the buffer. -
  • -
  • -The buf:set() method allows zero-copy consumption of a string -or an FFI cdata object as a buffer. -
  • -
  • -The FFI-specific methods allow zero-copy read/write-style operations or -modifying the buffer contents in-place. Please check the -FFI caveats below, too. -
  • -
  • -Methods that don't need to return anything specific, return the buffer -object itself as a convenience. This allows method chaining, e.g.: -buf:reset():encode(obj) or buf:skip(len):get() -
  • -
- -

Buffer Creation and Management

- -

local buf = buffer.new([size [,options]])
-local buf = buffer.new([options])

-

-Creates a new buffer object. -

-

-The optional size argument ensures a minimum initial buffer -size. This is strictly an optimization when the required buffer size is -known beforehand. The buffer space will grow as needed, in any case. -

-

-The optional table options sets various -serialization options. -

- -

buf = buf:reset()

-

-Reset (empty) the buffer. The allocated buffer space is not freed and -may be reused. -

- -

buf = buf:free()

-

-The buffer space of the buffer object is freed. The object itself -remains intact, empty and may be reused. -

-

-Note: you normally don't need to use this method. The garbage collector -automatically frees the buffer space, when the buffer object is -collected. Use this method, if you need to free the associated memory -immediately. -

- -

Buffer Writers

- -

buf = buf:put([str|num|obj] [,…])

-

-Appends a string str, a number num or any object -obj with a __tostring metamethod to the buffer. -Multiple arguments are appended in the given order. -

-

-Appending a buffer to a buffer is possible and short-circuited -internally. But it still involves a copy. Better combine the buffer -writes to use a single buffer. -

- -

buf = buf:putf(format, …)

-

-Appends the formatted arguments to the buffer. The format -string supports the same options as string.format(). -

- -

buf = buf:putcdata(cdata, len)FFI

-

-Appends the given len number of bytes from the memory pointed -to by the FFI cdata object to the buffer. The object needs to -be convertible to a (constant) pointer. -

- -

buf = buf:set(str)
-buf = buf:set(cdata, len)
FFI

-

-This method allows zero-copy consumption of a string or an FFI cdata -object as a buffer. It stores a reference to the passed string -str or the FFI cdata object in the buffer. Any buffer -space originally allocated is freed. This is not an append -operation, unlike the buf:put*() methods. -

-

-After calling this method, the buffer behaves as if -buf:free():put(str) or buf:free():put(cdata, len) -had been called. However, the data is only referenced and not copied, as -long as the buffer is only consumed. -

-

-In case the buffer is written to later on, the referenced data is copied -and the object reference is removed (copy-on-write semantics). -

-

-The stored reference is an anchor for the garbage collector and keeps the -originally passed string or FFI cdata object alive. -

- -

ptr, len = buf:reserve(size)FFI
-buf = buf:commit(used)FFI

-

-The reserve method reserves at least size bytes of -write space in the buffer. It returns an uint8_t * FFI -cdata pointer ptr that points to this space. -

-

-The available length in bytes is returned in len. This is at -least size bytes, but may be more to facilitate efficient -buffer growth. You can either make use of the additional space or ignore -len and only use size bytes. -

-

-The commit method appends the used bytes of the -previously returned write space to the buffer data. -

-

-This pair of methods allows zero-copy use of C read-style APIs: -

-
-local MIN_SIZE = 65536
-repeat
-  local ptr, len = buf:reserve(MIN_SIZE)
-  local n = C.read(fd, ptr, len)
-  if n == 0 then break end -- EOF.
-  if n < 0 then error("read error") end
-  buf:commit(n)
-until false
-
-

-The reserved write space is not initialized. At least the -used bytes must be written to before calling the -commit method. There's no need to call the commit -method, if nothing is added to the buffer (e.g. on error). -

- -

Buffer Readers

- -

len = #buf

-

-Returns the current length of the buffer data in bytes. -

- -

res = str|num|buf .. str|num|buf […]

-

-The Lua concatenation operator .. also accepts buffers, just -like strings or numbers. It always returns a string and not a buffer. -

-

-Note that although this is supported for convenience, this thwarts one -of the main reasons to use buffers, which is to avoid string -allocations. Rewrite it with buf:put() and buf:get(). -

-

-Mixing this with unrelated objects that have a __concat -metamethod may not work, since these probably only expect strings. -

- -

buf = buf:skip(len)

-

-Skips (consumes) len bytes from the buffer up to the current -length of the buffer data. -

- -

str, … = buf:get([len|nil] [,…])

-

-Consumes the buffer data and returns one or more strings. If called -without arguments, the whole buffer data is consumed. If called with a -number, up to len bytes are consumed. A nil argument -consumes the remaining buffer space (this only makes sense as the last -argument). Multiple arguments consume the buffer data in the given -order. -

-

-Note: a zero length or no remaining buffer data returns an empty string -and not nil. -

- -

str = buf:tostring()
-str = tostring(buf)

-

-Creates a string from the buffer data, but doesn't consume it. The -buffer remains unchanged. -

-

-Buffer objects also define a __tostring metamethod. This means -buffers can be passed to the global tostring() function and -many other functions that accept this in place of strings. The important -internal uses in functions like io.write() are short-circuited -to avoid the creation of an intermediate string object. -

- -

ptr, len = buf:ref()FFI

-

-Returns an uint8_t * FFI cdata pointer ptr that -points to the buffer data. The length of the buffer data in bytes is -returned in len. -

-

-The returned pointer can be directly passed to C functions that expect a -buffer and a length. You can also do bytewise reads -(local x = ptr[i]) or writes -(ptr[i] = 0x40) of the buffer data. -

-

-In conjunction with the skip method, this allows zero-copy use -of C write-style APIs: -

-
-repeat
-  local ptr, len = buf:ref()
-  if len == 0 then break end
-  local n = C.write(fd, ptr, len)
-  if n < 0 then error("write error") end
-  buf:skip(n)
-until n >= len
-
-

-Unlike Lua strings, buffer data is not implicitly -zero-terminated. It's not safe to pass ptr to C functions that -expect zero-terminated strings. If you're not using len, then -you're doing something wrong. -

- -

Serialization of Lua Objects

-

-The following functions and methods allow high-speed serialization -(encoding) of a Lua object into a string and decoding it back to a Lua -object. This allows convenient storage and transport of structured -data. -

-

-The encoded data is in an internal binary -format. The data can be stored in files, binary-transparent -databases or transmitted to other LuaJIT instances across threads, -processes or networks. -

-

-Encoding speed can reach up to 1 Gigabyte/second on a modern desktop- or -server-class system, even when serializing many small objects. Decoding -speed is mostly constrained by object creation cost. -

-

-The serializer handles most Lua types, common FFI number types and -nested structures. Functions, thread objects, other FFI cdata and full -userdata cannot be serialized (yet). -

-

-The encoder serializes nested structures as trees. Multiple references -to a single object will be stored separately and create distinct objects -after decoding. Circular references cause an error. -

- -

Serialization Functions and Methods

- -

str = buffer.encode(obj)
-buf = buf:encode(obj)

-

-Serializes (encodes) the Lua object obj. The stand-alone -function returns a string str. The buffer method appends the -encoding to the buffer. -

-

-obj can be any of the supported Lua types — it doesn't -need to be a Lua table. -

-

-This function may throw an error when attempting to serialize -unsupported object types, circular references or deeply nested tables. -

- -

obj = buffer.decode(str)
-obj = buf:decode()

-

-The stand-alone function deserializes (decodes) the string -str, the buffer method deserializes one object from the -buffer. Both return a Lua object obj. -

-

-The returned object may be any of the supported Lua types — -even nil. -

-

-This function may throw an error when fed with malformed or incomplete -encoded data. The stand-alone function throws when there's left-over -data after decoding a single top-level object. The buffer method leaves -any left-over data in the buffer. -

-

-Attempting to deserialize an FFI type will throw an error, if the FFI -library is not built-in or has not been loaded, yet. -

- -

Serialization Options

-

-The options table passed to buffer.new() may contain -the following members (all optional): -

-
    -
  • -dict is a Lua table holding a dictionary of strings that -commonly occur as table keys of objects you are serializing. These keys -are compactly encoded as indexes during serialization. A well-chosen -dictionary saves space and improves serialization performance. -
  • -
  • -metatable is a Lua table holding a dictionary of metatables -for the table objects you are serializing. -
  • -
-

-dict needs to be an array of strings and metatable needs -to be an array of tables. Both starting at index 1 and without holes (no -nil in between). The tables are anchored in the buffer object and -internally modified into a two-way index (don't do this yourself, just pass -a plain array). The tables must not be modified after they have been passed -to buffer.new(). -

-

-The dict and metatable tables used by the encoder and -decoder must be the same. Put the most common entries at the front. Extend -at the end to ensure backwards-compatibility — older encodings can -then still be read. You may also set some indexes to false to -explicitly drop backwards-compatibility. Old encodings that use these -indexes will throw an error when decoded. -

-

-Metatables that are not found in the metatable dictionary are -ignored when encoding. Decoding returns a table with a nil -metatable. -

-

-Note: parsing and preparation of the options table is somewhat -expensive. Create a buffer object only once and recycle it for multiple -uses. Avoid mixing encoder and decoder buffers, since the -buf:set() method frees the already allocated buffer space: -

-
-local options = {
-  dict = { "commonly", "used", "string", "keys" },
-}
-local buf_enc = buffer.new(options)
-local buf_dec = buffer.new(options)
-
-local function encode(obj)
-  return buf_enc:reset():encode(obj):get()
-end
-
-local function decode(str)
-  return buf_dec:set(str):decode()
-end
-
- -

Streaming Serialization

-

-In some contexts, it's desirable to do piecewise serialization of large -datasets, also known as streaming. -

-

-This serialization format can be safely concatenated and supports streaming. -Multiple encodings can simply be appended to a buffer and later decoded -individually: -

-
-local buf = buffer.new()
-buf:encode(obj1)
-buf:encode(obj2)
-local copy1 = buf:decode()
-local copy2 = buf:decode()
-
-

-Here's how to iterate over a stream: -

-
-while #buf ~= 0 do
-  local obj = buf:decode()
-  -- Do something with obj.
-end
-
-

-Since the serialization format doesn't prepend a length to its encoding, -network applications may need to transmit the length, too. -

- -

Serialization Format Specification

-

-This serialization format is designed for internal use by LuaJIT -applications. Serialized data is upwards-compatible and portable across -all supported LuaJIT platforms. -

-

-It's an 8-bit binary format and not human-readable. It uses e.g. -embedded zeroes and stores embedded Lua string objects unmodified, which -are 8-bit-clean, too. Encoded data can be safely concatenated for -streaming and later decoded one top-level object at a time. -

-

-The encoding is reasonably compact, but tuned for maximum performance, -not for minimum space usage. It compresses well with any of the common -byte-oriented data compression algorithms. -

-

-Although documented here for reference, this format is explicitly -not intended to be a 'public standard' for structured data -interchange across computer languages (like JSON or MessagePack). Please -do not use it as such. -

-

-The specification is given below as a context-free grammar with a -top-level object as the starting point. Alternatives are -separated by the | symbol and * indicates repeats. -Grouping is implicit or indicated by {…}. Terminals are -either plain hex numbers, encoded as bytes, or have a .format -suffix. -

-
-object    → nil | false | true
-          | null | lightud32 | lightud64
-          | int | num | tab | tab_mt
-          | int64 | uint64 | complex
-          | string
-
-nil       → 0x00
-false     → 0x01
-true      → 0x02
-
-null      → 0x03                            // NULL lightuserdata
-lightud32 → 0x04 data.I                   // 32 bit lightuserdata
-lightud64 → 0x05 data.L                   // 64 bit lightuserdata
-
-int       → 0x06 int.I                                 // int32_t
-num       → 0x07 double.L
-
-tab       → 0x08                                   // Empty table
-          | 0x09 h.U h*{object object}          // Key/value hash
-          | 0x0a a.U a*object                    // 0-based array
-          | 0x0b a.U a*object h.U h*{object object}      // Mixed
-          | 0x0c a.U (a-1)*object                // 1-based array
-          | 0x0d a.U (a-1)*object h.U h*{object object}  // Mixed
-tab_mt    → 0x0e (index-1).U tab          // Metatable dict entry
-
-int64     → 0x10 int.L                             // FFI int64_t
-uint64    → 0x11 uint.L                           // FFI uint64_t
-complex   → 0x12 re.L im.L                         // FFI complex
-
-string    → (0x20+len).U len*char.B
-          | 0x0f (index-1).U                 // String dict entry
-
-.B = 8 bit
-.I = 32 bit little-endian
-.L = 64 bit little-endian
-.U = prefix-encoded 32 bit unsigned number n:
-     0x00..0xdf   → n.B
-     0xe0..0x1fdf → (0xe0|(((n-0xe0)>>8)&0x1f)).B ((n-0xe0)&0xff).B
-   0x1fe0..       → 0xff n.I
-
- -

Error handling

-

-Many of the buffer methods can throw an error. Out-of-memory or usage -errors are best caught with an outer wrapper for larger parts of code. -There's not much one can do after that, anyway. -

-

-OTOH, you may want to catch some errors individually. Buffer methods need -to receive the buffer object as the first argument. The Lua colon-syntax -obj:method() does that implicitly. But to wrap a method with -pcall(), the arguments need to be passed like this: -

-
-local ok, err = pcall(buf.encode, buf, obj)
-if not ok then
-  -- Handle error in err.
-end
-
- -

FFI caveats

-

-The string buffer library has been designed to work well together with -the FFI library. But due to the low-level nature of the FFI library, -some care needs to be taken: -

-

-First, please remember that FFI pointers are zero-indexed. The space -returned by buf:reserve() and buf:ref() starts at the -returned pointer and ends before len bytes after that. -

-

-I.e. the first valid index is ptr[0] and the last valid index -is ptr[len-1]. If the returned length is zero, there's no valid -index at all. The returned pointer may even be NULL. -

-

-The space pointed to by the returned pointer is only valid as long as -the buffer is not modified in any way (neither append, nor consume, nor -reset, etc.). The pointer is also not a GC anchor for the buffer object -itself. -

-

-Buffer data is only guaranteed to be byte-aligned. Casting the returned -pointer to a data type with higher alignment may cause unaligned -accesses. It depends on the CPU architecture whether this is allowed or -not (it's always OK on x86/x64 and mostly OK on other modern -architectures). -

-

-FFI pointers or references do not count as GC anchors for an underlying -object. E.g. an array allocated with ffi.new() is -anchored by buf:set(array, len), but not by -buf:set(array+offset, len). The addition of the offset -creates a new pointer, even when the offset is zero. In this case, you -need to make sure there's still a reference to the original array as -long as its contents are in use by the buffer. -

-

-Even though each LuaJIT VM instance is single-threaded (but you can -create multiple VMs), FFI data structures can be accessed concurrently. -Be careful when reading/writing FFI cdata from/to buffers to avoid -concurrent accesses or modifications. In particular, the memory -referenced by buf:set(cdata, len) must not be modified -while buffer readers are working on it. Shared, but read-only memory -mappings of files are OK, but only if the file does not change. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_c_api.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_c_api.html deleted file mode 100644 index 151d20b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_c_api.html +++ /dev/null @@ -1,183 +0,0 @@ - - - -Lua/C API Extensions - - - - - - - -
-Lua -
- - -
-

-LuaJIT adds some extensions to the standard Lua/C API. The LuaJIT include -directory must be in the compiler search path (-Ipath) -to be able to include the required header for C code: -

-
-#include "luajit.h"
-
-

-Or for C++ code: -

-
-#include "lua.hpp"
-
- -

luaJIT_setmode(L, idx, mode) -— Control VM

-

-This is a C API extension to allow control of the VM from C code. The -full prototype of LuaJIT_setmode is: -

-
-LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
-
-

-The returned status is either success (1) or failure (0). -The second argument is either 0 or a stack index (similar to the -other Lua/C API functions). -

-

-The third argument specifies the mode, which is 'or'ed with a flag. -The flag can be LUAJIT_MODE_OFF to turn a feature off, -LUAJIT_MODE_ON to turn a feature on, or -LUAJIT_MODE_FLUSH to flush cached code. -

-

-The following modes are defined: -

- -

luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)

-

-Turn the whole JIT compiler on or off or flush the whole cache of compiled code. -

- -

luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)
-luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)
-luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)

-

-This sets the mode for the function at the stack index idx or -the parent of the calling function (idx = 0). It either -enables JIT compilation for a function, disables it and flushes any -already compiled code, or only flushes already compiled code. This -applies recursively to all sub-functions of the function with -LUAJIT_MODE_ALLFUNC or only to the sub-functions with -LUAJIT_MODE_ALLSUBFUNC. -

- -

luaJIT_setmode(L, trace,
-  LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)

-

-Flushes the specified root trace and all of its side traces from the cache. -The code for the trace will be retained as long as there are any other -traces which link to it. -

- -

luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)

-

-This mode defines a wrapper function for calls to C functions. If -called with LUAJIT_MODE_ON, the stack index at idx -must be a lightuserdata object holding a pointer to the wrapper -function. From now on, all C functions are called through the wrapper -function. If called with LUAJIT_MODE_OFF this mode is turned -off and all C functions are directly called. -

-

-The wrapper function can be used for debugging purposes or to catch -and convert foreign exceptions. But please read the section on -C++ exception interoperability -first. Recommended usage can be seen in this C++ code excerpt: -

-
-#include <exception>
-#include "lua.hpp"
-
-// Catch C++ exceptions and convert them to Lua error messages.
-// Customize as needed for your own exception classes.
-static int wrap_exceptions(lua_State *L, lua_CFunction f)
-{
-  try {
-    return f(L);  // Call wrapped function and return result.
-  } catch (const char *s) {  // Catch and convert exceptions.
-    lua_pushstring(L, s);
-  } catch (std::exception& e) {
-    lua_pushstring(L, e.what());
-  } catch (...) {
-    lua_pushliteral(L, "caught (...)");
-  }
-  return lua_error(L);  // Rethrow as a Lua error.
-}
-
-static int myinit(lua_State *L)
-{
-  ...
-  // Define wrapper function and enable it.
-  lua_pushlightuserdata(L, (void *)wrap_exceptions);
-  luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
-  lua_pop(L, 1);
-  ...
-}
-
-

-Note that you can only define a single global wrapper function, -so be careful when using this mechanism from multiple C++ modules. -Also note that this mechanism is not without overhead. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi.html deleted file mode 100644 index 5f1e2d7..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi.html +++ /dev/null @@ -1,326 +0,0 @@ - - - -FFI Library - - - - - - - -
-Lua -
- - -
-

- -The FFI library allows calling external C functions and -using C data structures from pure Lua code. - -

-

- -The FFI library largely obviates the need to write tedious manual -Lua/C bindings in C. No need to learn a separate binding language -— it parses plain C declarations! These can be -cut-n-pasted from C header files or reference manuals. It's up to -the task of binding large libraries without the need for dealing with -fragile binding generators. - -

-

-The FFI library is tightly integrated into LuaJIT (it's not available -as a separate module). The code generated by the JIT-compiler for -accesses to C data structures from Lua code is on par with the -code a C compiler would generate. Calls to C functions can -be inlined in JIT-compiled code, unlike calls to functions bound via -the classic Lua/C API. -

-

-This page gives a short introduction to the usage of the FFI library. -Please use the FFI sub-topics in the navigation bar to learn more. -

- -

Motivating Example: Calling External C Functions

-

-It's really easy to call an external C library function: -

-
-①
-②
-
-
-③local ffi = require("ffi")
-ffi.cdef[[
-int printf(const char *fmt, ...);
-]]
-ffi.C.printf("Hello %s!", "world")
-
-

-So, let's pick that apart: -

-

- Load the FFI library. -

-

- Add a C declaration -for the function. The part inside the double-brackets (in green) is -just standard C syntax. -

-

- Call the named -C function — Yes, it's that simple! -

-

-Actually, what goes on behind the scenes is far from simple: makes use of the standard -C library namespace ffi.C. Indexing this namespace with -a symbol name ("printf") automatically binds it to the -standard C library. The result is a special kind of object which, -when called, runs the printf function. The arguments passed -to this function are automatically converted from Lua objects to the -corresponding C types. -

-

-Ok, so maybe the use of printf() wasn't such a spectacular -example. You could have done that with io.write() and -string.format(), too. But you get the idea ... -

-

-So here's something to pop up a message box on Windows: -

-
-local ffi = require("ffi")
-ffi.cdef[[
-int MessageBoxA(void *w, const char *txt, const char *cap, int type);
-]]
-ffi.C.MessageBoxA(nil, "Hello world!", "Test", 0)
-
-

-Bing! Again, that was far too easy, no? -

-

-Compare this with the effort required to bind that function using the -classic Lua/C API: create an extra C file, add a C function -that retrieves and checks the argument types passed from Lua and calls -the actual C function, add a list of module functions and their -names, add a luaopen_* function and register all module -functions, compile and link it into a shared library (DLL), move it to -the proper path, add Lua code that loads the module aaaand ... finally -call the binding function. Phew! -

- -

Motivating Example: Using C Data Structures

-

-The FFI library allows you to create and access C data -structures. Of course, the main use for this is for interfacing with -C functions. But they can be used stand-alone, too. -

-

-Lua is built upon high-level data types. They are flexible, extensible -and dynamic. That's why we all love Lua so much. Alas, this can be -inefficient for certain tasks, where you'd really want a low-level -data type. E.g. a large array of a fixed structure needs to be -implemented with a big table holding lots of tiny tables. This imposes -both a substantial memory overhead as well as a performance overhead. -

-

-Here's a sketch of a library that operates on color images, plus a -simple benchmark. First, the plain Lua version: -

-
-local floor = math.floor
-
-local function image_ramp_green(n)
-  local img = {}
-  local f = 255/(n-1)
-  for i=1,n do
-    img[i] = { red = 0, green = floor((i-1)*f), blue = 0, alpha = 255 }
-  end
-  return img
-end
-
-local function image_to_gray(img, n)
-  for i=1,n do
-    local y = floor(0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue)
-    img[i].red = y; img[i].green = y; img[i].blue = y
-  end
-end
-
-local N = 400*400
-local img = image_ramp_green(N)
-for i=1,1000 do
-  image_to_gray(img, N)
-end
-
-

-This creates a table with 160.000 pixels, each of which is a table -holding four number values in the range of 0-255. First, an image with -a green ramp is created (1D for simplicity), then the image is -converted to grayscale 1000 times. Yes, that's silly, but I was in -need of a simple example ... -

-

-And here's the FFI version. The modified parts have been marked in -bold: -

-
-①
-
-
-
-
-
-②
-
-③
-④
-
-
-
-
-
-
-③
-⑤local ffi = require("ffi")
-ffi.cdef[[
-typedef struct { uint8_t red, green, blue, alpha; } rgba_pixel;
-]]
-
-local function image_ramp_green(n)
-  local img = ffi.new("rgba_pixel[?]", n)
-  local f = 255/(n-1)
-  for i=0,n-1 do
-    img[i].green = i*f
-    img[i].alpha = 255
-  end
-  return img
-end
-
-local function image_to_grey(img, n)
-  for i=0,n-1 do
-    local y = 0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue
-    img[i].red = y; img[i].green = y; img[i].blue = y
-  end
-end
-
-local N = 400*400
-local img = image_ramp_green(N)
-for i=1,1000 do
-  image_to_grey(img, N)
-end
-
-

-Ok, so that wasn't too difficult: -

-

- First, load the FFI -library and declare the low-level data type. Here we choose a -struct which holds four byte fields, one for each component -of a 4x8 bit RGBA pixel. -

-

- Creating the data -structure with ffi.new() is straightforward — the -'?' is a placeholder for the number of elements of a -variable-length array. -

-

- C arrays are -zero-based, so the indexes have to run from 0 to -n-1. One might want to allocate one more element instead to -simplify converting legacy code. -

-

- Since ffi.new() -zero-fills the array by default, we only need to set the green and the -alpha fields. -

-

- The calls to -math.floor() can be omitted here, because floating-point -numbers are already truncated towards zero when converting them to an -integer. This happens implicitly when the number is stored in the -fields of each pixel. -

-

-Now let's have a look at the impact of the changes: first, memory -consumption for the image is down from 22 Megabytes to -640 Kilobytes (400*400*4 bytes). That's a factor of 35x less! So, -yes, tables do have a noticeable overhead. BTW: The original program -would consume 40 Megabytes in plain Lua (on x64). -

-

-Next, performance: the pure Lua version runs in 9.57 seconds (52.9 -seconds with the Lua interpreter) and the FFI version runs in 0.48 -seconds on my machine (YMMV). That's a factor of 20x faster (110x -faster than the Lua interpreter). -

-

-The avid reader may notice that converting the pure Lua version over -to use array indexes for the colors ([1] instead of -.red, [2] instead of .green etc.) ought to -be more compact and faster. This is certainly true (by a factor of -~1.7x). Switching to a struct-of-arrays would help, too. -

-

-However, the resulting code would be less idiomatic and rather -error-prone. And it still doesn't get even close to the performance of -the FFI version of the code. Also, high-level data structures cannot -be easily passed to other C functions, especially I/O functions, -without undue conversion penalties. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_api.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_api.html deleted file mode 100644 index 89ddb0d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_api.html +++ /dev/null @@ -1,566 +0,0 @@ - - - -ffi.* API Functions - - - - - - - - -
-Lua -
- - -
-

-This page describes the API functions provided by the FFI library in -detail. It's recommended to read through the -introduction and the -FFI tutorial first. -

- -

Glossary

-
    -
  • cdecl — An abstract C type declaration (a Lua -string).
  • -
  • ctype — A C type object. This is a special kind of -cdata returned by ffi.typeof(). It serves as a -cdata constructor when called.
  • -
  • cdata — A C data object. It holds a value of the -corresponding ctype.
  • -
  • ct — A C type specification which can be used for -most of the API functions. Either a cdecl, a ctype or a -cdata serving as a template type.
  • -
  • cb — A callback object. This is a C data object -holding a special function pointer. Calling this function from -C code runs an associated Lua function.
  • -
  • VLA — A variable-length array is declared with a -? instead of the number of elements, e.g. "int[?]". -The number of elements (nelem) must be given when it's -created.
  • -
  • VLS — A variable-length struct is a struct C -type where the last element is a VLA. The same rules for -declaration and creation apply.
  • -
- -

Declaring and Accessing External Symbols

-

-External symbols must be declared first and can then be accessed by -indexing a C library -namespace, which automatically binds the symbol to a specific -library. -

- -

ffi.cdef(def)

-

-Adds multiple C declarations for types or external symbols (named -variables or functions). def must be a Lua string. It's -recommended to use the syntactic sugar for string arguments as -follows: -

-
-ffi.cdef[[
-typedef struct foo { int a, b; } foo_t;  // Declare a struct and typedef.
-int dofoo(foo_t *f, int n);  /* Declare an external C function. */
-]]
-
-

-The contents of the string (the part in green above) must be a -sequence of -C declarations, -separated by semicolons. The trailing semicolon for a single -declaration may be omitted. -

-

-Please note, that external symbols are only declared, but they -are not bound to any specific address, yet. Binding is -achieved with C library namespaces (see below). -

-

-C declarations are not passed through a C pre-processor, -yet. No pre-processor tokens are allowed, except for -#pragma pack. Replace #define in existing -C header files with enum, static const -or typedef and/or pass the files through an external -C pre-processor (once). Be careful not to include unneeded or -redundant declarations from unrelated header files. -

- -

ffi.C

-

-This is the default C library namespace — note the -uppercase 'C'. It binds to the default set of symbols or -libraries on the target system. These are more or less the same as a -C compiler would offer by default, without specifying extra link -libraries. -

-

-On POSIX systems, this binds to symbols in the default or global -namespace. This includes all exported symbols from the executable and -any libraries loaded into the global namespace. This includes at least -libc, libm, libdl (on Linux), -libgcc (if compiled with GCC), as well as any exported -symbols from the Lua/C API provided by LuaJIT itself. -

-

-On Windows systems, this binds to symbols exported from the -*.exe, the lua51.dll (i.e. the Lua/C API -provided by LuaJIT itself), the C runtime library LuaJIT was linked -with (msvcrt*.dll), kernel32.dll, -user32.dll and gdi32.dll. -

- -

clib = ffi.load(name [,global])

-

-This loads the dynamic library given by name and returns -a new C library namespace which binds to its symbols. On POSIX -systems, if global is true, the library symbols are -loaded into the global namespace, too. -

-

-If name is a path, the library is loaded from this path. -Otherwise name is canonicalized in a system-dependent way and -searched in the default search path for dynamic libraries: -

-

-On POSIX systems, if the name contains no dot, the extension -.so is appended. Also, the lib prefix is prepended -if necessary. So ffi.load("z") looks for "libz.so" -in the default shared library search path. -

-

-On Windows systems, if the name contains no dot, the extension -.dll is appended. So ffi.load("ws2_32") looks for -"ws2_32.dll" in the default DLL search path. -

- -

Creating cdata Objects

-

-The following API functions create cdata objects (type() -returns "cdata"). All created cdata objects are -garbage collected. -

- -

cdata = ffi.new(ct [,nelem] [,init...])
-cdata = ctype([nelem,] [init...])

-

-Creates a cdata object for the given ct. VLA/VLS types -require the nelem argument. The second syntax uses a ctype as -a constructor and is otherwise fully equivalent. -

-

-The cdata object is initialized according to the -rules for initializers, -using the optional init arguments. Excess initializers cause -an error. -

-

-Performance notice: if you want to create many objects of one kind, -parse the cdecl only once and get its ctype with -ffi.typeof(). Then use the ctype as a constructor repeatedly. -

-

-Please note, that an anonymous struct declaration implicitly -creates a new and distinguished ctype every time you use it for -ffi.new(). This is probably not what you want, -especially if you create more than one cdata object. Different anonymous -structs are not considered assignment-compatible by the -C standard, even though they may have the same fields! Also, they -are considered different types by the JIT-compiler, which may cause an -excessive number of traces. It's strongly suggested to either declare -a named struct or typedef with ffi.cdef() -or to create a single ctype object for an anonymous struct -with ffi.typeof(). -

- -

ctype = ffi.typeof(ct)

-

-Creates a ctype object for the given ct. -

-

-This function is especially useful to parse a cdecl only once and then -use the resulting ctype object as a constructor. -

- -

cdata = ffi.cast(ct, init)

-

-Creates a scalar cdata object for the given ct. The cdata -object is initialized with init using the "cast" variant of -the C type conversion -rules. -

-

-This functions is mainly useful to override the pointer compatibility -checks or to convert pointers to addresses or vice versa. -

- -

ctype = ffi.metatype(ct, metatable)

-

-Creates a ctype object for the given ct and associates it with -a metatable. Only struct/union types, complex numbers -and vectors are allowed. Other types may be wrapped in a -struct, if needed. -

-

-The association with a metatable is permanent and cannot be changed -afterwards. Neither the contents of the metatable nor the -contents of an __index table (if any) may be modified -afterwards. The associated metatable automatically applies to all uses -of this type, no matter how the objects are created or where they -originate from. Note that predefined operations on types have -precedence (e.g. declared field names cannot be overridden). -

-

-All standard Lua metamethods are implemented. These are called directly, -without shortcuts, and on any mix of types. For binary operations, the -left operand is checked first for a valid ctype metamethod. The -__gc metamethod only applies to struct/union -types and performs an implicit ffi.gc() -call during creation of an instance. -

- -

cdata = ffi.gc(cdata, finalizer)

-

-Associates a finalizer with a pointer or aggregate cdata object. The -cdata object is returned unchanged. -

-

-This function allows safe integration of unmanaged resources into the -automatic memory management of the LuaJIT garbage collector. Typical -usage: -

-
-local p = ffi.gc(ffi.C.malloc(n), ffi.C.free)
-...
-p = nil -- Last reference to p is gone.
--- GC will eventually run finalizer: ffi.C.free(p)
-
-

-A cdata finalizer works like the __gc metamethod for userdata -objects: when the last reference to a cdata object is gone, the -associated finalizer is called with the cdata object as an argument. The -finalizer can be a Lua function or a cdata function or cdata function -pointer. An existing finalizer can be removed by setting a nil -finalizer, e.g. right before explicitly deleting a resource: -

-
-ffi.C.free(ffi.gc(p, nil)) -- Manually free the memory.
-
- -

C Type Information

-

-The following API functions return information about C types. -They are most useful for inspecting cdata objects. -

- -

size = ffi.sizeof(ct [,nelem])

-

-Returns the size of ct in bytes. Returns nil if -the size is not known (e.g. for "void" or function types). -Requires nelem for VLA/VLS types, except for cdata objects. -

- -

align = ffi.alignof(ct)

-

-Returns the minimum required alignment for ct in bytes. -

- -

ofs [,bpos,bsize] = ffi.offsetof(ct, field)

-

-Returns the offset (in bytes) of field relative to the start -of ct, which must be a struct. Additionally returns -the position and the field size (in bits) for bit fields. -

- -

status = ffi.istype(ct, obj)

-

-Returns true if obj has the C type given by -ct. Returns false otherwise. -

-

-C type qualifiers (const etc.) are ignored. Pointers are -checked with the standard pointer compatibility rules, but without any -special treatment for void *. If ct specifies a -struct/union, then a pointer to this type is accepted, -too. Otherwise the types must match exactly. -

-

-Note: this function accepts all kinds of Lua objects for the -obj argument, but always returns false for non-cdata -objects. -

- -

Utility Functions

- -

err = ffi.errno([newerr])

-

-Returns the error number set by the last C function call which -indicated an error condition. If the optional newerr argument -is present, the error number is set to the new value and the previous -value is returned. -

-

-This function offers a portable and OS-independent way to get and set the -error number. Note that only some C functions set the error -number. And it's only significant if the function actually indicated an -error condition (e.g. with a return value of -1 or -NULL). Otherwise, it may or may not contain any previously set -value. -

-

-You're advised to call this function only when needed and as close as -possible after the return of the related C function. The -errno value is preserved across hooks, memory allocations, -invocations of the JIT compiler and other internal VM activity. The same -applies to the value returned by GetLastError() on Windows, but -you need to declare and call it yourself. -

- -

str = ffi.string(ptr [,len])

-

-Creates an interned Lua string from the data pointed to by -ptr. -

-

-If the optional argument len is missing, ptr is -converted to a "char *" and the data is assumed to be -zero-terminated. The length of the string is computed with -strlen(). -

-

-Otherwise ptr is converted to a "void *" and -len gives the length of the data. The data may contain -embedded zeros and need not be byte-oriented (though this may cause -endianess issues). -

-

-This function is mainly useful to convert (temporary) -"const char *" pointers returned by -C functions to Lua strings and store them or pass them to other -functions expecting a Lua string. The Lua string is an (interned) copy -of the data and bears no relation to the original data area anymore. -Lua strings are 8 bit clean and may be used to hold arbitrary, -non-character data. -

-

-Performance notice: it's faster to pass the length of the string, if -it's known. E.g. when the length is returned by a C call like -sprintf(). -

- -

ffi.copy(dst, src, len)
-ffi.copy(dst, str)

-

-Copies the data pointed to by src to dst. -dst is converted to a "void *" and src -is converted to a "const void *". -

-

-In the first syntax, len gives the number of bytes to copy. -Caveat: if src is a Lua string, then len must not -exceed #src+1. -

-

-In the second syntax, the source of the copy must be a Lua string. All -bytes of the string plus a zero-terminator are copied to -dst (i.e. #src+1 bytes). -

-

-Performance notice: ffi.copy() may be used as a faster -(inlinable) replacement for the C library functions -memcpy(), strcpy() and strncpy(). -

- -

ffi.fill(dst, len [,c])

-

-Fills the data pointed to by dst with len constant -bytes, given by c. If c is omitted, the data is -zero-filled. -

-

-Performance notice: ffi.fill() may be used as a faster -(inlinable) replacement for the C library function -memset(dst, c, len). Please note the different -order of arguments! -

- -

Target-specific Information

- -

status = ffi.abi(param)

-

-Returns true if param (a Lua string) applies for the -target ABI (Application Binary Interface). Returns false -otherwise. The following parameters are currently defined: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
ParameterDescription
32bit32 bit architecture
64bit64 bit architecture
leLittle-endian architecture
beBig-endian architecture
fpuTarget has a hardware FPU
softfpsoftfp calling conventions
hardfphardfp calling conventions
eabiEABI variant of the standard ABI
winWindows variant of the standard ABI
uwpUniversal Windows Platform
gc6464 bit GC references
- -

ffi.os

-

-Contains the target OS name. Same contents as -jit.os. -

- -

ffi.arch

-

-Contains the target architecture name. Same contents as -jit.arch. -

- -

Methods for Callbacks

-

-The C types for callbacks -have some extra methods: -

- -

cb:free()

-

-Free the resources associated with a callback. The associated Lua -function is unanchored and may be garbage collected. The callback -function pointer is no longer valid and must not be called again -(it may be reused by a subsequently created callback). -

- -

cb:set(func)

-

-Associate a new Lua function with a callback. The C type of the -callback and the callback function pointer are unchanged. -

-

-This method is useful to dynamically switch the receiver of callbacks -without creating a new callback each time and registering it again (e.g. -with a GUI library). -

- -

Extended Standard Library Functions

-

-The following standard library functions have been extended to work -with cdata objects: -

- -

n = tonumber(cdata)

-

-Converts a number cdata object to a double and returns it as -a Lua number. This is particularly useful for boxed 64 bit -integer values. Caveat: this conversion may incur a precision loss. -

- -

s = tostring(cdata)

-

-Returns a string representation of the value of 64 bit integers -("nnnLL" or "nnnULL") or -complex numbers ("re±imi"). Otherwise -returns a string representation of the C type of a ctype object -("ctype<type>") or a cdata object -("cdata<type>: address"), unless you -override it with a __tostring metamethod (see -ffi.metatype()). -

- -

iter, obj, start = pairs(cdata)
-iter, obj, start = ipairs(cdata)

-

-Calls the __pairs or __ipairs metamethod of the -corresponding ctype. -

- -

Extensions to the Lua Parser

-

-The parser for Lua source code treats numeric literals with the -suffixes LL or ULL as signed or unsigned 64 bit -integers. Case doesn't matter, but uppercase is recommended for -readability. It handles decimal (42LL), hexadecimal -(0x2aLL) and binary (0b101010LL) literals. -

-

-The imaginary part of complex numbers can be specified by suffixing -number literals with i or I, e.g. 12.5i. -Caveat: you'll need to use 1i to get an imaginary part with -the value one, since i itself still refers to a variable -named i. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_semantics.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_semantics.html deleted file mode 100644 index 603f995..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_semantics.html +++ /dev/null @@ -1,1256 +0,0 @@ - - - -FFI Semantics - - - - - - - - -
-Lua -
- - -
-

-This page describes the detailed semantics underlying the FFI library -and its interaction with both Lua and C code. -

-

-Given that the FFI library is designed to interface with C code -and that declarations can be written in plain C syntax, it -closely follows the C language semantics, wherever possible. -Some minor concessions are needed for smoother interoperation with Lua -language semantics. -

-

-Please don't be overwhelmed by the contents of this page — this -is a reference and you may need to consult it, if in doubt. It doesn't -hurt to skim this page, but most of the semantics "just work" as you'd -expect them to work. It should be straightforward to write -applications using the LuaJIT FFI for developers with a C or C++ -background. -

- -

C Language Support

-

-The FFI library has a built-in C parser with a minimal memory -footprint. It's used by the ffi.* library -functions to declare C types or external symbols. -

-

-Its only purpose is to parse C declarations, as found e.g. in -C header files. Although it does evaluate constant expressions, -it's not a C compiler. The body of inline -C function definitions is simply ignored. -

-

-Also, this is not a validating C parser. It expects and -accepts correctly formed C declarations, but it may choose to -ignore bad declarations or show rather generic error messages. If in -doubt, please check the input against your favorite C compiler. -

-

-The C parser complies to the C99 language standard plus -the following extensions: -

-
    - -
  • The '\e' escape in character and string literals.
  • - -
  • The C99/C++ boolean type, declared with the keywords bool -or _Bool.
  • - -
  • Complex numbers, declared with the keywords complex or -_Complex.
  • - -
  • Two complex number types: complex (aka -complex double) and complex float.
  • - -
  • Vector types, declared with the GCC mode or -vector_size attribute.
  • - -
  • Unnamed ('transparent') struct/union fields -inside a struct/union.
  • - -
  • Incomplete enum declarations, handled like incomplete -struct declarations.
  • - -
  • Unnamed enum fields inside a -struct/union. This is similar to a scoped C++ -enum, except that declared constants are visible in the -global namespace, too.
  • - -
  • Scoped static const declarations inside a -struct/union (from C++).
  • - -
  • Zero-length arrays ([0]), empty -struct/union, variable-length arrays (VLA, -[?]) and variable-length structs (VLS, with a trailing -VLA).
  • - -
  • C++ reference types (int &x).
  • - -
  • Alternate GCC keywords with '__', e.g. -__const__.
  • - -
  • GCC __attribute__ with the following attributes: -aligned, packed, mode, -vector_size, cdecl, fastcall, -stdcall, thiscall.
  • - -
  • The GCC __extension__ keyword and the GCC -__alignof__ operator.
  • - -
  • GCC __asm__("symname") symbol name redirection for -function declarations.
  • - -
  • MSVC keywords for fixed-length types: __int8, -__int16, __int32 and __int64.
  • - -
  • MSVC __cdecl, __fastcall, __stdcall, -__thiscall, __ptr32, __ptr64, -__declspec(align(n)) and #pragma pack.
  • - -
  • All other GCC/MSVC-specific attributes are ignored.
  • - -
-

-The following C types are predefined by the C parser (like -a typedef, except re-declarations will be ignored): -

-
    - -
  • Vararg handling: va_list, __builtin_va_list, -__gnuc_va_list.
  • - -
  • From <stddef.h>: ptrdiff_t, -size_t, wchar_t.
  • - -
  • From <stdint.h>: int8_t, int16_t, -int32_t, int64_t, uint8_t, -uint16_t, uint32_t, uint64_t, -intptr_t, uintptr_t.
  • - -
  • From <unistd.h> (POSIX): ssize_t.
  • - -
-

-You're encouraged to use these types in preference to -compiler-specific extensions or target-dependent standard types. -E.g. char differs in signedness and long differs in -size, depending on the target architecture and platform ABI. -

-

-The following C features are not supported: -

-
    - -
  • A declaration must always have a type specifier; it doesn't -default to an int type.
  • - -
  • Old-style empty function declarations (K&R) are not allowed. -All C functions must have a proper prototype declaration. A -function declared without parameters (int foo();) is -treated as a function taking zero arguments, like in C++.
  • - -
  • The long double C type is parsed correctly, but -there's no support for the related conversions, accesses or arithmetic -operations.
  • - -
  • Wide character strings and character literals are not -supported.
  • - -
  • See below for features that are currently -not implemented.
  • - -
- -

C Type Conversion Rules

- -

Conversions from C types to Lua objects

-

-These conversion rules apply for read accesses to -C types: indexing pointers, arrays or -struct/union types; reading external variables or -constant values; retrieving return values from C calls: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
int8_t, int16_tsign-ext int32_tdoublenumber
uint8_t, uint16_tzero-ext int32_tdoublenumber
int32_t, uint32_tdoublenumber
int64_t, uint64_tboxed value64 bit int cdata
double, floatdoublenumber
bool0 → false, otherwise trueboolean
enumboxed valueenum cdata
Complex numberboxed valuecomplex cdata
Vectorboxed valuevector cdata
Pointerboxed valuepointer cdata
Arrayboxed referencereference cdata
struct/unionboxed referencereference cdata
-

-Bitfields are treated like their underlying type. -

-

-Reference types are dereferenced before a conversion can take -place — the conversion is applied to the C type pointed to -by the reference. -

- -

Conversions from Lua objects to C types

-

-These conversion rules apply for write accesses to -C types: indexing pointers, arrays or -struct/union types; initializing cdata objects; -casts to C types; writing to external variables; passing -arguments to C calls: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
numberdouble
booleanfalse → 0, true → 1bool
nilNULL(void *)
lightuserdatalightuserdata address →(void *)
userdatauserdata payload →(void *)
io.* fileget FILE * handle →(void *)
stringmatch against enum constantenum
stringcopy string data + zero-byteint8_t[], uint8_t[]
stringstring data →const char[]
functioncreate callbackC function type
tabletable initializerArray
tabletable initializerstruct/union
cdatacdata payload →C type
-

-If the result type of this conversion doesn't match the -C type of the destination, the -conversion rules between C types -are applied. -

-

-Reference types are immutable after initialization ("no re-seating of -references"). For initialization purposes or when passing values to -reference parameters, they are treated like pointers. Note that unlike -in C++, there's no way to implement automatic reference generation of -variables under the Lua language semantics. If you want to call a -function with a reference parameter, you need to explicitly pass a -one-element array. -

- -

Conversions between C types

-

-These conversion rules are more or less the same as the standard -C conversion rules. Some rules only apply to casts, or require -pointer or type compatibility: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
Signed integernarrow or sign-extendInteger
Unsigned integernarrow or zero-extendInteger
Integerrounddouble, float
double, floattrunc int32_tnarrow(u)int8_t, (u)int16_t
double, floattrunc(u)int32_t, (u)int64_t
double, floatroundfloat, double
Numbern == 0 → 0, otherwise 1bool
boolfalse → 0, true → 1Number
Complex numberconvert real partNumber
Numberconvert real part, imag = 0Complex number
Complex numberconvert real and imag partComplex number
Numberconvert scalar and replicateVector
Vectorcopy (same size)Vector
struct/uniontake base address (compat)Pointer
Arraytake base address (compat)Pointer
Functiontake function addressFunction pointer
Numberconvert via uintptr_t (cast)Pointer
Pointerconvert address (compat/cast)Pointer
Pointerconvert address (cast)Integer
Arrayconvert base address (cast)Integer
Arraycopy (compat)Array
struct/unioncopy (identical type)struct/union
-

-Bitfields or enum types are treated like their underlying -type. -

-

-Conversions not listed above will raise an error. E.g. it's not -possible to convert a pointer to a complex number or vice versa. -

- -

Conversions for vararg C function arguments

-

-The following default conversion rules apply when passing Lua objects -to the variable argument part of vararg C functions: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
InputConversionOutput
numberdouble
booleanfalse → 0, true → 1bool
nilNULL(void *)
userdatauserdata payload →(void *)
lightuserdatalightuserdata address →(void *)
stringstring data →const char *
float cdatadouble
Array cdatatake base addressElement pointer
struct/union cdatatake base addressstruct/union pointer
Function cdatatake function addressFunction pointer
Any other cdatano conversionC type
-

-To pass a Lua object, other than a cdata object, as a specific type, -you need to override the conversion rules: create a temporary cdata -object with a constructor or a cast and initialize it with the value -to pass: -

-

-Assuming x is a Lua number, here's how to pass it as an -integer to a vararg function: -

-
-ffi.cdef[[
-int printf(const char *fmt, ...);
-]]
-ffi.C.printf("integer value: %d\n", ffi.new("int", x))
-
-

-If you don't do this, the default Lua number → double -conversion rule applies. A vararg C function expecting an integer -will see a garbled or uninitialized value. -

- -

Initializers

-

-Creating a cdata object with -ffi.new() or the -equivalent constructor syntax always initializes its contents, too. -Different rules apply, depending on the number of optional -initializers and the C types involved: -

-
    -
  • If no initializers are given, the object is filled with zero bytes.
  • - -
  • Scalar types (numbers and pointers) accept a single initializer. -The Lua object is converted to the scalar -C type.
  • - -
  • Valarrays (complex numbers and vectors) are treated like scalars -when a single initializer is given. Otherwise they are treated like -regular arrays.
  • - -
  • Aggregate types (arrays and structs) accept either a single cdata -initializer of the same type (copy constructor), a single -table initializer, or a flat list of -initializers.
  • - -
  • The elements of an array are initialized, starting at index zero. -If a single initializer is given for an array, it's repeated for all -remaining elements. This doesn't happen if two or more initializers -are given: all remaining uninitialized elements are filled with zero -bytes.
  • - -
  • Byte arrays may also be initialized with a Lua string. This copies -the whole string plus a terminating zero-byte. The copy stops early only -if the array has a known, fixed size.
  • - -
  • The fields of a struct are initialized in the order of -their declaration. Uninitialized fields are filled with zero -bytes.
  • - -
  • Only the first field of a union can be initialized with a -flat initializer.
  • - -
  • Elements or fields which are aggregates themselves are initialized -with a single initializer, but this may be a table -initializer or a compatible aggregate.
  • - -
  • Excess initializers cause an error.
  • - -
- -

Table Initializers

-

-The following rules apply if a Lua table is used to initialize an -Array or a struct/union: -

-
    - -
  • If the table index [0] is non-nil, then the -table is assumed to be zero-based. Otherwise it's assumed to be -one-based.
  • - -
  • Array elements, starting at index zero, are initialized one-by-one -with the consecutive table elements, starting at either index -[0] or [1]. This process stops at the first -nil table element.
  • - -
  • If exactly one array element was initialized, it's repeated for -all the remaining elements. Otherwise all remaining uninitialized -elements are filled with zero bytes.
  • - -
  • The above logic only applies to arrays with a known fixed size. -A VLA is only initialized with the element(s) given in the table. -Depending on the use case, you may need to explicitly add a -NULL or 0 terminator to a VLA.
  • - -
  • A struct/union can be initialized in the -order of the declaration of its fields. Each field is initialized with -consecutive table elements, starting at either index [0] -or [1]. This process stops at the first nil table -element.
  • - -
  • Otherwise, if neither index [0] nor [1] is present, -a struct/union is initialized by looking up each field -name (as a string key) in the table. Each non-nil value is -used to initialize the corresponding field.
  • - -
  • Uninitialized fields of a struct are filled with zero -bytes, except for the trailing VLA of a VLS.
  • - -
  • Initialization of a union stops after one field has been -initialized. If no field has been initialized, the union is -filled with zero bytes.
  • - -
  • Elements or fields which are aggregates themselves are initialized -with a single initializer, but this may be a nested table -initializer (or a compatible aggregate).
  • - -
  • Excess initializers for an array cause an error. Excess -initializers for a struct/union are ignored. -Unrelated table entries are ignored, too.
  • - -
-

-Example: -

-
-local ffi = require("ffi")
-
-ffi.cdef[[
-struct foo { int a, b; };
-union bar { int i; double d; };
-struct nested { int x; struct foo y; };
-]]
-
-ffi.new("int[3]", {})            --> 0, 0, 0
-ffi.new("int[3]", {1})           --> 1, 1, 1
-ffi.new("int[3]", {1,2})         --> 1, 2, 0
-ffi.new("int[3]", {1,2,3})       --> 1, 2, 3
-ffi.new("int[3]", {[0]=1})       --> 1, 1, 1
-ffi.new("int[3]", {[0]=1,2})     --> 1, 2, 0
-ffi.new("int[3]", {[0]=1,2,3})   --> 1, 2, 3
-ffi.new("int[3]", {[0]=1,2,3,4}) --> error: too many initializers
-
-ffi.new("struct foo", {})            --> a = 0, b = 0
-ffi.new("struct foo", {1})           --> a = 1, b = 0
-ffi.new("struct foo", {1,2})         --> a = 1, b = 2
-ffi.new("struct foo", {[0]=1,2})     --> a = 1, b = 2
-ffi.new("struct foo", {b=2})         --> a = 0, b = 2
-ffi.new("struct foo", {a=1,b=2,c=3}) --> a = 1, b = 2  'c' is ignored
-
-ffi.new("union bar", {})        --> i = 0, d = 0.0
-ffi.new("union bar", {1})       --> i = 1, d = ?
-ffi.new("union bar", {[0]=1,2}) --> i = 1, d = ?    '2' is ignored
-ffi.new("union bar", {d=2})     --> i = ?, d = 2.0
-
-ffi.new("struct nested", {1,{2,3}})     --> x = 1, y.a = 2, y.b = 3
-ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3
-
- -

Operations on cdata Objects

-

-All standard Lua operators can be applied to cdata objects or a -mix of a cdata object and another Lua object. The following list shows -the predefined operations. -

-

-Reference types are dereferenced before performing each of -the operations below — the operation is applied to the -C type pointed to by the reference. -

-

-The predefined operations are always tried first before deferring to a -metamethod or index table (if any) for the corresponding ctype (except -for __new). An error is raised if the metamethod lookup or -index table lookup fails. -

- -

Indexing a cdata object

-
    - -
  • Indexing a pointer/array: a cdata pointer/array can be -indexed by a cdata number or a Lua number. The element address is -computed as the base address plus the number value multiplied by the -element size in bytes. A read access loads the element value and -converts it to a Lua object. A write -access converts a Lua object to the element -type and stores the converted value to the element. An error is -raised if the element size is undefined or a write access to a -constant element is attempted.
  • - -
  • Dereferencing a struct/union field: a -cdata struct/union or a pointer to a -struct/union can be dereferenced by a string key, -giving the field name. The field address is computed as the base -address plus the relative offset of the field. A read access loads the -field value and converts it to a Lua -object. A write access converts a Lua -object to the field type and stores the converted value to the -field. An error is raised if a write access to a constant -struct/union or a constant field is attempted. -Scoped enum constants or static constants are treated like a constant -field.
  • - -
  • Indexing a complex number: a complex number can be indexed -either by a cdata number or a Lua number with the values 0 or 1, or by -the strings "re" or "im". A read access loads the -real part ([0], .re) or the imaginary part -([1], .im) part of a complex number and -converts it to a Lua number. The -sub-parts of a complex number are immutable — assigning to an -index of a complex number raises an error. Accessing out-of-bound -indexes returns unspecified results, but is guaranteed not to trigger -memory access violations.
  • - -
  • Indexing a vector: a vector is treated like an array for -indexing purposes, except the vector elements are immutable — -assigning to an index of a vector raises an error.
  • - -
-

-A ctype object can be indexed with a string key, too. The only -predefined operation is reading scoped constants of -struct/union types. All other accesses defer -to the corresponding metamethods or index tables (if any). -

-

-Note: since there's (deliberately) no address-of operator, a cdata -object holding a value type is effectively immutable after -initialization. The JIT compiler benefits from this fact when applying -certain optimizations. -

-

-As a consequence, the elements of complex numbers and -vectors are immutable. But the elements of an aggregate holding these -types may be modified, of course. I.e. you cannot assign to -foo.c.im, but you can assign a (newly created) complex number -to foo.c. -

-

-The JIT compiler implements strict aliasing rules: accesses to different -types do not alias, except for differences in signedness (this -applies even to char pointers, unlike C99). Type punning -through unions is explicitly detected and allowed. -

- -

Calling a cdata object

-
    - -
  • Constructor: a ctype object can be called and used as a -constructor. This is equivalent -to ffi.new(ct, ...), unless a __new metamethod is -defined. The __new metamethod is called with the ctype object -plus any other arguments passed to the constructor. Note that you have to -use ffi.new inside the metamethod, since calling ct(...) -would cause infinite recursion.
  • - -
  • C function call: a cdata function or cdata function -pointer can be called. The passed arguments are -converted to the C types of the -parameters given by the function declaration. Arguments passed to the -variable argument part of vararg C function use -special conversion rules. This -C function is called and the return value (if any) is -converted to a Lua object.
    -On Windows/x86 systems, __stdcall functions are automatically -detected, and a function declared as __cdecl (the default) is -silently fixed up after the first call.
  • - -
- -

Arithmetic on cdata objects

-
    - -
  • Pointer arithmetic: a cdata pointer/array and a cdata -number or a Lua number can be added or subtracted. The number must be -on the right-hand side for a subtraction. The result is a pointer of -the same type with an address plus or minus the number value -multiplied by the element size in bytes. An error is raised if the -element size is undefined.
  • - -
  • Pointer difference: two compatible cdata pointers/arrays -can be subtracted. The result is the difference between their -addresses, divided by the element size in bytes. An error is raised if -the element size is undefined or zero.
  • - -
  • 64 bit integer arithmetic: the standard arithmetic -operators (+ - * / % ^ and unary -minus) can be applied to two cdata numbers, or a cdata number and a -Lua number. If one of them is an uint64_t, the other side is -converted to an uint64_t and an unsigned arithmetic operation -is performed. Otherwise, both sides are converted to an -int64_t and a signed arithmetic operation is performed. The -result is a boxed 64 bit cdata object.
    - -If one of the operands is an enum and the other operand is a -string, the string is converted to the value of a matching enum -constant before the above conversion.
    - -These rules ensure that 64 bit integers are "sticky". Any -expression involving at least one 64 bit integer operand results -in another one. The undefined cases for the division, modulo and power -operators return 2LL ^ 63 or -2ULL ^ 63.
    - -You'll have to explicitly convert a 64 bit integer to a Lua -number (e.g. for regular floating-point calculations) with -tonumber(). But note this may incur a precision loss.
  • - -
  • 64 bit bitwise operations: the rules for 64 bit -arithmetic operators apply analogously.
    - -Unlike the other bit.* operations, bit.tobit() -converts a cdata number via int64_t to int32_t and -returns a Lua number.
    - -For bit.band(), bit.bor() and bit.bxor(), the -conversion to int64_t or uint64_t applies to -all arguments, if any argument is a cdata number.
    - -For all other operations, only the first argument is used to determine -the output type. This implies that a cdata number as a shift count for -shifts and rotates is accepted, but that alone does not cause -a cdata number output. - -
- -

Comparisons of cdata objects

-
    - -
  • Pointer comparison: two compatible cdata pointers/arrays -can be compared. The result is the same as an unsigned comparison of -their addresses. nil is treated like a NULL pointer, -which is compatible with any other pointer type.
  • - -
  • 64 bit integer comparison: two cdata numbers, or a -cdata number and a Lua number can be compared with each other. If one -of them is an uint64_t, the other side is converted to an -uint64_t and an unsigned comparison is performed. Otherwise, -both sides are converted to an int64_t and a signed -comparison is performed.
    - -If one of the operands is an enum and the other operand is a -string, the string is converted to the value of a matching enum -constant before the above conversion.
    - -
  • Comparisons for equality/inequality never raise an error. -Even incompatible pointers can be compared for equality by address. Any -other incompatible comparison (also with non-cdata objects) treats the -two sides as unequal.
  • - -
- -

cdata objects as table keys

-

-Lua tables may be indexed by cdata objects, but this doesn't provide -any useful semantics — cdata objects are unsuitable as table -keys! -

-

-A cdata object is treated like any other garbage-collected object and -is hashed and compared by its address for table indexing. Since -there's no interning for cdata value types, the same value may be -boxed in different cdata objects with different addresses. Thus, -t[1LL+1LL] and t[2LL] usually do not point to -the same hash slot, and they certainly do not point to the same -hash slot as t[2]. -

-

-It would seriously drive up implementation complexity and slow down -the common case, if one were to add extra handling for by-value -hashing and comparisons to Lua tables. Given the ubiquity of their use -inside the VM, this is not acceptable. -

-

-There are three viable alternatives, if you really need to use cdata -objects as keys: -

-
    - -
  • If you can get by with the precision of Lua numbers -(52 bits), then use tonumber() on a cdata number or -combine multiple fields of a cdata aggregate to a Lua number. Then use -the resulting Lua number as a key when indexing tables.
    -One obvious benefit: t[tonumber(2LL)] does point to -the same slot as t[2].
  • - -
  • Otherwise, use either tostring() on 64 bit integers -or complex numbers or combine multiple fields of a cdata aggregate to -a Lua string (e.g. with -ffi.string()). Then -use the resulting Lua string as a key when indexing tables.
  • - -
  • Create your own specialized hash table implementation using the -C types provided by the FFI library, just like you would in -C code. Ultimately, this may give much better performance than the -other alternatives or what a generic by-value hash table could -possibly provide.
  • - -
- -

Parameterized Types

-

-To facilitate some abstractions, the two functions -ffi.typeof and -ffi.cdef support -parameterized types in C declarations. Note: none of the other API -functions taking a cdecl allow this. -

-

-Any place you can write a typedef name, an -identifier or a number in a declaration, you can write -$ (the dollar sign) instead. These placeholders are replaced in -order of appearance with the arguments following the cdecl string: -

-
--- Declare a struct with a parameterized field type and name:
-ffi.cdef([[
-typedef struct { $ $; } foo_t;
-]], type1, name1)
-
--- Anonymous struct with dynamic names:
-local bar_t = ffi.typeof("struct { int $, $; }", name1, name2)
--- Derived pointer type:
-local bar_ptr_t = ffi.typeof("$ *", bar_t)
-
--- Parameterized dimensions work even where a VLA won't work:
-local matrix_t = ffi.typeof("uint8_t[$][$]", width, height)
-
-

-Caveat: this is not simple text substitution! A passed ctype or -cdata object is treated like the underlying type, a passed string is -considered an identifier and a number is considered a number. You must -not mix this up: e.g. passing "int" as a string doesn't work in -place of a type, you'd need to use ffi.typeof("int") instead. -

-

-The main use for parameterized types are libraries implementing abstract -data types -(» example), -similar to what can be achieved with C++ template metaprogramming. -Another use case are derived types of anonymous structs, which avoids -pollution of the global struct namespace. -

-

-Please note that parameterized types are a nice tool and indispensable -for certain use cases. But you'll want to use them sparingly in regular -code, e.g. when all types are actually fixed. -

- -

Garbage Collection of cdata Objects

-

-All explicitly (ffi.new(), ffi.cast() etc.) or -implicitly (accessors) created cdata objects are garbage collected. -You need to ensure to retain valid references to cdata objects -somewhere on a Lua stack, an upvalue or in a Lua table while they are -still in use. Once the last reference to a cdata object is gone, the -garbage collector will automatically free the memory used by it (at -the end of the next GC cycle). -

-

-Please note, that pointers themselves are cdata objects, however they -are not followed by the garbage collector. So e.g. if you -assign a cdata array to a pointer, you must keep the cdata object -holding the array alive as long as the pointer is still in use: -

-
-ffi.cdef[[
-typedef struct { int *a; } foo_t;
-]]
-
-local s = ffi.new("foo_t", ffi.new("int[10]")) -- WRONG!
-
-local a = ffi.new("int[10]") -- OK
-local s = ffi.new("foo_t", a)
--- Now do something with 's', but keep 'a' alive until you're done.
-
-

-Similar rules apply for Lua strings which are implicitly converted to -"const char *": the string object itself must be -referenced somewhere or it'll be garbage collected eventually. The -pointer will then point to stale data, which may have already been -overwritten. Note that string literals are automatically kept -alive as long as the function containing it (actually its prototype) -is not garbage collected. -

-

-Objects which are passed as an argument to an external C function -are kept alive until the call returns. So it's generally safe to -create temporary cdata objects in argument lists. This is a common -idiom for passing specific C types to -vararg functions. -

-

-Memory areas returned by C functions (e.g. from malloc()) -must be manually managed, of course (or use -ffi.gc()). Pointers to -cdata objects are indistinguishable from pointers returned by C -functions (which is one of the reasons why the GC cannot follow them). -

- -

Callbacks

-

-The LuaJIT FFI automatically generates special callback functions -whenever a Lua function is converted to a C function pointer. This -associates the generated callback function pointer with the C type -of the function pointer and the Lua function object (closure). -

-

-This can happen implicitly due to the usual conversions, e.g. when -passing a Lua function to a function pointer argument. Or, you can use -ffi.cast() to explicitly cast a Lua function to a -C function pointer. -

-

-Currently, only certain C function types can be used as callback -functions. Neither C vararg functions nor functions with -pass-by-value aggregate argument or result types are supported. There -are no restrictions on the kind of Lua functions that can be called -from the callback — no checks for the proper number of arguments -are made. The return value of the Lua function will be converted to the -result type, and an error will be thrown for invalid conversions. -

-

-It's allowed to throw errors across a callback invocation, but it's not -advisable in general. Do this only if you know the C function, that -called the callback, copes with the forced stack unwinding and doesn't -leak resources. -

-

-One thing that's not allowed, is to let an FFI call into a C function -get JIT-compiled, which in turn calls a callback, calling into Lua again. -Usually this attempt is caught by the interpreter first and the -C function is blacklisted for compilation. -

-

-However, this heuristic may fail under specific circumstances: e.g. a -message polling function might not run Lua callbacks right away and the call -gets JIT-compiled. If it later happens to call back into Lua (e.g. a rarely -invoked error callback), you'll get a VM PANIC with the message -"bad callback". Then you'll need to manually turn off -JIT-compilation with -jit.off() for the -surrounding Lua function that invokes such a message polling function (or -similar). -

- -

Callback resource handling

-

-Callbacks take up resources — you can only have a limited number -of them at the same time (500 - 1000, depending on the -architecture). The associated Lua functions are anchored to prevent -garbage collection, too. -

-

-Callbacks due to implicit conversions are permanent! There is no -way to guess their lifetime, since the C side might store the -function pointer for later use (typical for GUI toolkits). The associated -resources cannot be reclaimed until termination: -

-
-ffi.cdef[[
-typedef int (__stdcall *WNDENUMPROC)(void *hwnd, intptr_t l);
-int EnumWindows(WNDENUMPROC func, intptr_t l);
-]]
-
--- Implicit conversion to a callback via function pointer argument.
-local count = 0
-ffi.C.EnumWindows(function(hwnd, l)
-  count = count + 1
-  return true
-end, 0)
--- The callback is permanent and its resources cannot be reclaimed!
--- Ok, so this may not be a problem, if you do this only once.
-
-

-Note: this example shows that you must properly declare -__stdcall callbacks on Windows/x86 systems. The calling -convention cannot be automatically detected, unlike for -__stdcall calls to Windows functions. -

-

-For some use cases, it's necessary to free up the resources or to -dynamically redirect callbacks. Use an explicit cast to a -C function pointer and keep the resulting cdata object. Then use -the cb:free() -or cb:set() methods -on the cdata object: -

-
--- Explicitly convert to a callback via cast.
-local count = 0
-local cb = ffi.cast("WNDENUMPROC", function(hwnd, l)
-  count = count + 1
-  return true
-end)
-
--- Pass it to a C function.
-ffi.C.EnumWindows(cb, 0)
--- EnumWindows doesn't need the callback after it returns, so free it.
-
-cb:free()
--- The callback function pointer is no longer valid and its resources
--- will be reclaimed. The created Lua closure will be garbage collected.
-
- -

Callback performance

-

-Callbacks are slow! First, the C to Lua transition itself -has an unavoidable cost, similar to a lua_call() or -lua_pcall(). Argument and result marshalling add to that cost. -And finally, neither the C compiler nor LuaJIT can inline or -optimize across the language barrier and hoist repeated computations out -of a callback function. -

-

-Do not use callbacks for performance-sensitive work: e.g. consider a -numerical integration routine which takes a user-defined function to -integrate over. It's a bad idea to call a user-defined Lua function from -C code millions of times. The callback overhead will be absolutely -detrimental for performance. -

-

-It's considerably faster to write the numerical integration routine -itself in Lua — the JIT compiler will be able to inline the -user-defined function and optimize it together with its calling context, -with very competitive performance. -

-

-As a general guideline: use callbacks only when you must, because -of existing C APIs. E.g. callback performance is irrelevant for a -GUI application, which waits for user input most of the time, anyway. -

-

-For new designs avoid push-style APIs: a C function repeatedly -calling a callback for each result. Instead, use pull-style APIs: -call a C function repeatedly to get a new result. Calls from Lua -to C via the FFI are much faster than the other way round. Most well-designed -libraries already use pull-style APIs (read/write, get/put). -

- -

C Library Namespaces

-

-A C library namespace is a special kind of object which allows -access to the symbols contained in shared libraries or the default -symbol namespace. The default -ffi.C namespace is -automatically created when the FFI library is loaded. C library -namespaces for specific shared libraries may be created with the -ffi.load() API -function. -

-

-Indexing a C library namespace object with a symbol name (a Lua -string) automatically binds it to the library. First, the symbol type -is resolved — it must have been declared with -ffi.cdef. Then the -symbol address is resolved by searching for the symbol name in the -associated shared libraries or the default symbol namespace. Finally, -the resulting binding between the symbol name, the symbol type and its -address is cached. Missing symbol declarations or nonexistent symbol -names cause an error. -

-

-This is what happens on a read access for the different kinds of -symbols: -

-
    - -
  • External functions: a cdata object with the type of the function -and its address is returned.
  • - -
  • External variables: the symbol address is dereferenced and the -loaded value is converted to a Lua object -and returned.
  • - -
  • Constant values (static const or enum -constants): the constant is converted to a -Lua object and returned.
  • - -
-

-This is what happens on a write access: -

-
    - -
  • External variables: the value to be written is -converted to the C type of the -variable and then stored at the symbol address.
  • - -
  • Writing to constant variables or to any other symbol type causes -an error, like any other attempted write to a constant location.
  • - -
-

-C library namespaces themselves are garbage collected objects. If -the last reference to the namespace object is gone, the garbage -collector will eventually release the shared library reference and -remove all memory associated with the namespace. Since this may -trigger the removal of the shared library from the memory of the -running process, it's generally not safe to use function -cdata objects obtained from a library if the namespace object may be -unreferenced. -

-

-Performance notice: the JIT compiler specializes to the identity of -namespace objects and to the strings used to index it. This -effectively turns function cdata objects into constants. It's not -useful and actually counter-productive to explicitly cache these -function objects, e.g. local strlen = ffi.C.strlen. OTOH, it -is useful to cache the namespace itself, e.g. local C = -ffi.C. -

- -

No Hand-holding!

-

-The FFI library has been designed as a low-level library. The -goal is to interface with C code and C data types with a -minimum of overhead. This means you can do anything you can do -from C: access all memory, overwrite anything in memory, call -machine code at any memory address and so on. -

-

-The FFI library provides no memory safety, unlike regular Lua -code. It will happily allow you to dereference a NULL -pointer, to access arrays out of bounds or to misdeclare -C functions. If you make a mistake, your application might crash, -just like equivalent C code would. -

-

-This behavior is inevitable, since the goal is to provide full -interoperability with C code. Adding extra safety measures, like -bounds checks, would be futile. There's no way to detect -misdeclarations of C functions, since shared libraries only -provide symbol names, but no type information. Likewise, there's no way -to infer the valid range of indexes for a returned pointer. -

-

-Again: the FFI library is a low-level library. This implies it needs -to be used with care, but it's flexibility and performance often -outweigh this concern. If you're a C or C++ developer, it'll be easy -to apply your existing knowledge. OTOH, writing code for the FFI -library is not for the faint of heart and probably shouldn't be the -first exercise for someone with little experience in Lua, C or C++. -

-

-As a corollary of the above, the FFI library is not safe for use by -untrusted Lua code. If you're sandboxing untrusted Lua code, you -definitely don't want to give this code access to the FFI library or -to any cdata object (except 64 bit integers or complex -numbers). Any properly engineered Lua sandbox needs to provide safety -wrappers for many of the standard Lua library functions — -similar wrappers need to be written for high-level operations on FFI -data types, too. -

- -

Current Status

-

-The initial release of the FFI library has some limitations and is -missing some features. Most of these will be fixed in future releases. -

-

-C language support is -currently incomplete: -

-
    -
  • C declarations are not passed through a C pre-processor, -yet.
  • -
  • The C parser is able to evaluate most constant expressions -commonly found in C header files. However, it doesn't handle the -full range of C expression semantics and may fail for some -obscure constructs.
  • -
  • static const declarations only work for integer types -up to 32 bits. Neither declaring string constants nor -floating-point constants is supported.
  • -
  • Packed struct bitfields that cross container boundaries -are not implemented.
  • -
  • Native vector types may be defined with the GCC mode or -vector_size attribute. But no operations other than loading, -storing and initializing them are supported, yet.
  • -
  • The volatile type qualifier is currently ignored by -compiled code.
  • -
  • ffi.cdef silently -ignores most re-declarations. Note: avoid re-declarations which do not -conform to C99. The implementation will eventually be changed to -perform strict checks.
  • -
-

-The JIT compiler already handles a large subset of all FFI operations. -It automatically falls back to the interpreter for unimplemented -operations (you can check for this with the --jv command line option). -The following operations are currently not compiled and may exhibit -suboptimal performance, especially when used in inner loops: -

-
    -
  • Vector operations.
  • -
  • Table initializers.
  • -
  • Initialization of nested struct/union types.
  • -
  • Non-default initialization of VLA/VLS or large C types -(> 128 bytes or > 16 array elements).
  • -
  • Bitfield initializations.
  • -
  • Pointer differences for element sizes that are not a power of -two.
  • -
  • Calls to C functions with aggregates passed or returned by -value.
  • -
  • Calls to ctype metamethods which are not plain functions.
  • -
  • ctype __newindex tables and non-string lookups in ctype -__index tables.
  • -
  • tostring() for cdata types.
  • -
  • Calls to ffi.cdef(), ffi.load() and -ffi.metatype().
  • -
-

-Other missing features: -

-
    -
  • Arithmetic for complex numbers.
  • -
  • Passing structs by value to vararg C functions.
  • -
  • C++ exception interoperability -does not extend to C functions called via the FFI, if the call is -compiled.
  • -
-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_tutorial.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_tutorial.html deleted file mode 100644 index ff0c3a9..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_ffi_tutorial.html +++ /dev/null @@ -1,597 +0,0 @@ - - - -FFI Tutorial - - - - - - - - -
-Lua -
- - -
-

-This page is intended to give you an overview of the features of the FFI -library by presenting a few use cases and guidelines. -

-

-This page makes no attempt to explain all of the FFI library, though. -You'll want to have a look at the ffi.* API -function reference and the FFI -semantics to learn more. -

- -

Loading the FFI Library

-

-The FFI library is built into LuaJIT by default, but it's not loaded -and initialized by default. The suggested way to use the FFI library -is to add the following to the start of every Lua file that needs one -of its functions: -

-
-local ffi = require("ffi")
-
-

-Please note, this doesn't define an ffi variable in the table -of globals — you really need to use the local variable. The -require function ensures the library is only loaded once. -

-

-Note: If you want to experiment with the FFI from the interactive prompt -of the command line executable, omit the local, as it doesn't -preserve local variables across lines. -

- -

Accessing Standard System Functions

-

-The following code explains how to access standard system functions. -We slowly print two lines of dots by sleeping for 10 milliseconds -after each dot: -

-
- 
-①
-
-
-
-
-
-②
-③
-④
-
-
-
-⑤
-
-
-
-
-
-⑥local ffi = require("ffi")
-ffi.cdef[[
-void Sleep(int ms);
-int poll(struct pollfd *fds, unsigned long nfds, int timeout);
-]]
-
-local sleep
-if ffi.os == "Windows" then
-  function sleep(s)
-    ffi.C.Sleep(s*1000)
-  end
-else
-  function sleep(s)
-    ffi.C.poll(nil, 0, s*1000)
-  end
-end
-
-for i=1,160 do
-  io.write("."); io.flush()
-  sleep(0.01)
-end
-io.write("\n")
-
-

-Here's the step-by-step explanation: -

-

- This defines the -C library functions we're going to use. The part inside the -double-brackets (in green) is just standard C syntax. You can -usually get this info from the C header files or the -documentation provided by each C library or C compiler. -

-

- The difficulty we're -facing here, is that there are different standards to choose from. -Windows has a simple Sleep() function. On other systems there -are a variety of functions available to achieve sub-second sleeps, but -with no clear consensus. Thankfully poll() can be used for -this task, too, and it's present on most non-Windows systems. The -check for ffi.os makes sure we use the Windows-specific -function only on Windows systems. -

-

- Here we're wrapping the -call to the C function in a Lua function. This isn't strictly -necessary, but it's helpful to deal with system-specific issues only -in one part of the code. The way we're wrapping it ensures the check -for the OS is only done during initialization and not for every call. -

-

- A more subtle point is -that we defined our sleep() function (for the sake of this -example) as taking the number of seconds, but accepting fractional -seconds. Multiplying this by 1000 gets us milliseconds, but that still -leaves it a Lua number, which is a floating-point value. Alas, the -Sleep() function only accepts an integer value. Luckily for -us, the FFI library automatically performs the conversion when calling -the function (truncating the FP value towards zero, like in C). -

-

-Some readers will notice that Sleep() is part of -KERNEL32.DLL and is also a stdcall function. So how -can this possibly work? The FFI library provides the ffi.C -default C library namespace, which allows calling functions from -the default set of libraries, like a C compiler would. Also, the -FFI library automatically detects stdcall functions, so you -don't need to declare them as such. -

-

- The poll() -function takes a couple more arguments we're not going to use. You can -simply use nil to pass a NULL pointer and 0 -for the nfds parameter. Please note, that the -number 0 does not convert to a pointer value, -unlike in C++. You really have to pass pointers to pointer arguments -and numbers to number arguments. -

-

-The page on FFI semantics has all -of the gory details about -conversions between Lua -objects and C types. For the most part you don't have to deal -with this, as it's performed automatically and it's carefully designed -to bridge the semantic differences between Lua and C. -

-

- Now that we have defined -our own sleep() function, we can just call it from plain Lua -code. That wasn't so bad, huh? Turning these boring animated dots into -a fascinating best-selling game is left as an exercise for the reader. -:-) -

- -

Accessing the zlib Compression Library

-

-The following code shows how to access the » zlib compression library from Lua code. -We'll define two convenience wrapper functions that take a string and -compress or uncompress it to another string: -

-
- 
-①
-
-
-
-
-
-
-②
-
-
-③
-
-④
-
-
-⑤
-
-
-⑥
-
-
-
-
-
-
-
-⑦local ffi = require("ffi")
-ffi.cdef[[
-unsigned long compressBound(unsigned long sourceLen);
-int compress2(uint8_t *dest, unsigned long *destLen,
-	      const uint8_t *source, unsigned long sourceLen, int level);
-int uncompress(uint8_t *dest, unsigned long *destLen,
-	       const uint8_t *source, unsigned long sourceLen);
-]]
-local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z")
-
-local function compress(txt)
-  local n = zlib.compressBound(#txt)
-  local buf = ffi.new("uint8_t[?]", n)
-  local buflen = ffi.new("unsigned long[1]", n)
-  local res = zlib.compress2(buf, buflen, txt, #txt, 9)
-  assert(res == 0)
-  return ffi.string(buf, buflen[0])
-end
-
-local function uncompress(comp, n)
-  local buf = ffi.new("uint8_t[?]", n)
-  local buflen = ffi.new("unsigned long[1]", n)
-  local res = zlib.uncompress(buf, buflen, comp, #comp)
-  assert(res == 0)
-  return ffi.string(buf, buflen[0])
-end
-
--- Simple test code.
-local txt = string.rep("abcd", 1000)
-print("Uncompressed size: ", #txt)
-local c = compress(txt)
-print("Compressed size: ", #c)
-local txt2 = uncompress(c, #txt)
-assert(txt2 == txt)
-
-

-Here's the step-by-step explanation: -

-

- This defines some of the -C functions provided by zlib. For the sake of this example, some -type indirections have been reduced and it uses the predefined -fixed-size integer types, while still adhering to the zlib API/ABI. -

-

- This loads the zlib shared -library. On POSIX systems, it's named libz.so and usually -comes pre-installed. Since ffi.load() automatically adds any -missing standard prefixes/suffixes, we can simply load the -"z" library. On Windows it's named zlib1.dll and -you'll have to download it first from the -» zlib site. The check for -ffi.os makes sure we pass the right name to -ffi.load(). -

-

- First, the maximum size of -the compression buffer is obtained by calling the -zlib.compressBound function with the length of the -uncompressed string. The next line allocates a byte buffer of this -size. The [?] in the type specification indicates a -variable-length array (VLA). The actual number of elements of this -array is given as the 2nd argument to ffi.new(). -

-

- This may look strange at -first, but have a look at the declaration of the compress2 -function from zlib: the destination length is defined as a pointer! -This is because you pass in the maximum buffer size and get back the -actual length that was used. -

-

-In C you'd pass in the address of a local variable -(&buflen). But since there's no address-of operator in -Lua, we'll just pass in a one-element array. Conveniently, it can be -initialized with the maximum buffer size in one step. Calling the -actual zlib.compress2 function is then straightforward. -

-

- We want to return the -compressed data as a Lua string, so we'll use ffi.string(). -It needs a pointer to the start of the data and the actual length. The -length has been returned in the buflen array, so we'll just -get it from there. -

-

-Note that since the function returns now, the buf and -buflen variables will eventually be garbage collected. This -is fine, because ffi.string() has copied the contents to a -newly created (interned) Lua string. If you plan to call this function -lots of times, consider reusing the buffers and/or handing back the -results in buffers instead of strings. This will reduce the overhead -for garbage collection and string interning. -

-

- The uncompress -functions does the exact opposite of the compress function. -The compressed data doesn't include the size of the original string, -so this needs to be passed in. Otherwise, no surprises here. -

-

- The code, that makes use -of the functions we just defined, is just plain Lua code. It doesn't -need to know anything about the LuaJIT FFI — the convenience -wrapper functions completely hide it. -

-

-One major advantage of the LuaJIT FFI is that you are now able to -write those wrappers in Lua. And at a fraction of the time it -would cost you to create an extra C module using the Lua/C API. -Many of the simpler C functions can probably be used directly -from your Lua code, without any wrappers. -

-

-Side note: the zlib API uses the long type for passing -lengths and sizes around. But all those zlib functions actually only -deal with 32 bit values. This is an unfortunate choice for a -public API, but may be explained by zlib's history — we'll just -have to deal with it. -

-

-First, you should know that a long is a 64 bit type e.g. -on POSIX/x64 systems, but a 32 bit type on Windows/x64 and on -32 bit systems. Thus a long result can be either a plain -Lua number or a boxed 64 bit integer cdata object, depending on -the target system. -

-

-Ok, so the ffi.* functions generally accept cdata objects -wherever you'd want to use a number. That's why we get a away with -passing n to ffi.string() above. But other Lua -library functions or modules don't know how to deal with this. So for -maximum portability, one needs to use tonumber() on returned -long results before passing them on. Otherwise the -application might work on some systems, but would fail in a POSIX/x64 -environment. -

- -

Defining Metamethods for a C Type

-

-The following code explains how to define metamethods for a C type. -We define a simple point type and add some operations to it: -

-
- 
-①
-
-
-
-②
-
-③
-
-④
-
-
-
-⑤
-
-⑥local ffi = require("ffi")
-ffi.cdef[[
-typedef struct { double x, y; } point_t;
-]]
-
-local point
-local mt = {
-  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
-  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
-  __index = {
-    area = function(a) return a.x*a.x + a.y*a.y end,
-  },
-}
-point = ffi.metatype("point_t", mt)
-
-local a = point(3, 4)
-print(a.x, a.y)  --> 3  4
-print(#a)        --> 5
-print(a:area())  --> 25
-local b = a + point(0.5, 8)
-print(#b)        --> 12.5
-
-

-Here's the step-by-step explanation: -

-

- This defines the C type for a -two-dimensional point object. -

-

- We have to declare the variable -holding the point constructor first, because it's used inside of a -metamethod. -

-

- Let's define an __add -metamethod which adds the coordinates of two points and creates a new -point object. For simplicity, this function assumes that both arguments -are points. But it could be any mix of objects, if at least one operand -is of the required type (e.g. adding a point plus a number or vice -versa). Our __len metamethod returns the distance of a point to -the origin. -

-

- If we run out of operators, we can -define named methods, too. Here, the __index table defines an -area function. For custom indexing needs, one might want to -define __index and __newindex functions instead. -

-

- This associates the metamethods with -our C type. This only needs to be done once. For convenience, a -constructor is returned by -ffi.metatype(). -We're not required to use it, though. The original C type can still -be used e.g. to create an array of points. The metamethods automatically -apply to any and all uses of this type. -

-

-Please note, that the association with a metatable is permanent and -the metatable must not be modified afterwards! Ditto for the -__index table. -

-

- Here are some simple usage examples -for the point type and their expected results. The predefined -operations (such as a.x) can be freely mixed with the newly -defined metamethods. Note that area is a method and must be -called with the Lua syntax for methods: a:area(), not -a.area(). -

-

-The C type metamethod mechanism is most useful when used in -conjunction with C libraries that are written in an object-oriented -style. Creators return a pointer to a new instance, and methods take an -instance pointer as the first argument. Sometimes you can just point -__index to the library namespace and __gc to the -destructor and you're done. But often enough you'll want to add -convenience wrappers, e.g. to return actual Lua strings or when -returning multiple values. -

-

-Some C libraries only declare instance pointers as an opaque -void * type. In this case you can use a fake type for all -declarations, e.g. a pointer to a named (incomplete) struct will do: -typedef struct foo_type *foo_handle. The C side doesn't -know what you declare with the LuaJIT FFI, but as long as the underlying -types are compatible, everything still works. -

- -

Translating C Idioms

-

-Here's a list of common C idioms and their translation to the -LuaJIT FFI: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IdiomC codeLua code
Pointer dereference
int *p;
x = *p;
*p = y;
x = p[0]
p[0] = y
Pointer indexing
int i, *p;
x = p[i];
p[i+1] = y;
x = p[i]
p[i+1] = y
Array indexing
int i, a[];
x = a[i];
a[i+1] = y;
x = a[i]
a[i+1] = y
struct/union dereference
struct foo s;
x = s.field;
s.field = y;
x = s.field
s.field = y
struct/union pointer deref.
struct foo *sp;
x = sp->field;
sp->field = y;
x = s.field
s.field = y
Pointer arithmetic
int i, *p;
x = p + i;
y = p - i;
x = p + i
y = p - i
Pointer difference
int *p1, *p2;
x = p1 - p2;x = p1 - p2
Array element pointer
int i, a[];
x = &a[i];x = a+i
Cast pointer to address
int *p;
x = (intptr_t)p;x = tonumber(
 ffi.cast("intptr_t",
          p))
Functions with outargs
void foo(int *inoutlen);
int len = x;
foo(&len);
y = len;
local len =
  ffi.new("int[1]", x)
foo(len)
y = len[0]
Vararg conversions
int printf(char *fmt, ...);
printf("%g", 1.0);
printf("%d", 1);
 
printf("%g", 1);
printf("%d",
  ffi.new("int", 1))
- -

To Cache or Not to Cache

-

-It's a common Lua idiom to cache library functions in local variables -or upvalues, e.g.: -

-
-local byte, char = string.byte, string.char
-local function foo(x)
-  return char(byte(x)+1)
-end
-
-

-This replaces several hash-table lookups with a (faster) direct use of -a local or an upvalue. This is less important with LuaJIT, since the -JIT compiler optimizes hash-table lookups a lot and is even able to -hoist most of them out of the inner loops. It can't eliminate -all of them, though, and it saves some typing for often-used -functions. So there's still a place for this, even with LuaJIT. -

-

-The situation is a bit different with C function calls via the -FFI library. The JIT compiler has special logic to eliminate all -of the lookup overhead for functions resolved from a -C library namespace! -Thus it's not helpful and actually counter-productive to cache -individual C functions like this: -

-
-local funca, funcb = ffi.C.funca, ffi.C.funcb -- Not helpful!
-local function foo(x, n)
-  for i=1,n do funcb(funca(x, i), 1) end
-end
-
-

-This turns them into indirect calls and generates bigger and slower -machine code. Instead, you'll want to cache the namespace itself and -rely on the JIT compiler to eliminate the lookups: -

-
-local C = ffi.C          -- Instead use this!
-local function foo(x, n)
-  for i=1,n do C.funcb(C.funca(x, i), 1) end
-end
-
-

-This generates both shorter and faster code. So don't cache -C functions, but do cache namespaces! Most often the -namespace is already in a local variable at an outer scope, e.g. from -local lib = ffi.load(...). Note that copying -it to a local variable in the function scope is unnecessary. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_jit.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_jit.html deleted file mode 100644 index 3ff5c05..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_jit.html +++ /dev/null @@ -1,195 +0,0 @@ - - - -jit.* Library - - - - - - - -
-Lua -
- - -
-

-The functions in this built-in module control the behavior of the JIT -compiler engine. Note that JIT-compilation is fully automatic — -you probably won't need to use any of the following functions unless -you have special needs. -

- -

jit.on()
-jit.off()

-

-Turns the whole JIT compiler on (default) or off. -

-

-These functions are typically used with the command line options --j on or -j off. -

- -

jit.flush()

-

-Flushes the whole cache of compiled code. -

- -

jit.on(func|true [,true|false])
-jit.off(func|true [,true|false])
-jit.flush(func|true [,true|false])

-

-jit.on enables JIT compilation for a Lua function (this is -the default). -

-

-jit.off disables JIT compilation for a Lua function and -flushes any already compiled code from the code cache. -

-

-jit.flush flushes the code, but doesn't affect the -enable/disable status. -

-

-The current function, i.e. the Lua function calling this library -function, can also be specified by passing true as the first -argument. -

-

-If the second argument is true, JIT compilation is also -enabled, disabled or flushed recursively for all sub-functions of a -function. With false only the sub-functions are affected. -

-

-The jit.on and jit.off functions only set a flag -which is checked when the function is about to be compiled. They do -not trigger immediate compilation. -

-

-Typical usage is jit.off(true, true) in the main chunk -of a module to turn off JIT compilation for the whole module for -debugging purposes. -

- -

jit.flush(tr)

-

-Flushes the root trace, specified by its number, and all of its side -traces from the cache. The code for the trace will be retained as long -as there are any other traces which link to it. -

- -

status, ... = jit.status()

-

-Returns the current status of the JIT compiler. The first result is -either true or false if the JIT compiler is turned -on or off. The remaining results are strings for CPU-specific features -and enabled optimizations. -

- -

jit.version

-

-Contains the LuaJIT version string. -

- -

jit.version_num

-

-Contains the version number of the LuaJIT core. Version xx.yy.zz -is represented by the decimal number xxyyzz. -

- -

jit.os

-

-Contains the target OS name: -"Windows", "Linux", "OSX", "BSD", "POSIX" or "Other". -

- -

jit.arch

-

-Contains the target architecture name: -"x86", "x64", "arm", "arm64", "arm64be", "ppc", "mips", "mipsel", "mips64", "mips64el", "mips64r6", "mips64r6el". -

- -

jit.opt.* — JIT compiler optimization control

-

-This submodule provides the backend for the -O command line -option. -

-

-You can also use it programmatically, e.g.: -

-
-jit.opt.start(2) -- same as -O2
-jit.opt.start("-dce")
-jit.opt.start("hotloop=10", "hotexit=2")
-
-

-Unlike in LuaJIT 1.x, the module is built-in and -optimization is turned on by default! -It's no longer necessary to run require("jit.opt").start(), -which was one of the ways to enable optimization. -

- -

jit.util.* — JIT compiler introspection

-

-This submodule holds functions to introspect the bytecode, generated -traces, the IR and the generated machine code. The functionality -provided by this module is still in flux and therefore undocumented. -

-

-The debug modules -jbc, -jv and -jdump make -extensive use of these functions. Please check out their source code, -if you want to know more. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_profiler.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_profiler.html deleted file mode 100644 index d6e26ef..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/ext_profiler.html +++ /dev/null @@ -1,359 +0,0 @@ - - - -Profiler - - - - - - - -
-Lua -
- - -
-

-LuaJIT has an integrated statistical profiler with very low overhead. It -allows sampling the currently executing stack and other parameters in -regular intervals. -

-

-The integrated profiler can be accessed from three levels: -

- - -

High-Level Profiler

-

-The bundled high-level profiler offers basic profiling functionality. It -generates simple textual summaries or source code annotations. It can be -accessed with the -jp command line option -or from Lua code by loading the underlying jit.p module. -

-

-To cut to the chase — run this to get a CPU usage profile by -function name: -

-
-luajit -jp myapp.lua
-
-

-It's not a stated goal of the bundled profiler to add every -possible option or to cater for special profiling needs. The low-level -profiler APIs are documented below. They may be used by third-party -authors to implement advanced functionality, e.g. IDE integration or -graphical profilers. -

-

-Note: Sampling works for both interpreted and JIT-compiled code. The -results for JIT-compiled code may sometimes be surprising. LuaJIT -heavily optimizes and inlines Lua code — there's no simple -one-to-one correspondence between source code lines and the sampled -machine code. -

- -

-jp=[options[,output]]

-

-The -jp command line option starts the high-level profiler. -When the application run by the command line terminates, the profiler -stops and writes the results to stdout or to the specified -output file. -

-

-The options argument specifies how the profiling is to be -performed: -

-
    -
  • f — Stack dump: function name, otherwise module:line. -This is the default mode.
  • -
  • F — Stack dump: ditto, but dump module:name.
  • -
  • l — Stack dump: module:line.
  • -
  • <number> — stack dump depth (callee ← -caller). Default: 1.
  • -
  • -<number> — Inverse stack dump depth (caller -→ callee).
  • -
  • s — Split stack dump after first stack level. Implies -depth ≥ 2 or depth ≤ -2.
  • -
  • p — Show full path for module names.
  • -
  • v — Show VM states.
  • -
  • z — Show zones.
  • -
  • r — Show raw sample counts. Default: show percentages.
  • -
  • a — Annotate excerpts from source code files.
  • -
  • A — Annotate complete source code files.
  • -
  • G — Produce raw output suitable for graphical tools.
  • -
  • m<number> — Minimum sample percentage to be shown. -Default: 3%.
  • -
  • i<number> — Sampling interval in milliseconds. -Default: 10ms.
    -Note: The actual sampling precision is OS-dependent.
  • -
-

-The default output for -jp is a list of the most CPU consuming -spots in the application. Increasing the stack dump depth with (say) --jp=2 may help to point out the main callers or callees of -hotspots. But sample aggregation is still flat per unique stack dump. -

-

-To get a two-level view (split view) of callers/callees, use --jp=s or -jp=-s. The percentages shown for the second -level are relative to the first level. -

-

-To see how much time is spent in each line relative to a function, use --jp=fl. -

-

-To see how much time is spent in different VM states or -zones, use -jp=v or -jp=z. -

-

-Combinations of v/z with f/F/l produce two-level -views, e.g. -jp=vf or -jp=fv. This shows the time -spent in a VM state or zone vs. hotspots. This can be used to answer -questions like "Which time-consuming functions are only interpreted?" or -"What's the garbage collector overhead for a specific function?". -

-

-Multiple options can be combined — but not all combinations make -sense, see above. E.g. -jp=3si4m1 samples three stack levels -deep in 4ms intervals and shows a split view of the CPU consuming -functions and their callers with a 1% threshold. -

-

-Source code annotations produced by -jp=a or -jp=A are -always flat and at the line level. Obviously, the source code files need -to be readable by the profiler script. -

-

-The high-level profiler can also be started and stopped from Lua code with: -

-
-require("jit.p").start(options, output)
-...
-require("jit.p").stop()
-
- -

jit.zone — Zones

-

-Zones can be used to provide information about different parts of an -application to the high-level profiler. E.g. a game could make use of an -"AI" zone, a "PHYS" zone, etc. Zones are hierarchical, -organized as a stack. -

-

-The jit.zone module needs to be loaded explicitly: -

-
-local zone = require("jit.zone")
-
-
    -
  • zone("name") pushes a named zone to the zone stack.
  • -
  • zone() pops the current zone from the zone stack and -returns its name.
  • -
  • zone:get() returns the current zone name or nil.
  • -
  • zone:flush() flushes the zone stack.
  • -
-

-To show the time spent in each zone use -jp=z. To show the time -spent relative to hotspots use e.g. -jp=zf or -jp=fz. -

- -

Low-level Lua API

-

-The jit.profile module gives access to the low-level API of the -profiler from Lua code. This module needs to be loaded explicitly: -

-local profile = require("jit.profile")
-
-

-This module can be used to implement your own higher-level profiler. -A typical profiling run starts the profiler, captures stack dumps in -the profiler callback, adds them to a hash table to aggregate the number -of samples, stops the profiler and then analyzes all captured -stack dumps. Other parameters can be sampled in the profiler callback, -too. But it's important not to spend too much time in the callback, -since this may skew the statistics. -

- -

profile.start(mode, cb) -— Start profiler

-

-This function starts the profiler. The mode argument is a -string holding options: -

-
    -
  • f — Profile with precision down to the function level.
  • -
  • l — Profile with precision down to the line level.
  • -
  • i<number> — Sampling interval in milliseconds (default -10ms).
    -Note: The actual sampling precision is OS-dependent. -
  • -
-

-The cb argument is a callback function which is called with -three arguments: (thread, samples, vmstate). The callback is -called on a separate coroutine, the thread argument is the -state that holds the stack to sample for profiling. Note: do -not modify the stack of that state or call functions on it. -

-

-samples gives the number of accumulated samples since the last -callback (usually 1). -

-

-vmstate holds the VM state at the time the profiling timer -triggered. This may or may not correspond to the state of the VM when -the profiling callback is called. The state is either 'N' -native (compiled) code, 'I' interpreted code, 'C' -C code, 'G' the garbage collector, or 'J' the JIT -compiler. -

- -

profile.stop() -— Stop profiler

-

-This function stops the profiler. -

- -

dump = profile.dumpstack([thread,] fmt, depth) -— Dump stack

-

-This function allows taking stack dumps in an efficient manner. It -returns a string with a stack dump for the thread (coroutine), -formatted according to the fmt argument: -

-
    -
  • p — Preserve the full path for module names. Otherwise, -only the file name is used.
  • -
  • f — Dump the function name if it can be derived. Otherwise, -use module:line.
  • -
  • F — Ditto, but dump module:name.
  • -
  • l — Dump module:line.
  • -
  • Z — Zap the following characters for the last dumped -frame.
  • -
  • All other characters are added verbatim to the output string.
  • -
-

-The depth argument gives the number of frames to dump, starting -at the topmost frame of the thread. A negative number dumps the frames in -inverse order. -

-

-The first example prints a list of the current module names and line -numbers of up to 10 frames in separate lines. The second example prints -semicolon-separated function names for all frames (up to 100) in inverse -order: -

-
-print(profile.dumpstack(thread, "l\n", 10))
-print(profile.dumpstack(thread, "lZ;", -100))
-
- -

Low-level C API

-

-The profiler can be controlled directly from C code, e.g. for -use by IDEs. The declarations are in "luajit.h" (see -Lua/C API extensions). -

- -

luaJIT_profile_start(L, mode, cb, data) -— Start profiler

-

-This function starts the profiler. See -above for a description of the mode argument. -

-

-The cb argument is a callback function with the following -declaration: -

-
-typedef void (*luaJIT_profile_callback)(void *data, lua_State *L,
-                                        int samples, int vmstate);
-
-

-data is available for use by the callback. L is the -state that holds the stack to sample for profiling. Note: do -not modify this stack or call functions on this stack — -use a separate coroutine for this purpose. See -above for a description of samples and vmstate. -

- -

luaJIT_profile_stop(L) -— Stop profiler

-

-This function stops the profiler. -

- -

p = luaJIT_profile_dumpstack(L, fmt, depth, len) -— Dump stack

-

-This function allows taking stack dumps in an efficient manner. -See above for a description of fmt -and depth. -

-

-This function returns a const char * pointing to a -private string buffer of the profiler. The int *len -argument returns the length of the output string. The buffer is -overwritten on the next call and deallocated when the profiler stops. -You either need to consume the content immediately or copy it for later -use. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/extensions.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/extensions.html deleted file mode 100644 index f006a6d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/extensions.html +++ /dev/null @@ -1,472 +0,0 @@ - - - -Extensions - - - - - - - - -
-Lua -
- - -
-

-LuaJIT is fully upwards-compatible with Lua 5.1. It supports all -» standard Lua -library functions and the full set of -» Lua/C API -functions. -

-

-LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic -loader level. This means you can compile a C module against the -standard Lua headers and load the same shared library from either Lua -or LuaJIT. -

-

-LuaJIT extends the standard Lua VM with new functionality and adds -several extension modules. Please note, this page is only about -functional enhancements and not about performance enhancements, -such as the optimized VM, the faster interpreter or the JIT compiler. -

- -

Extensions Modules

-

-LuaJIT comes with several built-in extension modules: -

- -

bit.* — Bitwise operations

-

-LuaJIT supports all bitwise operations as defined by -» Lua BitOp: -

-
-bit.tobit  bit.tohex  bit.bnot    bit.band bit.bor  bit.bxor
-bit.lshift bit.rshift bit.arshift bit.rol  bit.ror  bit.bswap
-
-

-This module is a LuaJIT built-in — you don't need to download or -install Lua BitOp. The Lua BitOp site has full documentation for all -» Lua BitOp API functions. -The FFI adds support for -64 bit bitwise operations, -using the same API functions. -

-

-Please make sure to require the module before using any of -its functions: -

-
-local bit = require("bit")
-
-

-An already installed Lua BitOp module is ignored by LuaJIT. -This way you can use bit operations from both Lua and LuaJIT on a -shared installation. -

- -

ffi.* — FFI library

-

-The FFI library allows calling external -C functions and the use of C data structures from pure Lua -code. -

- -

jit.* — JIT compiler control

-

-The functions in this module -control the behavior of the JIT compiler engine. -

- -

C API extensions

-

-LuaJIT adds some -extra functions to the Lua/C API. -

- -

Profiler

-

-LuaJIT has an integrated profiler. -

- -

Enhanced Standard Library Functions

- -

xpcall(f, err [,args...]) passes arguments

-

-Unlike the standard implementation in Lua 5.1, xpcall() -passes any arguments after the error function to the function -which is called in a protected context. -

- -

loadfile() etc. handle UTF-8 source code

-

-Non-ASCII characters are handled transparently by the Lua source code parser. -This allows the use of UTF-8 characters in identifiers and strings. -A UTF-8 BOM is skipped at the start of the source code. -

- -

tostring() etc. canonicalize NaN and ±Inf

-

-All number-to-string conversions consistently convert non-finite numbers -to the same strings on all platforms. NaN results in "nan", -positive infinity results in "inf" and negative infinity results -in "-inf". -

- -

tonumber() etc. use builtin string to number conversion

-

-All string-to-number conversions consistently convert integer and -floating-point inputs in decimal, hexadecimal and binary on all platforms. -strtod() is not used anymore, which avoids numerous -problems with poor C library implementations. The builtin conversion -function provides full precision according to the IEEE-754 standard, it -works independently of the current locale and it supports hex floating-point -numbers (e.g. 0x1.5p-3). -

- -

string.dump(f [,strip]) generates portable bytecode

-

-An extra argument has been added to string.dump(). If set to -true, 'stripped' bytecode without debug information is -generated. This speeds up later bytecode loading and reduces memory -usage. See also the --b command line option. -

-

-The generated bytecode is portable and can be loaded on any architecture -that LuaJIT supports, independent of word size or endianess. However, the -bytecode compatibility versions must match. Bytecode stays compatible -for dot releases (x.y.0 → x.y.1), but may change with major or -minor releases (2.0 → 2.1) or between any beta release. Foreign -bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded. -

-

-Note: LJ_GC64 mode requires a different frame layout, which implies -a different, incompatible bytecode format for all 64 bit ports. This may be -rectified in the future. -

- -

table.new(narray, nhash) allocates a pre-sized table

-

-An extra library function table.new() can be made available via -require("table.new"). This creates a pre-sized table, just like -the C API equivalent lua_createtable(). This is useful for big -tables if the final table size is known and automatic table resizing is -too expensive. -

- -

table.clear(tab) clears a table

-

-An extra library function table.clear() can be made available -via require("table.clear"). This clears all keys and values -from a table, but preserves the allocated array/hash sizes. This is -useful when a table, which is linked from multiple places, needs to be -cleared and/or when recycling a table for use by the same context. This -avoids managing backlinks, saves an allocation and the overhead of -incremental array/hash part growth. -

-

-Please note, this function is meant for very specific situations. In most -cases it's better to replace the (usually single) link with a new table -and let the GC do its work. -

- -

Enhanced PRNG for math.random()

-

-LuaJIT uses a Tausworthe PRNG with period 2^223 to implement -math.random() and math.randomseed(). The quality of -the PRNG results is much superior compared to the standard Lua -implementation, which uses the platform-specific ANSI rand(). -

-

-The PRNG generates the same sequences from the same seeds on all -platforms and makes use of all bits in the seed argument. -math.random() without arguments generates 52 pseudo-random bits -for every call. The result is uniformly distributed between 0.0 and 1.0. -It's correctly scaled up and rounded for math.random(n [,m]) to -preserve uniformity. -

-

-Important: Neither this nor any other PRNG based on the simplistic -math.random() API is suitable for cryptographic use. -

- -

io.* functions handle 64 bit file offsets

-

-The file I/O functions in the standard io.* library handle -64 bit file offsets. In particular, this means it's possible -to open files larger than 2 Gigabytes and to reposition or obtain -the current file position for offsets beyond 2 GB -(fp:seek() method). -

- -

debug.* functions identify metamethods

-

-debug.getinfo() and lua_getinfo() also return information -about invoked metamethods. The namewhat field is set to -"metamethod" and the name field has the name of -the corresponding metamethod (e.g. "__index"). -

- -

Fully Resumable VM

-

-The LuaJIT VM is fully resumable. This means you can yield from a -coroutine even across contexts, where this would not possible with -the standard Lua 5.1 VM: e.g. you can yield across pcall() -and xpcall(), across iterators and across metamethods. -

- -

Extensions from Lua 5.2

-

-LuaJIT supports some language and library extensions from Lua 5.2. -Features that are unlikely to break existing code are unconditionally -enabled: -

-
    -
  • goto and ::labels::.
  • -
  • Hex escapes '\x3F' and '\*' escape in strings.
  • -
  • load(string|reader [, chunkname [,mode [,env]]]).
  • -
  • loadstring() is an alias for load().
  • -
  • loadfile(filename [,mode [,env]]).
  • -
  • math.log(x [,base]).
  • -
  • string.rep(s, n [,sep]).
  • -
  • string.format(): %q reversible. -%s checks __tostring. -%a and "%A added.
  • -
  • String matching pattern %g added.
  • -
  • io.read("*L").
  • -
  • io.lines() and file:lines() process -io.read() options.
  • -
  • os.exit(status|true|false [,close]).
  • -
  • package.searchpath(name, path [, sep [, rep]]).
  • -
  • package.loadlib(name, "*").
  • -
  • debug.getinfo() returns nparams and isvararg -for option "u".
  • -
  • debug.getlocal() accepts function instead of level.
  • -
  • debug.getlocal() and debug.setlocal() accept negative -indexes for varargs.
  • -
  • debug.getupvalue() and debug.setupvalue() handle -C functions.
  • -
  • debug.upvalueid() and debug.upvaluejoin().
  • -
  • Lua/C API extensions: -lua_version() -lua_upvalueid() -lua_upvaluejoin() -lua_loadx() -lua_copy() -lua_tonumberx() -lua_tointegerx() -luaL_fileresult() -luaL_execresult() -luaL_loadfilex() -luaL_loadbufferx() -luaL_traceback() -luaL_setfuncs() -luaL_pushmodule() -luaL_newlibtable() -luaL_newlib() -luaL_testudata() -luaL_setmetatable() -
  • -
  • Command line option -E.
  • -
  • Command line checks __tostring for errors.
  • -
-

-Other features are only enabled, if LuaJIT is built with --DLUAJIT_ENABLE_LUA52COMPAT: -

-
    -
  • goto is a keyword and not a valid variable name anymore.
  • -
  • break can be placed anywhere. Empty statements (;;) -are allowed.
  • -
  • __lt, __le are invoked for mixed types.
  • -
  • __len for tables. rawlen() library function.
  • -
  • pairs() and ipairs() check for __pairs and -__ipairs.
  • -
  • coroutine.running() returns two results.
  • -
  • table.pack() and table.unpack() -(same as unpack()).
  • -
  • io.write() and file:write() return file handle -instead of true.
  • -
  • os.execute() and pipe:close() return detailed -exit status.
  • -
  • debug.setmetatable() returns object.
  • -
  • debug.getuservalue() and debug.setuservalue().
  • -
  • Remove math.mod(), string.gfind().
  • -
  • package.searchers.
  • -
  • module() returns the module table.
  • -
-

-Note: this provides only partial compatibility with Lua 5.2 at the -language and Lua library level. LuaJIT is API+ABI-compatible with -Lua 5.1, which prevents implementing features that would otherwise -break the Lua/C API and ABI (e.g. _ENV). -

- -

Extensions from Lua 5.3

-

-LuaJIT supports some extensions from Lua 5.3: -

    -
  • Unicode escape '\u{XX...}' embeds the UTF-8 encoding in string literals.
  • -
  • The argument table arg can be read (and modified) by LUA_INIT and -e chunks.
  • -
  • io.read() and file:read() accept formats with or without a leading *.
  • -
  • assert() accepts any type of error object.
  • -
  • table.move(a1, f, e, t [,a2]).
  • -
  • coroutine.isyieldable().
  • -
  • Lua/C API extensions: -lua_isyieldable() -
  • -
- -

C++ Exception Interoperability

-

-LuaJIT has built-in support for interoperating with C++ exceptions. -The available range of features depends on the target platform and -the toolchain used to compile LuaJIT: -

- - - - - - - - - - - - - - - - - - - - - - - - - - -
PlatformCompilerInteroperability
External frame unwindingGCC, Clang, MSVCFull
Internal frame unwinding + DWARF2GCC, ClangLimited
Windows 64 bitnon-MSVCLimited
Other platformsOther compilersNo
-

-Full interoperability means: -

-
    -
  • C++ exceptions can be caught on the Lua side with pcall(), -lua_pcall() etc.
  • -
  • C++ exceptions will be converted to the generic Lua error -"C++ exception", unless you use the -C call wrapper feature.
  • -
  • It's safe to throw C++ exceptions across non-protected Lua frames -on the C stack. The contents of the C++ exception object -pass through unmodified.
  • -
  • Lua errors can be caught on the C++ side with catch(...). -The corresponding Lua error message can be retrieved from the Lua stack.
    -For MSVC for Windows 64 bit this requires compilation of your C++ code -with /EHa.
  • -
  • Throwing Lua errors across C++ frames is safe. C++ destructors -will be called.
  • -
-

-Limited interoperability means: -

-
    -
  • C++ exceptions can be caught on the Lua side with pcall(), -lua_pcall() etc.
  • -
  • C++ exceptions will be converted to the generic Lua error -"C++ exception", unless you use the -C call wrapper feature.
  • -
  • C++ exceptions will be caught by non-protected Lua frames and -are rethrown as a generic Lua error. The C++ exception object will -be destroyed.
  • -
  • Lua errors cannot be caught on the C++ side.
  • -
  • Throwing Lua errors across C++ frames will not call -C++ destructors.
  • -
- -

-No interoperability means: -

-
    -
  • It's not safe to throw C++ exceptions across Lua frames.
  • -
  • C++ exceptions cannot be caught on the Lua side.
  • -
  • Lua errors cannot be caught on the C++ side.
  • -
  • Throwing Lua errors across C++ frames will not call -C++ destructors.
  • -
-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/faq.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/faq.html deleted file mode 100644 index c07fd24..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/faq.html +++ /dev/null @@ -1,195 +0,0 @@ - - - -Frequently Asked Questions (FAQ) - - - - - - - - -
-Lua -
- - -
-
-
Q: Where can I learn more about LuaJIT and Lua?
-
-
-
- -
-
Q: Where can I learn more about the compiler technology used by LuaJIT?
-
-Please use the following Google Scholar searches to find relevant papers:
-Search for: » Trace Compiler
-Search for: » JIT Compiler
-Search for: » Dynamic Language Optimizations
-Search for: » SSA Form
-Search for: » Linear Scan Register Allocation
-Here is a list of the » innovative features in LuaJIT.
-And, you know, reading the source is of course the only way to enlightenment. -
-
- -
-
Q: Why do I get this error: "attempt to index global 'arg' (a nil value)"?
-Q: My vararg functions fail after switching to LuaJIT!
-
LuaJIT is compatible to the Lua 5.1 language standard. It doesn't -support the implicit arg parameter for old-style vararg -functions from Lua 5.0.
Please convert your code to the -» Lua 5.1 -vararg syntax.
-
- -
-
Q: Why do I get this error: "bad FPU precision"?
-
Q: I get weird behavior after initializing Direct3D.
-
Q: Some FPU operations crash after I load a Delphi DLL.
-
-
- -DirectX/Direct3D (up to version 9) sets the x87 FPU to single-precision -mode by default. This violates the Windows ABI and interferes with the -operation of many programs — LuaJIT is affected, too. Please make -sure you always use the D3DCREATE_FPU_PRESERVE flag when -initializing Direct3D.
- -Direct3D version 10 or higher do not show this behavior anymore. -Consider testing your application with older versions, too.
- -Similarly, the Borland/Delphi runtime modifies the FPU control word and -enables FP exceptions. Of course, this violates the Windows ABI, too. -Please check the Delphi docs for the Set8087CW method.
-
- -
-
Q: Sometimes Ctrl-C fails to stop my Lua program. Why?
-
The interrupt signal handler sets a Lua debug hook. But this is -ignored by compiled code. If your program is running in a tight loop -and never falls back to the interpreter, the debug hook never runs and -can't throw the "interrupted!" error.
-You have to press Ctrl-C twice to stop your program. That's similar -to when it's stuck running inside a C function under the Lua interpreter.
-
- -
-
Q: Table iteration with pairs() does not result in the same order?
-
The order of table iteration is explicitly undefined by -the Lua language standard.
-Different Lua implementations or versions may use different orders for -otherwise identical tables. Different ways of constructing a table may -result in different orders, too.
-Due to improved VM security, LuaJIT 2.1 may even use a different order -on separate VM invocations or when string keys are newly interned.

-If your program relies on a deterministic order, it has a bug. Rewrite it, -so it doesn't rely on the key order. Or sort the table keys, if you must.
-
- -
-
Q: Can Lua code be safely sandboxed?
-
-Maybe for an extremely restricted subset of Lua and if you relentlessly -scrutinize every single interface function you offer to the untrusted code.
- -Although Lua provides some sandboxing functionality (setfenv(), hooks), -it's very hard to get this right even for the Lua core libraries. Of course, -you'll need to inspect any extension library, too. And there are libraries -that are inherently unsafe, e.g. the FFI library.
- -More reading material at the » Lua Wiki and » Wikipedia.

- -Relatedly, loading untrusted bytecode is not safe!
- -It's trivial to crash the Lua or LuaJIT VM with maliciously crafted bytecode. -This is well known and there's no bytecode verification on purpose, so please -don't report a bug about it. Check the mode parameter for the -load*() functions to disable loading of bytecode.

- -In general, the only promising approach is to sandbox Lua code at the -process level and not the VM level. -
-
- -
-
Q: Lua runs everywhere. Why doesn't LuaJIT support my CPU?
-
Because it's a compiler — it needs to generate native -machine code. This means the code generator must be ported to each -architecture. And the fast interpreter is written in assembler and -must be ported, too. This is quite an undertaking.
-The install documentation shows the supported -architectures.
-Other architectures may follow based on sufficient user demand and -market-relevance of the architecture. Sponsoring is required to develop -the port itself, to integrate it and to continuously maintain it in the -actively developed branches.
-
-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/img/contact.png b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/img/contact.png deleted file mode 100644 index 9c73dc594efc1f47309d6c9b73d7719c3a9e04df..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1340 zcmV-C1;hG@P)sQ4|K#fZOHEVa==(`aQP0%nM@di3(&I%(O)@h#$&+f^&9&U}9=sU}#xfW8dZX zS6X4+hl`eQb9`=bdTMQUS6N_FRb1EH?oLrx)!OXR*y%bu zKs7fzyTH%6zRkD1%O4*iwYte19Urr}$DpLK7#SM{1qGa-tpfuC0RaJph?9SUjedfR ze1M8)YI0?0aARg}VPtGhP*&yZ{!C6(sq^PzF3k;#Avk3_bkCL7T2MCRjoQjT_h>Vwp zij{MAe{pnuZ*qHWaCvKPcUD+m*xm0?Qd!p9?bO-p)7k3L*XYmH<~BGyy}{8mHafb$ z&bYnIARr?h9v~YW9vK=O7Z@4@1O%I(tO5c8@Y5f50008dNklZz)3AlitwxnPH%H`Uot)Wm#A~UJD{dq04A@c=B#o!5#fVO zK9y#&^Z}?|*LsioK&(FDooZAR)tQj#4t{T51EP;48U9I=7br`Bk^O8MkeX!ZNe^m_BH=Vrj*jZZuSKj$t2j7FZ^2u4YKv8(~ zaMJ@oQZ>+`rSiG$iy;5QP}GpcaF4=ry2srsiZ0RqYjK@LQA)d@GB0_aay0Tvrp83dAL2x{13 zII2|!RUp(7P{l^2T*QHZ71!E*QcTqNtMh0|uLS!2k^8W?Ka#6$aBn3*=LUe(BpUo6 z=nkI1fP*0x3aYH^su6tQT}{)EfU4IyLBQ-;(w1i;#)>KNGk41l6I5_B2)f}c}CK-PL&a{M5Rut(jQ@VX1_p&z6vN)t zNcp7CYSP6-mE96jwhE}y?lvr|2kRa0g^iLTaJ1y+9qs~F+W}}-FHd&oy;TeG2n6hM z2eK~@vIu+Z%WDWZDK7wkG0}TEXW(~!Gk+0t(i)Sck-Ht%F=yzi{ZFXe-}F6T**!vM y)$^3mN5{Eb6*vAD>S)nmP3aw7v29)6WaBSReaJM4z7xj)0000 - - -Installation - - - - - - - - -
-Lua -
- - -
-

-LuaJIT is only distributed as a source package. This page explains -how to build and install LuaJIT with different operating systems -and C compilers. -

-

-For the impatient (on POSIX systems): -

-
-make && sudo make install
-
- -

Requirements

-

Systems

-

-LuaJIT currently builds out-of-the box on most systems: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OSMin. VersionRequirementsLuaJIT Versions
Windows7x86 or x64, ARM64: TBAv2.0 –
Linux  v2.0 –
*BSD  v2.0 –
macOS (OSX)10.4 v2.1 –
POSIX mmap, dlopenv2.0 –
Android4.0Recent Android NDKv2.0 –
iOS3.0Xcode iOS SDKv2.1 –
PS3 PS3 SDKv2.0 – v2.1 EOL
PS4 PS4 SDK (ORBIS)v2.0 –
PS5 PS5 SDK (PROSPERO)v2.1 –
PS Vita PS Vita SDK (PSP2)v2.0 – v2.1 EOL
Xbox 360 Xbox 360 SDK (XEDK)v2.0 – v2.1 EOL
Xbox One Xbox One SDK (DURANGO)v2.1 –
Nintendo Switch NintendoSDK + NX Addonv2.1 –
-

-The codebase has compatibility defines for some more systems, but -without official support. -

-

Toolchains

-

-Building LuaJIT requires a recent toolchain based on GCC, Clang/LLVM or -MSVC++. -

-

-The Makefile-based build system requires GNU Make and supports -cross-builds. Batch files are provided for MSVC++ builds and console -cross-builds. -

-

CPU Architectures

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CPUBitsRequirementsVariantsLuaJIT Versions
x8632v2.1+: SSE2 v2.0 –
x6464  v2.0 –
ARM32ARMv5+, ARM9E+hard-fp + soft-fpv2.0 –
ARM6464 ARM64le + ARM64bev2.1 –
PPC3232 hard-fp + soft-fpv2.0 – v2.1 EOL
PPC/e50032e500v2 v2.0 EOL
MIPS3232MIPS32r1 – r5hard-fp + soft-fpv2.0 –
MIPS6464MIPS64r1 – r5hard-fp + soft-fpv2.1 –
MIPS6464MIPS64r6hard-fp + soft-fpv2.1 EOL
RISC-V64RVA22+ TBA
-

-There are no plans to add historic architectures or to continue support -for end-of-life (EOL) architectures, for which no new CPUs are commonly -available anymore. Likewise, there are no plans to support marginal -and/or de-facto-dead architectures. -

- -

Configuring LuaJIT

-

-The standard configuration should work fine for most installations. -Usually there is no need to tweak the settings. The following files -hold all user-configurable settings: -

-
    -
  • src/luaconf.h sets some configuration variables.
  • -
  • Makefile has settings for installing LuaJIT (POSIX -only).
  • -
  • src/Makefile has settings for compiling LuaJIT -under POSIX, MinGW or Cygwin.
  • -
  • src/msvcbuild.bat has settings for compiling LuaJIT with -MSVC (Visual Studio).
  • -
-

-Please read the instructions given in these files, before changing -any settings. -

-

-All LuaJIT 64 bit ports use 64 bit GC objects by default (LJ_GC64). -For x64, you can select the old 32-on-64 bit mode by adding -XCFLAGS=-DLUAJIT_DISABLE_GC64 to the make command. -Please check the note about the -bytecode format differences, too. -

- -

POSIX Systems (Linux, macOS, *BSD etc.)

-

Prerequisites

-

-Depending on your distribution, you may need to install a package for -GCC, the development headers and/or a complete SDK. E.g. on a current -Debian/Ubuntu, install libc6-dev with the package manager. -

-

-The recommended way to fetch the latest version is to do a pull from -the git repository. -

-

-Alternatively, download the latest source package of LuaJIT (pick the .tar.gz). -Move it to a directory of your choice, open a terminal window and change -to this directory. Now unpack the archive and change to the newly created -directory (replace XX.YY.ZZ with the version you downloaded): -

-
-tar zxf LuaJIT-XX.YY.ZZ.tar.gz
-cd LuaJIT-XX.YY.ZZ
-
-

Building LuaJIT

-

-The supplied Makefiles try to auto-detect the settings needed for your -operating system and your compiler. They need to be run with GNU Make, -which is probably the default on your system, anyway. Simply run: -

-
-make
-
-

-This always builds a native binary, depending on the host OS -you're running this command on. Check the section on -cross-compilation for more options. -

-

-By default, modules are only searched under the prefix /usr/local. -You can add an extra prefix to the search paths by appending the -PREFIX option, e.g.: -

-
-make PREFIX=/home/myself/lj2
-
-

-Note for macOS: you must set the MACOSX_DEPLOYMENT_TARGET -environment variable to a value supported by your toolchain: -

-
-MACOSX_DEPLOYMENT_TARGET=XX.YY make
-
-

Installing LuaJIT

-

-The top-level Makefile installs LuaJIT by default under -/usr/local, i.e. the executable ends up in -/usr/local/bin and so on. You need root privileges -to write to this path. So, assuming sudo is installed on your system, -run the following command and enter your sudo password: -

-
-sudo make install
-
-

-Otherwise specify the directory prefix as an absolute path, e.g.: -

-
-make install PREFIX=/home/myself/lj2
-
-

-Obviously the prefixes given during build and installation need to be the same. -

- -

Windows Systems

-

Prerequisites

-

-Either install one of the open source SDKs -(» MinGW or -» Cygwin), which come with a modified -GCC plus the required development headers. -Or install Microsoft's Visual Studio (MSVC). -

-

-Next, pull from the git repository or download the source package and -unpack it using an archive manager (e.g. the Windows Explorer) to -a directory of your choice. -

-

Building with MSVC

-

-Open a "Visual Studio Command Prompt" (either x86 or x64), cd to the -directory where you've unpacked the sources and run these commands: -

-
-cd src
-msvcbuild
-
-

-Check the msvcbuild.bat file for more options. -Then follow the installation instructions below. -

-

Building with MinGW or Cygwin

-

-Open a command prompt window and make sure the MinGW or Cygwin programs -are in your path. Then cd to the directory of the git repository -or where you've unpacked the sources. Then run this command for MinGW: -

-
-mingw32-make
-
-

-Or this command for Cygwin: -

-
-make
-
-

-Then follow the installation instructions below. -

-

Installing LuaJIT

-

-Copy luajit.exe and lua51.dll (built in the src -directory) to a newly created directory (any location is ok). -Add lua and lua\jit directories below it and copy -all Lua files from the src\jit directory of the distribution -to the latter directory. -

-

-There are no hardcoded -absolute path names — all modules are loaded relative to the -directory where luajit.exe is installed -(see src/luaconf.h). -

- -

Cross-compiling LuaJIT

-

-First, let's clear up some terminology: -

-
    -
  • Host: This is your development system, usually based on a x64 or x86 CPU.
  • -
  • Target: This is the target system you want LuaJIT to run on, e.g. Android/ARM.
  • -
  • Toolchain: This comprises a C compiler, linker, assembler and a matching C library.
  • -
  • Host (or system) toolchain: This is the toolchain used to build native binaries for your host system.
  • -
  • Cross-compile toolchain: This is the toolchain used to build binaries for the target system. They can only be run on the target system.
  • -
-

-The GNU Makefile-based build system allows cross-compiling on any host -for any supported target: -

-
    -
  • Yes, you need a toolchain for both your host and your target!
  • -
  • Both host and target architectures must have the same pointer size.
  • -
  • E.g. if you want to cross-compile to a 32 bit target on a 64 bit host, you need to install the multilib development package (e.g. libc6-dev-i386 on Debian/Ubuntu) and build a 32 bit host part (HOST_CC="gcc -m32").
  • -
  • 64 bit targets always require compilation on a 64 bit host.
  • -
-

-You need to specify TARGET_SYS whenever the host OS and the -target OS differ, or you'll get assembler or linker errors: -

-
    -
  • E.g. if you're compiling on a Windows or macOS host for embedded Linux or Android, you need to add TARGET_SYS=Linux to the examples below.
  • -
  • For a minimal target OS, you may need to disable the built-in allocator in src/Makefile and use TARGET_SYS=Other.
  • -
  • Don't forget to specify the same TARGET_SYS for the install step, too.
  • -
-

-Here are some examples where host and target have the same CPU: -

-
-# Cross-compile to a 32 bit binary on a multilib x64 OS
-make CC="gcc -m32"
-
-# Cross-compile on Debian/Ubuntu for Windows (mingw32 package)
-make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows
-
-

-The CROSS prefix allows specifying a standard GNU cross-compile -toolchain (Binutils, GCC and a matching libc). The prefix may vary -depending on the --target the toolchain was built for (note the -CROSS prefix has a trailing "-"). The examples below -use the canonical toolchain triplets for Linux. -

-

-Since there's often no easy way to detect CPU features at runtime, it's -important to compile with the proper CPU or architecture settings: - -

    -
  • The best way to get consistent results is to specify the correct settings when building the toolchain yourself.
  • -
  • For a pre-built, generic toolchain add -mcpu=... or -march=... and other necessary flags to TARGET_CFLAGS.
  • -
  • For ARM it's important to have the correct -mfloat-abi=... setting, too. Otherwise LuaJIT may not run at the full performance of your target CPU.
  • -
  • For MIPS it's important to select a supported ABI (o32 on MIPS32, n64 on MIPS64) and consistently compile your project either with hard-float or soft-float compiler settings.
  • -
-

-Here are some examples for targets with a different CPU than the host: -

-
-# ARM soft-float
-make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
-     TARGET_CFLAGS="-mfloat-abi=soft"
-
-# ARM soft-float ABI with VFP (example for Cortex-A9)
-make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
-     TARGET_CFLAGS="-mcpu=cortex-a9 -mfloat-abi=softfp"
-
-# ARM hard-float ABI with VFP (armhf, most modern toolchains)
-make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabihf-
-
-# ARM64
-make CROSS=aarch64-linux-
-
-# PPC
-make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu-
-
-# MIPS32 big-endian
-make HOST_CC="gcc -m32" CROSS=mips-linux-
-# MIPS32 little-endian
-make HOST_CC="gcc -m32" CROSS=mipsel-linux-
-
-# MIPS64 big-endian
-make CROSS=mips-linux- TARGET_CFLAGS="-mips64r2 -mabi=64"
-# MIPS64 little-endian
-make CROSS=mipsel-linux- TARGET_CFLAGS="-mips64r2 -mabi=64"
-
-

-You can cross-compile for Android using the » Android NDK. -Please adapt the environment variables to match the install locations and the -desired target platform. E.g. Android 4.1 corresponds to ABI level 16. -

-
-# Android/ARM64, aarch64, Android 5.0+ (L)
-NDKDIR=/opt/android/ndk
-NDKBIN=$NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin
-NDKCROSS=$NDKBIN/aarch64-linux-android-
-NDKCC=$NDKBIN/aarch64-linux-android21-clang
-make CROSS=$NDKCROSS \
-     STATIC_CC=$NDKCC DYNAMIC_CC="$NDKCC -fPIC" \
-     TARGET_LD=$NDKCC TARGET_AR="$NDKBIN/llvm-ar rcus" \
-     TARGET_STRIP=$NDKBIN/llvm-strip
-
-# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.1+ (JB)
-NDKDIR=/opt/android/ndk
-NDKBIN=$NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin
-NDKCROSS=$NDKBIN/arm-linux-androideabi-
-NDKCC=$NDKBIN/armv7a-linux-androideabi16-clang
-make HOST_CC="gcc -m32" CROSS=$NDKCROSS \
-     STATIC_CC=$NDKCC DYNAMIC_CC="$NDKCC -fPIC" \
-     TARGET_LD=$NDKCC TARGET_AR="$NDKBIN/llvm-ar rcus" \
-     TARGET_STRIP=$NDKBIN/llvm-strip
-
-

-You can cross-compile for iOS 3.0+ (iPhone/iPad) using the » iOS SDK: -

-

-Note: the JIT compiler is disabled for iOS, because regular iOS Apps -are not allowed to generate code at runtime. You'll only get the performance -of the LuaJIT interpreter on iOS. This is still faster than plain Lua, but -much slower than the JIT compiler. Please complain to Apple, not me. -Or use Android. :-p -

-
-# iOS/ARM64
-ISDKP=$(xcrun --sdk iphoneos --show-sdk-path)
-ICC=$(xcrun --sdk iphoneos --find clang)
-ISDKF="-arch arm64 -isysroot $ISDKP"
-make DEFAULT_CC=clang CROSS="$(dirname $ICC)/" \
-     TARGET_FLAGS="$ISDKF" TARGET_SYS=iOS
-
- -

Cross-compiling for consoles

-

-Building LuaJIT for consoles requires both a supported host compiler -(x86 or x64) and a cross-compiler from the official console SDK. -

-

-Due to restrictions on consoles, the JIT compiler is disabled and only -the fast interpreter is built. This is still faster than plain Lua, -but much slower than the JIT compiler. The FFI is disabled, too, since -it's not very useful in such an environment. -

-

-The following commands build a static library libluajit.a, -which can be linked against your game, just like the Lua library. -

-

-To cross-compile for PS3 from a Linux host (requires -32 bit GCC, i.e. multilib Linux/x64) or a Windows host (requires -32 bit MinGW), run this command: -

-
-make HOST_CC="gcc -m32" CROSS=ppu-lv2-
-
-

-To cross-compile for the other consoles from a Windows host, open a -"Native Tools Command Prompt for VS". You need to choose either the 32 -or the 64 bit version of the host compiler to match the target. -Then cd to the src directory below where you've -unpacked the sources and run the build command given in the table: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ConsoleBitsBuild Command
PS464ps4build
PS564ps5build
PS Vita32psvitabuild
Xbox 36032xedkbuild
Xbox One64xb1build
Nintendo Switch NX3232nxbuild
Nintendo Switch NX6464nxbuild
-

-Please check out the comments in the corresponding *.bat -file for more options. -

- -

Embedding LuaJIT

-

-LuaJIT is API-compatible with Lua 5.1. If you've already embedded Lua -into your application, you probably don't need to do anything to switch -to LuaJIT, except link with a different library: -

-
    -
  • It's strongly suggested to build LuaJIT separately using the supplied -build system. Please do not attempt to integrate the individual -source files into your build tree. You'll most likely get the internal build -dependencies wrong or mess up the compiler flags. Treat LuaJIT like any -other external library and link your application with either the dynamic -or static library, depending on your needs.
  • -
  • If you want to load C modules compiled for plain Lua -with require(), you need to make sure the public symbols -(e.g. lua_pushnumber) are exported, too: -
    • On POSIX systems you can either link to the shared library -or link the static library into your application. In the latter case -you'll need to export all public symbols from your main executable -(e.g. -Wl,-E on Linux) and add the external dependencies -(e.g. -lm -ldl on Linux).
    • -
    • Since Windows symbols are bound to a specific DLL name, you need to -link to the lua51.dll created by the LuaJIT build (do not rename -the DLL). You may link LuaJIT statically on Windows only if you don't -intend to load Lua/C modules at runtime. -
    -
  • -
-

Additional hints for initializing LuaJIT using the C API functions:

-
    -
  • Here's a -» simple example -for embedding Lua or LuaJIT into your application.
  • -
  • Make sure you use luaL_newstate. Avoid using -lua_newstate, since this uses the (slower) default memory -allocator from your system (no support for this on 64 bit architectures).
  • -
  • Make sure you use luaL_openlibs and not the old Lua 5.0 style -of calling luaopen_base etc. directly.
  • -
  • To change or extend the list of standard libraries to load, copy -src/lib_init.c to your project and modify it accordingly. -Make sure the jit library is loaded, or the JIT compiler -will not be activated.
  • -
  • The bit.* module for bitwise operations -is already built-in. There's no need to statically link -» Lua BitOp to your application.
  • -
- -

Hints for Distribution Maintainers

-

-The LuaJIT build system has extra provisions for the needs of most -POSIX-based distributions. If you're a package maintainer for -a distribution, please make use of these features and -avoid patching, subverting, autotoolizing or messing up the build system -in unspeakable ways. -

-

-There should be absolutely no need to patch luaconf.h or any -of the Makefiles. And please do not hand-pick files for your packages — -simply use whatever make install creates. There's a reason -for all the files and directories it creates. -

-

-The build system uses GNU make and auto-detects most settings based on -the host you're building it on. This should work fine for native builds, -even when sandboxed. You may need to pass some of the following flags to -both the make and the make install command lines -for a regular distribution build: -

-
    -
  • PREFIX overrides the installation path and should usually -be set to /usr. Setting this also changes the module paths and -the paths needed to locate the shared library.
  • -
  • DESTDIR is an absolute path which allows you to install -to a shadow tree instead of the root tree of the build system.
  • -
  • MULTILIB sets the architecture-specific library path component -for multilib systems. The default is lib.
  • -
  • Have a look at the top-level Makefile and src/Makefile -for additional variables to tweak. The following variables may be -overridden, but it's not recommended, except for special needs -like cross-builds: -BUILDMODE, CC, HOST_CC, STATIC_CC, DYNAMIC_CC, CFLAGS, HOST_CFLAGS, -TARGET_CFLAGS, LDFLAGS, HOST_LDFLAGS, TARGET_LDFLAGS, TARGET_SHLDFLAGS, -TARGET_FLAGS, LIBS, HOST_LIBS, TARGET_LIBS, CROSS, HOST_SYS, TARGET_SYS -
  • -
-

-The build system has a special target for an amalgamated build, i.e. -make amalg. This compiles the LuaJIT core as one huge C file -and allows GCC to generate faster and shorter code. Alas, this requires -lots of memory during the build. This may be a problem for some users, -that's why it's not enabled by default. But it shouldn't be a problem for -most build farms. It's recommended that binary distributions use this -target for their LuaJIT builds. -

-

-The tl;dr version of the above: -

-
-make amalg PREFIX=/usr && \
-make install PREFIX=/usr DESTDIR=/tmp/buildroot
-
-

-Finally, if you encounter any difficulties, please -contact me first, instead of releasing a broken -package onto unsuspecting users. Because they'll usually gonna complain -to me (the upstream) and not you (the package maintainer), anyway. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/luajit.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/luajit.html deleted file mode 100644 index e3a5478..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/luajit.html +++ /dev/null @@ -1,230 +0,0 @@ - - - -LuaJIT - - - - - - - - - -
-Lua -
- - -
-

-LuaJIT is a Just-In-Time Compiler (JIT) for the -» Lua programming language. -Lua is a powerful, dynamic and light-weight programming language. -It may be embedded or used as a general-purpose, stand-alone language. -

-

-LuaJIT is Copyright © 2005-2022 Mike Pall, released under the -» MIT open source license. -

-

-

- -

Compatibility

- - -
WindowsLinuxBSDmacOSPOSIX
- - -
EmbeddedAndroidiOS
- - -
PS3PS4
PS5
PS VitaXbox 360Xbox OneNintendo
Switch
- - -
GCCClang
LLVM
MSVC
- - -
x86
x64
ARM
ARM64
PPCMIPS32
MIPS64
- - -
Lua 5.1
API+ABI
+ JIT+ BitOp+ FFIDrop-in
DLL/.so
- -

Overview

- - - - - - - - - -
3x
-  100x
115 KB
VM
90 KB
JIT
63 KLOC
C
24 KLOC
ASM
11 KLOC
Lua
-

-LuaJIT has been successfully used as a scripting middleware in -games, appliances, network and graphics apps, numerical simulations, -trading platforms and many other specialty applications. It scales from -embedded devices, smartphones, desktops up to server farms. It combines -high flexibility with high performance -and an unmatched low memory footprint. -

-

-LuaJIT has been in continuous development since 2005. It's widely -considered to be one of the fastest dynamic language -implementations. It has outperformed other dynamic languages on many -cross-language benchmarks since its first release — often by a -substantial margin. -

-

-For LuaJIT 2.0, the whole VM has been rewritten from the ground up -and relentlessly optimized for performance. It combines a high-speed -interpreter, written in assembler, with a state-of-the-art JIT -compiler. -

-

-An innovative trace compiler is integrated with advanced, -SSA-based optimizations and highly tuned code generation backends. -A substantial reduction of the overhead associated with dynamic languages -allows it to break into the performance range traditionally reserved for -offline, static language compilers. -

- -

More ...

-

-Please select a sub-topic in the navigation bar to learn more about LuaJIT. -

-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/running.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/running.html deleted file mode 100644 index edc049f..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/running.html +++ /dev/null @@ -1,312 +0,0 @@ - - - -Running LuaJIT - - - - - - - - -
-Lua -
- - -
-

-LuaJIT has only a single stand-alone executable, called luajit on -POSIX systems or luajit.exe on Windows. It can be used to run simple -Lua statements or whole Lua applications from the command line. It has an -interactive mode, too. -

- -

Command Line Options

-

-The luajit stand-alone executable is just a slightly modified -version of the regular lua stand-alone executable. -It supports the same basic options, too. luajit -h -prints a short list of the available options. Please have a look at the -» Lua manual -for details. -

-

-LuaJIT has some additional options: -

- -

-b[options] input output

-

-This option saves or lists bytecode. The following additional options -are accepted: -

-
    -
  • -l — Only list bytecode.
  • -
  • -s — Strip debug info (this is the default).
  • -
  • -g — Keep debug info.
  • -
  • -n name — Set module name (default: auto-detect from input name)
  • -
  • -t type — Set output file type (default: auto-detect from output name).
  • -
  • -a arch — Override architecture for object files (default: native).
  • -
  • -o os — Override OS for object files (default: native).
  • -
  • -F name — Override filename (default: input filename).
  • -
  • -e chunk — Use chunk string as input.
  • -
  • - (a single minus sign) — Use stdin as input and/or stdout as output.
  • -
-

-The output file type is auto-detected from the extension of the output -file name: -

-
    -
  • c — C source file, exported bytecode data.
  • -
  • h — C header file, static bytecode data.
  • -
  • obj or o — Object file, exported bytecode data -(OS- and architecture-specific).
  • -
  • raw or any other extension — Raw bytecode file (portable). -
-

-Notes: -

-
    -
  • See also string.dump() -for information on bytecode portability and compatibility.
  • -
  • A file in raw bytecode format is auto-detected and can be loaded like -any Lua source file. E.g. directly from the command line or with -loadfile(), dofile() etc.
  • -
  • To statically embed the bytecode of a module in your application, -generate an object file and just link it with your application.
  • -
  • On most ELF-based systems (e.g. Linux) you need to explicitly export the -global symbols when linking your application, e.g. with: -Wl,-E
  • -
  • require() tries to load embedded bytecode data from exported -symbols (in *.exe or lua51.dll on Windows) and from -shared libraries in package.cpath.
  • -
-

-Typical usage examples: -

-
-luajit -b test.lua test.out                 # Save bytecode to test.out
-luajit -bg test.lua test.out                # Keep debug info
-luajit -be "print('hello world')" test.out  # Save cmdline script
-
-luajit -bl test.lua                         # List to stdout
-luajit -bl test.lua test.txt                # List to test.txt
-luajit -ble "print('hello world')"          # List cmdline script
-
-luajit -b test.lua test.obj                 # Generate object file
-# Link test.obj with your application and load it with require("test")
-
- -

-j cmd[=arg[,arg...]]

-

-This option performs a LuaJIT control command or activates one of the -loadable extension modules. The command is first looked up in the -jit.* library. If no matching function is found, a module -named jit.<cmd> is loaded and the start() -function of the module is called with the specified arguments (if -any). The space between -j and cmd is optional. -

-

-Here are the available LuaJIT control commands: -

-
    -
  • -jon — Turns the JIT compiler on (default).
  • -
  • -joff — Turns the JIT compiler off (only use the interpreter).
  • -
  • -jflush — Flushes the whole cache of compiled code.
  • -
  • -jv — Shows verbose information about the progress of the JIT compiler.
  • -
  • -jdump — Dumps the code and structures used in various compiler stages.
  • -
  • -jp — Start the integrated profiler.
  • -
-

-The -jv and -jdump commands are extension modules -written in Lua. They are mainly used for debugging the JIT compiler -itself. For a description of their options and output format, please -read the comment block at the start of their source. -They can be found in the lib directory of the source -distribution or installed under the jit directory. By default, -this is /usr/local/share/luajit-XX.YY.ZZ>/jit on POSIX -systems (replace XX.YY.ZZ by the installed version). -

- -

-O[level]
--O[+]flag   -O-flag
--Oparam=value

-

-This options allows fine-tuned control of the optimizations used by -the JIT compiler. This is mainly intended for debugging LuaJIT itself. -Please note that the JIT compiler is extremely fast (we are talking -about the microsecond to millisecond range). Disabling optimizations -doesn't have any visible impact on its overhead, but usually generates -code that runs slower. -

-

-The first form sets an optimization level — this enables a -specific mix of optimization flags. -O0 turns off all -optimizations and higher numbers enable more optimizations. Omitting -the level (i.e. just -O) sets the default optimization level, -which is -O3 in the current version. -

-

-The second form adds or removes individual optimization flags. -The third form sets a parameter for the VM or the JIT compiler -to a specific value. -

-

-You can either use this option multiple times (like -Ocse --O-dce -Ohotloop=10) or separate several settings with a comma -(like -O+cse,-dce,hotloop=10). The settings are applied from -left to right, and later settings override earlier ones. You can freely -mix the three forms, but note that setting an optimization level -overrides all earlier flags. -

-

-Note that -Ofma is not enabled by default at any level, -because it affects floating-point result accuracy. Only enable this, -if you fully understand the trade-offs of FMA for performance (higher), -determinism (lower) and numerical accuracy (higher). -

-

-Here are the available flags and at what optimization levels they -are enabled: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Flag-O1-O2-O3 
foldConstant Folding, Simplifications and Reassociation
cseCommon-Subexpression Elimination
dceDead-Code Elimination
narrow Narrowing of numbers to integers
loop Loop Optimizations (code hoisting)
fwd  Load Forwarding (L2L) and Store Forwarding (S2L)
dse  Dead-Store Elimination
abc  Array Bounds Check Elimination
sink  Allocation/Store Sinking
fuse  Fusion of operands into instructions
fma    Fused multiply-add
-

-Here are the parameters and their default settings: -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ParameterDefault 
maxtrace1000Max. number of traces in the cache
maxrecord4000Max. number of recorded IR instructions
maxirconst500Max. number of IR constants of a trace
maxside100Max. number of side traces of a root trace
maxsnap500Max. number of snapshots for a trace
hotloop56Number of iterations to detect a hot loop or hot call
hotexit10Number of taken exits to start a side trace
tryside4Number of attempts to compile a side trace
instunroll4Max. unroll factor for instable loops
loopunroll15Max. unroll factor for loop ops in side traces
callunroll3Max. unroll factor for pseudo-recursive calls
recunroll2Min. unroll factor for true recursion
sizemcode32Size of each machine code area in KBytes (Windows: 64K)
maxmcode512Max. total size of all machine code areas in KBytes
-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/status.html b/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/status.html deleted file mode 100644 index efb1e06..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/doc/status.html +++ /dev/null @@ -1,105 +0,0 @@ - - - -Status - - - - - - - - -
-Lua -
- - -
-

-This documentation is for LuaJIT 2.1.0-beta3. Please check the doc -directory in each git branch for the version-specific documentation. -

-

-The currently developed branches are LuaJIT 2.1 and LuaJIT 2.0. -

-

-LuaJIT 2.0 is in feature-freeze — new features will only -be added to LuaJIT 2.1. -

- -

Current Status

-

-LuaJIT ought to run all Lua 5.1-compatible source code just fine. -It's considered a serious bug if the VM crashes or produces unexpected -results — please report this. -

-

-Known incompatibilities and issues in LuaJIT 2.0: -

-
    -
  • -There are some differences in implementation-defined behavior. -These either have a good reason, are arbitrary design choices, -or are due to quirks in the VM. The latter cases may get fixed if a -demonstrable need is shown. -
  • -
  • -The Lua debug API is missing a couple of features (return -hooks for non-Lua functions) and shows slightly different behavior -in LuaJIT (no per-coroutine hooks, no tail call counting). -
  • -
-
-
- - - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.h deleted file mode 100644 index fbfebee..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.h +++ /dev/null @@ -1,461 +0,0 @@ -/* -** DynASM ARM encoding engine. -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "arm" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, - DASM_IMM, DASM_IMM12, DASM_IMM16, DASM_IMML8, DASM_IMML12, DASM_IMMV8, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -static int dasm_imm12(unsigned int n) -{ - int i; - for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30)) - if (n <= 255) return (int)(n + (i << 8)); - return -1; -} - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: - case DASM_IMM16: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); - if ((ins & 0x8000)) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - case DASM_IMMV8: - CK((n & 3) == 0, RANGE_I); - n >>= 2; - /* fallthrough */ - case DASM_IMML8: - case DASM_IMML12: - CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) : - (((-n)>>((ins>>5)&31)) == 0), RANGE_I); - b[pos++] = n; - break; - case DASM_IMM12: - CK(dasm_imm12((unsigned int)n) != -1, RANGE_I); - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMM12: case DASM_IMM16: - case DASM_IMML8: case DASM_IMML12: case DASM_IMMV8: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048)); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000; - break; - case DASM_REL_LG: - if (n < 0) { - n = (int)((ptrdiff_t)D->globals[-n] - (ptrdiff_t)cp - 4); - goto patchrel; - } - /* fallthrough */ - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) - 4; - patchrel: - if ((ins & 0x800) == 0) { - CK((n & 3) == 0 && ((n+0x02000000) >> 26) == 0, RANGE_REL); - cp[-1] |= ((n >> 2) & 0x00ffffff); - } else if ((ins & 0x1000)) { - CK((n & 3) == 0 && -256 <= n && n <= 256, RANGE_REL); - goto patchimml8; - } else if ((ins & 0x2000) == 0) { - CK((n & 3) == 0 && -4096 <= n && n <= 4096, RANGE_REL); - goto patchimml; - } else { - CK((n & 3) == 0 && -1020 <= n && n <= 1020, RANGE_REL); - n >>= 2; - goto patchimml; - } - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - case DASM_IMM12: - cp[-1] |= dasm_imm12((unsigned int)n); - break; - case DASM_IMM16: - cp[-1] |= ((n & 0xf000) << 4) | (n & 0x0fff); - break; - case DASM_IMML8: patchimml8: - cp[-1] |= n >= 0 ? (0x00800000 | (n & 0x0f) | ((n & 0xf0) << 4)) : - ((-n & 0x0f) | ((-n & 0xf0) << 4)); - break; - case DASM_IMML12: case DASM_IMMV8: patchimml: - cp[-1] |= n >= 0 ? (0x00800000 | n) : (-n); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.lua deleted file mode 100644 index 3b4db86..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm.lua +++ /dev/null @@ -1,1125 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM ARM module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "arm", - description = "DynASM ARM module", - version = "1.5.0", - vernum = 10500, - release = "2021-05-02", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable, rawget = assert, setmetatable, rawget -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub -local concat, sort, insert = table.concat, table.sort, table.insert -local bit = bit or require("bit") -local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift -local ror, tohex = bit.ror, bit.tohex - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12", "IMMV8", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0x000fffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - if n <= 0x000fffff then - insert(actlist, pos+1, n) - n = map_action.ESC * 0x10000 - end - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. - --- Ext. register name -> int. name. -local map_archdef = { sp = "r13", lr = "r14", pc = "r15", } - --- Int. register name -> ext. name. -local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", } - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - return map_reg_rev[s] or s -end - -local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, } - -local map_cond = { - eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, - hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, - hs = 2, lo = 3, -} - ------------------------------------------------------------------------------- - --- Template strings for ARM instructions. -local map_op = { - -- Basic data processing instructions. - and_3 = "e0000000DNPs", - eor_3 = "e0200000DNPs", - sub_3 = "e0400000DNPs", - rsb_3 = "e0600000DNPs", - add_3 = "e0800000DNPs", - adc_3 = "e0a00000DNPs", - sbc_3 = "e0c00000DNPs", - rsc_3 = "e0e00000DNPs", - tst_2 = "e1100000NP", - teq_2 = "e1300000NP", - cmp_2 = "e1500000NP", - cmn_2 = "e1700000NP", - orr_3 = "e1800000DNPs", - mov_2 = "e1a00000DPs", - bic_3 = "e1c00000DNPs", - mvn_2 = "e1e00000DPs", - - and_4 = "e0000000DNMps", - eor_4 = "e0200000DNMps", - sub_4 = "e0400000DNMps", - rsb_4 = "e0600000DNMps", - add_4 = "e0800000DNMps", - adc_4 = "e0a00000DNMps", - sbc_4 = "e0c00000DNMps", - rsc_4 = "e0e00000DNMps", - tst_3 = "e1100000NMp", - teq_3 = "e1300000NMp", - cmp_3 = "e1500000NMp", - cmn_3 = "e1700000NMp", - orr_4 = "e1800000DNMps", - mov_3 = "e1a00000DMps", - bic_4 = "e1c00000DNMps", - mvn_3 = "e1e00000DMps", - - lsl_3 = "e1a00000DMws", - lsr_3 = "e1a00020DMws", - asr_3 = "e1a00040DMws", - ror_3 = "e1a00060DMws", - rrx_2 = "e1a00060DMs", - - -- Multiply and multiply-accumulate. - mul_3 = "e0000090NMSs", - mla_4 = "e0200090NMSDs", - umaal_4 = "e0400090DNMSs", -- v6 - mls_4 = "e0600090DNMSs", -- v6T2 - umull_4 = "e0800090DNMSs", - umlal_4 = "e0a00090DNMSs", - smull_4 = "e0c00090DNMSs", - smlal_4 = "e0e00090DNMSs", - - -- Halfword multiply and multiply-accumulate. - smlabb_4 = "e1000080NMSD", -- v5TE - smlatb_4 = "e10000a0NMSD", -- v5TE - smlabt_4 = "e10000c0NMSD", -- v5TE - smlatt_4 = "e10000e0NMSD", -- v5TE - smlawb_4 = "e1200080NMSD", -- v5TE - smulwb_3 = "e12000a0NMS", -- v5TE - smlawt_4 = "e12000c0NMSD", -- v5TE - smulwt_3 = "e12000e0NMS", -- v5TE - smlalbb_4 = "e1400080NMSD", -- v5TE - smlaltb_4 = "e14000a0NMSD", -- v5TE - smlalbt_4 = "e14000c0NMSD", -- v5TE - smlaltt_4 = "e14000e0NMSD", -- v5TE - smulbb_3 = "e1600080NMS", -- v5TE - smultb_3 = "e16000a0NMS", -- v5TE - smulbt_3 = "e16000c0NMS", -- v5TE - smultt_3 = "e16000e0NMS", -- v5TE - - -- Miscellaneous data processing instructions. - clz_2 = "e16f0f10DM", -- v5T - rev_2 = "e6bf0f30DM", -- v6 - rev16_2 = "e6bf0fb0DM", -- v6 - revsh_2 = "e6ff0fb0DM", -- v6 - sel_3 = "e6800fb0DNM", -- v6 - usad8_3 = "e780f010NMS", -- v6 - usada8_4 = "e7800010NMSD", -- v6 - rbit_2 = "e6ff0f30DM", -- v6T2 - movw_2 = "e3000000DW", -- v6T2 - movt_2 = "e3400000DW", -- v6T2 - -- Note: the X encodes width-1, not width. - sbfx_4 = "e7a00050DMvX", -- v6T2 - ubfx_4 = "e7e00050DMvX", -- v6T2 - -- Note: the X encodes the msb field, not the width. - bfc_3 = "e7c0001fDvX", -- v6T2 - bfi_4 = "e7c00010DMvX", -- v6T2 - - -- Packing and unpacking instructions. - pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6 - pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6 - sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6 - sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6 - sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6 - sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6 - sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6 - sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6 - uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6 - uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6 - uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6 - uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6 - uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6 - uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6 - - -- Saturating instructions. - qadd_3 = "e1000050DMN", -- v5TE - qsub_3 = "e1200050DMN", -- v5TE - qdadd_3 = "e1400050DMN", -- v5TE - qdsub_3 = "e1600050DMN", -- v5TE - -- Note: the X for ssat* encodes sat_imm-1, not sat_imm. - ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6 - usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6 - ssat16_3 = "e6a00f30DXM", -- v6 - usat16_3 = "e6e00f30DXM", -- v6 - - -- Parallel addition and subtraction. - sadd16_3 = "e6100f10DNM", -- v6 - sasx_3 = "e6100f30DNM", -- v6 - ssax_3 = "e6100f50DNM", -- v6 - ssub16_3 = "e6100f70DNM", -- v6 - sadd8_3 = "e6100f90DNM", -- v6 - ssub8_3 = "e6100ff0DNM", -- v6 - qadd16_3 = "e6200f10DNM", -- v6 - qasx_3 = "e6200f30DNM", -- v6 - qsax_3 = "e6200f50DNM", -- v6 - qsub16_3 = "e6200f70DNM", -- v6 - qadd8_3 = "e6200f90DNM", -- v6 - qsub8_3 = "e6200ff0DNM", -- v6 - shadd16_3 = "e6300f10DNM", -- v6 - shasx_3 = "e6300f30DNM", -- v6 - shsax_3 = "e6300f50DNM", -- v6 - shsub16_3 = "e6300f70DNM", -- v6 - shadd8_3 = "e6300f90DNM", -- v6 - shsub8_3 = "e6300ff0DNM", -- v6 - uadd16_3 = "e6500f10DNM", -- v6 - uasx_3 = "e6500f30DNM", -- v6 - usax_3 = "e6500f50DNM", -- v6 - usub16_3 = "e6500f70DNM", -- v6 - uadd8_3 = "e6500f90DNM", -- v6 - usub8_3 = "e6500ff0DNM", -- v6 - uqadd16_3 = "e6600f10DNM", -- v6 - uqasx_3 = "e6600f30DNM", -- v6 - uqsax_3 = "e6600f50DNM", -- v6 - uqsub16_3 = "e6600f70DNM", -- v6 - uqadd8_3 = "e6600f90DNM", -- v6 - uqsub8_3 = "e6600ff0DNM", -- v6 - uhadd16_3 = "e6700f10DNM", -- v6 - uhasx_3 = "e6700f30DNM", -- v6 - uhsax_3 = "e6700f50DNM", -- v6 - uhsub16_3 = "e6700f70DNM", -- v6 - uhadd8_3 = "e6700f90DNM", -- v6 - uhsub8_3 = "e6700ff0DNM", -- v6 - - -- Load/store instructions. - str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL", - strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL", - ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL", - ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL", - strh_2 = "e00000b0DL", strh_3 = "e00000b0DL", - ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL", - ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE - ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL", - strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE - ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL", - - ldm_2 = "e8900000oR", ldmia_2 = "e8900000oR", ldmfd_2 = "e8900000oR", - ldmda_2 = "e8100000oR", ldmfa_2 = "e8100000oR", - ldmdb_2 = "e9100000oR", ldmea_2 = "e9100000oR", - ldmib_2 = "e9900000oR", ldmed_2 = "e9900000oR", - stm_2 = "e8800000oR", stmia_2 = "e8800000oR", stmfd_2 = "e8800000oR", - stmda_2 = "e8000000oR", stmfa_2 = "e8000000oR", - stmdb_2 = "e9000000oR", stmea_2 = "e9000000oR", - stmib_2 = "e9800000oR", stmed_2 = "e9800000oR", - pop_1 = "e8bd0000R", push_1 = "e92d0000R", - - -- Branch instructions. - b_1 = "ea000000B", - bl_1 = "eb000000B", - blx_1 = "e12fff30C", - bx_1 = "e12fff10M", - - -- Miscellaneous instructions. - nop_0 = "e1a00000", - mrs_1 = "e10f0000D", - bkpt_1 = "e1200070K", -- v5T - svc_1 = "ef000000T", swi_1 = "ef000000T", - ud_0 = "e7f001f0", - - -- VFP instructions. - ["vadd.f32_3"] = "ee300a00dnm", - ["vadd.f64_3"] = "ee300b00Gdnm", - ["vsub.f32_3"] = "ee300a40dnm", - ["vsub.f64_3"] = "ee300b40Gdnm", - ["vmul.f32_3"] = "ee200a00dnm", - ["vmul.f64_3"] = "ee200b00Gdnm", - ["vnmul.f32_3"] = "ee200a40dnm", - ["vnmul.f64_3"] = "ee200b40Gdnm", - ["vmla.f32_3"] = "ee000a00dnm", - ["vmla.f64_3"] = "ee000b00Gdnm", - ["vmls.f32_3"] = "ee000a40dnm", - ["vmls.f64_3"] = "ee000b40Gdnm", - ["vnmla.f32_3"] = "ee100a40dnm", - ["vnmla.f64_3"] = "ee100b40Gdnm", - ["vnmls.f32_3"] = "ee100a00dnm", - ["vnmls.f64_3"] = "ee100b00Gdnm", - ["vdiv.f32_3"] = "ee800a00dnm", - ["vdiv.f64_3"] = "ee800b00Gdnm", - - ["vabs.f32_2"] = "eeb00ac0dm", - ["vabs.f64_2"] = "eeb00bc0Gdm", - ["vneg.f32_2"] = "eeb10a40dm", - ["vneg.f64_2"] = "eeb10b40Gdm", - ["vsqrt.f32_2"] = "eeb10ac0dm", - ["vsqrt.f64_2"] = "eeb10bc0Gdm", - ["vcmp.f32_2"] = "eeb40a40dm", - ["vcmp.f64_2"] = "eeb40b40Gdm", - ["vcmpe.f32_2"] = "eeb40ac0dm", - ["vcmpe.f64_2"] = "eeb40bc0Gdm", - ["vcmpz.f32_1"] = "eeb50a40d", - ["vcmpz.f64_1"] = "eeb50b40Gd", - ["vcmpze.f32_1"] = "eeb50ac0d", - ["vcmpze.f64_1"] = "eeb50bc0Gd", - - vldr_2 = "ed100a00dl|ed100b00Gdl", - vstr_2 = "ed000a00dl|ed000b00Gdl", - vldm_2 = "ec900a00or", - vldmia_2 = "ec900a00or", - vldmdb_2 = "ed100a00or", - vpop_1 = "ecbd0a00r", - vstm_2 = "ec800a00or", - vstmia_2 = "ec800a00or", - vstmdb_2 = "ed000a00or", - vpush_1 = "ed2d0a00r", - - ["vmov.f32_2"] = "eeb00a40dm|eeb00a00dY", -- #imm is VFPv3 only - ["vmov.f64_2"] = "eeb00b40Gdm|eeb00b00GdY", -- #imm is VFPv3 only - vmov_2 = "ee100a10Dn|ee000a10nD", - vmov_3 = "ec500a10DNm|ec400a10mDN|ec500b10GDNm|ec400b10GmDN", - - vmrs_0 = "eef1fa10", - vmrs_1 = "eef10a10D", - vmsr_1 = "eee10a10D", - - ["vcvt.s32.f32_2"] = "eebd0ac0dm", - ["vcvt.s32.f64_2"] = "eebd0bc0dGm", - ["vcvt.u32.f32_2"] = "eebc0ac0dm", - ["vcvt.u32.f64_2"] = "eebc0bc0dGm", - ["vcvtr.s32.f32_2"] = "eebd0a40dm", - ["vcvtr.s32.f64_2"] = "eebd0b40dGm", - ["vcvtr.u32.f32_2"] = "eebc0a40dm", - ["vcvtr.u32.f64_2"] = "eebc0b40dGm", - ["vcvt.f32.s32_2"] = "eeb80ac0dm", - ["vcvt.f64.s32_2"] = "eeb80bc0GdFm", - ["vcvt.f32.u32_2"] = "eeb80a40dm", - ["vcvt.f64.u32_2"] = "eeb80b40GdFm", - ["vcvt.f32.f64_2"] = "eeb70bc0dGm", - ["vcvt.f64.f32_2"] = "eeb70ac0GdFm", - - -- VFPv4 only: - ["vfma.f32_3"] = "eea00a00dnm", - ["vfma.f64_3"] = "eea00b00Gdnm", - ["vfms.f32_3"] = "eea00a40dnm", - ["vfms.f64_3"] = "eea00b40Gdnm", - ["vfnma.f32_3"] = "ee900a40dnm", - ["vfnma.f64_3"] = "ee900b40Gdnm", - ["vfnms.f32_3"] = "ee900a00dnm", - ["vfnms.f64_3"] = "ee900b00Gdnm", - - -- NYI: Advanced SIMD instructions. - - -- NYI: I have no need for these instructions right now: - -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh - -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe - -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb - -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2 -} - --- Add mnemonics for "s" variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "s" then - local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2) - t[sub(k, 1, -3).."s"..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r(1?[0-9])$") - if r then - r = tonumber(r) - if r <= 15 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_gpr_pm(expr) - local pm, expr2 = match(expr, "^([+-]?)(.*)$") - return parse_gpr(expr2), (pm == "-") -end - -local function parse_vr(expr, tp) - local t, r = match(expr, "^([sd])([0-9]+)$") - if t == tp then - r = tonumber(r) - if r <= 31 then - if t == "s" then return shr(r, 1), band(r, 1) end - return band(r, 15), shr(r, 4) - end - end - werror("bad register name `"..expr.."'") -end - -local function parse_reglist(reglist) - reglist = match(reglist, "^{%s*([^}]*)}$") - if not reglist then werror("register list expected") end - local rr = 0 - for p in gmatch(reglist..",", "%s*([^,]*),") do - local rbit = shl(1, parse_gpr(gsub(p, "%s+$", ""))) - if band(rr, rbit) ~= 0 then - werror("duplicate register `"..p.."'") - end - rr = rr + rbit - end - return rr -end - -local function parse_vrlist(reglist) - local ta, ra, tb, rb = match(reglist, - "^{%s*([sd])([0-9]+)%s*%-%s*([sd])([0-9]+)%s*}$") - ra, rb = tonumber(ra), tonumber(rb) - if ta and ta == tb and ra and rb and ra <= 31 and rb <= 31 and ra <= rb then - local nr = rb+1 - ra - if ta == "s" then - return shl(shr(ra,1),12)+shl(band(ra,1),22) + nr - else - return shl(band(ra,15),12)+shl(shr(ra,4),22) + nr*2 + 0x100 - end - end - werror("register list expected") -end - -local function parse_imm(imm, bits, shift, scale, signed) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_imm12(imm) - local n = tonumber(imm) - if n then - local m = band(n) - for i=0,-15,-1 do - if shr(m, 8) == 0 then return m + shl(band(i, 15), 8) end - m = ror(m, 2) - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM12", 0, imm) - return 0 - end -end - -local function parse_imm16(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - if shr(n, 16) == 0 then return band(n, 0x0fff) + shl(band(n, 0xf000), 4) end - werror("out of range immediate `"..imm.."'") - else - waction("IMM16", 32*16, imm) - return 0 - end -end - -local function parse_imm_load(imm, ext) - local n = tonumber(imm) - if n then - if ext then - if n >= -255 and n <= 255 then - local up = 0x00800000 - if n < 0 then n = -n; up = 0 end - return shl(band(n, 0xf0), 4) + band(n, 0x0f) + up - end - else - if n >= -4095 and n <= 4095 then - if n >= 0 then return n+0x00800000 end - return -n - end - end - werror("out of range immediate `"..imm.."'") - else - waction(ext and "IMML8" or "IMML12", 32768 + shl(ext and 8 or 12, 5), imm) - return 0 - end -end - -local function parse_shift(shift, gprok) - if shift == "rrx" then - return 3 * 32 - else - local s, s2 = match(shift, "^(%S+)%s*(.*)$") - s = map_shift[s] - if not s then werror("expected shift operand") end - if sub(s2, 1, 1) == "#" then - return parse_imm(s2, 5, 7, 0, false) + shl(s, 5) - else - if not gprok then werror("expected immediate shift operand") end - return shl(parse_gpr(s2), 8) + shl(s, 5) + 16 - end - end -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - -local function parse_load(params, nparams, n, op) - local oplo = band(op, 255) - local ext, ldrd = (oplo ~= 0), (oplo == 208) - local d - if (ldrd or oplo == 240) then - d = band(shr(op, 12), 15) - if band(d, 1) ~= 0 then werror("odd destination register") end - end - local pn = params[n] - local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") - local p2 = params[n+1] - if not p1 then - if not p2 then - if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then - local mode, n, s = parse_label(pn, false) - waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1) - return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0) - end - local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local d, tp = parse_gpr(reg) - if tp then - waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), - format(tp.ctypefmt, tailr)) - return op + shl(d, 16) + 0x01000000 + (ext and 0x00400000 or 0) - end - end - end - werror("expected address operand") - end - if wb == "!" then op = op + 0x00200000 end - if p2 then - if wb == "!" then werror("bad use of '!'") end - local p3 = params[n+2] - op = op + shl(parse_gpr(p1), 16) - local imm = match(p2, "^#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - if p3 then werror("too many parameters") end - op = op + m + (ext and 0x00400000 or 0) - else - local m, neg = parse_gpr_pm(p2) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 then op = op + parse_shift(p3) end - end - else - local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$") - op = op + shl(parse_gpr(p1a), 16) + 0x01000000 - if p2 ~= "" then - local imm = match(p2, "^,%s*#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - op = op + m + (ext and 0x00400000 or 0) - else - local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$") - local m, neg = parse_gpr_pm(p2a) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 ~= "" then - if ext then werror("too many parameters") end - op = op + parse_shift(p3) - end - end - else - if wb == "!" then werror("bad use of '!'") end - op = op + (ext and 0x00c00000 or 0x00800000) - end - end - return op -end - -local function parse_vload(q) - local reg, imm = match(q, "^%[%s*([^,%s]*)%s*(.*)%]$") - if reg then - local d = shl(parse_gpr(reg), 16) - if imm == "" then return d end - imm = match(imm, "^,%s*#(.*)$") - if imm then - local n = tonumber(imm) - if n then - if n >= -1020 and n <= 1020 and n%4 == 0 then - return d + (n >= 0 and n/4+0x00800000 or -n/4) - end - werror("out of range immediate `"..imm.."'") - else - waction("IMMV8", 32768 + 32*8, imm) - return d - end - end - else - if match(q, "^[<>=%-]") or match(q, "^extern%s+") then - local mode, n, s = parse_label(q, false) - waction("REL_"..mode, n + 0x2800, s, 1) - return 15 * 65536 - end - local reg, tailr = match(q, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local d, tp = parse_gpr(reg) - if tp then - waction("IMMV8", 32768 + 32*8, format(tp.ctypefmt, tailr)) - return shl(d, 16) - end - end - end - werror("expected address operand") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -local function parse_template(params, template, nparams, pos) - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - local vr = "s" - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - local q = params[n] - if p == "D" then - op = op + shl(parse_gpr(q), 12); n = n + 1 - elseif p == "N" then - op = op + shl(parse_gpr(q), 16); n = n + 1 - elseif p == "S" then - op = op + shl(parse_gpr(q), 8); n = n + 1 - elseif p == "M" then - op = op + parse_gpr(q); n = n + 1 - elseif p == "d" then - local r,h = parse_vr(q, vr); op = op+shl(r,12)+shl(h,22); n = n + 1 - elseif p == "n" then - local r,h = parse_vr(q, vr); op = op+shl(r,16)+shl(h,7); n = n + 1 - elseif p == "m" then - local r,h = parse_vr(q, vr); op = op+r+shl(h,5); n = n + 1 - elseif p == "P" then - local imm = match(q, "^#(.*)$") - if imm then - op = op + parse_imm12(imm) + 0x02000000 - else - op = op + parse_gpr(q) - end - n = n + 1 - elseif p == "p" then - op = op + parse_shift(q, true); n = n + 1 - elseif p == "L" then - op = parse_load(params, nparams, n, op) - elseif p == "l" then - op = op + parse_vload(q) - elseif p == "B" then - local mode, n, s = parse_label(q, false) - waction("REL_"..mode, n, s, 1) - elseif p == "C" then -- blx gpr vs. blx label. - if match(q, "^([%w_]+):(r1?[0-9])$") or match(q, "^r(1?[0-9])$") then - op = op + parse_gpr(q) - else - if op < 0xe0000000 then werror("unconditional instruction") end - local mode, n, s = parse_label(q, false) - waction("REL_"..mode, n, s, 1) - op = 0xfa000000 - end - elseif p == "F" then - vr = "s" - elseif p == "G" then - vr = "d" - elseif p == "o" then - local r, wb = match(q, "^([^!]*)(!?)$") - op = op + shl(parse_gpr(r), 16) + (wb == "!" and 0x00200000 or 0) - n = n + 1 - elseif p == "R" then - op = op + parse_reglist(q); n = n + 1 - elseif p == "r" then - op = op + parse_vrlist(q); n = n + 1 - elseif p == "W" then - op = op + parse_imm16(q); n = n + 1 - elseif p == "v" then - op = op + parse_imm(q, 5, 7, 0, false); n = n + 1 - elseif p == "w" then - local imm = match(q, "^#(.*)$") - if imm then - op = op + parse_imm(q, 5, 7, 0, false); n = n + 1 - else - op = op + shl(parse_gpr(q), 8) + 16 - end - elseif p == "X" then - op = op + parse_imm(q, 5, 16, 0, false); n = n + 1 - elseif p == "Y" then - local imm = tonumber(match(q, "^#(.*)$")); n = n + 1 - if not imm or shr(imm, 8) ~= 0 then - werror("bad immediate operand") - end - op = op + shl(band(imm, 0xf0), 12) + band(imm, 0x0f) - elseif p == "K" then - local imm = tonumber(match(q, "^#(.*)$")); n = n + 1 - if not imm or shr(imm, 16) ~= 0 then - werror("bad immediate operand") - end - op = op + shl(band(imm, 0xfff0), 4) + band(imm, 0x000f) - elseif p == "T" then - op = op + parse_imm(q, 24, 0, 0, false); n = n + 1 - elseif p == "s" then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - -map_op[".template__"] = function(params, template, nparams) - if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions. - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - local lpos, apos, spos = #actlist, #actargs, secpos - - local ok, err - for t in gmatch(template, "[^|]+") do - ok, err = pcall(parse_template, params, t, nparams, pos) - if ok then return end - secpos = spos - actlist[lpos+1] = nil - actlist[lpos+2] = nil - actlist[lpos+3] = nil - actargs[apos+1] = nil - actargs[apos+2] = nil - actargs[apos+3] = nil - end - error(err, 0) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = function(t, k) - local v = map_coreop[k] - if v then return v end - local k1, cc, k2 = match(k, "^(.-)(..)([._].*)$") - local cv = map_cond[cc] - if cv then - local v = rawget(t, k1..k2) - if type(v) == "string" then - local scv = format("%x", cv) - return gsub(scv..sub(v, 2), "|e", "|"..scv) - end - end - end }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.h deleted file mode 100644 index 47c9c37..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.h +++ /dev/null @@ -1,563 +0,0 @@ -/* -** DynASM ARM64 encoding engine. -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "arm64" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_REL_A, - DASM_IMM, DASM_IMM6, DASM_IMM12, DASM_IMM13W, DASM_IMM13X, DASM_IMML, - DASM_IMMV, DASM_VREG, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_RANGE_VREG 0x16000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -static int dasm_imm12(unsigned int n) -{ - if ((n >> 12) == 0) - return n; - else if ((n & 0xff000fff) == 0) - return (n >> 12) | 0x1000; - else - return -1; -} - -static int dasm_ffs(unsigned long long x) -{ - int n = -1; - while (x) { x >>= 1; n++; } - return n; -} - -static int dasm_imm13(int lo, int hi) -{ - int inv = 0, w = 64, s = 0xfff, xa, xb; - unsigned long long n = (((unsigned long long)hi) << 32) | (unsigned int)lo; - unsigned long long m = 1ULL, a, b, c; - if (n & 1) { n = ~n; inv = 1; } - a = n & (unsigned long long)-(long long)n; - b = (n+a)&(unsigned long long)-(long long)(n+a); - c = (n+a-b)&(unsigned long long)-(long long)(n+a-b); - xa = dasm_ffs(a); xb = dasm_ffs(b); - if (c) { - w = dasm_ffs(c) - xa; - if (w == 32) m = 0x0000000100000001UL; - else if (w == 16) m = 0x0001000100010001UL; - else if (w == 8) m = 0x0101010101010101UL; - else if (w == 4) m = 0x1111111111111111UL; - else if (w == 2) m = 0x5555555555555555UL; - else return -1; - s = (-2*w & 0x3f) - 1; - } else if (!a) { - return -1; - } else if (xb == -1) { - xb = 64; - } - if ((b-a) * m != n) return -1; - if (inv) { - return ((w - xb) << 6) | (s+w+xa-xb); - } else { - return ((w - xa) << 6) | (s+xb-xa); - } - return -1; -} - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: if ((ins & 0x8000)) ofs += 8; break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - if ((ins & 0x8000)) ofs += 8; - break; - case DASM_REL_A: - b[pos++] = n; - b[pos++] = va_arg(ap, int); - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if ((ins & 0x8000)) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - case DASM_IMM6: - CK((n >> 6) == 0, RANGE_I); - b[pos++] = n; - break; - case DASM_IMM12: - CK(dasm_imm12((unsigned int)n) != -1, RANGE_I); - b[pos++] = n; - break; - case DASM_IMM13W: - CK(dasm_imm13(n, n) != -1, RANGE_I); - b[pos++] = n; - break; - case DASM_IMM13X: { - int m = va_arg(ap, int); - CK(dasm_imm13(n, m) != -1, RANGE_I); - b[pos++] = n; - b[pos++] = m; - break; - } - case DASM_IMML: { -#ifdef DASM_CHECKS - int scale = (ins & 3); - CK((!(n & ((1<>scale) < 4096) || - (unsigned int)(n+256) < 512, RANGE_I); -#endif - b[pos++] = n; - break; - } - case DASM_IMMV: - ofs += 4; - b[pos++] = n; - break; - case DASM_VREG: - CK(n < 32, RANGE_VREG); - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMM6: case DASM_IMM12: case DASM_IMM13W: - case DASM_IMML: case DASM_IMMV: case DASM_VREG: pos++; break; - case DASM_IMM13X: case DASM_REL_A: pos += 2; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(int)(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048)); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xd503201f; - break; - case DASM_REL_LG: - if (n < 0) { - ptrdiff_t na = (ptrdiff_t)D->globals[-n] - (ptrdiff_t)cp + 4; - n = (int)na; - CK((ptrdiff_t)n == na, RANGE_REL); - goto patchrel; - } - /* fallthrough */ - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) + 4; - patchrel: - if (!(ins & 0xf800)) { /* B, BL */ - CK((n & 3) == 0 && ((n+0x08000000) >> 28) == 0, RANGE_REL); - cp[-1] |= ((n >> 2) & 0x03ffffff); - } else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */ - CK((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, RANGE_REL); - cp[-1] |= ((n << 3) & 0x00ffffe0); - } else if ((ins & 0x3000) == 0x2000) { /* ADR */ - CK(((n+0x00100000) >> 21) == 0, RANGE_REL); - cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29); - } else if ((ins & 0x3000) == 0x3000) { /* ADRP */ - cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29); - } else if ((ins & 0x1000)) { /* TBZ, TBNZ */ - CK((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, RANGE_REL); - cp[-1] |= ((n << 3) & 0x0007ffe0); - } else if ((ins & 0x8000)) { /* absolute */ - cp[0] = (unsigned int)((ptrdiff_t)cp - 4 + n); - cp[1] = (unsigned int)(((ptrdiff_t)cp - 4 + n) >> 32); - cp += 2; - } - break; - case DASM_REL_A: { - ptrdiff_t na = (((ptrdiff_t)(*b++) << 32) | (unsigned int)n); - if ((ins & 0x3000) == 0x3000) { /* ADRP */ - ins &= ~0x1000; - na = (na >> 12) - (((ptrdiff_t)cp - 4) >> 12); - } else { - na = na - (ptrdiff_t)cp + 4; - } - n = (int)na; - CK((ptrdiff_t)n == na, RANGE_REL); - goto patchrel; - } - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - case DASM_IMM6: - cp[-1] |= ((n&31) << 19) | ((n&32) << 26); - break; - case DASM_IMM12: - cp[-1] |= (dasm_imm12((unsigned int)n) << 10); - break; - case DASM_IMM13W: - cp[-1] |= (dasm_imm13(n, n) << 10); - break; - case DASM_IMM13X: - cp[-1] |= (dasm_imm13(n, *b++) << 10); - break; - case DASM_IMML: { - int scale = (ins & 3); - cp[-1] |= (!(n & ((1<>scale) < 4096) ? - ((n << (10-scale)) | 0x01000000) : ((n & 511) << 12); - break; - } - case DASM_IMMV: - *cp++ = n; - break; - case DASM_VREG: - cp[-1] |= (n & 0x1f) << (ins & 0x1f); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); - return D->status; -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.lua deleted file mode 100644 index 1f581ba..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_arm64.lua +++ /dev/null @@ -1,1219 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM ARM64 module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "arm", - description = "DynASM ARM64 module", - version = "1.5.0", - vernum = 10500, - release = "2021-05-02", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable, rawget = assert, setmetatable, rawget -local _s = string -local format, byte, char = _s.format, _s.byte, _s.char -local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub -local concat, sort, insert = table.concat, table.sort, table.insert -local bit = bit or require("bit") -local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift -local ror, tohex, tobit = bit.ror, bit.tohex, bit.tobit - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "REL_A", - "IMM", "IMM6", "IMM12", "IMM13W", "IMM13X", "IMML", "IMMV", - "VREG", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0x000fffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - if n <= 0x000fffff then - insert(actlist, pos+1, n) - n = map_action.ESC * 0x10000 - end - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. - --- Ext. register name -> int. name. -local map_archdef = { xzr = "@x31", wzr = "@w31", lr = "x30", } - --- Int. register name -> ext. name. -local map_reg_rev = { ["@x31"] = "xzr", ["@w31"] = "wzr", x30 = "lr", } - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - return map_reg_rev[s] or s -end - -local map_shift = { lsl = 0, lsr = 1, asr = 2, } - -local map_extend = { - uxtb = 0, uxth = 1, uxtw = 2, uxtx = 3, - sxtb = 4, sxth = 5, sxtw = 6, sxtx = 7, -} - -local map_cond = { - eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, - hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, - hs = 2, lo = 3, -} - ------------------------------------------------------------------------------- - -local parse_reg_type - -local function parse_reg(expr, shift, no_vreg) - if not expr then werror("expected register name") end - local tname, ovreg = match(expr, "^([%w_]+):(@?%l%d+)$") - if not tname then - tname, ovreg = match(expr, "^([%w_]+):(R[xwqdshb]%b())$") - end - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local ok31, rt, r = match(expr, "^(@?)([xwqdshb])([123]?[0-9])$") - if r then - r = tonumber(r) - if r <= 30 or (r == 31 and ok31 ~= "" or (rt ~= "w" and rt ~= "x")) then - if not parse_reg_type then - parse_reg_type = rt - elseif parse_reg_type ~= rt then - werror("register size mismatch") - end - return shl(r, shift), tp - end - end - local vrt, vreg = match(expr, "^R([xwqdshb])(%b())$") - if vreg then - if not parse_reg_type then - parse_reg_type = vrt - elseif parse_reg_type ~= vrt then - werror("register size mismatch") - end - if not no_vreg then waction("VREG", shift, vreg) end - return 0 - end - werror("bad register name `"..expr.."'") -end - -local function parse_reg_base(expr) - if expr == "sp" then return 0x3e0 end - local base, tp = parse_reg(expr, 5) - if parse_reg_type ~= "x" then werror("bad register type") end - parse_reg_type = false - return base, tp -end - -local parse_ctx = {} - -local loadenv = setfenv and function(s) - local code = loadstring(s, "") - if code then setfenv(code, parse_ctx) end - return code -end or function(s) - return load(s, "", nil, parse_ctx) -end - --- Try to parse simple arithmetic, too, since some basic ops are aliases. -local function parse_number(n) - local x = tonumber(n) - if x then return x end - local code = loadenv("return "..n) - if code then - local ok, y = pcall(code) - if ok and type(y) == "number" then return y end - end - return nil -end - -local function parse_imm(imm, bits, shift, scale, signed) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = parse_number(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_imm12(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = parse_number(imm) - if n then - if shr(n, 12) == 0 then - return shl(n, 10) - elseif band(n, 0xff000fff) == 0 then - return shr(n, 2) + 0x00400000 - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM12", 0, imm) - return 0 - end -end - -local function parse_imm13(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = parse_number(imm) - local r64 = parse_reg_type == "x" - if n and n % 1 == 0 and n >= 0 and n <= 0xffffffff then - local inv = false - if band(n, 1) == 1 then n = bit.bnot(n); inv = true end - local t = {} - for i=1,32 do t[i] = band(n, 1); n = shr(n, 1) end - local b = table.concat(t) - b = b..(r64 and (inv and "1" or "0"):rep(32) or b) - local p0, p1, p0a, p1a = b:match("^(0+)(1+)(0*)(1*)") - if p0 then - local w = p1a == "" and (r64 and 64 or 32) or #p1+#p0a - if band(w, w-1) == 0 and b == b:sub(1, w):rep(64/w) then - local s = band(-2*w, 0x3f) - 1 - if w == 64 then s = s + 0x1000 end - if inv then - return shl(w-#p1-#p0, 16) + shl(s+w-#p1, 10) - else - return shl(w-#p0, 16) + shl(s+#p1, 10) - end - end - end - werror("out of range immediate `"..imm.."'") - elseif r64 then - waction("IMM13X", 0, format("(unsigned int)(%s)", imm)) - actargs[#actargs+1] = format("(unsigned int)((unsigned long long)(%s)>>32)", imm) - return 0 - else - waction("IMM13W", 0, imm) - return 0 - end -end - -local function parse_imm6(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = parse_number(imm) - if n then - if n >= 0 and n <= 63 then - return shl(band(n, 0x1f), 19) + (n >= 32 and 0x80000000 or 0) - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM6", 0, imm) - return 0 - end -end - -local function parse_imm_load(imm, scale) - local n = parse_number(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n and m >= 0 and m < 0x1000 then - return shl(m, 10) + 0x01000000 -- Scaled, unsigned 12 bit offset. - elseif n >= -256 and n < 256 then - return shl(band(n, 511), 12) -- Unscaled, signed 9 bit offset. - end - werror("out of range immediate `"..imm.."'") - else - waction("IMML", scale, imm) - return 0 - end -end - -local function parse_fpimm(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = parse_number(imm) - if n then - local m, e = math.frexp(n) - local s, e2 = 0, band(e-2, 7) - if m < 0 then m = -m; s = 0x00100000 end - m = m*32-16 - if m % 1 == 0 and m >= 0 and m <= 15 and sar(shl(e2, 29), 29)+2 == e then - return s + shl(e2, 17) + shl(m, 13) - end - werror("out of range immediate `"..imm.."'") - else - werror("NYI fpimm action") - end -end - -local function parse_shift(expr) - local s, s2 = match(expr, "^(%S+)%s*(.*)$") - s = map_shift[s] - if not s then werror("expected shift operand") end - return parse_imm(s2, 6, 10, 0, false) + shl(s, 22) -end - -local function parse_lslx16(expr) - local n = match(expr, "^lsl%s*#(%d+)$") - n = tonumber(n) - if not n then werror("expected shift operand") end - if band(n, parse_reg_type == "x" and 0xffffffcf or 0xffffffef) ~= 0 then - werror("bad shift amount") - end - return shl(n, 17) -end - -local function parse_extend(expr) - local s, s2 = match(expr, "^(%S+)%s*(.*)$") - if s == "lsl" then - s = parse_reg_type == "x" and 3 or 2 - else - s = map_extend[s] - end - if not s then werror("expected extend operand") end - return (s2 == "" and 0 or parse_imm(s2, 3, 10, 0, false)) + shl(s, 13) -end - -local function parse_cond(expr, inv) - local c = map_cond[expr] - if not c then werror("expected condition operand") end - return shl(bit.bxor(c, inv), 12) -end - -local function parse_load(params, nparams, n, op) - if params[n+2] then werror("too many operands") end - local scale = shr(op, 30) - local pn, p2 = params[n], params[n+1] - local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") - if not p1 then - if not p2 then - local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local base, tp = parse_reg_base(reg) - if tp then - waction("IMML", scale, format(tp.ctypefmt, tailr)) - return op + base - end - end - end - werror("expected address operand") - end - if p2 then - if wb == "!" then werror("bad use of '!'") end - op = op + parse_reg_base(p1) + parse_imm(p2, 9, 12, 0, true) + 0x400 - elseif wb == "!" then - local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$") - if not p1a then werror("bad use of '!'") end - op = op + parse_reg_base(p1a) + parse_imm(p2a, 9, 12, 0, true) + 0xc00 - else - local p1a, p2a = match(p1, "^([^,%s]*)%s*(.*)$") - op = op + parse_reg_base(p1a) - if p2a ~= "" then - local imm = match(p2a, "^,%s*#(.*)$") - if imm then - op = op + parse_imm_load(imm, scale) - else - local p2b, p3b, p3s = match(p2a, "^,%s*([^,%s]*)%s*,?%s*(%S*)%s*(.*)$") - op = op + parse_reg(p2b, 16) + 0x00200800 - if parse_reg_type ~= "x" and parse_reg_type ~= "w" then - werror("bad index register type") - end - if p3b == "" then - if parse_reg_type ~= "x" then werror("bad index register type") end - op = op + 0x6000 - else - if p3s == "" or p3s == "#0" then - elseif p3s == "#"..scale then - op = op + 0x1000 - else - werror("bad scale") - end - if parse_reg_type == "x" then - if p3b == "lsl" and p3s ~= "" then op = op + 0x6000 - elseif p3b == "sxtx" then op = op + 0xe000 - else - werror("bad extend/shift specifier") - end - else - if p3b == "uxtw" then op = op + 0x4000 - elseif p3b == "sxtw" then op = op + 0xc000 - else - werror("bad extend/shift specifier") - end - end - end - end - else - if wb == "!" then werror("bad use of '!'") end - op = op + 0x01000000 - end - end - return op -end - -local function parse_load_pair(params, nparams, n, op) - if params[n+2] then werror("too many operands") end - local pn, p2 = params[n], params[n+1] - local scale = shr(op, 30) == 0 and 2 or 3 - local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") - if not p1 then - if not p2 then - local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local base, tp = parse_reg_base(reg) - if tp then - waction("IMM", 32768+7*32+15+scale*1024, format(tp.ctypefmt, tailr)) - return op + base + 0x01000000 - end - end - end - werror("expected address operand") - end - if p2 then - if wb == "!" then werror("bad use of '!'") end - op = op + 0x00800000 - else - local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$") - if p1a then p1, p2 = p1a, p2a else p2 = "#0" end - op = op + (wb == "!" and 0x01800000 or 0x01000000) - end - return op + parse_reg_base(p1) + parse_imm(p2, 7, 15, scale, true) -end - -local function parse_label(label, def) - local prefix = label:sub(1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, label:sub(3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[label:sub(3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - -- &expr (pointer) - if label:sub(1, 1) == "&" then - return "A", 0, format("(ptrdiff_t)(%s)", label:sub(2)) - end - end -end - -local function branch_type(op) - if band(op, 0x7c000000) == 0x14000000 then return 0 -- B, BL - elseif shr(op, 24) == 0x54 or band(op, 0x7e000000) == 0x34000000 or - band(op, 0x3b000000) == 0x18000000 then - return 0x800 -- B.cond, CBZ, CBNZ, LDR* literal - elseif band(op, 0x7e000000) == 0x36000000 then return 0x1000 -- TBZ, TBNZ - elseif band(op, 0x9f000000) == 0x10000000 then return 0x2000 -- ADR - elseif band(op, 0x9f000000) == band(0x90000000) then return 0x3000 -- ADRP - else - assert(false, "unknown branch type") - end -end - ------------------------------------------------------------------------------- - -local map_op, op_template - -local function op_alias(opname, f) - return function(params, nparams) - if not params then return "-> "..opname:sub(1, -3) end - f(params, nparams) - op_template(params, map_op[opname], nparams) - end -end - -local function alias_bfx(p) - p[4] = "#("..p[3]:sub(2)..")+("..p[4]:sub(2)..")-1" -end - -local function alias_bfiz(p) - parse_reg(p[1], 0, true) - if parse_reg_type == "w" then - p[3] = "#(32-("..p[3]:sub(2).."))%32" - p[4] = "#("..p[4]:sub(2)..")-1" - else - p[3] = "#(64-("..p[3]:sub(2).."))%64" - p[4] = "#("..p[4]:sub(2)..")-1" - end -end - -local alias_lslimm = op_alias("ubfm_4", function(p) - parse_reg(p[1], 0, true) - local sh = p[3]:sub(2) - if parse_reg_type == "w" then - p[3] = "#(32-("..sh.."))%32" - p[4] = "#31-("..sh..")" - else - p[3] = "#(64-("..sh.."))%64" - p[4] = "#63-("..sh..")" - end -end) - --- Template strings for ARM instructions. -map_op = { - -- Basic data processing instructions. - add_3 = "0b000000DNMg|11000000pDpNIg|8b206000pDpNMx", - add_4 = "0b000000DNMSg|0b200000DNMXg|8b200000pDpNMXx|8b200000pDpNxMwX", - adds_3 = "2b000000DNMg|31000000DpNIg|ab206000DpNMx", - adds_4 = "2b000000DNMSg|2b200000DNMXg|ab200000DpNMXx|ab200000DpNxMwX", - cmn_2 = "2b00001fNMg|3100001fpNIg|ab20601fpNMx", - cmn_3 = "2b00001fNMSg|2b20001fNMXg|ab20001fpNMXx|ab20001fpNxMwX", - - sub_3 = "4b000000DNMg|51000000pDpNIg|cb206000pDpNMx", - sub_4 = "4b000000DNMSg|4b200000DNMXg|cb200000pDpNMXx|cb200000pDpNxMwX", - subs_3 = "6b000000DNMg|71000000DpNIg|eb206000DpNMx", - subs_4 = "6b000000DNMSg|6b200000DNMXg|eb200000DpNMXx|eb200000DpNxMwX", - cmp_2 = "6b00001fNMg|7100001fpNIg|eb20601fpNMx", - cmp_3 = "6b00001fNMSg|6b20001fNMXg|eb20001fpNMXx|eb20001fpNxMwX", - - neg_2 = "4b0003e0DMg", - neg_3 = "4b0003e0DMSg", - negs_2 = "6b0003e0DMg", - negs_3 = "6b0003e0DMSg", - - adc_3 = "1a000000DNMg", - adcs_3 = "3a000000DNMg", - sbc_3 = "5a000000DNMg", - sbcs_3 = "7a000000DNMg", - ngc_2 = "5a0003e0DMg", - ngcs_2 = "7a0003e0DMg", - - and_3 = "0a000000DNMg|12000000pDNig", - and_4 = "0a000000DNMSg", - orr_3 = "2a000000DNMg|32000000pDNig", - orr_4 = "2a000000DNMSg", - eor_3 = "4a000000DNMg|52000000pDNig", - eor_4 = "4a000000DNMSg", - ands_3 = "6a000000DNMg|72000000DNig", - ands_4 = "6a000000DNMSg", - tst_2 = "6a00001fNMg|7200001fNig", - tst_3 = "6a00001fNMSg", - - bic_3 = "0a200000DNMg", - bic_4 = "0a200000DNMSg", - orn_3 = "2a200000DNMg", - orn_4 = "2a200000DNMSg", - eon_3 = "4a200000DNMg", - eon_4 = "4a200000DNMSg", - bics_3 = "6a200000DNMg", - bics_4 = "6a200000DNMSg", - - movn_2 = "12800000DWg", - movn_3 = "12800000DWRg", - movz_2 = "52800000DWg", - movz_3 = "52800000DWRg", - movk_2 = "72800000DWg", - movk_3 = "72800000DWRg", - - -- TODO: this doesn't cover all valid immediates for mov reg, #imm. - mov_2 = "2a0003e0DMg|52800000DW|320003e0pDig|11000000pDpNg", - mov_3 = "2a0003e0DMSg", - mvn_2 = "2a2003e0DMg", - mvn_3 = "2a2003e0DMSg", - - adr_2 = "10000000DBx", - adrp_2 = "90000000DBx", - - csel_4 = "1a800000DNMCg", - csinc_4 = "1a800400DNMCg", - csinv_4 = "5a800000DNMCg", - csneg_4 = "5a800400DNMCg", - cset_2 = "1a9f07e0Dcg", - csetm_2 = "5a9f03e0Dcg", - cinc_3 = "1a800400DNmcg", - cinv_3 = "5a800000DNmcg", - cneg_3 = "5a800400DNmcg", - - ccmn_4 = "3a400000NMVCg|3a400800N5VCg", - ccmp_4 = "7a400000NMVCg|7a400800N5VCg", - - madd_4 = "1b000000DNMAg", - msub_4 = "1b008000DNMAg", - mul_3 = "1b007c00DNMg", - mneg_3 = "1b00fc00DNMg", - - smaddl_4 = "9b200000DxNMwAx", - smsubl_4 = "9b208000DxNMwAx", - smull_3 = "9b207c00DxNMw", - smnegl_3 = "9b20fc00DxNMw", - smulh_3 = "9b407c00DNMx", - umaddl_4 = "9ba00000DxNMwAx", - umsubl_4 = "9ba08000DxNMwAx", - umull_3 = "9ba07c00DxNMw", - umnegl_3 = "9ba0fc00DxNMw", - umulh_3 = "9bc07c00DNMx", - - udiv_3 = "1ac00800DNMg", - sdiv_3 = "1ac00c00DNMg", - - -- Bit operations. - sbfm_4 = "13000000DN12w|93400000DN12x", - bfm_4 = "33000000DN12w|b3400000DN12x", - ubfm_4 = "53000000DN12w|d3400000DN12x", - extr_4 = "13800000DNM2w|93c00000DNM2x", - - sxtb_2 = "13001c00DNw|93401c00DNx", - sxth_2 = "13003c00DNw|93403c00DNx", - sxtw_2 = "93407c00DxNw", - uxtb_2 = "53001c00DNw", - uxth_2 = "53003c00DNw", - - sbfx_4 = op_alias("sbfm_4", alias_bfx), - bfxil_4 = op_alias("bfm_4", alias_bfx), - ubfx_4 = op_alias("ubfm_4", alias_bfx), - sbfiz_4 = op_alias("sbfm_4", alias_bfiz), - bfi_4 = op_alias("bfm_4", alias_bfiz), - ubfiz_4 = op_alias("ubfm_4", alias_bfiz), - - lsl_3 = function(params, nparams) - if params and params[3]:byte() == 35 then - return alias_lslimm(params, nparams) - else - return op_template(params, "1ac02000DNMg", nparams) - end - end, - lsr_3 = "1ac02400DNMg|53007c00DN1w|d340fc00DN1x", - asr_3 = "1ac02800DNMg|13007c00DN1w|9340fc00DN1x", - ror_3 = "1ac02c00DNMg|13800000DNm2w|93c00000DNm2x", - - clz_2 = "5ac01000DNg", - cls_2 = "5ac01400DNg", - rbit_2 = "5ac00000DNg", - rev_2 = "5ac00800DNw|dac00c00DNx", - rev16_2 = "5ac00400DNg", - rev32_2 = "dac00800DNx", - - -- Loads and stores. - ["strb_*"] = "38000000DwL", - ["ldrb_*"] = "38400000DwL", - ["ldrsb_*"] = "38c00000DwL|38800000DxL", - ["strh_*"] = "78000000DwL", - ["ldrh_*"] = "78400000DwL", - ["ldrsh_*"] = "78c00000DwL|78800000DxL", - ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL", - ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL", - ["ldrsw_*"] = "98000000DxB|b8800000DxL", - -- NOTE: ldur etc. are handled by ldr et al. - - ["stp_*"] = "28000000DAwP|a8000000DAxP|2c000000DAsP|6c000000DAdP", - ["ldp_*"] = "28400000DAwP|a8400000DAxP|2c400000DAsP|6c400000DAdP", - ["ldpsw_*"] = "68400000DAxP", - - -- Branches. - b_1 = "14000000B", - bl_1 = "94000000B", - blr_1 = "d63f0000Nx", - br_1 = "d61f0000Nx", - ret_0 = "d65f03c0", - ret_1 = "d65f0000Nx", - -- b.cond is added below. - cbz_2 = "34000000DBg", - cbnz_2 = "35000000DBg", - tbz_3 = "36000000DTBw|36000000DTBx", - tbnz_3 = "37000000DTBw|37000000DTBx", - - -- Miscellaneous instructions. - -- TODO: hlt, hvc, smc, svc, eret, dcps[123], drps, mrs, msr - -- TODO: sys, sysl, ic, dc, at, tlbi - -- TODO: hint, yield, wfe, wfi, sev, sevl - -- TODO: clrex, dsb, dmb, isb - nop_0 = "d503201f", - brk_0 = "d4200000", - brk_1 = "d4200000W", - - -- Floating point instructions. - fmov_2 = "1e204000DNf|1e260000DwNs|1e270000DsNw|9e660000DxNd|9e670000DdNx|1e201000DFf", - fabs_2 = "1e20c000DNf", - fneg_2 = "1e214000DNf", - fsqrt_2 = "1e21c000DNf", - - fcvt_2 = "1e22c000DdNs|1e624000DsNd", - - -- TODO: half-precision and fixed-point conversions. - fcvtas_2 = "1e240000DwNs|9e240000DxNs|1e640000DwNd|9e640000DxNd", - fcvtau_2 = "1e250000DwNs|9e250000DxNs|1e650000DwNd|9e650000DxNd", - fcvtms_2 = "1e300000DwNs|9e300000DxNs|1e700000DwNd|9e700000DxNd", - fcvtmu_2 = "1e310000DwNs|9e310000DxNs|1e710000DwNd|9e710000DxNd", - fcvtns_2 = "1e200000DwNs|9e200000DxNs|1e600000DwNd|9e600000DxNd", - fcvtnu_2 = "1e210000DwNs|9e210000DxNs|1e610000DwNd|9e610000DxNd", - fcvtps_2 = "1e280000DwNs|9e280000DxNs|1e680000DwNd|9e680000DxNd", - fcvtpu_2 = "1e290000DwNs|9e290000DxNs|1e690000DwNd|9e690000DxNd", - fcvtzs_2 = "1e380000DwNs|9e380000DxNs|1e780000DwNd|9e780000DxNd", - fcvtzu_2 = "1e390000DwNs|9e390000DxNs|1e790000DwNd|9e790000DxNd", - - scvtf_2 = "1e220000DsNw|9e220000DsNx|1e620000DdNw|9e620000DdNx", - ucvtf_2 = "1e230000DsNw|9e230000DsNx|1e630000DdNw|9e630000DdNx", - - frintn_2 = "1e244000DNf", - frintp_2 = "1e24c000DNf", - frintm_2 = "1e254000DNf", - frintz_2 = "1e25c000DNf", - frinta_2 = "1e264000DNf", - frintx_2 = "1e274000DNf", - frinti_2 = "1e27c000DNf", - - fadd_3 = "1e202800DNMf", - fsub_3 = "1e203800DNMf", - fmul_3 = "1e200800DNMf", - fnmul_3 = "1e208800DNMf", - fdiv_3 = "1e201800DNMf", - - fmadd_4 = "1f000000DNMAf", - fmsub_4 = "1f008000DNMAf", - fnmadd_4 = "1f200000DNMAf", - fnmsub_4 = "1f208000DNMAf", - - fmax_3 = "1e204800DNMf", - fmaxnm_3 = "1e206800DNMf", - fmin_3 = "1e205800DNMf", - fminnm_3 = "1e207800DNMf", - - fcmp_2 = "1e202000NMf|1e202008NZf", - fcmpe_2 = "1e202010NMf|1e202018NZf", - - fccmp_4 = "1e200400NMVCf", - fccmpe_4 = "1e200410NMVCf", - - fcsel_4 = "1e200c00DNMCf", - - -- TODO: crc32*, aes*, sha*, pmull - -- TODO: SIMD instructions. -} - -for cond,c in pairs(map_cond) do - map_op["b"..cond.."_1"] = tohex(0x54000000+c).."B" -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -local function parse_template(params, template, nparams, pos) - local op = tonumber(template:sub(1, 8), 16) - local n = 1 - local rtt = {} - - parse_reg_type = false - - -- Process each character. - for p in gmatch(template:sub(9), ".") do - local q = params[n] - if p == "D" then - op = op + parse_reg(q, 0); n = n + 1 - elseif p == "N" then - op = op + parse_reg(q, 5); n = n + 1 - elseif p == "M" then - op = op + parse_reg(q, 16); n = n + 1 - elseif p == "A" then - op = op + parse_reg(q, 10); n = n + 1 - elseif p == "m" then - op = op + parse_reg(params[n-1], 16) - - elseif p == "p" then - if q == "sp" then params[n] = "@x31" end - elseif p == "g" then - if parse_reg_type == "x" then - op = op + 0x80000000 - elseif parse_reg_type ~= "w" then - werror("bad register type") - end - parse_reg_type = false - elseif p == "f" then - if parse_reg_type == "d" then - op = op + 0x00400000 - elseif parse_reg_type ~= "s" then - werror("bad register type") - end - parse_reg_type = false - elseif p == "x" or p == "w" or p == "d" or p == "s" then - if parse_reg_type ~= p then - werror("register size mismatch") - end - parse_reg_type = false - - elseif p == "L" then - op = parse_load(params, nparams, n, op) - elseif p == "P" then - op = parse_load_pair(params, nparams, n, op) - - elseif p == "B" then - local mode, v, s = parse_label(q, false); n = n + 1 - if not mode then werror("bad label `"..q.."'") end - local m = branch_type(op) - if mode == "A" then - waction("REL_"..mode, v+m, format("(unsigned int)(%s)", s)) - actargs[#actargs+1] = format("(unsigned int)((%s)>>32)", s) - else - waction("REL_"..mode, v+m, s, 1) - end - - elseif p == "I" then - op = op + parse_imm12(q); n = n + 1 - elseif p == "i" then - op = op + parse_imm13(q); n = n + 1 - elseif p == "W" then - op = op + parse_imm(q, 16, 5, 0, false); n = n + 1 - elseif p == "T" then - op = op + parse_imm6(q); n = n + 1 - elseif p == "1" then - op = op + parse_imm(q, 6, 16, 0, false); n = n + 1 - elseif p == "2" then - op = op + parse_imm(q, 6, 10, 0, false); n = n + 1 - elseif p == "5" then - op = op + parse_imm(q, 5, 16, 0, false); n = n + 1 - elseif p == "V" then - op = op + parse_imm(q, 4, 0, 0, false); n = n + 1 - elseif p == "F" then - op = op + parse_fpimm(q); n = n + 1 - elseif p == "Z" then - if q ~= "#0" and q ~= "#0.0" then werror("expected zero immediate") end - n = n + 1 - - elseif p == "S" then - op = op + parse_shift(q); n = n + 1 - elseif p == "X" then - op = op + parse_extend(q); n = n + 1 - elseif p == "R" then - op = op + parse_lslx16(q); n = n + 1 - elseif p == "C" then - op = op + parse_cond(q, 0); n = n + 1 - elseif p == "c" then - op = op + parse_cond(q, 1); n = n + 1 - - else - assert(false) - end - end - wputpos(pos, op) -end - -function op_template(params, template, nparams) - if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 4 positions. - if secpos+4 > maxsecpos then wflush() end - local pos = wpos() - local lpos, apos, spos = #actlist, #actargs, secpos - - local ok, err - for t in gmatch(template, "[^|]+") do - ok, err = pcall(parse_template, params, t, nparams, pos) - if ok then return end - secpos = spos - actlist[lpos+1] = nil - actlist[lpos+2] = nil - actlist[lpos+3] = nil - actlist[lpos+4] = nil - actargs[apos+1] = nil - actargs[apos+2] = nil - actargs[apos+3] = nil - actargs[apos+4] = nil - end - error(err, 0) -end - -map_op[".template__"] = op_template - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if not mode or mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -local function op_data(params) - if not params then return "imm..." end - local sz = params.op == ".long" and 4 or 8 - for _,p in ipairs(params) do - local imm = parse_number(p) - if imm then - local n = tobit(imm) - if n == imm or (n < 0 and n + 2^32 == imm) then - wputw(n < 0 and n + 2^32 or n) - if sz == 8 then - wputw(imm < 0 and 0xffffffff or 0) - end - elseif sz == 4 then - werror("bad immediate `"..p.."'") - else - imm = nil - end - end - if not imm then - local mode, v, s = parse_label(p, false) - if sz == 4 then - if mode then werror("label does not fit into .long") end - waction("IMMV", 0, p) - elseif mode and mode ~= "A" then - waction("REL_"..mode, v+0x8000, s, 1) - else - if mode == "A" then p = s end - waction("IMMV", 0, format("(unsigned int)(%s)", p)) - waction("IMMV", 0, format("(unsigned int)((unsigned long long)(%s)>>32)", p)) - end - end - if secpos+2 > maxsecpos then wflush() end - end -end -map_op[".long_*"] = op_data -map_op[".quad_*"] = op_data -map_op[".addr_*"] = op_data - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.h deleted file mode 100644 index 3e99a00..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.h +++ /dev/null @@ -1,424 +0,0 @@ -/* -** DynASM MIPS encoding engine. -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "mips" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, DASM_IMMS, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: case DASM_IMMS: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMMS: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(int)(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - if (n < 0) { - n = (int)((ptrdiff_t)D->globals[-n] - (ptrdiff_t)cp); - goto patchrel; - } - /* fallthrough */ - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n); - if (ins & 2048) - n = (n + (int)(size_t)base) & 0x0fffffff; - else - n = n - (int)((char *)cp - base); - patchrel: { - unsigned int e = 16 + ((ins >> 12) & 15); - CK((n & 3) == 0 && - ((n + ((ins & 2048) ? 0 : (1<<(e+1)))) >> (e+2)) == 0, RANGE_REL); - cp[-1] |= ((n>>2) & ((1<= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMMS: - cp[-1] |= ((n>>3) & 4); n &= 0x1f; - /* fallthrough */ - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); - return D->status; -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.lua deleted file mode 100644 index 6f893fe..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips.lua +++ /dev/null @@ -1,1181 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM MIPS32/MIPS64 module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - -local mips64 = mips64 -local mipsr6 = _map_def.MIPSR6 - --- Module information: -local _info = { - arch = mips64 and "mips64" or "mips", - description = "DynASM MIPS32/MIPS64 module", - version = "1.5.0", - vernum = 10500, - release = "2021-05-02", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort -local bit = bit or require("bit") -local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift -local tohex = bit.tohex - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", "IMMS", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(0xff000000 + w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n >= 0xff000000 then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp="r29", ra="r31" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r29" then return "sp" - elseif s == "r31" then return "ra" end - return s -end - ------------------------------------------------------------------------------- - --- Template strings for MIPS instructions. -local map_op = { - -- First-level opcodes. - j_1 = "08000000J", - jal_1 = "0c000000J", - b_1 = "10000000B", - beqz_2 = "10000000SB", - beq_3 = "10000000STB", - bnez_2 = "14000000SB", - bne_3 = "14000000STB", - blez_2 = "18000000SB", - bgtz_2 = "1c000000SB", - li_2 = "24000000TI", - addiu_3 = "24000000TSI", - slti_3 = "28000000TSI", - sltiu_3 = "2c000000TSI", - andi_3 = "30000000TSU", - lu_2 = "34000000TU", - ori_3 = "34000000TSU", - xori_3 = "38000000TSU", - lui_2 = "3c000000TU", - daddiu_3 = mips64 and "64000000TSI", - ldl_2 = mips64 and "68000000TO", - ldr_2 = mips64 and "6c000000TO", - lb_2 = "80000000TO", - lh_2 = "84000000TO", - lw_2 = "8c000000TO", - lbu_2 = "90000000TO", - lhu_2 = "94000000TO", - lwu_2 = mips64 and "9c000000TO", - sb_2 = "a0000000TO", - sh_2 = "a4000000TO", - sw_2 = "ac000000TO", - lwc1_2 = "c4000000HO", - ldc1_2 = "d4000000HO", - ld_2 = mips64 and "dc000000TO", - swc1_2 = "e4000000HO", - sdc1_2 = "f4000000HO", - sd_2 = mips64 and "fc000000TO", - - -- Opcode SPECIAL. - nop_0 = "00000000", - sll_3 = "00000000DTA", - sextw_2 = "00000000DT", - srl_3 = "00000002DTA", - rotr_3 = "00200002DTA", - sra_3 = "00000003DTA", - sllv_3 = "00000004DTS", - srlv_3 = "00000006DTS", - rotrv_3 = "00000046DTS", - drotrv_3 = mips64 and "00000056DTS", - srav_3 = "00000007DTS", - jalr_1 = "0000f809S", - jalr_2 = "00000009DS", - syscall_0 = "0000000c", - syscall_1 = "0000000cY", - break_0 = "0000000d", - break_1 = "0000000dY", - sync_0 = "0000000f", - dsllv_3 = mips64 and "00000014DTS", - dsrlv_3 = mips64 and "00000016DTS", - dsrav_3 = mips64 and "00000017DTS", - add_3 = "00000020DST", - move_2 = mips64 and "00000025DS" or "00000021DS", - addu_3 = "00000021DST", - sub_3 = "00000022DST", - negu_2 = mips64 and "0000002fDT" or "00000023DT", - subu_3 = "00000023DST", - and_3 = "00000024DST", - or_3 = "00000025DST", - xor_3 = "00000026DST", - not_2 = "00000027DS", - nor_3 = "00000027DST", - slt_3 = "0000002aDST", - sltu_3 = "0000002bDST", - dadd_3 = mips64 and "0000002cDST", - daddu_3 = mips64 and "0000002dDST", - dsub_3 = mips64 and "0000002eDST", - dsubu_3 = mips64 and "0000002fDST", - tge_2 = "00000030ST", - tge_3 = "00000030STZ", - tgeu_2 = "00000031ST", - tgeu_3 = "00000031STZ", - tlt_2 = "00000032ST", - tlt_3 = "00000032STZ", - tltu_2 = "00000033ST", - tltu_3 = "00000033STZ", - teq_2 = "00000034ST", - teq_3 = "00000034STZ", - tne_2 = "00000036ST", - tne_3 = "00000036STZ", - dsll_3 = mips64 and "00000038DTa", - dsrl_3 = mips64 and "0000003aDTa", - drotr_3 = mips64 and "0020003aDTa", - dsra_3 = mips64 and "0000003bDTa", - dsll32_3 = mips64 and "0000003cDTA", - dsrl32_3 = mips64 and "0000003eDTA", - drotr32_3 = mips64 and "0020003eDTA", - dsra32_3 = mips64 and "0000003fDTA", - - -- Opcode REGIMM. - bltz_2 = "04000000SB", - bgez_2 = "04010000SB", - bltzl_2 = "04020000SB", - bgezl_2 = "04030000SB", - bal_1 = "04110000B", - synci_1 = "041f0000O", - - -- Opcode SPECIAL3. - ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1 - dextm_4 = mips64 and "7c000001TSAM", -- Args: pos | size-1-32 - dextu_4 = mips64 and "7c000002TSAM", -- Args: pos-32 | size-1 - dext_4 = mips64 and "7c000003TSAM", -- Args: pos | size-1 - zextw_2 = mips64 and "7c00f803TS", - ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1 - dinsm_4 = mips64 and "7c000005TSAM", -- Args: pos | pos+size-33 - dinsu_4 = mips64 and "7c000006TSAM", -- Args: pos-32 | pos+size-33 - dins_4 = mips64 and "7c000007TSAM", -- Args: pos | pos+size-1 - wsbh_2 = "7c0000a0DT", - dsbh_2 = mips64 and "7c0000a4DT", - dshd_2 = mips64 and "7c000164DT", - seb_2 = "7c000420DT", - seh_2 = "7c000620DT", - rdhwr_2 = "7c00003bTD", - - -- Opcode COP0. - mfc0_2 = "40000000TD", - mfc0_3 = "40000000TDW", - dmfc0_2 = mips64 and "40200000TD", - dmfc0_3 = mips64 and "40200000TDW", - mtc0_2 = "40800000TD", - mtc0_3 = "40800000TDW", - dmtc0_2 = mips64 and "40a00000TD", - dmtc0_3 = mips64 and "40a00000TDW", - rdpgpr_2 = "41400000DT", - di_0 = "41606000", - di_1 = "41606000T", - ei_0 = "41606020", - ei_1 = "41606020T", - wrpgpr_2 = "41c00000DT", - tlbr_0 = "42000001", - tlbwi_0 = "42000002", - tlbwr_0 = "42000006", - tlbp_0 = "42000008", - eret_0 = "42000018", - deret_0 = "4200001f", - wait_0 = "42000020", - - -- Opcode COP1. - mfc1_2 = "44000000TG", - dmfc1_2 = mips64 and "44200000TG", - cfc1_2 = "44400000TG", - mfhc1_2 = "44600000TG", - mtc1_2 = "44800000TG", - dmtc1_2 = mips64 and "44a00000TG", - ctc1_2 = "44c00000TG", - mthc1_2 = "44e00000TG", - - ["add.s_3"] = "46000000FGH", - ["sub.s_3"] = "46000001FGH", - ["mul.s_3"] = "46000002FGH", - ["div.s_3"] = "46000003FGH", - ["sqrt.s_2"] = "46000004FG", - ["abs.s_2"] = "46000005FG", - ["mov.s_2"] = "46000006FG", - ["neg.s_2"] = "46000007FG", - ["round.l.s_2"] = "46000008FG", - ["trunc.l.s_2"] = "46000009FG", - ["ceil.l.s_2"] = "4600000aFG", - ["floor.l.s_2"] = "4600000bFG", - ["round.w.s_2"] = "4600000cFG", - ["trunc.w.s_2"] = "4600000dFG", - ["ceil.w.s_2"] = "4600000eFG", - ["floor.w.s_2"] = "4600000fFG", - ["recip.s_2"] = "46000015FG", - ["rsqrt.s_2"] = "46000016FG", - ["cvt.d.s_2"] = "46000021FG", - ["cvt.w.s_2"] = "46000024FG", - ["cvt.l.s_2"] = "46000025FG", - ["add.d_3"] = "46200000FGH", - ["sub.d_3"] = "46200001FGH", - ["mul.d_3"] = "46200002FGH", - ["div.d_3"] = "46200003FGH", - ["sqrt.d_2"] = "46200004FG", - ["abs.d_2"] = "46200005FG", - ["mov.d_2"] = "46200006FG", - ["neg.d_2"] = "46200007FG", - ["round.l.d_2"] = "46200008FG", - ["trunc.l.d_2"] = "46200009FG", - ["ceil.l.d_2"] = "4620000aFG", - ["floor.l.d_2"] = "4620000bFG", - ["round.w.d_2"] = "4620000cFG", - ["trunc.w.d_2"] = "4620000dFG", - ["ceil.w.d_2"] = "4620000eFG", - ["floor.w.d_2"] = "4620000fFG", - ["recip.d_2"] = "46200015FG", - ["rsqrt.d_2"] = "46200016FG", - ["cvt.s.d_2"] = "46200020FG", - ["cvt.w.d_2"] = "46200024FG", - ["cvt.l.d_2"] = "46200025FG", - ["cvt.s.w_2"] = "46800020FG", - ["cvt.d.w_2"] = "46800021FG", - ["cvt.s.l_2"] = "46a00020FG", - ["cvt.d.l_2"] = "46a00021FG", -} - -if mipsr6 then -- Instructions added with MIPSR6. - - for k,v in pairs({ - - -- Add immediate to upper bits. - aui_3 = "3c000000TSI", - daui_3 = mips64 and "74000000TSI", - dahi_2 = mips64 and "04060000SI", - dati_2 = mips64 and "041e0000SI", - - -- TODO: addiupc, auipc, aluipc, lwpc, lwupc, ldpc. - - -- Compact branches. - blezalc_2 = "18000000TB", -- rt != 0. - bgezalc_2 = "18000000T=SB", -- rt != 0. - bgtzalc_2 = "1c000000TB", -- rt != 0. - bltzalc_2 = "1c000000T=SB", -- rt != 0. - - blezc_2 = "58000000TB", -- rt != 0. - bgezc_2 = "58000000T=SB", -- rt != 0. - bgec_3 = "58000000STB", -- rs != rt. - blec_3 = "58000000TSB", -- rt != rs. - - bgtzc_2 = "5c000000TB", -- rt != 0. - bltzc_2 = "5c000000T=SB", -- rt != 0. - bltc_3 = "5c000000STB", -- rs != rt. - bgtc_3 = "5c000000TSB", -- rt != rs. - - bgeuc_3 = "18000000STB", -- rs != rt. - bleuc_3 = "18000000TSB", -- rt != rs. - bltuc_3 = "1c000000STB", -- rs != rt. - bgtuc_3 = "1c000000TSB", -- rt != rs. - - beqzalc_2 = "20000000TB", -- rt != 0. - bnezalc_2 = "60000000TB", -- rt != 0. - beqc_3 = "20000000STB", -- rs < rt. - bnec_3 = "60000000STB", -- rs < rt. - bovc_3 = "20000000STB", -- rs >= rt. - bnvc_3 = "60000000STB", -- rs >= rt. - - beqzc_2 = "d8000000SK", -- rs != 0. - bnezc_2 = "f8000000SK", -- rs != 0. - jic_2 = "d8000000TI", - jialc_2 = "f8000000TI", - bc_1 = "c8000000L", - balc_1 = "e8000000L", - - -- Opcode SPECIAL. - jr_1 = "00000009S", - sdbbp_0 = "0000000e", - sdbbp_1 = "0000000eY", - lsa_4 = "00000005DSTA", - dlsa_4 = mips64 and "00000015DSTA", - seleqz_3 = "00000035DST", - selnez_3 = "00000037DST", - clz_2 = "00000050DS", - clo_2 = "00000051DS", - dclz_2 = mips64 and "00000052DS", - dclo_2 = mips64 and "00000053DS", - mul_3 = "00000098DST", - muh_3 = "000000d8DST", - mulu_3 = "00000099DST", - muhu_3 = "000000d9DST", - div_3 = "0000009aDST", - mod_3 = "000000daDST", - divu_3 = "0000009bDST", - modu_3 = "000000dbDST", - dmul_3 = mips64 and "0000009cDST", - dmuh_3 = mips64 and "000000dcDST", - dmulu_3 = mips64 and "0000009dDST", - dmuhu_3 = mips64 and "000000ddDST", - ddiv_3 = mips64 and "0000009eDST", - dmod_3 = mips64 and "000000deDST", - ddivu_3 = mips64 and "0000009fDST", - dmodu_3 = mips64 and "000000dfDST", - - -- Opcode SPECIAL3. - align_4 = "7c000220DSTA", - dalign_4 = mips64 and "7c000224DSTA", - bitswap_2 = "7c000020DT", - dbitswap_2 = mips64 and "7c000024DT", - - -- Opcode COP1. - bc1eqz_2 = "45200000HB", - bc1nez_2 = "45a00000HB", - - ["sel.s_3"] = "46000010FGH", - ["seleqz.s_3"] = "46000014FGH", - ["selnez.s_3"] = "46000017FGH", - ["maddf.s_3"] = "46000018FGH", - ["msubf.s_3"] = "46000019FGH", - ["rint.s_2"] = "4600001aFG", - ["class.s_2"] = "4600001bFG", - ["min.s_3"] = "4600001cFGH", - ["mina.s_3"] = "4600001dFGH", - ["max.s_3"] = "4600001eFGH", - ["maxa.s_3"] = "4600001fFGH", - ["cmp.af.s_3"] = "46800000FGH", - ["cmp.un.s_3"] = "46800001FGH", - ["cmp.or.s_3"] = "46800011FGH", - ["cmp.eq.s_3"] = "46800002FGH", - ["cmp.une.s_3"] = "46800012FGH", - ["cmp.ueq.s_3"] = "46800003FGH", - ["cmp.ne.s_3"] = "46800013FGH", - ["cmp.lt.s_3"] = "46800004FGH", - ["cmp.ult.s_3"] = "46800005FGH", - ["cmp.le.s_3"] = "46800006FGH", - ["cmp.ule.s_3"] = "46800007FGH", - ["cmp.saf.s_3"] = "46800008FGH", - ["cmp.sun.s_3"] = "46800009FGH", - ["cmp.sor.s_3"] = "46800019FGH", - ["cmp.seq.s_3"] = "4680000aFGH", - ["cmp.sune.s_3"] = "4680001aFGH", - ["cmp.sueq.s_3"] = "4680000bFGH", - ["cmp.sne.s_3"] = "4680001bFGH", - ["cmp.slt.s_3"] = "4680000cFGH", - ["cmp.sult.s_3"] = "4680000dFGH", - ["cmp.sle.s_3"] = "4680000eFGH", - ["cmp.sule.s_3"] = "4680000fFGH", - - ["sel.d_3"] = "46200010FGH", - ["seleqz.d_3"] = "46200014FGH", - ["selnez.d_3"] = "46200017FGH", - ["maddf.d_3"] = "46200018FGH", - ["msubf.d_3"] = "46200019FGH", - ["rint.d_2"] = "4620001aFG", - ["class.d_2"] = "4620001bFG", - ["min.d_3"] = "4620001cFGH", - ["mina.d_3"] = "4620001dFGH", - ["max.d_3"] = "4620001eFGH", - ["maxa.d_3"] = "4620001fFGH", - ["cmp.af.d_3"] = "46a00000FGH", - ["cmp.un.d_3"] = "46a00001FGH", - ["cmp.or.d_3"] = "46a00011FGH", - ["cmp.eq.d_3"] = "46a00002FGH", - ["cmp.une.d_3"] = "46a00012FGH", - ["cmp.ueq.d_3"] = "46a00003FGH", - ["cmp.ne.d_3"] = "46a00013FGH", - ["cmp.lt.d_3"] = "46a00004FGH", - ["cmp.ult.d_3"] = "46a00005FGH", - ["cmp.le.d_3"] = "46a00006FGH", - ["cmp.ule.d_3"] = "46a00007FGH", - ["cmp.saf.d_3"] = "46a00008FGH", - ["cmp.sun.d_3"] = "46a00009FGH", - ["cmp.sor.d_3"] = "46a00019FGH", - ["cmp.seq.d_3"] = "46a0000aFGH", - ["cmp.sune.d_3"] = "46a0001aFGH", - ["cmp.sueq.d_3"] = "46a0000bFGH", - ["cmp.sne.d_3"] = "46a0001bFGH", - ["cmp.slt.d_3"] = "46a0000cFGH", - ["cmp.sult.d_3"] = "46a0000dFGH", - ["cmp.sle.d_3"] = "46a0000eFGH", - ["cmp.sule.d_3"] = "46a0000fFGH", - - }) do map_op[k] = v end - -else -- Instructions removed by MIPSR6. - - for k,v in pairs({ - -- Traps, don't use. - addi_3 = "20000000TSI", - daddi_3 = mips64 and "60000000TSI", - - -- Branch on likely, don't use. - beqzl_2 = "50000000SB", - beql_3 = "50000000STB", - bnezl_2 = "54000000SB", - bnel_3 = "54000000STB", - blezl_2 = "58000000SB", - bgtzl_2 = "5c000000SB", - - lwl_2 = "88000000TO", - lwr_2 = "98000000TO", - swl_2 = "a8000000TO", - sdl_2 = mips64 and "b0000000TO", - sdr_2 = mips64 and "b1000000TO", - swr_2 = "b8000000TO", - cache_2 = "bc000000NO", - ll_2 = "c0000000TO", - pref_2 = "cc000000NO", - sc_2 = "e0000000TO", - scd_2 = mips64 and "f0000000TO", - - -- Opcode SPECIAL. - movf_2 = "00000001DS", - movf_3 = "00000001DSC", - movt_2 = "00010001DS", - movt_3 = "00010001DSC", - jr_1 = "00000008S", - movz_3 = "0000000aDST", - movn_3 = "0000000bDST", - mfhi_1 = "00000010D", - mthi_1 = "00000011S", - mflo_1 = "00000012D", - mtlo_1 = "00000013S", - mult_2 = "00000018ST", - multu_2 = "00000019ST", - div_3 = "0000001aST", - divu_3 = "0000001bST", - ddiv_3 = mips64 and "0000001eST", - ddivu_3 = mips64 and "0000001fST", - dmult_2 = mips64 and "0000001cST", - dmultu_2 = mips64 and "0000001dST", - - -- Opcode REGIMM. - tgei_2 = "04080000SI", - tgeiu_2 = "04090000SI", - tlti_2 = "040a0000SI", - tltiu_2 = "040b0000SI", - teqi_2 = "040c0000SI", - tnei_2 = "040e0000SI", - bltzal_2 = "04100000SB", - bgezal_2 = "04110000SB", - bltzall_2 = "04120000SB", - bgezall_2 = "04130000SB", - - -- Opcode SPECIAL2. - madd_2 = "70000000ST", - maddu_2 = "70000001ST", - mul_3 = "70000002DST", - msub_2 = "70000004ST", - msubu_2 = "70000005ST", - clz_2 = "70000020D=TS", - clo_2 = "70000021D=TS", - dclz_2 = mips64 and "70000024D=TS", - dclo_2 = mips64 and "70000025D=TS", - sdbbp_0 = "7000003f", - sdbbp_1 = "7000003fY", - - -- Opcode COP1. - bc1f_1 = "45000000B", - bc1f_2 = "45000000CB", - bc1t_1 = "45010000B", - bc1t_2 = "45010000CB", - bc1fl_1 = "45020000B", - bc1fl_2 = "45020000CB", - bc1tl_1 = "45030000B", - bc1tl_2 = "45030000CB", - - ["movf.s_2"] = "46000011FG", - ["movf.s_3"] = "46000011FGC", - ["movt.s_2"] = "46010011FG", - ["movt.s_3"] = "46010011FGC", - ["movz.s_3"] = "46000012FGT", - ["movn.s_3"] = "46000013FGT", - ["cvt.ps.s_3"] = "46000026FGH", - ["c.f.s_2"] = "46000030GH", - ["c.f.s_3"] = "46000030VGH", - ["c.un.s_2"] = "46000031GH", - ["c.un.s_3"] = "46000031VGH", - ["c.eq.s_2"] = "46000032GH", - ["c.eq.s_3"] = "46000032VGH", - ["c.ueq.s_2"] = "46000033GH", - ["c.ueq.s_3"] = "46000033VGH", - ["c.olt.s_2"] = "46000034GH", - ["c.olt.s_3"] = "46000034VGH", - ["c.ult.s_2"] = "46000035GH", - ["c.ult.s_3"] = "46000035VGH", - ["c.ole.s_2"] = "46000036GH", - ["c.ole.s_3"] = "46000036VGH", - ["c.ule.s_2"] = "46000037GH", - ["c.ule.s_3"] = "46000037VGH", - ["c.sf.s_2"] = "46000038GH", - ["c.sf.s_3"] = "46000038VGH", - ["c.ngle.s_2"] = "46000039GH", - ["c.ngle.s_3"] = "46000039VGH", - ["c.seq.s_2"] = "4600003aGH", - ["c.seq.s_3"] = "4600003aVGH", - ["c.ngl.s_2"] = "4600003bGH", - ["c.ngl.s_3"] = "4600003bVGH", - ["c.lt.s_2"] = "4600003cGH", - ["c.lt.s_3"] = "4600003cVGH", - ["c.nge.s_2"] = "4600003dGH", - ["c.nge.s_3"] = "4600003dVGH", - ["c.le.s_2"] = "4600003eGH", - ["c.le.s_3"] = "4600003eVGH", - ["c.ngt.s_2"] = "4600003fGH", - ["c.ngt.s_3"] = "4600003fVGH", - ["movf.d_2"] = "46200011FG", - ["movf.d_3"] = "46200011FGC", - ["movt.d_2"] = "46210011FG", - ["movt.d_3"] = "46210011FGC", - ["movz.d_3"] = "46200012FGT", - ["movn.d_3"] = "46200013FGT", - ["c.f.d_2"] = "46200030GH", - ["c.f.d_3"] = "46200030VGH", - ["c.un.d_2"] = "46200031GH", - ["c.un.d_3"] = "46200031VGH", - ["c.eq.d_2"] = "46200032GH", - ["c.eq.d_3"] = "46200032VGH", - ["c.ueq.d_2"] = "46200033GH", - ["c.ueq.d_3"] = "46200033VGH", - ["c.olt.d_2"] = "46200034GH", - ["c.olt.d_3"] = "46200034VGH", - ["c.ult.d_2"] = "46200035GH", - ["c.ult.d_3"] = "46200035VGH", - ["c.ole.d_2"] = "46200036GH", - ["c.ole.d_3"] = "46200036VGH", - ["c.ule.d_2"] = "46200037GH", - ["c.ule.d_3"] = "46200037VGH", - ["c.sf.d_2"] = "46200038GH", - ["c.sf.d_3"] = "46200038VGH", - ["c.ngle.d_2"] = "46200039GH", - ["c.ngle.d_3"] = "46200039VGH", - ["c.seq.d_2"] = "4620003aGH", - ["c.seq.d_3"] = "4620003aVGH", - ["c.ngl.d_2"] = "4620003bGH", - ["c.ngl.d_3"] = "4620003bVGH", - ["c.lt.d_2"] = "4620003cGH", - ["c.lt.d_3"] = "4620003cVGH", - ["c.nge.d_2"] = "4620003dGH", - ["c.nge.d_3"] = "4620003dVGH", - ["c.le.d_2"] = "4620003eGH", - ["c.le.d_3"] = "4620003eVGH", - ["c.ngt.d_2"] = "4620003fGH", - ["c.ngt.d_3"] = "4620003fVGH", - ["add.ps_3"] = "46c00000FGH", - ["sub.ps_3"] = "46c00001FGH", - ["mul.ps_3"] = "46c00002FGH", - ["abs.ps_2"] = "46c00005FG", - ["mov.ps_2"] = "46c00006FG", - ["neg.ps_2"] = "46c00007FG", - ["movf.ps_2"] = "46c00011FG", - ["movf.ps_3"] = "46c00011FGC", - ["movt.ps_2"] = "46c10011FG", - ["movt.ps_3"] = "46c10011FGC", - ["movz.ps_3"] = "46c00012FGT", - ["movn.ps_3"] = "46c00013FGT", - ["cvt.s.pu_2"] = "46c00020FG", - ["cvt.s.pl_2"] = "46c00028FG", - ["pll.ps_3"] = "46c0002cFGH", - ["plu.ps_3"] = "46c0002dFGH", - ["pul.ps_3"] = "46c0002eFGH", - ["puu.ps_3"] = "46c0002fFGH", - ["c.f.ps_2"] = "46c00030GH", - ["c.f.ps_3"] = "46c00030VGH", - ["c.un.ps_2"] = "46c00031GH", - ["c.un.ps_3"] = "46c00031VGH", - ["c.eq.ps_2"] = "46c00032GH", - ["c.eq.ps_3"] = "46c00032VGH", - ["c.ueq.ps_2"] = "46c00033GH", - ["c.ueq.ps_3"] = "46c00033VGH", - ["c.olt.ps_2"] = "46c00034GH", - ["c.olt.ps_3"] = "46c00034VGH", - ["c.ult.ps_2"] = "46c00035GH", - ["c.ult.ps_3"] = "46c00035VGH", - ["c.ole.ps_2"] = "46c00036GH", - ["c.ole.ps_3"] = "46c00036VGH", - ["c.ule.ps_2"] = "46c00037GH", - ["c.ule.ps_3"] = "46c00037VGH", - ["c.sf.ps_2"] = "46c00038GH", - ["c.sf.ps_3"] = "46c00038VGH", - ["c.ngle.ps_2"] = "46c00039GH", - ["c.ngle.ps_3"] = "46c00039VGH", - ["c.seq.ps_2"] = "46c0003aGH", - ["c.seq.ps_3"] = "46c0003aVGH", - ["c.ngl.ps_2"] = "46c0003bGH", - ["c.ngl.ps_3"] = "46c0003bVGH", - ["c.lt.ps_2"] = "46c0003cGH", - ["c.lt.ps_3"] = "46c0003cVGH", - ["c.nge.ps_2"] = "46c0003dGH", - ["c.nge.ps_3"] = "46c0003dVGH", - ["c.le.ps_2"] = "46c0003eGH", - ["c.le.ps_3"] = "46c0003eVGH", - ["c.ngt.ps_2"] = "46c0003fGH", - ["c.ngt.ps_3"] = "46c0003fVGH", - - -- Opcode COP1X. - lwxc1_2 = "4c000000FX", - ldxc1_2 = "4c000001FX", - luxc1_2 = "4c000005FX", - swxc1_2 = "4c000008FX", - sdxc1_2 = "4c000009FX", - suxc1_2 = "4c00000dFX", - prefx_2 = "4c00000fMX", - ["alnv.ps_4"] = "4c00001eFGHS", - ["madd.s_4"] = "4c000020FRGH", - ["madd.d_4"] = "4c000021FRGH", - ["madd.ps_4"] = "4c000026FRGH", - ["msub.s_4"] = "4c000028FRGH", - ["msub.d_4"] = "4c000029FRGH", - ["msub.ps_4"] = "4c00002eFRGH", - ["nmadd.s_4"] = "4c000030FRGH", - ["nmadd.d_4"] = "4c000031FRGH", - ["nmadd.ps_4"] = "4c000036FRGH", - ["nmsub.s_4"] = "4c000038FRGH", - ["nmsub.d_4"] = "4c000039FRGH", - ["nmsub.ps_4"] = "4c00003eFRGH", - - }) do map_op[k] = v end - -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed, action) - local n = tonumber(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^[rf]([1-3]?[0-9])$") or - match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction(action or "IMM", - (signed and 32768 or 0)+shl(scale, 10)+shl(bits, 5)+shift, imm) - return 0 - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = shl(parse_gpr(reg), 21) - local extname = match(imm, "^extern%s+(%S+)$") - if extname then - waction("REL_EXT", map_extern[extname], nil, 1) - return r - else - return r + parse_imm(imm, 16, 0, 0, true) - end - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return shl(r, 21) - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_index(idx) - local rt, rs = match(idx, "^(.*)%(([%w_:]+)%)$") - if rt then - rt = parse_gpr(rt) - rs = parse_gpr(rs) - return shl(rt, 16) + shl(rs, 21) - end - werror("bad index `"..idx.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 2 positions (ins/ext). - if secpos+2 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "D" then - op = op + shl(parse_gpr(params[n]), 11); n = n + 1 - elseif p == "T" then - op = op + shl(parse_gpr(params[n]), 16); n = n + 1 - elseif p == "S" then - op = op + shl(parse_gpr(params[n]), 21); n = n + 1 - elseif p == "F" then - op = op + shl(parse_fpr(params[n]), 6); n = n + 1 - elseif p == "G" then - op = op + shl(parse_fpr(params[n]), 11); n = n + 1 - elseif p == "H" then - op = op + shl(parse_fpr(params[n]), 16); n = n + 1 - elseif p == "R" then - op = op + shl(parse_fpr(params[n]), 21); n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "O" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "X" then - op = op + parse_index(params[n]); n = n + 1 - elseif p == "B" or p == "J" or p == "K" or p == "L" then - local mode, m, s = parse_label(params[n], false) - if p == "J" then m = m + 0xa800 - elseif p == "K" then m = m + 0x5000 - elseif p == "L" then m = m + 0xa000 end - waction("REL_"..mode, m, s, 1) - n = n + 1 - elseif p == "A" then - op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1 - elseif p == "a" then - local m = parse_imm(params[n], 6, 6, 0, false, "IMMS"); n = n + 1 - op = op + band(m, 0x7c0) + band(shr(m, 9), 4) - elseif p == "M" then - op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1 - elseif p == "N" then - op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 - elseif p == "C" then - op = op + parse_imm(params[n], 3, 18, 0, false); n = n + 1 - elseif p == "V" then - op = op + parse_imm(params[n], 3, 8, 0, false); n = n + 1 - elseif p == "W" then - op = op + parse_imm(params[n], 3, 0, 0, false); n = n + 1 - elseif p == "Y" then - op = op + parse_imm(params[n], 20, 6, 0, false); n = n + 1 - elseif p == "Z" then - op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1 - elseif p == "=" then - n = n - 1 -- Re-use previous parameter for next template char. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips64.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips64.lua deleted file mode 100644 index b4f8707..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_mips64.lua +++ /dev/null @@ -1,12 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM MIPS64 module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- --- This module just sets 64 bit mode for the combined MIPS/MIPS64 module. --- All the interesting stuff is there. ------------------------------------------------------------------------------- - -mips64 = true -- Using a global is an ugly, but effective solution. -return require("dasm_mips") diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.h deleted file mode 100644 index fdb89bc..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.h +++ /dev/null @@ -1,423 +0,0 @@ -/* -** DynASM PPC/PPC64 encoding engine. -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "ppc" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, DASM_IMMSH, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - case DASM_IMMSH: - CK((n >> 6) == 0, RANGE_I); - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMMSH: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1) - 4; - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - if (n < 0) { - n = (int)((ptrdiff_t)D->globals[-n] - (ptrdiff_t)cp); - goto patchrel; - } - /* fallthrough */ - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base); - patchrel: - CK((n & 3) == 0 && - (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >> - ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL); - cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - case DASM_IMMSH: - cp[-1] |= (ins & 1) ? ((n&31)<<11)|((n&32)>>4) : ((n&31)<<6)|(n&32); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.lua deleted file mode 100644 index 3624e88..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_ppc.lua +++ /dev/null @@ -1,1919 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM PPC/PPC64 module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. --- --- Support for various extensions contributed by Caio Souza Oliveira. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "ppc", - description = "DynASM PPC module", - version = "1.5.0", - vernum = 10500, - release = "2021-05-02", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort -local bit = bit or require("bit") -local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift -local tohex = bit.tohex - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", "IMMSH" -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0xffffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp = "r1" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r1" then return "sp" end - return s -end - -local map_cond = { - lt = 0, gt = 1, eq = 2, so = 3, - ge = 4, le = 5, ne = 6, ns = 7, -} - ------------------------------------------------------------------------------- - -local map_op, op_template - -local function op_alias(opname, f) - return function(params, nparams) - if not params then return "-> "..opname:sub(1, -3) end - f(params, nparams) - op_template(params, map_op[opname], nparams) - end -end - --- Template strings for PPC instructions. -map_op = { - tdi_3 = "08000000ARI", - twi_3 = "0c000000ARI", - mulli_3 = "1c000000RRI", - subfic_3 = "20000000RRI", - cmplwi_3 = "28000000XRU", - cmplwi_2 = "28000000-RU", - cmpldi_3 = "28200000XRU", - cmpldi_2 = "28200000-RU", - cmpwi_3 = "2c000000XRI", - cmpwi_2 = "2c000000-RI", - cmpdi_3 = "2c200000XRI", - cmpdi_2 = "2c200000-RI", - addic_3 = "30000000RRI", - ["addic._3"] = "34000000RRI", - addi_3 = "38000000RR0I", - li_2 = "38000000RI", - la_2 = "38000000RD", - addis_3 = "3c000000RR0I", - lis_2 = "3c000000RI", - lus_2 = "3c000000RU", - bc_3 = "40000000AAK", - bcl_3 = "40000001AAK", - bdnz_1 = "42000000K", - bdz_1 = "42400000K", - sc_0 = "44000000", - b_1 = "48000000J", - bl_1 = "48000001J", - rlwimi_5 = "50000000RR~AAA.", - rlwinm_5 = "54000000RR~AAA.", - rlwnm_5 = "5c000000RR~RAA.", - ori_3 = "60000000RR~U", - nop_0 = "60000000", - oris_3 = "64000000RR~U", - xori_3 = "68000000RR~U", - xoris_3 = "6c000000RR~U", - ["andi._3"] = "70000000RR~U", - ["andis._3"] = "74000000RR~U", - lwz_2 = "80000000RD", - lwzu_2 = "84000000RD", - lbz_2 = "88000000RD", - lbzu_2 = "8c000000RD", - stw_2 = "90000000RD", - stwu_2 = "94000000RD", - stb_2 = "98000000RD", - stbu_2 = "9c000000RD", - lhz_2 = "a0000000RD", - lhzu_2 = "a4000000RD", - lha_2 = "a8000000RD", - lhau_2 = "ac000000RD", - sth_2 = "b0000000RD", - sthu_2 = "b4000000RD", - lmw_2 = "b8000000RD", - stmw_2 = "bc000000RD", - lfs_2 = "c0000000FD", - lfsu_2 = "c4000000FD", - lfd_2 = "c8000000FD", - lfdu_2 = "cc000000FD", - stfs_2 = "d0000000FD", - stfsu_2 = "d4000000FD", - stfd_2 = "d8000000FD", - stfdu_2 = "dc000000FD", - ld_2 = "e8000000RD", -- NYI: displacement must be divisible by 4. - ldu_2 = "e8000001RD", - lwa_2 = "e8000002RD", - std_2 = "f8000000RD", - stdu_2 = "f8000001RD", - - subi_3 = op_alias("addi_3", function(p) p[3] = "-("..p[3]..")" end), - subis_3 = op_alias("addis_3", function(p) p[3] = "-("..p[3]..")" end), - subic_3 = op_alias("addic_3", function(p) p[3] = "-("..p[3]..")" end), - ["subic._3"] = op_alias("addic._3", function(p) p[3] = "-("..p[3]..")" end), - - rotlwi_3 = op_alias("rlwinm_5", function(p) - p[4] = "0"; p[5] = "31" - end), - rotrwi_3 = op_alias("rlwinm_5", function(p) - p[3] = "32-("..p[3]..")"; p[4] = "0"; p[5] = "31" - end), - rotlw_3 = op_alias("rlwnm_5", function(p) - p[4] = "0"; p[5] = "31" - end), - slwi_3 = op_alias("rlwinm_5", function(p) - p[5] = "31-("..p[3]..")"; p[4] = "0" - end), - srwi_3 = op_alias("rlwinm_5", function(p) - p[4] = p[3]; p[3] = "32-("..p[3]..")"; p[5] = "31" - end), - clrlwi_3 = op_alias("rlwinm_5", function(p) - p[4] = p[3]; p[3] = "0"; p[5] = "31" - end), - clrrwi_3 = op_alias("rlwinm_5", function(p) - p[5] = "31-("..p[3]..")"; p[3] = "0"; p[4] = "0" - end), - - -- Primary opcode 4: - mulhhwu_3 = "10000010RRR.", - machhwu_3 = "10000018RRR.", - mulhhw_3 = "10000050RRR.", - nmachhw_3 = "1000005cRRR.", - machhwsu_3 = "10000098RRR.", - machhws_3 = "100000d8RRR.", - nmachhws_3 = "100000dcRRR.", - mulchwu_3 = "10000110RRR.", - macchwu_3 = "10000118RRR.", - mulchw_3 = "10000150RRR.", - macchw_3 = "10000158RRR.", - nmacchw_3 = "1000015cRRR.", - macchwsu_3 = "10000198RRR.", - macchws_3 = "100001d8RRR.", - nmacchws_3 = "100001dcRRR.", - mullhw_3 = "10000350RRR.", - maclhw_3 = "10000358RRR.", - nmaclhw_3 = "1000035cRRR.", - maclhwsu_3 = "10000398RRR.", - maclhws_3 = "100003d8RRR.", - nmaclhws_3 = "100003dcRRR.", - machhwuo_3 = "10000418RRR.", - nmachhwo_3 = "1000045cRRR.", - machhwsuo_3 = "10000498RRR.", - machhwso_3 = "100004d8RRR.", - nmachhwso_3 = "100004dcRRR.", - macchwuo_3 = "10000518RRR.", - macchwo_3 = "10000558RRR.", - nmacchwo_3 = "1000055cRRR.", - macchwsuo_3 = "10000598RRR.", - macchwso_3 = "100005d8RRR.", - nmacchwso_3 = "100005dcRRR.", - maclhwo_3 = "10000758RRR.", - nmaclhwo_3 = "1000075cRRR.", - maclhwsuo_3 = "10000798RRR.", - maclhwso_3 = "100007d8RRR.", - nmaclhwso_3 = "100007dcRRR.", - - vaddubm_3 = "10000000VVV", - vmaxub_3 = "10000002VVV", - vrlb_3 = "10000004VVV", - vcmpequb_3 = "10000006VVV", - vmuloub_3 = "10000008VVV", - vaddfp_3 = "1000000aVVV", - vmrghb_3 = "1000000cVVV", - vpkuhum_3 = "1000000eVVV", - vmhaddshs_4 = "10000020VVVV", - vmhraddshs_4 = "10000021VVVV", - vmladduhm_4 = "10000022VVVV", - vmsumubm_4 = "10000024VVVV", - vmsummbm_4 = "10000025VVVV", - vmsumuhm_4 = "10000026VVVV", - vmsumuhs_4 = "10000027VVVV", - vmsumshm_4 = "10000028VVVV", - vmsumshs_4 = "10000029VVVV", - vsel_4 = "1000002aVVVV", - vperm_4 = "1000002bVVVV", - vsldoi_4 = "1000002cVVVP", - vpermxor_4 = "1000002dVVVV", - vmaddfp_4 = "1000002eVVVV~", - vnmsubfp_4 = "1000002fVVVV~", - vaddeuqm_4 = "1000003cVVVV", - vaddecuq_4 = "1000003dVVVV", - vsubeuqm_4 = "1000003eVVVV", - vsubecuq_4 = "1000003fVVVV", - vadduhm_3 = "10000040VVV", - vmaxuh_3 = "10000042VVV", - vrlh_3 = "10000044VVV", - vcmpequh_3 = "10000046VVV", - vmulouh_3 = "10000048VVV", - vsubfp_3 = "1000004aVVV", - vmrghh_3 = "1000004cVVV", - vpkuwum_3 = "1000004eVVV", - vadduwm_3 = "10000080VVV", - vmaxuw_3 = "10000082VVV", - vrlw_3 = "10000084VVV", - vcmpequw_3 = "10000086VVV", - vmulouw_3 = "10000088VVV", - vmuluwm_3 = "10000089VVV", - vmrghw_3 = "1000008cVVV", - vpkuhus_3 = "1000008eVVV", - vaddudm_3 = "100000c0VVV", - vmaxud_3 = "100000c2VVV", - vrld_3 = "100000c4VVV", - vcmpeqfp_3 = "100000c6VVV", - vcmpequd_3 = "100000c7VVV", - vpkuwus_3 = "100000ceVVV", - vadduqm_3 = "10000100VVV", - vmaxsb_3 = "10000102VVV", - vslb_3 = "10000104VVV", - vmulosb_3 = "10000108VVV", - vrefp_2 = "1000010aV-V", - vmrglb_3 = "1000010cVVV", - vpkshus_3 = "1000010eVVV", - vaddcuq_3 = "10000140VVV", - vmaxsh_3 = "10000142VVV", - vslh_3 = "10000144VVV", - vmulosh_3 = "10000148VVV", - vrsqrtefp_2 = "1000014aV-V", - vmrglh_3 = "1000014cVVV", - vpkswus_3 = "1000014eVVV", - vaddcuw_3 = "10000180VVV", - vmaxsw_3 = "10000182VVV", - vslw_3 = "10000184VVV", - vmulosw_3 = "10000188VVV", - vexptefp_2 = "1000018aV-V", - vmrglw_3 = "1000018cVVV", - vpkshss_3 = "1000018eVVV", - vmaxsd_3 = "100001c2VVV", - vsl_3 = "100001c4VVV", - vcmpgefp_3 = "100001c6VVV", - vlogefp_2 = "100001caV-V", - vpkswss_3 = "100001ceVVV", - vadduhs_3 = "10000240VVV", - vminuh_3 = "10000242VVV", - vsrh_3 = "10000244VVV", - vcmpgtuh_3 = "10000246VVV", - vmuleuh_3 = "10000248VVV", - vrfiz_2 = "1000024aV-V", - vsplth_3 = "1000024cVV3", - vupkhsh_2 = "1000024eV-V", - vminuw_3 = "10000282VVV", - vminud_3 = "100002c2VVV", - vcmpgtud_3 = "100002c7VVV", - vrfim_2 = "100002caV-V", - vcmpgtsb_3 = "10000306VVV", - vcfux_3 = "1000030aVVA~", - vaddshs_3 = "10000340VVV", - vminsh_3 = "10000342VVV", - vsrah_3 = "10000344VVV", - vcmpgtsh_3 = "10000346VVV", - vmulesh_3 = "10000348VVV", - vcfsx_3 = "1000034aVVA~", - vspltish_2 = "1000034cVS", - vupkhpx_2 = "1000034eV-V", - vaddsws_3 = "10000380VVV", - vminsw_3 = "10000382VVV", - vsraw_3 = "10000384VVV", - vcmpgtsw_3 = "10000386VVV", - vmulesw_3 = "10000388VVV", - vctuxs_3 = "1000038aVVA~", - vspltisw_2 = "1000038cVS", - vminsd_3 = "100003c2VVV", - vsrad_3 = "100003c4VVV", - vcmpbfp_3 = "100003c6VVV", - vcmpgtsd_3 = "100003c7VVV", - vctsxs_3 = "100003caVVA~", - vupklpx_2 = "100003ceV-V", - vsububm_3 = "10000400VVV", - ["bcdadd._4"] = "10000401VVVy.", - vavgub_3 = "10000402VVV", - vand_3 = "10000404VVV", - ["vcmpequb._3"] = "10000406VVV", - vmaxfp_3 = "1000040aVVV", - vsubuhm_3 = "10000440VVV", - ["bcdsub._4"] = "10000441VVVy.", - vavguh_3 = "10000442VVV", - vandc_3 = "10000444VVV", - ["vcmpequh._3"] = "10000446VVV", - vminfp_3 = "1000044aVVV", - vpkudum_3 = "1000044eVVV", - vsubuwm_3 = "10000480VVV", - vavguw_3 = "10000482VVV", - vor_3 = "10000484VVV", - ["vcmpequw._3"] = "10000486VVV", - vpmsumw_3 = "10000488VVV", - ["vcmpeqfp._3"] = "100004c6VVV", - ["vcmpequd._3"] = "100004c7VVV", - vpkudus_3 = "100004ceVVV", - vavgsb_3 = "10000502VVV", - vavgsh_3 = "10000542VVV", - vorc_3 = "10000544VVV", - vbpermq_3 = "1000054cVVV", - vpksdus_3 = "1000054eVVV", - vavgsw_3 = "10000582VVV", - vsld_3 = "100005c4VVV", - ["vcmpgefp._3"] = "100005c6VVV", - vpksdss_3 = "100005ceVVV", - vsububs_3 = "10000600VVV", - mfvscr_1 = "10000604V--", - vsum4ubs_3 = "10000608VVV", - vsubuhs_3 = "10000640VVV", - mtvscr_1 = "10000644--V", - ["vcmpgtuh._3"] = "10000646VVV", - vsum4shs_3 = "10000648VVV", - vupkhsw_2 = "1000064eV-V", - vsubuws_3 = "10000680VVV", - vshasigmaw_4 = "10000682VVYp", - veqv_3 = "10000684VVV", - vsum2sws_3 = "10000688VVV", - vmrgow_3 = "1000068cVVV", - vshasigmad_4 = "100006c2VVYp", - vsrd_3 = "100006c4VVV", - ["vcmpgtud._3"] = "100006c7VVV", - vupklsw_2 = "100006ceV-V", - vupkslw_2 = "100006ceV-V", - vsubsbs_3 = "10000700VVV", - vclzb_2 = "10000702V-V", - vpopcntb_2 = "10000703V-V", - ["vcmpgtsb._3"] = "10000706VVV", - vsum4sbs_3 = "10000708VVV", - vsubshs_3 = "10000740VVV", - vclzh_2 = "10000742V-V", - vpopcnth_2 = "10000743V-V", - ["vcmpgtsh._3"] = "10000746VVV", - vsubsws_3 = "10000780VVV", - vclzw_2 = "10000782V-V", - vpopcntw_2 = "10000783V-V", - ["vcmpgtsw._3"] = "10000786VVV", - vsumsws_3 = "10000788VVV", - vmrgew_3 = "1000078cVVV", - vclzd_2 = "100007c2V-V", - vpopcntd_2 = "100007c3V-V", - ["vcmpbfp._3"] = "100007c6VVV", - ["vcmpgtsd._3"] = "100007c7VVV", - - -- Primary opcode 19: - mcrf_2 = "4c000000XX", - isync_0 = "4c00012c", - crnor_3 = "4c000042CCC", - crnot_2 = "4c000042CC=", - crandc_3 = "4c000102CCC", - crxor_3 = "4c000182CCC", - crclr_1 = "4c000182C==", - crnand_3 = "4c0001c2CCC", - crand_3 = "4c000202CCC", - creqv_3 = "4c000242CCC", - crset_1 = "4c000242C==", - crorc_3 = "4c000342CCC", - cror_3 = "4c000382CCC", - crmove_2 = "4c000382CC=", - bclr_2 = "4c000020AA", - bclrl_2 = "4c000021AA", - bcctr_2 = "4c000420AA", - bcctrl_2 = "4c000421AA", - bctar_2 = "4c000460AA", - bctarl_2 = "4c000461AA", - blr_0 = "4e800020", - blrl_0 = "4e800021", - bctr_0 = "4e800420", - bctrl_0 = "4e800421", - - -- Primary opcode 31: - cmpw_3 = "7c000000XRR", - cmpw_2 = "7c000000-RR", - cmpd_3 = "7c200000XRR", - cmpd_2 = "7c200000-RR", - tw_3 = "7c000008ARR", - lvsl_3 = "7c00000cVRR", - subfc_3 = "7c000010RRR.", - subc_3 = "7c000010RRR~.", - mulhdu_3 = "7c000012RRR.", - addc_3 = "7c000014RRR.", - mulhwu_3 = "7c000016RRR.", - isel_4 = "7c00001eRRRC", - isellt_3 = "7c00001eRRR", - iselgt_3 = "7c00005eRRR", - iseleq_3 = "7c00009eRRR", - mfcr_1 = "7c000026R", - mfocrf_2 = "7c100026RG", - mtcrf_2 = "7c000120GR", - mtocrf_2 = "7c100120GR", - lwarx_3 = "7c000028RR0R", - ldx_3 = "7c00002aRR0R", - lwzx_3 = "7c00002eRR0R", - slw_3 = "7c000030RR~R.", - cntlzw_2 = "7c000034RR~", - sld_3 = "7c000036RR~R.", - and_3 = "7c000038RR~R.", - cmplw_3 = "7c000040XRR", - cmplw_2 = "7c000040-RR", - cmpld_3 = "7c200040XRR", - cmpld_2 = "7c200040-RR", - lvsr_3 = "7c00004cVRR", - subf_3 = "7c000050RRR.", - sub_3 = "7c000050RRR~.", - lbarx_3 = "7c000068RR0R", - ldux_3 = "7c00006aRR0R", - dcbst_2 = "7c00006c-RR", - lwzux_3 = "7c00006eRR0R", - cntlzd_2 = "7c000074RR~", - andc_3 = "7c000078RR~R.", - td_3 = "7c000088ARR", - lvewx_3 = "7c00008eVRR", - mulhd_3 = "7c000092RRR.", - addg6s_3 = "7c000094RRR", - mulhw_3 = "7c000096RRR.", - dlmzb_3 = "7c00009cRR~R.", - ldarx_3 = "7c0000a8RR0R", - dcbf_2 = "7c0000ac-RR", - lbzx_3 = "7c0000aeRR0R", - lvx_3 = "7c0000ceVRR", - neg_2 = "7c0000d0RR.", - lharx_3 = "7c0000e8RR0R", - lbzux_3 = "7c0000eeRR0R", - popcntb_2 = "7c0000f4RR~", - not_2 = "7c0000f8RR~%.", - nor_3 = "7c0000f8RR~R.", - stvebx_3 = "7c00010eVRR", - subfe_3 = "7c000110RRR.", - sube_3 = "7c000110RRR~.", - adde_3 = "7c000114RRR.", - stdx_3 = "7c00012aRR0R", - ["stwcx._3"] = "7c00012dRR0R.", - stwx_3 = "7c00012eRR0R", - prtyw_2 = "7c000134RR~", - stvehx_3 = "7c00014eVRR", - stdux_3 = "7c00016aRR0R", - ["stqcx._3"] = "7c00016dR:R0R.", - stwux_3 = "7c00016eRR0R", - prtyd_2 = "7c000174RR~", - stvewx_3 = "7c00018eVRR", - subfze_2 = "7c000190RR.", - addze_2 = "7c000194RR.", - ["stdcx._3"] = "7c0001adRR0R.", - stbx_3 = "7c0001aeRR0R", - stvx_3 = "7c0001ceVRR", - subfme_2 = "7c0001d0RR.", - mulld_3 = "7c0001d2RRR.", - addme_2 = "7c0001d4RR.", - mullw_3 = "7c0001d6RRR.", - dcbtst_2 = "7c0001ec-RR", - stbux_3 = "7c0001eeRR0R", - bpermd_3 = "7c0001f8RR~R", - lvepxl_3 = "7c00020eVRR", - add_3 = "7c000214RRR.", - lqarx_3 = "7c000228R:R0R", - dcbt_2 = "7c00022c-RR", - lhzx_3 = "7c00022eRR0R", - cdtbcd_2 = "7c000234RR~", - eqv_3 = "7c000238RR~R.", - lvepx_3 = "7c00024eVRR", - eciwx_3 = "7c00026cRR0R", - lhzux_3 = "7c00026eRR0R", - cbcdtd_2 = "7c000274RR~", - xor_3 = "7c000278RR~R.", - mfspefscr_1 = "7c0082a6R", - mfxer_1 = "7c0102a6R", - mflr_1 = "7c0802a6R", - mfctr_1 = "7c0902a6R", - lwax_3 = "7c0002aaRR0R", - lhax_3 = "7c0002aeRR0R", - mftb_1 = "7c0c42e6R", - mftbu_1 = "7c0d42e6R", - lvxl_3 = "7c0002ceVRR", - lwaux_3 = "7c0002eaRR0R", - lhaux_3 = "7c0002eeRR0R", - popcntw_2 = "7c0002f4RR~", - divdeu_3 = "7c000312RRR.", - divweu_3 = "7c000316RRR.", - sthx_3 = "7c00032eRR0R", - orc_3 = "7c000338RR~R.", - ecowx_3 = "7c00036cRR0R", - sthux_3 = "7c00036eRR0R", - or_3 = "7c000378RR~R.", - mr_2 = "7c000378RR~%.", - divdu_3 = "7c000392RRR.", - divwu_3 = "7c000396RRR.", - mtspefscr_1 = "7c0083a6R", - mtxer_1 = "7c0103a6R", - mtlr_1 = "7c0803a6R", - mtctr_1 = "7c0903a6R", - dcbi_2 = "7c0003ac-RR", - nand_3 = "7c0003b8RR~R.", - dsn_2 = "7c0003c6-RR", - stvxl_3 = "7c0003ceVRR", - divd_3 = "7c0003d2RRR.", - divw_3 = "7c0003d6RRR.", - popcntd_2 = "7c0003f4RR~", - cmpb_3 = "7c0003f8RR~R.", - mcrxr_1 = "7c000400X", - lbdx_3 = "7c000406RRR", - subfco_3 = "7c000410RRR.", - subco_3 = "7c000410RRR~.", - addco_3 = "7c000414RRR.", - ldbrx_3 = "7c000428RR0R", - lswx_3 = "7c00042aRR0R", - lwbrx_3 = "7c00042cRR0R", - lfsx_3 = "7c00042eFR0R", - srw_3 = "7c000430RR~R.", - srd_3 = "7c000436RR~R.", - lhdx_3 = "7c000446RRR", - subfo_3 = "7c000450RRR.", - subo_3 = "7c000450RRR~.", - lfsux_3 = "7c00046eFR0R", - lwdx_3 = "7c000486RRR", - lswi_3 = "7c0004aaRR0A", - sync_0 = "7c0004ac", - lwsync_0 = "7c2004ac", - ptesync_0 = "7c4004ac", - lfdx_3 = "7c0004aeFR0R", - lddx_3 = "7c0004c6RRR", - nego_2 = "7c0004d0RR.", - lfdux_3 = "7c0004eeFR0R", - stbdx_3 = "7c000506RRR", - subfeo_3 = "7c000510RRR.", - subeo_3 = "7c000510RRR~.", - addeo_3 = "7c000514RRR.", - stdbrx_3 = "7c000528RR0R", - stswx_3 = "7c00052aRR0R", - stwbrx_3 = "7c00052cRR0R", - stfsx_3 = "7c00052eFR0R", - sthdx_3 = "7c000546RRR", - ["stbcx._3"] = "7c00056dRRR", - stfsux_3 = "7c00056eFR0R", - stwdx_3 = "7c000586RRR", - subfzeo_2 = "7c000590RR.", - addzeo_2 = "7c000594RR.", - stswi_3 = "7c0005aaRR0A", - ["sthcx._3"] = "7c0005adRRR", - stfdx_3 = "7c0005aeFR0R", - stddx_3 = "7c0005c6RRR", - subfmeo_2 = "7c0005d0RR.", - mulldo_3 = "7c0005d2RRR.", - addmeo_2 = "7c0005d4RR.", - mullwo_3 = "7c0005d6RRR.", - dcba_2 = "7c0005ec-RR", - stfdux_3 = "7c0005eeFR0R", - stvepxl_3 = "7c00060eVRR", - addo_3 = "7c000614RRR.", - lhbrx_3 = "7c00062cRR0R", - lfdpx_3 = "7c00062eF:RR", - sraw_3 = "7c000630RR~R.", - srad_3 = "7c000634RR~R.", - lfddx_3 = "7c000646FRR", - stvepx_3 = "7c00064eVRR", - srawi_3 = "7c000670RR~A.", - sradi_3 = "7c000674RR~H.", - eieio_0 = "7c0006ac", - lfiwax_3 = "7c0006aeFR0R", - divdeuo_3 = "7c000712RRR.", - divweuo_3 = "7c000716RRR.", - sthbrx_3 = "7c00072cRR0R", - stfdpx_3 = "7c00072eF:RR", - extsh_2 = "7c000734RR~.", - stfddx_3 = "7c000746FRR", - divdeo_3 = "7c000752RRR.", - divweo_3 = "7c000756RRR.", - extsb_2 = "7c000774RR~.", - divduo_3 = "7c000792RRR.", - divwou_3 = "7c000796RRR.", - icbi_2 = "7c0007ac-RR", - stfiwx_3 = "7c0007aeFR0R", - extsw_2 = "7c0007b4RR~.", - divdo_3 = "7c0007d2RRR.", - divwo_3 = "7c0007d6RRR.", - dcbz_2 = "7c0007ec-RR", - - ["tbegin._1"] = "7c00051d1", - ["tbegin._0"] = "7c00051d", - ["tend._1"] = "7c00055dY", - ["tend._0"] = "7c00055d", - ["tendall._0"] = "7e00055d", - tcheck_1 = "7c00059cX", - ["tsr._1"] = "7c0005dd1", - ["tsuspend._0"] = "7c0005dd", - ["tresume._0"] = "7c2005dd", - ["tabortwc._3"] = "7c00061dARR", - ["tabortdc._3"] = "7c00065dARR", - ["tabortwci._3"] = "7c00069dARS", - ["tabortdci._3"] = "7c0006ddARS", - ["tabort._1"] = "7c00071d-R-", - ["treclaim._1"] = "7c00075d-R", - ["trechkpt._0"] = "7c0007dd", - - lxsiwzx_3 = "7c000018QRR", - lxsiwax_3 = "7c000098QRR", - mfvsrd_2 = "7c000066-Rq", - mfvsrwz_2 = "7c0000e6-Rq", - stxsiwx_3 = "7c000118QRR", - mtvsrd_2 = "7c000166QR", - mtvsrwa_2 = "7c0001a6QR", - lxvdsx_3 = "7c000298QRR", - lxsspx_3 = "7c000418QRR", - lxsdx_3 = "7c000498QRR", - stxsspx_3 = "7c000518QRR", - stxsdx_3 = "7c000598QRR", - lxvw4x_3 = "7c000618QRR", - lxvd2x_3 = "7c000698QRR", - stxvw4x_3 = "7c000718QRR", - stxvd2x_3 = "7c000798QRR", - - -- Primary opcode 30: - rldicl_4 = "78000000RR~HM.", - rldicr_4 = "78000004RR~HM.", - rldic_4 = "78000008RR~HM.", - rldimi_4 = "7800000cRR~HM.", - rldcl_4 = "78000010RR~RM.", - rldcr_4 = "78000012RR~RM.", - - rotldi_3 = op_alias("rldicl_4", function(p) - p[4] = "0" - end), - rotrdi_3 = op_alias("rldicl_4", function(p) - p[3] = "64-("..p[3]..")"; p[4] = "0" - end), - rotld_3 = op_alias("rldcl_4", function(p) - p[4] = "0" - end), - sldi_3 = op_alias("rldicr_4", function(p) - p[4] = "63-("..p[3]..")" - end), - srdi_3 = op_alias("rldicl_4", function(p) - p[4] = p[3]; p[3] = "64-("..p[3]..")" - end), - clrldi_3 = op_alias("rldicl_4", function(p) - p[4] = p[3]; p[3] = "0" - end), - clrrdi_3 = op_alias("rldicr_4", function(p) - p[4] = "63-("..p[3]..")"; p[3] = "0" - end), - - -- Primary opcode 56: - lq_2 = "e0000000R:D", -- NYI: displacement must be divisible by 8. - - -- Primary opcode 57: - lfdp_2 = "e4000000F:D", -- NYI: displacement must be divisible by 4. - - -- Primary opcode 59: - fdivs_3 = "ec000024FFF.", - fsubs_3 = "ec000028FFF.", - fadds_3 = "ec00002aFFF.", - fsqrts_2 = "ec00002cF-F.", - fres_2 = "ec000030F-F.", - fmuls_3 = "ec000032FF-F.", - frsqrtes_2 = "ec000034F-F.", - fmsubs_4 = "ec000038FFFF~.", - fmadds_4 = "ec00003aFFFF~.", - fnmsubs_4 = "ec00003cFFFF~.", - fnmadds_4 = "ec00003eFFFF~.", - fcfids_2 = "ec00069cF-F.", - fcfidus_2 = "ec00079cF-F.", - - dadd_3 = "ec000004FFF.", - dqua_4 = "ec000006FFFZ.", - dmul_3 = "ec000044FFF.", - drrnd_4 = "ec000046FFFZ.", - dscli_3 = "ec000084FF6.", - dquai_4 = "ec000086SF~FZ.", - dscri_3 = "ec0000c4FF6.", - drintx_4 = "ec0000c61F~FZ.", - dcmpo_3 = "ec000104XFF", - dtstex_3 = "ec000144XFF", - dtstdc_3 = "ec000184XF6", - dtstdg_3 = "ec0001c4XF6", - drintn_4 = "ec0001c61F~FZ.", - dctdp_2 = "ec000204F-F.", - dctfix_2 = "ec000244F-F.", - ddedpd_3 = "ec000284ZF~F.", - dxex_2 = "ec0002c4F-F.", - dsub_3 = "ec000404FFF.", - ddiv_3 = "ec000444FFF.", - dcmpu_3 = "ec000504XFF", - dtstsf_3 = "ec000544XFF", - drsp_2 = "ec000604F-F.", - dcffix_2 = "ec000644F-F.", - denbcd_3 = "ec000684YF~F.", - diex_3 = "ec0006c4FFF.", - - -- Primary opcode 60: - xsaddsp_3 = "f0000000QQQ", - xsmaddasp_3 = "f0000008QQQ", - xxsldwi_4 = "f0000010QQQz", - xsrsqrtesp_2 = "f0000028Q-Q", - xssqrtsp_2 = "f000002cQ-Q", - xxsel_4 = "f0000030QQQQ", - xssubsp_3 = "f0000040QQQ", - xsmaddmsp_3 = "f0000048QQQ", - xxpermdi_4 = "f0000050QQQz", - xsresp_2 = "f0000068Q-Q", - xsmulsp_3 = "f0000080QQQ", - xsmsubasp_3 = "f0000088QQQ", - xxmrghw_3 = "f0000090QQQ", - xsdivsp_3 = "f00000c0QQQ", - xsmsubmsp_3 = "f00000c8QQQ", - xsadddp_3 = "f0000100QQQ", - xsmaddadp_3 = "f0000108QQQ", - xscmpudp_3 = "f0000118XQQ", - xscvdpuxws_2 = "f0000120Q-Q", - xsrdpi_2 = "f0000124Q-Q", - xsrsqrtedp_2 = "f0000128Q-Q", - xssqrtdp_2 = "f000012cQ-Q", - xssubdp_3 = "f0000140QQQ", - xsmaddmdp_3 = "f0000148QQQ", - xscmpodp_3 = "f0000158XQQ", - xscvdpsxws_2 = "f0000160Q-Q", - xsrdpiz_2 = "f0000164Q-Q", - xsredp_2 = "f0000168Q-Q", - xsmuldp_3 = "f0000180QQQ", - xsmsubadp_3 = "f0000188QQQ", - xxmrglw_3 = "f0000190QQQ", - xsrdpip_2 = "f00001a4Q-Q", - xstsqrtdp_2 = "f00001a8X-Q", - xsrdpic_2 = "f00001acQ-Q", - xsdivdp_3 = "f00001c0QQQ", - xsmsubmdp_3 = "f00001c8QQQ", - xsrdpim_2 = "f00001e4Q-Q", - xstdivdp_3 = "f00001e8XQQ", - xvaddsp_3 = "f0000200QQQ", - xvmaddasp_3 = "f0000208QQQ", - xvcmpeqsp_3 = "f0000218QQQ", - xvcvspuxws_2 = "f0000220Q-Q", - xvrspi_2 = "f0000224Q-Q", - xvrsqrtesp_2 = "f0000228Q-Q", - xvsqrtsp_2 = "f000022cQ-Q", - xvsubsp_3 = "f0000240QQQ", - xvmaddmsp_3 = "f0000248QQQ", - xvcmpgtsp_3 = "f0000258QQQ", - xvcvspsxws_2 = "f0000260Q-Q", - xvrspiz_2 = "f0000264Q-Q", - xvresp_2 = "f0000268Q-Q", - xvmulsp_3 = "f0000280QQQ", - xvmsubasp_3 = "f0000288QQQ", - xxspltw_3 = "f0000290QQg~", - xvcmpgesp_3 = "f0000298QQQ", - xvcvuxwsp_2 = "f00002a0Q-Q", - xvrspip_2 = "f00002a4Q-Q", - xvtsqrtsp_2 = "f00002a8X-Q", - xvrspic_2 = "f00002acQ-Q", - xvdivsp_3 = "f00002c0QQQ", - xvmsubmsp_3 = "f00002c8QQQ", - xvcvsxwsp_2 = "f00002e0Q-Q", - xvrspim_2 = "f00002e4Q-Q", - xvtdivsp_3 = "f00002e8XQQ", - xvadddp_3 = "f0000300QQQ", - xvmaddadp_3 = "f0000308QQQ", - xvcmpeqdp_3 = "f0000318QQQ", - xvcvdpuxws_2 = "f0000320Q-Q", - xvrdpi_2 = "f0000324Q-Q", - xvrsqrtedp_2 = "f0000328Q-Q", - xvsqrtdp_2 = "f000032cQ-Q", - xvsubdp_3 = "f0000340QQQ", - xvmaddmdp_3 = "f0000348QQQ", - xvcmpgtdp_3 = "f0000358QQQ", - xvcvdpsxws_2 = "f0000360Q-Q", - xvrdpiz_2 = "f0000364Q-Q", - xvredp_2 = "f0000368Q-Q", - xvmuldp_3 = "f0000380QQQ", - xvmsubadp_3 = "f0000388QQQ", - xvcmpgedp_3 = "f0000398QQQ", - xvcvuxwdp_2 = "f00003a0Q-Q", - xvrdpip_2 = "f00003a4Q-Q", - xvtsqrtdp_2 = "f00003a8X-Q", - xvrdpic_2 = "f00003acQ-Q", - xvdivdp_3 = "f00003c0QQQ", - xvmsubmdp_3 = "f00003c8QQQ", - xvcvsxwdp_2 = "f00003e0Q-Q", - xvrdpim_2 = "f00003e4Q-Q", - xvtdivdp_3 = "f00003e8XQQ", - xsnmaddasp_3 = "f0000408QQQ", - xxland_3 = "f0000410QQQ", - xscvdpsp_2 = "f0000424Q-Q", - xscvdpspn_2 = "f000042cQ-Q", - xsnmaddmsp_3 = "f0000448QQQ", - xxlandc_3 = "f0000450QQQ", - xsrsp_2 = "f0000464Q-Q", - xsnmsubasp_3 = "f0000488QQQ", - xxlor_3 = "f0000490QQQ", - xscvuxdsp_2 = "f00004a0Q-Q", - xsnmsubmsp_3 = "f00004c8QQQ", - xxlxor_3 = "f00004d0QQQ", - xscvsxdsp_2 = "f00004e0Q-Q", - xsmaxdp_3 = "f0000500QQQ", - xsnmaddadp_3 = "f0000508QQQ", - xxlnor_3 = "f0000510QQQ", - xscvdpuxds_2 = "f0000520Q-Q", - xscvspdp_2 = "f0000524Q-Q", - xscvspdpn_2 = "f000052cQ-Q", - xsmindp_3 = "f0000540QQQ", - xsnmaddmdp_3 = "f0000548QQQ", - xxlorc_3 = "f0000550QQQ", - xscvdpsxds_2 = "f0000560Q-Q", - xsabsdp_2 = "f0000564Q-Q", - xscpsgndp_3 = "f0000580QQQ", - xsnmsubadp_3 = "f0000588QQQ", - xxlnand_3 = "f0000590QQQ", - xscvuxddp_2 = "f00005a0Q-Q", - xsnabsdp_2 = "f00005a4Q-Q", - xsnmsubmdp_3 = "f00005c8QQQ", - xxleqv_3 = "f00005d0QQQ", - xscvsxddp_2 = "f00005e0Q-Q", - xsnegdp_2 = "f00005e4Q-Q", - xvmaxsp_3 = "f0000600QQQ", - xvnmaddasp_3 = "f0000608QQQ", - ["xvcmpeqsp._3"] = "f0000618QQQ", - xvcvspuxds_2 = "f0000620Q-Q", - xvcvdpsp_2 = "f0000624Q-Q", - xvminsp_3 = "f0000640QQQ", - xvnmaddmsp_3 = "f0000648QQQ", - ["xvcmpgtsp._3"] = "f0000658QQQ", - xvcvspsxds_2 = "f0000660Q-Q", - xvabssp_2 = "f0000664Q-Q", - xvcpsgnsp_3 = "f0000680QQQ", - xvnmsubasp_3 = "f0000688QQQ", - ["xvcmpgesp._3"] = "f0000698QQQ", - xvcvuxdsp_2 = "f00006a0Q-Q", - xvnabssp_2 = "f00006a4Q-Q", - xvnmsubmsp_3 = "f00006c8QQQ", - xvcvsxdsp_2 = "f00006e0Q-Q", - xvnegsp_2 = "f00006e4Q-Q", - xvmaxdp_3 = "f0000700QQQ", - xvnmaddadp_3 = "f0000708QQQ", - ["xvcmpeqdp._3"] = "f0000718QQQ", - xvcvdpuxds_2 = "f0000720Q-Q", - xvcvspdp_2 = "f0000724Q-Q", - xvmindp_3 = "f0000740QQQ", - xvnmaddmdp_3 = "f0000748QQQ", - ["xvcmpgtdp._3"] = "f0000758QQQ", - xvcvdpsxds_2 = "f0000760Q-Q", - xvabsdp_2 = "f0000764Q-Q", - xvcpsgndp_3 = "f0000780QQQ", - xvnmsubadp_3 = "f0000788QQQ", - ["xvcmpgedp._3"] = "f0000798QQQ", - xvcvuxddp_2 = "f00007a0Q-Q", - xvnabsdp_2 = "f00007a4Q-Q", - xvnmsubmdp_3 = "f00007c8QQQ", - xvcvsxddp_2 = "f00007e0Q-Q", - xvnegdp_2 = "f00007e4Q-Q", - - -- Primary opcode 61: - stfdp_2 = "f4000000F:D", -- NYI: displacement must be divisible by 4. - - -- Primary opcode 62: - stq_2 = "f8000002R:D", -- NYI: displacement must be divisible by 8. - - -- Primary opcode 63: - fdiv_3 = "fc000024FFF.", - fsub_3 = "fc000028FFF.", - fadd_3 = "fc00002aFFF.", - fsqrt_2 = "fc00002cF-F.", - fsel_4 = "fc00002eFFFF~.", - fre_2 = "fc000030F-F.", - fmul_3 = "fc000032FF-F.", - frsqrte_2 = "fc000034F-F.", - fmsub_4 = "fc000038FFFF~.", - fmadd_4 = "fc00003aFFFF~.", - fnmsub_4 = "fc00003cFFFF~.", - fnmadd_4 = "fc00003eFFFF~.", - fcmpu_3 = "fc000000XFF", - fcpsgn_3 = "fc000010FFF.", - fcmpo_3 = "fc000040XFF", - mtfsb1_1 = "fc00004cA", - fneg_2 = "fc000050F-F.", - mcrfs_2 = "fc000080XX", - mtfsb0_1 = "fc00008cA", - fmr_2 = "fc000090F-F.", - frsp_2 = "fc000018F-F.", - fctiw_2 = "fc00001cF-F.", - fctiwz_2 = "fc00001eF-F.", - ftdiv_2 = "fc000100X-F.", - fctiwu_2 = "fc00011cF-F.", - fctiwuz_2 = "fc00011eF-F.", - mtfsfi_2 = "fc00010cAA", -- NYI: upshift. - fnabs_2 = "fc000110F-F.", - ftsqrt_2 = "fc000140X-F.", - fabs_2 = "fc000210F-F.", - frin_2 = "fc000310F-F.", - friz_2 = "fc000350F-F.", - frip_2 = "fc000390F-F.", - frim_2 = "fc0003d0F-F.", - mffs_1 = "fc00048eF.", - -- NYI: mtfsf, mtfsb0, mtfsb1. - fctid_2 = "fc00065cF-F.", - fctidz_2 = "fc00065eF-F.", - fmrgow_3 = "fc00068cFFF", - fcfid_2 = "fc00069cF-F.", - fctidu_2 = "fc00075cF-F.", - fctiduz_2 = "fc00075eF-F.", - fmrgew_3 = "fc00078cFFF", - fcfidu_2 = "fc00079cF-F.", - - daddq_3 = "fc000004F:F:F:.", - dquaq_4 = "fc000006F:F:F:Z.", - dmulq_3 = "fc000044F:F:F:.", - drrndq_4 = "fc000046F:F:F:Z.", - dscliq_3 = "fc000084F:F:6.", - dquaiq_4 = "fc000086SF:~F:Z.", - dscriq_3 = "fc0000c4F:F:6.", - drintxq_4 = "fc0000c61F:~F:Z.", - dcmpoq_3 = "fc000104XF:F:", - dtstexq_3 = "fc000144XF:F:", - dtstdcq_3 = "fc000184XF:6", - dtstdgq_3 = "fc0001c4XF:6", - drintnq_4 = "fc0001c61F:~F:Z.", - dctqpq_2 = "fc000204F:-F:.", - dctfixq_2 = "fc000244F:-F:.", - ddedpdq_3 = "fc000284ZF:~F:.", - dxexq_2 = "fc0002c4F:-F:.", - dsubq_3 = "fc000404F:F:F:.", - ddivq_3 = "fc000444F:F:F:.", - dcmpuq_3 = "fc000504XF:F:", - dtstsfq_3 = "fc000544XF:F:", - drdpq_2 = "fc000604F:-F:.", - dcffixq_2 = "fc000644F:-F:.", - denbcdq_3 = "fc000684YF:~F:.", - diexq_3 = "fc0006c4F:FF:.", - - -- Primary opcode 4, SPE APU extension: - evaddw_3 = "10000200RRR", - evaddiw_3 = "10000202RAR~", - evsubw_3 = "10000204RRR~", - evsubiw_3 = "10000206RAR~", - evabs_2 = "10000208RR", - evneg_2 = "10000209RR", - evextsb_2 = "1000020aRR", - evextsh_2 = "1000020bRR", - evrndw_2 = "1000020cRR", - evcntlzw_2 = "1000020dRR", - evcntlsw_2 = "1000020eRR", - brinc_3 = "1000020fRRR", - evand_3 = "10000211RRR", - evandc_3 = "10000212RRR", - evxor_3 = "10000216RRR", - evor_3 = "10000217RRR", - evmr_2 = "10000217RR=", - evnor_3 = "10000218RRR", - evnot_2 = "10000218RR=", - eveqv_3 = "10000219RRR", - evorc_3 = "1000021bRRR", - evnand_3 = "1000021eRRR", - evsrwu_3 = "10000220RRR", - evsrws_3 = "10000221RRR", - evsrwiu_3 = "10000222RRA", - evsrwis_3 = "10000223RRA", - evslw_3 = "10000224RRR", - evslwi_3 = "10000226RRA", - evrlw_3 = "10000228RRR", - evsplati_2 = "10000229RS", - evrlwi_3 = "1000022aRRA", - evsplatfi_2 = "1000022bRS", - evmergehi_3 = "1000022cRRR", - evmergelo_3 = "1000022dRRR", - evcmpgtu_3 = "10000230XRR", - evcmpgtu_2 = "10000230-RR", - evcmpgts_3 = "10000231XRR", - evcmpgts_2 = "10000231-RR", - evcmpltu_3 = "10000232XRR", - evcmpltu_2 = "10000232-RR", - evcmplts_3 = "10000233XRR", - evcmplts_2 = "10000233-RR", - evcmpeq_3 = "10000234XRR", - evcmpeq_2 = "10000234-RR", - evsel_4 = "10000278RRRW", - evsel_3 = "10000278RRR", - evfsadd_3 = "10000280RRR", - evfssub_3 = "10000281RRR", - evfsabs_2 = "10000284RR", - evfsnabs_2 = "10000285RR", - evfsneg_2 = "10000286RR", - evfsmul_3 = "10000288RRR", - evfsdiv_3 = "10000289RRR", - evfscmpgt_3 = "1000028cXRR", - evfscmpgt_2 = "1000028c-RR", - evfscmplt_3 = "1000028dXRR", - evfscmplt_2 = "1000028d-RR", - evfscmpeq_3 = "1000028eXRR", - evfscmpeq_2 = "1000028e-RR", - evfscfui_2 = "10000290R-R", - evfscfsi_2 = "10000291R-R", - evfscfuf_2 = "10000292R-R", - evfscfsf_2 = "10000293R-R", - evfsctui_2 = "10000294R-R", - evfsctsi_2 = "10000295R-R", - evfsctuf_2 = "10000296R-R", - evfsctsf_2 = "10000297R-R", - evfsctuiz_2 = "10000298R-R", - evfsctsiz_2 = "1000029aR-R", - evfststgt_3 = "1000029cXRR", - evfststgt_2 = "1000029c-RR", - evfststlt_3 = "1000029dXRR", - evfststlt_2 = "1000029d-RR", - evfststeq_3 = "1000029eXRR", - evfststeq_2 = "1000029e-RR", - efsadd_3 = "100002c0RRR", - efssub_3 = "100002c1RRR", - efsabs_2 = "100002c4RR", - efsnabs_2 = "100002c5RR", - efsneg_2 = "100002c6RR", - efsmul_3 = "100002c8RRR", - efsdiv_3 = "100002c9RRR", - efscmpgt_3 = "100002ccXRR", - efscmpgt_2 = "100002cc-RR", - efscmplt_3 = "100002cdXRR", - efscmplt_2 = "100002cd-RR", - efscmpeq_3 = "100002ceXRR", - efscmpeq_2 = "100002ce-RR", - efscfd_2 = "100002cfR-R", - efscfui_2 = "100002d0R-R", - efscfsi_2 = "100002d1R-R", - efscfuf_2 = "100002d2R-R", - efscfsf_2 = "100002d3R-R", - efsctui_2 = "100002d4R-R", - efsctsi_2 = "100002d5R-R", - efsctuf_2 = "100002d6R-R", - efsctsf_2 = "100002d7R-R", - efsctuiz_2 = "100002d8R-R", - efsctsiz_2 = "100002daR-R", - efststgt_3 = "100002dcXRR", - efststgt_2 = "100002dc-RR", - efststlt_3 = "100002ddXRR", - efststlt_2 = "100002dd-RR", - efststeq_3 = "100002deXRR", - efststeq_2 = "100002de-RR", - efdadd_3 = "100002e0RRR", - efdsub_3 = "100002e1RRR", - efdcfuid_2 = "100002e2R-R", - efdcfsid_2 = "100002e3R-R", - efdabs_2 = "100002e4RR", - efdnabs_2 = "100002e5RR", - efdneg_2 = "100002e6RR", - efdmul_3 = "100002e8RRR", - efddiv_3 = "100002e9RRR", - efdctuidz_2 = "100002eaR-R", - efdctsidz_2 = "100002ebR-R", - efdcmpgt_3 = "100002ecXRR", - efdcmpgt_2 = "100002ec-RR", - efdcmplt_3 = "100002edXRR", - efdcmplt_2 = "100002ed-RR", - efdcmpeq_3 = "100002eeXRR", - efdcmpeq_2 = "100002ee-RR", - efdcfs_2 = "100002efR-R", - efdcfui_2 = "100002f0R-R", - efdcfsi_2 = "100002f1R-R", - efdcfuf_2 = "100002f2R-R", - efdcfsf_2 = "100002f3R-R", - efdctui_2 = "100002f4R-R", - efdctsi_2 = "100002f5R-R", - efdctuf_2 = "100002f6R-R", - efdctsf_2 = "100002f7R-R", - efdctuiz_2 = "100002f8R-R", - efdctsiz_2 = "100002faR-R", - efdtstgt_3 = "100002fcXRR", - efdtstgt_2 = "100002fc-RR", - efdtstlt_3 = "100002fdXRR", - efdtstlt_2 = "100002fd-RR", - efdtsteq_3 = "100002feXRR", - efdtsteq_2 = "100002fe-RR", - evlddx_3 = "10000300RR0R", - evldd_2 = "10000301R8", - evldwx_3 = "10000302RR0R", - evldw_2 = "10000303R8", - evldhx_3 = "10000304RR0R", - evldh_2 = "10000305R8", - evlwhex_3 = "10000310RR0R", - evlwhe_2 = "10000311R4", - evlwhoux_3 = "10000314RR0R", - evlwhou_2 = "10000315R4", - evlwhosx_3 = "10000316RR0R", - evlwhos_2 = "10000317R4", - evstddx_3 = "10000320RR0R", - evstdd_2 = "10000321R8", - evstdwx_3 = "10000322RR0R", - evstdw_2 = "10000323R8", - evstdhx_3 = "10000324RR0R", - evstdh_2 = "10000325R8", - evstwhex_3 = "10000330RR0R", - evstwhe_2 = "10000331R4", - evstwhox_3 = "10000334RR0R", - evstwho_2 = "10000335R4", - evstwwex_3 = "10000338RR0R", - evstwwe_2 = "10000339R4", - evstwwox_3 = "1000033cRR0R", - evstwwo_2 = "1000033dR4", - evmhessf_3 = "10000403RRR", - evmhossf_3 = "10000407RRR", - evmheumi_3 = "10000408RRR", - evmhesmi_3 = "10000409RRR", - evmhesmf_3 = "1000040bRRR", - evmhoumi_3 = "1000040cRRR", - evmhosmi_3 = "1000040dRRR", - evmhosmf_3 = "1000040fRRR", - evmhessfa_3 = "10000423RRR", - evmhossfa_3 = "10000427RRR", - evmheumia_3 = "10000428RRR", - evmhesmia_3 = "10000429RRR", - evmhesmfa_3 = "1000042bRRR", - evmhoumia_3 = "1000042cRRR", - evmhosmia_3 = "1000042dRRR", - evmhosmfa_3 = "1000042fRRR", - evmwhssf_3 = "10000447RRR", - evmwlumi_3 = "10000448RRR", - evmwhumi_3 = "1000044cRRR", - evmwhsmi_3 = "1000044dRRR", - evmwhsmf_3 = "1000044fRRR", - evmwssf_3 = "10000453RRR", - evmwumi_3 = "10000458RRR", - evmwsmi_3 = "10000459RRR", - evmwsmf_3 = "1000045bRRR", - evmwhssfa_3 = "10000467RRR", - evmwlumia_3 = "10000468RRR", - evmwhumia_3 = "1000046cRRR", - evmwhsmia_3 = "1000046dRRR", - evmwhsmfa_3 = "1000046fRRR", - evmwssfa_3 = "10000473RRR", - evmwumia_3 = "10000478RRR", - evmwsmia_3 = "10000479RRR", - evmwsmfa_3 = "1000047bRRR", - evmra_2 = "100004c4RR", - evdivws_3 = "100004c6RRR", - evdivwu_3 = "100004c7RRR", - evmwssfaa_3 = "10000553RRR", - evmwumiaa_3 = "10000558RRR", - evmwsmiaa_3 = "10000559RRR", - evmwsmfaa_3 = "1000055bRRR", - evmwssfan_3 = "100005d3RRR", - evmwumian_3 = "100005d8RRR", - evmwsmian_3 = "100005d9RRR", - evmwsmfan_3 = "100005dbRRR", - evmergehilo_3 = "1000022eRRR", - evmergelohi_3 = "1000022fRRR", - evlhhesplatx_3 = "10000308RR0R", - evlhhesplat_2 = "10000309R2", - evlhhousplatx_3 = "1000030cRR0R", - evlhhousplat_2 = "1000030dR2", - evlhhossplatx_3 = "1000030eRR0R", - evlhhossplat_2 = "1000030fR2", - evlwwsplatx_3 = "10000318RR0R", - evlwwsplat_2 = "10000319R4", - evlwhsplatx_3 = "1000031cRR0R", - evlwhsplat_2 = "1000031dR4", - evaddusiaaw_2 = "100004c0RR", - evaddssiaaw_2 = "100004c1RR", - evsubfusiaaw_2 = "100004c2RR", - evsubfssiaaw_2 = "100004c3RR", - evaddumiaaw_2 = "100004c8RR", - evaddsmiaaw_2 = "100004c9RR", - evsubfumiaaw_2 = "100004caRR", - evsubfsmiaaw_2 = "100004cbRR", - evmheusiaaw_3 = "10000500RRR", - evmhessiaaw_3 = "10000501RRR", - evmhessfaaw_3 = "10000503RRR", - evmhousiaaw_3 = "10000504RRR", - evmhossiaaw_3 = "10000505RRR", - evmhossfaaw_3 = "10000507RRR", - evmheumiaaw_3 = "10000508RRR", - evmhesmiaaw_3 = "10000509RRR", - evmhesmfaaw_3 = "1000050bRRR", - evmhoumiaaw_3 = "1000050cRRR", - evmhosmiaaw_3 = "1000050dRRR", - evmhosmfaaw_3 = "1000050fRRR", - evmhegumiaa_3 = "10000528RRR", - evmhegsmiaa_3 = "10000529RRR", - evmhegsmfaa_3 = "1000052bRRR", - evmhogumiaa_3 = "1000052cRRR", - evmhogsmiaa_3 = "1000052dRRR", - evmhogsmfaa_3 = "1000052fRRR", - evmwlusiaaw_3 = "10000540RRR", - evmwlssiaaw_3 = "10000541RRR", - evmwlumiaaw_3 = "10000548RRR", - evmwlsmiaaw_3 = "10000549RRR", - evmheusianw_3 = "10000580RRR", - evmhessianw_3 = "10000581RRR", - evmhessfanw_3 = "10000583RRR", - evmhousianw_3 = "10000584RRR", - evmhossianw_3 = "10000585RRR", - evmhossfanw_3 = "10000587RRR", - evmheumianw_3 = "10000588RRR", - evmhesmianw_3 = "10000589RRR", - evmhesmfanw_3 = "1000058bRRR", - evmhoumianw_3 = "1000058cRRR", - evmhosmianw_3 = "1000058dRRR", - evmhosmfanw_3 = "1000058fRRR", - evmhegumian_3 = "100005a8RRR", - evmhegsmian_3 = "100005a9RRR", - evmhegsmfan_3 = "100005abRRR", - evmhogumian_3 = "100005acRRR", - evmhogsmian_3 = "100005adRRR", - evmhogsmfan_3 = "100005afRRR", - evmwlusianw_3 = "100005c0RRR", - evmwlssianw_3 = "100005c1RRR", - evmwlumianw_3 = "100005c8RRR", - evmwlsmianw_3 = "100005c9RRR", - - -- NYI: Book E instructions. -} - --- Add mnemonics for "." variants. -do - local t = {} - for k,v in pairs(map_op) do - if type(v) == "string" and sub(v, -1) == "." then - local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2) - t[sub(k, 1, -3).."."..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - --- Add more branch mnemonics. -for cond,c in pairs(map_cond) do - local b1 = "b"..cond - local c1 = shl(band(c, 3), 16) + (c < 4 and 0x01000000 or 0) - -- bX[l] - map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" - map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" - map_op[b1.."l_1"] = tohex(0x40800001 + c1).."K" - map_op[b1.."_2"] = tohex(0x40800000 + c1).."-XK" - map_op[b1.."y_2"] = tohex(0x40a00000 + c1).."-XK" - map_op[b1.."l_2"] = tohex(0x40800001 + c1).."-XK" - -- bXlr[l] - map_op[b1.."lr_0"] = tohex(0x4c800020 + c1) - map_op[b1.."lrl_0"] = tohex(0x4c800021 + c1) - map_op[b1.."ctr_0"] = tohex(0x4c800420 + c1) - map_op[b1.."ctrl_0"] = tohex(0x4c800421 + c1) - -- bXctr[l] - map_op[b1.."lr_1"] = tohex(0x4c800020 + c1).."-X" - map_op[b1.."lrl_1"] = tohex(0x4c800021 + c1).."-X" - map_op[b1.."ctr_1"] = tohex(0x4c800420 + c1).."-X" - map_op[b1.."ctrl_1"] = tohex(0x4c800421 + c1).."-X" -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_vr(expr) - local r = match(expr, "^v([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_vs(expr) - local r = match(expr, "^vs([1-6]?[0-9])$") - if r then - r = tonumber(r) - if r <= 63 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_cr(expr) - local r = match(expr, "^cr([0-7])$") - if r then return tonumber(r) end - werror("bad condition register name `"..expr.."'") -end - -local function parse_cond(expr) - local r, cond = match(expr, "^4%*cr([0-7])%+(%w%w)$") - if r then - r = tonumber(r) - local c = map_cond[cond] - if c and c < 4 then return r*4+c end - end - werror("bad condition bit name `"..expr.."'") -end - -local parse_ctx = {} - -local loadenv = setfenv and function(s) - local code = loadstring(s, "") - if code then setfenv(code, parse_ctx) end - return code -end or function(s) - return load(s, "", nil, parse_ctx) -end - --- Try to parse simple arithmetic, too, since some basic ops are aliases. -local function parse_number(n) - local x = tonumber(n) - if x then return x end - local code = loadenv("return "..n) - if code then - local ok, y = pcall(code) - if ok then return y end - end - return nil -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = parse_number(imm) - if n then - local m = sar(n, scale) - if shl(m, scale) == n then - if signed then - local s = sar(m, bits-1) - if s == 0 then return shl(m, shift) - elseif s == -1 then return shl(m + shl(1, bits), shift) end - else - if sar(m, bits) == 0 then return shl(m, shift) end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^[rfv]([1-3]?[0-9])$") or - match(imm, "^vs([1-6]?[0-9])$") or - match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_shiftmask(imm, isshift) - local n = parse_number(imm) - if n then - if shr(n, 6) == 0 then - local lsb = band(n, 31) - local msb = n - lsb - return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb) - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^r([1-3]?[0-9])$") or - match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMMSH", isshift and 1 or 0, imm) - return 0; - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return shl(r, 16) + parse_imm(imm, 16, 0, 0, true) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return shl(r, 16) - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_u5disp(disp, scale) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return shl(r, 16) + parse_imm(imm, 5, 11, scale, false) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) - return shl(r, 16) - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -op_template = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n, rs = 1, 26 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions (rlwinm). - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "R" then - rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1 - elseif p == "F" then - rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1 - elseif p == "V" then - rs = rs - 5; op = op + shl(parse_vr(params[n]), rs); n = n + 1 - elseif p == "Q" then - local vs = parse_vs(params[n]); n = n + 1; rs = rs - 5 - local sh = rs == 6 and 2 or 3 + band(shr(rs, 1), 3) - op = op + shl(band(vs, 31), rs) + shr(band(vs, 32), sh) - elseif p == "q" then - local vs = parse_vs(params[n]); n = n + 1 - op = op + shl(band(vs, 31), 21) + shr(band(vs, 32), 5) - elseif p == "A" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 - elseif p == "S" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, true); n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "D" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "2" then - op = op + parse_u5disp(params[n], 1); n = n + 1 - elseif p == "4" then - op = op + parse_u5disp(params[n], 2); n = n + 1 - elseif p == "8" then - op = op + parse_u5disp(params[n], 3); n = n + 1 - elseif p == "C" then - rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1 - elseif p == "X" then - rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1 - elseif p == "1" then - rs = rs - 5; op = op + parse_imm(params[n], 1, rs, 0, false); n = n + 1 - elseif p == "g" then - rs = rs - 5; op = op + parse_imm(params[n], 2, rs, 0, false); n = n + 1 - elseif p == "3" then - rs = rs - 5; op = op + parse_imm(params[n], 3, rs, 0, false); n = n + 1 - elseif p == "P" then - rs = rs - 5; op = op + parse_imm(params[n], 4, rs, 0, false); n = n + 1 - elseif p == "p" then - op = op + parse_imm(params[n], 4, rs, 0, false); n = n + 1 - elseif p == "6" then - rs = rs - 6; op = op + parse_imm(params[n], 6, rs, 0, false); n = n + 1 - elseif p == "Y" then - rs = rs - 5; op = op + parse_imm(params[n], 1, rs+4, 0, false); n = n + 1 - elseif p == "y" then - rs = rs - 5; op = op + parse_imm(params[n], 1, rs+3, 0, false); n = n + 1 - elseif p == "Z" then - rs = rs - 5; op = op + parse_imm(params[n], 2, rs+3, 0, false); n = n + 1 - elseif p == "z" then - rs = rs - 5; op = op + parse_imm(params[n], 2, rs+2, 0, false); n = n + 1 - elseif p == "W" then - op = op + parse_cr(params[n]); n = n + 1 - elseif p == "G" then - op = op + parse_imm(params[n], 8, 12, 0, false); n = n + 1 - elseif p == "H" then - op = op + parse_shiftmask(params[n], true); n = n + 1 - elseif p == "M" then - op = op + parse_shiftmask(params[n], false); n = n + 1 - elseif p == "J" or p == "K" then - local mode, m, s = parse_label(params[n], false) - if p == "K" then m = m + 2048 end - waction("REL_"..mode, m, s, 1) - n = n + 1 - elseif p == "0" then - if band(shr(op, rs), 31) == 0 then werror("cannot use r0") end - elseif p == "=" or p == "%" then - local t = band(shr(op, p == "%" and rs+5 or rs), 31) - rs = rs - 5 - op = op + shl(t, rs) - elseif p == "~" then - local mm = shl(31, rs) - local lo = band(op, mm) - local hi = band(op, shl(mm, 5)) - op = op - lo - hi + shl(lo, 5) + shr(hi, 5) - elseif p == ":" then - if band(shr(op, rs), 1) ~= 0 then werror("register pair expected") end - elseif p == "-" then - rs = rs - 5 - elseif p == "." then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - -map_op[".template__"] = op_template - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_proto.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_proto.h deleted file mode 100644 index d798554..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_proto.h +++ /dev/null @@ -1,83 +0,0 @@ -/* -** DynASM encoding engine prototypes. -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#ifndef _DASM_PROTO_H -#define _DASM_PROTO_H - -#include -#include - -#define DASM_IDENT "DynASM 1.5.0" -#define DASM_VERSION 10500 /* 1.5.0 */ - -#ifndef Dst_DECL -#define Dst_DECL dasm_State **Dst -#endif - -#ifndef Dst_REF -#define Dst_REF (*Dst) -#endif - -#ifndef DASM_FDEF -#define DASM_FDEF extern -#endif - -#ifndef DASM_M_GROW -#define DASM_M_GROW(ctx, t, p, sz, need) \ - do { \ - size_t _sz = (sz), _need = (need); \ - if (_sz < _need) { \ - if (_sz < 16) _sz = 16; \ - while (_sz < _need) _sz += _sz; \ - (p) = (t *)realloc((p), _sz); \ - if ((p) == NULL) exit(1); \ - (sz) = _sz; \ - } \ - } while(0) -#endif - -#ifndef DASM_M_FREE -#define DASM_M_FREE(ctx, p, sz) free(p) -#endif - -/* Internal DynASM encoder state. */ -typedef struct dasm_State dasm_State; - - -/* Initialize and free DynASM state. */ -DASM_FDEF void dasm_init(Dst_DECL, int maxsection); -DASM_FDEF void dasm_free(Dst_DECL); - -/* Setup global array. Must be called before dasm_setup(). */ -DASM_FDEF void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl); - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -DASM_FDEF void dasm_growpc(Dst_DECL, unsigned int maxpc); - -/* Setup encoder. */ -DASM_FDEF void dasm_setup(Dst_DECL, const void *actionlist); - -/* Feed encoder with actions. Calls are generated by pre-processor. */ -DASM_FDEF void dasm_put(Dst_DECL, int start, ...); - -/* Link sections and return the resulting size. */ -DASM_FDEF int dasm_link(Dst_DECL, size_t *szp); - -/* Encode sections into buffer. */ -DASM_FDEF int dasm_encode(Dst_DECL, void *buffer); - -/* Get PC label offset. */ -DASM_FDEF int dasm_getpclabel(Dst_DECL, unsigned int pc); - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -DASM_FDEF int dasm_checkstep(Dst_DECL, int secmatch); -#else -#define dasm_checkstep(a, b) 0 -#endif - - -#endif /* _DASM_PROTO_H */ diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x64.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x64.lua deleted file mode 100644 index 1779c5b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x64.lua +++ /dev/null @@ -1,12 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x64 module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- --- This module just sets 64 bit mode for the combined x86/x64 module. --- All the interesting stuff is there. ------------------------------------------------------------------------------- - -x64 = true -- Using a global is an ugly, but effective solution. -return require("dasm_x86") diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.h deleted file mode 100644 index f032730..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.h +++ /dev/null @@ -1,528 +0,0 @@ -/* -** DynASM x86 encoding engine. -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "x86" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. DASM_STOP must be 255. */ -enum { - DASM_DISP = 233, - DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB, - DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC, - DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN, - DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_VREG 0x15000000 -#define DASM_S_UNDEF_L 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned char *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs, mrm = -1; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - int action = *p++; - if (action < DASM_DISP) { - ofs++; - } else if (action <= DASM_REL_A) { - int n = va_arg(ap, int); - b[pos++] = n; - switch (action) { - case DASM_DISP: - if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; } - /* fallthrough */ - case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */ - case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */ - case DASM_IMM_D: ofs += 4; break; - case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob; - case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */ - case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break; - case DASM_SPACE: p++; ofs += n; break; - case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */ - case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG); - if (*p < 0x40 && p[1] == DASM_DISP) mrm = n; - if (*p < 0x20 && (n&7) == 4) ofs++; - switch ((*p++ >> 3) & 3) { - case 3: n |= b[pos-3]; /* fallthrough */ - case 2: n |= b[pos-2]; /* fallthrough */ - case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; } - } - continue; - } - mrm = -1; - } else { - int *pl, n; - switch (action) { - case DASM_REL_LG: - case DASM_IMM_LG: - n = *p++; pl = D->lglabels + n; - /* Bkwd rel or global. */ - if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; } - pl -= 246; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - ofs += 4; /* Maximum offset needed. */ - if (action == DASM_REL_LG || action == DASM_REL_PC) { - b[pos++] = ofs; /* Store pass1 offset estimate. */ - } else if (sizeof(ptrdiff_t) == 8) { - ofs += 4; - } - break; - case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_ALIGN: - ofs += *p++; /* Maximum alignment needed (arg is 2**n-1). */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_EXTERN: p += 2; ofs += 4; break; - case DASM_ESC: p++; ofs++; break; - case DASM_MARK: mrm = p[-2]; break; - case DASM_SECTION: - n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n]; - case DASM_STOP: goto stop; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - int op = 0; - while (1) { - int action = *p++; - switch (action) { - case DASM_REL_LG: p++; - /* fallthrough */ - case DASM_REL_PC: { - int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0); - if (shrink) { /* Shrinkable branch opcode? */ - int lofs, lpos = b[pos]; - if (lpos < 0) goto noshrink; /* Ext global? */ - lofs = *DASM_POS2PTR(D, lpos); - if (lpos > pos) { /* Fwd label: add cumulative section offsets. */ - int i; - for (i = secnum; i < DASM_POS2SEC(lpos); i++) - lofs += D->sections[i].ofs; - } else { - lofs -= ofs; /* Bkwd label: unfix offset. */ - } - lofs -= b[pos+1]; /* Short branch ok? */ - if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink; /* Yes. */ - else { noshrink: shrink = 0; } /* No, cannot shrink op. */ - } - b[pos+1] = shrink; - pos += 2; - break; - } - /* fallthrough */ - case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++; - /* fallthrough */ - case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W: - case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB: - case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break; - case DASM_LABEL_LG: p++; - /* fallthrough */ - case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */ - case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */ - case DASM_EXTERN: p += 2; break; - case DASM_ESC: op = *p++; break; - case DASM_MARK: break; - case DASM_SECTION: case DASM_STOP: goto stop; - default: op = action; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#define dasmb(x) *cp++ = (unsigned char)(x) -#ifndef DASM_ALIGNED_WRITES -#define dasmw(x) \ - do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0) -#define dasmd(x) \ - do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0) -#define dasmq(x) \ - do { *((unsigned long long *)cp) = (unsigned long long)(x); cp+=8; } while (0) -#else -#define dasmw(x) do { dasmb(x); dasmb((x)>>8); } while (0) -#define dasmd(x) do { dasmw(x); dasmw((x)>>16); } while (0) -#define dasmq(x) do { dasmd(x); dasmd((x)>>32); } while (0) -#endif -static unsigned char *dasma_(unsigned char *cp, ptrdiff_t x) -{ - if (sizeof(ptrdiff_t) == 8) - dasmq((unsigned long long)x); - else - dasmd((unsigned int)x); - return cp; -} -#define dasma(x) (cp = dasma_(cp, (x))) - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - unsigned char *base = (unsigned char *)buffer; - unsigned char *cp = base; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - unsigned char *mark = NULL; - while (1) { - int action = *p++; - int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0; - switch (action) { - case DASM_DISP: if (!mark) mark = cp; { - unsigned char *mm = mark; - if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL; - if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7; - if (mrm != 5) { mm[-1] -= 0x80; break; } } - if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40; - } - /* fallthrough */ - case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break; - case DASM_IMM_DB: if (((n+128)&-256) == 0) { - db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb; - } else mark = NULL; - /* fallthrough */ - case DASM_IMM_D: wd: dasmd(n); break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL; - /* fallthrough */ - case DASM_IMM_W: dasmw(n); break; - case DASM_VREG: { - int t = *p++; - unsigned char *ex = cp - (t&7); - if ((n & 8) && t < 0xa0) { - if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6); - n &= 7; - } else if (n & 0x10) { - if (*ex & 0x80) { - *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2; - } - while (++ex < cp) ex[-1] = *ex; - if (mark) mark--; - cp--; - n &= 7; - } - if (t >= 0xc0) n <<= 4; - else if (t >= 0x40) n <<= 3; - else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; } - cp[-1] ^= n; - break; - } - case DASM_REL_LG: p++; if (n >= 0) goto rel_pc; - b++; n = (int)(ptrdiff_t)D->globals[-n]; - /* fallthrough */ - case DASM_REL_A: rel_a: - n -= (unsigned int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */ - case DASM_REL_PC: rel_pc: { - int shrink = *b++; - int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; } - n = *pb - ((int)(cp-base) + 4-shrink); - if (shrink == 0) goto wd; - if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb; - goto wb; - } - case DASM_IMM_LG: - p++; - if (n < 0) { dasma((ptrdiff_t)D->globals[-n]); break; } - /* fallthrough */ - case DASM_IMM_PC: { - int *pb = DASM_POS2PTR(D, n); - dasma(*pb < 0 ? (ptrdiff_t)pb[1] : (*pb + (ptrdiff_t)base)); - break; - } - case DASM_LABEL_LG: { - int idx = *p++; - if (idx >= 10) - D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n)); - break; - } - case DASM_LABEL_PC: case DASM_SETLABEL: break; - case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; } - case DASM_ALIGN: - n = *p++; - while (((cp-base) & n)) *cp++ = 0x90; /* nop */ - break; - case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd; - case DASM_MARK: mark = cp; break; - case DASM_ESC: action = *p++; - /* fallthrough */ - default: *cp++ = action; break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); - return D->status; -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.lua deleted file mode 100644 index b442cd0..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dasm_x86.lua +++ /dev/null @@ -1,2388 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x86/x64 module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - -local x64 = x64 - --- Module information: -local _info = { - arch = x64 and "x64" or "x86", - description = "DynASM x86/x64 module", - version = "1.5.0", - vernum = 10500, - release = "2021-05-02", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub -local concat, sort, remove = table.concat, table.sort, table.remove -local bit = bit or require("bit") -local band, bxor, shl, shr = bit.band, bit.bxor, bit.lshift, bit.rshift - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - -- int arg, 1 buffer pos: - "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB", - -- action arg (1 byte), int arg, 1 buffer pos (reg/num): - "VREG", "SPACE", - -- ptrdiff_t arg, 1 buffer pos (address): !x64 - "SETLABEL", "REL_A", - -- action arg (1 byte) or int arg, 2 buffer pos (link, offset): - "REL_LG", "REL_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (link): - "IMM_LG", "IMM_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (offset): - "LABEL_LG", "LABEL_PC", - -- action arg (1 byte), 1 buffer pos (offset): - "ALIGN", - -- action args (2 bytes), no buffer pos. - "EXTERN", - -- action arg (1 byte), no buffer pos. - "ESC", - -- no action arg, no buffer pos. - "MARK", - -- action arg (1 byte), no buffer pos, terminal action: - "SECTION", - -- no args, no buffer pos, terminal action: - "STOP" -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number (dynamically generated below). -local map_action = {} --- First action number. Everything below does not need to be escaped. -local actfirst = 256-#action_names - --- Action list buffer and string (only used to remove dupes). -local actlist = {} -local actstr = "" - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - --- VREG kind encodings, pre-shifted by 5 bits. -local map_vreg = { - ["modrm.rm.m"] = 0x00, - ["modrm.rm.r"] = 0x20, - ["opcode"] = 0x20, - ["sib.base"] = 0x20, - ["sib.index"] = 0x40, - ["modrm.reg"] = 0x80, - ["vex.v"] = 0xa0, - ["imm.hi"] = 0xc0, -} - --- Current number of VREG actions contributing to REX/VEX shrinkage. -local vreg_shrink_count = 0 - ------------------------------------------------------------------------------- - --- Compute action numbers for action names. -for n,name in ipairs(action_names) do - local num = actfirst + n - 1 - map_action[name] = num -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - local last = actlist[nn] or 255 - actlist[nn] = nil -- Remove last byte. - if nn == 0 then nn = 1 end - out:write("static const unsigned char ", name, "[", nn, "] = {\n") - local s = " " - for n,b in ipairs(actlist) do - s = s..b.."," - if #s >= 75 then - assert(out:write(s, "\n")) - s = " " - end - end - out:write(s, last, "\n};\n\n") -- Add last byte back. -end - ------------------------------------------------------------------------------- - --- Add byte to action list. -local function wputxb(n) - assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, a, num) - wputxb(assert(map_action[action], "bad action name `"..action.."'")) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Optionally add a VREG action. -local function wvreg(kind, vreg, psz, sk, defer) - if not vreg then return end - waction("VREG", vreg) - local b = assert(map_vreg[kind], "bad vreg kind `"..vreg.."'") - if b < (sk or 0) then - vreg_shrink_count = vreg_shrink_count + 1 - end - if not defer then - b = b + vreg_shrink_count * 8 - vreg_shrink_count = 0 - end - wputxb(b + (psz or 0)) -end - --- Add call to embedded DynASM C code. -local function wcall(func, args) - wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true) -end - --- Delete duplicate action list chunks. A tad slow, but so what. -local function dedupechunk(offset) - local al, as = actlist, actstr - local chunk = char(unpack(al, offset+1, #al)) - local orig = find(as, chunk, 1, true) - if orig then - actargs[1] = orig-1 -- Replace with original offset. - for i=offset+1,#al do al[i] = nil end -- Kill dupe. - else - actstr = as..chunk - end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - local offset = actargs[1] - if #actlist == offset then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - dedupechunk(offset) - wcall("put", actargs) -- Add call to dasm_put(). - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped byte. -local function wputb(n) - if n >= actfirst then waction("ESC") end -- Need to escape byte. - wputxb(n) -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 10 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end - local n = next_global - if n > 246 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=10,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=10,next_global-1 do - out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=10,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = -1 -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n < -256 then werror("too many extern labels") end - next_extern = n - 1 - t[name] = n - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("Extern labels:\n") - for i=1,-next_extern-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=1,-next_extern-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = {} -- Ext. register name -> int. name. -local map_reg_rev = {} -- Int. register name -> ext. name. -local map_reg_num = {} -- Int. register name -> register number. -local map_reg_opsize = {} -- Int. register name -> operand size. -local map_reg_valid_base = {} -- Int. register name -> valid base register? -local map_reg_valid_index = {} -- Int. register name -> valid index register? -local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex. -local reg_list = {} -- Canonical list of int. register names. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for _PTx macros). - -local addrsize = x64 and "q" or "d" -- Size for address operands. - --- Helper functions to fill register maps. -local function mkrmap(sz, cl, names) - local cname = format("@%s", sz) - reg_list[#reg_list+1] = cname - map_archdef[cl] = cname - map_reg_rev[cname] = cl - map_reg_num[cname] = -1 - map_reg_opsize[cname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[cname] = true - map_reg_valid_index[cname] = true - end - if names then - for n,name in ipairs(names) do - local iname = format("@%s%x", sz, n-1) - reg_list[#reg_list+1] = iname - map_archdef[name] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = n-1 - map_reg_opsize[iname] = sz - if sz == "b" and n > 4 then map_reg_needrex[iname] = false end - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - for i=0,(x64 and sz ~= "f") and 15 or 7 do - local needrex = sz == "b" and i > 3 - local iname = format("@%s%x%s", sz, i, needrex and "R" or "") - if needrex then map_reg_needrex[iname] = true end - local name - if sz == "o" or sz == "y" then name = format("%s%d", cl, i) - elseif sz == "f" then name = format("st%d", i) - else name = format("r%d%s", i, sz == addrsize and "" or sz) end - map_archdef[name] = iname - if not map_reg_rev[iname] then - reg_list[#reg_list+1] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = i - map_reg_opsize[iname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - reg_list[#reg_list+1] = "" -end - --- Integer registers (qword, dword, word and byte sized). -if x64 then - mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"}) -end -mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}) -mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"}) -mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}) -map_reg_valid_index[map_archdef.esp] = false -if x64 then map_reg_valid_index[map_archdef.rsp] = false end -if x64 then map_reg_needrex[map_archdef.Rb] = true end -map_archdef["Ra"] = "@"..addrsize - --- FP registers (internally tword sized, but use "f" as operand size). -mkrmap("f", "Rf") - --- SSE registers (oword sized, but qword and dword accessible). -mkrmap("o", "xmm") - --- AVX registers (yword sized, but oword, qword and dword accessible). -mkrmap("y", "ymm") - --- Operand size prefixes to codes. -local map_opsize = { - byte = "b", word = "w", dword = "d", qword = "q", oword = "o", yword = "y", - tword = "t", aword = addrsize, -} - --- Operand size code to number. -local map_opsizenum = { - b = 1, w = 2, d = 4, q = 8, o = 16, y = 32, t = 10, -} - --- Operand size code to name. -local map_opsizename = { - b = "byte", w = "word", d = "dword", q = "qword", o = "oword", y = "yword", - t = "tword", f = "fpword", -} - --- Valid index register scale factors. -local map_xsc = { - ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3, -} - --- Condition codes. -local map_cc = { - o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7, - s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15, - c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7, - pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15, -} - - --- Reverse defines for registers. -function _M.revdef(s) - return gsub(s, "@%w+", map_reg_rev) -end - --- Dump register names and numbers -local function dumpregs(out) - out:write("Register names, sizes and internal numbers:\n") - for _,reg in ipairs(reg_list) do - if reg == "" then - out:write("\n") - else - local name = map_reg_rev[reg] - local num = map_reg_num[reg] - local opsize = map_opsizename[map_reg_opsize[reg]] - out:write(format(" %-5s %-8s %s\n", name, opsize, - num < 0 and "(variable)" or num)) - end - end -end - ------------------------------------------------------------------------------- - --- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC). -local function wputlabel(aprefix, imm, num) - if type(imm) == "number" then - if imm < 0 then - waction("EXTERN") - wputxb(aprefix == "IMM_" and 0 or 1) - imm = -imm-1 - else - waction(aprefix.."LG", nil, num); - end - wputxb(imm) - else - waction(aprefix.."PC", imm, num) - end -end - --- Put signed byte or arg. -local function wputsbarg(n) - if type(n) == "number" then - if n < -128 or n > 127 then - werror("signed immediate byte out of range") - end - if n < 0 then n = n + 256 end - wputb(n) - else waction("IMM_S", n) end -end - --- Put unsigned byte or arg. -local function wputbarg(n) - if type(n) == "number" then - if n < 0 or n > 255 then - werror("unsigned immediate byte out of range") - end - wputb(n) - else waction("IMM_B", n) end -end - --- Put unsigned word or arg. -local function wputwarg(n) - if type(n) == "number" then - if shr(n, 16) ~= 0 then - werror("unsigned immediate word out of range") - end - wputb(band(n, 255)); wputb(shr(n, 8)); - else waction("IMM_W", n) end -end - --- Put signed or unsigned dword or arg. -local function wputdarg(n) - local tn = type(n) - if tn == "number" then - wputb(band(n, 255)) - wputb(band(shr(n, 8), 255)) - wputb(band(shr(n, 16), 255)) - wputb(shr(n, 24)) - elseif tn == "table" then - wputlabel("IMM_", n[1], 1) - else - waction("IMM_D", n) - end -end - --- Put signed or unsigned qword or arg. -local function wputqarg(n) - local tn = type(n) - if tn == "number" then -- This is only used for numbers from -2^31..2^32-1. - wputb(band(n, 255)) - wputb(band(shr(n, 8), 255)) - wputb(band(shr(n, 16), 255)) - wputb(shr(n, 24)) - local sign = n < 0 and 255 or 0 - wputb(sign); wputb(sign); wputb(sign); wputb(sign) - else - waction("IMM_D", format("(unsigned int)(%s)", n)) - waction("IMM_D", format("(unsigned int)((unsigned long long)(%s)>>32)", n)) - end -end - --- Put operand-size dependent number or arg (defaults to dword). -local function wputszarg(sz, n) - if not sz or sz == "d" or sz == "q" then wputdarg(n) - elseif sz == "w" then wputwarg(n) - elseif sz == "b" then wputbarg(n) - elseif sz == "s" then wputsbarg(n) - else werror("bad operand size") end -end - --- Put multi-byte opcode with operand-size dependent modifications. -local function wputop(sz, op, rex, vex, vregr, vregxb) - local psz, sk = 0, nil - if vex then - local tail - if vex.m == 1 and band(rex, 11) == 0 then - if x64 and vregxb then - sk = map_vreg["modrm.reg"] - else - wputb(0xc5) - tail = shl(bxor(band(rex, 4), 4), 5) - psz = 3 - end - end - if not tail then - wputb(0xc4) - wputb(shl(bxor(band(rex, 7), 7), 5) + vex.m) - tail = shl(band(rex, 8), 4) - psz = 4 - end - local reg, vreg = 0, nil - if vex.v then - reg = vex.v.reg - if not reg then werror("bad vex operand") end - if reg < 0 then reg = 0; vreg = vex.v.vreg end - end - if sz == "y" or vex.l then tail = tail + 4 end - wputb(tail + shl(bxor(reg, 15), 3) + vex.p) - wvreg("vex.v", vreg) - rex = 0 - if op >= 256 then werror("bad vex opcode") end - else - if rex ~= 0 then - if not x64 then werror("bad operand size") end - elseif (vregr or vregxb) and x64 then - rex = 0x10 - sk = map_vreg["vex.v"] - end - end - local r - if sz == "w" then wputb(102) end - -- Needs >32 bit numbers, but only for crc32 eax, word [ebx] - if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end - if op >= 16777216 then wputb(shr(op, 24)); op = band(op, 0xffffff) end - if op >= 65536 then - if rex ~= 0 then - local opc3 = band(op, 0xffff00) - if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then - wputb(64 + band(rex, 15)); rex = 0; psz = 2 - end - end - wputb(shr(op, 16)); op = band(op, 0xffff); psz = psz + 1 - end - if op >= 256 then - local b = shr(op, 8) - if b == 15 and rex ~= 0 then wputb(64 + band(rex, 15)); rex = 0; psz = 2 end - wputb(b); op = band(op, 255); psz = psz + 1 - end - if rex ~= 0 then wputb(64 + band(rex, 15)); psz = 2 end - if sz == "b" then op = op - 1 end - wputb(op) - return psz, sk -end - --- Put ModRM or SIB formatted byte. -local function wputmodrm(m, s, rm, vs, vrm) - assert(m < 4 and s < 16 and rm < 16, "bad modrm operands") - wputb(shl(m, 6) + shl(band(s, 7), 3) + band(rm, 7)) -end - --- Put ModRM/SIB plus optional displacement. -local function wputmrmsib(t, imark, s, vsreg, psz, sk) - local vreg, vxreg - local reg, xreg = t.reg, t.xreg - if reg and reg < 0 then reg = 0; vreg = t.vreg end - if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end - if s < 0 then s = 0 end - - -- Register mode. - if sub(t.mode, 1, 1) == "r" then - wputmodrm(3, s, reg) - wvreg("modrm.reg", vsreg, psz+1, sk, vreg) - wvreg("modrm.rm.r", vreg, psz+1, sk) - return - end - - local disp = t.disp - local tdisp = type(disp) - -- No base register? - if not reg then - local riprel = false - if xreg then - -- Indexed mode with index register only. - -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp) - wputmodrm(0, s, 4) - if imark == "I" then waction("MARK") end - wvreg("modrm.reg", vsreg, psz+1, sk, vxreg) - wputmodrm(t.xsc, xreg, 5) - wvreg("sib.index", vxreg, psz+2, sk) - else - -- Pure 32 bit displacement. - if x64 and tdisp ~= "table" then - wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp) - wvreg("modrm.reg", vsreg, psz+1, sk) - if imark == "I" then waction("MARK") end - wputmodrm(0, 4, 5) - else - riprel = x64 - wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp) - wvreg("modrm.reg", vsreg, psz+1, sk) - if imark == "I" then waction("MARK") end - end - end - if riprel then -- Emit rip-relative displacement. - if match("UWSiI", imark) then - werror("NYI: rip-relative displacement followed by immediate") - end - -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f. - wputlabel("REL_", disp[1], 2) - else - wputdarg(disp) - end - return - end - - local m - if tdisp == "number" then -- Check displacement size at assembly time. - if disp == 0 and band(reg, 7) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too) - if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0] - elseif disp >= -128 and disp <= 127 then m = 1 - else m = 2 end - elseif tdisp == "table" then - m = 2 - end - - -- Index register present or esp as base register: need SIB encoding. - if xreg or band(reg, 7) == 4 then - wputmodrm(m or 2, s, 4) -- ModRM. - if m == nil or imark == "I" then waction("MARK") end - wvreg("modrm.reg", vsreg, psz+1, sk, vxreg or vreg) - wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB. - wvreg("sib.index", vxreg, psz+2, sk, vreg) - wvreg("sib.base", vreg, psz+2, sk) - else - wputmodrm(m or 2, s, reg) -- ModRM. - if (imark == "I" and (m == 1 or m == 2)) or - (m == nil and (vsreg or vreg)) then waction("MARK") end - wvreg("modrm.reg", vsreg, psz+1, sk, vreg) - wvreg("modrm.rm.m", vreg, psz+1, sk) - end - - -- Put displacement. - if m == 1 then wputsbarg(disp) - elseif m == 2 then wputdarg(disp) - elseif m == nil then waction("DISP", disp) end -end - ------------------------------------------------------------------------------- - --- Return human-readable operand mode string. -local function opmodestr(op, args) - local m = {} - for i=1,#args do - local a = args[i] - m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?") - end - return op.." "..concat(m, ",") -end - --- Convert number to valid integer or nil. -local function toint(expr, isqword) - local n = tonumber(expr) - if n then - if n % 1 ~= 0 then - werror("not an integer number `"..expr.."'") - elseif isqword then - if n < -2147483648 or n > 2147483647 then - n = nil -- Handle it as an expression to avoid precision loss. - end - elseif n < -2147483648 or n > 4294967295 then - werror("bad integer number `"..expr.."'") - end - return n - end -end - --- Parse immediate expression. -local function immexpr(expr) - -- &expr (pointer) - if sub(expr, 1, 1) == "&" then - return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2)) - end - - local prefix = sub(expr, 1, 2) - -- =>expr (pc label reference) - if prefix == "=>" then - return "iJ", sub(expr, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "iJ", map_global[sub(expr, 3)] - end - - -- [<>][1-9] (local label reference) - local dir, lnum = match(expr, "^([<>])([1-9])$") - if dir then -- Fwd: 247-255, Bkwd: 1-9. - return "iJ", lnum + (dir == ">" and 246 or 0) - end - - local extname = match(expr, "^extern%s+(%S+)$") - if extname then - return "iJ", map_extern[extname] - end - - -- expr (interpreted as immediate) - return "iI", expr -end - --- Parse displacement expression: +-num, +-expr, +-opsize*num -local function dispexpr(expr) - local disp = expr == "" and 0 or toint(expr) - if disp then return disp end - local c, dispt = match(expr, "^([+-])%s*(.+)$") - if c == "+" then - expr = dispt - elseif not c then - werror("bad displacement expression `"..expr.."'") - end - local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$") - local ops, imm = map_opsize[opsize], toint(tailops) - if ops and imm then - if c == "-" then imm = -imm end - return imm*map_opsizenum[ops] - end - local mode, iexpr = immexpr(dispt) - if mode == "iJ" then - if c == "-" then werror("cannot invert label reference") end - return { iexpr } - end - return expr -- Need to return original signed expression. -end - --- Parse register or type expression. -local function rtexpr(expr) - if not expr then return end - local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - local rnum = map_reg_num[reg] - if not rnum then - werror("type `"..(tname or expr).."' needs a register override") - end - if not map_reg_valid_base[reg] then - werror("bad base register override `"..(map_reg_rev[reg] or reg).."'") - end - return reg, rnum, tp - end - return expr, map_reg_num[expr] -end - --- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }. -local function parseoperand(param, isqword) - local t = {} - - local expr = param - local opsize, tailops = match(param, "^(%w+)%s*(.+)$") - if opsize then - t.opsize = map_opsize[opsize] - if t.opsize then expr = tailops end - end - - local br = match(expr, "^%[%s*(.-)%s*%]$") - repeat - if br then - t.mode = "xm" - - -- [disp] - t.disp = toint(br) - if t.disp then - t.mode = x64 and "xm" or "xmO" - break - end - - -- [reg...] - local tp - local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if not t.reg then - -- [expr] - t.mode = x64 and "xm" or "xmO" - t.disp = dispexpr("+"..br) - break - end - - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - - -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr] - local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$") - if xsc then - if not map_reg_valid_index[reg] then - werror("bad index register `"..map_reg_rev[reg].."'") - end - t.xsc = map_xsc[xsc] - t.xreg = t.reg - t.vxreg = t.vreg - t.reg = nil - t.vreg = nil - t.disp = dispexpr(tailsc) - break - end - if not map_reg_valid_base[reg] then - werror("bad base register `"..map_reg_rev[reg].."'") - end - - -- [reg] or [reg+-disp] - t.disp = toint(tailr) or (tailr == "" and 0) - if t.disp then break end - - -- [reg+xreg...] - local xreg, tailx = match(tailr, "^%+%s*([@%w_:]+)%s*(.*)$") - xreg, t.xreg, tp = rtexpr(xreg) - if not t.xreg then - -- [reg+-expr] - t.disp = dispexpr(tailr) - break - end - if not map_reg_valid_index[xreg] then - werror("bad index register `"..map_reg_rev[xreg].."'") - end - - if t.xreg == -1 then - t.vxreg, tailx = match(tailx, "^(%b())(.*)$") - if not t.vxreg then werror("bad variable register expression") end - end - - -- [reg+xreg*xsc...] - local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$") - if xsc then - t.xsc = map_xsc[xsc] - tailx = tailsc - end - - -- [...] or [...+-disp] or [...+-expr] - t.disp = dispexpr(tailx) - else - -- imm or opsize*imm - local imm = toint(expr, isqword) - if not imm and sub(expr, 1, 1) == "*" and t.opsize then - imm = toint(sub(expr, 2)) - if imm then - imm = imm * map_opsizenum[t.opsize] - t.opsize = nil - end - end - if imm then - if t.opsize then werror("bad operand size override") end - local m = "i" - if imm == 1 then m = m.."1" end - if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end - if imm >= -128 and imm <= 127 then m = m.."S" end - t.imm = imm - t.mode = m - break - end - - local tp - local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if t.reg then - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - -- reg - if tailr == "" then - if t.opsize then werror("bad operand size override") end - t.opsize = map_reg_opsize[reg] - if t.opsize == "f" then - t.mode = t.reg == 0 and "fF" or "f" - else - if reg == "@w4" or (x64 and reg == "@d4") then - wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'")) - end - t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm") - end - t.needrex = map_reg_needrex[reg] - break - end - - -- type[idx], type[idx].field, type->field -> [reg+offset_expr] - if not tp then werror("bad operand `"..param.."'") end - t.mode = "xm" - t.disp = format(tp.ctypefmt, tailr) - else - t.mode, t.imm = immexpr(expr) - if sub(t.mode, -1) == "J" then - if t.opsize and t.opsize ~= addrsize then - werror("bad operand size override") - end - t.opsize = addrsize - end - end - end - until true - return t -end - ------------------------------------------------------------------------------- --- x86 Template String Description --- =============================== --- --- Each template string is a list of [match:]pattern pairs, --- separated by "|". The first match wins. No match means a --- bad or unsupported combination of operand modes or sizes. --- --- The match part and the ":" is omitted if the operation has --- no operands. Otherwise the first N characters are matched --- against the mode strings of each of the N operands. --- --- The mode string for each operand type is (see parseoperand()): --- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl --- FP register: "f", +"F" for st0 --- Index operand: "xm", +"O" for [disp] (pure offset) --- Immediate: "i", +"S" for signed 8 bit, +"1" for 1, --- +"I" for arg, +"P" for pointer --- Any: +"J" for valid jump targets --- --- So a match character "m" (mixed) matches both an integer register --- and an index operand (to be encoded with the ModRM/SIB scheme). --- But "r" matches only a register and "x" only an index operand --- (e.g. for FP memory access operations). --- --- The operand size match string starts right after the mode match --- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty. --- The effective data size of the operation is matched against this list. --- --- If only the regular "b", "w", "d", "q", "t" operand sizes are --- present, then all operands must be the same size. Unspecified sizes --- are ignored, but at least one operand must have a size or the pattern --- won't match (use the "byte", "word", "dword", "qword", "tword" --- operand size overrides. E.g.: mov dword [eax], 1). --- --- If the list has a "1" or "2" prefix, the operand size is taken --- from the respective operand and any other operand sizes are ignored. --- If the list contains only ".", all operand sizes are ignored. --- If the list has a "/" prefix, the concatenated (mixed) operand sizes --- are compared to the match. --- --- E.g. "rrdw" matches for either two dword registers or two word --- registers. "Fx2dq" matches an st0 operand plus an index operand --- pointing to a dword (float) or qword (double). --- --- Every character after the ":" is part of the pattern string: --- Hex chars are accumulated to form the opcode (left to right). --- "n" disables the standard opcode mods --- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q") --- "X" Force REX.W. --- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode. --- "m"/"M" generates ModRM/SIB from the 1st/2nd operand. --- The spare 3 bits are either filled with the last hex digit or --- the result from a previous "r"/"R". The opcode is restored. --- "u" Use VEX encoding, vvvv unused. --- "v"/"V" Use VEX encoding, vvvv from 1st/2nd operand (the operand is --- removed from the list used by future characters). --- "w" Use VEX encoding, vvvv from 3rd operand. --- "L" Force VEX.L --- --- All of the following characters force a flush of the opcode: --- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand. --- "s" stores a 4 bit immediate from the last register operand, --- followed by 4 zero bits. --- "S" stores a signed 8 bit immediate from the last operand. --- "U" stores an unsigned 8 bit immediate from the last operand. --- "W" stores an unsigned 16 bit immediate from the last operand. --- "i" stores an operand sized immediate from the last operand. --- "I" dito, but generates an action code to optionally modify --- the opcode (+2) for a signed 8 bit immediate. --- "J" generates one of the REL action codes from the last operand. --- ------------------------------------------------------------------------------- - --- Template strings for x86 instructions. Ordered by first opcode byte. --- Unimplemented opcodes (deliberate omissions) are marked with *. -local map_op = { - -- 00-05: add... - -- 06: *push es - -- 07: *pop es - -- 08-0D: or... - -- 0E: *push cs - -- 0F: two byte opcode prefix - -- 10-15: adc... - -- 16: *push ss - -- 17: *pop ss - -- 18-1D: sbb... - -- 1E: *push ds - -- 1F: *pop ds - -- 20-25: and... - es_0 = "26", - -- 27: *daa - -- 28-2D: sub... - cs_0 = "2E", - -- 2F: *das - -- 30-35: xor... - ss_0 = "36", - -- 37: *aaa - -- 38-3D: cmp... - ds_0 = "3E", - -- 3F: *aas - inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m", - dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m", - push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or - "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i", - pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m", - -- 60: *pusha, *pushad, *pushaw - -- 61: *popa, *popad, *popaw - -- 62: *bound rdw,x - -- 63: x86: *arpl mw,rw - movsxd_2 = x64 and "rm/qd:63rM", - fs_0 = "64", - gs_0 = "65", - o16_0 = "66", - a16_0 = not x64 and "67" or nil, - a32_0 = x64 and "67", - -- 68: push idw - -- 69: imul rdw,mdw,idw - -- 6A: push ib - -- 6B: imul rdw,mdw,S - -- 6C: *insb - -- 6D: *insd, *insw - -- 6E: *outsb - -- 6F: *outsd, *outsw - -- 70-7F: jcc lb - -- 80: add... mb,i - -- 81: add... mdw,i - -- 82: *undefined - -- 83: add... mdw,S - test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi", - -- 86: xchg rb,mb - -- 87: xchg rdw,mdw - -- 88: mov mb,r - -- 89: mov mdw,r - -- 8A: mov r,mb - -- 8B: mov r,mdw - -- 8C: *mov mdw,seg - lea_2 = "rx1dq:8DrM", - -- 8E: *mov seg,mdw - -- 8F: pop mdw - nop_0 = "90", - xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm", - cbw_0 = "6698", - cwde_0 = "98", - cdqe_0 = "4898", - cwd_0 = "6699", - cdq_0 = "99", - cqo_0 = "4899", - -- 9A: *call iw:idw - wait_0 = "9B", - fwait_0 = "9B", - pushf_0 = "9C", - pushfd_0 = not x64 and "9C", - pushfq_0 = x64 and "9C", - popf_0 = "9D", - popfd_0 = not x64 and "9D", - popfq_0 = x64 and "9D", - sahf_0 = "9E", - lahf_0 = "9F", - mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi", - movsb_0 = "A4", - movsw_0 = "66A5", - movsd_0 = "A5", - cmpsb_0 = "A6", - cmpsw_0 = "66A7", - cmpsd_0 = "A7", - -- A8: test Rb,i - -- A9: test Rdw,i - stosb_0 = "AA", - stosw_0 = "66AB", - stosd_0 = "AB", - lodsb_0 = "AC", - lodsw_0 = "66AD", - lodsd_0 = "AD", - scasb_0 = "AE", - scasw_0 = "66AF", - scasd_0 = "AF", - -- B0-B7: mov rb,i - -- B8-BF: mov rdw,i - -- C0: rol... mb,i - -- C1: rol... mdw,i - ret_1 = "i.:nC2W", - ret_0 = "C3", - -- C4: *les rdw,mq - -- C5: *lds rdw,mq - -- C6: mov mb,i - -- C7: mov mdw,i - -- C8: *enter iw,ib - leave_0 = "C9", - -- CA: *retf iw - -- CB: *retf - int3_0 = "CC", - int_1 = "i.:nCDU", - into_0 = "CE", - -- CF: *iret - -- D0: rol... mb,1 - -- D1: rol... mdw,1 - -- D2: rol... mb,cl - -- D3: rol... mb,cl - -- D4: *aam ib - -- D5: *aad ib - -- D6: *salc - -- D7: *xlat - -- D8-DF: floating point ops - -- E0: *loopne - -- E1: *loope - -- E2: *loop - -- E3: *jcxz, *jecxz - -- E4: *in Rb,ib - -- E5: *in Rdw,ib - -- E6: *out ib,Rb - -- E7: *out ib,Rdw - call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J", - jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB - -- EA: *jmp iw:idw - -- EB: jmp ib - -- EC: *in Rb,dx - -- ED: *in Rdw,dx - -- EE: *out dx,Rb - -- EF: *out dx,Rdw - lock_0 = "F0", - int1_0 = "F1", - repne_0 = "F2", - repnz_0 = "F2", - rep_0 = "F3", - repe_0 = "F3", - repz_0 = "F3", - -- F4: *hlt - cmc_0 = "F5", - -- F6: test... mb,i; div... mb - -- F7: test... mdw,i; div... mdw - clc_0 = "F8", - stc_0 = "F9", - -- FA: *cli - cld_0 = "FC", - std_0 = "FD", - -- FE: inc... mb - -- FF: inc... mdw - - -- misc ops - not_1 = "m:F72m", - neg_1 = "m:F73m", - mul_1 = "m:F74m", - imul_1 = "m:F75m", - div_1 = "m:F76m", - idiv_1 = "m:F77m", - - imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi", - imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi", - - movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:", - movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:", - - bswap_1 = "rqd:0FC8r", - bsf_2 = "rmqdw:0FBCrM", - bsr_2 = "rmqdw:0FBDrM", - bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU", - btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU", - btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU", - bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU", - - shld_3 = "mriqdw:0FA4RmU|mrC/qq:0FA5Rm|mrC/dd:|mrC/ww:", - shrd_3 = "mriqdw:0FACRmU|mrC/qq:0FADRm|mrC/dd:|mrC/ww:", - - rdtsc_0 = "0F31", -- P1+ - rdpmc_0 = "0F33", -- P6+ - cpuid_0 = "0FA2", -- P1+ - - -- floating point ops - fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m", - fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m", - fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m", - - fpop_0 = "DDD8", -- Alias for fstp st0. - - fist_1 = "xw:nDF2m|xd:DB2m", - fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m", - fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m", - - fxch_0 = "D9C9", - fxch_1 = "ff:D9C8r", - fxch_2 = "fFf:D9C8r|Fff:D9C8R", - - fucom_1 = "ff:DDE0r", - fucom_2 = "Fff:DDE0R", - fucomp_1 = "ff:DDE8r", - fucomp_2 = "Fff:DDE8R", - fucomi_1 = "ff:DBE8r", -- P6+ - fucomi_2 = "Fff:DBE8R", -- P6+ - fucomip_1 = "ff:DFE8r", -- P6+ - fucomip_2 = "Fff:DFE8R", -- P6+ - fcomi_1 = "ff:DBF0r", -- P6+ - fcomi_2 = "Fff:DBF0R", -- P6+ - fcomip_1 = "ff:DFF0r", -- P6+ - fcomip_2 = "Fff:DFF0R", -- P6+ - fucompp_0 = "DAE9", - fcompp_0 = "DED9", - - fldenv_1 = "x.:D94m", - fnstenv_1 = "x.:D96m", - fstenv_1 = "x.:9BD96m", - fldcw_1 = "xw:nD95m", - fstcw_1 = "xw:n9BD97m", - fnstcw_1 = "xw:nD97m", - fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m", - fnstsw_1 = "Rw:nDFE0|xw:nDD7m", - fclex_0 = "9BDBE2", - fnclex_0 = "DBE2", - - fnop_0 = "D9D0", - -- D9D1-D9DF: unassigned - - fchs_0 = "D9E0", - fabs_0 = "D9E1", - -- D9E2: unassigned - -- D9E3: unassigned - ftst_0 = "D9E4", - fxam_0 = "D9E5", - -- D9E6: unassigned - -- D9E7: unassigned - fld1_0 = "D9E8", - fldl2t_0 = "D9E9", - fldl2e_0 = "D9EA", - fldpi_0 = "D9EB", - fldlg2_0 = "D9EC", - fldln2_0 = "D9ED", - fldz_0 = "D9EE", - -- D9EF: unassigned - - f2xm1_0 = "D9F0", - fyl2x_0 = "D9F1", - fptan_0 = "D9F2", - fpatan_0 = "D9F3", - fxtract_0 = "D9F4", - fprem1_0 = "D9F5", - fdecstp_0 = "D9F6", - fincstp_0 = "D9F7", - fprem_0 = "D9F8", - fyl2xp1_0 = "D9F9", - fsqrt_0 = "D9FA", - fsincos_0 = "D9FB", - frndint_0 = "D9FC", - fscale_0 = "D9FD", - fsin_0 = "D9FE", - fcos_0 = "D9FF", - - -- SSE, SSE2 - andnpd_2 = "rmo:660F55rM", - andnps_2 = "rmo:0F55rM", - andpd_2 = "rmo:660F54rM", - andps_2 = "rmo:0F54rM", - clflush_1 = "x.:0FAE7m", - cmppd_3 = "rmio:660FC2rMU", - cmpps_3 = "rmio:0FC2rMU", - cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:", - cmpss_3 = "rrio:F30FC2rMU|rxi/od:", - comisd_2 = "rro:660F2FrM|rx/oq:", - comiss_2 = "rro:0F2FrM|rx/od:", - cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:", - cvtdq2ps_2 = "rmo:0F5BrM", - cvtpd2dq_2 = "rmo:F20FE6rM", - cvtpd2ps_2 = "rmo:660F5ArM", - cvtpi2pd_2 = "rx/oq:660F2ArM", - cvtpi2ps_2 = "rx/oq:0F2ArM", - cvtps2dq_2 = "rmo:660F5BrM", - cvtps2pd_2 = "rro:0F5ArM|rx/oq:", - cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:", - cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:", - cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM", - cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM", - cvtss2sd_2 = "rro:F30F5ArM|rx/od:", - cvtss2si_2 = "rr/do:F30F2DrM|rr/qo:|rxd:|rx/qd:", - cvttpd2dq_2 = "rmo:660FE6rM", - cvttps2dq_2 = "rmo:F30F5BrM", - cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:", - cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:", - fxsave_1 = "x.:0FAE0m", - fxrstor_1 = "x.:0FAE1m", - ldmxcsr_1 = "xd:0FAE2m", - lfence_0 = "0FAEE8", - maskmovdqu_2 = "rro:660FF7rM", - mfence_0 = "0FAEF0", - movapd_2 = "rmo:660F28rM|mro:660F29Rm", - movaps_2 = "rmo:0F28rM|mro:0F29Rm", - movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:", - movdqa_2 = "rmo:660F6FrM|mro:660F7FRm", - movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm", - movhlps_2 = "rro:0F12rM", - movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm", - movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm", - movlhps_2 = "rro:0F16rM", - movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm", - movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm", - movmskpd_2 = "rr/do:660F50rM", - movmskps_2 = "rr/do:0F50rM", - movntdq_2 = "xro:660FE7Rm", - movnti_2 = "xrqd:0FC3Rm", - movntpd_2 = "xro:660F2BRm", - movntps_2 = "xro:0F2BRm", - movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm", - movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm", - movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm", - movupd_2 = "rmo:660F10rM|mro:660F11Rm", - movups_2 = "rmo:0F10rM|mro:0F11Rm", - orpd_2 = "rmo:660F56rM", - orps_2 = "rmo:0F56rM", - pause_0 = "F390", - pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nRmU", -- Mem op: SSE4.1 only. - pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:", - pmovmskb_2 = "rr/do:660FD7rM", - prefetchnta_1 = "xb:n0F180m", - prefetcht0_1 = "xb:n0F181m", - prefetcht1_1 = "xb:n0F182m", - prefetcht2_1 = "xb:n0F183m", - pshufd_3 = "rmio:660F70rMU", - pshufhw_3 = "rmio:F30F70rMU", - pshuflw_3 = "rmio:F20F70rMU", - pslld_2 = "rmo:660FF2rM|rio:660F726mU", - pslldq_2 = "rio:660F737mU", - psllq_2 = "rmo:660FF3rM|rio:660F736mU", - psllw_2 = "rmo:660FF1rM|rio:660F716mU", - psrad_2 = "rmo:660FE2rM|rio:660F724mU", - psraw_2 = "rmo:660FE1rM|rio:660F714mU", - psrld_2 = "rmo:660FD2rM|rio:660F722mU", - psrldq_2 = "rio:660F733mU", - psrlq_2 = "rmo:660FD3rM|rio:660F732mU", - psrlw_2 = "rmo:660FD1rM|rio:660F712mU", - rcpps_2 = "rmo:0F53rM", - rcpss_2 = "rro:F30F53rM|rx/od:", - rsqrtps_2 = "rmo:0F52rM", - rsqrtss_2 = "rmo:F30F52rM", - sfence_0 = "0FAEF8", - shufpd_3 = "rmio:660FC6rMU", - shufps_3 = "rmio:0FC6rMU", - stmxcsr_1 = "xd:0FAE3m", - ucomisd_2 = "rro:660F2ErM|rx/oq:", - ucomiss_2 = "rro:0F2ErM|rx/od:", - unpckhpd_2 = "rmo:660F15rM", - unpckhps_2 = "rmo:0F15rM", - unpcklpd_2 = "rmo:660F14rM", - unpcklps_2 = "rmo:0F14rM", - xorpd_2 = "rmo:660F57rM", - xorps_2 = "rmo:0F57rM", - - -- SSE3 ops - fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m", - addsubpd_2 = "rmo:660FD0rM", - addsubps_2 = "rmo:F20FD0rM", - haddpd_2 = "rmo:660F7CrM", - haddps_2 = "rmo:F20F7CrM", - hsubpd_2 = "rmo:660F7DrM", - hsubps_2 = "rmo:F20F7DrM", - lddqu_2 = "rxo:F20FF0rM", - movddup_2 = "rmo:F20F12rM", - movshdup_2 = "rmo:F30F16rM", - movsldup_2 = "rmo:F30F12rM", - - -- SSSE3 ops - pabsb_2 = "rmo:660F381CrM", - pabsd_2 = "rmo:660F381ErM", - pabsw_2 = "rmo:660F381DrM", - palignr_3 = "rmio:660F3A0FrMU", - phaddd_2 = "rmo:660F3802rM", - phaddsw_2 = "rmo:660F3803rM", - phaddw_2 = "rmo:660F3801rM", - phsubd_2 = "rmo:660F3806rM", - phsubsw_2 = "rmo:660F3807rM", - phsubw_2 = "rmo:660F3805rM", - pmaddubsw_2 = "rmo:660F3804rM", - pmulhrsw_2 = "rmo:660F380BrM", - pshufb_2 = "rmo:660F3800rM", - psignb_2 = "rmo:660F3808rM", - psignd_2 = "rmo:660F380ArM", - psignw_2 = "rmo:660F3809rM", - - -- SSE4.1 ops - blendpd_3 = "rmio:660F3A0DrMU", - blendps_3 = "rmio:660F3A0CrMU", - blendvpd_3 = "rmRo:660F3815rM", - blendvps_3 = "rmRo:660F3814rM", - dppd_3 = "rmio:660F3A41rMU", - dpps_3 = "rmio:660F3A40rMU", - extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU", - insertps_3 = "rrio:660F3A41rMU|rxi/od:", - movntdqa_2 = "rxo:660F382ArM", - mpsadbw_3 = "rmio:660F3A42rMU", - packusdw_2 = "rmo:660F382BrM", - pblendvb_3 = "rmRo:660F3810rM", - pblendw_3 = "rmio:660F3A0ErMU", - pcmpeqq_2 = "rmo:660F3829rM", - pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:", - pextrd_3 = "mri/do:660F3A16RmU", - pextrq_3 = "mri/qo:660F3A16RmU", - -- pextrw is SSE2, mem operand is SSE4.1 only - phminposuw_2 = "rmo:660F3841rM", - pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:", - pinsrd_3 = "rmi/od:660F3A22rMU", - pinsrq_3 = "rmi/oq:660F3A22rXMU", - pmaxsb_2 = "rmo:660F383CrM", - pmaxsd_2 = "rmo:660F383DrM", - pmaxud_2 = "rmo:660F383FrM", - pmaxuw_2 = "rmo:660F383ErM", - pminsb_2 = "rmo:660F3838rM", - pminsd_2 = "rmo:660F3839rM", - pminud_2 = "rmo:660F383BrM", - pminuw_2 = "rmo:660F383ArM", - pmovsxbd_2 = "rro:660F3821rM|rx/od:", - pmovsxbq_2 = "rro:660F3822rM|rx/ow:", - pmovsxbw_2 = "rro:660F3820rM|rx/oq:", - pmovsxdq_2 = "rro:660F3825rM|rx/oq:", - pmovsxwd_2 = "rro:660F3823rM|rx/oq:", - pmovsxwq_2 = "rro:660F3824rM|rx/od:", - pmovzxbd_2 = "rro:660F3831rM|rx/od:", - pmovzxbq_2 = "rro:660F3832rM|rx/ow:", - pmovzxbw_2 = "rro:660F3830rM|rx/oq:", - pmovzxdq_2 = "rro:660F3835rM|rx/oq:", - pmovzxwd_2 = "rro:660F3833rM|rx/oq:", - pmovzxwq_2 = "rro:660F3834rM|rx/od:", - pmuldq_2 = "rmo:660F3828rM", - pmulld_2 = "rmo:660F3840rM", - ptest_2 = "rmo:660F3817rM", - roundpd_3 = "rmio:660F3A09rMU", - roundps_3 = "rmio:660F3A08rMU", - roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:", - roundss_3 = "rrio:660F3A0ArMU|rxi/od:", - - -- SSE4.2 ops - crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:", - pcmpestri_3 = "rmio:660F3A61rMU", - pcmpestrm_3 = "rmio:660F3A60rMU", - pcmpgtq_2 = "rmo:660F3837rM", - pcmpistri_3 = "rmio:660F3A63rMU", - pcmpistrm_3 = "rmio:660F3A62rMU", - popcnt_2 = "rmqdw:F30FB8rM", - - -- SSE4a - extrq_2 = "rro:660F79rM", - extrq_3 = "riio:660F780mUU", - insertq_2 = "rro:F20F79rM", - insertq_4 = "rriio:F20F78rMUU", - lzcnt_2 = "rmqdw:F30FBDrM", - movntsd_2 = "xr/qo:nF20F2BRm", - movntss_2 = "xr/do:F30F2BRm", - -- popcnt is also in SSE4.2 - - -- AES-NI - aesdec_2 = "rmo:660F38DErM", - aesdeclast_2 = "rmo:660F38DFrM", - aesenc_2 = "rmo:660F38DCrM", - aesenclast_2 = "rmo:660F38DDrM", - aesimc_2 = "rmo:660F38DBrM", - aeskeygenassist_3 = "rmio:660F3ADFrMU", - pclmulqdq_3 = "rmio:660F3A44rMU", - - -- AVX FP ops - vaddsubpd_3 = "rrmoy:660FVD0rM", - vaddsubps_3 = "rrmoy:F20FVD0rM", - vandpd_3 = "rrmoy:660FV54rM", - vandps_3 = "rrmoy:0FV54rM", - vandnpd_3 = "rrmoy:660FV55rM", - vandnps_3 = "rrmoy:0FV55rM", - vblendpd_4 = "rrmioy:660F3AV0DrMU", - vblendps_4 = "rrmioy:660F3AV0CrMU", - vblendvpd_4 = "rrmroy:660F3AV4BrMs", - vblendvps_4 = "rrmroy:660F3AV4ArMs", - vbroadcastf128_2 = "rx/yo:660F38u1ArM", - vcmppd_4 = "rrmioy:660FVC2rMU", - vcmpps_4 = "rrmioy:0FVC2rMU", - vcmpsd_4 = "rrrio:F20FVC2rMU|rrxi/ooq:", - vcmpss_4 = "rrrio:F30FVC2rMU|rrxi/ood:", - vcomisd_2 = "rro:660Fu2FrM|rx/oq:", - vcomiss_2 = "rro:0Fu2FrM|rx/od:", - vcvtdq2pd_2 = "rro:F30FuE6rM|rx/oq:|rm/yo:", - vcvtdq2ps_2 = "rmoy:0Fu5BrM", - vcvtpd2dq_2 = "rmoy:F20FuE6rM", - vcvtpd2ps_2 = "rmoy:660Fu5ArM", - vcvtps2dq_2 = "rmoy:660Fu5BrM", - vcvtps2pd_2 = "rro:0Fu5ArM|rx/oq:|rm/yo:", - vcvtsd2si_2 = "rr/do:F20Fu2DrM|rx/dq:|rr/qo:|rxq:", - vcvtsd2ss_3 = "rrro:F20FV5ArM|rrx/ooq:", - vcvtsi2sd_3 = "rrm/ood:F20FV2ArM|rrm/ooq:F20FVX2ArM", - vcvtsi2ss_3 = "rrm/ood:F30FV2ArM|rrm/ooq:F30FVX2ArM", - vcvtss2sd_3 = "rrro:F30FV5ArM|rrx/ood:", - vcvtss2si_2 = "rr/do:F30Fu2DrM|rxd:|rr/qo:|rx/qd:", - vcvttpd2dq_2 = "rmo:660FuE6rM|rm/oy:660FuLE6rM", - vcvttps2dq_2 = "rmoy:F30Fu5BrM", - vcvttsd2si_2 = "rr/do:F20Fu2CrM|rx/dq:|rr/qo:|rxq:", - vcvttss2si_2 = "rr/do:F30Fu2CrM|rxd:|rr/qo:|rx/qd:", - vdppd_4 = "rrmio:660F3AV41rMU", - vdpps_4 = "rrmioy:660F3AV40rMU", - vextractf128_3 = "mri/oy:660F3AuL19RmU", - vextractps_3 = "mri/do:660F3Au17RmU", - vhaddpd_3 = "rrmoy:660FV7CrM", - vhaddps_3 = "rrmoy:F20FV7CrM", - vhsubpd_3 = "rrmoy:660FV7DrM", - vhsubps_3 = "rrmoy:F20FV7DrM", - vinsertf128_4 = "rrmi/yyo:660F3AV18rMU", - vinsertps_4 = "rrrio:660F3AV21rMU|rrxi/ood:", - vldmxcsr_1 = "xd:0FuAE2m", - vmaskmovps_3 = "rrxoy:660F38V2CrM|xrroy:660F38V2ERm", - vmaskmovpd_3 = "rrxoy:660F38V2DrM|xrroy:660F38V2FRm", - vmovapd_2 = "rmoy:660Fu28rM|mroy:660Fu29Rm", - vmovaps_2 = "rmoy:0Fu28rM|mroy:0Fu29Rm", - vmovd_2 = "rm/od:660Fu6ErM|rm/oq:660FuX6ErM|mr/do:660Fu7ERm|mr/qo:", - vmovq_2 = "rro:F30Fu7ErM|rx/oq:|xr/qo:660FuD6Rm", - vmovddup_2 = "rmy:F20Fu12rM|rro:|rx/oq:", - vmovhlps_3 = "rrro:0FV12rM", - vmovhpd_2 = "xr/qo:660Fu17Rm", - vmovhpd_3 = "rrx/ooq:660FV16rM", - vmovhps_2 = "xr/qo:0Fu17Rm", - vmovhps_3 = "rrx/ooq:0FV16rM", - vmovlhps_3 = "rrro:0FV16rM", - vmovlpd_2 = "xr/qo:660Fu13Rm", - vmovlpd_3 = "rrx/ooq:660FV12rM", - vmovlps_2 = "xr/qo:0Fu13Rm", - vmovlps_3 = "rrx/ooq:0FV12rM", - vmovmskpd_2 = "rr/do:660Fu50rM|rr/dy:660FuL50rM", - vmovmskps_2 = "rr/do:0Fu50rM|rr/dy:0FuL50rM", - vmovntpd_2 = "xroy:660Fu2BRm", - vmovntps_2 = "xroy:0Fu2BRm", - vmovsd_2 = "rx/oq:F20Fu10rM|xr/qo:F20Fu11Rm", - vmovsd_3 = "rrro:F20FV10rM", - vmovshdup_2 = "rmoy:F30Fu16rM", - vmovsldup_2 = "rmoy:F30Fu12rM", - vmovss_2 = "rx/od:F30Fu10rM|xr/do:F30Fu11Rm", - vmovss_3 = "rrro:F30FV10rM", - vmovupd_2 = "rmoy:660Fu10rM|mroy:660Fu11Rm", - vmovups_2 = "rmoy:0Fu10rM|mroy:0Fu11Rm", - vorpd_3 = "rrmoy:660FV56rM", - vorps_3 = "rrmoy:0FV56rM", - vpermilpd_3 = "rrmoy:660F38V0DrM|rmioy:660F3Au05rMU", - vpermilps_3 = "rrmoy:660F38V0CrM|rmioy:660F3Au04rMU", - vperm2f128_4 = "rrmiy:660F3AV06rMU", - vptestpd_2 = "rmoy:660F38u0FrM", - vptestps_2 = "rmoy:660F38u0ErM", - vrcpps_2 = "rmoy:0Fu53rM", - vrcpss_3 = "rrro:F30FV53rM|rrx/ood:", - vrsqrtps_2 = "rmoy:0Fu52rM", - vrsqrtss_3 = "rrro:F30FV52rM|rrx/ood:", - vroundpd_3 = "rmioy:660F3Au09rMU", - vroundps_3 = "rmioy:660F3Au08rMU", - vroundsd_4 = "rrrio:660F3AV0BrMU|rrxi/ooq:", - vroundss_4 = "rrrio:660F3AV0ArMU|rrxi/ood:", - vshufpd_4 = "rrmioy:660FVC6rMU", - vshufps_4 = "rrmioy:0FVC6rMU", - vsqrtps_2 = "rmoy:0Fu51rM", - vsqrtss_2 = "rro:F30Fu51rM|rx/od:", - vsqrtpd_2 = "rmoy:660Fu51rM", - vsqrtsd_2 = "rro:F20Fu51rM|rx/oq:", - vstmxcsr_1 = "xd:0FuAE3m", - vucomisd_2 = "rro:660Fu2ErM|rx/oq:", - vucomiss_2 = "rro:0Fu2ErM|rx/od:", - vunpckhpd_3 = "rrmoy:660FV15rM", - vunpckhps_3 = "rrmoy:0FV15rM", - vunpcklpd_3 = "rrmoy:660FV14rM", - vunpcklps_3 = "rrmoy:0FV14rM", - vxorpd_3 = "rrmoy:660FV57rM", - vxorps_3 = "rrmoy:0FV57rM", - vzeroall_0 = "0FuL77", - vzeroupper_0 = "0Fu77", - - -- AVX2 FP ops - vbroadcastss_2 = "rx/od:660F38u18rM|rx/yd:|rro:|rr/yo:", - vbroadcastsd_2 = "rx/yq:660F38u19rM|rr/yo:", - -- *vgather* (!vsib) - vpermpd_3 = "rmiy:660F3AuX01rMU", - vpermps_3 = "rrmy:660F38V16rM", - - -- AVX, AVX2 integer ops - -- In general, xmm requires AVX, ymm requires AVX2. - vaesdec_3 = "rrmo:660F38VDErM", - vaesdeclast_3 = "rrmo:660F38VDFrM", - vaesenc_3 = "rrmo:660F38VDCrM", - vaesenclast_3 = "rrmo:660F38VDDrM", - vaesimc_2 = "rmo:660F38uDBrM", - vaeskeygenassist_3 = "rmio:660F3AuDFrMU", - vlddqu_2 = "rxoy:F20FuF0rM", - vmaskmovdqu_2 = "rro:660FuF7rM", - vmovdqa_2 = "rmoy:660Fu6FrM|mroy:660Fu7FRm", - vmovdqu_2 = "rmoy:F30Fu6FrM|mroy:F30Fu7FRm", - vmovntdq_2 = "xroy:660FuE7Rm", - vmovntdqa_2 = "rxoy:660F38u2ArM", - vmpsadbw_4 = "rrmioy:660F3AV42rMU", - vpabsb_2 = "rmoy:660F38u1CrM", - vpabsd_2 = "rmoy:660F38u1ErM", - vpabsw_2 = "rmoy:660F38u1DrM", - vpackusdw_3 = "rrmoy:660F38V2BrM", - vpalignr_4 = "rrmioy:660F3AV0FrMU", - vpblendvb_4 = "rrmroy:660F3AV4CrMs", - vpblendw_4 = "rrmioy:660F3AV0ErMU", - vpclmulqdq_4 = "rrmio:660F3AV44rMU", - vpcmpeqq_3 = "rrmoy:660F38V29rM", - vpcmpestri_3 = "rmio:660F3Au61rMU", - vpcmpestrm_3 = "rmio:660F3Au60rMU", - vpcmpgtq_3 = "rrmoy:660F38V37rM", - vpcmpistri_3 = "rmio:660F3Au63rMU", - vpcmpistrm_3 = "rmio:660F3Au62rMU", - vpextrb_3 = "rri/do:660F3Au14nRmU|rri/qo:|xri/bo:", - vpextrw_3 = "rri/do:660FuC5rMU|xri/wo:660F3Au15nRmU", - vpextrd_3 = "mri/do:660F3Au16RmU", - vpextrq_3 = "mri/qo:660F3Au16RmU", - vphaddw_3 = "rrmoy:660F38V01rM", - vphaddd_3 = "rrmoy:660F38V02rM", - vphaddsw_3 = "rrmoy:660F38V03rM", - vphminposuw_2 = "rmo:660F38u41rM", - vphsubw_3 = "rrmoy:660F38V05rM", - vphsubd_3 = "rrmoy:660F38V06rM", - vphsubsw_3 = "rrmoy:660F38V07rM", - vpinsrb_4 = "rrri/ood:660F3AV20rMU|rrxi/oob:", - vpinsrw_4 = "rrri/ood:660FVC4rMU|rrxi/oow:", - vpinsrd_4 = "rrmi/ood:660F3AV22rMU", - vpinsrq_4 = "rrmi/ooq:660F3AVX22rMU", - vpmaddubsw_3 = "rrmoy:660F38V04rM", - vpmaxsb_3 = "rrmoy:660F38V3CrM", - vpmaxsd_3 = "rrmoy:660F38V3DrM", - vpmaxuw_3 = "rrmoy:660F38V3ErM", - vpmaxud_3 = "rrmoy:660F38V3FrM", - vpminsb_3 = "rrmoy:660F38V38rM", - vpminsd_3 = "rrmoy:660F38V39rM", - vpminuw_3 = "rrmoy:660F38V3ArM", - vpminud_3 = "rrmoy:660F38V3BrM", - vpmovmskb_2 = "rr/do:660FuD7rM|rr/dy:660FuLD7rM", - vpmovsxbw_2 = "rroy:660F38u20rM|rx/oq:|rx/yo:", - vpmovsxbd_2 = "rroy:660F38u21rM|rx/od:|rx/yq:", - vpmovsxbq_2 = "rroy:660F38u22rM|rx/ow:|rx/yd:", - vpmovsxwd_2 = "rroy:660F38u23rM|rx/oq:|rx/yo:", - vpmovsxwq_2 = "rroy:660F38u24rM|rx/od:|rx/yq:", - vpmovsxdq_2 = "rroy:660F38u25rM|rx/oq:|rx/yo:", - vpmovzxbw_2 = "rroy:660F38u30rM|rx/oq:|rx/yo:", - vpmovzxbd_2 = "rroy:660F38u31rM|rx/od:|rx/yq:", - vpmovzxbq_2 = "rroy:660F38u32rM|rx/ow:|rx/yd:", - vpmovzxwd_2 = "rroy:660F38u33rM|rx/oq:|rx/yo:", - vpmovzxwq_2 = "rroy:660F38u34rM|rx/od:|rx/yq:", - vpmovzxdq_2 = "rroy:660F38u35rM|rx/oq:|rx/yo:", - vpmuldq_3 = "rrmoy:660F38V28rM", - vpmulhrsw_3 = "rrmoy:660F38V0BrM", - vpmulld_3 = "rrmoy:660F38V40rM", - vpshufb_3 = "rrmoy:660F38V00rM", - vpshufd_3 = "rmioy:660Fu70rMU", - vpshufhw_3 = "rmioy:F30Fu70rMU", - vpshuflw_3 = "rmioy:F20Fu70rMU", - vpsignb_3 = "rrmoy:660F38V08rM", - vpsignw_3 = "rrmoy:660F38V09rM", - vpsignd_3 = "rrmoy:660F38V0ArM", - vpslldq_3 = "rrioy:660Fv737mU", - vpsllw_3 = "rrmoy:660FVF1rM|rrioy:660Fv716mU", - vpslld_3 = "rrmoy:660FVF2rM|rrioy:660Fv726mU", - vpsllq_3 = "rrmoy:660FVF3rM|rrioy:660Fv736mU", - vpsraw_3 = "rrmoy:660FVE1rM|rrioy:660Fv714mU", - vpsrad_3 = "rrmoy:660FVE2rM|rrioy:660Fv724mU", - vpsrldq_3 = "rrioy:660Fv733mU", - vpsrlw_3 = "rrmoy:660FVD1rM|rrioy:660Fv712mU", - vpsrld_3 = "rrmoy:660FVD2rM|rrioy:660Fv722mU", - vpsrlq_3 = "rrmoy:660FVD3rM|rrioy:660Fv732mU", - vptest_2 = "rmoy:660F38u17rM", - - -- AVX2 integer ops - vbroadcasti128_2 = "rx/yo:660F38u5ArM", - vinserti128_4 = "rrmi/yyo:660F3AV38rMU", - vextracti128_3 = "mri/oy:660F3AuL39RmU", - vpblendd_4 = "rrmioy:660F3AV02rMU", - vpbroadcastb_2 = "rro:660F38u78rM|rx/ob:|rr/yo:|rx/yb:", - vpbroadcastw_2 = "rro:660F38u79rM|rx/ow:|rr/yo:|rx/yw:", - vpbroadcastd_2 = "rro:660F38u58rM|rx/od:|rr/yo:|rx/yd:", - vpbroadcastq_2 = "rro:660F38u59rM|rx/oq:|rr/yo:|rx/yq:", - vpermd_3 = "rrmy:660F38V36rM", - vpermq_3 = "rmiy:660F3AuX00rMU", - -- *vpgather* (!vsib) - vperm2i128_4 = "rrmiy:660F3AV46rMU", - vpmaskmovd_3 = "rrxoy:660F38V8CrM|xrroy:660F38V8ERm", - vpmaskmovq_3 = "rrxoy:660F38VX8CrM|xrroy:660F38VX8ERm", - vpsllvd_3 = "rrmoy:660F38V47rM", - vpsllvq_3 = "rrmoy:660F38VX47rM", - vpsravd_3 = "rrmoy:660F38V46rM", - vpsrlvd_3 = "rrmoy:660F38V45rM", - vpsrlvq_3 = "rrmoy:660F38VX45rM", - - -- Intel ADX - adcx_2 = "rmqd:660F38F6rM", - adox_2 = "rmqd:F30F38F6rM", - - -- BMI1 - andn_3 = "rrmqd:0F38VF2rM", - bextr_3 = "rmrqd:0F38wF7rM", - blsi_2 = "rmqd:0F38vF33m", - blsmsk_2 = "rmqd:0F38vF32m", - blsr_2 = "rmqd:0F38vF31m", - tzcnt_2 = "rmqdw:F30FBCrM", - - -- BMI2 - bzhi_3 = "rmrqd:0F38wF5rM", - mulx_3 = "rrmqd:F20F38VF6rM", - pdep_3 = "rrmqd:F20F38VF5rM", - pext_3 = "rrmqd:F30F38VF5rM", - rorx_3 = "rmSqd:F20F3AuF0rMS", - sarx_3 = "rmrqd:F30F38wF7rM", - shrx_3 = "rmrqd:F20F38wF7rM", - shlx_3 = "rmrqd:660F38wF7rM", - - -- FMA3 - vfmaddsub132pd_3 = "rrmoy:660F38VX96rM", - vfmaddsub132ps_3 = "rrmoy:660F38V96rM", - vfmaddsub213pd_3 = "rrmoy:660F38VXA6rM", - vfmaddsub213ps_3 = "rrmoy:660F38VA6rM", - vfmaddsub231pd_3 = "rrmoy:660F38VXB6rM", - vfmaddsub231ps_3 = "rrmoy:660F38VB6rM", - - vfmsubadd132pd_3 = "rrmoy:660F38VX97rM", - vfmsubadd132ps_3 = "rrmoy:660F38V97rM", - vfmsubadd213pd_3 = "rrmoy:660F38VXA7rM", - vfmsubadd213ps_3 = "rrmoy:660F38VA7rM", - vfmsubadd231pd_3 = "rrmoy:660F38VXB7rM", - vfmsubadd231ps_3 = "rrmoy:660F38VB7rM", - - vfmadd132pd_3 = "rrmoy:660F38VX98rM", - vfmadd132ps_3 = "rrmoy:660F38V98rM", - vfmadd132sd_3 = "rrro:660F38VX99rM|rrx/ooq:", - vfmadd132ss_3 = "rrro:660F38V99rM|rrx/ood:", - vfmadd213pd_3 = "rrmoy:660F38VXA8rM", - vfmadd213ps_3 = "rrmoy:660F38VA8rM", - vfmadd213sd_3 = "rrro:660F38VXA9rM|rrx/ooq:", - vfmadd213ss_3 = "rrro:660F38VA9rM|rrx/ood:", - vfmadd231pd_3 = "rrmoy:660F38VXB8rM", - vfmadd231ps_3 = "rrmoy:660F38VB8rM", - vfmadd231sd_3 = "rrro:660F38VXB9rM|rrx/ooq:", - vfmadd231ss_3 = "rrro:660F38VB9rM|rrx/ood:", - - vfmsub132pd_3 = "rrmoy:660F38VX9ArM", - vfmsub132ps_3 = "rrmoy:660F38V9ArM", - vfmsub132sd_3 = "rrro:660F38VX9BrM|rrx/ooq:", - vfmsub132ss_3 = "rrro:660F38V9BrM|rrx/ood:", - vfmsub213pd_3 = "rrmoy:660F38VXAArM", - vfmsub213ps_3 = "rrmoy:660F38VAArM", - vfmsub213sd_3 = "rrro:660F38VXABrM|rrx/ooq:", - vfmsub213ss_3 = "rrro:660F38VABrM|rrx/ood:", - vfmsub231pd_3 = "rrmoy:660F38VXBArM", - vfmsub231ps_3 = "rrmoy:660F38VBArM", - vfmsub231sd_3 = "rrro:660F38VXBBrM|rrx/ooq:", - vfmsub231ss_3 = "rrro:660F38VBBrM|rrx/ood:", - - vfnmadd132pd_3 = "rrmoy:660F38VX9CrM", - vfnmadd132ps_3 = "rrmoy:660F38V9CrM", - vfnmadd132sd_3 = "rrro:660F38VX9DrM|rrx/ooq:", - vfnmadd132ss_3 = "rrro:660F38V9DrM|rrx/ood:", - vfnmadd213pd_3 = "rrmoy:660F38VXACrM", - vfnmadd213ps_3 = "rrmoy:660F38VACrM", - vfnmadd213sd_3 = "rrro:660F38VXADrM|rrx/ooq:", - vfnmadd213ss_3 = "rrro:660F38VADrM|rrx/ood:", - vfnmadd231pd_3 = "rrmoy:660F38VXBCrM", - vfnmadd231ps_3 = "rrmoy:660F38VBCrM", - vfnmadd231sd_3 = "rrro:660F38VXBDrM|rrx/ooq:", - vfnmadd231ss_3 = "rrro:660F38VBDrM|rrx/ood:", - - vfnmsub132pd_3 = "rrmoy:660F38VX9ErM", - vfnmsub132ps_3 = "rrmoy:660F38V9ErM", - vfnmsub132sd_3 = "rrro:660F38VX9FrM|rrx/ooq:", - vfnmsub132ss_3 = "rrro:660F38V9FrM|rrx/ood:", - vfnmsub213pd_3 = "rrmoy:660F38VXAErM", - vfnmsub213ps_3 = "rrmoy:660F38VAErM", - vfnmsub213sd_3 = "rrro:660F38VXAFrM|rrx/ooq:", - vfnmsub213ss_3 = "rrro:660F38VAFrM|rrx/ood:", - vfnmsub231pd_3 = "rrmoy:660F38VXBErM", - vfnmsub231ps_3 = "rrmoy:660F38VBErM", - vfnmsub231sd_3 = "rrro:660F38VXBFrM|rrx/ooq:", - vfnmsub231ss_3 = "rrro:660F38VBFrM|rrx/ood:", -} - ------------------------------------------------------------------------------- - --- Arithmetic ops. -for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3, - ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do - local n8 = shl(n, 3) - map_op[name.."_2"] = format( - "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi", - 1+n8, 3+n8, n, n, 5+n8, n) -end - --- Shift ops. -for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3, - shl = 4, shr = 5, sar = 7, sal = 4 } do - map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n) -end - --- Conditional ops. -for cc,n in pairs(map_cc) do - map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X - map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n) - map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+ -end - --- FP arithmetic ops. -for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3, - sub = 4, subr = 5, div = 6, divr = 7 } do - local nc = 0xc0 + shl(n, 3) - local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8)) - local fn = "f"..name - map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n) - if n == 2 or n == 3 then - map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n) - else - map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n) - map_op[fn.."p_1"] = format("ff:DE%02Xr", nr) - map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr) - end - map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n) -end - --- FP conditional moves. -for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do - local nc = 0xdac0 + shl(band(n, 3), 3) + shl(band(n, 4), 6) - map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+ - map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+ -end - --- SSE / AVX FP arithmetic ops. -for name,n in pairs{ sqrt = 1, add = 8, mul = 9, - sub = 12, min = 13, div = 14, max = 15 } do - map_op[name.."ps_2"] = format("rmo:0F5%XrM", n) - map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n) - map_op[name.."pd_2"] = format("rmo:660F5%XrM", n) - map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n) - if n ~= 1 then - map_op["v"..name.."ps_3"] = format("rrmoy:0FV5%XrM", n) - map_op["v"..name.."ss_3"] = format("rrro:F30FV5%XrM|rrx/ood:", n) - map_op["v"..name.."pd_3"] = format("rrmoy:660FV5%XrM", n) - map_op["v"..name.."sd_3"] = format("rrro:F20FV5%XrM|rrx/ooq:", n) - end -end - --- SSE2 / AVX / AVX2 integer arithmetic ops (66 0F leaf). -for name,n in pairs{ - paddb = 0xFC, paddw = 0xFD, paddd = 0xFE, paddq = 0xD4, - paddsb = 0xEC, paddsw = 0xED, packssdw = 0x6B, - packsswb = 0x63, packuswb = 0x67, paddusb = 0xDC, - paddusw = 0xDD, pand = 0xDB, pandn = 0xDF, pavgb = 0xE0, - pavgw = 0xE3, pcmpeqb = 0x74, pcmpeqd = 0x76, - pcmpeqw = 0x75, pcmpgtb = 0x64, pcmpgtd = 0x66, - pcmpgtw = 0x65, pmaddwd = 0xF5, pmaxsw = 0xEE, - pmaxub = 0xDE, pminsw = 0xEA, pminub = 0xDA, - pmulhuw = 0xE4, pmulhw = 0xE5, pmullw = 0xD5, - pmuludq = 0xF4, por = 0xEB, psadbw = 0xF6, psubb = 0xF8, - psubw = 0xF9, psubd = 0xFA, psubq = 0xFB, psubsb = 0xE8, - psubsw = 0xE9, psubusb = 0xD8, psubusw = 0xD9, - punpckhbw = 0x68, punpckhwd = 0x69, punpckhdq = 0x6A, - punpckhqdq = 0x6D, punpcklbw = 0x60, punpcklwd = 0x61, - punpckldq = 0x62, punpcklqdq = 0x6C, pxor = 0xEF -} do - map_op[name.."_2"] = format("rmo:660F%02XrM", n) - map_op["v"..name.."_3"] = format("rrmoy:660FV%02XrM", n) -end - ------------------------------------------------------------------------------- - -local map_vexarg = { u = false, v = 1, V = 2, w = 3 } - --- Process pattern string. -local function dopattern(pat, args, sz, op, needrex) - local digit, addin, vex - local opcode = 0 - local szov = sz - local narg = 1 - local rex = 0 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 6 positions. - if secpos+6 > maxsecpos then wflush() end - - -- Process each character. - for c in gmatch(pat.."|", ".") do - if match(c, "%x") then -- Hex digit. - digit = byte(c) - 48 - if digit > 48 then digit = digit - 39 - elseif digit > 16 then digit = digit - 7 end - opcode = opcode*16 + digit - addin = nil - elseif c == "n" then -- Disable operand size mods for opcode. - szov = nil - elseif c == "X" then -- Force REX.W. - rex = 8 - elseif c == "L" then -- Force VEX.L. - vex.l = true - elseif c == "r" then -- Merge 1st operand regno. into opcode. - addin = args[1]; opcode = opcode + (addin.reg % 8) - if narg < 2 then narg = 2 end - elseif c == "R" then -- Merge 2nd operand regno. into opcode. - addin = args[2]; opcode = opcode + (addin.reg % 8) - narg = 3 - elseif c == "m" or c == "M" then -- Encode ModRM/SIB. - local s - if addin then - s = addin.reg - opcode = opcode - band(s, 7) -- Undo regno opcode merge. - else - s = band(opcode, 15) -- Undo last digit. - opcode = shr(opcode, 4) - end - local nn = c == "m" and 1 or 2 - local t = args[nn] - if narg <= nn then narg = nn + 1 end - if szov == "q" and rex == 0 then rex = rex + 8 end - if t.reg and t.reg > 7 then rex = rex + 1 end - if t.xreg and t.xreg > 7 then rex = rex + 2 end - if s > 7 then rex = rex + 4 end - if needrex then rex = rex + 16 end - local psz, sk = wputop(szov, opcode, rex, vex, s < 0, t.vreg or t.vxreg) - opcode = nil - local imark = sub(pat, -1) -- Force a mark (ugly). - -- Put ModRM/SIB with regno/last digit as spare. - wputmrmsib(t, imark, s, addin and addin.vreg, psz, sk) - addin = nil - elseif map_vexarg[c] ~= nil then -- Encode using VEX prefix - local b = band(opcode, 255); opcode = shr(opcode, 8) - local m = 1 - if b == 0x38 then m = 2 - elseif b == 0x3a then m = 3 end - if m ~= 1 then b = band(opcode, 255); opcode = shr(opcode, 8) end - if b ~= 0x0f then - werror("expected `0F', `0F38', or `0F3A' to precede `"..c.. - "' in pattern `"..pat.."' for `"..op.."'") - end - local v = map_vexarg[c] - if v then v = remove(args, v) end - b = band(opcode, 255) - local p = 0 - if b == 0x66 then p = 1 - elseif b == 0xf3 then p = 2 - elseif b == 0xf2 then p = 3 end - if p ~= 0 then opcode = shr(opcode, 8) end - if opcode ~= 0 then wputop(nil, opcode, 0); opcode = 0 end - vex = { m = m, p = p, v = v } - else - if opcode then -- Flush opcode. - if szov == "q" and rex == 0 then rex = rex + 8 end - if needrex then rex = rex + 16 end - if addin and addin.reg == -1 then - local psz, sk = wputop(szov, opcode - 7, rex, vex, true) - wvreg("opcode", addin.vreg, psz, sk) - else - if addin and addin.reg > 7 then rex = rex + 1 end - wputop(szov, opcode, rex, vex) - end - opcode = nil - end - if c == "|" then break end - if c == "o" then -- Offset (pure 32 bit displacement). - wputdarg(args[1].disp); if narg < 2 then narg = 2 end - elseif c == "O" then - wputdarg(args[2].disp); narg = 3 - else - -- Anything else is an immediate operand. - local a = args[narg] - narg = narg + 1 - local mode, imm = a.mode, a.imm - if mode == "iJ" and not match(x64 and "J" or "iIJ", c) then - werror("bad operand size for label") - end - if c == "S" then - wputsbarg(imm) - elseif c == "U" then - wputbarg(imm) - elseif c == "W" then - wputwarg(imm) - elseif c == "i" or c == "I" then - if mode == "iJ" then - wputlabel("IMM_", imm, 1) - elseif mode == "iI" and c == "I" then - waction(sz == "w" and "IMM_WB" or "IMM_DB", imm) - else - wputszarg(sz, imm) - end - elseif c == "J" then - if mode == "iPJ" then - waction("REL_A", imm) -- !x64 (secpos) - else - wputlabel("REL_", imm, 2) - end - elseif c == "s" then - local reg = a.reg - if reg < 0 then - wputb(0) - wvreg("imm.hi", a.vreg) - else - wputb(shl(reg, 4)) - end - else - werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'") - end - end - end - end -end - ------------------------------------------------------------------------------- - --- Mapping of operand modes to short names. Suppress output with '#'. -local map_modename = { - r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm", - f = "stx", F = "st0", J = "lbl", ["1"] = "1", - I = "#", S = "#", O = "#", -} - --- Return a table/string showing all possible operand modes. -local function templatehelp(template, nparams) - if nparams == 0 then return "" end - local t = {} - for tm in gmatch(template, "[^%|]+") do - local s = map_modename[sub(tm, 1, 1)] - s = s..gsub(sub(tm, 2, nparams), ".", function(c) - return ", "..map_modename[c] - end) - if not match(s, "#") then t[#t+1] = s end - end - return t -end - --- Match operand modes against mode match part of template. -local function matchtm(tm, args) - for i=1,#args do - if not match(args[i].mode, sub(tm, i, i)) then return end - end - return true -end - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return templatehelp(template, nparams) end - local args = {} - - -- Zero-operand opcodes have no match part. - if #params == 0 then - dopattern(template, args, "d", params.op, nil) - return - end - - -- Determine common operand size (coerce undefined size) or flag as mixed. - local sz, szmix, needrex - for i,p in ipairs(params) do - args[i] = parseoperand(p) - local nsz = args[i].opsize - if nsz then - if sz and sz ~= nsz then szmix = true else sz = nsz end - end - local nrex = args[i].needrex - if nrex ~= nil then - if needrex == nil then - needrex = nrex - elseif needrex ~= nrex then - werror("bad mix of byte-addressable registers") - end - end - end - - -- Try all match:pattern pairs (separated by '|'). - local gotmatch, lastpat - for tm in gmatch(template, "[^%|]+") do - -- Split off size match (starts after mode match) and pattern string. - local szm, pat = match(tm, "^(.-):(.*)$", #args+1) - if pat == "" then pat = lastpat else lastpat = pat end - if matchtm(tm, args) then - local prefix = sub(szm, 1, 1) - if prefix == "/" then -- Exactly match leading operand sizes. - for i = #szm,1,-1 do - if i == 1 then - dopattern(pat, args, sz, params.op, needrex) -- Process pattern. - return - elseif args[i-1].opsize ~= sub(szm, i, i) then - break - end - end - else -- Match common operand size. - local szp = sz - if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes. - if prefix == "1" then szp = args[1].opsize; szmix = nil - elseif prefix == "2" then szp = args[2].opsize; szmix = nil end - if not szmix and (prefix == "." or match(szm, szp or "#")) then - dopattern(pat, args, szp, params.op, needrex) -- Process pattern. - return - end - end - gotmatch = true - end - end - - local msg = "bad operand mode" - if gotmatch then - if szmix then - msg = "mixed operand size" - else - msg = sz and "bad operand size" or "missing operand size" - end - end - - werror(msg.." in `"..opmodestr(params.op, args).."'") -end - ------------------------------------------------------------------------------- - --- x64-specific opcode for 64 bit immediates and displacements. -if x64 then - function map_op.mov64_2(params) - if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end - if secpos+2 > maxsecpos then wflush() end - local opcode, op64, sz, rex, vreg - local op64 = match(params[1], "^%[%s*(.-)%s*%]$") - if op64 then - local a = parseoperand(params[2]) - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa3 - else - op64 = match(params[2], "^%[%s*(.-)%s*%]$") - local a = parseoperand(params[1]) - if op64 then - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa1 - else - if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then - werror("bad operand mode") - end - op64 = params[2] - if a.reg == -1 then - vreg = a.vreg - opcode = 0xb8 - else - opcode = 0xb8 + band(a.reg, 7) - end - rex = a.reg > 7 and 9 or 8 - end - end - local psz, sk = wputop(sz, opcode, rex, nil, vreg) - wvreg("opcode", vreg, psz, sk) - waction("IMM_D", format("(unsigned int)(%s)", op64)) - waction("IMM_D", format("(unsigned int)((%s)>>32)", op64)) - end -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -local function op_data(params) - if not params then return "imm..." end - local sz = sub(params.op, 2, 2) - if sz == "l" then sz = "d" elseif sz == "a" then sz = addrsize end - for _,p in ipairs(params) do - local a = parseoperand(p, sz == "q") - if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then - werror("bad mode or size in `"..p.."'") - end - if a.mode == "iJ" then - wputlabel("IMM_", a.imm, 1) - elseif sz == "q" then - wputqarg(a.imm) - else - wputszarg(sz, a.imm) - end - if secpos+2 > maxsecpos then wflush() end - end -end - -map_op[".byte_*"] = op_data -map_op[".sbyte_*"] = op_data -map_op[".word_*"] = op_data -map_op[".dword_*"] = op_data -map_op[".qword_*"] = op_data -map_op[".aword_*"] = op_data -map_op[".long_*"] = op_data -map_op[".quad_*"] = op_data -map_op[".addr_*"] = op_data - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_2"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end - if secpos+2 > maxsecpos then wflush() end - local a = parseoperand(params[1]) - local mode, imm = a.mode, a.imm - if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then - -- Local label (1: ... 9:) or global label (->global:). - waction("LABEL_LG", nil, 1) - wputxb(imm) - elseif mode == "iJ" then - -- PC label (=>pcexpr:). - waction("LABEL_PC", imm) - else - werror("bad label definition") - end - -- SETLABEL must immediately follow LABEL_LG/LABEL_PC. - local addr = params[2] - if addr then - local a = parseoperand(addr) - if a.mode == "iPJ" then - waction("SETLABEL", a.imm) - else - werror("bad label assignment") - end - end -end -map_op[".label_1"] = map_op[".label_2"] - ------------------------------------------------------------------------------- - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]] - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", nil, 1) - wputxb(align-1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - --- Spacing pseudo-opcode. -map_op[".space_2"] = function(params) - if not params then return "num [, filler]" end - if secpos+1 > maxsecpos then wflush() end - waction("SPACE", params[1]) - local fill = params[2] - if fill then - fill = tonumber(fill) - if not fill or fill < 0 or fill > 255 then werror("bad filler") end - end - wputxb(fill or 0) -end -map_op[".space_1"] = map_op[".space_2"] - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - if reg and not map_reg_valid_base[reg] then - werror("bad base register `"..(map_reg_rev[reg] or reg).."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg and map_reg_rev[tp.reg] or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION") - wputxb(num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpregs(out) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dynasm.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dynasm.lua deleted file mode 100644 index 95251b9..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/dynasm/dynasm.lua +++ /dev/null @@ -1,1095 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM. A dynamic assembler for code generation engines. --- Originally designed and implemented for LuaJIT. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- See below for full copyright notice. ------------------------------------------------------------------------------- - --- Application information. -local _info = { - name = "DynASM", - description = "A dynamic assembler for code generation engines", - version = "1.5.0", - vernum = 10500, - release = "2021-05-02", - author = "Mike Pall", - url = "https://luajit.org/dynasm.html", - license = "MIT", - copyright = [[ -Copyright (C) 2005-2022 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -[ MIT license: https://www.opensource.org/licenses/mit-license.php ] -]], -} - --- Cache library functions. -local type, pairs, ipairs = type, pairs, ipairs -local pcall, error, assert = pcall, error, assert -local _s = string -local sub, match, gmatch, gsub = _s.sub, _s.match, _s.gmatch, _s.gsub -local format, rep, upper = _s.format, _s.rep, _s.upper -local _t = table -local insert, remove, concat, sort = _t.insert, _t.remove, _t.concat, _t.sort -local exit = os.exit -local io = io -local stdin, stdout, stderr = io.stdin, io.stdout, io.stderr - ------------------------------------------------------------------------------- - --- Program options. -local g_opt = {} - --- Global state for current file. -local g_fname, g_curline, g_indent, g_lineno, g_synclineno, g_arch -local g_errcount = 0 - --- Write buffer for output file. -local g_wbuffer, g_capbuffer - ------------------------------------------------------------------------------- - --- Write an output line (or callback function) to the buffer. -local function wline(line, needindent) - local buf = g_capbuffer or g_wbuffer - buf[#buf+1] = needindent and g_indent..line or line - g_synclineno = g_synclineno + 1 -end - --- Write assembler line as a comment, if requestd. -local function wcomment(aline) - if g_opt.comment then - wline(g_opt.comment..aline..g_opt.endcomment, true) - end -end - --- Resync CPP line numbers. -local function wsync() - if g_synclineno ~= g_lineno and g_opt.cpp then - wline("#line "..g_lineno..' "'..g_fname..'"') - g_synclineno = g_lineno - end -end - --- Dummy action flush function. Replaced with arch-specific function later. -local function wflush(term) -end - --- Dump all buffered output lines. -local function wdumplines(out, buf) - for _,line in ipairs(buf) do - if type(line) == "string" then - assert(out:write(line, "\n")) - else - -- Special callback to dynamically insert lines after end of processing. - line(out) - end - end -end - ------------------------------------------------------------------------------- - --- Emit an error. Processing continues with next statement. -local function werror(msg) - error(format("%s:%s: error: %s:\n%s", g_fname, g_lineno, msg, g_curline), 0) -end - --- Emit a fatal error. Processing stops. -local function wfatal(msg) - g_errcount = "fatal" - werror(msg) -end - --- Print a warning. Processing continues. -local function wwarn(msg) - stderr:write(format("%s:%s: warning: %s:\n%s\n", - g_fname, g_lineno, msg, g_curline)) -end - --- Print caught error message. But suppress excessive errors. -local function wprinterr(...) - if type(g_errcount) == "number" then - -- Regular error. - g_errcount = g_errcount + 1 - if g_errcount < 21 then -- Seems to be a reasonable limit. - stderr:write(...) - elseif g_errcount == 21 then - stderr:write(g_fname, - ":*: warning: too many errors (suppressed further messages).\n") - end - else - -- Fatal error. - stderr:write(...) - return true -- Stop processing. - end -end - ------------------------------------------------------------------------------- - --- Map holding all option handlers. -local opt_map = {} -local opt_current - --- Print error and exit with error status. -local function opterror(...) - stderr:write("dynasm.lua: ERROR: ", ...) - stderr:write("\n") - exit(1) -end - --- Get option parameter. -local function optparam(args) - local argn = args.argn - local p = args[argn] - if not p then - opterror("missing parameter for option `", opt_current, "'.") - end - args.argn = argn + 1 - return p -end - ------------------------------------------------------------------------------- - --- Core pseudo-opcodes. -local map_coreop = {} --- Dummy opcode map. Replaced by arch-specific map. -local map_op = {} - --- Forward declarations. -local dostmt -local readfile - ------------------------------------------------------------------------------- - --- Map for defines (initially empty, chains to arch-specific map). -local map_def = {} - --- Pseudo-opcode to define a substitution. -map_coreop[".define_2"] = function(params, nparams) - if not params then return nparams == 1 and "name" or "name, subst" end - local name, def = params[1], params[2] or "1" - if not match(name, "^[%a_][%w_]*$") then werror("bad or duplicate define") end - map_def[name] = def -end -map_coreop[".define_1"] = map_coreop[".define_2"] - --- Define a substitution on the command line. -function opt_map.D(args) - local namesubst = optparam(args) - local name, subst = match(namesubst, "^([%a_][%w_]*)=(.*)$") - if name then - map_def[name] = subst - elseif match(namesubst, "^[%a_][%w_]*$") then - map_def[namesubst] = "1" - else - opterror("bad define") - end -end - --- Undefine a substitution on the command line. -function opt_map.U(args) - local name = optparam(args) - if match(name, "^[%a_][%w_]*$") then - map_def[name] = nil - else - opterror("bad define") - end -end - --- Helper for definesubst. -local gotsubst - -local function definesubst_one(word) - local subst = map_def[word] - if subst then gotsubst = word; return subst else return word end -end - --- Iteratively substitute defines. -local function definesubst(stmt) - -- Limit number of iterations. - for i=1,100 do - gotsubst = false - stmt = gsub(stmt, "#?[%w_]+", definesubst_one) - if not gotsubst then break end - end - if gotsubst then wfatal("recursive define involving `"..gotsubst.."'") end - return stmt -end - --- Dump all defines. -local function dumpdefines(out, lvl) - local t = {} - for name in pairs(map_def) do - t[#t+1] = name - end - sort(t) - out:write("Defines:\n") - for _,name in ipairs(t) do - local subst = map_def[name] - if g_arch then subst = g_arch.revdef(subst) end - out:write(format(" %-20s %s\n", name, subst)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Support variables for conditional assembly. -local condlevel = 0 -local condstack = {} - --- Evaluate condition with a Lua expression. Substitutions already performed. -local function cond_eval(cond) - local func, err - if setfenv then - func, err = loadstring("return "..cond, "=expr") - else - -- No globals. All unknown identifiers evaluate to nil. - func, err = load("return "..cond, "=expr", "t", {}) - end - if func then - if setfenv then - setfenv(func, {}) -- No globals. All unknown identifiers evaluate to nil. - end - local ok, res = pcall(func) - if ok then - if res == 0 then return false end -- Oh well. - return not not res - end - err = res - end - wfatal("bad condition: "..err) -end - --- Skip statements until next conditional pseudo-opcode at the same level. -local function stmtskip() - local dostmt_save = dostmt - local lvl = 0 - dostmt = function(stmt) - local op = match(stmt, "^%s*(%S+)") - if op == ".if" then - lvl = lvl + 1 - elseif lvl ~= 0 then - if op == ".endif" then lvl = lvl - 1 end - elseif op == ".elif" or op == ".else" or op == ".endif" then - dostmt = dostmt_save - dostmt(stmt) - end - end -end - --- Pseudo-opcodes for conditional assembly. -map_coreop[".if_1"] = function(params) - if not params then return "condition" end - local lvl = condlevel + 1 - local res = cond_eval(params[1]) - condlevel = lvl - condstack[lvl] = res - if not res then stmtskip() end -end - -map_coreop[".elif_1"] = function(params) - if not params then return "condition" end - if condlevel == 0 then wfatal(".elif without .if") end - local lvl = condlevel - local res = condstack[lvl] - if res then - if res == "else" then wfatal(".elif after .else") end - else - res = cond_eval(params[1]) - if res then - condstack[lvl] = res - return - end - end - stmtskip() -end - -map_coreop[".else_0"] = function(params) - if condlevel == 0 then wfatal(".else without .if") end - local lvl = condlevel - local res = condstack[lvl] - condstack[lvl] = "else" - if res then - if res == "else" then wfatal(".else after .else") end - stmtskip() - end -end - -map_coreop[".endif_0"] = function(params) - local lvl = condlevel - if lvl == 0 then wfatal(".endif without .if") end - condlevel = lvl - 1 -end - --- Check for unfinished conditionals. -local function checkconds() - if g_errcount ~= "fatal" and condlevel ~= 0 then - wprinterr(g_fname, ":*: error: unbalanced conditional\n") - end -end - ------------------------------------------------------------------------------- - --- Search for a file in the given path and open it for reading. -local function pathopen(path, name) - local dirsep = package and match(package.path, "\\") and "\\" or "/" - for _,p in ipairs(path) do - local fullname = p == "" and name or p..dirsep..name - local fin = io.open(fullname, "r") - if fin then - g_fname = fullname - return fin - end - end -end - --- Include a file. -map_coreop[".include_1"] = function(params) - if not params then return "filename" end - local name = params[1] - -- Save state. Ugly, I know. but upvalues are fast. - local gf, gl, gcl, gi = g_fname, g_lineno, g_curline, g_indent - -- Read the included file. - local fatal = readfile(pathopen(g_opt.include, name) or - wfatal("include file `"..name.."' not found")) - -- Restore state. - g_synclineno = -1 - g_fname, g_lineno, g_curline, g_indent = gf, gl, gcl, gi - if fatal then wfatal("in include file") end -end - --- Make .include and conditionals initially available, too. -map_op[".include_1"] = map_coreop[".include_1"] -map_op[".if_1"] = map_coreop[".if_1"] -map_op[".elif_1"] = map_coreop[".elif_1"] -map_op[".else_0"] = map_coreop[".else_0"] -map_op[".endif_0"] = map_coreop[".endif_0"] - ------------------------------------------------------------------------------- - --- Support variables for macros. -local mac_capture, mac_lineno, mac_name -local mac_active = {} -local mac_list = {} - --- Pseudo-opcode to define a macro. -map_coreop[".macro_*"] = function(mparams) - if not mparams then return "name [, params...]" end - -- Split off and validate macro name. - local name = remove(mparams, 1) - if not name then werror("missing macro name") end - if not (match(name, "^[%a_][%w_%.]*$") or match(name, "^%.[%w_%.]*$")) then - wfatal("bad macro name `"..name.."'") - end - -- Validate macro parameter names. - local mdup = {} - for _,mp in ipairs(mparams) do - if not match(mp, "^[%a_][%w_]*$") then - wfatal("bad macro parameter name `"..mp.."'") - end - if mdup[mp] then wfatal("duplicate macro parameter name `"..mp.."'") end - mdup[mp] = true - end - -- Check for duplicate or recursive macro definitions. - local opname = name.."_"..#mparams - if map_op[opname] or map_op[name.."_*"] then - wfatal("duplicate macro `"..name.."' ("..#mparams.." parameters)") - end - if mac_capture then wfatal("recursive macro definition") end - - -- Enable statement capture. - local lines = {} - mac_lineno = g_lineno - mac_name = name - mac_capture = function(stmt) -- Statement capture function. - -- Stop macro definition with .endmacro pseudo-opcode. - if not match(stmt, "^%s*.endmacro%s*$") then - lines[#lines+1] = stmt - return - end - mac_capture = nil - mac_lineno = nil - mac_name = nil - mac_list[#mac_list+1] = opname - -- Add macro-op definition. - map_op[opname] = function(params) - if not params then return mparams, lines end - -- Protect against recursive macro invocation. - if mac_active[opname] then wfatal("recursive macro invocation") end - mac_active[opname] = true - -- Setup substitution map. - local subst = {} - for i,mp in ipairs(mparams) do subst[mp] = params[i] end - local mcom - if g_opt.maccomment and g_opt.comment then - mcom = " MACRO "..name.." ("..#mparams..")" - wcomment("{"..mcom) - end - -- Loop through all captured statements - for _,stmt in ipairs(lines) do - -- Substitute macro parameters. - local st = gsub(stmt, "[%w_]+", subst) - st = definesubst(st) - st = gsub(st, "%s*%.%.%s*", "") -- Token paste a..b. - if mcom and sub(st, 1, 1) ~= "|" then wcomment(st) end - -- Emit statement. Use a protected call for better diagnostics. - local ok, err = pcall(dostmt, st) - if not ok then - -- Add the captured statement to the error. - wprinterr(err, "\n", g_indent, "| ", stmt, - "\t[MACRO ", name, " (", #mparams, ")]\n") - end - end - if mcom then wcomment("}"..mcom) end - mac_active[opname] = nil - end - end -end - --- An .endmacro pseudo-opcode outside of a macro definition is an error. -map_coreop[".endmacro_0"] = function(params) - wfatal(".endmacro without .macro") -end - --- Dump all macros and their contents (with -PP only). -local function dumpmacros(out, lvl) - sort(mac_list) - out:write("Macros:\n") - for _,opname in ipairs(mac_list) do - local name = sub(opname, 1, -3) - local params, lines = map_op[opname]() - out:write(format(" %-20s %s\n", name, concat(params, ", "))) - if lvl > 1 then - for _,line in ipairs(lines) do - out:write(" |", line, "\n") - end - out:write("\n") - end - end - out:write("\n") -end - --- Check for unfinished macro definitions. -local function checkmacros() - if mac_capture then - wprinterr(g_fname, ":", mac_lineno, - ": error: unfinished .macro `", mac_name ,"'\n") - end -end - ------------------------------------------------------------------------------- - --- Support variables for captures. -local cap_lineno, cap_name -local cap_buffers = {} -local cap_used = {} - --- Start a capture. -map_coreop[".capture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - if cap_name then - wfatal("already capturing to `"..cap_name.."' since line "..cap_lineno) - end - cap_name = name - cap_lineno = g_lineno - -- Create or continue a capture buffer and start the output line capture. - local buf = cap_buffers[name] - if not buf then buf = {}; cap_buffers[name] = buf end - g_capbuffer = buf - g_synclineno = 0 -end - --- Stop a capture. -map_coreop[".endcapture_0"] = function(params) - wflush() - if not cap_name then wfatal(".endcapture without a valid .capture") end - cap_name = nil - cap_lineno = nil - g_capbuffer = nil - g_synclineno = 0 -end - --- Dump a capture buffer. -map_coreop[".dumpcapture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - cap_used[name] = true - wline(function(out) - local buf = cap_buffers[name] - if buf then wdumplines(out, buf) end - end) - g_synclineno = 0 -end - --- Dump all captures and their buffers (with -PP only). -local function dumpcaptures(out, lvl) - out:write("Captures:\n") - for name,buf in pairs(cap_buffers) do - out:write(format(" %-20s %4s)\n", name, "("..#buf)) - if lvl > 1 then - local bar = rep("=", 76) - out:write(" ", bar, "\n") - for _,line in ipairs(buf) do - out:write(" ", line, "\n") - end - out:write(" ", bar, "\n\n") - end - end - out:write("\n") -end - --- Check for unfinished or unused captures. -local function checkcaptures() - if cap_name then - wprinterr(g_fname, ":", cap_lineno, - ": error: unfinished .capture `", cap_name,"'\n") - return - end - for name in pairs(cap_buffers) do - if not cap_used[name] then - wprinterr(g_fname, ":*: error: missing .dumpcapture ", name ,"\n") - end - end -end - ------------------------------------------------------------------------------- - --- Sections names. -local map_sections = {} - --- Pseudo-opcode to define code sections. --- TODO: Data sections, BSS sections. Needs extra C code and API. -map_coreop[".section_*"] = function(params) - if not params then return "name..." end - if #map_sections > 0 then werror("duplicate section definition") end - wflush() - for sn,name in ipairs(params) do - local opname = "."..name.."_0" - if not match(name, "^[%a][%w_]*$") or - map_op[opname] or map_op["."..name.."_*"] then - werror("bad section name `"..name.."'") - end - map_sections[#map_sections+1] = name - wline(format("#define DASM_SECTION_%s\t%d", upper(name), sn-1)) - map_op[opname] = function(params) g_arch.section(sn-1) end - end - wline(format("#define DASM_MAXSECTION\t\t%d", #map_sections)) -end - --- Dump all sections. -local function dumpsections(out, lvl) - out:write("Sections:\n") - for _,name in ipairs(map_sections) do - out:write(format(" %s\n", name)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Replacement for customized Lua, which lacks the package library. -local prefix = "" -if not require then - function require(name) - local fp = assert(io.open(prefix..name..".lua")) - local s = fp:read("*a") - assert(fp:close()) - return assert(loadstring(s, "@"..name..".lua"))() - end -end - --- Load architecture-specific module. -local function loadarch(arch) - if not match(arch, "^[%w_]+$") then return "bad arch name" end - _G._map_def = map_def - local ok, m_arch = pcall(require, "dasm_"..arch) - if not ok then return "cannot load module: "..m_arch end - g_arch = m_arch - wflush = m_arch.passcb(wline, werror, wfatal, wwarn) - m_arch.setup(arch, g_opt) - map_op, map_def = m_arch.mergemaps(map_coreop, map_def) -end - --- Dump architecture description. -function opt_map.dumparch(args) - local name = optparam(args) - if not g_arch then - local err = loadarch(name) - if err then opterror(err) end - end - - local t = {} - for name in pairs(map_coreop) do t[#t+1] = name end - for name in pairs(map_op) do t[#t+1] = name end - sort(t) - - local out = stdout - local _arch = g_arch._info - out:write(format("%s version %s, released %s, %s\n", - _info.name, _info.version, _info.release, _info.url)) - g_arch.dumparch(out) - - local pseudo = true - out:write("Pseudo-Opcodes:\n") - for _,sname in ipairs(t) do - local name, nparam = match(sname, "^(.+)_([0-9%*])$") - if name then - if pseudo and sub(name, 1, 1) ~= "." then - out:write("\nOpcodes:\n") - pseudo = false - end - local f = map_op[sname] - local s - if nparam ~= "*" then nparam = nparam + 0 end - if nparam == 0 then - s = "" - elseif type(f) == "string" then - s = map_op[".template__"](nil, f, nparam) - else - s = f(nil, nparam) - end - if type(s) == "table" then - for _,s2 in ipairs(s) do - out:write(format(" %-12s %s\n", name, s2)) - end - else - out:write(format(" %-12s %s\n", name, s)) - end - end - end - out:write("\n") - exit(0) -end - --- Pseudo-opcode to set the architecture. --- Only initially available (map_op is replaced when called). -map_op[".arch_1"] = function(params) - if not params then return "name" end - local err = loadarch(params[1]) - if err then wfatal(err) end - wline(format("#if DASM_VERSION != %d", _info.vernum)) - wline('#error "Version mismatch between DynASM and included encoding engine"') - wline("#endif") -end - --- Dummy .arch pseudo-opcode to improve the error report. -map_coreop[".arch_1"] = function(params) - if not params then return "name" end - wfatal("duplicate .arch statement") -end - ------------------------------------------------------------------------------- - --- Dummy pseudo-opcode. Don't confuse '.nop' with 'nop'. -map_coreop[".nop_*"] = function(params) - if not params then return "[ignored...]" end -end - --- Pseudo-opcodes to raise errors. -map_coreop[".error_1"] = function(params) - if not params then return "message" end - werror(params[1]) -end - -map_coreop[".fatal_1"] = function(params) - if not params then return "message" end - wfatal(params[1]) -end - --- Dump all user defined elements. -local function dumpdef(out) - local lvl = g_opt.dumpdef - if lvl == 0 then return end - dumpsections(out, lvl) - dumpdefines(out, lvl) - if g_arch then g_arch.dumpdef(out, lvl) end - dumpmacros(out, lvl) - dumpcaptures(out, lvl) -end - ------------------------------------------------------------------------------- - --- Helper for splitstmt. -local splitlvl - -local function splitstmt_one(c) - if c == "(" then - splitlvl = ")"..splitlvl - elseif c == "[" then - splitlvl = "]"..splitlvl - elseif c == "{" then - splitlvl = "}"..splitlvl - elseif c == ")" or c == "]" or c == "}" then - if sub(splitlvl, 1, 1) ~= c then werror("unbalanced (), [] or {}") end - splitlvl = sub(splitlvl, 2) - elseif splitlvl == "" then - return " \0 " - end - return c -end - --- Split statement into (pseudo-)opcode and params. -local function splitstmt(stmt) - -- Convert label with trailing-colon into .label statement. - local label = match(stmt, "^%s*(.+):%s*$") - if label then return ".label", {label} end - - -- Split at commas and equal signs, but obey parentheses and brackets. - splitlvl = "" - stmt = gsub(stmt, "[,%(%)%[%]{}]", splitstmt_one) - if splitlvl ~= "" then werror("unbalanced () or []") end - - -- Split off opcode. - local op, other = match(stmt, "^%s*([^%s%z]+)%s*(.*)$") - if not op then werror("bad statement syntax") end - - -- Split parameters. - local params = {} - for p in gmatch(other, "%s*(%Z+)%z?") do - params[#params+1] = gsub(p, "%s+$", "") - end - if #params > 16 then werror("too many parameters") end - - params.op = op - return op, params -end - --- Process a single statement. -dostmt = function(stmt) - -- Ignore empty statements. - if match(stmt, "^%s*$") then return end - - -- Capture macro defs before substitution. - if mac_capture then return mac_capture(stmt) end - stmt = definesubst(stmt) - - -- Emit C code without parsing the line. - if sub(stmt, 1, 1) == "|" then - local tail = sub(stmt, 2) - wflush() - if sub(tail, 1, 2) == "//" then wcomment(tail) else wline(tail, true) end - return - end - - -- Split into (pseudo-)opcode and params. - local op, params = splitstmt(stmt) - - -- Get opcode handler (matching # of parameters or generic handler). - local f = map_op[op.."_"..#params] or map_op[op.."_*"] - if not f then - if not g_arch then wfatal("first statement must be .arch") end - -- Improve error report. - for i=0,9 do - if map_op[op.."_"..i] then - werror("wrong number of parameters for `"..op.."'") - end - end - werror("unknown statement `"..op.."'") - end - - -- Call opcode handler or special handler for template strings. - if type(f) == "string" then - map_op[".template__"](params, f) - else - f(params) - end -end - --- Process a single line. -local function doline(line) - if g_opt.flushline then wflush() end - - -- Assembler line? - local indent, aline = match(line, "^(%s*)%|(.*)$") - if not aline then - -- No, plain C code line, need to flush first. - wflush() - wsync() - wline(line, false) - return - end - - g_indent = indent -- Remember current line indentation. - - -- Emit C code (even from macros). Avoids echo and line parsing. - if sub(aline, 1, 1) == "|" then - if not mac_capture then - wsync() - elseif g_opt.comment then - wsync() - wcomment(aline) - end - dostmt(aline) - return - end - - -- Echo assembler line as a comment. - if g_opt.comment then - wsync() - wcomment(aline) - end - - -- Strip assembler comments. - aline = gsub(aline, "//.*$", "") - - -- Split line into statements at semicolons. - if match(aline, ";") then - for stmt in gmatch(aline, "[^;]+") do dostmt(stmt) end - else - dostmt(aline) - end -end - ------------------------------------------------------------------------------- - --- Write DynASM header. -local function dasmhead(out) - out:write(format([[ -/* -** This file has been pre-processed with DynASM. -** %s -** DynASM version %s, DynASM %s version %s -** DO NOT EDIT! The original file is in "%s". -*/ - -]], _info.url, - _info.version, g_arch._info.arch, g_arch._info.version, - g_fname)) -end - --- Read input file. -readfile = function(fin) - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Process all lines. - for line in fin:lines() do - g_lineno = g_lineno + 1 - g_curline = line - local ok, err = pcall(doline, line) - if not ok and wprinterr(err, "\n") then return true end - end - wflush() - - -- Close input file. - assert(fin == stdin or fin:close()) -end - --- Write output file. -local function writefile(outfile) - local fout - - -- Open output file. - if outfile == nil or outfile == "-" then - fout = stdout - else - fout = assert(io.open(outfile, "w")) - end - - -- Write all buffered lines - wdumplines(fout, g_wbuffer) - - -- Close output file. - assert(fout == stdout or fout:close()) - - -- Optionally dump definitions. - dumpdef(fout == stdout and stderr or stdout) -end - --- Translate an input file to an output file. -local function translate(infile, outfile) - g_wbuffer = {} - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Put header. - wline(dasmhead) - - -- Read input file. - local fin - if infile == "-" then - g_fname = "(stdin)" - fin = stdin - else - g_fname = infile - fin = assert(io.open(infile, "r")) - end - readfile(fin) - - -- Check for errors. - if not g_arch then - wprinterr(g_fname, ":*: error: missing .arch directive\n") - end - checkconds() - checkmacros() - checkcaptures() - - if g_errcount ~= 0 then - stderr:write(g_fname, ":*: info: ", g_errcount, " error", - (type(g_errcount) == "number" and g_errcount > 1) and "s" or "", - " in input file -- no output file generated.\n") - dumpdef(stderr) - exit(1) - end - - -- Write output file. - writefile(outfile) -end - ------------------------------------------------------------------------------- - --- Print help text. -function opt_map.help() - stdout:write("DynASM -- ", _info.description, ".\n") - stdout:write("DynASM ", _info.version, " ", _info.release, " ", _info.url, "\n") - stdout:write[[ - -Usage: dynasm [OPTION]... INFILE.dasc|- - - -h, --help Display this help text. - -V, --version Display version and copyright information. - - -o, --outfile FILE Output file name (default is stdout). - -I, --include DIR Add directory to the include search path. - - -c, --ccomment Use /* */ comments for assembler lines. - -C, --cppcomment Use // comments for assembler lines (default). - -N, --nocomment Suppress assembler lines in output. - -M, --maccomment Show macro expansions as comments (default off). - - -L, --nolineno Suppress CPP line number information in output. - -F, --flushline Flush action list for every line. - - -D NAME[=SUBST] Define a substitution. - -U NAME Undefine a substitution. - - -P, --dumpdef Dump defines, macros, etc. Repeat for more output. - -A, --dumparch ARCH Load architecture ARCH and dump description. -]] - exit(0) -end - --- Print version information. -function opt_map.version() - stdout:write(format("%s version %s, released %s\n%s\n\n%s", - _info.name, _info.version, _info.release, _info.url, _info.copyright)) - exit(0) -end - --- Misc. options. -function opt_map.outfile(args) g_opt.outfile = optparam(args) end -function opt_map.include(args) insert(g_opt.include, 1, optparam(args)) end -function opt_map.ccomment() g_opt.comment = "/*|"; g_opt.endcomment = " */" end -function opt_map.cppcomment() g_opt.comment = "//|"; g_opt.endcomment = "" end -function opt_map.nocomment() g_opt.comment = false end -function opt_map.maccomment() g_opt.maccomment = true end -function opt_map.nolineno() g_opt.cpp = false end -function opt_map.flushline() g_opt.flushline = true end -function opt_map.dumpdef() g_opt.dumpdef = g_opt.dumpdef + 1 end - ------------------------------------------------------------------------------- - --- Short aliases for long options. -local opt_alias = { - h = "help", ["?"] = "help", V = "version", - o = "outfile", I = "include", - c = "ccomment", C = "cppcomment", N = "nocomment", M = "maccomment", - L = "nolineno", F = "flushline", - P = "dumpdef", A = "dumparch", -} - --- Parse single option. -local function parseopt(opt, args) - opt_current = #opt == 1 and "-"..opt or "--"..opt - local f = opt_map[opt] or opt_map[opt_alias[opt]] - if not f then - opterror("unrecognized option `", opt_current, "'. Try `--help'.\n") - end - f(args) -end - --- Parse arguments. -local function parseargs(args) - -- Default options. - g_opt.comment = "//|" - g_opt.endcomment = "" - g_opt.cpp = true - g_opt.dumpdef = 0 - g_opt.include = { "" } - - -- Process all option arguments. - args.argn = 1 - repeat - local a = args[args.argn] - if not a then break end - local lopt, opt = match(a, "^%-(%-?)(.+)") - if not opt then break end - args.argn = args.argn + 1 - if lopt == "" then - -- Loop through short options. - for o in gmatch(opt, ".") do parseopt(o, args) end - else - -- Long option. - parseopt(opt, args) - end - until false - - -- Check for proper number of arguments. - local nargs = #args - args.argn + 1 - if nargs ~= 1 then - if nargs == 0 then - if g_opt.dumpdef > 0 then return dumpdef(stdout) end - end - opt_map.help() - end - - -- Translate a single input file to a single output file - -- TODO: Handle multiple files? - translate(args[args.argn], g_opt.outfile) -end - ------------------------------------------------------------------------------- - --- Add the directory dynasm.lua resides in to the Lua module search path. -local arg = arg -if arg and arg[0] then - prefix = match(arg[0], "^(.*[/\\])") - if package and prefix then package.path = prefix.."?.lua;"..package.path end -end - --- Start DynASM. -parseargs{...} - ------------------------------------------------------------------------------- - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.1 b/LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.1 deleted file mode 100644 index a979b47..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.1 +++ /dev/null @@ -1,88 +0,0 @@ -.TH luajit 1 "" "" "LuaJIT documentation" -.SH NAME -luajit \- Just-In-Time Compiler for the Lua Language -\fB -.SH SYNOPSIS -.B luajit -[\fIoptions\fR]... [\fIscript\fR [\fIargs\fR]...] -.SH "WEB SITE" -.IR https://luajit.org -.SH DESCRIPTION -.PP -This is the command-line program to run Lua programs with \fBLuaJIT\fR. -.PP -\fBLuaJIT\fR is a just-in-time (JIT) compiler for the Lua language. -The virtual machine (VM) is based on a fast interpreter combined with -a trace compiler. It can significantly improve the performance of Lua programs. -.PP -\fBLuaJIT\fR is API\- and ABI-compatible with the VM of the standard -Lua\ 5.1 interpreter. When embedding the VM into an application, -the built library can be used as a drop-in replacement. -.SH OPTIONS -.TP -.BI "\-e " chunk -Run the given chunk of Lua code. -.TP -.BI "\-l " library -Load the named library, just like \fBrequire("\fR\fIlibrary\fR\fB")\fR. -.TP -.BI "\-b " ... -Save or list bytecode. Run without arguments to get help on options. -.TP -.BI "\-j " command -Perform LuaJIT control command (optional space after \fB\-j\fR). -.TP -.BI "\-O" [opt] -Control LuaJIT optimizations. -.TP -.B "\-i" -Run in interactive mode. -.TP -.B "\-v" -Show \fBLuaJIT\fR version. -.TP -.B "\-E" -Ignore environment variables. -.TP -.B "\-\-" -Stop processing options. -.TP -.B "\-" -Read script from stdin instead. -.PP -After all options are processed, the given \fIscript\fR is run. -The arguments are passed in the global \fIarg\fR table. -.PP -Interactive mode is only entered, if no \fIscript\fR and no \fB\-e\fR -option is given. Interactive mode can be left with EOF (\fICtrl\-Z\fB). -.SH EXAMPLES -.TP -luajit hello.lua world - -Prints "Hello world", assuming \fIhello.lua\fR contains: -.br - print("Hello", arg[1]) -.TP -luajit \-e "local x=0; for i=1,1e9 do x=x+i end; print(x)" - -Calculates the sum of the numbers from 1 to 1000000000. -.br -And finishes in a reasonable amount of time, too. -.TP -luajit \-jv \-e "for i=1,10 do for j=1,10 do for k=1,100 do end end end" - -Runs some nested loops and shows the resulting traces. -.SH COPYRIGHT -.PP -\fBLuaJIT\fR is Copyright \(co 2005-2022 Mike Pall. -.br -\fBLuaJIT\fR is open source software, released under the MIT license. -.SH SEE ALSO -.PP -More details in the provided HTML docs or at: -.IR https://luajit.org -.br -More about the Lua language can be found at: -.IR https://lua.org/docs.html -.PP -lua(1) diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.pc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.pc deleted file mode 100644 index 39e1e57..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/etc/luajit.pc +++ /dev/null @@ -1,25 +0,0 @@ -# Package information for LuaJIT to be used by pkg-config. -majver=2 -minver=1 -relver=0 -version=${majver}.${minver}.${relver}-beta3 -abiver=5.1 - -prefix=/usr/local -multilib=lib -exec_prefix=${prefix} -libdir=${exec_prefix}/${multilib} -libname=luajit-${abiver} -includedir=${prefix}/include/luajit-${majver}.${minver} - -INSTALL_LMOD=${prefix}/share/lua/${abiver} -INSTALL_CMOD=${prefix}/${multilib}/lua/${abiver} - -Name: LuaJIT -Description: Just-in-time compiler for Lua -URL: https://luajit.org -Version: ${version} -Requires: -Libs: -L${libdir} -l${libname} -Libs.private: -Wl,-E -lm -ldl -Cflags: -I${includedir} diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/.gitignore b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/.gitignore deleted file mode 100644 index 1a30573..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -luajit -lj_bcdef.h -lj_ffdef.h -lj_libdef.h -lj_recdef.h -lj_folddef.h -lj_vm.[sS] diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/Makefile.dep b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/Makefile.dep deleted file mode 100644 index 1ad6701..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/Makefile.dep +++ /dev/null @@ -1,259 +0,0 @@ -lib_aux.o: lib_aux.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_state.h lj_trace.h lj_jit.h lj_ir.h \ - lj_dispatch.h lj_bc.h lj_traceerr.h lj_lib.h -lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_buf.h \ - lj_str.h lj_tab.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h \ - lj_cconv.h lj_ff.h lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h \ - lj_strscan.h lj_strfmt.h lj_lib.h lj_libdef.h -lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_strscan.h \ - lj_strfmt.h lj_ctype.h lj_cdata.h lj_cconv.h lj_carith.h lj_ff.h \ - lj_ffdef.h lj_lib.h lj_libdef.h -lib_buffer.o: lib_buffer.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h \ - lj_tab.h lj_udata.h lj_meta.h lj_ctype.h lj_cdata.h lj_cconv.h \ - lj_strfmt.h lj_serialize.h lj_lib.h lj_libdef.h -lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \ - lj_libdef.h -lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h \ - lj_ctype.h lj_cparse.h lj_cdata.h lj_cconv.h lj_carith.h lj_ccall.h \ - lj_ccallback.h lj_clib.h lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h \ - lj_libdef.h -lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h -lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h \ - lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h -lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \ - lj_state.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h \ - lj_target.h lj_target_*.h lj_trace.h lj_dispatch.h lj_traceerr.h \ - lj_vm.h lj_vmevent.h lj_lib.h luajit.h lj_libdef.h -lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_prng.h lj_libdef.h -lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \ - lj_libdef.h -lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h -lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h \ - lj_tab.h lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h \ - lj_char.h lj_strfmt.h lj_lib.h lj_libdef.h -lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h \ - lj_tab.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h -lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h \ - lj_prng.h -lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ - lj_meta.h lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h \ - lj_dispatch.h lj_traceerr.h lj_vm.h lj_strscan.h lj_strfmt.h -lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_buf.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h \ - lj_jit.h lj_ircall.h lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h \ - lj_traceerr.h lj_snap.h lj_asm.h lj_vm.h lj_target.h lj_target_*.h \ - lj_emit_*.h lj_asm_*.h -lj_assert.o: lj_assert.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h -lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ - lj_bcdef.h -lj_bcread.o: lj_bcread.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_bc.h \ - lj_ctype.h lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h \ - lj_strfmt.h -lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_buf.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h \ - lj_ir.h lj_strfmt.h lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h -lj_buf.o: lj_buf.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_strfmt.h -lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ir.h lj_ctype.h \ - lj_cconv.h lj_cdata.h lj_carith.h lj_strscan.h -lj_ccall.o: lj_ccall.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_cconv.h lj_cdata.h \ - lj_ccall.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ - lj_traceerr.h -lj_ccallback.o: lj_ccallback.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_state.h lj_frame.h \ - lj_bc.h lj_ctype.h lj_cconv.h lj_ccall.h lj_ccallback.h lj_target.h \ - lj_target_*.h lj_mcode.h lj_jit.h lj_ir.h lj_trace.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h -lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_tab.h lj_ctype.h \ - lj_cdata.h lj_cconv.h lj_ccallback.h -lj_cdata.o: lj_cdata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_cconv.h lj_cdata.h -lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h -lj_clib.o: lj_clib.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_tab.h lj_str.h lj_udata.h lj_ctype.h lj_cconv.h \ - lj_cdata.h lj_clib.h lj_strfmt.h -lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_ctype.h lj_cparse.h \ - lj_frame.h lj_bc.h lj_vm.h lj_char.h lj_strscan.h lj_strfmt.h -lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_gc.h \ - lj_cdata.h lj_cparse.h lj_cconv.h lj_carith.h lj_clib.h lj_ccall.h \ - lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h lj_snap.h \ - lj_crecord.h lj_strfmt.h -lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_strfmt.h lj_ctype.h \ - lj_ccallback.h lj_buf.h -lj_debug.o: lj_debug.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_debug.h lj_buf.h lj_gc.h lj_str.h lj_tab.h \ - lj_state.h lj_frame.h lj_bc.h lj_strfmt.h lj_jit.h lj_ir.h -lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_func.h lj_tab.h \ - lj_meta.h lj_debug.h lj_state.h lj_frame.h lj_bc.h lj_ff.h lj_ffdef.h \ - lj_strfmt.h lj_jit.h lj_ir.h lj_ccallback.h lj_ctype.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_profile.h lj_vm.h luajit.h -lj_err.o: lj_err.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_err.h \ - lj_errmsg.h lj_debug.h lj_str.h lj_func.h lj_state.h lj_frame.h lj_bc.h \ - lj_ff.h lj_ffdef.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h lj_strfmt.h -lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_tab.h lj_frame.h \ - lj_bc.h lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h \ - lj_trace.h lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h \ - lj_crecord.h lj_vm.h lj_strscan.h lj_strfmt.h lj_serialize.h lj_recdef.h -lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ - lj_traceerr.h lj_vm.h -lj_gc.o: lj_gc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ - lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h lj_trace.h \ - lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h -lj_gdbjit.o: lj_gdbjit.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_frame.h lj_bc.h lj_buf.h \ - lj_str.h lj_strfmt.h lj_jit.h lj_ir.h lj_dispatch.h -lj_ir.o: lj_ir.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_buf.h lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h \ - lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h lj_ctype.h lj_cdata.h \ - lj_carith.h lj_vm.h lj_strscan.h lj_serialize.h lj_strfmt.h lj_prng.h -lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_ctype.h lj_cdata.h \ - lualib.h lj_state.h lj_lex.h lj_parse.h lj_char.h lj_strscan.h \ - lj_strfmt.h -lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \ - lj_dispatch.h lj_jit.h lj_ir.h lj_ctype.h lj_vm.h lj_strscan.h \ - lj_strfmt.h lj_lex.h lj_bcdump.h lj_lib.h -lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ - lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_func.h \ - lj_frame.h lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h -lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h \ - lj_dispatch.h lj_bc.h lj_traceerr.h lj_prng.h lj_vm.h -lj_meta.o: lj_meta.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_meta.h lj_frame.h \ - lj_bc.h lj_vm.h lj_strscan.h lj_strfmt.h lj_lib.h -lj_obj.o: lj_obj.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h -lj_opt_dce.o: lj_opt_dce.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_ir.h lj_jit.h lj_iropt.h -lj_opt_fold.o: lj_opt_fold.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_buf.h lj_gc.h lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_ircall.h \ - lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h lj_ctype.h \ - lj_carith.h lj_vm.h lj_strscan.h lj_strfmt.h lj_folddef.h -lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_ir.h lj_jit.h \ - lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h \ - lj_vm.h -lj_opt_mem.o: lj_opt_mem.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_tab.h lj_ir.h lj_jit.h lj_iropt.h lj_ircall.h lj_dispatch.h lj_bc.h -lj_opt_narrow.o: lj_opt_narrow.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h lj_strscan.h -lj_opt_sink.o: lj_opt_sink.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_ir.h lj_jit.h lj_iropt.h lj_target.h lj_target_*.h -lj_opt_split.o: lj_opt_split.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_ir.h \ - lj_jit.h lj_ircall.h lj_iropt.h lj_dispatch.h lj_bc.h lj_vm.h -lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_buf.h lj_str.h lj_tab.h \ - lj_func.h lj_state.h lj_bc.h lj_ctype.h lj_strfmt.h lj_lex.h lj_parse.h \ - lj_vm.h lj_vmevent.h -lj_prng.o: lj_prng.c lj_def.h lua.h luaconf.h lj_arch.h lj_prng.h -lj_profile.o: lj_profile.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_buf.h lj_gc.h lj_str.h lj_frame.h lj_bc.h lj_debug.h lj_dispatch.h \ - lj_jit.h lj_ir.h lj_trace.h lj_traceerr.h lj_profile.h luajit.h -lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ - lj_ctype.h lj_gc.h lj_ff.h lj_ffdef.h lj_debug.h lj_ir.h lj_jit.h \ - lj_ircall.h lj_iropt.h lj_trace.h lj_dispatch.h lj_traceerr.h \ - lj_record.h lj_ffrecord.h lj_snap.h lj_vm.h lj_prng.h -lj_serialize.o: lj_serialize.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_tab.h \ - lj_udata.h lj_ctype.h lj_cdata.h lj_ir.h lj_serialize.h -lj_snap.o: lj_snap.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_tab.h lj_state.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \ - lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h \ - lj_target_*.h lj_ctype.h lj_cdata.h -lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_func.h \ - lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h \ - lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_prng.h lj_lex.h \ - lj_alloc.h luajit.h -lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_str.h lj_char.h lj_prng.h -lj_strfmt.o: lj_strfmt.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_meta.h lj_state.h \ - lj_char.h lj_strfmt.h lj_ctype.h lj_lib.h -lj_strfmt_num.o: lj_strfmt_num.c lj_obj.h lua.h luaconf.h lj_def.h \ - lj_arch.h lj_buf.h lj_gc.h lj_str.h lj_strfmt.h -lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_char.h lj_strscan.h -lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ - lj_err.h lj_errmsg.h lj_tab.h -lj_trace.o: lj_trace.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_frame.h lj_bc.h \ - lj_state.h lj_ir.h lj_jit.h lj_iropt.h lj_mcode.h lj_trace.h \ - lj_dispatch.h lj_traceerr.h lj_snap.h lj_gdbjit.h lj_record.h lj_asm.h \ - lj_vm.h lj_vmevent.h lj_target.h lj_target_*.h lj_prng.h -lj_udata.o: lj_udata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_udata.h -lj_vmevent.o: lj_vmevent.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_str.h lj_tab.h lj_state.h lj_dispatch.h lj_bc.h lj_jit.h lj_ir.h \ - lj_vm.h lj_vmevent.h -lj_vmmath.o: lj_vmmath.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_ir.h lj_vm.h -ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_assert.c lj_obj.h \ - lj_def.h lj_arch.h lj_gc.c lj_gc.h lj_err.h lj_errmsg.h lj_buf.h \ - lj_str.h lj_tab.h lj_func.h lj_udata.h lj_meta.h lj_state.h lj_frame.h \ - lj_bc.h lj_ctype.h lj_cdata.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \ - lj_traceerr.h lj_vm.h lj_err.c lj_debug.h lj_ff.h lj_ffdef.h lj_strfmt.h \ - lj_char.c lj_char.h lj_bc.c lj_bcdef.h lj_obj.c lj_buf.c lj_str.c \ - lj_prng.h lj_tab.c lj_func.c lj_udata.c lj_meta.c lj_strscan.h lj_lib.h \ - lj_debug.c lj_prng.c lj_state.c lj_lex.h lj_alloc.h luajit.h \ - lj_dispatch.c lj_ccallback.h lj_profile.h lj_vmevent.c lj_vmevent.h \ - lj_vmmath.c lj_strscan.c lj_strfmt.c lj_strfmt_num.c lj_serialize.c \ - lj_serialize.h lj_api.c lj_profile.c lj_lex.c lualib.h lj_parse.h \ - lj_parse.c lj_bcread.c lj_bcdump.h lj_bcwrite.c lj_load.c lj_ctype.c \ - lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h lj_ccallback.c \ - lj_target.h lj_target_*.h lj_mcode.h lj_carith.c lj_carith.h lj_clib.c \ - lj_clib.h lj_cparse.c lj_cparse.h lj_lib.c lj_ir.c lj_ircall.h \ - lj_iropt.h lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c \ - lj_opt_dce.c lj_opt_loop.c lj_snap.h lj_opt_split.c lj_opt_sink.c \ - lj_mcode.c lj_snap.c lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \ - lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h \ - lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \ - lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c \ - lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c \ - lib_buffer.c lib_init.c -luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h -host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \ - lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \ - lj_ircall.h lj_ir.h lj_jit.h lj_frame.h lj_bc.h lj_dispatch.h lj_ctype.h \ - lj_gc.h lj_ccall.h lj_ctype.h luajit.h \ - host/buildvm_arch.h lj_traceerr.h -host/buildvm_asm.o: host/buildvm_asm.c host/buildvm.h lj_def.h lua.h luaconf.h \ - lj_arch.h lj_bc.h lj_def.h lj_arch.h -host/buildvm_fold.o: host/buildvm_fold.c host/buildvm.h lj_def.h lua.h \ - luaconf.h lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_ir.h lj_obj.h -host/buildvm_lib.o: host/buildvm_lib.c host/buildvm.h lj_def.h lua.h luaconf.h \ - lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_bc.h lj_lib.h lj_obj.h \ - host/buildvm_libbc.h -host/buildvm_peobj.o: host/buildvm_peobj.c host/buildvm.h lj_def.h lua.h \ - luaconf.h lj_arch.h lj_bc.h lj_def.h lj_arch.h -host/minilua.o: host/minilua.c diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/.gitignore b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/.gitignore deleted file mode 100644 index 762ac2a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -minilua -buildvm -buildvm_arch.h diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/README b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/README deleted file mode 100644 index abfcdaa..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/README +++ /dev/null @@ -1,4 +0,0 @@ -The files in this directory are only used during the build process of LuaJIT. -For cross-compilation, they must be executed on the host, not on the target. - -These files should NOT be installed! diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.c deleted file mode 100644 index 9ee47ad..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.c +++ /dev/null @@ -1,528 +0,0 @@ -/* -** LuaJIT VM builder. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** This is a tool to build the hand-tuned assembler code required for -** LuaJIT's bytecode interpreter. It supports a variety of output formats -** to feed different toolchains (see usage() below). -** -** This tool is not particularly optimized because it's only used while -** _building_ LuaJIT. There's no point in distributing or installing it. -** Only the object code generated by this tool is linked into LuaJIT. -** -** Caveat: some memory is not free'd, error handling is lazy. -** It's a one-shot tool -- any effort fixing this would be wasted. -*/ - -#include "buildvm.h" -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_bc.h" -#if LJ_HASJIT -#include "lj_ir.h" -#include "lj_ircall.h" -#endif -#include "lj_frame.h" -#include "lj_dispatch.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_ccall.h" -#endif -#include "luajit.h" - -#if defined(_WIN32) -#include -#include -#endif - -/* ------------------------------------------------------------------------ */ - -/* DynASM glue definitions. */ -#define Dst ctx -#define Dst_DECL BuildCtx *ctx -#define Dst_REF (ctx->D) -#define DASM_CHECKS 1 - -#include "../dynasm/dasm_proto.h" - -/* Glue macros for DynASM. */ -static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type); - -#define DASM_EXTERN(ctx, addr, idx, type) \ - collect_reloc(ctx, addr, idx, type) - -/* ------------------------------------------------------------------------ */ - -/* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */ -#define DASM_ALIGNED_WRITES 1 - -/* Embed architecture-specific DynASM encoder. */ -#if LJ_TARGET_X86ORX64 -#include "../dynasm/dasm_x86.h" -#elif LJ_TARGET_ARM -#include "../dynasm/dasm_arm.h" -#elif LJ_TARGET_ARM64 -#include "../dynasm/dasm_arm64.h" -#elif LJ_TARGET_PPC -#include "../dynasm/dasm_ppc.h" -#elif LJ_TARGET_MIPS -#include "../dynasm/dasm_mips.h" -#else -#error "No support for this architecture (yet)" -#endif - -/* Embed generated architecture-specific backend. */ -#include "buildvm_arch.h" - -/* ------------------------------------------------------------------------ */ - -void owrite(BuildCtx *ctx, const void *ptr, size_t sz) -{ - if (fwrite(ptr, 1, sz, ctx->fp) != sz) { - fprintf(stderr, "Error: cannot write to output file: %s\n", - strerror(errno)); - exit(1); - } -} - -/* ------------------------------------------------------------------------ */ - -/* Emit code as raw bytes. Only used for DynASM debugging. */ -static void emit_raw(BuildCtx *ctx) -{ - owrite(ctx, ctx->code, ctx->codesz); -} - -/* -- Build machine code -------------------------------------------------- */ - -static const char *sym_decorate(BuildCtx *ctx, - const char *prefix, const char *suffix) -{ - char name[256]; - char *p; -#if LJ_64 - const char *symprefix = ctx->mode == BUILD_machasm ? "_" : ""; -#elif LJ_TARGET_XBOX360 - const char *symprefix = ""; -#else - const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : ""; -#endif - sprintf(name, "%s%s%s", symprefix, prefix, suffix); - p = strchr(name, '@'); - if (p) { -#if LJ_TARGET_X86ORX64 - if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj)) - name[0] = name[1] == 'R' ? '_' : '@'; /* Just for _RtlUnwind@16. */ - else - *p = '\0'; -#elif LJ_TARGET_PPC && !LJ_TARGET_CONSOLE - /* Keep @plt etc. */ -#else - *p = '\0'; -#endif - } - p = (char *)malloc(strlen(name)+1); /* MSVC doesn't like strdup. */ - strcpy(p, name); - return p; -} - -#define NRELOCSYM (sizeof(extnames)/sizeof(extnames[0])-1) - -static int relocmap[NRELOCSYM]; - -/* Collect external relocations. */ -static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type) -{ - if (ctx->nreloc >= BUILD_MAX_RELOC) { - fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n"); - exit(1); - } - if (relocmap[idx] < 0) { - relocmap[idx] = ctx->nrelocsym; - ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]); - ctx->nrelocsym++; - } - ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code); - ctx->reloc[ctx->nreloc].sym = relocmap[idx]; - ctx->reloc[ctx->nreloc].type = type; - ctx->nreloc++; -#if LJ_TARGET_XBOX360 - return (int)(ctx->code - addr) + 4; /* Encode symbol offset of .text. */ -#else - return 0; /* Encode symbol offset of 0. */ -#endif -} - -/* Naive insertion sort. Performance doesn't matter here. */ -static void sym_insert(BuildCtx *ctx, int32_t ofs, - const char *prefix, const char *suffix) -{ - ptrdiff_t i = ctx->nsym++; - while (i > 0) { - if (ctx->sym[i-1].ofs <= ofs) - break; - ctx->sym[i] = ctx->sym[i-1]; - i--; - } - ctx->sym[i].ofs = ofs; - ctx->sym[i].name = sym_decorate(ctx, prefix, suffix); -} - -/* Build the machine code. */ -static int build_code(BuildCtx *ctx) -{ - int status; - int i; - - /* Initialize DynASM structures. */ - ctx->nglob = GLOB__MAX; - ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *)); - memset(ctx->glob, 0, ctx->nglob*sizeof(void *)); - ctx->nreloc = 0; - - ctx->globnames = globnames; - ctx->extnames = extnames; - ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *)); - ctx->nrelocsym = 0; - for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1; - - ctx->dasm_ident = DASM_IDENT; - ctx->dasm_arch = DASM_ARCH; - - dasm_init(Dst, DASM_MAXSECTION); - dasm_setupglobal(Dst, ctx->glob, ctx->nglob); - dasm_setup(Dst, build_actionlist); - - /* Call arch-specific backend to emit the code. */ - ctx->npc = build_backend(ctx); - - /* Finalize the code. */ - (void)dasm_checkstep(Dst, -1); - if ((status = dasm_link(Dst, &ctx->codesz))) return status; - ctx->code = (uint8_t *)malloc(ctx->codesz); - if ((status = dasm_encode(Dst, (void *)ctx->code))) return status; - - /* Allocate symbol table and bytecode offsets. */ - ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin"); - ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym)); - ctx->nsym = 0; - ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t)); - - /* Collect the opcodes (PC labels). */ - for (i = 0; i < ctx->npc; i++) { - int32_t ofs = dasm_getpclabel(Dst, i); - if (ofs < 0) return 0x22000000|i; - ctx->bc_ofs[i] = ofs; - if ((LJ_HASJIT || - !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP || - i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) && - (LJ_HASFFI || i != BC_KCDATA)) - sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]); - } - - /* Collect the globals (named labels). */ - for (i = 0; i < ctx->nglob; i++) { - const char *gl = globnames[i]; - int len = (int)strlen(gl); - if (!ctx->glob[i]) { - fprintf(stderr, "Error: undefined global %s\n", gl); - exit(2); - } - /* Skip the _Z symbols. */ - if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z')) - sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code), - LABEL_PREFIX, globnames[i]); - } - - /* Close the address range. */ - sym_insert(ctx, (int32_t)ctx->codesz, "", ""); - ctx->nsym--; - - dasm_free(Dst); - - return 0; -} - -/* -- Generate VM enums --------------------------------------------------- */ - -const char *const bc_names[] = { -#define BCNAME(name, ma, mb, mc, mt) #name, -BCDEF(BCNAME) -#undef BCNAME - NULL -}; - -#if LJ_HASJIT -const char *const ir_names[] = { -#define IRNAME(name, m, m1, m2) #name, -IRDEF(IRNAME) -#undef IRNAME - NULL -}; - -const char *const irt_names[] = { -#define IRTNAME(name, size) #name, -IRTDEF(IRTNAME) -#undef IRTNAME - NULL -}; - -const char *const irfpm_names[] = { -#define FPMNAME(name) #name, -IRFPMDEF(FPMNAME) -#undef FPMNAME - NULL -}; - -const char *const irfield_names[] = { -#define FLNAME(name, ofs) #name, -IRFLDEF(FLNAME) -#undef FLNAME - NULL -}; - -const char *const ircall_names[] = { -#define IRCALLNAME(cond, name, nargs, kind, type, flags) #name, -IRCALLDEF(IRCALLNAME) -#undef IRCALLNAME - NULL -}; - -static const char *const trace_errors[] = { -#define TREDEF(name, msg) msg, -#include "lj_traceerr.h" - NULL -}; -#endif - -#if LJ_HASJIT -static const char *lower(char *buf, const char *s) -{ - char *p = buf; - while (*s) { - *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s; - s++; - } - *p = '\0'; - return buf; -} -#endif - -/* Emit C source code for bytecode-related definitions. */ -static void emit_bcdef(BuildCtx *ctx) -{ - int i; - fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); - fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n"); - for (i = 0; i < ctx->npc; i++) { - if (i != 0) - fprintf(ctx->fp, ",\n"); - fprintf(ctx->fp, "%d", ctx->bc_ofs[i]); - } -} - -/* Emit VM definitions as Lua code for debug modules. */ -static void emit_vmdef(BuildCtx *ctx) -{ -#if LJ_HASJIT - char buf[80]; -#endif - int i; - fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n"); - fprintf(ctx->fp, "return {\n\n"); - - fprintf(ctx->fp, "bcnames = \""); - for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]); - fprintf(ctx->fp, "\",\n\n"); - -#if LJ_HASJIT - fprintf(ctx->fp, "irnames = \""); - for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]); - fprintf(ctx->fp, "\",\n\n"); - - fprintf(ctx->fp, "irfpm = { [0]="); - for (i = 0; irfpm_names[i]; i++) - fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i])); - fprintf(ctx->fp, "},\n\n"); - - fprintf(ctx->fp, "irfield = { [0]="); - for (i = 0; irfield_names[i]; i++) { - char *p; - lower(buf, irfield_names[i]); - p = strchr(buf, '_'); - if (p) *p = '.'; - fprintf(ctx->fp, "\"%s\", ", buf); - } - fprintf(ctx->fp, "},\n\n"); - - fprintf(ctx->fp, "ircall = {\n[0]="); - for (i = 0; ircall_names[i]; i++) - fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]); - fprintf(ctx->fp, "},\n\n"); - - fprintf(ctx->fp, "traceerr = {\n[0]="); - for (i = 0; trace_errors[i]; i++) - fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]); - fprintf(ctx->fp, "},\n\n"); -#endif -} - -/* -- Argument parsing ---------------------------------------------------- */ - -/* Build mode names. */ -static const char *const modenames[] = { -#define BUILDNAME(name) #name, -BUILDDEF(BUILDNAME) -#undef BUILDNAME - NULL -}; - -/* Print usage information and exit. */ -static void usage(void) -{ - int i; - fprintf(stderr, LUAJIT_VERSION " VM builder.\n"); - fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n"); - fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n"); - fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n"); - fprintf(stderr, "Available modes:\n"); - for (i = 0; i < BUILD__MAX; i++) - fprintf(stderr, " %s\n", modenames[i]); - exit(1); -} - -/* Parse the output mode name. */ -static BuildMode parsemode(const char *mode) -{ - int i; - for (i = 0; modenames[i]; i++) - if (!strcmp(mode, modenames[i])) - return (BuildMode)i; - usage(); - return (BuildMode)-1; -} - -/* Parse arguments. */ -static void parseargs(BuildCtx *ctx, char **argv) -{ - const char *a; - int i; - ctx->mode = (BuildMode)-1; - ctx->outname = "-"; - for (i = 1; (a = argv[i]) != NULL; i++) { - if (a[0] != '-') - break; - switch (a[1]) { - case '-': - if (a[2]) goto err; - i++; - goto ok; - case '\0': - goto ok; - case 'm': - i++; - if (a[2] || argv[i] == NULL) goto err; - ctx->mode = parsemode(argv[i]); - break; - case 'o': - i++; - if (a[2] || argv[i] == NULL) goto err; - ctx->outname = argv[i]; - break; - default: err: - usage(); - break; - } - } -ok: - ctx->args = argv+i; - if (ctx->mode == (BuildMode)-1) goto err; -} - -int main(int argc, char **argv) -{ - BuildCtx ctx_; - BuildCtx *ctx = &ctx_; - int status, binmode; - - if (sizeof(void *) != 4*LJ_32+8*LJ_64) { - fprintf(stderr,"Error: pointer size mismatch in cross-build.\n"); - fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=...\n\n"); - return 1; - } - - UNUSED(argc); - parseargs(ctx, argv); - - if ((status = build_code(ctx))) { - fprintf(stderr,"Error: DASM error %08x\n", status); - return 1; - } - - switch (ctx->mode) { - case BUILD_peobj: - case BUILD_raw: - binmode = 1; - break; - default: - binmode = 0; - break; - } - - if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') { - ctx->fp = stdout; -#if defined(_WIN32) - if (binmode) - _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */ -#endif - } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) { - fprintf(stderr, "Error: cannot open output file '%s': %s\n", - ctx->outname, strerror(errno)); - exit(1); - } - - switch (ctx->mode) { - case BUILD_elfasm: - case BUILD_coffasm: - case BUILD_machasm: - emit_asm(ctx); - emit_asm_debug(ctx); - break; - case BUILD_peobj: - emit_peobj(ctx); - break; - case BUILD_raw: - emit_raw(ctx); - break; - case BUILD_bcdef: - emit_bcdef(ctx); - emit_lib(ctx); - break; - case BUILD_vmdef: - emit_vmdef(ctx); - emit_lib(ctx); - fprintf(ctx->fp, "}\n\n"); - break; - case BUILD_ffdef: - case BUILD_libdef: - case BUILD_recdef: - emit_lib(ctx); - break; - case BUILD_folddef: - emit_fold(ctx); - break; - default: - break; - } - - fflush(ctx->fp); - if (ferror(ctx->fp)) { - fprintf(stderr, "Error: cannot write to output file: %s\n", - strerror(errno)); - exit(1); - } - fclose(ctx->fp); - - return 0; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.h deleted file mode 100644 index 18cd884..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm.h +++ /dev/null @@ -1,105 +0,0 @@ -/* -** LuaJIT VM builder. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _BUILDVM_H -#define _BUILDVM_H - -#include -#include -#include -#include -#include - -#include "lj_def.h" -#include "lj_arch.h" - -/* Hardcoded limits. Increase as needed. */ -#define BUILD_MAX_RELOC 200 /* Max. number of relocations. */ -#define BUILD_MAX_FOLD 4096 /* Max. number of fold rules. */ - -/* Prefix for scanned library definitions. */ -#define LIBDEF_PREFIX "LJLIB_" - -/* Prefix for scanned fold definitions. */ -#define FOLDDEF_PREFIX "LJFOLD" - -/* Prefixes for generated labels. */ -#define LABEL_PREFIX "lj_" -#define LABEL_PREFIX_BC LABEL_PREFIX "BC_" -#define LABEL_PREFIX_FF LABEL_PREFIX "ff_" -#define LABEL_PREFIX_CF LABEL_PREFIX "cf_" -#define LABEL_PREFIX_FFH LABEL_PREFIX "ffh_" -#define LABEL_PREFIX_LIBCF LABEL_PREFIX "lib_cf_" -#define LABEL_PREFIX_LIBINIT LABEL_PREFIX "lib_init_" - -/* Forward declaration. */ -struct dasm_State; - -/* Build modes. */ -#define BUILDDEF(_) \ - _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \ - _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \ - _(folddef) - -typedef enum { -#define BUILDENUM(name) BUILD_##name, -BUILDDEF(BUILDENUM) -#undef BUILDENUM - BUILD__MAX -} BuildMode; - -/* Code relocation. */ -typedef struct BuildReloc { - int32_t ofs; - int sym; - int type; -} BuildReloc; - -typedef struct BuildSym { - const char *name; - int32_t ofs; -} BuildSym; - -/* Build context structure. */ -typedef struct BuildCtx { - /* DynASM state pointer. Should be first member. */ - struct dasm_State *D; - /* Parsed command line. */ - BuildMode mode; - FILE *fp; - const char *outname; - char **args; - /* Code and symbols generated by DynASM. */ - uint8_t *code; - size_t codesz; - int npc, nglob, nsym, nreloc, nrelocsym; - void **glob; - BuildSym *sym; - const char **relocsym; - int32_t *bc_ofs; - const char *beginsym; - /* Strings generated by DynASM. */ - const char *const *globnames; - const char *const *extnames; - const char *dasm_ident; - const char *dasm_arch; - /* Relocations. */ - BuildReloc reloc[BUILD_MAX_RELOC]; -} BuildCtx; - -extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz); -extern void emit_asm(BuildCtx *ctx); -extern void emit_peobj(BuildCtx *ctx); -extern void emit_lib(BuildCtx *ctx); -extern void emit_fold(BuildCtx *ctx); - -extern const char *const bc_names[]; -extern const char *const ir_names[]; -extern const char *const irt_names[]; -extern const char *const irfpm_names[]; -extern const char *const irfield_names[]; -extern const char *const ircall_names[]; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_asm.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_asm.c deleted file mode 100644 index 7baa011..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_asm.c +++ /dev/null @@ -1,348 +0,0 @@ -/* -** LuaJIT VM builder: Assembler source code emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "buildvm.h" -#include "lj_bc.h" - -/* ------------------------------------------------------------------------ */ - -#if LJ_TARGET_X86ORX64 -/* Emit bytes piecewise as assembler text. */ -static void emit_asm_bytes(BuildCtx *ctx, uint8_t *p, int n) -{ - int i; - for (i = 0; i < n; i++) { - if ((i & 15) == 0) - fprintf(ctx->fp, "\t.byte %d", p[i]); - else - fprintf(ctx->fp, ",%d", p[i]); - if ((i & 15) == 15) putc('\n', ctx->fp); - } - if ((n & 15) != 0) putc('\n', ctx->fp); -} - -/* Emit relocation */ -static void emit_asm_reloc(BuildCtx *ctx, int type, const char *sym) -{ - switch (ctx->mode) { - case BUILD_elfasm: - if (type) - fprintf(ctx->fp, "\t.long %s-.-4\n", sym); - else - fprintf(ctx->fp, "\t.long %s\n", sym); - break; - case BUILD_coffasm: - fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", sym); - if (type) - fprintf(ctx->fp, "\t.long %s-.-4\n", sym); - else - fprintf(ctx->fp, "\t.long %s\n", sym); - break; - default: /* BUILD_machasm for relative relocations handled below. */ - fprintf(ctx->fp, "\t.long %s\n", sym); - break; - } -} - -static const char *const jccnames[] = { - "jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "ja", - "js", "jns", "jpe", "jpo", "jl", "jge", "jle", "jg" -}; - -/* Emit x86/x64 text relocations. */ -static void emit_asm_reloc_text(BuildCtx *ctx, uint8_t *cp, int n, - const char *sym) -{ - const char *opname = NULL; - if (--n < 0) goto err; - if (cp[n] == 0xe8) { - opname = "call"; - } else if (cp[n] == 0xe9) { - opname = "jmp"; - } else if (cp[n] >= 0x80 && cp[n] <= 0x8f && n > 0 && cp[n-1] == 0x0f) { - opname = jccnames[cp[n]-0x80]; - n--; - } else { -err: - fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n", - sym); - exit(1); - } - emit_asm_bytes(ctx, cp, n); - if (strncmp(sym+(*sym == '_'), LABEL_PREFIX, sizeof(LABEL_PREFIX)-1)) { - /* Various fixups for external symbols outside of our binary. */ - if (ctx->mode == BUILD_elfasm) { - if (LJ_32) - fprintf(ctx->fp, "#if __PIC__\n\t%s lj_wrap_%s\n#else\n", opname, sym); - fprintf(ctx->fp, "\t%s %s@PLT\n", opname, sym); - if (LJ_32) - fprintf(ctx->fp, "#endif\n"); - return; - } else if (LJ_32 && ctx->mode == BUILD_machasm) { - fprintf(ctx->fp, "\t%s L%s$stub\n", opname, sym); - return; - } - } - fprintf(ctx->fp, "\t%s %s\n", opname, sym); -} -#else -/* Emit words piecewise as assembler text. */ -static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n) -{ - int i; - for (i = 0; i < n; i += 4) { - uint32_t ins = *(uint32_t *)(p+i); -#if LJ_TARGET_ARM64 && LJ_BE - ins = lj_bswap(ins); /* ARM64 instructions are always little-endian. */ -#endif - if ((i & 15) == 0) - fprintf(ctx->fp, "\t.long 0x%08x", ins); - else - fprintf(ctx->fp, ",0x%08x", ins); - if ((i & 15) == 12) putc('\n', ctx->fp); - } - if ((n & 15) != 0) putc('\n', ctx->fp); -} - -/* Emit relocation as part of an instruction. */ -static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n, - const char *sym) -{ - uint32_t ins; - emit_asm_words(ctx, p, n-4); - ins = *(uint32_t *)(p+n-4); -#if LJ_TARGET_ARM - if ((ins & 0xff000000u) == 0xfa000000u) { - fprintf(ctx->fp, "\tblx %s\n", sym); - } else if ((ins & 0x0e000000u) == 0x0a000000u) { - fprintf(ctx->fp, "\t%s%.2s %s\n", (ins & 0x01000000u) ? "bl" : "b", - &"eqnecsccmiplvsvchilsgeltgtle"[2*(ins >> 28)], sym); - } else { - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); - } -#elif LJ_TARGET_ARM64 - if ((ins >> 26) == 0x25u) { - fprintf(ctx->fp, "\tbl %s\n", sym); - } else { - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); - } -#elif LJ_TARGET_PPC -#if LJ_TARGET_PS3 -#define TOCPREFIX "." -#else -#define TOCPREFIX "" -#endif - if ((ins >> 26) == 16) { - fprintf(ctx->fp, "\t%s %d, %d, " TOCPREFIX "%s\n", - (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym); - } else if ((ins >> 26) == 18) { - fprintf(ctx->fp, "\t%s " TOCPREFIX "%s\n", (ins & 1) ? "bl" : "b", sym); - } else { - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); - } -#elif LJ_TARGET_MIPS - fprintf(stderr, - "Error: unsupported opcode %08x for %s symbol relocation.\n", - ins, sym); - exit(1); -#else -#error "missing relocation support for this architecture" -#endif -} -#endif - -#if LJ_TARGET_ARM -#define ELFASM_PX "%%" -#else -#define ELFASM_PX "@" -#endif - -/* Emit an assembler label. */ -static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc) -{ - switch (ctx->mode) { - case BUILD_elfasm: -#if LJ_TARGET_PS3 - if (!strncmp(name, "lj_vm_", 6) && - strcmp(name, ctx->beginsym) && - !strstr(name, "hook")) { - fprintf(ctx->fp, - "\n\t.globl %s\n" - "\t.section \".opd\",\"aw\"\n" - "%s:\n" - "\t.long .%s,.TOC.@tocbase32\n" - "\t.size %s,8\n" - "\t.previous\n" - "\t.globl .%s\n" - "\t.hidden .%s\n" - "\t.type .%s, " ELFASM_PX "function\n" - "\t.size .%s, %d\n" - ".%s:\n", - name, name, name, name, name, name, name, name, size, name); - break; - } -#endif - fprintf(ctx->fp, - "\n\t.globl %s\n" - "\t.hidden %s\n" - "\t.type %s, " ELFASM_PX "%s\n" - "\t.size %s, %d\n" - "%s:\n", - name, name, name, isfunc ? "function" : "object", name, size, name); - break; - case BUILD_coffasm: - fprintf(ctx->fp, "\n\t.globl %s\n", name); - if (isfunc) - fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name); - fprintf(ctx->fp, "%s:\n", name); - break; - case BUILD_machasm: - fprintf(ctx->fp, - "\n\t.private_extern %s\n" - "\t.no_dead_strip %s\n" - "%s:\n", name, name, name); - break; - default: - break; - } -} - -/* Emit alignment. */ -static void emit_asm_align(BuildCtx *ctx, int bits) -{ - switch (ctx->mode) { - case BUILD_elfasm: - case BUILD_coffasm: - fprintf(ctx->fp, "\t.p2align %d\n", bits); - break; - case BUILD_machasm: - fprintf(ctx->fp, "\t.align %d\n", bits); - break; - default: - break; - } -} - -/* ------------------------------------------------------------------------ */ - -/* Emit assembler source code. */ -void emit_asm(BuildCtx *ctx) -{ - int i, rel; - - fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch); - fprintf(ctx->fp, "\t.text\n"); - emit_asm_align(ctx, 4); - -#if LJ_TARGET_PS3 - emit_asm_label(ctx, ctx->beginsym, ctx->codesz, 0); -#else - emit_asm_label(ctx, ctx->beginsym, 0, 0); -#endif - if (ctx->mode != BUILD_machasm) - fprintf(ctx->fp, ".Lbegin:\n"); - -#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND - /* This should really be moved into buildvm_arm.dasc. */ -#if LJ_ARCH_HASFPU - fprintf(ctx->fp, - ".fnstart\n" - ".save {r5, r6, r7, r8, r9, r10, r11, lr}\n" - ".vsave {d8-d15}\n" - ".save {r4}\n" - ".pad #28\n"); -#else - fprintf(ctx->fp, - ".fnstart\n" - ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n" - ".pad #28\n"); -#endif -#endif -#if LJ_TARGET_MIPS - fprintf(ctx->fp, ".set nomips16\n.abicalls\n.set noreorder\n.set nomacro\n"); -#endif - - for (i = rel = 0; i < ctx->nsym; i++) { - int32_t ofs = ctx->sym[i].ofs; - int32_t next = ctx->sym[i+1].ofs; -#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI - if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call")) - fprintf(ctx->fp, - ".globl lj_err_unwind_arm\n" - ".personality lj_err_unwind_arm\n" - ".fnend\n" - ".fnstart\n" - ".save {r4, r5, r11, lr}\n" - ".setfp r11, sp\n"); -#endif - emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1); - while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) { - BuildReloc *r = &ctx->reloc[rel]; - int n = r->ofs - ofs; -#if LJ_TARGET_X86ORX64 - if (r->type != 0 && - (ctx->mode == BUILD_elfasm || ctx->mode == BUILD_machasm)) { - emit_asm_reloc_text(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]); - } else { - emit_asm_bytes(ctx, ctx->code+ofs, n); - emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]); - } - ofs += n+4; -#else - emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]); - ofs += n; -#endif - rel++; - } -#if LJ_TARGET_X86ORX64 - emit_asm_bytes(ctx, ctx->code+ofs, next-ofs); -#else - emit_asm_words(ctx, ctx->code+ofs, next-ofs); -#endif - } - -#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND - fprintf(ctx->fp, -#if !LJ_HASFFI - ".globl lj_err_unwind_arm\n" - ".personality lj_err_unwind_arm\n" -#endif - ".fnend\n"); -#endif - - fprintf(ctx->fp, "\n"); - switch (ctx->mode) { - case BUILD_elfasm: -#if !(LJ_TARGET_PS3 || LJ_TARGET_PSVITA) - fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n"); -#endif -#if LJ_TARGET_PPC && !LJ_TARGET_PS3 && !LJ_ABI_SOFTFP - /* Hard-float ABI. */ - fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n"); -#endif - /* fallthrough */ - case BUILD_coffasm: - fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident); - break; - case BUILD_machasm: - fprintf(ctx->fp, - "\t.cstring\n" - "\t.ascii \"%s\\0\"\n", ctx->dasm_ident); - break; - default: - break; - } - fprintf(ctx->fp, "\n"); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_fold.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_fold.c deleted file mode 100644 index edb5576..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_fold.c +++ /dev/null @@ -1,236 +0,0 @@ -/* -** LuaJIT VM builder: IR folding hash table generator. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "buildvm.h" -#include "lj_obj.h" -#if LJ_HASJIT -#include "lj_ir.h" - -/* Context for the folding hash table generator. */ -static int lineno; -static uint32_t funcidx; -static uint32_t foldkeys[BUILD_MAX_FOLD]; -static uint32_t nkeys; - -/* Try to fill the hash table with keys using the hash parameters. */ -static int tryhash(uint32_t *htab, uint32_t sz, uint32_t r, int dorol) -{ - uint32_t i; - if (dorol && ((r & 31) == 0 || (r>>5) == 0)) - return 0; /* Avoid zero rotates. */ - memset(htab, 0xff, (sz+1)*sizeof(uint32_t)); - for (i = 0; i < nkeys; i++) { - uint32_t key = foldkeys[i]; - uint32_t k = key & 0xffffff; - uint32_t h = (dorol ? lj_rol(lj_rol(k, r>>5) - k, r&31) : - (((k << (r>>5)) - k) << (r&31))) % sz; - if (htab[h] != 0xffffffff) { /* Collision on primary slot. */ - if (htab[h+1] != 0xffffffff) { /* Collision on secondary slot. */ - /* Try to move the colliding key, if possible. */ - if (h < sz-1 && htab[h+2] == 0xffffffff) { - uint32_t k2 = htab[h+1] & 0xffffff; - uint32_t h2 = (dorol ? lj_rol(lj_rol(k2, r>>5) - k2, r&31) : - (((k2 << (r>>5)) - k2) << (r&31))) % sz; - if (h2 != h+1) return 0; /* Cannot resolve collision. */ - htab[h+2] = htab[h+1]; /* Move colliding key to secondary slot. */ - } else { - return 0; /* Collision. */ - } - } - htab[h+1] = key; - } else { - htab[h] = key; - } - } - return 1; /* Success, all keys could be stored. */ -} - -/* Print the generated hash table. */ -static void printhash(BuildCtx *ctx, uint32_t *htab, uint32_t sz) -{ - uint32_t i; - fprintf(ctx->fp, "static const uint32_t fold_hash[%d] = {\n0x%08x", - sz+1, htab[0]); - for (i = 1; i < sz+1; i++) - fprintf(ctx->fp, ",\n0x%08x", htab[i]); - fprintf(ctx->fp, "\n};\n\n"); -} - -/* Exhaustive search for the shortest semi-perfect hash table. */ -static void makehash(BuildCtx *ctx) -{ - uint32_t htab[BUILD_MAX_FOLD*2+1]; - uint32_t sz, r; - /* Search for the smallest hash table with an odd size. */ - for (sz = (nkeys|1); sz < BUILD_MAX_FOLD*2; sz += 2) { - /* First try all shift hash combinations. */ - for (r = 0; r < 32*32; r++) { - if (tryhash(htab, sz, r, 0)) { - printhash(ctx, htab, sz); - fprintf(ctx->fp, - "#define fold_hashkey(k)\t(((((k)<<%u)-(k))<<%u)%%%u)\n\n", - r>>5, r&31, sz); - return; - } - } - /* Then try all rotate hash combinations. */ - for (r = 0; r < 32*32; r++) { - if (tryhash(htab, sz, r, 1)) { - printhash(ctx, htab, sz); - fprintf(ctx->fp, - "#define fold_hashkey(k)\t(lj_rol(lj_rol((k),%u)-(k),%u)%%%u)\n\n", - r>>5, r&31, sz); - return; - } - } - } - fprintf(stderr, "Error: search for perfect hash failed\n"); - exit(1); -} - -/* Parse one token of a fold rule. */ -static uint32_t nexttoken(char **pp, int allowlit, int allowany) -{ - char *p = *pp; - if (p) { - uint32_t i; - char *q = strchr(p, ' '); - if (q) *q++ = '\0'; - *pp = q; - if (allowlit && !strncmp(p, "IRFPM_", 6)) { - for (i = 0; irfpm_names[i]; i++) - if (!strcmp(irfpm_names[i], p+6)) - return i; - } else if (allowlit && !strncmp(p, "IRFL_", 5)) { - for (i = 0; irfield_names[i]; i++) - if (!strcmp(irfield_names[i], p+5)) - return i; - } else if (allowlit && !strncmp(p, "IRCALL_", 7)) { - for (i = 0; ircall_names[i]; i++) - if (!strcmp(ircall_names[i], p+7)) - return i; - } else if (allowlit && !strncmp(p, "IRCONV_", 7)) { - for (i = 0; irt_names[i]; i++) { - const char *r = strchr(p+7, '_'); - if (r && !strncmp(irt_names[i], p+7, r-(p+7))) { - uint32_t j; - for (j = 0; irt_names[j]; j++) - if (!strcmp(irt_names[j], r+1)) - return (i << 5) + j; - } - } - } else if (allowlit && *p >= '0' && *p <= '9') { - for (i = 0; *p >= '0' && *p <= '9'; p++) - i = i*10 + (*p - '0'); - if (*p == '\0') - return i; - } else if (allowany && !strcmp("any", p)) { - return allowany; - } else { - for (i = 0; ir_names[i]; i++) - if (!strcmp(ir_names[i], p)) - return i; - } - fprintf(stderr, "Error: bad fold definition token \"%s\" at line %d\n", p, lineno); - exit(1); - } - return 0; -} - -/* Parse a fold rule. */ -static void foldrule(char *p) -{ - uint32_t op = nexttoken(&p, 0, 0); - uint32_t left = nexttoken(&p, 0, 0x7f); - uint32_t right = nexttoken(&p, 1, 0x3ff); - uint32_t key = (funcidx << 24) | (op << 17) | (left << 10) | right; - uint32_t i; - if (nkeys >= BUILD_MAX_FOLD) { - fprintf(stderr, "Error: too many fold rules, increase BUILD_MAX_FOLD.\n"); - exit(1); - } - /* Simple insertion sort to detect duplicates. */ - for (i = nkeys; i > 0; i--) { - if ((foldkeys[i-1]&0xffffff) < (key & 0xffffff)) - break; - if ((foldkeys[i-1]&0xffffff) == (key & 0xffffff)) { - fprintf(stderr, "Error: duplicate fold definition at line %d\n", lineno); - exit(1); - } - foldkeys[i] = foldkeys[i-1]; - } - foldkeys[i] = key; - nkeys++; -} - -/* Emit C source code for IR folding hash table. */ -void emit_fold(BuildCtx *ctx) -{ - char buf[256]; /* We don't care about analyzing lines longer than that. */ - const char *fname = ctx->args[0]; - FILE *fp; - - if (fname == NULL) { - fprintf(stderr, "Error: missing input filename\n"); - exit(1); - } - - if (fname[0] == '-' && fname[1] == '\0') { - fp = stdin; - } else { - fp = fopen(fname, "r"); - if (!fp) { - fprintf(stderr, "Error: cannot open input file '%s': %s\n", - fname, strerror(errno)); - exit(1); - } - } - - fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); - fprintf(ctx->fp, "static const FoldFunc fold_func[] = {\n"); - - lineno = 0; - funcidx = 0; - nkeys = 0; - while (fgets(buf, sizeof(buf), fp) != NULL) { - lineno++; - /* The prefix must be at the start of a line, otherwise it's ignored. */ - if (!strncmp(buf, FOLDDEF_PREFIX, sizeof(FOLDDEF_PREFIX)-1)) { - char *p = buf+sizeof(FOLDDEF_PREFIX)-1; - char *q = strchr(p, ')'); - if (p[0] == '(' && q) { - p++; - *q = '\0'; - foldrule(p); - } else if ((p[0] == 'F' || p[0] == 'X') && p[1] == '(' && q) { - p += 2; - *q = '\0'; - if (funcidx) - fprintf(ctx->fp, ",\n"); - if (p[-2] == 'X') - fprintf(ctx->fp, " %s", p); - else - fprintf(ctx->fp, " fold_%s", p); - funcidx++; - } else { - buf[strlen(buf)-1] = '\0'; - fprintf(stderr, "Error: unknown fold definition tag %s%s at line %d\n", - FOLDDEF_PREFIX, p, lineno); - exit(1); - } - } - } - fclose(fp); - fprintf(ctx->fp, "\n};\n\n"); - - makehash(ctx); -} -#else -void emit_fold(BuildCtx *ctx) -{ - UNUSED(ctx); -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_lib.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_lib.c deleted file mode 100644 index b125ea1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_lib.c +++ /dev/null @@ -1,459 +0,0 @@ -/* -** LuaJIT VM builder: library definition compiler. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "buildvm.h" -#include "lj_obj.h" -#include "lj_bc.h" -#include "lj_lib.h" -#include "buildvm_libbc.h" - -/* Context for library definitions. */ -static uint8_t obuf[8192]; -static uint8_t *optr; -static char modname[80]; -static size_t modnamelen; -static char funcname[80]; -static int modstate, regfunc; -static int ffid, recffid, ffasmfunc; - -enum { - REGFUNC_OK, - REGFUNC_NOREG, - REGFUNC_NOREGUV -}; - -static void libdef_name(const char *p, int kind) -{ - size_t n = strlen(p); - if (kind != LIBINIT_STRING) { - if (n > modnamelen && p[modnamelen] == '_' && - !strncmp(p, modname, modnamelen)) { - p += modnamelen+1; - n -= modnamelen+1; - } - } - if (n > LIBINIT_MAXSTR) { - fprintf(stderr, "Error: string too long: '%s'\n", p); - exit(1); - } - if (optr+1+n+2 > obuf+sizeof(obuf)) { /* +2 for caller. */ - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = (uint8_t)(n | kind); - memcpy(optr, p, n); - optr += n; -} - -static void libdef_endmodule(BuildCtx *ctx) -{ - if (modstate != 0) { - char line[80]; - const uint8_t *p; - int n; - if (modstate == 1) - fprintf(ctx->fp, " (lua_CFunction)0"); - fprintf(ctx->fp, "\n};\n"); - fprintf(ctx->fp, "static const uint8_t %s%s[] = {\n", - LABEL_PREFIX_LIBINIT, modname); - line[0] = '\0'; - for (n = 0, p = obuf; p < optr; p++) { - n += sprintf(line+n, "%d,", *p); - if (n >= 75) { - fprintf(ctx->fp, "%s\n", line); - n = 0; - line[0] = '\0'; - } - } - fprintf(ctx->fp, "%s%d\n};\n#endif\n\n", line, LIBINIT_END); - } -} - -static void libdef_module(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - libdef_endmodule(ctx); - optr = obuf; - *optr++ = (uint8_t)ffid; - *optr++ = (uint8_t)ffasmfunc; - *optr++ = 0; /* Hash table size. */ - modstate = 1; - fprintf(ctx->fp, "#ifdef %sMODULE_%s\n", LIBDEF_PREFIX, p); - fprintf(ctx->fp, "#undef %sMODULE_%s\n", LIBDEF_PREFIX, p); - fprintf(ctx->fp, "static const lua_CFunction %s%s[] = {\n", - LABEL_PREFIX_LIBCF, p); - } - modnamelen = strlen(p); - if (modnamelen > sizeof(modname)-1) { - fprintf(stderr, "Error: module name too long: '%s'\n", p); - exit(1); - } - strcpy(modname, p); -} - -static int find_ffofs(BuildCtx *ctx, const char *name) -{ - int i; - for (i = 0; i < ctx->nglob; i++) { - const char *gl = ctx->globnames[i]; - if (gl[0] == 'f' && gl[1] == 'f' && gl[2] == '_' && !strcmp(gl+3, name)) { - return (int)((uint8_t *)ctx->glob[i] - ctx->code); - } - } - fprintf(stderr, "Error: undefined fast function %s%s\n", - LABEL_PREFIX_FF, name); - exit(1); -} - -static void libdef_func(BuildCtx *ctx, char *p, int arg) -{ - if (arg != LIBINIT_CF) - ffasmfunc++; - if (ctx->mode == BUILD_libdef) { - if (modstate == 0) { - fprintf(stderr, "Error: no module for function definition %s\n", p); - exit(1); - } - if (regfunc == REGFUNC_NOREG) { - if (optr+1 > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_FFID; - } else { - if (arg != LIBINIT_ASM_) { - if (modstate != 1) fprintf(ctx->fp, ",\n"); - modstate = 2; - fprintf(ctx->fp, " %s%s", arg ? LABEL_PREFIX_FFH : LABEL_PREFIX_CF, p); - } - if (regfunc != REGFUNC_NOREGUV) obuf[2]++; /* Bump hash table size. */ - libdef_name(regfunc == REGFUNC_NOREGUV ? "" : p, arg); - } - } else if (ctx->mode == BUILD_ffdef) { - fprintf(ctx->fp, "FFDEF(%s)\n", p); - } else if (ctx->mode == BUILD_recdef) { - if (strlen(p) > sizeof(funcname)-1) { - fprintf(stderr, "Error: function name too long: '%s'\n", p); - exit(1); - } - strcpy(funcname, p); - } else if (ctx->mode == BUILD_vmdef) { - int i; - for (i = 1; p[i] && modname[i-1]; i++) - if (p[i] == '_') p[i] = '.'; - fprintf(ctx->fp, "\"%s\",\n", p); - } else if (ctx->mode == BUILD_bcdef) { - if (arg != LIBINIT_CF) - fprintf(ctx->fp, ",\n%d", find_ffofs(ctx, p)); - } - ffid++; - regfunc = REGFUNC_OK; -} - -static uint8_t *libdef_uleb128(uint8_t *p, uint32_t *vv) -{ - uint32_t v = *p++; - if (v >= 0x80) { - int sh = 0; v &= 0x7f; - do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80); - } - *vv = v; - return p; -} - -static void libdef_fixupbc(uint8_t *p) -{ - uint32_t i, sizebc; - p += 4; - p = libdef_uleb128(p, &sizebc); - p = libdef_uleb128(p, &sizebc); - p = libdef_uleb128(p, &sizebc); - for (i = 0; i < sizebc; i++, p += 4) { - uint8_t op = p[libbc_endian ? 3 : 0]; - uint8_t ra = p[libbc_endian ? 2 : 1]; - uint8_t rc = p[libbc_endian ? 1 : 2]; - uint8_t rb = p[libbc_endian ? 0 : 3]; - if (!LJ_DUALNUM && op == BC_ISTYPE && rc == ~LJ_TNUMX+1) { - op = BC_ISNUM; rc++; - } - p[LJ_ENDIAN_SELECT(0, 3)] = op; - p[LJ_ENDIAN_SELECT(1, 2)] = ra; - p[LJ_ENDIAN_SELECT(2, 1)] = rc; - p[LJ_ENDIAN_SELECT(3, 0)] = rb; - } -} - -static void libdef_lua(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - int i; - for (i = 0; libbc_map[i].name != NULL; i++) { - if (!strcmp(libbc_map[i].name, p)) { - int ofs = libbc_map[i].ofs; - int len = libbc_map[i+1].ofs - ofs; - obuf[2]++; /* Bump hash table size. */ - *optr++ = LIBINIT_LUA; - libdef_name(p, 0); - memcpy(optr, libbc_code + ofs, len); - libdef_fixupbc(optr); - optr += len; - return; - } - } - fprintf(stderr, "Error: missing libbc definition for %s\n", p); - exit(1); - } -} - -static uint32_t find_rec(char *name) -{ - char *p = (char *)obuf; - uint32_t n; - for (n = 2; *p; n++) { - if (strcmp(p, name) == 0) - return n; - p += strlen(p)+1; - } - if (p+strlen(name)+1 >= (char *)obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - strcpy(p, name); - return n; -} - -static void libdef_rec(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_recdef) { - char *q; - uint32_t n; - for (; recffid+1 < ffid; recffid++) - fprintf(ctx->fp, ",\n0"); - recffid = ffid; - if (*p == '.') p = funcname; - q = strchr(p, ' '); - if (q) *q++ = '\0'; - n = find_rec(p); - if (q) - fprintf(ctx->fp, ",\n0x%02x00+(%s)", n, q); - else - fprintf(ctx->fp, ",\n0x%02x00", n); - } -} - -static void memcpy_endian(void *dst, void *src, size_t n) -{ - union { uint8_t b; uint32_t u; } host_endian; - host_endian.u = 1; - if (host_endian.b == LJ_ENDIAN_SELECT(1, 0)) { - memcpy(dst, src, n); - } else { - size_t i; - for (i = 0; i < n; i++) - ((uint8_t *)dst)[i] = ((uint8_t *)src)[n-i-1]; - } -} - -static void libdef_push(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - int len = (int)strlen(p); - if (*p == '"') { - if (len > 1 && p[len-1] == '"') { - p[len-1] = '\0'; - libdef_name(p+1, LIBINIT_STRING); - return; - } - } else if (*p >= '0' && *p <= '9') { - char *ep; - double d = strtod(p, &ep); - if (*ep == '\0') { - if (optr+1+sizeof(double) > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_NUMBER; - memcpy_endian(optr, &d, sizeof(double)); - optr += sizeof(double); - return; - } - } else if (!strcmp(p, "lastcl")) { - if (optr+1 > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_LASTCL; - return; - } else if (len > 4 && !strncmp(p, "top-", 4)) { - if (optr+2 > obuf+sizeof(obuf)) { - fprintf(stderr, "Error: output buffer overflow\n"); - exit(1); - } - *optr++ = LIBINIT_COPY; - *optr++ = (uint8_t)atoi(p+4); - return; - } - fprintf(stderr, "Error: bad value for %sPUSH(%s)\n", LIBDEF_PREFIX, p); - exit(1); - } -} - -static void libdef_set(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(arg); - if (ctx->mode == BUILD_libdef) { - if (p[0] == '!' && p[1] == '\0') p[0] = '\0'; /* Set env. */ - libdef_name(p, LIBINIT_STRING); - *optr++ = LIBINIT_SET; - obuf[2]++; /* Bump hash table size. */ - } -} - -static void libdef_regfunc(BuildCtx *ctx, char *p, int arg) -{ - UNUSED(ctx); UNUSED(p); - regfunc = arg; -} - -typedef void (*LibDefFunc)(BuildCtx *ctx, char *p, int arg); - -typedef struct LibDefHandler { - const char *suffix; - const char *stop; - const LibDefFunc func; - const int arg; -} LibDefHandler; - -static const LibDefHandler libdef_handlers[] = { - { "MODULE_", " \t\r\n", libdef_module, 0 }, - { "CF(", ")", libdef_func, LIBINIT_CF }, - { "ASM(", ")", libdef_func, LIBINIT_ASM }, - { "ASM_(", ")", libdef_func, LIBINIT_ASM_ }, - { "LUA(", ")", libdef_lua, 0 }, - { "REC(", ")", libdef_rec, 0 }, - { "PUSH(", ")", libdef_push, 0 }, - { "SET(", ")", libdef_set, 0 }, - { "NOREGUV", NULL, libdef_regfunc, REGFUNC_NOREGUV }, - { "NOREG", NULL, libdef_regfunc, REGFUNC_NOREG }, - { NULL, NULL, (LibDefFunc)0, 0 } -}; - -/* Emit C source code for library function definitions. */ -void emit_lib(BuildCtx *ctx) -{ - const char *fname; - - if (ctx->mode == BUILD_ffdef || ctx->mode == BUILD_libdef || - ctx->mode == BUILD_recdef) - fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); - else if (ctx->mode == BUILD_vmdef) - fprintf(ctx->fp, "ffnames = {\n[0]=\"Lua\",\n\"C\",\n"); - if (ctx->mode == BUILD_recdef) - fprintf(ctx->fp, "static const uint16_t recff_idmap[] = {\n0,\n0x0100"); - recffid = ffid = FF_C+1; - ffasmfunc = 0; - - while ((fname = *ctx->args++)) { - char buf[256]; /* We don't care about analyzing lines longer than that. */ - FILE *fp; - if (fname[0] == '-' && fname[1] == '\0') { - fp = stdin; - } else { - fp = fopen(fname, "r"); - if (!fp) { - fprintf(stderr, "Error: cannot open input file '%s': %s\n", - fname, strerror(errno)); - exit(1); - } - } - modstate = 0; - regfunc = REGFUNC_OK; - while (fgets(buf, sizeof(buf), fp) != NULL) { - char *p; - /* Simplistic pre-processor. Only handles top-level #if/#endif. */ - if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') { - int ok = 1; - if (!strcmp(buf, "#if LJ_52\n")) - ok = LJ_52; - else if (!strcmp(buf, "#if LJ_HASJIT\n")) - ok = LJ_HASJIT; - else if (!strcmp(buf, "#if LJ_HASFFI\n")) - ok = LJ_HASFFI; - else if (!strcmp(buf, "#if LJ_HASBUFFER\n")) - ok = LJ_HASBUFFER; - if (!ok) { - int lvl = 1; - while (fgets(buf, sizeof(buf), fp) != NULL) { - if (buf[0] == '#' && buf[1] == 'e' && buf[2] == 'n') { - if (--lvl == 0) break; - } else if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') { - lvl++; - } - } - continue; - } - } - for (p = buf; (p = strstr(p, LIBDEF_PREFIX)) != NULL; ) { - const LibDefHandler *ldh; - p += sizeof(LIBDEF_PREFIX)-1; - for (ldh = libdef_handlers; ldh->suffix != NULL; ldh++) { - size_t n, len = strlen(ldh->suffix); - if (!strncmp(p, ldh->suffix, len)) { - p += len; - n = ldh->stop ? strcspn(p, ldh->stop) : 0; - if (!p[n]) break; - p[n] = '\0'; - ldh->func(ctx, p, ldh->arg); - p += n+1; - break; - } - } - if (ldh->suffix == NULL) { - buf[strlen(buf)-1] = '\0'; - fprintf(stderr, "Error: unknown library definition tag %s%s\n", - LIBDEF_PREFIX, p); - exit(1); - } - } - } - fclose(fp); - if (ctx->mode == BUILD_libdef) { - libdef_endmodule(ctx); - } - } - - if (ctx->mode == BUILD_ffdef) { - fprintf(ctx->fp, "\n#undef FFDEF\n\n"); - fprintf(ctx->fp, - "#ifndef FF_NUM_ASMFUNC\n#define FF_NUM_ASMFUNC %d\n#endif\n\n", - ffasmfunc); - } else if (ctx->mode == BUILD_vmdef) { - fprintf(ctx->fp, "},\n\n"); - } else if (ctx->mode == BUILD_bcdef) { - int i; - fprintf(ctx->fp, "\n};\n\n"); - fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_mode[] = {\n"); - fprintf(ctx->fp, "BCDEF(BCMODE)\n"); - for (i = ffasmfunc-1; i > 0; i--) - fprintf(ctx->fp, "BCMODE_FF,\n"); - fprintf(ctx->fp, "BCMODE_FF\n};\n\n"); - } else if (ctx->mode == BUILD_recdef) { - char *p = (char *)obuf; - fprintf(ctx->fp, "\n};\n\n"); - fprintf(ctx->fp, "static const RecordFunc recff_func[] = {\n" - "recff_nyi,\n" - "recff_c"); - while (*p) { - fprintf(ctx->fp, ",\nrecff_%s", p); - p += strlen(p)+1; - } - fprintf(ctx->fp, "\n};\n\n"); - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_libbc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_libbc.h deleted file mode 100644 index 276463b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_libbc.h +++ /dev/null @@ -1,81 +0,0 @@ -/* This is a generated file. DO NOT EDIT! */ - -static const int libbc_endian = 0; - -static const uint8_t libbc_code[] = { -#if LJ_FR2 -/* math.deg */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,241,135,158,166,3, -220,203,178,130,4, -/* math.rad */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,243,244,148,165,20, -198,190,199,252,3, -/* string.len */ 0,1,2,0,0,0,3,BC_ISTYPE,0,5,0,BC_LEN,1,0,0,BC_RET1,1,2,0, -/* table.foreachi */ 0,2,10,0,0,0,15,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0, -BC_KSHORT,2,1,0,BC_LEN,3,0,0,BC_KSHORT,4,1,0,BC_FORI,2,8,128,BC_MOV,6,1,0, -BC_MOV,8,5,0,BC_TGETR,9,5,0,BC_CALL,6,3,2,BC_ISEQP,6,0,0,BC_JMP,7,1,128, -BC_RET1,6,2,0,BC_FORL,2,248,127,BC_RET0,0,1,0, -/* table.foreach */ 0,2,11,0,0,1,16,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0,BC_KPRI, -2,0,0,BC_MOV,3,0,0,BC_KNUM,4,0,0,BC_JMP,5,7,128,BC_MOV,7,1,0,BC_MOV,9,5,0, -BC_MOV,10,6,0,BC_CALL,7,3,2,BC_ISEQP,7,0,0,BC_JMP,8,1,128,BC_RET1,7,2,0, -BC_ITERN,5,3,3,BC_ITERL,5,247,127,BC_RET0,0,1,0,1,255,255,249,255,15, -/* table.getn */ 0,1,2,0,0,0,3,BC_ISTYPE,0,12,0,BC_LEN,1,0,0,BC_RET1,1,2,0, -/* table.remove */ 0,2,10,0,0,2,30,BC_ISTYPE,0,12,0,BC_LEN,2,0,0,BC_ISNEP,1,0, -0,BC_JMP,3,7,128,BC_ISEQN,2,0,0,BC_JMP,3,23,128,BC_TGETR,3,2,0,BC_KPRI,4,0,0, -BC_TSETR,4,2,0,BC_RET1,3,2,0,BC_JMP,3,18,128,BC_ISTYPE,1,14,0,BC_KSHORT,3,1,0, -BC_ISGT,3,1,0,BC_JMP,3,14,128,BC_ISGT,1,2,0,BC_JMP,3,12,128,BC_TGETR,3,1,0, -BC_ADDVN,4,1,1,BC_MOV,5,2,0,BC_KSHORT,6,1,0,BC_FORI,4,4,128,BC_SUBVN,8,1,7, -BC_TGETR,9,7,0,BC_TSETR,9,8,0,BC_FORL,4,252,127,BC_KPRI,4,0,0,BC_TSETR,4,2,0, -BC_RET1,3,2,0,BC_RET0,0,1,0,0,2, -/* table.move */ 0,5,12,0,0,0,35,BC_ISTYPE,0,12,0,BC_ISTYPE,1,14,0,BC_ISTYPE, -2,14,0,BC_ISTYPE,3,14,0,BC_ISNEP,4,0,0,BC_JMP,5,1,128,BC_MOV,4,0,0,BC_ISTYPE, -4,12,0,BC_ISGT,1,2,0,BC_JMP,5,24,128,BC_SUBVV,5,1,3,BC_ISLT,2,3,0,BC_JMP,6,4, -128,BC_ISLE,3,1,0,BC_JMP,6,2,128,BC_ISEQV,4,0,0,BC_JMP,6,9,128,BC_MOV,6,1,0, -BC_MOV,7,2,0,BC_KSHORT,8,1,0,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0, -BC_TSETR,11,10,4,BC_FORL,6,252,127,BC_JMP,6,8,128,BC_MOV,6,2,0,BC_MOV,7,1,0, -BC_KSHORT,8,255,255,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0,BC_TSETR, -11,10,4,BC_FORL,6,252,127,BC_RET1,4,2,0, -#else -/* math.deg */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,241,135,158,166,3, -220,203,178,130,4, -/* math.rad */ 0,1,2,0,0,1,2,BC_MULVN,1,0,0,BC_RET1,1,2,0,243,244,148,165,20, -198,190,199,252,3, -/* string.len */ 0,1,2,0,0,0,3,BC_ISTYPE,0,5,0,BC_LEN,1,0,0,BC_RET1,1,2,0, -/* table.foreachi */ 0,2,9,0,0,0,15,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0, -BC_KSHORT,2,1,0,BC_LEN,3,0,0,BC_KSHORT,4,1,0,BC_FORI,2,8,128,BC_MOV,6,1,0, -BC_MOV,7,5,0,BC_TGETR,8,5,0,BC_CALL,6,3,2,BC_ISEQP,6,0,0,BC_JMP,7,1,128, -BC_RET1,6,2,0,BC_FORL,2,248,127,BC_RET0,0,1,0, -/* table.foreach */ 0,2,10,0,0,1,16,BC_ISTYPE,0,12,0,BC_ISTYPE,1,9,0,BC_KPRI, -2,0,0,BC_MOV,3,0,0,BC_KNUM,4,0,0,BC_JMP,5,7,128,BC_MOV,7,1,0,BC_MOV,8,5,0, -BC_MOV,9,6,0,BC_CALL,7,3,2,BC_ISEQP,7,0,0,BC_JMP,8,1,128,BC_RET1,7,2,0, -BC_ITERN,5,3,3,BC_ITERL,5,247,127,BC_RET0,0,1,0,1,255,255,249,255,15, -/* table.getn */ 0,1,2,0,0,0,3,BC_ISTYPE,0,12,0,BC_LEN,1,0,0,BC_RET1,1,2,0, -/* table.remove */ 0,2,10,0,0,2,30,BC_ISTYPE,0,12,0,BC_LEN,2,0,0,BC_ISNEP,1,0, -0,BC_JMP,3,7,128,BC_ISEQN,2,0,0,BC_JMP,3,23,128,BC_TGETR,3,2,0,BC_KPRI,4,0,0, -BC_TSETR,4,2,0,BC_RET1,3,2,0,BC_JMP,3,18,128,BC_ISTYPE,1,14,0,BC_KSHORT,3,1,0, -BC_ISGT,3,1,0,BC_JMP,3,14,128,BC_ISGT,1,2,0,BC_JMP,3,12,128,BC_TGETR,3,1,0, -BC_ADDVN,4,1,1,BC_MOV,5,2,0,BC_KSHORT,6,1,0,BC_FORI,4,4,128,BC_SUBVN,8,1,7, -BC_TGETR,9,7,0,BC_TSETR,9,8,0,BC_FORL,4,252,127,BC_KPRI,4,0,0,BC_TSETR,4,2,0, -BC_RET1,3,2,0,BC_RET0,0,1,0,0,2, -/* table.move */ 0,5,12,0,0,0,35,BC_ISTYPE,0,12,0,BC_ISTYPE,1,14,0,BC_ISTYPE, -2,14,0,BC_ISTYPE,3,14,0,BC_ISNEP,4,0,0,BC_JMP,5,1,128,BC_MOV,4,0,0,BC_ISTYPE, -4,12,0,BC_ISGT,1,2,0,BC_JMP,5,24,128,BC_SUBVV,5,1,3,BC_ISLT,2,3,0,BC_JMP,6,4, -128,BC_ISLE,3,1,0,BC_JMP,6,2,128,BC_ISEQV,4,0,0,BC_JMP,6,9,128,BC_MOV,6,1,0, -BC_MOV,7,2,0,BC_KSHORT,8,1,0,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0, -BC_TSETR,11,10,4,BC_FORL,6,252,127,BC_JMP,6,8,128,BC_MOV,6,2,0,BC_MOV,7,1,0, -BC_KSHORT,8,255,255,BC_FORI,6,4,128,BC_ADDVV,10,5,9,BC_TGETR,11,9,0,BC_TSETR, -11,10,4,BC_FORL,6,252,127,BC_RET1,4,2,0, -#endif -0 -}; - -static const struct { const char *name; int ofs; } libbc_map[] = { -{"math_deg",0}, -{"math_rad",25}, -{"string_len",50}, -{"table_foreachi",69}, -{"table_foreach",136}, -{"table_getn",213}, -{"table_remove",232}, -{"table_move",361}, -{NULL,508} -}; - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_peobj.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_peobj.c deleted file mode 100644 index b030f23..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/buildvm_peobj.c +++ /dev/null @@ -1,379 +0,0 @@ -/* -** LuaJIT VM builder: PE object emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Only used for building on Windows, since we cannot assume the presence -** of a suitable assembler. The host and target byte order must match. -*/ - -#include "buildvm.h" -#include "lj_bc.h" - -#if LJ_TARGET_X86ORX64 - -/* Context for PE object emitter. */ -static char *strtab; -static size_t strtabofs; - -/* -- PE object definitions ----------------------------------------------- */ - -/* PE header. */ -typedef struct PEheader { - uint16_t arch; - uint16_t nsects; - uint32_t time; - uint32_t symtabofs; - uint32_t nsyms; - uint16_t opthdrsz; - uint16_t flags; -} PEheader; - -/* PE section. */ -typedef struct PEsection { - char name[8]; - uint32_t vsize; - uint32_t vaddr; - uint32_t size; - uint32_t ofs; - uint32_t relocofs; - uint32_t lineofs; - uint16_t nreloc; - uint16_t nline; - uint32_t flags; -} PEsection; - -/* PE relocation. */ -typedef struct PEreloc { - uint32_t vaddr; - uint32_t symidx; - uint16_t type; -} PEreloc; - -/* Cannot use sizeof, because it pads up to the max. alignment. */ -#define PEOBJ_RELOC_SIZE (4+4+2) - -/* PE symbol table entry. */ -typedef struct PEsym { - union { - char name[8]; - uint32_t nameref[2]; - } n; - uint32_t value; - int16_t sect; - uint16_t type; - uint8_t scl; - uint8_t naux; -} PEsym; - -/* PE symbol table auxiliary entry for a section. */ -typedef struct PEsymaux { - uint32_t size; - uint16_t nreloc; - uint16_t nline; - uint32_t cksum; - uint16_t assoc; - uint8_t comdatsel; - uint8_t unused[3]; -} PEsymaux; - -/* Cannot use sizeof, because it pads up to the max. alignment. */ -#define PEOBJ_SYM_SIZE (8+4+2+2+1+1) - -/* PE object CPU specific defines. */ -#if LJ_TARGET_X86 -#define PEOBJ_ARCH_TARGET 0x014c -#define PEOBJ_RELOC_REL32 0x14 /* MS: REL32, GNU: DISP32. */ -#define PEOBJ_RELOC_DIR32 0x06 -#define PEOBJ_RELOC_OFS 0 -#define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */ -#elif LJ_TARGET_X64 -#define PEOBJ_ARCH_TARGET 0x8664 -#define PEOBJ_RELOC_REL32 0x04 /* MS: REL32, GNU: DISP32. */ -#define PEOBJ_RELOC_DIR32 0x02 -#define PEOBJ_RELOC_ADDR32NB 0x03 -#define PEOBJ_RELOC_OFS 0 -#define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */ -#endif - -/* Section numbers (0-based). */ -enum { - PEOBJ_SECT_ABS = -2, - PEOBJ_SECT_UNDEF = -1, - PEOBJ_SECT_TEXT, -#if LJ_TARGET_X64 - PEOBJ_SECT_PDATA, - PEOBJ_SECT_XDATA, -#elif LJ_TARGET_X86 - PEOBJ_SECT_SXDATA, -#endif - PEOBJ_SECT_RDATA_Z, - PEOBJ_NSECTIONS -}; - -/* Symbol types. */ -#define PEOBJ_TYPE_NULL 0 -#define PEOBJ_TYPE_FUNC 0x20 - -/* Symbol storage class. */ -#define PEOBJ_SCL_EXTERN 2 -#define PEOBJ_SCL_STATIC 3 - -/* -- PE object emitter --------------------------------------------------- */ - -/* Emit PE object symbol. */ -static void emit_peobj_sym(BuildCtx *ctx, const char *name, uint32_t value, - int sect, int type, int scl) -{ - PEsym sym; - size_t len = strlen(name); - if (!strtab) { /* Pass 1: only calculate string table length. */ - if (len > 8) strtabofs += len+1; - return; - } - if (len <= 8) { - memcpy(sym.n.name, name, len); - memset(sym.n.name+len, 0, 8-len); - } else { - sym.n.nameref[0] = 0; - sym.n.nameref[1] = (uint32_t)strtabofs; - memcpy(strtab + strtabofs, name, len); - strtab[strtabofs+len] = 0; - strtabofs += len+1; - } - sym.value = value; - sym.sect = (int16_t)(sect+1); /* 1-based section number. */ - sym.type = (uint16_t)type; - sym.scl = (uint8_t)scl; - sym.naux = 0; - owrite(ctx, &sym, PEOBJ_SYM_SIZE); -} - -/* Emit PE object section symbol. */ -static void emit_peobj_sym_sect(BuildCtx *ctx, PEsection *pesect, int sect) -{ - PEsym sym; - PEsymaux aux; - if (!strtab) return; /* Pass 1: no output. */ - memcpy(sym.n.name, pesect[sect].name, 8); - sym.value = 0; - sym.sect = (int16_t)(sect+1); /* 1-based section number. */ - sym.type = PEOBJ_TYPE_NULL; - sym.scl = PEOBJ_SCL_STATIC; - sym.naux = 1; - owrite(ctx, &sym, PEOBJ_SYM_SIZE); - memset(&aux, 0, sizeof(PEsymaux)); - aux.size = pesect[sect].size; - aux.nreloc = pesect[sect].nreloc; - owrite(ctx, &aux, PEOBJ_SYM_SIZE); -} - -/* Emit Windows PE object file. */ -void emit_peobj(BuildCtx *ctx) -{ - PEheader pehdr; - PEsection pesect[PEOBJ_NSECTIONS]; - uint32_t sofs; - int i, nrsym; - union { uint8_t b; uint32_t u; } host_endian; - - sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection); - - /* Fill in PE sections. */ - memset(&pesect, 0, PEOBJ_NSECTIONS*sizeof(PEsection)); - memcpy(pesect[PEOBJ_SECT_TEXT].name, ".text", sizeof(".text")-1); - pesect[PEOBJ_SECT_TEXT].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_TEXT].size = (uint32_t)ctx->codesz); - pesect[PEOBJ_SECT_TEXT].relocofs = sofs; - sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE; - /* Flags: 60 = read+execute, 50 = align16, 20 = code. */ - pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS; - -#if LJ_TARGET_X64 - memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1); - pesect[PEOBJ_SECT_PDATA].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_PDATA].size = 6*4); - pesect[PEOBJ_SECT_PDATA].relocofs = sofs; - sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = 6) * PEOBJ_RELOC_SIZE; - /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ - pesect[PEOBJ_SECT_PDATA].flags = 0x40300040; - - memcpy(pesect[PEOBJ_SECT_XDATA].name, ".xdata", sizeof(".xdata")-1); - pesect[PEOBJ_SECT_XDATA].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_XDATA].size = 8*2+4+6*2); /* See below. */ - pesect[PEOBJ_SECT_XDATA].relocofs = sofs; - sofs += (pesect[PEOBJ_SECT_XDATA].nreloc = 1) * PEOBJ_RELOC_SIZE; - /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ - pesect[PEOBJ_SECT_XDATA].flags = 0x40300040; -#elif LJ_TARGET_X86 - memcpy(pesect[PEOBJ_SECT_SXDATA].name, ".sxdata", sizeof(".sxdata")-1); - pesect[PEOBJ_SECT_SXDATA].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_SXDATA].size = 4); - pesect[PEOBJ_SECT_SXDATA].relocofs = sofs; - /* Flags: 40 = read, 30 = align4, 02 = lnk_info, 40 = initialized data. */ - pesect[PEOBJ_SECT_SXDATA].flags = 0x40300240; -#endif - - memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1); - pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs; - sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1); - /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ - pesect[PEOBJ_SECT_RDATA_Z].flags = 0x40300040; - - /* Fill in PE header. */ - pehdr.arch = PEOBJ_ARCH_TARGET; - pehdr.nsects = PEOBJ_NSECTIONS; - pehdr.time = 0; /* Timestamp is optional. */ - pehdr.symtabofs = sofs; - pehdr.opthdrsz = 0; - pehdr.flags = 0; - - /* Compute the size of the symbol table: - ** @feat.00 + nsections*2 - ** + asm_start + nsym - ** + nrsym - */ - nrsym = ctx->nrelocsym; - pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+ctx->nsym + nrsym; -#if LJ_TARGET_X64 - pehdr.nsyms += 1; /* Symbol for lj_err_unwind_win. */ -#endif - - /* Write PE object header and all sections. */ - owrite(ctx, &pehdr, sizeof(PEheader)); - owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS); - - /* Write .text section. */ - host_endian.u = 1; - if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) { - fprintf(stderr, "Error: different byte order for host and target\n"); - exit(1); - } - owrite(ctx, ctx->code, ctx->codesz); - for (i = 0; i < ctx->nreloc; i++) { - PEreloc reloc; - reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS; - reloc.symidx = 1+2+ctx->reloc[i].sym; /* Reloc syms are after .text sym. */ - reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - } - -#if LJ_TARGET_X64 - { /* Write .pdata section. */ - uint32_t fcofs = (uint32_t)ctx->sym[ctx->nsym-1].ofs; - uint32_t pdata[3]; /* Start of .text, end of .text and .xdata. */ - PEreloc reloc; - pdata[0] = 0; pdata[1] = fcofs; pdata[2] = 0; - owrite(ctx, &pdata, sizeof(pdata)); - pdata[0] = fcofs; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 20; - owrite(ctx, &pdata, sizeof(pdata)); - reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 16; reloc.symidx = 1+2+nrsym+2+2+1; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - reloc.vaddr = 20; reloc.symidx = 1+2+nrsym+2; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - } - { /* Write .xdata section. */ - uint16_t xdata[8+2+6]; - PEreloc reloc; - xdata[0] = 0x01|0x08|0x10; /* Ver. 1, uhandler/ehandler, prolog size 0. */ - xdata[1] = 0x0005; /* Number of unwind codes, no frame pointer. */ - xdata[2] = 0x4200; /* Stack offset 4*8+8 = aword*5. */ - xdata[3] = 0x3000; /* Push rbx. */ - xdata[4] = 0x6000; /* Push rsi. */ - xdata[5] = 0x7000; /* Push rdi. */ - xdata[6] = 0x5000; /* Push rbp. */ - xdata[7] = 0; /* Alignment. */ - xdata[8] = xdata[9] = 0; /* Relocated address of exception handler. */ - xdata[10] = 0x01; /* Ver. 1, no handler, prolog size 0. */ - xdata[11] = 0x1504; /* Number of unwind codes, fp = rbp, fpofs = 16. */ - xdata[12] = 0x0300; /* set_fpreg. */ - xdata[13] = 0x0200; /* stack offset 0*8+8 = aword*1. */ - xdata[14] = 0x3000; /* Push rbx. */ - xdata[15] = 0x5000; /* Push rbp. */ - owrite(ctx, &xdata, sizeof(xdata)); - reloc.vaddr = 2*8; reloc.symidx = 1+2+nrsym+2+2; - reloc.type = PEOBJ_RELOC_ADDR32NB; - owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); - } -#elif LJ_TARGET_X86 - /* Write .sxdata section. */ - for (i = 0; i < nrsym; i++) { - if (!strcmp(ctx->relocsym[i], "_lj_err_unwind_win")) { - uint32_t symidx = 1+2+i; - owrite(ctx, &symidx, 4); - break; - } - } - if (i == nrsym) { - fprintf(stderr, "Error: extern lj_err_unwind_win not used\n"); - exit(1); - } -#endif - - /* Write .rdata$Z section. */ - owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1); - - /* Write symbol table. */ - strtab = NULL; /* 1st pass: collect string sizes. */ - for (;;) { - strtabofs = 4; - /* Mark as SafeSEH compliant. */ - emit_peobj_sym(ctx, "@feat.00", 1, - PEOBJ_SECT_ABS, PEOBJ_TYPE_NULL, PEOBJ_SCL_STATIC); - - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_TEXT); - for (i = 0; i < nrsym; i++) - emit_peobj_sym(ctx, ctx->relocsym[i], 0, - PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); - -#if LJ_TARGET_X64 - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_PDATA); - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_XDATA); - emit_peobj_sym(ctx, "lj_err_unwind_win", 0, - PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); -#elif LJ_TARGET_X86 - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_SXDATA); -#endif - - emit_peobj_sym(ctx, ctx->beginsym, 0, - PEOBJ_SECT_TEXT, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN); - for (i = 0; i < ctx->nsym; i++) - emit_peobj_sym(ctx, ctx->sym[i].name, (uint32_t)ctx->sym[i].ofs, - PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); - - emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z); - - if (strtab) - break; - /* 2nd pass: alloc strtab, write syms and copy strings. */ - strtab = (char *)malloc(strtabofs); - *(uint32_t *)strtab = (uint32_t)strtabofs; - } - - /* Write string table. */ - owrite(ctx, strtab, strtabofs); -} - -#else - -void emit_peobj(BuildCtx *ctx) -{ - UNUSED(ctx); - fprintf(stderr, "Error: no PE object support for this target\n"); - exit(1); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genlibbc.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genlibbc.lua deleted file mode 100644 index ba18812..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genlibbc.lua +++ /dev/null @@ -1,225 +0,0 @@ ----------------------------------------------------------------------------- --- Lua script to dump the bytecode of the library functions written in Lua. --- The resulting 'buildvm_libbc.h' is used for the build process of LuaJIT. ----------------------------------------------------------------------------- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- - -local ffi = require("ffi") -local bit = require("bit") -local vmdef = require("jit.vmdef") -local bcnames = vmdef.bcnames - -local format = string.format - -local isbe = (string.byte(string.dump(function() end), 5) % 2 == 1) - -local function usage(arg) - io.stderr:write("Usage: ", arg and arg[0] or "genlibbc", - " [-o buildvm_libbc.h] lib_*.c\n") - os.exit(1) -end - -local function parse_arg(arg) - local outfile = "-" - if not (arg and arg[1]) then - usage(arg) - end - if arg[1] == "-o" then - outfile = arg[2] - if not outfile then usage(arg) end - table.remove(arg, 1) - table.remove(arg, 1) - end - return outfile -end - -local function read_files(names) - local src = "" - for _,name in ipairs(names) do - local fp = assert(io.open(name)) - src = src .. fp:read("*a") - fp:close() - end - return src -end - -local function transform_lua(code) - local fixup = {} - local n = -30000 - code = string.gsub(code, "CHECK_(%w*)%((.-)%)", function(tp, var) - n = n + 1 - fixup[n] = { "CHECK", tp } - return format("%s=%d", var, n) - end) - code = string.gsub(code, "PAIRS%((.-)%)", function(var) - fixup.PAIRS = true - return format("nil, %s, 0x4dp80", var) - end) - return "return "..code, fixup -end - -local function read_uleb128(p) - local v = p[0]; p = p + 1 - if v >= 128 then - local sh = 7; v = v - 128 - repeat - local r = p[0] - v = v + bit.lshift(bit.band(r, 127), sh) - sh = sh + 7 - p = p + 1 - until r < 128 - end - return p, v -end - --- ORDER LJ_T -local name2itype = { - str = 5, func = 9, tab = 12, int = 14, num = 15 -} - -local BC, BCN = {}, {} -for i=0,#bcnames/6-1 do - local name = bcnames:sub(i*6+1, i*6+6):gsub(" ", "") - BC[name] = i - BCN[i] = name -end -local xop, xra = isbe and 3 or 0, isbe and 2 or 1 -local xrc, xrb = isbe and 1 or 2, isbe and 0 or 3 - -local function fixup_dump(dump, fixup) - local buf = ffi.new("uint8_t[?]", #dump+1, dump) - local p = buf+5 - local n, sizebc - p, n = read_uleb128(p) - local start = p - p = p + 4 - p = read_uleb128(p) - p = read_uleb128(p) - p, sizebc = read_uleb128(p) - local startbc = tonumber(p - start) - local rawtab = {} - for i=0,sizebc-1 do - local op = p[xop] - if op == BC.KSHORT then - local rd = p[xrc] + 256*p[xrb] - rd = bit.arshift(bit.lshift(rd, 16), 16) - local f = fixup[rd] - if f then - if f[1] == "CHECK" then - local tp = f[2] - if tp == "tab" then rawtab[p[xra]] = true end - p[xop] = tp == "num" and BC.ISNUM or BC.ISTYPE - p[xrb] = 0 - p[xrc] = name2itype[tp] - else - error("unhandled fixup type: "..f[1]) - end - end - elseif op == BC.TGETV then - if rawtab[p[xrb]] then - p[xop] = BC.TGETR - end - elseif op == BC.TSETV then - if rawtab[p[xrb]] then - p[xop] = BC.TSETR - end - elseif op == BC.ITERC then - if fixup.PAIRS then - p[xop] = BC.ITERN - end - end - p = p + 4 - end - local ndump = ffi.string(start, n) - -- Fixup hi-part of 0x4dp80 to LJ_KEYINDEX. - ndump = ndump:gsub("\x80\x80\xcd\xaa\x04", "\xff\xff\xf9\xff\x0f") - return { dump = ndump, startbc = startbc, sizebc = sizebc } -end - -local function find_defs(src) - local defs = {} - for name, code in string.gmatch(src, "LJLIB_LUA%(([^)]*)%)%s*/%*(.-)%*/") do - local env = {} - local tcode, fixup = transform_lua(code) - local func = assert(load(tcode, "", nil, env))() - defs[name] = fixup_dump(string.dump(func, true), fixup) - defs[#defs+1] = name - end - return defs -end - -local function gen_header(defs) - local t = {} - local function w(x) t[#t+1] = x end - w("/* This is a generated file. DO NOT EDIT! */\n\n") - w("static const int libbc_endian = ") w(isbe and 1 or 0) w(";\n\n") - local s, sb = "", "" - for i,name in ipairs(defs) do - local d = defs[name] - s = s .. d.dump - sb = sb .. string.char(i) .. ("\0"):rep(d.startbc - 1) - .. (isbe and "\0\0\0\255" or "\255\0\0\0"):rep(d.sizebc) - .. ("\0"):rep(#d.dump - d.startbc - d.sizebc*4) - end - w("static const uint8_t libbc_code[] = {\n") - local n = 0 - for i=1,#s do - local x = string.byte(s, i) - local xb = string.byte(sb, i) - if xb == 255 then - local name = BCN[x] - local m = #name + 4 - if n + m > 78 then n = 0; w("\n") end - n = n + m - w("BC_"); w(name) - else - local m = x < 10 and 2 or (x < 100 and 3 or 4) - if xb == 0 then - if n + m > 78 then n = 0; w("\n") end - else - local name = defs[xb]:gsub("_", ".") - if n ~= 0 then w("\n") end - w("/* "); w(name); w(" */ ") - n = #name + 7 - end - n = n + m - w(x) - end - w(",") - end - w("\n0\n};\n\n") - w("static const struct { const char *name; int ofs; } libbc_map[] = {\n") - local m = 0 - for _,name in ipairs(defs) do - w('{"'); w(name); w('",'); w(m) w('},\n') - m = m + #defs[name].dump - end - w("{NULL,"); w(m); w("}\n};\n\n") - return table.concat(t) -end - -local function write_file(name, data) - if name == "-" then - assert(io.write(data)) - assert(io.flush()) - else - local fp = io.open(name) - if fp then - local old = fp:read("*a") - fp:close() - if data == old then return end - end - fp = assert(io.open(name, "w")) - assert(fp:write(data)) - assert(fp:close()) - end -end - -local outfile = parse_arg(arg) -local src = read_files(arg) -local defs = find_defs(src) -local hdr = gen_header(defs) -write_file(outfile, hdr) - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genminilua.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genminilua.lua deleted file mode 100644 index e8e86c5..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/genminilua.lua +++ /dev/null @@ -1,436 +0,0 @@ ----------------------------------------------------------------------------- --- Lua script to generate a customized, minified version of Lua. --- The resulting 'minilua' is used for the build process of LuaJIT. ----------------------------------------------------------------------------- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- - -local sub, match, gsub = string.sub, string.match, string.gsub - -local LUA_VERSION = "5.1.5" -local LUA_SOURCE - -local function usage() - io.stderr:write("Usage: ", arg and arg[0] or "genminilua", - " lua-", LUA_VERSION, "-source-dir\n") - os.exit(1) -end - -local function find_sources() - LUA_SOURCE = arg and arg[1] - if not LUA_SOURCE then usage() end - if sub(LUA_SOURCE, -1) ~= "/" then LUA_SOURCE = LUA_SOURCE.."/" end - local fp = io.open(LUA_SOURCE .. "lua.h") - if not fp then - LUA_SOURCE = LUA_SOURCE.."src/" - fp = io.open(LUA_SOURCE .. "lua.h") - if not fp then usage() end - end - local all = fp:read("*a") - fp:close() - if not match(all, 'LUA_RELEASE%s*"Lua '..LUA_VERSION..'"') then - io.stderr:write("Error: version mismatch\n") - usage() - end -end - -local LUA_FILES = { -"lmem.c", "lobject.c", "ltm.c", "lfunc.c", "ldo.c", "lstring.c", "ltable.c", -"lgc.c", "lstate.c", "ldebug.c", "lzio.c", "lopcodes.c", -"llex.c", "lcode.c", "lparser.c", "lvm.c", "lapi.c", "lauxlib.c", -"lbaselib.c", "ltablib.c", "liolib.c", "loslib.c", "lstrlib.c", "linit.c", -} - -local REMOVE_LIB = {} -gsub([[ -collectgarbage dofile gcinfo getfenv getmetatable load print rawequal rawset -select tostring xpcall -foreach foreachi getn maxn setn -popen tmpfile seek setvbuf __tostring -clock date difftime execute getenv rename setlocale time tmpname -dump gfind len reverse -LUA_LOADLIBNAME LUA_MATHLIBNAME LUA_DBLIBNAME -]], "%S+", function(name) - REMOVE_LIB[name] = true -end) - -local REMOVE_EXTINC = { [""] = true, [""] = true, } - -local CUSTOM_MAIN = [[ -typedef unsigned int UB; -static UB barg(lua_State *L,int idx){ -union{lua_Number n;U64 b;}bn; -bn.n=lua_tonumber(L,idx)+6755399441055744.0; -if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); -return(UB)bn.b; -} -#define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1; -static int tobit(lua_State *L){ -BRET(barg(L,1))} -static int bnot(lua_State *L){ -BRET(~barg(L,1))} -static int band(lua_State *L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} -static int bor(lua_State *L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} -static int bxor(lua_State *L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} -static int lshift(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET(b<>n)} -static int arshift(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} -static int rol(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b<>(32-n)))} -static int ror(lua_State *L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} -static int bswap(lua_State *L){ -UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} -static int tohex(lua_State *L){ -UB b=barg(L,1); -int n=lua_isnone(L,2)?8:(int)barg(L,2); -const char *hexdigits="0123456789abcdef"; -char buf[8]; -int i; -if(n<0){n=-n;hexdigits="0123456789ABCDEF";} -if(n>8)n=8; -for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} -lua_pushlstring(L,buf,(size_t)n); -return 1; -} -static const struct luaL_Reg bitlib[] = { -{"tobit",tobit}, -{"bnot",bnot}, -{"band",band}, -{"bor",bor}, -{"bxor",bxor}, -{"lshift",lshift}, -{"rshift",rshift}, -{"arshift",arshift}, -{"rol",rol}, -{"ror",ror}, -{"bswap",bswap}, -{"tohex",tohex}, -{NULL,NULL} -}; -int main(int argc, char **argv){ - lua_State *L = luaL_newstate(); - int i; - luaL_openlibs(L); - luaL_register(L, "bit", bitlib); - if (argc < 2) return sizeof(void *); - lua_createtable(L, 0, 1); - lua_pushstring(L, argv[1]); - lua_rawseti(L, -2, 0); - lua_setglobal(L, "arg"); - if (luaL_loadfile(L, argv[1])) - goto err; - for (i = 2; i < argc; i++) - lua_pushstring(L, argv[i]); - if (lua_pcall(L, argc - 2, 0, 0)) { - err: - fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); - return 1; - } - lua_close(L); - return 0; -} -]] - -local function read_sources() - local t = {} - for i, name in ipairs(LUA_FILES) do - local fp = assert(io.open(LUA_SOURCE..name, "r")) - t[i] = fp:read("*a") - assert(fp:close()) - end - t[#t+1] = CUSTOM_MAIN - return table.concat(t) -end - -local includes = {} - -local function merge_includes(src) - return gsub(src, '#include%s*"([^"]*)"%s*\n', function(name) - if includes[name] then return "" end - includes[name] = true - local fp = assert(io.open(LUA_SOURCE..name, "r")) - local inc = fp:read("*a") - assert(fp:close()) - inc = gsub(inc, "#ifndef%s+%w+_h\n#define%s+%w+_h\n", "") - inc = gsub(inc, "#endif%s*$", "") - return merge_includes(inc) - end) -end - -local function get_license(src) - return match(src, "/%*+\n%* Copyright %(.-%*/\n") -end - -local function fold_lines(src) - return gsub(src, "\\\n", " ") -end - -local strings = {} - -local function save_str(str) - local n = #strings+1 - strings[n] = str - return "\1"..n.."\2" -end - -local function save_strings(src) - src = gsub(src, '"[^"\n]*"', save_str) - return gsub(src, "'[^'\n]*'", save_str) -end - -local function restore_strings(src) - return gsub(src, "\1(%d+)\2", function(numstr) - return strings[tonumber(numstr)] - end) -end - -local function def_istrue(def) - return def == "INT_MAX > 2147483640L" or - def == "LUAI_BITSINT >= 32" or - def == "SIZE_Bx < LUAI_BITSINT-1" or - def == "cast" or - def == "defined(LUA_CORE)" or - def == "MINSTRTABSIZE" or - def == "LUA_MINBUFFER" or - def == "HARDSTACKTESTS" or - def == "UNUSED" -end - -local head, defs = {[[ -#ifdef _MSC_VER -typedef unsigned __int64 U64; -#else -typedef unsigned long long U64; -#endif -int _CRT_glob = 0; -]]}, {} - -local function preprocess(src) - local t = { match(src, "^(.-)#") } - local lvl, on, oldon = 0, true, {} - for pp, def, txt in string.gmatch(src, "#(%w+) *([^\n]*)\n([^#]*)") do - if pp == "if" or pp == "ifdef" or pp == "ifndef" then - lvl = lvl + 1 - oldon[lvl] = on - on = def_istrue(def) - elseif pp == "else" then - if oldon[lvl] then - if on == false then on = true else on = false end - end - elseif pp == "elif" then - if oldon[lvl] then - on = def_istrue(def) - end - elseif pp == "endif" then - on = oldon[lvl] - lvl = lvl - 1 - elseif on then - if pp == "include" then - if not head[def] and not REMOVE_EXTINC[def] then - head[def] = true - head[#head+1] = "#include "..def.."\n" - end - elseif pp == "define" then - local k, sp, v = match(def, "([%w_]+)(%s*)(.*)") - if k and not (sp == "" and sub(v, 1, 1) == "(") then - defs[k] = gsub(v, "%a[%w_]*", function(tok) - return defs[tok] or tok - end) - else - t[#t+1] = "#define "..def.."\n" - end - elseif pp ~= "undef" then - error("unexpected directive: "..pp.." "..def) - end - end - if on then t[#t+1] = txt end - end - return gsub(table.concat(t), "%a[%w_]*", function(tok) - return defs[tok] or tok - end) -end - -local function merge_header(src, license) - local hdr = string.format([[ -/* This is a heavily customized and minimized copy of Lua %s. */ -/* It's only used to build LuaJIT. It does NOT have all standard functions! */ -]], LUA_VERSION) - return hdr..license..table.concat(head)..src -end - -local function strip_unused1(src) - return gsub(src, '( {"?([%w_]+)"?,%s+%a[%w_]*},\n)', function(line, func) - return REMOVE_LIB[func] and "" or line - end) -end - -local function strip_unused2(src) - return gsub(src, "Symbolic Execution.-}=", "") -end - -local function strip_unused3(src) - src = gsub(src, "extern", "static") - src = gsub(src, "\nstatic([^\n]-)%(([^)]*)%)%(", "\nstatic%1 %2(") - src = gsub(src, "#define lua_assert[^\n]*\n", "") - src = gsub(src, "lua_assert%b();?", "") - src = gsub(src, "default:\n}", "default:;\n}") - src = gsub(src, "lua_lock%b();", "") - src = gsub(src, "lua_unlock%b();", "") - src = gsub(src, "luai_threadyield%b();", "") - src = gsub(src, "luai_userstateopen%b();", "{}") - src = gsub(src, "luai_userstate%w+%b();", "") - src = gsub(src, "%(%(c==.*luaY_parser%)", "luaY_parser") - src = gsub(src, "trydecpoint%(ls,seminfo%)", - "luaX_lexerror(ls,\"malformed number\",TK_NUMBER)") - src = gsub(src, "int c=luaZ_lookahead%b();", "") - src = gsub(src, "luaL_register%(L,[^,]*,co_funcs%);\nreturn 2;", - "return 1;") - src = gsub(src, "getfuncname%b():", "NULL:") - src = gsub(src, "getobjname%b():", "NULL:") - src = gsub(src, "if%([^\n]*hookmask[^\n]*%)\n[^\n]*\n", "") - src = gsub(src, "if%([^\n]*hookmask[^\n]*%)%b{}\n", "") - src = gsub(src, "if%([^\n]*hookmask[^\n]*&&\n[^\n]*%b{}\n", "") - src = gsub(src, "(twoto%b()%()", "%1(size_t)") - src = gsub(src, "imaxstacksize)%)", "%1+p->numparams)") - src = gsub(src, "if%(sep==%-1%)(return'%[';)\nelse (luaX_lexerror%b();)", "if (sep!=-1)%2\n%1") - return gsub(src, "(default:{\nNode%*n=mainposition)", "/*fallthrough*/\n%1") -end - -local function func_gather(src) - local nodes, list = {}, {} - local pos, len = 1, #src - while pos < len do - local d, w = match(src, "^(#define ([%w_]+)[^\n]*\n)", pos) - if d then - local n = #list+1 - list[n] = d - nodes[w] = n - else - local s - d, w, s = match(src, "^(([%w_]+)[^\n]*([{;])\n)", pos) - if not d then - d, w, s = match(src, "^(([%w_]+)[^(]*%b()([{;])\n)", pos) - if not d then d = match(src, "^[^\n]*\n", pos) end - end - if s == "{" then - d = d..sub(match(src, "^%b{}[^;\n]*;?\n", pos+#d-2), 3) - if sub(d, -2) == "{\n" then - d = d..sub(match(src, "^%b{}[^;\n]*;?\n", pos+#d-2), 3) - end - end - local k, v = nil, d - if w == "typedef" then - if match(d, "^typedef enum") then - head[#head+1] = d - else - k = match(d, "([%w_]+);\n$") - if not k then k = match(d, "^.-%(.-([%w_]+)%)%(") end - end - elseif w == "enum" then - head[#head+1] = v - elseif w ~= nil then - k = match(d, "^[^\n]-([%w_]+)[(%[=]") - if k then - if w ~= "static" and k ~= "main" then v = "static "..d end - else - k = w - end - end - if w and k then - local o = nodes[k] - if o then nodes["*"..k] = o end - local n = #list+1 - list[n] = v - nodes[k] = n - end - end - pos = pos + #d - end - return nodes, list -end - -local function func_visit(nodes, list, used, n) - local i = nodes[n] - for m in string.gmatch(list[i], "[%w_]+") do - if nodes[m] then - local j = used[m] - if not j then - used[m] = i - func_visit(nodes, list, used, m) - elseif i < j then - used[m] = i - end - end - end -end - -local function func_collect(src) - local nodes, list = func_gather(src) - local used = {} - func_visit(nodes, list, used, "main") - for n,i in pairs(nodes) do - local j = used[n] - if j and j < i then used["*"..n] = j end - end - for n,i in pairs(nodes) do - if not used[n] then list[i] = "" end - end - return table.concat(list) -end - -find_sources() -local src = read_sources() -src = merge_includes(src) -local license = get_license(src) -src = fold_lines(src) -src = strip_unused1(src) -src = save_strings(src) -src = strip_unused2(src) -src = strip_comments(src) -src = preprocess(src) -src = strip_whitespace(src) -src = strip_unused3(src) -src = rename_tokens1(src) -src = func_collect(src) -src = rename_tokens2(src) -src = restore_strings(src) -src = fix_bugs_and_warnings(src) -src = merge_header(src, license) -io.write(src) diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/minilua.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/minilua.c deleted file mode 100644 index 76f32ae..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/host/minilua.c +++ /dev/null @@ -1,7771 +0,0 @@ -/* This is a heavily customized and minimized copy of Lua 5.1.5. */ -/* It's only used to build LuaJIT. It does NOT have all standard functions! */ -/****************************************************************************** -* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ -#ifdef _MSC_VER -typedef unsigned __int64 U64; -#else -typedef unsigned long long U64; -#endif -int _CRT_glob = 0; -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -typedef enum{ -TM_INDEX, -TM_NEWINDEX, -TM_GC, -TM_MODE, -TM_EQ, -TM_ADD, -TM_SUB, -TM_MUL, -TM_DIV, -TM_MOD, -TM_POW, -TM_UNM, -TM_LEN, -TM_LT, -TM_LE, -TM_CONCAT, -TM_CALL, -TM_N -}TMS; -enum OpMode{iABC,iABx,iAsBx}; -typedef enum{ -OP_MOVE, -OP_LOADK, -OP_LOADBOOL, -OP_LOADNIL, -OP_GETUPVAL, -OP_GETGLOBAL, -OP_GETTABLE, -OP_SETGLOBAL, -OP_SETUPVAL, -OP_SETTABLE, -OP_NEWTABLE, -OP_SELF, -OP_ADD, -OP_SUB, -OP_MUL, -OP_DIV, -OP_MOD, -OP_POW, -OP_UNM, -OP_NOT, -OP_LEN, -OP_CONCAT, -OP_JMP, -OP_EQ, -OP_LT, -OP_LE, -OP_TEST, -OP_TESTSET, -OP_CALL, -OP_TAILCALL, -OP_RETURN, -OP_FORLOOP, -OP_FORPREP, -OP_TFORLOOP, -OP_SETLIST, -OP_CLOSE, -OP_CLOSURE, -OP_VARARG -}OpCode; -enum OpArgMask{ -OpArgN, -OpArgU, -OpArgR, -OpArgK -}; -typedef enum{ -VVOID, -VNIL, -VTRUE, -VFALSE, -VK, -VKNUM, -VLOCAL, -VUPVAL, -VGLOBAL, -VINDEXED, -VJMP, -VRELOCABLE, -VNONRELOC, -VCALL, -VVARARG -}expkind; -enum RESERVED{ -TK_AND=257,TK_BREAK, -TK_DO,TK_ELSE,TK_ELSEIF,TK_END,TK_FALSE,TK_FOR,TK_FUNCTION, -TK_IF,TK_IN,TK_LOCAL,TK_NIL,TK_NOT,TK_OR,TK_REPEAT, -TK_RETURN,TK_THEN,TK_TRUE,TK_UNTIL,TK_WHILE, -TK_CONCAT,TK_DOTS,TK_EQ,TK_GE,TK_LE,TK_NE,TK_NUMBER, -TK_NAME,TK_STRING,TK_EOS -}; -typedef enum BinOpr{ -OPR_ADD,OPR_SUB,OPR_MUL,OPR_DIV,OPR_MOD,OPR_POW, -OPR_CONCAT, -OPR_NE,OPR_EQ, -OPR_LT,OPR_LE,OPR_GT,OPR_GE, -OPR_AND,OPR_OR, -OPR_NOBINOPR -}BinOpr; -typedef enum UnOpr{OPR_MINUS,OPR_NOT,OPR_LEN,OPR_NOUNOPR}UnOpr; -#define LUA_QL(x)"'"x"'" -#define luai_apicheck(L,o){(void)L;} -#define lua_number2str(s,n)sprintf((s),"%.14g",(n)) -#define lua_str2number(s,p)strtod((s),(p)) -#define luai_numadd(a,b)((a)+(b)) -#define luai_numsub(a,b)((a)-(b)) -#define luai_nummul(a,b)((a)*(b)) -#define luai_numdiv(a,b)((a)/(b)) -#define luai_nummod(a,b)((a)-floor((a)/(b))*(b)) -#define luai_numpow(a,b)(pow(a,b)) -#define luai_numunm(a)(-(a)) -#define luai_numeq(a,b)((a)==(b)) -#define luai_numlt(a,b)((a)<(b)) -#define luai_numle(a,b)((a)<=(b)) -#define luai_numisnan(a)(!luai_numeq((a),(a))) -#define lua_number2int(i,d)((i)=(int)(d)) -#define lua_number2integer(i,d)((i)=(lua_Integer)(d)) -#define LUAI_THROW(L,c)longjmp((c)->b,1) -#define LUAI_TRY(L,c,a)if(setjmp((c)->b)==0){a} -#define lua_pclose(L,file)((void)((void)L,file),0) -#define lua_upvalueindex(i)((-10002)-(i)) -typedef struct lua_State lua_State; -typedef int(*lua_CFunction)(lua_State*L); -typedef const char*(*lua_Reader)(lua_State*L,void*ud,size_t*sz); -typedef void*(*lua_Alloc)(void*ud,void*ptr,size_t osize,size_t nsize); -typedef double lua_Number; -typedef ptrdiff_t lua_Integer; -static void lua_settop(lua_State*L,int idx); -static int lua_type(lua_State*L,int idx); -static const char* lua_tolstring(lua_State*L,int idx,size_t*len); -static size_t lua_objlen(lua_State*L,int idx); -static void lua_pushlstring(lua_State*L,const char*s,size_t l); -static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n); -static void lua_createtable(lua_State*L,int narr,int nrec); -static void lua_setfield(lua_State*L,int idx,const char*k); -#define lua_pop(L,n)lua_settop(L,-(n)-1) -#define lua_newtable(L)lua_createtable(L,0,0) -#define lua_pushcfunction(L,f)lua_pushcclosure(L,(f),0) -#define lua_strlen(L,i)lua_objlen(L,(i)) -#define lua_isfunction(L,n)(lua_type(L,(n))==6) -#define lua_istable(L,n)(lua_type(L,(n))==5) -#define lua_isnil(L,n)(lua_type(L,(n))==0) -#define lua_isboolean(L,n)(lua_type(L,(n))==1) -#define lua_isnone(L,n)(lua_type(L,(n))==(-1)) -#define lua_isnoneornil(L,n)(lua_type(L,(n))<=0) -#define lua_pushliteral(L,s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1) -#define lua_setglobal(L,s)lua_setfield(L,(-10002),(s)) -#define lua_tostring(L,i)lua_tolstring(L,(i),NULL) -typedef struct lua_Debug lua_Debug; -typedef void(*lua_Hook)(lua_State*L,lua_Debug*ar); -struct lua_Debug{ -int event; -const char*name; -const char*namewhat; -const char*what; -const char*source; -int currentline; -int nups; -int linedefined; -int lastlinedefined; -char short_src[60]; -int i_ci; -}; -typedef unsigned int lu_int32; -typedef size_t lu_mem; -typedef ptrdiff_t l_mem; -typedef unsigned char lu_byte; -#define IntPoint(p)((unsigned int)(lu_mem)(p)) -typedef union{double u;void*s;long l;}L_Umaxalign; -typedef double l_uacNumber; -#define check_exp(c,e)(e) -#define UNUSED(x)((void)(x)) -#define cast(t,exp)((t)(exp)) -#define cast_byte(i)cast(lu_byte,(i)) -#define cast_num(i)cast(lua_Number,(i)) -#define cast_int(i)cast(int,(i)) -typedef lu_int32 Instruction; -#define condhardstacktests(x)((void)0) -typedef union GCObject GCObject; -typedef struct GCheader{ -GCObject*next;lu_byte tt;lu_byte marked; -}GCheader; -typedef union{ -GCObject*gc; -void*p; -lua_Number n; -int b; -}Value; -typedef struct lua_TValue{ -Value value;int tt; -}TValue; -#define ttisnil(o)(ttype(o)==0) -#define ttisnumber(o)(ttype(o)==3) -#define ttisstring(o)(ttype(o)==4) -#define ttistable(o)(ttype(o)==5) -#define ttisfunction(o)(ttype(o)==6) -#define ttisboolean(o)(ttype(o)==1) -#define ttisuserdata(o)(ttype(o)==7) -#define ttisthread(o)(ttype(o)==8) -#define ttislightuserdata(o)(ttype(o)==2) -#define ttype(o)((o)->tt) -#define gcvalue(o)check_exp(iscollectable(o),(o)->value.gc) -#define pvalue(o)check_exp(ttislightuserdata(o),(o)->value.p) -#define nvalue(o)check_exp(ttisnumber(o),(o)->value.n) -#define rawtsvalue(o)check_exp(ttisstring(o),&(o)->value.gc->ts) -#define tsvalue(o)(&rawtsvalue(o)->tsv) -#define rawuvalue(o)check_exp(ttisuserdata(o),&(o)->value.gc->u) -#define uvalue(o)(&rawuvalue(o)->uv) -#define clvalue(o)check_exp(ttisfunction(o),&(o)->value.gc->cl) -#define hvalue(o)check_exp(ttistable(o),&(o)->value.gc->h) -#define bvalue(o)check_exp(ttisboolean(o),(o)->value.b) -#define thvalue(o)check_exp(ttisthread(o),&(o)->value.gc->th) -#define l_isfalse(o)(ttisnil(o)||(ttisboolean(o)&&bvalue(o)==0)) -#define checkconsistency(obj) -#define checkliveness(g,obj) -#define setnilvalue(obj)((obj)->tt=0) -#define setnvalue(obj,x){TValue*i_o=(obj);i_o->value.n=(x);i_o->tt=3;} -#define setbvalue(obj,x){TValue*i_o=(obj);i_o->value.b=(x);i_o->tt=1;} -#define setsvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=4;checkliveness(G(L),i_o);} -#define setuvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=7;checkliveness(G(L),i_o);} -#define setthvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=8;checkliveness(G(L),i_o);} -#define setclvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=6;checkliveness(G(L),i_o);} -#define sethvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=5;checkliveness(G(L),i_o);} -#define setptvalue(L,obj,x){TValue*i_o=(obj);i_o->value.gc=cast(GCObject*,(x));i_o->tt=(8+1);checkliveness(G(L),i_o);} -#define setobj(L,obj1,obj2){const TValue*o2=(obj2);TValue*o1=(obj1);o1->value=o2->value;o1->tt=o2->tt;checkliveness(G(L),o1);} -#define setttype(obj,tt)(ttype(obj)=(tt)) -#define iscollectable(o)(ttype(o)>=4) -typedef TValue*StkId; -typedef union TString{ -L_Umaxalign dummy; -struct{ -GCObject*next;lu_byte tt;lu_byte marked; -lu_byte reserved; -unsigned int hash; -size_t len; -}tsv; -}TString; -#define getstr(ts)cast(const char*,(ts)+1) -#define svalue(o)getstr(rawtsvalue(o)) -typedef union Udata{ -L_Umaxalign dummy; -struct{ -GCObject*next;lu_byte tt;lu_byte marked; -struct Table*metatable; -struct Table*env; -size_t len; -}uv; -}Udata; -typedef struct Proto{ -GCObject*next;lu_byte tt;lu_byte marked; -TValue*k; -Instruction*code; -struct Proto**p; -int*lineinfo; -struct LocVar*locvars; -TString**upvalues; -TString*source; -int sizeupvalues; -int sizek; -int sizecode; -int sizelineinfo; -int sizep; -int sizelocvars; -int linedefined; -int lastlinedefined; -GCObject*gclist; -lu_byte nups; -lu_byte numparams; -lu_byte is_vararg; -lu_byte maxstacksize; -}Proto; -typedef struct LocVar{ -TString*varname; -int startpc; -int endpc; -}LocVar; -typedef struct UpVal{ -GCObject*next;lu_byte tt;lu_byte marked; -TValue*v; -union{ -TValue value; -struct{ -struct UpVal*prev; -struct UpVal*next; -}l; -}u; -}UpVal; -typedef struct CClosure{ -GCObject*next;lu_byte tt;lu_byte marked;lu_byte isC;lu_byte nupvalues;GCObject*gclist;struct Table*env; -lua_CFunction f; -TValue upvalue[1]; -}CClosure; -typedef struct LClosure{ -GCObject*next;lu_byte tt;lu_byte marked;lu_byte isC;lu_byte nupvalues;GCObject*gclist;struct Table*env; -struct Proto*p; -UpVal*upvals[1]; -}LClosure; -typedef union Closure{ -CClosure c; -LClosure l; -}Closure; -#define iscfunction(o)(ttype(o)==6&&clvalue(o)->c.isC) -typedef union TKey{ -struct{ -Value value;int tt; -struct Node*next; -}nk; -TValue tvk; -}TKey; -typedef struct Node{ -TValue i_val; -TKey i_key; -}Node; -typedef struct Table{ -GCObject*next;lu_byte tt;lu_byte marked; -lu_byte flags; -lu_byte lsizenode; -struct Table*metatable; -TValue*array; -Node*node; -Node*lastfree; -GCObject*gclist; -int sizearray; -}Table; -#define lmod(s,size)(check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1))))) -#define twoto(x)((size_t)1<<(x)) -#define sizenode(t)(twoto((t)->lsizenode)) -static const TValue luaO_nilobject_; -#define ceillog2(x)(luaO_log2((x)-1)+1) -static int luaO_log2(unsigned int x); -#define gfasttm(g,et,e)((et)==NULL?NULL:((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->tmname[e])) -#define fasttm(l,et,e)gfasttm(G(l),et,e) -static const TValue*luaT_gettm(Table*events,TMS event,TString*ename); -#define luaM_reallocv(L,b,on,n,e)((cast(size_t,(n)+1)<=((size_t)(~(size_t)0)-2)/(e))?luaM_realloc_(L,(b),(on)*(e),(n)*(e)):luaM_toobig(L)) -#define luaM_freemem(L,b,s)luaM_realloc_(L,(b),(s),0) -#define luaM_free(L,b)luaM_realloc_(L,(b),sizeof(*(b)),0) -#define luaM_freearray(L,b,n,t)luaM_reallocv(L,(b),n,0,sizeof(t)) -#define luaM_malloc(L,t)luaM_realloc_(L,NULL,0,(t)) -#define luaM_new(L,t)cast(t*,luaM_malloc(L,sizeof(t))) -#define luaM_newvector(L,n,t)cast(t*,luaM_reallocv(L,NULL,0,n,sizeof(t))) -#define luaM_growvector(L,v,nelems,size,t,limit,e)if((nelems)+1>(size))((v)=cast(t*,luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) -#define luaM_reallocvector(L,v,oldn,n,t)((v)=cast(t*,luaM_reallocv(L,v,oldn,n,sizeof(t)))) -static void*luaM_realloc_(lua_State*L,void*block,size_t oldsize, -size_t size); -static void*luaM_toobig(lua_State*L); -static void*luaM_growaux_(lua_State*L,void*block,int*size, -size_t size_elem,int limit, -const char*errormsg); -typedef struct Zio ZIO; -#define char2int(c)cast(int,cast(unsigned char,(c))) -#define zgetc(z)(((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z)) -typedef struct Mbuffer{ -char*buffer; -size_t n; -size_t buffsize; -}Mbuffer; -#define luaZ_initbuffer(L,buff)((buff)->buffer=NULL,(buff)->buffsize=0) -#define luaZ_buffer(buff)((buff)->buffer) -#define luaZ_sizebuffer(buff)((buff)->buffsize) -#define luaZ_bufflen(buff)((buff)->n) -#define luaZ_resetbuffer(buff)((buff)->n=0) -#define luaZ_resizebuffer(L,buff,size)(luaM_reallocvector(L,(buff)->buffer,(buff)->buffsize,size,char),(buff)->buffsize=size) -#define luaZ_freebuffer(L,buff)luaZ_resizebuffer(L,buff,0) -struct Zio{ -size_t n; -const char*p; -lua_Reader reader; -void*data; -lua_State*L; -}; -static int luaZ_fill(ZIO*z); -struct lua_longjmp; -#define gt(L)(&L->l_gt) -#define registry(L)(&G(L)->l_registry) -typedef struct stringtable{ -GCObject**hash; -lu_int32 nuse; -int size; -}stringtable; -typedef struct CallInfo{ -StkId base; -StkId func; -StkId top; -const Instruction*savedpc; -int nresults; -int tailcalls; -}CallInfo; -#define curr_func(L)(clvalue(L->ci->func)) -#define ci_func(ci)(clvalue((ci)->func)) -#define f_isLua(ci)(!ci_func(ci)->c.isC) -#define isLua(ci)(ttisfunction((ci)->func)&&f_isLua(ci)) -typedef struct global_State{ -stringtable strt; -lua_Alloc frealloc; -void*ud; -lu_byte currentwhite; -lu_byte gcstate; -int sweepstrgc; -GCObject*rootgc; -GCObject**sweepgc; -GCObject*gray; -GCObject*grayagain; -GCObject*weak; -GCObject*tmudata; -Mbuffer buff; -lu_mem GCthreshold; -lu_mem totalbytes; -lu_mem estimate; -lu_mem gcdept; -int gcpause; -int gcstepmul; -lua_CFunction panic; -TValue l_registry; -struct lua_State*mainthread; -UpVal uvhead; -struct Table*mt[(8+1)]; -TString*tmname[TM_N]; -}global_State; -struct lua_State{ -GCObject*next;lu_byte tt;lu_byte marked; -lu_byte status; -StkId top; -StkId base; -global_State*l_G; -CallInfo*ci; -const Instruction*savedpc; -StkId stack_last; -StkId stack; -CallInfo*end_ci; -CallInfo*base_ci; -int stacksize; -int size_ci; -unsigned short nCcalls; -unsigned short baseCcalls; -lu_byte hookmask; -lu_byte allowhook; -int basehookcount; -int hookcount; -lua_Hook hook; -TValue l_gt; -TValue env; -GCObject*openupval; -GCObject*gclist; -struct lua_longjmp*errorJmp; -ptrdiff_t errfunc; -}; -#define G(L)(L->l_G) -union GCObject{ -GCheader gch; -union TString ts; -union Udata u; -union Closure cl; -struct Table h; -struct Proto p; -struct UpVal uv; -struct lua_State th; -}; -#define rawgco2ts(o)check_exp((o)->gch.tt==4,&((o)->ts)) -#define gco2ts(o)(&rawgco2ts(o)->tsv) -#define rawgco2u(o)check_exp((o)->gch.tt==7,&((o)->u)) -#define gco2u(o)(&rawgco2u(o)->uv) -#define gco2cl(o)check_exp((o)->gch.tt==6,&((o)->cl)) -#define gco2h(o)check_exp((o)->gch.tt==5,&((o)->h)) -#define gco2p(o)check_exp((o)->gch.tt==(8+1),&((o)->p)) -#define gco2uv(o)check_exp((o)->gch.tt==(8+2),&((o)->uv)) -#define ngcotouv(o)check_exp((o)==NULL||(o)->gch.tt==(8+2),&((o)->uv)) -#define gco2th(o)check_exp((o)->gch.tt==8,&((o)->th)) -#define obj2gco(v)(cast(GCObject*,(v))) -static void luaE_freethread(lua_State*L,lua_State*L1); -#define pcRel(pc,p)(cast(int,(pc)-(p)->code)-1) -#define getline_(f,pc)(((f)->lineinfo)?(f)->lineinfo[pc]:0) -#define resethookcount(L)(L->hookcount=L->basehookcount) -static void luaG_typeerror(lua_State*L,const TValue*o, -const char*opname); -static void luaG_runerror(lua_State*L,const char*fmt,...); -#define luaD_checkstack(L,n)if((char*)L->stack_last-(char*)L->top<=(n)*(int)sizeof(TValue))luaD_growstack(L,n);else condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1)); -#define incr_top(L){luaD_checkstack(L,1);L->top++;} -#define savestack(L,p)((char*)(p)-(char*)L->stack) -#define restorestack(L,n)((TValue*)((char*)L->stack+(n))) -#define saveci(L,p)((char*)(p)-(char*)L->base_ci) -#define restoreci(L,n)((CallInfo*)((char*)L->base_ci+(n))) -typedef void(*Pfunc)(lua_State*L,void*ud); -static int luaD_poscall(lua_State*L,StkId firstResult); -static void luaD_reallocCI(lua_State*L,int newsize); -static void luaD_reallocstack(lua_State*L,int newsize); -static void luaD_growstack(lua_State*L,int n); -static void luaD_throw(lua_State*L,int errcode); -static void*luaM_growaux_(lua_State*L,void*block,int*size,size_t size_elems, -int limit,const char*errormsg){ -void*newblock; -int newsize; -if(*size>=limit/2){ -if(*size>=limit) -luaG_runerror(L,errormsg); -newsize=limit; -} -else{ -newsize=(*size)*2; -if(newsize<4) -newsize=4; -} -newblock=luaM_reallocv(L,block,*size,newsize,size_elems); -*size=newsize; -return newblock; -} -static void*luaM_toobig(lua_State*L){ -luaG_runerror(L,"memory allocation error: block too big"); -return NULL; -} -static void*luaM_realloc_(lua_State*L,void*block,size_t osize,size_t nsize){ -global_State*g=G(L); -block=(*g->frealloc)(g->ud,block,osize,nsize); -if(block==NULL&&nsize>0) -luaD_throw(L,4); -g->totalbytes=(g->totalbytes-osize)+nsize; -return block; -} -#define resetbits(x,m)((x)&=cast(lu_byte,~(m))) -#define setbits(x,m)((x)|=(m)) -#define testbits(x,m)((x)&(m)) -#define bitmask(b)(1<<(b)) -#define bit2mask(b1,b2)(bitmask(b1)|bitmask(b2)) -#define l_setbit(x,b)setbits(x,bitmask(b)) -#define resetbit(x,b)resetbits(x,bitmask(b)) -#define testbit(x,b)testbits(x,bitmask(b)) -#define set2bits(x,b1,b2)setbits(x,(bit2mask(b1,b2))) -#define reset2bits(x,b1,b2)resetbits(x,(bit2mask(b1,b2))) -#define test2bits(x,b1,b2)testbits(x,(bit2mask(b1,b2))) -#define iswhite(x)test2bits((x)->gch.marked,0,1) -#define isblack(x)testbit((x)->gch.marked,2) -#define isgray(x)(!isblack(x)&&!iswhite(x)) -#define otherwhite(g)(g->currentwhite^bit2mask(0,1)) -#define isdead(g,v)((v)->gch.marked&otherwhite(g)&bit2mask(0,1)) -#define changewhite(x)((x)->gch.marked^=bit2mask(0,1)) -#define gray2black(x)l_setbit((x)->gch.marked,2) -#define valiswhite(x)(iscollectable(x)&&iswhite(gcvalue(x))) -#define luaC_white(g)cast(lu_byte,(g)->currentwhite&bit2mask(0,1)) -#define luaC_checkGC(L){condhardstacktests(luaD_reallocstack(L,L->stacksize-5-1));if(G(L)->totalbytes>=G(L)->GCthreshold)luaC_step(L);} -#define luaC_barrier(L,p,v){if(valiswhite(v)&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),gcvalue(v));} -#define luaC_barriert(L,t,v){if(valiswhite(v)&&isblack(obj2gco(t)))luaC_barrierback(L,t);} -#define luaC_objbarrier(L,p,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(p)))luaC_barrierf(L,obj2gco(p),obj2gco(o));} -#define luaC_objbarriert(L,t,o){if(iswhite(obj2gco(o))&&isblack(obj2gco(t)))luaC_barrierback(L,t);} -static void luaC_step(lua_State*L); -static void luaC_link(lua_State*L,GCObject*o,lu_byte tt); -static void luaC_linkupval(lua_State*L,UpVal*uv); -static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v); -static void luaC_barrierback(lua_State*L,Table*t); -#define sizestring(s)(sizeof(union TString)+((s)->len+1)*sizeof(char)) -#define sizeudata(u)(sizeof(union Udata)+(u)->len) -#define luaS_new(L,s)(luaS_newlstr(L,s,strlen(s))) -#define luaS_newliteral(L,s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1)) -#define luaS_fix(s)l_setbit((s)->tsv.marked,5) -static TString*luaS_newlstr(lua_State*L,const char*str,size_t l); -#define tostring(L,o)((ttype(o)==4)||(luaV_tostring(L,o))) -#define tonumber(o,n)(ttype(o)==3||(((o)=luaV_tonumber(o,n))!=NULL)) -#define equalobj(L,o1,o2)(ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2)) -static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2); -static const TValue*luaV_tonumber(const TValue*obj,TValue*n); -static int luaV_tostring(lua_State*L,StkId obj); -static void luaV_execute(lua_State*L,int nexeccalls); -static void luaV_concat(lua_State*L,int total,int last); -static const TValue luaO_nilobject_={{NULL},0}; -static int luaO_int2fb(unsigned int x){ -int e=0; -while(x>=16){ -x=(x+1)>>1; -e++; -} -if(x<8)return x; -else return((e+1)<<3)|(cast_int(x)-8); -} -static int luaO_fb2int(int x){ -int e=(x>>3)&31; -if(e==0)return x; -else return((x&7)+8)<<(e-1); -} -static int luaO_log2(unsigned int x){ -static const lu_byte log_2[256]={ -0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, -6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, -7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, -7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, -8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 -}; -int l=-1; -while(x>=256){l+=8;x>>=8;} -return l+log_2[x]; -} -static int luaO_rawequalObj(const TValue*t1,const TValue*t2){ -if(ttype(t1)!=ttype(t2))return 0; -else switch(ttype(t1)){ -case 0: -return 1; -case 3: -return luai_numeq(nvalue(t1),nvalue(t2)); -case 1: -return bvalue(t1)==bvalue(t2); -case 2: -return pvalue(t1)==pvalue(t2); -default: -return gcvalue(t1)==gcvalue(t2); -} -} -static int luaO_str2d(const char*s,lua_Number*result){ -char*endptr; -*result=lua_str2number(s,&endptr); -if(endptr==s)return 0; -if(*endptr=='x'||*endptr=='X') -*result=cast_num(strtoul(s,&endptr,16)); -if(*endptr=='\0')return 1; -while(isspace(cast(unsigned char,*endptr)))endptr++; -if(*endptr!='\0')return 0; -return 1; -} -static void pushstr(lua_State*L,const char*str){ -setsvalue(L,L->top,luaS_new(L,str)); -incr_top(L); -} -static const char*luaO_pushvfstring(lua_State*L,const char*fmt,va_list argp){ -int n=1; -pushstr(L,""); -for(;;){ -const char*e=strchr(fmt,'%'); -if(e==NULL)break; -setsvalue(L,L->top,luaS_newlstr(L,fmt,e-fmt)); -incr_top(L); -switch(*(e+1)){ -case's':{ -const char*s=va_arg(argp,char*); -if(s==NULL)s="(null)"; -pushstr(L,s); -break; -} -case'c':{ -char buff[2]; -buff[0]=cast(char,va_arg(argp,int)); -buff[1]='\0'; -pushstr(L,buff); -break; -} -case'd':{ -setnvalue(L->top,cast_num(va_arg(argp,int))); -incr_top(L); -break; -} -case'f':{ -setnvalue(L->top,cast_num(va_arg(argp,l_uacNumber))); -incr_top(L); -break; -} -case'p':{ -char buff[4*sizeof(void*)+8]; -sprintf(buff,"%p",va_arg(argp,void*)); -pushstr(L,buff); -break; -} -case'%':{ -pushstr(L,"%"); -break; -} -default:{ -char buff[3]; -buff[0]='%'; -buff[1]=*(e+1); -buff[2]='\0'; -pushstr(L,buff); -break; -} -} -n+=2; -fmt=e+2; -} -pushstr(L,fmt); -luaV_concat(L,n+1,cast_int(L->top-L->base)-1); -L->top-=n; -return svalue(L->top-1); -} -static const char*luaO_pushfstring(lua_State*L,const char*fmt,...){ -const char*msg; -va_list argp; -va_start(argp,fmt); -msg=luaO_pushvfstring(L,fmt,argp); -va_end(argp); -return msg; -} -static void luaO_chunkid(char*out,const char*source,size_t bufflen){ -if(*source=='='){ -strncpy(out,source+1,bufflen); -out[bufflen-1]='\0'; -} -else{ -if(*source=='@'){ -size_t l; -source++; -bufflen-=sizeof(" '...' "); -l=strlen(source); -strcpy(out,""); -if(l>bufflen){ -source+=(l-bufflen); -strcat(out,"..."); -} -strcat(out,source); -} -else{ -size_t len=strcspn(source,"\n\r"); -bufflen-=sizeof(" [string \"...\"] "); -if(len>bufflen)len=bufflen; -strcpy(out,"[string \""); -if(source[len]!='\0'){ -strncat(out,source,len); -strcat(out,"..."); -} -else -strcat(out,source); -strcat(out,"\"]"); -} -} -} -#define gnode(t,i)(&(t)->node[i]) -#define gkey(n)(&(n)->i_key.nk) -#define gval(n)(&(n)->i_val) -#define gnext(n)((n)->i_key.nk.next) -#define key2tval(n)(&(n)->i_key.tvk) -static TValue*luaH_setnum(lua_State*L,Table*t,int key); -static const TValue*luaH_getstr(Table*t,TString*key); -static TValue*luaH_set(lua_State*L,Table*t,const TValue*key); -static const char*const luaT_typenames[]={ -"nil","boolean","userdata","number", -"string","table","function","userdata","thread", -"proto","upval" -}; -static void luaT_init(lua_State*L){ -static const char*const luaT_eventname[]={ -"__index","__newindex", -"__gc","__mode","__eq", -"__add","__sub","__mul","__div","__mod", -"__pow","__unm","__len","__lt","__le", -"__concat","__call" -}; -int i; -for(i=0;itmname[i]=luaS_new(L,luaT_eventname[i]); -luaS_fix(G(L)->tmname[i]); -} -} -static const TValue*luaT_gettm(Table*events,TMS event,TString*ename){ -const TValue*tm=luaH_getstr(events,ename); -if(ttisnil(tm)){ -events->flags|=cast_byte(1u<metatable; -break; -case 7: -mt=uvalue(o)->metatable; -break; -default: -mt=G(L)->mt[ttype(o)]; -} -return(mt?luaH_getstr(mt,G(L)->tmname[event]):(&luaO_nilobject_)); -} -#define sizeCclosure(n)(cast(int,sizeof(CClosure))+cast(int,sizeof(TValue)*((n)-1))) -#define sizeLclosure(n)(cast(int,sizeof(LClosure))+cast(int,sizeof(TValue*)*((n)-1))) -static Closure*luaF_newCclosure(lua_State*L,int nelems,Table*e){ -Closure*c=cast(Closure*,luaM_malloc(L,sizeCclosure(nelems))); -luaC_link(L,obj2gco(c),6); -c->c.isC=1; -c->c.env=e; -c->c.nupvalues=cast_byte(nelems); -return c; -} -static Closure*luaF_newLclosure(lua_State*L,int nelems,Table*e){ -Closure*c=cast(Closure*,luaM_malloc(L,sizeLclosure(nelems))); -luaC_link(L,obj2gco(c),6); -c->l.isC=0; -c->l.env=e; -c->l.nupvalues=cast_byte(nelems); -while(nelems--)c->l.upvals[nelems]=NULL; -return c; -} -static UpVal*luaF_newupval(lua_State*L){ -UpVal*uv=luaM_new(L,UpVal); -luaC_link(L,obj2gco(uv),(8+2)); -uv->v=&uv->u.value; -setnilvalue(uv->v); -return uv; -} -static UpVal*luaF_findupval(lua_State*L,StkId level){ -global_State*g=G(L); -GCObject**pp=&L->openupval; -UpVal*p; -UpVal*uv; -while(*pp!=NULL&&(p=ngcotouv(*pp))->v>=level){ -if(p->v==level){ -if(isdead(g,obj2gco(p))) -changewhite(obj2gco(p)); -return p; -} -pp=&p->next; -} -uv=luaM_new(L,UpVal); -uv->tt=(8+2); -uv->marked=luaC_white(g); -uv->v=level; -uv->next=*pp; -*pp=obj2gco(uv); -uv->u.l.prev=&g->uvhead; -uv->u.l.next=g->uvhead.u.l.next; -uv->u.l.next->u.l.prev=uv; -g->uvhead.u.l.next=uv; -return uv; -} -static void unlinkupval(UpVal*uv){ -uv->u.l.next->u.l.prev=uv->u.l.prev; -uv->u.l.prev->u.l.next=uv->u.l.next; -} -static void luaF_freeupval(lua_State*L,UpVal*uv){ -if(uv->v!=&uv->u.value) -unlinkupval(uv); -luaM_free(L,uv); -} -static void luaF_close(lua_State*L,StkId level){ -UpVal*uv; -global_State*g=G(L); -while(L->openupval!=NULL&&(uv=ngcotouv(L->openupval))->v>=level){ -GCObject*o=obj2gco(uv); -L->openupval=uv->next; -if(isdead(g,o)) -luaF_freeupval(L,uv); -else{ -unlinkupval(uv); -setobj(L,&uv->u.value,uv->v); -uv->v=&uv->u.value; -luaC_linkupval(L,uv); -} -} -} -static Proto*luaF_newproto(lua_State*L){ -Proto*f=luaM_new(L,Proto); -luaC_link(L,obj2gco(f),(8+1)); -f->k=NULL; -f->sizek=0; -f->p=NULL; -f->sizep=0; -f->code=NULL; -f->sizecode=0; -f->sizelineinfo=0; -f->sizeupvalues=0; -f->nups=0; -f->upvalues=NULL; -f->numparams=0; -f->is_vararg=0; -f->maxstacksize=0; -f->lineinfo=NULL; -f->sizelocvars=0; -f->locvars=NULL; -f->linedefined=0; -f->lastlinedefined=0; -f->source=NULL; -return f; -} -static void luaF_freeproto(lua_State*L,Proto*f){ -luaM_freearray(L,f->code,f->sizecode,Instruction); -luaM_freearray(L,f->p,f->sizep,Proto*); -luaM_freearray(L,f->k,f->sizek,TValue); -luaM_freearray(L,f->lineinfo,f->sizelineinfo,int); -luaM_freearray(L,f->locvars,f->sizelocvars,struct LocVar); -luaM_freearray(L,f->upvalues,f->sizeupvalues,TString*); -luaM_free(L,f); -} -static void luaF_freeclosure(lua_State*L,Closure*c){ -int size=(c->c.isC)?sizeCclosure(c->c.nupvalues): -sizeLclosure(c->l.nupvalues); -luaM_freemem(L,c,size); -} -#define MASK1(n,p)((~((~(Instruction)0)<>0)&MASK1(6,0))) -#define SET_OPCODE(i,o)((i)=(((i)&MASK0(6,0))|((cast(Instruction,o)<<0)&MASK1(6,0)))) -#define GETARG_A(i)(cast(int,((i)>>(0+6))&MASK1(8,0))) -#define SETARG_A(i,u)((i)=(((i)&MASK0(8,(0+6)))|((cast(Instruction,u)<<(0+6))&MASK1(8,(0+6))))) -#define GETARG_B(i)(cast(int,((i)>>(((0+6)+8)+9))&MASK1(9,0))) -#define SETARG_B(i,b)((i)=(((i)&MASK0(9,(((0+6)+8)+9)))|((cast(Instruction,b)<<(((0+6)+8)+9))&MASK1(9,(((0+6)+8)+9))))) -#define GETARG_C(i)(cast(int,((i)>>((0+6)+8))&MASK1(9,0))) -#define SETARG_C(i,b)((i)=(((i)&MASK0(9,((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1(9,((0+6)+8))))) -#define GETARG_Bx(i)(cast(int,((i)>>((0+6)+8))&MASK1((9+9),0))) -#define SETARG_Bx(i,b)((i)=(((i)&MASK0((9+9),((0+6)+8)))|((cast(Instruction,b)<<((0+6)+8))&MASK1((9+9),((0+6)+8))))) -#define GETARG_sBx(i)(GETARG_Bx(i)-(((1<<(9+9))-1)>>1)) -#define SETARG_sBx(i,b)SETARG_Bx((i),cast(unsigned int,(b)+(((1<<(9+9))-1)>>1))) -#define CREATE_ABC(o,a,b,c)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,b)<<(((0+6)+8)+9))|(cast(Instruction,c)<<((0+6)+8))) -#define CREATE_ABx(o,a,bc)((cast(Instruction,o)<<0)|(cast(Instruction,a)<<(0+6))|(cast(Instruction,bc)<<((0+6)+8))) -#define ISK(x)((x)&(1<<(9-1))) -#define INDEXK(r)((int)(r)&~(1<<(9-1))) -#define RKASK(x)((x)|(1<<(9-1))) -static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)]; -#define getBMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>4)&3)) -#define getCMode(m)(cast(enum OpArgMask,(luaP_opmodes[m]>>2)&3)) -#define testTMode(m)(luaP_opmodes[m]&(1<<7)) -typedef struct expdesc{ -expkind k; -union{ -struct{int info,aux;}s; -lua_Number nval; -}u; -int t; -int f; -}expdesc; -typedef struct upvaldesc{ -lu_byte k; -lu_byte info; -}upvaldesc; -struct BlockCnt; -typedef struct FuncState{ -Proto*f; -Table*h; -struct FuncState*prev; -struct LexState*ls; -struct lua_State*L; -struct BlockCnt*bl; -int pc; -int lasttarget; -int jpc; -int freereg; -int nk; -int np; -short nlocvars; -lu_byte nactvar; -upvaldesc upvalues[60]; -unsigned short actvar[200]; -}FuncState; -static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff, -const char*name); -struct lua_longjmp{ -struct lua_longjmp*previous; -jmp_buf b; -volatile int status; -}; -static void luaD_seterrorobj(lua_State*L,int errcode,StkId oldtop){ -switch(errcode){ -case 4:{ -setsvalue(L,oldtop,luaS_newliteral(L,"not enough memory")); -break; -} -case 5:{ -setsvalue(L,oldtop,luaS_newliteral(L,"error in error handling")); -break; -} -case 3: -case 2:{ -setobj(L,oldtop,L->top-1); -break; -} -} -L->top=oldtop+1; -} -static void restore_stack_limit(lua_State*L){ -if(L->size_ci>20000){ -int inuse=cast_int(L->ci-L->base_ci); -if(inuse+1<20000) -luaD_reallocCI(L,20000); -} -} -static void resetstack(lua_State*L,int status){ -L->ci=L->base_ci; -L->base=L->ci->base; -luaF_close(L,L->base); -luaD_seterrorobj(L,status,L->base); -L->nCcalls=L->baseCcalls; -L->allowhook=1; -restore_stack_limit(L); -L->errfunc=0; -L->errorJmp=NULL; -} -static void luaD_throw(lua_State*L,int errcode){ -if(L->errorJmp){ -L->errorJmp->status=errcode; -LUAI_THROW(L,L->errorJmp); -} -else{ -L->status=cast_byte(errcode); -if(G(L)->panic){ -resetstack(L,errcode); -G(L)->panic(L); -} -exit(EXIT_FAILURE); -} -} -static int luaD_rawrunprotected(lua_State*L,Pfunc f,void*ud){ -struct lua_longjmp lj; -lj.status=0; -lj.previous=L->errorJmp; -L->errorJmp=&lj; -LUAI_TRY(L,&lj, -(*f)(L,ud); -); -L->errorJmp=lj.previous; -return lj.status; -} -static void correctstack(lua_State*L,TValue*oldstack){ -CallInfo*ci; -GCObject*up; -L->top=(L->top-oldstack)+L->stack; -for(up=L->openupval;up!=NULL;up=up->gch.next) -gco2uv(up)->v=(gco2uv(up)->v-oldstack)+L->stack; -for(ci=L->base_ci;ci<=L->ci;ci++){ -ci->top=(ci->top-oldstack)+L->stack; -ci->base=(ci->base-oldstack)+L->stack; -ci->func=(ci->func-oldstack)+L->stack; -} -L->base=(L->base-oldstack)+L->stack; -} -static void luaD_reallocstack(lua_State*L,int newsize){ -TValue*oldstack=L->stack; -int realsize=newsize+1+5; -luaM_reallocvector(L,L->stack,L->stacksize,realsize,TValue); -L->stacksize=realsize; -L->stack_last=L->stack+newsize; -correctstack(L,oldstack); -} -static void luaD_reallocCI(lua_State*L,int newsize){ -CallInfo*oldci=L->base_ci; -luaM_reallocvector(L,L->base_ci,L->size_ci,newsize,CallInfo); -L->size_ci=newsize; -L->ci=(L->ci-oldci)+L->base_ci; -L->end_ci=L->base_ci+L->size_ci-1; -} -static void luaD_growstack(lua_State*L,int n){ -if(n<=L->stacksize) -luaD_reallocstack(L,2*L->stacksize); -else -luaD_reallocstack(L,L->stacksize+n); -} -static CallInfo*growCI(lua_State*L){ -if(L->size_ci>20000) -luaD_throw(L,5); -else{ -luaD_reallocCI(L,2*L->size_ci); -if(L->size_ci>20000) -luaG_runerror(L,"stack overflow"); -} -return++L->ci; -} -static StkId adjust_varargs(lua_State*L,Proto*p,int actual){ -int i; -int nfixargs=p->numparams; -Table*htab=NULL; -StkId base,fixed; -for(;actualtop++); -fixed=L->top-actual; -base=L->top; -for(i=0;itop++,fixed+i); -setnilvalue(fixed+i); -} -if(htab){ -sethvalue(L,L->top++,htab); -} -return base; -} -static StkId tryfuncTM(lua_State*L,StkId func){ -const TValue*tm=luaT_gettmbyobj(L,func,TM_CALL); -StkId p; -ptrdiff_t funcr=savestack(L,func); -if(!ttisfunction(tm)) -luaG_typeerror(L,func,"call"); -for(p=L->top;p>func;p--)setobj(L,p,p-1); -incr_top(L); -func=restorestack(L,funcr); -setobj(L,func,tm); -return func; -} -#define inc_ci(L)((L->ci==L->end_ci)?growCI(L):(condhardstacktests(luaD_reallocCI(L,L->size_ci)),++L->ci)) -static int luaD_precall(lua_State*L,StkId func,int nresults){ -LClosure*cl; -ptrdiff_t funcr; -if(!ttisfunction(func)) -func=tryfuncTM(L,func); -funcr=savestack(L,func); -cl=&clvalue(func)->l; -L->ci->savedpc=L->savedpc; -if(!cl->isC){ -CallInfo*ci; -StkId st,base; -Proto*p=cl->p; -luaD_checkstack(L,p->maxstacksize+p->numparams); -func=restorestack(L,funcr); -if(!p->is_vararg){ -base=func+1; -if(L->top>base+p->numparams) -L->top=base+p->numparams; -} -else{ -int nargs=cast_int(L->top-func)-1; -base=adjust_varargs(L,p,nargs); -func=restorestack(L,funcr); -} -ci=inc_ci(L); -ci->func=func; -L->base=ci->base=base; -ci->top=L->base+p->maxstacksize; -L->savedpc=p->code; -ci->tailcalls=0; -ci->nresults=nresults; -for(st=L->top;sttop;st++) -setnilvalue(st); -L->top=ci->top; -return 0; -} -else{ -CallInfo*ci; -int n; -luaD_checkstack(L,20); -ci=inc_ci(L); -ci->func=restorestack(L,funcr); -L->base=ci->base=ci->func+1; -ci->top=L->top+20; -ci->nresults=nresults; -n=(*curr_func(L)->c.f)(L); -if(n<0) -return 2; -else{ -luaD_poscall(L,L->top-n); -return 1; -} -} -} -static int luaD_poscall(lua_State*L,StkId firstResult){ -StkId res; -int wanted,i; -CallInfo*ci; -ci=L->ci--; -res=ci->func; -wanted=ci->nresults; -L->base=(ci-1)->base; -L->savedpc=(ci-1)->savedpc; -for(i=wanted;i!=0&&firstResulttop;i--) -setobj(L,res++,firstResult++); -while(i-->0) -setnilvalue(res++); -L->top=res; -return(wanted-(-1)); -} -static void luaD_call(lua_State*L,StkId func,int nResults){ -if(++L->nCcalls>=200){ -if(L->nCcalls==200) -luaG_runerror(L,"C stack overflow"); -else if(L->nCcalls>=(200+(200>>3))) -luaD_throw(L,5); -} -if(luaD_precall(L,func,nResults)==0) -luaV_execute(L,1); -L->nCcalls--; -luaC_checkGC(L); -} -static int luaD_pcall(lua_State*L,Pfunc func,void*u, -ptrdiff_t old_top,ptrdiff_t ef){ -int status; -unsigned short oldnCcalls=L->nCcalls; -ptrdiff_t old_ci=saveci(L,L->ci); -lu_byte old_allowhooks=L->allowhook; -ptrdiff_t old_errfunc=L->errfunc; -L->errfunc=ef; -status=luaD_rawrunprotected(L,func,u); -if(status!=0){ -StkId oldtop=restorestack(L,old_top); -luaF_close(L,oldtop); -luaD_seterrorobj(L,status,oldtop); -L->nCcalls=oldnCcalls; -L->ci=restoreci(L,old_ci); -L->base=L->ci->base; -L->savedpc=L->ci->savedpc; -L->allowhook=old_allowhooks; -restore_stack_limit(L); -} -L->errfunc=old_errfunc; -return status; -} -struct SParser{ -ZIO*z; -Mbuffer buff; -const char*name; -}; -static void f_parser(lua_State*L,void*ud){ -int i; -Proto*tf; -Closure*cl; -struct SParser*p=cast(struct SParser*,ud); -luaC_checkGC(L); -tf=luaY_parser(L,p->z, -&p->buff,p->name); -cl=luaF_newLclosure(L,tf->nups,hvalue(gt(L))); -cl->l.p=tf; -for(i=0;inups;i++) -cl->l.upvals[i]=luaF_newupval(L); -setclvalue(L,L->top,cl); -incr_top(L); -} -static int luaD_protectedparser(lua_State*L,ZIO*z,const char*name){ -struct SParser p; -int status; -p.z=z;p.name=name; -luaZ_initbuffer(L,&p.buff); -status=luaD_pcall(L,f_parser,&p,savestack(L,L->top),L->errfunc); -luaZ_freebuffer(L,&p.buff); -return status; -} -static void luaS_resize(lua_State*L,int newsize){ -GCObject**newhash; -stringtable*tb; -int i; -if(G(L)->gcstate==2) -return; -newhash=luaM_newvector(L,newsize,GCObject*); -tb=&G(L)->strt; -for(i=0;isize;i++){ -GCObject*p=tb->hash[i]; -while(p){ -GCObject*next=p->gch.next; -unsigned int h=gco2ts(p)->hash; -int h1=lmod(h,newsize); -p->gch.next=newhash[h1]; -newhash[h1]=p; -p=next; -} -} -luaM_freearray(L,tb->hash,tb->size,TString*); -tb->size=newsize; -tb->hash=newhash; -} -static TString*newlstr(lua_State*L,const char*str,size_t l, -unsigned int h){ -TString*ts; -stringtable*tb; -if(l+1>(((size_t)(~(size_t)0)-2)-sizeof(TString))/sizeof(char)) -luaM_toobig(L); -ts=cast(TString*,luaM_malloc(L,(l+1)*sizeof(char)+sizeof(TString))); -ts->tsv.len=l; -ts->tsv.hash=h; -ts->tsv.marked=luaC_white(G(L)); -ts->tsv.tt=4; -ts->tsv.reserved=0; -memcpy(ts+1,str,l*sizeof(char)); -((char*)(ts+1))[l]='\0'; -tb=&G(L)->strt; -h=lmod(h,tb->size); -ts->tsv.next=tb->hash[h]; -tb->hash[h]=obj2gco(ts); -tb->nuse++; -if(tb->nuse>cast(lu_int32,tb->size)&&tb->size<=(INT_MAX-2)/2) -luaS_resize(L,tb->size*2); -return ts; -} -static TString*luaS_newlstr(lua_State*L,const char*str,size_t l){ -GCObject*o; -unsigned int h=cast(unsigned int,l); -size_t step=(l>>5)+1; -size_t l1; -for(l1=l;l1>=step;l1-=step) -h=h^((h<<5)+(h>>2)+cast(unsigned char,str[l1-1])); -for(o=G(L)->strt.hash[lmod(h,G(L)->strt.size)]; -o!=NULL; -o=o->gch.next){ -TString*ts=rawgco2ts(o); -if(ts->tsv.len==l&&(memcmp(str,getstr(ts),l)==0)){ -if(isdead(G(L),o))changewhite(o); -return ts; -} -} -return newlstr(L,str,l,h); -} -static Udata*luaS_newudata(lua_State*L,size_t s,Table*e){ -Udata*u; -if(s>((size_t)(~(size_t)0)-2)-sizeof(Udata)) -luaM_toobig(L); -u=cast(Udata*,luaM_malloc(L,s+sizeof(Udata))); -u->uv.marked=luaC_white(G(L)); -u->uv.tt=7; -u->uv.len=s; -u->uv.metatable=NULL; -u->uv.env=e; -u->uv.next=G(L)->mainthread->next; -G(L)->mainthread->next=obj2gco(u); -return u; -} -#define hashpow2(t,n)(gnode(t,lmod((n),sizenode(t)))) -#define hashstr(t,str)hashpow2(t,(str)->tsv.hash) -#define hashboolean(t,p)hashpow2(t,p) -#define hashmod(t,n)(gnode(t,((n)%((sizenode(t)-1)|1)))) -#define hashpointer(t,p)hashmod(t,IntPoint(p)) -static const Node dummynode_={ -{{NULL},0}, -{{{NULL},0,NULL}} -}; -static Node*hashnum(const Table*t,lua_Number n){ -unsigned int a[cast_int(sizeof(lua_Number)/sizeof(int))]; -int i; -if(luai_numeq(n,0)) -return gnode(t,0); -memcpy(a,&n,sizeof(a)); -for(i=1;isizearray) -return i-1; -else{ -Node*n=mainposition(t,key); -do{ -if(luaO_rawequalObj(key2tval(n),key)|| -(ttype(gkey(n))==(8+3)&&iscollectable(key)&& -gcvalue(gkey(n))==gcvalue(key))){ -i=cast_int(n-gnode(t,0)); -return i+t->sizearray; -} -else n=gnext(n); -}while(n); -luaG_runerror(L,"invalid key to "LUA_QL("next")); -return 0; -} -} -static int luaH_next(lua_State*L,Table*t,StkId key){ -int i=findindex(L,t,key); -for(i++;isizearray;i++){ -if(!ttisnil(&t->array[i])){ -setnvalue(key,cast_num(i+1)); -setobj(L,key+1,&t->array[i]); -return 1; -} -} -for(i-=t->sizearray;i<(int)sizenode(t);i++){ -if(!ttisnil(gval(gnode(t,i)))){ -setobj(L,key,key2tval(gnode(t,i))); -setobj(L,key+1,gval(gnode(t,i))); -return 1; -} -} -return 0; -} -static int computesizes(int nums[],int*narray){ -int i; -int twotoi; -int a=0; -int na=0; -int n=0; -for(i=0,twotoi=1;twotoi/2<*narray;i++,twotoi*=2){ -if(nums[i]>0){ -a+=nums[i]; -if(a>twotoi/2){ -n=twotoi; -na=a; -} -} -if(a==*narray)break; -} -*narray=n; -return na; -} -static int countint(const TValue*key,int*nums){ -int k=arrayindex(key); -if(0t->sizearray){ -lim=t->sizearray; -if(i>lim) -break; -} -for(;i<=lim;i++){ -if(!ttisnil(&t->array[i-1])) -lc++; -} -nums[lg]+=lc; -ause+=lc; -} -return ause; -} -static int numusehash(const Table*t,int*nums,int*pnasize){ -int totaluse=0; -int ause=0; -int i=sizenode(t); -while(i--){ -Node*n=&t->node[i]; -if(!ttisnil(gval(n))){ -ause+=countint(key2tval(n),nums); -totaluse++; -} -} -*pnasize+=ause; -return totaluse; -} -static void setarrayvector(lua_State*L,Table*t,int size){ -int i; -luaM_reallocvector(L,t->array,t->sizearray,size,TValue); -for(i=t->sizearray;iarray[i]); -t->sizearray=size; -} -static void setnodevector(lua_State*L,Table*t,int size){ -int lsize; -if(size==0){ -t->node=cast(Node*,(&dummynode_)); -lsize=0; -} -else{ -int i; -lsize=ceillog2(size); -if(lsize>(32-2)) -luaG_runerror(L,"table overflow"); -size=twoto(lsize); -t->node=luaM_newvector(L,size,Node); -for(i=0;ilsizenode=cast_byte(lsize); -t->lastfree=gnode(t,size); -} -static void resize(lua_State*L,Table*t,int nasize,int nhsize){ -int i; -int oldasize=t->sizearray; -int oldhsize=t->lsizenode; -Node*nold=t->node; -if(nasize>oldasize) -setarrayvector(L,t,nasize); -setnodevector(L,t,nhsize); -if(nasizesizearray=nasize; -for(i=nasize;iarray[i])) -setobj(L,luaH_setnum(L,t,i+1),&t->array[i]); -} -luaM_reallocvector(L,t->array,oldasize,nasize,TValue); -} -for(i=twoto(oldhsize)-1;i>=0;i--){ -Node*old=nold+i; -if(!ttisnil(gval(old))) -setobj(L,luaH_set(L,t,key2tval(old)),gval(old)); -} -if(nold!=(&dummynode_)) -luaM_freearray(L,nold,twoto(oldhsize),Node); -} -static void luaH_resizearray(lua_State*L,Table*t,int nasize){ -int nsize=(t->node==(&dummynode_))?0:sizenode(t); -resize(L,t,nasize,nsize); -} -static void rehash(lua_State*L,Table*t,const TValue*ek){ -int nasize,na; -int nums[(32-2)+1]; -int i; -int totaluse; -for(i=0;i<=(32-2);i++)nums[i]=0; -nasize=numusearray(t,nums); -totaluse=nasize; -totaluse+=numusehash(t,nums,&nasize); -nasize+=countint(ek,nums); -totaluse++; -na=computesizes(nums,&nasize); -resize(L,t,nasize,totaluse-na); -} -static Table*luaH_new(lua_State*L,int narray,int nhash){ -Table*t=luaM_new(L,Table); -luaC_link(L,obj2gco(t),5); -t->metatable=NULL; -t->flags=cast_byte(~0); -t->array=NULL; -t->sizearray=0; -t->lsizenode=0; -t->node=cast(Node*,(&dummynode_)); -setarrayvector(L,t,narray); -setnodevector(L,t,nhash); -return t; -} -static void luaH_free(lua_State*L,Table*t){ -if(t->node!=(&dummynode_)) -luaM_freearray(L,t->node,sizenode(t),Node); -luaM_freearray(L,t->array,t->sizearray,TValue); -luaM_free(L,t); -} -static Node*getfreepos(Table*t){ -while(t->lastfree-->t->node){ -if(ttisnil(gkey(t->lastfree))) -return t->lastfree; -} -return NULL; -} -static TValue*newkey(lua_State*L,Table*t,const TValue*key){ -Node*mp=mainposition(t,key); -if(!ttisnil(gval(mp))||mp==(&dummynode_)){ -Node*othern; -Node*n=getfreepos(t); -if(n==NULL){ -rehash(L,t,key); -return luaH_set(L,t,key); -} -othern=mainposition(t,key2tval(mp)); -if(othern!=mp){ -while(gnext(othern)!=mp)othern=gnext(othern); -gnext(othern)=n; -*n=*mp; -gnext(mp)=NULL; -setnilvalue(gval(mp)); -} -else{ -gnext(n)=gnext(mp); -gnext(mp)=n; -mp=n; -} -} -gkey(mp)->value=key->value;gkey(mp)->tt=key->tt; -luaC_barriert(L,t,key); -return gval(mp); -} -static const TValue*luaH_getnum(Table*t,int key){ -if(cast(unsigned int,key)-1sizearray)) -return&t->array[key-1]; -else{ -lua_Number nk=cast_num(key); -Node*n=hashnum(t,nk); -do{ -if(ttisnumber(gkey(n))&&luai_numeq(nvalue(gkey(n)),nk)) -return gval(n); -else n=gnext(n); -}while(n); -return(&luaO_nilobject_); -} -} -static const TValue*luaH_getstr(Table*t,TString*key){ -Node*n=hashstr(t,key); -do{ -if(ttisstring(gkey(n))&&rawtsvalue(gkey(n))==key) -return gval(n); -else n=gnext(n); -}while(n); -return(&luaO_nilobject_); -} -static const TValue*luaH_get(Table*t,const TValue*key){ -switch(ttype(key)){ -case 0:return(&luaO_nilobject_); -case 4:return luaH_getstr(t,rawtsvalue(key)); -case 3:{ -int k; -lua_Number n=nvalue(key); -lua_number2int(k,n); -if(luai_numeq(cast_num(k),nvalue(key))) -return luaH_getnum(t,k); -} -/*fallthrough*/ -default:{ -Node*n=mainposition(t,key); -do{ -if(luaO_rawequalObj(key2tval(n),key)) -return gval(n); -else n=gnext(n); -}while(n); -return(&luaO_nilobject_); -} -} -} -static TValue*luaH_set(lua_State*L,Table*t,const TValue*key){ -const TValue*p=luaH_get(t,key); -t->flags=0; -if(p!=(&luaO_nilobject_)) -return cast(TValue*,p); -else{ -if(ttisnil(key))luaG_runerror(L,"table index is nil"); -else if(ttisnumber(key)&&luai_numisnan(nvalue(key))) -luaG_runerror(L,"table index is NaN"); -return newkey(L,t,key); -} -} -static TValue*luaH_setnum(lua_State*L,Table*t,int key){ -const TValue*p=luaH_getnum(t,key); -if(p!=(&luaO_nilobject_)) -return cast(TValue*,p); -else{ -TValue k; -setnvalue(&k,cast_num(key)); -return newkey(L,t,&k); -} -} -static TValue*luaH_setstr(lua_State*L,Table*t,TString*key){ -const TValue*p=luaH_getstr(t,key); -if(p!=(&luaO_nilobject_)) -return cast(TValue*,p); -else{ -TValue k; -setsvalue(L,&k,key); -return newkey(L,t,&k); -} -} -static int unbound_search(Table*t,unsigned int j){ -unsigned int i=j; -j++; -while(!ttisnil(luaH_getnum(t,j))){ -i=j; -j*=2; -if(j>cast(unsigned int,(INT_MAX-2))){ -i=1; -while(!ttisnil(luaH_getnum(t,i)))i++; -return i-1; -} -} -while(j-i>1){ -unsigned int m=(i+j)/2; -if(ttisnil(luaH_getnum(t,m)))j=m; -else i=m; -} -return i; -} -static int luaH_getn(Table*t){ -unsigned int j=t->sizearray; -if(j>0&&ttisnil(&t->array[j-1])){ -unsigned int i=0; -while(j-i>1){ -unsigned int m=(i+j)/2; -if(ttisnil(&t->array[m-1]))j=m; -else i=m; -} -return i; -} -else if(t->node==(&dummynode_)) -return j; -else return unbound_search(t,j); -} -#define makewhite(g,x)((x)->gch.marked=cast_byte(((x)->gch.marked&cast_byte(~(bitmask(2)|bit2mask(0,1))))|luaC_white(g))) -#define white2gray(x)reset2bits((x)->gch.marked,0,1) -#define black2gray(x)resetbit((x)->gch.marked,2) -#define stringmark(s)reset2bits((s)->tsv.marked,0,1) -#define isfinalized(u)testbit((u)->marked,3) -#define markfinalized(u)l_setbit((u)->marked,3) -#define markvalue(g,o){checkconsistency(o);if(iscollectable(o)&&iswhite(gcvalue(o)))reallymarkobject(g,gcvalue(o));} -#define markobject(g,t){if(iswhite(obj2gco(t)))reallymarkobject(g,obj2gco(t));} -#define setthreshold(g)(g->GCthreshold=(g->estimate/100)*g->gcpause) -static void removeentry(Node*n){ -if(iscollectable(gkey(n))) -setttype(gkey(n),(8+3)); -} -static void reallymarkobject(global_State*g,GCObject*o){ -white2gray(o); -switch(o->gch.tt){ -case 4:{ -return; -} -case 7:{ -Table*mt=gco2u(o)->metatable; -gray2black(o); -if(mt)markobject(g,mt); -markobject(g,gco2u(o)->env); -return; -} -case(8+2):{ -UpVal*uv=gco2uv(o); -markvalue(g,uv->v); -if(uv->v==&uv->u.value) -gray2black(o); -return; -} -case 6:{ -gco2cl(o)->c.gclist=g->gray; -g->gray=o; -break; -} -case 5:{ -gco2h(o)->gclist=g->gray; -g->gray=o; -break; -} -case 8:{ -gco2th(o)->gclist=g->gray; -g->gray=o; -break; -} -case(8+1):{ -gco2p(o)->gclist=g->gray; -g->gray=o; -break; -} -default:; -} -} -static void marktmu(global_State*g){ -GCObject*u=g->tmudata; -if(u){ -do{ -u=u->gch.next; -makewhite(g,u); -reallymarkobject(g,u); -}while(u!=g->tmudata); -} -} -static size_t luaC_separateudata(lua_State*L,int all){ -global_State*g=G(L); -size_t deadmem=0; -GCObject**p=&g->mainthread->next; -GCObject*curr; -while((curr=*p)!=NULL){ -if(!(iswhite(curr)||all)||isfinalized(gco2u(curr))) -p=&curr->gch.next; -else if(fasttm(L,gco2u(curr)->metatable,TM_GC)==NULL){ -markfinalized(gco2u(curr)); -p=&curr->gch.next; -} -else{ -deadmem+=sizeudata(gco2u(curr)); -markfinalized(gco2u(curr)); -*p=curr->gch.next; -if(g->tmudata==NULL) -g->tmudata=curr->gch.next=curr; -else{ -curr->gch.next=g->tmudata->gch.next; -g->tmudata->gch.next=curr; -g->tmudata=curr; -} -} -} -return deadmem; -} -static int traversetable(global_State*g,Table*h){ -int i; -int weakkey=0; -int weakvalue=0; -const TValue*mode; -if(h->metatable) -markobject(g,h->metatable); -mode=gfasttm(g,h->metatable,TM_MODE); -if(mode&&ttisstring(mode)){ -weakkey=(strchr(svalue(mode),'k')!=NULL); -weakvalue=(strchr(svalue(mode),'v')!=NULL); -if(weakkey||weakvalue){ -h->marked&=~(bitmask(3)|bitmask(4)); -h->marked|=cast_byte((weakkey<<3)| -(weakvalue<<4)); -h->gclist=g->weak; -g->weak=obj2gco(h); -} -} -if(weakkey&&weakvalue)return 1; -if(!weakvalue){ -i=h->sizearray; -while(i--) -markvalue(g,&h->array[i]); -} -i=sizenode(h); -while(i--){ -Node*n=gnode(h,i); -if(ttisnil(gval(n))) -removeentry(n); -else{ -if(!weakkey)markvalue(g,gkey(n)); -if(!weakvalue)markvalue(g,gval(n)); -} -} -return weakkey||weakvalue; -} -static void traverseproto(global_State*g,Proto*f){ -int i; -if(f->source)stringmark(f->source); -for(i=0;isizek;i++) -markvalue(g,&f->k[i]); -for(i=0;isizeupvalues;i++){ -if(f->upvalues[i]) -stringmark(f->upvalues[i]); -} -for(i=0;isizep;i++){ -if(f->p[i]) -markobject(g,f->p[i]); -} -for(i=0;isizelocvars;i++){ -if(f->locvars[i].varname) -stringmark(f->locvars[i].varname); -} -} -static void traverseclosure(global_State*g,Closure*cl){ -markobject(g,cl->c.env); -if(cl->c.isC){ -int i; -for(i=0;ic.nupvalues;i++) -markvalue(g,&cl->c.upvalue[i]); -} -else{ -int i; -markobject(g,cl->l.p); -for(i=0;il.nupvalues;i++) -markobject(g,cl->l.upvals[i]); -} -} -static void checkstacksizes(lua_State*L,StkId max){ -int ci_used=cast_int(L->ci-L->base_ci); -int s_used=cast_int(max-L->stack); -if(L->size_ci>20000) -return; -if(4*ci_usedsize_ci&&2*8size_ci) -luaD_reallocCI(L,L->size_ci/2); -condhardstacktests(luaD_reallocCI(L,ci_used+1)); -if(4*s_usedstacksize&& -2*((2*20)+5)stacksize) -luaD_reallocstack(L,L->stacksize/2); -condhardstacktests(luaD_reallocstack(L,s_used)); -} -static void traversestack(global_State*g,lua_State*l){ -StkId o,lim; -CallInfo*ci; -markvalue(g,gt(l)); -lim=l->top; -for(ci=l->base_ci;ci<=l->ci;ci++){ -if(limtop)lim=ci->top; -} -for(o=l->stack;otop;o++) -markvalue(g,o); -for(;o<=lim;o++) -setnilvalue(o); -checkstacksizes(l,lim); -} -static l_mem propagatemark(global_State*g){ -GCObject*o=g->gray; -gray2black(o); -switch(o->gch.tt){ -case 5:{ -Table*h=gco2h(o); -g->gray=h->gclist; -if(traversetable(g,h)) -black2gray(o); -return sizeof(Table)+sizeof(TValue)*h->sizearray+ -sizeof(Node)*sizenode(h); -} -case 6:{ -Closure*cl=gco2cl(o); -g->gray=cl->c.gclist; -traverseclosure(g,cl); -return(cl->c.isC)?sizeCclosure(cl->c.nupvalues): -sizeLclosure(cl->l.nupvalues); -} -case 8:{ -lua_State*th=gco2th(o); -g->gray=th->gclist; -th->gclist=g->grayagain; -g->grayagain=o; -black2gray(o); -traversestack(g,th); -return sizeof(lua_State)+sizeof(TValue)*th->stacksize+ -sizeof(CallInfo)*th->size_ci; -} -case(8+1):{ -Proto*p=gco2p(o); -g->gray=p->gclist; -traverseproto(g,p); -return sizeof(Proto)+sizeof(Instruction)*p->sizecode+ -sizeof(Proto*)*p->sizep+ -sizeof(TValue)*p->sizek+ -sizeof(int)*p->sizelineinfo+ -sizeof(LocVar)*p->sizelocvars+ -sizeof(TString*)*p->sizeupvalues; -} -default:return 0; -} -} -static size_t propagateall(global_State*g){ -size_t m=0; -while(g->gray)m+=propagatemark(g); -return m; -} -static int iscleared(const TValue*o,int iskey){ -if(!iscollectable(o))return 0; -if(ttisstring(o)){ -stringmark(rawtsvalue(o)); -return 0; -} -return iswhite(gcvalue(o))|| -(ttisuserdata(o)&&(!iskey&&isfinalized(uvalue(o)))); -} -static void cleartable(GCObject*l){ -while(l){ -Table*h=gco2h(l); -int i=h->sizearray; -if(testbit(h->marked,4)){ -while(i--){ -TValue*o=&h->array[i]; -if(iscleared(o,0)) -setnilvalue(o); -} -} -i=sizenode(h); -while(i--){ -Node*n=gnode(h,i); -if(!ttisnil(gval(n))&& -(iscleared(key2tval(n),1)||iscleared(gval(n),0))){ -setnilvalue(gval(n)); -removeentry(n); -} -} -l=h->gclist; -} -} -static void freeobj(lua_State*L,GCObject*o){ -switch(o->gch.tt){ -case(8+1):luaF_freeproto(L,gco2p(o));break; -case 6:luaF_freeclosure(L,gco2cl(o));break; -case(8+2):luaF_freeupval(L,gco2uv(o));break; -case 5:luaH_free(L,gco2h(o));break; -case 8:{ -luaE_freethread(L,gco2th(o)); -break; -} -case 4:{ -G(L)->strt.nuse--; -luaM_freemem(L,o,sizestring(gco2ts(o))); -break; -} -case 7:{ -luaM_freemem(L,o,sizeudata(gco2u(o))); -break; -} -default:; -} -} -#define sweepwholelist(L,p)sweeplist(L,p,((lu_mem)(~(lu_mem)0)-2)) -static GCObject**sweeplist(lua_State*L,GCObject**p,lu_mem count){ -GCObject*curr; -global_State*g=G(L); -int deadmask=otherwhite(g); -while((curr=*p)!=NULL&&count-->0){ -if(curr->gch.tt==8) -sweepwholelist(L,&gco2th(curr)->openupval); -if((curr->gch.marked^bit2mask(0,1))&deadmask){ -makewhite(g,curr); -p=&curr->gch.next; -} -else{ -*p=curr->gch.next; -if(curr==g->rootgc) -g->rootgc=curr->gch.next; -freeobj(L,curr); -} -} -return p; -} -static void checkSizes(lua_State*L){ -global_State*g=G(L); -if(g->strt.nusestrt.size/4)&& -g->strt.size>32*2) -luaS_resize(L,g->strt.size/2); -if(luaZ_sizebuffer(&g->buff)>32*2){ -size_t newsize=luaZ_sizebuffer(&g->buff)/2; -luaZ_resizebuffer(L,&g->buff,newsize); -} -} -static void GCTM(lua_State*L){ -global_State*g=G(L); -GCObject*o=g->tmudata->gch.next; -Udata*udata=rawgco2u(o); -const TValue*tm; -if(o==g->tmudata) -g->tmudata=NULL; -else -g->tmudata->gch.next=udata->uv.next; -udata->uv.next=g->mainthread->next; -g->mainthread->next=o; -makewhite(g,o); -tm=fasttm(L,udata->uv.metatable,TM_GC); -if(tm!=NULL){ -lu_byte oldah=L->allowhook; -lu_mem oldt=g->GCthreshold; -L->allowhook=0; -g->GCthreshold=2*g->totalbytes; -setobj(L,L->top,tm); -setuvalue(L,L->top+1,udata); -L->top+=2; -luaD_call(L,L->top-2,0); -L->allowhook=oldah; -g->GCthreshold=oldt; -} -} -static void luaC_callGCTM(lua_State*L){ -while(G(L)->tmudata) -GCTM(L); -} -static void luaC_freeall(lua_State*L){ -global_State*g=G(L); -int i; -g->currentwhite=bit2mask(0,1)|bitmask(6); -sweepwholelist(L,&g->rootgc); -for(i=0;istrt.size;i++) -sweepwholelist(L,&g->strt.hash[i]); -} -static void markmt(global_State*g){ -int i; -for(i=0;i<(8+1);i++) -if(g->mt[i])markobject(g,g->mt[i]); -} -static void markroot(lua_State*L){ -global_State*g=G(L); -g->gray=NULL; -g->grayagain=NULL; -g->weak=NULL; -markobject(g,g->mainthread); -markvalue(g,gt(g->mainthread)); -markvalue(g,registry(L)); -markmt(g); -g->gcstate=1; -} -static void remarkupvals(global_State*g){ -UpVal*uv; -for(uv=g->uvhead.u.l.next;uv!=&g->uvhead;uv=uv->u.l.next){ -if(isgray(obj2gco(uv))) -markvalue(g,uv->v); -} -} -static void atomic(lua_State*L){ -global_State*g=G(L); -size_t udsize; -remarkupvals(g); -propagateall(g); -g->gray=g->weak; -g->weak=NULL; -markobject(g,L); -markmt(g); -propagateall(g); -g->gray=g->grayagain; -g->grayagain=NULL; -propagateall(g); -udsize=luaC_separateudata(L,0); -marktmu(g); -udsize+=propagateall(g); -cleartable(g->weak); -g->currentwhite=cast_byte(otherwhite(g)); -g->sweepstrgc=0; -g->sweepgc=&g->rootgc; -g->gcstate=2; -g->estimate=g->totalbytes-udsize; -} -static l_mem singlestep(lua_State*L){ -global_State*g=G(L); -switch(g->gcstate){ -case 0:{ -markroot(L); -return 0; -} -case 1:{ -if(g->gray) -return propagatemark(g); -else{ -atomic(L); -return 0; -} -} -case 2:{ -lu_mem old=g->totalbytes; -sweepwholelist(L,&g->strt.hash[g->sweepstrgc++]); -if(g->sweepstrgc>=g->strt.size) -g->gcstate=3; -g->estimate-=old-g->totalbytes; -return 10; -} -case 3:{ -lu_mem old=g->totalbytes; -g->sweepgc=sweeplist(L,g->sweepgc,40); -if(*g->sweepgc==NULL){ -checkSizes(L); -g->gcstate=4; -} -g->estimate-=old-g->totalbytes; -return 40*10; -} -case 4:{ -if(g->tmudata){ -GCTM(L); -if(g->estimate>100) -g->estimate-=100; -return 100; -} -else{ -g->gcstate=0; -g->gcdept=0; -return 0; -} -} -default:return 0; -} -} -static void luaC_step(lua_State*L){ -global_State*g=G(L); -l_mem lim=(1024u/100)*g->gcstepmul; -if(lim==0) -lim=(((lu_mem)(~(lu_mem)0)-2)-1)/2; -g->gcdept+=g->totalbytes-g->GCthreshold; -do{ -lim-=singlestep(L); -if(g->gcstate==0) -break; -}while(lim>0); -if(g->gcstate!=0){ -if(g->gcdept<1024u) -g->GCthreshold=g->totalbytes+1024u; -else{ -g->gcdept-=1024u; -g->GCthreshold=g->totalbytes; -} -} -else{ -setthreshold(g); -} -} -static void luaC_barrierf(lua_State*L,GCObject*o,GCObject*v){ -global_State*g=G(L); -if(g->gcstate==1) -reallymarkobject(g,v); -else -makewhite(g,o); -} -static void luaC_barrierback(lua_State*L,Table*t){ -global_State*g=G(L); -GCObject*o=obj2gco(t); -black2gray(o); -t->gclist=g->grayagain; -g->grayagain=o; -} -static void luaC_link(lua_State*L,GCObject*o,lu_byte tt){ -global_State*g=G(L); -o->gch.next=g->rootgc; -g->rootgc=o; -o->gch.marked=luaC_white(g); -o->gch.tt=tt; -} -static void luaC_linkupval(lua_State*L,UpVal*uv){ -global_State*g=G(L); -GCObject*o=obj2gco(uv); -o->gch.next=g->rootgc; -g->rootgc=o; -if(isgray(o)){ -if(g->gcstate==1){ -gray2black(o); -luaC_barrier(L,uv,uv->v); -} -else{ -makewhite(g,o); -} -} -} -typedef union{ -lua_Number r; -TString*ts; -}SemInfo; -typedef struct Token{ -int token; -SemInfo seminfo; -}Token; -typedef struct LexState{ -int current; -int linenumber; -int lastline; -Token t; -Token lookahead; -struct FuncState*fs; -struct lua_State*L; -ZIO*z; -Mbuffer*buff; -TString*source; -char decpoint; -}LexState; -static void luaX_init(lua_State*L); -static void luaX_lexerror(LexState*ls,const char*msg,int token); -#define state_size(x)(sizeof(x)+0) -#define fromstate(l)(cast(lu_byte*,(l))-0) -#define tostate(l)(cast(lua_State*,cast(lu_byte*,l)+0)) -typedef struct LG{ -lua_State l; -global_State g; -}LG; -static void stack_init(lua_State*L1,lua_State*L){ -L1->base_ci=luaM_newvector(L,8,CallInfo); -L1->ci=L1->base_ci; -L1->size_ci=8; -L1->end_ci=L1->base_ci+L1->size_ci-1; -L1->stack=luaM_newvector(L,(2*20)+5,TValue); -L1->stacksize=(2*20)+5; -L1->top=L1->stack; -L1->stack_last=L1->stack+(L1->stacksize-5)-1; -L1->ci->func=L1->top; -setnilvalue(L1->top++); -L1->base=L1->ci->base=L1->top; -L1->ci->top=L1->top+20; -} -static void freestack(lua_State*L,lua_State*L1){ -luaM_freearray(L,L1->base_ci,L1->size_ci,CallInfo); -luaM_freearray(L,L1->stack,L1->stacksize,TValue); -} -static void f_luaopen(lua_State*L,void*ud){ -global_State*g=G(L); -UNUSED(ud); -stack_init(L,L); -sethvalue(L,gt(L),luaH_new(L,0,2)); -sethvalue(L,registry(L),luaH_new(L,0,2)); -luaS_resize(L,32); -luaT_init(L); -luaX_init(L); -luaS_fix(luaS_newliteral(L,"not enough memory")); -g->GCthreshold=4*g->totalbytes; -} -static void preinit_state(lua_State*L,global_State*g){ -G(L)=g; -L->stack=NULL; -L->stacksize=0; -L->errorJmp=NULL; -L->hook=NULL; -L->hookmask=0; -L->basehookcount=0; -L->allowhook=1; -resethookcount(L); -L->openupval=NULL; -L->size_ci=0; -L->nCcalls=L->baseCcalls=0; -L->status=0; -L->base_ci=L->ci=NULL; -L->savedpc=NULL; -L->errfunc=0; -setnilvalue(gt(L)); -} -static void close_state(lua_State*L){ -global_State*g=G(L); -luaF_close(L,L->stack); -luaC_freeall(L); -luaM_freearray(L,G(L)->strt.hash,G(L)->strt.size,TString*); -luaZ_freebuffer(L,&g->buff); -freestack(L,L); -(*g->frealloc)(g->ud,fromstate(L),state_size(LG),0); -} -static void luaE_freethread(lua_State*L,lua_State*L1){ -luaF_close(L1,L1->stack); -freestack(L,L1); -luaM_freemem(L,fromstate(L1),state_size(lua_State)); -} -static lua_State*lua_newstate(lua_Alloc f,void*ud){ -int i; -lua_State*L; -global_State*g; -void*l=(*f)(ud,NULL,0,state_size(LG)); -if(l==NULL)return NULL; -L=tostate(l); -g=&((LG*)L)->g; -L->next=NULL; -L->tt=8; -g->currentwhite=bit2mask(0,5); -L->marked=luaC_white(g); -set2bits(L->marked,5,6); -preinit_state(L,g); -g->frealloc=f; -g->ud=ud; -g->mainthread=L; -g->uvhead.u.l.prev=&g->uvhead; -g->uvhead.u.l.next=&g->uvhead; -g->GCthreshold=0; -g->strt.size=0; -g->strt.nuse=0; -g->strt.hash=NULL; -setnilvalue(registry(L)); -luaZ_initbuffer(L,&g->buff); -g->panic=NULL; -g->gcstate=0; -g->rootgc=obj2gco(L); -g->sweepstrgc=0; -g->sweepgc=&g->rootgc; -g->gray=NULL; -g->grayagain=NULL; -g->weak=NULL; -g->tmudata=NULL; -g->totalbytes=sizeof(LG); -g->gcpause=200; -g->gcstepmul=200; -g->gcdept=0; -for(i=0;i<(8+1);i++)g->mt[i]=NULL; -if(luaD_rawrunprotected(L,f_luaopen,NULL)!=0){ -close_state(L); -L=NULL; -} -else -{} -return L; -} -static void callallgcTM(lua_State*L,void*ud){ -UNUSED(ud); -luaC_callGCTM(L); -} -static void lua_close(lua_State*L){ -L=G(L)->mainthread; -luaF_close(L,L->stack); -luaC_separateudata(L,1); -L->errfunc=0; -do{ -L->ci=L->base_ci; -L->base=L->top=L->ci->base; -L->nCcalls=L->baseCcalls=0; -}while(luaD_rawrunprotected(L,callallgcTM,NULL)!=0); -close_state(L); -} -#define getcode(fs,e)((fs)->f->code[(e)->u.s.info]) -#define luaK_codeAsBx(fs,o,A,sBx)luaK_codeABx(fs,o,A,(sBx)+(((1<<(9+9))-1)>>1)) -#define luaK_setmultret(fs,e)luaK_setreturns(fs,e,(-1)) -static int luaK_codeABx(FuncState*fs,OpCode o,int A,unsigned int Bx); -static int luaK_codeABC(FuncState*fs,OpCode o,int A,int B,int C); -static void luaK_setreturns(FuncState*fs,expdesc*e,int nresults); -static void luaK_patchtohere(FuncState*fs,int list); -static void luaK_concat(FuncState*fs,int*l1,int l2); -static int currentpc(lua_State*L,CallInfo*ci){ -if(!isLua(ci))return-1; -if(ci==L->ci) -ci->savedpc=L->savedpc; -return pcRel(ci->savedpc,ci_func(ci)->l.p); -} -static int currentline(lua_State*L,CallInfo*ci){ -int pc=currentpc(L,ci); -if(pc<0) -return-1; -else -return getline_(ci_func(ci)->l.p,pc); -} -static int lua_getstack(lua_State*L,int level,lua_Debug*ar){ -int status; -CallInfo*ci; -for(ci=L->ci;level>0&&ci>L->base_ci;ci--){ -level--; -if(f_isLua(ci)) -level-=ci->tailcalls; -} -if(level==0&&ci>L->base_ci){ -status=1; -ar->i_ci=cast_int(ci-L->base_ci); -} -else if(level<0){ -status=1; -ar->i_ci=0; -} -else status=0; -return status; -} -static Proto*getluaproto(CallInfo*ci){ -return(isLua(ci)?ci_func(ci)->l.p:NULL); -} -static void funcinfo(lua_Debug*ar,Closure*cl){ -if(cl->c.isC){ -ar->source="=[C]"; -ar->linedefined=-1; -ar->lastlinedefined=-1; -ar->what="C"; -} -else{ -ar->source=getstr(cl->l.p->source); -ar->linedefined=cl->l.p->linedefined; -ar->lastlinedefined=cl->l.p->lastlinedefined; -ar->what=(ar->linedefined==0)?"main":"Lua"; -} -luaO_chunkid(ar->short_src,ar->source,60); -} -static void info_tailcall(lua_Debug*ar){ -ar->name=ar->namewhat=""; -ar->what="tail"; -ar->lastlinedefined=ar->linedefined=ar->currentline=-1; -ar->source="=(tail call)"; -luaO_chunkid(ar->short_src,ar->source,60); -ar->nups=0; -} -static void collectvalidlines(lua_State*L,Closure*f){ -if(f==NULL||f->c.isC){ -setnilvalue(L->top); -} -else{ -Table*t=luaH_new(L,0,0); -int*lineinfo=f->l.p->lineinfo; -int i; -for(i=0;il.p->sizelineinfo;i++) -setbvalue(luaH_setnum(L,t,lineinfo[i]),1); -sethvalue(L,L->top,t); -} -incr_top(L); -} -static int auxgetinfo(lua_State*L,const char*what,lua_Debug*ar, -Closure*f,CallInfo*ci){ -int status=1; -if(f==NULL){ -info_tailcall(ar); -return status; -} -for(;*what;what++){ -switch(*what){ -case'S':{ -funcinfo(ar,f); -break; -} -case'l':{ -ar->currentline=(ci)?currentline(L,ci):-1; -break; -} -case'u':{ -ar->nups=f->c.nupvalues; -break; -} -case'n':{ -ar->namewhat=(ci)?NULL:NULL; -if(ar->namewhat==NULL){ -ar->namewhat=""; -ar->name=NULL; -} -break; -} -case'L': -case'f': -break; -default:status=0; -} -} -return status; -} -static int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar){ -int status; -Closure*f=NULL; -CallInfo*ci=NULL; -if(*what=='>'){ -StkId func=L->top-1; -luai_apicheck(L,ttisfunction(func)); -what++; -f=clvalue(func); -L->top--; -} -else if(ar->i_ci!=0){ -ci=L->base_ci+ar->i_ci; -f=clvalue(ci->func); -} -status=auxgetinfo(L,what,ar,f,ci); -if(strchr(what,'f')){ -if(f==NULL)setnilvalue(L->top); -else setclvalue(L,L->top,f); -incr_top(L); -} -if(strchr(what,'L')) -collectvalidlines(L,f); -return status; -} -static int isinstack(CallInfo*ci,const TValue*o){ -StkId p; -for(p=ci->base;ptop;p++) -if(o==p)return 1; -return 0; -} -static void luaG_typeerror(lua_State*L,const TValue*o,const char*op){ -const char*name=NULL; -const char*t=luaT_typenames[ttype(o)]; -const char*kind=(isinstack(L->ci,o))? -NULL: -NULL; -if(kind) -luaG_runerror(L,"attempt to %s %s "LUA_QL("%s")" (a %s value)", -op,kind,name,t); -else -luaG_runerror(L,"attempt to %s a %s value",op,t); -} -static void luaG_concaterror(lua_State*L,StkId p1,StkId p2){ -if(ttisstring(p1)||ttisnumber(p1))p1=p2; -luaG_typeerror(L,p1,"concatenate"); -} -static void luaG_aritherror(lua_State*L,const TValue*p1,const TValue*p2){ -TValue temp; -if(luaV_tonumber(p1,&temp)==NULL) -p2=p1; -luaG_typeerror(L,p2,"perform arithmetic on"); -} -static int luaG_ordererror(lua_State*L,const TValue*p1,const TValue*p2){ -const char*t1=luaT_typenames[ttype(p1)]; -const char*t2=luaT_typenames[ttype(p2)]; -if(t1[2]==t2[2]) -luaG_runerror(L,"attempt to compare two %s values",t1); -else -luaG_runerror(L,"attempt to compare %s with %s",t1,t2); -return 0; -} -static void addinfo(lua_State*L,const char*msg){ -CallInfo*ci=L->ci; -if(isLua(ci)){ -char buff[60]; -int line=currentline(L,ci); -luaO_chunkid(buff,getstr(getluaproto(ci)->source),60); -luaO_pushfstring(L,"%s:%d: %s",buff,line,msg); -} -} -static void luaG_errormsg(lua_State*L){ -if(L->errfunc!=0){ -StkId errfunc=restorestack(L,L->errfunc); -if(!ttisfunction(errfunc))luaD_throw(L,5); -setobj(L,L->top,L->top-1); -setobj(L,L->top-1,errfunc); -incr_top(L); -luaD_call(L,L->top-2,1); -} -luaD_throw(L,2); -} -static void luaG_runerror(lua_State*L,const char*fmt,...){ -va_list argp; -va_start(argp,fmt); -addinfo(L,luaO_pushvfstring(L,fmt,argp)); -va_end(argp); -luaG_errormsg(L); -} -static int luaZ_fill(ZIO*z){ -size_t size; -lua_State*L=z->L; -const char*buff; -buff=z->reader(L,z->data,&size); -if(buff==NULL||size==0)return(-1); -z->n=size-1; -z->p=buff; -return char2int(*(z->p++)); -} -static void luaZ_init(lua_State*L,ZIO*z,lua_Reader reader,void*data){ -z->L=L; -z->reader=reader; -z->data=data; -z->n=0; -z->p=NULL; -} -static char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){ -if(n>buff->buffsize){ -if(n<32)n=32; -luaZ_resizebuffer(L,buff,n); -} -return buff->buffer; -} -#define opmode(t,a,b,c,m)(((t)<<7)|((a)<<6)|((b)<<4)|((c)<<2)|(m)) -static const lu_byte luaP_opmodes[(cast(int,OP_VARARG)+1)]={ -opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgK,OpArgN,iABx) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgU,OpArgN,iABC) -,opmode(0,1,OpArgK,OpArgN,iABx) -,opmode(0,1,OpArgR,OpArgK,iABC) -,opmode(0,0,OpArgK,OpArgN,iABx) -,opmode(0,0,OpArgU,OpArgN,iABC) -,opmode(0,0,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,1,OpArgR,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgK,OpArgK,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgR,iABC) -,opmode(0,0,OpArgR,OpArgN,iAsBx) -,opmode(1,0,OpArgK,OpArgK,iABC) -,opmode(1,0,OpArgK,OpArgK,iABC) -,opmode(1,0,OpArgK,OpArgK,iABC) -,opmode(1,1,OpArgR,OpArgU,iABC) -,opmode(1,1,OpArgR,OpArgU,iABC) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,1,OpArgU,OpArgU,iABC) -,opmode(0,0,OpArgU,OpArgN,iABC) -,opmode(0,1,OpArgR,OpArgN,iAsBx) -,opmode(0,1,OpArgR,OpArgN,iAsBx) -,opmode(1,0,OpArgN,OpArgU,iABC) -,opmode(0,0,OpArgU,OpArgU,iABC) -,opmode(0,0,OpArgN,OpArgN,iABC) -,opmode(0,1,OpArgU,OpArgN,iABx) -,opmode(0,1,OpArgU,OpArgN,iABC) -}; -#define next(ls)(ls->current=zgetc(ls->z)) -#define currIsNewline(ls)(ls->current=='\n'||ls->current=='\r') -static const char*const luaX_tokens[]={ -"and","break","do","else","elseif", -"end","false","for","function","if", -"in","local","nil","not","or","repeat", -"return","then","true","until","while", -"..","...","==",">=","<=","~=", -"","","","", -NULL -}; -#define save_and_next(ls)(save(ls,ls->current),next(ls)) -static void save(LexState*ls,int c){ -Mbuffer*b=ls->buff; -if(b->n+1>b->buffsize){ -size_t newsize; -if(b->buffsize>=((size_t)(~(size_t)0)-2)/2) -luaX_lexerror(ls,"lexical element too long",0); -newsize=b->buffsize*2; -luaZ_resizebuffer(ls->L,b,newsize); -} -b->buffer[b->n++]=cast(char,c); -} -static void luaX_init(lua_State*L){ -int i; -for(i=0;i<(cast(int,TK_WHILE-257+1));i++){ -TString*ts=luaS_new(L,luaX_tokens[i]); -luaS_fix(ts); -ts->tsv.reserved=cast_byte(i+1); -} -} -static const char*luaX_token2str(LexState*ls,int token){ -if(token<257){ -return(iscntrl(token))?luaO_pushfstring(ls->L,"char(%d)",token): -luaO_pushfstring(ls->L,"%c",token); -} -else -return luaX_tokens[token-257]; -} -static const char*txtToken(LexState*ls,int token){ -switch(token){ -case TK_NAME: -case TK_STRING: -case TK_NUMBER: -save(ls,'\0'); -return luaZ_buffer(ls->buff); -default: -return luaX_token2str(ls,token); -} -} -static void luaX_lexerror(LexState*ls,const char*msg,int token){ -char buff[80]; -luaO_chunkid(buff,getstr(ls->source),80); -msg=luaO_pushfstring(ls->L,"%s:%d: %s",buff,ls->linenumber,msg); -if(token) -luaO_pushfstring(ls->L,"%s near "LUA_QL("%s"),msg,txtToken(ls,token)); -luaD_throw(ls->L,3); -} -static void luaX_syntaxerror(LexState*ls,const char*msg){ -luaX_lexerror(ls,msg,ls->t.token); -} -static TString*luaX_newstring(LexState*ls,const char*str,size_t l){ -lua_State*L=ls->L; -TString*ts=luaS_newlstr(L,str,l); -TValue*o=luaH_setstr(L,ls->fs->h,ts); -if(ttisnil(o)){ -setbvalue(o,1); -luaC_checkGC(L); -} -return ts; -} -static void inclinenumber(LexState*ls){ -int old=ls->current; -next(ls); -if(currIsNewline(ls)&&ls->current!=old) -next(ls); -if(++ls->linenumber>=(INT_MAX-2)) -luaX_syntaxerror(ls,"chunk has too many lines"); -} -static void luaX_setinput(lua_State*L,LexState*ls,ZIO*z,TString*source){ -ls->decpoint='.'; -ls->L=L; -ls->lookahead.token=TK_EOS; -ls->z=z; -ls->fs=NULL; -ls->linenumber=1; -ls->lastline=1; -ls->source=source; -luaZ_resizebuffer(ls->L,ls->buff,32); -next(ls); -} -static int check_next(LexState*ls,const char*set){ -if(!strchr(set,ls->current)) -return 0; -save_and_next(ls); -return 1; -} -static void buffreplace(LexState*ls,char from,char to){ -size_t n=luaZ_bufflen(ls->buff); -char*p=luaZ_buffer(ls->buff); -while(n--) -if(p[n]==from)p[n]=to; -} -static void read_numeral(LexState*ls,SemInfo*seminfo){ -do{ -save_and_next(ls); -}while(isdigit(ls->current)||ls->current=='.'); -if(check_next(ls,"Ee")) -check_next(ls,"+-"); -while(isalnum(ls->current)||ls->current=='_') -save_and_next(ls); -save(ls,'\0'); -buffreplace(ls,'.',ls->decpoint); -if(!luaO_str2d(luaZ_buffer(ls->buff),&seminfo->r)) -luaX_lexerror(ls,"malformed number",TK_NUMBER); -} -static int skip_sep(LexState*ls){ -int count=0; -int s=ls->current; -save_and_next(ls); -while(ls->current=='='){ -save_and_next(ls); -count++; -} -return(ls->current==s)?count:(-count)-1; -} -static void read_long_string(LexState*ls,SemInfo*seminfo,int sep){ -int cont=0; -(void)(cont); -save_and_next(ls); -if(currIsNewline(ls)) -inclinenumber(ls); -for(;;){ -switch(ls->current){ -case(-1): -luaX_lexerror(ls,(seminfo)?"unfinished long string": -"unfinished long comment",TK_EOS); -break; -case']':{ -if(skip_sep(ls)==sep){ -save_and_next(ls); -goto endloop; -} -break; -} -case'\n': -case'\r':{ -save(ls,'\n'); -inclinenumber(ls); -if(!seminfo)luaZ_resetbuffer(ls->buff); -break; -} -default:{ -if(seminfo)save_and_next(ls); -else next(ls); -} -} -}endloop: -if(seminfo) -seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+(2+sep), -luaZ_bufflen(ls->buff)-2*(2+sep)); -} -static void read_string(LexState*ls,int del,SemInfo*seminfo){ -save_and_next(ls); -while(ls->current!=del){ -switch(ls->current){ -case(-1): -luaX_lexerror(ls,"unfinished string",TK_EOS); -continue; -case'\n': -case'\r': -luaX_lexerror(ls,"unfinished string",TK_STRING); -continue; -case'\\':{ -int c; -next(ls); -switch(ls->current){ -case'a':c='\a';break; -case'b':c='\b';break; -case'f':c='\f';break; -case'n':c='\n';break; -case'r':c='\r';break; -case't':c='\t';break; -case'v':c='\v';break; -case'\n': -case'\r':save(ls,'\n');inclinenumber(ls);continue; -case(-1):continue; -default:{ -if(!isdigit(ls->current)) -save_and_next(ls); -else{ -int i=0; -c=0; -do{ -c=10*c+(ls->current-'0'); -next(ls); -}while(++i<3&&isdigit(ls->current)); -if(c>UCHAR_MAX) -luaX_lexerror(ls,"escape sequence too large",TK_STRING); -save(ls,c); -} -continue; -} -} -save(ls,c); -next(ls); -continue; -} -default: -save_and_next(ls); -} -} -save_and_next(ls); -seminfo->ts=luaX_newstring(ls,luaZ_buffer(ls->buff)+1, -luaZ_bufflen(ls->buff)-2); -} -static int llex(LexState*ls,SemInfo*seminfo){ -luaZ_resetbuffer(ls->buff); -for(;;){ -switch(ls->current){ -case'\n': -case'\r':{ -inclinenumber(ls); -continue; -} -case'-':{ -next(ls); -if(ls->current!='-')return'-'; -next(ls); -if(ls->current=='['){ -int sep=skip_sep(ls); -luaZ_resetbuffer(ls->buff); -if(sep>=0){ -read_long_string(ls,NULL,sep); -luaZ_resetbuffer(ls->buff); -continue; -} -} -while(!currIsNewline(ls)&&ls->current!=(-1)) -next(ls); -continue; -} -case'[':{ -int sep=skip_sep(ls); -if(sep>=0){ -read_long_string(ls,seminfo,sep); -return TK_STRING; -} -else if (sep!=-1)luaX_lexerror(ls,"invalid long string delimiter",TK_STRING); -return'['; -} -case'=':{ -next(ls); -if(ls->current!='=')return'='; -else{next(ls);return TK_EQ;} -} -case'<':{ -next(ls); -if(ls->current!='=')return'<'; -else{next(ls);return TK_LE;} -} -case'>':{ -next(ls); -if(ls->current!='=')return'>'; -else{next(ls);return TK_GE;} -} -case'~':{ -next(ls); -if(ls->current!='=')return'~'; -else{next(ls);return TK_NE;} -} -case'"': -case'\'':{ -read_string(ls,ls->current,seminfo); -return TK_STRING; -} -case'.':{ -save_and_next(ls); -if(check_next(ls,".")){ -if(check_next(ls,".")) -return TK_DOTS; -else return TK_CONCAT; -} -else if(!isdigit(ls->current))return'.'; -else{ -read_numeral(ls,seminfo); -return TK_NUMBER; -} -} -case(-1):{ -return TK_EOS; -} -default:{ -if(isspace(ls->current)){ -next(ls); -continue; -} -else if(isdigit(ls->current)){ -read_numeral(ls,seminfo); -return TK_NUMBER; -} -else if(isalpha(ls->current)||ls->current=='_'){ -TString*ts; -do{ -save_and_next(ls); -}while(isalnum(ls->current)||ls->current=='_'); -ts=luaX_newstring(ls,luaZ_buffer(ls->buff), -luaZ_bufflen(ls->buff)); -if(ts->tsv.reserved>0) -return ts->tsv.reserved-1+257; -else{ -seminfo->ts=ts; -return TK_NAME; -} -} -else{ -int c=ls->current; -next(ls); -return c; -} -} -} -} -} -static void luaX_next(LexState*ls){ -ls->lastline=ls->linenumber; -if(ls->lookahead.token!=TK_EOS){ -ls->t=ls->lookahead; -ls->lookahead.token=TK_EOS; -} -else -ls->t.token=llex(ls,&ls->t.seminfo); -} -static void luaX_lookahead(LexState*ls){ -ls->lookahead.token=llex(ls,&ls->lookahead.seminfo); -} -#define hasjumps(e)((e)->t!=(e)->f) -static int isnumeral(expdesc*e){ -return(e->k==VKNUM&&e->t==(-1)&&e->f==(-1)); -} -static void luaK_nil(FuncState*fs,int from,int n){ -Instruction*previous; -if(fs->pc>fs->lasttarget){ -if(fs->pc==0){ -if(from>=fs->nactvar) -return; -} -else{ -previous=&fs->f->code[fs->pc-1]; -if(GET_OPCODE(*previous)==OP_LOADNIL){ -int pfrom=GETARG_A(*previous); -int pto=GETARG_B(*previous); -if(pfrom<=from&&from<=pto+1){ -if(from+n-1>pto) -SETARG_B(*previous,from+n-1); -return; -} -} -} -} -luaK_codeABC(fs,OP_LOADNIL,from,from+n-1,0); -} -static int luaK_jump(FuncState*fs){ -int jpc=fs->jpc; -int j; -fs->jpc=(-1); -j=luaK_codeAsBx(fs,OP_JMP,0,(-1)); -luaK_concat(fs,&j,jpc); -return j; -} -static void luaK_ret(FuncState*fs,int first,int nret){ -luaK_codeABC(fs,OP_RETURN,first,nret+1,0); -} -static int condjump(FuncState*fs,OpCode op,int A,int B,int C){ -luaK_codeABC(fs,op,A,B,C); -return luaK_jump(fs); -} -static void fixjump(FuncState*fs,int pc,int dest){ -Instruction*jmp=&fs->f->code[pc]; -int offset=dest-(pc+1); -if(abs(offset)>(((1<<(9+9))-1)>>1)) -luaX_syntaxerror(fs->ls,"control structure too long"); -SETARG_sBx(*jmp,offset); -} -static int luaK_getlabel(FuncState*fs){ -fs->lasttarget=fs->pc; -return fs->pc; -} -static int getjump(FuncState*fs,int pc){ -int offset=GETARG_sBx(fs->f->code[pc]); -if(offset==(-1)) -return(-1); -else -return(pc+1)+offset; -} -static Instruction*getjumpcontrol(FuncState*fs,int pc){ -Instruction*pi=&fs->f->code[pc]; -if(pc>=1&&testTMode(GET_OPCODE(*(pi-1)))) -return pi-1; -else -return pi; -} -static int need_value(FuncState*fs,int list){ -for(;list!=(-1);list=getjump(fs,list)){ -Instruction i=*getjumpcontrol(fs,list); -if(GET_OPCODE(i)!=OP_TESTSET)return 1; -} -return 0; -} -static int patchtestreg(FuncState*fs,int node,int reg){ -Instruction*i=getjumpcontrol(fs,node); -if(GET_OPCODE(*i)!=OP_TESTSET) -return 0; -if(reg!=((1<<8)-1)&®!=GETARG_B(*i)) -SETARG_A(*i,reg); -else -*i=CREATE_ABC(OP_TEST,GETARG_B(*i),0,GETARG_C(*i)); -return 1; -} -static void removevalues(FuncState*fs,int list){ -for(;list!=(-1);list=getjump(fs,list)) -patchtestreg(fs,list,((1<<8)-1)); -} -static void patchlistaux(FuncState*fs,int list,int vtarget,int reg, -int dtarget){ -while(list!=(-1)){ -int next=getjump(fs,list); -if(patchtestreg(fs,list,reg)) -fixjump(fs,list,vtarget); -else -fixjump(fs,list,dtarget); -list=next; -} -} -static void dischargejpc(FuncState*fs){ -patchlistaux(fs,fs->jpc,fs->pc,((1<<8)-1),fs->pc); -fs->jpc=(-1); -} -static void luaK_patchlist(FuncState*fs,int list,int target){ -if(target==fs->pc) -luaK_patchtohere(fs,list); -else{ -patchlistaux(fs,list,target,((1<<8)-1),target); -} -} -static void luaK_patchtohere(FuncState*fs,int list){ -luaK_getlabel(fs); -luaK_concat(fs,&fs->jpc,list); -} -static void luaK_concat(FuncState*fs,int*l1,int l2){ -if(l2==(-1))return; -else if(*l1==(-1)) -*l1=l2; -else{ -int list=*l1; -int next; -while((next=getjump(fs,list))!=(-1)) -list=next; -fixjump(fs,list,l2); -} -} -static void luaK_checkstack(FuncState*fs,int n){ -int newstack=fs->freereg+n; -if(newstack>fs->f->maxstacksize){ -if(newstack>=250) -luaX_syntaxerror(fs->ls,"function or expression too complex"); -fs->f->maxstacksize=cast_byte(newstack); -} -} -static void luaK_reserveregs(FuncState*fs,int n){ -luaK_checkstack(fs,n); -fs->freereg+=n; -} -static void freereg(FuncState*fs,int reg){ -if(!ISK(reg)&®>=fs->nactvar){ -fs->freereg--; -} -} -static void freeexp(FuncState*fs,expdesc*e){ -if(e->k==VNONRELOC) -freereg(fs,e->u.s.info); -} -static int addk(FuncState*fs,TValue*k,TValue*v){ -lua_State*L=fs->L; -TValue*idx=luaH_set(L,fs->h,k); -Proto*f=fs->f; -int oldsize=f->sizek; -if(ttisnumber(idx)){ -return cast_int(nvalue(idx)); -} -else{ -setnvalue(idx,cast_num(fs->nk)); -luaM_growvector(L,f->k,fs->nk,f->sizek,TValue, -((1<<(9+9))-1),"constant table overflow"); -while(oldsizesizek)setnilvalue(&f->k[oldsize++]); -setobj(L,&f->k[fs->nk],v); -luaC_barrier(L,f,v); -return fs->nk++; -} -} -static int luaK_stringK(FuncState*fs,TString*s){ -TValue o; -setsvalue(fs->L,&o,s); -return addk(fs,&o,&o); -} -static int luaK_numberK(FuncState*fs,lua_Number r){ -TValue o; -setnvalue(&o,r); -return addk(fs,&o,&o); -} -static int boolK(FuncState*fs,int b){ -TValue o; -setbvalue(&o,b); -return addk(fs,&o,&o); -} -static int nilK(FuncState*fs){ -TValue k,v; -setnilvalue(&v); -sethvalue(fs->L,&k,fs->h); -return addk(fs,&k,&v); -} -static void luaK_setreturns(FuncState*fs,expdesc*e,int nresults){ -if(e->k==VCALL){ -SETARG_C(getcode(fs,e),nresults+1); -} -else if(e->k==VVARARG){ -SETARG_B(getcode(fs,e),nresults+1); -SETARG_A(getcode(fs,e),fs->freereg); -luaK_reserveregs(fs,1); -} -} -static void luaK_setoneret(FuncState*fs,expdesc*e){ -if(e->k==VCALL){ -e->k=VNONRELOC; -e->u.s.info=GETARG_A(getcode(fs,e)); -} -else if(e->k==VVARARG){ -SETARG_B(getcode(fs,e),2); -e->k=VRELOCABLE; -} -} -static void luaK_dischargevars(FuncState*fs,expdesc*e){ -switch(e->k){ -case VLOCAL:{ -e->k=VNONRELOC; -break; -} -case VUPVAL:{ -e->u.s.info=luaK_codeABC(fs,OP_GETUPVAL,0,e->u.s.info,0); -e->k=VRELOCABLE; -break; -} -case VGLOBAL:{ -e->u.s.info=luaK_codeABx(fs,OP_GETGLOBAL,0,e->u.s.info); -e->k=VRELOCABLE; -break; -} -case VINDEXED:{ -freereg(fs,e->u.s.aux); -freereg(fs,e->u.s.info); -e->u.s.info=luaK_codeABC(fs,OP_GETTABLE,0,e->u.s.info,e->u.s.aux); -e->k=VRELOCABLE; -break; -} -case VVARARG: -case VCALL:{ -luaK_setoneret(fs,e); -break; -} -default:break; -} -} -static int code_label(FuncState*fs,int A,int b,int jump){ -luaK_getlabel(fs); -return luaK_codeABC(fs,OP_LOADBOOL,A,b,jump); -} -static void discharge2reg(FuncState*fs,expdesc*e,int reg){ -luaK_dischargevars(fs,e); -switch(e->k){ -case VNIL:{ -luaK_nil(fs,reg,1); -break; -} -case VFALSE:case VTRUE:{ -luaK_codeABC(fs,OP_LOADBOOL,reg,e->k==VTRUE,0); -break; -} -case VK:{ -luaK_codeABx(fs,OP_LOADK,reg,e->u.s.info); -break; -} -case VKNUM:{ -luaK_codeABx(fs,OP_LOADK,reg,luaK_numberK(fs,e->u.nval)); -break; -} -case VRELOCABLE:{ -Instruction*pc=&getcode(fs,e); -SETARG_A(*pc,reg); -break; -} -case VNONRELOC:{ -if(reg!=e->u.s.info) -luaK_codeABC(fs,OP_MOVE,reg,e->u.s.info,0); -break; -} -default:{ -return; -} -} -e->u.s.info=reg; -e->k=VNONRELOC; -} -static void discharge2anyreg(FuncState*fs,expdesc*e){ -if(e->k!=VNONRELOC){ -luaK_reserveregs(fs,1); -discharge2reg(fs,e,fs->freereg-1); -} -} -static void exp2reg(FuncState*fs,expdesc*e,int reg){ -discharge2reg(fs,e,reg); -if(e->k==VJMP) -luaK_concat(fs,&e->t,e->u.s.info); -if(hasjumps(e)){ -int final; -int p_f=(-1); -int p_t=(-1); -if(need_value(fs,e->t)||need_value(fs,e->f)){ -int fj=(e->k==VJMP)?(-1):luaK_jump(fs); -p_f=code_label(fs,reg,0,1); -p_t=code_label(fs,reg,1,0); -luaK_patchtohere(fs,fj); -} -final=luaK_getlabel(fs); -patchlistaux(fs,e->f,final,reg,p_f); -patchlistaux(fs,e->t,final,reg,p_t); -} -e->f=e->t=(-1); -e->u.s.info=reg; -e->k=VNONRELOC; -} -static void luaK_exp2nextreg(FuncState*fs,expdesc*e){ -luaK_dischargevars(fs,e); -freeexp(fs,e); -luaK_reserveregs(fs,1); -exp2reg(fs,e,fs->freereg-1); -} -static int luaK_exp2anyreg(FuncState*fs,expdesc*e){ -luaK_dischargevars(fs,e); -if(e->k==VNONRELOC){ -if(!hasjumps(e))return e->u.s.info; -if(e->u.s.info>=fs->nactvar){ -exp2reg(fs,e,e->u.s.info); -return e->u.s.info; -} -} -luaK_exp2nextreg(fs,e); -return e->u.s.info; -} -static void luaK_exp2val(FuncState*fs,expdesc*e){ -if(hasjumps(e)) -luaK_exp2anyreg(fs,e); -else -luaK_dischargevars(fs,e); -} -static int luaK_exp2RK(FuncState*fs,expdesc*e){ -luaK_exp2val(fs,e); -switch(e->k){ -case VKNUM: -case VTRUE: -case VFALSE: -case VNIL:{ -if(fs->nk<=((1<<(9-1))-1)){ -e->u.s.info=(e->k==VNIL)?nilK(fs): -(e->k==VKNUM)?luaK_numberK(fs,e->u.nval): -boolK(fs,(e->k==VTRUE)); -e->k=VK; -return RKASK(e->u.s.info); -} -else break; -} -case VK:{ -if(e->u.s.info<=((1<<(9-1))-1)) -return RKASK(e->u.s.info); -else break; -} -default:break; -} -return luaK_exp2anyreg(fs,e); -} -static void luaK_storevar(FuncState*fs,expdesc*var,expdesc*ex){ -switch(var->k){ -case VLOCAL:{ -freeexp(fs,ex); -exp2reg(fs,ex,var->u.s.info); -return; -} -case VUPVAL:{ -int e=luaK_exp2anyreg(fs,ex); -luaK_codeABC(fs,OP_SETUPVAL,e,var->u.s.info,0); -break; -} -case VGLOBAL:{ -int e=luaK_exp2anyreg(fs,ex); -luaK_codeABx(fs,OP_SETGLOBAL,e,var->u.s.info); -break; -} -case VINDEXED:{ -int e=luaK_exp2RK(fs,ex); -luaK_codeABC(fs,OP_SETTABLE,var->u.s.info,var->u.s.aux,e); -break; -} -default:{ -break; -} -} -freeexp(fs,ex); -} -static void luaK_self(FuncState*fs,expdesc*e,expdesc*key){ -int func; -luaK_exp2anyreg(fs,e); -freeexp(fs,e); -func=fs->freereg; -luaK_reserveregs(fs,2); -luaK_codeABC(fs,OP_SELF,func,e->u.s.info,luaK_exp2RK(fs,key)); -freeexp(fs,key); -e->u.s.info=func; -e->k=VNONRELOC; -} -static void invertjump(FuncState*fs,expdesc*e){ -Instruction*pc=getjumpcontrol(fs,e->u.s.info); -SETARG_A(*pc,!(GETARG_A(*pc))); -} -static int jumponcond(FuncState*fs,expdesc*e,int cond){ -if(e->k==VRELOCABLE){ -Instruction ie=getcode(fs,e); -if(GET_OPCODE(ie)==OP_NOT){ -fs->pc--; -return condjump(fs,OP_TEST,GETARG_B(ie),0,!cond); -} -} -discharge2anyreg(fs,e); -freeexp(fs,e); -return condjump(fs,OP_TESTSET,((1<<8)-1),e->u.s.info,cond); -} -static void luaK_goiftrue(FuncState*fs,expdesc*e){ -int pc; -luaK_dischargevars(fs,e); -switch(e->k){ -case VK:case VKNUM:case VTRUE:{ -pc=(-1); -break; -} -case VJMP:{ -invertjump(fs,e); -pc=e->u.s.info; -break; -} -default:{ -pc=jumponcond(fs,e,0); -break; -} -} -luaK_concat(fs,&e->f,pc); -luaK_patchtohere(fs,e->t); -e->t=(-1); -} -static void luaK_goiffalse(FuncState*fs,expdesc*e){ -int pc; -luaK_dischargevars(fs,e); -switch(e->k){ -case VNIL:case VFALSE:{ -pc=(-1); -break; -} -case VJMP:{ -pc=e->u.s.info; -break; -} -default:{ -pc=jumponcond(fs,e,1); -break; -} -} -luaK_concat(fs,&e->t,pc); -luaK_patchtohere(fs,e->f); -e->f=(-1); -} -static void codenot(FuncState*fs,expdesc*e){ -luaK_dischargevars(fs,e); -switch(e->k){ -case VNIL:case VFALSE:{ -e->k=VTRUE; -break; -} -case VK:case VKNUM:case VTRUE:{ -e->k=VFALSE; -break; -} -case VJMP:{ -invertjump(fs,e); -break; -} -case VRELOCABLE: -case VNONRELOC:{ -discharge2anyreg(fs,e); -freeexp(fs,e); -e->u.s.info=luaK_codeABC(fs,OP_NOT,0,e->u.s.info,0); -e->k=VRELOCABLE; -break; -} -default:{ -break; -} -} -{int temp=e->f;e->f=e->t;e->t=temp;} -removevalues(fs,e->f); -removevalues(fs,e->t); -} -static void luaK_indexed(FuncState*fs,expdesc*t,expdesc*k){ -t->u.s.aux=luaK_exp2RK(fs,k); -t->k=VINDEXED; -} -static int constfolding(OpCode op,expdesc*e1,expdesc*e2){ -lua_Number v1,v2,r; -if(!isnumeral(e1)||!isnumeral(e2))return 0; -v1=e1->u.nval; -v2=e2->u.nval; -switch(op){ -case OP_ADD:r=luai_numadd(v1,v2);break; -case OP_SUB:r=luai_numsub(v1,v2);break; -case OP_MUL:r=luai_nummul(v1,v2);break; -case OP_DIV: -if(v2==0)return 0; -r=luai_numdiv(v1,v2);break; -case OP_MOD: -if(v2==0)return 0; -r=luai_nummod(v1,v2);break; -case OP_POW:r=luai_numpow(v1,v2);break; -case OP_UNM:r=luai_numunm(v1);break; -case OP_LEN:return 0; -default:r=0;break; -} -if(luai_numisnan(r))return 0; -e1->u.nval=r; -return 1; -} -static void codearith(FuncState*fs,OpCode op,expdesc*e1,expdesc*e2){ -if(constfolding(op,e1,e2)) -return; -else{ -int o2=(op!=OP_UNM&&op!=OP_LEN)?luaK_exp2RK(fs,e2):0; -int o1=luaK_exp2RK(fs,e1); -if(o1>o2){ -freeexp(fs,e1); -freeexp(fs,e2); -} -else{ -freeexp(fs,e2); -freeexp(fs,e1); -} -e1->u.s.info=luaK_codeABC(fs,op,0,o1,o2); -e1->k=VRELOCABLE; -} -} -static void codecomp(FuncState*fs,OpCode op,int cond,expdesc*e1, -expdesc*e2){ -int o1=luaK_exp2RK(fs,e1); -int o2=luaK_exp2RK(fs,e2); -freeexp(fs,e2); -freeexp(fs,e1); -if(cond==0&&op!=OP_EQ){ -int temp; -temp=o1;o1=o2;o2=temp; -cond=1; -} -e1->u.s.info=condjump(fs,op,cond,o1,o2); -e1->k=VJMP; -} -static void luaK_prefix(FuncState*fs,UnOpr op,expdesc*e){ -expdesc e2; -e2.t=e2.f=(-1);e2.k=VKNUM;e2.u.nval=0; -switch(op){ -case OPR_MINUS:{ -if(!isnumeral(e)) -luaK_exp2anyreg(fs,e); -codearith(fs,OP_UNM,e,&e2); -break; -} -case OPR_NOT:codenot(fs,e);break; -case OPR_LEN:{ -luaK_exp2anyreg(fs,e); -codearith(fs,OP_LEN,e,&e2); -break; -} -default:; -} -} -static void luaK_infix(FuncState*fs,BinOpr op,expdesc*v){ -switch(op){ -case OPR_AND:{ -luaK_goiftrue(fs,v); -break; -} -case OPR_OR:{ -luaK_goiffalse(fs,v); -break; -} -case OPR_CONCAT:{ -luaK_exp2nextreg(fs,v); -break; -} -case OPR_ADD:case OPR_SUB:case OPR_MUL:case OPR_DIV: -case OPR_MOD:case OPR_POW:{ -if(!isnumeral(v))luaK_exp2RK(fs,v); -break; -} -default:{ -luaK_exp2RK(fs,v); -break; -} -} -} -static void luaK_posfix(FuncState*fs,BinOpr op,expdesc*e1,expdesc*e2){ -switch(op){ -case OPR_AND:{ -luaK_dischargevars(fs,e2); -luaK_concat(fs,&e2->f,e1->f); -*e1=*e2; -break; -} -case OPR_OR:{ -luaK_dischargevars(fs,e2); -luaK_concat(fs,&e2->t,e1->t); -*e1=*e2; -break; -} -case OPR_CONCAT:{ -luaK_exp2val(fs,e2); -if(e2->k==VRELOCABLE&&GET_OPCODE(getcode(fs,e2))==OP_CONCAT){ -freeexp(fs,e1); -SETARG_B(getcode(fs,e2),e1->u.s.info); -e1->k=VRELOCABLE;e1->u.s.info=e2->u.s.info; -} -else{ -luaK_exp2nextreg(fs,e2); -codearith(fs,OP_CONCAT,e1,e2); -} -break; -} -case OPR_ADD:codearith(fs,OP_ADD,e1,e2);break; -case OPR_SUB:codearith(fs,OP_SUB,e1,e2);break; -case OPR_MUL:codearith(fs,OP_MUL,e1,e2);break; -case OPR_DIV:codearith(fs,OP_DIV,e1,e2);break; -case OPR_MOD:codearith(fs,OP_MOD,e1,e2);break; -case OPR_POW:codearith(fs,OP_POW,e1,e2);break; -case OPR_EQ:codecomp(fs,OP_EQ,1,e1,e2);break; -case OPR_NE:codecomp(fs,OP_EQ,0,e1,e2);break; -case OPR_LT:codecomp(fs,OP_LT,1,e1,e2);break; -case OPR_LE:codecomp(fs,OP_LE,1,e1,e2);break; -case OPR_GT:codecomp(fs,OP_LT,0,e1,e2);break; -case OPR_GE:codecomp(fs,OP_LE,0,e1,e2);break; -default:; -} -} -static void luaK_fixline(FuncState*fs,int line){ -fs->f->lineinfo[fs->pc-1]=line; -} -static int luaK_code(FuncState*fs,Instruction i,int line){ -Proto*f=fs->f; -dischargejpc(fs); -luaM_growvector(fs->L,f->code,fs->pc,f->sizecode,Instruction, -(INT_MAX-2),"code size overflow"); -f->code[fs->pc]=i; -luaM_growvector(fs->L,f->lineinfo,fs->pc,f->sizelineinfo,int, -(INT_MAX-2),"code size overflow"); -f->lineinfo[fs->pc]=line; -return fs->pc++; -} -static int luaK_codeABC(FuncState*fs,OpCode o,int a,int b,int c){ -return luaK_code(fs,CREATE_ABC(o,a,b,c),fs->ls->lastline); -} -static int luaK_codeABx(FuncState*fs,OpCode o,int a,unsigned int bc){ -return luaK_code(fs,CREATE_ABx(o,a,bc),fs->ls->lastline); -} -static void luaK_setlist(FuncState*fs,int base,int nelems,int tostore){ -int c=(nelems-1)/50+1; -int b=(tostore==(-1))?0:tostore; -if(c<=((1<<9)-1)) -luaK_codeABC(fs,OP_SETLIST,base,b,c); -else{ -luaK_codeABC(fs,OP_SETLIST,base,b,0); -luaK_code(fs,cast(Instruction,c),fs->ls->lastline); -} -fs->freereg=base+1; -} -#define hasmultret(k)((k)==VCALL||(k)==VVARARG) -#define getlocvar(fs,i)((fs)->f->locvars[(fs)->actvar[i]]) -#define luaY_checklimit(fs,v,l,m)if((v)>(l))errorlimit(fs,l,m) -typedef struct BlockCnt{ -struct BlockCnt*previous; -int breaklist; -lu_byte nactvar; -lu_byte upval; -lu_byte isbreakable; -}BlockCnt; -static void chunk(LexState*ls); -static void expr(LexState*ls,expdesc*v); -static void anchor_token(LexState*ls){ -if(ls->t.token==TK_NAME||ls->t.token==TK_STRING){ -TString*ts=ls->t.seminfo.ts; -luaX_newstring(ls,getstr(ts),ts->tsv.len); -} -} -static void error_expected(LexState*ls,int token){ -luaX_syntaxerror(ls, -luaO_pushfstring(ls->L,LUA_QL("%s")" expected",luaX_token2str(ls,token))); -} -static void errorlimit(FuncState*fs,int limit,const char*what){ -const char*msg=(fs->f->linedefined==0)? -luaO_pushfstring(fs->L,"main function has more than %d %s",limit,what): -luaO_pushfstring(fs->L,"function at line %d has more than %d %s", -fs->f->linedefined,limit,what); -luaX_lexerror(fs->ls,msg,0); -} -static int testnext(LexState*ls,int c){ -if(ls->t.token==c){ -luaX_next(ls); -return 1; -} -else return 0; -} -static void check(LexState*ls,int c){ -if(ls->t.token!=c) -error_expected(ls,c); -} -static void checknext(LexState*ls,int c){ -check(ls,c); -luaX_next(ls); -} -#define check_condition(ls,c,msg){if(!(c))luaX_syntaxerror(ls,msg);} -static void check_match(LexState*ls,int what,int who,int where){ -if(!testnext(ls,what)){ -if(where==ls->linenumber) -error_expected(ls,what); -else{ -luaX_syntaxerror(ls,luaO_pushfstring(ls->L, -LUA_QL("%s")" expected (to close "LUA_QL("%s")" at line %d)", -luaX_token2str(ls,what),luaX_token2str(ls,who),where)); -} -} -} -static TString*str_checkname(LexState*ls){ -TString*ts; -check(ls,TK_NAME); -ts=ls->t.seminfo.ts; -luaX_next(ls); -return ts; -} -static void init_exp(expdesc*e,expkind k,int i){ -e->f=e->t=(-1); -e->k=k; -e->u.s.info=i; -} -static void codestring(LexState*ls,expdesc*e,TString*s){ -init_exp(e,VK,luaK_stringK(ls->fs,s)); -} -static void checkname(LexState*ls,expdesc*e){ -codestring(ls,e,str_checkname(ls)); -} -static int registerlocalvar(LexState*ls,TString*varname){ -FuncState*fs=ls->fs; -Proto*f=fs->f; -int oldsize=f->sizelocvars; -luaM_growvector(ls->L,f->locvars,fs->nlocvars,f->sizelocvars, -LocVar,SHRT_MAX,"too many local variables"); -while(oldsizesizelocvars)f->locvars[oldsize++].varname=NULL; -f->locvars[fs->nlocvars].varname=varname; -luaC_objbarrier(ls->L,f,varname); -return fs->nlocvars++; -} -#define new_localvarliteral(ls,v,n)new_localvar(ls,luaX_newstring(ls,""v,(sizeof(v)/sizeof(char))-1),n) -static void new_localvar(LexState*ls,TString*name,int n){ -FuncState*fs=ls->fs; -luaY_checklimit(fs,fs->nactvar+n+1,200,"local variables"); -fs->actvar[fs->nactvar+n]=cast(unsigned short,registerlocalvar(ls,name)); -} -static void adjustlocalvars(LexState*ls,int nvars){ -FuncState*fs=ls->fs; -fs->nactvar=cast_byte(fs->nactvar+nvars); -for(;nvars;nvars--){ -getlocvar(fs,fs->nactvar-nvars).startpc=fs->pc; -} -} -static void removevars(LexState*ls,int tolevel){ -FuncState*fs=ls->fs; -while(fs->nactvar>tolevel) -getlocvar(fs,--fs->nactvar).endpc=fs->pc; -} -static int indexupvalue(FuncState*fs,TString*name,expdesc*v){ -int i; -Proto*f=fs->f; -int oldsize=f->sizeupvalues; -for(i=0;inups;i++){ -if(fs->upvalues[i].k==v->k&&fs->upvalues[i].info==v->u.s.info){ -return i; -} -} -luaY_checklimit(fs,f->nups+1,60,"upvalues"); -luaM_growvector(fs->L,f->upvalues,f->nups,f->sizeupvalues, -TString*,(INT_MAX-2),""); -while(oldsizesizeupvalues)f->upvalues[oldsize++]=NULL; -f->upvalues[f->nups]=name; -luaC_objbarrier(fs->L,f,name); -fs->upvalues[f->nups].k=cast_byte(v->k); -fs->upvalues[f->nups].info=cast_byte(v->u.s.info); -return f->nups++; -} -static int searchvar(FuncState*fs,TString*n){ -int i; -for(i=fs->nactvar-1;i>=0;i--){ -if(n==getlocvar(fs,i).varname) -return i; -} -return-1; -} -static void markupval(FuncState*fs,int level){ -BlockCnt*bl=fs->bl; -while(bl&&bl->nactvar>level)bl=bl->previous; -if(bl)bl->upval=1; -} -static int singlevaraux(FuncState*fs,TString*n,expdesc*var,int base){ -if(fs==NULL){ -init_exp(var,VGLOBAL,((1<<8)-1)); -return VGLOBAL; -} -else{ -int v=searchvar(fs,n); -if(v>=0){ -init_exp(var,VLOCAL,v); -if(!base) -markupval(fs,v); -return VLOCAL; -} -else{ -if(singlevaraux(fs->prev,n,var,0)==VGLOBAL) -return VGLOBAL; -var->u.s.info=indexupvalue(fs,n,var); -var->k=VUPVAL; -return VUPVAL; -} -} -} -static void singlevar(LexState*ls,expdesc*var){ -TString*varname=str_checkname(ls); -FuncState*fs=ls->fs; -if(singlevaraux(fs,varname,var,1)==VGLOBAL) -var->u.s.info=luaK_stringK(fs,varname); -} -static void adjust_assign(LexState*ls,int nvars,int nexps,expdesc*e){ -FuncState*fs=ls->fs; -int extra=nvars-nexps; -if(hasmultret(e->k)){ -extra++; -if(extra<0)extra=0; -luaK_setreturns(fs,e,extra); -if(extra>1)luaK_reserveregs(fs,extra-1); -} -else{ -if(e->k!=VVOID)luaK_exp2nextreg(fs,e); -if(extra>0){ -int reg=fs->freereg; -luaK_reserveregs(fs,extra); -luaK_nil(fs,reg,extra); -} -} -} -static void enterlevel(LexState*ls){ -if(++ls->L->nCcalls>200) -luaX_lexerror(ls,"chunk has too many syntax levels",0); -} -#define leavelevel(ls)((ls)->L->nCcalls--) -static void enterblock(FuncState*fs,BlockCnt*bl,lu_byte isbreakable){ -bl->breaklist=(-1); -bl->isbreakable=isbreakable; -bl->nactvar=fs->nactvar; -bl->upval=0; -bl->previous=fs->bl; -fs->bl=bl; -} -static void leaveblock(FuncState*fs){ -BlockCnt*bl=fs->bl; -fs->bl=bl->previous; -removevars(fs->ls,bl->nactvar); -if(bl->upval) -luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0); -fs->freereg=fs->nactvar; -luaK_patchtohere(fs,bl->breaklist); -} -static void pushclosure(LexState*ls,FuncState*func,expdesc*v){ -FuncState*fs=ls->fs; -Proto*f=fs->f; -int oldsize=f->sizep; -int i; -luaM_growvector(ls->L,f->p,fs->np,f->sizep,Proto*, -((1<<(9+9))-1),"constant table overflow"); -while(oldsizesizep)f->p[oldsize++]=NULL; -f->p[fs->np++]=func->f; -luaC_objbarrier(ls->L,f,func->f); -init_exp(v,VRELOCABLE,luaK_codeABx(fs,OP_CLOSURE,0,fs->np-1)); -for(i=0;if->nups;i++){ -OpCode o=(func->upvalues[i].k==VLOCAL)?OP_MOVE:OP_GETUPVAL; -luaK_codeABC(fs,o,0,func->upvalues[i].info,0); -} -} -static void open_func(LexState*ls,FuncState*fs){ -lua_State*L=ls->L; -Proto*f=luaF_newproto(L); -fs->f=f; -fs->prev=ls->fs; -fs->ls=ls; -fs->L=L; -ls->fs=fs; -fs->pc=0; -fs->lasttarget=-1; -fs->jpc=(-1); -fs->freereg=0; -fs->nk=0; -fs->np=0; -fs->nlocvars=0; -fs->nactvar=0; -fs->bl=NULL; -f->source=ls->source; -f->maxstacksize=2; -fs->h=luaH_new(L,0,0); -sethvalue(L,L->top,fs->h); -incr_top(L); -setptvalue(L,L->top,f); -incr_top(L); -} -static void close_func(LexState*ls){ -lua_State*L=ls->L; -FuncState*fs=ls->fs; -Proto*f=fs->f; -removevars(ls,0); -luaK_ret(fs,0,0); -luaM_reallocvector(L,f->code,f->sizecode,fs->pc,Instruction); -f->sizecode=fs->pc; -luaM_reallocvector(L,f->lineinfo,f->sizelineinfo,fs->pc,int); -f->sizelineinfo=fs->pc; -luaM_reallocvector(L,f->k,f->sizek,fs->nk,TValue); -f->sizek=fs->nk; -luaM_reallocvector(L,f->p,f->sizep,fs->np,Proto*); -f->sizep=fs->np; -luaM_reallocvector(L,f->locvars,f->sizelocvars,fs->nlocvars,LocVar); -f->sizelocvars=fs->nlocvars; -luaM_reallocvector(L,f->upvalues,f->sizeupvalues,f->nups,TString*); -f->sizeupvalues=f->nups; -ls->fs=fs->prev; -if(fs)anchor_token(ls); -L->top-=2; -} -static Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff,const char*name){ -struct LexState lexstate; -struct FuncState funcstate; -lexstate.buff=buff; -luaX_setinput(L,&lexstate,z,luaS_new(L,name)); -open_func(&lexstate,&funcstate); -funcstate.f->is_vararg=2; -luaX_next(&lexstate); -chunk(&lexstate); -check(&lexstate,TK_EOS); -close_func(&lexstate); -return funcstate.f; -} -static void field(LexState*ls,expdesc*v){ -FuncState*fs=ls->fs; -expdesc key; -luaK_exp2anyreg(fs,v); -luaX_next(ls); -checkname(ls,&key); -luaK_indexed(fs,v,&key); -} -static void yindex(LexState*ls,expdesc*v){ -luaX_next(ls); -expr(ls,v); -luaK_exp2val(ls->fs,v); -checknext(ls,']'); -} -struct ConsControl{ -expdesc v; -expdesc*t; -int nh; -int na; -int tostore; -}; -static void recfield(LexState*ls,struct ConsControl*cc){ -FuncState*fs=ls->fs; -int reg=ls->fs->freereg; -expdesc key,val; -int rkkey; -if(ls->t.token==TK_NAME){ -luaY_checklimit(fs,cc->nh,(INT_MAX-2),"items in a constructor"); -checkname(ls,&key); -} -else -yindex(ls,&key); -cc->nh++; -checknext(ls,'='); -rkkey=luaK_exp2RK(fs,&key); -expr(ls,&val); -luaK_codeABC(fs,OP_SETTABLE,cc->t->u.s.info,rkkey,luaK_exp2RK(fs,&val)); -fs->freereg=reg; -} -static void closelistfield(FuncState*fs,struct ConsControl*cc){ -if(cc->v.k==VVOID)return; -luaK_exp2nextreg(fs,&cc->v); -cc->v.k=VVOID; -if(cc->tostore==50){ -luaK_setlist(fs,cc->t->u.s.info,cc->na,cc->tostore); -cc->tostore=0; -} -} -static void lastlistfield(FuncState*fs,struct ConsControl*cc){ -if(cc->tostore==0)return; -if(hasmultret(cc->v.k)){ -luaK_setmultret(fs,&cc->v); -luaK_setlist(fs,cc->t->u.s.info,cc->na,(-1)); -cc->na--; -} -else{ -if(cc->v.k!=VVOID) -luaK_exp2nextreg(fs,&cc->v); -luaK_setlist(fs,cc->t->u.s.info,cc->na,cc->tostore); -} -} -static void listfield(LexState*ls,struct ConsControl*cc){ -expr(ls,&cc->v); -luaY_checklimit(ls->fs,cc->na,(INT_MAX-2),"items in a constructor"); -cc->na++; -cc->tostore++; -} -static void constructor(LexState*ls,expdesc*t){ -FuncState*fs=ls->fs; -int line=ls->linenumber; -int pc=luaK_codeABC(fs,OP_NEWTABLE,0,0,0); -struct ConsControl cc; -cc.na=cc.nh=cc.tostore=0; -cc.t=t; -init_exp(t,VRELOCABLE,pc); -init_exp(&cc.v,VVOID,0); -luaK_exp2nextreg(ls->fs,t); -checknext(ls,'{'); -do{ -if(ls->t.token=='}')break; -closelistfield(fs,&cc); -switch(ls->t.token){ -case TK_NAME:{ -luaX_lookahead(ls); -if(ls->lookahead.token!='=') -listfield(ls,&cc); -else -recfield(ls,&cc); -break; -} -case'[':{ -recfield(ls,&cc); -break; -} -default:{ -listfield(ls,&cc); -break; -} -} -}while(testnext(ls,',')||testnext(ls,';')); -check_match(ls,'}','{',line); -lastlistfield(fs,&cc); -SETARG_B(fs->f->code[pc],luaO_int2fb(cc.na)); -SETARG_C(fs->f->code[pc],luaO_int2fb(cc.nh)); -} -static void parlist(LexState*ls){ -FuncState*fs=ls->fs; -Proto*f=fs->f; -int nparams=0; -f->is_vararg=0; -if(ls->t.token!=')'){ -do{ -switch(ls->t.token){ -case TK_NAME:{ -new_localvar(ls,str_checkname(ls),nparams++); -break; -} -case TK_DOTS:{ -luaX_next(ls); -f->is_vararg|=2; -break; -} -default:luaX_syntaxerror(ls," or "LUA_QL("...")" expected"); -} -}while(!f->is_vararg&&testnext(ls,',')); -} -adjustlocalvars(ls,nparams); -f->numparams=cast_byte(fs->nactvar-(f->is_vararg&1)); -luaK_reserveregs(fs,fs->nactvar); -} -static void body(LexState*ls,expdesc*e,int needself,int line){ -FuncState new_fs; -open_func(ls,&new_fs); -new_fs.f->linedefined=line; -checknext(ls,'('); -if(needself){ -new_localvarliteral(ls,"self",0); -adjustlocalvars(ls,1); -} -parlist(ls); -checknext(ls,')'); -chunk(ls); -new_fs.f->lastlinedefined=ls->linenumber; -check_match(ls,TK_END,TK_FUNCTION,line); -close_func(ls); -pushclosure(ls,&new_fs,e); -} -static int explist1(LexState*ls,expdesc*v){ -int n=1; -expr(ls,v); -while(testnext(ls,',')){ -luaK_exp2nextreg(ls->fs,v); -expr(ls,v); -n++; -} -return n; -} -static void funcargs(LexState*ls,expdesc*f){ -FuncState*fs=ls->fs; -expdesc args; -int base,nparams; -int line=ls->linenumber; -switch(ls->t.token){ -case'(':{ -if(line!=ls->lastline) -luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); -luaX_next(ls); -if(ls->t.token==')') -args.k=VVOID; -else{ -explist1(ls,&args); -luaK_setmultret(fs,&args); -} -check_match(ls,')','(',line); -break; -} -case'{':{ -constructor(ls,&args); -break; -} -case TK_STRING:{ -codestring(ls,&args,ls->t.seminfo.ts); -luaX_next(ls); -break; -} -default:{ -luaX_syntaxerror(ls,"function arguments expected"); -return; -} -} -base=f->u.s.info; -if(hasmultret(args.k)) -nparams=(-1); -else{ -if(args.k!=VVOID) -luaK_exp2nextreg(fs,&args); -nparams=fs->freereg-(base+1); -} -init_exp(f,VCALL,luaK_codeABC(fs,OP_CALL,base,nparams+1,2)); -luaK_fixline(fs,line); -fs->freereg=base+1; -} -static void prefixexp(LexState*ls,expdesc*v){ -switch(ls->t.token){ -case'(':{ -int line=ls->linenumber; -luaX_next(ls); -expr(ls,v); -check_match(ls,')','(',line); -luaK_dischargevars(ls->fs,v); -return; -} -case TK_NAME:{ -singlevar(ls,v); -return; -} -default:{ -luaX_syntaxerror(ls,"unexpected symbol"); -return; -} -} -} -static void primaryexp(LexState*ls,expdesc*v){ -FuncState*fs=ls->fs; -prefixexp(ls,v); -for(;;){ -switch(ls->t.token){ -case'.':{ -field(ls,v); -break; -} -case'[':{ -expdesc key; -luaK_exp2anyreg(fs,v); -yindex(ls,&key); -luaK_indexed(fs,v,&key); -break; -} -case':':{ -expdesc key; -luaX_next(ls); -checkname(ls,&key); -luaK_self(fs,v,&key); -funcargs(ls,v); -break; -} -case'(':case TK_STRING:case'{':{ -luaK_exp2nextreg(fs,v); -funcargs(ls,v); -break; -} -default:return; -} -} -} -static void simpleexp(LexState*ls,expdesc*v){ -switch(ls->t.token){ -case TK_NUMBER:{ -init_exp(v,VKNUM,0); -v->u.nval=ls->t.seminfo.r; -break; -} -case TK_STRING:{ -codestring(ls,v,ls->t.seminfo.ts); -break; -} -case TK_NIL:{ -init_exp(v,VNIL,0); -break; -} -case TK_TRUE:{ -init_exp(v,VTRUE,0); -break; -} -case TK_FALSE:{ -init_exp(v,VFALSE,0); -break; -} -case TK_DOTS:{ -FuncState*fs=ls->fs; -check_condition(ls,fs->f->is_vararg, -"cannot use "LUA_QL("...")" outside a vararg function"); -fs->f->is_vararg&=~4; -init_exp(v,VVARARG,luaK_codeABC(fs,OP_VARARG,0,1,0)); -break; -} -case'{':{ -constructor(ls,v); -return; -} -case TK_FUNCTION:{ -luaX_next(ls); -body(ls,v,0,ls->linenumber); -return; -} -default:{ -primaryexp(ls,v); -return; -} -} -luaX_next(ls); -} -static UnOpr getunopr(int op){ -switch(op){ -case TK_NOT:return OPR_NOT; -case'-':return OPR_MINUS; -case'#':return OPR_LEN; -default:return OPR_NOUNOPR; -} -} -static BinOpr getbinopr(int op){ -switch(op){ -case'+':return OPR_ADD; -case'-':return OPR_SUB; -case'*':return OPR_MUL; -case'/':return OPR_DIV; -case'%':return OPR_MOD; -case'^':return OPR_POW; -case TK_CONCAT:return OPR_CONCAT; -case TK_NE:return OPR_NE; -case TK_EQ:return OPR_EQ; -case'<':return OPR_LT; -case TK_LE:return OPR_LE; -case'>':return OPR_GT; -case TK_GE:return OPR_GE; -case TK_AND:return OPR_AND; -case TK_OR:return OPR_OR; -default:return OPR_NOBINOPR; -} -} -static const struct{ -lu_byte left; -lu_byte right; -}priority[]={ -{6,6},{6,6},{7,7},{7,7},{7,7}, -{10,9},{5,4}, -{3,3},{3,3}, -{3,3},{3,3},{3,3},{3,3}, -{2,2},{1,1} -}; -static BinOpr subexpr(LexState*ls,expdesc*v,unsigned int limit){ -BinOpr op; -UnOpr uop; -enterlevel(ls); -uop=getunopr(ls->t.token); -if(uop!=OPR_NOUNOPR){ -luaX_next(ls); -subexpr(ls,v,8); -luaK_prefix(ls->fs,uop,v); -} -else simpleexp(ls,v); -op=getbinopr(ls->t.token); -while(op!=OPR_NOBINOPR&&priority[op].left>limit){ -expdesc v2; -BinOpr nextop; -luaX_next(ls); -luaK_infix(ls->fs,op,v); -nextop=subexpr(ls,&v2,priority[op].right); -luaK_posfix(ls->fs,op,v,&v2); -op=nextop; -} -leavelevel(ls); -return op; -} -static void expr(LexState*ls,expdesc*v){ -subexpr(ls,v,0); -} -static int block_follow(int token){ -switch(token){ -case TK_ELSE:case TK_ELSEIF:case TK_END: -case TK_UNTIL:case TK_EOS: -return 1; -default:return 0; -} -} -static void block(LexState*ls){ -FuncState*fs=ls->fs; -BlockCnt bl; -enterblock(fs,&bl,0); -chunk(ls); -leaveblock(fs); -} -struct LHS_assign{ -struct LHS_assign*prev; -expdesc v; -}; -static void check_conflict(LexState*ls,struct LHS_assign*lh,expdesc*v){ -FuncState*fs=ls->fs; -int extra=fs->freereg; -int conflict=0; -for(;lh;lh=lh->prev){ -if(lh->v.k==VINDEXED){ -if(lh->v.u.s.info==v->u.s.info){ -conflict=1; -lh->v.u.s.info=extra; -} -if(lh->v.u.s.aux==v->u.s.info){ -conflict=1; -lh->v.u.s.aux=extra; -} -} -} -if(conflict){ -luaK_codeABC(fs,OP_MOVE,fs->freereg,v->u.s.info,0); -luaK_reserveregs(fs,1); -} -} -static void assignment(LexState*ls,struct LHS_assign*lh,int nvars){ -expdesc e; -check_condition(ls,VLOCAL<=lh->v.k&&lh->v.k<=VINDEXED, -"syntax error"); -if(testnext(ls,',')){ -struct LHS_assign nv; -nv.prev=lh; -primaryexp(ls,&nv.v); -if(nv.v.k==VLOCAL) -check_conflict(ls,lh,&nv.v); -luaY_checklimit(ls->fs,nvars,200-ls->L->nCcalls, -"variables in assignment"); -assignment(ls,&nv,nvars+1); -} -else{ -int nexps; -checknext(ls,'='); -nexps=explist1(ls,&e); -if(nexps!=nvars){ -adjust_assign(ls,nvars,nexps,&e); -if(nexps>nvars) -ls->fs->freereg-=nexps-nvars; -} -else{ -luaK_setoneret(ls->fs,&e); -luaK_storevar(ls->fs,&lh->v,&e); -return; -} -} -init_exp(&e,VNONRELOC,ls->fs->freereg-1); -luaK_storevar(ls->fs,&lh->v,&e); -} -static int cond(LexState*ls){ -expdesc v; -expr(ls,&v); -if(v.k==VNIL)v.k=VFALSE; -luaK_goiftrue(ls->fs,&v); -return v.f; -} -static void breakstat(LexState*ls){ -FuncState*fs=ls->fs; -BlockCnt*bl=fs->bl; -int upval=0; -while(bl&&!bl->isbreakable){ -upval|=bl->upval; -bl=bl->previous; -} -if(!bl) -luaX_syntaxerror(ls,"no loop to break"); -if(upval) -luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0); -luaK_concat(fs,&bl->breaklist,luaK_jump(fs)); -} -static void whilestat(LexState*ls,int line){ -FuncState*fs=ls->fs; -int whileinit; -int condexit; -BlockCnt bl; -luaX_next(ls); -whileinit=luaK_getlabel(fs); -condexit=cond(ls); -enterblock(fs,&bl,1); -checknext(ls,TK_DO); -block(ls); -luaK_patchlist(fs,luaK_jump(fs),whileinit); -check_match(ls,TK_END,TK_WHILE,line); -leaveblock(fs); -luaK_patchtohere(fs,condexit); -} -static void repeatstat(LexState*ls,int line){ -int condexit; -FuncState*fs=ls->fs; -int repeat_init=luaK_getlabel(fs); -BlockCnt bl1,bl2; -enterblock(fs,&bl1,1); -enterblock(fs,&bl2,0); -luaX_next(ls); -chunk(ls); -check_match(ls,TK_UNTIL,TK_REPEAT,line); -condexit=cond(ls); -if(!bl2.upval){ -leaveblock(fs); -luaK_patchlist(ls->fs,condexit,repeat_init); -} -else{ -breakstat(ls); -luaK_patchtohere(ls->fs,condexit); -leaveblock(fs); -luaK_patchlist(ls->fs,luaK_jump(fs),repeat_init); -} -leaveblock(fs); -} -static int exp1(LexState*ls){ -expdesc e; -int k; -expr(ls,&e); -k=e.k; -luaK_exp2nextreg(ls->fs,&e); -return k; -} -static void forbody(LexState*ls,int base,int line,int nvars,int isnum){ -BlockCnt bl; -FuncState*fs=ls->fs; -int prep,endfor; -adjustlocalvars(ls,3); -checknext(ls,TK_DO); -prep=isnum?luaK_codeAsBx(fs,OP_FORPREP,base,(-1)):luaK_jump(fs); -enterblock(fs,&bl,0); -adjustlocalvars(ls,nvars); -luaK_reserveregs(fs,nvars); -block(ls); -leaveblock(fs); -luaK_patchtohere(fs,prep); -endfor=(isnum)?luaK_codeAsBx(fs,OP_FORLOOP,base,(-1)): -luaK_codeABC(fs,OP_TFORLOOP,base,0,nvars); -luaK_fixline(fs,line); -luaK_patchlist(fs,(isnum?endfor:luaK_jump(fs)),prep+1); -} -static void fornum(LexState*ls,TString*varname,int line){ -FuncState*fs=ls->fs; -int base=fs->freereg; -new_localvarliteral(ls,"(for index)",0); -new_localvarliteral(ls,"(for limit)",1); -new_localvarliteral(ls,"(for step)",2); -new_localvar(ls,varname,3); -checknext(ls,'='); -exp1(ls); -checknext(ls,','); -exp1(ls); -if(testnext(ls,',')) -exp1(ls); -else{ -luaK_codeABx(fs,OP_LOADK,fs->freereg,luaK_numberK(fs,1)); -luaK_reserveregs(fs,1); -} -forbody(ls,base,line,1,1); -} -static void forlist(LexState*ls,TString*indexname){ -FuncState*fs=ls->fs; -expdesc e; -int nvars=0; -int line; -int base=fs->freereg; -new_localvarliteral(ls,"(for generator)",nvars++); -new_localvarliteral(ls,"(for state)",nvars++); -new_localvarliteral(ls,"(for control)",nvars++); -new_localvar(ls,indexname,nvars++); -while(testnext(ls,',')) -new_localvar(ls,str_checkname(ls),nvars++); -checknext(ls,TK_IN); -line=ls->linenumber; -adjust_assign(ls,3,explist1(ls,&e),&e); -luaK_checkstack(fs,3); -forbody(ls,base,line,nvars-3,0); -} -static void forstat(LexState*ls,int line){ -FuncState*fs=ls->fs; -TString*varname; -BlockCnt bl; -enterblock(fs,&bl,1); -luaX_next(ls); -varname=str_checkname(ls); -switch(ls->t.token){ -case'=':fornum(ls,varname,line);break; -case',':case TK_IN:forlist(ls,varname);break; -default:luaX_syntaxerror(ls,LUA_QL("=")" or "LUA_QL("in")" expected"); -} -check_match(ls,TK_END,TK_FOR,line); -leaveblock(fs); -} -static int test_then_block(LexState*ls){ -int condexit; -luaX_next(ls); -condexit=cond(ls); -checknext(ls,TK_THEN); -block(ls); -return condexit; -} -static void ifstat(LexState*ls,int line){ -FuncState*fs=ls->fs; -int flist; -int escapelist=(-1); -flist=test_then_block(ls); -while(ls->t.token==TK_ELSEIF){ -luaK_concat(fs,&escapelist,luaK_jump(fs)); -luaK_patchtohere(fs,flist); -flist=test_then_block(ls); -} -if(ls->t.token==TK_ELSE){ -luaK_concat(fs,&escapelist,luaK_jump(fs)); -luaK_patchtohere(fs,flist); -luaX_next(ls); -block(ls); -} -else -luaK_concat(fs,&escapelist,flist); -luaK_patchtohere(fs,escapelist); -check_match(ls,TK_END,TK_IF,line); -} -static void localfunc(LexState*ls){ -expdesc v,b; -FuncState*fs=ls->fs; -new_localvar(ls,str_checkname(ls),0); -init_exp(&v,VLOCAL,fs->freereg); -luaK_reserveregs(fs,1); -adjustlocalvars(ls,1); -body(ls,&b,0,ls->linenumber); -luaK_storevar(fs,&v,&b); -getlocvar(fs,fs->nactvar-1).startpc=fs->pc; -} -static void localstat(LexState*ls){ -int nvars=0; -int nexps; -expdesc e; -do{ -new_localvar(ls,str_checkname(ls),nvars++); -}while(testnext(ls,',')); -if(testnext(ls,'=')) -nexps=explist1(ls,&e); -else{ -e.k=VVOID; -nexps=0; -} -adjust_assign(ls,nvars,nexps,&e); -adjustlocalvars(ls,nvars); -} -static int funcname(LexState*ls,expdesc*v){ -int needself=0; -singlevar(ls,v); -while(ls->t.token=='.') -field(ls,v); -if(ls->t.token==':'){ -needself=1; -field(ls,v); -} -return needself; -} -static void funcstat(LexState*ls,int line){ -int needself; -expdesc v,b; -luaX_next(ls); -needself=funcname(ls,&v); -body(ls,&b,needself,line); -luaK_storevar(ls->fs,&v,&b); -luaK_fixline(ls->fs,line); -} -static void exprstat(LexState*ls){ -FuncState*fs=ls->fs; -struct LHS_assign v; -primaryexp(ls,&v.v); -if(v.v.k==VCALL) -SETARG_C(getcode(fs,&v.v),1); -else{ -v.prev=NULL; -assignment(ls,&v,1); -} -} -static void retstat(LexState*ls){ -FuncState*fs=ls->fs; -expdesc e; -int first,nret; -luaX_next(ls); -if(block_follow(ls->t.token)||ls->t.token==';') -first=nret=0; -else{ -nret=explist1(ls,&e); -if(hasmultret(e.k)){ -luaK_setmultret(fs,&e); -if(e.k==VCALL&&nret==1){ -SET_OPCODE(getcode(fs,&e),OP_TAILCALL); -} -first=fs->nactvar; -nret=(-1); -} -else{ -if(nret==1) -first=luaK_exp2anyreg(fs,&e); -else{ -luaK_exp2nextreg(fs,&e); -first=fs->nactvar; -} -} -} -luaK_ret(fs,first,nret); -} -static int statement(LexState*ls){ -int line=ls->linenumber; -switch(ls->t.token){ -case TK_IF:{ -ifstat(ls,line); -return 0; -} -case TK_WHILE:{ -whilestat(ls,line); -return 0; -} -case TK_DO:{ -luaX_next(ls); -block(ls); -check_match(ls,TK_END,TK_DO,line); -return 0; -} -case TK_FOR:{ -forstat(ls,line); -return 0; -} -case TK_REPEAT:{ -repeatstat(ls,line); -return 0; -} -case TK_FUNCTION:{ -funcstat(ls,line); -return 0; -} -case TK_LOCAL:{ -luaX_next(ls); -if(testnext(ls,TK_FUNCTION)) -localfunc(ls); -else -localstat(ls); -return 0; -} -case TK_RETURN:{ -retstat(ls); -return 1; -} -case TK_BREAK:{ -luaX_next(ls); -breakstat(ls); -return 1; -} -default:{ -exprstat(ls); -return 0; -} -} -} -static void chunk(LexState*ls){ -int islast=0; -enterlevel(ls); -while(!islast&&!block_follow(ls->t.token)){ -islast=statement(ls); -testnext(ls,';'); -ls->fs->freereg=ls->fs->nactvar; -} -leavelevel(ls); -} -static const TValue*luaV_tonumber(const TValue*obj,TValue*n){ -lua_Number num; -if(ttisnumber(obj))return obj; -if(ttisstring(obj)&&luaO_str2d(svalue(obj),&num)){ -setnvalue(n,num); -return n; -} -else -return NULL; -} -static int luaV_tostring(lua_State*L,StkId obj){ -if(!ttisnumber(obj)) -return 0; -else{ -char s[32]; -lua_Number n=nvalue(obj); -lua_number2str(s,n); -setsvalue(L,obj,luaS_new(L,s)); -return 1; -} -} -static void callTMres(lua_State*L,StkId res,const TValue*f, -const TValue*p1,const TValue*p2){ -ptrdiff_t result=savestack(L,res); -setobj(L,L->top,f); -setobj(L,L->top+1,p1); -setobj(L,L->top+2,p2); -luaD_checkstack(L,3); -L->top+=3; -luaD_call(L,L->top-3,1); -res=restorestack(L,result); -L->top--; -setobj(L,res,L->top); -} -static void callTM(lua_State*L,const TValue*f,const TValue*p1, -const TValue*p2,const TValue*p3){ -setobj(L,L->top,f); -setobj(L,L->top+1,p1); -setobj(L,L->top+2,p2); -setobj(L,L->top+3,p3); -luaD_checkstack(L,4); -L->top+=4; -luaD_call(L,L->top-4,0); -} -static void luaV_gettable(lua_State*L,const TValue*t,TValue*key,StkId val){ -int loop; -for(loop=0;loop<100;loop++){ -const TValue*tm; -if(ttistable(t)){ -Table*h=hvalue(t); -const TValue*res=luaH_get(h,key); -if(!ttisnil(res)|| -(tm=fasttm(L,h->metatable,TM_INDEX))==NULL){ -setobj(L,val,res); -return; -} -} -else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_INDEX))) -luaG_typeerror(L,t,"index"); -if(ttisfunction(tm)){ -callTMres(L,val,tm,t,key); -return; -} -t=tm; -} -luaG_runerror(L,"loop in gettable"); -} -static void luaV_settable(lua_State*L,const TValue*t,TValue*key,StkId val){ -int loop; -TValue temp; -for(loop=0;loop<100;loop++){ -const TValue*tm; -if(ttistable(t)){ -Table*h=hvalue(t); -TValue*oldval=luaH_set(L,h,key); -if(!ttisnil(oldval)|| -(tm=fasttm(L,h->metatable,TM_NEWINDEX))==NULL){ -setobj(L,oldval,val); -h->flags=0; -luaC_barriert(L,h,val); -return; -} -} -else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_NEWINDEX))) -luaG_typeerror(L,t,"index"); -if(ttisfunction(tm)){ -callTM(L,tm,t,key,val); -return; -} -setobj(L,&temp,tm); -t=&temp; -} -luaG_runerror(L,"loop in settable"); -} -static int call_binTM(lua_State*L,const TValue*p1,const TValue*p2, -StkId res,TMS event){ -const TValue*tm=luaT_gettmbyobj(L,p1,event); -if(ttisnil(tm)) -tm=luaT_gettmbyobj(L,p2,event); -if(ttisnil(tm))return 0; -callTMres(L,res,tm,p1,p2); -return 1; -} -static const TValue*get_compTM(lua_State*L,Table*mt1,Table*mt2, -TMS event){ -const TValue*tm1=fasttm(L,mt1,event); -const TValue*tm2; -if(tm1==NULL)return NULL; -if(mt1==mt2)return tm1; -tm2=fasttm(L,mt2,event); -if(tm2==NULL)return NULL; -if(luaO_rawequalObj(tm1,tm2)) -return tm1; -return NULL; -} -static int call_orderTM(lua_State*L,const TValue*p1,const TValue*p2, -TMS event){ -const TValue*tm1=luaT_gettmbyobj(L,p1,event); -const TValue*tm2; -if(ttisnil(tm1))return-1; -tm2=luaT_gettmbyobj(L,p2,event); -if(!luaO_rawequalObj(tm1,tm2)) -return-1; -callTMres(L,L->top,tm1,p1,p2); -return!l_isfalse(L->top); -} -static int l_strcmp(const TString*ls,const TString*rs){ -const char*l=getstr(ls); -size_t ll=ls->tsv.len; -const char*r=getstr(rs); -size_t lr=rs->tsv.len; -for(;;){ -int temp=strcoll(l,r); -if(temp!=0)return temp; -else{ -size_t len=strlen(l); -if(len==lr) -return(len==ll)?0:1; -else if(len==ll) -return-1; -len++; -l+=len;ll-=len;r+=len;lr-=len; -} -} -} -static int luaV_lessthan(lua_State*L,const TValue*l,const TValue*r){ -int res; -if(ttype(l)!=ttype(r)) -return luaG_ordererror(L,l,r); -else if(ttisnumber(l)) -return luai_numlt(nvalue(l),nvalue(r)); -else if(ttisstring(l)) -return l_strcmp(rawtsvalue(l),rawtsvalue(r))<0; -else if((res=call_orderTM(L,l,r,TM_LT))!=-1) -return res; -return luaG_ordererror(L,l,r); -} -static int lessequal(lua_State*L,const TValue*l,const TValue*r){ -int res; -if(ttype(l)!=ttype(r)) -return luaG_ordererror(L,l,r); -else if(ttisnumber(l)) -return luai_numle(nvalue(l),nvalue(r)); -else if(ttisstring(l)) -return l_strcmp(rawtsvalue(l),rawtsvalue(r))<=0; -else if((res=call_orderTM(L,l,r,TM_LE))!=-1) -return res; -else if((res=call_orderTM(L,r,l,TM_LT))!=-1) -return!res; -return luaG_ordererror(L,l,r); -} -static int luaV_equalval(lua_State*L,const TValue*t1,const TValue*t2){ -const TValue*tm; -switch(ttype(t1)){ -case 0:return 1; -case 3:return luai_numeq(nvalue(t1),nvalue(t2)); -case 1:return bvalue(t1)==bvalue(t2); -case 2:return pvalue(t1)==pvalue(t2); -case 7:{ -if(uvalue(t1)==uvalue(t2))return 1; -tm=get_compTM(L,uvalue(t1)->metatable,uvalue(t2)->metatable, -TM_EQ); -break; -} -case 5:{ -if(hvalue(t1)==hvalue(t2))return 1; -tm=get_compTM(L,hvalue(t1)->metatable,hvalue(t2)->metatable,TM_EQ); -break; -} -default:return gcvalue(t1)==gcvalue(t2); -} -if(tm==NULL)return 0; -callTMres(L,L->top,tm,t1,t2); -return!l_isfalse(L->top); -} -static void luaV_concat(lua_State*L,int total,int last){ -do{ -StkId top=L->base+last+1; -int n=2; -if(!(ttisstring(top-2)||ttisnumber(top-2))||!tostring(L,top-1)){ -if(!call_binTM(L,top-2,top-1,top-2,TM_CONCAT)) -luaG_concaterror(L,top-2,top-1); -}else if(tsvalue(top-1)->len==0) -(void)tostring(L,top-2); -else{ -size_t tl=tsvalue(top-1)->len; -char*buffer; -int i; -for(n=1;nlen; -if(l>=((size_t)(~(size_t)0)-2)-tl)luaG_runerror(L,"string length overflow"); -tl+=l; -} -buffer=luaZ_openspace(L,&G(L)->buff,tl); -tl=0; -for(i=n;i>0;i--){ -size_t l=tsvalue(top-i)->len; -memcpy(buffer+tl,svalue(top-i),l); -tl+=l; -} -setsvalue(L,top-n,luaS_newlstr(L,buffer,tl)); -} -total-=n-1; -last-=n-1; -}while(total>1); -} -static void Arith(lua_State*L,StkId ra,const TValue*rb, -const TValue*rc,TMS op){ -TValue tempb,tempc; -const TValue*b,*c; -if((b=luaV_tonumber(rb,&tempb))!=NULL&& -(c=luaV_tonumber(rc,&tempc))!=NULL){ -lua_Number nb=nvalue(b),nc=nvalue(c); -switch(op){ -case TM_ADD:setnvalue(ra,luai_numadd(nb,nc));break; -case TM_SUB:setnvalue(ra,luai_numsub(nb,nc));break; -case TM_MUL:setnvalue(ra,luai_nummul(nb,nc));break; -case TM_DIV:setnvalue(ra,luai_numdiv(nb,nc));break; -case TM_MOD:setnvalue(ra,luai_nummod(nb,nc));break; -case TM_POW:setnvalue(ra,luai_numpow(nb,nc));break; -case TM_UNM:setnvalue(ra,luai_numunm(nb));break; -default:break; -} -} -else if(!call_binTM(L,rb,rc,ra,op)) -luaG_aritherror(L,rb,rc); -} -#define runtime_check(L,c){if(!(c))break;} -#define RA(i)(base+GETARG_A(i)) -#define RB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgR,base+GETARG_B(i)) -#define RKB(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_B(i))?k+INDEXK(GETARG_B(i)):base+GETARG_B(i)) -#define RKC(i)check_exp(getCMode(GET_OPCODE(i))==OpArgK,ISK(GETARG_C(i))?k+INDEXK(GETARG_C(i)):base+GETARG_C(i)) -#define KBx(i)check_exp(getBMode(GET_OPCODE(i))==OpArgK,k+GETARG_Bx(i)) -#define dojump(L,pc,i){(pc)+=(i);} -#define Protect(x){L->savedpc=pc;{x;};base=L->base;} -#define arith_op(op,tm){TValue*rb=RKB(i);TValue*rc=RKC(i);if(ttisnumber(rb)&&ttisnumber(rc)){lua_Number nb=nvalue(rb),nc=nvalue(rc);setnvalue(ra,op(nb,nc));}else Protect(Arith(L,ra,rb,rc,tm));} -static void luaV_execute(lua_State*L,int nexeccalls){ -LClosure*cl; -StkId base; -TValue*k; -const Instruction*pc; -reentry: -pc=L->savedpc; -cl=&clvalue(L->ci->func)->l; -base=L->base; -k=cl->p->k; -for(;;){ -const Instruction i=*pc++; -StkId ra; -ra=RA(i); -switch(GET_OPCODE(i)){ -case OP_MOVE:{ -setobj(L,ra,RB(i)); -continue; -} -case OP_LOADK:{ -setobj(L,ra,KBx(i)); -continue; -} -case OP_LOADBOOL:{ -setbvalue(ra,GETARG_B(i)); -if(GETARG_C(i))pc++; -continue; -} -case OP_LOADNIL:{ -TValue*rb=RB(i); -do{ -setnilvalue(rb--); -}while(rb>=ra); -continue; -} -case OP_GETUPVAL:{ -int b=GETARG_B(i); -setobj(L,ra,cl->upvals[b]->v); -continue; -} -case OP_GETGLOBAL:{ -TValue g; -TValue*rb=KBx(i); -sethvalue(L,&g,cl->env); -Protect(luaV_gettable(L,&g,rb,ra)); -continue; -} -case OP_GETTABLE:{ -Protect(luaV_gettable(L,RB(i),RKC(i),ra)); -continue; -} -case OP_SETGLOBAL:{ -TValue g; -sethvalue(L,&g,cl->env); -Protect(luaV_settable(L,&g,KBx(i),ra)); -continue; -} -case OP_SETUPVAL:{ -UpVal*uv=cl->upvals[GETARG_B(i)]; -setobj(L,uv->v,ra); -luaC_barrier(L,uv,ra); -continue; -} -case OP_SETTABLE:{ -Protect(luaV_settable(L,ra,RKB(i),RKC(i))); -continue; -} -case OP_NEWTABLE:{ -int b=GETARG_B(i); -int c=GETARG_C(i); -sethvalue(L,ra,luaH_new(L,luaO_fb2int(b),luaO_fb2int(c))); -Protect(luaC_checkGC(L)); -continue; -} -case OP_SELF:{ -StkId rb=RB(i); -setobj(L,ra+1,rb); -Protect(luaV_gettable(L,rb,RKC(i),ra)); -continue; -} -case OP_ADD:{ -arith_op(luai_numadd,TM_ADD); -continue; -} -case OP_SUB:{ -arith_op(luai_numsub,TM_SUB); -continue; -} -case OP_MUL:{ -arith_op(luai_nummul,TM_MUL); -continue; -} -case OP_DIV:{ -arith_op(luai_numdiv,TM_DIV); -continue; -} -case OP_MOD:{ -arith_op(luai_nummod,TM_MOD); -continue; -} -case OP_POW:{ -arith_op(luai_numpow,TM_POW); -continue; -} -case OP_UNM:{ -TValue*rb=RB(i); -if(ttisnumber(rb)){ -lua_Number nb=nvalue(rb); -setnvalue(ra,luai_numunm(nb)); -} -else{ -Protect(Arith(L,ra,rb,rb,TM_UNM)); -} -continue; -} -case OP_NOT:{ -int res=l_isfalse(RB(i)); -setbvalue(ra,res); -continue; -} -case OP_LEN:{ -const TValue*rb=RB(i); -switch(ttype(rb)){ -case 5:{ -setnvalue(ra,cast_num(luaH_getn(hvalue(rb)))); -break; -} -case 4:{ -setnvalue(ra,cast_num(tsvalue(rb)->len)); -break; -} -default:{ -Protect( -if(!call_binTM(L,rb,(&luaO_nilobject_),ra,TM_LEN)) -luaG_typeerror(L,rb,"get length of"); -) -} -} -continue; -} -case OP_CONCAT:{ -int b=GETARG_B(i); -int c=GETARG_C(i); -Protect(luaV_concat(L,c-b+1,c);luaC_checkGC(L)); -setobj(L,RA(i),base+b); -continue; -} -case OP_JMP:{ -dojump(L,pc,GETARG_sBx(i)); -continue; -} -case OP_EQ:{ -TValue*rb=RKB(i); -TValue*rc=RKC(i); -Protect( -if(equalobj(L,rb,rc)==GETARG_A(i)) -dojump(L,pc,GETARG_sBx(*pc)); -) -pc++; -continue; -} -case OP_LT:{ -Protect( -if(luaV_lessthan(L,RKB(i),RKC(i))==GETARG_A(i)) -dojump(L,pc,GETARG_sBx(*pc)); -) -pc++; -continue; -} -case OP_LE:{ -Protect( -if(lessequal(L,RKB(i),RKC(i))==GETARG_A(i)) -dojump(L,pc,GETARG_sBx(*pc)); -) -pc++; -continue; -} -case OP_TEST:{ -if(l_isfalse(ra)!=GETARG_C(i)) -dojump(L,pc,GETARG_sBx(*pc)); -pc++; -continue; -} -case OP_TESTSET:{ -TValue*rb=RB(i); -if(l_isfalse(rb)!=GETARG_C(i)){ -setobj(L,ra,rb); -dojump(L,pc,GETARG_sBx(*pc)); -} -pc++; -continue; -} -case OP_CALL:{ -int b=GETARG_B(i); -int nresults=GETARG_C(i)-1; -if(b!=0)L->top=ra+b; -L->savedpc=pc; -switch(luaD_precall(L,ra,nresults)){ -case 0:{ -nexeccalls++; -goto reentry; -} -case 1:{ -if(nresults>=0)L->top=L->ci->top; -base=L->base; -continue; -} -default:{ -return; -} -} -} -case OP_TAILCALL:{ -int b=GETARG_B(i); -if(b!=0)L->top=ra+b; -L->savedpc=pc; -switch(luaD_precall(L,ra,(-1))){ -case 0:{ -CallInfo*ci=L->ci-1; -int aux; -StkId func=ci->func; -StkId pfunc=(ci+1)->func; -if(L->openupval)luaF_close(L,ci->base); -L->base=ci->base=ci->func+((ci+1)->base-pfunc); -for(aux=0;pfunc+auxtop;aux++) -setobj(L,func+aux,pfunc+aux); -ci->top=L->top=func+aux; -ci->savedpc=L->savedpc; -ci->tailcalls++; -L->ci--; -goto reentry; -} -case 1:{ -base=L->base; -continue; -} -default:{ -return; -} -} -} -case OP_RETURN:{ -int b=GETARG_B(i); -if(b!=0)L->top=ra+b-1; -if(L->openupval)luaF_close(L,base); -L->savedpc=pc; -b=luaD_poscall(L,ra); -if(--nexeccalls==0) -return; -else{ -if(b)L->top=L->ci->top; -goto reentry; -} -} -case OP_FORLOOP:{ -lua_Number step=nvalue(ra+2); -lua_Number idx=luai_numadd(nvalue(ra),step); -lua_Number limit=nvalue(ra+1); -if(luai_numlt(0,step)?luai_numle(idx,limit) -:luai_numle(limit,idx)){ -dojump(L,pc,GETARG_sBx(i)); -setnvalue(ra,idx); -setnvalue(ra+3,idx); -} -continue; -} -case OP_FORPREP:{ -const TValue*init=ra; -const TValue*plimit=ra+1; -const TValue*pstep=ra+2; -L->savedpc=pc; -if(!tonumber(init,ra)) -luaG_runerror(L,LUA_QL("for")" initial value must be a number"); -else if(!tonumber(plimit,ra+1)) -luaG_runerror(L,LUA_QL("for")" limit must be a number"); -else if(!tonumber(pstep,ra+2)) -luaG_runerror(L,LUA_QL("for")" step must be a number"); -setnvalue(ra,luai_numsub(nvalue(ra),nvalue(pstep))); -dojump(L,pc,GETARG_sBx(i)); -continue; -} -case OP_TFORLOOP:{ -StkId cb=ra+3; -setobj(L,cb+2,ra+2); -setobj(L,cb+1,ra+1); -setobj(L,cb,ra); -L->top=cb+3; -Protect(luaD_call(L,cb,GETARG_C(i))); -L->top=L->ci->top; -cb=RA(i)+3; -if(!ttisnil(cb)){ -setobj(L,cb-1,cb); -dojump(L,pc,GETARG_sBx(*pc)); -} -pc++; -continue; -} -case OP_SETLIST:{ -int n=GETARG_B(i); -int c=GETARG_C(i); -int last; -Table*h; -if(n==0){ -n=cast_int(L->top-ra)-1; -L->top=L->ci->top; -} -if(c==0)c=cast_int(*pc++); -runtime_check(L,ttistable(ra)); -h=hvalue(ra); -last=((c-1)*50)+n; -if(last>h->sizearray) -luaH_resizearray(L,h,last); -for(;n>0;n--){ -TValue*val=ra+n; -setobj(L,luaH_setnum(L,h,last--),val); -luaC_barriert(L,h,val); -} -continue; -} -case OP_CLOSE:{ -luaF_close(L,ra); -continue; -} -case OP_CLOSURE:{ -Proto*p; -Closure*ncl; -int nup,j; -p=cl->p->p[GETARG_Bx(i)]; -nup=p->nups; -ncl=luaF_newLclosure(L,nup,cl->env); -ncl->l.p=p; -for(j=0;jl.upvals[j]=cl->upvals[GETARG_B(*pc)]; -else{ -ncl->l.upvals[j]=luaF_findupval(L,base+GETARG_B(*pc)); -} -} -setclvalue(L,ra,ncl); -Protect(luaC_checkGC(L)); -continue; -} -case OP_VARARG:{ -int b=GETARG_B(i)-1; -int j; -CallInfo*ci=L->ci; -int n=cast_int(ci->base-ci->func)-cl->p->numparams-1; -if(b==(-1)){ -Protect(luaD_checkstack(L,n)); -ra=RA(i); -b=n; -L->top=ra+n; -} -for(j=0;jbase-n+j); -} -else{ -setnilvalue(ra+j); -} -} -continue; -} -} -} -} -#define api_checknelems(L,n)luai_apicheck(L,(n)<=(L->top-L->base)) -#define api_checkvalidindex(L,i)luai_apicheck(L,(i)!=(&luaO_nilobject_)) -#define api_incr_top(L){luai_apicheck(L,L->topci->top);L->top++;} -static TValue*index2adr(lua_State*L,int idx){ -if(idx>0){ -TValue*o=L->base+(idx-1); -luai_apicheck(L,idx<=L->ci->top-L->base); -if(o>=L->top)return cast(TValue*,(&luaO_nilobject_)); -else return o; -} -else if(idx>(-10000)){ -luai_apicheck(L,idx!=0&&-idx<=L->top-L->base); -return L->top+idx; -} -else switch(idx){ -case(-10000):return registry(L); -case(-10001):{ -Closure*func=curr_func(L); -sethvalue(L,&L->env,func->c.env); -return&L->env; -} -case(-10002):return gt(L); -default:{ -Closure*func=curr_func(L); -idx=(-10002)-idx; -return(idx<=func->c.nupvalues) -?&func->c.upvalue[idx-1] -:cast(TValue*,(&luaO_nilobject_)); -} -} -} -static Table*getcurrenv(lua_State*L){ -if(L->ci==L->base_ci) -return hvalue(gt(L)); -else{ -Closure*func=curr_func(L); -return func->c.env; -} -} -static int lua_checkstack(lua_State*L,int size){ -int res=1; -if(size>8000||(L->top-L->base+size)>8000) -res=0; -else if(size>0){ -luaD_checkstack(L,size); -if(L->ci->toptop+size) -L->ci->top=L->top+size; -} -return res; -} -static lua_CFunction lua_atpanic(lua_State*L,lua_CFunction panicf){ -lua_CFunction old; -old=G(L)->panic; -G(L)->panic=panicf; -return old; -} -static int lua_gettop(lua_State*L){ -return cast_int(L->top-L->base); -} -static void lua_settop(lua_State*L,int idx){ -if(idx>=0){ -luai_apicheck(L,idx<=L->stack_last-L->base); -while(L->topbase+idx) -setnilvalue(L->top++); -L->top=L->base+idx; -} -else{ -luai_apicheck(L,-(idx+1)<=(L->top-L->base)); -L->top+=idx+1; -} -} -static void lua_remove(lua_State*L,int idx){ -StkId p; -p=index2adr(L,idx); -api_checkvalidindex(L,p); -while(++ptop)setobj(L,p-1,p); -L->top--; -} -static void lua_insert(lua_State*L,int idx){ -StkId p; -StkId q; -p=index2adr(L,idx); -api_checkvalidindex(L,p); -for(q=L->top;q>p;q--)setobj(L,q,q-1); -setobj(L,p,L->top); -} -static void lua_replace(lua_State*L,int idx){ -StkId o; -if(idx==(-10001)&&L->ci==L->base_ci) -luaG_runerror(L,"no calling environment"); -api_checknelems(L,1); -o=index2adr(L,idx); -api_checkvalidindex(L,o); -if(idx==(-10001)){ -Closure*func=curr_func(L); -luai_apicheck(L,ttistable(L->top-1)); -func->c.env=hvalue(L->top-1); -luaC_barrier(L,func,L->top-1); -} -else{ -setobj(L,o,L->top-1); -if(idx<(-10002)) -luaC_barrier(L,curr_func(L),L->top-1); -} -L->top--; -} -static void lua_pushvalue(lua_State*L,int idx){ -setobj(L,L->top,index2adr(L,idx)); -api_incr_top(L); -} -static int lua_type(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -return(o==(&luaO_nilobject_))?(-1):ttype(o); -} -static const char*lua_typename(lua_State*L,int t){ -UNUSED(L); -return(t==(-1))?"no value":luaT_typenames[t]; -} -static int lua_iscfunction(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -return iscfunction(o); -} -static int lua_isnumber(lua_State*L,int idx){ -TValue n; -const TValue*o=index2adr(L,idx); -return tonumber(o,&n); -} -static int lua_isstring(lua_State*L,int idx){ -int t=lua_type(L,idx); -return(t==4||t==3); -} -static int lua_rawequal(lua_State*L,int index1,int index2){ -StkId o1=index2adr(L,index1); -StkId o2=index2adr(L,index2); -return(o1==(&luaO_nilobject_)||o2==(&luaO_nilobject_))?0 -:luaO_rawequalObj(o1,o2); -} -static int lua_lessthan(lua_State*L,int index1,int index2){ -StkId o1,o2; -int i; -o1=index2adr(L,index1); -o2=index2adr(L,index2); -i=(o1==(&luaO_nilobject_)||o2==(&luaO_nilobject_))?0 -:luaV_lessthan(L,o1,o2); -return i; -} -static lua_Number lua_tonumber(lua_State*L,int idx){ -TValue n; -const TValue*o=index2adr(L,idx); -if(tonumber(o,&n)) -return nvalue(o); -else -return 0; -} -static lua_Integer lua_tointeger(lua_State*L,int idx){ -TValue n; -const TValue*o=index2adr(L,idx); -if(tonumber(o,&n)){ -lua_Integer res; -lua_Number num=nvalue(o); -lua_number2integer(res,num); -return res; -} -else -return 0; -} -static int lua_toboolean(lua_State*L,int idx){ -const TValue*o=index2adr(L,idx); -return!l_isfalse(o); -} -static const char*lua_tolstring(lua_State*L,int idx,size_t*len){ -StkId o=index2adr(L,idx); -if(!ttisstring(o)){ -if(!luaV_tostring(L,o)){ -if(len!=NULL)*len=0; -return NULL; -} -luaC_checkGC(L); -o=index2adr(L,idx); -} -if(len!=NULL)*len=tsvalue(o)->len; -return svalue(o); -} -static size_t lua_objlen(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -switch(ttype(o)){ -case 4:return tsvalue(o)->len; -case 7:return uvalue(o)->len; -case 5:return luaH_getn(hvalue(o)); -case 3:{ -size_t l; -l=(luaV_tostring(L,o)?tsvalue(o)->len:0); -return l; -} -default:return 0; -} -} -static lua_CFunction lua_tocfunction(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -return(!iscfunction(o))?NULL:clvalue(o)->c.f; -} -static void*lua_touserdata(lua_State*L,int idx){ -StkId o=index2adr(L,idx); -switch(ttype(o)){ -case 7:return(rawuvalue(o)+1); -case 2:return pvalue(o); -default:return NULL; -} -} -static void lua_pushnil(lua_State*L){ -setnilvalue(L->top); -api_incr_top(L); -} -static void lua_pushnumber(lua_State*L,lua_Number n){ -setnvalue(L->top,n); -api_incr_top(L); -} -static void lua_pushinteger(lua_State*L,lua_Integer n){ -setnvalue(L->top,cast_num(n)); -api_incr_top(L); -} -static void lua_pushlstring(lua_State*L,const char*s,size_t len){ -luaC_checkGC(L); -setsvalue(L,L->top,luaS_newlstr(L,s,len)); -api_incr_top(L); -} -static void lua_pushstring(lua_State*L,const char*s){ -if(s==NULL) -lua_pushnil(L); -else -lua_pushlstring(L,s,strlen(s)); -} -static const char*lua_pushvfstring(lua_State*L,const char*fmt, -va_list argp){ -const char*ret; -luaC_checkGC(L); -ret=luaO_pushvfstring(L,fmt,argp); -return ret; -} -static const char*lua_pushfstring(lua_State*L,const char*fmt,...){ -const char*ret; -va_list argp; -luaC_checkGC(L); -va_start(argp,fmt); -ret=luaO_pushvfstring(L,fmt,argp); -va_end(argp); -return ret; -} -static void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n){ -Closure*cl; -luaC_checkGC(L); -api_checknelems(L,n); -cl=luaF_newCclosure(L,n,getcurrenv(L)); -cl->c.f=fn; -L->top-=n; -while(n--) -setobj(L,&cl->c.upvalue[n],L->top+n); -setclvalue(L,L->top,cl); -api_incr_top(L); -} -static void lua_pushboolean(lua_State*L,int b){ -setbvalue(L->top,(b!=0)); -api_incr_top(L); -} -static int lua_pushthread(lua_State*L){ -setthvalue(L,L->top,L); -api_incr_top(L); -return(G(L)->mainthread==L); -} -static void lua_gettable(lua_State*L,int idx){ -StkId t; -t=index2adr(L,idx); -api_checkvalidindex(L,t); -luaV_gettable(L,t,L->top-1,L->top-1); -} -static void lua_getfield(lua_State*L,int idx,const char*k){ -StkId t; -TValue key; -t=index2adr(L,idx); -api_checkvalidindex(L,t); -setsvalue(L,&key,luaS_new(L,k)); -luaV_gettable(L,t,&key,L->top); -api_incr_top(L); -} -static void lua_rawget(lua_State*L,int idx){ -StkId t; -t=index2adr(L,idx); -luai_apicheck(L,ttistable(t)); -setobj(L,L->top-1,luaH_get(hvalue(t),L->top-1)); -} -static void lua_rawgeti(lua_State*L,int idx,int n){ -StkId o; -o=index2adr(L,idx); -luai_apicheck(L,ttistable(o)); -setobj(L,L->top,luaH_getnum(hvalue(o),n)); -api_incr_top(L); -} -static void lua_createtable(lua_State*L,int narray,int nrec){ -luaC_checkGC(L); -sethvalue(L,L->top,luaH_new(L,narray,nrec)); -api_incr_top(L); -} -static int lua_getmetatable(lua_State*L,int objindex){ -const TValue*obj; -Table*mt=NULL; -int res; -obj=index2adr(L,objindex); -switch(ttype(obj)){ -case 5: -mt=hvalue(obj)->metatable; -break; -case 7: -mt=uvalue(obj)->metatable; -break; -default: -mt=G(L)->mt[ttype(obj)]; -break; -} -if(mt==NULL) -res=0; -else{ -sethvalue(L,L->top,mt); -api_incr_top(L); -res=1; -} -return res; -} -static void lua_getfenv(lua_State*L,int idx){ -StkId o; -o=index2adr(L,idx); -api_checkvalidindex(L,o); -switch(ttype(o)){ -case 6: -sethvalue(L,L->top,clvalue(o)->c.env); -break; -case 7: -sethvalue(L,L->top,uvalue(o)->env); -break; -case 8: -setobj(L,L->top,gt(thvalue(o))); -break; -default: -setnilvalue(L->top); -break; -} -api_incr_top(L); -} -static void lua_settable(lua_State*L,int idx){ -StkId t; -api_checknelems(L,2); -t=index2adr(L,idx); -api_checkvalidindex(L,t); -luaV_settable(L,t,L->top-2,L->top-1); -L->top-=2; -} -static void lua_setfield(lua_State*L,int idx,const char*k){ -StkId t; -TValue key; -api_checknelems(L,1); -t=index2adr(L,idx); -api_checkvalidindex(L,t); -setsvalue(L,&key,luaS_new(L,k)); -luaV_settable(L,t,&key,L->top-1); -L->top--; -} -static void lua_rawset(lua_State*L,int idx){ -StkId t; -api_checknelems(L,2); -t=index2adr(L,idx); -luai_apicheck(L,ttistable(t)); -setobj(L,luaH_set(L,hvalue(t),L->top-2),L->top-1); -luaC_barriert(L,hvalue(t),L->top-1); -L->top-=2; -} -static void lua_rawseti(lua_State*L,int idx,int n){ -StkId o; -api_checknelems(L,1); -o=index2adr(L,idx); -luai_apicheck(L,ttistable(o)); -setobj(L,luaH_setnum(L,hvalue(o),n),L->top-1); -luaC_barriert(L,hvalue(o),L->top-1); -L->top--; -} -static int lua_setmetatable(lua_State*L,int objindex){ -TValue*obj; -Table*mt; -api_checknelems(L,1); -obj=index2adr(L,objindex); -api_checkvalidindex(L,obj); -if(ttisnil(L->top-1)) -mt=NULL; -else{ -luai_apicheck(L,ttistable(L->top-1)); -mt=hvalue(L->top-1); -} -switch(ttype(obj)){ -case 5:{ -hvalue(obj)->metatable=mt; -if(mt) -luaC_objbarriert(L,hvalue(obj),mt); -break; -} -case 7:{ -uvalue(obj)->metatable=mt; -if(mt) -luaC_objbarrier(L,rawuvalue(obj),mt); -break; -} -default:{ -G(L)->mt[ttype(obj)]=mt; -break; -} -} -L->top--; -return 1; -} -static int lua_setfenv(lua_State*L,int idx){ -StkId o; -int res=1; -api_checknelems(L,1); -o=index2adr(L,idx); -api_checkvalidindex(L,o); -luai_apicheck(L,ttistable(L->top-1)); -switch(ttype(o)){ -case 6: -clvalue(o)->c.env=hvalue(L->top-1); -break; -case 7: -uvalue(o)->env=hvalue(L->top-1); -break; -case 8: -sethvalue(L,gt(thvalue(o)),hvalue(L->top-1)); -break; -default: -res=0; -break; -} -if(res)luaC_objbarrier(L,gcvalue(o),hvalue(L->top-1)); -L->top--; -return res; -} -#define adjustresults(L,nres){if(nres==(-1)&&L->top>=L->ci->top)L->ci->top=L->top;} -#define checkresults(L,na,nr)luai_apicheck(L,(nr)==(-1)||(L->ci->top-L->top>=(nr)-(na))) -static void lua_call(lua_State*L,int nargs,int nresults){ -StkId func; -api_checknelems(L,nargs+1); -checkresults(L,nargs,nresults); -func=L->top-(nargs+1); -luaD_call(L,func,nresults); -adjustresults(L,nresults); -} -struct CallS{ -StkId func; -int nresults; -}; -static void f_call(lua_State*L,void*ud){ -struct CallS*c=cast(struct CallS*,ud); -luaD_call(L,c->func,c->nresults); -} -static int lua_pcall(lua_State*L,int nargs,int nresults,int errfunc){ -struct CallS c; -int status; -ptrdiff_t func; -api_checknelems(L,nargs+1); -checkresults(L,nargs,nresults); -if(errfunc==0) -func=0; -else{ -StkId o=index2adr(L,errfunc); -api_checkvalidindex(L,o); -func=savestack(L,o); -} -c.func=L->top-(nargs+1); -c.nresults=nresults; -status=luaD_pcall(L,f_call,&c,savestack(L,c.func),func); -adjustresults(L,nresults); -return status; -} -static int lua_load(lua_State*L,lua_Reader reader,void*data, -const char*chunkname){ -ZIO z; -int status; -if(!chunkname)chunkname="?"; -luaZ_init(L,&z,reader,data); -status=luaD_protectedparser(L,&z,chunkname); -return status; -} -static int lua_error(lua_State*L){ -api_checknelems(L,1); -luaG_errormsg(L); -return 0; -} -static int lua_next(lua_State*L,int idx){ -StkId t; -int more; -t=index2adr(L,idx); -luai_apicheck(L,ttistable(t)); -more=luaH_next(L,hvalue(t),L->top-1); -if(more){ -api_incr_top(L); -} -else -L->top-=1; -return more; -} -static void lua_concat(lua_State*L,int n){ -api_checknelems(L,n); -if(n>=2){ -luaC_checkGC(L); -luaV_concat(L,n,cast_int(L->top-L->base)-1); -L->top-=(n-1); -} -else if(n==0){ -setsvalue(L,L->top,luaS_newlstr(L,"",0)); -api_incr_top(L); -} -} -static void*lua_newuserdata(lua_State*L,size_t size){ -Udata*u; -luaC_checkGC(L); -u=luaS_newudata(L,size,getcurrenv(L)); -setuvalue(L,L->top,u); -api_incr_top(L); -return u+1; -} -#define luaL_getn(L,i)((int)lua_objlen(L,i)) -#define luaL_setn(L,i,j)((void)0) -typedef struct luaL_Reg{ -const char*name; -lua_CFunction func; -}luaL_Reg; -static void luaI_openlib(lua_State*L,const char*libname, -const luaL_Reg*l,int nup); -static int luaL_argerror(lua_State*L,int numarg,const char*extramsg); -static const char* luaL_checklstring(lua_State*L,int numArg, -size_t*l); -static const char* luaL_optlstring(lua_State*L,int numArg, -const char*def,size_t*l); -static lua_Integer luaL_checkinteger(lua_State*L,int numArg); -static lua_Integer luaL_optinteger(lua_State*L,int nArg, -lua_Integer def); -static int luaL_error(lua_State*L,const char*fmt,...); -static const char* luaL_findtable(lua_State*L,int idx, -const char*fname,int szhint); -#define luaL_argcheck(L,cond,numarg,extramsg)((void)((cond)||luaL_argerror(L,(numarg),(extramsg)))) -#define luaL_checkstring(L,n)(luaL_checklstring(L,(n),NULL)) -#define luaL_optstring(L,n,d)(luaL_optlstring(L,(n),(d),NULL)) -#define luaL_checkint(L,n)((int)luaL_checkinteger(L,(n))) -#define luaL_optint(L,n,d)((int)luaL_optinteger(L,(n),(d))) -#define luaL_typename(L,i)lua_typename(L,lua_type(L,(i))) -#define luaL_getmetatable(L,n)(lua_getfield(L,(-10000),(n))) -#define luaL_opt(L,f,n,d)(lua_isnoneornil(L,(n))?(d):f(L,(n))) -typedef struct luaL_Buffer{ -char*p; -int lvl; -lua_State*L; -char buffer[BUFSIZ]; -}luaL_Buffer; -#define luaL_addchar(B,c)((void)((B)->p<((B)->buffer+BUFSIZ)||luaL_prepbuffer(B)),(*(B)->p++=(char)(c))) -#define luaL_addsize(B,n)((B)->p+=(n)) -static char* luaL_prepbuffer(luaL_Buffer*B); -static int luaL_argerror(lua_State*L,int narg,const char*extramsg){ -lua_Debug ar; -if(!lua_getstack(L,0,&ar)) -return luaL_error(L,"bad argument #%d (%s)",narg,extramsg); -lua_getinfo(L,"n",&ar); -if(strcmp(ar.namewhat,"method")==0){ -narg--; -if(narg==0) -return luaL_error(L,"calling "LUA_QL("%s")" on bad self (%s)", -ar.name,extramsg); -} -if(ar.name==NULL) -ar.name="?"; -return luaL_error(L,"bad argument #%d to "LUA_QL("%s")" (%s)", -narg,ar.name,extramsg); -} -static int luaL_typerror(lua_State*L,int narg,const char*tname){ -const char*msg=lua_pushfstring(L,"%s expected, got %s", -tname,luaL_typename(L,narg)); -return luaL_argerror(L,narg,msg); -} -static void tag_error(lua_State*L,int narg,int tag){ -luaL_typerror(L,narg,lua_typename(L,tag)); -} -static void luaL_where(lua_State*L,int level){ -lua_Debug ar; -if(lua_getstack(L,level,&ar)){ -lua_getinfo(L,"Sl",&ar); -if(ar.currentline>0){ -lua_pushfstring(L,"%s:%d: ",ar.short_src,ar.currentline); -return; -} -} -lua_pushliteral(L,""); -} -static int luaL_error(lua_State*L,const char*fmt,...){ -va_list argp; -va_start(argp,fmt); -luaL_where(L,1); -lua_pushvfstring(L,fmt,argp); -va_end(argp); -lua_concat(L,2); -return lua_error(L); -} -static int luaL_newmetatable(lua_State*L,const char*tname){ -lua_getfield(L,(-10000),tname); -if(!lua_isnil(L,-1)) -return 0; -lua_pop(L,1); -lua_newtable(L); -lua_pushvalue(L,-1); -lua_setfield(L,(-10000),tname); -return 1; -} -static void*luaL_checkudata(lua_State*L,int ud,const char*tname){ -void*p=lua_touserdata(L,ud); -if(p!=NULL){ -if(lua_getmetatable(L,ud)){ -lua_getfield(L,(-10000),tname); -if(lua_rawequal(L,-1,-2)){ -lua_pop(L,2); -return p; -} -} -} -luaL_typerror(L,ud,tname); -return NULL; -} -static void luaL_checkstack(lua_State*L,int space,const char*mes){ -if(!lua_checkstack(L,space)) -luaL_error(L,"stack overflow (%s)",mes); -} -static void luaL_checktype(lua_State*L,int narg,int t){ -if(lua_type(L,narg)!=t) -tag_error(L,narg,t); -} -static void luaL_checkany(lua_State*L,int narg){ -if(lua_type(L,narg)==(-1)) -luaL_argerror(L,narg,"value expected"); -} -static const char*luaL_checklstring(lua_State*L,int narg,size_t*len){ -const char*s=lua_tolstring(L,narg,len); -if(!s)tag_error(L,narg,4); -return s; -} -static const char*luaL_optlstring(lua_State*L,int narg, -const char*def,size_t*len){ -if(lua_isnoneornil(L,narg)){ -if(len) -*len=(def?strlen(def):0); -return def; -} -else return luaL_checklstring(L,narg,len); -} -static lua_Number luaL_checknumber(lua_State*L,int narg){ -lua_Number d=lua_tonumber(L,narg); -if(d==0&&!lua_isnumber(L,narg)) -tag_error(L,narg,3); -return d; -} -static lua_Integer luaL_checkinteger(lua_State*L,int narg){ -lua_Integer d=lua_tointeger(L,narg); -if(d==0&&!lua_isnumber(L,narg)) -tag_error(L,narg,3); -return d; -} -static lua_Integer luaL_optinteger(lua_State*L,int narg, -lua_Integer def){ -return luaL_opt(L,luaL_checkinteger,narg,def); -} -static int luaL_getmetafield(lua_State*L,int obj,const char*event){ -if(!lua_getmetatable(L,obj)) -return 0; -lua_pushstring(L,event); -lua_rawget(L,-2); -if(lua_isnil(L,-1)){ -lua_pop(L,2); -return 0; -} -else{ -lua_remove(L,-2); -return 1; -} -} -static void luaL_register(lua_State*L,const char*libname, -const luaL_Reg*l){ -luaI_openlib(L,libname,l,0); -} -static int libsize(const luaL_Reg*l){ -int size=0; -for(;l->name;l++)size++; -return size; -} -static void luaI_openlib(lua_State*L,const char*libname, -const luaL_Reg*l,int nup){ -if(libname){ -int size=libsize(l); -luaL_findtable(L,(-10000),"_LOADED",1); -lua_getfield(L,-1,libname); -if(!lua_istable(L,-1)){ -lua_pop(L,1); -if(luaL_findtable(L,(-10002),libname,size)!=NULL) -luaL_error(L,"name conflict for module "LUA_QL("%s"),libname); -lua_pushvalue(L,-1); -lua_setfield(L,-3,libname); -} -lua_remove(L,-2); -lua_insert(L,-(nup+1)); -} -for(;l->name;l++){ -int i; -for(i=0;ifunc,nup); -lua_setfield(L,-(nup+2),l->name); -} -lua_pop(L,nup); -} -static const char*luaL_findtable(lua_State*L,int idx, -const char*fname,int szhint){ -const char*e; -lua_pushvalue(L,idx); -do{ -e=strchr(fname,'.'); -if(e==NULL)e=fname+strlen(fname); -lua_pushlstring(L,fname,e-fname); -lua_rawget(L,-2); -if(lua_isnil(L,-1)){ -lua_pop(L,1); -lua_createtable(L,0,(*e=='.'?1:szhint)); -lua_pushlstring(L,fname,e-fname); -lua_pushvalue(L,-2); -lua_settable(L,-4); -} -else if(!lua_istable(L,-1)){ -lua_pop(L,2); -return fname; -} -lua_remove(L,-2); -fname=e+1; -}while(*e=='.'); -return NULL; -} -#define bufflen(B)((B)->p-(B)->buffer) -#define bufffree(B)((size_t)(BUFSIZ-bufflen(B))) -static int emptybuffer(luaL_Buffer*B){ -size_t l=bufflen(B); -if(l==0)return 0; -else{ -lua_pushlstring(B->L,B->buffer,l); -B->p=B->buffer; -B->lvl++; -return 1; -} -} -static void adjuststack(luaL_Buffer*B){ -if(B->lvl>1){ -lua_State*L=B->L; -int toget=1; -size_t toplen=lua_strlen(L,-1); -do{ -size_t l=lua_strlen(L,-(toget+1)); -if(B->lvl-toget+1>=(20/2)||toplen>l){ -toplen+=l; -toget++; -} -else break; -}while(togetlvl); -lua_concat(L,toget); -B->lvl=B->lvl-toget+1; -} -} -static char*luaL_prepbuffer(luaL_Buffer*B){ -if(emptybuffer(B)) -adjuststack(B); -return B->buffer; -} -static void luaL_addlstring(luaL_Buffer*B,const char*s,size_t l){ -while(l--) -luaL_addchar(B,*s++); -} -static void luaL_pushresult(luaL_Buffer*B){ -emptybuffer(B); -lua_concat(B->L,B->lvl); -B->lvl=1; -} -static void luaL_addvalue(luaL_Buffer*B){ -lua_State*L=B->L; -size_t vl; -const char*s=lua_tolstring(L,-1,&vl); -if(vl<=bufffree(B)){ -memcpy(B->p,s,vl); -B->p+=vl; -lua_pop(L,1); -} -else{ -if(emptybuffer(B)) -lua_insert(L,-2); -B->lvl++; -adjuststack(B); -} -} -static void luaL_buffinit(lua_State*L,luaL_Buffer*B){ -B->L=L; -B->p=B->buffer; -B->lvl=0; -} -typedef struct LoadF{ -int extraline; -FILE*f; -char buff[BUFSIZ]; -}LoadF; -static const char*getF(lua_State*L,void*ud,size_t*size){ -LoadF*lf=(LoadF*)ud; -(void)L; -if(lf->extraline){ -lf->extraline=0; -*size=1; -return"\n"; -} -if(feof(lf->f))return NULL; -*size=fread(lf->buff,1,sizeof(lf->buff),lf->f); -return(*size>0)?lf->buff:NULL; -} -static int errfile(lua_State*L,const char*what,int fnameindex){ -const char*serr=strerror(errno); -const char*filename=lua_tostring(L,fnameindex)+1; -lua_pushfstring(L,"cannot %s %s: %s",what,filename,serr); -lua_remove(L,fnameindex); -return(5+1); -} -static int luaL_loadfile(lua_State*L,const char*filename){ -LoadF lf; -int status,readstatus; -int c; -int fnameindex=lua_gettop(L)+1; -lf.extraline=0; -if(filename==NULL){ -lua_pushliteral(L,"=stdin"); -lf.f=stdin; -} -else{ -lua_pushfstring(L,"@%s",filename); -lf.f=fopen(filename,"r"); -if(lf.f==NULL)return errfile(L,"open",fnameindex); -} -c=getc(lf.f); -if(c=='#'){ -lf.extraline=1; -while((c=getc(lf.f))!=EOF&&c!='\n'); -if(c=='\n')c=getc(lf.f); -} -if(c=="\033Lua"[0]&&filename){ -lf.f=freopen(filename,"rb",lf.f); -if(lf.f==NULL)return errfile(L,"reopen",fnameindex); -while((c=getc(lf.f))!=EOF&&c!="\033Lua"[0]); -lf.extraline=0; -} -ungetc(c,lf.f); -status=lua_load(L,getF,&lf,lua_tostring(L,-1)); -readstatus=ferror(lf.f); -if(filename)fclose(lf.f); -if(readstatus){ -lua_settop(L,fnameindex); -return errfile(L,"read",fnameindex); -} -lua_remove(L,fnameindex); -return status; -} -typedef struct LoadS{ -const char*s; -size_t size; -}LoadS; -static const char*getS(lua_State*L,void*ud,size_t*size){ -LoadS*ls=(LoadS*)ud; -(void)L; -if(ls->size==0)return NULL; -*size=ls->size; -ls->size=0; -return ls->s; -} -static int luaL_loadbuffer(lua_State*L,const char*buff,size_t size, -const char*name){ -LoadS ls; -ls.s=buff; -ls.size=size; -return lua_load(L,getS,&ls,name); -} -static void*l_alloc(void*ud,void*ptr,size_t osize,size_t nsize){ -(void)ud; -(void)osize; -if(nsize==0){ -free(ptr); -return NULL; -} -else -return realloc(ptr,nsize); -} -static int panic(lua_State*L){ -(void)L; -fprintf(stderr,"PANIC: unprotected error in call to Lua API (%s)\n", -lua_tostring(L,-1)); -return 0; -} -static lua_State*luaL_newstate(void){ -lua_State*L=lua_newstate(l_alloc,NULL); -if(L)lua_atpanic(L,&panic); -return L; -} -static int luaB_tonumber(lua_State*L){ -int base=luaL_optint(L,2,10); -if(base==10){ -luaL_checkany(L,1); -if(lua_isnumber(L,1)){ -lua_pushnumber(L,lua_tonumber(L,1)); -return 1; -} -} -else{ -const char*s1=luaL_checkstring(L,1); -char*s2; -unsigned long n; -luaL_argcheck(L,2<=base&&base<=36,2,"base out of range"); -n=strtoul(s1,&s2,base); -if(s1!=s2){ -while(isspace((unsigned char)(*s2)))s2++; -if(*s2=='\0'){ -lua_pushnumber(L,(lua_Number)n); -return 1; -} -} -} -lua_pushnil(L); -return 1; -} -static int luaB_error(lua_State*L){ -int level=luaL_optint(L,2,1); -lua_settop(L,1); -if(lua_isstring(L,1)&&level>0){ -luaL_where(L,level); -lua_pushvalue(L,1); -lua_concat(L,2); -} -return lua_error(L); -} -static int luaB_setmetatable(lua_State*L){ -int t=lua_type(L,2); -luaL_checktype(L,1,5); -luaL_argcheck(L,t==0||t==5,2, -"nil or table expected"); -if(luaL_getmetafield(L,1,"__metatable")) -luaL_error(L,"cannot change a protected metatable"); -lua_settop(L,2); -lua_setmetatable(L,1); -return 1; -} -static void getfunc(lua_State*L,int opt){ -if(lua_isfunction(L,1))lua_pushvalue(L,1); -else{ -lua_Debug ar; -int level=opt?luaL_optint(L,1,1):luaL_checkint(L,1); -luaL_argcheck(L,level>=0,1,"level must be non-negative"); -if(lua_getstack(L,level,&ar)==0) -luaL_argerror(L,1,"invalid level"); -lua_getinfo(L,"f",&ar); -if(lua_isnil(L,-1)) -luaL_error(L,"no function environment for tail call at level %d", -level); -} -} -static int luaB_setfenv(lua_State*L){ -luaL_checktype(L,2,5); -getfunc(L,0); -lua_pushvalue(L,2); -if(lua_isnumber(L,1)&&lua_tonumber(L,1)==0){ -lua_pushthread(L); -lua_insert(L,-2); -lua_setfenv(L,-2); -return 0; -} -else if(lua_iscfunction(L,-2)||lua_setfenv(L,-2)==0) -luaL_error(L, -LUA_QL("setfenv")" cannot change environment of given object"); -return 1; -} -static int luaB_rawget(lua_State*L){ -luaL_checktype(L,1,5); -luaL_checkany(L,2); -lua_settop(L,2); -lua_rawget(L,1); -return 1; -} -static int luaB_type(lua_State*L){ -luaL_checkany(L,1); -lua_pushstring(L,luaL_typename(L,1)); -return 1; -} -static int luaB_next(lua_State*L){ -luaL_checktype(L,1,5); -lua_settop(L,2); -if(lua_next(L,1)) -return 2; -else{ -lua_pushnil(L); -return 1; -} -} -static int luaB_pairs(lua_State*L){ -luaL_checktype(L,1,5); -lua_pushvalue(L,lua_upvalueindex(1)); -lua_pushvalue(L,1); -lua_pushnil(L); -return 3; -} -static int ipairsaux(lua_State*L){ -int i=luaL_checkint(L,2); -luaL_checktype(L,1,5); -i++; -lua_pushinteger(L,i); -lua_rawgeti(L,1,i); -return(lua_isnil(L,-1))?0:2; -} -static int luaB_ipairs(lua_State*L){ -luaL_checktype(L,1,5); -lua_pushvalue(L,lua_upvalueindex(1)); -lua_pushvalue(L,1); -lua_pushinteger(L,0); -return 3; -} -static int load_aux(lua_State*L,int status){ -if(status==0) -return 1; -else{ -lua_pushnil(L); -lua_insert(L,-2); -return 2; -} -} -static int luaB_loadstring(lua_State*L){ -size_t l; -const char*s=luaL_checklstring(L,1,&l); -const char*chunkname=luaL_optstring(L,2,s); -return load_aux(L,luaL_loadbuffer(L,s,l,chunkname)); -} -static int luaB_loadfile(lua_State*L){ -const char*fname=luaL_optstring(L,1,NULL); -return load_aux(L,luaL_loadfile(L,fname)); -} -static int luaB_assert(lua_State*L){ -luaL_checkany(L,1); -if(!lua_toboolean(L,1)) -return luaL_error(L,"%s",luaL_optstring(L,2,"assertion failed!")); -return lua_gettop(L); -} -static int luaB_unpack(lua_State*L){ -int i,e,n; -luaL_checktype(L,1,5); -i=luaL_optint(L,2,1); -e=luaL_opt(L,luaL_checkint,3,luaL_getn(L,1)); -if(i>e)return 0; -n=e-i+1; -if(n<=0||!lua_checkstack(L,n)) -return luaL_error(L,"too many results to unpack"); -lua_rawgeti(L,1,i); -while(i++e)e=pos; -for(i=e;i>pos;i--){ -lua_rawgeti(L,1,i-1); -lua_rawseti(L,1,i); -} -break; -} -default:{ -return luaL_error(L,"wrong number of arguments to "LUA_QL("insert")); -} -} -luaL_setn(L,1,e); -lua_rawseti(L,1,pos); -return 0; -} -static int tremove(lua_State*L){ -int e=aux_getn(L,1); -int pos=luaL_optint(L,2,e); -if(!(1<=pos&&pos<=e)) -return 0; -luaL_setn(L,1,e-1); -lua_rawgeti(L,1,pos); -for(;posu)luaL_error(L,"invalid order function for sorting"); -lua_pop(L,1); -} -while(lua_rawgeti(L,1,--j),sort_comp(L,-3,-1)){ -if(j0); -} -l=strlen(p); -if(l==0||p[l-1]!='\n') -luaL_addsize(&b,l); -else{ -luaL_addsize(&b,l-1); -luaL_pushresult(&b); -return 1; -} -} -} -static int read_chars(lua_State*L,FILE*f,size_t n){ -size_t rlen; -size_t nr; -luaL_Buffer b; -luaL_buffinit(L,&b); -rlen=BUFSIZ; -do{ -char*p=luaL_prepbuffer(&b); -if(rlen>n)rlen=n; -nr=fread(p,sizeof(char),rlen,f); -luaL_addsize(&b,nr); -n-=nr; -}while(n>0&&nr==rlen); -luaL_pushresult(&b); -return(n==0||lua_objlen(L,-1)>0); -} -static int g_read(lua_State*L,FILE*f,int first){ -int nargs=lua_gettop(L)-1; -int success; -int n; -clearerr(f); -if(nargs==0){ -success=read_line(L,f); -n=first+1; -} -else{ -luaL_checkstack(L,nargs+20,"too many arguments"); -success=1; -for(n=first;nargs--&&success;n++){ -if(lua_type(L,n)==3){ -size_t l=(size_t)lua_tointeger(L,n); -success=(l==0)?test_eof(L,f):read_chars(L,f,l); -} -else{ -const char*p=lua_tostring(L,n); -luaL_argcheck(L,p&&p[0]=='*',n,"invalid option"); -switch(p[1]){ -case'n': -success=read_number(L,f); -break; -case'l': -success=read_line(L,f); -break; -case'a': -read_chars(L,f,~((size_t)0)); -success=1; -break; -default: -return luaL_argerror(L,n,"invalid format"); -} -} -} -} -if(ferror(f)) -return pushresult(L,0,NULL); -if(!success){ -lua_pop(L,1); -lua_pushnil(L); -} -return n-first; -} -static int io_read(lua_State*L){ -return g_read(L,getiofile(L,1),1); -} -static int f_read(lua_State*L){ -return g_read(L,tofile(L),2); -} -static int io_readline(lua_State*L){ -FILE*f=*(FILE**)lua_touserdata(L,lua_upvalueindex(1)); -int sucess; -if(f==NULL) -luaL_error(L,"file is already closed"); -sucess=read_line(L,f); -if(ferror(f)) -return luaL_error(L,"%s",strerror(errno)); -if(sucess)return 1; -else{ -if(lua_toboolean(L,lua_upvalueindex(2))){ -lua_settop(L,0); -lua_pushvalue(L,lua_upvalueindex(1)); -aux_close(L); -} -return 0; -} -} -static int g_write(lua_State*L,FILE*f,int arg){ -int nargs=lua_gettop(L)-1; -int status=1; -for(;nargs--;arg++){ -if(lua_type(L,arg)==3){ -status=status&& -fprintf(f,"%.14g",lua_tonumber(L,arg))>0; -} -else{ -size_t l; -const char*s=luaL_checklstring(L,arg,&l); -status=status&&(fwrite(s,sizeof(char),l,f)==l); -} -} -return pushresult(L,status,NULL); -} -static int io_write(lua_State*L){ -return g_write(L,getiofile(L,2),1); -} -static int f_write(lua_State*L){ -return g_write(L,tofile(L),2); -} -static int io_flush(lua_State*L){ -return pushresult(L,fflush(getiofile(L,2))==0,NULL); -} -static int f_flush(lua_State*L){ -return pushresult(L,fflush(tofile(L))==0,NULL); -} -static const luaL_Reg iolib[]={ -{"close",io_close}, -{"flush",io_flush}, -{"input",io_input}, -{"lines",io_lines}, -{"open",io_open}, -{"output",io_output}, -{"read",io_read}, -{"type",io_type}, -{"write",io_write}, -{NULL,NULL} -}; -static const luaL_Reg flib[]={ -{"close",io_close}, -{"flush",f_flush}, -{"lines",f_lines}, -{"read",f_read}, -{"write",f_write}, -{"__gc",io_gc}, -{NULL,NULL} -}; -static void createmeta(lua_State*L){ -luaL_newmetatable(L,"FILE*"); -lua_pushvalue(L,-1); -lua_setfield(L,-2,"__index"); -luaL_register(L,NULL,flib); -} -static void createstdfile(lua_State*L,FILE*f,int k,const char*fname){ -*newfile(L)=f; -if(k>0){ -lua_pushvalue(L,-1); -lua_rawseti(L,(-10001),k); -} -lua_pushvalue(L,-2); -lua_setfenv(L,-2); -lua_setfield(L,-3,fname); -} -static void newfenv(lua_State*L,lua_CFunction cls){ -lua_createtable(L,0,1); -lua_pushcfunction(L,cls); -lua_setfield(L,-2,"__close"); -} -static int luaopen_io(lua_State*L){ -createmeta(L); -newfenv(L,io_fclose); -lua_replace(L,(-10001)); -luaL_register(L,"io",iolib); -newfenv(L,io_noclose); -createstdfile(L,stdin,1,"stdin"); -createstdfile(L,stdout,2,"stdout"); -createstdfile(L,stderr,0,"stderr"); -lua_pop(L,1); -lua_getfield(L,-1,"popen"); -newfenv(L,io_pclose); -lua_setfenv(L,-2); -lua_pop(L,1); -return 1; -} -static int os_pushresult(lua_State*L,int i,const char*filename){ -int en=errno; -if(i){ -lua_pushboolean(L,1); -return 1; -} -else{ -lua_pushnil(L); -lua_pushfstring(L,"%s: %s",filename,strerror(en)); -lua_pushinteger(L,en); -return 3; -} -} -static int os_remove(lua_State*L){ -const char*filename=luaL_checkstring(L,1); -return os_pushresult(L,remove(filename)==0,filename); -} -static int os_exit(lua_State*L){ -exit(luaL_optint(L,1,EXIT_SUCCESS)); -} -static const luaL_Reg syslib[]={ -{"exit",os_exit}, -{"remove",os_remove}, -{NULL,NULL} -}; -static int luaopen_os(lua_State*L){ -luaL_register(L,"os",syslib); -return 1; -} -#define uchar(c)((unsigned char)(c)) -static ptrdiff_t posrelat(ptrdiff_t pos,size_t len){ -if(pos<0)pos+=(ptrdiff_t)len+1; -return(pos>=0)?pos:0; -} -static int str_sub(lua_State*L){ -size_t l; -const char*s=luaL_checklstring(L,1,&l); -ptrdiff_t start=posrelat(luaL_checkinteger(L,2),l); -ptrdiff_t end=posrelat(luaL_optinteger(L,3,-1),l); -if(start<1)start=1; -if(end>(ptrdiff_t)l)end=(ptrdiff_t)l; -if(start<=end) -lua_pushlstring(L,s+start-1,end-start+1); -else lua_pushliteral(L,""); -return 1; -} -static int str_lower(lua_State*L){ -size_t l; -size_t i; -luaL_Buffer b; -const char*s=luaL_checklstring(L,1,&l); -luaL_buffinit(L,&b); -for(i=0;i0) -luaL_addlstring(&b,s,l); -luaL_pushresult(&b); -return 1; -} -static int str_byte(lua_State*L){ -size_t l; -const char*s=luaL_checklstring(L,1,&l); -ptrdiff_t posi=posrelat(luaL_optinteger(L,2,1),l); -ptrdiff_t pose=posrelat(luaL_optinteger(L,3,posi),l); -int n,i; -if(posi<=0)posi=1; -if((size_t)pose>l)pose=l; -if(posi>pose)return 0; -n=(int)(pose-posi+1); -if(posi+n<=pose) -luaL_error(L,"string slice too long"); -luaL_checkstack(L,n,"string slice too long"); -for(i=0;i=ms->level||ms->capture[l].len==(-1)) -return luaL_error(ms->L,"invalid capture index"); -return l; -} -static int capture_to_close(MatchState*ms){ -int level=ms->level; -for(level--;level>=0;level--) -if(ms->capture[level].len==(-1))return level; -return luaL_error(ms->L,"invalid pattern capture"); -} -static const char*classend(MatchState*ms,const char*p){ -switch(*p++){ -case'%':{ -if(*p=='\0') -luaL_error(ms->L,"malformed pattern (ends with "LUA_QL("%%")")"); -return p+1; -} -case'[':{ -if(*p=='^')p++; -do{ -if(*p=='\0') -luaL_error(ms->L,"malformed pattern (missing "LUA_QL("]")")"); -if(*(p++)=='%'&&*p!='\0') -p++; -}while(*p!=']'); -return p+1; -} -default:{ -return p; -} -} -} -static int match_class(int c,int cl){ -int res; -switch(tolower(cl)){ -case'a':res=isalpha(c);break; -case'c':res=iscntrl(c);break; -case'd':res=isdigit(c);break; -case'l':res=islower(c);break; -case'p':res=ispunct(c);break; -case's':res=isspace(c);break; -case'u':res=isupper(c);break; -case'w':res=isalnum(c);break; -case'x':res=isxdigit(c);break; -case'z':res=(c==0);break; -default:return(cl==c); -} -return(islower(cl)?res:!res); -} -static int matchbracketclass(int c,const char*p,const char*ec){ -int sig=1; -if(*(p+1)=='^'){ -sig=0; -p++; -} -while(++pL,"unbalanced pattern"); -if(*s!=*p)return NULL; -else{ -int b=*p; -int e=*(p+1); -int cont=1; -while(++ssrc_end){ -if(*s==e){ -if(--cont==0)return s+1; -} -else if(*s==b)cont++; -} -} -return NULL; -} -static const char*max_expand(MatchState*ms,const char*s, -const char*p,const char*ep){ -ptrdiff_t i=0; -while((s+i)src_end&&singlematch(uchar(*(s+i)),p,ep)) -i++; -while(i>=0){ -const char*res=match(ms,(s+i),ep+1); -if(res)return res; -i--; -} -return NULL; -} -static const char*min_expand(MatchState*ms,const char*s, -const char*p,const char*ep){ -for(;;){ -const char*res=match(ms,s,ep+1); -if(res!=NULL) -return res; -else if(ssrc_end&&singlematch(uchar(*s),p,ep)) -s++; -else return NULL; -} -} -static const char*start_capture(MatchState*ms,const char*s, -const char*p,int what){ -const char*res; -int level=ms->level; -if(level>=32)luaL_error(ms->L,"too many captures"); -ms->capture[level].init=s; -ms->capture[level].len=what; -ms->level=level+1; -if((res=match(ms,s,p))==NULL) -ms->level--; -return res; -} -static const char*end_capture(MatchState*ms,const char*s, -const char*p){ -int l=capture_to_close(ms); -const char*res; -ms->capture[l].len=s-ms->capture[l].init; -if((res=match(ms,s,p))==NULL) -ms->capture[l].len=(-1); -return res; -} -static const char*match_capture(MatchState*ms,const char*s,int l){ -size_t len; -l=check_capture(ms,l); -len=ms->capture[l].len; -if((size_t)(ms->src_end-s)>=len&& -memcmp(ms->capture[l].init,s,len)==0) -return s+len; -else return NULL; -} -static const char*match(MatchState*ms,const char*s,const char*p){ -init: -switch(*p){ -case'(':{ -if(*(p+1)==')') -return start_capture(ms,s,p+2,(-2)); -else -return start_capture(ms,s,p+1,(-1)); -} -case')':{ -return end_capture(ms,s,p+1); -} -case'%':{ -switch(*(p+1)){ -case'b':{ -s=matchbalance(ms,s,p+2); -if(s==NULL)return NULL; -p+=4;goto init; -} -case'f':{ -const char*ep;char previous; -p+=2; -if(*p!='[') -luaL_error(ms->L,"missing "LUA_QL("[")" after " -LUA_QL("%%f")" in pattern"); -ep=classend(ms,p); -previous=(s==ms->src_init)?'\0':*(s-1); -if(matchbracketclass(uchar(previous),p,ep-1)|| -!matchbracketclass(uchar(*s),p,ep-1))return NULL; -p=ep;goto init; -} -default:{ -if(isdigit(uchar(*(p+1)))){ -s=match_capture(ms,s,uchar(*(p+1))); -if(s==NULL)return NULL; -p+=2;goto init; -} -goto dflt; -} -} -} -case'\0':{ -return s; -} -case'$':{ -if(*(p+1)=='\0') -return(s==ms->src_end)?s:NULL; -else goto dflt; -} -default:dflt:{ -const char*ep=classend(ms,p); -int m=ssrc_end&&singlematch(uchar(*s),p,ep); -switch(*ep){ -case'?':{ -const char*res; -if(m&&((res=match(ms,s+1,ep+1))!=NULL)) -return res; -p=ep+1;goto init; -} -case'*':{ -return max_expand(ms,s,p,ep); -} -case'+':{ -return(m?max_expand(ms,s+1,p,ep):NULL); -} -case'-':{ -return min_expand(ms,s,p,ep); -} -default:{ -if(!m)return NULL; -s++;p=ep;goto init; -} -} -} -} -} -static const char*lmemfind(const char*s1,size_t l1, -const char*s2,size_t l2){ -if(l2==0)return s1; -else if(l2>l1)return NULL; -else{ -const char*init; -l2--; -l1=l1-l2; -while(l1>0&&(init=(const char*)memchr(s1,*s2,l1))!=NULL){ -init++; -if(memcmp(init,s2+1,l2)==0) -return init-1; -else{ -l1-=init-s1; -s1=init; -} -} -return NULL; -} -} -static void push_onecapture(MatchState*ms,int i,const char*s, -const char*e){ -if(i>=ms->level){ -if(i==0) -lua_pushlstring(ms->L,s,e-s); -else -luaL_error(ms->L,"invalid capture index"); -} -else{ -ptrdiff_t l=ms->capture[i].len; -if(l==(-1))luaL_error(ms->L,"unfinished capture"); -if(l==(-2)) -lua_pushinteger(ms->L,ms->capture[i].init-ms->src_init+1); -else -lua_pushlstring(ms->L,ms->capture[i].init,l); -} -} -static int push_captures(MatchState*ms,const char*s,const char*e){ -int i; -int nlevels=(ms->level==0&&s)?1:ms->level; -luaL_checkstack(ms->L,nlevels,"too many captures"); -for(i=0;il1)init=(ptrdiff_t)l1; -if(find&&(lua_toboolean(L,4)|| -strpbrk(p,"^$*+?.([%-")==NULL)){ -const char*s2=lmemfind(s+init,l1-init,p,l2); -if(s2){ -lua_pushinteger(L,s2-s+1); -lua_pushinteger(L,s2-s+l2); -return 2; -} -} -else{ -MatchState ms; -int anchor=(*p=='^')?(p++,1):0; -const char*s1=s+init; -ms.L=L; -ms.src_init=s; -ms.src_end=s+l1; -do{ -const char*res; -ms.level=0; -if((res=match(&ms,s1,p))!=NULL){ -if(find){ -lua_pushinteger(L,s1-s+1); -lua_pushinteger(L,res-s); -return push_captures(&ms,NULL,0)+2; -} -else -return push_captures(&ms,s1,res); -} -}while(s1++L,3,&l); -for(i=0;iL; -switch(lua_type(L,3)){ -case 3: -case 4:{ -add_s(ms,b,s,e); -return; -} -case 6:{ -int n; -lua_pushvalue(L,3); -n=push_captures(ms,s,e); -lua_call(L,n,1); -break; -} -case 5:{ -push_onecapture(ms,0,s,e); -lua_gettable(L,3); -break; -} -} -if(!lua_toboolean(L,-1)){ -lua_pop(L,1); -lua_pushlstring(L,s,e-s); -} -else if(!lua_isstring(L,-1)) -luaL_error(L,"invalid replacement value (a %s)",luaL_typename(L,-1)); -luaL_addvalue(b); -} -static int str_gsub(lua_State*L){ -size_t srcl; -const char*src=luaL_checklstring(L,1,&srcl); -const char*p=luaL_checkstring(L,2); -int tr=lua_type(L,3); -int max_s=luaL_optint(L,4,srcl+1); -int anchor=(*p=='^')?(p++,1):0; -int n=0; -MatchState ms; -luaL_Buffer b; -luaL_argcheck(L,tr==3||tr==4|| -tr==6||tr==5,3, -"string/function/table expected"); -luaL_buffinit(L,&b); -ms.L=L; -ms.src_init=src; -ms.src_end=src+srcl; -while(nsrc) -src=e; -else if(src=sizeof("-+ #0")) -luaL_error(L,"invalid format (repeated flags)"); -if(isdigit(uchar(*p)))p++; -if(isdigit(uchar(*p)))p++; -if(*p=='.'){ -p++; -if(isdigit(uchar(*p)))p++; -if(isdigit(uchar(*p)))p++; -} -if(isdigit(uchar(*p))) -luaL_error(L,"invalid format (width or precision too long)"); -*(form++)='%'; -strncpy(form,strfrmt,p-strfrmt+1); -form+=p-strfrmt+1; -*form='\0'; -return p; -} -static void addintlen(char*form){ -size_t l=strlen(form); -char spec=form[l-1]; -strcpy(form+l-1,"l"); -form[l+sizeof("l")-2]=spec; -form[l+sizeof("l")-1]='\0'; -} -static int str_format(lua_State*L){ -int top=lua_gettop(L); -int arg=1; -size_t sfl; -const char*strfrmt=luaL_checklstring(L,arg,&sfl); -const char*strfrmt_end=strfrmt+sfl; -luaL_Buffer b; -luaL_buffinit(L,&b); -while(strfrmttop) -luaL_argerror(L,arg,"no value"); -strfrmt=scanformat(L,strfrmt,form); -switch(*strfrmt++){ -case'c':{ -sprintf(buff,form,(int)luaL_checknumber(L,arg)); -break; -} -case'd':case'i':{ -addintlen(form); -sprintf(buff,form,(long)luaL_checknumber(L,arg)); -break; -} -case'o':case'u':case'x':case'X':{ -addintlen(form); -sprintf(buff,form,(unsigned long)luaL_checknumber(L,arg)); -break; -} -case'e':case'E':case'f': -case'g':case'G':{ -sprintf(buff,form,(double)luaL_checknumber(L,arg)); -break; -} -case'q':{ -addquoted(L,&b,arg); -continue; -} -case's':{ -size_t l; -const char*s=luaL_checklstring(L,arg,&l); -if(!strchr(form,'.')&&l>=100){ -lua_pushvalue(L,arg); -luaL_addvalue(&b); -continue; -} -else{ -sprintf(buff,form,s); -break; -} -} -default:{ -return luaL_error(L,"invalid option "LUA_QL("%%%c")" to " -LUA_QL("format"),*(strfrmt-1)); -} -} -luaL_addlstring(&b,buff,strlen(buff)); -} -} -luaL_pushresult(&b); -return 1; -} -static const luaL_Reg strlib[]={ -{"byte",str_byte}, -{"char",str_char}, -{"find",str_find}, -{"format",str_format}, -{"gmatch",gmatch}, -{"gsub",str_gsub}, -{"lower",str_lower}, -{"match",str_match}, -{"rep",str_rep}, -{"sub",str_sub}, -{"upper",str_upper}, -{NULL,NULL} -}; -static void createmetatable(lua_State*L){ -lua_createtable(L,0,1); -lua_pushliteral(L,""); -lua_pushvalue(L,-2); -lua_setmetatable(L,-2); -lua_pop(L,1); -lua_pushvalue(L,-2); -lua_setfield(L,-2,"__index"); -lua_pop(L,1); -} -static int luaopen_string(lua_State*L){ -luaL_register(L,"string",strlib); -createmetatable(L); -return 1; -} -static const luaL_Reg lualibs[]={ -{"",luaopen_base}, -{"table",luaopen_table}, -{"io",luaopen_io}, -{"os",luaopen_os}, -{"string",luaopen_string}, -{NULL,NULL} -}; -static void luaL_openlibs(lua_State*L){ -const luaL_Reg*lib=lualibs; -for(;lib->func;lib++){ -lua_pushcfunction(L,lib->func); -lua_pushstring(L,lib->name); -lua_call(L,1,0); -} -} -typedef unsigned int UB; -static UB barg(lua_State*L,int idx){ -union{lua_Number n;U64 b;}bn; -bn.n=lua_tonumber(L,idx)+6755399441055744.0; -if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); -return(UB)bn.b; -} -#define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1; -static int tobit(lua_State*L){ -BRET(barg(L,1))} -static int bnot(lua_State*L){ -BRET(~barg(L,1))} -static int band(lua_State*L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} -static int bor(lua_State*L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} -static int bxor(lua_State*L){ -int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} -static int lshift(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET(b<>n)} -static int arshift(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} -static int rol(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b<>(32-n)))} -static int ror(lua_State*L){ -UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} -static int bswap(lua_State*L){ -UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} -static int tohex(lua_State*L){ -UB b=barg(L,1); -int n=lua_isnone(L,2)?8:(int)barg(L,2); -const char*hexdigits="0123456789abcdef"; -char buf[8]; -int i; -if(n<0){n=-n;hexdigits="0123456789ABCDEF";} -if(n>8)n=8; -for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} -lua_pushlstring(L,buf,(size_t)n); -return 1; -} -static const struct luaL_Reg bitlib[]={ -{"tobit",tobit}, -{"bnot",bnot}, -{"band",band}, -{"bor",bor}, -{"bxor",bxor}, -{"lshift",lshift}, -{"rshift",rshift}, -{"arshift",arshift}, -{"rol",rol}, -{"ror",ror}, -{"bswap",bswap}, -{"tohex",tohex}, -{NULL,NULL} -}; -int main(int argc,char**argv){ -lua_State*L=luaL_newstate(); -int i; -luaL_openlibs(L); -luaL_register(L,"bit",bitlib); -if(argc<2)return sizeof(void*); -lua_createtable(L,0,1); -lua_pushstring(L,argv[1]); -lua_rawseti(L,-2,0); -lua_setglobal(L,"arg"); -if(luaL_loadfile(L,argv[1])) -goto err; -for(i=2;i -- BYTECODE -- [...] --- print(bc.line(foo, 2)) --> 0002 KSTR 1 1 ; "hello" --- --- local out = { --- -- Do something with each line: --- write = function(t, ...) io.write(...) end, --- close = function(t) end, --- flush = function(t) end, --- } --- bc.dump(foo, out) --- ------------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20100, "LuaJIT core/library version mismatch") -local jutil = require("jit.util") -local vmdef = require("jit.vmdef") -local bit = require("bit") -local sub, gsub, format = string.sub, string.gsub, string.format -local byte, band, shr = string.byte, bit.band, bit.rshift -local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck -local funcuvname = jutil.funcuvname -local bcnames = vmdef.bcnames -local stdout, stderr = io.stdout, io.stderr - ------------------------------------------------------------------------------- - -local function ctlsub(c) - if c == "\n" then return "\\n" - elseif c == "\r" then return "\\r" - elseif c == "\t" then return "\\t" - else return format("\\%03d", byte(c)) - end -end - --- Return one bytecode line. -local function bcline(func, pc, prefix) - local ins, m = funcbc(func, pc) - if not ins then return end - local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128) - local a = band(shr(ins, 8), 0xff) - local oidx = 6*band(ins, 0xff) - local op = sub(bcnames, oidx+1, oidx+6) - local s = format("%04d %s %-6s %3s ", - pc, prefix or " ", op, ma == 0 and "" or a) - local d = shr(ins, 16) - if mc == 13*128 then -- BCMjump - return format("%s=> %04d\n", s, pc+d-0x7fff) - end - if mb ~= 0 then - d = band(d, 0xff) - elseif mc == 0 then - return s.."\n" - end - local kc - if mc == 10*128 then -- BCMstr - kc = funck(func, -d-1) - kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub)) - elseif mc == 9*128 then -- BCMnum - kc = funck(func, d) - if op == "TSETM " then kc = kc - 2^52 end - elseif mc == 12*128 then -- BCMfunc - local fi = funcinfo(funck(func, -d-1)) - if fi.ffid then - kc = vmdef.ffnames[fi.ffid] - else - kc = fi.loc - end - elseif mc == 5*128 then -- BCMuv - kc = funcuvname(func, d) - end - if ma == 5 then -- BCMuv - local ka = funcuvname(func, a) - if kc then kc = ka.." ; "..kc else kc = ka end - end - if mb ~= 0 then - local b = shr(ins, 24) - if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end - return format("%s%3d %3d\n", s, b, d) - end - if kc then return format("%s%3d ; %s\n", s, d, kc) end - if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits - return format("%s%3d\n", s, d) -end - --- Collect branch targets of a function. -local function bctargets(func) - local target = {} - for pc=1,1000000000 do - local ins, m = funcbc(func, pc) - if not ins then break end - if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end - end - return target -end - --- Dump bytecode instructions of a function. -local function bcdump(func, out, all) - if not out then out = stdout end - local fi = funcinfo(func) - if all and fi.children then - for n=-1,-1000000000,-1 do - local k = funck(func, n) - if not k then break end - if type(k) == "proto" then bcdump(k, out, true) end - end - end - out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined)) - local target = bctargets(func) - for pc=1,1000000000 do - local s = bcline(func, pc, target[pc] and "=>") - if not s then break end - out:write(s) - end - out:write("\n") - out:flush() -end - ------------------------------------------------------------------------------- - --- Active flag and output file handle. -local active, out - --- List handler. -local function h_list(func) - return bcdump(func, out) -end - --- Detach list handler. -local function bclistoff() - if active then - active = false - jit.attach(h_list) - if out and out ~= stdout and out ~= stderr then out:close() end - out = nil - end -end - --- Open the output file and attach list handler. -local function bcliston(outfile) - if active then bclistoff() end - if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stderr - end - jit.attach(h_list, "bc") - active = true -end - --- Public module functions. -return { - line = bcline, - dump = bcdump, - targets = bctargets, - on = bcliston, - off = bclistoff, - start = bcliston -- For -j command line option. -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/bcsave.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/bcsave.lua deleted file mode 100644 index 90fe9da..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/bcsave.lua +++ /dev/null @@ -1,705 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT module to save/list bytecode. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module saves or lists the bytecode for an input file. --- It's run by the -b command line option. --- ------------------------------------------------------------------------------- - -local jit = require("jit") -assert(jit.version_num == 20100, "LuaJIT core/library version mismatch") -local bit = require("bit") - --- Symbol name prefix for LuaJIT bytecode. -local LJBC_PREFIX = "luaJIT_BC_" - -local type, assert = type, assert -local format = string.format -local tremove, tconcat = table.remove, table.concat - ------------------------------------------------------------------------------- - -local function usage() - io.stderr:write[[ -Save LuaJIT bytecode: luajit -b[options] input output - -l Only list bytecode. - -s Strip debug info (default). - -g Keep debug info. - -n name Set module name (default: auto-detect from input name). - -t type Set output file type (default: auto-detect from output name). - -a arch Override architecture for object files (default: native). - -o os Override OS for object files (default: native). - -F name Override filename (default: input filename). - -e chunk Use chunk string as input. - -- Stop handling options. - - Use stdin as input and/or stdout as output. - -File types: c h obj o raw (default) -]] - os.exit(1) -end - -local function check(ok, ...) - if ok then return ok, ... end - io.stderr:write("luajit: ", ...) - io.stderr:write("\n") - os.exit(1) -end - -local function readfile(ctx, input) - if type(input) == "function" then return input end - if ctx.filename then - local data - if input == "-" then - data = io.stdin:read("*a") - else - local fp = assert(io.open(input, "rb")) - data = assert(fp:read("*a")) - assert(fp:close()) - end - return check(load(data, ctx.filename)) - else - if input == "-" then input = nil end - return check(loadfile(input)) - end -end - -local function savefile(name, mode) - if name == "-" then return io.stdout end - return check(io.open(name, mode)) -end - -local function set_stdout_binary(ffi) - ffi.cdef[[int _setmode(int fd, int mode);]] - ffi.C._setmode(1, 0x8000) -end - ------------------------------------------------------------------------------- - -local map_type = { - raw = "raw", c = "c", h = "h", o = "obj", obj = "obj", -} - -local map_arch = { - x86 = { e = "le", b = 32, m = 3, p = 0x14c, }, - x64 = { e = "le", b = 64, m = 62, p = 0x8664, }, - arm = { e = "le", b = 32, m = 40, p = 0x1c0, }, - arm64 = { e = "le", b = 64, m = 183, p = 0xaa64, }, - arm64be = { e = "be", b = 64, m = 183, }, - ppc = { e = "be", b = 32, m = 20, }, - mips = { e = "be", b = 32, m = 8, f = 0x50001006, }, - mipsel = { e = "le", b = 32, m = 8, f = 0x50001006, }, - mips64 = { e = "be", b = 64, m = 8, f = 0x80000007, }, - mips64el = { e = "le", b = 64, m = 8, f = 0x80000007, }, - mips64r6 = { e = "be", b = 64, m = 8, f = 0xa0000407, }, - mips64r6el = { e = "le", b = 64, m = 8, f = 0xa0000407, }, -} - -local map_os = { - linux = true, windows = true, osx = true, freebsd = true, netbsd = true, - openbsd = true, dragonfly = true, solaris = true, -} - -local function checkarg(str, map, err) - str = str:lower() - local s = check(map[str], "unknown ", err) - return type(s) == "string" and s or str -end - -local function detecttype(str) - local ext = str:lower():match("%.(%a+)$") - return map_type[ext] or "raw" -end - -local function checkmodname(str) - check(str:match("^[%w_.%-]+$"), "bad module name") - return str:gsub("[%.%-]", "_") -end - -local function detectmodname(str) - if type(str) == "string" then - local tail = str:match("[^/\\]+$") - if tail then str = tail end - local head = str:match("^(.*)%.[^.]*$") - if head then str = head end - str = str:match("^[%w_.%-]+") - else - str = nil - end - check(str, "cannot derive module name, use -n name") - return str:gsub("[%.%-]", "_") -end - ------------------------------------------------------------------------------- - -local function bcsave_tail(fp, output, s) - local ok, err = fp:write(s) - if ok and output ~= "-" then ok, err = fp:close() end - check(ok, "cannot write ", output, ": ", err) -end - -local function bcsave_raw(output, s) - if output == "-" and jit.os == "Windows" then - local ok, ffi = pcall(require, "ffi") - check(ok, "FFI library required to write binary file to stdout") - set_stdout_binary(ffi) - end - local fp = savefile(output, "wb") - bcsave_tail(fp, output, s) -end - -local function bcsave_c(ctx, output, s) - local fp = savefile(output, "w") - if ctx.type == "c" then - fp:write(format([[ -#ifdef __cplusplus -extern "C" -#endif -#ifdef _WIN32 -__declspec(dllexport) -#endif -const unsigned char %s%s[] = { -]], LJBC_PREFIX, ctx.modname)) - else - fp:write(format([[ -#define %s%s_SIZE %d -static const unsigned char %s%s[] = { -]], LJBC_PREFIX, ctx.modname, #s, LJBC_PREFIX, ctx.modname)) - end - local t, n, m = {}, 0, 0 - for i=1,#s do - local b = tostring(string.byte(s, i)) - m = m + #b + 1 - if m > 78 then - fp:write(tconcat(t, ",", 1, n), ",\n") - n, m = 0, #b + 1 - end - n = n + 1 - t[n] = b - end - bcsave_tail(fp, output, tconcat(t, ",", 1, n).."\n};\n") -end - -local function bcsave_elfobj(ctx, output, s, ffi) - ffi.cdef[[ -typedef struct { - uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7]; - uint16_t type, machine; - uint32_t version; - uint32_t entry, phofs, shofs; - uint32_t flags; - uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx; -} ELF32header; -typedef struct { - uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7]; - uint16_t type, machine; - uint32_t version; - uint64_t entry, phofs, shofs; - uint32_t flags; - uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx; -} ELF64header; -typedef struct { - uint32_t name, type, flags, addr, ofs, size, link, info, align, entsize; -} ELF32sectheader; -typedef struct { - uint32_t name, type; - uint64_t flags, addr, ofs, size; - uint32_t link, info; - uint64_t align, entsize; -} ELF64sectheader; -typedef struct { - uint32_t name, value, size; - uint8_t info, other; - uint16_t sectidx; -} ELF32symbol; -typedef struct { - uint32_t name; - uint8_t info, other; - uint16_t sectidx; - uint64_t value, size; -} ELF64symbol; -typedef struct { - ELF32header hdr; - ELF32sectheader sect[6]; - ELF32symbol sym[2]; - uint8_t space[4096]; -} ELF32obj; -typedef struct { - ELF64header hdr; - ELF64sectheader sect[6]; - ELF64symbol sym[2]; - uint8_t space[4096]; -} ELF64obj; -]] - local symname = LJBC_PREFIX..ctx.modname - local ai = assert(map_arch[ctx.arch]) - local is64, isbe = ai.b == 64, ai.e == "be" - - -- Handle different host/target endianess. - local function f32(x) return x end - local f16, fofs = f32, f32 - if ffi.abi("be") ~= isbe then - f32 = bit.bswap - function f16(x) return bit.rshift(bit.bswap(x), 16) end - if is64 then - local two32 = ffi.cast("int64_t", 2^32) - function fofs(x) return bit.bswap(x)*two32 end - else - fofs = f32 - end - end - - -- Create ELF object and fill in header. - local o = ffi.new(is64 and "ELF64obj" or "ELF32obj") - local hdr = o.hdr - if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi. - local bf = assert(io.open("/bin/ls", "rb")) - local bs = bf:read(9) - bf:close() - ffi.copy(o, bs, 9) - check(hdr.emagic[0] == 127, "no support for writing native object files") - else - hdr.emagic = "\127ELF" - hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0 - end - hdr.eclass = is64 and 2 or 1 - hdr.eendian = isbe and 2 or 1 - hdr.eversion = 1 - hdr.type = f16(1) - hdr.machine = f16(ai.m) - hdr.flags = f32(ai.f or 0) - hdr.version = f32(1) - hdr.shofs = fofs(ffi.offsetof(o, "sect")) - hdr.ehsize = f16(ffi.sizeof(hdr)) - hdr.shentsize = f16(ffi.sizeof(o.sect[0])) - hdr.shnum = f16(6) - hdr.shstridx = f16(2) - - -- Fill in sections and symbols. - local sofs, ofs = ffi.offsetof(o, "space"), 1 - for i,name in ipairs{ - ".symtab", ".shstrtab", ".strtab", ".rodata", ".note.GNU-stack", - } do - local sect = o.sect[i] - sect.align = fofs(1) - sect.name = f32(ofs) - ffi.copy(o.space+ofs, name) - ofs = ofs + #name+1 - end - o.sect[1].type = f32(2) -- .symtab - o.sect[1].link = f32(3) - o.sect[1].info = f32(1) - o.sect[1].align = fofs(8) - o.sect[1].ofs = fofs(ffi.offsetof(o, "sym")) - o.sect[1].entsize = fofs(ffi.sizeof(o.sym[0])) - o.sect[1].size = fofs(ffi.sizeof(o.sym)) - o.sym[1].name = f32(1) - o.sym[1].sectidx = f16(4) - o.sym[1].size = fofs(#s) - o.sym[1].info = 17 - o.sect[2].type = f32(3) -- .shstrtab - o.sect[2].ofs = fofs(sofs) - o.sect[2].size = fofs(ofs) - o.sect[3].type = f32(3) -- .strtab - o.sect[3].ofs = fofs(sofs + ofs) - o.sect[3].size = fofs(#symname+2) - ffi.copy(o.space+ofs+1, symname) - ofs = ofs + #symname + 2 - o.sect[4].type = f32(1) -- .rodata - o.sect[4].flags = fofs(2) - o.sect[4].ofs = fofs(sofs + ofs) - o.sect[4].size = fofs(#s) - o.sect[5].type = f32(1) -- .note.GNU-stack - o.sect[5].ofs = fofs(sofs + ofs + #s) - - -- Write ELF object file. - local fp = savefile(output, "wb") - fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs)) - bcsave_tail(fp, output, s) -end - -local function bcsave_peobj(ctx, output, s, ffi) - ffi.cdef[[ -typedef struct { - uint16_t arch, nsects; - uint32_t time, symtabofs, nsyms; - uint16_t opthdrsz, flags; -} PEheader; -typedef struct { - char name[8]; - uint32_t vsize, vaddr, size, ofs, relocofs, lineofs; - uint16_t nreloc, nline; - uint32_t flags; -} PEsection; -typedef struct __attribute((packed)) { - union { - char name[8]; - uint32_t nameref[2]; - }; - uint32_t value; - int16_t sect; - uint16_t type; - uint8_t scl, naux; -} PEsym; -typedef struct __attribute((packed)) { - uint32_t size; - uint16_t nreloc, nline; - uint32_t cksum; - uint16_t assoc; - uint8_t comdatsel, unused[3]; -} PEsymaux; -typedef struct { - PEheader hdr; - PEsection sect[2]; - // Must be an even number of symbol structs. - PEsym sym0; - PEsymaux sym0aux; - PEsym sym1; - PEsymaux sym1aux; - PEsym sym2; - PEsym sym3; - uint32_t strtabsize; - uint8_t space[4096]; -} PEobj; -]] - local symname = LJBC_PREFIX..ctx.modname - local ai = assert(map_arch[ctx.arch]) - local is64 = ai.b == 64 - local symexport = " /EXPORT:"..symname..",DATA " - - -- The file format is always little-endian. Swap if the host is big-endian. - local function f32(x) return x end - local f16 = f32 - if ffi.abi("be") then - f32 = bit.bswap - function f16(x) return bit.rshift(bit.bswap(x), 16) end - end - - -- Create PE object and fill in header. - local o = ffi.new("PEobj") - local hdr = o.hdr - hdr.arch = f16(assert(ai.p)) - hdr.nsects = f16(2) - hdr.symtabofs = f32(ffi.offsetof(o, "sym0")) - hdr.nsyms = f32(6) - - -- Fill in sections and symbols. - o.sect[0].name = ".drectve" - o.sect[0].size = f32(#symexport) - o.sect[0].flags = f32(0x00100a00) - o.sym0.sect = f16(1) - o.sym0.scl = 3 - o.sym0.name = ".drectve" - o.sym0.naux = 1 - o.sym0aux.size = f32(#symexport) - o.sect[1].name = ".rdata" - o.sect[1].size = f32(#s) - o.sect[1].flags = f32(0x40300040) - o.sym1.sect = f16(2) - o.sym1.scl = 3 - o.sym1.name = ".rdata" - o.sym1.naux = 1 - o.sym1aux.size = f32(#s) - o.sym2.sect = f16(2) - o.sym2.scl = 2 - o.sym2.nameref[1] = f32(4) - o.sym3.sect = f16(-1) - o.sym3.scl = 2 - o.sym3.value = f32(1) - o.sym3.name = "@feat.00" -- Mark as SafeSEH compliant. - ffi.copy(o.space, symname) - local ofs = #symname + 1 - o.strtabsize = f32(ofs + 4) - o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs) - ffi.copy(o.space + ofs, symexport) - ofs = ofs + #symexport - o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs) - - -- Write PE object file. - local fp = savefile(output, "wb") - fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs)) - bcsave_tail(fp, output, s) -end - -local function bcsave_machobj(ctx, output, s, ffi) - ffi.cdef[[ -typedef struct -{ - uint32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags; -} mach_header; -typedef struct -{ - mach_header; uint32_t reserved; -} mach_header_64; -typedef struct { - uint32_t cmd, cmdsize; - char segname[16]; - uint32_t vmaddr, vmsize, fileoff, filesize; - uint32_t maxprot, initprot, nsects, flags; -} mach_segment_command; -typedef struct { - uint32_t cmd, cmdsize; - char segname[16]; - uint64_t vmaddr, vmsize, fileoff, filesize; - uint32_t maxprot, initprot, nsects, flags; -} mach_segment_command_64; -typedef struct { - char sectname[16], segname[16]; - uint32_t addr, size; - uint32_t offset, align, reloff, nreloc, flags; - uint32_t reserved1, reserved2; -} mach_section; -typedef struct { - char sectname[16], segname[16]; - uint64_t addr, size; - uint32_t offset, align, reloff, nreloc, flags; - uint32_t reserved1, reserved2, reserved3; -} mach_section_64; -typedef struct { - uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize; -} mach_symtab_command; -typedef struct { - int32_t strx; - uint8_t type, sect; - int16_t desc; - uint32_t value; -} mach_nlist; -typedef struct { - int32_t strx; - uint8_t type, sect; - uint16_t desc; - uint64_t value; -} mach_nlist_64; -typedef struct -{ - int32_t magic, nfat_arch; -} mach_fat_header; -typedef struct -{ - int32_t cputype, cpusubtype, offset, size, align; -} mach_fat_arch; -typedef struct { - struct { - mach_header hdr; - mach_segment_command seg; - mach_section sec; - mach_symtab_command sym; - } arch[1]; - mach_nlist sym_entry; - uint8_t space[4096]; -} mach_obj; -typedef struct { - struct { - mach_header_64 hdr; - mach_segment_command_64 seg; - mach_section_64 sec; - mach_symtab_command sym; - } arch[1]; - mach_nlist_64 sym_entry; - uint8_t space[4096]; -} mach_obj_64; -typedef struct { - mach_fat_header fat; - mach_fat_arch fat_arch[2]; - struct { - mach_header hdr; - mach_segment_command seg; - mach_section sec; - mach_symtab_command sym; - } arch[2]; - mach_nlist sym_entry; - uint8_t space[4096]; -} mach_fat_obj; -typedef struct { - mach_fat_header fat; - mach_fat_arch fat_arch[2]; - struct { - mach_header_64 hdr; - mach_segment_command_64 seg; - mach_section_64 sec; - mach_symtab_command sym; - } arch[2]; - mach_nlist_64 sym_entry; - uint8_t space[4096]; -} mach_fat_obj_64; -]] - local symname = '_'..LJBC_PREFIX..ctx.modname - local isfat, is64, align, mobj = false, false, 4, "mach_obj" - if ctx.arch == "x64" then - is64, align, mobj = true, 8, "mach_obj_64" - elseif ctx.arch == "arm" then - isfat, mobj = true, "mach_fat_obj" - elseif ctx.arch == "arm64" then - is64, align, isfat, mobj = true, 8, true, "mach_fat_obj_64" - else - check(ctx.arch == "x86", "unsupported architecture for OSX") - end - local function aligned(v, a) return bit.band(v+a-1, -a) end - local be32 = bit.bswap -- Mach-O FAT is BE, supported archs are LE. - - -- Create Mach-O object and fill in header. - local o = ffi.new(mobj) - local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, align) - local cputype = ({ x86={7}, x64={0x01000007}, arm={7,12}, arm64={0x01000007,0x0100000c} })[ctx.arch] - local cpusubtype = ({ x86={3}, x64={3}, arm={3,9}, arm64={3,0} })[ctx.arch] - if isfat then - o.fat.magic = be32(0xcafebabe) - o.fat.nfat_arch = be32(#cpusubtype) - end - - -- Fill in sections and symbols. - for i=0,#cpusubtype-1 do - local ofs = 0 - if isfat then - local a = o.fat_arch[i] - a.cputype = be32(cputype[i+1]) - a.cpusubtype = be32(cpusubtype[i+1]) - -- Subsequent slices overlap each other to share data. - ofs = ffi.offsetof(o, "arch") + i*ffi.sizeof(o.arch[0]) - a.offset = be32(ofs) - a.size = be32(mach_size-ofs+#s) - end - local a = o.arch[i] - a.hdr.magic = is64 and 0xfeedfacf or 0xfeedface - a.hdr.cputype = cputype[i+1] - a.hdr.cpusubtype = cpusubtype[i+1] - a.hdr.filetype = 1 - a.hdr.ncmds = 2 - a.hdr.sizeofcmds = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)+ffi.sizeof(a.sym) - a.seg.cmd = is64 and 0x19 or 0x1 - a.seg.cmdsize = ffi.sizeof(a.seg)+ffi.sizeof(a.sec) - a.seg.vmsize = #s - a.seg.fileoff = mach_size-ofs - a.seg.filesize = #s - a.seg.maxprot = 1 - a.seg.initprot = 1 - a.seg.nsects = 1 - ffi.copy(a.sec.sectname, "__data") - ffi.copy(a.sec.segname, "__DATA") - a.sec.size = #s - a.sec.offset = mach_size-ofs - a.sym.cmd = 2 - a.sym.cmdsize = ffi.sizeof(a.sym) - a.sym.symoff = ffi.offsetof(o, "sym_entry")-ofs - a.sym.nsyms = 1 - a.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)-ofs - a.sym.strsize = aligned(#symname+2, align) - end - o.sym_entry.type = 0xf - o.sym_entry.sect = 1 - o.sym_entry.strx = 1 - ffi.copy(o.space+1, symname) - - -- Write Macho-O object file. - local fp = savefile(output, "wb") - fp:write(ffi.string(o, mach_size)) - bcsave_tail(fp, output, s) -end - -local function bcsave_obj(ctx, output, s) - local ok, ffi = pcall(require, "ffi") - check(ok, "FFI library required to write this file type") - if output == "-" and jit.os == "Windows" then - set_stdout_binary(ffi) - end - if ctx.os == "windows" then - return bcsave_peobj(ctx, output, s, ffi) - elseif ctx.os == "osx" then - return bcsave_machobj(ctx, output, s, ffi) - else - return bcsave_elfobj(ctx, output, s, ffi) - end -end - ------------------------------------------------------------------------------- - -local function bclist(ctx, input, output) - local f = readfile(ctx, input) - require("jit.bc").dump(f, savefile(output, "w"), true) -end - -local function bcsave(ctx, input, output) - local f = readfile(ctx, input) - local s = string.dump(f, ctx.strip) - local t = ctx.type - if not t then - t = detecttype(output) - ctx.type = t - end - if t == "raw" then - bcsave_raw(output, s) - else - if not ctx.modname then ctx.modname = detectmodname(input) end - if t == "obj" then - bcsave_obj(ctx, output, s) - else - bcsave_c(ctx, output, s) - end - end -end - -local function docmd(...) - local arg = {...} - local n = 1 - local list = false - local ctx = { - strip = true, arch = jit.arch, os = jit.os:lower(), - type = false, modname = false, - } - while n <= #arg do - local a = arg[n] - if type(a) == "string" and a:sub(1, 1) == "-" and a ~= "-" then - tremove(arg, n) - if a == "--" then break end - for m=2,#a do - local opt = a:sub(m, m) - if opt == "l" then - list = true - elseif opt == "s" then - ctx.strip = true - elseif opt == "g" then - ctx.strip = false - else - if arg[n] == nil or m ~= #a then usage() end - if opt == "e" then - if n ~= 1 then usage() end - arg[1] = check(loadstring(arg[1])) - elseif opt == "n" then - ctx.modname = checkmodname(tremove(arg, n)) - elseif opt == "t" then - ctx.type = checkarg(tremove(arg, n), map_type, "file type") - elseif opt == "a" then - ctx.arch = checkarg(tremove(arg, n), map_arch, "architecture") - elseif opt == "o" then - ctx.os = checkarg(tremove(arg, n), map_os, "OS name") - elseif opt == "F" then - ctx.filename = "@"..tremove(arg, n) - else - usage() - end - end - end - else - n = n + 1 - end - end - if list then - if #arg == 0 or #arg > 2 then usage() end - bclist(ctx, arg[1], arg[2] or "-") - else - if #arg ~= 2 then usage() end - bcsave(ctx, arg[1], arg[2]) - end -end - ------------------------------------------------------------------------------- - --- Public module functions. -return { - start = docmd -- Process -b command line option. -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm.lua deleted file mode 100644 index 18ab68d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm.lua +++ /dev/null @@ -1,689 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT ARM disassembler module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles most user-mode ARMv7 instructions --- NYI: Advanced SIMD and VFP instructions. ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch = string.match, string.gmatch -local concat = table.concat -local bit = require("bit") -local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift - ------------------------------------------------------------------------------- --- Opcode maps ------------------------------------------------------------------------------- - -local map_loadc = { - shift = 8, mask = 15, - [10] = { - shift = 20, mask = 1, - [0] = { - shift = 23, mask = 3, - [0] = "vmovFmDN", "vstmFNdr", - _ = { - shift = 21, mask = 1, - [0] = "vstrFdl", - { shift = 16, mask = 15, [13] = "vpushFdr", _ = "vstmdbFNdr", } - }, - }, - { - shift = 23, mask = 3, - [0] = "vmovFDNm", - { shift = 16, mask = 15, [13] = "vpopFdr", _ = "vldmFNdr", }, - _ = { - shift = 21, mask = 1, - [0] = "vldrFdl", "vldmdbFNdr", - }, - }, - }, - [11] = { - shift = 20, mask = 1, - [0] = { - shift = 23, mask = 3, - [0] = "vmovGmDN", "vstmGNdr", - _ = { - shift = 21, mask = 1, - [0] = "vstrGdl", - { shift = 16, mask = 15, [13] = "vpushGdr", _ = "vstmdbGNdr", } - }, - }, - { - shift = 23, mask = 3, - [0] = "vmovGDNm", - { shift = 16, mask = 15, [13] = "vpopGdr", _ = "vldmGNdr", }, - _ = { - shift = 21, mask = 1, - [0] = "vldrGdl", "vldmdbGNdr", - }, - }, - }, - _ = { - shift = 0, mask = 0 -- NYI ldc, mcrr, mrrc. - }, -} - -local map_vfps = { - shift = 6, mask = 0x2c001, - [0] = "vmlaF.dnm", "vmlsF.dnm", - [0x04000] = "vnmlsF.dnm", [0x04001] = "vnmlaF.dnm", - [0x08000] = "vmulF.dnm", [0x08001] = "vnmulF.dnm", - [0x0c000] = "vaddF.dnm", [0x0c001] = "vsubF.dnm", - [0x20000] = "vdivF.dnm", - [0x24000] = "vfnmsF.dnm", [0x24001] = "vfnmaF.dnm", - [0x28000] = "vfmaF.dnm", [0x28001] = "vfmsF.dnm", - [0x2c000] = "vmovF.dY", - [0x2c001] = { - shift = 7, mask = 0x1e01, - [0] = "vmovF.dm", "vabsF.dm", - [0x0200] = "vnegF.dm", [0x0201] = "vsqrtF.dm", - [0x0800] = "vcmpF.dm", [0x0801] = "vcmpeF.dm", - [0x0a00] = "vcmpzF.d", [0x0a01] = "vcmpzeF.d", - [0x0e01] = "vcvtG.dF.m", - [0x1000] = "vcvt.f32.u32Fdm", [0x1001] = "vcvt.f32.s32Fdm", - [0x1800] = "vcvtr.u32F.dm", [0x1801] = "vcvt.u32F.dm", - [0x1a00] = "vcvtr.s32F.dm", [0x1a01] = "vcvt.s32F.dm", - }, -} - -local map_vfpd = { - shift = 6, mask = 0x2c001, - [0] = "vmlaG.dnm", "vmlsG.dnm", - [0x04000] = "vnmlsG.dnm", [0x04001] = "vnmlaG.dnm", - [0x08000] = "vmulG.dnm", [0x08001] = "vnmulG.dnm", - [0x0c000] = "vaddG.dnm", [0x0c001] = "vsubG.dnm", - [0x20000] = "vdivG.dnm", - [0x24000] = "vfnmsG.dnm", [0x24001] = "vfnmaG.dnm", - [0x28000] = "vfmaG.dnm", [0x28001] = "vfmsG.dnm", - [0x2c000] = "vmovG.dY", - [0x2c001] = { - shift = 7, mask = 0x1e01, - [0] = "vmovG.dm", "vabsG.dm", - [0x0200] = "vnegG.dm", [0x0201] = "vsqrtG.dm", - [0x0800] = "vcmpG.dm", [0x0801] = "vcmpeG.dm", - [0x0a00] = "vcmpzG.d", [0x0a01] = "vcmpzeG.d", - [0x0e01] = "vcvtF.dG.m", - [0x1000] = "vcvt.f64.u32GdFm", [0x1001] = "vcvt.f64.s32GdFm", - [0x1800] = "vcvtr.u32FdG.m", [0x1801] = "vcvt.u32FdG.m", - [0x1a00] = "vcvtr.s32FdG.m", [0x1a01] = "vcvt.s32FdG.m", - }, -} - -local map_datac = { - shift = 24, mask = 1, - [0] = { - shift = 4, mask = 1, - [0] = { - shift = 8, mask = 15, - [10] = map_vfps, - [11] = map_vfpd, - -- NYI cdp, mcr, mrc. - }, - { - shift = 8, mask = 15, - [10] = { - shift = 20, mask = 15, - [0] = "vmovFnD", "vmovFDn", - [14] = "vmsrD", - [15] = { shift = 12, mask = 15, [15] = "vmrs", _ = "vmrsD", }, - }, - }, - }, - "svcT", -} - -local map_loadcu = { - shift = 0, mask = 0, -- NYI unconditional CP load/store. -} - -local map_datacu = { - shift = 0, mask = 0, -- NYI unconditional CP data. -} - -local map_simddata = { - shift = 0, mask = 0, -- NYI SIMD data. -} - -local map_simdload = { - shift = 0, mask = 0, -- NYI SIMD load/store, preload. -} - -local map_preload = { - shift = 0, mask = 0, -- NYI preload. -} - -local map_media = { - shift = 20, mask = 31, - [0] = false, - { --01 - shift = 5, mask = 7, - [0] = "sadd16DNM", "sasxDNM", "ssaxDNM", "ssub16DNM", - "sadd8DNM", false, false, "ssub8DNM", - }, - { --02 - shift = 5, mask = 7, - [0] = "qadd16DNM", "qasxDNM", "qsaxDNM", "qsub16DNM", - "qadd8DNM", false, false, "qsub8DNM", - }, - { --03 - shift = 5, mask = 7, - [0] = "shadd16DNM", "shasxDNM", "shsaxDNM", "shsub16DNM", - "shadd8DNM", false, false, "shsub8DNM", - }, - false, - { --05 - shift = 5, mask = 7, - [0] = "uadd16DNM", "uasxDNM", "usaxDNM", "usub16DNM", - "uadd8DNM", false, false, "usub8DNM", - }, - { --06 - shift = 5, mask = 7, - [0] = "uqadd16DNM", "uqasxDNM", "uqsaxDNM", "uqsub16DNM", - "uqadd8DNM", false, false, "uqsub8DNM", - }, - { --07 - shift = 5, mask = 7, - [0] = "uhadd16DNM", "uhasxDNM", "uhsaxDNM", "uhsub16DNM", - "uhadd8DNM", false, false, "uhsub8DNM", - }, - { --08 - shift = 5, mask = 7, - [0] = "pkhbtDNMU", false, "pkhtbDNMU", - { shift = 16, mask = 15, [15] = "sxtb16DMU", _ = "sxtab16DNMU", }, - "pkhbtDNMU", "selDNM", "pkhtbDNMU", - }, - false, - { --0a - shift = 5, mask = 7, - [0] = "ssatDxMu", "ssat16DxM", "ssatDxMu", - { shift = 16, mask = 15, [15] = "sxtbDMU", _ = "sxtabDNMU", }, - "ssatDxMu", false, "ssatDxMu", - }, - { --0b - shift = 5, mask = 7, - [0] = "ssatDxMu", "revDM", "ssatDxMu", - { shift = 16, mask = 15, [15] = "sxthDMU", _ = "sxtahDNMU", }, - "ssatDxMu", "rev16DM", "ssatDxMu", - }, - { --0c - shift = 5, mask = 7, - [3] = { shift = 16, mask = 15, [15] = "uxtb16DMU", _ = "uxtab16DNMU", }, - }, - false, - { --0e - shift = 5, mask = 7, - [0] = "usatDwMu", "usat16DwM", "usatDwMu", - { shift = 16, mask = 15, [15] = "uxtbDMU", _ = "uxtabDNMU", }, - "usatDwMu", false, "usatDwMu", - }, - { --0f - shift = 5, mask = 7, - [0] = "usatDwMu", "rbitDM", "usatDwMu", - { shift = 16, mask = 15, [15] = "uxthDMU", _ = "uxtahDNMU", }, - "usatDwMu", "revshDM", "usatDwMu", - }, - { --10 - shift = 12, mask = 15, - [15] = { - shift = 5, mask = 7, - "smuadNMS", "smuadxNMS", "smusdNMS", "smusdxNMS", - }, - _ = { - shift = 5, mask = 7, - [0] = "smladNMSD", "smladxNMSD", "smlsdNMSD", "smlsdxNMSD", - }, - }, - false, false, false, - { --14 - shift = 5, mask = 7, - [0] = "smlaldDNMS", "smlaldxDNMS", "smlsldDNMS", "smlsldxDNMS", - }, - { --15 - shift = 5, mask = 7, - [0] = { shift = 12, mask = 15, [15] = "smmulNMS", _ = "smmlaNMSD", }, - { shift = 12, mask = 15, [15] = "smmulrNMS", _ = "smmlarNMSD", }, - false, false, false, false, - "smmlsNMSD", "smmlsrNMSD", - }, - false, false, - { --18 - shift = 5, mask = 7, - [0] = { shift = 12, mask = 15, [15] = "usad8NMS", _ = "usada8NMSD", }, - }, - false, - { --1a - shift = 5, mask = 3, [2] = "sbfxDMvw", - }, - { --1b - shift = 5, mask = 3, [2] = "sbfxDMvw", - }, - { --1c - shift = 5, mask = 3, - [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", }, - }, - { --1d - shift = 5, mask = 3, - [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", }, - }, - { --1e - shift = 5, mask = 3, [2] = "ubfxDMvw", - }, - { --1f - shift = 5, mask = 3, [2] = "ubfxDMvw", - }, -} - -local map_load = { - shift = 21, mask = 9, - { - shift = 20, mask = 5, - [0] = "strtDL", "ldrtDL", [4] = "strbtDL", [5] = "ldrbtDL", - }, - _ = { - shift = 20, mask = 5, - [0] = "strDL", "ldrDL", [4] = "strbDL", [5] = "ldrbDL", - } -} - -local map_load1 = { - shift = 4, mask = 1, - [0] = map_load, map_media, -} - -local map_loadm = { - shift = 20, mask = 1, - [0] = { - shift = 23, mask = 3, - [0] = "stmdaNR", "stmNR", - { shift = 16, mask = 63, [45] = "pushR", _ = "stmdbNR", }, "stmibNR", - }, - { - shift = 23, mask = 3, - [0] = "ldmdaNR", { shift = 16, mask = 63, [61] = "popR", _ = "ldmNR", }, - "ldmdbNR", "ldmibNR", - }, -} - -local map_data = { - shift = 21, mask = 15, - [0] = "andDNPs", "eorDNPs", "subDNPs", "rsbDNPs", - "addDNPs", "adcDNPs", "sbcDNPs", "rscDNPs", - "tstNP", "teqNP", "cmpNP", "cmnNP", - "orrDNPs", "movDPs", "bicDNPs", "mvnDPs", -} - -local map_mul = { - shift = 21, mask = 7, - [0] = "mulNMSs", "mlaNMSDs", "umaalDNMS", "mlsDNMS", - "umullDNMSs", "umlalDNMSs", "smullDNMSs", "smlalDNMSs", -} - -local map_sync = { - shift = 20, mask = 15, -- NYI: brackets around N. R(D+1) for ldrexd/strexd. - [0] = "swpDMN", false, false, false, - "swpbDMN", false, false, false, - "strexDMN", "ldrexDN", "strexdDN", "ldrexdDN", - "strexbDMN", "ldrexbDN", "strexhDN", "ldrexhDN", -} - -local map_mulh = { - shift = 21, mask = 3, - [0] = { shift = 5, mask = 3, - [0] = "smlabbNMSD", "smlatbNMSD", "smlabtNMSD", "smlattNMSD", }, - { shift = 5, mask = 3, - [0] = "smlawbNMSD", "smulwbNMS", "smlawtNMSD", "smulwtNMS", }, - { shift = 5, mask = 3, - [0] = "smlalbbDNMS", "smlaltbDNMS", "smlalbtDNMS", "smlalttDNMS", }, - { shift = 5, mask = 3, - [0] = "smulbbNMS", "smultbNMS", "smulbtNMS", "smulttNMS", }, -} - -local map_misc = { - shift = 4, mask = 7, - -- NYI: decode PSR bits of msr. - [0] = { shift = 21, mask = 1, [0] = "mrsD", "msrM", }, - { shift = 21, mask = 3, "bxM", false, "clzDM", }, - { shift = 21, mask = 3, "bxjM", }, - { shift = 21, mask = 3, "blxM", }, - false, - { shift = 21, mask = 3, [0] = "qaddDMN", "qsubDMN", "qdaddDMN", "qdsubDMN", }, - false, - { shift = 21, mask = 3, "bkptK", }, -} - -local map_datar = { - shift = 4, mask = 9, - [9] = { - shift = 5, mask = 3, - [0] = { shift = 24, mask = 1, [0] = map_mul, map_sync, }, - { shift = 20, mask = 1, [0] = "strhDL", "ldrhDL", }, - { shift = 20, mask = 1, [0] = "ldrdDL", "ldrsbDL", }, - { shift = 20, mask = 1, [0] = "strdDL", "ldrshDL", }, - }, - _ = { - shift = 20, mask = 25, - [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, }, - _ = { - shift = 0, mask = 0xffffffff, - [bor(0xe1a00000)] = "nop", - _ = map_data, - } - }, -} - -local map_datai = { - shift = 20, mask = 31, -- NYI: decode PSR bits of msr. Decode imm12. - [16] = "movwDW", [20] = "movtDW", - [18] = { shift = 0, mask = 0xf00ff, [0] = "nopv6", _ = "msrNW", }, - [22] = "msrNW", - _ = map_data, -} - -local map_branch = { - shift = 24, mask = 1, - [0] = "bB", "blB" -} - -local map_condins = { - [0] = map_datar, map_datai, map_load, map_load1, - map_loadm, map_branch, map_loadc, map_datac -} - --- NYI: setend. -local map_uncondins = { - [0] = false, map_simddata, map_simdload, map_preload, - false, "blxB", map_loadcu, map_datacu, -} - ------------------------------------------------------------------------------- - -local map_gpr = { - [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", -} - -local map_cond = { - [0] = "eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc", - "hi", "ls", "ge", "lt", "gt", "le", "al", -} - -local map_shift = { [0] = "lsl", "lsr", "asr", "ror", } - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then - extra = "\t->"..sym - elseif band(ctx.op, 0x0e000000) ~= 0x0a000000 then - extra = "\t; 0x"..tohex(ctx.rel) - end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-5s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-5s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - --- Format operand 2 of load/store opcodes. -local function fmtload(ctx, op, pos) - local base = map_gpr[band(rshift(op, 16), 15)] - local x, ofs - local ext = (band(op, 0x04000000) == 0) - if not ext and band(op, 0x02000000) == 0 then - ofs = band(op, 4095) - if band(op, 0x00800000) == 0 then ofs = -ofs end - if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end - ofs = "#"..ofs - elseif ext and band(op, 0x00400000) ~= 0 then - ofs = band(op, 15) + band(rshift(op, 4), 0xf0) - if band(op, 0x00800000) == 0 then ofs = -ofs end - if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end - ofs = "#"..ofs - else - ofs = map_gpr[band(op, 15)] - if ext or band(op, 0xfe0) == 0 then - elseif band(op, 0xfe0) == 0x60 then - ofs = format("%s, rrx", ofs) - else - local sh = band(rshift(op, 7), 31) - if sh == 0 then sh = 32 end - ofs = format("%s, %s #%d", ofs, map_shift[band(rshift(op, 5), 3)], sh) - end - if band(op, 0x00800000) == 0 then ofs = "-"..ofs end - end - if ofs == "#0" then - x = format("[%s]", base) - elseif band(op, 0x01000000) == 0 then - x = format("[%s], %s", base, ofs) - else - x = format("[%s, %s]", base, ofs) - end - if band(op, 0x01200000) == 0x01200000 then x = x.."!" end - return x -end - --- Format operand 2 of vector load/store opcodes. -local function fmtvload(ctx, op, pos) - local base = map_gpr[band(rshift(op, 16), 15)] - local ofs = band(op, 255)*4 - if band(op, 0x00800000) == 0 then ofs = -ofs end - if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end - if ofs == 0 then - return format("[%s]", base) - else - return format("[%s, #%d]", base, ofs) - end -end - -local function fmtvr(op, vr, sh0, sh1) - if vr == "s" then - return format("s%d", 2*band(rshift(op, sh0), 15)+band(rshift(op, sh1), 1)) - else - return format("d%d", band(rshift(op, sh0), 15)+band(rshift(op, sh1-4), 16)) - end -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) - local operands = {} - local suffix = "" - local last, name, pat - local vr - ctx.op = op - ctx.rel = nil - - local cond = rshift(op, 28) - local opat - if cond == 15 then - opat = map_uncondins[band(rshift(op, 25), 7)] - else - if cond ~= 14 then suffix = map_cond[cond] end - opat = map_condins[band(rshift(op, 25), 7)] - end - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ - end - name, pat = match(opat, "^([a-z0-9]*)(.*)") - if sub(pat, 1, 1) == "." then - local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") - suffix = suffix..s2 - pat = p2 - end - - for p in gmatch(pat, ".") do - local x = nil - if p == "D" then - x = map_gpr[band(rshift(op, 12), 15)] - elseif p == "N" then - x = map_gpr[band(rshift(op, 16), 15)] - elseif p == "S" then - x = map_gpr[band(rshift(op, 8), 15)] - elseif p == "M" then - x = map_gpr[band(op, 15)] - elseif p == "d" then - x = fmtvr(op, vr, 12, 22) - elseif p == "n" then - x = fmtvr(op, vr, 16, 7) - elseif p == "m" then - x = fmtvr(op, vr, 0, 5) - elseif p == "P" then - if band(op, 0x02000000) ~= 0 then - x = ror(band(op, 255), 2*band(rshift(op, 8), 15)) - else - x = map_gpr[band(op, 15)] - if band(op, 0xff0) ~= 0 then - operands[#operands+1] = x - local s = map_shift[band(rshift(op, 5), 3)] - local r = nil - if band(op, 0xf90) == 0 then - if s == "ror" then s = "rrx" else r = "#32" end - elseif band(op, 0x10) == 0 then - r = "#"..band(rshift(op, 7), 31) - else - r = map_gpr[band(rshift(op, 8), 15)] - end - if name == "mov" then name = s; x = r - elseif r then x = format("%s %s", s, r) - else x = s end - end - end - elseif p == "L" then - x = fmtload(ctx, op, pos) - elseif p == "l" then - x = fmtvload(ctx, op, pos) - elseif p == "B" then - local addr = ctx.addr + pos + 8 + arshift(lshift(op, 8), 6) - if cond == 15 then addr = addr + band(rshift(op, 23), 2) end - ctx.rel = addr - x = "0x"..tohex(addr) - elseif p == "F" then - vr = "s" - elseif p == "G" then - vr = "d" - elseif p == "." then - suffix = suffix..(vr == "s" and ".f32" or ".f64") - elseif p == "R" then - if band(op, 0x00200000) ~= 0 and #operands == 1 then - operands[1] = operands[1].."!" - end - local t = {} - for i=0,15 do - if band(rshift(op, i), 1) == 1 then t[#t+1] = map_gpr[i] end - end - x = "{"..concat(t, ", ").."}" - elseif p == "r" then - if band(op, 0x00200000) ~= 0 and #operands == 2 then - operands[1] = operands[1].."!" - end - local s = tonumber(sub(last, 2)) - local n = band(op, 255) - if vr == "d" then n = rshift(n, 1) end - operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1) - elseif p == "W" then - x = band(op, 0x0fff) + band(rshift(op, 4), 0xf000) - elseif p == "T" then - x = "#0x"..tohex(band(op, 0x00ffffff), 6) - elseif p == "U" then - x = band(rshift(op, 7), 31) - if x == 0 then x = nil end - elseif p == "u" then - x = band(rshift(op, 7), 31) - if band(op, 0x40) == 0 then - if x == 0 then x = nil else x = "lsl #"..x end - else - if x == 0 then x = "asr #32" else x = "asr #"..x end - end - elseif p == "v" then - x = band(rshift(op, 7), 31) - elseif p == "w" then - x = band(rshift(op, 16), 31) - elseif p == "x" then - x = band(rshift(op, 16), 31) + 1 - elseif p == "X" then - x = band(rshift(op, 16), 31) - last + 1 - elseif p == "Y" then - x = band(rshift(op, 12), 0xf0) + band(op, 0x0f) - elseif p == "K" then - x = "#0x"..tohex(band(rshift(op, 4), 0x0000fff0) + band(op, 15), 4) - elseif p == "s" then - if band(op, 0x00100000) ~= 0 then suffix = "s"..suffix end - else - assert(false) - end - if x then - last = x - if type(x) == "number" then x = "#"..x end - operands[#operands+1] = x - end - end - - return putop(ctx, name..suffix, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - ctx.pos = ofs - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass(code, addr, out) - create(code, addr, out):disass() -end - --- Return register name for RID. -local function regname(r) - if r < 16 then return map_gpr[r] end - return "d"..(r-16) -end - --- Public module functions. -return { - create = create, - disass = disass, - regname = regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64.lua deleted file mode 100644 index 531584a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64.lua +++ /dev/null @@ -1,1216 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT ARM64 disassembler module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h --- --- Contributed by Djordje Kovacevic and Stefan Pejic from RT-RK.com. --- Sponsored by Cisco Systems, Inc. ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles most user-mode AArch64 instructions. --- NYI: Advanced SIMD and VFP instructions. ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local concat = table.concat -local bit = require("bit") -local band, bor, bxor, tohex = bit.band, bit.bor, bit.bxor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift -local ror = bit.ror - ------------------------------------------------------------------------------- --- Opcode maps ------------------------------------------------------------------------------- - -local map_adr = { -- PC-relative addressing. - shift = 31, mask = 1, - [0] = "adrDBx", "adrpDBx" -} - -local map_addsubi = { -- Add/subtract immediate. - shift = 29, mask = 3, - [0] = "add|movDNIg", "adds|cmnD0NIg", "subDNIg", "subs|cmpD0NIg", -} - -local map_logi = { -- Logical immediate. - shift = 31, mask = 1, - [0] = { - shift = 22, mask = 1, - [0] = { - shift = 29, mask = 3, - [0] = "andDNig", "orr|movDN0ig", "eorDNig", "ands|tstD0Nig" - }, - false -- unallocated - }, - { - shift = 29, mask = 3, - [0] = "andDNig", "orr|movDN0ig", "eorDNig", "ands|tstD0Nig" - } -} - -local map_movwi = { -- Move wide immediate. - shift = 31, mask = 1, - [0] = { - shift = 22, mask = 1, - [0] = { - shift = 29, mask = 3, - [0] = "movnDWRg", false, "movz|movDYRg", "movkDWRg" - }, false -- unallocated - }, - { - shift = 29, mask = 3, - [0] = "movnDWRg", false, "movz|movDYRg", "movkDWRg" - }, -} - -local map_bitf = { -- Bitfield. - shift = 31, mask = 1, - [0] = { - shift = 22, mask = 1, - [0] = { - shift = 29, mask = 3, - [0] = "sbfm|sbfiz|sbfx|asr|sxtw|sxth|sxtbDN12w", - "bfm|bfi|bfxilDN13w", - "ubfm|ubfiz|ubfx|lsr|lsl|uxth|uxtbDN12w" - } - }, - { - shift = 22, mask = 1, - { - shift = 29, mask = 3, - [0] = "sbfm|sbfiz|sbfx|asr|sxtw|sxth|sxtbDN12x", - "bfm|bfi|bfxilDN13x", - "ubfm|ubfiz|ubfx|lsr|lsl|uxth|uxtbDN12x" - } - } -} - -local map_datai = { -- Data processing - immediate. - shift = 23, mask = 7, - [0] = map_adr, map_adr, map_addsubi, false, - map_logi, map_movwi, map_bitf, - { - shift = 15, mask = 0x1c0c1, - [0] = "extr|rorDNM4w", [0x10080] = "extr|rorDNM4x", - [0x10081] = "extr|rorDNM4x" - } -} - -local map_logsr = { -- Logical, shifted register. - shift = 31, mask = 1, - [0] = { - shift = 15, mask = 1, - [0] = { - shift = 29, mask = 3, - [0] = { - shift = 21, mask = 7, - [0] = "andDNMSg", "bicDNMSg", "andDNMSg", "bicDNMSg", - "andDNMSg", "bicDNMSg", "andDNMg", "bicDNMg" - }, - { - shift = 21, mask = 7, - [0] ="orr|movDN0MSg", "orn|mvnDN0MSg", "orr|movDN0MSg", "orn|mvnDN0MSg", - "orr|movDN0MSg", "orn|mvnDN0MSg", "orr|movDN0Mg", "orn|mvnDN0Mg" - }, - { - shift = 21, mask = 7, - [0] = "eorDNMSg", "eonDNMSg", "eorDNMSg", "eonDNMSg", - "eorDNMSg", "eonDNMSg", "eorDNMg", "eonDNMg" - }, - { - shift = 21, mask = 7, - [0] = "ands|tstD0NMSg", "bicsDNMSg", "ands|tstD0NMSg", "bicsDNMSg", - "ands|tstD0NMSg", "bicsDNMSg", "ands|tstD0NMg", "bicsDNMg" - } - }, - false -- unallocated - }, - { - shift = 29, mask = 3, - [0] = { - shift = 21, mask = 7, - [0] = "andDNMSg", "bicDNMSg", "andDNMSg", "bicDNMSg", - "andDNMSg", "bicDNMSg", "andDNMg", "bicDNMg" - }, - { - shift = 21, mask = 7, - [0] = "orr|movDN0MSg", "orn|mvnDN0MSg", "orr|movDN0MSg", "orn|mvnDN0MSg", - "orr|movDN0MSg", "orn|mvnDN0MSg", "orr|movDN0Mg", "orn|mvnDN0Mg" - }, - { - shift = 21, mask = 7, - [0] = "eorDNMSg", "eonDNMSg", "eorDNMSg", "eonDNMSg", - "eorDNMSg", "eonDNMSg", "eorDNMg", "eonDNMg" - }, - { - shift = 21, mask = 7, - [0] = "ands|tstD0NMSg", "bicsDNMSg", "ands|tstD0NMSg", "bicsDNMSg", - "ands|tstD0NMSg", "bicsDNMSg", "ands|tstD0NMg", "bicsDNMg" - } - } -} - -local map_assh = { - shift = 31, mask = 1, - [0] = { - shift = 15, mask = 1, - [0] = { - shift = 29, mask = 3, - [0] = { - shift = 22, mask = 3, - [0] = "addDNMSg", "addDNMSg", "addDNMSg", "addDNMg" - }, - { - shift = 22, mask = 3, - [0] = "adds|cmnD0NMSg", "adds|cmnD0NMSg", - "adds|cmnD0NMSg", "adds|cmnD0NMg" - }, - { - shift = 22, mask = 3, - [0] = "sub|negDN0MSg", "sub|negDN0MSg", "sub|negDN0MSg", "sub|negDN0Mg" - }, - { - shift = 22, mask = 3, - [0] = "subs|cmp|negsD0N0MzSg", "subs|cmp|negsD0N0MzSg", - "subs|cmp|negsD0N0MzSg", "subs|cmp|negsD0N0Mzg" - }, - }, - false -- unallocated - }, - { - shift = 29, mask = 3, - [0] = { - shift = 22, mask = 3, - [0] = "addDNMSg", "addDNMSg", "addDNMSg", "addDNMg" - }, - { - shift = 22, mask = 3, - [0] = "adds|cmnD0NMSg", "adds|cmnD0NMSg", "adds|cmnD0NMSg", - "adds|cmnD0NMg" - }, - { - shift = 22, mask = 3, - [0] = "sub|negDN0MSg", "sub|negDN0MSg", "sub|negDN0MSg", "sub|negDN0Mg" - }, - { - shift = 22, mask = 3, - [0] = "subs|cmp|negsD0N0MzSg", "subs|cmp|negsD0N0MzSg", - "subs|cmp|negsD0N0MzSg", "subs|cmp|negsD0N0Mzg" - } - } -} - -local map_addsubsh = { -- Add/subtract, shifted register. - shift = 22, mask = 3, - [0] = map_assh, map_assh, map_assh -} - -local map_addsubex = { -- Add/subtract, extended register. - shift = 22, mask = 3, - [0] = { - shift = 29, mask = 3, - [0] = "addDNMXg", "adds|cmnD0NMXg", "subDNMXg", "subs|cmpD0NMzXg", - } -} - -local map_addsubc = { -- Add/subtract, with carry. - shift = 10, mask = 63, - [0] = { - shift = 29, mask = 3, - [0] = "adcDNMg", "adcsDNMg", "sbc|ngcDN0Mg", "sbcs|ngcsDN0Mg", - } -} - -local map_ccomp = { - shift = 4, mask = 1, - [0] = { - shift = 10, mask = 3, - [0] = { -- Conditional compare register. - shift = 29, mask = 3, - "ccmnNMVCg", false, "ccmpNMVCg", - }, - [2] = { -- Conditional compare immediate. - shift = 29, mask = 3, - "ccmnN5VCg", false, "ccmpN5VCg", - } - } -} - -local map_csel = { -- Conditional select. - shift = 11, mask = 1, - [0] = { - shift = 10, mask = 1, - [0] = { - shift = 29, mask = 3, - [0] = "cselDNMzCg", false, "csinv|cinv|csetmDNMcg", false, - }, - { - shift = 29, mask = 3, - [0] = "csinc|cinc|csetDNMcg", false, "csneg|cnegDNMcg", false, - } - } -} - -local map_data1s = { -- Data processing, 1 source. - shift = 29, mask = 1, - [0] = { - shift = 31, mask = 1, - [0] = { - shift = 10, mask = 0x7ff, - [0] = "rbitDNg", "rev16DNg", "revDNw", false, "clzDNg", "clsDNg" - }, - { - shift = 10, mask = 0x7ff, - [0] = "rbitDNg", "rev16DNg", "rev32DNx", "revDNx", "clzDNg", "clsDNg" - } - } -} - -local map_data2s = { -- Data processing, 2 sources. - shift = 29, mask = 1, - [0] = { - shift = 10, mask = 63, - false, "udivDNMg", "sdivDNMg", false, false, false, false, "lslDNMg", - "lsrDNMg", "asrDNMg", "rorDNMg" - } -} - -local map_data3s = { -- Data processing, 3 sources. - shift = 29, mask = 7, - [0] = { - shift = 21, mask = 7, - [0] = { - shift = 15, mask = 1, - [0] = "madd|mulDNMA0g", "msub|mnegDNMA0g" - } - }, false, false, false, - { - shift = 15, mask = 1, - [0] = { - shift = 21, mask = 7, - [0] = "madd|mulDNMA0g", "smaddl|smullDxNMwA0x", "smulhDNMx", false, - false, "umaddl|umullDxNMwA0x", "umulhDNMx" - }, - { - shift = 21, mask = 7, - [0] = "msub|mnegDNMA0g", "smsubl|smneglDxNMwA0x", false, false, - false, "umsubl|umneglDxNMwA0x" - } - } -} - -local map_datar = { -- Data processing, register. - shift = 28, mask = 1, - [0] = { - shift = 24, mask = 1, - [0] = map_logsr, - { - shift = 21, mask = 1, - [0] = map_addsubsh, map_addsubex - } - }, - { - shift = 21, mask = 15, - [0] = map_addsubc, false, map_ccomp, false, map_csel, false, - { - shift = 30, mask = 1, - [0] = map_data2s, map_data1s - }, - false, map_data3s, map_data3s, map_data3s, map_data3s, map_data3s, - map_data3s, map_data3s, map_data3s - } -} - -local map_lrl = { -- Load register, literal. - shift = 26, mask = 1, - [0] = { - shift = 30, mask = 3, - [0] = "ldrDwB", "ldrDxB", "ldrswDxB" - }, - { - shift = 30, mask = 3, - [0] = "ldrDsB", "ldrDdB" - } -} - -local map_lsriind = { -- Load/store register, immediate pre/post-indexed. - shift = 30, mask = 3, - [0] = { - shift = 26, mask = 1, - [0] = { - shift = 22, mask = 3, - [0] = "strbDwzL", "ldrbDwzL", "ldrsbDxzL", "ldrsbDwzL" - } - }, - { - shift = 26, mask = 1, - [0] = { - shift = 22, mask = 3, - [0] = "strhDwzL", "ldrhDwzL", "ldrshDxzL", "ldrshDwzL" - } - }, - { - shift = 26, mask = 1, - [0] = { - shift = 22, mask = 3, - [0] = "strDwzL", "ldrDwzL", "ldrswDxzL" - }, - { - shift = 22, mask = 3, - [0] = "strDszL", "ldrDszL" - } - }, - { - shift = 26, mask = 1, - [0] = { - shift = 22, mask = 3, - [0] = "strDxzL", "ldrDxzL" - }, - { - shift = 22, mask = 3, - [0] = "strDdzL", "ldrDdzL" - } - } -} - -local map_lsriro = { - shift = 21, mask = 1, - [0] = { -- Load/store register immediate. - shift = 10, mask = 3, - [0] = { -- Unscaled immediate. - shift = 26, mask = 1, - [0] = { - shift = 30, mask = 3, - [0] = { - shift = 22, mask = 3, - [0] = "sturbDwK", "ldurbDwK" - }, - { - shift = 22, mask = 3, - [0] = "sturhDwK", "ldurhDwK" - }, - { - shift = 22, mask = 3, - [0] = "sturDwK", "ldurDwK" - }, - { - shift = 22, mask = 3, - [0] = "sturDxK", "ldurDxK" - } - } - }, map_lsriind, false, map_lsriind - }, - { -- Load/store register, register offset. - shift = 10, mask = 3, - [2] = { - shift = 26, mask = 1, - [0] = { - shift = 30, mask = 3, - [0] = { - shift = 22, mask = 3, - [0] = "strbDwO", "ldrbDwO", "ldrsbDxO", "ldrsbDwO" - }, - { - shift = 22, mask = 3, - [0] = "strhDwO", "ldrhDwO", "ldrshDxO", "ldrshDwO" - }, - { - shift = 22, mask = 3, - [0] = "strDwO", "ldrDwO", "ldrswDxO" - }, - { - shift = 22, mask = 3, - [0] = "strDxO", "ldrDxO" - } - }, - { - shift = 30, mask = 3, - [2] = { - shift = 22, mask = 3, - [0] = "strDsO", "ldrDsO" - }, - [3] = { - shift = 22, mask = 3, - [0] = "strDdO", "ldrDdO" - } - } - } - } -} - -local map_lsp = { -- Load/store register pair, offset. - shift = 22, mask = 1, - [0] = { - shift = 30, mask = 3, - [0] = { - shift = 26, mask = 1, - [0] = "stpDzAzwP", "stpDzAzsP", - }, - { - shift = 26, mask = 1, - "stpDzAzdP" - }, - { - shift = 26, mask = 1, - [0] = "stpDzAzxP" - } - }, - { - shift = 30, mask = 3, - [0] = { - shift = 26, mask = 1, - [0] = "ldpDzAzwP", "ldpDzAzsP", - }, - { - shift = 26, mask = 1, - [0] = "ldpswDAxP", "ldpDzAzdP" - }, - { - shift = 26, mask = 1, - [0] = "ldpDzAzxP" - } - } -} - -local map_ls = { -- Loads and stores. - shift = 24, mask = 0x31, - [0x10] = map_lrl, [0x30] = map_lsriro, - [0x20] = { - shift = 23, mask = 3, - map_lsp, map_lsp, map_lsp - }, - [0x21] = { - shift = 23, mask = 3, - map_lsp, map_lsp, map_lsp - }, - [0x31] = { - shift = 26, mask = 1, - [0] = { - shift = 30, mask = 3, - [0] = { - shift = 22, mask = 3, - [0] = "strbDwzU", "ldrbDwzU" - }, - { - shift = 22, mask = 3, - [0] = "strhDwzU", "ldrhDwzU" - }, - { - shift = 22, mask = 3, - [0] = "strDwzU", "ldrDwzU" - }, - { - shift = 22, mask = 3, - [0] = "strDxzU", "ldrDxzU" - } - }, - { - shift = 30, mask = 3, - [2] = { - shift = 22, mask = 3, - [0] = "strDszU", "ldrDszU" - }, - [3] = { - shift = 22, mask = 3, - [0] = "strDdzU", "ldrDdzU" - } - } - }, -} - -local map_datafp = { -- Data processing, SIMD and FP. - shift = 28, mask = 7, - { -- 001 - shift = 24, mask = 1, - [0] = { - shift = 21, mask = 1, - { - shift = 10, mask = 3, - [0] = { - shift = 12, mask = 1, - [0] = { - shift = 13, mask = 1, - [0] = { - shift = 14, mask = 1, - [0] = { - shift = 15, mask = 1, - [0] = { -- FP/int conversion. - shift = 31, mask = 1, - [0] = { - shift = 16, mask = 0xff, - [0x20] = "fcvtnsDwNs", [0x21] = "fcvtnuDwNs", - [0x22] = "scvtfDsNw", [0x23] = "ucvtfDsNw", - [0x24] = "fcvtasDwNs", [0x25] = "fcvtauDwNs", - [0x26] = "fmovDwNs", [0x27] = "fmovDsNw", - [0x28] = "fcvtpsDwNs", [0x29] = "fcvtpuDwNs", - [0x30] = "fcvtmsDwNs", [0x31] = "fcvtmuDwNs", - [0x38] = "fcvtzsDwNs", [0x39] = "fcvtzuDwNs", - [0x60] = "fcvtnsDwNd", [0x61] = "fcvtnuDwNd", - [0x62] = "scvtfDdNw", [0x63] = "ucvtfDdNw", - [0x64] = "fcvtasDwNd", [0x65] = "fcvtauDwNd", - [0x68] = "fcvtpsDwNd", [0x69] = "fcvtpuDwNd", - [0x70] = "fcvtmsDwNd", [0x71] = "fcvtmuDwNd", - [0x78] = "fcvtzsDwNd", [0x79] = "fcvtzuDwNd" - }, - { - shift = 16, mask = 0xff, - [0x20] = "fcvtnsDxNs", [0x21] = "fcvtnuDxNs", - [0x22] = "scvtfDsNx", [0x23] = "ucvtfDsNx", - [0x24] = "fcvtasDxNs", [0x25] = "fcvtauDxNs", - [0x28] = "fcvtpsDxNs", [0x29] = "fcvtpuDxNs", - [0x30] = "fcvtmsDxNs", [0x31] = "fcvtmuDxNs", - [0x38] = "fcvtzsDxNs", [0x39] = "fcvtzuDxNs", - [0x60] = "fcvtnsDxNd", [0x61] = "fcvtnuDxNd", - [0x62] = "scvtfDdNx", [0x63] = "ucvtfDdNx", - [0x64] = "fcvtasDxNd", [0x65] = "fcvtauDxNd", - [0x66] = "fmovDxNd", [0x67] = "fmovDdNx", - [0x68] = "fcvtpsDxNd", [0x69] = "fcvtpuDxNd", - [0x70] = "fcvtmsDxNd", [0x71] = "fcvtmuDxNd", - [0x78] = "fcvtzsDxNd", [0x79] = "fcvtzuDxNd" - } - } - }, - { -- FP data-processing, 1 source. - shift = 31, mask = 1, - [0] = { - shift = 22, mask = 3, - [0] = { - shift = 15, mask = 63, - [0] = "fmovDNf", "fabsDNf", "fnegDNf", - "fsqrtDNf", false, "fcvtDdNs", false, false, - "frintnDNf", "frintpDNf", "frintmDNf", "frintzDNf", - "frintaDNf", false, "frintxDNf", "frintiDNf", - }, - { - shift = 15, mask = 63, - [0] = "fmovDNf", "fabsDNf", "fnegDNf", - "fsqrtDNf", "fcvtDsNd", false, false, false, - "frintnDNf", "frintpDNf", "frintmDNf", "frintzDNf", - "frintaDNf", false, "frintxDNf", "frintiDNf", - } - } - } - }, - { -- FP compare. - shift = 31, mask = 1, - [0] = { - shift = 14, mask = 3, - [0] = { - shift = 23, mask = 1, - [0] = { - shift = 0, mask = 31, - [0] = "fcmpNMf", [8] = "fcmpNZf", - [16] = "fcmpeNMf", [24] = "fcmpeNZf", - } - } - } - } - }, - { -- FP immediate. - shift = 31, mask = 1, - [0] = { - shift = 5, mask = 31, - [0] = { - shift = 23, mask = 1, - [0] = "fmovDFf" - } - } - } - }, - { -- FP conditional compare. - shift = 31, mask = 1, - [0] = { - shift = 23, mask = 1, - [0] = { - shift = 4, mask = 1, - [0] = "fccmpNMVCf", "fccmpeNMVCf" - } - } - }, - { -- FP data-processing, 2 sources. - shift = 31, mask = 1, - [0] = { - shift = 23, mask = 1, - [0] = { - shift = 12, mask = 15, - [0] = "fmulDNMf", "fdivDNMf", "faddDNMf", "fsubDNMf", - "fmaxDNMf", "fminDNMf", "fmaxnmDNMf", "fminnmDNMf", - "fnmulDNMf" - } - } - }, - { -- FP conditional select. - shift = 31, mask = 1, - [0] = { - shift = 23, mask = 1, - [0] = "fcselDNMCf" - } - } - } - }, - { -- FP data-processing, 3 sources. - shift = 31, mask = 1, - [0] = { - shift = 15, mask = 1, - [0] = { - shift = 21, mask = 5, - [0] = "fmaddDNMAf", "fnmaddDNMAf" - }, - { - shift = 21, mask = 5, - [0] = "fmsubDNMAf", "fnmsubDNMAf" - } - } - } - } -} - -local map_br = { -- Branches, exception generating and system instructions. - shift = 29, mask = 7, - [0] = "bB", - { -- Compare & branch, immediate. - shift = 24, mask = 3, - [0] = "cbzDBg", "cbnzDBg", "tbzDTBw", "tbnzDTBw" - }, - { -- Conditional branch, immediate. - shift = 24, mask = 3, - [0] = { - shift = 4, mask = 1, - [0] = { - shift = 0, mask = 15, - [0] = "beqB", "bneB", "bhsB", "bloB", "bmiB", "bplB", "bvsB", "bvcB", - "bhiB", "blsB", "bgeB", "bltB", "bgtB", "bleB", "balB" - } - } - }, false, "blB", - { -- Compare & branch, immediate. - shift = 24, mask = 3, - [0] = "cbzDBg", "cbnzDBg", "tbzDTBx", "tbnzDTBx" - }, - { - shift = 24, mask = 3, - [0] = { -- Exception generation. - shift = 0, mask = 0xe0001f, - [0x200000] = "brkW" - }, - { -- System instructions. - shift = 0, mask = 0x3fffff, - [0x03201f] = "nop" - }, - { -- Unconditional branch, register. - shift = 0, mask = 0xfffc1f, - [0x1f0000] = "brNx", [0x3f0000] = "blrNx", - [0x5f0000] = "retNx" - }, - } -} - -local map_init = { - shift = 25, mask = 15, - [0] = false, false, false, false, map_ls, map_datar, map_ls, map_datafp, - map_datai, map_datai, map_br, map_br, map_ls, map_datar, map_ls, map_datafp -} - ------------------------------------------------------------------------------- - -local map_regs = { x = {}, w = {}, d = {}, s = {} } - -for i=0,30 do - map_regs.x[i] = "x"..i - map_regs.w[i] = "w"..i - map_regs.d[i] = "d"..i - map_regs.s[i] = "s"..i -end -map_regs.x[31] = "sp" -map_regs.w[31] = "wsp" -map_regs.d[31] = "d31" -map_regs.s[31] = "s31" - -local map_cond = { - [0] = "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", - "hi", "ls", "ge", "lt", "gt", "le", "al", -} - -local map_shift = { [0] = "lsl", "lsr", "asr", } - -local map_extend = { - [0] = "uxtb", "uxth", "uxtw", "uxtx", "sxtb", "sxth", "sxtw", "sxtx", -} - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then - extra = "\t->"..sym - end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-5s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-5s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - -local function match_reg(p, pat, regnum) - return map_regs[match(pat, p.."%w-([xwds])")][regnum] -end - -local function fmt_hex32(x) - if x < 0 then - return tohex(x) - else - return format("%x", x) - end -end - -local imm13_rep = { 0x55555555, 0x11111111, 0x01010101, 0x00010001, 0x00000001 } - -local function decode_imm13(op) - local imms = band(rshift(op, 10), 63) - local immr = band(rshift(op, 16), 63) - if band(op, 0x00400000) == 0 then - local len = 5 - if imms >= 56 then - if imms >= 60 then len = 1 else len = 2 end - elseif imms >= 48 then len = 3 elseif imms >= 32 then len = 4 end - local l = lshift(1, len)-1 - local s = band(imms, l) - local r = band(immr, l) - local imm = ror(rshift(-1, 31-s), r) - if len ~= 5 then imm = band(imm, lshift(1, l)-1) + rshift(imm, 31-l) end - imm = imm * imm13_rep[len] - local ix = fmt_hex32(imm) - if rshift(op, 31) ~= 0 then - return ix..tohex(imm) - else - return ix - end - else - local lo, hi = -1, 0 - if imms < 32 then lo = rshift(-1, 31-imms) else hi = rshift(-1, 63-imms) end - if immr ~= 0 then - lo, hi = ror(lo, immr), ror(hi, immr) - local x = immr == 32 and 0 or band(bxor(lo, hi), lshift(-1, 32-immr)) - lo, hi = bxor(lo, x), bxor(hi, x) - if immr >= 32 then lo, hi = hi, lo end - end - if hi ~= 0 then - return fmt_hex32(hi)..tohex(lo) - else - return fmt_hex32(lo) - end - end -end - -local function parse_immpc(op, name) - if name == "b" or name == "bl" then - return arshift(lshift(op, 6), 4) - elseif name == "adr" or name == "adrp" then - local immlo = band(rshift(op, 29), 3) - local immhi = lshift(arshift(lshift(op, 8), 13), 2) - return bor(immhi, immlo) - elseif name == "tbz" or name == "tbnz" then - return lshift(arshift(lshift(op, 13), 18), 2) - else - return lshift(arshift(lshift(op, 8), 13), 2) - end -end - -local function parse_fpimm8(op) - local sign = band(op, 0x100000) == 0 and 1 or -1 - local exp = bxor(rshift(arshift(lshift(op, 12), 5), 24), 0x80) - 131 - local frac = 16+band(rshift(op, 13), 15) - return sign * frac * 2^exp -end - -local function prefer_bfx(sf, uns, imms, immr) - if imms < immr or imms == 31 or imms == 63 then - return false - end - if immr == 0 then - if sf == 0 and (imms == 7 or imms == 15) then - return false - end - if sf ~= 0 and uns == 0 and (imms == 7 or imms == 15 or imms == 31) then - return false - end - end - return true -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) - local operands = {} - local suffix = "" - local last, name, pat - local map_reg - ctx.op = op - ctx.rel = nil - last = nil - local opat - opat = map_init[band(rshift(op, 25), 15)] - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ - end - name, pat = match(opat, "^([a-z0-9]*)(.*)") - local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)") - if altname then pat = pat2 end - if sub(pat, 1, 1) == "." then - local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") - suffix = suffix..s2 - pat = p2 - end - - local rt = match(pat, "[gf]") - if rt then - if rt == "g" then - map_reg = band(op, 0x80000000) ~= 0 and map_regs.x or map_regs.w - else - map_reg = band(op, 0x400000) ~= 0 and map_regs.d or map_regs.s - end - end - - local second0, immr - - for p in gmatch(pat, ".") do - local x = nil - if p == "D" then - local regnum = band(op, 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) - elseif p == "N" then - local regnum = band(rshift(op, 5), 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) - elseif p == "M" then - local regnum = band(rshift(op, 16), 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) - elseif p == "A" then - local regnum = band(rshift(op, 10), 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) - elseif p == "B" then - local addr = ctx.addr + pos + parse_immpc(op, name) - ctx.rel = addr - x = "0x"..tohex(addr) - elseif p == "T" then - x = bor(band(rshift(op, 26), 32), band(rshift(op, 19), 31)) - elseif p == "V" then - x = band(op, 15) - elseif p == "C" then - x = map_cond[band(rshift(op, 12), 15)] - elseif p == "c" then - local rn = band(rshift(op, 5), 31) - local rm = band(rshift(op, 16), 31) - local cond = band(rshift(op, 12), 15) - local invc = bxor(cond, 1) - x = map_cond[cond] - if altname and cond ~= 14 and cond ~= 15 then - local a1, a2 = match(altname, "([^|]*)|(.*)") - if rn == rm then - local n = #operands - operands[n] = nil - x = map_cond[invc] - if rn ~= 31 then - if a1 then name = a1 else name = altname end - else - operands[n-1] = nil - name = a2 - end - end - end - elseif p == "W" then - x = band(rshift(op, 5), 0xffff) - elseif p == "Y" then - x = band(rshift(op, 5), 0xffff) - local hw = band(rshift(op, 21), 3) - if altname and (hw == 0 or x ~= 0) then - name = altname - end - elseif p == "L" then - local rn = map_regs.x[band(rshift(op, 5), 31)] - local imm9 = arshift(lshift(op, 11), 23) - if band(op, 0x800) ~= 0 then - x = "["..rn..", #"..imm9.."]!" - else - x = "["..rn.."], #"..imm9 - end - elseif p == "U" then - local rn = map_regs.x[band(rshift(op, 5), 31)] - local sz = band(rshift(op, 30), 3) - local imm12 = lshift(arshift(lshift(op, 10), 20), sz) - if imm12 ~= 0 then - x = "["..rn..", #"..imm12.."]" - else - x = "["..rn.."]" - end - elseif p == "K" then - local rn = map_regs.x[band(rshift(op, 5), 31)] - local imm9 = arshift(lshift(op, 11), 23) - if imm9 ~= 0 then - x = "["..rn..", #"..imm9.."]" - else - x = "["..rn.."]" - end - elseif p == "O" then - local rn, rm = map_regs.x[band(rshift(op, 5), 31)] - local m = band(rshift(op, 13), 1) - if m == 0 then - rm = map_regs.w[band(rshift(op, 16), 31)] - else - rm = map_regs.x[band(rshift(op, 16), 31)] - end - x = "["..rn..", "..rm - local opt = band(rshift(op, 13), 7) - local s = band(rshift(op, 12), 1) - local sz = band(rshift(op, 30), 3) - -- extension to be applied - if opt == 3 then - if s == 0 then x = x.."]" - else x = x..", lsl #"..sz.."]" end - elseif opt == 2 or opt == 6 or opt == 7 then - if s == 0 then x = x..", "..map_extend[opt].."]" - else x = x..", "..map_extend[opt].." #"..sz.."]" end - else - x = x.."]" - end - elseif p == "P" then - local opcv, sh = rshift(op, 26), 2 - if opcv >= 0x2a then sh = 4 elseif opcv >= 0x1b then sh = 3 end - local imm7 = lshift(arshift(lshift(op, 10), 25), sh) - local rn = map_regs.x[band(rshift(op, 5), 31)] - local ind = band(rshift(op, 23), 3) - if ind == 1 then - x = "["..rn.."], #"..imm7 - elseif ind == 2 then - if imm7 == 0 then - x = "["..rn.."]" - else - x = "["..rn..", #"..imm7.."]" - end - elseif ind == 3 then - x = "["..rn..", #"..imm7.."]!" - end - elseif p == "I" then - local shf = band(rshift(op, 22), 3) - local imm12 = band(rshift(op, 10), 0x0fff) - local rn, rd = band(rshift(op, 5), 31), band(op, 31) - if altname == "mov" and shf == 0 and imm12 == 0 and (rn == 31 or rd == 31) then - name = altname - x = nil - elseif shf == 0 then - x = imm12 - elseif shf == 1 then - x = imm12..", lsl #12" - end - elseif p == "i" then - x = "#0x"..decode_imm13(op) - elseif p == "1" then - immr = band(rshift(op, 16), 63) - x = immr - elseif p == "2" then - x = band(rshift(op, 10), 63) - if altname then - local a1, a2, a3, a4, a5, a6 = - match(altname, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|(.*)") - local sf = band(rshift(op, 26), 32) - local uns = band(rshift(op, 30), 1) - if prefer_bfx(sf, uns, x, immr) then - name = a2 - x = x - immr + 1 - elseif immr == 0 and x == 7 then - local n = #operands - operands[n] = nil - if sf ~= 0 then - operands[n-1] = gsub(operands[n-1], "x", "w") - end - last = operands[n-1] - name = a6 - x = nil - elseif immr == 0 and x == 15 then - local n = #operands - operands[n] = nil - if sf ~= 0 then - operands[n-1] = gsub(operands[n-1], "x", "w") - end - last = operands[n-1] - name = a5 - x = nil - elseif x == 31 or x == 63 then - if x == 31 and immr == 0 and name == "sbfm" then - name = a4 - local n = #operands - operands[n] = nil - if sf ~= 0 then - operands[n-1] = gsub(operands[n-1], "x", "w") - end - last = operands[n-1] - else - name = a3 - end - x = nil - elseif band(x, 31) ~= 31 and immr == x+1 and name == "ubfm" then - name = a4 - last = "#"..(sf+32 - immr) - operands[#operands] = last - x = nil - elseif x < immr then - name = a1 - last = "#"..(sf+32 - immr) - operands[#operands] = last - x = x + 1 - end - end - elseif p == "3" then - x = band(rshift(op, 10), 63) - if altname then - local a1, a2 = match(altname, "([^|]*)|(.*)") - if x < immr then - name = a1 - local sf = band(rshift(op, 26), 32) - last = "#"..(sf+32 - immr) - operands[#operands] = last - x = x + 1 - else - name = a2 - x = x - immr + 1 - end - end - elseif p == "4" then - x = band(rshift(op, 10), 63) - local rn = band(rshift(op, 5), 31) - local rm = band(rshift(op, 16), 31) - if altname and rn == rm then - local n = #operands - operands[n] = nil - last = operands[n-1] - name = altname - end - elseif p == "5" then - x = band(rshift(op, 16), 31) - elseif p == "S" then - x = band(rshift(op, 10), 63) - if x == 0 then x = nil - else x = map_shift[band(rshift(op, 22), 3)].." #"..x end - elseif p == "X" then - local opt = band(rshift(op, 13), 7) - -- Width specifier . - if opt ~= 3 and opt ~= 7 then - last = map_regs.w[band(rshift(op, 16), 31)] - operands[#operands] = last - end - x = band(rshift(op, 10), 7) - -- Extension. - if opt == 2 + band(rshift(op, 31), 1) and - band(rshift(op, second0 and 5 or 0), 31) == 31 then - if x == 0 then x = nil - else x = "lsl #"..x end - else - if x == 0 then x = map_extend[band(rshift(op, 13), 7)] - else x = map_extend[band(rshift(op, 13), 7)].." #"..x end - end - elseif p == "R" then - x = band(rshift(op,21), 3) - if x == 0 then x = nil - else x = "lsl #"..x*16 end - elseif p == "z" then - local n = #operands - if operands[n] == "sp" then operands[n] = "xzr" - elseif operands[n] == "wsp" then operands[n] = "wzr" - end - elseif p == "Z" then - x = 0 - elseif p == "F" then - x = parse_fpimm8(op) - elseif p == "g" or p == "f" or p == "x" or p == "w" or - p == "d" or p == "s" then - -- These are handled in D/N/M/A. - elseif p == "0" then - if last == "sp" or last == "wsp" then - local n = #operands - operands[n] = nil - last = operands[n-1] - if altname then - local a1, a2 = match(altname, "([^|]*)|(.*)") - if not a1 then - name = altname - elseif second0 then - name, altname = a2, a1 - else - name, altname = a1, a2 - end - end - end - second0 = true - else - assert(false) - end - if x then - last = x - if type(x) == "number" then x = "#"..x end - operands[#operands+1] = x - end - end - - return putop(ctx, name..suffix, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - ctx.pos = ofs - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass(code, addr, out) - create(code, addr, out):disass() -end - --- Return register name for RID. -local function regname(r) - if r < 32 then return map_regs.x[r] end - return map_regs.d[r-32] -end - --- Public module functions. -return { - create = create, - disass = disass, - regname = regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64be.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64be.lua deleted file mode 100644 index 7337f5b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_arm64be.lua +++ /dev/null @@ -1,12 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT ARM64BE disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- ARM64 instructions are always little-endian. So just forward to the --- common ARM64 disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -return require((string.match(..., ".*%.") or "").."dis_arm64") - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips.lua deleted file mode 100644 index 05dc30f..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips.lua +++ /dev/null @@ -1,694 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPS disassembler module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT/X license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles all standard MIPS32R1/R2 instructions. --- Default mode is big-endian, but see: dis_mipsel.lua ------------------------------------------------------------------------------- - -local type = type -local byte, format = string.byte, string.format -local match, gmatch = string.match, string.gmatch -local concat = table.concat -local bit = require("bit") -local band, bor, tohex = bit.band, bit.bor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift - ------------------------------------------------------------------------------- --- Extended opcode maps common to all MIPS releases ------------------------------------------------------------------------------- - -local map_srl = { shift = 21, mask = 1, [0] = "srlDTA", "rotrDTA", } -local map_srlv = { shift = 6, mask = 1, [0] = "srlvDTS", "rotrvDTS", } - -local map_cop0 = { - shift = 25, mask = 1, - [0] = { - shift = 21, mask = 15, - [0] = "mfc0TDW", [4] = "mtc0TDW", - [10] = "rdpgprDT", - [11] = { shift = 5, mask = 1, [0] = "diT0", "eiT0", }, - [14] = "wrpgprDT", - }, { - shift = 0, mask = 63, - [1] = "tlbr", [2] = "tlbwi", [6] = "tlbwr", [8] = "tlbp", - [24] = "eret", [31] = "deret", - [32] = "wait", - }, -} - ------------------------------------------------------------------------------- --- Primary and extended opcode maps for MIPS R1-R5 ------------------------------------------------------------------------------- - -local map_movci = { shift = 16, mask = 1, [0] = "movfDSC", "movtDSC", } - -local map_special = { - shift = 0, mask = 63, - [0] = { shift = 0, mask = -1, [0] = "nop", _ = "sllDTA" }, - map_movci, map_srl, "sraDTA", - "sllvDTS", false, map_srlv, "sravDTS", - "jrS", "jalrD1S", "movzDST", "movnDST", - "syscallY", "breakY", false, "sync", - "mfhiD", "mthiS", "mfloD", "mtloS", - "dsllvDST", false, "dsrlvDST", "dsravDST", - "multST", "multuST", "divST", "divuST", - "dmultST", "dmultuST", "ddivST", "ddivuST", - "addDST", "addu|moveDST0", "subDST", "subu|neguDS0T", - "andDST", "or|moveDST0", "xorDST", "nor|notDST0", - false, false, "sltDST", "sltuDST", - "daddDST", "dadduDST", "dsubDST", "dsubuDST", - "tgeSTZ", "tgeuSTZ", "tltSTZ", "tltuSTZ", - "teqSTZ", false, "tneSTZ", false, - "dsllDTA", false, "dsrlDTA", "dsraDTA", - "dsll32DTA", false, "dsrl32DTA", "dsra32DTA", -} - -local map_special2 = { - shift = 0, mask = 63, - [0] = "maddST", "madduST", "mulDST", false, - "msubST", "msubuST", - [32] = "clzDS", [33] = "cloDS", - [63] = "sdbbpY", -} - -local map_bshfl = { - shift = 6, mask = 31, - [2] = "wsbhDT", - [16] = "sebDT", - [24] = "sehDT", -} - -local map_dbshfl = { - shift = 6, mask = 31, - [2] = "dsbhDT", - [5] = "dshdDT", -} - -local map_special3 = { - shift = 0, mask = 63, - [0] = "extTSAK", [1] = "dextmTSAP", [3] = "dextTSAK", - [4] = "insTSAL", [6] = "dinsuTSEQ", [7] = "dinsTSAL", - [32] = map_bshfl, [36] = map_dbshfl, [59] = "rdhwrTD", -} - -local map_regimm = { - shift = 16, mask = 31, - [0] = "bltzSB", "bgezSB", "bltzlSB", "bgezlSB", - false, false, false, false, - "tgeiSI", "tgeiuSI", "tltiSI", "tltiuSI", - "teqiSI", false, "tneiSI", false, - "bltzalSB", "bgezalSB", "bltzallSB", "bgezallSB", - false, false, false, false, - false, false, false, false, - false, false, false, "synciSO", -} - -local map_cop1s = { - shift = 0, mask = 63, - [0] = "add.sFGH", "sub.sFGH", "mul.sFGH", "div.sFGH", - "sqrt.sFG", "abs.sFG", "mov.sFG", "neg.sFG", - "round.l.sFG", "trunc.l.sFG", "ceil.l.sFG", "floor.l.sFG", - "round.w.sFG", "trunc.w.sFG", "ceil.w.sFG", "floor.w.sFG", - false, - { shift = 16, mask = 1, [0] = "movf.sFGC", "movt.sFGC" }, - "movz.sFGT", "movn.sFGT", - false, "recip.sFG", "rsqrt.sFG", false, - false, false, false, false, - false, false, false, false, - false, "cvt.d.sFG", false, false, - "cvt.w.sFG", "cvt.l.sFG", "cvt.ps.sFGH", false, - false, false, false, false, - false, false, false, false, - "c.f.sVGH", "c.un.sVGH", "c.eq.sVGH", "c.ueq.sVGH", - "c.olt.sVGH", "c.ult.sVGH", "c.ole.sVGH", "c.ule.sVGH", - "c.sf.sVGH", "c.ngle.sVGH", "c.seq.sVGH", "c.ngl.sVGH", - "c.lt.sVGH", "c.nge.sVGH", "c.le.sVGH", "c.ngt.sVGH", -} - -local map_cop1d = { - shift = 0, mask = 63, - [0] = "add.dFGH", "sub.dFGH", "mul.dFGH", "div.dFGH", - "sqrt.dFG", "abs.dFG", "mov.dFG", "neg.dFG", - "round.l.dFG", "trunc.l.dFG", "ceil.l.dFG", "floor.l.dFG", - "round.w.dFG", "trunc.w.dFG", "ceil.w.dFG", "floor.w.dFG", - false, - { shift = 16, mask = 1, [0] = "movf.dFGC", "movt.dFGC" }, - "movz.dFGT", "movn.dFGT", - false, "recip.dFG", "rsqrt.dFG", false, - false, false, false, false, - false, false, false, false, - "cvt.s.dFG", false, false, false, - "cvt.w.dFG", "cvt.l.dFG", false, false, - false, false, false, false, - false, false, false, false, - "c.f.dVGH", "c.un.dVGH", "c.eq.dVGH", "c.ueq.dVGH", - "c.olt.dVGH", "c.ult.dVGH", "c.ole.dVGH", "c.ule.dVGH", - "c.df.dVGH", "c.ngle.dVGH", "c.deq.dVGH", "c.ngl.dVGH", - "c.lt.dVGH", "c.nge.dVGH", "c.le.dVGH", "c.ngt.dVGH", -} - -local map_cop1ps = { - shift = 0, mask = 63, - [0] = "add.psFGH", "sub.psFGH", "mul.psFGH", false, - false, "abs.psFG", "mov.psFG", "neg.psFG", - false, false, false, false, - false, false, false, false, - false, - { shift = 16, mask = 1, [0] = "movf.psFGC", "movt.psFGC" }, - "movz.psFGT", "movn.psFGT", - false, false, false, false, - false, false, false, false, - false, false, false, false, - "cvt.s.puFG", false, false, false, - false, false, false, false, - "cvt.s.plFG", false, false, false, - "pll.psFGH", "plu.psFGH", "pul.psFGH", "puu.psFGH", - "c.f.psVGH", "c.un.psVGH", "c.eq.psVGH", "c.ueq.psVGH", - "c.olt.psVGH", "c.ult.psVGH", "c.ole.psVGH", "c.ule.psVGH", - "c.psf.psVGH", "c.ngle.psVGH", "c.pseq.psVGH", "c.ngl.psVGH", - "c.lt.psVGH", "c.nge.psVGH", "c.le.psVGH", "c.ngt.psVGH", -} - -local map_cop1w = { - shift = 0, mask = 63, - [32] = "cvt.s.wFG", [33] = "cvt.d.wFG", -} - -local map_cop1l = { - shift = 0, mask = 63, - [32] = "cvt.s.lFG", [33] = "cvt.d.lFG", -} - -local map_cop1bc = { - shift = 16, mask = 3, - [0] = "bc1fCB", "bc1tCB", "bc1flCB", "bc1tlCB", -} - -local map_cop1 = { - shift = 21, mask = 31, - [0] = "mfc1TG", "dmfc1TG", "cfc1TG", "mfhc1TG", - "mtc1TG", "dmtc1TG", "ctc1TG", "mthc1TG", - map_cop1bc, false, false, false, - false, false, false, false, - map_cop1s, map_cop1d, false, false, - map_cop1w, map_cop1l, map_cop1ps, -} - -local map_cop1x = { - shift = 0, mask = 63, - [0] = "lwxc1FSX", "ldxc1FSX", false, false, - false, "luxc1FSX", false, false, - "swxc1FSX", "sdxc1FSX", false, false, - false, "suxc1FSX", false, "prefxMSX", - false, false, false, false, - false, false, false, false, - false, false, false, false, - false, false, "alnv.psFGHS", false, - "madd.sFRGH", "madd.dFRGH", false, false, - false, false, "madd.psFRGH", false, - "msub.sFRGH", "msub.dFRGH", false, false, - false, false, "msub.psFRGH", false, - "nmadd.sFRGH", "nmadd.dFRGH", false, false, - false, false, "nmadd.psFRGH", false, - "nmsub.sFRGH", "nmsub.dFRGH", false, false, - false, false, "nmsub.psFRGH", false, -} - -local map_pri = { - [0] = map_special, map_regimm, "jJ", "jalJ", - "beq|beqz|bST00B", "bne|bnezST0B", "blezSB", "bgtzSB", - "addiTSI", "addiu|liTS0I", "sltiTSI", "sltiuTSI", - "andiTSU", "ori|liTS0U", "xoriTSU", "luiTU", - map_cop0, map_cop1, false, map_cop1x, - "beql|beqzlST0B", "bnel|bnezlST0B", "blezlSB", "bgtzlSB", - "daddiTSI", "daddiuTSI", false, false, - map_special2, "jalxJ", false, map_special3, - "lbTSO", "lhTSO", "lwlTSO", "lwTSO", - "lbuTSO", "lhuTSO", "lwrTSO", false, - "sbTSO", "shTSO", "swlTSO", "swTSO", - false, false, "swrTSO", "cacheNSO", - "llTSO", "lwc1HSO", "lwc2TSO", "prefNSO", - false, "ldc1HSO", "ldc2TSO", "ldTSO", - "scTSO", "swc1HSO", "swc2TSO", false, - false, "sdc1HSO", "sdc2TSO", "sdTSO", -} - ------------------------------------------------------------------------------- --- Primary and extended opcode maps for MIPS R6 ------------------------------------------------------------------------------- - -local map_mul_r6 = { shift = 6, mask = 3, [2] = "mulDST", [3] = "muhDST" } -local map_mulu_r6 = { shift = 6, mask = 3, [2] = "muluDST", [3] = "muhuDST" } -local map_div_r6 = { shift = 6, mask = 3, [2] = "divDST", [3] = "modDST" } -local map_divu_r6 = { shift = 6, mask = 3, [2] = "divuDST", [3] = "moduDST" } -local map_dmul_r6 = { shift = 6, mask = 3, [2] = "dmulDST", [3] = "dmuhDST" } -local map_dmulu_r6 = { shift = 6, mask = 3, [2] = "dmuluDST", [3] = "dmuhuDST" } -local map_ddiv_r6 = { shift = 6, mask = 3, [2] = "ddivDST", [3] = "dmodDST" } -local map_ddivu_r6 = { shift = 6, mask = 3, [2] = "ddivuDST", [3] = "dmoduDST" } - -local map_special_r6 = { - shift = 0, mask = 63, - [0] = { shift = 0, mask = -1, [0] = "nop", _ = "sllDTA" }, - false, map_srl, "sraDTA", - "sllvDTS", false, map_srlv, "sravDTS", - "jrS", "jalrD1S", false, false, - "syscallY", "breakY", false, "sync", - "clzDS", "cloDS", "dclzDS", "dcloDS", - "dsllvDST", "dlsaDSTA", "dsrlvDST", "dsravDST", - map_mul_r6, map_mulu_r6, map_div_r6, map_divu_r6, - map_dmul_r6, map_dmulu_r6, map_ddiv_r6, map_ddivu_r6, - "addDST", "addu|moveDST0", "subDST", "subu|neguDS0T", - "andDST", "or|moveDST0", "xorDST", "nor|notDST0", - false, false, "sltDST", "sltuDST", - "daddDST", "dadduDST", "dsubDST", "dsubuDST", - "tgeSTZ", "tgeuSTZ", "tltSTZ", "tltuSTZ", - "teqSTZ", "seleqzDST", "tneSTZ", "selnezDST", - "dsllDTA", false, "dsrlDTA", "dsraDTA", - "dsll32DTA", false, "dsrl32DTA", "dsra32DTA", -} - -local map_bshfl_r6 = { - shift = 9, mask = 3, - [1] = "alignDSTa", - _ = { - shift = 6, mask = 31, - [0] = "bitswapDT", - [2] = "wsbhDT", - [16] = "sebDT", - [24] = "sehDT", - } -} - -local map_dbshfl_r6 = { - shift = 9, mask = 3, - [1] = "dalignDSTa", - _ = { - shift = 6, mask = 31, - [0] = "dbitswapDT", - [2] = "dsbhDT", - [5] = "dshdDT", - } -} - -local map_special3_r6 = { - shift = 0, mask = 63, - [0] = "extTSAK", [1] = "dextmTSAP", [3] = "dextTSAK", - [4] = "insTSAL", [6] = "dinsuTSEQ", [7] = "dinsTSAL", - [32] = map_bshfl_r6, [36] = map_dbshfl_r6, [59] = "rdhwrTD", -} - -local map_regimm_r6 = { - shift = 16, mask = 31, - [0] = "bltzSB", [1] = "bgezSB", - [6] = "dahiSI", [30] = "datiSI", - [23] = "sigrieI", [31] = "synciSO", -} - -local map_pcrel_r6 = { - shift = 19, mask = 3, - [0] = "addiupcS2", "lwpcS2", "lwupcS2", { - shift = 18, mask = 1, - [0] = "ldpcS3", { shift = 16, mask = 3, [2] = "auipcSI", [3] = "aluipcSI" } - } -} - -local map_cop1s_r6 = { - shift = 0, mask = 63, - [0] = "add.sFGH", "sub.sFGH", "mul.sFGH", "div.sFGH", - "sqrt.sFG", "abs.sFG", "mov.sFG", "neg.sFG", - "round.l.sFG", "trunc.l.sFG", "ceil.l.sFG", "floor.l.sFG", - "round.w.sFG", "trunc.w.sFG", "ceil.w.sFG", "floor.w.sFG", - "sel.sFGH", false, false, false, - "seleqz.sFGH", "recip.sFG", "rsqrt.sFG", "selnez.sFGH", - "maddf.sFGH", "msubf.sFGH", "rint.sFG", "class.sFG", - "min.sFGH", "mina.sFGH", "max.sFGH", "maxa.sFGH", - false, "cvt.d.sFG", false, false, - "cvt.w.sFG", "cvt.l.sFG", -} - -local map_cop1d_r6 = { - shift = 0, mask = 63, - [0] = "add.dFGH", "sub.dFGH", "mul.dFGH", "div.dFGH", - "sqrt.dFG", "abs.dFG", "mov.dFG", "neg.dFG", - "round.l.dFG", "trunc.l.dFG", "ceil.l.dFG", "floor.l.dFG", - "round.w.dFG", "trunc.w.dFG", "ceil.w.dFG", "floor.w.dFG", - "sel.dFGH", false, false, false, - "seleqz.dFGH", "recip.dFG", "rsqrt.dFG", "selnez.dFGH", - "maddf.dFGH", "msubf.dFGH", "rint.dFG", "class.dFG", - "min.dFGH", "mina.dFGH", "max.dFGH", "maxa.dFGH", - "cvt.s.dFG", false, false, false, - "cvt.w.dFG", "cvt.l.dFG", -} - -local map_cop1w_r6 = { - shift = 0, mask = 63, - [0] = "cmp.af.sFGH", "cmp.un.sFGH", "cmp.eq.sFGH", "cmp.ueq.sFGH", - "cmp.lt.sFGH", "cmp.ult.sFGH", "cmp.le.sFGH", "cmp.ule.sFGH", - "cmp.saf.sFGH", "cmp.sun.sFGH", "cmp.seq.sFGH", "cmp.sueq.sFGH", - "cmp.slt.sFGH", "cmp.sult.sFGH", "cmp.sle.sFGH", "cmp.sule.sFGH", - false, "cmp.or.sFGH", "cmp.une.sFGH", "cmp.ne.sFGH", - false, false, false, false, - false, "cmp.sor.sFGH", "cmp.sune.sFGH", "cmp.sne.sFGH", - false, false, false, false, - "cvt.s.wFG", "cvt.d.wFG", -} - -local map_cop1l_r6 = { - shift = 0, mask = 63, - [0] = "cmp.af.dFGH", "cmp.un.dFGH", "cmp.eq.dFGH", "cmp.ueq.dFGH", - "cmp.lt.dFGH", "cmp.ult.dFGH", "cmp.le.dFGH", "cmp.ule.dFGH", - "cmp.saf.dFGH", "cmp.sun.dFGH", "cmp.seq.dFGH", "cmp.sueq.dFGH", - "cmp.slt.dFGH", "cmp.sult.dFGH", "cmp.sle.dFGH", "cmp.sule.dFGH", - false, "cmp.or.dFGH", "cmp.une.dFGH", "cmp.ne.dFGH", - false, false, false, false, - false, "cmp.sor.dFGH", "cmp.sune.dFGH", "cmp.sne.dFGH", - false, false, false, false, - "cvt.s.lFG", "cvt.d.lFG", -} - -local map_cop1_r6 = { - shift = 21, mask = 31, - [0] = "mfc1TG", "dmfc1TG", "cfc1TG", "mfhc1TG", - "mtc1TG", "dmtc1TG", "ctc1TG", "mthc1TG", - false, "bc1eqzHB", false, false, - false, "bc1nezHB", false, false, - map_cop1s_r6, map_cop1d_r6, false, false, - map_cop1w_r6, map_cop1l_r6, -} - -local function maprs_popTS(rs, rt) - if rt == 0 then return 0 elseif rs == 0 then return 1 - elseif rs == rt then return 2 else return 3 end -end - -local map_pop06_r6 = { - maprs = maprs_popTS, [0] = "blezSB", "blezalcTB", "bgezalcTB", "bgeucSTB" -} -local map_pop07_r6 = { - maprs = maprs_popTS, [0] = "bgtzSB", "bgtzalcTB", "bltzalcTB", "bltucSTB" -} -local map_pop26_r6 = { - maprs = maprs_popTS, "blezcTB", "bgezcTB", "bgecSTB" -} -local map_pop27_r6 = { - maprs = maprs_popTS, "bgtzcTB", "bltzcTB", "bltcSTB" -} - -local function maprs_popS(rs, rt) - if rs == 0 then return 0 else return 1 end -end - -local map_pop66_r6 = { - maprs = maprs_popS, [0] = "jicTI", "beqzcSb" -} -local map_pop76_r6 = { - maprs = maprs_popS, [0] = "jialcTI", "bnezcSb" -} - -local function maprs_popST(rs, rt) - if rs >= rt then return 0 elseif rs == 0 then return 1 else return 2 end -end - -local map_pop10_r6 = { - maprs = maprs_popST, [0] = "bovcSTB", "beqzalcTB", "beqcSTB" -} -local map_pop30_r6 = { - maprs = maprs_popST, [0] = "bnvcSTB", "bnezalcTB", "bnecSTB" -} - -local map_pri_r6 = { - [0] = map_special_r6, map_regimm_r6, "jJ", "jalJ", - "beq|beqz|bST00B", "bne|bnezST0B", map_pop06_r6, map_pop07_r6, - map_pop10_r6, "addiu|liTS0I", "sltiTSI", "sltiuTSI", - "andiTSU", "ori|liTS0U", "xoriTSU", "aui|luiTS0U", - map_cop0, map_cop1_r6, false, false, - false, false, map_pop26_r6, map_pop27_r6, - map_pop30_r6, "daddiuTSI", false, false, - false, "dauiTSI", false, map_special3_r6, - "lbTSO", "lhTSO", false, "lwTSO", - "lbuTSO", "lhuTSO", false, false, - "sbTSO", "shTSO", false, "swTSO", - false, false, false, false, - false, "lwc1HSO", "bc#", false, - false, "ldc1HSO", map_pop66_r6, "ldTSO", - false, "swc1HSO", "balc#", map_pcrel_r6, - false, "sdc1HSO", map_pop76_r6, "sdTSO", -} - ------------------------------------------------------------------------------- - -local map_gpr = { - [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", - "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", - "r24", "r25", "r26", "r27", "r28", "sp", "r30", "ra", -} - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then extra = "\t->"..sym end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-7s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-7s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - -local function get_be(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - return bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) -end - -local function get_le(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - return bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local op = ctx:get() - local operands = {} - local last = nil - ctx.op = op - ctx.rel = nil - - local opat = ctx.map_pri[rshift(op, 26)] - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - if opat.maprs then - opat = opat[opat.maprs(band(rshift(op,21),31), band(rshift(op,16),31))] - else - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ - end - end - local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") - local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)") - if altname then pat = pat2 end - - for p in gmatch(pat, ".") do - local x = nil - if p == "S" then - x = map_gpr[band(rshift(op, 21), 31)] - elseif p == "T" then - x = map_gpr[band(rshift(op, 16), 31)] - elseif p == "D" then - x = map_gpr[band(rshift(op, 11), 31)] - elseif p == "F" then - x = "f"..band(rshift(op, 6), 31) - elseif p == "G" then - x = "f"..band(rshift(op, 11), 31) - elseif p == "H" then - x = "f"..band(rshift(op, 16), 31) - elseif p == "R" then - x = "f"..band(rshift(op, 21), 31) - elseif p == "A" then - x = band(rshift(op, 6), 31) - elseif p == "a" then - x = band(rshift(op, 6), 7) - elseif p == "E" then - x = band(rshift(op, 6), 31) + 32 - elseif p == "M" then - x = band(rshift(op, 11), 31) - elseif p == "N" then - x = band(rshift(op, 16), 31) - elseif p == "C" then - x = band(rshift(op, 18), 7) - if x == 0 then x = nil end - elseif p == "K" then - x = band(rshift(op, 11), 31) + 1 - elseif p == "P" then - x = band(rshift(op, 11), 31) + 33 - elseif p == "L" then - x = band(rshift(op, 11), 31) - last + 1 - elseif p == "Q" then - x = band(rshift(op, 11), 31) - last + 33 - elseif p == "I" then - x = arshift(lshift(op, 16), 16) - elseif p == "2" then - x = arshift(lshift(op, 13), 11) - elseif p == "3" then - x = arshift(lshift(op, 14), 11) - elseif p == "U" then - x = band(op, 0xffff) - elseif p == "O" then - local disp = arshift(lshift(op, 16), 16) - operands[#operands] = format("%d(%s)", disp, last) - elseif p == "X" then - local index = map_gpr[band(rshift(op, 16), 31)] - operands[#operands] = format("%s(%s)", index, last) - elseif p == "B" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 16), 14) + 4 - ctx.rel = x - x = format("0x%08x", x) - elseif p == "b" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 11), 9) + 4 - ctx.rel = x - x = format("0x%08x", x) - elseif p == "#" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 6), 4) + 4 - ctx.rel = x - x = format("0x%08x", x) - elseif p == "J" then - local a = ctx.addr + ctx.pos - x = a - band(a, 0x0fffffff) + band(op, 0x03ffffff)*4 - ctx.rel = x - x = format("0x%08x", x) - elseif p == "V" then - x = band(rshift(op, 8), 7) - if x == 0 then x = nil end - elseif p == "W" then - x = band(op, 7) - if x == 0 then x = nil end - elseif p == "Y" then - x = band(rshift(op, 6), 0x000fffff) - if x == 0 then x = nil end - elseif p == "Z" then - x = band(rshift(op, 6), 1023) - if x == 0 then x = nil end - elseif p == "0" then - if last == "r0" or last == 0 then - local n = #operands - operands[n] = nil - last = operands[n-1] - if altname then - local a1, a2 = match(altname, "([^|]*)|(.*)") - if a1 then name, altname = a1, a2 - else name = altname end - end - end - elseif p == "1" then - if last == "ra" then - operands[#operands] = nil - end - else - assert(false) - end - if x then operands[#operands+1] = x; last = x end - end - - return putop(ctx, name, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - stop = stop - stop % 4 - ctx.pos = ofs - ofs % 4 - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - ctx.get = get_be - ctx.map_pri = map_pri - return ctx -end - -local function create_el(code, addr, out) - local ctx = create(code, addr, out) - ctx.get = get_le - return ctx -end - -local function create_r6(code, addr, out) - local ctx = create(code, addr, out) - ctx.map_pri = map_pri_r6 - return ctx -end - -local function create_r6_el(code, addr, out) - local ctx = create(code, addr, out) - ctx.get = get_le - ctx.map_pri = map_pri_r6 - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass(code, addr, out) - create(code, addr, out):disass() -end - -local function disass_el(code, addr, out) - create_el(code, addr, out):disass() -end - -local function disass_r6(code, addr, out) - create_r6(code, addr, out):disass() -end - -local function disass_r6_el(code, addr, out) - create_r6_el(code, addr, out):disass() -end - --- Return register name for RID. -local function regname(r) - if r < 32 then return map_gpr[r] end - return "f"..(r-32) -end - --- Public module functions. -return { - create = create, - create_el = create_el, - create_r6 = create_r6, - create_r6_el = create_r6_el, - disass = disass, - disass_el = disass_el, - disass_r6 = disass_r6, - disass_r6_el = disass_r6_el, - regname = regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64.lua deleted file mode 100644 index 1236e52..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64.lua +++ /dev/null @@ -1,17 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPS64 disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the big-endian functions from the --- MIPS disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") -return { - create = dis_mips.create, - disass = dis_mips.disass, - regname = dis_mips.regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64el.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64el.lua deleted file mode 100644 index 7c478d2..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64el.lua +++ /dev/null @@ -1,17 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPS64EL disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the little-endian functions from the --- MIPS disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") -return { - create = dis_mips.create_el, - disass = dis_mips.disass_el, - regname = dis_mips.regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6.lua deleted file mode 100644 index c5789ce..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6.lua +++ /dev/null @@ -1,17 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPS64R6 disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the r6 big-endian functions from the --- MIPS disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") -return { - create = dis_mips.create_r6, - disass = dis_mips.disass_r6, - regname = dis_mips.regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6el.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6el.lua deleted file mode 100644 index f67f624..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mips64r6el.lua +++ /dev/null @@ -1,17 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPS64R6EL disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the r6 little-endian functions from the --- MIPS disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") -return { - create = dis_mips.create_r6_el, - disass = dis_mips.disass_r6_el, - regname = dis_mips.regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mipsel.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mipsel.lua deleted file mode 100644 index a4fa6c6..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_mipsel.lua +++ /dev/null @@ -1,17 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT MIPSEL disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the little-endian functions from the --- MIPS disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local dis_mips = require((string.match(..., ".*%.") or "").."dis_mips") -return { - create = dis_mips.create_el, - disass = dis_mips.disass_el, - regname = dis_mips.regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_ppc.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_ppc.lua deleted file mode 100644 index 8f65f25..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_ppc.lua +++ /dev/null @@ -1,591 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT PPC disassembler module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT/X license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- It disassembles all common, non-privileged 32/64 bit PowerPC instructions --- plus the e500 SPE instructions and some Cell/Xenon extensions. --- --- NYI: VMX, VMX128 ------------------------------------------------------------------------------- - -local type = type -local byte, format = string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local concat = table.concat -local bit = require("bit") -local band, bor, tohex = bit.band, bit.bor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift - ------------------------------------------------------------------------------- --- Primary and extended opcode maps ------------------------------------------------------------------------------- - -local map_crops = { - shift = 1, mask = 1023, - [0] = "mcrfXX", - [33] = "crnor|crnotCCC=", [129] = "crandcCCC", - [193] = "crxor|crclrCCC%", [225] = "crnandCCC", - [257] = "crandCCC", [289] = "creqv|crsetCCC%", - [417] = "crorcCCC", [449] = "cror|crmoveCCC=", - [16] = "b_lrKB", [528] = "b_ctrKB", - [150] = "isync", -} - -local map_rlwinm = setmetatable({ - shift = 0, mask = -1, -}, -{ __index = function(t, x) - local rot = band(rshift(x, 11), 31) - local mb = band(rshift(x, 6), 31) - local me = band(rshift(x, 1), 31) - if mb == 0 and me == 31-rot then - return "slwiRR~A." - elseif me == 31 and mb == 32-rot then - return "srwiRR~-A." - else - return "rlwinmRR~AAA." - end - end -}) - -local map_rld = { - shift = 2, mask = 7, - [0] = "rldiclRR~HM.", "rldicrRR~HM.", "rldicRR~HM.", "rldimiRR~HM.", - { - shift = 1, mask = 1, - [0] = "rldclRR~RM.", "rldcrRR~RM.", - }, -} - -local map_ext = setmetatable({ - shift = 1, mask = 1023, - - [0] = "cmp_YLRR", [32] = "cmpl_YLRR", - [4] = "twARR", [68] = "tdARR", - - [8] = "subfcRRR.", [40] = "subfRRR.", - [104] = "negRR.", [136] = "subfeRRR.", - [200] = "subfzeRR.", [232] = "subfmeRR.", - [520] = "subfcoRRR.", [552] = "subfoRRR.", - [616] = "negoRR.", [648] = "subfeoRRR.", - [712] = "subfzeoRR.", [744] = "subfmeoRR.", - - [9] = "mulhduRRR.", [73] = "mulhdRRR.", [233] = "mulldRRR.", - [457] = "divduRRR.", [489] = "divdRRR.", - [745] = "mulldoRRR.", - [969] = "divduoRRR.", [1001] = "divdoRRR.", - - [10] = "addcRRR.", [138] = "addeRRR.", - [202] = "addzeRR.", [234] = "addmeRR.", [266] = "addRRR.", - [522] = "addcoRRR.", [650] = "addeoRRR.", - [714] = "addzeoRR.", [746] = "addmeoRR.", [778] = "addoRRR.", - - [11] = "mulhwuRRR.", [75] = "mulhwRRR.", [235] = "mullwRRR.", - [459] = "divwuRRR.", [491] = "divwRRR.", - [747] = "mullwoRRR.", - [971] = "divwouRRR.", [1003] = "divwoRRR.", - - [15] = "iselltRRR", [47] = "iselgtRRR", [79] = "iseleqRRR", - - [144] = { shift = 20, mask = 1, [0] = "mtcrfRZ~", "mtocrfRZ~", }, - [19] = { shift = 20, mask = 1, [0] = "mfcrR", "mfocrfRZ", }, - [371] = { shift = 11, mask = 1023, [392] = "mftbR", [424] = "mftbuR", }, - [339] = { - shift = 11, mask = 1023, - [32] = "mferR", [256] = "mflrR", [288] = "mfctrR", [16] = "mfspefscrR", - }, - [467] = { - shift = 11, mask = 1023, - [32] = "mtxerR", [256] = "mtlrR", [288] = "mtctrR", [16] = "mtspefscrR", - }, - - [20] = "lwarxRR0R", [84] = "ldarxRR0R", - - [21] = "ldxRR0R", [53] = "lduxRRR", - [149] = "stdxRR0R", [181] = "stduxRRR", - [341] = "lwaxRR0R", [373] = "lwauxRRR", - - [23] = "lwzxRR0R", [55] = "lwzuxRRR", - [87] = "lbzxRR0R", [119] = "lbzuxRRR", - [151] = "stwxRR0R", [183] = "stwuxRRR", - [215] = "stbxRR0R", [247] = "stbuxRRR", - [279] = "lhzxRR0R", [311] = "lhzuxRRR", - [343] = "lhaxRR0R", [375] = "lhauxRRR", - [407] = "sthxRR0R", [439] = "sthuxRRR", - - [54] = "dcbst-R0R", [86] = "dcbf-R0R", - [150] = "stwcxRR0R.", [214] = "stdcxRR0R.", - [246] = "dcbtst-R0R", [278] = "dcbt-R0R", - [310] = "eciwxRR0R", [438] = "ecowxRR0R", - [470] = "dcbi-RR", - - [598] = { - shift = 21, mask = 3, - [0] = "sync", "lwsync", "ptesync", - }, - [758] = "dcba-RR", - [854] = "eieio", [982] = "icbi-R0R", [1014] = "dcbz-R0R", - - [26] = "cntlzwRR~", [58] = "cntlzdRR~", - [122] = "popcntbRR~", - [154] = "prtywRR~", [186] = "prtydRR~", - - [28] = "andRR~R.", [60] = "andcRR~R.", [124] = "nor|notRR~R=.", - [284] = "eqvRR~R.", [316] = "xorRR~R.", - [412] = "orcRR~R.", [444] = "or|mrRR~R=.", [476] = "nandRR~R.", - [508] = "cmpbRR~R", - - [512] = "mcrxrX", - - [532] = "ldbrxRR0R", [660] = "stdbrxRR0R", - - [533] = "lswxRR0R", [597] = "lswiRR0A", - [661] = "stswxRR0R", [725] = "stswiRR0A", - - [534] = "lwbrxRR0R", [662] = "stwbrxRR0R", - [790] = "lhbrxRR0R", [918] = "sthbrxRR0R", - - [535] = "lfsxFR0R", [567] = "lfsuxFRR", - [599] = "lfdxFR0R", [631] = "lfduxFRR", - [663] = "stfsxFR0R", [695] = "stfsuxFRR", - [727] = "stfdxFR0R", [759] = "stfduxFR0R", - [855] = "lfiwaxFR0R", - [983] = "stfiwxFR0R", - - [24] = "slwRR~R.", - - [27] = "sldRR~R.", [536] = "srwRR~R.", - [792] = "srawRR~R.", [824] = "srawiRR~A.", - - [794] = "sradRR~R.", [826] = "sradiRR~H.", [827] = "sradiRR~H.", - [922] = "extshRR~.", [954] = "extsbRR~.", [986] = "extswRR~.", - - [539] = "srdRR~R.", -}, -{ __index = function(t, x) - if band(x, 31) == 15 then return "iselRRRC" end - end -}) - -local map_ld = { - shift = 0, mask = 3, - [0] = "ldRRE", "lduRRE", "lwaRRE", -} - -local map_std = { - shift = 0, mask = 3, - [0] = "stdRRE", "stduRRE", -} - -local map_fps = { - shift = 5, mask = 1, - { - shift = 1, mask = 15, - [0] = false, false, "fdivsFFF.", false, - "fsubsFFF.", "faddsFFF.", "fsqrtsF-F.", false, - "fresF-F.", "fmulsFF-F.", "frsqrtesF-F.", false, - "fmsubsFFFF~.", "fmaddsFFFF~.", "fnmsubsFFFF~.", "fnmaddsFFFF~.", - } -} - -local map_fpd = { - shift = 5, mask = 1, - [0] = { - shift = 1, mask = 1023, - [0] = "fcmpuXFF", [32] = "fcmpoXFF", [64] = "mcrfsXX", - [38] = "mtfsb1A.", [70] = "mtfsb0A.", [134] = "mtfsfiA>>-A>", - [8] = "fcpsgnFFF.", [40] = "fnegF-F.", [72] = "fmrF-F.", - [136] = "fnabsF-F.", [264] = "fabsF-F.", - [12] = "frspF-F.", - [14] = "fctiwF-F.", [15] = "fctiwzF-F.", - [583] = "mffsF.", [711] = "mtfsfZF.", - [392] = "frinF-F.", [424] = "frizF-F.", - [456] = "fripF-F.", [488] = "frimF-F.", - [814] = "fctidF-F.", [815] = "fctidzF-F.", [846] = "fcfidF-F.", - }, - { - shift = 1, mask = 15, - [0] = false, false, "fdivFFF.", false, - "fsubFFF.", "faddFFF.", "fsqrtF-F.", "fselFFFF~.", - "freF-F.", "fmulFF-F.", "frsqrteF-F.", false, - "fmsubFFFF~.", "fmaddFFFF~.", "fnmsubFFFF~.", "fnmaddFFFF~.", - } -} - -local map_spe = { - shift = 0, mask = 2047, - - [512] = "evaddwRRR", [514] = "evaddiwRAR~", - [516] = "evsubwRRR~", [518] = "evsubiwRAR~", - [520] = "evabsRR", [521] = "evnegRR", - [522] = "evextsbRR", [523] = "evextshRR", [524] = "evrndwRR", - [525] = "evcntlzwRR", [526] = "evcntlswRR", - - [527] = "brincRRR", - - [529] = "evandRRR", [530] = "evandcRRR", [534] = "evxorRRR", - [535] = "evor|evmrRRR=", [536] = "evnor|evnotRRR=", - [537] = "eveqvRRR", [539] = "evorcRRR", [542] = "evnandRRR", - - [544] = "evsrwuRRR", [545] = "evsrwsRRR", - [546] = "evsrwiuRRA", [547] = "evsrwisRRA", - [548] = "evslwRRR", [550] = "evslwiRRA", - [552] = "evrlwRRR", [553] = "evsplatiRS", - [554] = "evrlwiRRA", [555] = "evsplatfiRS", - [556] = "evmergehiRRR", [557] = "evmergeloRRR", - [558] = "evmergehiloRRR", [559] = "evmergelohiRRR", - - [560] = "evcmpgtuYRR", [561] = "evcmpgtsYRR", - [562] = "evcmpltuYRR", [563] = "evcmpltsYRR", - [564] = "evcmpeqYRR", - - [632] = "evselRRR", [633] = "evselRRRW", - [634] = "evselRRRW", [635] = "evselRRRW", - [636] = "evselRRRW", [637] = "evselRRRW", - [638] = "evselRRRW", [639] = "evselRRRW", - - [640] = "evfsaddRRR", [641] = "evfssubRRR", - [644] = "evfsabsRR", [645] = "evfsnabsRR", [646] = "evfsnegRR", - [648] = "evfsmulRRR", [649] = "evfsdivRRR", - [652] = "evfscmpgtYRR", [653] = "evfscmpltYRR", [654] = "evfscmpeqYRR", - [656] = "evfscfuiR-R", [657] = "evfscfsiR-R", - [658] = "evfscfufR-R", [659] = "evfscfsfR-R", - [660] = "evfsctuiR-R", [661] = "evfsctsiR-R", - [662] = "evfsctufR-R", [663] = "evfsctsfR-R", - [664] = "evfsctuizR-R", [666] = "evfsctsizR-R", - [668] = "evfststgtYRR", [669] = "evfststltYRR", [670] = "evfststeqYRR", - - [704] = "efsaddRRR", [705] = "efssubRRR", - [708] = "efsabsRR", [709] = "efsnabsRR", [710] = "efsnegRR", - [712] = "efsmulRRR", [713] = "efsdivRRR", - [716] = "efscmpgtYRR", [717] = "efscmpltYRR", [718] = "efscmpeqYRR", - [719] = "efscfdR-R", - [720] = "efscfuiR-R", [721] = "efscfsiR-R", - [722] = "efscfufR-R", [723] = "efscfsfR-R", - [724] = "efsctuiR-R", [725] = "efsctsiR-R", - [726] = "efsctufR-R", [727] = "efsctsfR-R", - [728] = "efsctuizR-R", [730] = "efsctsizR-R", - [732] = "efststgtYRR", [733] = "efststltYRR", [734] = "efststeqYRR", - - [736] = "efdaddRRR", [737] = "efdsubRRR", - [738] = "efdcfuidR-R", [739] = "efdcfsidR-R", - [740] = "efdabsRR", [741] = "efdnabsRR", [742] = "efdnegRR", - [744] = "efdmulRRR", [745] = "efddivRRR", - [746] = "efdctuidzR-R", [747] = "efdctsidzR-R", - [748] = "efdcmpgtYRR", [749] = "efdcmpltYRR", [750] = "efdcmpeqYRR", - [751] = "efdcfsR-R", - [752] = "efdcfuiR-R", [753] = "efdcfsiR-R", - [754] = "efdcfufR-R", [755] = "efdcfsfR-R", - [756] = "efdctuiR-R", [757] = "efdctsiR-R", - [758] = "efdctufR-R", [759] = "efdctsfR-R", - [760] = "efdctuizR-R", [762] = "efdctsizR-R", - [764] = "efdtstgtYRR", [765] = "efdtstltYRR", [766] = "efdtsteqYRR", - - [768] = "evlddxRR0R", [769] = "evlddRR8", - [770] = "evldwxRR0R", [771] = "evldwRR8", - [772] = "evldhxRR0R", [773] = "evldhRR8", - [776] = "evlhhesplatxRR0R", [777] = "evlhhesplatRR2", - [780] = "evlhhousplatxRR0R", [781] = "evlhhousplatRR2", - [782] = "evlhhossplatxRR0R", [783] = "evlhhossplatRR2", - [784] = "evlwhexRR0R", [785] = "evlwheRR4", - [788] = "evlwhouxRR0R", [789] = "evlwhouRR4", - [790] = "evlwhosxRR0R", [791] = "evlwhosRR4", - [792] = "evlwwsplatxRR0R", [793] = "evlwwsplatRR4", - [796] = "evlwhsplatxRR0R", [797] = "evlwhsplatRR4", - - [800] = "evstddxRR0R", [801] = "evstddRR8", - [802] = "evstdwxRR0R", [803] = "evstdwRR8", - [804] = "evstdhxRR0R", [805] = "evstdhRR8", - [816] = "evstwhexRR0R", [817] = "evstwheRR4", - [820] = "evstwhoxRR0R", [821] = "evstwhoRR4", - [824] = "evstwwexRR0R", [825] = "evstwweRR4", - [828] = "evstwwoxRR0R", [829] = "evstwwoRR4", - - [1027] = "evmhessfRRR", [1031] = "evmhossfRRR", [1032] = "evmheumiRRR", - [1033] = "evmhesmiRRR", [1035] = "evmhesmfRRR", [1036] = "evmhoumiRRR", - [1037] = "evmhosmiRRR", [1039] = "evmhosmfRRR", [1059] = "evmhessfaRRR", - [1063] = "evmhossfaRRR", [1064] = "evmheumiaRRR", [1065] = "evmhesmiaRRR", - [1067] = "evmhesmfaRRR", [1068] = "evmhoumiaRRR", [1069] = "evmhosmiaRRR", - [1071] = "evmhosmfaRRR", [1095] = "evmwhssfRRR", [1096] = "evmwlumiRRR", - [1100] = "evmwhumiRRR", [1101] = "evmwhsmiRRR", [1103] = "evmwhsmfRRR", - [1107] = "evmwssfRRR", [1112] = "evmwumiRRR", [1113] = "evmwsmiRRR", - [1115] = "evmwsmfRRR", [1127] = "evmwhssfaRRR", [1128] = "evmwlumiaRRR", - [1132] = "evmwhumiaRRR", [1133] = "evmwhsmiaRRR", [1135] = "evmwhsmfaRRR", - [1139] = "evmwssfaRRR", [1144] = "evmwumiaRRR", [1145] = "evmwsmiaRRR", - [1147] = "evmwsmfaRRR", - - [1216] = "evaddusiaawRR", [1217] = "evaddssiaawRR", - [1218] = "evsubfusiaawRR", [1219] = "evsubfssiaawRR", - [1220] = "evmraRR", - [1222] = "evdivwsRRR", [1223] = "evdivwuRRR", - [1224] = "evaddumiaawRR", [1225] = "evaddsmiaawRR", - [1226] = "evsubfumiaawRR", [1227] = "evsubfsmiaawRR", - - [1280] = "evmheusiaawRRR", [1281] = "evmhessiaawRRR", - [1283] = "evmhessfaawRRR", [1284] = "evmhousiaawRRR", - [1285] = "evmhossiaawRRR", [1287] = "evmhossfaawRRR", - [1288] = "evmheumiaawRRR", [1289] = "evmhesmiaawRRR", - [1291] = "evmhesmfaawRRR", [1292] = "evmhoumiaawRRR", - [1293] = "evmhosmiaawRRR", [1295] = "evmhosmfaawRRR", - [1320] = "evmhegumiaaRRR", [1321] = "evmhegsmiaaRRR", - [1323] = "evmhegsmfaaRRR", [1324] = "evmhogumiaaRRR", - [1325] = "evmhogsmiaaRRR", [1327] = "evmhogsmfaaRRR", - [1344] = "evmwlusiaawRRR", [1345] = "evmwlssiaawRRR", - [1352] = "evmwlumiaawRRR", [1353] = "evmwlsmiaawRRR", - [1363] = "evmwssfaaRRR", [1368] = "evmwumiaaRRR", - [1369] = "evmwsmiaaRRR", [1371] = "evmwsmfaaRRR", - [1408] = "evmheusianwRRR", [1409] = "evmhessianwRRR", - [1411] = "evmhessfanwRRR", [1412] = "evmhousianwRRR", - [1413] = "evmhossianwRRR", [1415] = "evmhossfanwRRR", - [1416] = "evmheumianwRRR", [1417] = "evmhesmianwRRR", - [1419] = "evmhesmfanwRRR", [1420] = "evmhoumianwRRR", - [1421] = "evmhosmianwRRR", [1423] = "evmhosmfanwRRR", - [1448] = "evmhegumianRRR", [1449] = "evmhegsmianRRR", - [1451] = "evmhegsmfanRRR", [1452] = "evmhogumianRRR", - [1453] = "evmhogsmianRRR", [1455] = "evmhogsmfanRRR", - [1472] = "evmwlusianwRRR", [1473] = "evmwlssianwRRR", - [1480] = "evmwlumianwRRR", [1481] = "evmwlsmianwRRR", - [1491] = "evmwssfanRRR", [1496] = "evmwumianRRR", - [1497] = "evmwsmianRRR", [1499] = "evmwsmfanRRR", -} - -local map_pri = { - [0] = false, false, "tdiARI", "twiARI", - map_spe, false, false, "mulliRRI", - "subficRRI", false, "cmpl_iYLRU", "cmp_iYLRI", - "addicRRI", "addic.RRI", "addi|liRR0I", "addis|lisRR0I", - "b_KBJ", "sc", "bKJ", map_crops, - "rlwimiRR~AAA.", map_rlwinm, false, "rlwnmRR~RAA.", - "oriNRR~U", "orisRR~U", "xoriRR~U", "xorisRR~U", - "andi.RR~U", "andis.RR~U", map_rld, map_ext, - "lwzRRD", "lwzuRRD", "lbzRRD", "lbzuRRD", - "stwRRD", "stwuRRD", "stbRRD", "stbuRRD", - "lhzRRD", "lhzuRRD", "lhaRRD", "lhauRRD", - "sthRRD", "sthuRRD", "lmwRRD", "stmwRRD", - "lfsFRD", "lfsuFRD", "lfdFRD", "lfduFRD", - "stfsFRD", "stfsuFRD", "stfdFRD", "stfduFRD", - false, false, map_ld, map_fps, - false, false, map_std, map_fpd, -} - ------------------------------------------------------------------------------- - -local map_gpr = { - [0] = "r0", "sp", "r2", "r3", "r4", "r5", "r6", "r7", - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", - "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", - "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", -} - -local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", } - --- Format a condition bit. -local function condfmt(cond) - if cond <= 3 then - return map_cond[band(cond, 3)] - else - return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)]) - end -end - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local pos = ctx.pos - local extra = "" - if ctx.rel then - local sym = ctx.symtab[ctx.rel] - if sym then extra = "\t->"..sym end - end - if ctx.hexdump > 0 then - ctx.out(format("%08x %s %-7s %s%s\n", - ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) - else - ctx.out(format("%08x %-7s %s%s\n", - ctx.addr+pos, text, concat(operands, ", "), extra)) - end - ctx.pos = pos + 4 -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) -end - --- Disassemble a single instruction. -local function disass_ins(ctx) - local pos = ctx.pos - local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) - local operands = {} - local last = nil - local rs = 21 - ctx.op = op - ctx.rel = nil - - local opat = map_pri[rshift(b0, 2)] - while type(opat) ~= "string" do - if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] - end - local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") - local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)") - if altname then pat = pat2 end - - for p in gmatch(pat, ".") do - local x = nil - if p == "R" then - x = map_gpr[band(rshift(op, rs), 31)] - rs = rs - 5 - elseif p == "F" then - x = "f"..band(rshift(op, rs), 31) - rs = rs - 5 - elseif p == "A" then - x = band(rshift(op, rs), 31) - rs = rs - 5 - elseif p == "S" then - x = arshift(lshift(op, 27-rs), 27) - rs = rs - 5 - elseif p == "I" then - x = arshift(lshift(op, 16), 16) - elseif p == "U" then - x = band(op, 0xffff) - elseif p == "D" or p == "E" then - local disp = arshift(lshift(op, 16), 16) - if p == "E" then disp = band(disp, -4) end - if last == "r0" then last = "0" end - operands[#operands] = format("%d(%s)", disp, last) - elseif p >= "2" and p <= "8" then - local disp = band(rshift(op, rs), 31) * p - if last == "r0" then last = "0" end - operands[#operands] = format("%d(%s)", disp, last) - elseif p == "H" then - x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4) - rs = rs - 5 - elseif p == "M" then - x = band(rshift(op, rs), 31) + band(op, 0x20) - elseif p == "C" then - x = condfmt(band(rshift(op, rs), 31)) - rs = rs - 5 - elseif p == "B" then - local bo = rshift(op, 21) - local cond = band(rshift(op, 16), 31) - local cn = "" - rs = rs - 10 - if band(bo, 4) == 0 then - cn = band(bo, 2) == 0 and "dnz" or "dz" - if band(bo, 0x10) == 0 then - cn = cn..(band(bo, 8) == 0 and "f" or "t") - end - if band(bo, 0x10) == 0 then x = condfmt(cond) end - name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") - elseif band(bo, 0x10) == 0 then - cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)] - if cond > 3 then x = "cr"..rshift(cond, 2) end - name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") - end - name = gsub(name, "_", cn) - elseif p == "J" then - x = arshift(lshift(op, 27-rs), 29-rs)*4 - if band(op, 2) == 0 then x = ctx.addr + pos + x end - ctx.rel = x - x = "0x"..tohex(x) - elseif p == "K" then - if band(op, 1) ~= 0 then name = name.."l" end - if band(op, 2) ~= 0 then name = name.."a" end - elseif p == "X" or p == "Y" then - x = band(rshift(op, rs+2), 7) - if x == 0 and p == "Y" then x = nil else x = "cr"..x end - rs = rs - 5 - elseif p == "W" then - x = "cr"..band(op, 7) - elseif p == "Z" then - x = band(rshift(op, rs-4), 255) - rs = rs - 10 - elseif p == ">" then - operands[#operands] = rshift(operands[#operands], 1) - elseif p == "0" then - if last == "r0" then - operands[#operands] = nil - if altname then name = altname end - end - elseif p == "L" then - name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w") - elseif p == "." then - if band(op, 1) == 1 then name = name.."." end - elseif p == "N" then - if op == 0x60000000 then name = "nop"; break end - elseif p == "~" then - local n = #operands - operands[n-1], operands[n] = operands[n], operands[n-1] - elseif p == "=" then - local n = #operands - if last == operands[n-1] then - operands[n] = nil - name = altname - end - elseif p == "%" then - local n = #operands - if last == operands[n-1] and last == operands[n-2] then - operands[n] = nil - operands[n-1] = nil - name = altname - end - elseif p == "-" then - rs = rs - 5 - else - assert(false) - end - if x then operands[#operands+1] = x; last = x end - end - - return putop(ctx, name, operands) -end - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - stop = stop - stop % 4 - ctx.pos = ofs - ofs % 4 - ctx.rel = nil - while ctx.pos < stop do disass_ins(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = addr or 0 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 8 - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass(code, addr, out) - create(code, addr, out):disass() -end - --- Return register name for RID. -local function regname(r) - if r < 32 then return map_gpr[r] end - return "f"..(r-32) -end - --- Public module functions. -return { - create = create, - disass = disass, - regname = regname -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x64.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x64.lua deleted file mode 100644 index d076c6a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x64.lua +++ /dev/null @@ -1,17 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT x64 disassembler wrapper module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This module just exports the 64 bit functions from the combined --- x86/x64 disassembler module. All the interesting stuff is there. ------------------------------------------------------------------------------- - -local dis_x86 = require((string.match(..., ".*%.") or "").."dis_x86") -return { - create = dis_x86.create64, - disass = dis_x86.disass64, - regname = dis_x86.regname64 -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x86.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x86.lua deleted file mode 100644 index 84492ff..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dis_x86.lua +++ /dev/null @@ -1,953 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT x86/x64 disassembler module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- This is a helper module used by the LuaJIT machine code dumper module. --- --- Sending small code snippets to an external disassembler and mixing the --- output with our own stuff was too fragile. So I had to bite the bullet --- and write yet another x86 disassembler. Oh well ... --- --- The output format is very similar to what ndisasm generates. But it has --- been developed independently by looking at the opcode tables from the --- Intel and AMD manuals. The supported instruction set is quite extensive --- and reflects what a current generation Intel or AMD CPU implements in --- 32 bit and 64 bit mode. Yes, this includes MMX, SSE, SSE2, SSE3, SSSE3, --- SSE4.1, SSE4.2, SSE4a, AVX, AVX2 and even privileged and hypervisor --- (VMX/SVM) instructions. --- --- Notes: --- * The (useless) a16 prefix, 3DNow and pre-586 opcodes are unsupported. --- * No attempt at optimization has been made -- it's fast enough for my needs. ------------------------------------------------------------------------------- - -local type = type -local sub, byte, format = string.sub, string.byte, string.format -local match, gmatch, gsub = string.match, string.gmatch, string.gsub -local lower, rep = string.lower, string.rep -local bit = require("bit") -local tohex = bit.tohex - --- Map for 1st opcode byte in 32 bit mode. Ugly? Well ... read on. -local map_opc1_32 = { ---0x -[0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es", -"orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*", ---1x -"adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss", -"sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds", ---2x -"andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa", -"subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das", ---3x -"xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa", -"cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas", ---4x -"incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR", -"decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR", ---5x -"pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR", -"popUR","popUR","popUR","popUR","popUR","popUR","popUR","popUR", ---6x -"sz*pushaw,pusha","sz*popaw,popa","boundVrm","arplWmr", -"fs:seg","gs:seg","o16:","a16", -"pushUi","imulVrmi","pushBs","imulVrms", -"insb","insVS","outsb","outsVS", ---7x -"joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj", -"jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj", ---8x -"arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms", -"testBmr","testVmr","xchgBrm","xchgVrm", -"movBmr","movVmr","movBrm","movVrm", -"movVmg","leaVrm","movWgm","popUm", ---9x -"nop*xchgVaR|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR", -"xchgVaR","xchgVaR","xchgVaR","xchgVaR", -"sz*cbw,cwde,cdqe","sz*cwd,cdq,cqo","call farViw","wait", -"sz*pushfw,pushf","sz*popfw,popf","sahf","lahf", ---Ax -"movBao","movVao","movBoa","movVoa", -"movsb","movsVS","cmpsb","cmpsVS", -"testBai","testVai","stosb","stosVS", -"lodsb","lodsVS","scasb","scasVS", ---Bx -"movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi", -"movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI", ---Cx -"shift!Bmu","shift!Vmu","retBw","ret","vex*3$lesVrm","vex*2$ldsVrm","movBmi","movVmi", -"enterBwu","leave","retfBw","retf","int3","intBu","into","iretVS", ---Dx -"shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb", -"fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7", ---Ex -"loopneBj","loopeBj","loopBj","sz*jcxzBj,jecxzBj,jrcxzBj", -"inBau","inVau","outBua","outVua", -"callVj","jmpVj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda", ---Fx -"lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm", -"clc","stc","cli","sti","cld","std","incb!Bm","incd!Vm", -} -assert(#map_opc1_32 == 255) - --- Map for 1st opcode byte in 64 bit mode (overrides only). -local map_opc1_64 = setmetatable({ - [0x06]=false, [0x07]=false, [0x0e]=false, - [0x16]=false, [0x17]=false, [0x1e]=false, [0x1f]=false, - [0x27]=false, [0x2f]=false, [0x37]=false, [0x3f]=false, - [0x60]=false, [0x61]=false, [0x62]=false, [0x63]="movsxdVrDmt", [0x67]="a32:", - [0x40]="rex*", [0x41]="rex*b", [0x42]="rex*x", [0x43]="rex*xb", - [0x44]="rex*r", [0x45]="rex*rb", [0x46]="rex*rx", [0x47]="rex*rxb", - [0x48]="rex*w", [0x49]="rex*wb", [0x4a]="rex*wx", [0x4b]="rex*wxb", - [0x4c]="rex*wr", [0x4d]="rex*wrb", [0x4e]="rex*wrx", [0x4f]="rex*wrxb", - [0x82]=false, [0x9a]=false, [0xc4]="vex*3", [0xc5]="vex*2", [0xce]=false, - [0xd4]=false, [0xd5]=false, [0xd6]=false, [0xea]=false, -}, { __index = map_opc1_32 }) - --- Map for 2nd opcode byte (0F xx). True CISC hell. Hey, I told you. --- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne, -|F3|66|F2 -local map_opc2 = { ---0x -[0]="sldt!Dmp","sgdt!Ump","larVrm","lslVrm",nil,"syscall","clts","sysret", -"invd","wbinvd",nil,"ud1",nil,"$prefetch!Bm","femms","3dnowMrmu", ---1x -"movupsXrm|movssXrvm|movupdXrm|movsdXrvm", -"movupsXmr|movssXmvr|movupdXmr|movsdXmvr", -"movhlpsXrm$movlpsXrm|movsldupXrm|movlpdXrm|movddupXrm", -"movlpsXmr||movlpdXmr", -"unpcklpsXrvm||unpcklpdXrvm", -"unpckhpsXrvm||unpckhpdXrvm", -"movlhpsXrm$movhpsXrm|movshdupXrm|movhpdXrm", -"movhpsXmr||movhpdXmr", -"$prefetcht!Bm","hintnopVm","hintnopVm","hintnopVm", -"hintnopVm","hintnopVm","hintnopVm","hintnopVm", ---2x -"movUmx$","movUmy$","movUxm$","movUym$","movUmz$",nil,"movUzm$",nil, -"movapsXrm||movapdXrm", -"movapsXmr||movapdXmr", -"cvtpi2psXrMm|cvtsi2ssXrvVmt|cvtpi2pdXrMm|cvtsi2sdXrvVmt", -"movntpsXmr|movntssXmr|movntpdXmr|movntsdXmr", -"cvttps2piMrXm|cvttss2siVrXm|cvttpd2piMrXm|cvttsd2siVrXm", -"cvtps2piMrXm|cvtss2siVrXm|cvtpd2piMrXm|cvtsd2siVrXm", -"ucomissXrm||ucomisdXrm", -"comissXrm||comisdXrm", ---3x -"wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,"getsec", -"opc3*38",nil,"opc3*3a",nil,nil,nil,nil,nil, ---4x -"cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm", -"cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm", -"cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm", -"cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm", ---5x -"movmskpsVrXm$||movmskpdVrXm$","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm", -"rsqrtpsXrm|rsqrtssXrvm","rcppsXrm|rcpssXrvm", -"andpsXrvm||andpdXrvm","andnpsXrvm||andnpdXrvm", -"orpsXrvm||orpdXrvm","xorpsXrvm||xorpdXrvm", -"addpsXrvm|addssXrvm|addpdXrvm|addsdXrvm","mulpsXrvm|mulssXrvm|mulpdXrvm|mulsdXrvm", -"cvtps2pdXrm|cvtss2sdXrvm|cvtpd2psXrm|cvtsd2ssXrvm", -"cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm", -"subpsXrvm|subssXrvm|subpdXrvm|subsdXrvm","minpsXrvm|minssXrvm|minpdXrvm|minsdXrvm", -"divpsXrvm|divssXrvm|divpdXrvm|divsdXrvm","maxpsXrvm|maxssXrvm|maxpdXrvm|maxsdXrvm", ---6x -"punpcklbwPrvm","punpcklwdPrvm","punpckldqPrvm","packsswbPrvm", -"pcmpgtbPrvm","pcmpgtwPrvm","pcmpgtdPrvm","packuswbPrvm", -"punpckhbwPrvm","punpckhwdPrvm","punpckhdqPrvm","packssdwPrvm", -"||punpcklqdqXrvm","||punpckhqdqXrvm", -"movPrVSm","movqMrm|movdquXrm|movdqaXrm", ---7x -"pshufwMrmu|pshufhwXrmu|pshufdXrmu|pshuflwXrmu","pshiftw!Pvmu", -"pshiftd!Pvmu","pshiftq!Mvmu||pshiftdq!Xvmu", -"pcmpeqbPrvm","pcmpeqwPrvm","pcmpeqdPrvm","emms*|", -"vmreadUmr||extrqXmuu$|insertqXrmuu$","vmwriteUrm||extrqXrm$|insertqXrm$", -nil,nil, -"||haddpdXrvm|haddpsXrvm","||hsubpdXrvm|hsubpsXrvm", -"movVSmMr|movqXrm|movVSmXr","movqMmr|movdquXmr|movdqaXmr", ---8x -"joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj", -"jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj", ---9x -"setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm", -"setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm", ---Ax -"push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil, -"push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm", ---Bx -"cmpxchgBmr","cmpxchgVmr","$lssVrm","btrVmr", -"$lfsVrm","$lgsVrm","movzxVrBmt","movzxVrWmt", -"|popcntVrm","ud2Dp","bt!Vmu","btcVmr", -"bsfVrm","bsrVrm|lzcntVrm|bsrWrm","movsxVrBmt","movsxVrWmt", ---Cx -"xaddBmr","xaddVmr", -"cmppsXrvmu|cmpssXrvmu|cmppdXrvmu|cmpsdXrvmu","$movntiVmr|", -"pinsrwPrvWmu","pextrwDrPmu", -"shufpsXrvmu||shufpdXrvmu","$cmpxchg!Qmp", -"bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR", ---Dx -"||addsubpdXrvm|addsubpsXrvm","psrlwPrvm","psrldPrvm","psrlqPrvm", -"paddqPrvm","pmullwPrvm", -"|movq2dqXrMm|movqXmr|movdq2qMrXm$","pmovmskbVrMm||pmovmskbVrXm", -"psubusbPrvm","psubuswPrvm","pminubPrvm","pandPrvm", -"paddusbPrvm","padduswPrvm","pmaxubPrvm","pandnPrvm", ---Ex -"pavgbPrvm","psrawPrvm","psradPrvm","pavgwPrvm", -"pmulhuwPrvm","pmulhwPrvm", -"|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","$movntqMmr||$movntdqXmr", -"psubsbPrvm","psubswPrvm","pminswPrvm","porPrvm", -"paddsbPrvm","paddswPrvm","pmaxswPrvm","pxorPrvm", ---Fx -"|||lddquXrm","psllwPrvm","pslldPrvm","psllqPrvm", -"pmuludqPrvm","pmaddwdPrvm","psadbwPrvm","maskmovqMrm||maskmovdquXrm$", -"psubbPrvm","psubwPrvm","psubdPrvm","psubqPrvm", -"paddbPrvm","paddwPrvm","padddPrvm","ud", -} -assert(map_opc2[255] == "ud") - --- Map for three-byte opcodes. Can't wait for their next invention. -local map_opc3 = { -["38"] = { -- [66] 0f 38 xx ---0x -[0]="pshufbPrvm","phaddwPrvm","phadddPrvm","phaddswPrvm", -"pmaddubswPrvm","phsubwPrvm","phsubdPrvm","phsubswPrvm", -"psignbPrvm","psignwPrvm","psigndPrvm","pmulhrswPrvm", -"||permilpsXrvm","||permilpdXrvm",nil,nil, ---1x -"||pblendvbXrma",nil,nil,nil, -"||blendvpsXrma","||blendvpdXrma","||permpsXrvm","||ptestXrm", -"||broadcastssXrm","||broadcastsdXrm","||broadcastf128XrlXm",nil, -"pabsbPrm","pabswPrm","pabsdPrm",nil, ---2x -"||pmovsxbwXrm","||pmovsxbdXrm","||pmovsxbqXrm","||pmovsxwdXrm", -"||pmovsxwqXrm","||pmovsxdqXrm",nil,nil, -"||pmuldqXrvm","||pcmpeqqXrvm","||$movntdqaXrm","||packusdwXrvm", -"||maskmovpsXrvm","||maskmovpdXrvm","||maskmovpsXmvr","||maskmovpdXmvr", ---3x -"||pmovzxbwXrm","||pmovzxbdXrm","||pmovzxbqXrm","||pmovzxwdXrm", -"||pmovzxwqXrm","||pmovzxdqXrm","||permdXrvm","||pcmpgtqXrvm", -"||pminsbXrvm","||pminsdXrvm","||pminuwXrvm","||pminudXrvm", -"||pmaxsbXrvm","||pmaxsdXrvm","||pmaxuwXrvm","||pmaxudXrvm", ---4x -"||pmulddXrvm","||phminposuwXrm",nil,nil, -nil,"||psrlvVSXrvm","||psravdXrvm","||psllvVSXrvm", ---5x -[0x58] = "||pbroadcastdXrlXm",[0x59] = "||pbroadcastqXrlXm", -[0x5a] = "||broadcasti128XrlXm", ---7x -[0x78] = "||pbroadcastbXrlXm",[0x79] = "||pbroadcastwXrlXm", ---8x -[0x8c] = "||pmaskmovXrvVSm", -[0x8e] = "||pmaskmovVSmXvr", ---9x -[0x96] = "||fmaddsub132pHXrvm",[0x97] = "||fmsubadd132pHXrvm", -[0x98] = "||fmadd132pHXrvm",[0x99] = "||fmadd132sHXrvm", -[0x9a] = "||fmsub132pHXrvm",[0x9b] = "||fmsub132sHXrvm", -[0x9c] = "||fnmadd132pHXrvm",[0x9d] = "||fnmadd132sHXrvm", -[0x9e] = "||fnmsub132pHXrvm",[0x9f] = "||fnmsub132sHXrvm", ---Ax -[0xa6] = "||fmaddsub213pHXrvm",[0xa7] = "||fmsubadd213pHXrvm", -[0xa8] = "||fmadd213pHXrvm",[0xa9] = "||fmadd213sHXrvm", -[0xaa] = "||fmsub213pHXrvm",[0xab] = "||fmsub213sHXrvm", -[0xac] = "||fnmadd213pHXrvm",[0xad] = "||fnmadd213sHXrvm", -[0xae] = "||fnmsub213pHXrvm",[0xaf] = "||fnmsub213sHXrvm", ---Bx -[0xb6] = "||fmaddsub231pHXrvm",[0xb7] = "||fmsubadd231pHXrvm", -[0xb8] = "||fmadd231pHXrvm",[0xb9] = "||fmadd231sHXrvm", -[0xba] = "||fmsub231pHXrvm",[0xbb] = "||fmsub231sHXrvm", -[0xbc] = "||fnmadd231pHXrvm",[0xbd] = "||fnmadd231sHXrvm", -[0xbe] = "||fnmsub231pHXrvm",[0xbf] = "||fnmsub231sHXrvm", ---Dx -[0xdc] = "||aesencXrvm", [0xdd] = "||aesenclastXrvm", -[0xde] = "||aesdecXrvm", [0xdf] = "||aesdeclastXrvm", ---Fx -[0xf0] = "|||crc32TrBmt",[0xf1] = "|||crc32TrVmt", -[0xf7] = "| sarxVrmv| shlxVrmv| shrxVrmv", -}, - -["3a"] = { -- [66] 0f 3a xx ---0x -[0x00]="||permqXrmu","||permpdXrmu","||pblenddXrvmu",nil, -"||permilpsXrmu","||permilpdXrmu","||perm2f128Xrvmu",nil, -"||roundpsXrmu","||roundpdXrmu","||roundssXrvmu","||roundsdXrvmu", -"||blendpsXrvmu","||blendpdXrvmu","||pblendwXrvmu","palignrPrvmu", ---1x -nil,nil,nil,nil, -"||pextrbVmXru","||pextrwVmXru","||pextrVmSXru","||extractpsVmXru", -"||insertf128XrvlXmu","||extractf128XlXmYru",nil,nil, -nil,nil,nil,nil, ---2x -"||pinsrbXrvVmu","||insertpsXrvmu","||pinsrXrvVmuS",nil, ---3x -[0x38] = "||inserti128Xrvmu",[0x39] = "||extracti128XlXmYru", ---4x -[0x40] = "||dppsXrvmu", -[0x41] = "||dppdXrvmu", -[0x42] = "||mpsadbwXrvmu", -[0x44] = "||pclmulqdqXrvmu", -[0x46] = "||perm2i128Xrvmu", -[0x4a] = "||blendvpsXrvmb",[0x4b] = "||blendvpdXrvmb", -[0x4c] = "||pblendvbXrvmb", ---6x -[0x60] = "||pcmpestrmXrmu",[0x61] = "||pcmpestriXrmu", -[0x62] = "||pcmpistrmXrmu",[0x63] = "||pcmpistriXrmu", -[0xdf] = "||aeskeygenassistXrmu", ---Fx -[0xf0] = "||| rorxVrmu", -}, -} - --- Map for VMX/SVM opcodes 0F 01 C0-FF (sgdt group with register operands). -local map_opcvm = { -[0xc1]="vmcall",[0xc2]="vmlaunch",[0xc3]="vmresume",[0xc4]="vmxoff", -[0xc8]="monitor",[0xc9]="mwait", -[0xd8]="vmrun",[0xd9]="vmmcall",[0xda]="vmload",[0xdb]="vmsave", -[0xdc]="stgi",[0xdd]="clgi",[0xde]="skinit",[0xdf]="invlpga", -[0xf8]="swapgs",[0xf9]="rdtscp", -} - --- Map for FP opcodes. And you thought stack machines are simple? -local map_opcfp = { --- D8-DF 00-BF: opcodes with a memory operand. --- D8 -[0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm", -"fldFm",nil,"fstFm","fstpFm","fldenvVm","fldcwWm","fnstenvVm","fnstcwWm", --- DA -"fiaddDm","fimulDm","ficomDm","ficompDm", -"fisubDm","fisubrDm","fidivDm","fidivrDm", --- DB -"fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp", --- DC -"faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm", --- DD -"fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm", --- DE -"fiaddWm","fimulWm","ficomWm","ficompWm", -"fisubWm","fisubrWm","fidivWm","fidivrWm", --- DF -"fildWm","fisttpWm","fistWm","fistpWm", -"fbld twordFmp","fildQm","fbstp twordFmp","fistpQm", --- xx C0-FF: opcodes with a pseudo-register operand. --- D8 -"faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf", --- D9 -"fldFf","fxchFf",{"fnop"},nil, -{"fchs","fabs",nil,nil,"ftst","fxam"}, -{"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"}, -{"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"}, -{"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"}, --- DA -"fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil, --- DB -"fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf", -{nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil, --- DC -"fadd toFf","fmul toFf",nil,nil, -"fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf", --- DD -"ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil, --- DE -"faddpFf","fmulpFf",nil,{nil,"fcompp"}, -"fsubrpFf","fsubpFf","fdivrpFf","fdivpFf", --- DF -nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil, -} -assert(map_opcfp[126] == "fcomipFf") - --- Map for opcode groups. The subkey is sp from the ModRM byte. -local map_opcgroup = { - arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" }, - shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" }, - testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" }, - testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" }, - incb = { "inc", "dec" }, - incd = { "inc", "dec", "callUmp", "$call farDmp", - "jmpUmp", "$jmp farDmp", "pushUm" }, - sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" }, - sgdt = { "vm*$sgdt", "vm*$sidt", "$lgdt", "vm*$lidt", - "smsw", nil, "lmsw", "vm*$invlpg" }, - bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" }, - cmpxchg = { nil, "sz*,cmpxchg8bQmp,cmpxchg16bXmp", nil, nil, - nil, nil, "vmptrld|vmxon|vmclear", "vmptrst" }, - pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" }, - pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" }, - pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" }, - pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" }, - fxsave = { "$fxsave", "$fxrstor", "$ldmxcsr", "$stmxcsr", - nil, "lfenceDp$", "mfenceDp$", "sfenceDp$clflush" }, - prefetch = { "prefetch", "prefetchw" }, - prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" }, -} - ------------------------------------------------------------------------------- - --- Maps for register names. -local map_regs = { - B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh", - "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }, - B64 = { "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", - "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }, - W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", - "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" }, - D = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", - "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" }, - Q = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }, - M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7", - "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, -- No x64 ext! - X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", - "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" }, - Y = { "ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7", - "ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15" }, -} -local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" } - --- Maps for size names. -local map_sz2n = { - B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16, Y = 32, -} -local map_sz2prefix = { - B = "byte", W = "word", D = "dword", - Q = "qword", - M = "qword", X = "xword", Y = "yword", - F = "dword", G = "qword", -- No need for sizes/register names for these two. -} - ------------------------------------------------------------------------------- - --- Output a nicely formatted line with an opcode and operands. -local function putop(ctx, text, operands) - local code, pos, hex = ctx.code, ctx.pos, "" - local hmax = ctx.hexdump - if hmax > 0 then - for i=ctx.start,pos-1 do - hex = hex..format("%02X", byte(code, i, i)) - end - if #hex > hmax then hex = sub(hex, 1, hmax)..". " - else hex = hex..rep(" ", hmax-#hex+2) end - end - if operands then text = text.." "..operands end - if ctx.o16 then text = "o16 "..text; ctx.o16 = false end - if ctx.a32 then text = "a32 "..text; ctx.a32 = false end - if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end - if ctx.rex then - local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "").. - (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "").. - (ctx.vexl and "l" or "") - if ctx.vexv and ctx.vexv ~= 0 then t = t.."v"..ctx.vexv end - if t ~= "" then text = ctx.rex.."."..t.." "..gsub(text, "^ ", "") - elseif ctx.rex == "vex" then text = gsub("v"..text, "^v ", "") end - ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false - ctx.rex = false; ctx.vexl = false; ctx.vexv = false - end - if ctx.seg then - local text2, n = gsub(text, "%[", "["..ctx.seg..":") - if n == 0 then text = ctx.seg.." "..text else text = text2 end - ctx.seg = false - end - if ctx.lock then text = "lock "..text; ctx.lock = false end - local imm = ctx.imm - if imm then - local sym = ctx.symtab[imm] - if sym then text = text.."\t->"..sym end - end - ctx.out(format("%08x %s%s\n", ctx.addr+ctx.start, hex, text)) - ctx.mrm = false - ctx.vexv = false - ctx.start = pos - ctx.imm = nil -end - --- Clear all prefix flags. -local function clearprefixes(ctx) - ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false - ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false - ctx.rex = false; ctx.a32 = false; ctx.vexl = false -end - --- Fallback for incomplete opcodes at the end. -local function incomplete(ctx) - ctx.pos = ctx.stop+1 - clearprefixes(ctx) - return putop(ctx, "(incomplete)") -end - --- Fallback for unknown opcodes. -local function unknown(ctx) - clearprefixes(ctx) - return putop(ctx, "(unknown)") -end - --- Return an immediate of the specified size. -local function getimm(ctx, pos, n) - if pos+n-1 > ctx.stop then return incomplete(ctx) end - local code = ctx.code - if n == 1 then - local b1 = byte(code, pos, pos) - return b1 - elseif n == 2 then - local b1, b2 = byte(code, pos, pos+1) - return b1+b2*256 - else - local b1, b2, b3, b4 = byte(code, pos, pos+3) - local imm = b1+b2*256+b3*65536+b4*16777216 - ctx.imm = imm - return imm - end -end - --- Process pattern string and generate the operands. -local function putpat(ctx, name, pat) - local operands, regs, sz, mode, sp, rm, sc, rx, sdisp - local code, pos, stop, vexl = ctx.code, ctx.pos, ctx.stop, ctx.vexl - - -- Chars used: 1DFGHIMPQRSTUVWXYabcdfgijlmoprstuvwxyz - for p in gmatch(pat, ".") do - local x = nil - if p == "V" or p == "U" then - if ctx.rexw then sz = "Q"; ctx.rexw = false - elseif ctx.o16 then sz = "W"; ctx.o16 = false - elseif p == "U" and ctx.x64 then sz = "Q" - else sz = "D" end - regs = map_regs[sz] - elseif p == "T" then - if ctx.rexw then sz = "Q"; ctx.rexw = false else sz = "D" end - regs = map_regs[sz] - elseif p == "B" then - sz = "B" - regs = ctx.rex and map_regs.B64 or map_regs.B - elseif match(p, "[WDQMXYFG]") then - sz = p - if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end - regs = map_regs[sz] - elseif p == "P" then - sz = ctx.o16 and "X" or "M"; ctx.o16 = false - if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end - regs = map_regs[sz] - elseif p == "H" then - name = name..(ctx.rexw and "d" or "s") - ctx.rexw = false - elseif p == "S" then - name = name..lower(sz) - elseif p == "s" then - local imm = getimm(ctx, pos, 1); if not imm then return end - x = imm <= 127 and format("+0x%02x", imm) - or format("-0x%02x", 256-imm) - pos = pos+1 - elseif p == "u" then - local imm = getimm(ctx, pos, 1); if not imm then return end - x = format("0x%02x", imm) - pos = pos+1 - elseif p == "b" then - local imm = getimm(ctx, pos, 1); if not imm then return end - x = regs[imm/16+1] - pos = pos+1 - elseif p == "w" then - local imm = getimm(ctx, pos, 2); if not imm then return end - x = format("0x%x", imm) - pos = pos+2 - elseif p == "o" then -- [offset] - if ctx.x64 then - local imm1 = getimm(ctx, pos, 4); if not imm1 then return end - local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end - x = format("[0x%08x%08x]", imm2, imm1) - pos = pos+8 - else - local imm = getimm(ctx, pos, 4); if not imm then return end - x = format("[0x%08x]", imm) - pos = pos+4 - end - elseif p == "i" or p == "I" then - local n = map_sz2n[sz] - if n == 8 and ctx.x64 and p == "I" then - local imm1 = getimm(ctx, pos, 4); if not imm1 then return end - local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end - x = format("0x%08x%08x", imm2, imm1) - else - if n == 8 then n = 4 end - local imm = getimm(ctx, pos, n); if not imm then return end - if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then - imm = (0xffffffff+1)-imm - x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm) - else - x = format(imm > 65535 and "0x%08x" or "0x%x", imm) - end - end - pos = pos+n - elseif p == "j" then - local n = map_sz2n[sz] - if n == 8 then n = 4 end - local imm = getimm(ctx, pos, n); if not imm then return end - if sz == "B" and imm > 127 then imm = imm-256 - elseif imm > 2147483647 then imm = imm-4294967296 end - pos = pos+n - imm = imm + pos + ctx.addr - if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end - ctx.imm = imm - if sz == "W" then - x = format("word 0x%04x", imm%65536) - elseif ctx.x64 then - local lo = imm % 0x1000000 - x = format("0x%02x%06x", (imm-lo) / 0x1000000, lo) - else - x = "0x"..tohex(imm) - end - elseif p == "R" then - local r = byte(code, pos-1, pos-1)%8 - if ctx.rexb then r = r + 8; ctx.rexb = false end - x = regs[r+1] - elseif p == "a" then x = regs[1] - elseif p == "c" then x = "cl" - elseif p == "d" then x = "dx" - elseif p == "1" then x = "1" - else - if not mode then - mode = ctx.mrm - if not mode then - if pos > stop then return incomplete(ctx) end - mode = byte(code, pos, pos) - pos = pos+1 - end - rm = mode%8; mode = (mode-rm)/8 - sp = mode%8; mode = (mode-sp)/8 - sdisp = "" - if mode < 3 then - if rm == 4 then - if pos > stop then return incomplete(ctx) end - sc = byte(code, pos, pos) - pos = pos+1 - rm = sc%8; sc = (sc-rm)/8 - rx = sc%8; sc = (sc-rx)/8 - if ctx.rexx then rx = rx + 8; ctx.rexx = false end - if rx == 4 then rx = nil end - end - if mode > 0 or rm == 5 then - local dsz = mode - if dsz ~= 1 then dsz = 4 end - local disp = getimm(ctx, pos, dsz); if not disp then return end - if mode == 0 then rm = nil end - if rm or rx or (not sc and ctx.x64 and not ctx.a32) then - if dsz == 1 and disp > 127 then - sdisp = format("-0x%x", 256-disp) - elseif disp >= 0 and disp <= 0x7fffffff then - sdisp = format("+0x%x", disp) - else - sdisp = format("-0x%x", (0xffffffff+1)-disp) - end - else - sdisp = format(ctx.x64 and not ctx.a32 and - not (disp >= 0 and disp <= 0x7fffffff) - and "0xffffffff%08x" or "0x%08x", disp) - end - pos = pos+dsz - end - end - if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end - if ctx.rexr then sp = sp + 8; ctx.rexr = false end - end - if p == "m" then - if mode == 3 then x = regs[rm+1] - else - local aregs = ctx.a32 and map_regs.D or ctx.aregs - local srm, srx = "", "" - if rm then srm = aregs[rm+1] - elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end - ctx.a32 = false - if rx then - if rm then srm = srm.."+" end - srx = aregs[rx+1] - if sc > 0 then srx = srx.."*"..(2^sc) end - end - x = format("[%s%s%s]", srm, srx, sdisp) - end - if mode < 3 and - (not match(pat, "[aRrgp]") or match(pat, "t")) then -- Yuck. - x = map_sz2prefix[sz].." "..x - end - elseif p == "r" then x = regs[sp+1] - elseif p == "g" then x = map_segregs[sp+1] - elseif p == "p" then -- Suppress prefix. - elseif p == "f" then x = "st"..rm - elseif p == "x" then - if sp == 0 and ctx.lock and not ctx.x64 then - x = "CR8"; ctx.lock = false - else - x = "CR"..sp - end - elseif p == "v" then - if ctx.vexv then - x = regs[ctx.vexv+1]; ctx.vexv = false - end - elseif p == "y" then x = "DR"..sp - elseif p == "z" then x = "TR"..sp - elseif p == "l" then vexl = false - elseif p == "t" then - else - error("bad pattern `"..pat.."'") - end - end - if x then operands = operands and operands..", "..x or x end - end - ctx.pos = pos - return putop(ctx, name, operands) -end - --- Forward declaration. -local map_act - --- Fetch and cache MRM byte. -local function getmrm(ctx) - local mrm = ctx.mrm - if not mrm then - local pos = ctx.pos - if pos > ctx.stop then return nil end - mrm = byte(ctx.code, pos, pos) - ctx.pos = pos+1 - ctx.mrm = mrm - end - return mrm -end - --- Dispatch to handler depending on pattern. -local function dispatch(ctx, opat, patgrp) - if not opat then return unknown(ctx) end - if match(opat, "%|") then -- MMX/SSE variants depending on prefix. - local p - if ctx.rep then - p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)" - ctx.rep = false - elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false - else p = "^[^%|]*" end - opat = match(opat, p) - if not opat then return unknown(ctx) end --- ctx.rep = false; ctx.o16 = false - --XXX fails for 66 f2 0f 38 f1 06 crc32 eax,WORD PTR [esi] - --XXX remove in branches? - end - if match(opat, "%$") then -- reg$mem variants. - local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)") - if opat == "" then return unknown(ctx) end - end - if opat == "" then return unknown(ctx) end - local name, pat = match(opat, "^([a-z0-9 ]*)(.*)") - if pat == "" and patgrp then pat = patgrp end - return map_act[sub(pat, 1, 1)](ctx, name, pat) -end - --- Get a pattern from an opcode map and dispatch to handler. -local function dispatchmap(ctx, opcmap) - local pos = ctx.pos - local opat = opcmap[byte(ctx.code, pos, pos)] - pos = pos + 1 - ctx.pos = pos - return dispatch(ctx, opat) -end - --- Map for action codes. The key is the first char after the name. -map_act = { - -- Simple opcodes without operands. - [""] = function(ctx, name, pat) - return putop(ctx, name) - end, - - -- Operand size chars fall right through. - B = putpat, W = putpat, D = putpat, Q = putpat, - V = putpat, U = putpat, T = putpat, - M = putpat, X = putpat, P = putpat, - F = putpat, G = putpat, Y = putpat, - H = putpat, - - -- Collect prefixes. - [":"] = function(ctx, name, pat) - ctx[pat == ":" and name or sub(pat, 2)] = name - if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes. - end, - - -- Chain to special handler specified by name. - ["*"] = function(ctx, name, pat) - return map_act[name](ctx, name, sub(pat, 2)) - end, - - -- Use named subtable for opcode group. - ["!"] = function(ctx, name, pat) - local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2)) - end, - - -- o16,o32[,o64] variants. - sz = function(ctx, name, pat) - if ctx.o16 then ctx.o16 = false - else - pat = match(pat, ",(.*)") - if ctx.rexw then - local p = match(pat, ",(.*)") - if p then pat = p; ctx.rexw = false end - end - end - pat = match(pat, "^[^,]*") - return dispatch(ctx, pat) - end, - - -- Two-byte opcode dispatch. - opc2 = function(ctx, name, pat) - return dispatchmap(ctx, map_opc2) - end, - - -- Three-byte opcode dispatch. - opc3 = function(ctx, name, pat) - return dispatchmap(ctx, map_opc3[pat]) - end, - - -- VMX/SVM dispatch. - vm = function(ctx, name, pat) - return dispatch(ctx, map_opcvm[ctx.mrm]) - end, - - -- Floating point opcode dispatch. - fp = function(ctx, name, pat) - local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - local rm = mrm%8 - local idx = pat*8 + ((mrm-rm)/8)%8 - if mrm >= 192 then idx = idx + 64 end - local opat = map_opcfp[idx] - if type(opat) == "table" then opat = opat[rm+1] end - return dispatch(ctx, opat) - end, - - -- REX prefix. - rex = function(ctx, name, pat) - if ctx.rex then return unknown(ctx) end -- Only 1 REX or VEX prefix allowed. - for p in gmatch(pat, ".") do ctx["rex"..p] = true end - ctx.rex = "rex" - end, - - -- VEX prefix. - vex = function(ctx, name, pat) - if ctx.rex then return unknown(ctx) end -- Only 1 REX or VEX prefix allowed. - ctx.rex = "vex" - local pos = ctx.pos - if ctx.mrm then - ctx.mrm = nil - pos = pos-1 - end - local b = byte(ctx.code, pos, pos) - if not b then return incomplete(ctx) end - pos = pos+1 - if b < 128 then ctx.rexr = true end - local m = 1 - if pat == "3" then - m = b%32; b = (b-m)/32 - local nb = b%2; b = (b-nb)/2 - if nb == 0 then ctx.rexb = true end - local nx = b%2 - if nx == 0 then ctx.rexx = true end - b = byte(ctx.code, pos, pos) - if not b then return incomplete(ctx) end - pos = pos+1 - if b >= 128 then ctx.rexw = true end - end - ctx.pos = pos - local map - if m == 1 then map = map_opc2 - elseif m == 2 then map = map_opc3["38"] - elseif m == 3 then map = map_opc3["3a"] - else return unknown(ctx) end - local p = b%4; b = (b-p)/4 - if p == 1 then ctx.o16 = "o16" - elseif p == 2 then ctx.rep = "rep" - elseif p == 3 then ctx.rep = "repne" end - local l = b%2; b = (b-l)/2 - if l ~= 0 then ctx.vexl = true end - ctx.vexv = (-1-b)%16 - return dispatchmap(ctx, map) - end, - - -- Special case for nop with REX prefix. - nop = function(ctx, name, pat) - return dispatch(ctx, ctx.rex and pat or "nop") - end, - - -- Special case for 0F 77. - emms = function(ctx, name, pat) - if ctx.rex ~= "vex" then - return putop(ctx, "emms") - elseif ctx.vexl then - ctx.vexl = false - return putop(ctx, "zeroall") - else - return putop(ctx, "zeroupper") - end - end, -} - ------------------------------------------------------------------------------- - --- Disassemble a block of code. -local function disass_block(ctx, ofs, len) - if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - ofs = ofs + 1 - ctx.start = ofs - ctx.pos = ofs - ctx.stop = stop - ctx.imm = nil - ctx.mrm = false - clearprefixes(ctx) - while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end - if ctx.pos ~= ctx.start then incomplete(ctx) end -end - --- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). -local function create(code, addr, out) - local ctx = {} - ctx.code = code - ctx.addr = (addr or 0) - 1 - ctx.out = out or io.write - ctx.symtab = {} - ctx.disass = disass_block - ctx.hexdump = 16 - ctx.x64 = false - ctx.map1 = map_opc1_32 - ctx.aregs = map_regs.D - return ctx -end - -local function create64(code, addr, out) - local ctx = create(code, addr, out) - ctx.x64 = true - ctx.map1 = map_opc1_64 - ctx.aregs = map_regs.Q - return ctx -end - --- Simple API: disassemble code (a string) at address and output via out. -local function disass(code, addr, out) - create(code, addr, out):disass() -end - -local function disass64(code, addr, out) - create64(code, addr, out):disass() -end - --- Return register name for RID. -local function regname(r) - if r < 8 then return map_regs.D[r+1] end - return map_regs.X[r-7] -end - -local function regname64(r) - if r < 16 then return map_regs.Q[r+1] end - return map_regs.X[r-15] -end - --- Public module functions. -return { - create = create, - create64 = create64, - disass = disass, - disass64 = disass64, - regname = regname, - regname64 = regname64 -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dump.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dump.lua deleted file mode 100644 index 18e7a4b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/dump.lua +++ /dev/null @@ -1,726 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT compiler dump module. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module can be used to debug the JIT compiler itself. It dumps the --- code representations and structures used in various compiler stages. --- --- Example usage: --- --- luajit -jdump -e "local x=0; for i=1,1e6 do x=x+i end; print(x)" --- luajit -jdump=im -e "for i=1,1000 do for j=1,1000 do end end" | less -R --- luajit -jdump=is myapp.lua | less -R --- luajit -jdump=-b myapp.lua --- luajit -jdump=+aH,myapp.html myapp.lua --- luajit -jdump=ixT,myapp.dump myapp.lua --- --- The first argument specifies the dump mode. The second argument gives --- the output file name. Default output is to stdout, unless the environment --- variable LUAJIT_DUMPFILE is set. The file is overwritten every time the --- module is started. --- --- Different features can be turned on or off with the dump mode. If the --- mode starts with a '+', the following features are added to the default --- set of features; a '-' removes them. Otherwise the features are replaced. --- --- The following dump features are available (* marks the default): --- --- * t Print a line for each started, ended or aborted trace (see also -jv). --- * b Dump the traced bytecode. --- * i Dump the IR (intermediate representation). --- r Augment the IR with register/stack slots. --- s Dump the snapshot map. --- * m Dump the generated machine code. --- x Print each taken trace exit. --- X Print each taken trace exit and the contents of all registers. --- a Print the IR of aborted traces, too. --- --- The output format can be set with the following characters: --- --- T Plain text output. --- A ANSI-colored text output --- H Colorized HTML + CSS output. --- --- The default output format is plain text. It's set to ANSI-colored text --- if the COLORTERM variable is set. Note: this is independent of any output --- redirection, which is actually considered a feature. --- --- You probably want to use less -R to enjoy viewing ANSI-colored text from --- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R" --- ------------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20100, "LuaJIT core/library version mismatch") -local jutil = require("jit.util") -local vmdef = require("jit.vmdef") -local funcinfo, funcbc = jutil.funcinfo, jutil.funcbc -local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek -local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap -local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr -local bit = require("bit") -local band, shr, tohex = bit.band, bit.rshift, bit.tohex -local sub, gsub, format = string.sub, string.gsub, string.format -local byte, rep = string.byte, string.rep -local type, tostring = type, tostring -local stdout, stderr = io.stdout, io.stderr - --- Load other modules on-demand. -local bcline, disass - --- Active flag, output file handle and dump mode. -local active, out, dumpmode - ------------------------------------------------------------------------------- - -local symtabmt = { __index = false } -local symtab = {} -local nexitsym = 0 - --- Fill nested symbol table with per-trace exit stub addresses. -local function fillsymtab_tr(tr, nexit) - local t = {} - symtabmt.__index = t - if jit.arch:sub(1, 4) == "mips" then - t[traceexitstub(tr, 0)] = "exit" - return - end - for i=0,nexit-1 do - local addr = traceexitstub(tr, i) - if addr < 0 then addr = addr + 2^32 end - t[addr] = tostring(i) - end - local addr = traceexitstub(tr, nexit) - if addr then t[addr] = "stack_check" end -end - --- Fill symbol table with trace exit stub addresses. -local function fillsymtab(tr, nexit) - local t = symtab - if nexitsym == 0 then - local maskaddr = jit.arch == "arm" and -2 - local ircall = vmdef.ircall - for i=0,#ircall do - local addr = ircalladdr(i) - if addr ~= 0 then - if maskaddr then addr = band(addr, maskaddr) end - if addr < 0 then addr = addr + 2^32 end - t[addr] = ircall[i] - end - end - end - if nexitsym == 1000000 then -- Per-trace exit stubs. - fillsymtab_tr(tr, nexit) - elseif nexit > nexitsym then -- Shared exit stubs. - for i=nexitsym,nexit-1 do - local addr = traceexitstub(i) - if addr == nil then -- Fall back to per-trace exit stubs. - fillsymtab_tr(tr, nexit) - setmetatable(symtab, symtabmt) - nexit = 1000000 - break - end - if addr < 0 then addr = addr + 2^32 end - t[addr] = tostring(i) - end - nexitsym = nexit - end - return t -end - -local function dumpwrite(s) - out:write(s) -end - --- Disassemble machine code. -local function dump_mcode(tr) - local info = traceinfo(tr) - if not info then return end - local mcode, addr, loop = tracemc(tr) - if not mcode then return end - if not disass then disass = require("jit.dis_"..jit.arch) end - if addr < 0 then addr = addr + 2^32 end - out:write("---- TRACE ", tr, " mcode ", #mcode, "\n") - local ctx = disass.create(mcode, addr, dumpwrite) - ctx.hexdump = 0 - ctx.symtab = fillsymtab(tr, info.nexit) - if loop ~= 0 then - symtab[addr+loop] = "LOOP" - ctx:disass(0, loop) - out:write("->LOOP:\n") - ctx:disass(loop, #mcode-loop) - symtab[addr+loop] = nil - else - ctx:disass(0, #mcode) - end -end - ------------------------------------------------------------------------------- - -local irtype_text = { - [0] = "nil", - "fal", - "tru", - "lud", - "str", - "p32", - "thr", - "pro", - "fun", - "p64", - "cdt", - "tab", - "udt", - "flt", - "num", - "i8 ", - "u8 ", - "i16", - "u16", - "int", - "u32", - "i64", - "u64", - "sfp", -} - -local colortype_ansi = { - [0] = "%s", - "%s", - "%s", - "\027[36m%s\027[m", - "\027[32m%s\027[m", - "%s", - "\027[1m%s\027[m", - "%s", - "\027[1m%s\027[m", - "%s", - "\027[33m%s\027[m", - "\027[31m%s\027[m", - "\027[36m%s\027[m", - "\027[34m%s\027[m", - "\027[34m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", - "\027[35m%s\027[m", -} - -local function colorize_text(s) - return s -end - -local function colorize_ansi(s, t, extra) - local out = format(colortype_ansi[t], s) - if extra then out = "\027[3m"..out end - return out -end - -local irtype_ansi = setmetatable({}, - { __index = function(tab, t) - local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end }) - -local html_escape = { ["<"] = "<", [">"] = ">", ["&"] = "&", } - -local function colorize_html(s, t, extra) - s = gsub(s, "[<>&]", html_escape) - return format('%s', - irtype_text[t], extra and " irt_extra" or "", s) -end - -local irtype_html = setmetatable({}, - { __index = function(tab, t) - local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end }) - -local header_html = [[ - -]] - -local colorize, irtype - --- Lookup tables to convert some literals into names. -local litname = { - ["SLOAD "] = setmetatable({}, { __index = function(t, mode) - local s = "" - if band(mode, 1) ~= 0 then s = s.."P" end - if band(mode, 2) ~= 0 then s = s.."F" end - if band(mode, 4) ~= 0 then s = s.."T" end - if band(mode, 8) ~= 0 then s = s.."C" end - if band(mode, 16) ~= 0 then s = s.."R" end - if band(mode, 32) ~= 0 then s = s.."I" end - if band(mode, 64) ~= 0 then s = s.."K" end - t[mode] = s - return s - end}), - ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", }, - ["CONV "] = setmetatable({}, { __index = function(t, mode) - local s = irtype[band(mode, 31)] - s = irtype[band(shr(mode, 5), 31)].."."..s - if band(mode, 0x800) ~= 0 then s = s.." sext" end - local c = shr(mode, 12) - if c == 1 then s = s.." none" - elseif c == 2 then s = s.." index" - elseif c == 3 then s = s.." check" end - t[mode] = s - return s - end}), - ["FLOAD "] = vmdef.irfield, - ["FREF "] = vmdef.irfield, - ["FPMATH"] = vmdef.irfpm, - ["TMPREF"] = { [0] = "", "IN", "OUT", "INOUT", "", "", "OUT2", "INOUT2" }, - ["BUFHDR"] = { [0] = "RESET", "APPEND", "WRITE" }, - ["TOSTR "] = { [0] = "INT", "NUM", "CHAR" }, -} - -local function ctlsub(c) - if c == "\n" then return "\\n" - elseif c == "\r" then return "\\r" - elseif c == "\t" then return "\\t" - else return format("\\%03d", byte(c)) - end -end - -local function fmtfunc(func, pc) - local fi = funcinfo(func, pc) - if fi.loc then - return fi.loc - elseif fi.ffid then - return vmdef.ffnames[fi.ffid] - elseif fi.addr then - return format("C:%x", fi.addr) - else - return "(?)" - end -end - -local function formatk(tr, idx, sn) - local k, t, slot = tracek(tr, idx) - local tn = type(k) - local s - if tn == "number" then - if t < 12 then - s = k == 0 and "NULL" or format("[0x%08x]", k) - elseif band(sn or 0, 0x30000) ~= 0 then - s = band(sn, 0x20000) ~= 0 and "contpc" or "ftsz" - elseif k == 2^52+2^51 then - s = "bias" - else - s = format(0 < k and k < 0x1p-1026 and "%+a" or "%+.14g", k) - end - elseif tn == "string" then - s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub)) - elseif tn == "function" then - s = fmtfunc(k) - elseif tn == "table" then - s = format("{%p}", k) - elseif tn == "userdata" then - if t == 12 then - s = format("userdata:%p", k) - else - s = format("[%p]", k) - if s == "[NULL]" then s = "NULL" end - end - elseif t == 21 then -- int64_t - s = sub(tostring(k), 1, -3) - if sub(s, 1, 1) ~= "-" then s = "+"..s end - elseif sn == 0x1057fff then -- SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL) - return "----" -- Special case for LJ_FR2 slot 1. - else - s = tostring(k) -- For primitives. - end - s = colorize(format("%-4s", s), t, band(sn or 0, 0x100000) ~= 0) - if slot then - s = format("%s @%d", s, slot) - end - return s -end - -local function printsnap(tr, snap) - local n = 2 - for s=0,snap[1]-1 do - local sn = snap[n] - if shr(sn, 24) == s then - n = n + 1 - local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS - if ref < 0 then - out:write(formatk(tr, ref, sn)) - elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM - out:write(colorize(format("%04d/%04d", ref, ref+1), 14)) - else - local m, ot, op1, op2 = traceir(tr, ref) - out:write(colorize(format("%04d", ref), band(ot, 31), band(sn, 0x100000) ~= 0)) - end - out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME - else - out:write("---- ") - end - end - out:write("]\n") -end - --- Dump snapshots (not interleaved with IR). -local function dump_snap(tr) - out:write("---- TRACE ", tr, " snapshots\n") - for i=0,1000000000 do - local snap = tracesnap(tr, i) - if not snap then break end - out:write(format("#%-3d %04d [ ", i, snap[0])) - printsnap(tr, snap) - end -end - --- Return a register name or stack slot for a rid/sp location. -local function ridsp_name(ridsp, ins) - if not disass then disass = require("jit.dis_"..jit.arch) end - local rid, slot = band(ridsp, 0xff), shr(ridsp, 8) - if rid == 253 or rid == 254 then - return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot) - end - if ridsp > 255 then return format("[%x]", slot*4) end - if rid < 128 then return disass.regname(rid) end - return "" -end - --- Dump CALL* function ref and return optional ctype. -local function dumpcallfunc(tr, ins) - local ctype - if ins > 0 then - local m, ot, op1, op2 = traceir(tr, ins) - if band(ot, 31) == 0 then -- nil type means CARG(func, ctype). - ins = op1 - ctype = formatk(tr, op2) - end - end - if ins < 0 then - out:write(format("[0x%x](", tonumber((tracek(tr, ins))))) - else - out:write(format("%04d (", ins)) - end - return ctype -end - --- Recursively gather CALL* args and dump them. -local function dumpcallargs(tr, ins) - if ins < 0 then - out:write(formatk(tr, ins)) - else - local m, ot, op1, op2 = traceir(tr, ins) - local oidx = 6*shr(ot, 8) - local op = sub(vmdef.irnames, oidx+1, oidx+6) - if op == "CARG " then - dumpcallargs(tr, op1) - if op2 < 0 then - out:write(" ", formatk(tr, op2)) - else - out:write(" ", format("%04d", op2)) - end - else - out:write(format("%04d", ins)) - end - end -end - --- Dump IR and interleaved snapshots. -local function dump_ir(tr, dumpsnap, dumpreg) - local info = traceinfo(tr) - if not info then return end - local nins = info.nins - out:write("---- TRACE ", tr, " IR\n") - local irnames = vmdef.irnames - local snapref = 65536 - local snap, snapno - if dumpsnap then - snap = tracesnap(tr, 0) - snapref = snap[0] - snapno = 0 - end - for ins=1,nins do - if ins >= snapref then - if dumpreg then - out:write(format(".... SNAP #%-3d [ ", snapno)) - else - out:write(format(".... SNAP #%-3d [ ", snapno)) - end - printsnap(tr, snap) - snapno = snapno + 1 - snap = tracesnap(tr, snapno) - snapref = snap and snap[0] or 65536 - end - local m, ot, op1, op2, ridsp = traceir(tr, ins) - local oidx, t = 6*shr(ot, 8), band(ot, 31) - local op = sub(irnames, oidx+1, oidx+6) - if op == "LOOP " then - if dumpreg then - out:write(format("%04d ------------ LOOP ------------\n", ins)) - else - out:write(format("%04d ------ LOOP ------------\n", ins)) - end - elseif op ~= "NOP " and op ~= "CARG " and - (dumpreg or op ~= "RENAME") then - local rid = band(ridsp, 255) - if dumpreg then - out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins))) - else - out:write(format("%04d ", ins)) - end - out:write(format("%s%s %s %s ", - (rid == 254 or rid == 253) and "}" or - (band(ot, 128) == 0 and " " or ">"), - band(ot, 64) == 0 and " " or "+", - irtype[t], op)) - local m1, m2 = band(m, 3), band(m, 3*4) - if sub(op, 1, 4) == "CALL" then - local ctype - if m2 == 1*4 then -- op2 == IRMlit - out:write(format("%-10s (", vmdef.ircall[op2])) - else - ctype = dumpcallfunc(tr, op2) - end - if op1 ~= -1 then dumpcallargs(tr, op1) end - out:write(")") - if ctype then out:write(" ctype ", ctype) end - elseif op == "CNEW " and op2 == -1 then - out:write(formatk(tr, op1)) - elseif m1 ~= 3 then -- op1 != IRMnone - if op1 < 0 then - out:write(formatk(tr, op1)) - else - out:write(format(m1 == 0 and "%04d" or "#%-3d", op1)) - end - if m2 ~= 3*4 then -- op2 != IRMnone - if m2 == 1*4 then -- op2 == IRMlit - local litn = litname[op] - if litn and litn[op2] then - out:write(" ", litn[op2]) - elseif op == "UREFO " or op == "UREFC " then - out:write(format(" #%-3d", shr(op2, 8))) - else - out:write(format(" #%-3d", op2)) - end - elseif op2 < 0 then - out:write(" ", formatk(tr, op2)) - else - out:write(format(" %04d", op2)) - end - end - end - out:write("\n") - end - end - if snap then - if dumpreg then - out:write(format(".... SNAP #%-3d [ ", snapno)) - else - out:write(format(".... SNAP #%-3d [ ", snapno)) - end - printsnap(tr, snap) - end -end - ------------------------------------------------------------------------------- - -local recprefix = "" -local recdepth = 0 - --- Format trace error message. -local function fmterr(err, info) - if type(err) == "number" then - if type(info) == "function" then info = fmtfunc(info) end - err = format(vmdef.traceerr[err], info) - end - return err -end - --- Dump trace states. -local function dump_trace(what, tr, func, pc, otr, oex) - if what == "stop" or (what == "abort" and dumpmode.a) then - if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop") - elseif dumpmode.s then dump_snap(tr) end - if dumpmode.m then dump_mcode(tr) end - end - if what == "start" then - if dumpmode.H then out:write('
\n') end
-    out:write("---- TRACE ", tr, " ", what)
-    if otr then out:write(" ", otr, "/", oex == -1 and "stitch" or oex) end
-    out:write(" ", fmtfunc(func, pc), "\n")
-  elseif what == "stop" or what == "abort" then
-    out:write("---- TRACE ", tr, " ", what)
-    if what == "abort" then
-      out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
-    else
-      local info = traceinfo(tr)
-      local link, ltype = info.link, info.linktype
-      if link == tr or link == 0 then
-	out:write(" -> ", ltype, "\n")
-      elseif ltype == "root" then
-	out:write(" -> ", link, "\n")
-      else
-	out:write(" -> ", link, " ", ltype, "\n")
-      end
-    end
-    if dumpmode.H then out:write("
\n\n") else out:write("\n") end - else - if what == "flush" then symtab, nexitsym = {}, 0 end - out:write("---- TRACE ", what, "\n\n") - end - out:flush() -end - --- Dump recorded bytecode. -local function dump_record(tr, func, pc, depth) - if depth ~= recdepth then - recdepth = depth - recprefix = rep(" .", depth) - end - local line - if pc >= 0 then - line = bcline(func, pc, recprefix) - if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end - else - line = "0000 "..recprefix.." FUNCC \n" - end - if pc <= 0 then - out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n") - else - out:write(line) - end - if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC - out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond. - end -end - ------------------------------------------------------------------------------- - -local gpr64 = jit.arch:match("64") -local fprmips32 = jit.arch == "mips" or jit.arch == "mipsel" - --- Dump taken trace exits. -local function dump_texit(tr, ex, ngpr, nfpr, ...) - out:write("---- TRACE ", tr, " exit ", ex, "\n") - if dumpmode.X then - local regs = {...} - if gpr64 then - for i=1,ngpr do - out:write(format(" %016x", regs[i])) - if i % 4 == 0 then out:write("\n") end - end - else - for i=1,ngpr do - out:write(" ", tohex(regs[i])) - if i % 8 == 0 then out:write("\n") end - end - end - if fprmips32 then - for i=1,nfpr,2 do - out:write(format(" %+17.14g", regs[ngpr+i])) - if i % 8 == 7 then out:write("\n") end - end - else - for i=1,nfpr do - out:write(format(" %+17.14g", regs[ngpr+i])) - if i % 4 == 0 then out:write("\n") end - end - end - end -end - ------------------------------------------------------------------------------- - --- Detach dump handlers. -local function dumpoff() - if active then - active = false - jit.attach(dump_texit) - jit.attach(dump_record) - jit.attach(dump_trace) - if out and out ~= stdout and out ~= stderr then out:close() end - out = nil - end -end - --- Open the output file and attach dump handlers. -local function dumpon(opt, outfile) - if active then dumpoff() end - - local term = os.getenv("TERM") - local colormode = (term and term:match("color") or os.getenv("COLORTERM")) and "A" or "T" - if opt then - opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end) - end - - local m = { t=true, b=true, i=true, m=true, } - if opt and opt ~= "" then - local o = sub(opt, 1, 1) - if o ~= "+" and o ~= "-" then m = {} end - for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end - end - dumpmode = m - - if m.t or m.b or m.i or m.s or m.m then - jit.attach(dump_trace, "trace") - end - if m.b then - jit.attach(dump_record, "record") - if not bcline then bcline = require("jit.bc").line end - end - if m.x or m.X then - jit.attach(dump_texit, "texit") - end - - if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stdout - end - - m[colormode] = true - if colormode == "A" then - colorize = colorize_ansi - irtype = irtype_ansi - elseif colormode == "H" then - colorize = colorize_html - irtype = irtype_html - out:write(header_html) - else - colorize = colorize_text - irtype = irtype_text - end - - active = true -end - --- Public module functions. -return { - on = dumpon, - off = dumpoff, - start = dumpon -- For -j command line option. -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/p.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/p.lua deleted file mode 100644 index f225c31..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/p.lua +++ /dev/null @@ -1,312 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT profiler. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module is a simple command line interface to the built-in --- low-overhead profiler of LuaJIT. --- --- The lower-level API of the profiler is accessible via the "jit.profile" --- module or the luaJIT_profile_* C API. --- --- Example usage: --- --- luajit -jp myapp.lua --- luajit -jp=s myapp.lua --- luajit -jp=-s myapp.lua --- luajit -jp=vl myapp.lua --- luajit -jp=G,profile.txt myapp.lua --- --- The following dump features are available: --- --- f Stack dump: function name, otherwise module:line. Default mode. --- F Stack dump: ditto, but always prepend module. --- l Stack dump: module:line. --- stack dump depth (callee < caller). Default: 1. --- - Inverse stack dump depth (caller > callee). --- s Split stack dump after first stack level. Implies abs(depth) >= 2. --- p Show full path for module names. --- v Show VM states. Can be combined with stack dumps, e.g. vf or fv. --- z Show zones. Can be combined with stack dumps, e.g. zf or fz. --- r Show raw sample counts. Default: show percentages. --- a Annotate excerpts from source code files. --- A Annotate complete source code files. --- G Produce raw output suitable for graphical tools (e.g. flame graphs). --- m Minimum sample percentage to be shown. Default: 3. --- i Sampling interval in milliseconds. Default: 10. --- ----------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20100, "LuaJIT core/library version mismatch") -local profile = require("jit.profile") -local vmdef = require("jit.vmdef") -local math = math -local pairs, ipairs, tonumber, floor = pairs, ipairs, tonumber, math.floor -local sort, format = table.sort, string.format -local stdout = io.stdout -local zone -- Load jit.zone module on demand. - --- Output file handle. -local out - ------------------------------------------------------------------------------- - -local prof_ud -local prof_states, prof_split, prof_min, prof_raw, prof_fmt, prof_depth -local prof_ann, prof_count1, prof_count2, prof_samples - -local map_vmmode = { - N = "Compiled", - I = "Interpreted", - C = "C code", - G = "Garbage Collector", - J = "JIT Compiler", -} - --- Profiler callback. -local function prof_cb(th, samples, vmmode) - prof_samples = prof_samples + samples - local key_stack, key_stack2, key_state - -- Collect keys for sample. - if prof_states then - if prof_states == "v" then - key_state = map_vmmode[vmmode] or vmmode - else - key_state = zone:get() or "(none)" - end - end - if prof_fmt then - key_stack = profile.dumpstack(th, prof_fmt, prof_depth) - key_stack = key_stack:gsub("%[builtin#(%d+)%]", function(x) - return vmdef.ffnames[tonumber(x)] - end) - if prof_split == 2 then - local k1, k2 = key_stack:match("(.-) [<>] (.*)") - if k2 then key_stack, key_stack2 = k1, k2 end - elseif prof_split == 3 then - key_stack2 = profile.dumpstack(th, "l", 1) - end - end - -- Order keys. - local k1, k2 - if prof_split == 1 then - if key_state then - k1 = key_state - if key_stack then k2 = key_stack end - end - elseif key_stack then - k1 = key_stack - if key_stack2 then k2 = key_stack2 elseif key_state then k2 = key_state end - end - -- Coalesce samples in one or two levels. - if k1 then - local t1 = prof_count1 - t1[k1] = (t1[k1] or 0) + samples - if k2 then - local t2 = prof_count2 - local t3 = t2[k1] - if not t3 then t3 = {}; t2[k1] = t3 end - t3[k2] = (t3[k2] or 0) + samples - end - end -end - ------------------------------------------------------------------------------- - --- Show top N list. -local function prof_top(count1, count2, samples, indent) - local t, n = {}, 0 - for k in pairs(count1) do - n = n + 1 - t[n] = k - end - sort(t, function(a, b) return count1[a] > count1[b] end) - for i=1,n do - local k = t[i] - local v = count1[k] - local pct = floor(v*100/samples + 0.5) - if pct < prof_min then break end - if not prof_raw then - out:write(format("%s%2d%% %s\n", indent, pct, k)) - elseif prof_raw == "r" then - out:write(format("%s%5d %s\n", indent, v, k)) - else - out:write(format("%s %d\n", k, v)) - end - if count2 then - local r = count2[k] - if r then - prof_top(r, nil, v, (prof_split == 3 or prof_split == 1) and " -- " or - (prof_depth < 0 and " -> " or " <- ")) - end - end - end -end - --- Annotate source code -local function prof_annotate(count1, samples) - local files = {} - local ms = 0 - for k, v in pairs(count1) do - local pct = floor(v*100/samples + 0.5) - ms = math.max(ms, v) - if pct >= prof_min then - local file, line = k:match("^(.*):(%d+)$") - if not file then file = k; line = 0 end - local fl = files[file] - if not fl then fl = {}; files[file] = fl; files[#files+1] = file end - line = tonumber(line) - fl[line] = prof_raw and v or pct - end - end - sort(files) - local fmtv, fmtn = " %3d%% | %s\n", " | %s\n" - if prof_raw then - local n = math.max(5, math.ceil(math.log10(ms))) - fmtv = "%"..n.."d | %s\n" - fmtn = (" "):rep(n).." | %s\n" - end - local ann = prof_ann - for _, file in ipairs(files) do - local f0 = file:byte() - if f0 == 40 or f0 == 91 then - out:write(format("\n====== %s ======\n[Cannot annotate non-file]\n", file)) - break - end - local fp, err = io.open(file) - if not fp then - out:write(format("====== ERROR: %s: %s\n", file, err)) - break - end - out:write(format("\n====== %s ======\n", file)) - local fl = files[file] - local n, show = 1, false - if ann ~= 0 then - for i=1,ann do - if fl[i] then show = true; out:write("@@ 1 @@\n"); break end - end - end - for line in fp:lines() do - if line:byte() == 27 then - out:write("[Cannot annotate bytecode file]\n") - break - end - local v = fl[n] - if ann ~= 0 then - local v2 = fl[n+ann] - if show then - if v2 then show = n+ann elseif v then show = n - elseif show+ann < n then show = false end - elseif v2 then - show = n+ann - out:write(format("@@ %d @@\n", n)) - end - if not show then goto next end - end - if v then - out:write(format(fmtv, v, line)) - else - out:write(format(fmtn, line)) - end - ::next:: - n = n + 1 - end - fp:close() - end -end - ------------------------------------------------------------------------------- - --- Finish profiling and dump result. -local function prof_finish() - if prof_ud then - profile.stop() - local samples = prof_samples - if samples == 0 then - if prof_raw ~= true then out:write("[No samples collected]\n") end - return - end - if prof_ann then - prof_annotate(prof_count1, samples) - else - prof_top(prof_count1, prof_count2, samples, "") - end - prof_count1 = nil - prof_count2 = nil - prof_ud = nil - if out ~= stdout then out:close() end - end -end - --- Start profiling. -local function prof_start(mode) - local interval = "" - mode = mode:gsub("i%d*", function(s) interval = s; return "" end) - prof_min = 3 - mode = mode:gsub("m(%d+)", function(s) prof_min = tonumber(s); return "" end) - prof_depth = 1 - mode = mode:gsub("%-?%d+", function(s) prof_depth = tonumber(s); return "" end) - local m = {} - for c in mode:gmatch(".") do m[c] = c end - prof_states = m.z or m.v - if prof_states == "z" then zone = require("jit.zone") end - local scope = m.l or m.f or m.F or (prof_states and "" or "f") - local flags = (m.p or "") - prof_raw = m.r - if m.s then - prof_split = 2 - if prof_depth == -1 or m["-"] then prof_depth = -2 - elseif prof_depth == 1 then prof_depth = 2 end - elseif mode:find("[fF].*l") then - scope = "l" - prof_split = 3 - else - prof_split = (scope == "" or mode:find("[zv].*[lfF]")) and 1 or 0 - end - prof_ann = m.A and 0 or (m.a and 3) - if prof_ann then - scope = "l" - prof_fmt = "pl" - prof_split = 0 - prof_depth = 1 - elseif m.G and scope ~= "" then - prof_fmt = flags..scope.."Z;" - prof_depth = -100 - prof_raw = true - prof_min = 0 - elseif scope == "" then - prof_fmt = false - else - local sc = prof_split == 3 and m.f or m.F or scope - prof_fmt = flags..sc..(prof_depth >= 0 and "Z < " or "Z > ") - end - prof_count1 = {} - prof_count2 = {} - prof_samples = 0 - profile.start(scope:lower()..interval, prof_cb) - prof_ud = newproxy(true) - getmetatable(prof_ud).__gc = prof_finish -end - ------------------------------------------------------------------------------- - -local function start(mode, outfile) - if not outfile then outfile = os.getenv("LUAJIT_PROFILEFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stdout - end - prof_start(mode or "f") -end - --- Public module functions. -return { - start = start, -- For -j command line option. - stop = prof_finish -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/v.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/v.lua deleted file mode 100644 index ac8b19d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/v.lua +++ /dev/null @@ -1,170 +0,0 @@ ----------------------------------------------------------------------------- --- Verbose mode of the LuaJIT compiler. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module shows verbose information about the progress of the --- JIT compiler. It prints one line for each generated trace. This module --- is useful to see which code has been compiled or where the compiler --- punts and falls back to the interpreter. --- --- Example usage: --- --- luajit -jv -e "for i=1,1000 do for j=1,1000 do end end" --- luajit -jv=myapp.out myapp.lua --- --- Default output is to stderr. To redirect the output to a file, pass a --- filename as an argument (use '-' for stdout) or set the environment --- variable LUAJIT_VERBOSEFILE. The file is overwritten every time the --- module is started. --- --- The output from the first example should look like this: --- --- [TRACE 1 (command line):1 loop] --- [TRACE 2 (1/3) (command line):1 -> 1] --- --- The first number in each line is the internal trace number. Next are --- the file name ('(command line)') and the line number (':1') where the --- trace has started. Side traces also show the parent trace number and --- the exit number where they are attached to in parentheses ('(1/3)'). --- An arrow at the end shows where the trace links to ('-> 1'), unless --- it loops to itself. --- --- In this case the inner loop gets hot and is traced first, generating --- a root trace. Then the last exit from the 1st trace gets hot, too, --- and triggers generation of the 2nd trace. The side trace follows the --- path along the outer loop and *around* the inner loop, back to its --- start, and then links to the 1st trace. Yes, this may seem unusual, --- if you know how traditional compilers work. Trace compilers are full --- of surprises like this -- have fun! :-) --- --- Aborted traces are shown like this: --- --- [TRACE --- foo.lua:44 -- leaving loop in root trace at foo:lua:50] --- --- Don't worry -- trace aborts are quite common, even in programs which --- can be fully compiled. The compiler may retry several times until it --- finds a suitable trace. --- --- Of course this doesn't work with features that are not-yet-implemented --- (NYI error messages). The VM simply falls back to the interpreter. This --- may not matter at all if the particular trace is not very high up in --- the CPU usage profile. Oh, and the interpreter is quite fast, too. --- --- Also check out the -jdump module, which prints all the gory details. --- ------------------------------------------------------------------------------- - --- Cache some library functions and objects. -local jit = require("jit") -assert(jit.version_num == 20100, "LuaJIT core/library version mismatch") -local jutil = require("jit.util") -local vmdef = require("jit.vmdef") -local funcinfo, traceinfo = jutil.funcinfo, jutil.traceinfo -local type, format = type, string.format -local stdout, stderr = io.stdout, io.stderr - --- Active flag and output file handle. -local active, out - ------------------------------------------------------------------------------- - -local startloc, startex - -local function fmtfunc(func, pc) - local fi = funcinfo(func, pc) - if fi.loc then - return fi.loc - elseif fi.ffid then - return vmdef.ffnames[fi.ffid] - elseif fi.addr then - return format("C:%x", fi.addr) - else - return "(?)" - end -end - --- Format trace error message. -local function fmterr(err, info) - if type(err) == "number" then - if type(info) == "function" then info = fmtfunc(info) end - err = format(vmdef.traceerr[err], info) - end - return err -end - --- Dump trace states. -local function dump_trace(what, tr, func, pc, otr, oex) - if what == "start" then - startloc = fmtfunc(func, pc) - startex = otr and "("..otr.."/"..(oex == -1 and "stitch" or oex)..") " or "" - else - if what == "abort" then - local loc = fmtfunc(func, pc) - if loc ~= startloc then - out:write(format("[TRACE --- %s%s -- %s at %s]\n", - startex, startloc, fmterr(otr, oex), loc)) - else - out:write(format("[TRACE --- %s%s -- %s]\n", - startex, startloc, fmterr(otr, oex))) - end - elseif what == "stop" then - local info = traceinfo(tr) - local link, ltype = info.link, info.linktype - if ltype == "interpreter" then - out:write(format("[TRACE %3s %s%s -- fallback to interpreter]\n", - tr, startex, startloc)) - elseif ltype == "stitch" then - out:write(format("[TRACE %3s %s%s %s %s]\n", - tr, startex, startloc, ltype, fmtfunc(func, pc))) - elseif link == tr or link == 0 then - out:write(format("[TRACE %3s %s%s %s]\n", - tr, startex, startloc, ltype)) - elseif ltype == "root" then - out:write(format("[TRACE %3s %s%s -> %d]\n", - tr, startex, startloc, link)) - else - out:write(format("[TRACE %3s %s%s -> %d %s]\n", - tr, startex, startloc, link, ltype)) - end - else - out:write(format("[TRACE %s]\n", what)) - end - out:flush() - end -end - ------------------------------------------------------------------------------- - --- Detach dump handlers. -local function dumpoff() - if active then - active = false - jit.attach(dump_trace) - if out and out ~= stdout and out ~= stderr then out:close() end - out = nil - end -end - --- Open the output file and attach dump handlers. -local function dumpon(outfile) - if active then dumpoff() end - if not outfile then outfile = os.getenv("LUAJIT_VERBOSEFILE") end - if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) - else - out = stderr - end - jit.attach(dump_trace, "trace") - active = true -end - --- Public module functions. -return { - on = dumpon, - off = dumpoff, - start = dumpon -- For -j command line option. -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/zone.lua b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/zone.lua deleted file mode 100644 index 1308cb7..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/jit/zone.lua +++ /dev/null @@ -1,45 +0,0 @@ ----------------------------------------------------------------------------- --- LuaJIT profiler zones. --- --- Copyright (C) 2005-2022 Mike Pall. All rights reserved. --- Released under the MIT license. See Copyright Notice in luajit.h ----------------------------------------------------------------------------- --- --- This module implements a simple hierarchical zone model. --- --- Example usage: --- --- local zone = require("jit.zone") --- zone("AI") --- ... --- zone("A*") --- ... --- print(zone:get()) --> "A*" --- ... --- zone() --- ... --- print(zone:get()) --> "AI" --- ... --- zone() --- ----------------------------------------------------------------------------- - -local remove = table.remove - -return setmetatable({ - flush = function(t) - for i=#t,1,-1 do t[i] = nil end - end, - get = function(t) - return t[#t] - end -}, { - __call = function(t, zone) - if zone then - t[#t+1] = zone - else - return (assert(remove(t), "empty zone stack")) - end - end -}) - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lauxlib.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lauxlib.h deleted file mode 100644 index a44f027..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lauxlib.h +++ /dev/null @@ -1,161 +0,0 @@ -/* -** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ -** Auxiliary functions for building Lua libraries -** See Copyright Notice in lua.h -*/ - - -#ifndef lauxlib_h -#define lauxlib_h - - -#include -#include - -#include "lua.h" - - -/* extra error code for `luaL_load' */ -#define LUA_ERRFILE (LUA_ERRERR+1) - -typedef struct luaL_Reg { - const char *name; - lua_CFunction func; -} luaL_Reg; - -LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); -LUALIB_API void (luaL_register) (lua_State *L, const char *libname, - const luaL_Reg *l); -LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); -LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); -LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); -LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, - size_t *l); -LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, - const char *def, size_t *l); -LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); -LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); - -LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); -LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, - lua_Integer def); - -LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); -LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); -LUALIB_API void (luaL_checkany) (lua_State *L, int narg); - -LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); -LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); - -LUALIB_API void (luaL_where) (lua_State *L, int lvl); -LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); - -LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, - const char *const lst[]); - -/* pre-defined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - -LUALIB_API int (luaL_ref) (lua_State *L, int t); -LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); - -LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); -LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, - const char *name); -LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); - -LUALIB_API lua_State *(luaL_newstate) (void); - - -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); - -LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, - const char *fname, int szhint); - -/* From Lua 5.2. */ -LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname); -LUALIB_API int luaL_execresult(lua_State *L, int stat); -LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, - const char *mode); -LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, - const char *name, const char *mode); -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, - int level); -LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); -LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, - int sizehint); -LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); -LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define luaL_argcheck(L, cond,numarg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) -#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) -#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) -#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) -#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) -#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) -#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) - -#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - -#define luaL_dofile(L, fn) \ - (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_dostring(L, s) \ - (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - -#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) - -#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) - -/* From Lua 5.2. */ -#define luaL_newlibtable(L, l) \ - lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) -#define luaL_newlib(L, l) (luaL_newlibtable(L, l), luaL_setfuncs(L, l, 0)) - -/* -** {====================================================== -** Generic Buffer manipulation -** ======================================================= -*/ - - - -typedef struct luaL_Buffer { - char *p; /* current position in buffer */ - int lvl; /* number of strings in the stack (level) */ - lua_State *L; - char buffer[LUAL_BUFFERSIZE]; -} luaL_Buffer; - -#define luaL_addchar(B,c) \ - ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ - (*(B)->p++ = (char)(c))) - -/* compatibility only */ -#define luaL_putchar(B,c) luaL_addchar(B,c) - -#define luaL_addsize(B,n) ((B)->p += (n)) - -LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); -LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); -LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); -LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); -LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); -LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); - - -/* }====================================================== */ - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_aux.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_aux.c deleted file mode 100644 index b8e5643..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_aux.c +++ /dev/null @@ -1,370 +0,0 @@ -/* -** Auxiliary library for the Lua/C API. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major parts taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include -#include - -#define lib_aux_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_state.h" -#include "lj_trace.h" -#include "lj_lib.h" - -#if LJ_TARGET_POSIX -#include -#endif - -/* -- I/O error handling -------------------------------------------------- */ - -LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname) -{ - if (stat) { - setboolV(L->top++, 1); - return 1; - } else { - int en = errno; /* Lua API calls may change this value. */ - setnilV(L->top++); - if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); - else - lua_pushfstring(L, "%s", strerror(en)); - setintV(L->top++, en); - lj_trace_abort(G(L)); - return 3; - } -} - -LUALIB_API int luaL_execresult(lua_State *L, int stat) -{ - if (stat != -1) { -#if LJ_TARGET_POSIX - if (WIFSIGNALED(stat)) { - stat = WTERMSIG(stat); - setnilV(L->top++); - lua_pushliteral(L, "signal"); - } else { - if (WIFEXITED(stat)) - stat = WEXITSTATUS(stat); - if (stat == 0) - setboolV(L->top++, 1); - else - setnilV(L->top++); - lua_pushliteral(L, "exit"); - } -#else - if (stat == 0) - setboolV(L->top++, 1); - else - setnilV(L->top++); - lua_pushliteral(L, "exit"); -#endif - setintV(L->top++, stat); - return 3; - } - return luaL_fileresult(L, 0, NULL); -} - -/* -- Module registration ------------------------------------------------- */ - -LUALIB_API const char *luaL_findtable(lua_State *L, int idx, - const char *fname, int szhint) -{ - const char *e; - lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, (size_t)(e - fname)); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { /* no such field? */ - lua_pop(L, 1); /* remove this nil */ - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ - lua_pushlstring(L, fname, (size_t)(e - fname)); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - -static int libsize(const luaL_Reg *l) -{ - int size = 0; - for (; l && l->name; l++) size++; - return size; -} - -LUALIB_API void luaL_pushmodule(lua_State *L, const char *modname, int sizehint) -{ - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); - lua_getfield(L, -1, modname); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, sizehint) != NULL) - lj_err_callerv(L, LJ_ERR_BADMODN, modname); - lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); /* _LOADED[modname] = new table. */ - } - lua_remove(L, -2); /* Remove _LOADED table. */ -} - -LUALIB_API void luaL_openlib(lua_State *L, const char *libname, - const luaL_Reg *l, int nup) -{ - lj_lib_checkfpu(L); - if (libname) { - luaL_pushmodule(L, libname, libsize(l)); - lua_insert(L, -(nup + 1)); /* Move module table below upvalues. */ - } - if (l) - luaL_setfuncs(L, l, nup); - else - lua_pop(L, nup); /* Remove upvalues. */ -} - -LUALIB_API void luaL_register(lua_State *L, const char *libname, - const luaL_Reg *l) -{ - luaL_openlib(L, libname, l, 0); -} - -LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) -{ - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name; l++) { - int i; - for (i = 0; i < nup; i++) /* Copy upvalues to the top. */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* Remove upvalues. */ -} - -LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, - const char *p, const char *r) -{ - const char *wild; - size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); - while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, (size_t)(wild - s)); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ - s = wild + l; /* continue after `p' */ - } - luaL_addstring(&b, s); /* push last suffix */ - luaL_pushresult(&b); - return lua_tostring(L, -1); -} - -/* -- Buffer handling ----------------------------------------------------- */ - -#define bufflen(B) ((size_t)((B)->p - (B)->buffer)) -#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) - -static int emptybuffer(luaL_Buffer *B) -{ - size_t l = bufflen(B); - if (l == 0) - return 0; /* put nothing on stack */ - lua_pushlstring(B->L, B->buffer, l); - B->p = B->buffer; - B->lvl++; - return 1; -} - -static void adjuststack(luaL_Buffer *B) -{ - if (B->lvl > 1) { - lua_State *L = B->L; - int toget = 1; /* number of levels to concat */ - size_t toplen = lua_strlen(L, -1); - do { - size_t l = lua_strlen(L, -(toget+1)); - if (!(B->lvl - toget + 1 >= LUA_MINSTACK/2 || toplen > l)) - break; - toplen += l; - toget++; - } while (toget < B->lvl); - lua_concat(L, toget); - B->lvl = B->lvl - toget + 1; - } -} - -LUALIB_API char *luaL_prepbuffer(luaL_Buffer *B) -{ - if (emptybuffer(B)) - adjuststack(B); - return B->buffer; -} - -LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l) -{ - if (l <= bufffree(B)) { - memcpy(B->p, s, l); - B->p += l; - } else { - emptybuffer(B); - lua_pushlstring(B->L, s, l); - B->lvl++; - adjuststack(B); - } -} - -LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s) -{ - luaL_addlstring(B, s, strlen(s)); -} - -LUALIB_API void luaL_pushresult(luaL_Buffer *B) -{ - emptybuffer(B); - lua_concat(B->L, B->lvl); - B->lvl = 1; -} - -LUALIB_API void luaL_addvalue(luaL_Buffer *B) -{ - lua_State *L = B->L; - size_t vl; - const char *s = lua_tolstring(L, -1, &vl); - if (vl <= bufffree(B)) { /* fit into buffer? */ - memcpy(B->p, s, vl); /* put it there */ - B->p += vl; - lua_pop(L, 1); /* remove from stack */ - } else { - if (emptybuffer(B)) - lua_insert(L, -2); /* put buffer before new value */ - B->lvl++; /* add new value into B stack */ - adjuststack(B); - } -} - -LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B) -{ - B->L = L; - B->p = B->buffer; - B->lvl = 0; -} - -/* -- Reference management ------------------------------------------------ */ - -#define FREELIST_REF 0 - -/* Convert a stack index to an absolute index. */ -#define abs_index(L, i) \ - ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1) - -LUALIB_API int luaL_ref(lua_State *L, int t) -{ - int ref; - t = abs_index(L, t); - if (lua_isnil(L, -1)) { - lua_pop(L, 1); /* remove from stack */ - return LUA_REFNIL; /* `nil' has a unique fixed reference */ - } - lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ - lua_pop(L, 1); /* remove it from stack */ - if (ref != 0) { /* any free element? */ - lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ - } else { /* no free elements */ - ref = (int)lua_objlen(L, t); - ref++; /* create new reference */ - } - lua_rawseti(L, t, ref); - return ref; -} - -LUALIB_API void luaL_unref(lua_State *L, int t, int ref) -{ - if (ref >= 0) { - t = abs_index(L, t); - lua_rawgeti(L, t, FREELIST_REF); - lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ - lua_pushinteger(L, ref); - lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ - } -} - -/* -- Default allocator and panic function -------------------------------- */ - -static int panic(lua_State *L) -{ - const char *s = lua_tostring(L, -1); - fputs("PANIC: unprotected error in call to Lua API (", stderr); - fputs(s ? s : "?", stderr); - fputc(')', stderr); fputc('\n', stderr); - fflush(stderr); - return 0; -} - -#ifdef LUAJIT_USE_SYSMALLOC - -#if LJ_64 && !LJ_GC64 && !defined(LUAJIT_USE_VALGRIND) -#error "Must use builtin allocator for 64 bit target" -#endif - -static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize) -{ - (void)ud; - (void)osize; - if (nsize == 0) { - free(ptr); - return NULL; - } else { - return realloc(ptr, nsize); - } -} - -LUALIB_API lua_State *luaL_newstate(void) -{ - lua_State *L = lua_newstate(mem_alloc, NULL); - if (L) G(L)->panic = panic; - return L; -} - -#else - -LUALIB_API lua_State *luaL_newstate(void) -{ - lua_State *L; -#if LJ_64 && !LJ_GC64 - L = lj_state_newstate(LJ_ALLOCF_INTERNAL, NULL); -#else - L = lua_newstate(LJ_ALLOCF_INTERNAL, NULL); -#endif - if (L) G(L)->panic = panic; - return L; -} - -#if LJ_64 && !LJ_GC64 -LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) -{ - UNUSED(f); UNUSED(ud); - fputs("Must use luaL_newstate() for 64 bit target\n", stderr); - return NULL; -} -#endif - -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_base.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_base.c deleted file mode 100644 index 98ec67c..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_base.c +++ /dev/null @@ -1,696 +0,0 @@ -/* -** Base and coroutine library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include - -#define lib_base_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cconv.h" -#endif -#include "lj_bc.h" -#include "lj_ff.h" -#include "lj_dispatch.h" -#include "lj_char.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" -#include "lj_lib.h" - -/* -- Base library: checks ------------------------------------------------ */ - -#define LJLIB_MODULE_base - -LJLIB_ASM(assert) LJLIB_REC(.) -{ - lj_lib_checkany(L, 1); - if (L->top == L->base+1) - lj_err_caller(L, LJ_ERR_ASSERT); - else if (tvisstr(L->base+1) || tvisnumber(L->base+1)) - lj_err_callermsg(L, strdata(lj_lib_checkstr(L, 2))); - else - lj_err_run(L); - return FFH_UNREACHABLE; -} - -/* ORDER LJ_T */ -LJLIB_PUSH("nil") -LJLIB_PUSH("boolean") -LJLIB_PUSH(top-1) /* boolean */ -LJLIB_PUSH("userdata") -LJLIB_PUSH("string") -LJLIB_PUSH("upval") -LJLIB_PUSH("thread") -LJLIB_PUSH("proto") -LJLIB_PUSH("function") -LJLIB_PUSH("trace") -LJLIB_PUSH("cdata") -LJLIB_PUSH("table") -LJLIB_PUSH(top-9) /* userdata */ -LJLIB_PUSH("number") -LJLIB_ASM_(type) LJLIB_REC(.) -/* Recycle the lj_lib_checkany(L, 1) from assert. */ - -/* -- Base library: iterators --------------------------------------------- */ - -/* This solves a circular dependency problem -- change FF_next_N as needed. */ -LJ_STATIC_ASSERT((int)FF_next == FF_next_N); - -LJLIB_ASM(next) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_err_msg(L, LJ_ERR_NEXTIDX); - return FFH_UNREACHABLE; -} - -#if LJ_52 || LJ_HASFFI -static int ffh_pairs(lua_State *L, MMS mm) -{ - TValue *o = lj_lib_checkany(L, 1); - cTValue *mo = lj_meta_lookup(L, o, mm); - if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) { - L->top = o+1; /* Only keep one argument. */ - copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */ - return FFH_TAILCALL; - } else { - if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE); - if (LJ_FR2) { copyTV(L, o-1, o); o--; } - setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1))); - if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0); - return FFH_RES(3); - } -} -#else -#define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE) -#endif - -LJLIB_PUSH(lastcl) -LJLIB_ASM(pairs) LJLIB_REC(xpairs 0) -{ - return ffh_pairs(L, MM_pairs); -} - -LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_lib_checkint(L, 2); - return FFH_UNREACHABLE; -} - -LJLIB_PUSH(lastcl) -LJLIB_ASM(ipairs) LJLIB_REC(xpairs 1) -{ - return ffh_pairs(L, MM_ipairs); -} - -/* -- Base library: getters and setters ----------------------------------- */ - -LJLIB_ASM_(getmetatable) LJLIB_REC(.) -/* Recycle the lj_lib_checkany(L, 1) from assert. */ - -LJLIB_ASM(setmetatable) LJLIB_REC(.) -{ - GCtab *t = lj_lib_checktab(L, 1); - GCtab *mt = lj_lib_checktabornil(L, 2); - if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable))) - lj_err_caller(L, LJ_ERR_PROTMT); - setgcref(t->metatable, obj2gco(mt)); - if (mt) { lj_gc_objbarriert(L, t, mt); } - settabV(L, L->base-1-LJ_FR2, t); - return FFH_RES(1); -} - -LJLIB_CF(getfenv) LJLIB_REC(.) -{ - GCfunc *fn; - cTValue *o = L->base; - if (!(o < L->top && tvisfunc(o))) { - int level = lj_lib_optint(L, 1, 1); - o = lj_debug_frame(L, level, &level); - if (o == NULL) - lj_err_arg(L, 1, LJ_ERR_INVLVL); - if (LJ_FR2) o--; - } - fn = &gcval(o)->fn; - settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env)); - return 1; -} - -LJLIB_CF(setfenv) -{ - GCfunc *fn; - GCtab *t = lj_lib_checktab(L, 2); - cTValue *o = L->base; - if (!(o < L->top && tvisfunc(o))) { - int level = lj_lib_checkint(L, 1); - if (level == 0) { - /* NOBARRIER: A thread (i.e. L) is never black. */ - setgcref(L->env, obj2gco(t)); - return 0; - } - o = lj_debug_frame(L, level, &level); - if (o == NULL) - lj_err_arg(L, 1, LJ_ERR_INVLVL); - if (LJ_FR2) o--; - } - fn = &gcval(o)->fn; - if (!isluafunc(fn)) - lj_err_caller(L, LJ_ERR_SETFENV); - setgcref(fn->l.env, obj2gco(t)); - lj_gc_objbarrier(L, obj2gco(fn), t); - setfuncV(L, L->top++, fn); - return 1; -} - -LJLIB_ASM(rawget) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_lib_checkany(L, 2); - return FFH_UNREACHABLE; -} - -LJLIB_CF(rawset) LJLIB_REC(.) -{ - lj_lib_checktab(L, 1); - lj_lib_checkany(L, 2); - L->top = 1+lj_lib_checkany(L, 3); - lua_rawset(L, 1); - return 1; -} - -LJLIB_CF(rawequal) LJLIB_REC(.) -{ - cTValue *o1 = lj_lib_checkany(L, 1); - cTValue *o2 = lj_lib_checkany(L, 2); - setboolV(L->top-1, lj_obj_equal(o1, o2)); - return 1; -} - -#if LJ_52 -LJLIB_CF(rawlen) LJLIB_REC(.) -{ - cTValue *o = L->base; - int32_t len; - if (L->top > o && tvisstr(o)) - len = (int32_t)strV(o)->len; - else - len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1)); - setintV(L->top-1, len); - return 1; -} -#endif - -LJLIB_CF(unpack) -{ - GCtab *t = lj_lib_checktab(L, 1); - int32_t n, i = lj_lib_optint(L, 2, 1); - int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ? - lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t); - uint32_t nu; - if (i > e) return 0; - nu = (uint32_t)e - (uint32_t)i; - n = (int32_t)(nu+1); - if (nu >= LUAI_MAXCSTACK || !lua_checkstack(L, n)) - lj_err_caller(L, LJ_ERR_UNPACK); - do { - cTValue *tv = lj_tab_getint(t, i); - if (tv) { - copyTV(L, L->top++, tv); - } else { - setnilV(L->top++); - } - } while (i++ < e); - return n; -} - -LJLIB_CF(select) LJLIB_REC(.) -{ - int32_t n = (int32_t)(L->top - L->base); - if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') { - setintV(L->top-1, n-1); - return 1; - } else { - int32_t i = lj_lib_checkint(L, 1); - if (i < 0) i = n + i; else if (i > n) i = n; - if (i < 1) - lj_err_arg(L, 1, LJ_ERR_IDXRNG); - return n - i; - } -} - -/* -- Base library: conversions ------------------------------------------- */ - -LJLIB_ASM(tonumber) LJLIB_REC(.) -{ - int32_t base = lj_lib_optint(L, 2, 10); - if (base == 10) { - TValue *o = lj_lib_checkany(L, 1); - if (lj_strscan_numberobj(o)) { - copyTV(L, L->base-1-LJ_FR2, o); - return FFH_RES(1); - } -#if LJ_HASFFI - if (tviscdata(o)) { - CTState *cts = ctype_cts(L); - CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) { - if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) && - ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) { - int32_t i; - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0); - setintV(L->base-1-LJ_FR2, i); - return FFH_RES(1); - } - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE), - (uint8_t *)&(L->base-1-LJ_FR2)->n, o, 0); - return FFH_RES(1); - } - } -#endif - } else { - const char *p = strdata(lj_lib_checkstr(L, 1)); - char *ep; - unsigned int neg = 0; - unsigned long ul; - if (base < 2 || base > 36) - lj_err_arg(L, 2, LJ_ERR_BASERNG); - while (lj_char_isspace((unsigned char)(*p))) p++; - if (*p == '-') { p++; neg = 1; } else if (*p == '+') { p++; } - if (lj_char_isalnum((unsigned char)(*p))) { - ul = strtoul(p, &ep, base); - if (p != ep) { - while (lj_char_isspace((unsigned char)(*ep))) ep++; - if (*ep == '\0') { - if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u+neg)) { - if (neg) ul = (unsigned long)-(long)ul; - setintV(L->base-1-LJ_FR2, (int32_t)ul); - } else { - lua_Number n = (lua_Number)ul; - if (neg) n = -n; - setnumV(L->base-1-LJ_FR2, n); - } - return FFH_RES(1); - } - } - } - } - setnilV(L->base-1-LJ_FR2); - return FFH_RES(1); -} - -LJLIB_ASM(tostring) LJLIB_REC(.) -{ - TValue *o = lj_lib_checkany(L, 1); - cTValue *mo; - L->top = o+1; /* Only keep one argument. */ - if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { - copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */ - return FFH_TAILCALL; - } - lj_gc_check(L); - setstrV(L, L->base-1-LJ_FR2, lj_strfmt_obj(L, L->base)); - return FFH_RES(1); -} - -/* -- Base library: throw and catch errors -------------------------------- */ - -LJLIB_CF(error) -{ - int32_t level = lj_lib_optint(L, 2, 1); - lua_settop(L, 1); - if (lua_isstring(L, 1) && level > 0) { - luaL_where(L, level); - lua_pushvalue(L, 1); - lua_concat(L, 2); - } - return lua_error(L); -} - -LJLIB_ASM(pcall) LJLIB_REC(.) -{ - lj_lib_checkany(L, 1); - lj_lib_checkfunc(L, 2); /* For xpcall only. */ - return FFH_UNREACHABLE; -} -LJLIB_ASM_(xpcall) LJLIB_REC(.) - -/* -- Base library: load Lua code ----------------------------------------- */ - -static int load_aux(lua_State *L, int status, int envarg) -{ - if (status == LUA_OK) { - if (tvistab(L->base+envarg-1)) { - GCfunc *fn = funcV(L->top-1); - GCtab *t = tabV(L->base+envarg-1); - setgcref(fn->c.env, obj2gco(t)); - lj_gc_objbarrier(L, fn, t); - } - return 1; - } else { - setnilV(L->top-2); - return 2; - } -} - -LJLIB_CF(loadfile) -{ - GCstr *fname = lj_lib_optstr(L, 1); - GCstr *mode = lj_lib_optstr(L, 2); - int status; - lua_settop(L, 3); /* Ensure env arg exists. */ - status = luaL_loadfilex(L, fname ? strdata(fname) : NULL, - mode ? strdata(mode) : NULL); - return load_aux(L, status, 3); -} - -static const char *reader_func(lua_State *L, void *ud, size_t *size) -{ - UNUSED(ud); - luaL_checkstack(L, 2, "too many nested functions"); - copyTV(L, L->top++, L->base); - lua_call(L, 0, 1); /* Call user-supplied function. */ - L->top--; - if (tvisnil(L->top)) { - *size = 0; - return NULL; - } else if (tvisstr(L->top) || tvisnumber(L->top)) { - copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */ - return lua_tolstring(L, 5, size); - } else { - lj_err_caller(L, LJ_ERR_RDRSTR); - return NULL; - } -} - -LJLIB_CF(load) -{ - GCstr *name = lj_lib_optstr(L, 2); - GCstr *mode = lj_lib_optstr(L, 3); - int status; - if (L->base < L->top && - (tvisstr(L->base) || tvisnumber(L->base) || tvisbuf(L->base))) { - const char *s; - MSize len; - if (tvisbuf(L->base)) { - SBufExt *sbx = bufV(L->base); - s = sbx->r; - len = sbufxlen(sbx); - if (!name) name = &G(L)->strempty; /* Buffers are not NUL-terminated. */ - } else { - GCstr *str = lj_lib_checkstr(L, 1); - s = strdata(str); - len = str->len; - } - lua_settop(L, 4); /* Ensure env arg exists. */ - status = luaL_loadbufferx(L, s, len, name ? strdata(name) : s, - mode ? strdata(mode) : NULL); - } else { - lj_lib_checkfunc(L, 1); - lua_settop(L, 5); /* Reserve a slot for the string from the reader. */ - status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)", - mode ? strdata(mode) : NULL); - } - return load_aux(L, status, 4); -} - -LJLIB_CF(loadstring) -{ - return lj_cf_load(L); -} - -LJLIB_CF(dofile) -{ - GCstr *fname = lj_lib_optstr(L, 1); - setnilV(L->top); - L->top = L->base+1; - if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != LUA_OK) - lua_error(L); - lua_call(L, 0, LUA_MULTRET); - return (int)(L->top - L->base) - 1; -} - -/* -- Base library: GC control -------------------------------------------- */ - -LJLIB_CF(gcinfo) -{ - setintV(L->top++, (int32_t)(G(L)->gc.total >> 10)); - return 1; -} - -LJLIB_CF(collectgarbage) -{ - int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */ - "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul\1\377\11isrunning"); - int32_t data = lj_lib_optint(L, 2, 0); - if (opt == LUA_GCCOUNT) { - setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0); - } else { - int res = lua_gc(L, opt, data); - if (opt == LUA_GCSTEP || opt == LUA_GCISRUNNING) - setboolV(L->top, res); - else - setintV(L->top, res); - } - L->top++; - return 1; -} - -/* -- Base library: miscellaneous functions ------------------------------- */ - -LJLIB_PUSH(top-2) /* Upvalue holds weak table. */ -LJLIB_CF(newproxy) -{ - lua_settop(L, 1); - lua_newuserdata(L, 0); - if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */ - return 1; - } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */ - lua_newtable(L); - lua_pushvalue(L, -1); - lua_pushboolean(L, 1); - lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */ - } else { /* newproxy(proxy): inherit metatable. */ - int validproxy = 0; - if (lua_getmetatable(L, 1)) { - lua_rawget(L, lua_upvalueindex(1)); - validproxy = lua_toboolean(L, -1); - lua_pop(L, 1); - } - if (!validproxy) - lj_err_arg(L, 1, LJ_ERR_NOPROXY); - lua_getmetatable(L, 1); - } - lua_setmetatable(L, 2); - return 1; -} - -LJLIB_PUSH("tostring") -LJLIB_CF(print) -{ - ptrdiff_t i, nargs = L->top - L->base; - cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1))); - int shortcut; - if (tv && !tvisnil(tv)) { - copyTV(L, L->top++, tv); - } else { - setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1))); - lua_gettable(L, LUA_GLOBALSINDEX); - tv = L->top-1; - } - shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring) && - !gcrefu(basemt_it(G(L), LJ_TNUMX)); - for (i = 0; i < nargs; i++) { - cTValue *o = &L->base[i]; - const char *str; - size_t size; - MSize len; - if (shortcut && (str = lj_strfmt_wstrnum(L, o, &len)) != NULL) { - size = len; - } else { - copyTV(L, L->top+1, o); - copyTV(L, L->top, L->top-1); - L->top += 2; - lua_call(L, 1, 1); - str = lua_tolstring(L, -1, &size); - if (!str) - lj_err_caller(L, LJ_ERR_PRTOSTR); - L->top--; - } - if (i) - putchar('\t'); - fwrite(str, 1, size, stdout); - } - putchar('\n'); - return 0; -} - -LJLIB_PUSH(top-3) -LJLIB_SET(_VERSION) - -#include "lj_libdef.h" - -/* -- Coroutine library --------------------------------------------------- */ - -#define LJLIB_MODULE_coroutine - -LJLIB_CF(coroutine_status) -{ - const char *s; - lua_State *co; - if (!(L->top > L->base && tvisthread(L->base))) - lj_err_arg(L, 1, LJ_ERR_NOCORO); - co = threadV(L->base); - if (co == L) s = "running"; - else if (co->status == LUA_YIELD) s = "suspended"; - else if (co->status != LUA_OK) s = "dead"; - else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal"; - else if (co->top == co->base) s = "dead"; - else s = "suspended"; - lua_pushstring(L, s); - return 1; -} - -LJLIB_CF(coroutine_running) -{ -#if LJ_52 - int ismain = lua_pushthread(L); - setboolV(L->top++, ismain); - return 2; -#else - if (lua_pushthread(L)) - setnilV(L->top++); - return 1; -#endif -} - -LJLIB_CF(coroutine_isyieldable) -{ - setboolV(L->top++, cframe_canyield(L->cframe)); - return 1; -} - -LJLIB_CF(coroutine_create) -{ - lua_State *L1; - if (!(L->base < L->top && tvisfunc(L->base))) - lj_err_argt(L, 1, LUA_TFUNCTION); - L1 = lua_newthread(L); - setfuncV(L, L1->top++, funcV(L->base)); - return 1; -} - -LJLIB_ASM(coroutine_yield) -{ - lj_err_caller(L, LJ_ERR_CYIELD); - return FFH_UNREACHABLE; -} - -static int ffh_resume(lua_State *L, lua_State *co, int wrap) -{ - if (co->cframe != NULL || co->status > LUA_YIELD || - (co->status == LUA_OK && co->top == co->base)) { - ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD; - if (wrap) lj_err_caller(L, em); - setboolV(L->base-1-LJ_FR2, 0); - setstrV(L, L->base-LJ_FR2, lj_err_str(L, em)); - return FFH_RES(2); - } - lj_state_growstack(co, (MSize)(L->top - L->base)); - return FFH_RETRY; -} - -LJLIB_ASM(coroutine_resume) -{ - if (!(L->top > L->base && tvisthread(L->base))) - lj_err_arg(L, 1, LJ_ERR_NOCORO); - return ffh_resume(L, threadV(L->base), 0); -} - -LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux) -{ - return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1); -} - -/* Inline declarations. */ -LJ_ASMF void lj_ff_coroutine_wrap_aux(void); -#if !(LJ_TARGET_MIPS && defined(ljamalg_c)) -LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, - lua_State *co); -#endif - -/* Error handler, called from assembler VM. */ -void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co) -{ - co->top--; copyTV(L, L->top, co->top); L->top++; - if (tvisstr(L->top-1)) - lj_err_callermsg(L, strVdata(L->top-1)); - else - lj_err_run(L); -} - -/* Forward declaration. */ -static void setpc_wrap_aux(lua_State *L, GCfunc *fn); - -LJLIB_CF(coroutine_wrap) -{ - GCfunc *fn; - lj_cf_coroutine_create(L); - fn = lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1); - setpc_wrap_aux(L, fn); - return 1; -} - -#include "lj_libdef.h" - -/* Fix the PC of wrap_aux. Really ugly workaround. */ -static void setpc_wrap_aux(lua_State *L, GCfunc *fn) -{ - setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]); -} - -/* ------------------------------------------------------------------------ */ - -static void newproxy_weaktable(lua_State *L) -{ - /* NOBARRIER: The table is new (marked white). */ - GCtab *t = lj_tab_new(L, 0, 1); - settabV(L, L->top++, t); - setgcref(t->metatable, obj2gco(t)); - setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")), - lj_str_newlit(L, "kv")); - t->nomm = (uint8_t)(~(1u<env); - settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env); - lua_pushliteral(L, LUA_VERSION); /* top-3. */ - newproxy_weaktable(L); /* top-2. */ - LJ_LIB_REG(L, "_G", base); - LJ_LIB_REG(L, LUA_COLIBNAME, coroutine); - return 2; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_bit.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_bit.c deleted file mode 100644 index 38c0f57..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_bit.c +++ /dev/null @@ -1,180 +0,0 @@ -/* -** Bit manipulation library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_bit_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_cconv.h" -#include "lj_carith.h" -#endif -#include "lj_ff.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_bit - -#if LJ_HASFFI -static int bit_result64(lua_State *L, CTypeID id, uint64_t x) -{ - GCcdata *cd = lj_cdata_new_(L, id, 8); - *(uint64_t *)cdataptr(cd) = x; - setcdataV(L, L->base-1-LJ_FR2, cd); - return FFH_RES(1); -} -#else -static int32_t bit_checkbit(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else { - int32_t i = lj_num2bit(numV(o)); - if (LJ_DUALNUM) setintV(o, i); - return i; - } -} -#endif - -LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit) -{ -#if LJ_HASFFI - CTypeID id = 0; - setintV(L->base-1-LJ_FR2, (int32_t)lj_carith_check64(L, 1, &id)); - return FFH_RES(1); -#else - lj_lib_checknumber(L, 1); - return FFH_RETRY; -#endif -} - -LJLIB_ASM(bit_bnot) LJLIB_REC(bit_unary IR_BNOT) -{ -#if LJ_HASFFI - CTypeID id = 0; - uint64_t x = lj_carith_check64(L, 1, &id); - return id ? bit_result64(L, id, ~x) : FFH_RETRY; -#else - lj_lib_checknumber(L, 1); - return FFH_RETRY; -#endif -} - -LJLIB_ASM(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP) -{ -#if LJ_HASFFI - CTypeID id = 0; - uint64_t x = lj_carith_check64(L, 1, &id); - return id ? bit_result64(L, id, lj_bswap64(x)) : FFH_RETRY; -#else - lj_lib_checknumber(L, 1); - return FFH_RETRY; -#endif -} - -LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) -{ -#if LJ_HASFFI - CTypeID id = 0, id2 = 0; - uint64_t x = lj_carith_check64(L, 1, &id); - int32_t sh = (int32_t)lj_carith_check64(L, 2, &id2); - if (id) { - x = lj_carith_shift64(x, sh, curr_func(L)->c.ffid - (int)FF_bit_lshift); - return bit_result64(L, id, x); - } - if (id2) setintV(L->base+1, sh); - return FFH_RETRY; -#else - lj_lib_checknumber(L, 1); - bit_checkbit(L, 2); - return FFH_RETRY; -#endif -} -LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR) -LJLIB_ASM_(bit_arshift) LJLIB_REC(bit_shift IR_BSAR) -LJLIB_ASM_(bit_rol) LJLIB_REC(bit_shift IR_BROL) -LJLIB_ASM_(bit_ror) LJLIB_REC(bit_shift IR_BROR) - -LJLIB_ASM(bit_band) LJLIB_REC(bit_nary IR_BAND) -{ -#if LJ_HASFFI - CTypeID id = 0; - TValue *o = L->base, *top = L->top; - int i = 0; - do { lj_carith_check64(L, ++i, &id); } while (++o < top); - if (id) { - CTState *cts = ctype_cts(L); - CType *ct = ctype_get(cts, id); - int op = curr_func(L)->c.ffid - (int)FF_bit_bor; - uint64_t x, y = op >= 0 ? 0 : ~(uint64_t)0; - o = L->base; - do { - lj_cconv_ct_tv(cts, ct, (uint8_t *)&x, o, 0); - if (op < 0) y &= x; else if (op == 0) y |= x; else y ^= x; - } while (++o < top); - return bit_result64(L, id, y); - } - return FFH_RETRY; -#else - int i = 0; - do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); - return FFH_RETRY; -#endif -} -LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR) -LJLIB_ASM_(bit_bxor) LJLIB_REC(bit_nary IR_BXOR) - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(bit_tohex) LJLIB_REC(.) -{ -#if LJ_HASFFI - CTypeID id = 0, id2 = 0; - uint64_t b = lj_carith_check64(L, 1, &id); - int32_t n = L->base+1>=L->top ? (id ? 16 : 8) : - (int32_t)lj_carith_check64(L, 2, &id2); -#else - uint32_t b = (uint32_t)bit_checkbit(L, 1); - int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2); -#endif - SBuf *sb = lj_buf_tmp_(L); - SFormat sf = (STRFMT_UINT|STRFMT_T_HEX); - if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; } - sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC); -#if LJ_HASFFI - if (n < 16) b &= ((uint64_t)1 << 4*n)-1; -#else - if (n < 8) b &= (1u << 4*n)-1; -#endif - sb = lj_strfmt_putfxint(sb, sf, b); - setstrV(L, L->top-1, lj_buf_str(L, sb)); - lj_gc_check(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_bit(lua_State *L) -{ - LJ_LIB_REG(L, LUA_BITLIBNAME, bit); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_buffer.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_buffer.c deleted file mode 100644 index d6ff134..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_buffer.c +++ /dev/null @@ -1,360 +0,0 @@ -/* -** Buffer library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_buffer_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" - -#if LJ_HASBUFFER -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_udata.h" -#include "lj_meta.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_cconv.h" -#endif -#include "lj_strfmt.h" -#include "lj_serialize.h" -#include "lj_lib.h" - -/* -- Helper functions ---------------------------------------------------- */ - -/* Check that the first argument is a string buffer. */ -static SBufExt *buffer_tobuf(lua_State *L) -{ - if (!(L->base < L->top && tvisbuf(L->base))) - lj_err_argtype(L, 1, "buffer"); - return bufV(L->base); -} - -/* Ditto, but for writers. */ -static LJ_AINLINE SBufExt *buffer_tobufw(lua_State *L) -{ - SBufExt *sbx = buffer_tobuf(L); - setsbufXL_(sbx, L); - return sbx; -} - -#define buffer_toudata(sbx) ((GCudata *)(sbx)-1) - -/* -- Buffer methods ------------------------------------------------------ */ - -#define LJLIB_MODULE_buffer_method - -LJLIB_CF(buffer_method_free) -{ - SBufExt *sbx = buffer_tobuf(L); - lj_bufx_free(L, sbx); - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_reset) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - lj_bufx_reset(sbx); - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_skip) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - MSize n = (MSize)lj_lib_checkintrange(L, 2, 0, LJ_MAX_BUF); - MSize len = sbufxlen(sbx); - if (n < len) { - sbx->r += n; - } else if (sbufiscow(sbx)) { - sbx->r = sbx->w; - } else { - sbx->r = sbx->w = sbx->b; - } - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_set) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - GCobj *ref; - const char *p; - MSize len; -#if LJ_HASFFI - if (tviscdata(L->base+1)) { - CTState *cts = ctype_cts(L); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, - L->base+1, CCF_ARG(2)); - len = (MSize)lj_lib_checkintrange(L, 3, 0, LJ_MAX_BUF); - } else -#endif - { - GCstr *str = lj_lib_checkstrx(L, 2); - p = strdata(str); - len = str->len; - } - lj_bufx_free(L, sbx); - lj_bufx_set_cow(L, sbx, p, len); - ref = gcV(L->base+1); - setgcref(sbx->cowref, ref); - lj_gc_objbarrier(L, buffer_toudata(sbx), ref); - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_put) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobufw(L); - ptrdiff_t arg, narg = L->top - L->base; - for (arg = 1; arg < narg; arg++) { - cTValue *o = &L->base[arg], *mo = NULL; - retry: - if (tvisstr(o)) { - lj_buf_putstr((SBuf *)sbx, strV(o)); - } else if (tvisint(o)) { - lj_strfmt_putint((SBuf *)sbx, intV(o)); - } else if (tvisnum(o)) { - lj_strfmt_putfnum((SBuf *)sbx, STRFMT_G14, numV(o)); - } else if (tvisbuf(o)) { - SBufExt *sbx2 = bufV(o); - if (sbx2 == sbx) lj_err_arg(L, (int)(arg+1), LJ_ERR_BUFFER_SELF); - lj_buf_putmem((SBuf *)sbx, sbx2->r, sbufxlen(sbx2)); - } else if (!mo && !tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { - /* Call __tostring metamethod inline. */ - copyTV(L, L->top++, mo); - copyTV(L, L->top++, o); - lua_call(L, 1, 1); - o = &L->base[arg]; /* The stack may have been reallocated. */ - copyTV(L, &L->base[arg], L->top-1); - L->top = L->base + narg; - goto retry; /* Retry with the result. */ - } else { - lj_err_argtype(L, (int)(arg+1), "string/number/__tostring"); - } - /* Probably not useful to inline other __tostring MMs, e.g. FFI numbers. */ - } - L->top = L->base+1; /* Chain buffer object. */ - lj_gc_check(L); - return 1; -} - -LJLIB_CF(buffer_method_putf) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobufw(L); - lj_strfmt_putarg(L, (SBuf *)sbx, 2, 2); - L->top = L->base+1; /* Chain buffer object. */ - lj_gc_check(L); - return 1; -} - -LJLIB_CF(buffer_method_get) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - ptrdiff_t arg, narg = L->top - L->base; - if (narg == 1) { - narg++; - setnilV(L->top++); /* get() is the same as get(nil). */ - } - for (arg = 1; arg < narg; arg++) { - TValue *o = &L->base[arg]; - MSize n = tvisnil(o) ? LJ_MAX_BUF : - (MSize) lj_lib_checkintrange(L, (int)(arg+1), 0, LJ_MAX_BUF); - MSize len = sbufxlen(sbx); - if (n > len) n = len; - setstrV(L, o, lj_str_new(L, sbx->r, n)); - sbx->r += n; - } - if (sbx->r == sbx->w && !sbufiscow(sbx)) sbx->r = sbx->w = sbx->b; - lj_gc_check(L); - return (int)(narg-1); -} - -#if LJ_HASFFI -LJLIB_CF(buffer_method_putcdata) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobufw(L); - const char *p; - MSize len; - if (tviscdata(L->base+1)) { - CTState *cts = ctype_cts(L); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, - L->base+1, CCF_ARG(2)); - } else { - lj_err_argtype(L, 2, "cdata"); - } - len = (MSize)lj_lib_checkintrange(L, 3, 0, LJ_MAX_BUF); - lj_buf_putmem((SBuf *)sbx, p, len); - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_reserve) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobufw(L); - MSize sz = (MSize)lj_lib_checkintrange(L, 2, 0, LJ_MAX_BUF); - GCcdata *cd; - lj_buf_more((SBuf *)sbx, sz); - ctype_loadffi(L); - cd = lj_cdata_new_(L, CTID_P_UINT8, CTSIZE_PTR); - *(void **)cdataptr(cd) = sbx->w; - setcdataV(L, L->top++, cd); - setintV(L->top++, sbufleft(sbx)); - return 2; -} - -LJLIB_CF(buffer_method_commit) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - MSize len = (MSize)lj_lib_checkintrange(L, 2, 0, LJ_MAX_BUF); - if (len > sbufleft(sbx)) lj_err_arg(L, 2, LJ_ERR_NUMRNG); - sbx->w += len; - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_ref) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - GCcdata *cd; - ctype_loadffi(L); - cd = lj_cdata_new_(L, CTID_P_UINT8, CTSIZE_PTR); - *(void **)cdataptr(cd) = sbx->r; - setcdataV(L, L->top++, cd); - setintV(L->top++, sbufxlen(sbx)); - return 2; -} -#endif - -LJLIB_CF(buffer_method_encode) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobufw(L); - cTValue *o = lj_lib_checkany(L, 2); - lj_serialize_put(sbx, o); - lj_gc_check(L); - L->top = L->base+1; /* Chain buffer object. */ - return 1; -} - -LJLIB_CF(buffer_method_decode) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobufw(L); - setnilV(L->top++); - sbx->r = lj_serialize_get(sbx, L->top-1); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(buffer_method___gc) -{ - SBufExt *sbx = buffer_tobuf(L); - lj_bufx_free(L, sbx); - return 0; -} - -LJLIB_CF(buffer_method___tostring) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - setstrV(L, L->top-1, lj_str_new(L, sbx->r, sbufxlen(sbx))); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(buffer_method___len) LJLIB_REC(.) -{ - SBufExt *sbx = buffer_tobuf(L); - setintV(L->top-1, (int32_t)sbufxlen(sbx)); - return 1; -} - -LJLIB_PUSH("buffer") LJLIB_SET(__metatable) -LJLIB_PUSH(top-1) LJLIB_SET(__index) - -/* -- Buffer library functions -------------------------------------------- */ - -#define LJLIB_MODULE_buffer - -LJLIB_PUSH(top-2) LJLIB_SET(!) /* Set environment. */ - -LJLIB_CF(buffer_new) -{ - MSize sz = 0; - int targ = 1; - GCtab *env, *dict_str = NULL, *dict_mt = NULL; - GCudata *ud; - SBufExt *sbx; - if (L->base < L->top && !tvistab(L->base)) { - targ = 2; - if (!tvisnil(L->base)) - sz = (MSize)lj_lib_checkintrange(L, 1, 0, LJ_MAX_BUF); - } - if (L->base+targ-1 < L->top) { - GCtab *options = lj_lib_checktab(L, targ); - cTValue *opt_dict, *opt_mt; - opt_dict = lj_tab_getstr(options, lj_str_newlit(L, "dict")); - if (opt_dict && tvistab(opt_dict)) { - dict_str = tabV(opt_dict); - lj_serialize_dict_prep_str(L, dict_str); - } - opt_mt = lj_tab_getstr(options, lj_str_newlit(L, "metatable")); - if (opt_mt && tvistab(opt_mt)) { - dict_mt = tabV(opt_mt); - lj_serialize_dict_prep_mt(L, dict_mt); - } - } - env = tabref(curr_func(L)->c.env); - ud = lj_udata_new(L, sizeof(SBufExt), env); - ud->udtype = UDTYPE_BUFFER; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcref(ud->metatable, obj2gco(env)); - setudataV(L, L->top++, ud); - sbx = (SBufExt *)uddata(ud); - lj_bufx_init(L, sbx); - setgcref(sbx->dict_str, obj2gco(dict_str)); - setgcref(sbx->dict_mt, obj2gco(dict_mt)); - if (sz > 0) lj_buf_need2((SBuf *)sbx, sz); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(buffer_encode) LJLIB_REC(.) -{ - cTValue *o = lj_lib_checkany(L, 1); - setstrV(L, L->top++, lj_serialize_encode(L, o)); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(buffer_decode) LJLIB_REC(.) -{ - GCstr *str = lj_lib_checkstrx(L, 1); - setnilV(L->top++); - lj_serialize_decode(L, L->top-1, str); - lj_gc_check(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -int luaopen_string_buffer(lua_State *L) -{ - LJ_LIB_REG(L, NULL, buffer_method); - lua_getfield(L, -1, "__tostring"); - lua_setfield(L, -2, "tostring"); - LJ_LIB_REG(L, NULL, buffer); - return 1; -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_debug.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_debug.c deleted file mode 100644 index 3af7a35..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_debug.c +++ /dev/null @@ -1,406 +0,0 @@ -/* -** Debug library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_debug_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_debug - -LJLIB_CF(debug_getregistry) -{ - copyTV(L, L->top++, registry(L)); - return 1; -} - -LJLIB_CF(debug_getmetatable) LJLIB_REC(.) -{ - lj_lib_checkany(L, 1); - if (!lua_getmetatable(L, 1)) { - setnilV(L->top-1); - } - return 1; -} - -LJLIB_CF(debug_setmetatable) -{ - lj_lib_checktabornil(L, 2); - L->top = L->base+2; - lua_setmetatable(L, 1); -#if !LJ_52 - setboolV(L->top-1, 1); -#endif - return 1; -} - -LJLIB_CF(debug_getfenv) -{ - lj_lib_checkany(L, 1); - lua_getfenv(L, 1); - return 1; -} - -LJLIB_CF(debug_setfenv) -{ - lj_lib_checktab(L, 2); - L->top = L->base+2; - if (!lua_setfenv(L, 1)) - lj_err_caller(L, LJ_ERR_SETFENV); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void settabss(lua_State *L, const char *i, const char *v) -{ - lua_pushstring(L, v); - lua_setfield(L, -2, i); -} - -static void settabsi(lua_State *L, const char *i, int v) -{ - lua_pushinteger(L, v); - lua_setfield(L, -2, i); -} - -static void settabsb(lua_State *L, const char *i, int v) -{ - lua_pushboolean(L, v); - lua_setfield(L, -2, i); -} - -static lua_State *getthread(lua_State *L, int *arg) -{ - if (L->base < L->top && tvisthread(L->base)) { - *arg = 1; - return threadV(L->base); - } else { - *arg = 0; - return L; - } -} - -static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) -{ - if (L == L1) { - lua_pushvalue(L, -2); - lua_remove(L, -3); - } - else - lua_xmove(L1, L, 1); - lua_setfield(L, -2, fname); -} - -LJLIB_CF(debug_getinfo) -{ - lj_Debug ar; - int arg, opt_f = 0, opt_L = 0; - lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnSu"); - if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), (lua_Debug *)&ar)) { - setnilV(L->top-1); - return 1; - } - } else if (L->base+arg < L->top && tvisfunc(L->base+arg)) { - options = lua_pushfstring(L, ">%s", options); - setfuncV(L1, L1->top++, funcV(L->base+arg)); - } else { - lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL); - } - if (!lj_debug_getinfo(L1, options, &ar, 1)) - lj_err_arg(L, arg+2, LJ_ERR_INVOPT); - lua_createtable(L, 0, 16); /* Create result table. */ - for (; *options; options++) { - switch (*options) { - case 'S': - settabss(L, "source", ar.source); - settabss(L, "short_src", ar.short_src); - settabsi(L, "linedefined", ar.linedefined); - settabsi(L, "lastlinedefined", ar.lastlinedefined); - settabss(L, "what", ar.what); - break; - case 'l': - settabsi(L, "currentline", ar.currentline); - break; - case 'u': - settabsi(L, "nups", ar.nups); - settabsi(L, "nparams", ar.nparams); - settabsb(L, "isvararg", ar.isvararg); - break; - case 'n': - settabss(L, "name", ar.name); - settabss(L, "namewhat", ar.namewhat); - break; - case 'f': opt_f = 1; break; - case 'L': opt_L = 1; break; - default: break; - } - } - if (opt_L) treatstackoption(L, L1, "activelines"); - if (opt_f) treatstackoption(L, L1, "func"); - return 1; /* Return result table. */ -} - -LJLIB_CF(debug_getlocal) -{ - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; - int slot = lj_lib_checkint(L, arg+2); - if (tvisfunc(L->base+arg)) { - L->top = L->base+arg+1; - lua_pushstring(L, lua_getlocal(L, NULL, slot)); - return 1; - } - if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) - lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); - name = lua_getlocal(L1, &ar, slot); - if (name) { - lua_xmove(L1, L, 1); - lua_pushstring(L, name); - lua_pushvalue(L, -2); - return 2; - } else { - setnilV(L->top-1); - return 1; - } -} - -LJLIB_CF(debug_setlocal) -{ - int arg; - lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - TValue *tv; - if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) - lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); - tv = lj_lib_checkany(L, arg+3); - copyTV(L1, L1->top++, tv); - lua_pushstring(L, lua_setlocal(L1, &ar, lj_lib_checkint(L, arg+2))); - return 1; -} - -static int debug_getupvalue(lua_State *L, int get) -{ - int32_t n = lj_lib_checkint(L, 2); - const char *name; - lj_lib_checkfunc(L, 1); - name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); - if (name) { - lua_pushstring(L, name); - if (!get) return 1; - copyTV(L, L->top, L->top-2); - L->top++; - return 2; - } - return 0; -} - -LJLIB_CF(debug_getupvalue) -{ - return debug_getupvalue(L, 1); -} - -LJLIB_CF(debug_setupvalue) -{ - lj_lib_checkany(L, 3); - return debug_getupvalue(L, 0); -} - -LJLIB_CF(debug_upvalueid) -{ - GCfunc *fn = lj_lib_checkfunc(L, 1); - int32_t n = lj_lib_checkint(L, 2) - 1; - if ((uint32_t)n >= fn->l.nupvalues) - lj_err_arg(L, 2, LJ_ERR_IDXRNG); - lua_pushlightuserdata(L, isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : - (void *)&fn->c.upvalue[n]); - return 1; -} - -LJLIB_CF(debug_upvaluejoin) -{ - GCfunc *fn[2]; - GCRef *p[2]; - int i; - for (i = 0; i < 2; i++) { - int32_t n; - fn[i] = lj_lib_checkfunc(L, 2*i+1); - if (!isluafunc(fn[i])) - lj_err_arg(L, 2*i+1, LJ_ERR_NOLFUNC); - n = lj_lib_checkint(L, 2*i+2) - 1; - if ((uint32_t)n >= fn[i]->l.nupvalues) - lj_err_arg(L, 2*i+2, LJ_ERR_IDXRNG); - p[i] = &fn[i]->l.uvptr[n]; - } - setgcrefr(*p[0], *p[1]); - lj_gc_objbarrier(L, fn[0], gcref(*p[1])); - return 0; -} - -#if LJ_52 -LJLIB_CF(debug_getuservalue) -{ - TValue *o = L->base; - if (o < L->top && tvisudata(o)) - settabV(L, o, tabref(udataV(o)->env)); - else - setnilV(o); - L->top = o+1; - return 1; -} - -LJLIB_CF(debug_setuservalue) -{ - TValue *o = L->base; - if (!(o < L->top && tvisudata(o))) - lj_err_argt(L, 1, LUA_TUSERDATA); - if (!(o+1 < L->top && tvistab(o+1))) - lj_err_argt(L, 2, LUA_TTABLE); - L->top = o+2; - lua_setfenv(L, 1); - return 1; -} -#endif - -/* ------------------------------------------------------------------------ */ - -#define KEY_HOOK (U64x(80000000,00000000)|'h') - -static void hookf(lua_State *L, lua_Debug *ar) -{ - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail return"}; - (L->top++)->u64 = KEY_HOOK; - lua_rawget(L, LUA_REGISTRYINDEX); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); - if (ar->currentline >= 0) - lua_pushinteger(L, ar->currentline); - else lua_pushnil(L); - lua_call(L, 2, 0); - } -} - -static int makemask(const char *smask, int count) -{ - int mask = 0; - if (strchr(smask, 'c')) mask |= LUA_MASKCALL; - if (strchr(smask, 'r')) mask |= LUA_MASKRET; - if (strchr(smask, 'l')) mask |= LUA_MASKLINE; - if (count > 0) mask |= LUA_MASKCOUNT; - return mask; -} - -static char *unmakemask(int mask, char *smask) -{ - int i = 0; - if (mask & LUA_MASKCALL) smask[i++] = 'c'; - if (mask & LUA_MASKRET) smask[i++] = 'r'; - if (mask & LUA_MASKLINE) smask[i++] = 'l'; - smask[i] = '\0'; - return smask; -} - -LJLIB_CF(debug_sethook) -{ - int arg, mask, count; - lua_Hook func; - (void)getthread(L, &arg); - if (lua_isnoneornil(L, arg+1)) { - lua_settop(L, arg+1); - func = NULL; mask = 0; count = 0; /* turn off hooks */ - } else { - const char *smask = luaL_checkstring(L, arg+2); - luaL_checktype(L, arg+1, LUA_TFUNCTION); - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } - (L->top++)->u64 = KEY_HOOK; - lua_pushvalue(L, arg+1); - lua_rawset(L, LUA_REGISTRYINDEX); - lua_sethook(L, func, mask, count); - return 0; -} - -LJLIB_CF(debug_gethook) -{ - char buff[5]; - int mask = lua_gethookmask(L); - lua_Hook hook = lua_gethook(L); - if (hook != NULL && hook != hookf) { /* external hook? */ - lua_pushliteral(L, "external hook"); - } else { - (L->top++)->u64 = KEY_HOOK; - lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */ - } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L)); - return 3; -} - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(debug_debug) -{ - for (;;) { - char buffer[250]; - fputs("lua_debug> ", stderr); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || - strcmp(buffer, "cont\n") == 0) - return 0; - if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || - lua_pcall(L, 0, 0, 0)) { - const char *s = lua_tostring(L, -1); - fputs(s ? s : "(error object is not a string)", stderr); - fputs("\n", stderr); - } - lua_settop(L, 0); /* remove eventual returns */ - } -} - -/* ------------------------------------------------------------------------ */ - -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ - -LJLIB_CF(debug_traceback) -{ - int arg; - lua_State *L1 = getthread(L, &arg); - const char *msg = lua_tostring(L, arg+1); - if (msg == NULL && L->top > L->base+arg) - L->top = L->base+arg+1; - else - luaL_traceback(L, L1, msg, lj_lib_optint(L, arg+2, (L == L1))); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_debug(lua_State *L) -{ - LJ_LIB_REG(L, LUA_DBLIBNAME, debug); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_ffi.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_ffi.c deleted file mode 100644 index 2295cf1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_ffi.c +++ /dev/null @@ -1,870 +0,0 @@ -/* -** FFI library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_ffi_c -#define LUA_LIB - -#include - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_ctype.h" -#include "lj_cparse.h" -#include "lj_cdata.h" -#include "lj_cconv.h" -#include "lj_carith.h" -#include "lj_ccall.h" -#include "lj_ccallback.h" -#include "lj_clib.h" -#include "lj_strfmt.h" -#include "lj_ff.h" -#include "lj_lib.h" - -/* -- C type checks ------------------------------------------------------- */ - -/* Check first argument for a C type and returns its ID. */ -static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param) -{ - TValue *o = L->base; - if (!(o < L->top)) { - err_argtype: - lj_err_argtype(L, 1, "C type"); - } - if (tvisstr(o)) { /* Parse an abstract C type declaration. */ - GCstr *s = strV(o); - CPState cp; - int errcode; - cp.L = L; - cp.cts = cts; - cp.srcname = strdata(s); - cp.p = strdata(s); - cp.param = param; - cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; - errcode = lj_cparse(&cp); - if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ - return cp.val.id; - } else { - GCcdata *cd; - if (!tviscdata(o)) goto err_argtype; - if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM); - cd = cdataV(o); - return cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->ctypeid; - } -} - -/* Check argument for C data and return it. */ -static GCcdata *ffi_checkcdata(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tviscdata(o))) - lj_err_argt(L, narg, LUA_TCDATA); - return cdataV(o); -} - -/* Convert argument to C pointer. */ -static void *ffi_checkptr(lua_State *L, int narg, CTypeID id) -{ - CTState *cts = ctype_cts(L); - TValue *o = L->base + narg-1; - void *p; - if (o >= L->top) - lj_err_arg(L, narg, LJ_ERR_NOVAL); - lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg)); - return p; -} - -/* Convert argument to int32_t. */ -static int32_t ffi_checkint(lua_State *L, int narg) -{ - CTState *cts = ctype_cts(L); - TValue *o = L->base + narg-1; - int32_t i; - if (o >= L->top) - lj_err_arg(L, narg, LJ_ERR_NOVAL); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, - CCF_ARG(narg)); - return i; -} - -/* -- C type metamethods -------------------------------------------------- */ - -#define LJLIB_MODULE_ffi_meta - -/* Handle ctype __index/__newindex metamethods. */ -static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm) -{ - CTypeID id = ctype_typeid(cts, ct); - cTValue *tv = lj_ctype_meta(cts, id, mm); - TValue *base = L->base; - if (!tv) { - const char *s; - err_index: - s = strdata(lj_ctype_repr(L, id, NULL)); - if (tvisstr(L->base+1)) { - lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1)); - } else { - const char *key = tviscdata(L->base+1) ? - strdata(lj_ctype_repr(L, cdataV(L->base+1)->ctypeid, NULL)) : - lj_typename(L->base+1); - lj_err_callerv(L, LJ_ERR_FFI_BADIDXW, s, key); - } - } - if (!tvisfunc(tv)) { - if (mm == MM_index) { - cTValue *o = lj_meta_tget(L, tv, base+1); - if (o) { - if (tvisnil(o)) goto err_index; - copyTV(L, L->top-1, o); - return 1; - } - } else { - TValue *o = lj_meta_tset(L, tv, base+1); - if (o) { - copyTV(L, o, base+2); - return 0; - } - } - copyTV(L, base, L->top); - tv = L->top-1-LJ_FR2; - } - return lj_meta_tailcall(L, tv); -} - -LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0) -{ - CTState *cts = ctype_cts(L); - CTInfo qual = 0; - CType *ct; - uint8_t *p; - TValue *o = L->base; - if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */ - lj_err_argt(L, 1, LUA_TCDATA); - ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual); - if ((qual & 1)) - return ffi_index_meta(L, cts, ct, MM_index); - if (lj_cdata_get(cts, ct, L->top-1, p)) - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1) -{ - CTState *cts = ctype_cts(L); - CTInfo qual = 0; - CType *ct; - uint8_t *p; - TValue *o = L->base; - if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */ - lj_err_argt(L, 1, LUA_TCDATA); - ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual); - if ((qual & 1)) { - if ((qual & CTF_CONST)) - lj_err_caller(L, LJ_ERR_FFI_WRCONST); - return ffi_index_meta(L, cts, ct, MM_newindex); - } - lj_cdata_set(cts, ct, p, o+2, qual); - return 0; -} - -/* Common handler for cdata arithmetic. */ -static int ffi_arith(lua_State *L) -{ - MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq); - return lj_carith_op(L, mm); -} - -/* The following functions must be in contiguous ORDER MM. */ -LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat) -{ - return ffi_arith(L); -} - -/* Forward declaration. */ -static int lj_cf_ffi_new(lua_State *L); - -LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call) -{ - CTState *cts = ctype_cts(L); - GCcdata *cd = ffi_checkcdata(L, 1); - CTypeID id = cd->ctypeid; - CType *ct; - cTValue *tv; - MMS mm = MM_call; - if (cd->ctypeid == CTID_CTYPEID) { - id = *(CTypeID *)cdataptr(cd); - mm = MM_new; - } else { - int ret = lj_ccall_func(L, cd); - if (ret >= 0) - return ret; - } - /* Handle ctype __call/__new metamethod. */ - ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - if (tv) - return lj_meta_tailcall(L, tv); - else if (mm == MM_call) - lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL))); - return lj_cf_ffi_new(L); -} - -LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow) -{ - return ffi_arith(L); -} - -LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm) -{ - return ffi_arith(L); -} -/* End of contiguous ORDER MM. */ - -LJLIB_CF(ffi_meta___tostring) -{ - GCcdata *cd = ffi_checkcdata(L, 1); - const char *msg = "cdata<%s>: %p"; - CTypeID id = cd->ctypeid; - void *p = cdataptr(cd); - if (id == CTID_CTYPEID) { - msg = "ctype<%s>"; - id = *(CTypeID *)p; - } else { - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, id); - if (ctype_isref(ct->info)) { - p = *(void **)p; - ct = ctype_rawchild(cts, ct); - } - if (ctype_iscomplex(ct->info)) { - setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size)); - goto checkgc; - } else if (ct->size == 8 && ctype_isinteger(ct->info)) { - setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd), - (ct->info & CTF_UNSIGNED))); - goto checkgc; - } else if (ctype_isfunc(ct->info)) { - p = *(void **)p; - } else if (ctype_isenum(ct->info)) { - msg = "cdata<%s>: %d"; - p = (void *)(uintptr_t)*(uint32_t **)p; - } else { - if (ctype_isptr(ct->info)) { - p = cdata_getptr(p, ct->size); - ct = ctype_rawchild(cts, ct); - } - if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) { - /* Handle ctype __tostring metamethod. */ - cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring); - if (tv) - return lj_meta_tailcall(L, tv); - } - } - } - lj_strfmt_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p); -checkgc: - lj_gc_check(L); - return 1; -} - -static int ffi_pairs(lua_State *L, MMS mm) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkcdata(L, 1)->ctypeid; - CType *ct = ctype_raw(cts, id); - cTValue *tv; - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - if (!tv) - lj_err_callerv(L, LJ_ERR_FFI_BADMM, strdata(lj_ctype_repr(L, id, NULL)), - strdata(mmname_str(G(L), mm))); - return lj_meta_tailcall(L, tv); -} - -LJLIB_CF(ffi_meta___pairs) -{ - return ffi_pairs(L, MM_pairs); -} - -LJLIB_CF(ffi_meta___ipairs) -{ - return ffi_pairs(L, MM_ipairs); -} - -LJLIB_PUSH("ffi") LJLIB_SET(__metatable) - -#include "lj_libdef.h" - -/* -- C library metamethods ----------------------------------------------- */ - -#define LJLIB_MODULE_ffi_clib - -/* Index C library by a name. */ -static TValue *ffi_clib_index(lua_State *L) -{ - TValue *o = L->base; - CLibrary *cl; - if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)) - lj_err_argt(L, 1, LUA_TUSERDATA); - cl = (CLibrary *)uddata(udataV(o)); - if (!(o+1 < L->top && tvisstr(o+1))) - lj_err_argt(L, 2, LUA_TSTRING); - return lj_clib_index(L, cl, strV(o+1)); -} - -LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1) -{ - TValue *tv = ffi_clib_index(L); - if (tviscdata(tv)) { - CTState *cts = ctype_cts(L); - GCcdata *cd = cdataV(tv); - CType *s = ctype_get(cts, cd->ctypeid); - if (ctype_isextern(s->info)) { - CTypeID sid = ctype_cid(s->info); - void *sp = *(void **)cdataptr(cd); - CType *ct = ctype_raw(cts, sid); - if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp)) - lj_gc_check(L); - return 1; - } - } - copyTV(L, L->top-1, tv); - return 1; -} - -LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0) -{ - TValue *tv = ffi_clib_index(L); - TValue *o = L->base+2; - if (o < L->top && tviscdata(tv)) { - CTState *cts = ctype_cts(L); - GCcdata *cd = cdataV(tv); - CType *d = ctype_get(cts, cd->ctypeid); - if (ctype_isextern(d->info)) { - CTInfo qual = 0; - for (;;) { /* Skip attributes and collect qualifiers. */ - d = ctype_child(cts, d); - if (!ctype_isattrib(d->info)) break; - if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size; - } - if (!((d->info|qual) & CTF_CONST)) { - lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0); - return 0; - } - } - } - lj_err_caller(L, LJ_ERR_FFI_WRCONST); - return 0; /* unreachable */ -} - -LJLIB_CF(ffi_clib___gc) -{ - TValue *o = L->base; - if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB) - lj_clib_unload((CLibrary *)uddata(udataV(o))); - return 0; -} - -#include "lj_libdef.h" - -/* -- Callback function metamethods --------------------------------------- */ - -#define LJLIB_MODULE_ffi_callback - -static int ffi_callback_set(lua_State *L, GCfunc *fn) -{ - GCcdata *cd = ffi_checkcdata(L, 1); - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, cd->ctypeid); - if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) { - MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd)); - if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) { - GCtab *t = cts->miscmap; - TValue *tv = lj_tab_setint(L, t, (int32_t)slot); - if (fn) { - setfuncV(L, tv, fn); - lj_gc_anybarriert(L, t); - } else { - setnilV(tv); - cts->cb.cbid[slot] = 0; - cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid; - } - return 0; - } - } - lj_err_caller(L, LJ_ERR_FFI_BADCBACK); - return 0; -} - -LJLIB_CF(ffi_callback_free) -{ - return ffi_callback_set(L, NULL); -} - -LJLIB_CF(ffi_callback_set) -{ - GCfunc *fn = lj_lib_checkfunc(L, 2); - return ffi_callback_set(L, fn); -} - -LJLIB_PUSH(top-1) LJLIB_SET(__index) - -#include "lj_libdef.h" - -/* -- FFI library functions ----------------------------------------------- */ - -#define LJLIB_MODULE_ffi - -LJLIB_CF(ffi_cdef) -{ - GCstr *s = lj_lib_checkstr(L, 1); - CPState cp; - int errcode; - cp.L = L; - cp.cts = ctype_cts(L); - cp.srcname = strdata(s); - cp.p = strdata(s); - cp.param = L->base+1; - cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT; - errcode = lj_cparse(&cp); - if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ - lj_gc_check(L); - return 0; -} - -LJLIB_CF(ffi_new) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CType *ct = ctype_raw(cts, id); - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - TValue *o = L->base+1; - GCcdata *cd; - if ((info & CTF_VLA)) { - o++; - sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2)); - } - if (sz == CTSIZE_INVALID) - lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE); - cd = lj_cdata_newx(cts, id, sz, info); - setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */ - lj_cconv_ct_init(cts, ct, sz, cdataptr(cd), - o, (MSize)(L->top - o)); /* Initialize cdata. */ - if (ctype_isstruct(ct->info)) { - /* Handle ctype __gc metamethod. Use the fast lookup here. */ - cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); - if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) { - GCtab *t = cts->finalizer; - if (gcref(t->metatable)) { - /* Add to finalizer table, if still enabled. */ - copyTV(L, lj_tab_set(L, t, o-1), tv); - lj_gc_anybarriert(L, t); - cd->marked |= LJ_GC_CDATA_FIN; - } - } - } - L->top = o; /* Only return the cdata itself. */ - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CType *d = ctype_raw(cts, id); - TValue *o = lj_lib_checkany(L, 2); - L->top = o+1; /* Make sure this is the last item on the stack. */ - if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info))) - lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); - if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) { - GCcdata *cd = lj_cdata_new(cts, id, d->size); - lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST); - setcdataV(L, o, cd); - lj_gc_check(L); - } - return 1; -} - -LJLIB_CF(ffi_typeof) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, L->base+1); - GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4); - *(CTypeID *)cdataptr(cd) = id; - setcdataV(L, L->top-1, cd); - lj_gc_check(L); - return 1; -} - -/* Internal and unsupported API. */ -LJLIB_CF(ffi_typeinfo) -{ - CTState *cts = ctype_cts(L); - CTypeID id = (CTypeID)ffi_checkint(L, 1); - if (id > 0 && id < cts->top) { - CType *ct = ctype_get(cts, id); - GCtab *t; - lua_createtable(L, 0, 4); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "info")), (int32_t)ct->info); - if (ct->size != CTSIZE_INVALID) - setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "size")), (int32_t)ct->size); - if (ct->sib) - setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib); - if (gcref(ct->name)) { - GCstr *s = gco2str(gcref(ct->name)); - if (isdead(G(L), obj2gco(s))) flipwhite(obj2gco(s)); - setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s); - } - lj_gc_check(L); - return 1; - } - return 0; -} - -LJLIB_CF(ffi_istype) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - CTypeID id1 = ffi_checkctype(L, cts, NULL); - TValue *o = lj_lib_checkany(L, 2); - int b = 0; - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : - cd->ctypeid; - CType *ct1 = lj_ctype_rawref(cts, id1); - CType *ct2 = lj_ctype_rawref(cts, id2); - if (ct1 == ct2) { - b = 1; - } else if (ctype_type(ct1->info) == ctype_type(ct2->info) && - ct1->size == ct2->size) { - if (ctype_ispointer(ct1->info)) - b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL); - else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info)) - b = (((ct1->info ^ ct2->info) & ~(CTF_QUAL|CTF_LONG)) == 0); - } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) && - ct1 == ctype_rawchild(cts, ct2)) { - b = 1; - } - } - setboolV(L->top-1, b); - setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ - return 1; -} - -LJLIB_CF(ffi_sizeof) LJLIB_REC(ffi_xof FF_ffi_sizeof) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CTSize sz; - if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) { - sz = cdatavlen(cdataV(L->base)); - } else { - CType *ct = lj_ctype_rawref(cts, id); - if (ctype_isvltype(ct->info)) - sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2)); - else - sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; - if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) { - setnilV(L->top-1); - return 1; - } - } - setintV(L->top-1, (int32_t)sz); - return 1; -} - -LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - CTSize sz = 0; - CTInfo info = lj_ctype_info_raw(cts, id, &sz); - setintV(L->top-1, 1 << ctype_align(info)); - return 1; -} - -LJLIB_CF(ffi_offsetof) LJLIB_REC(ffi_xof FF_ffi_offsetof) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - GCstr *name = lj_lib_checkstr(L, 2); - CType *ct = lj_ctype_rawref(cts, id); - CTSize ofs; - if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) { - CType *fct = lj_ctype_getfield(cts, ct, name, &ofs); - if (fct) { - setintV(L->top-1, ofs); - if (ctype_isfield(fct->info)) { - return 1; - } else if (ctype_isbitfield(fct->info)) { - setintV(L->top++, ctype_bitpos(fct->info)); - setintV(L->top++, ctype_bitbsz(fct->info)); - return 3; - } - } - } - return 0; -} - -LJLIB_CF(ffi_errno) LJLIB_REC(.) -{ - int err = errno; - if (L->top > L->base) - errno = ffi_checkint(L, 1); - setintV(L->top++, err); - return 1; -} - -LJLIB_CF(ffi_string) LJLIB_REC(.) -{ - CTState *cts = ctype_cts(L); - TValue *o = lj_lib_checkany(L, 1); - const char *p; - size_t len; - if (o+1 < L->top && !tvisnil(o+1)) { - len = (size_t)ffi_checkint(L, 2); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o, - CCF_ARG(1)); - } else { - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o, - CCF_ARG(1)); - len = strlen(p); - } - L->top = o+1; /* Make sure this is the last item on the stack. */ - setstrV(L, o, lj_str_new(L, p, len)); - lj_gc_check(L); - return 1; -} - -LJLIB_CF(ffi_copy) LJLIB_REC(.) -{ - void *dp = ffi_checkptr(L, 1, CTID_P_VOID); - void *sp = ffi_checkptr(L, 2, CTID_P_CVOID); - TValue *o = L->base+1; - CTSize len; - if (tvisstr(o) && o+1 >= L->top) - len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */ - else - len = (CTSize)ffi_checkint(L, 3); - memcpy(dp, sp, len); - return 0; -} - -LJLIB_CF(ffi_fill) LJLIB_REC(.) -{ - void *dp = ffi_checkptr(L, 1, CTID_P_VOID); - CTSize len = (CTSize)ffi_checkint(L, 2); - int32_t fill = 0; - if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3); - memset(dp, fill, len); - return 0; -} - -/* Test ABI string. */ -LJLIB_CF(ffi_abi) LJLIB_REC(.) -{ - GCstr *s = lj_lib_checkstr(L, 1); - int b = lj_cparse_case(s, -#if LJ_64 - "\00564bit" -#else - "\00532bit" -#endif -#if LJ_ARCH_HASFPU - "\003fpu" -#endif -#if LJ_ABI_SOFTFP - "\006softfp" -#else - "\006hardfp" -#endif -#if LJ_ABI_EABI - "\004eabi" -#endif -#if LJ_ABI_WIN - "\003win" -#endif -#if LJ_TARGET_UWP - "\003uwp" -#endif -#if LJ_LE - "\002le" -#else - "\002be" -#endif -#if LJ_GC64 - "\004gc64" -#endif - ) >= 0; - setboolV(L->top-1, b); - setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ - return 1; -} - -LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */ - -LJLIB_CF(ffi_metatype) -{ - CTState *cts = ctype_cts(L); - CTypeID id = ffi_checkctype(L, cts, NULL); - GCtab *mt = lj_lib_checktab(L, 2); - GCtab *t = cts->miscmap; - CType *ct = ctype_raw(cts, id); - TValue *tv; - GCcdata *cd; - if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) || - ctype_isvector(ct->info))) - lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); - tv = lj_tab_setinth(L, t, -(int32_t)id); - if (!tvisnil(tv)) - lj_err_caller(L, LJ_ERR_PROTMT); - settabV(L, tv, mt); - lj_gc_anybarriert(L, t); - cd = lj_cdata_new(cts, CTID_CTYPEID, 4); - *(CTypeID *)cdataptr(cd) = id; - setcdataV(L, L->top-1, cd); - lj_gc_check(L); - return 1; -} - -LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */ - -LJLIB_CF(ffi_gc) LJLIB_REC(.) -{ - GCcdata *cd = ffi_checkcdata(L, 1); - TValue *fin = lj_lib_checkany(L, 2); - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, cd->ctypeid); - if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) || - ctype_isrefarray(ct->info))) - lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); - lj_cdata_setfin(L, cd, gcval(fin), itype(fin)); - L->top = L->base+1; /* Pass through the cdata object. */ - return 1; -} - -LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */ - -LJLIB_CF(ffi_load) -{ - GCstr *name = lj_lib_checkstr(L, 1); - int global = (L->base+1 < L->top && tvistruecond(L->base+1)); - lj_clib_load(L, tabref(curr_func(L)->c.env), name, global); - return 1; -} - -LJLIB_PUSH(top-4) LJLIB_SET(C) -LJLIB_PUSH(top-3) LJLIB_SET(os) -LJLIB_PUSH(top-2) LJLIB_SET(arch) - -#include "lj_libdef.h" - -/* ------------------------------------------------------------------------ */ - -/* Create special weak-keyed finalizer table. */ -static GCtab *ffi_finalizer(lua_State *L) -{ - /* NOBARRIER: The table is new (marked white). */ - GCtab *t = lj_tab_new(L, 0, 1); - settabV(L, L->top++, t); - setgcref(t->metatable, obj2gco(t)); - setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")), - lj_str_newlit(L, "k")); - t->nomm = (uint8_t)(~(1u<top-1); - lj_gc_anybarriert(L, t); - } -} - -LUALIB_API int luaopen_ffi(lua_State *L) -{ - CTState *cts = lj_ctype_init(L); - settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1))); - cts->finalizer = ffi_finalizer(L); - LJ_LIB_REG(L, NULL, ffi_meta); - /* NOBARRIER: basemt is a GC root. */ - setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1))); - LJ_LIB_REG(L, NULL, ffi_clib); - LJ_LIB_REG(L, NULL, ffi_callback); - /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */ - settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1)); - L->top--; - lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */ - lua_pushliteral(L, LJ_OS_NAME); - lua_pushliteral(L, LJ_ARCH_NAME); - LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */ - ffi_register_module(L); - return 1; -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_init.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_init.c deleted file mode 100644 index 35e06fe..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_init.c +++ /dev/null @@ -1,55 +0,0 @@ -/* -** Library initialization. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major parts taken verbatim from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_init_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_arch.h" - -static const luaL_Reg lj_lib_load[] = { - { "", luaopen_base }, - { LUA_LOADLIBNAME, luaopen_package }, - { LUA_TABLIBNAME, luaopen_table }, - { LUA_IOLIBNAME, luaopen_io }, - { LUA_OSLIBNAME, luaopen_os }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_DBLIBNAME, luaopen_debug }, - { LUA_BITLIBNAME, luaopen_bit }, - { LUA_JITLIBNAME, luaopen_jit }, - { NULL, NULL } -}; - -static const luaL_Reg lj_lib_preload[] = { -#if LJ_HASFFI - { LUA_FFILIBNAME, luaopen_ffi }, -#endif - { NULL, NULL } -}; - -LUALIB_API void luaL_openlibs(lua_State *L) -{ - const luaL_Reg *lib; - for (lib = lj_lib_load; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); - } - luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", - sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1); - for (lib = lj_lib_preload; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); - } - lua_pop(L, 1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_io.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_io.c deleted file mode 100644 index c22faa2..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_io.c +++ /dev/null @@ -1,551 +0,0 @@ -/* -** I/O library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include - -#define lib_io_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_state.h" -#include "lj_strfmt.h" -#include "lj_ff.h" -#include "lj_lib.h" - -/* Userdata payload for I/O file. */ -typedef struct IOFileUD { - FILE *fp; /* File handle. */ - uint32_t type; /* File type. */ -} IOFileUD; - -#define IOFILE_TYPE_FILE 0 /* Regular file. */ -#define IOFILE_TYPE_PIPE 1 /* Pipe. */ -#define IOFILE_TYPE_STDF 2 /* Standard file handle. */ -#define IOFILE_TYPE_MASK 3 - -#define IOFILE_FLAG_CLOSE 4 /* Close after io.lines() iterator. */ - -#define IOSTDF_UD(L, id) (&gcref(G(L)->gcroot[(id)])->ud) -#define IOSTDF_IOF(L, id) ((IOFileUD *)uddata(IOSTDF_UD(L, (id)))) - -/* -- Open/close helpers -------------------------------------------------- */ - -static IOFileUD *io_tofilep(lua_State *L) -{ - if (!(L->base < L->top && tvisudata(L->base) && - udataV(L->base)->udtype == UDTYPE_IO_FILE)) - lj_err_argtype(L, 1, "FILE*"); - return (IOFileUD *)uddata(udataV(L->base)); -} - -static IOFileUD *io_tofile(lua_State *L) -{ - IOFileUD *iof = io_tofilep(L); - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOCLFL); - return iof; -} - -static IOFileUD *io_stdfile(lua_State *L, ptrdiff_t id) -{ - IOFileUD *iof = IOSTDF_IOF(L, id); - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOSTDCL); - return iof; -} - -static IOFileUD *io_file_new(lua_State *L) -{ - IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD)); - GCudata *ud = udataV(L->top-1); - ud->udtype = UDTYPE_IO_FILE; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcrefr(ud->metatable, curr_func(L)->c.env); - iof->fp = NULL; - iof->type = IOFILE_TYPE_FILE; - return iof; -} - -static IOFileUD *io_file_open(lua_State *L, const char *mode) -{ - const char *fname = strdata(lj_lib_checkstr(L, 1)); - IOFileUD *iof = io_file_new(L); - iof->fp = fopen(fname, mode); - if (iof->fp == NULL) - luaL_argerror(L, 1, lj_strfmt_pushf(L, "%s: %s", fname, strerror(errno))); - return iof; -} - -static int io_file_close(lua_State *L, IOFileUD *iof) -{ - int ok; - if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_FILE) { - ok = (fclose(iof->fp) == 0); - } else if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_PIPE) { - int stat = -1; -#if LJ_TARGET_POSIX - stat = pclose(iof->fp); -#elif LJ_TARGET_WINDOWS && !LJ_TARGET_XBOXONE && !LJ_TARGET_UWP - stat = _pclose(iof->fp); -#endif -#if LJ_52 - iof->fp = NULL; - return luaL_execresult(L, stat); -#else - ok = (stat != -1); -#endif - } else { - lj_assertL((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_STDF, - "close of unknown FILE* type"); - setnilV(L->top++); - lua_pushliteral(L, "cannot close standard file"); - return 2; - } - iof->fp = NULL; - return luaL_fileresult(L, ok, NULL); -} - -/* -- Read/write helpers -------------------------------------------------- */ - -static int io_file_readnum(lua_State *L, FILE *fp) -{ - lua_Number d; - if (fscanf(fp, LUA_NUMBER_SCAN, &d) == 1) { - if (LJ_DUALNUM) { - int32_t i = lj_num2int(d); - if (d == (lua_Number)i && !tvismzero((cTValue *)&d)) { - setintV(L->top++, i); - return 1; - } - } - setnumV(L->top++, d); - return 1; - } else { - setnilV(L->top++); - return 0; - } -} - -static int io_file_readline(lua_State *L, FILE *fp, MSize chop) -{ - MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0; - char *buf; - for (;;) { - buf = lj_buf_tmp(L, m); - if (fgets(buf+n, m-n, fp) == NULL) break; - n += (MSize)strlen(buf+n); - ok |= n; - if (n && buf[n-1] == '\n') { n -= chop; break; } - if (n >= m - 64) m += m; - } - setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); - lj_gc_check(L); - return (int)ok; -} - -static void io_file_readall(lua_State *L, FILE *fp) -{ - MSize m, n; - for (m = LUAL_BUFFERSIZE, n = 0; ; m += m) { - char *buf = lj_buf_tmp(L, m); - n += (MSize)fread(buf+n, 1, m-n, fp); - if (n != m) { - setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); - lj_gc_check(L); - return; - } - } -} - -static int io_file_readlen(lua_State *L, FILE *fp, MSize m) -{ - if (m) { - char *buf = lj_buf_tmp(L, m); - MSize n = (MSize)fread(buf, 1, m, fp); - setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); - lj_gc_check(L); - return n > 0; - } else { - int c = getc(fp); - ungetc(c, fp); - setstrV(L, L->top++, &G(L)->strempty); - return (c != EOF); - } -} - -static int io_file_read(lua_State *L, IOFileUD *iof, int start) -{ - FILE *fp = iof->fp; - int ok, n, nargs = (int)(L->top - L->base) - start; - clearerr(fp); - if (nargs == 0) { - ok = io_file_readline(L, fp, 1); - n = start+1; /* Return 1 result. */ - } else { - /* The results plus the buffers go on top of the args. */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - ok = 1; - for (n = start; nargs-- && ok; n++) { - if (tvisstr(L->base+n)) { - const char *p = strVdata(L->base+n); - if (p[0] == '*') p++; - if (p[0] == 'n') - ok = io_file_readnum(L, fp); - else if ((p[0] & ~0x20) == 'L') - ok = io_file_readline(L, fp, (p[0] == 'l')); - else if (p[0] == 'a') - io_file_readall(L, fp); - else - lj_err_arg(L, n+1, LJ_ERR_INVFMT); - } else if (tvisnumber(L->base+n)) { - ok = io_file_readlen(L, fp, (MSize)lj_lib_checkint(L, n+1)); - } else { - lj_err_arg(L, n+1, LJ_ERR_INVOPT); - } - } - } - if (ferror(fp)) - return luaL_fileresult(L, 0, NULL); - if (!ok) - setnilV(L->top-1); /* Replace last result with nil. */ - return n - start; -} - -static int io_file_write(lua_State *L, IOFileUD *iof, int start) -{ - FILE *fp = iof->fp; - cTValue *tv; - int status = 1; - for (tv = L->base+start; tv < L->top; tv++) { - MSize len; - const char *p = lj_strfmt_wstrnum(L, tv, &len); - if (!p) - lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING); - status = status && (fwrite(p, 1, len, fp) == len); - } - if (LJ_52 && status) { - L->top = L->base+1; - if (start == 0) - setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_OUTPUT)); - return 1; - } - return luaL_fileresult(L, status, NULL); -} - -static int io_file_iter(lua_State *L) -{ - GCfunc *fn = curr_func(L); - IOFileUD *iof = uddata(udataV(&fn->c.upvalue[0])); - int n = fn->c.nupvalues - 1; - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOCLFL); - L->top = L->base; - if (n) { /* Copy upvalues with options to stack. */ - lj_state_checkstack(L, (MSize)n); - memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue)); - L->top += n; - } - n = io_file_read(L, iof, 0); - if (ferror(iof->fp)) - lj_err_callermsg(L, strVdata(L->top-2)); - if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) { - io_file_close(L, iof); /* Return values are ignored. */ - return 0; - } - return n; -} - -static int io_file_lines(lua_State *L) -{ - int n = (int)(L->top - L->base); - if (n > LJ_MAX_UPVAL) - lj_err_caller(L, LJ_ERR_UNPACK); - lua_pushcclosure(L, io_file_iter, n); - return 1; -} - -/* -- I/O file methods ---------------------------------------------------- */ - -#define LJLIB_MODULE_io_method - -LJLIB_CF(io_method_close) -{ - IOFileUD *iof; - if (L->base < L->top) { - iof = io_tofile(L); - } else { - iof = IOSTDF_IOF(L, GCROOT_IO_OUTPUT); - if (iof->fp == NULL) - lj_err_caller(L, LJ_ERR_IOCLFL); - } - return io_file_close(L, iof); -} - -LJLIB_CF(io_method_read) -{ - return io_file_read(L, io_tofile(L), 1); -} - -LJLIB_CF(io_method_write) LJLIB_REC(io_write 0) -{ - return io_file_write(L, io_tofile(L), 1); -} - -LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0) -{ - return luaL_fileresult(L, fflush(io_tofile(L)->fp) == 0, NULL); -} - -#if LJ_32 && defined(__ANDROID__) && __ANDROID_API__ < 24 -/* The Android NDK is such an unmatched marvel of engineering. */ -extern int fseeko32(FILE *, long int, int) __asm__("fseeko"); -extern long int ftello32(FILE *) __asm__("ftello"); -#define fseeko(fp, pos, whence) (fseeko32((fp), (pos), (whence))) -#define ftello(fp) (ftello32((fp))) -#endif - -LJLIB_CF(io_method_seek) -{ - FILE *fp = io_tofile(L)->fp; - int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end"); - int64_t ofs = 0; - cTValue *o; - int res; - if (opt == 0) opt = SEEK_SET; - else if (opt == 1) opt = SEEK_CUR; - else if (opt == 2) opt = SEEK_END; - o = L->base+2; - if (o < L->top) { - if (tvisint(o)) - ofs = (int64_t)intV(o); - else if (tvisnum(o)) - ofs = (int64_t)numV(o); - else if (!tvisnil(o)) - lj_err_argt(L, 3, LUA_TNUMBER); - } -#if LJ_TARGET_POSIX - res = fseeko(fp, ofs, opt); -#elif _MSC_VER >= 1400 - res = _fseeki64(fp, ofs, opt); -#elif defined(__MINGW32__) - res = fseeko64(fp, ofs, opt); -#else - res = fseek(fp, (long)ofs, opt); -#endif - if (res) - return luaL_fileresult(L, 0, NULL); -#if LJ_TARGET_POSIX - ofs = ftello(fp); -#elif _MSC_VER >= 1400 - ofs = _ftelli64(fp); -#elif defined(__MINGW32__) - ofs = ftello64(fp); -#else - ofs = (int64_t)ftell(fp); -#endif - setint64V(L->top-1, ofs); - return 1; -} - -LJLIB_CF(io_method_setvbuf) -{ - FILE *fp = io_tofile(L)->fp; - int opt = lj_lib_checkopt(L, 2, -1, "\4full\4line\2no"); - size_t sz = (size_t)lj_lib_optint(L, 3, LUAL_BUFFERSIZE); - if (opt == 0) opt = _IOFBF; - else if (opt == 1) opt = _IOLBF; - else if (opt == 2) opt = _IONBF; - return luaL_fileresult(L, setvbuf(fp, NULL, opt, sz) == 0, NULL); -} - -LJLIB_CF(io_method_lines) -{ - io_tofile(L); - return io_file_lines(L); -} - -LJLIB_CF(io_method___gc) -{ - IOFileUD *iof = io_tofilep(L); - if (iof->fp != NULL && (iof->type & IOFILE_TYPE_MASK) != IOFILE_TYPE_STDF) - io_file_close(L, iof); - return 0; -} - -LJLIB_CF(io_method___tostring) -{ - IOFileUD *iof = io_tofilep(L); - if (iof->fp != NULL) - lua_pushfstring(L, "file (%p)", iof->fp); - else - lua_pushliteral(L, "file (closed)"); - return 1; -} - -LJLIB_PUSH(top-1) LJLIB_SET(__index) - -#include "lj_libdef.h" - -/* -- I/O library functions ----------------------------------------------- */ - -#define LJLIB_MODULE_io - -LJLIB_PUSH(top-2) LJLIB_SET(!) /* Set environment. */ - -LJLIB_CF(io_open) -{ - const char *fname = strdata(lj_lib_checkstr(L, 1)); - GCstr *s = lj_lib_optstr(L, 2); - const char *mode = s ? strdata(s) : "r"; - IOFileUD *iof = io_file_new(L); - iof->fp = fopen(fname, mode); - return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname); -} - -LJLIB_CF(io_popen) -{ -#if LJ_TARGET_POSIX || (LJ_TARGET_WINDOWS && !LJ_TARGET_XBOXONE && !LJ_TARGET_UWP) - const char *fname = strdata(lj_lib_checkstr(L, 1)); - GCstr *s = lj_lib_optstr(L, 2); - const char *mode = s ? strdata(s) : "r"; - IOFileUD *iof = io_file_new(L); - iof->type = IOFILE_TYPE_PIPE; -#if LJ_TARGET_POSIX - fflush(NULL); - iof->fp = popen(fname, mode); -#else - iof->fp = _popen(fname, mode); -#endif - return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname); -#else - return luaL_error(L, LUA_QL("popen") " not supported"); -#endif -} - -LJLIB_CF(io_tmpfile) -{ - IOFileUD *iof = io_file_new(L); -#if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX - iof->fp = NULL; errno = ENOSYS; -#else - iof->fp = tmpfile(); -#endif - return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, NULL); -} - -LJLIB_CF(io_close) -{ - return lj_cf_io_method_close(L); -} - -LJLIB_CF(io_read) -{ - return io_file_read(L, io_stdfile(L, GCROOT_IO_INPUT), 0); -} - -LJLIB_CF(io_write) LJLIB_REC(io_write GCROOT_IO_OUTPUT) -{ - return io_file_write(L, io_stdfile(L, GCROOT_IO_OUTPUT), 0); -} - -LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT) -{ - return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)->fp) == 0, NULL); -} - -static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode) -{ - if (L->base < L->top && !tvisnil(L->base)) { - if (tvisudata(L->base)) { - io_tofile(L); - L->top = L->base+1; - } else { - io_file_open(L, mode); - } - /* NOBARRIER: The standard I/O handles are GC roots. */ - setgcref(G(L)->gcroot[id], gcV(L->top-1)); - } else { - setudataV(L, L->top++, IOSTDF_UD(L, id)); - } - return 1; -} - -LJLIB_CF(io_input) -{ - return io_std_getset(L, GCROOT_IO_INPUT, "r"); -} - -LJLIB_CF(io_output) -{ - return io_std_getset(L, GCROOT_IO_OUTPUT, "w"); -} - -LJLIB_CF(io_lines) -{ - if (L->base == L->top) setnilV(L->top++); - if (!tvisnil(L->base)) { /* io.lines(fname) */ - IOFileUD *iof = io_file_open(L, "r"); - iof->type = IOFILE_TYPE_FILE|IOFILE_FLAG_CLOSE; - L->top--; - setudataV(L, L->base, udataV(L->top)); - } else { /* io.lines() iterates over stdin. */ - setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_INPUT)); - } - return io_file_lines(L); -} - -LJLIB_CF(io_type) -{ - cTValue *o = lj_lib_checkany(L, 1); - if (!(tvisudata(o) && udataV(o)->udtype == UDTYPE_IO_FILE)) - setnilV(L->top++); - else if (((IOFileUD *)uddata(udataV(o)))->fp != NULL) - lua_pushliteral(L, "file"); - else - lua_pushliteral(L, "closed file"); - return 1; -} - -#include "lj_libdef.h" - -/* ------------------------------------------------------------------------ */ - -static GCobj *io_std_new(lua_State *L, FILE *fp, const char *name) -{ - IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD)); - GCudata *ud = udataV(L->top-1); - ud->udtype = UDTYPE_IO_FILE; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcref(ud->metatable, gcV(L->top-3)); - iof->fp = fp; - iof->type = IOFILE_TYPE_STDF; - lua_setfield(L, -2, name); - return obj2gco(ud); -} - -LUALIB_API int luaopen_io(lua_State *L) -{ - LJ_LIB_REG(L, NULL, io_method); - copyTV(L, L->top, L->top-1); L->top++; - lua_setfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); - LJ_LIB_REG(L, LUA_IOLIBNAME, io); - setgcref(G(L)->gcroot[GCROOT_IO_INPUT], io_std_new(L, stdin, "stdin")); - setgcref(G(L)->gcroot[GCROOT_IO_OUTPUT], io_std_new(L, stdout, "stdout")); - io_std_new(L, stderr, "stderr"); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_jit.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_jit.c deleted file mode 100644 index 2867d42..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_jit.c +++ /dev/null @@ -1,761 +0,0 @@ -/* -** JIT library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lib_jit_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#if LJ_HASJIT -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_target.h" -#endif -#include "lj_trace.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_vmevent.h" -#include "lj_lib.h" - -#include "luajit.h" - -/* -- jit.* functions ----------------------------------------------------- */ - -#define LJLIB_MODULE_jit - -static int setjitmode(lua_State *L, int mode) -{ - int idx = 0; - if (L->base == L->top || tvisnil(L->base)) { /* jit.on/off/flush([nil]) */ - mode |= LUAJIT_MODE_ENGINE; - } else { - /* jit.on/off/flush(func|proto, nil|true|false) */ - if (tvisfunc(L->base) || tvisproto(L->base)) - idx = 1; - else if (!tvistrue(L->base)) /* jit.on/off/flush(true, nil|true|false) */ - goto err; - if (L->base+1 < L->top && tvisbool(L->base+1)) - mode |= boolV(L->base+1) ? LUAJIT_MODE_ALLFUNC : LUAJIT_MODE_ALLSUBFUNC; - else - mode |= LUAJIT_MODE_FUNC; - } - if (luaJIT_setmode(L, idx, mode) != 1) { - if ((mode & LUAJIT_MODE_MASK) == LUAJIT_MODE_ENGINE) - lj_err_caller(L, LJ_ERR_NOJIT); - err: - lj_err_argt(L, 1, LUA_TFUNCTION); - } - return 0; -} - -LJLIB_CF(jit_on) -{ - return setjitmode(L, LUAJIT_MODE_ON); -} - -LJLIB_CF(jit_off) -{ - return setjitmode(L, LUAJIT_MODE_OFF); -} - -LJLIB_CF(jit_flush) -{ -#if LJ_HASJIT - if (L->base < L->top && tvisnumber(L->base)) { - int traceno = lj_lib_checkint(L, 1); - luaJIT_setmode(L, traceno, LUAJIT_MODE_FLUSH|LUAJIT_MODE_TRACE); - return 0; - } -#endif - return setjitmode(L, LUAJIT_MODE_FLUSH); -} - -#if LJ_HASJIT -/* Push a string for every flag bit that is set. */ -static void flagbits_to_strings(lua_State *L, uint32_t flags, uint32_t base, - const char *str) -{ - for (; *str; base <<= 1, str += 1+*str) - if (flags & base) - setstrV(L, L->top++, lj_str_new(L, str+1, *(uint8_t *)str)); -} -#endif - -LJLIB_CF(jit_status) -{ -#if LJ_HASJIT - jit_State *J = L2J(L); - L->top = L->base; - setboolV(L->top++, (J->flags & JIT_F_ON) ? 1 : 0); - flagbits_to_strings(L, J->flags, JIT_F_CPU, JIT_F_CPUSTRING); - flagbits_to_strings(L, J->flags, JIT_F_OPT, JIT_F_OPTSTRING); - return (int)(L->top - L->base); -#else - setboolV(L->top++, 0); - return 1; -#endif -} - -LJLIB_CF(jit_security) -{ - int idx = lj_lib_checkopt(L, 1, -1, LJ_SECURITY_MODESTRING); - setintV(L->top++, ((LJ_SECURITY_MODE >> (2*idx)) & 3)); - return 1; -} - -LJLIB_CF(jit_attach) -{ -#ifdef LUAJIT_DISABLE_VMEVENT - luaL_error(L, "vmevent API disabled"); -#else - GCfunc *fn = lj_lib_checkfunc(L, 1); - GCstr *s = lj_lib_optstr(L, 2); - luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE); - if (s) { /* Attach to given event. */ - const uint8_t *p = (const uint8_t *)strdata(s); - uint32_t h = s->len; - while (*p) h = h ^ (lj_rol(h, 6) + *p++); - lua_pushvalue(L, 1); - lua_rawseti(L, -2, VMEVENT_HASHIDX(h)); - G(L)->vmevmask = VMEVENT_NOCACHE; /* Invalidate cache. */ - } else { /* Detach if no event given. */ - setnilV(L->top++); - while (lua_next(L, -2)) { - L->top--; - if (tvisfunc(L->top) && funcV(L->top) == fn) { - setnilV(lj_tab_set(L, tabV(L->top-2), L->top-1)); - } - } - } -#endif - return 0; -} - -LJLIB_PUSH(top-5) LJLIB_SET(os) -LJLIB_PUSH(top-4) LJLIB_SET(arch) -LJLIB_PUSH(top-3) LJLIB_SET(version_num) -LJLIB_PUSH(top-2) LJLIB_SET(version) - -#include "lj_libdef.h" - -/* -- jit.util.* functions ------------------------------------------------ */ - -#define LJLIB_MODULE_jit_util - -/* -- Reflection API for Lua functions ------------------------------------ */ - -/* Return prototype of first argument (Lua function or prototype object) */ -static GCproto *check_Lproto(lua_State *L, int nolua) -{ - TValue *o = L->base; - if (L->top > o) { - if (tvisproto(o)) { - return protoV(o); - } else if (tvisfunc(o)) { - if (isluafunc(funcV(o))) - return funcproto(funcV(o)); - else if (nolua) - return NULL; - } - } - lj_err_argt(L, 1, LUA_TFUNCTION); - return NULL; /* unreachable */ -} - -static void setintfield(lua_State *L, GCtab *t, const char *name, int32_t val) -{ - setintV(lj_tab_setstr(L, t, lj_str_newz(L, name)), val); -} - -/* local info = jit.util.funcinfo(func [,pc]) */ -LJLIB_CF(jit_util_funcinfo) -{ - GCproto *pt = check_Lproto(L, 1); - if (pt) { - BCPos pc = (BCPos)lj_lib_optint(L, 2, 0); - GCtab *t; - lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - setintfield(L, t, "linedefined", pt->firstline); - setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline); - setintfield(L, t, "stackslots", pt->framesize); - setintfield(L, t, "params", pt->numparams); - setintfield(L, t, "bytecodes", (int32_t)pt->sizebc); - setintfield(L, t, "gcconsts", (int32_t)pt->sizekgc); - setintfield(L, t, "nconsts", (int32_t)pt->sizekn); - setintfield(L, t, "upvalues", (int32_t)pt->sizeuv); - if (pc < pt->sizebc) - setintfield(L, t, "currentline", lj_debug_line(pt, pc)); - lua_pushboolean(L, (pt->flags & PROTO_VARARG)); - lua_setfield(L, -2, "isvararg"); - lua_pushboolean(L, (pt->flags & PROTO_CHILD)); - lua_setfield(L, -2, "children"); - setstrV(L, L->top++, proto_chunkname(pt)); - lua_setfield(L, -2, "source"); - lj_debug_pushloc(L, pt, pc); - lua_setfield(L, -2, "loc"); - setprotoV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "proto")), pt); - } else { - GCfunc *fn = funcV(L->base); - GCtab *t; - lua_createtable(L, 0, 4); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - if (!iscfunc(fn)) - setintfield(L, t, "ffid", fn->c.ffid); - setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")), - (intptr_t)(void *)fn->c.f); - setintfield(L, t, "upvalues", fn->c.nupvalues); - } - return 1; -} - -/* local ins, m = jit.util.funcbc(func, pc) */ -LJLIB_CF(jit_util_funcbc) -{ - GCproto *pt = check_Lproto(L, 0); - BCPos pc = (BCPos)lj_lib_checkint(L, 2); - if (pc < pt->sizebc) { - BCIns ins = proto_bc(pt)[pc]; - BCOp op = bc_op(ins); - lj_assertL(op < BC__MAX, "bad bytecode op %d", op); - setintV(L->top, ins); - setintV(L->top+1, lj_bc_mode[op]); - L->top += 2; - return 2; - } - return 0; -} - -/* local k = jit.util.funck(func, idx) */ -LJLIB_CF(jit_util_funck) -{ - GCproto *pt = check_Lproto(L, 0); - ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2); - if (idx >= 0) { - if (idx < (ptrdiff_t)pt->sizekn) { - copyTV(L, L->top-1, proto_knumtv(pt, idx)); - return 1; - } - } else { - if (~idx < (ptrdiff_t)pt->sizekgc) { - GCobj *gc = proto_kgc(pt, idx); - setgcV(L, L->top-1, gc, ~gc->gch.gct); - return 1; - } - } - return 0; -} - -/* local name = jit.util.funcuvname(func, idx) */ -LJLIB_CF(jit_util_funcuvname) -{ - GCproto *pt = check_Lproto(L, 0); - uint32_t idx = (uint32_t)lj_lib_checkint(L, 2); - if (idx < pt->sizeuv) { - setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx))); - return 1; - } - return 0; -} - -/* -- Reflection API for traces ------------------------------------------- */ - -#if LJ_HASJIT - -/* Check trace argument. Must not throw for non-existent trace numbers. */ -static GCtrace *jit_checktrace(lua_State *L) -{ - TraceNo tr = (TraceNo)lj_lib_checkint(L, 1); - jit_State *J = L2J(L); - if (tr > 0 && tr < J->sizetrace) - return traceref(J, tr); - return NULL; -} - -/* Names of link types. ORDER LJ_TRLINK */ -static const char *const jit_trlinkname[] = { - "none", "root", "loop", "tail-recursion", "up-recursion", "down-recursion", - "interpreter", "return", "stitch" -}; - -/* local info = jit.util.traceinfo(tr) */ -LJLIB_CF(jit_util_traceinfo) -{ - GCtrace *T = jit_checktrace(L); - if (T) { - GCtab *t; - lua_createtable(L, 0, 8); /* Increment hash size if fields are added. */ - t = tabV(L->top-1); - setintfield(L, t, "nins", (int32_t)T->nins - REF_BIAS - 1); - setintfield(L, t, "nk", REF_BIAS - (int32_t)T->nk); - setintfield(L, t, "link", T->link); - setintfield(L, t, "nexit", T->nsnap); - setstrV(L, L->top++, lj_str_newz(L, jit_trlinkname[T->linktype])); - lua_setfield(L, -2, "linktype"); - /* There are many more fields. Add them only when needed. */ - return 1; - } - return 0; -} - -/* local m, ot, op1, op2, prev = jit.util.traceir(tr, idx) */ -LJLIB_CF(jit_util_traceir) -{ - GCtrace *T = jit_checktrace(L); - IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS; - if (T && ref >= REF_BIAS && ref < T->nins) { - IRIns *ir = &T->ir[ref]; - int32_t m = lj_ir_mode[ir->o]; - setintV(L->top-2, m); - setintV(L->top-1, ir->ot); - setintV(L->top++, (int32_t)ir->op1 - (irm_op1(m)==IRMref ? REF_BIAS : 0)); - setintV(L->top++, (int32_t)ir->op2 - (irm_op2(m)==IRMref ? REF_BIAS : 0)); - setintV(L->top++, ir->prev); - return 5; - } - return 0; -} - -/* local k, t [, slot] = jit.util.tracek(tr, idx) */ -LJLIB_CF(jit_util_tracek) -{ - GCtrace *T = jit_checktrace(L); - IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS; - if (T && ref >= T->nk && ref < REF_BIAS) { - IRIns *ir = &T->ir[ref]; - int32_t slot = -1; - if (ir->o == IR_KSLOT) { - slot = ir->op2; - ir = &T->ir[ir->op1]; - } -#if LJ_HASFFI - if (ir->o == IR_KINT64) ctype_loadffi(L); -#endif - lj_ir_kvalue(L, L->top-2, ir); - setintV(L->top-1, (int32_t)irt_type(ir->t)); - if (slot == -1) - return 2; - setintV(L->top++, slot); - return 3; - } - return 0; -} - -/* local snap = jit.util.tracesnap(tr, sn) */ -LJLIB_CF(jit_util_tracesnap) -{ - GCtrace *T = jit_checktrace(L); - SnapNo sn = (SnapNo)lj_lib_checkint(L, 2); - if (T && sn < T->nsnap) { - SnapShot *snap = &T->snap[sn]; - SnapEntry *map = &T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - GCtab *t; - lua_createtable(L, nent+2, 0); - t = tabV(L->top-1); - setintV(lj_tab_setint(L, t, 0), (int32_t)snap->ref - REF_BIAS); - setintV(lj_tab_setint(L, t, 1), (int32_t)snap->nslots); - for (n = 0; n < nent; n++) - setintV(lj_tab_setint(L, t, (int32_t)(n+2)), (int32_t)map[n]); - setintV(lj_tab_setint(L, t, (int32_t)(nent+2)), (int32_t)SNAP(255, 0, 0)); - return 1; - } - return 0; -} - -/* local mcode, addr, loop = jit.util.tracemc(tr) */ -LJLIB_CF(jit_util_tracemc) -{ - GCtrace *T = jit_checktrace(L); - if (T && T->mcode != NULL) { - setstrV(L, L->top-1, lj_str_new(L, (const char *)T->mcode, T->szmcode)); - setintptrV(L->top++, (intptr_t)(void *)T->mcode); - setintV(L->top++, T->mcloop); - return 3; - } - return 0; -} - -/* local addr = jit.util.traceexitstub([tr,] exitno) */ -LJLIB_CF(jit_util_traceexitstub) -{ -#ifdef EXITSTUBS_PER_GROUP - ExitNo exitno = (ExitNo)lj_lib_checkint(L, 1); - jit_State *J = L2J(L); - if (exitno < EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) { - setintptrV(L->top-1, (intptr_t)(void *)exitstub_addr(J, exitno)); - return 1; - } -#else - if (L->top > L->base+1) { /* Don't throw for one-argument variant. */ - GCtrace *T = jit_checktrace(L); - ExitNo exitno = (ExitNo)lj_lib_checkint(L, 2); - ExitNo maxexit = T->root ? T->nsnap+1 : T->nsnap; - if (T && T->mcode != NULL && exitno < maxexit) { - setintptrV(L->top-1, (intptr_t)(void *)exitstub_trace_addr(T, exitno)); - return 1; - } - } -#endif - return 0; -} - -/* local addr = jit.util.ircalladdr(idx) */ -LJLIB_CF(jit_util_ircalladdr) -{ - uint32_t idx = (uint32_t)lj_lib_checkint(L, 1); - if (idx < IRCALL__MAX) { - setintptrV(L->top-1, (intptr_t)(void *)lj_ir_callinfo[idx].func); - return 1; - } - return 0; -} - -#endif - -#include "lj_libdef.h" - -static int luaopen_jit_util(lua_State *L) -{ - LJ_LIB_REG(L, NULL, jit_util); - return 1; -} - -/* -- jit.opt module ------------------------------------------------------ */ - -#if LJ_HASJIT - -#define LJLIB_MODULE_jit_opt - -/* Parse optimization level. */ -static int jitopt_level(jit_State *J, const char *str) -{ - if (str[0] >= '0' && str[0] <= '9' && str[1] == '\0') { - uint32_t flags; - if (str[0] == '0') flags = JIT_F_OPT_0; - else if (str[0] == '1') flags = JIT_F_OPT_1; - else if (str[0] == '2') flags = JIT_F_OPT_2; - else flags = JIT_F_OPT_3; - J->flags = (J->flags & ~JIT_F_OPT_MASK) | flags; - return 1; /* Ok. */ - } - return 0; /* No match. */ -} - -/* Parse optimization flag. */ -static int jitopt_flag(jit_State *J, const char *str) -{ - const char *lst = JIT_F_OPTSTRING; - uint32_t opt; - int set = 1; - if (str[0] == '+') { - str++; - } else if (str[0] == '-') { - str++; - set = 0; - } else if (str[0] == 'n' && str[1] == 'o') { - str += str[2] == '-' ? 3 : 2; - set = 0; - } - for (opt = JIT_F_OPT; ; opt <<= 1) { - size_t len = *(const uint8_t *)lst; - if (len == 0) - break; - if (strncmp(str, lst+1, len) == 0 && str[len] == '\0') { - if (set) J->flags |= opt; else J->flags &= ~opt; - return 1; /* Ok. */ - } - lst += 1+len; - } - return 0; /* No match. */ -} - -/* Parse optimization parameter. */ -static int jitopt_param(jit_State *J, const char *str) -{ - const char *lst = JIT_P_STRING; - int i; - for (i = 0; i < JIT_P__MAX; i++) { - size_t len = *(const uint8_t *)lst; - lj_assertJ(len != 0, "bad JIT_P_STRING"); - if (strncmp(str, lst+1, len) == 0 && str[len] == '=') { - int32_t n = 0; - const char *p = &str[len+1]; - while (*p >= '0' && *p <= '9') - n = n*10 + (*p++ - '0'); - if (*p) return 0; /* Malformed number. */ - J->param[i] = n; - if (i == JIT_P_hotloop) - lj_dispatch_init_hotcount(J2G(J)); - return 1; /* Ok. */ - } - lst += 1+len; - } - return 0; /* No match. */ -} - -/* jit.opt.start(flags...) */ -LJLIB_CF(jit_opt_start) -{ - jit_State *J = L2J(L); - int nargs = (int)(L->top - L->base); - if (nargs == 0) { - J->flags = (J->flags & ~JIT_F_OPT_MASK) | JIT_F_OPT_DEFAULT; - } else { - int i; - for (i = 1; i <= nargs; i++) { - const char *str = strdata(lj_lib_checkstr(L, i)); - if (!jitopt_level(J, str) && - !jitopt_flag(J, str) && - !jitopt_param(J, str)) - lj_err_callerv(L, LJ_ERR_JITOPT, str); - } - } - return 0; -} - -#include "lj_libdef.h" - -#endif - -/* -- jit.profile module -------------------------------------------------- */ - -#if LJ_HASPROFILE - -#define LJLIB_MODULE_jit_profile - -/* Not loaded by default, use: local profile = require("jit.profile") */ - -#define KEY_PROFILE_THREAD (U64x(80000000,00000000)|'t') -#define KEY_PROFILE_FUNC (U64x(80000000,00000000)|'f') - -static void jit_profile_callback(lua_State *L2, lua_State *L, int samples, - int vmstate) -{ - TValue key; - cTValue *tv; - key.u64 = KEY_PROFILE_FUNC; - tv = lj_tab_get(L, tabV(registry(L)), &key); - if (tvisfunc(tv)) { - char vmst = (char)vmstate; - int status; - setfuncV(L2, L2->top++, funcV(tv)); - setthreadV(L2, L2->top++, L); - setintV(L2->top++, samples); - setstrV(L2, L2->top++, lj_str_new(L2, &vmst, 1)); - status = lua_pcall(L2, 3, 0, 0); /* callback(thread, samples, vmstate) */ - if (status) { - if (G(L2)->panic) G(L2)->panic(L2); - exit(EXIT_FAILURE); - } - lj_trace_abort(G(L2)); - } -} - -/* profile.start(mode, cb) */ -LJLIB_CF(jit_profile_start) -{ - GCtab *registry = tabV(registry(L)); - GCstr *mode = lj_lib_optstr(L, 1); - GCfunc *func = lj_lib_checkfunc(L, 2); - lua_State *L2 = lua_newthread(L); /* Thread that runs profiler callback. */ - TValue key; - /* Anchor thread and function in registry. */ - key.u64 = KEY_PROFILE_THREAD; - setthreadV(L, lj_tab_set(L, registry, &key), L2); - key.u64 = KEY_PROFILE_FUNC; - setfuncV(L, lj_tab_set(L, registry, &key), func); - lj_gc_anybarriert(L, registry); - luaJIT_profile_start(L, mode ? strdata(mode) : "", - (luaJIT_profile_callback)jit_profile_callback, L2); - return 0; -} - -/* profile.stop() */ -LJLIB_CF(jit_profile_stop) -{ - GCtab *registry; - TValue key; - luaJIT_profile_stop(L); - registry = tabV(registry(L)); - key.u64 = KEY_PROFILE_THREAD; - setnilV(lj_tab_set(L, registry, &key)); - key.u64 = KEY_PROFILE_FUNC; - setnilV(lj_tab_set(L, registry, &key)); - lj_gc_anybarriert(L, registry); - return 0; -} - -/* dump = profile.dumpstack([thread,] fmt, depth) */ -LJLIB_CF(jit_profile_dumpstack) -{ - lua_State *L2 = L; - int arg = 0; - size_t len; - int depth; - GCstr *fmt; - const char *p; - if (L->top > L->base && tvisthread(L->base)) { - L2 = threadV(L->base); - arg = 1; - } - fmt = lj_lib_checkstr(L, arg+1); - depth = lj_lib_checkint(L, arg+2); - p = luaJIT_profile_dumpstack(L2, strdata(fmt), depth, &len); - lua_pushlstring(L, p, len); - return 1; -} - -#include "lj_libdef.h" - -static int luaopen_jit_profile(lua_State *L) -{ - LJ_LIB_REG(L, NULL, jit_profile); - return 1; -} - -#endif - -/* -- JIT compiler initialization ----------------------------------------- */ - -#if LJ_HASJIT -/* Default values for JIT parameters. */ -static const int32_t jit_param_default[JIT_P__MAX+1] = { -#define JIT_PARAMINIT(len, name, value) (value), -JIT_PARAMDEF(JIT_PARAMINIT) -#undef JIT_PARAMINIT - 0 -}; - -#if LJ_TARGET_ARM && LJ_TARGET_LINUX -#include -#endif - -/* Arch-dependent CPU feature detection. */ -static uint32_t jit_cpudetect(void) -{ - uint32_t flags = 0; -#if LJ_TARGET_X86ORX64 - - uint32_t vendor[4]; - uint32_t features[4]; - if (lj_vm_cpuid(0, vendor) && lj_vm_cpuid(1, features)) { - flags |= ((features[2] >> 0)&1) * JIT_F_SSE3; - flags |= ((features[2] >> 19)&1) * JIT_F_SSE4_1; - if (vendor[0] >= 7) { - uint32_t xfeatures[4]; - lj_vm_cpuid(7, xfeatures); - flags |= ((xfeatures[1] >> 8)&1) * JIT_F_BMI2; - } - } - /* Don't bother checking for SSE2 -- the VM will crash before getting here. */ - -#elif LJ_TARGET_ARM - - int ver = LJ_ARCH_VERSION; /* Compile-time ARM CPU detection. */ -#if LJ_TARGET_LINUX - if (ver < 70) { /* Runtime ARM CPU detection. */ - struct utsname ut; - uname(&ut); - if (strncmp(ut.machine, "armv", 4) == 0) { - if (ut.machine[4] >= '8') ver = 80; - else if (ut.machine[4] == '7') ver = 70; - else if (ut.machine[4] == '6') ver = 60; - } - } -#endif - flags |= ver >= 70 ? JIT_F_ARMV7 : - ver >= 61 ? JIT_F_ARMV6T2_ : - ver >= 60 ? JIT_F_ARMV6_ : 0; - flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2; - -#elif LJ_TARGET_ARM64 - - /* No optional CPU features to detect (for now). */ - -#elif LJ_TARGET_PPC - -#if LJ_ARCH_SQRT - flags |= JIT_F_SQRT; -#endif -#if LJ_ARCH_ROUND - flags |= JIT_F_ROUND; -#endif - -#elif LJ_TARGET_MIPS - - /* Compile-time MIPS CPU detection. */ -#if LJ_ARCH_VERSION >= 20 - flags |= JIT_F_MIPSXXR2; -#endif - /* Runtime MIPS CPU detection. */ -#if defined(__GNUC__) - if (!(flags & JIT_F_MIPSXXR2)) { - int x; -#ifdef __mips16 - x = 0; /* Runtime detection is difficult. Ensure optimal -march flags. */ -#else - /* On MIPS32R1 rotr is treated as srl. rotr r2,r2,1 -> srl r2,r2,1. */ - __asm__("li $2, 1\n\t.long 0x00221042\n\tmove %0, $2" : "=r"(x) : : "$2"); -#endif - if (x) flags |= JIT_F_MIPSXXR2; /* Either 0x80000000 (R2) or 0 (R1). */ - } -#endif - -#else -#error "Missing CPU detection for this architecture" -#endif - return flags; -} - -/* Initialize JIT compiler. */ -static void jit_init(lua_State *L) -{ - jit_State *J = L2J(L); - J->flags = jit_cpudetect() | JIT_F_ON | JIT_F_OPT_DEFAULT; - memcpy(J->param, jit_param_default, sizeof(J->param)); - lj_dispatch_update(G(L)); -} -#endif - -LUALIB_API int luaopen_jit(lua_State *L) -{ -#if LJ_HASJIT - jit_init(L); -#endif - lua_pushliteral(L, LJ_OS_NAME); - lua_pushliteral(L, LJ_ARCH_NAME); - lua_pushinteger(L, LUAJIT_VERSION_NUM); - lua_pushliteral(L, LUAJIT_VERSION); - LJ_LIB_REG(L, LUA_JITLIBNAME, jit); -#if LJ_HASPROFILE - lj_lib_prereg(L, LUA_JITLIBNAME ".profile", luaopen_jit_profile, - tabref(L->env)); -#endif -#ifndef LUAJIT_DISABLE_JITUTIL - lj_lib_prereg(L, LUA_JITLIBNAME ".util", luaopen_jit_util, tabref(L->env)); -#endif -#if LJ_HASJIT - LJ_LIB_REG(L, "jit.opt", jit_opt); -#endif - L->top -= 2; - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_math.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_math.c deleted file mode 100644 index b677bbc..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_math.c +++ /dev/null @@ -1,201 +0,0 @@ -/* -** Math library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lib_math_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_lib.h" -#include "lj_vm.h" -#include "lj_prng.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_math - -LJLIB_ASM(math_abs) LJLIB_REC(.) -{ - lj_lib_checknumber(L, 1); - return FFH_RETRY; -} -LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR) -LJLIB_ASM_(math_ceil) LJLIB_REC(math_round IRFPM_CEIL) - -LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT) -{ - lj_lib_checknum(L, 1); - return FFH_RETRY; -} -LJLIB_ASM_(math_log10) LJLIB_REC(math_call IRCALL_log10) -LJLIB_ASM_(math_exp) LJLIB_REC(math_call IRCALL_exp) -LJLIB_ASM_(math_sin) LJLIB_REC(math_call IRCALL_sin) -LJLIB_ASM_(math_cos) LJLIB_REC(math_call IRCALL_cos) -LJLIB_ASM_(math_tan) LJLIB_REC(math_call IRCALL_tan) -LJLIB_ASM_(math_asin) LJLIB_REC(math_call IRCALL_asin) -LJLIB_ASM_(math_acos) LJLIB_REC(math_call IRCALL_acos) -LJLIB_ASM_(math_atan) LJLIB_REC(math_call IRCALL_atan) -LJLIB_ASM_(math_sinh) LJLIB_REC(math_call IRCALL_sinh) -LJLIB_ASM_(math_cosh) LJLIB_REC(math_call IRCALL_cosh) -LJLIB_ASM_(math_tanh) LJLIB_REC(math_call IRCALL_tanh) -LJLIB_ASM_(math_frexp) -LJLIB_ASM_(math_modf) - -LJLIB_ASM(math_log) LJLIB_REC(math_log) -{ - double x = lj_lib_checknum(L, 1); - if (L->base+1 < L->top) { - double y = lj_lib_checknum(L, 2); -#ifdef LUAJIT_NO_LOG2 - x = log(x); y = 1.0 / log(y); -#else - x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y); -#endif - setnumV(L->base-1-LJ_FR2, x*y); /* Do NOT join the expression to x / y. */ - return FFH_RES(1); - } - return FFH_RETRY; -} - -LJLIB_LUA(math_deg) /* function(x) return x * 57.29577951308232 end */ -LJLIB_LUA(math_rad) /* function(x) return x * 0.017453292519943295 end */ - -LJLIB_ASM(math_atan2) LJLIB_REC(.) -{ - lj_lib_checknum(L, 1); - lj_lib_checknum(L, 2); - return FFH_RETRY; -} -LJLIB_ASM_(math_pow) LJLIB_REC(.) -LJLIB_ASM_(math_fmod) - -LJLIB_ASM(math_ldexp) LJLIB_REC(.) -{ - lj_lib_checknum(L, 1); -#if LJ_DUALNUM && !LJ_TARGET_X86ORX64 - lj_lib_checkint(L, 2); -#else - lj_lib_checknum(L, 2); -#endif - return FFH_RETRY; -} - -LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN) -{ - int i = 0; - do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); - return FFH_RETRY; -} -LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX) - -LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi) -LJLIB_PUSH(1e310) LJLIB_SET(huge) - -/* ------------------------------------------------------------------------ */ - -/* This implements a Tausworthe PRNG with period 2^223. Based on: -** Tables of maximally-equidistributed combined LFSR generators, -** Pierre L'Ecuyer, 1991, table 3, 1st entry. -** Full-period ME-CF generator with L=64, J=4, k=223, N1=49. -*/ - -/* Union needed for bit-pattern conversion between uint64_t and double. */ -typedef union { uint64_t u64; double d; } U64double; - -/* PRNG seeding function. */ -static void random_seed(PRNGState *rs, double d) -{ - uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */ - int i; - for (i = 0; i < 4; i++) { - U64double u; - uint32_t m = 1u << (r&255); - r >>= 8; - u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; - if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of u[i] are non-zero. */ - rs->u[i] = u.u64; - } - for (i = 0; i < 10; i++) - (void)lj_prng_u64(rs); -} - -/* PRNG extract function. */ -LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */ -LJLIB_CF(math_random) LJLIB_REC(.) -{ - int n = (int)(L->top - L->base); - PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); - U64double u; - double d; - u.u64 = lj_prng_u64d(rs); - d = u.d - 1.0; - if (n > 0) { -#if LJ_DUALNUM - int isint = 1; - double r1; - lj_lib_checknumber(L, 1); - if (tvisint(L->base)) { - r1 = (lua_Number)intV(L->base); - } else { - isint = 0; - r1 = numV(L->base); - } -#else - double r1 = lj_lib_checknum(L, 1); -#endif - if (n == 1) { - d = lj_vm_floor(d*r1) + 1.0; /* d is an int in range [1, r1] */ - } else { -#if LJ_DUALNUM - double r2; - lj_lib_checknumber(L, 2); - if (tvisint(L->base+1)) { - r2 = (lua_Number)intV(L->base+1); - } else { - isint = 0; - r2 = numV(L->base+1); - } -#else - double r2 = lj_lib_checknum(L, 2); -#endif - d = lj_vm_floor(d*(r2-r1+1.0)) + r1; /* d is an int in range [r1, r2] */ - } -#if LJ_DUALNUM - if (isint) { - setintV(L->top-1, lj_num2int(d)); - return 1; - } -#endif - } /* else: d is a double in range [0, 1] */ - setnumV(L->top++, d); - return 1; -} - -/* PRNG seed function. */ -LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */ -LJLIB_CF(math_randomseed) -{ - PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); - random_seed(rs, lj_lib_checknum(L, 1)); - return 0; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_math(lua_State *L) -{ - PRNGState *rs = (PRNGState *)lua_newuserdata(L, sizeof(PRNGState)); - lj_prng_seed_fixed(rs); - LJ_LIB_REG(L, LUA_MATHLIBNAME, math); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_os.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_os.c deleted file mode 100644 index 6bcd014..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_os.c +++ /dev/null @@ -1,292 +0,0 @@ -/* -** OS library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include - -#define lib_os_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_lib.h" - -#if LJ_TARGET_POSIX -#include -#else -#include -#endif - -#if !LJ_TARGET_PSVITA -#include -#endif - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_os - -LJLIB_CF(os_execute) -{ -#if LJ_NO_SYSTEM -#if LJ_52 - errno = ENOSYS; - return luaL_fileresult(L, 0, NULL); -#else - lua_pushinteger(L, -1); - return 1; -#endif -#else - const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); -#if LJ_52 - if (cmd) - return luaL_execresult(L, stat); - setboolV(L->top++, 1); -#else - setintV(L->top++, stat); -#endif - return 1; -#endif -} - -LJLIB_CF(os_remove) -{ - const char *filename = luaL_checkstring(L, 1); - return luaL_fileresult(L, remove(filename) == 0, filename); -} - -LJLIB_CF(os_rename) -{ - const char *fromname = luaL_checkstring(L, 1); - const char *toname = luaL_checkstring(L, 2); - return luaL_fileresult(L, rename(fromname, toname) == 0, fromname); -} - -LJLIB_CF(os_tmpname) -{ -#if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX - lj_err_caller(L, LJ_ERR_OSUNIQF); - return 0; -#else -#if LJ_TARGET_POSIX - char buf[15+1]; - int fp; - strcpy(buf, "/tmp/lua_XXXXXX"); - fp = mkstemp(buf); - if (fp != -1) - close(fp); - else - lj_err_caller(L, LJ_ERR_OSUNIQF); -#else - char buf[L_tmpnam]; - if (tmpnam(buf) == NULL) - lj_err_caller(L, LJ_ERR_OSUNIQF); -#endif - lua_pushstring(L, buf); - return 1; -#endif -} - -LJLIB_CF(os_getenv) -{ -#if LJ_TARGET_CONSOLE - lua_pushnil(L); -#else - lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ -#endif - return 1; -} - -LJLIB_CF(os_exit) -{ - int status; - if (L->base < L->top && tvisbool(L->base)) - status = boolV(L->base) ? EXIT_SUCCESS : EXIT_FAILURE; - else - status = lj_lib_optint(L, 1, EXIT_SUCCESS); - if (L->base+1 < L->top && tvistruecond(L->base+1)) - lua_close(L); - exit(status); - return 0; /* Unreachable. */ -} - -LJLIB_CF(os_clock) -{ - setnumV(L->top++, ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC)); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void setfield(lua_State *L, const char *key, int value) -{ - lua_pushinteger(L, value); - lua_setfield(L, -2, key); -} - -static void setboolfield(lua_State *L, const char *key, int value) -{ - if (value < 0) /* undefined? */ - return; /* does not set field */ - lua_pushboolean(L, value); - lua_setfield(L, -2, key); -} - -static int getboolfield(lua_State *L, const char *key) -{ - int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); - lua_pop(L, 1); - return res; -} - -static int getfield(lua_State *L, const char *key, int d) -{ - int res; - lua_getfield(L, -1, key); - if (lua_isnumber(L, -1)) { - res = (int)lua_tointeger(L, -1); - } else { - if (d < 0) - lj_err_callerv(L, LJ_ERR_OSDATEF, key); - res = d; - } - lua_pop(L, 1); - return res; -} - -LJLIB_CF(os_date) -{ - const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); - struct tm *stm; -#if LJ_TARGET_POSIX - struct tm rtm; -#endif - if (*s == '!') { /* UTC? */ - s++; /* Skip '!' */ -#if LJ_TARGET_POSIX - stm = gmtime_r(&t, &rtm); -#else - stm = gmtime(&t); -#endif - } else { -#if LJ_TARGET_POSIX - stm = localtime_r(&t, &rtm); -#else - stm = localtime(&t); -#endif - } - if (stm == NULL) { /* Invalid date? */ - setnilV(L->top++); - } else if (strcmp(s, "*t") == 0) { - lua_createtable(L, 0, 9); /* 9 = number of fields */ - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon+1); - setfield(L, "year", stm->tm_year+1900); - setfield(L, "wday", stm->tm_wday+1); - setfield(L, "yday", stm->tm_yday+1); - setboolfield(L, "isdst", stm->tm_isdst); - } else if (*s) { - SBuf *sb = &G(L)->tmpbuf; - MSize sz = 0, retry = 4; - const char *q; - for (q = s; *q; q++) - sz += (*q == '%') ? 30 : 1; /* Overflow doesn't matter. */ - setsbufL(sb, L); - while (retry--) { /* Limit growth for invalid format or empty result. */ - char *buf = lj_buf_need(sb, sz); - size_t len = strftime(buf, sbufsz(sb), s, stm); - if (len) { - setstrV(L, L->top++, lj_str_new(L, buf, len)); - lj_gc_check(L); - break; - } - sz += (sz|1); - } - } else { - setstrV(L, L->top++, &G(L)->strempty); - } - return 1; -} - -LJLIB_CF(os_time) -{ - time_t t; - if (lua_isnoneornil(L, 1)) { /* called without args? */ - t = time(NULL); /* get current time */ - } else { - struct tm ts; - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0); - ts.tm_min = getfield(L, "min", 0); - ts.tm_hour = getfield(L, "hour", 12); - ts.tm_mday = getfield(L, "day", -1); - ts.tm_mon = getfield(L, "month", -1) - 1; - ts.tm_year = getfield(L, "year", -1) - 1900; - ts.tm_isdst = getboolfield(L, "isdst"); - t = mktime(&ts); - } - if (t == (time_t)(-1)) - lua_pushnil(L); - else - lua_pushnumber(L, (lua_Number)t); - return 1; -} - -LJLIB_CF(os_difftime) -{ - lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), - (time_t)(luaL_optnumber(L, 2, (lua_Number)0)))); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(os_setlocale) -{ -#if LJ_TARGET_PSVITA - lua_pushliteral(L, "C"); -#else - GCstr *s = lj_lib_optstr(L, 1); - const char *str = s ? strdata(s) : NULL; - int opt = lj_lib_checkopt(L, 2, 6, - "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all"); - if (opt == 0) opt = LC_CTYPE; - else if (opt == 1) opt = LC_NUMERIC; - else if (opt == 2) opt = LC_TIME; - else if (opt == 3) opt = LC_COLLATE; - else if (opt == 4) opt = LC_MONETARY; - else if (opt == 6) opt = LC_ALL; - lua_pushstring(L, setlocale(opt, str)); -#endif - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_os(lua_State *L) -{ - LJ_LIB_REG(L, LUA_OSLIBNAME, os); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_package.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_package.c deleted file mode 100644 index 63a9121..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_package.c +++ /dev/null @@ -1,628 +0,0 @@ -/* -** Package library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_package_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -/* Error codes for ll_loadfunc. */ -#define PACKAGE_ERR_LIB 1 -#define PACKAGE_ERR_FUNC 2 -#define PACKAGE_ERR_LOAD 3 - -/* Redefined in platform specific part. */ -#define PACKAGE_LIB_FAIL "open" -#define setprogdir(L) ((void)0) - -/* Symbol name prefixes. */ -#define SYMPREFIX_CF "luaopen_%s" -#define SYMPREFIX_BC "luaJIT_BC_%s" - -#if LJ_TARGET_DLOPEN - -#include - -static void ll_unloadlib(void *lib) -{ - dlclose(lib); -} - -static void *ll_load(lua_State *L, const char *path, int gl) -{ - void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL)); - if (lib == NULL) lua_pushstring(L, dlerror()); - return lib; -} - -static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) -{ - lua_CFunction f = (lua_CFunction)dlsym(lib, sym); - if (f == NULL) lua_pushstring(L, dlerror()); - return f; -} - -static const char *ll_bcsym(void *lib, const char *sym) -{ -#if defined(RTLD_DEFAULT) && !defined(NO_RTLD_DEFAULT) - if (lib == NULL) lib = RTLD_DEFAULT; -#elif LJ_TARGET_OSX || LJ_TARGET_BSD - if (lib == NULL) lib = (void *)(intptr_t)-2; -#endif - return (const char *)dlsym(lib, sym); -} - -#elif LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS -#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 -#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 -BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); -#endif - -#if LJ_TARGET_UWP -void *LJ_WIN_LOADLIBA(const char *path) -{ - DWORD err = GetLastError(); - wchar_t wpath[256]; - HANDLE lib = NULL; - if (MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, 256) > 0) { - lib = LoadPackagedLibrary(wpath, 0); - } - SetLastError(err); - return lib; -} -#endif - -#undef setprogdir - -static void setprogdir(lua_State *L) -{ - char buff[MAX_PATH + 1]; - char *lb; - DWORD nsize = sizeof(buff); - DWORD n = GetModuleFileNameA(NULL, buff, nsize); - if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) { - luaL_error(L, "unable to get ModuleFileName"); - } else { - *lb = '\0'; - luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); - lua_remove(L, -2); /* remove original string */ - } -} - -static void pusherror(lua_State *L) -{ - DWORD error = GetLastError(); -#if LJ_TARGET_XBOXONE - wchar_t wbuffer[128]; - char buffer[128*2]; - if (FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, wbuffer, sizeof(wbuffer)/sizeof(wchar_t), NULL) && - WideCharToMultiByte(CP_ACP, 0, wbuffer, 128, buffer, 128*2, NULL, NULL)) -#else - char buffer[128]; - if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error, 0, buffer, sizeof(buffer), NULL)) -#endif - lua_pushstring(L, buffer); - else - lua_pushfstring(L, "system error %d\n", error); -} - -static void ll_unloadlib(void *lib) -{ - FreeLibrary((HINSTANCE)lib); -} - -static void *ll_load(lua_State *L, const char *path, int gl) -{ - HINSTANCE lib = LJ_WIN_LOADLIBA(path); - if (lib == NULL) pusherror(L); - UNUSED(gl); - return lib; -} - -static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) -{ - lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); - if (f == NULL) pusherror(L); - return f; -} - -#if LJ_TARGET_UWP -EXTERN_C IMAGE_DOS_HEADER __ImageBase; -#endif - -static const char *ll_bcsym(void *lib, const char *sym) -{ - if (lib) { - return (const char *)GetProcAddress((HINSTANCE)lib, sym); - } else { -#if LJ_TARGET_UWP - return (const char *)GetProcAddress((HINSTANCE)&__ImageBase, sym); -#else - HINSTANCE h = GetModuleHandleA(NULL); - const char *p = (const char *)GetProcAddress(h, sym); - if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (const char *)ll_bcsym, &h)) - p = (const char *)GetProcAddress(h, sym); - return p; -#endif - } -} - -#else - -#undef PACKAGE_LIB_FAIL -#define PACKAGE_LIB_FAIL "absent" - -#define DLMSG "dynamic libraries not enabled; no support for target OS" - -static void ll_unloadlib(void *lib) -{ - UNUSED(lib); -} - -static void *ll_load(lua_State *L, const char *path, int gl) -{ - UNUSED(path); UNUSED(gl); - lua_pushliteral(L, DLMSG); - return NULL; -} - -static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) -{ - UNUSED(lib); UNUSED(sym); - lua_pushliteral(L, DLMSG); - return NULL; -} - -static const char *ll_bcsym(void *lib, const char *sym) -{ - UNUSED(lib); UNUSED(sym); - return NULL; -} - -#endif - -/* ------------------------------------------------------------------------ */ - -static void **ll_register(lua_State *L, const char *path) -{ - void **plib; - lua_pushfstring(L, "LOADLIB: %s", path); - lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ - if (!lua_isnil(L, -1)) { /* is there an entry? */ - plib = (void **)lua_touserdata(L, -1); - } else { /* no entry yet; create one */ - lua_pop(L, 1); - plib = (void **)lua_newuserdata(L, sizeof(void *)); - *plib = NULL; - luaL_setmetatable(L, "_LOADLIB"); - lua_pushfstring(L, "LOADLIB: %s", path); - lua_pushvalue(L, -2); - lua_settable(L, LUA_REGISTRYINDEX); - } - return plib; -} - -static const char *mksymname(lua_State *L, const char *modname, - const char *prefix) -{ - const char *funcname; - const char *mark = strchr(modname, *LUA_IGMARK); - if (mark) modname = mark + 1; - funcname = luaL_gsub(L, modname, ".", "_"); - funcname = lua_pushfstring(L, prefix, funcname); - lua_remove(L, -2); /* remove 'gsub' result */ - return funcname; -} - -static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r) -{ - void **reg; - if (strlen(path) >= 4096) { - lua_pushliteral(L, "path too long"); - return PACKAGE_ERR_LIB; - } - reg = ll_register(L, path); - if (*reg == NULL) *reg = ll_load(L, path, (*name == '*')); - if (*reg == NULL) { - return PACKAGE_ERR_LIB; /* Unable to load library. */ - } else if (*name == '*') { /* Only load library into global namespace. */ - lua_pushboolean(L, 1); - return 0; - } else { - const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF); - lua_CFunction f = ll_sym(L, *reg, sym); - if (f) { - lua_pushcfunction(L, f); - return 0; - } - if (!r) { - const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC)); - lua_pop(L, 1); - if (bcdata) { - if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) - return PACKAGE_ERR_LOAD; - return 0; - } - } - return PACKAGE_ERR_FUNC; /* Unable to find function. */ - } -} - -static int lj_cf_package_loadlib(lua_State *L) -{ - const char *path = luaL_checkstring(L, 1); - const char *init = luaL_checkstring(L, 2); - int st = ll_loadfunc(L, path, init, 1); - if (st == 0) { /* no errors? */ - return 1; /* return the loaded function */ - } else { /* error; error message is on stack top */ - lua_pushnil(L); - lua_insert(L, -2); - lua_pushstring(L, (st == PACKAGE_ERR_LIB) ? PACKAGE_LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ - } -} - -static int lj_cf_package_unloadlib(lua_State *L) -{ - void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); - if (*lib) ll_unloadlib(*lib); - *lib = NULL; /* mark library as closed */ - return 0; -} - -/* ------------------------------------------------------------------------ */ - -static int readable(const char *filename) -{ - FILE *f = fopen(filename, "r"); /* try to open file */ - if (f == NULL) return 0; /* open failed */ - fclose(f); - return 1; -} - -static const char *pushnexttemplate(lua_State *L, const char *path) -{ - const char *l; - while (*path == *LUA_PATHSEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ - l = strchr(path, *LUA_PATHSEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, (size_t)(l - path)); /* template */ - return l; -} - -static const char *searchpath (lua_State *L, const char *name, - const char *path, const char *sep, - const char *dirsep) -{ - luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); - if (*sep != '\0') /* non-empty separator? */ - name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename = luaL_gsub(L, lua_tostring(L, -1), - LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ - if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file " LUA_QS, filename); - lua_remove(L, -2); /* remove file name */ - luaL_addvalue(&msg); /* concatenate error msg. entry */ - } - luaL_pushresult(&msg); /* create error message */ - return NULL; /* not found */ -} - -static int lj_cf_package_searchpath(lua_State *L) -{ - const char *f = searchpath(L, luaL_checkstring(L, 1), - luaL_checkstring(L, 2), - luaL_optstring(L, 3, "."), - luaL_optstring(L, 4, LUA_DIRSEP)); - if (f != NULL) { - return 1; - } else { /* error message is on top of the stack */ - lua_pushnil(L); - lua_insert(L, -2); - return 2; /* return nil + error message */ - } -} - -static const char *findfile(lua_State *L, const char *name, - const char *pname) -{ - const char *path; - lua_getfield(L, LUA_ENVIRONINDEX, pname); - path = lua_tostring(L, -1); - if (path == NULL) - luaL_error(L, LUA_QL("package.%s") " must be a string", pname); - return searchpath(L, name, path, ".", LUA_DIRSEP); -} - -static void loaderror(lua_State *L, const char *filename) -{ - luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", - lua_tostring(L, 1), filename, lua_tostring(L, -1)); -} - -static int lj_cf_package_loader_lua(lua_State *L) -{ - const char *filename; - const char *name = luaL_checkstring(L, 1); - filename = findfile(L, name, "path"); - if (filename == NULL) return 1; /* library not found in this path */ - if (luaL_loadfile(L, filename) != 0) - loaderror(L, filename); - return 1; /* library loaded successfully */ -} - -static int lj_cf_package_loader_c(lua_State *L) -{ - const char *name = luaL_checkstring(L, 1); - const char *filename = findfile(L, name, "cpath"); - if (filename == NULL) return 1; /* library not found in this path */ - if (ll_loadfunc(L, filename, name, 0) != 0) - loaderror(L, filename); - return 1; /* library loaded successfully */ -} - -static int lj_cf_package_loader_croot(lua_State *L) -{ - const char *filename; - const char *name = luaL_checkstring(L, 1); - const char *p = strchr(name, '.'); - int st; - if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, (size_t)(p - name)); - filename = findfile(L, lua_tostring(L, -1), "cpath"); - if (filename == NULL) return 1; /* root not found */ - if ((st = ll_loadfunc(L, filename, name, 0)) != 0) { - if (st != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */ - lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, - name, filename); - return 1; /* function not found */ - } - return 1; -} - -static int lj_cf_package_loader_preload(lua_State *L) -{ - const char *name = luaL_checkstring(L, 1); - lua_getfield(L, LUA_ENVIRONINDEX, "preload"); - if (!lua_istable(L, -1)) - luaL_error(L, LUA_QL("package.preload") " must be a table"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) { /* Not found? */ - const char *bcname = mksymname(L, name, SYMPREFIX_BC); - const char *bcdata = ll_bcsym(NULL, bcname); - if (bcdata == NULL || luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - } - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#define KEY_SENTINEL (U64x(80000000,00000000)|'s') - -static int lj_cf_package_require(lua_State *L) -{ - const char *name = luaL_checkstring(L, 1); - int i; - lua_settop(L, 1); /* _LOADED table will be at index 2 */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); - if (lua_toboolean(L, -1)) { /* is it there? */ - if ((L->top-1)->u64 == KEY_SENTINEL) /* check loops */ - luaL_error(L, "loop or previous error loading module " LUA_QS, name); - return 1; /* package is already loaded */ - } - /* else must load it; iterate over available loaders */ - lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); - if (!lua_istable(L, -1)) - luaL_error(L, LUA_QL("package.loaders") " must be a table"); - lua_pushliteral(L, ""); /* error message accumulator */ - for (i = 1; ; i++) { - lua_rawgeti(L, -2, i); /* get a loader */ - if (lua_isnil(L, -1)) - luaL_error(L, "module " LUA_QS " not found:%s", - name, lua_tostring(L, -2)); - lua_pushstring(L, name); - lua_call(L, 1, 1); /* call it */ - if (lua_isfunction(L, -1)) /* did it find module? */ - break; /* module loaded successfully */ - else if (lua_isstring(L, -1)) /* loader returned error message? */ - lua_concat(L, 2); /* accumulate it */ - else - lua_pop(L, 1); - } - (L->top++)->u64 = KEY_SENTINEL; - lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ - lua_pushstring(L, name); /* pass name as argument to module */ - lua_call(L, 1, 1); /* run loaded module */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ - lua_getfield(L, 2, name); - if ((L->top-1)->u64 == KEY_SENTINEL) { /* module did not set a value? */ - lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ - lua_setfield(L, 2, name); /* _LOADED[name] = true */ - } - lj_lib_checkfpu(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void setfenv(lua_State *L) -{ - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - luaL_error(L, LUA_QL("module") " not called from a Lua function"); - lua_pushvalue(L, -2); - lua_setfenv(L, -2); - lua_pop(L, 1); -} - -static void dooptions(lua_State *L, int n) -{ - int i; - for (i = 2; i <= n; i++) { - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } -} - -static void modinit(lua_State *L, const char *modname) -{ - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, (size_t)(dot - modname)); - lua_setfield(L, -2, "_PACKAGE"); -} - -static int lj_cf_package_module(lua_State *L) -{ - const char *modname = luaL_checkstring(L, 1); - int lastarg = (int)(L->top - L->base); - luaL_pushmodule(L, modname, 1); - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) { /* Module already initialized? */ - lua_pop(L, 1); - } else { - lua_pop(L, 1); - modinit(L, modname); - } - lua_pushvalue(L, -1); - setfenv(L); - dooptions(L, lastarg); - return LJ_52; -} - -static int lj_cf_package_seeall(lua_State *L) -{ - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushvalue(L, LUA_GLOBALSINDEX); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - -/* ------------------------------------------------------------------------ */ - -#define AUXMARK "\1" - -static void setpath(lua_State *L, const char *fieldname, const char *envname, - const char *def, int noenv) -{ -#if LJ_TARGET_CONSOLE - const char *path = NULL; - UNUSED(envname); -#else - const char *path = getenv(envname); -#endif - if (path == NULL || noenv) { - lua_pushstring(L, def); - } else { - path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, - LUA_PATHSEP AUXMARK LUA_PATHSEP); - luaL_gsub(L, path, AUXMARK, def); - lua_remove(L, -2); - } - setprogdir(L); - lua_setfield(L, -2, fieldname); -} - -static const luaL_Reg package_lib[] = { - { "loadlib", lj_cf_package_loadlib }, - { "searchpath", lj_cf_package_searchpath }, - { "seeall", lj_cf_package_seeall }, - { NULL, NULL } -}; - -static const luaL_Reg package_global[] = { - { "module", lj_cf_package_module }, - { "require", lj_cf_package_require }, - { NULL, NULL } -}; - -static const lua_CFunction package_loaders[] = -{ - lj_cf_package_loader_preload, - lj_cf_package_loader_lua, - lj_cf_package_loader_c, - lj_cf_package_loader_croot, - NULL -}; - -LUALIB_API int luaopen_package(lua_State *L) -{ - int i; - int noenv; - luaL_newmetatable(L, "_LOADLIB"); - lj_lib_pushcf(L, lj_cf_package_unloadlib, 1); - lua_setfield(L, -2, "__gc"); - luaL_register(L, LUA_LOADLIBNAME, package_lib); - lua_copy(L, -1, LUA_ENVIRONINDEX); - lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0); - for (i = 0; package_loaders[i] != NULL; i++) { - lj_lib_pushcf(L, package_loaders[i], 1); - lua_rawseti(L, -2, i+1); - } -#if LJ_52 - lua_pushvalue(L, -1); - lua_setfield(L, -3, "searchers"); -#endif - lua_setfield(L, -2, "loaders"); - lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - noenv = lua_toboolean(L, -1); - lua_pop(L, 1); - setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv); - setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv); - lua_pushliteral(L, LUA_PATH_CONFIG); - lua_setfield(L, -2, "config"); - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); - lua_setfield(L, -2, "loaded"); - luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); - lua_setfield(L, -2, "preload"); - lua_pushvalue(L, LUA_GLOBALSINDEX); - luaL_register(L, NULL, package_global); - lua_pop(L, 1); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_string.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_string.c deleted file mode 100644 index 79aeddf..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_string.c +++ /dev/null @@ -1,676 +0,0 @@ -/* -** String library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_string_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_ff.h" -#include "lj_bcdump.h" -#include "lj_char.h" -#include "lj_strfmt.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_string - -LJLIB_LUA(string_len) /* - function(s) - CHECK_str(s) - return #s - end -*/ - -LJLIB_ASM(string_byte) LJLIB_REC(string_range 0) -{ - GCstr *s = lj_lib_checkstr(L, 1); - int32_t len = (int32_t)s->len; - int32_t start = lj_lib_optint(L, 2, 1); - int32_t stop = lj_lib_optint(L, 3, start); - int32_t n, i; - const unsigned char *p; - if (stop < 0) stop += len+1; - if (start < 0) start += len+1; - if (start <= 0) start = 1; - if (stop > len) stop = len; - if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */ - start--; - n = stop - start; - if ((uint32_t)n > LUAI_MAXCSTACK) - lj_err_caller(L, LJ_ERR_STRSLC); - lj_state_checkstack(L, (MSize)n); - p = (const unsigned char *)strdata(s) + start; - for (i = 0; i < n; i++) - setintV(L->base + i-1-LJ_FR2, p[i]); - return FFH_RES(n); -} - -LJLIB_ASM(string_char) LJLIB_REC(.) -{ - int i, nargs = (int)(L->top - L->base); - char *buf = lj_buf_tmp(L, (MSize)nargs); - for (i = 1; i <= nargs; i++) { - int32_t k = lj_lib_checkint(L, i); - if (!checku8(k)) - lj_err_arg(L, i, LJ_ERR_BADVAL); - buf[i-1] = (char)k; - } - setstrV(L, L->base-1-LJ_FR2, lj_str_new(L, buf, (size_t)nargs)); - return FFH_RES(1); -} - -LJLIB_ASM(string_sub) LJLIB_REC(string_range 1) -{ - lj_lib_checkstr(L, 1); - lj_lib_checkint(L, 2); - setintV(L->base+2, lj_lib_optint(L, 3, -1)); - return FFH_RETRY; -} - -LJLIB_CF(string_rep) LJLIB_REC(.) -{ - GCstr *s = lj_lib_checkstr(L, 1); - int32_t rep = lj_lib_checkint(L, 2); - GCstr *sep = lj_lib_optstr(L, 3); - SBuf *sb = lj_buf_tmp_(L); - if (sep && rep > 1) { - GCstr *s2 = lj_buf_cat2str(L, sep, s); - lj_buf_reset(sb); - lj_buf_putstr(sb, s); - s = s2; - rep--; - } - sb = lj_buf_putstr_rep(sb, s, rep); - setstrV(L, L->top-1, lj_buf_str(L, sb)); - lj_gc_check(L); - return 1; -} - -LJLIB_ASM(string_reverse) LJLIB_REC(string_op IRCALL_lj_buf_putstr_reverse) -{ - lj_lib_checkstr(L, 1); - return FFH_RETRY; -} -LJLIB_ASM_(string_lower) LJLIB_REC(string_op IRCALL_lj_buf_putstr_lower) -LJLIB_ASM_(string_upper) LJLIB_REC(string_op IRCALL_lj_buf_putstr_upper) - -/* ------------------------------------------------------------------------ */ - -static int writer_buf(lua_State *L, const void *p, size_t size, void *sb) -{ - lj_buf_putmem((SBuf *)sb, p, (MSize)size); - UNUSED(L); - return 0; -} - -LJLIB_CF(string_dump) -{ - GCfunc *fn = lj_lib_checkfunc(L, 1); - int strip = L->base+1 < L->top && tvistruecond(L->base+1); - SBuf *sb = lj_buf_tmp_(L); /* Assumes lj_bcwrite() doesn't use tmpbuf. */ - L->top = L->base+1; - if (!isluafunc(fn) || lj_bcwrite(L, funcproto(fn), writer_buf, sb, strip)) - lj_err_caller(L, LJ_ERR_STRDUMP); - setstrV(L, L->top-1, lj_buf_str(L, sb)); - lj_gc_check(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -/* macro to `unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - -#define CAP_UNFINISHED (-1) -#define CAP_POSITION (-2) - -typedef struct MatchState { - const char *src_init; /* init of source string */ - const char *src_end; /* end (`\0') of source string */ - lua_State *L; - int level; /* total number of captures (finished or unfinished) */ - int depth; - struct { - const char *init; - ptrdiff_t len; - } capture[LUA_MAXCAPTURES]; -} MatchState; - -#define L_ESC '%' - -static int check_capture(MatchState *ms, int l) -{ - l -= '1'; - if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) - lj_err_caller(ms->L, LJ_ERR_STRCAPI); - return l; -} - -static int capture_to_close(MatchState *ms) -{ - int level = ms->level; - for (level--; level>=0; level--) - if (ms->capture[level].len == CAP_UNFINISHED) return level; - lj_err_caller(ms->L, LJ_ERR_STRPATC); - return 0; /* unreachable */ -} - -static const char *classend(MatchState *ms, const char *p) -{ - switch (*p++) { - case L_ESC: - if (*p == '\0') - lj_err_caller(ms->L, LJ_ERR_STRPATE); - return p+1; - case '[': - if (*p == '^') p++; - do { /* look for a `]' */ - if (*p == '\0') - lj_err_caller(ms->L, LJ_ERR_STRPATM); - if (*(p++) == L_ESC && *p != '\0') - p++; /* skip escapes (e.g. `%]') */ - } while (*p != ']'); - return p+1; - default: - return p; - } -} - -static const unsigned char match_class_map[32] = { - 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0, - LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0, - LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0 -}; - -static int match_class(int c, int cl) -{ - if ((cl & 0xc0) == 0x40) { - int t = match_class_map[(cl&0x1f)]; - if (t) { - t = lj_char_isa(c, t); - return (cl & 0x20) ? t : !t; - } - if (cl == 'z') return c == 0; - if (cl == 'Z') return c != 0; - } - return (cl == c); -} - -static int matchbracketclass(int c, const char *p, const char *ec) -{ - int sig = 1; - if (*(p+1) == '^') { - sig = 0; - p++; /* skip the `^' */ - } - while (++p < ec) { - if (*p == L_ESC) { - p++; - if (match_class(c, uchar(*p))) - return sig; - } - else if ((*(p+1) == '-') && (p+2 < ec)) { - p+=2; - if (uchar(*(p-2)) <= c && c <= uchar(*p)) - return sig; - } - else if (uchar(*p) == c) return sig; - } - return !sig; -} - -static int singlematch(int c, const char *p, const char *ep) -{ - switch (*p) { - case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); - case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); - } -} - -static const char *match(MatchState *ms, const char *s, const char *p); - -static const char *matchbalance(MatchState *ms, const char *s, const char *p) -{ - if (*p == 0 || *(p+1) == 0) - lj_err_caller(ms->L, LJ_ERR_STRPATU); - if (*s != *p) { - return NULL; - } else { - int b = *p; - int e = *(p+1); - int cont = 1; - while (++s < ms->src_end) { - if (*s == e) { - if (--cont == 0) return s+1; - } else if (*s == b) { - cont++; - } - } - } - return NULL; /* string ends out of balance */ -} - -static const char *max_expand(MatchState *ms, const char *s, - const char *p, const char *ep) -{ - ptrdiff_t i = 0; /* counts maximum expand for item */ - while ((s+i)src_end && singlematch(uchar(*(s+i)), p, ep)) - i++; - /* keeps trying to match with the maximum repetitions */ - while (i>=0) { - const char *res = match(ms, (s+i), ep+1); - if (res) return res; - i--; /* else didn't match; reduce 1 repetition to try again */ - } - return NULL; -} - -static const char *min_expand(MatchState *ms, const char *s, - const char *p, const char *ep) -{ - for (;;) { - const char *res = match(ms, s, ep+1); - if (res != NULL) - return res; - else if (ssrc_end && singlematch(uchar(*s), p, ep)) - s++; /* try with one more repetition */ - else - return NULL; - } -} - -static const char *start_capture(MatchState *ms, const char *s, - const char *p, int what) -{ - const char *res; - int level = ms->level; - if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN); - ms->capture[level].init = s; - ms->capture[level].len = what; - ms->level = level+1; - if ((res=match(ms, s, p)) == NULL) /* match failed? */ - ms->level--; /* undo capture */ - return res; -} - -static const char *end_capture(MatchState *ms, const char *s, - const char *p) -{ - int l = capture_to_close(ms); - const char *res; - ms->capture[l].len = s - ms->capture[l].init; /* close capture */ - if ((res = match(ms, s, p)) == NULL) /* match failed? */ - ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ - return res; -} - -static const char *match_capture(MatchState *ms, const char *s, int l) -{ - size_t len; - l = check_capture(ms, l); - len = (size_t)ms->capture[l].len; - if ((size_t)(ms->src_end-s) >= len && - memcmp(ms->capture[l].init, s, len) == 0) - return s+len; - else - return NULL; -} - -static const char *match(MatchState *ms, const char *s, const char *p) -{ - if (++ms->depth > LJ_MAX_XLEVEL) - lj_err_caller(ms->L, LJ_ERR_STRPATX); - init: /* using goto's to optimize tail recursion */ - switch (*p) { - case '(': /* start capture */ - if (*(p+1) == ')') /* position capture? */ - s = start_capture(ms, s, p+2, CAP_POSITION); - else - s = start_capture(ms, s, p+1, CAP_UNFINISHED); - break; - case ')': /* end capture */ - s = end_capture(ms, s, p+1); - break; - case L_ESC: - switch (*(p+1)) { - case 'b': /* balanced string? */ - s = matchbalance(ms, s, p+2); - if (s == NULL) break; - p+=4; - goto init; /* else s = match(ms, s, p+4); */ - case 'f': { /* frontier? */ - const char *ep; char previous; - p += 2; - if (*p != '[') - lj_err_caller(ms->L, LJ_ERR_STRPATB); - ep = classend(ms, p); /* points to what is next */ - previous = (s == ms->src_init) ? '\0' : *(s-1); - if (matchbracketclass(uchar(previous), p, ep-1) || - !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; } - p=ep; - goto init; /* else s = match(ms, s, ep); */ - } - default: - if (lj_char_isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p+1))); - if (s == NULL) break; - p+=2; - goto init; /* else s = match(ms, s, p+2) */ - } - goto dflt; /* case default */ - } - break; - case '\0': /* end of pattern */ - break; /* match succeeded */ - case '$': - /* is the `$' the last char in pattern? */ - if (*(p+1) != '\0') goto dflt; - if (s != ms->src_end) s = NULL; /* check end of string */ - break; - default: dflt: { /* it is a pattern item */ - const char *ep = classend(ms, p); /* points to what is next */ - int m = ssrc_end && singlematch(uchar(*s), p, ep); - switch (*ep) { - case '?': { /* optional */ - const char *res; - if (m && ((res=match(ms, s+1, ep+1)) != NULL)) { - s = res; - break; - } - p=ep+1; - goto init; /* else s = match(ms, s, ep+1); */ - } - case '*': /* 0 or more repetitions */ - s = max_expand(ms, s, p, ep); - break; - case '+': /* 1 or more repetitions */ - s = (m ? max_expand(ms, s+1, p, ep) : NULL); - break; - case '-': /* 0 or more repetitions (minimum) */ - s = min_expand(ms, s, p, ep); - break; - default: - if (m) { s++; p=ep; goto init; } /* else s = match(ms, s+1, ep); */ - s = NULL; - break; - } - break; - } - } - ms->depth--; - return s; -} - -static void push_onecapture(MatchState *ms, int i, const char *s, const char *e) -{ - if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, (size_t)(e - s)); /* add whole match */ - else - lj_err_caller(ms->L, LJ_ERR_STRCAPI); - } else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU); - if (l == CAP_POSITION) - lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l); - } -} - -static int push_captures(MatchState *ms, const char *s, const char *e) -{ - int i; - int nlevels = (ms->level == 0 && s) ? 1 : ms->level; - luaL_checkstack(ms->L, nlevels, "too many captures"); - for (i = 0; i < nlevels; i++) - push_onecapture(ms, i, s, e); - return nlevels; /* number of strings pushed */ -} - -static int str_find_aux(lua_State *L, int find) -{ - GCstr *s = lj_lib_checkstr(L, 1); - GCstr *p = lj_lib_checkstr(L, 2); - int32_t start = lj_lib_optint(L, 3, 1); - MSize st; - if (start < 0) start += (int32_t)s->len; else start--; - if (start < 0) start = 0; - st = (MSize)start; - if (st > s->len) { -#if LJ_52 - setnilV(L->top-1); - return 1; -#else - st = s->len; -#endif - } - if (find && ((L->base+3 < L->top && tvistruecond(L->base+3)) || - !lj_str_haspattern(p))) { /* Search for fixed string. */ - const char *q = lj_str_find(strdata(s)+st, strdata(p), s->len-st, p->len); - if (q) { - setintV(L->top-2, (int32_t)(q-strdata(s)) + 1); - setintV(L->top-1, (int32_t)(q-strdata(s)) + (int32_t)p->len); - return 2; - } - } else { /* Search for pattern. */ - MatchState ms; - const char *pstr = strdata(p); - const char *sstr = strdata(s) + st; - int anchor = 0; - if (*pstr == '^') { pstr++; anchor = 1; } - ms.L = L; - ms.src_init = strdata(s); - ms.src_end = strdata(s) + s->len; - do { /* Loop through string and try to match the pattern. */ - const char *q; - ms.level = ms.depth = 0; - q = match(&ms, sstr, pstr); - if (q) { - if (find) { - setintV(L->top++, (int32_t)(sstr-(strdata(s)-1))); - setintV(L->top++, (int32_t)(q-strdata(s))); - return push_captures(&ms, NULL, NULL) + 2; - } else { - return push_captures(&ms, sstr, q); - } - } - } while (sstr++ < ms.src_end && !anchor); - } - setnilV(L->top-1); /* Not found. */ - return 1; -} - -LJLIB_CF(string_find) LJLIB_REC(.) -{ - return str_find_aux(L, 1); -} - -LJLIB_CF(string_match) -{ - return str_find_aux(L, 0); -} - -LJLIB_NOREG LJLIB_CF(string_gmatch_aux) -{ - const char *p = strVdata(lj_lib_upvalue(L, 2)); - GCstr *str = strV(lj_lib_upvalue(L, 1)); - const char *s = strdata(str); - TValue *tvpos = lj_lib_upvalue(L, 3); - const char *src = s + tvpos->u32.lo; - MatchState ms; - ms.L = L; - ms.src_init = s; - ms.src_end = s + str->len; - for (; src <= ms.src_end; src++) { - const char *e; - ms.level = ms.depth = 0; - if ((e = match(&ms, src, p)) != NULL) { - int32_t pos = (int32_t)(e - s); - if (e == src) pos++; /* Ensure progress for empty match. */ - tvpos->u32.lo = (uint32_t)pos; - return push_captures(&ms, src, e); - } - } - return 0; /* not found */ -} - -LJLIB_CF(string_gmatch) -{ - lj_lib_checkstr(L, 1); - lj_lib_checkstr(L, 2); - L->top = L->base+3; - (L->top-1)->u64 = 0; - lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3); - return 1; -} - -static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e) -{ - size_t l, i; - const char *news = lua_tolstring(ms->L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) { - luaL_addchar(b, news[i]); - } else { - i++; /* skip ESC */ - if (!lj_char_isdigit(uchar(news[i]))) { - luaL_addchar(b, news[i]); - } else if (news[i] == '0') { - luaL_addlstring(b, s, (size_t)(e - s)); - } else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_addvalue(b); /* add capture to accumulated result */ - } - } - } -} - -static void add_value(MatchState *ms, luaL_Buffer *b, - const char *s, const char *e) -{ - lua_State *L = ms->L; - switch (lua_type(L, 3)) { - case LUA_TNUMBER: - case LUA_TSTRING: { - add_s(ms, b, s, e); - return; - } - case LUA_TFUNCTION: { - int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); - break; - } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); - lua_gettable(L, 3); - break; - } - } - if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, (size_t)(e - s)); /* keep original text */ - } else if (!lua_isstring(L, -1)) { - lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1)); - } - luaL_addvalue(b); /* add result to accumulator */ -} - -LJLIB_CF(string_gsub) -{ - size_t srcl; - const char *src = luaL_checklstring(L, 1, &srcl); - const char *p = luaL_checkstring(L, 2); - int tr = lua_type(L, 3); - int max_s = luaL_optint(L, 4, (int)(srcl+1)); - int anchor = (*p == '^') ? (p++, 1) : 0; - int n = 0; - MatchState ms; - luaL_Buffer b; - if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING || - tr == LUA_TFUNCTION || tr == LUA_TTABLE)) - lj_err_arg(L, 3, LJ_ERR_NOSFT); - luaL_buffinit(L, &b); - ms.L = L; - ms.src_init = src; - ms.src_end = src+srcl; - while (n < max_s) { - const char *e; - ms.level = ms.depth = 0; - e = match(&ms, src, p); - if (e) { - n++; - add_value(&ms, &b, src, e); - } - if (e && e>src) /* non empty match? */ - src = e; /* skip it */ - else if (src < ms.src_end) - luaL_addchar(&b, *src++); - else - break; - if (anchor) - break; - } - luaL_addlstring(&b, src, (size_t)(ms.src_end-src)); - luaL_pushresult(&b); - lua_pushinteger(L, n); /* number of substitutions */ - return 2; -} - -/* ------------------------------------------------------------------------ */ - -LJLIB_CF(string_format) LJLIB_REC(.) -{ - int retry = 0; - SBuf *sb; - do { - sb = lj_buf_tmp_(L); - retry = lj_strfmt_putarg(L, sb, 1, -retry); - } while (retry > 0); - setstrV(L, L->top-1, lj_buf_str(L, sb)); - lj_gc_check(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_string(lua_State *L) -{ - GCtab *mt; - global_State *g; - LJ_LIB_REG(L, LUA_STRLIBNAME, string); - mt = lj_tab_new(L, 0, 1); - /* NOBARRIER: basemt is a GC root. */ - g = G(L); - setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt)); - settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1)); - mt->nomm = (uint8_t)(~(1u<top-1)); -#endif - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_table.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_table.c deleted file mode 100644 index a723326..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lib_table.c +++ /dev/null @@ -1,327 +0,0 @@ -/* -** Table library. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lib_table_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_tab.h" -#include "lj_ff.h" -#include "lj_lib.h" - -/* ------------------------------------------------------------------------ */ - -#define LJLIB_MODULE_table - -LJLIB_LUA(table_foreachi) /* - function(t, f) - CHECK_tab(t) - CHECK_func(f) - for i=1,#t do - local r = f(i, t[i]) - if r ~= nil then return r end - end - end -*/ - -LJLIB_LUA(table_foreach) /* - function(t, f) - CHECK_tab(t) - CHECK_func(f) - for k, v in PAIRS(t) do - local r = f(k, v) - if r ~= nil then return r end - end - end -*/ - -LJLIB_LUA(table_getn) /* - function(t) - CHECK_tab(t) - return #t - end -*/ - -LJLIB_CF(table_maxn) -{ - GCtab *t = lj_lib_checktab(L, 1); - TValue *array = tvref(t->array); - Node *node; - lua_Number m = 0; - ptrdiff_t i; - for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--) - if (!tvisnil(&array[i])) { - m = (lua_Number)(int32_t)i; - break; - } - node = noderef(t->node); - for (i = (ptrdiff_t)t->hmask; i >= 0; i--) - if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) { - lua_Number n = numberVnum(&node[i].key); - if (n > m) m = n; - } - setnumV(L->top-1, m); - return 1; -} - -LJLIB_CF(table_insert) LJLIB_REC(.) -{ - GCtab *t = lj_lib_checktab(L, 1); - int32_t n, i = (int32_t)lj_tab_len(t) + 1; - int nargs = (int)((char *)L->top - (char *)L->base); - if (nargs != 2*sizeof(TValue)) { - if (nargs != 3*sizeof(TValue)) - lj_err_caller(L, LJ_ERR_TABINS); - /* NOBARRIER: This just moves existing elements around. */ - for (n = lj_lib_checkint(L, 2); i > n; i--) { - /* The set may invalidate the get pointer, so need to do it first! */ - TValue *dst = lj_tab_setint(L, t, i); - cTValue *src = lj_tab_getint(t, i-1); - if (src) { - copyTV(L, dst, src); - } else { - setnilV(dst); - } - } - i = n; - } - { - TValue *dst = lj_tab_setint(L, t, i); - copyTV(L, dst, L->top-1); /* Set new value. */ - lj_gc_barriert(L, t, dst); - } - return 0; -} - -LJLIB_LUA(table_remove) /* - function(t, pos) - CHECK_tab(t) - local len = #t - if pos == nil then - if len ~= 0 then - local old = t[len] - t[len] = nil - return old - end - else - CHECK_int(pos) - if pos >= 1 and pos <= len then - local old = t[pos] - for i=pos+1,len do - t[i-1] = t[i] - end - t[len] = nil - return old - end - end - end -*/ - -LJLIB_LUA(table_move) /* - function(a1, f, e, t, a2) - CHECK_tab(a1) - CHECK_int(f) - CHECK_int(e) - CHECK_int(t) - if a2 == nil then a2 = a1 end - CHECK_tab(a2) - if e >= f then - local d = t - f - if t > e or t <= f or a2 ~= a1 then - for i=f,e do a2[i+d] = a1[i] end - else - for i=e,f,-1 do a2[i+d] = a1[i] end - end - end - return a2 - end -*/ - -LJLIB_CF(table_concat) LJLIB_REC(.) -{ - GCtab *t = lj_lib_checktab(L, 1); - GCstr *sep = lj_lib_optstr(L, 2); - int32_t i = lj_lib_optint(L, 3, 1); - int32_t e = (L->base+3 < L->top && !tvisnil(L->base+3)) ? - lj_lib_checkint(L, 4) : (int32_t)lj_tab_len(t); - SBuf *sb = lj_buf_tmp_(L); - SBuf *sbx = lj_buf_puttab(sb, t, sep, i, e); - if (LJ_UNLIKELY(!sbx)) { /* Error: bad element type. */ - int32_t idx = (int32_t)(intptr_t)sb->w; - cTValue *o = lj_tab_getint(t, idx); - lj_err_callerv(L, LJ_ERR_TABCAT, - lj_obj_itypename[o ? itypemap(o) : ~LJ_TNIL], idx); - } - setstrV(L, L->top-1, lj_buf_str(L, sbx)); - lj_gc_check(L); - return 1; -} - -/* ------------------------------------------------------------------------ */ - -static void set2(lua_State *L, int i, int j) -{ - lua_rawseti(L, 1, i); - lua_rawseti(L, 1, j); -} - -static int sort_comp(lua_State *L, int a, int b) -{ - if (!lua_isnil(L, 2)) { /* function? */ - int res; - lua_pushvalue(L, 2); - lua_pushvalue(L, a-1); /* -1 to compensate function */ - lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ - lua_call(L, 2, 1); - res = lua_toboolean(L, -1); - lua_pop(L, 1); - return res; - } else { /* a < b? */ - return lua_lessthan(L, a, b); - } -} - -static void auxsort(lua_State *L, int l, int u) -{ - while (l < u) { /* for tail recursion */ - int i, j; - /* sort elements a[l], a[(l+u)/2] and a[u] */ - lua_rawgeti(L, 1, l); - lua_rawgeti(L, 1, u); - if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ - set2(L, l, u); /* swap a[l] - a[u] */ - else - lua_pop(L, 2); - if (u-l == 1) break; /* only 2 elements */ - i = (l+u)/2; - lua_rawgeti(L, 1, i); - lua_rawgeti(L, 1, l); - if (sort_comp(L, -2, -1)) { /* a[i]= P */ - while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i>=u) lj_err_caller(L, LJ_ERR_TABSORT); - lua_pop(L, 1); /* remove a[i] */ - } - /* repeat --j until a[j] <= P */ - while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j<=l) lj_err_caller(L, LJ_ERR_TABSORT); - lua_pop(L, 1); /* remove a[j] */ - } - if (jbase+1)) - lj_lib_checkfunc(L, 2); - auxsort(L, 1, n); - return 0; -} - -#if LJ_52 -LJLIB_PUSH("n") -LJLIB_CF(table_pack) -{ - TValue *array, *base = L->base; - MSize i, n = (uint32_t)(L->top - base); - GCtab *t = lj_tab_new(L, n ? n+1 : 0, 1); - /* NOBARRIER: The table is new (marked white). */ - setintV(lj_tab_setstr(L, t, strV(lj_lib_upvalue(L, 1))), (int32_t)n); - for (array = tvref(t->array) + 1, i = 0; i < n; i++) - copyTV(L, &array[i], &base[i]); - settabV(L, base, t); - L->top = base+1; - lj_gc_check(L); - return 1; -} -#endif - -LJLIB_NOREG LJLIB_CF(table_new) LJLIB_REC(.) -{ - int32_t a = lj_lib_checkint(L, 1); - int32_t h = lj_lib_checkint(L, 2); - lua_createtable(L, a, h); - return 1; -} - -LJLIB_NOREG LJLIB_CF(table_clear) LJLIB_REC(.) -{ - lj_tab_clear(lj_lib_checktab(L, 1)); - return 0; -} - -static int luaopen_table_new(lua_State *L) -{ - return lj_lib_postreg(L, lj_cf_table_new, FF_table_new, "new"); -} - -static int luaopen_table_clear(lua_State *L) -{ - return lj_lib_postreg(L, lj_cf_table_clear, FF_table_clear, "clear"); -} - -/* ------------------------------------------------------------------------ */ - -#include "lj_libdef.h" - -LUALIB_API int luaopen_table(lua_State *L) -{ - LJ_LIB_REG(L, LUA_TABLIBNAME, table); -#if LJ_52 - lua_getglobal(L, "unpack"); - lua_setfield(L, -2, "unpack"); -#endif - lj_lib_prereg(L, LUA_TABLIBNAME ".new", luaopen_table_new, tabV(L->top-1)); - lj_lib_prereg(L, LUA_TABLIBNAME ".clear", luaopen_table_clear, tabV(L->top-1)); - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.c deleted file mode 100644 index 20e6049..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.c +++ /dev/null @@ -1,1485 +0,0 @@ -/* -** Bundled memory allocator. -** -** Beware: this is a HEAVILY CUSTOMIZED version of dlmalloc. -** The original bears the following remark: -** -** This is a version (aka dlmalloc) of malloc/free/realloc written by -** Doug Lea and released to the public domain, as explained at -** https://creativecommons.org/licenses/publicdomain. -** -** * Version pre-2.8.4 Wed Mar 29 19:46:29 2006 (dl at gee) -** -** No additional copyright is claimed over the customizations. -** Please do NOT bother the original author about this version here! -** -** If you want to use dlmalloc in another project, you should get -** the original from: ftp://gee.cs.oswego.edu/pub/misc/ -** For thread-safe derivatives, take a look at: -** - ptmalloc: https://www.malloc.de/ -** - nedmalloc: https://www.nedprod.com/programs/portable/nedmalloc/ -*/ - -#define lj_alloc_c -#define LUA_CORE - -/* To get the mremap prototype. Must be defined before any system includes. */ -#if defined(__linux__) && !defined(_GNU_SOURCE) -#define _GNU_SOURCE -#endif - -#include "lj_def.h" -#include "lj_arch.h" -#include "lj_alloc.h" -#include "lj_prng.h" - -#ifndef LUAJIT_USE_SYSMALLOC - -#define MAX_SIZE_T (~(size_t)0) -#define MALLOC_ALIGNMENT ((size_t)8U) - -#define DEFAULT_GRANULARITY ((size_t)128U * (size_t)1024U) -#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) -#define DEFAULT_MMAP_THRESHOLD ((size_t)128U * (size_t)1024U) -#define MAX_RELEASE_CHECK_RATE 255 - -/* ------------------- size_t and alignment properties -------------------- */ - -/* The byte and bit size of a size_t */ -#define SIZE_T_SIZE (sizeof(size_t)) -#define SIZE_T_BITSIZE (sizeof(size_t) << 3) - -/* Some constants coerced to size_t */ -/* Annoying but necessary to avoid errors on some platforms */ -#define SIZE_T_ZERO ((size_t)0) -#define SIZE_T_ONE ((size_t)1) -#define SIZE_T_TWO ((size_t)2) -#define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) -#define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) -#define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) - -/* The bit mask value corresponding to MALLOC_ALIGNMENT */ -#define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) - -/* the number of bytes to offset an address to align it */ -#define align_offset(A)\ - ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ - ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) - -/* -------------------------- MMAP support ------------------------------- */ - -#define MFAIL ((void *)(MAX_SIZE_T)) -#define CMFAIL ((char *)(MFAIL)) /* defined for convenience */ - -#define IS_DIRECT_BIT (SIZE_T_ONE) - - -/* Determine system-specific block allocation method. */ -#if LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#define LJ_ALLOC_VIRTUALALLOC 1 - -#if LJ_64 && !LJ_GC64 -#define LJ_ALLOC_NTAVM 1 -#endif - -#else - -#include -/* If this include fails, then rebuild with: -DLUAJIT_USE_SYSMALLOC */ -#include - -#define LJ_ALLOC_MMAP 1 - -#if LJ_64 - -#define LJ_ALLOC_MMAP_PROBE 1 - -#if LJ_GC64 -#define LJ_ALLOC_MBITS 47 /* 128 TB in LJ_GC64 mode. */ -#elif LJ_TARGET_X64 && LJ_HASJIT -/* Due to limitations in the x64 compiler backend. */ -#define LJ_ALLOC_MBITS 31 /* 2 GB on x64 with !LJ_GC64. */ -#else -#define LJ_ALLOC_MBITS 32 /* 4 GB on other archs with !LJ_GC64. */ -#endif - -#endif - -#if LJ_64 && !LJ_GC64 && defined(MAP_32BIT) -#define LJ_ALLOC_MMAP32 1 -#endif - -#if LJ_TARGET_LINUX -#define LJ_ALLOC_MREMAP 1 -#endif - -#endif - - -#if LJ_ALLOC_VIRTUALALLOC - -#if LJ_ALLOC_NTAVM -/* Undocumented, but hey, that's what we all love so much about Windows. */ -typedef long (*PNTAVM)(HANDLE handle, void **addr, ULONG_PTR zbits, - size_t *size, ULONG alloctype, ULONG prot); -static PNTAVM ntavm; - -/* Number of top bits of the lower 32 bits of an address that must be zero. -** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB. -*/ -#define NTAVM_ZEROBITS 1 - -static void init_mmap(void) -{ - ntavm = (PNTAVM)GetProcAddress(GetModuleHandleA("ntdll.dll"), - "NtAllocateVirtualMemory"); -} -#define INIT_MMAP() init_mmap() - -/* Win64 32 bit MMAP via NtAllocateVirtualMemory. */ -static void *mmap_plain(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = NULL; - long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size, - MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - SetLastError(olderr); - return st == 0 ? ptr : MFAIL; -} - -/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ -static void *direct_mmap(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = NULL; - long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size, - MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE); - SetLastError(olderr); - return st == 0 ? ptr : MFAIL; -} - -#else - -/* Win32 MMAP via VirtualAlloc */ -static void *mmap_plain(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = LJ_WIN_VALLOC(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - SetLastError(olderr); - return ptr ? ptr : MFAIL; -} - -/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ -static void *direct_mmap(size_t size) -{ - DWORD olderr = GetLastError(); - void *ptr = LJ_WIN_VALLOC(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, - PAGE_READWRITE); - SetLastError(olderr); - return ptr ? ptr : MFAIL; -} - -#endif - -#define CALL_MMAP(prng, size) mmap_plain(size) -#define DIRECT_MMAP(prng, size) direct_mmap(size) - -/* This function supports releasing coalesed segments */ -static int CALL_MUNMAP(void *ptr, size_t size) -{ - DWORD olderr = GetLastError(); - MEMORY_BASIC_INFORMATION minfo; - char *cptr = (char *)ptr; - while (size) { - if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) - return -1; - if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || - minfo.State != MEM_COMMIT || minfo.RegionSize > size) - return -1; - if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) - return -1; - cptr += minfo.RegionSize; - size -= minfo.RegionSize; - } - SetLastError(olderr); - return 0; -} - -#elif LJ_ALLOC_MMAP - -#define MMAP_PROT (PROT_READ|PROT_WRITE) -#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) -#define MAP_ANONYMOUS MAP_ANON -#endif -#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS) - -#if LJ_ALLOC_MMAP_PROBE - -#ifdef MAP_TRYFIXED -#define MMAP_FLAGS_PROBE (MMAP_FLAGS|MAP_TRYFIXED) -#else -#define MMAP_FLAGS_PROBE MMAP_FLAGS -#endif - -#define LJ_ALLOC_MMAP_PROBE_MAX 30 -#define LJ_ALLOC_MMAP_PROBE_LINEAR 5 - -#define LJ_ALLOC_MMAP_PROBE_LOWER ((uintptr_t)0x4000) - -static void *mmap_probe(PRNGState *rs, size_t size) -{ - /* Hint for next allocation. Doesn't need to be thread-safe. */ - static uintptr_t hint_addr = 0; - int olderr = errno; - int retry; - for (retry = 0; retry < LJ_ALLOC_MMAP_PROBE_MAX; retry++) { - void *p = mmap((void *)hint_addr, size, MMAP_PROT, MMAP_FLAGS_PROBE, -1, 0); - uintptr_t addr = (uintptr_t)p; - if ((addr >> LJ_ALLOC_MBITS) == 0 && addr >= LJ_ALLOC_MMAP_PROBE_LOWER && - ((addr + size) >> LJ_ALLOC_MBITS) == 0) { - /* We got a suitable address. Bump the hint address. */ - hint_addr = addr + size; - errno = olderr; - return p; - } - if (p != MFAIL) { - munmap(p, size); - } else if (errno == ENOMEM) { - return MFAIL; - } - if (hint_addr) { - /* First, try linear probing. */ - if (retry < LJ_ALLOC_MMAP_PROBE_LINEAR) { - hint_addr += 0x1000000; - if (((hint_addr + size) >> LJ_ALLOC_MBITS) != 0) - hint_addr = 0; - continue; - } else if (retry == LJ_ALLOC_MMAP_PROBE_LINEAR) { - /* Next, try a no-hint probe to get back an ASLR address. */ - hint_addr = 0; - continue; - } - } - /* Finally, try pseudo-random probing. */ - do { - hint_addr = lj_prng_u64(rs) & (((uintptr_t)1< - -static void init_mmap(void) -{ - struct rlimit rlim; - rlim.rlim_cur = rlim.rlim_max = 0x10000; - setrlimit(RLIMIT_DATA, &rlim); /* Ignore result. May fail later. */ -} -#define INIT_MMAP() init_mmap() - -#endif - -static int CALL_MUNMAP(void *ptr, size_t size) -{ - int olderr = errno; - int ret = munmap(ptr, size); - errno = olderr; - return ret; -} - -#if LJ_ALLOC_MREMAP -/* Need to define _GNU_SOURCE to get the mremap prototype. */ -static void *CALL_MREMAP_(void *ptr, size_t osz, size_t nsz, int flags) -{ - int olderr = errno; - ptr = mremap(ptr, osz, nsz, flags); - errno = olderr; - return ptr; -} - -#define CALL_MREMAP(addr, osz, nsz, mv) CALL_MREMAP_((addr), (osz), (nsz), (mv)) -#define CALL_MREMAP_NOMOVE 0 -#define CALL_MREMAP_MAYMOVE 1 -#if LJ_64 && (!LJ_GC64 || LJ_TARGET_ARM64) -#define CALL_MREMAP_MV CALL_MREMAP_NOMOVE -#else -#define CALL_MREMAP_MV CALL_MREMAP_MAYMOVE -#endif -#endif - -#endif - - -#ifndef INIT_MMAP -#define INIT_MMAP() ((void)0) -#endif - -#ifndef DIRECT_MMAP -#define DIRECT_MMAP(prng, s) CALL_MMAP(prng, s) -#endif - -#ifndef CALL_MREMAP -#define CALL_MREMAP(addr, osz, nsz, mv) ((void)osz, MFAIL) -#endif - -/* ----------------------- Chunk representations ------------------------ */ - -struct malloc_chunk { - size_t prev_foot; /* Size of previous chunk (if free). */ - size_t head; /* Size and inuse bits. */ - struct malloc_chunk *fd; /* double links -- used only if free. */ - struct malloc_chunk *bk; -}; - -typedef struct malloc_chunk mchunk; -typedef struct malloc_chunk *mchunkptr; -typedef struct malloc_chunk *sbinptr; /* The type of bins of chunks */ -typedef size_t bindex_t; /* Described below */ -typedef unsigned int binmap_t; /* Described below */ -typedef unsigned int flag_t; /* The type of various bit flag sets */ - -/* ------------------- Chunks sizes and alignments ----------------------- */ - -#define MCHUNK_SIZE (sizeof(mchunk)) - -#define CHUNK_OVERHEAD (SIZE_T_SIZE) - -/* Direct chunks need a second word of overhead ... */ -#define DIRECT_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) -/* ... and additional padding for fake next-chunk at foot */ -#define DIRECT_FOOT_PAD (FOUR_SIZE_T_SIZES) - -/* The smallest size we can malloc is an aligned minimal chunk */ -#define MIN_CHUNK_SIZE\ - ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) - -/* conversion from malloc headers to user pointers, and back */ -#define chunk2mem(p) ((void *)((char *)(p) + TWO_SIZE_T_SIZES)) -#define mem2chunk(mem) ((mchunkptr)((char *)(mem) - TWO_SIZE_T_SIZES)) -/* chunk associated with aligned address A */ -#define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A))) - -/* Bounds on request (not chunk) sizes. */ -#define MAX_REQUEST ((~MIN_CHUNK_SIZE+1) << 2) -#define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE) - -/* pad request bytes into a usable size */ -#define pad_request(req) \ - (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) - -/* pad request, checking for minimum (but not maximum) */ -#define request2size(req) \ - (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) - -/* ------------------ Operations on head and foot fields ----------------- */ - -#define PINUSE_BIT (SIZE_T_ONE) -#define CINUSE_BIT (SIZE_T_TWO) -#define INUSE_BITS (PINUSE_BIT|CINUSE_BIT) - -/* Head value for fenceposts */ -#define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE) - -/* extraction of fields from head words */ -#define cinuse(p) ((p)->head & CINUSE_BIT) -#define pinuse(p) ((p)->head & PINUSE_BIT) -#define chunksize(p) ((p)->head & ~(INUSE_BITS)) - -#define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) -#define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT) - -/* Treat space at ptr +/- offset as a chunk */ -#define chunk_plus_offset(p, s) ((mchunkptr)(((char *)(p)) + (s))) -#define chunk_minus_offset(p, s) ((mchunkptr)(((char *)(p)) - (s))) - -/* Ptr to next or previous physical malloc_chunk. */ -#define next_chunk(p) ((mchunkptr)(((char *)(p)) + ((p)->head & ~INUSE_BITS))) -#define prev_chunk(p) ((mchunkptr)(((char *)(p)) - ((p)->prev_foot) )) - -/* extract next chunk's pinuse bit */ -#define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT) - -/* Get/set size at footer */ -#define get_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot) -#define set_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot = (s)) - -/* Set size, pinuse bit, and foot */ -#define set_size_and_pinuse_of_free_chunk(p, s)\ - ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) - -/* Set size, pinuse bit, foot, and clear next pinuse */ -#define set_free_with_pinuse(p, s, n)\ - (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) - -#define is_direct(p)\ - (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_DIRECT_BIT)) - -/* Get the internal overhead associated with chunk p */ -#define overhead_for(p)\ - (is_direct(p)? DIRECT_CHUNK_OVERHEAD : CHUNK_OVERHEAD) - -/* ---------------------- Overlaid data structures ----------------------- */ - -struct malloc_tree_chunk { - /* The first four fields must be compatible with malloc_chunk */ - size_t prev_foot; - size_t head; - struct malloc_tree_chunk *fd; - struct malloc_tree_chunk *bk; - - struct malloc_tree_chunk *child[2]; - struct malloc_tree_chunk *parent; - bindex_t index; -}; - -typedef struct malloc_tree_chunk tchunk; -typedef struct malloc_tree_chunk *tchunkptr; -typedef struct malloc_tree_chunk *tbinptr; /* The type of bins of trees */ - -/* A little helper macro for trees */ -#define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1]) - -/* ----------------------------- Segments -------------------------------- */ - -struct malloc_segment { - char *base; /* base address */ - size_t size; /* allocated size */ - struct malloc_segment *next; /* ptr to next segment */ -}; - -typedef struct malloc_segment msegment; -typedef struct malloc_segment *msegmentptr; - -/* ---------------------------- malloc_state ----------------------------- */ - -/* Bin types, widths and sizes */ -#define NSMALLBINS (32U) -#define NTREEBINS (32U) -#define SMALLBIN_SHIFT (3U) -#define SMALLBIN_WIDTH (SIZE_T_ONE << SMALLBIN_SHIFT) -#define TREEBIN_SHIFT (8U) -#define MIN_LARGE_SIZE (SIZE_T_ONE << TREEBIN_SHIFT) -#define MAX_SMALL_SIZE (MIN_LARGE_SIZE - SIZE_T_ONE) -#define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD) - -struct malloc_state { - binmap_t smallmap; - binmap_t treemap; - size_t dvsize; - size_t topsize; - mchunkptr dv; - mchunkptr top; - size_t trim_check; - size_t release_checks; - mchunkptr smallbins[(NSMALLBINS+1)*2]; - tbinptr treebins[NTREEBINS]; - msegment seg; - PRNGState *prng; -}; - -typedef struct malloc_state *mstate; - -#define is_initialized(M) ((M)->top != 0) - -/* -------------------------- system alloc setup ------------------------- */ - -/* page-align a size */ -#define page_align(S)\ - (((S) + (LJ_PAGESIZE - SIZE_T_ONE)) & ~(LJ_PAGESIZE - SIZE_T_ONE)) - -/* granularity-align a size */ -#define granularity_align(S)\ - (((S) + (DEFAULT_GRANULARITY - SIZE_T_ONE))\ - & ~(DEFAULT_GRANULARITY - SIZE_T_ONE)) - -#if LJ_TARGET_WINDOWS -#define mmap_align(S) granularity_align(S) -#else -#define mmap_align(S) page_align(S) -#endif - -/* True if segment S holds address A */ -#define segment_holds(S, A)\ - ((char *)(A) >= S->base && (char *)(A) < S->base + S->size) - -/* Return segment holding given address */ -static msegmentptr segment_holding(mstate m, char *addr) -{ - msegmentptr sp = &m->seg; - for (;;) { - if (addr >= sp->base && addr < sp->base + sp->size) - return sp; - if ((sp = sp->next) == 0) - return 0; - } -} - -/* Return true if segment contains a segment link */ -static int has_segment_link(mstate m, msegmentptr ss) -{ - msegmentptr sp = &m->seg; - for (;;) { - if ((char *)sp >= ss->base && (char *)sp < ss->base + ss->size) - return 1; - if ((sp = sp->next) == 0) - return 0; - } -} - -/* - TOP_FOOT_SIZE is padding at the end of a segment, including space - that may be needed to place segment records and fenceposts when new - noncontiguous segments are added. -*/ -#define TOP_FOOT_SIZE\ - (align_offset(TWO_SIZE_T_SIZES)+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) - -/* ---------------------------- Indexing Bins ---------------------------- */ - -#define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS) -#define small_index(s) ((s) >> SMALLBIN_SHIFT) -#define small_index2size(i) ((i) << SMALLBIN_SHIFT) -#define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE)) - -/* addressing by index. See above about smallbin repositioning */ -#define smallbin_at(M, i) ((sbinptr)((char *)&((M)->smallbins[(i)<<1]))) -#define treebin_at(M,i) (&((M)->treebins[i])) - -/* assign tree index for size S to variable I */ -#define compute_tree_index(S, I)\ -{\ - unsigned int X = (unsigned int)(S >> TREEBIN_SHIFT);\ - if (X == 0) {\ - I = 0;\ - } else if (X > 0xFFFF) {\ - I = NTREEBINS-1;\ - } else {\ - unsigned int K = lj_fls(X);\ - I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ - }\ -} - -/* Bit representing maximum resolved size in a treebin at i */ -#define bit_for_tree_index(i) \ - (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) - -/* Shift placing maximum resolved bit in a treebin at i as sign bit */ -#define leftshift_for_tree_index(i) \ - ((i == NTREEBINS-1)? 0 : \ - ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) - -/* The size of the smallest chunk held in bin with index i */ -#define minsize_for_tree_index(i) \ - ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ - (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) - -/* ------------------------ Operations on bin maps ----------------------- */ - -/* bit corresponding to given index */ -#define idx2bit(i) ((binmap_t)(1) << (i)) - -/* Mark/Clear bits with given index */ -#define mark_smallmap(M,i) ((M)->smallmap |= idx2bit(i)) -#define clear_smallmap(M,i) ((M)->smallmap &= ~idx2bit(i)) -#define smallmap_is_marked(M,i) ((M)->smallmap & idx2bit(i)) - -#define mark_treemap(M,i) ((M)->treemap |= idx2bit(i)) -#define clear_treemap(M,i) ((M)->treemap &= ~idx2bit(i)) -#define treemap_is_marked(M,i) ((M)->treemap & idx2bit(i)) - -/* mask with all bits to left of least bit of x on */ -#define left_bits(x) ((x<<1) | (~(x<<1)+1)) - -/* Set cinuse bit and pinuse bit of next chunk */ -#define set_inuse(M,p,s)\ - ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ - ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT) - -/* Set cinuse and pinuse of this chunk and pinuse of next chunk */ -#define set_inuse_and_pinuse(M,p,s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ - ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT) - -/* Set size, cinuse and pinuse bit of this chunk */ -#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) - -/* ----------------------- Operations on smallbins ----------------------- */ - -/* Link a free chunk into a smallbin */ -#define insert_small_chunk(M, P, S) {\ - bindex_t I = small_index(S);\ - mchunkptr B = smallbin_at(M, I);\ - mchunkptr F = B;\ - if (!smallmap_is_marked(M, I))\ - mark_smallmap(M, I);\ - else\ - F = B->fd;\ - B->fd = P;\ - F->bk = P;\ - P->fd = F;\ - P->bk = B;\ -} - -/* Unlink a chunk from a smallbin */ -#define unlink_small_chunk(M, P, S) {\ - mchunkptr F = P->fd;\ - mchunkptr B = P->bk;\ - bindex_t I = small_index(S);\ - if (F == B) {\ - clear_smallmap(M, I);\ - } else {\ - F->bk = B;\ - B->fd = F;\ - }\ -} - -/* Unlink the first chunk from a smallbin */ -#define unlink_first_small_chunk(M, B, P, I) {\ - mchunkptr F = P->fd;\ - if (B == F) {\ - clear_smallmap(M, I);\ - } else {\ - B->fd = F;\ - F->bk = B;\ - }\ -} - -/* Replace dv node, binning the old one */ -/* Used only when dvsize known to be small */ -#define replace_dv(M, P, S) {\ - size_t DVS = M->dvsize;\ - if (DVS != 0) {\ - mchunkptr DV = M->dv;\ - insert_small_chunk(M, DV, DVS);\ - }\ - M->dvsize = S;\ - M->dv = P;\ -} - -/* ------------------------- Operations on trees ------------------------- */ - -/* Insert chunk into tree */ -#define insert_large_chunk(M, X, S) {\ - tbinptr *H;\ - bindex_t I;\ - compute_tree_index(S, I);\ - H = treebin_at(M, I);\ - X->index = I;\ - X->child[0] = X->child[1] = 0;\ - if (!treemap_is_marked(M, I)) {\ - mark_treemap(M, I);\ - *H = X;\ - X->parent = (tchunkptr)H;\ - X->fd = X->bk = X;\ - } else {\ - tchunkptr T = *H;\ - size_t K = S << leftshift_for_tree_index(I);\ - for (;;) {\ - if (chunksize(T) != S) {\ - tchunkptr *C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ - K <<= 1;\ - if (*C != 0) {\ - T = *C;\ - } else {\ - *C = X;\ - X->parent = T;\ - X->fd = X->bk = X;\ - break;\ - }\ - } else {\ - tchunkptr F = T->fd;\ - T->fd = F->bk = X;\ - X->fd = F;\ - X->bk = T;\ - X->parent = 0;\ - break;\ - }\ - }\ - }\ -} - -#define unlink_large_chunk(M, X) {\ - tchunkptr XP = X->parent;\ - tchunkptr R;\ - if (X->bk != X) {\ - tchunkptr F = X->fd;\ - R = X->bk;\ - F->bk = R;\ - R->fd = F;\ - } else {\ - tchunkptr *RP;\ - if (((R = *(RP = &(X->child[1]))) != 0) ||\ - ((R = *(RP = &(X->child[0]))) != 0)) {\ - tchunkptr *CP;\ - while ((*(CP = &(R->child[1])) != 0) ||\ - (*(CP = &(R->child[0])) != 0)) {\ - R = *(RP = CP);\ - }\ - *RP = 0;\ - }\ - }\ - if (XP != 0) {\ - tbinptr *H = treebin_at(M, X->index);\ - if (X == *H) {\ - if ((*H = R) == 0) \ - clear_treemap(M, X->index);\ - } else {\ - if (XP->child[0] == X) \ - XP->child[0] = R;\ - else \ - XP->child[1] = R;\ - }\ - if (R != 0) {\ - tchunkptr C0, C1;\ - R->parent = XP;\ - if ((C0 = X->child[0]) != 0) {\ - R->child[0] = C0;\ - C0->parent = R;\ - }\ - if ((C1 = X->child[1]) != 0) {\ - R->child[1] = C1;\ - C1->parent = R;\ - }\ - }\ - }\ -} - -/* Relays to large vs small bin operations */ - -#define insert_chunk(M, P, S)\ - if (is_small(S)) { insert_small_chunk(M, P, S)\ - } else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } - -#define unlink_chunk(M, P, S)\ - if (is_small(S)) { unlink_small_chunk(M, P, S)\ - } else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } - -/* ----------------------- Direct-mmapping chunks ----------------------- */ - -static void *direct_alloc(mstate m, size_t nb) -{ - size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - if (LJ_LIKELY(mmsize > nb)) { /* Check for wrap around 0 */ - char *mm = (char *)(DIRECT_MMAP(m->prng, mmsize)); - if (mm != CMFAIL) { - size_t offset = align_offset(chunk2mem(mm)); - size_t psize = mmsize - offset - DIRECT_FOOT_PAD; - mchunkptr p = (mchunkptr)(mm + offset); - p->prev_foot = offset | IS_DIRECT_BIT; - p->head = psize|CINUSE_BIT; - chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; - chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; - return chunk2mem(p); - } - } - UNUSED(m); - return NULL; -} - -static mchunkptr direct_resize(mchunkptr oldp, size_t nb) -{ - size_t oldsize = chunksize(oldp); - if (is_small(nb)) /* Can't shrink direct regions below small size */ - return NULL; - /* Keep old chunk if big enough but not too big */ - if (oldsize >= nb + SIZE_T_SIZE && - (oldsize - nb) <= (DEFAULT_GRANULARITY >> 1)) { - return oldp; - } else { - size_t offset = oldp->prev_foot & ~IS_DIRECT_BIT; - size_t oldmmsize = oldsize + offset + DIRECT_FOOT_PAD; - size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - char *cp = (char *)CALL_MREMAP((char *)oldp - offset, - oldmmsize, newmmsize, CALL_MREMAP_MV); - if (cp != CMFAIL) { - mchunkptr newp = (mchunkptr)(cp + offset); - size_t psize = newmmsize - offset - DIRECT_FOOT_PAD; - newp->head = psize|CINUSE_BIT; - chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; - chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; - return newp; - } - } - return NULL; -} - -/* -------------------------- mspace management -------------------------- */ - -/* Initialize top chunk and its size */ -static void init_top(mstate m, mchunkptr p, size_t psize) -{ - /* Ensure alignment */ - size_t offset = align_offset(chunk2mem(p)); - p = (mchunkptr)((char *)p + offset); - psize -= offset; - - m->top = p; - m->topsize = psize; - p->head = psize | PINUSE_BIT; - /* set size of fake trailing chunk holding overhead space only once */ - chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; - m->trim_check = DEFAULT_TRIM_THRESHOLD; /* reset on each update */ -} - -/* Initialize bins for a new mstate that is otherwise zeroed out */ -static void init_bins(mstate m) -{ - /* Establish circular links for smallbins */ - bindex_t i; - for (i = 0; i < NSMALLBINS; i++) { - sbinptr bin = smallbin_at(m,i); - bin->fd = bin->bk = bin; - } -} - -/* Allocate chunk and prepend remainder with chunk in successor base. */ -static void *prepend_alloc(mstate m, char *newbase, char *oldbase, size_t nb) -{ - mchunkptr p = align_as_chunk(newbase); - mchunkptr oldfirst = align_as_chunk(oldbase); - size_t psize = (size_t)((char *)oldfirst - (char *)p); - mchunkptr q = chunk_plus_offset(p, nb); - size_t qsize = psize - nb; - set_size_and_pinuse_of_inuse_chunk(m, p, nb); - - /* consolidate remainder with first chunk of old base */ - if (oldfirst == m->top) { - size_t tsize = m->topsize += qsize; - m->top = q; - q->head = tsize | PINUSE_BIT; - } else if (oldfirst == m->dv) { - size_t dsize = m->dvsize += qsize; - m->dv = q; - set_size_and_pinuse_of_free_chunk(q, dsize); - } else { - if (!cinuse(oldfirst)) { - size_t nsize = chunksize(oldfirst); - unlink_chunk(m, oldfirst, nsize); - oldfirst = chunk_plus_offset(oldfirst, nsize); - qsize += nsize; - } - set_free_with_pinuse(q, qsize, oldfirst); - insert_chunk(m, q, qsize); - } - - return chunk2mem(p); -} - -/* Add a segment to hold a new noncontiguous region */ -static void add_segment(mstate m, char *tbase, size_t tsize) -{ - /* Determine locations and sizes of segment, fenceposts, old top */ - char *old_top = (char *)m->top; - msegmentptr oldsp = segment_holding(m, old_top); - char *old_end = oldsp->base + oldsp->size; - size_t ssize = pad_request(sizeof(struct malloc_segment)); - char *rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - size_t offset = align_offset(chunk2mem(rawsp)); - char *asp = rawsp + offset; - char *csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp; - mchunkptr sp = (mchunkptr)csp; - msegmentptr ss = (msegmentptr)(chunk2mem(sp)); - mchunkptr tnext = chunk_plus_offset(sp, ssize); - mchunkptr p = tnext; - - /* reset top to new space */ - init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); - - /* Set up segment record */ - set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); - *ss = m->seg; /* Push current record */ - m->seg.base = tbase; - m->seg.size = tsize; - m->seg.next = ss; - - /* Insert trailing fenceposts */ - for (;;) { - mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); - p->head = FENCEPOST_HEAD; - if ((char *)(&(nextp->head)) < old_end) - p = nextp; - else - break; - } - - /* Insert the rest of old top into a bin as an ordinary free chunk */ - if (csp != old_top) { - mchunkptr q = (mchunkptr)old_top; - size_t psize = (size_t)(csp - old_top); - mchunkptr tn = chunk_plus_offset(q, psize); - set_free_with_pinuse(q, psize, tn); - insert_chunk(m, q, psize); - } -} - -/* -------------------------- System allocation -------------------------- */ - -static void *alloc_sys(mstate m, size_t nb) -{ - char *tbase = CMFAIL; - size_t tsize = 0; - - /* Directly map large chunks */ - if (LJ_UNLIKELY(nb >= DEFAULT_MMAP_THRESHOLD)) { - void *mem = direct_alloc(m, nb); - if (mem != 0) - return mem; - } - - { - size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE; - size_t rsize = granularity_align(req); - if (LJ_LIKELY(rsize > nb)) { /* Fail if wraps around zero */ - char *mp = (char *)(CALL_MMAP(m->prng, rsize)); - if (mp != CMFAIL) { - tbase = mp; - tsize = rsize; - } - } - } - - if (tbase != CMFAIL) { - msegmentptr sp = &m->seg; - /* Try to merge with an existing segment */ - while (sp != 0 && tbase != sp->base + sp->size) - sp = sp->next; - if (sp != 0 && segment_holds(sp, m->top)) { /* append */ - sp->size += tsize; - init_top(m, m->top, m->topsize + tsize); - } else { - sp = &m->seg; - while (sp != 0 && sp->base != tbase + tsize) - sp = sp->next; - if (sp != 0) { - char *oldbase = sp->base; - sp->base = tbase; - sp->size += tsize; - return prepend_alloc(m, tbase, oldbase, nb); - } else { - add_segment(m, tbase, tsize); - } - } - - if (nb < m->topsize) { /* Allocate from new or extended top space */ - size_t rsize = m->topsize -= nb; - mchunkptr p = m->top; - mchunkptr r = m->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(m, p, nb); - return chunk2mem(p); - } - } - - return NULL; -} - -/* ----------------------- system deallocation -------------------------- */ - -/* Unmap and unlink any mmapped segments that don't contain used chunks */ -static size_t release_unused_segments(mstate m) -{ - size_t released = 0; - size_t nsegs = 0; - msegmentptr pred = &m->seg; - msegmentptr sp = pred->next; - while (sp != 0) { - char *base = sp->base; - size_t size = sp->size; - msegmentptr next = sp->next; - nsegs++; - { - mchunkptr p = align_as_chunk(base); - size_t psize = chunksize(p); - /* Can unmap if first chunk holds entire segment and not pinned */ - if (!cinuse(p) && (char *)p + psize >= base + size - TOP_FOOT_SIZE) { - tchunkptr tp = (tchunkptr)p; - if (p == m->dv) { - m->dv = 0; - m->dvsize = 0; - } else { - unlink_large_chunk(m, tp); - } - if (CALL_MUNMAP(base, size) == 0) { - released += size; - /* unlink obsoleted record */ - sp = pred; - sp->next = next; - } else { /* back out if cannot unmap */ - insert_large_chunk(m, tp, psize); - } - } - } - pred = sp; - sp = next; - } - /* Reset check counter */ - m->release_checks = nsegs > MAX_RELEASE_CHECK_RATE ? - nsegs : MAX_RELEASE_CHECK_RATE; - return released; -} - -static int alloc_trim(mstate m, size_t pad) -{ - size_t released = 0; - if (pad < MAX_REQUEST && is_initialized(m)) { - pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ - - if (m->topsize > pad) { - /* Shrink top space in granularity-size units, keeping at least one */ - size_t unit = DEFAULT_GRANULARITY; - size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - - SIZE_T_ONE) * unit; - msegmentptr sp = segment_holding(m, (char *)m->top); - - if (sp->size >= extra && - !has_segment_link(m, sp)) { /* can't shrink if pinned */ - size_t newsize = sp->size - extra; - /* Prefer mremap, fall back to munmap */ - if ((CALL_MREMAP(sp->base, sp->size, newsize, CALL_MREMAP_NOMOVE) != MFAIL) || - (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { - released = extra; - } - } - - if (released != 0) { - sp->size -= released; - init_top(m, m->top, m->topsize - released); - } - } - - /* Unmap any unused mmapped segments */ - released += release_unused_segments(m); - - /* On failure, disable autotrim to avoid repeated failed future calls */ - if (released == 0 && m->topsize > m->trim_check) - m->trim_check = MAX_SIZE_T; - } - - return (released != 0)? 1 : 0; -} - -/* ---------------------------- malloc support --------------------------- */ - -/* allocate a large request from the best fitting chunk in a treebin */ -static void *tmalloc_large(mstate m, size_t nb) -{ - tchunkptr v = 0; - size_t rsize = ~nb+1; /* Unsigned negation */ - tchunkptr t; - bindex_t idx; - compute_tree_index(nb, idx); - - if ((t = *treebin_at(m, idx)) != 0) { - /* Traverse tree for this bin looking for node with size == nb */ - size_t sizebits = nb << leftshift_for_tree_index(idx); - tchunkptr rst = 0; /* The deepest untaken right subtree */ - for (;;) { - tchunkptr rt; - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - v = t; - if ((rsize = trem) == 0) - break; - } - rt = t->child[1]; - t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; - if (rt != 0 && rt != t) - rst = rt; - if (t == 0) { - t = rst; /* set t to least subtree holding sizes > nb */ - break; - } - sizebits <<= 1; - } - } - - if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ - binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; - if (leftbits != 0) - t = *treebin_at(m, lj_ffs(leftbits)); - } - - while (t != 0) { /* find smallest of tree or subtree */ - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - rsize = trem; - v = t; - } - t = leftmost_child(t); - } - - /* If dv is a better fit, return NULL so malloc will use it */ - if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { - mchunkptr r = chunk_plus_offset(v, nb); - unlink_large_chunk(m, v); - if (rsize < MIN_CHUNK_SIZE) { - set_inuse_and_pinuse(m, v, (rsize + nb)); - } else { - set_size_and_pinuse_of_inuse_chunk(m, v, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - insert_chunk(m, r, rsize); - } - return chunk2mem(v); - } - return NULL; -} - -/* allocate a small request from the best fitting chunk in a treebin */ -static void *tmalloc_small(mstate m, size_t nb) -{ - tchunkptr t, v; - mchunkptr r; - size_t rsize; - bindex_t i = lj_ffs(m->treemap); - - v = t = *treebin_at(m, i); - rsize = chunksize(t) - nb; - - while ((t = leftmost_child(t)) != 0) { - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - rsize = trem; - v = t; - } - } - - r = chunk_plus_offset(v, nb); - unlink_large_chunk(m, v); - if (rsize < MIN_CHUNK_SIZE) { - set_inuse_and_pinuse(m, v, (rsize + nb)); - } else { - set_size_and_pinuse_of_inuse_chunk(m, v, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(m, r, rsize); - } - return chunk2mem(v); -} - -/* ----------------------------------------------------------------------- */ - -void *lj_alloc_create(PRNGState *rs) -{ - size_t tsize = DEFAULT_GRANULARITY; - char *tbase; - INIT_MMAP(); - UNUSED(rs); - tbase = (char *)(CALL_MMAP(rs, tsize)); - if (tbase != CMFAIL) { - size_t msize = pad_request(sizeof(struct malloc_state)); - mchunkptr mn; - mchunkptr msp = align_as_chunk(tbase); - mstate m = (mstate)(chunk2mem(msp)); - memset(m, 0, msize); - msp->head = (msize|PINUSE_BIT|CINUSE_BIT); - m->seg.base = tbase; - m->seg.size = tsize; - m->release_checks = MAX_RELEASE_CHECK_RATE; - init_bins(m); - mn = next_chunk(mem2chunk(m)); - init_top(m, mn, (size_t)((tbase + tsize) - (char *)mn) - TOP_FOOT_SIZE); - return m; - } - return NULL; -} - -void lj_alloc_setprng(void *msp, PRNGState *rs) -{ - mstate ms = (mstate)msp; - ms->prng = rs; -} - -void lj_alloc_destroy(void *msp) -{ - mstate ms = (mstate)msp; - msegmentptr sp = &ms->seg; - while (sp != 0) { - char *base = sp->base; - size_t size = sp->size; - sp = sp->next; - CALL_MUNMAP(base, size); - } -} - -static LJ_NOINLINE void *lj_alloc_malloc(void *msp, size_t nsize) -{ - mstate ms = (mstate)msp; - void *mem; - size_t nb; - if (nsize <= MAX_SMALL_REQUEST) { - bindex_t idx; - binmap_t smallbits; - nb = (nsize < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(nsize); - idx = small_index(nb); - smallbits = ms->smallmap >> idx; - - if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ - mchunkptr b, p; - idx += ~smallbits & 1; /* Uses next bin if idx empty */ - b = smallbin_at(ms, idx); - p = b->fd; - unlink_first_small_chunk(ms, b, p, idx); - set_inuse_and_pinuse(ms, p, small_index2size(idx)); - mem = chunk2mem(p); - return mem; - } else if (nb > ms->dvsize) { - if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ - mchunkptr b, p, r; - size_t rsize; - binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); - bindex_t i = lj_ffs(leftbits); - b = smallbin_at(ms, i); - p = b->fd; - unlink_first_small_chunk(ms, b, p, i); - rsize = small_index2size(i) - nb; - /* Fit here cannot be remainderless if 4byte sizes */ - if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) { - set_inuse_and_pinuse(ms, p, small_index2size(i)); - } else { - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - r = chunk_plus_offset(p, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(ms, r, rsize); - } - mem = chunk2mem(p); - return mem; - } else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { - return mem; - } - } - } else if (nsize >= MAX_REQUEST) { - nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ - } else { - nb = pad_request(nsize); - if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { - return mem; - } - } - - if (nb <= ms->dvsize) { - size_t rsize = ms->dvsize - nb; - mchunkptr p = ms->dv; - if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ - mchunkptr r = ms->dv = chunk_plus_offset(p, nb); - ms->dvsize = rsize; - set_size_and_pinuse_of_free_chunk(r, rsize); - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - } else { /* exhaust dv */ - size_t dvs = ms->dvsize; - ms->dvsize = 0; - ms->dv = 0; - set_inuse_and_pinuse(ms, p, dvs); - } - mem = chunk2mem(p); - return mem; - } else if (nb < ms->topsize) { /* Split top */ - size_t rsize = ms->topsize -= nb; - mchunkptr p = ms->top; - mchunkptr r = ms->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - mem = chunk2mem(p); - return mem; - } - return alloc_sys(ms, nb); -} - -static LJ_NOINLINE void *lj_alloc_free(void *msp, void *ptr) -{ - if (ptr != 0) { - mchunkptr p = mem2chunk(ptr); - mstate fm = (mstate)msp; - size_t psize = chunksize(p); - mchunkptr next = chunk_plus_offset(p, psize); - if (!pinuse(p)) { - size_t prevsize = p->prev_foot; - if ((prevsize & IS_DIRECT_BIT) != 0) { - prevsize &= ~IS_DIRECT_BIT; - psize += prevsize + DIRECT_FOOT_PAD; - CALL_MUNMAP((char *)p - prevsize, psize); - return NULL; - } else { - mchunkptr prev = chunk_minus_offset(p, prevsize); - psize += prevsize; - p = prev; - /* consolidate backward */ - if (p != fm->dv) { - unlink_chunk(fm, p, prevsize); - } else if ((next->head & INUSE_BITS) == INUSE_BITS) { - fm->dvsize = psize; - set_free_with_pinuse(p, psize, next); - return NULL; - } - } - } - if (!cinuse(next)) { /* consolidate forward */ - if (next == fm->top) { - size_t tsize = fm->topsize += psize; - fm->top = p; - p->head = tsize | PINUSE_BIT; - if (p == fm->dv) { - fm->dv = 0; - fm->dvsize = 0; - } - if (tsize > fm->trim_check) - alloc_trim(fm, 0); - return NULL; - } else if (next == fm->dv) { - size_t dsize = fm->dvsize += psize; - fm->dv = p; - set_size_and_pinuse_of_free_chunk(p, dsize); - return NULL; - } else { - size_t nsize = chunksize(next); - psize += nsize; - unlink_chunk(fm, next, nsize); - set_size_and_pinuse_of_free_chunk(p, psize); - if (p == fm->dv) { - fm->dvsize = psize; - return NULL; - } - } - } else { - set_free_with_pinuse(p, psize, next); - } - - if (is_small(psize)) { - insert_small_chunk(fm, p, psize); - } else { - tchunkptr tp = (tchunkptr)p; - insert_large_chunk(fm, tp, psize); - if (--fm->release_checks == 0) - release_unused_segments(fm); - } - } - return NULL; -} - -static LJ_NOINLINE void *lj_alloc_realloc(void *msp, void *ptr, size_t nsize) -{ - if (nsize >= MAX_REQUEST) { - return NULL; - } else { - mstate m = (mstate)msp; - mchunkptr oldp = mem2chunk(ptr); - size_t oldsize = chunksize(oldp); - mchunkptr next = chunk_plus_offset(oldp, oldsize); - mchunkptr newp = 0; - size_t nb = request2size(nsize); - - /* Try to either shrink or extend into top. Else malloc-copy-free */ - if (is_direct(oldp)) { - newp = direct_resize(oldp, nb); /* this may return NULL. */ - } else if (oldsize >= nb) { /* already big enough */ - size_t rsize = oldsize - nb; - newp = oldp; - if (rsize >= MIN_CHUNK_SIZE) { - mchunkptr rem = chunk_plus_offset(newp, nb); - set_inuse(m, newp, nb); - set_inuse(m, rem, rsize); - lj_alloc_free(m, chunk2mem(rem)); - } - } else if (next == m->top && oldsize + m->topsize > nb) { - /* Expand into top */ - size_t newsize = oldsize + m->topsize; - size_t newtopsize = newsize - nb; - mchunkptr newtop = chunk_plus_offset(oldp, nb); - set_inuse(m, oldp, nb); - newtop->head = newtopsize |PINUSE_BIT; - m->top = newtop; - m->topsize = newtopsize; - newp = oldp; - } - - if (newp != 0) { - return chunk2mem(newp); - } else { - void *newmem = lj_alloc_malloc(m, nsize); - if (newmem != 0) { - size_t oc = oldsize - overhead_for(oldp); - memcpy(newmem, ptr, oc < nsize ? oc : nsize); - lj_alloc_free(m, ptr); - } - return newmem; - } - } -} - -void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize) -{ - (void)osize; - if (nsize == 0) { - return lj_alloc_free(msp, ptr); - } else if (ptr == NULL) { - return lj_alloc_malloc(msp, nsize); - } else { - return lj_alloc_realloc(msp, ptr, nsize); - } -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.h deleted file mode 100644 index 669f50b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_alloc.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -** Bundled memory allocator. -** Donated to the public domain. -*/ - -#ifndef _LJ_ALLOC_H -#define _LJ_ALLOC_H - -#include "lj_def.h" - -#ifndef LUAJIT_USE_SYSMALLOC -LJ_FUNC void *lj_alloc_create(PRNGState *rs); -LJ_FUNC void lj_alloc_setprng(void *msp, PRNGState *rs); -LJ_FUNC void lj_alloc_destroy(void *msp); -LJ_FUNC void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_api.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_api.c deleted file mode 100644 index e6b6747..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_api.c +++ /dev/null @@ -1,1313 +0,0 @@ -/* -** Public Lua/C API. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_api_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_udata.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_bc.h" -#include "lj_frame.h" -#include "lj_trace.h" -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" - -/* -- Common helper functions --------------------------------------------- */ - -#define lj_checkapi_slot(idx) \ - lj_checkapi((idx) <= (L->top - L->base), "stack slot %d out of range", (idx)) - -static TValue *index2adr(lua_State *L, int idx) -{ - if (idx > 0) { - TValue *o = L->base + (idx - 1); - return o < L->top ? o : niltv(L); - } else if (idx > LUA_REGISTRYINDEX) { - lj_checkapi(idx != 0 && -idx <= L->top - L->base, - "bad stack slot %d", idx); - return L->top + idx; - } else if (idx == LUA_GLOBALSINDEX) { - TValue *o = &G(L)->tmptv; - settabV(L, o, tabref(L->env)); - return o; - } else if (idx == LUA_REGISTRYINDEX) { - return registry(L); - } else { - GCfunc *fn = curr_func(L); - lj_checkapi(fn->c.gct == ~LJ_TFUNC && !isluafunc(fn), - "calling frame is not a C function"); - if (idx == LUA_ENVIRONINDEX) { - TValue *o = &G(L)->tmptv; - settabV(L, o, tabref(fn->c.env)); - return o; - } else { - idx = LUA_GLOBALSINDEX - idx; - return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L); - } - } -} - -static LJ_AINLINE TValue *index2adr_check(lua_State *L, int idx) -{ - TValue *o = index2adr(L, idx); - lj_checkapi(o != niltv(L), "invalid stack slot %d", idx); - return o; -} - -static TValue *index2adr_stack(lua_State *L, int idx) -{ - if (idx > 0) { - TValue *o = L->base + (idx - 1); - if (o < L->top) { - return o; - } else { - lj_checkapi(0, "invalid stack slot %d", idx); - return niltv(L); - } - return o < L->top ? o : niltv(L); - } else { - lj_checkapi(idx != 0 && -idx <= L->top - L->base, - "invalid stack slot %d", idx); - return L->top + idx; - } -} - -static GCtab *getcurrenv(lua_State *L) -{ - GCfunc *fn = curr_func(L); - return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env); -} - -/* -- Miscellaneous API functions ----------------------------------------- */ - -LUA_API int lua_status(lua_State *L) -{ - return L->status; -} - -LUA_API int lua_checkstack(lua_State *L, int size) -{ - if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) { - return 0; /* Stack overflow. */ - } else if (size > 0) { - lj_state_checkstack(L, (MSize)size); - } - return 1; -} - -LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg) -{ - if (!lua_checkstack(L, size)) - lj_err_callerv(L, LJ_ERR_STKOVM, msg); -} - -LUA_API void lua_xmove(lua_State *L, lua_State *to, int n) -{ - TValue *f, *t; - if (L == to) return; - lj_checkapi_slot(n); - lj_checkapi(G(L) == G(to), "move across global states"); - lj_state_checkstack(to, (MSize)n); - f = L->top; - t = to->top = to->top + n; - while (--n >= 0) copyTV(to, --t, --f); - L->top = f; -} - -LUA_API const lua_Number *lua_version(lua_State *L) -{ - static const lua_Number version = LUA_VERSION_NUM; - UNUSED(L); - return &version; -} - -/* -- Stack manipulation -------------------------------------------------- */ - -LUA_API int lua_gettop(lua_State *L) -{ - return (int)(L->top - L->base); -} - -LUA_API void lua_settop(lua_State *L, int idx) -{ - if (idx >= 0) { - lj_checkapi(idx <= tvref(L->maxstack) - L->base, "bad stack slot %d", idx); - if (L->base + idx > L->top) { - if (L->base + idx >= tvref(L->maxstack)) - lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base)); - do { setnilV(L->top++); } while (L->top < L->base + idx); - } else { - L->top = L->base + idx; - } - } else { - lj_checkapi(-(idx+1) <= (L->top - L->base), "bad stack slot %d", idx); - L->top += idx+1; /* Shrinks top (idx < 0). */ - } -} - -LUA_API void lua_remove(lua_State *L, int idx) -{ - TValue *p = index2adr_stack(L, idx); - while (++p < L->top) copyTV(L, p-1, p); - L->top--; -} - -LUA_API void lua_insert(lua_State *L, int idx) -{ - TValue *q, *p = index2adr_stack(L, idx); - for (q = L->top; q > p; q--) copyTV(L, q, q-1); - copyTV(L, p, L->top); -} - -static void copy_slot(lua_State *L, TValue *f, int idx) -{ - if (idx == LUA_GLOBALSINDEX) { - lj_checkapi(tvistab(f), "stack slot %d is not a table", idx); - /* NOBARRIER: A thread (i.e. L) is never black. */ - setgcref(L->env, obj2gco(tabV(f))); - } else if (idx == LUA_ENVIRONINDEX) { - GCfunc *fn = curr_func(L); - if (fn->c.gct != ~LJ_TFUNC) - lj_err_msg(L, LJ_ERR_NOENV); - lj_checkapi(tvistab(f), "stack slot %d is not a table", idx); - setgcref(fn->c.env, obj2gco(tabV(f))); - lj_gc_barrier(L, fn, f); - } else { - TValue *o = index2adr_check(L, idx); - copyTV(L, o, f); - if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */ - lj_gc_barrier(L, curr_func(L), f); - } -} - -LUA_API void lua_replace(lua_State *L, int idx) -{ - lj_checkapi_slot(1); - copy_slot(L, L->top - 1, idx); - L->top--; -} - -LUA_API void lua_copy(lua_State *L, int fromidx, int toidx) -{ - copy_slot(L, index2adr(L, fromidx), toidx); -} - -LUA_API void lua_pushvalue(lua_State *L, int idx) -{ - copyTV(L, L->top, index2adr(L, idx)); - incr_top(L); -} - -/* -- Stack getters ------------------------------------------------------- */ - -LUA_API int lua_type(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisnumber(o)) { - return LUA_TNUMBER; -#if LJ_64 && !LJ_GC64 - } else if (tvislightud(o)) { - return LUA_TLIGHTUSERDATA; -#endif - } else if (o == niltv(L)) { - return LUA_TNONE; - } else { /* Magic internal/external tag conversion. ORDER LJ_T */ - uint32_t t = ~itype(o); -#if LJ_64 - int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u); -#else - int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u); -#endif - lj_assertL(tt != LUA_TNIL || tvisnil(o), "bad tag conversion"); - return tt; - } -} - -LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt) -{ - if (lua_type(L, idx) != tt) - lj_err_argt(L, idx, tt); -} - -LUALIB_API void luaL_checkany(lua_State *L, int idx) -{ - if (index2adr(L, idx) == niltv(L)) - lj_err_arg(L, idx, LJ_ERR_NOVAL); -} - -LUA_API const char *lua_typename(lua_State *L, int t) -{ - UNUSED(L); - return lj_obj_typename[t+1]; -} - -LUA_API int lua_iscfunction(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return tvisfunc(o) && !isluafunc(funcV(o)); -} - -LUA_API int lua_isnumber(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp))); -} - -LUA_API int lua_isstring(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return (tvisstr(o) || tvisnumber(o)); -} - -LUA_API int lua_isuserdata(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return (tvisudata(o) || tvislightud(o)); -} - -LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2) -{ - cTValue *o1 = index2adr(L, idx1); - cTValue *o2 = index2adr(L, idx2); - return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2); -} - -LUA_API int lua_equal(lua_State *L, int idx1, int idx2) -{ - cTValue *o1 = index2adr(L, idx1); - cTValue *o2 = index2adr(L, idx2); - if (tvisint(o1) && tvisint(o2)) { - return intV(o1) == intV(o2); - } else if (tvisnumber(o1) && tvisnumber(o2)) { - return numberVnum(o1) == numberVnum(o2); - } else if (itype(o1) != itype(o2)) { - return 0; - } else if (tvispri(o1)) { - return o1 != niltv(L) && o2 != niltv(L); -#if LJ_64 && !LJ_GC64 - } else if (tvislightud(o1)) { - return o1->u64 == o2->u64; -#endif - } else if (gcrefeq(o1->gcr, o2->gcr)) { - return 1; - } else if (!tvistabud(o1)) { - return 0; - } else { - TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0); - if ((uintptr_t)base <= 1) { - return (int)(uintptr_t)base; - } else { - L->top = base+2; - lj_vm_call(L, base, 1+1); - L->top -= 2+LJ_FR2; - return tvistruecond(L->top+1+LJ_FR2); - } - } -} - -LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2) -{ - cTValue *o1 = index2adr(L, idx1); - cTValue *o2 = index2adr(L, idx2); - if (o1 == niltv(L) || o2 == niltv(L)) { - return 0; - } else if (tvisint(o1) && tvisint(o2)) { - return intV(o1) < intV(o2); - } else if (tvisnumber(o1) && tvisnumber(o2)) { - return numberVnum(o1) < numberVnum(o2); - } else { - TValue *base = lj_meta_comp(L, o1, o2, 0); - if ((uintptr_t)base <= 1) { - return (int)(uintptr_t)base; - } else { - L->top = base+2; - lj_vm_call(L, base, 1+1); - L->top -= 2+LJ_FR2; - return tvistruecond(L->top+1+LJ_FR2); - } - } -} - -LUA_API lua_Number lua_tonumber(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) - return numberVnum(o); - else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) - return numV(&tmp); - else - return 0; -} - -LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *ok) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) { - if (ok) *ok = 1; - return numberVnum(o); - } else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) { - if (ok) *ok = 1; - return numV(&tmp); - } else { - if (ok) *ok = 0; - return 0; - } -} - -LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) - return numberVnum(o); - else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - return numV(&tmp); -} - -LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - if (LJ_LIKELY(tvisnumber(o))) - return numberVnum(o); - else if (tvisnil(o)) - return def; - else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - return numV(&tmp); -} - -LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) - return 0; - if (tvisint(&tmp)) - return intV(&tmp); - n = numV(&tmp); - } -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *ok) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - if (ok) *ok = 1; - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) { - if (ok) *ok = 0; - return 0; - } - if (tvisint(&tmp)) { - if (ok) *ok = 1; - return intV(&tmp); - } - n = numV(&tmp); - } - if (ok) *ok = 1; -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - if (tvisint(&tmp)) - return (lua_Integer)intV(&tmp); - n = numV(&tmp); - } -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def) -{ - cTValue *o = index2adr(L, idx); - TValue tmp; - lua_Number n; - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else if (LJ_LIKELY(tvisnum(o))) { - n = numV(o); - } else if (tvisnil(o)) { - return def; - } else { - if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) - lj_err_argt(L, idx, LUA_TNUMBER); - if (tvisint(&tmp)) - return (lua_Integer)intV(&tmp); - n = numV(&tmp); - } -#if LJ_64 - return (lua_Integer)n; -#else - return lj_num2int(n); -#endif -} - -LUA_API int lua_toboolean(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return tvistruecond(o); -} - -LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) -{ - TValue *o = index2adr(L, idx); - GCstr *s; - if (LJ_LIKELY(tvisstr(o))) { - s = strV(o); - } else if (tvisnumber(o)) { - lj_gc_check(L); - o = index2adr(L, idx); /* GC may move the stack. */ - s = lj_strfmt_number(L, o); - setstrV(L, o, s); - } else { - if (len != NULL) *len = 0; - return NULL; - } - if (len != NULL) *len = s->len; - return strdata(s); -} - -LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len) -{ - TValue *o = index2adr(L, idx); - GCstr *s; - if (LJ_LIKELY(tvisstr(o))) { - s = strV(o); - } else if (tvisnumber(o)) { - lj_gc_check(L); - o = index2adr(L, idx); /* GC may move the stack. */ - s = lj_strfmt_number(L, o); - setstrV(L, o, s); - } else { - lj_err_argt(L, idx, LUA_TSTRING); - } - if (len != NULL) *len = s->len; - return strdata(s); -} - -LUALIB_API const char *luaL_optlstring(lua_State *L, int idx, - const char *def, size_t *len) -{ - TValue *o = index2adr(L, idx); - GCstr *s; - if (LJ_LIKELY(tvisstr(o))) { - s = strV(o); - } else if (tvisnil(o)) { - if (len != NULL) *len = def ? strlen(def) : 0; - return def; - } else if (tvisnumber(o)) { - lj_gc_check(L); - o = index2adr(L, idx); /* GC may move the stack. */ - s = lj_strfmt_number(L, o); - setstrV(L, o, s); - } else { - lj_err_argt(L, idx, LUA_TSTRING); - } - if (len != NULL) *len = s->len; - return strdata(s); -} - -LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def, - const char *const lst[]) -{ - ptrdiff_t i; - const char *s = lua_tolstring(L, idx, NULL); - if (s == NULL && (s = def) == NULL) - lj_err_argt(L, idx, LUA_TSTRING); - for (i = 0; lst[i]; i++) - if (strcmp(lst[i], s) == 0) - return (int)i; - lj_err_argv(L, idx, LJ_ERR_INVOPTM, s); -} - -LUA_API size_t lua_objlen(lua_State *L, int idx) -{ - TValue *o = index2adr(L, idx); - if (tvisstr(o)) { - return strV(o)->len; - } else if (tvistab(o)) { - return (size_t)lj_tab_len(tabV(o)); - } else if (tvisudata(o)) { - return udataV(o)->len; - } else if (tvisnumber(o)) { - GCstr *s = lj_strfmt_number(L, o); - setstrV(L, o, s); - return s->len; - } else { - return 0; - } -} - -LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisfunc(o)) { - BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns)); - if (op == BC_FUNCC || op == BC_FUNCCW) - return funcV(o)->c.f; - } - return NULL; -} - -LUA_API void *lua_touserdata(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - if (tvisudata(o)) - return uddata(udataV(o)); - else if (tvislightud(o)) - return lightudV(G(L), o); - else - return NULL; -} - -LUA_API lua_State *lua_tothread(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - return (!tvisthread(o)) ? NULL : threadV(o); -} - -LUA_API const void *lua_topointer(lua_State *L, int idx) -{ - return lj_obj_ptr(G(L), index2adr(L, idx)); -} - -/* -- Stack setters (object creation) ------------------------------------- */ - -LUA_API void lua_pushnil(lua_State *L) -{ - setnilV(L->top); - incr_top(L); -} - -LUA_API void lua_pushnumber(lua_State *L, lua_Number n) -{ - setnumV(L->top, n); - if (LJ_UNLIKELY(tvisnan(L->top))) - setnanV(L->top); /* Canonicalize injected NaNs. */ - incr_top(L); -} - -LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) -{ - setintptrV(L->top, n); - incr_top(L); -} - -LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len) -{ - GCstr *s; - lj_gc_check(L); - s = lj_str_new(L, str, len); - setstrV(L, L->top, s); - incr_top(L); -} - -LUA_API void lua_pushstring(lua_State *L, const char *str) -{ - if (str == NULL) { - setnilV(L->top); - } else { - GCstr *s; - lj_gc_check(L); - s = lj_str_newz(L, str); - setstrV(L, L->top, s); - } - incr_top(L); -} - -LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt, - va_list argp) -{ - lj_gc_check(L); - return lj_strfmt_pushvf(L, fmt, argp); -} - -LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...) -{ - const char *ret; - va_list argp; - lj_gc_check(L); - va_start(argp, fmt); - ret = lj_strfmt_pushvf(L, fmt, argp); - va_end(argp); - return ret; -} - -LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n) -{ - GCfunc *fn; - lj_gc_check(L); - lj_checkapi_slot(n); - fn = lj_func_newC(L, (MSize)n, getcurrenv(L)); - fn->c.f = f; - L->top -= n; - while (n--) - copyTV(L, &fn->c.upvalue[n], L->top+n); - setfuncV(L, L->top, fn); - lj_assertL(iswhite(obj2gco(fn)), "new GC object is not white"); - incr_top(L); -} - -LUA_API void lua_pushboolean(lua_State *L, int b) -{ - setboolV(L->top, (b != 0)); - incr_top(L); -} - -LUA_API void lua_pushlightuserdata(lua_State *L, void *p) -{ -#if LJ_64 - p = lj_lightud_intern(L, p); -#endif - setrawlightudV(L->top, p); - incr_top(L); -} - -LUA_API void lua_createtable(lua_State *L, int narray, int nrec) -{ - lj_gc_check(L); - settabV(L, L->top, lj_tab_new_ah(L, narray, nrec)); - incr_top(L); -} - -LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname) -{ - GCtab *regt = tabV(registry(L)); - TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname)); - if (tvisnil(tv)) { - GCtab *mt = lj_tab_new(L, 0, 1); - settabV(L, tv, mt); - settabV(L, L->top++, mt); - lj_gc_anybarriert(L, regt); - return 1; - } else { - copyTV(L, L->top++, tv); - return 0; - } -} - -LUA_API int lua_pushthread(lua_State *L) -{ - setthreadV(L, L->top, L); - incr_top(L); - return (mainthread(G(L)) == L); -} - -LUA_API lua_State *lua_newthread(lua_State *L) -{ - lua_State *L1; - lj_gc_check(L); - L1 = lj_state_new(L); - setthreadV(L, L->top, L1); - incr_top(L); - return L1; -} - -LUA_API void *lua_newuserdata(lua_State *L, size_t size) -{ - GCudata *ud; - lj_gc_check(L); - if (size > LJ_MAX_UDATA) - lj_err_msg(L, LJ_ERR_UDATAOV); - ud = lj_udata_new(L, (MSize)size, getcurrenv(L)); - setudataV(L, L->top, ud); - incr_top(L); - return uddata(ud); -} - -LUA_API void lua_concat(lua_State *L, int n) -{ - lj_checkapi_slot(n); - if (n >= 2) { - n--; - do { - TValue *top = lj_meta_cat(L, L->top-1, -n); - if (top == NULL) { - L->top -= n; - break; - } - n -= (int)(L->top - (top - 2*LJ_FR2)); - L->top = top+2; - lj_vm_call(L, top, 1+1); - L->top -= 1+LJ_FR2; - copyTV(L, L->top-1, L->top+LJ_FR2); - } while (--n > 0); - } else if (n == 0) { /* Push empty string. */ - setstrV(L, L->top, &G(L)->strempty); - incr_top(L); - } - /* else n == 1: nothing to do. */ -} - -/* -- Object getters ------------------------------------------------------ */ - -LUA_API void lua_gettable(lua_State *L, int idx) -{ - cTValue *t = index2adr_check(L, idx); - cTValue *v = lj_meta_tget(L, t, L->top-1); - if (v == NULL) { - L->top += 2; - lj_vm_call(L, L->top-2, 1+1); - L->top -= 2+LJ_FR2; - v = L->top+1+LJ_FR2; - } - copyTV(L, L->top-1, v); -} - -LUA_API void lua_getfield(lua_State *L, int idx, const char *k) -{ - cTValue *v, *t = index2adr_check(L, idx); - TValue key; - setstrV(L, &key, lj_str_newz(L, k)); - v = lj_meta_tget(L, t, &key); - if (v == NULL) { - L->top += 2; - lj_vm_call(L, L->top-2, 1+1); - L->top -= 2+LJ_FR2; - v = L->top+1+LJ_FR2; - } - copyTV(L, L->top, v); - incr_top(L); -} - -LUA_API void lua_rawget(lua_State *L, int idx) -{ - cTValue *t = index2adr(L, idx); - lj_checkapi(tvistab(t), "stack slot %d is not a table", idx); - copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1)); -} - -LUA_API void lua_rawgeti(lua_State *L, int idx, int n) -{ - cTValue *v, *t = index2adr(L, idx); - lj_checkapi(tvistab(t), "stack slot %d is not a table", idx); - v = lj_tab_getint(tabV(t), n); - if (v) { - copyTV(L, L->top, v); - } else { - setnilV(L->top); - } - incr_top(L); -} - -LUA_API int lua_getmetatable(lua_State *L, int idx) -{ - cTValue *o = index2adr(L, idx); - GCtab *mt = NULL; - if (tvistab(o)) - mt = tabref(tabV(o)->metatable); - else if (tvisudata(o)) - mt = tabref(udataV(o)->metatable); - else - mt = tabref(basemt_obj(G(L), o)); - if (mt == NULL) - return 0; - settabV(L, L->top, mt); - incr_top(L); - return 1; -} - -LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field) -{ - if (lua_getmetatable(L, idx)) { - cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field)); - if (tv && !tvisnil(tv)) { - copyTV(L, L->top-1, tv); - return 1; - } - L->top--; - } - return 0; -} - -LUA_API void lua_getfenv(lua_State *L, int idx) -{ - cTValue *o = index2adr_check(L, idx); - if (tvisfunc(o)) { - settabV(L, L->top, tabref(funcV(o)->c.env)); - } else if (tvisudata(o)) { - settabV(L, L->top, tabref(udataV(o)->env)); - } else if (tvisthread(o)) { - settabV(L, L->top, tabref(threadV(o)->env)); - } else { - setnilV(L->top); - } - incr_top(L); -} - -LUA_API int lua_next(lua_State *L, int idx) -{ - cTValue *t = index2adr(L, idx); - int more; - lj_checkapi(tvistab(t), "stack slot %d is not a table", idx); - more = lj_tab_next(tabV(t), L->top-1, L->top-1); - if (more > 0) { - incr_top(L); /* Return new key and value slot. */ - } else if (!more) { /* End of traversal. */ - L->top--; /* Remove key slot. */ - } else { - lj_err_msg(L, LJ_ERR_NEXTIDX); - } - return more; -} - -LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n) -{ - TValue *val; - GCobj *o; - const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val, &o); - if (name) { - copyTV(L, L->top, val); - incr_top(L); - } - return name; -} - -LUA_API void *lua_upvalueid(lua_State *L, int idx, int n) -{ - GCfunc *fn = funcV(index2adr(L, idx)); - n--; - lj_checkapi((uint32_t)n < fn->l.nupvalues, "bad upvalue %d", n); - return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : - (void *)&fn->c.upvalue[n]; -} - -LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2) -{ - GCfunc *fn1 = funcV(index2adr(L, idx1)); - GCfunc *fn2 = funcV(index2adr(L, idx2)); - n1--; n2--; - lj_checkapi(isluafunc(fn1), "stack slot %d is not a Lua function", idx1); - lj_checkapi(isluafunc(fn2), "stack slot %d is not a Lua function", idx2); - lj_checkapi((uint32_t)n1 < fn1->l.nupvalues, "bad upvalue %d", n1+1); - lj_checkapi((uint32_t)n2 < fn2->l.nupvalues, "bad upvalue %d", n2+1); - setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]); - lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1])); -} - -LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname) -{ - cTValue *o = index2adr(L, idx); - if (tvisudata(o)) { - GCudata *ud = udataV(o); - cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname)); - if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable)) - return uddata(ud); - } - return NULL; /* value is not a userdata with a metatable */ -} - -LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname) -{ - void *p = luaL_testudata(L, idx, tname); - if (!p) lj_err_argtype(L, idx, tname); - return p; -} - -/* -- Object setters ------------------------------------------------------ */ - -LUA_API void lua_settable(lua_State *L, int idx) -{ - TValue *o; - cTValue *t = index2adr_check(L, idx); - lj_checkapi_slot(2); - o = lj_meta_tset(L, t, L->top-2); - if (o) { - /* NOBARRIER: lj_meta_tset ensures the table is not black. */ - L->top -= 2; - copyTV(L, o, L->top+1); - } else { - TValue *base = L->top; - copyTV(L, base+2, base-3-2*LJ_FR2); - L->top = base+3; - lj_vm_call(L, base, 0+1); - L->top -= 3+LJ_FR2; - } -} - -LUA_API void lua_setfield(lua_State *L, int idx, const char *k) -{ - TValue *o; - TValue key; - cTValue *t = index2adr_check(L, idx); - lj_checkapi_slot(1); - setstrV(L, &key, lj_str_newz(L, k)); - o = lj_meta_tset(L, t, &key); - if (o) { - /* NOBARRIER: lj_meta_tset ensures the table is not black. */ - copyTV(L, o, --L->top); - } else { - TValue *base = L->top; - copyTV(L, base+2, base-3-2*LJ_FR2); - L->top = base+3; - lj_vm_call(L, base, 0+1); - L->top -= 2+LJ_FR2; - } -} - -LUA_API void lua_rawset(lua_State *L, int idx) -{ - GCtab *t = tabV(index2adr(L, idx)); - TValue *dst, *key; - lj_checkapi_slot(2); - key = L->top-2; - dst = lj_tab_set(L, t, key); - copyTV(L, dst, key+1); - lj_gc_anybarriert(L, t); - L->top = key; -} - -LUA_API void lua_rawseti(lua_State *L, int idx, int n) -{ - GCtab *t = tabV(index2adr(L, idx)); - TValue *dst, *src; - lj_checkapi_slot(1); - dst = lj_tab_setint(L, t, n); - src = L->top-1; - copyTV(L, dst, src); - lj_gc_barriert(L, t, dst); - L->top = src; -} - -LUA_API int lua_setmetatable(lua_State *L, int idx) -{ - global_State *g; - GCtab *mt; - cTValue *o = index2adr_check(L, idx); - lj_checkapi_slot(1); - if (tvisnil(L->top-1)) { - mt = NULL; - } else { - lj_checkapi(tvistab(L->top-1), "top stack slot is not a table"); - mt = tabV(L->top-1); - } - g = G(L); - if (tvistab(o)) { - setgcref(tabV(o)->metatable, obj2gco(mt)); - if (mt) - lj_gc_objbarriert(L, tabV(o), mt); - } else if (tvisudata(o)) { - setgcref(udataV(o)->metatable, obj2gco(mt)); - if (mt) - lj_gc_objbarrier(L, udataV(o), mt); - } else { - /* Flush cache, since traces specialize to basemt. But not during __gc. */ - if (lj_trace_flushall(L)) - lj_err_caller(L, LJ_ERR_NOGCMM); - if (tvisbool(o)) { - /* NOBARRIER: basemt is a GC root. */ - setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt)); - setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt)); - } else { - /* NOBARRIER: basemt is a GC root. */ - setgcref(basemt_obj(g, o), obj2gco(mt)); - } - } - L->top--; - return 1; -} - -LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname) -{ - lua_getfield(L, LUA_REGISTRYINDEX, tname); - lua_setmetatable(L, -2); -} - -LUA_API int lua_setfenv(lua_State *L, int idx) -{ - cTValue *o = index2adr_check(L, idx); - GCtab *t; - lj_checkapi_slot(1); - lj_checkapi(tvistab(L->top-1), "top stack slot is not a table"); - t = tabV(L->top-1); - if (tvisfunc(o)) { - setgcref(funcV(o)->c.env, obj2gco(t)); - } else if (tvisudata(o)) { - setgcref(udataV(o)->env, obj2gco(t)); - } else if (tvisthread(o)) { - setgcref(threadV(o)->env, obj2gco(t)); - } else { - L->top--; - return 0; - } - lj_gc_objbarrier(L, gcV(o), t); - L->top--; - return 1; -} - -LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n) -{ - cTValue *f = index2adr(L, idx); - TValue *val; - GCobj *o; - const char *name; - lj_checkapi_slot(1); - name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val, &o); - if (name) { - L->top--; - copyTV(L, val, L->top); - lj_gc_barrier(L, o, L->top); - } - return name; -} - -/* -- Calls --------------------------------------------------------------- */ - -#if LJ_FR2 -static TValue *api_call_base(lua_State *L, int nargs) -{ - TValue *o = L->top, *base = o - nargs; - L->top = o+1; - for (; o > base; o--) copyTV(L, o, o-1); - setnilV(o); - return o+1; -} -#else -#define api_call_base(L, nargs) (L->top - (nargs)) -#endif - -LUA_API void lua_call(lua_State *L, int nargs, int nresults) -{ - lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR, - "thread called in wrong state %d", L->status); - lj_checkapi_slot(nargs+1); - lj_vm_call(L, api_call_base(L, nargs), nresults+1); -} - -LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc) -{ - global_State *g = G(L); - uint8_t oldh = hook_save(g); - ptrdiff_t ef; - int status; - lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR, - "thread called in wrong state %d", L->status); - lj_checkapi_slot(nargs+1); - if (errfunc == 0) { - ef = 0; - } else { - cTValue *o = index2adr_stack(L, errfunc); - ef = savestack(L, o); - } - status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef); - if (status) hook_restore(g, oldh); - return status; -} - -static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud) -{ - GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L)); - TValue *top = L->top; - fn->c.f = func; - setfuncV(L, top++, fn); - if (LJ_FR2) setnilV(top++); -#if LJ_64 - ud = lj_lightud_intern(L, ud); -#endif - setrawlightudV(top++, ud); - cframe_nres(L->cframe) = 1+0; /* Zero results. */ - L->top = top; - return top-1; /* Now call the newly allocated C function. */ -} - -LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud) -{ - global_State *g = G(L); - uint8_t oldh = hook_save(g); - int status; - lj_checkapi(L->status == LUA_OK || L->status == LUA_ERRERR, - "thread called in wrong state %d", L->status); - status = lj_vm_cpcall(L, func, ud, cpcall); - if (status) hook_restore(g, oldh); - return status; -} - -LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field) -{ - if (luaL_getmetafield(L, idx, field)) { - TValue *top = L->top--; - if (LJ_FR2) setnilV(top++); - copyTV(L, top++, index2adr(L, idx)); - L->top = top; - lj_vm_call(L, top-1, 1+1); - return 1; - } - return 0; -} - -/* -- Coroutine yield and resume ------------------------------------------ */ - -LUA_API int lua_isyieldable(lua_State *L) -{ - return cframe_canyield(L->cframe); -} - -LUA_API int lua_yield(lua_State *L, int nresults) -{ - void *cf = L->cframe; - global_State *g = G(L); - if (cframe_canyield(cf)) { - cf = cframe_raw(cf); - if (!hook_active(g)) { /* Regular yield: move results down if needed. */ - cTValue *f = L->top - nresults; - if (f > L->base) { - TValue *t = L->base; - while (--nresults >= 0) copyTV(L, t++, f++); - L->top = t; - } - L->cframe = NULL; - L->status = LUA_YIELD; - return -1; - } else { /* Yield from hook: add a pseudo-frame. */ - TValue *top = L->top; - hook_leave(g); - (top++)->u64 = cframe_multres(cf); - setcont(top, lj_cont_hook); - if (LJ_FR2) top++; - setframe_pc(top, cframe_pc(cf)-1); - top++; - setframe_gc(top, obj2gco(L), LJ_TTHREAD); - if (LJ_FR2) top++; - setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT); - L->top = L->base = top+1; -#if ((defined(__GNUC__) || defined(__clang__)) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND) || LJ_TARGET_WINDOWS - lj_err_throw(L, LUA_YIELD); -#else - L->cframe = NULL; - L->status = LUA_YIELD; - lj_vm_unwind_c(cf, LUA_YIELD); -#endif - } - } - lj_err_msg(L, LJ_ERR_CYIELD); - return 0; /* unreachable */ -} - -LUA_API int lua_resume(lua_State *L, int nargs) -{ - if (L->cframe == NULL && L->status <= LUA_YIELD) - return lj_vm_resume(L, - L->status == LUA_OK ? api_call_base(L, nargs) : L->top - nargs, - 0, 0); - L->top = L->base; - setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP)); - incr_top(L); - return LUA_ERRRUN; -} - -/* -- GC and memory management -------------------------------------------- */ - -LUA_API int lua_gc(lua_State *L, int what, int data) -{ - global_State *g = G(L); - int res = 0; - switch (what) { - case LUA_GCSTOP: - g->gc.threshold = LJ_MAX_MEM; - break; - case LUA_GCRESTART: - g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total; - break; - case LUA_GCCOLLECT: - lj_gc_fullgc(L); - break; - case LUA_GCCOUNT: - res = (int)(g->gc.total >> 10); - break; - case LUA_GCCOUNTB: - res = (int)(g->gc.total & 0x3ff); - break; - case LUA_GCSTEP: { - GCSize a = (GCSize)data << 10; - g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0; - while (g->gc.total >= g->gc.threshold) - if (lj_gc_step(L) > 0) { - res = 1; - break; - } - break; - } - case LUA_GCSETPAUSE: - res = (int)(g->gc.pause); - g->gc.pause = (MSize)data; - break; - case LUA_GCSETSTEPMUL: - res = (int)(g->gc.stepmul); - g->gc.stepmul = (MSize)data; - break; - case LUA_GCISRUNNING: - res = (g->gc.threshold != LJ_MAX_MEM); - break; - default: - res = -1; /* Invalid option. */ - } - return res; -} - -LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud) -{ - global_State *g = G(L); - if (ud) *ud = g->allocd; - return g->allocf; -} - -LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) -{ - global_State *g = G(L); - g->allocd = ud; - g->allocf = f; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_arch.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_arch.h deleted file mode 100644 index 5fb798d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_arch.h +++ /dev/null @@ -1,719 +0,0 @@ -/* -** Target architecture selection. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_ARCH_H -#define _LJ_ARCH_H - -#include "lua.h" - -/* -- Target definitions -------------------------------------------------- */ - -/* Target endianess. */ -#define LUAJIT_LE 0 -#define LUAJIT_BE 1 - -/* Target architectures. */ -#define LUAJIT_ARCH_X86 1 -#define LUAJIT_ARCH_x86 1 -#define LUAJIT_ARCH_X64 2 -#define LUAJIT_ARCH_x64 2 -#define LUAJIT_ARCH_ARM 3 -#define LUAJIT_ARCH_arm 3 -#define LUAJIT_ARCH_ARM64 4 -#define LUAJIT_ARCH_arm64 4 -#define LUAJIT_ARCH_PPC 5 -#define LUAJIT_ARCH_ppc 5 -#define LUAJIT_ARCH_MIPS 6 -#define LUAJIT_ARCH_mips 6 -#define LUAJIT_ARCH_MIPS32 6 -#define LUAJIT_ARCH_mips32 6 -#define LUAJIT_ARCH_MIPS64 7 -#define LUAJIT_ARCH_mips64 7 - -/* Target OS. */ -#define LUAJIT_OS_OTHER 0 -#define LUAJIT_OS_WINDOWS 1 -#define LUAJIT_OS_LINUX 2 -#define LUAJIT_OS_OSX 3 -#define LUAJIT_OS_BSD 4 -#define LUAJIT_OS_POSIX 5 - -/* Number mode. */ -#define LJ_NUMMODE_SINGLE 0 /* Single-number mode only. */ -#define LJ_NUMMODE_SINGLE_DUAL 1 /* Default to single-number mode. */ -#define LJ_NUMMODE_DUAL 2 /* Dual-number mode only. */ -#define LJ_NUMMODE_DUAL_SINGLE 3 /* Default to dual-number mode. */ - -/* -- Target detection ---------------------------------------------------- */ - -/* Select native target if no target defined. */ -#ifndef LUAJIT_TARGET - -#if defined(__i386) || defined(__i386__) || defined(_M_IX86) -#define LUAJIT_TARGET LUAJIT_ARCH_X86 -#elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) -#define LUAJIT_TARGET LUAJIT_ARCH_X64 -#elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM) -#define LUAJIT_TARGET LUAJIT_ARCH_ARM -#elif defined(__aarch64__) -#define LUAJIT_TARGET LUAJIT_ARCH_ARM64 -#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC) -#define LUAJIT_TARGET LUAJIT_ARCH_PPC -#elif defined(__mips64__) || defined(__mips64) || defined(__MIPS64__) || defined(__MIPS64) -#define LUAJIT_TARGET LUAJIT_ARCH_MIPS64 -#elif defined(__mips__) || defined(__mips) || defined(__MIPS__) || defined(__MIPS) -#define LUAJIT_TARGET LUAJIT_ARCH_MIPS32 -#else -#error "No support for this architecture (yet)" -#endif - -#endif - -/* Select native OS if no target OS defined. */ -#ifndef LUAJIT_OS - -#if defined(_WIN32) && !defined(_XBOX_VER) -#define LUAJIT_OS LUAJIT_OS_WINDOWS -#elif defined(__linux__) -#define LUAJIT_OS LUAJIT_OS_LINUX -#elif defined(__MACH__) && defined(__APPLE__) -#include "TargetConditionals.h" -#define LUAJIT_OS LUAJIT_OS_OSX -#elif (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__NetBSD__) || defined(__OpenBSD__) || \ - defined(__DragonFly__)) && !defined(__ORBIS__) && !defined(__PROSPERO__) -#define LUAJIT_OS LUAJIT_OS_BSD -#elif (defined(__sun__) && defined(__svr4__)) -#define LJ_TARGET_SOLARIS 1 -#define LUAJIT_OS LUAJIT_OS_POSIX -#elif defined(__HAIKU__) -#define LUAJIT_OS LUAJIT_OS_POSIX -#elif defined(__CYGWIN__) -#define LJ_TARGET_CYGWIN 1 -#define LUAJIT_OS LUAJIT_OS_POSIX -#elif defined(__QNX__) -#define LJ_TARGET_QNX 1 -#define LUAJIT_OS LUAJIT_OS_POSIX -#else -#define LUAJIT_OS LUAJIT_OS_OTHER -#endif - -#endif - -/* Set target OS properties. */ -#if LUAJIT_OS == LUAJIT_OS_WINDOWS -#define LJ_OS_NAME "Windows" -#elif LUAJIT_OS == LUAJIT_OS_LINUX -#define LJ_OS_NAME "Linux" -#elif LUAJIT_OS == LUAJIT_OS_OSX -#define LJ_OS_NAME "OSX" -#elif LUAJIT_OS == LUAJIT_OS_BSD -#define LJ_OS_NAME "BSD" -#elif LUAJIT_OS == LUAJIT_OS_POSIX -#define LJ_OS_NAME "POSIX" -#else -#define LJ_OS_NAME "Other" -#endif - -#define LJ_TARGET_WINDOWS (LUAJIT_OS == LUAJIT_OS_WINDOWS) -#define LJ_TARGET_LINUX (LUAJIT_OS == LUAJIT_OS_LINUX) -#define LJ_TARGET_OSX (LUAJIT_OS == LUAJIT_OS_OSX) -#define LJ_TARGET_BSD (LUAJIT_OS == LUAJIT_OS_BSD) -#define LJ_TARGET_POSIX (LUAJIT_OS > LUAJIT_OS_WINDOWS) -#define LJ_TARGET_DLOPEN LJ_TARGET_POSIX - -#if TARGET_OS_IPHONE -#define LJ_TARGET_IOS 1 -#else -#define LJ_TARGET_IOS 0 -#endif - -#ifdef __CELLOS_LV2__ -#define LJ_TARGET_PS3 1 -#define LJ_TARGET_CONSOLE 1 -#endif - -#ifdef __ORBIS__ -#define LJ_TARGET_PS4 1 -#define LJ_TARGET_CONSOLE 1 -#undef NULL -#define NULL ((void*)0) -#endif - -#ifdef __PROSPERO__ -#define LJ_TARGET_PS5 1 -#define LJ_TARGET_CONSOLE 1 -#undef NULL -#define NULL ((void*)0) -#endif - -#ifdef __psp2__ -#define LJ_TARGET_PSVITA 1 -#define LJ_TARGET_CONSOLE 1 -#endif - -#if _XBOX_VER >= 200 -#define LJ_TARGET_XBOX360 1 -#define LJ_TARGET_CONSOLE 1 -#endif - -#ifdef _DURANGO -#define LJ_TARGET_XBOXONE 1 -#define LJ_TARGET_CONSOLE 1 -#define LJ_TARGET_GC64 1 -#endif - -#ifdef __NX__ -#define LJ_TARGET_NX 1 -#define LJ_TARGET_CONSOLE 1 -#undef NULL -#define NULL ((void*)0) -#endif - -#ifdef _UWP -#define LJ_TARGET_UWP 1 -#if LUAJIT_TARGET == LUAJIT_ARCH_X64 -#define LJ_TARGET_GC64 1 -#endif -#endif - -/* -- Arch-specific settings ---------------------------------------------- */ - -/* Set target architecture properties. */ -#if LUAJIT_TARGET == LUAJIT_ARCH_X86 - -#define LJ_ARCH_NAME "x86" -#define LJ_ARCH_BITS 32 -#define LJ_ARCH_ENDIAN LUAJIT_LE -#define LJ_TARGET_X86 1 -#define LJ_TARGET_X86ORX64 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_EHRAREG 8 -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNALIGNED 1 -#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL - -#elif LUAJIT_TARGET == LUAJIT_ARCH_X64 - -#define LJ_ARCH_NAME "x64" -#define LJ_ARCH_BITS 64 -#define LJ_ARCH_ENDIAN LUAJIT_LE -#define LJ_TARGET_X64 1 -#define LJ_TARGET_X86ORX64 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_EHRAREG 16 -#define LJ_TARGET_JUMPRANGE 31 /* +-2^31 = +-2GB */ -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNALIGNED 1 -#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL -#ifndef LUAJIT_DISABLE_GC64 -#define LJ_TARGET_GC64 1 -#elif LJ_TARGET_OSX -#error "macOS requires GC64 -- don't disable it" -#endif - -#elif LUAJIT_TARGET == LUAJIT_ARCH_ARM - -#define LJ_ARCH_NAME "arm" -#define LJ_ARCH_BITS 32 -#define LJ_ARCH_ENDIAN LUAJIT_LE -#if !defined(LJ_ARCH_HASFPU) && __SOFTFP__ -#define LJ_ARCH_HASFPU 0 -#endif -#if !defined(LJ_ABI_SOFTFP) && !__ARM_PCS_VFP -#define LJ_ABI_SOFTFP 1 -#endif -#define LJ_ABI_EABI 1 -#define LJ_TARGET_ARM 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_EHRAREG 14 -#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ -#define LJ_TARGET_MASKSHIFT 0 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL - -#if __ARM_ARCH == 8 || __ARM_ARCH_8__ || __ARM_ARCH_8A__ -#define LJ_ARCH_VERSION 80 -#elif __ARM_ARCH == 7 || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH_7S__ || __ARM_ARCH_7VE__ -#define LJ_ARCH_VERSION 70 -#elif __ARM_ARCH_6T2__ -#define LJ_ARCH_VERSION 61 -#elif __ARM_ARCH == 6 || __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6K__ || __ARM_ARCH_6Z__ || __ARM_ARCH_6ZK__ -#define LJ_ARCH_VERSION 60 -#else -#define LJ_ARCH_VERSION 50 -#endif - -#elif LUAJIT_TARGET == LUAJIT_ARCH_ARM64 - -#define LJ_ARCH_BITS 64 -#if defined(__AARCH64EB__) -#define LJ_ARCH_NAME "arm64be" -#define LJ_ARCH_ENDIAN LUAJIT_BE -#else -#define LJ_ARCH_NAME "arm64" -#define LJ_ARCH_ENDIAN LUAJIT_LE -#endif -#define LJ_TARGET_ARM64 1 -#define LJ_TARGET_EHRETREG 0 -#define LJ_TARGET_EHRAREG 30 -#define LJ_TARGET_JUMPRANGE 27 /* +-2^27 = +-128MB */ -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ -#define LJ_TARGET_GC64 1 -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL - -#define LJ_ARCH_VERSION 80 - -#elif LUAJIT_TARGET == LUAJIT_ARCH_PPC - -#ifndef LJ_ARCH_ENDIAN -#if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__ -#define LJ_ARCH_ENDIAN LUAJIT_LE -#else -#define LJ_ARCH_ENDIAN LUAJIT_BE -#endif -#endif - -#if _LP64 -#define LJ_ARCH_BITS 64 -#if LJ_ARCH_ENDIAN == LUAJIT_LE -#define LJ_ARCH_NAME "ppc64le" -#else -#define LJ_ARCH_NAME "ppc64" -#endif -#else -#define LJ_ARCH_BITS 32 -#define LJ_ARCH_NAME "ppc" - -#if !defined(LJ_ARCH_HASFPU) -#if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE) -#define LJ_ARCH_HASFPU 0 -#else -#define LJ_ARCH_HASFPU 1 -#endif -#endif - -#if !defined(LJ_ABI_SOFTFP) -#if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE) -#define LJ_ABI_SOFTFP 1 -#else -#define LJ_ABI_SOFTFP 0 -#endif -#endif -#endif - -#if LJ_ABI_SOFTFP -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL -#else -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE -#endif - -#define LJ_TARGET_PPC 1 -#define LJ_TARGET_EHRETREG 3 -#define LJ_TARGET_EHRAREG 65 -#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ -#define LJ_TARGET_MASKSHIFT 0 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ - -#if LJ_TARGET_CONSOLE -#define LJ_ARCH_PPC32ON64 1 -#define LJ_ARCH_NOFFI 1 -#elif LJ_ARCH_BITS == 64 -#error "No support for PPC64" -#endif - -#if _ARCH_PWR7 -#define LJ_ARCH_VERSION 70 -#elif _ARCH_PWR6 -#define LJ_ARCH_VERSION 60 -#elif _ARCH_PWR5X -#define LJ_ARCH_VERSION 51 -#elif _ARCH_PWR5 -#define LJ_ARCH_VERSION 50 -#elif _ARCH_PWR4 -#define LJ_ARCH_VERSION 40 -#else -#define LJ_ARCH_VERSION 0 -#endif -#if _ARCH_PPCSQ -#define LJ_ARCH_SQRT 1 -#endif -#if _ARCH_PWR5X -#define LJ_ARCH_ROUND 1 -#endif -#if __PPU__ -#define LJ_ARCH_CELL 1 -#endif -#if LJ_TARGET_XBOX360 -#define LJ_ARCH_XENON 1 -#endif - -#elif LUAJIT_TARGET == LUAJIT_ARCH_MIPS32 || LUAJIT_TARGET == LUAJIT_ARCH_MIPS64 - -#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) -#if __mips_isa_rev >= 6 -#define LJ_TARGET_MIPSR6 1 -#define LJ_TARGET_UNALIGNED 1 -#endif -#if LUAJIT_TARGET == LUAJIT_ARCH_MIPS32 -#if LJ_TARGET_MIPSR6 -#define LJ_ARCH_NAME "mips32r6el" -#else -#define LJ_ARCH_NAME "mipsel" -#endif -#else -#if LJ_TARGET_MIPSR6 -#define LJ_ARCH_NAME "mips64r6el" -#else -#define LJ_ARCH_NAME "mips64el" -#endif -#endif -#define LJ_ARCH_ENDIAN LUAJIT_LE -#else -#if LUAJIT_TARGET == LUAJIT_ARCH_MIPS32 -#if LJ_TARGET_MIPSR6 -#define LJ_ARCH_NAME "mips32r6" -#else -#define LJ_ARCH_NAME "mips" -#endif -#else -#if LJ_TARGET_MIPSR6 -#define LJ_ARCH_NAME "mips64r6" -#else -#define LJ_ARCH_NAME "mips64" -#endif -#endif -#define LJ_ARCH_ENDIAN LUAJIT_BE -#endif - -#if !defined(LJ_ARCH_HASFPU) -#ifdef __mips_soft_float -#define LJ_ARCH_HASFPU 0 -#else -#define LJ_ARCH_HASFPU 1 -#endif -#endif - -#if !defined(LJ_ABI_SOFTFP) -#ifdef __mips_soft_float -#define LJ_ABI_SOFTFP 1 -#else -#define LJ_ABI_SOFTFP 0 -#endif -#endif - -#if LUAJIT_TARGET == LUAJIT_ARCH_MIPS32 -#define LJ_ARCH_BITS 32 -#define LJ_TARGET_MIPS32 1 -#else -#define LJ_ARCH_BITS 64 -#define LJ_TARGET_MIPS64 1 -#define LJ_TARGET_GC64 1 -#endif -#define LJ_TARGET_MIPS 1 -#define LJ_TARGET_EHRETREG 4 -#define LJ_TARGET_EHRAREG 31 -#define LJ_TARGET_JUMPRANGE 27 /* 2*2^27 = 256MB-aligned region */ -#define LJ_TARGET_MASKSHIFT 1 -#define LJ_TARGET_MASKROT 1 -#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ -#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL - -#if LJ_TARGET_MIPSR6 -#define LJ_ARCH_VERSION 60 -#elif _MIPS_ARCH_MIPS32R2 || _MIPS_ARCH_MIPS64R2 -#define LJ_ARCH_VERSION 20 -#else -#define LJ_ARCH_VERSION 10 -#endif - -#else -#error "No target architecture defined" -#endif - -/* -- Checks for requirements --------------------------------------------- */ - -/* Check for minimum required compiler versions. */ -#if defined(__GNUC__) -#if LJ_TARGET_X86 -#if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4) -#error "Need at least GCC 3.4 or newer" -#endif -#elif LJ_TARGET_X64 -#if __GNUC__ < 4 -#error "Need at least GCC 4.0 or newer" -#endif -#elif LJ_TARGET_ARM -#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 2) -#error "Need at least GCC 4.2 or newer" -#endif -#elif LJ_TARGET_ARM64 -#if __clang__ -#if ((__clang_major__ < 3) || ((__clang_major__ == 3) && __clang_minor__ < 5)) && !defined(__NX_TOOLCHAIN_MAJOR__) -#error "Need at least Clang 3.5 or newer" -#endif -#else -#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 8) -#error "Need at least GCC 4.8 or newer" -#endif -#endif -#elif !LJ_TARGET_PS3 -#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 3) -#error "Need at least GCC 4.3 or newer" -#endif -#endif -#endif - -/* Check target-specific constraints. */ -#ifndef _BUILDVM_H -#if LJ_TARGET_X64 -#if __USING_SJLJ_EXCEPTIONS__ -#error "Need a C compiler with native exception handling on x64" -#endif -#elif LJ_TARGET_ARM -#if defined(__ARMEB__) -#error "No support for big-endian ARM" -#endif -#if __ARM_ARCH_6M__ || __ARM_ARCH_7M__ || __ARM_ARCH_7EM__ -#error "No support for Cortex-M CPUs" -#endif -#if !(__ARM_EABI__ || LJ_TARGET_IOS) -#error "Only ARM EABI or iOS 3.0+ ABI is supported" -#endif -#elif LJ_TARGET_ARM64 -#if defined(_ILP32) -#error "No support for ILP32 model on ARM64" -#endif -#elif LJ_TARGET_PPC -#if defined(_LITTLE_ENDIAN) && (!defined(_BYTE_ORDER) || (_BYTE_ORDER == _LITTLE_ENDIAN)) -#error "No support for little-endian PPC32" -#endif -#if defined(__NO_FPRS__) && !defined(_SOFT_FLOAT) -#error "No support for PPC/e500 anymore (use LuaJIT 2.0)" -#endif -#elif LJ_TARGET_MIPS32 -#if !((defined(_MIPS_SIM_ABI32) && _MIPS_SIM == _MIPS_SIM_ABI32) || (defined(_ABIO32) && _MIPS_SIM == _ABIO32)) -#error "Only o32 ABI supported for MIPS32" -#endif -#if LJ_TARGET_MIPSR6 -/* Not that useful, since most available r6 CPUs are 64 bit. */ -#error "No support for MIPS32R6" -#endif -#elif LJ_TARGET_MIPS64 -#if !((defined(_MIPS_SIM_ABI64) && _MIPS_SIM == _MIPS_SIM_ABI64) || (defined(_ABI64) && _MIPS_SIM == _ABI64)) -/* MIPS32ON64 aka n32 ABI support might be desirable, but difficult. */ -#error "Only n64 ABI supported for MIPS64" -#endif -#endif -#endif - -/* -- Derived defines ----------------------------------------------------- */ - -/* Enable or disable the dual-number mode for the VM. */ -#if (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE && LUAJIT_NUMMODE == 2) || \ - (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL && LUAJIT_NUMMODE == 1) -#error "No support for this number mode on this architecture" -#endif -#if LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL || \ - (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL_SINGLE && LUAJIT_NUMMODE != 1) || \ - (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE_DUAL && LUAJIT_NUMMODE == 2) -#define LJ_DUALNUM 1 -#else -#define LJ_DUALNUM 0 -#endif - -#if LJ_TARGET_IOS || LJ_TARGET_CONSOLE -/* Runtime code generation is restricted on iOS. Complain to Apple, not me. */ -/* Ditto for the consoles. Complain to Sony or MS, not me. */ -#ifndef LUAJIT_ENABLE_JIT -#define LJ_OS_NOJIT 1 -#endif -#endif - -/* 64 bit GC references. */ -#if LJ_TARGET_GC64 -#define LJ_GC64 1 -#else -#define LJ_GC64 0 -#endif - -/* 2-slot frame info. */ -#if LJ_GC64 -#define LJ_FR2 1 -#else -#define LJ_FR2 0 -#endif - -/* Disable or enable the JIT compiler. */ -#if defined(LUAJIT_DISABLE_JIT) || defined(LJ_ARCH_NOJIT) || defined(LJ_OS_NOJIT) -#define LJ_HASJIT 0 -#else -#define LJ_HASJIT 1 -#endif - -/* Disable or enable the FFI extension. */ -#if defined(LUAJIT_DISABLE_FFI) || defined(LJ_ARCH_NOFFI) -#define LJ_HASFFI 0 -#else -#define LJ_HASFFI 1 -#endif - -/* Disable or enable the string buffer extension. */ -#if defined(LUAJIT_DISABLE_BUFFER) -#define LJ_HASBUFFER 0 -#else -#define LJ_HASBUFFER 1 -#endif - -#if defined(LUAJIT_DISABLE_PROFILE) -#define LJ_HASPROFILE 0 -#elif LJ_TARGET_POSIX -#define LJ_HASPROFILE 1 -#define LJ_PROFILE_SIGPROF 1 -#elif LJ_TARGET_PS3 -#define LJ_HASPROFILE 1 -#define LJ_PROFILE_PTHREAD 1 -#elif LJ_TARGET_WINDOWS || LJ_TARGET_XBOX360 -#define LJ_HASPROFILE 1 -#define LJ_PROFILE_WTHREAD 1 -#else -#define LJ_HASPROFILE 0 -#endif - -#ifndef LJ_ARCH_HASFPU -#define LJ_ARCH_HASFPU 1 -#endif -#ifndef LJ_ABI_SOFTFP -#define LJ_ABI_SOFTFP 0 -#endif -#define LJ_SOFTFP (!LJ_ARCH_HASFPU) -#define LJ_SOFTFP32 (LJ_SOFTFP && LJ_32) - -#if LJ_ARCH_ENDIAN == LUAJIT_BE -#define LJ_LE 0 -#define LJ_BE 1 -#define LJ_ENDIAN_SELECT(le, be) be -#define LJ_ENDIAN_LOHI(lo, hi) hi lo -#else -#define LJ_LE 1 -#define LJ_BE 0 -#define LJ_ENDIAN_SELECT(le, be) le -#define LJ_ENDIAN_LOHI(lo, hi) lo hi -#endif - -#if LJ_ARCH_BITS == 32 -#define LJ_32 1 -#define LJ_64 0 -#else -#define LJ_32 0 -#define LJ_64 1 -#endif - -#ifndef LJ_TARGET_UNALIGNED -#define LJ_TARGET_UNALIGNED 0 -#endif - -#ifndef LJ_PAGESIZE -#define LJ_PAGESIZE 4096 -#endif - -/* Various workarounds for embedded operating systems or weak C runtimes. */ -#if defined(__ANDROID__) || defined(__symbian__) || LJ_TARGET_XBOX360 || LJ_TARGET_WINDOWS -#define LUAJIT_NO_LOG2 -#endif -#if LJ_TARGET_CONSOLE || (LJ_TARGET_IOS && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0) -#define LJ_NO_SYSTEM 1 -#endif - -#if LJ_TARGET_WINDOWS || LJ_TARGET_CYGWIN -#define LJ_ABI_WIN 1 -#else -#define LJ_ABI_WIN 0 -#endif - -#if LJ_TARGET_WINDOWS -#if LJ_TARGET_UWP -#define LJ_WIN_VALLOC VirtualAllocFromApp -#define LJ_WIN_VPROTECT VirtualProtectFromApp -extern void *LJ_WIN_LOADLIBA(const char *path); -#else -#define LJ_WIN_VALLOC VirtualAlloc -#define LJ_WIN_VPROTECT VirtualProtect -#define LJ_WIN_LOADLIBA(path) LoadLibraryExA((path), NULL, 0) -#endif -#endif - -#if defined(LUAJIT_NO_UNWIND) || __GNU_COMPACT_EH__ || defined(__symbian__) || LJ_TARGET_IOS || LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 -#define LJ_NO_UNWIND 1 -#endif - -#if !LJ_NO_UNWIND && !defined(LUAJIT_UNWIND_INTERNAL) && (LJ_ABI_WIN || (defined(LUAJIT_UNWIND_EXTERNAL) && (defined(__GNUC__) || defined(__clang__)))) -#define LJ_UNWIND_EXT 1 -#else -#define LJ_UNWIND_EXT 0 -#endif - -#if LJ_UNWIND_EXT && LJ_HASJIT && !LJ_TARGET_ARM && !(LJ_ABI_WIN && LJ_TARGET_X86) -#define LJ_UNWIND_JIT 1 -#else -#define LJ_UNWIND_JIT 0 -#endif - -/* Compatibility with Lua 5.1 vs. 5.2. */ -#ifdef LUAJIT_ENABLE_LUA52COMPAT -#define LJ_52 1 -#else -#define LJ_52 0 -#endif - -/* -- VM security --------------------------------------------------------- */ - -/* Don't make any changes here. Instead build with: -** make "XCFLAGS=-DLUAJIT_SECURITY_flag=value" -** -** Important note to distro maintainers: DO NOT change the defaults for a -** regular distro build -- neither upwards, nor downwards! -** These build-time configurable security flags are intended for embedders -** who may have specific needs wrt. security vs. performance. -*/ - -/* Security defaults. */ -#ifndef LUAJIT_SECURITY_PRNG -/* PRNG init: 0 = fixed/insecure, 1 = secure from OS. */ -#define LUAJIT_SECURITY_PRNG 1 -#endif - -#ifndef LUAJIT_SECURITY_STRHASH -/* String hash: 0 = sparse only, 1 = sparse + dense. */ -#define LUAJIT_SECURITY_STRHASH 1 -#endif - -#ifndef LUAJIT_SECURITY_STRID -/* String IDs: 0 = linear, 1 = reseed < 255, 2 = reseed < 15, 3 = random. */ -#define LUAJIT_SECURITY_STRID 1 -#endif - -#ifndef LUAJIT_SECURITY_MCODE -/* Machine code page protection: 0 = insecure RWX, 1 = secure RW^X. */ -#define LUAJIT_SECURITY_MCODE 1 -#endif - -#define LJ_SECURITY_MODE \ - ( 0u \ - | ((LUAJIT_SECURITY_PRNG & 3) << 0) \ - | ((LUAJIT_SECURITY_STRHASH & 3) << 2) \ - | ((LUAJIT_SECURITY_STRID & 3) << 4) \ - | ((LUAJIT_SECURITY_MCODE & 3) << 6) \ - ) -#define LJ_SECURITY_MODESTRING \ - "\004prng\007strhash\005strid\005mcode" - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.c deleted file mode 100644 index 6f5e0c4..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.c +++ /dev/null @@ -1,2571 +0,0 @@ -/* -** IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_asm_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_asm.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_target.h" - -#ifdef LUA_USE_ASSERT -#include -#endif - -/* -- Assembler state and common macros ----------------------------------- */ - -/* Assembler state. */ -typedef struct ASMState { - RegCost cost[RID_MAX]; /* Reference and blended allocation cost for regs. */ - - MCode *mcp; /* Current MCode pointer (grows down). */ - MCode *mclim; /* Lower limit for MCode memory + red zone. */ -#ifdef LUA_USE_ASSERT - MCode *mcp_prev; /* Red zone overflow check. */ -#endif - - IRIns *ir; /* Copy of pointer to IR instructions/constants. */ - jit_State *J; /* JIT compiler state. */ - -#if LJ_TARGET_X86ORX64 - x86ModRM mrm; /* Fused x86 address operand. */ -#endif - - RegSet freeset; /* Set of free registers. */ - RegSet modset; /* Set of registers modified inside the loop. */ - RegSet weakset; /* Set of weakly referenced registers. */ - RegSet phiset; /* Set of PHI registers. */ - - uint32_t flags; /* Copy of JIT compiler flags. */ - int loopinv; /* Loop branch inversion (0:no, 1:yes, 2:yes+CC_P). */ - - int32_t evenspill; /* Next even spill slot. */ - int32_t oddspill; /* Next odd spill slot (or 0). */ - - IRRef curins; /* Reference of current instruction. */ - IRRef stopins; /* Stop assembly before hitting this instruction. */ - IRRef orignins; /* Original T->nins. */ - - IRRef snapref; /* Current snapshot is active after this reference. */ - IRRef snaprename; /* Rename highwater mark for snapshot check. */ - SnapNo snapno; /* Current snapshot number. */ - SnapNo loopsnapno; /* Loop snapshot number. */ - int snapalloc; /* Current snapshot needs allocation. */ - BloomFilter snapfilt1, snapfilt2; /* Filled with snapshot refs. */ - - IRRef fuseref; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */ - IRRef sectref; /* Section base reference (loopref or 0). */ - IRRef loopref; /* Reference of LOOP instruction (or 0). */ - - BCReg topslot; /* Number of slots for stack check (unless 0). */ - int32_t gcsteps; /* Accumulated number of GC steps (per section). */ - - GCtrace *T; /* Trace to assemble. */ - GCtrace *parent; /* Parent trace (or NULL). */ - - MCode *mcbot; /* Bottom of reserved MCode. */ - MCode *mctop; /* Top of generated MCode. */ - MCode *mctoporig; /* Original top of generated MCode. */ - MCode *mcloop; /* Pointer to loop MCode (or NULL). */ - MCode *invmcp; /* Points to invertible loop branch (or NULL). */ - MCode *flagmcp; /* Pending opportunity to merge flag setting ins. */ - MCode *realign; /* Realign loop if not NULL. */ - -#ifdef RID_NUM_KREF - intptr_t krefk[RID_NUM_KREF]; -#endif - IRRef1 phireg[RID_MAX]; /* PHI register references. */ - uint16_t parentmap[LJ_MAX_JSLOTS]; /* Parent instruction to RegSP map. */ -} ASMState; - -#ifdef LUA_USE_ASSERT -#define lj_assertA(c, ...) lj_assertG_(J2G(as->J), (c), __VA_ARGS__) -#else -#define lj_assertA(c, ...) ((void)as) -#endif - -#define IR(ref) (&as->ir[(ref)]) - -#define ASMREF_TMP1 REF_TRUE /* Temp. register. */ -#define ASMREF_TMP2 REF_FALSE /* Temp. register. */ -#define ASMREF_L REF_NIL /* Stores register for L. */ - -/* Check for variant to invariant references. */ -#define iscrossref(as, ref) ((ref) < as->sectref) - -/* Inhibit memory op fusion from variant to invariant references. */ -#define FUSE_DISABLED (~(IRRef)0) -#define mayfuse(as, ref) ((ref) > as->fuseref) -#define neverfuse(as) (as->fuseref == FUSE_DISABLED) -#define canfuse(as, ir) (!neverfuse(as) && !irt_isphi((ir)->t)) -#define opisfusableload(o) \ - ((o) == IR_ALOAD || (o) == IR_HLOAD || (o) == IR_ULOAD || \ - (o) == IR_FLOAD || (o) == IR_XLOAD || (o) == IR_SLOAD || (o) == IR_VLOAD) - -/* Sparse limit checks using a red zone before the actual limit. */ -#define MCLIM_REDZONE 64 - -static LJ_NORET LJ_NOINLINE void asm_mclimit(ASMState *as) -{ - lj_mcode_limiterr(as->J, (size_t)(as->mctop - as->mcp + 4*MCLIM_REDZONE)); -} - -static LJ_AINLINE void checkmclim(ASMState *as) -{ -#ifdef LUA_USE_ASSERT - if (as->mcp + MCLIM_REDZONE < as->mcp_prev) { - IRIns *ir = IR(as->curins+1); - lj_assertA(0, "red zone overflow: %p IR %04d %02d %04d %04d\n", as->mcp, - as->curins+1-REF_BIAS, ir->o, ir->op1-REF_BIAS, ir->op2-REF_BIAS); - } -#endif - if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as); -#ifdef LUA_USE_ASSERT - as->mcp_prev = as->mcp; -#endif -} - -#ifdef RID_NUM_KREF -#define ra_iskref(ref) ((ref) < RID_NUM_KREF) -#define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref))) -#define ra_krefk(as, ref) (as->krefk[(ref)]) - -static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, intptr_t k) -{ - IRRef ref = (IRRef)(r - RID_MIN_KREF); - as->krefk[ref] = k; - as->cost[r] = REGCOST(ref, ref); -} - -#else -#define ra_iskref(ref) 0 -#define ra_krefreg(ref) RID_MIN_GPR -#define ra_krefk(as, ref) 0 -#endif - -/* Arch-specific field offsets. */ -static const uint8_t field_ofs[IRFL__MAX+1] = { -#define FLOFS(name, ofs) (uint8_t)(ofs), -IRFLDEF(FLOFS) -#undef FLOFS - 0 -}; - -/* -- Target-specific instruction emitter --------------------------------- */ - -#if LJ_TARGET_X86ORX64 -#include "lj_emit_x86.h" -#elif LJ_TARGET_ARM -#include "lj_emit_arm.h" -#elif LJ_TARGET_ARM64 -#include "lj_emit_arm64.h" -#elif LJ_TARGET_PPC -#include "lj_emit_ppc.h" -#elif LJ_TARGET_MIPS -#include "lj_emit_mips.h" -#else -#error "Missing instruction emitter for target CPU" -#endif - -/* Generic load/store of register from/to stack slot. */ -#define emit_spload(as, ir, r, ofs) \ - emit_loadofs(as, ir, (r), RID_SP, (ofs)) -#define emit_spstore(as, ir, r, ofs) \ - emit_storeofs(as, ir, (r), RID_SP, (ofs)) - -/* -- Register allocator debugging ---------------------------------------- */ - -/* #define LUAJIT_DEBUG_RA */ - -#ifdef LUAJIT_DEBUG_RA - -#include -#include - -#define RIDNAME(name) #name, -static const char *const ra_regname[] = { - GPRDEF(RIDNAME) - FPRDEF(RIDNAME) - VRIDDEF(RIDNAME) - NULL -}; -#undef RIDNAME - -static char ra_dbg_buf[65536]; -static char *ra_dbg_p; -static char *ra_dbg_merge; -static MCode *ra_dbg_mcp; - -static void ra_dstart(void) -{ - ra_dbg_p = ra_dbg_buf; - ra_dbg_merge = NULL; - ra_dbg_mcp = NULL; -} - -static void ra_dflush(void) -{ - fwrite(ra_dbg_buf, 1, (size_t)(ra_dbg_p-ra_dbg_buf), stdout); - ra_dstart(); -} - -static void ra_dprintf(ASMState *as, const char *fmt, ...) -{ - char *p; - va_list argp; - va_start(argp, fmt); - p = ra_dbg_mcp == as->mcp ? ra_dbg_merge : ra_dbg_p; - ra_dbg_mcp = NULL; - p += sprintf(p, "%08x \e[36m%04d ", (uintptr_t)as->mcp, as->curins-REF_BIAS); - for (;;) { - const char *e = strchr(fmt, '$'); - if (e == NULL) break; - memcpy(p, fmt, (size_t)(e-fmt)); - p += e-fmt; - if (e[1] == 'r') { - Reg r = va_arg(argp, Reg) & RID_MASK; - if (r <= RID_MAX) { - const char *q; - for (q = ra_regname[r]; *q; q++) - *p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q; - } else { - *p++ = '?'; - lj_assertA(0, "bad register %d for debug format \"%s\"", r, fmt); - } - } else if (e[1] == 'f' || e[1] == 'i') { - IRRef ref; - if (e[1] == 'f') - ref = va_arg(argp, IRRef); - else - ref = va_arg(argp, IRIns *) - as->ir; - if (ref >= REF_BIAS) - p += sprintf(p, "%04d", ref - REF_BIAS); - else - p += sprintf(p, "K%03d", REF_BIAS - ref); - } else if (e[1] == 's') { - uint32_t slot = va_arg(argp, uint32_t); - p += sprintf(p, "[sp+0x%x]", sps_scale(slot)); - } else if (e[1] == 'x') { - p += sprintf(p, "%08x", va_arg(argp, int32_t)); - } else { - lj_assertA(0, "bad debug format code"); - } - fmt = e+2; - } - va_end(argp); - while (*fmt) - *p++ = *fmt++; - *p++ = '\e'; *p++ = '['; *p++ = 'm'; *p++ = '\n'; - if (p > ra_dbg_buf+sizeof(ra_dbg_buf)-256) { - fwrite(ra_dbg_buf, 1, (size_t)(p-ra_dbg_buf), stdout); - p = ra_dbg_buf; - } - ra_dbg_p = p; -} - -#define RA_DBG_START() ra_dstart() -#define RA_DBG_FLUSH() ra_dflush() -#define RA_DBG_REF() \ - do { char *_p = ra_dbg_p; ra_dprintf(as, ""); \ - ra_dbg_merge = _p; ra_dbg_mcp = as->mcp; } while (0) -#define RA_DBGX(x) ra_dprintf x - -#else -#define RA_DBG_START() ((void)0) -#define RA_DBG_FLUSH() ((void)0) -#define RA_DBG_REF() ((void)0) -#define RA_DBGX(x) ((void)0) -#endif - -/* -- Register allocator -------------------------------------------------- */ - -#define ra_free(as, r) rset_set(as->freeset, (r)) -#define ra_modified(as, r) rset_set(as->modset, (r)) -#define ra_weak(as, r) rset_set(as->weakset, (r)) -#define ra_noweak(as, r) rset_clear(as->weakset, (r)) - -#define ra_used(ir) (ra_hasreg((ir)->r) || ra_hasspill((ir)->s)) - -/* Setup register allocator. */ -static void ra_setup(ASMState *as) -{ - Reg r; - /* Initially all regs (except the stack pointer) are free for use. */ - as->freeset = RSET_INIT; - as->modset = RSET_EMPTY; - as->weakset = RSET_EMPTY; - as->phiset = RSET_EMPTY; - memset(as->phireg, 0, sizeof(as->phireg)); - for (r = RID_MIN_GPR; r < RID_MAX; r++) - as->cost[r] = REGCOST(~0u, 0u); -} - -/* Rematerialize constants. */ -static Reg ra_rematk(ASMState *as, IRRef ref) -{ - IRIns *ir; - Reg r; - if (ra_iskref(ref)) { - r = ra_krefreg(ref); - lj_assertA(!rset_test(as->freeset, r), "rematk of free reg %d", r); - ra_free(as, r); - ra_modified(as, r); -#if LJ_64 - emit_loadu64(as, r, ra_krefk(as, ref)); -#else - emit_loadi(as, r, ra_krefk(as, ref)); -#endif - return r; - } - ir = IR(ref); - r = ir->r; - lj_assertA(ra_hasreg(r), "rematk of K%03d has no reg", REF_BIAS - ref); - lj_assertA(!ra_hasspill(ir->s), - "rematk of K%03d has spill slot [%x]", REF_BIAS - ref, ir->s); - ra_free(as, r); - ra_modified(as, r); - ir->r = RID_INIT; /* Do not keep any hint. */ - RA_DBGX((as, "remat $i $r", ir, r)); -#if !LJ_SOFTFP32 - if (ir->o == IR_KNUM) { - emit_loadk64(as, r, ir); - } else -#endif - if (emit_canremat(REF_BASE) && ir->o == IR_BASE) { - ra_sethint(ir->r, RID_BASE); /* Restore BASE register hint. */ - emit_getgl(as, r, jit_base); - } else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) { - /* REF_NIL stores ASMREF_L register. */ - lj_assertA(irt_isnil(ir->t), "rematk of bad ASMREF_L"); - emit_getgl(as, r, cur_L); -#if LJ_64 - } else if (ir->o == IR_KINT64) { - emit_loadu64(as, r, ir_kint64(ir)->u64); -#if LJ_GC64 - } else if (ir->o == IR_KGC) { - emit_loadu64(as, r, (uintptr_t)ir_kgc(ir)); - } else if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { - emit_loadu64(as, r, (uintptr_t)ir_kptr(ir)); -#endif -#endif - } else { - lj_assertA(ir->o == IR_KINT || ir->o == IR_KGC || - ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL, - "rematk of bad IR op %d", ir->o); - emit_loadi(as, r, ir->i); - } - return r; -} - -/* Force a spill. Allocate a new spill slot if needed. */ -static int32_t ra_spill(ASMState *as, IRIns *ir) -{ - int32_t slot = ir->s; - lj_assertA(ir >= as->ir + REF_TRUE, - "spill of K%03d", REF_BIAS - (int)(ir - as->ir)); - if (!ra_hasspill(slot)) { - if (irt_is64(ir->t)) { - slot = as->evenspill; - as->evenspill += 2; - } else if (as->oddspill) { - slot = as->oddspill; - as->oddspill = 0; - } else { - slot = as->evenspill; - as->oddspill = slot+1; - as->evenspill += 2; - } - if (as->evenspill > 256) - lj_trace_err(as->J, LJ_TRERR_SPILLOV); - ir->s = (uint8_t)slot; - } - return sps_scale(slot); -} - -/* Release the temporarily allocated register in ASMREF_TMP1/ASMREF_TMP2. */ -static Reg ra_releasetmp(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - Reg r = ir->r; - lj_assertA(ra_hasreg(r), "release of TMP%d has no reg", ref-ASMREF_TMP1+1); - lj_assertA(!ra_hasspill(ir->s), - "release of TMP%d has spill slot [%x]", ref-ASMREF_TMP1+1, ir->s); - ra_free(as, r); - ra_modified(as, r); - ir->r = RID_INIT; - return r; -} - -/* Restore a register (marked as free). Rematerialize or force a spill. */ -static Reg ra_restore(ASMState *as, IRRef ref) -{ - if (emit_canremat(ref)) { - return ra_rematk(as, ref); - } else { - IRIns *ir = IR(ref); - int32_t ofs = ra_spill(as, ir); /* Force a spill slot. */ - Reg r = ir->r; - lj_assertA(ra_hasreg(r), "restore of IR %04d has no reg", ref - REF_BIAS); - ra_sethint(ir->r, r); /* Keep hint. */ - ra_free(as, r); - if (!rset_test(as->weakset, r)) { /* Only restore non-weak references. */ - ra_modified(as, r); - RA_DBGX((as, "restore $i $r", ir, r)); - emit_spload(as, ir, r, ofs); - } - return r; - } -} - -/* Save a register to a spill slot. */ -static void ra_save(ASMState *as, IRIns *ir, Reg r) -{ - RA_DBGX((as, "save $i $r", ir, r)); - emit_spstore(as, ir, r, sps_scale(ir->s)); -} - -#define MINCOST(name) \ - if (rset_test(RSET_ALL, RID_##name) && \ - LJ_LIKELY(allow&RID2RSET(RID_##name)) && as->cost[RID_##name] < cost) \ - cost = as->cost[RID_##name]; - -/* Evict the register with the lowest cost, forcing a restore. */ -static Reg ra_evict(ASMState *as, RegSet allow) -{ - IRRef ref; - RegCost cost = ~(RegCost)0; - lj_assertA(allow != RSET_EMPTY, "evict from empty set"); - if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) { - GPRDEF(MINCOST) - } else { - FPRDEF(MINCOST) - } - ref = regcost_ref(cost); - lj_assertA(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins), - "evict of out-of-range IR %04d", ref - REF_BIAS); - /* Preferably pick any weak ref instead of a non-weak, non-const ref. */ - if (!irref_isk(ref) && (as->weakset & allow)) { - IRIns *ir = IR(ref); - if (!rset_test(as->weakset, ir->r)) - ref = regcost_ref(as->cost[rset_pickbot((as->weakset & allow))]); - } - return ra_restore(as, ref); -} - -/* Pick any register (marked as free). Evict on-demand. */ -static Reg ra_pick(ASMState *as, RegSet allow) -{ - RegSet pick = as->freeset & allow; - if (!pick) - return ra_evict(as, allow); - else - return rset_picktop(pick); -} - -/* Get a scratch register (marked as free). */ -static Reg ra_scratch(ASMState *as, RegSet allow) -{ - Reg r = ra_pick(as, allow); - ra_modified(as, r); - RA_DBGX((as, "scratch $r", r)); - return r; -} - -/* Evict all registers from a set (if not free). */ -static void ra_evictset(ASMState *as, RegSet drop) -{ - RegSet work; - as->modset |= drop; -#if !LJ_SOFTFP - work = (drop & ~as->freeset) & RSET_FPR; - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } -#endif - work = (drop & ~as->freeset); - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } -} - -/* Evict (rematerialize) all registers allocated to constants. */ -static void ra_evictk(ASMState *as) -{ - RegSet work; -#if !LJ_SOFTFP - work = ~as->freeset & RSET_FPR; - while (work) { - Reg r = rset_pickbot(work); - IRRef ref = regcost_ref(as->cost[r]); - if (emit_canremat(ref) && irref_isk(ref)) { - ra_rematk(as, ref); - checkmclim(as); - } - rset_clear(work, r); - } -#endif - work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_pickbot(work); - IRRef ref = regcost_ref(as->cost[r]); - if (emit_canremat(ref) && irref_isk(ref)) { - ra_rematk(as, ref); - checkmclim(as); - } - rset_clear(work, r); - } -} - -#ifdef RID_NUM_KREF -/* Allocate a register for a constant. */ -static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow) -{ - /* First try to find a register which already holds the same constant. */ - RegSet pick, work = ~as->freeset & RSET_GPR; - Reg r; - while (work) { - IRRef ref; - r = rset_pickbot(work); - ref = regcost_ref(as->cost[r]); -#if LJ_64 - if (ref < ASMREF_L) { - if (ra_iskref(ref)) { - if (k == ra_krefk(as, ref)) - return r; - } else { - IRIns *ir = IR(ref); - if ((ir->o == IR_KINT64 && k == (int64_t)ir_kint64(ir)->u64) || -#if LJ_GC64 - (ir->o == IR_KINT && k == ir->i) || - (ir->o == IR_KGC && k == (intptr_t)ir_kgc(ir)) || - ((ir->o == IR_KPTR || ir->o == IR_KKPTR) && - k == (intptr_t)ir_kptr(ir)) -#else - (ir->o != IR_KINT64 && k == ir->i) -#endif - ) - return r; - } - } -#else - if (ref < ASMREF_L && - k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i)) - return r; -#endif - rset_clear(work, r); - } - pick = as->freeset & allow; - if (pick) { - /* Constants should preferably get unmodified registers. */ - if ((pick & ~as->modset)) - pick &= ~as->modset; - r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ - } else { - r = ra_evict(as, allow); - } - RA_DBGX((as, "allock $x $r", k, r)); - ra_setkref(as, r, k); - rset_clear(as->freeset, r); - ra_noweak(as, r); - return r; -} - -/* Allocate a specific register for a constant. */ -static void ra_allockreg(ASMState *as, intptr_t k, Reg r) -{ - Reg kr = ra_allock(as, k, RID2RSET(r)); - if (kr != r) { - IRIns irdummy; - irdummy.t.irt = IRT_INT; - ra_scratch(as, RID2RSET(r)); - emit_movrr(as, &irdummy, r, kr); - } -} -#else -#define ra_allockreg(as, k, r) emit_loadi(as, (r), (k)) -#endif - -/* Allocate a register for ref from the allowed set of registers. -** Note: this function assumes the ref does NOT have a register yet! -** Picks an optimal register, sets the cost and marks the register as non-free. -*/ -static Reg ra_allocref(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - RegSet pick = as->freeset & allow; - Reg r; - lj_assertA(ra_noreg(ir->r), - "IR %04d already has reg %d", ref - REF_BIAS, ir->r); - if (pick) { - /* First check register hint from propagation or PHI. */ - if (ra_hashint(ir->r)) { - r = ra_gethint(ir->r); - if (rset_test(pick, r)) /* Use hint register if possible. */ - goto found; - /* Rematerialization is cheaper than missing a hint. */ - if (rset_test(allow, r) && emit_canremat(regcost_ref(as->cost[r]))) { - ra_rematk(as, regcost_ref(as->cost[r])); - goto found; - } - RA_DBGX((as, "hintmiss $f $r", ref, r)); - } - /* Invariants should preferably get unmodified registers. */ - if (ref < as->loopref && !irt_isphi(ir->t)) { - if ((pick & ~as->modset)) - pick &= ~as->modset; - r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ - } else { - /* We've got plenty of regs, so get callee-save regs if possible. */ - if (RID_NUM_GPR > 8 && (pick & ~RSET_SCRATCH)) - pick &= ~RSET_SCRATCH; - r = rset_picktop(pick); - } - } else { - r = ra_evict(as, allow); - } -found: - RA_DBGX((as, "alloc $f $r", ref, r)); - ir->r = (uint8_t)r; - rset_clear(as->freeset, r); - ra_noweak(as, r); - as->cost[r] = REGCOST_REF_T(ref, irt_t(ir->t)); - return r; -} - -/* Allocate a register on-demand. */ -static Reg ra_alloc1(ASMState *as, IRRef ref, RegSet allow) -{ - Reg r = IR(ref)->r; - /* Note: allow is ignored if the register is already allocated. */ - if (ra_noreg(r)) r = ra_allocref(as, ref, allow); - ra_noweak(as, r); - return r; -} - -/* Add a register rename to the IR. */ -static void ra_addrename(ASMState *as, Reg down, IRRef ref, SnapNo snapno) -{ - IRRef ren; - lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, snapno); - ren = tref_ref(lj_ir_emit(as->J)); - as->J->cur.ir[ren].r = (uint8_t)down; - as->J->cur.ir[ren].s = SPS_NONE; -} - -/* Rename register allocation and emit move. */ -static void ra_rename(ASMState *as, Reg down, Reg up) -{ - IRRef ref = regcost_ref(as->cost[up] = as->cost[down]); - IRIns *ir = IR(ref); - ir->r = (uint8_t)up; - as->cost[down] = 0; - lj_assertA((down < RID_MAX_GPR) == (up < RID_MAX_GPR), - "rename between GPR/FPR %d and %d", down, up); - lj_assertA(!rset_test(as->freeset, down), "rename from free reg %d", down); - lj_assertA(rset_test(as->freeset, up), "rename to non-free reg %d", up); - ra_free(as, down); /* 'down' is free ... */ - ra_modified(as, down); - rset_clear(as->freeset, up); /* ... and 'up' is now allocated. */ - ra_noweak(as, up); - RA_DBGX((as, "rename $f $r $r", regcost_ref(as->cost[up]), down, up)); - emit_movrr(as, ir, down, up); /* Backwards codegen needs inverse move. */ - if (!ra_hasspill(IR(ref)->s)) { /* Add the rename to the IR. */ - /* - ** The rename is effective at the subsequent (already emitted) exit - ** branch. This is for the current snapshot (as->snapno). Except if we - ** haven't yet allocated any refs for the snapshot (as->snapalloc == 1), - ** then it belongs to the next snapshot. - ** See also the discussion at asm_snap_checkrename(). - */ - ra_addrename(as, down, ref, as->snapno + as->snapalloc); - } -} - -/* Pick a destination register (marked as free). -** Caveat: allow is ignored if there's already a destination register. -** Use ra_destreg() to get a specific register. -*/ -static Reg ra_dest(ASMState *as, IRIns *ir, RegSet allow) -{ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - } else { - if (ra_hashint(dest) && rset_test((as->freeset&allow), ra_gethint(dest))) { - dest = ra_gethint(dest); - ra_modified(as, dest); - RA_DBGX((as, "dest $r", dest)); - } else { - dest = ra_scratch(as, allow); - } - ir->r = dest; - } - if (LJ_UNLIKELY(ra_hasspill(ir->s))) ra_save(as, ir, dest); - return dest; -} - -/* Force a specific destination register (marked as free). */ -static void ra_destreg(ASMState *as, IRIns *ir, Reg r) -{ - Reg dest = ra_dest(as, ir, RID2RSET(r)); - if (dest != r) { - lj_assertA(rset_test(as->freeset, r), "dest reg %d is not free", r); - ra_modified(as, r); - emit_movrr(as, ir, dest, r); - } -} - -#if LJ_TARGET_X86ORX64 -/* Propagate dest register to left reference. Emit moves as needed. -** This is a required fixup step for all 2-operand machine instructions. -*/ -static void ra_left(ASMState *as, Reg dest, IRRef lref) -{ - IRIns *ir = IR(lref); - Reg left = ir->r; - if (ra_noreg(left)) { - if (irref_isk(lref)) { - if (ir->o == IR_KNUM) { - /* FP remat needs a load except for +0. Still better than eviction. */ - if (tvispzero(ir_knum(ir)) || !(as->freeset & RSET_FPR)) { - emit_loadk64(as, dest, ir); - return; - } -#if LJ_64 - } else if (ir->o == IR_KINT64) { - emit_loadk64(as, dest, ir); - return; -#if LJ_GC64 - } else if (ir->o == IR_KGC || ir->o == IR_KPTR || ir->o == IR_KKPTR) { - emit_loadk64(as, dest, ir); - return; -#endif -#endif - } else if (ir->o != IR_KPRI) { - lj_assertA(ir->o == IR_KINT || ir->o == IR_KGC || - ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL, - "K%03d has bad IR op %d", REF_BIAS - lref, ir->o); - emit_loadi(as, dest, ir->i); - return; - } - } - if (!ra_hashint(left) && !iscrossref(as, lref)) - ra_sethint(ir->r, dest); /* Propagate register hint. */ - left = ra_allocref(as, lref, dest < RID_MAX_GPR ? RSET_GPR : RSET_FPR); - } - ra_noweak(as, left); - /* Move needed for true 3-operand instruction: y=a+b ==> y=a; y+=b. */ - if (dest != left) { - /* Use register renaming if dest is the PHI reg. */ - if (irt_isphi(ir->t) && as->phireg[dest] == lref) { - ra_modified(as, left); - ra_rename(as, left, dest); - } else { - emit_movrr(as, ir, dest, left); - } - } -} -#else -/* Similar to ra_left, except we override any hints. */ -static void ra_leftov(ASMState *as, Reg dest, IRRef lref) -{ - IRIns *ir = IR(lref); - Reg left = ir->r; - if (ra_noreg(left)) { - ra_sethint(ir->r, dest); /* Propagate register hint. */ - left = ra_allocref(as, lref, - (LJ_SOFTFP || dest < RID_MAX_GPR) ? RSET_GPR : RSET_FPR); - } - ra_noweak(as, left); - if (dest != left) { - /* Use register renaming if dest is the PHI reg. */ - if (irt_isphi(ir->t) && as->phireg[dest] == lref) { - ra_modified(as, left); - ra_rename(as, left, dest); - } else { - emit_movrr(as, ir, dest, left); - } - } -} -#endif - -/* Force a RID_RETLO/RID_RETHI destination register pair (marked as free). */ -static void ra_destpair(ASMState *as, IRIns *ir) -{ - Reg destlo = ir->r, desthi = (ir+1)->r; - IRIns *irx = (LJ_64 && !irt_is64(ir->t)) ? ir+1 : ir; - /* First spill unrelated refs blocking the destination registers. */ - if (!rset_test(as->freeset, RID_RETLO) && - destlo != RID_RETLO && desthi != RID_RETLO) - ra_restore(as, regcost_ref(as->cost[RID_RETLO])); - if (!rset_test(as->freeset, RID_RETHI) && - destlo != RID_RETHI && desthi != RID_RETHI) - ra_restore(as, regcost_ref(as->cost[RID_RETHI])); - /* Next free the destination registers (if any). */ - if (ra_hasreg(destlo)) { - ra_free(as, destlo); - ra_modified(as, destlo); - } else { - destlo = RID_RETLO; - } - if (ra_hasreg(desthi)) { - ra_free(as, desthi); - ra_modified(as, desthi); - } else { - desthi = RID_RETHI; - } - /* Check for conflicts and shuffle the registers as needed. */ - if (destlo == RID_RETHI) { - if (desthi == RID_RETLO) { -#if LJ_TARGET_X86ORX64 - *--as->mcp = XI_XCHGa + RID_RETHI; - if (LJ_64 && irt_is64(irx->t)) *--as->mcp = 0x48; -#else - emit_movrr(as, irx, RID_RETHI, RID_TMP); - emit_movrr(as, irx, RID_RETLO, RID_RETHI); - emit_movrr(as, irx, RID_TMP, RID_RETLO); -#endif - } else { - emit_movrr(as, irx, RID_RETHI, RID_RETLO); - if (desthi != RID_RETHI) emit_movrr(as, irx, desthi, RID_RETHI); - } - } else if (desthi == RID_RETLO) { - emit_movrr(as, irx, RID_RETLO, RID_RETHI); - if (destlo != RID_RETLO) emit_movrr(as, irx, destlo, RID_RETLO); - } else { - if (desthi != RID_RETHI) emit_movrr(as, irx, desthi, RID_RETHI); - if (destlo != RID_RETLO) emit_movrr(as, irx, destlo, RID_RETLO); - } - /* Restore spill slots (if any). */ - if (ra_hasspill((ir+1)->s)) ra_save(as, ir+1, RID_RETHI); - if (ra_hasspill(ir->s)) ra_save(as, ir, RID_RETLO); -} - -/* -- Snapshot handling --------- ----------------------------------------- */ - -/* Can we rematerialize a KNUM instead of forcing a spill? */ -static int asm_snap_canremat(ASMState *as) -{ - Reg r; - for (r = RID_MIN_FPR; r < RID_MAX_FPR; r++) - if (irref_isk(regcost_ref(as->cost[r]))) - return 1; - return 0; -} - -/* Check whether a sunk store corresponds to an allocation. */ -static int asm_sunk_store(ASMState *as, IRIns *ira, IRIns *irs) -{ - if (irs->s == 255) { - if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE || irs->o == IR_XSTORE) { - IRIns *irk = IR(irs->op1); - if (irk->o == IR_AREF || irk->o == IR_HREFK) - irk = IR(irk->op1); - return (IR(irk->op1) == ira); - } - return 0; - } else { - return (ira + irs->s == irs); /* Quick check. */ - } -} - -/* Allocate register or spill slot for a ref that escapes to a snapshot. */ -static void asm_snap_alloc1(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (!irref_isk(ref) && ir->r != RID_SUNK) { - bloomset(as->snapfilt1, ref); - bloomset(as->snapfilt2, hashrot(ref, ref + HASH_BIAS)); - if (ra_used(ir)) return; - if (ir->r == RID_SINK) { - ir->r = RID_SUNK; -#if LJ_HASFFI - if (ir->o == IR_CNEWI) { /* Allocate CNEWI value. */ - asm_snap_alloc1(as, ir->op2); - if (LJ_32 && (ir+1)->o == IR_HIOP) - asm_snap_alloc1(as, (ir+1)->op2); - } else -#endif - { /* Allocate stored values for TNEW, TDUP and CNEW. */ - IRIns *irs; - lj_assertA(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW, - "sink of IR %04d has bad op %d", ref - REF_BIAS, ir->o); - for (irs = IR(as->snapref-1); irs > ir; irs--) - if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) { - lj_assertA(irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE || irs->o == IR_XSTORE, - "sunk store IR %04d has bad op %d", - (int)(irs - as->ir) - REF_BIAS, irs->o); - asm_snap_alloc1(as, irs->op2); - if (LJ_32 && (irs+1)->o == IR_HIOP) - asm_snap_alloc1(as, (irs+1)->op2); - } - } - } else { - RegSet allow; - if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT) { - IRIns *irc; - for (irc = IR(as->curins); irc > ir; irc--) - if ((irc->op1 == ref || irc->op2 == ref) && - !(irc->r == RID_SINK || irc->r == RID_SUNK)) - goto nosink; /* Don't sink conversion if result is used. */ - asm_snap_alloc1(as, ir->op1); - return; - } - nosink: - allow = (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR; - if ((as->freeset & allow) || - (allow == RSET_FPR && asm_snap_canremat(as))) { - /* Get a weak register if we have a free one or can rematerialize. */ - Reg r = ra_allocref(as, ref, allow); /* Allocate a register. */ - if (!irt_isphi(ir->t)) - ra_weak(as, r); /* But mark it as weakly referenced. */ - checkmclim(as); - RA_DBGX((as, "snapreg $f $r", ref, ir->r)); - } else { - ra_spill(as, ir); /* Otherwise force a spill slot. */ - RA_DBGX((as, "snapspill $f $s", ref, ir->s)); - } - } - } -} - -/* Allocate refs escaping to a snapshot. */ -static void asm_snap_alloc(ASMState *as, int snapno) -{ - SnapShot *snap = &as->T->snap[snapno]; - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - as->snapfilt1 = as->snapfilt2 = 0; - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRRef ref = snap_ref(sn); - if (!irref_isk(ref)) { - asm_snap_alloc1(as, ref); - if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) { - lj_assertA(irt_type(IR(ref+1)->t) == IRT_SOFTFP, - "snap %d[%d] points to bad SOFTFP IR %04d", - snapno, n, ref - REF_BIAS); - asm_snap_alloc1(as, ref+1); - } - } - } -} - -/* All guards for a snapshot use the same exitno. This is currently the -** same as the snapshot number. Since the exact origin of the exit cannot -** be determined, all guards for the same snapshot must exit with the same -** RegSP mapping. -** A renamed ref which has been used in a prior guard for the same snapshot -** would cause an inconsistency. The easy way out is to force a spill slot. -*/ -static int asm_snap_checkrename(ASMState *as, IRRef ren) -{ - if (bloomtest(as->snapfilt1, ren) && - bloomtest(as->snapfilt2, hashrot(ren, ren + HASH_BIAS))) { - IRIns *ir = IR(ren); - ra_spill(as, ir); /* Register renamed, so force a spill slot. */ - RA_DBGX((as, "snaprensp $f $s", ren, ir->s)); - return 1; /* Found. */ - } - return 0; /* Not found. */ -} - -/* Prepare snapshot for next guard or throwing instruction. */ -static void asm_snap_prep(ASMState *as) -{ - if (as->snapalloc) { - /* Alloc on first invocation for each snapshot. */ - as->snapalloc = 0; - asm_snap_alloc(as, as->snapno); - as->snaprename = as->T->nins; - } else { - /* Check any renames above the highwater mark. */ - for (; as->snaprename < as->T->nins; as->snaprename++) { - IRIns *ir = &as->T->ir[as->snaprename]; - if (asm_snap_checkrename(as, ir->op1)) - ir->op2 = REF_BIAS-1; /* Kill rename. */ - } - } -} - -/* Move to previous snapshot when we cross the current snapshot ref. */ -static void asm_snap_prev(ASMState *as) -{ - if (as->curins < as->snapref) { - uintptr_t ofs = (uintptr_t)(as->mctoporig - as->mcp); - if (ofs >= 0x10000) lj_trace_err(as->J, LJ_TRERR_MCODEOV); - do { - if (as->snapno == 0) return; - as->snapno--; - as->snapref = as->T->snap[as->snapno].ref; - as->T->snap[as->snapno].mcofs = (uint16_t)ofs; /* Remember mcode ofs. */ - } while (as->curins < as->snapref); /* May have no ins inbetween. */ - as->snapalloc = 1; - } -} - -/* Fixup snapshot mcode offsetst. */ -static void asm_snap_fixup_mcofs(ASMState *as) -{ - uint32_t sz = (uint32_t)(as->mctoporig - as->mcp); - SnapShot *snap = as->T->snap; - SnapNo i; - for (i = as->T->nsnap-1; i > 0; i--) { - /* Compute offset from mcode start and store in correct snapshot. */ - snap[i].mcofs = (uint16_t)(sz - snap[i-1].mcofs); - } - snap[0].mcofs = 0; -} - -/* -- Miscellaneous helpers ----------------------------------------------- */ - -/* Calculate stack adjustment. */ -static int32_t asm_stack_adjust(ASMState *as) -{ - if (as->evenspill <= SPS_FIXED) - return 0; - return sps_scale(sps_align(as->evenspill)); -} - -/* Must match with hash*() in lj_tab.c. */ -static uint32_t ir_khash(ASMState *as, IRIns *ir) -{ - uint32_t lo, hi; - UNUSED(as); - if (irt_isstr(ir->t)) { - return ir_kstr(ir)->sid; - } else if (irt_isnum(ir->t)) { - lo = ir_knum(ir)->u32.lo; - hi = ir_knum(ir)->u32.hi << 1; - } else if (irt_ispri(ir->t)) { - lj_assertA(!irt_isnil(ir->t), "hash of nil key"); - return irt_type(ir->t)-IRT_FALSE; - } else { - lj_assertA(irt_isgcv(ir->t), "hash of bad IR type %d", irt_type(ir->t)); - lo = u32ptr(ir_kgc(ir)); -#if LJ_GC64 - hi = (uint32_t)(u64ptr(ir_kgc(ir)) >> 32) | (irt_toitype(ir->t) << 15); -#else - hi = lo + HASH_BIAS; -#endif - } - return hashrot(lo, hi); -} - -/* -- Allocations --------------------------------------------------------- */ - -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args); -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci); - -static void asm_snew(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new]; - IRRef args[3]; - asm_snap_prep(as); - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* const char *str */ - args[2] = ir->op2; /* size_t len */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); -} - -static void asm_tnew(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1]; - IRRef args[2]; - asm_snap_prep(as); - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* uint32_t ahsize */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCtab * */ - asm_gencall(as, ci, args); - ra_allockreg(as, ir->op1 | (ir->op2 << 24), ra_releasetmp(as, ASMREF_TMP1)); -} - -static void asm_tdup(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup]; - IRRef args[2]; - asm_snap_prep(as); - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* const GCtab *kt */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCtab * */ - asm_gencall(as, ci, args); -} - -static void asm_gc_check(ASMState *as); - -/* Explicit GC step. */ -static void asm_gcstep(ASMState *as, IRIns *ir) -{ - IRIns *ira; - for (ira = IR(as->stopins+1); ira < ir; ira++) - if ((ira->o == IR_TNEW || ira->o == IR_TDUP || - (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI))) && - ra_used(ira)) - as->gcsteps++; - if (as->gcsteps) - asm_gc_check(as); - as->gcsteps = 0x80000000; /* Prevent implicit GC check further up. */ -} - -/* -- Buffer operations --------------------------------------------------- */ - -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode); -#if LJ_HASBUFFER -static void asm_bufhdr_write(ASMState *as, Reg sb); -#endif - -static void asm_bufhdr(ASMState *as, IRIns *ir) -{ - Reg sb = ra_dest(as, ir, RSET_GPR); - switch (ir->op2) { - case IRBUFHDR_RESET: { - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); - IRIns irbp; - irbp.ot = IRT(0, IRT_PTR); /* Buffer data pointer type. */ - emit_storeofs(as, &irbp, tmp, sb, offsetof(SBuf, w)); - emit_loadofs(as, &irbp, tmp, sb, offsetof(SBuf, b)); - break; - } - case IRBUFHDR_APPEND: { - /* Rematerialize const buffer pointer instead of likely spill. */ - IRIns *irp = IR(ir->op1); - if (!(ra_hasreg(irp->r) || irp == ir-1 || - (irp == ir-2 && !ra_used(ir-1)))) { - while (!(irp->o == IR_BUFHDR && irp->op2 == IRBUFHDR_RESET)) - irp = IR(irp->op1); - if (irref_isk(irp->op1)) { - ra_weak(as, ra_allocref(as, ir->op1, RSET_GPR)); - ir = irp; - } - } - break; - } -#if LJ_HASBUFFER - case IRBUFHDR_WRITE: - asm_bufhdr_write(as, sb); - break; -#endif - default: lj_assertA(0, "bad BUFHDR op2 %d", ir->op2); break; - } -#if LJ_TARGET_X86ORX64 - ra_left(as, sb, ir->op1); -#else - ra_leftov(as, sb, ir->op1); -#endif -} - -static void asm_bufput(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_putstr]; - IRRef args[3]; - IRIns *irs; - int kchar = -129; - args[0] = ir->op1; /* SBuf * */ - args[1] = ir->op2; /* GCstr * */ - irs = IR(ir->op2); - lj_assertA(irt_isstr(irs->t), - "BUFPUT of non-string IR %04d", ir->op2 - REF_BIAS); - if (irs->o == IR_KGC) { - GCstr *s = ir_kstr(irs); - if (s->len == 1) { /* Optimize put of single-char string constant. */ - kchar = (int8_t)strdata(s)[0]; /* Signed! */ - args[1] = ASMREF_TMP1; /* int, truncated to char */ - ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar]; - } - } else if (mayfuse(as, ir->op2) && ra_noreg(irs->r)) { - if (irs->o == IR_TOSTR) { /* Fuse number to string conversions. */ - if (irs->op2 == IRTOSTR_NUM) { - args[1] = ASMREF_TMP1; /* TValue * */ - ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putnum]; - } else { - lj_assertA(irt_isinteger(IR(irs->op1)->t), - "TOSTR of non-numeric IR %04d", irs->op1); - args[1] = irs->op1; /* int */ - if (irs->op2 == IRTOSTR_INT) - ci = &lj_ir_callinfo[IRCALL_lj_strfmt_putint]; - else - ci = &lj_ir_callinfo[IRCALL_lj_buf_putchar]; - } - } else if (irs->o == IR_SNEW) { /* Fuse string allocation. */ - args[1] = irs->op1; /* const void * */ - args[2] = irs->op2; /* MSize */ - ci = &lj_ir_callinfo[IRCALL_lj_buf_putmem]; - } - } - asm_setupresult(as, ir, ci); /* SBuf * */ - asm_gencall(as, ci, args); - if (args[1] == ASMREF_TMP1) { - Reg tmp = ra_releasetmp(as, ASMREF_TMP1); - if (kchar == -129) - asm_tvptr(as, tmp, irs->op1, IRTMPREF_IN1); - else - ra_allockreg(as, kchar, tmp); - } -} - -static void asm_bufstr(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_buf_tostr]; - IRRef args[1]; - args[0] = ir->op1; /* SBuf *sb */ - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); -} - -/* -- Type conversions ---------------------------------------------------- */ - -static void asm_tostr(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci; - IRRef args[2]; - asm_snap_prep(as); - args[0] = ASMREF_L; - as->gcsteps++; - if (ir->op2 == IRTOSTR_NUM) { - args[1] = ASMREF_TMP1; /* cTValue * */ - ci = &lj_ir_callinfo[IRCALL_lj_strfmt_num]; - } else { - args[1] = ir->op1; /* int32_t k */ - if (ir->op2 == IRTOSTR_INT) - ci = &lj_ir_callinfo[IRCALL_lj_strfmt_int]; - else - ci = &lj_ir_callinfo[IRCALL_lj_strfmt_char]; - } - asm_setupresult(as, ir, ci); /* GCstr * */ - asm_gencall(as, ci, args); - if (ir->op2 == IRTOSTR_NUM) - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1, IRTMPREF_IN1); -} - -#if LJ_32 && LJ_HASFFI && !LJ_SOFTFP && !LJ_TARGET_X86 -static void asm_conv64(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); - IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); - IRCallID id; - IRRef args[2]; - lj_assertA((ir-1)->o == IR_CONV && ir->o == IR_HIOP, - "not a CONV/HIOP pair at IR %04d", (int)(ir - as->ir) - REF_BIAS); - args[LJ_BE] = (ir-1)->op1; - args[LJ_LE] = ir->op1; - if (st == IRT_NUM || st == IRT_FLOAT) { - id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); - ir--; - } else { - id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); - } - { -#if LJ_TARGET_ARM && !LJ_ABI_SOFTFP - CCallInfo cim = lj_ir_callinfo[id], *ci = &cim; - cim.flags |= CCI_VARARG; /* These calls don't use the hard-float ABI! */ -#else - const CCallInfo *ci = &lj_ir_callinfo[id]; -#endif - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); - } -} -#endif - -/* -- Memory references --------------------------------------------------- */ - -static void asm_newref(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; - IRRef args[3]; - if (ir->r == RID_SINK) - return; - asm_snap_prep(as); - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* GCtab *t */ - args[2] = ASMREF_TMP1; /* cTValue *key */ - asm_setupresult(as, ir, ci); /* TValue * */ - asm_gencall(as, ci, args); - asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2, IRTMPREF_IN1); -} - -static void asm_tmpref(ASMState *as, IRIns *ir) -{ - Reg r = ra_dest(as, ir, RSET_GPR); - asm_tvptr(as, r, ir->op1, ir->op2); -} - -static void asm_lref(ASMState *as, IRIns *ir) -{ - Reg r = ra_dest(as, ir, RSET_GPR); -#if LJ_TARGET_X86ORX64 - ra_left(as, r, ASMREF_L); -#else - ra_leftov(as, r, ASMREF_L); -#endif -} - -/* -- Calls --------------------------------------------------------------- */ - -/* Collect arguments from CALL* and CARG instructions. */ -static void asm_collectargs(ASMState *as, IRIns *ir, - const CCallInfo *ci, IRRef *args) -{ - uint32_t n = CCI_XNARGS(ci); - /* Account for split args. */ - lj_assertA(n <= CCI_NARGS_MAX*2, "too many args %d to collect", n); - if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; } - while (n-- > 1) { - ir = IR(ir->op1); - lj_assertA(ir->o == IR_CARG, "malformed CALL arg tree"); - args[n] = ir->op2 == REF_NIL ? 0 : ir->op2; - } - args[0] = ir->op1 == REF_NIL ? 0 : ir->op1; - lj_assertA(IR(ir->op1)->o != IR_CARG, "malformed CALL arg tree"); -} - -/* Reconstruct CCallInfo flags for CALLX*. */ -static uint32_t asm_callx_flags(ASMState *as, IRIns *ir) -{ - uint32_t nargs = 0; - if (ir->op1 != REF_NIL) { /* Count number of arguments first. */ - IRIns *ira = IR(ir->op1); - nargs++; - while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); } - } -#if LJ_HASFFI - if (IR(ir->op2)->o == IR_CARG) { /* Copy calling convention info. */ - CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i; - CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id); - nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0); -#if LJ_TARGET_X86 - nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT); -#endif - } -#endif - return (nargs | (ir->t.irt << CCI_OTSHIFT)); -} - -static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -static void asm_call(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX]; - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - asm_collectargs(as, ir, ci, args); - asm_setupresult(as, ir, ci); - asm_gencall(as, ci, args); -} - -/* -- PHI and loop handling ----------------------------------------------- */ - -/* Break a PHI cycle by renaming to a free register (evict if needed). */ -static void asm_phi_break(ASMState *as, RegSet blocked, RegSet blockedby, - RegSet allow) -{ - RegSet candidates = blocked & allow; - if (candidates) { /* If this register file has candidates. */ - /* Note: the set for ra_pick cannot be empty, since each register file - ** has some registers never allocated to PHIs. - */ - Reg down, up = ra_pick(as, ~blocked & allow); /* Get a free register. */ - if (candidates & ~blockedby) /* Optimize shifts, else it's a cycle. */ - candidates = candidates & ~blockedby; - down = rset_picktop(candidates); /* Pick candidate PHI register. */ - ra_rename(as, down, up); /* And rename it to the free register. */ - } -} - -/* PHI register shuffling. -** -** The allocator tries hard to preserve PHI register assignments across -** the loop body. Most of the time this loop does nothing, since there -** are no register mismatches. -** -** If a register mismatch is detected and ... -** - the register is currently free: rename it. -** - the register is blocked by an invariant: restore/remat and rename it. -** - Otherwise the register is used by another PHI, so mark it as blocked. -** -** The renames are order-sensitive, so just retry the loop if a register -** is marked as blocked, but has been freed in the meantime. A cycle is -** detected if all of the blocked registers are allocated. To break the -** cycle rename one of them to a free register and retry. -** -** Note that PHI spill slots are kept in sync and don't need to be shuffled. -*/ -static void asm_phi_shuffle(ASMState *as) -{ - RegSet work; - - /* Find and resolve PHI register mismatches. */ - for (;;) { - RegSet blocked = RSET_EMPTY; - RegSet blockedby = RSET_EMPTY; - RegSet phiset = as->phiset; - while (phiset) { /* Check all left PHI operand registers. */ - Reg r = rset_pickbot(phiset); - IRIns *irl = IR(as->phireg[r]); - Reg left = irl->r; - if (r != left) { /* Mismatch? */ - if (!rset_test(as->freeset, r)) { /* PHI register blocked? */ - IRRef ref = regcost_ref(as->cost[r]); - /* Blocked by other PHI (w/reg)? */ - if (!ra_iskref(ref) && irt_ismarked(IR(ref)->t)) { - rset_set(blocked, r); - if (ra_hasreg(left)) - rset_set(blockedby, left); - left = RID_NONE; - } else { /* Otherwise grab register from invariant. */ - ra_restore(as, ref); - checkmclim(as); - } - } - if (ra_hasreg(left)) { - ra_rename(as, left, r); - checkmclim(as); - } - } - rset_clear(phiset, r); - } - if (!blocked) break; /* Finished. */ - if (!(as->freeset & blocked)) { /* Break cycles if none are free. */ - asm_phi_break(as, blocked, blockedby, RSET_GPR); - if (!LJ_SOFTFP) asm_phi_break(as, blocked, blockedby, RSET_FPR); - checkmclim(as); - } /* Else retry some more renames. */ - } - - /* Restore/remat invariants whose registers are modified inside the loop. */ -#if !LJ_SOFTFP - work = as->modset & ~(as->freeset | as->phiset) & RSET_FPR; - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } -#endif - work = as->modset & ~(as->freeset | as->phiset); - while (work) { - Reg r = rset_pickbot(work); - ra_restore(as, regcost_ref(as->cost[r])); - rset_clear(work, r); - checkmclim(as); - } - - /* Allocate and save all unsaved PHI regs and clear marks. */ - work = as->phiset; - while (work) { - Reg r = rset_picktop(work); - IRRef lref = as->phireg[r]; - IRIns *ir = IR(lref); - if (ra_hasspill(ir->s)) { /* Left PHI gained a spill slot? */ - irt_clearmark(ir->t); /* Handled here, so clear marker now. */ - ra_alloc1(as, lref, RID2RSET(r)); - ra_save(as, ir, r); /* Save to spill slot inside the loop. */ - checkmclim(as); - } - rset_clear(work, r); - } -} - -/* Copy unsynced left/right PHI spill slots. Rarely needed. */ -static void asm_phi_copyspill(ASMState *as) -{ - int need = 0; - IRIns *ir; - for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) - if (ra_hasspill(ir->s) && ra_hasspill(IR(ir->op1)->s)) - need |= irt_isfp(ir->t) ? 2 : 1; /* Unsynced spill slot? */ - if ((need & 1)) { /* Copy integer spill slots. */ -#if !LJ_TARGET_X86ORX64 - Reg r = RID_TMP; -#else - Reg r = RID_RET; - if ((as->freeset & RSET_GPR)) - r = rset_pickbot((as->freeset & RSET_GPR)); - else - emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); -#endif - for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { - if (ra_hasspill(ir->s)) { - IRIns *irl = IR(ir->op1); - if (ra_hasspill(irl->s) && !irt_isfp(ir->t)) { - emit_spstore(as, irl, r, sps_scale(irl->s)); - emit_spload(as, ir, r, sps_scale(ir->s)); - checkmclim(as); - } - } - } -#if LJ_TARGET_X86ORX64 - if (!rset_test(as->freeset, r)) - emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); -#endif - } -#if !LJ_SOFTFP - if ((need & 2)) { /* Copy FP spill slots. */ -#if LJ_TARGET_X86 - Reg r = RID_XMM0; -#else - Reg r = RID_FPRET; -#endif - if ((as->freeset & RSET_FPR)) - r = rset_pickbot((as->freeset & RSET_FPR)); - if (!rset_test(as->freeset, r)) - emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); - for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { - if (ra_hasspill(ir->s)) { - IRIns *irl = IR(ir->op1); - if (ra_hasspill(irl->s) && irt_isfp(ir->t)) { - emit_spstore(as, irl, r, sps_scale(irl->s)); - emit_spload(as, ir, r, sps_scale(ir->s)); - checkmclim(as); - } - } - } - if (!rset_test(as->freeset, r)) - emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); - } -#endif -} - -/* Emit renames for left PHIs which are only spilled outside the loop. */ -static void asm_phi_fixup(ASMState *as) -{ - RegSet work = as->phiset; - while (work) { - Reg r = rset_picktop(work); - IRRef lref = as->phireg[r]; - IRIns *ir = IR(lref); - if (irt_ismarked(ir->t)) { - irt_clearmark(ir->t); - /* Left PHI gained a spill slot before the loop? */ - if (ra_hasspill(ir->s)) { - ra_addrename(as, r, lref, as->loopsnapno); - } - } - rset_clear(work, r); - } -} - -/* Setup right PHI reference. */ -static void asm_phi(ASMState *as, IRIns *ir) -{ - RegSet allow = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & - ~as->phiset; - RegSet afree = (as->freeset & allow); - IRIns *irl = IR(ir->op1); - IRIns *irr = IR(ir->op2); - if (ir->r == RID_SINK) /* Sink PHI. */ - return; - /* Spill slot shuffling is not implemented yet (but rarely needed). */ - if (ra_hasspill(irl->s) || ra_hasspill(irr->s)) - lj_trace_err(as->J, LJ_TRERR_NYIPHI); - /* Leave at least one register free for non-PHIs (and PHI cycle breaking). */ - if ((afree & (afree-1))) { /* Two or more free registers? */ - Reg r; - if (ra_noreg(irr->r)) { /* Get a register for the right PHI. */ - r = ra_allocref(as, ir->op2, allow); - } else { /* Duplicate right PHI, need a copy (rare). */ - r = ra_scratch(as, allow); - emit_movrr(as, irr, r, irr->r); - } - ir->r = (uint8_t)r; - rset_set(as->phiset, r); - as->phireg[r] = (IRRef1)ir->op1; - irt_setmark(irl->t); /* Marks left PHIs _with_ register. */ - if (ra_noreg(irl->r)) - ra_sethint(irl->r, r); /* Set register hint for left PHI. */ - } else { /* Otherwise allocate a spill slot. */ - /* This is overly restrictive, but it triggers only on synthetic code. */ - if (ra_hasreg(irl->r) || ra_hasreg(irr->r)) - lj_trace_err(as->J, LJ_TRERR_NYIPHI); - ra_spill(as, ir); - irr->s = ir->s; /* Set right PHI spill slot. Sync left slot later. */ - } -} - -static void asm_loop_fixup(ASMState *as); - -/* Middle part of a loop. */ -static void asm_loop(ASMState *as) -{ - MCode *mcspill; - /* LOOP is a guard, so the snapno is up to date. */ - as->loopsnapno = as->snapno; - if (as->gcsteps) - asm_gc_check(as); - /* LOOP marks the transition from the variant to the invariant part. */ - as->flagmcp = as->invmcp = NULL; - as->sectref = 0; - if (!neverfuse(as)) as->fuseref = 0; - asm_phi_shuffle(as); - mcspill = as->mcp; - asm_phi_copyspill(as); - asm_loop_fixup(as); - as->mcloop = as->mcp; - RA_DBGX((as, "===== LOOP =====")); - if (!as->realign) RA_DBG_FLUSH(); - if (as->mcp != mcspill) - emit_jmp(as, mcspill); -} - -/* -- Target-specific assembler ------------------------------------------- */ - -#if LJ_TARGET_X86ORX64 -#include "lj_asm_x86.h" -#elif LJ_TARGET_ARM -#include "lj_asm_arm.h" -#elif LJ_TARGET_ARM64 -#include "lj_asm_arm64.h" -#elif LJ_TARGET_PPC -#include "lj_asm_ppc.h" -#elif LJ_TARGET_MIPS -#include "lj_asm_mips.h" -#else -#error "Missing assembler for target CPU" -#endif - -/* -- Common instruction helpers ------------------------------------------ */ - -#if !LJ_SOFTFP32 -#if !LJ_TARGET_X86ORX64 -#define asm_ldexp(as, ir) asm_callid(as, ir, IRCALL_ldexp) -#endif - -static void asm_pow(ASMState *as, IRIns *ir) -{ -#if LJ_64 && LJ_HASFFI - if (!irt_isnum(ir->t)) - asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : - IRCALL_lj_carith_powu64); - else -#endif - asm_callid(as, ir, IRCALL_pow); -} - -static void asm_div(ASMState *as, IRIns *ir) -{ -#if LJ_64 && LJ_HASFFI - if (!irt_isnum(ir->t)) - asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : - IRCALL_lj_carith_divu64); - else -#endif - asm_fpdiv(as, ir); -} -#endif - -static void asm_mod(ASMState *as, IRIns *ir) -{ -#if LJ_64 && LJ_HASFFI - if (!irt_isint(ir->t)) - asm_callid(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : - IRCALL_lj_carith_modu64); - else -#endif - asm_callid(as, ir, IRCALL_lj_vm_modi); -} - -static void asm_fuseequal(ASMState *as, IRIns *ir) -{ - /* Fuse HREF + EQ/NE. */ - if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { - as->curins--; - asm_href(as, ir-1, (IROp)ir->o); - } else { - asm_equal(as, ir); - } -} - -static void asm_alen(ASMState *as, IRIns *ir) -{ - asm_callid(as, ir, ir->op2 == REF_NIL ? IRCALL_lj_tab_len : - IRCALL_lj_tab_len_hint); -} - -/* -- Instruction dispatch ------------------------------------------------ */ - -/* Assemble a single instruction. */ -static void asm_ir(ASMState *as, IRIns *ir) -{ - switch ((IROp)ir->o) { - /* Miscellaneous ops. */ - case IR_LOOP: asm_loop(as); break; - case IR_NOP: case IR_XBAR: - lj_assertA(!ra_used(ir), - "IR %04d not unused", (int)(ir - as->ir) - REF_BIAS); - break; - case IR_USE: - ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; - case IR_PHI: asm_phi(as, ir); break; - case IR_HIOP: asm_hiop(as, ir); break; - case IR_GCSTEP: asm_gcstep(as, ir); break; - case IR_PROF: asm_prof(as, ir); break; - - /* Guarded assertions. */ - case IR_LT: case IR_GE: case IR_LE: case IR_GT: - case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: - case IR_ABC: - asm_comp(as, ir); - break; - case IR_EQ: case IR_NE: asm_fuseequal(as, ir); break; - - case IR_RETF: asm_retf(as, ir); break; - - /* Bit ops. */ - case IR_BNOT: asm_bnot(as, ir); break; - case IR_BSWAP: asm_bswap(as, ir); break; - case IR_BAND: asm_band(as, ir); break; - case IR_BOR: asm_bor(as, ir); break; - case IR_BXOR: asm_bxor(as, ir); break; - case IR_BSHL: asm_bshl(as, ir); break; - case IR_BSHR: asm_bshr(as, ir); break; - case IR_BSAR: asm_bsar(as, ir); break; - case IR_BROL: asm_brol(as, ir); break; - case IR_BROR: asm_bror(as, ir); break; - - /* Arithmetic ops. */ - case IR_ADD: asm_add(as, ir); break; - case IR_SUB: asm_sub(as, ir); break; - case IR_MUL: asm_mul(as, ir); break; - case IR_MOD: asm_mod(as, ir); break; - case IR_NEG: asm_neg(as, ir); break; -#if LJ_SOFTFP32 - case IR_DIV: case IR_POW: case IR_ABS: - case IR_LDEXP: case IR_FPMATH: case IR_TOBIT: - /* Unused for LJ_SOFTFP32. */ - lj_assertA(0, "IR %04d with unused op %d", - (int)(ir - as->ir) - REF_BIAS, ir->o); - break; -#else - case IR_DIV: asm_div(as, ir); break; - case IR_POW: asm_pow(as, ir); break; - case IR_ABS: asm_abs(as, ir); break; - case IR_LDEXP: asm_ldexp(as, ir); break; - case IR_FPMATH: asm_fpmath(as, ir); break; - case IR_TOBIT: asm_tobit(as, ir); break; -#endif - case IR_MIN: asm_min(as, ir); break; - case IR_MAX: asm_max(as, ir); break; - - /* Overflow-checking arithmetic ops. */ - case IR_ADDOV: asm_addov(as, ir); break; - case IR_SUBOV: asm_subov(as, ir); break; - case IR_MULOV: asm_mulov(as, ir); break; - - /* Memory references. */ - case IR_AREF: asm_aref(as, ir); break; - case IR_HREF: asm_href(as, ir, 0); break; - case IR_HREFK: asm_hrefk(as, ir); break; - case IR_NEWREF: asm_newref(as, ir); break; - case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; - case IR_FREF: asm_fref(as, ir); break; - case IR_TMPREF: asm_tmpref(as, ir); break; - case IR_STRREF: asm_strref(as, ir); break; - case IR_LREF: asm_lref(as, ir); break; - - /* Loads and stores. */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - asm_ahuvload(as, ir); - break; - case IR_FLOAD: asm_fload(as, ir); break; - case IR_XLOAD: asm_xload(as, ir); break; - case IR_SLOAD: asm_sload(as, ir); break; - case IR_ALEN: asm_alen(as, ir); break; - - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; - case IR_FSTORE: asm_fstore(as, ir); break; - case IR_XSTORE: asm_xstore(as, ir); break; - - /* Allocations. */ - case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; - case IR_TNEW: asm_tnew(as, ir); break; - case IR_TDUP: asm_tdup(as, ir); break; - case IR_CNEW: case IR_CNEWI: -#if LJ_HASFFI - asm_cnew(as, ir); -#else - lj_assertA(0, "IR %04d with unused op %d", - (int)(ir - as->ir) - REF_BIAS, ir->o); -#endif - break; - - /* Buffer operations. */ - case IR_BUFHDR: asm_bufhdr(as, ir); break; - case IR_BUFPUT: asm_bufput(as, ir); break; - case IR_BUFSTR: asm_bufstr(as, ir); break; - - /* Write barriers. */ - case IR_TBAR: asm_tbar(as, ir); break; - case IR_OBAR: asm_obar(as, ir); break; - - /* Type conversions. */ - case IR_CONV: asm_conv(as, ir); break; - case IR_TOSTR: asm_tostr(as, ir); break; - case IR_STRTO: asm_strto(as, ir); break; - - /* Calls. */ - case IR_CALLA: - as->gcsteps++; - /* fallthrough */ - case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; - case IR_CALLXS: asm_callx(as, ir); break; - case IR_CARG: break; - - default: - setintV(&as->J->errinfo, ir->o); - lj_trace_err_info(as->J, LJ_TRERR_NYIIR); - break; - } -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Head of a root trace. */ -static void asm_head_root(ASMState *as) -{ - int32_t spadj; - asm_head_root_base(as); - emit_setvmstate(as, (int32_t)as->T->traceno); - spadj = asm_stack_adjust(as); - as->T->spadjust = (uint16_t)spadj; - emit_spsub(as, spadj); - /* Root traces assume a checked stack for the starting proto. */ - as->T->topslot = gcref(as->T->startpt)->pt.framesize; -} - -/* Head of a side trace. -** -** The current simplistic algorithm requires that all slots inherited -** from the parent are live in a register between pass 2 and pass 3. This -** avoids the complexity of stack slot shuffling. But of course this may -** overflow the register set in some cases and cause the dreaded error: -** "NYI: register coalescing too complex". A refined algorithm is needed. -*/ -static void asm_head_side(ASMState *as) -{ - IRRef1 sloadins[RID_MAX]; - RegSet allow = RSET_ALL; /* Inverse of all coalesced registers. */ - RegSet live = RSET_EMPTY; /* Live parent registers. */ - IRIns *irp = &as->parent->ir[REF_BASE]; /* Parent base. */ - int32_t spadj, spdelta; - int pass2 = 0; - int pass3 = 0; - IRRef i; - - if (as->snapno && as->topslot > as->parent->topslot) { - /* Force snap #0 alloc to prevent register overwrite in stack check. */ - asm_snap_alloc(as, 0); - } - allow = asm_head_side_base(as, irp, allow); - - /* Scan all parent SLOADs and collect register dependencies. */ - for (i = as->stopins; i > REF_BASE; i--) { - IRIns *ir = IR(i); - RegSP rs; - lj_assertA((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) || - (LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL, - "IR %04d has bad parent op %d", - (int)(ir - as->ir) - REF_BIAS, ir->o); - rs = as->parentmap[i - REF_FIRST]; - if (ra_hasreg(ir->r)) { - rset_clear(allow, ir->r); - if (ra_hasspill(ir->s)) { - ra_save(as, ir, ir->r); - checkmclim(as); - } - } else if (ra_hasspill(ir->s)) { - irt_setmark(ir->t); - pass2 = 1; - } - if (ir->r == rs) { /* Coalesce matching registers right now. */ - ra_free(as, ir->r); - } else if (ra_hasspill(regsp_spill(rs))) { - if (ra_hasreg(ir->r)) - pass3 = 1; - } else if (ra_used(ir)) { - sloadins[rs] = (IRRef1)i; - rset_set(live, rs); /* Block live parent register. */ - } - } - - /* Calculate stack frame adjustment. */ - spadj = asm_stack_adjust(as); - spdelta = spadj - (int32_t)as->parent->spadjust; - if (spdelta < 0) { /* Don't shrink the stack frame. */ - spadj = (int32_t)as->parent->spadjust; - spdelta = 0; - } - as->T->spadjust = (uint16_t)spadj; - - /* Reload spilled target registers. */ - if (pass2) { - for (i = as->stopins; i > REF_BASE; i--) { - IRIns *ir = IR(i); - if (irt_ismarked(ir->t)) { - RegSet mask; - Reg r; - RegSP rs; - irt_clearmark(ir->t); - rs = as->parentmap[i - REF_FIRST]; - if (!ra_hasspill(regsp_spill(rs))) - ra_sethint(ir->r, rs); /* Hint may be gone, set it again. */ - else if (sps_scale(regsp_spill(rs))+spdelta == sps_scale(ir->s)) - continue; /* Same spill slot, do nothing. */ - mask = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & allow; - if (mask == RSET_EMPTY) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - r = ra_allocref(as, i, mask); - ra_save(as, ir, r); - rset_clear(allow, r); - if (r == rs) { /* Coalesce matching registers right now. */ - ra_free(as, r); - rset_clear(live, r); - } else if (ra_hasspill(regsp_spill(rs))) { - pass3 = 1; - } - checkmclim(as); - } - } - } - - /* Store trace number and adjust stack frame relative to the parent. */ - emit_setvmstate(as, (int32_t)as->T->traceno); - emit_spsub(as, spdelta); - -#if !LJ_TARGET_X86ORX64 - /* Restore BASE register from parent spill slot. */ - if (ra_hasspill(irp->s)) - emit_spload(as, IR(REF_BASE), IR(REF_BASE)->r, sps_scale(irp->s)); -#endif - - /* Restore target registers from parent spill slots. */ - if (pass3) { - RegSet work = ~as->freeset & RSET_ALL; - while (work) { - Reg r = rset_pickbot(work); - IRRef ref = regcost_ref(as->cost[r]); - RegSP rs = as->parentmap[ref - REF_FIRST]; - rset_clear(work, r); - if (ra_hasspill(regsp_spill(rs))) { - int32_t ofs = sps_scale(regsp_spill(rs)); - ra_free(as, r); - emit_spload(as, IR(ref), r, ofs); - checkmclim(as); - } - } - } - - /* Shuffle registers to match up target regs with parent regs. */ - for (;;) { - RegSet work; - - /* Repeatedly coalesce free live registers by moving to their target. */ - while ((work = as->freeset & live) != RSET_EMPTY) { - Reg rp = rset_pickbot(work); - IRIns *ir = IR(sloadins[rp]); - rset_clear(live, rp); - rset_clear(allow, rp); - ra_free(as, ir->r); - emit_movrr(as, ir, ir->r, rp); - checkmclim(as); - } - - /* We're done if no live registers remain. */ - if (live == RSET_EMPTY) - break; - - /* Break cycles by renaming one target to a temp. register. */ - if (live & RSET_GPR) { - RegSet tmpset = as->freeset & ~live & allow & RSET_GPR; - if (tmpset == RSET_EMPTY) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - ra_rename(as, rset_pickbot(live & RSET_GPR), rset_pickbot(tmpset)); - } - if (!LJ_SOFTFP && (live & RSET_FPR)) { - RegSet tmpset = as->freeset & ~live & allow & RSET_FPR; - if (tmpset == RSET_EMPTY) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - ra_rename(as, rset_pickbot(live & RSET_FPR), rset_pickbot(tmpset)); - } - checkmclim(as); - /* Continue with coalescing to fix up the broken cycle(s). */ - } - - /* Inherit top stack slot already checked by parent trace. */ - as->T->topslot = as->parent->topslot; - if (as->topslot > as->T->topslot) { /* Need to check for higher slot? */ -#ifdef EXITSTATE_CHECKEXIT - /* Highest exit + 1 indicates stack check. */ - ExitNo exitno = as->T->nsnap; -#else - /* Reuse the parent exit in the context of the parent trace. */ - ExitNo exitno = as->J->exitno; -#endif - as->T->topslot = (uint8_t)as->topslot; /* Remember for child traces. */ - asm_stack_check(as, as->topslot, irp, allow & RSET_GPR, exitno); - } -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Get base slot for a snapshot. */ -static BCReg asm_baseslot(ASMState *as, SnapShot *snap, int *gotframe) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - MSize n; - for (n = snap->nent; n > 0; n--) { - SnapEntry sn = map[n-1]; - if ((sn & SNAP_FRAME)) { - *gotframe = 1; - return snap_slot(sn) - LJ_FR2; - } - } - return 0; -} - -/* Link to another trace. */ -static void asm_tail_link(ASMState *as) -{ - SnapNo snapno = as->T->nsnap-1; /* Last snapshot. */ - SnapShot *snap = &as->T->snap[snapno]; - int gotframe = 0; - BCReg baseslot = asm_baseslot(as, snap, &gotframe); - - as->topslot = snap->topslot; - checkmclim(as); - ra_allocref(as, REF_BASE, RID2RSET(RID_BASE)); - - if (as->T->link == 0) { - /* Setup fixed registers for exit to interpreter. */ - const BCIns *pc = snap_pc(&as->T->snapmap[snap->mapofs + snap->nent]); - int32_t mres; - if (bc_op(*pc) == BC_JLOOP) { /* NYI: find a better way to do this. */ - BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins; - if (bc_isret(bc_op(*retpc))) - pc = retpc; - } -#if LJ_GC64 - emit_loadu64(as, RID_LPC, u64ptr(pc)); -#else - ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH); - ra_allockreg(as, i32ptr(pc), RID_LPC); -#endif - mres = (int32_t)(snap->nslots - baseslot - LJ_FR2); - switch (bc_op(*pc)) { - case BC_CALLM: case BC_CALLMT: - mres -= (int32_t)(1 + LJ_FR2 + bc_a(*pc) + bc_c(*pc)); break; - case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break; - case BC_TSETM: mres -= (int32_t)bc_a(*pc); break; - default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break; - } - ra_allockreg(as, mres, RID_RET); /* Return MULTRES or 0. */ - } else if (baseslot) { - /* Save modified BASE for linking to trace with higher start frame. */ - emit_setgl(as, RID_BASE, jit_base); - } - emit_addptr(as, RID_BASE, 8*(int32_t)baseslot); - - if (as->J->ktrace) { /* Patch ktrace slot with the final GCtrace pointer. */ - setgcref(IR(as->J->ktrace)[LJ_GC64].gcr, obj2gco(as->J->curfinal)); - IR(as->J->ktrace)->o = IR_KGC; - } - - /* Sync the interpreter state with the on-trace state. */ - asm_stack_restore(as, snap); - - /* Root traces that add frames need to check the stack at the end. */ - if (!as->parent && gotframe) - asm_stack_check(as, as->topslot, NULL, as->freeset & RSET_GPR, snapno); -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Clear reg/sp for all instructions and add register hints. */ -static void asm_setup_regsp(ASMState *as) -{ - GCtrace *T = as->T; - int sink = T->sinktags; - IRRef nins = T->nins; - IRIns *ir, *lastir; - int inloop; -#if LJ_TARGET_ARM - uint32_t rload = 0xa6402a64; -#endif - - ra_setup(as); -#if LJ_TARGET_ARM64 - ra_setkref(as, RID_GL, (intptr_t)J2G(as->J)); -#endif - - /* Clear reg/sp for constants. */ - for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++) { - ir->prev = REGSP_INIT; - if (irt_is64(ir->t) && ir->o != IR_KNULL) { -#if LJ_GC64 - /* The false-positive of irt_is64() for ASMREF_L (REF_NIL) is OK here. */ - ir->i = 0; /* Will become non-zero only for RIP-relative addresses. */ -#else - /* Make life easier for backends by putting address of constant in i. */ - ir->i = (int32_t)(intptr_t)(ir+1); -#endif - ir++; - } - } - - /* REF_BASE is used for implicit references to the BASE register. */ - lastir->prev = REGSP_HINT(RID_BASE); - - as->snaprename = nins; - as->snapref = nins; - as->snapno = T->nsnap; - as->snapalloc = 0; - - as->stopins = REF_BASE; - as->orignins = nins; - as->curins = nins; - - /* Setup register hints for parent link instructions. */ - ir = IR(REF_FIRST); - if (as->parent) { - uint16_t *p; - lastir = lj_snap_regspmap(as->J, as->parent, as->J->exitno, ir); - if (lastir - ir > LJ_MAX_JSLOTS) - lj_trace_err(as->J, LJ_TRERR_NYICOAL); - as->stopins = (IRRef)((lastir-1) - as->ir); - for (p = as->parentmap; ir < lastir; ir++) { - RegSP rs = ir->prev; - *p++ = (uint16_t)rs; /* Copy original parent RegSP to parentmap. */ - if (!ra_hasspill(regsp_spill(rs))) - ir->prev = (uint16_t)REGSP_HINT(regsp_reg(rs)); - else - ir->prev = REGSP_INIT; - } - } - - inloop = 0; - as->evenspill = SPS_FIRST; - for (lastir = IR(nins); ir < lastir; ir++) { - if (sink) { - if (ir->r == RID_SINK) - continue; - if (ir->r == RID_SUNK) { /* Revert after ASM restart. */ - ir->r = RID_SINK; - continue; - } - } - switch (ir->o) { - case IR_LOOP: - inloop = 1; - break; -#if LJ_TARGET_ARM - case IR_SLOAD: - if (!((ir->op2 & IRSLOAD_TYPECHECK) || (ir+1)->o == IR_HIOP)) - break; - /* fallthrough */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - if (!LJ_SOFTFP && irt_isnum(ir->t)) break; - ir->prev = (uint16_t)REGSP_HINT((rload & 15)); - rload = lj_ror(rload, 4); - continue; - case IR_TMPREF: - if ((ir->op2 & IRTMPREF_OUT2) && as->evenspill < 4) - as->evenspill = 4; /* TMPREF OUT2 needs two TValues on the stack. */ - break; -#endif - case IR_CALLXS: { - CCallInfo ci; - ci.flags = asm_callx_flags(as, ir); - ir->prev = asm_setup_call_slots(as, ir, &ci); - if (inloop) - as->modset |= RSET_SCRATCH; - continue; - } - case IR_CALLL: - /* lj_vm_next needs two TValues on the stack. */ -#if LJ_TARGET_X64 && LJ_ABI_WIN - if (ir->op2 == IRCALL_lj_vm_next && as->evenspill < SPS_FIRST + 4) - as->evenspill = SPS_FIRST + 4; -#else - if (SPS_FIRST < 4 && ir->op2 == IRCALL_lj_vm_next && as->evenspill < 4) - as->evenspill = 4; -#endif - /* fallthrough */ - case IR_CALLN: case IR_CALLA: case IR_CALLS: { - const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; - ir->prev = asm_setup_call_slots(as, ir, ci); - if (inloop) - as->modset |= (ci->flags & CCI_NOFPRCLOBBER) ? - (RSET_SCRATCH & ~RSET_FPR) : RSET_SCRATCH; - continue; - } - case IR_HIOP: - switch ((ir-1)->o) { -#if LJ_SOFTFP && LJ_TARGET_ARM - case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - if (ra_hashint((ir-1)->r)) { - ir->prev = (ir-1)->prev + 1; - continue; - } - break; -#endif -#if !LJ_SOFTFP && LJ_NEED_FP64 && LJ_32 && LJ_HASFFI - case IR_CONV: - if (irt_isfp((ir-1)->t)) { - ir->prev = REGSP_HINT(RID_FPRET); - continue; - } -#endif - /* fallthrough */ - case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS: -#if LJ_SOFTFP - case IR_MIN: case IR_MAX: -#endif - (ir-1)->prev = REGSP_HINT(RID_RETLO); - ir->prev = REGSP_HINT(RID_RETHI); - continue; - default: - break; - } - break; -#if LJ_SOFTFP - case IR_MIN: case IR_MAX: - if ((ir+1)->o != IR_HIOP) break; -#endif - /* fallthrough */ - /* C calls evict all scratch regs and return results in RID_RET. */ - case IR_SNEW: case IR_XSNEW: case IR_NEWREF: case IR_BUFPUT: - if (REGARG_NUMGPR < 3 && as->evenspill < 3) - as->evenspill = 3; /* lj_str_new and lj_tab_newkey need 3 args. */ -#if LJ_TARGET_X86 && LJ_HASFFI - if (0) { - case IR_CNEW: - if (ir->op2 != REF_NIL && as->evenspill < 4) - as->evenspill = 4; /* lj_cdata_newv needs 4 args. */ - } - /* fallthrough */ -#else - /* fallthrough */ - case IR_CNEW: -#endif - /* fallthrough */ - case IR_TNEW: case IR_TDUP: case IR_CNEWI: case IR_TOSTR: - case IR_BUFSTR: - ir->prev = REGSP_HINT(RID_RET); - if (inloop) - as->modset = RSET_SCRATCH; - continue; - case IR_STRTO: case IR_OBAR: - if (inloop) - as->modset = RSET_SCRATCH; - break; -#if !LJ_SOFTFP -#if !LJ_TARGET_X86ORX64 - case IR_LDEXP: -#endif -#endif - /* fallthrough */ - case IR_POW: - if (!LJ_SOFTFP && irt_isnum(ir->t)) { - if (inloop) - as->modset |= RSET_SCRATCH; -#if LJ_TARGET_X86 - if (irt_isnum(IR(ir->op2)->t)) { - if (as->evenspill < 4) /* Leave room to call pow(). */ - as->evenspill = 4; - } - break; -#else - ir->prev = REGSP_HINT(RID_FPRET); - continue; -#endif - } - /* fallthrough */ /* for integer POW */ - case IR_DIV: case IR_MOD: - if ((LJ_64 && LJ_SOFTFP) || !irt_isnum(ir->t)) { - ir->prev = REGSP_HINT(RID_RET); - if (inloop) - as->modset |= (RSET_SCRATCH & RSET_GPR); - continue; - } - break; -#if LJ_64 && LJ_SOFTFP - case IR_ADD: case IR_SUB: case IR_MUL: - if (irt_isnum(ir->t)) { - ir->prev = REGSP_HINT(RID_RET); - if (inloop) - as->modset |= (RSET_SCRATCH & RSET_GPR); - continue; - } - break; -#endif - case IR_FPMATH: -#if LJ_TARGET_X86ORX64 - if (ir->op2 <= IRFPM_TRUNC) { - if (!(as->flags & JIT_F_SSE4_1)) { - ir->prev = REGSP_HINT(RID_XMM0); - if (inloop) - as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); - continue; - } - break; - } -#endif - if (inloop) - as->modset |= RSET_SCRATCH; -#if LJ_TARGET_X86 - break; -#else - ir->prev = REGSP_HINT(RID_FPRET); - continue; -#endif -#if LJ_TARGET_X86ORX64 - /* Non-constant shift counts need to be in RID_ECX on x86/x64. */ - case IR_BSHL: case IR_BSHR: case IR_BSAR: - if ((as->flags & JIT_F_BMI2)) /* Except if BMI2 is available. */ - break; - /* fallthrough */ - case IR_BROL: case IR_BROR: - if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) { - IR(ir->op2)->r = REGSP_HINT(RID_ECX); - if (inloop) - rset_set(as->modset, RID_ECX); - } - break; -#endif - /* Do not propagate hints across type conversions or loads. */ - case IR_TOBIT: - case IR_XLOAD: -#if !LJ_TARGET_ARM - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: -#endif - break; - case IR_CONV: - if (irt_isfp(ir->t) || (ir->op2 & IRCONV_SRCMASK) == IRT_NUM || - (ir->op2 & IRCONV_SRCMASK) == IRT_FLOAT) - break; - /* fallthrough */ - default: - /* Propagate hints across likely 'op reg, imm' or 'op reg'. */ - if (irref_isk(ir->op2) && !irref_isk(ir->op1) && - ra_hashint(regsp_reg(IR(ir->op1)->prev))) { - ir->prev = IR(ir->op1)->prev; - continue; - } - break; - } - ir->prev = REGSP_INIT; - } - if ((as->evenspill & 1)) - as->oddspill = as->evenspill++; - else - as->oddspill = 0; -} - -/* -- Assembler core ------------------------------------------------------ */ - -/* Assemble a trace. */ -void lj_asm_trace(jit_State *J, GCtrace *T) -{ - ASMState as_; - ASMState *as = &as_; - - /* Remove nops/renames left over from ASM restart due to LJ_TRERR_MCODELM. */ - { - IRRef nins = T->nins; - IRIns *ir = &T->ir[nins-1]; - if (ir->o == IR_NOP || ir->o == IR_RENAME) { - do { ir--; nins--; } while (ir->o == IR_NOP || ir->o == IR_RENAME); - T->nins = nins; - } - } - - /* Ensure an initialized instruction beyond the last one for HIOP checks. */ - /* This also allows one RENAME to be added without reallocating curfinal. */ - as->orignins = lj_ir_nextins(J); - lj_ir_nop(&J->cur.ir[as->orignins]); - - /* Setup initial state. Copy some fields to reduce indirections. */ - as->J = J; - as->T = T; - J->curfinal = lj_trace_alloc(J->L, T); /* This copies the IR, too. */ - as->flags = J->flags; - as->loopref = J->loopref; - as->realign = NULL; - as->loopinv = 0; - as->parent = J->parent ? traceref(J, J->parent) : NULL; - - /* Reserve MCode memory. */ - as->mctop = as->mctoporig = lj_mcode_reserve(J, &as->mcbot); - as->mcp = as->mctop; - as->mclim = as->mcbot + MCLIM_REDZONE; - asm_setup_target(as); - - /* - ** This is a loop, because the MCode may have to be (re-)assembled - ** multiple times: - ** - ** 1. as->realign is set (and the assembly aborted), if the arch-specific - ** backend wants the MCode to be aligned differently. - ** - ** This is currently only the case on x86/x64, where small loops get - ** an aligned loop body plus a short branch. Not much effort is wasted, - ** because the abort happens very quickly and only once. - ** - ** 2. The IR is immovable, since the MCode embeds pointers to various - ** constants inside the IR. But RENAMEs may need to be added to the IR - ** during assembly, which might grow and reallocate the IR. We check - ** at the end if the IR (in J->cur.ir) has actually grown, resize the - ** copy (in J->curfinal.ir) and try again. - ** - ** 95% of all traces have zero RENAMEs, 3% have one RENAME, 1.5% have - ** 2 RENAMEs and only 0.5% have more than that. That's why we opt to - ** always have one spare slot in the IR (see above), which means we - ** have to redo the assembly for only ~2% of all traces. - ** - ** Very, very rarely, this needs to be done repeatedly, since the - ** location of constants inside the IR (actually, reachability from - ** a global pointer) may affect register allocation and thus the - ** number of RENAMEs. - */ - for (;;) { - as->mcp = as->mctop; -#ifdef LUA_USE_ASSERT - as->mcp_prev = as->mcp; -#endif - as->ir = J->curfinal->ir; /* Use the copied IR. */ - as->curins = J->cur.nins = as->orignins; - - RA_DBG_START(); - RA_DBGX((as, "===== STOP =====")); - - /* General trace setup. Emit tail of trace. */ - asm_tail_prep(as); - as->mcloop = NULL; - as->flagmcp = NULL; - as->topslot = 0; - as->gcsteps = 0; - as->sectref = as->loopref; - as->fuseref = (as->flags & JIT_F_OPT_FUSE) ? as->loopref : FUSE_DISABLED; - asm_setup_regsp(as); - if (!as->loopref) - asm_tail_link(as); - - /* Assemble a trace in linear backwards order. */ - for (as->curins--; as->curins > as->stopins; as->curins--) { - IRIns *ir = IR(as->curins); - /* 64 bit types handled by SPLIT for 32 bit archs. */ - lj_assertA(!(LJ_32 && irt_isint64(ir->t)), - "IR %04d has unsplit 64 bit type", - (int)(ir - as->ir) - REF_BIAS); - asm_snap_prev(as); - if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE)) - continue; /* Dead-code elimination can be soooo easy. */ - if (irt_isguard(ir->t)) - asm_snap_prep(as); - RA_DBG_REF(); - checkmclim(as); - asm_ir(as, ir); - } - - if (as->realign && J->curfinal->nins >= T->nins) - continue; /* Retry in case only the MCode needs to be realigned. */ - - /* Emit head of trace. */ - RA_DBG_REF(); - checkmclim(as); - if (as->gcsteps > 0) { - as->curins = as->T->snap[0].ref; - asm_snap_prep(as); /* The GC check is a guard. */ - asm_gc_check(as); - as->curins = as->stopins; - } - ra_evictk(as); - if (as->parent) - asm_head_side(as); - else - asm_head_root(as); - asm_phi_fixup(as); - - if (J->curfinal->nins >= T->nins) { /* IR didn't grow? */ - lj_assertA(J->curfinal->nk == T->nk, "unexpected IR constant growth"); - memcpy(J->curfinal->ir + as->orignins, T->ir + as->orignins, - (T->nins - as->orignins) * sizeof(IRIns)); /* Copy RENAMEs. */ - T->nins = J->curfinal->nins; - /* Fill mcofs of any unprocessed snapshots. */ - as->curins = REF_FIRST; - asm_snap_prev(as); - break; /* Done. */ - } - - /* Otherwise try again with a bigger IR. */ - lj_trace_free(J2G(J), J->curfinal); - J->curfinal = NULL; /* In case lj_trace_alloc() OOMs. */ - J->curfinal = lj_trace_alloc(J->L, T); - as->realign = NULL; - } - - RA_DBGX((as, "===== START ====")); - RA_DBG_FLUSH(); - if (as->freeset != RSET_ALL) - lj_trace_err(as->J, LJ_TRERR_BADRA); /* Ouch! Should never happen. */ - - /* Set trace entry point before fixing up tail to allow link to self. */ - T->mcode = as->mcp; - T->mcloop = as->mcloop ? (MSize)((char *)as->mcloop - (char *)as->mcp) : 0; - if (as->loopref) - asm_loop_tail_fixup(as); - else - asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ - T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); - asm_snap_fixup_mcofs(as); -#if LJ_TARGET_MCODE_FIXUP - asm_mcode_fixup(T->mcode, T->szmcode); -#endif - lj_mcode_sync(T->mcode, as->mctoporig); -} - -#undef IR - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.h deleted file mode 100644 index f0a4f2d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm.h +++ /dev/null @@ -1,17 +0,0 @@ -/* -** IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_ASM_H -#define _LJ_ASM_H - -#include "lj_jit.h" - -#if LJ_HASJIT -LJ_FUNC void lj_asm_trace(jit_State *J, GCtrace *T); -LJ_FUNC void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, - MCode *target); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm.h deleted file mode 100644 index ba6267e..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm.h +++ /dev/null @@ -1,2290 +0,0 @@ -/* -** ARM IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate a scratch register pair. */ -static Reg ra_scratchpair(ASMState *as, RegSet allow) -{ - RegSet pick1 = as->freeset & allow; - RegSet pick2 = pick1 & (pick1 >> 1) & RSET_GPREVEN; - Reg r; - if (pick2) { - r = rset_picktop(pick2); - } else { - RegSet pick = pick1 & (allow >> 1) & RSET_GPREVEN; - if (pick) { - r = rset_picktop(pick); - ra_restore(as, regcost_ref(as->cost[r+1])); - } else { - pick = pick1 & (allow << 1) & RSET_GPRODD; - if (pick) { - r = ra_restore(as, regcost_ref(as->cost[rset_picktop(pick)-1])); - } else { - r = ra_evict(as, allow & (allow >> 1) & RSET_GPREVEN); - ra_restore(as, regcost_ref(as->cost[r+1])); - } - } - } - lj_assertA(rset_test(RSET_GPREVEN, r), "odd reg %d", r); - ra_modified(as, r); - ra_modified(as, r+1); - RA_DBGX((as, "scratchpair $r $r", r, r+1)); - return r; -} - -#if !LJ_SOFTFP -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_allocref(as, ir->op2, allow); - left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_allocref(as, ir->op1, allow); - right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} -#endif - -/* -- Guard handling ------------------------------------------------------ */ - -/* Generate an exit stub group at the bottom of the reserved MCode memory. */ -static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) -{ - MCode *mxp = as->mcbot; - int i; - if (mxp + 4*4+4*EXITSTUBS_PER_GROUP >= as->mctop) - asm_mclimit(as); - /* str lr, [sp]; bl ->vm_exit_handler; .long DISPATCH_address, group. */ - *mxp++ = ARMI_STR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_LR)|ARMF_N(RID_SP); - *mxp = ARMI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)-2)&0x00ffffffu); - mxp++; - *mxp++ = (MCode)i32ptr(J2GG(as->J)->dispatch); /* DISPATCH address */ - *mxp++ = group*EXITSTUBS_PER_GROUP; - for (i = 0; i < EXITSTUBS_PER_GROUP; i++) - *mxp++ = ARMI_B|((-6-i)&0x00ffffffu); - lj_mcode_sync(as->mcbot, mxp); - lj_mcode_commitbot(as->J, mxp); - as->mcbot = mxp; - as->mclim = as->mcbot + MCLIM_REDZONE; - return mxp - EXITSTUBS_PER_GROUP; -} - -/* Setup all needed exit stubs. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) - lj_trace_err(as->J, LJ_TRERR_SNAPOV); - for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) - if (as->J->exitstubgroup[i] == NULL) - as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); -} - -/* Emit conditional branch to exit for guard. */ -static void asm_guardcc(ASMState *as, ARMCC cc) -{ - MCode *target = exitstub_addr(as->J, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = ARMI_BL | ((target-p-2) & 0x00ffffffu); - emit_branch(as, ARMF_CC(ARMI_B, cc^1), p+1); - return; - } - emit_branch(as, ARMF_CC(ARMI_BL, cc), target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow, - int lim) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (ofs > -lim && ofs < lim) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (ofs < lim) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); - *ofsp = (ofs & 255); /* Mask out less bits to allow LDRD. */ - return ra_allock(as, (ofs & ~255), allow); - } - } else if (ir->o == IR_TMPREF) { - *ofsp = 0; - return RID_SP; - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse m operand into arithmetic/logic instructions. */ -static uint32_t asm_fuseopm(ASMState *as, ARMIns ai, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_hasreg(ir->r)) { - ra_noweak(as, ir->r); - return ARMF_M(ir->r); - } else if (irref_isk(ref)) { - uint32_t k = emit_isk12(ai, ir->i); - if (k) - return k; - } else if (mayfuse(as, ref)) { - if (ir->o >= IR_BSHL && ir->o <= IR_BROR) { - Reg m = ra_alloc1(as, ir->op1, allow); - ARMShift sh = ir->o == IR_BSHL ? ARMSH_LSL : - ir->o == IR_BSHR ? ARMSH_LSR : - ir->o == IR_BSAR ? ARMSH_ASR : ARMSH_ROR; - if (irref_isk(ir->op2)) { - return m | ARMF_SH(sh, (IR(ir->op2)->i & 31)); - } else { - Reg s = ra_alloc1(as, ir->op2, rset_exclude(allow, m)); - return m | ARMF_RSH(sh, s); - } - } else if (ir->o == IR_ADD && ir->op1 == ir->op2) { - Reg m = ra_alloc1(as, ir->op1, allow); - return m | ARMF_SH(ARMSH_LSL, 1); - } - } - return ra_allocref(as, ref, allow); -} - -/* Fuse shifts into loads/stores. Only bother with BSHL 2 => lsl #2. */ -static IRRef asm_fuselsl2(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r) && mayfuse(as, ref) && ir->o == IR_BSHL && - irref_isk(ir->op2) && IR(ir->op2)->i == 2) - return ir->op1; - return 0; /* No fusion. */ -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, ARMIns ai, Reg rd, IRRef ref, - RegSet allow, int32_t ofs) -{ - IRIns *ir = IR(ref); - Reg base; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - int32_t lim = (!LJ_SOFTFP && (ai & 0x08000000)) ? 1024 : - (ai & 0x04000000) ? 4096 : 256; - if (ir->o == IR_ADD) { - int32_t ofs2; - if (irref_isk(ir->op2) && - (ofs2 = ofs + IR(ir->op2)->i) > -lim && ofs2 < lim && - (!(!LJ_SOFTFP && (ai & 0x08000000)) || !(ofs2 & 3))) { - ofs = ofs2; - ref = ir->op1; - } else if (ofs == 0 && !(!LJ_SOFTFP && (ai & 0x08000000))) { - IRRef lref = ir->op1, rref = ir->op2; - Reg rn, rm; - if ((ai & 0x04000000)) { - IRRef sref = asm_fuselsl2(as, rref); - if (sref) { - rref = sref; - ai |= ARMF_SH(ARMSH_LSL, 2); - } else if ((sref = asm_fuselsl2(as, lref)) != 0) { - lref = rref; - rref = sref; - ai |= ARMF_SH(ARMSH_LSL, 2); - } - } - rn = ra_alloc1(as, lref, allow); - rm = ra_alloc1(as, rref, rset_exclude(allow, rn)); - if ((ai & 0x04000000)) ai |= ARMI_LS_R; - emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm); - return; - } - } else if (ir->o == IR_STRREF && !(!LJ_SOFTFP && (ai & 0x08000000))) { - lj_assertA(ofs == 0, "bad usage"); - ofs = (int32_t)sizeof(GCstr); - if (irref_isk(ir->op2)) { - ofs += IR(ir->op2)->i; - ref = ir->op1; - } else if (irref_isk(ir->op1)) { - ofs += IR(ir->op1)->i; - ref = ir->op2; - } else { - /* NYI: Fuse ADD with constant. */ - Reg rn = ra_alloc1(as, ir->op1, allow); - uint32_t m = asm_fuseopm(as, 0, ir->op2, rset_exclude(allow, rn)); - if ((ai & 0x04000000)) - emit_lso(as, ai, rd, rd, ofs); - else - emit_lsox(as, ai, rd, rd, ofs); - emit_dn(as, ARMI_ADD^m, rd, rn); - return; - } - if (ofs <= -lim || ofs >= lim) { - Reg rn = ra_alloc1(as, ref, allow); - Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn)); - if ((ai & 0x04000000)) ai |= ARMI_LS_R; - emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm); - return; - } - } - } - base = ra_alloc1(as, ref, allow); -#if !LJ_SOFTFP - if ((ai & 0x08000000)) - emit_vlso(as, ai, rd, base, ofs); - else -#endif - if ((ai & 0x04000000)) - emit_lso(as, ai, rd, base, ofs); - else - emit_lsox(as, ai, rd, base, ofs); -} - -#if !LJ_SOFTFP -/* -** Fuse to multiply-add/sub instruction. -** VMLA rounds twice (UMA, not FMA) -- no need to check for JIT_F_OPT_FMA. -** VFMA needs VFPv4, which is uncommon on the remaining ARM32 targets. -*/ -static int asm_fusemadd(ASMState *as, IRIns *ir, ARMIns ai, ARMIns air) -{ - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irm; - if (lref != rref && - ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && - ra_noreg(irm->r)) || - (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && - (rref = lref, ai = air, ra_noreg(irm->r))))) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg add = ra_hintalloc(as, rref, dest, RSET_FPR); - Reg right, left = ra_alloc2(as, irm, - rset_exclude(rset_exclude(RSET_FPR, dest), add)); - right = (left >> 8); left &= 255; - emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15)); - if (dest != add) emit_dm(as, ARMI_VMOV_D, (dest & 15), (add & 15)); - return 1; - } - return 0; -} -#endif - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_XNARGS(ci); - int32_t ofs = 0; -#if LJ_SOFTFP - Reg gpr = REGARG_FIRSTGPR; -#else - Reg gpr, fpr = REGARG_FIRSTFPR, fprodd = 0; -#endif - if ((void *)ci->func) - emit_call(as, (void *)ci->func); -#if !LJ_SOFTFP - for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) - as->cost[gpr] = REGCOST(~0u, ASMREF_L); - gpr = REGARG_FIRSTGPR; -#endif - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - IRIns *ir = IR(ref); -#if !LJ_SOFTFP - if (ref && irt_isfp(ir->t)) { - RegSet of = as->freeset; - Reg src; - if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) { - if (irt_isnum(ir->t)) { - if (fpr <= REGARG_LASTFPR) { - ra_leftov(as, fpr, ref); - fpr++; - continue; - } - } else if (fprodd) { /* Ick. */ - src = ra_alloc1(as, ref, RSET_FPR); - emit_dm(as, ARMI_VMOV_S, (fprodd & 15), (src & 15) | 0x00400000); - fprodd = 0; - continue; - } else if (fpr <= REGARG_LASTFPR) { - ra_leftov(as, fpr, ref); - fprodd = fpr++; - continue; - } - /* Workaround to protect argument GPRs from being used for remat. */ - as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); - src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */ - as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); - fprodd = 0; - goto stackfp; - } - /* Workaround to protect argument GPRs from being used for remat. */ - as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); - src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */ - as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); - if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1u; - if (gpr <= REGARG_LASTGPR) { - lj_assertA(rset_test(as->freeset, gpr), - "reg %d not free", gpr); /* Must have been evicted. */ - if (irt_isnum(ir->t)) { - lj_assertA(rset_test(as->freeset, gpr+1), - "reg %d not free", gpr+1); /* Ditto. */ - emit_dnm(as, ARMI_VMOV_RR_D, gpr, gpr+1, (src & 15)); - gpr += 2; - } else { - emit_dn(as, ARMI_VMOV_R_S, gpr, (src & 15)); - gpr++; - } - } else { - stackfp: - if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; - emit_spstore(as, ir, src, ofs); - ofs += irt_isnum(ir->t) ? 8 : 4; - } - } else -#endif - { - if (gpr <= REGARG_LASTGPR) { - lj_assertA(rset_test(as->freeset, gpr), - "reg %d not free", gpr); /* Must have been evicted. */ - if (ref) ra_leftov(as, gpr, ref); - gpr++; - } else { - if (ref) { - Reg r = ra_alloc1(as, ref, RSET_GPR); - emit_spstore(as, ir, r, ofs); - } - ofs += 4; - } - } - } -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)); - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lj_assertA(!irt_ispri(ir->t), "PRI dest"); - if (!LJ_SOFTFP && irt_isfp(ir->t)) { - if (LJ_ABI_SOFTFP || (ci->flags & (CCI_CASTU64|CCI_VARARG))) { - Reg dest = (ra_dest(as, ir, RSET_FPR) & 15); - if (irt_isnum(ir->t)) - emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, dest); - else - emit_dn(as, ARMI_VMOV_S_R, RID_RET, dest); - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } - UNUSED(ci); -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(void *)(irf->i); - } else { /* Need a non-argument register for indirect calls. */ - Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_R4, RID_R12+1)); - emit_m(as, ARMI_BLXr, freg); - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */ - /* Need to force a spill on REF_BASE now to update the stack slot. */ - emit_lso(as, ARMI_STR, base, RID_SP, ra_spill(as, IR(REF_BASE))); - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); - emit_nm(as, ARMI_CMP, RID_TMP, - ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); - emit_lso(as, ARMI_LDR, RID_TMP, base, -4); -} - -/* -- Buffer operations --------------------------------------------------- */ - -#if LJ_HASBUFFER -static void asm_bufhdr_write(ASMState *as, Reg sb) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); - IRIns irgc; - int32_t addr = i32ptr((void *)&J2G(as->J)->cur_L); - irgc.ot = IRT(0, IRT_PGC); /* GC type. */ - emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L)); - if ((as->flags & JIT_F_ARMV6T2)) { - emit_dnm(as, ARMI_BFI, RID_TMP, lj_fls(SBUF_MASK_FLAG), tmp); - } else { - emit_dnm(as, ARMI_ORR, RID_TMP, RID_TMP, tmp); - emit_dn(as, ARMI_AND|ARMI_K12|SBUF_MASK_FLAG, tmp, tmp); - } - emit_lso(as, ARMI_LDR, RID_TMP, - ra_allock(as, (addr & ~4095), - rset_exclude(rset_exclude(RSET_GPR, sb), tmp)), - (addr & 4095)); - emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L)); -} -#endif - -/* -- Type conversions ---------------------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_guardcc(as, CC_NE); - emit_d(as, ARMI_VMRS, 0); - emit_dm(as, ARMI_VCMP_D, (tmp & 15), (left & 15)); - emit_dm(as, ARMI_VCVT_F64_S32, (tmp & 15), (tmp & 15)); - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (left & 15)); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - emit_dnm(as, ARMI_VADD_D, (tmp & 15), (left & 15), (right & 15)); -} -#endif - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if !LJ_SOFTFP - int stfp = (st == IRT_NUM || st == IRT_FLOAT); -#endif - IRRef lref = ir->op1; - /* 64 bit integer conversions are handled by SPLIT. */ - lj_assertA(!irt_isint64(ir->t) && !(st == IRT_I64 || st == IRT_U64), - "IR %04d has unsplit 64 bit type", - (int)(ir - as->ir) - REF_BIAS); -#if LJ_SOFTFP - /* FP conversions are handled by SPLIT. */ - lj_assertA(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT), - "IR %04d has FP type", - (int)(ir - as->ir) - REF_BIAS); - /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */ -#else - lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV"); - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - emit_dm(as, st == IRT_NUM ? ARMI_VCVT_F32_F64 : ARMI_VCVT_F64_F32, - (dest & 15), (ra_alloc1(as, lref, RSET_FPR) & 15)); - } else { /* Integer to FP conversion. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - ARMIns ai = irt_isfloat(ir->t) ? - (st == IRT_INT ? ARMI_VCVT_F32_S32 : ARMI_VCVT_F32_U32) : - (st == IRT_INT ? ARMI_VCVT_F64_S32 : ARMI_VCVT_F64_U32); - emit_dm(as, ai, (dest & 15), (dest & 15)); - emit_dn(as, ARMI_VMOV_S_R, left, (dest & 15)); - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lj_assertA(irt_isint(ir->t) && st == IRT_NUM, - "bad type for checked CONV"); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - ARMIns ai; - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - ai = irt_isint(ir->t) ? - (st == IRT_NUM ? ARMI_VCVT_S32_F64 : ARMI_VCVT_S32_F32) : - (st == IRT_NUM ? ARMI_VCVT_U32_F64 : ARMI_VCVT_U32_F32); - emit_dm(as, ai, (tmp & 15), (left & 15)); - } - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT"); - if ((as->flags & JIT_F_ARMV6)) { - ARMIns ai = st == IRT_I8 ? ARMI_SXTB : - st == IRT_U8 ? ARMI_UXTB : - st == IRT_I16 ? ARMI_SXTH : ARMI_UXTH; - emit_dm(as, ai, dest, left); - } else if (st == IRT_U8) { - emit_dn(as, ARMI_AND|ARMI_K12|255, dest, left); - } else { - uint32_t shift = st == IRT_I8 ? 24 : 16; - ARMShift sh = st == IRT_U16 ? ARMSH_LSR : ARMSH_ASR; - emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, RID_TMP); - emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_LSL, shift), RID_TMP, left); - } - } else { /* Handle 32/32 bit no-op (cast). */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } -} - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - Reg rlo = 0, rhi = 0, tmp; - int destused = ra_used(ir); - int32_t ofs = 0; - ra_evictset(as, RSET_SCRATCH); -#if LJ_SOFTFP - if (destused) { - if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) && - (ir->s & 1) == 0 && ir->s + 1 == (ir+1)->s) { - int i; - for (i = 0; i < 2; i++) { - Reg r = (ir+i)->r; - if (ra_hasreg(r)) { - ra_free(as, r); - ra_modified(as, r); - emit_spload(as, ir+i, r, sps_scale((ir+i)->s)); - } - } - ofs = sps_scale(ir->s); - destused = 0; - } else { - rhi = ra_dest(as, ir+1, RSET_GPR); - rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi)); - } - } - asm_guardcc(as, CC_EQ); - if (destused) { - emit_lso(as, ARMI_LDR, rhi, RID_SP, 4); - emit_lso(as, ARMI_LDR, rlo, RID_SP, 0); - } -#else - UNUSED(rhi); - if (destused) { - if (ra_hasspill(ir->s)) { - ofs = sps_scale(ir->s); - destused = 0; - if (ra_hasreg(ir->r)) { - ra_free(as, ir->r); - ra_modified(as, ir->r); - emit_spload(as, ir, ir->r, ofs); - } - } else { - rlo = ra_dest(as, ir, RSET_FPR); - } - } - asm_guardcc(as, CC_EQ); - if (destused) - emit_vlso(as, ARMI_VLDR_D, rlo, RID_SP, 0); -#endif - emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - tmp = ra_releasetmp(as, ASMREF_TMP1); - if (ofs == 0) - emit_dm(as, ARMI_MOV, tmp, RID_SP); - else - emit_opk(as, ARMI_ADD, tmp, RID_SP, ofs, RSET_GPR); -} - -/* -- Memory references --------------------------------------------------- */ - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode) -{ - if ((mode & IRTMPREF_IN1)) { - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if ((mode & IRTMPREF_OUT1)) { -#if LJ_SOFTFP - lj_assertA(irref_isk(ref), "unsplit FP op"); - emit_dm(as, ARMI_MOV, dest, RID_SP); - emit_lso(as, ARMI_STR, - ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, RSET_GPR), - RID_SP, 0); - emit_lso(as, ARMI_STR, - ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, RSET_GPR), - RID_SP, 4); -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_dm(as, ARMI_MOV, dest, RID_SP); - emit_vlso(as, ARMI_VSTR_D, src, RID_SP, 0); -#endif - } else if (irref_isk(ref)) { - /* Use the number constant itself as a TValue. */ - ra_allockreg(as, i32ptr(ir_knum(ir)), dest); - } else { -#if LJ_SOFTFP - lj_assertA(0, "unsplit FP op"); -#else - /* Otherwise force a spill and use the spill slot. */ - emit_opk(as, ARMI_ADD, dest, RID_SP, ra_spill(as, ir), RSET_GPR); -#endif - } - } else { - /* Otherwise use [sp] and [sp+4] to hold the TValue. - ** This assumes the following call has max. 4 args. - */ - Reg type; - emit_dm(as, ARMI_MOV, dest, RID_SP); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_GPR); - emit_lso(as, ARMI_STR, src, RID_SP, 0); - } - if (LJ_SOFTFP && (ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)) - type = ra_alloc1(as, ref+1, RSET_GPR); - else - type = ra_allock(as, irt_toitype(ir->t), RSET_GPR); - emit_lso(as, ARMI_STR, type, RID_SP, 4); - } - } else { - emit_dm(as, ARMI_MOV, dest, RID_SP); - } -} - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - uint32_t k = emit_isk12(ARMI_ADD, ofs + 8*IR(ir->op2)->i); - if (k) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_dn(as, ARMI_ADD^k, dest, base); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, base, idx); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = 0, keyhi = 0, keynumhi = RID_NONE, tmp = RID_TMP; - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - IRType1 kt = irkey->t; - int32_t k = 0, khi = emit_isk12(ARMI_CMP, irt_toitype(kt)); - uint32_t khash; - MCLabel l_end, l_loop; - rset_clear(allow, tab); - if (!irref_isk(refkey) || irt_isstr(kt)) { -#if LJ_SOFTFP - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - if (irkey[1].o == IR_HIOP) { - if (ra_hasreg((irkey+1)->r)) { - keynumhi = (irkey+1)->r; - keyhi = RID_TMP; - ra_noweak(as, keynumhi); - } else { - keyhi = keynumhi = ra_allocref(as, refkey+1, allow); - } - rset_clear(allow, keynumhi); - khi = 0; - } -#else - if (irt_isnum(kt)) { - key = ra_scratch(as, allow); - rset_clear(allow, key); - keyhi = keynumhi = ra_scratch(as, allow); - rset_clear(allow, keyhi); - khi = 0; - } else { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } -#endif - } else if (irt_isnum(kt)) { - int32_t val = (int32_t)ir_knum(irkey)->u32.lo; - k = emit_isk12(ARMI_CMP, val); - if (!k) { - key = ra_allock(as, val, allow); - rset_clear(allow, key); - } - val = (int32_t)ir_knum(irkey)->u32.hi; - khi = emit_isk12(ARMI_CMP, val); - if (!khi) { - keyhi = ra_allock(as, val, allow); - rset_clear(allow, keyhi); - } - } else if (!irt_ispri(kt)) { - k = emit_isk12(ARMI_CMP, irkey->i); - if (!k) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } - } - if (!irt_ispri(kt)) - tmp = ra_scratchpair(as, allow); - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - as->invmcp = NULL; - if (merge == IR_NE) - asm_guardcc(as, CC_AL); - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - - /* Follow hash chain until the end. */ - l_loop = --as->mcp; - emit_n(as, ARMI_CMP|ARMI_K12|0, dest); - emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(Node, next)); - - /* Type and value comparison. */ - if (merge == IR_EQ) - asm_guardcc(as, CC_EQ); - else - emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); - if (!irt_ispri(kt)) { - emit_nm(as, ARMF_CC(ARMI_CMP, CC_EQ)^k, tmp, key); - emit_nm(as, ARMI_CMP^khi, tmp+1, keyhi); - emit_lsox(as, ARMI_LDRD, tmp, dest, (int32_t)offsetof(Node, key)); - } else { - emit_n(as, ARMI_CMP^khi, tmp); - emit_lso(as, ARMI_LDR, tmp, dest, (int32_t)offsetof(Node, key.it)); - } - *l_loop = ARMF_CC(ARMI_B, CC_NE) | ((as->mcp-l_loop-2) & 0x00ffffffu); - - /* Load main position relative to tab->node into dest. */ - khash = irref_isk(refkey) ? ir_khash(as, irkey) : 1; - if (khash == 0) { - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - } else { - emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, dest, tmp); - emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 1), tmp, tmp, tmp); - if (irt_isstr(kt)) { /* Fetch of str->sid is cheaper than ra_allock. */ - emit_dnm(as, ARMI_AND, tmp, tmp+1, RID_TMP); - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_lso(as, ARMI_LDR, tmp+1, key, (int32_t)offsetof(GCstr, sid)); - emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); - } else if (irref_isk(refkey)) { - emit_opk(as, ARMI_AND, tmp, RID_TMP, (int32_t)khash, - rset_exclude(rset_exclude(RSET_GPR, tab), dest)); - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); - } else { /* Must match with hash*() in lj_tab.c. */ - if (ra_hasreg(keynumhi)) { /* Canonicalize +-0.0 to 0.0. */ - if (keyhi == RID_TMP) - emit_dm(as, ARMF_CC(ARMI_MOV, CC_NE), keyhi, keynumhi); - emit_d(as, ARMF_CC(ARMI_MOV, CC_EQ)|ARMI_K12|0, keyhi); - } - emit_dnm(as, ARMI_AND, tmp, tmp, RID_TMP); - emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT3), tmp, tmp, tmp+1); - emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 32-((HASH_ROT2+HASH_ROT1)&31)), - tmp, tmp+1, tmp); - emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); - emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT1), tmp+1, tmp+1, tmp); - if (ra_hasreg(keynumhi)) { - emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); - emit_dnm(as, ARMI_ORR|ARMI_S, RID_TMP, tmp, key); /* Test for +-0.0. */ - emit_dnm(as, ARMI_ADD, tmp, keynumhi, keynumhi); -#if !LJ_SOFTFP - emit_dnm(as, ARMI_VMOV_RR_D, key, keynumhi, - (ra_alloc1(as, refkey, RSET_FPR) & 15)); -#endif - } else { - emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); - emit_opk(as, ARMI_ADD, tmp, key, (int32_t)HASH_BIAS, - rset_exclude(rset_exclude(RSET_GPR, tab), key)); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - Reg dest = (ra_used(ir) || ofs > 4095) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - Reg key = RID_NONE, type = RID_TMP, idx = node; - RegSet allow = rset_exclude(RSET_GPR, node); - lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot"); - if (ofs > 4095) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_opk(as, ARMI_ADD, dest, node, ofs, allow); - } - asm_guardcc(as, CC_NE); - if (!irt_ispri(irkey->t)) { - RegSet even = (as->freeset & allow); - even = even & (even >> 1) & RSET_GPREVEN; - if (even) { - key = ra_scratch(as, even); - if (rset_test(as->freeset, key+1)) { - type = key+1; - ra_modified(as, type); - } - } else { - key = ra_scratch(as, allow); - } - rset_clear(allow, key); - } - rset_clear(allow, type); - if (irt_isnum(irkey->t)) { - emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, type, - (int32_t)ir_knum(irkey)->u32.hi, allow); - emit_opk(as, ARMI_CMP, 0, key, - (int32_t)ir_knum(irkey)->u32.lo, allow); - } else { - if (ra_hasreg(key)) - emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, key, irkey->i, allow); - emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype(irkey->t), type); - } - emit_lso(as, ARMI_LDR, type, idx, kofs+4); - if (ra_hasreg(key)) emit_lso(as, ARMI_LDR, key, idx, kofs); - if (ofs > 4095) - emit_opk(as, ARMI_ADD, dest, node, ofs, RSET_GPR); -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, ARMI_LDR, dest, v); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guardcc(as, CC_NE); - emit_n(as, ARMI_CMP|ARMI_K12|1, RID_TMP); - emit_opk(as, ARMI_ADD, dest, uv, - (int32_t)offsetof(GCupval, tv), RSET_GPR); - emit_lso(as, ARMI_LDRB, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_lso(as, ARMI_LDR, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_lso(as, ARMI_LDR, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lj_assertA(!ra_used(ir), "unfused FREF"); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRRef ref = ir->op2, refk = ir->op1; - Reg r; - if (irref_isk(ref)) { - IRRef tmp = refk; refk = ref; ref = tmp; - } else if (!irref_isk(refk)) { - uint32_t k, m = ARMI_K12|sizeof(GCstr); - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - IRIns *irr = IR(ir->op2); - if (ra_hasreg(irr->r)) { - ra_noweak(as, irr->r); - right = irr->r; - } else if (mayfuse(as, irr->op2) && - irr->o == IR_ADD && irref_isk(irr->op2) && - (k = emit_isk12(ARMI_ADD, - (int32_t)sizeof(GCstr) + IR(irr->op2)->i))) { - m = k; - right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); - } else { - right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_dn(as, ARMI_ADD^m, dest, dest); - emit_dnm(as, ARMI_ADD, dest, left, right); - return; - } - r = ra_alloc1(as, ref, RSET_GPR); - emit_opk(as, ARMI_ADD, dest, r, - sizeof(GCstr) + IR(refk)->i, rset_exclude(RSET_GPR, r)); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static ARMIns asm_fxloadins(ASMState *as, IRIns *ir) -{ - UNUSED(as); - switch (irt_type(ir->t)) { - case IRT_I8: return ARMI_LDRSB; - case IRT_U8: return ARMI_LDRB; - case IRT_I16: return ARMI_LDRSH; - case IRT_U16: return ARMI_LDRH; - case IRT_NUM: lj_assertA(!LJ_SOFTFP, "unsplit FP op"); return ARMI_VLDR_D; - case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VLDR_S; /* fallthrough */ - default: return ARMI_LDR; - } -} - -static ARMIns asm_fxstoreins(ASMState *as, IRIns *ir) -{ - UNUSED(as); - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return ARMI_STRB; - case IRT_I16: case IRT_U16: return ARMI_STRH; - case IRT_NUM: lj_assertA(!LJ_SOFTFP, "unsplit FP op"); return ARMI_VSTR_D; - case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VSTR_S; /* fallthrough */ - default: return ARMI_STR; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - ARMIns ai = asm_fxloadins(as, ir); - Reg idx; - int32_t ofs; - if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */ - idx = ra_allock(as, (int32_t)(ir->op2<<2) + (int32_t)J2GG(as->J), RSET_GPR); - ofs = 0; - } else { - idx = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_dn(as, ARMI_ADD|ARMI_K12|ofs, dest, idx); - return; - } - } - ofs = field_ofs[ir->op2]; - } - if ((ai & 0x04000000)) - emit_lso(as, ai, dest, idx, ofs); - else - emit_lsox(as, ai, dest, idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - ARMIns ai = asm_fxstoreins(as, ir); - if ((ai & 0x04000000)) - emit_lso(as, ai, src, idx, ofs); - else - emit_lsox(as, ai, src, idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - lj_assertA(!(ir->op2 & IRXLOAD_UNALIGNED), "unaligned XLOAD"); - asm_fusexref(as, asm_fxloadins(as, ir), dest, ir->op1, RSET_GPR, 0); -} - -static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(as, ir), src, ir->op1, - rset_exclude(RSET_GPR, src), ofs); - } -} - -#define asm_xstore(as, ir) asm_xstore_(as, ir, 0) - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - IRType t = hiop ? IRT_NUM : irt_type(ir->t); - Reg dest = RID_NONE, type = RID_NONE, idx; - RegSet allow = RSET_GPR; - int32_t ofs = 0; - if (hiop && ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } - if (ra_used(ir)) { - lj_assertA((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t), - "bad load type %d", irt_type(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow); - rset_clear(allow, dest); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow, - (!LJ_SOFTFP && t == IRT_NUM) ? 1024 : 4096); - if (ir->o == IR_VLOAD) ofs += 8 * ir->op2; - if (!hiop || type == RID_NONE) { - rset_clear(allow, idx); - if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && - rset_test((as->freeset & allow), dest+1)) { - type = dest+1; - ra_modified(as, type); - } else { - type = RID_TMP; - } - } - asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); - emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); - if (ra_hasreg(dest)) { -#if !LJ_SOFTFP - if (t == IRT_NUM) - emit_vlso(as, ARMI_VLDR_D, dest, idx, ofs); - else -#endif - emit_lso(as, ARMI_LDR, dest, idx, ofs); - } - emit_lso(as, ARMI_LDR, type, idx, ofs+4); -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, type = RID_NONE; - int32_t ofs = 0; -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, RSET_FPR); - idx = asm_fuseahuref(as, ir->op1, &ofs, allow, 1024); - emit_vlso(as, ARMI_VSTR_D, src, idx, ofs); - } else -#endif - { - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - } - if (hiop) - type = ra_alloc1(as, (ir+1)->op2, allow); - else - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type), 4096); - if (ra_hasreg(src)) emit_lso(as, ARMI_STR, src, idx, ofs); - emit_lso(as, ARMI_STR, type, idx, ofs+4); - } - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - IRType t = hiop ? IRT_NUM : irt_type(ir->t); - Reg dest = RID_NONE, type = RID_NONE, base; - RegSet allow = RSET_GPR; - lj_assertA(!(ir->op2 & IRSLOAD_PARENT), - "bad parent SLOAD"); /* Handled by asm_head_side(). */ - lj_assertA(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK), - "inconsistent SLOAD variant"); -#if LJ_SOFTFP - lj_assertA(!(ir->op2 & IRSLOAD_CONVERT), - "unsplit SLOAD convert"); /* Handled by LJ_SOFTFP SPLIT. */ - if (hiop && ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } -#else - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(ir->t) && t == IRT_INT) { - dest = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, dest); - t = IRT_NUM; /* Continue with a regular number type check. */ - } else -#endif - if (ra_used(ir)) { - Reg tmp = RID_NONE; - if ((ir->op2 & IRSLOAD_CONVERT)) - tmp = ra_scratch(as, t == IRT_INT ? RSET_FPR : RSET_GPR); - lj_assertA((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t), - "bad SLOAD type %d", irt_type(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow); - rset_clear(allow, dest); - base = ra_alloc1(as, REF_BASE, allow); - if ((ir->op2 & IRSLOAD_CONVERT)) { - if (t == IRT_INT) { - emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); - emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (tmp & 15)); - t = IRT_NUM; /* Check for original type. */ - } else { - emit_dm(as, ARMI_VCVT_F64_S32, (dest & 15), (dest & 15)); - emit_dn(as, ARMI_VMOV_S_R, tmp, (dest & 15)); - t = IRT_INT; /* Check for original type. */ - } - dest = tmp; - } - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); -dotypecheck: - rset_clear(allow, base); - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - if (ra_noreg(type)) { - if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && - rset_test((as->freeset & allow), dest+1)) { - type = dest+1; - ra_modified(as, type); - } else { - type = RID_TMP; - } - } - asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); - if ((ir->op2 & IRSLOAD_KEYINDEX)) { - emit_n(as, ARMI_CMN|ARMI_K12|1, type); - emit_dn(as, ARMI_EOR^emit_isk12(ARMI_EOR, ~LJ_KEYINDEX), type, type); - } else { - emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); - } - } - if (ra_hasreg(dest)) { -#if !LJ_SOFTFP - if (t == IRT_NUM) { - if (ofs < 1024) { - emit_vlso(as, ARMI_VLDR_D, dest, base, ofs); - } else { - if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); - emit_vlso(as, ARMI_VLDR_D, dest, RID_TMP, 0); - emit_opk(as, ARMI_ADD, RID_TMP, base, ofs, allow); - return; - } - } else -#endif - emit_lso(as, ARMI_LDR, dest, base, ofs); - } - if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID id = (CTypeID)IR(ir->op1)->i; - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[4]; - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - RegSet drop = RSET_SCRATCH; - lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL), - "bad CNEW/CNEWI operands"); - - as->gcsteps++; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - if (ra_used(ir)) - ra_destreg(as, ir, RID_RET); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - int32_t ofs = sizeof(GCcdata); - lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz); - if (sz == 8) { - ofs += 4; ir++; - lj_assertA(ir->o == IR_HIOP, "expected HIOP for CNEWI"); - } - for (;;) { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_lso(as, ARMI_STR, r, RID_RET, ofs); - rset_clear(allow, r); - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; ir--; - } - } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */ - ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* CTypeID id */ - args[2] = ir->op2; /* CTSize sz */ - args[3] = ASMREF_TMP1; /* CTSize align */ - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info)); - return; - } - - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - { - uint32_t k = emit_isk12(ARMI_MOV, id); - Reg r = k ? RID_R1 : ra_allock(as, id, allow); - emit_lso(as, ARMI_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct)); - emit_lsox(as, ARMI_STRH, r, RID_RET, offsetof(GCcdata, ctypeid)); - emit_d(as, ARMI_MOV|ARMI_K12|~LJ_TCDATA, RID_TMP); - if (k) emit_d(as, ARMI_MOV^k, RID_R1); - } - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg gr = ra_allock(as, i32ptr(J2G(as->J)), - rset_exclude(rset_exclude(RSET_GPR, tab), link)); - Reg mark = RID_TMP; - MCLabel l_end = emit_label(as); - emit_lso(as, ARMI_STR, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_lso(as, ARMI_STRB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_lso(as, ARMI_STR, tab, gr, - (int32_t)offsetof(global_State, gc.grayagain)); - emit_dn(as, ARMI_BIC|ARMI_K12|LJ_GC_BLACK, mark, mark); - emit_lso(as, ARMI_LDR, link, gr, - (int32_t)offsetof(global_State, gc.grayagain)); - emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); - emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_BLACK, mark); - emit_lso(as, ARMI_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type"); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - if ((l_end[-1] >> 28) == CC_AL) - l_end[-1] = ARMF_CC(l_end[-1], CC_NE); - else - emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); - ra_allockreg(as, i32ptr(J2G(as->J)), ra_releasetmp(as, ASMREF_TMP1)); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); - emit_n(as, ARMF_CC(ARMI_TST, CC_NE)|ARMI_K12|LJ_GC_BLACK, tmp); - emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_WHITES, RID_TMP); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_lso(as, ARMI_LDRB, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_lso(as, ARMI_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_fparith(ASMState *as, IRIns *ir, ARMIns ai) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15)); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, ARMIns ai) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_dm(as, ai, (dest & 15), (left & 15)); -} - -static void asm_callround(ASMState *as, IRIns *ir, int id) -{ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RID2RSET(RID_R0)|RID2RSET(RID_R1)|RID2RSET(RID_R2)| - RID2RSET(RID_R3)|RID2RSET(RID_R12); - RegSet of; - Reg dest, src; - ra_evictset(as, drop); - dest = ra_dest(as, ir, RSET_FPR); - emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, (dest & 15)); - emit_call(as, id == IRFPM_FLOOR ? (void *)lj_vm_floor_sf : - id == IRFPM_CEIL ? (void *)lj_vm_ceil_sf : - (void *)lj_vm_trunc_sf); - /* Workaround to protect argument GPRs from being used for remat. */ - of = as->freeset; - as->freeset &= ~RSET_RANGE(RID_R0, RID_R1+1); - as->cost[RID_R0] = as->cost[RID_R1] = REGCOST(~0u, ASMREF_L); - src = ra_alloc1(as, ir->op1, RSET_FPR); /* May alloc GPR to remat FPR. */ - as->freeset |= (of & RSET_RANGE(RID_R0, RID_R1+1)); - emit_dnm(as, ARMI_VMOV_RR_D, RID_R0, RID_R1, (src & 15)); -} - -static void asm_fpmath(ASMState *as, IRIns *ir) -{ - if (ir->op2 <= IRFPM_TRUNC) - asm_callround(as, ir, ir->op2); - else if (ir->op2 == IRFPM_SQRT) - asm_fpunary(as, ir, ARMI_VSQRT_D); - else - asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); -} -#endif - -static int asm_swapops(ASMState *as, IRRef lref, IRRef rref) -{ - IRIns *ir; - if (irref_isk(rref)) - return 0; /* Don't swap constants to the left. */ - if (irref_isk(lref)) - return 1; /* But swap constants to the right. */ - ir = IR(rref); - if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || - (ir->o == IR_ADD && ir->op1 == ir->op2)) - return 0; /* Don't swap fusable operands to the left. */ - ir = IR(lref); - if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || - (ir->o == IR_ADD && ir->op1 == ir->op2)) - return 1; /* But swap fusable operands to the right. */ - return 0; /* Otherwise don't swap. */ -} - -static void asm_intop(ASMState *as, IRIns *ir, ARMIns ai) -{ - IRRef lref = ir->op1, rref = ir->op2; - Reg left, dest = ra_dest(as, ir, RSET_GPR); - uint32_t m; - if (asm_swapops(as, lref, rref)) { - IRRef tmp = lref; lref = rref; rref = tmp; - if ((ai & ~ARMI_S) == ARMI_SUB || (ai & ~ARMI_S) == ARMI_SBC) - ai ^= (ARMI_SUB^ARMI_RSB); - } - left = ra_hintalloc(as, lref, dest, RSET_GPR); - m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left)); - if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */ - asm_guardcc(as, CC_VS); - ai |= ARMI_S; - } - emit_dn(as, ai^m, dest, left); -} - -/* Try to drop cmp r, #0. */ -static ARMIns asm_drop_cmp0(ASMState *as, ARMIns ai) -{ - if (as->flagmcp == as->mcp) { - uint32_t cc = (as->mcp[1] >> 28); - as->flagmcp = NULL; - if (cc <= CC_NE) { - as->mcp++; - ai |= ARMI_S; - } else if (cc == CC_GE) { - *++as->mcp ^= ((CC_GE^CC_PL) << 28); - ai |= ARMI_S; - } else if (cc == CC_LT) { - *++as->mcp ^= ((CC_LT^CC_MI) << 28); - ai |= ARMI_S; - } /* else: other conds don't work in general. */ - } - return ai; -} - -static void asm_intop_s(ASMState *as, IRIns *ir, ARMIns ai) -{ - asm_intop(as, ir, asm_drop_cmp0(as, ai)); -} - -static void asm_intneg(ASMState *as, IRIns *ir, ARMIns ai) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_dn(as, ai|ARMI_K12|0, dest, left); -} - -/* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */ -static void asm_intmul(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest)); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - Reg tmp = RID_NONE; - /* ARMv5 restriction: dest != left and dest_hi != left. */ - if (dest == left && left != right) { left = right; right = dest; } - if (irt_isguard(ir->t)) { /* IR_MULOV */ - if (!(as->flags & JIT_F_ARMV6) && dest == left) - tmp = left = ra_scratch(as, rset_exclude(RSET_GPR, left)); - asm_guardcc(as, CC_NE); - emit_nm(as, ARMI_TEQ|ARMF_SH(ARMSH_ASR, 31), RID_TMP, dest); - emit_dnm(as, ARMI_SMULL|ARMF_S(right), dest, RID_TMP, left); - } else { - if (!(as->flags & JIT_F_ARMV6) && dest == left) tmp = left = RID_TMP; - emit_nm(as, ARMI_MUL|ARMF_S(right), dest, left); - } - /* Only need this for the dest == left == right case. */ - if (ra_hasreg(tmp)) emit_dm(as, ARMI_MOV, tmp, right); -} - -static void asm_add(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, ARMI_VMLA_D, ARMI_VMLA_D)) - asm_fparith(as, ir, ARMI_VADD_D); - return; - } -#endif - asm_intop_s(as, ir, ARMI_ADD); -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, ARMI_VNMLS_D, ARMI_VMLS_D)) - asm_fparith(as, ir, ARMI_VSUB_D); - return; - } -#endif - asm_intop_s(as, ir, ARMI_SUB); -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, ARMI_VMUL_D); - return; - } -#endif - asm_intmul(as, ir); -} - -#define asm_addov(as, ir) asm_add(as, ir) -#define asm_subov(as, ir) asm_sub(as, ir) -#define asm_mulov(as, ir) asm_mul(as, ir) - -#if !LJ_SOFTFP -#define asm_fpdiv(as, ir) asm_fparith(as, ir, ARMI_VDIV_D) -#define asm_abs(as, ir) asm_fpunary(as, ir, ARMI_VABS_D) -#endif - -static void asm_neg(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, ARMI_VNEG_D); - return; - } -#endif - asm_intneg(as, ir, ARMI_RSB); -} - -static void asm_bitop(ASMState *as, IRIns *ir, ARMIns ai) -{ - ai = asm_drop_cmp0(as, ai); - if (ir->op2 == 0) { - Reg dest = ra_dest(as, ir, RSET_GPR); - uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR); - emit_d(as, ai^m, dest); - } else { - /* NYI: Turn BAND !k12 into uxtb, uxth or bfc or shl+shr. */ - asm_intop(as, ir, ai); - } -} - -#define asm_bnot(as, ir) asm_bitop(as, ir, ARMI_MVN) - -static void asm_bswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - if ((as->flags & JIT_F_ARMV6)) { - emit_dm(as, ARMI_REV, dest, left); - } else { - Reg tmp2 = dest; - if (tmp2 == left) - tmp2 = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, dest), left)); - emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_LSR, 8), dest, tmp2, RID_TMP); - emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_ROR, 8), tmp2, left); - emit_dn(as, ARMI_BIC|ARMI_K12|256*8|255, RID_TMP, RID_TMP); - emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 16), RID_TMP, left, left); - } -} - -#define asm_band(as, ir) asm_bitop(as, ir, ARMI_AND) -#define asm_bor(as, ir) asm_bitop(as, ir, ARMI_ORR) -#define asm_bxor(as, ir) asm_bitop(as, ir, ARMI_EOR) - -static void asm_bitshift(ASMState *as, IRIns *ir, ARMShift sh) -{ - if (irref_isk(ir->op2)) { /* Constant shifts. */ - /* NYI: Turn SHL+SHR or BAND+SHR into uxtb, uxth or ubfx. */ - /* NYI: Turn SHL+ASR into sxtb, sxth or sbfx. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - int32_t shift = (IR(ir->op2)->i & 31); - emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, left); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dm(as, ARMI_MOV|ARMF_RSH(sh, right), dest, left); - } -} - -#define asm_bshl(as, ir) asm_bitshift(as, ir, ARMSH_LSL) -#define asm_bshr(as, ir) asm_bitshift(as, ir, ARMSH_LSR) -#define asm_bsar(as, ir) asm_bitshift(as, ir, ARMSH_ASR) -#define asm_bror(as, ir) asm_bitshift(as, ir, ARMSH_ROR) -#define asm_brol(as, ir) lj_assertA(0, "unexpected BROL") - -static void asm_intmin_max(ASMState *as, IRIns *ir, int cc) -{ - uint32_t kcmp = 0, kmov = 0; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - Reg right = 0; - if (irref_isk(ir->op2)) { - kcmp = emit_isk12(ARMI_CMP, IR(ir->op2)->i); - if (kcmp) kmov = emit_isk12(ARMI_MOV, IR(ir->op2)->i); - } - if (!kmov) { - kcmp = 0; - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - if (kmov || dest != right) { - emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, right); - cc ^= 1; /* Must use opposite conditions for paired moves. */ - } else { - cc ^= (CC_LT^CC_GT); /* Otherwise may swap CC_LT <-> CC_GT. */ - } - if (dest != left) emit_dm(as, ARMF_CC(ARMI_MOV, cc), dest, left); - emit_nm(as, ARMI_CMP^kcmp, left, right); -} - -#if LJ_SOFTFP -static void asm_sfpmin_max(ASMState *as, IRIns *ir, int cc) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; - RegSet drop = RSET_SCRATCH; - Reg r; - IRRef args[4]; - args[0] = ir->op1; args[1] = (ir+1)->op1; - args[2] = ir->op2; args[3] = (ir+1)->op2; - /* __aeabi_cdcmple preserves r0-r3. */ - if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); - if (ra_hasreg((ir+1)->r)) rset_clear(drop, (ir+1)->r); - if (!rset_test(as->freeset, RID_R2) && - regcost_ref(as->cost[RID_R2]) == args[2]) rset_clear(drop, RID_R2); - if (!rset_test(as->freeset, RID_R3) && - regcost_ref(as->cost[RID_R3]) == args[3]) rset_clear(drop, RID_R3); - ra_evictset(as, drop); - ra_destpair(as, ir); - emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETHI, RID_R3); - emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETLO, RID_R2); - emit_call(as, (void *)ci->func); - for (r = RID_R0; r <= RID_R3; r++) - ra_leftov(as, r, args[r-RID_R0]); -} -#else -static void asm_fpmin_max(ASMState *as, IRIns *ir, int cc) -{ - Reg dest = (ra_dest(as, ir, RSET_FPR) & 15); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = ((left >> 8) & 15); left &= 15; - if (dest != left) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc^1), dest, left); - if (dest != right) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc), dest, right); - emit_d(as, ARMI_VMRS, 0); - emit_dm(as, ARMI_VCMP_D, left, right); -} -#endif - -static void asm_min_max(ASMState *as, IRIns *ir, int cc, int fcc) -{ -#if LJ_SOFTFP - UNUSED(fcc); -#else - if (irt_isnum(ir->t)) - asm_fpmin_max(as, ir, fcc); - else -#endif - asm_intmin_max(as, ir, cc); -} - -#define asm_min(as, ir) asm_min_max(as, ir, CC_GT, CC_PL) -#define asm_max(as, ir) asm_min_max(as, ir, CC_LT, CC_LE) - -/* -- Comparisons --------------------------------------------------------- */ - -/* Map of comparisons to flags. ORDER IR. */ -static const uint8_t asm_compmap[IR_ABC+1] = { - /* op FP swp int cc FP cc */ - /* LT */ CC_GE + (CC_HS << 4), - /* GE x */ CC_LT + (CC_HI << 4), - /* LE */ CC_GT + (CC_HI << 4), - /* GT x */ CC_LE + (CC_HS << 4), - /* ULT x */ CC_HS + (CC_LS << 4), - /* UGE */ CC_LO + (CC_LO << 4), - /* ULE x */ CC_HI + (CC_LO << 4), - /* UGT */ CC_LS + (CC_LS << 4), - /* EQ */ CC_NE + (CC_NE << 4), - /* NE */ CC_EQ + (CC_EQ << 4), - /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */ -}; - -#if LJ_SOFTFP -/* FP comparisons. */ -static void asm_sfpcomp(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; - RegSet drop = RSET_SCRATCH; - Reg r; - IRRef args[4]; - int swp = (((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1) << 1); - args[swp^0] = ir->op1; args[swp^1] = (ir+1)->op1; - args[swp^2] = ir->op2; args[swp^3] = (ir+1)->op2; - /* __aeabi_cdcmple preserves r0-r3. This helps to reduce spills. */ - for (r = RID_R0; r <= RID_R3; r++) - if (!rset_test(as->freeset, r) && - regcost_ref(as->cost[r]) == args[r-RID_R0]) rset_clear(drop, r); - ra_evictset(as, drop); - asm_guardcc(as, (asm_compmap[ir->o] >> 4)); - emit_call(as, (void *)ci->func); - for (r = RID_R0; r <= RID_R3; r++) - ra_leftov(as, r, args[r-RID_R0]); -} -#else -/* FP comparisons. */ -static void asm_fpcomp(ASMState *as, IRIns *ir) -{ - Reg left, right; - ARMIns ai; - int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1); - if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) { - left = (ra_alloc1(as, ir->op1, RSET_FPR) & 15); - right = 0; - ai = ARMI_VCMPZ_D; - } else { - left = ra_alloc2(as, ir, RSET_FPR); - if (swp) { - right = (left & 15); left = ((left >> 8) & 15); - } else { - right = ((left >> 8) & 15); left &= 15; - } - ai = ARMI_VCMP_D; - } - asm_guardcc(as, (asm_compmap[ir->o] >> 4)); - emit_d(as, ARMI_VMRS, 0); - emit_dm(as, ai, left, right); -} -#endif - -/* Integer comparisons. */ -static void asm_intcomp(ASMState *as, IRIns *ir) -{ - ARMCC cc = (asm_compmap[ir->o] & 15); - IRRef lref = ir->op1, rref = ir->op2; - Reg left; - uint32_t m; - int cmpprev0 = 0; - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t), - "bad comparison data type %d", irt_type(ir->t)); - if (asm_swapops(as, lref, rref)) { - Reg tmp = lref; lref = rref; rref = tmp; - if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */ - else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */ - } - if (irref_isk(rref) && IR(rref)->i == 0) { - IRIns *irl = IR(lref); - cmpprev0 = (irl+1 == ir); - /* Combine comp(BAND(left, right), 0) into tst left, right. */ - if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) { - IRRef blref = irl->op1, brref = irl->op2; - uint32_t m2 = 0; - Reg bleft; - if (asm_swapops(as, blref, brref)) { - Reg tmp = blref; blref = brref; brref = tmp; - } - if (irref_isk(brref)) { - m2 = emit_isk12(ARMI_AND, IR(brref)->i); - if ((m2 & (ARMI_AND^ARMI_BIC))) - goto notst; /* Not beneficial if we miss a constant operand. */ - } - if (cc == CC_GE) cc = CC_PL; - else if (cc == CC_LT) cc = CC_MI; - else if (cc > CC_NE) goto notst; /* Other conds don't work with tst. */ - bleft = ra_alloc1(as, blref, RSET_GPR); - if (!m2) m2 = asm_fuseopm(as, 0, brref, rset_exclude(RSET_GPR, bleft)); - asm_guardcc(as, cc); - emit_n(as, ARMI_TST^m2, bleft); - return; - } - } -notst: - left = ra_alloc1(as, lref, RSET_GPR); - m = asm_fuseopm(as, ARMI_CMP, rref, rset_exclude(RSET_GPR, left)); - asm_guardcc(as, cc); - emit_n(as, ARMI_CMP^m, left); - /* Signed comparison with zero and referencing previous ins? */ - if (cmpprev0 && (cc <= CC_NE || cc >= CC_GE)) - as->flagmcp = as->mcp; /* Allow elimination of the compare. */ -} - -static void asm_comp(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) - asm_fpcomp(as, ir); - else -#endif - asm_intcomp(as, ir); -} - -#define asm_equal(as, ir) asm_comp(as, ir) - -#if LJ_HASFFI -/* 64 bit integer comparisons. */ -static void asm_int64comp(ASMState *as, IRIns *ir) -{ - int signedcomp = (ir->o <= IR_GT); - ARMCC cclo, cchi; - Reg leftlo, lefthi; - uint32_t mlo, mhi; - RegSet allow = RSET_GPR, oldfree; - - /* Always use unsigned comparison for loword. */ - cclo = asm_compmap[ir->o + (signedcomp ? 4 : 0)] & 15; - leftlo = ra_alloc1(as, ir->op1, allow); - oldfree = as->freeset; - mlo = asm_fuseopm(as, ARMI_CMP, ir->op2, rset_clear(allow, leftlo)); - allow &= ~(oldfree & ~as->freeset); /* Update for allocs of asm_fuseopm. */ - - /* Use signed or unsigned comparison for hiword. */ - cchi = asm_compmap[ir->o] & 15; - lefthi = ra_alloc1(as, (ir+1)->op1, allow); - mhi = asm_fuseopm(as, ARMI_CMP, (ir+1)->op2, rset_clear(allow, lefthi)); - - /* All register allocations must be performed _before_ this point. */ - if (signedcomp) { - MCLabel l_around = emit_label(as); - asm_guardcc(as, cclo); - emit_n(as, ARMI_CMP^mlo, leftlo); - emit_branch(as, ARMF_CC(ARMI_B, CC_NE), l_around); - if (cchi == CC_GE || cchi == CC_LE) cchi ^= 6; /* GE -> GT, LE -> LT */ - asm_guardcc(as, cchi); - } else { - asm_guardcc(as, cclo); - emit_n(as, ARMF_CC(ARMI_CMP, CC_EQ)^mlo, leftlo); - } - emit_n(as, ARMI_CMP^mhi, lefthi); -} -#endif - -/* -- Split register ops -------------------------------------------------- */ - -/* Hiword op of a split 32/32 bit op. Previous op is the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; -#if LJ_HASFFI || LJ_SOFTFP - if ((ir-1)->o <= IR_NE) { /* 64 bit integer or FP comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ -#if LJ_SOFTFP - if (!irt_isint(ir->t)) { - asm_sfpcomp(as, ir-1); - return; - } -#endif -#if LJ_HASFFI - asm_int64comp(as, ir-1); -#endif - return; -#if LJ_SOFTFP - } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { - as->curins--; /* Always skip the loword min/max. */ - if (uselo || usehi) - asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_PL : CC_LE); - return; -#elif LJ_HASFFI - } else if ((ir-1)->o == IR_CONV) { - as->curins--; /* Always skip the CONV. */ - if (usehi || uselo) - asm_conv64(as, ir); - return; -#endif - } else if ((ir-1)->o == IR_XSTORE) { - if ((ir-1)->r != RID_SINK) - asm_xstore_(as, ir, 4); - return; - } -#endif - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { -#if LJ_HASFFI - case IR_ADD: - as->curins--; - asm_intop(as, ir, ARMI_ADC); - asm_intop(as, ir-1, ARMI_ADD|ARMI_S); - break; - case IR_SUB: - as->curins--; - asm_intop(as, ir, ARMI_SBC); - asm_intop(as, ir-1, ARMI_SUB|ARMI_S); - break; - case IR_NEG: - as->curins--; - asm_intneg(as, ir, ARMI_RSC); - asm_intneg(as, ir-1, ARMI_RSB|ARMI_S); - break; - case IR_CNEWI: - /* Nothing to do here. Handled by lo op itself. */ - break; -#endif -#if LJ_SOFTFP - case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - case IR_STRTO: - if (!uselo) - ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */ - break; - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: case IR_TMPREF: - /* Nothing to do here. Handled by lo op itself. */ - break; -#endif - case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break; - } -} - -/* -- Profiling ----------------------------------------------------------- */ - -static void asm_prof(ASMState *as, IRIns *ir) -{ - UNUSED(ir); - asm_guardcc(as, CC_NE); - emit_n(as, ARMI_TST|ARMI_K12|HOOK_PROFILE, RID_TMP); - emit_lsptr(as, ARMI_LDRB, RID_TMP, (void *)&J2G(as->J)->hookmask); -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - Reg pbase; - uint32_t k; - if (irp) { - if (!ra_hasspill(irp->s)) { - pbase = irp->r; - lj_assertA(ra_hasreg(pbase), "base reg lost"); - } else if (allow) { - pbase = rset_pickbot(allow); - } else { - pbase = RID_RET; - emit_lso(as, ARMI_LDR, RID_RET, RID_SP, 0); /* Restore temp. register. */ - } - } else { - pbase = RID_BASE; - } - emit_branch(as, ARMF_CC(ARMI_BL, CC_LS), exitstub_addr(as->J, exitno)); - k = emit_isk12(0, (int32_t)(8*topslot)); - lj_assertA(k, "slot offset %d does not fit in K12", 8*topslot); - emit_n(as, ARMI_CMP^k, RID_TMP); - emit_dnm(as, ARMI_SUB, RID_TMP, RID_TMP, pbase); - emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, - (int32_t)offsetof(lua_State, maxstack)); - if (irp) { /* Must not spill arbitrary registers in head of side trace. */ - int32_t i = i32ptr(&J2G(as->J)->cur_L); - if (ra_hasspill(irp->s)) - emit_lso(as, ARMI_LDR, pbase, RID_SP, sps_scale(irp->s)); - emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, (i & 4095)); - if (ra_hasspill(irp->s) && !allow) - emit_lso(as, ARMI_STR, RID_RET, RID_SP, 0); /* Save temp. register. */ - emit_loadi(as, RID_TMP, (i & ~4095)); - } else { - emit_getgl(as, RID_TMP, cur_L); - } -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { -#if LJ_SOFTFP - RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); - Reg tmp; - /* LJ_SOFTFP: must be a number constant. */ - lj_assertA(irref_isk(ref), "unsplit FP op"); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, - rset_exclude(RSET_GPREVEN, RID_BASE)); - emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs); - if (rset_test(as->freeset, tmp+1)) odd = RID2RSET(tmp+1); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, odd); - emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs+4); -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_vlso(as, ARMI_VSTR_D, src, RID_BASE, ofs); -#endif - } else { - RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); - Reg type; - lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t), - "restore of IR type %d", irt_type(ir->t)); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPREVEN, RID_BASE)); - emit_lso(as, ARMI_STR, src, RID_BASE, ofs); - if (rset_test(as->freeset, src+1)) odd = RID2RSET(src+1); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s == 0) continue; /* Do not overwrite link to previous frame. */ - type = ra_allock(as, (int32_t)(*flinks--), odd); -#if LJ_SOFTFP - } else if ((sn & SNAP_SOFTFPNUM)) { - type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPRODD, RID_BASE)); -#endif - } else if ((sn & SNAP_KEYINDEX)) { - type = ra_allock(as, (int32_t)LJ_KEYINDEX, odd); - } else { - type = ra_allock(as, (int32_t)irt_toitype(ir->t), odd); - } - emit_lso(as, ARMI_STR, type, RID_BASE, ofs+4); - } - checkmclim(as); - } - lj_assertA(map + nent == flinks, "inconsistent frames in snapshot"); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Marker to prevent patching the GC check exit. */ -#define ARM_NOPATCH_GC_CHECK (ARMI_BIC|ARMI_K12) - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp1, tmp2; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ - *--as->mcp = ARM_NOPATCH_GC_CHECK; - emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - tmp1 = ra_releasetmp(as, ASMREF_TMP1); - tmp2 = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp2, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_branch(as, ARMF_CC(ARMI_B, CC_LS), l_end); - emit_nm(as, ARMI_CMP, RID_TMP, tmp2); - emit_lso(as, ARMI_LDR, tmp2, tmp1, - (int32_t)offsetof(global_State, gc.threshold)); - emit_lso(as, ARMI_LDR, RID_TMP, tmp1, - (int32_t)offsetof(global_State, gc.total)); - ra_allockreg(as, i32ptr(J2G(as->J)), tmp1); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guardcc already inverted the bcc and patched the final bl. */ - p[-2] |= ((uint32_t)(target-p) & 0x00ffffffu); - } else { - p[-1] = ARMI_B | ((uint32_t)((target-p)-1) & 0x00ffffffu); - } -} - -/* Fixup the tail of the loop. */ -static void asm_loop_tail_fixup(ASMState *as) -{ - UNUSED(as); /* Nothing to do. */ -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Reload L register from g->cur_L. */ -static void asm_head_lreg(ASMState *as) -{ - IRIns *ir = IR(ASMREF_L); - if (ra_used(ir)) { - Reg r = ra_dest(as, ir, RSET_GPR); - emit_getgl(as, r, cur_L); - ra_evictk(as); - } -} - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir; - asm_head_lreg(as); - ir = IR(REF_BASE); - if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t))) - ra_spill(as, ir); - ra_destreg(as, ir, RID_BASE); -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir; - asm_head_lreg(as); - ir = IR(REF_BASE); - if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t))) - ra_spill(as, ir); - if (ra_hasspill(irp->s)) { - rset_clear(allow, ra_dest(as, ir, allow)); - } else { - Reg r = irp->r; - lj_assertA(ra_hasreg(r), "base reg lost"); - rset_clear(allow, r); - if (r != ir->r && !rset_test(as->freeset, r)) - ra_restore(as, regcost_ref(as->cost[r])); - ra_destreg(as, ir, r); - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *p = as->mctop; - MCode *target; - int32_t spadj = as->T->spadjust; - if (spadj == 0) { - as->mctop = --p; - } else { - /* Patch stack adjustment. */ - uint32_t k = emit_isk12(ARMI_ADD, spadj); - lj_assertA(k, "stack adjustment %d does not fit in K12", spadj); - p[-2] = (ARMI_ADD^k) | ARMF_D(RID_SP) | ARMF_N(RID_SP); - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - p[-1] = ARMI_B|(((target-p)-1)&0x00ffffffu); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop - 1; /* Leave room for exit branch. */ - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - as->mcp = p-1; /* Leave room for stack pointer adjustment. */ - as->invmcp = NULL; - } - *p = 0; /* Prevent load/store merging. */ -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = CCI_XNARGS(ci); - int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR, fprodd = 0; - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) { - if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t)) { - if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) { - if (irt_isnum(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; - else fprodd = 0, nslots = (nslots + 3) & ~1; - } else { - if (fprodd) fprodd--; - else if (nfpr > 0) fprodd = 1, nfpr--; - else nslots++; - } - } else if (irt_isnum(IR(args[i])->t)) { - ngpr &= ~1; - if (ngpr > 0) ngpr -= 2; else nslots += 2; - } else { - if (ngpr > 0) ngpr--; else nslots++; - } - } else { - if (ngpr > 0) ngpr--; else nslots++; - } - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - /* May need extra exit for asm_stack_check on side traces. */ - asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *cstart = NULL, *cend = p; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MCode *px = exitstub_addr(J, exitno) - 2; - for (; p < pe; p++) { - /* Look for bl_cc exitstub, replace with b_cc target. */ - uint32_t ins = *p; - if ((ins & 0x0f000000u) == 0x0b000000u && ins < 0xf0000000u && - ((ins ^ (px-p)) & 0x00ffffffu) == 0 && - p[-1] != ARM_NOPATCH_GC_CHECK) { - *p = (ins & 0xfe000000u) | (((target-p)-2) & 0x00ffffffu); - cend = p+1; - if (!cstart) cstart = p; - } - } - lj_assertJ(cstart != NULL, "exit stub %d not found", exitno); - lj_mcode_sync(cstart, cend); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm64.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm64.h deleted file mode 100644 index 805ea54..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_arm64.h +++ /dev/null @@ -1,2071 +0,0 @@ -/* -** ARM64 IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Contributed by Djordje Kovacevic and Stefan Pejic from RT-RK.com. -** Sponsored by Cisco Systems, Inc. -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_allocref(as, ir->op2, allow); - left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_allocref(as, ir->op1, allow); - right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} - -/* -- Guard handling ------------------------------------------------------ */ - -/* Setup all needed exit stubs. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - MCode *mxp = as->mctop; - if (mxp - (nexits + 3 + MCLIM_REDZONE) < as->mclim) - asm_mclimit(as); - /* 1: str lr,[sp]; bl ->vm_exit_handler; movz w0,traceno; bl <1; bl <1; ... */ - for (i = nexits-1; (int32_t)i >= 0; i--) - *--mxp = A64I_LE(A64I_BL | A64F_S26(-3-i)); - *--mxp = A64I_LE(A64I_MOVZw | A64F_U16(as->T->traceno)); - mxp--; - *mxp = A64I_LE(A64I_BL | A64F_S26(((MCode *)(void *)lj_vm_exit_handler-mxp))); - *--mxp = A64I_LE(A64I_STRx | A64F_D(RID_LR) | A64F_N(RID_SP)); - as->mctop = mxp; -} - -static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno) -{ - /* Keep this in-sync with exitstub_trace_addr(). */ - return as->mctop + exitno + 3; -} - -/* Emit conditional branch to exit for guard. */ -static void asm_guardcc(ASMState *as, A64CC cc) -{ - MCode *target = asm_exitstub_addr(as, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = A64I_B | A64F_S26(target-p); - emit_cond_branch(as, cc^1, p-1); - return; - } - emit_cond_branch(as, cc, target); -} - -/* Emit test and branch instruction to exit for guard. */ -static void asm_guardtnb(ASMState *as, A64Ins ai, Reg r, uint32_t bit) -{ - MCode *target = asm_exitstub_addr(as, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = A64I_B | A64F_S26(target-p); - emit_tnb(as, ai^0x01000000u, r, bit, p-1); - return; - } - emit_tnb(as, ai, r, bit, target); -} - -/* Emit compare and branch instruction to exit for guard. */ -static void asm_guardcnb(ASMState *as, A64Ins ai, Reg r) -{ - MCode *target = asm_exitstub_addr(as, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = A64I_B | A64F_S26(target-p); - emit_cnb(as, ai^0x01000000u, r, p-1); - return; - } - emit_cnb(as, ai, r, target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -static int asm_isk32(ASMState *as, IRRef ref, int32_t *k) -{ - if (irref_isk(ref)) { - IRIns *ir = IR(ref); - if (ir->o == IR_KNULL || !irt_is64(ir->t)) { - *k = ir->i; - return 1; - } else if (checki32((int64_t)ir_k64(ir)->u64)) { - *k = (int32_t)ir_k64(ir)->u64; - return 1; - } - } - return 0; -} - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -#define FUSE_REG 0x40000000 - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow, - A64Ins ins) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (emit_checkofs(ins, ofs)) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } else { - Reg base = ra_alloc1(as, ir->op1, allow); - *ofsp = FUSE_REG|ra_alloc1(as, ir->op2, rset_exclude(allow, base)); - return base; - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (emit_checkofs(ins, ofs)) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv; - int64_t ofs = glofs(as, &uv->tv); - if (emit_checkofs(ins, ofs)) { - *ofsp = (int32_t)ofs; - return RID_GL; - } - } - } else if (ir->o == IR_TMPREF) { - *ofsp = (int32_t)glofs(as, &J2G(as->J)->tmptv); - return RID_GL; - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse m operand into arithmetic/logic instructions. */ -static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_hasreg(ir->r)) { - ra_noweak(as, ir->r); - return A64F_M(ir->r); - } else if (irref_isk(ref)) { - uint32_t m; - int64_t k = get_k64val(as, ref); - if ((ai & 0x1f000000) == 0x0a000000) - m = emit_isk13(k, irt_is64(ir->t)); - else - m = emit_isk12(k); - if (m) - return m; - } else if (mayfuse(as, ref)) { - if ((ir->o >= IR_BSHL && ir->o <= IR_BSAR && irref_isk(ir->op2)) || - (ir->o == IR_ADD && ir->op1 == ir->op2)) { - A64Shift sh = ir->o == IR_BSHR ? A64SH_LSR : - ir->o == IR_BSAR ? A64SH_ASR : A64SH_LSL; - int shift = ir->o == IR_ADD ? 1 : - (IR(ir->op2)->i & (irt_is64(ir->t) ? 63 : 31)); - IRIns *irl = IR(ir->op1); - if (sh == A64SH_LSL && - irl->o == IR_CONV && - irl->op2 == ((IRT_I64<op1, allow); - return A64F_M(m) | A64F_EXSH(A64EX_SXTW, shift); - } else { - Reg m = ra_alloc1(as, ir->op1, allow); - return A64F_M(m) | A64F_SH(sh, shift); - } - } else if (ir->o == IR_CONV && - ir->op2 == ((IRT_I64<op1, allow); - return A64F_M(m) | A64F_EX(A64EX_SXTW); - } - } - return A64F_M(ra_allocref(as, ref, allow)); -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, A64Ins ai, Reg rd, IRRef ref, - RegSet allow) -{ - IRIns *ir = IR(ref); - Reg base; - int32_t ofs = 0; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - if (ir->o == IR_ADD) { - if (asm_isk32(as, ir->op2, &ofs) && emit_checkofs(ai, ofs)) { - ref = ir->op1; - } else { - Reg rn, rm; - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irl = IR(lref); - if (mayfuse(as, irl->op1)) { - unsigned int shift = 4; - if (irl->o == IR_BSHL && irref_isk(irl->op2)) { - shift = (IR(irl->op2)->i & 63); - } else if (irl->o == IR_ADD && irl->op1 == irl->op2) { - shift = 1; - } - if ((ai >> 30) == shift) { - lref = irl->op1; - irl = IR(lref); - ai |= A64I_LS_SH; - } - } - if (irl->o == IR_CONV && - irl->op2 == ((IRT_I64<op1; - ai |= A64I_LS_SXTWx; - } else { - ai |= A64I_LS_LSLx; - } - rm = ra_alloc1(as, lref, allow); - rn = ra_alloc1(as, rref, rset_exclude(allow, rm)); - emit_dnm(as, (ai^A64I_LS_R), (rd & 31), rn, rm); - return; - } - } else if (ir->o == IR_STRREF) { - if (asm_isk32(as, ir->op2, &ofs)) { - ref = ir->op1; - } else if (asm_isk32(as, ir->op1, &ofs)) { - ref = ir->op2; - } else { - Reg refk = irref_isk(ir->op1) ? ir->op1 : ir->op2; - Reg refv = irref_isk(ir->op1) ? ir->op2 : ir->op1; - Reg rn = ra_alloc1(as, refv, allow); - IRIns *irr = IR(refk); - uint32_t m; - if (irr+1 == ir && !ra_used(irr) && - irr->o == IR_ADD && irref_isk(irr->op2)) { - ofs = sizeof(GCstr) + IR(irr->op2)->i; - if (emit_checkofs(ai, ofs)) { - Reg rm = ra_alloc1(as, irr->op1, rset_exclude(allow, rn)); - m = A64F_M(rm) | A64F_EX(A64EX_SXTW); - goto skipopm; - } - } - m = asm_fuseopm(as, 0, refk, rset_exclude(allow, rn)); - ofs = sizeof(GCstr); - skipopm: - emit_lso(as, ai, rd, rd, ofs); - emit_dn(as, A64I_ADDx^m, rd, rn); - return; - } - ofs += sizeof(GCstr); - if (!emit_checkofs(ai, ofs)) { - Reg rn = ra_alloc1(as, ref, allow); - Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn)); - emit_dnm(as, (ai^A64I_LS_R)|A64I_LS_UXTWx, rd, rn, rm); - return; - } - } - } - base = ra_alloc1(as, ref, allow); - emit_lso(as, ai, (rd & 31), base, ofs); -} - -/* Fuse FP multiply-add/sub. */ -static int asm_fusemadd(ASMState *as, IRIns *ir, A64Ins ai, A64Ins air) -{ - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irm; - if ((as->flags & JIT_F_OPT_FMA) && - lref != rref && - ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && - ra_noreg(irm->r)) || - (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && - (rref = lref, ai = air, ra_noreg(irm->r))))) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg add = ra_hintalloc(as, rref, dest, RSET_FPR); - Reg left = ra_alloc2(as, irm, - rset_exclude(rset_exclude(RSET_FPR, dest), add)); - Reg right = (left >> 8); left &= 255; - emit_dnma(as, ai, (dest & 31), (left & 31), (right & 31), (add & 31)); - return 1; - } - return 0; -} - -/* Fuse BAND + BSHL/BSHR into UBFM. */ -static int asm_fuseandshift(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1); - lj_assertA(ir->o == IR_BAND, "bad usage"); - if (canfuse(as, irl) && irref_isk(ir->op2)) { - uint64_t mask = get_k64val(as, ir->op2); - if (irref_isk(irl->op2) && (irl->o == IR_BSHR || irl->o == IR_BSHL)) { - int32_t shmask = irt_is64(irl->t) ? 63 : 31; - int32_t shift = (IR(irl->op2)->i & shmask); - int32_t imms = shift; - if (irl->o == IR_BSHL) { - mask >>= shift; - shift = (shmask-shift+1) & shmask; - imms = 0; - } - if (mask && !((mask+1) & mask)) { /* Contiguous 1-bits at the bottom. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, irl->op1, RSET_GPR); - A64Ins ai = shmask == 63 ? A64I_UBFMx : A64I_UBFMw; - imms += 63 - emit_clz64(mask); - if (imms > shmask) imms = shmask; - emit_dn(as, ai | A64F_IMMS(imms) | A64F_IMMR(shift), dest, left); - return 1; - } - } - } - return 0; -} - -/* Fuse BOR(BSHL, BSHR) into EXTR/ROR. */ -static int asm_fuseorshift(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - lj_assertA(ir->o == IR_BOR, "bad usage"); - if (canfuse(as, irl) && canfuse(as, irr) && - ((irl->o == IR_BSHR && irr->o == IR_BSHL) || - (irl->o == IR_BSHL && irr->o == IR_BSHR))) { - if (irref_isk(irl->op2) && irref_isk(irr->op2)) { - IRRef lref = irl->op1, rref = irr->op1; - uint32_t lshift = IR(irl->op2)->i, rshift = IR(irr->op2)->i; - if (irl->o == IR_BSHR) { /* BSHR needs to be the right operand. */ - uint32_t tmp2; - IRRef tmp1 = lref; lref = rref; rref = tmp1; - tmp2 = lshift; lshift = rshift; rshift = tmp2; - } - if (rshift + lshift == (irt_is64(ir->t) ? 64 : 32)) { - A64Ins ai = irt_is64(ir->t) ? A64I_EXTRx : A64I_EXTRw; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_GPR); - Reg right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left)); - emit_dnm(as, ai | A64F_IMMS(rshift), dest, left, right); - return 1; - } - } - } - return 0; -} - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_XNARGS(ci); - int32_t ofs = 0; - Reg gpr, fpr = REGARG_FIRSTFPR; - if ((void *)ci->func) - emit_call(as, (void *)ci->func); - for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) - as->cost[gpr] = REGCOST(~0u, ASMREF_L); - gpr = REGARG_FIRSTGPR; - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - IRIns *ir = IR(ref); - if (ref) { - if (irt_isfp(ir->t)) { - if (fpr <= REGARG_LASTFPR) { - lj_assertA(rset_test(as->freeset, fpr), - "reg %d not free", fpr); /* Must have been evicted. */ - ra_leftov(as, fpr, ref); - fpr++; - } else { - Reg r = ra_alloc1(as, ref, RSET_FPR); - emit_spstore(as, ir, r, ofs + ((LJ_BE && !irt_isnum(ir->t)) ? 4 : 0)); - ofs += 8; - } - } else { - if (gpr <= REGARG_LASTGPR) { - lj_assertA(rset_test(as->freeset, gpr), - "reg %d not free", gpr); /* Must have been evicted. */ - ra_leftov(as, gpr, ref); - gpr++; - } else { - Reg r = ra_alloc1(as, ref, RSET_GPR); - emit_spstore(as, ir, r, ofs + ((LJ_BE && !irt_is64(ir->t)) ? 4 : 0)); - ofs += 8; - } - } - } - } -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)); - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lj_assertA(!irt_ispri(ir->t), "PRI dest"); - if (irt_isfp(ir->t)) { - if (ci->flags & CCI_CASTU64) { - Reg dest = ra_dest(as, ir, RSET_FPR) & 31; - emit_dn(as, irt_isnum(ir->t) ? A64I_FMOV_D_R : A64I_FMOV_S_R, - dest, RID_RET); - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } - UNUSED(ci); -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(ir_k64(irf)->u64); - } else { /* Need a non-argument register for indirect calls. */ - Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_X8, RID_MAX_GPR)-RSET_FIXED); - emit_n(as, A64I_BLR, freg); - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */ - /* Need to force a spill on REF_BASE now to update the stack slot. */ - emit_lso(as, A64I_STRx, base, RID_SP, ra_spill(as, IR(REF_BASE))); - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); - emit_nm(as, A64I_CMPx, RID_TMP, - ra_allock(as, i64ptr(pc), rset_exclude(RSET_GPR, base))); - emit_lso(as, A64I_LDRx, RID_TMP, base, -8); -} - -/* -- Buffer operations --------------------------------------------------- */ - -#if LJ_HASBUFFER -static void asm_bufhdr_write(ASMState *as, Reg sb) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); - IRIns irgc; - irgc.ot = IRT(0, IRT_PGC); /* GC type. */ - emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L)); - emit_dn(as, A64I_BFMx | A64F_IMMS(lj_fls(SBUF_MASK_FLAG)) | A64F_IMMR(0), RID_TMP, tmp); - emit_getgl(as, RID_TMP, cur_L); - emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L)); -} -#endif - -/* -- Type conversions ---------------------------------------------------- */ - -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_guardcc(as, CC_NE); - emit_nm(as, A64I_FCMPd, (tmp & 31), (left & 31)); - emit_dn(as, A64I_FCVT_F64_S32, (tmp & 31), dest); - emit_dn(as, A64I_FCVT_S32_F64, dest, (left & 31)); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_dn(as, A64I_FMOV_R_S, dest, (tmp & 31)); - emit_dnm(as, A64I_FADDd, (tmp & 31), (left & 31), (right & 31)); -} - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - int st64 = (st == IRT_I64 || st == IRT_U64 || st == IRT_P64); - int stfp = (st == IRT_NUM || st == IRT_FLOAT); - IRRef lref = ir->op1; - lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV"); - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - emit_dn(as, st == IRT_NUM ? A64I_FCVT_F32_F64 : A64I_FCVT_F64_F32, - (dest & 31), (ra_alloc1(as, lref, RSET_FPR) & 31)); - } else { /* Integer to FP conversion. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - A64Ins ai = irt_isfloat(ir->t) ? - (((IRT_IS64 >> st) & 1) ? - (st == IRT_I64 ? A64I_FCVT_F32_S64 : A64I_FCVT_F32_U64) : - (st == IRT_INT ? A64I_FCVT_F32_S32 : A64I_FCVT_F32_U32)) : - (((IRT_IS64 >> st) & 1) ? - (st == IRT_I64 ? A64I_FCVT_F64_S64 : A64I_FCVT_F64_U64) : - (st == IRT_INT ? A64I_FCVT_F64_S32 : A64I_FCVT_F64_U32)); - emit_dn(as, ai, (dest & 31), left); - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lj_assertA(irt_isint(ir->t) && st == IRT_NUM, - "bad type for checked CONV"); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg dest = ra_dest(as, ir, RSET_GPR); - A64Ins ai = irt_is64(ir->t) ? - (st == IRT_NUM ? - (irt_isi64(ir->t) ? A64I_FCVT_S64_F64 : A64I_FCVT_U64_F64) : - (irt_isi64(ir->t) ? A64I_FCVT_S64_F32 : A64I_FCVT_U64_F32)) : - (st == IRT_NUM ? - (irt_isint(ir->t) ? A64I_FCVT_S32_F64 : A64I_FCVT_U32_F64) : - (irt_isint(ir->t) ? A64I_FCVT_S32_F32 : A64I_FCVT_U32_F32)); - emit_dn(as, ai, dest, (left & 31)); - } - } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_GPR); - A64Ins ai = st == IRT_I8 ? A64I_SXTBw : - st == IRT_U8 ? A64I_UXTBw : - st == IRT_I16 ? A64I_SXTHw : A64I_UXTHw; - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT"); - emit_dn(as, ai, dest, left); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irt_is64(ir->t)) { - if (st64 || !(ir->op2 & IRCONV_SEXT)) { - /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else { /* 32 to 64 bit sign extension. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - emit_dn(as, A64I_SXTW, dest, left); - } - } else { - if (st64 && !(ir->op2 & IRCONV_NONE)) { - /* This is either a 32 bit reg/reg mov which zeroes the hiword - ** or a load of the loword from a 64 bit address. - */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - emit_dm(as, A64I_MOVw, dest, left); - } else { /* 32/32 bit no-op (cast). */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } - } -} - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - Reg dest = 0, tmp; - int destused = ra_used(ir); - int32_t ofs = 0; - ra_evictset(as, RSET_SCRATCH); - if (destused) { - if (ra_hasspill(ir->s)) { - ofs = sps_scale(ir->s); - destused = 0; - if (ra_hasreg(ir->r)) { - ra_free(as, ir->r); - ra_modified(as, ir->r); - emit_spload(as, ir, ir->r, ofs); - } - } else { - dest = ra_dest(as, ir, RSET_FPR); - } - } - if (destused) - emit_lso(as, A64I_LDRd, (dest & 31), RID_SP, 0); - asm_guardcnb(as, A64I_CBZ, RID_RET); - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - tmp = ra_releasetmp(as, ASMREF_TMP1); - emit_opk(as, A64I_ADDx, tmp, RID_SP, ofs, RSET_GPR); -} - -/* -- Memory references --------------------------------------------------- */ - -/* Store tagged value for ref at base+ofs. */ -static void asm_tvstore64(ASMState *as, Reg base, int32_t ofs, IRRef ref) -{ - RegSet allow = rset_exclude(RSET_GPR, base); - IRIns *ir = IR(ref); - lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t), - "store of IR type %d", irt_type(ir->t)); - if (irref_isk(ref)) { - TValue k; - lj_ir_kvalue(as->J->L, &k, ir); - emit_lso(as, A64I_STRx, ra_allock(as, k.u64, allow), base, ofs); - } else { - Reg src = ra_alloc1(as, ref, allow); - rset_clear(allow, src); - if (irt_isinteger(ir->t)) { - Reg type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow); - emit_lso(as, A64I_STRx, RID_TMP, base, ofs); - emit_dnm(as, A64I_ADDx | A64F_EX(A64EX_UXTW), RID_TMP, type, src); - } else { - Reg type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - emit_lso(as, A64I_STRx, RID_TMP, base, ofs); - emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 47), RID_TMP, src, type); - } - } -} - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode) -{ - if ((mode & IRTMPREF_IN1)) { - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if (irref_isk(ref) && !(mode & IRTMPREF_OUT1)) { - /* Use the number constant itself as a TValue. */ - ra_allockreg(as, i64ptr(ir_knum(ir)), dest); - return; - } - emit_lso(as, A64I_STRd, (ra_alloc1(as, ref, RSET_FPR) & 31), dest, 0); - } else { - asm_tvstore64(as, dest, 0, ref); - } - } - /* g->tmptv holds the TValue(s). */ - emit_dn(as, A64I_ADDx^emit_isk12(glofs(as, &J2G(as->J)->tmptv)), dest, RID_GL); -} - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - uint32_t k = emit_isk12(ofs + 8*IR(ir->op2)->i); - if (k) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_dn(as, A64I_ADDx^k, dest, base); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - emit_dnm(as, A64I_ADDx | A64F_EXSH(A64EX_UXTW, 3), dest, base, idx); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = 0, tmp = RID_TMP; - Reg ftmp = RID_NONE, type = RID_NONE, scr = RID_NONE, tisnum = RID_NONE; - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - int isk = irref_isk(ir->op2); - IRType1 kt = irkey->t; - uint32_t k = 0; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - rset_clear(allow, tab); - - if (!isk) { - key = ra_alloc1(as, ir->op2, irt_isnum(kt) ? RSET_FPR : allow); - rset_clear(allow, key); - if (!irt_isstr(kt)) { - tmp = ra_scratch(as, allow); - rset_clear(allow, tmp); - } - } else if (irt_isnum(kt)) { - int64_t val = (int64_t)ir_knum(irkey)->u64; - if (!(k = emit_isk12(val))) { - key = ra_allock(as, val, allow); - rset_clear(allow, key); - } - } else if (!irt_ispri(kt)) { - if (!(k = emit_isk12(irkey->i))) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } - } - - /* Allocate constants early. */ - if (irt_isnum(kt)) { - if (!isk) { - tisnum = ra_allock(as, LJ_TISNUM << 15, allow); - ftmp = ra_scratch(as, rset_exclude(RSET_FPR, key)); - rset_clear(allow, tisnum); - } - } else if (irt_isaddr(kt)) { - if (isk) { - int64_t kk = ((int64_t)irt_toitype(kt) << 47) | irkey[1].tv.u64; - scr = ra_allock(as, kk, allow); - } else { - scr = ra_scratch(as, allow); - } - rset_clear(allow, scr); - } else { - lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type"); - type = ra_allock(as, ~((int64_t)~irt_toitype(kt) << 47), allow); - scr = ra_scratch(as, rset_clear(allow, type)); - rset_clear(allow, scr); - } - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - as->invmcp = NULL; - if (merge == IR_NE) - asm_guardcc(as, CC_AL); - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - - /* Follow hash chain until the end. */ - l_loop = --as->mcp; - emit_n(as, A64I_CMPx^A64I_K12^0, dest); - emit_lso(as, A64I_LDRx, dest, dest, offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (merge == IR_EQ) - asm_guardcc(as, CC_EQ); - else - emit_cond_branch(as, CC_EQ, l_end); - - if (irt_isnum(kt)) { - if (isk) { - /* Assumes -0.0 is already canonicalized to +0.0. */ - if (k) - emit_n(as, A64I_CMPx^k, tmp); - else - emit_nm(as, A64I_CMPx, key, tmp); - emit_lso(as, A64I_LDRx, tmp, dest, offsetof(Node, key.u64)); - } else { - emit_nm(as, A64I_FCMPd, key, ftmp); - emit_dn(as, A64I_FMOV_D_R, (ftmp & 31), (tmp & 31)); - emit_cond_branch(as, CC_LO, l_next); - emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32), tisnum, tmp); - emit_lso(as, A64I_LDRx, tmp, dest, offsetof(Node, key.n)); - } - } else if (irt_isaddr(kt)) { - if (isk) { - emit_nm(as, A64I_CMPx, scr, tmp); - emit_lso(as, A64I_LDRx, tmp, dest, offsetof(Node, key.u64)); - } else { - emit_nm(as, A64I_CMPx, tmp, scr); - emit_lso(as, A64I_LDRx, scr, dest, offsetof(Node, key.u64)); - } - } else { - emit_nm(as, A64I_CMPx, scr, type); - emit_lso(as, A64I_LDRx, scr, dest, offsetof(Node, key)); - } - - *l_loop = A64I_BCC | A64F_S19(as->mcp - l_loop) | CC_NE; - if (!isk && irt_isaddr(kt)) { - type = ra_allock(as, (int32_t)irt_toitype(kt), allow); - emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 47), tmp, key, type); - rset_clear(allow, type); - } - /* Load main position relative to tab->node into dest. */ - khash = isk ? ir_khash(as, irkey) : 1; - if (khash == 0) { - emit_lso(as, A64I_LDRx, dest, tab, offsetof(GCtab, node)); - } else { - emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 3), dest, tmp, dest); - emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 1), dest, dest, dest); - emit_lso(as, A64I_LDRx, tmp, tab, offsetof(GCtab, node)); - if (isk) { - Reg tmphash = ra_allock(as, khash, allow); - emit_dnm(as, A64I_ANDw, dest, dest, tmphash); - emit_lso(as, A64I_LDRw, dest, tab, offsetof(GCtab, hmask)); - } else if (irt_isstr(kt)) { - /* Fetch of str->sid is cheaper than ra_allock. */ - emit_dnm(as, A64I_ANDw, dest, dest, tmp); - emit_lso(as, A64I_LDRw, tmp, key, offsetof(GCstr, sid)); - emit_lso(as, A64I_LDRw, dest, tab, offsetof(GCtab, hmask)); - } else { /* Must match with hash*() in lj_tab.c. */ - emit_dnm(as, A64I_ANDw, dest, dest, tmp); - emit_lso(as, A64I_LDRw, tmp, tab, offsetof(GCtab, hmask)); - emit_dnm(as, A64I_SUBw, dest, dest, tmp); - emit_dnm(as, A64I_EXTRw | (A64F_IMMS(32-HASH_ROT3)), tmp, tmp, tmp); - emit_dnm(as, A64I_EORw, dest, dest, tmp); - emit_dnm(as, A64I_EXTRw | (A64F_IMMS(32-HASH_ROT2)), dest, dest, dest); - emit_dnm(as, A64I_SUBw, tmp, tmp, dest); - emit_dnm(as, A64I_EXTRw | (A64F_IMMS(32-HASH_ROT1)), dest, dest, dest); - emit_dnm(as, A64I_EORw, tmp, tmp, dest); - if (irt_isnum(kt)) { - emit_dnm(as, A64I_ADDw, dest, dest, dest); - emit_dn(as, A64I_LSRx | A64F_IMMR(32)|A64F_IMMS(32), dest, dest); - emit_dm(as, A64I_MOVw, tmp, dest); - emit_dn(as, A64I_FMOV_R_D, dest, (key & 31)); - } else { - checkmclim(as); - emit_dm(as, A64I_MOVw, tmp, key); - emit_dnm(as, A64I_EORw, dest, dest, - ra_allock(as, irt_toitype(kt) << 15, allow)); - emit_dn(as, A64I_LSRx | A64F_IMMR(32)|A64F_IMMS(32), dest, dest); - emit_dm(as, A64I_MOVx, dest, key); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - int bigofs = !emit_checkofs(A64I_LDRx, ofs); - Reg dest = (ra_used(ir) || bigofs) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - Reg key, idx = node; - RegSet allow = rset_exclude(RSET_GPR, node); - uint64_t k; - lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot"); - if (bigofs) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_opk(as, A64I_ADDx, dest, node, ofs, allow); - } - asm_guardcc(as, CC_NE); - if (irt_ispri(irkey->t)) { - k = ~((int64_t)~irt_toitype(irkey->t) << 47); - } else if (irt_isnum(irkey->t)) { - k = ir_knum(irkey)->u64; - } else { - k = ((uint64_t)irt_toitype(irkey->t) << 47) | (uint64_t)ir_kgc(irkey); - } - key = ra_scratch(as, allow); - emit_nm(as, A64I_CMPx, key, ra_allock(as, k, rset_exclude(allow, key))); - emit_lso(as, A64I_LDRx, key, idx, kofs); - if (bigofs) - emit_opk(as, A64I_ADDx, dest, node, ofs, RSET_GPR); -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, A64I_LDRx, dest, v); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guardcc(as, CC_NE); - emit_n(as, (A64I_CMPx^A64I_K12) | A64F_U12(1), RID_TMP); - emit_opk(as, A64I_ADDx, dest, uv, - (int32_t)offsetof(GCupval, tv), RSET_GPR); - emit_lso(as, A64I_LDRB, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_lso(as, A64I_LDRx, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_lso(as, A64I_LDRx, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 8*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lj_assertA(!ra_used(ir), "unfused FREF"); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - Reg base = ra_alloc1(as, ir->op1, allow); - IRIns *irr = IR(ir->op2); - int32_t ofs = sizeof(GCstr); - uint32_t m; - rset_clear(allow, base); - if (irref_isk(ir->op2) && (m = emit_isk12(ofs + irr->i))) { - emit_dn(as, A64I_ADDx^m, dest, base); - } else { - emit_dn(as, (A64I_ADDx^A64I_K12) | A64F_U12(ofs), dest, dest); - emit_dnm(as, A64I_ADDx, dest, base, ra_alloc1(as, ir->op2, allow)); - } -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static A64Ins asm_fxloadins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: return A64I_LDRB ^ A64I_LS_S; - case IRT_U8: return A64I_LDRB; - case IRT_I16: return A64I_LDRH ^ A64I_LS_S; - case IRT_U16: return A64I_LDRH; - case IRT_NUM: return A64I_LDRd; - case IRT_FLOAT: return A64I_LDRs; - default: return irt_is64(ir->t) ? A64I_LDRx : A64I_LDRw; - } -} - -static A64Ins asm_fxstoreins(IRIns *ir) -{ - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return A64I_STRB; - case IRT_I16: case IRT_U16: return A64I_STRH; - case IRT_NUM: return A64I_STRd; - case IRT_FLOAT: return A64I_STRs; - default: return irt_is64(ir->t) ? A64I_STRx : A64I_STRw; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx; - A64Ins ai = asm_fxloadins(ir); - int32_t ofs; - if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */ - idx = RID_GL; - ofs = (ir->op2 << 2) - GG_OFS(g); - } else { - idx = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_dn(as, (A64I_ADDx^A64I_K12) | A64F_U12(ofs), dest, idx); - return; - } - } - ofs = field_ofs[ir->op2]; - } - emit_lso(as, ai, (dest & 31), idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - emit_lso(as, asm_fxstoreins(ir), (src & 31), idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - lj_assertA(!(ir->op2 & IRXLOAD_UNALIGNED), "unaligned XLOAD"); - asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR); -} - -static void asm_xstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, - rset_exclude(RSET_GPR, src)); - } -} - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - Reg idx, tmp, type; - int32_t ofs = 0; - RegSet gpr = RSET_GPR, allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR; - lj_assertA(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) || - irt_isint(ir->t), - "bad load type %d", irt_type(ir->t)); - if (ra_used(ir)) { - Reg dest = ra_dest(as, ir, allow); - tmp = irt_isnum(ir->t) ? ra_scratch(as, rset_clear(gpr, dest)) : dest; - if (irt_isaddr(ir->t)) { - emit_dn(as, A64I_ANDx^emit_isk13(LJ_GCVMASK, 1), dest, dest); - } else if (irt_isnum(ir->t)) { - emit_dn(as, A64I_FMOV_D_R, (dest & 31), tmp); - } else if (irt_isint(ir->t)) { - emit_dm(as, A64I_MOVw, dest, dest); - } - } else { - tmp = ra_scratch(as, gpr); - } - type = ra_scratch(as, rset_clear(gpr, tmp)); - idx = asm_fuseahuref(as, ir->op1, &ofs, rset_clear(gpr, type), A64I_LDRx); - if (ir->o == IR_VLOAD) ofs += 8 * ir->op2; - /* Always do the type check, even if the load result is unused. */ - asm_guardcc(as, irt_isnum(ir->t) ? CC_LS : CC_NE); - if (irt_type(ir->t) >= IRT_NUM) { - lj_assertA(irt_isinteger(ir->t) || irt_isnum(ir->t), - "bad load type %d", irt_type(ir->t)); - emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32), - ra_allock(as, LJ_TISNUM << 15, rset_exclude(gpr, idx)), tmp); - } else if (irt_isaddr(ir->t)) { - emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(-irt_toitype(ir->t)), type); - emit_dn(as, A64I_ASRx | A64F_IMMR(47), type, tmp); - } else if (irt_isnil(ir->t)) { - emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(1), tmp); - } else { - emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32), - ra_allock(as, (irt_toitype(ir->t) << 15) | 0x7fff, gpr), tmp); - } - if (ofs & FUSE_REG) - emit_dnm(as, (A64I_LDRx^A64I_LS_R)|A64I_LS_UXTWx|A64I_LS_SH, tmp, idx, (ofs & 31)); - else - emit_lso(as, A64I_LDRx, tmp, idx, ofs); -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, tmp = RID_TMP, type = RID_NONE; - int32_t ofs = 0; - if (irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, RSET_FPR); - idx = asm_fuseahuref(as, ir->op1, &ofs, allow, A64I_STRd); - if (ofs & FUSE_REG) - emit_dnm(as, (A64I_STRd^A64I_LS_R)|A64I_LS_UXTWx|A64I_LS_SH, (src & 31), idx, (ofs &31)); - else - emit_lso(as, A64I_STRd, (src & 31), idx, ofs); - } else { - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - if (irt_isinteger(ir->t)) - type = ra_allock(as, (uint64_t)(int32_t)LJ_TISNUM << 47, allow); - else - type = ra_allock(as, irt_toitype(ir->t), allow); - } else { - tmp = type = ra_allock(as, ~((int64_t)~irt_toitype(ir->t)<<47), allow); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type), - A64I_STRx); - if (ofs & FUSE_REG) - emit_dnm(as, (A64I_STRx^A64I_LS_R)|A64I_LS_UXTWx|A64I_LS_SH, tmp, idx, (ofs & 31)); - else - emit_lso(as, A64I_STRx, tmp, idx, ofs); - if (ra_hasreg(src)) { - if (irt_isinteger(ir->t)) { - emit_dnm(as, A64I_ADDx | A64F_EX(A64EX_UXTW), tmp, type, src); - } else { - emit_dnm(as, A64I_ADDx | A64F_SH(A64SH_LSL, 47), tmp, src, type); - } - } - } - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-2); - IRType1 t = ir->t; - Reg dest = RID_NONE, base; - RegSet allow = RSET_GPR; - lj_assertA(!(ir->op2 & IRSLOAD_PARENT), - "bad parent SLOAD"); /* Handled by asm_head_side(). */ - lj_assertA(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK), - "inconsistent SLOAD variant"); - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - dest = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, dest); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ - } else if (ra_used(ir)) { - Reg tmp = RID_NONE; - if ((ir->op2 & IRSLOAD_CONVERT)) - tmp = ra_scratch(as, irt_isint(t) ? RSET_FPR : RSET_GPR); - lj_assertA((irt_isnum(t)) || irt_isint(t) || irt_isaddr(t), - "bad SLOAD type %d", irt_type(t)); - dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : allow); - base = ra_alloc1(as, REF_BASE, rset_clear(allow, dest)); - if (irt_isaddr(t)) { - emit_dn(as, A64I_ANDx^emit_isk13(LJ_GCVMASK, 1), dest, dest); - } else if ((ir->op2 & IRSLOAD_CONVERT)) { - if (irt_isint(t)) { - emit_dn(as, A64I_FCVT_S32_F64, dest, (tmp & 31)); - /* If value is already loaded for type check, move it to FPR. */ - if ((ir->op2 & IRSLOAD_TYPECHECK)) - emit_dn(as, A64I_FMOV_D_R, (tmp & 31), dest); - else - dest = tmp; - t.irt = IRT_NUM; /* Check for original type. */ - } else { - emit_dn(as, A64I_FCVT_F64_S32, (dest & 31), tmp); - dest = tmp; - t.irt = IRT_INT; /* Check for original type. */ - } - } else if (irt_isint(t) && (ir->op2 & IRSLOAD_TYPECHECK)) { - emit_dm(as, A64I_MOVw, dest, dest); - } - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); -dotypecheck: - rset_clear(allow, base); - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - Reg tmp; - if (ra_hasreg(dest) && rset_test(RSET_GPR, dest)) { - tmp = dest; - } else { - tmp = ra_scratch(as, allow); - rset_clear(allow, tmp); - } - if (ra_hasreg(dest) && tmp != dest) - emit_dn(as, A64I_FMOV_D_R, (dest & 31), tmp); - /* Need type check, even if the load result is unused. */ - asm_guardcc(as, irt_isnum(t) ? CC_LS : CC_NE); - if (irt_type(t) >= IRT_NUM) { - lj_assertA(irt_isinteger(t) || irt_isnum(t), - "bad SLOAD type %d", irt_type(t)); - emit_nm(as, A64I_CMPx | A64F_SH(A64SH_LSR, 32), - ra_allock(as, (ir->op2 & IRSLOAD_KEYINDEX) ? LJ_KEYINDEX : (LJ_TISNUM << 15), allow), tmp); - } else if (irt_isnil(t)) { - emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(1), tmp); - } else if (irt_ispri(t)) { - emit_nm(as, A64I_CMPx, - ra_allock(as, ~((int64_t)~irt_toitype(t) << 47) , allow), tmp); - } else { - Reg type = ra_scratch(as, allow); - emit_n(as, (A64I_CMNx^A64I_K12) | A64F_U12(-irt_toitype(t)), type); - emit_dn(as, A64I_ASRx | A64F_IMMR(47), type, tmp); - } - emit_lso(as, A64I_LDRx, tmp, base, ofs); - return; - } - if (ra_hasreg(dest)) { - emit_lso(as, irt_isnum(t) ? A64I_LDRd : - (irt_isint(t) ? A64I_LDRw : A64I_LDRx), (dest & 31), base, - ofs ^ ((LJ_BE && irt_isint(t) ? 4 : 0))); - } -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID id = (CTypeID)IR(ir->op1)->i; - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[4]; - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL), - "bad CNEW/CNEWI operands"); - - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCcdata * */ - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - int32_t ofs = sizeof(GCcdata); - Reg r = ra_alloc1(as, ir->op2, allow); - lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz); - emit_lso(as, sz == 8 ? A64I_STRx : A64I_STRw, r, RID_RET, ofs); - } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */ - ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* CTypeID id */ - args[2] = ir->op2; /* CTSize sz */ - args[3] = ASMREF_TMP1; /* CTSize align */ - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info)); - return; - } - - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - { - Reg r = (id < 65536) ? RID_X1 : ra_allock(as, id, allow); - emit_lso(as, A64I_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct)); - emit_lso(as, A64I_STRH, r, RID_RET, offsetof(GCcdata, ctypeid)); - emit_d(as, A64I_MOVZw | A64F_U16(~LJ_TCDATA), RID_TMP); - if (id < 65536) emit_d(as, A64I_MOVZw | A64F_U16(id), RID_X1); - } - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg mark = RID_TMP; - MCLabel l_end = emit_label(as); - emit_lso(as, A64I_STRx, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_lso(as, A64I_STRB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_setgl(as, tab, gc.grayagain); - emit_dn(as, A64I_ANDw^emit_isk13(~LJ_GC_BLACK, 0), mark, mark); - emit_getgl(as, link, gc.grayagain); - emit_cond_branch(as, CC_EQ, l_end); - emit_n(as, A64I_TSTw^emit_isk13(LJ_GC_BLACK, 0), mark); - emit_lso(as, A64I_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - RegSet allow = RSET_GPR; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type"); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_dm(as, A64I_MOVx, ra_releasetmp(as, ASMREF_TMP1), RID_GL); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(allow, obj)); - emit_cond_branch(as, CC_EQ, l_end); - emit_n(as, A64I_TSTw^emit_isk13(LJ_GC_BLACK, 0), tmp); - emit_cond_branch(as, CC_EQ, l_end); - emit_n(as, A64I_TSTw^emit_isk13(LJ_GC_WHITES, 0), RID_TMP); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_lso(as, A64I_LDRB, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_lso(as, A64I_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -static void asm_fparith(ASMState *as, IRIns *ir, A64Ins ai) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - emit_dnm(as, ai, (dest & 31), (left & 31), (right & 31)); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, A64Ins ai) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_dn(as, ai, (dest & 31), (left & 31)); -} - -static void asm_fpmath(ASMState *as, IRIns *ir) -{ - IRFPMathOp fpm = (IRFPMathOp)ir->op2; - if (fpm == IRFPM_SQRT) { - asm_fpunary(as, ir, A64I_FSQRTd); - } else if (fpm <= IRFPM_TRUNC) { - asm_fpunary(as, ir, fpm == IRFPM_FLOOR ? A64I_FRINTMd : - fpm == IRFPM_CEIL ? A64I_FRINTPd : A64I_FRINTZd); - } else { - asm_callid(as, ir, IRCALL_lj_vm_floor + fpm); - } -} - -static int asm_swapops(ASMState *as, IRRef lref, IRRef rref) -{ - IRIns *ir; - if (irref_isk(rref)) - return 0; /* Don't swap constants to the left. */ - if (irref_isk(lref)) - return 1; /* But swap constants to the right. */ - ir = IR(rref); - if ((ir->o >= IR_BSHL && ir->o <= IR_BSAR) || - (ir->o == IR_ADD && ir->op1 == ir->op2) || - (ir->o == IR_CONV && ir->op2 == ((IRT_I64<o >= IR_BSHL && ir->o <= IR_BSAR) || - (ir->o == IR_ADD && ir->op1 == ir->op2) || - (ir->o == IR_CONV && ir->op2 == ((IRT_I64<op1, rref = ir->op2; - Reg left, dest = ra_dest(as, ir, RSET_GPR); - uint32_t m; - if ((ai & ~A64I_S) != A64I_SUBw && asm_swapops(as, lref, rref)) { - IRRef tmp = lref; lref = rref; rref = tmp; - } - left = ra_hintalloc(as, lref, dest, RSET_GPR); - if (irt_is64(ir->t)) ai |= A64I_X; - m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left)); - if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */ - asm_guardcc(as, CC_VS); - ai |= A64I_S; - } - emit_dn(as, ai^m, dest, left); -} - -static void asm_intop_s(ASMState *as, IRIns *ir, A64Ins ai) -{ - if (as->flagmcp == as->mcp) { /* Drop cmp r, #0. */ - as->flagmcp = NULL; - as->mcp++; - ai |= A64I_S; - } - asm_intop(as, ir, ai); -} - -static void asm_intneg(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_dm(as, irt_is64(ir->t) ? A64I_NEGx : A64I_NEGw, dest, left); -} - -/* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */ -static void asm_intmul(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest)); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - if (irt_isguard(ir->t)) { /* IR_MULOV */ - asm_guardcc(as, CC_NE); - emit_dm(as, A64I_MOVw, dest, dest); /* Zero-extend. */ - emit_nm(as, A64I_CMPw | A64F_SH(A64SH_ASR, 31), RID_TMP, dest); - emit_dn(as, A64I_ASRx | A64F_IMMR(32), RID_TMP, dest); - emit_dnm(as, A64I_SMULL, dest, right, left); - } else { - emit_dnm(as, irt_is64(ir->t) ? A64I_MULx : A64I_MULw, dest, left, right); - } -} - -static void asm_add(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, A64I_FMADDd, A64I_FMADDd)) - asm_fparith(as, ir, A64I_FADDd); - return; - } - asm_intop_s(as, ir, A64I_ADDw); -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, A64I_FNMSUBd, A64I_FMSUBd)) - asm_fparith(as, ir, A64I_FSUBd); - return; - } - asm_intop_s(as, ir, A64I_SUBw); -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, A64I_FMULd); - return; - } - asm_intmul(as, ir); -} - -#define asm_addov(as, ir) asm_add(as, ir) -#define asm_subov(as, ir) asm_sub(as, ir) -#define asm_mulov(as, ir) asm_mul(as, ir) - -#define asm_fpdiv(as, ir) asm_fparith(as, ir, A64I_FDIVd) -#define asm_abs(as, ir) asm_fpunary(as, ir, A64I_FABS) - -static void asm_neg(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, A64I_FNEGd); - return; - } - asm_intneg(as, ir); -} - -static void asm_band(ASMState *as, IRIns *ir) -{ - A64Ins ai = A64I_ANDw; - if (asm_fuseandshift(as, ir)) - return; - if (as->flagmcp == as->mcp) { - /* Try to drop cmp r, #0. */ - as->flagmcp = NULL; - as->mcp++; - ai = A64I_ANDSw; - } - asm_intop(as, ir, ai); -} - -static void asm_borbxor(ASMState *as, IRIns *ir, A64Ins ai) -{ - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irl = IR(lref), *irr = IR(rref); - if ((canfuse(as, irl) && irl->o == IR_BNOT && !irref_isk(rref)) || - (canfuse(as, irr) && irr->o == IR_BNOT && !irref_isk(lref))) { - Reg left, dest = ra_dest(as, ir, RSET_GPR); - uint32_t m; - if (irl->o == IR_BNOT) { - IRRef tmp = lref; lref = rref; rref = tmp; - } - left = ra_alloc1(as, lref, RSET_GPR); - ai |= A64I_ON; - if (irt_is64(ir->t)) ai |= A64I_X; - m = asm_fuseopm(as, ai, IR(rref)->op1, rset_exclude(RSET_GPR, left)); - emit_dn(as, ai^m, dest, left); - } else { - asm_intop(as, ir, ai); - } -} - -static void asm_bor(ASMState *as, IRIns *ir) -{ - if (asm_fuseorshift(as, ir)) - return; - asm_borbxor(as, ir, A64I_ORRw); -} - -#define asm_bxor(as, ir) asm_borbxor(as, ir, A64I_EORw) - -static void asm_bnot(ASMState *as, IRIns *ir) -{ - A64Ins ai = A64I_MVNw; - Reg dest = ra_dest(as, ir, RSET_GPR); - uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR); - if (irt_is64(ir->t)) ai |= A64I_X; - emit_d(as, ai^m, dest); -} - -static void asm_bswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_dn(as, irt_is64(ir->t) ? A64I_REVx : A64I_REVw, dest, left); -} - -static void asm_bitshift(ASMState *as, IRIns *ir, A64Ins ai, A64Shift sh) -{ - int32_t shmask = irt_is64(ir->t) ? 63 : 31; - if (irref_isk(ir->op2)) { /* Constant shifts. */ - Reg left, dest = ra_dest(as, ir, RSET_GPR); - int32_t shift = (IR(ir->op2)->i & shmask); - IRIns *irl = IR(ir->op1); - if (shmask == 63) ai += A64I_UBFMx - A64I_UBFMw; - - /* Fuse BSHL + BSHR/BSAR into UBFM/SBFM aka UBFX/SBFX/UBFIZ/SBFIZ. */ - if ((sh == A64SH_LSR || sh == A64SH_ASR) && canfuse(as, irl)) { - if (irl->o == IR_BSHL && irref_isk(irl->op2)) { - int32_t shift2 = (IR(irl->op2)->i & shmask); - shift = ((shift - shift2) & shmask); - shmask -= shift2; - ir = irl; - } - } - - left = ra_alloc1(as, ir->op1, RSET_GPR); - switch (sh) { - case A64SH_LSL: - emit_dn(as, ai | A64F_IMMS(shmask-shift) | - A64F_IMMR((shmask-shift+1)&shmask), dest, left); - break; - case A64SH_LSR: case A64SH_ASR: - emit_dn(as, ai | A64F_IMMS(shmask) | A64F_IMMR(shift), dest, left); - break; - case A64SH_ROR: - emit_dnm(as, ai | A64F_IMMS(shift), dest, left, left); - break; - } - } else { /* Variable-length shifts. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dnm(as, (shmask == 63 ? A64I_SHRx : A64I_SHRw) | A64F_BSH(sh), dest, left, right); - } -} - -#define asm_bshl(as, ir) asm_bitshift(as, ir, A64I_UBFMw, A64SH_LSL) -#define asm_bshr(as, ir) asm_bitshift(as, ir, A64I_UBFMw, A64SH_LSR) -#define asm_bsar(as, ir) asm_bitshift(as, ir, A64I_SBFMw, A64SH_ASR) -#define asm_bror(as, ir) asm_bitshift(as, ir, A64I_EXTRw, A64SH_ROR) -#define asm_brol(as, ir) lj_assertA(0, "unexpected BROL") - -static void asm_intmin_max(ASMState *as, IRIns *ir, A64CC cc) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dnm(as, A64I_CSELw|A64F_CC(cc), dest, left, right); - emit_nm(as, A64I_CMPw, left, right); -} - -static void asm_fpmin_max(ASMState *as, IRIns *ir, A64CC fcc) -{ - Reg dest = (ra_dest(as, ir, RSET_FPR) & 31); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = ((left >> 8) & 31); left &= 31; - emit_dnm(as, A64I_FCSELd | A64F_CC(fcc), dest, right, left); - emit_nm(as, A64I_FCMPd, left, right); -} - -static void asm_min_max(ASMState *as, IRIns *ir, A64CC cc, A64CC fcc) -{ - if (irt_isnum(ir->t)) - asm_fpmin_max(as, ir, fcc); - else - asm_intmin_max(as, ir, cc); -} - -#define asm_min(as, ir) asm_min_max(as, ir, CC_LT, CC_PL) -#define asm_max(as, ir) asm_min_max(as, ir, CC_GT, CC_LE) - -/* -- Comparisons --------------------------------------------------------- */ - -/* Map of comparisons to flags. ORDER IR. */ -static const uint8_t asm_compmap[IR_ABC+1] = { - /* op FP swp int cc FP cc */ - /* LT */ CC_GE + (CC_HS << 4), - /* GE x */ CC_LT + (CC_HI << 4), - /* LE */ CC_GT + (CC_HI << 4), - /* GT x */ CC_LE + (CC_HS << 4), - /* ULT x */ CC_HS + (CC_LS << 4), - /* UGE */ CC_LO + (CC_LO << 4), - /* ULE x */ CC_HI + (CC_LO << 4), - /* UGT */ CC_LS + (CC_LS << 4), - /* EQ */ CC_NE + (CC_NE << 4), - /* NE */ CC_EQ + (CC_EQ << 4), - /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */ -}; - -/* FP comparisons. */ -static void asm_fpcomp(ASMState *as, IRIns *ir) -{ - Reg left, right; - A64Ins ai; - int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1); - if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) { - left = (ra_alloc1(as, ir->op1, RSET_FPR) & 31); - right = 0; - ai = A64I_FCMPZd; - } else { - left = ra_alloc2(as, ir, RSET_FPR); - if (swp) { - right = (left & 31); left = ((left >> 8) & 31); - } else { - right = ((left >> 8) & 31); left &= 31; - } - ai = A64I_FCMPd; - } - asm_guardcc(as, (asm_compmap[ir->o] >> 4)); - emit_nm(as, ai, left, right); -} - -/* Integer comparisons. */ -static void asm_intcomp(ASMState *as, IRIns *ir) -{ - A64CC oldcc, cc = (asm_compmap[ir->o] & 15); - A64Ins ai = irt_is64(ir->t) ? A64I_CMPx : A64I_CMPw; - IRRef lref = ir->op1, rref = ir->op2; - Reg left; - uint32_t m; - int cmpprev0 = 0; - lj_assertA(irt_is64(ir->t) || irt_isint(ir->t) || - irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t), - "bad comparison data type %d", irt_type(ir->t)); - if (asm_swapops(as, lref, rref)) { - IRRef tmp = lref; lref = rref; rref = tmp; - if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */ - else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */ - } - oldcc = cc; - if (irref_isk(rref) && get_k64val(as, rref) == 0) { - IRIns *irl = IR(lref); - if (cc == CC_GE) cc = CC_PL; - else if (cc == CC_LT) cc = CC_MI; - else if (cc > CC_NE) goto nocombine; /* Other conds don't work with tst. */ - cmpprev0 = (irl+1 == ir); - /* Combine and-cmp-bcc into tbz/tbnz or and-cmp into tst. */ - if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) { - IRRef blref = irl->op1, brref = irl->op2; - uint32_t m2 = 0; - Reg bleft; - if (asm_swapops(as, blref, brref)) { - Reg tmp = blref; blref = brref; brref = tmp; - } - if (irref_isk(brref)) { - uint64_t k = get_k64val(as, brref); - if (k && !(k & (k-1)) && (cc == CC_EQ || cc == CC_NE)) { - asm_guardtnb(as, cc == CC_EQ ? A64I_TBZ : A64I_TBNZ, - ra_alloc1(as, blref, RSET_GPR), emit_ctz64(k)); - return; - } - m2 = emit_isk13(k, irt_is64(irl->t)); - } - bleft = ra_alloc1(as, blref, RSET_GPR); - ai = (irt_is64(irl->t) ? A64I_TSTx : A64I_TSTw); - if (!m2) - m2 = asm_fuseopm(as, ai, brref, rset_exclude(RSET_GPR, bleft)); - asm_guardcc(as, cc); - emit_n(as, ai^m2, bleft); - return; - } - if (cc == CC_EQ || cc == CC_NE) { - /* Combine cmp-bcc into cbz/cbnz. */ - ai = cc == CC_EQ ? A64I_CBZ : A64I_CBNZ; - if (irt_is64(ir->t)) ai |= A64I_X; - asm_guardcnb(as, ai, ra_alloc1(as, lref, RSET_GPR)); - return; - } - } -nocombine: - left = ra_alloc1(as, lref, RSET_GPR); - m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left)); - asm_guardcc(as, cc); - emit_n(as, ai^m, left); - /* Signed comparison with zero and referencing previous ins? */ - if (cmpprev0 && (oldcc <= CC_NE || oldcc >= CC_GE)) - as->flagmcp = as->mcp; /* Allow elimination of the compare. */ -} - -static void asm_comp(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fpcomp(as, ir); - else - asm_intcomp(as, ir); -} - -#define asm_equal(as, ir) asm_comp(as, ir) - -/* -- Split register ops -------------------------------------------------- */ - -/* Hiword op of a split 64/64 bit op. Previous op is the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { - case IR_CALLN: - case IR_CALLL: - case IR_CALLS: - case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break; - } -} - -/* -- Profiling ----------------------------------------------------------- */ - -static void asm_prof(ASMState *as, IRIns *ir) -{ - uint32_t k = emit_isk13(HOOK_PROFILE, 0); - lj_assertA(k != 0, "HOOK_PROFILE does not fit in K13"); - UNUSED(ir); - asm_guardcc(as, CC_NE); - emit_n(as, A64I_TSTw^k, RID_TMP); - emit_lsptr(as, A64I_LDRB, RID_TMP, (void *)&J2G(as->J)->hookmask); -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - Reg pbase; - uint32_t k; - if (irp) { - if (!ra_hasspill(irp->s)) { - pbase = irp->r; - lj_assertA(ra_hasreg(pbase), "base reg lost"); - } else if (allow) { - pbase = rset_pickbot(allow); - } else { - pbase = RID_RET; - emit_lso(as, A64I_LDRx, RID_RET, RID_SP, 0); /* Restore temp register. */ - } - } else { - pbase = RID_BASE; - } - emit_cond_branch(as, CC_LS, asm_exitstub_addr(as, exitno)); - k = emit_isk12((8*topslot)); - lj_assertA(k, "slot offset %d does not fit in K12", 8*topslot); - emit_n(as, A64I_CMPx^k, RID_TMP); - emit_dnm(as, A64I_SUBx, RID_TMP, RID_TMP, pbase); - emit_lso(as, A64I_LDRx, RID_TMP, RID_TMP, - (int32_t)offsetof(lua_State, maxstack)); - if (irp) { /* Must not spill arbitrary registers in head of side trace. */ - if (ra_hasspill(irp->s)) - emit_lso(as, A64I_LDRx, pbase, RID_SP, sps_scale(irp->s)); - emit_lso(as, A64I_LDRx, RID_TMP, RID_GL, glofs(as, &J2G(as->J)->cur_L)); - if (ra_hasspill(irp->s) && !allow) - emit_lso(as, A64I_STRx, RID_RET, RID_SP, 0); /* Save temp register. */ - } else { - emit_getgl(as, RID_TMP, cur_L); - } -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; -#ifdef LUA_USE_ASSERT - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2]; -#endif - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1-LJ_FR2); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if ((sn & SNAP_KEYINDEX)) { - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - Reg r = irref_isk(ref) ? ra_allock(as, ir->i, allow) : - ra_alloc1(as, ref, allow); - rset_clear(allow, r); - emit_lso(as, A64I_STRw, r, RID_BASE, ofs); - emit_lso(as, A64I_STRw, ra_allock(as, LJ_KEYINDEX, allow), RID_BASE, ofs+4); - } else if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_lso(as, A64I_STRd, (src & 31), RID_BASE, ofs); - } else { - asm_tvstore64(as, RID_BASE, ofs, ref); - } - checkmclim(as); - } - lj_assertA(map + nent == flinks, "inconsistent frames in snapshot"); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Marker to prevent patching the GC check exit. */ -#define ARM64_NOPATCH_GC_CHECK \ - (A64I_ORRx|A64F_D(RID_TMP)|A64F_M(RID_TMP)|A64F_N(RID_TMP)) - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp2; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcnb(as, A64I_CBNZ, RID_RET); /* Assumes asm_snap_prep() is done. */ - *--as->mcp = ARM64_NOPATCH_GC_CHECK; - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - emit_dm(as, A64I_MOVx, ra_releasetmp(as, ASMREF_TMP1), RID_GL); - tmp2 = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp2, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_cond_branch(as, CC_LS, l_end); - emit_nm(as, A64I_CMPx, RID_TMP, tmp2); - emit_getgl(as, tmp2, gc.threshold); - emit_getgl(as, RID_TMP, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->loopinv) { /* Inverted loop branch? */ - uint32_t mask = (p[-2] & 0x7e000000) == 0x36000000 ? 0x3fffu : 0x7ffffu; - ptrdiff_t delta = target - (p - 2); - /* asm_guard* already inverted the bcc/tnb/cnb and patched the final b. */ - p[-2] |= ((uint32_t)delta & mask) << 5; - } else { - ptrdiff_t delta = target - (p - 1); - p[-1] = A64I_B | A64F_S26(delta); - } -} - -/* Fixup the tail of the loop. */ -static void asm_loop_tail_fixup(ASMState *as) -{ - UNUSED(as); /* Nothing to do. */ -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Reload L register from g->cur_L. */ -static void asm_head_lreg(ASMState *as) -{ - IRIns *ir = IR(ASMREF_L); - if (ra_used(ir)) { - Reg r = ra_dest(as, ir, RSET_GPR); - emit_getgl(as, r, cur_L); - ra_evictk(as); - } -} - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir; - asm_head_lreg(as); - ir = IR(REF_BASE); - if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t))) - ra_spill(as, ir); - ra_destreg(as, ir, RID_BASE); -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir; - asm_head_lreg(as); - ir = IR(REF_BASE); - if (ra_hasreg(ir->r) && (rset_test(as->modset, ir->r) || irt_ismarked(ir->t))) - ra_spill(as, ir); - if (ra_hasspill(irp->s)) { - rset_clear(allow, ra_dest(as, ir, allow)); - } else { - Reg r = irp->r; - lj_assertA(ra_hasreg(r), "base reg lost"); - rset_clear(allow, r); - if (r != ir->r && !rset_test(as->freeset, r)) - ra_restore(as, regcost_ref(as->cost[r])); - ra_destreg(as, ir, r); - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *p = as->mctop; - MCode *target; - /* Undo the sp adjustment in BC_JLOOP when exiting to the interpreter. */ - int32_t spadj = as->T->spadjust + (lnk ? 0 : sps_scale(SPS_FIXED)); - if (spadj == 0) { - *--p = A64I_LE(A64I_NOP); - as->mctop = p; - } else { - /* Patch stack adjustment. */ - uint32_t k = emit_isk12(spadj); - lj_assertA(k, "stack adjustment %d does not fit in K12", spadj); - p[-2] = (A64I_ADDx^k) | A64F_D(RID_SP) | A64F_N(RID_SP); - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - p[-1] = A64I_B | A64F_S26((target-p)+1); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop - 1; /* Leave room for exit branch. */ - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - as->mcp = p-1; /* Leave room for stack pointer adjustment. */ - as->invmcp = NULL; - } - *p = 0; /* Prevent load/store merging. */ -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = CCI_XNARGS(ci); - int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) { - if (args[i] && irt_isfp(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; else nslots += 2; - } else { - if (ngpr > 0) ngpr--; else nslots += 2; - } - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - /* May need extra exit for asm_stack_check on side traces. */ - asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); -} - -#if LJ_BE -/* ARM64 instructions are always little-endian. Swap for ARM64BE. */ -static void asm_mcode_fixup(MCode *mcode, MSize size) -{ - MCode *pe = (MCode *)((char *)mcode + size); - while (mcode < pe) { - MCode ins = *mcode; - *mcode++ = lj_bswap(ins); - } -} -#define LJ_TARGET_MCODE_FIXUP 1 -#endif - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *cstart = NULL; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MCode *px = exitstub_trace_addr(T, exitno); - int patchlong = 1; - /* Note: this assumes a trace exit is only ever patched once. */ - for (; p < pe; p++) { - /* Look for exitstub branch, replace with branch to target. */ - ptrdiff_t delta = target - p; - MCode ins = A64I_LE(*p); - if ((ins & 0xff000000u) == 0x54000000u && - ((ins ^ ((px-p)<<5)) & 0x00ffffe0u) == 0) { - /* Patch bcc, if within range. */ - if (A64F_S_OK(delta, 19)) { - *p = A64I_LE((ins & 0xff00001fu) | A64F_S19(delta)); - if (!cstart) cstart = p; - } - } else if ((ins & 0xfc000000u) == 0x14000000u && - ((ins ^ (px-p)) & 0x03ffffffu) == 0) { - /* Patch b. */ - lj_assertJ(A64F_S_OK(delta, 26), "branch target out of range"); - *p = A64I_LE((ins & 0xfc000000u) | A64F_S26(delta)); - if (!cstart) cstart = p; - } else if ((ins & 0x7e000000u) == 0x34000000u && - ((ins ^ ((px-p)<<5)) & 0x00ffffe0u) == 0) { - /* Patch cbz/cbnz, if within range. */ - if (p[-1] == ARM64_NOPATCH_GC_CHECK) { - patchlong = 0; - } else if (A64F_S_OK(delta, 19)) { - *p = A64I_LE((ins & 0xff00001fu) | A64F_S19(delta)); - if (!cstart) cstart = p; - } - } else if ((ins & 0x7e000000u) == 0x36000000u && - ((ins ^ ((px-p)<<5)) & 0x0007ffe0u) == 0) { - /* Patch tbz/tbnz, if within range. */ - if (A64F_S_OK(delta, 14)) { - *p = A64I_LE((ins & 0xfff8001fu) | A64F_S14(delta)); - if (!cstart) cstart = p; - } - } - } - /* Always patch long-range branch in exit stub itself. Except, if we can't. */ - if (patchlong) { - ptrdiff_t delta = target - px; - lj_assertJ(A64F_S_OK(delta, 26), "branch target out of range"); - *px = A64I_B | A64F_S26(delta); - if (!cstart) cstart = px; - } - if (cstart) lj_mcode_sync(cstart, px+1); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_mips.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_mips.h deleted file mode 100644 index 1686b40..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_mips.h +++ /dev/null @@ -1,2808 +0,0 @@ -/* -** MIPS IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate a register or RID_ZERO. */ -static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!(allow & RSET_FPR) && irref_isk(ref) && get_kval(as, ref) == 0) - return RID_ZERO; - r = ra_allocref(as, ref, allow); - } else { - ra_noweak(as, r); - } - return r; -} - -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_alloc1z(as, ir->op2, allow); - left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_alloc1z(as, ir->op1, allow); - right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} - -/* -- Guard handling ------------------------------------------------------ */ - -/* Need some spare long-range jump slots, for out-of-range branches. */ -#define MIPS_SPAREJUMP 4 - -/* Setup spare long-range jump slots per mcarea. */ -static void asm_sparejump_setup(ASMState *as) -{ - MCode *mxp = as->mctop; - if ((char *)mxp == (char *)as->J->mcarea + as->J->szmcarea) { - mxp -= MIPS_SPAREJUMP*2; - lj_assertA(MIPSI_NOP == 0, "bad NOP"); - memset(mxp, 0, MIPS_SPAREJUMP*2*sizeof(MCode)); - as->mctop = mxp; - } -} - -static MCode *asm_sparejump_use(MCode *mcarea, MCode tjump) -{ - MCode *mxp = (MCode *)((char *)mcarea + ((MCLink *)mcarea)->size); - int slot = MIPS_SPAREJUMP; - while (slot--) { - mxp -= 2; - if (*mxp == tjump) { - return mxp; - } else if (*mxp == MIPSI_NOP) { - *mxp = tjump; - return mxp; - } - } - return NULL; -} - -/* Setup exit stub after the end of each trace. */ -static void asm_exitstub_setup(ASMState *as) -{ - MCode *mxp = as->mctop; - /* sw TMP, 0(sp); j ->vm_exit_handler; li TMP, traceno */ - *--mxp = MIPSI_LI|MIPSF_T(RID_TMP)|as->T->traceno; - *--mxp = MIPSI_J|((((uintptr_t)(void *)lj_vm_exit_handler)>>2)&0x03ffffffu); - lj_assertA(((uintptr_t)mxp ^ (uintptr_t)(void *)lj_vm_exit_handler)>>28 == 0, - "branch target out of range"); - *--mxp = MIPSI_SW|MIPSF_T(RID_TMP)|MIPSF_S(RID_SP)|0; - as->mctop = mxp; -} - -/* Keep this in-sync with exitstub_trace_addr(). */ -#define asm_exitstub_addr(as) ((as)->mctop) - -/* Emit conditional branch to exit for guard. */ -static void asm_guard(ASMState *as, MIPSIns mi, Reg rs, Reg rt) -{ - MCode *target = asm_exitstub_addr(as); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->invmcp = NULL; - as->loopinv = 1; - as->mcp = p+1; -#if !LJ_TARGET_MIPSR6 - mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : 0x00010000u); /* Invert cond. */ -#else - mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : - (mi>>28) == 4 ? 0x00800000u : 0x00010000u); /* Invert cond. */ -#endif - target = p; /* Patch target later in asm_loop_fixup. */ - } - emit_ti(as, MIPSI_LI, RID_TMP, as->snapno); - emit_branch(as, mi, rs, rt, target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - intptr_t ofs = (intptr_t)&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv; - intptr_t jgl = (intptr_t)J2G(as->J); - if ((uintptr_t)(ofs-jgl) < 65536) { - *ofsp = ofs-jgl-32768; - return RID_JGL; - } else { - *ofsp = (int16_t)ofs; - return ra_allock(as, ofs-(int16_t)ofs, allow); - } - } - } else if (ir->o == IR_TMPREF) { - *ofsp = (int32_t)(offsetof(global_State, tmptv)-32768); - return RID_JGL; - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref, - RegSet allow, int32_t ofs) -{ - IRIns *ir = IR(ref); - Reg base; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - if (ir->o == IR_ADD) { - intptr_t ofs2; - if (irref_isk(ir->op2) && (ofs2 = ofs + get_kval(as, ir->op2), - checki16(ofs2))) { - ref = ir->op1; - ofs = (int32_t)ofs2; - } - } else if (ir->o == IR_STRREF) { - intptr_t ofs2 = 65536; - lj_assertA(ofs == 0, "bad usage"); - ofs = (int32_t)sizeof(GCstr); - if (irref_isk(ir->op2)) { - ofs2 = ofs + get_kval(as, ir->op2); - ref = ir->op1; - } else if (irref_isk(ir->op1)) { - ofs2 = ofs + get_kval(as, ir->op1); - ref = ir->op2; - } - if (!checki16(ofs2)) { - /* NYI: Fuse ADD with constant. */ - Reg right, left = ra_alloc2(as, ir, allow); - right = (left >> 8); left &= 255; - emit_hsi(as, mi, rt, RID_TMP, ofs); - emit_dst(as, MIPSI_AADDU, RID_TMP, left, right); - return; - } - ofs = ofs2; - } - } - base = ra_alloc1(as, ref, allow); - emit_hsi(as, mi, rt, base, ofs); -} - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_XNARGS(ci); - int32_t ofs = LJ_32 ? 16 : 0; -#if LJ_SOFTFP - Reg gpr = REGARG_FIRSTGPR; -#else - Reg gpr, fpr = REGARG_FIRSTFPR; -#endif - if ((void *)ci->func) - emit_call(as, (void *)ci->func, 1); -#if !LJ_SOFTFP - for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) - as->cost[gpr] = REGCOST(~0u, ASMREF_L); - gpr = REGARG_FIRSTGPR; -#endif - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - if (ref) { - IRIns *ir = IR(ref); -#if !LJ_SOFTFP - if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR && - !(ci->flags & CCI_VARARG)) { - lj_assertA(rset_test(as->freeset, fpr), - "reg %d not free", fpr); /* Already evicted. */ - ra_leftov(as, fpr, ref); - fpr += LJ_32 ? 2 : 1; - gpr += (LJ_32 && irt_isnum(ir->t)) ? 2 : 1; - } else -#endif - { -#if LJ_32 && !LJ_SOFTFP - fpr = REGARG_LASTFPR+1; -#endif - if (LJ_32 && irt_isnum(ir->t)) gpr = (gpr+1) & ~1; - if (gpr <= REGARG_LASTGPR) { - lj_assertA(rset_test(as->freeset, gpr), - "reg %d not free", gpr); /* Already evicted. */ -#if !LJ_SOFTFP - if (irt_isfp(ir->t)) { - RegSet of = as->freeset; - Reg r; - /* Workaround to protect argument GPRs from being used for remat. */ - as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); - r = ra_alloc1(as, ref, RSET_FPR); - as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); - if (irt_isnum(ir->t)) { -#if LJ_32 - emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1); - emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r); - lj_assertA(rset_test(as->freeset, gpr+1), - "reg %d not free", gpr+1); /* Already evicted. */ - gpr += 2; -#else - emit_tg(as, MIPSI_DMFC1, gpr, r); - gpr++; fpr++; -#endif - } else if (irt_isfloat(ir->t)) { - emit_tg(as, MIPSI_MFC1, gpr, r); - gpr++; -#if LJ_64 - fpr++; -#endif - } - } else -#endif - { - ra_leftov(as, gpr, ref); - gpr++; -#if LJ_64 && !LJ_SOFTFP - fpr++; -#endif - } - } else { - Reg r = ra_alloc1z(as, ref, !LJ_SOFTFP && irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); -#if LJ_32 - if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; - emit_spstore(as, ir, r, ofs); - ofs += irt_isnum(ir->t) ? 8 : 4; -#else - emit_spstore(as, ir, r, ofs + ((LJ_BE && !irt_isfp(ir->t) && !irt_is64(ir->t)) ? 4 : 0)); - ofs += 8; -#endif - } - } - } else { -#if !LJ_SOFTFP - fpr = REGARG_LASTFPR+1; -#endif - if (gpr <= REGARG_LASTGPR) { - gpr++; -#if LJ_64 && !LJ_SOFTFP - fpr++; -#endif - } else { - ofs += LJ_32 ? 4 : 8; - } - } - checkmclim(as); - } -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)); -#if !LJ_SOFTFP - if ((ci->flags & CCI_NOFPRCLOBBER)) - drop &= ~RSET_FPR; -#endif - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lj_assertA(!irt_ispri(ir->t), "PRI dest"); - if (!LJ_SOFTFP && irt_isfp(ir->t)) { - if ((ci->flags & CCI_CASTU64)) { - int32_t ofs = sps_scale(ir->s); - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); -#if LJ_32 - emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1); - emit_tg(as, MIPSI_MTC1, RID_RETLO, dest); -#else - emit_tg(as, MIPSI_DMTC1, RID_RET, dest); -#endif - } - if (ofs) { -#if LJ_32 - emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0)); - emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4)); -#else - emit_tsi(as, MIPSI_SD, RID_RET, RID_SP, ofs); -#endif - } - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(void *)get_kval(as, func); - } else { /* Need specific register for indirect calls. */ - Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR)); - MCode *p = as->mcp; - if (r == RID_CFUNCADDR) - *--p = MIPSI_NOP; - else - *--p = MIPSI_MOVE | MIPSF_D(RID_CFUNCADDR) | MIPSF_S(r); - *--p = MIPSI_JALR | MIPSF_S(r); - as->mcp = p; - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -#if !LJ_SOFTFP -static void asm_callround(ASMState *as, IRIns *ir, IRCallID id) -{ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RID2RSET(RID_R1)|RID2RSET(RID_R12)|RID2RSET(RID_FPRET)| - RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(REGARG_FIRSTFPR) -#if LJ_TARGET_MIPSR6 - |RID2RSET(RID_F21) -#endif - ; - if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); - ra_evictset(as, drop); - ra_destreg(as, ir, RID_FPRET); - emit_call(as, (void *)lj_ir_callinfo[id].func, 0); - ra_leftov(as, REGARG_FIRSTFPR, ir->op1); -} -#endif - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */ - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guard(as, MIPSI_BNE, RID_TMP, - ra_allock(as, igcptr(pc), rset_exclude(RSET_GPR, base))); - emit_tsi(as, MIPSI_AL, RID_TMP, base, -8); -} - -/* -- Buffer operations --------------------------------------------------- */ - -#if LJ_HASBUFFER -static void asm_bufhdr_write(ASMState *as, Reg sb) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); - IRIns irgc; - irgc.ot = IRT(0, IRT_PGC); /* GC type. */ - emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L)); - if ((as->flags & JIT_F_MIPSXXR2)) { - emit_tsml(as, LJ_64 ? MIPSI_DINS : MIPSI_INS, RID_TMP, tmp, - lj_fls(SBUF_MASK_FLAG), 0); - } else { - emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp); - emit_tsi(as, MIPSI_ANDI, tmp, tmp, SBUF_MASK_FLAG); - } - emit_getgl(as, RID_TMP, cur_L); - emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L)); -} -#endif - -/* -- Type conversions ---------------------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); -#if !LJ_TARGET_MIPSR6 - asm_guard(as, MIPSI_BC1F, 0, 0); - emit_fgh(as, MIPSI_C_EQ_D, 0, tmp, left); -#else - asm_guard(as, MIPSI_BC1EQZ, 0, (tmp&31)); - emit_fgh(as, MIPSI_CMP_EQ_D, tmp, tmp, left); -#endif - emit_fg(as, MIPSI_CVT_D_W, tmp, tmp); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, MIPSI_CVT_W_D, tmp, left); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fgh(as, MIPSI_ADD_D, tmp, left, right); -} -#elif LJ_64 /* && LJ_SOFTFP */ -static void asm_tointg(ASMState *as, IRIns *ir, Reg r) -{ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RID2RSET(REGARG_FIRSTGPR)|RID2RSET(RID_RET)|RID2RSET(RID_RET+1)| - RID2RSET(RID_R1)|RID2RSET(RID_R12); - if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); - ra_evictset(as, drop); - /* Return values are in RID_RET (converted value) and RID_RET+1 (status). */ - ra_destreg(as, ir, RID_RET); - asm_guard(as, MIPSI_BNE, RID_RET+1, RID_ZERO); - emit_call(as, (void *)lj_ir_callinfo[IRCALL_lj_vm_tointg].func, 0); - if (r == RID_NONE) - ra_leftov(as, REGARG_FIRSTGPR, ir->op1); - else if (r != REGARG_FIRSTGPR) - emit_move(as, REGARG_FIRSTGPR, r); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_dta(as, MIPSI_SLL, dest, dest, 0); - asm_callid(as, ir, IRCALL_lj_vm_tobit); -} -#endif - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if !LJ_SOFTFP32 - int stfp = (st == IRT_NUM || st == IRT_FLOAT); -#endif -#if LJ_64 - int st64 = (st == IRT_I64 || st == IRT_U64 || st == IRT_P64); -#endif - IRRef lref = ir->op1; -#if LJ_32 - /* 64 bit integer conversions are handled by SPLIT. */ - lj_assertA(!(irt_isint64(ir->t) || (st == IRT_I64 || st == IRT_U64)), - "IR %04d has unsplit 64 bit type", - (int)(ir - as->ir) - REF_BIAS); -#endif -#if LJ_SOFTFP32 - /* FP conversions are handled by SPLIT. */ - lj_assertA(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT), - "IR %04d has FP type", - (int)(ir - as->ir) - REF_BIAS); - /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */ -#else - lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV"); -#if !LJ_SOFTFP - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - emit_fg(as, st == IRT_NUM ? MIPSI_CVT_S_D : MIPSI_CVT_D_S, - dest, ra_alloc1(as, lref, RSET_FPR)); - } else if (st == IRT_U32) { /* U32 to FP conversion. */ - /* y = (x ^ 0x8000000) + 2147483648.0 */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - if (irt_isfloat(ir->t)) - emit_fg(as, MIPSI_CVT_S_D, dest, dest); - /* Must perform arithmetic with doubles to keep the precision. */ - emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp); - emit_fg(as, MIPSI_CVT_D_W, dest, dest); - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), - (void *)&as->J->k64[LJ_K64_2P31], RSET_GPR); - emit_tg(as, MIPSI_MTC1, RID_TMP, dest); - emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left); - emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); -#if LJ_64 - } else if(st == IRT_U64) { /* U64 to FP conversion. */ - /* if (x >= 1u<<63) y = (double)(int64_t)(x&(1u<<63)-1) + pow(2.0, 63) */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - MCLabel l_end = emit_label(as); - if (irt_isfloat(ir->t)) { - emit_fgh(as, MIPSI_ADD_S, dest, dest, tmp); - emit_lsptr(as, MIPSI_LWC1, (tmp & 31), (void *)&as->J->k32[LJ_K32_2P63], - rset_exclude(RSET_GPR, left)); - emit_fg(as, MIPSI_CVT_S_L, dest, dest); - } else { - emit_fgh(as, MIPSI_ADD_D, dest, dest, tmp); - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), (void *)&as->J->k64[LJ_K64_2P63], - rset_exclude(RSET_GPR, left)); - emit_fg(as, MIPSI_CVT_D_L, dest, dest); - } - emit_branch(as, MIPSI_BGEZ, left, RID_ZERO, l_end); - emit_tg(as, MIPSI_DMTC1, RID_TMP, dest); - emit_tsml(as, MIPSI_DEXTM, RID_TMP, left, 30, 0); -#endif - } else { /* Integer to FP conversion. */ - Reg left = ra_alloc1(as, lref, RSET_GPR); -#if LJ_32 - emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W, - dest, dest); - emit_tg(as, MIPSI_MTC1, left, dest); -#else - MIPSIns mi = irt_isfloat(ir->t) ? - (st64 ? MIPSI_CVT_S_L : MIPSI_CVT_S_W) : - (st64 ? MIPSI_CVT_D_L : MIPSI_CVT_D_W); - emit_fg(as, mi, dest, dest); - emit_tg(as, st64 ? MIPSI_DMTC1 : MIPSI_MTC1, left, dest); -#endif - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lj_assertA(irt_isint(ir->t) && st == IRT_NUM, - "bad type for checked CONV"); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - if (irt_isu32(ir->t)) { /* FP to U32 conversion. */ - /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */ - emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP); - emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, st == IRT_FLOAT ? MIPSI_FLOOR_W_S : MIPSI_FLOOR_W_D, - tmp, tmp); - emit_fgh(as, st == IRT_FLOAT ? MIPSI_SUB_S : MIPSI_SUB_D, - tmp, left, tmp); - if (st == IRT_FLOAT) - emit_lsptr(as, MIPSI_LWC1, (tmp & 31), - (void *)&as->J->k32[LJ_K32_2P31], RSET_GPR); - else - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), - (void *)&as->J->k64[LJ_K64_2P31], RSET_GPR); -#if LJ_64 - } else if (irt_isu64(ir->t)) { /* FP to U64 conversion. */ - MCLabel l_end; - emit_tg(as, MIPSI_DMFC1, dest, tmp); - l_end = emit_label(as); - /* For inputs >= 2^63 add -2^64 and convert again. */ - if (st == IRT_NUM) { - emit_fg(as, MIPSI_TRUNC_L_D, tmp, tmp); - emit_fgh(as, MIPSI_ADD_D, tmp, left, tmp); - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), - (void *)&as->J->k64[LJ_K64_M2P64], - rset_exclude(RSET_GPR, dest)); - emit_fg(as, MIPSI_TRUNC_L_D, tmp, left); /* Delay slot. */ -#if !LJ_TARGET_MIPSR6 - emit_branch(as, MIPSI_BC1T, 0, 0, l_end); - emit_fgh(as, MIPSI_C_OLT_D, 0, left, tmp); -#else - emit_branch(as, MIPSI_BC1NEZ, 0, (left&31), l_end); - emit_fgh(as, MIPSI_CMP_LT_D, left, left, tmp); -#endif - emit_lsptr(as, MIPSI_LDC1, (tmp & 31), - (void *)&as->J->k64[LJ_K64_2P63], - rset_exclude(RSET_GPR, dest)); - } else { - emit_fg(as, MIPSI_TRUNC_L_S, tmp, tmp); - emit_fgh(as, MIPSI_ADD_S, tmp, left, tmp); - emit_lsptr(as, MIPSI_LWC1, (tmp & 31), - (void *)&as->J->k32[LJ_K32_M2P64], - rset_exclude(RSET_GPR, dest)); - emit_fg(as, MIPSI_TRUNC_L_S, tmp, left); /* Delay slot. */ -#if !LJ_TARGET_MIPSR6 - emit_branch(as, MIPSI_BC1T, 0, 0, l_end); - emit_fgh(as, MIPSI_C_OLT_S, 0, left, tmp); -#else - emit_branch(as, MIPSI_BC1NEZ, 0, (left&31), l_end); - emit_fgh(as, MIPSI_CMP_LT_S, left, left, tmp); -#endif - emit_lsptr(as, MIPSI_LWC1, (tmp & 31), - (void *)&as->J->k32[LJ_K32_2P63], - rset_exclude(RSET_GPR, dest)); - } -#endif - } else { -#if LJ_32 - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D, - tmp, left); -#else - MIPSIns mi = irt_is64(ir->t) ? - (st == IRT_NUM ? MIPSI_TRUNC_L_D : MIPSI_TRUNC_L_S) : - (st == IRT_NUM ? MIPSI_TRUNC_W_D : MIPSI_TRUNC_W_S); - emit_tg(as, irt_is64(ir->t) ? MIPSI_DMFC1 : MIPSI_MFC1, dest, left); - emit_fg(as, mi, left, left); -#endif - } - } - } else -#else - if (irt_isfp(ir->t)) { -#if LJ_64 && LJ_HASFFI - if (stfp) { /* FP to FP conversion. */ - asm_callid(as, ir, irt_isnum(ir->t) ? IRCALL_softfp_f2d : - IRCALL_softfp_d2f); - } else { /* Integer to FP conversion. */ - IRCallID cid = ((IRT_IS64 >> st) & 1) ? - (irt_isnum(ir->t) ? - (st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d) : - (st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f)) : - (irt_isnum(ir->t) ? - (st == IRT_INT ? IRCALL_softfp_i2d : IRCALL_softfp_ui2d) : - (st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f)); - asm_callid(as, ir, cid); - } -#else - asm_callid(as, ir, IRCALL_softfp_i2d); -#endif - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lj_assertA(irt_isint(ir->t) && st == IRT_NUM, - "bad type for checked CONV"); - asm_tointg(as, ir, RID_NONE); - } else { - IRCallID cid = irt_is64(ir->t) ? - ((st == IRT_NUM) ? - (irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul) : - (irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul)) : - ((st == IRT_NUM) ? - (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) : - (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui)); - asm_callid(as, ir, cid); - } - } else -#endif -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT"); - if ((ir->op2 & IRCONV_SEXT)) { - if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) { - emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left); - } else { - uint32_t shift = st == IRT_I8 ? 24 : 16; - emit_dta(as, MIPSI_SRA, dest, dest, shift); - emit_dta(as, MIPSI_SLL, dest, left, shift); - } - } else { - emit_tsi(as, MIPSI_ANDI, dest, left, - (int32_t)(st == IRT_U8 ? 0xff : 0xffff)); - } - } else { /* 32/64 bit integer conversions. */ -#if LJ_32 - /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ -#else - if (irt_is64(ir->t)) { - if (st64) { - /* 64/64 bit no-op (cast)*/ - ra_leftov(as, dest, lref); - } else { - Reg left = ra_alloc1(as, lref, RSET_GPR); - if ((ir->op2 & IRCONV_SEXT)) { /* 32 to 64 bit sign extension. */ - emit_dta(as, MIPSI_SLL, dest, left, 0); - } else { /* 32 to 64 bit zero extension. */ - emit_tsml(as, MIPSI_DEXT, dest, left, 31, 0); - } - } - } else { - if (st64 && !(ir->op2 & IRCONV_NONE)) { - /* This is either a 32 bit reg/reg mov which zeroes the hiword - ** or a load of the loword from a 64 bit address. - */ - Reg left = ra_alloc1(as, lref, RSET_GPR); - emit_tsml(as, MIPSI_DEXT, dest, left, 31, 0); - } else { /* 32/32 bit no-op (cast). */ - /* Do nothing, but may need to move regs. */ - ra_leftov(as, dest, lref); - } - } -#endif - } - } -} - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - int32_t ofs = 0; -#if LJ_SOFTFP32 - ra_evictset(as, RSET_SCRATCH); - if (ra_used(ir)) { - if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) && - (ir->s & 1) == LJ_BE && (ir->s ^ 1) == (ir+1)->s) { - int i; - for (i = 0; i < 2; i++) { - Reg r = (ir+i)->r; - if (ra_hasreg(r)) { - ra_free(as, r); - ra_modified(as, r); - emit_spload(as, ir+i, r, sps_scale((ir+i)->s)); - } - } - ofs = sps_scale(ir->s & ~1); - } else { - Reg rhi = ra_dest(as, ir+1, RSET_GPR); - Reg rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi)); - emit_tsi(as, MIPSI_LW, rhi, RID_SP, ofs+(LJ_BE?0:4)); - emit_tsi(as, MIPSI_LW, rlo, RID_SP, ofs+(LJ_BE?4:0)); - } - } -#else - RegSet drop = RSET_SCRATCH; - if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ - ra_evictset(as, drop); - ofs = sps_scale(ir->s); -#endif - asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - /* Store the result to the spill slot or temp slots. */ - emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), - RID_SP, ofs); -} - -/* -- Memory references --------------------------------------------------- */ - -#if LJ_64 -/* Store tagged value for ref at base+ofs. */ -static void asm_tvstore64(ASMState *as, Reg base, int32_t ofs, IRRef ref) -{ - RegSet allow = rset_exclude(RSET_GPR, base); - IRIns *ir = IR(ref); - lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t), - "store of IR type %d", irt_type(ir->t)); - if (irref_isk(ref)) { - TValue k; - lj_ir_kvalue(as->J->L, &k, ir); - emit_tsi(as, MIPSI_SD, ra_allock(as, (int64_t)k.u64, allow), base, ofs); - } else { - Reg src = ra_alloc1(as, ref, allow); - Reg type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, - rset_exclude(allow, src)); - emit_tsi(as, MIPSI_SD, RID_TMP, base, ofs); - if (irt_isinteger(ir->t)) { - emit_dst(as, MIPSI_DADDU, RID_TMP, RID_TMP, type); - emit_tsml(as, MIPSI_DEXT, RID_TMP, src, 31, 0); - } else { - emit_dst(as, MIPSI_DADDU, RID_TMP, src, type); - } - } -} -#endif - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode) -{ - int32_t tmpofs = (int32_t)(offsetof(global_State, tmptv)-32768); - if ((mode & IRTMPREF_IN1)) { - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if ((mode & IRTMPREF_OUT1)) { -#if LJ_SOFTFP - emit_tsi(as, MIPSI_AADDIU, dest, RID_JGL, tmpofs); -#if LJ_64 - emit_setgl(as, ra_alloc1(as, ref, RSET_GPR), tmptv.u64); -#else - lj_assertA(irref_isk(ref), "unsplit FP op"); - emit_setgl(as, - ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, RSET_GPR), - tmptv.u32.lo); - emit_setgl(as, - ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, RSET_GPR), - tmptv.u32.hi); -#endif -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_tsi(as, MIPSI_AADDIU, dest, RID_JGL, tmpofs); - emit_tsi(as, MIPSI_SDC1, (src & 31), RID_JGL, tmpofs); -#endif - } else if (irref_isk(ref)) { - /* Use the number constant itself as a TValue. */ - ra_allockreg(as, igcptr(ir_knum(ir)), dest); - } else { -#if LJ_SOFTFP32 - lj_assertA(0, "unsplit FP op"); -#else - /* Otherwise force a spill and use the spill slot. */ - emit_tsi(as, MIPSI_AADDIU, dest, RID_SP, ra_spill(as, ir)); -#endif - } - } else { - /* Otherwise use g->tmptv to hold the TValue. */ -#if LJ_32 - Reg type; - emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, tmpofs); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_GPR); - emit_setgl(as, src, tmptv.gcr); - } - if (LJ_SOFTFP && (ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)) - type = ra_alloc1(as, ref+1, RSET_GPR); - else - type = ra_allock(as, (int32_t)irt_toitype(ir->t), RSET_GPR); - emit_setgl(as, type, tmptv.it); -#else - asm_tvstore64(as, dest, 0, ref); - emit_tsi(as, MIPSI_DADDIU, dest, RID_JGL, tmpofs); -#endif - } - } else { - emit_tsi(as, MIPSI_AADDIU, dest, RID_JGL, tmpofs); - } -} - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_tsi(as, MIPSI_AADDIU, dest, base, ofs); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); -#if !LJ_TARGET_MIPSR6 - emit_dst(as, MIPSI_AADDU, dest, RID_TMP, base); - emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3); -#else - emit_dst(as, MIPSI_ALSA | MIPSF_A(3-1), dest, idx, base); -#endif -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2; -#if LJ_64 - Reg cmp64 = RID_NONE; -#endif - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - int isk = irref_isk(refkey); - IRType1 kt = irkey->t; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - - rset_clear(allow, tab); - if (!LJ_SOFTFP && irt_isnum(kt)) { - key = ra_alloc1(as, refkey, RSET_FPR); - tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); - } else { - if (!irt_ispri(kt)) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } -#if LJ_32 - if (LJ_SOFTFP && irkey[1].o == IR_HIOP) { - if (ra_hasreg((irkey+1)->r)) { - type = tmpnum = (irkey+1)->r; - tmp1 = ra_scratch(as, allow); - rset_clear(allow, tmp1); - ra_noweak(as, tmpnum); - } else { - type = tmpnum = ra_allocref(as, refkey+1, allow); - } - rset_clear(allow, tmpnum); - } else { - type = ra_allock(as, (int32_t)irt_toitype(kt), allow); - rset_clear(allow, type); - } -#endif - } - tmp2 = ra_scratch(as, allow); - rset_clear(allow, tmp2); -#if LJ_64 - if (LJ_SOFTFP || !irt_isnum(kt)) { - /* Allocate cmp64 register used for 64-bit comparisons */ - if (LJ_SOFTFP && irt_isnum(kt)) { - cmp64 = key; - } else if (!isk && irt_isaddr(kt)) { - cmp64 = tmp2; - } else { - int64_t k; - if (isk && irt_isaddr(kt)) { - k = ((int64_t)irt_toitype(kt) << 47) | irkey[1].tv.u64; - } else { - lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type"); - k = ~((int64_t)~irt_toitype(kt) << 47); - } - cmp64 = ra_allock(as, k, allow); - rset_clear(allow, cmp64); - } - } -#endif - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - as->invmcp = NULL; - if (merge == IR_NE) - asm_guard(as, MIPSI_B, RID_ZERO, RID_ZERO); - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - /* Follow hash chain until the end. */ - emit_move(as, dest, tmp1); - l_loop = --as->mcp; - emit_tsi(as, MIPSI_AL, tmp1, dest, (int32_t)offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (merge == IR_EQ) { /* Must match asm_guard(). */ - emit_ti(as, MIPSI_LI, RID_TMP, as->snapno); - l_end = asm_exitstub_addr(as); - } - if (!LJ_SOFTFP && irt_isnum(kt)) { -#if !LJ_TARGET_MIPSR6 - emit_branch(as, MIPSI_BC1T, 0, 0, l_end); - emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key); -#else - emit_branch(as, MIPSI_BC1NEZ, 0, (tmpnum&31), l_end); - emit_fgh(as, MIPSI_CMP_EQ_D, tmpnum, tmpnum, key); -#endif - *--as->mcp = MIPSI_NOP; /* Avoid NaN comparison overhead. */ - emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next); - emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM); -#if LJ_32 - emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n)); - } else { - if (irt_ispri(kt)) { - emit_branch(as, MIPSI_BEQ, tmp1, type, l_end); - } else { - emit_branch(as, MIPSI_BEQ, tmp2, key, l_end); - emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); - emit_branch(as, MIPSI_BNE, tmp1, type, l_next); - } - } - emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it)); - *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu); -#else - emit_dta(as, MIPSI_DSRA32, tmp1, tmp1, 15); - emit_tg(as, MIPSI_DMTC1, tmp1, tmpnum); - emit_tsi(as, MIPSI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64)); - } else { - emit_branch(as, MIPSI_BEQ, tmp1, cmp64, l_end); - emit_tsi(as, MIPSI_LD, tmp1, dest, (int32_t)offsetof(Node, key.u64)); - } - *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu); - if (!isk && irt_isaddr(kt)) { - type = ra_allock(as, (int64_t)irt_toitype(kt) << 47, allow); - emit_dst(as, MIPSI_DADDU, tmp2, key, type); - rset_clear(allow, type); - } -#endif - - /* Load main position relative to tab->node into dest. */ - khash = isk ? ir_khash(as, irkey) : 1; - if (khash == 0) { - emit_tsi(as, MIPSI_AL, dest, tab, (int32_t)offsetof(GCtab, node)); - } else { - Reg tmphash = tmp1; - if (isk) - tmphash = ra_allock(as, khash, allow); - emit_dst(as, MIPSI_AADDU, dest, dest, tmp1); - lj_assertA(sizeof(Node) == 24, "bad Node size"); - emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1); - emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3); - emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5); - emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash); - emit_tsi(as, MIPSI_AL, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); - if (isk) { - /* Nothing to do. */ - } else if (irt_isstr(kt)) { - emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, sid)); - } else { /* Must match with hash*() in lj_tab.c. */ - emit_dst(as, MIPSI_SUBU, tmp1, tmp1, tmp2); - emit_rotr(as, tmp2, tmp2, dest, (-HASH_ROT3)&31); - emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2); - emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31); - emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest); -#if LJ_32 - if (LJ_SOFTFP ? (irkey[1].o == IR_HIOP) : irt_isnum(kt)) { - emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1); - if ((as->flags & JIT_F_MIPSXXR2)) { - emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31); - } else { - emit_dst(as, MIPSI_OR, dest, dest, tmp1); - emit_dta(as, MIPSI_SLL, tmp1, tmp1, HASH_ROT1); - emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31); - } - emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1); -#if LJ_SOFTFP - emit_ds(as, MIPSI_MOVE, tmp1, type); - emit_ds(as, MIPSI_MOVE, tmp2, key); -#else - emit_tg(as, MIPSI_MFC1, tmp2, key); - emit_tg(as, MIPSI_MFC1, tmp1, key+1); -#endif - } else { - emit_dst(as, MIPSI_XOR, tmp2, key, tmp1); - emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31); - emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow)); - } -#else - emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1); - emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31); - if (irt_isnum(kt)) { - emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1); - emit_dta(as, MIPSI_DSRA32, tmp1, LJ_SOFTFP ? key : tmp1, 0); - emit_dta(as, MIPSI_SLL, tmp2, LJ_SOFTFP ? key : tmp1, 0); -#if !LJ_SOFTFP - emit_tg(as, MIPSI_DMFC1, tmp1, key); -#endif - } else { - checkmclim(as); - emit_dta(as, MIPSI_DSRA32, tmp1, tmp1, 0); - emit_dta(as, MIPSI_SLL, tmp2, key, 0); - emit_dst(as, MIPSI_DADDU, tmp1, key, type); - } -#endif - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - RegSet allow = rset_exclude(RSET_GPR, node); - Reg idx = node; -#if LJ_32 - Reg key = RID_NONE, type = RID_TMP; - int32_t lo, hi; -#else - Reg key = ra_scratch(as, allow); - int64_t k; -#endif - lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot"); - if (ofs > 32736) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_tsi(as, MIPSI_AADDIU, dest, node, ofs); - } -#if LJ_32 - if (!irt_ispri(irkey->t)) { - key = ra_scratch(as, allow); - rset_clear(allow, key); - } - if (irt_isnum(irkey->t)) { - lo = (int32_t)ir_knum(irkey)->u32.lo; - hi = (int32_t)ir_knum(irkey)->u32.hi; - } else { - lo = irkey->i; - hi = irt_toitype(irkey->t); - if (!ra_hasreg(key)) - goto nolo; - } - asm_guard(as, MIPSI_BNE, key, lo ? ra_allock(as, lo, allow) : RID_ZERO); -nolo: - asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO); - if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0)); - emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4)); -#else - if (irt_ispri(irkey->t)) { - lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type"); - k = ~((int64_t)~irt_toitype(irkey->t) << 47); - } else if (irt_isnum(irkey->t)) { - k = (int64_t)ir_knum(irkey)->u64; - } else { - k = ((int64_t)irt_toitype(irkey->t) << 47) | (int64_t)ir_kgc(irkey); - } - asm_guard(as, MIPSI_BNE, key, ra_allock(as, k, allow)); - emit_tsi(as, MIPSI_LD, key, idx, kofs); -#endif - if (ofs > 32736) - emit_tsi(as, MIPSI_AADDU, dest, node, ra_allock(as, ofs, allow)); -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, MIPSI_AL, dest, v, RSET_GPR); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_AADDIU, dest, uv, (int32_t)offsetof(GCupval, tv)); - emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_tsi(as, MIPSI_AL, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_tsi(as, MIPSI_AL, uv, func, (int32_t)offsetof(GCfuncL, uvptr) + - (int32_t)sizeof(MRef) * (int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lj_assertA(!ra_used(ir), "unfused FREF"); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ -#if LJ_32 - Reg dest = ra_dest(as, ir, RSET_GPR); - IRRef ref = ir->op2, refk = ir->op1; - int32_t ofs = (int32_t)sizeof(GCstr); - Reg r; - if (irref_isk(ref)) { - IRRef tmp = refk; refk = ref; ref = tmp; - } else if (!irref_isk(refk)) { - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - IRIns *irr = IR(ir->op2); - if (ra_hasreg(irr->r)) { - ra_noweak(as, irr->r); - right = irr->r; - } else if (mayfuse(as, irr->op2) && - irr->o == IR_ADD && irref_isk(irr->op2) && - checki16(ofs + IR(irr->op2)->i)) { - ofs += IR(irr->op2)->i; - right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); - } else { - right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_tsi(as, MIPSI_ADDIU, dest, dest, ofs); - emit_dst(as, MIPSI_ADDU, dest, left, right); - return; - } - r = ra_alloc1(as, ref, RSET_GPR); - ofs += IR(refk)->i; - if (checki16(ofs)) - emit_tsi(as, MIPSI_ADDIU, dest, r, ofs); - else - emit_dst(as, MIPSI_ADDU, dest, r, - ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); -#else - RegSet allow = RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - Reg base = ra_alloc1(as, ir->op1, allow); - IRIns *irr = IR(ir->op2); - int32_t ofs = sizeof(GCstr); - rset_clear(allow, base); - if (irref_isk(ir->op2) && checki16(ofs + irr->i)) { - emit_tsi(as, MIPSI_DADDIU, dest, base, ofs + irr->i); - } else { - emit_tsi(as, MIPSI_DADDIU, dest, dest, ofs); - emit_dst(as, MIPSI_DADDU, dest, base, ra_alloc1(as, ir->op2, allow)); - } -#endif -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static MIPSIns asm_fxloadins(ASMState *as, IRIns *ir) -{ - UNUSED(as); - switch (irt_type(ir->t)) { - case IRT_I8: return MIPSI_LB; - case IRT_U8: return MIPSI_LBU; - case IRT_I16: return MIPSI_LH; - case IRT_U16: return MIPSI_LHU; - case IRT_NUM: - lj_assertA(!LJ_SOFTFP32, "unsplit FP op"); - if (!LJ_SOFTFP) return MIPSI_LDC1; - /* fallthrough */ - case IRT_FLOAT: if (!LJ_SOFTFP) return MIPSI_LWC1; - /* fallthrough */ - default: return (LJ_64 && irt_is64(ir->t)) ? MIPSI_LD : MIPSI_LW; - } -} - -static MIPSIns asm_fxstoreins(ASMState *as, IRIns *ir) -{ - UNUSED(as); - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return MIPSI_SB; - case IRT_I16: case IRT_U16: return MIPSI_SH; - case IRT_NUM: - lj_assertA(!LJ_SOFTFP32, "unsplit FP op"); - if (!LJ_SOFTFP) return MIPSI_SDC1; - /* fallthrough */ - case IRT_FLOAT: if (!LJ_SOFTFP) return MIPSI_SWC1; - /* fallthrough */ - default: return (LJ_64 && irt_is64(ir->t)) ? MIPSI_SD : MIPSI_SW; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - MIPSIns mi = asm_fxloadins(as, ir); - Reg idx; - int32_t ofs; - if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */ - idx = RID_JGL; - ofs = (ir->op2 << 2) - 32768 - GG_OFS(g); - } else { - idx = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_tsi(as, MIPSI_AADDIU, dest, idx, ofs); - return; - } - } - ofs = field_ofs[ir->op2]; - } - lj_assertA(!irt_isfp(ir->t), "bad FP FLOAD"); - emit_tsi(as, mi, dest, idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1z(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - MIPSIns mi = asm_fxstoreins(as, ir); - lj_assertA(!irt_isfp(ir->t), "bad FP FSTORE"); - emit_tsi(as, mi, src, idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - lj_assertA(LJ_TARGET_UNALIGNED || !(ir->op2 & IRXLOAD_UNALIGNED), - "unaligned XLOAD"); - asm_fusexref(as, asm_fxloadins(as, ir), dest, ir->op1, RSET_GPR, 0); -} - -static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1z(as, ir->op2, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(as, ir), src, ir->op1, - rset_exclude(RSET_GPR, src), ofs); - } -} - -#define asm_xstore(as, ir) asm_xstore_(as, ir, 0) - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - int hiop = (LJ_SOFTFP32 && (ir+1)->o == IR_HIOP); - Reg dest = RID_NONE, type = RID_TMP, idx; - RegSet allow = RSET_GPR; - int32_t ofs = 0; - IRType1 t = ir->t; - if (hiop) { - t.irt = IRT_NUM; - if (ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } - } - if (ra_used(ir)) { - lj_assertA((LJ_SOFTFP32 ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t), - "bad load type %d", irt_type(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow); - rset_clear(allow, dest); -#if LJ_64 - if (irt_isaddr(t)) - emit_tsml(as, MIPSI_DEXTM, dest, dest, 14, 0); - else if (irt_isint(t)) - emit_dta(as, MIPSI_SLL, dest, dest, 0); -#endif - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (ir->o == IR_VLOAD) ofs += 8 * ir->op2; - rset_clear(allow, idx); - if (irt_isnum(t)) { - asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM); - } else { - asm_guard(as, MIPSI_BNE, type, - ra_allock(as, (int32_t)irt_toitype(t), allow)); - } -#if LJ_32 - if (ra_hasreg(dest)) { - if (!LJ_SOFTFP && irt_isnum(t)) - emit_hsi(as, MIPSI_LDC1, dest, idx, ofs); - else - emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0)); - } - emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4)); -#else - if (ra_hasreg(dest)) { - if (!LJ_SOFTFP && irt_isnum(t)) { - emit_hsi(as, MIPSI_LDC1, dest, idx, ofs); - dest = type; - } - } else { - dest = type; - } - emit_dta(as, MIPSI_DSRA32, type, dest, 15); - emit_tsi(as, MIPSI_LD, dest, idx, ofs); -#endif -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, type = RID_NONE; - int32_t ofs = 0; - if (ir->r == RID_SINK) - return; - if (!LJ_SOFTFP32 && irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, LJ_SOFTFP ? RSET_GPR : RSET_FPR); - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - emit_hsi(as, LJ_SOFTFP ? MIPSI_SD : MIPSI_SDC1, src, idx, ofs); - } else { -#if LJ_32 - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - } - if (LJ_SOFTFP && (ir+1)->o == IR_HIOP) - type = ra_alloc1(as, (ir+1)->op2, allow); - else - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - rset_clear(allow, type); - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (ra_hasreg(src)) - emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0)); - emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4)); -#else - Reg tmp = RID_TMP; - if (irt_ispri(ir->t)) { - tmp = ra_allock(as, ~((int64_t)~irt_toitype(ir->t) << 47), allow); - rset_clear(allow, tmp); - } else { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - type = ra_allock(as, (int64_t)irt_toitype(ir->t) << 47, allow); - rset_clear(allow, type); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - emit_tsi(as, MIPSI_SD, tmp, idx, ofs); - if (ra_hasreg(src)) { - if (irt_isinteger(ir->t)) { - emit_dst(as, MIPSI_DADDU, tmp, tmp, type); - emit_tsml(as, MIPSI_DEXT, tmp, src, 31, 0); - } else { - emit_dst(as, MIPSI_DADDU, tmp, src, type); - } - } -#endif - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - Reg dest = RID_NONE, type = RID_NONE, base; - RegSet allow = RSET_GPR; - IRType1 t = ir->t; -#if LJ_32 - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - int hiop = (LJ_SOFTFP32 && (ir+1)->o == IR_HIOP); - if (hiop) - t.irt = IRT_NUM; -#else - int32_t ofs = 8*((int32_t)ir->op1-2); -#endif - lj_assertA(!(ir->op2 & IRSLOAD_PARENT), - "bad parent SLOAD"); /* Handled by asm_head_side(). */ - lj_assertA(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK), - "inconsistent SLOAD variant"); -#if LJ_SOFTFP32 - lj_assertA(!(ir->op2 & IRSLOAD_CONVERT), - "unsplit SLOAD convert"); /* Handled by LJ_SOFTFP SPLIT. */ - if (hiop && ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } -#else - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - dest = ra_scratch(as, LJ_SOFTFP ? allow : RSET_FPR); - asm_tointg(as, ir, dest); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ - } else -#endif - if (ra_used(ir)) { - lj_assertA((LJ_SOFTFP32 ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t), - "bad SLOAD type %d", irt_type(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow); - rset_clear(allow, dest); - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); - if (!LJ_SOFTFP32 && (ir->op2 & IRSLOAD_CONVERT)) { - if (irt_isint(t)) { - Reg tmp = ra_scratch(as, LJ_SOFTFP ? RSET_GPR : RSET_FPR); -#if LJ_SOFTFP - ra_evictset(as, rset_exclude(RSET_SCRATCH, dest)); - ra_destreg(as, ir, RID_RET); - emit_call(as, (void *)lj_ir_callinfo[IRCALL_softfp_d2i].func, 0); - if (tmp != REGARG_FIRSTGPR) - emit_move(as, REGARG_FIRSTGPR, tmp); -#else - emit_tg(as, MIPSI_MFC1, dest, tmp); - emit_fg(as, MIPSI_TRUNC_W_D, tmp, tmp); -#endif - dest = tmp; - t.irt = IRT_NUM; /* Check for original type. */ - } else { - Reg tmp = ra_scratch(as, RSET_GPR); -#if LJ_SOFTFP - ra_evictset(as, rset_exclude(RSET_SCRATCH, dest)); - ra_destreg(as, ir, RID_RET); - emit_call(as, (void *)lj_ir_callinfo[IRCALL_softfp_i2d].func, 0); - emit_dta(as, MIPSI_SLL, REGARG_FIRSTGPR, tmp, 0); -#else - emit_fg(as, MIPSI_CVT_D_W, dest, dest); - emit_tg(as, MIPSI_MTC1, tmp, dest); -#endif - dest = tmp; - t.irt = IRT_INT; /* Check for original type. */ - } - } -#if LJ_64 - else if (irt_isaddr(t)) { - /* Clear type from pointers. */ - emit_tsml(as, MIPSI_DEXTM, dest, dest, 14, 0); - } else if (irt_isint(t) && (ir->op2 & IRSLOAD_TYPECHECK)) { - /* Sign-extend integers. */ - emit_dta(as, MIPSI_SLL, dest, dest, 0); - } -#endif - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); -dotypecheck: -#if LJ_32 - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - if (ra_noreg(type)) - type = RID_TMP; - if (irt_isnum(t)) { - asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_SLTIU, RID_TMP, type, (int32_t)LJ_TISNUM); - } else { - Reg ktype = ra_allock(as, (ir->op2 & IRSLOAD_KEYINDEX) ? LJ_KEYINDEX : irt_toitype(t), allow); - asm_guard(as, MIPSI_BNE, type, ktype); - } - } - if (ra_hasreg(dest)) { - if (!LJ_SOFTFP && irt_isnum(t)) - emit_hsi(as, MIPSI_LDC1, dest, base, ofs); - else - emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0)); - } - if (ra_hasreg(type)) - emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4)); -#else - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - type = dest < RID_MAX_GPR ? dest : RID_TMP; - if (irt_ispri(t)) { - asm_guard(as, MIPSI_BNE, type, - ra_allock(as, ~((int64_t)~irt_toitype(t) << 47) , allow)); - } else if ((ir->op2 & IRSLOAD_KEYINDEX)) { - asm_guard(as, MIPSI_BNE, RID_TMP, - ra_allock(as, (int32_t)LJ_KEYINDEX, allow)); - emit_dta(as, MIPSI_DSRA32, RID_TMP, type, 0); - } else { - if (irt_isnum(t)) { - asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM); - if (!LJ_SOFTFP && ra_hasreg(dest)) - emit_hsi(as, MIPSI_LDC1, dest, base, ofs); - } else { - asm_guard(as, MIPSI_BNE, RID_TMP, - ra_allock(as, (int32_t)irt_toitype(t), allow)); - } - emit_dta(as, MIPSI_DSRA32, RID_TMP, type, 15); - } - emit_tsi(as, MIPSI_LD, type, base, ofs); - } else if (ra_hasreg(dest)) { - if (!LJ_SOFTFP && irt_isnum(t)) - emit_hsi(as, MIPSI_LDC1, dest, base, ofs); - else - emit_tsi(as, irt_isint(t) ? MIPSI_LW : MIPSI_LD, dest, base, - ofs ^ ((LJ_BE && irt_isint(t)) ? 4 : 0)); - } -#endif -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID id = (CTypeID)IR(ir->op1)->i; - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[4]; - RegSet drop = RSET_SCRATCH; - lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL), - "bad CNEW/CNEWI operands"); - - as->gcsteps++; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - if (ra_used(ir)) - ra_destreg(as, ir, RID_RET); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); -#if LJ_32 - int32_t ofs = sizeof(GCcdata); - if (sz == 8) { - ofs += 4; - lj_assertA((ir+1)->o == IR_HIOP, "expected HIOP for CNEWI"); - if (LJ_LE) ir++; - } - for (;;) { - Reg r = ra_alloc1z(as, ir->op2, allow); - emit_tsi(as, MIPSI_SW, r, RID_RET, ofs); - rset_clear(allow, r); - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; if (LJ_BE) ir++; else ir--; - } -#else - emit_tsi(as, sz == 8 ? MIPSI_SD : MIPSI_SW, ra_alloc1(as, ir->op2, allow), - RID_RET, sizeof(GCcdata)); -#endif - lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz); - } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */ - ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* CTypeID id */ - args[2] = ir->op2; /* CTSize sz */ - args[3] = ASMREF_TMP1; /* CTSize align */ - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info)); - return; - } - - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); - emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); - emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA); - emit_ti(as, MIPSI_LI, RID_TMP, id); /* Lower 16 bit used. Sign-ext ok. */ - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg link = RID_TMP; - MCLabel l_end = emit_label(as); - emit_tsi(as, MIPSI_AS, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_setgl(as, tab, gc.grayagain); - emit_getgl(as, link, gc.grayagain); - emit_dst(as, MIPSI_XOR, mark, mark, RID_TMP); /* Clear black bit. */ - emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); - emit_tsi(as, MIPSI_ANDI, RID_TMP, mark, LJ_GC_BLACK); - emit_tsi(as, MIPSI_LBU, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type"); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); - emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); - emit_tsi(as, MIPSI_ANDI, tmp, tmp, LJ_GC_BLACK); - emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); - emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_tsi(as, MIPSI_LBU, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_tsi(as, MIPSI_LBU, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - emit_fgh(as, mi, dest, left, right); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_fg(as, mi, dest, left); -} -#endif - -#if !LJ_SOFTFP32 -static void asm_fpmath(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (ir->op2 <= IRFPM_TRUNC) - asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2); - else if (ir->op2 == IRFPM_SQRT) - asm_fpunary(as, ir, MIPSI_SQRT_D); - else -#endif - asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); -} -#endif - -#if !LJ_SOFTFP -#define asm_fpadd(as, ir) asm_fparith(as, ir, MIPSI_ADD_D) -#define asm_fpsub(as, ir) asm_fparith(as, ir, MIPSI_SUB_D) -#define asm_fpmul(as, ir) asm_fparith(as, ir, MIPSI_MUL_D) -#elif LJ_64 /* && LJ_SOFTFP */ -#define asm_fpadd(as, ir) asm_callid(as, ir, IRCALL_softfp_add) -#define asm_fpsub(as, ir) asm_callid(as, ir, IRCALL_softfp_sub) -#define asm_fpmul(as, ir) asm_callid(as, ir, IRCALL_softfp_mul) -#endif - -static void asm_add(ASMState *as, IRIns *ir) -{ - IRType1 t = ir->t; -#if !LJ_SOFTFP32 - if (irt_isnum(t)) { - asm_fpadd(as, ir); - } else -#endif - { - /* TODO MIPSR6: Fuse ADD(BSHL(a,1-4),b) or ADD(ADD(a,a),b) to MIPSI_ALSA. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - intptr_t k = get_kval(as, ir->op2); - if (checki16(k)) { - emit_tsi(as, (LJ_64 && irt_is64(t)) ? MIPSI_DADDIU : MIPSI_ADDIU, dest, - left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dst(as, (LJ_64 && irt_is64(t)) ? MIPSI_DADDU : MIPSI_ADDU, dest, - left, right); - } -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP32 - if (irt_isnum(ir->t)) { - asm_fpsub(as, ir); - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, (LJ_64 && irt_is64(ir->t)) ? MIPSI_DSUBU : MIPSI_SUBU, dest, - left, right); - } -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP32 - if (irt_isnum(ir->t)) { - asm_fpmul(as, ir); - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (LJ_64 && irt_is64(ir->t)) { -#if !LJ_TARGET_MIPSR6 - emit_dst(as, MIPSI_MFLO, dest, 0, 0); - emit_dst(as, MIPSI_DMULT, 0, left, right); -#else - emit_dst(as, MIPSI_DMUL, dest, left, right); -#endif - } else { - emit_dst(as, MIPSI_MUL, dest, left, right); - } - } -} - -#if !LJ_SOFTFP32 -static void asm_fpdiv(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - asm_fparith(as, ir, MIPSI_DIV_D); -#else - asm_callid(as, ir, IRCALL_softfp_div); -#endif -} -#endif - -static void asm_neg(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, MIPSI_NEG_D); - } else -#elif LJ_64 /* && LJ_SOFTFP */ - if (irt_isnum(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_dst(as, MIPSI_XOR, dest, left, - ra_allock(as, 0x8000000000000000ll, rset_exclude(RSET_GPR, dest))); - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_dst(as, (LJ_64 && irt_is64(ir->t)) ? MIPSI_DSUBU : MIPSI_SUBU, dest, - RID_ZERO, left); - } -} - -#if !LJ_SOFTFP -#define asm_abs(as, ir) asm_fpunary(as, ir, MIPSI_ABS_D) -#elif LJ_64 /* && LJ_SOFTFP */ -static void asm_abs(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_tsml(as, MIPSI_DEXTM, dest, left, 30, 0); -} -#endif - -static void asm_arithov(ASMState *as, IRIns *ir) -{ - /* TODO MIPSR6: bovc/bnvc. Caveat: no delay slot to load RID_TMP. */ - Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR); - lj_assertA(!irt_is64(ir->t), "bad usage"); - if (irref_isk(ir->op2)) { - int k = IR(ir->op2)->i; - if (ir->o == IR_SUBOV) k = -k; - if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */ - left = ra_alloc1(as, ir->op1, RSET_GPR); - asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_dst(as, MIPSI_SLT, RID_TMP, dest, dest == left ? RID_TMP : left); - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - if (dest == left) emit_move(as, RID_TMP, left); - return; - } - } - left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left), - right), dest)); - asm_guard(as, MIPSI_BLTZ, RID_TMP, 0); - emit_dst(as, MIPSI_AND, RID_TMP, RID_TMP, tmp); - if (ir->o == IR_ADDOV) { /* ((dest^left) & (dest^right)) < 0 */ - emit_dst(as, MIPSI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right); - } else { /* ((dest^left) & (dest^~right)) < 0 */ - emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, dest); - emit_dst(as, MIPSI_NOR, RID_TMP, dest == right ? RID_TMP : right, RID_ZERO); - } - emit_dst(as, MIPSI_XOR, tmp, dest, dest == left ? RID_TMP : left); - emit_dst(as, ir->o == IR_ADDOV ? MIPSI_ADDU : MIPSI_SUBU, dest, left, right); - if (dest == left || dest == right) - emit_move(as, RID_TMP, dest == left ? left : right); -} - -#define asm_addov(as, ir) asm_arithov(as, ir) -#define asm_subov(as, ir) asm_arithov(as, ir) - -static void asm_mulov(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left), - right), dest)); - asm_guard(as, MIPSI_BNE, RID_TMP, tmp); - emit_dta(as, MIPSI_SRA, RID_TMP, dest, 31); -#if !LJ_TARGET_MIPSR6 - emit_dst(as, MIPSI_MFHI, tmp, 0, 0); - emit_dst(as, MIPSI_MFLO, dest, 0, 0); - emit_dst(as, MIPSI_MULT, 0, left, right); -#else - emit_dst(as, MIPSI_MUL, dest, left, right); - emit_dst(as, MIPSI_MUH, tmp, left, right); -#endif -} - -#if LJ_32 && LJ_HASFFI -static void asm_add64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k == 0) { - emit_dst(as, MIPSI_ADDU, dest, left, RID_TMP); - goto loarith; - } else if (checki16(k)) { - emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP); - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - goto loarith; - } - } - emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP); - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dst(as, MIPSI_ADDU, dest, left, right); -loarith: - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k == 0) { - if (dest != left) - emit_move(as, dest, left); - return; - } else if (checki16(k)) { - if (dest == left) { - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, left)); - emit_move(as, dest, tmp); - dest = tmp; - } - emit_dst(as, MIPSI_SLTU, RID_TMP, dest, left); - emit_tsi(as, MIPSI_ADDIU, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - if (dest == left && dest == right) { - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); - emit_move(as, dest, tmp); - dest = tmp; - } - emit_dst(as, MIPSI_SLTU, RID_TMP, dest, dest == left ? right : left); - emit_dst(as, MIPSI_ADDU, dest, left, right); -} - -static void asm_sub64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP); - emit_dst(as, MIPSI_SUBU, dest, left, right); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (dest == left) { - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); - emit_move(as, dest, tmp); - dest = tmp; - } - emit_dst(as, MIPSI_SLTU, RID_TMP, left, dest); - emit_dst(as, MIPSI_SUBU, dest, left, right); -} - -static void asm_neg64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP); - emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_dst(as, MIPSI_SLTU, RID_TMP, RID_ZERO, dest); - emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); -} -#endif - -static void asm_bnot(ASMState *as, IRIns *ir) -{ - Reg left, right, dest = ra_dest(as, ir, RSET_GPR); - IRIns *irl = IR(ir->op1); - if (mayfuse(as, ir->op1) && irl->o == IR_BOR) { - left = ra_alloc2(as, irl, RSET_GPR); - right = (left >> 8); left &= 255; - } else { - left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - right = RID_ZERO; - } - emit_dst(as, MIPSI_NOR, dest, left, right); -} - -static void asm_bswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); -#if LJ_32 - if ((as->flags & JIT_F_MIPSXXR2)) { - emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16); - emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left); - } else { - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), dest)); - emit_dst(as, MIPSI_OR, dest, dest, tmp); - emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); - emit_tsi(as, MIPSI_ANDI, dest, dest, 0xff00); - emit_dta(as, MIPSI_SLL, RID_TMP, RID_TMP, 8); - emit_dta(as, MIPSI_SRL, dest, left, 8); - emit_tsi(as, MIPSI_ANDI, RID_TMP, left, 0xff00); - emit_dst(as, MIPSI_OR, tmp, tmp, RID_TMP); - emit_dta(as, MIPSI_SRL, tmp, left, 24); - emit_dta(as, MIPSI_SLL, RID_TMP, left, 24); - } -#else - if (irt_is64(ir->t)) { - emit_dst(as, MIPSI_DSHD, dest, 0, RID_TMP); - emit_dst(as, MIPSI_DSBH, RID_TMP, 0, left); - } else { - emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16); - emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left); - } -#endif -} - -static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - intptr_t k = get_kval(as, ir->op2); - if (checku16(k)) { - emit_tsi(as, mik, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_dst(as, mi, dest, left, right); -} - -#define asm_band(as, ir) asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI) -#define asm_bor(as, ir) asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI) -#define asm_bxor(as, ir) asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI) - -static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { /* Constant shifts. */ - uint32_t shift = (uint32_t)IR(ir->op2)->i; - if (LJ_64 && irt_is64(ir->t)) mik |= (shift & 32) ? MIPSI_D32 : MIPSI_D; - emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), - (shift & 31)); - } else { - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (LJ_64 && irt_is64(ir->t)) mi |= MIPSI_DV; - emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */ - } -} - -#define asm_bshl(as, ir) asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL) -#define asm_bshr(as, ir) asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL) -#define asm_bsar(as, ir) asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA) -#define asm_brol(as, ir) lj_assertA(0, "unexpected BROL") - -static void asm_bror(ASMState *as, IRIns *ir) -{ - if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) { - asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { /* Constant shifts. */ - uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_rotr(as, dest, left, RID_TMP, shift); - } else { - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); - emit_dst(as, MIPSI_SRLV, dest, right, left); - emit_dst(as, MIPSI_SLLV, RID_TMP, RID_TMP, left); - emit_dst(as, MIPSI_SUBU, RID_TMP, ra_allock(as, 32, RSET_GPR), right); - } - } -} - -#if LJ_SOFTFP -static void asm_sfpmin_max(ASMState *as, IRIns *ir) -{ - CCallInfo ci = lj_ir_callinfo[(IROp)ir->o == IR_MIN ? IRCALL_lj_vm_sfmin : IRCALL_lj_vm_sfmax]; -#if LJ_64 - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; -#else - IRRef args[4]; - args[0^LJ_BE] = ir->op1; - args[1^LJ_BE] = (ir+1)->op1; - args[2^LJ_BE] = ir->op2; - args[3^LJ_BE] = (ir+1)->op2; -#endif - asm_setupresult(as, ir, &ci); - emit_call(as, (void *)ci.func, 0); - ci.func = NULL; - asm_gencall(as, &ci, args); -} -#endif - -static void asm_min_max(ASMState *as, IRIns *ir, int ismax) -{ - if (!LJ_SOFTFP32 && irt_isnum(ir->t)) { -#if LJ_SOFTFP - asm_sfpmin_max(as, ir); -#else - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; -#if !LJ_TARGET_MIPSR6 - if (dest == left) { - emit_fg(as, MIPSI_MOVF_D, dest, right); - } else { - emit_fg(as, MIPSI_MOVT_D, dest, left); - if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right); - } - emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? right : left, ismax ? left : right); -#else - emit_fgh(as, ismax ? MIPSI_MAX_D : MIPSI_MIN_D, dest, left, right); -#endif -#endif - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (left == right) { - if (dest != left) emit_move(as, dest, left); - } else { -#if !LJ_TARGET_MIPSR6 - if (dest == left) { - emit_dst(as, MIPSI_MOVN, dest, right, RID_TMP); - } else { - emit_dst(as, MIPSI_MOVZ, dest, left, RID_TMP); - if (dest != right) emit_move(as, dest, right); - } -#else - emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); - if (dest != right) { - emit_dst(as, MIPSI_SELNEZ, RID_TMP, right, RID_TMP); - emit_dst(as, MIPSI_SELEQZ, dest, left, RID_TMP); - } else { - emit_dst(as, MIPSI_SELEQZ, RID_TMP, left, RID_TMP); - emit_dst(as, MIPSI_SELNEZ, dest, right, RID_TMP); - } -#endif - emit_dst(as, MIPSI_SLT, RID_TMP, - ismax ? left : right, ismax ? right : left); - } - } -} - -#define asm_min(as, ir) asm_min_max(as, ir, 0) -#define asm_max(as, ir) asm_min_max(as, ir, 1) - -/* -- Comparisons --------------------------------------------------------- */ - -#if LJ_SOFTFP -/* SFP comparisons. */ -static void asm_sfpcomp(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; - RegSet drop = RSET_SCRATCH; - Reg r; -#if LJ_64 - IRRef args[2]; - args[0] = ir->op1; - args[1] = ir->op2; -#else - IRRef args[4]; - args[LJ_LE ? 0 : 1] = ir->op1; args[LJ_LE ? 1 : 0] = (ir+1)->op1; - args[LJ_LE ? 2 : 3] = ir->op2; args[LJ_LE ? 3 : 2] = (ir+1)->op2; -#endif - - for (r = REGARG_FIRSTGPR; r <= REGARG_FIRSTGPR+(LJ_64?1:3); r++) { - if (!rset_test(as->freeset, r) && - regcost_ref(as->cost[r]) == args[r-REGARG_FIRSTGPR]) - rset_clear(drop, r); - } - ra_evictset(as, drop); - - asm_setupresult(as, ir, ci); - - switch ((IROp)ir->o) { - case IR_LT: - asm_guard(as, MIPSI_BGEZ, RID_RET, 0); - break; - case IR_ULT: - asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP); - emit_loadi(as, RID_TMP, 1); - asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); - break; - case IR_GE: - asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP); - emit_loadi(as, RID_TMP, 2); - asm_guard(as, MIPSI_BLTZ, RID_RET, 0); - break; - case IR_LE: - asm_guard(as, MIPSI_BGTZ, RID_RET, 0); - break; - case IR_GT: - asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP); - emit_loadi(as, RID_TMP, 2); - asm_guard(as, MIPSI_BLEZ, RID_RET, 0); - break; - case IR_UGE: - asm_guard(as, MIPSI_BLTZ, RID_RET, 0); - break; - case IR_ULE: - asm_guard(as, MIPSI_BEQ, RID_RET, RID_TMP); - emit_loadi(as, RID_TMP, 1); - break; - case IR_UGT: case IR_ABC: - asm_guard(as, MIPSI_BLEZ, RID_RET, 0); - break; - case IR_EQ: case IR_NE: - asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_RET, RID_ZERO); - default: - break; - } - asm_gencall(as, ci, args); -} -#endif - -static void asm_comp(ASMState *as, IRIns *ir) -{ - /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ - IROp op = ir->o; - if (!LJ_SOFTFP32 && irt_isnum(ir->t)) { -#if LJ_SOFTFP - asm_sfpcomp(as, ir); -#else -#if !LJ_TARGET_MIPSR6 - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); - emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right); -#else - Reg tmp, right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_FPR, left), right)); - asm_guard(as, (op&1) ? MIPSI_BC1NEZ : MIPSI_BC1EQZ, 0, (tmp&31)); - emit_fgh(as, MIPSI_CMP_LT_D + ((op&3) ^ ((op>>2)&1)), tmp, left, right); -#endif -#endif - } else { - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - if (op == IR_ABC) op = IR_UGT; - if ((op&4) == 0 && irref_isk(ir->op2) && get_kval(as, ir->op2) == 0) { - MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) : - ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ); - asm_guard(as, mi, left, 0); - } else { - if (irref_isk(ir->op2)) { - intptr_t k = get_kval(as, ir->op2); - if ((op&2)) k++; - if (checki16(k)) { - asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_tsi(as, (op&4) ? MIPSI_SLTIU : MIPSI_SLTI, - RID_TMP, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, - RID_TMP, (op&2) ? right : left, (op&2) ? left : right); - } - } -} - -static void asm_equal(ASMState *as, IRIns *ir) -{ - Reg right, left = ra_alloc2(as, ir, (!LJ_SOFTFP && irt_isnum(ir->t)) ? - RSET_FPR : RSET_GPR); - right = (left >> 8); left &= 255; - if (!LJ_SOFTFP32 && irt_isnum(ir->t)) { -#if LJ_SOFTFP - asm_sfpcomp(as, ir); -#elif !LJ_TARGET_MIPSR6 - asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); - emit_fgh(as, MIPSI_C_EQ_D, 0, left, right); -#else - Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_FPR, left), right)); - asm_guard(as, (ir->o & 1) ? MIPSI_BC1NEZ : MIPSI_BC1EQZ, 0, (tmp&31)); - emit_fgh(as, MIPSI_CMP_EQ_D, tmp, left, right); -#endif - } else { - asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right); - } -} - -#if LJ_32 && LJ_HASFFI -/* 64 bit integer comparisons. */ -static void asm_comp64(ASMState *as, IRIns *ir) -{ - /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ - IROp op = (ir-1)->o; - MCLabel l_end; - Reg rightlo, leftlo, righthi, lefthi = ra_alloc2(as, ir, RSET_GPR); - righthi = (lefthi >> 8); lefthi &= 255; - leftlo = ra_alloc2(as, ir-1, - rset_exclude(rset_exclude(RSET_GPR, lefthi), righthi)); - rightlo = (leftlo >> 8); leftlo &= 255; - asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); - l_end = emit_label(as); - if (lefthi != righthi) - emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, RID_TMP, - (op&2) ? righthi : lefthi, (op&2) ? lefthi : righthi); - emit_dst(as, MIPSI_SLTU, RID_TMP, - (op&2) ? rightlo : leftlo, (op&2) ? leftlo : rightlo); - if (lefthi != righthi) - emit_branch(as, MIPSI_BEQ, lefthi, righthi, l_end); -} - -static void asm_comp64eq(ASMState *as, IRIns *ir) -{ - Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - asm_guard(as, ((ir-1)->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO); - tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); - emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp); - emit_dst(as, MIPSI_XOR, tmp, left, right); - left = ra_alloc2(as, ir-1, RSET_GPR); - right = (left >> 8); left &= 255; - emit_dst(as, MIPSI_XOR, RID_TMP, left, right); -} -#endif - -/* -- Split register ops -------------------------------------------------- */ - -/* Hiword op of a split 32/32 or 64/64 bit op. Previous op is the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; -#if LJ_32 && (LJ_HASFFI || LJ_SOFTFP) - if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ - as->curins--; /* Always skip the CONV. */ -#if LJ_HASFFI && !LJ_SOFTFP - if (usehi || uselo) - asm_conv64(as, ir); - return; -#endif - } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ -#if LJ_SOFTFP - if (!irt_isint(ir->t)) { - asm_sfpcomp(as, ir-1); - return; - } -#endif -#if LJ_HASFFI - asm_comp64(as, ir); -#endif - return; - } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ -#if LJ_SOFTFP - if (!irt_isint(ir->t)) { - asm_sfpcomp(as, ir-1); - return; - } -#endif -#if LJ_HASFFI - asm_comp64eq(as, ir); -#endif - return; -#if LJ_SOFTFP - } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { - as->curins--; /* Always skip the loword min/max. */ - if (uselo || usehi) - asm_sfpmin_max(as, ir-1); - return; -#endif - } else if ((ir-1)->o == IR_XSTORE) { - as->curins--; /* Handle both stores here. */ - if ((ir-1)->r != RID_SINK) { - asm_xstore_(as, ir, LJ_LE ? 4 : 0); - asm_xstore_(as, ir-1, LJ_LE ? 0 : 4); - } - return; - } -#endif - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { -#if LJ_32 && LJ_HASFFI - case IR_ADD: as->curins--; asm_add64(as, ir); break; - case IR_SUB: as->curins--; asm_sub64(as, ir); break; - case IR_NEG: as->curins--; asm_neg64(as, ir); break; - case IR_CNEWI: - /* Nothing to do here. Handled by lo op itself. */ - break; -#endif -#if LJ_32 && LJ_SOFTFP - case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - case IR_STRTO: - if (!uselo) - ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */ - break; - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: case IR_TMPREF: - /* Nothing to do here. Handled by lo op itself. */ - break; -#endif - case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break; - } -} - -/* -- Profiling ----------------------------------------------------------- */ - -static void asm_prof(ASMState *as, IRIns *ir) -{ - UNUSED(ir); - asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO); - emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, HOOK_PROFILE); - emit_lsglptr(as, MIPSI_LBU, RID_TMP, - (int32_t)offsetof(global_State, hookmask)); -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ - Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; - ExitNo oldsnap = as->snapno; - rset_clear(allow, pbase); -#if LJ_32 - tmp = allow ? rset_pickbot(allow) : - (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); -#else - tmp = allow ? rset_pickbot(allow) : RID_RET; -#endif - as->snapno = exitno; - asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO); - as->snapno = oldsnap; - if (allow == RSET_EMPTY) /* Restore temp. register. */ - emit_tsi(as, MIPSI_AL, tmp, RID_SP, 0); - else - ra_modified(as, tmp); - emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot)); - emit_dst(as, MIPSI_ASUBU, RID_TMP, tmp, pbase); - emit_tsi(as, MIPSI_AL, tmp, tmp, offsetof(lua_State, maxstack)); - if (pbase == RID_TMP) - emit_getgl(as, RID_TMP, jit_base); - emit_getgl(as, tmp, cur_L); - if (allow == RSET_EMPTY) /* Spill temp. register. */ - emit_tsi(as, MIPSI_AS, tmp, RID_SP, 0); -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; -#if LJ_32 || defined(LUA_USE_ASSERT) - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2]; -#endif - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1-LJ_FR2); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { -#if LJ_SOFTFP32 - Reg tmp; - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - /* LJ_SOFTFP: must be a number constant. */ - lj_assertA(irref_isk(ref), "unsplit FP op"); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, allow); - emit_tsi(as, MIPSI_SW, tmp, RID_BASE, ofs+(LJ_BE?4:0)); - if (rset_test(as->freeset, tmp+1)) allow = RID2RSET(tmp+1); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, allow); - emit_tsi(as, MIPSI_SW, tmp, RID_BASE, ofs+(LJ_BE?0:4)); -#elif LJ_SOFTFP /* && LJ_64 */ - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE)); - emit_tsi(as, MIPSI_SD, src, RID_BASE, ofs); -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs); -#endif - } else { -#if LJ_32 - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - Reg type; - lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t), - "restore of IR type %d", irt_type(ir->t)); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - rset_clear(allow, src); - emit_tsi(as, MIPSI_SW, src, RID_BASE, ofs+(LJ_BE?4:0)); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s == 0) continue; /* Do not overwrite link to previous frame. */ - type = ra_allock(as, (int32_t)(*flinks--), allow); -#if LJ_SOFTFP - } else if ((sn & SNAP_SOFTFPNUM)) { - type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPR, RID_BASE)); -#endif - } else if ((sn & SNAP_KEYINDEX)) { - type = ra_allock(as, (int32_t)LJ_KEYINDEX, allow); - } else { - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - } - emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4)); -#else - if ((sn & SNAP_KEYINDEX)) { - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - int64_t kki = (int64_t)LJ_KEYINDEX << 32; - if (irref_isk(ref)) { - emit_tsi(as, MIPSI_SD, - ra_allock(as, kki | (int64_t)(uint32_t)ir->i, allow), - RID_BASE, ofs); - } else { - Reg src = ra_alloc1(as, ref, allow); - Reg rki = ra_allock(as, kki, rset_exclude(allow, src)); - emit_tsi(as, MIPSI_SD, RID_TMP, RID_BASE, ofs); - emit_dst(as, MIPSI_DADDU, RID_TMP, src, rki); - } - } else { - asm_tvstore64(as, RID_BASE, ofs, ref); - } -#endif - } - checkmclim(as); - } - lj_assertA(map + nent == flinks, "inconsistent frames in snapshot"); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Marker to prevent patching the GC check exit. */ -#define MIPS_NOPATCH_GC_CHECK MIPSI_OR - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - /* Assumes asm_snap_prep() already done. */ - asm_guard(as, MIPSI_BNE, RID_RET, RID_ZERO); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - l_end[-3] = MIPS_NOPATCH_GC_CHECK; /* Replace the nop after the call. */ - emit_tsi(as, MIPSI_AADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - tmp = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_branch(as, MIPSI_BNE, RID_TMP, RID_ZERO, l_end); - emit_dst(as, MIPSI_SLTU, RID_TMP, RID_TMP, tmp); - emit_getgl(as, tmp, gc.threshold); - emit_getgl(as, RID_TMP, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - p[-1] = MIPSI_NOP; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guard already inverted the cond branch. Only patch the target. */ - p[-3] |= ((target-p+2) & 0x0000ffffu); - } else { - p[-2] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); - } -} - -/* Fixup the tail of the loop. */ -static void asm_loop_tail_fixup(ASMState *as) -{ - if (as->loopinv) as->mctop--; -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r) || irt_ismarked(ir->t)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (r != RID_BASE) - emit_move(as, r, RID_BASE); - } -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r) || irt_ismarked(ir->t)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ - } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); - emit_move(as, r, irp->r); /* Move from coalesced parent reg. */ - } else { - emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ - } - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp; - int32_t spadj = as->T->spadjust; - MCode *p = as->mctop-1; - *p = spadj ? (MIPSI_AADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP; - p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - as->mcp = as->mctop-2; /* Leave room for branch plus nop or stack adj. */ - as->invmcp = as->loopref ? as->mcp : NULL; -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = CCI_XNARGS(ci); -#if LJ_32 - int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; -#else - int nslots = 0, ngpr = REGARG_NUMGPR; -#endif - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) { -#if LJ_32 - if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t) && - nfpr > 0 && !(ci->flags & CCI_VARARG)) { - nfpr--; - ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1; - } else if (!LJ_SOFTFP && args[i] && irt_isnum(IR(args[i])->t)) { - nfpr = 0; - ngpr = ngpr & ~1; - if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1; - } else { - nfpr = 0; - if (ngpr > 0) ngpr--; else nslots++; - } -#else - if (ngpr > 0) ngpr--; else nslots += 2; -#endif - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - asm_sparejump_setup(as); - asm_exitstub_setup(as); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *px = exitstub_trace_addr(T, exitno); - MCode *cstart = NULL, *cstop = NULL; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MCode exitload = MIPSI_LI | MIPSF_T(RID_TMP) | exitno; - MCode tjump = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); - for (p++; p < pe; p++) { - if (*p == exitload) { /* Look for load of exit number. */ - /* Look for exitstub branch. Yes, this covers all used branch variants. */ - if (((p[-1] ^ (px-p)) & 0xffffu) == 0 && - ((p[-1] & 0xf0000000u) == MIPSI_BEQ || - (p[-1] & 0xfc1e0000u) == MIPSI_BLTZ || -#if !LJ_TARGET_MIPSR6 - (p[-1] & 0xffe00000u) == MIPSI_BC1F -#else - (p[-1] & 0xff600000u) == MIPSI_BC1EQZ -#endif - ) && p[-2] != MIPS_NOPATCH_GC_CHECK) { - ptrdiff_t delta = target - p; - if (((delta + 0x8000) >> 16) == 0) { /* Patch in-range branch. */ - patchbranch: - p[-1] = (p[-1] & 0xffff0000u) | (delta & 0xffffu); - *p = MIPSI_NOP; /* Replace the load of the exit number. */ - cstop = p+1; - if (!cstart) cstart = p-1; - } else { /* Branch out of range. Use spare jump slot in mcarea. */ - MCode *mcjump = asm_sparejump_use(mcarea, tjump); - if (mcjump) { - lj_mcode_sync(mcjump, mcjump+1); - delta = mcjump - p; - if (((delta + 0x8000) >> 16) == 0) { - goto patchbranch; - } else { - lj_assertJ(0, "spare jump out of range: -Osizemcode too big"); - } - } - /* Ignore jump slot overflow. Child trace is simply not attached. */ - } - } else if (p+1 == pe) { - /* Patch NOP after code for inverted loop branch. Use of J is ok. */ - lj_assertJ(p[1] == MIPSI_NOP, "expected NOP"); - p[1] = tjump; - *p = MIPSI_NOP; /* Replace the load of the exit number. */ - cstop = p+2; - if (!cstart) cstart = p+1; - } - } - } - if (cstart) lj_mcode_sync(cstart, cstop); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_ppc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_ppc.h deleted file mode 100644 index aa81874..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_ppc.h +++ /dev/null @@ -1,2326 +0,0 @@ -/* -** PPC IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Register allocator extensions --------------------------------------- */ - -/* Allocate a register with a hint. */ -static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) -{ - Reg r = IR(ref)->r; - if (ra_noreg(r)) { - if (!ra_hashint(r) && !iscrossref(as, ref)) - ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ - r = ra_allocref(as, ref, allow); - } - ra_noweak(as, r); - return r; -} - -/* Allocate two source registers for three-operand instructions. */ -static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - Reg left = irl->r, right = irr->r; - if (ra_hasreg(left)) { - ra_noweak(as, left); - if (ra_noreg(right)) - right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); - else - ra_noweak(as, right); - } else if (ra_hasreg(right)) { - ra_noweak(as, right); - left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); - } else if (ra_hashint(right)) { - right = ra_allocref(as, ir->op2, allow); - left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); - } else { - left = ra_allocref(as, ir->op1, allow); - right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); - } - return left | (right << 8); -} - -/* -- Guard handling ------------------------------------------------------ */ - -/* Setup exit stubs after the end of each trace. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - MCode *mxp = as->mctop; - if (mxp - (nexits + 3 + MCLIM_REDZONE) < as->mclim) - asm_mclimit(as); - /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */ - for (i = nexits-1; (int32_t)i >= 0; i--) - *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2); - *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno; /* Read by exit handler. */ - mxp--; - *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2); - *--mxp = PPCI_MFLR|PPCF_T(RID_TMP); - as->mctop = mxp; -} - -static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno) -{ - /* Keep this in-sync with exitstub_trace_addr(). */ - return as->mctop + exitno + 3; -} - -/* Emit conditional branch to exit for guard. */ -static void asm_guardcc(ASMState *as, PPCCC cc) -{ - MCode *target = asm_exitstub_addr(as, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2); - emit_condbranch(as, PPCI_BC, cc^4, p); - return; - } - emit_condbranch(as, PPCI_BC, cc, target); -} - -/* -- Operand fusion ------------------------------------------------------ */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if there's no conflicting instruction between curins and ref. */ -static int noconflict(ASMState *as, IRRef ref, IROp conflict) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - return 1; /* Ok, no conflict. */ -} - -/* Fuse the array base of colocated arrays. */ -static int32_t asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) - return (int32_t)sizeof(GCtab); - return 0; -} - -/* Indicates load/store indexed is ok. */ -#define AHUREF_LSX ((int32_t)0x80000000) - -/* Fuse array/hash/upvalue reference into register+offset operand. */ -static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - if (ir->o == IR_AREF) { - if (mayfuse(as, ref)) { - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, refa, allow); - } - } - if (*ofsp == AHUREF_LSX) { - Reg base = ra_alloc1(as, ir->op1, allow); - Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - return base | (idx << 8); - } - } - } else if (ir->o == IR_HREFK) { - if (mayfuse(as, ref)) { - int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - if (checki16(ofs)) { - *ofsp = ofs; - return ra_alloc1(as, ir->op1, allow); - } - } - } else if (ir->o == IR_UREFC) { - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); - int32_t jgl = (intptr_t)J2G(as->J); - if ((uint32_t)(ofs-jgl) < 65536) { - *ofsp = ofs-jgl-32768; - return RID_JGL; - } else { - *ofsp = (int16_t)ofs; - return ra_allock(as, ofs-(int16_t)ofs, allow); - } - } - } else if (ir->o == IR_TMPREF) { - *ofsp = (int32_t)(offsetof(global_State, tmptv)-32768); - return RID_JGL; - } - } - *ofsp = 0; - return ra_alloc1(as, ref, allow); -} - -/* Fuse XLOAD/XSTORE reference into load/store operand. */ -static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref, - RegSet allow, int32_t ofs) -{ - IRIns *ir = IR(ref); - Reg base; - if (ra_noreg(ir->r) && canfuse(as, ir)) { - if (ir->o == IR_ADD) { - int32_t ofs2; - if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) { - ofs = ofs2; - ref = ir->op1; - } else if (ofs == 0) { - Reg right, left = ra_alloc2(as, ir, allow); - right = (left >> 8); left &= 255; - emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); - return; - } - } else if (ir->o == IR_STRREF) { - lj_assertA(ofs == 0, "bad usage"); - ofs = (int32_t)sizeof(GCstr); - if (irref_isk(ir->op2)) { - ofs += IR(ir->op2)->i; - ref = ir->op1; - } else if (irref_isk(ir->op1)) { - ofs += IR(ir->op1)->i; - ref = ir->op2; - } else { - /* NYI: Fuse ADD with constant. */ - Reg tmp, right, left = ra_alloc2(as, ir, allow); - right = (left >> 8); left &= 255; - tmp = ra_scratch(as, rset_exclude(rset_exclude(allow, left), right)); - emit_fai(as, pi, rt, tmp, ofs); - emit_tab(as, PPCI_ADD, tmp, left, right); - return; - } - if (!checki16(ofs)) { - Reg left = ra_alloc1(as, ref, allow); - Reg right = ra_allock(as, ofs, rset_exclude(allow, left)); - emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); - return; - } - } - } - base = ra_alloc1(as, ref, allow); - emit_fai(as, pi, rt, base, ofs); -} - -/* Fuse XLOAD/XSTORE reference into indexed-only load/store operand. */ -static void asm_fusexrefx(ASMState *as, PPCIns pi, Reg rt, IRRef ref, - RegSet allow) -{ - IRIns *ira = IR(ref); - Reg right, left; - if (canfuse(as, ira) && ira->o == IR_ADD && ra_noreg(ira->r)) { - left = ra_alloc2(as, ira, allow); - right = (left >> 8); left &= 255; - } else { - right = ra_alloc1(as, ref, allow); - left = RID_R0; - } - emit_tab(as, pi, rt, left, right); -} - -#if !LJ_SOFTFP -/* Fuse to multiply-add/sub instruction. */ -static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir) -{ - IRRef lref = ir->op1, rref = ir->op2; - IRIns *irm; - if ((as->flags & JIT_F_OPT_FMA) && - lref != rref && - ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && - ra_noreg(irm->r)) || - (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && - (rref = lref, pi = pir, ra_noreg(irm->r))))) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg add = ra_alloc1(as, rref, RSET_FPR); - Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add)); - right = (left >> 8); left &= 255; - emit_facb(as, pi, dest, left, right, add); - return 1; - } - return 0; -} -#endif - -/* -- Calls --------------------------------------------------------------- */ - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_XNARGS(ci); - int32_t ofs = 8; - Reg gpr = REGARG_FIRSTGPR; -#if !LJ_SOFTFP - Reg fpr = REGARG_FIRSTFPR; -#endif - if ((void *)ci->func) - emit_call(as, (void *)ci->func); - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - if (ref) { - IRIns *ir = IR(ref); -#if !LJ_SOFTFP - if (irt_isfp(ir->t)) { - if (fpr <= REGARG_LASTFPR) { - lj_assertA(rset_test(as->freeset, fpr), - "reg %d not free", fpr); /* Already evicted. */ - ra_leftov(as, fpr, ref); - fpr++; - } else { - Reg r = ra_alloc1(as, ref, RSET_FPR); - if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; - emit_spstore(as, ir, r, ofs); - ofs += irt_isnum(ir->t) ? 8 : 4; - } - } else -#endif - { - if (gpr <= REGARG_LASTGPR) { - lj_assertA(rset_test(as->freeset, gpr), - "reg %d not free", gpr); /* Already evicted. */ - ra_leftov(as, gpr, ref); - gpr++; - } else { - Reg r = ra_alloc1(as, ref, RSET_GPR); - emit_spstore(as, ir, r, ofs); - ofs += 4; - } - } - } else { - if (gpr <= REGARG_LASTGPR) - gpr++; - else - ofs += 4; - } - checkmclim(as); - } -#if !LJ_SOFTFP - if ((ci->flags & CCI_VARARG)) /* Vararg calls need to know about FPR use. */ - emit_tab(as, fpr == REGARG_FIRSTFPR ? PPCI_CRXOR : PPCI_CREQV, 6, 6, 6); -#endif -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)); -#if !LJ_SOFTFP - if ((ci->flags & CCI_NOFPRCLOBBER)) - drop &= ~RSET_FPR; -#endif - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - lj_assertA(!irt_ispri(ir->t), "PRI dest"); - if (!LJ_SOFTFP && irt_isfp(ir->t)) { - if ((ci->flags & CCI_CASTU64)) { - /* Use spill slot or temp slots. */ - int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_fai(as, PPCI_LFD, dest, RID_SP, ofs); - } - emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs); - emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4); - } else { - ra_destreg(as, ir, RID_FPRET); - } - } else if (hiop) { - ra_destpair(as, ir); - } else { - ra_destreg(as, ir, RID_RET); - } - } -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - if (irref_isk(func)) { /* Call to constant address. */ - ci.func = (ASMFunction)(void *)(intptr_t)(irf->i); - } else { /* Need a non-argument register for indirect calls. */ - RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); - Reg freg = ra_alloc1(as, func, allow); - *--as->mcp = PPCI_BCTRL; - *--as->mcp = PPCI_MTCTR | PPCF_T(freg); - ci.func = (ASMFunction)(void *)0; - } - asm_gencall(as, &ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */ - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); - emit_ab(as, PPCI_CMPW, RID_TMP, - ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); - emit_tai(as, PPCI_LWZ, RID_TMP, base, -8); -} - -/* -- Buffer operations --------------------------------------------------- */ - -#if LJ_HASBUFFER -static void asm_bufhdr_write(ASMState *as, Reg sb) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); - IRIns irgc; - irgc.ot = IRT(0, IRT_PGC); /* GC type. */ - emit_storeofs(as, &irgc, RID_TMP, sb, offsetof(SBuf, L)); - emit_rot(as, PPCI_RLWIMI, RID_TMP, tmp, 0, 31-lj_fls(SBUF_MASK_FLAG), 31); - emit_getgl(as, RID_TMP, cur_L); - emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L)); -} -#endif - -/* -- Type conversions ---------------------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - RegSet allow = RSET_FPR; - Reg tmp = ra_scratch(as, rset_clear(allow, left)); - Reg fbias = ra_scratch(as, rset_clear(allow, tmp)); - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest)); - asm_guardcc(as, CC_NE); - emit_fab(as, PPCI_FCMPU, 0, tmp, left); - emit_fab(as, PPCI_FSUB, tmp, tmp, fbias); - emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP); - emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO); - emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); - emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000); - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - emit_lsptr(as, PPCI_LFS, (fbias & 31), - (void *)&as->J->k32[LJ_K32_2P52_2P31], RSET_GPR); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, tmp, left); -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_FPR; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, allow); - Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); - Reg tmp = ra_scratch(as, rset_clear(allow, right)); - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fab(as, PPCI_FADD, tmp, left, right); -} -#endif - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if !LJ_SOFTFP - int stfp = (st == IRT_NUM || st == IRT_FLOAT); -#endif - IRRef lref = ir->op1; - /* 64 bit integer conversions are handled by SPLIT. */ - lj_assertA(!(irt_isint64(ir->t) || (st == IRT_I64 || st == IRT_U64)), - "IR %04d has unsplit 64 bit type", - (int)(ir - as->ir) - REF_BIAS); -#if LJ_SOFTFP - /* FP conversions are handled by SPLIT. */ - lj_assertA(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT), - "IR %04d has FP type", - (int)(ir - as->ir) - REF_BIAS); - /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */ -#else - lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV"); - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - if (st == IRT_NUM) /* double -> float conversion. */ - emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR)); - else /* float -> double conversion is a no-op on PPC. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else { /* Integer to FP conversion. */ - /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */ - /* IRT_U32: Bias with 2^52, subtract 2^52. */ - RegSet allow = RSET_GPR; - Reg left = ra_alloc1(as, lref, allow); - Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left)); - Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest); - emit_fab(as, PPCI_FSUB, dest, dest, fbias); - emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); - emit_lsptr(as, PPCI_LFS, (fbias & 31), - &as->J->k32[st == IRT_U32 ? LJ_K32_2P52 : LJ_K32_2P52_2P31], - rset_clear(allow, hibias)); - emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP, - RID_SP, SPOFS_TMPLO); - emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); - if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000); - } - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lj_assertA(irt_isint(ir->t) && st == IRT_NUM, - "bad type for checked CONV"); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, lref, RSET_FPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - if (irt_isu32(ir->t)) { - /* Convert both x and x-2^31 to int and merge results. */ - Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest)); - emit_asb(as, PPCI_OR, dest, dest, tmpi); /* Select with mask idiom. */ - emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP); - emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP); - emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO); /* tmp = (int)(x) */ - emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000); /* dest += 2^31 */ - emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31); /* mask = -(dest < 0) */ - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_tai(as, PPCI_LWZ, dest, - RID_SP, SPOFS_TMPLO); /* dest = (int)(x-2^31) */ - emit_fb(as, PPCI_FCTIWZ, tmp, left); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, tmp, tmp); - emit_fab(as, PPCI_FSUB, tmp, left, tmp); - emit_lsptr(as, PPCI_LFS, (tmp & 31), - (void *)&as->J->k32[LJ_K32_2P31], RSET_GPR); - } else { - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, tmp, left); - } - } - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT"); - if ((ir->op2 & IRCONV_SEXT)) - emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left); - else - emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31); - } else { /* 32/64 bit integer conversions. */ - /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ - ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } -} - -static void asm_strto(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - int32_t ofs = SPOFS_TMP; -#if LJ_SOFTFP - ra_evictset(as, RSET_SCRATCH); - if (ra_used(ir)) { - if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) && - (ir->s & 1) == LJ_BE && (ir->s ^ 1) == (ir+1)->s) { - int i; - for (i = 0; i < 2; i++) { - Reg r = (ir+i)->r; - if (ra_hasreg(r)) { - ra_free(as, r); - ra_modified(as, r); - emit_spload(as, ir+i, r, sps_scale((ir+i)->s)); - } - } - ofs = sps_scale(ir->s & ~1); - } else { - Reg rhi = ra_dest(as, ir+1, RSET_GPR); - Reg rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi)); - emit_tai(as, PPCI_LWZ, rhi, RID_SP, ofs); - emit_tai(as, PPCI_LWZ, rlo, RID_SP, ofs+4); - } - } -#else - RegSet drop = RSET_SCRATCH; - if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ - ra_evictset(as, drop); - if (ir->s) ofs = sps_scale(ir->s); -#endif - asm_guardcc(as, CC_EQ); - emit_ai(as, PPCI_CMPWI, RID_RET, 0); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - /* Store the result to the spill slot or temp slots. */ - emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs); -} - -/* -- Memory references --------------------------------------------------- */ - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode) -{ - int32_t tmpofs = (int32_t)(offsetof(global_State, tmptv)-32768); - if ((mode & IRTMPREF_IN1)) { - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if ((mode & IRTMPREF_OUT1)) { -#if LJ_SOFTFP - lj_assertA(irref_isk(ref), "unsplit FP op"); - emit_tai(as, PPCI_ADDI, dest, RID_JGL, tmpofs); - emit_setgl(as, - ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, RSET_GPR), - tmptv.u32.lo); - emit_setgl(as, - ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, RSET_GPR), - tmptv.u32.hi); -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_tai(as, PPCI_ADDI, dest, RID_JGL, tmpofs); - emit_fai(as, PPCI_STFD, src, RID_JGL, tmpofs); -#endif - } else if (irref_isk(ref)) { - /* Use the number constant itself as a TValue. */ - ra_allockreg(as, i32ptr(ir_knum(ir)), dest); - } else { -#if LJ_SOFTFP - lj_assertA(0, "unsplit FP op"); -#else - /* Otherwise force a spill and use the spill slot. */ - emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir)); -#endif - } - } else { - /* Otherwise use g->tmptv to hold the TValue. */ - Reg type; - emit_tai(as, PPCI_ADDI, dest, RID_JGL, tmpofs); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_GPR); - emit_setgl(as, src, tmptv.gcr); - } - if (LJ_SOFTFP && (ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)) - type = ra_alloc1(as, ref+1, RSET_GPR); - else - type = ra_allock(as, irt_toitype(ir->t), RSET_GPR); - emit_setgl(as, type, tmptv.it); - } - } else { - emit_tai(as, PPCI_ADDI, dest, RID_JGL, tmpofs); - } -} - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg idx, base; - if (irref_isk(ir->op2)) { - IRRef tab = IR(ir->op1)->op1; - int32_t ofs = asm_fuseabase(as, tab); - IRRef refa = ofs ? tab : ir->op1; - ofs += 8*IR(ir->op2)->i; - if (checki16(ofs)) { - base = ra_alloc1(as, refa, RSET_GPR); - emit_tai(as, PPCI_ADDI, dest, base, ofs); - return; - } - } - base = ra_alloc1(as, ir->op1, RSET_GPR); - idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); - emit_tab(as, PPCI_ADD, dest, RID_TMP, base); - emit_slwi(as, RID_TMP, idx, 3); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = RID_NONE, tmp1 = RID_TMP, tmp2; - Reg tisnum = RID_NONE, tmpnum = RID_NONE; - IRRef refkey = ir->op2; - IRIns *irkey = IR(refkey); - int isk = irref_isk(refkey); - IRType1 kt = irkey->t; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - - rset_clear(allow, tab); -#if LJ_SOFTFP - if (!isk) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - if (irkey[1].o == IR_HIOP) { - if (ra_hasreg((irkey+1)->r)) { - tmpnum = (irkey+1)->r; - ra_noweak(as, tmpnum); - } else { - tmpnum = ra_allocref(as, refkey+1, allow); - } - rset_clear(allow, tmpnum); - } - } -#else - if (irt_isnum(kt)) { - key = ra_alloc1(as, refkey, RSET_FPR); - tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); - tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); - rset_clear(allow, tisnum); - } else if (!irt_ispri(kt)) { - key = ra_alloc1(as, refkey, allow); - rset_clear(allow, key); - } -#endif - tmp2 = ra_scratch(as, allow); - rset_clear(allow, tmp2); - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - as->invmcp = NULL; - if (merge == IR_NE) - asm_guardcc(as, CC_EQ); - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - - /* Follow hash chain until the end. */ - l_loop = --as->mcp; - emit_ai(as, PPCI_CMPWI, dest, 0); - emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (merge == IR_EQ) - asm_guardcc(as, CC_EQ); - else - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); - if (!LJ_SOFTFP && irt_isnum(kt)) { - emit_fab(as, PPCI_FCMPU, 0, tmpnum, key); - emit_condbranch(as, PPCI_BC, CC_GE, l_next); - emit_ab(as, PPCI_CMPLW, tmp1, tisnum); - emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n)); - } else { - if (!irt_ispri(kt)) { - emit_ab(as, PPCI_CMPW, tmp2, key); - emit_condbranch(as, PPCI_BC, CC_NE, l_next); - } - if (LJ_SOFTFP && ra_hasreg(tmpnum)) - emit_ab(as, PPCI_CMPW, tmp1, tmpnum); - else - emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t)); - if (!irt_ispri(kt)) - emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); - } - emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it)); - *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) | - (((char *)as->mcp-(char *)l_loop) & 0xffffu); - - /* Load main position relative to tab->node into dest. */ - khash = isk ? ir_khash(as, irkey) : 1; - if (khash == 0) { - emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); - } else { - Reg tmphash = tmp1; - if (isk) - tmphash = ra_allock(as, khash, allow); - emit_tab(as, PPCI_ADD, dest, dest, tmp1); - emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node)); - emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash); - emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); - emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); - if (isk) { - /* Nothing to do. */ - } else if (irt_isstr(kt)) { - emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, sid)); - } else { /* Must match with hash*() in lj_tab.c. */ - emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1); - emit_rotlwi(as, tmp2, tmp2, HASH_ROT3); - emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2); - emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31); - emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2); - if (LJ_SOFTFP ? (irkey[1].o == IR_HIOP) : irt_isnum(kt)) { -#if LJ_SOFTFP - emit_asb(as, PPCI_XOR, tmp2, key, tmp1); - emit_rotlwi(as, dest, tmp1, HASH_ROT1); - emit_tab(as, PPCI_ADD, tmp1, tmpnum, tmpnum); -#else - int32_t ofs = ra_spill(as, irkey); - emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1); - emit_rotlwi(as, dest, tmp1, HASH_ROT1); - emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1); - emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4); - emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs); -#endif - } else { - emit_asb(as, PPCI_XOR, tmp2, key, tmp1); - emit_rotlwi(as, dest, tmp1, HASH_ROT1); - emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS); - emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16); - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - int32_t kofs = ofs + (int32_t)offsetof(Node, key); - Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); - Reg key = RID_NONE, type = RID_TMP, idx = node; - RegSet allow = rset_exclude(RSET_GPR, node); - lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot"); - if (ofs > 32736) { - idx = dest; - rset_clear(allow, dest); - kofs = (int32_t)offsetof(Node, key); - } else if (ra_hasreg(dest)) { - emit_tai(as, PPCI_ADDI, dest, node, ofs); - } - asm_guardcc(as, CC_NE); - if (!irt_ispri(irkey->t)) { - key = ra_scratch(as, allow); - rset_clear(allow, key); - } - rset_clear(allow, type); - if (irt_isnum(irkey->t)) { - emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo); - asm_guardcc(as, CC_NE); - emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi); - } else { - if (ra_hasreg(key)) { - emit_cmpi(as, key, irkey->i); /* May use RID_TMP, i.e. type. */ - asm_guardcc(as, CC_NE); - } - emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t)); - } - if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4); - emit_tai(as, PPCI_LWZ, type, idx, kofs); - if (ofs > 32736) { - emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16); - emit_tai(as, PPCI_ADDI, dest, node, ofs); - } -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - asm_guardcc(as, CC_NE); - emit_ai(as, PPCI_CMPWI, RID_TMP, 1); - emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv)); - emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); - } else { - emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v)); - } - emit_tai(as, PPCI_LWZ, uv, func, - (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - UNUSED(as); UNUSED(ir); - lj_assertA(!ra_used(ir), "unfused FREF"); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRRef ref = ir->op2, refk = ir->op1; - int32_t ofs = (int32_t)sizeof(GCstr); - Reg r; - if (irref_isk(ref)) { - IRRef tmp = refk; refk = ref; ref = tmp; - } else if (!irref_isk(refk)) { - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - IRIns *irr = IR(ir->op2); - if (ra_hasreg(irr->r)) { - ra_noweak(as, irr->r); - right = irr->r; - } else if (mayfuse(as, irr->op2) && - irr->o == IR_ADD && irref_isk(irr->op2) && - checki16(ofs + IR(irr->op2)->i)) { - ofs += IR(irr->op2)->i; - right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); - } else { - right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_tai(as, PPCI_ADDI, dest, dest, ofs); - emit_tab(as, PPCI_ADD, dest, left, right); - return; - } - r = ra_alloc1(as, ref, RSET_GPR); - ofs += IR(refk)->i; - if (checki16(ofs)) - emit_tai(as, PPCI_ADDI, dest, r, ofs); - else - emit_tab(as, PPCI_ADD, dest, r, - ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static PPCIns asm_fxloadins(ASMState *as, IRIns *ir) -{ - UNUSED(as); - switch (irt_type(ir->t)) { - case IRT_I8: return PPCI_LBZ; /* Needs sign-extension. */ - case IRT_U8: return PPCI_LBZ; - case IRT_I16: return PPCI_LHA; - case IRT_U16: return PPCI_LHZ; - case IRT_NUM: lj_assertA(!LJ_SOFTFP, "unsplit FP op"); return PPCI_LFD; - case IRT_FLOAT: if (!LJ_SOFTFP) return PPCI_LFS; - default: return PPCI_LWZ; - } -} - -static PPCIns asm_fxstoreins(ASMState *as, IRIns *ir) -{ - UNUSED(as); - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: return PPCI_STB; - case IRT_I16: case IRT_U16: return PPCI_STH; - case IRT_NUM: lj_assertA(!LJ_SOFTFP, "unsplit FP op"); return PPCI_STFD; - case IRT_FLOAT: if (!LJ_SOFTFP) return PPCI_STFS; - default: return PPCI_STW; - } -} - -static void asm_fload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - PPCIns pi = asm_fxloadins(as, ir); - Reg idx; - int32_t ofs; - if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */ - idx = RID_JGL; - ofs = (ir->op2 << 2) - 32768 - GG_OFS(g); - } else { - idx = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->op2 == IRFL_TAB_ARRAY) { - ofs = asm_fuseabase(as, ir->op1); - if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ - emit_tai(as, PPCI_ADDI, dest, idx, ofs); - return; - } - } - ofs = field_ofs[ir->op2]; - } - lj_assertA(!irt_isi8(ir->t), "unsupported FLOAD I8"); - emit_tai(as, pi, dest, idx, ofs); -} - -static void asm_fstore(ASMState *as, IRIns *ir) -{ - if (ir->r != RID_SINK) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - IRIns *irf = IR(ir->op1); - Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); - int32_t ofs = field_ofs[irf->op2]; - PPCIns pi = asm_fxstoreins(as, ir); - emit_tai(as, pi, src, idx, ofs); - } -} - -static void asm_xload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - lj_assertA(!(ir->op2 & IRXLOAD_UNALIGNED), "unaligned XLOAD"); - if (irt_isi8(ir->t)) - emit_as(as, PPCI_EXTSB, dest, dest); - asm_fusexref(as, asm_fxloadins(as, ir), dest, ir->op1, RSET_GPR, 0); -} - -static void asm_xstore_(ASMState *as, IRIns *ir, int32_t ofs) -{ - IRIns *irb; - if (ir->r == RID_SINK) - return; - if (ofs == 0 && mayfuse(as, ir->op2) && (irb = IR(ir->op2))->o == IR_BSWAP && - ra_noreg(irb->r) && (irt_isint(ir->t) || irt_isu32(ir->t))) { - /* Fuse BSWAP with XSTORE to stwbrx. */ - Reg src = ra_alloc1(as, irb->op1, RSET_GPR); - asm_fusexrefx(as, PPCI_STWBRX, src, ir->op1, rset_exclude(RSET_GPR, src)); - } else { - Reg src = ra_alloc1(as, ir->op2, - (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); - asm_fusexref(as, asm_fxstoreins(as, ir), src, ir->op1, - rset_exclude(RSET_GPR, src), ofs); - } -} - -#define asm_xstore(as, ir) asm_xstore_(as, ir, 0) - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ - IRType1 t = ir->t; - Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx; - RegSet allow = RSET_GPR; - int32_t ofs = AHUREF_LSX; - if (LJ_SOFTFP && (ir+1)->o == IR_HIOP) { - t.irt = IRT_NUM; - if (ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } - ofs = 0; - } - if (ra_used(ir)) { - lj_assertA((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || - irt_isint(ir->t) || irt_isaddr(ir->t), - "bad load type %d", irt_type(ir->t)); - if (LJ_SOFTFP || !irt_isnum(t)) ofs = 0; - dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow); - rset_clear(allow, dest); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (ir->o == IR_VLOAD) { - ofs = ofs != AHUREF_LSX ? ofs + 8 * ir->op2 : - ir->op2 ? 8 * ir->op2 : AHUREF_LSX; - } - if (irt_isnum(t)) { - Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx)); - asm_guardcc(as, CC_GE); - emit_ab(as, PPCI_CMPLW, type, tisnum); - if (ra_hasreg(dest)) { - if (!LJ_SOFTFP && ofs == AHUREF_LSX) { - tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, - (idx&255)), (idx>>8))); - emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp); - } else { - emit_fai(as, LJ_SOFTFP ? PPCI_LWZ : PPCI_LFD, dest, idx, - ofs+4*LJ_SOFTFP); - } - } - } else { - asm_guardcc(as, CC_NE); - emit_ai(as, PPCI_CMPWI, type, irt_toitype(t)); - if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4); - } - if (ofs == AHUREF_LSX) { - emit_tab(as, PPCI_LWZX, type, (idx&255), tmp); - emit_slwi(as, tmp, (idx>>8), 3); - } else { - emit_tai(as, PPCI_LWZ, type, idx, ofs); - } -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg idx, src = RID_NONE, type = RID_NONE; - int32_t ofs = AHUREF_LSX; - if (ir->r == RID_SINK) - return; - if (!LJ_SOFTFP && irt_isnum(ir->t)) { - src = ra_alloc1(as, ir->op2, RSET_FPR); - } else { - if (!irt_ispri(ir->t)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - ofs = 0; - } - if (LJ_SOFTFP && (ir+1)->o == IR_HIOP) - type = ra_alloc1(as, (ir+1)->op2, allow); - else - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - rset_clear(allow, type); - } - idx = asm_fuseahuref(as, ir->op1, &ofs, allow); - if (!LJ_SOFTFP && irt_isnum(ir->t)) { - if (ofs == AHUREF_LSX) { - emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP); - emit_slwi(as, RID_TMP, (idx>>8), 3); - } else { - emit_fai(as, PPCI_STFD, src, idx, ofs); - } - } else { - if (ra_hasreg(src)) - emit_tai(as, PPCI_STW, src, idx, ofs+4); - if (ofs == AHUREF_LSX) { - emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP); - emit_slwi(as, RID_TMP, (idx>>8), 3); - } else { - emit_tai(as, PPCI_STW, type, idx, ofs); - } - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4); - IRType1 t = ir->t; - Reg dest = RID_NONE, type = RID_NONE, base; - RegSet allow = RSET_GPR; - int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); - if (hiop) - t.irt = IRT_NUM; - lj_assertA(!(ir->op2 & IRSLOAD_PARENT), - "bad parent SLOAD"); /* Handled by asm_head_side(). */ - lj_assertA(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK), - "inconsistent SLOAD variant"); - lj_assertA(LJ_DUALNUM || - !irt_isint(t) || - (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME|IRSLOAD_KEYINDEX)), - "bad SLOAD type"); -#if LJ_SOFTFP - lj_assertA(!(ir->op2 & IRSLOAD_CONVERT), - "unsplit SLOAD convert"); /* Handled by LJ_SOFTFP SPLIT. */ - if (hiop && ra_used(ir+1)) { - type = ra_dest(as, ir+1, allow); - rset_clear(allow, type); - } -#else - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - dest = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, dest); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ - } else -#endif - if (ra_used(ir)) { - lj_assertA(irt_isnum(t) || irt_isint(t) || irt_isaddr(t), - "bad SLOAD type %d", irt_type(ir->t)); - dest = ra_dest(as, ir, (!LJ_SOFTFP && irt_isnum(t)) ? RSET_FPR : allow); - rset_clear(allow, dest); - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); - if (!LJ_SOFTFP && (ir->op2 & IRSLOAD_CONVERT)) { - if (irt_isint(t)) { - emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); - dest = ra_scratch(as, RSET_FPR); - emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP); - emit_fb(as, PPCI_FCTIWZ, dest, dest); - t.irt = IRT_NUM; /* Check for original type. */ - } else { - Reg tmp = ra_scratch(as, allow); - Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp)); - Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - emit_fab(as, PPCI_FSUB, dest, dest, fbias); - emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); - emit_lsptr(as, PPCI_LFS, (fbias & 31), - (void *)&as->J->k32[LJ_K32_2P52_2P31], - rset_clear(allow, hibias)); - emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO); - emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); - emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000); - dest = tmp; - t.irt = IRT_INT; /* Check for original type. */ - } - } - goto dotypecheck; - } - base = ra_alloc1(as, REF_BASE, allow); - rset_clear(allow, base); -dotypecheck: - if (irt_isnum(t)) { - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); - asm_guardcc(as, CC_GE); -#if !LJ_SOFTFP - type = RID_TMP; -#endif - emit_ab(as, PPCI_CMPLW, type, tisnum); - } - if (ra_hasreg(dest)) emit_fai(as, LJ_SOFTFP ? PPCI_LWZ : PPCI_LFD, dest, - base, ofs-(LJ_SOFTFP?0:4)); - } else { - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - asm_guardcc(as, CC_NE); - if ((ir->op2 & IRSLOAD_KEYINDEX)) { - emit_ai(as, PPCI_CMPWI, RID_TMP, (LJ_KEYINDEX & 0xffff)); - emit_asi(as, PPCI_XORIS, RID_TMP, RID_TMP, (LJ_KEYINDEX >> 16)); - } else { - emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t)); - } - type = RID_TMP; - } - if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs); - } - if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4); -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID id = (CTypeID)IR(ir->op1)->i; - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[4]; - RegSet drop = RSET_SCRATCH; - lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL), - "bad CNEW/CNEWI operands"); - - as->gcsteps++; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - if (ra_used(ir)) - ra_destreg(as, ir, RID_RET); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - int32_t ofs = sizeof(GCcdata); - lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz); - if (sz == 8) { - ofs += 4; - lj_assertA((ir+1)->o == IR_HIOP, "expected HIOP for CNEWI"); - } - for (;;) { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_tai(as, PPCI_STW, r, RID_RET, ofs); - rset_clear(allow, r); - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; ir++; - } - } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */ - ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* CTypeID id */ - args[2] = ir->op2; /* CTSize sz */ - args[3] = ASMREF_TMP1; /* CTSize align */ - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info)); - return; - } - - /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ - emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); - emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); - emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA); - emit_ti(as, PPCI_LI, RID_TMP, id); /* Lower 16 bit used. Sign-ext ok. */ - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - asm_gencall(as, ci, args); - ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), - ra_releasetmp(as, ASMREF_TMP1)); -} -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - Reg link = RID_TMP; - MCLabel l_end = emit_label(as); - emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist)); - emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked)); - emit_setgl(as, tab, gc.grayagain); - lj_assertA(LJ_GC_BLACK == 0x04, "bad LJ_GC_BLACK"); - emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28); /* Clear black bit. */ - emit_getgl(as, link, gc.grayagain); - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); - emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK); - emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj, val, tmp; - /* No need for other object barriers (yet). */ - lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type"); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - obj = IR(ir->op1)->r; - tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); - emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK); - emit_condbranch(as, PPCI_BC, CC_EQ, l_end); - emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES); - val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); - emit_tai(as, PPCI_LBZ, tmp, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); - emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked)); -} - -/* -- Arithmetic and logic operations ------------------------------------- */ - -#if !LJ_SOFTFP -static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - if (pi == PPCI_FMUL) - emit_fac(as, pi, dest, left, right); - else - emit_fab(as, pi, dest, left, right); -} - -static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi) -{ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); - emit_fb(as, pi, dest, left); -} - -static void asm_fpmath(ASMState *as, IRIns *ir) -{ - if (ir->op2 == IRFPM_SQRT && (as->flags & JIT_F_SQRT)) - asm_fpunary(as, ir, PPCI_FSQRT); - else - asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); -} -#endif - -static void asm_add(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD)) - asm_fparith(as, ir, PPCI_FADD); - } else -#endif - { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - PPCIns pi; - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - pi = PPCI_ADDI; - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi = PPCI_ADDICDOT; - } - emit_tai(as, pi, dest, left, k); - return; - } else if ((k & 0xffff) == 0) { - emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16)); - return; - } else if (!as->sectref) { - emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16); - emit_tai(as, PPCI_ADDI, dest, left, k); - return; - } - } - pi = PPCI_ADD; - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, pi, dest, left, right); - } -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB)) - asm_fparith(as, ir, PPCI_FSUB); - } else -#endif - { - PPCIns pi = PPCI_SUBF; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left, right; - if (irref_isk(ir->op1)) { - int32_t k = IR(ir->op1)->i; - if (checki16(k)) { - right = ra_alloc1(as, ir->op2, RSET_GPR); - emit_tai(as, PPCI_SUBFIC, dest, right, k); - return; - } - } - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ - } -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fparith(as, ir, PPCI_FMUL); - } else -#endif - { - PPCIns pi = PPCI_MULLW; - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - emit_tai(as, PPCI_MULLI, dest, left, k); - return; - } - } - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, pi, dest, left, right); - } -} - -#define asm_fpdiv(as, ir) asm_fparith(as, ir, PPCI_FDIV) - -static void asm_neg(ASMState *as, IRIns *ir) -{ -#if !LJ_SOFTFP - if (irt_isnum(ir->t)) { - asm_fpunary(as, ir, PPCI_FNEG); - } else -#endif - { - Reg dest, left; - PPCIns pi = PPCI_NEG; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - emit_tab(as, pi, dest, left, 0); - } -} - -#define asm_abs(as, ir) asm_fpunary(as, ir, PPCI_FABS) - -static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi) -{ - Reg dest, left, right; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - } - asm_guardcc(as, CC_SO); - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; } - emit_tab(as, pi|PPCF_DOT, dest, left, right); -} - -#define asm_addov(as, ir) asm_arithov(as, ir, PPCI_ADDO) -#define asm_subov(as, ir) asm_arithov(as, ir, PPCI_SUBFO) -#define asm_mulov(as, ir) asm_arithov(as, ir, PPCI_MULLWO) - -#if LJ_HASFFI -static void asm_add64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); - PPCIns pi = PPCI_ADDE; - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k == 0) - pi = PPCI_ADDZE; - else if (k == -1) - pi = PPCI_ADDME; - else - goto needright; - right = 0; - } else { - needright: - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - } - emit_tab(as, pi, dest, left, right); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (checki16(k)) { - emit_tai(as, PPCI_ADDIC, dest, left, k); - return; - } - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_tab(as, PPCI_ADDC, dest, left, right); -} - -static void asm_sub64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR); - PPCIns pi = PPCI_SUBFE; - if (irref_isk(ir->op1)) { - int32_t k = IR(ir->op1)->i; - if (k == 0) - pi = PPCI_SUBFZE; - else if (k == -1) - pi = PPCI_SUBFME; - else - goto needleft; - left = 0; - } else { - needleft: - left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); - } - emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ - ir--; - dest = ra_dest(as, ir, RSET_GPR); - right = ra_alloc1(as, ir->op2, RSET_GPR); - if (irref_isk(ir->op1)) { - int32_t k = IR(ir->op1)->i; - if (checki16(k)) { - emit_tai(as, PPCI_SUBFIC, dest, right, k); - return; - } - } - left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); - emit_tab(as, PPCI_SUBFC, dest, right, left); -} - -static void asm_neg64(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_tab(as, PPCI_SUBFZE, dest, left, 0); - ir--; - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - emit_tai(as, PPCI_SUBFIC, dest, left, 0); -} -#endif - -static void asm_bnot(ASMState *as, IRIns *ir) -{ - Reg dest, left, right; - PPCIns pi = PPCI_NOR; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - if (mayfuse(as, ir->op1)) { - IRIns *irl = IR(ir->op1); - if (irl->o == IR_BAND) - pi ^= (PPCI_NOR ^ PPCI_NAND); - else if (irl->o == IR_BXOR) - pi ^= (PPCI_NOR ^ PPCI_EQV); - else if (irl->o != IR_BOR) - goto nofuse; - left = ra_hintalloc(as, irl->op1, dest, RSET_GPR); - right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left)); - } else { -nofuse: - left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - } - emit_asb(as, pi, dest, left, right); -} - -static void asm_bswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - IRIns *irx; - if (mayfuse(as, ir->op1) && (irx = IR(ir->op1))->o == IR_XLOAD && - ra_noreg(irx->r) && (irt_isint(irx->t) || irt_isu32(irx->t))) { - /* Fuse BSWAP with XLOAD to lwbrx. */ - asm_fusexrefx(as, PPCI_LWBRX, dest, irx->op1, RSET_GPR); - } else { - Reg left = ra_alloc1(as, ir->op1, RSET_GPR); - Reg tmp = dest; - if (tmp == left) { - tmp = RID_TMP; - emit_mr(as, dest, RID_TMP); - } - emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23); - emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7); - emit_rotlwi(as, tmp, left, 8); - } -} - -/* Fuse BAND with contiguous bitmask and a shift to rlwinm. */ -static void asm_fuseandsh(ASMState *as, PPCIns pi, int32_t mask, IRRef ref) -{ - IRIns *ir; - Reg left; - if (mayfuse(as, ref) && (ir = IR(ref), ra_noreg(ir->r)) && - irref_isk(ir->op2) && ir->o >= IR_BSHL && ir->o <= IR_BROR) { - int32_t sh = (IR(ir->op2)->i & 31); - switch (ir->o) { - case IR_BSHL: - if ((mask & ((1u<>sh))) goto nofuse; - sh = ((32-sh)&31); - break; - case IR_BROL: - break; - default: - goto nofuse; - } - left = ra_alloc1(as, ir->op1, RSET_GPR); - *--as->mcp = pi | PPCF_T(left) | PPCF_B(sh); - return; - } -nofuse: - left = ra_alloc1(as, ref, RSET_GPR); - *--as->mcp = pi | PPCF_T(left); -} - -static void asm_band(ASMState *as, IRIns *ir) -{ - Reg dest, left, right; - IRRef lref = ir->op1; - PPCIns dot = 0; - IRRef op2; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - dot = PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - if (k) { - /* First check for a contiguous bitmask as used by rlwinm. */ - uint32_t s1 = lj_ffs((uint32_t)k); - uint32_t k1 = ((uint32_t)k >> s1); - if ((k1 & (k1+1)) == 0) { - asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) | - PPCF_MB(31-lj_fls((uint32_t)k)) | PPCF_ME(31-s1), - k, lref); - return; - } - if (~(uint32_t)k) { - uint32_t s2 = lj_ffs(~(uint32_t)k); - uint32_t k2 = (~(uint32_t)k >> s2); - if ((k2 & (k2+1)) == 0) { - asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) | - PPCF_MB(32-s2) | PPCF_ME(30-lj_fls(~(uint32_t)k)), - k, lref); - return; - } - } - } - if (checku16(k)) { - left = ra_alloc1(as, lref, RSET_GPR); - emit_asi(as, PPCI_ANDIDOT, dest, left, k); - return; - } else if ((k & 0xffff) == 0) { - left = ra_alloc1(as, lref, RSET_GPR); - emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16)); - return; - } - } - op2 = ir->op2; - if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT && ra_noreg(IR(op2)->r)) { - dot ^= (PPCI_AND ^ PPCI_ANDC); - op2 = IR(op2)->op1; - } - left = ra_hintalloc(as, lref, dest, RSET_GPR); - right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left)); - emit_asb(as, PPCI_AND ^ dot, dest, left, right); -} - -static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); - if (irref_isk(ir->op2)) { - int32_t k = IR(ir->op2)->i; - Reg tmp = left; - if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) { - if (!checku16(k)) { - emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16)); - if ((k & 0xffff) == 0) return; - } - emit_asi(as, pik, dest, left, k); - return; - } - } - /* May fail due to spills/restores above, but simplifies the logic. */ - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - pi |= PPCF_DOT; - } - right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_asb(as, pi, dest, left, right); -} - -#define asm_bor(as, ir) asm_bitop(as, ir, PPCI_OR, PPCI_ORI) -#define asm_bxor(as, ir) asm_bitop(as, ir, PPCI_XOR, PPCI_XORI) - -static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) -{ - Reg dest, left; - Reg dot = 0; - if (as->flagmcp == as->mcp) { - as->flagmcp = NULL; - as->mcp++; - dot = PPCF_DOT; - } - dest = ra_dest(as, ir, RSET_GPR); - left = ra_alloc1(as, ir->op1, RSET_GPR); - if (irref_isk(ir->op2)) { /* Constant shifts. */ - int32_t shift = (IR(ir->op2)->i & 31); - if (pik == 0) /* SLWI */ - emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift); - else if (pik == 1) /* SRWI */ - emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31); - else - emit_asb(as, pik|dot, dest, left, shift); - } else { - Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); - emit_asb(as, pi|dot, dest, left, right); - } -} - -#define asm_bshl(as, ir) asm_bitshift(as, ir, PPCI_SLW, 0) -#define asm_bshr(as, ir) asm_bitshift(as, ir, PPCI_SRW, 1) -#define asm_bsar(as, ir) asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI) -#define asm_brol(as, ir) \ - asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), \ - PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31)) -#define asm_bror(as, ir) lj_assertA(0, "unexpected BROR") - -#if LJ_SOFTFP -static void asm_sfpmin_max(ASMState *as, IRIns *ir) -{ - CCallInfo ci = lj_ir_callinfo[IRCALL_softfp_cmp]; - IRRef args[4]; - MCLabel l_right, l_end; - Reg desthi = ra_dest(as, ir, RSET_GPR), destlo = ra_dest(as, ir+1, RSET_GPR); - Reg righthi, lefthi = ra_alloc2(as, ir, RSET_GPR); - Reg rightlo, leftlo = ra_alloc2(as, ir+1, RSET_GPR); - PPCCC cond = (IROp)ir->o == IR_MIN ? CC_EQ : CC_NE; - righthi = (lefthi >> 8); lefthi &= 255; - rightlo = (leftlo >> 8); leftlo &= 255; - args[0^LJ_BE] = ir->op1; args[1^LJ_BE] = (ir+1)->op1; - args[2^LJ_BE] = ir->op2; args[3^LJ_BE] = (ir+1)->op2; - l_end = emit_label(as); - if (desthi != righthi) emit_mr(as, desthi, righthi); - if (destlo != rightlo) emit_mr(as, destlo, rightlo); - l_right = emit_label(as); - if (l_end != l_right) emit_jmp(as, l_end); - if (desthi != lefthi) emit_mr(as, desthi, lefthi); - if (destlo != leftlo) emit_mr(as, destlo, leftlo); - if (l_right == as->mcp+1) { - cond ^= 4; l_right = l_end; ++as->mcp; - } - emit_condbranch(as, PPCI_BC, cond, l_right); - ra_evictset(as, RSET_SCRATCH); - emit_cmpi(as, RID_RET, 1); - asm_gencall(as, &ci, args); -} -#endif - -static void asm_min_max(ASMState *as, IRIns *ir, int ismax) -{ - if (!LJ_SOFTFP && irt_isnum(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg tmp = dest; - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - if (tmp == left || tmp == right) - tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR, - dest), left), right)); - emit_facb(as, PPCI_FSEL, dest, tmp, left, right); - emit_fab(as, PPCI_FSUB, tmp, ismax ? left : right, ismax ? right : left); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg tmp1 = RID_TMP, tmp2 = dest; - Reg right, left = ra_alloc2(as, ir, RSET_GPR); - right = (left >> 8); left &= 255; - if (tmp2 == left || tmp2 == right) - tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, - dest), left), right)); - emit_tab(as, PPCI_ADD, dest, tmp2, right); - emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1); - emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1); - emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1); - emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000); - emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000); - } -} - -#define asm_min(as, ir) asm_min_max(as, ir, 0) -#define asm_max(as, ir) asm_min_max(as, ir, 1) - -/* -- Comparisons --------------------------------------------------------- */ - -#define CC_UNSIGNED 0x08 /* Unsigned integer comparison. */ -#define CC_TWO 0x80 /* Check two flags for FP comparison. */ - -/* Map of comparisons to flags. ORDER IR. */ -static const uint8_t asm_compmap[IR_ABC+1] = { - /* op int cc FP cc */ - /* LT */ CC_GE + (CC_GE<<4), - /* GE */ CC_LT + (CC_LE<<4) + CC_TWO, - /* LE */ CC_GT + (CC_GE<<4) + CC_TWO, - /* GT */ CC_LE + (CC_LE<<4), - /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO, - /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4), - /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4), - /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO, - /* EQ */ CC_NE + (CC_NE<<4), - /* NE */ CC_EQ + (CC_EQ<<4), - /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO /* Same as UGT. */ -}; - -static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc) -{ - Reg right, left = ra_alloc1(as, lref, RSET_GPR); - if (irref_isk(rref)) { - int32_t k = IR(rref)->i; - if ((cc & CC_UNSIGNED) == 0) { /* Signed comparison with constant. */ - if (checki16(k)) { - emit_tai(as, PPCI_CMPWI, cr, left, k); - /* Signed comparison with zero and referencing previous ins? */ - if (k == 0 && lref == as->curins-1) - as->flagmcp = as->mcp; /* Allow elimination of the compare. */ - return; - } else if ((cc & 3) == (CC_EQ & 3)) { /* Use CMPLWI for EQ or NE. */ - if (checku16(k)) { - emit_tai(as, PPCI_CMPLWI, cr, left, k); - return; - } else if (!as->sectref && ra_noreg(IR(rref)->r)) { - emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k); - emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16)); - return; - } - } - } else { /* Unsigned comparison with constant. */ - if (checku16(k)) { - emit_tai(as, PPCI_CMPLWI, cr, left, k); - return; - } - } - } - right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left)); - emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right); -} - -static void asm_comp(ASMState *as, IRIns *ir) -{ - PPCCC cc = asm_compmap[ir->o]; - if (!LJ_SOFTFP && irt_isnum(ir->t)) { - Reg right, left = ra_alloc2(as, ir, RSET_FPR); - right = (left >> 8); left &= 255; - asm_guardcc(as, (cc >> 4)); - if ((cc & CC_TWO)) - emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3)); - emit_fab(as, PPCI_FCMPU, 0, left, right); - } else { - IRRef lref = ir->op1, rref = ir->op2; - if (irref_isk(lref) && !irref_isk(rref)) { - /* Swap constants to the right (only for ABC). */ - IRRef tmp = lref; lref = rref; rref = tmp; - if ((cc & 2) == 0) cc ^= 1; /* LT <-> GT, LE <-> GE */ - } - asm_guardcc(as, cc); - asm_intcomp_(as, lref, rref, 0, cc); - } -} - -#define asm_equal(as, ir) asm_comp(as, ir) - -#if LJ_SOFTFP -/* SFP comparisons. */ -static void asm_sfpcomp(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; - RegSet drop = RSET_SCRATCH; - Reg r; - IRRef args[4]; - args[0^LJ_BE] = ir->op1; args[1^LJ_BE] = (ir+1)->op1; - args[2^LJ_BE] = ir->op2; args[3^LJ_BE] = (ir+1)->op2; - - for (r = REGARG_FIRSTGPR; r <= REGARG_FIRSTGPR+3; r++) { - if (!rset_test(as->freeset, r) && - regcost_ref(as->cost[r]) == args[r-REGARG_FIRSTGPR]) - rset_clear(drop, r); - } - ra_evictset(as, drop); - asm_setupresult(as, ir, ci); - switch ((IROp)ir->o) { - case IR_ULT: - asm_guardcc(as, CC_EQ); - emit_ai(as, PPCI_CMPWI, RID_RET, 0); - case IR_ULE: - asm_guardcc(as, CC_EQ); - emit_ai(as, PPCI_CMPWI, RID_RET, 1); - break; - case IR_GE: case IR_GT: - asm_guardcc(as, CC_EQ); - emit_ai(as, PPCI_CMPWI, RID_RET, 2); - default: - asm_guardcc(as, (asm_compmap[ir->o] & 0xf)); - emit_ai(as, PPCI_CMPWI, RID_RET, 0); - break; - } - asm_gencall(as, ci, args); -} -#endif - -#if LJ_HASFFI -/* 64 bit integer comparisons. */ -static void asm_comp64(ASMState *as, IRIns *ir) -{ - PPCCC cc = asm_compmap[(ir-1)->o]; - if ((cc&3) == (CC_EQ&3)) { - asm_guardcc(as, cc); - emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR, - (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3)); - } else { - asm_guardcc(as, CC_EQ); - emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1)); - emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC, - (CC_EQ&3), (CC_EQ&3), 4+(cc&3)); - } - /* Loword comparison sets cr1 and is unsigned, except for equality. */ - asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4, - cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED)); - /* Hiword comparison sets cr0. */ - asm_intcomp_(as, ir->op1, ir->op2, 0, cc); - as->flagmcp = NULL; /* Doesn't work here. */ -} -#endif - -/* -- Split register ops -------------------------------------------------- */ - -/* Hiword op of a split 32/32 bit op. Previous op is be the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; -#if LJ_HASFFI || LJ_SOFTFP - if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ - as->curins--; /* Always skip the CONV. */ -#if LJ_HASFFI && !LJ_SOFTFP - if (usehi || uselo) - asm_conv64(as, ir); - return; -#endif - } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ - as->curins--; /* Always skip the loword comparison. */ -#if LJ_SOFTFP - if (!irt_isint(ir->t)) { - asm_sfpcomp(as, ir-1); - return; - } -#endif -#if LJ_HASFFI - asm_comp64(as, ir); -#endif - return; -#if LJ_SOFTFP - } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { - as->curins--; /* Always skip the loword min/max. */ - if (uselo || usehi) - asm_sfpmin_max(as, ir-1); - return; -#endif - } else if ((ir-1)->o == IR_XSTORE) { - as->curins--; /* Handle both stores here. */ - if ((ir-1)->r != RID_SINK) { - asm_xstore_(as, ir, 0); - asm_xstore_(as, ir-1, 4); - } - return; - } -#endif - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { -#if LJ_HASFFI - case IR_ADD: as->curins--; asm_add64(as, ir); break; - case IR_SUB: as->curins--; asm_sub64(as, ir); break; - case IR_NEG: as->curins--; asm_neg64(as, ir); break; - case IR_CNEWI: - /* Nothing to do here. Handled by lo op itself. */ - break; -#endif -#if LJ_SOFTFP - case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - case IR_STRTO: - if (!uselo) - ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */ - break; - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: case IR_TMPREF: - /* Nothing to do here. Handled by lo op itself. */ - break; -#endif - case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break; - } -} - -/* -- Profiling ----------------------------------------------------------- */ - -static void asm_prof(ASMState *as, IRIns *ir) -{ - UNUSED(ir); - asm_guardcc(as, CC_NE); - emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, HOOK_PROFILE); - emit_lsglptr(as, PPCI_LBZ, RID_TMP, - (int32_t)offsetof(global_State, hookmask)); -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ - Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; - rset_clear(allow, pbase); - tmp = allow ? rset_pickbot(allow) : - (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); - emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno)); - if (allow == RSET_EMPTY) /* Restore temp. register. */ - emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW); - else - ra_modified(as, tmp); - emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot)); - emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp); - emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack)); - if (pbase == RID_TMP) - emit_getgl(as, RID_TMP, jit_base); - emit_getgl(as, tmp, cur_L); - if (allow == RSET_EMPTY) /* Spill temp. register. */ - emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW); -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if (irt_isnum(ir->t)) { -#if LJ_SOFTFP - Reg tmp; - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - /* LJ_SOFTFP: must be a number constant. */ - lj_assertA(irref_isk(ref), "unsplit FP op"); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, allow); - emit_tai(as, PPCI_STW, tmp, RID_BASE, ofs+(LJ_BE?4:0)); - if (rset_test(as->freeset, tmp+1)) allow = RID2RSET(tmp+1); - tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, allow); - emit_tai(as, PPCI_STW, tmp, RID_BASE, ofs+(LJ_BE?0:4)); -#else - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_fai(as, PPCI_STFD, src, RID_BASE, ofs); -#endif - } else { - Reg type; - RegSet allow = rset_exclude(RSET_GPR, RID_BASE); - lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t), - "restore of IR type %d", irt_type(ir->t)); - if (!irt_ispri(ir->t)) { - Reg src = ra_alloc1(as, ref, allow); - rset_clear(allow, src); - emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4); - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { - if (s == 0) continue; /* Do not overwrite link to previous frame. */ - type = ra_allock(as, (int32_t)(*flinks--), allow); -#if LJ_SOFTFP - } else if ((sn & SNAP_SOFTFPNUM)) { - type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPR, RID_BASE)); -#endif - } else if ((sn & SNAP_KEYINDEX)) { - type = ra_allock(as, (int32_t)LJ_KEYINDEX, allow); - } else { - type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); - } - emit_tai(as, PPCI_STW, type, RID_BASE, ofs); - } - checkmclim(as); - } - lj_assertA(map + nent == flinks, "inconsistent frames in snapshot"); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Marker to prevent patching the GC check exit. */ -#define PPC_NOPATCH_GC_CHECK PPCI_ORIS - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ - *--as->mcp = PPC_NOPATCH_GC_CHECK; - emit_ai(as, PPCI_CMPWI, RID_RET, 0); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); - tmp = ra_releasetmp(as, ASMREF_TMP2); - emit_loadi(as, tmp, as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end); - emit_ab(as, PPCI_CMPLW, RID_TMP, tmp); - emit_getgl(as, tmp, gc.threshold); - emit_getgl(as, RID_TMP, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guardcc already inverted the cond branch and patched the final b. */ - p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2); - } else { - p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); - } -} - -/* Fixup the tail of the loop. */ -static void asm_loop_tail_fixup(ASMState *as) -{ - UNUSED(as); /* Nothing to do. */ -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r) || irt_ismarked(ir->t)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (r != RID_BASE) - emit_mr(as, r, RID_BASE); - } -} - -/* Coalesce BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r) || irt_ismarked(ir->t)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ - } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - rset_clear(allow, irp->r); - emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */ - } else { - emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ - } - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - MCode *p = as->mctop; - MCode *target; - int32_t spadj = as->T->spadjust; - if (spadj == 0) { - *--p = PPCI_NOP; - *--p = PPCI_NOP; - as->mctop = p; - } else { - /* Patch stack adjustment. */ - lj_assertA(checki16(CFRAME_SIZE+spadj), "stack adjustment out of range"); - p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj); - p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj; - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop - 1; /* Leave room for exit branch. */ - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - as->mcp = p-2; /* Leave room for stack pointer adjustment. */ - as->invmcp = NULL; - } -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - uint32_t i, nargs = CCI_XNARGS(ci); - int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; - asm_collectargs(as, ir, ci, args); - for (i = 0; i < nargs; i++) - if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1; - } else { - if (ngpr > 0) ngpr--; else nslots++; - } - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; - return (!LJ_SOFTFP && irt_isfp(ir->t)) ? REGSP_HINT(RID_FPRET) : - REGSP_HINT(RID_RET); -} - -static void asm_setup_target(ASMState *as) -{ - asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); -} - -/* -- Trace patching ------------------------------------------------------ */ - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *pe = (MCode *)((char *)p + T->szmcode); - MCode *px = exitstub_trace_addr(T, exitno); - MCode *cstart = NULL; - MCode *mcarea = lj_mcode_patch(J, p, 0); - int clearso = 0, patchlong = 1; - for (; p < pe; p++) { - /* Look for exitstub branch, try to replace with branch to target. */ - uint32_t ins = *p; - if ((ins & 0xfc000000u) == 0x40000000u && - ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) { - ptrdiff_t delta = (char *)target - (char *)p; - if (((ins >> 16) & 3) == (CC_SO&3)) { - clearso = sizeof(MCode); - delta -= sizeof(MCode); - } - /* Many, but not all short-range branches can be patched directly. */ - if (p[-1] == PPC_NOPATCH_GC_CHECK) { - patchlong = 0; - } else if (((delta + 0x8000) >> 16) == 0) { - *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) | - ((delta & 0x8000) * (PPCF_Y/0x8000)); - if (!cstart) cstart = p; - } - } else if ((ins & 0xfc000000u) == PPCI_B && - ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) { - ptrdiff_t delta = (char *)target - (char *)p; - lj_assertJ(((delta + 0x02000000) >> 26) == 0, - "branch target out of range"); - *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu); - if (!cstart) cstart = p; - } - } - /* Always patch long-range branch in exit stub itself. Except, if we can't. */ - if (patchlong) { - ptrdiff_t delta = (char *)target - (char *)px - clearso; - lj_assertJ(((delta + 0x02000000) >> 26) == 0, - "branch target out of range"); - *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu); - } - if (!cstart) cstart = px; - lj_mcode_sync(cstart, px+1); - if (clearso) { /* Extend the current trace. Ugly workaround. */ - MCode *pp = J->cur.mcode; - J->cur.szmcode += sizeof(MCode); - *--pp = PPCI_MCRXR; /* Clear SO flag. */ - J->cur.mcode = pp; - lj_mcode_sync(pp, pp+1); - } - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_x86.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_x86.h deleted file mode 100644 index 2bf9d93..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_asm_x86.h +++ /dev/null @@ -1,3125 +0,0 @@ -/* -** x86/x64 IR assembler (SSA IR -> machine code). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Guard handling ------------------------------------------------------ */ - -/* Generate an exit stub group at the bottom of the reserved MCode memory. */ -static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) -{ - ExitNo i, groupofs = (group*EXITSTUBS_PER_GROUP) & 0xff; - MCode *mxp = as->mcbot; - MCode *mxpstart = mxp; - if (mxp + (2+2)*EXITSTUBS_PER_GROUP+8+5 >= as->mctop) - asm_mclimit(as); - /* Push low byte of exitno for each exit stub. */ - *mxp++ = XI_PUSHi8; *mxp++ = (MCode)groupofs; - for (i = 1; i < EXITSTUBS_PER_GROUP; i++) { - *mxp++ = XI_JMPs; *mxp++ = (MCode)((2+2)*(EXITSTUBS_PER_GROUP - i) - 2); - *mxp++ = XI_PUSHi8; *mxp++ = (MCode)(groupofs + i); - } - /* Push the high byte of the exitno for each exit stub group. */ - *mxp++ = XI_PUSHi8; *mxp++ = (MCode)((group*EXITSTUBS_PER_GROUP)>>8); -#if !LJ_GC64 - /* Store DISPATCH at original stack slot 0. Account for the two push ops. */ - *mxp++ = XI_MOVmi; - *mxp++ = MODRM(XM_OFS8, 0, RID_ESP); - *mxp++ = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - *mxp++ = 2*sizeof(void *); - *(int32_t *)mxp = ptr2addr(J2GG(as->J)->dispatch); mxp += 4; -#endif - /* Jump to exit handler which fills in the ExitState. */ - *mxp++ = XI_JMP; mxp += 4; - *((int32_t *)(mxp-4)) = jmprel(as->J, mxp, (MCode *)(void *)lj_vm_exit_handler); - /* Commit the code for this group (even if assembly fails later on). */ - lj_mcode_commitbot(as->J, mxp); - as->mcbot = mxp; - as->mclim = as->mcbot + MCLIM_REDZONE; - return mxpstart; -} - -/* Setup all needed exit stubs. */ -static void asm_exitstub_setup(ASMState *as, ExitNo nexits) -{ - ExitNo i; - if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) - lj_trace_err(as->J, LJ_TRERR_SNAPOV); - for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) - if (as->J->exitstubgroup[i] == NULL) - as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); -} - -/* Emit conditional branch to exit for guard. -** It's important to emit this *after* all registers have been allocated, -** because rematerializations may invalidate the flags. -*/ -static void asm_guardcc(ASMState *as, int cc) -{ - MCode *target = exitstub_addr(as->J, as->snapno); - MCode *p = as->mcp; - if (LJ_UNLIKELY(p == as->invmcp)) { - as->loopinv = 1; - *(int32_t *)(p+1) = jmprel(as->J, p+5, target); - target = p; - cc ^= 1; - if (as->realign) { - if (LJ_GC64 && LJ_UNLIKELY(as->mrm.base == RID_RIP)) - as->mrm.ofs += 2; /* Fixup RIP offset for pending fused load. */ - emit_sjcc(as, cc, target); - return; - } - } - if (LJ_GC64 && LJ_UNLIKELY(as->mrm.base == RID_RIP)) - as->mrm.ofs += 6; /* Fixup RIP offset for pending fused load. */ - emit_jcc(as, cc, target); -} - -/* -- Memory operand fusion ----------------------------------------------- */ - -/* Limit linear search to this distance. Avoids O(n^2) behavior. */ -#define CONFLICT_SEARCH_LIM 31 - -/* Check if a reference is a signed 32 bit constant. */ -static int asm_isk32(ASMState *as, IRRef ref, int32_t *k) -{ - if (irref_isk(ref)) { - IRIns *ir = IR(ref); -#if LJ_GC64 - if (ir->o == IR_KNULL || !irt_is64(ir->t)) { - *k = ir->i; - return 1; - } else if (checki32((int64_t)ir_k64(ir)->u64)) { - *k = (int32_t)ir_k64(ir)->u64; - return 1; - } -#else - if (ir->o != IR_KINT64) { - *k = ir->i; - return 1; - } else if (checki32((int64_t)ir_kint64(ir)->u64)) { - *k = (int32_t)ir_kint64(ir)->u64; - return 1; - } -#endif - } - return 0; -} - -/* Check if there's no conflicting instruction between curins and ref. -** Also avoid fusing loads if there are multiple references. -*/ -static int noconflict(ASMState *as, IRRef ref, IROp conflict, int noload) -{ - IRIns *ir = as->ir; - IRRef i = as->curins; - if (i > ref + CONFLICT_SEARCH_LIM) - return 0; /* Give up, ref is too far away. */ - while (--i > ref) { - if (ir[i].o == conflict) - return 0; /* Conflict found. */ - else if (!noload && (ir[i].op1 == ref || ir[i].op2 == ref)) - return 0; - } - return 1; /* Ok, no conflict. */ -} - -/* Fuse array base into memory operand. */ -static IRRef asm_fuseabase(ASMState *as, IRRef ref) -{ - IRIns *irb = IR(ref); - as->mrm.ofs = 0; - if (irb->o == IR_FLOAD) { - IRIns *ira = IR(irb->op1); - lj_assertA(irb->op2 == IRFL_TAB_ARRAY, "expected FLOAD TAB_ARRAY"); - /* We can avoid the FLOAD of t->array for colocated arrays. */ - if (ira->o == IR_TNEW && ira->op1 <= LJ_MAX_COLOSIZE && - !neverfuse(as) && noconflict(as, irb->op1, IR_NEWREF, 1)) { - as->mrm.ofs = (int32_t)sizeof(GCtab); /* Ofs to colocated array. */ - return irb->op1; /* Table obj. */ - } - } else if (irb->o == IR_ADD && irref_isk(irb->op2)) { - /* Fuse base offset (vararg load). */ - as->mrm.ofs = IR(irb->op2)->i; - return irb->op1; - } - return ref; /* Otherwise use the given array base. */ -} - -/* Fuse array reference into memory operand. */ -static void asm_fusearef(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irx; - lj_assertA(ir->o == IR_AREF, "expected AREF"); - as->mrm.base = (uint8_t)ra_alloc1(as, asm_fuseabase(as, ir->op1), allow); - irx = IR(ir->op2); - if (irref_isk(ir->op2)) { - as->mrm.ofs += 8*irx->i; - as->mrm.idx = RID_NONE; - } else { - rset_clear(allow, as->mrm.base); - as->mrm.scale = XM_SCALE8; - /* Fuse a constant ADD (e.g. t[i+1]) into the offset. - ** Doesn't help much without ABCelim, but reduces register pressure. - */ - if (!LJ_64 && /* Has bad effects with negative index on x64. */ - mayfuse(as, ir->op2) && ra_noreg(irx->r) && - irx->o == IR_ADD && irref_isk(irx->op2)) { - as->mrm.ofs += 8*IR(irx->op2)->i; - as->mrm.idx = (uint8_t)ra_alloc1(as, irx->op1, allow); - } else { - as->mrm.idx = (uint8_t)ra_alloc1(as, ir->op2, allow); - } - } -} - -/* Fuse array/hash/upvalue reference into memory operand. -** Caveat: this may allocate GPRs for the base/idx registers. Be sure to -** pass the final allow mask, excluding any GPRs used for other inputs. -** In particular: 2-operand GPR instructions need to call ra_dest() first! -*/ -static void asm_fuseahuref(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_noreg(ir->r)) { - switch ((IROp)ir->o) { - case IR_AREF: - if (mayfuse(as, ref)) { - asm_fusearef(as, ir, allow); - return; - } - break; - case IR_HREFK: - if (mayfuse(as, ref)) { - as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow); - as->mrm.ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); - as->mrm.idx = RID_NONE; - return; - } - break; - case IR_UREFC: - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv; -#if LJ_GC64 - int64_t ofs = dispofs(as, &uv->tv); - if (checki32(ofs) && checki32(ofs+4)) { - as->mrm.ofs = (int32_t)ofs; - as->mrm.base = RID_DISPATCH; - as->mrm.idx = RID_NONE; - return; - } -#else - as->mrm.ofs = ptr2addr(&uv->tv); - as->mrm.base = as->mrm.idx = RID_NONE; - return; -#endif - } - break; - case IR_TMPREF: -#if LJ_GC64 - as->mrm.ofs = (int32_t)dispofs(as, &J2G(as->J)->tmptv); - as->mrm.base = RID_DISPATCH; - as->mrm.idx = RID_NONE; -#else - as->mrm.ofs = igcptr(&J2G(as->J)->tmptv); - as->mrm.base = as->mrm.idx = RID_NONE; -#endif - return; - default: - break; - } - } - as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow); - as->mrm.ofs = 0; - as->mrm.idx = RID_NONE; -} - -/* Fuse FLOAD/FREF reference into memory operand. */ -static void asm_fusefref(ASMState *as, IRIns *ir, RegSet allow) -{ - lj_assertA(ir->o == IR_FLOAD || ir->o == IR_FREF, - "bad IR op %d", ir->o); - as->mrm.idx = RID_NONE; - if (ir->op1 == REF_NIL) { /* FLOAD from GG_State with offset. */ -#if LJ_GC64 - as->mrm.ofs = (int32_t)(ir->op2 << 2) - GG_OFS(dispatch); - as->mrm.base = RID_DISPATCH; -#else - as->mrm.ofs = (int32_t)(ir->op2 << 2) + ptr2addr(J2GG(as->J)); - as->mrm.base = RID_NONE; -#endif - return; - } - as->mrm.ofs = field_ofs[ir->op2]; - if (irref_isk(ir->op1)) { - IRIns *op1 = IR(ir->op1); -#if LJ_GC64 - if (ir->op1 == REF_NIL) { - as->mrm.ofs -= GG_OFS(dispatch); - as->mrm.base = RID_DISPATCH; - return; - } else if (op1->o == IR_KPTR || op1->o == IR_KKPTR) { - intptr_t ofs = dispofs(as, ir_kptr(op1)); - if (checki32(as->mrm.ofs + ofs)) { - as->mrm.ofs += (int32_t)ofs; - as->mrm.base = RID_DISPATCH; - return; - } - } -#else - as->mrm.ofs += op1->i; - as->mrm.base = RID_NONE; - return; -#endif - } - as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow); -} - -/* Fuse string reference into memory operand. */ -static void asm_fusestrref(ASMState *as, IRIns *ir, RegSet allow) -{ - IRIns *irr; - lj_assertA(ir->o == IR_STRREF, "bad IR op %d", ir->o); - as->mrm.base = as->mrm.idx = RID_NONE; - as->mrm.scale = XM_SCALE1; - as->mrm.ofs = sizeof(GCstr); - if (!LJ_GC64 && irref_isk(ir->op1)) { - as->mrm.ofs += IR(ir->op1)->i; - } else { - Reg r = ra_alloc1(as, ir->op1, allow); - rset_clear(allow, r); - as->mrm.base = (uint8_t)r; - } - irr = IR(ir->op2); - if (irref_isk(ir->op2)) { - as->mrm.ofs += irr->i; - } else { - Reg r; - /* Fuse a constant add into the offset, e.g. string.sub(s, i+10). */ - if (!LJ_64 && /* Has bad effects with negative index on x64. */ - mayfuse(as, ir->op2) && irr->o == IR_ADD && irref_isk(irr->op2)) { - as->mrm.ofs += IR(irr->op2)->i; - r = ra_alloc1(as, irr->op1, allow); - } else { - r = ra_alloc1(as, ir->op2, allow); - } - if (as->mrm.base == RID_NONE) - as->mrm.base = (uint8_t)r; - else - as->mrm.idx = (uint8_t)r; - } -} - -static void asm_fusexref(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - as->mrm.idx = RID_NONE; - if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { -#if LJ_GC64 - intptr_t ofs = dispofs(as, ir_kptr(ir)); - if (checki32(ofs)) { - as->mrm.ofs = (int32_t)ofs; - as->mrm.base = RID_DISPATCH; - return; - } - } if (0) { -#else - as->mrm.ofs = ir->i; - as->mrm.base = RID_NONE; - } else if (ir->o == IR_STRREF) { - asm_fusestrref(as, ir, allow); -#endif - } else { - as->mrm.ofs = 0; - if (canfuse(as, ir) && ir->o == IR_ADD && ra_noreg(ir->r)) { - /* Gather (base+idx*sz)+ofs as emitted by cdata ptr/array indexing. */ - IRIns *irx; - IRRef idx; - Reg r; - if (asm_isk32(as, ir->op2, &as->mrm.ofs)) { /* Recognize x+ofs. */ - ref = ir->op1; - ir = IR(ref); - if (!(ir->o == IR_ADD && canfuse(as, ir) && ra_noreg(ir->r))) - goto noadd; - } - as->mrm.scale = XM_SCALE1; - idx = ir->op1; - ref = ir->op2; - irx = IR(idx); - if (!(irx->o == IR_BSHL || irx->o == IR_ADD)) { /* Try other operand. */ - idx = ir->op2; - ref = ir->op1; - irx = IR(idx); - } - if (canfuse(as, irx) && ra_noreg(irx->r)) { - if (irx->o == IR_BSHL && irref_isk(irx->op2) && IR(irx->op2)->i <= 3) { - /* Recognize idx<op1; - as->mrm.scale = (uint8_t)(IR(irx->op2)->i << 6); - } else if (irx->o == IR_ADD && irx->op1 == irx->op2) { - /* FOLD does idx*2 ==> idx<<1 ==> idx+idx. */ - idx = irx->op1; - as->mrm.scale = XM_SCALE2; - } - } - r = ra_alloc1(as, idx, allow); - rset_clear(allow, r); - as->mrm.idx = (uint8_t)r; - } - noadd: - as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow); - } -} - -/* Fuse load of 64 bit IR constant into memory operand. */ -static Reg asm_fuseloadk64(ASMState *as, IRIns *ir) -{ - const uint64_t *k = &ir_k64(ir)->u64; - if (!LJ_GC64 || checki32((intptr_t)k)) { - as->mrm.ofs = ptr2addr(k); - as->mrm.base = RID_NONE; -#if LJ_GC64 - } else if (checki32(dispofs(as, k))) { - as->mrm.ofs = (int32_t)dispofs(as, k); - as->mrm.base = RID_DISPATCH; - } else if (checki32(mcpofs(as, k)) && checki32(mcpofs(as, k+1)) && - checki32(mctopofs(as, k)) && checki32(mctopofs(as, k+1))) { - as->mrm.ofs = (int32_t)mcpofs(as, k); - as->mrm.base = RID_RIP; - } else { /* Intern 64 bit constant at bottom of mcode. */ - if (ir->i) { - lj_assertA(*k == *(uint64_t*)(as->mctop - ir->i), - "bad interned 64 bit constant"); - } else { - while ((uintptr_t)as->mcbot & 7) *as->mcbot++ = XI_INT3; - *(uint64_t*)as->mcbot = *k; - ir->i = (int32_t)(as->mctop - as->mcbot); - as->mcbot += 8; - as->mclim = as->mcbot + MCLIM_REDZONE; - lj_mcode_commitbot(as->J, as->mcbot); - } - as->mrm.ofs = (int32_t)mcpofs(as, as->mctop - ir->i); - as->mrm.base = RID_RIP; -#endif - } - as->mrm.idx = RID_NONE; - return RID_MRM; -} - -/* Fuse load into memory operand. -** -** Important caveat: this may emit RIP-relative loads! So don't place any -** code emitters between this function and the use of its result. -** The only permitted exception is asm_guardcc(). -*/ -static Reg asm_fuseload(ASMState *as, IRRef ref, RegSet allow) -{ - IRIns *ir = IR(ref); - if (ra_hasreg(ir->r)) { - if (allow != RSET_EMPTY) { /* Fast path. */ - ra_noweak(as, ir->r); - return ir->r; - } - fusespill: - /* Force a spill if only memory operands are allowed (asm_x87load). */ - as->mrm.base = RID_ESP; - as->mrm.ofs = ra_spill(as, ir); - as->mrm.idx = RID_NONE; - return RID_MRM; - } - if (ir->o == IR_KNUM) { - RegSet avail = as->freeset & ~as->modset & RSET_FPR; - lj_assertA(allow != RSET_EMPTY, "no register allowed"); - if (!(avail & (avail-1))) /* Fuse if less than two regs available. */ - return asm_fuseloadk64(as, ir); - } else if (ref == REF_BASE || ir->o == IR_KINT64) { - RegSet avail = as->freeset & ~as->modset & RSET_GPR; - lj_assertA(allow != RSET_EMPTY, "no register allowed"); - if (!(avail & (avail-1))) { /* Fuse if less than two regs available. */ - if (ref == REF_BASE) { -#if LJ_GC64 - as->mrm.ofs = (int32_t)dispofs(as, &J2G(as->J)->jit_base); - as->mrm.base = RID_DISPATCH; -#else - as->mrm.ofs = ptr2addr(&J2G(as->J)->jit_base); - as->mrm.base = RID_NONE; -#endif - as->mrm.idx = RID_NONE; - return RID_MRM; - } else { - return asm_fuseloadk64(as, ir); - } - } - } else if (mayfuse(as, ref)) { - RegSet xallow = (allow & RSET_GPR) ? allow : RSET_GPR; - if (ir->o == IR_SLOAD) { - if (!(ir->op2 & (IRSLOAD_PARENT|IRSLOAD_CONVERT)) && - noconflict(as, ref, IR_RETF, 0) && - !(LJ_GC64 && irt_isaddr(ir->t))) { - as->mrm.base = (uint8_t)ra_alloc1(as, REF_BASE, xallow); - as->mrm.ofs = 8*((int32_t)ir->op1-1-LJ_FR2) + - (!LJ_FR2 && (ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - as->mrm.idx = RID_NONE; - return RID_MRM; - } - } else if (ir->o == IR_FLOAD) { - /* Generic fusion is only ok for 32 bit operand (but see asm_comp). */ - if ((irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)) && - noconflict(as, ref, IR_FSTORE, 0)) { - asm_fusefref(as, ir, xallow); - return RID_MRM; - } - } else if (ir->o == IR_ALOAD || ir->o == IR_HLOAD || ir->o == IR_ULOAD) { - if (noconflict(as, ref, ir->o + IRDELTA_L2S, 0) && - !(LJ_GC64 && irt_isaddr(ir->t))) { - asm_fuseahuref(as, ir->op1, xallow); - return RID_MRM; - } - } else if (ir->o == IR_XLOAD) { - /* Generic fusion is not ok for 8/16 bit operands (but see asm_comp). - ** Fusing unaligned memory operands is ok on x86 (except for SIMD types). - */ - if ((!irt_typerange(ir->t, IRT_I8, IRT_U16)) && - noconflict(as, ref, IR_XSTORE, 0)) { - asm_fusexref(as, ir->op1, xallow); - return RID_MRM; - } - } else if (ir->o == IR_VLOAD && IR(ir->op1)->o == IR_AREF && - !(LJ_GC64 && irt_isaddr(ir->t))) { - asm_fuseahuref(as, ir->op1, xallow); - as->mrm.ofs += 8 * ir->op2; - return RID_MRM; - } - } - if (ir->o == IR_FLOAD && ir->op1 == REF_NIL) { - asm_fusefref(as, ir, RSET_EMPTY); - return RID_MRM; - } - if (!(as->freeset & allow) && !emit_canremat(ref) && - (allow == RSET_EMPTY || ra_hasspill(ir->s) || iscrossref(as, ref))) - goto fusespill; - return ra_allocref(as, ref, allow); -} - -#if LJ_64 -/* Don't fuse a 32 bit load into a 64 bit operation. */ -static Reg asm_fuseloadm(ASMState *as, IRRef ref, RegSet allow, int is64) -{ - if (is64 && !irt_is64(IR(ref)->t)) - return ra_alloc1(as, ref, allow); - return asm_fuseload(as, ref, allow); -} -#else -#define asm_fuseloadm(as, ref, allow, is64) asm_fuseload(as, (ref), (allow)) -#endif - -/* -- Calls --------------------------------------------------------------- */ - -/* Count the required number of stack slots for a call. */ -static int asm_count_call_slots(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t i, nargs = CCI_XNARGS(ci); - int nslots = 0; -#if LJ_64 - if (LJ_ABI_WIN) { - nslots = (int)(nargs*2); /* Only matters for more than four args. */ - } else { - int ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; - for (i = 0; i < nargs; i++) - if (args[i] && irt_isfp(IR(args[i])->t)) { - if (nfpr > 0) nfpr--; else nslots += 2; - } else { - if (ngpr > 0) ngpr--; else nslots += 2; - } - } -#else - int ngpr = 0; - if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL) - ngpr = 2; - else if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL) - ngpr = 1; - for (i = 0; i < nargs; i++) - if (args[i] && irt_isfp(IR(args[i])->t)) { - nslots += irt_isnum(IR(args[i])->t) ? 2 : 1; - } else { - if (ngpr > 0) ngpr--; else nslots++; - } -#endif - return nslots; -} - -/* Generate a call to a C function. */ -static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) -{ - uint32_t n, nargs = CCI_XNARGS(ci); - int32_t ofs = STACKARG_OFS; -#if LJ_64 - uint32_t gprs = REGARG_GPRS; - Reg fpr = REGARG_FIRSTFPR; -#if !LJ_ABI_WIN - MCode *patchnfpr = NULL; -#endif -#else - uint32_t gprs = 0; - if ((ci->flags & CCI_CC_MASK) != CCI_CC_CDECL) { - if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL) - gprs = (REGARG_GPRS & 31); - else if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL) - gprs = REGARG_GPRS; - } -#endif - if ((void *)ci->func) - emit_call(as, ci->func); -#if LJ_64 - if ((ci->flags & CCI_VARARG)) { /* Special handling for vararg calls. */ -#if LJ_ABI_WIN - for (n = 0; n < 4 && n < nargs; n++) { - IRIns *ir = IR(args[n]); - if (irt_isfp(ir->t)) /* Duplicate FPRs in GPRs. */ - emit_rr(as, XO_MOVDto, (irt_isnum(ir->t) ? REX_64 : 0) | (fpr+n), - ((gprs >> (n*5)) & 31)); /* Either MOVD or MOVQ. */ - } -#else - patchnfpr = --as->mcp; /* Indicate number of used FPRs in register al. */ - *--as->mcp = XI_MOVrib | RID_EAX; -#endif - } -#endif - for (n = 0; n < nargs; n++) { /* Setup args. */ - IRRef ref = args[n]; - IRIns *ir = IR(ref); - Reg r; -#if LJ_64 && LJ_ABI_WIN - /* Windows/x64 argument registers are strictly positional. */ - r = irt_isfp(ir->t) ? (fpr <= REGARG_LASTFPR ? fpr : 0) : (gprs & 31); - fpr++; gprs >>= 5; -#elif LJ_64 - /* POSIX/x64 argument registers are used in order of appearance. */ - if (irt_isfp(ir->t)) { - r = fpr <= REGARG_LASTFPR ? fpr++ : 0; - } else { - r = gprs & 31; gprs >>= 5; - } -#else - if (ref && irt_isfp(ir->t)) { - r = 0; - } else { - r = gprs & 31; gprs >>= 5; - if (!ref) continue; - } -#endif - if (r) { /* Argument is in a register. */ - if (r < RID_MAX_GPR && ref < ASMREF_TMP1) { -#if LJ_64 - if (LJ_GC64 ? !(ir->o == IR_KINT || ir->o == IR_KNULL) : ir->o == IR_KINT64) - emit_loadu64(as, r, ir_k64(ir)->u64); - else -#endif - emit_loadi(as, r, ir->i); - } else { - /* Must have been evicted. */ - lj_assertA(rset_test(as->freeset, r), "reg %d not free", r); - if (ra_hasreg(ir->r)) { - ra_noweak(as, ir->r); - emit_movrr(as, ir, r, ir->r); - } else { - ra_allocref(as, ref, RID2RSET(r)); - } - } - } else if (irt_isfp(ir->t)) { /* FP argument is on stack. */ - lj_assertA(!(irt_isfloat(ir->t) && irref_isk(ref)), - "unexpected float constant"); - if (LJ_32 && (ofs & 4) && irref_isk(ref)) { - /* Split stores for unaligned FP consts. */ - emit_movmroi(as, RID_ESP, ofs, (int32_t)ir_knum(ir)->u32.lo); - emit_movmroi(as, RID_ESP, ofs+4, (int32_t)ir_knum(ir)->u32.hi); - } else { - r = ra_alloc1(as, ref, RSET_FPR); - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, - r, RID_ESP, ofs); - } - ofs += (LJ_32 && irt_isfloat(ir->t)) ? 4 : 8; - } else { /* Non-FP argument is on stack. */ - if (LJ_32 && ref < ASMREF_TMP1) { - emit_movmroi(as, RID_ESP, ofs, ir->i); - } else { - r = ra_alloc1(as, ref, RSET_GPR); - emit_movtomro(as, REX_64 + r, RID_ESP, ofs); - } - ofs += sizeof(intptr_t); - } - checkmclim(as); - } -#if LJ_64 && !LJ_ABI_WIN - if (patchnfpr) *patchnfpr = fpr - REGARG_FIRSTFPR; -#endif -} - -/* Setup result reg/sp for call. Evict scratch regs. */ -static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - RegSet drop = RSET_SCRATCH; - int hiop = ((ir+1)->o == IR_HIOP && !irt_isnil((ir+1)->t)); - if ((ci->flags & CCI_NOFPRCLOBBER)) - drop &= ~RSET_FPR; - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - if (hiop && ra_hasreg((ir+1)->r)) - rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ - ra_evictset(as, drop); /* Evictions must be performed first. */ - if (ra_used(ir)) { - if (irt_isfp(ir->t)) { - int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ -#if LJ_64 - if ((ci->flags & CCI_CASTU64)) { - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rr(as, XO_MOVD, dest|REX_64, RID_RET); /* Really MOVQ. */ - } - if (ofs) emit_movtomro(as, RID_RET|REX_64, RID_ESP, ofs); - } else { - ra_destreg(as, ir, RID_FPRET); - } -#else - /* Number result is in x87 st0 for x86 calling convention. */ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, - dest, RID_ESP, ofs); - } - if ((ci->flags & CCI_CASTU64)) { - emit_movtomro(as, RID_RETLO, RID_ESP, ofs); - emit_movtomro(as, RID_RETHI, RID_ESP, ofs+4); - } else { - emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd, - irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs); - } -#endif - } else if (hiop) { - ra_destpair(as, ir); - } else { - lj_assertA(!irt_ispri(ir->t), "PRI dest"); - ra_destreg(as, ir, RID_RET); - } - } else if (LJ_32 && irt_isfp(ir->t) && !(ci->flags & CCI_CASTU64)) { - emit_x87op(as, XI_FPOP); /* Pop unused result from x87 st0. */ - } -} - -/* Return a constant function pointer or NULL for indirect calls. */ -static void *asm_callx_func(ASMState *as, IRIns *irf, IRRef func) -{ -#if LJ_32 - UNUSED(as); - if (irref_isk(func)) - return (void *)irf->i; -#else - if (irref_isk(func)) { - MCode *p; - if (irf->o == IR_KINT64) - p = (MCode *)(void *)ir_k64(irf)->u64; - else - p = (MCode *)(void *)(uintptr_t)(uint32_t)irf->i; - if (p - as->mcp == (int32_t)(p - as->mcp)) - return p; /* Call target is still in +-2GB range. */ - /* Avoid the indirect case of emit_call(). Try to hoist func addr. */ - } -#endif - return NULL; -} - -static void asm_callx(ASMState *as, IRIns *ir) -{ - IRRef args[CCI_NARGS_MAX*2]; - CCallInfo ci; - IRRef func; - IRIns *irf; - int32_t spadj = 0; - ci.flags = asm_callx_flags(as, ir); - asm_collectargs(as, ir, &ci, args); - asm_setupresult(as, ir, &ci); -#if LJ_32 - /* Have to readjust stack after non-cdecl calls due to callee cleanup. */ - if ((ci.flags & CCI_CC_MASK) != CCI_CC_CDECL) - spadj = 4 * asm_count_call_slots(as, &ci, args); -#endif - func = ir->op2; irf = IR(func); - if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } - ci.func = (ASMFunction)asm_callx_func(as, irf, func); - if (!(void *)ci.func) { - /* Use a (hoistable) non-scratch register for indirect calls. */ - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); - Reg r = ra_alloc1(as, func, allow); - if (LJ_32) emit_spsub(as, spadj); /* Above code may cause restores! */ - emit_rr(as, XO_GROUP5, XOg_CALL, r); - } else if (LJ_32) { - emit_spsub(as, spadj); - } - asm_gencall(as, &ci, args); -} - -/* -- Returns ------------------------------------------------------------- */ - -/* Return to lower frame. Guard that it goes to the right spot. */ -static void asm_retf(ASMState *as, IRIns *ir) -{ - Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); -#if LJ_FR2 - Reg rpc = ra_scratch(as, rset_exclude(RSET_GPR, base)); -#endif - void *pc = ir_kptr(IR(ir->op2)); - int32_t delta = 1+LJ_FR2+bc_a(*((const BCIns *)pc - 1)); - as->topslot -= (BCReg)delta; - if ((int32_t)as->topslot < 0) as->topslot = 0; - irt_setmark(IR(REF_BASE)->t); /* Children must not coalesce with BASE reg. */ - emit_setgl(as, base, jit_base); - emit_addptr(as, base, -8*delta); - asm_guardcc(as, CC_NE); -#if LJ_FR2 - emit_rmro(as, XO_CMP, rpc|REX_GC64, base, -8); - emit_loadu64(as, rpc, u64ptr(pc)); -#else - emit_gmroi(as, XG_ARITHi(XOg_CMP), base, -4, ptr2addr(pc)); -#endif -} - -/* -- Buffer operations --------------------------------------------------- */ - -#if LJ_HASBUFFER -static void asm_bufhdr_write(ASMState *as, Reg sb) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, sb)); - IRIns irgc; - irgc.ot = IRT(0, IRT_PGC); /* GC type. */ - emit_storeofs(as, &irgc, tmp, sb, offsetof(SBuf, L)); - emit_opgl(as, XO_ARITH(XOg_OR), tmp|REX_GC64, cur_L); - emit_gri(as, XG_ARITHi(XOg_AND), tmp, SBUF_MASK_FLAG); - emit_loadofs(as, &irgc, tmp, sb, offsetof(SBuf, L)); -} -#endif - -/* -- Type conversions ---------------------------------------------------- */ - -static void asm_tointg(ASMState *as, IRIns *ir, Reg left) -{ - Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_guardcc(as, CC_P); - asm_guardcc(as, CC_NE); - emit_rr(as, XO_UCOMISD, left, tmp); - emit_rr(as, XO_CVTSI2SD, tmp, dest); - emit_rr(as, XO_XORPS, tmp, tmp); /* Avoid partial register stall. */ - emit_rr(as, XO_CVTTSD2SI, dest, left); - /* Can't fuse since left is needed twice. */ -} - -static void asm_tobit(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - Reg tmp = ra_noreg(IR(ir->op1)->r) ? - ra_alloc1(as, ir->op1, RSET_FPR) : - ra_scratch(as, RSET_FPR); - Reg right; - emit_rr(as, XO_MOVDto, tmp, dest); - right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp)); - emit_mrm(as, XO_ADDSD, tmp, right); - ra_left(as, tmp, ir->op1); -} - -static void asm_conv(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - int st64 = (st == IRT_I64 || st == IRT_U64 || (LJ_64 && st == IRT_P64)); - int stfp = (st == IRT_NUM || st == IRT_FLOAT); - IRRef lref = ir->op1; - lj_assertA(irt_type(ir->t) != st, "inconsistent types for CONV"); - lj_assertA(!(LJ_32 && (irt_isint64(ir->t) || st64)), - "IR %04d has unsplit 64 bit type", - (int)(ir - as->ir) - REF_BIAS); - if (irt_isfp(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_FPR); - if (stfp) { /* FP to FP conversion. */ - Reg left = asm_fuseload(as, lref, RSET_FPR); - emit_mrm(as, st == IRT_NUM ? XO_CVTSD2SS : XO_CVTSS2SD, dest, left); - if (left == dest) return; /* Avoid the XO_XORPS. */ - } else if (LJ_32 && st == IRT_U32) { /* U32 to FP conversion on x86. */ - /* number = (2^52+2^51 .. u32) - (2^52+2^51) */ - cTValue *k = &as->J->k64[LJ_K64_TOBIT]; - Reg bias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); - if (irt_isfloat(ir->t)) - emit_rr(as, XO_CVTSD2SS, dest, dest); - emit_rr(as, XO_SUBSD, dest, bias); /* Subtract 2^52+2^51 bias. */ - emit_rr(as, XO_XORPS, dest, bias); /* Merge bias and integer. */ - emit_rma(as, XO_MOVSD, bias, k); - emit_mrm(as, XO_MOVD, dest, asm_fuseload(as, lref, RSET_GPR)); - return; - } else { /* Integer to FP conversion. */ - Reg left = (LJ_64 && (st == IRT_U32 || st == IRT_U64)) ? - ra_alloc1(as, lref, RSET_GPR) : - asm_fuseloadm(as, lref, RSET_GPR, st64); - if (LJ_64 && st == IRT_U64) { - MCLabel l_end = emit_label(as); - cTValue *k = &as->J->k64[LJ_K64_2P64]; - emit_rma(as, XO_ADDSD, dest, k); /* Add 2^64 to compensate. */ - emit_sjcc(as, CC_NS, l_end); - emit_rr(as, XO_TEST, left|REX_64, left); /* Check if u64 >= 2^63. */ - } - emit_mrm(as, irt_isnum(ir->t) ? XO_CVTSI2SD : XO_CVTSI2SS, - dest|((LJ_64 && (st64 || st == IRT_U32)) ? REX_64 : 0), left); - } - emit_rr(as, XO_XORPS, dest, dest); /* Avoid partial register stall. */ - } else if (stfp) { /* FP to integer conversion. */ - if (irt_isguard(ir->t)) { - /* Checked conversions are only supported from number to int. */ - lj_assertA(irt_isint(ir->t) && st == IRT_NUM, - "bad type for checked CONV"); - asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - x86Op op = st == IRT_NUM ? XO_CVTTSD2SI : XO_CVTTSS2SI; - if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) { - /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */ - /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */ - Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) : - ra_scratch(as, RSET_FPR); - MCLabel l_end = emit_label(as); - if (LJ_32) - emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000); - emit_rr(as, op, dest|REX_64, tmp); - if (st == IRT_NUM) - emit_rma(as, XO_ADDSD, tmp, &as->J->k64[LJ_K64_M2P64_31]); - else - emit_rma(as, XO_ADDSS, tmp, &as->J->k32[LJ_K32_M2P64_31]); - emit_sjcc(as, CC_NS, l_end); - emit_rr(as, XO_TEST, dest|REX_64, dest); /* Check if dest negative. */ - emit_rr(as, op, dest|REX_64, tmp); - ra_left(as, tmp, lref); - } else { - if (LJ_64 && irt_isu32(ir->t)) - emit_rr(as, XO_MOV, dest, dest); /* Zero hiword. */ - emit_mrm(as, op, - dest|((LJ_64 && - (irt_is64(ir->t) || irt_isu32(ir->t))) ? REX_64 : 0), - asm_fuseload(as, lref, RSET_FPR)); - } - } - } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ - Reg left, dest = ra_dest(as, ir, RSET_GPR); - RegSet allow = RSET_GPR; - x86Op op; - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t), "bad type for CONV EXT"); - if (st == IRT_I8) { - op = XO_MOVSXb; allow = RSET_GPR8; dest |= FORCE_REX; - } else if (st == IRT_U8) { - op = XO_MOVZXb; allow = RSET_GPR8; dest |= FORCE_REX; - } else if (st == IRT_I16) { - op = XO_MOVSXw; - } else { - op = XO_MOVZXw; - } - left = asm_fuseload(as, lref, allow); - /* Add extra MOV if source is already in wrong register. */ - if (!LJ_64 && left != RID_MRM && !rset_test(allow, left)) { - Reg tmp = ra_scratch(as, allow); - emit_rr(as, op, dest, tmp); - emit_rr(as, XO_MOV, tmp, left); - } else { - emit_mrm(as, op, dest, left); - } - } else { /* 32/64 bit integer conversions. */ - if (LJ_32) { /* Only need to handle 32/32 bit no-op (cast) on x86. */ - Reg dest = ra_dest(as, ir, RSET_GPR); - ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else if (irt_is64(ir->t)) { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st64 || !(ir->op2 & IRCONV_SEXT)) { - /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */ - ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ - } else { /* 32 to 64 bit sign extension. */ - Reg left = asm_fuseload(as, lref, RSET_GPR); - emit_mrm(as, XO_MOVSXd, dest|REX_64, left); - } - } else { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (st64 && !(ir->op2 & IRCONV_NONE)) { - Reg left = asm_fuseload(as, lref, RSET_GPR); - /* This is either a 32 bit reg/reg mov which zeroes the hiword - ** or a load of the loword from a 64 bit address. - */ - emit_mrm(as, XO_MOV, dest, left); - } else { /* 32/32 bit no-op (cast). */ - ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ - } - } - } -} - -#if LJ_32 && LJ_HASFFI -/* No SSE conversions to/from 64 bit on x86, so resort to ugly x87 code. */ - -/* 64 bit integer to FP conversion in 32 bit mode. */ -static void asm_conv_fp_int64(ASMState *as, IRIns *ir) -{ - Reg hi = ra_alloc1(as, ir->op1, RSET_GPR); - Reg lo = ra_alloc1(as, (ir-1)->op1, rset_exclude(RSET_GPR, hi)); - int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, dest, RID_ESP, ofs); - } - emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd, - irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs); - if (((ir-1)->op2 & IRCONV_SRCMASK) == IRT_U64) { - /* For inputs in [2^63,2^64-1] add 2^64 to compensate. */ - MCLabel l_end = emit_label(as); - emit_rma(as, XO_FADDq, XOg_FADDq, &as->J->k64[LJ_K64_2P64]); - emit_sjcc(as, CC_NS, l_end); - emit_rr(as, XO_TEST, hi, hi); /* Check if u64 >= 2^63. */ - } else { - lj_assertA(((ir-1)->op2 & IRCONV_SRCMASK) == IRT_I64, "bad type for CONV"); - } - emit_rmro(as, XO_FILDq, XOg_FILDq, RID_ESP, 0); - /* NYI: Avoid narrow-to-wide store-to-load forwarding stall. */ - emit_rmro(as, XO_MOVto, hi, RID_ESP, 4); - emit_rmro(as, XO_MOVto, lo, RID_ESP, 0); -} - -/* FP to 64 bit integer conversion in 32 bit mode. */ -static void asm_conv_int64_fp(ASMState *as, IRIns *ir) -{ - IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); - IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); - Reg lo, hi; - lj_assertA(st == IRT_NUM || st == IRT_FLOAT, "bad type for CONV"); - lj_assertA(dt == IRT_I64 || dt == IRT_U64, "bad type for CONV"); - hi = ra_dest(as, ir, RSET_GPR); - lo = ra_dest(as, ir-1, rset_exclude(RSET_GPR, hi)); - if (ra_used(ir-1)) emit_rmro(as, XO_MOV, lo, RID_ESP, 0); - /* NYI: Avoid wide-to-narrow store-to-load forwarding stall. */ - if (!(as->flags & JIT_F_SSE3)) { /* Set FPU rounding mode to default. */ - emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 4); - emit_rmro(as, XO_MOVto, lo, RID_ESP, 4); - emit_gri(as, XG_ARITHi(XOg_AND), lo, 0xf3ff); - } - if (dt == IRT_U64) { - /* For inputs in [2^63,2^64-1] add -2^64 and convert again. */ - MCLabel l_pop, l_end = emit_label(as); - emit_x87op(as, XI_FPOP); - l_pop = emit_label(as); - emit_sjmp(as, l_end); - emit_rmro(as, XO_MOV, hi, RID_ESP, 4); - if ((as->flags & JIT_F_SSE3)) - emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0); - else - emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0); - emit_rma(as, XO_FADDq, XOg_FADDq, &as->J->k64[LJ_K64_M2P64]); - emit_sjcc(as, CC_NS, l_pop); - emit_rr(as, XO_TEST, hi, hi); /* Check if out-of-range (2^63). */ - } - emit_rmro(as, XO_MOV, hi, RID_ESP, 4); - if ((as->flags & JIT_F_SSE3)) { /* Truncation is easy with SSE3. */ - emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0); - } else { /* Otherwise set FPU rounding mode to truncate before the store. */ - emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0); - emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 0); - emit_rmro(as, XO_MOVtow, lo, RID_ESP, 0); - emit_rmro(as, XO_ARITHw(XOg_OR), lo, RID_ESP, 0); - emit_loadi(as, lo, 0xc00); - emit_rmro(as, XO_FNSTCW, XOg_FNSTCW, RID_ESP, 0); - } - if (dt == IRT_U64) - emit_x87op(as, XI_FDUP); - emit_mrm(as, st == IRT_NUM ? XO_FLDq : XO_FLDd, - st == IRT_NUM ? XOg_FLDq: XOg_FLDd, - asm_fuseload(as, ir->op1, RSET_EMPTY)); -} - -static void asm_conv64(ASMState *as, IRIns *ir) -{ - if (irt_isfp(ir->t)) - asm_conv_fp_int64(as, ir); - else - asm_conv_int64_fp(as, ir); -} -#endif - -static void asm_strto(ASMState *as, IRIns *ir) -{ - /* Force a spill slot for the destination register (if any). */ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; - IRRef args[2]; - RegSet drop = RSET_SCRATCH; - if ((drop & RSET_FPR) != RSET_FPR && ra_hasreg(ir->r)) - rset_set(drop, ir->r); /* WIN64 doesn't spill all FPRs. */ - ra_evictset(as, drop); - asm_guardcc(as, CC_E); - emit_rr(as, XO_TEST, RID_RET, RID_RET); /* Test return status. */ - args[0] = ir->op1; /* GCstr *str */ - args[1] = ASMREF_TMP1; /* TValue *n */ - asm_gencall(as, ci, args); - /* Store the result to the spill slot or temp slots. */ - emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64, - RID_ESP, sps_scale(ir->s)); -} - -/* -- Memory references --------------------------------------------------- */ - -/* Get pointer to TValue. */ -static void asm_tvptr(ASMState *as, Reg dest, IRRef ref, MSize mode) -{ - if ((mode & IRTMPREF_IN1)) { - IRIns *ir = IR(ref); - if (irt_isnum(ir->t)) { - if (irref_isk(ref) && !(mode & IRTMPREF_OUT1)) { - /* Use the number constant itself as a TValue. */ - emit_loada(as, dest, ir_knum(ir)); - return; - } - emit_rmro(as, XO_MOVSDto, ra_alloc1(as, ref, RSET_FPR), dest, 0); - } else { -#if LJ_GC64 - if (irref_isk(ref)) { - TValue k; - lj_ir_kvalue(as->J->L, &k, ir); - emit_movmroi(as, dest, 4, k.u32.hi); - emit_movmroi(as, dest, 0, k.u32.lo); - } else { - /* TODO: 64 bit store + 32 bit load-modify-store is suboptimal. */ - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, dest)); - if (irt_is64(ir->t)) { - emit_u32(as, irt_toitype(ir->t) << 15); - emit_rmro(as, XO_ARITHi, XOg_OR, dest, 4); - } else { - emit_movmroi(as, dest, 4, (irt_toitype(ir->t) << 15)); - } - emit_movtomro(as, REX_64IR(ir, src), dest, 0); - } -#else - if (!irref_isk(ref)) { - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, dest)); - emit_movtomro(as, REX_64IR(ir, src), dest, 0); - } else if (!irt_ispri(ir->t)) { - emit_movmroi(as, dest, 0, ir->i); - } - if (!(LJ_64 && irt_islightud(ir->t))) - emit_movmroi(as, dest, 4, irt_toitype(ir->t)); -#endif - } - } - emit_loada(as, dest, &J2G(as->J)->tmptv); /* g->tmptv holds the TValue(s). */ -} - -static void asm_aref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_fusearef(as, ir, RSET_GPR); - if (!(as->mrm.idx == RID_NONE && as->mrm.ofs == 0)) - emit_mrm(as, XO_LEA, dest|REX_GC64, RID_MRM); - else if (as->mrm.base != dest) - emit_rr(as, XO_MOV, dest|REX_GC64, as->mrm.base); -} - -/* Inlined hash lookup. Specialized for key type and for const keys. -** The equivalent C code is: -** Node *n = hashkey(t, key); -** do { -** if (lj_obj_equal(&n->key, key)) return &n->val; -** } while ((n = nextnode(n))); -** return niltv(L); -*/ -static void asm_href(ASMState *as, IRIns *ir, IROp merge) -{ - RegSet allow = RSET_GPR; - int destused = ra_used(ir); - Reg dest = ra_dest(as, ir, allow); - Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); - Reg key = RID_NONE, tmp = RID_NONE; - IRIns *irkey = IR(ir->op2); - int isk = irref_isk(ir->op2); - IRType1 kt = irkey->t; - uint32_t khash; - MCLabel l_end, l_loop, l_next; - - if (!isk) { - rset_clear(allow, tab); - key = ra_alloc1(as, ir->op2, irt_isnum(kt) ? RSET_FPR : allow); - if (LJ_GC64 || !irt_isstr(kt)) - tmp = ra_scratch(as, rset_exclude(allow, key)); - } - - /* Key not found in chain: jump to exit (if merged) or load niltv. */ - l_end = emit_label(as); - if (merge == IR_NE) - asm_guardcc(as, CC_E); /* XI_JMP is not found by lj_asm_patchexit. */ - else if (destused) - emit_loada(as, dest, niltvg(J2G(as->J))); - - /* Follow hash chain until the end. */ - l_loop = emit_sjcc_label(as, CC_NZ); - emit_rr(as, XO_TEST, dest|REX_GC64, dest); - emit_rmro(as, XO_MOV, dest|REX_GC64, dest, offsetof(Node, next)); - l_next = emit_label(as); - - /* Type and value comparison. */ - if (merge == IR_EQ) - asm_guardcc(as, CC_E); - else - emit_sjcc(as, CC_E, l_end); - if (irt_isnum(kt)) { - if (isk) { - /* Assumes -0.0 is already canonicalized to +0.0. */ - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo), - (int32_t)ir_knum(irkey)->u32.lo); - emit_sjcc(as, CC_NE, l_next); - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi), - (int32_t)ir_knum(irkey)->u32.hi); - } else { - emit_sjcc(as, CC_P, l_next); - emit_rmro(as, XO_UCOMISD, key, dest, offsetof(Node, key.n)); - emit_sjcc(as, CC_AE, l_next); - /* The type check avoids NaN penalties and complaints from Valgrind. */ -#if LJ_64 && !LJ_GC64 - emit_u32(as, LJ_TISNUM); - emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it)); -#else - emit_i8(as, LJ_TISNUM); - emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it)); -#endif - } -#if LJ_64 && !LJ_GC64 - } else if (irt_islightud(kt)) { - emit_rmro(as, XO_CMP, key|REX_64, dest, offsetof(Node, key.u64)); -#endif -#if LJ_GC64 - } else if (irt_isaddr(kt)) { - if (isk) { - TValue k; - k.u64 = ((uint64_t)irt_toitype(irkey->t) << 47) | irkey[1].tv.u64; - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo), - k.u32.lo); - emit_sjcc(as, CC_NE, l_next); - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi), - k.u32.hi); - } else { - emit_rmro(as, XO_CMP, tmp|REX_64, dest, offsetof(Node, key.u64)); - } - } else { - lj_assertA(irt_ispri(kt) && !irt_isnil(kt), "bad HREF key type"); - emit_u32(as, (irt_toitype(kt)<<15)|0x7fff); - emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it)); -#else - } else { - if (!irt_ispri(kt)) { - lj_assertA(irt_isaddr(kt), "bad HREF key type"); - if (isk) - emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.gcr), - ptr2addr(ir_kgc(irkey))); - else - emit_rmro(as, XO_CMP, key, dest, offsetof(Node, key.gcr)); - emit_sjcc(as, CC_NE, l_next); - } - lj_assertA(!irt_isnil(kt), "bad HREF key type"); - emit_i8(as, irt_toitype(kt)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it)); -#endif - } - emit_sfixup(as, l_loop); - checkmclim(as); -#if LJ_GC64 - if (!isk && irt_isaddr(kt)) { - emit_rr(as, XO_OR, tmp|REX_64, key); - emit_loadu64(as, tmp, (uint64_t)irt_toitype(kt) << 47); - } -#endif - - /* Load main position relative to tab->node into dest. */ - khash = isk ? ir_khash(as, irkey) : 1; - if (khash == 0) { - emit_rmro(as, XO_MOV, dest|REX_GC64, tab, offsetof(GCtab, node)); - } else { - emit_rmro(as, XO_ARITH(XOg_ADD), dest|REX_GC64, tab, offsetof(GCtab,node)); - emit_shifti(as, XOg_SHL, dest, 3); - emit_rmrxo(as, XO_LEA, dest, dest, dest, XM_SCALE2, 0); - if (isk) { - emit_gri(as, XG_ARITHi(XOg_AND), dest, (int32_t)khash); - emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask)); - } else if (irt_isstr(kt)) { - emit_rmro(as, XO_ARITH(XOg_AND), dest, key, offsetof(GCstr, sid)); - emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask)); - } else { /* Must match with hashrot() in lj_tab.c. */ - emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask)); - emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp); - emit_shifti(as, XOg_ROL, tmp, HASH_ROT3); - emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp); - emit_shifti(as, XOg_ROL, dest, HASH_ROT2); - emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest); - emit_shifti(as, XOg_ROL, dest, HASH_ROT1); - emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest); - if (irt_isnum(kt)) { - emit_rr(as, XO_ARITH(XOg_ADD), dest, dest); -#if LJ_64 - emit_shifti(as, XOg_SHR|REX_64, dest, 32); - emit_rr(as, XO_MOV, tmp, dest); - emit_rr(as, XO_MOVDto, key|REX_64, dest); -#else - emit_rmro(as, XO_MOV, dest, RID_ESP, ra_spill(as, irkey)+4); - emit_rr(as, XO_MOVDto, key, tmp); -#endif - } else { - emit_rr(as, XO_MOV, tmp, key); -#if LJ_GC64 - checkmclim(as); - emit_gri(as, XG_ARITHi(XOg_XOR), dest, irt_toitype(kt) << 15); - if ((as->flags & JIT_F_BMI2)) { - emit_i8(as, 32); - emit_mrm(as, XV_RORX|VEX_64, dest, key); - } else { - emit_shifti(as, XOg_SHR|REX_64, dest, 32); - emit_rr(as, XO_MOV, dest|REX_64, key|REX_64); - } -#else - emit_rmro(as, XO_LEA, dest, key, HASH_BIAS); -#endif - } - } - } -} - -static void asm_hrefk(ASMState *as, IRIns *ir) -{ - IRIns *kslot = IR(ir->op2); - IRIns *irkey = IR(kslot->op1); - int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); - Reg dest = ra_used(ir) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; - Reg node = ra_alloc1(as, ir->op1, RSET_GPR); -#if !LJ_64 - MCLabel l_exit; -#endif - lj_assertA(ofs % sizeof(Node) == 0, "unaligned HREFK slot"); - if (ra_hasreg(dest)) { - if (ofs != 0) { - if (dest == node) - emit_gri(as, XG_ARITHi(XOg_ADD), dest|REX_GC64, ofs); - else - emit_rmro(as, XO_LEA, dest|REX_GC64, node, ofs); - } else if (dest != node) { - emit_rr(as, XO_MOV, dest|REX_GC64, node); - } - } - asm_guardcc(as, CC_NE); -#if LJ_64 - if (!irt_ispri(irkey->t)) { - Reg key = ra_scratch(as, rset_exclude(RSET_GPR, node)); - emit_rmro(as, XO_CMP, key|REX_64, node, - ofs + (int32_t)offsetof(Node, key.u64)); - lj_assertA(irt_isnum(irkey->t) || irt_isgcv(irkey->t), - "bad HREFK key type"); - /* Assumes -0.0 is already canonicalized to +0.0. */ - emit_loadu64(as, key, irt_isnum(irkey->t) ? ir_knum(irkey)->u64 : -#if LJ_GC64 - ((uint64_t)irt_toitype(irkey->t) << 47) | - (uint64_t)ir_kgc(irkey)); -#else - ((uint64_t)irt_toitype(irkey->t) << 32) | - (uint64_t)(uint32_t)ptr2addr(ir_kgc(irkey))); -#endif - } else { - lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type"); -#if LJ_GC64 - emit_i32(as, (irt_toitype(irkey->t)<<15)|0x7fff); - emit_rmro(as, XO_ARITHi, XOg_CMP, node, - ofs + (int32_t)offsetof(Node, key.it)); -#else - emit_i8(as, irt_toitype(irkey->t)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, node, - ofs + (int32_t)offsetof(Node, key.it)); -#endif - } -#else - l_exit = emit_label(as); - if (irt_isnum(irkey->t)) { - /* Assumes -0.0 is already canonicalized to +0.0. */ - emit_gmroi(as, XG_ARITHi(XOg_CMP), node, - ofs + (int32_t)offsetof(Node, key.u32.lo), - (int32_t)ir_knum(irkey)->u32.lo); - emit_sjcc(as, CC_NE, l_exit); - emit_gmroi(as, XG_ARITHi(XOg_CMP), node, - ofs + (int32_t)offsetof(Node, key.u32.hi), - (int32_t)ir_knum(irkey)->u32.hi); - } else { - if (!irt_ispri(irkey->t)) { - lj_assertA(irt_isgcv(irkey->t), "bad HREFK key type"); - emit_gmroi(as, XG_ARITHi(XOg_CMP), node, - ofs + (int32_t)offsetof(Node, key.gcr), - ptr2addr(ir_kgc(irkey))); - emit_sjcc(as, CC_NE, l_exit); - } - lj_assertA(!irt_isnil(irkey->t), "bad HREFK key type"); - emit_i8(as, irt_toitype(irkey->t)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, node, - ofs + (int32_t)offsetof(Node, key.it)); - } -#endif -} - -static void asm_uref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - if (irref_isk(ir->op1)) { - GCfunc *fn = ir_kfunc(IR(ir->op1)); - MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; - emit_rma(as, XO_MOV, dest|REX_GC64, v); - } else { - Reg uv = ra_scratch(as, RSET_GPR); - Reg func = ra_alloc1(as, ir->op1, RSET_GPR); - if (ir->o == IR_UREFC) { - emit_rmro(as, XO_LEA, dest|REX_GC64, uv, offsetof(GCupval, tv)); - asm_guardcc(as, CC_NE); - emit_i8(as, 1); - emit_rmro(as, XO_ARITHib, XOg_CMP, uv, offsetof(GCupval, closed)); - } else { - emit_rmro(as, XO_MOV, dest|REX_GC64, uv, offsetof(GCupval, v)); - } - emit_rmro(as, XO_MOV, uv|REX_GC64, func, - (int32_t)offsetof(GCfuncL, uvptr) + - (int32_t)sizeof(MRef) * (int32_t)(ir->op2 >> 8)); - } -} - -static void asm_fref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_fusefref(as, ir, RSET_GPR); - emit_mrm(as, XO_LEA, dest, RID_MRM); -} - -static void asm_strref(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - asm_fusestrref(as, ir, RSET_GPR); - if (as->mrm.base == RID_NONE) - emit_loadi(as, dest, as->mrm.ofs); - else if (as->mrm.base == dest && as->mrm.idx == RID_NONE) - emit_gri(as, XG_ARITHi(XOg_ADD), dest|REX_GC64, as->mrm.ofs); - else - emit_mrm(as, XO_LEA, dest|REX_GC64, RID_MRM); -} - -/* -- Loads and stores ---------------------------------------------------- */ - -static void asm_fxload(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); - x86Op xo; - if (ir->o == IR_FLOAD) - asm_fusefref(as, ir, RSET_GPR); - else - asm_fusexref(as, ir->op1, RSET_GPR); - /* ir->op2 is ignored -- unaligned loads are ok on x86. */ - switch (irt_type(ir->t)) { - case IRT_I8: xo = XO_MOVSXb; break; - case IRT_U8: xo = XO_MOVZXb; break; - case IRT_I16: xo = XO_MOVSXw; break; - case IRT_U16: xo = XO_MOVZXw; break; - case IRT_NUM: xo = XO_MOVSD; break; - case IRT_FLOAT: xo = XO_MOVSS; break; - default: - if (LJ_64 && irt_is64(ir->t)) - dest |= REX_64; - else - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t), - "unsplit 64 bit load"); - xo = XO_MOV; - break; - } - emit_mrm(as, xo, dest, RID_MRM); -} - -#define asm_fload(as, ir) asm_fxload(as, ir) -#define asm_xload(as, ir) asm_fxload(as, ir) - -static void asm_fxstore(ASMState *as, IRIns *ir) -{ - RegSet allow = RSET_GPR; - Reg src = RID_NONE, osrc = RID_NONE; - int32_t k = 0; - if (ir->r == RID_SINK) - return; - /* The IRT_I16/IRT_U16 stores should never be simplified for constant - ** values since mov word [mem], imm16 has a length-changing prefix. - */ - if (irt_isi16(ir->t) || irt_isu16(ir->t) || irt_isfp(ir->t) || - !asm_isk32(as, ir->op2, &k)) { - RegSet allow8 = irt_isfp(ir->t) ? RSET_FPR : - (irt_isi8(ir->t) || irt_isu8(ir->t)) ? RSET_GPR8 : RSET_GPR; - src = osrc = ra_alloc1(as, ir->op2, allow8); - if (!LJ_64 && !rset_test(allow8, src)) { /* Already in wrong register. */ - rset_clear(allow, osrc); - src = ra_scratch(as, allow8); - } - rset_clear(allow, src); - } - if (ir->o == IR_FSTORE) { - asm_fusefref(as, IR(ir->op1), allow); - } else { - asm_fusexref(as, ir->op1, allow); - if (LJ_32 && ir->o == IR_HIOP) as->mrm.ofs += 4; - } - if (ra_hasreg(src)) { - x86Op xo; - switch (irt_type(ir->t)) { - case IRT_I8: case IRT_U8: xo = XO_MOVtob; src |= FORCE_REX; break; - case IRT_I16: case IRT_U16: xo = XO_MOVtow; break; - case IRT_NUM: xo = XO_MOVSDto; break; - case IRT_FLOAT: xo = XO_MOVSSto; break; -#if LJ_64 && !LJ_GC64 - case IRT_LIGHTUD: - /* NYI: mask 64 bit lightuserdata. */ - lj_assertA(0, "store of lightuserdata"); -#endif - default: - if (LJ_64 && irt_is64(ir->t)) - src |= REX_64; - else - lj_assertA(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t), - "unsplit 64 bit store"); - xo = XO_MOVto; - break; - } - emit_mrm(as, xo, src, RID_MRM); - if (!LJ_64 && src != osrc) { - ra_noweak(as, osrc); - emit_rr(as, XO_MOV, src, osrc); - } - } else { - if (irt_isi8(ir->t) || irt_isu8(ir->t)) { - emit_i8(as, k); - emit_mrm(as, XO_MOVmib, 0, RID_MRM); - } else { - lj_assertA(irt_is64(ir->t) || irt_isint(ir->t) || irt_isu32(ir->t) || - irt_isaddr(ir->t), "bad store type"); - emit_i32(as, k); - emit_mrm(as, XO_MOVmi, REX_64IR(ir, 0), RID_MRM); - } - } -} - -#define asm_fstore(as, ir) asm_fxstore(as, ir) -#define asm_xstore(as, ir) asm_fxstore(as, ir) - -#if LJ_64 && !LJ_GC64 -static Reg asm_load_lightud64(ASMState *as, IRIns *ir, int typecheck) -{ - if (ra_used(ir) || typecheck) { - Reg dest = ra_dest(as, ir, RSET_GPR); - if (typecheck) { - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, dest)); - asm_guardcc(as, CC_NE); - emit_i8(as, -2); - emit_rr(as, XO_ARITHi8, XOg_CMP, tmp); - emit_shifti(as, XOg_SAR|REX_64, tmp, 47); - emit_rr(as, XO_MOV, tmp|REX_64, dest); - } - return dest; - } else { - return RID_NONE; - } -} -#endif - -static void asm_ahuvload(ASMState *as, IRIns *ir) -{ -#if LJ_GC64 - Reg tmp = RID_NONE; -#endif - lj_assertA(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) || - (LJ_DUALNUM && irt_isint(ir->t)), - "bad load type %d", irt_type(ir->t)); -#if LJ_64 && !LJ_GC64 - if (irt_islightud(ir->t)) { - Reg dest = asm_load_lightud64(as, ir, 1); - if (ra_hasreg(dest)) { - asm_fuseahuref(as, ir->op1, RSET_GPR); - if (ir->o == IR_VLOAD) as->mrm.ofs += 8 * ir->op2; - emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM); - } - return; - } else -#endif - if (ra_used(ir)) { - RegSet allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - asm_fuseahuref(as, ir->op1, RSET_GPR); - if (ir->o == IR_VLOAD) as->mrm.ofs += 8 * ir->op2; -#if LJ_GC64 - if (irt_isaddr(ir->t)) { - emit_shifti(as, XOg_SHR|REX_64, dest, 17); - asm_guardcc(as, CC_NE); - emit_i8(as, irt_toitype(ir->t)); - emit_rr(as, XO_ARITHi8, XOg_CMP, dest); - emit_i8(as, XI_O16); - if ((as->flags & JIT_F_BMI2)) { - emit_i8(as, 47); - emit_mrm(as, XV_RORX|VEX_64, dest, RID_MRM); - } else { - emit_shifti(as, XOg_ROR|REX_64, dest, 47); - emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM); - } - return; - } else -#endif - emit_mrm(as, dest < RID_MAX_GPR ? XO_MOV : XO_MOVSD, dest, RID_MRM); - } else { - RegSet gpr = RSET_GPR; -#if LJ_GC64 - if (irt_isaddr(ir->t)) { - tmp = ra_scratch(as, RSET_GPR); - gpr = rset_exclude(gpr, tmp); - } -#endif - asm_fuseahuref(as, ir->op1, gpr); - if (ir->o == IR_VLOAD) as->mrm.ofs += 8 * ir->op2; - } - /* Always do the type check, even if the load result is unused. */ - as->mrm.ofs += 4; - asm_guardcc(as, irt_isnum(ir->t) ? CC_AE : CC_NE); - if (LJ_64 && irt_type(ir->t) >= IRT_NUM) { - lj_assertA(irt_isinteger(ir->t) || irt_isnum(ir->t), - "bad load type %d", irt_type(ir->t)); -#if LJ_GC64 - emit_u32(as, LJ_TISNUM << 15); -#else - emit_u32(as, LJ_TISNUM); -#endif - emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM); -#if LJ_GC64 - } else if (irt_isaddr(ir->t)) { - as->mrm.ofs -= 4; - emit_i8(as, irt_toitype(ir->t)); - emit_mrm(as, XO_ARITHi8, XOg_CMP, tmp); - emit_shifti(as, XOg_SAR|REX_64, tmp, 47); - emit_mrm(as, XO_MOV, tmp|REX_64, RID_MRM); - } else if (irt_isnil(ir->t)) { - as->mrm.ofs -= 4; - emit_i8(as, -1); - emit_mrm(as, XO_ARITHi8, XOg_CMP|REX_64, RID_MRM); - } else { - emit_u32(as, (irt_toitype(ir->t) << 15) | 0x7fff); - emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM); -#else - } else { - emit_i8(as, irt_toitype(ir->t)); - emit_mrm(as, XO_ARITHi8, XOg_CMP, RID_MRM); -#endif - } -} - -static void asm_ahustore(ASMState *as, IRIns *ir) -{ - if (ir->r == RID_SINK) - return; - if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ir->op2, RSET_FPR); - asm_fuseahuref(as, ir->op1, RSET_GPR); - emit_mrm(as, XO_MOVSDto, src, RID_MRM); -#if LJ_64 && !LJ_GC64 - } else if (irt_islightud(ir->t)) { - Reg src = ra_alloc1(as, ir->op2, RSET_GPR); - asm_fuseahuref(as, ir->op1, rset_exclude(RSET_GPR, src)); - emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM); -#endif -#if LJ_GC64 - } else if (irref_isk(ir->op2)) { - TValue k; - lj_ir_kvalue(as->J->L, &k, IR(ir->op2)); - asm_fuseahuref(as, ir->op1, RSET_GPR); - if (tvisnil(&k)) { - emit_i32(as, -1); - emit_mrm(as, XO_MOVmi, REX_64, RID_MRM); - } else { - emit_u32(as, k.u32.lo); - emit_mrm(as, XO_MOVmi, 0, RID_MRM); - as->mrm.ofs += 4; - emit_u32(as, k.u32.hi); - emit_mrm(as, XO_MOVmi, 0, RID_MRM); - } -#endif - } else { - IRIns *irr = IR(ir->op2); - RegSet allow = RSET_GPR; - Reg src = RID_NONE; - if (!irref_isk(ir->op2)) { - src = ra_alloc1(as, ir->op2, allow); - rset_clear(allow, src); - } - asm_fuseahuref(as, ir->op1, allow); - if (ra_hasreg(src)) { -#if LJ_GC64 - if (!(LJ_DUALNUM && irt_isinteger(ir->t))) { - /* TODO: 64 bit store + 32 bit load-modify-store is suboptimal. */ - as->mrm.ofs += 4; - emit_u32(as, irt_toitype(ir->t) << 15); - emit_mrm(as, XO_ARITHi, XOg_OR, RID_MRM); - as->mrm.ofs -= 4; - emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM); - return; - } -#endif - emit_mrm(as, XO_MOVto, src, RID_MRM); - } else if (!irt_ispri(irr->t)) { - lj_assertA(irt_isaddr(ir->t) || (LJ_DUALNUM && irt_isinteger(ir->t)), - "bad store type"); - emit_i32(as, irr->i); - emit_mrm(as, XO_MOVmi, 0, RID_MRM); - } - as->mrm.ofs += 4; -#if LJ_GC64 - lj_assertA(LJ_DUALNUM && irt_isinteger(ir->t), "bad store type"); - emit_i32(as, LJ_TNUMX << 15); -#else - emit_i32(as, (int32_t)irt_toitype(ir->t)); -#endif - emit_mrm(as, XO_MOVmi, 0, RID_MRM); - } -} - -static void asm_sload(ASMState *as, IRIns *ir) -{ - int32_t ofs = 8*((int32_t)ir->op1-1-LJ_FR2) + - (!LJ_FR2 && (ir->op2 & IRSLOAD_FRAME) ? 4 : 0); - IRType1 t = ir->t; - Reg base; - lj_assertA(!(ir->op2 & IRSLOAD_PARENT), - "bad parent SLOAD"); /* Handled by asm_head_side(). */ - lj_assertA(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK), - "inconsistent SLOAD variant"); - lj_assertA(LJ_DUALNUM || - !irt_isint(t) || - (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME|IRSLOAD_KEYINDEX)), - "bad SLOAD type"); - if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { - Reg left = ra_scratch(as, RSET_FPR); - asm_tointg(as, ir, left); /* Frees dest reg. Do this before base alloc. */ - base = ra_alloc1(as, REF_BASE, RSET_GPR); - emit_rmro(as, XO_MOVSD, left, base, ofs); - t.irt = IRT_NUM; /* Continue with a regular number type check. */ -#if LJ_64 && !LJ_GC64 - } else if (irt_islightud(t)) { - Reg dest = asm_load_lightud64(as, ir, (ir->op2 & IRSLOAD_TYPECHECK)); - if (ra_hasreg(dest)) { - base = ra_alloc1(as, REF_BASE, RSET_GPR); - emit_rmro(as, XO_MOV, dest|REX_64, base, ofs); - } - return; -#endif - } else if (ra_used(ir)) { - RegSet allow = irt_isnum(t) ? RSET_FPR : RSET_GPR; - Reg dest = ra_dest(as, ir, allow); - base = ra_alloc1(as, REF_BASE, RSET_GPR); - lj_assertA(irt_isnum(t) || irt_isint(t) || irt_isaddr(t), - "bad SLOAD type %d", irt_type(t)); - if ((ir->op2 & IRSLOAD_CONVERT)) { - t.irt = irt_isint(t) ? IRT_NUM : IRT_INT; /* Check for original type. */ - emit_rmro(as, irt_isint(t) ? XO_CVTSI2SD : XO_CVTTSD2SI, dest, base, ofs); - } else { -#if LJ_GC64 - if (irt_isaddr(t)) { - /* LJ_GC64 type check + tag removal without BMI2 and with BMI2: - ** - ** mov r64, [addr] rorx r64, [addr], 47 - ** ror r64, 47 - ** cmp r16, itype cmp r16, itype - ** jne ->exit jne ->exit - ** shr r64, 16 shr r64, 16 - */ - emit_shifti(as, XOg_SHR|REX_64, dest, 17); - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - asm_guardcc(as, CC_NE); - emit_i8(as, irt_toitype(t)); - emit_rr(as, XO_ARITHi8, XOg_CMP, dest); - emit_i8(as, XI_O16); - } - if ((as->flags & JIT_F_BMI2)) { - emit_i8(as, 47); - emit_rmro(as, XV_RORX|VEX_64, dest, base, ofs); - } else { - if ((ir->op2 & IRSLOAD_TYPECHECK)) - emit_shifti(as, XOg_ROR|REX_64, dest, 47); - else - emit_shifti(as, XOg_SHL|REX_64, dest, 17); - emit_rmro(as, XO_MOV, dest|REX_64, base, ofs); - } - return; - } else -#endif - emit_rmro(as, irt_isnum(t) ? XO_MOVSD : XO_MOV, dest, base, ofs); - } - } else { - if (!(ir->op2 & IRSLOAD_TYPECHECK)) - return; /* No type check: avoid base alloc. */ - base = ra_alloc1(as, REF_BASE, RSET_GPR); - } - if ((ir->op2 & IRSLOAD_TYPECHECK)) { - /* Need type check, even if the load result is unused. */ - asm_guardcc(as, irt_isnum(t) ? CC_AE : CC_NE); - if ((LJ_64 && irt_type(t) >= IRT_NUM) || (ir->op2 & IRSLOAD_KEYINDEX)) { - lj_assertA(irt_isinteger(t) || irt_isnum(t), - "bad SLOAD type %d", irt_type(t)); - emit_u32(as, (ir->op2 & IRSLOAD_KEYINDEX) ? LJ_KEYINDEX : - LJ_GC64 ? (LJ_TISNUM << 15) : LJ_TISNUM); - emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4); -#if LJ_GC64 - } else if (irt_isnil(t)) { - /* LJ_GC64 type check for nil: - ** - ** cmp qword [addr], -1 - ** jne ->exit - */ - emit_i8(as, -1); - emit_rmro(as, XO_ARITHi8, XOg_CMP|REX_64, base, ofs); - } else if (irt_ispri(t)) { - emit_u32(as, (irt_toitype(t) << 15) | 0x7fff); - emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4); - } else { - /* LJ_GC64 type check only: - ** - ** mov r64, [addr] - ** sar r64, 47 - ** cmp r32, itype - ** jne ->exit - */ - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, base)); - emit_i8(as, irt_toitype(t)); - emit_rr(as, XO_ARITHi8, XOg_CMP, tmp); - emit_shifti(as, XOg_SAR|REX_64, tmp, 47); - emit_rmro(as, XO_MOV, tmp|REX_64, base, ofs); -#else - } else { - emit_i8(as, irt_toitype(t)); - emit_rmro(as, XO_ARITHi8, XOg_CMP, base, ofs+4); -#endif - } - } -} - -/* -- Allocations --------------------------------------------------------- */ - -#if LJ_HASFFI -static void asm_cnew(ASMState *as, IRIns *ir) -{ - CTState *cts = ctype_ctsG(J2G(as->J)); - CTypeID id = (CTypeID)IR(ir->op1)->i; - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; - IRRef args[4]; - lj_assertA(sz != CTSIZE_INVALID || (ir->o == IR_CNEW && ir->op2 != REF_NIL), - "bad CNEW/CNEWI operands"); - - as->gcsteps++; - asm_setupresult(as, ir, ci); /* GCcdata * */ - - /* Initialize immutable cdata object. */ - if (ir->o == IR_CNEWI) { - RegSet allow = (RSET_GPR & ~RSET_SCRATCH); -#if LJ_64 - Reg r64 = sz == 8 ? REX_64 : 0; - if (irref_isk(ir->op2)) { - IRIns *irk = IR(ir->op2); - uint64_t k = (irk->o == IR_KINT64 || - (LJ_GC64 && (irk->o == IR_KPTR || irk->o == IR_KKPTR))) ? - ir_k64(irk)->u64 : (uint64_t)(uint32_t)irk->i; - if (sz == 4 || checki32((int64_t)k)) { - emit_i32(as, (int32_t)k); - emit_rmro(as, XO_MOVmi, r64, RID_RET, sizeof(GCcdata)); - } else { - emit_movtomro(as, RID_ECX + r64, RID_RET, sizeof(GCcdata)); - emit_loadu64(as, RID_ECX, k); - } - } else { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_movtomro(as, r + r64, RID_RET, sizeof(GCcdata)); - } -#else - int32_t ofs = sizeof(GCcdata); - if (sz == 8) { - ofs += 4; ir++; - lj_assertA(ir->o == IR_HIOP, "missing CNEWI HIOP"); - } - do { - if (irref_isk(ir->op2)) { - emit_movmroi(as, RID_RET, ofs, IR(ir->op2)->i); - } else { - Reg r = ra_alloc1(as, ir->op2, allow); - emit_movtomro(as, r, RID_RET, ofs); - rset_clear(allow, r); - } - if (ofs == sizeof(GCcdata)) break; - ofs -= 4; ir--; - } while (1); -#endif - lj_assertA(sz == 4 || sz == 8, "bad CNEWI size %d", sz); - } else if (ir->op2 != REF_NIL) { /* Create VLA/VLS/aligned cdata. */ - ci = &lj_ir_callinfo[IRCALL_lj_cdata_newv]; - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ir->op1; /* CTypeID id */ - args[2] = ir->op2; /* CTSize sz */ - args[3] = ASMREF_TMP1; /* CTSize align */ - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)ctype_align(info)); - return; - } - - /* Combine initialization of marked, gct and ctypeid. */ - emit_movtomro(as, RID_ECX, RID_RET, offsetof(GCcdata, marked)); - emit_gri(as, XG_ARITHi(XOg_OR), RID_ECX, - (int32_t)((~LJ_TCDATA<<8)+(id<<16))); - emit_gri(as, XG_ARITHi(XOg_AND), RID_ECX, LJ_GC_WHITES); - emit_opgl(as, XO_MOVZXb, RID_ECX, gc.currentwhite); - - args[0] = ASMREF_L; /* lua_State *L */ - args[1] = ASMREF_TMP1; /* MSize size */ - asm_gencall(as, ci, args); - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)(sz+sizeof(GCcdata))); -} -#endif - -/* -- Write barriers ------------------------------------------------------ */ - -static void asm_tbar(ASMState *as, IRIns *ir) -{ - Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); - Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, tab)); - MCLabel l_end = emit_label(as); - emit_movtomro(as, tmp|REX_GC64, tab, offsetof(GCtab, gclist)); - emit_setgl(as, tab, gc.grayagain); - emit_getgl(as, tmp, gc.grayagain); - emit_i8(as, ~LJ_GC_BLACK); - emit_rmro(as, XO_ARITHib, XOg_AND, tab, offsetof(GCtab, marked)); - emit_sjcc(as, CC_Z, l_end); - emit_i8(as, LJ_GC_BLACK); - emit_rmro(as, XO_GROUP3b, XOg_TEST, tab, offsetof(GCtab, marked)); -} - -static void asm_obar(ASMState *as, IRIns *ir) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; - IRRef args[2]; - MCLabel l_end; - Reg obj; - /* No need for other object barriers (yet). */ - lj_assertA(IR(ir->op1)->o == IR_UREFC, "bad OBAR type"); - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ir->op1; /* TValue *tv */ - asm_gencall(as, ci, args); - emit_loada(as, ra_releasetmp(as, ASMREF_TMP1), J2G(as->J)); - obj = IR(ir->op1)->r; - emit_sjcc(as, CC_Z, l_end); - emit_i8(as, LJ_GC_WHITES); - if (irref_isk(ir->op2)) { - GCobj *vp = ir_kgc(IR(ir->op2)); - emit_rma(as, XO_GROUP3b, XOg_TEST, &vp->gch.marked); - } else { - Reg val = ra_alloc1(as, ir->op2, rset_exclude(RSET_SCRATCH&RSET_GPR, obj)); - emit_rmro(as, XO_GROUP3b, XOg_TEST, val, (int32_t)offsetof(GChead, marked)); - } - emit_sjcc(as, CC_Z, l_end); - emit_i8(as, LJ_GC_BLACK); - emit_rmro(as, XO_GROUP3b, XOg_TEST, obj, - (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); -} - -/* -- FP/int arithmetic and logic operations ------------------------------ */ - -/* Load reference onto x87 stack. Force a spill to memory if needed. */ -static void asm_x87load(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_KNUM) { - cTValue *tv = ir_knum(ir); - if (tvispzero(tv)) /* Use fldz only for +0. */ - emit_x87op(as, XI_FLDZ); - else if (tvispone(tv)) - emit_x87op(as, XI_FLD1); - else - emit_rma(as, XO_FLDq, XOg_FLDq, tv); - } else if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && !ra_used(ir) && - !irref_isk(ir->op1) && mayfuse(as, ir->op1)) { - IRIns *iri = IR(ir->op1); - emit_rmro(as, XO_FILDd, XOg_FILDd, RID_ESP, ra_spill(as, iri)); - } else { - emit_mrm(as, XO_FLDq, XOg_FLDq, asm_fuseload(as, ref, RSET_EMPTY)); - } -} - -static void asm_fpmath(ASMState *as, IRIns *ir) -{ - IRFPMathOp fpm = (IRFPMathOp)ir->op2; - if (fpm == IRFPM_SQRT) { - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = asm_fuseload(as, ir->op1, RSET_FPR); - emit_mrm(as, XO_SQRTSD, dest, left); - } else if (fpm <= IRFPM_TRUNC) { - if (as->flags & JIT_F_SSE4_1) { /* SSE4.1 has a rounding instruction. */ - Reg dest = ra_dest(as, ir, RSET_FPR); - Reg left = asm_fuseload(as, ir->op1, RSET_FPR); - /* ROUNDSD has a 4-byte opcode which doesn't fit in x86Op. - ** Let's pretend it's a 3-byte opcode, and compensate afterwards. - ** This is atrocious, but the alternatives are much worse. - */ - /* Round down/up/trunc == 1001/1010/1011. */ - emit_i8(as, 0x09 + fpm); - emit_mrm(as, XO_ROUNDSD, dest, left); - if (LJ_64 && as->mcp[1] != (MCode)(XO_ROUNDSD >> 16)) { - as->mcp[0] = as->mcp[1]; as->mcp[1] = 0x0f; /* Swap 0F and REX. */ - } - *--as->mcp = 0x66; /* 1st byte of ROUNDSD opcode. */ - } else { /* Call helper functions for SSE2 variant. */ - /* The modified regs must match with the *.dasc implementation. */ - RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); - if (ra_hasreg(ir->r)) - rset_clear(drop, ir->r); /* Dest reg handled below. */ - ra_evictset(as, drop); - ra_destreg(as, ir, RID_XMM0); - emit_call(as, fpm == IRFPM_FLOOR ? lj_vm_floor_sse : - fpm == IRFPM_CEIL ? lj_vm_ceil_sse : lj_vm_trunc_sse); - ra_left(as, RID_XMM0, ir->op1); - } - } else { - asm_callid(as, ir, IRCALL_lj_vm_floor + fpm); - } -} - -static void asm_ldexp(ASMState *as, IRIns *ir) -{ - int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ - Reg dest = ir->r; - if (ra_hasreg(dest)) { - ra_free(as, dest); - ra_modified(as, dest); - emit_rmro(as, XO_MOVSD, dest, RID_ESP, ofs); - } - emit_rmro(as, XO_FSTPq, XOg_FSTPq, RID_ESP, ofs); - emit_x87op(as, XI_FPOP1); - emit_x87op(as, XI_FSCALE); - asm_x87load(as, ir->op1); - asm_x87load(as, ir->op2); -} - -static int asm_swapops(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1); - IRIns *irr = IR(ir->op2); - lj_assertA(ra_noreg(irr->r), "bad usage"); - if (!irm_iscomm(lj_ir_mode[ir->o])) - return 0; /* Can't swap non-commutative operations. */ - if (irref_isk(ir->op2)) - return 0; /* Don't swap constants to the left. */ - if (ra_hasreg(irl->r)) - return 1; /* Swap if left already has a register. */ - if (ra_samehint(ir->r, irr->r)) - return 1; /* Swap if dest and right have matching hints. */ - if (as->curins > as->loopref) { /* In variant part? */ - if (ir->op2 < as->loopref && !irt_isphi(irr->t)) - return 0; /* Keep invariants on the right. */ - if (ir->op1 < as->loopref && !irt_isphi(irl->t)) - return 1; /* Swap invariants to the right. */ - } - if (opisfusableload(irl->o)) - return 1; /* Swap fusable loads to the right. */ - return 0; /* Otherwise don't swap. */ -} - -static void asm_fparith(ASMState *as, IRIns *ir, x86Op xo) -{ - IRRef lref = ir->op1; - IRRef rref = ir->op2; - RegSet allow = RSET_FPR; - Reg dest; - Reg right = IR(rref)->r; - if (ra_hasreg(right)) { - rset_clear(allow, right); - ra_noweak(as, right); - } - dest = ra_dest(as, ir, allow); - if (lref == rref) { - right = dest; - } else if (ra_noreg(right)) { - if (asm_swapops(as, ir)) { - IRRef tmp = lref; lref = rref; rref = tmp; - } - right = asm_fuseload(as, rref, rset_clear(allow, dest)); - } - emit_mrm(as, xo, dest, right); - ra_left(as, dest, lref); -} - -static void asm_intarith(ASMState *as, IRIns *ir, x86Arith xa) -{ - IRRef lref = ir->op1; - IRRef rref = ir->op2; - RegSet allow = RSET_GPR; - Reg dest, right; - int32_t k = 0; - if (as->flagmcp == as->mcp) { /* Drop test r,r instruction. */ - MCode *p = as->mcp + ((LJ_64 && *as->mcp < XI_TESTb) ? 3 : 2); - MCode *q = p[0] == 0x0f ? p+1 : p; - if ((*q & 15) < 14) { - if ((*q & 15) >= 12) *q -= 4; /* L <->S, NL <-> NS */ - as->flagmcp = NULL; - as->mcp = p; - } /* else: cannot transform LE/NLE to cc without use of OF. */ - } - right = IR(rref)->r; - if (ra_hasreg(right)) { - rset_clear(allow, right); - ra_noweak(as, right); - } - dest = ra_dest(as, ir, allow); - if (lref == rref) { - right = dest; - } else if (ra_noreg(right) && !asm_isk32(as, rref, &k)) { - if (asm_swapops(as, ir)) { - IRRef tmp = lref; lref = rref; rref = tmp; - } - right = asm_fuseloadm(as, rref, rset_clear(allow, dest), irt_is64(ir->t)); - } - if (irt_isguard(ir->t)) /* For IR_ADDOV etc. */ - asm_guardcc(as, CC_O); - if (xa != XOg_X_IMUL) { - if (ra_hasreg(right)) - emit_mrm(as, XO_ARITH(xa), REX_64IR(ir, dest), right); - else - emit_gri(as, XG_ARITHi(xa), REX_64IR(ir, dest), k); - } else if (ra_hasreg(right)) { /* IMUL r, mrm. */ - emit_mrm(as, XO_IMUL, REX_64IR(ir, dest), right); - } else { /* IMUL r, r, k. */ - /* NYI: use lea/shl/add/sub (FOLD only does 2^k) depending on CPU. */ - Reg left = asm_fuseloadm(as, lref, RSET_GPR, irt_is64(ir->t)); - x86Op xo; - if (checki8(k)) { emit_i8(as, k); xo = XO_IMULi8; - } else { emit_i32(as, k); xo = XO_IMULi; } - emit_mrm(as, xo, REX_64IR(ir, dest), left); - return; - } - ra_left(as, dest, lref); -} - -/* LEA is really a 4-operand ADD with an independent destination register, -** up to two source registers and an immediate. One register can be scaled -** by 1, 2, 4 or 8. This can be used to avoid moves or to fuse several -** instructions. -** -** Currently only a few common cases are supported: -** - 3-operand ADD: y = a+b; y = a+k with a and b already allocated -** - Left ADD fusion: y = (a+b)+k; y = (a+k)+b -** - Right ADD fusion: y = a+(b+k) -** The ommited variants have already been reduced by FOLD. -** -** There are more fusion opportunities, like gathering shifts or joining -** common references. But these are probably not worth the trouble, since -** array indexing is not decomposed and already makes use of all fields -** of the ModRM operand. -*/ -static int asm_lea(ASMState *as, IRIns *ir) -{ - IRIns *irl = IR(ir->op1); - IRIns *irr = IR(ir->op2); - RegSet allow = RSET_GPR; - Reg dest; - as->mrm.base = as->mrm.idx = RID_NONE; - as->mrm.scale = XM_SCALE1; - as->mrm.ofs = 0; - if (ra_hasreg(irl->r)) { - rset_clear(allow, irl->r); - ra_noweak(as, irl->r); - as->mrm.base = irl->r; - if (irref_isk(ir->op2) || ra_hasreg(irr->r)) { - /* The PHI renaming logic does a better job in some cases. */ - if (ra_hasreg(ir->r) && - ((irt_isphi(irl->t) && as->phireg[ir->r] == ir->op1) || - (irt_isphi(irr->t) && as->phireg[ir->r] == ir->op2))) - return 0; - if (irref_isk(ir->op2)) { - as->mrm.ofs = irr->i; - } else { - rset_clear(allow, irr->r); - ra_noweak(as, irr->r); - as->mrm.idx = irr->r; - } - } else if (irr->o == IR_ADD && mayfuse(as, ir->op2) && - irref_isk(irr->op2)) { - Reg idx = ra_alloc1(as, irr->op1, allow); - rset_clear(allow, idx); - as->mrm.idx = (uint8_t)idx; - as->mrm.ofs = IR(irr->op2)->i; - } else { - return 0; - } - } else if (ir->op1 != ir->op2 && irl->o == IR_ADD && mayfuse(as, ir->op1) && - (irref_isk(ir->op2) || irref_isk(irl->op2))) { - Reg idx, base = ra_alloc1(as, irl->op1, allow); - rset_clear(allow, base); - as->mrm.base = (uint8_t)base; - if (irref_isk(ir->op2)) { - as->mrm.ofs = irr->i; - idx = ra_alloc1(as, irl->op2, allow); - } else { - as->mrm.ofs = IR(irl->op2)->i; - idx = ra_alloc1(as, ir->op2, allow); - } - rset_clear(allow, idx); - as->mrm.idx = (uint8_t)idx; - } else { - return 0; - } - dest = ra_dest(as, ir, allow); - emit_mrm(as, XO_LEA, dest, RID_MRM); - return 1; /* Success. */ -} - -static void asm_add(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_ADDSD); - else if (as->flagmcp == as->mcp || irt_is64(ir->t) || !asm_lea(as, ir)) - asm_intarith(as, ir, XOg_ADD); -} - -static void asm_sub(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_SUBSD); - else /* Note: no need for LEA trick here. i-k is encoded as i+(-k). */ - asm_intarith(as, ir, XOg_SUB); -} - -static void asm_mul(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_MULSD); - else - asm_intarith(as, ir, XOg_X_IMUL); -} - -#define asm_fpdiv(as, ir) asm_fparith(as, ir, XO_DIVSD) - -static void asm_neg_not(ASMState *as, IRIns *ir, x86Group3 xg) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_rr(as, XO_GROUP3, REX_64IR(ir, xg), dest); - ra_left(as, dest, ir->op1); -} - -static void asm_neg(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_XORPS); - else - asm_neg_not(as, ir, XOg_NEG); -} - -#define asm_abs(as, ir) asm_fparith(as, ir, XO_ANDPS) - -static void asm_intmin_max(ASMState *as, IRIns *ir, int cc) -{ - Reg right, dest = ra_dest(as, ir, RSET_GPR); - IRRef lref = ir->op1, rref = ir->op2; - if (irref_isk(rref)) { lref = rref; rref = ir->op1; } - right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, dest)); - emit_rr(as, XO_CMOV + (cc<<24), REX_64IR(ir, dest), right); - emit_rr(as, XO_CMP, REX_64IR(ir, dest), right); - ra_left(as, dest, lref); -} - -static void asm_min(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_MINSD); - else - asm_intmin_max(as, ir, CC_G); -} - -static void asm_max(ASMState *as, IRIns *ir) -{ - if (irt_isnum(ir->t)) - asm_fparith(as, ir, XO_MAXSD); - else - asm_intmin_max(as, ir, CC_L); -} - -/* Note: don't use LEA for overflow-checking arithmetic! */ -#define asm_addov(as, ir) asm_intarith(as, ir, XOg_ADD) -#define asm_subov(as, ir) asm_intarith(as, ir, XOg_SUB) -#define asm_mulov(as, ir) asm_intarith(as, ir, XOg_X_IMUL) - -#define asm_bnot(as, ir) asm_neg_not(as, ir, XOg_NOT) - -static void asm_bswap(ASMState *as, IRIns *ir) -{ - Reg dest = ra_dest(as, ir, RSET_GPR); - as->mcp = emit_op(XO_BSWAP + ((dest&7) << 24), - REX_64IR(ir, 0), dest, 0, as->mcp, 1); - ra_left(as, dest, ir->op1); -} - -#define asm_band(as, ir) asm_intarith(as, ir, XOg_AND) -#define asm_bor(as, ir) asm_intarith(as, ir, XOg_OR) -#define asm_bxor(as, ir) asm_intarith(as, ir, XOg_XOR) - -static void asm_bitshift(ASMState *as, IRIns *ir, x86Shift xs, x86Op xv) -{ - IRRef rref = ir->op2; - IRIns *irr = IR(rref); - Reg dest; - if (irref_isk(rref)) { /* Constant shifts. */ - int shift; - dest = ra_dest(as, ir, RSET_GPR); - shift = irr->i & (irt_is64(ir->t) ? 63 : 31); - if (!xv && shift && (as->flags & JIT_F_BMI2)) { - Reg left = asm_fuseloadm(as, ir->op1, RSET_GPR, irt_is64(ir->t)); - if (left != dest) { /* BMI2 rotate right by constant. */ - emit_i8(as, xs == XOg_ROL ? -shift : shift); - emit_mrm(as, VEX_64IR(ir, XV_RORX), dest, left); - return; - } - } - switch (shift) { - case 0: break; - case 1: emit_rr(as, XO_SHIFT1, REX_64IR(ir, xs), dest); break; - default: emit_shifti(as, REX_64IR(ir, xs), dest, shift); break; - } - } else if ((as->flags & JIT_F_BMI2) && xv) { /* BMI2 variable shifts. */ - Reg left, right; - dest = ra_dest(as, ir, RSET_GPR); - right = ra_alloc1(as, rref, RSET_GPR); - left = asm_fuseloadm(as, ir->op1, rset_exclude(RSET_GPR, right), - irt_is64(ir->t)); - emit_mrm(as, VEX_64IR(ir, xv) ^ (right << 19), dest, left); - return; - } else { /* Variable shifts implicitly use register cl (i.e. ecx). */ - Reg right; - dest = ra_dest(as, ir, rset_exclude(RSET_GPR, RID_ECX)); - if (dest == RID_ECX) { - dest = ra_scratch(as, rset_exclude(RSET_GPR, RID_ECX)); - emit_rr(as, XO_MOV, REX_64IR(ir, RID_ECX), dest); - } - right = irr->r; - if (ra_noreg(right)) - right = ra_allocref(as, rref, RID2RSET(RID_ECX)); - else if (right != RID_ECX) - ra_scratch(as, RID2RSET(RID_ECX)); - emit_rr(as, XO_SHIFTcl, REX_64IR(ir, xs), dest); - ra_noweak(as, right); - if (right != RID_ECX) - emit_rr(as, XO_MOV, RID_ECX, right); - } - ra_left(as, dest, ir->op1); - /* - ** Note: avoid using the flags resulting from a shift or rotate! - ** All of them cause a partial flag stall, except for r,1 shifts - ** (but not rotates). And a shift count of 0 leaves the flags unmodified. - */ -} - -#define asm_bshl(as, ir) asm_bitshift(as, ir, XOg_SHL, XV_SHLX) -#define asm_bshr(as, ir) asm_bitshift(as, ir, XOg_SHR, XV_SHRX) -#define asm_bsar(as, ir) asm_bitshift(as, ir, XOg_SAR, XV_SARX) -#define asm_brol(as, ir) asm_bitshift(as, ir, XOg_ROL, 0) -#define asm_bror(as, ir) asm_bitshift(as, ir, XOg_ROR, 0) - -/* -- Comparisons --------------------------------------------------------- */ - -/* Virtual flags for unordered FP comparisons. */ -#define VCC_U 0x1000 /* Unordered. */ -#define VCC_P 0x2000 /* Needs extra CC_P branch. */ -#define VCC_S 0x4000 /* Swap avoids CC_P branch. */ -#define VCC_PS (VCC_P|VCC_S) - -/* Map of comparisons to flags. ORDER IR. */ -#define COMPFLAGS(ci, cin, cu, cf) ((ci)+((cu)<<4)+((cin)<<8)+(cf)) -static const uint16_t asm_compmap[IR_ABC+1] = { - /* signed non-eq unsigned flags */ - /* LT */ COMPFLAGS(CC_GE, CC_G, CC_AE, VCC_PS), - /* GE */ COMPFLAGS(CC_L, CC_L, CC_B, 0), - /* LE */ COMPFLAGS(CC_G, CC_G, CC_A, VCC_PS), - /* GT */ COMPFLAGS(CC_LE, CC_L, CC_BE, 0), - /* ULT */ COMPFLAGS(CC_AE, CC_A, CC_AE, VCC_U), - /* UGE */ COMPFLAGS(CC_B, CC_B, CC_B, VCC_U|VCC_PS), - /* ULE */ COMPFLAGS(CC_A, CC_A, CC_A, VCC_U), - /* UGT */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS), - /* EQ */ COMPFLAGS(CC_NE, CC_NE, CC_NE, VCC_P), - /* NE */ COMPFLAGS(CC_E, CC_E, CC_E, VCC_U|VCC_P), - /* ABC */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS) /* Same as UGT. */ -}; - -/* FP and integer comparisons. */ -static void asm_comp(ASMState *as, IRIns *ir) -{ - uint32_t cc = asm_compmap[ir->o]; - if (irt_isnum(ir->t)) { - IRRef lref = ir->op1; - IRRef rref = ir->op2; - Reg left, right; - MCLabel l_around; - /* - ** An extra CC_P branch is required to preserve ordered/unordered - ** semantics for FP comparisons. This can be avoided by swapping - ** the operands and inverting the condition (except for EQ and UNE). - ** So always try to swap if possible. - ** - ** Another option would be to swap operands to achieve better memory - ** operand fusion. But it's unlikely that this outweighs the cost - ** of the extra branches. - */ - if (cc & VCC_S) { /* Swap? */ - IRRef tmp = lref; lref = rref; rref = tmp; - cc ^= (VCC_PS|(5<<4)); /* A <-> B, AE <-> BE, PS <-> none */ - } - left = ra_alloc1(as, lref, RSET_FPR); - l_around = emit_label(as); - asm_guardcc(as, cc >> 4); - if (cc & VCC_P) { /* Extra CC_P branch required? */ - if (!(cc & VCC_U)) { - asm_guardcc(as, CC_P); /* Branch to exit for ordered comparisons. */ - } else if (l_around != as->invmcp) { - emit_sjcc(as, CC_P, l_around); /* Branch around for unordered. */ - } else { - /* Patched to mcloop by asm_loop_fixup. */ - as->loopinv = 2; - if (as->realign) - emit_sjcc(as, CC_P, as->mcp); - else - emit_jcc(as, CC_P, as->mcp); - } - } - right = asm_fuseload(as, rref, rset_exclude(RSET_FPR, left)); - emit_mrm(as, XO_UCOMISD, left, right); - } else { - IRRef lref = ir->op1, rref = ir->op2; - IROp leftop = (IROp)(IR(lref)->o); - Reg r64 = REX_64IR(ir, 0); - int32_t imm = 0; - lj_assertA(irt_is64(ir->t) || irt_isint(ir->t) || - irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t), - "bad comparison data type %d", irt_type(ir->t)); - /* Swap constants (only for ABC) and fusable loads to the right. */ - if (irref_isk(lref) || (!irref_isk(rref) && opisfusableload(leftop))) { - if ((cc & 0xc) == 0xc) cc ^= 0x53; /* L <-> G, LE <-> GE */ - else if ((cc & 0xa) == 0x2) cc ^= 0x55; /* A <-> B, AE <-> BE */ - lref = ir->op2; rref = ir->op1; - } - if (asm_isk32(as, rref, &imm)) { - IRIns *irl = IR(lref); - /* Check wether we can use test ins. Not for unsigned, since CF=0. */ - int usetest = (imm == 0 && (cc & 0xa) != 0x2); - if (usetest && irl->o == IR_BAND && irl+1 == ir && !ra_used(irl)) { - /* Combine comp(BAND(ref, r/imm), 0) into test mrm, r/imm. */ - Reg right, left = RID_NONE; - RegSet allow = RSET_GPR; - if (!asm_isk32(as, irl->op2, &imm)) { - left = ra_alloc1(as, irl->op2, allow); - rset_clear(allow, left); - } else { /* Try to Fuse IRT_I8/IRT_U8 loads, too. See below. */ - IRIns *irll = IR(irl->op1); - if (opisfusableload((IROp)irll->o) && - (irt_isi8(irll->t) || irt_isu8(irll->t))) { - IRType1 origt = irll->t; /* Temporarily flip types. */ - irll->t.irt = (irll->t.irt & ~IRT_TYPE) | IRT_INT; - as->curins--; /* Skip to BAND to avoid failing in noconflict(). */ - right = asm_fuseload(as, irl->op1, RSET_GPR); - as->curins++; - irll->t = origt; - if (right != RID_MRM) goto test_nofuse; - /* Fusion succeeded, emit test byte mrm, imm8. */ - asm_guardcc(as, cc); - emit_i8(as, (imm & 0xff)); - emit_mrm(as, XO_GROUP3b, XOg_TEST, RID_MRM); - return; - } - } - as->curins--; /* Skip to BAND to avoid failing in noconflict(). */ - right = asm_fuseloadm(as, irl->op1, allow, r64); - as->curins++; /* Undo the above. */ - test_nofuse: - asm_guardcc(as, cc); - if (ra_noreg(left)) { - emit_i32(as, imm); - emit_mrm(as, XO_GROUP3, r64 + XOg_TEST, right); - } else { - emit_mrm(as, XO_TEST, r64 + left, right); - } - } else { - Reg left; - if (opisfusableload((IROp)irl->o) && - ((irt_isu8(irl->t) && checku8(imm)) || - ((irt_isi8(irl->t) || irt_isi16(irl->t)) && checki8(imm)) || - (irt_isu16(irl->t) && checku16(imm) && checki8((int16_t)imm)))) { - /* Only the IRT_INT case is fused by asm_fuseload. - ** The IRT_I8/IRT_U8 loads and some IRT_I16/IRT_U16 loads - ** are handled here. - ** Note that cmp word [mem], imm16 should not be generated, - ** since it has a length-changing prefix. Compares of a word - ** against a sign-extended imm8 are ok, however. - */ - IRType1 origt = irl->t; /* Temporarily flip types. */ - irl->t.irt = (irl->t.irt & ~IRT_TYPE) | IRT_INT; - left = asm_fuseload(as, lref, RSET_GPR); - irl->t = origt; - if (left == RID_MRM) { /* Fusion succeeded? */ - if (irt_isu8(irl->t) || irt_isu16(irl->t)) - cc >>= 4; /* Need unsigned compare. */ - asm_guardcc(as, cc); - emit_i8(as, imm); - emit_mrm(as, (irt_isi8(origt) || irt_isu8(origt)) ? - XO_ARITHib : XO_ARITHiw8, r64 + XOg_CMP, RID_MRM); - return; - } /* Otherwise handle register case as usual. */ - } else { - left = asm_fuseloadm(as, lref, - irt_isu8(ir->t) ? RSET_GPR8 : RSET_GPR, r64); - } - asm_guardcc(as, cc); - if (usetest && left != RID_MRM) { - /* Use test r,r instead of cmp r,0. */ - x86Op xo = XO_TEST; - if (irt_isu8(ir->t)) { - lj_assertA(ir->o == IR_EQ || ir->o == IR_NE, "bad usage"); - xo = XO_TESTb; - if (!rset_test(RSET_RANGE(RID_EAX, RID_EBX+1), left)) { - if (LJ_64) { - left |= FORCE_REX; - } else { - emit_i32(as, 0xff); - emit_mrm(as, XO_GROUP3, XOg_TEST, left); - return; - } - } - } - emit_rr(as, xo, r64 + left, left); - if (irl+1 == ir) /* Referencing previous ins? */ - as->flagmcp = as->mcp; /* Set flag to drop test r,r if possible. */ - } else { - emit_gmrmi(as, XG_ARITHi(XOg_CMP), r64 + left, imm); - } - } - } else { - Reg left = ra_alloc1(as, lref, RSET_GPR); - Reg right = asm_fuseloadm(as, rref, rset_exclude(RSET_GPR, left), r64); - asm_guardcc(as, cc); - emit_mrm(as, XO_CMP, r64 + left, right); - } - } -} - -#define asm_equal(as, ir) asm_comp(as, ir) - -#if LJ_32 && LJ_HASFFI -/* 64 bit integer comparisons in 32 bit mode. */ -static void asm_comp_int64(ASMState *as, IRIns *ir) -{ - uint32_t cc = asm_compmap[(ir-1)->o]; - RegSet allow = RSET_GPR; - Reg lefthi = RID_NONE, leftlo = RID_NONE; - Reg righthi = RID_NONE, rightlo = RID_NONE; - MCLabel l_around; - x86ModRM mrm; - - as->curins--; /* Skip loword ins. Avoids failing in noconflict(), too. */ - - /* Allocate/fuse hiword operands. */ - if (irref_isk(ir->op2)) { - lefthi = asm_fuseload(as, ir->op1, allow); - } else { - lefthi = ra_alloc1(as, ir->op1, allow); - rset_clear(allow, lefthi); - righthi = asm_fuseload(as, ir->op2, allow); - if (righthi == RID_MRM) { - if (as->mrm.base != RID_NONE) rset_clear(allow, as->mrm.base); - if (as->mrm.idx != RID_NONE) rset_clear(allow, as->mrm.idx); - } else { - rset_clear(allow, righthi); - } - } - mrm = as->mrm; /* Save state for hiword instruction. */ - - /* Allocate/fuse loword operands. */ - if (irref_isk((ir-1)->op2)) { - leftlo = asm_fuseload(as, (ir-1)->op1, allow); - } else { - leftlo = ra_alloc1(as, (ir-1)->op1, allow); - rset_clear(allow, leftlo); - rightlo = asm_fuseload(as, (ir-1)->op2, allow); - } - - /* All register allocations must be performed _before_ this point. */ - l_around = emit_label(as); - as->invmcp = as->flagmcp = NULL; /* Cannot use these optimizations. */ - - /* Loword comparison and branch. */ - asm_guardcc(as, cc >> 4); /* Always use unsigned compare for loword. */ - if (ra_noreg(rightlo)) { - int32_t imm = IR((ir-1)->op2)->i; - if (imm == 0 && ((cc >> 4) & 0xa) != 0x2 && leftlo != RID_MRM) - emit_rr(as, XO_TEST, leftlo, leftlo); - else - emit_gmrmi(as, XG_ARITHi(XOg_CMP), leftlo, imm); - } else { - emit_mrm(as, XO_CMP, leftlo, rightlo); - } - - /* Hiword comparison and branches. */ - if ((cc & 15) != CC_NE) - emit_sjcc(as, CC_NE, l_around); /* Hiword unequal: skip loword compare. */ - if ((cc & 15) != CC_E) - asm_guardcc(as, cc >> 8); /* Hiword compare without equality check. */ - as->mrm = mrm; /* Restore state. */ - if (ra_noreg(righthi)) { - int32_t imm = IR(ir->op2)->i; - if (imm == 0 && (cc & 0xa) != 0x2 && lefthi != RID_MRM) - emit_rr(as, XO_TEST, lefthi, lefthi); - else - emit_gmrmi(as, XG_ARITHi(XOg_CMP), lefthi, imm); - } else { - emit_mrm(as, XO_CMP, lefthi, righthi); - } -} -#endif - -/* -- Split register ops -------------------------------------------------- */ - -/* Hiword op of a split 32/32 or 64/64 bit op. Previous op is the loword op. */ -static void asm_hiop(ASMState *as, IRIns *ir) -{ - /* HIOP is marked as a store because it needs its own DCE logic. */ - int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ - if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; -#if LJ_32 && LJ_HASFFI - if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ - as->curins--; /* Always skip the CONV. */ - if (usehi || uselo) - asm_conv64(as, ir); - return; - } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ - asm_comp_int64(as, ir); - return; - } else if ((ir-1)->o == IR_XSTORE) { - if ((ir-1)->r != RID_SINK) - asm_fxstore(as, ir); - return; - } -#endif - if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ - switch ((ir-1)->o) { -#if LJ_32 && LJ_HASFFI - case IR_ADD: - as->flagmcp = NULL; - as->curins--; - asm_intarith(as, ir, XOg_ADC); - asm_intarith(as, ir-1, XOg_ADD); - break; - case IR_SUB: - as->flagmcp = NULL; - as->curins--; - asm_intarith(as, ir, XOg_SBB); - asm_intarith(as, ir-1, XOg_SUB); - break; - case IR_NEG: { - Reg dest = ra_dest(as, ir, RSET_GPR); - emit_rr(as, XO_GROUP3, XOg_NEG, dest); - emit_i8(as, 0); - emit_rr(as, XO_ARITHi8, XOg_ADC, dest); - ra_left(as, dest, ir->op1); - as->curins--; - asm_neg_not(as, ir-1, XOg_NEG); - break; - } - case IR_CNEWI: - /* Nothing to do here. Handled by CNEWI itself. */ - break; -#endif - case IR_CALLN: case IR_CALLL: case IR_CALLS: case IR_CALLXS: - if (!uselo) - ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ - break; - default: lj_assertA(0, "bad HIOP for op %d", (ir-1)->o); break; - } -} - -/* -- Profiling ----------------------------------------------------------- */ - -static void asm_prof(ASMState *as, IRIns *ir) -{ - UNUSED(ir); - asm_guardcc(as, CC_NE); - emit_i8(as, HOOK_PROFILE); - emit_rma(as, XO_GROUP3b, XOg_TEST, &J2G(as->J)->hookmask); -} - -/* -- Stack handling ------------------------------------------------------ */ - -/* Check Lua stack size for overflow. Use exit handler as fallback. */ -static void asm_stack_check(ASMState *as, BCReg topslot, - IRIns *irp, RegSet allow, ExitNo exitno) -{ - /* Try to get an unused temp. register, otherwise spill/restore eax. */ - Reg pbase = irp ? irp->r : RID_BASE; - Reg r = allow ? rset_pickbot(allow) : RID_EAX; - emit_jcc(as, CC_B, exitstub_addr(as->J, exitno)); - if (allow == RSET_EMPTY) /* Restore temp. register. */ - emit_rmro(as, XO_MOV, r|REX_64, RID_ESP, 0); - else - ra_modified(as, r); - emit_gri(as, XG_ARITHi(XOg_CMP), r|REX_GC64, (int32_t)(8*topslot)); - if (ra_hasreg(pbase) && pbase != r) - emit_rr(as, XO_ARITH(XOg_SUB), r|REX_GC64, pbase); - else -#if LJ_GC64 - emit_rmro(as, XO_ARITH(XOg_SUB), r|REX_64, RID_DISPATCH, - (int32_t)dispofs(as, &J2G(as->J)->jit_base)); -#else - emit_rmro(as, XO_ARITH(XOg_SUB), r, RID_NONE, - ptr2addr(&J2G(as->J)->jit_base)); -#endif - emit_rmro(as, XO_MOV, r|REX_GC64, r, offsetof(lua_State, maxstack)); - emit_getgl(as, r, cur_L); - if (allow == RSET_EMPTY) /* Spill temp. register. */ - emit_rmro(as, XO_MOVto, r|REX_64, RID_ESP, 0); -} - -/* Restore Lua stack from on-trace state. */ -static void asm_stack_restore(ASMState *as, SnapShot *snap) -{ - SnapEntry *map = &as->T->snapmap[snap->mapofs]; -#if !LJ_FR2 || defined(LUA_USE_ASSERT) - SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1-LJ_FR2]; -#endif - MSize n, nent = snap->nent; - /* Store the value of all modified slots to the Lua stack. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - int32_t ofs = 8*((int32_t)s-1-LJ_FR2); - IRRef ref = snap_ref(sn); - IRIns *ir = IR(ref); - if ((sn & SNAP_NORESTORE)) - continue; - if ((sn & SNAP_KEYINDEX)) { - emit_movmroi(as, RID_BASE, ofs+4, LJ_KEYINDEX); - if (irref_isk(ref)) { - emit_movmroi(as, RID_BASE, ofs, ir->i); - } else { - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE)); - emit_movtomro(as, src, RID_BASE, ofs); - } - } else if (irt_isnum(ir->t)) { - Reg src = ra_alloc1(as, ref, RSET_FPR); - emit_rmro(as, XO_MOVSDto, src, RID_BASE, ofs); - } else { - lj_assertA(irt_ispri(ir->t) || irt_isaddr(ir->t) || - (LJ_DUALNUM && irt_isinteger(ir->t)), - "restore of IR type %d", irt_type(ir->t)); - if (!irref_isk(ref)) { - Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE)); -#if LJ_GC64 - if (irt_is64(ir->t)) { - /* TODO: 64 bit store + 32 bit load-modify-store is suboptimal. */ - emit_u32(as, irt_toitype(ir->t) << 15); - emit_rmro(as, XO_ARITHi, XOg_OR, RID_BASE, ofs+4); - } else if (LJ_DUALNUM && irt_isinteger(ir->t)) { - emit_movmroi(as, RID_BASE, ofs+4, LJ_TISNUM << 15); - } else { - emit_movmroi(as, RID_BASE, ofs+4, (irt_toitype(ir->t)<<15)|0x7fff); - } -#endif - emit_movtomro(as, REX_64IR(ir, src), RID_BASE, ofs); -#if LJ_GC64 - } else { - TValue k; - lj_ir_kvalue(as->J->L, &k, ir); - if (tvisnil(&k)) { - emit_i32(as, -1); - emit_rmro(as, XO_MOVmi, REX_64, RID_BASE, ofs); - } else { - emit_movmroi(as, RID_BASE, ofs+4, k.u32.hi); - emit_movmroi(as, RID_BASE, ofs, k.u32.lo); - } -#else - } else if (!irt_ispri(ir->t)) { - emit_movmroi(as, RID_BASE, ofs, ir->i); -#endif - } - if ((sn & (SNAP_CONT|SNAP_FRAME))) { -#if !LJ_FR2 - if (s != 0) /* Do not overwrite link to previous frame. */ - emit_movmroi(as, RID_BASE, ofs+4, (int32_t)(*flinks--)); -#endif -#if !LJ_GC64 - } else { - if (!(LJ_64 && irt_islightud(ir->t))) - emit_movmroi(as, RID_BASE, ofs+4, irt_toitype(ir->t)); -#endif - } - } - checkmclim(as); - } - lj_assertA(map + nent == flinks, "inconsistent frames in snapshot"); -} - -/* -- GC handling --------------------------------------------------------- */ - -/* Check GC threshold and do one or more GC steps. */ -static void asm_gc_check(ASMState *as) -{ - const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; - IRRef args[2]; - MCLabel l_end; - Reg tmp; - ra_evictset(as, RSET_SCRATCH); - l_end = emit_label(as); - /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ - asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ - emit_rr(as, XO_TEST, RID_RET, RID_RET); - args[0] = ASMREF_TMP1; /* global_State *g */ - args[1] = ASMREF_TMP2; /* MSize steps */ - asm_gencall(as, ci, args); - tmp = ra_releasetmp(as, ASMREF_TMP1); -#if LJ_GC64 - emit_rmro(as, XO_LEA, tmp|REX_64, RID_DISPATCH, GG_DISP2G); -#else - emit_loada(as, tmp, J2G(as->J)); -#endif - emit_loadi(as, ra_releasetmp(as, ASMREF_TMP2), as->gcsteps); - /* Jump around GC step if GC total < GC threshold. */ - emit_sjcc(as, CC_B, l_end); - emit_opgl(as, XO_ARITH(XOg_CMP), tmp|REX_GC64, gc.threshold); - emit_getgl(as, tmp, gc.total); - as->gcsteps = 0; - checkmclim(as); -} - -/* -- Loop handling ------------------------------------------------------- */ - -/* Fixup the loop branch. */ -static void asm_loop_fixup(ASMState *as) -{ - MCode *p = as->mctop; - MCode *target = as->mcp; - if (as->realign) { /* Realigned loops use short jumps. */ - as->realign = NULL; /* Stop another retry. */ - lj_assertA(((intptr_t)target & 15) == 0, "loop realign failed"); - if (as->loopinv) { /* Inverted loop branch? */ - p -= 5; - p[0] = XI_JMP; - lj_assertA(target - p >= -128, "loop realign failed"); - p[-1] = (MCode)(target - p); /* Patch sjcc. */ - if (as->loopinv == 2) - p[-3] = (MCode)(target - p + 2); /* Patch opt. short jp. */ - } else { - lj_assertA(target - p >= -128, "loop realign failed"); - p[-1] = (MCode)(int8_t)(target - p); /* Patch short jmp. */ - p[-2] = XI_JMPs; - } - } else { - MCode *newloop; - p[-5] = XI_JMP; - if (as->loopinv) { /* Inverted loop branch? */ - /* asm_guardcc already inverted the jcc and patched the jmp. */ - p -= 5; - newloop = target+4; - *(int32_t *)(p-4) = (int32_t)(target - p); /* Patch jcc. */ - if (as->loopinv == 2) { - *(int32_t *)(p-10) = (int32_t)(target - p + 6); /* Patch opt. jp. */ - newloop = target+8; - } - } else { /* Otherwise just patch jmp. */ - *(int32_t *)(p-4) = (int32_t)(target - p); - newloop = target+3; - } - /* Realign small loops and shorten the loop branch. */ - if (newloop >= p - 128) { - as->realign = newloop; /* Force a retry and remember alignment. */ - as->curins = as->stopins; /* Abort asm_trace now. */ - as->T->nins = as->orignins; /* Remove any added renames. */ - } - } -} - -/* Fixup the tail of the loop. */ -static void asm_loop_tail_fixup(ASMState *as) -{ - UNUSED(as); /* Nothing to do. */ -} - -/* -- Head of trace ------------------------------------------------------- */ - -/* Coalesce BASE register for a root trace. */ -static void asm_head_root_base(ASMState *as) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r) || irt_ismarked(ir->t)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (r != RID_BASE) - emit_rr(as, XO_MOV, r|REX_GC64, RID_BASE); - } -} - -/* Coalesce or reload BASE register for a side trace. */ -static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) -{ - IRIns *ir = IR(REF_BASE); - Reg r = ir->r; - if (ra_hasreg(r)) { - ra_free(as, r); - if (rset_test(as->modset, r) || irt_ismarked(ir->t)) - ir->r = RID_INIT; /* No inheritance for modified BASE register. */ - if (irp->r == r) { - rset_clear(allow, r); /* Mark same BASE register as coalesced. */ - } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { - /* Move from coalesced parent reg. */ - rset_clear(allow, irp->r); - emit_rr(as, XO_MOV, r|REX_GC64, irp->r); - } else { - emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ - } - } - return allow; -} - -/* -- Tail of trace ------------------------------------------------------- */ - -/* Fixup the tail code. */ -static void asm_tail_fixup(ASMState *as, TraceNo lnk) -{ - /* Note: don't use as->mcp swap + emit_*: emit_op overwrites more bytes. */ - MCode *p = as->mctop; - MCode *target, *q; - int32_t spadj = as->T->spadjust; - if (spadj == 0) { - p -= LJ_64 ? 7 : 6; - } else { - MCode *p1; - /* Patch stack adjustment. */ - if (checki8(spadj)) { - p -= 3; - p1 = p-6; - *p1 = (MCode)spadj; - } else { - p1 = p-9; - *(int32_t *)p1 = spadj; - } -#if LJ_64 - p1[-3] = 0x48; -#endif - p1[-2] = (MCode)(checki8(spadj) ? XI_ARITHi8 : XI_ARITHi); - p1[-1] = MODRM(XM_REG, XOg_ADD, RID_ESP); - } - /* Patch exit branch. */ - target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; - *(int32_t *)(p-4) = jmprel(as->J, p, target); - p[-5] = XI_JMP; - /* Drop unused mcode tail. Fill with NOPs to make the prefetcher happy. */ - for (q = as->mctop-1; q >= p; q--) - *q = XI_NOP; - as->mctop = p; -} - -/* Prepare tail of code. */ -static void asm_tail_prep(ASMState *as) -{ - MCode *p = as->mctop; - /* Realign and leave room for backwards loop branch or exit branch. */ - if (as->realign) { - int i = ((int)(intptr_t)as->realign) & 15; - /* Fill unused mcode tail with NOPs to make the prefetcher happy. */ - while (i-- > 0) - *--p = XI_NOP; - as->mctop = p; - p -= (as->loopinv ? 5 : 2); /* Space for short/near jmp. */ - } else { - p -= 5; /* Space for exit branch (near jmp). */ - } - if (as->loopref) { - as->invmcp = as->mcp = p; - } else { - /* Leave room for ESP adjustment: add esp, imm or lea esp, [esp+imm] */ - as->mcp = p - (LJ_64 ? 7 : 6); - as->invmcp = NULL; - } -} - -/* -- Trace setup --------------------------------------------------------- */ - -/* Ensure there are enough stack slots for call arguments. */ -static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) -{ - IRRef args[CCI_NARGS_MAX*2]; - int nslots; - asm_collectargs(as, ir, ci, args); - nslots = asm_count_call_slots(as, ci, args); - if (nslots > as->evenspill) /* Leave room for args in stack slots. */ - as->evenspill = nslots; -#if LJ_64 - return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); -#else - return irt_isfp(ir->t) ? REGSP_INIT : REGSP_HINT(RID_RET); -#endif -} - -/* Target-specific setup. */ -static void asm_setup_target(ASMState *as) -{ - asm_exitstub_setup(as, as->T->nsnap); - as->mrm.base = 0; -} - -/* -- Trace patching ------------------------------------------------------ */ - -static const uint8_t map_op1[256] = { -0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x20, -0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x51,0x51, -0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51, -0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51,0x92,0x92,0x92,0x92,0x52,0x45,0x10,0x51, -#if LJ_64 -0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14, -#else -0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51, -#endif -0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51, -0x51,0x51,0x92,0x92,0x10,0x10,0x12,0x11,0x45,0x86,0x52,0x93,0x51,0x51,0x51,0x51, -0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52, -0x93,0x86,0x93,0x93,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92, -0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x51,0x47,0x51,0x51,0x51,0x51,0x51, -#if LJ_64 -0x59,0x59,0x59,0x59,0x51,0x51,0x51,0x51,0x52,0x45,0x51,0x51,0x51,0x51,0x51,0x51, -#else -0x55,0x55,0x55,0x55,0x51,0x51,0x51,0x51,0x52,0x45,0x51,0x51,0x51,0x51,0x51,0x51, -#endif -0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05, -0x93,0x93,0x53,0x51,0x70,0x71,0x93,0x86,0x54,0x51,0x53,0x51,0x51,0x52,0x51,0x51, -0x92,0x92,0x92,0x92,0x52,0x52,0x51,0x51,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92, -0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x45,0x45,0x47,0x52,0x51,0x51,0x51,0x51, -0x10,0x51,0x10,0x10,0x51,0x51,0x63,0x66,0x51,0x51,0x51,0x51,0x51,0x51,0x92,0x92 -}; - -static const uint8_t map_op2[256] = { -0x93,0x93,0x93,0x93,0x52,0x52,0x52,0x52,0x52,0x52,0x51,0x52,0x51,0x93,0x52,0x94, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x34,0x51,0x35,0x51,0x51,0x51,0x51,0x51, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x53,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x94,0x54,0x54,0x54,0x93,0x93,0x93,0x52,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x52,0x52,0x52,0x93,0x94,0x93,0x51,0x51,0x52,0x52,0x52,0x93,0x94,0x93,0x93,0x93, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x94,0x93,0x93,0x93,0x93,0x93, -0x93,0x93,0x94,0x93,0x94,0x94,0x94,0x93,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93, -0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x52 -}; - -static uint32_t asm_x86_inslen(const uint8_t* p) -{ - uint32_t result = 0; - uint32_t prefixes = 0; - uint32_t x = map_op1[*p]; - for (;;) { - switch (x >> 4) { - case 0: return result + x + (prefixes & 4); - case 1: prefixes |= x; x = map_op1[*++p]; result++; break; - case 2: x = map_op2[*++p]; break; - case 3: p++; goto mrm; - case 4: result -= (prefixes & 2); /* fallthrough */ - case 5: return result + (x & 15); - case 6: /* Group 3. */ - if (p[1] & 0x38) x = 2; - else if ((prefixes & 2) && (x == 0x66)) x = 4; - goto mrm; - case 7: /* VEX c4/c5. */ - if (LJ_32 && p[1] < 0xc0) { - x = 2; - goto mrm; - } - if (x == 0x70) { - x = *++p & 0x1f; - result++; - if (x >= 2) { - p += 2; - result += 2; - goto mrm; - } - } - p++; - result++; - x = map_op2[*++p]; - break; - case 8: result -= (prefixes & 2); /* fallthrough */ - case 9: mrm: /* ModR/M and possibly SIB. */ - result += (x & 15); - x = *++p; - switch (x >> 6) { - case 0: if ((x & 7) == 5) return result + 4; break; - case 1: result++; break; - case 2: result += 4; break; - case 3: return result; - } - if ((x & 7) == 4) { - result++; - if (x < 0x40 && (p[1] & 7) == 5) result += 4; - } - return result; - } - } -} - -/* Patch exit jumps of existing machine code to a new target. */ -void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) -{ - MCode *p = T->mcode; - MCode *mcarea = lj_mcode_patch(J, p, 0); - MSize len = T->szmcode; - MCode *px = exitstub_addr(J, exitno) - 6; - MCode *pe = p+len-6; - MCode *pgc = NULL; -#if LJ_GC64 - uint32_t statei = (uint32_t)(GG_OFS(g.vmstate) - GG_OFS(dispatch)); -#else - uint32_t statei = u32ptr(&J2G(J)->vmstate); -#endif - if (len > 5 && p[len-5] == XI_JMP && p+len-6 + *(int32_t *)(p+len-4) == px) - *(int32_t *)(p+len-4) = jmprel(J, p+len, target); - /* Do not patch parent exit for a stack check. Skip beyond vmstate update. */ - for (; p < pe; p += asm_x86_inslen(p)) { - intptr_t ofs = LJ_GC64 ? (p[0] & 0xf0) == 0x40 : LJ_64; - if (*(uint32_t *)(p+2+ofs) == statei && p[ofs+LJ_GC64-LJ_64] == XI_MOVmi) - break; - } - lj_assertJ(p < pe, "instruction length decoder failed"); - for (; p < pe; p += asm_x86_inslen(p)) { - if ((*(uint16_t *)p & 0xf0ff) == 0x800f && p + *(int32_t *)(p+2) == px && - p != pgc) { - *(int32_t *)(p+2) = jmprel(J, p+6, target); - } else if (*p == XI_CALL && - (void *)(p+5+*(int32_t *)(p+1)) == (void *)lj_gc_step_jit) { - pgc = p+7; /* Do not patch GC check exit. */ - } - } - lj_mcode_sync(T->mcode, T->mcode + T->szmcode); - lj_mcode_patch(J, mcarea, 1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_assert.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_assert.c deleted file mode 100644 index 4b713b2..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_assert.c +++ /dev/null @@ -1,28 +0,0 @@ -/* -** Internal assertions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_assert_c -#define LUA_CORE - -#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) - -#include - -#include "lj_obj.h" - -void lj_assert_fail(global_State *g, const char *file, int line, - const char *func, const char *fmt, ...) -{ - va_list argp; - va_start(argp, fmt); - fprintf(stderr, "LuaJIT ASSERT %s:%d: %s: ", file, line, func); - vfprintf(stderr, fmt, argp); - fputc('\n', stderr); - va_end(argp); - UNUSED(g); /* May be NULL. TODO: optionally dump state. */ - abort(); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.c deleted file mode 100644 index b692cb5..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.c +++ /dev/null @@ -1,14 +0,0 @@ -/* -** Bytecode instruction modes. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_bc_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_bc.h" - -/* Bytecode offsets and bytecode instruction modes. */ -#include "lj_bcdef.h" - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.h deleted file mode 100644 index 02356e5..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bc.h +++ /dev/null @@ -1,265 +0,0 @@ -/* -** Bytecode instruction format. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_BC_H -#define _LJ_BC_H - -#include "lj_def.h" -#include "lj_arch.h" - -/* Bytecode instruction format, 32 bit wide, fields of 8 or 16 bit: -** -** +----+----+----+----+ -** | B | C | A | OP | Format ABC -** +----+----+----+----+ -** | D | A | OP | Format AD -** +-------------------- -** MSB LSB -** -** In-memory instructions are always stored in host byte order. -*/ - -/* Operand ranges and related constants. */ -#define BCMAX_A 0xff -#define BCMAX_B 0xff -#define BCMAX_C 0xff -#define BCMAX_D 0xffff -#define BCBIAS_J 0x8000 -#define NO_REG BCMAX_A -#define NO_JMP (~(BCPos)0) - -/* Macros to get instruction fields. */ -#define bc_op(i) ((BCOp)((i)&0xff)) -#define bc_a(i) ((BCReg)(((i)>>8)&0xff)) -#define bc_b(i) ((BCReg)((i)>>24)) -#define bc_c(i) ((BCReg)(((i)>>16)&0xff)) -#define bc_d(i) ((BCReg)((i)>>16)) -#define bc_j(i) ((ptrdiff_t)bc_d(i)-BCBIAS_J) - -/* Macros to set instruction fields. */ -#define setbc_byte(p, x, ofs) \ - ((uint8_t *)(p))[LJ_ENDIAN_SELECT(ofs, 3-ofs)] = (uint8_t)(x) -#define setbc_op(p, x) setbc_byte(p, (x), 0) -#define setbc_a(p, x) setbc_byte(p, (x), 1) -#define setbc_b(p, x) setbc_byte(p, (x), 3) -#define setbc_c(p, x) setbc_byte(p, (x), 2) -#define setbc_d(p, x) \ - ((uint16_t *)(p))[LJ_ENDIAN_SELECT(1, 0)] = (uint16_t)(x) -#define setbc_j(p, x) setbc_d(p, (BCPos)((int32_t)(x)+BCBIAS_J)) - -/* Macros to compose instructions. */ -#define BCINS_ABC(o, a, b, c) \ - (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(b)<<24)|((BCIns)(c)<<16)) -#define BCINS_AD(o, a, d) \ - (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(d)<<16)) -#define BCINS_AJ(o, a, j) BCINS_AD(o, a, (BCPos)((int32_t)(j)+BCBIAS_J)) - -/* Bytecode instruction definition. Order matters, see below. -** -** (name, filler, Amode, Bmode, Cmode or Dmode, metamethod) -** -** The opcode name suffixes specify the type for RB/RC or RD: -** V = variable slot -** S = string const -** N = number const -** P = primitive type (~itype) -** B = unsigned byte literal -** M = multiple args/results -*/ -#define BCDEF(_) \ - /* Comparison ops. ORDER OPR. */ \ - _(ISLT, var, ___, var, lt) \ - _(ISGE, var, ___, var, lt) \ - _(ISLE, var, ___, var, le) \ - _(ISGT, var, ___, var, le) \ - \ - _(ISEQV, var, ___, var, eq) \ - _(ISNEV, var, ___, var, eq) \ - _(ISEQS, var, ___, str, eq) \ - _(ISNES, var, ___, str, eq) \ - _(ISEQN, var, ___, num, eq) \ - _(ISNEN, var, ___, num, eq) \ - _(ISEQP, var, ___, pri, eq) \ - _(ISNEP, var, ___, pri, eq) \ - \ - /* Unary test and copy ops. */ \ - _(ISTC, dst, ___, var, ___) \ - _(ISFC, dst, ___, var, ___) \ - _(IST, ___, ___, var, ___) \ - _(ISF, ___, ___, var, ___) \ - _(ISTYPE, var, ___, lit, ___) \ - _(ISNUM, var, ___, lit, ___) \ - \ - /* Unary ops. */ \ - _(MOV, dst, ___, var, ___) \ - _(NOT, dst, ___, var, ___) \ - _(UNM, dst, ___, var, unm) \ - _(LEN, dst, ___, var, len) \ - \ - /* Binary ops. ORDER OPR. VV last, POW must be next. */ \ - _(ADDVN, dst, var, num, add) \ - _(SUBVN, dst, var, num, sub) \ - _(MULVN, dst, var, num, mul) \ - _(DIVVN, dst, var, num, div) \ - _(MODVN, dst, var, num, mod) \ - \ - _(ADDNV, dst, var, num, add) \ - _(SUBNV, dst, var, num, sub) \ - _(MULNV, dst, var, num, mul) \ - _(DIVNV, dst, var, num, div) \ - _(MODNV, dst, var, num, mod) \ - \ - _(ADDVV, dst, var, var, add) \ - _(SUBVV, dst, var, var, sub) \ - _(MULVV, dst, var, var, mul) \ - _(DIVVV, dst, var, var, div) \ - _(MODVV, dst, var, var, mod) \ - \ - _(POW, dst, var, var, pow) \ - _(CAT, dst, rbase, rbase, concat) \ - \ - /* Constant ops. */ \ - _(KSTR, dst, ___, str, ___) \ - _(KCDATA, dst, ___, cdata, ___) \ - _(KSHORT, dst, ___, lits, ___) \ - _(KNUM, dst, ___, num, ___) \ - _(KPRI, dst, ___, pri, ___) \ - _(KNIL, base, ___, base, ___) \ - \ - /* Upvalue and function ops. */ \ - _(UGET, dst, ___, uv, ___) \ - _(USETV, uv, ___, var, ___) \ - _(USETS, uv, ___, str, ___) \ - _(USETN, uv, ___, num, ___) \ - _(USETP, uv, ___, pri, ___) \ - _(UCLO, rbase, ___, jump, ___) \ - _(FNEW, dst, ___, func, gc) \ - \ - /* Table ops. */ \ - _(TNEW, dst, ___, lit, gc) \ - _(TDUP, dst, ___, tab, gc) \ - _(GGET, dst, ___, str, index) \ - _(GSET, var, ___, str, newindex) \ - _(TGETV, dst, var, var, index) \ - _(TGETS, dst, var, str, index) \ - _(TGETB, dst, var, lit, index) \ - _(TGETR, dst, var, var, index) \ - _(TSETV, var, var, var, newindex) \ - _(TSETS, var, var, str, newindex) \ - _(TSETB, var, var, lit, newindex) \ - _(TSETM, base, ___, num, newindex) \ - _(TSETR, var, var, var, newindex) \ - \ - /* Calls and vararg handling. T = tail call. */ \ - _(CALLM, base, lit, lit, call) \ - _(CALL, base, lit, lit, call) \ - _(CALLMT, base, ___, lit, call) \ - _(CALLT, base, ___, lit, call) \ - _(ITERC, base, lit, lit, call) \ - _(ITERN, base, lit, lit, call) \ - _(VARG, base, lit, lit, ___) \ - _(ISNEXT, base, ___, jump, ___) \ - \ - /* Returns. */ \ - _(RETM, base, ___, lit, ___) \ - _(RET, rbase, ___, lit, ___) \ - _(RET0, rbase, ___, lit, ___) \ - _(RET1, rbase, ___, lit, ___) \ - \ - /* Loops and branches. I/J = interp/JIT, I/C/L = init/call/loop. */ \ - _(FORI, base, ___, jump, ___) \ - _(JFORI, base, ___, jump, ___) \ - \ - _(FORL, base, ___, jump, ___) \ - _(IFORL, base, ___, jump, ___) \ - _(JFORL, base, ___, lit, ___) \ - \ - _(ITERL, base, ___, jump, ___) \ - _(IITERL, base, ___, jump, ___) \ - _(JITERL, base, ___, lit, ___) \ - \ - _(LOOP, rbase, ___, jump, ___) \ - _(ILOOP, rbase, ___, jump, ___) \ - _(JLOOP, rbase, ___, lit, ___) \ - \ - _(JMP, rbase, ___, jump, ___) \ - \ - /* Function headers. I/J = interp/JIT, F/V/C = fixarg/vararg/C func. */ \ - _(FUNCF, rbase, ___, ___, ___) \ - _(IFUNCF, rbase, ___, ___, ___) \ - _(JFUNCF, rbase, ___, lit, ___) \ - _(FUNCV, rbase, ___, ___, ___) \ - _(IFUNCV, rbase, ___, ___, ___) \ - _(JFUNCV, rbase, ___, lit, ___) \ - _(FUNCC, rbase, ___, ___, ___) \ - _(FUNCCW, rbase, ___, ___, ___) - -/* Bytecode opcode numbers. */ -typedef enum { -#define BCENUM(name, ma, mb, mc, mt) BC_##name, -BCDEF(BCENUM) -#undef BCENUM - BC__MAX -} BCOp; - -LJ_STATIC_ASSERT((int)BC_ISEQV+1 == (int)BC_ISNEV); -LJ_STATIC_ASSERT(((int)BC_ISEQV^1) == (int)BC_ISNEV); -LJ_STATIC_ASSERT(((int)BC_ISEQS^1) == (int)BC_ISNES); -LJ_STATIC_ASSERT(((int)BC_ISEQN^1) == (int)BC_ISNEN); -LJ_STATIC_ASSERT(((int)BC_ISEQP^1) == (int)BC_ISNEP); -LJ_STATIC_ASSERT(((int)BC_ISLT^1) == (int)BC_ISGE); -LJ_STATIC_ASSERT(((int)BC_ISLE^1) == (int)BC_ISGT); -LJ_STATIC_ASSERT(((int)BC_ISLT^3) == (int)BC_ISGT); -LJ_STATIC_ASSERT((int)BC_IST-(int)BC_ISTC == (int)BC_ISF-(int)BC_ISFC); -LJ_STATIC_ASSERT((int)BC_CALLT-(int)BC_CALL == (int)BC_CALLMT-(int)BC_CALLM); -LJ_STATIC_ASSERT((int)BC_CALLMT + 1 == (int)BC_CALLT); -LJ_STATIC_ASSERT((int)BC_RETM + 1 == (int)BC_RET); -LJ_STATIC_ASSERT((int)BC_FORL + 1 == (int)BC_IFORL); -LJ_STATIC_ASSERT((int)BC_FORL + 2 == (int)BC_JFORL); -LJ_STATIC_ASSERT((int)BC_ITERL + 1 == (int)BC_IITERL); -LJ_STATIC_ASSERT((int)BC_ITERL + 2 == (int)BC_JITERL); -LJ_STATIC_ASSERT((int)BC_LOOP + 1 == (int)BC_ILOOP); -LJ_STATIC_ASSERT((int)BC_LOOP + 2 == (int)BC_JLOOP); -LJ_STATIC_ASSERT((int)BC_FUNCF + 1 == (int)BC_IFUNCF); -LJ_STATIC_ASSERT((int)BC_FUNCF + 2 == (int)BC_JFUNCF); -LJ_STATIC_ASSERT((int)BC_FUNCV + 1 == (int)BC_IFUNCV); -LJ_STATIC_ASSERT((int)BC_FUNCV + 2 == (int)BC_JFUNCV); - -/* This solves a circular dependency problem, change as needed. */ -#define FF_next_N 4 - -/* Stack slots used by FORI/FORL, relative to operand A. */ -enum { - FORL_IDX, FORL_STOP, FORL_STEP, FORL_EXT -}; - -/* Bytecode operand modes. ORDER BCMode */ -typedef enum { - BCMnone, BCMdst, BCMbase, BCMvar, BCMrbase, BCMuv, /* Mode A must be <= 7 */ - BCMlit, BCMlits, BCMpri, BCMnum, BCMstr, BCMtab, BCMfunc, BCMjump, BCMcdata, - BCM_max -} BCMode; -#define BCM___ BCMnone - -#define bcmode_a(op) ((BCMode)(lj_bc_mode[op] & 7)) -#define bcmode_b(op) ((BCMode)((lj_bc_mode[op]>>3) & 15)) -#define bcmode_c(op) ((BCMode)((lj_bc_mode[op]>>7) & 15)) -#define bcmode_d(op) bcmode_c(op) -#define bcmode_hasd(op) ((lj_bc_mode[op] & (15<<3)) == (BCMnone<<3)) -#define bcmode_mm(op) ((MMS)(lj_bc_mode[op]>>11)) - -#define BCMODE(name, ma, mb, mc, mm) \ - (BCM##ma|(BCM##mb<<3)|(BCM##mc<<7)|(MM_##mm<<11)), -#define BCMODE_FF 0 - -static LJ_AINLINE int bc_isret(BCOp op) -{ - return (op == BC_RETM || op == BC_RET || op == BC_RET0 || op == BC_RET1); -} - -LJ_DATA const uint16_t lj_bc_mode[]; -LJ_DATA const uint16_t lj_bc_ofs[]; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcdump.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcdump.h deleted file mode 100644 index 69da16e..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcdump.h +++ /dev/null @@ -1,68 +0,0 @@ -/* -** Bytecode dump definitions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_BCDUMP_H -#define _LJ_BCDUMP_H - -#include "lj_obj.h" -#include "lj_lex.h" - -/* -- Bytecode dump format ------------------------------------------------ */ - -/* -** dump = header proto+ 0U -** header = ESC 'L' 'J' versionB flagsU [namelenU nameB*] -** proto = lengthU pdata -** pdata = phead bcinsW* uvdataH* kgc* knum* [debugB*] -** phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU -** [debuglenU [firstlineU numlineU]] -** kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* } -** knum = intU0 | (loU1 hiU) -** ktab = narrayU nhashU karray* khash* -** karray = ktabk -** khash = ktabk ktabk -** ktabk = ktabtypeU { intU | (loU hiU) | strB* } -** -** B = 8 bit, H = 16 bit, W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1 -*/ - -/* Bytecode dump header. */ -#define BCDUMP_HEAD1 0x1b -#define BCDUMP_HEAD2 0x4c -#define BCDUMP_HEAD3 0x4a - -/* If you perform *any* kind of private modifications to the bytecode itself -** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher. -*/ -#define BCDUMP_VERSION 2 - -/* Compatibility flags. */ -#define BCDUMP_F_BE 0x01 -#define BCDUMP_F_STRIP 0x02 -#define BCDUMP_F_FFI 0x04 -#define BCDUMP_F_FR2 0x08 - -#define BCDUMP_F_KNOWN (BCDUMP_F_FR2*2-1) - -/* Type codes for the GC constants of a prototype. Plus length for strings. */ -enum { - BCDUMP_KGC_CHILD, BCDUMP_KGC_TAB, BCDUMP_KGC_I64, BCDUMP_KGC_U64, - BCDUMP_KGC_COMPLEX, BCDUMP_KGC_STR -}; - -/* Type codes for the keys/values of a constant table. */ -enum { - BCDUMP_KTAB_NIL, BCDUMP_KTAB_FALSE, BCDUMP_KTAB_TRUE, - BCDUMP_KTAB_INT, BCDUMP_KTAB_NUM, BCDUMP_KTAB_STR -}; - -/* -- Bytecode reader/writer ---------------------------------------------- */ - -LJ_FUNC int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, - void *data, int strip); -LJ_FUNC GCproto *lj_bcread_proto(LexState *ls); -LJ_FUNC GCproto *lj_bcread(LexState *ls); - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcread.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcread.c deleted file mode 100644 index 2ce0570..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcread.c +++ /dev/null @@ -1,453 +0,0 @@ -/* -** Bytecode reader. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_bcread_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lualib.h" -#endif -#include "lj_lex.h" -#include "lj_bcdump.h" -#include "lj_state.h" -#include "lj_strfmt.h" - -/* Reuse some lexer fields for our own purposes. */ -#define bcread_flags(ls) ls->level -#define bcread_swap(ls) \ - ((bcread_flags(ls) & BCDUMP_F_BE) != LJ_BE*BCDUMP_F_BE) -#define bcread_oldtop(L, ls) restorestack(L, ls->lastline) -#define bcread_savetop(L, ls, top) \ - ls->lastline = (BCLine)savestack(L, (top)) - -/* -- Input buffer handling ----------------------------------------------- */ - -/* Throw reader error. */ -static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em) -{ - lua_State *L = ls->L; - const char *name = ls->chunkarg; - if (*name == BCDUMP_HEAD1) name = "(binary)"; - else if (*name == '@' || *name == '=') name++; - lj_strfmt_pushf(L, "%s: %s", name, err2msg(em)); - lj_err_throw(L, LUA_ERRSYNTAX); -} - -/* Refill buffer. */ -static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) -{ - lj_assertLS(len != 0, "empty refill"); - if (len > LJ_MAX_BUF || ls->c < 0) - bcread_error(ls, LJ_ERR_BCBAD); - do { - const char *buf; - size_t sz; - char *p = ls->sb.b; - MSize n = (MSize)(ls->pe - ls->p); - if (n) { /* Copy remainder to buffer. */ - if (sbuflen(&ls->sb)) { /* Move down in buffer. */ - lj_assertLS(ls->pe == ls->sb.w, "bad buffer pointer"); - if (ls->p != p) memmove(p, ls->p, n); - } else { /* Copy from buffer provided by reader. */ - p = lj_buf_need(&ls->sb, len); - memcpy(p, ls->p, n); - } - ls->p = p; - ls->pe = p + n; - } - ls->sb.w = p + n; - buf = ls->rfunc(ls->L, ls->rdata, &sz); /* Get more data from reader. */ - if (buf == NULL || sz == 0) { /* EOF? */ - if (need) bcread_error(ls, LJ_ERR_BCBAD); - ls->c = -1; /* Only bad if we get called again. */ - break; - } - if (sz >= LJ_MAX_BUF - n) lj_err_mem(ls->L); - if (n) { /* Append to buffer. */ - n += (MSize)sz; - p = lj_buf_need(&ls->sb, n < len ? len : n); - memcpy(ls->sb.w, buf, sz); - ls->sb.w = p + n; - ls->p = p; - ls->pe = p + n; - } else { /* Return buffer provided by reader. */ - ls->p = buf; - ls->pe = buf + sz; - } - } while ((MSize)(ls->pe - ls->p) < len); -} - -/* Need a certain number of bytes. */ -static LJ_AINLINE void bcread_need(LexState *ls, MSize len) -{ - if (LJ_UNLIKELY((MSize)(ls->pe - ls->p) < len)) - bcread_fill(ls, len, 1); -} - -/* Want to read up to a certain number of bytes, but may need less. */ -static LJ_AINLINE void bcread_want(LexState *ls, MSize len) -{ - if (LJ_UNLIKELY((MSize)(ls->pe - ls->p) < len)) - bcread_fill(ls, len, 0); -} - -/* Return memory block from buffer. */ -static LJ_AINLINE uint8_t *bcread_mem(LexState *ls, MSize len) -{ - uint8_t *p = (uint8_t *)ls->p; - ls->p += len; - lj_assertLS(ls->p <= ls->pe, "buffer read overflow"); - return p; -} - -/* Copy memory block from buffer. */ -static void bcread_block(LexState *ls, void *q, MSize len) -{ - memcpy(q, bcread_mem(ls, len), len); -} - -/* Read byte from buffer. */ -static LJ_AINLINE uint32_t bcread_byte(LexState *ls) -{ - lj_assertLS(ls->p < ls->pe, "buffer read overflow"); - return (uint32_t)(uint8_t)*ls->p++; -} - -/* Read ULEB128 value from buffer. */ -static LJ_AINLINE uint32_t bcread_uleb128(LexState *ls) -{ - uint32_t v = lj_buf_ruleb128(&ls->p); - lj_assertLS(ls->p <= ls->pe, "buffer read overflow"); - return v; -} - -/* Read top 32 bits of 33 bit ULEB128 value from buffer. */ -static uint32_t bcread_uleb128_33(LexState *ls) -{ - const uint8_t *p = (const uint8_t *)ls->p; - uint32_t v = (*p++ >> 1); - if (LJ_UNLIKELY(v >= 0x40)) { - int sh = -1; - v &= 0x3f; - do { - v |= ((*p & 0x7f) << (sh += 7)); - } while (*p++ >= 0x80); - } - ls->p = (char *)p; - lj_assertLS(ls->p <= ls->pe, "buffer read overflow"); - return v; -} - -/* -- Bytecode reader ----------------------------------------------------- */ - -/* Read debug info of a prototype. */ -static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg) -{ - void *lineinfo = (void *)proto_lineinfo(pt); - bcread_block(ls, lineinfo, sizedbg); - /* Swap lineinfo if the endianess differs. */ - if (bcread_swap(ls) && pt->numline >= 256) { - MSize i, n = pt->sizebc-1; - if (pt->numline < 65536) { - uint16_t *p = (uint16_t *)lineinfo; - for (i = 0; i < n; i++) p[i] = (uint16_t)((p[i] >> 8)|(p[i] << 8)); - } else { - uint32_t *p = (uint32_t *)lineinfo; - for (i = 0; i < n; i++) p[i] = lj_bswap(p[i]); - } - } -} - -/* Find pointer to varinfo. */ -static const void *bcread_varinfo(GCproto *pt) -{ - const uint8_t *p = proto_uvinfo(pt); - MSize n = pt->sizeuv; - if (n) while (*p++ || --n) ; - return p; -} - -/* Read a single constant key/value of a template table. */ -static void bcread_ktabk(LexState *ls, TValue *o) -{ - MSize tp = bcread_uleb128(ls); - if (tp >= BCDUMP_KTAB_STR) { - MSize len = tp - BCDUMP_KTAB_STR; - const char *p = (const char *)bcread_mem(ls, len); - setstrV(ls->L, o, lj_str_new(ls->L, p, len)); - } else if (tp == BCDUMP_KTAB_INT) { - setintV(o, (int32_t)bcread_uleb128(ls)); - } else if (tp == BCDUMP_KTAB_NUM) { - o->u32.lo = bcread_uleb128(ls); - o->u32.hi = bcread_uleb128(ls); - } else { - lj_assertLS(tp <= BCDUMP_KTAB_TRUE, "bad constant type %d", tp); - setpriV(o, ~tp); - } -} - -/* Read a template table. */ -static GCtab *bcread_ktab(LexState *ls) -{ - MSize narray = bcread_uleb128(ls); - MSize nhash = bcread_uleb128(ls); - GCtab *t = lj_tab_new(ls->L, narray, hsize2hbits(nhash)); - if (narray) { /* Read array entries. */ - MSize i; - TValue *o = tvref(t->array); - for (i = 0; i < narray; i++, o++) - bcread_ktabk(ls, o); - } - if (nhash) { /* Read hash entries. */ - MSize i; - for (i = 0; i < nhash; i++) { - TValue key; - bcread_ktabk(ls, &key); - lj_assertLS(!tvisnil(&key), "nil key"); - bcread_ktabk(ls, lj_tab_set(ls->L, t, &key)); - } - } - return t; -} - -/* Read GC constants of a prototype. */ -static void bcread_kgc(LexState *ls, GCproto *pt, MSize sizekgc) -{ - MSize i; - GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc; - for (i = 0; i < sizekgc; i++, kr++) { - MSize tp = bcread_uleb128(ls); - if (tp >= BCDUMP_KGC_STR) { - MSize len = tp - BCDUMP_KGC_STR; - const char *p = (const char *)bcread_mem(ls, len); - setgcref(*kr, obj2gco(lj_str_new(ls->L, p, len))); - } else if (tp == BCDUMP_KGC_TAB) { - setgcref(*kr, obj2gco(bcread_ktab(ls))); -#if LJ_HASFFI - } else if (tp != BCDUMP_KGC_CHILD) { - CTypeID id = tp == BCDUMP_KGC_COMPLEX ? CTID_COMPLEX_DOUBLE : - tp == BCDUMP_KGC_I64 ? CTID_INT64 : CTID_UINT64; - CTSize sz = tp == BCDUMP_KGC_COMPLEX ? 16 : 8; - GCcdata *cd = lj_cdata_new_(ls->L, id, sz); - TValue *p = (TValue *)cdataptr(cd); - setgcref(*kr, obj2gco(cd)); - p[0].u32.lo = bcread_uleb128(ls); - p[0].u32.hi = bcread_uleb128(ls); - if (tp == BCDUMP_KGC_COMPLEX) { - p[1].u32.lo = bcread_uleb128(ls); - p[1].u32.hi = bcread_uleb128(ls); - } -#endif - } else { - lua_State *L = ls->L; - lj_assertLS(tp == BCDUMP_KGC_CHILD, "bad constant type %d", tp); - if (L->top <= bcread_oldtop(L, ls)) /* Stack underflow? */ - bcread_error(ls, LJ_ERR_BCBAD); - L->top--; - setgcref(*kr, obj2gco(protoV(L->top))); - } - } -} - -/* Read number constants of a prototype. */ -static void bcread_knum(LexState *ls, GCproto *pt, MSize sizekn) -{ - MSize i; - TValue *o = mref(pt->k, TValue); - for (i = 0; i < sizekn; i++, o++) { - int isnum = (ls->p[0] & 1); - uint32_t lo = bcread_uleb128_33(ls); - if (isnum) { - o->u32.lo = lo; - o->u32.hi = bcread_uleb128(ls); - } else { - setintV(o, lo); - } - } -} - -/* Read bytecode instructions. */ -static void bcread_bytecode(LexState *ls, GCproto *pt, MSize sizebc) -{ - BCIns *bc = proto_bc(pt); - bc[0] = BCINS_AD((pt->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF, - pt->framesize, 0); - bcread_block(ls, bc+1, (sizebc-1)*(MSize)sizeof(BCIns)); - /* Swap bytecode instructions if the endianess differs. */ - if (bcread_swap(ls)) { - MSize i; - for (i = 1; i < sizebc; i++) bc[i] = lj_bswap(bc[i]); - } -} - -/* Read upvalue refs. */ -static void bcread_uv(LexState *ls, GCproto *pt, MSize sizeuv) -{ - if (sizeuv) { - uint16_t *uv = proto_uv(pt); - bcread_block(ls, uv, sizeuv*2); - /* Swap upvalue refs if the endianess differs. */ - if (bcread_swap(ls)) { - MSize i; - for (i = 0; i < sizeuv; i++) - uv[i] = (uint16_t)((uv[i] >> 8)|(uv[i] << 8)); - } - } -} - -/* Read a prototype. */ -GCproto *lj_bcread_proto(LexState *ls) -{ - GCproto *pt; - MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept; - MSize ofsk, ofsuv, ofsdbg; - MSize sizedbg = 0; - BCLine firstline = 0, numline = 0; - - /* Read prototype header. */ - flags = bcread_byte(ls); - numparams = bcread_byte(ls); - framesize = bcread_byte(ls); - sizeuv = bcread_byte(ls); - sizekgc = bcread_uleb128(ls); - sizekn = bcread_uleb128(ls); - sizebc = bcread_uleb128(ls) + 1; - if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) { - sizedbg = bcread_uleb128(ls); - if (sizedbg) { - firstline = bcread_uleb128(ls); - numline = bcread_uleb128(ls); - } - } - - /* Calculate total size of prototype including all colocated arrays. */ - sizept = (MSize)sizeof(GCproto) + - sizebc*(MSize)sizeof(BCIns) + - sizekgc*(MSize)sizeof(GCRef); - sizept = (sizept + (MSize)sizeof(TValue)-1) & ~((MSize)sizeof(TValue)-1); - ofsk = sizept; sizept += sizekn*(MSize)sizeof(TValue); - ofsuv = sizept; sizept += ((sizeuv+1)&~1)*2; - ofsdbg = sizept; sizept += sizedbg; - - /* Allocate prototype object and initialize its fields. */ - pt = (GCproto *)lj_mem_newgco(ls->L, (MSize)sizept); - pt->gct = ~LJ_TPROTO; - pt->numparams = (uint8_t)numparams; - pt->framesize = (uint8_t)framesize; - pt->sizebc = sizebc; - setmref(pt->k, (char *)pt + ofsk); - setmref(pt->uv, (char *)pt + ofsuv); - pt->sizekgc = 0; /* Set to zero until fully initialized. */ - pt->sizekn = sizekn; - pt->sizept = sizept; - pt->sizeuv = (uint8_t)sizeuv; - pt->flags = (uint8_t)flags; - pt->trace = 0; - setgcref(pt->chunkname, obj2gco(ls->chunkname)); - - /* Close potentially uninitialized gap between bc and kgc. */ - *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(sizekgc+1)) = 0; - - /* Read bytecode instructions and upvalue refs. */ - bcread_bytecode(ls, pt, sizebc); - bcread_uv(ls, pt, sizeuv); - - /* Read constants. */ - bcread_kgc(ls, pt, sizekgc); - pt->sizekgc = sizekgc; - bcread_knum(ls, pt, sizekn); - - /* Read and initialize debug info. */ - pt->firstline = firstline; - pt->numline = numline; - if (sizedbg) { - MSize sizeli = (sizebc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); - setmref(pt->lineinfo, (char *)pt + ofsdbg); - setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli); - bcread_dbg(ls, pt, sizedbg); - setmref(pt->varinfo, bcread_varinfo(pt)); - } else { - setmref(pt->lineinfo, NULL); - setmref(pt->uvinfo, NULL); - setmref(pt->varinfo, NULL); - } - return pt; -} - -/* Read and check header of bytecode dump. */ -static int bcread_header(LexState *ls) -{ - uint32_t flags; - bcread_want(ls, 3+5+5); - if (bcread_byte(ls) != BCDUMP_HEAD2 || - bcread_byte(ls) != BCDUMP_HEAD3 || - bcread_byte(ls) != BCDUMP_VERSION) return 0; - bcread_flags(ls) = flags = bcread_uleb128(ls); - if ((flags & ~(BCDUMP_F_KNOWN)) != 0) return 0; - if ((flags & BCDUMP_F_FR2) != LJ_FR2*BCDUMP_F_FR2) return 0; - if ((flags & BCDUMP_F_FFI)) { -#if LJ_HASFFI - lua_State *L = ls->L; - ctype_loadffi(L); -#else - return 0; -#endif - } - if ((flags & BCDUMP_F_STRIP)) { - ls->chunkname = lj_str_newz(ls->L, ls->chunkarg); - } else { - MSize len = bcread_uleb128(ls); - bcread_need(ls, len); - ls->chunkname = lj_str_new(ls->L, (const char *)bcread_mem(ls, len), len); - } - return 1; /* Ok. */ -} - -/* Read a bytecode dump. */ -GCproto *lj_bcread(LexState *ls) -{ - lua_State *L = ls->L; - lj_assertLS(ls->c == BCDUMP_HEAD1, "bad bytecode header"); - bcread_savetop(L, ls, L->top); - lj_buf_reset(&ls->sb); - /* Check for a valid bytecode dump header. */ - if (!bcread_header(ls)) - bcread_error(ls, LJ_ERR_BCFMT); - for (;;) { /* Process all prototypes in the bytecode dump. */ - GCproto *pt; - MSize len; - const char *startp; - /* Read length. */ - if (ls->p < ls->pe && ls->p[0] == 0) { /* Shortcut EOF. */ - ls->p++; - break; - } - bcread_want(ls, 5); - len = bcread_uleb128(ls); - if (!len) break; /* EOF */ - bcread_need(ls, len); - startp = ls->p; - pt = lj_bcread_proto(ls); - if (ls->p != startp + len) - bcread_error(ls, LJ_ERR_BCBAD); - setprotoV(L, L->top, pt); - incr_top(L); - } - if ((ls->pe != ls->p && !ls->endmark) || L->top-1 != bcread_oldtop(L, ls)) - bcread_error(ls, LJ_ERR_BCBAD); - /* Pop off last prototype. */ - L->top--; - return protoV(L->top); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcwrite.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcwrite.c deleted file mode 100644 index 2c70ff4..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_bcwrite.c +++ /dev/null @@ -1,372 +0,0 @@ -/* -** Bytecode writer. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_bcwrite_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_buf.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#if LJ_HASJIT -#include "lj_dispatch.h" -#include "lj_jit.h" -#endif -#include "lj_strfmt.h" -#include "lj_bcdump.h" -#include "lj_vm.h" - -/* Context for bytecode writer. */ -typedef struct BCWriteCtx { - SBuf sb; /* Output buffer. */ - GCproto *pt; /* Root prototype. */ - lua_Writer wfunc; /* Writer callback. */ - void *wdata; /* Writer callback data. */ - int strip; /* Strip debug info. */ - int status; /* Status from writer callback. */ -#ifdef LUA_USE_ASSERT - global_State *g; -#endif -} BCWriteCtx; - -#ifdef LUA_USE_ASSERT -#define lj_assertBCW(c, ...) lj_assertG_(ctx->g, (c), __VA_ARGS__) -#else -#define lj_assertBCW(c, ...) ((void)ctx) -#endif - -/* -- Bytecode writer ----------------------------------------------------- */ - -/* Write a single constant key/value of a template table. */ -static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) -{ - char *p = lj_buf_more(&ctx->sb, 1+10); - if (tvisstr(o)) { - const GCstr *str = strV(o); - MSize len = str->len; - p = lj_buf_more(&ctx->sb, 5+len); - p = lj_strfmt_wuleb128(p, BCDUMP_KTAB_STR+len); - p = lj_buf_wmem(p, strdata(str), len); - } else if (tvisint(o)) { - *p++ = BCDUMP_KTAB_INT; - p = lj_strfmt_wuleb128(p, intV(o)); - } else if (tvisnum(o)) { - if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */ - lua_Number num = numV(o); - int32_t k = lj_num2int(num); - if (num == (lua_Number)k) { /* -0 is never a constant. */ - *p++ = BCDUMP_KTAB_INT; - p = lj_strfmt_wuleb128(p, k); - ctx->sb.w = p; - return; - } - } - *p++ = BCDUMP_KTAB_NUM; - p = lj_strfmt_wuleb128(p, o->u32.lo); - p = lj_strfmt_wuleb128(p, o->u32.hi); - } else { - lj_assertBCW(tvispri(o), "unhandled type %d", itype(o)); - *p++ = BCDUMP_KTAB_NIL+~itype(o); - } - ctx->sb.w = p; -} - -/* Write a template table. */ -static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t) -{ - MSize narray = 0, nhash = 0; - if (t->asize > 0) { /* Determine max. length of array part. */ - ptrdiff_t i; - TValue *array = tvref(t->array); - for (i = (ptrdiff_t)t->asize-1; i >= 0; i--) - if (!tvisnil(&array[i])) - break; - narray = (MSize)(i+1); - } - if (t->hmask > 0) { /* Count number of used hash slots. */ - MSize i, hmask = t->hmask; - Node *node = noderef(t->node); - for (i = 0; i <= hmask; i++) - nhash += !tvisnil(&node[i].val); - } - /* Write number of array slots and hash slots. */ - p = lj_strfmt_wuleb128(p, narray); - p = lj_strfmt_wuleb128(p, nhash); - ctx->sb.w = p; - if (narray) { /* Write array entries (may contain nil). */ - MSize i; - TValue *o = tvref(t->array); - for (i = 0; i < narray; i++, o++) - bcwrite_ktabk(ctx, o, 1); - } - if (nhash) { /* Write hash entries. */ - MSize i = nhash; - Node *node = noderef(t->node) + t->hmask; - for (;; node--) - if (!tvisnil(&node->val)) { - bcwrite_ktabk(ctx, &node->key, 0); - bcwrite_ktabk(ctx, &node->val, 1); - if (--i == 0) break; - } - } -} - -/* Write GC constants of a prototype. */ -static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt) -{ - MSize i, sizekgc = pt->sizekgc; - GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc; - for (i = 0; i < sizekgc; i++, kr++) { - GCobj *o = gcref(*kr); - MSize tp, need = 1; - char *p; - /* Determine constant type and needed size. */ - if (o->gch.gct == ~LJ_TSTR) { - tp = BCDUMP_KGC_STR + gco2str(o)->len; - need = 5+gco2str(o)->len; - } else if (o->gch.gct == ~LJ_TPROTO) { - lj_assertBCW((pt->flags & PROTO_CHILD), "prototype has unexpected child"); - tp = BCDUMP_KGC_CHILD; -#if LJ_HASFFI - } else if (o->gch.gct == ~LJ_TCDATA) { - CTypeID id = gco2cd(o)->ctypeid; - need = 1+4*5; - if (id == CTID_INT64) { - tp = BCDUMP_KGC_I64; - } else if (id == CTID_UINT64) { - tp = BCDUMP_KGC_U64; - } else { - lj_assertBCW(id == CTID_COMPLEX_DOUBLE, - "bad cdata constant CTID %d", id); - tp = BCDUMP_KGC_COMPLEX; - } -#endif - } else { - lj_assertBCW(o->gch.gct == ~LJ_TTAB, - "bad constant GC type %d", o->gch.gct); - tp = BCDUMP_KGC_TAB; - need = 1+2*5; - } - /* Write constant type. */ - p = lj_buf_more(&ctx->sb, need); - p = lj_strfmt_wuleb128(p, tp); - /* Write constant data (if any). */ - if (tp >= BCDUMP_KGC_STR) { - p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len); - } else if (tp == BCDUMP_KGC_TAB) { - bcwrite_ktab(ctx, p, gco2tab(o)); - continue; -#if LJ_HASFFI - } else if (tp != BCDUMP_KGC_CHILD) { - cTValue *q = (TValue *)cdataptr(gco2cd(o)); - p = lj_strfmt_wuleb128(p, q[0].u32.lo); - p = lj_strfmt_wuleb128(p, q[0].u32.hi); - if (tp == BCDUMP_KGC_COMPLEX) { - p = lj_strfmt_wuleb128(p, q[1].u32.lo); - p = lj_strfmt_wuleb128(p, q[1].u32.hi); - } -#endif - } - ctx->sb.w = p; - } -} - -/* Write number constants of a prototype. */ -static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt) -{ - MSize i, sizekn = pt->sizekn; - cTValue *o = mref(pt->k, TValue); - char *p = lj_buf_more(&ctx->sb, 10*sizekn); - for (i = 0; i < sizekn; i++, o++) { - int32_t k; - if (tvisint(o)) { - k = intV(o); - goto save_int; - } else { - /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */ - if (!LJ_DUALNUM) { /* Narrow number constants to integers. */ - lua_Number num = numV(o); - k = lj_num2int(num); - if (num == (lua_Number)k) { /* -0 is never a constant. */ - save_int: - p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u)); - if (k < 0) - p[-1] = (p[-1] & 7) | ((k>>27) & 0x18); - continue; - } - } - p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u))); - if (o->u32.lo >= 0x80000000u) - p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18); - p = lj_strfmt_wuleb128(p, o->u32.hi); - } - } - ctx->sb.w = p; -} - -/* Write bytecode instructions. */ -static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt) -{ - MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */ -#if LJ_HASJIT - uint8_t *q = (uint8_t *)p; -#endif - p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns)); - UNUSED(ctx); -#if LJ_HASJIT - /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */ - if ((pt->flags & PROTO_ILOOP) || pt->trace) { - jit_State *J = L2J(sbufL(&ctx->sb)); - MSize i; - for (i = 0; i < nbc; i++, q += sizeof(BCIns)) { - BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)]; - if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP || - op == BC_JFORI) { - q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL); - } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { - BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8); - memcpy(q, &traceref(J, rd)->startins, 4); - } - } - } -#endif - return p; -} - -/* Write prototype. */ -static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) -{ - MSize sizedbg = 0; - char *p; - - /* Recursively write children of prototype. */ - if ((pt->flags & PROTO_CHILD)) { - ptrdiff_t i, n = pt->sizekgc; - GCRef *kr = mref(pt->k, GCRef) - 1; - for (i = 0; i < n; i++, kr--) { - GCobj *o = gcref(*kr); - if (o->gch.gct == ~LJ_TPROTO) - bcwrite_proto(ctx, gco2pt(o)); - } - } - - /* Start writing the prototype info to a buffer. */ - p = lj_buf_need(&ctx->sb, - 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); - p += 5; /* Leave room for final size. */ - - /* Write prototype header. */ - *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI)); - *p++ = pt->numparams; - *p++ = pt->framesize; - *p++ = pt->sizeuv; - p = lj_strfmt_wuleb128(p, pt->sizekgc); - p = lj_strfmt_wuleb128(p, pt->sizekn); - p = lj_strfmt_wuleb128(p, pt->sizebc-1); - if (!ctx->strip) { - if (proto_lineinfo(pt)) - sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt); - p = lj_strfmt_wuleb128(p, sizedbg); - if (sizedbg) { - p = lj_strfmt_wuleb128(p, pt->firstline); - p = lj_strfmt_wuleb128(p, pt->numline); - } - } - - /* Write bytecode instructions and upvalue refs. */ - p = bcwrite_bytecode(ctx, p, pt); - p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2); - ctx->sb.w = p; - - /* Write constants. */ - bcwrite_kgc(ctx, pt); - bcwrite_knum(ctx, pt); - - /* Write debug info, if not stripped. */ - if (sizedbg) { - p = lj_buf_more(&ctx->sb, sizedbg); - p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg); - ctx->sb.w = p; - } - - /* Pass buffer to writer function. */ - if (ctx->status == 0) { - MSize n = sbuflen(&ctx->sb) - 5; - MSize nn = (lj_fls(n)+8)*9 >> 6; - char *q = ctx->sb.b + (5 - nn); - p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */ - lj_assertBCW(p == ctx->sb.b + 5, "bad ULEB128 write"); - ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata); - } -} - -/* Write header of bytecode dump. */ -static void bcwrite_header(BCWriteCtx *ctx) -{ - GCstr *chunkname = proto_chunkname(ctx->pt); - const char *name = strdata(chunkname); - MSize len = chunkname->len; - char *p = lj_buf_need(&ctx->sb, 5+5+len); - *p++ = BCDUMP_HEAD1; - *p++ = BCDUMP_HEAD2; - *p++ = BCDUMP_HEAD3; - *p++ = BCDUMP_VERSION; - *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) + - LJ_BE*BCDUMP_F_BE + - ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0) + - LJ_FR2*BCDUMP_F_FR2; - if (!ctx->strip) { - p = lj_strfmt_wuleb128(p, len); - p = lj_buf_wmem(p, name, len); - } - ctx->status = ctx->wfunc(sbufL(&ctx->sb), ctx->sb.b, - (MSize)(p - ctx->sb.b), ctx->wdata); -} - -/* Write footer of bytecode dump. */ -static void bcwrite_footer(BCWriteCtx *ctx) -{ - if (ctx->status == 0) { - uint8_t zero = 0; - ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata); - } -} - -/* Protected callback for bytecode writer. */ -static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud) -{ - BCWriteCtx *ctx = (BCWriteCtx *)ud; - UNUSED(L); UNUSED(dummy); - lj_buf_need(&ctx->sb, 1024); /* Avoids resize for most prototypes. */ - bcwrite_header(ctx); - bcwrite_proto(ctx, ctx->pt); - bcwrite_footer(ctx); - return NULL; -} - -/* Write bytecode for a prototype. */ -int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data, - int strip) -{ - BCWriteCtx ctx; - int status; - ctx.pt = pt; - ctx.wfunc = writer; - ctx.wdata = data; - ctx.strip = strip; - ctx.status = 0; -#ifdef LUA_USE_ASSERT - ctx.g = G(L); -#endif - lj_buf_init(L, &ctx.sb); - status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); - if (status == 0) status = ctx.status; - lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb); - return status; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.c deleted file mode 100644 index cf268af..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.c +++ /dev/null @@ -1,305 +0,0 @@ -/* -** Buffer handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_buf_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_strfmt.h" - -/* -- Buffer management --------------------------------------------------- */ - -static void buf_grow(SBuf *sb, MSize sz) -{ - MSize osz = sbufsz(sb), len = sbuflen(sb), nsz = osz; - char *b; - GCSize flag; - if (nsz < LJ_MIN_SBUF) nsz = LJ_MIN_SBUF; - while (nsz < sz) nsz += nsz; - flag = sbufflag(sb); - if ((flag & SBUF_FLAG_COW)) { /* Copy-on-write semantics. */ - lj_assertG_(G(sbufL(sb)), sb->w == sb->e, "bad SBuf COW"); - b = (char *)lj_mem_new(sbufL(sb), nsz); - setsbufflag(sb, flag & ~(GCSize)SBUF_FLAG_COW); - setgcrefnull(sbufX(sb)->cowref); - memcpy(b, sb->b, osz); - } else { - b = (char *)lj_mem_realloc(sbufL(sb), sb->b, osz, nsz); - } - if ((flag & SBUF_FLAG_EXT)) { - sbufX(sb)->r = sbufX(sb)->r - sb->b + b; /* Adjust read pointer, too. */ - } - /* Adjust buffer pointers. */ - sb->b = b; - sb->w = b + len; - sb->e = b + nsz; - if ((flag & SBUF_FLAG_BORROW)) { /* Adjust borrowed buffer pointers. */ - SBuf *bsb = mref(sbufX(sb)->bsb, SBuf); - bsb->b = b; - bsb->w = b + len; - bsb->e = b + nsz; - } -} - -LJ_NOINLINE char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz) -{ - lj_assertG_(G(sbufL(sb)), sz > sbufsz(sb), "SBuf overflow"); - if (LJ_UNLIKELY(sz > LJ_MAX_BUF)) - lj_err_mem(sbufL(sb)); - buf_grow(sb, sz); - return sb->b; -} - -LJ_NOINLINE char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz) -{ - if (sbufisext(sb)) { - SBufExt *sbx = (SBufExt *)sb; - MSize len = sbufxlen(sbx); - if (LJ_UNLIKELY(sz > LJ_MAX_BUF || len + sz > LJ_MAX_BUF)) - lj_err_mem(sbufL(sbx)); - if (len + sz > sbufsz(sbx)) { /* Must grow. */ - buf_grow((SBuf *)sbx, len + sz); - } else if (sbufiscow(sb) || sbufxslack(sbx) < (sbufsz(sbx) >> 3)) { - /* Also grow to avoid excessive compactions, if slack < size/8. */ - buf_grow((SBuf *)sbx, sbuflen(sbx) + sz); /* Not sbufxlen! */ - return sbx->w; - } - if (sbx->r != sbx->b) { /* Compact by moving down. */ - memmove(sbx->b, sbx->r, len); - sbx->r = sbx->b; - sbx->w = sbx->b + len; - lj_assertG_(G(sbufL(sbx)), len + sz <= sbufsz(sbx), "bad SBuf compact"); - } - } else { - MSize len = sbuflen(sb); - lj_assertG_(G(sbufL(sb)), sz > sbufleft(sb), "SBuf overflow"); - if (LJ_UNLIKELY(sz > LJ_MAX_BUF || len + sz > LJ_MAX_BUF)) - lj_err_mem(sbufL(sb)); - buf_grow(sb, len + sz); - } - return sb->w; -} - -void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb) -{ - char *b = sb->b; - MSize osz = (MSize)(sb->e - b); - if (osz > 2*LJ_MIN_SBUF) { - MSize n = (MSize)(sb->w - b); - b = lj_mem_realloc(L, b, osz, (osz >> 1)); - sb->b = b; - sb->w = b + n; - sb->e = b + (osz >> 1); - } - lj_assertG_(G(sbufL(sb)), !sbufisext(sb), "YAGNI shrink SBufExt"); -} - -char * LJ_FASTCALL lj_buf_tmp(lua_State *L, MSize sz) -{ - SBuf *sb = &G(L)->tmpbuf; - setsbufL(sb, L); - return lj_buf_need(sb, sz); -} - -#if LJ_HASBUFFER && LJ_HASJIT -void lj_bufx_set(SBufExt *sbx, const char *p, MSize len, GCobj *ref) -{ - lua_State *L = sbufL(sbx); - lj_bufx_free(L, sbx); - lj_bufx_set_cow(L, sbx, p, len); - setgcref(sbx->cowref, ref); - lj_gc_objbarrier(L, (GCudata *)sbx - 1, ref); -} - -#if LJ_HASFFI -MSize LJ_FASTCALL lj_bufx_more(SBufExt *sbx, MSize sz) -{ - lj_buf_more((SBuf *)sbx, sz); - return sbufleft(sbx); -} -#endif -#endif - -/* -- Low-level buffer put operations ------------------------------------- */ - -SBuf *lj_buf_putmem(SBuf *sb, const void *q, MSize len) -{ - char *w = lj_buf_more(sb, len); - w = lj_buf_wmem(w, q, len); - sb->w = w; - return sb; -} - -#if LJ_HASJIT || LJ_HASFFI -static LJ_NOINLINE SBuf * LJ_FASTCALL lj_buf_putchar2(SBuf *sb, int c) -{ - char *w = lj_buf_more2(sb, 1); - *w++ = (char)c; - sb->w = w; - return sb; -} - -SBuf * LJ_FASTCALL lj_buf_putchar(SBuf *sb, int c) -{ - char *w = sb->w; - if (LJ_LIKELY(w < sb->e)) { - *w++ = (char)c; - sb->w = w; - return sb; - } - return lj_buf_putchar2(sb, c); -} -#endif - -SBuf * LJ_FASTCALL lj_buf_putstr(SBuf *sb, GCstr *s) -{ - MSize len = s->len; - char *w = lj_buf_more(sb, len); - w = lj_buf_wmem(w, strdata(s), len); - sb->w = w; - return sb; -} - -/* -- High-level buffer put operations ------------------------------------ */ - -SBuf * LJ_FASTCALL lj_buf_putstr_reverse(SBuf *sb, GCstr *s) -{ - MSize len = s->len; - char *w = lj_buf_more(sb, len), *e = w+len; - const char *q = strdata(s)+len-1; - while (w < e) - *w++ = *q--; - sb->w = w; - return sb; -} - -SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s) -{ - MSize len = s->len; - char *w = lj_buf_more(sb, len), *e = w+len; - const char *q = strdata(s); - for (; w < e; w++, q++) { - uint32_t c = *(unsigned char *)q; -#if LJ_TARGET_PPC - *w = c + ((c >= 'A' && c <= 'Z') << 5); -#else - if (c >= 'A' && c <= 'Z') c += 0x20; - *w = c; -#endif - } - sb->w = w; - return sb; -} - -SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s) -{ - MSize len = s->len; - char *w = lj_buf_more(sb, len), *e = w+len; - const char *q = strdata(s); - for (; w < e; w++, q++) { - uint32_t c = *(unsigned char *)q; -#if LJ_TARGET_PPC - *w = c - ((c >= 'a' && c <= 'z') << 5); -#else - if (c >= 'a' && c <= 'z') c -= 0x20; - *w = c; -#endif - } - sb->w = w; - return sb; -} - -SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep) -{ - MSize len = s->len; - if (rep > 0 && len) { - uint64_t tlen = (uint64_t)rep * len; - char *w; - if (LJ_UNLIKELY(tlen > LJ_MAX_STR)) - lj_err_mem(sbufL(sb)); - w = lj_buf_more(sb, (MSize)tlen); - if (len == 1) { /* Optimize a common case. */ - uint32_t c = strdata(s)[0]; - do { *w++ = c; } while (--rep > 0); - } else { - const char *e = strdata(s) + len; - do { - const char *q = strdata(s); - do { *w++ = *q++; } while (q < e); - } while (--rep > 0); - } - sb->w = w; - } - return sb; -} - -SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep, int32_t i, int32_t e) -{ - MSize seplen = sep ? sep->len : 0; - if (i <= e) { - for (;;) { - cTValue *o = lj_tab_getint(t, i); - char *w; - if (!o) { - badtype: /* Error: bad element type. */ - sb->w = (char *)(intptr_t)i; /* Store failing index. */ - return NULL; - } else if (tvisstr(o)) { - MSize len = strV(o)->len; - w = lj_buf_wmem(lj_buf_more(sb, len + seplen), strVdata(o), len); - } else if (tvisint(o)) { - w = lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT+seplen), intV(o)); - } else if (tvisnum(o)) { - w = lj_buf_more(lj_strfmt_putfnum(sb, STRFMT_G14, numV(o)), seplen); - } else { - goto badtype; - } - if (i++ == e) { - sb->w = w; - break; - } - if (seplen) w = lj_buf_wmem(w, strdata(sep), seplen); - sb->w = w; - } - } - return sb; -} - -/* -- Miscellaneous buffer operations ------------------------------------- */ - -GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb) -{ - return lj_str_new(sbufL(sb), sb->b, sbuflen(sb)); -} - -/* Concatenate two strings. */ -GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2) -{ - MSize len1 = s1->len, len2 = s2->len; - char *buf = lj_buf_tmp(L, len1 + len2); - memcpy(buf, strdata(s1), len1); - memcpy(buf+len1, strdata(s2), len2); - return lj_str_new(L, buf, len1 + len2); -} - -/* Read ULEB128 from buffer. */ -uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp) -{ - const uint8_t *w = (const uint8_t *)*pp; - uint32_t v = *w++; - if (LJ_UNLIKELY(v >= 0x80)) { - int sh = 0; - v &= 0x7f; - do { v |= ((*w & 0x7f) << (sh += 7)); } while (*w++ >= 0x80); - } - *pp = (const char *)w; - return v; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.h deleted file mode 100644 index 7611420..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_buf.h +++ /dev/null @@ -1,198 +0,0 @@ -/* -** Buffer handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_BUF_H -#define _LJ_BUF_H - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_str.h" - -/* Resizable string buffers. */ - -/* The SBuf struct definition is in lj_obj.h: -** char *w; Write pointer. -** char *e; End pointer. -** char *b; Base pointer. -** MRef L; lua_State, used for buffer resizing. Extension bits in 3 LSB. -*/ - -/* Extended string buffer. */ -typedef struct SBufExt { - SBufHeader; - union { - GCRef cowref; /* Copy-on-write object reference. */ - MRef bsb; /* Borrowed string buffer. */ - }; - char *r; /* Read pointer. */ - GCRef dict_str; /* Serialization string dictionary table. */ - GCRef dict_mt; /* Serialization metatable dictionary table. */ - int depth; /* Remaining recursion depth. */ -} SBufExt; - -#define sbufsz(sb) ((MSize)((sb)->e - (sb)->b)) -#define sbuflen(sb) ((MSize)((sb)->w - (sb)->b)) -#define sbufleft(sb) ((MSize)((sb)->e - (sb)->w)) -#define sbufxlen(sbx) ((MSize)((sbx)->w - (sbx)->r)) -#define sbufxslack(sbx) ((MSize)((sbx)->r - (sbx)->b)) - -#define SBUF_MASK_FLAG (7) -#define SBUF_MASK_L (~(GCSize)SBUF_MASK_FLAG) -#define SBUF_FLAG_EXT 1 /* Extended string buffer. */ -#define SBUF_FLAG_COW 2 /* Copy-on-write buffer. */ -#define SBUF_FLAG_BORROW 4 /* Borrowed string buffer. */ - -#define sbufL(sb) \ - ((lua_State *)(void *)(uintptr_t)(mrefu((sb)->L) & SBUF_MASK_L)) -#define setsbufL(sb, l) (setmref((sb)->L, (l))) -#define setsbufXL(sb, l, flag) \ - (setmrefu((sb)->L, (GCSize)(uintptr_t)(void *)(l) + (flag))) -#define setsbufXL_(sb, l) \ - (setmrefu((sb)->L, (GCSize)(uintptr_t)(void *)(l) | (mrefu((sb)->L) & SBUF_MASK_FLAG))) - -#define sbufflag(sb) (mrefu((sb)->L)) -#define sbufisext(sb) (sbufflag((sb)) & SBUF_FLAG_EXT) -#define sbufiscow(sb) (sbufflag((sb)) & SBUF_FLAG_COW) -#define sbufisborrow(sb) (sbufflag((sb)) & SBUF_FLAG_BORROW) -#define sbufiscoworborrow(sb) (sbufflag((sb)) & (SBUF_FLAG_COW|SBUF_FLAG_BORROW)) -#define sbufX(sb) \ - (lj_assertG_(G(sbufL(sb)), sbufisext(sb), "not an SBufExt"), (SBufExt *)(sb)) -#define setsbufflag(sb, flag) (setmrefu((sb)->L, (flag))) - -#define tvisbuf(o) \ - (LJ_HASBUFFER && tvisudata(o) && udataV(o)->udtype == UDTYPE_BUFFER) -#define bufV(o) check_exp(tvisbuf(o), ((SBufExt *)uddata(udataV(o)))) - -/* Buffer management */ -LJ_FUNC char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz); -LJ_FUNC char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz); -LJ_FUNC void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb); -LJ_FUNC char * LJ_FASTCALL lj_buf_tmp(lua_State *L, MSize sz); - -static LJ_AINLINE void lj_buf_init(lua_State *L, SBuf *sb) -{ - setsbufL(sb, L); - sb->w = sb->e = sb->b = NULL; -} - -static LJ_AINLINE void lj_buf_reset(SBuf *sb) -{ - sb->w = sb->b; -} - -static LJ_AINLINE SBuf *lj_buf_tmp_(lua_State *L) -{ - SBuf *sb = &G(L)->tmpbuf; - setsbufL(sb, L); - lj_buf_reset(sb); - return sb; -} - -static LJ_AINLINE void lj_buf_free(global_State *g, SBuf *sb) -{ - lj_assertG(!sbufisext(sb), "bad free of SBufExt"); - lj_mem_free(g, sb->b, sbufsz(sb)); -} - -static LJ_AINLINE char *lj_buf_need(SBuf *sb, MSize sz) -{ - if (LJ_UNLIKELY(sz > sbufsz(sb))) - return lj_buf_need2(sb, sz); - return sb->b; -} - -static LJ_AINLINE char *lj_buf_more(SBuf *sb, MSize sz) -{ - if (LJ_UNLIKELY(sz > sbufleft(sb))) - return lj_buf_more2(sb, sz); - return sb->w; -} - -/* Extended buffer management */ -static LJ_AINLINE void lj_bufx_init(lua_State *L, SBufExt *sbx) -{ - memset(sbx, 0, sizeof(SBufExt)); - setsbufXL(sbx, L, SBUF_FLAG_EXT); -} - -static LJ_AINLINE void lj_bufx_set_borrow(lua_State *L, SBufExt *sbx, SBuf *sb) -{ - setsbufXL(sbx, L, SBUF_FLAG_EXT | SBUF_FLAG_BORROW); - setmref(sbx->bsb, sb); - sbx->r = sbx->w = sbx->b = sb->b; - sbx->e = sb->e; -} - -static LJ_AINLINE void lj_bufx_set_cow(lua_State *L, SBufExt *sbx, - const char *p, MSize len) -{ - setsbufXL(sbx, L, SBUF_FLAG_EXT | SBUF_FLAG_COW); - sbx->r = sbx->b = (char *)p; - sbx->w = sbx->e = (char *)p + len; -} - -static LJ_AINLINE void lj_bufx_reset(SBufExt *sbx) -{ - if (sbufiscow(sbx)) { - setmrefu(sbx->L, (mrefu(sbx->L) & ~(GCSize)SBUF_FLAG_COW)); - setgcrefnull(sbx->cowref); - sbx->b = sbx->e = NULL; - } - sbx->r = sbx->w = sbx->b; -} - -static LJ_AINLINE void lj_bufx_free(lua_State *L, SBufExt *sbx) -{ - if (!sbufiscoworborrow(sbx)) lj_mem_free(G(L), sbx->b, sbufsz(sbx)); - setsbufXL(sbx, L, SBUF_FLAG_EXT); - setgcrefnull(sbx->cowref); - sbx->r = sbx->w = sbx->b = sbx->e = NULL; -} - -#if LJ_HASBUFFER && LJ_HASJIT -LJ_FUNC void lj_bufx_set(SBufExt *sbx, const char *p, MSize len, GCobj *o); -#if LJ_HASFFI -LJ_FUNC MSize LJ_FASTCALL lj_bufx_more(SBufExt *sbx, MSize sz); -#endif -#endif - -/* Low-level buffer put operations */ -LJ_FUNC SBuf *lj_buf_putmem(SBuf *sb, const void *q, MSize len); -#if LJ_HASJIT || LJ_HASFFI -LJ_FUNC SBuf * LJ_FASTCALL lj_buf_putchar(SBuf *sb, int c); -#endif -LJ_FUNC SBuf * LJ_FASTCALL lj_buf_putstr(SBuf *sb, GCstr *s); - -static LJ_AINLINE char *lj_buf_wmem(char *p, const void *q, MSize len) -{ - return (char *)memcpy(p, q, len) + len; -} - -static LJ_AINLINE void lj_buf_putb(SBuf *sb, int c) -{ - char *w = lj_buf_more(sb, 1); - *w++ = (char)c; - sb->w = w; -} - -/* High-level buffer put operations */ -LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_reverse(SBuf *sb, GCstr *s); -LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s); -LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s); -LJ_FUNC SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep); -LJ_FUNC SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep, - int32_t i, int32_t e); - -/* Miscellaneous buffer operations */ -LJ_FUNCA GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb); -LJ_FUNC GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2); -LJ_FUNC uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp); - -static LJ_AINLINE GCstr *lj_buf_str(lua_State *L, SBuf *sb) -{ - return lj_str_new(L, sb->b, sbuflen(sb)); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.c deleted file mode 100644 index 1a2a058..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.c +++ /dev/null @@ -1,432 +0,0 @@ -/* -** C data arithmetic. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_ir.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" -#include "lj_carith.h" -#include "lj_strscan.h" - -/* -- C data arithmetic --------------------------------------------------- */ - -/* Binary operands of an operator converted to ctypes. */ -typedef struct CDArith { - uint8_t *p[2]; - CType *ct[2]; -} CDArith; - -/* Check arguments for arithmetic metamethods. */ -static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca) -{ - TValue *o = L->base; - int ok = 1; - MSize i; - if (o+1 >= L->top) - lj_err_argt(L, 1, LUA_TCDATA); - for (i = 0; i < 2; i++, o++) { - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - CTypeID id = (CTypeID)cd->ctypeid; - CType *ct = ctype_raw(cts, id); - uint8_t *p = (uint8_t *)cdataptr(cd); - if (ctype_isptr(ct->info)) { - p = (uint8_t *)cdata_getptr(p, ct->size); - if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); - } else if (ctype_isfunc(ct->info)) { - p = (uint8_t *)*(void **)p; - ct = ctype_get(cts, - lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); - } - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - ca->ct[i] = ct; - ca->p[i] = p; - } else if (tvisint(o)) { - ca->ct[i] = ctype_get(cts, CTID_INT32); - ca->p[i] = (uint8_t *)&o->i; - } else if (tvisnum(o)) { - ca->ct[i] = ctype_get(cts, CTID_DOUBLE); - ca->p[i] = (uint8_t *)&o->n; - } else if (tvisnil(o)) { - ca->ct[i] = ctype_get(cts, CTID_P_VOID); - ca->p[i] = (uint8_t *)0; - } else if (tvisstr(o)) { - TValue *o2 = i == 0 ? o+1 : o-1; - CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid); - ca->ct[i] = NULL; - ca->p[i] = (uint8_t *)strVdata(o); - ok = 0; - if (ctype_isenum(ct->info)) { - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs); - if (cct && ctype_isconstval(cct->info)) { - ca->ct[i] = ctype_child(cts, cct); - ca->p[i] = (uint8_t *)&cct->size; /* Assumes ct does not grow. */ - ok = 1; - } else { - ca->ct[1-i] = ct; /* Use enum to improve error message. */ - ca->p[1-i] = NULL; - break; - } - } - } else { - ca->ct[i] = NULL; - ca->p[i] = (void *)(intptr_t)1; /* To make it unequal. */ - ok = 0; - } - } - return ok; -} - -/* Pointer arithmetic. */ -static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm) -{ - CType *ctp = ca->ct[0]; - uint8_t *pp = ca->p[0]; - ptrdiff_t idx; - CTSize sz; - CTypeID id; - GCcdata *cd; - if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) { - if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) && - (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) { - uint8_t *pp2 = ca->p[1]; - if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */ - setboolV(L->top-1, (pp == pp2)); - return 1; - } - if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL)) - return 0; - if (mm == MM_sub) { /* Pointer difference. */ - intptr_t diff; - sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ - if (sz == 0 || sz == CTSIZE_INVALID) - return 0; - diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz; - /* All valid pointer differences on x64 are in (-2^47, +2^47), - ** which fits into a double without loss of precision. - */ - setintptrV(L->top-1, (int32_t)diff); - return 1; - } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */ - setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2)); - return 1; - } else { - lj_assertL(mm == MM_le, "bad metamethod %d", mm); - setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2)); - return 1; - } - } - if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info))) - return 0; - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1], - (uint8_t *)&idx, ca->p[1], 0); - if (mm == MM_sub) idx = -idx; - } else if (mm == MM_add && ctype_isnum(ctp->info) && - (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) { - /* Swap pointer and index. */ - ctp = ca->ct[1]; pp = ca->p[1]; - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0], - (uint8_t *)&idx, ca->p[0], 0); - } else { - return 0; - } - sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ - if (sz == CTSIZE_INVALID) - return 0; - pp += idx*(int32_t)sz; /* Compute pointer + index. */ - id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), - CTSIZE_PTR); - cd = lj_cdata_new(cts, id, CTSIZE_PTR); - *(uint8_t **)cdataptr(cd) = pp; - setcdataV(L, L->top-1, cd); - lj_gc_check(L); - return 1; -} - -/* 64 bit integer arithmetic. */ -static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm) -{ - if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 && - ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) { - CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) || - ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ? - CTID_UINT64 : CTID_INT64; - CType *ct = ctype_get(cts, id); - GCcdata *cd; - uint64_t u0, u1, *up; - lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0); - if (mm != MM_unm) - lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0); - switch (mm) { - case MM_eq: - setboolV(L->top-1, (u0 == u1)); - return 1; - case MM_lt: - setboolV(L->top-1, - id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1)); - return 1; - case MM_le: - setboolV(L->top-1, - id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1)); - return 1; - default: break; - } - cd = lj_cdata_new(cts, id, 8); - up = (uint64_t *)cdataptr(cd); - setcdataV(L, L->top-1, cd); - switch (mm) { - case MM_add: *up = u0 + u1; break; - case MM_sub: *up = u0 - u1; break; - case MM_mul: *up = u0 * u1; break; - case MM_div: - if (id == CTID_INT64) - *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1); - else - *up = lj_carith_divu64(u0, u1); - break; - case MM_mod: - if (id == CTID_INT64) - *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1); - else - *up = lj_carith_modu64(u0, u1); - break; - case MM_pow: - if (id == CTID_INT64) - *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1); - else - *up = lj_carith_powu64(u0, u1); - break; - case MM_unm: *up = (uint64_t)-(int64_t)u0; break; - default: - lj_assertL(0, "bad metamethod %d", mm); - break; - } - lj_gc_check(L); - return 1; - } - return 0; -} - -/* Handle ctype arithmetic metamethods. */ -static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm) -{ - cTValue *tv = NULL; - if (tviscdata(L->base)) { - CTypeID id = cdataV(L->base)->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - } - if (!tv && L->base+1 < L->top && tviscdata(L->base+1)) { - CTypeID id = cdataV(L->base+1)->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, mm); - } - if (!tv) { - const char *repr[2]; - int i, isenum = -1, isstr = -1; - if (mm == MM_eq) { /* Equality checks never raise an error. */ - int eq = ca->p[0] == ca->p[1]; - setboolV(L->top-1, eq); - setboolV(&G(L)->tmptv2, eq); /* Remember for trace recorder. */ - return 1; - } - for (i = 0; i < 2; i++) { - if (ca->ct[i] && tviscdata(L->base+i)) { - if (ctype_isenum(ca->ct[i]->info)) isenum = i; - repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL)); - } else { - if (tvisstr(&L->base[i])) isstr = i; - repr[i] = lj_typename(&L->base[i]); - } - } - if ((isenum ^ isstr) == 1) - lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]); - lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN : - mm == MM_concat ? LJ_ERR_FFI_BADCONCAT : - mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH, - repr[0], repr[1]); - } - return lj_meta_tailcall(L, tv); -} - -/* Arithmetic operators for cdata. */ -int lj_carith_op(lua_State *L, MMS mm) -{ - CTState *cts = ctype_cts(L); - CDArith ca; - if (carith_checkarg(L, cts, &ca) && mm != MM_len && mm != MM_concat) { - if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) { - copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */ - return 1; - } - } - return lj_carith_meta(L, cts, &ca, mm); -} - -/* -- 64 bit bit operations helpers --------------------------------------- */ - -#if LJ_64 -#define B64DEF(name) \ - static LJ_AINLINE uint64_t lj_carith_##name(uint64_t x, int32_t sh) -#else -/* Not inlined on 32 bit archs, since some of these are quite lengthy. */ -#define B64DEF(name) \ - uint64_t LJ_NOINLINE lj_carith_##name(uint64_t x, int32_t sh) -#endif - -B64DEF(shl64) { return x << (sh&63); } -B64DEF(shr64) { return x >> (sh&63); } -B64DEF(sar64) { return (uint64_t)((int64_t)x >> (sh&63)); } -B64DEF(rol64) { return lj_rol(x, (sh&63)); } -B64DEF(ror64) { return lj_ror(x, (sh&63)); } - -#undef B64DEF - -uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op) -{ - switch (op) { - case IR_BSHL-IR_BSHL: x = lj_carith_shl64(x, sh); break; - case IR_BSHR-IR_BSHL: x = lj_carith_shr64(x, sh); break; - case IR_BSAR-IR_BSHL: x = lj_carith_sar64(x, sh); break; - case IR_BROL-IR_BSHL: x = lj_carith_rol64(x, sh); break; - case IR_BROR-IR_BSHL: x = lj_carith_ror64(x, sh); break; - default: - lj_assertX(0, "bad shift op %d", op); - break; - } - return x; -} - -/* Equivalent to lj_lib_checkbit(), but handles cdata. */ -uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id) -{ - TValue *o = L->base + narg-1; - if (o >= L->top) { - err: - lj_err_argt(L, narg, LUA_TNUMBER); - } else if (LJ_LIKELY(tvisnumber(o))) { - /* Handled below. */ - } else if (tviscdata(o)) { - CTState *cts = ctype_cts(L); - uint8_t *sp = (uint8_t *)cdataptr(cdataV(o)); - CTypeID sid = cdataV(o)->ctypeid; - CType *s = ctype_get(cts, sid); - uint64_t x; - if (ctype_isref(s->info)) { - sp = *(void **)sp; - sid = ctype_cid(s->info); - } - s = ctype_raw(cts, sid); - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - if ((s->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) == - CTINFO(CT_NUM, CTF_UNSIGNED) && s->size == 8) - *id = CTID_UINT64; /* Use uint64_t, since it has the highest rank. */ - else if (!*id) - *id = CTID_INT64; /* Use int64_t, unless already set. */ - lj_cconv_ct_ct(cts, ctype_get(cts, *id), s, - (uint8_t *)&x, sp, CCF_ARG(narg)); - return x; - } else if (!(tvisstr(o) && lj_strscan_number(strV(o), o))) { - goto err; - } - if (LJ_LIKELY(tvisint(o))) { - return (uint32_t)intV(o); - } else { - int32_t i = lj_num2bit(numV(o)); - if (LJ_DUALNUM) setintV(o, i); - return (uint32_t)i; - } -} - -/* -- 64 bit integer arithmetic helpers ----------------------------------- */ - -#if LJ_32 && LJ_HASJIT -/* Signed/unsigned 64 bit multiplication. */ -int64_t lj_carith_mul64(int64_t a, int64_t b) -{ - return a * b; -} -#endif - -/* Unsigned 64 bit division. */ -uint64_t lj_carith_divu64(uint64_t a, uint64_t b) -{ - if (b == 0) return U64x(80000000,00000000); - return a / b; -} - -/* Signed 64 bit division. */ -int64_t lj_carith_divi64(int64_t a, int64_t b) -{ - if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1)) - return U64x(80000000,00000000); - return a / b; -} - -/* Unsigned 64 bit modulo. */ -uint64_t lj_carith_modu64(uint64_t a, uint64_t b) -{ - if (b == 0) return U64x(80000000,00000000); - return a % b; -} - -/* Signed 64 bit modulo. */ -int64_t lj_carith_modi64(int64_t a, int64_t b) -{ - if (b == 0) return U64x(80000000,00000000); - if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0; - return a % b; -} - -/* Unsigned 64 bit x^k. */ -uint64_t lj_carith_powu64(uint64_t x, uint64_t k) -{ - uint64_t y; - if (k == 0) - return 1; - for (; (k & 1) == 0; k >>= 1) x *= x; - y = x; - if ((k >>= 1) != 0) { - for (;;) { - x *= x; - if (k == 1) break; - if (k & 1) y *= x; - k >>= 1; - } - y *= x; - } - return y; -} - -/* Signed 64 bit x^k. */ -int64_t lj_carith_powi64(int64_t x, int64_t k) -{ - if (k == 0) - return 1; - if (k < 0) { - if (x == 0) - return U64x(7fffffff,ffffffff); - else if (x == 1) - return 1; - else if (x == -1) - return (k & 1) ? -1 : 1; - else - return 0; - } - return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.h deleted file mode 100644 index 9d6b1dc..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_carith.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -** C data arithmetic. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CARITH_H -#define _LJ_CARITH_H - -#include "lj_obj.h" - -#if LJ_HASFFI - -LJ_FUNC int lj_carith_op(lua_State *L, MMS mm); - -#if LJ_32 -LJ_FUNC uint64_t lj_carith_shl64(uint64_t x, int32_t sh); -LJ_FUNC uint64_t lj_carith_shr64(uint64_t x, int32_t sh); -LJ_FUNC uint64_t lj_carith_sar64(uint64_t x, int32_t sh); -LJ_FUNC uint64_t lj_carith_rol64(uint64_t x, int32_t sh); -LJ_FUNC uint64_t lj_carith_ror64(uint64_t x, int32_t sh); -#endif -LJ_FUNC uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op); -LJ_FUNC uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id); - -#if LJ_32 && LJ_HASJIT -LJ_FUNC int64_t lj_carith_mul64(int64_t x, int64_t k); -#endif -LJ_FUNC uint64_t lj_carith_divu64(uint64_t a, uint64_t b); -LJ_FUNC int64_t lj_carith_divi64(int64_t a, int64_t b); -LJ_FUNC uint64_t lj_carith_modu64(uint64_t a, uint64_t b); -LJ_FUNC int64_t lj_carith_modi64(int64_t a, int64_t b); -LJ_FUNC uint64_t lj_carith_powu64(uint64_t x, uint64_t k); -LJ_FUNC int64_t lj_carith_powi64(int64_t x, int64_t k); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.c deleted file mode 100644 index 25f54de..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.c +++ /dev/null @@ -1,1189 +0,0 @@ -/* -** FFI C call handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" -#include "lj_ccall.h" -#include "lj_trace.h" - -/* Target-specific handling of register arguments. */ -#if LJ_TARGET_X86 -/* -- x86 calling conventions --------------------------------------------- */ - -#if LJ_ABI_WIN - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs bigger than 8 by reference (on stack only). */ \ - cc->retref = (sz > 8); \ - if (cc->retref) cc->stack[nsp++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET - -#else - -#if LJ_TARGET_OSX - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs of size 1, 2, 4 or 8 in registers. */ \ - cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \ - if (cc->retref) { \ - if (ngpr < maxgpr) \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - else \ - cc->stack[nsp++] = (GPRArg)dp; \ - } else { /* Struct with single FP field ends up in FPR. */ \ - cc->resx87 = ccall_classify_struct(cts, ctr); \ - } - -#define CCALL_HANDLE_STRUCTRET2 \ - if (cc->resx87) sp = (uint8_t *)&cc->fpr[0]; \ - memcpy(dp, sp, ctr->size); - -#else - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference (in reg or on stack). */ \ - if (ngpr < maxgpr) \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - else \ - cc->stack[nsp++] = (GPRArg)dp; - -#endif - -#define CCALL_HANDLE_COMPLEXRET \ - /* Return complex float in GPRs and complex double by reference. */ \ - cc->retref = (sz > 8); \ - if (cc->retref) { \ - if (ngpr < maxgpr) \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - else \ - cc->stack[nsp++] = (GPRArg)dp; \ - } - -#endif - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (!cc->retref) \ - *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - ngpr = maxgpr; /* Pass all structs by value on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - isfp = 1; /* Pass complex by value on stack. */ - -#define CCALL_HANDLE_REGARG \ - if (!isfp) { /* Only non-FP values may be passed in registers. */ \ - if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \ - if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \ - } else if (ngpr + 1 <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_X64 && LJ_ABI_WIN -/* -- Windows/x64 calling conventions ------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs of size 1, 2, 4 or 8 in a GPR. */ \ - cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (!cc->retref) \ - *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \ - if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; /* Pass all other structs by reference. */ \ - } - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex float in a GPR and complex double by reference. */ \ - if (sz != 2*sizeof(float)) { \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; \ - } - -/* Windows/x64 argument registers are strictly positional (use ngpr). */ -#define CCALL_HANDLE_REGARG \ - if (isfp) { \ - if (ngpr < maxgpr) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \ - } else { \ - if (ngpr < maxgpr) { dp = &cc->gpr[ngpr++]; goto done; } \ - } - -#elif LJ_TARGET_X64 -/* -- POSIX/x64 calling conventions --------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - int rcl[2]; rcl[0] = rcl[1] = 0; \ - if (ccall_classify_struct(cts, ctr, rcl, 0)) { \ - cc->retref = 1; /* Return struct by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; \ - } else { \ - cc->retref = 0; /* Return small structs in registers. */ \ - } - -#define CCALL_HANDLE_STRUCTRET2 \ - int rcl[2]; rcl[0] = rcl[1] = 0; \ - ccall_classify_struct(cts, ctr, rcl, 0); \ - ccall_struct_ret(cc, rcl, dp, ctr->size); - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in one or two FPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPR. */ \ - *(int64_t *)dp = cc->fpr[0].l[0]; \ - } else { /* Copy non-contiguous complex double from FPRs. */ \ - ((int64_t *)dp)[0] = cc->fpr[0].l[0]; \ - ((int64_t *)dp)[1] = cc->fpr[1].l[0]; \ - } - -#define CCALL_HANDLE_STRUCTARG \ - int rcl[2]; rcl[0] = rcl[1] = 0; \ - if (!ccall_classify_struct(cts, d, rcl, 0)) { \ - cc->nsp = nsp; cc->ngpr = ngpr; cc->nfpr = nfpr; \ - if (ccall_struct_arg(cc, cts, d, rcl, o, narg)) goto err_nyi; \ - nsp = cc->nsp; ngpr = cc->ngpr; nfpr = cc->nfpr; \ - continue; \ - } /* Pass all other structs by value on stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - isfp = 2; /* Pass complex in FPRs or on stack. Needs postprocessing. */ - -#define CCALL_HANDLE_REGARG \ - if (isfp) { /* Try to pass argument in FPRs. */ \ - int n2 = ctype_isvector(d->info) ? 1 : n; \ - if (nfpr + n2 <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += n2; \ - goto done; \ - } \ - } else { /* Try to pass argument in GPRs. */ \ - /* Note that reordering is explicitly allowed in the x64 ABI. */ \ - if (n <= 2 && ngpr + n <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_ARM -/* -- ARM calling conventions --------------------------------------------- */ - -#if LJ_ABI_SOFTFP - -#define CCALL_HANDLE_STRUCTRET \ - /* Return structs of size <= 4 in a GPR. */ \ - cc->retref = !(sz <= 4); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - cc->retref = 1; /* Return all complex values by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET2 \ - UNUSED(dp); /* Nothing to do. */ - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -#define CCALL_HANDLE_REGARG_FP1 -#define CCALL_HANDLE_REGARG_FP2 - -#else - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = !ccall_classify_struct(cts, ctr, ct); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_STRUCTRET2 \ - if (ccall_classify_struct(cts, ctr, ct) > 1) sp = (uint8_t *)&cc->fpr[0]; \ - memcpy(dp, sp, ctr->size); - -#define CCALL_HANDLE_COMPLEXRET \ - if (!(ct->info & CTF_VARARG)) cc->retref = 0; /* Return complex in FPRs. */ - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (!(ct->info & CTF_VARARG)) memcpy(dp, &cc->fpr[0], ctr->size); - -#define CCALL_HANDLE_STRUCTARG \ - isfp = (ccall_classify_struct(cts, d, ct) > 1); - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - isfp = 1; /* Pass complex by value in FPRs or on stack. */ - -#define CCALL_HANDLE_REGARG_FP1 \ - if (isfp && !(ct->info & CTF_VARARG)) { \ - if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \ - if (nfpr + (n >> 1) <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += (n >> 1); \ - goto done; \ - } \ - } else { \ - if (sz > 1 && fprodd != nfpr) fprodd = 0; \ - if (fprodd) { \ - if (2*nfpr+n <= 2*CCALL_NARG_FPR+1) { \ - dp = (void *)&cc->fpr[fprodd-1].f[1]; \ - nfpr += (n >> 1); \ - if ((n & 1)) fprodd = 0; else fprodd = nfpr-1; \ - goto done; \ - } \ - } else { \ - if (2*nfpr+n <= 2*CCALL_NARG_FPR) { \ - dp = (void *)&cc->fpr[nfpr]; \ - nfpr += (n >> 1); \ - if ((n & 1)) fprodd = ++nfpr; else fprodd = 0; \ - goto done; \ - } \ - } \ - } \ - fprodd = 0; /* No reordering after the first FP value is on stack. */ \ - } else { - -#define CCALL_HANDLE_REGARG_FP2 } - -#endif - -#define CCALL_HANDLE_REGARG \ - CCALL_HANDLE_REGARG_FP1 \ - if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \ - if (ngpr < maxgpr) \ - ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - } \ - if (ngpr < maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - if (ngpr + n > maxgpr) { \ - nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ - if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ - ngpr = maxgpr; \ - } else { \ - ngpr += n; \ - } \ - goto done; \ - } CCALL_HANDLE_REGARG_FP2 - -#define CCALL_HANDLE_RET \ - if ((ct->info & CTF_VARARG)) sp = (uint8_t *)&cc->gpr[0]; - -#elif LJ_TARGET_ARM64 -/* -- ARM64 calling conventions ------------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = !ccall_classify_struct(cts, ctr); \ - if (cc->retref) cc->retp = dp; - -#define CCALL_HANDLE_STRUCTRET2 \ - unsigned int cl = ccall_classify_struct(cts, ctr); \ - if ((cl & 4)) { /* Combine float HFA from separate registers. */ \ - CTSize i = (cl >> 8) - 1; \ - do { ((uint32_t *)dp)[i] = cc->fpr[i].lo; } while (i--); \ - } else { \ - if (cl > 1) sp = (uint8_t *)&cc->fpr[0]; \ - memcpy(dp, sp, ctr->size); \ - } - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in one or two FPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \ - ((float *)dp)[0] = cc->fpr[0].f; \ - ((float *)dp)[1] = cc->fpr[1].f; \ - } else { /* Copy complex double from FPRs. */ \ - ((double *)dp)[0] = cc->fpr[0].d; \ - ((double *)dp)[1] = cc->fpr[1].d; \ - } - -#define CCALL_HANDLE_STRUCTARG \ - unsigned int cl = ccall_classify_struct(cts, d); \ - if (cl == 0) { /* Pass struct by reference. */ \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; \ - } else if (cl > 1) { /* Pass struct in FPRs or on stack. */ \ - isfp = (cl & 4) ? 2 : 1; \ - } /* else: Pass struct in GPRs or on stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in separate (!) FPRs or on stack. */ \ - isfp = sz == 2*sizeof(float) ? 2 : 1; - -#define CCALL_HANDLE_REGARG \ - if (LJ_TARGET_OSX && isva) { \ - /* IOS: All variadic arguments are on the stack. */ \ - } else if (isfp) { /* Try to pass argument in FPRs. */ \ - int n2 = ctype_isvector(d->info) ? 1 : \ - isfp == 1 ? n : (d->size >> (4-isfp)); \ - if (nfpr + n2 <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += n2; \ - goto done; \ - } else { \ - nfpr = CCALL_NARG_FPR; /* Prevent reordering. */ \ - if (LJ_TARGET_OSX && d->size < 8) goto err_nyi; \ - } \ - } else { /* Try to pass argument in GPRs. */ \ - if (!LJ_TARGET_OSX && (d->info & CTF_ALIGN) > CTALIGN_PTR) \ - ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr + n <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } else { \ - ngpr = maxgpr; /* Prevent reordering. */ \ - if (LJ_TARGET_OSX && d->size < 8) goto err_nyi; \ - } \ - } - -#if LJ_BE -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - sp = (uint8_t *)&cc->fpr[0].f; -#endif - - -#elif LJ_TARGET_PPC -/* -- PPC calling conventions --------------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in 2 or 4 GPRs. */ \ - cc->retref = 0; - -#define CCALL_HANDLE_COMPLEXRET2 \ - memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */ - -#define CCALL_HANDLE_STRUCTARG \ - rp = cdataptr(lj_cdata_new(cts, did, sz)); \ - sz = CTSIZE_PTR; /* Pass all structs by reference. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -#define CCALL_HANDLE_GPR \ - /* Try to pass argument in GPRs. */ \ - if (n > 1) { \ - /* int64_t or complex (float). */ \ - lj_assertL(n == 2 || n == 4, "bad GPR size %d", n); \ - if (ctype_isinteger(d->info) || ctype_isfp(d->info)) \ - ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ - else if (ngpr + n > maxgpr) \ - ngpr = maxgpr; /* Prevent reordering. */ \ - } \ - if (ngpr + n <= maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - -#if LJ_ABI_SOFTFP -#define CCALL_HANDLE_REGARG CCALL_HANDLE_GPR -#else -#define CCALL_HANDLE_REGARG \ - if (isfp) { /* Try to pass argument in FPRs. */ \ - if (nfpr + 1 <= CCALL_NARG_FPR) { \ - dp = &cc->fpr[nfpr]; \ - nfpr += 1; \ - d = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \ - goto done; \ - } \ - } else { \ - CCALL_HANDLE_GPR \ - } -#endif - -#if !LJ_ABI_SOFTFP -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - ctr = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ -#endif - -#elif LJ_TARGET_MIPS32 -/* -- MIPS o32 calling conventions ---------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = 1; /* Return all structs by reference. */ \ - cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in 1 or 2 FPRs. */ \ - cc->retref = 0; - -#if LJ_ABI_SOFTFP -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from GPRs. */ \ - ((intptr_t *)dp)[0] = cc->gpr[0]; \ - ((intptr_t *)dp)[1] = cc->gpr[1]; \ - } else { /* Copy complex double from GPRs. */ \ - ((intptr_t *)dp)[0] = cc->gpr[0]; \ - ((intptr_t *)dp)[1] = cc->gpr[1]; \ - ((intptr_t *)dp)[2] = cc->gpr[2]; \ - ((intptr_t *)dp)[3] = cc->gpr[3]; \ - } -#else -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \ - ((float *)dp)[0] = cc->fpr[0].f; \ - ((float *)dp)[1] = cc->fpr[1].f; \ - } else { /* Copy complex double from FPRs. */ \ - ((double *)dp)[0] = cc->fpr[0].d; \ - ((double *)dp)[1] = cc->fpr[1].d; \ - } -#endif - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -#define CCALL_HANDLE_GPR \ - if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \ - ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr < maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - if (ngpr + n > maxgpr) { \ - nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ - if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ - ngpr = maxgpr; \ - } else { \ - ngpr += n; \ - } \ - goto done; \ - } - -#if !LJ_ABI_SOFTFP /* MIPS32 hard-float */ -#define CCALL_HANDLE_REGARG \ - if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \ - /* Try to pass argument in FPRs. */ \ - dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \ - nfpr++; ngpr += n; \ - goto done; \ - } else { /* Try to pass argument in GPRs. */ \ - nfpr = CCALL_NARG_FPR; \ - CCALL_HANDLE_GPR \ - } -#else /* MIPS32 soft-float */ -#define CCALL_HANDLE_REGARG CCALL_HANDLE_GPR -#endif - -#if !LJ_ABI_SOFTFP -/* On MIPS64 soft-float, position of float return values is endian-dependant. */ -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - sp = (uint8_t *)&cc->fpr[0].f; -#endif - -#elif LJ_TARGET_MIPS64 -/* -- MIPS n64 calling conventions ---------------------------------------- */ - -#define CCALL_HANDLE_STRUCTRET \ - cc->retref = !(sz <= 16); \ - if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; - -#define CCALL_HANDLE_STRUCTRET2 \ - ccall_copy_struct(cc, ctr, dp, sp, ccall_classify_struct(cts, ctr, ct)); - -#define CCALL_HANDLE_COMPLEXRET \ - /* Complex values are returned in 1 or 2 FPRs. */ \ - cc->retref = 0; - -#if LJ_ABI_SOFTFP /* MIPS64 soft-float */ - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from GPRs. */ \ - ((intptr_t *)dp)[0] = cc->gpr[0]; \ - } else { /* Copy complex double from GPRs. */ \ - ((intptr_t *)dp)[0] = cc->gpr[0]; \ - ((intptr_t *)dp)[1] = cc->gpr[1]; \ - } - -#define CCALL_HANDLE_COMPLEXARG \ - /* Pass complex by value in 2 or 4 GPRs. */ - -/* Position of soft-float 'float' return value depends on endianess. */ -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - sp = (uint8_t *)cc->gpr + LJ_ENDIAN_SELECT(0, 4); - -#else /* MIPS64 hard-float */ - -#define CCALL_HANDLE_COMPLEXRET2 \ - if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \ - ((float *)dp)[0] = cc->fpr[0].f; \ - ((float *)dp)[1] = cc->fpr[1].f; \ - } else { /* Copy complex double from FPRs. */ \ - ((double *)dp)[0] = cc->fpr[0].d; \ - ((double *)dp)[1] = cc->fpr[1].d; \ - } - -#define CCALL_HANDLE_COMPLEXARG \ - if (sz == 2*sizeof(float)) { \ - isfp = 2; \ - if (ngpr < maxgpr) \ - sz *= 2; \ - } - -#define CCALL_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - sp = (uint8_t *)&cc->fpr[0].f; - -#endif - -#define CCALL_HANDLE_STRUCTARG \ - /* Pass all structs by value in registers and/or on the stack. */ - -#define CCALL_HANDLE_REGARG \ - if (ngpr < maxgpr) { \ - dp = &cc->gpr[ngpr]; \ - if (ngpr + n > maxgpr) { \ - nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ - if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ - ngpr = maxgpr; \ - } else { \ - ngpr += n; \ - } \ - goto done; \ - } - -#else -#error "Missing calling convention definitions for this architecture" -#endif - -#ifndef CCALL_HANDLE_STRUCTRET2 -#define CCALL_HANDLE_STRUCTRET2 \ - memcpy(dp, sp, ctr->size); /* Copy struct return value from GPRs. */ -#endif - -/* -- x86 OSX ABI struct classification ----------------------------------- */ - -#if LJ_TARGET_X86 && LJ_TARGET_OSX - -/* Check for struct with single FP field. */ -static int ccall_classify_struct(CTState *cts, CType *ct) -{ - CTSize sz = ct->size; - if (!(sz == sizeof(float) || sz == sizeof(double))) return 0; - if ((ct->info & CTF_UNION)) return 0; - while (ct->sib) { - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - CType *sct = ctype_rawchild(cts, ct); - if (ctype_isfp(sct->info)) { - if (sct->size == sz) - return (sz >> 2); /* Return 1 for float or 2 for double. */ - } else if (ctype_isstruct(sct->info)) { - if (sct->size) - return ccall_classify_struct(cts, sct); - } else { - break; - } - } else if (ctype_isbitfield(ct->info)) { - break; - } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - CType *sct = ctype_rawchild(cts, ct); - if (sct->size) - return ccall_classify_struct(cts, sct); - } - } - return 0; -} - -#endif - -/* -- x64 struct classification ------------------------------------------- */ - -#if LJ_TARGET_X64 && !LJ_ABI_WIN - -/* Register classes for x64 struct classification. */ -#define CCALL_RCL_INT 1 -#define CCALL_RCL_SSE 2 -#define CCALL_RCL_MEM 4 -/* NYI: classify vectors. */ - -static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs); - -/* Classify a C type. */ -static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs) -{ - if (ctype_isarray(ct->info)) { - CType *cct = ctype_rawchild(cts, ct); - CTSize eofs, esz = cct->size, asz = ct->size; - for (eofs = 0; eofs < asz; eofs += esz) - ccall_classify_ct(cts, cct, rcl, ofs+eofs); - } else if (ctype_isstruct(ct->info)) { - ccall_classify_struct(cts, ct, rcl, ofs); - } else { - int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT; - lj_assertCTS(ctype_hassize(ct->info), - "classify ctype %08x without size", ct->info); - if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM; /* Unaligned. */ - rcl[(ofs >= 8)] |= cl; - } -} - -/* Recursively classify a struct based on its fields. */ -static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs) -{ - if (ct->size > 16) return CCALL_RCL_MEM; /* Too big, gets memory class. */ - while (ct->sib) { - CTSize fofs; - ct = ctype_get(cts, ct->sib); - fofs = ofs+ct->size; - if (ctype_isfield(ct->info)) - ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs); - else if (ctype_isbitfield(ct->info)) - rcl[(fofs >= 8)] |= CCALL_RCL_INT; /* NYI: unaligned bitfields? */ - else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) - ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs); - } - return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM); /* Memory class? */ -} - -/* Try to split up a small struct into registers. */ -static int ccall_struct_reg(CCallState *cc, CTState *cts, GPRArg *dp, int *rcl) -{ - MSize ngpr = cc->ngpr, nfpr = cc->nfpr; - uint32_t i; - UNUSED(cts); - for (i = 0; i < 2; i++) { - lj_assertCTS(!(rcl[i] & CCALL_RCL_MEM), "pass mem struct in reg"); - if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */ - if (ngpr >= CCALL_NARG_GPR) return 1; /* Register overflow. */ - cc->gpr[ngpr++] = dp[i]; - } else if ((rcl[i] & CCALL_RCL_SSE)) { - if (nfpr >= CCALL_NARG_FPR) return 1; /* Register overflow. */ - cc->fpr[nfpr++].l[0] = dp[i]; - } - } - cc->ngpr = ngpr; cc->nfpr = nfpr; - return 0; /* Ok. */ -} - -/* Pass a small struct argument. */ -static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl, - TValue *o, int narg) -{ - GPRArg dp[2]; - dp[0] = dp[1] = 0; - /* Convert to temp. struct. */ - lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg)); - if (ccall_struct_reg(cc, cts, dp, rcl)) { - /* Register overflow? Pass on stack. */ - MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1; - if (nsp + n > CCALL_MAXSTACK) return 1; /* Too many arguments. */ - cc->nsp = nsp + n; - memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR); - } - return 0; /* Ok. */ -} - -/* Combine returned small struct. */ -static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz) -{ - GPRArg sp[2]; - MSize ngpr = 0, nfpr = 0; - uint32_t i; - for (i = 0; i < 2; i++) { - if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */ - sp[i] = cc->gpr[ngpr++]; - } else if ((rcl[i] & CCALL_RCL_SSE)) { - sp[i] = cc->fpr[nfpr++].l[0]; - } - } - memcpy(dp, sp, sz); -} -#endif - -/* -- ARM hard-float ABI struct classification ---------------------------- */ - -#if LJ_TARGET_ARM && !LJ_ABI_SOFTFP - -/* Classify a struct based on its fields. */ -static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf) -{ - CTSize sz = ct->size; - unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION); - if ((ctf->info & CTF_VARARG)) goto noth; - while (ct->sib) { - CType *sct; - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - sct = ctype_rawchild(cts, ct); - if (ctype_isfp(sct->info)) { - r |= sct->size; - if (!isu) n++; else if (n == 0) n = 1; - } else if (ctype_iscomplex(sct->info)) { - r |= (sct->size >> 1); - if (!isu) n += 2; else if (n < 2) n = 2; - } else if (ctype_isstruct(sct->info)) { - goto substruct; - } else { - goto noth; - } - } else if (ctype_isbitfield(ct->info)) { - goto noth; - } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - sct = ctype_rawchild(cts, ct); - substruct: - if (sct->size > 0) { - unsigned int s = ccall_classify_struct(cts, sct, ctf); - if (s <= 1) goto noth; - r |= (s & 255); - if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8); - } - } - } - if ((r == 4 || r == 8) && n <= 4) - return r + (n << 8); -noth: /* Not a homogeneous float/double aggregate. */ - return (sz <= 4); /* Return structs of size <= 4 in a GPR. */ -} - -#endif - -/* -- ARM64 ABI struct classification ------------------------------------- */ - -#if LJ_TARGET_ARM64 - -/* Classify a struct based on its fields. */ -static unsigned int ccall_classify_struct(CTState *cts, CType *ct) -{ - CTSize sz = ct->size; - unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION); - while (ct->sib) { - CType *sct; - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - sct = ctype_rawchild(cts, ct); - if (ctype_isfp(sct->info)) { - r |= sct->size; - if (!isu) n++; else if (n == 0) n = 1; - } else if (ctype_iscomplex(sct->info)) { - r |= (sct->size >> 1); - if (!isu) n += 2; else if (n < 2) n = 2; - } else if (ctype_isstruct(sct->info)) { - goto substruct; - } else { - goto noth; - } - } else if (ctype_isbitfield(ct->info)) { - goto noth; - } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - sct = ctype_rawchild(cts, ct); - substruct: - if (sct->size > 0) { - unsigned int s = ccall_classify_struct(cts, sct); - if (s <= 1) goto noth; - r |= (s & 255); - if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8); - } - } - } - if ((r == 4 || r == 8) && n <= 4) - return r + (n << 8); -noth: /* Not a homogeneous float/double aggregate. */ - return (sz <= 16); /* Return structs of size <= 16 in GPRs. */ -} - -#endif - -/* -- MIPS64 ABI struct classification ---------------------------- */ - -#if LJ_TARGET_MIPS64 - -#define FTYPE_FLOAT 1 -#define FTYPE_DOUBLE 2 - -/* Classify FP fields (max. 2) and their types. */ -static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf) -{ - int n = 0, ft = 0; - if ((ctf->info & CTF_VARARG) || (ct->info & CTF_UNION)) - goto noth; - while (ct->sib) { - CType *sct; - ct = ctype_get(cts, ct->sib); - if (n == 2) { - goto noth; - } else if (ctype_isfield(ct->info)) { - sct = ctype_rawchild(cts, ct); - if (ctype_isfp(sct->info)) { - ft |= (sct->size == 4 ? FTYPE_FLOAT : FTYPE_DOUBLE) << 2*n; - n++; - } else { - goto noth; - } - } else if (ctype_isbitfield(ct->info) || - ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - goto noth; - } - } - if (n <= 2) - return ft; -noth: /* Not a homogeneous float/double aggregate. */ - return 0; /* Struct is in GPRs. */ -} - -static void ccall_copy_struct(CCallState *cc, CType *ctr, void *dp, void *sp, - int ft) -{ - if (LJ_ABI_SOFTFP ? ft : - ((ft & 3) == FTYPE_FLOAT || (ft >> 2) == FTYPE_FLOAT)) { - int i, ofs = 0; - for (i = 0; ft != 0; i++, ft >>= 2) { - if ((ft & 3) == FTYPE_FLOAT) { -#if LJ_ABI_SOFTFP - /* The 2nd FP struct result is in CARG1 (gpr[2]) and not CRET2. */ - memcpy((uint8_t *)dp + ofs, - (uint8_t *)&cc->gpr[2*i] + LJ_ENDIAN_SELECT(0, 4), 4); -#else - *(float *)((uint8_t *)dp + ofs) = cc->fpr[i].f; -#endif - ofs += 4; - } else { - ofs = (ofs + 7) & ~7; /* 64 bit alignment. */ -#if LJ_ABI_SOFTFP - *(intptr_t *)((uint8_t *)dp + ofs) = cc->gpr[2*i]; -#else - *(double *)((uint8_t *)dp + ofs) = cc->fpr[i].d; -#endif - ofs += 8; - } - } - } else { -#if !LJ_ABI_SOFTFP - if (ft) sp = (uint8_t *)&cc->fpr[0]; -#endif - memcpy(dp, sp, ctr->size); - } -} - -#endif - -/* -- Common C call handling ---------------------------------------------- */ - -/* Infer the destination CTypeID for a vararg argument. */ -CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o) -{ - if (tvisnumber(o)) { - return CTID_DOUBLE; - } else if (tviscdata(o)) { - CTypeID id = cdataV(o)->ctypeid; - CType *s = ctype_get(cts, id); - if (ctype_isrefarray(s->info)) { - return lj_ctype_intern(cts, - CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR); - } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) { - /* NYI: how to pass a struct by value in a vararg argument? */ - return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR); - } else if (ctype_isfp(s->info) && s->size == sizeof(float)) { - return CTID_DOUBLE; - } else { - return id; - } - } else if (tvisstr(o)) { - return CTID_P_CCHAR; - } else if (tvisbool(o)) { - return CTID_BOOL; - } else { - return CTID_P_VOID; - } -} - -/* Setup arguments for C call. */ -static int ccall_set_args(lua_State *L, CTState *cts, CType *ct, - CCallState *cc) -{ - int gcsteps = 0; - TValue *o, *top = L->top; - CTypeID fid; - CType *ctr; - MSize maxgpr, ngpr = 0, nsp = 0, narg; -#if CCALL_NARG_FPR - MSize nfpr = 0; -#if LJ_TARGET_ARM - MSize fprodd = 0; -#endif -#endif - - /* Clear unused regs to get some determinism in case of misdeclaration. */ - memset(cc->gpr, 0, sizeof(cc->gpr)); -#if CCALL_NUM_FPR - memset(cc->fpr, 0, sizeof(cc->fpr)); -#endif - -#if LJ_TARGET_X86 - /* x86 has several different calling conventions. */ - cc->resx87 = 0; - switch (ctype_cconv(ct->info)) { - case CTCC_FASTCALL: maxgpr = 2; break; - case CTCC_THISCALL: maxgpr = 1; break; - default: maxgpr = 0; break; - } -#else - maxgpr = CCALL_NARG_GPR; -#endif - - /* Perform required setup for some result types. */ - ctr = ctype_rawchild(cts, ct); - if (ctype_isvector(ctr->info)) { - if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16))) - goto err_nyi; - } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) { - /* Preallocate cdata object and anchor it after arguments. */ - CTSize sz = ctr->size; - GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz); - void *dp = cdataptr(cd); - setcdataV(L, L->top++, cd); - if (ctype_isstruct(ctr->info)) { - CCALL_HANDLE_STRUCTRET - } else { - CCALL_HANDLE_COMPLEXRET - } -#if LJ_TARGET_X86 - } else if (ctype_isfp(ctr->info)) { - cc->resx87 = ctr->size == sizeof(float) ? 1 : 2; -#endif - } - - /* Skip initial attributes. */ - fid = ct->sib; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) break; - fid = ctf->sib; - } - - /* Walk through all passed arguments. */ - for (o = L->base+1, narg = 1; o < top; o++, narg++) { - CTypeID did; - CType *d; - CTSize sz; - MSize n, isfp = 0, isva = 0; - void *dp, *rp = NULL; - - if (fid) { /* Get argument type from field. */ - CType *ctf = ctype_get(cts, fid); - fid = ctf->sib; - lj_assertL(ctype_isfield(ctf->info), "field expected"); - did = ctype_cid(ctf->info); - } else { - if (!(ct->info & CTF_VARARG)) - lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */ - did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */ - isva = 1; - } - d = ctype_raw(cts, did); - sz = d->size; - - /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */ - if (ctype_isnum(d->info)) { - if (sz > 8) goto err_nyi; - if ((d->info & CTF_FP)) - isfp = 1; - } else if (ctype_isvector(d->info)) { - if (CCALL_VECTOR_REG && (sz == 8 || sz == 16)) - isfp = 1; - else - goto err_nyi; - } else if (ctype_isstruct(d->info)) { - CCALL_HANDLE_STRUCTARG - } else if (ctype_iscomplex(d->info)) { - CCALL_HANDLE_COMPLEXARG - } else { - sz = CTSIZE_PTR; - } - sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); - n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */ - - CCALL_HANDLE_REGARG /* Handle register arguments. */ - - /* Otherwise pass argument on stack. */ - if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) { - MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1; - nsp = (nsp + align) & ~align; /* Align argument on stack. */ - } - if (nsp + n > CCALL_MAXSTACK) { /* Too many arguments. */ - err_nyi: - lj_err_caller(L, LJ_ERR_FFI_NYICALL); - } - dp = &cc->stack[nsp]; - nsp += n; - isva = 0; - - done: - if (rp) { /* Pass by reference. */ - gcsteps++; - *(void **)dp = rp; - dp = rp; - } - lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg)); - /* Extend passed integers to 32 bits at least. */ - if (ctype_isinteger_or_bool(d->info) && d->size < 4) { - if (d->info & CTF_UNSIGNED) - *(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp : - (uint32_t)*(uint16_t *)dp; - else - *(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp : - (int32_t)*(int16_t *)dp; - } -#if LJ_TARGET_ARM64 && LJ_BE - if (isfp && d->size == sizeof(float)) - ((float *)dp)[1] = ((float *)dp)[0]; /* Floats occupy high slot. */ -#endif -#if LJ_TARGET_MIPS64 || (LJ_TARGET_ARM64 && LJ_BE) - if ((ctype_isinteger_or_bool(d->info) || ctype_isenum(d->info) -#if LJ_TARGET_MIPS64 - || (isfp && nsp == 0) -#endif - ) && d->size <= 4) { - *(int64_t *)dp = (int64_t)*(int32_t *)dp; /* Sign-extend to 64 bit. */ - } -#endif -#if LJ_TARGET_X64 && LJ_ABI_WIN - if (isva) { /* Windows/x64 mirrors varargs in both register sets. */ - if (nfpr == ngpr) - cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0]; - else - cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1]; - } -#else - UNUSED(isva); -#endif -#if LJ_TARGET_X64 && !LJ_ABI_WIN - if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) { - cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1]; /* Split complex double. */ - cc->fpr[nfpr-2].d[1] = 0; - } -#elif LJ_TARGET_ARM64 || (LJ_TARGET_MIPS64 && !LJ_ABI_SOFTFP) - if (isfp == 2 && (uint8_t *)dp < (uint8_t *)cc->stack) { - /* Split float HFA or complex float into separate registers. */ - CTSize i = (sz >> 2) - 1; - do { ((uint64_t *)dp)[i] = ((uint32_t *)dp)[i]; } while (i--); - } -#else - UNUSED(isfp); -#endif - } - if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too few arguments. */ - -#if LJ_TARGET_X64 || (LJ_TARGET_PPC && !LJ_ABI_SOFTFP) - cc->nfpr = nfpr; /* Required for vararg functions. */ -#endif - cc->nsp = nsp; - cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR; - if (nsp > CCALL_SPS_FREE) - cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u); - return gcsteps; -} - -/* Get results from C call. */ -static int ccall_get_results(lua_State *L, CTState *cts, CType *ct, - CCallState *cc, int *ret) -{ - CType *ctr = ctype_rawchild(cts, ct); - uint8_t *sp = (uint8_t *)&cc->gpr[0]; - if (ctype_isvoid(ctr->info)) { - *ret = 0; /* Zero results. */ - return 0; /* No additional GC step. */ - } - *ret = 1; /* One result. */ - if (ctype_isstruct(ctr->info)) { - /* Return cdata object which is already on top of stack. */ - if (!cc->retref) { - void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */ - CCALL_HANDLE_STRUCTRET2 - } - return 1; /* One GC step. */ - } - if (ctype_iscomplex(ctr->info)) { - /* Return cdata object which is already on top of stack. */ - void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */ - CCALL_HANDLE_COMPLEXRET2 - return 1; /* One GC step. */ - } - if (LJ_BE && ctr->size < CTSIZE_PTR && - (ctype_isinteger_or_bool(ctr->info) || ctype_isenum(ctr->info))) - sp += (CTSIZE_PTR - ctr->size); -#if CCALL_NUM_FPR - if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info)) - sp = (uint8_t *)&cc->fpr[0]; -#endif -#ifdef CCALL_HANDLE_RET - CCALL_HANDLE_RET -#endif - /* No reference types end up here, so there's no need for the CTypeID. */ - lj_assertL(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info)), - "unexpected reference ctype"); - return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp); -} - -/* Call C function. */ -int lj_ccall_func(lua_State *L, GCcdata *cd) -{ - CTState *cts = ctype_cts(L); - CType *ct = ctype_raw(cts, cd->ctypeid); - CTSize sz = CTSIZE_PTR; - if (ctype_isptr(ct->info)) { - sz = ct->size; - ct = ctype_rawchild(cts, ct); - } - if (ctype_isfunc(ct->info)) { - CCallState cc; - int gcsteps, ret; - cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz); - gcsteps = ccall_set_args(L, cts, ct, &cc); - ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab); - cts->cb.slot = ~0u; - lj_vm_ffi_call(&cc); - if (cts->cb.slot != ~0u) { /* Blacklist function that called a callback. */ - TValue tv; - tv.u64 = ((uintptr_t)(void *)cc.func >> 2) | U64x(800000000, 00000000); - setboolV(lj_tab_set(L, cts->miscmap, &tv), 1); - } - ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab); /* May be reallocated. */ - gcsteps += ccall_get_results(L, cts, ct, &cc, &ret); -#if LJ_TARGET_X86 && LJ_ABI_WIN - /* Automatically detect __stdcall and fix up C function declaration. */ - if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) { - CTF_INSERT(ct->info, CCONV, CTCC_STDCALL); - lj_trace_abort(G(L)); - } -#endif - while (gcsteps-- > 0) - lj_gc_check(L); - return ret; - } - return -1; /* Not a function. */ -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.h deleted file mode 100644 index 0b3c524..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccall.h +++ /dev/null @@ -1,194 +0,0 @@ -/* -** FFI C call handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CCALL_H -#define _LJ_CCALL_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* -- C calling conventions ----------------------------------------------- */ - -#if LJ_TARGET_X86ORX64 - -#if LJ_TARGET_X86 -#define CCALL_NARG_GPR 2 /* For fastcall arguments. */ -#define CCALL_NARG_FPR 0 -#define CCALL_NRET_GPR 2 -#define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */ -#define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */ -#elif LJ_ABI_WIN -#define CCALL_NARG_GPR 4 -#define CCALL_NARG_FPR 4 -#define CCALL_NRET_GPR 1 -#define CCALL_NRET_FPR 1 -#define CCALL_SPS_EXTRA 4 -#else -#define CCALL_NARG_GPR 6 -#define CCALL_NARG_FPR 8 -#define CCALL_NRET_GPR 2 -#define CCALL_NRET_FPR 2 -#define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */ -#endif - -#define CCALL_SPS_FREE 1 -#define CCALL_ALIGN_CALLSTATE 16 - -typedef LJ_ALIGN(16) union FPRArg { - double d[2]; - float f[4]; - uint8_t b[16]; - uint16_t s[8]; - int i[4]; - int64_t l[2]; -} FPRArg; - -typedef intptr_t GPRArg; - -#elif LJ_TARGET_ARM - -#define CCALL_NARG_GPR 4 -#define CCALL_NRET_GPR 2 /* For softfp double. */ -#if LJ_ABI_SOFTFP -#define CCALL_NARG_FPR 0 -#define CCALL_NRET_FPR 0 -#else -#define CCALL_NARG_FPR 8 -#define CCALL_NRET_FPR 4 -#endif -#define CCALL_SPS_FREE 0 - -typedef intptr_t GPRArg; -typedef union FPRArg { - double d; - float f[2]; -} FPRArg; - -#elif LJ_TARGET_ARM64 - -#define CCALL_NARG_GPR 8 -#define CCALL_NRET_GPR 2 -#define CCALL_NARG_FPR 8 -#define CCALL_NRET_FPR 4 -#define CCALL_SPS_FREE 0 - -typedef intptr_t GPRArg; -typedef union FPRArg { - double d; - struct { LJ_ENDIAN_LOHI(float f; , float g;) }; - struct { LJ_ENDIAN_LOHI(uint32_t lo; , uint32_t hi;) }; -} FPRArg; - -#elif LJ_TARGET_PPC - -#define CCALL_NARG_GPR 8 -#define CCALL_NARG_FPR (LJ_ABI_SOFTFP ? 0 : 8) -#define CCALL_NRET_GPR 4 /* For complex double. */ -#define CCALL_NRET_FPR (LJ_ABI_SOFTFP ? 0 : 1) -#define CCALL_SPS_EXTRA 4 -#define CCALL_SPS_FREE 0 - -typedef intptr_t GPRArg; -typedef double FPRArg; - -#elif LJ_TARGET_MIPS32 - -#define CCALL_NARG_GPR 4 -#define CCALL_NARG_FPR (LJ_ABI_SOFTFP ? 0 : 2) -#define CCALL_NRET_GPR (LJ_ABI_SOFTFP ? 4 : 2) -#define CCALL_NRET_FPR (LJ_ABI_SOFTFP ? 0 : 2) -#define CCALL_SPS_EXTRA 7 -#define CCALL_SPS_FREE 1 - -typedef intptr_t GPRArg; -typedef union FPRArg { - double d; - struct { LJ_ENDIAN_LOHI(float f; , float g;) }; -} FPRArg; - -#elif LJ_TARGET_MIPS64 - -/* FP args are positional and overlay the GPR array. */ -#define CCALL_NARG_GPR 8 -#define CCALL_NARG_FPR 0 -#define CCALL_NRET_GPR 2 -#define CCALL_NRET_FPR (LJ_ABI_SOFTFP ? 0 : 2) -#define CCALL_SPS_EXTRA 3 -#define CCALL_SPS_FREE 1 - -typedef intptr_t GPRArg; -typedef union FPRArg { - double d; - struct { LJ_ENDIAN_LOHI(float f; , float g;) }; -} FPRArg; - -#else -#error "Missing calling convention definitions for this architecture" -#endif - -#ifndef CCALL_SPS_EXTRA -#define CCALL_SPS_EXTRA 0 -#endif -#ifndef CCALL_VECTOR_REG -#define CCALL_VECTOR_REG 0 -#endif -#ifndef CCALL_ALIGN_STACKARG -#define CCALL_ALIGN_STACKARG 1 -#endif -#ifndef CCALL_ALIGN_CALLSTATE -#define CCALL_ALIGN_CALLSTATE 8 -#endif - -#define CCALL_NUM_GPR \ - (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR) -#define CCALL_NUM_FPR \ - (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR) - -/* Check against constants in lj_ctype.h. */ -LJ_STATIC_ASSERT(CCALL_NUM_GPR <= CCALL_MAX_GPR); -LJ_STATIC_ASSERT(CCALL_NUM_FPR <= CCALL_MAX_FPR); - -#define CCALL_MAXSTACK 32 - -/* -- C call state -------------------------------------------------------- */ - -typedef LJ_ALIGN(CCALL_ALIGN_CALLSTATE) struct CCallState { - void (*func)(void); /* Pointer to called function. */ - uint32_t spadj; /* Stack pointer adjustment. */ - uint8_t nsp; /* Number of stack slots. */ - uint8_t retref; /* Return value by reference. */ -#if LJ_TARGET_X64 - uint8_t ngpr; /* Number of arguments in GPRs. */ - uint8_t nfpr; /* Number of arguments in FPRs. */ -#elif LJ_TARGET_X86 - uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */ -#elif LJ_TARGET_ARM64 - void *retp; /* Aggregate return pointer in x8. */ -#elif LJ_TARGET_PPC - uint8_t nfpr; /* Number of arguments in FPRs. */ -#endif -#if LJ_32 - int32_t align1; -#endif -#if CCALL_NUM_FPR - FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */ -#endif - GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */ - GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */ -} CCallState; - -/* -- C call handling ----------------------------------------------------- */ - -/* Really belongs to lj_vm.h. */ -LJ_ASMF void LJ_FASTCALL lj_vm_ffi_call(CCallState *cc); - -LJ_FUNC CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o); -LJ_FUNC int lj_ccall_func(lua_State *L, GCcdata *cd); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.c deleted file mode 100644 index 43e4430..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.c +++ /dev/null @@ -1,796 +0,0 @@ -/* -** FFI C callback handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_ccall.h" -#include "lj_ccallback.h" -#include "lj_target.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_vm.h" - -/* -- Target-specific handling of callback slots -------------------------- */ - -#define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE) - -#if LJ_OS_NOJIT - -/* Callbacks disabled. */ -#define CALLBACK_SLOT2OFS(slot) (0*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (0*(ofs)) -#define CALLBACK_MAX_SLOT 0 - -#elif LJ_TARGET_X86ORX64 - -#define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0) -#define CALLBACK_MCODE_GROUP (-2+1+2+(LJ_GC64 ? 10 : 5)+(LJ_64 ? 6 : 5)) - -#define CALLBACK_SLOT2OFS(slot) \ - (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot)) - -static MSize CALLBACK_OFS2SLOT(MSize ofs) -{ - MSize group; - ofs -= CALLBACK_MCODE_HEAD; - group = ofs / (32*4 + CALLBACK_MCODE_GROUP); - return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32; -} - -#define CALLBACK_MAX_SLOT \ - (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32) - -#elif LJ_TARGET_ARM - -#define CALLBACK_MCODE_HEAD 32 - -#elif LJ_TARGET_ARM64 - -#define CALLBACK_MCODE_HEAD 32 - -#elif LJ_TARGET_PPC - -#define CALLBACK_MCODE_HEAD 24 - -#elif LJ_TARGET_MIPS32 - -#define CALLBACK_MCODE_HEAD 20 - -#elif LJ_TARGET_MIPS64 - -#define CALLBACK_MCODE_HEAD 52 - -#else - -/* Missing support for this architecture. */ -#define CALLBACK_SLOT2OFS(slot) (0*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (0*(ofs)) -#define CALLBACK_MAX_SLOT 0 - -#endif - -#ifndef CALLBACK_SLOT2OFS -#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) -#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) -#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) -#endif - -/* Convert callback slot number to callback function pointer. */ -static void *callback_slot2ptr(CTState *cts, MSize slot) -{ - return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot); -} - -/* Convert callback function pointer to slot number. */ -MSize lj_ccallback_ptr2slot(CTState *cts, void *p) -{ - uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode); - if (ofs < CALLBACK_MCODE_SIZE) { - MSize slot = CALLBACK_OFS2SLOT((MSize)ofs); - if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs) - return slot; - } - return ~0u; /* Not a known callback function pointer. */ -} - -/* Initialize machine code for callback function pointers. */ -#if LJ_OS_NOJIT -/* Disabled callback support. */ -#define callback_mcode_init(g, p) (p) -#elif LJ_TARGET_X86ORX64 -static void *callback_mcode_init(global_State *g, uint8_t *page) -{ - uint8_t *p = page; - uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback; - MSize slot; -#if LJ_64 - *(void **)p = target; p += 8; -#endif - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - /* mov al, slot; jmp group */ - *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot; - if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) { - /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */ - *p++ = XI_PUSH + RID_EBP; - *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8); -#if LJ_GC64 - *p++ = 0x48; *p++ = XI_MOVri | RID_EBP; - *(uint64_t *)p = (uint64_t)(g); p += 8; -#else - *p++ = XI_MOVri | RID_EBP; - *(int32_t *)p = i32ptr(g); p += 4; -#endif -#if LJ_64 - /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */ - *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP; - *(int32_t *)p = (int32_t)(page-(p+4)); p += 4; -#else - /* jmp lj_vm_ffi_callback. */ - *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4; -#endif - } else { - *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2); - } - } - return p; -} -#elif LJ_TARGET_ARM -static void *callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - void *target = (void *)lj_vm_ffi_callback; - MSize slot; - /* This must match with the saveregs macro in buildvm_arm.dasc. */ - *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC); - *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR); - *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD; - *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9); - *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC); - *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC); - *p++ = u32ptr(g); - *p++ = u32ptr(target); - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC); - *p = ARMI_B | ((page-p-2) & 0x00ffffffu); - p++; - } - return p; -} -#elif LJ_TARGET_ARM64 -static void *callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - void *target = (void *)lj_vm_ffi_callback; - MSize slot; - *p++ = A64I_LE(A64I_LDRLx | A64F_D(RID_X11) | A64F_S19(4)); - *p++ = A64I_LE(A64I_LDRLx | A64F_D(RID_X10) | A64F_S19(5)); - *p++ = A64I_LE(A64I_BR | A64F_N(RID_X11)); - *p++ = A64I_LE(A64I_NOP); - ((void **)p)[0] = target; - ((void **)p)[1] = g; - p += 4; - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p++ = A64I_LE(A64I_MOVZw | A64F_D(RID_X9) | A64F_U16(slot)); - *p = A64I_LE(A64I_B | A64F_S26((page-p) & 0x03ffffffu)); - p++; - } - return p; -} -#elif LJ_TARGET_PPC -static void *callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - void *target = (void *)lj_vm_ffi_callback; - MSize slot; - *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16); - *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16); - *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff); - *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff); - *p++ = PPCI_MTCTR | PPCF_T(RID_TMP); - *p++ = PPCI_BCTR; - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p++ = PPCI_LI | PPCF_T(RID_R11) | slot; - *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2); - p++; - } - return p; -} -#elif LJ_TARGET_MIPS -static void *callback_mcode_init(global_State *g, uint32_t *page) -{ - uint32_t *p = page; - uintptr_t target = (uintptr_t)(void *)lj_vm_ffi_callback; - uintptr_t ug = (uintptr_t)(void *)g; - MSize slot; -#if LJ_TARGET_MIPS32 - *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (target >> 16); - *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (ug >> 16); -#else - *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (target >> 48); - *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (ug >> 48); - *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) | ((target >> 32) & 0xffff); - *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | ((ug >> 32) & 0xffff); - *p++ = MIPSI_DSLL | MIPSF_D(RID_R3)|MIPSF_T(RID_R3) | MIPSF_A(16); - *p++ = MIPSI_DSLL | MIPSF_D(RID_R2)|MIPSF_T(RID_R2) | MIPSF_A(16); - *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) | ((target >> 16) & 0xffff); - *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | ((ug >> 16) & 0xffff); - *p++ = MIPSI_DSLL | MIPSF_D(RID_R3)|MIPSF_T(RID_R3) | MIPSF_A(16); - *p++ = MIPSI_DSLL | MIPSF_D(RID_R2)|MIPSF_T(RID_R2) | MIPSF_A(16); -#endif - *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) | (target & 0xffff); - *p++ = MIPSI_JR | MIPSF_S(RID_R3); - *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (ug & 0xffff); - for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { - *p = MIPSI_B | ((page-p-1) & 0x0000ffffu); - p++; - *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot; - } - return p; -} -#else -/* Missing support for this architecture. */ -#define callback_mcode_init(g, p) (p) -#endif - -/* -- Machine code management --------------------------------------------- */ - -#if LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#elif LJ_TARGET_POSIX - -#include -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif -#ifdef PROT_MPROTECT -#define CCPROT_CREATE (PROT_MPROTECT(PROT_EXEC)) -#else -#define CCPROT_CREATE 0 -#endif - -#endif - -/* Allocate and initialize area for callback function pointers. */ -static void callback_mcode_new(CTState *cts) -{ - size_t sz = (size_t)CALLBACK_MCODE_SIZE; - void *p, *pe; - if (CALLBACK_MAX_SLOT == 0) - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); -#if LJ_TARGET_WINDOWS - p = LJ_WIN_VALLOC(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - if (!p) - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); -#elif LJ_TARGET_POSIX - p = mmap(NULL, sz, (PROT_READ|PROT_WRITE|CCPROT_CREATE), MAP_PRIVATE|MAP_ANONYMOUS, - -1, 0); - if (p == MAP_FAILED) - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); -#else - /* Fallback allocator. Fails if memory is not executable by default. */ - p = lj_mem_new(cts->L, sz); -#endif - cts->cb.mcode = p; - pe = callback_mcode_init(cts->g, p); - UNUSED(pe); - lj_assertCTS((size_t)((char *)pe - (char *)p) <= sz, - "miscalculated CALLBACK_MAX_SLOT"); - lj_mcode_sync(p, (char *)p + sz); -#if LJ_TARGET_WINDOWS - { - DWORD oprot; - LJ_WIN_VPROTECT(p, sz, PAGE_EXECUTE_READ, &oprot); - } -#elif LJ_TARGET_POSIX - mprotect(p, sz, (PROT_READ|PROT_EXEC)); -#endif -} - -/* Free area for callback function pointers. */ -void lj_ccallback_mcode_free(CTState *cts) -{ - size_t sz = (size_t)CALLBACK_MCODE_SIZE; - void *p = cts->cb.mcode; - if (p == NULL) return; -#if LJ_TARGET_WINDOWS - VirtualFree(p, 0, MEM_RELEASE); - UNUSED(sz); -#elif LJ_TARGET_POSIX - munmap(p, sz); -#else - lj_mem_free(cts->g, p, sz); -#endif -} - -/* -- C callback entry ---------------------------------------------------- */ - -/* Target-specific handling of register arguments. Similar to lj_ccall.c. */ -#if LJ_TARGET_X86 - -#define CALLBACK_HANDLE_REGARG \ - if (!isfp) { /* Only non-FP values may be passed in registers. */ \ - if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \ - if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \ - } else if (ngpr + 1 <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_X64 && LJ_ABI_WIN - -/* Windows/x64 argument registers are strictly positional (use ngpr). */ -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (ngpr < maxgpr) { sp = &cts->cb.fpr[ngpr++]; UNUSED(nfpr); goto done; } \ - } else { \ - if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \ - } - -#elif LJ_TARGET_X64 - -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (nfpr + n <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr]; \ - nfpr += n; \ - goto done; \ - } \ - } else { \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } \ - } - -#elif LJ_TARGET_ARM - -#if LJ_ABI_SOFTFP - -#define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp); -#define CALLBACK_HANDLE_REGARG_FP2 - -#else - -#define CALLBACK_HANDLE_REGARG_FP1 \ - if (isfp) { \ - if (n == 1) { \ - if (fprodd) { \ - sp = &cts->cb.fpr[fprodd-1]; \ - fprodd = 0; \ - goto done; \ - } else if (nfpr + 1 <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr++]; \ - fprodd = nfpr; \ - goto done; \ - } \ - } else { \ - if (nfpr + 1 <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr++]; \ - goto done; \ - } \ - } \ - fprodd = 0; /* No reordering after the first FP value is on stack. */ \ - } else { - -#define CALLBACK_HANDLE_REGARG_FP2 } - -#endif - -#define CALLBACK_HANDLE_REGARG \ - CALLBACK_HANDLE_REGARG_FP1 \ - if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } CALLBACK_HANDLE_REGARG_FP2 - -#elif LJ_TARGET_ARM64 - -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (nfpr + n <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr]; \ - nfpr += n; \ - goto done; \ - } else { \ - nfpr = CCALL_NARG_FPR; /* Prevent reordering. */ \ - } \ - } else { \ - if (!LJ_TARGET_OSX && n > 1) \ - ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } else { \ - ngpr = CCALL_NARG_GPR; /* Prevent reordering. */ \ - } \ - } - -#elif LJ_TARGET_PPC - -#define CALLBACK_HANDLE_GPR \ - if (n > 1) { \ - lj_assertCTS(((LJ_ABI_SOFTFP && ctype_isnum(cta->info)) || /* double. */ \ - ctype_isinteger(cta->info)) && n == 2, /* int64_t. */ \ - "bad GPR type"); \ - ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ - } \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } - -#if LJ_ABI_SOFTFP -#define CALLBACK_HANDLE_REGARG \ - CALLBACK_HANDLE_GPR \ - UNUSED(isfp); -#else -#define CALLBACK_HANDLE_REGARG \ - if (isfp) { \ - if (nfpr + 1 <= CCALL_NARG_FPR) { \ - sp = &cts->cb.fpr[nfpr++]; \ - cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \ - goto done; \ - } \ - } else { /* Try to pass argument in GPRs. */ \ - CALLBACK_HANDLE_GPR \ - } -#endif - -#if !LJ_ABI_SOFTFP -#define CALLBACK_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */ -#endif - -#elif LJ_TARGET_MIPS32 - -#define CALLBACK_HANDLE_GPR \ - if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ - if (ngpr + n <= maxgpr) { \ - sp = &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } - -#if !LJ_ABI_SOFTFP /* MIPS32 hard-float */ -#define CALLBACK_HANDLE_REGARG \ - if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \ - sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \ - nfpr++; ngpr += n; \ - goto done; \ - } else { /* Try to pass argument in GPRs. */ \ - nfpr = CCALL_NARG_FPR; \ - CALLBACK_HANDLE_GPR \ - } -#else /* MIPS32 soft-float */ -#define CALLBACK_HANDLE_REGARG \ - CALLBACK_HANDLE_GPR \ - UNUSED(isfp); -#endif - -#define CALLBACK_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - ((float *)dp)[1] = *(float *)dp; - -#elif LJ_TARGET_MIPS64 - -#if !LJ_ABI_SOFTFP /* MIPS64 hard-float */ -#define CALLBACK_HANDLE_REGARG \ - if (ngpr + n <= maxgpr) { \ - sp = isfp ? (void*) &cts->cb.fpr[ngpr] : (void*) &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } -#else /* MIPS64 soft-float */ -#define CALLBACK_HANDLE_REGARG \ - if (ngpr + n <= maxgpr) { \ - UNUSED(isfp); \ - sp = (void*) &cts->cb.gpr[ngpr]; \ - ngpr += n; \ - goto done; \ - } -#endif - -#define CALLBACK_HANDLE_RET \ - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ - ((float *)dp)[1] = *(float *)dp; - -#else -#error "Missing calling convention definitions for this architecture" -#endif - -/* Convert and push callback arguments to Lua stack. */ -static void callback_conv_args(CTState *cts, lua_State *L) -{ - TValue *o = L->top; - intptr_t *stack = cts->cb.stack; - MSize slot = cts->cb.slot; - CTypeID id = 0, rid, fid; - int gcsteps = 0; - CType *ct; - GCfunc *fn; - int fntp; - MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR; -#if CCALL_NARG_FPR - MSize nfpr = 0; -#if LJ_TARGET_ARM - MSize fprodd = 0; -#endif -#endif - - if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) { - ct = ctype_get(cts, id); - rid = ctype_cid(ct->info); /* Return type. x86: +(spadj<<16). */ - fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot)); - fntp = LJ_TFUNC; - } else { /* Must set up frame first, before throwing the error. */ - ct = NULL; - rid = 0; - fn = (GCfunc *)L; - fntp = LJ_TTHREAD; - } - /* Continuation returns from callback. */ - if (LJ_FR2) { - (o++)->u64 = LJ_CONT_FFI_CALLBACK; - (o++)->u64 = rid; - } else { - o->u32.lo = LJ_CONT_FFI_CALLBACK; - o->u32.hi = rid; - o++; - } - setframe_gc(o, obj2gco(fn), fntp); - if (LJ_FR2) o++; - setframe_ftsz(o, ((char *)(o+1) - (char *)L->base) + FRAME_CONT); - L->top = L->base = ++o; - if (!ct) - lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK); - if (isluafunc(fn)) - setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1); - lj_state_checkstack(L, LUA_MINSTACK); /* May throw. */ - o = L->base; /* Might have been reallocated. */ - -#if LJ_TARGET_X86 - /* x86 has several different calling conventions. */ - switch (ctype_cconv(ct->info)) { - case CTCC_FASTCALL: maxgpr = 2; break; - case CTCC_THISCALL: maxgpr = 1; break; - default: maxgpr = 0; break; - } -#endif - - fid = ct->sib; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) { - CType *cta; - void *sp; - CTSize sz; - int isfp; - MSize n; - lj_assertCTS(ctype_isfield(ctf->info), "field expected"); - cta = ctype_rawchild(cts, ctf); - isfp = ctype_isfp(cta->info); - sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); - n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */ - - CALLBACK_HANDLE_REGARG /* Handle register arguments. */ - - /* Otherwise pass argument on stack. */ - if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8) - nsp = (nsp + 1) & ~1u; /* Align 64 bit argument on stack. */ - sp = &stack[nsp]; - nsp += n; - - done: - if (LJ_BE && cta->size < CTSIZE_PTR -#if LJ_TARGET_MIPS64 - && !(isfp && nsp) -#endif - ) - sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size); - gcsteps += lj_cconv_tv_ct(cts, cta, 0, o++, sp); - } - fid = ctf->sib; - } - L->top = o; -#if LJ_TARGET_X86 - /* Store stack adjustment for returns from non-cdecl callbacks. */ - if (ctype_cconv(ct->info) != CTCC_CDECL) { -#if LJ_FR2 - (L->base-3)->u64 |= (nsp << (16+2)); -#else - (L->base-2)->u32.hi |= (nsp << (16+2)); -#endif - } -#endif - while (gcsteps-- > 0) - lj_gc_check(L); -} - -/* Convert Lua object to callback result. */ -static void callback_conv_result(CTState *cts, lua_State *L, TValue *o) -{ -#if LJ_FR2 - CType *ctr = ctype_raw(cts, (uint16_t)(L->base-3)->u64); -#else - CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi); -#endif -#if LJ_TARGET_X86 - cts->cb.gpr[2] = 0; -#endif - if (!ctype_isvoid(ctr->info)) { - uint8_t *dp = (uint8_t *)&cts->cb.gpr[0]; -#if CCALL_NUM_FPR - if (ctype_isfp(ctr->info)) - dp = (uint8_t *)&cts->cb.fpr[0]; -#endif -#if LJ_TARGET_ARM64 && LJ_BE - if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) - dp = (uint8_t *)&cts->cb.fpr[0].f[1]; -#endif - lj_cconv_ct_tv(cts, ctr, dp, o, 0); -#ifdef CALLBACK_HANDLE_RET - CALLBACK_HANDLE_RET -#endif - /* Extend returned integers to (at least) 32 bits. */ - if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) { - if (ctr->info & CTF_UNSIGNED) - *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp : - (uint32_t)*(uint16_t *)dp; - else - *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp : - (int32_t)*(int16_t *)dp; - } -#if LJ_TARGET_MIPS64 || (LJ_TARGET_ARM64 && LJ_BE) - /* Always sign-extend results to 64 bits. Even a soft-fp 'float'. */ - if (ctr->size <= 4 && - (LJ_ABI_SOFTFP || ctype_isinteger_or_bool(ctr->info))) - *(int64_t *)dp = (int64_t)*(int32_t *)dp; -#endif -#if LJ_TARGET_X86 - if (ctype_isfp(ctr->info)) - cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2; -#endif - } -} - -/* Enter callback. */ -lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf) -{ - lua_State *L = cts->L; - global_State *g = cts->g; - lj_assertG(L != NULL, "uninitialized cts->L in callback"); - if (tvref(g->jit_base)) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK)); - if (g->panic) g->panic(L); - exit(EXIT_FAILURE); - } - lj_trace_abort(g); /* Never record across callback. */ - /* Setup C frame. */ - cframe_prev(cf) = L->cframe; - setcframe_L(cf, L); - cframe_errfunc(cf) = -1; - cframe_nres(cf) = 0; - L->cframe = cf; - callback_conv_args(cts, L); - return L; /* Now call the function on this stack. */ -} - -/* Leave callback. */ -void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o) -{ - lua_State *L = cts->L; - GCfunc *fn; - TValue *obase = L->base; - L->base = L->top; /* Keep continuation frame for throwing errors. */ - if (o >= L->base) { - /* PC of RET* is lost. Point to last line for result conv. errors. */ - fn = curr_func(L); - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1); - } - } - callback_conv_result(cts, L, o); - /* Finally drop C frame and continuation frame. */ - L->top -= 2+2*LJ_FR2; - L->base = obase; - L->cframe = cframe_prev(L->cframe); - cts->cb.slot = 0; /* Blacklist C function that called the callback. */ -} - -/* -- C callback management ----------------------------------------------- */ - -/* Get an unused slot in the callback slot table. */ -static MSize callback_slot_new(CTState *cts, CType *ct) -{ - CTypeID id = ctype_typeid(cts, ct); - CTypeID1 *cbid = cts->cb.cbid; - MSize top; - for (top = cts->cb.topid; top < cts->cb.sizeid; top++) - if (LJ_LIKELY(cbid[top] == 0)) - goto found; -#if CALLBACK_MAX_SLOT - if (top >= CALLBACK_MAX_SLOT) -#endif - lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); - if (!cts->cb.mcode) - callback_mcode_new(cts); - lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1); - cts->cb.cbid = cbid; - memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1)); -found: - cbid[top] = id; - cts->cb.topid = top+1; - return top; -} - -/* Check for function pointer and supported argument/result types. */ -static CType *callback_checkfunc(CTState *cts, CType *ct) -{ - int narg = 0; - if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR)) - return NULL; - ct = ctype_rawchild(cts, ct); - if (ctype_isfunc(ct->info)) { - CType *ctr = ctype_rawchild(cts, ct); - CTypeID fid = ct->sib; - if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) || - ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8))) - return NULL; - if ((ct->info & CTF_VARARG)) - return NULL; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) { - CType *cta; - lj_assertCTS(ctype_isfield(ctf->info), "field expected"); - cta = ctype_rawchild(cts, ctf); - if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) || - (ctype_isnum(cta->info) && cta->size <= 8)) || - ++narg >= LUA_MINSTACK-3) - return NULL; - } - fid = ctf->sib; - } - return ct; - } - return NULL; -} - -/* Create a new callback and return the callback function pointer. */ -void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn) -{ - ct = callback_checkfunc(cts, ct); - if (ct) { - MSize slot = callback_slot_new(cts, ct); - GCtab *t = cts->miscmap; - setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn); - lj_gc_anybarriert(cts->L, t); - return callback_slot2ptr(cts, slot); - } - return NULL; /* Bad conversion. */ -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.h deleted file mode 100644 index 8a2c31d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ccallback.h +++ /dev/null @@ -1,25 +0,0 @@ -/* -** FFI C callback handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CCALLBACK_H -#define _LJ_CCALLBACK_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* Really belongs to lj_vm.h. */ -LJ_ASMF void lj_vm_ffi_callback(void); - -LJ_FUNC MSize lj_ccallback_ptr2slot(CTState *cts, void *p); -LJ_FUNCA lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf); -LJ_FUNCA void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o); -LJ_FUNC void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn); -LJ_FUNC void lj_ccallback_mcode_free(CTState *cts); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.c deleted file mode 100644 index 3bbfd3f..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.c +++ /dev/null @@ -1,770 +0,0 @@ -/* -** C type conversions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_cconv.h" -#include "lj_ccallback.h" - -/* -- Conversion errors --------------------------------------------------- */ - -/* Bad conversion. */ -LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s, - CTInfo flags) -{ - const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); - const char *src; - if ((flags & CCF_FROMTV)) - src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER : - ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)]; - else - src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL)); - if (CCF_GETARG(flags)) - lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst); - else - lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst); -} - -/* Bad conversion from TValue. */ -LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o, - CTInfo flags) -{ - const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); - const char *src = lj_typename(o); - if (CCF_GETARG(flags)) - lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst); - else - lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst); -} - -/* Initializer overflow. */ -LJ_NORET static void cconv_err_initov(CTState *cts, CType *d) -{ - const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); - lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst); -} - -/* -- C type compatibility checks ----------------------------------------- */ - -/* Get raw type and qualifiers for a child type. Resolves enums, too. */ -static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual) -{ - ct = ctype_child(cts, ct); - for (;;) { - if (ctype_isattrib(ct->info)) { - if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size; - } else if (!ctype_isenum(ct->info)) { - break; - } - ct = ctype_child(cts, ct); - } - *qual |= (ct->info & CTF_QUAL); - return ct; -} - -/* Check for compatible types when converting to a pointer. -** Note: these checks are more relaxed than what C99 mandates. -*/ -int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags) -{ - if (!((flags & CCF_CAST) || d == s)) { - CTInfo dqual = 0, squal = 0; - d = cconv_childqual(cts, d, &dqual); - if (!ctype_isstruct(s->info)) - s = cconv_childqual(cts, s, &squal); - if ((flags & CCF_SAME)) { - if (dqual != squal) - return 0; /* Different qualifiers. */ - } else if (!(flags & CCF_IGNQUAL)) { - if ((dqual & squal) != squal) - return 0; /* Discarded qualifiers. */ - if (ctype_isvoid(d->info) || ctype_isvoid(s->info)) - return 1; /* Converting to/from void * is always ok. */ - } - if (ctype_type(d->info) != ctype_type(s->info) || - d->size != s->size) - return 0; /* Different type or different size. */ - if (ctype_isnum(d->info)) { - if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP))) - return 0; /* Different numeric types. */ - } else if (ctype_ispointer(d->info)) { - /* Check child types for compatibility. */ - return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME); - } else if (ctype_isstruct(d->info)) { - if (d != s) - return 0; /* Must be exact same type for struct/union. */ - } else if (ctype_isfunc(d->info)) { - /* NYI: structural equality of functions. */ - } - } - return 1; /* Types are compatible. */ -} - -/* -- C type to C type conversion ----------------------------------------- */ - -/* Convert C type to C type. Caveat: expects to get the raw CType! -** -** Note: This is only used by the interpreter and not optimized at all. -** The JIT compiler will do a much better job specializing for each case. -*/ -void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, - uint8_t *dp, uint8_t *sp, CTInfo flags) -{ - CTSize dsize = d->size, ssize = s->size; - CTInfo dinfo = d->info, sinfo = s->info; - void *tmpptr; - - lj_assertCTS(!ctype_isenum(dinfo) && !ctype_isenum(sinfo), - "unresolved enum"); - lj_assertCTS(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo), - "unstripped attribute"); - - if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT) - goto err_conv; - - /* Some basic sanity checks. */ - lj_assertCTS(!ctype_isnum(dinfo) || dsize > 0, "bad size for number type"); - lj_assertCTS(!ctype_isnum(sinfo) || ssize > 0, "bad size for number type"); - lj_assertCTS(!ctype_isbool(dinfo) || dsize == 1 || dsize == 4, - "bad size for bool type"); - lj_assertCTS(!ctype_isbool(sinfo) || ssize == 1 || ssize == 4, - "bad size for bool type"); - lj_assertCTS(!ctype_isinteger(dinfo) || (1u< ssize) { /* Zero-extend or sign-extend LSB. */ -#if LJ_LE - uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0; - memcpy(dp, sp, ssize); - memset(dp + ssize, fill, dsize-ssize); -#else - uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0; - memset(dp, fill, dsize-ssize); - memcpy(dp + (dsize-ssize), sp, ssize); -#endif - } else { /* Copy LSB. */ -#if LJ_LE - memcpy(dp, sp, dsize); -#else - memcpy(dp, sp + (ssize-dsize), dsize); -#endif - } - break; - case CCX(I, F): { - double n; /* Always convert via double. */ - conv_I_F: - /* Convert source to double. */ - if (ssize == sizeof(double)) n = *(double *)sp; - else if (ssize == sizeof(float)) n = (double)*(float *)sp; - else goto err_conv; /* NYI: long double. */ - /* Then convert double to integer. */ - /* The conversion must exactly match the semantics of JIT-compiled code! */ - if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) { - int32_t i = (int32_t)n; - if (dsize == 4) *(int32_t *)dp = i; - else if (dsize == 2) *(int16_t *)dp = (int16_t)i; - else *(int8_t *)dp = (int8_t)i; - } else if (dsize == 4) { - *(uint32_t *)dp = (uint32_t)n; - } else if (dsize == 8) { - if (!(dinfo & CTF_UNSIGNED)) - *(int64_t *)dp = (int64_t)n; - else - *(uint64_t *)dp = lj_num2u64(n); - } else { - goto err_conv; /* NYI: conversion to >64 bit integers. */ - } - break; - } - case CCX(I, C): - s = ctype_child(cts, s); - sinfo = s->info; - ssize = s->size; - goto conv_I_F; /* Just convert re. */ - case CCX(I, P): - if (!(flags & CCF_CAST)) goto err_conv; - sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - goto conv_I_I; - case CCX(I, A): - if (!(flags & CCF_CAST)) goto err_conv; - sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - ssize = CTSIZE_PTR; - tmpptr = sp; - sp = (uint8_t *)&tmpptr; - goto conv_I_I; - - /* Destination is a floating-point number. */ - case CCX(F, B): - case CCX(F, I): { - double n; /* Always convert via double. */ - conv_F_I: - /* First convert source to double. */ - /* The conversion must exactly match the semantics of JIT-compiled code! */ - if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) { - int32_t i; - if (ssize == 4) { - i = *(int32_t *)sp; - } else if (!(sinfo & CTF_UNSIGNED)) { - if (ssize == 2) i = *(int16_t *)sp; - else i = *(int8_t *)sp; - } else { - if (ssize == 2) i = *(uint16_t *)sp; - else i = *(uint8_t *)sp; - } - n = (double)i; - } else if (ssize == 4) { - n = (double)*(uint32_t *)sp; - } else if (ssize == 8) { - if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp; - else n = (double)*(uint64_t *)sp; - } else { - goto err_conv; /* NYI: conversion from >64 bit integers. */ - } - /* Convert double to destination. */ - if (dsize == sizeof(double)) *(double *)dp = n; - else if (dsize == sizeof(float)) *(float *)dp = (float)n; - else goto err_conv; /* NYI: long double. */ - break; - } - case CCX(F, F): { - double n; /* Always convert via double. */ - conv_F_F: - if (ssize == dsize) goto copyval; - /* Convert source to double. */ - if (ssize == sizeof(double)) n = *(double *)sp; - else if (ssize == sizeof(float)) n = (double)*(float *)sp; - else goto err_conv; /* NYI: long double. */ - /* Convert double to destination. */ - if (dsize == sizeof(double)) *(double *)dp = n; - else if (dsize == sizeof(float)) *(float *)dp = (float)n; - else goto err_conv; /* NYI: long double. */ - break; - } - case CCX(F, C): - s = ctype_child(cts, s); - sinfo = s->info; - ssize = s->size; - goto conv_F_F; /* Ignore im, and convert from re. */ - - /* Destination is a complex number. */ - case CCX(C, I): - d = ctype_child(cts, d); - dinfo = d->info; - dsize = d->size; - memset(dp + dsize, 0, dsize); /* Clear im. */ - goto conv_F_I; /* Convert to re. */ - case CCX(C, F): - d = ctype_child(cts, d); - dinfo = d->info; - dsize = d->size; - memset(dp + dsize, 0, dsize); /* Clear im. */ - goto conv_F_F; /* Convert to re. */ - - case CCX(C, C): - if (dsize != ssize) { /* Different types: convert re/im separately. */ - CType *dc = ctype_child(cts, d); - CType *sc = ctype_child(cts, s); - lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags); - lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags); - return; - } - goto copyval; /* Otherwise this is easy. */ - - /* Destination is a vector. */ - case CCX(V, I): - case CCX(V, F): - case CCX(V, C): { - CType *dc = ctype_child(cts, d); - CTSize esize; - /* First convert the scalar to the first element. */ - lj_cconv_ct_ct(cts, dc, s, dp, sp, flags); - /* Then replicate it to the other elements (splat). */ - for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) { - dp += esize; - memcpy(dp, sp, esize); - } - break; - } - - case CCX(V, V): - /* Copy same-sized vectors, even for different lengths/element-types. */ - if (dsize != ssize) goto err_conv; - goto copyval; - - /* Destination is a pointer. */ - case CCX(P, I): - if (!(flags & CCF_CAST)) goto err_conv; - dinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - goto conv_I_I; - - case CCX(P, F): - if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv; - /* The signed conversion is cheaper. x64 really has 47 bit pointers. */ - dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED); - goto conv_I_F; - - case CCX(P, P): - if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv; - cdata_setptr(dp, dsize, cdata_getptr(sp, ssize)); - break; - - case CCX(P, A): - case CCX(P, S): - if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv; - cdata_setptr(dp, dsize, sp); - break; - - /* Destination is an array. */ - case CCX(A, A): - if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize || - d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags)) - goto err_conv; - goto copyval; - - /* Destination is a struct/union. */ - case CCX(S, S): - if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s) - goto err_conv; /* Must be exact same type. */ -copyval: /* Copy value. */ - lj_assertCTS(dsize == ssize, "value copy with different sizes"); - memcpy(dp, sp, dsize); - break; - - default: - err_conv: - cconv_err_conv(cts, d, s, flags); - } -} - -/* -- C type to TValue conversion ----------------------------------------- */ - -/* Convert C type to TValue. Caveat: expects to get the raw CType! */ -int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, - TValue *o, uint8_t *sp) -{ - CTInfo sinfo = s->info; - if (ctype_isnum(sinfo)) { - if (!ctype_isbool(sinfo)) { - if (ctype_isinteger(sinfo) && s->size > 4) goto copyval; - if (LJ_DUALNUM && ctype_isinteger(sinfo)) { - int32_t i; - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s, - (uint8_t *)&i, sp, 0); - if ((sinfo & CTF_UNSIGNED) && i < 0) - setnumV(o, (lua_Number)(uint32_t)i); - else - setintV(o, i); - } else { - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s, - (uint8_t *)&o->n, sp, 0); - /* Numbers are NOT canonicalized here! Beware of uninitialized data. */ - lj_assertCTS(tvisnum(o), "non-canonical NaN passed"); - } - } else { - uint32_t b = s->size == 1 ? (*sp != 0) : (*(int *)sp != 0); - setboolV(o, b); - setboolV(&cts->g->tmptv2, b); /* Remember for trace recorder. */ - } - return 0; - } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) { - /* Create reference. */ - setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid)); - return 1; /* Need GC step. */ - } else { - GCcdata *cd; - CTSize sz; - copyval: /* Copy value. */ - sz = s->size; - lj_assertCTS(sz != CTSIZE_INVALID, "value copy with invalid size"); - /* Attributes are stripped, qualifiers are kept (but mostly ignored). */ - cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz); - setcdataV(cts->L, o, cd); - memcpy(cdataptr(cd), sp, sz); - return 1; /* Need GC step. */ - } -} - -/* Convert bitfield to TValue. */ -int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp) -{ - CTInfo info = s->info; - CTSize pos, bsz; - uint32_t val; - lj_assertCTS(ctype_isbitfield(info), "bitfield expected"); - /* NYI: packed bitfields may cause misaligned reads. */ - switch (ctype_bitcsz(info)) { - case 4: val = *(uint32_t *)sp; break; - case 2: val = *(uint16_t *)sp; break; - case 1: val = *(uint8_t *)sp; break; - default: - lj_assertCTS(0, "bad bitfield container size %d", ctype_bitcsz(info)); - val = 0; - break; - } - /* Check if a packed bitfield crosses a container boundary. */ - pos = ctype_bitpos(info); - bsz = ctype_bitbsz(info); - lj_assertCTS(pos < 8*ctype_bitcsz(info), "bad bitfield position"); - lj_assertCTS(bsz > 0 && bsz <= 8*ctype_bitcsz(info), "bad bitfield size"); - if (pos + bsz > 8*ctype_bitcsz(info)) - lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT); - if (!(info & CTF_BOOL)) { - CTSize shift = 32 - bsz; - if (!(info & CTF_UNSIGNED)) { - setintV(o, (int32_t)(val << (shift-pos)) >> shift); - } else { - val = (val << (shift-pos)) >> shift; - if (!LJ_DUALNUM || (int32_t)val < 0) - setnumV(o, (lua_Number)(uint32_t)val); - else - setintV(o, (int32_t)val); - } - } else { - uint32_t b = (val >> pos) & 1; - lj_assertCTS(bsz == 1, "bad bool bitfield size"); - setboolV(o, b); - setboolV(&cts->g->tmptv2, b); /* Remember for trace recorder. */ - } - return 0; /* No GC step needed. */ -} - -/* -- TValue to C type conversion ----------------------------------------- */ - -/* Convert table to array. */ -static void cconv_array_tab(CTState *cts, CType *d, - uint8_t *dp, GCtab *t, CTInfo flags) -{ - int32_t i; - CType *dc = ctype_rawchild(cts, d); /* Array element type. */ - CTSize size = d->size, esize = dc->size, ofs = 0; - for (i = 0; ; i++) { - TValue *tv = (TValue *)lj_tab_getint(t, i); - if (!tv || tvisnil(tv)) { - if (i == 0) continue; /* Try again for 1-based tables. */ - break; /* Stop at first nil. */ - } - if (ofs >= size) - cconv_err_initov(cts, d); - lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags); - ofs += esize; - } - if (size != CTSIZE_INVALID) { /* Only fill up arrays with known size. */ - if (ofs == esize) { /* Replicate a single element. */ - for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize); - } else { /* Otherwise fill the remainder with zero. */ - memset(dp + ofs, 0, size - ofs); - } - } -} - -/* Convert table to sub-struct/union. */ -static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp, - GCtab *t, int32_t *ip, CTInfo flags) -{ - CTypeID id = d->sib; - while (id) { - CType *df = ctype_get(cts, id); - id = df->sib; - if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) { - TValue *tv; - int32_t i = *ip, iz = i; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - if (i >= 0) { - retry: - tv = (TValue *)lj_tab_getint(t, i); - if (!tv || tvisnil(tv)) { - if (i == 0) { i = 1; goto retry; } /* 1-based tables. */ - if (iz == 0) { *ip = i = -1; goto tryname; } /* Init named fields. */ - break; /* Stop at first nil. */ - } - *ip = i + 1; - } else { - tryname: - tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name))); - if (!tv || tvisnil(tv)) continue; - } - if (ctype_isfield(df->info)) - lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags); - else - lj_cconv_bf_tv(cts, df, dp+df->size, tv); - if ((d->info & CTF_UNION)) break; - } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) { - cconv_substruct_tab(cts, ctype_rawchild(cts, df), - dp+df->size, t, ip, flags); - } /* Ignore all other entries in the chain. */ - } -} - -/* Convert table to struct/union. */ -static void cconv_struct_tab(CTState *cts, CType *d, - uint8_t *dp, GCtab *t, CTInfo flags) -{ - int32_t i = 0; - memset(dp, 0, d->size); /* Much simpler to clear the struct first. */ - cconv_substruct_tab(cts, d, dp, t, &i, flags); -} - -/* Convert TValue to C type. Caveat: expects to get the raw CType! */ -void lj_cconv_ct_tv(CTState *cts, CType *d, - uint8_t *dp, TValue *o, CTInfo flags) -{ - CTypeID sid = CTID_P_VOID; - CType *s; - void *tmpptr; - uint8_t tmpbool, *sp = (uint8_t *)&tmpptr; - if (LJ_LIKELY(tvisint(o))) { - sp = (uint8_t *)&o->i; - sid = CTID_INT32; - flags |= CCF_FROMTV; - } else if (LJ_LIKELY(tvisnum(o))) { - sp = (uint8_t *)&o->n; - sid = CTID_DOUBLE; - flags |= CCF_FROMTV; - } else if (tviscdata(o)) { - sp = cdataptr(cdataV(o)); - sid = cdataV(o)->ctypeid; - s = ctype_get(cts, sid); - if (ctype_isref(s->info)) { /* Resolve reference for value. */ - lj_assertCTS(s->size == CTSIZE_PTR, "ref is not pointer-sized"); - sp = *(void **)sp; - sid = ctype_cid(s->info); - } - s = ctype_raw(cts, sid); - if (ctype_isfunc(s->info)) { - CTypeID did = ctype_typeid(cts, d); - sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR); - d = ctype_get(cts, did); /* cts->tab may have been reallocated. */ - } else { - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - goto doconv; - } - } else if (tvisstr(o)) { - GCstr *str = strV(o); - if (ctype_isenum(d->info)) { /* Match string against enum constant. */ - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, d, str, &ofs); - if (!cct || !ctype_isconstval(cct->info)) - goto err_conv; - lj_assertCTS(d->size == 4, "only 32 bit enum supported"); /* NYI */ - sp = (uint8_t *)&cct->size; - sid = ctype_cid(cct->info); - } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */ - CType *dc = ctype_rawchild(cts, d); - CTSize sz = str->len+1; - if (!ctype_isinteger(dc->info) || dc->size != 1) - goto err_conv; - if (d->size != 0 && d->size < sz) - sz = d->size; - memcpy(dp, strdata(str), sz); - return; - } else { /* Otherwise pass it as a const char[]. */ - sp = (uint8_t *)strdata(str); - sid = CTID_A_CCHAR; - flags |= CCF_FROMTV; - } - } else if (tvistab(o)) { - if (ctype_isarray(d->info)) { - cconv_array_tab(cts, d, dp, tabV(o), flags); - return; - } else if (ctype_isstruct(d->info)) { - cconv_struct_tab(cts, d, dp, tabV(o), flags); - return; - } else { - goto err_conv; - } - } else if (tvisbool(o)) { - tmpbool = boolV(o); - sp = &tmpbool; - sid = CTID_BOOL; - } else if (tvisnil(o)) { - tmpptr = (void *)0; - flags |= CCF_FROMTV; - } else if (tvisudata(o)) { - GCudata *ud = udataV(o); - tmpptr = uddata(ud); - if (ud->udtype == UDTYPE_IO_FILE) - tmpptr = *(void **)tmpptr; - else if (ud->udtype == UDTYPE_BUFFER) - tmpptr = ((SBufExt *)tmpptr)->r; - } else if (tvislightud(o)) { - tmpptr = lightudV(cts->g, o); - } else if (tvisfunc(o)) { - void *p = lj_ccallback_new(cts, d, funcV(o)); - if (p) { - *(void **)dp = p; - return; - } - goto err_conv; - } else { - err_conv: - cconv_err_convtv(cts, d, o, flags); - } - s = ctype_get(cts, sid); -doconv: - if (ctype_isenum(d->info)) d = ctype_child(cts, d); - lj_cconv_ct_ct(cts, d, s, dp, sp, flags); -} - -/* Convert TValue to bitfield. */ -void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o) -{ - CTInfo info = d->info; - CTSize pos, bsz; - uint32_t val, mask; - lj_assertCTS(ctype_isbitfield(info), "bitfield expected"); - if ((info & CTF_BOOL)) { - uint8_t tmpbool; - lj_assertCTS(ctype_bitbsz(info) == 1, "bad bool bitfield size"); - lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0); - val = tmpbool; - } else { - CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32; - lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0); - } - pos = ctype_bitpos(info); - bsz = ctype_bitbsz(info); - lj_assertCTS(pos < 8*ctype_bitcsz(info), "bad bitfield position"); - lj_assertCTS(bsz > 0 && bsz <= 8*ctype_bitcsz(info), "bad bitfield size"); - /* Check if a packed bitfield crosses a container boundary. */ - if (pos + bsz > 8*ctype_bitcsz(info)) - lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT); - mask = ((1u << bsz) - 1u) << pos; - val = (val << pos) & mask; - /* NYI: packed bitfields may cause misaligned reads/writes. */ - switch (ctype_bitcsz(info)) { - case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break; - case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break; - case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break; - default: - lj_assertCTS(0, "bad bitfield container size %d", ctype_bitcsz(info)); - break; - } -} - -/* -- Initialize C type with TValues -------------------------------------- */ - -/* Initialize an array with TValues. */ -static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp, - TValue *o, MSize len) -{ - CType *dc = ctype_rawchild(cts, d); /* Array element type. */ - CTSize ofs, esize = dc->size; - MSize i; - if (len*esize > sz) - cconv_err_initov(cts, d); - for (i = 0, ofs = 0; i < len; i++, ofs += esize) - lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0); - if (ofs == esize) { /* Replicate a single element. */ - for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize); - } else { /* Otherwise fill the remainder with zero. */ - memset(dp + ofs, 0, sz - ofs); - } -} - -/* Initialize a sub-struct/union with TValues. */ -static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp, - TValue *o, MSize len, MSize *ip) -{ - CTypeID id = d->sib; - while (id) { - CType *df = ctype_get(cts, id); - id = df->sib; - if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) { - MSize i = *ip; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - if (i >= len) break; - *ip = i + 1; - if (ctype_isfield(df->info)) - lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0); - else - lj_cconv_bf_tv(cts, df, dp+df->size, o + i); - if ((d->info & CTF_UNION)) break; - } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) { - cconv_substruct_init(cts, ctype_rawchild(cts, df), - dp+df->size, o, len, ip); - if ((d->info & CTF_UNION)) break; - } /* Ignore all other entries in the chain. */ - } -} - -/* Initialize a struct/union with TValues. */ -static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp, - TValue *o, MSize len) -{ - MSize i = 0; - memset(dp, 0, sz); /* Much simpler to clear the struct first. */ - cconv_substruct_init(cts, d, dp, o, len, &i); - if (i < len) - cconv_err_initov(cts, d); -} - -/* Check whether to use a multi-value initializer. -** This is true if an aggregate is to be initialized with a value. -** Valarrays are treated as values here so ct_tv handles (V|C, I|F). -*/ -int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o) -{ - if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info))) - return 0; /* Destination is not an aggregate. */ - if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info))) - return 0; /* Initializer is not a value. */ - if (tviscdata(o) && lj_ctype_rawref(cts, cdataV(o)->ctypeid) == d) - return 0; /* Source and destination are identical aggregates. */ - return 1; /* Otherwise the initializer is a value. */ -} - -/* Initialize C type with TValues. Caveat: expects to get the raw CType! */ -void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, - uint8_t *dp, TValue *o, MSize len) -{ - if (len == 0) - memset(dp, 0, sz); - else if (len == 1 && !lj_cconv_multi_init(cts, d, o)) - lj_cconv_ct_tv(cts, d, dp, o, 0); - else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */ - cconv_array_init(cts, d, sz, dp, o, len); - else if (ctype_isstruct(d->info)) - cconv_struct_init(cts, d, sz, dp, o, len); - else - cconv_err_initov(cts, d); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.h deleted file mode 100644 index 45b0ca1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cconv.h +++ /dev/null @@ -1,71 +0,0 @@ -/* -** C type conversions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CCONV_H -#define _LJ_CCONV_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* Compressed C type index. ORDER CCX. */ -enum { - CCX_B, /* Bool. */ - CCX_I, /* Integer. */ - CCX_F, /* Floating-point number. */ - CCX_C, /* Complex. */ - CCX_V, /* Vector. */ - CCX_P, /* Pointer. */ - CCX_A, /* Refarray. */ - CCX_S /* Struct/union. */ -}; - -/* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */ -static LJ_AINLINE uint32_t cconv_idx(CTInfo info) -{ - uint32_t idx = ((info >> 26) & 15u); /* Dispatch bits. */ - lj_assertX(ctype_type(info) <= CT_MAYCONVERT, - "cannot convert ctype %08x", info); -#if LJ_64 - idx = ((uint32_t)(U64x(f436fff5,fff7f021) >> 4*idx) & 15u); -#else - idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u); -#endif - lj_assertX(idx < 8, "cannot convert ctype %08x", info); - return idx; -} - -#define cconv_idx2(dinfo, sinfo) \ - ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo))) - -#define CCX(dst, src) ((CCX_##dst << 3) + CCX_##src) - -/* Conversion flags. */ -#define CCF_CAST 0x00000001u -#define CCF_FROMTV 0x00000002u -#define CCF_SAME 0x00000004u -#define CCF_IGNQUAL 0x00000008u - -#define CCF_ARG_SHIFT 8 -#define CCF_ARG(n) ((n) << CCF_ARG_SHIFT) -#define CCF_GETARG(f) ((f) >> CCF_ARG_SHIFT) - -LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags); -LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, - uint8_t *dp, uint8_t *sp, CTInfo flags); -LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, - TValue *o, uint8_t *sp); -LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp); -LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d, - uint8_t *dp, TValue *o, CTInfo flags); -LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o); -LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o); -LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, - uint8_t *dp, TValue *o, MSize len); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.c deleted file mode 100644 index 01a74f5..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.c +++ /dev/null @@ -1,304 +0,0 @@ -/* -** C data management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" - -/* -- C data allocation --------------------------------------------------- */ - -/* Allocate a new C data object holding a reference to another object. */ -GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id) -{ - CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR); - GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR); - *(const void **)cdataptr(cd) = p; - return cd; -} - -/* Allocate variable-sized or specially aligned C data object. */ -GCcdata *lj_cdata_newv(lua_State *L, CTypeID id, CTSize sz, CTSize align) -{ - global_State *g; - MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) + - (align > CT_MEMALIGN ? (1u<offset = (uint16_t)((char *)cd - p); - cdatav(cd)->extra = extra; - cdatav(cd)->len = sz; - g = G(L); - setgcrefr(cd->nextgc, g->gc.root); - setgcref(g->gc.root, obj2gco(cd)); - newwhite(g, obj2gco(cd)); - cd->marked |= 0x80; - cd->gct = ~LJ_TCDATA; - cd->ctypeid = id; - return cd; -} - -/* Allocate arbitrary C data object. */ -GCcdata *lj_cdata_newx(CTState *cts, CTypeID id, CTSize sz, CTInfo info) -{ - if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN) - return lj_cdata_new(cts, id, sz); - else - return lj_cdata_newv(cts->L, id, sz, ctype_align(info)); -} - -/* Free a C data object. */ -void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd) -{ - if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) { - GCobj *root; - makewhite(g, obj2gco(cd)); - markfinalized(obj2gco(cd)); - if ((root = gcref(g->gc.mmudata)) != NULL) { - setgcrefr(cd->nextgc, root->gch.nextgc); - setgcref(root->gch.nextgc, obj2gco(cd)); - setgcref(g->gc.mmudata, obj2gco(cd)); - } else { - setgcref(cd->nextgc, obj2gco(cd)); - setgcref(g->gc.mmudata, obj2gco(cd)); - } - } else if (LJ_LIKELY(!cdataisv(cd))) { - CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid); - CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR; - lj_assertG(ctype_hassize(ct->info) || ctype_isfunc(ct->info) || - ctype_isextern(ct->info), "free of ctype without a size"); - lj_mem_free(g, cd, sizeof(GCcdata) + sz); - } else { - lj_mem_free(g, memcdatav(cd), sizecdatav(cd)); - } -} - -void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj, uint32_t it) -{ - GCtab *t = ctype_ctsG(G(L))->finalizer; - if (gcref(t->metatable)) { - /* Add cdata to finalizer table, if still enabled. */ - TValue *tv, tmp; - setcdataV(L, &tmp, cd); - lj_gc_anybarriert(L, t); - tv = lj_tab_set(L, t, &tmp); - if (it == LJ_TNIL) { - setnilV(tv); - cd->marked &= ~LJ_GC_CDATA_FIN; - } else { - setgcV(L, tv, obj, it); - cd->marked |= LJ_GC_CDATA_FIN; - } - } -} - -/* -- C data indexing ----------------------------------------------------- */ - -/* Index C data by a TValue. Return CType and pointer. */ -CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp, - CTInfo *qual) -{ - uint8_t *p = (uint8_t *)cdataptr(cd); - CType *ct = ctype_get(cts, cd->ctypeid); - ptrdiff_t idx; - - /* Resolve reference for cdata object. */ - if (ctype_isref(ct->info)) { - lj_assertCTS(ct->size == CTSIZE_PTR, "ref is not pointer-sized"); - p = *(uint8_t **)p; - ct = ctype_child(cts, ct); - } - -collect_attrib: - /* Skip attributes and collect qualifiers. */ - while (ctype_isattrib(ct->info)) { - if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size; - ct = ctype_child(cts, ct); - } - /* Interning rejects refs to refs. */ - lj_assertCTS(!ctype_isref(ct->info), "bad ref of ref"); - - if (tvisint(key)) { - idx = (ptrdiff_t)intV(key); - goto integer_key; - } else if (tvisnum(key)) { /* Numeric key. */ -#ifdef _MSC_VER - /* Workaround for MSVC bug. */ - volatile -#endif - lua_Number n = numV(key); - idx = LJ_64 ? (ptrdiff_t)n : (ptrdiff_t)lj_num2int(n); - integer_key: - if (ctype_ispointer(ct->info)) { - CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */ - if (sz == CTSIZE_INVALID) - lj_err_caller(cts->L, LJ_ERR_FFI_INVSIZE); - if (ctype_isptr(ct->info)) { - p = (uint8_t *)cdata_getptr(p, ct->size); - } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) { - if ((ct->info & CTF_COMPLEX)) idx &= 1; - *qual |= CTF_CONST; /* Valarray elements are constant. */ - } - *pp = p + idx*(int32_t)sz; - return ct; - } - } else if (tviscdata(key)) { /* Integer cdata key. */ - GCcdata *cdk = cdataV(key); - CType *ctk = ctype_raw(cts, cdk->ctypeid); - if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk); - if (ctype_isinteger(ctk->info)) { - lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk, - (uint8_t *)&idx, cdataptr(cdk), 0); - goto integer_key; - } - } else if (tvisstr(key)) { /* String key. */ - GCstr *name = strV(key); - if (ctype_isstruct(ct->info)) { - CTSize ofs; - CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual); - if (fct) { - *pp = p + ofs; - return fct; - } - } else if (ctype_iscomplex(ct->info)) { - if (name->len == 2) { - *qual |= CTF_CONST; /* Complex fields are constant. */ - if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') { - *pp = p; - return ct; - } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') { - *pp = p + (ct->size >> 1); - return ct; - } - } - } else if (cd->ctypeid == CTID_CTYPEID) { - /* Allow indexing a (pointer to) struct constructor to get constants. */ - CType *sct = ctype_raw(cts, *(CTypeID *)p); - if (ctype_isptr(sct->info)) - sct = ctype_rawchild(cts, sct); - if (ctype_isstruct(sct->info)) { - CTSize ofs; - CType *fct = lj_ctype_getfield(cts, sct, name, &ofs); - if (fct && ctype_isconstval(fct->info)) - return fct; - } - ct = sct; /* Allow resolving metamethods for constructors, too. */ - } - } - if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ - if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) { - p = (uint8_t *)cdata_getptr(p, ct->size); - ct = ctype_child(cts, ct); - goto collect_attrib; - } - } - *qual |= 1; /* Lookup failed. */ - return ct; /* But return the resolved raw type. */ -} - -/* -- C data getters ------------------------------------------------------ */ - -/* Get constant value and convert to TValue. */ -static void cdata_getconst(CTState *cts, TValue *o, CType *ct) -{ - CType *ctt = ctype_child(cts, ct); - lj_assertCTS(ctype_isinteger(ctt->info) && ctt->size <= 4, - "only 32 bit const supported"); /* NYI */ - /* Constants are already zero-extended/sign-extended to 32 bits. */ - if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0) - setnumV(o, (lua_Number)(uint32_t)ct->size); - else - setintV(o, (int32_t)ct->size); -} - -/* Get C data value and convert to TValue. */ -int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp) -{ - CTypeID sid; - - if (ctype_isconstval(s->info)) { - cdata_getconst(cts, o, s); - return 0; /* No GC step needed. */ - } else if (ctype_isbitfield(s->info)) { - return lj_cconv_tv_bf(cts, s, o, sp); - } - - /* Get child type of pointer/array/field. */ - lj_assertCTS(ctype_ispointer(s->info) || ctype_isfield(s->info), - "pointer or field expected"); - sid = ctype_cid(s->info); - s = ctype_get(cts, sid); - - /* Resolve reference for field. */ - if (ctype_isref(s->info)) { - lj_assertCTS(s->size == CTSIZE_PTR, "ref is not pointer-sized"); - sp = *(uint8_t **)sp; - sid = ctype_cid(s->info); - s = ctype_get(cts, sid); - } - - /* Skip attributes. */ - while (ctype_isattrib(s->info)) - s = ctype_child(cts, s); - - return lj_cconv_tv_ct(cts, s, sid, o, sp); -} - -/* -- C data setters ------------------------------------------------------ */ - -/* Convert TValue and set C data value. */ -void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual) -{ - if (ctype_isconstval(d->info)) { - goto err_const; - } else if (ctype_isbitfield(d->info)) { - if (((d->info|qual) & CTF_CONST)) goto err_const; - lj_cconv_bf_tv(cts, d, dp, o); - return; - } - - /* Get child type of pointer/array/field. */ - lj_assertCTS(ctype_ispointer(d->info) || ctype_isfield(d->info), - "pointer or field expected"); - d = ctype_child(cts, d); - - /* Resolve reference for field. */ - if (ctype_isref(d->info)) { - lj_assertCTS(d->size == CTSIZE_PTR, "ref is not pointer-sized"); - dp = *(uint8_t **)dp; - d = ctype_child(cts, d); - } - - /* Skip attributes and collect qualifiers. */ - for (;;) { - if (ctype_isattrib(d->info)) { - if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size; - } else { - break; - } - d = ctype_child(cts, d); - } - - lj_assertCTS(ctype_hassize(d->info), "store to ctype without size"); - lj_assertCTS(!ctype_isvoid(d->info), "store to void type"); - - if (((d->info|qual) & CTF_CONST)) { - err_const: - lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST); - } - - lj_cconv_ct_tv(cts, d, dp, o, 0); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.h deleted file mode 100644 index de52e8a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cdata.h +++ /dev/null @@ -1,79 +0,0 @@ -/* -** C data management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CDATA_H -#define _LJ_CDATA_H - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* Get C data pointer. */ -static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz) -{ - if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ - return ((void *)(uintptr_t)*(uint32_t *)p); - } else { - lj_assertX(sz == CTSIZE_PTR, "bad pointer size %d", sz); - return *(void **)p; - } -} - -/* Set C data pointer. */ -static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v) -{ - if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ - *(uint32_t *)p = (uint32_t)(uintptr_t)v; - } else { - lj_assertX(sz == CTSIZE_PTR, "bad pointer size %d", sz); - *(void **)p = (void *)v; - } -} - -/* Allocate fixed-size C data object. */ -static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz) -{ - GCcdata *cd; -#ifdef LUA_USE_ASSERT - CType *ct = ctype_raw(cts, id); - lj_assertCTS((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz, - "inconsistent size of fixed-size cdata alloc"); -#endif - cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz); - cd->gct = ~LJ_TCDATA; - cd->ctypeid = ctype_check(cts, id); - return cd; -} - -/* Variant which works without a valid CTState. */ -static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz) -{ - GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz); - cd->gct = ~LJ_TCDATA; - cd->ctypeid = id; - return cd; -} - -LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id); -LJ_FUNC GCcdata *lj_cdata_newv(lua_State *L, CTypeID id, CTSize sz, - CTSize align); -LJ_FUNC GCcdata *lj_cdata_newx(CTState *cts, CTypeID id, CTSize sz, - CTInfo info); - -LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd); -LJ_FUNC void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj, - uint32_t it); - -LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, - uint8_t **pp, CTInfo *qual); -LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp); -LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, - CTInfo qual); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.c deleted file mode 100644 index 11f23ef..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.c +++ /dev/null @@ -1,43 +0,0 @@ -/* -** Character types. -** Donated to the public domain. -** -** This is intended to replace the problematic libc single-byte NLS functions. -** These just don't make sense anymore with UTF-8 locales becoming the norm -** on POSIX systems. It never worked too well on Windows systems since hardly -** anyone bothered to call setlocale(). -** -** This table is hardcoded for ASCII. Identifiers include the characters -** 128-255, too. This allows for the use of all non-ASCII chars as identifiers -** in the lexer. This is a broad definition, but works well in practice -** for both UTF-8 locales and most single-byte locales (such as ISO-8859-*). -** -** If you really need proper character types for UTF-8 strings, please use -** an add-on library such as slnunicode: http://luaforge.net/projects/sln/ -*/ - -#define lj_char_c -#define LUA_CORE - -#include "lj_char.h" - -LJ_DATADEF const uint8_t lj_char_bits[257] = { - 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 152,152,152,152,152,152,152,152,152,152, 4, 4, 4, 4, 4, 4, - 4,176,176,176,176,176,176,160,160,160,160,160,160,160,160,160, - 160,160,160,160,160,160,160,160,160,160,160, 4, 4, 4, 4,132, - 4,208,208,208,208,208,208,192,192,192,192,192,192,192,192,192, - 192,192,192,192,192,192,192,192,192,192,192, 4, 4, 4, 4, 1, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, - 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128 -}; - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.h deleted file mode 100644 index c3c86d3..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_char.h +++ /dev/null @@ -1,42 +0,0 @@ -/* -** Character types. -** Donated to the public domain. -*/ - -#ifndef _LJ_CHAR_H -#define _LJ_CHAR_H - -#include "lj_def.h" - -#define LJ_CHAR_CNTRL 0x01 -#define LJ_CHAR_SPACE 0x02 -#define LJ_CHAR_PUNCT 0x04 -#define LJ_CHAR_DIGIT 0x08 -#define LJ_CHAR_XDIGIT 0x10 -#define LJ_CHAR_UPPER 0x20 -#define LJ_CHAR_LOWER 0x40 -#define LJ_CHAR_IDENT 0x80 -#define LJ_CHAR_ALPHA (LJ_CHAR_LOWER|LJ_CHAR_UPPER) -#define LJ_CHAR_ALNUM (LJ_CHAR_ALPHA|LJ_CHAR_DIGIT) -#define LJ_CHAR_GRAPH (LJ_CHAR_ALNUM|LJ_CHAR_PUNCT) - -/* Only pass -1 or 0..255 to these macros. Never pass a signed char! */ -#define lj_char_isa(c, t) ((lj_char_bits+1)[(c)] & t) -#define lj_char_iscntrl(c) lj_char_isa((c), LJ_CHAR_CNTRL) -#define lj_char_isspace(c) lj_char_isa((c), LJ_CHAR_SPACE) -#define lj_char_ispunct(c) lj_char_isa((c), LJ_CHAR_PUNCT) -#define lj_char_isdigit(c) lj_char_isa((c), LJ_CHAR_DIGIT) -#define lj_char_isxdigit(c) lj_char_isa((c), LJ_CHAR_XDIGIT) -#define lj_char_isupper(c) lj_char_isa((c), LJ_CHAR_UPPER) -#define lj_char_islower(c) lj_char_isa((c), LJ_CHAR_LOWER) -#define lj_char_isident(c) lj_char_isa((c), LJ_CHAR_IDENT) -#define lj_char_isalpha(c) lj_char_isa((c), LJ_CHAR_ALPHA) -#define lj_char_isalnum(c) lj_char_isa((c), LJ_CHAR_ALNUM) -#define lj_char_isgraph(c) lj_char_isa((c), LJ_CHAR_GRAPH) - -#define lj_char_toupper(c) ((c) - (lj_char_islower(c) >> 1)) -#define lj_char_tolower(c) ((c) + lj_char_isupper(c)) - -LJ_DATA const uint8_t lj_char_bits[257]; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.c deleted file mode 100644 index f0ef6ed..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.c +++ /dev/null @@ -1,434 +0,0 @@ -/* -** FFI C library loader. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_str.h" -#include "lj_udata.h" -#include "lj_ctype.h" -#include "lj_cconv.h" -#include "lj_cdata.h" -#include "lj_clib.h" -#include "lj_strfmt.h" - -/* -- OS-specific functions ----------------------------------------------- */ - -#if LJ_TARGET_DLOPEN - -#include -#include - -#if defined(RTLD_DEFAULT) && !defined(NO_RTLD_DEFAULT) -#define CLIB_DEFHANDLE RTLD_DEFAULT -#elif LJ_TARGET_OSX || LJ_TARGET_BSD -#define CLIB_DEFHANDLE ((void *)(intptr_t)-2) -#else -#define CLIB_DEFHANDLE NULL -#endif - -LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L) -{ - lj_err_callermsg(L, dlerror()); -} - -#define clib_error(L, fmt, name) clib_error_(L) - -#if LJ_TARGET_CYGWIN -#define CLIB_SOPREFIX "cyg" -#else -#define CLIB_SOPREFIX "lib" -#endif - -#if LJ_TARGET_OSX -#define CLIB_SOEXT "%s.dylib" -#elif LJ_TARGET_CYGWIN -#define CLIB_SOEXT "%s.dll" -#else -#define CLIB_SOEXT "%s.so" -#endif - -static const char *clib_extname(lua_State *L, const char *name) -{ - if (!strchr(name, '/') -#if LJ_TARGET_CYGWIN - && !strchr(name, '\\') -#endif - ) { - if (!strchr(name, '.')) { - name = lj_strfmt_pushf(L, CLIB_SOEXT, name); - L->top--; -#if LJ_TARGET_CYGWIN - } else { - return name; -#endif - } - if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] && - name[2] == CLIB_SOPREFIX[2])) { - name = lj_strfmt_pushf(L, CLIB_SOPREFIX "%s", name); - L->top--; - } - } - return name; -} - -/* Check for a recognized ld script line. */ -static const char *clib_check_lds(lua_State *L, const char *buf) -{ - char *p, *e; - if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) && - (p = strchr(buf, '('))) { - while (*++p == ' ') ; - for (e = p; *e && *e != ' ' && *e != ')'; e++) ; - return strdata(lj_str_new(L, p, e-p)); - } - return NULL; -} - -/* Quick and dirty solution to resolve shared library name from ld script. */ -static const char *clib_resolve_lds(lua_State *L, const char *name) -{ - FILE *fp = fopen(name, "r"); - const char *p = NULL; - if (fp) { - char buf[256]; - if (fgets(buf, sizeof(buf), fp)) { - if (!strncmp(buf, "/* GNU ld script", 16)) { /* ld script magic? */ - while (fgets(buf, sizeof(buf), fp)) { /* Check all lines. */ - p = clib_check_lds(L, buf); - if (p) break; - } - } else { /* Otherwise check only the first line. */ - p = clib_check_lds(L, buf); - } - } - fclose(fp); - } - return p; -} - -static void *clib_loadlib(lua_State *L, const char *name, int global) -{ - void *h = dlopen(clib_extname(L, name), - RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL)); - if (!h) { - const char *e, *err = dlerror(); - if (err && *err == '/' && (e = strchr(err, ':')) && - (name = clib_resolve_lds(L, strdata(lj_str_new(L, err, e-err))))) { - h = dlopen(name, RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL)); - if (h) return h; - err = dlerror(); - } - if (!err) err = "dlopen failed"; - lj_err_callermsg(L, err); - } - return h; -} - -static void clib_unloadlib(CLibrary *cl) -{ - if (cl->handle && cl->handle != CLIB_DEFHANDLE) - dlclose(cl->handle); -} - -static void *clib_getsym(CLibrary *cl, const char *name) -{ - void *p = dlsym(cl->handle, name); - return p; -} - -#elif LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS -#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 -#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 -BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); -#endif - -#define CLIB_DEFHANDLE ((void *)-1) - -/* Default libraries. */ -enum { - CLIB_HANDLE_EXE, -#if !LJ_TARGET_UWP - CLIB_HANDLE_DLL, - CLIB_HANDLE_CRT, - CLIB_HANDLE_KERNEL32, - CLIB_HANDLE_USER32, - CLIB_HANDLE_GDI32, -#endif - CLIB_HANDLE_MAX -}; - -static void *clib_def_handle[CLIB_HANDLE_MAX]; - -LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt, - const char *name) -{ - DWORD err = GetLastError(); -#if LJ_TARGET_XBOXONE - wchar_t wbuf[128]; - char buf[128*2]; - if (!FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, - NULL, err, 0, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL) || - !WideCharToMultiByte(CP_ACP, 0, wbuf, 128, buf, 128*2, NULL, NULL)) -#else - char buf[128]; - if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, - NULL, err, 0, buf, sizeof(buf), NULL)) -#endif - buf[0] = '\0'; - lj_err_callermsg(L, lj_strfmt_pushf(L, fmt, name, buf)); -} - -static int clib_needext(const char *s) -{ - while (*s) { - if (*s == '/' || *s == '\\' || *s == '.') return 0; - s++; - } - return 1; -} - -static const char *clib_extname(lua_State *L, const char *name) -{ - if (clib_needext(name)) { - name = lj_strfmt_pushf(L, "%s.dll", name); - L->top--; - } - return name; -} - -static void *clib_loadlib(lua_State *L, const char *name, int global) -{ - DWORD oldwerr = GetLastError(); - void *h = LJ_WIN_LOADLIBA(clib_extname(L, name)); - if (!h) clib_error(L, "cannot load module " LUA_QS ": %s", name); - SetLastError(oldwerr); - UNUSED(global); - return h; -} - -static void clib_unloadlib(CLibrary *cl) -{ - if (cl->handle == CLIB_DEFHANDLE) { -#if !LJ_TARGET_UWP - MSize i; - for (i = CLIB_HANDLE_KERNEL32; i < CLIB_HANDLE_MAX; i++) { - void *h = clib_def_handle[i]; - if (h) { - clib_def_handle[i] = NULL; - FreeLibrary((HINSTANCE)h); - } - } -#endif - } else if (cl->handle) { - FreeLibrary((HINSTANCE)cl->handle); - } -} - -#if LJ_TARGET_UWP -EXTERN_C IMAGE_DOS_HEADER __ImageBase; -#endif - -static void *clib_getsym(CLibrary *cl, const char *name) -{ - void *p = NULL; - if (cl->handle == CLIB_DEFHANDLE) { /* Search default libraries. */ - MSize i; - for (i = 0; i < CLIB_HANDLE_MAX; i++) { - HINSTANCE h = (HINSTANCE)clib_def_handle[i]; - if (!(void *)h) { /* Resolve default library handles (once). */ -#if LJ_TARGET_UWP - h = (HINSTANCE)&__ImageBase; -#else - switch (i) { - case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break; - case CLIB_HANDLE_DLL: - GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (const char *)clib_def_handle, &h); - break; - case CLIB_HANDLE_CRT: - GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (const char *)&_fmode, &h); - break; - case CLIB_HANDLE_KERNEL32: h = LJ_WIN_LOADLIBA("kernel32.dll"); break; - case CLIB_HANDLE_USER32: h = LJ_WIN_LOADLIBA("user32.dll"); break; - case CLIB_HANDLE_GDI32: h = LJ_WIN_LOADLIBA("gdi32.dll"); break; - } - if (!h) continue; -#endif - clib_def_handle[i] = (void *)h; - } - p = (void *)GetProcAddress(h, name); - if (p) break; - } - } else { - p = (void *)GetProcAddress((HINSTANCE)cl->handle, name); - } - return p; -} - -#else - -#define CLIB_DEFHANDLE NULL - -LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt, - const char *name) -{ - lj_err_callermsg(L, lj_strfmt_pushf(L, fmt, name, "no support for this OS")); -} - -static void *clib_loadlib(lua_State *L, const char *name, int global) -{ - lj_err_callermsg(L, "no support for loading dynamic libraries for this OS"); - UNUSED(name); UNUSED(global); - return NULL; -} - -static void clib_unloadlib(CLibrary *cl) -{ - UNUSED(cl); -} - -static void *clib_getsym(CLibrary *cl, const char *name) -{ - UNUSED(cl); UNUSED(name); - return NULL; -} - -#endif - -/* -- C library indexing -------------------------------------------------- */ - -#if LJ_TARGET_X86 && LJ_ABI_WIN -/* Compute argument size for fastcall/stdcall functions. */ -static CTSize clib_func_argsize(CTState *cts, CType *ct) -{ - CTSize n = 0; - while (ct->sib) { - CType *d; - ct = ctype_get(cts, ct->sib); - if (ctype_isfield(ct->info)) { - d = ctype_rawchild(cts, ct); - n += ((d->size + 3) & ~3); - } - } - return n; -} -#endif - -/* Get redirected or mangled external symbol. */ -static const char *clib_extsym(CTState *cts, CType *ct, GCstr *name) -{ - if (ct->sib) { - CType *ctf = ctype_get(cts, ct->sib); - if (ctype_isxattrib(ctf->info, CTA_REDIR)) - return strdata(gco2str(gcref(ctf->name))); - } - return strdata(name); -} - -/* Index a C library by name. */ -TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name) -{ - TValue *tv = lj_tab_setstr(L, cl->cache, name); - if (LJ_UNLIKELY(tvisnil(tv))) { - CTState *cts = ctype_cts(L); - CType *ct; - CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX); - if (!id) - lj_err_callerv(L, LJ_ERR_FFI_NODECL, strdata(name)); - if (ctype_isconstval(ct->info)) { - CType *ctt = ctype_child(cts, ct); - lj_assertCTS(ctype_isinteger(ctt->info) && ctt->size <= 4, - "only 32 bit const supported"); /* NYI */ - if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0) - setnumV(tv, (lua_Number)(uint32_t)ct->size); - else - setintV(tv, (int32_t)ct->size); - } else { - const char *sym = clib_extsym(cts, ct, name); -#if LJ_TARGET_WINDOWS - DWORD oldwerr = GetLastError(); -#endif - void *p = clib_getsym(cl, sym); - GCcdata *cd; - lj_assertCTS(ctype_isfunc(ct->info) || ctype_isextern(ct->info), - "unexpected ctype %08x in clib", ct->info); -#if LJ_TARGET_X86 && LJ_ABI_WIN - /* Retry with decorated name for fastcall/stdcall functions. */ - if (!p && ctype_isfunc(ct->info)) { - CTInfo cconv = ctype_cconv(ct->info); - if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) { - CTSize sz = clib_func_argsize(cts, ct); - const char *symd = lj_strfmt_pushf(L, - cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d", - sym, sz); - L->top--; - p = clib_getsym(cl, symd); - } - } -#endif - if (!p) - clib_error(L, "cannot resolve symbol " LUA_QS ": %s", sym); -#if LJ_TARGET_WINDOWS - SetLastError(oldwerr); -#endif - cd = lj_cdata_new(cts, id, CTSIZE_PTR); - *(void **)cdataptr(cd) = p; - setcdataV(L, tv, cd); - lj_gc_anybarriert(L, cl->cache); - } - } - return tv; -} - -/* -- C library management ------------------------------------------------ */ - -/* Create a new CLibrary object and push it on the stack. */ -static CLibrary *clib_new(lua_State *L, GCtab *mt) -{ - GCtab *t = lj_tab_new(L, 0, 0); - GCudata *ud = lj_udata_new(L, sizeof(CLibrary), t); - CLibrary *cl = (CLibrary *)uddata(ud); - cl->cache = t; - ud->udtype = UDTYPE_FFI_CLIB; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcref(ud->metatable, obj2gco(mt)); - setudataV(L, L->top++, ud); - return cl; -} - -/* Load a C library. */ -void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global) -{ - void *handle = clib_loadlib(L, strdata(name), global); - CLibrary *cl = clib_new(L, mt); - cl->handle = handle; -} - -/* Unload a C library. */ -void lj_clib_unload(CLibrary *cl) -{ - clib_unloadlib(cl); - cl->handle = NULL; -} - -/* Create the default C library object. */ -void lj_clib_default(lua_State *L, GCtab *mt) -{ - CLibrary *cl = clib_new(L, mt); - cl->handle = CLIB_DEFHANDLE; -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.h deleted file mode 100644 index 4429486..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_clib.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -** FFI C library loader. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CLIB_H -#define _LJ_CLIB_H - -#include "lj_obj.h" - -#if LJ_HASFFI - -/* Namespace for C library indexing. */ -#define CLNS_INDEX ((1u<env. */ -} CLibrary; - -LJ_FUNC TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name); -LJ_FUNC void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global); -LJ_FUNC void lj_clib_unload(CLibrary *cl); -LJ_FUNC void lj_clib_default(lua_State *L, GCtab *mt); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.c deleted file mode 100644 index 7fd8399..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.c +++ /dev/null @@ -1,1927 +0,0 @@ -/* -** C declaration parser. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_ctype.h" -#include "lj_cparse.h" -#include "lj_frame.h" -#include "lj_vm.h" -#include "lj_char.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" - -/* -** Important note: this is NOT a validating C parser! This is a minimal -** C declaration parser, solely for use by the LuaJIT FFI. -** -** It ought to return correct results for properly formed C declarations, -** but it may accept some invalid declarations, too (and return nonsense). -** Also, it shows rather generic error messages to avoid unnecessary bloat. -** If in doubt, please check the input against your favorite C compiler. -*/ - -#ifdef LUA_USE_ASSERT -#define lj_assertCP(c, ...) (lj_assertG_(G(cp->L), (c), __VA_ARGS__)) -#else -#define lj_assertCP(c, ...) ((void)cp) -#endif - -/* -- Miscellaneous ------------------------------------------------------- */ - -/* Match string against a C literal. */ -#define cp_str_is(str, k) \ - ((str)->len == sizeof(k)-1 && !memcmp(strdata(str), k, sizeof(k)-1)) - -/* Check string against a linear list of matches. */ -int lj_cparse_case(GCstr *str, const char *match) -{ - MSize len; - int n; - for (n = 0; (len = (MSize)*match++); n++, match += len) { - if (str->len == len && !memcmp(match, strdata(str), len)) - return n; - } - return -1; -} - -/* -- C lexer ------------------------------------------------------------- */ - -/* C lexer token names. */ -static const char *const ctoknames[] = { -#define CTOKSTR(name, str) str, -CTOKDEF(CTOKSTR) -#undef CTOKSTR - NULL -}; - -/* Forward declaration. */ -LJ_NORET static void cp_err(CPState *cp, ErrMsg em); - -static const char *cp_tok2str(CPState *cp, CPToken tok) -{ - lj_assertCP(tok < CTOK_FIRSTDECL, "bad CPToken %d", tok); - if (tok > CTOK_OFS) - return ctoknames[tok-CTOK_OFS-1]; - else if (!lj_char_iscntrl(tok)) - return lj_strfmt_pushf(cp->L, "%c", tok); - else - return lj_strfmt_pushf(cp->L, "char(%d)", tok); -} - -/* End-of-line? */ -static LJ_AINLINE int cp_iseol(CPChar c) -{ - return (c == '\n' || c == '\r'); -} - -/* Peek next raw character. */ -static LJ_AINLINE CPChar cp_rawpeek(CPState *cp) -{ - return (CPChar)(uint8_t)(*cp->p); -} - -static LJ_NOINLINE CPChar cp_get_bs(CPState *cp); - -/* Get next character. */ -static LJ_AINLINE CPChar cp_get(CPState *cp) -{ - cp->c = (CPChar)(uint8_t)(*cp->p++); - if (LJ_LIKELY(cp->c != '\\')) return cp->c; - return cp_get_bs(cp); -} - -/* Transparently skip backslash-escaped line breaks. */ -static LJ_NOINLINE CPChar cp_get_bs(CPState *cp) -{ - CPChar c2, c = cp_rawpeek(cp); - if (!cp_iseol(c)) return cp->c; - cp->p++; - c2 = cp_rawpeek(cp); - if (cp_iseol(c2) && c2 != c) cp->p++; - cp->linenumber++; - return cp_get(cp); -} - -/* Save character in buffer. */ -static LJ_AINLINE void cp_save(CPState *cp, CPChar c) -{ - lj_buf_putb(&cp->sb, c); -} - -/* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ -static void cp_newline(CPState *cp) -{ - CPChar c = cp_rawpeek(cp); - if (cp_iseol(c) && c != cp->c) cp->p++; - cp->linenumber++; -} - -LJ_NORET static void cp_errmsg(CPState *cp, CPToken tok, ErrMsg em, ...) -{ - const char *msg, *tokstr; - lua_State *L; - va_list argp; - if (tok == 0) { - tokstr = NULL; - } else if (tok == CTOK_IDENT || tok == CTOK_INTEGER || tok == CTOK_STRING || - tok >= CTOK_FIRSTDECL) { - if (cp->sb.w == cp->sb.b) cp_save(cp, '$'); - cp_save(cp, '\0'); - tokstr = cp->sb.b; - } else { - tokstr = cp_tok2str(cp, tok); - } - L = cp->L; - va_start(argp, em); - msg = lj_strfmt_pushvf(L, err2msg(em), argp); - va_end(argp); - if (tokstr) - msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr); - if (cp->linenumber > 1) - msg = lj_strfmt_pushf(L, "%s at line %d", msg, cp->linenumber); - lj_err_callermsg(L, msg); -} - -LJ_NORET LJ_NOINLINE static void cp_err_token(CPState *cp, CPToken tok) -{ - cp_errmsg(cp, cp->tok, LJ_ERR_XTOKEN, cp_tok2str(cp, tok)); -} - -LJ_NORET LJ_NOINLINE static void cp_err_badidx(CPState *cp, CType *ct) -{ - GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); - cp_errmsg(cp, 0, LJ_ERR_FFI_BADIDX, strdata(s)); -} - -LJ_NORET LJ_NOINLINE static void cp_err(CPState *cp, ErrMsg em) -{ - cp_errmsg(cp, 0, em); -} - -/* -- Main lexical scanner ------------------------------------------------ */ - -/* Parse number literal. Only handles int32_t/uint32_t right now. */ -static CPToken cp_number(CPState *cp) -{ - StrScanFmt fmt; - TValue o; - do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); - cp_save(cp, '\0'); - fmt = lj_strscan_scan((const uint8_t *)(cp->sb.b), sbuflen(&cp->sb)-1, - &o, STRSCAN_OPT_C); - if (fmt == STRSCAN_INT) cp->val.id = CTID_INT32; - else if (fmt == STRSCAN_U32) cp->val.id = CTID_UINT32; - else if (!(cp->mode & CPARSE_MODE_SKIP)) - cp_errmsg(cp, CTOK_INTEGER, LJ_ERR_XNUMBER); - cp->val.u32 = (uint32_t)o.i; - return CTOK_INTEGER; -} - -/* Parse identifier or keyword. */ -static CPToken cp_ident(CPState *cp) -{ - do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); - cp->str = lj_buf_str(cp->L, &cp->sb); - cp->val.id = lj_ctype_getname(cp->cts, &cp->ct, cp->str, cp->tmask); - if (ctype_type(cp->ct->info) == CT_KW) - return ctype_cid(cp->ct->info); - return CTOK_IDENT; -} - -/* Parse parameter. */ -static CPToken cp_param(CPState *cp) -{ - CPChar c = cp_get(cp); - TValue *o = cp->param; - if (lj_char_isident(c) || c == '$') /* Reserve $xyz for future extensions. */ - cp_errmsg(cp, c, LJ_ERR_XSYNTAX); - if (!o || o >= cp->L->top) - cp_err(cp, LJ_ERR_FFI_NUMPARAM); - cp->param = o+1; - if (tvisstr(o)) { - cp->str = strV(o); - cp->val.id = 0; - cp->ct = &cp->cts->tab[0]; - return CTOK_IDENT; - } else if (tvisnumber(o)) { - cp->val.i32 = numberVint(o); - cp->val.id = CTID_INT32; - return CTOK_INTEGER; - } else { - GCcdata *cd; - if (!tviscdata(o)) - lj_err_argtype(cp->L, (int)(o-cp->L->base)+1, "type parameter"); - cd = cdataV(o); - if (cd->ctypeid == CTID_CTYPEID) - cp->val.id = *(CTypeID *)cdataptr(cd); - else - cp->val.id = cd->ctypeid; - return '$'; - } -} - -/* Parse string or character constant. */ -static CPToken cp_string(CPState *cp) -{ - CPChar delim = cp->c; - cp_get(cp); - while (cp->c != delim) { - CPChar c = cp->c; - if (c == '\0') cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); - if (c == '\\') { - c = cp_get(cp); - switch (c) { - case '\0': cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); break; - case 'a': c = '\a'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 'r': c = '\r'; break; - case 't': c = '\t'; break; - case 'v': c = '\v'; break; - case 'e': c = 27; break; - case 'x': - c = 0; - while (lj_char_isxdigit(cp_get(cp))) - c = (c<<4) + (lj_char_isdigit(cp->c) ? cp->c-'0' : (cp->c&15)+9); - cp_save(cp, (c & 0xff)); - continue; - default: - if (lj_char_isdigit(c)) { - c -= '0'; - if (lj_char_isdigit(cp_get(cp))) { - c = c*8 + (cp->c - '0'); - if (lj_char_isdigit(cp_get(cp))) { - c = c*8 + (cp->c - '0'); - cp_get(cp); - } - } - cp_save(cp, (c & 0xff)); - continue; - } - break; - } - } - cp_save(cp, c); - cp_get(cp); - } - cp_get(cp); - if (delim == '"') { - cp->str = lj_buf_str(cp->L, &cp->sb); - return CTOK_STRING; - } else { - if (sbuflen(&cp->sb) != 1) cp_err_token(cp, '\''); - cp->val.i32 = (int32_t)(char)*cp->sb.b; - cp->val.id = CTID_INT32; - return CTOK_INTEGER; - } -} - -/* Skip C comment. */ -static void cp_comment_c(CPState *cp) -{ - do { - if (cp_get(cp) == '*') { - do { - if (cp_get(cp) == '/') { cp_get(cp); return; } - } while (cp->c == '*'); - } - if (cp_iseol(cp->c)) cp_newline(cp); - } while (cp->c != '\0'); -} - -/* Skip C++ comment. */ -static void cp_comment_cpp(CPState *cp) -{ - while (!cp_iseol(cp_get(cp)) && cp->c != '\0') - ; -} - -/* Lexical scanner for C. Only a minimal subset is implemented. */ -static CPToken cp_next_(CPState *cp) -{ - lj_buf_reset(&cp->sb); - for (;;) { - if (lj_char_isident(cp->c)) - return lj_char_isdigit(cp->c) ? cp_number(cp) : cp_ident(cp); - switch (cp->c) { - case '\n': case '\r': cp_newline(cp); /* fallthrough. */ - case ' ': case '\t': case '\v': case '\f': cp_get(cp); break; - case '"': case '\'': return cp_string(cp); - case '/': - if (cp_get(cp) == '*') cp_comment_c(cp); - else if (cp->c == '/') cp_comment_cpp(cp); - else return '/'; - break; - case '|': - if (cp_get(cp) != '|') return '|'; - cp_get(cp); return CTOK_OROR; - case '&': - if (cp_get(cp) != '&') return '&'; - cp_get(cp); return CTOK_ANDAND; - case '=': - if (cp_get(cp) != '=') return '='; - cp_get(cp); return CTOK_EQ; - case '!': - if (cp_get(cp) != '=') return '!'; - cp_get(cp); return CTOK_NE; - case '<': - if (cp_get(cp) == '=') { cp_get(cp); return CTOK_LE; } - else if (cp->c == '<') { cp_get(cp); return CTOK_SHL; } - return '<'; - case '>': - if (cp_get(cp) == '=') { cp_get(cp); return CTOK_GE; } - else if (cp->c == '>') { cp_get(cp); return CTOK_SHR; } - return '>'; - case '-': - if (cp_get(cp) != '>') return '-'; - cp_get(cp); return CTOK_DEREF; - case '$': - return cp_param(cp); - case '\0': return CTOK_EOF; - default: { CPToken c = cp->c; cp_get(cp); return c; } - } - } -} - -static LJ_NOINLINE CPToken cp_next(CPState *cp) -{ - return (cp->tok = cp_next_(cp)); -} - -/* -- C parser ------------------------------------------------------------ */ - -/* Namespaces for resolving identifiers. */ -#define CPNS_DEFAULT \ - ((1u<linenumber = 1; - cp->depth = 0; - cp->curpack = 0; - cp->packstack[0] = 255; - lj_buf_init(cp->L, &cp->sb); - lj_assertCP(cp->p != NULL, "uninitialized cp->p"); - cp_get(cp); /* Read-ahead first char. */ - cp->tok = 0; - cp->tmask = CPNS_DEFAULT; - cp_next(cp); /* Read-ahead first token. */ -} - -/* Cleanup C parser state. */ -static void cp_cleanup(CPState *cp) -{ - global_State *g = G(cp->L); - lj_buf_free(g, &cp->sb); -} - -/* Check and consume optional token. */ -static int cp_opt(CPState *cp, CPToken tok) -{ - if (cp->tok == tok) { cp_next(cp); return 1; } - return 0; -} - -/* Check and consume token. */ -static void cp_check(CPState *cp, CPToken tok) -{ - if (cp->tok != tok) cp_err_token(cp, tok); - cp_next(cp); -} - -/* Check if the next token may start a type declaration. */ -static int cp_istypedecl(CPState *cp) -{ - if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECL) return 1; - if (cp->tok == CTOK_IDENT && ctype_istypedef(cp->ct->info)) return 1; - if (cp->tok == '$') return 1; - return 0; -} - -/* -- Constant expression evaluator --------------------------------------- */ - -/* Forward declarations. */ -static void cp_expr_unary(CPState *cp, CPValue *k); -static void cp_expr_sub(CPState *cp, CPValue *k, int pri); - -/* Please note that type handling is very weak here. Most ops simply -** assume integer operands. Accessors are only needed to compute types and -** return synthetic values. The only purpose of the expression evaluator -** is to compute the values of constant expressions one would typically -** find in C header files. And again: this is NOT a validating C parser! -*/ - -/* Parse comma separated expression and return last result. */ -static void cp_expr_comma(CPState *cp, CPValue *k) -{ - do { cp_expr_sub(cp, k, 0); } while (cp_opt(cp, ',')); -} - -/* Parse sizeof/alignof operator. */ -static void cp_expr_sizeof(CPState *cp, CPValue *k, int wantsz) -{ - CTSize sz; - CTInfo info; - if (cp_opt(cp, '(')) { - if (cp_istypedecl(cp)) - k->id = cp_decl_abstract(cp); - else - cp_expr_comma(cp, k); - cp_check(cp, ')'); - } else { - cp_expr_unary(cp, k); - } - info = lj_ctype_info_raw(cp->cts, k->id, &sz); - if (wantsz) { - if (sz != CTSIZE_INVALID) - k->u32 = sz; - else if (k->id != CTID_A_CCHAR) /* Special case for sizeof("string"). */ - cp_err(cp, LJ_ERR_FFI_INVSIZE); - } else { - k->u32 = 1u << ctype_align(info); - } - k->id = CTID_UINT32; /* Really size_t. */ -} - -/* Parse prefix operators. */ -static void cp_expr_prefix(CPState *cp, CPValue *k) -{ - if (cp->tok == CTOK_INTEGER) { - *k = cp->val; cp_next(cp); - } else if (cp_opt(cp, '+')) { - cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */ - } else if (cp_opt(cp, '-')) { - cp_expr_unary(cp, k); k->i32 = -k->i32; - } else if (cp_opt(cp, '~')) { - cp_expr_unary(cp, k); k->i32 = ~k->i32; - } else if (cp_opt(cp, '!')) { - cp_expr_unary(cp, k); k->i32 = !k->i32; k->id = CTID_INT32; - } else if (cp_opt(cp, '(')) { - if (cp_istypedecl(cp)) { /* Cast operator. */ - CTypeID id = cp_decl_abstract(cp); - cp_check(cp, ')'); - cp_expr_unary(cp, k); - k->id = id; /* No conversion performed. */ - } else { /* Sub-expression. */ - cp_expr_comma(cp, k); - cp_check(cp, ')'); - } - } else if (cp_opt(cp, '*')) { /* Indirection. */ - CType *ct; - cp_expr_unary(cp, k); - ct = lj_ctype_rawref(cp->cts, k->id); - if (!ctype_ispointer(ct->info)) - cp_err_badidx(cp, ct); - k->u32 = 0; k->id = ctype_cid(ct->info); - } else if (cp_opt(cp, '&')) { /* Address operator. */ - cp_expr_unary(cp, k); - k->id = lj_ctype_intern(cp->cts, CTINFO(CT_PTR, CTALIGN_PTR+k->id), - CTSIZE_PTR); - } else if (cp_opt(cp, CTOK_SIZEOF)) { - cp_expr_sizeof(cp, k, 1); - } else if (cp_opt(cp, CTOK_ALIGNOF)) { - cp_expr_sizeof(cp, k, 0); - } else if (cp->tok == CTOK_IDENT) { - if (ctype_type(cp->ct->info) == CT_CONSTVAL) { - k->u32 = cp->ct->size; k->id = ctype_cid(cp->ct->info); - } else if (ctype_type(cp->ct->info) == CT_EXTERN) { - k->u32 = cp->val.id; k->id = ctype_cid(cp->ct->info); - } else if (ctype_type(cp->ct->info) == CT_FUNC) { - k->u32 = cp->val.id; k->id = cp->val.id; - } else { - goto err_expr; - } - cp_next(cp); - } else if (cp->tok == CTOK_STRING) { - CTSize sz = cp->str->len; - while (cp_next(cp) == CTOK_STRING) - sz += cp->str->len; - k->u32 = sz + 1; - k->id = CTID_A_CCHAR; - } else { - err_expr: - cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); - } -} - -/* Parse postfix operators. */ -static void cp_expr_postfix(CPState *cp, CPValue *k) -{ - for (;;) { - CType *ct; - if (cp_opt(cp, '[')) { /* Array/pointer index. */ - CPValue k2; - cp_expr_comma(cp, &k2); - ct = lj_ctype_rawref(cp->cts, k->id); - if (!ctype_ispointer(ct->info)) { - ct = lj_ctype_rawref(cp->cts, k2.id); - if (!ctype_ispointer(ct->info)) - cp_err_badidx(cp, ct); - } - cp_check(cp, ']'); - k->u32 = 0; - } else if (cp->tok == '.' || cp->tok == CTOK_DEREF) { /* Struct deref. */ - CTSize ofs; - CType *fct; - ct = lj_ctype_rawref(cp->cts, k->id); - if (cp->tok == CTOK_DEREF) { - if (!ctype_ispointer(ct->info)) - cp_err_badidx(cp, ct); - ct = lj_ctype_rawref(cp->cts, ctype_cid(ct->info)); - } - cp_next(cp); - if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); - if (!ctype_isstruct(ct->info) || ct->size == CTSIZE_INVALID || - !(fct = lj_ctype_getfield(cp->cts, ct, cp->str, &ofs)) || - ctype_isbitfield(fct->info)) { - GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); - cp_errmsg(cp, 0, LJ_ERR_FFI_BADMEMBER, strdata(s), strdata(cp->str)); - } - ct = fct; - k->u32 = ctype_isconstval(ct->info) ? ct->size : 0; - cp_next(cp); - } else { - return; - } - k->id = ctype_cid(ct->info); - } -} - -/* Parse infix operators. */ -static void cp_expr_infix(CPState *cp, CPValue *k, int pri) -{ - CPValue k2; - k2.u32 = 0; k2.id = 0; /* Silence the compiler. */ - for (;;) { - switch (pri) { - case 0: - if (cp_opt(cp, '?')) { - CPValue k3; - cp_expr_comma(cp, &k2); /* Right-associative. */ - cp_check(cp, ':'); - cp_expr_sub(cp, &k3, 0); - k->u32 = k->u32 ? k2.u32 : k3.u32; - k->id = k2.id > k3.id ? k2.id : k3.id; - continue; - } - /* fallthrough */ - case 1: - if (cp_opt(cp, CTOK_OROR)) { - cp_expr_sub(cp, &k2, 2); k->i32 = k->u32 || k2.u32; k->id = CTID_INT32; - continue; - } - /* fallthrough */ - case 2: - if (cp_opt(cp, CTOK_ANDAND)) { - cp_expr_sub(cp, &k2, 3); k->i32 = k->u32 && k2.u32; k->id = CTID_INT32; - continue; - } - /* fallthrough */ - case 3: - if (cp_opt(cp, '|')) { - cp_expr_sub(cp, &k2, 4); k->u32 = k->u32 | k2.u32; goto arith_result; - } - /* fallthrough */ - case 4: - if (cp_opt(cp, '^')) { - cp_expr_sub(cp, &k2, 5); k->u32 = k->u32 ^ k2.u32; goto arith_result; - } - /* fallthrough */ - case 5: - if (cp_opt(cp, '&')) { - cp_expr_sub(cp, &k2, 6); k->u32 = k->u32 & k2.u32; goto arith_result; - } - /* fallthrough */ - case 6: - if (cp_opt(cp, CTOK_EQ)) { - cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 == k2.u32; k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, CTOK_NE)) { - cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 != k2.u32; k->id = CTID_INT32; - continue; - } - /* fallthrough */ - case 7: - if (cp_opt(cp, '<')) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 < k2.i32; - else - k->i32 = k->u32 < k2.u32; - k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, '>')) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 > k2.i32; - else - k->i32 = k->u32 > k2.u32; - k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, CTOK_LE)) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 <= k2.i32; - else - k->i32 = k->u32 <= k2.u32; - k->id = CTID_INT32; - continue; - } else if (cp_opt(cp, CTOK_GE)) { - cp_expr_sub(cp, &k2, 8); - if (k->id == CTID_INT32 && k2.id == CTID_INT32) - k->i32 = k->i32 >= k2.i32; - else - k->i32 = k->u32 >= k2.u32; - k->id = CTID_INT32; - continue; - } - /* fallthrough */ - case 8: - if (cp_opt(cp, CTOK_SHL)) { - cp_expr_sub(cp, &k2, 9); k->u32 = k->u32 << k2.u32; - continue; - } else if (cp_opt(cp, CTOK_SHR)) { - cp_expr_sub(cp, &k2, 9); - if (k->id == CTID_INT32) - k->i32 = k->i32 >> k2.i32; - else - k->u32 = k->u32 >> k2.u32; - continue; - } - /* fallthrough */ - case 9: - if (cp_opt(cp, '+')) { - cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 + k2.u32; - arith_result: - if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ - continue; - } else if (cp_opt(cp, '-')) { - cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 - k2.u32; goto arith_result; - } - /* fallthrough */ - case 10: - if (cp_opt(cp, '*')) { - cp_expr_unary(cp, &k2); k->u32 = k->u32 * k2.u32; goto arith_result; - } else if (cp_opt(cp, '/')) { - cp_expr_unary(cp, &k2); - if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ - if (k2.u32 == 0 || - (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) - cp_err(cp, LJ_ERR_BADVAL); - if (k->id == CTID_INT32) - k->i32 = k->i32 / k2.i32; - else - k->u32 = k->u32 / k2.u32; - continue; - } else if (cp_opt(cp, '%')) { - cp_expr_unary(cp, &k2); - if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ - if (k2.u32 == 0 || - (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) - cp_err(cp, LJ_ERR_BADVAL); - if (k->id == CTID_INT32) - k->i32 = k->i32 % k2.i32; - else - k->u32 = k->u32 % k2.u32; - continue; - } - default: - return; - } - } -} - -/* Parse and evaluate unary expression. */ -static void cp_expr_unary(CPState *cp, CPValue *k) -{ - if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); - cp_expr_prefix(cp, k); - cp_expr_postfix(cp, k); - cp->depth--; -} - -/* Parse and evaluate sub-expression. */ -static void cp_expr_sub(CPState *cp, CPValue *k, int pri) -{ - cp_expr_unary(cp, k); - cp_expr_infix(cp, k, pri); -} - -/* Parse constant integer expression. */ -static void cp_expr_kint(CPState *cp, CPValue *k) -{ - CType *ct; - cp_expr_sub(cp, k, 0); - ct = ctype_raw(cp->cts, k->id); - if (!ctype_isinteger(ct->info)) cp_err(cp, LJ_ERR_BADVAL); -} - -/* Parse (non-negative) size expression. */ -static CTSize cp_expr_ksize(CPState *cp) -{ - CPValue k; - cp_expr_kint(cp, &k); - if (k.u32 >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); - return k.u32; -} - -/* -- Type declaration stack management ----------------------------------- */ - -/* Add declaration element behind the insertion position. */ -static CPDeclIdx cp_add(CPDecl *decl, CTInfo info, CTSize size) -{ - CPDeclIdx top = decl->top; - if (top >= CPARSE_MAX_DECLSTACK) cp_err(decl->cp, LJ_ERR_XLEVELS); - decl->stack[top].info = info; - decl->stack[top].size = size; - decl->stack[top].sib = 0; - setgcrefnull(decl->stack[top].name); - decl->stack[top].next = decl->stack[decl->pos].next; - decl->stack[decl->pos].next = (CTypeID1)top; - decl->top = top+1; - return top; -} - -/* Push declaration element before the insertion position. */ -static CPDeclIdx cp_push(CPDecl *decl, CTInfo info, CTSize size) -{ - return (decl->pos = cp_add(decl, info, size)); -} - -/* Push or merge attributes. */ -static void cp_push_attributes(CPDecl *decl) -{ - CType *ct = &decl->stack[decl->pos]; - if (ctype_isfunc(ct->info)) { /* Ok to modify in-place. */ -#if LJ_TARGET_X86 - if ((decl->fattr & CTFP_CCONV)) - ct->info = (ct->info & (CTMASK_NUM|CTF_VARARG|CTMASK_CID)) + - (decl->fattr & ~CTMASK_CID); -#endif - } else { - if ((decl->attr & CTFP_ALIGNED) && !(decl->mode & CPARSE_MODE_FIELD)) - cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_ALIGN)), - ctype_align(decl->attr)); - } -} - -/* Push unrolled type to declaration stack and merge qualifiers. */ -static void cp_push_type(CPDecl *decl, CTypeID id) -{ - CType *ct = ctype_get(decl->cp->cts, id); - CTInfo info = ct->info; - CTSize size = ct->size; - switch (ctype_type(info)) { - case CT_STRUCT: case CT_ENUM: - cp_push(decl, CTINFO(CT_TYPEDEF, id), 0); /* Don't copy unique types. */ - if ((decl->attr & CTF_QUAL)) { /* Push unmerged qualifiers. */ - cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_QUAL)), - (decl->attr & CTF_QUAL)); - decl->attr &= ~CTF_QUAL; - } - break; - case CT_ATTRIB: - if (ctype_isxattrib(info, CTA_QUAL)) - decl->attr &= ~size; /* Remove redundant qualifiers. */ - cp_push_type(decl, ctype_cid(info)); /* Unroll. */ - cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ - break; - case CT_ARRAY: - if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) { - info |= (decl->attr & CTF_QUAL); - decl->attr &= ~CTF_QUAL; - } - cp_push_type(decl, ctype_cid(info)); /* Unroll. */ - cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ - decl->stack[decl->pos].sib = 1; /* Mark as already checked and sized. */ - /* Note: this is not copied to the ct->sib in the C type table. */ - break; - case CT_FUNC: - /* Copy type, link parameters (shared). */ - decl->stack[cp_push(decl, info, size)].sib = ct->sib; - break; - default: - /* Copy type, merge common qualifiers. */ - cp_push(decl, info|(decl->attr & CTF_QUAL), size); - decl->attr &= ~CTF_QUAL; - break; - } -} - -/* Consume the declaration element chain and intern the C type. */ -static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl) -{ - CTypeID id = 0; - CPDeclIdx idx = 0; - CTSize csize = CTSIZE_INVALID; - CTSize cinfo = 0; - do { - CType *ct = &decl->stack[idx]; - CTInfo info = ct->info; - CTInfo size = ct->size; - /* The cid is already part of info for copies of pointers/functions. */ - idx = ct->next; - if (ctype_istypedef(info)) { - lj_assertCP(id == 0, "typedef not at toplevel"); - id = ctype_cid(info); - /* Always refetch info/size, since struct/enum may have been completed. */ - cinfo = ctype_get(cp->cts, id)->info; - csize = ctype_get(cp->cts, id)->size; - lj_assertCP(ctype_isstruct(cinfo) || ctype_isenum(cinfo), - "typedef of bad type"); - } else if (ctype_isfunc(info)) { /* Intern function. */ - CType *fct; - CTypeID fid; - CTypeID sib; - if (id) { - CType *refct = ctype_raw(cp->cts, id); - /* Reject function or refarray return types. */ - if (ctype_isfunc(refct->info) || ctype_isrefarray(refct->info)) - cp_err(cp, LJ_ERR_FFI_INVTYPE); - } - /* No intervening attributes allowed, skip forward. */ - while (idx) { - CType *ctn = &decl->stack[idx]; - if (!ctype_isattrib(ctn->info)) break; - idx = ctn->next; /* Skip attribute. */ - } - sib = ct->sib; /* Next line may reallocate the C type table. */ - fid = lj_ctype_new(cp->cts, &fct); - csize = CTSIZE_INVALID; - fct->info = cinfo = info + id; - fct->size = size; - fct->sib = sib; - id = fid; - } else if (ctype_isattrib(info)) { - if (ctype_isxattrib(info, CTA_QUAL)) - cinfo |= size; - else if (ctype_isxattrib(info, CTA_ALIGN)) - CTF_INSERT(cinfo, ALIGN, size); - id = lj_ctype_intern(cp->cts, info+id, size); - /* Inherit csize/cinfo from original type. */ - } else { - if (ctype_isnum(info)) { /* Handle mode/vector-size attributes. */ - lj_assertCP(id == 0, "number not at toplevel"); - if (!(info & CTF_BOOL)) { - CTSize msize = ctype_msizeP(decl->attr); - CTSize vsize = ctype_vsizeP(decl->attr); - if (msize && (!(info & CTF_FP) || (msize == 4 || msize == 8))) { - CTSize malign = lj_fls(msize); - if (malign > 4) malign = 4; /* Limit alignment. */ - CTF_INSERT(info, ALIGN, malign); - size = msize; /* Override size via mode. */ - } - if (vsize) { /* Vector size set? */ - CTSize esize = lj_fls(size); - if (vsize >= esize) { - /* Intern the element type first. */ - id = lj_ctype_intern(cp->cts, info, size); - /* Then create a vector (array) with vsize alignment. */ - size = (1u << vsize); - if (vsize > 4) vsize = 4; /* Limit alignment. */ - if (ctype_align(info) > vsize) vsize = ctype_align(info); - info = CTINFO(CT_ARRAY, (info & CTF_QUAL) + CTF_VECTOR + - CTALIGN(vsize)); - } - } - } - } else if (ctype_isptr(info)) { - /* Reject pointer/ref to ref. */ - if (id && ctype_isref(ctype_raw(cp->cts, id)->info)) - cp_err(cp, LJ_ERR_FFI_INVTYPE); - if (ctype_isref(info)) { - info &= ~CTF_VOLATILE; /* Refs are always const, never volatile. */ - /* No intervening attributes allowed, skip forward. */ - while (idx) { - CType *ctn = &decl->stack[idx]; - if (!ctype_isattrib(ctn->info)) break; - idx = ctn->next; /* Skip attribute. */ - } - } - } else if (ctype_isarray(info)) { /* Check for valid array size etc. */ - if (ct->sib == 0) { /* Only check/size arrays not copied by unroll. */ - if (ctype_isref(cinfo)) /* Reject arrays of refs. */ - cp_err(cp, LJ_ERR_FFI_INVTYPE); - /* Reject VLS or unknown-sized types. */ - if (ctype_isvltype(cinfo) || csize == CTSIZE_INVALID) - cp_err(cp, LJ_ERR_FFI_INVSIZE); - /* a[] and a[?] keep their invalid size. */ - if (size != CTSIZE_INVALID) { - uint64_t xsz = (uint64_t)size * csize; - if (xsz >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); - size = (CTSize)xsz; - } - } - if ((cinfo & CTF_ALIGN) > (info & CTF_ALIGN)) /* Find max. align. */ - info = (info & ~CTF_ALIGN) | (cinfo & CTF_ALIGN); - info |= (cinfo & CTF_QUAL); /* Inherit qual. */ - } else { - lj_assertCP(ctype_isvoid(info), "bad ctype %08x", info); - } - csize = size; - cinfo = info+id; - id = lj_ctype_intern(cp->cts, info+id, size); - } - } while (idx); - return id; -} - -/* -- C declaration parser ------------------------------------------------ */ - -/* Reset declaration state to declaration specifier. */ -static void cp_decl_reset(CPDecl *decl) -{ - decl->pos = decl->specpos; - decl->top = decl->specpos+1; - decl->stack[decl->specpos].next = 0; - decl->attr = decl->specattr; - decl->fattr = decl->specfattr; - decl->name = NULL; - decl->redir = NULL; -} - -/* Parse constant initializer. */ -/* NYI: FP constants and strings as initializers. */ -static CTypeID cp_decl_constinit(CPState *cp, CType **ctp, CTypeID ctypeid) -{ - CType *ctt = ctype_get(cp->cts, ctypeid); - CTInfo info; - CTSize size; - CPValue k; - CTypeID constid; - while (ctype_isattrib(ctt->info)) { /* Skip attributes. */ - ctypeid = ctype_cid(ctt->info); /* Update ID, too. */ - ctt = ctype_get(cp->cts, ctypeid); - } - info = ctt->info; - size = ctt->size; - if (!ctype_isinteger(info) || !(info & CTF_CONST) || size > 4) - cp_err(cp, LJ_ERR_FFI_INVTYPE); - cp_check(cp, '='); - cp_expr_sub(cp, &k, 0); - constid = lj_ctype_new(cp->cts, ctp); - (*ctp)->info = CTINFO(CT_CONSTVAL, CTF_CONST|ctypeid); - k.u32 <<= 8*(4-size); - if ((info & CTF_UNSIGNED)) - k.u32 >>= 8*(4-size); - else - k.u32 = (uint32_t)((int32_t)k.u32 >> 8*(4-size)); - (*ctp)->size = k.u32; - return constid; -} - -/* Parse size in parentheses as part of attribute. */ -static CTSize cp_decl_sizeattr(CPState *cp) -{ - CTSize sz; - uint32_t oldtmask = cp->tmask; - cp->tmask = CPNS_DEFAULT; /* Required for expression evaluator. */ - cp_check(cp, '('); - sz = cp_expr_ksize(cp); - cp->tmask = oldtmask; - cp_check(cp, ')'); - return sz; -} - -/* Parse alignment attribute. */ -static void cp_decl_align(CPState *cp, CPDecl *decl) -{ - CTSize al = 4; /* Unspecified alignment is 16 bytes. */ - if (cp->tok == '(') { - al = cp_decl_sizeattr(cp); - al = al ? lj_fls(al) : 0; - } - CTF_INSERT(decl->attr, ALIGN, al); - decl->attr |= CTFP_ALIGNED; -} - -/* Parse GCC asm("name") redirect. */ -static void cp_decl_asm(CPState *cp, CPDecl *decl) -{ - UNUSED(decl); - cp_next(cp); - cp_check(cp, '('); - if (cp->tok == CTOK_STRING) { - GCstr *str = cp->str; - while (cp_next(cp) == CTOK_STRING) { - lj_strfmt_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str)); - cp->L->top--; - str = strV(cp->L->top); - } - decl->redir = str; - } - cp_check(cp, ')'); -} - -/* Parse GCC __attribute__((mode(...))). */ -static void cp_decl_mode(CPState *cp, CPDecl *decl) -{ - cp_check(cp, '('); - if (cp->tok == CTOK_IDENT) { - const char *s = strdata(cp->str); - CTSize sz = 0, vlen = 0; - if (s[0] == '_' && s[1] == '_') s += 2; - if (*s == 'V') { - s++; - vlen = *s++ - '0'; - if (*s >= '0' && *s <= '9') - vlen = vlen*10 + (*s++ - '0'); - } - switch (*s++) { - case 'Q': sz = 1; break; - case 'H': sz = 2; break; - case 'S': sz = 4; break; - case 'D': sz = 8; break; - case 'T': sz = 16; break; - case 'O': sz = 32; break; - default: goto bad_size; - } - if (*s == 'I' || *s == 'F') { - CTF_INSERT(decl->attr, MSIZEP, sz); - if (vlen) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vlen*sz)); - } - bad_size: - cp_next(cp); - } - cp_check(cp, ')'); -} - -/* Parse GCC __attribute__((...)). */ -static void cp_decl_gccattribute(CPState *cp, CPDecl *decl) -{ - cp_next(cp); - cp_check(cp, '('); - cp_check(cp, '('); - while (cp->tok != ')') { - if (cp->tok == CTOK_IDENT) { - GCstr *attrstr = cp->str; - cp_next(cp); - switch (lj_cparse_case(attrstr, - "\007aligned" "\013__aligned__" - "\006packed" "\012__packed__" - "\004mode" "\010__mode__" - "\013vector_size" "\017__vector_size__" -#if LJ_TARGET_X86 - "\007regparm" "\013__regparm__" - "\005cdecl" "\011__cdecl__" - "\010thiscall" "\014__thiscall__" - "\010fastcall" "\014__fastcall__" - "\007stdcall" "\013__stdcall__" - "\012sseregparm" "\016__sseregparm__" -#endif - )) { - case 0: case 1: /* aligned */ - cp_decl_align(cp, decl); - break; - case 2: case 3: /* packed */ - decl->attr |= CTFP_PACKED; - break; - case 4: case 5: /* mode */ - cp_decl_mode(cp, decl); - break; - case 6: case 7: /* vector_size */ - { - CTSize vsize = cp_decl_sizeattr(cp); - if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize)); - } - break; -#if LJ_TARGET_X86 - case 8: case 9: /* regparm */ - CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp)); - decl->fattr |= CTFP_CCONV; - break; - case 10: case 11: /* cdecl */ - CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL); - decl->fattr |= CTFP_CCONV; - break; - case 12: case 13: /* thiscall */ - CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL); - decl->fattr |= CTFP_CCONV; - break; - case 14: case 15: /* fastcall */ - CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL); - decl->fattr |= CTFP_CCONV; - break; - case 16: case 17: /* stdcall */ - CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL); - decl->fattr |= CTFP_CCONV; - break; - case 18: case 19: /* sseregparm */ - decl->fattr |= CTF_SSEREGPARM; - decl->fattr |= CTFP_CCONV; - break; -#endif - default: /* Skip all other attributes. */ - goto skip_attr; - } - } else if (cp->tok >= CTOK_FIRSTDECL) { /* For __attribute((const)) etc. */ - cp_next(cp); - skip_attr: - if (cp_opt(cp, '(')) { - while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); - cp_check(cp, ')'); - } - } else { - break; - } - if (!cp_opt(cp, ',')) break; - } - cp_check(cp, ')'); - cp_check(cp, ')'); -} - -/* Parse MSVC __declspec(...). */ -static void cp_decl_msvcattribute(CPState *cp, CPDecl *decl) -{ - cp_next(cp); - cp_check(cp, '('); - while (cp->tok == CTOK_IDENT) { - GCstr *attrstr = cp->str; - cp_next(cp); - if (cp_str_is(attrstr, "align")) { - cp_decl_align(cp, decl); - } else { /* Ignore all other attributes. */ - if (cp_opt(cp, '(')) { - while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); - cp_check(cp, ')'); - } - } - } - cp_check(cp, ')'); -} - -/* Parse declaration attributes (and common qualifiers). */ -static void cp_decl_attributes(CPState *cp, CPDecl *decl) -{ - for (;;) { - switch (cp->tok) { - case CTOK_CONST: decl->attr |= CTF_CONST; break; - case CTOK_VOLATILE: decl->attr |= CTF_VOLATILE; break; - case CTOK_RESTRICT: break; /* Ignore. */ - case CTOK_EXTENSION: break; /* Ignore. */ - case CTOK_ATTRIBUTE: cp_decl_gccattribute(cp, decl); continue; - case CTOK_ASM: cp_decl_asm(cp, decl); continue; - case CTOK_DECLSPEC: cp_decl_msvcattribute(cp, decl); continue; - case CTOK_CCDECL: -#if LJ_TARGET_X86 - CTF_INSERT(decl->fattr, CCONV, cp->ct->size); - decl->fattr |= CTFP_CCONV; -#endif - break; - case CTOK_PTRSZ: -#if LJ_64 - CTF_INSERT(decl->attr, MSIZEP, cp->ct->size); -#endif - break; - default: return; - } - cp_next(cp); - } -} - -/* Parse struct/union/enum name. */ -static CTypeID cp_struct_name(CPState *cp, CPDecl *sdecl, CTInfo info) -{ - CTypeID sid; - CType *ct; - cp->tmask = CPNS_STRUCT; - cp_next(cp); - cp_decl_attributes(cp, sdecl); - cp->tmask = CPNS_DEFAULT; - if (cp->tok != '{') { - if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); - if (cp->val.id) { /* Name of existing struct/union/enum. */ - sid = cp->val.id; - ct = cp->ct; - if ((ct->info ^ info) & (CTMASK_NUM|CTF_UNION)) /* Wrong type. */ - cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); - } else { /* Create named, incomplete struct/union/enum. */ - if ((cp->mode & CPARSE_MODE_NOIMPLICIT)) - cp_errmsg(cp, 0, LJ_ERR_FFI_BADTAG, strdata(cp->str)); - sid = lj_ctype_new(cp->cts, &ct); - ct->info = info; - ct->size = CTSIZE_INVALID; - ctype_setname(ct, cp->str); - lj_ctype_addname(cp->cts, ct, sid); - } - cp_next(cp); - } else { /* Create anonymous, incomplete struct/union/enum. */ - sid = lj_ctype_new(cp->cts, &ct); - ct->info = info; - ct->size = CTSIZE_INVALID; - } - if (cp->tok == '{') { - if (ct->size != CTSIZE_INVALID || ct->sib) - cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); - ct->sib = 1; /* Indicate the type is currently being defined. */ - } - return sid; -} - -/* Determine field alignment. */ -static CTSize cp_field_align(CPState *cp, CType *ct, CTInfo info) -{ - CTSize align = ctype_align(info); - UNUSED(cp); UNUSED(ct); -#if (LJ_TARGET_X86 && !LJ_ABI_WIN) || (LJ_TARGET_ARM && __APPLE__) - /* The SYSV i386 and iOS ABIs limit alignment of non-vector fields to 2^2. */ - if (align > 2 && !(info & CTFP_ALIGNED)) { - if (ctype_isarray(info) && !(info & CTF_VECTOR)) { - do { - ct = ctype_rawchild(cp->cts, ct); - info = ct->info; - } while (ctype_isarray(info) && !(info & CTF_VECTOR)); - } - if (ctype_isnum(info) || ctype_isenum(info)) - align = 2; - } -#endif - return align; -} - -/* Layout struct/union fields. */ -static void cp_struct_layout(CPState *cp, CTypeID sid, CTInfo sattr) -{ - CTSize bofs = 0, bmaxofs = 0; /* Bit offset and max. bit offset. */ - CTSize maxalign = ctype_align(sattr); - CType *sct = ctype_get(cp->cts, sid); - CTInfo sinfo = sct->info; - CTypeID fieldid = sct->sib; - while (fieldid) { - CType *ct = ctype_get(cp->cts, fieldid); - CTInfo attr = ct->size; /* Field declaration attributes (temp.). */ - - if (ctype_isfield(ct->info) || - (ctype_isxattrib(ct->info, CTA_SUBTYPE) && attr)) { - CTSize align, amask; /* Alignment (pow2) and alignment mask (bits). */ - CTSize sz; - CTInfo info = lj_ctype_info(cp->cts, ctype_cid(ct->info), &sz); - CTSize bsz, csz = 8*sz; /* Field size and container size (in bits). */ - sinfo |= (info & (CTF_QUAL|CTF_VLA)); /* Merge pseudo-qualifiers. */ - - /* Check for size overflow and determine alignment. */ - if (sz >= 0x20000000u || bofs + csz < bofs || (info & CTF_VLA)) { - if (!(sz == CTSIZE_INVALID && ctype_isarray(info) && - !(sinfo & CTF_UNION))) - cp_err(cp, LJ_ERR_FFI_INVSIZE); - csz = sz = 0; /* Treat a[] and a[?] as zero-sized. */ - } - align = cp_field_align(cp, ct, info); - if (((attr|sattr) & CTFP_PACKED) || - ((attr & CTFP_ALIGNED) && ctype_align(attr) > align)) - align = ctype_align(attr); - if (cp->packstack[cp->curpack] < align) - align = cp->packstack[cp->curpack]; - if (align > maxalign) maxalign = align; - amask = (8u << align) - 1; - - bsz = ctype_bitcsz(ct->info); /* Bitfield size (temp.). */ - if (bsz == CTBSZ_FIELD || !ctype_isfield(ct->info)) { - bsz = csz; /* Regular fields or subtypes always fill the container. */ - bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ - ct->size = (bofs >> 3); /* Store field offset. */ - } else { /* Bitfield. */ - if (bsz == 0 || (attr & CTFP_ALIGNED) || - (!((attr|sattr) & CTFP_PACKED) && (bofs & amask) + bsz > csz)) - bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ - - /* Prefer regular field over bitfield. */ - if (bsz == csz && (bofs & amask) == 0) { - ct->info = CTINFO(CT_FIELD, ctype_cid(ct->info)); - ct->size = (bofs >> 3); /* Store field offset. */ - } else { - ct->info = CTINFO(CT_BITFIELD, - (info & (CTF_QUAL|CTF_UNSIGNED|CTF_BOOL)) + - (csz << (CTSHIFT_BITCSZ-3)) + (bsz << CTSHIFT_BITBSZ)); -#if LJ_BE - ct->info += ((csz - (bofs & (csz-1)) - bsz) << CTSHIFT_BITPOS); -#else - ct->info += ((bofs & (csz-1)) << CTSHIFT_BITPOS); -#endif - ct->size = ((bofs & ~(csz-1)) >> 3); /* Store container offset. */ - } - } - - /* Determine next offset or max. offset. */ - if ((sinfo & CTF_UNION)) { - if (bsz > bmaxofs) bmaxofs = bsz; - } else { - bofs += bsz; - } - } /* All other fields in the chain are already set up. */ - - fieldid = ct->sib; - } - - /* Complete struct/union. */ - sct->info = sinfo + CTALIGN(maxalign); - bofs = (sinfo & CTF_UNION) ? bmaxofs : bofs; - maxalign = (8u << maxalign) - 1; - sct->size = (((bofs + maxalign) & ~maxalign) >> 3); -} - -/* Parse struct/union declaration. */ -static CTypeID cp_decl_struct(CPState *cp, CPDecl *sdecl, CTInfo sinfo) -{ - CTypeID sid = cp_struct_name(cp, sdecl, sinfo); - if (cp_opt(cp, '{')) { /* Struct/union definition. */ - CTypeID lastid = sid; - int lastdecl = 0; - while (cp->tok != '}') { - CPDecl decl; - CPscl scl = cp_decl_spec(cp, &decl, CDF_STATIC); - decl.mode = scl ? CPARSE_MODE_DIRECT : - CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT|CPARSE_MODE_FIELD; - - for (;;) { - CTypeID ctypeid; - - if (lastdecl) cp_err_token(cp, '}'); - - /* Parse field declarator. */ - decl.bits = CTSIZE_INVALID; - cp_declarator(cp, &decl); - ctypeid = cp_decl_intern(cp, &decl); - - if ((scl & CDF_STATIC)) { /* Static constant in struct namespace. */ - CType *ct; - CTypeID fieldid = cp_decl_constinit(cp, &ct, ctypeid); - ctype_get(cp->cts, lastid)->sib = fieldid; - lastid = fieldid; - ctype_setname(ct, decl.name); - } else { - CTSize bsz = CTBSZ_FIELD; /* Temp. for layout phase. */ - CType *ct; - CTypeID fieldid = lj_ctype_new(cp->cts, &ct); /* Do this first. */ - CType *tct = ctype_raw(cp->cts, ctypeid); - - if (decl.bits == CTSIZE_INVALID) { /* Regular field. */ - if (ctype_isarray(tct->info) && tct->size == CTSIZE_INVALID) - lastdecl = 1; /* a[] or a[?] must be the last declared field. */ - - /* Accept transparent struct/union/enum. */ - if (!decl.name) { - if (!((ctype_isstruct(tct->info) && !(tct->info & CTF_VLA)) || - ctype_isenum(tct->info))) - cp_err_token(cp, CTOK_IDENT); - ct->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_SUBTYPE) + ctypeid); - ct->size = ctype_isstruct(tct->info) ? - (decl.attr|0x80000000u) : 0; /* For layout phase. */ - goto add_field; - } - } else { /* Bitfield. */ - bsz = decl.bits; - if (!ctype_isinteger_or_bool(tct->info) || - (bsz == 0 && decl.name) || 8*tct->size > CTBSZ_MAX || - bsz > ((tct->info & CTF_BOOL) ? 1 : 8*tct->size)) - cp_errmsg(cp, ':', LJ_ERR_BADVAL); - } - - /* Create temporary field for layout phase. */ - ct->info = CTINFO(CT_FIELD, ctypeid + (bsz << CTSHIFT_BITCSZ)); - ct->size = decl.attr; - if (decl.name) ctype_setname(ct, decl.name); - - add_field: - ctype_get(cp->cts, lastid)->sib = fieldid; - lastid = fieldid; - } - if (!cp_opt(cp, ',')) break; - cp_decl_reset(&decl); - } - cp_check(cp, ';'); - } - cp_check(cp, '}'); - ctype_get(cp->cts, lastid)->sib = 0; /* Drop sib = 1 for empty structs. */ - cp_decl_attributes(cp, sdecl); /* Layout phase needs postfix attributes. */ - cp_struct_layout(cp, sid, sdecl->attr); - } - return sid; -} - -/* Parse enum declaration. */ -static CTypeID cp_decl_enum(CPState *cp, CPDecl *sdecl) -{ - CTypeID eid = cp_struct_name(cp, sdecl, CTINFO(CT_ENUM, CTID_VOID)); - CTInfo einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_UINT32); - CTSize esize = 4; /* Only 32 bit enums are supported. */ - if (cp_opt(cp, '{')) { /* Enum definition. */ - CPValue k; - CTypeID lastid = eid; - k.u32 = 0; - k.id = CTID_INT32; - do { - GCstr *name = cp->str; - if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); - if (cp->val.id) cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(name)); - cp_next(cp); - if (cp_opt(cp, '=')) { - cp_expr_kint(cp, &k); - if (k.id == CTID_UINT32) { - /* C99 says that enum constants are always (signed) integers. - ** But since unsigned constants like 0x80000000 are quite common, - ** those are left as uint32_t. - */ - if (k.i32 >= 0) k.id = CTID_INT32; - } else { - /* OTOH it's common practice and even mandated by some ABIs - ** that the enum type itself is unsigned, unless there are any - ** negative constants. - */ - k.id = CTID_INT32; - if (k.i32 < 0) einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_INT32); - } - } - /* Add named enum constant. */ - { - CType *ct; - CTypeID constid = lj_ctype_new(cp->cts, &ct); - ctype_get(cp->cts, lastid)->sib = constid; - lastid = constid; - ctype_setname(ct, name); - ct->info = CTINFO(CT_CONSTVAL, CTF_CONST|k.id); - ct->size = k.u32++; - if (k.u32 == 0x80000000u) k.id = CTID_UINT32; - lj_ctype_addname(cp->cts, ct, constid); - } - if (!cp_opt(cp, ',')) break; - } while (cp->tok != '}'); /* Trailing ',' is ok. */ - cp_check(cp, '}'); - /* Complete enum. */ - ctype_get(cp->cts, eid)->info = einfo; - ctype_get(cp->cts, eid)->size = esize; - } - return eid; -} - -/* Parse declaration specifiers. */ -static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl) -{ - uint32_t cds = 0, sz = 0; - CTypeID tdef = 0; - - decl->cp = cp; - decl->mode = cp->mode; - decl->name = NULL; - decl->redir = NULL; - decl->attr = 0; - decl->fattr = 0; - decl->pos = decl->top = 0; - decl->stack[0].next = 0; - - for (;;) { /* Parse basic types. */ - cp_decl_attributes(cp, decl); - if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECLFLAG) { - uint32_t cbit; - if (cp->ct->size) { - if (sz) goto end_decl; - sz = cp->ct->size; - } - cbit = (1u << (cp->tok - CTOK_FIRSTDECL)); - cds = cds | cbit | ((cbit & cds & CDF_LONG) << 1); - if (cp->tok >= CTOK_FIRSTSCL) { - if (!(scl & cbit)) cp_errmsg(cp, cp->tok, LJ_ERR_FFI_BADSCL); - } else if (tdef) { - goto end_decl; - } - cp_next(cp); - continue; - } - if (sz || tdef || - (cds & (CDF_SHORT|CDF_LONG|CDF_SIGNED|CDF_UNSIGNED|CDF_COMPLEX))) - break; - switch (cp->tok) { - case CTOK_STRUCT: - tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, 0)); - continue; - case CTOK_UNION: - tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, CTF_UNION)); - continue; - case CTOK_ENUM: - tdef = cp_decl_enum(cp, decl); - continue; - case CTOK_IDENT: - if (ctype_istypedef(cp->ct->info)) { - tdef = ctype_cid(cp->ct->info); /* Get typedef. */ - cp_next(cp); - continue; - } - break; - case '$': - tdef = cp->val.id; - cp_next(cp); - continue; - default: - break; - } - break; - } -end_decl: - - if ((cds & CDF_COMPLEX)) /* Use predefined complex types. */ - tdef = sz == 4 ? CTID_COMPLEX_FLOAT : CTID_COMPLEX_DOUBLE; - - if (tdef) { - cp_push_type(decl, tdef); - } else if ((cds & CDF_VOID)) { - cp_push(decl, CTINFO(CT_VOID, (decl->attr & CTF_QUAL)), CTSIZE_INVALID); - decl->attr &= ~CTF_QUAL; - } else { - /* Determine type info and size. */ - CTInfo info = CTINFO(CT_NUM, (cds & CDF_UNSIGNED) ? CTF_UNSIGNED : 0); - if ((cds & CDF_BOOL)) { - if ((cds & ~(CDF_SCL|CDF_BOOL|CDF_INT|CDF_SIGNED|CDF_UNSIGNED))) - cp_errmsg(cp, 0, LJ_ERR_FFI_INVTYPE); - info |= CTF_BOOL; - if (!(cds & CDF_SIGNED)) info |= CTF_UNSIGNED; - if (!sz) { - sz = 1; - } - } else if ((cds & CDF_FP)) { - info = CTINFO(CT_NUM, CTF_FP); - if ((cds & CDF_LONG)) sz = sizeof(long double); - } else if ((cds & CDF_CHAR)) { - if ((cds & (CDF_CHAR|CDF_SIGNED|CDF_UNSIGNED)) == CDF_CHAR) - info |= CTF_UCHAR; /* Handle platforms where char is unsigned. */ - } else if ((cds & CDF_SHORT)) { - sz = sizeof(short); - } else if ((cds & CDF_LONGLONG)) { - sz = 8; - } else if ((cds & CDF_LONG)) { - info |= CTF_LONG; - sz = sizeof(long); - } else if (!sz) { - if (!(cds & (CDF_SIGNED|CDF_UNSIGNED))) - cp_errmsg(cp, cp->tok, LJ_ERR_FFI_DECLSPEC); - sz = sizeof(int); - } - lj_assertCP(sz != 0, "basic ctype with zero size"); - info += CTALIGN(lj_fls(sz)); /* Use natural alignment. */ - info += (decl->attr & CTF_QUAL); /* Merge qualifiers. */ - cp_push(decl, info, sz); - decl->attr &= ~CTF_QUAL; - } - decl->specpos = decl->pos; - decl->specattr = decl->attr; - decl->specfattr = decl->fattr; - return (cds & CDF_SCL); /* Return storage class. */ -} - -/* Parse array declaration. */ -static void cp_decl_array(CPState *cp, CPDecl *decl) -{ - CTInfo info = CTINFO(CT_ARRAY, 0); - CTSize nelem = CTSIZE_INVALID; /* Default size for a[] or a[?]. */ - cp_decl_attributes(cp, decl); - if (cp_opt(cp, '?')) - info |= CTF_VLA; /* Create variable-length array a[?]. */ - else if (cp->tok != ']') - nelem = cp_expr_ksize(cp); - cp_check(cp, ']'); - cp_add(decl, info, nelem); -} - -/* Parse function declaration. */ -static void cp_decl_func(CPState *cp, CPDecl *fdecl) -{ - CTSize nargs = 0; - CTInfo info = CTINFO(CT_FUNC, 0); - CTypeID lastid = 0, anchor = 0; - if (cp->tok != ')') { - do { - CPDecl decl; - CTypeID ctypeid, fieldid; - CType *ct; - if (cp_opt(cp, '.')) { /* Vararg function. */ - cp_check(cp, '.'); /* Workaround for the minimalistic lexer. */ - cp_check(cp, '.'); - info |= CTF_VARARG; - break; - } - cp_decl_spec(cp, &decl, CDF_REGISTER); - decl.mode = CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT; - cp_declarator(cp, &decl); - ctypeid = cp_decl_intern(cp, &decl); - ct = ctype_raw(cp->cts, ctypeid); - if (ctype_isvoid(ct->info)) - break; - else if (ctype_isrefarray(ct->info)) - ctypeid = lj_ctype_intern(cp->cts, - CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ct->info)), CTSIZE_PTR); - else if (ctype_isfunc(ct->info)) - ctypeid = lj_ctype_intern(cp->cts, - CTINFO(CT_PTR, CTALIGN_PTR|ctypeid), CTSIZE_PTR); - /* Add new parameter. */ - fieldid = lj_ctype_new(cp->cts, &ct); - if (anchor) - ctype_get(cp->cts, lastid)->sib = fieldid; - else - anchor = fieldid; - lastid = fieldid; - if (decl.name) ctype_setname(ct, decl.name); - ct->info = CTINFO(CT_FIELD, ctypeid); - ct->size = nargs++; - } while (cp_opt(cp, ',')); - } - cp_check(cp, ')'); - if (cp_opt(cp, '{')) { /* Skip function definition. */ - int level = 1; - cp->mode |= CPARSE_MODE_SKIP; - for (;;) { - if (cp->tok == '{') level++; - else if (cp->tok == '}' && --level == 0) break; - else if (cp->tok == CTOK_EOF) cp_err_token(cp, '}'); - cp_next(cp); - } - cp->mode &= ~CPARSE_MODE_SKIP; - cp->tok = ';'; /* Ok for cp_decl_multi(), error in cp_decl_single(). */ - } - info |= (fdecl->fattr & ~CTMASK_CID); - fdecl->fattr = 0; - fdecl->stack[cp_add(fdecl, info, nargs)].sib = anchor; -} - -/* Parse declarator. */ -static void cp_declarator(CPState *cp, CPDecl *decl) -{ - if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); - - for (;;) { /* Head of declarator. */ - if (cp_opt(cp, '*')) { /* Pointer. */ - CTSize sz; - CTInfo info; - cp_decl_attributes(cp, decl); - sz = CTSIZE_PTR; - info = CTINFO(CT_PTR, CTALIGN_PTR); -#if LJ_64 - if (ctype_msizeP(decl->attr) == 4) { - sz = 4; - info = CTINFO(CT_PTR, CTALIGN(2)); - } -#endif - info += (decl->attr & (CTF_QUAL|CTF_REF)); - decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<mode & CPARSE_MODE_ABSTRACT) && - (cp->tok == ')' || cp_istypedecl(cp))) goto func_decl; - pos = decl->pos; - cp_declarator(cp, decl); - cp_check(cp, ')'); - decl->pos = pos; - } else if (cp->tok == CTOK_IDENT) { /* Direct declarator. */ - if (!(decl->mode & CPARSE_MODE_DIRECT)) cp_err_token(cp, CTOK_EOF); - decl->name = cp->str; - decl->nameid = cp->val.id; - cp_next(cp); - } else { /* Abstract declarator. */ - if (!(decl->mode & CPARSE_MODE_ABSTRACT)) cp_err_token(cp, CTOK_IDENT); - } - - for (;;) { /* Tail of declarator. */ - if (cp_opt(cp, '[')) { /* Array. */ - cp_decl_array(cp, decl); - } else if (cp_opt(cp, '(')) { /* Function. */ - func_decl: - cp_decl_func(cp, decl); - } else { - break; - } - } - - if ((decl->mode & CPARSE_MODE_FIELD) && cp_opt(cp, ':')) /* Field width. */ - decl->bits = cp_expr_ksize(cp); - - /* Process postfix attributes. */ - cp_decl_attributes(cp, decl); - cp_push_attributes(decl); - - cp->depth--; -} - -/* Parse an abstract type declaration and return it's C type ID. */ -static CTypeID cp_decl_abstract(CPState *cp) -{ - CPDecl decl; - cp_decl_spec(cp, &decl, 0); - decl.mode = CPARSE_MODE_ABSTRACT; - cp_declarator(cp, &decl); - return cp_decl_intern(cp, &decl); -} - -/* Handle pragmas. */ -static void cp_pragma(CPState *cp, BCLine pragmaline) -{ - cp_next(cp); - if (cp->tok == CTOK_IDENT && cp_str_is(cp->str, "pack")) { - cp_next(cp); - cp_check(cp, '('); - if (cp->tok == CTOK_IDENT) { - if (cp_str_is(cp->str, "push")) { - if (cp->curpack < CPARSE_MAX_PACKSTACK) { - cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack]; - cp->curpack++; - } - } else if (cp_str_is(cp->str, "pop")) { - if (cp->curpack > 0) cp->curpack--; - } else { - cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); - } - cp_next(cp); - if (!cp_opt(cp, ',')) goto end_pack; - } - if (cp->tok == CTOK_INTEGER) { - cp->packstack[cp->curpack] = cp->val.u32 ? lj_fls(cp->val.u32) : 0; - cp_next(cp); - } else { - cp->packstack[cp->curpack] = 255; - } - end_pack: - cp_check(cp, ')'); - } else { /* Ignore all other pragmas. */ - while (cp->tok != CTOK_EOF && cp->linenumber == pragmaline) - cp_next(cp); - } -} - -/* Handle line number. */ -static void cp_line(CPState *cp, BCLine hashline) -{ - BCLine newline = cp->val.u32; - /* TODO: Handle file name and include it in error messages. */ - while (cp->tok != CTOK_EOF && cp->linenumber == hashline) - cp_next(cp); - cp->linenumber = newline; -} - -/* Parse multiple C declarations of types or extern identifiers. */ -static void cp_decl_multi(CPState *cp) -{ - int first = 1; - while (cp->tok != CTOK_EOF) { - CPDecl decl; - CPscl scl; - if (cp_opt(cp, ';')) { /* Skip empty statements. */ - first = 0; - continue; - } - if (cp->tok == '#') { /* Workaround, since we have no preprocessor, yet. */ - BCLine hashline = cp->linenumber; - CPToken tok = cp_next(cp); - if (tok == CTOK_INTEGER) { - cp_line(cp, hashline); - continue; - } else if (tok == CTOK_IDENT && cp_str_is(cp->str, "line")) { - if (cp_next(cp) != CTOK_INTEGER) cp_err_token(cp, tok); - cp_line(cp, hashline); - continue; - } else if (tok == CTOK_IDENT && cp_str_is(cp->str, "pragma")) { - cp_pragma(cp, hashline); - continue; - } else { - cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); - } - } - scl = cp_decl_spec(cp, &decl, CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC); - if ((cp->tok == ';' || cp->tok == CTOK_EOF) && - ctype_istypedef(decl.stack[0].info)) { - CTInfo info = ctype_rawchild(cp->cts, &decl.stack[0])->info; - if (ctype_isstruct(info) || ctype_isenum(info)) - goto decl_end; /* Accept empty declaration of struct/union/enum. */ - } - for (;;) { - CTypeID ctypeid; - cp_declarator(cp, &decl); - ctypeid = cp_decl_intern(cp, &decl); - if (decl.name && !decl.nameid) { /* NYI: redeclarations are ignored. */ - CType *ct; - CTypeID id; - if ((scl & CDF_TYPEDEF)) { /* Create new typedef. */ - id = lj_ctype_new(cp->cts, &ct); - ct->info = CTINFO(CT_TYPEDEF, ctypeid); - goto noredir; - } else if (ctype_isfunc(ctype_get(cp->cts, ctypeid)->info)) { - /* Treat both static and extern function declarations as extern. */ - ct = ctype_get(cp->cts, ctypeid); - /* We always get new anonymous functions (typedefs are copied). */ - lj_assertCP(gcref(ct->name) == NULL, "unexpected named function"); - id = ctypeid; /* Just name it. */ - } else if ((scl & CDF_STATIC)) { /* Accept static constants. */ - id = cp_decl_constinit(cp, &ct, ctypeid); - goto noredir; - } else { /* External references have extern or no storage class. */ - id = lj_ctype_new(cp->cts, &ct); - ct->info = CTINFO(CT_EXTERN, ctypeid); - } - if (decl.redir) { /* Add attribute for redirected symbol name. */ - CType *cta; - CTypeID aid = lj_ctype_new(cp->cts, &cta); - ct = ctype_get(cp->cts, id); /* Table may have been reallocated. */ - cta->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_REDIR)); - cta->sib = ct->sib; - ct->sib = aid; - ctype_setname(cta, decl.redir); - } - noredir: - ctype_setname(ct, decl.name); - lj_ctype_addname(cp->cts, ct, id); - } - if (!cp_opt(cp, ',')) break; - cp_decl_reset(&decl); - } - decl_end: - if (cp->tok == CTOK_EOF && first) break; /* May omit ';' for 1 decl. */ - first = 0; - cp_check(cp, ';'); - } -} - -/* Parse a single C type declaration. */ -static void cp_decl_single(CPState *cp) -{ - CPDecl decl; - cp_decl_spec(cp, &decl, 0); - cp_declarator(cp, &decl); - cp->val.id = cp_decl_intern(cp, &decl); - if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF); -} - -/* ------------------------------------------------------------------------ */ - -/* Protected callback for C parser. */ -static TValue *cpcparser(lua_State *L, lua_CFunction dummy, void *ud) -{ - CPState *cp = (CPState *)ud; - UNUSED(dummy); - cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ - cp_init(cp); - if ((cp->mode & CPARSE_MODE_MULTI)) - cp_decl_multi(cp); - else - cp_decl_single(cp); - if (cp->param && cp->param != cp->L->top) - cp_err(cp, LJ_ERR_FFI_NUMPARAM); - lj_assertCP(cp->depth == 0, "unbalanced cparser declaration depth"); - return NULL; -} - -/* C parser. */ -int lj_cparse(CPState *cp) -{ - LJ_CTYPE_SAVE(cp->cts); - int errcode = lj_vm_cpcall(cp->L, NULL, cp, cpcparser); - if (errcode) - LJ_CTYPE_RESTORE(cp->cts); - cp_cleanup(cp); - return errcode; -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.h deleted file mode 100644 index c0f61ed..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_cparse.h +++ /dev/null @@ -1,67 +0,0 @@ -/* -** C declaration parser. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CPARSE_H -#define _LJ_CPARSE_H - -#include "lj_obj.h" -#include "lj_ctype.h" - -#if LJ_HASFFI - -/* C parser limits. */ -#define CPARSE_MAX_BUF 32768 /* Max. token buffer size. */ -#define CPARSE_MAX_DECLSTACK 100 /* Max. declaration stack depth. */ -#define CPARSE_MAX_DECLDEPTH 20 /* Max. recursive declaration depth. */ -#define CPARSE_MAX_PACKSTACK 7 /* Max. pack pragma stack depth. */ - -/* Flags for C parser mode. */ -#define CPARSE_MODE_MULTI 1 /* Process multiple declarations. */ -#define CPARSE_MODE_ABSTRACT 2 /* Accept abstract declarators. */ -#define CPARSE_MODE_DIRECT 4 /* Accept direct declarators. */ -#define CPARSE_MODE_FIELD 8 /* Accept field width in bits, too. */ -#define CPARSE_MODE_NOIMPLICIT 16 /* Reject implicit declarations. */ -#define CPARSE_MODE_SKIP 32 /* Skip definitions, ignore errors. */ - -typedef int CPChar; /* C parser character. Unsigned ext. from char. */ -typedef int CPToken; /* C parser token. */ - -/* C parser internal value representation. */ -typedef struct CPValue { - union { - int32_t i32; /* Value for CTID_INT32. */ - uint32_t u32; /* Value for CTID_UINT32. */ - }; - CTypeID id; /* C Type ID of the value. */ -} CPValue; - -/* C parser state. */ -typedef struct CPState { - CPChar c; /* Current character. */ - CPToken tok; /* Current token. */ - CPValue val; /* Token value. */ - GCstr *str; /* Interned string of identifier/keyword. */ - CType *ct; /* C type table entry. */ - const char *p; /* Current position in input buffer. */ - SBuf sb; /* String buffer for tokens. */ - lua_State *L; /* Lua state. */ - CTState *cts; /* C type state. */ - TValue *param; /* C type parameters. */ - const char *srcname; /* Current source name. */ - BCLine linenumber; /* Input line counter. */ - int depth; /* Recursive declaration depth. */ - uint32_t tmask; /* Type mask for next identifier. */ - uint32_t mode; /* C parser mode. */ - uint8_t packstack[CPARSE_MAX_PACKSTACK]; /* Stack for pack pragmas. */ - uint8_t curpack; /* Current position in pack pragma stack. */ -} CPState; - -LJ_FUNC int lj_cparse(CPState *cp); - -LJ_FUNC int lj_cparse_case(GCstr *str, const char *match); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.c deleted file mode 100644 index e0f581c..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.c +++ /dev/null @@ -1,1948 +0,0 @@ -/* -** Trace recorder for C data operations. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_ffrecord_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT && LJ_HASFFI - -#include "lj_err.h" -#include "lj_tab.h" -#include "lj_frame.h" -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_cparse.h" -#include "lj_cconv.h" -#include "lj_carith.h" -#include "lj_clib.h" -#include "lj_ccall.h" -#include "lj_ff.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_record.h" -#include "lj_ffrecord.h" -#include "lj_snap.h" -#include "lj_crecord.h" -#include "lj_dispatch.h" -#include "lj_strfmt.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -#define emitconv(a, dt, st, flags) \ - emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags)) - -/* -- C type checks ------------------------------------------------------- */ - -static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o) -{ - GCcdata *cd; - TRef trtypeid; - if (!tref_iscdata(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - cd = cdataV(o); - /* Specialize to the CTypeID. */ - trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID); - emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid)); - return cd; -} - -/* Specialize to the CTypeID held by a cdata constructor. */ -static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr) -{ - CTypeID id; - lj_assertJ(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID, - "expected CTypeID cdata"); - id = *(CTypeID *)cdataptr(cd); - tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT); - emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id)); - return id; -} - -static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o) -{ - if (tref_isstr(tr)) { - GCstr *s = strV(o); - CPState cp; - CTypeID oldtop; - /* Specialize to the string containing the C type declaration. */ - emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s)); - cp.L = J->L; - cp.cts = ctype_cts(J->L); - oldtop = cp.cts->top; - cp.srcname = strdata(s); - cp.p = strdata(s); - cp.param = NULL; - cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; - if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); - return cp.val.id; - } else { - GCcdata *cd = argv2cdata(J, tr, o); - return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) : - cd->ctypeid; - } -} - -/* Convert CType to IRType (if possible). */ -static IRType crec_ct2irt(CTState *cts, CType *ct) -{ - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (LJ_LIKELY(ctype_isnum(ct->info))) { - if ((ct->info & CTF_FP)) { - if (ct->size == sizeof(double)) - return IRT_NUM; - else if (ct->size == sizeof(float)) - return IRT_FLOAT; - } else { - uint32_t b = lj_fls(ct->size); - if (b <= 3) - return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0); - } - } else if (ctype_isptr(ct->info)) { - return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; - } else if (ctype_iscomplex(ct->info)) { - if (ct->size == 2*sizeof(double)) - return IRT_NUM; - else if (ct->size == 2*sizeof(float)) - return IRT_FLOAT; - } - return IRT_CDATA; -} - -/* -- Optimized memory fill and copy -------------------------------------- */ - -/* Maximum length and unroll of inlined copy/fill. */ -#define CREC_COPY_MAXUNROLL 16 -#define CREC_COPY_MAXLEN 128 - -#define CREC_FILL_MAXUNROLL 16 - -/* Number of windowed registers used for optimized memory copy. */ -#if LJ_TARGET_X86 -#define CREC_COPY_REGWIN 2 -#elif LJ_TARGET_PPC || LJ_TARGET_MIPS -#define CREC_COPY_REGWIN 8 -#else -#define CREC_COPY_REGWIN 4 -#endif - -/* List of memory offsets for copy/fill. */ -typedef struct CRecMemList { - CTSize ofs; /* Offset in bytes. */ - IRType tp; /* Type of load/store. */ - TRef trofs; /* TRef of interned offset. */ - TRef trval; /* TRef of load value. */ -} CRecMemList; - -/* Generate copy list for element-wise struct copy. */ -static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct) -{ - CTypeID fid = ct->sib; - MSize mlp = 0; - while (fid) { - CType *df = ctype_get(cts, fid); - fid = df->sib; - if (ctype_isfield(df->info)) { - CType *cct; - IRType tp; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - cct = ctype_rawchild(cts, df); /* Field type. */ - tp = crec_ct2irt(cts, cct); - if (tp == IRT_CDATA) return 0; /* NYI: aggregates. */ - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = df->size; - ml[mlp].tp = tp; - mlp++; - if (ctype_iscomplex(cct->info)) { - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = df->size + (cct->size >> 1); - ml[mlp].tp = tp; - mlp++; - } - } else if (!ctype_isconstval(df->info)) { - /* NYI: bitfields and sub-structures. */ - return 0; - } - } - return mlp; -} - -/* Generate unrolled copy list, from highest to lowest step size/alignment. */ -static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step, - IRType tp) -{ - CTSize ofs = 0; - MSize mlp = 0; - if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step); - do { - while (ofs + step <= len) { - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = ofs; - ml[mlp].tp = tp; - mlp++; - ofs += step; - } - step >>= 1; - tp -= 2; - } while (ofs < len); - return mlp; -} - -/* -** Emit copy list with windowed loads/stores. -** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such). -*/ -static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp, - TRef trdst, TRef trsrc) -{ - MSize i, j, rwin = 0; - for (i = 0, j = 0; i < mlp; ) { - TRef trofs = lj_ir_kintp(J, ml[i].ofs); - TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs); - ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0); - ml[i].trofs = trofs; - i++; - rwin += (LJ_SOFTFP32 && ml[i].tp == IRT_NUM) ? 2 : 1; - if (rwin >= CREC_COPY_REGWIN || i >= mlp) { /* Flush buffered stores. */ - rwin = 0; - for ( ; j < i; j++) { - TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs); - emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval); - } - } - } -} - -/* Optimized memory copy. */ -static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen, - CType *ct) -{ - if (tref_isk(trlen)) { /* Length must be constant. */ - CRecMemList ml[CREC_COPY_MAXUNROLL]; - MSize mlp = 0; - CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i; - IRType tp = IRT_CDATA; - int needxbar = 0; - if (len == 0) return; /* Shortcut. */ - if (len > CREC_COPY_MAXLEN) goto fallback; - if (ct) { - CTState *cts = ctype_ctsG(J2G(J)); - lj_assertJ(ctype_isarray(ct->info) || ctype_isstruct(ct->info), - "copy of non-aggregate"); - if (ctype_isarray(ct->info)) { - CType *cct = ctype_rawchild(cts, ct); - tp = crec_ct2irt(cts, cct); - if (tp == IRT_CDATA) goto rawcopy; - step = lj_ir_type_size[tp]; - lj_assertJ((len & (step-1)) == 0, "copy of fractional size"); - } else if ((ct->info & CTF_UNION)) { - step = (1u << ctype_align(ct->info)); - goto rawcopy; - } else { - mlp = crec_copy_struct(ml, cts, ct); - goto emitcopy; - } - } else { - rawcopy: - needxbar = 1; - if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR) - step = CTSIZE_PTR; - } - mlp = crec_copy_unroll(ml, len, step, tp); - emitcopy: - if (mlp) { - crec_copy_emit(J, ml, mlp, trdst, trsrc); - if (needxbar) - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); - return; - } - } -fallback: - /* Call memcpy. Always needs a barrier to disable alias analysis. */ - lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen); - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); -} - -/* Generate unrolled fill list, from highest to lowest step size/alignment. */ -static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step) -{ - CTSize ofs = 0; - MSize mlp = 0; - IRType tp = IRT_U8 + 2*lj_fls(step); - do { - while (ofs + step <= len) { - if (mlp >= CREC_COPY_MAXUNROLL) return 0; - ml[mlp].ofs = ofs; - ml[mlp].tp = tp; - mlp++; - ofs += step; - } - step >>= 1; - tp -= 2; - } while (ofs < len); - return mlp; -} - -/* -** Emit stores for fill list. -** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such). -*/ -static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp, - TRef trdst, TRef trfill) -{ - MSize i; - for (i = 0; i < mlp; i++) { - TRef trofs = lj_ir_kintp(J, ml[i].ofs); - TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs); - emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill); - } -} - -/* Optimized memory fill. */ -static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill, - CTSize step) -{ - if (tref_isk(trlen)) { /* Length must be constant. */ - CRecMemList ml[CREC_FILL_MAXUNROLL]; - MSize mlp; - CTSize len = (CTSize)IR(tref_ref(trlen))->i; - if (len == 0) return; /* Shortcut. */ - if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR) - step = CTSIZE_PTR; - if (step * CREC_FILL_MAXUNROLL < len) goto fallback; - mlp = crec_fill_unroll(ml, len, step); - if (!mlp) goto fallback; - if (tref_isk(trfill) || ml[0].tp != IRT_U8) - trfill = emitconv(trfill, IRT_INT, IRT_U8, 0); - if (ml[0].tp != IRT_U8) { /* Scatter U8 to U16/U32/U64. */ - if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) { - if (tref_isk(trfill)) /* Pointless on x64 with zero-extended regs. */ - trfill = emitconv(trfill, IRT_U64, IRT_U32, 0); - trfill = emitir(IRT(IR_MUL, IRT_U64), trfill, - lj_ir_kint64(J, U64x(01010101,01010101))); - } else { - trfill = emitir(IRTI(IR_MUL), trfill, - lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101)); - } - } - crec_fill_emit(J, ml, mlp, trdst, trfill); - } else { -fallback: - /* Call memset. Always needs a barrier to disable alias analysis. */ - lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen); /* Note: arg order! */ - } - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); -} - -/* -- Convert C type to C type -------------------------------------------- */ - -/* -** This code mirrors the code in lj_cconv.c. It performs the same steps -** for the trace recorder that lj_cconv.c does for the interpreter. -** -** One major difference is that we can get away with much fewer checks -** here. E.g. checks for casts, constness or correct types can often be -** omitted, even if they might fail. The interpreter subsequently throws -** an error, which aborts the trace. -** -** All operations are specialized to their C types, so the on-trace -** outcome must be the same as the outcome in the interpreter. If the -** interpreter doesn't throw an error, then the trace is correct, too. -** Care must be taken not to generate invalid (temporary) IR or to -** trigger asserts. -*/ - -/* Determine whether a passed number or cdata number is non-zero. */ -static int crec_isnonzero(CType *s, void *p) -{ - if (p == (void *)0) - return 0; - if (p == (void *)1) - return 1; - if ((s->info & CTF_FP)) { - if (s->size == sizeof(float)) - return (*(float *)p != 0); - else - return (*(double *)p != 0); - } else { - if (s->size == 1) - return (*(uint8_t *)p != 0); - else if (s->size == 2) - return (*(uint16_t *)p != 0); - else if (s->size == 4) - return (*(uint32_t *)p != 0); - else - return (*(uint64_t *)p != 0); - } -} - -static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp, - void *svisnz) -{ - IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d); - IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s); - CTSize dsize = d->size, ssize = s->size; - CTInfo dinfo = d->info, sinfo = s->info; - - if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT) - goto err_conv; - - /* - ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and - ** numbers up to 8 bytes. Otherwise sp holds a pointer. - */ - - switch (cconv_idx2(dinfo, sinfo)) { - /* Destination is a bool. */ - case CCX(B, B): - goto xstore; /* Source operand is already normalized. */ - case CCX(B, I): - case CCX(B, F): - if (st != IRT_CDATA) { - /* Specialize to the result of a comparison against 0. */ - TRef zero = (st == IRT_NUM || st == IRT_FLOAT) ? lj_ir_knum(J, 0) : - (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) : - lj_ir_kint(J, 0); - int isnz = crec_isnonzero(s, svisnz); - emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero); - sp = lj_ir_kint(J, isnz); - goto xstore; - } - goto err_nyi; - - /* Destination is an integer. */ - case CCX(I, B): - case CCX(I, I): - conv_I_I: - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - /* Extend 32 to 64 bit integer. */ - if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED))) - sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, - (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT); - else if (dsize < 8 && ssize == 8) /* Truncate from 64 bit integer. */ - sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0); - else if (st == IRT_INT) - sp = lj_opt_narrow_toint(J, sp); - xstore: - if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J); - if (dp == 0) return sp; - emitir(IRT(IR_XSTORE, dt), dp, sp); - break; - case CCX(I, C): - sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */ - /* fallthrough */ - case CCX(I, F): - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_ANY); - goto xstore; - case CCX(I, P): - case CCX(I, A): - sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); - ssize = CTSIZE_PTR; - st = IRT_UINTP; - if (((dsize ^ ssize) & 8) == 0) { /* Must insert no-op type conversion. */ - sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0); - goto xstore; - } - goto conv_I_I; - - /* Destination is a floating-point number. */ - case CCX(F, B): - case CCX(F, I): - conv_F_I: - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0); - goto xstore; - case CCX(F, C): - sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */ - /* fallthrough */ - case CCX(F, F): - conv_F_F: - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - if (dt != st) sp = emitconv(sp, dt, st, 0); - goto xstore; - - /* Destination is a complex number. */ - case CCX(C, I): - case CCX(C, F): - { /* Clear im. */ - TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1))); - emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0)); - } - /* Convert to re. */ - if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I; - - case CCX(C, C): - if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; - { - TRef re, im, ptr; - re = emitir(IRT(IR_XLOAD, st), sp, 0); - ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1))); - im = emitir(IRT(IR_XLOAD, st), ptr, 0); - if (dt != st) { - re = emitconv(re, dt, st, 0); - im = emitconv(im, dt, st, 0); - } - emitir(IRT(IR_XSTORE, dt), dp, re); - ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1))); - emitir(IRT(IR_XSTORE, dt), ptr, im); - } - break; - - /* Destination is a vector. */ - case CCX(V, I): - case CCX(V, F): - case CCX(V, C): - case CCX(V, V): - goto err_nyi; - - /* Destination is a pointer. */ - case CCX(P, P): - case CCX(P, A): - case CCX(P, S): - /* There are only 32 bit pointers/addresses on 32 bit machines. - ** Also ok on x64, since all 32 bit ops clear the upper part of the reg. - */ - goto xstore; - case CCX(P, I): - if (st == IRT_CDATA) goto err_nyi; - if (!LJ_64 && ssize == 8) /* Truncate from 64 bit integer. */ - sp = emitconv(sp, IRT_U32, st, 0); - goto xstore; - case CCX(P, F): - if (st == IRT_CDATA) goto err_nyi; - /* The signed conversion is cheaper. x64 really has 47 bit pointers. */ - sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32, - st, IRCONV_ANY); - goto xstore; - - /* Destination is an array. */ - case CCX(A, A): - /* Destination is a struct/union. */ - case CCX(S, S): - if (dp == 0) goto err_conv; - crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d); - break; - - default: - err_conv: - err_nyi: - lj_trace_err(J, LJ_TRERR_NYICONV); - break; - } - return 0; -} - -/* -- Convert C type to TValue (load) ------------------------------------- */ - -static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp) -{ - CTState *cts = ctype_ctsG(J2G(J)); - IRType t = crec_ct2irt(cts, s); - CTInfo sinfo = s->info; - if (ctype_isnum(sinfo)) { - TRef tr; - if (t == IRT_CDATA) - goto err_nyi; /* NYI: copyval of >64 bit integers. */ - tr = emitir(IRT(IR_XLOAD, t), sp, 0); - if (t == IRT_FLOAT || t == IRT_U32) { /* Keep uint32_t/float as numbers. */ - return emitconv(tr, IRT_NUM, t, 0); - } else if (t == IRT_I64 || t == IRT_U64) { /* Box 64 bit integer. */ - sp = tr; - lj_needsplit(J); - } else if ((sinfo & CTF_BOOL)) { - /* Assume not equal to zero. Fixup and emit pending guard later. */ - lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } else { - return tr; - } - } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) { - sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Box pointers and enums. */ - } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) { - cts->L = J->L; - sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR); /* Create ref. */ - } else if (ctype_iscomplex(sinfo)) { /* Unbox/box complex. */ - ptrdiff_t esz = (ptrdiff_t)(s->size >> 1); - TRef ptr, tr1, tr2, dp; - dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL); - tr1 = emitir(IRT(IR_XLOAD, t), sp, 0); - ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz)); - tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0); - ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata))); - emitir(IRT(IR_XSTORE, t), ptr, tr1); - ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz)); - emitir(IRT(IR_XSTORE, t), ptr, tr2); - return dp; - } else { - /* NYI: copyval of vectors. */ - err_nyi: - lj_trace_err(J, LJ_TRERR_NYICONV); - } - /* Box pointer, ref, enum or 64 bit integer. */ - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp); -} - -/* -- Convert TValue to C type (store) ------------------------------------ */ - -static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID sid = CTID_P_VOID; - void *svisnz = 0; - CType *s; - if (LJ_LIKELY(tref_isinteger(sp))) { - sid = CTID_INT32; - svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval)); - } else if (tref_isnum(sp)) { - sid = CTID_DOUBLE; - svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval)); - } else if (tref_isbool(sp)) { - sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0); - sid = CTID_BOOL; - } else if (tref_isnil(sp)) { - sp = lj_ir_kptr(J, NULL); - } else if (tref_isudata(sp)) { - GCudata *ud = udataV(sval); - if (ud->udtype == UDTYPE_IO_FILE || ud->udtype == UDTYPE_BUFFER) { - TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, ud->udtype)); - sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, - ud->udtype == UDTYPE_IO_FILE ? IRFL_UDATA_FILE : - IRFL_SBUF_R); - } else { - sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata))); - } - } else if (tref_isstr(sp)) { - if (ctype_isenum(d->info)) { /* Match string against enum constant. */ - GCstr *str = strV(sval); - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, d, str, &ofs); - /* Specialize to the name of the enum constant. */ - emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str)); - if (cct && ctype_isconstval(cct->info)) { - lj_assertJ(ctype_child(cts, cct)->size == 4, - "only 32 bit const supported"); /* NYI */ - svisnz = (void *)(intptr_t)(ofs != 0); - sp = lj_ir_kint(J, (int32_t)ofs); - sid = ctype_cid(cct->info); - } /* else: interpreter will throw. */ - } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); /* NYI */ - } else { /* Otherwise pass the string data as a const char[]. */ - /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */ - sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr))); - sid = CTID_A_CCHAR; - } - } else if (tref_islightud(sp)) { -#if LJ_64 - lj_trace_err(J, LJ_TRERR_NYICONV); -#endif - } else { /* NYI: tref_istab(sp). */ - IRType t; - sid = argv2cdata(J, sp, sval)->ctypeid; - s = ctype_raw(cts, sid); - svisnz = cdataptr(cdataV(sval)); - if (ctype_isfunc(s->info)) { - sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR); - s = ctype_get(cts, sid); - t = IRT_PTR; - } else { - t = crec_ct2irt(cts, s); - } - if (ctype_isptr(s->info)) { - sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR); - if (ctype_isref(s->info)) { - svisnz = *(void **)svisnz; - s = ctype_rawchild(cts, s); - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - t = crec_ct2irt(cts, s); - } else { - goto doconv; - } - } else if (t == IRT_I64 || t == IRT_U64) { - sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64); - lj_needsplit(J); - goto doconv; - } else if (t == IRT_INT || t == IRT_U32) { - if (ctype_isenum(s->info)) s = ctype_child(cts, s); - sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT); - goto doconv; - } else { - sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata))); - } - if (ctype_isnum(s->info) && t != IRT_CDATA) - sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Load number value. */ - goto doconv; - } - s = ctype_get(cts, sid); -doconv: - if (ctype_isenum(d->info)) d = ctype_child(cts, d); - return crec_ct_ct(J, d, s, dp, sp, svisnz); -} - -/* -- C data metamethods -------------------------------------------------- */ - -/* This would be rather difficult in FOLD, so do it here: -** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k) -** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz) -*/ -static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz) -{ - IRIns *ir = IR(tref_ref(tr)); - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) && - (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) { - IRIns *irk = IR(ir->op2); - ptrdiff_t k; - if (LJ_64 && irk->o == IR_KINT64) - k = (ptrdiff_t)ir_kint64(irk)->u64 * sz; - else - k = (ptrdiff_t)irk->i * sz; - if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k; - tr = ir->op1; /* Not a TRef, but the caller doesn't care. */ - } - return tr; -} - -/* Tailcall to function. */ -static void crec_tailcall(jit_State *J, RecordFFData *rd, cTValue *tv) -{ - TRef kfunc = lj_ir_kfunc(J, funcV(tv)); -#if LJ_FR2 - J->base[-2] = kfunc; - J->base[-1] = TREF_FRAME; -#else - J->base[-1] = kfunc | TREF_FRAME; -#endif - rd->nres = -1; /* Pending tailcall. */ -} - -/* Record ctype __index/__newindex metamethods. */ -static void crec_index_meta(jit_State *J, CTState *cts, CType *ct, - RecordFFData *rd) -{ - CTypeID id = ctype_typeid(cts, ct); - cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index); - if (!tv) - lj_trace_err(J, LJ_TRERR_BADTYPE); - if (tvisfunc(tv)) { - crec_tailcall(J, rd, tv); - } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) { - /* Specialize to result of __index lookup. */ - cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]); - J->base[0] = lj_record_constify(J, o); - if (!J->base[0]) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* Always specialize to the key. */ - emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1]))); - } else { - /* NYI: resolving of non-function metamethods. */ - /* NYI: non-string keys for __index table. */ - /* NYI: stores to __newindex table. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); - } -} - -/* Record bitfield load/store. */ -static void crec_index_bf(jit_State *J, RecordFFData *rd, TRef ptr, CTInfo info) -{ - IRType t = IRT_I8 + 2*lj_fls(ctype_bitcsz(info)) + ((info&CTF_UNSIGNED)?1:0); - TRef tr = emitir(IRT(IR_XLOAD, t), ptr, 0); - CTSize pos = ctype_bitpos(info), bsz = ctype_bitbsz(info), shift = 32 - bsz; - lj_assertJ(t <= IRT_U32, "only 32 bit bitfields supported"); /* NYI */ - if (rd->data == 0) { /* __index metamethod. */ - if ((info & CTF_BOOL)) { - tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << pos)))); - /* Assume not equal to zero. Fixup and emit pending guard later. */ - lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); - J->postproc = LJ_POST_FIXGUARD; - tr = TREF_TRUE; - } else if (!(info & CTF_UNSIGNED)) { - tr = emitir(IRTI(IR_BSHL), tr, lj_ir_kint(J, shift - pos)); - tr = emitir(IRTI(IR_BSAR), tr, lj_ir_kint(J, shift)); - } else { - lj_assertJ(bsz < 32, "unexpected full bitfield index"); - tr = emitir(IRTI(IR_BSHR), tr, lj_ir_kint(J, pos)); - tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << bsz)-1))); - /* We can omit the U32 to NUM conversion, since bsz < 32. */ - } - J->base[0] = tr; - } else { /* __newindex metamethod. */ - CTState *cts = ctype_ctsG(J2G(J)); - CType *ct = ctype_get(cts, - (info & CTF_BOOL) ? CTID_BOOL : - (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32); - int32_t mask = (int32_t)(((1u << bsz)-1) << pos); - TRef sp = crec_ct_tv(J, ct, 0, J->base[2], &rd->argv[2]); - sp = emitir(IRTI(IR_BSHL), sp, lj_ir_kint(J, pos)); - /* Use of the target type avoids forwarding conversions. */ - sp = emitir(IRT(IR_BAND, t), sp, lj_ir_kint(J, mask)); - tr = emitir(IRT(IR_BAND, t), tr, lj_ir_kint(J, (int32_t)~mask)); - tr = emitir(IRT(IR_BOR, t), tr, sp); - emitir(IRT(IR_XSTORE, t), ptr, tr); - rd->nres = 0; - J->needsnap = 1; - } -} - -void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd) -{ - TRef idx, ptr = J->base[0]; - ptrdiff_t ofs = sizeof(GCcdata); - GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]); - CTState *cts = ctype_ctsG(J2G(J)); - CType *ct = ctype_raw(cts, cd->ctypeid); - CTypeID sid = 0; - - /* Resolve pointer or reference for cdata object. */ - if (ctype_isptr(ct->info)) { - IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; - if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); - ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR); - ofs = 0; - ptr = crec_reassoc_ofs(J, ptr, &ofs, 1); - } - -again: - idx = J->base[1]; - if (tref_isnumber(idx)) { - idx = lj_opt_narrow_cindex(J, idx); - if (ctype_ispointer(ct->info)) { - CTSize sz; - integer_key: - if ((ct->info & CTF_COMPLEX)) - idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1)); - sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info))); - idx = crec_reassoc_ofs(J, idx, &ofs, sz); -#if LJ_TARGET_ARM || LJ_TARGET_PPC - /* Hoist base add to allow fusion of index/shift into operands. */ - if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs -#if LJ_TARGET_ARM - && (sz == 1 || sz == 4) -#endif - ) { - ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); - ofs = 0; - } -#endif - idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz)); - ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr); - } - } else if (tref_iscdata(idx)) { - GCcdata *cdk = cdataV(&rd->argv[1]); - CType *ctk = ctype_raw(cts, cdk->ctypeid); - IRType t = crec_ct2irt(cts, ctk); - if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) { - if (ctk->size == 8) { - idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64); - } else if (ctk->size == 4) { - idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT); - } else { - idx = emitir(IRT(IR_ADD, IRT_PTR), idx, - lj_ir_kintp(J, sizeof(GCcdata))); - idx = emitir(IRT(IR_XLOAD, t), idx, 0); - } - if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED)) - idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT); - if (!LJ_64 && ctk->size > sizeof(intptr_t)) { - idx = emitconv(idx, IRT_INTP, t, 0); - lj_needsplit(J); - } - goto integer_key; - } - } else if (tref_isstr(idx)) { - GCstr *name = strV(&rd->argv[1]); - if (cd && cd->ctypeid == CTID_CTYPEID) - ct = ctype_raw(cts, crec_constructor(J, cd, ptr)); - if (ctype_isstruct(ct->info)) { - CTSize fofs; - CType *fct; - fct = lj_ctype_getfield(cts, ct, name, &fofs); - if (fct) { - ofs += (ptrdiff_t)fofs; - /* Always specialize to the field name. */ - emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name)); - if (ctype_isconstval(fct->info)) { - if (fct->size >= 0x80000000u && - (ctype_child(cts, fct)->info & CTF_UNSIGNED)) { - J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size); - return; - } - J->base[0] = lj_ir_kint(J, (int32_t)fct->size); - return; /* Interpreter will throw for newindex. */ - } else if (ctype_isbitfield(fct->info)) { - if (ofs) - ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); - crec_index_bf(J, rd, ptr, fct->info); - return; - } else { - lj_assertJ(ctype_isfield(fct->info), "field expected"); - sid = ctype_cid(fct->info); - } - } - } else if (ctype_iscomplex(ct->info)) { - if (name->len == 2 && - ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') || - (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) { - /* Always specialize to the field name. */ - emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name)); - if (strdata(name)[0] == 'i') ofs += (ct->size >> 1); - sid = ctype_cid(ct->info); - } - } - } - if (!sid) { - if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ - CType *cct = ctype_rawchild(cts, ct); - if (ctype_isstruct(cct->info)) { - ct = cct; - cd = NULL; - if (tref_isstr(idx)) goto again; - } - } - crec_index_meta(J, cts, ct, rd); - return; - } - - if (ofs) - ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); - - /* Resolve reference for field. */ - ct = ctype_get(cts, sid); - if (ctype_isref(ct->info)) { - ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0); - sid = ctype_cid(ct->info); - ct = ctype_get(cts, sid); - } - - while (ctype_isattrib(ct->info)) - ct = ctype_child(cts, ct); /* Skip attributes. */ - - if (rd->data == 0) { /* __index metamethod. */ - J->base[0] = crec_tv_ct(J, ct, sid, ptr); - } else { /* __newindex metamethod. */ - rd->nres = 0; - J->needsnap = 1; - crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]); - } -} - -/* Record setting a finalizer. */ -static void crec_finalizer(jit_State *J, TRef trcd, TRef trfin, cTValue *fin) -{ - if (tvisgcv(fin)) { - if (!trfin) trfin = lj_ir_kptr(J, gcval(fin)); - } else if (tvisnil(fin)) { - trfin = lj_ir_kptr(J, NULL); - } else { - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd, - trfin, lj_ir_kint(J, (int32_t)itype(fin))); - J->needsnap = 1; -} - -/* Record cdata allocation. */ -static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - CType *d = ctype_raw(cts, id); - TRef trcd, trid = lj_ir_kint(J, id); - cTValue *fin; - /* Use special instruction to box pointer or 32/64 bit integer. */ - if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) { - TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) : - ctype_isptr(info) ? lj_ir_kptr(J, NULL) : - sz == 4 ? lj_ir_kint(J, 0) : - (lj_needsplit(J), lj_ir_kint64(J, 0)); - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp); - return; - } else { - TRef trsz = TREF_NIL; - if ((info & CTF_VLA)) { /* Calculate VLA/VLS size at runtime. */ - CTSize sz0, sz1; - if (!J->base[1] || J->base[2]) - lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init VLA/VLS. */ - trsz = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, - J->base[1], &rd->argv[1]); - sz0 = lj_ctype_vlsize(cts, d, 0); - sz1 = lj_ctype_vlsize(cts, d, 1); - trsz = emitir(IRTGI(IR_MULOV), trsz, lj_ir_kint(J, (int32_t)(sz1-sz0))); - trsz = emitir(IRTGI(IR_ADDOV), trsz, lj_ir_kint(J, (int32_t)sz0)); - J->base[1] = 0; /* Simplify logic below. */ - } else if (ctype_align(info) > CT_MEMALIGN) { - trsz = lj_ir_kint(J, sz); - } - trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, trsz); - if (sz > 128 || (info & CTF_VLA)) { - TRef dp; - CTSize align; - special: /* Only handle bulk zero-fill for large/VLA/VLS types. */ - if (J->base[1]) - lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init large/VLA/VLS types. */ - dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata))); - if (trsz == TREF_NIL) trsz = lj_ir_kint(J, sz); - align = ctype_align(info); - if (align < CT_MEMALIGN) align = CT_MEMALIGN; - crec_fill(J, dp, trsz, lj_ir_kint(J, 0), (1u << align)); - } else if (J->base[1] && !J->base[2] && - !lj_cconv_multi_init(cts, d, &rd->argv[1])) { - goto single_init; - } else if (ctype_isarray(d->info)) { - CType *dc = ctype_rawchild(cts, d); /* Array element type. */ - CTSize ofs, esize = dc->size; - TRef sp = 0; - TValue tv; - TValue *sval = &tv; - MSize i; - tv.u64 = 0; - if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)) || - esize * CREC_FILL_MAXUNROLL < sz) - goto special; - for (i = 1, ofs = 0; ofs < sz; ofs += esize) { - TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, - lj_ir_kintp(J, ofs + sizeof(GCcdata))); - if (J->base[i]) { - sp = J->base[i]; - sval = &rd->argv[i]; - i++; - } else if (i != 2) { - sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL; - } - crec_ct_tv(J, dc, dp, sp, sval); - } - } else if (ctype_isstruct(d->info)) { - CTypeID fid; - MSize i = 1; - if (!J->base[1]) { /* Handle zero-fill of struct-of-NYI. */ - fid = d->sib; - while (fid) { - CType *df = ctype_get(cts, fid); - fid = df->sib; - if (ctype_isfield(df->info)) { - CType *dc; - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - dc = ctype_rawchild(cts, df); /* Field type. */ - if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) || - ctype_isenum(dc->info))) - goto special; - } else if (!ctype_isconstval(df->info)) { - goto special; - } - } - } - fid = d->sib; - while (fid) { - CType *df = ctype_get(cts, fid); - fid = df->sib; - if (ctype_isfield(df->info)) { - CType *dc; - TRef sp, dp; - TValue tv; - TValue *sval = &tv; - setintV(&tv, 0); - if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ - dc = ctype_rawchild(cts, df); /* Field type. */ - if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) || - ctype_isenum(dc->info))) - lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */ - if (J->base[i]) { - sp = J->base[i]; - sval = &rd->argv[i]; - i++; - } else { - sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0); - } - dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, - lj_ir_kintp(J, df->size + sizeof(GCcdata))); - crec_ct_tv(J, dc, dp, sp, sval); - if ((d->info & CTF_UNION)) { - if (d->size != dc->size) /* NYI: partial init of union. */ - lj_trace_err(J, LJ_TRERR_NYICONV); - break; - } - } else if (!ctype_isconstval(df->info)) { - /* NYI: init bitfields and sub-structures. */ - lj_trace_err(J, LJ_TRERR_NYICONV); - } - } - } else { - TRef dp; - single_init: - dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata))); - if (J->base[1]) { - crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]); - } else { - TValue tv; - tv.u64 = 0; - crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv); - } - } - } - J->base[0] = trcd; - /* Handle __gc metamethod. */ - fin = lj_ctype_meta(cts, id, MM_gc); - if (fin) - crec_finalizer(J, trcd, 0, fin); -} - -/* Record argument conversions. */ -static TRef crec_call_args(jit_State *J, RecordFFData *rd, - CTState *cts, CType *ct) -{ - TRef args[CCI_NARGS_MAX]; - CTypeID fid; - MSize i, n; - TRef tr, *base; - cTValue *o; -#if LJ_TARGET_X86 -#if LJ_ABI_WIN - TRef *arg0 = NULL, *arg1 = NULL; -#endif - int ngpr = 0; - if (ctype_cconv(ct->info) == CTCC_THISCALL) - ngpr = 1; - else if (ctype_cconv(ct->info) == CTCC_FASTCALL) - ngpr = 2; -#endif - - /* Skip initial attributes. */ - fid = ct->sib; - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (!ctype_isattrib(ctf->info)) break; - fid = ctf->sib; - } - args[0] = TREF_NIL; - for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) { - CTypeID did; - CType *d; - - if (n >= CCI_NARGS_MAX) - lj_trace_err(J, LJ_TRERR_NYICALL); - - if (fid) { /* Get argument type from field. */ - CType *ctf = ctype_get(cts, fid); - fid = ctf->sib; - lj_assertJ(ctype_isfield(ctf->info), "field expected"); - did = ctype_cid(ctf->info); - } else { - if (!(ct->info & CTF_VARARG)) - lj_trace_err(J, LJ_TRERR_NYICALL); /* Too many arguments. */ - did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */ - } - d = ctype_raw(cts, did); - if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || - ctype_isenum(d->info))) - lj_trace_err(J, LJ_TRERR_NYICALL); - tr = crec_ct_tv(J, d, 0, *base, o); - if (ctype_isinteger_or_bool(d->info)) { - if (d->size < 4) { - if ((d->info & CTF_UNSIGNED)) - tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0); - else - tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT); - } - } else if (LJ_SOFTFP32 && ctype_isfp(d->info) && d->size > 4) { - lj_needsplit(J); - } -#if LJ_TARGET_X86 - /* 64 bit args must not end up in registers for fastcall/thiscall. */ -#if LJ_ABI_WIN - if (!ctype_isfp(d->info)) { - /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */ - if (tref_typerange(tr, IRT_I64, IRT_U64)) { - if (ngpr) { - arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--; - if (ngpr) { - arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--; - } - } - } else { - if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; } - if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; } - if (ngpr) ngpr--; - } - } -#else - if (!ctype_isfp(d->info) && ngpr) { - if (tref_typerange(tr, IRT_I64, IRT_U64)) { - /* No reordering for other x86 ABIs. Simply add alignment args. */ - do { args[n++] = TREF_NIL; } while (--ngpr); - } else { - ngpr--; - } - } -#endif -#endif - args[n] = tr; - } - tr = args[0]; - for (i = 1; i < n; i++) - tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]); - return tr; -} - -/* Create a snapshot for the caller, simulating a 'false' return value. */ -static void crec_snap_caller(jit_State *J) -{ - lua_State *L = J->L; - TValue *base = L->base, *top = L->top; - const BCIns *pc = J->pc; - TRef ftr = J->base[-1-LJ_FR2]; - ptrdiff_t delta; - if (!frame_islua(base-1) || J->framedepth <= 0) - lj_trace_err(J, LJ_TRERR_NYICALL); - J->pc = frame_pc(base-1); delta = 1+LJ_FR2+bc_a(J->pc[-1]); - L->top = base; L->base = base - delta; - J->base[-1-LJ_FR2] = TREF_FALSE; - J->base -= delta; J->baseslot -= (BCReg)delta; - J->maxslot = (BCReg)delta-LJ_FR2; J->framedepth--; - lj_snap_add(J); - L->base = base; L->top = top; - J->framedepth++; J->maxslot = 1; - J->base += delta; J->baseslot += (BCReg)delta; - J->base[-1-LJ_FR2] = ftr; J->pc = pc; -} - -/* Record function call. */ -static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CType *ct = ctype_raw(cts, cd->ctypeid); - IRType tp = IRT_PTR; - if (ctype_isptr(ct->info)) { - tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; - ct = ctype_rawchild(cts, ct); - } - if (ctype_isfunc(ct->info)) { - TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR); - CType *ctr = ctype_rawchild(cts, ct); - IRType t = crec_ct2irt(cts, ctr); - TRef tr; - TValue tv; - /* Check for blacklisted C functions that might call a callback. */ - tv.u64 = ((uintptr_t)cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4) >> 2) | U64x(800000000, 00000000); - if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv))) - lj_trace_err(J, LJ_TRERR_BLACKL); - if (ctype_isvoid(ctr->info)) { - t = IRT_NIL; - rd->nres = 0; - } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) || - ctype_isenum(ctr->info)) || t == IRT_CDATA) { - lj_trace_err(J, LJ_TRERR_NYICALL); - } - if ((ct->info & CTF_VARARG) -#if LJ_TARGET_X86 - || ctype_cconv(ct->info) != CTCC_CDECL -#endif - ) - func = emitir(IRT(IR_CARG, IRT_NIL), func, - lj_ir_kint(J, ctype_typeid(cts, ct))); - tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func); - if (ctype_isbool(ctr->info)) { - if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) { - /* Don't check result if ignored. */ - tr = TREF_NIL; - } else { - crec_snap_caller(J); -#if LJ_TARGET_X86ORX64 - /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */ - lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0)); -#else - lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); -#endif - J->postproc = LJ_POST_FIXGUARDSNAP; - tr = TREF_TRUE; - } - } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) || - t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) { - TRef trid = lj_ir_kint(J, ctype_cid(ct->info)); - tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr); - if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J); - } else if (t == IRT_FLOAT || t == IRT_U32) { - tr = emitconv(tr, IRT_NUM, t, 0); - } else if (t == IRT_I8 || t == IRT_I16) { - tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT); - } else if (t == IRT_U8 || t == IRT_U16) { - tr = emitconv(tr, IRT_INT, t, 0); - } - J->base[0] = tr; - J->needsnap = 1; - return 1; - } - return 0; -} - -void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]); - CTypeID id = cd->ctypeid; - CType *ct; - cTValue *tv; - MMS mm = MM_call; - if (id == CTID_CTYPEID) { - id = crec_constructor(J, cd, J->base[0]); - mm = MM_new; - } else if (crec_call(J, rd, cd)) { - return; - } - /* Record ctype __call/__new metamethod. */ - ct = ctype_raw(cts, id); - tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm); - if (tv) { - if (tvisfunc(tv)) { - crec_tailcall(J, rd, tv); - return; - } - } else if (mm == MM_new) { - crec_alloc(J, rd, id); - return; - } - /* No metamethod or NYI: non-function metamethods. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); -} - -static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm) -{ - if (sp[0] && sp[1] && ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) { - IRType dt; - CTypeID id; - TRef tr; - MSize i; - IROp op; - lj_needsplit(J); - if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) || - ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) { - dt = IRT_U64; id = CTID_UINT64; - } else { - dt = IRT_I64; id = CTID_INT64; - if (mm < MM_add && - !((s[0]->info | s[1]->info) & CTF_FP) && - s[0]->size == 4 && s[1]->size == 4) { /* Try to narrow comparison. */ - if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) || - (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) { - dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT; - goto comp; - } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) { - dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT; - goto comp; - } - } - } - for (i = 0; i < 2; i++) { - IRType st = tref_type(sp[i]); - if (st == IRT_NUM || st == IRT_FLOAT) - sp[i] = emitconv(sp[i], dt, st, IRCONV_ANY); - else if (!(st == IRT_I64 || st == IRT_U64)) - sp[i] = emitconv(sp[i], dt, IRT_INT, - (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT); - } - if (mm < MM_add) { - comp: - /* Assume true comparison. Fixup and emit pending guard later. */ - if (mm == MM_eq) { - op = IR_EQ; - } else { - op = mm == MM_lt ? IR_LT : IR_LE; - if (dt == IRT_U32 || dt == IRT_U64) - op += (IR_ULT-IR_LT); - } - lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } else { - tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]); - } - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - } - return 0; -} - -static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CType *ctp = s[0]; - if (!(sp[0] && sp[1])) return 0; - if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) { - if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) && - (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) { - if (mm == MM_sub) { /* Pointer difference. */ - TRef tr; - CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info)); - if (sz == 0 || (sz & (sz-1)) != 0) - return 0; /* NYI: integer division. */ - tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]); - tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz))); -#if LJ_64 - tr = emitconv(tr, IRT_NUM, IRT_INTP, 0); -#endif - return tr; - } else { /* Pointer comparison (unsigned). */ - /* Assume true comparison. Fixup and emit pending guard later. */ - IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE; - lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } - } - if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info))) - return 0; - } else if (mm == MM_add && ctype_isnum(ctp->info) && - (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) { - TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr; /* Swap pointer and index. */ - ctp = s[1]; - } else { - return 0; - } - { - TRef tr = sp[1]; - IRType t = tref_type(tr); - CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info)); - CTypeID id; -#if LJ_64 - if (t == IRT_NUM || t == IRT_FLOAT) - tr = emitconv(tr, IRT_INTP, t, IRCONV_ANY); - else if (!(t == IRT_I64 || t == IRT_U64)) - tr = emitconv(tr, IRT_INTP, IRT_INT, - ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT); -#else - if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) { - tr = emitconv(tr, IRT_INTP, t, - (t == IRT_NUM || t == IRT_FLOAT) ? IRCONV_ANY : 0); - } -#endif - tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz)); - tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr); - id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), - CTSIZE_PTR); - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - } -} - -/* Record ctype arithmetic metamethods. */ -static TRef crec_arith_meta(jit_State *J, TRef *sp, CType **s, CTState *cts, - RecordFFData *rd) -{ - cTValue *tv = NULL; - if (J->base[0]) { - if (tviscdata(&rd->argv[0])) { - CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, (MMS)rd->data); - } - if (!tv && J->base[1] && tviscdata(&rd->argv[1])) { - CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid; - CType *ct = ctype_raw(cts, id); - if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); - tv = lj_ctype_meta(cts, id, (MMS)rd->data); - } - } - if (tv) { - if (tvisfunc(tv)) { - crec_tailcall(J, rd, tv); - return 0; - } /* NYI: non-function metamethods. */ - } else if ((MMS)rd->data == MM_eq) { /* Fallback cdata pointer comparison. */ - if (sp[0] && sp[1] && ctype_isnum(s[0]->info) == ctype_isnum(s[1]->info)) { - /* Assume true comparison. Fixup and emit pending guard later. */ - lj_ir_set(J, IRTG(IR_EQ, IRT_PTR), sp[0], sp[1]); - J->postproc = LJ_POST_FIXGUARD; - return TREF_TRUE; - } else { - return TREF_FALSE; - } - } - lj_trace_err(J, LJ_TRERR_BADTYPE); - return 0; -} - -void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef sp[2]; - CType *s[2]; - MSize i; - for (i = 0; i < 2; i++) { - TRef tr = J->base[i]; - CType *ct = ctype_get(cts, CTID_DOUBLE); - if (!tr) { - lj_trace_err(J, LJ_TRERR_BADTYPE); - } else if (tref_iscdata(tr)) { - CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid; - IRType t; - ct = ctype_raw(cts, id); - t = crec_ct2irt(cts, ct); - if (ctype_isptr(ct->info)) { /* Resolve pointer or reference. */ - tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR); - if (ctype_isref(ct->info)) { - ct = ctype_rawchild(cts, ct); - t = crec_ct2irt(cts, ct); - } - } else if (t == IRT_I64 || t == IRT_U64) { - tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64); - lj_needsplit(J); - goto ok; - } else if (t == IRT_INT || t == IRT_U32) { - tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - goto ok; - } else if (ctype_isfunc(ct->info)) { - CTypeID id0 = i ? ctype_typeid(cts, s[0]) : 0; - tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR); - ct = ctype_get(cts, - lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); - if (i) { - s[0] = ctype_get(cts, id0); /* cts->tab may have been reallocated. */ - } - goto ok; - } else { - tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata))); - } - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (ctype_isnum(ct->info)) { - if (t == IRT_CDATA) { - tr = 0; - } else { - if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J); - tr = emitir(IRT(IR_XLOAD, t), tr, 0); - } - } - } else if (tref_isnil(tr)) { - tr = lj_ir_kptr(J, NULL); - ct = ctype_get(cts, CTID_P_VOID); - } else if (tref_isinteger(tr)) { - ct = ctype_get(cts, CTID_INT32); - } else if (tref_isstr(tr)) { - TRef tr2 = J->base[1-i]; - CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid; - ct = ctype_raw(cts, id); - if (ctype_isenum(ct->info)) { /* Match string against enum constant. */ - GCstr *str = strV(&rd->argv[i]); - CTSize ofs; - CType *cct = lj_ctype_getfield(cts, ct, str, &ofs); - if (cct && ctype_isconstval(cct->info)) { - /* Specialize to the name of the enum constant. */ - emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str)); - ct = ctype_child(cts, cct); - tr = lj_ir_kint(J, (int32_t)ofs); - } else { /* Interpreter will throw or return false. */ - ct = ctype_get(cts, CTID_P_VOID); - } - } else if (ctype_isptr(ct->info)) { - tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCstr))); - } else { - ct = ctype_get(cts, CTID_P_VOID); - } - } else if (!tref_isnum(tr)) { - tr = 0; - ct = ctype_get(cts, CTID_P_VOID); - } - ok: - s[i] = ct; - sp[i] = tr; - } - { - TRef tr; - MMS mm = (MMS)rd->data; - if ((mm == MM_len || mm == MM_concat || - (!(tr = crec_arith_int64(J, sp, s, mm)) && - !(tr = crec_arith_ptr(J, sp, s, mm)))) && - !(tr = crec_arith_meta(J, sp, s, cts, rd))) - return; - J->base[0] = tr; - /* Fixup cdata comparisons, too. Avoids some cdata escapes. */ - if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) && - !irt_isguard(J->guardemit)) { - const BCIns *pc = frame_contpc(J->L->base-1) - 1; - if (bc_op(*pc) <= BC_ISNEP) { - J2G(J)->tmptv.u64 = (uint64_t)(uintptr_t)pc; - J->postproc = LJ_POST_FIXCOMP; - } - } - } -} - -/* -- C library namespace metamethods ------------------------------------- */ - -void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) && - udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) { - CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0])); - GCstr *name = strV(&rd->argv[1]); - CType *ct; - CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX); - cTValue *tv = lj_tab_getstr(cl->cache, name); - rd->nres = rd->data; - if (id && tv && !tvisnil(tv)) { - /* Specialize to the symbol name and make the result a constant. */ - emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name)); - if (ctype_isconstval(ct->info)) { - if (ct->size >= 0x80000000u && - (ctype_child(cts, ct)->info & CTF_UNSIGNED)) - J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size); - else - J->base[0] = lj_ir_kint(J, (int32_t)ct->size); - } else if (ctype_isextern(ct->info)) { - CTypeID sid = ctype_cid(ct->info); - void *sp = *(void **)cdataptr(cdataV(tv)); - TRef ptr; - ct = ctype_raw(cts, sid); - if (LJ_64 && !checkptr32(sp)) - ptr = lj_ir_kintp(J, (uintptr_t)sp); - else - ptr = lj_ir_kptr(J, sp); - if (rd->data) { - J->base[0] = crec_tv_ct(J, ct, sid, ptr); - } else { - J->needsnap = 1; - crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]); - } - } else { - J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA); - } - } else { - lj_trace_err(J, LJ_TRERR_NOCACHE); - } - } /* else: interpreter will throw. */ -} - -/* -- FFI library functions ----------------------------------------------- */ - -static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval) -{ - return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval); -} - -void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd) -{ - crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0])); -} - -void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd) -{ - UNUSED(rd); - if (J->base[0]) - lj_trace_err(J, LJ_TRERR_NYICALL); - J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno); -} - -void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef tr = J->base[0]; - if (tr) { - TRef trlen = J->base[1]; - if (!tref_isnil(trlen)) { - trlen = crec_toint(J, cts, trlen, &rd->argv[1]); - tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]); - } else { - tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]); - trlen = lj_ir_call(J, IRCALL_strlen, tr); - } - J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen); - } /* else: interpreter will throw. */ -} - -void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2]; - if (trdst && trsrc && (trlen || tref_isstr(trsrc))) { - trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]); - trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]); - if (trlen) { - trlen = crec_toint(J, cts, trlen, &rd->argv[2]); - } else { - trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN); - trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); - } - rd->nres = 0; - crec_copy(J, trdst, trsrc, trlen, NULL); - } /* else: interpreter will throw. */ -} - -void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2]; - if (trdst && trlen) { - CTSize step = 1; - if (tviscdata(&rd->argv[0])) { /* Get alignment of original destination. */ - CTSize sz; - CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid); - if (ctype_isptr(ct->info)) - ct = ctype_rawchild(cts, ct); - step = (1u<argv[0]); - trlen = crec_toint(J, cts, trlen, &rd->argv[1]); - if (trfill) - trfill = crec_toint(J, cts, trfill, &rd->argv[2]); - else - trfill = lj_ir_kint(J, 0); - rd->nres = 0; - crec_fill(J, trdst, trlen, trfill, step); - } /* else: interpreter will throw. */ -} - -void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd) -{ - if (tref_iscdata(J->base[0])) { - TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0])); - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), - lj_ir_kint(J, CTID_CTYPEID), trid); - } else { - setfuncV(J->L, &J->errinfo, J->fn); - lj_trace_err_info(J, LJ_TRERR_NYIFFU); - } -} - -void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd) -{ - argv2ctype(J, J->base[0], &rd->argv[0]); - if (tref_iscdata(J->base[1])) { - argv2ctype(J, J->base[1], &rd->argv[1]); - J->postproc = LJ_POST_FIXBOOL; - J->base[0] = TREF_TRUE; - } else { - J->base[0] = TREF_FALSE; - } -} - -void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd) -{ - if (tref_isstr(J->base[0])) { - /* Specialize to the ABI string to make the boolean result a constant. */ - emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0]))); - J->postproc = LJ_POST_FIXBOOL; - J->base[0] = TREF_TRUE; - } else { - lj_trace_err(J, LJ_TRERR_BADTYPE); - } -} - -/* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */ -void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd) -{ - CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]); - if (rd->data == FF_ffi_sizeof) { - CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id); - if (ctype_isvltype(ct->info)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - } else if (rd->data == FF_ffi_offsetof) { /* Specialize to the field name. */ - if (!tref_isstr(J->base[1])) - lj_trace_err(J, LJ_TRERR_BADTYPE); - emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1]))); - rd->nres = 3; /* Just in case. */ - } - J->postproc = LJ_POST_FIXCONST; - J->base[0] = J->base[1] = J->base[2] = TREF_NIL; -} - -void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd) -{ - argv2cdata(J, J->base[0], &rd->argv[0]); - if (!J->base[1]) - lj_trace_err(J, LJ_TRERR_BADTYPE); - crec_finalizer(J, J->base[0], J->base[1], &rd->argv[1]); -} - -/* -- 64 bit bit.* library functions -------------------------------------- */ - -/* Determine bit operation type from argument type. */ -static CTypeID crec_bit64_type(CTState *cts, cTValue *tv) -{ - if (tviscdata(tv)) { - CType *ct = lj_ctype_rawref(cts, cdataV(tv)->ctypeid); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if ((ct->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) == - CTINFO(CT_NUM, CTF_UNSIGNED) && ct->size == 8) - return CTID_UINT64; /* Use uint64_t, since it has the highest rank. */ - return CTID_INT64; /* Otherwise use int64_t. */ - } - return 0; /* Use regular 32 bit ops. */ -} - -void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - TRef tr = crec_ct_tv(J, ctype_get(cts, CTID_INT64), 0, - J->base[0], &rd->argv[0]); - if (!tref_isinteger(tr)) - tr = emitconv(tr, IRT_INT, tref_type(tr), 0); - J->base[0] = tr; -} - -int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID id = crec_bit64_type(cts, &rd->argv[0]); - if (id) { - TRef tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]); - tr = emitir(IRT(rd->data, id-CTID_INT64+IRT_I64), tr, 0); - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - return 1; - } - return 0; -} - -int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID id = 0; - MSize i; - for (i = 0; J->base[i] != 0; i++) { - CTypeID aid = crec_bit64_type(cts, &rd->argv[i]); - if (id < aid) id = aid; /* Determine highest type rank of all arguments. */ - } - if (id) { - CType *ct = ctype_get(cts, id); - uint32_t ot = IRT(rd->data, id-CTID_INT64+IRT_I64); - TRef tr = crec_ct_tv(J, ct, 0, J->base[0], &rd->argv[0]); - for (i = 1; J->base[i] != 0; i++) { - TRef tr2 = crec_ct_tv(J, ct, 0, J->base[i], &rd->argv[i]); - tr = emitir(ot, tr, tr2); - } - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - return 1; - } - return 0; -} - -int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID id; - TRef tsh = 0; - if (J->base[0] && tref_iscdata(J->base[1])) { - tsh = crec_ct_tv(J, ctype_get(cts, CTID_INT64), 0, - J->base[1], &rd->argv[1]); - if (!tref_isinteger(tsh)) - tsh = emitconv(tsh, IRT_INT, tref_type(tsh), 0); - J->base[1] = tsh; - } - id = crec_bit64_type(cts, &rd->argv[0]); - if (id) { - TRef tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]); - uint32_t op = rd->data; - if (!tsh) tsh = lj_opt_narrow_tobit(J, J->base[1]); - if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && - !tref_isk(tsh)) - tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 63)); -#ifdef LJ_TARGET_UNIFYROT - if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) { - op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR; - tsh = emitir(IRTI(IR_NEG), tsh, tsh); - } -#endif - tr = emitir(IRT(op, id-CTID_INT64+IRT_I64), tr, tsh); - J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); - return 1; - } - return 0; -} - -TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CTypeID id = crec_bit64_type(cts, &rd->argv[0]); - TRef tr, trsf = J->base[1]; - SFormat sf = (STRFMT_UINT|STRFMT_T_HEX); - int32_t n; - if (trsf) { - CTypeID id2 = 0; - n = (int32_t)lj_carith_check64(J->L, 2, &id2); - if (id2) - trsf = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, trsf, &rd->argv[1]); - else - trsf = lj_opt_narrow_tobit(J, trsf); - emitir(IRTGI(IR_EQ), trsf, lj_ir_kint(J, n)); /* Specialize to n. */ - } else { - n = id ? 16 : 8; - } - if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; } - sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC); - if (id) { - tr = crec_ct_tv(J, ctype_get(cts, id), 0, J->base[0], &rd->argv[0]); - if (n < 16) - tr = emitir(IRT(IR_BAND, IRT_U64), tr, - lj_ir_kint64(J, ((uint64_t)1 << 4*n)-1)); - } else { - tr = lj_opt_narrow_tobit(J, J->base[0]); - if (n < 8) - tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << 4*n)-1))); - tr = emitconv(tr, IRT_U64, IRT_INT, 0); /* No sign-extension. */ - lj_needsplit(J); - } - return lj_ir_call(J, IRCALL_lj_strfmt_putfxint, hdr, lj_ir_kint(J, sf), tr); -} - -/* -- Miscellaneous library functions ------------------------------------- */ - -void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd) -{ - CTState *cts = ctype_ctsG(J2G(J)); - CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid); - if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); - if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) { - if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 && - !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) - d = ctype_get(cts, CTID_INT32); - else - d = ctype_get(cts, CTID_DOUBLE); - J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]); - } else { - /* Specialize to the ctype that couldn't be converted. */ - argv2cdata(J, J->base[0], &rd->argv[0]); - J->base[0] = TREF_NIL; - } -} - -TRef lj_crecord_loadiu64(jit_State *J, TRef tr, cTValue *o) -{ - CTypeID id = argv2cdata(J, tr, o)->ctypeid; - if (!(id == CTID_INT64 || id == CTID_UINT64)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - lj_needsplit(J); - return emitir(IRT(IR_FLOAD, id == CTID_INT64 ? IRT_I64 : IRT_U64), tr, - IRFL_CDATA_INT64); -} - -#if LJ_HASBUFFER -TRef lj_crecord_topcvoid(jit_State *J, TRef tr, cTValue *o) -{ - CTState *cts = ctype_ctsG(J2G(J)); - if (!tref_iscdata(tr)) lj_trace_err(J, LJ_TRERR_BADTYPE); - return crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, o); -} - -TRef lj_crecord_topuint8(jit_State *J, TRef tr) -{ - return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, CTID_P_UINT8), tr); -} -#endif - -#undef IR -#undef emitir -#undef emitconv - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.h deleted file mode 100644 index 2c8cf05..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_crecord.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -** Trace recorder for C data operations. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CRECORD_H -#define _LJ_CRECORD_H - -#include "lj_obj.h" -#include "lj_jit.h" -#include "lj_ffrecord.h" - -#if LJ_HASJIT && LJ_HASFFI -LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd); -LJ_FUNC void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd); - -LJ_FUNC void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd); -LJ_FUNC int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd); -LJ_FUNC int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd); -LJ_FUNC int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd); -LJ_FUNC TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr); - -LJ_FUNC void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd); -LJ_FUNC TRef lj_crecord_loadiu64(jit_State *J, TRef tr, cTValue *o); -#if LJ_HASBUFFER -LJ_FUNC TRef lj_crecord_topcvoid(jit_State *J, TRef tr, cTValue *o); -LJ_FUNC TRef lj_crecord_topuint8(jit_State *J, TRef tr); -#endif -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.c deleted file mode 100644 index 10322c0..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.c +++ /dev/null @@ -1,658 +0,0 @@ -/* -** C type management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include "lj_obj.h" - -#if LJ_HASFFI - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_strfmt.h" -#include "lj_ctype.h" -#include "lj_ccallback.h" -#include "lj_buf.h" - -/* -- C type definitions -------------------------------------------------- */ - -/* Predefined typedefs. */ -#define CTTDDEF(_) \ - /* Vararg handling. */ \ - _("va_list", P_VOID) \ - _("__builtin_va_list", P_VOID) \ - _("__gnuc_va_list", P_VOID) \ - /* From stddef.h. */ \ - _("ptrdiff_t", INT_PSZ) \ - _("size_t", UINT_PSZ) \ - _("wchar_t", WCHAR) \ - /* Subset of stdint.h. */ \ - _("int8_t", INT8) \ - _("int16_t", INT16) \ - _("int32_t", INT32) \ - _("int64_t", INT64) \ - _("uint8_t", UINT8) \ - _("uint16_t", UINT16) \ - _("uint32_t", UINT32) \ - _("uint64_t", UINT64) \ - _("intptr_t", INT_PSZ) \ - _("uintptr_t", UINT_PSZ) \ - /* From POSIX. */ \ - _("ssize_t", INT_PSZ) \ - /* End of typedef list. */ - -/* Keywords (only the ones we actually care for). */ -#define CTKWDEF(_) \ - /* Type specifiers. */ \ - _("void", -1, CTOK_VOID) \ - _("_Bool", 0, CTOK_BOOL) \ - _("bool", 1, CTOK_BOOL) \ - _("char", 1, CTOK_CHAR) \ - _("int", 4, CTOK_INT) \ - _("__int8", 1, CTOK_INT) \ - _("__int16", 2, CTOK_INT) \ - _("__int32", 4, CTOK_INT) \ - _("__int64", 8, CTOK_INT) \ - _("float", 4, CTOK_FP) \ - _("double", 8, CTOK_FP) \ - _("long", 0, CTOK_LONG) \ - _("short", 0, CTOK_SHORT) \ - _("_Complex", 0, CTOK_COMPLEX) \ - _("complex", 0, CTOK_COMPLEX) \ - _("__complex", 0, CTOK_COMPLEX) \ - _("__complex__", 0, CTOK_COMPLEX) \ - _("signed", 0, CTOK_SIGNED) \ - _("__signed", 0, CTOK_SIGNED) \ - _("__signed__", 0, CTOK_SIGNED) \ - _("unsigned", 0, CTOK_UNSIGNED) \ - /* Type qualifiers. */ \ - _("const", 0, CTOK_CONST) \ - _("__const", 0, CTOK_CONST) \ - _("__const__", 0, CTOK_CONST) \ - _("volatile", 0, CTOK_VOLATILE) \ - _("__volatile", 0, CTOK_VOLATILE) \ - _("__volatile__", 0, CTOK_VOLATILE) \ - _("restrict", 0, CTOK_RESTRICT) \ - _("__restrict", 0, CTOK_RESTRICT) \ - _("__restrict__", 0, CTOK_RESTRICT) \ - _("inline", 0, CTOK_INLINE) \ - _("__inline", 0, CTOK_INLINE) \ - _("__inline__", 0, CTOK_INLINE) \ - /* Storage class specifiers. */ \ - _("typedef", 0, CTOK_TYPEDEF) \ - _("extern", 0, CTOK_EXTERN) \ - _("static", 0, CTOK_STATIC) \ - _("auto", 0, CTOK_AUTO) \ - _("register", 0, CTOK_REGISTER) \ - /* GCC Attributes. */ \ - _("__extension__", 0, CTOK_EXTENSION) \ - _("__attribute", 0, CTOK_ATTRIBUTE) \ - _("__attribute__", 0, CTOK_ATTRIBUTE) \ - _("asm", 0, CTOK_ASM) \ - _("__asm", 0, CTOK_ASM) \ - _("__asm__", 0, CTOK_ASM) \ - /* MSVC Attributes. */ \ - _("__declspec", 0, CTOK_DECLSPEC) \ - _("__cdecl", CTCC_CDECL, CTOK_CCDECL) \ - _("__thiscall", CTCC_THISCALL, CTOK_CCDECL) \ - _("__fastcall", CTCC_FASTCALL, CTOK_CCDECL) \ - _("__stdcall", CTCC_STDCALL, CTOK_CCDECL) \ - _("__ptr32", 4, CTOK_PTRSZ) \ - _("__ptr64", 8, CTOK_PTRSZ) \ - /* Other type specifiers. */ \ - _("struct", 0, CTOK_STRUCT) \ - _("union", 0, CTOK_UNION) \ - _("enum", 0, CTOK_ENUM) \ - /* Operators. */ \ - _("sizeof", 0, CTOK_SIZEOF) \ - _("__alignof", 0, CTOK_ALIGNOF) \ - _("__alignof__", 0, CTOK_ALIGNOF) \ - /* End of keyword list. */ - -/* Type info for predefined types. Size merged in. */ -static CTInfo lj_ctype_typeinfo[] = { -#define CTTYINFODEF(id, sz, ct, info) CTINFO((ct),(((sz)&0x3fu)<<10)+(info)), -#define CTTDINFODEF(name, id) CTINFO(CT_TYPEDEF, CTID_##id), -#define CTKWINFODEF(name, sz, kw) CTINFO(CT_KW,(((sz)&0x3fu)<<10)+(kw)), -CTTYDEF(CTTYINFODEF) -CTTDDEF(CTTDINFODEF) -CTKWDEF(CTKWINFODEF) -#undef CTTYINFODEF -#undef CTTDINFODEF -#undef CTKWINFODEF - 0 -}; - -/* Predefined type names collected in a single string. */ -static const char * const lj_ctype_typenames = -#define CTTDNAMEDEF(name, id) name "\0" -#define CTKWNAMEDEF(name, sz, cds) name "\0" -CTTDDEF(CTTDNAMEDEF) -CTKWDEF(CTKWNAMEDEF) -#undef CTTDNAMEDEF -#undef CTKWNAMEDEF -; - -#define CTTYPEINFO_NUM (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1) -#ifdef LUAJIT_CTYPE_CHECK_ANCHOR -#define CTTYPETAB_MIN CTTYPEINFO_NUM -#else -#define CTTYPETAB_MIN 128 -#endif - -/* -- C type interning ---------------------------------------------------- */ - -#define ct_hashtype(info, size) (hashrot(info, size) & CTHASH_MASK) -#define ct_hashname(name) \ - (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK) - -/* Create new type element. */ -CTypeID lj_ctype_new(CTState *cts, CType **ctp) -{ - CTypeID id = cts->top; - CType *ct; - lj_assertCTS(cts->L, "uninitialized cts->L"); - if (LJ_UNLIKELY(id >= cts->sizetab)) { - if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); -#ifdef LUAJIT_CTYPE_CHECK_ANCHOR - ct = lj_mem_newvec(cts->L, id+1, CType); - memcpy(ct, cts->tab, id*sizeof(CType)); - memset(cts->tab, 0, id*sizeof(CType)); - lj_mem_freevec(cts->g, cts->tab, cts->sizetab, CType); - cts->tab = ct; - cts->sizetab = id+1; -#else - lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); -#endif - } - cts->top = id+1; - *ctp = ct = &cts->tab[id]; - ct->info = 0; - ct->size = 0; - ct->sib = 0; - ct->next = 0; - setgcrefnull(ct->name); - return id; -} - -/* Intern a type element. */ -CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size) -{ - uint32_t h = ct_hashtype(info, size); - CTypeID id = cts->hash[h]; - lj_assertCTS(cts->L, "uninitialized cts->L"); - while (id) { - CType *ct = ctype_get(cts, id); - if (ct->info == info && ct->size == size) - return id; - id = ct->next; - } - id = cts->top; - if (LJ_UNLIKELY(id >= cts->sizetab)) { -#ifdef LUAJIT_CTYPE_CHECK_ANCHOR - CType *ct; -#endif - if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); -#ifdef LUAJIT_CTYPE_CHECK_ANCHOR - ct = lj_mem_newvec(cts->L, id+1, CType); - memcpy(ct, cts->tab, id*sizeof(CType)); - memset(cts->tab, 0, id*sizeof(CType)); - lj_mem_freevec(cts->g, cts->tab, cts->sizetab, CType); - cts->tab = ct; - cts->sizetab = id+1; -#else - lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); -#endif - } - cts->top = id+1; - cts->tab[id].info = info; - cts->tab[id].size = size; - cts->tab[id].sib = 0; - cts->tab[id].next = cts->hash[h]; - setgcrefnull(cts->tab[id].name); - cts->hash[h] = (CTypeID1)id; - return id; -} - -/* Add type element to hash table. */ -static void ctype_addtype(CTState *cts, CType *ct, CTypeID id) -{ - uint32_t h = ct_hashtype(ct->info, ct->size); - ct->next = cts->hash[h]; - cts->hash[h] = (CTypeID1)id; -} - -/* Add named element to hash table. */ -void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id) -{ - uint32_t h = ct_hashname(gcref(ct->name)); - ct->next = cts->hash[h]; - cts->hash[h] = (CTypeID1)id; -} - -/* Get a C type by name, matching the type mask. */ -CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, uint32_t tmask) -{ - CTypeID id = cts->hash[ct_hashname(name)]; - while (id) { - CType *ct = ctype_get(cts, id); - if (gcref(ct->name) == obj2gco(name) && - ((tmask >> ctype_type(ct->info)) & 1)) { - *ctp = ct; - return id; - } - id = ct->next; - } - *ctp = &cts->tab[0]; /* Simplify caller logic. ctype_get() would assert. */ - return 0; -} - -/* Get a struct/union/enum/function field by name. */ -CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, CTSize *ofs, - CTInfo *qual) -{ - while (ct->sib) { - ct = ctype_get(cts, ct->sib); - if (gcref(ct->name) == obj2gco(name)) { - *ofs = ct->size; - return ct; - } - if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { - CType *fct, *cct = ctype_child(cts, ct); - CTInfo q = 0; - while (ctype_isattrib(cct->info)) { - if (ctype_attrib(cct->info) == CTA_QUAL) q |= cct->size; - cct = ctype_child(cts, cct); - } - fct = lj_ctype_getfieldq(cts, cct, name, ofs, qual); - if (fct) { - if (qual) *qual |= q; - *ofs += ct->size; - return fct; - } - } - } - return NULL; /* Not found. */ -} - -/* -- C type information -------------------------------------------------- */ - -/* Follow references and get raw type for a C type ID. */ -CType *lj_ctype_rawref(CTState *cts, CTypeID id) -{ - CType *ct = ctype_get(cts, id); - while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) - ct = ctype_child(cts, ct); - return ct; -} - -/* Get size for a C type ID. Does NOT support VLA/VLS. */ -CTSize lj_ctype_size(CTState *cts, CTypeID id) -{ - CType *ct = ctype_raw(cts, id); - return ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; -} - -/* Get size for a variable-length C type. Does NOT support other C types. */ -CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem) -{ - uint64_t xsz = 0; - if (ctype_isstruct(ct->info)) { - CTypeID arrid = 0, fid = ct->sib; - xsz = ct->size; /* Add the struct size. */ - while (fid) { - CType *ctf = ctype_get(cts, fid); - if (ctype_type(ctf->info) == CT_FIELD) - arrid = ctype_cid(ctf->info); /* Remember last field of VLS. */ - fid = ctf->sib; - } - ct = ctype_raw(cts, arrid); - } - lj_assertCTS(ctype_isvlarray(ct->info), "VLA expected"); - ct = ctype_rawchild(cts, ct); /* Get array element. */ - lj_assertCTS(ctype_hassize(ct->info), "bad VLA without size"); - /* Calculate actual size of VLA and check for overflow. */ - xsz += (uint64_t)ct->size * nelem; - return xsz < 0x80000000u ? (CTSize)xsz : CTSIZE_INVALID; -} - -/* Get type, qualifiers, size and alignment for a C type ID. */ -CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp) -{ - CTInfo qual = 0; - CType *ct = ctype_get(cts, id); - for (;;) { - CTInfo info = ct->info; - if (ctype_isenum(info)) { - /* Follow child. Need to look at its attributes, too. */ - } else if (ctype_isattrib(info)) { - if (ctype_isxattrib(info, CTA_QUAL)) - qual |= ct->size; - else if (ctype_isxattrib(info, CTA_ALIGN) && !(qual & CTFP_ALIGNED)) - qual |= CTFP_ALIGNED + CTALIGN(ct->size); - } else { - if (!(qual & CTFP_ALIGNED)) qual |= (info & CTF_ALIGN); - qual |= (info & ~(CTF_ALIGN|CTMASK_CID)); - lj_assertCTS(ctype_hassize(info) || ctype_isfunc(info), - "ctype without size"); - *szp = ctype_isfunc(info) ? CTSIZE_INVALID : ct->size; - break; - } - ct = ctype_get(cts, ctype_cid(info)); - } - return qual; -} - -/* Ditto, but follow a reference. */ -CTInfo lj_ctype_info_raw(CTState *cts, CTypeID id, CTSize *szp) -{ - CType *ct = ctype_get(cts, id); - if (ctype_isref(ct->info)) id = ctype_cid(ct->info); - return lj_ctype_info(cts, id, szp); -} - -/* Get ctype metamethod. */ -cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm) -{ - CType *ct = ctype_get(cts, id); - cTValue *tv; - while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) { - id = ctype_cid(ct->info); - ct = ctype_get(cts, id); - } - if (ctype_isptr(ct->info) && - ctype_isfunc(ctype_get(cts, ctype_cid(ct->info))->info)) - tv = lj_tab_getstr(cts->miscmap, &cts->g->strempty); - else - tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); - if (tv && tvistab(tv) && - (tv = lj_tab_getstr(tabV(tv), mmname_str(cts->g, mm))) && !tvisnil(tv)) - return tv; - return NULL; -} - -/* -- C type representation ----------------------------------------------- */ - -/* Fixed max. length of a C type representation. */ -#define CTREPR_MAX 512 - -typedef struct CTRepr { - char *pb, *pe; - CTState *cts; - lua_State *L; - int needsp; - int ok; - char buf[CTREPR_MAX]; -} CTRepr; - -/* Prepend string. */ -static void ctype_prepstr(CTRepr *ctr, const char *str, MSize len) -{ - char *p = ctr->pb; - if (ctr->buf + len+1 > p) { ctr->ok = 0; return; } - if (ctr->needsp) *--p = ' '; - ctr->needsp = 1; - p -= len; - while (len-- > 0) p[len] = str[len]; - ctr->pb = p; -} - -#define ctype_preplit(ctr, str) ctype_prepstr((ctr), "" str, sizeof(str)-1) - -/* Prepend char. */ -static void ctype_prepc(CTRepr *ctr, int c) -{ - if (ctr->buf >= ctr->pb) { ctr->ok = 0; return; } - *--ctr->pb = c; -} - -/* Prepend number. */ -static void ctype_prepnum(CTRepr *ctr, uint32_t n) -{ - char *p = ctr->pb; - if (ctr->buf + 10+1 > p) { ctr->ok = 0; return; } - do { *--p = (char)('0' + n % 10); } while (n /= 10); - ctr->pb = p; - ctr->needsp = 0; -} - -/* Append char. */ -static void ctype_appc(CTRepr *ctr, int c) -{ - if (ctr->pe >= ctr->buf + CTREPR_MAX) { ctr->ok = 0; return; } - *ctr->pe++ = c; -} - -/* Append number. */ -static void ctype_appnum(CTRepr *ctr, uint32_t n) -{ - char buf[10]; - char *p = buf+sizeof(buf); - char *q = ctr->pe; - if (q > ctr->buf + CTREPR_MAX - 10) { ctr->ok = 0; return; } - do { *--p = (char)('0' + n % 10); } while (n /= 10); - do { *q++ = *p++; } while (p < buf+sizeof(buf)); - ctr->pe = q; -} - -/* Prepend qualifiers. */ -static void ctype_prepqual(CTRepr *ctr, CTInfo info) -{ - if ((info & CTF_VOLATILE)) ctype_preplit(ctr, "volatile"); - if ((info & CTF_CONST)) ctype_preplit(ctr, "const"); -} - -/* Prepend named type. */ -static void ctype_preptype(CTRepr *ctr, CType *ct, CTInfo qual, const char *t) -{ - if (gcref(ct->name)) { - GCstr *str = gco2str(gcref(ct->name)); - ctype_prepstr(ctr, strdata(str), str->len); - } else { - if (ctr->needsp) ctype_prepc(ctr, ' '); - ctype_prepnum(ctr, ctype_typeid(ctr->cts, ct)); - ctr->needsp = 1; - } - ctype_prepstr(ctr, t, (MSize)strlen(t)); - ctype_prepqual(ctr, qual); -} - -static void ctype_repr(CTRepr *ctr, CTypeID id) -{ - CType *ct = ctype_get(ctr->cts, id); - CTInfo qual = 0; - int ptrto = 0; - for (;;) { - CTInfo info = ct->info; - CTSize size = ct->size; - switch (ctype_type(info)) { - case CT_NUM: - if ((info & CTF_BOOL)) { - ctype_preplit(ctr, "bool"); - } else if ((info & CTF_FP)) { - if (size == sizeof(double)) ctype_preplit(ctr, "double"); - else if (size == sizeof(float)) ctype_preplit(ctr, "float"); - else ctype_preplit(ctr, "long double"); - } else if (size == 1) { - if (!((info ^ CTF_UCHAR) & CTF_UNSIGNED)) ctype_preplit(ctr, "char"); - else if (CTF_UCHAR) ctype_preplit(ctr, "signed char"); - else ctype_preplit(ctr, "unsigned char"); - } else if (size < 8) { - if (size == 4) ctype_preplit(ctr, "int"); - else ctype_preplit(ctr, "short"); - if ((info & CTF_UNSIGNED)) ctype_preplit(ctr, "unsigned"); - } else { - ctype_preplit(ctr, "_t"); - ctype_prepnum(ctr, size*8); - ctype_preplit(ctr, "int"); - if ((info & CTF_UNSIGNED)) ctype_prepc(ctr, 'u'); - } - ctype_prepqual(ctr, (qual|info)); - return; - case CT_VOID: - ctype_preplit(ctr, "void"); - ctype_prepqual(ctr, (qual|info)); - return; - case CT_STRUCT: - ctype_preptype(ctr, ct, qual, (info & CTF_UNION) ? "union" : "struct"); - return; - case CT_ENUM: - if (id == CTID_CTYPEID) { - ctype_preplit(ctr, "ctype"); - return; - } - ctype_preptype(ctr, ct, qual, "enum"); - return; - case CT_ATTRIB: - if (ctype_attrib(info) == CTA_QUAL) qual |= size; - break; - case CT_PTR: - if ((info & CTF_REF)) { - ctype_prepc(ctr, '&'); - } else { - ctype_prepqual(ctr, (qual|info)); - if (LJ_64 && size == 4) ctype_preplit(ctr, "__ptr32"); - ctype_prepc(ctr, '*'); - } - qual = 0; - ptrto = 1; - ctr->needsp = 1; - break; - case CT_ARRAY: - if (ctype_isrefarray(info)) { - ctr->needsp = 1; - if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } - ctype_appc(ctr, '['); - if (size != CTSIZE_INVALID) { - CTSize csize = ctype_child(ctr->cts, ct)->size; - ctype_appnum(ctr, csize ? size/csize : 0); - } else if ((info & CTF_VLA)) { - ctype_appc(ctr, '?'); - } - ctype_appc(ctr, ']'); - } else if ((info & CTF_COMPLEX)) { - if (size == 2*sizeof(float)) ctype_preplit(ctr, "float"); - ctype_preplit(ctr, "complex"); - return; - } else { - ctype_preplit(ctr, ")))"); - ctype_prepnum(ctr, size); - ctype_preplit(ctr, "__attribute__((vector_size("); - } - break; - case CT_FUNC: - ctr->needsp = 1; - if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } - ctype_appc(ctr, '('); - ctype_appc(ctr, ')'); - break; - default: - lj_assertG_(ctr->cts->g, 0, "bad ctype %08x", info); - break; - } - ct = ctype_get(ctr->cts, ctype_cid(info)); - } -} - -/* Return a printable representation of a C type. */ -GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name) -{ - global_State *g = G(L); - CTRepr ctr; - ctr.pb = ctr.pe = &ctr.buf[CTREPR_MAX/2]; - ctr.cts = ctype_ctsG(g); - ctr.L = L; - ctr.ok = 1; - ctr.needsp = 0; - if (name) ctype_prepstr(&ctr, strdata(name), name->len); - ctype_repr(&ctr, id); - if (LJ_UNLIKELY(!ctr.ok)) return lj_str_newlit(L, "?"); - return lj_str_new(L, ctr.pb, ctr.pe - ctr.pb); -} - -/* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */ -GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned) -{ - char buf[1+20+3]; - char *p = buf+sizeof(buf); - int sign = 0; - *--p = 'L'; *--p = 'L'; - if (isunsigned) { - *--p = 'U'; - } else if ((int64_t)n < 0) { - n = (uint64_t)-(int64_t)n; - sign = 1; - } - do { *--p = (char)('0' + n % 10); } while (n /= 10); - if (sign) *--p = '-'; - return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p)); -} - -/* Convert complex to string with 'i' or 'I' suffix. */ -GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size) -{ - SBuf *sb = lj_buf_tmp_(L); - TValue re, im; - if (size == 2*sizeof(double)) { - re.n = *(double *)sp; im.n = ((double *)sp)[1]; - } else { - re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1]; - } - lj_strfmt_putfnum(sb, STRFMT_G14, re.n); - if (!(im.u32.hi & 0x80000000u) || im.n != im.n) lj_buf_putchar(sb, '+'); - lj_strfmt_putfnum(sb, STRFMT_G14, im.n); - lj_buf_putchar(sb, sb->w[-1] >= 'a' ? 'I' : 'i'); - return lj_buf_str(L, sb); -} - -/* -- C type state -------------------------------------------------------- */ - -/* Initialize C type table and state. */ -CTState *lj_ctype_init(lua_State *L) -{ - CTState *cts = lj_mem_newt(L, sizeof(CTState), CTState); - CType *ct = lj_mem_newvec(L, CTTYPETAB_MIN, CType); - const char *name = lj_ctype_typenames; - CTypeID id; - memset(cts, 0, sizeof(CTState)); - cts->tab = ct; - cts->sizetab = CTTYPETAB_MIN; - cts->top = CTTYPEINFO_NUM; - cts->L = NULL; - cts->g = G(L); - for (id = 0; id < CTTYPEINFO_NUM; id++, ct++) { - CTInfo info = lj_ctype_typeinfo[id]; - ct->size = (CTSize)((int32_t)(info << 16) >> 26); - ct->info = info & 0xffff03ffu; - ct->sib = 0; - if (ctype_type(info) == CT_KW || ctype_istypedef(info)) { - size_t len = strlen(name); - GCstr *str = lj_str_new(L, name, len); - ctype_setname(ct, str); - name += len+1; - lj_ctype_addname(cts, ct, id); - } else { - setgcrefnull(ct->name); - ct->next = 0; - if (!ctype_isenum(info)) ctype_addtype(cts, ct, id); - } - } - setmref(G(L)->ctype_state, cts); - return cts; -} - -/* Free C type table and state. */ -void lj_ctype_freestate(global_State *g) -{ - CTState *cts = ctype_ctsG(g); - if (cts) { - lj_ccallback_mcode_free(cts); - lj_mem_freevec(g, cts->tab, cts->sizetab, CType); - lj_mem_freevec(g, cts->cb.cbid, cts->cb.sizeid, CTypeID1); - lj_mem_freet(g, cts); - } -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.h deleted file mode 100644 index 3dbcdbf..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ctype.h +++ /dev/null @@ -1,481 +0,0 @@ -/* -** C type management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_CTYPE_H -#define _LJ_CTYPE_H - -#include "lj_obj.h" -#include "lj_gc.h" - -#if LJ_HASFFI - -/* -- C type definitions -------------------------------------------------- */ - -/* C type numbers. Highest 4 bits of C type info. ORDER CT. */ -enum { - /* Externally visible types. */ - CT_NUM, /* Integer or floating-point numbers. */ - CT_STRUCT, /* Struct or union. */ - CT_PTR, /* Pointer or reference. */ - CT_ARRAY, /* Array or complex type. */ - CT_MAYCONVERT = CT_ARRAY, - CT_VOID, /* Void type. */ - CT_ENUM, /* Enumeration. */ - CT_HASSIZE = CT_ENUM, /* Last type where ct->size holds the actual size. */ - CT_FUNC, /* Function. */ - CT_TYPEDEF, /* Typedef. */ - CT_ATTRIB, /* Miscellaneous attributes. */ - /* Internal element types. */ - CT_FIELD, /* Struct/union field or function parameter. */ - CT_BITFIELD, /* Struct/union bitfield. */ - CT_CONSTVAL, /* Constant value. */ - CT_EXTERN, /* External reference. */ - CT_KW /* Keyword. */ -}; - -LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR); -LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT); - -/* -** ---------- info ------------ -** |type flags... A cid | size | sib | next | name | -** +----------------------------+--------+-------+-------+-------+-- -** |NUM BFcvUL.. A | size | | type | | -** |STRUCT ..cvU..V A | size | field | name? | name? | -** |PTR ..cvR... A cid | size | | type | | -** |ARRAY VCcv...V A cid | size | | type | | -** |VOID ..cv.... A | size | | type | | -** |ENUM A cid | size | const | name? | name? | -** |FUNC ....VS.. cc cid | nargs | field | name? | name? | -** |TYPEDEF cid | | | name | name | -** |ATTRIB attrnum cid | attr | sib? | type? | | -** |FIELD cid | offset | field | | name? | -** |BITFIELD B.cvU csz bsz pos | offset | field | | name? | -** |CONSTVAL c cid | value | const | name | name | -** |EXTERN cid | | sib? | name | name | -** |KW tok | size | | name | name | -** +----------------------------+--------+-------+-------+-------+-- -** ^^ ^^--- bits used for C type conversion dispatch -*/ - -/* C type info flags. TFFArrrr */ -#define CTF_BOOL 0x08000000u /* Boolean: NUM, BITFIELD. */ -#define CTF_FP 0x04000000u /* Floating-point: NUM. */ -#define CTF_CONST 0x02000000u /* Const qualifier. */ -#define CTF_VOLATILE 0x01000000u /* Volatile qualifier. */ -#define CTF_UNSIGNED 0x00800000u /* Unsigned: NUM, BITFIELD. */ -#define CTF_LONG 0x00400000u /* Long: NUM. */ -#define CTF_VLA 0x00100000u /* Variable-length: ARRAY, STRUCT. */ -#define CTF_REF 0x00800000u /* Reference: PTR. */ -#define CTF_VECTOR 0x08000000u /* Vector: ARRAY. */ -#define CTF_COMPLEX 0x04000000u /* Complex: ARRAY. */ -#define CTF_UNION 0x00800000u /* Union: STRUCT. */ -#define CTF_VARARG 0x00800000u /* Vararg: FUNC. */ -#define CTF_SSEREGPARM 0x00400000u /* SSE register parameters: FUNC. */ - -#define CTF_QUAL (CTF_CONST|CTF_VOLATILE) -#define CTF_ALIGN (CTMASK_ALIGN< 0 ? CTF_UNSIGNED : 0) - -/* Flags used in parser. .F.Ammvf cp->attr */ -#define CTFP_ALIGNED 0x00000001u /* cp->attr + ALIGN */ -#define CTFP_PACKED 0x00000002u /* cp->attr */ -/* ...C...f cp->fattr */ -#define CTFP_CCONV 0x00000001u /* cp->fattr + CCONV/[SSE]REGPARM */ - -/* C type info bitfields. */ -#define CTMASK_CID 0x0000ffffu /* Max. 65536 type IDs. */ -#define CTMASK_NUM 0xf0000000u /* Max. 16 type numbers. */ -#define CTSHIFT_NUM 28 -#define CTMASK_ALIGN 15 /* Max. alignment is 2^15. */ -#define CTSHIFT_ALIGN 16 -#define CTMASK_ATTRIB 255 /* Max. 256 attributes. */ -#define CTSHIFT_ATTRIB 16 -#define CTMASK_CCONV 3 /* Max. 4 calling conventions. */ -#define CTSHIFT_CCONV 16 -#define CTMASK_REGPARM 3 /* Max. 0-3 regparms. */ -#define CTSHIFT_REGPARM 18 -/* Bitfields only used in parser. */ -#define CTMASK_VSIZEP 15 /* Max. vector size is 2^15. */ -#define CTSHIFT_VSIZEP 4 -#define CTMASK_MSIZEP 255 /* Max. type size (via mode) is 128. */ -#define CTSHIFT_MSIZEP 8 - -/* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */ -#define CTBSZ_MAX 32 /* Max. size of bitfield is 32 bit. */ -#define CTBSZ_FIELD 127 /* Temp. marker for regular field. */ -#define CTMASK_BITPOS 127 -#define CTMASK_BITBSZ 127 -#define CTMASK_BITCSZ 127 -#define CTSHIFT_BITPOS 0 -#define CTSHIFT_BITBSZ 8 -#define CTSHIFT_BITCSZ 16 - -#define CTF_INSERT(info, field, val) \ - info = (info & ~(CTMASK_##field<> CTSHIFT_NUM) -#define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID)) -#define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN) -#define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB) -#define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS) -#define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ) -#define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ) -#define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP) -#define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP) -#define ctype_cconv(info) (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV) - -/* Simple type checks. */ -#define ctype_isnum(info) (ctype_type((info)) == CT_NUM) -#define ctype_isvoid(info) (ctype_type((info)) == CT_VOID) -#define ctype_isptr(info) (ctype_type((info)) == CT_PTR) -#define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY) -#define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT) -#define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC) -#define ctype_isenum(info) (ctype_type((info)) == CT_ENUM) -#define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF) -#define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB) -#define ctype_isfield(info) (ctype_type((info)) == CT_FIELD) -#define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD) -#define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL) -#define ctype_isextern(info) (ctype_type((info)) == CT_EXTERN) -#define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE) - -/* Combined type and flag checks. */ -#define ctype_isinteger(info) \ - (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0)) -#define ctype_isinteger_or_bool(info) \ - (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0)) -#define ctype_isbool(info) \ - (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL)) -#define ctype_isfp(info) \ - (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP)) - -#define ctype_ispointer(info) \ - ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */ -#define ctype_isref(info) \ - (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF)) - -#define ctype_isrefarray(info) \ - (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0)) -#define ctype_isvector(info) \ - (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR)) -#define ctype_iscomplex(info) \ - (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX)) - -#define ctype_isvltype(info) \ - (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<g, (c), __VA_ARGS__)) -#else -#define lj_assertCTS(c, ...) ((void)cts) -#endif - -/* -- Predefined types ---------------------------------------------------- */ - -/* Target-dependent types. */ -#if LJ_TARGET_PPC -#define CTTYDEFP(_) \ - _(LINT32, 4, CT_NUM, CTF_LONG|CTALIGN(2)) -#else -#define CTTYDEFP(_) -#endif - -/* Common types. */ -#define CTTYDEF(_) \ - _(NONE, 0, CT_ATTRIB, CTATTRIB(CTA_BAD)) \ - _(VOID, -1, CT_VOID, CTALIGN(0)) \ - _(CVOID, -1, CT_VOID, CTF_CONST|CTALIGN(0)) \ - _(BOOL, 1, CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \ - _(CCHAR, 1, CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \ - _(INT8, 1, CT_NUM, CTALIGN(0)) \ - _(UINT8, 1, CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \ - _(INT16, 2, CT_NUM, CTALIGN(1)) \ - _(UINT16, 2, CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \ - _(INT32, 4, CT_NUM, CTALIGN(2)) \ - _(UINT32, 4, CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \ - _(INT64, 8, CT_NUM, CTF_LONG|CTALIGN(3)) \ - _(UINT64, 8, CT_NUM, CTF_UNSIGNED|CTF_LONG|CTALIGN(3)) \ - _(FLOAT, 4, CT_NUM, CTF_FP|CTALIGN(2)) \ - _(DOUBLE, 8, CT_NUM, CTF_FP|CTALIGN(3)) \ - _(COMPLEX_FLOAT, 8, CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \ - _(COMPLEX_DOUBLE, 16, CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \ - _(P_VOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_VOID) \ - _(P_CVOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CVOID) \ - _(P_CCHAR, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CCHAR) \ - _(P_UINT8, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_UINT8) \ - _(A_CCHAR, -1, CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \ - _(CTYPEID, 4, CT_ENUM, CTALIGN(2)|CTID_INT32) \ - CTTYDEFP(_) \ - /* End of type list. */ - -/* Public predefined type IDs. */ -enum { -#define CTTYIDDEF(id, sz, ct, info) CTID_##id, -CTTYDEF(CTTYIDDEF) -#undef CTTYIDDEF - /* Predefined typedefs and keywords follow. */ - CTID_MAX = 65536 -}; - -/* Target-dependent type IDs. */ -#if LJ_64 -#define CTID_INT_PSZ CTID_INT64 -#define CTID_UINT_PSZ CTID_UINT64 -#else -#define CTID_INT_PSZ CTID_INT32 -#define CTID_UINT_PSZ CTID_UINT32 -#endif - -#if LJ_ABI_WIN -#define CTID_WCHAR CTID_UINT16 -#elif LJ_TARGET_PPC -#define CTID_WCHAR CTID_LINT32 -#else -#define CTID_WCHAR CTID_INT32 -#endif - -/* -- C tokens and keywords ----------------------------------------------- */ - -/* C lexer keywords. */ -#define CTOKDEF(_) \ - _(IDENT, "") _(STRING, "") \ - _(INTEGER, "") _(EOF, "") \ - _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \ - _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->") - -/* Simple declaration specifiers. */ -#define CDSDEF(_) \ - _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \ - _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \ - _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \ - _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER) - -/* C keywords. */ -#define CKWDEF(_) \ - CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \ - _(DECLSPEC) _(CCDECL) _(PTRSZ) \ - _(STRUCT) _(UNION) _(ENUM) \ - _(SIZEOF) _(ALIGNOF) - -/* C token numbers. */ -enum { - CTOK_OFS = 255, -#define CTOKNUM(name, sym) CTOK_##name, -#define CKWNUM(name) CTOK_##name, -CTOKDEF(CTOKNUM) -CKWDEF(CKWNUM) -#undef CTOKNUM -#undef CKWNUM - CTOK_FIRSTDECL = CTOK_VOID, - CTOK_FIRSTSCL = CTOK_TYPEDEF, - CTOK_LASTDECLFLAG = CTOK_REGISTER, - CTOK_LASTDECL = CTOK_ENUM -}; - -/* Declaration specifier flags. */ -enum { -#define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)), -CDSDEF(CDSFLAG) -#undef CDSFLAG - CDF__END -}; - -#define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER) - -/* -- C type management --------------------------------------------------- */ - -#define ctype_ctsG(g) (mref((g)->ctype_state, CTState)) - -/* Get C type state. */ -static LJ_AINLINE CTState *ctype_cts(lua_State *L) -{ - CTState *cts = ctype_ctsG(G(L)); - cts->L = L; /* Save L for errors and allocations. */ - return cts; -} - -/* Load FFI library on-demand. */ -#define ctype_loadffi(L) \ - do { \ - if (!ctype_ctsG(G(L))) { \ - ptrdiff_t oldtop = (char *)L->top - mref(L->stack, char); \ - luaopen_ffi(L); \ - L->top = (TValue *)(mref(L->stack, char) + oldtop); \ - } \ - } while (0) - -/* Save and restore state of C type table. */ -#define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts) -#define LJ_CTYPE_RESTORE(cts) \ - ((cts)->top = savects_.top, \ - memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash))) - -/* Check C type ID for validity when assertions are enabled. */ -static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id) -{ - UNUSED(cts); - lj_assertCTS(id > 0 && id < cts->top, "bad CTID %d", id); - return id; -} - -/* Get C type for C type ID. */ -static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id) -{ - return &cts->tab[ctype_check(cts, id)]; -} - -/* Get C type ID for a C type. */ -#define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab)) - -/* Get child C type. */ -static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct) -{ - lj_assertCTS(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) || - ctype_isbitfield(ct->info)), - "ctype %08x has no children", ct->info); - return ctype_get(cts, ctype_cid(ct->info)); -} - -/* Get raw type for a C type ID. */ -static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id) -{ - CType *ct = ctype_get(cts, id); - while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct); - return ct; -} - -/* Get raw type of the child of a C type. */ -static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct) -{ - do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info)); - return ct; -} - -/* Set the name of a C type table element. */ -static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s) -{ - /* NOBARRIER: mark string as fixed -- the C type table is never collected. */ - fixstring(s); - setgcref(ct->name, obj2gco(s)); -} - -LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp); -LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size); -LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id); -LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, - uint32_t tmask); -LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, - CTSize *ofs, CTInfo *qual); -#define lj_ctype_getfield(cts, ct, name, ofs) \ - lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL) -LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id); -LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id); -LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem); -LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp); -LJ_FUNC CTInfo lj_ctype_info_raw(CTState *cts, CTypeID id, CTSize *szp); -LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm); -LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); -LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); -LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size); -LJ_FUNC CTState *lj_ctype_init(lua_State *L); -LJ_FUNC void lj_ctype_freestate(global_State *g); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.c deleted file mode 100644 index ca89315..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.c +++ /dev/null @@ -1,708 +0,0 @@ -/* -** Debugging and introspection. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_debug_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_buf.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_strfmt.h" -#if LJ_HASJIT -#include "lj_jit.h" -#endif - -/* -- Frames -------------------------------------------------------------- */ - -/* Get frame corresponding to a level. */ -cTValue *lj_debug_frame(lua_State *L, int level, int *size) -{ - cTValue *frame, *nextframe, *bot = tvref(L->stack)+LJ_FR2; - /* Traverse frames backwards. */ - for (nextframe = frame = L->base-1; frame > bot; ) { - if (frame_gc(frame) == obj2gco(L)) - level++; /* Skip dummy frames. See lj_err_optype_call(). */ - if (level-- == 0) { - *size = (int)(nextframe - frame); - return frame; /* Level found. */ - } - nextframe = frame; - if (frame_islua(frame)) { - frame = frame_prevl(frame); - } else { - if (frame_isvarg(frame)) - level++; /* Skip vararg pseudo-frame. */ - frame = frame_prevd(frame); - } - } - *size = level; - return NULL; /* Level not found. */ -} - -/* Invalid bytecode position. */ -#define NO_BCPOS (~(BCPos)0) - -/* Return bytecode position for function/frame or NO_BCPOS. */ -static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe) -{ - const BCIns *ins; - GCproto *pt; - BCPos pos; - lj_assertL(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD, - "function or frame expected"); - if (!isluafunc(fn)) { /* Cannot derive a PC for non-Lua functions. */ - return NO_BCPOS; - } else if (nextframe == NULL) { /* Lua function on top. */ - void *cf = cframe_raw(L->cframe); - if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf)) - return NO_BCPOS; - ins = cframe_pc(cf); /* Only happens during error/hook handling. */ - } else { - if (frame_islua(nextframe)) { - ins = frame_pc(nextframe); - } else if (frame_iscont(nextframe)) { - ins = frame_contpc(nextframe); - } else { - /* Lua function below errfunc/gc/hook: find cframe to get the PC. */ - void *cf = cframe_raw(L->cframe); - TValue *f = L->base-1; - for (;;) { - if (cf == NULL) - return NO_BCPOS; - while (cframe_nres(cf) < 0) { - if (f >= restorestack(L, -cframe_nres(cf))) - break; - cf = cframe_raw(cframe_prev(cf)); - if (cf == NULL) - return NO_BCPOS; - } - if (f < nextframe) - break; - if (frame_islua(f)) { - f = frame_prevl(f); - } else { - if (frame_isc(f) || (frame_iscont(f) && frame_iscont_fficb(f))) - cf = cframe_raw(cframe_prev(cf)); - f = frame_prevd(f); - } - } - ins = cframe_pc(cf); - if (!ins) return NO_BCPOS; - } - } - pt = funcproto(fn); - pos = proto_bcpos(pt, ins) - 1; -#if LJ_HASJIT - if (pos > pt->sizebc) { /* Undo the effects of lj_trace_exit for JLOOP. */ - if (bc_isret(bc_op(ins[-1]))) { - GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins)); - pos = proto_bcpos(pt, mref(T->startpc, const BCIns)); - } else { - pos = NO_BCPOS; /* Punt in case of stack overflow for stitched trace. */ - } - } -#endif - return pos; -} - -/* -- Line numbers -------------------------------------------------------- */ - -/* Get line number for a bytecode position. */ -BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc) -{ - const void *lineinfo = proto_lineinfo(pt); - if (pc <= pt->sizebc && lineinfo) { - BCLine first = pt->firstline; - if (pc == pt->sizebc) return first + pt->numline; - if (pc-- == 0) return first; - if (pt->numline < 256) - return first + (BCLine)((const uint8_t *)lineinfo)[pc]; - else if (pt->numline < 65536) - return first + (BCLine)((const uint16_t *)lineinfo)[pc]; - else - return first + (BCLine)((const uint32_t *)lineinfo)[pc]; - } - return 0; -} - -/* Get line number for function/frame. */ -static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe) -{ - BCPos pc = debug_framepc(L, fn, nextframe); - if (pc != NO_BCPOS) { - GCproto *pt = funcproto(fn); - lj_assertL(pc <= pt->sizebc, "PC out of range"); - return lj_debug_line(pt, pc); - } - return -1; -} - -/* -- Variable names ------------------------------------------------------ */ - -/* Get name of a local variable from slot number and PC. */ -static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) -{ - const char *p = (const char *)proto_varinfo(pt); - if (p) { - BCPos lastpc = 0; - for (;;) { - const char *name = p; - uint32_t vn = *(const uint8_t *)p; - BCPos startpc, endpc; - if (vn < VARNAME__MAX) { - if (vn == VARNAME_END) break; /* End of varinfo. */ - } else { - do { p++; } while (*(const uint8_t *)p); /* Skip over variable name. */ - } - p++; - lastpc = startpc = lastpc + lj_buf_ruleb128(&p); - if (startpc > pc) break; - endpc = startpc + lj_buf_ruleb128(&p); - if (pc < endpc && slot-- == 0) { - if (vn < VARNAME__MAX) { -#define VARNAMESTR(name, str) str "\0" - name = VARNAMEDEF(VARNAMESTR); -#undef VARNAMESTR - if (--vn) while (*name++ || --vn) ; - } - return name; - } - } - } - return NULL; -} - -/* Get name of local variable from 1-based slot number and function/frame. */ -static TValue *debug_localname(lua_State *L, const lua_Debug *ar, - const char **name, BCReg slot1) -{ - uint32_t offset = (uint32_t)ar->i_ci & 0xffff; - uint32_t size = (uint32_t)ar->i_ci >> 16; - TValue *frame = tvref(L->stack) + offset; - TValue *nextframe = size ? frame + size : NULL; - GCfunc *fn = frame_func(frame); - BCPos pc = debug_framepc(L, fn, nextframe); - if (!nextframe) nextframe = L->top+LJ_FR2; - if ((int)slot1 < 0) { /* Negative slot number is for varargs. */ - if (pc != NO_BCPOS) { - GCproto *pt = funcproto(fn); - if ((pt->flags & PROTO_VARARG)) { - slot1 = pt->numparams + (BCReg)(-(int)slot1); - if (frame_isvarg(frame)) { /* Vararg frame has been set up? (pc!=0) */ - nextframe = frame; - frame = frame_prevd(frame); - } - if (frame + slot1+LJ_FR2 < nextframe) { - *name = "(*vararg)"; - return frame+slot1; - } - } - } - return NULL; - } - if (pc != NO_BCPOS && - (*name = debug_varname(funcproto(fn), pc, slot1-1)) != NULL) - ; - else if (slot1 > 0 && frame + slot1+LJ_FR2 < nextframe) - *name = "(*temporary)"; - return frame+slot1; -} - -/* Get name of upvalue. */ -const char *lj_debug_uvname(GCproto *pt, uint32_t idx) -{ - const uint8_t *p = proto_uvinfo(pt); - lj_assertX(idx < pt->sizeuv, "bad upvalue index"); - if (!p) return ""; - if (idx) while (*p++ || --idx) ; - return (const char *)p; -} - -/* Get name and value of upvalue. */ -const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp, GCobj **op) -{ - if (tvisfunc(o)) { - GCfunc *fn = funcV(o); - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - if (idx < pt->sizeuv) { - GCobj *uvo = gcref(fn->l.uvptr[idx]); - *tvp = uvval(&uvo->uv); - *op = uvo; - return lj_debug_uvname(pt, idx); - } - } else { - if (idx < fn->c.nupvalues) { - *tvp = &fn->c.upvalue[idx]; - *op = obj2gco(fn); - return ""; - } - } - } - return NULL; -} - -/* Deduce name of an object from slot number and PC. */ -const char *lj_debug_slotname(GCproto *pt, const BCIns *ip, BCReg slot, - const char **name) -{ - const char *lname; -restart: - lname = debug_varname(pt, proto_bcpos(pt, ip), slot); - if (lname != NULL) { *name = lname; return "local"; } - while (--ip > proto_bc(pt)) { - BCIns ins = *ip; - BCOp op = bc_op(ins); - BCReg ra = bc_a(ins); - if (bcmode_a(op) == BCMbase) { - if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins))) - return NULL; - } else if (bcmode_a(op) == BCMdst && ra == slot) { - switch (bc_op(ins)) { - case BC_MOV: - if (ra == slot) { slot = bc_d(ins); goto restart; } - break; - case BC_GGET: - *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins)))); - return "global"; - case BC_TGETS: - *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins)))); - if (ip > proto_bc(pt)) { - BCIns insp = ip[-1]; - if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1+LJ_FR2 && - bc_d(insp) == bc_b(ins)) - return "method"; - } - return "field"; - case BC_UGET: - *name = lj_debug_uvname(pt, bc_d(ins)); - return "upvalue"; - default: - return NULL; - } - } - } - return NULL; -} - -/* Deduce function name from caller of a frame. */ -const char *lj_debug_funcname(lua_State *L, cTValue *frame, const char **name) -{ - cTValue *pframe; - GCfunc *fn; - BCPos pc; - if (frame <= tvref(L->stack)+LJ_FR2) - return NULL; - if (frame_isvarg(frame)) - frame = frame_prevd(frame); - pframe = frame_prev(frame); - fn = frame_func(pframe); - pc = debug_framepc(L, fn, frame); - if (pc != NO_BCPOS) { - GCproto *pt = funcproto(fn); - const BCIns *ip = &proto_bc(pt)[check_exp(pc < pt->sizebc, pc)]; - MMS mm = bcmode_mm(bc_op(*ip)); - if (mm == MM_call) { - BCReg slot = bc_a(*ip); - if (bc_op(*ip) == BC_ITERC) slot -= 3; - return lj_debug_slotname(pt, ip, slot, name); - } else if (mm != MM__MAX) { - *name = strdata(mmname_str(G(L), mm)); - return "metamethod"; - } - } - return NULL; -} - -/* -- Source code locations ----------------------------------------------- */ - -/* Generate shortened source name. */ -void lj_debug_shortname(char *out, GCstr *str, BCLine line) -{ - const char *src = strdata(str); - if (*src == '=') { - strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */ - out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */ - } else if (*src == '@') { /* Output "source", or "...source". */ - size_t len = str->len-1; - src++; /* Skip the `@' */ - if (len >= LUA_IDSIZE) { - src += len-(LUA_IDSIZE-4); /* Get last part of file name. */ - *out++ = '.'; *out++ = '.'; *out++ = '.'; - } - strcpy(out, src); - } else { /* Output [string "string"] or [builtin:name]. */ - size_t len; /* Length, up to first control char. */ - for (len = 0; len < LUA_IDSIZE-12; len++) - if (((const unsigned char *)src)[len] < ' ') break; - strcpy(out, line == ~(BCLine)0 ? "[builtin:" : "[string \""); out += 9; - if (src[len] != '\0') { /* Must truncate? */ - if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; - strncpy(out, src, len); out += len; - strcpy(out, "..."); out += 3; - } else { - strcpy(out, src); out += len; - } - strcpy(out, line == ~(BCLine)0 ? "]" : "\"]"); - } -} - -/* Add current location of a frame to error message. */ -void lj_debug_addloc(lua_State *L, const char *msg, - cTValue *frame, cTValue *nextframe) -{ - if (frame) { - GCfunc *fn = frame_func(frame); - if (isluafunc(fn)) { - BCLine line = debug_frameline(L, fn, nextframe); - if (line >= 0) { - GCproto *pt = funcproto(fn); - char buf[LUA_IDSIZE]; - lj_debug_shortname(buf, proto_chunkname(pt), pt->firstline); - lj_strfmt_pushf(L, "%s:%d: %s", buf, line, msg); - return; - } - } - } - lj_strfmt_pushf(L, "%s", msg); -} - -/* Push location string for a bytecode position to Lua stack. */ -void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) -{ - GCstr *name = proto_chunkname(pt); - const char *s = strdata(name); - MSize i, len = name->len; - BCLine line = lj_debug_line(pt, pc); - if (pt->firstline == ~(BCLine)0) { - lj_strfmt_pushf(L, "builtin:%s", s); - } else if (*s == '@') { - s++; len--; - for (i = len; i > 0; i--) - if (s[i] == '/' || s[i] == '\\') { - s += i+1; - break; - } - lj_strfmt_pushf(L, "%s:%d", s, line); - } else if (len > 40) { - lj_strfmt_pushf(L, "%p:%d", pt, line); - } else if (*s == '=') { - lj_strfmt_pushf(L, "%s:%d", s+1, line); - } else { - lj_strfmt_pushf(L, "\"%s\":%d", s, line); - } -} - -/* -- Public debug API ---------------------------------------------------- */ - -/* lua_getupvalue() and lua_setupvalue() are in lj_api.c. */ - -LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) -{ - const char *name = NULL; - if (ar) { - TValue *o = debug_localname(L, ar, &name, (BCReg)n); - if (name) { - copyTV(L, L->top, o); - incr_top(L); - } - } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) { - name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1); - } - return name; -} - -LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) -{ - const char *name = NULL; - TValue *o = debug_localname(L, ar, &name, (BCReg)n); - if (name) - copyTV(L, o, L->top-1); - L->top--; - return name; -} - -int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext) -{ - int opt_f = 0, opt_L = 0; - TValue *frame = NULL; - TValue *nextframe = NULL; - GCfunc *fn; - if (*what == '>') { - TValue *func = L->top - 1; - if (!tvisfunc(func)) return 0; - fn = funcV(func); - L->top--; - what++; - } else { - uint32_t offset = (uint32_t)ar->i_ci & 0xffff; - uint32_t size = (uint32_t)ar->i_ci >> 16; - lj_assertL(offset != 0, "bad frame offset"); - frame = tvref(L->stack) + offset; - if (size) nextframe = frame + size; - lj_assertL(frame <= tvref(L->maxstack) && - (!nextframe || nextframe <= tvref(L->maxstack)), - "broken frame chain"); - fn = frame_func(frame); - lj_assertL(fn->c.gct == ~LJ_TFUNC, "bad frame function"); - } - for (; *what; what++) { - if (*what == 'S') { - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - BCLine firstline = pt->firstline; - GCstr *name = proto_chunkname(pt); - ar->source = strdata(name); - lj_debug_shortname(ar->short_src, name, pt->firstline); - ar->linedefined = (int)firstline; - ar->lastlinedefined = (int)(firstline + pt->numline); - ar->what = (firstline || !pt->numline) ? "Lua" : "main"; - } else { - ar->source = "=[C]"; - ar->short_src[0] = '['; - ar->short_src[1] = 'C'; - ar->short_src[2] = ']'; - ar->short_src[3] = '\0'; - ar->linedefined = -1; - ar->lastlinedefined = -1; - ar->what = "C"; - } - } else if (*what == 'l') { - ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1; - } else if (*what == 'u') { - ar->nups = fn->c.nupvalues; - if (ext) { - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - ar->nparams = pt->numparams; - ar->isvararg = !!(pt->flags & PROTO_VARARG); - } else { - ar->nparams = 0; - ar->isvararg = 1; - } - } - } else if (*what == 'n') { - ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL; - if (ar->namewhat == NULL) { - ar->namewhat = ""; - ar->name = NULL; - } - } else if (*what == 'f') { - opt_f = 1; - } else if (*what == 'L') { - opt_L = 1; - } else { - return 0; /* Bad option. */ - } - } - if (opt_f) { - setfuncV(L, L->top, fn); - incr_top(L); - } - if (opt_L) { - if (isluafunc(fn)) { - GCtab *t = lj_tab_new(L, 0, 0); - GCproto *pt = funcproto(fn); - const void *lineinfo = proto_lineinfo(pt); - if (lineinfo) { - BCLine first = pt->firstline; - int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4; - MSize i, szl = pt->sizebc-1; - for (i = 0; i < szl; i++) { - BCLine line = first + - (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] : - sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] : - (BCLine)((const uint32_t *)lineinfo)[i]); - setboolV(lj_tab_setint(L, t, line), 1); - } - } - settabV(L, L->top, t); - } else { - setnilV(L->top); - } - incr_top(L); - } - return 1; /* Ok. */ -} - -LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) -{ - return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0); -} - -LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) -{ - int size; - cTValue *frame = lj_debug_frame(L, level, &size); - if (frame) { - ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack)); - return 1; - } else { - ar->i_ci = level - size; - return 0; - } -} - -#if LJ_HASPROFILE -/* Put the chunkname into a buffer. */ -static int debug_putchunkname(SBuf *sb, GCproto *pt, int pathstrip) -{ - GCstr *name = proto_chunkname(pt); - const char *p = strdata(name); - if (pt->firstline == ~(BCLine)0) { - lj_buf_putmem(sb, "[builtin:", 9); - lj_buf_putstr(sb, name); - lj_buf_putb(sb, ']'); - return 0; - } - if (*p == '=' || *p == '@') { - MSize len = name->len-1; - p++; - if (pathstrip) { - int i; - for (i = len-1; i >= 0; i--) - if (p[i] == '/' || p[i] == '\\') { - len -= i+1; - p = p+i+1; - break; - } - } - lj_buf_putmem(sb, p, len); - } else { - lj_buf_putmem(sb, "[string]", 8); - } - return 1; -} - -/* Put a compact stack dump into a buffer. */ -void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt, int depth) -{ - int level = 0, dir = 1, pathstrip = 1; - MSize lastlen = 0; - if (depth < 0) { level = ~depth; depth = dir = -1; } /* Reverse frames. */ - while (level != depth) { /* Loop through all frame. */ - int size; - cTValue *frame = lj_debug_frame(L, level, &size); - if (frame) { - cTValue *nextframe = size ? frame+size : NULL; - GCfunc *fn = frame_func(frame); - const uint8_t *p = (const uint8_t *)fmt; - int c; - while ((c = *p++)) { - switch (c) { - case 'p': /* Preserve full path. */ - pathstrip = 0; - break; - case 'F': case 'f': { /* Dump function name. */ - const char *name; - const char *what = lj_debug_funcname(L, frame, &name); - if (what) { - if (c == 'F' && isluafunc(fn)) { /* Dump module:name for 'F'. */ - GCproto *pt = funcproto(fn); - if (pt->firstline != ~(BCLine)0) { /* Not a bytecode builtin. */ - debug_putchunkname(sb, pt, pathstrip); - lj_buf_putb(sb, ':'); - } - } - lj_buf_putmem(sb, name, (MSize)strlen(name)); - break; - } /* else: can't derive a name, dump module:line. */ - } - /* fallthrough */ - case 'l': /* Dump module:line. */ - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - if (debug_putchunkname(sb, pt, pathstrip)) { - /* Regular Lua function. */ - BCLine line = c == 'l' ? debug_frameline(L, fn, nextframe) : - pt->firstline; - lj_buf_putb(sb, ':'); - lj_strfmt_putint(sb, line >= 0 ? line : pt->firstline); - } - } else if (isffunc(fn)) { /* Dump numbered builtins. */ - lj_buf_putmem(sb, "[builtin#", 9); - lj_strfmt_putint(sb, fn->c.ffid); - lj_buf_putb(sb, ']'); - } else { /* Dump C function address. */ - lj_buf_putb(sb, '@'); - lj_strfmt_putptr(sb, fn->c.f); - } - break; - case 'Z': /* Zap trailing separator. */ - lastlen = sbuflen(sb); - break; - default: - lj_buf_putb(sb, c); - break; - } - } - } else if (dir == 1) { - break; - } else { - level -= size; /* Reverse frame order: quickly skip missing level. */ - } - level += dir; - } - if (lastlen) - sb->w = sb->b + lastlen; /* Zap trailing separator. */ -} -#endif - -/* Number of frames for the leading and trailing part of a traceback. */ -#define TRACEBACK_LEVELS1 12 -#define TRACEBACK_LEVELS2 10 - -LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, - int level) -{ - int top = (int)(L->top - L->base); - int lim = TRACEBACK_LEVELS1; - lua_Debug ar; - if (msg) lua_pushfstring(L, "%s\n", msg); - lua_pushliteral(L, "stack traceback:"); - while (lua_getstack(L1, level++, &ar)) { - GCfunc *fn; - if (level > lim) { - if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) { - level--; - } else { - lua_pushliteral(L, "\n\t..."); - lua_getstack(L1, -10, &ar); - level = ar.i_ci - TRACEBACK_LEVELS2; - } - lim = 2147483647; - continue; - } - lua_getinfo(L1, "Snlf", &ar); - fn = funcV(L1->top-1); L1->top--; - if (isffunc(fn) && !*ar.namewhat) - lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid); - else - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - if (*ar.namewhat) { - lua_pushfstring(L, " in function " LUA_QS, ar.name); - } else { - if (*ar.what == 'm') { - lua_pushliteral(L, " in main chunk"); - } else if (*ar.what == 'C') { - lua_pushfstring(L, " at %p", fn->c.f); - } else { - lua_pushfstring(L, " in function <%s:%d>", - ar.short_src, ar.linedefined); - } - } - if ((int)(L->top - L->base) - top >= 15) - lua_concat(L, (int)(L->top - L->base) - top); - } - lua_concat(L, (int)(L->top - L->base) - top); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.h deleted file mode 100644 index 28127ae..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_debug.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -** Debugging and introspection. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_DEBUG_H -#define _LJ_DEBUG_H - -#include "lj_obj.h" - -typedef struct lj_Debug { - /* Common fields. Must be in the same order as in lua.h. */ - int event; - const char *name; - const char *namewhat; - const char *what; - const char *source; - int currentline; - int nups; - int linedefined; - int lastlinedefined; - char short_src[LUA_IDSIZE]; - int i_ci; - /* Extended fields. Only valid if lj_debug_getinfo() is called with ext = 1.*/ - int nparams; - int isvararg; -} lj_Debug; - -LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size); -LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc); -LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx); -LJ_FUNC const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp, - GCobj **op); -LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc, - BCReg slot, const char **name); -LJ_FUNC const char *lj_debug_funcname(lua_State *L, cTValue *frame, - const char **name); -LJ_FUNC void lj_debug_shortname(char *out, GCstr *str, BCLine line); -LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg, - cTValue *frame, cTValue *nextframe); -LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc); -LJ_FUNC int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, - int ext); -#if LJ_HASPROFILE -LJ_FUNC void lj_debug_dumpstack(lua_State *L, SBuf *sb, const char *fmt, - int depth); -#endif - -/* Fixed internal variable names. */ -#define VARNAMEDEF(_) \ - _(FOR_IDX, "(for index)") \ - _(FOR_STOP, "(for limit)") \ - _(FOR_STEP, "(for step)") \ - _(FOR_GEN, "(for generator)") \ - _(FOR_STATE, "(for state)") \ - _(FOR_CTL, "(for control)") - -enum { - VARNAME_END, -#define VARNAMEENUM(name, str) VARNAME_##name, - VARNAMEDEF(VARNAMEENUM) -#undef VARNAMEENUM - VARNAME__MAX -}; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_def.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_def.h deleted file mode 100644 index b61297a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_def.h +++ /dev/null @@ -1,381 +0,0 @@ -/* -** LuaJIT common internal definitions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_DEF_H -#define _LJ_DEF_H - -#include "lua.h" - -#if defined(_MSC_VER) && (_MSC_VER < 1700) -/* Old MSVC is stuck in the last century and doesn't have C99's stdint.h. */ -typedef __int8 int8_t; -typedef __int16 int16_t; -typedef __int32 int32_t; -typedef __int64 int64_t; -typedef unsigned __int8 uint8_t; -typedef unsigned __int16 uint16_t; -typedef unsigned __int32 uint32_t; -typedef unsigned __int64 uint64_t; -#ifdef _WIN64 -typedef __int64 intptr_t; -typedef unsigned __int64 uintptr_t; -#else -typedef __int32 intptr_t; -typedef unsigned __int32 uintptr_t; -#endif -#elif defined(__symbian__) -/* Cough. */ -typedef signed char int8_t; -typedef short int int16_t; -typedef int int32_t; -typedef long long int64_t; -typedef unsigned char uint8_t; -typedef unsigned short int uint16_t; -typedef unsigned int uint32_t; -typedef unsigned long long uint64_t; -typedef int intptr_t; -typedef unsigned int uintptr_t; -#else -#include -#endif - -/* Needed everywhere. */ -#include -#include - -/* Various VM limits. */ -#define LJ_MAX_MEM32 0x7fffff00 /* Max. 32 bit memory allocation. */ -#define LJ_MAX_MEM64 ((uint64_t)1<<47) /* Max. 64 bit memory allocation. */ -/* Max. total memory allocation. */ -#define LJ_MAX_MEM (LJ_GC64 ? LJ_MAX_MEM64 : LJ_MAX_MEM32) -#define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */ -#define LJ_MAX_STR LJ_MAX_MEM32 /* Max. string length. */ -#define LJ_MAX_BUF LJ_MAX_MEM32 /* Max. buffer length. */ -#define LJ_MAX_UDATA LJ_MAX_MEM32 /* Max. userdata length. */ - -#define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */ -#define LJ_MAX_HBITS 26 /* Max. hash bits. */ -#define LJ_MAX_ABITS 28 /* Max. bits of array key. */ -#define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */ -#define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */ - -#define LJ_MAX_LINE LJ_MAX_MEM32 /* Max. source code line number. */ -#define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */ -#define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */ -#define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */ -#define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */ -#define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */ - -#define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */ -#define LJ_STACK_EXTRA (5+2*LJ_FR2) /* Extra stack space (metamethods). */ - -#define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */ - -/* Minimum table/buffer sizes. */ -#define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */ -#define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */ -#define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */ -#define LJ_MIN_SBUF 32 /* Min. string buffer length. */ -#define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */ -#define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */ - -/* JIT compiler limits. */ -#define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */ -#define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */ -#define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */ - -/* Various macros. */ -#ifndef UNUSED -#define UNUSED(x) ((void)(x)) /* to avoid warnings */ -#endif - -#define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo) -#define i32ptr(p) ((int32_t)(intptr_t)(void *)(p)) -#define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p)) -#define i64ptr(p) ((int64_t)(intptr_t)(void *)(p)) -#define u64ptr(p) ((uint64_t)(intptr_t)(void *)(p)) -#define igcptr(p) (LJ_GC64 ? i64ptr(p) : i32ptr(p)) - -#define checki8(x) ((x) == (int32_t)(int8_t)(x)) -#define checku8(x) ((x) == (int32_t)(uint8_t)(x)) -#define checki16(x) ((x) == (int32_t)(int16_t)(x)) -#define checku16(x) ((x) == (int32_t)(uint16_t)(x)) -#define checki32(x) ((x) == (int32_t)(x)) -#define checku32(x) ((x) == (uint32_t)(x)) -#define checkptr31(x) (((uint64_t)(uintptr_t)(x) >> 31) == 0) -#define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x)) -#define checkptr47(x) (((uint64_t)(uintptr_t)(x) >> 47) == 0) -#define checkptrGC(x) (LJ_GC64 ? checkptr47((x)) : LJ_64 ? checkptr31((x)) :1) - -/* Every half-decent C compiler transforms this into a rotate instruction. */ -#define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1)))) -#define lj_ror(x, n) (((x)<<(-(int)(n)&(8*sizeof(x)-1))) | ((x)>>(n))) - -/* A really naive Bloom filter. But sufficient for our needs. */ -typedef uintptr_t BloomFilter; -#define BLOOM_MASK (8*sizeof(BloomFilter) - 1) -#define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK)) -#define bloomset(b, x) ((b) |= bloombit((x))) -#define bloomtest(b, x) ((b) & bloombit((x))) - -#if defined(__GNUC__) || defined(__clang__) || defined(__psp2__) - -#define LJ_NORET __attribute__((noreturn)) -#define LJ_ALIGN(n) __attribute__((aligned(n))) -#define LJ_INLINE inline -#define LJ_AINLINE inline __attribute__((always_inline)) -#define LJ_NOINLINE __attribute__((noinline)) - -#if defined(__ELF__) || defined(__MACH__) || defined(__psp2__) -#if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__)) -#define LJ_NOAPI extern __attribute__((visibility("hidden"))) -#endif -#endif - -/* Note: it's only beneficial to use fastcall on x86 and then only for up to -** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only -** indirect calls and related tail-called C functions are marked as fastcall. -*/ -#if defined(__i386__) -#define LJ_FASTCALL __attribute__((fastcall)) -#endif - -#define LJ_LIKELY(x) __builtin_expect(!!(x), 1) -#define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0) - -#define lj_ffs(x) ((uint32_t)__builtin_ctz(x)) -/* Don't ask ... */ -#if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__)) -static LJ_AINLINE uint32_t lj_fls(uint32_t x) -{ - uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r; -} -#else -#define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31)) -#endif - -#if defined(__arm__) -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ -#if defined(__psp2__) - return __builtin_rev(x); -#else - uint32_t r; -#if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\ - __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ - __asm__("rev %0, %1" : "=r" (r) : "r" (x)); - return r; -#else -#ifdef __thumb__ - r = x ^ lj_ror(x, 16); -#else - __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x)); -#endif - return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8); -#endif -#endif -} - -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); -} -#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __clang__ -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - return (uint32_t)__builtin_bswap32((int32_t)x); -} - -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return (uint64_t)__builtin_bswap64((int64_t)x); -} -#elif defined(__i386__) || defined(__x86_64__) -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; -} - -#if defined(__i386__) -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); -} -#else -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; -} -#endif -#else -static LJ_AINLINE uint32_t lj_bswap(uint32_t x) -{ - return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24); -} - -static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) -{ - return (uint64_t)lj_bswap((uint32_t)(x >> 32)) | - ((uint64_t)lj_bswap((uint32_t)x) << 32); -} -#endif - -typedef union __attribute__((packed)) Unaligned16 { - uint16_t u; - uint8_t b[2]; -} Unaligned16; - -typedef union __attribute__((packed)) Unaligned32 { - uint32_t u; - uint8_t b[4]; -} Unaligned32; - -/* Unaligned load of uint16_t. */ -static LJ_AINLINE uint16_t lj_getu16(const void *p) -{ - return ((const Unaligned16 *)p)->u; -} - -/* Unaligned load of uint32_t. */ -static LJ_AINLINE uint32_t lj_getu32(const void *p) -{ - return ((const Unaligned32 *)p)->u; -} - -#elif defined(_MSC_VER) - -#define LJ_NORET __declspec(noreturn) -#define LJ_ALIGN(n) __declspec(align(n)) -#define LJ_INLINE __inline -#define LJ_AINLINE __forceinline -#define LJ_NOINLINE __declspec(noinline) -#if defined(_M_IX86) -#define LJ_FASTCALL __fastcall -#endif - -#ifdef _M_PPC -unsigned int _CountLeadingZeros(long); -#pragma intrinsic(_CountLeadingZeros) -static LJ_AINLINE uint32_t lj_fls(uint32_t x) -{ - return _CountLeadingZeros(x) ^ 31; -} -#else -unsigned char _BitScanForward(unsigned long *, unsigned long); -unsigned char _BitScanReverse(unsigned long *, unsigned long); -#pragma intrinsic(_BitScanForward) -#pragma intrinsic(_BitScanReverse) - -static LJ_AINLINE uint32_t lj_ffs(uint32_t x) -{ - unsigned long r; _BitScanForward(&r, x); return (uint32_t)r; -} - -static LJ_AINLINE uint32_t lj_fls(uint32_t x) -{ - unsigned long r; _BitScanReverse(&r, x); return (uint32_t)r; -} -#endif - -unsigned long _byteswap_ulong(unsigned long); -uint64_t _byteswap_uint64(uint64_t); -#define lj_bswap(x) (_byteswap_ulong((x))) -#define lj_bswap64(x) (_byteswap_uint64((x))) - -#if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED) -/* -** Replacement for unaligned loads on Xbox 360. Disabled by default since it's -** usually more costly than the occasional stall when crossing a cache-line. -*/ -static LJ_AINLINE uint16_t lj_getu16(const void *v) -{ - const uint8_t *p = (const uint8_t *)v; - return (uint16_t)((p[0]<<8) | p[1]); -} -static LJ_AINLINE uint32_t lj_getu32(const void *v) -{ - const uint8_t *p = (const uint8_t *)v; - return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]); -} -#else -/* Unaligned loads are generally ok on x86/x64. */ -#define lj_getu16(p) (*(uint16_t *)(p)) -#define lj_getu32(p) (*(uint32_t *)(p)) -#endif - -#else -#error "missing defines for your compiler" -#endif - -/* Optional defines. */ -#ifndef LJ_FASTCALL -#define LJ_FASTCALL -#endif -#ifndef LJ_NORET -#define LJ_NORET -#endif -#ifndef LJ_NOAPI -#define LJ_NOAPI extern -#endif -#ifndef LJ_LIKELY -#define LJ_LIKELY(x) (x) -#define LJ_UNLIKELY(x) (x) -#endif - -/* Attributes for internal functions. */ -#define LJ_DATA LJ_NOAPI -#define LJ_DATADEF -#define LJ_ASMF LJ_NOAPI -#define LJ_FUNCA LJ_NOAPI -#if defined(ljamalg_c) -#define LJ_FUNC static -#else -#define LJ_FUNC LJ_NOAPI -#endif -#define LJ_FUNC_NORET LJ_FUNC LJ_NORET -#define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET -#define LJ_ASMF_NORET LJ_ASMF LJ_NORET - -/* Internal assertions. */ -#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) -#define lj_assert_check(g, c, ...) \ - ((c) ? (void)0 : \ - (lj_assert_fail((g), __FILE__, __LINE__, __func__, __VA_ARGS__), 0)) -#define lj_checkapi(c, ...) lj_assert_check(G(L), (c), __VA_ARGS__) -#else -#define lj_checkapi(c, ...) ((void)L) -#endif - -#ifdef LUA_USE_ASSERT -#define lj_assertG_(g, c, ...) lj_assert_check((g), (c), __VA_ARGS__) -#define lj_assertG(c, ...) lj_assert_check(g, (c), __VA_ARGS__) -#define lj_assertL(c, ...) lj_assert_check(G(L), (c), __VA_ARGS__) -#define lj_assertX(c, ...) lj_assert_check(NULL, (c), __VA_ARGS__) -#define check_exp(c, e) (lj_assertX((c), #c), (e)) -#else -#define lj_assertG_(g, c, ...) ((void)0) -#define lj_assertG(c, ...) ((void)g) -#define lj_assertL(c, ...) ((void)L) -#define lj_assertX(c, ...) ((void)0) -#define check_exp(c, e) (e) -#endif - -/* Static assertions. */ -#define LJ_ASSERT_NAME2(name, line) name ## line -#define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line) -#ifdef __COUNTER__ -#define LJ_STATIC_ASSERT(cond) \ - extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) -#else -#define LJ_STATIC_ASSERT(cond) \ - extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) -#endif - -/* PRNG state. Need this here, details in lj_prng.h. */ -typedef struct PRNGState { - uint64_t u[4]; -} PRNGState; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.c deleted file mode 100644 index ded382a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.c +++ /dev/null @@ -1,559 +0,0 @@ -/* -** Instruction dispatch handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_dispatch_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_func.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_debug.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_ff.h" -#include "lj_strfmt.h" -#if LJ_HASJIT -#include "lj_jit.h" -#endif -#if LJ_HASFFI -#include "lj_ccallback.h" -#endif -#include "lj_trace.h" -#include "lj_dispatch.h" -#if LJ_HASPROFILE -#include "lj_profile.h" -#endif -#include "lj_vm.h" -#include "luajit.h" - -/* Bump GG_NUM_ASMFF in lj_dispatch.h as needed. Ugly. */ -LJ_STATIC_ASSERT(GG_NUM_ASMFF == FF_NUM_ASMFUNC); - -/* -- Dispatch table management ------------------------------------------- */ - -#if LJ_TARGET_MIPS -#include -LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, - lua_State *co); -#if !LJ_HASJIT -#define lj_dispatch_stitch lj_dispatch_ins -#endif -#if !LJ_HASPROFILE -#define lj_dispatch_profile lj_dispatch_ins -#endif - -#define GOTFUNC(name) (ASMFunction)name, -static const ASMFunction dispatch_got[] = { - GOTDEF(GOTFUNC) -}; -#undef GOTFUNC -#endif - -/* Initialize instruction dispatch table and hot counters. */ -void lj_dispatch_init(GG_State *GG) -{ - uint32_t i; - ASMFunction *disp = GG->dispatch; - for (i = 0; i < GG_LEN_SDISP; i++) - disp[GG_LEN_DDISP+i] = disp[i] = makeasmfunc(lj_bc_ofs[i]); - for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) - disp[i] = makeasmfunc(lj_bc_ofs[i]); - /* The JIT engine is off by default. luaopen_jit() turns it on. */ - disp[BC_FORL] = disp[BC_IFORL]; - disp[BC_ITERL] = disp[BC_IITERL]; - /* Workaround for stable v2.1 bytecode. TODO: Replace with BC_IITERN. */ - disp[BC_ITERN] = &lj_vm_IITERN; - disp[BC_LOOP] = disp[BC_ILOOP]; - disp[BC_FUNCF] = disp[BC_IFUNCF]; - disp[BC_FUNCV] = disp[BC_IFUNCV]; - GG->g.bc_cfunc_ext = GG->g.bc_cfunc_int = BCINS_AD(BC_FUNCC, LUA_MINSTACK, 0); - for (i = 0; i < GG_NUM_ASMFF; i++) - GG->bcff[i] = BCINS_AD(BC__MAX+i, 0, 0); -#if LJ_TARGET_MIPS - memcpy(GG->got, dispatch_got, LJ_GOT__MAX*sizeof(ASMFunction *)); -#endif -} - -#if LJ_HASJIT -/* Initialize hotcount table. */ -void lj_dispatch_init_hotcount(global_State *g) -{ - int32_t hotloop = G2J(g)->param[JIT_P_hotloop]; - HotCount start = (HotCount)(hotloop*HOTCOUNT_LOOP - 1); - HotCount *hotcount = G2GG(g)->hotcount; - uint32_t i; - for (i = 0; i < HOTCOUNT_SIZE; i++) - hotcount[i] = start; -} -#endif - -/* Internal dispatch mode bits. */ -#define DISPMODE_CALL 0x01 /* Override call dispatch. */ -#define DISPMODE_RET 0x02 /* Override return dispatch. */ -#define DISPMODE_INS 0x04 /* Override instruction dispatch. */ -#define DISPMODE_JIT 0x10 /* JIT compiler on. */ -#define DISPMODE_REC 0x20 /* Recording active. */ -#define DISPMODE_PROF 0x40 /* Profiling active. */ - -/* Update dispatch table depending on various flags. */ -void lj_dispatch_update(global_State *g) -{ - uint8_t oldmode = g->dispatchmode; - uint8_t mode = 0; -#if LJ_HASJIT - mode |= (G2J(g)->flags & JIT_F_ON) ? DISPMODE_JIT : 0; - mode |= G2J(g)->state != LJ_TRACE_IDLE ? - (DISPMODE_REC|DISPMODE_INS|DISPMODE_CALL) : 0; -#endif -#if LJ_HASPROFILE - mode |= (g->hookmask & HOOK_PROFILE) ? (DISPMODE_PROF|DISPMODE_INS) : 0; -#endif - mode |= (g->hookmask & (LUA_MASKLINE|LUA_MASKCOUNT)) ? DISPMODE_INS : 0; - mode |= (g->hookmask & LUA_MASKCALL) ? DISPMODE_CALL : 0; - mode |= (g->hookmask & LUA_MASKRET) ? DISPMODE_RET : 0; - if (oldmode != mode) { /* Mode changed? */ - ASMFunction *disp = G2GG(g)->dispatch; - ASMFunction f_forl, f_iterl, f_itern, f_loop, f_funcf, f_funcv; - g->dispatchmode = mode; - - /* Hotcount if JIT is on, but not while recording. */ - if ((mode & (DISPMODE_JIT|DISPMODE_REC)) == DISPMODE_JIT) { - f_forl = makeasmfunc(lj_bc_ofs[BC_FORL]); - f_iterl = makeasmfunc(lj_bc_ofs[BC_ITERL]); - f_itern = makeasmfunc(lj_bc_ofs[BC_ITERN]); - f_loop = makeasmfunc(lj_bc_ofs[BC_LOOP]); - f_funcf = makeasmfunc(lj_bc_ofs[BC_FUNCF]); - f_funcv = makeasmfunc(lj_bc_ofs[BC_FUNCV]); - } else { /* Otherwise use the non-hotcounting instructions. */ - f_forl = disp[GG_LEN_DDISP+BC_IFORL]; - f_iterl = disp[GG_LEN_DDISP+BC_IITERL]; - f_itern = &lj_vm_IITERN; - f_loop = disp[GG_LEN_DDISP+BC_ILOOP]; - f_funcf = makeasmfunc(lj_bc_ofs[BC_IFUNCF]); - f_funcv = makeasmfunc(lj_bc_ofs[BC_IFUNCV]); - } - /* Init static counting instruction dispatch first (may be copied below). */ - disp[GG_LEN_DDISP+BC_FORL] = f_forl; - disp[GG_LEN_DDISP+BC_ITERL] = f_iterl; - disp[GG_LEN_DDISP+BC_ITERN] = f_itern; - disp[GG_LEN_DDISP+BC_LOOP] = f_loop; - - /* Set dynamic instruction dispatch. */ - if ((oldmode ^ mode) & (DISPMODE_PROF|DISPMODE_REC|DISPMODE_INS)) { - /* Need to update the whole table. */ - if (!(mode & DISPMODE_INS)) { /* No ins dispatch? */ - /* Copy static dispatch table to dynamic dispatch table. */ - memcpy(&disp[0], &disp[GG_LEN_DDISP], GG_LEN_SDISP*sizeof(ASMFunction)); - /* Overwrite with dynamic return dispatch. */ - if ((mode & DISPMODE_RET)) { - disp[BC_RETM] = lj_vm_rethook; - disp[BC_RET] = lj_vm_rethook; - disp[BC_RET0] = lj_vm_rethook; - disp[BC_RET1] = lj_vm_rethook; - } - } else { - /* The recording dispatch also checks for hooks. */ - ASMFunction f = (mode & DISPMODE_PROF) ? lj_vm_profhook : - (mode & DISPMODE_REC) ? lj_vm_record : lj_vm_inshook; - uint32_t i; - for (i = 0; i < GG_LEN_SDISP; i++) - disp[i] = f; - } - } else if (!(mode & DISPMODE_INS)) { - /* Otherwise set dynamic counting ins. */ - disp[BC_FORL] = f_forl; - disp[BC_ITERL] = f_iterl; - disp[BC_ITERN] = f_itern; - disp[BC_LOOP] = f_loop; - /* Set dynamic return dispatch. */ - if ((mode & DISPMODE_RET)) { - disp[BC_RETM] = lj_vm_rethook; - disp[BC_RET] = lj_vm_rethook; - disp[BC_RET0] = lj_vm_rethook; - disp[BC_RET1] = lj_vm_rethook; - } else { - disp[BC_RETM] = disp[GG_LEN_DDISP+BC_RETM]; - disp[BC_RET] = disp[GG_LEN_DDISP+BC_RET]; - disp[BC_RET0] = disp[GG_LEN_DDISP+BC_RET0]; - disp[BC_RET1] = disp[GG_LEN_DDISP+BC_RET1]; - } - } - - /* Set dynamic call dispatch. */ - if ((oldmode ^ mode) & DISPMODE_CALL) { /* Update the whole table? */ - uint32_t i; - if ((mode & DISPMODE_CALL) == 0) { /* No call hooks? */ - for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) - disp[i] = makeasmfunc(lj_bc_ofs[i]); - } else { - for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) - disp[i] = lj_vm_callhook; - } - } - if (!(mode & DISPMODE_CALL)) { /* Overwrite dynamic counting ins. */ - disp[BC_FUNCF] = f_funcf; - disp[BC_FUNCV] = f_funcv; - } - -#if LJ_HASJIT - /* Reset hotcounts for JIT off to on transition. */ - if ((mode & DISPMODE_JIT) && !(oldmode & DISPMODE_JIT)) - lj_dispatch_init_hotcount(g); -#endif - } -} - -/* -- JIT mode setting ---------------------------------------------------- */ - -#if LJ_HASJIT -/* Set JIT mode for a single prototype. */ -static void setptmode(global_State *g, GCproto *pt, int mode) -{ - if ((mode & LUAJIT_MODE_ON)) { /* (Re-)enable JIT compilation. */ - pt->flags &= ~PROTO_NOJIT; - lj_trace_reenableproto(pt); /* Unpatch all ILOOP etc. bytecodes. */ - } else { /* Flush and/or disable JIT compilation. */ - if (!(mode & LUAJIT_MODE_FLUSH)) - pt->flags |= PROTO_NOJIT; - lj_trace_flushproto(g, pt); /* Flush all traces of prototype. */ - } -} - -/* Recursively set the JIT mode for all children of a prototype. */ -static void setptmode_all(global_State *g, GCproto *pt, int mode) -{ - ptrdiff_t i; - if (!(pt->flags & PROTO_CHILD)) return; - for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) { - GCobj *o = proto_kgc(pt, i); - if (o->gch.gct == ~LJ_TPROTO) { - setptmode(g, gco2pt(o), mode); - setptmode_all(g, gco2pt(o), mode); - } - } -} -#endif - -/* Public API function: control the JIT engine. */ -int luaJIT_setmode(lua_State *L, int idx, int mode) -{ - global_State *g = G(L); - int mm = mode & LUAJIT_MODE_MASK; - lj_trace_abort(g); /* Abort recording on any state change. */ - /* Avoid pulling the rug from under our own feet. */ - if ((g->hookmask & HOOK_GC)) - lj_err_caller(L, LJ_ERR_NOGCMM); - switch (mm) { -#if LJ_HASJIT - case LUAJIT_MODE_ENGINE: - if ((mode & LUAJIT_MODE_FLUSH)) { - lj_trace_flushall(L); - } else { - if (!(mode & LUAJIT_MODE_ON)) - G2J(g)->flags &= ~(uint32_t)JIT_F_ON; - else - G2J(g)->flags |= (uint32_t)JIT_F_ON; - lj_dispatch_update(g); - } - break; - case LUAJIT_MODE_FUNC: - case LUAJIT_MODE_ALLFUNC: - case LUAJIT_MODE_ALLSUBFUNC: { - cTValue *tv = idx == 0 ? frame_prev(L->base-1)-LJ_FR2 : - idx > 0 ? L->base + (idx-1) : L->top + idx; - GCproto *pt; - if ((idx == 0 || tvisfunc(tv)) && isluafunc(&gcval(tv)->fn)) - pt = funcproto(&gcval(tv)->fn); /* Cannot use funcV() for frame slot. */ - else if (tvisproto(tv)) - pt = protoV(tv); - else - return 0; /* Failed. */ - if (mm != LUAJIT_MODE_ALLSUBFUNC) - setptmode(g, pt, mode); - if (mm != LUAJIT_MODE_FUNC) - setptmode_all(g, pt, mode); - break; - } - case LUAJIT_MODE_TRACE: - if (!(mode & LUAJIT_MODE_FLUSH)) - return 0; /* Failed. */ - lj_trace_flush(G2J(g), idx); - break; -#else - case LUAJIT_MODE_ENGINE: - case LUAJIT_MODE_FUNC: - case LUAJIT_MODE_ALLFUNC: - case LUAJIT_MODE_ALLSUBFUNC: - UNUSED(idx); - if ((mode & LUAJIT_MODE_ON)) - return 0; /* Failed. */ - break; -#endif - case LUAJIT_MODE_WRAPCFUNC: - if ((mode & LUAJIT_MODE_ON)) { - if (idx != 0) { - cTValue *tv = idx > 0 ? L->base + (idx-1) : L->top + idx; - if (tvislightud(tv)) - g->wrapf = (lua_CFunction)lightudV(g, tv); - else - return 0; /* Failed. */ - } else { - return 0; /* Failed. */ - } - g->bc_cfunc_ext = BCINS_AD(BC_FUNCCW, 0, 0); - } else { - g->bc_cfunc_ext = BCINS_AD(BC_FUNCC, 0, 0); - } - break; - default: - return 0; /* Failed. */ - } - return 1; /* OK. */ -} - -/* Enforce (dynamic) linker error for version mismatches. See luajit.c. */ -LUA_API void LUAJIT_VERSION_SYM(void) -{ -} - -/* -- Hooks --------------------------------------------------------------- */ - -/* This function can be called asynchronously (e.g. during a signal). */ -LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count) -{ - global_State *g = G(L); - mask &= HOOK_EVENTMASK; - if (func == NULL || mask == 0) { mask = 0; func = NULL; } /* Consistency. */ - g->hookf = func; - g->hookcount = g->hookcstart = (int32_t)count; - g->hookmask = (uint8_t)((g->hookmask & ~HOOK_EVENTMASK) | mask); - lj_trace_abort(g); /* Abort recording on any hook change. */ - lj_dispatch_update(g); - return 1; -} - -LUA_API lua_Hook lua_gethook(lua_State *L) -{ - return G(L)->hookf; -} - -LUA_API int lua_gethookmask(lua_State *L) -{ - return G(L)->hookmask & HOOK_EVENTMASK; -} - -LUA_API int lua_gethookcount(lua_State *L) -{ - return (int)G(L)->hookcstart; -} - -/* Call a hook. */ -static void callhook(lua_State *L, int event, BCLine line) -{ - global_State *g = G(L); - lua_Hook hookf = g->hookf; - if (hookf && !hook_active(g)) { - lua_Debug ar; - lj_trace_abort(g); /* Abort recording on any hook call. */ - ar.event = event; - ar.currentline = line; - /* Top frame, nextframe = NULL. */ - ar.i_ci = (int)((L->base-1) - tvref(L->stack)); - lj_state_checkstack(L, 1+LUA_MINSTACK); -#if LJ_HASPROFILE && !LJ_PROFILE_SIGPROF - lj_profile_hook_enter(g); -#else - hook_enter(g); -#endif - hookf(L, &ar); - lj_assertG(hook_active(g), "active hook flag removed"); - setgcref(g->cur_L, obj2gco(L)); -#if LJ_HASPROFILE && !LJ_PROFILE_SIGPROF - lj_profile_hook_leave(g); -#else - hook_leave(g); -#endif - } -} - -/* -- Dispatch callbacks -------------------------------------------------- */ - -/* Calculate number of used stack slots in the current frame. */ -static BCReg cur_topslot(GCproto *pt, const BCIns *pc, uint32_t nres) -{ - BCIns ins = pc[-1]; - if (bc_op(ins) == BC_UCLO) - ins = pc[bc_j(ins)]; - switch (bc_op(ins)) { - case BC_CALLM: case BC_CALLMT: return bc_a(ins) + bc_c(ins) + nres-1+1+LJ_FR2; - case BC_RETM: return bc_a(ins) + bc_d(ins) + nres-1; - case BC_TSETM: return bc_a(ins) + nres-1; - default: return pt->framesize; - } -} - -/* Instruction dispatch. Used by instr/line/return hooks or when recording. */ -void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc) -{ - ERRNO_SAVE - GCfunc *fn = curr_func(L); - GCproto *pt = funcproto(fn); - void *cf = cframe_raw(L->cframe); - const BCIns *oldpc = cframe_pc(cf); - global_State *g = G(L); - BCReg slots; - setcframe_pc(cf, pc); - slots = cur_topslot(pt, pc, cframe_multres_n(cf)); - L->top = L->base + slots; /* Fix top. */ -#if LJ_HASJIT - { - jit_State *J = G2J(g); - if (J->state != LJ_TRACE_IDLE) { -#ifdef LUA_USE_ASSERT - ptrdiff_t delta = L->top - L->base; -#endif - J->L = L; - lj_trace_ins(J, pc-1); /* The interpreter bytecode PC is offset by 1. */ - lj_assertG(L->top - L->base == delta, - "unbalanced stack after tracing of instruction"); - } - } -#endif - if ((g->hookmask & LUA_MASKCOUNT) && g->hookcount == 0) { - g->hookcount = g->hookcstart; - callhook(L, LUA_HOOKCOUNT, -1); - L->top = L->base + slots; /* Fix top again. */ - } - if ((g->hookmask & LUA_MASKLINE)) { - BCPos npc = proto_bcpos(pt, pc) - 1; - BCPos opc = proto_bcpos(pt, oldpc) - 1; - BCLine line = lj_debug_line(pt, npc); - if (pc <= oldpc || opc >= pt->sizebc || line != lj_debug_line(pt, opc)) { - callhook(L, LUA_HOOKLINE, line); - L->top = L->base + slots; /* Fix top again. */ - } - } - if ((g->hookmask & LUA_MASKRET) && bc_isret(bc_op(pc[-1]))) - callhook(L, LUA_HOOKRET, -1); - ERRNO_RESTORE -} - -/* Initialize call. Ensure stack space and return # of missing parameters. */ -static int call_init(lua_State *L, GCfunc *fn) -{ - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - int numparams = pt->numparams; - int gotparams = (int)(L->top - L->base); - int need = pt->framesize; - if ((pt->flags & PROTO_VARARG)) need += 1+gotparams; - lj_state_checkstack(L, (MSize)need); - numparams -= gotparams; - return numparams >= 0 ? numparams : 0; - } else { - lj_state_checkstack(L, LUA_MINSTACK); - return 0; - } -} - -/* Call dispatch. Used by call hooks, hot calls or when recording. */ -ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns *pc) -{ - ERRNO_SAVE - GCfunc *fn = curr_func(L); - BCOp op; - global_State *g = G(L); -#if LJ_HASJIT - jit_State *J = G2J(g); -#endif - int missing = call_init(L, fn); -#if LJ_HASJIT - J->L = L; - if ((uintptr_t)pc & 1) { /* Marker for hot call. */ -#ifdef LUA_USE_ASSERT - ptrdiff_t delta = L->top - L->base; -#endif - pc = (const BCIns *)((uintptr_t)pc & ~(uintptr_t)1); - lj_trace_hot(J, pc); - lj_assertG(L->top - L->base == delta, - "unbalanced stack after hot call"); - goto out; - } else if (J->state != LJ_TRACE_IDLE && - !(g->hookmask & (HOOK_GC|HOOK_VMEVENT))) { -#ifdef LUA_USE_ASSERT - ptrdiff_t delta = L->top - L->base; -#endif - /* Record the FUNC* bytecodes, too. */ - lj_trace_ins(J, pc-1); /* The interpreter bytecode PC is offset by 1. */ - lj_assertG(L->top - L->base == delta, - "unbalanced stack after hot instruction"); - } -#endif - if ((g->hookmask & LUA_MASKCALL)) { - int i; - for (i = 0; i < missing; i++) /* Add missing parameters. */ - setnilV(L->top++); - callhook(L, LUA_HOOKCALL, -1); - /* Preserve modifications of missing parameters by lua_setlocal(). */ - while (missing-- > 0 && tvisnil(L->top - 1)) - L->top--; - } -#if LJ_HASJIT -out: -#endif - op = bc_op(pc[-1]); /* Get FUNC* op. */ -#if LJ_HASJIT - /* Use the non-hotcounting variants if JIT is off or while recording. */ - if ((!(J->flags & JIT_F_ON) || J->state != LJ_TRACE_IDLE) && - (op == BC_FUNCF || op == BC_FUNCV)) - op = (BCOp)((int)op+(int)BC_IFUNCF-(int)BC_FUNCF); -#endif - ERRNO_RESTORE - return makeasmfunc(lj_bc_ofs[op]); /* Return static dispatch target. */ -} - -#if LJ_HASJIT -/* Stitch a new trace. */ -void LJ_FASTCALL lj_dispatch_stitch(jit_State *J, const BCIns *pc) -{ - ERRNO_SAVE - lua_State *L = J->L; - void *cf = cframe_raw(L->cframe); - const BCIns *oldpc = cframe_pc(cf); - setcframe_pc(cf, pc); - /* Before dispatch, have to bias PC by 1. */ - L->top = L->base + cur_topslot(curr_proto(L), pc+1, cframe_multres_n(cf)); - lj_trace_stitch(J, pc-1); /* Point to the CALL instruction. */ - setcframe_pc(cf, oldpc); - ERRNO_RESTORE -} -#endif - -#if LJ_HASPROFILE -/* Profile dispatch. */ -void LJ_FASTCALL lj_dispatch_profile(lua_State *L, const BCIns *pc) -{ - ERRNO_SAVE - GCfunc *fn = curr_func(L); - GCproto *pt = funcproto(fn); - void *cf = cframe_raw(L->cframe); - const BCIns *oldpc = cframe_pc(cf); - global_State *g; - setcframe_pc(cf, pc); - L->top = L->base + cur_topslot(pt, pc, cframe_multres_n(cf)); - lj_profile_interpreter(L); - setcframe_pc(cf, oldpc); - g = G(L); - setgcref(g->cur_L, obj2gco(L)); - setvmstate(g, INTERP); - ERRNO_RESTORE -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.h deleted file mode 100644 index 52762ee..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_dispatch.h +++ /dev/null @@ -1,164 +0,0 @@ -/* -** Instruction dispatch handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_DISPATCH_H -#define _LJ_DISPATCH_H - -#include "lj_obj.h" -#include "lj_bc.h" -#if LJ_HASJIT -#include "lj_jit.h" -#endif - -#if LJ_TARGET_MIPS -/* Need our own global offset table for the dreaded MIPS calling conventions. */ - -#ifndef _LJ_VM_H -LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b); -#endif - -#if LJ_SOFTFP -#ifndef _LJ_IRCALL_H -extern double __adddf3(double a, double b); -extern double __subdf3(double a, double b); -extern double __muldf3(double a, double b); -extern double __divdf3(double a, double b); -#endif -#define SFGOTDEF(_) _(sqrt) _(__adddf3) _(__subdf3) _(__muldf3) _(__divdf3) -#else -#define SFGOTDEF(_) -#endif -#if LJ_HASJIT -#define JITGOTDEF(_) _(lj_err_trace) _(lj_trace_exit) _(lj_trace_hot) -#else -#define JITGOTDEF(_) -#endif -#if LJ_HASFFI -#define FFIGOTDEF(_) \ - _(lj_meta_equal_cd) _(lj_ccallback_enter) _(lj_ccallback_leave) -#else -#define FFIGOTDEF(_) -#endif -#define GOTDEF(_) \ - _(floor) _(ceil) _(trunc) _(log) _(log10) _(exp) _(sin) _(cos) _(tan) \ - _(asin) _(acos) _(atan) _(sinh) _(cosh) _(tanh) _(frexp) _(modf) _(atan2) \ - _(pow) _(fmod) _(ldexp) _(lj_vm_modi) \ - _(lj_dispatch_call) _(lj_dispatch_ins) _(lj_dispatch_stitch) \ - _(lj_dispatch_profile) _(lj_err_throw) \ - _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \ - _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \ - _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \ - _(lj_meta_for) _(lj_meta_istype) _(lj_meta_len) _(lj_meta_tget) \ - _(lj_meta_tset) _(lj_state_growstack) _(lj_strfmt_number) \ - _(lj_str_new) _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) \ - _(lj_tab_new) _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \ - _(lj_tab_setinth) _(lj_buf_putstr_reverse) _(lj_buf_putstr_lower) \ - _(lj_buf_putstr_upper) _(lj_buf_tostr) \ - JITGOTDEF(_) FFIGOTDEF(_) SFGOTDEF(_) - -enum { -#define GOTENUM(name) LJ_GOT_##name, -GOTDEF(GOTENUM) -#undef GOTENUM - LJ_GOT__MAX -}; -#endif - -/* Type of hot counter. Must match the code in the assembler VM. */ -/* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */ -typedef uint16_t HotCount; - -/* Number of hot counter hash table entries (must be a power of two). */ -#define HOTCOUNT_SIZE 64 -#define HOTCOUNT_PCMASK ((HOTCOUNT_SIZE-1)*sizeof(HotCount)) - -/* Hotcount decrements. */ -#define HOTCOUNT_LOOP 2 -#define HOTCOUNT_CALL 1 - -/* This solves a circular dependency problem -- bump as needed. Sigh. */ -#define GG_NUM_ASMFF 57 - -#define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF) -#define GG_LEN_SDISP BC_FUNCF -#define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP) - -/* Global state, main thread and extra fields are allocated together. */ -typedef struct GG_State { - lua_State L; /* Main thread. */ - global_State g; /* Global state. */ -#if LJ_TARGET_ARM && !LJ_TARGET_NX - /* Make g reachable via K12 encoded DISPATCH-relative addressing. */ - uint8_t align1[(16-sizeof(global_State))&15]; -#endif -#if LJ_TARGET_MIPS - ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */ -#endif -#if LJ_HASJIT - jit_State J; /* JIT state. */ - HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */ -#if LJ_TARGET_ARM && !LJ_TARGET_NX - /* Ditto for J. */ - uint8_t align2[(16-sizeof(jit_State)-sizeof(HotCount)*HOTCOUNT_SIZE)&15]; -#endif -#endif - ASMFunction dispatch[GG_LEN_DISP]; /* Instruction dispatch tables. */ - BCIns bcff[GG_NUM_ASMFF]; /* Bytecode for ASM fast functions. */ -} GG_State; - -#define GG_OFS(field) ((int)offsetof(GG_State, field)) -#define G2GG(gl) ((GG_State *)((char *)(gl) - GG_OFS(g))) -#define J2GG(j) ((GG_State *)((char *)(j) - GG_OFS(J))) -#define L2GG(L) (G2GG(G(L))) -#define J2G(J) (&J2GG(J)->g) -#define G2J(gl) (&G2GG(gl)->J) -#define L2J(L) (&L2GG(L)->J) -#define GG_G2J (GG_OFS(J) - GG_OFS(g)) -#define GG_G2DISP (GG_OFS(dispatch) - GG_OFS(g)) -#define GG_DISP2G (GG_OFS(g) - GG_OFS(dispatch)) -#define GG_DISP2J (GG_OFS(J) - GG_OFS(dispatch)) -#define GG_DISP2HOT (GG_OFS(hotcount) - GG_OFS(dispatch)) -#define GG_DISP2STATIC (GG_LEN_DDISP*(int)sizeof(ASMFunction)) - -#define hotcount_get(gg, pc) \ - (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)] -#define hotcount_set(gg, pc, val) \ - (hotcount_get((gg), (pc)) = (HotCount)(val)) - -/* Dispatch table management. */ -LJ_FUNC void lj_dispatch_init(GG_State *GG); -#if LJ_HASJIT -LJ_FUNC void lj_dispatch_init_hotcount(global_State *g); -#endif -LJ_FUNC void lj_dispatch_update(global_State *g); - -/* Instruction dispatch callback for hooks or when recording. */ -LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc); -LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc); -#if LJ_HASJIT -LJ_FUNCA void LJ_FASTCALL lj_dispatch_stitch(jit_State *J, const BCIns *pc); -#endif -#if LJ_HASPROFILE -LJ_FUNCA void LJ_FASTCALL lj_dispatch_profile(lua_State *L, const BCIns *pc); -#endif - -#if LJ_HASFFI && !defined(_BUILDVM_H) -/* Save/restore errno and GetLastError() around hooks, exits and recording. */ -#include -#if LJ_TARGET_WINDOWS -#define WIN32_LEAN_AND_MEAN -#include -#define ERRNO_SAVE int olderr = errno; DWORD oldwerr = GetLastError(); -#define ERRNO_RESTORE errno = olderr; SetLastError(oldwerr); -#else -#define ERRNO_SAVE int olderr = errno; -#define ERRNO_RESTORE errno = olderr; -#endif -#else -#define ERRNO_SAVE -#define ERRNO_RESTORE -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm.h deleted file mode 100644 index cfb174f..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm.h +++ /dev/null @@ -1,361 +0,0 @@ -/* -** ARM instruction emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Constant encoding --------------------------------------------------- */ - -static uint8_t emit_invai[16] = { - /* AND */ (ARMI_AND^ARMI_BIC) >> 21, - /* EOR */ 0, - /* SUB */ (ARMI_SUB^ARMI_ADD) >> 21, - /* RSB */ 0, - /* ADD */ (ARMI_ADD^ARMI_SUB) >> 21, - /* ADC */ (ARMI_ADC^ARMI_SBC) >> 21, - /* SBC */ (ARMI_SBC^ARMI_ADC) >> 21, - /* RSC */ 0, - /* TST */ 0, - /* TEQ */ 0, - /* CMP */ (ARMI_CMP^ARMI_CMN) >> 21, - /* CMN */ (ARMI_CMN^ARMI_CMP) >> 21, - /* ORR */ 0, - /* MOV */ (ARMI_MOV^ARMI_MVN) >> 21, - /* BIC */ (ARMI_BIC^ARMI_AND) >> 21, - /* MVN */ (ARMI_MVN^ARMI_MOV) >> 21 -}; - -/* Encode constant in K12 format for data processing instructions. */ -static uint32_t emit_isk12(ARMIns ai, int32_t n) -{ - uint32_t invai, i, m = (uint32_t)n; - /* K12: unsigned 8 bit value, rotated in steps of two bits. */ - for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) - if (m <= 255) return ARMI_K12|m|i; - /* Otherwise try negation/complement with the inverse instruction. */ - invai = emit_invai[((ai >> 21) & 15)]; - if (!invai) return 0; /* Failed. No inverse instruction. */ - m = ~(uint32_t)n; - if (invai == ((ARMI_SUB^ARMI_ADD) >> 21) || - invai == (ARMI_CMP^ARMI_CMN) >> 21) m++; - for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) - if (m <= 255) return ARMI_K12|(invai<<21)|m|i; - return 0; /* Failed. */ -} - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_dnm(ASMState *as, ARMIns ai, Reg rd, Reg rn, Reg rm) -{ - *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn) | ARMF_M(rm); -} - -static void emit_dm(ASMState *as, ARMIns ai, Reg rd, Reg rm) -{ - *--as->mcp = ai | ARMF_D(rd) | ARMF_M(rm); -} - -static void emit_dn(ASMState *as, ARMIns ai, Reg rd, Reg rn) -{ - *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn); -} - -static void emit_nm(ASMState *as, ARMIns ai, Reg rn, Reg rm) -{ - *--as->mcp = ai | ARMF_N(rn) | ARMF_M(rm); -} - -static void emit_d(ASMState *as, ARMIns ai, Reg rd) -{ - *--as->mcp = ai | ARMF_D(rd); -} - -static void emit_n(ASMState *as, ARMIns ai, Reg rn) -{ - *--as->mcp = ai | ARMF_N(rn); -} - -static void emit_m(ASMState *as, ARMIns ai, Reg rm) -{ - *--as->mcp = ai | ARMF_M(rm); -} - -static void emit_lsox(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) -{ - lj_assertA(ofs >= -255 && ofs <= 255, - "load/store offset %d out of range", ofs); - if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; - *--as->mcp = ai | ARMI_LS_P | ARMI_LSX_I | ARMF_D(rd) | ARMF_N(rn) | - ((ofs & 0xf0) << 4) | (ofs & 0x0f); -} - -static void emit_lso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) -{ - lj_assertA(ofs >= -4095 && ofs <= 4095, - "load/store offset %d out of range", ofs); - /* Combine LDR/STR pairs to LDRD/STRD. */ - if (*as->mcp == (ai|ARMI_LS_P|ARMI_LS_U|ARMF_D(rd^1)|ARMF_N(rn)|(ofs^4)) && - (ai & ~(ARMI_LDR^ARMI_STR)) == ARMI_STR && rd != rn && - (uint32_t)ofs <= 252 && !(ofs & 3) && !((rd ^ (ofs >>2)) & 1) && - as->mcp != as->mcloop) { - as->mcp++; - emit_lsox(as, ai == ARMI_LDR ? ARMI_LDRD : ARMI_STRD, rd&~1, rn, ofs&~4); - return; - } - if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; - *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd) | ARMF_N(rn) | ofs; -} - -#if !LJ_SOFTFP -static void emit_vlso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) -{ - lj_assertA(ofs >= -1020 && ofs <= 1020 && (ofs&3) == 0, - "load/store offset %d out of range", ofs); - if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; - *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd & 15) | ARMF_N(rn) | (ofs >> 2); -} -#endif - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer spills of BASE/L. */ -#define emit_canremat(ref) ((ref) < ASMREF_L) - -/* Try to find a one step delta relative to another constant. */ -static int emit_kdelta1(ASMState *as, Reg d, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lj_assertA(r != d, "dest reg not free"); - if (emit_canremat(ref)) { - int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); - uint32_t k = emit_isk12(ARMI_ADD, delta); - if (k) { - if (k == ARMI_K12) - emit_dm(as, ARMI_MOV, d, r); - else - emit_dn(as, ARMI_ADD^k, d, r); - return 1; - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Try to find a two step delta relative to another constant. */ -static int emit_kdelta2(ASMState *as, Reg rd, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lj_assertA(r != rd, "dest reg %d not free", rd); - if (emit_canremat(ref)) { - int32_t other = ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i; - if (other) { - int32_t delta = i - other; - uint32_t sh, inv = 0, k2, k; - if (delta < 0) { delta = -delta; inv = ARMI_ADD^ARMI_SUB; } - sh = lj_ffs(delta) & ~1; - k2 = emit_isk12(0, delta & (255 << sh)); - k = emit_isk12(0, delta & ~(255 << sh)); - if (k) { - emit_dn(as, ARMI_ADD^k2^inv, rd, rd); - emit_dn(as, ARMI_ADD^k^inv, rd, r); - return 1; - } - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Load a 32 bit constant into a GPR. */ -static void emit_loadi(ASMState *as, Reg rd, int32_t i) -{ - uint32_t k = emit_isk12(ARMI_MOV, i); - lj_assertA(rset_test(as->freeset, rd) || rd == RID_TMP, - "dest reg %d not free", rd); - if (k) { - /* Standard K12 constant. */ - emit_d(as, ARMI_MOV^k, rd); - } else if ((as->flags & JIT_F_ARMV6T2) && (uint32_t)i < 0x00010000u) { - /* 16 bit loword constant for ARMv6T2. */ - emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), rd); - } else if (emit_kdelta1(as, rd, i)) { - /* One step delta relative to another constant. */ - } else if ((as->flags & JIT_F_ARMV6T2)) { - /* 32 bit hiword/loword constant for ARMv6T2. */ - emit_d(as, ARMI_MOVT|((i>>16) & 0x0fff)|(((i>>16) & 0xf000)<<4), rd); - emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), rd); - } else if (emit_kdelta2(as, rd, i)) { - /* Two step delta relative to another constant. */ - } else { - /* Otherwise construct the constant with up to 4 instructions. */ - /* NYI: use mvn+bic, use pc-relative loads. */ - for (;;) { - uint32_t sh = lj_ffs(i) & ~1; - int32_t m = i & (255 << sh); - i &= ~(255 << sh); - if (i == 0) { - emit_d(as, ARMI_MOV ^ emit_isk12(0, m), rd); - break; - } - emit_dn(as, ARMI_ORR ^ emit_isk12(0, m), rd, rd); - } - } -} - -#define emit_loada(as, rd, addr) emit_loadi(as, (rd), i32ptr((addr))) - -static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, ARMIns ai, Reg r, void *p) -{ - int32_t i = i32ptr(p); - emit_lso(as, ai, r, ra_allock(as, (i & ~4095), rset_exclude(RSET_GPR, r)), - (i & 4095)); -} - -#if !LJ_SOFTFP -/* Load a number constant into an FPR. */ -static void emit_loadk64(ASMState *as, Reg r, IRIns *ir) -{ - cTValue *tv = ir_knum(ir); - int32_t i; - if ((as->flags & JIT_F_VFPV3) && !tv->u32.lo) { - uint32_t hi = tv->u32.hi; - uint32_t b = ((hi >> 22) & 0x1ff); - if (!(hi & 0xffff) && (b == 0x100 || b == 0x0ff)) { - *--as->mcp = ARMI_VMOVI_D | ARMF_D(r & 15) | - ((tv->u32.hi >> 12) & 0x00080000) | - ((tv->u32.hi >> 4) & 0x00070000) | - ((tv->u32.hi >> 16) & 0x0000000f); - return; - } - } - i = i32ptr(tv); - emit_vlso(as, ARMI_VLDR_D, r, - ra_allock(as, (i & ~1020), RSET_GPR), (i & 1020)); -} -#endif - -/* Get/set global_State fields. */ -#define emit_getgl(as, r, field) \ - emit_lsptr(as, ARMI_LDR, (r), (void *)&J2G(as->J)->field) -#define emit_setgl(as, r, field) \ - emit_lsptr(as, ARMI_STR, (r), (void *)&J2G(as->J)->field) - -/* Trace number is determined from pc of exit instruction. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_branch(ASMState *as, ARMIns ai, MCode *target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = (target - p) - 1; - lj_assertA(((delta + 0x00800000) >> 24) == 0, "branch target out of range"); - *--p = ai | ((uint32_t)delta & 0x00ffffffu); - as->mcp = p; -} - -#define emit_jmp(as, target) emit_branch(as, ARMI_B, (target)) - -static void emit_call(ASMState *as, void *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = ((char *)target - (char *)p) - 8; - if ((((delta>>2) + 0x00800000) >> 24) == 0) { - if ((delta & 1)) - *p = ARMI_BLX | ((uint32_t)(delta>>2) & 0x00ffffffu) | ((delta&2) << 23); - else - *p = ARMI_BL | ((uint32_t)(delta>>2) & 0x00ffffffu); - } else { /* Target out of range: need indirect call. But don't use R0-R3. */ - Reg r = ra_allock(as, i32ptr(target), RSET_RANGE(RID_R4, RID_R12+1)); - *p = ARMI_BLXr | ARMF_M(r); - } -} - -/* -- Emit generic operations --------------------------------------------- */ - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ -#if LJ_SOFTFP - lj_assertA(!irt_isnum(ir->t), "unexpected FP op"); UNUSED(ir); -#else - if (dst >= RID_MAX_GPR) { - emit_dm(as, irt_isnum(ir->t) ? ARMI_VMOV_D : ARMI_VMOV_S, - (dst & 15), (src & 15)); - return; - } -#endif - if (as->mcp != as->mcloop) { /* Swap early registers for loads/stores. */ - MCode ins = *as->mcp, swp = (src^dst); - if ((ins & 0x0c000000) == 0x04000000 && (ins & 0x02000010) != 0x02000010) { - if (!((ins ^ (dst << 16)) & 0x000f0000)) - *as->mcp = ins ^ (swp << 16); /* Swap N in load/store. */ - if (!(ins & 0x00100000) && !((ins ^ (dst << 12)) & 0x0000f000)) - *as->mcp = ins ^ (swp << 12); /* Swap D in store. */ - } - } - emit_dm(as, ARMI_MOV, dst, src); -} - -/* Generic load of register with base and (small) offset address. */ -static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ -#if LJ_SOFTFP - lj_assertA(!irt_isnum(ir->t), "unexpected FP op"); UNUSED(ir); -#else - if (r >= RID_MAX_GPR) - emit_vlso(as, irt_isnum(ir->t) ? ARMI_VLDR_D : ARMI_VLDR_S, r, base, ofs); - else -#endif - emit_lso(as, ARMI_LDR, r, base, ofs); -} - -/* Generic store of register with base and (small) offset address. */ -static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ -#if LJ_SOFTFP - lj_assertA(!irt_isnum(ir->t), "unexpected FP op"); UNUSED(ir); -#else - if (r >= RID_MAX_GPR) - emit_vlso(as, irt_isnum(ir->t) ? ARMI_VSTR_D : ARMI_VSTR_S, r, base, ofs); - else -#endif - emit_lso(as, ARMI_STR, r, base, ofs); -} - -/* Emit an arithmetic/logic operation with a constant operand. */ -static void emit_opk(ASMState *as, ARMIns ai, Reg dest, Reg src, - int32_t i, RegSet allow) -{ - uint32_t k = emit_isk12(ai, i); - if (k) - emit_dn(as, ai^k, dest, src); - else - emit_dnm(as, ai, dest, src, ra_allock(as, i, allow)); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) - emit_opk(as, ARMI_ADD, r, r, ofs, rset_exclude(RSET_GPR, r)); -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm64.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm64.h deleted file mode 100644 index c4b4c14..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_arm64.h +++ /dev/null @@ -1,424 +0,0 @@ -/* -** ARM64 instruction emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Contributed by Djordje Kovacevic and Stefan Pejic from RT-RK.com. -** Sponsored by Cisco Systems, Inc. -*/ - -/* -- Constant encoding --------------------------------------------------- */ - -static uint64_t get_k64val(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_KINT64) { - return ir_kint64(ir)->u64; - } else if (ir->o == IR_KGC) { - return (uint64_t)ir_kgc(ir); - } else if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { - return (uint64_t)ir_kptr(ir); - } else { - lj_assertA(ir->o == IR_KINT || ir->o == IR_KNULL, - "bad 64 bit const IR op %d", ir->o); - return ir->i; /* Sign-extended. */ - } -} - -/* Encode constant in K12 format for data processing instructions. */ -static uint32_t emit_isk12(int64_t n) -{ - uint64_t k = (n < 0) ? -n : n; - uint32_t m = (n < 0) ? 0x40000000 : 0; - if (k < 0x1000) { - return A64I_K12|m|A64F_U12(k); - } else if ((k & 0xfff000) == k) { - return A64I_K12|m|0x400000|A64F_U12(k>>12); - } - return 0; -} - -#define emit_clz64(n) __builtin_clzll(n) -#define emit_ctz64(n) __builtin_ctzll(n) - -/* Encode constant in K13 format for logical data processing instructions. */ -static uint32_t emit_isk13(uint64_t n, int is64) -{ - int inv = 0, w = 128, lz, tz; - if (n & 1) { n = ~n; w = 64; inv = 1; } /* Avoid wrap-around of ones. */ - if (!n) return 0; /* Neither all-zero nor all-ones are allowed. */ - do { /* Find the repeat width. */ - if (is64 && (uint32_t)(n^(n>>32))) break; - n = (uint32_t)n; - if (!n) return 0; /* Ditto when passing n=0xffffffff and is64=0. */ - w = 32; if ((n^(n>>16)) & 0xffff) break; - n = n & 0xffff; w = 16; if ((n^(n>>8)) & 0xff) break; - n = n & 0xff; w = 8; if ((n^(n>>4)) & 0xf) break; - n = n & 0xf; w = 4; if ((n^(n>>2)) & 0x3) break; - n = n & 0x3; w = 2; - } while (0); - lz = emit_clz64(n); - tz = emit_ctz64(n); - if ((int64_t)(n << lz) >> (lz+tz) != -1ll) return 0; /* Non-contiguous? */ - if (inv) - return A64I_K13 | (((lz-w) & 127) << 16) | (((lz+tz-w-1) & 63) << 10); - else - return A64I_K13 | ((w-tz) << 16) | (((63-lz-tz-w-w) & 63) << 10); -} - -static uint32_t emit_isfpk64(uint64_t n) -{ - uint64_t etop9 = ((n >> 54) & 0x1ff); - if ((n << 16) == 0 && (etop9 == 0x100 || etop9 == 0x0ff)) { - return (uint32_t)(((n >> 48) & 0x7f) | ((n >> 56) & 0x80)); - } - return ~0u; -} - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_dnma(ASMState *as, A64Ins ai, Reg rd, Reg rn, Reg rm, Reg ra) -{ - *--as->mcp = ai | A64F_D(rd) | A64F_N(rn) | A64F_M(rm) | A64F_A(ra); -} - -static void emit_dnm(ASMState *as, A64Ins ai, Reg rd, Reg rn, Reg rm) -{ - *--as->mcp = ai | A64F_D(rd) | A64F_N(rn) | A64F_M(rm); -} - -static void emit_dm(ASMState *as, A64Ins ai, Reg rd, Reg rm) -{ - *--as->mcp = ai | A64F_D(rd) | A64F_M(rm); -} - -static void emit_dn(ASMState *as, A64Ins ai, Reg rd, Reg rn) -{ - *--as->mcp = ai | A64F_D(rd) | A64F_N(rn); -} - -static void emit_nm(ASMState *as, A64Ins ai, Reg rn, Reg rm) -{ - *--as->mcp = ai | A64F_N(rn) | A64F_M(rm); -} - -static void emit_d(ASMState *as, A64Ins ai, Reg rd) -{ - *--as->mcp = ai | A64F_D(rd); -} - -static void emit_n(ASMState *as, A64Ins ai, Reg rn) -{ - *--as->mcp = ai | A64F_N(rn); -} - -static int emit_checkofs(A64Ins ai, int64_t ofs) -{ - int scale = (ai >> 30) & 3; - if (ofs < 0 || (ofs & ((1<= -256 && ofs <= 255) ? -1 : 0; - } else { - return (ofs < (4096<> 30) & 3; - lj_assertA(ot, "load/store offset %d out of range", ofs); - /* Combine LDR/STR pairs to LDP/STP. */ - if ((sc == 2 || sc == 3) && - (!(ai & 0x400000) || rd != rn) && - as->mcp != as->mcloop) { - uint32_t prev = *as->mcp & ~A64F_D(31); - int ofsm = ofs - (1<>sc)) || - prev == ((ai^A64I_LS_U) | A64F_N(rn) | A64F_S9(ofsm&0x1ff))) { - aip = (A64F_A(rd) | A64F_D(*as->mcp & 31)); - } else if (prev == (ai | A64F_N(rn) | A64F_U12(ofsp>>sc)) || - prev == ((ai^A64I_LS_U) | A64F_N(rn) | A64F_S9(ofsp&0x1ff))) { - aip = (A64F_D(rd) | A64F_A(*as->mcp & 31)); - ofsm = ofs; - } else { - goto nopair; - } - if (ofsm >= (int)((unsigned int)-64<mcp = aip | A64F_N(rn) | ((ofsm >> sc) << 15) | - (ai ^ ((ai == A64I_LDRx || ai == A64I_STRx) ? 0x50000000 : 0x90000000)); - return; - } - } -nopair: - if (ot == 1) - *--as->mcp = ai | A64F_D(rd) | A64F_N(rn) | A64F_U12(ofs >> sc); - else - *--as->mcp = (ai^A64I_LS_U) | A64F_D(rd) | A64F_N(rn) | A64F_S9(ofs & 0x1ff); -} - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= ASMREF_L) - -/* Try to find an N-step delta relative to other consts with N < lim. */ -static int emit_kdelta(ASMState *as, Reg rd, uint64_t k, int lim) -{ - RegSet work = (~as->freeset & RSET_GPR) | RID2RSET(RID_GL); - if (lim <= 1) return 0; /* Can't beat that. */ - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lj_assertA(r != rd, "dest reg %d not free", rd); - if (ref < REF_TRUE) { - uint64_t kx = ra_iskref(ref) ? (uint64_t)ra_krefk(as, ref) : - get_k64val(as, ref); - int64_t delta = (int64_t)(k - kx); - if (delta == 0) { - emit_dm(as, A64I_MOVx, rd, r); - return 1; - } else { - uint32_t k12 = emit_isk12(delta < 0 ? -delta : delta); - if (k12) { - emit_dn(as, (delta < 0 ? A64I_SUBx : A64I_ADDx)^k12, rd, r); - return 1; - } - /* Do other ops or multi-step deltas pay off? Probably not. - ** E.g. XOR rarely helps with pointer consts. - */ - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -static void emit_loadk(ASMState *as, Reg rd, uint64_t u64, int is64) -{ - int i, zeros = 0, ones = 0, neg; - if (!is64) u64 = (int64_t)(int32_t)u64; /* Sign-extend. */ - /* Count homogeneous 16 bit fragments. */ - for (i = 0; i < 4; i++) { - uint64_t frag = (u64 >> i*16) & 0xffff; - zeros += (frag == 0); - ones += (frag == 0xffff); - } - neg = ones > zeros; /* Use MOVN if it pays off. */ - if ((neg ? ones : zeros) < 3) { /* Need 2+ ins. Try shorter K13 encoding. */ - uint32_t k13 = emit_isk13(u64, is64); - if (k13) { - emit_dn(as, (is64|A64I_ORRw)^k13, rd, RID_ZERO); - return; - } - } - if (!emit_kdelta(as, rd, u64, 4 - (neg ? ones : zeros))) { - int shift = 0, lshift = 0; - uint64_t n64 = neg ? ~u64 : u64; - if (n64 != 0) { - /* Find first/last fragment to be filled. */ - shift = (63-emit_clz64(n64)) & ~15; - lshift = emit_ctz64(n64) & ~15; - } - /* MOVK requires the original value (u64). */ - while (shift > lshift) { - uint32_t u16 = (u64 >> shift) & 0xffff; - /* Skip fragments that are correctly filled by MOVN/MOVZ. */ - if (u16 != (neg ? 0xffff : 0)) - emit_d(as, is64 | A64I_MOVKw | A64F_U16(u16) | A64F_LSL16(shift), rd); - shift -= 16; - } - /* But MOVN needs an inverted value (n64). */ - emit_d(as, (neg ? A64I_MOVNx : A64I_MOVZx) | - A64F_U16((n64 >> lshift) & 0xffff) | A64F_LSL16(lshift), rd); - } -} - -/* Load a 32 bit constant into a GPR. */ -#define emit_loadi(as, rd, i) emit_loadk(as, rd, i, 0) - -/* Load a 64 bit constant into a GPR. */ -#define emit_loadu64(as, rd, i) emit_loadk(as, rd, i, A64I_X) - -#define emit_loada(as, r, addr) emit_loadu64(as, (r), (uintptr_t)(addr)) - -#define glofs(as, k) \ - ((intptr_t)((uintptr_t)(k) - (uintptr_t)&J2GG(as->J)->g)) -#define mcpofs(as, k) \ - ((intptr_t)((uintptr_t)(k) - (uintptr_t)(as->mcp - 1))) -#define checkmcpofs(as, k) \ - (A64F_S_OK(mcpofs(as, k)>>2, 19)) - -static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, A64Ins ai, Reg r, void *p) -{ - /* First, check if ip + offset is in range. */ - if ((ai & 0x00400000) && checkmcpofs(as, p)) { - emit_d(as, A64I_LDRLx | A64F_S19(mcpofs(as, p)>>2), r); - } else { - Reg base = RID_GL; /* Next, try GL + offset. */ - int64_t ofs = glofs(as, p); - if (!emit_checkofs(ai, ofs)) { /* Else split up into base reg + offset. */ - int64_t i64 = i64ptr(p); - base = ra_allock(as, (i64 & ~0x7fffull), rset_exclude(RSET_GPR, r)); - ofs = i64 & 0x7fffull; - } - emit_lso(as, ai, r, base, ofs); - } -} - -/* Load 64 bit IR constant into register. */ -static void emit_loadk64(ASMState *as, Reg r, IRIns *ir) -{ - const uint64_t *k = &ir_k64(ir)->u64; - int64_t ofs; - if (r >= RID_MAX_GPR) { - uint32_t fpk = emit_isfpk64(*k); - if (fpk != ~0u) { - emit_d(as, A64I_FMOV_DI | A64F_FP8(fpk), (r & 31)); - return; - } - } - ofs = glofs(as, k); - if (emit_checkofs(A64I_LDRx, ofs)) { - emit_lso(as, r >= RID_MAX_GPR ? A64I_LDRd : A64I_LDRx, - (r & 31), RID_GL, ofs); - } else { - if (r >= RID_MAX_GPR) { - emit_dn(as, A64I_FMOV_D_R, (r & 31), RID_TMP); - r = RID_TMP; - } - if (checkmcpofs(as, k)) - emit_d(as, A64I_LDRLx | A64F_S19(mcpofs(as, k)>>2), r); - else - emit_loadu64(as, r, *k); - } -} - -/* Get/set global_State fields. */ -#define emit_getgl(as, r, field) \ - emit_lsptr(as, A64I_LDRx, (r), (void *)&J2G(as->J)->field) -#define emit_setgl(as, r, field) \ - emit_lsptr(as, A64I_STRx, (r), (void *)&J2G(as->J)->field) - -/* Trace number is determined from pc of exit instruction. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_cond_branch(ASMState *as, A64CC cond, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(A64F_S_OK(delta, 19), "branch target out of range"); - *p = A64I_BCC | A64F_S19(delta) | cond; -} - -static void emit_branch(ASMState *as, A64Ins ai, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(A64F_S_OK(delta, 26), "branch target out of range"); - *p = ai | A64F_S26(delta); -} - -static void emit_tnb(ASMState *as, A64Ins ai, Reg r, uint32_t bit, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(bit < 63, "bit number out of range"); - lj_assertA(A64F_S_OK(delta, 14), "branch target out of range"); - if (bit > 31) ai |= A64I_X; - *p = ai | A64F_BIT(bit & 31) | A64F_S14(delta) | r; -} - -static void emit_cnb(ASMState *as, A64Ins ai, Reg r, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(A64F_S_OK(delta, 19), "branch target out of range"); - *p = ai | A64F_S19(delta) | r; -} - -#define emit_jmp(as, target) emit_branch(as, A64I_B, (target)) - -static void emit_call(ASMState *as, void *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - if (A64F_S_OK(delta>>2, 26)) { - *p = A64I_BL | A64F_S26(delta>>2); - } else { /* Target out of range: need indirect call. But don't use R0-R7. */ - Reg r = ra_allock(as, i64ptr(target), - RSET_RANGE(RID_X8, RID_MAX_GPR)-RSET_FIXED); - *p = A64I_BLR | A64F_N(r); - } -} - -/* -- Emit generic operations --------------------------------------------- */ - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - if (dst >= RID_MAX_GPR) { - emit_dn(as, irt_isnum(ir->t) ? A64I_FMOV_D : A64I_FMOV_S, - (dst & 31), (src & 31)); - return; - } - if (as->mcp != as->mcloop) { /* Swap early registers for loads/stores. */ - MCode ins = *as->mcp, swp = (src^dst); - if ((ins & 0xbf800000) == 0xb9000000) { - if (!((ins ^ (dst << 5)) & 0x000003e0)) - *as->mcp = ins ^ (swp << 5); /* Swap N in load/store. */ - if (!(ins & 0x00400000) && !((ins ^ dst) & 0x0000001f)) - *as->mcp = ins ^ swp; /* Swap D in store. */ - } - } - emit_dm(as, A64I_MOVx, dst, src); -} - -/* Generic load of register with base and (small) offset address. */ -static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r >= RID_MAX_GPR) - emit_lso(as, irt_isnum(ir->t) ? A64I_LDRd : A64I_LDRs, (r & 31), base, ofs); - else - emit_lso(as, irt_is64(ir->t) ? A64I_LDRx : A64I_LDRw, r, base, ofs); -} - -/* Generic store of register with base and (small) offset address. */ -static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r >= RID_MAX_GPR) - emit_lso(as, irt_isnum(ir->t) ? A64I_STRd : A64I_STRs, (r & 31), base, ofs); - else - emit_lso(as, irt_is64(ir->t) ? A64I_STRx : A64I_STRw, r, base, ofs); -} - -/* Emit an arithmetic operation with a constant operand. */ -static void emit_opk(ASMState *as, A64Ins ai, Reg dest, Reg src, - int32_t i, RegSet allow) -{ - uint32_t k = emit_isk12(i); - if (k) - emit_dn(as, ai^k, dest, src); - else - emit_dnm(as, ai, dest, src, ra_allock(as, i, allow)); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) - emit_opk(as, ofs < 0 ? A64I_SUBx : A64I_ADDx, r, r, - ofs < 0 ? -ofs : ofs, rset_exclude(RSET_GPR, r)); -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_mips.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_mips.h deleted file mode 100644 index 0cea547..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_mips.h +++ /dev/null @@ -1,310 +0,0 @@ -/* -** MIPS instruction emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#if LJ_64 -static intptr_t get_k64val(ASMState *as, IRRef ref) -{ - IRIns *ir = IR(ref); - if (ir->o == IR_KINT64) { - return (intptr_t)ir_kint64(ir)->u64; - } else if (ir->o == IR_KGC) { - return (intptr_t)ir_kgc(ir); - } else if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { - return (intptr_t)ir_kptr(ir); - } else if (LJ_SOFTFP && ir->o == IR_KNUM) { - return (intptr_t)ir_knum(ir)->u64; - } else { - lj_assertA(ir->o == IR_KINT || ir->o == IR_KNULL, - "bad 64 bit const IR op %d", ir->o); - return ir->i; /* Sign-extended. */ - } -} -#endif - -#if LJ_64 -#define get_kval(as, ref) get_k64val(as, ref) -#else -#define get_kval(as, ref) (IR((ref))->i) -#endif - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt) -{ - *--as->mcp = mi | MIPSF_D(rd) | MIPSF_S(rs) | MIPSF_T(rt); -} - -static void emit_dta(ASMState *as, MIPSIns mi, Reg rd, Reg rt, uint32_t a) -{ - *--as->mcp = mi | MIPSF_D(rd) | MIPSF_T(rt) | MIPSF_A(a); -} - -#define emit_ds(as, mi, rd, rs) emit_dst(as, (mi), (rd), (rs), 0) -#define emit_tg(as, mi, rt, rg) emit_dst(as, (mi), (rg)&31, 0, (rt)) - -static void emit_tsi(ASMState *as, MIPSIns mi, Reg rt, Reg rs, int32_t i) -{ - *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | (i & 0xffff); -} - -#define emit_ti(as, mi, rt, i) emit_tsi(as, (mi), (rt), 0, (i)) -#define emit_hsi(as, mi, rh, rs, i) emit_tsi(as, (mi), (rh) & 31, (rs), (i)) - -static void emit_fgh(ASMState *as, MIPSIns mi, Reg rf, Reg rg, Reg rh) -{ - *--as->mcp = mi | MIPSF_F(rf&31) | MIPSF_G(rg&31) | MIPSF_H(rh&31); -} - -#define emit_fg(as, mi, rf, rg) emit_fgh(as, (mi), (rf), (rg), 0) - -static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift) -{ - if (LJ_64 || (as->flags & JIT_F_MIPSXXR2)) { - emit_dta(as, MIPSI_ROTR, dest, src, shift); - } else { - emit_dst(as, MIPSI_OR, dest, dest, tmp); - emit_dta(as, MIPSI_SLL, dest, src, (-shift)&31); - emit_dta(as, MIPSI_SRL, tmp, src, shift); - } -} - -#if LJ_64 || LJ_HASBUFFER -static void emit_tsml(ASMState *as, MIPSIns mi, Reg rt, Reg rs, uint32_t msb, - uint32_t lsb) -{ - *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | MIPSF_M(msb) | MIPSF_L(lsb); -} -#endif - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= REF_BASE) - -/* Try to find a one step delta relative to another constant. */ -static int emit_kdelta1(ASMState *as, Reg rd, intptr_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lj_assertA(r != rd, "dest reg %d not free", rd); - if (ref < ASMREF_L) { - intptr_t delta = (intptr_t)((uintptr_t)i - - (uintptr_t)(ra_iskref(ref) ? ra_krefk(as, ref) : get_kval(as, ref))); - if (checki16(delta)) { - emit_tsi(as, MIPSI_AADDIU, rd, r, delta); - return 1; - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Load a 32 bit constant into a GPR. */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - if (checki16(i)) { - emit_ti(as, MIPSI_LI, r, i); - } else { - if ((i & 0xffff)) { - intptr_t jgl = (intptr_t)(void *)J2G(as->J); - if ((uintptr_t)(i-jgl) < 65536) { - emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768); - return; - } else if (emit_kdelta1(as, r, i)) { - return; - } else if ((i >> 16) == 0) { - emit_tsi(as, MIPSI_ORI, r, RID_ZERO, i); - return; - } - emit_tsi(as, MIPSI_ORI, r, r, i); - } - emit_ti(as, MIPSI_LUI, r, (i >> 16)); - } -} - -#if LJ_64 -/* Load a 64 bit constant into a GPR. */ -static void emit_loadu64(ASMState *as, Reg r, uint64_t u64) -{ - if (checki32((int64_t)u64)) { - emit_loadi(as, r, (int32_t)u64); - } else { - uint64_t delta = u64 - (uint64_t)(void *)J2G(as->J); - if (delta < 65536) { - emit_tsi(as, MIPSI_DADDIU, r, RID_JGL, (int32_t)(delta-32768)); - } else if (emit_kdelta1(as, r, (intptr_t)u64)) { - return; - } else { - /* TODO MIPSR6: Use DAHI & DATI. Caveat: sign-extension. */ - if ((u64 & 0xffff)) { - emit_tsi(as, MIPSI_ORI, r, r, u64 & 0xffff); - } - if (((u64 >> 16) & 0xffff)) { - emit_dta(as, MIPSI_DSLL, r, r, 16); - emit_tsi(as, MIPSI_ORI, r, r, (u64 >> 16) & 0xffff); - emit_dta(as, MIPSI_DSLL, r, r, 16); - } else { - emit_dta(as, MIPSI_DSLL32, r, r, 0); - } - emit_loadi(as, r, (int32_t)(u64 >> 32)); - } - /* TODO: There are probably more optimization opportunities. */ - } -} - -#define emit_loada(as, r, addr) emit_loadu64(as, (r), u64ptr((addr))) -#else -#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) -#endif - -static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow); -static void ra_allockreg(ASMState *as, intptr_t k, Reg r); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow) -{ - intptr_t jgl = (intptr_t)(J2G(as->J)); - intptr_t i = (intptr_t)(p); - Reg base; - if ((uint32_t)(i-jgl) < 65536) { - i = i-jgl-32768; - base = RID_JGL; - } else { - base = ra_allock(as, i-(int16_t)i, allow); - } - emit_tsi(as, mi, r, base, i); -} - -#if LJ_64 -static void emit_loadk64(ASMState *as, Reg r, IRIns *ir) -{ - const uint64_t *k = &ir_k64(ir)->u64; - Reg r64 = r; - if (rset_test(RSET_FPR, r)) { - r64 = RID_TMP; - emit_tg(as, MIPSI_DMTC1, r64, r); - } - if ((uint32_t)((intptr_t)k-(intptr_t)J2G(as->J)) < 65536) - emit_lsptr(as, MIPSI_LD, r64, (void *)k, 0); - else - emit_loadu64(as, r64, *k); -} -#else -#define emit_loadk64(as, r, ir) \ - emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)&ir_knum((ir))->u64, RSET_GPR) -#endif - -/* Get/set global_State fields. */ -static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs) -{ - emit_tsi(as, mi, r, RID_JGL, ofs-32768); -} - -#define emit_getgl(as, r, field) \ - emit_lsglptr(as, MIPSI_AL, (r), (int32_t)offsetof(global_State, field)) -#define emit_setgl(as, r, field) \ - emit_lsglptr(as, MIPSI_AS, (r), (int32_t)offsetof(global_State, field)) - -/* Trace number is determined from per-trace exit stubs. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_branch(ASMState *as, MIPSIns mi, Reg rs, Reg rt, MCode *target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(((delta + 0x8000) >> 16) == 0, "branch target out of range"); - *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu); - as->mcp = p; -} - -static void emit_jmp(ASMState *as, MCode *target) -{ - *--as->mcp = MIPSI_NOP; - emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target)); -} - -static void emit_call(ASMState *as, void *target, int needcfa) -{ - MCode *p = as->mcp; -#if LJ_TARGET_MIPSR6 - ptrdiff_t delta = (char *)target - (char *)p; - if ((((delta>>2) + 0x02000000) >> 26) == 0) { /* Try compact call first. */ - *--p = MIPSI_BALC | (((uintptr_t)delta >>2) & 0x03ffffffu); - as->mcp = p; - return; - } -#endif - *--p = MIPSI_NOP; /* Delay slot. */ - if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0) { -#if !LJ_TARGET_MIPSR6 - *--p = (((uintptr_t)target & 1) ? MIPSI_JALX : MIPSI_JAL) | - (((uintptr_t)target >>2) & 0x03ffffffu); -#else - *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu); -#endif - } else { /* Target out of range: need indirect call. */ - *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR); - needcfa = 1; - } - as->mcp = p; - if (needcfa) ra_allockreg(as, (intptr_t)target, RID_CFUNCADDR); -} - -/* -- Emit generic operations --------------------------------------------- */ - -#define emit_move(as, dst, src) \ - emit_ds(as, MIPSI_MOVE, (dst), (src)) - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - if (dst < RID_MAX_GPR) - emit_move(as, dst, src); - else - emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src); -} - -/* Generic load of register with base and (small) offset address. */ -static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tsi(as, irt_is64(ir->t) ? MIPSI_LD : MIPSI_LW, r, base, ofs); - else - emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1, - (r & 31), base, ofs); -} - -/* Generic store of register with base and (small) offset address. */ -static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tsi(as, irt_is64(ir->t) ? MIPSI_SD : MIPSI_SW, r, base, ofs); - else - emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1, - (r&31), base, ofs); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) { - lj_assertA(checki16(ofs), "offset %d out of range", ofs); - emit_tsi(as, MIPSI_AADDIU, r, r, ofs); - } -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_ppc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_ppc.h deleted file mode 100644 index 86760e7..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_ppc.h +++ /dev/null @@ -1,238 +0,0 @@ -/* -** PPC instruction emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Emit basic instructions --------------------------------------------- */ - -static void emit_tab(ASMState *as, PPCIns pi, Reg rt, Reg ra, Reg rb) -{ - *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | PPCF_B(rb); -} - -#define emit_asb(as, pi, ra, rs, rb) emit_tab(as, (pi), (rs), (ra), (rb)) -#define emit_as(as, pi, ra, rs) emit_tab(as, (pi), (rs), (ra), 0) -#define emit_ab(as, pi, ra, rb) emit_tab(as, (pi), 0, (ra), (rb)) - -static void emit_tai(ASMState *as, PPCIns pi, Reg rt, Reg ra, int32_t i) -{ - *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | (i & 0xffff); -} - -#define emit_ti(as, pi, rt, i) emit_tai(as, (pi), (rt), 0, (i)) -#define emit_ai(as, pi, ra, i) emit_tai(as, (pi), 0, (ra), (i)) -#define emit_asi(as, pi, ra, rs, i) emit_tai(as, (pi), (rs), (ra), (i)) - -#define emit_fab(as, pi, rf, ra, rb) \ - emit_tab(as, (pi), (rf)&31, (ra)&31, (rb)&31) -#define emit_fb(as, pi, rf, rb) emit_tab(as, (pi), (rf)&31, 0, (rb)&31) -#define emit_fac(as, pi, rf, ra, rc) \ - emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, 0) -#define emit_facb(as, pi, rf, ra, rc, rb) \ - emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, (rb)&31) -#define emit_fai(as, pi, rf, ra, i) emit_tai(as, (pi), (rf)&31, (ra), (i)) - -static void emit_rot(ASMState *as, PPCIns pi, Reg ra, Reg rs, - int32_t n, int32_t b, int32_t e) -{ - *--as->mcp = pi | PPCF_T(rs) | PPCF_A(ra) | PPCF_B(n) | - PPCF_MB(b) | PPCF_ME(e); -} - -static void emit_slwi(ASMState *as, Reg ra, Reg rs, int32_t n) -{ - lj_assertA(n >= 0 && n < 32, "shift out or range"); - emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31-n); -} - -static void emit_rotlwi(ASMState *as, Reg ra, Reg rs, int32_t n) -{ - lj_assertA(n >= 0 && n < 32, "shift out or range"); - emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31); -} - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= REF_BASE) - -/* Try to find a one step delta relative to another constant. */ -static int emit_kdelta1(ASMState *as, Reg rd, int32_t i) -{ - RegSet work = ~as->freeset & RSET_GPR; - while (work) { - Reg r = rset_picktop(work); - IRRef ref = regcost_ref(as->cost[r]); - lj_assertA(r != rd, "dest reg %d not free", rd); - if (ref < ASMREF_L) { - int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); - if (checki16(delta)) { - emit_tai(as, PPCI_ADDI, rd, r, delta); - return 1; - } - } - rset_clear(work, r); - } - return 0; /* Failed. */ -} - -/* Load a 32 bit constant into a GPR. */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - if (checki16(i)) { - emit_ti(as, PPCI_LI, r, i); - } else { - if ((i & 0xffff)) { - int32_t jgl = i32ptr(J2G(as->J)); - if ((uint32_t)(i-jgl) < 65536) { - emit_tai(as, PPCI_ADDI, r, RID_JGL, i-jgl-32768); - return; - } else if (emit_kdelta1(as, r, i)) { - return; - } - emit_asi(as, PPCI_ORI, r, r, i); - } - emit_ti(as, PPCI_LIS, r, (i >> 16)); - } -} - -#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) - -static Reg ra_allock(ASMState *as, intptr_t k, RegSet allow); - -/* Get/set from constant pointer. */ -static void emit_lsptr(ASMState *as, PPCIns pi, Reg r, void *p, RegSet allow) -{ - int32_t jgl = i32ptr(J2G(as->J)); - int32_t i = i32ptr(p); - Reg base; - if ((uint32_t)(i-jgl) < 65536) { - i = i-jgl-32768; - base = RID_JGL; - } else { - base = ra_allock(as, i-(int16_t)i, allow); - } - emit_tai(as, pi, r, base, i); -} - -#define emit_loadk64(as, r, ir) \ - emit_lsptr(as, PPCI_LFD, ((r) & 31), (void *)&ir_knum((ir))->u64, RSET_GPR) - -/* Get/set global_State fields. */ -static void emit_lsglptr(ASMState *as, PPCIns pi, Reg r, int32_t ofs) -{ - emit_tai(as, pi, r, RID_JGL, ofs-32768); -} - -#define emit_getgl(as, r, field) \ - emit_lsglptr(as, PPCI_LWZ, (r), (int32_t)offsetof(global_State, field)) -#define emit_setgl(as, r, field) \ - emit_lsglptr(as, PPCI_STW, (r), (int32_t)offsetof(global_State, field)) - -/* Trace number is determined from per-trace exit stubs. */ -#define emit_setvmstate(as, i) UNUSED(i) - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for internal jumps. */ -typedef MCode *MCLabel; - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -static void emit_condbranch(ASMState *as, PPCIns pi, PPCCC cc, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - lj_assertA(((delta + 0x8000) >> 16) == 0, "branch target out of range"); - pi ^= (delta & 0x8000) * (PPCF_Y/0x8000); - *p = pi | PPCF_CC(cc) | ((uint32_t)delta & 0xffffu); -} - -static void emit_jmp(ASMState *as, MCode *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - *p = PPCI_B | (delta & 0x03fffffcu); -} - -static void emit_call(ASMState *as, void *target) -{ - MCode *p = --as->mcp; - ptrdiff_t delta = (char *)target - (char *)p; - if ((((delta>>2) + 0x00800000) >> 24) == 0) { - *p = PPCI_BL | (delta & 0x03fffffcu); - } else { /* Target out of range: need indirect call. Don't use arg reg. */ - RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); - Reg r = ra_allock(as, i32ptr(target), allow); - *p = PPCI_BCTRL; - p[-1] = PPCI_MTCTR | PPCF_T(r); - as->mcp = p-1; - } -} - -/* -- Emit generic operations --------------------------------------------- */ - -#define emit_mr(as, dst, src) \ - emit_asb(as, PPCI_MR, (dst), (src), (src)) - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - UNUSED(ir); - if (dst < RID_MAX_GPR) - emit_mr(as, dst, src); - else - emit_fb(as, PPCI_FMR, dst, src); -} - -/* Generic load of register with base and (small) offset address. */ -static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tai(as, PPCI_LWZ, r, base, ofs); - else - emit_fai(as, irt_isnum(ir->t) ? PPCI_LFD : PPCI_LFS, r, base, ofs); -} - -/* Generic store of register with base and (small) offset address. */ -static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_tai(as, PPCI_STW, r, base, ofs); - else - emit_fai(as, irt_isnum(ir->t) ? PPCI_STFD : PPCI_STFS, r, base, ofs); -} - -/* Emit a compare (for equality) with a constant operand. */ -static void emit_cmpi(ASMState *as, Reg r, int32_t k) -{ - if (checki16(k)) { - emit_ai(as, PPCI_CMPWI, r, k); - } else if (checku16(k)) { - emit_ai(as, PPCI_CMPLWI, r, k); - } else { - emit_ai(as, PPCI_CMPLWI, RID_TMP, k); - emit_asi(as, PPCI_XORIS, RID_TMP, r, (k >> 16)); - } -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) { - emit_tai(as, PPCI_ADDI, r, r, ofs); - if (!checki16(ofs)) - emit_tai(as, PPCI_ADDIS, r, r, (ofs + 32768) >> 16); - } -} - -static void emit_spsub(ASMState *as, int32_t ofs) -{ - if (ofs) { - emit_tai(as, PPCI_STWU, RID_TMP, RID_SP, -ofs); - emit_tai(as, PPCI_ADDI, RID_TMP, RID_SP, - CFRAME_SIZE + (as->parent ? as->parent->spadjust : 0)); - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_x86.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_x86.h deleted file mode 100644 index 3d3beda..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_emit_x86.h +++ /dev/null @@ -1,572 +0,0 @@ -/* -** x86/x64 instruction emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* -- Emit basic instructions --------------------------------------------- */ - -#define MODRM(mode, r1, r2) ((MCode)((mode)+(((r1)&7)<<3)+((r2)&7))) - -#if LJ_64 -#define REXRB(p, rr, rb) \ - { MCode rex = 0x40 + (((rr)>>1)&4) + (((rb)>>3)&1); \ - if (rex != 0x40) *--(p) = rex; } -#define FORCE_REX 0x200 -#define REX_64 (FORCE_REX|0x080000) -#define VEX_64 0x800000 -#else -#define REXRB(p, rr, rb) ((void)0) -#define FORCE_REX 0 -#define REX_64 0 -#define VEX_64 0 -#endif -#if LJ_GC64 -#define REX_GC64 REX_64 -#else -#define REX_GC64 0 -#endif - -#define emit_i8(as, i) (*--as->mcp = (MCode)(i)) -#define emit_i32(as, i) (*(int32_t *)(as->mcp-4) = (i), as->mcp -= 4) -#define emit_u32(as, u) (*(uint32_t *)(as->mcp-4) = (u), as->mcp -= 4) - -#define emit_x87op(as, xo) \ - (*(uint16_t *)(as->mcp-2) = (uint16_t)(xo), as->mcp -= 2) - -/* op */ -static LJ_AINLINE MCode *emit_op(x86Op xo, Reg rr, Reg rb, Reg rx, - MCode *p, int delta) -{ - int n = (int8_t)xo; - if (n == -60) { /* VEX-encoded instruction */ -#if LJ_64 - xo ^= (((rr>>1)&4)+((rx>>2)&2)+((rb>>3)&1))<<13; -#endif - *(uint32_t *)(p+delta-5) = (uint32_t)xo; - return p+delta-5; - } -#if defined(__GNUC__) || defined(__clang__) - if (__builtin_constant_p(xo) && n == -2) - p[delta-2] = (MCode)(xo >> 24); - else if (__builtin_constant_p(xo) && n == -3) - *(uint16_t *)(p+delta-3) = (uint16_t)(xo >> 16); - else -#endif - *(uint32_t *)(p+delta-5) = (uint32_t)xo; - p += n + delta; -#if LJ_64 - { - uint32_t rex = 0x40 + ((rr>>1)&(4+(FORCE_REX>>1)))+((rx>>2)&2)+((rb>>3)&1); - if (rex != 0x40) { - rex |= (rr >> 16); - if (n == -4) { *p = (MCode)rex; rex = (MCode)(xo >> 8); } - else if ((xo & 0xffffff) == 0x6600fd) { *p = (MCode)rex; rex = 0x66; } - *--p = (MCode)rex; - } - } -#else - UNUSED(rr); UNUSED(rb); UNUSED(rx); -#endif - return p; -} - -/* op + modrm */ -#define emit_opm(xo, mode, rr, rb, p, delta) \ - (p[(delta)-1] = MODRM((mode), (rr), (rb)), \ - emit_op((xo), (rr), (rb), 0, (p), (delta))) - -/* op + modrm + sib */ -#define emit_opmx(xo, mode, scale, rr, rb, rx, p) \ - (p[-1] = MODRM((scale), (rx), (rb)), \ - p[-2] = MODRM((mode), (rr), RID_ESP), \ - emit_op((xo), (rr), (rb), (rx), (p), -1)) - -/* op r1, r2 */ -static void emit_rr(ASMState *as, x86Op xo, Reg r1, Reg r2) -{ - MCode *p = as->mcp; - as->mcp = emit_opm(xo, XM_REG, r1, r2, p, 0); -} - -#if LJ_64 && defined(LUA_USE_ASSERT) -/* [addr] is sign-extended in x64 and must be in lower 2G (not 4G). */ -static int32_t ptr2addr(const void *p) -{ - lj_assertX((uintptr_t)p < (uintptr_t)0x80000000, "pointer outside 2G range"); - return i32ptr(p); -} -#else -#define ptr2addr(p) (i32ptr((p))) -#endif - -/* op r, [base+ofs] */ -static void emit_rmro(ASMState *as, x86Op xo, Reg rr, Reg rb, int32_t ofs) -{ - MCode *p = as->mcp; - x86Mode mode; - if (ra_hasreg(rb)) { - if (LJ_GC64 && rb == RID_RIP) { - mode = XM_OFS0; - p -= 4; - *(int32_t *)p = ofs; - } else if (ofs == 0 && (rb&7) != RID_EBP) { - mode = XM_OFS0; - } else if (checki8(ofs)) { - *--p = (MCode)ofs; - mode = XM_OFS8; - } else { - p -= 4; - *(int32_t *)p = ofs; - mode = XM_OFS32; - } - if ((rb&7) == RID_ESP) - *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - } else { - *(int32_t *)(p-4) = ofs; -#if LJ_64 - p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP); - p -= 5; - rb = RID_ESP; -#else - p -= 4; - rb = RID_EBP; -#endif - mode = XM_OFS0; - } - as->mcp = emit_opm(xo, mode, rr, rb, p, 0); -} - -/* op r, [base+idx*scale+ofs] */ -static void emit_rmrxo(ASMState *as, x86Op xo, Reg rr, Reg rb, Reg rx, - x86Mode scale, int32_t ofs) -{ - MCode *p = as->mcp; - x86Mode mode; - if (ofs == 0 && (rb&7) != RID_EBP) { - mode = XM_OFS0; - } else if (checki8(ofs)) { - mode = XM_OFS8; - *--p = (MCode)ofs; - } else { - mode = XM_OFS32; - p -= 4; - *(int32_t *)p = ofs; - } - as->mcp = emit_opmx(xo, mode, scale, rr, rb, rx, p); -} - -/* op r, i */ -static void emit_gri(ASMState *as, x86Group xg, Reg rb, int32_t i) -{ - MCode *p = as->mcp; - x86Op xo; - if (checki8(i)) { - *--p = (MCode)i; - xo = XG_TOXOi8(xg); - } else { - p -= 4; - *(int32_t *)p = i; - xo = XG_TOXOi(xg); - } - as->mcp = emit_opm(xo, XM_REG, (Reg)(xg & 7) | (rb & REX_64), rb, p, 0); -} - -/* op [base+ofs], i */ -static void emit_gmroi(ASMState *as, x86Group xg, Reg rb, int32_t ofs, - int32_t i) -{ - x86Op xo; - if (checki8(i)) { - emit_i8(as, i); - xo = XG_TOXOi8(xg); - } else { - emit_i32(as, i); - xo = XG_TOXOi(xg); - } - emit_rmro(as, xo, (Reg)(xg & 7), rb, ofs); -} - -#define emit_shifti(as, xg, r, i) \ - (emit_i8(as, (i)), emit_rr(as, XO_SHIFTi, (Reg)(xg), (r))) - -/* op r, rm/mrm */ -static void emit_mrm(ASMState *as, x86Op xo, Reg rr, Reg rb) -{ - MCode *p = as->mcp; - x86Mode mode = XM_REG; - if (rb == RID_MRM) { - rb = as->mrm.base; - if (rb == RID_NONE) { - rb = RID_EBP; - mode = XM_OFS0; - p -= 4; - *(int32_t *)p = as->mrm.ofs; - if (as->mrm.idx != RID_NONE) - goto mrmidx; -#if LJ_64 - *--p = MODRM(XM_SCALE1, RID_ESP, RID_EBP); - rb = RID_ESP; -#endif - } else if (LJ_GC64 && rb == RID_RIP) { - lj_assertA(as->mrm.idx == RID_NONE, "RIP-rel mrm cannot have index"); - mode = XM_OFS0; - p -= 4; - *(int32_t *)p = as->mrm.ofs; - } else { - if (as->mrm.ofs == 0 && (rb&7) != RID_EBP) { - mode = XM_OFS0; - } else if (checki8(as->mrm.ofs)) { - *--p = (MCode)as->mrm.ofs; - mode = XM_OFS8; - } else { - p -= 4; - *(int32_t *)p = as->mrm.ofs; - mode = XM_OFS32; - } - if (as->mrm.idx != RID_NONE) { - mrmidx: - as->mcp = emit_opmx(xo, mode, as->mrm.scale, rr, rb, as->mrm.idx, p); - return; - } - if ((rb&7) == RID_ESP) - *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP); - } - } - as->mcp = emit_opm(xo, mode, rr, rb, p, 0); -} - -/* op rm/mrm, i */ -static void emit_gmrmi(ASMState *as, x86Group xg, Reg rb, int32_t i) -{ - x86Op xo; - if (checki8(i)) { - emit_i8(as, i); - xo = XG_TOXOi8(xg); - } else { - emit_i32(as, i); - xo = XG_TOXOi(xg); - } - emit_mrm(as, xo, (Reg)(xg & 7) | (rb & REX_64), (rb & ~REX_64)); -} - -/* -- Emit loads/stores --------------------------------------------------- */ - -/* mov [base+ofs], i */ -static void emit_movmroi(ASMState *as, Reg base, int32_t ofs, int32_t i) -{ - emit_i32(as, i); - emit_rmro(as, XO_MOVmi, 0, base, ofs); -} - -/* mov [base+ofs], r */ -#define emit_movtomro(as, r, base, ofs) \ - emit_rmro(as, XO_MOVto, (r), (base), (ofs)) - -/* Get/set global_State fields. */ -#define emit_opgl(as, xo, r, field) \ - emit_rma(as, (xo), (r), (void *)&J2G(as->J)->field) -#define emit_getgl(as, r, field) emit_opgl(as, XO_MOV, (r)|REX_GC64, field) -#define emit_setgl(as, r, field) emit_opgl(as, XO_MOVto, (r)|REX_GC64, field) - -#define emit_setvmstate(as, i) \ - (emit_i32(as, i), emit_opgl(as, XO_MOVmi, 0, vmstate)) - -/* mov r, i / xor r, r */ -static void emit_loadi(ASMState *as, Reg r, int32_t i) -{ - /* XOR r,r is shorter, but modifies the flags. This is bad for HIOP/jcc. */ - if (i == 0 && !(LJ_32 && (IR(as->curins)->o == IR_HIOP || - (as->curins+1 < as->T->nins && - IR(as->curins+1)->o == IR_HIOP))) && - !((*as->mcp == 0x0f && (as->mcp[1] & 0xf0) == XI_JCCn) || - (*as->mcp & 0xf0) == XI_JCCs)) { - emit_rr(as, XO_ARITH(XOg_XOR), r, r); - } else { - MCode *p = as->mcp; - *(int32_t *)(p-4) = i; - p[-5] = (MCode)(XI_MOVri+(r&7)); - p -= 5; - REXRB(p, 0, r); - as->mcp = p; - } -} - -#if LJ_GC64 -#define dispofs(as, k) \ - ((intptr_t)((uintptr_t)(k) - (uintptr_t)J2GG(as->J)->dispatch)) -#define mcpofs(as, k) \ - ((intptr_t)((uintptr_t)(k) - (uintptr_t)as->mcp)) -#define mctopofs(as, k) \ - ((intptr_t)((uintptr_t)(k) - (uintptr_t)as->mctop)) -/* mov r, addr */ -#define emit_loada(as, r, addr) \ - emit_loadu64(as, (r), (uintptr_t)(addr)) -#else -/* mov r, addr */ -#define emit_loada(as, r, addr) \ - emit_loadi(as, (r), ptr2addr((addr))) -#endif - -#if LJ_64 -/* mov r, imm64 or shorter 32 bit extended load. */ -static void emit_loadu64(ASMState *as, Reg r, uint64_t u64) -{ - if (checku32(u64)) { /* 32 bit load clears upper 32 bits. */ - emit_loadi(as, r, (int32_t)u64); - } else if (checki32((int64_t)u64)) { /* Sign-extended 32 bit load. */ - MCode *p = as->mcp; - *(int32_t *)(p-4) = (int32_t)u64; - as->mcp = emit_opm(XO_MOVmi, XM_REG, REX_64, r, p, -4); -#if LJ_GC64 - } else if (checki32(dispofs(as, u64))) { - emit_rmro(as, XO_LEA, r|REX_64, RID_DISPATCH, (int32_t)dispofs(as, u64)); - } else if (checki32(mcpofs(as, u64)) && checki32(mctopofs(as, u64))) { - /* Since as->realign assumes the code size doesn't change, check - ** RIP-relative addressing reachability for both as->mcp and as->mctop. - */ - emit_rmro(as, XO_LEA, r|REX_64, RID_RIP, (int32_t)mcpofs(as, u64)); -#endif - } else { /* Full-size 64 bit load. */ - MCode *p = as->mcp; - *(uint64_t *)(p-8) = u64; - p[-9] = (MCode)(XI_MOVri+(r&7)); - p[-10] = 0x48 + ((r>>3)&1); - p -= 10; - as->mcp = p; - } -} -#endif - -/* op r, [addr] */ -static void emit_rma(ASMState *as, x86Op xo, Reg rr, const void *addr) -{ -#if LJ_GC64 - if (checki32(dispofs(as, addr))) { - emit_rmro(as, xo, rr, RID_DISPATCH, (int32_t)dispofs(as, addr)); - } else if (checki32(mcpofs(as, addr)) && checki32(mctopofs(as, addr))) { - emit_rmro(as, xo, rr, RID_RIP, (int32_t)mcpofs(as, addr)); - } else if (!checki32((intptr_t)addr)) { - Reg ra = (rr & 15); - if (xo != XO_MOV) { - /* We can't allocate a register here. Use and restore DISPATCH. Ugly. */ - uint64_t dispaddr = (uintptr_t)J2GG(as->J)->dispatch; - uint8_t i8 = xo == XO_GROUP3b ? *as->mcp++ : 0; - ra = RID_DISPATCH; - if (checku32(dispaddr)) { - emit_loadi(as, ra, (int32_t)dispaddr); - } else { /* Full-size 64 bit load. */ - MCode *p = as->mcp; - *(uint64_t *)(p-8) = dispaddr; - p[-9] = (MCode)(XI_MOVri+(ra&7)); - p[-10] = 0x48 + ((ra>>3)&1); - p -= 10; - as->mcp = p; - } - if (xo == XO_GROUP3b) emit_i8(as, i8); - } - emit_rmro(as, xo, rr, ra, 0); - emit_loadu64(as, ra, (uintptr_t)addr); - } else -#endif - { - MCode *p = as->mcp; - *(int32_t *)(p-4) = ptr2addr(addr); -#if LJ_64 - p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP); - as->mcp = emit_opm(xo, XM_OFS0, rr, RID_ESP, p, -5); -#else - as->mcp = emit_opm(xo, XM_OFS0, rr, RID_EBP, p, -4); -#endif - } -} - -/* Load 64 bit IR constant into register. */ -static void emit_loadk64(ASMState *as, Reg r, IRIns *ir) -{ - Reg r64; - x86Op xo; - const uint64_t *k = &ir_k64(ir)->u64; - if (rset_test(RSET_FPR, r)) { - r64 = r; - xo = XO_MOVSD; - } else { - r64 = r | REX_64; - xo = XO_MOV; - } - if (*k == 0) { - emit_rr(as, rset_test(RSET_FPR, r) ? XO_XORPS : XO_ARITH(XOg_XOR), r, r); -#if LJ_GC64 - } else if (checki32((intptr_t)k) || checki32(dispofs(as, k)) || - (checki32(mcpofs(as, k)) && checki32(mctopofs(as, k)))) { - emit_rma(as, xo, r64, k); - } else { - if (ir->i) { - lj_assertA(*k == *(uint64_t*)(as->mctop - ir->i), - "bad interned 64 bit constant"); - } else if (as->curins <= as->stopins && rset_test(RSET_GPR, r)) { - emit_loadu64(as, r, *k); - return; - } else { - /* If all else fails, add the FP constant at the MCode area bottom. */ - while ((uintptr_t)as->mcbot & 7) *as->mcbot++ = XI_INT3; - *(uint64_t *)as->mcbot = *k; - ir->i = (int32_t)(as->mctop - as->mcbot); - as->mcbot += 8; - as->mclim = as->mcbot + MCLIM_REDZONE; - lj_mcode_commitbot(as->J, as->mcbot); - } - emit_rmro(as, xo, r64, RID_RIP, (int32_t)mcpofs(as, as->mctop - ir->i)); -#else - } else { - emit_rma(as, xo, r64, k); -#endif - } -} - -/* -- Emit control-flow instructions -------------------------------------- */ - -/* Label for short jumps. */ -typedef MCode *MCLabel; - -#if LJ_32 && LJ_HASFFI -/* jmp short target */ -static void emit_sjmp(ASMState *as, MCLabel target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(delta == (int8_t)delta, "short jump target out of range"); - p[-1] = (MCode)(int8_t)delta; - p[-2] = XI_JMPs; - as->mcp = p - 2; -} -#endif - -/* jcc short target */ -static void emit_sjcc(ASMState *as, int cc, MCLabel target) -{ - MCode *p = as->mcp; - ptrdiff_t delta = target - p; - lj_assertA(delta == (int8_t)delta, "short jump target out of range"); - p[-1] = (MCode)(int8_t)delta; - p[-2] = (MCode)(XI_JCCs+(cc&15)); - as->mcp = p - 2; -} - -/* jcc short (pending target) */ -static MCLabel emit_sjcc_label(ASMState *as, int cc) -{ - MCode *p = as->mcp; - p[-1] = 0; - p[-2] = (MCode)(XI_JCCs+(cc&15)); - as->mcp = p - 2; - return p; -} - -/* Fixup jcc short target. */ -static void emit_sfixup(ASMState *as, MCLabel source) -{ - source[-1] = (MCode)(as->mcp-source); -} - -/* Return label pointing to current PC. */ -#define emit_label(as) ((as)->mcp) - -/* Compute relative 32 bit offset for jump and call instructions. */ -static LJ_AINLINE int32_t jmprel(jit_State *J, MCode *p, MCode *target) -{ - ptrdiff_t delta = target - p; - UNUSED(J); - lj_assertJ(delta == (int32_t)delta, "jump target out of range"); - return (int32_t)delta; -} - -/* jcc target */ -static void emit_jcc(ASMState *as, int cc, MCode *target) -{ - MCode *p = as->mcp; - *(int32_t *)(p-4) = jmprel(as->J, p, target); - p[-5] = (MCode)(XI_JCCn+(cc&15)); - p[-6] = 0x0f; - as->mcp = p - 6; -} - -/* jmp target */ -static void emit_jmp(ASMState *as, MCode *target) -{ - MCode *p = as->mcp; - *(int32_t *)(p-4) = jmprel(as->J, p, target); - p[-5] = XI_JMP; - as->mcp = p - 5; -} - -/* call target */ -static void emit_call_(ASMState *as, MCode *target) -{ - MCode *p = as->mcp; -#if LJ_64 - if (target-p != (int32_t)(target-p)) { - /* Assumes RID_RET is never an argument to calls and always clobbered. */ - emit_rr(as, XO_GROUP5, XOg_CALL, RID_RET); - emit_loadu64(as, RID_RET, (uint64_t)target); - return; - } -#endif - *(int32_t *)(p-4) = jmprel(as->J, p, target); - p[-5] = XI_CALL; - as->mcp = p - 5; -} - -#define emit_call(as, f) emit_call_(as, (MCode *)(void *)(f)) - -/* -- Emit generic operations --------------------------------------------- */ - -/* Use 64 bit operations to handle 64 bit IR types. */ -#if LJ_64 -#define REX_64IR(ir, r) ((r) + (irt_is64((ir)->t) ? REX_64 : 0)) -#define VEX_64IR(ir, r) ((r) + (irt_is64((ir)->t) ? VEX_64 : 0)) -#else -#define REX_64IR(ir, r) (r) -#define VEX_64IR(ir, r) (r) -#endif - -/* Generic move between two regs. */ -static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) -{ - UNUSED(ir); - if (dst < RID_MAX_GPR) - emit_rr(as, XO_MOV, REX_64IR(ir, dst), src); - else - emit_rr(as, XO_MOVAPS, dst, src); -} - -/* Generic load of register with base and (small) offset address. */ -static void emit_loadofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_rmro(as, XO_MOV, REX_64IR(ir, r), base, ofs); - else - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSD : XO_MOVSS, r, base, ofs); -} - -/* Generic store of register with base and (small) offset address. */ -static void emit_storeofs(ASMState *as, IRIns *ir, Reg r, Reg base, int32_t ofs) -{ - if (r < RID_MAX_GPR) - emit_rmro(as, XO_MOVto, REX_64IR(ir, r), base, ofs); - else - emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, r, base, ofs); -} - -/* Add offset to pointer. */ -static void emit_addptr(ASMState *as, Reg r, int32_t ofs) -{ - if (ofs) { - emit_gri(as, XG_ARITHi(XOg_ADD), r|REX_GC64, ofs); - } -} - -#define emit_spsub(as, ofs) emit_addptr(as, RID_ESP|REX_64, -(ofs)) - -/* Prefer rematerialization of BASE/L from global_State over spills. */ -#define emit_canremat(ref) ((ref) <= REF_BASE) - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.c deleted file mode 100644 index 283c3d1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.c +++ /dev/null @@ -1,1098 +0,0 @@ -/* -** Error handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_err_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_func.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_ff.h" -#include "lj_trace.h" -#include "lj_vm.h" -#include "lj_strfmt.h" - -/* -** LuaJIT can either use internal or external frame unwinding: -** -** - Internal frame unwinding (INT) is free-standing and doesn't require -** any OS or library support. -** -** - External frame unwinding (EXT) uses the system-provided unwind handler. -** -** Pros and Cons: -** -** - EXT requires unwind tables for *all* functions on the C stack between -** the pcall/catch and the error/throw. C modules used by Lua code can -** throw errors, so these need to have unwind tables, too. Transitively -** this applies to all system libraries used by C modules -- at least -** when they have callbacks which may throw an error. -** -** - INT is faster when actually throwing errors, but this happens rarely. -** Setting up error handlers is zero-cost in any case. -** -** - INT needs to save *all* callee-saved registers when entering the -** interpreter. EXT only needs to save those actually used inside the -** interpreter. JIT-compiled code may need to save some more. -** -** - EXT provides full interoperability with C++ exceptions. You can throw -** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames. -** C++ destructors are called as needed. C++ exceptions caught by pcall -** are converted to the string "C++ exception". Lua errors can be caught -** with catch (...) in C++. -** -** - INT has only limited support for automatically catching C++ exceptions -** on POSIX systems using DWARF2 stack unwinding. Other systems may use -** the wrapper function feature. Lua errors thrown through C++ frames -** cannot be caught by C++ code and C++ destructors are not run. -** -** - EXT can handle errors from internal helper functions that are called -** from JIT-compiled code (except for Windows/x86 and 32 bit ARM). -** INT has no choice but to call the panic handler, if this happens. -** Note: this is mainly relevant for out-of-memory errors. -** -** EXT is the default on all systems where the toolchain produces unwind -** tables by default (*). This is hard-coded and/or detected in src/Makefile. -** You can thwart the detection with: TARGET_XCFLAGS=-DLUAJIT_UNWIND_INTERNAL -** -** INT is the default on all other systems. -** -** EXT can be manually enabled for toolchains that are able to produce -** conforming unwind tables: -** "TARGET_XCFLAGS=-funwind-tables -DLUAJIT_UNWIND_EXTERNAL" -** As explained above, *all* C code used directly or indirectly by LuaJIT -** must be compiled with -funwind-tables (or -fexceptions). C++ code must -** *not* be compiled with -fno-exceptions. -** -** If you're unsure whether error handling inside the VM works correctly, -** try running this and check whether it prints "OK": -** -** luajit -e "print(select(2, load('OK')):match('OK'))" -** -** (*) Originally, toolchains only generated unwind tables for C++ code. For -** interoperability reasons, this can be manually enabled for plain C code, -** too (with -funwind-tables). With the introduction of the x64 architecture, -** the corresponding POSIX and Windows ABIs mandated unwind tables for all -** code. Over the following years most desktop and server platforms have -** enabled unwind tables by default on all architectures. OTOH mobile and -** embedded platforms do not consistently mandate unwind tables. -*/ - -/* -- Error messages ------------------------------------------------------ */ - -/* Error message strings. */ -LJ_DATADEF const char *lj_err_allmsg = -#define ERRDEF(name, msg) msg "\0" -#include "lj_errmsg.h" -; - -/* -- Internal frame unwinding -------------------------------------------- */ - -/* Unwind Lua stack and move error message to new top. */ -LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top) -{ - lj_func_closeuv(L, top); - if (top < L->top-1) { - copyTV(L, top, L->top-1); - L->top = top+1; - } - lj_state_relimitstack(L); -} - -/* Unwind until stop frame. Optionally cleanup frames. */ -static void *err_unwind(lua_State *L, void *stopcf, int errcode) -{ - TValue *frame = L->base-1; - void *cf = L->cframe; - while (cf) { - int32_t nres = cframe_nres(cframe_raw(cf)); - if (nres < 0) { /* C frame without Lua frame? */ - TValue *top = restorestack(L, -nres); - if (frame < top) { /* Frame reached? */ - if (errcode) { - L->base = frame+1; - L->cframe = cframe_prev(cf); - unwindstack(L, top); - } - return cf; - } - } - if (frame <= tvref(L->stack)+LJ_FR2) - break; - switch (frame_typep(frame)) { - case FRAME_LUA: /* Lua frame. */ - case FRAME_LUAP: - frame = frame_prevl(frame); - break; - case FRAME_C: /* C frame. */ - unwind_c: -#if LJ_UNWIND_EXT - if (errcode) { - L->base = frame_prevd(frame) + 1; - L->cframe = cframe_prev(cf); - unwindstack(L, frame - LJ_FR2); - } else if (cf != stopcf) { - cf = cframe_prev(cf); - frame = frame_prevd(frame); - break; - } - return NULL; /* Continue unwinding. */ -#else - UNUSED(stopcf); - cf = cframe_prev(cf); - frame = frame_prevd(frame); - break; -#endif - case FRAME_CP: /* Protected C frame. */ - if (cframe_canyield(cf)) { /* Resume? */ - if (errcode) { - hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */ - L->cframe = NULL; - L->status = (uint8_t)errcode; - } - return cf; - } - if (errcode) { - L->base = frame_prevd(frame) + 1; - L->cframe = cframe_prev(cf); - unwindstack(L, frame - LJ_FR2); - } - return cf; - case FRAME_CONT: /* Continuation frame. */ - if (frame_iscont_fficb(frame)) - goto unwind_c; - /* fallthrough */ - case FRAME_VARG: /* Vararg frame. */ - frame = frame_prevd(frame); - break; - case FRAME_PCALL: /* FF pcall() frame. */ - case FRAME_PCALLH: /* FF pcall() frame inside hook. */ - if (errcode) { - if (errcode == LUA_YIELD) { - frame = frame_prevd(frame); - break; - } - if (frame_typep(frame) == FRAME_PCALL) - hook_leave(G(L)); - L->base = frame_prevd(frame) + 1; - L->cframe = cf; - unwindstack(L, L->base); - } - return (void *)((intptr_t)cf | CFRAME_UNWIND_FF); - } - } - /* No C frame. */ - if (errcode) { - L->base = tvref(L->stack)+1+LJ_FR2; - L->cframe = NULL; - unwindstack(L, L->base); - if (G(L)->panic) - G(L)->panic(L); - exit(EXIT_FAILURE); - } - return L; /* Anything non-NULL will do. */ -} - -/* -- External frame unwinding -------------------------------------------- */ - -#if LJ_ABI_WIN - -/* -** Someone in Redmond owes me several days of my life. A lot of this is -** undocumented or just plain wrong on MSDN. Some of it can be gathered -** from 3rd party docs or must be found by trial-and-error. They really -** don't want you to write your own language-specific exception handler -** or to interact gracefully with MSVC. :-( -** -** Apparently MSVC doesn't call C++ destructors for foreign exceptions -** unless you compile your C++ code with /EHa. Unfortunately this means -** catch (...) also catches things like access violations. The use of -** _set_se_translator doesn't really help, because it requires /EHa, too. -*/ - -#define WIN32_LEAN_AND_MEAN -#include - -#if LJ_TARGET_X86 -typedef void *UndocumentedDispatcherContext; /* Unused on x86. */ -#else -/* Taken from: http://www.nynaeve.net/?p=99 */ -typedef struct UndocumentedDispatcherContext { - ULONG64 ControlPc; - ULONG64 ImageBase; - PRUNTIME_FUNCTION FunctionEntry; - ULONG64 EstablisherFrame; - ULONG64 TargetIp; - PCONTEXT ContextRecord; - void (*LanguageHandler)(void); - PVOID HandlerData; - PUNWIND_HISTORY_TABLE HistoryTable; - ULONG ScopeIndex; - ULONG Fill0; -} UndocumentedDispatcherContext; -#endif - -/* Another wild guess. */ -extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow); - -#if LJ_TARGET_X64 && defined(MINGW_SDK_INIT) -/* Workaround for broken MinGW64 declaration. */ -VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx"); -#define RtlUnwindEx RtlUnwindEx_FIXED -#endif - -#define LJ_MSVC_EXCODE ((DWORD)0xe06d7363) -#define LJ_GCC_EXCODE ((DWORD)0x20474343) - -#define LJ_EXCODE ((DWORD)0xe24c4a00) -#define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c)) -#define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff) -#define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff)) - -/* Windows exception handler for interpreter frame. */ -LJ_FUNCA int lj_err_unwind_win(EXCEPTION_RECORD *rec, - void *f, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch) -{ -#if LJ_TARGET_X86 - void *cf = (char *)f - CFRAME_OFS_SEH; -#else - void *cf = f; -#endif - lua_State *L = cframe_L(cf); - int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ? - LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN; - if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */ - /* Unwind internal frames. */ - err_unwind(L, cf, errcode); - } else { - void *cf2 = err_unwind(L, cf, 0); - if (cf2) { /* We catch it, so start unwinding the upper frames. */ - if (rec->ExceptionCode == LJ_MSVC_EXCODE || - rec->ExceptionCode == LJ_GCC_EXCODE) { -#if !LJ_TARGET_CYGWIN - __DestructExceptionObject(rec, 1); -#endif - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); - } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) { - /* Don't catch access violations etc. */ - return 1; /* ExceptionContinueSearch */ - } -#if LJ_TARGET_X86 - UNUSED(ctx); - UNUSED(dispatch); - /* Call all handlers for all lower C frames (including ourselves) again - ** with EH_UNWINDING set. Then call the specified function, passing cf - ** and errcode. - */ - lj_vm_rtlunwind(cf, (void *)rec, - (cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ? - (void *)lj_vm_unwind_ff : (void *)lj_vm_unwind_c, errcode); - /* lj_vm_rtlunwind does not return. */ -#else - /* Unwind the stack and call all handlers for all lower C frames - ** (including ourselves) again with EH_UNWINDING set. Then set - ** stack pointer = cf, result = errcode and jump to the specified target. - */ - RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ? - lj_vm_unwind_ff_eh : - lj_vm_unwind_c_eh), - rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable); - /* RtlUnwindEx should never return. */ -#endif - } - } - return 1; /* ExceptionContinueSearch */ -} - -#if LJ_UNWIND_JIT - -#if LJ_TARGET_X64 -#define CONTEXT_REG_PC Rip -#elif LJ_TARGET_ARM64 -#define CONTEXT_REG_PC Pc -#else -#error "NYI: Windows arch-specific unwinder for JIT-compiled code" -#endif - -/* Windows unwinder for JIT-compiled code. */ -static void err_unwind_win_jit(global_State *g, int errcode) -{ - CONTEXT ctx; - UNWIND_HISTORY_TABLE hist; - - memset(&hist, 0, sizeof(hist)); - RtlCaptureContext(&ctx); - while (1) { - uintptr_t frame, base, addr = ctx.CONTEXT_REG_PC; - void *hdata; - PRUNTIME_FUNCTION func = RtlLookupFunctionEntry(addr, &base, &hist); - if (!func) { /* Found frame without .pdata: must be JIT-compiled code. */ - ExitNo exitno; - uintptr_t stub = lj_trace_unwind(G2J(g), addr - sizeof(MCode), &exitno); - if (stub) { /* Jump to side exit to unwind the trace. */ - ctx.CONTEXT_REG_PC = stub; - G2J(g)->exitcode = errcode; - RtlRestoreContext(&ctx, NULL); /* Does not return. */ - } - break; - } - RtlVirtualUnwind(UNW_FLAG_NHANDLER, base, addr, func, - &ctx, &hdata, &frame, NULL); - if (!addr) break; - } - /* Unwinding failed, if we end up here. */ -} -#endif - -/* Raise Windows exception. */ -static void err_raise_ext(global_State *g, int errcode) -{ -#if LJ_UNWIND_JIT - if (tvref(g->jit_base)) { - err_unwind_win_jit(g, errcode); - return; /* Unwinding failed. */ - } -#elif LJ_HASJIT - /* Cannot catch on-trace errors for Windows/x86 SEH. Unwind to interpreter. */ - setmref(g->jit_base, NULL); -#endif - UNUSED(g); - RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL); -} - -#elif !LJ_NO_UNWIND && (defined(__GNUC__) || defined(__clang__)) - -/* -** We have to use our own definitions instead of the mandatory (!) unwind.h, -** since various OS, distros and compilers mess up the header installation. -*/ - -typedef struct _Unwind_Context _Unwind_Context; - -#define _URC_OK 0 -#define _URC_FATAL_PHASE2_ERROR 2 -#define _URC_FATAL_PHASE1_ERROR 3 -#define _URC_HANDLER_FOUND 6 -#define _URC_INSTALL_CONTEXT 7 -#define _URC_CONTINUE_UNWIND 8 -#define _URC_FAILURE 9 - -#define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */ -#define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c)) -#define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff) -#define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff)) - -#if !LJ_TARGET_ARM - -typedef struct _Unwind_Exception -{ - uint64_t exclass; - void (*excleanup)(int, struct _Unwind_Exception *); - uintptr_t p1, p2; -} __attribute__((__aligned__)) _Unwind_Exception; -#define UNWIND_EXCEPTION_TYPE _Unwind_Exception - -extern uintptr_t _Unwind_GetCFA(_Unwind_Context *); -extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t); -extern uintptr_t _Unwind_GetIP(_Unwind_Context *); -extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t); -extern void _Unwind_DeleteException(_Unwind_Exception *); -extern int _Unwind_RaiseException(_Unwind_Exception *); - -#define _UA_SEARCH_PHASE 1 -#define _UA_CLEANUP_PHASE 2 -#define _UA_HANDLER_FRAME 4 -#define _UA_FORCE_UNWIND 8 - -/* DWARF2 personality handler referenced from interpreter .eh_frame. */ -LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions, - uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx) -{ - void *cf; - lua_State *L; - if (version != 1) - return _URC_FATAL_PHASE1_ERROR; - cf = (void *)_Unwind_GetCFA(ctx); - L = cframe_L(cf); - if ((actions & _UA_SEARCH_PHASE)) { -#if LJ_UNWIND_EXT - if (err_unwind(L, cf, 0) == NULL) - return _URC_CONTINUE_UNWIND; -#endif - if (!LJ_UEXCLASS_CHECK(uexclass)) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); - } - return _URC_HANDLER_FOUND; - } - if ((actions & _UA_CLEANUP_PHASE)) { - int errcode; - if (LJ_UEXCLASS_CHECK(uexclass)) { - errcode = LJ_UEXCLASS_ERRCODE(uexclass); - } else { - if ((actions & _UA_HANDLER_FRAME)) - _Unwind_DeleteException(uex); - errcode = LUA_ERRRUN; - } -#if LJ_UNWIND_EXT - cf = err_unwind(L, cf, errcode); - if ((actions & _UA_FORCE_UNWIND)) { - return _URC_CONTINUE_UNWIND; - } else if (cf) { - _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode); - _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ? - lj_vm_unwind_ff_eh : - lj_vm_unwind_c_eh)); - return _URC_INSTALL_CONTEXT; - } -#if LJ_TARGET_X86ORX64 - else if ((actions & _UA_HANDLER_FRAME)) { - /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/ - ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h - */ - _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode); - _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow); - return _URC_INSTALL_CONTEXT; - } -#endif -#else - /* This is not the proper way to escape from the unwinder. We get away with - ** it on non-x64 because the interpreter restores all callee-saved regs. - */ - lj_err_throw(L, errcode); -#if LJ_TARGET_X64 -#error "Broken build system -- only use the provided Makefiles!" -#endif -#endif - } - return _URC_CONTINUE_UNWIND; -} - -#if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT) -struct dwarf_eh_bases { void *tbase, *dbase, *func; }; -extern const void *_Unwind_Find_FDE(void *pc, struct dwarf_eh_bases *bases); - -/* Verify that external error handling actually has a chance to work. */ -void lj_err_verify(void) -{ -#if !LJ_TARGET_OSX - /* Check disabled on MacOS due to brilliant software engineering at Apple. */ - struct dwarf_eh_bases ehb; - lj_assertX(_Unwind_Find_FDE((void *)lj_err_throw, &ehb), "broken build: external frame unwinding enabled, but missing -funwind-tables"); -#endif - /* Check disabled, because of broken Fedora/ARM64. See #722. - lj_assertX(_Unwind_Find_FDE((void *)_Unwind_RaiseException, &ehb), "broken build: external frame unwinding enabled, but system libraries have no unwind tables"); - */ -} -#endif - -#if LJ_UNWIND_JIT -/* DWARF2 personality handler for JIT-compiled code. */ -static int err_unwind_jit(int version, int actions, - uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx) -{ - /* NYI: FFI C++ exception interoperability. */ - if (version != 1 || !LJ_UEXCLASS_CHECK(uexclass)) - return _URC_FATAL_PHASE1_ERROR; - if ((actions & _UA_SEARCH_PHASE)) { - return _URC_HANDLER_FOUND; - } - if ((actions & _UA_CLEANUP_PHASE)) { - global_State *g = *(global_State **)(uex+1); - ExitNo exitno; - uintptr_t addr = _Unwind_GetIP(ctx); /* Return address _after_ call. */ - uintptr_t stub = lj_trace_unwind(G2J(g), addr - sizeof(MCode), &exitno); - lj_assertG(tvref(g->jit_base), "unexpected throw across mcode frame"); - if (stub) { /* Jump to side exit to unwind the trace. */ - G2J(g)->exitcode = LJ_UEXCLASS_ERRCODE(uexclass); -#ifdef LJ_TARGET_MIPS - _Unwind_SetGR(ctx, 4, stub); - _Unwind_SetGR(ctx, 5, exitno); - _Unwind_SetIP(ctx, (uintptr_t)(void *)lj_vm_unwind_stub); -#else - _Unwind_SetIP(ctx, stub); -#endif - return _URC_INSTALL_CONTEXT; - } - return _URC_FATAL_PHASE2_ERROR; - } - return _URC_FATAL_PHASE1_ERROR; -} - -/* DWARF2 template frame info for JIT-compiled code. -** -** After copying the template to the start of the mcode segment, -** the frame handler function and the code size is patched. -** The frame handler always installs a new context to jump to the exit, -** so don't bother to add any unwind opcodes. -*/ -static const uint8_t err_frame_jit_template[] = { -#if LJ_BE - 0,0,0, -#endif - LJ_64 ? 0x1c : 0x14, /* CIE length. */ -#if LJ_LE - 0,0,0, -#endif - 0,0,0,0, 1, 'z','P','R',0, /* CIE mark, CIE version, augmentation. */ - 1, LJ_64 ? 0x78 : 0x7c, LJ_TARGET_EHRAREG, /* Code/data align, RA. */ -#if LJ_64 - 10, 0, 0,0,0,0,0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */ - 0,0,0,0,0, /* Alignment. */ -#else - 6, 0, 0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */ - 0, /* Alignment. */ -#endif -#if LJ_BE - 0,0,0, -#endif - LJ_64 ? 0x14 : 0x10, /* FDE length. */ - 0,0,0, - LJ_64 ? 0x24 : 0x1c, /* CIE offset. */ - 0,0,0, - LJ_64 ? 0x14 : 0x10, /* Code offset. After Final FDE. */ -#if LJ_LE - 0,0,0, -#endif - 0,0,0,0, 0, 0,0,0, /* Code size, augmentation length, alignment. */ -#if LJ_64 - 0,0,0,0, /* Alignment. */ -#endif - 0,0,0,0 /* Final FDE. */ -}; - -#define ERR_FRAME_JIT_OFS_HANDLER 0x12 -#define ERR_FRAME_JIT_OFS_FDE (LJ_64 ? 0x20 : 0x18) -#define ERR_FRAME_JIT_OFS_CODE_SIZE (LJ_64 ? 0x2c : 0x24) -#if LJ_TARGET_OSX -#define ERR_FRAME_JIT_OFS_REGISTER ERR_FRAME_JIT_OFS_FDE -#else -#define ERR_FRAME_JIT_OFS_REGISTER 0 -#endif - -extern void __register_frame(const void *); -extern void __deregister_frame(const void *); - -uint8_t *lj_err_register_mcode(void *base, size_t sz, uint8_t *info) -{ - void **handler; - memcpy(info, err_frame_jit_template, sizeof(err_frame_jit_template)); - handler = (void *)err_unwind_jit; - memcpy(info + ERR_FRAME_JIT_OFS_HANDLER, &handler, sizeof(handler)); - *(uint32_t *)(info + ERR_FRAME_JIT_OFS_CODE_SIZE) = - (uint32_t)(sz - sizeof(err_frame_jit_template) - (info - (uint8_t *)base)); - __register_frame(info + ERR_FRAME_JIT_OFS_REGISTER); -#ifdef LUA_USE_ASSERT - { - struct dwarf_eh_bases ehb; - lj_assertX(_Unwind_Find_FDE(info + sizeof(err_frame_jit_template)+1, &ehb), - "bad JIT unwind table registration"); - } -#endif - return info + sizeof(err_frame_jit_template); -} - -void lj_err_deregister_mcode(void *base, size_t sz, uint8_t *info) -{ - UNUSED(base); UNUSED(sz); - __deregister_frame(info + ERR_FRAME_JIT_OFS_REGISTER); -} -#endif - -#else /* LJ_TARGET_ARM */ - -#define _US_VIRTUAL_UNWIND_FRAME 0 -#define _US_UNWIND_FRAME_STARTING 1 -#define _US_ACTION_MASK 3 -#define _US_FORCE_UNWIND 8 - -typedef struct _Unwind_Control_Block _Unwind_Control_Block; -#define UNWIND_EXCEPTION_TYPE _Unwind_Control_Block - -struct _Unwind_Control_Block { - uint64_t exclass; - uint32_t misc[20]; -}; - -extern int _Unwind_RaiseException(_Unwind_Control_Block *); -extern int __gnu_unwind_frame(_Unwind_Control_Block *, _Unwind_Context *); -extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *); -extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *); - -static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r) -{ - uint32_t v; - _Unwind_VRS_Get(ctx, 0, r, 0, &v); - return v; -} - -static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v) -{ - _Unwind_VRS_Set(ctx, 0, r, 0, &v); -} - -extern void lj_vm_unwind_ext(void); - -/* ARM unwinder personality handler referenced from interpreter .ARM.extab. */ -LJ_FUNCA int lj_err_unwind_arm(int state, _Unwind_Control_Block *ucb, - _Unwind_Context *ctx) -{ - void *cf = (void *)_Unwind_GetGR(ctx, 13); - lua_State *L = cframe_L(cf); - int errcode; - - switch ((state & _US_ACTION_MASK)) { - case _US_VIRTUAL_UNWIND_FRAME: - if ((state & _US_FORCE_UNWIND)) break; - return _URC_HANDLER_FOUND; - case _US_UNWIND_FRAME_STARTING: - if (LJ_UEXCLASS_CHECK(ucb->exclass)) { - errcode = LJ_UEXCLASS_ERRCODE(ucb->exclass); - } else { - errcode = LUA_ERRRUN; - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); - } - cf = err_unwind(L, cf, errcode); - if ((state & _US_FORCE_UNWIND) || cf == NULL) break; - _Unwind_SetGR(ctx, 15, (uint32_t)lj_vm_unwind_ext); - _Unwind_SetGR(ctx, 0, (uint32_t)ucb); - _Unwind_SetGR(ctx, 1, (uint32_t)errcode); - _Unwind_SetGR(ctx, 2, cframe_unwind_ff(cf) ? - (uint32_t)lj_vm_unwind_ff_eh : - (uint32_t)lj_vm_unwind_c_eh); - return _URC_INSTALL_CONTEXT; - default: - return _URC_FAILURE; - } - if (__gnu_unwind_frame(ucb, ctx) != _URC_OK) - return _URC_FAILURE; -#ifdef LUA_USE_ASSERT - /* We should never get here unless this is a forced unwind aka backtrace. */ - if (_Unwind_GetGR(ctx, 0) == 0xff33aa77) { - _Unwind_SetGR(ctx, 0, 0xff33aa88); - } -#endif - return _URC_CONTINUE_UNWIND; -} - -#if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT) -typedef int (*_Unwind_Trace_Fn)(_Unwind_Context *, void *); -extern int _Unwind_Backtrace(_Unwind_Trace_Fn, void *); - -static int err_verify_bt(_Unwind_Context *ctx, int *got) -{ - if (_Unwind_GetGR(ctx, 0) == 0xff33aa88) { *got = 2; } - else if (*got == 0) { *got = 1; _Unwind_SetGR(ctx, 0, 0xff33aa77); } - return _URC_OK; -} - -/* Verify that external error handling actually has a chance to work. */ -void lj_err_verify(void) -{ - int got = 0; - _Unwind_Backtrace((_Unwind_Trace_Fn)err_verify_bt, &got); - lj_assertX(got == 2, "broken build: external frame unwinding enabled, but missing -funwind-tables"); -} -#endif - -/* -** Note: LJ_UNWIND_JIT is not implemented for 32 bit ARM. -** -** The quirky ARM unwind API doesn't have __register_frame(). -** A potential workaround might involve _Unwind_Backtrace. -** But most 32 bit ARM targets don't qualify for LJ_UNWIND_EXT, anyway, -** since they are built without unwind tables by default. -*/ - -#endif /* LJ_TARGET_ARM */ - - -#if LJ_UNWIND_EXT -static __thread struct { - UNWIND_EXCEPTION_TYPE ex; - global_State *g; -} static_uex; - -/* Raise external exception. */ -static void err_raise_ext(global_State *g, int errcode) -{ - memset(&static_uex, 0, sizeof(static_uex)); - static_uex.ex.exclass = LJ_UEXCLASS_MAKE(errcode); - static_uex.g = g; - _Unwind_RaiseException(&static_uex.ex); -} - -#endif - -#endif - -/* -- Error handling ------------------------------------------------------ */ - -/* Throw error. Find catch frame, unwind stack and continue. */ -LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode) -{ - global_State *g = G(L); - lj_trace_abort(g); - L->status = LUA_OK; -#if LJ_UNWIND_EXT - err_raise_ext(g, errcode); - /* - ** A return from this function signals a corrupt C stack that cannot be - ** unwound. We have no choice but to call the panic function and exit. - ** - ** Usually this is caused by a C function without unwind information. - ** This may happen if you've manually enabled LUAJIT_UNWIND_EXTERNAL - ** and forgot to recompile *every* non-C++ file with -funwind-tables. - */ - if (G(L)->panic) - G(L)->panic(L); -#else -#if LJ_HASJIT - setmref(g->jit_base, NULL); -#endif - { - void *cf = err_unwind(L, NULL, errcode); - if (cframe_unwind_ff(cf)) - lj_vm_unwind_ff(cframe_raw(cf)); - else - lj_vm_unwind_c(cframe_raw(cf), errcode); - } -#endif - exit(EXIT_FAILURE); -} - -/* Return string object for error message. */ -LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em) -{ - return lj_str_newz(L, err2msg(em)); -} - -/* Out-of-memory error. */ -LJ_NOINLINE void lj_err_mem(lua_State *L) -{ - if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */ - lj_vm_unwind_c(L->cframe, LUA_ERRMEM); - if (curr_funcisL(L)) L->top = curr_topL(L); - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM)); - lj_err_throw(L, LUA_ERRMEM); -} - -/* Find error function for runtime errors. Requires an extra stack traversal. */ -static ptrdiff_t finderrfunc(lua_State *L) -{ - cTValue *frame = L->base-1, *bot = tvref(L->stack)+LJ_FR2; - void *cf = L->cframe; - while (frame > bot && cf) { - while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */ - if (frame >= restorestack(L, -cframe_nres(cf))) - break; - if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */ - return cframe_errfunc(cf); - cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */ - if (cf == NULL) - return 0; - } - switch (frame_typep(frame)) { - case FRAME_LUA: - case FRAME_LUAP: - frame = frame_prevl(frame); - break; - case FRAME_C: - cf = cframe_prev(cf); - /* fallthrough */ - case FRAME_VARG: - frame = frame_prevd(frame); - break; - case FRAME_CONT: - if (frame_iscont_fficb(frame)) - cf = cframe_prev(cf); - frame = frame_prevd(frame); - break; - case FRAME_CP: - if (cframe_canyield(cf)) return 0; - if (cframe_errfunc(cf) >= 0) - return cframe_errfunc(cf); - cf = cframe_prev(cf); - frame = frame_prevd(frame); - break; - case FRAME_PCALL: - case FRAME_PCALLH: - if (frame_func(frame_prevd(frame))->c.ffid == FF_xpcall) - return savestack(L, frame_prevd(frame)+1); /* xpcall's errorfunc. */ - return 0; - default: - lj_assertL(0, "bad frame type"); - return 0; - } - } - return 0; -} - -/* Runtime error. */ -LJ_NOINLINE void LJ_FASTCALL lj_err_run(lua_State *L) -{ - ptrdiff_t ef = (LJ_HASJIT && tvref(G(L)->jit_base)) ? 0 : finderrfunc(L); - if (ef) { - TValue *errfunc = restorestack(L, ef); - TValue *top = L->top; - lj_trace_abort(G(L)); - if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) { - setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR)); - lj_err_throw(L, LUA_ERRERR); - } - L->status = LUA_ERRERR; - copyTV(L, top+LJ_FR2, top-1); - copyTV(L, top-1, errfunc); - if (LJ_FR2) setnilV(top++); - L->top = top+1; - lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */ - } - lj_err_throw(L, LUA_ERRRUN); -} - -#if LJ_HASJIT -LJ_NOINLINE void LJ_FASTCALL lj_err_trace(lua_State *L, int errcode) -{ - if (errcode == LUA_ERRRUN) - lj_err_run(L); - else - lj_err_throw(L, errcode); -} -#endif - -/* Formatted runtime error message. */ -LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...) -{ - const char *msg; - va_list argp; - va_start(argp, em); - if (curr_funcisL(L)) L->top = curr_topL(L); - msg = lj_strfmt_pushvf(L, err2msg(em), argp); - va_end(argp); - lj_debug_addloc(L, msg, L->base-1, NULL); - lj_err_run(L); -} - -/* Non-vararg variant for better calling conventions. */ -LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em) -{ - err_msgv(L, em); -} - -/* Lexer error. */ -LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok, - BCLine line, ErrMsg em, va_list argp) -{ - char buff[LUA_IDSIZE]; - const char *msg; - lj_debug_shortname(buff, src, line); - msg = lj_strfmt_pushvf(L, err2msg(em), argp); - msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg); - if (tok) - lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok); - lj_err_throw(L, LUA_ERRSYNTAX); -} - -/* Typecheck error for operands. */ -LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm) -{ - const char *tname = lj_typename(o); - const char *opname = err2msg(opm); - if (curr_funcisL(L)) { - GCproto *pt = curr_proto(L); - const BCIns *pc = cframe_Lpc(L) - 1; - const char *oname = NULL; - const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname); - if (kind) - err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname); - } - err_msgv(L, LJ_ERR_BADOPRV, opname, tname); -} - -/* Typecheck error for ordered comparisons. */ -LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2) -{ - const char *t1 = lj_typename(o1); - const char *t2 = lj_typename(o2); - err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2); - /* This assumes the two "boolean" entries are commoned by the C compiler. */ -} - -/* Typecheck error for __call. */ -LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o) -{ - /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object: - ** L->base still points to the caller. So add a dummy frame with L instead - ** of a function. See lua_getstack(). - */ - const BCIns *pc = cframe_Lpc(L); - if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) { - const char *tname = lj_typename(o); - setframe_gc(o, obj2gco(L), LJ_TTHREAD); - if (LJ_FR2) o++; - setframe_pc(o, pc); - L->top = L->base = o+1; - err_msgv(L, LJ_ERR_BADCALL, tname); - } - lj_err_optype(L, o, LJ_ERR_OPCALL); -} - -/* Error in context of caller. */ -LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg) -{ - TValue *frame = NULL, *pframe = NULL; - if (!(LJ_HASJIT && tvref(G(L)->jit_base))) { - frame = L->base-1; - if (frame_islua(frame)) { - pframe = frame_prevl(frame); - } else if (frame_iscont(frame)) { - if (frame_iscont_fficb(frame)) { - pframe = frame; - frame = NULL; - } else { - pframe = frame_prevd(frame); -#if LJ_HASFFI - /* Remove frame for FFI metamethods. */ - if (frame_func(frame)->c.ffid >= FF_ffi_meta___index && - frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) { - L->base = pframe+1; - L->top = frame; - setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame)); - } -#endif - } - } - } - lj_debug_addloc(L, msg, pframe, frame); - lj_err_run(L); -} - -/* Formatted error in context of caller. */ -LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...) -{ - const char *msg; - va_list argp; - va_start(argp, em); - msg = lj_strfmt_pushvf(L, err2msg(em), argp); - va_end(argp); - lj_err_callermsg(L, msg); -} - -/* Error in context of caller. */ -LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em) -{ - lj_err_callermsg(L, err2msg(em)); -} - -/* Argument error message. */ -LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg, - const char *msg) -{ - const char *fname = "?"; - const char *ftype = lj_debug_funcname(L, L->base - 1, &fname); - if (narg < 0 && narg > LUA_REGISTRYINDEX) - narg = (int)(L->top - L->base) + narg + 1; - if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */ - msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg); - else - msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg); - lj_err_callermsg(L, msg); -} - -/* Formatted argument error. */ -LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...) -{ - const char *msg; - va_list argp; - va_start(argp, em); - msg = lj_strfmt_pushvf(L, err2msg(em), argp); - va_end(argp); - err_argmsg(L, narg, msg); -} - -/* Argument error. */ -LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em) -{ - err_argmsg(L, narg, err2msg(em)); -} - -/* Typecheck error for arguments. */ -LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname) -{ - const char *tname, *msg; - if (narg <= LUA_REGISTRYINDEX) { - if (narg >= LUA_GLOBALSINDEX) { - tname = lj_obj_itypename[~LJ_TTAB]; - } else { - GCfunc *fn = curr_func(L); - int idx = LUA_GLOBALSINDEX - narg; - if (idx <= fn->c.nupvalues) - tname = lj_typename(&fn->c.upvalue[idx-1]); - else - tname = lj_obj_typename[0]; - } - } else { - TValue *o = narg < 0 ? L->top + narg : L->base + narg-1; - tname = o < L->top ? lj_typename(o) : lj_obj_typename[0]; - } - msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname); - err_argmsg(L, narg, msg); -} - -/* Typecheck error for arguments. */ -LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt) -{ - lj_err_argtype(L, narg, lj_obj_typename[tt+1]); -} - -/* -- Public error handling API ------------------------------------------- */ - -LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf) -{ - lua_CFunction old = G(L)->panic; - G(L)->panic = panicf; - return old; -} - -/* Forwarders for the public API (C calling convention and no LJ_NORET). */ -LUA_API int lua_error(lua_State *L) -{ - lj_err_run(L); - return 0; /* unreachable */ -} - -LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg) -{ - err_argmsg(L, narg, msg); - return 0; /* unreachable */ -} - -LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname) -{ - lj_err_argtype(L, narg, xname); - return 0; /* unreachable */ -} - -LUALIB_API void luaL_where(lua_State *L, int level) -{ - int size; - cTValue *frame = lj_debug_frame(L, level, &size); - lj_debug_addloc(L, "", frame, size ? frame+size : NULL); -} - -LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...) -{ - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = lj_strfmt_pushvf(L, fmt, argp); - va_end(argp); - lj_err_callermsg(L, msg); - return 0; /* unreachable */ -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.h deleted file mode 100644 index bd4de9a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_err.h +++ /dev/null @@ -1,58 +0,0 @@ -/* -** Error handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_ERR_H -#define _LJ_ERR_H - -#include - -#include "lj_obj.h" - -typedef enum { -#define ERRDEF(name, msg) \ - LJ_ERR_##name, LJ_ERR_##name##_ = LJ_ERR_##name + sizeof(msg)-1, -#include "lj_errmsg.h" - LJ_ERR__MAX -} ErrMsg; - -LJ_DATA const char *lj_err_allmsg; -#define err2msg(em) (lj_err_allmsg+(int)(em)) - -LJ_FUNC GCstr *lj_err_str(lua_State *L, ErrMsg em); -LJ_FUNCA_NORET void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode); -LJ_FUNC_NORET void lj_err_mem(lua_State *L); -LJ_FUNC_NORET void LJ_FASTCALL lj_err_run(lua_State *L); -#if LJ_HASJIT -LJ_FUNCA_NORET void LJ_FASTCALL lj_err_trace(lua_State *L, int errcode); -#endif -LJ_FUNC_NORET void lj_err_msg(lua_State *L, ErrMsg em); -LJ_FUNC_NORET void lj_err_lex(lua_State *L, GCstr *src, const char *tok, - BCLine line, ErrMsg em, va_list argp); -LJ_FUNC_NORET void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm); -LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2); -LJ_FUNC_NORET void lj_err_optype_call(lua_State *L, TValue *o); -LJ_FUNC_NORET void lj_err_callermsg(lua_State *L, const char *msg); -LJ_FUNC_NORET void lj_err_callerv(lua_State *L, ErrMsg em, ...); -LJ_FUNC_NORET void lj_err_caller(lua_State *L, ErrMsg em); -LJ_FUNC_NORET void lj_err_arg(lua_State *L, int narg, ErrMsg em); -LJ_FUNC_NORET void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...); -LJ_FUNC_NORET void lj_err_argtype(lua_State *L, int narg, const char *xname); -LJ_FUNC_NORET void lj_err_argt(lua_State *L, int narg, int tt); - -#if LJ_UNWIND_JIT && !LJ_ABI_WIN -LJ_FUNC uint8_t *lj_err_register_mcode(void *base, size_t sz, uint8_t *info); -LJ_FUNC void lj_err_deregister_mcode(void *base, size_t sz, uint8_t *info); -#else -#define lj_err_register_mcode(base, sz, info) (info) -#define lj_err_deregister_mcode(base, sz, info) UNUSED(base) -#endif - -#if LJ_UNWIND_EXT && !LJ_ABI_WIN && defined(LUA_USE_ASSERT) -LJ_FUNC void lj_err_verify(void); -#else -#define lj_err_verify() ((void)0) -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_errmsg.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_errmsg.h deleted file mode 100644 index 2e5c776..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_errmsg.h +++ /dev/null @@ -1,200 +0,0 @@ -/* -** VM error messages. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* This file may be included multiple times with different ERRDEF macros. */ - -/* Basic error handling. */ -ERRDEF(ERRMEM, "not enough memory") -ERRDEF(ERRERR, "error in error handling") -ERRDEF(ERRCPP, "C++ exception") - -/* Allocations. */ -ERRDEF(STROV, "string length overflow") -ERRDEF(UDATAOV, "userdata length overflow") -ERRDEF(STKOV, "stack overflow") -ERRDEF(STKOVM, "stack overflow (%s)") -ERRDEF(TABOV, "table overflow") - -/* Table indexing. */ -ERRDEF(NANIDX, "table index is NaN") -ERRDEF(NILIDX, "table index is nil") -ERRDEF(NEXTIDX, "invalid key to " LUA_QL("next")) - -/* Metamethod resolving. */ -ERRDEF(BADCALL, "attempt to call a %s value") -ERRDEF(BADOPRT, "attempt to %s %s " LUA_QS " (a %s value)") -ERRDEF(BADOPRV, "attempt to %s a %s value") -ERRDEF(BADCMPT, "attempt to compare %s with %s") -ERRDEF(BADCMPV, "attempt to compare two %s values") -ERRDEF(GETLOOP, "loop in gettable") -ERRDEF(SETLOOP, "loop in settable") -ERRDEF(OPCALL, "call") -ERRDEF(OPINDEX, "index") -ERRDEF(OPARITH, "perform arithmetic on") -ERRDEF(OPCAT, "concatenate") -ERRDEF(OPLEN, "get length of") - -/* Type checks. */ -ERRDEF(BADSELF, "calling " LUA_QS " on bad self (%s)") -ERRDEF(BADARG, "bad argument #%d to " LUA_QS " (%s)") -ERRDEF(BADTYPE, "%s expected, got %s") -ERRDEF(BADVAL, "invalid value") -ERRDEF(NOVAL, "value expected") -ERRDEF(NOCORO, "coroutine expected") -ERRDEF(NOTABN, "nil or table expected") -ERRDEF(NOLFUNC, "Lua function expected") -ERRDEF(NOFUNCL, "function or level expected") -ERRDEF(NOSFT, "string/function/table expected") -ERRDEF(NOPROXY, "boolean or proxy expected") -ERRDEF(FORINIT, LUA_QL("for") " initial value must be a number") -ERRDEF(FORLIM, LUA_QL("for") " limit must be a number") -ERRDEF(FORSTEP, LUA_QL("for") " step must be a number") - -/* C API checks. */ -ERRDEF(NOENV, "no calling environment") -ERRDEF(CYIELD, "attempt to yield across C-call boundary") -ERRDEF(BADLU, "bad light userdata pointer") -ERRDEF(NOGCMM, "bad action while in __gc metamethod") -#if LJ_TARGET_WINDOWS -ERRDEF(BADFPU, "bad FPU precision (use D3DCREATE_FPU_PRESERVE with DirectX)") -#endif - -/* Standard library function errors. */ -ERRDEF(ASSERT, "assertion failed!") -ERRDEF(PROTMT, "cannot change a protected metatable") -ERRDEF(UNPACK, "too many results to unpack") -ERRDEF(RDRSTR, "reader function must return a string") -ERRDEF(PRTOSTR, LUA_QL("tostring") " must return a string to " LUA_QL("print")) -ERRDEF(NUMRNG, "number out of range") -ERRDEF(IDXRNG, "index out of range") -ERRDEF(BASERNG, "base out of range") -ERRDEF(LVLRNG, "level out of range") -ERRDEF(INVLVL, "invalid level") -ERRDEF(INVOPT, "invalid option") -ERRDEF(INVOPTM, "invalid option " LUA_QS) -ERRDEF(INVFMT, "invalid format") -ERRDEF(SETFENV, LUA_QL("setfenv") " cannot change environment of given object") -ERRDEF(CORUN, "cannot resume running coroutine") -ERRDEF(CODEAD, "cannot resume dead coroutine") -ERRDEF(COSUSP, "cannot resume non-suspended coroutine") -ERRDEF(TABINS, "wrong number of arguments to " LUA_QL("insert")) -ERRDEF(TABCAT, "invalid value (%s) at index %d in table for " LUA_QL("concat")) -ERRDEF(TABSORT, "invalid order function for sorting") -ERRDEF(IOCLFL, "attempt to use a closed file") -ERRDEF(IOSTDCL, "standard file is closed") -ERRDEF(OSUNIQF, "unable to generate a unique filename") -ERRDEF(OSDATEF, "field " LUA_QS " missing in date table") -ERRDEF(STRDUMP, "unable to dump given function") -ERRDEF(STRSLC, "string slice too long") -ERRDEF(STRPATB, "missing " LUA_QL("[") " after " LUA_QL("%f") " in pattern") -ERRDEF(STRPATC, "invalid pattern capture") -ERRDEF(STRPATE, "malformed pattern (ends with " LUA_QL("%") ")") -ERRDEF(STRPATM, "malformed pattern (missing " LUA_QL("]") ")") -ERRDEF(STRPATU, "unbalanced pattern") -ERRDEF(STRPATX, "pattern too complex") -ERRDEF(STRCAPI, "invalid capture index") -ERRDEF(STRCAPN, "too many captures") -ERRDEF(STRCAPU, "unfinished capture") -ERRDEF(STRFMT, "invalid option " LUA_QS " to " LUA_QL("format")) -ERRDEF(STRGSRV, "invalid replacement value (a %s)") -ERRDEF(BADMODN, "name conflict for module " LUA_QS) -#if LJ_HASJIT -ERRDEF(JITPROT, "runtime code generation failed, restricted kernel?") -ERRDEF(NOJIT, "JIT compiler disabled") -#elif defined(LJ_ARCH_NOJIT) -ERRDEF(NOJIT, "no JIT compiler for this architecture (yet)") -#else -ERRDEF(NOJIT, "JIT compiler permanently disabled by build option") -#endif -ERRDEF(JITOPT, "unknown or malformed optimization flag " LUA_QS) - -/* Lexer/parser errors. */ -ERRDEF(XMODE, "attempt to load chunk with wrong mode") -ERRDEF(XNEAR, "%s near " LUA_QS) -ERRDEF(XLINES, "chunk has too many lines") -ERRDEF(XLEVELS, "chunk has too many syntax levels") -ERRDEF(XNUMBER, "malformed number") -ERRDEF(XLSTR, "unfinished long string") -ERRDEF(XLCOM, "unfinished long comment") -ERRDEF(XSTR, "unfinished string") -ERRDEF(XESC, "invalid escape sequence") -ERRDEF(XLDELIM, "invalid long string delimiter") -ERRDEF(XTOKEN, LUA_QS " expected") -ERRDEF(XJUMP, "control structure too long") -ERRDEF(XSLOTS, "function or expression too complex") -ERRDEF(XLIMC, "chunk has more than %d local variables") -ERRDEF(XLIMM, "main function has more than %d %s") -ERRDEF(XLIMF, "function at line %d has more than %d %s") -ERRDEF(XMATCH, LUA_QS " expected (to close " LUA_QS " at line %d)") -ERRDEF(XFIXUP, "function too long for return fixup") -ERRDEF(XPARAM, " or " LUA_QL("...") " expected") -#if !LJ_52 -ERRDEF(XAMBIG, "ambiguous syntax (function call x new statement)") -#endif -ERRDEF(XFUNARG, "function arguments expected") -ERRDEF(XSYMBOL, "unexpected symbol") -ERRDEF(XDOTS, "cannot use " LUA_QL("...") " outside a vararg function") -ERRDEF(XSYNTAX, "syntax error") -ERRDEF(XFOR, LUA_QL("=") " or " LUA_QL("in") " expected") -ERRDEF(XBREAK, "no loop to break") -ERRDEF(XLUNDEF, "undefined label " LUA_QS) -ERRDEF(XLDUP, "duplicate label " LUA_QS) -ERRDEF(XGSCOPE, " jumps into the scope of local " LUA_QS) - -/* Bytecode reader errors. */ -ERRDEF(BCFMT, "cannot load incompatible bytecode") -ERRDEF(BCBAD, "cannot load malformed bytecode") - -#if LJ_HASFFI -/* FFI errors. */ -ERRDEF(FFI_INVTYPE, "invalid C type") -ERRDEF(FFI_INVSIZE, "size of C type is unknown or too large") -ERRDEF(FFI_BADSCL, "bad storage class") -ERRDEF(FFI_DECLSPEC, "declaration specifier expected") -ERRDEF(FFI_BADTAG, "undeclared or implicit tag " LUA_QS) -ERRDEF(FFI_REDEF, "attempt to redefine " LUA_QS) -ERRDEF(FFI_NUMPARAM, "wrong number of type parameters") -ERRDEF(FFI_INITOV, "too many initializers for " LUA_QS) -ERRDEF(FFI_BADCONV, "cannot convert " LUA_QS " to " LUA_QS) -ERRDEF(FFI_BADLEN, "attempt to get length of " LUA_QS) -ERRDEF(FFI_BADCONCAT, "attempt to concatenate " LUA_QS " and " LUA_QS) -ERRDEF(FFI_BADARITH, "attempt to perform arithmetic on " LUA_QS " and " LUA_QS) -ERRDEF(FFI_BADCOMP, "attempt to compare " LUA_QS " with " LUA_QS) -ERRDEF(FFI_BADCALL, LUA_QS " is not callable") -ERRDEF(FFI_NUMARG, "wrong number of arguments for function call") -ERRDEF(FFI_BADMEMBER, LUA_QS " has no member named " LUA_QS) -ERRDEF(FFI_BADIDX, LUA_QS " cannot be indexed") -ERRDEF(FFI_BADIDXW, LUA_QS " cannot be indexed with " LUA_QS) -ERRDEF(FFI_BADMM, LUA_QS " has no " LUA_QS " metamethod") -ERRDEF(FFI_WRCONST, "attempt to write to constant location") -ERRDEF(FFI_NODECL, "missing declaration for symbol " LUA_QS) -ERRDEF(FFI_BADCBACK, "bad callback") -#if LJ_OS_NOJIT -ERRDEF(FFI_CBACKOV, "no support for callbacks on this OS") -#else -ERRDEF(FFI_CBACKOV, "too many callbacks") -#endif -ERRDEF(FFI_NYIPACKBIT, "NYI: packed bit fields") -ERRDEF(FFI_NYICALL, "NYI: cannot call this C function (yet)") -#endif - -#if LJ_HASBUFFER -/* String buffer errors. */ -ERRDEF(BUFFER_SELF, "cannot put buffer into itself") -ERRDEF(BUFFER_BADOPT, "bad options table") -ERRDEF(BUFFER_BADENC, "cannot serialize " LUA_QS) -ERRDEF(BUFFER_BADDEC, "cannot deserialize tag 0x%02x") -ERRDEF(BUFFER_BADDICTX, "cannot deserialize dictionary index %d") -ERRDEF(BUFFER_DEPTH, "too deep to serialize") -ERRDEF(BUFFER_DUPKEY, "duplicate table key") -ERRDEF(BUFFER_EOB, "unexpected end of buffer") -ERRDEF(BUFFER_LEFTOV, "left-over data in buffer") -#endif - -#undef ERRDEF - -/* Detecting unused error messages: - awk -F, '/^ERRDEF/ { gsub(/ERRDEF./, ""); printf "grep -q LJ_ERR_%s *.[ch] || echo %s\n", $1, $1}' lj_errmsg.h | sh -*/ diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ff.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ff.h deleted file mode 100644 index d00c32f..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ff.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -** Fast function IDs. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FF_H -#define _LJ_FF_H - -/* Fast function ID. */ -typedef enum { - FF_LUA_ = FF_LUA, /* Lua function (must be 0). */ - FF_C_ = FF_C, /* Regular C function (must be 1). */ -#define FFDEF(name) FF_##name, -#include "lj_ffdef.h" - FF__MAX -} FastFunc; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.c deleted file mode 100644 index 022de1a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.c +++ /dev/null @@ -1,1574 +0,0 @@ -/* -** Fast function call recorder. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_ffrecord_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_ff.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_record.h" -#include "lj_ffrecord.h" -#include "lj_crecord.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" -#include "lj_serialize.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* -- Fast function recording handlers ------------------------------------ */ - -/* Conventions for fast function call handlers: -** -** The argument slots start at J->base[0]. All of them are guaranteed to be -** valid and type-specialized references. J->base[J->maxslot] is set to 0 -** as a sentinel. The runtime argument values start at rd->argv[0]. -** -** In general fast functions should check for presence of all of their -** arguments and for the correct argument types. Some simplifications -** are allowed if the interpreter throws instead. But even if recording -** is aborted, the generated IR must be consistent (no zero-refs). -** -** The number of results in rd->nres is set to 1. Handlers that return -** a different number of results need to override it. A negative value -** prevents return processing (e.g. for pending calls). -** -** Results need to be stored starting at J->base[0]. Return processing -** moves them to the right slots later. -** -** The per-ffid auxiliary data is the value of the 2nd part of the -** LJLIB_REC() annotation. This allows handling similar functionality -** in a common handler. -*/ - -/* Type of handler to record a fast function. */ -typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd); - -/* Get runtime value of int argument. */ -static int32_t argv2int(jit_State *J, TValue *o) -{ - if (!lj_strscan_numberobj(o)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - return tvisint(o) ? intV(o) : lj_num2int(numV(o)); -} - -/* Get runtime value of string argument. */ -static GCstr *argv2str(jit_State *J, TValue *o) -{ - if (LJ_LIKELY(tvisstr(o))) { - return strV(o); - } else { - GCstr *s; - if (!tvisnumber(o)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - s = lj_strfmt_number(J->L, o); - setstrV(J->L, o, s); - return s; - } -} - -/* Return number of results wanted by caller. */ -static ptrdiff_t results_wanted(jit_State *J) -{ - TValue *frame = J->L->base-1; - if (frame_islua(frame)) - return (ptrdiff_t)bc_b(frame_pc(frame)[-1]) - 1; - else - return -1; -} - -/* Trace stitching: add continuation below frame to start a new trace. */ -static void recff_stitch(jit_State *J) -{ - ASMFunction cont = lj_cont_stitch; - lua_State *L = J->L; - TValue *base = L->base; - BCReg nslot = J->maxslot + 1 + LJ_FR2; - TValue *nframe = base + 1 + LJ_FR2; - const BCIns *pc = frame_pc(base-1); - TValue *pframe = frame_prevl(base-1); - - /* Check for this now. Throwing in lj_record_stop messes up the stack. */ - if (J->cur.nsnap >= (MSize)J->param[JIT_P_maxsnap]) - lj_trace_err(J, LJ_TRERR_SNAPOV); - - /* Move func + args up in Lua stack and insert continuation. */ - memmove(&base[1], &base[-1-LJ_FR2], sizeof(TValue)*nslot); - setframe_ftsz(nframe, ((char *)nframe - (char *)pframe) + FRAME_CONT); - setcont(base-LJ_FR2, cont); - setframe_pc(base, pc); - setnilV(base-1-LJ_FR2); /* Incorrect, but rec_check_slots() won't run anymore. */ - L->base += 2 + LJ_FR2; - L->top += 2 + LJ_FR2; - - /* Ditto for the IR. */ - memmove(&J->base[1], &J->base[-1-LJ_FR2], sizeof(TRef)*nslot); -#if LJ_FR2 - J->base[2] = TREF_FRAME; - J->base[-1] = lj_ir_k64(J, IR_KNUM, u64ptr(contptr(cont))); - J->base[0] = lj_ir_k64(J, IR_KNUM, u64ptr(pc)) | TREF_CONT; -#else - J->base[0] = lj_ir_kptr(J, contptr(cont)) | TREF_CONT; -#endif - J->ktrace = tref_ref((J->base[-1-LJ_FR2] = lj_ir_ktrace(J))); - J->base += 2 + LJ_FR2; - J->baseslot += 2 + LJ_FR2; - J->framedepth++; - - lj_record_stop(J, LJ_TRLINK_STITCH, 0); - - /* Undo Lua stack changes. */ - memmove(&base[-1-LJ_FR2], &base[1], sizeof(TValue)*nslot); - setframe_pc(base-1, pc); - L->base -= 2 + LJ_FR2; - L->top -= 2 + LJ_FR2; -} - -/* Fallback handler for fast functions that are not recorded (yet). */ -static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd) -{ - if (J->cur.nins < (IRRef)J->param[JIT_P_minstitch] + REF_BASE) { - lj_trace_err_info(J, LJ_TRERR_TRACEUV); - } else { - /* Can only stitch from Lua call. */ - if (J->framedepth && frame_islua(J->L->base-1)) { - BCOp op = bc_op(*frame_pc(J->L->base-1)); - /* Stitched trace cannot start with *M op with variable # of args. */ - if (!(op == BC_CALLM || op == BC_CALLMT || - op == BC_RETM || op == BC_TSETM)) { - switch (J->fn->c.ffid) { - case FF_error: - case FF_debug_sethook: - case FF_jit_flush: - break; /* Don't stitch across special builtins. */ - default: - recff_stitch(J); /* Use trace stitching. */ - rd->nres = -1; - return; - } - } - } - /* Otherwise stop trace and return to interpreter. */ - lj_record_stop(J, LJ_TRLINK_RETURN, 0); - rd->nres = -1; - } -} - -/* Fallback handler for unsupported variants of fast functions. */ -#define recff_nyiu recff_nyi - -/* Must stop the trace for classic C functions with arbitrary side-effects. */ -#define recff_c recff_nyi - -/* Emit BUFHDR for the global temporary buffer. */ -static TRef recff_bufhdr(jit_State *J) -{ - return emitir(IRT(IR_BUFHDR, IRT_PGC), - lj_ir_kptr(J, &J2G(J)->tmpbuf), IRBUFHDR_RESET); -} - -/* Emit TMPREF. */ -static TRef recff_tmpref(jit_State *J, TRef tr, int mode) -{ - if (!LJ_DUALNUM && tref_isinteger(tr)) - tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - return emitir(IRT(IR_TMPREF, IRT_PGC), tr, mode); -} - -/* -- Base library fast functions ----------------------------------------- */ - -static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd) -{ - /* Arguments already specialized. The interpreter throws for nil/false. */ - rd->nres = J->maxslot; /* Pass through all arguments. */ -} - -static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd) -{ - /* Arguments already specialized. Result is a constant string. Neat, huh? */ - uint32_t t; - if (tvisnumber(&rd->argv[0])) - t = ~LJ_TNUMX; - else if (LJ_64 && !LJ_GC64 && tvislightud(&rd->argv[0])) - t = ~LJ_TLIGHTUD; - else - t = ~itype(&rd->argv[0]); - J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t])); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tr) { - RecordIndex ix; - ix.tab = tr; - copyTV(J->L, &ix.tabv, &rd->argv[0]); - if (lj_record_mm_lookup(J, &ix, MM_metatable)) - J->base[0] = ix.mobj; - else - J->base[0] = ix.mt; - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - TRef mt = J->base[1]; - if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) { - TRef fref, mtref; - RecordIndex ix; - ix.tab = tr; - copyTV(J->L, &ix.tabv, &rd->argv[0]); - lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */ - fref = emitir(IRT(IR_FREF, IRT_PGC), tr, IRFL_TAB_META); - mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt; - emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref); - if (!tref_isnil(mt)) - emitir(IRT(IR_TBAR, IRT_TAB), tr, 0); - J->base[0] = tr; - J->needsnap = 1; - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; ix.key = J->base[1]; - if (tref_istab(ix.tab) && ix.key) { - ix.val = 0; ix.idxchain = 0; - settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); - copyTV(J->L, &ix.keyv, &rd->argv[1]); - J->base[0] = lj_record_idx(J, &ix); - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2]; - if (tref_istab(ix.tab) && ix.key && ix.val) { - ix.idxchain = 0; - settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); - copyTV(J->L, &ix.keyv, &rd->argv[1]); - copyTV(J->L, &ix.valv, &rd->argv[2]); - lj_record_idx(J, &ix); - /* Pass through table at J->base[0] as result. */ - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd) -{ - TRef tra = J->base[0]; - TRef trb = J->base[1]; - if (tra && trb) { - int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]); - J->base[0] = diff ? TREF_FALSE : TREF_TRUE; - } /* else: Interpreter will throw. */ -} - -#if LJ_52 -static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tref_isstr(tr)) - J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN); - else if (tref_istab(tr)) - J->base[0] = emitir(IRTI(IR_ALEN), tr, TREF_NIL); - /* else: Interpreter will throw. */ - UNUSED(rd); -} -#endif - -/* Determine mode of select() call. */ -int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv) -{ - if (tref_isstr(tr) && *strVdata(tv) == '#') { /* select('#', ...) */ - if (strV(tv)->len == 1) { - emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv))); - } else { - TRef trptr = emitir(IRT(IR_STRREF, IRT_PGC), tr, lj_ir_kint(J, 0)); - TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY); - emitir(IRTGI(IR_EQ), trchar, lj_ir_kint(J, '#')); - } - return 0; - } else { /* select(n, ...) */ - int32_t start = argv2int(J, tv); - if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE); /* A bit misleading. */ - return start; - } -} - -static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tr) { - ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]); - if (start == 0) { /* select('#', ...) */ - J->base[0] = lj_ir_kint(J, J->maxslot - 1); - } else if (tref_isk(tr)) { /* select(k, ...) */ - ptrdiff_t n = (ptrdiff_t)J->maxslot; - if (start < 0) start += n; - else if (start > n) start = n; - if (start >= 1) { - ptrdiff_t i; - rd->nres = n - start; - for (i = 0; i < n - start; i++) - J->base[i] = J->base[start+i]; - } /* else: Interpreter will throw. */ - } else { - recff_nyiu(J, rd); - return; - } - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - TRef base = J->base[1]; - if (tr && !tref_isnil(base)) { - base = lj_opt_narrow_toint(J, base); - if (!tref_isk(base) || IR(tref_ref(base))->i != 10) { - recff_nyiu(J, rd); - return; - } - } - if (tref_isnumber_str(tr)) { - if (tref_isstr(tr)) { - TValue tmp; - if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) { - recff_nyiu(J, rd); /* Would need an inverted STRTO for this case. */ - return; - } - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - } -#if LJ_HASFFI - } else if (tref_iscdata(tr)) { - lj_crecord_tonumber(J, rd); - return; -#endif - } else { - tr = TREF_NIL; - } - J->base[0] = tr; - UNUSED(rd); -} - -static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - lj_record_tailcall(J, 0, 1); - UNUSED(L); UNUSED(dummy); - return NULL; -} - -static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm) -{ - RecordIndex ix; - ix.tab = J->base[0]; - copyTV(J->L, &ix.tabv, &rd->argv[0]); - if (lj_record_mm_lookup(J, &ix, mm)) { /* Has metamethod? */ - int errcode; - TValue argv0; - /* Temporarily insert metamethod below object. */ - J->base[1+LJ_FR2] = J->base[0]; - J->base[0] = ix.mobj; - copyTV(J->L, &argv0, &rd->argv[0]); - copyTV(J->L, &rd->argv[1+LJ_FR2], &rd->argv[0]); - copyTV(J->L, &rd->argv[0], &ix.mobjv); - /* Need to protect lj_record_tailcall because it may throw. */ - errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp); - /* Always undo Lua stack changes to avoid confusing the interpreter. */ - copyTV(J->L, &rd->argv[0], &argv0); - if (errcode) - lj_err_throw(J->L, errcode); /* Propagate errors. */ - rd->nres = -1; /* Pending call. */ - return 1; /* Tailcalled to metamethod. */ - } - return 0; -} - -static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tref_isstr(tr)) { - /* Ignore __tostring in the string base metatable. */ - /* Pass on result in J->base[0]. */ - } else if (tr && !recff_metacall(J, rd, MM_tostring)) { - if (tref_isnumber(tr)) { - J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, - tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT); - } else if (tref_ispri(tr)) { - J->base[0] = lj_ir_kstr(J, lj_strfmt_obj(J->L, &rd->argv[0])); - } else { - recff_nyiu(J, rd); - return; - } - } -} - -static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; - if (tref_istab(ix.tab)) { - if (!tvisnumber(&rd->argv[1])) /* No support for string coercion. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); - setintV(&ix.keyv, numberVint(&rd->argv[1])+1); - settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); - ix.val = 0; ix.idxchain = 0; - ix.key = lj_opt_narrow_toint(J, J->base[1]); - J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1)); - J->base[1] = lj_record_idx(J, &ix); - rd->nres = tref_isnil(J->base[1]) ? 0 : 2; - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_xpairs(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (!((LJ_52 || (LJ_HASFFI && tref_iscdata(tr))) && - recff_metacall(J, rd, MM_pairs + rd->data))) { - if (tref_istab(tr)) { - J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0])); - J->base[1] = tr; - J->base[2] = rd->data ? lj_ir_kint(J, 0) : TREF_NIL; - rd->nres = 3; - } /* else: Interpreter will throw. */ - } -} - -static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd) -{ - if (J->maxslot >= 1) { -#if LJ_FR2 - /* Shift function arguments up. */ - memmove(J->base + 1, J->base, sizeof(TRef) * J->maxslot); -#endif - lj_record_call(J, 0, J->maxslot - 1); - rd->nres = -1; /* Pending call. */ - J->needsnap = 1; /* Start catching on-trace errors. */ - } /* else: Interpreter will throw. */ -} - -static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - lj_record_call(J, 1, J->maxslot - 2); - UNUSED(L); UNUSED(dummy); - return NULL; -} - -static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) -{ - if (J->maxslot >= 2) { - TValue argv0, argv1; - TRef tmp; - int errcode; - /* Swap function and traceback. */ - tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp; - copyTV(J->L, &argv0, &rd->argv[0]); - copyTV(J->L, &argv1, &rd->argv[1]); - copyTV(J->L, &rd->argv[0], &argv1); - copyTV(J->L, &rd->argv[1], &argv0); -#if LJ_FR2 - /* Shift function arguments up. */ - memmove(J->base + 2, J->base + 1, sizeof(TRef) * (J->maxslot-1)); -#endif - /* Need to protect lj_record_call because it may throw. */ - errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp); - /* Always undo Lua stack swap to avoid confusing the interpreter. */ - copyTV(J->L, &rd->argv[0], &argv0); - copyTV(J->L, &rd->argv[1], &argv1); - if (errcode) - lj_err_throw(J->L, errcode); /* Propagate errors. */ - rd->nres = -1; /* Pending call. */ - J->needsnap = 1; /* Start catching on-trace errors. */ - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_getfenv(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - /* Only support getfenv(0) for now. */ - if (tref_isint(tr) && tref_isk(tr) && IR(tref_ref(tr))->i == 0) { - TRef trl = emitir(IRT(IR_LREF, IRT_THREAD), 0, 0); - J->base[0] = emitir(IRT(IR_FLOAD, IRT_TAB), trl, IRFL_THREAD_ENV); - return; - } - recff_nyiu(J, rd); -} - -static void LJ_FASTCALL recff_next(jit_State *J, RecordFFData *rd) -{ -#if LJ_BE - /* YAGNI: Disabled on big-endian due to issues with lj_vm_next, - ** IR_HIOP, RID_RETLO/RID_RETHI and ra_destpair. - */ - recff_nyi(J, rd); -#else - TRef tab = J->base[0]; - if (tref_istab(tab)) { - RecordIndex ix; - cTValue *keyv; - ix.tab = tab; - if (tref_isnil(J->base[1])) { /* Shortcut for start of traversal. */ - ix.key = lj_ir_kint(J, 0); - keyv = niltvg(J2G(J)); - } else { - TRef tmp = recff_tmpref(J, J->base[1], IRTMPREF_IN1); - ix.key = lj_ir_call(J, IRCALL_lj_tab_keyindex, tab, tmp); - keyv = &rd->argv[1]; - } - copyTV(J->L, &ix.tabv, &rd->argv[0]); - ix.keyv.u32.lo = lj_tab_keyindex(tabV(&ix.tabv), keyv); - /* Omit the value, if not used by the caller. */ - ix.idxchain = (J->framedepth && frame_islua(J->L->base-1) && - bc_b(frame_pc(J->L->base-1)[-1])-1 < 2); - ix.mobj = 0; /* We don't need the next index. */ - rd->nres = lj_record_next(J, &ix); - J->base[0] = ix.key; - J->base[1] = ix.val; - } /* else: Interpreter will throw. */ -#endif -} - -/* -- Math library fast functions ----------------------------------------- */ - -static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_ksimd(J, LJ_KSIMD_ABS)); - UNUSED(rd); -} - -/* Record rounding functions math.floor and math.ceil. */ -static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (!tref_isinteger(tr)) { /* Pass through integers unmodified. */ - tr = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, tr), rd->data); - /* Result is integral (or NaN/Inf), but may not fit an int32_t. */ - if (LJ_DUALNUM) { /* Try to narrow using a guarded conversion to int. */ - lua_Number n = lj_vm_foldfpm(numberVnum(&rd->argv[0]), rd->data); - if (n == (lua_Number)lj_num2int(n)) - tr = emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK); - } - J->base[0] = tr; - } -} - -/* Record unary math.* functions, mapped to IR_FPMATH opcode. */ -static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd) -{ - J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data); -} - -/* Record math.log. */ -static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - if (J->base[1]) { -#ifdef LUAJIT_NO_LOG2 - uint32_t fpm = IRFPM_LOG; -#else - uint32_t fpm = IRFPM_LOG2; -#endif - TRef trb = lj_ir_tonum(J, J->base[1]); - tr = emitir(IRTN(IR_FPMATH), tr, fpm); - trb = emitir(IRTN(IR_FPMATH), trb, fpm); - trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb); - tr = emitir(IRTN(IR_MUL), tr, trb); - } else { - tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG); - } - J->base[0] = tr; - UNUSED(rd); -} - -/* Record math.atan2. */ -static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - TRef tr2 = lj_ir_tonum(J, J->base[1]); - J->base[0] = lj_ir_call(J, IRCALL_atan2, tr, tr2); - UNUSED(rd); -} - -/* Record math.ldexp. */ -static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); -#if LJ_TARGET_X86ORX64 - TRef tr2 = lj_ir_tonum(J, J->base[1]); -#else - TRef tr2 = lj_opt_narrow_toint(J, J->base[1]); -#endif - J->base[0] = emitir(IRTN(IR_LDEXP), tr, tr2); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_math_call(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonum(J, J->base[0]); - J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data); -} - -static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd) -{ - J->base[0] = lj_opt_narrow_arith(J, J->base[0], J->base[1], - &rd->argv[0], &rd->argv[1], IR_POW); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd) -{ - TRef tr = lj_ir_tonumber(J, J->base[0]); - uint32_t op = rd->data; - BCReg i; - for (i = 1; J->base[i] != 0; i++) { - TRef tr2 = lj_ir_tonumber(J, J->base[i]); - IRType t = IRT_INT; - if (!(tref_isinteger(tr) && tref_isinteger(tr2))) { - if (tref_isinteger(tr)) tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - if (tref_isinteger(tr2)) tr2 = emitir(IRTN(IR_CONV), tr2, IRCONV_NUM_INT); - t = IRT_NUM; - } - tr = emitir(IRT(op, t), tr, tr2); - } - J->base[0] = tr; -} - -static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd) -{ - GCudata *ud = udataV(&J->fn->c.upvalue[0]); - TRef tr, one; - lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */ - tr = lj_ir_call(J, IRCALL_lj_prng_u64d, lj_ir_kptr(J, uddata(ud))); - one = lj_ir_knum_one(J); - tr = emitir(IRTN(IR_SUB), tr, one); - if (J->base[0]) { - TRef tr1 = lj_ir_tonum(J, J->base[0]); - if (J->base[1]) { /* d = floor(d*(r2-r1+1.0)) + r1 */ - TRef tr2 = lj_ir_tonum(J, J->base[1]); - tr2 = emitir(IRTN(IR_SUB), tr2, tr1); - tr2 = emitir(IRTN(IR_ADD), tr2, one); - tr = emitir(IRTN(IR_MUL), tr, tr2); - tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); - tr = emitir(IRTN(IR_ADD), tr, tr1); - } else { /* d = floor(d*r1) + 1.0 */ - tr = emitir(IRTN(IR_MUL), tr, tr1); - tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); - tr = emitir(IRTN(IR_ADD), tr, one); - } - } - J->base[0] = tr; - UNUSED(rd); -} - -/* -- Bit library fast functions ------------------------------------------ */ - -/* Record bit.tobit. */ -static void LJ_FASTCALL recff_bit_tobit(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; -#if LJ_HASFFI - if (tref_iscdata(tr)) { recff_bit64_tobit(J, rd); return; } -#endif - J->base[0] = lj_opt_narrow_tobit(J, tr); - UNUSED(rd); -} - -/* Record unary bit.bnot, bit.bswap. */ -static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd) -{ -#if LJ_HASFFI - if (recff_bit64_unary(J, rd)) - return; -#endif - J->base[0] = emitir(IRTI(rd->data), lj_opt_narrow_tobit(J, J->base[0]), 0); -} - -/* Record N-ary bit.band, bit.bor, bit.bxor. */ -static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd) -{ -#if LJ_HASFFI - if (recff_bit64_nary(J, rd)) - return; -#endif - { - TRef tr = lj_opt_narrow_tobit(J, J->base[0]); - uint32_t ot = IRTI(rd->data); - BCReg i; - for (i = 1; J->base[i] != 0; i++) - tr = emitir(ot, tr, lj_opt_narrow_tobit(J, J->base[i])); - J->base[0] = tr; - } -} - -/* Record bit shifts. */ -static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd) -{ -#if LJ_HASFFI - if (recff_bit64_shift(J, rd)) - return; -#endif - { - TRef tr = lj_opt_narrow_tobit(J, J->base[0]); - TRef tsh = lj_opt_narrow_tobit(J, J->base[1]); - IROp op = (IROp)rd->data; - if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && - !tref_isk(tsh)) - tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31)); -#ifdef LJ_TARGET_UNIFYROT - if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) { - op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR; - tsh = emitir(IRTI(IR_NEG), tsh, tsh); - } -#endif - J->base[0] = emitir(IRTI(op), tr, tsh); - } -} - -static void LJ_FASTCALL recff_bit_tohex(jit_State *J, RecordFFData *rd) -{ -#if LJ_HASFFI - TRef hdr = recff_bufhdr(J); - TRef tr = recff_bit64_tohex(J, rd, hdr); - J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); -#else - recff_nyiu(J, rd); /* Don't bother working around this NYI. */ -#endif -} - -/* -- String library fast functions --------------------------------------- */ - -/* Specialize to relative starting position for string. */ -static TRef recff_string_start(jit_State *J, GCstr *s, int32_t *st, TRef tr, - TRef trlen, TRef tr0) -{ - int32_t start = *st; - if (start < 0) { - emitir(IRTGI(IR_LT), tr, tr0); - tr = emitir(IRTI(IR_ADD), trlen, tr); - start = start + (int32_t)s->len; - emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), tr, tr0); - if (start < 0) { - tr = tr0; - start = 0; - } - } else if (start == 0) { - emitir(IRTGI(IR_EQ), tr, tr0); - tr = tr0; - } else { - tr = emitir(IRTI(IR_ADD), tr, lj_ir_kint(J, -1)); - emitir(IRTGI(IR_GE), tr, tr0); - start--; - } - *st = start; - return tr; -} - -/* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */ -static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) -{ - TRef trstr = lj_ir_tostr(J, J->base[0]); - TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN); - TRef tr0 = lj_ir_kint(J, 0); - TRef trstart, trend; - GCstr *str = argv2str(J, &rd->argv[0]); - int32_t start, end; - if (rd->data) { /* string.sub(str, start [,end]) */ - start = argv2int(J, &rd->argv[1]); - trstart = lj_opt_narrow_toint(J, J->base[1]); - trend = J->base[2]; - if (tref_isnil(trend)) { - trend = lj_ir_kint(J, -1); - end = -1; - } else { - trend = lj_opt_narrow_toint(J, trend); - end = argv2int(J, &rd->argv[2]); - } - } else { /* string.byte(str, [,start [,end]]) */ - if (tref_isnil(J->base[1])) { - start = 1; - trstart = lj_ir_kint(J, 1); - } else { - start = argv2int(J, &rd->argv[1]); - trstart = lj_opt_narrow_toint(J, J->base[1]); - } - if (J->base[1] && !tref_isnil(J->base[2])) { - trend = lj_opt_narrow_toint(J, J->base[2]); - end = argv2int(J, &rd->argv[2]); - } else { - trend = trstart; - end = start; - } - } - if (end < 0) { - emitir(IRTGI(IR_LT), trend, tr0); - trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend), - lj_ir_kint(J, 1)); - end = end+(int32_t)str->len+1; - } else if ((MSize)end <= str->len) { - emitir(IRTGI(IR_ULE), trend, trlen); - } else { - emitir(IRTGI(IR_UGT), trend, trlen); - end = (int32_t)str->len; - trend = trlen; - } - trstart = recff_string_start(J, str, &start, trstart, trlen, tr0); - if (rd->data) { /* Return string.sub result. */ - if (end - start >= 0) { - /* Also handle empty range here, to avoid extra traces. */ - TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart); - emitir(IRTGI(IR_GE), trslen, tr0); - trptr = emitir(IRT(IR_STRREF, IRT_PGC), trstr, trstart); - J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen); - } else { /* Range underflow: return empty string. */ - emitir(IRTGI(IR_LT), trend, trstart); - J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty); - } - } else { /* Return string.byte result(s). */ - ptrdiff_t i, len = end - start; - if (len > 0) { - TRef trslen = emitir(IRTI(IR_SUB), trend, trstart); - emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len)); - if (J->baseslot + len > LJ_MAX_JSLOTS) - lj_trace_err_info(J, LJ_TRERR_STACKOV); - rd->nres = len; - for (i = 0; i < len; i++) { - TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i)); - tmp = emitir(IRT(IR_STRREF, IRT_PGC), trstr, tmp); - J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY); - } - } else { /* Empty range or range underflow: return no results. */ - emitir(IRTGI(IR_LE), trend, trstart); - rd->nres = 0; - } - } -} - -static void LJ_FASTCALL recff_string_char(jit_State *J, RecordFFData *rd) -{ - TRef k255 = lj_ir_kint(J, 255); - BCReg i; - for (i = 0; J->base[i] != 0; i++) { /* Convert char values to strings. */ - TRef tr = lj_opt_narrow_toint(J, J->base[i]); - emitir(IRTGI(IR_ULE), tr, k255); - J->base[i] = emitir(IRT(IR_TOSTR, IRT_STR), tr, IRTOSTR_CHAR); - } - if (i > 1) { /* Concatenate the strings, if there's more than one. */ - TRef hdr = recff_bufhdr(J), tr = hdr; - for (i = 0; J->base[i] != 0; i++) - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, J->base[i]); - J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); - } else if (i == 0) { - J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty); - } - UNUSED(rd); -} - -static void LJ_FASTCALL recff_string_rep(jit_State *J, RecordFFData *rd) -{ - TRef str = lj_ir_tostr(J, J->base[0]); - TRef rep = lj_opt_narrow_toint(J, J->base[1]); - TRef hdr, tr, str2 = 0; - if (!tref_isnil(J->base[2])) { - TRef sep = lj_ir_tostr(J, J->base[2]); - int32_t vrep = argv2int(J, &rd->argv[1]); - emitir(IRTGI(vrep > 1 ? IR_GT : IR_LE), rep, lj_ir_kint(J, 1)); - if (vrep > 1) { - TRef hdr2 = recff_bufhdr(J); - TRef tr2 = emitir(IRTG(IR_BUFPUT, IRT_PGC), hdr2, sep); - tr2 = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr2, str); - str2 = emitir(IRTG(IR_BUFSTR, IRT_STR), tr2, hdr2); - } - } - tr = hdr = recff_bufhdr(J); - if (str2) { - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, str); - str = str2; - rep = emitir(IRTI(IR_ADD), rep, lj_ir_kint(J, -1)); - } - tr = lj_ir_call(J, IRCALL_lj_buf_putstr_rep, tr, str, rep); - J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); -} - -static void LJ_FASTCALL recff_string_op(jit_State *J, RecordFFData *rd) -{ - TRef str = lj_ir_tostr(J, J->base[0]); - TRef hdr = recff_bufhdr(J); - TRef tr = lj_ir_call(J, rd->data, hdr, str); - J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); -} - -static void LJ_FASTCALL recff_string_find(jit_State *J, RecordFFData *rd) -{ - TRef trstr = lj_ir_tostr(J, J->base[0]); - TRef trpat = lj_ir_tostr(J, J->base[1]); - TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN); - TRef tr0 = lj_ir_kint(J, 0); - TRef trstart; - GCstr *str = argv2str(J, &rd->argv[0]); - GCstr *pat = argv2str(J, &rd->argv[1]); - int32_t start; - J->needsnap = 1; - if (tref_isnil(J->base[2])) { - trstart = lj_ir_kint(J, 1); - start = 1; - } else { - trstart = lj_opt_narrow_toint(J, J->base[2]); - start = argv2int(J, &rd->argv[2]); - } - trstart = recff_string_start(J, str, &start, trstart, trlen, tr0); - if ((MSize)start <= str->len) { - emitir(IRTGI(IR_ULE), trstart, trlen); - } else { - emitir(IRTGI(IR_UGT), trstart, trlen); -#if LJ_52 - J->base[0] = TREF_NIL; - return; -#else - trstart = trlen; - start = str->len; -#endif - } - /* Fixed arg or no pattern matching chars? (Specialized to pattern string.) */ - if ((J->base[2] && tref_istruecond(J->base[3])) || - (emitir(IRTG(IR_EQ, IRT_STR), trpat, lj_ir_kstr(J, pat)), - !lj_str_haspattern(pat))) { /* Search for fixed string. */ - TRef trsptr = emitir(IRT(IR_STRREF, IRT_PGC), trstr, trstart); - TRef trpptr = emitir(IRT(IR_STRREF, IRT_PGC), trpat, tr0); - TRef trslen = emitir(IRTI(IR_SUB), trlen, trstart); - TRef trplen = emitir(IRTI(IR_FLOAD), trpat, IRFL_STR_LEN); - TRef tr = lj_ir_call(J, IRCALL_lj_str_find, trsptr, trpptr, trslen, trplen); - TRef trp0 = lj_ir_kkptr(J, NULL); - if (lj_str_find(strdata(str)+(MSize)start, strdata(pat), - str->len-(MSize)start, pat->len)) { - TRef pos; - emitir(IRTG(IR_NE, IRT_PGC), tr, trp0); - /* Recompute offset. trsptr may not point into trstr after folding. */ - pos = emitir(IRTI(IR_ADD), emitir(IRTI(IR_SUB), tr, trsptr), trstart); - J->base[0] = emitir(IRTI(IR_ADD), pos, lj_ir_kint(J, 1)); - J->base[1] = emitir(IRTI(IR_ADD), pos, trplen); - rd->nres = 2; - } else { - emitir(IRTG(IR_EQ, IRT_PGC), tr, trp0); - J->base[0] = TREF_NIL; - } - } else { /* Search for pattern. */ - recff_nyiu(J, rd); - return; - } -} - -static void recff_format(jit_State *J, RecordFFData *rd, TRef hdr, int sbufx) -{ - ptrdiff_t arg = sbufx; - TRef tr = hdr, trfmt = lj_ir_tostr(J, J->base[arg]); - GCstr *fmt = argv2str(J, &rd->argv[arg]); - FormatState fs; - SFormat sf; - /* Specialize to the format string. */ - emitir(IRTG(IR_EQ, IRT_STR), trfmt, lj_ir_kstr(J, fmt)); - lj_strfmt_init(&fs, strdata(fmt), fmt->len); - while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) { /* Parse format. */ - TRef tra = sf == STRFMT_LIT ? 0 : J->base[++arg]; - TRef trsf = lj_ir_kint(J, (int32_t)sf); - IRCallID id; - switch (STRFMT_TYPE(sf)) { - case STRFMT_LIT: - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, - lj_ir_kstr(J, lj_str_new(J->L, fs.str, fs.len))); - break; - case STRFMT_INT: - id = IRCALL_lj_strfmt_putfnum_int; - handle_int: - if (!tref_isinteger(tra)) { -#if LJ_HASFFI - if (tref_iscdata(tra)) { - tra = lj_crecord_loadiu64(J, tra, &rd->argv[arg]); - tr = lj_ir_call(J, IRCALL_lj_strfmt_putfxint, tr, trsf, tra); - break; - } -#endif - goto handle_num; - } - if (sf == STRFMT_INT) { /* Shortcut for plain %d. */ - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, - emitir(IRT(IR_TOSTR, IRT_STR), tra, IRTOSTR_INT)); - } else { -#if LJ_HASFFI - tra = emitir(IRT(IR_CONV, IRT_U64), tra, - (IRT_INT|(IRT_U64<<5)|IRCONV_SEXT)); - tr = lj_ir_call(J, IRCALL_lj_strfmt_putfxint, tr, trsf, tra); - lj_needsplit(J); -#else - recff_nyiu(J, rd); /* Don't bother working around this NYI. */ - return; -#endif - } - break; - case STRFMT_UINT: - id = IRCALL_lj_strfmt_putfnum_uint; - goto handle_int; - case STRFMT_NUM: - id = IRCALL_lj_strfmt_putfnum; - handle_num: - tra = lj_ir_tonum(J, tra); - tr = lj_ir_call(J, id, tr, trsf, tra); - if (LJ_SOFTFP32) lj_needsplit(J); - break; - case STRFMT_STR: - if (!tref_isstr(tra)) { - recff_nyiu(J, rd); /* NYI: __tostring and non-string types for %s. */ - /* NYI: also buffers. */ - return; - } - if (sf == STRFMT_STR) /* Shortcut for plain %s. */ - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, tra); - else if ((sf & STRFMT_T_QUOTED)) - tr = lj_ir_call(J, IRCALL_lj_strfmt_putquoted, tr, tra); - else - tr = lj_ir_call(J, IRCALL_lj_strfmt_putfstr, tr, trsf, tra); - break; - case STRFMT_CHAR: - tra = lj_opt_narrow_toint(J, tra); - if (sf == STRFMT_CHAR) /* Shortcut for plain %c. */ - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, - emitir(IRT(IR_TOSTR, IRT_STR), tra, IRTOSTR_CHAR)); - else - tr = lj_ir_call(J, IRCALL_lj_strfmt_putfchar, tr, trsf, tra); - break; - case STRFMT_PTR: /* NYI */ - case STRFMT_ERR: - default: - recff_nyiu(J, rd); - return; - } - } - if (sbufx) { - emitir(IRT(IR_USE, IRT_NIL), tr, 0); - } else { - J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); - } -} - -static void LJ_FASTCALL recff_string_format(jit_State *J, RecordFFData *rd) -{ - recff_format(J, rd, recff_bufhdr(J), 0); -} - -/* -- Buffer library fast functions --------------------------------------- */ - -#if LJ_HASBUFFER - -static LJ_AINLINE TRef recff_sbufx_get_L(jit_State *J, TRef ud) -{ - return emitir(IRT(IR_FLOAD, IRT_PGC), ud, IRFL_SBUF_L); -} - -static LJ_AINLINE void recff_sbufx_set_L(jit_State *J, TRef ud, TRef val) -{ - TRef fref = emitir(IRT(IR_FREF, IRT_PGC), ud, IRFL_SBUF_L); - emitir(IRT(IR_FSTORE, IRT_PGC), fref, val); -} - -static LJ_AINLINE TRef recff_sbufx_get_ptr(jit_State *J, TRef ud, IRFieldID fl) -{ - return emitir(IRT(IR_FLOAD, IRT_PTR), ud, fl); -} - -static LJ_AINLINE void recff_sbufx_set_ptr(jit_State *J, TRef ud, IRFieldID fl, TRef val) -{ - TRef fref = emitir(IRT(IR_FREF, IRT_PTR), ud, fl); - emitir(IRT(IR_FSTORE, IRT_PTR), fref, val); -} - -static LJ_AINLINE TRef recff_sbufx_len(jit_State *J, TRef trr, TRef trw) -{ - TRef len = emitir(IRT(IR_SUB, IRT_INTP), trw, trr); - if (LJ_64) - len = emitir(IRTI(IR_CONV), len, (IRT_INT<<5)|IRT_INTP|IRCONV_NONE); - return len; -} - -/* Emit typecheck for string buffer. */ -static TRef recff_sbufx_check(jit_State *J, RecordFFData *rd, ptrdiff_t arg) -{ - TRef trtype, ud = J->base[arg]; - if (!tvisbuf(&rd->argv[arg])) lj_trace_err(J, LJ_TRERR_BADTYPE); - trtype = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), trtype, lj_ir_kint(J, UDTYPE_BUFFER)); - J->needsnap = 1; - return ud; -} - -/* Emit BUFHDR for write to extended string buffer. */ -static TRef recff_sbufx_write(jit_State *J, TRef ud) -{ - TRef trbuf = emitir(IRT(IR_ADD, IRT_PGC), ud, lj_ir_kint(J, sizeof(GCudata))); - return emitir(IRT(IR_BUFHDR, IRT_PGC), trbuf, IRBUFHDR_WRITE); -} - -/* Check for integer in range for the buffer API. */ -static TRef recff_sbufx_checkint(jit_State *J, RecordFFData *rd, ptrdiff_t arg) -{ - TRef tr = J->base[arg]; - TRef trlim = lj_ir_kint(J, LJ_MAX_BUF); - if (tref_isinteger(tr)) { - emitir(IRTGI(IR_ULE), tr, trlim); - } else if (tref_isnum(tr)) { - tr = emitir(IRTI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_ANY); - emitir(IRTGI(IR_ULE), tr, trlim); -#if LJ_HASFFI - } else if (tref_iscdata(tr)) { - tr = lj_crecord_loadiu64(J, tr, &rd->argv[arg]); - emitir(IRTG(IR_ULE, IRT_U64), tr, lj_ir_kint64(J, LJ_MAX_BUF)); - tr = emitir(IRTI(IR_CONV), tr, (IRT_INT<<5)|IRT_I64|IRCONV_NONE); -#else - UNUSED(rd); -#endif - } else { - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - return tr; -} - -static void LJ_FASTCALL recff_buffer_method_reset(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - SBufExt *sbx = bufV(&rd->argv[0]); - int iscow = (int)sbufiscow(sbx); - TRef trl = recff_sbufx_get_L(J, ud); - TRef trcow = emitir(IRT(IR_BAND, IRT_IGC), trl, lj_ir_kint(J, SBUF_FLAG_COW)); - TRef zero = lj_ir_kint(J, 0); - emitir(IRTG(iscow ? IR_NE : IR_EQ, IRT_IGC), trcow, zero); - if (iscow) { - trl = emitir(IRT(IR_BXOR, IRT_IGC), trl, - LJ_GC64 ? lj_ir_kint64(J, SBUF_FLAG_COW) : - lj_ir_kint(J, SBUF_FLAG_COW)); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, zero); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_E, zero); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_B, zero); - recff_sbufx_set_L(J, ud, trl); - emitir(IRT(IR_FSTORE, IRT_PGC), - emitir(IRT(IR_FREF, IRT_PGC), ud, IRFL_SBUF_REF), zero); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, zero); - } else { - TRef trb = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_B); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, trb); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trb); - } -} - -static void LJ_FASTCALL recff_buffer_method_skip(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); - TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); - TRef len = recff_sbufx_len(J, trr, trw); - TRef trn = recff_sbufx_checkint(J, rd, 1); - len = emitir(IRTI(IR_MIN), len, trn); - trr = emitir(IRT(IR_ADD, IRT_PTR), trr, len); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr); -} - -static void LJ_FASTCALL recff_buffer_method_set(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - TRef tr = J->base[1]; - if (tref_isstr(tr)) { - TRef trp = emitir(IRT(IR_STRREF, IRT_PGC), tr, lj_ir_kint(J, 0)); - TRef len = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN); - lj_ir_call(J, IRCALL_lj_bufx_set, trbuf, trp, len, tr); -#if LJ_HASFFI - } else if (tref_iscdata(tr)) { - TRef trp = lj_crecord_topcvoid(J, tr, &rd->argv[1]); - TRef len = recff_sbufx_checkint(J, rd, 2); - lj_ir_call(J, IRCALL_lj_bufx_set, trbuf, trp, len, tr); -#endif - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_buffer_method_put(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - TRef tr; - ptrdiff_t arg; - if (!J->base[1]) return; - for (arg = 1; (tr = J->base[arg]); arg++) { - if (tref_isudata(tr)) { - TRef ud2 = recff_sbufx_check(J, rd, arg); - emitir(IRTG(IR_NE, IRT_PGC), ud, ud2); - } - } - for (arg = 1; (tr = J->base[arg]); arg++) { - if (tref_isstr(tr)) { - trbuf = emitir(IRTG(IR_BUFPUT, IRT_PGC), trbuf, tr); - } else if (tref_isnumber(tr)) { - trbuf = emitir(IRTG(IR_BUFPUT, IRT_PGC), trbuf, - emitir(IRT(IR_TOSTR, IRT_STR), tr, - tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT)); - } else if (tref_isudata(tr)) { - TRef trr = recff_sbufx_get_ptr(J, tr, IRFL_SBUF_R); - TRef trw = recff_sbufx_get_ptr(J, tr, IRFL_SBUF_W); - TRef len = recff_sbufx_len(J, trr, trw); - trbuf = lj_ir_call(J, IRCALL_lj_buf_putmem, trbuf, trr, len); - } else { - recff_nyiu(J, rd); - } - } - emitir(IRT(IR_USE, IRT_NIL), trbuf, 0); -} - -static void LJ_FASTCALL recff_buffer_method_putf(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - recff_format(J, rd, trbuf, 1); -} - -static void LJ_FASTCALL recff_buffer_method_get(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); - TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); - TRef tr; - ptrdiff_t arg; - if (!J->base[1]) { J->base[1] = TREF_NIL; J->base[2] = 0; } - for (arg = 0; (tr = J->base[arg+1]); arg++) { - if (!tref_isnil(tr)) { - J->base[arg+1] = recff_sbufx_checkint(J, rd, arg+1); - } - } - for (arg = 0; (tr = J->base[arg+1]); arg++) { - TRef len = recff_sbufx_len(J, trr, trw); - if (tref_isnil(tr)) { - J->base[arg] = emitir(IRT(IR_XSNEW, IRT_STR), trr, len); - trr = trw; - } else { - TRef tru; - len = emitir(IRTI(IR_MIN), len, tr); - tru = emitir(IRT(IR_ADD, IRT_PTR), trr, len); - J->base[arg] = emitir(IRT(IR_XSNEW, IRT_STR), trr, len); - trr = tru; /* Doing the ADD before the SNEW generates better code. */ - } - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr); - } - rd->nres = arg; -} - -static void LJ_FASTCALL recff_buffer_method___tostring(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); - TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); - J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), trr, recff_sbufx_len(J, trr, trw)); -} - -static void LJ_FASTCALL recff_buffer_method___len(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); - TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); - J->base[0] = recff_sbufx_len(J, trr, trw); -} - -#if LJ_HASFFI -static void LJ_FASTCALL recff_buffer_method_putcdata(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - TRef tr = lj_crecord_topcvoid(J, J->base[1], &rd->argv[1]); - TRef len = recff_sbufx_checkint(J, rd, 2); - trbuf = lj_ir_call(J, IRCALL_lj_buf_putmem, trbuf, tr, len); - emitir(IRT(IR_USE, IRT_NIL), trbuf, 0); -} - -static void LJ_FASTCALL recff_buffer_method_reserve(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - TRef trsz = recff_sbufx_checkint(J, rd, 1); - J->base[1] = lj_ir_call(J, IRCALL_lj_bufx_more, trbuf, trsz); - J->base[0] = lj_crecord_topuint8(J, recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W)); - rd->nres = 2; -} - -static void LJ_FASTCALL recff_buffer_method_commit(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef len = recff_sbufx_checkint(J, rd, 1); - TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); - TRef tre = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_E); - TRef left = emitir(IRT(IR_SUB, IRT_INTP), tre, trw); - if (LJ_64) - left = emitir(IRTI(IR_CONV), left, (IRT_INT<<5)|IRT_INTP|IRCONV_NONE); - emitir(IRTGI(IR_ULE), len, left); - trw = emitir(IRT(IR_ADD, IRT_PTR), trw, len); - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, trw); -} - -static void LJ_FASTCALL recff_buffer_method_ref(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); - TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); - J->base[0] = lj_crecord_topuint8(J, trr); - J->base[1] = recff_sbufx_len(J, trr, trw); - rd->nres = 2; -} -#endif - -static void LJ_FASTCALL recff_buffer_method_encode(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - TRef tmp = recff_tmpref(J, J->base[1], IRTMPREF_IN1); - lj_ir_call(J, IRCALL_lj_serialize_put, trbuf, tmp); - /* No IR_USE needed, since the call is a store. */ -} - -static void LJ_FASTCALL recff_buffer_method_decode(jit_State *J, RecordFFData *rd) -{ - TRef ud = recff_sbufx_check(J, rd, 0); - TRef trbuf = recff_sbufx_write(J, ud); - TRef tmp = recff_tmpref(J, TREF_NIL, IRTMPREF_OUT1); - TRef trr = lj_ir_call(J, IRCALL_lj_serialize_get, trbuf, tmp); - IRType t = (IRType)lj_serialize_peektype(bufV(&rd->argv[0])); - /* No IR_USE needed, since the call is a store. */ - J->base[0] = lj_record_vload(J, tmp, 0, t); - /* The sbx->r store must be after the VLOAD type check, in case it fails. */ - recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr); -} - -static void LJ_FASTCALL recff_buffer_encode(jit_State *J, RecordFFData *rd) -{ - TRef tmp = recff_tmpref(J, J->base[0], IRTMPREF_IN1); - J->base[0] = lj_ir_call(J, IRCALL_lj_serialize_encode, tmp); - /* IR_USE needed for IR_CALLA, because the encoder may throw non-OOM. */ - emitir(IRT(IR_USE, IRT_NIL), J->base[0], 0); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_buffer_decode(jit_State *J, RecordFFData *rd) -{ - if (tvisstr(&rd->argv[0])) { - GCstr *str = strV(&rd->argv[0]); - SBufExt sbx; - IRType t; - TRef tmp = recff_tmpref(J, TREF_NIL, IRTMPREF_OUT1); - TRef tr = lj_ir_call(J, IRCALL_lj_serialize_decode, tmp, J->base[0]); - /* IR_USE needed for IR_CALLA, because the decoder may throw non-OOM. - ** That's why IRCALL_lj_serialize_decode needs a fake INT result. - */ - emitir(IRT(IR_USE, IRT_NIL), tr, 0); - memset(&sbx, 0, sizeof(SBufExt)); - lj_bufx_set_cow(J->L, &sbx, strdata(str), str->len); - t = (IRType)lj_serialize_peektype(&sbx); - J->base[0] = lj_record_vload(J, tmp, 0, t); - } /* else: Interpreter will throw. */ -} - -#endif - -/* -- Table library fast functions ---------------------------------------- */ - -static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) -{ - RecordIndex ix; - ix.tab = J->base[0]; - ix.val = J->base[1]; - rd->nres = 0; - if (tref_istab(ix.tab) && ix.val) { - if (!J->base[2]) { /* Simple push: t[#t+1] = v */ - TRef trlen = emitir(IRTI(IR_ALEN), ix.tab, TREF_NIL); - GCtab *t = tabV(&rd->argv[0]); - ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); - settabV(J->L, &ix.tabv, t); - setintV(&ix.keyv, lj_tab_len(t) + 1); - ix.idxchain = 0; - lj_record_idx(J, &ix); /* Set new value. */ - } else { /* Complex case: insert in the middle. */ - recff_nyiu(J, rd); - return; - } - } /* else: Interpreter will throw. */ -} - -static void LJ_FASTCALL recff_table_concat(jit_State *J, RecordFFData *rd) -{ - TRef tab = J->base[0]; - if (tref_istab(tab)) { - TRef sep = !tref_isnil(J->base[1]) ? - lj_ir_tostr(J, J->base[1]) : lj_ir_knull(J, IRT_STR); - TRef tri = (J->base[1] && !tref_isnil(J->base[2])) ? - lj_opt_narrow_toint(J, J->base[2]) : lj_ir_kint(J, 1); - TRef tre = (J->base[1] && J->base[2] && !tref_isnil(J->base[3])) ? - lj_opt_narrow_toint(J, J->base[3]) : - emitir(IRTI(IR_ALEN), tab, TREF_NIL); - TRef hdr = recff_bufhdr(J); - TRef tr = lj_ir_call(J, IRCALL_lj_buf_puttab, hdr, tab, sep, tri, tre); - emitir(IRTG(IR_NE, IRT_PTR), tr, lj_ir_kptr(J, NULL)); - J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); - } /* else: Interpreter will throw. */ - UNUSED(rd); -} - -static void LJ_FASTCALL recff_table_new(jit_State *J, RecordFFData *rd) -{ - TRef tra = lj_opt_narrow_toint(J, J->base[0]); - TRef trh = lj_opt_narrow_toint(J, J->base[1]); - J->base[0] = lj_ir_call(J, IRCALL_lj_tab_new_ah, tra, trh); - UNUSED(rd); -} - -static void LJ_FASTCALL recff_table_clear(jit_State *J, RecordFFData *rd) -{ - TRef tr = J->base[0]; - if (tref_istab(tr)) { - rd->nres = 0; - lj_ir_call(J, IRCALL_lj_tab_clear, tr); - J->needsnap = 1; - } /* else: Interpreter will throw. */ -} - -/* -- I/O library fast functions ------------------------------------------ */ - -/* Get FILE* for I/O function. Any I/O error aborts recording, so there's -** no need to encode the alternate cases for any of the guards. -*/ -static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id) -{ - TRef tr, ud, fp; - if (id) { /* io.func() */ - ud = lj_ir_ggfload(J, IRT_UDATA, GG_OFS(g.gcroot[id])); - } else { /* fp:method() */ - ud = J->base[0]; - if (!tref_isudata(ud)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); - } - *udp = ud; - fp = emitir(IRT(IR_FLOAD, IRT_PTR), ud, IRFL_UDATA_FILE); - emitir(IRTG(IR_NE, IRT_PTR), fp, lj_ir_knull(J, IRT_PTR)); - return fp; -} - -static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd) -{ - TRef ud, fp = recff_io_fp(J, &ud, rd->data); - TRef zero = lj_ir_kint(J, 0); - TRef one = lj_ir_kint(J, 1); - ptrdiff_t i = rd->data == 0 ? 1 : 0; - for (; J->base[i]; i++) { - TRef str = lj_ir_tostr(J, J->base[i]); - TRef buf = emitir(IRT(IR_STRREF, IRT_PGC), str, zero); - TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN); - if (tref_isk(len) && IR(tref_ref(len))->i == 1) { - IRIns *irs = IR(tref_ref(str)); - TRef tr = (irs->o == IR_TOSTR && irs->op2 == IRTOSTR_CHAR) ? - irs->op1 : - emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY); - tr = lj_ir_call(J, IRCALL_fputc, tr, fp); - if (results_wanted(J) != 0) /* Check result only if not ignored. */ - emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1)); - } else { - TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp); - if (results_wanted(J) != 0) /* Check result only if not ignored. */ - emitir(IRTGI(IR_EQ), tr, len); - } - } - J->base[0] = LJ_52 ? ud : TREF_TRUE; -} - -static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd) -{ - TRef ud, fp = recff_io_fp(J, &ud, rd->data); - TRef tr = lj_ir_call(J, IRCALL_fflush, fp); - if (results_wanted(J) != 0) /* Check result only if not ignored. */ - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); - J->base[0] = TREF_TRUE; -} - -/* -- Debug library fast functions ---------------------------------------- */ - -static void LJ_FASTCALL recff_debug_getmetatable(jit_State *J, RecordFFData *rd) -{ - GCtab *mt; - TRef mtref; - TRef tr = J->base[0]; - if (tref_istab(tr)) { - mt = tabref(tabV(&rd->argv[0])->metatable); - mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_TAB_META); - } else if (tref_isudata(tr)) { - mt = tabref(udataV(&rd->argv[0])->metatable); - mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_UDATA_META); - } else { - mt = tabref(basemt_obj(J2G(J), &rd->argv[0])); - J->base[0] = mt ? lj_ir_ktab(J, mt) : TREF_NIL; - return; - } - emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB)); - J->base[0] = mt ? mtref : TREF_NIL; -} - -/* -- Record calls to fast functions -------------------------------------- */ - -#include "lj_recdef.h" - -static uint32_t recdef_lookup(GCfunc *fn) -{ - if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0])) - return recff_idmap[fn->c.ffid]; - else - return 0; -} - -/* Record entry to a fast function or C function. */ -void lj_ffrecord_func(jit_State *J) -{ - RecordFFData rd; - uint32_t m = recdef_lookup(J->fn); - rd.data = m & 0xff; - rd.nres = 1; /* Default is one result. */ - rd.argv = J->L->base; - J->base[J->maxslot] = 0; /* Mark end of arguments. */ - (recff_func[m >> 8])(J, &rd); /* Call recff_* handler. */ - if (rd.nres >= 0) { - if (J->postproc == LJ_POST_NONE) J->postproc = LJ_POST_FFRETRY; - lj_record_ret(J, 0, rd.nres); - } -} - -#undef IR -#undef emitir - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.h deleted file mode 100644 index 0acb8ed..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ffrecord.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** Fast function call recorder. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FFRECORD_H -#define _LJ_FFRECORD_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -/* Data used by handlers to record a fast function. */ -typedef struct RecordFFData { - TValue *argv; /* Runtime argument values. */ - ptrdiff_t nres; /* Number of returned results (defaults to 1). */ - uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */ -} RecordFFData; - -LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv); -LJ_FUNC void lj_ffrecord_func(jit_State *J); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_frame.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_frame.h deleted file mode 100644 index aa1dc11..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_frame.h +++ /dev/null @@ -1,297 +0,0 @@ -/* -** Stack frames. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FRAME_H -#define _LJ_FRAME_H - -#include "lj_obj.h" -#include "lj_bc.h" - -/* -- Lua stack frame ----------------------------------------------------- */ - -/* Frame type markers in LSB of PC (4-byte aligned) or delta (8-byte aligned: -** -** PC 00 Lua frame -** delta 001 C frame -** delta 010 Continuation frame -** delta 011 Lua vararg frame -** delta 101 cpcall() frame -** delta 110 ff pcall() frame -** delta 111 ff pcall() frame with active hook -*/ -enum { - FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG, - FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH -}; -#define FRAME_TYPE 3 -#define FRAME_P 4 -#define FRAME_TYPEP (FRAME_TYPE|FRAME_P) - -/* Macros to access and modify Lua frames. */ -#if LJ_FR2 -/* Two-slot frame info, required for 64 bit PC/GCRef: -** -** base-2 base-1 | base base+1 ... -** [func PC/delta/ft] | [slots ...] -** ^-- frame | ^-- base ^-- top -** -** Continuation frames: -** -** base-4 base-3 base-2 base-1 | base base+1 ... -** [cont PC ] [func PC/delta/ft] | [slots ...] -** ^-- frame | ^-- base ^-- top -*/ -#define frame_gc(f) (gcval((f)-1)) -#define frame_ftsz(f) ((ptrdiff_t)(f)->ftsz) -#define frame_pc(f) ((const BCIns *)frame_ftsz(f)) -#define setframe_gc(f, p, tp) (setgcVraw((f), (p), (tp))) -#define setframe_ftsz(f, sz) ((f)->ftsz = (sz)) -#define setframe_pc(f, pc) ((f)->ftsz = (int64_t)(intptr_t)(pc)) -#else -/* One-slot frame info, sufficient for 32 bit PC/GCRef: -** -** base-1 | base base+1 ... -** lo hi | -** [func | PC/delta/ft] | [slots ...] -** ^-- frame | ^-- base ^-- top -** -** Continuation frames: -** -** base-2 base-1 | base base+1 ... -** lo hi lo hi | -** [cont | PC] [func | PC/delta/ft] | [slots ...] -** ^-- frame | ^-- base ^-- top -*/ -#define frame_gc(f) (gcref((f)->fr.func)) -#define frame_ftsz(f) ((ptrdiff_t)(f)->fr.tp.ftsz) -#define frame_pc(f) (mref((f)->fr.tp.pcr, const BCIns)) -#define setframe_gc(f, p, tp) (setgcref((f)->fr.func, (p)), UNUSED(tp)) -#define setframe_ftsz(f, sz) ((f)->fr.tp.ftsz = (int32_t)(sz)) -#define setframe_pc(f, pc) (setmref((f)->fr.tp.pcr, (pc))) -#endif - -#define frame_type(f) (frame_ftsz(f) & FRAME_TYPE) -#define frame_typep(f) (frame_ftsz(f) & FRAME_TYPEP) -#define frame_islua(f) (frame_type(f) == FRAME_LUA) -#define frame_isc(f) (frame_type(f) == FRAME_C) -#define frame_iscont(f) (frame_typep(f) == FRAME_CONT) -#define frame_isvarg(f) (frame_typep(f) == FRAME_VARG) -#define frame_ispcall(f) ((frame_ftsz(f) & 6) == FRAME_PCALL) - -#define frame_func(f) (&frame_gc(f)->fn) -#define frame_delta(f) (frame_ftsz(f) >> 3) -#define frame_sized(f) (frame_ftsz(f) & ~FRAME_TYPEP) - -enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */ - -#if LJ_FR2 -#define frame_contpc(f) (frame_pc((f)-2)) -#define frame_contv(f) (((f)-3)->u64) -#else -#define frame_contpc(f) (frame_pc((f)-1)) -#define frame_contv(f) (((f)-1)->u32.lo) -#endif -#if LJ_FR2 -#define frame_contf(f) ((ASMFunction)(uintptr_t)((f)-3)->u64) -#elif LJ_64 -#define frame_contf(f) \ - ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \ - (intptr_t)(int32_t)((f)-1)->u32.lo)) -#else -#define frame_contf(f) ((ASMFunction)gcrefp(((f)-1)->gcr, void)) -#endif -#define frame_iscont_fficb(f) \ - (LJ_HASFFI && frame_contv(f) == LJ_CONT_FFI_CALLBACK) - -#define frame_prevl(f) ((f) - (1+LJ_FR2+bc_a(frame_pc(f)[-1]))) -#define frame_prevd(f) ((TValue *)((char *)(f) - frame_sized(f))) -#define frame_prev(f) (frame_islua(f)?frame_prevl(f):frame_prevd(f)) -/* Note: this macro does not skip over FRAME_VARG. */ - -/* -- C stack frame ------------------------------------------------------- */ - -/* Macros to access and modify the C stack frame chain. */ - -/* These definitions must match with the arch-specific *.dasc files. */ -#if LJ_TARGET_X86 -#if LJ_ABI_WIN -#define CFRAME_OFS_ERRF (19*4) -#define CFRAME_OFS_NRES (18*4) -#define CFRAME_OFS_PREV (17*4) -#define CFRAME_OFS_L (16*4) -#define CFRAME_OFS_SEH (9*4) -#define CFRAME_OFS_PC (6*4) -#define CFRAME_OFS_MULTRES (5*4) -#define CFRAME_SIZE (16*4) -#define CFRAME_SHIFT_MULTRES 0 -#else -#define CFRAME_OFS_ERRF (15*4) -#define CFRAME_OFS_NRES (14*4) -#define CFRAME_OFS_PREV (13*4) -#define CFRAME_OFS_L (12*4) -#define CFRAME_OFS_PC (6*4) -#define CFRAME_OFS_MULTRES (5*4) -#define CFRAME_SIZE (12*4) -#define CFRAME_SHIFT_MULTRES 0 -#endif -#elif LJ_TARGET_X64 -#if LJ_ABI_WIN -#define CFRAME_OFS_PREV (13*8) -#if LJ_GC64 -#define CFRAME_OFS_PC (12*8) -#define CFRAME_OFS_L (11*8) -#define CFRAME_OFS_ERRF (21*4) -#define CFRAME_OFS_NRES (20*4) -#define CFRAME_OFS_MULTRES (8*4) -#else -#define CFRAME_OFS_PC (25*4) -#define CFRAME_OFS_L (24*4) -#define CFRAME_OFS_ERRF (23*4) -#define CFRAME_OFS_NRES (22*4) -#define CFRAME_OFS_MULTRES (21*4) -#endif -#define CFRAME_SIZE (10*8) -#define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8) -#define CFRAME_SHIFT_MULTRES 0 -#else -#define CFRAME_OFS_PREV (4*8) -#if LJ_GC64 -#define CFRAME_OFS_PC (3*8) -#define CFRAME_OFS_L (2*8) -#define CFRAME_OFS_ERRF (3*4) -#define CFRAME_OFS_NRES (2*4) -#define CFRAME_OFS_MULTRES (0*4) -#else -#define CFRAME_OFS_PC (7*4) -#define CFRAME_OFS_L (6*4) -#define CFRAME_OFS_ERRF (5*4) -#define CFRAME_OFS_NRES (4*4) -#define CFRAME_OFS_MULTRES (1*4) -#endif -#if LJ_NO_UNWIND -#define CFRAME_SIZE (12*8) -#else -#define CFRAME_SIZE (10*8) -#endif -#define CFRAME_SIZE_JIT (CFRAME_SIZE + 16) -#define CFRAME_SHIFT_MULTRES 0 -#endif -#elif LJ_TARGET_ARM -#define CFRAME_OFS_ERRF 24 -#define CFRAME_OFS_NRES 20 -#define CFRAME_OFS_PREV 16 -#define CFRAME_OFS_L 12 -#define CFRAME_OFS_PC 8 -#define CFRAME_OFS_MULTRES 4 -#if LJ_ARCH_HASFPU -#define CFRAME_SIZE 128 -#else -#define CFRAME_SIZE 64 -#endif -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_TARGET_ARM64 -#define CFRAME_OFS_ERRF 36 -#define CFRAME_OFS_NRES 40 -#define CFRAME_OFS_PREV 0 -#define CFRAME_OFS_L 16 -#define CFRAME_OFS_PC 8 -#define CFRAME_OFS_MULTRES 32 -#define CFRAME_SIZE 208 -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_TARGET_PPC -#if LJ_TARGET_XBOX360 -#define CFRAME_OFS_ERRF 424 -#define CFRAME_OFS_NRES 420 -#define CFRAME_OFS_PREV 400 -#define CFRAME_OFS_L 416 -#define CFRAME_OFS_PC 412 -#define CFRAME_OFS_MULTRES 408 -#define CFRAME_SIZE 384 -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_ARCH_PPC32ON64 -#define CFRAME_OFS_ERRF 472 -#define CFRAME_OFS_NRES 468 -#define CFRAME_OFS_PREV 448 -#define CFRAME_OFS_L 464 -#define CFRAME_OFS_PC 460 -#define CFRAME_OFS_MULTRES 456 -#define CFRAME_SIZE 400 -#define CFRAME_SHIFT_MULTRES 3 -#else -#define CFRAME_OFS_ERRF 48 -#define CFRAME_OFS_NRES 44 -#define CFRAME_OFS_PREV 40 -#define CFRAME_OFS_L 36 -#define CFRAME_OFS_PC 32 -#define CFRAME_OFS_MULTRES 28 -#define CFRAME_SIZE (LJ_ARCH_HASFPU ? 272 : 128) -#define CFRAME_SHIFT_MULTRES 3 -#endif -#elif LJ_TARGET_MIPS32 -#if LJ_ARCH_HASFPU -#define CFRAME_OFS_ERRF 124 -#define CFRAME_OFS_NRES 120 -#define CFRAME_OFS_PREV 116 -#define CFRAME_OFS_L 112 -#define CFRAME_SIZE 112 -#else -#define CFRAME_OFS_ERRF 76 -#define CFRAME_OFS_NRES 72 -#define CFRAME_OFS_PREV 68 -#define CFRAME_OFS_L 64 -#define CFRAME_SIZE 64 -#endif -#define CFRAME_OFS_PC 20 -#define CFRAME_OFS_MULTRES 16 -#define CFRAME_SHIFT_MULTRES 3 -#elif LJ_TARGET_MIPS64 -#if LJ_ARCH_HASFPU -#define CFRAME_OFS_ERRF 188 -#define CFRAME_OFS_NRES 184 -#define CFRAME_OFS_PREV 176 -#define CFRAME_OFS_L 168 -#define CFRAME_OFS_PC 160 -#define CFRAME_SIZE 192 -#else -#define CFRAME_OFS_ERRF 124 -#define CFRAME_OFS_NRES 120 -#define CFRAME_OFS_PREV 112 -#define CFRAME_OFS_L 104 -#define CFRAME_OFS_PC 96 -#define CFRAME_SIZE 128 -#endif -#define CFRAME_OFS_MULTRES 0 -#define CFRAME_SHIFT_MULTRES 3 -#else -#error "Missing CFRAME_* definitions for this architecture" -#endif - -#ifndef CFRAME_SIZE_JIT -#define CFRAME_SIZE_JIT CFRAME_SIZE -#endif - -#define CFRAME_RESUME 1 -#define CFRAME_UNWIND_FF 2 /* Only used in unwinder. */ -#define CFRAME_RAWMASK (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF)) - -#define cframe_errfunc(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF)) -#define cframe_nres(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES)) -#define cframe_prev(cf) (*(void **)(((char *)(cf))+CFRAME_OFS_PREV)) -#define cframe_multres(cf) (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES)) -#define cframe_multres_n(cf) (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES) -#define cframe_L(cf) \ - (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th) -#define cframe_pc(cf) \ - (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns)) -#define setcframe_L(cf, L) \ - (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L))) -#define setcframe_pc(cf, pc) \ - (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc))) -#define cframe_canyield(cf) ((intptr_t)(cf) & CFRAME_RESUME) -#define cframe_unwind_ff(cf) ((intptr_t)(cf) & CFRAME_UNWIND_FF) -#define cframe_raw(cf) ((void *)((intptr_t)(cf) & CFRAME_RAWMASK)) -#define cframe_Lpc(L) cframe_pc(cframe_raw(L->cframe)) - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.c deleted file mode 100644 index 9795a77..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.c +++ /dev/null @@ -1,191 +0,0 @@ -/* -** Function handling (prototypes, functions and upvalues). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_func_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_func.h" -#include "lj_trace.h" -#include "lj_vm.h" - -/* -- Prototypes ---------------------------------------------------------- */ - -void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt) -{ - lj_mem_free(g, pt, pt->sizept); -} - -/* -- Upvalues ------------------------------------------------------------ */ - -static void unlinkuv(global_State *g, GCupval *uv) -{ - UNUSED(g); - lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv, - "broken upvalue chain"); - setgcrefr(uvnext(uv)->prev, uv->prev); - setgcrefr(uvprev(uv)->next, uv->next); -} - -/* Find existing open upvalue for a stack slot or create a new one. */ -static GCupval *func_finduv(lua_State *L, TValue *slot) -{ - global_State *g = G(L); - GCRef *pp = &L->openupval; - GCupval *p; - GCupval *uv; - /* Search the sorted list of open upvalues. */ - while (gcref(*pp) != NULL && uvval((p = gco2uv(gcref(*pp)))) >= slot) { - lj_assertG(!p->closed && uvval(p) != &p->tv, "closed upvalue in chain"); - if (uvval(p) == slot) { /* Found open upvalue pointing to same slot? */ - if (isdead(g, obj2gco(p))) /* Resurrect it, if it's dead. */ - flipwhite(obj2gco(p)); - return p; - } - pp = &p->nextgc; - } - /* No matching upvalue found. Create a new one. */ - uv = lj_mem_newt(L, sizeof(GCupval), GCupval); - newwhite(g, uv); - uv->gct = ~LJ_TUPVAL; - uv->closed = 0; /* Still open. */ - setmref(uv->v, slot); /* Pointing to the stack slot. */ - /* NOBARRIER: The GCupval is new (marked white) and open. */ - setgcrefr(uv->nextgc, *pp); /* Insert into sorted list of open upvalues. */ - setgcref(*pp, obj2gco(uv)); - setgcref(uv->prev, obj2gco(&g->uvhead)); /* Insert into GC list, too. */ - setgcrefr(uv->next, g->uvhead.next); - setgcref(uvnext(uv)->prev, obj2gco(uv)); - setgcref(g->uvhead.next, obj2gco(uv)); - lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv, - "broken upvalue chain"); - return uv; -} - -/* Create an empty and closed upvalue. */ -static GCupval *func_emptyuv(lua_State *L) -{ - GCupval *uv = (GCupval *)lj_mem_newgco(L, sizeof(GCupval)); - uv->gct = ~LJ_TUPVAL; - uv->closed = 1; - setnilV(&uv->tv); - setmref(uv->v, &uv->tv); - return uv; -} - -/* Close all open upvalues pointing to some stack level or above. */ -void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level) -{ - GCupval *uv; - global_State *g = G(L); - while (gcref(L->openupval) != NULL && - uvval((uv = gco2uv(gcref(L->openupval)))) >= level) { - GCobj *o = obj2gco(uv); - lj_assertG(!isblack(o), "bad black upvalue"); - lj_assertG(!uv->closed && uvval(uv) != &uv->tv, "closed upvalue in chain"); - setgcrefr(L->openupval, uv->nextgc); /* No longer in open list. */ - if (isdead(g, o)) { - lj_func_freeuv(g, uv); - } else { - unlinkuv(g, uv); - lj_gc_closeuv(g, uv); - } - } -} - -void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv) -{ - if (!uv->closed) - unlinkuv(g, uv); - lj_mem_freet(g, uv); -} - -/* -- Functions (closures) ------------------------------------------------ */ - -GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env) -{ - GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeCfunc(nelems)); - fn->c.gct = ~LJ_TFUNC; - fn->c.ffid = FF_C; - fn->c.nupvalues = (uint8_t)nelems; - /* NOBARRIER: The GCfunc is new (marked white). */ - setmref(fn->c.pc, &G(L)->bc_cfunc_ext); - setgcref(fn->c.env, obj2gco(env)); - return fn; -} - -static GCfunc *func_newL(lua_State *L, GCproto *pt, GCtab *env) -{ - uint32_t count; - GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeLfunc((MSize)pt->sizeuv)); - fn->l.gct = ~LJ_TFUNC; - fn->l.ffid = FF_LUA; - fn->l.nupvalues = 0; /* Set to zero until upvalues are initialized. */ - /* NOBARRIER: Really a setgcref. But the GCfunc is new (marked white). */ - setmref(fn->l.pc, proto_bc(pt)); - setgcref(fn->l.env, obj2gco(env)); - /* Saturating 3 bit counter (0..7) for created closures. */ - count = (uint32_t)pt->flags + PROTO_CLCOUNT; - pt->flags = (uint8_t)(count - ((count >> PROTO_CLC_BITS) & PROTO_CLCOUNT)); - return fn; -} - -/* Create a new Lua function with empty upvalues. */ -GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env) -{ - GCfunc *fn = func_newL(L, pt, env); - MSize i, nuv = pt->sizeuv; - /* NOBARRIER: The GCfunc is new (marked white). */ - for (i = 0; i < nuv; i++) { - GCupval *uv = func_emptyuv(L); - int32_t v = proto_uv(pt)[i]; - uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1); - uv->dhash = (uint32_t)(uintptr_t)pt ^ (v << 24); - setgcref(fn->l.uvptr[i], obj2gco(uv)); - } - fn->l.nupvalues = (uint8_t)nuv; - return fn; -} - -/* Do a GC check and create a new Lua function with inherited upvalues. */ -GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent) -{ - GCfunc *fn; - GCRef *puv; - MSize i, nuv; - TValue *base; - lj_gc_check_fixtop(L); - fn = func_newL(L, pt, tabref(parent->env)); - /* NOBARRIER: The GCfunc is new (marked white). */ - puv = parent->uvptr; - nuv = pt->sizeuv; - base = L->base; - for (i = 0; i < nuv; i++) { - uint32_t v = proto_uv(pt)[i]; - GCupval *uv; - if ((v & PROTO_UV_LOCAL)) { - uv = func_finduv(L, base + (v & 0xff)); - uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1); - uv->dhash = (uint32_t)(uintptr_t)mref(parent->pc, char) ^ (v << 24); - } else { - uv = &gcref(puv[v])->uv; - } - setgcref(fn->l.uvptr[i], obj2gco(uv)); - } - fn->l.nupvalues = (uint8_t)nuv; - return fn; -} - -void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *fn) -{ - MSize size = isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) : - sizeCfunc((MSize)fn->c.nupvalues); - lj_mem_free(g, fn, size); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.h deleted file mode 100644 index 44df4de..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_func.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** Function handling (prototypes, functions and upvalues). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_FUNC_H -#define _LJ_FUNC_H - -#include "lj_obj.h" - -/* Prototypes. */ -LJ_FUNC void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt); - -/* Upvalues. */ -LJ_FUNCA void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level); -LJ_FUNC void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv); - -/* Functions (closures). */ -LJ_FUNC GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env); -LJ_FUNC GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env); -LJ_FUNCA GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent); -LJ_FUNC void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *c); - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.c deleted file mode 100644 index 2fc52ec..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.c +++ /dev/null @@ -1,909 +0,0 @@ -/* -** Garbage collector. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_gc_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_udata.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#endif -#include "lj_trace.h" -#include "lj_dispatch.h" -#include "lj_vm.h" - -#define GCSTEPSIZE 1024u -#define GCSWEEPMAX 40 -#define GCSWEEPCOST 10 -#define GCFINALIZECOST 100 - -/* Macros to set GCobj colors and flags. */ -#define white2gray(x) ((x)->gch.marked &= (uint8_t)~LJ_GC_WHITES) -#define gray2black(x) ((x)->gch.marked |= LJ_GC_BLACK) -#define isfinalized(u) ((u)->marked & LJ_GC_FINALIZED) - -/* -- Mark phase ---------------------------------------------------------- */ - -/* Mark a TValue (if needed). */ -#define gc_marktv(g, tv) \ - { lj_assertG(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct), \ - "TValue and GC type mismatch"); \ - if (tviswhite(tv)) gc_mark(g, gcV(tv)); } - -/* Mark a GCobj (if needed). */ -#define gc_markobj(g, o) \ - { if (iswhite(obj2gco(o))) gc_mark(g, obj2gco(o)); } - -/* Mark a string object. */ -#define gc_mark_str(s) ((s)->marked &= (uint8_t)~LJ_GC_WHITES) - -/* Mark a white GCobj. */ -static void gc_mark(global_State *g, GCobj *o) -{ - int gct = o->gch.gct; - lj_assertG(iswhite(o), "mark of non-white object"); - lj_assertG(!isdead(g, o), "mark of dead object"); - white2gray(o); - if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) { - GCtab *mt = tabref(gco2ud(o)->metatable); - gray2black(o); /* Userdata are never gray. */ - if (mt) gc_markobj(g, mt); - gc_markobj(g, tabref(gco2ud(o)->env)); - if (LJ_HASBUFFER && gco2ud(o)->udtype == UDTYPE_BUFFER) { - SBufExt *sbx = (SBufExt *)uddata(gco2ud(o)); - if (sbufiscow(sbx) && gcref(sbx->cowref)) - gc_markobj(g, gcref(sbx->cowref)); - if (gcref(sbx->dict_str)) - gc_markobj(g, gcref(sbx->dict_str)); - if (gcref(sbx->dict_mt)) - gc_markobj(g, gcref(sbx->dict_mt)); - } - } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) { - GCupval *uv = gco2uv(o); - gc_marktv(g, uvval(uv)); - if (uv->closed) - gray2black(o); /* Closed upvalues are never gray. */ - } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) { - lj_assertG(gct == ~LJ_TFUNC || gct == ~LJ_TTAB || - gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO || gct == ~LJ_TTRACE, - "bad GC type %d", gct); - setgcrefr(o->gch.gclist, g->gc.gray); - setgcref(g->gc.gray, o); - } -} - -/* Mark GC roots. */ -static void gc_mark_gcroot(global_State *g) -{ - ptrdiff_t i; - for (i = 0; i < GCROOT_MAX; i++) - if (gcref(g->gcroot[i]) != NULL) - gc_markobj(g, gcref(g->gcroot[i])); -} - -/* Start a GC cycle and mark the root set. */ -static void gc_mark_start(global_State *g) -{ - setgcrefnull(g->gc.gray); - setgcrefnull(g->gc.grayagain); - setgcrefnull(g->gc.weak); - gc_markobj(g, mainthread(g)); - gc_markobj(g, tabref(mainthread(g)->env)); - gc_marktv(g, &g->registrytv); - gc_mark_gcroot(g); - g->gc.state = GCSpropagate; -} - -/* Mark open upvalues. */ -static void gc_mark_uv(global_State *g) -{ - GCupval *uv; - for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) { - lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv, - "broken upvalue chain"); - if (isgray(obj2gco(uv))) - gc_marktv(g, uvval(uv)); - } -} - -/* Mark userdata in mmudata list. */ -static void gc_mark_mmudata(global_State *g) -{ - GCobj *root = gcref(g->gc.mmudata); - GCobj *u = root; - if (u) { - do { - u = gcnext(u); - makewhite(g, u); /* Could be from previous GC. */ - gc_mark(g, u); - } while (u != root); - } -} - -/* Separate userdata objects to be finalized to mmudata list. */ -size_t lj_gc_separateudata(global_State *g, int all) -{ - size_t m = 0; - GCRef *p = &mainthread(g)->nextgc; - GCobj *o; - while ((o = gcref(*p)) != NULL) { - if (!(iswhite(o) || all) || isfinalized(gco2ud(o))) { - p = &o->gch.nextgc; /* Nothing to do. */ - } else if (!lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc)) { - markfinalized(o); /* Done, as there's no __gc metamethod. */ - p = &o->gch.nextgc; - } else { /* Otherwise move userdata to be finalized to mmudata list. */ - m += sizeudata(gco2ud(o)); - markfinalized(o); - *p = o->gch.nextgc; - if (gcref(g->gc.mmudata)) { /* Link to end of mmudata list. */ - GCobj *root = gcref(g->gc.mmudata); - setgcrefr(o->gch.nextgc, root->gch.nextgc); - setgcref(root->gch.nextgc, o); - setgcref(g->gc.mmudata, o); - } else { /* Create circular list. */ - setgcref(o->gch.nextgc, o); - setgcref(g->gc.mmudata, o); - } - } - } - return m; -} - -/* -- Propagation phase --------------------------------------------------- */ - -/* Traverse a table. */ -static int gc_traverse_tab(global_State *g, GCtab *t) -{ - int weak = 0; - cTValue *mode; - GCtab *mt = tabref(t->metatable); - if (mt) - gc_markobj(g, mt); - mode = lj_meta_fastg(g, mt, MM_mode); - if (mode && tvisstr(mode)) { /* Valid __mode field? */ - const char *modestr = strVdata(mode); - int c; - while ((c = *modestr++)) { - if (c == 'k') weak |= LJ_GC_WEAKKEY; - else if (c == 'v') weak |= LJ_GC_WEAKVAL; - } - if (weak) { /* Weak tables are cleared in the atomic phase. */ -#if LJ_HASFFI - CTState *cts = ctype_ctsG(g); - if (cts && cts->finalizer == t) { - weak = (int)(~0u & ~LJ_GC_WEAKVAL); - } else -#endif - { - t->marked = (uint8_t)((t->marked & ~LJ_GC_WEAK) | weak); - setgcrefr(t->gclist, g->gc.weak); - setgcref(g->gc.weak, obj2gco(t)); - } - } - } - if (weak == LJ_GC_WEAK) /* Nothing to mark if both keys/values are weak. */ - return 1; - if (!(weak & LJ_GC_WEAKVAL)) { /* Mark array part. */ - MSize i, asize = t->asize; - for (i = 0; i < asize; i++) - gc_marktv(g, arrayslot(t, i)); - } - if (t->hmask > 0) { /* Mark hash part. */ - Node *node = noderef(t->node); - MSize i, hmask = t->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (!tvisnil(&n->val)) { /* Mark non-empty slot. */ - lj_assertG(!tvisnil(&n->key), "mark of nil key in non-empty slot"); - if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key); - if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val); - } - } - } - return weak; -} - -/* Traverse a function. */ -static void gc_traverse_func(global_State *g, GCfunc *fn) -{ - gc_markobj(g, tabref(fn->c.env)); - if (isluafunc(fn)) { - uint32_t i; - lj_assertG(fn->l.nupvalues <= funcproto(fn)->sizeuv, - "function upvalues out of range"); - gc_markobj(g, funcproto(fn)); - for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */ - gc_markobj(g, &gcref(fn->l.uvptr[i])->uv); - } else { - uint32_t i; - for (i = 0; i < fn->c.nupvalues; i++) /* Mark C function upvalues. */ - gc_marktv(g, &fn->c.upvalue[i]); - } -} - -#if LJ_HASJIT -/* Mark a trace. */ -static void gc_marktrace(global_State *g, TraceNo traceno) -{ - GCobj *o = obj2gco(traceref(G2J(g), traceno)); - lj_assertG(traceno != G2J(g)->cur.traceno, "active trace escaped"); - if (iswhite(o)) { - white2gray(o); - setgcrefr(o->gch.gclist, g->gc.gray); - setgcref(g->gc.gray, o); - } -} - -/* Traverse a trace. */ -static void gc_traverse_trace(global_State *g, GCtrace *T) -{ - IRRef ref; - if (T->traceno == 0) return; - for (ref = T->nk; ref < REF_TRUE; ref++) { - IRIns *ir = &T->ir[ref]; - if (ir->o == IR_KGC) - gc_markobj(g, ir_kgc(ir)); - if (irt_is64(ir->t) && ir->o != IR_KNULL) - ref++; - } - if (T->link) gc_marktrace(g, T->link); - if (T->nextroot) gc_marktrace(g, T->nextroot); - if (T->nextside) gc_marktrace(g, T->nextside); - gc_markobj(g, gcref(T->startpt)); -} - -/* The current trace is a GC root while not anchored in the prototype (yet). */ -#define gc_traverse_curtrace(g) gc_traverse_trace(g, &G2J(g)->cur) -#else -#define gc_traverse_curtrace(g) UNUSED(g) -#endif - -/* Traverse a prototype. */ -static void gc_traverse_proto(global_State *g, GCproto *pt) -{ - ptrdiff_t i; - gc_mark_str(proto_chunkname(pt)); - for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */ - gc_markobj(g, proto_kgc(pt, i)); -#if LJ_HASJIT - if (pt->trace) gc_marktrace(g, pt->trace); -#endif -} - -/* Traverse the frame structure of a stack. */ -static MSize gc_traverse_frames(global_State *g, lua_State *th) -{ - TValue *frame, *top = th->top-1, *bot = tvref(th->stack); - /* Note: extra vararg frame not skipped, marks function twice (harmless). */ - for (frame = th->base-1; frame > bot+LJ_FR2; frame = frame_prev(frame)) { - GCfunc *fn = frame_func(frame); - TValue *ftop = frame; - if (isluafunc(fn)) ftop += funcproto(fn)->framesize; - if (ftop > top) top = ftop; - if (!LJ_FR2) gc_markobj(g, fn); /* Need to mark hidden function (or L). */ - } - top++; /* Correct bias of -1 (frame == base-1). */ - if (top > tvref(th->maxstack)) top = tvref(th->maxstack); - return (MSize)(top - bot); /* Return minimum needed stack size. */ -} - -/* Traverse a thread object. */ -static void gc_traverse_thread(global_State *g, lua_State *th) -{ - TValue *o, *top = th->top; - for (o = tvref(th->stack)+1+LJ_FR2; o < top; o++) - gc_marktv(g, o); - if (g->gc.state == GCSatomic) { - top = tvref(th->stack) + th->stacksize; - for (; o < top; o++) /* Clear unmarked slots. */ - setnilV(o); - } - gc_markobj(g, tabref(th->env)); - lj_state_shrinkstack(th, gc_traverse_frames(g, th)); -} - -/* Propagate one gray object. Traverse it and turn it black. */ -static size_t propagatemark(global_State *g) -{ - GCobj *o = gcref(g->gc.gray); - int gct = o->gch.gct; - lj_assertG(isgray(o), "propagation of non-gray object"); - gray2black(o); - setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */ - if (LJ_LIKELY(gct == ~LJ_TTAB)) { - GCtab *t = gco2tab(o); - if (gc_traverse_tab(g, t) > 0) - black2gray(o); /* Keep weak tables gray. */ - return sizeof(GCtab) + sizeof(TValue) * t->asize + - (t->hmask ? sizeof(Node) * (t->hmask + 1) : 0); - } else if (LJ_LIKELY(gct == ~LJ_TFUNC)) { - GCfunc *fn = gco2func(o); - gc_traverse_func(g, fn); - return isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) : - sizeCfunc((MSize)fn->c.nupvalues); - } else if (LJ_LIKELY(gct == ~LJ_TPROTO)) { - GCproto *pt = gco2pt(o); - gc_traverse_proto(g, pt); - return pt->sizept; - } else if (LJ_LIKELY(gct == ~LJ_TTHREAD)) { - lua_State *th = gco2th(o); - setgcrefr(th->gclist, g->gc.grayagain); - setgcref(g->gc.grayagain, o); - black2gray(o); /* Threads are never black. */ - gc_traverse_thread(g, th); - return sizeof(lua_State) + sizeof(TValue) * th->stacksize; - } else { -#if LJ_HASJIT - GCtrace *T = gco2trace(o); - gc_traverse_trace(g, T); - return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + - T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry); -#else - lj_assertG(0, "bad GC type %d", gct); - return 0; -#endif - } -} - -/* Propagate all gray objects. */ -static size_t gc_propagate_gray(global_State *g) -{ - size_t m = 0; - while (gcref(g->gc.gray) != NULL) - m += propagatemark(g); - return m; -} - -/* -- Sweep phase --------------------------------------------------------- */ - -/* Type of GC free functions. */ -typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o); - -/* GC free functions for LJ_TSTR .. LJ_TUDATA. ORDER LJ_T */ -static const GCFreeFunc gc_freefunc[] = { - (GCFreeFunc)lj_str_free, - (GCFreeFunc)lj_func_freeuv, - (GCFreeFunc)lj_state_free, - (GCFreeFunc)lj_func_freeproto, - (GCFreeFunc)lj_func_free, -#if LJ_HASJIT - (GCFreeFunc)lj_trace_free, -#else - (GCFreeFunc)0, -#endif -#if LJ_HASFFI - (GCFreeFunc)lj_cdata_free, -#else - (GCFreeFunc)0, -#endif - (GCFreeFunc)lj_tab_free, - (GCFreeFunc)lj_udata_free -}; - -/* Full sweep of a GC list. */ -#define gc_fullsweep(g, p) gc_sweep(g, (p), ~(uint32_t)0) - -/* Partial sweep of a GC list. */ -static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim) -{ - /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */ - int ow = otherwhite(g); - GCobj *o; - while ((o = gcref(*p)) != NULL && lim-- > 0) { - if (o->gch.gct == ~LJ_TTHREAD) /* Need to sweep open upvalues, too. */ - gc_fullsweep(g, &gco2th(o)->openupval); - if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */ - lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED), - "sweep of undead object"); - makewhite(g, o); /* Value is alive, change to the current white. */ - p = &o->gch.nextgc; - } else { /* Otherwise value is dead, free it. */ - lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED, - "sweep of unlive object"); - setgcrefr(*p, o->gch.nextgc); - if (o == gcref(g->gc.root)) - setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */ - gc_freefunc[o->gch.gct - ~LJ_TSTR](g, o); - } - } - return p; -} - -/* Sweep one string interning table chain. Preserves hashalg bit. */ -static void gc_sweepstr(global_State *g, GCRef *chain) -{ - /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */ - int ow = otherwhite(g); - uintptr_t u = gcrefu(*chain); - GCRef q; - GCRef *p = &q; - GCobj *o; - setgcrefp(q, (u & ~(uintptr_t)1)); - while ((o = gcref(*p)) != NULL) { - if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */ - lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED), - "sweep of undead string"); - makewhite(g, o); /* String is alive, change to the current white. */ - p = &o->gch.nextgc; - } else { /* Otherwise string is dead, free it. */ - lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED, - "sweep of unlive string"); - setgcrefr(*p, o->gch.nextgc); - lj_str_free(g, gco2str(o)); - } - } - setgcrefp(*chain, (gcrefu(q) | (u & 1))); -} - -/* Check whether we can clear a key or a value slot from a table. */ -static int gc_mayclear(cTValue *o, int val) -{ - if (tvisgcv(o)) { /* Only collectable objects can be weak references. */ - if (tvisstr(o)) { /* But strings cannot be used as weak references. */ - gc_mark_str(strV(o)); /* And need to be marked. */ - return 0; - } - if (iswhite(gcV(o))) - return 1; /* Object is about to be collected. */ - if (tvisudata(o) && val && isfinalized(udataV(o))) - return 1; /* Finalized userdata is dropped only from values. */ - } - return 0; /* Cannot clear. */ -} - -/* Clear collected entries from weak tables. */ -static void gc_clearweak(global_State *g, GCobj *o) -{ - UNUSED(g); - while (o) { - GCtab *t = gco2tab(o); - lj_assertG((t->marked & LJ_GC_WEAK), "clear of non-weak table"); - if ((t->marked & LJ_GC_WEAKVAL)) { - MSize i, asize = t->asize; - for (i = 0; i < asize; i++) { - /* Clear array slot when value is about to be collected. */ - TValue *tv = arrayslot(t, i); - if (gc_mayclear(tv, 1)) - setnilV(tv); - } - } - if (t->hmask > 0) { - Node *node = noderef(t->node); - MSize i, hmask = t->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - /* Clear hash slot when key or value is about to be collected. */ - if (!tvisnil(&n->val) && (gc_mayclear(&n->key, 0) || - gc_mayclear(&n->val, 1))) - setnilV(&n->val); - } - } - o = gcref(t->gclist); - } -} - -/* Call a userdata or cdata finalizer. */ -static void gc_call_finalizer(global_State *g, lua_State *L, - cTValue *mo, GCobj *o) -{ - /* Save and restore lots of state around the __gc callback. */ - uint8_t oldh = hook_save(g); - GCSize oldt = g->gc.threshold; - int errcode; - TValue *top; - lj_trace_abort(g); - hook_entergc(g); /* Disable hooks and new traces during __gc. */ - if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g); - g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */ - top = L->top; - copyTV(L, top++, mo); - if (LJ_FR2) setnilV(top++); - setgcV(L, top, o, ~o->gch.gct); - L->top = top+1; - errcode = lj_vm_pcall(L, top, 1+0, -1); /* Stack: |mo|o| -> | */ - hook_restore(g, oldh); - if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g); - g->gc.threshold = oldt; /* Restore GC threshold. */ - if (errcode) - lj_err_throw(L, errcode); /* Propagate errors. */ -} - -/* Finalize one userdata or cdata object from the mmudata list. */ -static void gc_finalize(lua_State *L) -{ - global_State *g = G(L); - GCobj *o = gcnext(gcref(g->gc.mmudata)); - cTValue *mo; - lj_assertG(tvref(g->jit_base) == NULL, "finalizer called on trace"); - /* Unchain from list of userdata to be finalized. */ - if (o == gcref(g->gc.mmudata)) - setgcrefnull(g->gc.mmudata); - else - setgcrefr(gcref(g->gc.mmudata)->gch.nextgc, o->gch.nextgc); -#if LJ_HASFFI - if (o->gch.gct == ~LJ_TCDATA) { - TValue tmp, *tv; - /* Add cdata back to the GC list and make it white. */ - setgcrefr(o->gch.nextgc, g->gc.root); - setgcref(g->gc.root, o); - makewhite(g, o); - o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN; - /* Resolve finalizer. */ - setcdataV(L, &tmp, gco2cd(o)); - tv = lj_tab_set(L, ctype_ctsG(g)->finalizer, &tmp); - if (!tvisnil(tv)) { - g->gc.nocdatafin = 0; - copyTV(L, &tmp, tv); - setnilV(tv); /* Clear entry in finalizer table. */ - gc_call_finalizer(g, L, &tmp, o); - } - return; - } -#endif - /* Add userdata back to the main userdata list and make it white. */ - setgcrefr(o->gch.nextgc, mainthread(g)->nextgc); - setgcref(mainthread(g)->nextgc, o); - makewhite(g, o); - /* Resolve the __gc metamethod. */ - mo = lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc); - if (mo) - gc_call_finalizer(g, L, mo, o); -} - -/* Finalize all userdata objects from mmudata list. */ -void lj_gc_finalize_udata(lua_State *L) -{ - while (gcref(G(L)->gc.mmudata) != NULL) - gc_finalize(L); -} - -#if LJ_HASFFI -/* Finalize all cdata objects from finalizer table. */ -void lj_gc_finalize_cdata(lua_State *L) -{ - global_State *g = G(L); - CTState *cts = ctype_ctsG(g); - if (cts) { - GCtab *t = cts->finalizer; - Node *node = noderef(t->node); - ptrdiff_t i; - setgcrefnull(t->metatable); /* Mark finalizer table as disabled. */ - for (i = (ptrdiff_t)t->hmask; i >= 0; i--) - if (!tvisnil(&node[i].val) && tviscdata(&node[i].key)) { - GCobj *o = gcV(&node[i].key); - TValue tmp; - makewhite(g, o); - o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN; - copyTV(L, &tmp, &node[i].val); - setnilV(&node[i].val); - gc_call_finalizer(g, L, &tmp, o); - } - } -} -#endif - -/* Free all remaining GC objects. */ -void lj_gc_freeall(global_State *g) -{ - MSize i, strmask; - /* Free everything, except super-fixed objects (the main thread). */ - g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED; - gc_fullsweep(g, &g->gc.root); - strmask = g->str.mask; - for (i = 0; i <= strmask; i++) /* Free all string hash chains. */ - gc_sweepstr(g, &g->str.tab[i]); -} - -/* -- Collector ----------------------------------------------------------- */ - -/* Atomic part of the GC cycle, transitioning from mark to sweep phase. */ -static void atomic(global_State *g, lua_State *L) -{ - size_t udsize; - - gc_mark_uv(g); /* Need to remark open upvalues (the thread may be dead). */ - gc_propagate_gray(g); /* Propagate any left-overs. */ - - setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */ - setgcrefnull(g->gc.weak); - lj_assertG(!iswhite(obj2gco(mainthread(g))), "main thread turned white"); - gc_markobj(g, L); /* Mark running thread. */ - gc_traverse_curtrace(g); /* Traverse current trace. */ - gc_mark_gcroot(g); /* Mark GC roots (again). */ - gc_propagate_gray(g); /* Propagate all of the above. */ - - setgcrefr(g->gc.gray, g->gc.grayagain); /* Empty the 2nd chance list. */ - setgcrefnull(g->gc.grayagain); - gc_propagate_gray(g); /* Propagate it. */ - - udsize = lj_gc_separateudata(g, 0); /* Separate userdata to be finalized. */ - gc_mark_mmudata(g); /* Mark them. */ - udsize += gc_propagate_gray(g); /* And propagate the marks. */ - - /* All marking done, clear weak tables. */ - gc_clearweak(g, gcref(g->gc.weak)); - - lj_buf_shrink(L, &g->tmpbuf); /* Shrink temp buffer. */ - - /* Prepare for sweep phase. */ - g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */ - g->strempty.marked = g->gc.currentwhite; - setmref(g->gc.sweep, &g->gc.root); - g->gc.estimate = g->gc.total - (GCSize)udsize; /* Initial estimate. */ -} - -/* GC state machine. Returns a cost estimate for each step performed. */ -static size_t gc_onestep(lua_State *L) -{ - global_State *g = G(L); - switch (g->gc.state) { - case GCSpause: - gc_mark_start(g); /* Start a new GC cycle by marking all GC roots. */ - return 0; - case GCSpropagate: - if (gcref(g->gc.gray) != NULL) - return propagatemark(g); /* Propagate one gray object. */ - g->gc.state = GCSatomic; /* End of mark phase. */ - return 0; - case GCSatomic: - if (tvref(g->jit_base)) /* Don't run atomic phase on trace. */ - return LJ_MAX_MEM; - atomic(g, L); - g->gc.state = GCSsweepstring; /* Start of sweep phase. */ - g->gc.sweepstr = 0; - return 0; - case GCSsweepstring: { - GCSize old = g->gc.total; - gc_sweepstr(g, &g->str.tab[g->gc.sweepstr++]); /* Sweep one chain. */ - if (g->gc.sweepstr > g->str.mask) - g->gc.state = GCSsweep; /* All string hash chains sweeped. */ - lj_assertG(old >= g->gc.total, "sweep increased memory"); - g->gc.estimate -= old - g->gc.total; - return GCSWEEPCOST; - } - case GCSsweep: { - GCSize old = g->gc.total; - setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX)); - lj_assertG(old >= g->gc.total, "sweep increased memory"); - g->gc.estimate -= old - g->gc.total; - if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) { - if (g->str.num <= (g->str.mask >> 2) && g->str.mask > LJ_MIN_STRTAB*2-1) - lj_str_resize(L, g->str.mask >> 1); /* Shrink string table. */ - if (gcref(g->gc.mmudata)) { /* Need any finalizations? */ - g->gc.state = GCSfinalize; -#if LJ_HASFFI - g->gc.nocdatafin = 1; -#endif - } else { /* Otherwise skip this phase to help the JIT. */ - g->gc.state = GCSpause; /* End of GC cycle. */ - g->gc.debt = 0; - } - } - return GCSWEEPMAX*GCSWEEPCOST; - } - case GCSfinalize: - if (gcref(g->gc.mmudata) != NULL) { - GCSize old = g->gc.total; - if (tvref(g->jit_base)) /* Don't call finalizers on trace. */ - return LJ_MAX_MEM; - gc_finalize(L); /* Finalize one userdata object. */ - if (old >= g->gc.total && g->gc.estimate > old - g->gc.total) - g->gc.estimate -= old - g->gc.total; - if (g->gc.estimate > GCFINALIZECOST) - g->gc.estimate -= GCFINALIZECOST; - return GCFINALIZECOST; - } -#if LJ_HASFFI - if (!g->gc.nocdatafin) lj_tab_rehash(L, ctype_ctsG(g)->finalizer); -#endif - g->gc.state = GCSpause; /* End of GC cycle. */ - g->gc.debt = 0; - return 0; - default: - lj_assertG(0, "bad GC state"); - return 0; - } -} - -/* Perform a limited amount of incremental GC steps. */ -int LJ_FASTCALL lj_gc_step(lua_State *L) -{ - global_State *g = G(L); - GCSize lim; - int32_t ostate = g->vmstate; - setvmstate(g, GC); - lim = (GCSTEPSIZE/100) * g->gc.stepmul; - if (lim == 0) - lim = LJ_MAX_MEM; - if (g->gc.total > g->gc.threshold) - g->gc.debt += g->gc.total - g->gc.threshold; - do { - lim -= (GCSize)gc_onestep(L); - if (g->gc.state == GCSpause) { - g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; - g->vmstate = ostate; - return 1; /* Finished a GC cycle. */ - } - } while (sizeof(lim) == 8 ? ((int64_t)lim > 0) : ((int32_t)lim > 0)); - if (g->gc.debt < GCSTEPSIZE) { - g->gc.threshold = g->gc.total + GCSTEPSIZE; - g->vmstate = ostate; - return -1; - } else { - g->gc.debt -= GCSTEPSIZE; - g->gc.threshold = g->gc.total; - g->vmstate = ostate; - return 0; - } -} - -/* Ditto, but fix the stack top first. */ -void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L) -{ - if (curr_funcisL(L)) L->top = curr_topL(L); - lj_gc_step(L); -} - -#if LJ_HASJIT -/* Perform multiple GC steps. Called from JIT-compiled code. */ -int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps) -{ - lua_State *L = gco2th(gcref(g->cur_L)); - L->base = tvref(G(L)->jit_base); - L->top = curr_topL(L); - while (steps-- > 0 && lj_gc_step(L) == 0) - ; - /* Return 1 to force a trace exit. */ - return (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize); -} -#endif - -/* Perform a full GC cycle. */ -void lj_gc_fullgc(lua_State *L) -{ - global_State *g = G(L); - int32_t ostate = g->vmstate; - setvmstate(g, GC); - if (g->gc.state <= GCSatomic) { /* Caught somewhere in the middle. */ - setmref(g->gc.sweep, &g->gc.root); /* Sweep everything (preserving it). */ - setgcrefnull(g->gc.gray); /* Reset lists from partial propagation. */ - setgcrefnull(g->gc.grayagain); - setgcrefnull(g->gc.weak); - g->gc.state = GCSsweepstring; /* Fast forward to the sweep phase. */ - g->gc.sweepstr = 0; - } - while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep) - gc_onestep(L); /* Finish sweep. */ - lj_assertG(g->gc.state == GCSfinalize || g->gc.state == GCSpause, - "bad GC state"); - /* Now perform a full GC. */ - g->gc.state = GCSpause; - do { gc_onestep(L); } while (g->gc.state != GCSpause); - g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; - g->vmstate = ostate; -} - -/* -- Write barriers ------------------------------------------------------ */ - -/* Move the GC propagation frontier forward. */ -void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v) -{ - lj_assertG(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o), - "bad object states for forward barrier"); - lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause, - "bad GC state"); - lj_assertG(o->gch.gct != ~LJ_TTAB, "barrier object is not a table"); - /* Preserve invariant during propagation. Otherwise it doesn't matter. */ - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) - gc_mark(g, v); /* Move frontier forward. */ - else - makewhite(g, o); /* Make it white to avoid the following barrier. */ -} - -/* Specialized barrier for closed upvalue. Pass &uv->tv. */ -void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv) -{ -#define TV2MARKED(x) \ - (*((uint8_t *)(x) - offsetof(GCupval, tv) + offsetof(GCupval, marked))) - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) - gc_mark(g, gcV(tv)); - else - TV2MARKED(tv) = (TV2MARKED(tv) & (uint8_t)~LJ_GC_COLORS) | curwhite(g); -#undef TV2MARKED -} - -/* Close upvalue. Also needs a write barrier. */ -void lj_gc_closeuv(global_State *g, GCupval *uv) -{ - GCobj *o = obj2gco(uv); - /* Copy stack slot to upvalue itself and point to the copy. */ - copyTV(mainthread(g), &uv->tv, uvval(uv)); - setmref(uv->v, &uv->tv); - uv->closed = 1; - setgcrefr(o->gch.nextgc, g->gc.root); - setgcref(g->gc.root, o); - if (isgray(o)) { /* A closed upvalue is never gray, so fix this. */ - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) { - gray2black(o); /* Make it black and preserve invariant. */ - if (tviswhite(&uv->tv)) - lj_gc_barrierf(g, o, gcV(&uv->tv)); - } else { - makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */ - lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause, - "bad GC state"); - } - } -} - -#if LJ_HASJIT -/* Mark a trace if it's saved during the propagation phase. */ -void lj_gc_barriertrace(global_State *g, uint32_t traceno) -{ - if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) - gc_marktrace(g, traceno); -} -#endif - -/* -- Allocator ----------------------------------------------------------- */ - -/* Call pluggable memory allocator to allocate or resize a fragment. */ -void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz) -{ - global_State *g = G(L); - lj_assertG((osz == 0) == (p == NULL), "realloc API violation"); - p = g->allocf(g->allocd, p, osz, nsz); - if (p == NULL && nsz > 0) - lj_err_mem(L); - lj_assertG((nsz == 0) == (p == NULL), "allocf API violation"); - lj_assertG(checkptrGC(p), - "allocated memory address %p outside required range", p); - g->gc.total = (g->gc.total - osz) + nsz; - return p; -} - -/* Allocate new GC object and link it to the root set. */ -void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size) -{ - global_State *g = G(L); - GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size); - if (o == NULL) - lj_err_mem(L); - lj_assertG(checkptrGC(o), - "allocated memory address %p outside required range", o); - g->gc.total += size; - setgcrefr(o->gch.nextgc, g->gc.root); - setgcref(g->gc.root, o); - newwhite(g, o); - return o; -} - -/* Resize growable vector. */ -void *lj_mem_grow(lua_State *L, void *p, MSize *szp, MSize lim, MSize esz) -{ - MSize sz = (*szp) << 1; - if (sz < LJ_MIN_VECSZ) - sz = LJ_MIN_VECSZ; - if (sz > lim) - sz = lim; - p = lj_mem_realloc(L, p, (*szp)*esz, sz*esz); - *szp = sz; - return p; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.h deleted file mode 100644 index 0df7dee..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gc.h +++ /dev/null @@ -1,136 +0,0 @@ -/* -** Garbage collector. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_GC_H -#define _LJ_GC_H - -#include "lj_obj.h" - -/* Garbage collector states. Order matters. */ -enum { - GCSpause, GCSpropagate, GCSatomic, GCSsweepstring, GCSsweep, GCSfinalize -}; - -/* Bitmasks for marked field of GCobj. */ -#define LJ_GC_WHITE0 0x01 -#define LJ_GC_WHITE1 0x02 -#define LJ_GC_BLACK 0x04 -#define LJ_GC_FINALIZED 0x08 -#define LJ_GC_WEAKKEY 0x08 -#define LJ_GC_WEAKVAL 0x10 -#define LJ_GC_CDATA_FIN 0x10 -#define LJ_GC_FIXED 0x20 -#define LJ_GC_SFIXED 0x40 - -#define LJ_GC_WHITES (LJ_GC_WHITE0 | LJ_GC_WHITE1) -#define LJ_GC_COLORS (LJ_GC_WHITES | LJ_GC_BLACK) -#define LJ_GC_WEAK (LJ_GC_WEAKKEY | LJ_GC_WEAKVAL) - -/* Macros to test and set GCobj colors. */ -#define iswhite(x) ((x)->gch.marked & LJ_GC_WHITES) -#define isblack(x) ((x)->gch.marked & LJ_GC_BLACK) -#define isgray(x) (!((x)->gch.marked & (LJ_GC_BLACK|LJ_GC_WHITES))) -#define tviswhite(x) (tvisgcv(x) && iswhite(gcV(x))) -#define otherwhite(g) (g->gc.currentwhite ^ LJ_GC_WHITES) -#define isdead(g, v) ((v)->gch.marked & otherwhite(g) & LJ_GC_WHITES) - -#define curwhite(g) ((g)->gc.currentwhite & LJ_GC_WHITES) -#define newwhite(g, x) (obj2gco(x)->gch.marked = (uint8_t)curwhite(g)) -#define makewhite(g, x) \ - ((x)->gch.marked = ((x)->gch.marked & (uint8_t)~LJ_GC_COLORS) | curwhite(g)) -#define flipwhite(x) ((x)->gch.marked ^= LJ_GC_WHITES) -#define black2gray(x) ((x)->gch.marked &= (uint8_t)~LJ_GC_BLACK) -#define fixstring(s) ((s)->marked |= LJ_GC_FIXED) -#define markfinalized(x) ((x)->gch.marked |= LJ_GC_FINALIZED) - -/* Collector. */ -LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all); -LJ_FUNC void lj_gc_finalize_udata(lua_State *L); -#if LJ_HASFFI -LJ_FUNC void lj_gc_finalize_cdata(lua_State *L); -#else -#define lj_gc_finalize_cdata(L) UNUSED(L) -#endif -LJ_FUNC void lj_gc_freeall(global_State *g); -LJ_FUNCA int LJ_FASTCALL lj_gc_step(lua_State *L); -LJ_FUNCA void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L); -#if LJ_HASJIT -LJ_FUNC int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps); -#endif -LJ_FUNC void lj_gc_fullgc(lua_State *L); - -/* GC check: drive collector forward if the GC threshold has been reached. */ -#define lj_gc_check(L) \ - { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \ - lj_gc_step(L); } -#define lj_gc_check_fixtop(L) \ - { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \ - lj_gc_step_fixtop(L); } - -/* Write barriers. */ -LJ_FUNC void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v); -LJ_FUNCA void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv); -LJ_FUNC void lj_gc_closeuv(global_State *g, GCupval *uv); -#if LJ_HASJIT -LJ_FUNC void lj_gc_barriertrace(global_State *g, uint32_t traceno); -#endif - -/* Move the GC propagation frontier back for tables (make it gray again). */ -static LJ_AINLINE void lj_gc_barrierback(global_State *g, GCtab *t) -{ - GCobj *o = obj2gco(t); - lj_assertG(isblack(o) && !isdead(g, o), - "bad object states for backward barrier"); - lj_assertG(g->gc.state != GCSfinalize && g->gc.state != GCSpause, - "bad GC state"); - black2gray(o); - setgcrefr(t->gclist, g->gc.grayagain); - setgcref(g->gc.grayagain, o); -} - -/* Barrier for stores to table objects. TValue and GCobj variant. */ -#define lj_gc_anybarriert(L, t) \ - { if (LJ_UNLIKELY(isblack(obj2gco(t)))) lj_gc_barrierback(G(L), (t)); } -#define lj_gc_barriert(L, t, tv) \ - { if (tviswhite(tv) && isblack(obj2gco(t))) \ - lj_gc_barrierback(G(L), (t)); } -#define lj_gc_objbarriert(L, t, o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) \ - lj_gc_barrierback(G(L), (t)); } - -/* Barrier for stores to any other object. TValue and GCobj variant. */ -#define lj_gc_barrier(L, p, tv) \ - { if (tviswhite(tv) && isblack(obj2gco(p))) \ - lj_gc_barrierf(G(L), obj2gco(p), gcV(tv)); } -#define lj_gc_objbarrier(L, p, o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ - lj_gc_barrierf(G(L), obj2gco(p), obj2gco(o)); } - -/* Allocator. */ -LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz); -LJ_FUNC void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size); -LJ_FUNC void *lj_mem_grow(lua_State *L, void *p, - MSize *szp, MSize lim, MSize esz); - -#define lj_mem_new(L, s) lj_mem_realloc(L, NULL, 0, (s)) - -static LJ_AINLINE void lj_mem_free(global_State *g, void *p, size_t osize) -{ - g->gc.total -= (GCSize)osize; - g->allocf(g->allocd, p, osize, 0); -} - -#define lj_mem_newvec(L, n, t) ((t *)lj_mem_new(L, (GCSize)((n)*sizeof(t)))) -#define lj_mem_reallocvec(L, p, on, n, t) \ - ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (GCSize)((n)*sizeof(t)))) -#define lj_mem_growvec(L, p, n, m, t) \ - ((p) = (t *)lj_mem_grow(L, (p), &(n), (m), (MSize)sizeof(t))) -#define lj_mem_freevec(g, p, n, t) lj_mem_free(g, (p), (n)*sizeof(t)) - -#define lj_mem_newobj(L, t) ((t *)lj_mem_newgco(L, sizeof(t))) -#define lj_mem_newt(L, s, t) ((t *)lj_mem_new(L, (s))) -#define lj_mem_freet(g, p) lj_mem_free(g, (p), sizeof(*(p))) - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.c deleted file mode 100644 index c50d0d4..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.c +++ /dev/null @@ -1,818 +0,0 @@ -/* -** Client for the GDB JIT API. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_gdbjit_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_frame.h" -#include "lj_buf.h" -#include "lj_strfmt.h" -#include "lj_jit.h" -#include "lj_dispatch.h" - -/* This is not compiled in by default. -** Enable with -DLUAJIT_USE_GDBJIT in the Makefile and recompile everything. -*/ -#ifdef LUAJIT_USE_GDBJIT - -/* The GDB JIT API allows JIT compilers to pass debug information about -** JIT-compiled code back to GDB. You need at least GDB 7.0 or higher -** to see it in action. -** -** This is a passive API, so it works even when not running under GDB -** or when attaching to an already running process. Alas, this implies -** enabling it always has a non-negligible overhead -- do not use in -** release mode! -** -** The LuaJIT GDB JIT client is rather minimal at the moment. It gives -** each trace a symbol name and adds a source location and frame unwind -** information. Obviously LuaJIT itself and any embedding C application -** should be compiled with debug symbols, too (see the Makefile). -** -** Traces are named TRACE_1, TRACE_2, ... these correspond to the trace -** numbers from -jv or -jdump. Use "break TRACE_1" or "tbreak TRACE_1" etc. -** to set breakpoints on specific traces (even ahead of their creation). -** -** The source location for each trace allows listing the corresponding -** source lines with the GDB command "list" (but only if the Lua source -** has been loaded from a file). Currently this is always set to the -** location where the trace has been started. -** -** Frame unwind information can be inspected with the GDB command -** "info frame". This also allows proper backtraces across JIT-compiled -** code with the GDB command "bt". -** -** You probably want to add the following settings to a .gdbinit file -** (or add them to ~/.gdbinit): -** set disassembly-flavor intel -** set breakpoint pending on -** -** Here's a sample GDB session: -** ------------------------------------------------------------------------ - -$ cat >x.lua -for outer=1,100 do - for inner=1,100 do end -end -^D - -$ luajit -jv x.lua -[TRACE 1 x.lua:2] -[TRACE 2 (1/3) x.lua:1 -> 1] - -$ gdb --quiet --args luajit x.lua -(gdb) tbreak TRACE_1 -Function "TRACE_1" not defined. -Temporary breakpoint 1 (TRACE_1) pending. -(gdb) run -Starting program: luajit x.lua - -Temporary breakpoint 1, TRACE_1 () at x.lua:2 -2 for inner=1,100 do end -(gdb) list -1 for outer=1,100 do -2 for inner=1,100 do end -3 end -(gdb) bt -#0 TRACE_1 () at x.lua:2 -#1 0x08053690 in lua_pcall [...] -[...] -#7 0x0806ff90 in main [...] -(gdb) disass TRACE_1 -Dump of assembler code for function TRACE_1: -0xf7fd9fba : mov DWORD PTR ds:0xf7e0e2a0,0x1 -0xf7fd9fc4 : movsd xmm7,QWORD PTR [edx+0x20] -[...] -0xf7fd9ff8 : jmp 0xf7fd2014 -End of assembler dump. -(gdb) tbreak TRACE_2 -Function "TRACE_2" not defined. -Temporary breakpoint 2 (TRACE_2) pending. -(gdb) cont -Continuing. - -Temporary breakpoint 2, TRACE_2 () at x.lua:1 -1 for outer=1,100 do -(gdb) info frame -Stack level 0, frame at 0xffffd7c0: - eip = 0xf7fd9f60 in TRACE_2 (x.lua:1); saved eip 0x8053690 - called by frame at 0xffffd7e0 - source language unknown. - Arglist at 0xffffd78c, args: - Locals at 0xffffd78c, Previous frame's sp is 0xffffd7c0 - Saved registers: - ebx at 0xffffd7ac, ebp at 0xffffd7b8, esi at 0xffffd7b0, edi at 0xffffd7b4, - eip at 0xffffd7bc -(gdb) - -** ------------------------------------------------------------------------ -*/ - -/* -- GDB JIT API --------------------------------------------------------- */ - -/* GDB JIT actions. */ -enum { - GDBJIT_NOACTION = 0, - GDBJIT_REGISTER, - GDBJIT_UNREGISTER -}; - -/* GDB JIT entry. */ -typedef struct GDBJITentry { - struct GDBJITentry *next_entry; - struct GDBJITentry *prev_entry; - const char *symfile_addr; - uint64_t symfile_size; -} GDBJITentry; - -/* GDB JIT descriptor. */ -typedef struct GDBJITdesc { - uint32_t version; - uint32_t action_flag; - GDBJITentry *relevant_entry; - GDBJITentry *first_entry; -} GDBJITdesc; - -GDBJITdesc __jit_debug_descriptor = { - 1, GDBJIT_NOACTION, NULL, NULL -}; - -/* GDB sets a breakpoint at this function. */ -void LJ_NOINLINE __jit_debug_register_code() -{ - __asm__ __volatile__(""); -}; - -/* -- In-memory ELF object definitions ------------------------------------ */ - -/* ELF definitions. */ -typedef struct ELFheader { - uint8_t emagic[4]; - uint8_t eclass; - uint8_t eendian; - uint8_t eversion; - uint8_t eosabi; - uint8_t eabiversion; - uint8_t epad[7]; - uint16_t type; - uint16_t machine; - uint32_t version; - uintptr_t entry; - uintptr_t phofs; - uintptr_t shofs; - uint32_t flags; - uint16_t ehsize; - uint16_t phentsize; - uint16_t phnum; - uint16_t shentsize; - uint16_t shnum; - uint16_t shstridx; -} ELFheader; - -typedef struct ELFsectheader { - uint32_t name; - uint32_t type; - uintptr_t flags; - uintptr_t addr; - uintptr_t ofs; - uintptr_t size; - uint32_t link; - uint32_t info; - uintptr_t align; - uintptr_t entsize; -} ELFsectheader; - -#define ELFSECT_IDX_ABS 0xfff1 - -enum { - ELFSECT_TYPE_PROGBITS = 1, - ELFSECT_TYPE_SYMTAB = 2, - ELFSECT_TYPE_STRTAB = 3, - ELFSECT_TYPE_NOBITS = 8 -}; - -#define ELFSECT_FLAGS_WRITE 1 -#define ELFSECT_FLAGS_ALLOC 2 -#define ELFSECT_FLAGS_EXEC 4 - -typedef struct ELFsymbol { -#if LJ_64 - uint32_t name; - uint8_t info; - uint8_t other; - uint16_t sectidx; - uintptr_t value; - uint64_t size; -#else - uint32_t name; - uintptr_t value; - uint32_t size; - uint8_t info; - uint8_t other; - uint16_t sectidx; -#endif -} ELFsymbol; - -enum { - ELFSYM_TYPE_FUNC = 2, - ELFSYM_TYPE_FILE = 4, - ELFSYM_BIND_LOCAL = 0 << 4, - ELFSYM_BIND_GLOBAL = 1 << 4, -}; - -/* DWARF definitions. */ -#define DW_CIE_VERSION 1 - -enum { - DW_CFA_nop = 0x0, - DW_CFA_offset_extended = 0x5, - DW_CFA_def_cfa = 0xc, - DW_CFA_def_cfa_offset = 0xe, - DW_CFA_offset_extended_sf = 0x11, - DW_CFA_advance_loc = 0x40, - DW_CFA_offset = 0x80 -}; - -enum { - DW_EH_PE_udata4 = 3, - DW_EH_PE_textrel = 0x20 -}; - -enum { - DW_TAG_compile_unit = 0x11 -}; - -enum { - DW_children_no = 0, - DW_children_yes = 1 -}; - -enum { - DW_AT_name = 0x03, - DW_AT_stmt_list = 0x10, - DW_AT_low_pc = 0x11, - DW_AT_high_pc = 0x12 -}; - -enum { - DW_FORM_addr = 0x01, - DW_FORM_data4 = 0x06, - DW_FORM_string = 0x08 -}; - -enum { - DW_LNS_extended_op = 0, - DW_LNS_copy = 1, - DW_LNS_advance_pc = 2, - DW_LNS_advance_line = 3 -}; - -enum { - DW_LNE_end_sequence = 1, - DW_LNE_set_address = 2 -}; - -enum { -#if LJ_TARGET_X86 - DW_REG_AX, DW_REG_CX, DW_REG_DX, DW_REG_BX, - DW_REG_SP, DW_REG_BP, DW_REG_SI, DW_REG_DI, - DW_REG_RA, -#elif LJ_TARGET_X64 - /* Yes, the order is strange, but correct. */ - DW_REG_AX, DW_REG_DX, DW_REG_CX, DW_REG_BX, - DW_REG_SI, DW_REG_DI, DW_REG_BP, DW_REG_SP, - DW_REG_8, DW_REG_9, DW_REG_10, DW_REG_11, - DW_REG_12, DW_REG_13, DW_REG_14, DW_REG_15, - DW_REG_RA, -#elif LJ_TARGET_ARM - DW_REG_SP = 13, - DW_REG_RA = 14, -#elif LJ_TARGET_ARM64 - DW_REG_SP = 31, - DW_REG_RA = 30, -#elif LJ_TARGET_PPC - DW_REG_SP = 1, - DW_REG_RA = 65, - DW_REG_CR = 70, -#elif LJ_TARGET_MIPS - DW_REG_SP = 29, - DW_REG_RA = 31, -#else -#error "Unsupported target architecture" -#endif -}; - -/* Minimal list of sections for the in-memory ELF object. */ -enum { - GDBJIT_SECT_NULL, - GDBJIT_SECT_text, - GDBJIT_SECT_eh_frame, - GDBJIT_SECT_shstrtab, - GDBJIT_SECT_strtab, - GDBJIT_SECT_symtab, - GDBJIT_SECT_debug_info, - GDBJIT_SECT_debug_abbrev, - GDBJIT_SECT_debug_line, - GDBJIT_SECT__MAX -}; - -enum { - GDBJIT_SYM_UNDEF, - GDBJIT_SYM_FILE, - GDBJIT_SYM_FUNC, - GDBJIT_SYM__MAX -}; - -/* In-memory ELF object. */ -typedef struct GDBJITobj { - ELFheader hdr; /* ELF header. */ - ELFsectheader sect[GDBJIT_SECT__MAX]; /* ELF sections. */ - ELFsymbol sym[GDBJIT_SYM__MAX]; /* ELF symbol table. */ - uint8_t space[4096]; /* Space for various section data. */ -} GDBJITobj; - -/* Combined structure for GDB JIT entry and ELF object. */ -typedef struct GDBJITentryobj { - GDBJITentry entry; - size_t sz; - GDBJITobj obj; -} GDBJITentryobj; - -/* Template for in-memory ELF header. */ -static const ELFheader elfhdr_template = { - .emagic = { 0x7f, 'E', 'L', 'F' }, - .eclass = LJ_64 ? 2 : 1, - .eendian = LJ_ENDIAN_SELECT(1, 2), - .eversion = 1, -#if LJ_TARGET_LINUX - .eosabi = 0, /* Nope, it's not 3. */ -#elif defined(__FreeBSD__) - .eosabi = 9, -#elif defined(__NetBSD__) - .eosabi = 2, -#elif defined(__OpenBSD__) - .eosabi = 12, -#elif defined(__DragonFly__) - .eosabi = 0, -#elif LJ_TARGET_SOLARIS - .eosabi = 6, -#else - .eosabi = 0, -#endif - .eabiversion = 0, - .epad = { 0, 0, 0, 0, 0, 0, 0 }, - .type = 1, -#if LJ_TARGET_X86 - .machine = 3, -#elif LJ_TARGET_X64 - .machine = 62, -#elif LJ_TARGET_ARM - .machine = 40, -#elif LJ_TARGET_ARM64 - .machine = 183, -#elif LJ_TARGET_PPC - .machine = 20, -#elif LJ_TARGET_MIPS - .machine = 8, -#else -#error "Unsupported target architecture" -#endif - .version = 1, - .entry = 0, - .phofs = 0, - .shofs = offsetof(GDBJITobj, sect), - .flags = 0, - .ehsize = sizeof(ELFheader), - .phentsize = 0, - .phnum = 0, - .shentsize = sizeof(ELFsectheader), - .shnum = GDBJIT_SECT__MAX, - .shstridx = GDBJIT_SECT_shstrtab -}; - -/* -- In-memory ELF object generation ------------------------------------- */ - -/* Context for generating the ELF object for the GDB JIT API. */ -typedef struct GDBJITctx { - uint8_t *p; /* Pointer to next address in obj.space. */ - uint8_t *startp; /* Pointer to start address in obj.space. */ - GCtrace *T; /* Generate symbols for this trace. */ - uintptr_t mcaddr; /* Machine code address. */ - MSize szmcode; /* Size of machine code. */ - MSize spadjp; /* Stack adjustment for parent trace or interpreter. */ - MSize spadj; /* Stack adjustment for trace itself. */ - BCLine lineno; /* Starting line number. */ - const char *filename; /* Starting file name. */ - size_t objsize; /* Final size of ELF object. */ - GDBJITobj obj; /* In-memory ELF object. */ -} GDBJITctx; - -/* Add a zero-terminated string. */ -static uint32_t gdbjit_strz(GDBJITctx *ctx, const char *str) -{ - uint8_t *p = ctx->p; - uint32_t ofs = (uint32_t)(p - ctx->startp); - do { - *p++ = (uint8_t)*str; - } while (*str++); - ctx->p = p; - return ofs; -} - -/* Append a decimal number. */ -static void gdbjit_catnum(GDBJITctx *ctx, uint32_t n) -{ - if (n >= 10) { uint32_t m = n / 10; n = n % 10; gdbjit_catnum(ctx, m); } - *ctx->p++ = '0' + n; -} - -/* Add a SLEB128 value. */ -static void gdbjit_sleb128(GDBJITctx *ctx, int32_t v) -{ - uint8_t *p = ctx->p; - for (; (uint32_t)(v+0x40) >= 0x80; v >>= 7) - *p++ = (uint8_t)((v & 0x7f) | 0x80); - *p++ = (uint8_t)(v & 0x7f); - ctx->p = p; -} - -/* Shortcuts to generate DWARF structures. */ -#define DB(x) (*p++ = (x)) -#define DI8(x) (*(int8_t *)p = (x), p++) -#define DU16(x) (*(uint16_t *)p = (x), p += 2) -#define DU32(x) (*(uint32_t *)p = (x), p += 4) -#define DADDR(x) (*(uintptr_t *)p = (x), p += sizeof(uintptr_t)) -#define DUV(x) (p = (uint8_t *)lj_strfmt_wuleb128((char *)p, (x))) -#define DSV(x) (ctx->p = p, gdbjit_sleb128(ctx, (x)), p = ctx->p) -#define DSTR(str) (ctx->p = p, gdbjit_strz(ctx, (str)), p = ctx->p) -#define DALIGNNOP(s) while ((uintptr_t)p & ((s)-1)) *p++ = DW_CFA_nop -#define DSECT(name, stmt) \ - { uint32_t *szp_##name = (uint32_t *)p; p += 4; stmt \ - *szp_##name = (uint32_t)((p-(uint8_t *)szp_##name)-4); } \ - -/* Initialize ELF section headers. */ -static void LJ_FASTCALL gdbjit_secthdr(GDBJITctx *ctx) -{ - ELFsectheader *sect; - - *ctx->p++ = '\0'; /* Empty string at start of string table. */ - -#define SECTDEF(id, tp, al) \ - sect = &ctx->obj.sect[GDBJIT_SECT_##id]; \ - sect->name = gdbjit_strz(ctx, "." #id); \ - sect->type = ELFSECT_TYPE_##tp; \ - sect->align = (al) - - SECTDEF(text, NOBITS, 16); - sect->flags = ELFSECT_FLAGS_ALLOC|ELFSECT_FLAGS_EXEC; - sect->addr = ctx->mcaddr; - sect->ofs = 0; - sect->size = ctx->szmcode; - - SECTDEF(eh_frame, PROGBITS, sizeof(uintptr_t)); - sect->flags = ELFSECT_FLAGS_ALLOC; - - SECTDEF(shstrtab, STRTAB, 1); - SECTDEF(strtab, STRTAB, 1); - - SECTDEF(symtab, SYMTAB, sizeof(uintptr_t)); - sect->ofs = offsetof(GDBJITobj, sym); - sect->size = sizeof(ctx->obj.sym); - sect->link = GDBJIT_SECT_strtab; - sect->entsize = sizeof(ELFsymbol); - sect->info = GDBJIT_SYM_FUNC; - - SECTDEF(debug_info, PROGBITS, 1); - SECTDEF(debug_abbrev, PROGBITS, 1); - SECTDEF(debug_line, PROGBITS, 1); - -#undef SECTDEF -} - -/* Initialize symbol table. */ -static void LJ_FASTCALL gdbjit_symtab(GDBJITctx *ctx) -{ - ELFsymbol *sym; - - *ctx->p++ = '\0'; /* Empty string at start of string table. */ - - sym = &ctx->obj.sym[GDBJIT_SYM_FILE]; - sym->name = gdbjit_strz(ctx, "JIT mcode"); - sym->sectidx = ELFSECT_IDX_ABS; - sym->info = ELFSYM_TYPE_FILE|ELFSYM_BIND_LOCAL; - - sym = &ctx->obj.sym[GDBJIT_SYM_FUNC]; - sym->name = gdbjit_strz(ctx, "TRACE_"); ctx->p--; - gdbjit_catnum(ctx, ctx->T->traceno); *ctx->p++ = '\0'; - sym->sectidx = GDBJIT_SECT_text; - sym->value = 0; - sym->size = ctx->szmcode; - sym->info = ELFSYM_TYPE_FUNC|ELFSYM_BIND_GLOBAL; -} - -/* Initialize .eh_frame section. */ -static void LJ_FASTCALL gdbjit_ehframe(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - uint8_t *framep = p; - - /* Emit DWARF EH CIE. */ - DSECT(CIE, - DU32(0); /* Offset to CIE itself. */ - DB(DW_CIE_VERSION); - DSTR("zR"); /* Augmentation. */ - DUV(1); /* Code alignment factor. */ - DSV(-(int32_t)sizeof(uintptr_t)); /* Data alignment factor. */ - DB(DW_REG_RA); /* Return address register. */ - DB(1); DB(DW_EH_PE_textrel|DW_EH_PE_udata4); /* Augmentation data. */ - DB(DW_CFA_def_cfa); DUV(DW_REG_SP); DUV(sizeof(uintptr_t)); -#if LJ_TARGET_PPC - DB(DW_CFA_offset_extended_sf); DB(DW_REG_RA); DSV(-1); -#else - DB(DW_CFA_offset|DW_REG_RA); DUV(1); -#endif - DALIGNNOP(sizeof(uintptr_t)); - ) - - /* Emit DWARF EH FDE. */ - DSECT(FDE, - DU32((uint32_t)(p-framep)); /* Offset to CIE. */ - DU32(0); /* Machine code offset relative to .text. */ - DU32(ctx->szmcode); /* Machine code length. */ - DB(0); /* Augmentation data. */ - /* Registers saved in CFRAME. */ -#if LJ_TARGET_X86 - DB(DW_CFA_offset|DW_REG_BP); DUV(2); - DB(DW_CFA_offset|DW_REG_DI); DUV(3); - DB(DW_CFA_offset|DW_REG_SI); DUV(4); - DB(DW_CFA_offset|DW_REG_BX); DUV(5); -#elif LJ_TARGET_X64 - DB(DW_CFA_offset|DW_REG_BP); DUV(2); - DB(DW_CFA_offset|DW_REG_BX); DUV(3); - DB(DW_CFA_offset|DW_REG_15); DUV(4); - DB(DW_CFA_offset|DW_REG_14); DUV(5); - /* Extra registers saved for JIT-compiled code. */ - DB(DW_CFA_offset|DW_REG_13); DUV(LJ_GC64 ? 10 : 9); - DB(DW_CFA_offset|DW_REG_12); DUV(LJ_GC64 ? 11 : 10); -#elif LJ_TARGET_ARM - { - int i; - for (i = 11; i >= 4; i--) { DB(DW_CFA_offset|i); DUV(2+(11-i)); } - } -#elif LJ_TARGET_ARM64 - { - int i; - DB(DW_CFA_offset|31); DUV(2); - for (i = 28; i >= 19; i--) { DB(DW_CFA_offset|i); DUV(3+(28-i)); } - for (i = 15; i >= 8; i--) { DB(DW_CFA_offset|32|i); DUV(28-i); } - } -#elif LJ_TARGET_PPC - { - int i; - DB(DW_CFA_offset_extended); DB(DW_REG_CR); DUV(55); - for (i = 14; i <= 31; i++) { - DB(DW_CFA_offset|i); DUV(37+(31-i)); - DB(DW_CFA_offset|32|i); DUV(2+2*(31-i)); - } - } -#elif LJ_TARGET_MIPS - { - int i; - DB(DW_CFA_offset|30); DUV(2); - for (i = 23; i >= 16; i--) { DB(DW_CFA_offset|i); DUV(26-i); } - for (i = 30; i >= 20; i -= 2) { DB(DW_CFA_offset|32|i); DUV(42-i); } - } -#else -#error "Unsupported target architecture" -#endif - if (ctx->spadjp != ctx->spadj) { /* Parent/interpreter stack frame size. */ - DB(DW_CFA_def_cfa_offset); DUV(ctx->spadjp); - DB(DW_CFA_advance_loc|1); /* Only an approximation. */ - } - DB(DW_CFA_def_cfa_offset); DUV(ctx->spadj); /* Trace stack frame size. */ - DALIGNNOP(sizeof(uintptr_t)); - ) - - ctx->p = p; -} - -/* Initialize .debug_info section. */ -static void LJ_FASTCALL gdbjit_debuginfo(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - - DSECT(info, - DU16(2); /* DWARF version. */ - DU32(0); /* Abbrev offset. */ - DB(sizeof(uintptr_t)); /* Pointer size. */ - - DUV(1); /* Abbrev #1: DW_TAG_compile_unit. */ - DSTR(ctx->filename); /* DW_AT_name. */ - DADDR(ctx->mcaddr); /* DW_AT_low_pc. */ - DADDR(ctx->mcaddr + ctx->szmcode); /* DW_AT_high_pc. */ - DU32(0); /* DW_AT_stmt_list. */ - ) - - ctx->p = p; -} - -/* Initialize .debug_abbrev section. */ -static void LJ_FASTCALL gdbjit_debugabbrev(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - - /* Abbrev #1: DW_TAG_compile_unit. */ - DUV(1); DUV(DW_TAG_compile_unit); - DB(DW_children_no); - DUV(DW_AT_name); DUV(DW_FORM_string); - DUV(DW_AT_low_pc); DUV(DW_FORM_addr); - DUV(DW_AT_high_pc); DUV(DW_FORM_addr); - DUV(DW_AT_stmt_list); DUV(DW_FORM_data4); - DB(0); DB(0); - - ctx->p = p; -} - -#define DLNE(op, s) (DB(DW_LNS_extended_op), DUV(1+(s)), DB((op))) - -/* Initialize .debug_line section. */ -static void LJ_FASTCALL gdbjit_debugline(GDBJITctx *ctx) -{ - uint8_t *p = ctx->p; - - DSECT(line, - DU16(2); /* DWARF version. */ - DSECT(header, - DB(1); /* Minimum instruction length. */ - DB(1); /* is_stmt. */ - DI8(0); /* Line base for special opcodes. */ - DB(2); /* Line range for special opcodes. */ - DB(3+1); /* Opcode base at DW_LNS_advance_line+1. */ - DB(0); DB(1); DB(1); /* Standard opcode lengths. */ - /* Directory table. */ - DB(0); - /* File name table. */ - DSTR(ctx->filename); DUV(0); DUV(0); DUV(0); - DB(0); - ) - - DLNE(DW_LNE_set_address, sizeof(uintptr_t)); DADDR(ctx->mcaddr); - if (ctx->lineno) { - DB(DW_LNS_advance_line); DSV(ctx->lineno-1); - } - DB(DW_LNS_copy); - DB(DW_LNS_advance_pc); DUV(ctx->szmcode); - DLNE(DW_LNE_end_sequence, 0); - ) - - ctx->p = p; -} - -#undef DLNE - -/* Undef shortcuts. */ -#undef DB -#undef DI8 -#undef DU16 -#undef DU32 -#undef DADDR -#undef DUV -#undef DSV -#undef DSTR -#undef DALIGNNOP -#undef DSECT - -/* Type of a section initializer callback. */ -typedef void (LJ_FASTCALL *GDBJITinitf)(GDBJITctx *ctx); - -/* Call section initializer and set the section offset and size. */ -static void gdbjit_initsect(GDBJITctx *ctx, int sect, GDBJITinitf initf) -{ - ctx->startp = ctx->p; - ctx->obj.sect[sect].ofs = (uintptr_t)((char *)ctx->p - (char *)&ctx->obj); - initf(ctx); - ctx->obj.sect[sect].size = (uintptr_t)(ctx->p - ctx->startp); -} - -#define SECTALIGN(p, a) \ - ((p) = (uint8_t *)(((uintptr_t)(p) + ((a)-1)) & ~(uintptr_t)((a)-1))) - -/* Build in-memory ELF object. */ -static void gdbjit_buildobj(GDBJITctx *ctx) -{ - GDBJITobj *obj = &ctx->obj; - /* Fill in ELF header and clear structures. */ - memcpy(&obj->hdr, &elfhdr_template, sizeof(ELFheader)); - memset(&obj->sect, 0, sizeof(ELFsectheader)*GDBJIT_SECT__MAX); - memset(&obj->sym, 0, sizeof(ELFsymbol)*GDBJIT_SYM__MAX); - /* Initialize sections. */ - ctx->p = obj->space; - gdbjit_initsect(ctx, GDBJIT_SECT_shstrtab, gdbjit_secthdr); - gdbjit_initsect(ctx, GDBJIT_SECT_strtab, gdbjit_symtab); - gdbjit_initsect(ctx, GDBJIT_SECT_debug_info, gdbjit_debuginfo); - gdbjit_initsect(ctx, GDBJIT_SECT_debug_abbrev, gdbjit_debugabbrev); - gdbjit_initsect(ctx, GDBJIT_SECT_debug_line, gdbjit_debugline); - SECTALIGN(ctx->p, sizeof(uintptr_t)); - gdbjit_initsect(ctx, GDBJIT_SECT_eh_frame, gdbjit_ehframe); - ctx->objsize = (size_t)((char *)ctx->p - (char *)obj); - lj_assertX(ctx->objsize < sizeof(GDBJITobj), "GDBJITobj overflow"); -} - -#undef SECTALIGN - -/* -- Interface to GDB JIT API -------------------------------------------- */ - -static int gdbjit_lock; - -static void gdbjit_lock_acquire() -{ - while (__sync_lock_test_and_set(&gdbjit_lock, 1)) { - /* Just spin; futexes or pthreads aren't worth the portability cost. */ - } -} - -static void gdbjit_lock_release() -{ - __sync_lock_release(&gdbjit_lock); -} - -/* Add new entry to GDB JIT symbol chain. */ -static void gdbjit_newentry(lua_State *L, GDBJITctx *ctx) -{ - /* Allocate memory for GDB JIT entry and ELF object. */ - MSize sz = (MSize)(sizeof(GDBJITentryobj) - sizeof(GDBJITobj) + ctx->objsize); - GDBJITentryobj *eo = lj_mem_newt(L, sz, GDBJITentryobj); - memcpy(&eo->obj, &ctx->obj, ctx->objsize); /* Copy ELF object. */ - eo->sz = sz; - ctx->T->gdbjit_entry = (void *)eo; - /* Link new entry to chain and register it. */ - eo->entry.prev_entry = NULL; - gdbjit_lock_acquire(); - eo->entry.next_entry = __jit_debug_descriptor.first_entry; - if (eo->entry.next_entry) - eo->entry.next_entry->prev_entry = &eo->entry; - eo->entry.symfile_addr = (const char *)&eo->obj; - eo->entry.symfile_size = ctx->objsize; - __jit_debug_descriptor.first_entry = &eo->entry; - __jit_debug_descriptor.relevant_entry = &eo->entry; - __jit_debug_descriptor.action_flag = GDBJIT_REGISTER; - __jit_debug_register_code(); - gdbjit_lock_release(); -} - -/* Add debug info for newly compiled trace and notify GDB. */ -void lj_gdbjit_addtrace(jit_State *J, GCtrace *T) -{ - GDBJITctx ctx; - GCproto *pt = &gcref(T->startpt)->pt; - TraceNo parent = T->ir[REF_BASE].op1; - const BCIns *startpc = mref(T->startpc, const BCIns); - ctx.T = T; - ctx.mcaddr = (uintptr_t)T->mcode; - ctx.szmcode = T->szmcode; - ctx.spadjp = CFRAME_SIZE_JIT + - (MSize)(parent ? traceref(J, parent)->spadjust : 0); - ctx.spadj = CFRAME_SIZE_JIT + T->spadjust; - lj_assertJ(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc, - "start PC out of range"); - ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); - ctx.filename = proto_chunknamestr(pt); - if (*ctx.filename == '@' || *ctx.filename == '=') - ctx.filename++; - else - ctx.filename = "(string)"; - gdbjit_buildobj(&ctx); - gdbjit_newentry(J->L, &ctx); -} - -/* Delete debug info for trace and notify GDB. */ -void lj_gdbjit_deltrace(jit_State *J, GCtrace *T) -{ - GDBJITentryobj *eo = (GDBJITentryobj *)T->gdbjit_entry; - if (eo) { - gdbjit_lock_acquire(); - if (eo->entry.prev_entry) - eo->entry.prev_entry->next_entry = eo->entry.next_entry; - else - __jit_debug_descriptor.first_entry = eo->entry.next_entry; - if (eo->entry.next_entry) - eo->entry.next_entry->prev_entry = eo->entry.prev_entry; - __jit_debug_descriptor.relevant_entry = &eo->entry; - __jit_debug_descriptor.action_flag = GDBJIT_UNREGISTER; - __jit_debug_register_code(); - gdbjit_lock_release(); - lj_mem_free(J2G(J), eo, eo->sz); - } -} - -#endif -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.h deleted file mode 100644 index f531be6..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_gdbjit.h +++ /dev/null @@ -1,22 +0,0 @@ -/* -** Client for the GDB JIT API. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_GDBJIT_H -#define _LJ_GDBJIT_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT && defined(LUAJIT_USE_GDBJIT) - -LJ_FUNC void lj_gdbjit_addtrace(jit_State *J, GCtrace *T); -LJ_FUNC void lj_gdbjit_deltrace(jit_State *J, GCtrace *T); - -#else -#define lj_gdbjit_addtrace(J, T) UNUSED(T) -#define lj_gdbjit_deltrace(J, T) UNUSED(T) -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.c deleted file mode 100644 index 6590151..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.c +++ /dev/null @@ -1,500 +0,0 @@ -/* -** SSA IR (Intermediate Representation) emitter. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_ir_c -#define LUA_CORE - -/* For pointers to libc/libm functions. */ -#include -#include - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lj_carith.h" -#endif -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_serialize.h" -#include "lj_strfmt.h" -#include "lj_prng.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* -- IR tables ----------------------------------------------------------- */ - -/* IR instruction modes. */ -LJ_DATADEF const uint8_t lj_ir_mode[IR__MAX+1] = { -IRDEF(IRMODE) - 0 -}; - -/* IR type sizes. */ -LJ_DATADEF const uint8_t lj_ir_type_size[IRT__MAX+1] = { -#define IRTSIZE(name, size) size, -IRTDEF(IRTSIZE) -#undef IRTSIZE - 0 -}; - -/* C call info for CALL* instructions. */ -LJ_DATADEF const CCallInfo lj_ir_callinfo[] = { -#define IRCALLCI(cond, name, nargs, kind, type, flags) \ - { (ASMFunction)IRCALLCOND_##cond(name), \ - (nargs)|(CCI_CALL_##kind)|(IRT_##type<irbuf + J->irbotlim; - MSize szins = J->irtoplim - J->irbotlim; - if (szins) { - baseir = (IRIns *)lj_mem_realloc(J->L, baseir, szins*sizeof(IRIns), - 2*szins*sizeof(IRIns)); - J->irtoplim = J->irbotlim + 2*szins; - } else { - baseir = (IRIns *)lj_mem_realloc(J->L, NULL, 0, LJ_MIN_IRSZ*sizeof(IRIns)); - J->irbotlim = REF_BASE - LJ_MIN_IRSZ/4; - J->irtoplim = J->irbotlim + LJ_MIN_IRSZ; - } - J->cur.ir = J->irbuf = baseir - J->irbotlim; -} - -/* Grow IR buffer at the bottom or shift it up. */ -static void lj_ir_growbot(jit_State *J) -{ - IRIns *baseir = J->irbuf + J->irbotlim; - MSize szins = J->irtoplim - J->irbotlim; - lj_assertJ(szins != 0, "zero IR size"); - lj_assertJ(J->cur.nk == J->irbotlim || J->cur.nk-1 == J->irbotlim, - "unexpected IR growth"); - if (J->cur.nins + (szins >> 1) < J->irtoplim) { - /* More than half of the buffer is free on top: shift up by a quarter. */ - MSize ofs = szins >> 2; - memmove(baseir + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns)); - J->irbotlim -= ofs; - J->irtoplim -= ofs; - J->cur.ir = J->irbuf = baseir - J->irbotlim; - } else { - /* Double the buffer size, but split the growth amongst top/bottom. */ - IRIns *newbase = lj_mem_newt(J->L, 2*szins*sizeof(IRIns), IRIns); - MSize ofs = szins >= 256 ? 128 : (szins >> 1); /* Limit bottom growth. */ - memcpy(newbase + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns)); - lj_mem_free(G(J->L), baseir, szins*sizeof(IRIns)); - J->irbotlim -= ofs; - J->irtoplim = J->irbotlim + 2*szins; - J->cur.ir = J->irbuf = newbase - J->irbotlim; - } -} - -/* Emit IR without any optimizations. */ -TRef LJ_FASTCALL lj_ir_emit(jit_State *J) -{ - IRRef ref = lj_ir_nextins(J); - IRIns *ir = IR(ref); - IROp op = fins->o; - ir->prev = J->chain[op]; - J->chain[op] = (IRRef1)ref; - ir->o = op; - ir->op1 = fins->op1; - ir->op2 = fins->op2; - J->guardemit.irt |= fins->t.irt; - return TREF(ref, irt_t((ir->t = fins->t))); -} - -/* Emit call to a C function. */ -TRef lj_ir_call(jit_State *J, IRCallID id, ...) -{ - const CCallInfo *ci = &lj_ir_callinfo[id]; - uint32_t n = CCI_NARGS(ci); - TRef tr = TREF_NIL; - va_list argp; - va_start(argp, id); - if ((ci->flags & CCI_L)) n--; - if (n > 0) - tr = va_arg(argp, IRRef); - while (n-- > 1) - tr = emitir(IRT(IR_CARG, IRT_NIL), tr, va_arg(argp, IRRef)); - va_end(argp); - if (CCI_OP(ci) == IR_CALLS) - J->needsnap = 1; /* Need snapshot after call with side effect. */ - return emitir(CCI_OPTYPE(ci), tr, id); -} - -/* Load field of type t from GG_State + offset. Must be 32 bit aligned. */ -TRef lj_ir_ggfload(jit_State *J, IRType t, uintptr_t ofs) -{ - lj_assertJ((ofs & 3) == 0, "unaligned GG_State field offset"); - ofs >>= 2; - lj_assertJ(ofs >= IRFL__MAX && ofs <= 0x3ff, - "GG_State field offset breaks 10 bit FOLD key limit"); - lj_ir_set(J, IRT(IR_FLOAD, t), REF_NIL, ofs); - return lj_opt_fold(J); -} - -/* -- Interning of constants ---------------------------------------------- */ - -/* -** IR instructions for constants are kept between J->cur.nk >= ref < REF_BIAS. -** They are chained like all other instructions, but grow downwards. -** The are interned (like strings in the VM) to facilitate reference -** comparisons. The same constant must get the same reference. -*/ - -/* Get ref of next IR constant and optionally grow IR. -** Note: this may invalidate all IRIns *! -*/ -static LJ_AINLINE IRRef ir_nextk(jit_State *J) -{ - IRRef ref = J->cur.nk; - if (LJ_UNLIKELY(ref <= J->irbotlim)) lj_ir_growbot(J); - J->cur.nk = --ref; - return ref; -} - -/* Get ref of next 64 bit IR constant and optionally grow IR. -** Note: this may invalidate all IRIns *! -*/ -static LJ_AINLINE IRRef ir_nextk64(jit_State *J) -{ - IRRef ref = J->cur.nk - 2; - lj_assertJ(J->state != LJ_TRACE_ASM, "bad JIT state"); - if (LJ_UNLIKELY(ref < J->irbotlim)) lj_ir_growbot(J); - J->cur.nk = ref; - return ref; -} - -#if LJ_GC64 -#define ir_nextkgc ir_nextk64 -#else -#define ir_nextkgc ir_nextk -#endif - -/* Intern int32_t constant. */ -TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - for (ref = J->chain[IR_KINT]; ref; ref = cir[ref].prev) - if (cir[ref].i == k) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - ir->i = k; - ir->t.irt = IRT_INT; - ir->o = IR_KINT; - ir->prev = J->chain[IR_KINT]; - J->chain[IR_KINT] = (IRRef1)ref; -found: - return TREF(ref, IRT_INT); -} - -/* Intern 64 bit constant, given by its 64 bit pattern. */ -TRef lj_ir_k64(jit_State *J, IROp op, uint64_t u64) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64; - for (ref = J->chain[op]; ref; ref = cir[ref].prev) - if (ir_k64(&cir[ref])->u64 == u64) - goto found; - ref = ir_nextk64(J); - ir = IR(ref); - ir[1].tv.u64 = u64; - ir->t.irt = t; - ir->o = op; - ir->op12 = 0; - ir->prev = J->chain[op]; - J->chain[op] = (IRRef1)ref; -found: - return TREF(ref, t); -} - -/* Intern FP constant, given by its 64 bit pattern. */ -TRef lj_ir_knum_u64(jit_State *J, uint64_t u64) -{ - return lj_ir_k64(J, IR_KNUM, u64); -} - -/* Intern 64 bit integer constant. */ -TRef lj_ir_kint64(jit_State *J, uint64_t u64) -{ - return lj_ir_k64(J, IR_KINT64, u64); -} - -/* Check whether a number is int and return it. -0 is NOT considered an int. */ -static int numistrueint(lua_Number n, int32_t *kp) -{ - int32_t k = lj_num2int(n); - if (n == (lua_Number)k) { - if (kp) *kp = k; - if (k == 0) { /* Special check for -0. */ - TValue tv; - setnumV(&tv, n); - if (tv.u32.hi != 0) - return 0; - } - return 1; - } - return 0; -} - -/* Intern number as int32_t constant if possible, otherwise as FP constant. */ -TRef lj_ir_knumint(jit_State *J, lua_Number n) -{ - int32_t k; - if (numistrueint(n, &k)) - return lj_ir_kint(J, k); - else - return lj_ir_knum(J, n); -} - -/* Intern GC object "constant". */ -TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - lj_assertJ(!isdead(J2G(J), o), "interning of dead GC object"); - for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev) - if (ir_kgc(&cir[ref]) == o) - goto found; - ref = ir_nextkgc(J); - ir = IR(ref); - /* NOBARRIER: Current trace is a GC root. */ - ir->op12 = 0; - setgcref(ir[LJ_GC64].gcr, o); - ir->t.irt = (uint8_t)t; - ir->o = IR_KGC; - ir->prev = J->chain[IR_KGC]; - J->chain[IR_KGC] = (IRRef1)ref; -found: - return TREF(ref, t); -} - -/* Allocate GCtrace constant placeholder (no interning). */ -TRef lj_ir_ktrace(jit_State *J) -{ - IRRef ref = ir_nextkgc(J); - IRIns *ir = IR(ref); - lj_assertJ(irt_toitype_(IRT_P64) == LJ_TTRACE, "mismatched type mapping"); - ir->t.irt = IRT_P64; - ir->o = LJ_GC64 ? IR_KNUM : IR_KNULL; /* Not IR_KGC yet, but same size. */ - ir->op12 = 0; - ir->prev = 0; - return TREF(ref, IRT_P64); -} - -/* Intern pointer constant. */ -TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; -#if LJ_64 && !LJ_GC64 - lj_assertJ((void *)(uintptr_t)u32ptr(ptr) == ptr, "out-of-range GC pointer"); -#endif - for (ref = J->chain[op]; ref; ref = cir[ref].prev) - if (ir_kptr(&cir[ref]) == ptr) - goto found; -#if LJ_GC64 - ref = ir_nextk64(J); -#else - ref = ir_nextk(J); -#endif - ir = IR(ref); - ir->op12 = 0; - setmref(ir[LJ_GC64].ptr, ptr); - ir->t.irt = IRT_PGC; - ir->o = op; - ir->prev = J->chain[op]; - J->chain[op] = (IRRef1)ref; -found: - return TREF(ref, IRT_PGC); -} - -/* Intern typed NULL constant. */ -TRef lj_ir_knull(jit_State *J, IRType t) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef ref; - for (ref = J->chain[IR_KNULL]; ref; ref = cir[ref].prev) - if (irt_t(cir[ref].t) == t) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - ir->i = 0; - ir->t.irt = (uint8_t)t; - ir->o = IR_KNULL; - ir->prev = J->chain[IR_KNULL]; - J->chain[IR_KNULL] = (IRRef1)ref; -found: - return TREF(ref, t); -} - -/* Intern key slot. */ -TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot) -{ - IRIns *ir, *cir = J->cur.ir; - IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot); - IRRef ref; - /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */ - lj_assertJ(tref_isk(key) && slot == (IRRef)(IRRef1)slot, - "out-of-range key/slot"); - for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev) - if (cir[ref].op12 == op12) - goto found; - ref = ir_nextk(J); - ir = IR(ref); - ir->op12 = op12; - ir->t.irt = IRT_P32; - ir->o = IR_KSLOT; - ir->prev = J->chain[IR_KSLOT]; - J->chain[IR_KSLOT] = (IRRef1)ref; -found: - return TREF(ref, IRT_P32); -} - -/* -- Access to IR constants ---------------------------------------------- */ - -/* Copy value of IR constant. */ -void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir) -{ - UNUSED(L); - lj_assertL(ir->o != IR_KSLOT, "unexpected KSLOT"); /* Common mistake. */ - switch (ir->o) { - case IR_KPRI: setpriV(tv, irt_toitype(ir->t)); break; - case IR_KINT: setintV(tv, ir->i); break; - case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break; - case IR_KPTR: case IR_KKPTR: - setnumV(tv, (lua_Number)(uintptr_t)ir_kptr(ir)); - break; - case IR_KNULL: setintV(tv, 0); break; - case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break; -#if LJ_HASFFI - case IR_KINT64: { - GCcdata *cd = lj_cdata_new_(L, CTID_INT64, 8); - *(uint64_t *)cdataptr(cd) = ir_kint64(ir)->u64; - setcdataV(L, tv, cd); - break; - } -#endif - default: lj_assertL(0, "bad IR constant op %d", ir->o); break; - } -} - -/* -- Convert IR operand types -------------------------------------------- */ - -/* Convert from string to number. */ -TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr) -{ - if (!tref_isnumber(tr)) { - if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - else - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - return tr; -} - -/* Convert from integer or string to number. */ -TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr) -{ - if (!tref_isnum(tr)) { - if (tref_isinteger(tr)) - tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - else if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - else - lj_trace_err(J, LJ_TRERR_BADTYPE); - } - return tr; -} - -/* Convert from integer or number to string. */ -TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr) -{ - if (!tref_isstr(tr)) { - if (!tref_isnumber(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - tr = emitir(IRT(IR_TOSTR, IRT_STR), tr, - tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT); - } - return tr; -} - -/* -- Miscellaneous IR ops ------------------------------------------------ */ - -/* Evaluate numeric comparison. */ -int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op) -{ - switch (op) { - case IR_EQ: return (a == b); - case IR_NE: return (a != b); - case IR_LT: return (a < b); - case IR_GE: return (a >= b); - case IR_LE: return (a <= b); - case IR_GT: return (a > b); - case IR_ULT: return !(a >= b); - case IR_UGE: return !(a < b); - case IR_ULE: return !(a > b); - case IR_UGT: return !(a <= b); - default: lj_assertX(0, "bad IR op %d", op); return 0; - } -} - -/* Evaluate string comparison. */ -int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op) -{ - int res = lj_str_cmp(a, b); - switch (op) { - case IR_LT: return (res < 0); - case IR_GE: return (res >= 0); - case IR_LE: return (res <= 0); - case IR_GT: return (res > 0); - default: lj_assertX(0, "bad IR op %d", op); return 0; - } -} - -/* Rollback IR to previous state. */ -void lj_ir_rollback(jit_State *J, IRRef ref) -{ - IRRef nins = J->cur.nins; - while (nins > ref) { - IRIns *ir; - nins--; - ir = IR(nins); - J->chain[ir->o] = ir->prev; - } - J->cur.nins = nins; -} - -#undef IR -#undef fins -#undef emitir - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.h deleted file mode 100644 index ed492e9..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ir.h +++ /dev/null @@ -1,614 +0,0 @@ -/* -** SSA IR (Intermediate Representation) format. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_IR_H -#define _LJ_IR_H - -#include "lj_obj.h" - -/* -- IR instructions ----------------------------------------------------- */ - -/* IR instruction definition. Order matters, see below. ORDER IR */ -#define IRDEF(_) \ - /* Guarded assertions. */ \ - /* Must be properly aligned to flip opposites (^1) and (un)ordered (^4). */ \ - _(LT, N , ref, ref) \ - _(GE, N , ref, ref) \ - _(LE, N , ref, ref) \ - _(GT, N , ref, ref) \ - \ - _(ULT, N , ref, ref) \ - _(UGE, N , ref, ref) \ - _(ULE, N , ref, ref) \ - _(UGT, N , ref, ref) \ - \ - _(EQ, C , ref, ref) \ - _(NE, C , ref, ref) \ - \ - _(ABC, N , ref, ref) \ - _(RETF, S , ref, ref) \ - \ - /* Miscellaneous ops. */ \ - _(NOP, N , ___, ___) \ - _(BASE, N , lit, lit) \ - _(PVAL, N , lit, ___) \ - _(GCSTEP, S , ___, ___) \ - _(HIOP, S , ref, ref) \ - _(LOOP, S , ___, ___) \ - _(USE, S , ref, ___) \ - _(PHI, S , ref, ref) \ - _(RENAME, S , ref, lit) \ - _(PROF, S , ___, ___) \ - \ - /* Constants. */ \ - _(KPRI, N , ___, ___) \ - _(KINT, N , cst, ___) \ - _(KGC, N , cst, ___) \ - _(KPTR, N , cst, ___) \ - _(KKPTR, N , cst, ___) \ - _(KNULL, N , cst, ___) \ - _(KNUM, N , cst, ___) \ - _(KINT64, N , cst, ___) \ - _(KSLOT, N , ref, lit) \ - \ - /* Bit ops. */ \ - _(BNOT, N , ref, ___) \ - _(BSWAP, N , ref, ___) \ - _(BAND, C , ref, ref) \ - _(BOR, C , ref, ref) \ - _(BXOR, C , ref, ref) \ - _(BSHL, N , ref, ref) \ - _(BSHR, N , ref, ref) \ - _(BSAR, N , ref, ref) \ - _(BROL, N , ref, ref) \ - _(BROR, N , ref, ref) \ - \ - /* Arithmetic ops. ORDER ARITH */ \ - _(ADD, C , ref, ref) \ - _(SUB, N , ref, ref) \ - _(MUL, C , ref, ref) \ - _(DIV, N , ref, ref) \ - _(MOD, N , ref, ref) \ - _(POW, N , ref, ref) \ - _(NEG, N , ref, ref) \ - \ - _(ABS, N , ref, ref) \ - _(LDEXP, N , ref, ref) \ - _(MIN, C , ref, ref) \ - _(MAX, C , ref, ref) \ - _(FPMATH, N , ref, lit) \ - \ - /* Overflow-checking arithmetic ops. */ \ - _(ADDOV, CW, ref, ref) \ - _(SUBOV, NW, ref, ref) \ - _(MULOV, CW, ref, ref) \ - \ - /* Memory ops. A = array, H = hash, U = upvalue, F = field, S = stack. */ \ - \ - /* Memory references. */ \ - _(AREF, R , ref, ref) \ - _(HREFK, R , ref, ref) \ - _(HREF, L , ref, ref) \ - _(NEWREF, S , ref, ref) \ - _(UREFO, LW, ref, lit) \ - _(UREFC, LW, ref, lit) \ - _(FREF, R , ref, lit) \ - _(TMPREF, S , ref, lit) \ - _(STRREF, N , ref, ref) \ - _(LREF, L , ___, ___) \ - \ - /* Loads and Stores. These must be in the same order. */ \ - _(ALOAD, L , ref, ___) \ - _(HLOAD, L , ref, ___) \ - _(ULOAD, L , ref, ___) \ - _(FLOAD, L , ref, lit) \ - _(XLOAD, L , ref, lit) \ - _(SLOAD, L , lit, lit) \ - _(VLOAD, L , ref, lit) \ - _(ALEN, L , ref, ref) \ - \ - _(ASTORE, S , ref, ref) \ - _(HSTORE, S , ref, ref) \ - _(USTORE, S , ref, ref) \ - _(FSTORE, S , ref, ref) \ - _(XSTORE, S , ref, ref) \ - \ - /* Allocations. */ \ - _(SNEW, N , ref, ref) /* CSE is ok, not marked as A. */ \ - _(XSNEW, A , ref, ref) \ - _(TNEW, AW, lit, lit) \ - _(TDUP, AW, ref, ___) \ - _(CNEW, AW, ref, ref) \ - _(CNEWI, NW, ref, ref) /* CSE is ok, not marked as A. */ \ - \ - /* Buffer operations. */ \ - _(BUFHDR, L , ref, lit) \ - _(BUFPUT, LW, ref, ref) \ - _(BUFSTR, AW, ref, ref) \ - \ - /* Barriers. */ \ - _(TBAR, S , ref, ___) \ - _(OBAR, S , ref, ref) \ - _(XBAR, S , ___, ___) \ - \ - /* Type conversions. */ \ - _(CONV, N , ref, lit) \ - _(TOBIT, N , ref, ref) \ - _(TOSTR, N , ref, lit) \ - _(STRTO, N , ref, ___) \ - \ - /* Calls. */ \ - _(CALLN, NW, ref, lit) \ - _(CALLA, AW, ref, lit) \ - _(CALLL, LW, ref, lit) \ - _(CALLS, S , ref, lit) \ - _(CALLXS, S , ref, ref) \ - _(CARG, N , ref, ref) \ - \ - /* End of list. */ - -/* IR opcodes (max. 256). */ -typedef enum { -#define IRENUM(name, m, m1, m2) IR_##name, -IRDEF(IRENUM) -#undef IRENUM - IR__MAX -} IROp; - -/* Stored opcode. */ -typedef uint8_t IROp1; - -LJ_STATIC_ASSERT(((int)IR_EQ^1) == (int)IR_NE); -LJ_STATIC_ASSERT(((int)IR_LT^1) == (int)IR_GE); -LJ_STATIC_ASSERT(((int)IR_LE^1) == (int)IR_GT); -LJ_STATIC_ASSERT(((int)IR_LT^3) == (int)IR_GT); -LJ_STATIC_ASSERT(((int)IR_LT^4) == (int)IR_ULT); - -/* Delta between xLOAD and xSTORE. */ -#define IRDELTA_L2S ((int)IR_ASTORE - (int)IR_ALOAD) - -LJ_STATIC_ASSERT((int)IR_HLOAD + IRDELTA_L2S == (int)IR_HSTORE); -LJ_STATIC_ASSERT((int)IR_ULOAD + IRDELTA_L2S == (int)IR_USTORE); -LJ_STATIC_ASSERT((int)IR_FLOAD + IRDELTA_L2S == (int)IR_FSTORE); -LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE); - -/* -- Named IR literals --------------------------------------------------- */ - -/* FPMATH sub-functions. ORDER FPM. */ -#define IRFPMDEF(_) \ - _(FLOOR) _(CEIL) _(TRUNC) /* Must be first and in this order. */ \ - _(SQRT) _(LOG) _(LOG2) \ - _(OTHER) - -typedef enum { -#define FPMENUM(name) IRFPM_##name, -IRFPMDEF(FPMENUM) -#undef FPMENUM - IRFPM__MAX -} IRFPMathOp; - -/* FLOAD fields. */ -#define IRFLDEF(_) \ - _(STR_LEN, offsetof(GCstr, len)) \ - _(FUNC_ENV, offsetof(GCfunc, l.env)) \ - _(FUNC_PC, offsetof(GCfunc, l.pc)) \ - _(FUNC_FFID, offsetof(GCfunc, l.ffid)) \ - _(THREAD_ENV, offsetof(lua_State, env)) \ - _(TAB_META, offsetof(GCtab, metatable)) \ - _(TAB_ARRAY, offsetof(GCtab, array)) \ - _(TAB_NODE, offsetof(GCtab, node)) \ - _(TAB_ASIZE, offsetof(GCtab, asize)) \ - _(TAB_HMASK, offsetof(GCtab, hmask)) \ - _(TAB_NOMM, offsetof(GCtab, nomm)) \ - _(UDATA_META, offsetof(GCudata, metatable)) \ - _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \ - _(UDATA_FILE, sizeof(GCudata)) \ - _(SBUF_W, sizeof(GCudata) + offsetof(SBufExt, w)) \ - _(SBUF_E, sizeof(GCudata) + offsetof(SBufExt, e)) \ - _(SBUF_B, sizeof(GCudata) + offsetof(SBufExt, b)) \ - _(SBUF_L, sizeof(GCudata) + offsetof(SBufExt, L)) \ - _(SBUF_REF, sizeof(GCudata) + offsetof(SBufExt, cowref)) \ - _(SBUF_R, sizeof(GCudata) + offsetof(SBufExt, r)) \ - _(CDATA_CTYPEID, offsetof(GCcdata, ctypeid)) \ - _(CDATA_PTR, sizeof(GCcdata)) \ - _(CDATA_INT, sizeof(GCcdata)) \ - _(CDATA_INT64, sizeof(GCcdata)) \ - _(CDATA_INT64_4, sizeof(GCcdata) + 4) - -typedef enum { -#define FLENUM(name, ofs) IRFL_##name, -IRFLDEF(FLENUM) -#undef FLENUM - IRFL__MAX -} IRFieldID; - -/* TMPREF mode bits, stored in op2. */ -#define IRTMPREF_IN1 0x01 /* First input value. */ -#define IRTMPREF_OUT1 0x02 /* First output value. */ -#define IRTMPREF_OUT2 0x04 /* Second output value. */ - -/* SLOAD mode bits, stored in op2. */ -#define IRSLOAD_PARENT 0x01 /* Coalesce with parent trace. */ -#define IRSLOAD_FRAME 0x02 /* Load 32 bits of ftsz. */ -#define IRSLOAD_TYPECHECK 0x04 /* Needs type check. */ -#define IRSLOAD_CONVERT 0x08 /* Number to integer conversion. */ -#define IRSLOAD_READONLY 0x10 /* Read-only, omit slot store. */ -#define IRSLOAD_INHERIT 0x20 /* Inherited by exits/side traces. */ -#define IRSLOAD_KEYINDEX 0x40 /* Table traversal key index. */ - -/* XLOAD mode bits, stored in op2. */ -#define IRXLOAD_READONLY 0x01 /* Load from read-only data. */ -#define IRXLOAD_VOLATILE 0x02 /* Load from volatile data. */ -#define IRXLOAD_UNALIGNED 0x04 /* Unaligned load. */ - -/* BUFHDR mode, stored in op2. */ -#define IRBUFHDR_RESET 0 /* Reset buffer. */ -#define IRBUFHDR_APPEND 1 /* Append to buffer. */ -#define IRBUFHDR_WRITE 2 /* Write to string buffer. */ - -/* CONV mode, stored in op2. */ -#define IRCONV_SRCMASK 0x001f /* Source IRType. */ -#define IRCONV_DSTMASK 0x03e0 /* Dest. IRType (also in ir->t). */ -#define IRCONV_DSH 5 -#define IRCONV_NUM_INT ((IRT_NUM<>2)&3)) -#define irm_iscomm(m) ((m) & IRM_C) -#define irm_kind(m) ((m) & IRM_S) - -#define IRMODE(name, m, m1, m2) (((IRM##m1)|((IRM##m2)<<2)|(IRM_##m))^IRM_W), - -LJ_DATA const uint8_t lj_ir_mode[IR__MAX+1]; - -/* -- IR instruction types ------------------------------------------------ */ - -#define IRTSIZE_PGC (LJ_GC64 ? 8 : 4) - -/* Map of itypes to non-negative numbers and their sizes. ORDER LJ_T. -** LJ_TUPVAL/LJ_TTRACE never appear in a TValue. Use these itypes for -** IRT_P32 and IRT_P64, which never escape the IR. -** The various integers are only used in the IR and can only escape to -** a TValue after implicit or explicit conversion. Their types must be -** contiguous and next to IRT_NUM (see the typerange macros below). -*/ -#define IRTDEF(_) \ - _(NIL, 4) _(FALSE, 4) _(TRUE, 4) _(LIGHTUD, LJ_64 ? 8 : 4) \ - _(STR, IRTSIZE_PGC) _(P32, 4) _(THREAD, IRTSIZE_PGC) _(PROTO, IRTSIZE_PGC) \ - _(FUNC, IRTSIZE_PGC) _(P64, 8) _(CDATA, IRTSIZE_PGC) _(TAB, IRTSIZE_PGC) \ - _(UDATA, IRTSIZE_PGC) \ - _(FLOAT, 4) _(NUM, 8) _(I8, 1) _(U8, 1) _(I16, 2) _(U16, 2) \ - _(INT, 4) _(U32, 4) _(I64, 8) _(U64, 8) \ - _(SOFTFP, 4) /* There is room for 8 more types. */ - -/* IR result type and flags (8 bit). */ -typedef enum { -#define IRTENUM(name, size) IRT_##name, -IRTDEF(IRTENUM) -#undef IRTENUM - IRT__MAX, - - /* Native pointer type and the corresponding integer type. */ - IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32, - IRT_PGC = LJ_GC64 ? IRT_P64 : IRT_P32, - IRT_IGC = LJ_GC64 ? IRT_I64 : IRT_INT, - IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT, - IRT_UINTP = LJ_64 ? IRT_U64 : IRT_U32, - - /* Additional flags. */ - IRT_MARK = 0x20, /* Marker for misc. purposes. */ - IRT_ISPHI = 0x40, /* Instruction is left or right PHI operand. */ - IRT_GUARD = 0x80, /* Instruction is a guard. */ - - /* Masks. */ - IRT_TYPE = 0x1f, - IRT_T = 0xff -} IRType; - -#define irtype_ispri(irt) ((uint32_t)(irt) <= IRT_TRUE) - -/* Stored IRType. */ -typedef struct IRType1 { uint8_t irt; } IRType1; - -#define IRT(o, t) ((uint32_t)(((o)<<8) | (t))) -#define IRTI(o) (IRT((o), IRT_INT)) -#define IRTN(o) (IRT((o), IRT_NUM)) -#define IRTG(o, t) (IRT((o), IRT_GUARD|(t))) -#define IRTGI(o) (IRT((o), IRT_GUARD|IRT_INT)) - -#define irt_t(t) ((IRType)(t).irt) -#define irt_type(t) ((IRType)((t).irt & IRT_TYPE)) -#define irt_sametype(t1, t2) ((((t1).irt ^ (t2).irt) & IRT_TYPE) == 0) -#define irt_typerange(t, first, last) \ - ((uint32_t)((t).irt & IRT_TYPE) - (uint32_t)(first) <= (uint32_t)(last-first)) - -#define irt_isnil(t) (irt_type(t) == IRT_NIL) -#define irt_ispri(t) ((uint32_t)irt_type(t) <= IRT_TRUE) -#define irt_islightud(t) (irt_type(t) == IRT_LIGHTUD) -#define irt_isstr(t) (irt_type(t) == IRT_STR) -#define irt_istab(t) (irt_type(t) == IRT_TAB) -#define irt_iscdata(t) (irt_type(t) == IRT_CDATA) -#define irt_isfloat(t) (irt_type(t) == IRT_FLOAT) -#define irt_isnum(t) (irt_type(t) == IRT_NUM) -#define irt_isint(t) (irt_type(t) == IRT_INT) -#define irt_isi8(t) (irt_type(t) == IRT_I8) -#define irt_isu8(t) (irt_type(t) == IRT_U8) -#define irt_isi16(t) (irt_type(t) == IRT_I16) -#define irt_isu16(t) (irt_type(t) == IRT_U16) -#define irt_isu32(t) (irt_type(t) == IRT_U32) -#define irt_isi64(t) (irt_type(t) == IRT_I64) -#define irt_isu64(t) (irt_type(t) == IRT_U64) - -#define irt_isfp(t) (irt_isnum(t) || irt_isfloat(t)) -#define irt_isinteger(t) (irt_typerange((t), IRT_I8, IRT_INT)) -#define irt_isgcv(t) (irt_typerange((t), IRT_STR, IRT_UDATA)) -#define irt_isaddr(t) (irt_typerange((t), IRT_LIGHTUD, IRT_UDATA)) -#define irt_isint64(t) (irt_typerange((t), IRT_I64, IRT_U64)) - -#if LJ_GC64 -/* Include IRT_NIL, so IR(ASMREF_L) (aka REF_NIL) is considered 64 bit. */ -#define IRT_IS64 \ - ((1u<> irt_type(t)) & 1) -#define irt_is64orfp(t) (((IRT_IS64|(1u<>irt_type(t)) & 1) - -#define irt_size(t) (lj_ir_type_size[irt_t((t))]) - -LJ_DATA const uint8_t lj_ir_type_size[]; - -static LJ_AINLINE IRType itype2irt(const TValue *tv) -{ - if (tvisint(tv)) - return IRT_INT; - else if (tvisnum(tv)) - return IRT_NUM; -#if LJ_64 && !LJ_GC64 - else if (tvislightud(tv)) - return IRT_LIGHTUD; -#endif - else - return (IRType)~itype(tv); -} - -static LJ_AINLINE uint32_t irt_toitype_(IRType t) -{ - lj_assertX(!LJ_64 || LJ_GC64 || t != IRT_LIGHTUD, - "no plain type tag for lightuserdata"); - if (LJ_DUALNUM && t > IRT_NUM) { - return LJ_TISNUM; - } else { - lj_assertX(t <= IRT_NUM, "no plain type tag for IR type %d", t); - return ~(uint32_t)t; - } -} - -#define irt_toitype(t) irt_toitype_(irt_type((t))) - -#define irt_isguard(t) ((t).irt & IRT_GUARD) -#define irt_ismarked(t) ((t).irt & IRT_MARK) -#define irt_setmark(t) ((t).irt |= IRT_MARK) -#define irt_clearmark(t) ((t).irt &= ~IRT_MARK) -#define irt_isphi(t) ((t).irt & IRT_ISPHI) -#define irt_setphi(t) ((t).irt |= IRT_ISPHI) -#define irt_clearphi(t) ((t).irt &= ~IRT_ISPHI) - -/* Stored combined IR opcode and type. */ -typedef uint16_t IROpT; - -/* -- IR references ------------------------------------------------------- */ - -/* IR references. */ -typedef uint16_t IRRef1; /* One stored reference. */ -typedef uint32_t IRRef2; /* Two stored references. */ -typedef uint32_t IRRef; /* Used to pass around references. */ - -/* Fixed references. */ -enum { - REF_BIAS = 0x8000, - REF_TRUE = REF_BIAS-3, - REF_FALSE = REF_BIAS-2, - REF_NIL = REF_BIAS-1, /* \--- Constants grow downwards. */ - REF_BASE = REF_BIAS, /* /--- IR grows upwards. */ - REF_FIRST = REF_BIAS+1, - REF_DROP = 0xffff -}; - -/* Note: IRMlit operands must be < REF_BIAS, too! -** This allows for fast and uniform manipulation of all operands -** without looking up the operand mode in lj_ir_mode: -** - CSE calculates the maximum reference of two operands. -** This must work with mixed reference/literal operands, too. -** - DCE marking only checks for operand >= REF_BIAS. -** - LOOP needs to substitute reference operands. -** Constant references and literals must not be modified. -*/ - -#define IRREF2(lo, hi) ((IRRef2)(lo) | ((IRRef2)(hi) << 16)) - -#define irref_isk(ref) ((ref) < REF_BIAS) - -/* Tagged IR references (32 bit). -** -** +-------+-------+---------------+ -** | irt | flags | ref | -** +-------+-------+---------------+ -** -** The tag holds a copy of the IRType and speeds up IR type checks. -*/ -typedef uint32_t TRef; - -#define TREF_REFMASK 0x0000ffff -#define TREF_FRAME 0x00010000 -#define TREF_CONT 0x00020000 -#define TREF_KEYINDEX 0x00100000 - -#define TREF(ref, t) ((TRef)((ref) + ((t)<<24))) - -#define tref_ref(tr) ((IRRef1)(tr)) -#define tref_t(tr) ((IRType)((tr)>>24)) -#define tref_type(tr) ((IRType)(((tr)>>24) & IRT_TYPE)) -#define tref_typerange(tr, first, last) \ - ((((tr)>>24) & IRT_TYPE) - (TRef)(first) <= (TRef)(last-first)) - -#define tref_istype(tr, t) (((tr) & (IRT_TYPE<<24)) == ((t)<<24)) -#define tref_isnil(tr) (tref_istype((tr), IRT_NIL)) -#define tref_isfalse(tr) (tref_istype((tr), IRT_FALSE)) -#define tref_istrue(tr) (tref_istype((tr), IRT_TRUE)) -#define tref_islightud(tr) (tref_istype((tr), IRT_LIGHTUD)) -#define tref_isstr(tr) (tref_istype((tr), IRT_STR)) -#define tref_isfunc(tr) (tref_istype((tr), IRT_FUNC)) -#define tref_iscdata(tr) (tref_istype((tr), IRT_CDATA)) -#define tref_istab(tr) (tref_istype((tr), IRT_TAB)) -#define tref_isudata(tr) (tref_istype((tr), IRT_UDATA)) -#define tref_isnum(tr) (tref_istype((tr), IRT_NUM)) -#define tref_isint(tr) (tref_istype((tr), IRT_INT)) - -#define tref_isbool(tr) (tref_typerange((tr), IRT_FALSE, IRT_TRUE)) -#define tref_ispri(tr) (tref_typerange((tr), IRT_NIL, IRT_TRUE)) -#define tref_istruecond(tr) (!tref_typerange((tr), IRT_NIL, IRT_FALSE)) -#define tref_isinteger(tr) (tref_typerange((tr), IRT_I8, IRT_INT)) -#define tref_isnumber(tr) (tref_typerange((tr), IRT_NUM, IRT_INT)) -#define tref_isnumber_str(tr) (tref_isnumber((tr)) || tref_isstr((tr))) -#define tref_isgcv(tr) (tref_typerange((tr), IRT_STR, IRT_UDATA)) - -#define tref_isk(tr) (irref_isk(tref_ref((tr)))) -#define tref_isk2(tr1, tr2) (irref_isk(tref_ref((tr1) | (tr2)))) - -#define TREF_PRI(t) (TREF(REF_NIL-(t), (t))) -#define TREF_NIL (TREF_PRI(IRT_NIL)) -#define TREF_FALSE (TREF_PRI(IRT_FALSE)) -#define TREF_TRUE (TREF_PRI(IRT_TRUE)) - -/* -- IR format ----------------------------------------------------------- */ - -/* IR instruction format (64 bit). -** -** 16 16 8 8 8 8 -** +-------+-------+---+---+---+---+ -** | op1 | op2 | t | o | r | s | -** +-------+-------+---+---+---+---+ -** | op12/i/gco32 | ot | prev | (alternative fields in union) -** +-------+-------+---+---+---+---+ -** | TValue/gco64 | (2nd IR slot for 64 bit constants) -** +---------------+-------+-------+ -** 32 16 16 -** -** prev is only valid prior to register allocation and then reused for r + s. -*/ - -typedef union IRIns { - struct { - LJ_ENDIAN_LOHI( - IRRef1 op1; /* IR operand 1. */ - , IRRef1 op2; /* IR operand 2. */ - ) - IROpT ot; /* IR opcode and type (overlaps t and o). */ - IRRef1 prev; /* Previous ins in same chain (overlaps r and s). */ - }; - struct { - IRRef2 op12; /* IR operand 1 and 2 (overlaps op1 and op2). */ - LJ_ENDIAN_LOHI( - IRType1 t; /* IR type. */ - , IROp1 o; /* IR opcode. */ - ) - LJ_ENDIAN_LOHI( - uint8_t r; /* Register allocation (overlaps prev). */ - , uint8_t s; /* Spill slot allocation (overlaps prev). */ - ) - }; - int32_t i; /* 32 bit signed integer literal (overlaps op12). */ - GCRef gcr; /* GCobj constant (overlaps op12 or entire slot). */ - MRef ptr; /* Pointer constant (overlaps op12 or entire slot). */ - TValue tv; /* TValue constant (overlaps entire slot). */ -} IRIns; - -#define ir_isk64(ir) \ - ((ir)->o == IR_KNUM || (ir)->o == IR_KINT64 || \ - (LJ_GC64 && \ - ((ir)->o == IR_KGC || (ir)->o == IR_KPTR || (ir)->o == IR_KKPTR))) - -#define ir_kgc(ir) check_exp((ir)->o == IR_KGC, gcref((ir)[LJ_GC64].gcr)) -#define ir_kstr(ir) (gco2str(ir_kgc((ir)))) -#define ir_ktab(ir) (gco2tab(ir_kgc((ir)))) -#define ir_kfunc(ir) (gco2func(ir_kgc((ir)))) -#define ir_kcdata(ir) (gco2cd(ir_kgc((ir)))) -#define ir_knum(ir) check_exp((ir)->o == IR_KNUM, &(ir)[1].tv) -#define ir_kint64(ir) check_exp((ir)->o == IR_KINT64, &(ir)[1].tv) -#define ir_k64(ir) check_exp(ir_isk64(ir), &(ir)[1].tv) -#define ir_kptr(ir) \ - check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, \ - mref((ir)[LJ_GC64].ptr, void)) - -/* A store or any other op with a non-weak guard has a side-effect. */ -static LJ_AINLINE int ir_sideeff(IRIns *ir) -{ - return (((ir->t.irt | ~IRT_GUARD) & lj_ir_mode[ir->o]) >= IRM_S); -} - -LJ_STATIC_ASSERT((int)IRT_GUARD == (int)IRM_W); - -/* Replace IR instruction with NOP. */ -static LJ_AINLINE void lj_ir_nop(IRIns *ir) -{ - ir->ot = IRT(IR_NOP, IRT_NIL); - ir->op1 = ir->op2 = 0; - ir->prev = 0; -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ircall.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ircall.h deleted file mode 100644 index 67fb58a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_ircall.h +++ /dev/null @@ -1,383 +0,0 @@ -/* -** IR CALL* instruction definitions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_IRCALL_H -#define _LJ_IRCALL_H - -#include "lj_obj.h" -#include "lj_ir.h" -#include "lj_jit.h" - -/* C call info for CALL* instructions. */ -typedef struct CCallInfo { - ASMFunction func; /* Function pointer. */ - uint32_t flags; /* Number of arguments and flags. */ -} CCallInfo; - -#define CCI_NARGS(ci) ((ci)->flags & 0xff) /* # of args. */ -#define CCI_NARGS_MAX 32 /* Max. # of args. */ - -#define CCI_OTSHIFT 16 -#define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */ -#define CCI_TYPE(ci) (((ci)->flags>>CCI_OTSHIFT) & IRT_TYPE) -#define CCI_OPSHIFT 24 -#define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */ - -#define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT) -#define CCI_CALL_A (IR_CALLA << CCI_OPSHIFT) -#define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT) -#define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT) -#define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL) -#define CCI_CALL_FA (CCI_CALL_A|CCI_CC_FASTCALL) -#define CCI_CALL_FL (CCI_CALL_L|CCI_CC_FASTCALL) -#define CCI_CALL_FS (CCI_CALL_S|CCI_CC_FASTCALL) - -/* C call info flags. */ -#define CCI_T (IRT_GUARD << CCI_OTSHIFT) /* May throw. */ -#define CCI_L 0x0100 /* Implicit L arg. */ -#define CCI_CASTU64 0x0200 /* Cast u64 result to number. */ -#define CCI_NOFPRCLOBBER 0x0400 /* Does not clobber any FPRs. */ -#define CCI_VARARG 0x0800 /* Vararg function. */ - -#define CCI_CC_MASK 0x3000 /* Calling convention mask. */ -#define CCI_CC_SHIFT 12 -/* ORDER CC */ -#define CCI_CC_CDECL 0x0000 /* Default cdecl calling convention. */ -#define CCI_CC_THISCALL 0x1000 /* Thiscall calling convention. */ -#define CCI_CC_FASTCALL 0x2000 /* Fastcall calling convention. */ -#define CCI_CC_STDCALL 0x3000 /* Stdcall calling convention. */ - -/* Extra args for SOFTFP, SPLIT 64 bit. */ -#define CCI_XARGS_SHIFT 14 -#define CCI_XARGS(ci) (((ci)->flags >> CCI_XARGS_SHIFT) & 3) -#define CCI_XA (1u << CCI_XARGS_SHIFT) - -#if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI) -#define CCI_XNARGS(ci) (CCI_NARGS((ci)) + CCI_XARGS((ci))) -#else -#define CCI_XNARGS(ci) CCI_NARGS((ci)) -#endif - -/* Helpers for conditional function definitions. */ -#define IRCALLCOND_ANY(x) x - -#if LJ_TARGET_X86ORX64 -#define IRCALLCOND_FPMATH(x) NULL -#else -#define IRCALLCOND_FPMATH(x) x -#endif - -#if LJ_SOFTFP -#define IRCALLCOND_SOFTFP(x) x -#if LJ_HASFFI -#define IRCALLCOND_SOFTFP_FFI(x) x -#else -#define IRCALLCOND_SOFTFP_FFI(x) NULL -#endif -#else -#define IRCALLCOND_SOFTFP(x) NULL -#define IRCALLCOND_SOFTFP_FFI(x) NULL -#endif - -#if LJ_SOFTFP && LJ_TARGET_MIPS -#define IRCALLCOND_SOFTFP_MIPS(x) x -#else -#define IRCALLCOND_SOFTFP_MIPS(x) NULL -#endif - -#if LJ_SOFTFP && LJ_TARGET_MIPS64 -#define IRCALLCOND_SOFTFP_MIPS64(x) x -#else -#define IRCALLCOND_SOFTFP_MIPS64(x) NULL -#endif - -#define LJ_NEED_FP64 (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS) - -#if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) -#define IRCALLCOND_FP64_FFI(x) x -#else -#define IRCALLCOND_FP64_FFI(x) NULL -#endif - -#if LJ_HASFFI -#define IRCALLCOND_FFI(x) x -#if LJ_32 -#define IRCALLCOND_FFI32(x) x -#else -#define IRCALLCOND_FFI32(x) NULL -#endif -#else -#define IRCALLCOND_FFI(x) NULL -#define IRCALLCOND_FFI32(x) NULL -#endif - -#if LJ_HASBUFFER -#define IRCALLCOND_BUFFER(x) x -#else -#define IRCALLCOND_BUFFER(x) NULL -#endif - -#if LJ_HASBUFFER && LJ_HASFFI -#define IRCALLCOND_BUFFFI(x) x -#else -#define IRCALLCOND_BUFFFI(x) NULL -#endif - -#if LJ_SOFTFP -#define XA_FP CCI_XA -#define XA2_FP (CCI_XA+CCI_XA) -#else -#define XA_FP 0 -#define XA2_FP 0 -#endif - -#if LJ_SOFTFP32 -#define XA_FP32 CCI_XA -#define XA2_FP32 (CCI_XA+CCI_XA) -#else -#define XA_FP32 0 -#define XA2_FP32 0 -#endif - -#if LJ_32 -#define XA_64 CCI_XA -#define XA2_64 (CCI_XA+CCI_XA) -#else -#define XA_64 0 -#define XA2_64 0 -#endif - -/* Function definitions for CALL* instructions. */ -#define IRCALLDEF(_) \ - _(ANY, lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \ - _(ANY, lj_str_find, 4, N, PGC, 0) \ - _(ANY, lj_str_new, 3, S, STR, CCI_L|CCI_T) \ - _(ANY, lj_strscan_num, 2, FN, INT, 0) \ - _(ANY, lj_strfmt_int, 2, FN, STR, CCI_L|CCI_T) \ - _(ANY, lj_strfmt_num, 2, FN, STR, CCI_L|CCI_T) \ - _(ANY, lj_strfmt_char, 2, FN, STR, CCI_L|CCI_T) \ - _(ANY, lj_strfmt_putint, 2, FL, PGC, CCI_T) \ - _(ANY, lj_strfmt_putnum, 2, FL, PGC, CCI_T) \ - _(ANY, lj_strfmt_putquoted, 2, FL, PGC, CCI_T) \ - _(ANY, lj_strfmt_putfxint, 3, L, PGC, XA_64|CCI_T) \ - _(ANY, lj_strfmt_putfnum_int, 3, L, PGC, XA_FP|CCI_T) \ - _(ANY, lj_strfmt_putfnum_uint, 3, L, PGC, XA_FP|CCI_T) \ - _(ANY, lj_strfmt_putfnum, 3, L, PGC, XA_FP|CCI_T) \ - _(ANY, lj_strfmt_putfstr, 3, L, PGC, CCI_T) \ - _(ANY, lj_strfmt_putfchar, 3, L, PGC, CCI_T) \ - _(ANY, lj_buf_putmem, 3, S, PGC, CCI_T) \ - _(ANY, lj_buf_putstr, 2, FL, PGC, CCI_T) \ - _(ANY, lj_buf_putchar, 2, FL, PGC, CCI_T) \ - _(ANY, lj_buf_putstr_reverse, 2, FL, PGC, CCI_T) \ - _(ANY, lj_buf_putstr_lower, 2, FL, PGC, CCI_T) \ - _(ANY, lj_buf_putstr_upper, 2, FL, PGC, CCI_T) \ - _(ANY, lj_buf_putstr_rep, 3, L, PGC, CCI_T) \ - _(ANY, lj_buf_puttab, 5, L, PGC, CCI_T) \ - _(BUFFER, lj_bufx_set, 4, S, NIL, 0) \ - _(BUFFFI, lj_bufx_more, 2, FS, INT, CCI_T) \ - _(BUFFER, lj_serialize_put, 2, FS, PGC, CCI_T) \ - _(BUFFER, lj_serialize_get, 2, FS, PTR, CCI_T) \ - _(BUFFER, lj_serialize_encode, 2, FA, STR, CCI_L|CCI_T) \ - _(BUFFER, lj_serialize_decode, 3, A, INT, CCI_L|CCI_T) \ - _(ANY, lj_buf_tostr, 1, FL, STR, CCI_T) \ - _(ANY, lj_tab_new_ah, 3, A, TAB, CCI_L|CCI_T) \ - _(ANY, lj_tab_new1, 2, FA, TAB, CCI_L|CCI_T) \ - _(ANY, lj_tab_dup, 2, FA, TAB, CCI_L|CCI_T) \ - _(ANY, lj_tab_clear, 1, FS, NIL, 0) \ - _(ANY, lj_tab_newkey, 3, S, PGC, CCI_L|CCI_T) \ - _(ANY, lj_tab_keyindex, 2, FL, INT, 0) \ - _(ANY, lj_vm_next, 2, FL, PTR, 0) \ - _(ANY, lj_tab_len, 1, FL, INT, 0) \ - _(ANY, lj_tab_len_hint, 2, FL, INT, 0) \ - _(ANY, lj_gc_step_jit, 2, FS, NIL, CCI_L) \ - _(ANY, lj_gc_barrieruv, 2, FS, NIL, 0) \ - _(ANY, lj_mem_newgco, 2, FA, PGC, CCI_L|CCI_T) \ - _(ANY, lj_prng_u64d, 1, FS, NUM, CCI_CASTU64) \ - _(ANY, lj_vm_modi, 2, FN, INT, 0) \ - _(ANY, log10, 1, N, NUM, XA_FP) \ - _(ANY, exp, 1, N, NUM, XA_FP) \ - _(ANY, sin, 1, N, NUM, XA_FP) \ - _(ANY, cos, 1, N, NUM, XA_FP) \ - _(ANY, tan, 1, N, NUM, XA_FP) \ - _(ANY, asin, 1, N, NUM, XA_FP) \ - _(ANY, acos, 1, N, NUM, XA_FP) \ - _(ANY, atan, 1, N, NUM, XA_FP) \ - _(ANY, sinh, 1, N, NUM, XA_FP) \ - _(ANY, cosh, 1, N, NUM, XA_FP) \ - _(ANY, tanh, 1, N, NUM, XA_FP) \ - _(ANY, fputc, 2, S, INT, 0) \ - _(ANY, fwrite, 4, S, INT, 0) \ - _(ANY, fflush, 1, S, INT, 0) \ - /* ORDER FPM */ \ - _(FPMATH, lj_vm_floor, 1, N, NUM, XA_FP) \ - _(FPMATH, lj_vm_ceil, 1, N, NUM, XA_FP) \ - _(FPMATH, lj_vm_trunc, 1, N, NUM, XA_FP) \ - _(FPMATH, sqrt, 1, N, NUM, XA_FP) \ - _(ANY, log, 1, N, NUM, XA_FP) \ - _(ANY, lj_vm_log2, 1, N, NUM, XA_FP) \ - _(ANY, pow, 2, N, NUM, XA2_FP) \ - _(ANY, atan2, 2, N, NUM, XA2_FP) \ - _(ANY, ldexp, 2, N, NUM, XA_FP) \ - _(SOFTFP, lj_vm_tobit, 1, N, INT, XA_FP32) \ - _(SOFTFP, softfp_add, 2, N, NUM, XA2_FP32) \ - _(SOFTFP, softfp_sub, 2, N, NUM, XA2_FP32) \ - _(SOFTFP, softfp_mul, 2, N, NUM, XA2_FP32) \ - _(SOFTFP, softfp_div, 2, N, NUM, XA2_FP32) \ - _(SOFTFP, softfp_cmp, 2, N, NIL, XA2_FP32) \ - _(SOFTFP, softfp_i2d, 1, N, NUM, 0) \ - _(SOFTFP, softfp_d2i, 1, N, INT, XA_FP32) \ - _(SOFTFP_MIPS, lj_vm_sfmin, 2, N, NUM, XA2_FP32) \ - _(SOFTFP_MIPS, lj_vm_sfmax, 2, N, NUM, XA2_FP32) \ - _(SOFTFP_MIPS64, lj_vm_tointg, 1, N, INT, 0) \ - _(SOFTFP_FFI, softfp_ui2d, 1, N, NUM, 0) \ - _(SOFTFP_FFI, softfp_f2d, 1, N, NUM, 0) \ - _(SOFTFP_FFI, softfp_d2ui, 1, N, INT, XA_FP32) \ - _(SOFTFP_FFI, softfp_d2f, 1, N, FLOAT, XA_FP32) \ - _(SOFTFP_FFI, softfp_i2f, 1, N, FLOAT, 0) \ - _(SOFTFP_FFI, softfp_ui2f, 1, N, FLOAT, 0) \ - _(SOFTFP_FFI, softfp_f2i, 1, N, INT, 0) \ - _(SOFTFP_FFI, softfp_f2ui, 1, N, INT, 0) \ - _(FP64_FFI, fp64_l2d, 1, N, NUM, XA_64) \ - _(FP64_FFI, fp64_ul2d, 1, N, NUM, XA_64) \ - _(FP64_FFI, fp64_l2f, 1, N, FLOAT, XA_64) \ - _(FP64_FFI, fp64_ul2f, 1, N, FLOAT, XA_64) \ - _(FP64_FFI, fp64_d2l, 1, N, I64, XA_FP) \ - _(FP64_FFI, fp64_d2ul, 1, N, U64, XA_FP) \ - _(FP64_FFI, fp64_f2l, 1, N, I64, 0) \ - _(FP64_FFI, fp64_f2ul, 1, N, U64, 0) \ - _(FFI, lj_carith_divi64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_divu64, 2, N, U64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_modi64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_modu64, 2, N, U64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_powi64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI, lj_carith_powu64, 2, N, U64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI, lj_cdata_newv, 4, S, CDATA, CCI_L) \ - _(FFI, lj_cdata_setfin, 4, S, NIL, CCI_L) \ - _(FFI, strlen, 1, L, INTP, 0) \ - _(FFI, memcpy, 3, S, PTR, 0) \ - _(FFI, memset, 3, S, PTR, 0) \ - _(FFI, lj_vm_errno, 0, S, INT, CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_mul64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_shl64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_shr64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_sar64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_rol64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ - _(FFI32, lj_carith_ror64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ - \ - /* End of list. */ - -typedef enum { -#define IRCALLENUM(cond, name, nargs, kind, type, flags) IRCALL_##name, -IRCALLDEF(IRCALLENUM) -#undef IRCALLENUM - IRCALL__MAX -} IRCallID; - -LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...); - -LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1]; - -/* Soft-float declarations. */ -#if LJ_SOFTFP -#if LJ_TARGET_ARM -#define softfp_add __aeabi_dadd -#define softfp_sub __aeabi_dsub -#define softfp_mul __aeabi_dmul -#define softfp_div __aeabi_ddiv -#define softfp_cmp __aeabi_cdcmple -#define softfp_i2d __aeabi_i2d -#define softfp_d2i __aeabi_d2iz -#define softfp_ui2d __aeabi_ui2d -#define softfp_f2d __aeabi_f2d -#define softfp_d2ui __aeabi_d2uiz -#define softfp_d2f __aeabi_d2f -#define softfp_i2f __aeabi_i2f -#define softfp_ui2f __aeabi_ui2f -#define softfp_f2i __aeabi_f2iz -#define softfp_f2ui __aeabi_f2uiz -#define fp64_l2d __aeabi_l2d -#define fp64_ul2d __aeabi_ul2d -#define fp64_l2f __aeabi_l2f -#define fp64_ul2f __aeabi_ul2f -#if LJ_TARGET_IOS -#define fp64_d2l __fixdfdi -#define fp64_d2ul __fixunsdfdi -#define fp64_f2l __fixsfdi -#define fp64_f2ul __fixunssfdi -#else -#define fp64_d2l __aeabi_d2lz -#define fp64_d2ul __aeabi_d2ulz -#define fp64_f2l __aeabi_f2lz -#define fp64_f2ul __aeabi_f2ulz -#endif -#elif LJ_TARGET_MIPS || LJ_TARGET_PPC -#define softfp_add __adddf3 -#define softfp_sub __subdf3 -#define softfp_mul __muldf3 -#define softfp_div __divdf3 -#define softfp_cmp __ledf2 -#define softfp_i2d __floatsidf -#define softfp_d2i __fixdfsi -#define softfp_ui2d __floatunsidf -#define softfp_f2d __extendsfdf2 -#define softfp_d2ui __fixunsdfsi -#define softfp_d2f __truncdfsf2 -#define softfp_i2f __floatsisf -#define softfp_ui2f __floatunsisf -#define softfp_f2i __fixsfsi -#define softfp_f2ui __fixunssfsi -#else -#error "Missing soft-float definitions for target architecture" -#endif -extern double softfp_add(double a, double b); -extern double softfp_sub(double a, double b); -extern double softfp_mul(double a, double b); -extern double softfp_div(double a, double b); -extern void softfp_cmp(double a, double b); -extern double softfp_i2d(int32_t a); -extern int32_t softfp_d2i(double a); -#if LJ_HASFFI -extern double softfp_ui2d(uint32_t a); -extern double softfp_f2d(float a); -extern uint32_t softfp_d2ui(double a); -extern float softfp_d2f(double a); -extern float softfp_i2f(int32_t a); -extern float softfp_ui2f(uint32_t a); -extern int32_t softfp_f2i(float a); -extern uint32_t softfp_f2ui(float a); -#endif -#if LJ_TARGET_MIPS -extern double lj_vm_sfmin(double a, double b); -extern double lj_vm_sfmax(double a, double b); -#endif -#endif - -#if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP) -#if defined(__GNUC__) || defined(__clang__) -#define fp64_l2d __floatdidf -#define fp64_ul2d __floatundidf -#define fp64_l2f __floatdisf -#define fp64_ul2f __floatundisf -#define fp64_d2l __fixdfdi -#define fp64_d2ul __fixunsdfdi -#define fp64_f2l __fixsfdi -#define fp64_f2ul __fixunssfdi -#else -#error "Missing fp64 helper definitions for this compiler" -#endif -#endif - -#if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) -extern double fp64_l2d(int64_t a); -extern double fp64_ul2d(uint64_t a); -extern float fp64_l2f(int64_t a); -extern float fp64_ul2f(uint64_t a); -extern int64_t fp64_d2l(double a); -extern uint64_t fp64_d2ul(double a); -extern int64_t fp64_f2l(float a); -extern uint64_t fp64_f2ul(float a); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_iropt.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_iropt.h deleted file mode 100644 index d239f17..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_iropt.h +++ /dev/null @@ -1,162 +0,0 @@ -/* -** Common header for IR emitter and optimizations. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_IROPT_H -#define _LJ_IROPT_H - -#include - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -/* IR emitter. */ -LJ_FUNC void LJ_FASTCALL lj_ir_growtop(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_ir_emit(jit_State *J); - -/* Save current IR in J->fold.ins, but do not emit it (yet). */ -static LJ_AINLINE void lj_ir_set_(jit_State *J, uint16_t ot, IRRef1 a, IRRef1 b) -{ - J->fold.ins.ot = ot; J->fold.ins.op1 = a; J->fold.ins.op2 = b; -} - -#define lj_ir_set(J, ot, a, b) \ - lj_ir_set_(J, (uint16_t)(ot), (IRRef1)(a), (IRRef1)(b)) - -/* Get ref of next IR instruction and optionally grow IR. -** Note: this may invalidate all IRIns*! -*/ -static LJ_AINLINE IRRef lj_ir_nextins(jit_State *J) -{ - IRRef ref = J->cur.nins; - if (LJ_UNLIKELY(ref >= J->irtoplim)) lj_ir_growtop(J); - J->cur.nins = ref + 1; - return ref; -} - -LJ_FUNC TRef lj_ir_ggfload(jit_State *J, IRType t, uintptr_t ofs); - -/* Interning of constants. */ -LJ_FUNC TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k); -LJ_FUNC TRef lj_ir_k64(jit_State *J, IROp op, uint64_t u64); -LJ_FUNC TRef lj_ir_knum_u64(jit_State *J, uint64_t u64); -LJ_FUNC TRef lj_ir_knumint(jit_State *J, lua_Number n); -LJ_FUNC TRef lj_ir_kint64(jit_State *J, uint64_t u64); -LJ_FUNC TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t); -LJ_FUNC TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr); -LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t); -LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot); -LJ_FUNC TRef lj_ir_ktrace(jit_State *J); - -#if LJ_64 -#define lj_ir_kintp(J, k) lj_ir_kint64(J, (uint64_t)(k)) -#else -#define lj_ir_kintp(J, k) lj_ir_kint(J, (int32_t)(k)) -#endif - -static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n) -{ - TValue tv; - tv.n = n; - return lj_ir_knum_u64(J, tv.u64); -} - -#define lj_ir_kstr(J, str) lj_ir_kgc(J, obj2gco((str)), IRT_STR) -#define lj_ir_ktab(J, tab) lj_ir_kgc(J, obj2gco((tab)), IRT_TAB) -#define lj_ir_kfunc(J, func) lj_ir_kgc(J, obj2gco((func)), IRT_FUNC) -#define lj_ir_kptr(J, ptr) lj_ir_kptr_(J, IR_KPTR, (ptr)) -#define lj_ir_kkptr(J, ptr) lj_ir_kptr_(J, IR_KKPTR, (ptr)) - -/* Special FP constants. */ -#define lj_ir_knum_zero(J) lj_ir_knum_u64(J, U64x(00000000,00000000)) -#define lj_ir_knum_one(J) lj_ir_knum_u64(J, U64x(3ff00000,00000000)) -#define lj_ir_knum_tobit(J) lj_ir_knum_u64(J, U64x(43380000,00000000)) - -/* Special 128 bit SIMD constants. */ -#define lj_ir_ksimd(J, idx) \ - lj_ir_ggfload(J, IRT_NUM, (uintptr_t)LJ_KSIMD(J, idx) - (uintptr_t)J2GG(J)) - -/* Access to constants. */ -LJ_FUNC void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir); - -/* Convert IR operand types. */ -LJ_FUNC TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr); -LJ_FUNC TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr); -LJ_FUNC TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr); - -/* Miscellaneous IR ops. */ -LJ_FUNC int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op); -LJ_FUNC int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op); -LJ_FUNC void lj_ir_rollback(jit_State *J, IRRef ref); - -/* Emit IR instructions with on-the-fly optimizations. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_fold(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_cse(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim); - -/* Special return values for the fold functions. */ -enum { - NEXTFOLD, /* Couldn't fold, pass on. */ - RETRYFOLD, /* Retry fold with modified fins. */ - KINTFOLD, /* Return ref for int constant in fins->i. */ - FAILFOLD, /* Guard would always fail. */ - DROPFOLD, /* Guard eliminated. */ - MAX_FOLD -}; - -#define INTFOLD(k) ((J->fold.ins.i = (k)), (TRef)KINTFOLD) -#define INT64FOLD(k) (lj_ir_kint64(J, (k))) -#define CONDFOLD(cond) ((TRef)FAILFOLD + (TRef)(cond)) -#define LEFTFOLD (J->fold.ins.op1) -#define RIGHTFOLD (J->fold.ins.op2) -#define CSEFOLD (lj_opt_cse(J)) -#define EMITFOLD (lj_ir_emit(J)) - -/* Load/store forwarding. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_alen(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J); -LJ_FUNC int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J); -LJ_FUNC int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim); -LJ_FUNC int LJ_FASTCALL lj_opt_fwd_sbuf(jit_State *J, IRRef lim); -LJ_FUNC int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref); - -/* Dead-store elimination. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J); - -/* Narrowing. */ -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J); -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef key); -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr); -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr); -#if LJ_HASFFI -LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef key); -#endif -LJ_FUNC TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, - TValue *vb, TValue *vc, IROp op); -LJ_FUNC TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc); -LJ_FUNC TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vb, TValue *vc); -LJ_FUNC IRType lj_opt_narrow_forl(jit_State *J, cTValue *forbase); - -/* Optimization passes. */ -LJ_FUNC void lj_opt_dce(jit_State *J); -LJ_FUNC int lj_opt_loop(jit_State *J); -#if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI) -LJ_FUNC void lj_opt_split(jit_State *J); -#else -#define lj_opt_split(J) UNUSED(J) -#endif -LJ_FUNC void lj_opt_sink(jit_State *J); - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_jit.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_jit.h deleted file mode 100644 index 7f08173..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_jit.h +++ /dev/null @@ -1,530 +0,0 @@ -/* -** Common definitions for the JIT compiler. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_JIT_H -#define _LJ_JIT_H - -#include "lj_obj.h" -#if LJ_HASJIT -#include "lj_ir.h" - -/* -- JIT engine flags ---------------------------------------------------- */ - -/* General JIT engine flags. 4 bits. */ -#define JIT_F_ON 0x00000001 - -/* CPU-specific JIT engine flags. 12 bits. Flags and strings must match. */ -#define JIT_F_CPU 0x00000010 - -#if LJ_TARGET_X86ORX64 - -#define JIT_F_SSE3 (JIT_F_CPU << 0) -#define JIT_F_SSE4_1 (JIT_F_CPU << 1) -#define JIT_F_BMI2 (JIT_F_CPU << 2) - - -#define JIT_F_CPUSTRING "\4SSE3\6SSE4.1\4BMI2" - -#elif LJ_TARGET_ARM - -#define JIT_F_ARMV6_ (JIT_F_CPU << 0) -#define JIT_F_ARMV6T2_ (JIT_F_CPU << 1) -#define JIT_F_ARMV7 (JIT_F_CPU << 2) -#define JIT_F_ARMV8 (JIT_F_CPU << 3) -#define JIT_F_VFPV2 (JIT_F_CPU << 4) -#define JIT_F_VFPV3 (JIT_F_CPU << 5) - -#define JIT_F_ARMV6 (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7|JIT_F_ARMV8) -#define JIT_F_ARMV6T2 (JIT_F_ARMV6T2_|JIT_F_ARMV7|JIT_F_ARMV8) -#define JIT_F_VFP (JIT_F_VFPV2|JIT_F_VFPV3) - -#define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7\5ARMv8\5VFPv2\5VFPv3" - -#elif LJ_TARGET_PPC - -#define JIT_F_SQRT (JIT_F_CPU << 0) -#define JIT_F_ROUND (JIT_F_CPU << 1) - -#define JIT_F_CPUSTRING "\4SQRT\5ROUND" - -#elif LJ_TARGET_MIPS - -#define JIT_F_MIPSXXR2 (JIT_F_CPU << 0) - -#if LJ_TARGET_MIPS32 -#if LJ_TARGET_MIPSR6 -#define JIT_F_CPUSTRING "\010MIPS32R6" -#else -#define JIT_F_CPUSTRING "\010MIPS32R2" -#endif -#else -#if LJ_TARGET_MIPSR6 -#define JIT_F_CPUSTRING "\010MIPS64R6" -#else -#define JIT_F_CPUSTRING "\010MIPS64R2" -#endif -#endif - -#else - -#define JIT_F_CPUSTRING "" - -#endif - -/* Optimization flags. 12 bits. */ -#define JIT_F_OPT 0x00010000 -#define JIT_F_OPT_MASK 0x0fff0000 - -#define JIT_F_OPT_FOLD (JIT_F_OPT << 0) -#define JIT_F_OPT_CSE (JIT_F_OPT << 1) -#define JIT_F_OPT_DCE (JIT_F_OPT << 2) -#define JIT_F_OPT_FWD (JIT_F_OPT << 3) -#define JIT_F_OPT_DSE (JIT_F_OPT << 4) -#define JIT_F_OPT_NARROW (JIT_F_OPT << 5) -#define JIT_F_OPT_LOOP (JIT_F_OPT << 6) -#define JIT_F_OPT_ABC (JIT_F_OPT << 7) -#define JIT_F_OPT_SINK (JIT_F_OPT << 8) -#define JIT_F_OPT_FUSE (JIT_F_OPT << 9) -#define JIT_F_OPT_FMA (JIT_F_OPT << 10) - -/* Optimizations names for -O. Must match the order above. */ -#define JIT_F_OPTSTRING \ - "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse\3fma" - -/* Optimization levels set a fixed combination of flags. */ -#define JIT_F_OPT_0 0 -#define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE) -#define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP) -#define JIT_F_OPT_3 (JIT_F_OPT_2|\ - JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE) -#define JIT_F_OPT_DEFAULT JIT_F_OPT_3 -/* Note: FMA is not set by default. */ - -/* -- JIT engine parameters ----------------------------------------------- */ - -#if LJ_TARGET_WINDOWS || LJ_64 -/* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */ -#define JIT_P_sizemcode_DEFAULT 64 -#else -/* Could go as low as 4K, but the mmap() overhead would be rather high. */ -#define JIT_P_sizemcode_DEFAULT 32 -#endif - -/* Optimization parameters and their defaults. Length is a char in octal! */ -#define JIT_PARAMDEF(_) \ - _(\010, maxtrace, 1000) /* Max. # of traces in cache. */ \ - _(\011, maxrecord, 4000) /* Max. # of recorded IR instructions. */ \ - _(\012, maxirconst, 500) /* Max. # of IR constants of a trace. */ \ - _(\007, maxside, 100) /* Max. # of side traces of a root trace. */ \ - _(\007, maxsnap, 500) /* Max. # of snapshots for a trace. */ \ - _(\011, minstitch, 0) /* Min. # of IR ins for a stitched trace. */ \ - \ - _(\007, hotloop, 56) /* # of iter. to detect a hot loop/call. */ \ - _(\007, hotexit, 10) /* # of taken exits to start a side trace. */ \ - _(\007, tryside, 4) /* # of attempts to compile a side trace. */ \ - \ - _(\012, instunroll, 4) /* Max. unroll for instable loops. */ \ - _(\012, loopunroll, 15) /* Max. unroll for loop ops in side traces. */ \ - _(\012, callunroll, 3) /* Max. unroll for recursive calls. */ \ - _(\011, recunroll, 2) /* Min. unroll for true recursion. */ \ - \ - /* Size of each machine code area (in KBytes). */ \ - _(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \ - /* Max. total size of all machine code areas (in KBytes). */ \ - _(\010, maxmcode, 512) \ - /* End of list. */ - -enum { -#define JIT_PARAMENUM(len, name, value) JIT_P_##name, -JIT_PARAMDEF(JIT_PARAMENUM) -#undef JIT_PARAMENUM - JIT_P__MAX -}; - -#define JIT_PARAMSTR(len, name, value) #len #name -#define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR) - -/* -- JIT engine data structures ------------------------------------------ */ - -/* Trace compiler state. */ -typedef enum { - LJ_TRACE_IDLE, /* Trace compiler idle. */ - LJ_TRACE_ACTIVE = 0x10, - LJ_TRACE_RECORD, /* Bytecode recording active. */ - LJ_TRACE_RECORD_1ST, /* Record 1st instruction, too. */ - LJ_TRACE_START, /* New trace started. */ - LJ_TRACE_END, /* End of trace. */ - LJ_TRACE_ASM, /* Assemble trace. */ - LJ_TRACE_ERR /* Trace aborted with error. */ -} TraceState; - -/* Post-processing action. */ -typedef enum { - LJ_POST_NONE, /* No action. */ - LJ_POST_FIXCOMP, /* Fixup comparison and emit pending guard. */ - LJ_POST_FIXGUARD, /* Fixup and emit pending guard. */ - LJ_POST_FIXGUARDSNAP, /* Fixup and emit pending guard and snapshot. */ - LJ_POST_FIXBOOL, /* Fixup boolean result. */ - LJ_POST_FIXCONST, /* Fixup constant results. */ - LJ_POST_FFRETRY /* Suppress recording of retried fast functions. */ -} PostProc; - -/* Machine code type. */ -#if LJ_TARGET_X86ORX64 -typedef uint8_t MCode; -#else -typedef uint32_t MCode; -#endif - -/* Linked list of MCode areas. */ -typedef struct MCLink { - MCode *next; /* Next area. */ - size_t size; /* Size of current area. */ -} MCLink; - -/* Stack snapshot header. */ -typedef struct SnapShot { - uint32_t mapofs; /* Offset into snapshot map. */ - IRRef1 ref; /* First IR ref for this snapshot. */ - uint16_t mcofs; /* Offset into machine code in MCode units. */ - uint8_t nslots; /* Number of valid slots. */ - uint8_t topslot; /* Maximum frame extent. */ - uint8_t nent; /* Number of compressed entries. */ - uint8_t count; /* Count of taken exits for this snapshot. */ -} SnapShot; - -#define SNAPCOUNT_DONE 255 /* Already compiled and linked a side trace. */ - -/* Compressed snapshot entry. */ -typedef uint32_t SnapEntry; - -#define SNAP_FRAME 0x010000 /* Frame slot. */ -#define SNAP_CONT 0x020000 /* Continuation slot. */ -#define SNAP_NORESTORE 0x040000 /* No need to restore slot. */ -#define SNAP_SOFTFPNUM 0x080000 /* Soft-float number. */ -#define SNAP_KEYINDEX 0x100000 /* Traversal key index. */ -LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME); -LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT); -LJ_STATIC_ASSERT(SNAP_KEYINDEX == TREF_KEYINDEX); - -#define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref)) -#define SNAP_TR(slot, tr) \ - (((SnapEntry)(slot) << 24) + \ - ((tr) & (TREF_KEYINDEX|TREF_CONT|TREF_FRAME|TREF_REFMASK))) -#if !LJ_FR2 -#define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc)) -#endif -#define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz)) -#define snap_ref(sn) ((sn) & 0xffff) -#define snap_slot(sn) ((BCReg)((sn) >> 24)) -#define snap_isframe(sn) ((sn) & SNAP_FRAME) -#define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref)) - -static LJ_AINLINE const BCIns *snap_pc(SnapEntry *sn) -{ -#if LJ_FR2 - uint64_t pcbase; - memcpy(&pcbase, sn, sizeof(uint64_t)); - return (const BCIns *)(pcbase >> 8); -#else - return (const BCIns *)(uintptr_t)*sn; -#endif -} - -/* Snapshot and exit numbers. */ -typedef uint32_t SnapNo; -typedef uint32_t ExitNo; - -/* Trace number. */ -typedef uint32_t TraceNo; /* Used to pass around trace numbers. */ -typedef uint16_t TraceNo1; /* Stored trace number. */ - -/* Type of link. ORDER LJ_TRLINK */ -typedef enum { - LJ_TRLINK_NONE, /* Incomplete trace. No link, yet. */ - LJ_TRLINK_ROOT, /* Link to other root trace. */ - LJ_TRLINK_LOOP, /* Loop to same trace. */ - LJ_TRLINK_TAILREC, /* Tail-recursion. */ - LJ_TRLINK_UPREC, /* Up-recursion. */ - LJ_TRLINK_DOWNREC, /* Down-recursion. */ - LJ_TRLINK_INTERP, /* Fallback to interpreter. */ - LJ_TRLINK_RETURN, /* Return to interpreter. */ - LJ_TRLINK_STITCH /* Trace stitching. */ -} TraceLink; - -/* Trace object. */ -typedef struct GCtrace { - GCHeader; - uint16_t nsnap; /* Number of snapshots. */ - IRRef nins; /* Next IR instruction. Biased with REF_BIAS. */ -#if LJ_GC64 - uint32_t unused_gc64; -#endif - GCRef gclist; - IRIns *ir; /* IR instructions/constants. Biased with REF_BIAS. */ - IRRef nk; /* Lowest IR constant. Biased with REF_BIAS. */ - uint32_t nsnapmap; /* Number of snapshot map elements. */ - SnapShot *snap; /* Snapshot array. */ - SnapEntry *snapmap; /* Snapshot map. */ - GCRef startpt; /* Starting prototype. */ - MRef startpc; /* Bytecode PC of starting instruction. */ - BCIns startins; /* Original bytecode of starting instruction. */ - MSize szmcode; /* Size of machine code. */ - MCode *mcode; /* Start of machine code. */ - MSize mcloop; /* Offset of loop start in machine code. */ - uint16_t nchild; /* Number of child traces (root trace only). */ - uint16_t spadjust; /* Stack pointer adjustment (offset in bytes). */ - TraceNo1 traceno; /* Trace number. */ - TraceNo1 link; /* Linked trace (or self for loops). */ - TraceNo1 root; /* Root trace of side trace (or 0 for root traces). */ - TraceNo1 nextroot; /* Next root trace for same prototype. */ - TraceNo1 nextside; /* Next side trace of same root trace. */ - uint8_t sinktags; /* Trace has SINK tags. */ - uint8_t topslot; /* Top stack slot already checked to be allocated. */ - uint8_t linktype; /* Type of link. */ - uint8_t unused1; -#ifdef LUAJIT_USE_GDBJIT - void *gdbjit_entry; /* GDB JIT entry. */ -#endif -} GCtrace; - -#define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o)) -#define traceref(J, n) \ - check_exp((n)>0 && (MSize)(n)sizetrace, (GCtrace *)gcref(J->trace[(n)])) - -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist)); - -static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap) -{ - if (snap+1 == &T->snap[T->nsnap]) - return T->nsnapmap; - else - return (snap+1)->mapofs; -} - -/* Round-robin penalty cache for bytecodes leading to aborted traces. */ -typedef struct HotPenalty { - MRef pc; /* Starting bytecode PC. */ - uint16_t val; /* Penalty value, i.e. hotcount start. */ - uint16_t reason; /* Abort reason (really TraceErr). */ -} HotPenalty; - -#define PENALTY_SLOTS 64 /* Penalty cache slot. Must be a power of 2. */ -#define PENALTY_MIN (36*2) /* Minimum penalty value. */ -#define PENALTY_MAX 60000 /* Maximum penalty value. */ -#define PENALTY_RNDBITS 4 /* # of random bits to add to penalty value. */ - -/* Round-robin backpropagation cache for narrowing conversions. */ -typedef struct BPropEntry { - IRRef1 key; /* Key: original reference. */ - IRRef1 val; /* Value: reference after conversion. */ - IRRef mode; /* Mode for this entry (currently IRCONV_*). */ -} BPropEntry; - -/* Number of slots for the backpropagation cache. Must be a power of 2. */ -#define BPROP_SLOTS 16 - -/* Scalar evolution analysis cache. */ -typedef struct ScEvEntry { - MRef pc; /* Bytecode PC of FORI. */ - IRRef1 idx; /* Index reference. */ - IRRef1 start; /* Constant start reference. */ - IRRef1 stop; /* Constant stop reference. */ - IRRef1 step; /* Constant step reference. */ - IRType1 t; /* Scalar type. */ - uint8_t dir; /* Direction. 1: +, 0: -. */ -} ScEvEntry; - -/* Reverse bytecode map (IRRef -> PC). Only for selected instructions. */ -typedef struct RBCHashEntry { - MRef pc; /* Bytecode PC. */ - GCRef pt; /* Prototype. */ - IRRef ref; /* IR reference. */ -} RBCHashEntry; - -/* Number of slots in the reverse bytecode hash table. Must be a power of 2. */ -#define RBCHASH_SLOTS 8 - -/* 128 bit SIMD constants. */ -enum { - LJ_KSIMD_ABS, - LJ_KSIMD_NEG, - LJ_KSIMD__MAX -}; - -enum { -#if LJ_TARGET_X86ORX64 - LJ_K64_TOBIT, /* 2^52 + 2^51 */ - LJ_K64_2P64, /* 2^64 */ - LJ_K64_M2P64, /* -2^64 */ -#if LJ_32 - LJ_K64_M2P64_31, /* -2^64 or -2^31 */ -#else - LJ_K64_M2P64_31 = LJ_K64_M2P64, -#endif -#endif -#if LJ_TARGET_MIPS - LJ_K64_2P31, /* 2^31 */ -#if LJ_64 - LJ_K64_2P63, /* 2^63 */ - LJ_K64_M2P64, /* -2^64 */ -#endif -#endif - LJ_K64__MAX, -}; -#define LJ_K64__USED (LJ_TARGET_X86ORX64 || LJ_TARGET_MIPS) - -enum { -#if LJ_TARGET_X86ORX64 - LJ_K32_M2P64_31, /* -2^64 or -2^31 */ -#endif -#if LJ_TARGET_PPC - LJ_K32_2P52_2P31, /* 2^52 + 2^31 */ - LJ_K32_2P52, /* 2^52 */ -#endif -#if LJ_TARGET_PPC || LJ_TARGET_MIPS - LJ_K32_2P31, /* 2^31 */ -#endif -#if LJ_TARGET_MIPS64 - LJ_K32_2P63, /* 2^63 */ - LJ_K32_M2P64, /* -2^64 */ -#endif - LJ_K32__MAX -}; -#define LJ_K32__USED (LJ_TARGET_X86ORX64 || LJ_TARGET_PPC || LJ_TARGET_MIPS) - -/* Get 16 byte aligned pointer to SIMD constant. */ -#define LJ_KSIMD(J, n) \ - ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15)) - -/* Set/reset flag to activate the SPLIT pass for the current trace. */ -#if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI) -#define lj_needsplit(J) (J->needsplit = 1) -#define lj_resetsplit(J) (J->needsplit = 0) -#else -#define lj_needsplit(J) UNUSED(J) -#define lj_resetsplit(J) UNUSED(J) -#endif - -/* Fold state is used to fold instructions on-the-fly. */ -typedef struct FoldState { - IRIns ins; /* Currently emitted instruction. */ - IRIns left[2]; /* Instruction referenced by left operand. */ - IRIns right[2]; /* Instruction referenced by right operand. */ -} FoldState; - -/* JIT compiler state. */ -typedef struct jit_State { - GCtrace cur; /* Current trace. */ - GCtrace *curfinal; /* Final address of current trace (set during asm). */ - - lua_State *L; /* Current Lua state. */ - const BCIns *pc; /* Current PC. */ - GCfunc *fn; /* Current function. */ - GCproto *pt; /* Current prototype. */ - TRef *base; /* Current frame base, points into J->slots. */ - - uint32_t flags; /* JIT engine flags. */ - BCReg maxslot; /* Relative to baseslot. */ - BCReg baseslot; /* Current frame base, offset into J->slots. */ - - uint8_t mergesnap; /* Allowed to merge with next snapshot. */ - uint8_t needsnap; /* Need snapshot before recording next bytecode. */ - IRType1 guardemit; /* Accumulated IRT_GUARD for emitted instructions. */ - uint8_t bcskip; /* Number of bytecode instructions to skip. */ - - FoldState fold; /* Fold state. */ - - const BCIns *bc_min; /* Start of allowed bytecode range for root trace. */ - MSize bc_extent; /* Extent of the range. */ - - TraceState state; /* Trace compiler state. */ - - int32_t instunroll; /* Unroll counter for instable loops. */ - int32_t loopunroll; /* Unroll counter for loop ops in side traces. */ - int32_t tailcalled; /* Number of successive tailcalls. */ - int32_t framedepth; /* Current frame depth. */ - int32_t retdepth; /* Return frame depth (count of RETF). */ - -#if LJ_K32__USED - uint32_t k32[LJ_K32__MAX]; /* Common 4 byte constants used by backends. */ -#endif - TValue ksimd[LJ_KSIMD__MAX*2+1]; /* 16 byte aligned SIMD constants. */ -#if LJ_K64__USED - TValue k64[LJ_K64__MAX]; /* Common 8 byte constants. */ -#endif - - IRIns *irbuf; /* Temp. IR instruction buffer. Biased with REF_BIAS. */ - IRRef irtoplim; /* Upper limit of instuction buffer (biased). */ - IRRef irbotlim; /* Lower limit of instuction buffer (biased). */ - IRRef loopref; /* Last loop reference or ref of final LOOP (or 0). */ - - MSize sizesnap; /* Size of temp. snapshot buffer. */ - SnapShot *snapbuf; /* Temp. snapshot buffer. */ - SnapEntry *snapmapbuf; /* Temp. snapshot map buffer. */ - MSize sizesnapmap; /* Size of temp. snapshot map buffer. */ - - PostProc postproc; /* Required post-processing after execution. */ -#if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI) - uint8_t needsplit; /* Need SPLIT pass. */ -#endif - uint8_t retryrec; /* Retry recording. */ - - GCRef *trace; /* Array of traces. */ - TraceNo freetrace; /* Start of scan for next free trace. */ - MSize sizetrace; /* Size of trace array. */ - IRRef1 ktrace; /* Reference to KGC with GCtrace. */ - - IRRef1 chain[IR__MAX]; /* IR instruction skip-list chain anchors. */ - TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; /* Stack slot map. */ - - int32_t param[JIT_P__MAX]; /* JIT engine parameters. */ - - MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; /* Exit stub group addresses. */ - - HotPenalty penalty[PENALTY_SLOTS]; /* Penalty slots. */ - uint32_t penaltyslot; /* Round-robin index into penalty slots. */ - -#ifdef LUAJIT_ENABLE_TABLE_BUMP - RBCHashEntry rbchash[RBCHASH_SLOTS]; /* Reverse bytecode map. */ -#endif - - BPropEntry bpropcache[BPROP_SLOTS]; /* Backpropagation cache slots. */ - uint32_t bpropslot; /* Round-robin index into bpropcache slots. */ - - ScEvEntry scev; /* Scalar evolution analysis cache slots. */ - - const BCIns *startpc; /* Bytecode PC of starting instruction. */ - TraceNo parent; /* Parent of current side trace (0 for root traces). */ - ExitNo exitno; /* Exit number in parent of current side trace. */ - int exitcode; /* Exit code from unwound trace. */ - - BCIns *patchpc; /* PC for pending re-patch. */ - BCIns patchins; /* Instruction for pending re-patch. */ - - int mcprot; /* Protection of current mcode area. */ - MCode *mcarea; /* Base of current mcode area. */ - MCode *mctop; /* Top of current mcode area. */ - MCode *mcbot; /* Bottom of current mcode area. */ - size_t szmcarea; /* Size of current mcode area. */ - size_t szallmcarea; /* Total size of all allocated mcode areas. */ - - TValue errinfo; /* Additional info element for trace errors. */ - -#if LJ_HASPROFILE - GCproto *prev_pt; /* Previous prototype. */ - BCLine prev_line; /* Previous line. */ - int prof_mode; /* Profiling mode: 0, 'f', 'l'. */ -#endif -} jit_State; - -#ifdef LUA_USE_ASSERT -#define lj_assertJ(c, ...) lj_assertG_(J2G(J), (c), __VA_ARGS__) -#else -#define lj_assertJ(c, ...) ((void)J) -#endif -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.c deleted file mode 100644 index 463a87c..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.c +++ /dev/null @@ -1,514 +0,0 @@ -/* -** Lexical analyzer. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_lex_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#if LJ_HASFFI -#include "lj_tab.h" -#include "lj_ctype.h" -#include "lj_cdata.h" -#include "lualib.h" -#endif -#include "lj_state.h" -#include "lj_lex.h" -#include "lj_parse.h" -#include "lj_char.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" - -/* Lua lexer token names. */ -static const char *const tokennames[] = { -#define TKSTR1(name) #name, -#define TKSTR2(name, sym) #sym, -TKDEF(TKSTR1, TKSTR2) -#undef TKSTR1 -#undef TKSTR2 - NULL -}; - -/* -- Buffer handling ----------------------------------------------------- */ - -#define LEX_EOF (-1) -#define lex_iseol(ls) (ls->c == '\n' || ls->c == '\r') - -/* Get more input from reader. */ -static LJ_NOINLINE LexChar lex_more(LexState *ls) -{ - size_t sz; - const char *p = ls->rfunc(ls->L, ls->rdata, &sz); - if (p == NULL || sz == 0) return LEX_EOF; - if (sz >= LJ_MAX_BUF) { - if (sz != ~(size_t)0) lj_err_mem(ls->L); - sz = ~(uintptr_t)0 - (uintptr_t)p; - if (sz >= LJ_MAX_BUF) sz = LJ_MAX_BUF-1; - ls->endmark = 1; - } - ls->pe = p + sz; - ls->p = p + 1; - return (LexChar)(uint8_t)p[0]; -} - -/* Get next character. */ -static LJ_AINLINE LexChar lex_next(LexState *ls) -{ - return (ls->c = ls->p < ls->pe ? (LexChar)(uint8_t)*ls->p++ : lex_more(ls)); -} - -/* Save character. */ -static LJ_AINLINE void lex_save(LexState *ls, LexChar c) -{ - lj_buf_putb(&ls->sb, c); -} - -/* Save previous character and get next character. */ -static LJ_AINLINE LexChar lex_savenext(LexState *ls) -{ - lex_save(ls, ls->c); - return lex_next(ls); -} - -/* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ -static void lex_newline(LexState *ls) -{ - LexChar old = ls->c; - lj_assertLS(lex_iseol(ls), "bad usage"); - lex_next(ls); /* Skip "\n" or "\r". */ - if (lex_iseol(ls) && ls->c != old) lex_next(ls); /* Skip "\n\r" or "\r\n". */ - if (++ls->linenumber >= LJ_MAX_LINE) - lj_lex_error(ls, ls->tok, LJ_ERR_XLINES); -} - -/* -- Scanner for terminals ----------------------------------------------- */ - -/* Parse a number literal. */ -static void lex_number(LexState *ls, TValue *tv) -{ - StrScanFmt fmt; - LexChar c, xp = 'e'; - lj_assertLS(lj_char_isdigit(ls->c), "bad usage"); - if ((c = ls->c) == '0' && (lex_savenext(ls) | 0x20) == 'x') - xp = 'p'; - while (lj_char_isident(ls->c) || ls->c == '.' || - ((ls->c == '-' || ls->c == '+') && (c | 0x20) == xp)) { - c = ls->c; - lex_savenext(ls); - } - lex_save(ls, '\0'); - fmt = lj_strscan_scan((const uint8_t *)ls->sb.b, sbuflen(&ls->sb)-1, tv, - (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | - (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); - if (LJ_DUALNUM && fmt == STRSCAN_INT) { - setitype(tv, LJ_TISNUM); - } else if (fmt == STRSCAN_NUM) { - /* Already in correct format. */ -#if LJ_HASFFI - } else if (fmt != STRSCAN_ERROR) { - lua_State *L = ls->L; - GCcdata *cd; - lj_assertLS(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG, - "unexpected number format %d", fmt); - ctype_loadffi(L); - if (fmt == STRSCAN_IMAG) { - cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); - ((double *)cdataptr(cd))[0] = 0; - ((double *)cdataptr(cd))[1] = numV(tv); - } else { - cd = lj_cdata_new_(L, fmt==STRSCAN_I64 ? CTID_INT64 : CTID_UINT64, 8); - *(uint64_t *)cdataptr(cd) = tv->u64; - } - lj_parse_keepcdata(ls, tv, cd); -#endif - } else { - lj_assertLS(fmt == STRSCAN_ERROR, - "unexpected number format %d", fmt); - lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER); - } -} - -/* Skip equal signs for "[=...=[" and "]=...=]" and return their count. */ -static int lex_skipeq(LexState *ls) -{ - int count = 0; - LexChar s = ls->c; - lj_assertLS(s == '[' || s == ']', "bad usage"); - while (lex_savenext(ls) == '=' && count < 0x20000000) - count++; - return (ls->c == s) ? count : (-count) - 1; -} - -/* Parse a long string or long comment (tv set to NULL). */ -static void lex_longstring(LexState *ls, TValue *tv, int sep) -{ - lex_savenext(ls); /* Skip second '['. */ - if (lex_iseol(ls)) /* Skip initial newline. */ - lex_newline(ls); - for (;;) { - switch (ls->c) { - case LEX_EOF: - lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM); - break; - case ']': - if (lex_skipeq(ls) == sep) { - lex_savenext(ls); /* Skip second ']'. */ - goto endloop; - } - break; - case '\n': - case '\r': - lex_save(ls, '\n'); - lex_newline(ls); - if (!tv) lj_buf_reset(&ls->sb); /* Don't waste space for comments. */ - break; - default: - lex_savenext(ls); - break; - } - } endloop: - if (tv) { - GCstr *str = lj_parse_keepstr(ls, ls->sb.b + (2 + (MSize)sep), - sbuflen(&ls->sb) - 2*(2 + (MSize)sep)); - setstrV(ls->L, tv, str); - } -} - -/* Parse a string. */ -static void lex_string(LexState *ls, TValue *tv) -{ - LexChar delim = ls->c; /* Delimiter is '\'' or '"'. */ - lex_savenext(ls); - while (ls->c != delim) { - switch (ls->c) { - case LEX_EOF: - lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); - continue; - case '\n': - case '\r': - lj_lex_error(ls, TK_string, LJ_ERR_XSTR); - continue; - case '\\': { - LexChar c = lex_next(ls); /* Skip the '\\'. */ - switch (c) { - case 'a': c = '\a'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 'r': c = '\r'; break; - case 't': c = '\t'; break; - case 'v': c = '\v'; break; - case 'x': /* Hexadecimal escape '\xXX'. */ - c = (lex_next(ls) & 15u) << 4; - if (!lj_char_isdigit(ls->c)) { - if (!lj_char_isxdigit(ls->c)) goto err_xesc; - c += 9 << 4; - } - c += (lex_next(ls) & 15u); - if (!lj_char_isdigit(ls->c)) { - if (!lj_char_isxdigit(ls->c)) goto err_xesc; - c += 9; - } - break; - case 'u': /* Unicode escape '\u{XX...}'. */ - if (lex_next(ls) != '{') goto err_xesc; - lex_next(ls); - c = 0; - do { - c = (c << 4) | (ls->c & 15u); - if (!lj_char_isdigit(ls->c)) { - if (!lj_char_isxdigit(ls->c)) goto err_xesc; - c += 9; - } - if (c >= 0x110000) goto err_xesc; /* Out of Unicode range. */ - } while (lex_next(ls) != '}'); - if (c < 0x800) { - if (c < 0x80) break; - lex_save(ls, 0xc0 | (c >> 6)); - } else { - if (c >= 0x10000) { - lex_save(ls, 0xf0 | (c >> 18)); - lex_save(ls, 0x80 | ((c >> 12) & 0x3f)); - } else { - if (c >= 0xd800 && c < 0xe000) goto err_xesc; /* No surrogates. */ - lex_save(ls, 0xe0 | (c >> 12)); - } - lex_save(ls, 0x80 | ((c >> 6) & 0x3f)); - } - c = 0x80 | (c & 0x3f); - break; - case 'z': /* Skip whitespace. */ - lex_next(ls); - while (lj_char_isspace(ls->c)) - if (lex_iseol(ls)) lex_newline(ls); else lex_next(ls); - continue; - case '\n': case '\r': lex_save(ls, '\n'); lex_newline(ls); continue; - case '\\': case '\"': case '\'': break; - case LEX_EOF: continue; - default: - if (!lj_char_isdigit(c)) - goto err_xesc; - c -= '0'; /* Decimal escape '\ddd'. */ - if (lj_char_isdigit(lex_next(ls))) { - c = c*10 + (ls->c - '0'); - if (lj_char_isdigit(lex_next(ls))) { - c = c*10 + (ls->c - '0'); - if (c > 255) { - err_xesc: - lj_lex_error(ls, TK_string, LJ_ERR_XESC); - } - lex_next(ls); - } - } - lex_save(ls, c); - continue; - } - lex_save(ls, c); - lex_next(ls); - continue; - } - default: - lex_savenext(ls); - break; - } - } - lex_savenext(ls); /* Skip trailing delimiter. */ - setstrV(ls->L, tv, - lj_parse_keepstr(ls, ls->sb.b+1, sbuflen(&ls->sb)-2)); -} - -/* -- Main lexical scanner ------------------------------------------------ */ - -/* Get next lexical token. */ -static LexToken lex_scan(LexState *ls, TValue *tv) -{ - lj_buf_reset(&ls->sb); - for (;;) { - if (lj_char_isident(ls->c)) { - GCstr *s; - if (lj_char_isdigit(ls->c)) { /* Numeric literal. */ - lex_number(ls, tv); - return TK_number; - } - /* Identifier or reserved word. */ - do { - lex_savenext(ls); - } while (lj_char_isident(ls->c)); - s = lj_parse_keepstr(ls, ls->sb.b, sbuflen(&ls->sb)); - setstrV(ls->L, tv, s); - if (s->reserved > 0) /* Reserved word? */ - return TK_OFS + s->reserved; - return TK_name; - } - switch (ls->c) { - case '\n': - case '\r': - lex_newline(ls); - continue; - case ' ': - case '\t': - case '\v': - case '\f': - lex_next(ls); - continue; - case '-': - lex_next(ls); - if (ls->c != '-') return '-'; - lex_next(ls); - if (ls->c == '[') { /* Long comment "--[=*[...]=*]". */ - int sep = lex_skipeq(ls); - lj_buf_reset(&ls->sb); /* `lex_skipeq' may dirty the buffer */ - if (sep >= 0) { - lex_longstring(ls, NULL, sep); - lj_buf_reset(&ls->sb); - continue; - } - } - /* Short comment "--.*\n". */ - while (!lex_iseol(ls) && ls->c != LEX_EOF) - lex_next(ls); - continue; - case '[': { - int sep = lex_skipeq(ls); - if (sep >= 0) { - lex_longstring(ls, tv, sep); - return TK_string; - } else if (sep == -1) { - return '['; - } else { - lj_lex_error(ls, TK_string, LJ_ERR_XLDELIM); - continue; - } - } - case '=': - lex_next(ls); - if (ls->c != '=') return '='; else { lex_next(ls); return TK_eq; } - case '<': - lex_next(ls); - if (ls->c != '=') return '<'; else { lex_next(ls); return TK_le; } - case '>': - lex_next(ls); - if (ls->c != '=') return '>'; else { lex_next(ls); return TK_ge; } - case '~': - lex_next(ls); - if (ls->c != '=') return '~'; else { lex_next(ls); return TK_ne; } - case ':': - lex_next(ls); - if (ls->c != ':') return ':'; else { lex_next(ls); return TK_label; } - case '"': - case '\'': - lex_string(ls, tv); - return TK_string; - case '.': - if (lex_savenext(ls) == '.') { - lex_next(ls); - if (ls->c == '.') { - lex_next(ls); - return TK_dots; /* ... */ - } - return TK_concat; /* .. */ - } else if (!lj_char_isdigit(ls->c)) { - return '.'; - } else { - lex_number(ls, tv); - return TK_number; - } - case LEX_EOF: - return TK_eof; - default: { - LexChar c = ls->c; - lex_next(ls); - return c; /* Single-char tokens (+ - / ...). */ - } - } - } -} - -/* -- Lexer API ----------------------------------------------------------- */ - -/* Setup lexer state. */ -int lj_lex_setup(lua_State *L, LexState *ls) -{ - int header = 0; - ls->L = L; - ls->fs = NULL; - ls->pe = ls->p = NULL; - ls->vstack = NULL; - ls->sizevstack = 0; - ls->vtop = 0; - ls->bcstack = NULL; - ls->sizebcstack = 0; - ls->tok = 0; - ls->lookahead = TK_eof; /* No look-ahead token. */ - ls->linenumber = 1; - ls->lastline = 1; - ls->endmark = 0; - lex_next(ls); /* Read-ahead first char. */ - if (ls->c == 0xef && ls->p + 2 <= ls->pe && (uint8_t)ls->p[0] == 0xbb && - (uint8_t)ls->p[1] == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ - ls->p += 2; - lex_next(ls); - header = 1; - } - if (ls->c == '#') { /* Skip POSIX #! header line. */ - do { - lex_next(ls); - if (ls->c == LEX_EOF) return 0; - } while (!lex_iseol(ls)); - lex_newline(ls); - header = 1; - } - if (ls->c == LUA_SIGNATURE[0]) { /* Bytecode dump. */ - if (header) { - /* - ** Loading bytecode with an extra header is disabled for security - ** reasons. This may circumvent the usual check for bytecode vs. - ** Lua code by looking at the first char. Since this is a potential - ** security violation no attempt is made to echo the chunkname either. - */ - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCBAD)); - lj_err_throw(L, LUA_ERRSYNTAX); - } - return 1; - } - return 0; -} - -/* Cleanup lexer state. */ -void lj_lex_cleanup(lua_State *L, LexState *ls) -{ - global_State *g = G(L); - lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine); - lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo); - lj_buf_free(g, &ls->sb); -} - -/* Return next lexical token. */ -void lj_lex_next(LexState *ls) -{ - ls->lastline = ls->linenumber; - if (LJ_LIKELY(ls->lookahead == TK_eof)) { /* No lookahead token? */ - ls->tok = lex_scan(ls, &ls->tokval); /* Get next token. */ - } else { /* Otherwise return lookahead token. */ - ls->tok = ls->lookahead; - ls->lookahead = TK_eof; - ls->tokval = ls->lookaheadval; - } -} - -/* Look ahead for the next token. */ -LexToken lj_lex_lookahead(LexState *ls) -{ - lj_assertLS(ls->lookahead == TK_eof, "double lookahead"); - ls->lookahead = lex_scan(ls, &ls->lookaheadval); - return ls->lookahead; -} - -/* Convert token to string. */ -const char *lj_lex_token2str(LexState *ls, LexToken tok) -{ - if (tok > TK_OFS) - return tokennames[tok-TK_OFS-1]; - else if (!lj_char_iscntrl(tok)) - return lj_strfmt_pushf(ls->L, "%c", tok); - else - return lj_strfmt_pushf(ls->L, "char(%d)", tok); -} - -/* Lexer error. */ -void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...) -{ - const char *tokstr; - va_list argp; - if (tok == 0) { - tokstr = NULL; - } else if (tok == TK_name || tok == TK_string || tok == TK_number) { - lex_save(ls, '\0'); - tokstr = ls->sb.b; - } else { - tokstr = lj_lex_token2str(ls, tok); - } - va_start(argp, em); - lj_err_lex(ls->L, ls->chunkname, tokstr, ls->linenumber, em, argp); - va_end(argp); -} - -/* Initialize strings for reserved words. */ -void lj_lex_init(lua_State *L) -{ - uint32_t i; - for (i = 0; i < TK_RESERVED; i++) { - GCstr *s = lj_str_newz(L, tokennames[i]); - fixstring(s); /* Reserved words are never collected. */ - s->reserved = (uint8_t)(i+1); - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.h deleted file mode 100644 index cb5b576..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lex.h +++ /dev/null @@ -1,93 +0,0 @@ -/* -** Lexical analyzer. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_LEX_H -#define _LJ_LEX_H - -#include - -#include "lj_obj.h" -#include "lj_err.h" - -/* Lua lexer tokens. */ -#define TKDEF(_, __) \ - _(and) _(break) _(do) _(else) _(elseif) _(end) _(false) \ - _(for) _(function) _(goto) _(if) _(in) _(local) _(nil) _(not) _(or) \ - _(repeat) _(return) _(then) _(true) _(until) _(while) \ - __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) __(ne, ~=) \ - __(label, ::) __(number, ) __(name, ) __(string, ) \ - __(eof, ) - -enum { - TK_OFS = 256, -#define TKENUM1(name) TK_##name, -#define TKENUM2(name, sym) TK_##name, -TKDEF(TKENUM1, TKENUM2) -#undef TKENUM1 -#undef TKENUM2 - TK_RESERVED = TK_while - TK_OFS -}; - -typedef int LexChar; /* Lexical character. Unsigned ext. from char. */ -typedef int LexToken; /* Lexical token. */ - -/* Combined bytecode ins/line. Only used during bytecode generation. */ -typedef struct BCInsLine { - BCIns ins; /* Bytecode instruction. */ - BCLine line; /* Line number for this bytecode. */ -} BCInsLine; - -/* Info for local variables. Only used during bytecode generation. */ -typedef struct VarInfo { - GCRef name; /* Local variable name or goto/label name. */ - BCPos startpc; /* First point where the local variable is active. */ - BCPos endpc; /* First point where the local variable is dead. */ - uint8_t slot; /* Variable slot. */ - uint8_t info; /* Variable/goto/label info. */ -} VarInfo; - -/* Lua lexer state. */ -typedef struct LexState { - struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */ - struct lua_State *L; /* Lua state. */ - TValue tokval; /* Current token value. */ - TValue lookaheadval; /* Lookahead token value. */ - const char *p; /* Current position in input buffer. */ - const char *pe; /* End of input buffer. */ - LexChar c; /* Current character. */ - LexToken tok; /* Current token. */ - LexToken lookahead; /* Lookahead token. */ - SBuf sb; /* String buffer for tokens. */ - lua_Reader rfunc; /* Reader callback. */ - void *rdata; /* Reader callback data. */ - BCLine linenumber; /* Input line counter. */ - BCLine lastline; /* Line of last token. */ - GCstr *chunkname; /* Current chunk name (interned string). */ - const char *chunkarg; /* Chunk name argument. */ - const char *mode; /* Allow loading bytecode (b) and/or source text (t). */ - VarInfo *vstack; /* Stack for names and extents of local variables. */ - MSize sizevstack; /* Size of variable stack. */ - MSize vtop; /* Top of variable stack. */ - BCInsLine *bcstack; /* Stack for bytecode instructions/line numbers. */ - MSize sizebcstack; /* Size of bytecode stack. */ - uint32_t level; /* Syntactical nesting level. */ - int endmark; /* Trust bytecode end marker, even if not at EOF. */ -} LexState; - -LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls); -LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls); -LJ_FUNC void lj_lex_next(LexState *ls); -LJ_FUNC LexToken lj_lex_lookahead(LexState *ls); -LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken tok); -LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...); -LJ_FUNC void lj_lex_init(lua_State *L); - -#ifdef LUA_USE_ASSERT -#define lj_assertLS(c, ...) (lj_assertG_(G(ls->L), (c), __VA_ARGS__)) -#else -#define lj_assertLS(c, ...) ((void)ls) -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.c deleted file mode 100644 index 82a9e25..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.c +++ /dev/null @@ -1,359 +0,0 @@ -/* -** Library function support. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_lib_c -#define LUA_CORE - -#include "lauxlib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_bc.h" -#include "lj_dispatch.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" -#include "lj_lex.h" -#include "lj_bcdump.h" -#include "lj_lib.h" - -/* -- Library initialization ---------------------------------------------- */ - -static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize) -{ - if (libname) { - luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); - lua_getfield(L, -1, libname); - if (!tvistab(L->top-1)) { - L->top--; - if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL) - lj_err_callerv(L, LJ_ERR_BADMODN, libname); - settabV(L, L->top, tabV(L->top-1)); - L->top++; - lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ - } - L->top--; - settabV(L, L->top-1, tabV(L->top)); - } else { - lua_createtable(L, 0, hsize); - } - return tabV(L->top-1); -} - -static const uint8_t *lib_read_lfunc(lua_State *L, const uint8_t *p, GCtab *tab) -{ - int len = *p++; - GCstr *name = lj_str_new(L, (const char *)p, len); - LexState ls; - GCproto *pt; - GCfunc *fn; - memset(&ls, 0, sizeof(ls)); - ls.L = L; - ls.p = (const char *)(p+len); - ls.pe = (const char *)~(uintptr_t)0; - ls.c = -1; - ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE)); - ls.chunkname = name; - pt = lj_bcread_proto(&ls); - pt->firstline = ~(BCLine)0; - fn = lj_func_newL_empty(L, pt, tabref(L->env)); - /* NOBARRIER: See below for common barrier. */ - setfuncV(L, lj_tab_setstr(L, tab, name), fn); - return (const uint8_t *)ls.p; -} - -void lj_lib_register(lua_State *L, const char *libname, - const uint8_t *p, const lua_CFunction *cf) -{ - GCtab *env = tabref(L->env); - GCfunc *ofn = NULL; - int ffid = *p++; - BCIns *bcff = &L2GG(L)->bcff[*p++]; - GCtab *tab = lib_create_table(L, libname, *p++); - ptrdiff_t tpos = L->top - L->base; - - /* Avoid barriers further down. */ - lj_gc_anybarriert(L, tab); - tab->nomm = 0; - - for (;;) { - uint32_t tag = *p++; - MSize len = tag & LIBINIT_LENMASK; - tag &= LIBINIT_TAGMASK; - if (tag != LIBINIT_STRING) { - const char *name; - MSize nuv = (MSize)(L->top - L->base - tpos); - GCfunc *fn = lj_func_newC(L, nuv, env); - if (nuv) { - L->top = L->base + tpos; - memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv); - } - fn->c.ffid = (uint8_t)(ffid++); - name = (const char *)p; - p += len; - if (tag == LIBINIT_CF) - setmref(fn->c.pc, &G(L)->bc_cfunc_int); - else - setmref(fn->c.pc, bcff++); - if (tag == LIBINIT_ASM_) - fn->c.f = ofn->c.f; /* Copy handler from previous function. */ - else - fn->c.f = *cf++; /* Get cf or handler from C function table. */ - if (len) { - /* NOBARRIER: See above for common barrier. */ - setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn); - } - ofn = fn; - } else { - switch (tag | len) { - case LIBINIT_LUA: - p = lib_read_lfunc(L, p, tab); - break; - case LIBINIT_SET: - L->top -= 2; - if (tvisstr(L->top+1) && strV(L->top+1)->len == 0) - env = tabV(L->top); - else /* NOBARRIER: See above for common barrier. */ - copyTV(L, lj_tab_set(L, tab, L->top+1), L->top); - break; - case LIBINIT_NUMBER: - memcpy(&L->top->n, p, sizeof(double)); - L->top++; - p += sizeof(double); - break; - case LIBINIT_COPY: - copyTV(L, L->top, L->top - *p++); - L->top++; - break; - case LIBINIT_LASTCL: - setfuncV(L, L->top++, ofn); - break; - case LIBINIT_FFID: - ffid++; - break; - case LIBINIT_END: - return; - default: - setstrV(L, L->top++, lj_str_new(L, (const char *)p, len)); - p += len; - break; - } - } - } -} - -/* Push internal function on the stack. */ -GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n) -{ - GCfunc *fn; - lua_pushcclosure(L, f, n); - fn = funcV(L->top-1); - fn->c.ffid = (uint8_t)id; - setmref(fn->c.pc, &G(L)->bc_cfunc_int); - return fn; -} - -void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env) -{ - luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); - lua_pushcfunction(L, f); - /* NOBARRIER: The function is new (marked white). */ - setgcref(funcV(L->top-1)->c.env, obj2gco(env)); - lua_setfield(L, -2, name); - L->top--; -} - -int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name) -{ - GCfunc *fn = lj_lib_pushcf(L, cf, id); - GCtab *t = tabref(curr_func(L)->c.env); /* Reference to parent table. */ - setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn); - lj_gc_anybarriert(L, t); - setfuncV(L, L->top++, fn); - return 1; -} - -/* -- Type checks --------------------------------------------------------- */ - -TValue *lj_lib_checkany(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (o >= L->top) - lj_err_arg(L, narg, LJ_ERR_NOVAL); - return o; -} - -GCstr *lj_lib_checkstr(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (o < L->top) { - if (LJ_LIKELY(tvisstr(o))) { - return strV(o); - } else if (tvisnumber(o)) { - GCstr *s = lj_strfmt_number(L, o); - setstrV(L, o, s); - return s; - } - } - lj_err_argt(L, narg, LUA_TSTRING); - return NULL; /* unreachable */ -} - -GCstr *lj_lib_optstr(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL; -} - -#if LJ_DUALNUM -void lj_lib_checknumber(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); -} -#endif - -lua_Number lj_lib_checknum(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && - (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o))))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_UNLIKELY(tvisint(o))) { - lua_Number n = (lua_Number)intV(o); - setnumV(o, n); - return n; - } else { - return numV(o); - } -} - -int32_t lj_lib_checkint(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && lj_strscan_numberobj(o))) - lj_err_argt(L, narg, LUA_TNUMBER); - if (LJ_LIKELY(tvisint(o))) { - return intV(o); - } else { - int32_t i = lj_num2int(numV(o)); - if (LJ_DUALNUM) setintV(o, i); - return i; - } -} - -int32_t lj_lib_optint(lua_State *L, int narg, int32_t def) -{ - TValue *o = L->base + narg-1; - return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def; -} - -GCfunc *lj_lib_checkfunc(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tvisfunc(o))) - lj_err_argt(L, narg, LUA_TFUNCTION); - return funcV(o); -} - -GCtab *lj_lib_checktab(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tvistab(o))) - lj_err_argt(L, narg, LUA_TTABLE); - return tabV(o); -} - -GCtab *lj_lib_checktabornil(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (o < L->top) { - if (tvistab(o)) - return tabV(o); - else if (tvisnil(o)) - return NULL; - } - lj_err_arg(L, narg, LJ_ERR_NOTABN); - return NULL; /* unreachable */ -} - -int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst) -{ - GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg); - if (s) { - const char *opt = strdata(s); - MSize len = s->len; - int i; - for (i = 0; *(const uint8_t *)lst; i++) { - if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0) - return i; - lst += 1+*(const uint8_t *)lst; - } - lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt); - } - return def; -} - -/* -- Strict type checks -------------------------------------------------- */ - -/* The following type checks do not coerce between strings and numbers. -** And they handle plain int64_t/uint64_t FFI numbers, too. -*/ - -#if LJ_HASBUFFER -GCstr *lj_lib_checkstrx(lua_State *L, int narg) -{ - TValue *o = L->base + narg-1; - if (!(o < L->top && tvisstr(o))) lj_err_argt(L, narg, LUA_TSTRING); - return strV(o); -} - -int32_t lj_lib_checkintrange(lua_State *L, int narg, int32_t a, int32_t b) -{ - TValue *o = L->base + narg-1; - lj_assertL(b >= 0, "expected range must be non-negative"); - if (o < L->top) { - if (LJ_LIKELY(tvisint(o))) { - int32_t i = intV(o); - if (i >= a && i <= b) return i; - } else if (LJ_LIKELY(tvisnum(o))) { - /* For performance reasons, this doesn't check for integerness or - ** integer overflow. Overflow detection still works, since all FPUs - ** return either MININT or MAXINT, which is then out of range. - */ - int32_t i = (int32_t)numV(o); - if (i >= a && i <= b) return i; -#if LJ_HASFFI - } else if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - if (cd->ctypeid == CTID_INT64) { - int64_t i = *(int64_t *)cdataptr(cd); - if (i >= (int64_t)a && i <= (int64_t)b) return (int32_t)i; - } else if (cd->ctypeid == CTID_UINT64) { - uint64_t i = *(uint64_t *)cdataptr(cd); - if ((a < 0 || i >= (uint64_t)a) && i <= (uint64_t)b) return (int32_t)i; - } else { - goto badtype; - } -#endif - } else { - goto badtype; - } - lj_err_arg(L, narg, LJ_ERR_NUMRNG); - } -badtype: - lj_err_argt(L, narg, LUA_TNUMBER); - return 0; /* unreachable */ -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.h deleted file mode 100644 index a18f52b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_lib.h +++ /dev/null @@ -1,116 +0,0 @@ -/* -** Library function support. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_LIB_H -#define _LJ_LIB_H - -#include "lj_obj.h" - -/* -** A fallback handler is called by the assembler VM if the fast path fails: -** -** - too few arguments: unrecoverable. -** - wrong argument type: recoverable, if coercion succeeds. -** - bad argument value: unrecoverable. -** - stack overflow: recoverable, if stack reallocation succeeds. -** - extra handling: recoverable. -** -** The unrecoverable cases throw an error with lj_err_arg(), lj_err_argtype(), -** lj_err_caller() or lj_err_callermsg(). -** The recoverable cases return 0 or the number of results + 1. -** The assembler VM retries the fast path only if 0 is returned. -** This time the fallback must not be called again or it gets stuck in a loop. -*/ - -/* Return values from fallback handler. */ -#define FFH_RETRY 0 -#define FFH_UNREACHABLE FFH_RETRY -#define FFH_RES(n) ((n)+1) -#define FFH_TAILCALL (-1) - -LJ_FUNC TValue *lj_lib_checkany(lua_State *L, int narg); -LJ_FUNC GCstr *lj_lib_checkstr(lua_State *L, int narg); -LJ_FUNC GCstr *lj_lib_optstr(lua_State *L, int narg); -#if LJ_DUALNUM -LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg); -#else -#define lj_lib_checknumber(L, narg) lj_lib_checknum((L), (narg)) -#endif -LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg); -LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg); -LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def); -LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg); -LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg); -LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg); -LJ_FUNC int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst); - -#if LJ_HASBUFFER -LJ_FUNC GCstr *lj_lib_checkstrx(lua_State *L, int narg); -LJ_FUNC int32_t lj_lib_checkintrange(lua_State *L, int narg, - int32_t a, int32_t b); -#endif - -/* Avoid including lj_frame.h. */ -#if LJ_GC64 -#define lj_lib_upvalue(L, n) \ - (&gcval(L->base-2)->fn.c.upvalue[(n)-1]) -#elif LJ_FR2 -#define lj_lib_upvalue(L, n) \ - (&gcref((L->base-2)->gcr)->fn.c.upvalue[(n)-1]) -#else -#define lj_lib_upvalue(L, n) \ - (&gcref((L->base-1)->fr.func)->fn.c.upvalue[(n)-1]) -#endif - -#if LJ_TARGET_WINDOWS -#define lj_lib_checkfpu(L) \ - do { setnumV(L->top++, (lua_Number)1437217655); \ - if (lua_tointeger(L, -1) != 1437217655) lj_err_caller(L, LJ_ERR_BADFPU); \ - L->top--; } while (0) -#else -#define lj_lib_checkfpu(L) UNUSED(L) -#endif - -LJ_FUNC GCfunc *lj_lib_pushcc(lua_State *L, lua_CFunction f, int id, int n); -#define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0)) - -/* Library function declarations. Scanned by buildvm. */ -#define LJLIB_CF(name) static int lj_cf_##name(lua_State *L) -#define LJLIB_ASM(name) static int lj_ffh_##name(lua_State *L) -#define LJLIB_ASM_(name) -#define LJLIB_LUA(name) -#define LJLIB_SET(name) -#define LJLIB_PUSH(arg) -#define LJLIB_REC(handler) -#define LJLIB_NOREGUV -#define LJLIB_NOREG - -#define LJ_LIB_REG(L, regname, name) \ - lj_lib_register(L, regname, lj_lib_init_##name, lj_lib_cf_##name) - -LJ_FUNC void lj_lib_register(lua_State *L, const char *libname, - const uint8_t *init, const lua_CFunction *cf); -LJ_FUNC void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, - GCtab *env); -LJ_FUNC int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, - const char *name); - -/* Library init data tags. */ -#define LIBINIT_LENMASK 0x3f -#define LIBINIT_TAGMASK 0xc0 -#define LIBINIT_CF 0x00 -#define LIBINIT_ASM 0x40 -#define LIBINIT_ASM_ 0x80 -#define LIBINIT_STRING 0xc0 -#define LIBINIT_MAXSTR 0x38 -#define LIBINIT_LUA 0xf9 -#define LIBINIT_SET 0xfa -#define LIBINIT_NUMBER 0xfb -#define LIBINIT_COPY 0xfc -#define LIBINIT_LASTCL 0xfd -#define LIBINIT_FFID 0xfe -#define LIBINIT_END 0xff - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_load.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_load.c deleted file mode 100644 index 0aab488..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_load.c +++ /dev/null @@ -1,168 +0,0 @@ -/* -** Load and dump code. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include -#include - -#define lj_load_c -#define LUA_CORE - -#include "lua.h" -#include "lauxlib.h" - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_func.h" -#include "lj_frame.h" -#include "lj_vm.h" -#include "lj_lex.h" -#include "lj_bcdump.h" -#include "lj_parse.h" - -/* -- Load Lua source code and bytecode ----------------------------------- */ - -static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud) -{ - LexState *ls = (LexState *)ud; - GCproto *pt; - GCfunc *fn; - int bc; - UNUSED(dummy); - cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ - bc = lj_lex_setup(L, ls); - if (ls->mode && !strchr(ls->mode, bc ? 'b' : 't')) { - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE)); - lj_err_throw(L, LUA_ERRSYNTAX); - } - pt = bc ? lj_bcread(ls) : lj_parse(ls); - fn = lj_func_newL_empty(L, pt, tabref(L->env)); - /* Don't combine above/below into one statement. */ - setfuncV(L, L->top++, fn); - return NULL; -} - -LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data, - const char *chunkname, const char *mode) -{ - LexState ls; - int status; - ls.rfunc = reader; - ls.rdata = data; - ls.chunkarg = chunkname ? chunkname : "?"; - ls.mode = mode; - lj_buf_init(L, &ls.sb); - status = lj_vm_cpcall(L, NULL, &ls, cpparser); - lj_lex_cleanup(L, &ls); - lj_gc_check(L); - return status; -} - -LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, - const char *chunkname) -{ - return lua_loadx(L, reader, data, chunkname, NULL); -} - -typedef struct FileReaderCtx { - FILE *fp; - char buf[LUAL_BUFFERSIZE]; -} FileReaderCtx; - -static const char *reader_file(lua_State *L, void *ud, size_t *size) -{ - FileReaderCtx *ctx = (FileReaderCtx *)ud; - UNUSED(L); - if (feof(ctx->fp)) return NULL; - *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp); - return *size > 0 ? ctx->buf : NULL; -} - -LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, - const char *mode) -{ - FileReaderCtx ctx; - int status; - const char *chunkname; - if (filename) { - ctx.fp = fopen(filename, "rb"); - if (ctx.fp == NULL) { - lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); - return LUA_ERRFILE; - } - chunkname = lua_pushfstring(L, "@%s", filename); - } else { - ctx.fp = stdin; - chunkname = "=stdin"; - } - status = lua_loadx(L, reader_file, &ctx, chunkname, mode); - if (ferror(ctx.fp)) { - L->top -= filename ? 2 : 1; - lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); - if (filename) - fclose(ctx.fp); - return LUA_ERRFILE; - } - if (filename) { - L->top--; - copyTV(L, L->top-1, L->top); - fclose(ctx.fp); - } - return status; -} - -LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) -{ - return luaL_loadfilex(L, filename, NULL); -} - -typedef struct StringReaderCtx { - const char *str; - size_t size; -} StringReaderCtx; - -static const char *reader_string(lua_State *L, void *ud, size_t *size) -{ - StringReaderCtx *ctx = (StringReaderCtx *)ud; - UNUSED(L); - if (ctx->size == 0) return NULL; - *size = ctx->size; - ctx->size = 0; - return ctx->str; -} - -LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size, - const char *name, const char *mode) -{ - StringReaderCtx ctx; - ctx.str = buf; - ctx.size = size; - return lua_loadx(L, reader_string, &ctx, name, mode); -} - -LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, - const char *name) -{ - return luaL_loadbufferx(L, buf, size, name, NULL); -} - -LUALIB_API int luaL_loadstring(lua_State *L, const char *s) -{ - return luaL_loadbuffer(L, s, strlen(s), s); -} - -/* -- Dump bytecode ------------------------------------------------------- */ - -LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) -{ - cTValue *o = L->top-1; - lj_checkapi(L->top > L->base, "top slot empty"); - if (tvisfunc(o) && isluafunc(funcV(o))) - return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0); - else - return 1; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.c deleted file mode 100644 index 163aada..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.c +++ /dev/null @@ -1,374 +0,0 @@ -/* -** Machine code management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_mcode_c -#define LUA_CORE - -#include "lj_obj.h" -#if LJ_HASJIT -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_jit.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_dispatch.h" -#include "lj_prng.h" -#endif -#if LJ_HASJIT || LJ_HASFFI -#include "lj_vm.h" -#endif - -/* -- OS-specific functions ----------------------------------------------- */ - -#if LJ_HASJIT || LJ_HASFFI - -/* Define this if you want to run LuaJIT with Valgrind. */ -#ifdef LUAJIT_USE_VALGRIND -#include -#endif - -#if LJ_TARGET_IOS -void sys_icache_invalidate(void *start, size_t len); -#endif - -/* Synchronize data/instruction cache. */ -void lj_mcode_sync(void *start, void *end) -{ -#ifdef LUAJIT_USE_VALGRIND - VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start); -#endif -#if LJ_TARGET_X86ORX64 - UNUSED(start); UNUSED(end); -#elif LJ_TARGET_IOS - sys_icache_invalidate(start, (char *)end-(char *)start); -#elif LJ_TARGET_PPC - lj_vm_cachesync(start, end); -#elif defined(__GNUC__) || defined(__clang__) - __clear_cache(start, end); -#else -#error "Missing builtin to flush instruction cache" -#endif -} - -#endif - -#if LJ_HASJIT - -#if LJ_TARGET_WINDOWS - -#define WIN32_LEAN_AND_MEAN -#include - -#define MCPROT_RW PAGE_READWRITE -#define MCPROT_RX PAGE_EXECUTE_READ -#define MCPROT_RWX PAGE_EXECUTE_READWRITE - -static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, DWORD prot) -{ - void *p = LJ_WIN_VALLOC((void *)hint, sz, - MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, prot); - if (!p && !hint) - lj_trace_err(J, LJ_TRERR_MCODEAL); - return p; -} - -static void mcode_free(jit_State *J, void *p, size_t sz) -{ - UNUSED(J); UNUSED(sz); - VirtualFree(p, 0, MEM_RELEASE); -} - -static int mcode_setprot(void *p, size_t sz, DWORD prot) -{ - DWORD oprot; - return !LJ_WIN_VPROTECT(p, sz, prot, &oprot); -} - -#elif LJ_TARGET_POSIX - -#include - -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif - -#define MCPROT_RW (PROT_READ|PROT_WRITE) -#define MCPROT_RX (PROT_READ|PROT_EXEC) -#define MCPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC) -#ifdef PROT_MPROTECT -#define MCPROT_CREATE (PROT_MPROTECT(MCPROT_RWX)) -#else -#define MCPROT_CREATE 0 -#endif - -static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) -{ - void *p = mmap((void *)hint, sz, prot|MCPROT_CREATE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (p == MAP_FAILED) { - if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL); - p = NULL; - } - return p; -} - -static void mcode_free(jit_State *J, void *p, size_t sz) -{ - UNUSED(J); - munmap(p, sz); -} - -static int mcode_setprot(void *p, size_t sz, int prot) -{ - return mprotect(p, sz, prot); -} - -#else - -#error "Missing OS support for explicit placement of executable memory" - -#endif - -/* -- MCode area protection ----------------------------------------------- */ - -#if LUAJIT_SECURITY_MCODE == 0 - -/* Define this ONLY if page protection twiddling becomes a bottleneck. -** -** It's generally considered to be a potential security risk to have -** pages with simultaneous write *and* execute access in a process. -** -** Do not even think about using this mode for server processes or -** apps handling untrusted external data. -** -** The security risk is not in LuaJIT itself -- but if an adversary finds -** any *other* flaw in your C application logic, then any RWX memory pages -** simplify writing an exploit considerably. -*/ -#define MCPROT_GEN MCPROT_RWX -#define MCPROT_RUN MCPROT_RWX - -static void mcode_protect(jit_State *J, int prot) -{ - UNUSED(J); UNUSED(prot); UNUSED(mcode_setprot); -} - -#else - -/* This is the default behaviour and much safer: -** -** Most of the time the memory pages holding machine code are executable, -** but NONE of them is writable. -** -** The current memory area is marked read-write (but NOT executable) only -** during the short time window while the assembler generates machine code. -*/ -#define MCPROT_GEN MCPROT_RW -#define MCPROT_RUN MCPROT_RX - -/* Protection twiddling failed. Probably due to kernel security. */ -static LJ_NORET LJ_NOINLINE void mcode_protfail(jit_State *J) -{ - lua_CFunction panic = J2G(J)->panic; - if (panic) { - lua_State *L = J->L; - setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITPROT)); - panic(L); - } - exit(EXIT_FAILURE); -} - -/* Change protection of MCode area. */ -static void mcode_protect(jit_State *J, int prot) -{ - if (J->mcprot != prot) { - if (LJ_UNLIKELY(mcode_setprot(J->mcarea, J->szmcarea, prot))) - mcode_protfail(J); - J->mcprot = prot; - } -} - -#endif - -/* -- MCode area allocation ----------------------------------------------- */ - -#if LJ_64 -#define mcode_validptr(p) (p) -#else -#define mcode_validptr(p) ((p) && (uintptr_t)(p) < 0xffff0000) -#endif - -#ifdef LJ_TARGET_JUMPRANGE - -/* Get memory within relative jump distance of our code in 64 bit mode. */ -static void *mcode_alloc(jit_State *J, size_t sz) -{ - /* Target an address in the static assembler code (64K aligned). - ** Try addresses within a distance of target-range/2+1MB..target+range/2-1MB. - ** Use half the jump range so every address in the range can reach any other. - */ -#if LJ_TARGET_MIPS - /* Use the middle of the 256MB-aligned region. */ - uintptr_t target = ((uintptr_t)(void *)lj_vm_exit_handler & - ~(uintptr_t)0x0fffffffu) + 0x08000000u; -#else - uintptr_t target = (uintptr_t)(void *)lj_vm_exit_handler & ~(uintptr_t)0xffff; -#endif - const uintptr_t range = (1u << (LJ_TARGET_JUMPRANGE-1)) - (1u << 21); - /* First try a contiguous area below the last one. */ - uintptr_t hint = J->mcarea ? (uintptr_t)J->mcarea - sz : 0; - int i; - /* Limit probing iterations, depending on the available pool size. */ - for (i = 0; i < LJ_TARGET_JUMPRANGE; i++) { - if (mcode_validptr(hint)) { - void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN); - - if (mcode_validptr(p) && - ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range)) - return p; - if (p) mcode_free(J, p, sz); /* Free badly placed area. */ - } - /* Next try probing 64K-aligned pseudo-random addresses. */ - do { - hint = lj_prng_u64(&J2G(J)->prng) & ((1u<mcarea; - size_t sz = (size_t)J->param[JIT_P_sizemcode] << 10; - sz = (sz + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1); - J->mcarea = (MCode *)mcode_alloc(J, sz); - J->szmcarea = sz; - J->mcprot = MCPROT_GEN; - J->mctop = (MCode *)((char *)J->mcarea + J->szmcarea); - J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink)); - ((MCLink *)J->mcarea)->next = oldarea; - ((MCLink *)J->mcarea)->size = sz; - J->szallmcarea += sz; - J->mcbot = (MCode *)lj_err_register_mcode(J->mcarea, sz, (uint8_t *)J->mcbot); -} - -/* Free all MCode areas. */ -void lj_mcode_free(jit_State *J) -{ - MCode *mc = J->mcarea; - J->mcarea = NULL; - J->szallmcarea = 0; - while (mc) { - MCode *next = ((MCLink *)mc)->next; - size_t sz = ((MCLink *)mc)->size; - lj_err_deregister_mcode(mc, sz, (uint8_t *)mc + sizeof(MCLink)); - mcode_free(J, mc, sz); - mc = next; - } -} - -/* -- MCode transactions -------------------------------------------------- */ - -/* Reserve the remainder of the current MCode area. */ -MCode *lj_mcode_reserve(jit_State *J, MCode **lim) -{ - if (!J->mcarea) - mcode_allocarea(J); - else - mcode_protect(J, MCPROT_GEN); - *lim = J->mcbot; - return J->mctop; -} - -/* Commit the top part of the current MCode area. */ -void lj_mcode_commit(jit_State *J, MCode *top) -{ - J->mctop = top; - mcode_protect(J, MCPROT_RUN); -} - -/* Abort the reservation. */ -void lj_mcode_abort(jit_State *J) -{ - if (J->mcarea) - mcode_protect(J, MCPROT_RUN); -} - -/* Set/reset protection to allow patching of MCode areas. */ -MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish) -{ - if (finish) { -#if LUAJIT_SECURITY_MCODE - if (J->mcarea == ptr) - mcode_protect(J, MCPROT_RUN); - else if (LJ_UNLIKELY(mcode_setprot(ptr, ((MCLink *)ptr)->size, MCPROT_RUN))) - mcode_protfail(J); -#endif - return NULL; - } else { - MCode *mc = J->mcarea; - /* Try current area first to use the protection cache. */ - if (ptr >= mc && ptr < (MCode *)((char *)mc + J->szmcarea)) { -#if LUAJIT_SECURITY_MCODE - mcode_protect(J, MCPROT_GEN); -#endif - return mc; - } - /* Otherwise search through the list of MCode areas. */ - for (;;) { - mc = ((MCLink *)mc)->next; - lj_assertJ(mc != NULL, "broken MCode area chain"); - if (ptr >= mc && ptr < (MCode *)((char *)mc + ((MCLink *)mc)->size)) { -#if LUAJIT_SECURITY_MCODE - if (LJ_UNLIKELY(mcode_setprot(mc, ((MCLink *)mc)->size, MCPROT_GEN))) - mcode_protfail(J); -#endif - return mc; - } - } - } -} - -/* Limit of MCode reservation reached. */ -void lj_mcode_limiterr(jit_State *J, size_t need) -{ - size_t sizemcode, maxmcode; - lj_mcode_abort(J); - sizemcode = (size_t)J->param[JIT_P_sizemcode] << 10; - sizemcode = (sizemcode + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1); - maxmcode = (size_t)J->param[JIT_P_maxmcode] << 10; - if ((size_t)need > sizemcode) - lj_trace_err(J, LJ_TRERR_MCODEOV); /* Too long for any area. */ - if (J->szallmcarea + sizemcode > maxmcode) - lj_trace_err(J, LJ_TRERR_MCODEAL); - mcode_allocarea(J); - lj_trace_err(J, LJ_TRERR_MCODELM); /* Retry with new area. */ -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.h deleted file mode 100644 index be35925..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_mcode.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -** Machine code management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_MCODE_H -#define _LJ_MCODE_H - -#include "lj_obj.h" - -#if LJ_HASJIT || LJ_HASFFI -LJ_FUNC void lj_mcode_sync(void *start, void *end); -#endif - -#if LJ_HASJIT - -#include "lj_jit.h" - -LJ_FUNC void lj_mcode_free(jit_State *J); -LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim); -LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m); -LJ_FUNC void lj_mcode_abort(jit_State *J); -LJ_FUNC MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish); -LJ_FUNC_NORET void lj_mcode_limiterr(jit_State *J, size_t need); - -#define lj_mcode_commitbot(J, m) (J->mcbot = (m)) - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.c deleted file mode 100644 index 5324c66..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.c +++ /dev/null @@ -1,482 +0,0 @@ -/* -** Metamethod handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_meta_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" -#include "lj_lib.h" - -/* -- Metamethod handling ------------------------------------------------- */ - -/* String interning of metamethod names for fast indexing. */ -void lj_meta_init(lua_State *L) -{ -#define MMNAME(name) "__" #name - const char *metanames = MMDEF(MMNAME); -#undef MMNAME - global_State *g = G(L); - const char *p, *q; - uint32_t mm; - for (mm = 0, p = metanames; *p; mm++, p = q) { - GCstr *s; - for (q = p+2; *q && *q != '_'; q++) ; - s = lj_str_new(L, p, (size_t)(q-p)); - /* NOBARRIER: g->gcroot[] is a GC root. */ - setgcref(g->gcroot[GCROOT_MMNAME+mm], obj2gco(s)); - } -} - -/* Negative caching of a few fast metamethods. See the lj_meta_fast() macro. */ -cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name) -{ - cTValue *mo = lj_tab_getstr(mt, name); - lj_assertX(mm <= MM_FAST, "bad metamethod %d", mm); - if (!mo || tvisnil(mo)) { /* No metamethod? */ - mt->nomm |= (uint8_t)(1u<metatable); - else if (tvisudata(o)) - mt = tabref(udataV(o)->metatable); - else - mt = tabref(basemt_obj(G(L), o)); - if (mt) { - cTValue *mo = lj_tab_getstr(mt, mmname_str(G(L), mm)); - if (mo) - return mo; - } - return niltv(L); -} - -#if LJ_HASFFI -/* Tailcall from C function. */ -int lj_meta_tailcall(lua_State *L, cTValue *tv) -{ - TValue *base = L->base; - TValue *top = L->top; - const BCIns *pc = frame_pc(base-1); /* Preserve old PC from frame. */ - copyTV(L, base-1-LJ_FR2, tv); /* Replace frame with new object. */ - if (LJ_FR2) - (top++)->u64 = LJ_CONT_TAILCALL; - else - top->u32.lo = LJ_CONT_TAILCALL; - setframe_pc(top++, pc); - setframe_gc(top, obj2gco(L), LJ_TTHREAD); /* Dummy frame object. */ - if (LJ_FR2) top++; - setframe_ftsz(top, ((char *)(top+1) - (char *)base) + FRAME_CONT); - L->base = L->top = top+1; - /* - ** before: [old_mo|PC] [... ...] - ** ^base ^top - ** after: [new_mo|itype] [... ...] [NULL|PC] [dummy|delta] - ** ^base/top - ** tailcall: [new_mo|PC] [... ...] - ** ^base ^top - */ - return 0; -} -#endif - -/* Setup call to metamethod to be run by Assembler VM. */ -static TValue *mmcall(lua_State *L, ASMFunction cont, cTValue *mo, - cTValue *a, cTValue *b) -{ - /* - ** |-- framesize -> top top+1 top+2 top+3 - ** before: [func slots ...] - ** mm setup: [func slots ...] [cont|?] [mo|tmtype] [a] [b] - ** in asm: [func slots ...] [cont|PC] [mo|delta] [a] [b] - ** ^-- func base ^-- mm base - ** after mm: [func slots ...] [result] - ** ^-- copy to base[PC_RA] --/ for lj_cont_ra - ** istruecond + branch for lj_cont_cond* - ** ignore for lj_cont_nop - ** next PC: [func slots ...] - */ - TValue *top = L->top; - if (curr_funcisL(L)) top = curr_topL(L); - setcont(top++, cont); /* Assembler VM stores PC in upper word or FR2. */ - if (LJ_FR2) setnilV(top++); - copyTV(L, top++, mo); /* Store metamethod and two arguments. */ - if (LJ_FR2) setnilV(top++); - copyTV(L, top, a); - copyTV(L, top+1, b); - return top; /* Return new base. */ -} - -/* -- C helpers for some instructions, called from assembler VM ----------- */ - -/* Helper for TGET*. __index chain and metamethod. */ -cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k) -{ - int loop; - for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { - cTValue *mo; - if (LJ_LIKELY(tvistab(o))) { - GCtab *t = tabV(o); - cTValue *tv = lj_tab_get(L, t, k); - if (!tvisnil(tv) || - !(mo = lj_meta_fast(L, tabref(t->metatable), MM_index))) - return tv; - } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_index))) { - lj_err_optype(L, o, LJ_ERR_OPINDEX); - return NULL; /* unreachable */ - } - if (tvisfunc(mo)) { - L->top = mmcall(L, lj_cont_ra, mo, o, k); - return NULL; /* Trigger metamethod call. */ - } - o = mo; - } - lj_err_msg(L, LJ_ERR_GETLOOP); - return NULL; /* unreachable */ -} - -/* Helper for TSET*. __newindex chain and metamethod. */ -TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k) -{ - TValue tmp; - int loop; - for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { - cTValue *mo; - if (LJ_LIKELY(tvistab(o))) { - GCtab *t = tabV(o); - cTValue *tv = lj_tab_get(L, t, k); - if (LJ_LIKELY(!tvisnil(tv))) { - t->nomm = 0; /* Invalidate negative metamethod cache. */ - lj_gc_anybarriert(L, t); - return (TValue *)tv; - } else if (!(mo = lj_meta_fast(L, tabref(t->metatable), MM_newindex))) { - t->nomm = 0; /* Invalidate negative metamethod cache. */ - lj_gc_anybarriert(L, t); - if (tv != niltv(L)) - return (TValue *)tv; - if (tvisnil(k)) lj_err_msg(L, LJ_ERR_NILIDX); - else if (tvisint(k)) { setnumV(&tmp, (lua_Number)intV(k)); k = &tmp; } - else if (tvisnum(k) && tvisnan(k)) lj_err_msg(L, LJ_ERR_NANIDX); - return lj_tab_newkey(L, t, k); - } - } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_newindex))) { - lj_err_optype(L, o, LJ_ERR_OPINDEX); - return NULL; /* unreachable */ - } - if (tvisfunc(mo)) { - L->top = mmcall(L, lj_cont_nop, mo, o, k); - /* L->top+2 = v filled in by caller. */ - return NULL; /* Trigger metamethod call. */ - } - copyTV(L, &tmp, mo); - o = &tmp; - } - lj_err_msg(L, LJ_ERR_SETLOOP); - return NULL; /* unreachable */ -} - -static cTValue *str2num(cTValue *o, TValue *n) -{ - if (tvisnum(o)) - return o; - else if (tvisint(o)) - return (setnumV(n, (lua_Number)intV(o)), n); - else if (tvisstr(o) && lj_strscan_num(strV(o), n)) - return n; - else - return NULL; -} - -/* Helper for arithmetic instructions. Coercion, metamethod. */ -TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, cTValue *rc, - BCReg op) -{ - MMS mm = bcmode_mm(op); - TValue tempb, tempc; - cTValue *b, *c; - if ((b = str2num(rb, &tempb)) != NULL && - (c = str2num(rc, &tempc)) != NULL) { /* Try coercion first. */ - setnumV(ra, lj_vm_foldarith(numV(b), numV(c), (int)mm-MM_add)); - return NULL; - } else { - cTValue *mo = lj_meta_lookup(L, rb, mm); - if (tvisnil(mo)) { - mo = lj_meta_lookup(L, rc, mm); - if (tvisnil(mo)) { - if (str2num(rb, &tempb) == NULL) rc = rb; - lj_err_optype(L, rc, LJ_ERR_OPARITH); - return NULL; /* unreachable */ - } - } - return mmcall(L, lj_cont_ra, mo, rb, rc); - } -} - -/* Helper for CAT. Coercion, iterative concat, __concat metamethod. */ -TValue *lj_meta_cat(lua_State *L, TValue *top, int left) -{ - int fromc = 0; - if (left < 0) { left = -left; fromc = 1; } - do { - if (!(tvisstr(top) || tvisnumber(top) || tvisbuf(top)) || - !(tvisstr(top-1) || tvisnumber(top-1) || tvisbuf(top-1))) { - cTValue *mo = lj_meta_lookup(L, top-1, MM_concat); - if (tvisnil(mo)) { - mo = lj_meta_lookup(L, top, MM_concat); - if (tvisnil(mo)) { - if (tvisstr(top-1) || tvisnumber(top-1)) top++; - lj_err_optype(L, top-1, LJ_ERR_OPCAT); - return NULL; /* unreachable */ - } - } - /* One of the top two elements is not a string, call __cat metamethod: - ** - ** before: [...][CAT stack .........................] - ** top-1 top top+1 top+2 - ** pick two: [...][CAT stack ...] [o1] [o2] - ** setup mm: [...][CAT stack ...] [cont|?] [mo|tmtype] [o1] [o2] - ** in asm: [...][CAT stack ...] [cont|PC] [mo|delta] [o1] [o2] - ** ^-- func base ^-- mm base - ** after mm: [...][CAT stack ...] <--push-- [result] - ** next step: [...][CAT stack .............] - */ - copyTV(L, top+2*LJ_FR2+2, top); /* Carefully ordered stack copies! */ - copyTV(L, top+2*LJ_FR2+1, top-1); - copyTV(L, top+LJ_FR2, mo); - setcont(top-1, lj_cont_cat); - if (LJ_FR2) { setnilV(top); setnilV(top+2); top += 2; } - return top+1; /* Trigger metamethod call. */ - } else { - /* Pick as many strings as possible from the top and concatenate them: - ** - ** before: [...][CAT stack ...........................] - ** pick str: [...][CAT stack ...] [...... strings ......] - ** concat: [...][CAT stack ...] [result] - ** next step: [...][CAT stack ............] - */ - TValue *e, *o = top; - uint64_t tlen = tvisstr(o) ? strV(o)->len : - tvisbuf(o) ? sbufxlen(bufV(o)) : STRFMT_MAXBUF_NUM; - SBuf *sb; - do { - o--; tlen += tvisstr(o) ? strV(o)->len : - tvisbuf(o) ? sbufxlen(bufV(o)) : STRFMT_MAXBUF_NUM; - } while (--left > 0 && (tvisstr(o-1) || tvisnumber(o-1))); - if (tlen >= LJ_MAX_STR) lj_err_msg(L, LJ_ERR_STROV); - sb = lj_buf_tmp_(L); - lj_buf_more(sb, (MSize)tlen); - for (e = top, top = o; o <= e; o++) { - if (tvisstr(o)) { - GCstr *s = strV(o); - MSize len = s->len; - lj_buf_putmem(sb, strdata(s), len); - } else if (tvisbuf(o)) { - SBufExt *sbx = bufV(o); - lj_buf_putmem(sb, sbx->r, sbufxlen(sbx)); - } else if (tvisint(o)) { - lj_strfmt_putint(sb, intV(o)); - } else { - lj_strfmt_putfnum(sb, STRFMT_G14, numV(o)); - } - } - setstrV(L, top, lj_buf_str(L, sb)); - } - } while (left >= 1); - if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) { - if (!fromc) L->top = curr_topL(L); - lj_gc_step(L); - } - return NULL; -} - -/* Helper for LEN. __len metamethod. */ -TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o) -{ - cTValue *mo = lj_meta_lookup(L, o, MM_len); - if (tvisnil(mo)) { - if (LJ_52 && tvistab(o)) - tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<gch.metatable), MM_eq); - if (mo) { - TValue *top; - uint32_t it; - if (tabref(o1->gch.metatable) != tabref(o2->gch.metatable)) { - cTValue *mo2 = lj_meta_fast(L, tabref(o2->gch.metatable), MM_eq); - if (mo2 == NULL || !lj_obj_equal(mo, mo2)) - return (TValue *)(intptr_t)ne; - } - top = curr_top(L); - setcont(top++, ne ? lj_cont_condf : lj_cont_condt); - if (LJ_FR2) setnilV(top++); - copyTV(L, top++, mo); - if (LJ_FR2) setnilV(top++); - it = ~(uint32_t)o1->gch.gct; - setgcV(L, top, o1, it); - setgcV(L, top+1, o2, it); - return top; /* Trigger metamethod call. */ - } - return (TValue *)(intptr_t)ne; -} - -#if LJ_HASFFI -TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins) -{ - ASMFunction cont = (bc_op(ins) & 1) ? lj_cont_condf : lj_cont_condt; - int op = (int)bc_op(ins) & ~1; - TValue tv; - cTValue *mo, *o2, *o1 = &L->base[bc_a(ins)]; - cTValue *o1mm = o1; - if (op == BC_ISEQV) { - o2 = &L->base[bc_d(ins)]; - if (!tviscdata(o1mm)) o1mm = o2; - } else if (op == BC_ISEQS) { - setstrV(L, &tv, gco2str(proto_kgc(curr_proto(L), ~(ptrdiff_t)bc_d(ins)))); - o2 = &tv; - } else if (op == BC_ISEQN) { - o2 = &mref(curr_proto(L)->k, cTValue)[bc_d(ins)]; - } else { - lj_assertL(op == BC_ISEQP, "bad bytecode op %d", op); - setpriV(&tv, ~bc_d(ins)); - o2 = &tv; - } - mo = lj_meta_lookup(L, o1mm, MM_eq); - if (LJ_LIKELY(!tvisnil(mo))) - return mmcall(L, cont, mo, o1, o2); - else - return (TValue *)(intptr_t)(bc_op(ins) & 1); -} -#endif - -/* Helper for ordered comparisons. String compare, __lt/__le metamethods. */ -TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op) -{ - if (LJ_HASFFI && (tviscdata(o1) || tviscdata(o2))) { - ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt; - MMS mm = (op & 2) ? MM_le : MM_lt; - cTValue *mo = lj_meta_lookup(L, tviscdata(o1) ? o1 : o2, mm); - if (LJ_UNLIKELY(tvisnil(mo))) goto err; - return mmcall(L, cont, mo, o1, o2); - } else if (LJ_52 || itype(o1) == itype(o2)) { - /* Never called with two numbers. */ - if (tvisstr(o1) && tvisstr(o2)) { - int32_t res = lj_str_cmp(strV(o1), strV(o2)); - return (TValue *)(intptr_t)(((op&2) ? res <= 0 : res < 0) ^ (op&1)); - } else { - trymt: - while (1) { - ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt; - MMS mm = (op & 2) ? MM_le : MM_lt; - cTValue *mo = lj_meta_lookup(L, o1, mm); -#if LJ_52 - if (tvisnil(mo) && tvisnil((mo = lj_meta_lookup(L, o2, mm)))) -#else - cTValue *mo2 = lj_meta_lookup(L, o2, mm); - if (tvisnil(mo) || !lj_obj_equal(mo, mo2)) -#endif - { - if (op & 2) { /* MM_le not found: retry with MM_lt. */ - cTValue *ot = o1; o1 = o2; o2 = ot; /* Swap operands. */ - op ^= 3; /* Use LT and flip condition. */ - continue; - } - goto err; - } - return mmcall(L, cont, mo, o1, o2); - } - } - } else if (tvisbool(o1) && tvisbool(o2)) { - goto trymt; - } else { - err: - lj_err_comp(L, o1, o2); - return NULL; - } -} - -/* Helper for ISTYPE and ISNUM. Implicit coercion or error. */ -void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp) -{ - L->top = curr_topL(L); - ra++; tp--; - lj_assertL(LJ_DUALNUM || tp != ~LJ_TNUMX, "bad type for ISTYPE"); - if (LJ_DUALNUM && tp == ~LJ_TNUMX) lj_lib_checkint(L, ra); - else if (tp == ~LJ_TNUMX+1) lj_lib_checknum(L, ra); - else if (tp == ~LJ_TSTR) lj_lib_checkstr(L, ra); - else lj_err_argtype(L, ra, lj_obj_itypename[tp]); -} - -/* Helper for calls. __call metamethod. */ -void lj_meta_call(lua_State *L, TValue *func, TValue *top) -{ - cTValue *mo = lj_meta_lookup(L, func, MM_call); - TValue *p; - if (!tvisfunc(mo)) - lj_err_optype_call(L, func); - for (p = top; p > func+2*LJ_FR2; p--) copyTV(L, p, p-1); - if (LJ_FR2) copyTV(L, func+2, func); - copyTV(L, func, mo); -} - -/* Helper for FORI. Coercion. */ -void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o) -{ - if (!lj_strscan_numberobj(o)) lj_err_msg(L, LJ_ERR_FORINIT); - if (!lj_strscan_numberobj(o+1)) lj_err_msg(L, LJ_ERR_FORLIM); - if (!lj_strscan_numberobj(o+2)) lj_err_msg(L, LJ_ERR_FORSTEP); - if (LJ_DUALNUM) { - /* Ensure all slots are integers or all slots are numbers. */ - int32_t k[3]; - int nint = 0; - ptrdiff_t i; - for (i = 0; i <= 2; i++) { - if (tvisint(o+i)) { - k[i] = intV(o+i); nint++; - } else { - k[i] = lj_num2int(numV(o+i)); nint += ((lua_Number)k[i] == numV(o+i)); - } - } - if (nint == 3) { /* Narrow to integers. */ - setintV(o, k[0]); - setintV(o+1, k[1]); - setintV(o+2, k[2]); - } else if (nint != 0) { /* Widen to numbers. */ - if (tvisint(o)) setnumV(o, (lua_Number)intV(o)); - if (tvisint(o+1)) setnumV(o+1, (lua_Number)intV(o+1)); - if (tvisint(o+2)) setnumV(o+2, (lua_Number)intV(o+2)); - } - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.h deleted file mode 100644 index 3a6eaac..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_meta.h +++ /dev/null @@ -1,38 +0,0 @@ -/* -** Metamethod handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_META_H -#define _LJ_META_H - -#include "lj_obj.h" - -/* Metamethod handling */ -LJ_FUNC void lj_meta_init(lua_State *L); -LJ_FUNC cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name); -LJ_FUNC cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm); -#if LJ_HASFFI -LJ_FUNC int lj_meta_tailcall(lua_State *L, cTValue *tv); -#endif - -#define lj_meta_fastg(g, mt, mm) \ - ((mt) == NULL ? NULL : ((mt)->nomm & (1u<<(mm))) ? NULL : \ - lj_meta_cache(mt, mm, mmname_str(g, mm))) -#define lj_meta_fast(L, mt, mm) lj_meta_fastg(G(L), mt, mm) - -/* C helpers for some instructions, called from assembler VM. */ -LJ_FUNCA cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k); -LJ_FUNCA TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k); -LJ_FUNCA TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, - cTValue *rc, BCReg op); -LJ_FUNCA TValue *lj_meta_cat(lua_State *L, TValue *top, int left); -LJ_FUNCA TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o); -LJ_FUNCA TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne); -LJ_FUNCA TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins); -LJ_FUNCA TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op); -LJ_FUNCA void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp); -LJ_FUNCA void lj_meta_call(lua_State *L, TValue *func, TValue *top); -LJ_FUNCA void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o); - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.c deleted file mode 100644 index 65cbe1a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.c +++ /dev/null @@ -1,51 +0,0 @@ -/* -** Miscellaneous object handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_obj_c -#define LUA_CORE - -#include "lj_obj.h" - -/* Object type names. */ -LJ_DATADEF const char *const lj_obj_typename[] = { /* ORDER LUA_T */ - "no value", "nil", "boolean", "userdata", "number", "string", - "table", "function", "userdata", "thread", "proto", "cdata" -}; - -LJ_DATADEF const char *const lj_obj_itypename[] = { /* ORDER LJ_T */ - "nil", "boolean", "boolean", "userdata", "string", "upval", "thread", - "proto", "function", "trace", "cdata", "table", "userdata", "number" -}; - -/* Compare two objects without calling metamethods. */ -int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2) -{ - if (itype(o1) == itype(o2)) { - if (tvispri(o1)) - return 1; - if (!tvisnum(o1)) - return gcrefeq(o1->gcr, o2->gcr); - } else if (!tvisnumber(o1) || !tvisnumber(o2)) { - return 0; - } - return numberVnum(o1) == numberVnum(o2); -} - -/* Return pointer to object or its object data. */ -const void * LJ_FASTCALL lj_obj_ptr(global_State *g, cTValue *o) -{ - UNUSED(g); - if (tvisudata(o)) - return uddata(udataV(o)); - else if (tvislightud(o)) - return lightudV(g, o); - else if (LJ_HASFFI && tviscdata(o)) - return cdataptr(cdataV(o)); - else if (tvisgcv(o)) - return gcV(o); - else - return NULL; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.h deleted file mode 100644 index 67e4118..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_obj.h +++ /dev/null @@ -1,1045 +0,0 @@ -/* -** LuaJIT VM tags, values and objects. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#ifndef _LJ_OBJ_H -#define _LJ_OBJ_H - -#include "lua.h" -#include "lj_def.h" -#include "lj_arch.h" - -/* -- Memory references --------------------------------------------------- */ - -/* Memory and GC object sizes. */ -typedef uint32_t MSize; -#if LJ_GC64 -typedef uint64_t GCSize; -#else -typedef uint32_t GCSize; -#endif - -/* Memory reference */ -typedef struct MRef { -#if LJ_GC64 - uint64_t ptr64; /* True 64 bit pointer. */ -#else - uint32_t ptr32; /* Pseudo 32 bit pointer. */ -#endif -} MRef; - -#if LJ_GC64 -#define mref(r, t) ((t *)(void *)(r).ptr64) -#define mrefu(r) ((r).ptr64) - -#define setmref(r, p) ((r).ptr64 = (uint64_t)(void *)(p)) -#define setmrefu(r, u) ((r).ptr64 = (uint64_t)(u)) -#define setmrefr(r, v) ((r).ptr64 = (v).ptr64) -#else -#define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32) -#define mrefu(r) ((r).ptr32) - -#define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p)) -#define setmrefu(r, u) ((r).ptr32 = (uint32_t)(u)) -#define setmrefr(r, v) ((r).ptr32 = (v).ptr32) -#endif - -/* -- GC object references ------------------------------------------------ */ - -/* GCobj reference */ -typedef struct GCRef { -#if LJ_GC64 - uint64_t gcptr64; /* True 64 bit pointer. */ -#else - uint32_t gcptr32; /* Pseudo 32 bit pointer. */ -#endif -} GCRef; - -/* Common GC header for all collectable objects. */ -#define GCHeader GCRef nextgc; uint8_t marked; uint8_t gct -/* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */ - -#if LJ_GC64 -#define gcref(r) ((GCobj *)(r).gcptr64) -#define gcrefp(r, t) ((t *)(void *)(r).gcptr64) -#define gcrefu(r) ((r).gcptr64) -#define gcrefeq(r1, r2) ((r1).gcptr64 == (r2).gcptr64) - -#define setgcref(r, gc) ((r).gcptr64 = (uint64_t)&(gc)->gch) -#define setgcreft(r, gc, it) \ - (r).gcptr64 = (uint64_t)&(gc)->gch | (((uint64_t)(it)) << 47) -#define setgcrefp(r, p) ((r).gcptr64 = (uint64_t)(p)) -#define setgcrefnull(r) ((r).gcptr64 = 0) -#define setgcrefr(r, v) ((r).gcptr64 = (v).gcptr64) -#else -#define gcref(r) ((GCobj *)(uintptr_t)(r).gcptr32) -#define gcrefp(r, t) ((t *)(void *)(uintptr_t)(r).gcptr32) -#define gcrefu(r) ((r).gcptr32) -#define gcrefeq(r1, r2) ((r1).gcptr32 == (r2).gcptr32) - -#define setgcref(r, gc) ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch) -#define setgcrefp(r, p) ((r).gcptr32 = (uint32_t)(uintptr_t)(p)) -#define setgcrefnull(r) ((r).gcptr32 = 0) -#define setgcrefr(r, v) ((r).gcptr32 = (v).gcptr32) -#endif - -#define gcnext(gc) (gcref((gc)->gch.nextgc)) - -/* IMPORTANT NOTE: -** -** All uses of the setgcref* macros MUST be accompanied with a write barrier. -** -** This is to ensure the integrity of the incremental GC. The invariant -** to preserve is that a black object never points to a white object. -** I.e. never store a white object into a field of a black object. -** -** It's ok to LEAVE OUT the write barrier ONLY in the following cases: -** - The source is not a GC object (NULL). -** - The target is a GC root. I.e. everything in global_State. -** - The target is a lua_State field (threads are never black). -** - The target is a stack slot, see setgcV et al. -** - The target is an open upvalue, i.e. pointing to a stack slot. -** - The target is a newly created object (i.e. marked white). But make -** sure nothing invokes the GC inbetween. -** - The target and the source are the same object (self-reference). -** - The target already contains the object (e.g. moving elements around). -** -** The most common case is a store to a stack slot. All other cases where -** a barrier has been omitted are annotated with a NOBARRIER comment. -** -** The same logic applies for stores to table slots (array part or hash -** part). ALL uses of lj_tab_set* require a barrier for the stored value -** *and* the stored key, based on the above rules. In practice this means -** a barrier is needed if *either* of the key or value are a GC object. -** -** It's ok to LEAVE OUT the write barrier in the following special cases: -** - The stored value is nil. The key doesn't matter because it's either -** not resurrected or lj_tab_newkey() will take care of the key barrier. -** - The key doesn't matter if the *previously* stored value is guaranteed -** to be non-nil (because the key is kept alive in the table). -** - The key doesn't matter if it's guaranteed not to be part of the table, -** since lj_tab_newkey() takes care of the key barrier. This applies -** trivially to new tables, but watch out for resurrected keys. Storing -** a nil value leaves the key in the table! -** -** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used -** by the interpreter for all table stores. -** -** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark -** dead keys in tables. The reference is left in, but it's guaranteed to -** be never dereferenced as long as the value is nil. It's ok if the key is -** freed or if any object subsequently gets the same address. -** -** Not destroying dead keys helps to keep key hash slots stable. This avoids -** specialization back-off for HREFK when a value flips between nil and -** non-nil and the GC gets in the way. It also allows safely hoisting -** HREF/HREFK across GC steps. Dead keys are only removed if a table is -** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize. -** -** The trade-off is that a write barrier for tables must take the key into -** account, too. Implicitly resurrecting the key by storing a non-nil value -** may invalidate the incremental GC invariant. -*/ - -/* -- Common type definitions --------------------------------------------- */ - -/* Types for handling bytecodes. Need this here, details in lj_bc.h. */ -typedef uint32_t BCIns; /* Bytecode instruction. */ -typedef uint32_t BCPos; /* Bytecode position. */ -typedef uint32_t BCReg; /* Bytecode register. */ -typedef int32_t BCLine; /* Bytecode line number. */ - -/* Internal assembler functions. Never call these directly from C. */ -typedef void (*ASMFunction)(void); - -/* Resizable string buffer. Need this here, details in lj_buf.h. */ -#define SBufHeader char *w, *e, *b; MRef L -typedef struct SBuf { - SBufHeader; -} SBuf; - -/* -- Tags and values ----------------------------------------------------- */ - -/* Frame link. */ -typedef union { - int32_t ftsz; /* Frame type and size of previous frame. */ - MRef pcr; /* Or PC for Lua frames. */ -} FrameLink; - -/* Tagged value. */ -typedef LJ_ALIGN(8) union TValue { - uint64_t u64; /* 64 bit pattern overlaps number. */ - lua_Number n; /* Number object overlaps split tag/value object. */ -#if LJ_GC64 - GCRef gcr; /* GCobj reference with tag. */ - int64_t it64; - struct { - LJ_ENDIAN_LOHI( - int32_t i; /* Integer value. */ - , uint32_t it; /* Internal object tag. Must overlap MSW of number. */ - ) - }; -#else - struct { - LJ_ENDIAN_LOHI( - union { - GCRef gcr; /* GCobj reference (if any). */ - int32_t i; /* Integer value. */ - }; - , uint32_t it; /* Internal object tag. Must overlap MSW of number. */ - ) - }; -#endif -#if LJ_FR2 - int64_t ftsz; /* Frame type and size of previous frame, or PC. */ -#else - struct { - LJ_ENDIAN_LOHI( - GCRef func; /* Function for next frame (or dummy L). */ - , FrameLink tp; /* Link to previous frame. */ - ) - } fr; -#endif - struct { - LJ_ENDIAN_LOHI( - uint32_t lo; /* Lower 32 bits of number. */ - , uint32_t hi; /* Upper 32 bits of number. */ - ) - } u32; -} TValue; - -typedef const TValue cTValue; - -#define tvref(r) (mref(r, TValue)) - -/* More external and GCobj tags for internal objects. */ -#define LAST_TT LUA_TTHREAD -#define LUA_TPROTO (LAST_TT+1) -#define LUA_TCDATA (LAST_TT+2) - -/* Internal object tags. -** -** Format for 32 bit GC references (!LJ_GC64): -** -** Internal tags overlap the MSW of a number object (must be a double). -** Interpreted as a double these are special NaNs. The FPU only generates -** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available -** for use as internal tags. Small negative numbers are used to shorten the -** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate). -** -** ---MSW---.---LSW--- -** primitive types | itype | | -** lightuserdata | itype | void * | (32 bit platforms) -** lightuserdata |ffff|seg| ofs | (64 bit platforms) -** GC objects | itype | GCRef | -** int (LJ_DUALNUM)| itype | int | -** number -------double------ -** -** Format for 64 bit GC references (LJ_GC64): -** -** The upper 13 bits must be 1 (0xfff8...) for a special NaN. The next -** 4 bits hold the internal tag. The lowest 47 bits either hold a pointer, -** a zero-extended 32 bit integer or all bits set to 1 for primitive types. -** -** ------MSW------.------LSW------ -** primitive types |1..1|itype|1..................1| -** GC objects |1..1|itype|-------GCRef--------| -** lightuserdata |1..1|itype|seg|------ofs-------| -** int (LJ_DUALNUM) |1..1|itype|0..0|-----int-------| -** number ------------double------------- -** -** ORDER LJ_T -** Primitive types nil/false/true must be first, lightuserdata next. -** GC objects are at the end, table/userdata must be lowest. -** Also check lj_ir.h for similar ordering constraints. -*/ -#define LJ_TNIL (~0u) -#define LJ_TFALSE (~1u) -#define LJ_TTRUE (~2u) -#define LJ_TLIGHTUD (~3u) -#define LJ_TSTR (~4u) -#define LJ_TUPVAL (~5u) -#define LJ_TTHREAD (~6u) -#define LJ_TPROTO (~7u) -#define LJ_TFUNC (~8u) -#define LJ_TTRACE (~9u) -#define LJ_TCDATA (~10u) -#define LJ_TTAB (~11u) -#define LJ_TUDATA (~12u) -/* This is just the canonical number type used in some places. */ -#define LJ_TNUMX (~13u) - -/* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */ -#if LJ_64 && !LJ_GC64 -#define LJ_TISNUM 0xfffeffffu -#else -#define LJ_TISNUM LJ_TNUMX -#endif -#define LJ_TISTRUECOND LJ_TFALSE -#define LJ_TISPRI LJ_TTRUE -#define LJ_TISGCV (LJ_TSTR+1) -#define LJ_TISTABUD LJ_TTAB - -/* Type marker for slot holding a traversal index. Must be lightuserdata. */ -#define LJ_KEYINDEX 0xfffe7fffu - -#if LJ_GC64 -#define LJ_GCVMASK (((uint64_t)1 << 47) - 1) -#endif - -#if LJ_64 -/* To stay within 47 bits, lightuserdata is segmented. */ -#define LJ_LIGHTUD_BITS_SEG 8 -#define LJ_LIGHTUD_BITS_LO (47 - LJ_LIGHTUD_BITS_SEG) -#endif - -/* -- String object ------------------------------------------------------- */ - -typedef uint32_t StrHash; /* String hash value. */ -typedef uint32_t StrID; /* String ID. */ - -/* String object header. String payload follows. */ -typedef struct GCstr { - GCHeader; - uint8_t reserved; /* Used by lexer for fast lookup of reserved words. */ - uint8_t hashalg; /* Hash algorithm. */ - StrID sid; /* Interned string ID. */ - StrHash hash; /* Hash of string. */ - MSize len; /* Size of string. */ -} GCstr; - -#define strref(r) (&gcref((r))->str) -#define strdata(s) ((const char *)((s)+1)) -#define strdatawr(s) ((char *)((s)+1)) -#define strVdata(o) strdata(strV(o)) - -/* -- Userdata object ----------------------------------------------------- */ - -/* Userdata object. Payload follows. */ -typedef struct GCudata { - GCHeader; - uint8_t udtype; /* Userdata type. */ - uint8_t unused2; - GCRef env; /* Should be at same offset in GCfunc. */ - MSize len; /* Size of payload. */ - GCRef metatable; /* Must be at same offset in GCtab. */ - uint32_t align1; /* To force 8 byte alignment of the payload. */ -} GCudata; - -/* Userdata types. */ -enum { - UDTYPE_USERDATA, /* Regular userdata. */ - UDTYPE_IO_FILE, /* I/O library FILE. */ - UDTYPE_FFI_CLIB, /* FFI C library namespace. */ - UDTYPE_BUFFER, /* String buffer. */ - UDTYPE__MAX -}; - -#define uddata(u) ((void *)((u)+1)) -#define sizeudata(u) (sizeof(struct GCudata)+(u)->len) - -/* -- C data object ------------------------------------------------------- */ - -/* C data object. Payload follows. */ -typedef struct GCcdata { - GCHeader; - uint16_t ctypeid; /* C type ID. */ -} GCcdata; - -/* Prepended to variable-sized or realigned C data objects. */ -typedef struct GCcdataVar { - uint16_t offset; /* Offset to allocated memory (relative to GCcdata). */ - uint16_t extra; /* Extra space allocated (incl. GCcdata + GCcdatav). */ - MSize len; /* Size of payload. */ -} GCcdataVar; - -#define cdataptr(cd) ((void *)((cd)+1)) -#define cdataisv(cd) ((cd)->marked & 0x80) -#define cdatav(cd) ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar))) -#define cdatavlen(cd) check_exp(cdataisv(cd), cdatav(cd)->len) -#define sizecdatav(cd) (cdatavlen(cd) + cdatav(cd)->extra) -#define memcdatav(cd) ((void *)((char *)(cd) - cdatav(cd)->offset)) - -/* -- Prototype object ---------------------------------------------------- */ - -#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) -#define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) - -typedef struct GCproto { - GCHeader; - uint8_t numparams; /* Number of parameters. */ - uint8_t framesize; /* Fixed frame size. */ - MSize sizebc; /* Number of bytecode instructions. */ -#if LJ_GC64 - uint32_t unused_gc64; -#endif - GCRef gclist; - MRef k; /* Split constant array (points to the middle). */ - MRef uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */ - MSize sizekgc; /* Number of collectable constants. */ - MSize sizekn; /* Number of lua_Number constants. */ - MSize sizept; /* Total size including colocated arrays. */ - uint8_t sizeuv; /* Number of upvalues. */ - uint8_t flags; /* Miscellaneous flags (see below). */ - uint16_t trace; /* Anchor for chain of root traces. */ - /* ------ The following fields are for debugging/tracebacks only ------ */ - GCRef chunkname; /* Name of the chunk this function was defined in. */ - BCLine firstline; /* First line of the function definition. */ - BCLine numline; /* Number of lines for the function definition. */ - MRef lineinfo; /* Compressed map from bytecode ins. to source line. */ - MRef uvinfo; /* Upvalue names. */ - MRef varinfo; /* Names and compressed extents of local variables. */ -} GCproto; - -/* Flags for prototype. */ -#define PROTO_CHILD 0x01 /* Has child prototypes. */ -#define PROTO_VARARG 0x02 /* Vararg function. */ -#define PROTO_FFI 0x04 /* Uses BC_KCDATA for FFI datatypes. */ -#define PROTO_NOJIT 0x08 /* JIT disabled for this function. */ -#define PROTO_ILOOP 0x10 /* Patched bytecode with ILOOP etc. */ -/* Only used during parsing. */ -#define PROTO_HAS_RETURN 0x20 /* Already emitted a return. */ -#define PROTO_FIXUP_RETURN 0x40 /* Need to fixup emitted returns. */ -/* Top bits used for counting created closures. */ -#define PROTO_CLCOUNT 0x20 /* Base of saturating 3 bit counter. */ -#define PROTO_CLC_BITS 3 -#define PROTO_CLC_POLY (3*PROTO_CLCOUNT) /* Polymorphic threshold. */ - -#define PROTO_UV_LOCAL 0x8000 /* Upvalue for local slot. */ -#define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */ - -#define proto_kgc(pt, idx) \ - check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \ - gcref(mref((pt)->k, GCRef)[(idx)])) -#define proto_knumtv(pt, idx) \ - check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)]) -#define proto_bc(pt) ((BCIns *)((char *)(pt) + sizeof(GCproto))) -#define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt))) -#define proto_uv(pt) (mref((pt)->uv, uint16_t)) - -#define proto_chunkname(pt) (strref((pt)->chunkname)) -#define proto_chunknamestr(pt) (strdata(proto_chunkname((pt)))) -#define proto_lineinfo(pt) (mref((pt)->lineinfo, const void)) -#define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t)) -#define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t)) - -/* -- Upvalue object ------------------------------------------------------ */ - -typedef struct GCupval { - GCHeader; - uint8_t closed; /* Set if closed (i.e. uv->v == &uv->u.value). */ - uint8_t immutable; /* Immutable value. */ - union { - TValue tv; /* If closed: the value itself. */ - struct { /* If open: double linked list, anchored at thread. */ - GCRef prev; - GCRef next; - }; - }; - MRef v; /* Points to stack slot (open) or above (closed). */ - uint32_t dhash; /* Disambiguation hash: dh1 != dh2 => cannot alias. */ -} GCupval; - -#define uvprev(uv_) (&gcref((uv_)->prev)->uv) -#define uvnext(uv_) (&gcref((uv_)->next)->uv) -#define uvval(uv_) (mref((uv_)->v, TValue)) - -/* -- Function object (closures) ------------------------------------------ */ - -/* Common header for functions. env should be at same offset in GCudata. */ -#define GCfuncHeader \ - GCHeader; uint8_t ffid; uint8_t nupvalues; \ - GCRef env; GCRef gclist; MRef pc - -typedef struct GCfuncC { - GCfuncHeader; - lua_CFunction f; /* C function to be called. */ - TValue upvalue[1]; /* Array of upvalues (TValue). */ -} GCfuncC; - -typedef struct GCfuncL { - GCfuncHeader; - GCRef uvptr[1]; /* Array of _pointers_ to upvalue objects (GCupval). */ -} GCfuncL; - -typedef union GCfunc { - GCfuncC c; - GCfuncL l; -} GCfunc; - -#define FF_LUA 0 -#define FF_C 1 -#define isluafunc(fn) ((fn)->c.ffid == FF_LUA) -#define iscfunc(fn) ((fn)->c.ffid == FF_C) -#define isffunc(fn) ((fn)->c.ffid > FF_C) -#define funcproto(fn) \ - check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto))) -#define sizeCfunc(n) (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n)) -#define sizeLfunc(n) (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n)) - -/* -- Table object -------------------------------------------------------- */ - -/* Hash node. */ -typedef struct Node { - TValue val; /* Value object. Must be first field. */ - TValue key; /* Key object. */ - MRef next; /* Hash chain. */ -#if !LJ_GC64 - MRef freetop; /* Top of free elements (stored in t->node[0]). */ -#endif -} Node; - -LJ_STATIC_ASSERT(offsetof(Node, val) == 0); - -typedef struct GCtab { - GCHeader; - uint8_t nomm; /* Negative cache for fast metamethods. */ - int8_t colo; /* Array colocation. */ - MRef array; /* Array part. */ - GCRef gclist; - GCRef metatable; /* Must be at same offset in GCudata. */ - MRef node; /* Hash part. */ - uint32_t asize; /* Size of array part (keys [0, asize-1]). */ - uint32_t hmask; /* Hash part mask (size of hash part - 1). */ -#if LJ_GC64 - MRef freetop; /* Top of free elements. */ -#endif -} GCtab; - -#define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab)) -#define tabref(r) ((GCtab *)gcref((r))) -#define noderef(r) (mref((r), Node)) -#define nextnode(n) (mref((n)->next, Node)) -#if LJ_GC64 -#define getfreetop(t, n) (noderef((t)->freetop)) -#define setfreetop(t, n, v) (setmref((t)->freetop, (v))) -#else -#define getfreetop(t, n) (noderef((n)->freetop)) -#define setfreetop(t, n, v) (setmref((n)->freetop, (v))) -#endif - -/* -- State objects ------------------------------------------------------- */ - -/* VM states. */ -enum { - LJ_VMST_INTERP, /* Interpreter. */ - LJ_VMST_C, /* C function. */ - LJ_VMST_GC, /* Garbage collector. */ - LJ_VMST_EXIT, /* Trace exit handler. */ - LJ_VMST_RECORD, /* Trace recorder. */ - LJ_VMST_OPT, /* Optimizer. */ - LJ_VMST_ASM, /* Assembler. */ - LJ_VMST__MAX -}; - -#define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st) - -/* Metamethods. ORDER MM */ -#ifdef LJ_HASFFI -#define MMDEF_FFI(_) _(new) -#else -#define MMDEF_FFI(_) -#endif - -#if LJ_52 || LJ_HASFFI -#define MMDEF_PAIRS(_) _(pairs) _(ipairs) -#else -#define MMDEF_PAIRS(_) -#define MM_pairs 255 -#define MM_ipairs 255 -#endif - -#define MMDEF(_) \ - _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \ - /* Only the above (fast) metamethods are negative cached (max. 8). */ \ - _(lt) _(le) _(concat) _(call) \ - /* The following must be in ORDER ARITH. */ \ - _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \ - /* The following are used in the standard libraries. */ \ - _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_) - -typedef enum { -#define MMENUM(name) MM_##name, -MMDEF(MMENUM) -#undef MMENUM - MM__MAX, - MM____ = MM__MAX, - MM_FAST = MM_len -} MMS; - -/* GC root IDs. */ -typedef enum { - GCROOT_MMNAME, /* Metamethod names. */ - GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1, - GCROOT_BASEMT, /* Metatables for base types. */ - GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX, - GCROOT_IO_INPUT, /* Userdata for default I/O input file. */ - GCROOT_IO_OUTPUT, /* Userdata for default I/O output file. */ - GCROOT_MAX -} GCRootID; - -#define basemt_it(g, it) ((g)->gcroot[GCROOT_BASEMT+~(it)]) -#define basemt_obj(g, o) ((g)->gcroot[GCROOT_BASEMT+itypemap(o)]) -#define mmname_str(g, mm) (strref((g)->gcroot[GCROOT_MMNAME+(mm)])) - -/* Garbage collector state. */ -typedef struct GCState { - GCSize total; /* Memory currently allocated. */ - GCSize threshold; /* Memory threshold. */ - uint8_t currentwhite; /* Current white color. */ - uint8_t state; /* GC state. */ - uint8_t nocdatafin; /* No cdata finalizer called. */ -#if LJ_64 - uint8_t lightudnum; /* Number of lightuserdata segments - 1. */ -#else - uint8_t unused1; -#endif - MSize sweepstr; /* Sweep position in string table. */ - GCRef root; /* List of all collectable objects. */ - MRef sweep; /* Sweep position in root list. */ - GCRef gray; /* List of gray objects. */ - GCRef grayagain; /* List of objects for atomic traversal. */ - GCRef weak; /* List of weak tables (to be cleared). */ - GCRef mmudata; /* List of userdata (to be finalized). */ - GCSize debt; /* Debt (how much GC is behind schedule). */ - GCSize estimate; /* Estimate of memory actually in use. */ - MSize stepmul; /* Incremental GC step granularity. */ - MSize pause; /* Pause between successive GC cycles. */ -#if LJ_64 - MRef lightudseg; /* Upper bits of lightuserdata segments. */ -#endif -} GCState; - -/* String interning state. */ -typedef struct StrInternState { - GCRef *tab; /* String hash table anchors. */ - MSize mask; /* String hash mask (size of hash table - 1). */ - MSize num; /* Number of strings in hash table. */ - StrID id; /* Next string ID. */ - uint8_t idreseed; /* String ID reseed counter. */ - uint8_t second; /* String interning table uses secondary hashing. */ - uint8_t unused1; - uint8_t unused2; - LJ_ALIGN(8) uint64_t seed; /* Random string seed. */ -} StrInternState; - -/* Global state, shared by all threads of a Lua universe. */ -typedef struct global_State { - lua_Alloc allocf; /* Memory allocator. */ - void *allocd; /* Memory allocator data. */ - GCState gc; /* Garbage collector. */ - GCstr strempty; /* Empty string. */ - uint8_t stremptyz; /* Zero terminator of empty string. */ - uint8_t hookmask; /* Hook mask. */ - uint8_t dispatchmode; /* Dispatch mode. */ - uint8_t vmevmask; /* VM event mask. */ - StrInternState str; /* String interning. */ - volatile int32_t vmstate; /* VM state or current JIT code trace number. */ - GCRef mainthref; /* Link to main thread. */ - SBuf tmpbuf; /* Temporary string buffer. */ - TValue tmptv, tmptv2; /* Temporary TValues. */ - Node nilnode; /* Fallback 1-element hash part (nil key and value). */ - TValue registrytv; /* Anchor for registry. */ - GCupval uvhead; /* Head of double-linked list of all open upvalues. */ - int32_t hookcount; /* Instruction hook countdown. */ - int32_t hookcstart; /* Start count for instruction hook counter. */ - lua_Hook hookf; /* Hook function. */ - lua_CFunction wrapf; /* Wrapper for C function calls. */ - lua_CFunction panic; /* Called as a last resort for errors. */ - BCIns bc_cfunc_int; /* Bytecode for internal C function calls. */ - BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */ - GCRef cur_L; /* Currently executing lua_State. */ - MRef jit_base; /* Current JIT code L->base or NULL. */ - MRef ctype_state; /* Pointer to C type state. */ - PRNGState prng; /* Global PRNG state. */ - GCRef gcroot[GCROOT_MAX]; /* GC roots. */ -} global_State; - -#define mainthread(g) (&gcref(g->mainthref)->th) -#define niltv(L) \ - check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val) -#define niltvg(g) \ - check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val) - -/* Hook management. Hook event masks are defined in lua.h. */ -#define HOOK_EVENTMASK 0x0f -#define HOOK_ACTIVE 0x10 -#define HOOK_ACTIVE_SHIFT 4 -#define HOOK_VMEVENT 0x20 -#define HOOK_GC 0x40 -#define HOOK_PROFILE 0x80 -#define hook_active(g) ((g)->hookmask & HOOK_ACTIVE) -#define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE) -#define hook_entergc(g) \ - ((g)->hookmask = ((g)->hookmask | (HOOK_ACTIVE|HOOK_GC)) & ~HOOK_PROFILE) -#define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT)) -#define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE) -#define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK) -#define hook_restore(g, h) \ - ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h)) - -/* Per-thread state object. */ -struct lua_State { - GCHeader; - uint8_t dummy_ffid; /* Fake FF_C for curr_funcisL() on dummy frames. */ - uint8_t status; /* Thread status. */ - MRef glref; /* Link to global state. */ - GCRef gclist; /* GC chain. */ - TValue *base; /* Base of currently executing function. */ - TValue *top; /* First free slot in the stack. */ - MRef maxstack; /* Last free slot in the stack. */ - MRef stack; /* Stack base. */ - GCRef openupval; /* List of open upvalues in the stack. */ - GCRef env; /* Thread environment (table of globals). */ - void *cframe; /* End of C stack frame chain. */ - MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */ -}; - -#define G(L) (mref(L->glref, global_State)) -#define registry(L) (&G(L)->registrytv) - -/* Macros to access the currently executing (Lua) function. */ -#if LJ_GC64 -#define curr_func(L) (&gcval(L->base-2)->fn) -#elif LJ_FR2 -#define curr_func(L) (&gcref((L->base-2)->gcr)->fn) -#else -#define curr_func(L) (&gcref((L->base-1)->fr.func)->fn) -#endif -#define curr_funcisL(L) (isluafunc(curr_func(L))) -#define curr_proto(L) (funcproto(curr_func(L))) -#define curr_topL(L) (L->base + curr_proto(L)->framesize) -#define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top) - -#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) -LJ_FUNC_NORET void lj_assert_fail(global_State *g, const char *file, int line, - const char *func, const char *fmt, ...); -#endif - -/* -- GC object definition and conversions -------------------------------- */ - -/* GC header for generic access to common fields of GC objects. */ -typedef struct GChead { - GCHeader; - uint8_t unused1; - uint8_t unused2; - GCRef env; - GCRef gclist; - GCRef metatable; -} GChead; - -/* The env field SHOULD be at the same offset for all GC objects. */ -LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env)); -LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env)); - -/* The metatable field MUST be at the same offset for all GC objects. */ -LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable)); -LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable)); - -/* The gclist field MUST be at the same offset for all GC objects. */ -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist)); -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist)); -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist)); -LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist)); - -typedef union GCobj { - GChead gch; - GCstr str; - GCupval uv; - lua_State th; - GCproto pt; - GCfunc fn; - GCcdata cd; - GCtab tab; - GCudata ud; -} GCobj; - -/* Macros to convert a GCobj pointer into a specific value. */ -#define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str) -#define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv) -#define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th) -#define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt) -#define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn) -#define gco2cd(o) check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd) -#define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab) -#define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud) - -/* Macro to convert any collectable object into a GCobj pointer. */ -#define obj2gco(v) ((GCobj *)(v)) - -/* -- TValue getters/setters ---------------------------------------------- */ - -/* Macros to test types. */ -#if LJ_GC64 -#define itype(o) ((uint32_t)((o)->it64 >> 47)) -#define tvisnil(o) ((o)->it64 == -1) -#else -#define itype(o) ((o)->it) -#define tvisnil(o) (itype(o) == LJ_TNIL) -#endif -#define tvisfalse(o) (itype(o) == LJ_TFALSE) -#define tvistrue(o) (itype(o) == LJ_TTRUE) -#define tvisbool(o) (tvisfalse(o) || tvistrue(o)) -#if LJ_64 && !LJ_GC64 -#define tvislightud(o) (((int32_t)itype(o) >> 15) == -2) -#else -#define tvislightud(o) (itype(o) == LJ_TLIGHTUD) -#endif -#define tvisstr(o) (itype(o) == LJ_TSTR) -#define tvisfunc(o) (itype(o) == LJ_TFUNC) -#define tvisthread(o) (itype(o) == LJ_TTHREAD) -#define tvisproto(o) (itype(o) == LJ_TPROTO) -#define tviscdata(o) (itype(o) == LJ_TCDATA) -#define tvistab(o) (itype(o) == LJ_TTAB) -#define tvisudata(o) (itype(o) == LJ_TUDATA) -#define tvisnumber(o) (itype(o) <= LJ_TISNUM) -#define tvisint(o) (LJ_DUALNUM && itype(o) == LJ_TISNUM) -#define tvisnum(o) (itype(o) < LJ_TISNUM) - -#define tvistruecond(o) (itype(o) < LJ_TISTRUECOND) -#define tvispri(o) (itype(o) >= LJ_TISPRI) -#define tvistabud(o) (itype(o) <= LJ_TISTABUD) /* && !tvisnum() */ -#define tvisgcv(o) ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV)) - -/* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */ -#define tvisnan(o) ((o)->n != (o)->n) -#if LJ_64 -#define tviszero(o) (((o)->u64 << 1) == 0) -#else -#define tviszero(o) (((o)->u32.lo | ((o)->u32.hi << 1)) == 0) -#endif -#define tvispzero(o) ((o)->u64 == 0) -#define tvismzero(o) ((o)->u64 == U64x(80000000,00000000)) -#define tvispone(o) ((o)->u64 == U64x(3ff00000,00000000)) -#define rawnumequal(o1, o2) ((o1)->u64 == (o2)->u64) - -/* Macros to convert type ids. */ -#if LJ_64 && !LJ_GC64 -#define itypemap(o) \ - (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o)) -#else -#define itypemap(o) (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o)) -#endif - -/* Macros to get tagged values. */ -#if LJ_GC64 -#define gcval(o) ((GCobj *)(gcrefu((o)->gcr) & LJ_GCVMASK)) -#else -#define gcval(o) (gcref((o)->gcr)) -#endif -#define boolV(o) check_exp(tvisbool(o), (LJ_TFALSE - itype(o))) -#if LJ_64 -#define lightudseg(u) \ - (((u) >> LJ_LIGHTUD_BITS_LO) & ((1 << LJ_LIGHTUD_BITS_SEG)-1)) -#define lightudlo(u) \ - ((u) & (((uint64_t)1 << LJ_LIGHTUD_BITS_LO) - 1)) -#define lightudup(p) \ - ((uint32_t)(((p) >> LJ_LIGHTUD_BITS_LO) << (LJ_LIGHTUD_BITS_LO-32))) -static LJ_AINLINE void *lightudV(global_State *g, cTValue *o) -{ - uint64_t u = o->u64; - uint64_t seg = lightudseg(u); - uint32_t *segmap = mref(g->gc.lightudseg, uint32_t); - lj_assertG(tvislightud(o), "lightuserdata expected"); - if (seg == (1 << LJ_LIGHTUD_BITS_SEG)-1) return NULL; - lj_assertG(seg <= g->gc.lightudnum, "bad lightuserdata segment %d", seg); - return (void *)(((uint64_t)segmap[seg] << 32) | lightudlo(u)); -} -#else -#define lightudV(g, o) check_exp(tvislightud(o), gcrefp((o)->gcr, void)) -#endif -#define gcV(o) check_exp(tvisgcv(o), gcval(o)) -#define strV(o) check_exp(tvisstr(o), &gcval(o)->str) -#define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn) -#define threadV(o) check_exp(tvisthread(o), &gcval(o)->th) -#define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt) -#define cdataV(o) check_exp(tviscdata(o), &gcval(o)->cd) -#define tabV(o) check_exp(tvistab(o), &gcval(o)->tab) -#define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud) -#define numV(o) check_exp(tvisnum(o), (o)->n) -#define intV(o) check_exp(tvisint(o), (int32_t)(o)->i) - -/* Macros to set tagged values. */ -#if LJ_GC64 -#define setitype(o, i) ((o)->it = ((i) << 15)) -#define setnilV(o) ((o)->it64 = -1) -#define setpriV(o, x) ((o)->it64 = (int64_t)~((uint64_t)~(x)<<47)) -#define setboolV(o, x) ((o)->it64 = (int64_t)~((uint64_t)((x)+1)<<47)) -#else -#define setitype(o, i) ((o)->it = (i)) -#define setnilV(o) ((o)->it = LJ_TNIL) -#define setboolV(o, x) ((o)->it = LJ_TFALSE-(uint32_t)(x)) -#define setpriV(o, i) (setitype((o), (i))) -#endif - -static LJ_AINLINE void setrawlightudV(TValue *o, void *p) -{ -#if LJ_GC64 - o->u64 = (uint64_t)p | (((uint64_t)LJ_TLIGHTUD) << 47); -#elif LJ_64 - o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48); -#else - setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD); -#endif -} - -#if LJ_FR2 || LJ_32 -#define contptr(f) ((void *)(f)) -#define setcont(o, f) ((o)->u64 = (uint64_t)(uintptr_t)contptr(f)) -#else -#define contptr(f) \ - ((void *)(uintptr_t)(uint32_t)((intptr_t)(f) - (intptr_t)lj_vm_asm_begin)) -#define setcont(o, f) \ - ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin) -#endif - -static LJ_AINLINE void checklivetv(lua_State *L, TValue *o, const char *msg) -{ - UNUSED(L); UNUSED(o); UNUSED(msg); -#if LUA_USE_ASSERT - if (tvisgcv(o)) { - lj_assertL(~itype(o) == gcval(o)->gch.gct, - "mismatch of TValue type %d vs GC type %d", - ~itype(o), gcval(o)->gch.gct); - /* Copy of isdead check from lj_gc.h to avoid circular include. */ - lj_assertL(!(gcval(o)->gch.marked & (G(L)->gc.currentwhite ^ 3) & 3), msg); - } -#endif -} - -static LJ_AINLINE void setgcVraw(TValue *o, GCobj *v, uint32_t itype) -{ -#if LJ_GC64 - setgcreft(o->gcr, v, itype); -#else - setgcref(o->gcr, v); setitype(o, itype); -#endif -} - -static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t it) -{ - setgcVraw(o, v, it); - checklivetv(L, o, "store to dead GC object"); -} - -#define define_setV(name, type, tag) \ -static LJ_AINLINE void name(lua_State *L, TValue *o, const type *v) \ -{ \ - setgcV(L, o, obj2gco(v), tag); \ -} -define_setV(setstrV, GCstr, LJ_TSTR) -define_setV(setthreadV, lua_State, LJ_TTHREAD) -define_setV(setprotoV, GCproto, LJ_TPROTO) -define_setV(setfuncV, GCfunc, LJ_TFUNC) -define_setV(setcdataV, GCcdata, LJ_TCDATA) -define_setV(settabV, GCtab, LJ_TTAB) -define_setV(setudataV, GCudata, LJ_TUDATA) - -#define setnumV(o, x) ((o)->n = (x)) -#define setnanV(o) ((o)->u64 = U64x(fff80000,00000000)) -#define setpinfV(o) ((o)->u64 = U64x(7ff00000,00000000)) -#define setminfV(o) ((o)->u64 = U64x(fff00000,00000000)) - -static LJ_AINLINE void setintV(TValue *o, int32_t i) -{ -#if LJ_DUALNUM - o->i = (uint32_t)i; setitype(o, LJ_TISNUM); -#else - o->n = (lua_Number)i; -#endif -} - -static LJ_AINLINE void setint64V(TValue *o, int64_t i) -{ - if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i)) - setintV(o, (int32_t)i); - else - setnumV(o, (lua_Number)i); -} - -#if LJ_64 -#define setintptrV(o, i) setint64V((o), (i)) -#else -#define setintptrV(o, i) setintV((o), (i)) -#endif - -/* Copy tagged values. */ -static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2) -{ - *o1 = *o2; - checklivetv(L, o1, "copy of dead GC object"); -} - -/* -- Number to integer conversion ---------------------------------------- */ - -#if LJ_SOFTFP -LJ_ASMF int32_t lj_vm_tobit(double x); -#if LJ_TARGET_MIPS64 -LJ_ASMF int32_t lj_vm_tointg(double x); -#endif -#endif - -static LJ_AINLINE int32_t lj_num2bit(lua_Number n) -{ -#if LJ_SOFTFP - return lj_vm_tobit(n); -#else - TValue o; - o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */ - return (int32_t)o.u32.lo; -#endif -} - -#define lj_num2int(n) ((int32_t)(n)) - -/* -** This must match the JIT backend behavior. In particular for archs -** that don't have a common hardware instruction for this conversion. -** Note that signed FP to unsigned int conversions have an undefined -** result and should never be relied upon in portable FFI code. -** See also: C99 or C11 standard, 6.3.1.4, footnote of (1). -*/ -static LJ_AINLINE uint64_t lj_num2u64(lua_Number n) -{ -#if LJ_TARGET_X86ORX64 || LJ_TARGET_MIPS - int64_t i = (int64_t)n; - if (i < 0) i = (int64_t)(n - 18446744073709551616.0); - return (uint64_t)i; -#else - return (uint64_t)n; -#endif -} - -static LJ_AINLINE int32_t numberVint(cTValue *o) -{ - if (LJ_LIKELY(tvisint(o))) - return intV(o); - else - return lj_num2int(numV(o)); -} - -static LJ_AINLINE lua_Number numberVnum(cTValue *o) -{ - if (LJ_UNLIKELY(tvisint(o))) - return (lua_Number)intV(o); - else - return numV(o); -} - -/* -- Miscellaneous object handling --------------------------------------- */ - -/* Names and maps for internal and external object tags. */ -LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1]; -LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1]; - -#define lj_typename(o) (lj_obj_itypename[itypemap(o)]) - -/* Compare two objects without calling metamethods. */ -LJ_FUNC int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2); -LJ_FUNC const void * LJ_FASTCALL lj_obj_ptr(global_State *g, cTValue *o); - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_dce.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_dce.c deleted file mode 100644 index cff54a8..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_dce.c +++ /dev/null @@ -1,75 +0,0 @@ -/* -** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_dce_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Scan through all snapshots and mark all referenced instructions. */ -static void dce_marksnap(jit_State *J) -{ - SnapNo i, nsnap = J->cur.nsnap; - for (i = 0; i < nsnap; i++) { - SnapShot *snap = &J->cur.snap[i]; - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - IRRef ref = snap_ref(map[n]); - if (ref >= REF_FIRST) - irt_setmark(IR(ref)->t); - } - } -} - -/* Backwards propagate marks. Replace unused instructions with NOPs. */ -static void dce_propagate(jit_State *J) -{ - IRRef1 *pchain[IR__MAX]; - IRRef ins; - uint32_t i; - for (i = 0; i < IR__MAX; i++) pchain[i] = &J->chain[i]; - for (ins = J->cur.nins-1; ins >= REF_FIRST; ins--) { - IRIns *ir = IR(ins); - if (irt_ismarked(ir->t)) { - irt_clearmark(ir->t); - pchain[ir->o] = &ir->prev; - } else if (!ir_sideeff(ir)) { - *pchain[ir->o] = ir->prev; /* Reroute original instruction chain. */ - lj_ir_nop(ir); - continue; - } - if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); - if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); - } -} - -/* Dead Code Elimination. -** -** First backpropagate marks for all used instructions. Then replace -** the unused ones with a NOP. Note that compressing the IR to eliminate -** the NOPs does not pay off. -*/ -void lj_opt_dce(jit_State *J) -{ - if ((J->flags & JIT_F_OPT_DCE)) { - dce_marksnap(J); - dce_propagate(J); - memset(J->bpropcache, 0, sizeof(J->bpropcache)); /* Invalidate cache. */ - } -} - -#undef IR - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_fold.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_fold.c deleted file mode 100644 index 7ef09a1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_fold.c +++ /dev/null @@ -1,2602 +0,0 @@ -/* -** FOLD: Constant Folding, Algebraic Simplifications and Reassociation. -** ABCelim: Array Bounds Check Elimination. -** CSE: Common-Subexpression Elimination. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_fold_c -#define LUA_CORE - -#include - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_carith.h" -#endif -#include "lj_vm.h" -#include "lj_strscan.h" -#include "lj_strfmt.h" - -/* Here's a short description how the FOLD engine processes instructions: -** -** The FOLD engine receives a single instruction stored in fins (J->fold.ins). -** The instruction and its operands are used to select matching fold rules. -** These are applied iteratively until a fixed point is reached. -** -** The 8 bit opcode of the instruction itself plus the opcodes of the -** two instructions referenced by its operands form a 24 bit key -** 'ins left right' (unused operands -> 0, literals -> lowest 8 bits). -** -** This key is used for partial matching against the fold rules. The -** left/right operand fields of the key are successively masked with -** the 'any' wildcard, from most specific to least specific: -** -** ins left right -** ins any right -** ins left any -** ins any any -** -** The masked key is used to lookup a matching fold rule in a semi-perfect -** hash table. If a matching rule is found, the related fold function is run. -** Multiple rules can share the same fold function. A fold rule may return -** one of several special values: -** -** - NEXTFOLD means no folding was applied, because an additional test -** inside the fold function failed. Matching continues against less -** specific fold rules. Finally the instruction is passed on to CSE. -** -** - RETRYFOLD means the instruction was modified in-place. Folding is -** retried as if this instruction had just been received. -** -** All other return values are terminal actions -- no further folding is -** applied: -** -** - INTFOLD(i) returns a reference to the integer constant i. -** -** - LEFTFOLD and RIGHTFOLD return the left/right operand reference -** without emitting an instruction. -** -** - CSEFOLD and EMITFOLD pass the instruction directly to CSE or emit -** it without passing through any further optimizations. -** -** - FAILFOLD, DROPFOLD and CONDFOLD only apply to instructions which have -** no result (e.g. guarded assertions): FAILFOLD means the guard would -** always fail, i.e. the current trace is pointless. DROPFOLD means -** the guard is always true and has been eliminated. CONDFOLD is a -** shortcut for FAILFOLD + cond (i.e. drop if true, otherwise fail). -** -** - Any other return value is interpreted as an IRRef or TRef. This -** can be a reference to an existing or a newly created instruction. -** Only the least-significant 16 bits (IRRef1) are used to form a TRef -** which is finally returned to the caller. -** -** The FOLD engine receives instructions both from the trace recorder and -** substituted instructions from LOOP unrolling. This means all types -** of instructions may end up here, even though the recorder bypasses -** FOLD in some cases. Thus all loads, stores and allocations must have -** an any/any rule to avoid being passed on to CSE. -** -** Carefully read the following requirements before adding or modifying -** any fold rules: -** -** Requirement #1: All fold rules must preserve their destination type. -** -** Consistently use INTFOLD() (KINT result) or lj_ir_knum() (KNUM result). -** Never use lj_ir_knumint() which can have either a KINT or KNUM result. -** -** Requirement #2: Fold rules should not create *new* instructions which -** reference operands *across* PHIs. -** -** E.g. a RETRYFOLD with 'fins->op1 = fleft->op1' is invalid if the -** left operand is a PHI. Then fleft->op1 would point across the PHI -** frontier to an invariant instruction. Adding a PHI for this instruction -** would be counterproductive. The solution is to add a barrier which -** prevents folding across PHIs, i.e. 'PHIBARRIER(fleft)' in this case. -** The only exception is for recurrences with high latencies like -** repeated int->num->int conversions. -** -** One could relax this condition a bit if the referenced instruction is -** a PHI, too. But this often leads to worse code due to excessive -** register shuffling. -** -** Note: returning *existing* instructions (e.g. LEFTFOLD) is ok, though. -** Even returning fleft->op1 would be ok, because a new PHI will added, -** if needed. But again, this leads to excessive register shuffling and -** should be avoided. -** -** Requirement #3: The set of all fold rules must be monotonic to guarantee -** termination. -** -** The goal is optimization, so one primarily wants to add strength-reducing -** rules. This means eliminating an instruction or replacing an instruction -** with one or more simpler instructions. Don't add fold rules which point -** into the other direction. -** -** Some rules (like commutativity) do not directly reduce the strength of -** an instruction, but enable other fold rules (e.g. by moving constants -** to the right operand). These rules must be made unidirectional to avoid -** cycles. -** -** Rule of thumb: the trace recorder expands the IR and FOLD shrinks it. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) -#define fleft (J->fold.left) -#define fright (J->fold.right) -#define knumleft (ir_knum(fleft)->n) -#define knumright (ir_knum(fright)->n) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Fold function type. Fastcall on x86 significantly reduces their size. */ -typedef IRRef (LJ_FASTCALL *FoldFunc)(jit_State *J); - -/* Macros for the fold specs, so buildvm can recognize them. */ -#define LJFOLD(x) -#define LJFOLDX(x) -#define LJFOLDF(name) static TRef LJ_FASTCALL fold_##name(jit_State *J) -/* Note: They must be at the start of a line or buildvm ignores them! */ - -/* Barrier to prevent using operands across PHIs. */ -#define PHIBARRIER(ir) if (irt_isphi((ir)->t)) return NEXTFOLD - -/* Barrier to prevent folding across a GC step. -** GC steps can only happen at the head of a trace and at LOOP. -** And the GC is only driven forward if there's at least one allocation. -*/ -#define gcstep_barrier(J, ref) \ - ((ref) < J->chain[IR_LOOP] && \ - (J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \ - J->chain[IR_TNEW] || J->chain[IR_TDUP] || \ - J->chain[IR_CNEW] || J->chain[IR_CNEWI] || \ - J->chain[IR_BUFSTR] || J->chain[IR_TOSTR] || J->chain[IR_CALLA])) - -/* -- Constant folding for FP numbers ------------------------------------- */ - -LJFOLD(ADD KNUM KNUM) -LJFOLD(SUB KNUM KNUM) -LJFOLD(MUL KNUM KNUM) -LJFOLD(DIV KNUM KNUM) -LJFOLD(LDEXP KNUM KNUM) -LJFOLD(MIN KNUM KNUM) -LJFOLD(MAX KNUM KNUM) -LJFOLDF(kfold_numarith) -{ - lua_Number a = knumleft; - lua_Number b = knumright; - lua_Number y = lj_vm_foldarith(a, b, fins->o - IR_ADD); - return lj_ir_knum(J, y); -} - -LJFOLD(NEG KNUM FLOAD) -LJFOLD(ABS KNUM FLOAD) -LJFOLDF(kfold_numabsneg) -{ - lua_Number a = knumleft; - lua_Number y = lj_vm_foldarith(a, a, fins->o - IR_ADD); - return lj_ir_knum(J, y); -} - -LJFOLD(LDEXP KNUM KINT) -LJFOLDF(kfold_ldexp) -{ -#if LJ_TARGET_X86ORX64 - UNUSED(J); - return NEXTFOLD; -#else - return lj_ir_knum(J, ldexp(knumleft, fright->i)); -#endif -} - -LJFOLD(FPMATH KNUM any) -LJFOLDF(kfold_fpmath) -{ - lua_Number a = knumleft; - lua_Number y = lj_vm_foldfpm(a, fins->op2); - return lj_ir_knum(J, y); -} - -LJFOLD(CALLN KNUM any) -LJFOLDF(kfold_fpcall1) -{ - const CCallInfo *ci = &lj_ir_callinfo[fins->op2]; - if (CCI_TYPE(ci) == IRT_NUM) { - double y = ((double (*)(double))ci->func)(knumleft); - return lj_ir_knum(J, y); - } - return NEXTFOLD; -} - -LJFOLD(CALLN CARG IRCALL_atan2) -LJFOLDF(kfold_fpcall2) -{ - if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) { - const CCallInfo *ci = &lj_ir_callinfo[fins->op2]; - double a = ir_knum(IR(fleft->op1))->n; - double b = ir_knum(IR(fleft->op2))->n; - double y = ((double (*)(double, double))ci->func)(a, b); - return lj_ir_knum(J, y); - } - return NEXTFOLD; -} - -LJFOLD(POW KNUM KNUM) -LJFOLDF(kfold_numpow) -{ - return lj_ir_knum(J, lj_vm_foldarith(knumleft, knumright, IR_POW - IR_ADD)); -} - -/* Must not use kfold_kref for numbers (could be NaN). */ -LJFOLD(EQ KNUM KNUM) -LJFOLD(NE KNUM KNUM) -LJFOLD(LT KNUM KNUM) -LJFOLD(GE KNUM KNUM) -LJFOLD(LE KNUM KNUM) -LJFOLD(GT KNUM KNUM) -LJFOLD(ULT KNUM KNUM) -LJFOLD(UGE KNUM KNUM) -LJFOLD(ULE KNUM KNUM) -LJFOLD(UGT KNUM KNUM) -LJFOLDF(kfold_numcomp) -{ - return CONDFOLD(lj_ir_numcmp(knumleft, knumright, (IROp)fins->o)); -} - -/* -- Constant folding for 32 bit integers -------------------------------- */ - -static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op) -{ - switch (op) { - case IR_ADD: k1 += k2; break; - case IR_SUB: k1 -= k2; break; - case IR_MUL: k1 *= k2; break; - case IR_MOD: k1 = lj_vm_modi(k1, k2); break; - case IR_NEG: k1 = -k1; break; - case IR_BAND: k1 &= k2; break; - case IR_BOR: k1 |= k2; break; - case IR_BXOR: k1 ^= k2; break; - case IR_BSHL: k1 <<= (k2 & 31); break; - case IR_BSHR: k1 = (int32_t)((uint32_t)k1 >> (k2 & 31)); break; - case IR_BSAR: k1 >>= (k2 & 31); break; - case IR_BROL: k1 = (int32_t)lj_rol((uint32_t)k1, (k2 & 31)); break; - case IR_BROR: k1 = (int32_t)lj_ror((uint32_t)k1, (k2 & 31)); break; - case IR_MIN: k1 = k1 < k2 ? k1 : k2; break; - case IR_MAX: k1 = k1 > k2 ? k1 : k2; break; - default: lj_assertX(0, "bad IR op %d", op); break; - } - return k1; -} - -LJFOLD(ADD KINT KINT) -LJFOLD(SUB KINT KINT) -LJFOLD(MUL KINT KINT) -LJFOLD(MOD KINT KINT) -LJFOLD(NEG KINT KINT) -LJFOLD(BAND KINT KINT) -LJFOLD(BOR KINT KINT) -LJFOLD(BXOR KINT KINT) -LJFOLD(BSHL KINT KINT) -LJFOLD(BSHR KINT KINT) -LJFOLD(BSAR KINT KINT) -LJFOLD(BROL KINT KINT) -LJFOLD(BROR KINT KINT) -LJFOLD(MIN KINT KINT) -LJFOLD(MAX KINT KINT) -LJFOLDF(kfold_intarith) -{ - return INTFOLD(kfold_intop(fleft->i, fright->i, (IROp)fins->o)); -} - -LJFOLD(ADDOV KINT KINT) -LJFOLD(SUBOV KINT KINT) -LJFOLD(MULOV KINT KINT) -LJFOLDF(kfold_intovarith) -{ - lua_Number n = lj_vm_foldarith((lua_Number)fleft->i, (lua_Number)fright->i, - fins->o - IR_ADDOV); - int32_t k = lj_num2int(n); - if (n != (lua_Number)k) - return FAILFOLD; - return INTFOLD(k); -} - -LJFOLD(BNOT KINT) -LJFOLDF(kfold_bnot) -{ - return INTFOLD(~fleft->i); -} - -LJFOLD(BSWAP KINT) -LJFOLDF(kfold_bswap) -{ - return INTFOLD((int32_t)lj_bswap((uint32_t)fleft->i)); -} - -LJFOLD(LT KINT KINT) -LJFOLD(GE KINT KINT) -LJFOLD(LE KINT KINT) -LJFOLD(GT KINT KINT) -LJFOLD(ULT KINT KINT) -LJFOLD(UGE KINT KINT) -LJFOLD(ULE KINT KINT) -LJFOLD(UGT KINT KINT) -LJFOLD(ABC KINT KINT) -LJFOLDF(kfold_intcomp) -{ - int32_t a = fleft->i, b = fright->i; - switch ((IROp)fins->o) { - case IR_LT: return CONDFOLD(a < b); - case IR_GE: return CONDFOLD(a >= b); - case IR_LE: return CONDFOLD(a <= b); - case IR_GT: return CONDFOLD(a > b); - case IR_ULT: return CONDFOLD((uint32_t)a < (uint32_t)b); - case IR_UGE: return CONDFOLD((uint32_t)a >= (uint32_t)b); - case IR_ULE: return CONDFOLD((uint32_t)a <= (uint32_t)b); - case IR_ABC: - case IR_UGT: return CONDFOLD((uint32_t)a > (uint32_t)b); - default: lj_assertJ(0, "bad IR op %d", fins->o); return FAILFOLD; - } -} - -LJFOLD(UGE any KINT) -LJFOLDF(kfold_intcomp0) -{ - if (fright->i == 0) - return DROPFOLD; - return NEXTFOLD; -} - -/* -- Constant folding for 64 bit integers -------------------------------- */ - -static uint64_t kfold_int64arith(jit_State *J, uint64_t k1, uint64_t k2, - IROp op) -{ - UNUSED(J); -#if LJ_HASFFI - switch (op) { - case IR_ADD: k1 += k2; break; - case IR_SUB: k1 -= k2; break; - case IR_MUL: k1 *= k2; break; - case IR_BAND: k1 &= k2; break; - case IR_BOR: k1 |= k2; break; - case IR_BXOR: k1 ^= k2; break; - case IR_BSHL: k1 <<= (k2 & 63); break; - case IR_BSHR: k1 = (int32_t)((uint32_t)k1 >> (k2 & 63)); break; - case IR_BSAR: k1 >>= (k2 & 63); break; - case IR_BROL: k1 = (int32_t)lj_rol((uint32_t)k1, (k2 & 63)); break; - case IR_BROR: k1 = (int32_t)lj_ror((uint32_t)k1, (k2 & 63)); break; - default: lj_assertJ(0, "bad IR op %d", op); break; - } -#else - UNUSED(k2); UNUSED(op); - lj_assertJ(0, "FFI IR op without FFI"); -#endif - return k1; -} - -LJFOLD(ADD KINT64 KINT64) -LJFOLD(SUB KINT64 KINT64) -LJFOLD(MUL KINT64 KINT64) -LJFOLD(BAND KINT64 KINT64) -LJFOLD(BOR KINT64 KINT64) -LJFOLD(BXOR KINT64 KINT64) -LJFOLDF(kfold_int64arith) -{ - return INT64FOLD(kfold_int64arith(J, ir_k64(fleft)->u64, - ir_k64(fright)->u64, (IROp)fins->o)); -} - -LJFOLD(DIV KINT64 KINT64) -LJFOLD(MOD KINT64 KINT64) -LJFOLD(POW KINT64 KINT64) -LJFOLDF(kfold_int64arith2) -{ -#if LJ_HASFFI - uint64_t k1 = ir_k64(fleft)->u64, k2 = ir_k64(fright)->u64; - if (irt_isi64(fins->t)) { - k1 = fins->o == IR_DIV ? lj_carith_divi64((int64_t)k1, (int64_t)k2) : - fins->o == IR_MOD ? lj_carith_modi64((int64_t)k1, (int64_t)k2) : - lj_carith_powi64((int64_t)k1, (int64_t)k2); - } else { - k1 = fins->o == IR_DIV ? lj_carith_divu64(k1, k2) : - fins->o == IR_MOD ? lj_carith_modu64(k1, k2) : - lj_carith_powu64(k1, k2); - } - return INT64FOLD(k1); -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(BSHL KINT64 KINT) -LJFOLD(BSHR KINT64 KINT) -LJFOLD(BSAR KINT64 KINT) -LJFOLD(BROL KINT64 KINT) -LJFOLD(BROR KINT64 KINT) -LJFOLDF(kfold_int64shift) -{ -#if LJ_HASFFI - uint64_t k = ir_k64(fleft)->u64; - int32_t sh = (fright->i & 63); - return INT64FOLD(lj_carith_shift64(k, sh, fins->o - IR_BSHL)); -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(BNOT KINT64) -LJFOLDF(kfold_bnot64) -{ -#if LJ_HASFFI - return INT64FOLD(~ir_k64(fleft)->u64); -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(BSWAP KINT64) -LJFOLDF(kfold_bswap64) -{ -#if LJ_HASFFI - return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64)); -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(LT KINT64 KINT64) -LJFOLD(GE KINT64 KINT64) -LJFOLD(LE KINT64 KINT64) -LJFOLD(GT KINT64 KINT64) -LJFOLD(ULT KINT64 KINT64) -LJFOLD(UGE KINT64 KINT64) -LJFOLD(ULE KINT64 KINT64) -LJFOLD(UGT KINT64 KINT64) -LJFOLDF(kfold_int64comp) -{ -#if LJ_HASFFI - uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64; - switch ((IROp)fins->o) { - case IR_LT: return CONDFOLD((int64_t)a < (int64_t)b); - case IR_GE: return CONDFOLD((int64_t)a >= (int64_t)b); - case IR_LE: return CONDFOLD((int64_t)a <= (int64_t)b); - case IR_GT: return CONDFOLD((int64_t)a > (int64_t)b); - case IR_ULT: return CONDFOLD(a < b); - case IR_UGE: return CONDFOLD(a >= b); - case IR_ULE: return CONDFOLD(a <= b); - case IR_UGT: return CONDFOLD(a > b); - default: lj_assertJ(0, "bad IR op %d", fins->o); return FAILFOLD; - } -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(UGE any KINT64) -LJFOLDF(kfold_int64comp0) -{ -#if LJ_HASFFI - if (ir_k64(fright)->u64 == 0) - return DROPFOLD; - return NEXTFOLD; -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -/* -- Constant folding for strings ---------------------------------------- */ - -LJFOLD(SNEW KKPTR KINT) -LJFOLDF(kfold_snew_kptr) -{ - GCstr *s = lj_str_new(J->L, (const char *)ir_kptr(fleft), (size_t)fright->i); - return lj_ir_kstr(J, s); -} - -LJFOLD(SNEW any KINT) -LJFOLD(XSNEW any KINT) -LJFOLDF(kfold_snew_empty) -{ - if (fright->i == 0) - return lj_ir_kstr(J, &J2G(J)->strempty); - return NEXTFOLD; -} - -LJFOLD(STRREF KGC KINT) -LJFOLDF(kfold_strref) -{ - GCstr *str = ir_kstr(fleft); - lj_assertJ((MSize)fright->i <= str->len, "bad string ref"); - return lj_ir_kkptr(J, (char *)strdata(str) + fright->i); -} - -LJFOLD(STRREF SNEW any) -LJFOLDF(kfold_strref_snew) -{ - PHIBARRIER(fleft); - if (irref_isk(fins->op2) && fright->i == 0) { - return fleft->op1; /* strref(snew(ptr, len), 0) ==> ptr */ - } else { - /* Reassociate: strref(snew(strref(str, a), len), b) ==> strref(str, a+b) */ - IRIns *ir = IR(fleft->op1); - if (ir->o == IR_STRREF) { - IRRef1 str = ir->op1; /* IRIns * is not valid across emitir. */ - PHIBARRIER(ir); - fins->op2 = emitir(IRTI(IR_ADD), ir->op2, fins->op2); /* Clobbers fins! */ - fins->op1 = str; - fins->ot = IRT(IR_STRREF, IRT_PGC); - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(CALLN CARG IRCALL_lj_str_cmp) -LJFOLDF(kfold_strcmp) -{ - if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) { - GCstr *a = ir_kstr(IR(fleft->op1)); - GCstr *b = ir_kstr(IR(fleft->op2)); - return INTFOLD(lj_str_cmp(a, b)); - } - return NEXTFOLD; -} - -/* -- Constant folding and forwarding for buffers ------------------------- */ - -/* -** Buffer ops perform stores, but their effect is limited to the buffer -** itself. Also, buffer ops are chained: a use of an op implies a use of -** all other ops up the chain. Conversely, if an op is unused, all ops -** up the chain can go unsed. This largely eliminates the need to treat -** them as stores. -** -** Alas, treating them as normal (IRM_N) ops doesn't work, because they -** cannot be CSEd in isolation. CSE for IRM_N is implicitly done in LOOP -** or if FOLD is disabled. -** -** The compromise is to declare them as loads, emit them like stores and -** CSE whole chains manually when the BUFSTR is to be emitted. Any chain -** fragments left over from CSE are eliminated by DCE. -** -** The string buffer methods emit a USE instead of a BUFSTR to keep the -** chain alive. -*/ - -LJFOLD(BUFHDR any any) -LJFOLDF(bufhdr_merge) -{ - return fins->op2 == IRBUFHDR_WRITE ? CSEFOLD : EMITFOLD; -} - -LJFOLD(BUFPUT any BUFSTR) -LJFOLDF(bufput_bufstr) -{ - if ((J->flags & JIT_F_OPT_FWD)) { - IRRef hdr = fright->op2; - /* New buffer, no other buffer op inbetween and same buffer? */ - if (fleft->o == IR_BUFHDR && fleft->op2 == IRBUFHDR_RESET && - fleft->prev == hdr && - fleft->op1 == IR(hdr)->op1 && - !(irt_isphi(fright->t) && IR(hdr)->prev) && - (!LJ_HASBUFFER || J->chain[IR_CALLA] < hdr)) { - IRRef ref = fins->op1; - IR(ref)->op2 = IRBUFHDR_APPEND; /* Modify BUFHDR. */ - IR(ref)->op1 = fright->op1; - return ref; - } - /* Replay puts to global temporary buffer. */ - if (IR(hdr)->op2 == IRBUFHDR_RESET && !irt_isphi(fright->t)) { - IRIns *ir = IR(fright->op1); - /* For now only handle single string.reverse .lower .upper .rep. */ - if (ir->o == IR_CALLL && - ir->op2 >= IRCALL_lj_buf_putstr_reverse && - ir->op2 <= IRCALL_lj_buf_putstr_rep) { - IRIns *carg1 = IR(ir->op1); - if (ir->op2 == IRCALL_lj_buf_putstr_rep) { - IRIns *carg2 = IR(carg1->op1); - if (carg2->op1 == hdr) { - return lj_ir_call(J, ir->op2, fins->op1, carg2->op2, carg1->op2); - } - } else if (carg1->op1 == hdr) { - return lj_ir_call(J, ir->op2, fins->op1, carg1->op2); - } - } - } - } - return EMITFOLD; /* Always emit, CSE later. */ -} - -LJFOLD(BUFPUT any any) -LJFOLDF(bufput_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && fright->o == IR_KGC) { - GCstr *s2 = ir_kstr(fright); - if (s2->len == 0) { /* Empty string? */ - return LEFTFOLD; - } else { - if (fleft->o == IR_BUFPUT && irref_isk(fleft->op2) && - !irt_isphi(fleft->t)) { /* Join two constant string puts in a row. */ - GCstr *s1 = ir_kstr(IR(fleft->op2)); - IRRef kref = lj_ir_kstr(J, lj_buf_cat2str(J->L, s1, s2)); - /* lj_ir_kstr() may realloc the IR and invalidates any IRIns *. */ - IR(fins->op1)->op2 = kref; /* Modify previous BUFPUT. */ - return fins->op1; - } - } - } - return EMITFOLD; /* Always emit, CSE later. */ -} - -LJFOLD(BUFSTR any any) -LJFOLDF(bufstr_kfold_cse) -{ - lj_assertJ(fleft->o == IR_BUFHDR || fleft->o == IR_BUFPUT || - fleft->o == IR_CALLL, - "bad buffer constructor IR op %d", fleft->o); - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { - if (fleft->o == IR_BUFHDR) { /* No put operations? */ - if (fleft->op2 == IRBUFHDR_RESET) /* Empty buffer? */ - return lj_ir_kstr(J, &J2G(J)->strempty); - fins->op1 = fleft->op1; - fins->op2 = fleft->prev; /* Relies on checks in bufput_append. */ - return CSEFOLD; - } else if (fleft->o == IR_BUFPUT) { - IRIns *irb = IR(fleft->op1); - if (irb->o == IR_BUFHDR && irb->op2 == IRBUFHDR_RESET) - return fleft->op2; /* Shortcut for a single put operation. */ - } - } - /* Try to CSE the whole chain. */ - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - IRRef ref = J->chain[IR_BUFSTR]; - while (ref) { - IRIns *irs = IR(ref), *ira = fleft, *irb = IR(irs->op1); - while (ira->o == irb->o && ira->op2 == irb->op2) { - lj_assertJ(ira->o == IR_BUFHDR || ira->o == IR_BUFPUT || - ira->o == IR_CALLL || ira->o == IR_CARG, - "bad buffer constructor IR op %d", ira->o); - if (ira->o == IR_BUFHDR && ira->op2 == IRBUFHDR_RESET) - return ref; /* CSE succeeded. */ - if (ira->o == IR_CALLL && ira->op2 == IRCALL_lj_buf_puttab) - break; - ira = IR(ira->op1); - irb = IR(irb->op1); - } - ref = irs->prev; - } - } - return EMITFOLD; /* No CSE possible. */ -} - -LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_reverse) -LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_upper) -LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_lower) -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putquoted) -LJFOLDF(bufput_kfold_op) -{ - if (irref_isk(fleft->op2)) { - const CCallInfo *ci = &lj_ir_callinfo[fins->op2]; - SBuf *sb = lj_buf_tmp_(J->L); - sb = ((SBuf * (LJ_FASTCALL *)(SBuf *, GCstr *))ci->func)(sb, - ir_kstr(IR(fleft->op2))); - fins->o = IR_BUFPUT; - fins->op1 = fleft->op1; - fins->op2 = lj_ir_kstr(J, lj_buf_tostr(sb)); - return RETRYFOLD; - } - return EMITFOLD; /* Always emit, CSE later. */ -} - -LJFOLD(CALLL CARG IRCALL_lj_buf_putstr_rep) -LJFOLDF(bufput_kfold_rep) -{ - if (irref_isk(fleft->op2)) { - IRIns *irc = IR(fleft->op1); - if (irref_isk(irc->op2)) { - SBuf *sb = lj_buf_tmp_(J->L); - sb = lj_buf_putstr_rep(sb, ir_kstr(IR(irc->op2)), IR(fleft->op2)->i); - fins->o = IR_BUFPUT; - fins->op1 = irc->op1; - fins->op2 = lj_ir_kstr(J, lj_buf_tostr(sb)); - return RETRYFOLD; - } - } - return EMITFOLD; /* Always emit, CSE later. */ -} - -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfxint) -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfnum_int) -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfnum_uint) -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfnum) -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfstr) -LJFOLD(CALLL CARG IRCALL_lj_strfmt_putfchar) -LJFOLDF(bufput_kfold_fmt) -{ - IRIns *irc = IR(fleft->op1); - lj_assertJ(irref_isk(irc->op2), "SFormat must be const"); - if (irref_isk(fleft->op2)) { - SFormat sf = (SFormat)IR(irc->op2)->i; - IRIns *ira = IR(fleft->op2); - SBuf *sb = lj_buf_tmp_(J->L); - switch (fins->op2) { - case IRCALL_lj_strfmt_putfxint: - sb = lj_strfmt_putfxint(sb, sf, ir_k64(ira)->u64); - break; - case IRCALL_lj_strfmt_putfstr: - sb = lj_strfmt_putfstr(sb, sf, ir_kstr(ira)); - break; - case IRCALL_lj_strfmt_putfchar: - sb = lj_strfmt_putfchar(sb, sf, ira->i); - break; - case IRCALL_lj_strfmt_putfnum_int: - case IRCALL_lj_strfmt_putfnum_uint: - case IRCALL_lj_strfmt_putfnum: - default: { - const CCallInfo *ci = &lj_ir_callinfo[fins->op2]; - sb = ((SBuf * (*)(SBuf *, SFormat, lua_Number))ci->func)(sb, sf, - ir_knum(ira)->n); - break; - } - } - fins->o = IR_BUFPUT; - fins->op1 = irc->op1; - fins->op2 = lj_ir_kstr(J, lj_buf_tostr(sb)); - return RETRYFOLD; - } - return EMITFOLD; /* Always emit, CSE later. */ -} - -/* -- Constant folding of pointer arithmetic ------------------------------ */ - -LJFOLD(ADD KGC KINT) -LJFOLD(ADD KGC KINT64) -LJFOLDF(kfold_add_kgc) -{ - GCobj *o = ir_kgc(fleft); -#if LJ_64 - ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64; -#else - ptrdiff_t ofs = fright->i; -#endif -#if LJ_HASFFI - if (irt_iscdata(fleft->t)) { - CType *ct = ctype_raw(ctype_ctsG(J2G(J)), gco2cd(o)->ctypeid); - if (ctype_isnum(ct->info) || ctype_isenum(ct->info) || - ctype_isptr(ct->info) || ctype_isfunc(ct->info) || - ctype_iscomplex(ct->info) || ctype_isvector(ct->info)) - return lj_ir_kkptr(J, (char *)o + ofs); - } -#endif - return lj_ir_kptr(J, (char *)o + ofs); -} - -LJFOLD(ADD KPTR KINT) -LJFOLD(ADD KPTR KINT64) -LJFOLD(ADD KKPTR KINT) -LJFOLD(ADD KKPTR KINT64) -LJFOLDF(kfold_add_kptr) -{ - void *p = ir_kptr(fleft); -#if LJ_64 - ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64; -#else - ptrdiff_t ofs = fright->i; -#endif - return lj_ir_kptr_(J, fleft->o, (char *)p + ofs); -} - -LJFOLD(ADD any KGC) -LJFOLD(ADD any KPTR) -LJFOLD(ADD any KKPTR) -LJFOLDF(kfold_add_kright) -{ - if (fleft->o == IR_KINT || fleft->o == IR_KINT64) { - IRRef1 tmp = fins->op1; fins->op1 = fins->op2; fins->op2 = tmp; - return RETRYFOLD; - } - return NEXTFOLD; -} - -/* -- Constant folding of conversions ------------------------------------- */ - -LJFOLD(TOBIT KNUM KNUM) -LJFOLDF(kfold_tobit) -{ - return INTFOLD(lj_num2bit(knumleft)); -} - -LJFOLD(CONV KINT IRCONV_NUM_INT) -LJFOLDF(kfold_conv_kint_num) -{ - return lj_ir_knum(J, (lua_Number)fleft->i); -} - -LJFOLD(CONV KINT IRCONV_NUM_U32) -LJFOLDF(kfold_conv_kintu32_num) -{ - return lj_ir_knum(J, (lua_Number)(uint32_t)fleft->i); -} - -LJFOLD(CONV KINT IRCONV_INT_I8) -LJFOLD(CONV KINT IRCONV_INT_U8) -LJFOLD(CONV KINT IRCONV_INT_I16) -LJFOLD(CONV KINT IRCONV_INT_U16) -LJFOLDF(kfold_conv_kint_ext) -{ - int32_t k = fleft->i; - if ((fins->op2 & IRCONV_SRCMASK) == IRT_I8) k = (int8_t)k; - else if ((fins->op2 & IRCONV_SRCMASK) == IRT_U8) k = (uint8_t)k; - else if ((fins->op2 & IRCONV_SRCMASK) == IRT_I16) k = (int16_t)k; - else k = (uint16_t)k; - return INTFOLD(k); -} - -LJFOLD(CONV KINT IRCONV_I64_INT) -LJFOLD(CONV KINT IRCONV_U64_INT) -LJFOLD(CONV KINT IRCONV_I64_U32) -LJFOLD(CONV KINT IRCONV_U64_U32) -LJFOLDF(kfold_conv_kint_i64) -{ - if ((fins->op2 & IRCONV_SEXT)) - return INT64FOLD((uint64_t)(int64_t)fleft->i); - else - return INT64FOLD((uint64_t)(int64_t)(uint32_t)fleft->i); -} - -LJFOLD(CONV KINT64 IRCONV_NUM_I64) -LJFOLDF(kfold_conv_kint64_num_i64) -{ - return lj_ir_knum(J, (lua_Number)(int64_t)ir_kint64(fleft)->u64); -} - -LJFOLD(CONV KINT64 IRCONV_NUM_U64) -LJFOLDF(kfold_conv_kint64_num_u64) -{ - return lj_ir_knum(J, (lua_Number)ir_kint64(fleft)->u64); -} - -LJFOLD(CONV KINT64 IRCONV_INT_I64) -LJFOLD(CONV KINT64 IRCONV_U32_I64) -LJFOLDF(kfold_conv_kint64_int_i64) -{ - return INTFOLD((int32_t)ir_kint64(fleft)->u64); -} - -LJFOLD(CONV KNUM IRCONV_INT_NUM) -LJFOLDF(kfold_conv_knum_int_num) -{ - lua_Number n = knumleft; - int32_t k = lj_num2int(n); - if (irt_isguard(fins->t) && n != (lua_Number)k) { - /* We're about to create a guard which always fails, like CONV +1.5. - ** Some pathological loops cause this during LICM, e.g.: - ** local x,k,t = 0,1.5,{1,[1.5]=2} - ** for i=1,200 do x = x+ t[k]; k = k == 1 and 1.5 or 1 end - ** assert(x == 300) - */ - return FAILFOLD; - } - return INTFOLD(k); -} - -LJFOLD(CONV KNUM IRCONV_U32_NUM) -LJFOLDF(kfold_conv_knum_u32_num) -{ -#ifdef _MSC_VER - { /* Workaround for MSVC bug. */ - volatile uint32_t u = (uint32_t)knumleft; - return INTFOLD((int32_t)u); - } -#else - return INTFOLD((int32_t)(uint32_t)knumleft); -#endif -} - -LJFOLD(CONV KNUM IRCONV_I64_NUM) -LJFOLDF(kfold_conv_knum_i64_num) -{ - return INT64FOLD((uint64_t)(int64_t)knumleft); -} - -LJFOLD(CONV KNUM IRCONV_U64_NUM) -LJFOLDF(kfold_conv_knum_u64_num) -{ - return INT64FOLD(lj_num2u64(knumleft)); -} - -LJFOLD(TOSTR KNUM any) -LJFOLDF(kfold_tostr_knum) -{ - return lj_ir_kstr(J, lj_strfmt_num(J->L, ir_knum(fleft))); -} - -LJFOLD(TOSTR KINT any) -LJFOLDF(kfold_tostr_kint) -{ - return lj_ir_kstr(J, fins->op2 == IRTOSTR_INT ? - lj_strfmt_int(J->L, fleft->i) : - lj_strfmt_char(J->L, fleft->i)); -} - -LJFOLD(STRTO KGC) -LJFOLDF(kfold_strto) -{ - TValue n; - if (lj_strscan_num(ir_kstr(fleft), &n)) - return lj_ir_knum(J, numV(&n)); - return FAILFOLD; -} - -/* -- Constant folding of equality checks --------------------------------- */ - -/* Don't constant-fold away FLOAD checks against KNULL. */ -LJFOLD(EQ FLOAD KNULL) -LJFOLD(NE FLOAD KNULL) -LJFOLDX(lj_opt_cse) - -/* But fold all other KNULL compares, since only KNULL is equal to KNULL. */ -LJFOLD(EQ any KNULL) -LJFOLD(NE any KNULL) -LJFOLD(EQ KNULL any) -LJFOLD(NE KNULL any) -LJFOLD(EQ KINT KINT) /* Constants are unique, so same refs <==> same value. */ -LJFOLD(NE KINT KINT) -LJFOLD(EQ KINT64 KINT64) -LJFOLD(NE KINT64 KINT64) -LJFOLD(EQ KGC KGC) -LJFOLD(NE KGC KGC) -LJFOLDF(kfold_kref) -{ - return CONDFOLD((fins->op1 == fins->op2) ^ (fins->o == IR_NE)); -} - -/* -- Algebraic shortcuts ------------------------------------------------- */ - -LJFOLD(FPMATH FPMATH IRFPM_FLOOR) -LJFOLD(FPMATH FPMATH IRFPM_CEIL) -LJFOLD(FPMATH FPMATH IRFPM_TRUNC) -LJFOLDF(shortcut_round) -{ - IRFPMathOp op = (IRFPMathOp)fleft->op2; - if (op == IRFPM_FLOOR || op == IRFPM_CEIL || op == IRFPM_TRUNC) - return LEFTFOLD; /* round(round_left(x)) = round_left(x) */ - return NEXTFOLD; -} - -LJFOLD(ABS ABS FLOAD) -LJFOLDF(shortcut_left) -{ - return LEFTFOLD; /* f(g(x)) ==> g(x) */ -} - -LJFOLD(ABS NEG FLOAD) -LJFOLDF(shortcut_dropleft) -{ - PHIBARRIER(fleft); - fins->op1 = fleft->op1; /* abs(neg(x)) ==> abs(x) */ - return RETRYFOLD; -} - -/* Note: no safe shortcuts with STRTO and TOSTR ("1e2" ==> +100 ==> "100"). */ -LJFOLD(NEG NEG any) -LJFOLD(BNOT BNOT) -LJFOLD(BSWAP BSWAP) -LJFOLDF(shortcut_leftleft) -{ - PHIBARRIER(fleft); /* See above. Fold would be ok, but not beneficial. */ - return fleft->op1; /* f(g(x)) ==> x */ -} - -/* -- FP algebraic simplifications ---------------------------------------- */ - -/* FP arithmetic is tricky -- there's not much to simplify. -** Please note the following common pitfalls before sending "improvements": -** x+0 ==> x is INVALID for x=-0 -** 0-x ==> -x is INVALID for x=+0 -** x*0 ==> 0 is INVALID for x=-0, x=+-Inf or x=NaN -*/ - -LJFOLD(ADD NEG any) -LJFOLDF(simplify_numadd_negx) -{ - PHIBARRIER(fleft); - fins->o = IR_SUB; /* (-a) + b ==> b - a */ - fins->op1 = fins->op2; - fins->op2 = fleft->op1; - return RETRYFOLD; -} - -LJFOLD(ADD any NEG) -LJFOLDF(simplify_numadd_xneg) -{ - PHIBARRIER(fright); - fins->o = IR_SUB; /* a + (-b) ==> a - b */ - fins->op2 = fright->op1; - return RETRYFOLD; -} - -LJFOLD(SUB any KNUM) -LJFOLDF(simplify_numsub_k) -{ - if (ir_knum(fright)->u64 == 0) /* x - (+0) ==> x */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(SUB NEG KNUM) -LJFOLDF(simplify_numsub_negk) -{ - PHIBARRIER(fleft); - fins->op2 = fleft->op1; /* (-x) - k ==> (-k) - x */ - fins->op1 = (IRRef1)lj_ir_knum(J, -knumright); - return RETRYFOLD; -} - -LJFOLD(SUB any NEG) -LJFOLDF(simplify_numsub_xneg) -{ - PHIBARRIER(fright); - fins->o = IR_ADD; /* a - (-b) ==> a + b */ - fins->op2 = fright->op1; - return RETRYFOLD; -} - -LJFOLD(MUL any KNUM) -LJFOLD(DIV any KNUM) -LJFOLDF(simplify_nummuldiv_k) -{ - lua_Number n = knumright; - if (n == 1.0) { /* x o 1 ==> x */ - return LEFTFOLD; - } else if (n == -1.0) { /* x o -1 ==> -x */ - IRRef op1 = fins->op1; - fins->op2 = (IRRef1)lj_ir_ksimd(J, LJ_KSIMD_NEG); /* Modifies fins. */ - fins->op1 = op1; - fins->o = IR_NEG; - return RETRYFOLD; - } else if (fins->o == IR_MUL && n == 2.0) { /* x * 2 ==> x + x */ - fins->o = IR_ADD; - fins->op2 = fins->op1; - return RETRYFOLD; - } else if (fins->o == IR_DIV) { /* x / 2^k ==> x * 2^-k */ - uint64_t u = ir_knum(fright)->u64; - uint32_t ex = ((uint32_t)(u >> 52) & 0x7ff); - if ((u & U64x(000fffff,ffffffff)) == 0 && ex - 1 < 0x7fd) { - u = (u & ((uint64_t)1 << 63)) | ((uint64_t)(0x7fe - ex) << 52); - fins->o = IR_MUL; /* Multiply by exact reciprocal. */ - fins->op2 = lj_ir_knum_u64(J, u); - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(MUL NEG KNUM) -LJFOLD(DIV NEG KNUM) -LJFOLDF(simplify_nummuldiv_negk) -{ - PHIBARRIER(fleft); - fins->op1 = fleft->op1; /* (-a) o k ==> a o (-k) */ - fins->op2 = (IRRef1)lj_ir_knum(J, -knumright); - return RETRYFOLD; -} - -LJFOLD(MUL NEG NEG) -LJFOLD(DIV NEG NEG) -LJFOLDF(simplify_nummuldiv_negneg) -{ - PHIBARRIER(fleft); - PHIBARRIER(fright); - fins->op1 = fleft->op1; /* (-a) o (-b) ==> a o b */ - fins->op2 = fright->op1; - return RETRYFOLD; -} - -LJFOLD(POW any KNUM) -LJFOLDF(simplify_numpow_k) -{ - if (knumright == 0.0) /* x ^ 0 ==> 1 */ - return lj_ir_knum_one(J); /* Result must be a number, not an int. */ - else if (knumright == 1.0) /* x ^ 1 ==> x */ - return LEFTFOLD; - else if (knumright == 2.0) /* x ^ 2 ==> x * x */ - return emitir(IRTN(IR_MUL), fins->op1, fins->op1); - else - return NEXTFOLD; -} - -/* -- Simplify conversions ------------------------------------------------ */ - -LJFOLD(CONV CONV IRCONV_NUM_INT) /* _NUM */ -LJFOLDF(shortcut_conv_num_int) -{ - PHIBARRIER(fleft); - /* Only safe with a guarded conversion to int. */ - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_NUM && irt_isguard(fleft->t)) - return fleft->op1; /* f(g(x)) ==> x */ - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_INT_NUM) /* _INT */ -LJFOLD(CONV CONV IRCONV_U32_NUM) /* _U32*/ -LJFOLDF(simplify_conv_int_num) -{ - /* Fold even across PHI to avoid expensive num->int conversions in loop. */ - if ((fleft->op2 & IRCONV_SRCMASK) == - ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) - return fleft->op1; - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_I64_NUM) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_U64_NUM) /* _INT or _U32 */ -LJFOLDF(simplify_conv_i64_num) -{ - PHIBARRIER(fleft); - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) { - /* Reduce to a sign-extension. */ - fins->op1 = fleft->op1; - fins->op2 = ((IRT_I64<<5)|IRT_INT|IRCONV_SEXT); - return RETRYFOLD; - } else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) { -#if LJ_TARGET_X64 - return fleft->op1; -#else - /* Reduce to a zero-extension. */ - fins->op1 = fleft->op1; - fins->op2 = (IRT_I64<<5)|IRT_U32; - return RETRYFOLD; -#endif - } - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_INT_I64) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_INT_U64) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_U32_I64) /* _INT or _U32 */ -LJFOLD(CONV CONV IRCONV_U32_U64) /* _INT or _U32 */ -LJFOLDF(simplify_conv_int_i64) -{ - int src; - PHIBARRIER(fleft); - src = (fleft->op2 & IRCONV_SRCMASK); - if (src == IRT_INT || src == IRT_U32) { - if (src == ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) { - return fleft->op1; - } else { - fins->op2 = ((fins->op2 & IRCONV_DSTMASK) | src); - fins->op1 = fleft->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(CONV CONV IRCONV_FLOAT_NUM) /* _FLOAT */ -LJFOLDF(simplify_conv_flt_num) -{ - PHIBARRIER(fleft); - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_FLOAT) - return fleft->op1; - return NEXTFOLD; -} - -/* Shortcut TOBIT + IRT_NUM <- IRT_INT/IRT_U32 conversion. */ -LJFOLD(TOBIT CONV KNUM) -LJFOLDF(simplify_tobit_conv) -{ - /* Fold even across PHI to avoid expensive num->int conversions in loop. */ - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) { - lj_assertJ(irt_isnum(fleft->t), "expected TOBIT number arg"); - return fleft->op1; - } else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) { - lj_assertJ(irt_isnum(fleft->t), "expected TOBIT number arg"); - fins->o = IR_CONV; - fins->op1 = fleft->op1; - fins->op2 = (IRT_INT<<5)|IRT_U32; - return RETRYFOLD; - } - return NEXTFOLD; -} - -/* Shortcut floor/ceil/round + IRT_NUM <- IRT_INT/IRT_U32 conversion. */ -LJFOLD(FPMATH CONV IRFPM_FLOOR) -LJFOLD(FPMATH CONV IRFPM_CEIL) -LJFOLD(FPMATH CONV IRFPM_TRUNC) -LJFOLDF(simplify_floor_conv) -{ - if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT || - (fleft->op2 & IRCONV_SRCMASK) == IRT_U32) - return LEFTFOLD; - return NEXTFOLD; -} - -/* Strength reduction of widening. */ -LJFOLD(CONV any IRCONV_I64_INT) -LJFOLD(CONV any IRCONV_U64_INT) -LJFOLDF(simplify_conv_sext) -{ - IRRef ref = fins->op1; - int64_t ofs = 0; - if (!(fins->op2 & IRCONV_SEXT)) - return NEXTFOLD; - PHIBARRIER(fleft); - if (fleft->o == IR_XLOAD && (irt_isu8(fleft->t) || irt_isu16(fleft->t))) - goto ok_reduce; - if (fleft->o == IR_ADD && irref_isk(fleft->op2)) { - ofs = (int64_t)IR(fleft->op2)->i; - ref = fleft->op1; - } - /* Use scalar evolution analysis results to strength-reduce sign-extension. */ - if (ref == J->scev.idx) { - IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop; - lj_assertJ(irt_isint(J->scev.t), "only int SCEV supported"); - if (lo && IR(lo)->o == IR_KINT && IR(lo)->i + ofs >= 0) { - ok_reduce: -#if LJ_TARGET_X64 - /* Eliminate widening. All 32 bit ops do an implicit zero-extension. */ - return LEFTFOLD; -#else - /* Reduce to a (cheaper) zero-extension. */ - fins->op2 &= ~IRCONV_SEXT; - return RETRYFOLD; -#endif - } - } - return NEXTFOLD; -} - -/* Strength reduction of narrowing. */ -LJFOLD(CONV ADD IRCONV_INT_I64) -LJFOLD(CONV SUB IRCONV_INT_I64) -LJFOLD(CONV MUL IRCONV_INT_I64) -LJFOLD(CONV ADD IRCONV_INT_U64) -LJFOLD(CONV SUB IRCONV_INT_U64) -LJFOLD(CONV MUL IRCONV_INT_U64) -LJFOLD(CONV ADD IRCONV_U32_I64) -LJFOLD(CONV SUB IRCONV_U32_I64) -LJFOLD(CONV MUL IRCONV_U32_I64) -LJFOLD(CONV ADD IRCONV_U32_U64) -LJFOLD(CONV SUB IRCONV_U32_U64) -LJFOLD(CONV MUL IRCONV_U32_U64) -LJFOLDF(simplify_conv_narrow) -{ -#if LJ_64 - UNUSED(J); - return NEXTFOLD; -#else - IROp op = (IROp)fleft->o; - IRType t = irt_type(fins->t); - IRRef op1 = fleft->op1, op2 = fleft->op2, mode = fins->op2; - PHIBARRIER(fleft); - op1 = emitir(IRT(IR_CONV, t), op1, mode); - op2 = emitir(IRT(IR_CONV, t), op2, mode); - fins->ot = IRT(op, t); - fins->op1 = op1; - fins->op2 = op2; - return RETRYFOLD; -#endif -} - -/* Special CSE rule for CONV. */ -LJFOLD(CONV any any) -LJFOLDF(cse_conv) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - IRRef op1 = fins->op1, op2 = (fins->op2 & IRCONV_MODEMASK); - uint8_t guard = irt_isguard(fins->t); - IRRef ref = J->chain[IR_CONV]; - while (ref > op1) { - IRIns *ir = IR(ref); - /* Commoning with stronger checks is ok. */ - if (ir->op1 == op1 && (ir->op2 & IRCONV_MODEMASK) == op2 && - irt_isguard(ir->t) >= guard) - return ref; - ref = ir->prev; - } - } - return EMITFOLD; /* No fallthrough to regular CSE. */ -} - -/* FP conversion narrowing. */ -LJFOLD(TOBIT ADD KNUM) -LJFOLD(TOBIT SUB KNUM) -LJFOLD(CONV ADD IRCONV_INT_NUM) -LJFOLD(CONV SUB IRCONV_INT_NUM) -LJFOLD(CONV ADD IRCONV_I64_NUM) -LJFOLD(CONV SUB IRCONV_I64_NUM) -LJFOLDF(narrow_convert) -{ - PHIBARRIER(fleft); - /* Narrowing ignores PHIs and repeating it inside the loop is not useful. */ - if (J->chain[IR_LOOP]) - return NEXTFOLD; - lj_assertJ(fins->o != IR_CONV || (fins->op2&IRCONV_CONVMASK) != IRCONV_TOBIT, - "unexpected CONV TOBIT"); - return lj_opt_narrow_convert(J); -} - -/* -- Integer algebraic simplifications ----------------------------------- */ - -LJFOLD(ADD any KINT) -LJFOLD(ADDOV any KINT) -LJFOLD(SUBOV any KINT) -LJFOLDF(simplify_intadd_k) -{ - if (fright->i == 0) /* i o 0 ==> i */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(MULOV any KINT) -LJFOLDF(simplify_intmul_k) -{ - if (fright->i == 0) /* i * 0 ==> 0 */ - return RIGHTFOLD; - if (fright->i == 1) /* i * 1 ==> i */ - return LEFTFOLD; - if (fright->i == 2) { /* i * 2 ==> i + i */ - fins->o = IR_ADDOV; - fins->op2 = fins->op1; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(SUB any KINT) -LJFOLDF(simplify_intsub_k) -{ - if (fright->i == 0) /* i - 0 ==> i */ - return LEFTFOLD; - fins->o = IR_ADD; /* i - k ==> i + (-k) */ - fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i); /* Overflow for -2^31 ok. */ - return RETRYFOLD; -} - -LJFOLD(SUB KINT any) -LJFOLD(SUB KINT64 any) -LJFOLDF(simplify_intsub_kleft) -{ - if (fleft->o == IR_KINT ? (fleft->i == 0) : (ir_kint64(fleft)->u64 == 0)) { - fins->o = IR_NEG; /* 0 - i ==> -i */ - fins->op1 = fins->op2; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(ADD any KINT64) -LJFOLDF(simplify_intadd_k64) -{ - if (ir_kint64(fright)->u64 == 0) /* i + 0 ==> i */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(SUB any KINT64) -LJFOLDF(simplify_intsub_k64) -{ - uint64_t k = ir_kint64(fright)->u64; - if (k == 0) /* i - 0 ==> i */ - return LEFTFOLD; - fins->o = IR_ADD; /* i - k ==> i + (-k) */ - fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k); - return RETRYFOLD; -} - -static TRef simplify_intmul_k(jit_State *J, int32_t k) -{ - /* Note: many more simplifications are possible, e.g. 2^k1 +- 2^k2. - ** But this is mainly intended for simple address arithmetic. - ** Also it's easier for the backend to optimize the original multiplies. - */ - if (k == 0) { /* i * 0 ==> 0 */ - return RIGHTFOLD; - } else if (k == 1) { /* i * 1 ==> i */ - return LEFTFOLD; - } else if ((k & (k-1)) == 0) { /* i * 2^k ==> i << k */ - fins->o = IR_BSHL; - fins->op2 = lj_ir_kint(J, lj_fls((uint32_t)k)); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(MUL any KINT) -LJFOLDF(simplify_intmul_k32) -{ - if (fright->i >= 0) - return simplify_intmul_k(J, fright->i); - return NEXTFOLD; -} - -LJFOLD(MUL any KINT64) -LJFOLDF(simplify_intmul_k64) -{ -#if LJ_HASFFI - if (ir_kint64(fright)->u64 < 0x80000000u) - return simplify_intmul_k(J, (int32_t)ir_kint64(fright)->u64); - return NEXTFOLD; -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(MOD any KINT) -LJFOLDF(simplify_intmod_k) -{ - int32_t k = fright->i; - lj_assertJ(k != 0, "integer mod 0"); - if (k > 0 && (k & (k-1)) == 0) { /* i % (2^k) ==> i & (2^k-1) */ - fins->o = IR_BAND; - fins->op2 = lj_ir_kint(J, k-1); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(MOD KINT any) -LJFOLDF(simplify_intmod_kleft) -{ - if (fleft->i == 0) - return INTFOLD(0); - return NEXTFOLD; -} - -LJFOLD(SUB any any) -LJFOLD(SUBOV any any) -LJFOLDF(simplify_intsub) -{ - if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) /* i - i ==> 0 */ - return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0); - return NEXTFOLD; -} - -LJFOLD(SUB ADD any) -LJFOLDF(simplify_intsubadd_leftcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fleft); - if (fins->op2 == fleft->op1) /* (i + j) - i ==> j */ - return fleft->op2; - if (fins->op2 == fleft->op2) /* (i + j) - j ==> i */ - return fleft->op1; - } - return NEXTFOLD; -} - -LJFOLD(SUB SUB any) -LJFOLDF(simplify_intsubsub_leftcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fleft); - if (fins->op2 == fleft->op1) { /* (i - j) - i ==> 0 - j */ - fins->op1 = (IRRef1)lj_ir_kint(J, 0); - fins->op2 = fleft->op2; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(SUB any SUB) -LJFOLDF(simplify_intsubsub_rightcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fright); - if (fins->op1 == fright->op1) /* i - (i - j) ==> j */ - return fright->op2; - } - return NEXTFOLD; -} - -LJFOLD(SUB any ADD) -LJFOLDF(simplify_intsubadd_rightcancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fright); - if (fins->op1 == fright->op1) { /* i - (i + j) ==> 0 - j */ - fins->op2 = fright->op2; - fins->op1 = (IRRef1)lj_ir_kint(J, 0); - return RETRYFOLD; - } - if (fins->op1 == fright->op2) { /* i - (j + i) ==> 0 - j */ - fins->op2 = fright->op1; - fins->op1 = (IRRef1)lj_ir_kint(J, 0); - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(SUB ADD ADD) -LJFOLDF(simplify_intsubaddadd_cancel) -{ - if (!irt_isnum(fins->t)) { - PHIBARRIER(fleft); - PHIBARRIER(fright); - if (fleft->op1 == fright->op1) { /* (i + j1) - (i + j2) ==> j1 - j2 */ - fins->op1 = fleft->op2; - fins->op2 = fright->op2; - return RETRYFOLD; - } - if (fleft->op1 == fright->op2) { /* (i + j1) - (j2 + i) ==> j1 - j2 */ - fins->op1 = fleft->op2; - fins->op2 = fright->op1; - return RETRYFOLD; - } - if (fleft->op2 == fright->op1) { /* (j1 + i) - (i + j2) ==> j1 - j2 */ - fins->op1 = fleft->op1; - fins->op2 = fright->op2; - return RETRYFOLD; - } - if (fleft->op2 == fright->op2) { /* (j1 + i) - (j2 + i) ==> j1 - j2 */ - fins->op1 = fleft->op1; - fins->op2 = fright->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(BAND any KINT) -LJFOLD(BAND any KINT64) -LJFOLDF(simplify_band_k) -{ - int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : - (int64_t)ir_k64(fright)->u64; - if (k == 0) /* i & 0 ==> 0 */ - return RIGHTFOLD; - if (k == -1) /* i & -1 ==> i */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BOR any KINT) -LJFOLD(BOR any KINT64) -LJFOLDF(simplify_bor_k) -{ - int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : - (int64_t)ir_k64(fright)->u64; - if (k == 0) /* i | 0 ==> i */ - return LEFTFOLD; - if (k == -1) /* i | -1 ==> -1 */ - return RIGHTFOLD; - return NEXTFOLD; -} - -LJFOLD(BXOR any KINT) -LJFOLD(BXOR any KINT64) -LJFOLDF(simplify_bxor_k) -{ - int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : - (int64_t)ir_k64(fright)->u64; - if (k == 0) /* i xor 0 ==> i */ - return LEFTFOLD; - if (k == -1) { /* i xor -1 ==> ~i */ - fins->o = IR_BNOT; - fins->op2 = 0; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(BSHL any KINT) -LJFOLD(BSHR any KINT) -LJFOLD(BSAR any KINT) -LJFOLD(BROL any KINT) -LJFOLD(BROR any KINT) -LJFOLDF(simplify_shift_ik) -{ - int32_t mask = irt_is64(fins->t) ? 63 : 31; - int32_t k = (fright->i & mask); - if (k == 0) /* i o 0 ==> i */ - return LEFTFOLD; - if (k == 1 && fins->o == IR_BSHL) { /* i << 1 ==> i + i */ - fins->o = IR_ADD; - fins->op2 = fins->op1; - return RETRYFOLD; - } - if (k != fright->i) { /* i o k ==> i o (k & mask) */ - fins->op2 = (IRRef1)lj_ir_kint(J, k); - return RETRYFOLD; - } -#ifndef LJ_TARGET_UNIFYROT - if (fins->o == IR_BROR) { /* bror(i, k) ==> brol(i, (-k)&mask) */ - fins->o = IR_BROL; - fins->op2 = (IRRef1)lj_ir_kint(J, (-k)&mask); - return RETRYFOLD; - } -#endif - return NEXTFOLD; -} - -LJFOLD(BSHL any BAND) -LJFOLD(BSHR any BAND) -LJFOLD(BSAR any BAND) -LJFOLD(BROL any BAND) -LJFOLD(BROR any BAND) -LJFOLDF(simplify_shift_andk) -{ - IRIns *irk = IR(fright->op2); - PHIBARRIER(fright); - if ((fins->o < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && - irk->o == IR_KINT) { /* i o (j & mask) ==> i o j */ - int32_t mask = irt_is64(fins->t) ? 63 : 31; - int32_t k = irk->i & mask; - if (k == mask) { - fins->op2 = fright->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(BSHL KINT any) -LJFOLD(BSHR KINT any) -LJFOLD(BSHL KINT64 any) -LJFOLD(BSHR KINT64 any) -LJFOLDF(simplify_shift1_ki) -{ - int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i : - (int64_t)ir_k64(fleft)->u64; - if (k == 0) /* 0 o i ==> 0 */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BSAR KINT any) -LJFOLD(BROL KINT any) -LJFOLD(BROR KINT any) -LJFOLD(BSAR KINT64 any) -LJFOLD(BROL KINT64 any) -LJFOLD(BROR KINT64 any) -LJFOLDF(simplify_shift2_ki) -{ - int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i : - (int64_t)ir_k64(fleft)->u64; - if (k == 0 || k == -1) /* 0 o i ==> 0; -1 o i ==> -1 */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BSHL BAND KINT) -LJFOLD(BSHR BAND KINT) -LJFOLD(BROL BAND KINT) -LJFOLD(BROR BAND KINT) -LJFOLDF(simplify_shiftk_andk) -{ - IRIns *irk = IR(fleft->op2); - PHIBARRIER(fleft); - if (irk->o == IR_KINT) { /* (i & k1) o k2 ==> (i o k2) & (k1 o k2) */ - int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); - fins->op1 = fleft->op1; - fins->op1 = (IRRef1)lj_opt_fold(J); - fins->op2 = (IRRef1)lj_ir_kint(J, k); - fins->ot = IRTI(IR_BAND); - return RETRYFOLD; - } else if (irk->o == IR_KINT64) { - uint64_t k = kfold_int64arith(J, ir_k64(irk)->u64, fright->i, - (IROp)fins->o); - IROpT ot = fleft->ot; - fins->op1 = fleft->op1; - fins->op1 = (IRRef1)lj_opt_fold(J); - fins->op2 = (IRRef1)lj_ir_kint64(J, k); - fins->ot = ot; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(BAND BSHL KINT) -LJFOLD(BAND BSHR KINT) -LJFOLDF(simplify_andk_shiftk) -{ - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT && - kfold_intop(-1, irk->i, (IROp)fleft->o) == fright->i) - return LEFTFOLD; /* (i o k1) & k2 ==> i, if (-1 o k1) == k2 */ - return NEXTFOLD; -} - -LJFOLD(BAND BOR KINT) -LJFOLD(BOR BAND KINT) -LJFOLDF(simplify_andor_k) -{ - IRIns *irk = IR(fleft->op2); - PHIBARRIER(fleft); - if (irk->o == IR_KINT) { - int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); - /* (i | k1) & k2 ==> i & k2, if (k1 & k2) == 0. */ - /* (i & k1) | k2 ==> i | k2, if (k1 | k2) == -1. */ - if (k == (fins->o == IR_BAND ? 0 : -1)) { - fins->op1 = fleft->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -} - -LJFOLD(BAND BOR KINT64) -LJFOLD(BOR BAND KINT64) -LJFOLDF(simplify_andor_k64) -{ -#if LJ_HASFFI - IRIns *irk = IR(fleft->op2); - PHIBARRIER(fleft); - if (irk->o == IR_KINT64) { - uint64_t k = kfold_int64arith(J, ir_k64(irk)->u64, ir_k64(fright)->u64, - (IROp)fins->o); - /* (i | k1) & k2 ==> i & k2, if (k1 & k2) == 0. */ - /* (i & k1) | k2 ==> i | k2, if (k1 | k2) == -1. */ - if (k == (fins->o == IR_BAND ? (uint64_t)0 : ~(uint64_t)0)) { - fins->op1 = fleft->op1; - return RETRYFOLD; - } - } - return NEXTFOLD; -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -/* -- Reassociation ------------------------------------------------------- */ - -LJFOLD(ADD ADD KINT) -LJFOLD(MUL MUL KINT) -LJFOLD(BAND BAND KINT) -LJFOLD(BOR BOR KINT) -LJFOLD(BXOR BXOR KINT) -LJFOLDF(reassoc_intarith_k) -{ - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT) { - int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); - if (k == irk->i) /* (i o k1) o k2 ==> i o k1, if (k1 o k2) == k1. */ - return LEFTFOLD; - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint(J, k); - return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ - } - return NEXTFOLD; -} - -LJFOLD(ADD ADD KINT64) -LJFOLD(MUL MUL KINT64) -LJFOLD(BAND BAND KINT64) -LJFOLD(BOR BOR KINT64) -LJFOLD(BXOR BXOR KINT64) -LJFOLDF(reassoc_intarith_k64) -{ -#if LJ_HASFFI - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT64) { - uint64_t k = kfold_int64arith(J, ir_k64(irk)->u64, ir_k64(fright)->u64, - (IROp)fins->o); - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint64(J, k); - return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ - } - return NEXTFOLD; -#else - UNUSED(J); lj_assertJ(0, "FFI IR op without FFI"); return FAILFOLD; -#endif -} - -LJFOLD(BAND BAND any) -LJFOLD(BOR BOR any) -LJFOLDF(reassoc_dup) -{ - if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2) - return LEFTFOLD; /* (a o b) o a ==> a o b; (a o b) o b ==> a o b */ - return NEXTFOLD; -} - -LJFOLD(MIN MIN any) -LJFOLD(MAX MAX any) -LJFOLDF(reassoc_dup_minmax) -{ - if (fins->op2 == fleft->op2) - return LEFTFOLD; /* (a o b) o b ==> a o b */ - return NEXTFOLD; -} - -LJFOLD(BXOR BXOR any) -LJFOLDF(reassoc_bxor) -{ - PHIBARRIER(fleft); - if (fins->op2 == fleft->op1) /* (a xor b) xor a ==> b */ - return fleft->op2; - if (fins->op2 == fleft->op2) /* (a xor b) xor b ==> a */ - return fleft->op1; - return NEXTFOLD; -} - -LJFOLD(BSHL BSHL KINT) -LJFOLD(BSHR BSHR KINT) -LJFOLD(BSAR BSAR KINT) -LJFOLD(BROL BROL KINT) -LJFOLD(BROR BROR KINT) -LJFOLDF(reassoc_shift) -{ - IRIns *irk = IR(fleft->op2); - PHIBARRIER(fleft); /* The (shift any KINT) rule covers k2 == 0 and more. */ - if (irk->o == IR_KINT) { /* (i o k1) o k2 ==> i o (k1 + k2) */ - int32_t mask = irt_is64(fins->t) ? 63 : 31; - int32_t k = (irk->i & mask) + (fright->i & mask); - if (k > mask) { /* Combined shift too wide? */ - if (fins->o == IR_BSHL || fins->o == IR_BSHR) - return mask == 31 ? INTFOLD(0) : INT64FOLD(0); - else if (fins->o == IR_BSAR) - k = mask; - else - k &= mask; - } - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint(J, k); - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(MIN MIN KINT) -LJFOLD(MAX MAX KINT) -LJFOLDF(reassoc_minmax_k) -{ - IRIns *irk = IR(fleft->op2); - if (irk->o == IR_KINT) { - int32_t a = irk->i; - int32_t y = kfold_intop(a, fright->i, fins->o); - if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ - return LEFTFOLD; - PHIBARRIER(fleft); - fins->op1 = fleft->op1; - fins->op2 = (IRRef1)lj_ir_kint(J, y); - return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */ - } - return NEXTFOLD; -} - -/* -- Array bounds check elimination -------------------------------------- */ - -/* Eliminate ABC across PHIs to handle t[i-1] forwarding case. -** ABC(asize, (i+k)+(-k)) ==> ABC(asize, i), but only if it already exists. -** Could be generalized to (i+k1)+k2 ==> i+(k1+k2), but needs better disambig. -*/ -LJFOLD(ABC any ADD) -LJFOLDF(abc_fwd) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) { - if (irref_isk(fright->op2)) { - IRIns *add2 = IR(fright->op1); - if (add2->o == IR_ADD && irref_isk(add2->op2) && - IR(fright->op2)->i == -IR(add2->op2)->i) { - IRRef ref = J->chain[IR_ABC]; - IRRef lim = add2->op1; - if (fins->op1 > lim) lim = fins->op1; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == fins->op1 && ir->op2 == add2->op1) - return DROPFOLD; - ref = ir->prev; - } - } - } - } - return NEXTFOLD; -} - -/* Eliminate ABC for constants. -** ABC(asize, k1), ABC(asize k2) ==> ABC(asize, max(k1, k2)) -** Drop second ABC if k2 is lower. Otherwise patch first ABC with k2. -*/ -LJFOLD(ABC any KINT) -LJFOLDF(abc_k) -{ - PHIBARRIER(fleft); - if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) { - IRRef ref = J->chain[IR_ABC]; - IRRef asize = fins->op1; - while (ref > asize) { - IRIns *ir = IR(ref); - if (ir->op1 == asize && irref_isk(ir->op2)) { - uint32_t k = (uint32_t)IR(ir->op2)->i; - if ((uint32_t)fright->i > k) - ir->op2 = fins->op2; - return DROPFOLD; - } - ref = ir->prev; - } - return EMITFOLD; /* Already performed CSE. */ - } - return NEXTFOLD; -} - -/* Eliminate invariant ABC inside loop. */ -LJFOLD(ABC any any) -LJFOLDF(abc_invar) -{ - /* Invariant ABC marked as PTR. Drop if op1 is invariant, too. */ - if (!irt_isint(fins->t) && fins->op1 < J->chain[IR_LOOP] && - !irt_isphi(IR(fins->op1)->t)) - return DROPFOLD; - return NEXTFOLD; -} - -/* -- Commutativity ------------------------------------------------------- */ - -/* The refs of commutative ops are canonicalized. Lower refs go to the right. -** Rationale behind this: -** - It (also) moves constants to the right. -** - It reduces the number of FOLD rules (e.g. (BOR any KINT) suffices). -** - It helps CSE to find more matches. -** - The assembler generates better code with constants at the right. -*/ - -LJFOLD(ADD any any) -LJFOLD(MUL any any) -LJFOLD(ADDOV any any) -LJFOLD(MULOV any any) -LJFOLDF(comm_swap) -{ - if (fins->op1 < fins->op2) { /* Move lower ref to the right. */ - IRRef1 tmp = fins->op1; - fins->op1 = fins->op2; - fins->op2 = tmp; - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(EQ any any) -LJFOLD(NE any any) -LJFOLDF(comm_equal) -{ - /* For non-numbers only: x == x ==> drop; x ~= x ==> fail */ - if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) - return CONDFOLD(fins->o == IR_EQ); - return fold_comm_swap(J); -} - -LJFOLD(LT any any) -LJFOLD(GE any any) -LJFOLD(LE any any) -LJFOLD(GT any any) -LJFOLD(ULT any any) -LJFOLD(UGE any any) -LJFOLD(ULE any any) -LJFOLD(UGT any any) -LJFOLDF(comm_comp) -{ - /* For non-numbers only: x <=> x ==> drop; x <> x ==> fail */ - if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) - return CONDFOLD((fins->o ^ (fins->o >> 1)) & 1); - if (fins->op1 < fins->op2) { /* Move lower ref to the right. */ - IRRef1 tmp = fins->op1; - fins->op1 = fins->op2; - fins->op2 = tmp; - fins->o ^= 3; /* GT <-> LT, GE <-> LE, does not affect U */ - return RETRYFOLD; - } - return NEXTFOLD; -} - -LJFOLD(BAND any any) -LJFOLD(BOR any any) -LJFOLDF(comm_dup) -{ - if (fins->op1 == fins->op2) /* x o x ==> x */ - return LEFTFOLD; - return fold_comm_swap(J); -} - -LJFOLD(MIN any any) -LJFOLD(MAX any any) -LJFOLDF(comm_dup_minmax) -{ - if (fins->op1 == fins->op2) /* x o x ==> x */ - return LEFTFOLD; - return NEXTFOLD; -} - -LJFOLD(BXOR any any) -LJFOLDF(comm_bxor) -{ - if (fins->op1 == fins->op2) /* i xor i ==> 0 */ - return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0); - return fold_comm_swap(J); -} - -/* -- Simplification of compound expressions ------------------------------ */ - -static TRef kfold_xload(jit_State *J, IRIns *ir, const void *p) -{ - int32_t k; - switch (irt_type(ir->t)) { - case IRT_NUM: return lj_ir_knum_u64(J, *(uint64_t *)p); - case IRT_I8: k = (int32_t)*(int8_t *)p; break; - case IRT_U8: k = (int32_t)*(uint8_t *)p; break; - case IRT_I16: k = (int32_t)(int16_t)lj_getu16(p); break; - case IRT_U16: k = (int32_t)(uint16_t)lj_getu16(p); break; - case IRT_INT: case IRT_U32: k = (int32_t)lj_getu32(p); break; - case IRT_I64: case IRT_U64: return lj_ir_kint64(J, *(uint64_t *)p); - default: return 0; - } - return lj_ir_kint(J, k); -} - -/* Turn: string.sub(str, a, b) == kstr -** into: string.byte(str, a) == string.byte(kstr, 1) etc. -** Note: this creates unaligned XLOADs on x86/x64. -*/ -LJFOLD(EQ SNEW KGC) -LJFOLD(NE SNEW KGC) -LJFOLDF(merge_eqne_snew_kgc) -{ - GCstr *kstr = ir_kstr(fright); - int32_t len = (int32_t)kstr->len; - lj_assertJ(irt_isstr(fins->t), "bad equality IR type"); - -#if LJ_TARGET_UNALIGNED -#define FOLD_SNEW_MAX_LEN 4 /* Handle string lengths 0, 1, 2, 3, 4. */ -#define FOLD_SNEW_TYPE8 IRT_I8 /* Creates shorter immediates. */ -#else -#define FOLD_SNEW_MAX_LEN 1 /* Handle string lengths 0 or 1. */ -#define FOLD_SNEW_TYPE8 IRT_U8 /* Prefer unsigned loads. */ -#endif - - PHIBARRIER(fleft); - if (len <= FOLD_SNEW_MAX_LEN) { - IROp op = (IROp)fins->o; - IRRef strref = fleft->op1; - if (IR(strref)->o != IR_STRREF) - return NEXTFOLD; - if (op == IR_EQ) { - emitir(IRTGI(IR_EQ), fleft->op2, lj_ir_kint(J, len)); - /* Caveat: fins/fleft/fright is no longer valid after emitir. */ - } else { - /* NE is not expanded since this would need an OR of two conds. */ - if (!irref_isk(fleft->op2)) /* Only handle the constant length case. */ - return NEXTFOLD; - if (IR(fleft->op2)->i != len) - return DROPFOLD; - } - if (len > 0) { - /* A 4 byte load for length 3 is ok -- all strings have an extra NUL. */ - uint16_t ot = (uint16_t)(len == 1 ? IRT(IR_XLOAD, FOLD_SNEW_TYPE8) : - len == 2 ? IRT(IR_XLOAD, IRT_U16) : - IRTI(IR_XLOAD)); - TRef tmp = emitir(ot, strref, - IRXLOAD_READONLY | (len > 1 ? IRXLOAD_UNALIGNED : 0)); - TRef val = kfold_xload(J, IR(tref_ref(tmp)), strdata(kstr)); - if (len == 3) - tmp = emitir(IRTI(IR_BAND), tmp, - lj_ir_kint(J, LJ_ENDIAN_SELECT(0x00ffffff, 0xffffff00))); - fins->op1 = (IRRef1)tmp; - fins->op2 = (IRRef1)val; - fins->ot = (IROpT)IRTGI(op); - return RETRYFOLD; - } else { - return DROPFOLD; - } - } - return NEXTFOLD; -} - -/* -- Loads --------------------------------------------------------------- */ - -/* Loads cannot be folded or passed on to CSE in general. -** Alias analysis is needed to check for forwarding opportunities. -** -** Caveat: *all* loads must be listed here or they end up at CSE! -*/ - -LJFOLD(ALOAD any) -LJFOLDX(lj_opt_fwd_aload) - -/* From HREF fwd (see below). Must eliminate, not supported by fwd/backend. */ -LJFOLD(HLOAD KKPTR) -LJFOLDF(kfold_hload_kkptr) -{ - UNUSED(J); - lj_assertJ(ir_kptr(fleft) == niltvg(J2G(J)), "expected niltv"); - return TREF_NIL; -} - -LJFOLD(HLOAD any) -LJFOLDX(lj_opt_fwd_hload) - -LJFOLD(ULOAD any) -LJFOLDX(lj_opt_fwd_uload) - -LJFOLD(ALEN any any) -LJFOLDX(lj_opt_fwd_alen) - -/* Upvalue refs are really loads, but there are no corresponding stores. -** So CSE is ok for them, except for UREFO across a GC step (see below). -** If the referenced function is const, its upvalue addresses are const, too. -** This can be used to improve CSE by looking for the same address, -** even if the upvalues originate from a different function. -*/ -LJFOLD(UREFO KGC any) -LJFOLD(UREFC KGC any) -LJFOLDF(cse_uref) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - IRRef ref = J->chain[fins->o]; - GCfunc *fn = ir_kfunc(fleft); - GCupval *uv = gco2uv(gcref(fn->l.uvptr[(fins->op2 >> 8)])); - while (ref > 0) { - IRIns *ir = IR(ref); - if (irref_isk(ir->op1)) { - GCfunc *fn2 = ir_kfunc(IR(ir->op1)); - if (gco2uv(gcref(fn2->l.uvptr[(ir->op2 >> 8)])) == uv) { - if (fins->o == IR_UREFO && gcstep_barrier(J, ref)) - break; - return ref; - } - } - ref = ir->prev; - } - } - return EMITFOLD; -} - -LJFOLD(HREFK any any) -LJFOLDX(lj_opt_fwd_hrefk) - -LJFOLD(HREF TNEW any) -LJFOLDF(fwd_href_tnew) -{ - if (lj_opt_fwd_href_nokey(J)) - return lj_ir_kkptr(J, niltvg(J2G(J))); - return NEXTFOLD; -} - -LJFOLD(HREF TDUP KPRI) -LJFOLD(HREF TDUP KGC) -LJFOLD(HREF TDUP KNUM) -LJFOLDF(fwd_href_tdup) -{ - TValue keyv; - lj_ir_kvalue(J->L, &keyv, fright); - if (lj_tab_get(J->L, ir_ktab(IR(fleft->op1)), &keyv) == niltvg(J2G(J)) && - lj_opt_fwd_href_nokey(J)) - return lj_ir_kkptr(J, niltvg(J2G(J))); - return NEXTFOLD; -} - -/* We can safely FOLD/CSE array/hash refs and field loads, since there -** are no corresponding stores. But we need to check for any NEWREF with -** an aliased table, as it may invalidate all of the pointers and fields. -** Only HREF needs the NEWREF check -- AREF and HREFK already depend on -** FLOADs. And NEWREF itself is treated like a store (see below). -** LREF is constant (per trace) since coroutine switches are not inlined. -*/ -LJFOLD(FLOAD TNEW IRFL_TAB_ASIZE) -LJFOLDF(fload_tab_tnew_asize) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD(fleft->op1); - return NEXTFOLD; -} - -LJFOLD(FLOAD TNEW IRFL_TAB_HMASK) -LJFOLDF(fload_tab_tnew_hmask) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD((1 << fleft->op2)-1); - return NEXTFOLD; -} - -LJFOLD(FLOAD TDUP IRFL_TAB_ASIZE) -LJFOLDF(fload_tab_tdup_asize) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->asize); - return NEXTFOLD; -} - -LJFOLD(FLOAD TDUP IRFL_TAB_HMASK) -LJFOLDF(fload_tab_tdup_hmask) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) - return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->hmask); - return NEXTFOLD; -} - -LJFOLD(HREF any any) -LJFOLD(FLOAD any IRFL_TAB_ARRAY) -LJFOLD(FLOAD any IRFL_TAB_NODE) -LJFOLD(FLOAD any IRFL_TAB_ASIZE) -LJFOLD(FLOAD any IRFL_TAB_HMASK) -LJFOLDF(fload_tab_ah) -{ - TRef tr = lj_opt_cse(J); - return lj_opt_fwd_tptr(J, tref_ref(tr)) ? tr : EMITFOLD; -} - -/* Strings are immutable, so we can safely FOLD/CSE the related FLOAD. */ -LJFOLD(FLOAD KGC IRFL_STR_LEN) -LJFOLDF(fload_str_len_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return INTFOLD((int32_t)ir_kstr(fleft)->len); - return NEXTFOLD; -} - -LJFOLD(FLOAD SNEW IRFL_STR_LEN) -LJFOLDF(fload_str_len_snew) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { - PHIBARRIER(fleft); - return fleft->op2; - } - return NEXTFOLD; -} - -LJFOLD(FLOAD TOSTR IRFL_STR_LEN) -LJFOLDF(fload_str_len_tostr) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && fleft->op2 == IRTOSTR_CHAR) - return INTFOLD(1); - return NEXTFOLD; -} - -LJFOLD(FLOAD any IRFL_SBUF_W) -LJFOLD(FLOAD any IRFL_SBUF_E) -LJFOLD(FLOAD any IRFL_SBUF_B) -LJFOLD(FLOAD any IRFL_SBUF_L) -LJFOLD(FLOAD any IRFL_SBUF_REF) -LJFOLD(FLOAD any IRFL_SBUF_R) -LJFOLDF(fload_sbuf) -{ - TRef tr = lj_opt_fwd_fload(J); - return lj_opt_fwd_sbuf(J, tref_ref(tr)) ? tr : EMITFOLD; -} - -/* The fast function ID of function objects is immutable. */ -LJFOLD(FLOAD KGC IRFL_FUNC_FFID) -LJFOLDF(fload_func_ffid_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return INTFOLD((int32_t)ir_kfunc(fleft)->c.ffid); - return NEXTFOLD; -} - -/* The C type ID of cdata objects is immutable. */ -LJFOLD(FLOAD KGC IRFL_CDATA_CTYPEID) -LJFOLDF(fload_cdata_typeid_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return INTFOLD((int32_t)ir_kcdata(fleft)->ctypeid); - return NEXTFOLD; -} - -/* Get the contents of immutable cdata objects. */ -LJFOLD(FLOAD KGC IRFL_CDATA_PTR) -LJFOLD(FLOAD KGC IRFL_CDATA_INT) -LJFOLD(FLOAD KGC IRFL_CDATA_INT64) -LJFOLDF(fload_cdata_int64_kgc) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { - void *p = cdataptr(ir_kcdata(fleft)); - if (irt_is64(fins->t)) - return INT64FOLD(*(uint64_t *)p); - else - return INTFOLD(*(int32_t *)p); - } - return NEXTFOLD; -} - -LJFOLD(FLOAD CNEW IRFL_CDATA_CTYPEID) -LJFOLD(FLOAD CNEWI IRFL_CDATA_CTYPEID) -LJFOLDF(fload_cdata_typeid_cnew) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return fleft->op1; /* No PHI barrier needed. CNEW/CNEWI op1 is const. */ - return NEXTFOLD; -} - -/* Pointer, int and int64 cdata objects are immutable. */ -LJFOLD(FLOAD CNEWI IRFL_CDATA_PTR) -LJFOLD(FLOAD CNEWI IRFL_CDATA_INT) -LJFOLD(FLOAD CNEWI IRFL_CDATA_INT64) -LJFOLDF(fload_cdata_ptr_int64_cnew) -{ - if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) - return fleft->op2; /* Fold even across PHI to avoid allocations. */ - return NEXTFOLD; -} - -LJFOLD(FLOAD any IRFL_STR_LEN) -LJFOLD(FLOAD any IRFL_FUNC_ENV) -LJFOLD(FLOAD any IRFL_THREAD_ENV) -LJFOLD(FLOAD any IRFL_CDATA_CTYPEID) -LJFOLD(FLOAD any IRFL_CDATA_PTR) -LJFOLD(FLOAD any IRFL_CDATA_INT) -LJFOLD(FLOAD any IRFL_CDATA_INT64) -LJFOLD(VLOAD any any) /* Vararg loads have no corresponding stores. */ -LJFOLDX(lj_opt_cse) - -/* All other field loads need alias analysis. */ -LJFOLD(FLOAD any any) -LJFOLDX(lj_opt_fwd_fload) - -/* This is for LOOP only. Recording handles SLOADs internally. */ -LJFOLD(SLOAD any any) -LJFOLDF(fwd_sload) -{ - if ((fins->op2 & IRSLOAD_FRAME)) { - TRef tr = lj_opt_cse(J); - return tref_ref(tr) < J->chain[IR_RETF] ? EMITFOLD : tr; - } else { - lj_assertJ(J->slot[fins->op1] != 0, "uninitialized slot accessed"); - return J->slot[fins->op1]; - } -} - -/* Only fold for KKPTR. The pointer _and_ the contents must be const. */ -LJFOLD(XLOAD KKPTR any) -LJFOLDF(xload_kptr) -{ - TRef tr = kfold_xload(J, fins, ir_kptr(fleft)); - return tr ? tr : NEXTFOLD; -} - -LJFOLD(XLOAD any any) -LJFOLDX(lj_opt_fwd_xload) - -/* -- Frame handling ------------------------------------------------------ */ - -/* Prevent CSE of a REF_BASE operand across IR_RETF. */ -LJFOLD(SUB any BASE) -LJFOLD(SUB BASE any) -LJFOLD(EQ any BASE) -LJFOLDF(fold_base) -{ - return lj_opt_cselim(J, J->chain[IR_RETF]); -} - -/* -- Write barriers ------------------------------------------------------ */ - -/* Write barriers are amenable to CSE, but not across any incremental -** GC steps. -** -** The same logic applies to open upvalue references, because a stack -** may be resized during a GC step (not the current stack, but maybe that -** of a coroutine). -*/ -LJFOLD(TBAR any) -LJFOLD(OBAR any any) -LJFOLD(UREFO any any) -LJFOLDF(barrier_tab) -{ - TRef tr = lj_opt_cse(J); - if (gcstep_barrier(J, tref_ref(tr))) /* CSE across GC step? */ - return EMITFOLD; /* Raw emit. Assumes fins is left intact by CSE. */ - return tr; -} - -LJFOLD(TBAR TNEW) -LJFOLD(TBAR TDUP) -LJFOLDF(barrier_tnew_tdup) -{ - /* New tables are always white and never need a barrier. */ - if (fins->op1 < J->chain[IR_LOOP]) /* Except across a GC step. */ - return NEXTFOLD; - return DROPFOLD; -} - -/* -- Profiling ----------------------------------------------------------- */ - -LJFOLD(PROF any any) -LJFOLDF(prof) -{ - IRRef ref = J->chain[IR_PROF]; - if (ref+1 == J->cur.nins) /* Drop neighbouring IR_PROF. */ - return ref; - return EMITFOLD; -} - -/* -- Stores and allocations ---------------------------------------------- */ - -/* Stores and allocations cannot be folded or passed on to CSE in general. -** But some stores can be eliminated with dead-store elimination (DSE). -** -** Caveat: *all* stores and allocs must be listed here or they end up at CSE! -*/ - -LJFOLD(ASTORE any any) -LJFOLD(HSTORE any any) -LJFOLDX(lj_opt_dse_ahstore) - -LJFOLD(USTORE any any) -LJFOLDX(lj_opt_dse_ustore) - -LJFOLD(FSTORE any any) -LJFOLDX(lj_opt_dse_fstore) - -LJFOLD(XSTORE any any) -LJFOLDX(lj_opt_dse_xstore) - -LJFOLD(NEWREF any any) /* Treated like a store. */ -LJFOLD(TMPREF any any) -LJFOLD(CALLA any any) -LJFOLD(CALLL any any) /* Safeguard fallback. */ -LJFOLD(CALLS any any) -LJFOLD(CALLXS any any) -LJFOLD(XBAR) -LJFOLD(RETF any any) /* Modifies BASE. */ -LJFOLD(TNEW any any) -LJFOLD(TDUP any) -LJFOLD(CNEW any any) -LJFOLD(XSNEW any any) -LJFOLDX(lj_ir_emit) - -/* ------------------------------------------------------------------------ */ - -/* Every entry in the generated hash table is a 32 bit pattern: -** -** xxxxxxxx iiiiiii lllllll rrrrrrrrrr -** -** xxxxxxxx = 8 bit index into fold function table -** iiiiiii = 7 bit folded instruction opcode -** lllllll = 7 bit left instruction opcode -** rrrrrrrrrr = 8 bit right instruction opcode or 10 bits from literal field -*/ - -#include "lj_folddef.h" - -/* ------------------------------------------------------------------------ */ - -/* Fold IR instruction. */ -TRef LJ_FASTCALL lj_opt_fold(jit_State *J) -{ - uint32_t key, any; - IRRef ref; - - if (LJ_UNLIKELY((J->flags & JIT_F_OPT_MASK) != JIT_F_OPT_DEFAULT)) { - lj_assertJ(((JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE|JIT_F_OPT_DSE) | - JIT_F_OPT_DEFAULT) == JIT_F_OPT_DEFAULT, - "bad JIT_F_OPT_DEFAULT"); - /* Folding disabled? Chain to CSE, but not for loads/stores/allocs. */ - if (!(J->flags & JIT_F_OPT_FOLD) && irm_kind(lj_ir_mode[fins->o]) == IRM_N) - return lj_opt_cse(J); - - /* No FOLD, forwarding or CSE? Emit raw IR for loads, except for SLOAD. */ - if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE)) != - (JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE) && - irm_kind(lj_ir_mode[fins->o]) == IRM_L && fins->o != IR_SLOAD) - return lj_ir_emit(J); - - /* No FOLD or DSE? Emit raw IR for stores. */ - if ((J->flags & (JIT_F_OPT_FOLD|JIT_F_OPT_DSE)) != - (JIT_F_OPT_FOLD|JIT_F_OPT_DSE) && - irm_kind(lj_ir_mode[fins->o]) == IRM_S) - return lj_ir_emit(J); - } - - /* Fold engine start/retry point. */ -retry: - /* Construct key from opcode and operand opcodes (unless literal/none). */ - key = ((uint32_t)fins->o << 17); - if (fins->op1 >= J->cur.nk) { - key += (uint32_t)IR(fins->op1)->o << 10; - *fleft = *IR(fins->op1); - if (fins->op1 < REF_TRUE) - fleft[1] = IR(fins->op1)[1]; - } - if (fins->op2 >= J->cur.nk) { - key += (uint32_t)IR(fins->op2)->o; - *fright = *IR(fins->op2); - if (fins->op2 < REF_TRUE) - fright[1] = IR(fins->op2)[1]; - } else { - key += (fins->op2 & 0x3ffu); /* Literal mask. Must include IRCONV_*MASK. */ - } - - /* Check for a match in order from most specific to least specific. */ - any = 0; - for (;;) { - uint32_t k = key | (any & 0x1ffff); - uint32_t h = fold_hashkey(k); - uint32_t fh = fold_hash[h]; /* Lookup key in semi-perfect hash table. */ - if ((fh & 0xffffff) == k || (fh = fold_hash[h+1], (fh & 0xffffff) == k)) { - ref = (IRRef)tref_ref(fold_func[fh >> 24](J)); - if (ref != NEXTFOLD) - break; - } - if (any == 0xfffff) /* Exhausted folding. Pass on to CSE. */ - return lj_opt_cse(J); - any = (any | (any >> 10)) ^ 0xffc00; - } - - /* Return value processing, ordered by frequency. */ - if (LJ_LIKELY(ref >= MAX_FOLD)) - return TREF(ref, irt_t(IR(ref)->t)); - if (ref == RETRYFOLD) - goto retry; - if (ref == KINTFOLD) - return lj_ir_kint(J, fins->i); - if (ref == FAILFOLD) - lj_trace_err(J, LJ_TRERR_GFAIL); - lj_assertJ(ref == DROPFOLD, "bad fold result"); - return REF_DROP; -} - -/* -- Common-Subexpression Elimination ------------------------------------ */ - -/* CSE an IR instruction. This is very fast due to the skip-list chains. */ -TRef LJ_FASTCALL lj_opt_cse(jit_State *J) -{ - /* Avoid narrow to wide store-to-load forwarding stall */ - IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16); - IROp op = fins->o; - if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { - /* Limited search for same operands in per-opcode chain. */ - IRRef ref = J->chain[op]; - IRRef lim = fins->op1; - if (fins->op2 > lim) lim = fins->op2; /* Relies on lit < REF_BIAS. */ - while (ref > lim) { - if (IR(ref)->op12 == op12) - return TREF(ref, irt_t(IR(ref)->t)); /* Common subexpression found. */ - ref = IR(ref)->prev; - } - } - /* Otherwise emit IR (inlined for speed). */ - { - IRRef ref = lj_ir_nextins(J); - IRIns *ir = IR(ref); - ir->prev = J->chain[op]; - ir->op12 = op12; - J->chain[op] = (IRRef1)ref; - ir->o = fins->o; - J->guardemit.irt |= fins->t.irt; - return TREF(ref, irt_t((ir->t = fins->t))); - } -} - -/* CSE with explicit search limit. */ -TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim) -{ - IRRef ref = J->chain[fins->o]; - IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16); - while (ref > lim) { - if (IR(ref)->op12 == op12) - return ref; - ref = IR(ref)->prev; - } - return lj_ir_emit(J); -} - -/* ------------------------------------------------------------------------ */ - -#undef IR -#undef fins -#undef fleft -#undef fright -#undef knumleft -#undef knumright -#undef emitir - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_loop.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_loop.c deleted file mode 100644 index ee3ee04..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_loop.c +++ /dev/null @@ -1,453 +0,0 @@ -/* -** LOOP: Loop Optimizations. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_loop_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_vm.h" - -/* Loop optimization: -** -** Traditional Loop-Invariant Code Motion (LICM) splits the instructions -** of a loop into invariant and variant instructions. The invariant -** instructions are hoisted out of the loop and only the variant -** instructions remain inside the loop body. -** -** Unfortunately LICM is mostly useless for compiling dynamic languages. -** The IR has many guards and most of the subsequent instructions are -** control-dependent on them. The first non-hoistable guard would -** effectively prevent hoisting of all subsequent instructions. -** -** That's why we use a special form of unrolling using copy-substitution, -** combined with redundancy elimination: -** -** The recorded instruction stream is re-emitted to the compiler pipeline -** with substituted operands. The substitution table is filled with the -** refs returned by re-emitting each instruction. This can be done -** on-the-fly, because the IR is in strict SSA form, where every ref is -** defined before its use. -** -** This aproach generates two code sections, separated by the LOOP -** instruction: -** -** 1. The recorded instructions form a kind of pre-roll for the loop. It -** contains a mix of invariant and variant instructions and performs -** exactly one loop iteration (but not necessarily the 1st iteration). -** -** 2. The loop body contains only the variant instructions and performs -** all remaining loop iterations. -** -** On first sight that looks like a waste of space, because the variant -** instructions are present twice. But the key insight is that the -** pre-roll honors the control-dependencies for *both* the pre-roll itself -** *and* the loop body! -** -** It also means one doesn't have to explicitly model control-dependencies -** (which, BTW, wouldn't help LICM much). And it's much easier to -** integrate sparse snapshotting with this approach. -** -** One of the nicest aspects of this approach is that all of the -** optimizations of the compiler pipeline (FOLD, CSE, FWD, etc.) can be -** reused with only minor restrictions (e.g. one should not fold -** instructions across loop-carried dependencies). -** -** But in general all optimizations can be applied which only need to look -** backwards into the generated instruction stream. At any point in time -** during the copy-substitution process this contains both a static loop -** iteration (the pre-roll) and a dynamic one (from the to-be-copied -** instruction up to the end of the partial loop body). -** -** Since control-dependencies are implicitly kept, CSE also applies to all -** kinds of guards. The major advantage is that all invariant guards can -** be hoisted, too. -** -** Load/store forwarding works across loop iterations, too. This is -** important if loop-carried dependencies are kept in upvalues or tables. -** E.g. 'self.idx = self.idx + 1' deep down in some OO-style method may -** become a forwarded loop-recurrence after inlining. -** -** Since the IR is in SSA form, loop-carried dependencies have to be -** modeled with PHI instructions. The potential candidates for PHIs are -** collected on-the-fly during copy-substitution. After eliminating the -** redundant ones, PHI instructions are emitted *below* the loop body. -** -** Note that this departure from traditional SSA form doesn't change the -** semantics of the PHI instructions themselves. But it greatly simplifies -** on-the-fly generation of the IR and the machine code. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Emit raw IR without passing through optimizations. */ -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- PHI elimination ----------------------------------------------------- */ - -/* Emit or eliminate collected PHIs. */ -static void loop_emit_phi(jit_State *J, IRRef1 *subst, IRRef1 *phi, IRRef nphi, - SnapNo onsnap) -{ - int passx = 0; - IRRef i, j, nslots; - IRRef invar = J->chain[IR_LOOP]; - /* Pass #1: mark redundant and potentially redundant PHIs. */ - for (i = 0, j = 0; i < nphi; i++) { - IRRef lref = phi[i]; - IRRef rref = subst[lref]; - if (lref == rref || rref == REF_DROP) { /* Invariants are redundant. */ - irt_clearphi(IR(lref)->t); - } else { - phi[j++] = (IRRef1)lref; - if (!(IR(rref)->op1 == lref || IR(rref)->op2 == lref)) { - /* Quick check for simple recurrences failed, need pass2. */ - irt_setmark(IR(lref)->t); - passx = 1; - } - } - } - nphi = j; - /* Pass #2: traverse variant part and clear marks of non-redundant PHIs. */ - if (passx) { - SnapNo s; - for (i = J->cur.nins-1; i > invar; i--) { - IRIns *ir = IR(i); - if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t); - if (!irref_isk(ir->op1)) { - irt_clearmark(IR(ir->op1)->t); - if (ir->op1 < invar && - ir->o >= IR_CALLN && ir->o <= IR_CARG) { /* ORDER IR */ - ir = IR(ir->op1); - while (ir->o == IR_CARG) { - if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t); - if (irref_isk(ir->op1)) break; - ir = IR(ir->op1); - irt_clearmark(ir->t); - } - } - } - } - for (s = J->cur.nsnap-1; s >= onsnap; s--) { - SnapShot *snap = &J->cur.snap[s]; - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - IRRef ref = snap_ref(map[n]); - if (!irref_isk(ref)) irt_clearmark(IR(ref)->t); - } - } - } - /* Pass #3: add PHIs for variant slots without a corresponding SLOAD. */ - nslots = J->baseslot+J->maxslot; - for (i = 1; i < nslots; i++) { - IRRef ref = tref_ref(J->slot[i]); - while (!irref_isk(ref) && ref != subst[ref]) { - IRIns *ir = IR(ref); - irt_clearmark(ir->t); /* Unmark potential uses, too. */ - if (irt_isphi(ir->t) || irt_ispri(ir->t)) - break; - irt_setphi(ir->t); - if (nphi >= LJ_MAX_PHI) - lj_trace_err(J, LJ_TRERR_PHIOV); - phi[nphi++] = (IRRef1)ref; - ref = subst[ref]; - if (ref > invar) - break; - } - } - /* Pass #4: propagate non-redundant PHIs. */ - while (passx) { - passx = 0; - for (i = 0; i < nphi; i++) { - IRRef lref = phi[i]; - IRIns *ir = IR(lref); - if (!irt_ismarked(ir->t)) { /* Propagate only from unmarked PHIs. */ - IRIns *irr = IR(subst[lref]); - if (irt_ismarked(irr->t)) { /* Right ref points to other PHI? */ - irt_clearmark(irr->t); /* Mark that PHI as non-redundant. */ - passx = 1; /* Retry. */ - } - } - } - } - /* Pass #5: emit PHI instructions or eliminate PHIs. */ - for (i = 0; i < nphi; i++) { - IRRef lref = phi[i]; - IRIns *ir = IR(lref); - if (!irt_ismarked(ir->t)) { /* Emit PHI if not marked. */ - IRRef rref = subst[lref]; - if (rref > invar) - irt_setphi(IR(rref)->t); - emitir_raw(IRT(IR_PHI, irt_type(ir->t)), lref, rref); - } else { /* Otherwise eliminate PHI. */ - irt_clearmark(ir->t); - irt_clearphi(ir->t); - } - } -} - -/* -- Loop unrolling using copy-substitution ------------------------------ */ - -/* Copy-substitute snapshot. */ -static void loop_subst_snap(jit_State *J, SnapShot *osnap, - SnapEntry *loopmap, IRRef1 *subst) -{ - SnapEntry *nmap, *omap = &J->cur.snapmap[osnap->mapofs]; - SnapEntry *nextmap = &J->cur.snapmap[snap_nextofs(&J->cur, osnap)]; - MSize nmapofs; - MSize on, ln, nn, onent = osnap->nent; - BCReg nslots = osnap->nslots; - SnapShot *snap = &J->cur.snap[J->cur.nsnap]; - if (irt_isguard(J->guardemit)) { /* Guard inbetween? */ - nmapofs = J->cur.nsnapmap; - J->cur.nsnap++; /* Add new snapshot. */ - } else { /* Otherwise overwrite previous snapshot. */ - snap--; - nmapofs = snap->mapofs; - } - J->guardemit.irt = 0; - /* Setup new snapshot. */ - snap->mapofs = (uint32_t)nmapofs; - snap->ref = (IRRef1)J->cur.nins; - snap->mcofs = 0; - snap->nslots = nslots; - snap->topslot = osnap->topslot; - snap->count = 0; - nmap = &J->cur.snapmap[nmapofs]; - /* Substitute snapshot slots. */ - on = ln = nn = 0; - while (on < onent) { - SnapEntry osn = omap[on], lsn = loopmap[ln]; - if (snap_slot(lsn) < snap_slot(osn)) { /* Copy slot from loop map. */ - nmap[nn++] = lsn; - ln++; - } else { /* Copy substituted slot from snapshot map. */ - if (snap_slot(lsn) == snap_slot(osn)) ln++; /* Shadowed loop slot. */ - if (!irref_isk(snap_ref(osn))) - osn = snap_setref(osn, subst[snap_ref(osn)]); - nmap[nn++] = osn; - on++; - } - } - while (snap_slot(loopmap[ln]) < nslots) /* Copy remaining loop slots. */ - nmap[nn++] = loopmap[ln++]; - snap->nent = (uint8_t)nn; - omap += onent; - nmap += nn; - while (omap < nextmap) /* Copy PC + frame links. */ - *nmap++ = *omap++; - J->cur.nsnapmap = (uint32_t)(nmap - J->cur.snapmap); -} - -typedef struct LoopState { - jit_State *J; - IRRef1 *subst; - MSize sizesubst; -} LoopState; - -/* Unroll loop. */ -static void loop_unroll(LoopState *lps) -{ - jit_State *J = lps->J; - IRRef1 phi[LJ_MAX_PHI]; - uint32_t nphi = 0; - IRRef1 *subst; - SnapNo onsnap; - SnapShot *osnap, *loopsnap; - SnapEntry *loopmap, *psentinel; - IRRef ins, invar; - - /* Allocate substitution table. - ** Only non-constant refs in [REF_BIAS,invar) are valid indexes. - */ - invar = J->cur.nins; - lps->sizesubst = invar - REF_BIAS; - lps->subst = lj_mem_newvec(J->L, lps->sizesubst, IRRef1); - subst = lps->subst - REF_BIAS; - subst[REF_BASE] = REF_BASE; - - /* LOOP separates the pre-roll from the loop body. */ - emitir_raw(IRTG(IR_LOOP, IRT_NIL), 0, 0); - - /* Grow snapshot buffer and map for copy-substituted snapshots. - ** Need up to twice the number of snapshots minus #0 and loop snapshot. - ** Need up to twice the number of entries plus fallback substitutions - ** from the loop snapshot entries for each new snapshot. - ** Caveat: both calls may reallocate J->cur.snap and J->cur.snapmap! - */ - onsnap = J->cur.nsnap; - lj_snap_grow_buf(J, 2*onsnap-2); - lj_snap_grow_map(J, J->cur.nsnapmap*2+(onsnap-2)*J->cur.snap[onsnap-1].nent); - - /* The loop snapshot is used for fallback substitutions. */ - loopsnap = &J->cur.snap[onsnap-1]; - loopmap = &J->cur.snapmap[loopsnap->mapofs]; - /* The PC of snapshot #0 and the loop snapshot must match. */ - psentinel = &loopmap[loopsnap->nent]; - lj_assertJ(*psentinel == J->cur.snapmap[J->cur.snap[0].nent], - "mismatched PC for loop snapshot"); - *psentinel = SNAP(255, 0, 0); /* Replace PC with temporary sentinel. */ - - /* Start substitution with snapshot #1 (#0 is empty for root traces). */ - osnap = &J->cur.snap[1]; - - /* Copy and substitute all recorded instructions and snapshots. */ - for (ins = REF_FIRST; ins < invar; ins++) { - IRIns *ir; - IRRef op1, op2; - - if (ins >= osnap->ref) /* Instruction belongs to next snapshot? */ - loop_subst_snap(J, osnap++, loopmap, subst); /* Copy-substitute it. */ - - /* Substitute instruction operands. */ - ir = IR(ins); - op1 = ir->op1; - if (!irref_isk(op1)) op1 = subst[op1]; - op2 = ir->op2; - if (!irref_isk(op2)) op2 = subst[op2]; - if (irm_kind(lj_ir_mode[ir->o]) == IRM_N && - op1 == ir->op1 && op2 == ir->op2) { /* Regular invariant ins? */ - subst[ins] = (IRRef1)ins; /* Shortcut. */ - } else { - /* Re-emit substituted instruction to the FOLD/CSE/etc. pipeline. */ - IRType1 t = ir->t; /* Get this first, since emitir may invalidate ir. */ - IRRef ref = tref_ref(emitir(ir->ot & ~IRT_ISPHI, op1, op2)); - subst[ins] = (IRRef1)ref; - if (ref != ins) { - IRIns *irr = IR(ref); - if (ref < invar) { /* Loop-carried dependency? */ - /* Potential PHI? */ - if (!irref_isk(ref) && !irt_isphi(irr->t) && !irt_ispri(irr->t)) { - irt_setphi(irr->t); - if (nphi >= LJ_MAX_PHI) - lj_trace_err(J, LJ_TRERR_PHIOV); - phi[nphi++] = (IRRef1)ref; - } - /* Check all loop-carried dependencies for type instability. */ - if (!irt_sametype(t, irr->t)) { - if (irt_isinteger(t) && irt_isinteger(irr->t)) - continue; - else if (irt_isnum(t) && irt_isinteger(irr->t)) /* Fix int->num. */ - ref = tref_ref(emitir(IRTN(IR_CONV), ref, IRCONV_NUM_INT)); - else if (irt_isnum(irr->t) && irt_isinteger(t)) /* Fix num->int. */ - ref = tref_ref(emitir(IRTGI(IR_CONV), ref, - IRCONV_INT_NUM|IRCONV_CHECK)); - else - lj_trace_err(J, LJ_TRERR_TYPEINS); - subst[ins] = (IRRef1)ref; - irr = IR(ref); - goto phiconv; - } - } else if (ref != REF_DROP && ref > invar && - ((irr->o == IR_CONV && irr->op1 < invar) || - (irr->o == IR_ALEN && irr->op2 < invar && - irr->op2 != REF_NIL))) { - /* May need an extra PHI for a CONV or ALEN hint. */ - ref = irr->o == IR_CONV ? irr->op1 : irr->op2; - irr = IR(ref); - phiconv: - if (ref < invar && !irref_isk(ref) && !irt_isphi(irr->t)) { - irt_setphi(irr->t); - if (nphi >= LJ_MAX_PHI) - lj_trace_err(J, LJ_TRERR_PHIOV); - phi[nphi++] = (IRRef1)ref; - } - } - } - } - } - if (!irt_isguard(J->guardemit)) /* Drop redundant snapshot. */ - J->cur.nsnapmap = (uint32_t)J->cur.snap[--J->cur.nsnap].mapofs; - lj_assertJ(J->cur.nsnapmap <= J->sizesnapmap, "bad snapshot map index"); - *psentinel = J->cur.snapmap[J->cur.snap[0].nent]; /* Restore PC. */ - - loop_emit_phi(J, subst, phi, nphi, onsnap); -} - -/* Undo any partial changes made by the loop optimization. */ -static void loop_undo(jit_State *J, IRRef ins, SnapNo nsnap, MSize nsnapmap) -{ - ptrdiff_t i; - SnapShot *snap = &J->cur.snap[nsnap-1]; - SnapEntry *map = J->cur.snapmap; - map[snap->mapofs + snap->nent] = map[J->cur.snap[0].nent]; /* Restore PC. */ - J->cur.nsnapmap = (uint32_t)nsnapmap; - J->cur.nsnap = nsnap; - J->guardemit.irt = 0; - lj_ir_rollback(J, ins); - for (i = 0; i < BPROP_SLOTS; i++) { /* Remove backprop. cache entries. */ - BPropEntry *bp = &J->bpropcache[i]; - if (bp->val >= ins) - bp->key = 0; - } - for (ins--; ins >= REF_FIRST; ins--) { /* Remove flags. */ - IRIns *ir = IR(ins); - irt_clearphi(ir->t); - irt_clearmark(ir->t); - } -} - -/* Protected callback for loop optimization. */ -static TValue *cploop_opt(lua_State *L, lua_CFunction dummy, void *ud) -{ - UNUSED(L); UNUSED(dummy); - loop_unroll((LoopState *)ud); - return NULL; -} - -/* Loop optimization. */ -int lj_opt_loop(jit_State *J) -{ - IRRef nins = J->cur.nins; - SnapNo nsnap = J->cur.nsnap; - MSize nsnapmap = J->cur.nsnapmap; - LoopState lps; - int errcode; - lps.J = J; - lps.subst = NULL; - lps.sizesubst = 0; - errcode = lj_vm_cpcall(J->L, NULL, &lps, cploop_opt); - lj_mem_freevec(J2G(J), lps.subst, lps.sizesubst, IRRef1); - if (LJ_UNLIKELY(errcode)) { - lua_State *L = J->L; - if (errcode == LUA_ERRRUN && tvisnumber(L->top-1)) { /* Trace error? */ - int32_t e = numberVint(L->top-1); - switch ((TraceError)e) { - case LJ_TRERR_TYPEINS: /* Type instability. */ - case LJ_TRERR_GFAIL: /* Guard would always fail. */ - /* Unrolling via recording fixes many cases, e.g. a flipped boolean. */ - if (--J->instunroll < 0) /* But do not unroll forever. */ - break; - L->top--; /* Remove error object. */ - loop_undo(J, nins, nsnap, nsnapmap); - return 1; /* Loop optimization failed, continue recording. */ - default: - break; - } - } - lj_err_throw(L, errcode); /* Propagate all other errors. */ - } - return 0; /* Loop optimization is ok. */ -} - -#undef IR -#undef emitir -#undef emitir_raw - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_mem.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_mem.c deleted file mode 100644 index 09de2f0..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_mem.c +++ /dev/null @@ -1,979 +0,0 @@ -/* -** Memory access optimizations. -** AA: Alias Analysis using high-level semantic disambiguation. -** FWD: Load Forwarding (L2L) + Store Forwarding (S2L). -** DSE: Dead-Store Elimination. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_mem_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_tab.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_ircall.h" -#include "lj_dispatch.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) -#define fleft (J->fold.left) -#define fright (J->fold.right) - -/* -** Caveat #1: return value is not always a TRef -- only use with tref_ref(). -** Caveat #2: FWD relies on active CSE for xREF operands -- see lj_opt_fold(). -*/ - -/* Return values from alias analysis. */ -typedef enum { - ALIAS_NO, /* The two refs CANNOT alias (exact). */ - ALIAS_MAY, /* The two refs MAY alias (inexact). */ - ALIAS_MUST /* The two refs MUST alias (exact). */ -} AliasRet; - -/* -- ALOAD/HLOAD forwarding and ASTORE/HSTORE elimination ---------------- */ - -/* Simplified escape analysis: check for intervening stores. */ -static AliasRet aa_escape(jit_State *J, IRIns *ir, IRIns *stop) -{ - IRRef ref = (IRRef)(ir - J->cur.ir); /* The ref that might be stored. */ - for (ir++; ir < stop; ir++) - if (ir->op2 == ref && - (ir->o == IR_ASTORE || ir->o == IR_HSTORE || - ir->o == IR_USTORE || ir->o == IR_FSTORE)) - return ALIAS_MAY; /* Reference was stored and might alias. */ - return ALIAS_NO; /* Reference was not stored. */ -} - -/* Alias analysis for two different table references. */ -static AliasRet aa_table(jit_State *J, IRRef ta, IRRef tb) -{ - IRIns *taba = IR(ta), *tabb = IR(tb); - int newa, newb; - lj_assertJ(ta != tb, "bad usage"); - lj_assertJ(irt_istab(taba->t) && irt_istab(tabb->t), "bad usage"); - /* Disambiguate new allocations. */ - newa = (taba->o == IR_TNEW || taba->o == IR_TDUP); - newb = (tabb->o == IR_TNEW || tabb->o == IR_TDUP); - if (newa && newb) - return ALIAS_NO; /* Two different allocations never alias. */ - if (newb) { /* At least one allocation? */ - IRIns *tmp = taba; taba = tabb; tabb = tmp; - } else if (!newa) { - return ALIAS_MAY; /* Anything else: we just don't know. */ - } - return aa_escape(J, taba, tabb); -} - -/* Check whether there's no aliasing table.clear. */ -static int fwd_aa_tab_clear(jit_State *J, IRRef lim, IRRef ta) -{ - IRRef ref = J->chain[IR_CALLS]; - while (ref > lim) { - IRIns *calls = IR(ref); - if (calls->op2 == IRCALL_lj_tab_clear && - (ta == calls->op1 || aa_table(J, ta, calls->op1) != ALIAS_NO)) - return 0; /* Conflict. */ - ref = calls->prev; - } - return 1; /* No conflict. Can safely FOLD/CSE. */ -} - -/* Check whether there's no aliasing NEWREF/table.clear for the left operand. */ -int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim) -{ - IRRef ta = fins->op1; - IRRef ref = J->chain[IR_NEWREF]; - while (ref > lim) { - IRIns *newref = IR(ref); - if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO) - return 0; /* Conflict. */ - ref = newref->prev; - } - return fwd_aa_tab_clear(J, lim, ta); -} - -/* Alias analysis for array and hash access using key-based disambiguation. */ -static AliasRet aa_ahref(jit_State *J, IRIns *refa, IRIns *refb) -{ - IRRef ka = refa->op2; - IRRef kb = refb->op2; - IRIns *keya, *keyb; - IRRef ta, tb; - if (refa == refb) - return ALIAS_MUST; /* Shortcut for same refs. */ - keya = IR(ka); - if (keya->o == IR_KSLOT) { ka = keya->op1; keya = IR(ka); } - keyb = IR(kb); - if (keyb->o == IR_KSLOT) { kb = keyb->op1; keyb = IR(kb); } - ta = (refa->o==IR_HREFK || refa->o==IR_AREF) ? IR(refa->op1)->op1 : refa->op1; - tb = (refb->o==IR_HREFK || refb->o==IR_AREF) ? IR(refb->op1)->op1 : refb->op1; - if (ka == kb) { - /* Same key. Check for same table with different ref (NEWREF vs. HREF). */ - if (ta == tb) - return ALIAS_MUST; /* Same key, same table. */ - else - return aa_table(J, ta, tb); /* Same key, possibly different table. */ - } - if (irref_isk(ka) && irref_isk(kb)) - return ALIAS_NO; /* Different constant keys. */ - if (refa->o == IR_AREF) { - /* Disambiguate array references based on index arithmetic. */ - int32_t ofsa = 0, ofsb = 0; - IRRef basea = ka, baseb = kb; - lj_assertJ(refb->o == IR_AREF, "expected AREF"); - /* Gather base and offset from t[base] or t[base+-ofs]. */ - if (keya->o == IR_ADD && irref_isk(keya->op2)) { - basea = keya->op1; - ofsa = IR(keya->op2)->i; - if (basea == kb && ofsa != 0) - return ALIAS_NO; /* t[base+-ofs] vs. t[base]. */ - } - if (keyb->o == IR_ADD && irref_isk(keyb->op2)) { - baseb = keyb->op1; - ofsb = IR(keyb->op2)->i; - if (ka == baseb && ofsb != 0) - return ALIAS_NO; /* t[base] vs. t[base+-ofs]. */ - } - if (basea == baseb && ofsa != ofsb) - return ALIAS_NO; /* t[base+-o1] vs. t[base+-o2] and o1 != o2. */ - } else { - /* Disambiguate hash references based on the type of their keys. */ - lj_assertJ((refa->o==IR_HREF || refa->o==IR_HREFK || refa->o==IR_NEWREF) && - (refb->o==IR_HREF || refb->o==IR_HREFK || refb->o==IR_NEWREF), - "bad xREF IR op %d or %d", refa->o, refb->o); - if (!irt_sametype(keya->t, keyb->t)) - return ALIAS_NO; /* Different key types. */ - } - if (ta == tb) - return ALIAS_MAY; /* Same table, cannot disambiguate keys. */ - else - return aa_table(J, ta, tb); /* Try to disambiguate tables. */ -} - -/* Array and hash load forwarding. */ -static TRef fwd_ahload(jit_State *J, IRRef xref) -{ - IRIns *xr = IR(xref); - IRRef lim = xref; /* Search limit. */ - IRRef ref; - - /* Search for conflicting stores. */ - ref = J->chain[fins->o+IRDELTA_L2S]; - while (ref > xref) { - IRIns *store = IR(ref); - switch (aa_ahref(J, xr, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - - /* No conflicting store (yet): const-fold loads from allocations. */ - { - IRIns *ir = (xr->o == IR_HREFK || xr->o == IR_AREF) ? IR(xr->op1) : xr; - IRRef tab = ir->op1; - ir = IR(tab); - if ((ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) && - fwd_aa_tab_clear(J, tab, tab)) { - /* A NEWREF with a number key may end up pointing to the array part. - ** But it's referenced from HSTORE and not found in the ASTORE chain. - ** For now simply consider this a conflict without forwarding anything. - */ - if (xr->o == IR_AREF) { - IRRef ref2 = J->chain[IR_NEWREF]; - while (ref2 > tab) { - IRIns *newref = IR(ref2); - if (irt_isnum(IR(newref->op2)->t)) - goto cselim; - ref2 = newref->prev; - } - } - /* NEWREF inhibits CSE for HREF, and dependent FLOADs from HREFK/AREF. - ** But the above search for conflicting stores was limited by xref. - ** So continue searching, limited by the TNEW/TDUP. Store forwarding - ** is ok, too. A conflict does NOT limit the search for a matching load. - */ - while (ref > tab) { - IRIns *store = IR(ref); - switch (aa_ahref(J, xr, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: goto cselim; /* Conflicting store. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - if (ir->o == IR_TNEW && !irt_isnil(fins->t)) - return 0; /* Type instability in loop-carried dependency. */ - if (irt_ispri(fins->t)) { - return TREF_PRI(irt_type(fins->t)); - } else if (irt_isnum(fins->t) || (LJ_DUALNUM && irt_isint(fins->t)) || - irt_isstr(fins->t)) { - TValue keyv; - cTValue *tv; - IRIns *key = IR(xr->op2); - if (key->o == IR_KSLOT) key = IR(key->op1); - lj_ir_kvalue(J->L, &keyv, key); - tv = lj_tab_get(J->L, ir_ktab(IR(ir->op1)), &keyv); - lj_assertJ(itype2irt(tv) == irt_type(fins->t), - "mismatched type in constant table"); - if (irt_isnum(fins->t)) - return lj_ir_knum_u64(J, tv->u64); - else if (LJ_DUALNUM && irt_isint(fins->t)) - return lj_ir_kint(J, intV(tv)); - else - return lj_ir_kstr(J, strV(tv)); - } - /* Othwerwise: don't intern as a constant. */ - } - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - ref = J->chain[fins->o]; - while (ref > lim) { - IRIns *load = IR(ref); - if (load->op1 == xref) - return ref; /* Load forwarding. */ - ref = load->prev; - } - return 0; /* Conflict or no match. */ -} - -/* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */ -static TRef fwd_aload_reassoc(jit_State *J) -{ - IRIns *irx = IR(fins->op1); - IRIns *key = IR(irx->op2); - if (key->o == IR_ADD && irref_isk(key->op2)) { - IRIns *add2 = IR(key->op1); - if (add2->o == IR_ADD && irref_isk(add2->op2) && - IR(key->op2)->i == -IR(add2->op2)->i) { - IRRef ref = J->chain[IR_AREF]; - IRRef lim = add2->op1; - if (irx->op1 > lim) lim = irx->op1; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == irx->op1 && ir->op2 == add2->op1) - return fwd_ahload(J, ref); - ref = ir->prev; - } - } - } - return 0; -} - -/* ALOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J) -{ - IRRef ref; - if ((ref = fwd_ahload(J, fins->op1)) || - (ref = fwd_aload_reassoc(J))) - return ref; - return EMITFOLD; -} - -/* HLOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J) -{ - IRRef ref = fwd_ahload(J, fins->op1); - if (ref) - return ref; - return EMITFOLD; -} - -/* HREFK forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J) -{ - IRRef tab = fleft->op1; - IRRef ref = J->chain[IR_NEWREF]; - while (ref > tab) { - IRIns *newref = IR(ref); - if (tab == newref->op1) { - if (fright->op1 == newref->op2 && fwd_aa_tab_clear(J, ref, tab)) - return ref; /* Forward from NEWREF. */ - else - goto docse; - } else if (aa_table(J, tab, newref->op1) != ALIAS_NO) { - goto docse; - } - ref = newref->prev; - } - /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */ - if (IR(tab)->o == IR_TDUP && fwd_aa_tab_clear(J, tab, tab)) - fins->t.irt &= ~IRT_GUARD; /* Drop HREFK guard. */ -docse: - return CSEFOLD; -} - -/* Check whether HREF of TNEW/TDUP can be folded to niltv. */ -int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J) -{ - IRRef lim = fins->op1; /* Search limit. */ - IRRef ref; - - /* The key for an ASTORE may end up in the hash part after a NEWREF. */ - if (irt_isnum(fright->t) && J->chain[IR_NEWREF] > lim) { - ref = J->chain[IR_ASTORE]; - while (ref > lim) { - if (ref < J->chain[IR_NEWREF]) - return 0; /* Conflict. */ - ref = IR(ref)->prev; - } - } - - /* Search for conflicting stores. */ - ref = J->chain[IR_HSTORE]; - while (ref > lim) { - IRIns *store = IR(ref); - if (aa_ahref(J, fins, IR(store->op1)) != ALIAS_NO) - return 0; /* Conflict. */ - ref = store->prev; - } - - return 1; /* No conflict. Can fold to niltv. */ -} - -/* ASTORE/HSTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J) -{ - IRRef xref = fins->op1; /* xREF reference. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRIns *xr = IR(xref); - IRRef1 *refp = &J->chain[fins->o]; - IRRef ref = *refp; - while (ref > xref) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_ahref(J, xr, IR(store->op1))) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: /* Store to MAYBE the same location. */ - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: /* Store to the same location. */ - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards (includes conflicting loads). - ** Note that lj_tab_keyindex and lj_vm_next don't need guards, - ** since they are followed by at least one guarded VLOAD. - */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t) || ir->o == IR_ALEN) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - lj_ir_nop(store); - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* ALEN forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_alen(jit_State *J) -{ - IRRef tab = fins->op1; /* Table reference. */ - IRRef lim = tab; /* Search limit. */ - IRRef ref; - - /* Search for conflicting HSTORE with numeric key. */ - ref = J->chain[IR_HSTORE]; - while (ref > lim) { - IRIns *store = IR(ref); - IRIns *href = IR(store->op1); - IRIns *key = IR(href->op2); - if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) { - lim = ref; /* Conflicting store found, limits search for ALEN. */ - break; - } - ref = store->prev; - } - - /* Try to find a matching ALEN. */ - ref = J->chain[IR_ALEN]; - while (ref > lim) { - /* CSE for ALEN only depends on the table, not the hint. */ - if (IR(ref)->op1 == tab) { - IRRef sref; - - /* Search for aliasing table.clear. */ - if (!fwd_aa_tab_clear(J, ref, tab)) - break; - - /* Search for hint-forwarding or conflicting store. */ - sref = J->chain[IR_ASTORE]; - while (sref > ref) { - IRIns *store = IR(sref); - IRIns *aref = IR(store->op1); - IRIns *fref = IR(aref->op1); - if (tab == fref->op1) { /* ASTORE to the same table. */ - /* Detect t[#t+1] = x idiom for push. */ - IRIns *idx = IR(aref->op2); - if (!irt_isnil(store->t) && - idx->o == IR_ADD && idx->op1 == ref && - IR(idx->op2)->o == IR_KINT && IR(idx->op2)->i == 1) { - /* Note: this requires an extra PHI check in loop unroll. */ - fins->op2 = aref->op2; /* Set ALEN hint. */ - } - goto doemit; /* Conflicting store, possibly giving a hint. */ - } else if (aa_table(J, tab, fref->op1) != ALIAS_NO) { - goto doemit; /* Conflicting store. */ - } - sref = store->prev; - } - - return ref; /* Plain ALEN forwarding. */ - } - ref = IR(ref)->prev; - } -doemit: - return EMITFOLD; -} - -/* -- ULOAD forwarding ---------------------------------------------------- */ - -/* The current alias analysis for upvalues is very simplistic. It only -** disambiguates between the unique upvalues of the same function. -** This is good enough for now, since most upvalues are read-only. -** -** A more precise analysis would be feasible with the help of the parser: -** generate a unique key for every upvalue, even across all prototypes. -** Lacking a realistic use-case, it's unclear whether this is beneficial. -*/ -static AliasRet aa_uref(IRIns *refa, IRIns *refb) -{ - if (refa->o != refb->o) - return ALIAS_NO; /* Different UREFx type. */ - if (refa->op1 == refb->op1) { /* Same function. */ - if (refa->op2 == refb->op2) - return ALIAS_MUST; /* Same function, same upvalue idx. */ - else - return ALIAS_NO; /* Same function, different upvalue idx. */ - } else { /* Different functions, check disambiguation hash values. */ - if (((refa->op2 ^ refb->op2) & 0xff)) - return ALIAS_NO; /* Upvalues with different hash values cannot alias. */ - else - return ALIAS_MAY; /* No conclusion can be drawn for same hash value. */ - } -} - -/* ULOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J) -{ - IRRef uref = fins->op1; - IRRef lim = REF_BASE; /* Search limit. */ - IRIns *xr = IR(uref); - IRRef ref; - - /* Search for conflicting stores. */ - ref = J->chain[IR_USTORE]; - while (ref > lim) { - IRIns *store = IR(ref); - switch (aa_uref(xr, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - ref = J->chain[IR_ULOAD]; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == uref || - (IR(ir->op1)->op12 == IR(uref)->op12 && IR(ir->op1)->o == IR(uref)->o)) - return ref; /* Match for identical or equal UREFx (non-CSEable UREFO). */ - ref = ir->prev; - } - return lj_ir_emit(J); -} - -/* USTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J) -{ - IRRef xref = fins->op1; /* xREF reference. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRIns *xr = IR(xref); - IRRef1 *refp = &J->chain[IR_USTORE]; - IRRef ref = *refp; - while (ref > xref) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_uref(xr, IR(store->op1))) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: /* Store to MAYBE the same location. */ - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: /* Store to the same location. */ - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards (includes conflicting loads). */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t)) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - lj_ir_nop(store); - if (ref+1 < J->cur.nins && - store[1].o == IR_OBAR && store[1].op1 == xref) { - IRRef1 *bp = &J->chain[IR_OBAR]; - IRIns *obar; - for (obar = IR(*bp); *bp > ref+1; obar = IR(*bp)) - bp = &obar->prev; - /* Remove OBAR, too. */ - *bp = obar->prev; - lj_ir_nop(obar); - } - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* -- FLOAD forwarding and FSTORE elimination ----------------------------- */ - -/* Alias analysis for field access. -** Field loads are cheap and field stores are rare. -** Simple disambiguation based on field types is good enough. -*/ -static AliasRet aa_fref(jit_State *J, IRIns *refa, IRIns *refb) -{ - if (refa->op2 != refb->op2) - return ALIAS_NO; /* Different fields. */ - if (refa->op1 == refb->op1) - return ALIAS_MUST; /* Same field, same object. */ - else if (refa->op2 >= IRFL_TAB_META && refa->op2 <= IRFL_TAB_NOMM) - return aa_table(J, refa->op1, refb->op1); /* Disambiguate tables. */ - else - return ALIAS_MAY; /* Same field, possibly different object. */ -} - -/* Only the loads for mutable fields end up here (see FOLD). */ -TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J) -{ - IRRef oref = fins->op1; /* Object reference. */ - IRRef fid = fins->op2; /* Field ID. */ - IRRef lim = oref; /* Search limit. */ - IRRef ref; - - /* Search for conflicting stores. */ - ref = J->chain[IR_FSTORE]; - while (ref > oref) { - IRIns *store = IR(ref); - switch (aa_fref(J, fins, IR(store->op1))) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - - /* No conflicting store: const-fold field loads from allocations. */ - if (fid == IRFL_TAB_META) { - IRIns *ir = IR(oref); - if (ir->o == IR_TNEW || ir->o == IR_TDUP) - return lj_ir_knull(J, IRT_TAB); - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - return lj_opt_cselim(J, lim); -} - -/* FSTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J) -{ - IRRef fref = fins->op1; /* FREF reference. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRIns *xr = IR(fref); - IRRef1 *refp = &J->chain[IR_FSTORE]; - IRRef ref = *refp; - while (ref > fref) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_fref(J, xr, IR(store->op1))) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: - if (store->op2 == val && - !(xr->op2 >= IRFL_SBUF_W && xr->op2 <= IRFL_SBUF_R)) - return DROPFOLD; /* Same value: drop the new store. */ - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards or conflicting loads. */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t) || (ir->o == IR_FLOAD && ir->op2 == xr->op2)) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - lj_ir_nop(store); - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* Check whether there's no aliasing buffer op between IRFL_SBUF_*. */ -int LJ_FASTCALL lj_opt_fwd_sbuf(jit_State *J, IRRef lim) -{ - IRRef ref; - if (J->chain[IR_BUFPUT] > lim) - return 0; /* Conflict. */ - ref = J->chain[IR_CALLS]; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op2 >= IRCALL_lj_strfmt_putint && ir->op2 < IRCALL_lj_buf_tostr) - return 0; /* Conflict. */ - ref = ir->prev; - } - ref = J->chain[IR_CALLL]; - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op2 >= IRCALL_lj_strfmt_putint && ir->op2 < IRCALL_lj_buf_tostr) - return 0; /* Conflict. */ - ref = ir->prev; - } - return 1; /* No conflict. Can safely FOLD/CSE. */ -} - -/* -- XLOAD forwarding and XSTORE elimination ----------------------------- */ - -/* Find cdata allocation for a reference (if any). */ -static IRIns *aa_findcnew(jit_State *J, IRIns *ir) -{ - while (ir->o == IR_ADD) { - if (!irref_isk(ir->op1)) { - IRIns *ir1 = aa_findcnew(J, IR(ir->op1)); /* Left-recursion. */ - if (ir1) return ir1; - } - if (irref_isk(ir->op2)) return NULL; - ir = IR(ir->op2); /* Flatten right-recursion. */ - } - return ir->o == IR_CNEW ? ir : NULL; -} - -/* Alias analysis for two cdata allocations. */ -static AliasRet aa_cnew(jit_State *J, IRIns *refa, IRIns *refb) -{ - IRIns *cnewa = aa_findcnew(J, refa); - IRIns *cnewb = aa_findcnew(J, refb); - if (cnewa == cnewb) - return ALIAS_MAY; /* Same allocation or neither is an allocation. */ - if (cnewa && cnewb) - return ALIAS_NO; /* Two different allocations never alias. */ - if (cnewb) { cnewa = cnewb; refb = refa; } - return aa_escape(J, cnewa, refb); -} - -/* Alias analysis for XLOAD/XSTORE. */ -static AliasRet aa_xref(jit_State *J, IRIns *refa, IRIns *xa, IRIns *xb) -{ - ptrdiff_t ofsa = 0, ofsb = 0; - IRIns *refb = IR(xb->op1); - IRIns *basea = refa, *baseb = refb; - if (refa == refb && irt_sametype(xa->t, xb->t)) - return ALIAS_MUST; /* Shortcut for same refs with identical type. */ - /* Offset-based disambiguation. */ - if (refa->o == IR_ADD && irref_isk(refa->op2)) { - IRIns *irk = IR(refa->op2); - basea = IR(refa->op1); - ofsa = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : - (ptrdiff_t)irk->i; - } - if (refb->o == IR_ADD && irref_isk(refb->op2)) { - IRIns *irk = IR(refb->op2); - baseb = IR(refb->op1); - ofsb = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : - (ptrdiff_t)irk->i; - } - /* Treat constified pointers like base vs. base+offset. */ - if (basea->o == IR_KPTR && baseb->o == IR_KPTR) { - ofsb += (char *)ir_kptr(baseb) - (char *)ir_kptr(basea); - baseb = basea; - } - /* This implements (very) strict aliasing rules. - ** Different types do NOT alias, except for differences in signedness. - ** Type punning through unions is allowed (but forces a reload). - */ - if (basea == baseb) { - ptrdiff_t sza = irt_size(xa->t), szb = irt_size(xb->t); - if (ofsa == ofsb) { - if (sza == szb && irt_isfp(xa->t) == irt_isfp(xb->t)) - return ALIAS_MUST; /* Same-sized, same-kind. May need to convert. */ - } else if (ofsa + sza <= ofsb || ofsb + szb <= ofsa) { - return ALIAS_NO; /* Non-overlapping base+-o1 vs. base+-o2. */ - } - /* NYI: extract, extend or reinterpret bits (int <-> fp). */ - return ALIAS_MAY; /* Overlapping or type punning: force reload. */ - } - if (!irt_sametype(xa->t, xb->t) && - !(irt_typerange(xa->t, IRT_I8, IRT_U64) && - ((xa->t.irt - IRT_I8) ^ (xb->t.irt - IRT_I8)) == 1)) - return ALIAS_NO; - /* NYI: structural disambiguation. */ - return aa_cnew(J, basea, baseb); /* Try to disambiguate allocations. */ -} - -/* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */ -static IRRef reassoc_trycse(jit_State *J, IROp op, IRRef op1, IRRef op2) -{ - IRRef ref = J->chain[op]; - IRRef lim = op1; - if (op2 > lim) { lim = op2; op2 = op1; op1 = lim; } - while (ref > lim) { - IRIns *ir = IR(ref); - if (ir->op1 == op1 && ir->op2 == op2) - return ref; - ref = ir->prev; - } - return 0; -} - -/* Reassociate index references. */ -static IRRef reassoc_xref(jit_State *J, IRIns *ir) -{ - ptrdiff_t ofs = 0; - if (ir->o == IR_ADD && irref_isk(ir->op2)) { /* Get constant offset. */ - IRIns *irk = IR(ir->op2); - ofs = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : - (ptrdiff_t)irk->i; - ir = IR(ir->op1); - } - if (ir->o == IR_ADD) { /* Add of base + index. */ - /* Index ref > base ref for loop-carried dependences. Only check op1. */ - IRIns *ir2, *ir1 = IR(ir->op1); - int32_t shift = 0; - IRRef idxref; - /* Determine index shifts. Don't bother with IR_MUL here. */ - if (ir1->o == IR_BSHL && irref_isk(ir1->op2)) - shift = IR(ir1->op2)->i; - else if (ir1->o == IR_ADD && ir1->op1 == ir1->op2) - shift = 1; - else - ir1 = ir; - ir2 = IR(ir1->op1); - /* A non-reassociated add. Must be a loop-carried dependence. */ - if (ir2->o == IR_ADD && irt_isint(ir2->t) && irref_isk(ir2->op2)) - ofs += (ptrdiff_t)IR(ir2->op2)->i << shift; - else - return 0; - idxref = ir2->op1; - /* Try to CSE the reassociated chain. Give up if not found. */ - if (ir1 != ir && - !(idxref = reassoc_trycse(J, ir1->o, idxref, - ir1->o == IR_BSHL ? ir1->op2 : idxref))) - return 0; - if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, ir->op2))) - return 0; - if (ofs != 0) { - IRRef refk = tref_ref(lj_ir_kintp(J, ofs)); - if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, refk))) - return 0; - } - return idxref; /* Success, found a reassociated index reference. Phew. */ - } - return 0; /* Failure. */ -} - -/* XLOAD forwarding. */ -TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J) -{ - IRRef xref = fins->op1; - IRIns *xr = IR(xref); - IRRef lim = xref; /* Search limit. */ - IRRef ref; - - if ((fins->op2 & IRXLOAD_READONLY)) - goto cselim; - if ((fins->op2 & IRXLOAD_VOLATILE)) - goto doemit; - - /* Search for conflicting stores. */ - ref = J->chain[IR_XSTORE]; -retry: - if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS]; - if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR]; - while (ref > lim) { - IRIns *store = IR(ref); - switch (aa_xref(J, xr, fins, store)) { - case ALIAS_NO: break; /* Continue searching. */ - case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ - case ALIAS_MUST: - /* Emit conversion if the loaded type doesn't match the forwarded type. */ - if (!irt_sametype(fins->t, IR(store->op2)->t)) { - IRType dt = irt_type(fins->t), st = irt_type(IR(store->op2)->t); - if (dt == IRT_I8 || dt == IRT_I16) { /* Trunc + sign-extend. */ - st = dt | IRCONV_SEXT; - dt = IRT_INT; - } else if (dt == IRT_U8 || dt == IRT_U16) { /* Trunc + zero-extend. */ - st = dt; - dt = IRT_INT; - } - fins->ot = IRT(IR_CONV, dt); - fins->op1 = store->op2; - fins->op2 = (dt<<5)|st; - return RETRYFOLD; - } - return store->op2; /* Store forwarding. */ - } - ref = store->prev; - } - -cselim: - /* Try to find a matching load. Below the conflicting store, if any. */ - ref = J->chain[IR_XLOAD]; - while (ref > lim) { - /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */ - if (IR(ref)->op1 == xref && irt_sametype(IR(ref)->t, fins->t)) - return ref; - ref = IR(ref)->prev; - } - - /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */ - if (!(fins->op2 & IRXLOAD_READONLY) && J->chain[IR_LOOP] && - xref == fins->op1 && (xref = reassoc_xref(J, xr)) != 0) { - ref = J->chain[IR_XSTORE]; - while (ref > lim) /* Skip stores that have already been checked. */ - ref = IR(ref)->prev; - lim = xref; - xr = IR(xref); - goto retry; /* Retry with the reassociated reference. */ - } -doemit: - return EMITFOLD; -} - -/* XSTORE elimination. */ -TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J) -{ - IRRef xref = fins->op1; - IRIns *xr = IR(xref); - IRRef lim = xref; /* Search limit. */ - IRRef val = fins->op2; /* Stored value reference. */ - IRRef1 *refp = &J->chain[IR_XSTORE]; - IRRef ref = *refp; - if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS]; - if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR]; - if (J->chain[IR_XSNEW] > lim) lim = J->chain[IR_XSNEW]; - while (ref > lim) { /* Search for redundant or conflicting stores. */ - IRIns *store = IR(ref); - switch (aa_xref(J, xr, fins, store)) { - case ALIAS_NO: - break; /* Continue searching. */ - case ALIAS_MAY: - if (store->op2 != val) /* Conflict if the value is different. */ - goto doemit; - break; /* Otherwise continue searching. */ - case ALIAS_MUST: - if (store->op2 == val) /* Same value: drop the new store. */ - return DROPFOLD; - /* Different value: try to eliminate the redundant store. */ - if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ - IRIns *ir; - /* Check for any intervening guards or any XLOADs (no AA performed). */ - for (ir = IR(J->cur.nins-1); ir > store; ir--) - if (irt_isguard(ir->t) || ir->o == IR_XLOAD) - goto doemit; /* No elimination possible. */ - /* Remove redundant store from chain and replace with NOP. */ - *refp = store->prev; - lj_ir_nop(store); - /* Now emit the new store instead. */ - } - goto doemit; - } - ref = *(refp = &store->prev); - } -doemit: - return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ -} - -/* -- ASTORE/HSTORE previous type analysis -------------------------------- */ - -/* Check whether the previous value for a table store is non-nil. -** This can be derived either from a previous store or from a previous -** load (because all loads from tables perform a type check). -** -** The result of the analysis can be used to avoid the metatable check -** and the guard against HREF returning niltv. Both of these are cheap, -** so let's not spend too much effort on the analysis. -** -** A result of 1 is exact: previous value CANNOT be nil. -** A result of 0 is inexact: previous value MAY be nil. -*/ -int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref) -{ - /* First check stores. */ - IRRef ref = J->chain[loadop+IRDELTA_L2S]; - while (ref > xref) { - IRIns *store = IR(ref); - if (store->op1 == xref) { /* Same xREF. */ - /* A nil store MAY alias, but a non-nil store MUST alias. */ - return !irt_isnil(store->t); - } else if (irt_isnil(store->t)) { /* Must check any nil store. */ - IRRef skref = IR(store->op1)->op2; - IRRef xkref = IR(xref)->op2; - /* Same key type MAY alias. Need ALOAD check due to multiple int types. */ - if (loadop == IR_ALOAD || irt_sametype(IR(skref)->t, IR(xkref)->t)) { - if (skref == xkref || !irref_isk(skref) || !irref_isk(xkref)) - return 0; /* A nil store with same const key or var key MAY alias. */ - /* Different const keys CANNOT alias. */ - } /* Different key types CANNOT alias. */ - } /* Other non-nil stores MAY alias. */ - ref = store->prev; - } - - /* Check loads since nothing could be derived from stores. */ - ref = J->chain[loadop]; - while (ref > xref) { - IRIns *load = IR(ref); - if (load->op1 == xref) { /* Same xREF. */ - /* A nil load MAY alias, but a non-nil load MUST alias. */ - return !irt_isnil(load->t); - } /* Other non-nil loads MAY alias. */ - ref = load->prev; - } - return 0; /* Nothing derived at all, previous value MAY be nil. */ -} - -/* ------------------------------------------------------------------------ */ - -#undef IR -#undef fins -#undef fleft -#undef fright - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_narrow.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_narrow.c deleted file mode 100644 index 586f1bc..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_narrow.c +++ /dev/null @@ -1,622 +0,0 @@ -/* -** NARROW: Narrowing of numbers to integers (double to int32_t). -** STRIPOV: Stripping of overflow checks. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_narrow_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_vm.h" -#include "lj_strscan.h" - -/* Rationale for narrowing optimizations: -** -** Lua has only a single number type and this is a FP double by default. -** Narrowing doubles to integers does not pay off for the interpreter on a -** current-generation x86/x64 machine. Most FP operations need the same -** amount of execution resources as their integer counterparts, except -** with slightly longer latencies. Longer latencies are a non-issue for -** the interpreter, since they are usually hidden by other overhead. -** -** The total CPU execution bandwidth is the sum of the bandwidth of the FP -** and the integer units, because they execute in parallel. The FP units -** have an equal or higher bandwidth than the integer units. Not using -** them means losing execution bandwidth. Moving work away from them to -** the already quite busy integer units is a losing proposition. -** -** The situation for JIT-compiled code is a bit different: the higher code -** density makes the extra latencies much more visible. Tight loops expose -** the latencies for updating the induction variables. Array indexing -** requires narrowing conversions with high latencies and additional -** guards (to check that the index is really an integer). And many common -** optimizations only work on integers. -** -** One solution would be speculative, eager narrowing of all number loads. -** This causes many problems, like losing -0 or the need to resolve type -** mismatches between traces. It also effectively forces the integer type -** to have overflow-checking semantics. This impedes many basic -** optimizations and requires adding overflow checks to all integer -** arithmetic operations (whereas FP arithmetics can do without). -** -** Always replacing an FP op with an integer op plus an overflow check is -** counter-productive on a current-generation super-scalar CPU. Although -** the overflow check branches are highly predictable, they will clog the -** execution port for the branch unit and tie up reorder buffers. This is -** turning a pure data-flow dependency into a different data-flow -** dependency (with slightly lower latency) *plus* a control dependency. -** In general, you don't want to do this since latencies due to data-flow -** dependencies can be well hidden by out-of-order execution. -** -** A better solution is to keep all numbers as FP values and only narrow -** when it's beneficial to do so. LuaJIT uses predictive narrowing for -** induction variables and demand-driven narrowing for index expressions, -** integer arguments and bit operations. Additionally it can eliminate or -** hoist most of the resulting overflow checks. Regular arithmetic -** computations are never narrowed to integers. -** -** The integer type in the IR has convenient wrap-around semantics and -** ignores overflow. Extra operations have been added for -** overflow-checking arithmetic (ADDOV/SUBOV) instead of an extra type. -** Apart from reducing overall complexity of the compiler, this also -** nicely solves the problem where you want to apply algebraic -** simplifications to ADD, but not to ADDOV. And the x86/x64 assembler can -** use lea instead of an add for integer ADD, but not for ADDOV (lea does -** not affect the flags, but it helps to avoid register moves). -** -** -** All of the above has to be reconsidered for architectures with slow FP -** operations or without a hardware FPU. The dual-number mode of LuaJIT -** addresses this issue. Arithmetic operations are performed on integers -** as far as possible and overflow checks are added as needed. -** -** This implies that narrowing for integer arguments and bit operations -** should also strip overflow checks, e.g. replace ADDOV with ADD. The -** original overflow guards are weak and can be eliminated by DCE, if -** there's no other use. -** -** A slight twist is that it's usually beneficial to use overflow-checked -** integer arithmetics if all inputs are already integers. This is the only -** change that affects the single-number mode, too. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) -#define fins (&J->fold.ins) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- Elimination of narrowing type conversions --------------------------- */ - -/* Narrowing of index expressions and bit operations is demand-driven. The -** trace recorder emits a narrowing type conversion (CONV.int.num or TOBIT) -** in all of these cases (e.g. array indexing or string indexing). FOLD -** already takes care of eliminating simple redundant conversions like -** CONV.int.num(CONV.num.int(x)) ==> x. -** -** But the surrounding code is FP-heavy and arithmetic operations are -** performed on FP numbers (for the single-number mode). Consider a common -** example such as 'x=t[i+1]', with 'i' already an integer (due to induction -** variable narrowing). The index expression would be recorded as -** CONV.int.num(ADD(CONV.num.int(i), 1)) -** which is clearly suboptimal. -** -** One can do better by recursively backpropagating the narrowing type -** conversion across FP arithmetic operations. This turns FP ops into -** their corresponding integer counterparts. Depending on the semantics of -** the conversion they also need to check for overflow. Currently only ADD -** and SUB are supported. -** -** The above example can be rewritten as -** ADDOV(CONV.int.num(CONV.num.int(i)), 1) -** and then into ADDOV(i, 1) after folding of the conversions. The original -** FP ops remain in the IR and are eliminated by DCE since all references to -** them are gone. -** -** [In dual-number mode the trace recorder already emits ADDOV etc., but -** this can be further reduced. See below.] -** -** Special care has to be taken to avoid narrowing across an operation -** which is potentially operating on non-integral operands. One obvious -** case is when an expression contains a non-integral constant, but ends -** up as an integer index at runtime (like t[x+1.5] with x=0.5). -** -** Operations with two non-constant operands illustrate a similar problem -** (like t[a+b] with a=1.5 and b=2.5). Backpropagation has to stop there, -** unless it can be proven that either operand is integral (e.g. by CSEing -** a previous conversion). As a not-so-obvious corollary this logic also -** applies for a whole expression tree (e.g. t[(a+1)+(b+1)]). -** -** Correctness of the transformation is guaranteed by avoiding to expand -** the tree by adding more conversions than the one we would need to emit -** if not backpropagating. TOBIT employs a more optimistic rule, because -** the conversion has special semantics, designed to make the life of the -** compiler writer easier. ;-) -** -** Using on-the-fly backpropagation of an expression tree doesn't work -** because it's unknown whether the transform is correct until the end. -** This either requires IR rollback and cache invalidation for every -** subtree or a two-pass algorithm. The former didn't work out too well, -** so the code now combines a recursive collector with a stack-based -** emitter. -** -** [A recursive backpropagation algorithm with backtracking, employing -** skip-list lookup and round-robin caching, emitting stack operations -** on-the-fly for a stack-based interpreter -- and all of that in a meager -** kilobyte? Yep, compilers are a great treasure chest. Throw away your -** textbooks and read the codebase of a compiler today!] -** -** There's another optimization opportunity for array indexing: it's -** always accompanied by an array bounds-check. The outermost overflow -** check may be delegated to the ABC operation. This works because ABC is -** an unsigned comparison and wrap-around due to overflow creates negative -** numbers. -** -** But this optimization is only valid for constants that cannot overflow -** an int32_t into the range of valid array indexes [0..2^27+1). A check -** for +-2^30 is safe since -2^31 - 2^30 wraps to 2^30 and 2^31-1 + 2^30 -** wraps to -2^30-1. -** -** It's also good enough in practice, since e.g. t[i+1] or t[i-10] are -** quite common. So the above example finally ends up as ADD(i, 1)! -** -** Later on, the assembler is able to fuse the whole array reference and -** the ADD into the memory operands of loads and other instructions. This -** is why LuaJIT is able to generate very pretty (and fast) machine code -** for array indexing. And that, my dear, concludes another story about -** one of the hidden secrets of LuaJIT ... -*/ - -/* Maximum backpropagation depth and maximum stack size. */ -#define NARROW_MAX_BACKPROP 100 -#define NARROW_MAX_STACK 256 - -/* The stack machine has a 32 bit instruction format: [IROpT | IRRef1] -** The lower 16 bits hold a reference (or 0). The upper 16 bits hold -** the IR opcode + type or one of the following special opcodes: -*/ -enum { - NARROW_REF, /* Push ref. */ - NARROW_CONV, /* Push conversion of ref. */ - NARROW_SEXT, /* Push sign-extension of ref. */ - NARROW_INT /* Push KINT ref. The next code holds an int32_t. */ -}; - -typedef uint32_t NarrowIns; - -#define NARROWINS(op, ref) (((op) << 16) + (ref)) -#define narrow_op(ins) ((IROpT)((ins) >> 16)) -#define narrow_ref(ins) ((IRRef1)(ins)) - -/* Context used for narrowing of type conversions. */ -typedef struct NarrowConv { - jit_State *J; /* JIT compiler state. */ - NarrowIns *sp; /* Current stack pointer. */ - NarrowIns *maxsp; /* Maximum stack pointer minus redzone. */ - IRRef mode; /* Conversion mode (IRCONV_*). */ - IRType t; /* Destination type: IRT_INT or IRT_I64. */ - NarrowIns stack[NARROW_MAX_STACK]; /* Stack holding stack-machine code. */ -} NarrowConv; - -/* Lookup a reference in the backpropagation cache. */ -static BPropEntry *narrow_bpc_get(jit_State *J, IRRef1 key, IRRef mode) -{ - ptrdiff_t i; - for (i = 0; i < BPROP_SLOTS; i++) { - BPropEntry *bp = &J->bpropcache[i]; - /* Stronger checks are ok, too. */ - if (bp->key == key && bp->mode >= mode && - ((bp->mode ^ mode) & IRCONV_MODEMASK) == 0) - return bp; - } - return NULL; -} - -/* Add an entry to the backpropagation cache. */ -static void narrow_bpc_set(jit_State *J, IRRef1 key, IRRef1 val, IRRef mode) -{ - uint32_t slot = J->bpropslot; - BPropEntry *bp = &J->bpropcache[slot]; - J->bpropslot = (slot + 1) & (BPROP_SLOTS-1); - bp->key = key; - bp->val = val; - bp->mode = mode; -} - -/* Backpropagate overflow stripping. */ -static void narrow_stripov_backprop(NarrowConv *nc, IRRef ref, int depth) -{ - jit_State *J = nc->J; - IRIns *ir = IR(ref); - if (ir->o == IR_ADDOV || ir->o == IR_SUBOV || - (ir->o == IR_MULOV && (nc->mode & IRCONV_CONVMASK) == IRCONV_ANY)) { - BPropEntry *bp = narrow_bpc_get(nc->J, ref, IRCONV_TOBIT); - if (bp) { - ref = bp->val; - } else if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) { - NarrowIns *savesp = nc->sp; - narrow_stripov_backprop(nc, ir->op1, depth); - if (nc->sp < nc->maxsp) { - narrow_stripov_backprop(nc, ir->op2, depth); - if (nc->sp < nc->maxsp) { - *nc->sp++ = NARROWINS(IRT(ir->o - IR_ADDOV + IR_ADD, IRT_INT), ref); - return; - } - } - nc->sp = savesp; /* Path too deep, need to backtrack. */ - } - } - *nc->sp++ = NARROWINS(NARROW_REF, ref); -} - -/* Backpropagate narrowing conversion. Return number of needed conversions. */ -static int narrow_conv_backprop(NarrowConv *nc, IRRef ref, int depth) -{ - jit_State *J = nc->J; - IRIns *ir = IR(ref); - IRRef cref; - - if (nc->sp >= nc->maxsp) return 10; /* Path too deep. */ - - /* Check the easy cases first. */ - if (ir->o == IR_CONV && (ir->op2 & IRCONV_SRCMASK) == IRT_INT) { - if ((nc->mode & IRCONV_CONVMASK) <= IRCONV_ANY) - narrow_stripov_backprop(nc, ir->op1, depth+1); - else - *nc->sp++ = NARROWINS(NARROW_REF, ir->op1); /* Undo conversion. */ - if (nc->t == IRT_I64) - *nc->sp++ = NARROWINS(NARROW_SEXT, 0); /* Sign-extend integer. */ - return 0; - } else if (ir->o == IR_KNUM) { /* Narrow FP constant. */ - lua_Number n = ir_knum(ir)->n; - if ((nc->mode & IRCONV_CONVMASK) == IRCONV_TOBIT) { - /* Allows a wider range of constants. */ - int64_t k64 = (int64_t)n; - if (n == (lua_Number)k64) { /* Only if const doesn't lose precision. */ - *nc->sp++ = NARROWINS(NARROW_INT, 0); - *nc->sp++ = (NarrowIns)k64; /* But always truncate to 32 bits. */ - return 0; - } - } else { - int32_t k = lj_num2int(n); - /* Only if constant is a small integer. */ - if (checki16(k) && n == (lua_Number)k) { - *nc->sp++ = NARROWINS(NARROW_INT, 0); - *nc->sp++ = (NarrowIns)k; - return 0; - } - } - return 10; /* Never narrow other FP constants (this is rare). */ - } - - /* Try to CSE the conversion. Stronger checks are ok, too. */ - cref = J->chain[fins->o]; - while (cref > ref) { - IRIns *cr = IR(cref); - if (cr->op1 == ref && - (fins->o == IR_TOBIT || - ((cr->op2 & IRCONV_MODEMASK) == (nc->mode & IRCONV_MODEMASK) && - irt_isguard(cr->t) >= irt_isguard(fins->t)))) { - *nc->sp++ = NARROWINS(NARROW_REF, cref); - return 0; /* Already there, no additional conversion needed. */ - } - cref = cr->prev; - } - - /* Backpropagate across ADD/SUB. */ - if (ir->o == IR_ADD || ir->o == IR_SUB) { - /* Try cache lookup first. */ - IRRef mode = nc->mode; - BPropEntry *bp; - /* Inner conversions need a stronger check. */ - if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX && depth > 0) - mode += IRCONV_CHECK-IRCONV_INDEX; - bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode); - if (bp) { - *nc->sp++ = NARROWINS(NARROW_REF, bp->val); - return 0; - } else if (nc->t == IRT_I64) { - /* Try sign-extending from an existing (checked) conversion to int. */ - mode = (IRT_INT<<5)|IRT_NUM|IRCONV_INDEX; - bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode); - if (bp) { - *nc->sp++ = NARROWINS(NARROW_REF, bp->val); - *nc->sp++ = NARROWINS(NARROW_SEXT, 0); - return 0; - } - } - if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) { - NarrowIns *savesp = nc->sp; - int count = narrow_conv_backprop(nc, ir->op1, depth); - count += narrow_conv_backprop(nc, ir->op2, depth); - if (count <= 1) { /* Limit total number of conversions. */ - *nc->sp++ = NARROWINS(IRT(ir->o, nc->t), ref); - return count; - } - nc->sp = savesp; /* Too many conversions, need to backtrack. */ - } - } - - /* Otherwise add a conversion. */ - *nc->sp++ = NARROWINS(NARROW_CONV, ref); - return 1; -} - -/* Emit the conversions collected during backpropagation. */ -static IRRef narrow_conv_emit(jit_State *J, NarrowConv *nc) -{ - /* The fins fields must be saved now -- emitir() overwrites them. */ - IROpT guardot = irt_isguard(fins->t) ? IRTG(IR_ADDOV-IR_ADD, 0) : 0; - IROpT convot = fins->ot; - IRRef1 convop2 = fins->op2; - NarrowIns *next = nc->stack; /* List of instructions from backpropagation. */ - NarrowIns *last = nc->sp; - NarrowIns *sp = nc->stack; /* Recycle the stack to store operands. */ - while (next < last) { /* Simple stack machine to process the ins. list. */ - NarrowIns ref = *next++; - IROpT op = narrow_op(ref); - if (op == NARROW_REF) { - *sp++ = ref; - } else if (op == NARROW_CONV) { - *sp++ = emitir_raw(convot, ref, convop2); /* Raw emit avoids a loop. */ - } else if (op == NARROW_SEXT) { - lj_assertJ(sp >= nc->stack+1, "stack underflow"); - sp[-1] = emitir(IRT(IR_CONV, IRT_I64), sp[-1], - (IRT_I64<<5)|IRT_INT|IRCONV_SEXT); - } else if (op == NARROW_INT) { - lj_assertJ(next < last, "missing arg to NARROW_INT"); - *sp++ = nc->t == IRT_I64 ? - lj_ir_kint64(J, (int64_t)(int32_t)*next++) : - lj_ir_kint(J, *next++); - } else { /* Regular IROpT. Pops two operands and pushes one result. */ - IRRef mode = nc->mode; - lj_assertJ(sp >= nc->stack+2, "stack underflow"); - sp--; - /* Omit some overflow checks for array indexing. See comments above. */ - if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX) { - if (next == last && irref_isk(narrow_ref(sp[0])) && - (uint32_t)IR(narrow_ref(sp[0]))->i + 0x40000000u < 0x80000000u) - guardot = 0; - else /* Otherwise cache a stronger check. */ - mode += IRCONV_CHECK-IRCONV_INDEX; - } - sp[-1] = emitir(op+guardot, sp[-1], sp[0]); - /* Add to cache. */ - if (narrow_ref(ref)) - narrow_bpc_set(J, narrow_ref(ref), narrow_ref(sp[-1]), mode); - } - } - lj_assertJ(sp == nc->stack+1, "stack misalignment"); - return nc->stack[0]; -} - -/* Narrow a type conversion of an arithmetic operation. */ -TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J) -{ - if ((J->flags & JIT_F_OPT_NARROW)) { - NarrowConv nc; - nc.J = J; - nc.sp = nc.stack; - nc.maxsp = &nc.stack[NARROW_MAX_STACK-4]; - nc.t = irt_type(fins->t); - if (fins->o == IR_TOBIT) { - nc.mode = IRCONV_TOBIT; /* Used only in the backpropagation cache. */ - } else { - nc.mode = fins->op2; - } - if (narrow_conv_backprop(&nc, fins->op1, 0) <= 1) - return narrow_conv_emit(J, &nc); - } - return NEXTFOLD; -} - -/* -- Narrowing of implicit conversions ----------------------------------- */ - -/* Recursively strip overflow checks. */ -static TRef narrow_stripov(jit_State *J, TRef tr, int lastop, IRRef mode) -{ - IRRef ref = tref_ref(tr); - IRIns *ir = IR(ref); - int op = ir->o; - if (op >= IR_ADDOV && op <= lastop) { - BPropEntry *bp = narrow_bpc_get(J, ref, mode); - if (bp) { - return TREF(bp->val, irt_t(IR(bp->val)->t)); - } else { - IRRef op1 = ir->op1, op2 = ir->op2; /* The IR may be reallocated. */ - op1 = narrow_stripov(J, op1, lastop, mode); - op2 = narrow_stripov(J, op2, lastop, mode); - tr = emitir(IRT(op - IR_ADDOV + IR_ADD, - ((mode & IRCONV_DSTMASK) >> IRCONV_DSH)), op1, op2); - narrow_bpc_set(J, ref, tref_ref(tr), mode); - } - } else if (LJ_64 && (mode & IRCONV_SEXT) && !irt_is64(ir->t)) { - tr = emitir(IRT(IR_CONV, IRT_INTP), tr, mode); - } - return tr; -} - -/* Narrow array index. */ -TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef tr) -{ - IRIns *ir; - lj_assertJ(tref_isnumber(tr), "expected number type"); - if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ - return emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_INDEX); - /* Omit some overflow checks for array indexing. See comments above. */ - ir = IR(tref_ref(tr)); - if ((ir->o == IR_ADDOV || ir->o == IR_SUBOV) && irref_isk(ir->op2) && - (uint32_t)IR(ir->op2)->i + 0x40000000u < 0x80000000u) - return emitir(IRTI(ir->o - IR_ADDOV + IR_ADD), ir->op1, ir->op2); - return tr; -} - -/* Narrow conversion to integer operand (overflow undefined). */ -TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr) -{ - if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ - return emitir(IRTI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_ANY); - if (!tref_isinteger(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* - ** Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. - ** Use IRCONV_TOBIT for the cache entries, since the semantics are the same. - */ - return narrow_stripov(J, tr, IR_MULOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT); -} - -/* Narrow conversion to bitop operand (overflow wrapped). */ -TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr) -{ - if (tref_isstr(tr)) - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ - return emitir(IRTI(IR_TOBIT), tr, lj_ir_knum_tobit(J)); - if (!tref_isinteger(tr)) - lj_trace_err(J, LJ_TRERR_BADTYPE); - /* - ** Wrapped overflow semantics allow stripping of ADDOV and SUBOV. - ** MULOV cannot be stripped due to precision widening. - */ - return narrow_stripov(J, tr, IR_SUBOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT); -} - -#if LJ_HASFFI -/* Narrow C array index (overflow undefined). */ -TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef tr) -{ - lj_assertJ(tref_isnumber(tr), "expected number type"); - if (tref_isnum(tr)) - return emitir(IRT(IR_CONV, IRT_INTP), tr, (IRT_INTP<<5)|IRT_NUM|IRCONV_ANY); - /* Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. */ - return narrow_stripov(J, tr, IR_MULOV, - LJ_64 ? ((IRT_INTP<<5)|IRT_INT|IRCONV_SEXT) : - ((IRT_INTP<<5)|IRT_INT|IRCONV_TOBIT)); -} -#endif - -/* -- Narrowing of arithmetic operators ----------------------------------- */ - -/* Check whether a number fits into an int32_t (-0 is ok, too). */ -static int numisint(lua_Number n) -{ - return (n == (lua_Number)lj_num2int(n)); -} - -/* Convert string to number. Error out for non-numeric string values. */ -static TRef conv_str_tonum(jit_State *J, TRef tr, TValue *o) -{ - if (tref_isstr(tr)) { - tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); - /* Would need an inverted STRTO for this rare and useless case. */ - if (!lj_strscan_num(strV(o), o)) /* Convert in-place. Value used below. */ - lj_trace_err(J, LJ_TRERR_BADTYPE); /* Punt if non-numeric. */ - } - return tr; -} - -/* Narrowing of arithmetic operations. */ -TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, - TValue *vb, TValue *vc, IROp op) -{ - rb = conv_str_tonum(J, rb, vb); - rc = conv_str_tonum(J, rc, vc); - /* Must not narrow MUL in non-DUALNUM variant, because it loses -0. */ - if ((op >= IR_ADD && op <= (LJ_DUALNUM ? IR_MUL : IR_SUB)) && - tref_isinteger(rb) && tref_isinteger(rc) && - numisint(lj_vm_foldarith(numberVnum(vb), numberVnum(vc), - (int)op - (int)IR_ADD))) - return emitir(IRTGI((int)op - (int)IR_ADD + (int)IR_ADDOV), rb, rc); - if (!tref_isnum(rb)) rb = emitir(IRTN(IR_CONV), rb, IRCONV_NUM_INT); - if (!tref_isnum(rc)) rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); - return emitir(IRTN(op), rb, rc); -} - -/* Narrowing of unary minus operator. */ -TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc) -{ - rc = conv_str_tonum(J, rc, vc); - if (tref_isinteger(rc)) { - uint32_t k = (uint32_t)numberVint(vc); - if ((LJ_DUALNUM || k != 0) && k != 0x80000000u) { - TRef zero = lj_ir_kint(J, 0); - if (!LJ_DUALNUM) - emitir(IRTGI(IR_NE), rc, zero); - return emitir(IRTGI(IR_SUBOV), zero, rc); - } - rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); - } - return emitir(IRTN(IR_NEG), rc, lj_ir_ksimd(J, LJ_KSIMD_NEG)); -} - -/* Narrowing of modulo operator. */ -TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vb, TValue *vc) -{ - TRef tmp; - rb = conv_str_tonum(J, rb, vb); - rc = conv_str_tonum(J, rc, vc); - if ((LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) && - tref_isinteger(rb) && tref_isinteger(rc) && - (tvisint(vc) ? intV(vc) != 0 : !tviszero(vc))) { - emitir(IRTGI(IR_NE), rc, lj_ir_kint(J, 0)); - return emitir(IRTI(IR_MOD), rb, rc); - } - /* b % c ==> b - floor(b/c)*c */ - rb = lj_ir_tonum(J, rb); - rc = lj_ir_tonum(J, rc); - tmp = emitir(IRTN(IR_DIV), rb, rc); - tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_FLOOR); - tmp = emitir(IRTN(IR_MUL), tmp, rc); - return emitir(IRTN(IR_SUB), rb, tmp); -} - -/* -- Predictive narrowing of induction variables ------------------------- */ - -/* Narrow a single runtime value. */ -static int narrow_forl(jit_State *J, cTValue *o) -{ - if (tvisint(o)) return 1; - if (LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) return numisint(numV(o)); - return 0; -} - -/* Narrow the FORL index type by looking at the runtime values. */ -IRType lj_opt_narrow_forl(jit_State *J, cTValue *tv) -{ - lj_assertJ(tvisnumber(&tv[FORL_IDX]) && - tvisnumber(&tv[FORL_STOP]) && - tvisnumber(&tv[FORL_STEP]), - "expected number types"); - /* Narrow only if the runtime values of start/stop/step are all integers. */ - if (narrow_forl(J, &tv[FORL_IDX]) && - narrow_forl(J, &tv[FORL_STOP]) && - narrow_forl(J, &tv[FORL_STEP])) { - /* And if the loop index can't possibly overflow. */ - lua_Number step = numberVnum(&tv[FORL_STEP]); - lua_Number sum = numberVnum(&tv[FORL_STOP]) + step; - if (0 <= step ? (sum <= 2147483647.0) : (sum >= -2147483648.0)) - return IRT_INT; - } - return IRT_NUM; -} - -#undef IR -#undef fins -#undef emitir -#undef emitir_raw - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_sink.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_sink.c deleted file mode 100644 index 4b9008b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_sink.c +++ /dev/null @@ -1,258 +0,0 @@ -/* -** SINK: Allocation Sinking and Store Sinking. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_sink_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_target.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Check whether the store ref points to an eligible allocation. */ -static IRIns *sink_checkalloc(jit_State *J, IRIns *irs) -{ - IRIns *ir = IR(irs->op1); - if (!irref_isk(ir->op2)) - return NULL; /* Non-constant key. */ - if (ir->o == IR_HREFK || ir->o == IR_AREF) - ir = IR(ir->op1); - else if (!(ir->o == IR_HREF || ir->o == IR_NEWREF || - ir->o == IR_FREF || ir->o == IR_ADD)) - return NULL; /* Unhandled reference type (for XSTORE). */ - ir = IR(ir->op1); - if (!(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW)) - return NULL; /* Not an allocation. */ - return ir; /* Return allocation. */ -} - -/* Recursively check whether a value depends on a PHI. */ -static int sink_phidep(jit_State *J, IRRef ref, int *workp) -{ - IRIns *ir = IR(ref); - if (!*workp) return 1; /* Give up and pretend it does. */ - (*workp)--; - if (irt_isphi(ir->t)) return 1; - if (ir->op1 >= REF_FIRST && sink_phidep(J, ir->op1, workp)) return 1; - if (ir->op2 >= REF_FIRST && sink_phidep(J, ir->op2, workp)) return 1; - return 0; -} - -/* Check whether a value is a sinkable PHI or loop-invariant. */ -static int sink_checkphi(jit_State *J, IRIns *ira, IRRef ref) -{ - if (ref >= REF_FIRST) { - IRIns *ir = IR(ref); - if (irt_isphi(ir->t) || (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && - irt_isphi(IR(ir->op1)->t))) { - ira->prev++; - return 1; /* Sinkable PHI. */ - } - /* Otherwise the value must be loop-invariant. */ - if (ref < J->loopref) { - /* Check for PHI dependencies, but give up after reasonable effort. */ - int work = 64; - return !sink_phidep(J, ref, &work); - } else { - return 0; /* Loop-variant. */ - } - } - return 1; /* Constant (non-PHI). */ -} - -/* Mark non-sinkable allocations using single-pass backward propagation. -** -** Roots for the marking process are: -** - Some PHIs or snapshots (see below). -** - Non-PHI, non-constant values stored to PHI allocations. -** - All guards. -** - Any remaining loads not eliminated by store-to-load forwarding. -** - Stores with non-constant keys. -** - All stored values. -*/ -static void sink_mark_ins(jit_State *J) -{ - IRIns *ir, *irlast = IR(J->cur.nins-1); - for (ir = irlast ; ; ir--) { - switch (ir->o) { - case IR_BASE: - return; /* Finished. */ - case IR_ALOAD: case IR_HLOAD: case IR_XLOAD: case IR_TBAR: case IR_ALEN: - irt_setmark(IR(ir->op1)->t); /* Mark ref for remaining loads. */ - break; - case IR_FLOAD: - if (irt_ismarked(ir->t) || ir->op2 == IRFL_TAB_META) - irt_setmark(IR(ir->op1)->t); /* Mark table for remaining loads. */ - break; - case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: { - IRIns *ira = sink_checkalloc(J, ir); - if (!ira || (irt_isphi(ira->t) && !sink_checkphi(J, ira, ir->op2))) - irt_setmark(IR(ir->op1)->t); /* Mark ineligible ref. */ - irt_setmark(IR(ir->op2)->t); /* Mark stored value. */ - break; - } -#if LJ_HASFFI - case IR_CNEWI: - if (irt_isphi(ir->t) && - (!sink_checkphi(J, ir, ir->op2) || - (LJ_32 && ir+1 < irlast && (ir+1)->o == IR_HIOP && - !sink_checkphi(J, ir, (ir+1)->op2)))) - irt_setmark(ir->t); /* Mark ineligible allocation. */ -#endif - /* fallthrough */ - case IR_USTORE: - irt_setmark(IR(ir->op2)->t); /* Mark stored value. */ - break; -#if LJ_HASFFI - case IR_CALLXS: -#endif - case IR_CALLS: - irt_setmark(IR(ir->op1)->t); /* Mark (potentially) stored values. */ - break; - case IR_PHI: { - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - irl->prev = irr->prev = 0; /* Clear PHI value counts. */ - if (irl->o == irr->o && - (irl->o == IR_TNEW || irl->o == IR_TDUP || - (LJ_HASFFI && (irl->o == IR_CNEW || irl->o == IR_CNEWI)))) - break; - irt_setmark(irl->t); - irt_setmark(irr->t); - break; - } - default: - if (irt_ismarked(ir->t) || irt_isguard(ir->t)) { /* Propagate mark. */ - if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); - if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); - } - break; - } - } -} - -/* Mark all instructions referenced by a snapshot. */ -static void sink_mark_snap(jit_State *J, SnapShot *snap) -{ - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - IRRef ref = snap_ref(map[n]); - if (!irref_isk(ref)) - irt_setmark(IR(ref)->t); - } -} - -/* Iteratively remark PHI refs with differing marks or PHI value counts. */ -static void sink_remark_phi(jit_State *J) -{ - IRIns *ir; - int remark; - do { - remark = 0; - for (ir = IR(J->cur.nins-1); ir->o == IR_PHI; ir--) { - IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); - if (!((irl->t.irt ^ irr->t.irt) & IRT_MARK) && irl->prev == irr->prev) - continue; - remark |= (~(irl->t.irt & irr->t.irt) & IRT_MARK); - irt_setmark(IR(ir->op1)->t); - irt_setmark(IR(ir->op2)->t); - } - } while (remark); -} - -/* Sweep instructions and tag sunken allocations and stores. */ -static void sink_sweep_ins(jit_State *J) -{ - IRIns *ir, *irbase = IR(REF_BASE); - for (ir = IR(J->cur.nins-1) ; ir >= irbase; ir--) { - switch (ir->o) { - case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: { - IRIns *ira = sink_checkalloc(J, ir); - if (ira && !irt_ismarked(ira->t)) { - int delta = (int)(ir - ira); - ir->prev = REGSP(RID_SINK, delta > 255 ? 255 : delta); - } else { - ir->prev = REGSP_INIT; - } - break; - } - case IR_NEWREF: - if (!irt_ismarked(IR(ir->op1)->t)) { - ir->prev = REGSP(RID_SINK, 0); - } else { - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - } - break; -#if LJ_HASFFI - case IR_CNEW: case IR_CNEWI: -#endif - case IR_TNEW: case IR_TDUP: - if (!irt_ismarked(ir->t)) { - ir->t.irt &= ~IRT_GUARD; - ir->prev = REGSP(RID_SINK, 0); - J->cur.sinktags = 1; /* Signal present SINK tags to assembler. */ - } else { - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - } - break; - case IR_PHI: { - IRIns *ira = IR(ir->op2); - if (!irt_ismarked(ira->t) && - (ira->o == IR_TNEW || ira->o == IR_TDUP || - (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI)))) { - ir->prev = REGSP(RID_SINK, 0); - } else { - ir->prev = REGSP_INIT; - } - break; - } - default: - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - break; - } - } - for (ir = IR(J->cur.nk); ir < irbase; ir++) { - irt_clearmark(ir->t); - ir->prev = REGSP_INIT; - /* The false-positive of irt_is64() for ASMREF_L (REF_NIL) is OK here. */ - if (irt_is64(ir->t) && ir->o != IR_KNULL) - ir++; - } -} - -/* Allocation sinking and store sinking. -** -** 1. Mark all non-sinkable allocations. -** 2. Then sink all remaining allocations and the related stores. -*/ -void lj_opt_sink(jit_State *J) -{ - const uint32_t need = (JIT_F_OPT_SINK|JIT_F_OPT_FWD| - JIT_F_OPT_DCE|JIT_F_OPT_CSE|JIT_F_OPT_FOLD); - if ((J->flags & need) == need && - (J->chain[IR_TNEW] || J->chain[IR_TDUP] || - (LJ_HASFFI && (J->chain[IR_CNEW] || J->chain[IR_CNEWI])))) { - if (!J->loopref) - sink_mark_snap(J, &J->cur.snap[J->cur.nsnap-1]); - sink_mark_ins(J); - if (J->loopref) - sink_remark_phi(J); - sink_sweep_ins(J); - } -} - -#undef IR - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_split.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_split.c deleted file mode 100644 index 506b981..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_opt_split.c +++ /dev/null @@ -1,848 +0,0 @@ -/* -** SPLIT: Split 64 bit IR instructions into 32 bit IR instructions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_opt_split_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT && (LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) - -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_dispatch.h" -#include "lj_vm.h" - -/* SPLIT pass: -** -** This pass splits up 64 bit IR instructions into multiple 32 bit IR -** instructions. It's only active for soft-float targets or for 32 bit CPUs -** which lack native 64 bit integer operations (the FFI is currently the -** only emitter for 64 bit integer instructions). -** -** Splitting the IR in a separate pass keeps each 32 bit IR assembler -** backend simple. Only a small amount of extra functionality needs to be -** implemented. This is much easier than adding support for allocating -** register pairs to each backend (believe me, I tried). A few simple, but -** important optimizations can be performed by the SPLIT pass, which would -** be tedious to do in the backend. -** -** The basic idea is to replace each 64 bit IR instruction with its 32 bit -** equivalent plus an extra HIOP instruction. The splitted IR is not passed -** through FOLD or any other optimizations, so each HIOP is guaranteed to -** immediately follow it's counterpart. The actual functionality of HIOP is -** inferred from the previous instruction. -** -** The operands of HIOP hold the hiword input references. The output of HIOP -** is the hiword output reference, which is also used to hold the hiword -** register or spill slot information. The register allocator treats this -** instruction independently of any other instruction, which improves code -** quality compared to using fixed register pairs. -** -** It's easier to split up some instructions into two regular 32 bit -** instructions. E.g. XLOAD is split up into two XLOADs with two different -** addresses. Obviously 64 bit constants need to be split up into two 32 bit -** constants, too. Some hiword instructions can be entirely omitted, e.g. -** when zero-extending a 32 bit value to 64 bits. 64 bit arguments for calls -** are split up into two 32 bit arguments each. -** -** On soft-float targets, floating-point instructions are directly converted -** to soft-float calls by the SPLIT pass (except for comparisons and MIN/MAX). -** HIOP for number results has the type IRT_SOFTFP ("sfp" in -jdump). -** -** Here's the IR and x64 machine code for 'x.b = x.a + 1' for a struct with -** two int64_t fields: -** -** 0100 p32 ADD base +8 -** 0101 i64 XLOAD 0100 -** 0102 i64 ADD 0101 +1 -** 0103 p32 ADD base +16 -** 0104 i64 XSTORE 0103 0102 -** -** mov rax, [esi+0x8] -** add rax, +0x01 -** mov [esi+0x10], rax -** -** Here's the transformed IR and the x86 machine code after the SPLIT pass: -** -** 0100 p32 ADD base +8 -** 0101 int XLOAD 0100 -** 0102 p32 ADD base +12 -** 0103 int XLOAD 0102 -** 0104 int ADD 0101 +1 -** 0105 int HIOP 0103 +0 -** 0106 p32 ADD base +16 -** 0107 int XSTORE 0106 0104 -** 0108 int HIOP 0106 0105 -** -** mov eax, [esi+0x8] -** mov ecx, [esi+0xc] -** add eax, +0x01 -** adc ecx, +0x00 -** mov [esi+0x10], eax -** mov [esi+0x14], ecx -** -** You may notice the reassociated hiword address computation, which is -** later fused into the mov operands by the assembler. -*/ - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Directly emit the transformed IR without updating chains etc. */ -static IRRef split_emit(jit_State *J, uint16_t ot, IRRef1 op1, IRRef1 op2) -{ - IRRef nref = lj_ir_nextins(J); - IRIns *ir = IR(nref); - ir->ot = ot; - ir->op1 = op1; - ir->op2 = op2; - return nref; -} - -#if LJ_SOFTFP -/* Emit a (checked) number to integer conversion. */ -static IRRef split_num2int(jit_State *J, IRRef lo, IRRef hi, int check) -{ - IRRef tmp, res; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), lo, hi); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hi, lo); -#endif - res = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_softfp_d2i); - if (check) { - tmp = split_emit(J, IRTI(IR_CALLN), res, IRCALL_softfp_i2d); - split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); - split_emit(J, IRTGI(IR_EQ), tmp, lo); - split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), tmp+1, hi); - } - return res; -} - -/* Emit a CALLN with one split 64 bit argument. */ -static IRRef split_call_l(jit_State *J, IRRef1 *hisubst, IRIns *oir, - IRIns *ir, IRCallID id) -{ - IRRef tmp, op1 = ir->op1; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); -#endif - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); - return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); -} -#endif - -/* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */ -static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir, - IRIns *ir, IRCallID id) -{ - IRRef tmp, op1 = ir->op1, op2 = ir->op2; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); -#endif - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); - return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); -} - -/* Emit a CALLN with two split 64 bit arguments. */ -static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir, - IRIns *ir, IRCallID id) -{ - IRRef tmp, op1 = ir->op1, op2 = ir->op2; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]); - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); -#endif - ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); - return split_emit(J, - IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT), - tmp, tmp); -} - -/* Get a pointer to the other 32 bit word (LE: hiword, BE: loword). */ -static IRRef split_ptr(jit_State *J, IRIns *oir, IRRef ref) -{ - IRRef nref = oir[ref].prev; - IRIns *ir = IR(nref); - int32_t ofs = 4; - if (ir->o == IR_KPTR) - return lj_ir_kptr(J, (char *)ir_kptr(ir) + ofs); - if (ir->o == IR_ADD && irref_isk(ir->op2) && !irt_isphi(oir[ref].t)) { - /* Reassociate address. */ - ofs += IR(ir->op2)->i; - nref = ir->op1; - if (ofs == 0) return nref; - } - return split_emit(J, IRT(IR_ADD, IRT_PTR), nref, lj_ir_kint(J, ofs)); -} - -#if LJ_HASFFI -static IRRef split_bitshift(jit_State *J, IRRef1 *hisubst, - IRIns *oir, IRIns *nir, IRIns *ir) -{ - IROp op = ir->o; - IRRef kref = nir->op2; - if (irref_isk(kref)) { /* Optimize constant shifts. */ - int32_t k = (IR(kref)->i & 63); - IRRef lo = nir->op1, hi = hisubst[ir->op1]; - if (op == IR_BROL || op == IR_BROR) { - if (op == IR_BROR) k = (-k & 63); - if (k >= 32) { IRRef t = lo; lo = hi; hi = t; k -= 32; } - if (k == 0) { - passthrough: - J->cur.nins--; - ir->prev = lo; - return hi; - } else { - TRef k1, k2; - IRRef t1, t2, t3, t4; - J->cur.nins--; - k1 = lj_ir_kint(J, k); - k2 = lj_ir_kint(J, (-k & 31)); - t1 = split_emit(J, IRTI(IR_BSHL), lo, k1); - t2 = split_emit(J, IRTI(IR_BSHL), hi, k1); - t3 = split_emit(J, IRTI(IR_BSHR), lo, k2); - t4 = split_emit(J, IRTI(IR_BSHR), hi, k2); - ir->prev = split_emit(J, IRTI(IR_BOR), t1, t4); - return split_emit(J, IRTI(IR_BOR), t2, t3); - } - } else if (k == 0) { - goto passthrough; - } else if (k < 32) { - if (op == IR_BSHL) { - IRRef t1 = split_emit(J, IRTI(IR_BSHL), hi, kref); - IRRef t2 = split_emit(J, IRTI(IR_BSHR), lo, lj_ir_kint(J, (-k&31))); - return split_emit(J, IRTI(IR_BOR), t1, t2); - } else { - IRRef t1 = ir->prev, t2; - lj_assertJ(op == IR_BSHR || op == IR_BSAR, "bad usage"); - nir->o = IR_BSHR; - t2 = split_emit(J, IRTI(IR_BSHL), hi, lj_ir_kint(J, (-k&31))); - ir->prev = split_emit(J, IRTI(IR_BOR), t1, t2); - return split_emit(J, IRTI(op), hi, kref); - } - } else { - if (op == IR_BSHL) { - if (k == 32) - J->cur.nins--; - else - lo = ir->prev; - ir->prev = lj_ir_kint(J, 0); - return lo; - } else { - lj_assertJ(op == IR_BSHR || op == IR_BSAR, "bad usage"); - if (k == 32) { - J->cur.nins--; - ir->prev = hi; - } else { - nir->op1 = hi; - } - if (op == IR_BSHR) - return lj_ir_kint(J, 0); - else - return split_emit(J, IRTI(IR_BSAR), hi, lj_ir_kint(J, 31)); - } - } - } - return split_call_li(J, hisubst, oir, ir, - op - IR_BSHL + IRCALL_lj_carith_shl64); -} - -static IRRef split_bitop(jit_State *J, IRRef1 *hisubst, - IRIns *nir, IRIns *ir) -{ - IROp op = ir->o; - IRRef hi, kref = nir->op2; - if (irref_isk(kref)) { /* Optimize bit operations with lo constant. */ - int32_t k = IR(kref)->i; - if (k == 0 || k == -1) { - if (op == IR_BAND) k = ~k; - if (k == 0) { - J->cur.nins--; - ir->prev = nir->op1; - } else if (op == IR_BXOR) { - nir->o = IR_BNOT; - nir->op2 = 0; - } else { - J->cur.nins--; - ir->prev = kref; - } - } - } - hi = hisubst[ir->op1]; - kref = hisubst[ir->op2]; - if (irref_isk(kref)) { /* Optimize bit operations with hi constant. */ - int32_t k = IR(kref)->i; - if (k == 0 || k == -1) { - if (op == IR_BAND) k = ~k; - if (k == 0) { - return hi; - } else if (op == IR_BXOR) { - return split_emit(J, IRTI(IR_BNOT), hi, 0); - } else { - return kref; - } - } - } - return split_emit(J, IRTI(op), hi, kref); -} -#endif - -/* Substitute references of a snapshot. */ -static void split_subst_snap(jit_State *J, SnapShot *snap, IRIns *oir) -{ - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRIns *ir = &oir[snap_ref(sn)]; - if (!(LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && irref_isk(snap_ref(sn)))) - map[n] = ((sn & 0xffff0000) | ir->prev); - } -} - -/* Transform the old IR to the new IR. */ -static void split_ir(jit_State *J) -{ - IRRef nins = J->cur.nins, nk = J->cur.nk; - MSize irlen = nins - nk; - MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1)); - IRIns *oir = (IRIns *)lj_buf_tmp(J->L, need); - IRRef1 *hisubst; - IRRef ref, snref; - SnapShot *snap; - - /* Copy old IR to buffer. */ - memcpy(oir, IR(nk), irlen*sizeof(IRIns)); - /* Bias hiword substitution table and old IR. Loword kept in field prev. */ - hisubst = (IRRef1 *)&oir[irlen] - nk; - oir -= nk; - - /* Remove all IR instructions, but retain IR constants. */ - J->cur.nins = REF_FIRST; - J->loopref = 0; - - /* Process constants and fixed references. */ - for (ref = nk; ref <= REF_BASE; ref++) { - IRIns *ir = &oir[ref]; - if ((LJ_SOFTFP && ir->o == IR_KNUM) || ir->o == IR_KINT64) { - /* Split up 64 bit constant. */ - TValue tv = *ir_k64(ir); - ir->prev = lj_ir_kint(J, (int32_t)tv.u32.lo); - hisubst[ref] = lj_ir_kint(J, (int32_t)tv.u32.hi); - } else { - ir->prev = ref; /* Identity substitution for loword. */ - hisubst[ref] = 0; - } - if (irt_is64(ir->t) && ir->o != IR_KNULL) - ref++; - } - - /* Process old IR instructions. */ - snap = J->cur.snap; - snref = snap->ref; - for (ref = REF_FIRST; ref < nins; ref++) { - IRIns *ir = &oir[ref]; - IRRef nref = lj_ir_nextins(J); - IRIns *nir = IR(nref); - IRRef hi = 0; - - if (ref >= snref) { - snap->ref = nref; - split_subst_snap(J, snap++, oir); - snref = snap < &J->cur.snap[J->cur.nsnap] ? snap->ref : ~(IRRef)0; - } - - /* Copy-substitute old instruction to new instruction. */ - nir->op1 = ir->op1 < nk ? ir->op1 : oir[ir->op1].prev; - nir->op2 = ir->op2 < nk ? ir->op2 : oir[ir->op2].prev; - ir->prev = nref; /* Loword substitution. */ - nir->o = ir->o; - nir->t.irt = ir->t.irt & ~(IRT_MARK|IRT_ISPHI); - hisubst[ref] = 0; - - /* Split 64 bit instructions. */ -#if LJ_SOFTFP - if (irt_isnum(ir->t)) { - nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */ - /* Note: hi ref = lo ref + 1! Required for SNAP_SOFTFPNUM logic. */ - switch (ir->o) { - case IR_ADD: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_add); - break; - case IR_SUB: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_sub); - break; - case IR_MUL: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_mul); - break; - case IR_DIV: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_div); - break; - case IR_POW: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_pow); - break; - case IR_FPMATH: - hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2); - break; - case IR_LDEXP: - hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp); - break; - case IR_NEG: case IR_ABS: - nir->o = IR_CONV; /* Pass through loword. */ - nir->op2 = (IRT_INT << 5) | IRT_INT; - hi = split_emit(J, IRT(ir->o == IR_NEG ? IR_BXOR : IR_BAND, IRT_SOFTFP), - hisubst[ir->op1], - lj_ir_kint(J, (int32_t)(0x7fffffffu + (ir->o == IR_NEG)))); - break; - case IR_SLOAD: - if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from int to number. */ - nir->op2 &= ~IRSLOAD_CONVERT; - ir->prev = nref = split_emit(J, IRTI(IR_CALLN), nref, - IRCALL_softfp_i2d); - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - break; - } - /* fallthrough */ - case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: - case IR_STRTO: - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - break; - case IR_FLOAD: - lj_assertJ(ir->op1 == REF_NIL, "expected FLOAD from GG_State"); - hi = lj_ir_kint(J, *(int32_t*)((char*)J2GG(J) + ir->op2 + LJ_LE*4)); - nir->op2 += LJ_BE*4; - break; - case IR_XLOAD: { - IRIns inslo = *nir; /* Save/undo the emit of the lo XLOAD. */ - J->cur.nins--; - hi = split_ptr(J, oir, ir->op1); /* Insert the hiref ADD. */ -#if LJ_BE - hi = split_emit(J, IRT(IR_XLOAD, IRT_INT), hi, ir->op2); - inslo.t.irt = IRT_SOFTFP | (inslo.t.irt & IRT_GUARD); -#endif - nref = lj_ir_nextins(J); - nir = IR(nref); - *nir = inslo; /* Re-emit lo XLOAD. */ -#if LJ_LE - hi = split_emit(J, IRT(IR_XLOAD, IRT_SOFTFP), hi, ir->op2); - ir->prev = nref; -#else - ir->prev = hi; hi = nref; -#endif - break; - } - case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_XSTORE: - split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nir->op1, hisubst[ir->op2]); - break; - case IR_CONV: { /* Conversion to number. Others handled below. */ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); - UNUSED(st); -#if LJ_32 && LJ_HASFFI - if (st == IRT_I64 || st == IRT_U64) { - hi = split_call_l(J, hisubst, oir, ir, - st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d); - break; - } -#endif - lj_assertJ(st == IRT_INT || - (LJ_32 && LJ_HASFFI && (st == IRT_U32 || st == IRT_FLOAT)), - "bad source type for CONV"); - nir->o = IR_CALLN; -#if LJ_32 && LJ_HASFFI - nir->op2 = st == IRT_INT ? IRCALL_softfp_i2d : - st == IRT_FLOAT ? IRCALL_softfp_f2d : - IRCALL_softfp_ui2d; -#else - nir->op2 = IRCALL_softfp_i2d; -#endif - hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - break; - } - case IR_CALLN: - case IR_CALLL: - case IR_CALLS: - case IR_CALLXS: - goto split_call; - case IR_PHI: - if (nir->op1 == nir->op2) - J->cur.nins--; /* Drop useless PHIs. */ - if (hisubst[ir->op1] != hisubst[ir->op2]) - split_emit(J, IRT(IR_PHI, IRT_SOFTFP), - hisubst[ir->op1], hisubst[ir->op2]); - break; - case IR_HIOP: - J->cur.nins--; /* Drop joining HIOP. */ - ir->prev = nir->op1; - hi = nir->op2; - break; - default: - lj_assertJ(ir->o <= IR_NE || ir->o == IR_MIN || ir->o == IR_MAX, - "bad IR op %d", ir->o); - hi = split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), - hisubst[ir->op1], hisubst[ir->op2]); - break; - } - } else -#endif -#if LJ_32 && LJ_HASFFI - if (irt_isint64(ir->t)) { - IRRef hiref = hisubst[ir->op1]; - nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */ - switch (ir->o) { - case IR_ADD: - case IR_SUB: - /* Use plain op for hiword if loword cannot produce a carry/borrow. */ - if (irref_isk(nir->op2) && IR(nir->op2)->i == 0) { - ir->prev = nir->op1; /* Pass through loword. */ - nir->op1 = hiref; nir->op2 = hisubst[ir->op2]; - hi = nref; - break; - } - /* fallthrough */ - case IR_NEG: - hi = split_emit(J, IRTI(IR_HIOP), hiref, hisubst[ir->op2]); - break; - case IR_MUL: - hi = split_call_ll(J, hisubst, oir, ir, IRCALL_lj_carith_mul64); - break; - case IR_DIV: - hi = split_call_ll(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : - IRCALL_lj_carith_divu64); - break; - case IR_MOD: - hi = split_call_ll(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : - IRCALL_lj_carith_modu64); - break; - case IR_POW: - hi = split_call_ll(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : - IRCALL_lj_carith_powu64); - break; - case IR_BNOT: - hi = split_emit(J, IRTI(IR_BNOT), hiref, 0); - break; - case IR_BSWAP: - ir->prev = split_emit(J, IRTI(IR_BSWAP), hiref, 0); - hi = nref; - break; - case IR_BAND: case IR_BOR: case IR_BXOR: - hi = split_bitop(J, hisubst, nir, ir); - break; - case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR: - hi = split_bitshift(J, hisubst, oir, nir, ir); - break; - case IR_FLOAD: - lj_assertJ(ir->op2 == IRFL_CDATA_INT64, "only INT64 supported"); - hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4); -#if LJ_BE - ir->prev = hi; hi = nref; -#endif - break; - case IR_XLOAD: - hi = split_emit(J, IRTI(IR_XLOAD), split_ptr(J, oir, ir->op1), ir->op2); -#if LJ_BE - ir->prev = hi; hi = nref; -#endif - break; - case IR_XSTORE: - split_emit(J, IRTI(IR_HIOP), nir->op1, hisubst[ir->op2]); - break; - case IR_CONV: { /* Conversion to 64 bit integer. Others handled below. */ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if LJ_SOFTFP - if (st == IRT_NUM) { /* NUM to 64 bit int conv. */ - hi = split_call_l(J, hisubst, oir, ir, - irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul); - } else if (st == IRT_FLOAT) { /* FLOAT to 64 bit int conv. */ - nir->o = IR_CALLN; - nir->op2 = irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul; - hi = split_emit(J, IRTI(IR_HIOP), nref, nref); - } -#else - if (st == IRT_NUM || st == IRT_FLOAT) { /* FP to 64 bit int conv. */ - hi = split_emit(J, IRTI(IR_HIOP), nir->op1, nref); - } -#endif - else if (st == IRT_I64 || st == IRT_U64) { /* 64/64 bit cast. */ - /* Drop cast, since assembler doesn't care. But fwd both parts. */ - hi = hiref; - goto fwdlo; - } else if ((ir->op2 & IRCONV_SEXT)) { /* Sign-extend to 64 bit. */ - IRRef k31 = lj_ir_kint(J, 31); - nir = IR(nref); /* May have been reallocated. */ - ir->prev = nir->op1; /* Pass through loword. */ - nir->o = IR_BSAR; /* hi = bsar(lo, 31). */ - nir->op2 = k31; - hi = nref; - } else { /* Zero-extend to 64 bit. */ - hi = lj_ir_kint(J, 0); - goto fwdlo; - } - break; - } - case IR_CALLXS: - goto split_call; - case IR_PHI: { - IRRef hiref2; - if ((irref_isk(nir->op1) && irref_isk(nir->op2)) || - nir->op1 == nir->op2) - J->cur.nins--; /* Drop useless PHIs. */ - hiref2 = hisubst[ir->op2]; - if (!((irref_isk(hiref) && irref_isk(hiref2)) || hiref == hiref2)) - split_emit(J, IRTI(IR_PHI), hiref, hiref2); - break; - } - case IR_HIOP: - J->cur.nins--; /* Drop joining HIOP. */ - ir->prev = nir->op1; - hi = nir->op2; - break; - default: - lj_assertJ(ir->o <= IR_NE, "bad IR op %d", ir->o); /* Comparisons. */ - split_emit(J, IRTGI(IR_HIOP), hiref, hisubst[ir->op2]); - break; - } - } else -#endif -#if LJ_SOFTFP - if (ir->o == IR_SLOAD) { - if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from number to int. */ - nir->op2 &= ~IRSLOAD_CONVERT; - if (!(nir->op2 & IRSLOAD_TYPECHECK)) - nir->t.irt = IRT_INT; /* Drop guard. */ - split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); - ir->prev = split_num2int(J, nref, nref+1, irt_isguard(ir->t)); - } - } else if (ir->o == IR_TOBIT) { - IRRef tmp, op1 = ir->op1; - J->cur.nins--; -#if LJ_LE - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); -#else - tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); -#endif - ir->prev = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_lj_vm_tobit); - } else if (ir->o == IR_TOSTR || ir->o == IR_TMPREF) { - if (hisubst[ir->op1]) { - if (irref_isk(ir->op1)) - nir->op1 = ir->op1; - else - split_emit(J, IRT(IR_HIOP, IRT_NIL), hisubst[ir->op1], nref); - } - } else if (ir->o == IR_HREF || ir->o == IR_NEWREF) { - if (irref_isk(ir->op2) && hisubst[ir->op2]) - nir->op2 = ir->op2; - } else -#endif - if (ir->o == IR_CONV) { /* See above, too. */ - IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); -#if LJ_32 && LJ_HASFFI - if (st == IRT_I64 || st == IRT_U64) { /* Conversion from 64 bit int. */ -#if LJ_SOFTFP - if (irt_isfloat(ir->t)) { - split_call_l(J, hisubst, oir, ir, - st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f); - J->cur.nins--; /* Drop unused HIOP. */ - } -#else - if (irt_isfp(ir->t)) { /* 64 bit integer to FP conversion. */ - ir->prev = split_emit(J, IRT(IR_HIOP, irt_type(ir->t)), - hisubst[ir->op1], nref); - } -#endif - else { /* Truncate to lower 32 bits. */ - fwdlo: - ir->prev = nir->op1; /* Forward loword. */ - /* Replace with NOP to avoid messing up the snapshot logic. */ - nir->ot = IRT(IR_NOP, IRT_NIL); - nir->op1 = nir->op2 = 0; - } - } -#endif -#if LJ_SOFTFP && LJ_32 && LJ_HASFFI - else if (irt_isfloat(ir->t)) { - if (st == IRT_NUM) { - split_call_l(J, hisubst, oir, ir, IRCALL_softfp_d2f); - J->cur.nins--; /* Drop unused HIOP. */ - } else { - nir->o = IR_CALLN; - nir->op2 = st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f; - } - } else if (st == IRT_FLOAT) { - nir->o = IR_CALLN; - nir->op2 = irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui; - } else -#endif -#if LJ_SOFTFP - if (st == IRT_NUM || (LJ_32 && LJ_HASFFI && st == IRT_FLOAT)) { - if (irt_isguard(ir->t)) { - lj_assertJ(st == IRT_NUM && irt_isint(ir->t), "bad CONV types"); - J->cur.nins--; - ir->prev = split_num2int(J, nir->op1, hisubst[ir->op1], 1); - } else { - split_call_l(J, hisubst, oir, ir, -#if LJ_32 && LJ_HASFFI - st == IRT_NUM ? - (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) : - (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui) -#else - IRCALL_softfp_d2i -#endif - ); - J->cur.nins--; /* Drop unused HIOP. */ - } - } -#endif - } else if (ir->o == IR_CALLXS) { - IRRef hiref; - split_call: - hiref = hisubst[ir->op1]; - if (hiref) { - IROpT ot = nir->ot; - IRRef op2 = nir->op2; - nir->ot = IRT(IR_CARG, IRT_NIL); -#if LJ_LE - nir->op2 = hiref; -#else - nir->op2 = nir->op1; nir->op1 = hiref; -#endif - ir->prev = nref = split_emit(J, ot, nref, op2); - } - if (LJ_SOFTFP ? irt_is64(ir->t) : irt_isint64(ir->t)) - hi = split_emit(J, - IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT), - nref, nref); - } else if (ir->o == IR_CARG) { - IRRef hiref = hisubst[ir->op1]; - if (hiref) { - IRRef op2 = nir->op2; -#if LJ_LE - nir->op2 = hiref; -#else - nir->op2 = nir->op1; nir->op1 = hiref; -#endif - ir->prev = nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2); - nir = IR(nref); - } - hiref = hisubst[ir->op2]; - if (hiref) { -#if !LJ_TARGET_X86 - int carg = 0; - IRIns *cir; - for (cir = IR(nir->op1); cir->o == IR_CARG; cir = IR(cir->op1)) - carg++; - if ((carg & 1) == 0) { /* Align 64 bit arguments. */ - IRRef op2 = nir->op2; - nir->op2 = REF_NIL; - nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2); - nir = IR(nref); - } -#endif -#if LJ_BE - { IRRef tmp = nir->op2; nir->op2 = hiref; hiref = tmp; } -#endif - ir->prev = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, hiref); - } - } else if (ir->o == IR_CNEWI) { - if (hisubst[ir->op2]) - split_emit(J, IRT(IR_HIOP, IRT_NIL), nref, hisubst[ir->op2]); - } else if (ir->o == IR_LOOP) { - J->loopref = nref; /* Needed by assembler. */ - } - hisubst[ref] = hi; /* Store hiword substitution. */ - } - if (snref == nins) { /* Substitution for last snapshot. */ - snap->ref = J->cur.nins; - split_subst_snap(J, snap, oir); - } - - /* Add PHI marks. */ - for (ref = J->cur.nins-1; ref >= REF_FIRST; ref--) { - IRIns *ir = IR(ref); - if (ir->o != IR_PHI) break; - if (!irref_isk(ir->op1)) irt_setphi(IR(ir->op1)->t); - if (ir->op2 > J->loopref) irt_setphi(IR(ir->op2)->t); - } -} - -/* Protected callback for split pass. */ -static TValue *cpsplit(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - split_ir(J); - UNUSED(L); UNUSED(dummy); - return NULL; -} - -#if defined(LUA_USE_ASSERT) || LJ_SOFTFP -/* Slow, but sure way to check whether a SPLIT pass is needed. */ -static int split_needsplit(jit_State *J) -{ - IRIns *ir, *irend; - IRRef ref; - for (ir = IR(REF_FIRST), irend = IR(J->cur.nins); ir < irend; ir++) - if (LJ_SOFTFP ? irt_is64orfp(ir->t) : irt_isint64(ir->t)) - return 1; - if (LJ_SOFTFP) { - for (ref = J->chain[IR_SLOAD]; ref; ref = IR(ref)->prev) - if ((IR(ref)->op2 & IRSLOAD_CONVERT)) - return 1; - if (J->chain[IR_TOBIT]) - return 1; - } - for (ref = J->chain[IR_CONV]; ref; ref = IR(ref)->prev) { - IRType st = (IR(ref)->op2 & IRCONV_SRCMASK); - if ((LJ_SOFTFP && (st == IRT_NUM || st == IRT_FLOAT)) || - st == IRT_I64 || st == IRT_U64) - return 1; - } - return 0; /* Nope. */ -} -#endif - -/* SPLIT pass. */ -void lj_opt_split(jit_State *J) -{ -#if LJ_SOFTFP - if (!J->needsplit) - J->needsplit = split_needsplit(J); -#else - lj_assertJ(J->needsplit >= split_needsplit(J), "bad SPLIT state"); -#endif - if (J->needsplit) { - int errcode = lj_vm_cpcall(J->L, NULL, J, cpsplit); - if (errcode) { - /* Completely reset the trace to avoid inconsistent dump on abort. */ - J->cur.nins = J->cur.nk = REF_BASE; - J->cur.nsnap = 0; - lj_err_throw(J->L, errcode); /* Propagate errors. */ - } - } -} - -#undef IR - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.c deleted file mode 100644 index 9ddf60e..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.c +++ /dev/null @@ -1,2747 +0,0 @@ -/* -** Lua parser (source code -> bytecode). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_parse_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_state.h" -#include "lj_bc.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_strfmt.h" -#include "lj_lex.h" -#include "lj_parse.h" -#include "lj_vm.h" -#include "lj_vmevent.h" - -/* -- Parser structures and definitions ----------------------------------- */ - -/* Expression kinds. */ -typedef enum { - /* Constant expressions must be first and in this order: */ - VKNIL, - VKFALSE, - VKTRUE, - VKSTR, /* sval = string value */ - VKNUM, /* nval = number value */ - VKLAST = VKNUM, - VKCDATA, /* nval = cdata value, not treated as a constant expression */ - /* Non-constant expressions follow: */ - VLOCAL, /* info = local register, aux = vstack index */ - VUPVAL, /* info = upvalue index, aux = vstack index */ - VGLOBAL, /* sval = string value */ - VINDEXED, /* info = table register, aux = index reg/byte/string const */ - VJMP, /* info = instruction PC */ - VRELOCABLE, /* info = instruction PC */ - VNONRELOC, /* info = result register */ - VCALL, /* info = instruction PC, aux = base */ - VVOID -} ExpKind; - -/* Expression descriptor. */ -typedef struct ExpDesc { - union { - struct { - uint32_t info; /* Primary info. */ - uint32_t aux; /* Secondary info. */ - } s; - TValue nval; /* Number value. */ - GCstr *sval; /* String value. */ - } u; - ExpKind k; - BCPos t; /* True condition jump list. */ - BCPos f; /* False condition jump list. */ -} ExpDesc; - -/* Macros for expressions. */ -#define expr_hasjump(e) ((e)->t != (e)->f) - -#define expr_isk(e) ((e)->k <= VKLAST) -#define expr_isk_nojump(e) (expr_isk(e) && !expr_hasjump(e)) -#define expr_isnumk(e) ((e)->k == VKNUM) -#define expr_isnumk_nojump(e) (expr_isnumk(e) && !expr_hasjump(e)) -#define expr_isstrk(e) ((e)->k == VKSTR) - -#define expr_numtv(e) check_exp(expr_isnumk((e)), &(e)->u.nval) -#define expr_numberV(e) numberVnum(expr_numtv((e))) - -/* Initialize expression. */ -static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info) -{ - e->k = k; - e->u.s.info = info; - e->f = e->t = NO_JMP; -} - -/* Check number constant for +-0. */ -static int expr_numiszero(ExpDesc *e) -{ - TValue *o = expr_numtv(e); - return tvisint(o) ? (intV(o) == 0) : tviszero(o); -} - -/* Per-function linked list of scope blocks. */ -typedef struct FuncScope { - struct FuncScope *prev; /* Link to outer scope. */ - MSize vstart; /* Start of block-local variables. */ - uint8_t nactvar; /* Number of active vars outside the scope. */ - uint8_t flags; /* Scope flags. */ -} FuncScope; - -#define FSCOPE_LOOP 0x01 /* Scope is a (breakable) loop. */ -#define FSCOPE_BREAK 0x02 /* Break used in scope. */ -#define FSCOPE_GOLA 0x04 /* Goto or label used in scope. */ -#define FSCOPE_UPVAL 0x08 /* Upvalue in scope. */ -#define FSCOPE_NOCLOSE 0x10 /* Do not close upvalues. */ - -#define NAME_BREAK ((GCstr *)(uintptr_t)1) - -/* Index into variable stack. */ -typedef uint16_t VarIndex; -#define LJ_MAX_VSTACK (65536 - LJ_MAX_UPVAL) - -/* Variable/goto/label info. */ -#define VSTACK_VAR_RW 0x01 /* R/W variable. */ -#define VSTACK_GOTO 0x02 /* Pending goto. */ -#define VSTACK_LABEL 0x04 /* Label. */ - -/* Per-function state. */ -typedef struct FuncState { - GCtab *kt; /* Hash table for constants. */ - LexState *ls; /* Lexer state. */ - lua_State *L; /* Lua state. */ - FuncScope *bl; /* Current scope. */ - struct FuncState *prev; /* Enclosing function. */ - BCPos pc; /* Next bytecode position. */ - BCPos lasttarget; /* Bytecode position of last jump target. */ - BCPos jpc; /* Pending jump list to next bytecode. */ - BCReg freereg; /* First free register. */ - BCReg nactvar; /* Number of active local variables. */ - BCReg nkn, nkgc; /* Number of lua_Number/GCobj constants */ - BCLine linedefined; /* First line of the function definition. */ - BCInsLine *bcbase; /* Base of bytecode stack. */ - BCPos bclim; /* Limit of bytecode stack. */ - MSize vbase; /* Base of variable stack for this function. */ - uint8_t flags; /* Prototype flags. */ - uint8_t numparams; /* Number of parameters. */ - uint8_t framesize; /* Fixed frame size. */ - uint8_t nuv; /* Number of upvalues */ - VarIndex varmap[LJ_MAX_LOCVAR]; /* Map from register to variable idx. */ - VarIndex uvmap[LJ_MAX_UPVAL]; /* Map from upvalue to variable idx. */ - VarIndex uvtmp[LJ_MAX_UPVAL]; /* Temporary upvalue map. */ -} FuncState; - -/* Binary and unary operators. ORDER OPR */ -typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, /* ORDER ARITH */ - OPR_CONCAT, - OPR_NE, OPR_EQ, - OPR_LT, OPR_GE, OPR_LE, OPR_GT, - OPR_AND, OPR_OR, - OPR_NOBINOPR -} BinOpr; - -LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT); -LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT); -LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT); -LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD); -LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD); -LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD); -LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD); - -#ifdef LUA_USE_ASSERT -#define lj_assertFS(c, ...) (lj_assertG_(G(fs->L), (c), __VA_ARGS__)) -#else -#define lj_assertFS(c, ...) ((void)fs) -#endif - -/* -- Error handling ------------------------------------------------------ */ - -LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em) -{ - lj_lex_error(ls, ls->tok, em); -} - -LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken tok) -{ - lj_lex_error(ls, ls->tok, LJ_ERR_XTOKEN, lj_lex_token2str(ls, tok)); -} - -LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what) -{ - if (fs->linedefined == 0) - lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what); - else - lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what); -} - -#define checklimit(fs, v, l, m) if ((v) >= (l)) err_limit(fs, l, m) -#define checklimitgt(fs, v, l, m) if ((v) > (l)) err_limit(fs, l, m) -#define checkcond(ls, c, em) { if (!(c)) err_syntax(ls, em); } - -/* -- Management of constants --------------------------------------------- */ - -/* Return bytecode encoding for primitive constant. */ -#define const_pri(e) check_exp((e)->k <= VKTRUE, (e)->k) - -#define tvhaskslot(o) ((o)->u32.hi == 0) -#define tvkslot(o) ((o)->u32.lo) - -/* Add a number constant. */ -static BCReg const_num(FuncState *fs, ExpDesc *e) -{ - lua_State *L = fs->L; - TValue *o; - lj_assertFS(expr_isnumk(e), "bad usage"); - o = lj_tab_set(L, fs->kt, &e->u.nval); - if (tvhaskslot(o)) - return tvkslot(o); - o->u64 = fs->nkn; - return fs->nkn++; -} - -/* Add a GC object constant. */ -static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype) -{ - lua_State *L = fs->L; - TValue key, *o; - setgcV(L, &key, gc, itype); - /* NOBARRIER: the key is new or kept alive. */ - o = lj_tab_set(L, fs->kt, &key); - if (tvhaskslot(o)) - return tvkslot(o); - o->u64 = fs->nkgc; - return fs->nkgc++; -} - -/* Add a string constant. */ -static BCReg const_str(FuncState *fs, ExpDesc *e) -{ - lj_assertFS(expr_isstrk(e) || e->k == VGLOBAL, "bad usage"); - return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR); -} - -/* Anchor string constant to avoid GC. */ -GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len) -{ - /* NOBARRIER: the key is new or kept alive. */ - lua_State *L = ls->L; - GCstr *s = lj_str_new(L, str, len); - TValue *tv = lj_tab_setstr(L, ls->fs->kt, s); - if (tvisnil(tv)) setboolV(tv, 1); - lj_gc_check(L); - return s; -} - -#if LJ_HASFFI -/* Anchor cdata to avoid GC. */ -void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd) -{ - /* NOBARRIER: the key is new or kept alive. */ - lua_State *L = ls->L; - setcdataV(L, tv, cd); - setboolV(lj_tab_set(L, ls->fs->kt, tv), 1); -} -#endif - -/* -- Jump list handling -------------------------------------------------- */ - -/* Get next element in jump list. */ -static BCPos jmp_next(FuncState *fs, BCPos pc) -{ - ptrdiff_t delta = bc_j(fs->bcbase[pc].ins); - if ((BCPos)delta == NO_JMP) - return NO_JMP; - else - return (BCPos)(((ptrdiff_t)pc+1)+delta); -} - -/* Check if any of the instructions on the jump list produce no value. */ -static int jmp_novalue(FuncState *fs, BCPos list) -{ - for (; list != NO_JMP; list = jmp_next(fs, list)) { - BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins; - if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG)) - return 1; - } - return 0; -} - -/* Patch register of test instructions. */ -static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg) -{ - BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc]; - BCOp op = bc_op(ilp->ins); - if (op == BC_ISTC || op == BC_ISFC) { - if (reg != NO_REG && reg != bc_d(ilp->ins)) { - setbc_a(&ilp->ins, reg); - } else { /* Nothing to store or already in the right register. */ - setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC)); - setbc_a(&ilp->ins, 0); - } - } else if (bc_a(ilp->ins) == NO_REG) { - if (reg == NO_REG) { - ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0); - } else { - setbc_a(&ilp->ins, reg); - if (reg >= bc_a(ilp[1].ins)) - setbc_a(&ilp[1].ins, reg+1); - } - } else { - return 0; /* Cannot patch other instructions. */ - } - return 1; -} - -/* Drop values for all instructions on jump list. */ -static void jmp_dropval(FuncState *fs, BCPos list) -{ - for (; list != NO_JMP; list = jmp_next(fs, list)) - jmp_patchtestreg(fs, list, NO_REG); -} - -/* Patch jump instruction to target. */ -static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest) -{ - BCIns *jmp = &fs->bcbase[pc].ins; - BCPos offset = dest-(pc+1)+BCBIAS_J; - lj_assertFS(dest != NO_JMP, "uninitialized jump target"); - if (offset > BCMAX_D) - err_syntax(fs->ls, LJ_ERR_XJUMP); - setbc_d(jmp, offset); -} - -/* Append to jump list. */ -static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2) -{ - if (l2 == NO_JMP) { - return; - } else if (*l1 == NO_JMP) { - *l1 = l2; - } else { - BCPos list = *l1; - BCPos next; - while ((next = jmp_next(fs, list)) != NO_JMP) /* Find last element. */ - list = next; - jmp_patchins(fs, list, l2); - } -} - -/* Patch jump list and preserve produced values. */ -static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget, - BCReg reg, BCPos dtarget) -{ - while (list != NO_JMP) { - BCPos next = jmp_next(fs, list); - if (jmp_patchtestreg(fs, list, reg)) - jmp_patchins(fs, list, vtarget); /* Jump to target with value. */ - else - jmp_patchins(fs, list, dtarget); /* Jump to default target. */ - list = next; - } -} - -/* Jump to following instruction. Append to list of pending jumps. */ -static void jmp_tohere(FuncState *fs, BCPos list) -{ - fs->lasttarget = fs->pc; - jmp_append(fs, &fs->jpc, list); -} - -/* Patch jump list to target. */ -static void jmp_patch(FuncState *fs, BCPos list, BCPos target) -{ - if (target == fs->pc) { - jmp_tohere(fs, list); - } else { - lj_assertFS(target < fs->pc, "bad jump target"); - jmp_patchval(fs, list, target, NO_REG, target); - } -} - -/* -- Bytecode register allocator ----------------------------------------- */ - -/* Bump frame size. */ -static void bcreg_bump(FuncState *fs, BCReg n) -{ - BCReg sz = fs->freereg + n; - if (sz > fs->framesize) { - if (sz >= LJ_MAX_SLOTS) - err_syntax(fs->ls, LJ_ERR_XSLOTS); - fs->framesize = (uint8_t)sz; - } -} - -/* Reserve registers. */ -static void bcreg_reserve(FuncState *fs, BCReg n) -{ - bcreg_bump(fs, n); - fs->freereg += n; -} - -/* Free register. */ -static void bcreg_free(FuncState *fs, BCReg reg) -{ - if (reg >= fs->nactvar) { - fs->freereg--; - lj_assertFS(reg == fs->freereg, "bad regfree"); - } -} - -/* Free register for expression. */ -static void expr_free(FuncState *fs, ExpDesc *e) -{ - if (e->k == VNONRELOC) - bcreg_free(fs, e->u.s.info); -} - -/* -- Bytecode emitter ---------------------------------------------------- */ - -/* Emit bytecode instruction. */ -static BCPos bcemit_INS(FuncState *fs, BCIns ins) -{ - BCPos pc = fs->pc; - LexState *ls = fs->ls; - jmp_patchval(fs, fs->jpc, pc, NO_REG, pc); - fs->jpc = NO_JMP; - if (LJ_UNLIKELY(pc >= fs->bclim)) { - ptrdiff_t base = fs->bcbase - ls->bcstack; - checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions"); - lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine); - fs->bclim = (BCPos)(ls->sizebcstack - base); - fs->bcbase = ls->bcstack + base; - } - fs->bcbase[pc].ins = ins; - fs->bcbase[pc].line = ls->lastline; - fs->pc = pc+1; - return pc; -} - -#define bcemit_ABC(fs, o, a, b, c) bcemit_INS(fs, BCINS_ABC(o, a, b, c)) -#define bcemit_AD(fs, o, a, d) bcemit_INS(fs, BCINS_AD(o, a, d)) -#define bcemit_AJ(fs, o, a, j) bcemit_INS(fs, BCINS_AJ(o, a, j)) - -#define bcptr(fs, e) (&(fs)->bcbase[(e)->u.s.info].ins) - -/* -- Bytecode emitter for expressions ------------------------------------ */ - -/* Discharge non-constant expression to any register. */ -static void expr_discharge(FuncState *fs, ExpDesc *e) -{ - BCIns ins; - if (e->k == VUPVAL) { - ins = BCINS_AD(BC_UGET, 0, e->u.s.info); - } else if (e->k == VGLOBAL) { - ins = BCINS_AD(BC_GGET, 0, const_str(fs, e)); - } else if (e->k == VINDEXED) { - BCReg rc = e->u.s.aux; - if ((int32_t)rc < 0) { - ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc); - } else if (rc > BCMAX_C) { - ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1)); - } else { - bcreg_free(fs, rc); - ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc); - } - bcreg_free(fs, e->u.s.info); - } else if (e->k == VCALL) { - e->u.s.info = e->u.s.aux; - e->k = VNONRELOC; - return; - } else if (e->k == VLOCAL) { - e->k = VNONRELOC; - return; - } else { - return; - } - e->u.s.info = bcemit_INS(fs, ins); - e->k = VRELOCABLE; -} - -/* Emit bytecode to set a range of registers to nil. */ -static void bcemit_nil(FuncState *fs, BCReg from, BCReg n) -{ - if (fs->pc > fs->lasttarget) { /* No jumps to current position? */ - BCIns *ip = &fs->bcbase[fs->pc-1].ins; - BCReg pto, pfrom = bc_a(*ip); - switch (bc_op(*ip)) { /* Try to merge with the previous instruction. */ - case BC_KPRI: - if (bc_d(*ip) != ~LJ_TNIL) break; - if (from == pfrom) { - if (n == 1) return; - } else if (from == pfrom+1) { - from = pfrom; - n++; - } else { - break; - } - *ip = BCINS_AD(BC_KNIL, from, from+n-1); /* Replace KPRI. */ - return; - case BC_KNIL: - pto = bc_d(*ip); - if (pfrom <= from && from <= pto+1) { /* Can we connect both ranges? */ - if (from+n-1 > pto) - setbc_d(ip, from+n-1); /* Patch previous instruction range. */ - return; - } - break; - default: - break; - } - } - /* Emit new instruction or replace old instruction. */ - bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) : - BCINS_AD(BC_KNIL, from, from+n-1)); -} - -/* Discharge an expression to a specific register. Ignore branches. */ -static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg) -{ - BCIns ins; - expr_discharge(fs, e); - if (e->k == VKSTR) { - ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e)); - } else if (e->k == VKNUM) { -#if LJ_DUALNUM - cTValue *tv = expr_numtv(e); - if (tvisint(tv) && checki16(intV(tv))) - ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv)); - else -#else - lua_Number n = expr_numberV(e); - int32_t k = lj_num2int(n); - if (checki16(k) && n == (lua_Number)k) - ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k); - else -#endif - ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e)); -#if LJ_HASFFI - } else if (e->k == VKCDATA) { - fs->flags |= PROTO_FFI; - ins = BCINS_AD(BC_KCDATA, reg, - const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA)); -#endif - } else if (e->k == VRELOCABLE) { - setbc_a(bcptr(fs, e), reg); - goto noins; - } else if (e->k == VNONRELOC) { - if (reg == e->u.s.info) - goto noins; - ins = BCINS_AD(BC_MOV, reg, e->u.s.info); - } else if (e->k == VKNIL) { - bcemit_nil(fs, reg, 1); - goto noins; - } else if (e->k <= VKTRUE) { - ins = BCINS_AD(BC_KPRI, reg, const_pri(e)); - } else { - lj_assertFS(e->k == VVOID || e->k == VJMP, "bad expr type %d", e->k); - return; - } - bcemit_INS(fs, ins); -noins: - e->u.s.info = reg; - e->k = VNONRELOC; -} - -/* Forward declaration. */ -static BCPos bcemit_jmp(FuncState *fs); - -/* Discharge an expression to a specific register. */ -static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg) -{ - expr_toreg_nobranch(fs, e, reg); - if (e->k == VJMP) - jmp_append(fs, &e->t, e->u.s.info); /* Add it to the true jump list. */ - if (expr_hasjump(e)) { /* Discharge expression with branches. */ - BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP; - if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) { - BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs); - jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE); - bcemit_AJ(fs, BC_JMP, fs->freereg, 1); - jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE); - jmp_tohere(fs, jval); - } - jend = fs->pc; - fs->lasttarget = jend; - jmp_patchval(fs, e->f, jend, reg, jfalse); - jmp_patchval(fs, e->t, jend, reg, jtrue); - } - e->f = e->t = NO_JMP; - e->u.s.info = reg; - e->k = VNONRELOC; -} - -/* Discharge an expression to the next free register. */ -static void expr_tonextreg(FuncState *fs, ExpDesc *e) -{ - expr_discharge(fs, e); - expr_free(fs, e); - bcreg_reserve(fs, 1); - expr_toreg(fs, e, fs->freereg - 1); -} - -/* Discharge an expression to any register. */ -static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e) -{ - expr_discharge(fs, e); - if (e->k == VNONRELOC) { - if (!expr_hasjump(e)) return e->u.s.info; /* Already in a register. */ - if (e->u.s.info >= fs->nactvar) { - expr_toreg(fs, e, e->u.s.info); /* Discharge to temp. register. */ - return e->u.s.info; - } - } - expr_tonextreg(fs, e); /* Discharge to next register. */ - return e->u.s.info; -} - -/* Partially discharge expression to a value. */ -static void expr_toval(FuncState *fs, ExpDesc *e) -{ - if (expr_hasjump(e)) - expr_toanyreg(fs, e); - else - expr_discharge(fs, e); -} - -/* Emit store for LHS expression. */ -static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e) -{ - BCIns ins; - if (var->k == VLOCAL) { - fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW; - expr_free(fs, e); - expr_toreg(fs, e, var->u.s.info); - return; - } else if (var->k == VUPVAL) { - fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW; - expr_toval(fs, e); - if (e->k <= VKTRUE) - ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e)); - else if (e->k == VKSTR) - ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e)); - else if (e->k == VKNUM) - ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e)); - else - ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e)); - } else if (var->k == VGLOBAL) { - BCReg ra = expr_toanyreg(fs, e); - ins = BCINS_AD(BC_GSET, ra, const_str(fs, var)); - } else { - BCReg ra, rc; - lj_assertFS(var->k == VINDEXED, "bad expr type %d", var->k); - ra = expr_toanyreg(fs, e); - rc = var->u.s.aux; - if ((int32_t)rc < 0) { - ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc); - } else if (rc > BCMAX_C) { - ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1)); - } else { -#ifdef LUA_USE_ASSERT - /* Free late alloced key reg to avoid assert on free of value reg. */ - /* This can only happen when called from expr_table(). */ - if (e->k == VNONRELOC && ra >= fs->nactvar && rc >= ra) - bcreg_free(fs, rc); -#endif - ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc); - } - } - bcemit_INS(fs, ins); - expr_free(fs, e); -} - -/* Emit method lookup expression. */ -static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key) -{ - BCReg idx, func, obj = expr_toanyreg(fs, e); - expr_free(fs, e); - func = fs->freereg; - bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj); /* Copy object to 1st argument. */ - lj_assertFS(expr_isstrk(key), "bad usage"); - idx = const_str(fs, key); - if (idx <= BCMAX_C) { - bcreg_reserve(fs, 2+LJ_FR2); - bcemit_ABC(fs, BC_TGETS, func, obj, idx); - } else { - bcreg_reserve(fs, 3+LJ_FR2); - bcemit_AD(fs, BC_KSTR, func+2+LJ_FR2, idx); - bcemit_ABC(fs, BC_TGETV, func, obj, func+2+LJ_FR2); - fs->freereg--; - } - e->u.s.info = func; - e->k = VNONRELOC; -} - -/* -- Bytecode emitter for branches --------------------------------------- */ - -/* Emit unconditional branch. */ -static BCPos bcemit_jmp(FuncState *fs) -{ - BCPos jpc = fs->jpc; - BCPos j = fs->pc - 1; - BCIns *ip = &fs->bcbase[j].ins; - fs->jpc = NO_JMP; - if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) { - setbc_j(ip, NO_JMP); - fs->lasttarget = j+1; - } else { - j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP); - } - jmp_append(fs, &j, jpc); - return j; -} - -/* Invert branch condition of bytecode instruction. */ -static void invertcond(FuncState *fs, ExpDesc *e) -{ - BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins; - setbc_op(ip, bc_op(*ip)^1); -} - -/* Emit conditional branch. */ -static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond) -{ - BCPos pc; - if (e->k == VRELOCABLE) { - BCIns *ip = bcptr(fs, e); - if (bc_op(*ip) == BC_NOT) { - *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip)); - return bcemit_jmp(fs); - } - } - if (e->k != VNONRELOC) { - bcreg_reserve(fs, 1); - expr_toreg_nobranch(fs, e, fs->freereg-1); - } - bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info); - pc = bcemit_jmp(fs); - expr_free(fs, e); - return pc; -} - -/* Emit branch on true condition. */ -static void bcemit_branch_t(FuncState *fs, ExpDesc *e) -{ - BCPos pc; - expr_discharge(fs, e); - if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE) - pc = NO_JMP; /* Never jump. */ - else if (e->k == VJMP) - invertcond(fs, e), pc = e->u.s.info; - else if (e->k == VKFALSE || e->k == VKNIL) - expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs); - else - pc = bcemit_branch(fs, e, 0); - jmp_append(fs, &e->f, pc); - jmp_tohere(fs, e->t); - e->t = NO_JMP; -} - -/* Emit branch on false condition. */ -static void bcemit_branch_f(FuncState *fs, ExpDesc *e) -{ - BCPos pc; - expr_discharge(fs, e); - if (e->k == VKNIL || e->k == VKFALSE) - pc = NO_JMP; /* Never jump. */ - else if (e->k == VJMP) - pc = e->u.s.info; - else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE) - expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs); - else - pc = bcemit_branch(fs, e, 1); - jmp_append(fs, &e->t, pc); - jmp_tohere(fs, e->f); - e->f = NO_JMP; -} - -/* -- Bytecode emitter for operators -------------------------------------- */ - -/* Try constant-folding of arithmetic operators. */ -static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2) -{ - TValue o; - lua_Number n; - if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0; - n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD); - setnumV(&o, n); - if (tvisnan(&o) || tvismzero(&o)) return 0; /* Avoid NaN and -0 as consts. */ - if (LJ_DUALNUM) { - int32_t k = lj_num2int(n); - if ((lua_Number)k == n) { - setintV(&e1->u.nval, k); - return 1; - } - } - setnumV(&e1->u.nval, n); - return 1; -} - -/* Emit arithmetic operator. */ -static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2) -{ - BCReg rb, rc, t; - uint32_t op; - if (foldarith(opr, e1, e2)) - return; - if (opr == OPR_POW) { - op = BC_POW; - rc = expr_toanyreg(fs, e2); - rb = expr_toanyreg(fs, e1); - } else { - op = opr-OPR_ADD+BC_ADDVV; - /* Must discharge 2nd operand first since VINDEXED might free regs. */ - expr_toval(fs, e2); - if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C) - op -= BC_ADDVV-BC_ADDVN; - else - rc = expr_toanyreg(fs, e2); - /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */ - lj_assertFS(expr_isnumk(e1) || e1->k == VNONRELOC, - "bad expr type %d", e1->k); - expr_toval(fs, e1); - /* Avoid two consts to satisfy bytecode constraints. */ - if (expr_isnumk(e1) && !expr_isnumk(e2) && - (t = const_num(fs, e1)) <= BCMAX_B) { - rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV; - } else { - rb = expr_toanyreg(fs, e1); - } - } - /* Using expr_free might cause asserts if the order is wrong. */ - if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--; - if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--; - e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc); - e1->k = VRELOCABLE; -} - -/* Emit comparison operator. */ -static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2) -{ - ExpDesc *eret = e1; - BCIns ins; - expr_toval(fs, e1); - if (opr == OPR_EQ || opr == OPR_NE) { - BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV; - BCReg ra; - if (expr_isk(e1)) { e1 = e2; e2 = eret; } /* Need constant in 2nd arg. */ - ra = expr_toanyreg(fs, e1); /* First arg must be in a reg. */ - expr_toval(fs, e2); - switch (e2->k) { - case VKNIL: case VKFALSE: case VKTRUE: - ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2)); - break; - case VKSTR: - ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2)); - break; - case VKNUM: - ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2)); - break; - default: - ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2)); - break; - } - } else { - uint32_t op = opr-OPR_LT+BC_ISLT; - BCReg ra, rd; - if ((op-BC_ISLT) & 1) { /* GT -> LT, GE -> LE */ - e1 = e2; e2 = eret; /* Swap operands. */ - op = ((op-BC_ISLT)^3)+BC_ISLT; - expr_toval(fs, e1); - ra = expr_toanyreg(fs, e1); - rd = expr_toanyreg(fs, e2); - } else { - rd = expr_toanyreg(fs, e2); - ra = expr_toanyreg(fs, e1); - } - ins = BCINS_AD(op, ra, rd); - } - /* Using expr_free might cause asserts if the order is wrong. */ - if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--; - if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--; - bcemit_INS(fs, ins); - eret->u.s.info = bcemit_jmp(fs); - eret->k = VJMP; -} - -/* Fixup left side of binary operator. */ -static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e) -{ - if (op == OPR_AND) { - bcemit_branch_t(fs, e); - } else if (op == OPR_OR) { - bcemit_branch_f(fs, e); - } else if (op == OPR_CONCAT) { - expr_tonextreg(fs, e); - } else if (op == OPR_EQ || op == OPR_NE) { - if (!expr_isk_nojump(e)) expr_toanyreg(fs, e); - } else { - if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e); - } -} - -/* Emit binary operator. */ -static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2) -{ - if (op <= OPR_POW) { - bcemit_arith(fs, op, e1, e2); - } else if (op == OPR_AND) { - lj_assertFS(e1->t == NO_JMP, "jump list not closed"); - expr_discharge(fs, e2); - jmp_append(fs, &e2->f, e1->f); - *e1 = *e2; - } else if (op == OPR_OR) { - lj_assertFS(e1->f == NO_JMP, "jump list not closed"); - expr_discharge(fs, e2); - jmp_append(fs, &e2->t, e1->t); - *e1 = *e2; - } else if (op == OPR_CONCAT) { - expr_toval(fs, e2); - if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) { - lj_assertFS(e1->u.s.info == bc_b(*bcptr(fs, e2))-1, - "bad CAT stack layout"); - expr_free(fs, e1); - setbc_b(bcptr(fs, e2), e1->u.s.info); - e1->u.s.info = e2->u.s.info; - } else { - expr_tonextreg(fs, e2); - expr_free(fs, e2); - expr_free(fs, e1); - e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info); - } - e1->k = VRELOCABLE; - } else { - lj_assertFS(op == OPR_NE || op == OPR_EQ || - op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT, - "bad binop %d", op); - bcemit_comp(fs, op, e1, e2); - } -} - -/* Emit unary operator. */ -static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e) -{ - if (op == BC_NOT) { - /* Swap true and false lists. */ - { BCPos temp = e->f; e->f = e->t; e->t = temp; } - jmp_dropval(fs, e->f); - jmp_dropval(fs, e->t); - expr_discharge(fs, e); - if (e->k == VKNIL || e->k == VKFALSE) { - e->k = VKTRUE; - return; - } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) { - e->k = VKFALSE; - return; - } else if (e->k == VJMP) { - invertcond(fs, e); - return; - } else if (e->k == VRELOCABLE) { - bcreg_reserve(fs, 1); - setbc_a(bcptr(fs, e), fs->freereg-1); - e->u.s.info = fs->freereg-1; - e->k = VNONRELOC; - } else { - lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k); - } - } else { - lj_assertFS(op == BC_UNM || op == BC_LEN, "bad unop %d", op); - if (op == BC_UNM && !expr_hasjump(e)) { /* Constant-fold negations. */ -#if LJ_HASFFI - if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */ - GCcdata *cd = cdataV(&e->u.nval); - int64_t *p = (int64_t *)cdataptr(cd); - if (cd->ctypeid == CTID_COMPLEX_DOUBLE) - p[1] ^= (int64_t)U64x(80000000,00000000); - else - *p = -*p; - return; - } else -#endif - if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */ - TValue *o = expr_numtv(e); - if (tvisint(o)) { - int32_t k = intV(o); - if (k == -k) - setnumV(o, -(lua_Number)k); - else - setintV(o, -k); - return; - } else { - o->u64 ^= U64x(80000000,00000000); - return; - } - } - } - expr_toanyreg(fs, e); - } - expr_free(fs, e); - e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info); - e->k = VRELOCABLE; -} - -/* -- Lexer support ------------------------------------------------------- */ - -/* Check and consume optional token. */ -static int lex_opt(LexState *ls, LexToken tok) -{ - if (ls->tok == tok) { - lj_lex_next(ls); - return 1; - } - return 0; -} - -/* Check and consume token. */ -static void lex_check(LexState *ls, LexToken tok) -{ - if (ls->tok != tok) - err_token(ls, tok); - lj_lex_next(ls); -} - -/* Check for matching token. */ -static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line) -{ - if (!lex_opt(ls, what)) { - if (line == ls->linenumber) { - err_token(ls, what); - } else { - const char *swhat = lj_lex_token2str(ls, what); - const char *swho = lj_lex_token2str(ls, who); - lj_lex_error(ls, ls->tok, LJ_ERR_XMATCH, swhat, swho, line); - } - } -} - -/* Check for string token. */ -static GCstr *lex_str(LexState *ls) -{ - GCstr *s; - if (ls->tok != TK_name && (LJ_52 || ls->tok != TK_goto)) - err_token(ls, TK_name); - s = strV(&ls->tokval); - lj_lex_next(ls); - return s; -} - -/* -- Variable handling --------------------------------------------------- */ - -#define var_get(ls, fs, i) ((ls)->vstack[(fs)->varmap[(i)]]) - -/* Define a new local variable. */ -static void var_new(LexState *ls, BCReg n, GCstr *name) -{ - FuncState *fs = ls->fs; - MSize vtop = ls->vtop; - checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables"); - if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { - if (ls->sizevstack >= LJ_MAX_VSTACK) - lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); - lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); - } - lj_assertFS((uintptr_t)name < VARNAME__MAX || - lj_tab_getstr(fs->kt, name) != NULL, - "unanchored variable name"); - /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ - setgcref(ls->vstack[vtop].name, obj2gco(name)); - fs->varmap[fs->nactvar+n] = (uint16_t)vtop; - ls->vtop = vtop+1; -} - -#define var_new_lit(ls, n, v) \ - var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1)) - -#define var_new_fixed(ls, n, vn) \ - var_new(ls, (n), (GCstr *)(uintptr_t)(vn)) - -/* Add local variables. */ -static void var_add(LexState *ls, BCReg nvars) -{ - FuncState *fs = ls->fs; - BCReg nactvar = fs->nactvar; - while (nvars--) { - VarInfo *v = &var_get(ls, fs, nactvar); - v->startpc = fs->pc; - v->slot = nactvar++; - v->info = 0; - } - fs->nactvar = nactvar; -} - -/* Remove local variables. */ -static void var_remove(LexState *ls, BCReg tolevel) -{ - FuncState *fs = ls->fs; - while (fs->nactvar > tolevel) - var_get(ls, fs, --fs->nactvar).endpc = fs->pc; -} - -/* Lookup local variable name. */ -static BCReg var_lookup_local(FuncState *fs, GCstr *n) -{ - int i; - for (i = fs->nactvar-1; i >= 0; i--) { - if (n == strref(var_get(fs->ls, fs, i).name)) - return (BCReg)i; - } - return (BCReg)-1; /* Not found. */ -} - -/* Lookup or add upvalue index. */ -static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e) -{ - MSize i, n = fs->nuv; - for (i = 0; i < n; i++) - if (fs->uvmap[i] == vidx) - return i; /* Already exists. */ - /* Otherwise create a new one. */ - checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues"); - lj_assertFS(e->k == VLOCAL || e->k == VUPVAL, "bad expr type %d", e->k); - fs->uvmap[n] = (uint16_t)vidx; - fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info); - fs->nuv = n+1; - return n; -} - -/* Forward declaration. */ -static void fscope_uvmark(FuncState *fs, BCReg level); - -/* Recursively lookup variables in enclosing functions. */ -static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first) -{ - if (fs) { - BCReg reg = var_lookup_local(fs, name); - if ((int32_t)reg >= 0) { /* Local in this function? */ - expr_init(e, VLOCAL, reg); - if (!first) - fscope_uvmark(fs, reg); /* Scope now has an upvalue. */ - return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]); - } else { - MSize vidx = var_lookup_(fs->prev, name, e, 0); /* Var in outer func? */ - if ((int32_t)vidx >= 0) { /* Yes, make it an upvalue here. */ - e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e); - e->k = VUPVAL; - return vidx; - } - } - } else { /* Not found in any function, must be a global. */ - expr_init(e, VGLOBAL, 0); - e->u.sval = name; - } - return (MSize)-1; /* Global. */ -} - -/* Lookup variable name. */ -#define var_lookup(ls, e) \ - var_lookup_((ls)->fs, lex_str(ls), (e), 1) - -/* -- Goto an label handling ---------------------------------------------- */ - -/* Add a new goto or label. */ -static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc) -{ - FuncState *fs = ls->fs; - MSize vtop = ls->vtop; - if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { - if (ls->sizevstack >= LJ_MAX_VSTACK) - lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); - lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); - } - lj_assertFS(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL, - "unanchored label name"); - /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ - setgcref(ls->vstack[vtop].name, obj2gco(name)); - ls->vstack[vtop].startpc = pc; - ls->vstack[vtop].slot = (uint8_t)fs->nactvar; - ls->vstack[vtop].info = info; - ls->vtop = vtop+1; - return vtop; -} - -#define gola_isgoto(v) ((v)->info & VSTACK_GOTO) -#define gola_islabel(v) ((v)->info & VSTACK_LABEL) -#define gola_isgotolabel(v) ((v)->info & (VSTACK_GOTO|VSTACK_LABEL)) - -/* Patch goto to jump to label. */ -static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl) -{ - FuncState *fs = ls->fs; - BCPos pc = vg->startpc; - setgcrefnull(vg->name); /* Invalidate pending goto. */ - setbc_a(&fs->bcbase[pc].ins, vl->slot); - jmp_patch(fs, pc, vl->startpc); -} - -/* Patch goto to close upvalues. */ -static void gola_close(LexState *ls, VarInfo *vg) -{ - FuncState *fs = ls->fs; - BCPos pc = vg->startpc; - BCIns *ip = &fs->bcbase[pc].ins; - lj_assertFS(gola_isgoto(vg), "expected goto"); - lj_assertFS(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO, - "bad bytecode op %d", bc_op(*ip)); - setbc_a(ip, vg->slot); - if (bc_op(*ip) == BC_JMP) { - BCPos next = jmp_next(fs, pc); - if (next != NO_JMP) jmp_patch(fs, next, pc); /* Jump to UCLO. */ - setbc_op(ip, BC_UCLO); /* Turn into UCLO. */ - setbc_j(ip, NO_JMP); - } -} - -/* Resolve pending forward gotos for label. */ -static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx) -{ - VarInfo *vg = ls->vstack + bl->vstart; - VarInfo *vl = ls->vstack + idx; - for (; vg < vl; vg++) - if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) { - if (vg->slot < vl->slot) { - GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name); - lj_assertLS((uintptr_t)name >= VARNAME__MAX, "expected goto name"); - ls->linenumber = ls->fs->bcbase[vg->startpc].line; - lj_assertLS(strref(vg->name) != NAME_BREAK, "unexpected break"); - lj_lex_error(ls, 0, LJ_ERR_XGSCOPE, - strdata(strref(vg->name)), strdata(name)); - } - gola_patch(ls, vg, vl); - } -} - -/* Fixup remaining gotos and labels for scope. */ -static void gola_fixup(LexState *ls, FuncScope *bl) -{ - VarInfo *v = ls->vstack + bl->vstart; - VarInfo *ve = ls->vstack + ls->vtop; - for (; v < ve; v++) { - GCstr *name = strref(v->name); - if (name != NULL) { /* Only consider remaining valid gotos/labels. */ - if (gola_islabel(v)) { - VarInfo *vg; - setgcrefnull(v->name); /* Invalidate label that goes out of scope. */ - for (vg = v+1; vg < ve; vg++) /* Resolve pending backward gotos. */ - if (strref(vg->name) == name && gola_isgoto(vg)) { - if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot) - gola_close(ls, vg); - gola_patch(ls, vg, v); - } - } else if (gola_isgoto(v)) { - if (bl->prev) { /* Propagate goto or break to outer scope. */ - bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA; - v->slot = bl->nactvar; - if ((bl->flags & FSCOPE_UPVAL)) - gola_close(ls, v); - } else { /* No outer scope: undefined goto label or no loop. */ - ls->linenumber = ls->fs->bcbase[v->startpc].line; - if (name == NAME_BREAK) - lj_lex_error(ls, 0, LJ_ERR_XBREAK); - else - lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name)); - } - } - } - } -} - -/* Find existing label. */ -static VarInfo *gola_findlabel(LexState *ls, GCstr *name) -{ - VarInfo *v = ls->vstack + ls->fs->bl->vstart; - VarInfo *ve = ls->vstack + ls->vtop; - for (; v < ve; v++) - if (strref(v->name) == name && gola_islabel(v)) - return v; - return NULL; -} - -/* -- Scope handling ------------------------------------------------------ */ - -/* Begin a scope. */ -static void fscope_begin(FuncState *fs, FuncScope *bl, int flags) -{ - bl->nactvar = (uint8_t)fs->nactvar; - bl->flags = flags; - bl->vstart = fs->ls->vtop; - bl->prev = fs->bl; - fs->bl = bl; - lj_assertFS(fs->freereg == fs->nactvar, "bad regalloc"); -} - -/* End a scope. */ -static void fscope_end(FuncState *fs) -{ - FuncScope *bl = fs->bl; - LexState *ls = fs->ls; - fs->bl = bl->prev; - var_remove(ls, bl->nactvar); - fs->freereg = fs->nactvar; - lj_assertFS(bl->nactvar == fs->nactvar, "bad regalloc"); - if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL) - bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0); - if ((bl->flags & FSCOPE_BREAK)) { - if ((bl->flags & FSCOPE_LOOP)) { - MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc); - ls->vtop = idx; /* Drop break label immediately. */ - gola_resolve(ls, bl, idx); - } else { /* Need the fixup step to propagate the breaks. */ - gola_fixup(ls, bl); - return; - } - } - if ((bl->flags & FSCOPE_GOLA)) { - gola_fixup(ls, bl); - } -} - -/* Mark scope as having an upvalue. */ -static void fscope_uvmark(FuncState *fs, BCReg level) -{ - FuncScope *bl; - for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev) - ; - if (bl) - bl->flags |= FSCOPE_UPVAL; -} - -/* -- Function state management ------------------------------------------- */ - -/* Fixup bytecode for prototype. */ -static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n) -{ - BCInsLine *base = fs->bcbase; - MSize i; - pt->sizebc = n; - bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF, - fs->framesize, 0); - for (i = 1; i < n; i++) - bc[i] = base[i].ins; -} - -/* Fixup upvalues for child prototype, step #2. */ -static void fs_fixup_uv2(FuncState *fs, GCproto *pt) -{ - VarInfo *vstack = fs->ls->vstack; - uint16_t *uv = proto_uv(pt); - MSize i, n = pt->sizeuv; - for (i = 0; i < n; i++) { - VarIndex vidx = uv[i]; - if (vidx >= LJ_MAX_VSTACK) - uv[i] = vidx - LJ_MAX_VSTACK; - else if ((vstack[vidx].info & VSTACK_VAR_RW)) - uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL; - else - uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE; - } -} - -/* Fixup constants for prototype. */ -static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr) -{ - GCtab *kt; - TValue *array; - Node *node; - MSize i, hmask; - checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants"); - checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants"); - setmref(pt->k, kptr); - pt->sizekn = fs->nkn; - pt->sizekgc = fs->nkgc; - kt = fs->kt; - array = tvref(kt->array); - for (i = 0; i < kt->asize; i++) - if (tvhaskslot(&array[i])) { - TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])]; - if (LJ_DUALNUM) - setintV(tv, (int32_t)i); - else - setnumV(tv, (lua_Number)i); - } - node = noderef(kt->node); - hmask = kt->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (tvhaskslot(&n->val)) { - ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val); - lj_assertFS(!tvisint(&n->key), "unexpected integer key"); - if (tvisnum(&n->key)) { - TValue *tv = &((TValue *)kptr)[kidx]; - if (LJ_DUALNUM) { - lua_Number nn = numV(&n->key); - int32_t k = lj_num2int(nn); - lj_assertFS(!tvismzero(&n->key), "unexpected -0 key"); - if ((lua_Number)k == nn) - setintV(tv, k); - else - *tv = n->key; - } else { - *tv = n->key; - } - } else { - GCobj *o = gcV(&n->key); - setgcref(((GCRef *)kptr)[~kidx], o); - lj_gc_objbarrier(fs->L, pt, o); - if (tvisproto(&n->key)) - fs_fixup_uv2(fs, gco2pt(o)); - } - } - } -} - -/* Fixup upvalues for prototype, step #1. */ -static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv) -{ - setmref(pt->uv, uv); - pt->sizeuv = fs->nuv; - memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex)); -} - -#ifndef LUAJIT_DISABLE_DEBUGINFO -/* Prepare lineinfo for prototype. */ -static size_t fs_prep_line(FuncState *fs, BCLine numline) -{ - return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); -} - -/* Fixup lineinfo for prototype. */ -static void fs_fixup_line(FuncState *fs, GCproto *pt, - void *lineinfo, BCLine numline) -{ - BCInsLine *base = fs->bcbase + 1; - BCLine first = fs->linedefined; - MSize i = 0, n = fs->pc-1; - pt->firstline = fs->linedefined; - pt->numline = numline; - setmref(pt->lineinfo, lineinfo); - if (LJ_LIKELY(numline < 256)) { - uint8_t *li = (uint8_t *)lineinfo; - do { - BCLine delta = base[i].line - first; - lj_assertFS(delta >= 0 && delta < 256, "bad line delta"); - li[i] = (uint8_t)delta; - } while (++i < n); - } else if (LJ_LIKELY(numline < 65536)) { - uint16_t *li = (uint16_t *)lineinfo; - do { - BCLine delta = base[i].line - first; - lj_assertFS(delta >= 0 && delta < 65536, "bad line delta"); - li[i] = (uint16_t)delta; - } while (++i < n); - } else { - uint32_t *li = (uint32_t *)lineinfo; - do { - BCLine delta = base[i].line - first; - lj_assertFS(delta >= 0, "bad line delta"); - li[i] = (uint32_t)delta; - } while (++i < n); - } -} - -/* Prepare variable info for prototype. */ -static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar) -{ - VarInfo *vs =ls->vstack, *ve; - MSize i, n; - BCPos lastpc; - lj_buf_reset(&ls->sb); /* Copy to temp. string buffer. */ - /* Store upvalue names. */ - for (i = 0, n = fs->nuv; i < n; i++) { - GCstr *s = strref(vs[fs->uvmap[i]].name); - MSize len = s->len+1; - char *p = lj_buf_more(&ls->sb, len); - p = lj_buf_wmem(p, strdata(s), len); - ls->sb.w = p; - } - *ofsvar = sbuflen(&ls->sb); - lastpc = 0; - /* Store local variable names and compressed ranges. */ - for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) { - if (!gola_isgotolabel(vs)) { - GCstr *s = strref(vs->name); - BCPos startpc; - char *p; - if ((uintptr_t)s < VARNAME__MAX) { - p = lj_buf_more(&ls->sb, 1 + 2*5); - *p++ = (char)(uintptr_t)s; - } else { - MSize len = s->len+1; - p = lj_buf_more(&ls->sb, len + 2*5); - p = lj_buf_wmem(p, strdata(s), len); - } - startpc = vs->startpc; - p = lj_strfmt_wuleb128(p, startpc-lastpc); - p = lj_strfmt_wuleb128(p, vs->endpc-startpc); - ls->sb.w = p; - lastpc = startpc; - } - } - lj_buf_putb(&ls->sb, '\0'); /* Terminator for varinfo. */ - return sbuflen(&ls->sb); -} - -/* Fixup variable info for prototype. */ -static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar) -{ - setmref(pt->uvinfo, p); - setmref(pt->varinfo, (char *)p + ofsvar); - memcpy(p, ls->sb.b, sbuflen(&ls->sb)); /* Copy from temp. buffer. */ -} -#else - -/* Initialize with empty debug info, if disabled. */ -#define fs_prep_line(fs, numline) (UNUSED(numline), 0) -#define fs_fixup_line(fs, pt, li, numline) \ - pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL) -#define fs_prep_var(ls, fs, ofsvar) (UNUSED(ofsvar), 0) -#define fs_fixup_var(ls, pt, p, ofsvar) \ - setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL) - -#endif - -/* Check if bytecode op returns. */ -static int bcopisret(BCOp op) -{ - switch (op) { - case BC_CALLMT: case BC_CALLT: - case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: - return 1; - default: - return 0; - } -} - -/* Fixup return instruction for prototype. */ -static void fs_fixup_ret(FuncState *fs) -{ - BCPos lastpc = fs->pc; - if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) { - if ((fs->bl->flags & FSCOPE_UPVAL)) - bcemit_AJ(fs, BC_UCLO, 0, 0); - bcemit_AD(fs, BC_RET0, 0, 1); /* Need final return. */ - } - fs->bl->flags |= FSCOPE_NOCLOSE; /* Handled above. */ - fscope_end(fs); - lj_assertFS(fs->bl == NULL, "bad scope nesting"); - /* May need to fixup returns encoded before first function was created. */ - if (fs->flags & PROTO_FIXUP_RETURN) { - BCPos pc; - for (pc = 1; pc < lastpc; pc++) { - BCIns ins = fs->bcbase[pc].ins; - BCPos offset; - switch (bc_op(ins)) { - case BC_CALLMT: case BC_CALLT: - case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: - offset = bcemit_INS(fs, ins); /* Copy original instruction. */ - fs->bcbase[offset].line = fs->bcbase[pc].line; - offset = offset-(pc+1)+BCBIAS_J; - if (offset > BCMAX_D) - err_syntax(fs->ls, LJ_ERR_XFIXUP); - /* Replace with UCLO plus branch. */ - fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset); - break; - case BC_FNEW: - return; /* We're done. */ - default: - break; - } - } - } -} - -/* Finish a FuncState and return the new prototype. */ -static GCproto *fs_finish(LexState *ls, BCLine line) -{ - lua_State *L = ls->L; - FuncState *fs = ls->fs; - BCLine numline = line - fs->linedefined; - size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar; - GCproto *pt; - - /* Apply final fixups. */ - fs_fixup_ret(fs); - - /* Calculate total size of prototype including all colocated arrays. */ - sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef); - sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1); - ofsk = sizept; sizept += fs->nkn*sizeof(TValue); - ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2; - ofsli = sizept; sizept += fs_prep_line(fs, numline); - ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar); - - /* Allocate prototype and initialize its fields. */ - pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept); - pt->gct = ~LJ_TPROTO; - pt->sizept = (MSize)sizept; - pt->trace = 0; - pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN)); - pt->numparams = fs->numparams; - pt->framesize = fs->framesize; - setgcref(pt->chunkname, obj2gco(ls->chunkname)); - - /* Close potentially uninitialized gap between bc and kgc. */ - *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0; - fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc); - fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk)); - fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv)); - fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline); - fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar); - - lj_vmevent_send(L, BC, - setprotoV(L, L->top++, pt); - ); - - L->top--; /* Pop table of constants. */ - ls->vtop = fs->vbase; /* Reset variable stack. */ - ls->fs = fs->prev; - lj_assertL(ls->fs != NULL || ls->tok == TK_eof, "bad parser state"); - return pt; -} - -/* Initialize a new FuncState. */ -static void fs_init(LexState *ls, FuncState *fs) -{ - lua_State *L = ls->L; - fs->prev = ls->fs; ls->fs = fs; /* Append to list. */ - fs->ls = ls; - fs->vbase = ls->vtop; - fs->L = L; - fs->pc = 0; - fs->lasttarget = 0; - fs->jpc = NO_JMP; - fs->freereg = 0; - fs->nkgc = 0; - fs->nkn = 0; - fs->nactvar = 0; - fs->nuv = 0; - fs->bl = NULL; - fs->flags = 0; - fs->framesize = 1; /* Minimum frame size. */ - fs->kt = lj_tab_new(L, 0, 0); - /* Anchor table of constants in stack to avoid being collected. */ - settabV(L, L->top, fs->kt); - incr_top(L); -} - -/* -- Expressions --------------------------------------------------------- */ - -/* Forward declaration. */ -static void expr(LexState *ls, ExpDesc *v); - -/* Return string expression. */ -static void expr_str(LexState *ls, ExpDesc *e) -{ - expr_init(e, VKSTR, 0); - e->u.sval = lex_str(ls); -} - -/* Return index expression. */ -static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e) -{ - /* Already called: expr_toval(fs, e). */ - t->k = VINDEXED; - if (expr_isnumk(e)) { -#if LJ_DUALNUM - if (tvisint(expr_numtv(e))) { - int32_t k = intV(expr_numtv(e)); - if (checku8(k)) { - t->u.s.aux = BCMAX_C+1+(uint32_t)k; /* 256..511: const byte key */ - return; - } - } -#else - lua_Number n = expr_numberV(e); - int32_t k = lj_num2int(n); - if (checku8(k) && n == (lua_Number)k) { - t->u.s.aux = BCMAX_C+1+(uint32_t)k; /* 256..511: const byte key */ - return; - } -#endif - } else if (expr_isstrk(e)) { - BCReg idx = const_str(fs, e); - if (idx <= BCMAX_C) { - t->u.s.aux = ~idx; /* -256..-1: const string key */ - return; - } - } - t->u.s.aux = expr_toanyreg(fs, e); /* 0..255: register */ -} - -/* Parse index expression with named field. */ -static void expr_field(LexState *ls, ExpDesc *v) -{ - FuncState *fs = ls->fs; - ExpDesc key; - expr_toanyreg(fs, v); - lj_lex_next(ls); /* Skip dot or colon. */ - expr_str(ls, &key); - expr_index(fs, v, &key); -} - -/* Parse index expression with brackets. */ -static void expr_bracket(LexState *ls, ExpDesc *v) -{ - lj_lex_next(ls); /* Skip '['. */ - expr(ls, v); - expr_toval(ls->fs, v); - lex_check(ls, ']'); -} - -/* Get value of constant expression. */ -static void expr_kvalue(FuncState *fs, TValue *v, ExpDesc *e) -{ - UNUSED(fs); - if (e->k <= VKTRUE) { - setpriV(v, ~(uint32_t)e->k); - } else if (e->k == VKSTR) { - setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR); - } else { - lj_assertFS(tvisnumber(expr_numtv(e)), "bad number constant"); - *v = *expr_numtv(e); - } -} - -/* Parse table constructor expression. */ -static void expr_table(LexState *ls, ExpDesc *e) -{ - FuncState *fs = ls->fs; - BCLine line = ls->linenumber; - GCtab *t = NULL; - int vcall = 0, needarr = 0, fixt = 0; - uint32_t narr = 1; /* First array index. */ - uint32_t nhash = 0; /* Number of hash entries. */ - BCReg freg = fs->freereg; - BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0); - expr_init(e, VNONRELOC, freg); - bcreg_reserve(fs, 1); - freg++; - lex_check(ls, '{'); - while (ls->tok != '}') { - ExpDesc key, val; - vcall = 0; - if (ls->tok == '[') { - expr_bracket(ls, &key); /* Already calls expr_toval. */ - if (!expr_isk(&key)) expr_index(fs, e, &key); - if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++; - lex_check(ls, '='); - } else if ((ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) && - lj_lex_lookahead(ls) == '=') { - expr_str(ls, &key); - lex_check(ls, '='); - nhash++; - } else { - expr_init(&key, VKNUM, 0); - setintV(&key.u.nval, (int)narr); - narr++; - needarr = vcall = 1; - } - expr(ls, &val); - if (expr_isk(&key) && key.k != VKNIL && - (key.k == VKSTR || expr_isk_nojump(&val))) { - TValue k, *v; - if (!t) { /* Create template table on demand. */ - BCReg kidx; - t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash)); - kidx = const_gc(fs, obj2gco(t), LJ_TTAB); - fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx); - } - vcall = 0; - expr_kvalue(fs, &k, &key); - v = lj_tab_set(fs->L, t, &k); - lj_gc_anybarriert(fs->L, t); - if (expr_isk_nojump(&val)) { /* Add const key/value to template table. */ - expr_kvalue(fs, v, &val); - } else { /* Otherwise create dummy string key (avoids lj_tab_newkey). */ - settabV(fs->L, v, t); /* Preserve key with table itself as value. */ - fixt = 1; /* Fix this later, after all resizes. */ - goto nonconst; - } - } else { - nonconst: - if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; } - if (expr_isk(&key)) expr_index(fs, e, &key); - bcemit_store(fs, e, &val); - } - fs->freereg = freg; - if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break; - } - lex_match(ls, '}', '{', line); - if (vcall) { - BCInsLine *ilp = &fs->bcbase[fs->pc-1]; - ExpDesc en; - lj_assertFS(bc_a(ilp->ins) == freg && - bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB), - "bad CALL code generation"); - expr_init(&en, VKNUM, 0); - en.u.nval.u32.lo = narr-1; - en.u.nval.u32.hi = 0x43300000; /* Biased integer to avoid denormals. */ - if (narr > 256) { fs->pc--; ilp--; } - ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en)); - setbc_b(&ilp[-1].ins, 0); - } - if (pc == fs->pc-1) { /* Make expr relocable if possible. */ - e->u.s.info = pc; - fs->freereg--; - e->k = VRELOCABLE; - } else { - e->k = VNONRELOC; /* May have been changed by expr_index. */ - } - if (!t) { /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */ - BCIns *ip = &fs->bcbase[pc].ins; - if (!needarr) narr = 0; - else if (narr < 3) narr = 3; - else if (narr > 0x7ff) narr = 0x7ff; - setbc_d(ip, narr|(hsize2hbits(nhash)<<11)); - } else { - if (needarr && t->asize < narr) - lj_tab_reasize(fs->L, t, narr-1); - if (fixt) { /* Fix value for dummy keys in template table. */ - Node *node = noderef(t->node); - uint32_t i, hmask = t->hmask; - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (tvistab(&n->val)) { - lj_assertFS(tabV(&n->val) == t, "bad dummy key in template table"); - setnilV(&n->val); /* Turn value into nil. */ - } - } - } - lj_gc_check(fs->L); - } -} - -/* Parse function parameters. */ -static BCReg parse_params(LexState *ls, int needself) -{ - FuncState *fs = ls->fs; - BCReg nparams = 0; - lex_check(ls, '('); - if (needself) - var_new_lit(ls, nparams++, "self"); - if (ls->tok != ')') { - do { - if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) { - var_new(ls, nparams++, lex_str(ls)); - } else if (ls->tok == TK_dots) { - lj_lex_next(ls); - fs->flags |= PROTO_VARARG; - break; - } else { - err_syntax(ls, LJ_ERR_XPARAM); - } - } while (lex_opt(ls, ',')); - } - var_add(ls, nparams); - lj_assertFS(fs->nactvar == nparams, "bad regalloc"); - bcreg_reserve(fs, nparams); - lex_check(ls, ')'); - return nparams; -} - -/* Forward declaration. */ -static void parse_chunk(LexState *ls); - -/* Parse body of a function. */ -static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line) -{ - FuncState fs, *pfs = ls->fs; - FuncScope bl; - GCproto *pt; - ptrdiff_t oldbase = pfs->bcbase - ls->bcstack; - fs_init(ls, &fs); - fscope_begin(&fs, &bl, 0); - fs.linedefined = line; - fs.numparams = (uint8_t)parse_params(ls, needself); - fs.bcbase = pfs->bcbase + pfs->pc; - fs.bclim = pfs->bclim - pfs->pc; - bcemit_AD(&fs, BC_FUNCF, 0, 0); /* Placeholder. */ - parse_chunk(ls); - if (ls->tok != TK_end) lex_match(ls, TK_end, TK_function, line); - pt = fs_finish(ls, (ls->lastline = ls->linenumber)); - pfs->bcbase = ls->bcstack + oldbase; /* May have been reallocated. */ - pfs->bclim = (BCPos)(ls->sizebcstack - oldbase); - /* Store new prototype in the constant array of the parent. */ - expr_init(e, VRELOCABLE, - bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO))); -#if LJ_HASFFI - pfs->flags |= (fs.flags & PROTO_FFI); -#endif - if (!(pfs->flags & PROTO_CHILD)) { - if (pfs->flags & PROTO_HAS_RETURN) - pfs->flags |= PROTO_FIXUP_RETURN; - pfs->flags |= PROTO_CHILD; - } - lj_lex_next(ls); -} - -/* Parse expression list. Last expression is left open. */ -static BCReg expr_list(LexState *ls, ExpDesc *v) -{ - BCReg n = 1; - expr(ls, v); - while (lex_opt(ls, ',')) { - expr_tonextreg(ls->fs, v); - expr(ls, v); - n++; - } - return n; -} - -/* Parse function argument list. */ -static void parse_args(LexState *ls, ExpDesc *e) -{ - FuncState *fs = ls->fs; - ExpDesc args; - BCIns ins; - BCReg base; - BCLine line = ls->linenumber; - if (ls->tok == '(') { -#if !LJ_52 - if (line != ls->lastline) - err_syntax(ls, LJ_ERR_XAMBIG); -#endif - lj_lex_next(ls); - if (ls->tok == ')') { /* f(). */ - args.k = VVOID; - } else { - expr_list(ls, &args); - if (args.k == VCALL) /* f(a, b, g()) or f(a, b, ...). */ - setbc_b(bcptr(fs, &args), 0); /* Pass on multiple results. */ - } - lex_match(ls, ')', '(', line); - } else if (ls->tok == '{') { - expr_table(ls, &args); - } else if (ls->tok == TK_string) { - expr_init(&args, VKSTR, 0); - args.u.sval = strV(&ls->tokval); - lj_lex_next(ls); - } else { - err_syntax(ls, LJ_ERR_XFUNARG); - return; /* Silence compiler. */ - } - lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k); - base = e->u.s.info; /* Base register for call. */ - if (args.k == VCALL) { - ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2); - } else { - if (args.k != VVOID) - expr_tonextreg(fs, &args); - ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base - LJ_FR2); - } - expr_init(e, VCALL, bcemit_INS(fs, ins)); - e->u.s.aux = base; - fs->bcbase[fs->pc - 1].line = line; - fs->freereg = base+1; /* Leave one result by default. */ -} - -/* Parse primary expression. */ -static void expr_primary(LexState *ls, ExpDesc *v) -{ - FuncState *fs = ls->fs; - /* Parse prefix expression. */ - if (ls->tok == '(') { - BCLine line = ls->linenumber; - lj_lex_next(ls); - expr(ls, v); - lex_match(ls, ')', '(', line); - expr_discharge(ls->fs, v); - } else if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) { - var_lookup(ls, v); - } else { - err_syntax(ls, LJ_ERR_XSYMBOL); - } - for (;;) { /* Parse multiple expression suffixes. */ - if (ls->tok == '.') { - expr_field(ls, v); - } else if (ls->tok == '[') { - ExpDesc key; - expr_toanyreg(fs, v); - expr_bracket(ls, &key); - expr_index(fs, v, &key); - } else if (ls->tok == ':') { - ExpDesc key; - lj_lex_next(ls); - expr_str(ls, &key); - bcemit_method(fs, v, &key); - parse_args(ls, v); - } else if (ls->tok == '(' || ls->tok == TK_string || ls->tok == '{') { - expr_tonextreg(fs, v); - if (LJ_FR2) bcreg_reserve(fs, 1); - parse_args(ls, v); - } else { - break; - } - } -} - -/* Parse simple expression. */ -static void expr_simple(LexState *ls, ExpDesc *v) -{ - switch (ls->tok) { - case TK_number: - expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokval)) ? VKCDATA : VKNUM, 0); - copyTV(ls->L, &v->u.nval, &ls->tokval); - break; - case TK_string: - expr_init(v, VKSTR, 0); - v->u.sval = strV(&ls->tokval); - break; - case TK_nil: - expr_init(v, VKNIL, 0); - break; - case TK_true: - expr_init(v, VKTRUE, 0); - break; - case TK_false: - expr_init(v, VKFALSE, 0); - break; - case TK_dots: { /* Vararg. */ - FuncState *fs = ls->fs; - BCReg base; - checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS); - bcreg_reserve(fs, 1); - base = fs->freereg-1; - expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams)); - v->u.s.aux = base; - break; - } - case '{': /* Table constructor. */ - expr_table(ls, v); - return; - case TK_function: - lj_lex_next(ls); - parse_body(ls, v, 0, ls->linenumber); - return; - default: - expr_primary(ls, v); - return; - } - lj_lex_next(ls); -} - -/* Manage syntactic levels to avoid blowing up the stack. */ -static void synlevel_begin(LexState *ls) -{ - if (++ls->level >= LJ_MAX_XLEVEL) - lj_lex_error(ls, 0, LJ_ERR_XLEVELS); -} - -#define synlevel_end(ls) ((ls)->level--) - -/* Convert token to binary operator. */ -static BinOpr token2binop(LexToken tok) -{ - switch (tok) { - case '+': return OPR_ADD; - case '-': return OPR_SUB; - case '*': return OPR_MUL; - case '/': return OPR_DIV; - case '%': return OPR_MOD; - case '^': return OPR_POW; - case TK_concat: return OPR_CONCAT; - case TK_ne: return OPR_NE; - case TK_eq: return OPR_EQ; - case '<': return OPR_LT; - case TK_le: return OPR_LE; - case '>': return OPR_GT; - case TK_ge: return OPR_GE; - case TK_and: return OPR_AND; - case TK_or: return OPR_OR; - default: return OPR_NOBINOPR; - } -} - -/* Priorities for each binary operator. ORDER OPR. */ -static const struct { - uint8_t left; /* Left priority. */ - uint8_t right; /* Right priority. */ -} priority[] = { - {6,6}, {6,6}, {7,7}, {7,7}, {7,7}, /* ADD SUB MUL DIV MOD */ - {10,9}, {5,4}, /* POW CONCAT (right associative) */ - {3,3}, {3,3}, /* EQ NE */ - {3,3}, {3,3}, {3,3}, {3,3}, /* LT GE GT LE */ - {2,2}, {1,1} /* AND OR */ -}; - -#define UNARY_PRIORITY 8 /* Priority for unary operators. */ - -/* Forward declaration. */ -static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit); - -/* Parse unary expression. */ -static void expr_unop(LexState *ls, ExpDesc *v) -{ - BCOp op; - if (ls->tok == TK_not) { - op = BC_NOT; - } else if (ls->tok == '-') { - op = BC_UNM; - } else if (ls->tok == '#') { - op = BC_LEN; - } else { - expr_simple(ls, v); - return; - } - lj_lex_next(ls); - expr_binop(ls, v, UNARY_PRIORITY); - bcemit_unop(ls->fs, op, v); -} - -/* Parse binary expressions with priority higher than the limit. */ -static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit) -{ - BinOpr op; - synlevel_begin(ls); - expr_unop(ls, v); - op = token2binop(ls->tok); - while (op != OPR_NOBINOPR && priority[op].left > limit) { - ExpDesc v2; - BinOpr nextop; - lj_lex_next(ls); - bcemit_binop_left(ls->fs, op, v); - /* Parse binary expression with higher priority. */ - nextop = expr_binop(ls, &v2, priority[op].right); - bcemit_binop(ls->fs, op, v, &v2); - op = nextop; - } - synlevel_end(ls); - return op; /* Return unconsumed binary operator (if any). */ -} - -/* Parse expression. */ -static void expr(LexState *ls, ExpDesc *v) -{ - expr_binop(ls, v, 0); /* Priority 0: parse whole expression. */ -} - -/* Assign expression to the next register. */ -static void expr_next(LexState *ls) -{ - ExpDesc e; - expr(ls, &e); - expr_tonextreg(ls->fs, &e); -} - -/* Parse conditional expression. */ -static BCPos expr_cond(LexState *ls) -{ - ExpDesc v; - expr(ls, &v); - if (v.k == VKNIL) v.k = VKFALSE; - bcemit_branch_t(ls->fs, &v); - return v.f; -} - -/* -- Assignments --------------------------------------------------------- */ - -/* List of LHS variables. */ -typedef struct LHSVarList { - ExpDesc v; /* LHS variable. */ - struct LHSVarList *prev; /* Link to previous LHS variable. */ -} LHSVarList; - -/* Eliminate write-after-read hazards for local variable assignment. */ -static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v) -{ - FuncState *fs = ls->fs; - BCReg reg = v->u.s.info; /* Check against this variable. */ - BCReg tmp = fs->freereg; /* Rename to this temp. register (if needed). */ - int hazard = 0; - for (; lh; lh = lh->prev) { - if (lh->v.k == VINDEXED) { - if (lh->v.u.s.info == reg) { /* t[i], t = 1, 2 */ - hazard = 1; - lh->v.u.s.info = tmp; - } - if (lh->v.u.s.aux == reg) { /* t[i], i = 1, 2 */ - hazard = 1; - lh->v.u.s.aux = tmp; - } - } - } - if (hazard) { - bcemit_AD(fs, BC_MOV, tmp, reg); /* Rename conflicting variable. */ - bcreg_reserve(fs, 1); - } -} - -/* Adjust LHS/RHS of an assignment. */ -static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e) -{ - FuncState *fs = ls->fs; - int32_t extra = (int32_t)nvars - (int32_t)nexps; - if (e->k == VCALL) { - extra++; /* Compensate for the VCALL itself. */ - if (extra < 0) extra = 0; - setbc_b(bcptr(fs, e), extra+1); /* Fixup call results. */ - if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1); - } else { - if (e->k != VVOID) - expr_tonextreg(fs, e); /* Close last expression. */ - if (extra > 0) { /* Leftover LHS are set to nil. */ - BCReg reg = fs->freereg; - bcreg_reserve(fs, (BCReg)extra); - bcemit_nil(fs, reg, (BCReg)extra); - } - } - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* Drop leftover regs. */ -} - -/* Recursively parse assignment statement. */ -static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars) -{ - ExpDesc e; - checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX); - if (lex_opt(ls, ',')) { /* Collect LHS list and recurse upwards. */ - LHSVarList vl; - vl.prev = lh; - expr_primary(ls, &vl.v); - if (vl.v.k == VLOCAL) - assign_hazard(ls, lh, &vl.v); - checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names"); - parse_assignment(ls, &vl, nvars+1); - } else { /* Parse RHS. */ - BCReg nexps; - lex_check(ls, '='); - nexps = expr_list(ls, &e); - if (nexps == nvars) { - if (e.k == VCALL) { - if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) { /* Vararg assignment. */ - ls->fs->freereg--; - e.k = VRELOCABLE; - } else { /* Multiple call results. */ - e.u.s.info = e.u.s.aux; /* Base of call is not relocatable. */ - e.k = VNONRELOC; - } - } - bcemit_store(ls->fs, &lh->v, &e); - return; - } - assign_adjust(ls, nvars, nexps, &e); - } - /* Assign RHS to LHS and recurse downwards. */ - expr_init(&e, VNONRELOC, ls->fs->freereg-1); - bcemit_store(ls->fs, &lh->v, &e); -} - -/* Parse call statement or assignment. */ -static void parse_call_assign(LexState *ls) -{ - FuncState *fs = ls->fs; - LHSVarList vl; - expr_primary(ls, &vl.v); - if (vl.v.k == VCALL) { /* Function call statement. */ - setbc_b(bcptr(fs, &vl.v), 1); /* No results. */ - } else { /* Start of an assignment. */ - vl.prev = NULL; - parse_assignment(ls, &vl, 1); - } -} - -/* Parse 'local' statement. */ -static void parse_local(LexState *ls) -{ - if (lex_opt(ls, TK_function)) { /* Local function declaration. */ - ExpDesc v, b; - FuncState *fs = ls->fs; - var_new(ls, 0, lex_str(ls)); - expr_init(&v, VLOCAL, fs->freereg); - v.u.s.aux = fs->varmap[fs->freereg]; - bcreg_reserve(fs, 1); - var_add(ls, 1); - parse_body(ls, &b, 0, ls->linenumber); - /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */ - expr_free(fs, &b); - expr_toreg(fs, &b, v.u.s.info); - /* The upvalue is in scope, but the local is only valid after the store. */ - var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc; - } else { /* Local variable declaration. */ - ExpDesc e; - BCReg nexps, nvars = 0; - do { /* Collect LHS. */ - var_new(ls, nvars++, lex_str(ls)); - } while (lex_opt(ls, ',')); - if (lex_opt(ls, '=')) { /* Optional RHS. */ - nexps = expr_list(ls, &e); - } else { /* Or implicitly set to nil. */ - e.k = VVOID; - nexps = 0; - } - assign_adjust(ls, nvars, nexps, &e); - var_add(ls, nvars); - } -} - -/* Parse 'function' statement. */ -static void parse_func(LexState *ls, BCLine line) -{ - FuncState *fs; - ExpDesc v, b; - int needself = 0; - lj_lex_next(ls); /* Skip 'function'. */ - /* Parse function name. */ - var_lookup(ls, &v); - while (ls->tok == '.') /* Multiple dot-separated fields. */ - expr_field(ls, &v); - if (ls->tok == ':') { /* Optional colon to signify method call. */ - needself = 1; - expr_field(ls, &v); - } - parse_body(ls, &b, needself, line); - fs = ls->fs; - bcemit_store(fs, &v, &b); - fs->bcbase[fs->pc - 1].line = line; /* Set line for the store. */ -} - -/* -- Control transfer statements ----------------------------------------- */ - -/* Check for end of block. */ -static int parse_isend(LexToken tok) -{ - switch (tok) { - case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof: - return 1; - default: - return 0; - } -} - -/* Parse 'return' statement. */ -static void parse_return(LexState *ls) -{ - BCIns ins; - FuncState *fs = ls->fs; - lj_lex_next(ls); /* Skip 'return'. */ - fs->flags |= PROTO_HAS_RETURN; - if (parse_isend(ls->tok) || ls->tok == ';') { /* Bare return. */ - ins = BCINS_AD(BC_RET0, 0, 1); - } else { /* Return with one or more values. */ - ExpDesc e; /* Receives the _last_ expression in the list. */ - BCReg nret = expr_list(ls, &e); - if (nret == 1) { /* Return one result. */ - if (e.k == VCALL) { /* Check for tail call. */ - BCIns *ip = bcptr(fs, &e); - /* It doesn't pay off to add BC_VARGT just for 'return ...'. */ - if (bc_op(*ip) == BC_VARG) goto notailcall; - fs->pc--; - ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip)); - } else { /* Can return the result from any register. */ - ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2); - } - } else { - if (e.k == VCALL) { /* Append all results from a call. */ - notailcall: - setbc_b(bcptr(fs, &e), 0); - ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar); - } else { - expr_tonextreg(fs, &e); /* Force contiguous registers. */ - ins = BCINS_AD(BC_RET, fs->nactvar, nret+1); - } - } - } - if (fs->flags & PROTO_CHILD) - bcemit_AJ(fs, BC_UCLO, 0, 0); /* May need to close upvalues first. */ - bcemit_INS(fs, ins); -} - -/* Parse 'break' statement. */ -static void parse_break(LexState *ls) -{ - ls->fs->bl->flags |= FSCOPE_BREAK; - gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs)); -} - -/* Parse 'goto' statement. */ -static void parse_goto(LexState *ls) -{ - FuncState *fs = ls->fs; - GCstr *name = lex_str(ls); - VarInfo *vl = gola_findlabel(ls, name); - if (vl) /* Treat backwards goto within same scope like a loop. */ - bcemit_AJ(fs, BC_LOOP, vl->slot, -1); /* No BC range check. */ - fs->bl->flags |= FSCOPE_GOLA; - gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs)); -} - -/* Parse label. */ -static void parse_label(LexState *ls) -{ - FuncState *fs = ls->fs; - GCstr *name; - MSize idx; - fs->lasttarget = fs->pc; - fs->bl->flags |= FSCOPE_GOLA; - lj_lex_next(ls); /* Skip '::'. */ - name = lex_str(ls); - if (gola_findlabel(ls, name)) - lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name)); - idx = gola_new(ls, name, VSTACK_LABEL, fs->pc); - lex_check(ls, TK_label); - /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */ - for (;;) { - if (ls->tok == TK_label) { - synlevel_begin(ls); - parse_label(ls); - synlevel_end(ls); - } else if (LJ_52 && ls->tok == ';') { - lj_lex_next(ls); - } else { - break; - } - } - /* Trailing label is considered to be outside of scope. */ - if (parse_isend(ls->tok) && ls->tok != TK_until) - ls->vstack[idx].slot = fs->bl->nactvar; - gola_resolve(ls, fs->bl, idx); -} - -/* -- Blocks, loops and conditional statements ---------------------------- */ - -/* Parse a block. */ -static void parse_block(LexState *ls) -{ - FuncState *fs = ls->fs; - FuncScope bl; - fscope_begin(fs, &bl, 0); - parse_chunk(ls); - fscope_end(fs); -} - -/* Parse 'while' statement. */ -static void parse_while(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - BCPos start, loop, condexit; - FuncScope bl; - lj_lex_next(ls); /* Skip 'while'. */ - start = fs->lasttarget = fs->pc; - condexit = expr_cond(ls); - fscope_begin(fs, &bl, FSCOPE_LOOP); - lex_check(ls, TK_do); - loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0); - parse_block(ls); - jmp_patch(fs, bcemit_jmp(fs), start); - lex_match(ls, TK_end, TK_while, line); - fscope_end(fs); - jmp_tohere(fs, condexit); - jmp_patchins(fs, loop, fs->pc); -} - -/* Parse 'repeat' statement. */ -static void parse_repeat(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - BCPos loop = fs->lasttarget = fs->pc; - BCPos condexit; - FuncScope bl1, bl2; - fscope_begin(fs, &bl1, FSCOPE_LOOP); /* Breakable loop scope. */ - fscope_begin(fs, &bl2, 0); /* Inner scope. */ - lj_lex_next(ls); /* Skip 'repeat'. */ - bcemit_AD(fs, BC_LOOP, fs->nactvar, 0); - parse_chunk(ls); - lex_match(ls, TK_until, TK_repeat, line); - condexit = expr_cond(ls); /* Parse condition (still inside inner scope). */ - if (!(bl2.flags & FSCOPE_UPVAL)) { /* No upvalues? Just end inner scope. */ - fscope_end(fs); - } else { /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */ - parse_break(ls); /* Break from loop and close upvalues. */ - jmp_tohere(fs, condexit); - fscope_end(fs); /* End inner scope and close upvalues. */ - condexit = bcemit_jmp(fs); - } - jmp_patch(fs, condexit, loop); /* Jump backwards if !cond. */ - jmp_patchins(fs, loop, fs->pc); - fscope_end(fs); /* End loop scope. */ -} - -/* Parse numeric 'for'. */ -static void parse_for_num(LexState *ls, GCstr *varname, BCLine line) -{ - FuncState *fs = ls->fs; - BCReg base = fs->freereg; - FuncScope bl; - BCPos loop, loopend; - /* Hidden control variables. */ - var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX); - var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP); - var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP); - /* Visible copy of index variable. */ - var_new(ls, FORL_EXT, varname); - lex_check(ls, '='); - expr_next(ls); - lex_check(ls, ','); - expr_next(ls); - if (lex_opt(ls, ',')) { - expr_next(ls); - } else { - bcemit_AD(fs, BC_KSHORT, fs->freereg, 1); /* Default step is 1. */ - bcreg_reserve(fs, 1); - } - var_add(ls, 3); /* Hidden control variables. */ - lex_check(ls, TK_do); - loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP); - fscope_begin(fs, &bl, 0); /* Scope for visible variables. */ - var_add(ls, 1); - bcreg_reserve(fs, 1); - parse_block(ls); - fscope_end(fs); - /* Perform loop inversion. Loop control instructions are at the end. */ - loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP); - fs->bcbase[loopend].line = line; /* Fix line for control ins. */ - jmp_patchins(fs, loopend, loop+1); - jmp_patchins(fs, loop, fs->pc); -} - -/* Try to predict whether the iterator is next() and specialize the bytecode. -** Detecting next() and pairs() by name is simplistic, but quite effective. -** The interpreter backs off if the check for the closure fails at runtime. -*/ -static int predict_next(LexState *ls, FuncState *fs, BCPos pc) -{ - BCIns ins = fs->bcbase[pc].ins; - GCstr *name; - cTValue *o; - switch (bc_op(ins)) { - case BC_MOV: - name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name)); - break; - case BC_UGET: - name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name)); - break; - case BC_GGET: - /* There's no inverse index (yet), so lookup the strings. */ - o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs")); - if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins)) - return 1; - o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next")); - if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins)) - return 1; - return 0; - default: - return 0; - } - return (name->len == 5 && !strcmp(strdata(name), "pairs")) || - (name->len == 4 && !strcmp(strdata(name), "next")); -} - -/* Parse 'for' iterator. */ -static void parse_for_iter(LexState *ls, GCstr *indexname) -{ - FuncState *fs = ls->fs; - ExpDesc e; - BCReg nvars = 0; - BCLine line; - BCReg base = fs->freereg + 3; - BCPos loop, loopend, exprpc = fs->pc; - FuncScope bl; - int isnext; - /* Hidden control variables. */ - var_new_fixed(ls, nvars++, VARNAME_FOR_GEN); - var_new_fixed(ls, nvars++, VARNAME_FOR_STATE); - var_new_fixed(ls, nvars++, VARNAME_FOR_CTL); - /* Visible variables returned from iterator. */ - var_new(ls, nvars++, indexname); - while (lex_opt(ls, ',')) - var_new(ls, nvars++, lex_str(ls)); - lex_check(ls, TK_in); - line = ls->linenumber; - assign_adjust(ls, 3, expr_list(ls, &e), &e); - /* The iterator needs another 3 [4] slots (func [pc] | state ctl). */ - bcreg_bump(fs, 3+LJ_FR2); - isnext = (nvars <= 5 && predict_next(ls, fs, exprpc)); - var_add(ls, 3); /* Hidden control variables. */ - lex_check(ls, TK_do); - loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP); - fscope_begin(fs, &bl, 0); /* Scope for visible variables. */ - var_add(ls, nvars-3); - bcreg_reserve(fs, nvars-3); - parse_block(ls); - fscope_end(fs); - /* Perform loop inversion. Loop control instructions are at the end. */ - jmp_patchins(fs, loop, fs->pc); - bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1); - loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP); - fs->bcbase[loopend-1].line = line; /* Fix line for control ins. */ - fs->bcbase[loopend].line = line; - jmp_patchins(fs, loopend, loop+1); -} - -/* Parse 'for' statement. */ -static void parse_for(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - GCstr *varname; - FuncScope bl; - fscope_begin(fs, &bl, FSCOPE_LOOP); - lj_lex_next(ls); /* Skip 'for'. */ - varname = lex_str(ls); /* Get first variable name. */ - if (ls->tok == '=') - parse_for_num(ls, varname, line); - else if (ls->tok == ',' || ls->tok == TK_in) - parse_for_iter(ls, varname); - else - err_syntax(ls, LJ_ERR_XFOR); - lex_match(ls, TK_end, TK_for, line); - fscope_end(fs); /* Resolve break list. */ -} - -/* Parse condition and 'then' block. */ -static BCPos parse_then(LexState *ls) -{ - BCPos condexit; - lj_lex_next(ls); /* Skip 'if' or 'elseif'. */ - condexit = expr_cond(ls); - lex_check(ls, TK_then); - parse_block(ls); - return condexit; -} - -/* Parse 'if' statement. */ -static void parse_if(LexState *ls, BCLine line) -{ - FuncState *fs = ls->fs; - BCPos flist; - BCPos escapelist = NO_JMP; - flist = parse_then(ls); - while (ls->tok == TK_elseif) { /* Parse multiple 'elseif' blocks. */ - jmp_append(fs, &escapelist, bcemit_jmp(fs)); - jmp_tohere(fs, flist); - flist = parse_then(ls); - } - if (ls->tok == TK_else) { /* Parse optional 'else' block. */ - jmp_append(fs, &escapelist, bcemit_jmp(fs)); - jmp_tohere(fs, flist); - lj_lex_next(ls); /* Skip 'else'. */ - parse_block(ls); - } else { - jmp_append(fs, &escapelist, flist); - } - jmp_tohere(fs, escapelist); - lex_match(ls, TK_end, TK_if, line); -} - -/* -- Parse statements ---------------------------------------------------- */ - -/* Parse a statement. Returns 1 if it must be the last one in a chunk. */ -static int parse_stmt(LexState *ls) -{ - BCLine line = ls->linenumber; - switch (ls->tok) { - case TK_if: - parse_if(ls, line); - break; - case TK_while: - parse_while(ls, line); - break; - case TK_do: - lj_lex_next(ls); - parse_block(ls); - lex_match(ls, TK_end, TK_do, line); - break; - case TK_for: - parse_for(ls, line); - break; - case TK_repeat: - parse_repeat(ls, line); - break; - case TK_function: - parse_func(ls, line); - break; - case TK_local: - lj_lex_next(ls); - parse_local(ls); - break; - case TK_return: - parse_return(ls); - return 1; /* Must be last. */ - case TK_break: - lj_lex_next(ls); - parse_break(ls); - return !LJ_52; /* Must be last in Lua 5.1. */ -#if LJ_52 - case ';': - lj_lex_next(ls); - break; -#endif - case TK_label: - parse_label(ls); - break; - case TK_goto: - if (LJ_52 || lj_lex_lookahead(ls) == TK_name) { - lj_lex_next(ls); - parse_goto(ls); - break; - } - /* fallthrough */ - default: - parse_call_assign(ls); - break; - } - return 0; -} - -/* A chunk is a list of statements optionally separated by semicolons. */ -static void parse_chunk(LexState *ls) -{ - int islast = 0; - synlevel_begin(ls); - while (!islast && !parse_isend(ls->tok)) { - islast = parse_stmt(ls); - lex_opt(ls, ';'); - lj_assertLS(ls->fs->framesize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar, - "bad regalloc"); - ls->fs->freereg = ls->fs->nactvar; /* Free registers after each stmt. */ - } - synlevel_end(ls); -} - -/* Entry point of bytecode parser. */ -GCproto *lj_parse(LexState *ls) -{ - FuncState fs; - FuncScope bl; - GCproto *pt; - lua_State *L = ls->L; -#ifdef LUAJIT_DISABLE_DEBUGINFO - ls->chunkname = lj_str_newlit(L, "="); -#else - ls->chunkname = lj_str_newz(L, ls->chunkarg); -#endif - setstrV(L, L->top, ls->chunkname); /* Anchor chunkname string. */ - incr_top(L); - ls->level = 0; - fs_init(ls, &fs); - fs.linedefined = 0; - fs.numparams = 0; - fs.bcbase = NULL; - fs.bclim = 0; - fs.flags |= PROTO_VARARG; /* Main chunk is always a vararg func. */ - fscope_begin(&fs, &bl, 0); - bcemit_AD(&fs, BC_FUNCV, 0, 0); /* Placeholder. */ - lj_lex_next(ls); /* Read-ahead first token. */ - parse_chunk(ls); - if (ls->tok != TK_eof) - err_token(ls, TK_eof); - pt = fs_finish(ls, ls->linenumber); - L->top--; /* Drop chunkname. */ - lj_assertL(fs.prev == NULL && ls->fs == NULL, "mismatched frame nesting"); - lj_assertL(pt->sizeuv == 0, "toplevel proto has upvalues"); - return pt; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.h deleted file mode 100644 index 4206f00..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_parse.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -** Lua parser (source code -> bytecode). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_PARSE_H -#define _LJ_PARSE_H - -#include "lj_obj.h" -#include "lj_lex.h" - -LJ_FUNC GCproto *lj_parse(LexState *ls); -LJ_FUNC GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t l); -#if LJ_HASFFI -LJ_FUNC void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.c deleted file mode 100644 index 01935e5..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.c +++ /dev/null @@ -1,259 +0,0 @@ -/* -** Pseudo-random number generation. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_prng_c -#define LUA_CORE - -/* To get the syscall prototype. */ -#if defined(__linux__) && !defined(_GNU_SOURCE) -#define _GNU_SOURCE -#endif - -#include "lj_def.h" -#include "lj_arch.h" -#include "lj_prng.h" - -/* -- PRNG step function -------------------------------------------------- */ - -/* This implements a Tausworthe PRNG with period 2^223. Based on: -** Tables of maximally-equidistributed combined LFSR generators, -** Pierre L'Ecuyer, 1991, table 3, 1st entry. -** Full-period ME-CF generator with L=64, J=4, k=223, N1=49. -** -** Important note: This PRNG is NOT suitable for cryptographic use! -** -** But it works fine for math.random(), which has an API that's not -** suitable for cryptography, anyway. -** -** When used as a securely seeded global PRNG, it substantially raises -** the difficulty for various attacks on the VM. -*/ - -/* Update generator i and compute a running xor of all states. */ -#define TW223_GEN(rs, z, r, i, k, q, s) \ - z = rs->u[i]; \ - z = (((z<> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<u[i] = z; - -#define TW223_STEP(rs, z, r) \ - TW223_GEN(rs, z, r, 0, 63, 31, 18) \ - TW223_GEN(rs, z, r, 1, 58, 19, 28) \ - TW223_GEN(rs, z, r, 2, 55, 24, 7) \ - TW223_GEN(rs, z, r, 3, 47, 21, 8) - -/* PRNG step function with uint64_t result. */ -LJ_NOINLINE uint64_t LJ_FASTCALL lj_prng_u64(PRNGState *rs) -{ - uint64_t z, r = 0; - TW223_STEP(rs, z, r) - return r; -} - -/* PRNG step function with double in uint64_t result. */ -LJ_NOINLINE uint64_t LJ_FASTCALL lj_prng_u64d(PRNGState *rs) -{ - uint64_t z, r = 0; - TW223_STEP(rs, z, r) - /* Returns a double bit pattern in the range 1.0 <= d < 2.0. */ - return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000); -} - -/* Condition seed: ensure k[i] MSB of u[i] are non-zero. */ -static LJ_AINLINE void lj_prng_condition(PRNGState *rs) -{ - if (rs->u[0] < (1u << 1)) rs->u[0] += (1u << 1); - if (rs->u[1] < (1u << 6)) rs->u[1] += (1u << 6); - if (rs->u[2] < (1u << 9)) rs->u[2] += (1u << 9); - if (rs->u[3] < (1u << 17)) rs->u[3] += (1u << 17); -} - -/* -- PRNG seeding from OS ------------------------------------------------ */ - -#if LUAJIT_SECURITY_PRNG == 0 - -/* Nothing to define. */ - -#elif LJ_TARGET_XBOX360 - -extern int XNetRandom(void *buf, unsigned int len); - -#elif LJ_TARGET_PS3 - -extern int sys_get_random_number(void *buf, uint64_t len); - -#elif LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA - -extern int sceRandomGetRandomNumber(void *buf, size_t len); - -#elif LJ_TARGET_NX - -#include - -#elif LJ_TARGET_WINDOWS || LJ_TARGET_XBOXONE - -#define WIN32_LEAN_AND_MEAN -#include - -#if LJ_TARGET_UWP || LJ_TARGET_XBOXONE -/* Must use BCryptGenRandom. */ -#include -#pragma comment(lib, "bcrypt.lib") -#else -/* If you wonder about this mess, then search online for RtlGenRandom. */ -typedef BOOLEAN (WINAPI *PRGR)(void *buf, ULONG len); -static PRGR libfunc_rgr; -#endif - -#elif LJ_TARGET_POSIX - -#if LJ_TARGET_LINUX -/* Avoid a dependency on glibc 2.25+ and use the getrandom syscall instead. */ -#include -#else - -#if LJ_TARGET_OSX && !LJ_TARGET_IOS -/* -** In their infinite wisdom Apple decided to disallow getentropy() in the -** iOS App Store. Even though the call is common to all BSD-ish OS, it's -** recommended by Apple in their own security-related docs, and, to top -** off the foolery, /dev/urandom is handled by the same kernel code, -** yet accessing it is actually permitted (but less efficient). -*/ -#include -#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200 -#define LJ_TARGET_HAS_GETENTROPY 1 -#endif -#elif (LJ_TARGET_BSD && !defined(__NetBSD__)) || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN || LJ_TARGET_QNX -#define LJ_TARGET_HAS_GETENTROPY 1 -#endif - -#if LJ_TARGET_HAS_GETENTROPY -extern int getentropy(void *buf, size_t len) -#ifdef __ELF__ - __attribute__((weak)) -#endif -; -#endif - -#endif - -/* For the /dev/urandom fallback. */ -#include -#include - -#endif - -#if LUAJIT_SECURITY_PRNG == 0 - -/* If you really don't care about security, then define -** LUAJIT_SECURITY_PRNG=0. This yields a predictable seed -** and provides NO SECURITY against various attacks on the VM. -** -** BTW: This is NOT the way to get predictable table iteration, -** predictable trace generation, predictable bytecode generation, etc. -*/ -int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs) -{ - lj_prng_seed_fixed(rs); /* The fixed seed is already conditioned. */ - return 1; -} - -#else - -/* Securely seed PRNG from system entropy. Returns 0 on failure. */ -int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs) -{ -#if LJ_TARGET_XBOX360 - - if (XNetRandom(rs->u, (unsigned int)sizeof(rs->u)) == 0) - goto ok; - -#elif LJ_TARGET_PS3 - - if (sys_get_random_number(rs->u, sizeof(rs->u)) == 0) - goto ok; - -#elif LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA - - if (sceRandomGetRandomNumber(rs->u, sizeof(rs->u)) == 0) - goto ok; - -#elif LJ_TARGET_NX - - if (getentropy(rs->u, sizeof(rs->u)) == 0) - goto ok; - -#elif LJ_TARGET_UWP || LJ_TARGET_XBOXONE - - if (BCryptGenRandom(NULL, (PUCHAR)(rs->u), (ULONG)sizeof(rs->u), - BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0) - goto ok; - -#elif LJ_TARGET_WINDOWS - - /* Keep the library loaded in case multiple VMs are started. */ - if (!libfunc_rgr) { - HMODULE lib = LJ_WIN_LOADLIBA("advapi32.dll"); - if (!lib) return 0; - libfunc_rgr = (PRGR)GetProcAddress(lib, "SystemFunction036"); - if (!libfunc_rgr) return 0; - } - if (libfunc_rgr(rs->u, (ULONG)sizeof(rs->u))) - goto ok; - -#elif LJ_TARGET_POSIX - -#if LJ_TARGET_LINUX && defined(SYS_getrandom) - - if (syscall(SYS_getrandom, rs->u, sizeof(rs->u), 0) == (long)sizeof(rs->u)) - goto ok; - -#elif LJ_TARGET_HAS_GETENTROPY - -#ifdef __ELF__ - if (&getentropy && getentropy(rs->u, sizeof(rs->u)) == 0) - goto ok; -#else - if (getentropy(rs->u, sizeof(rs->u)) == 0) - goto ok; -#endif - -#endif - - /* Fallback to /dev/urandom. This may fail if the device is not - ** existent or accessible in a chroot or container, or if the process - ** or the OS ran out of file descriptors. - */ - { - int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); - if (fd != -1) { - ssize_t n = read(fd, rs->u, sizeof(rs->u)); - (void)close(fd); - if (n == (ssize_t)sizeof(rs->u)) - goto ok; - } - } - -#else - - /* Add an elif above for your OS with a secure PRNG seed. - ** Note that fiddling around with rand(), getpid(), time() or coercing - ** ASLR to yield a few bits of randomness is not helpful. - ** If you don't want any security, then don't pretend you have any - ** and simply define LUAJIT_SECURITY_PRNG=0 for the build. - */ -#error "Missing secure PRNG seed for this OS" - -#endif - return 0; /* Fail. */ - -ok: - lj_prng_condition(rs); - (void)lj_prng_u64(rs); - return 1; /* Success. */ -} - -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.h deleted file mode 100644 index bdc958a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_prng.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -** Pseudo-random number generation. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_PRNG_H -#define _LJ_PRNG_H - -#include "lj_def.h" - -LJ_FUNC int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs); -LJ_FUNC uint64_t LJ_FASTCALL lj_prng_u64(PRNGState *rs); -LJ_FUNC uint64_t LJ_FASTCALL lj_prng_u64d(PRNGState *rs); - -/* This is just the precomputed result of lib_math.c:random_seed(rs, 0.0). */ -static LJ_AINLINE void lj_prng_seed_fixed(PRNGState *rs) -{ - rs->u[0] = U64x(a0d27757,0a345b8c); - rs->u[1] = U64x(764a296c,5d4aa64f); - rs->u[2] = U64x(51220704,070adeaa); - rs->u[3] = U64x(2a2717b5,a7b7b927); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.c deleted file mode 100644 index 4a13537..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.c +++ /dev/null @@ -1,371 +0,0 @@ -/* -** Low-overhead profiling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_profile_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASPROFILE - -#include "lj_buf.h" -#include "lj_frame.h" -#include "lj_debug.h" -#include "lj_dispatch.h" -#if LJ_HASJIT -#include "lj_jit.h" -#include "lj_trace.h" -#endif -#include "lj_profile.h" - -#include "luajit.h" - -#if LJ_PROFILE_SIGPROF - -#include -#include -#define profile_lock(ps) UNUSED(ps) -#define profile_unlock(ps) UNUSED(ps) - -#elif LJ_PROFILE_PTHREAD - -#include -#include -#if LJ_TARGET_PS3 -#include -#endif -#define profile_lock(ps) pthread_mutex_lock(&ps->lock) -#define profile_unlock(ps) pthread_mutex_unlock(&ps->lock) - -#elif LJ_PROFILE_WTHREAD - -#define WIN32_LEAN_AND_MEAN -#if LJ_TARGET_XBOX360 -#include -#include -#else -#include -#endif -typedef unsigned int (WINAPI *WMM_TPFUNC)(unsigned int); -#define profile_lock(ps) EnterCriticalSection(&ps->lock) -#define profile_unlock(ps) LeaveCriticalSection(&ps->lock) - -#endif - -/* Profiler state. */ -typedef struct ProfileState { - global_State *g; /* VM state that started the profiler. */ - luaJIT_profile_callback cb; /* Profiler callback. */ - void *data; /* Profiler callback data. */ - SBuf sb; /* String buffer for stack dumps. */ - int interval; /* Sample interval in milliseconds. */ - int samples; /* Number of samples for next callback. */ - int vmstate; /* VM state when profile timer triggered. */ -#if LJ_PROFILE_SIGPROF - struct sigaction oldsa; /* Previous SIGPROF state. */ -#elif LJ_PROFILE_PTHREAD - pthread_mutex_t lock; /* g->hookmask update lock. */ - pthread_t thread; /* Timer thread. */ - int abort; /* Abort timer thread. */ -#elif LJ_PROFILE_WTHREAD -#if LJ_TARGET_WINDOWS - HINSTANCE wmm; /* WinMM library handle. */ - WMM_TPFUNC wmm_tbp; /* WinMM timeBeginPeriod function. */ - WMM_TPFUNC wmm_tep; /* WinMM timeEndPeriod function. */ -#endif - CRITICAL_SECTION lock; /* g->hookmask update lock. */ - HANDLE thread; /* Timer thread. */ - int abort; /* Abort timer thread. */ -#endif -} ProfileState; - -/* Sadly, we have to use a static profiler state. -** -** The SIGPROF variant needs a static pointer to the global state, anyway. -** And it would be hard to extend for multiple threads. You can still use -** multiple VMs in multiple threads, but only profile one at a time. -*/ -static ProfileState profile_state; - -/* Default sample interval in milliseconds. */ -#define LJ_PROFILE_INTERVAL_DEFAULT 10 - -/* -- Profiler/hook interaction ------------------------------------------- */ - -#if !LJ_PROFILE_SIGPROF -void LJ_FASTCALL lj_profile_hook_enter(global_State *g) -{ - ProfileState *ps = &profile_state; - if (ps->g) { - profile_lock(ps); - hook_enter(g); - profile_unlock(ps); - } else { - hook_enter(g); - } -} - -void LJ_FASTCALL lj_profile_hook_leave(global_State *g) -{ - ProfileState *ps = &profile_state; - if (ps->g) { - profile_lock(ps); - hook_leave(g); - profile_unlock(ps); - } else { - hook_leave(g); - } -} -#endif - -/* -- Profile callbacks --------------------------------------------------- */ - -/* Callback from profile hook (HOOK_PROFILE already cleared). */ -void LJ_FASTCALL lj_profile_interpreter(lua_State *L) -{ - ProfileState *ps = &profile_state; - global_State *g = G(L); - uint8_t mask; - profile_lock(ps); - mask = (g->hookmask & ~HOOK_PROFILE); - if (!(mask & HOOK_VMEVENT)) { - int samples = ps->samples; - ps->samples = 0; - g->hookmask = HOOK_VMEVENT; - lj_dispatch_update(g); - profile_unlock(ps); - ps->cb(ps->data, L, samples, ps->vmstate); /* Invoke user callback. */ - profile_lock(ps); - mask |= (g->hookmask & HOOK_PROFILE); - } - g->hookmask = mask; - lj_dispatch_update(g); - profile_unlock(ps); -} - -/* Trigger profile hook. Asynchronous call from OS-specific profile timer. */ -static void profile_trigger(ProfileState *ps) -{ - global_State *g = ps->g; - uint8_t mask; - profile_lock(ps); - ps->samples++; /* Always increment number of samples. */ - mask = g->hookmask; - if (!(mask & (HOOK_PROFILE|HOOK_VMEVENT|HOOK_GC))) { /* Set profile hook. */ - int st = g->vmstate; - ps->vmstate = st >= 0 ? 'N' : - st == ~LJ_VMST_INTERP ? 'I' : - st == ~LJ_VMST_C ? 'C' : - st == ~LJ_VMST_GC ? 'G' : 'J'; - g->hookmask = (mask | HOOK_PROFILE); - lj_dispatch_update(g); - } - profile_unlock(ps); -} - -/* -- OS-specific profile timer handling ---------------------------------- */ - -#if LJ_PROFILE_SIGPROF - -/* SIGPROF handler. */ -static void profile_signal(int sig) -{ - UNUSED(sig); - profile_trigger(&profile_state); -} - -/* Start profiling timer. */ -static void profile_timer_start(ProfileState *ps) -{ - int interval = ps->interval; - struct itimerval tm; - struct sigaction sa; - tm.it_value.tv_sec = tm.it_interval.tv_sec = interval / 1000; - tm.it_value.tv_usec = tm.it_interval.tv_usec = (interval % 1000) * 1000; - setitimer(ITIMER_PROF, &tm, NULL); -#if LJ_TARGET_QNX - sa.sa_flags = 0; -#else - sa.sa_flags = SA_RESTART; -#endif - sa.sa_handler = profile_signal; - sigemptyset(&sa.sa_mask); - sigaction(SIGPROF, &sa, &ps->oldsa); -} - -/* Stop profiling timer. */ -static void profile_timer_stop(ProfileState *ps) -{ - struct itimerval tm; - tm.it_value.tv_sec = tm.it_interval.tv_sec = 0; - tm.it_value.tv_usec = tm.it_interval.tv_usec = 0; - setitimer(ITIMER_PROF, &tm, NULL); - sigaction(SIGPROF, &ps->oldsa, NULL); -} - -#elif LJ_PROFILE_PTHREAD - -/* POSIX timer thread. */ -static void *profile_thread(ProfileState *ps) -{ - int interval = ps->interval; -#if !LJ_TARGET_PS3 - struct timespec ts; - ts.tv_sec = interval / 1000; - ts.tv_nsec = (interval % 1000) * 1000000; -#endif - while (1) { -#if LJ_TARGET_PS3 - sys_timer_usleep(interval * 1000); -#else - nanosleep(&ts, NULL); -#endif - if (ps->abort) break; - profile_trigger(ps); - } - return NULL; -} - -/* Start profiling timer thread. */ -static void profile_timer_start(ProfileState *ps) -{ - pthread_mutex_init(&ps->lock, 0); - ps->abort = 0; - pthread_create(&ps->thread, NULL, (void *(*)(void *))profile_thread, ps); -} - -/* Stop profiling timer thread. */ -static void profile_timer_stop(ProfileState *ps) -{ - ps->abort = 1; - pthread_join(ps->thread, NULL); - pthread_mutex_destroy(&ps->lock); -} - -#elif LJ_PROFILE_WTHREAD - -/* Windows timer thread. */ -static DWORD WINAPI profile_thread(void *psx) -{ - ProfileState *ps = (ProfileState *)psx; - int interval = ps->interval; -#if LJ_TARGET_WINDOWS && !LJ_TARGET_UWP - ps->wmm_tbp(interval); -#endif - while (1) { - Sleep(interval); - if (ps->abort) break; - profile_trigger(ps); - } -#if LJ_TARGET_WINDOWS && !LJ_TARGET_UWP - ps->wmm_tep(interval); -#endif - return 0; -} - -/* Start profiling timer thread. */ -static void profile_timer_start(ProfileState *ps) -{ -#if LJ_TARGET_WINDOWS && !LJ_TARGET_UWP - if (!ps->wmm) { /* Load WinMM library on-demand. */ - ps->wmm = LJ_WIN_LOADLIBA("winmm.dll"); - if (ps->wmm) { - ps->wmm_tbp = (WMM_TPFUNC)GetProcAddress(ps->wmm, "timeBeginPeriod"); - ps->wmm_tep = (WMM_TPFUNC)GetProcAddress(ps->wmm, "timeEndPeriod"); - if (!ps->wmm_tbp || !ps->wmm_tep) { - ps->wmm = NULL; - return; - } - } - } -#endif - InitializeCriticalSection(&ps->lock); - ps->abort = 0; - ps->thread = CreateThread(NULL, 0, profile_thread, ps, 0, NULL); -} - -/* Stop profiling timer thread. */ -static void profile_timer_stop(ProfileState *ps) -{ - ps->abort = 1; - WaitForSingleObject(ps->thread, INFINITE); - DeleteCriticalSection(&ps->lock); -} - -#endif - -/* -- Public profiling API ------------------------------------------------ */ - -/* Start profiling. */ -LUA_API void luaJIT_profile_start(lua_State *L, const char *mode, - luaJIT_profile_callback cb, void *data) -{ - ProfileState *ps = &profile_state; - int interval = LJ_PROFILE_INTERVAL_DEFAULT; - while (*mode) { - int m = *mode++; - switch (m) { - case 'i': - interval = 0; - while (*mode >= '0' && *mode <= '9') - interval = interval * 10 + (*mode++ - '0'); - if (interval <= 0) interval = 1; - break; -#if LJ_HASJIT - case 'l': case 'f': - L2J(L)->prof_mode = m; - lj_trace_flushall(L); - break; -#endif - default: /* Ignore unknown mode chars. */ - break; - } - } - if (ps->g) { - luaJIT_profile_stop(L); - if (ps->g) return; /* Profiler in use by another VM. */ - } - ps->g = G(L); - ps->interval = interval; - ps->cb = cb; - ps->data = data; - ps->samples = 0; - lj_buf_init(L, &ps->sb); - profile_timer_start(ps); -} - -/* Stop profiling. */ -LUA_API void luaJIT_profile_stop(lua_State *L) -{ - ProfileState *ps = &profile_state; - global_State *g = ps->g; - if (G(L) == g) { /* Only stop profiler if started by this VM. */ - profile_timer_stop(ps); - g->hookmask &= ~HOOK_PROFILE; - lj_dispatch_update(g); -#if LJ_HASJIT - G2J(g)->prof_mode = 0; - lj_trace_flushall(L); -#endif - lj_buf_free(g, &ps->sb); - ps->sb.w = ps->sb.e = NULL; - ps->g = NULL; - } -} - -/* Return a compact stack dump. */ -LUA_API const char *luaJIT_profile_dumpstack(lua_State *L, const char *fmt, - int depth, size_t *len) -{ - ProfileState *ps = &profile_state; - SBuf *sb = &ps->sb; - setsbufL(sb, L); - lj_buf_reset(sb); - lj_debug_dumpstack(L, sb, fmt, depth); - *len = (size_t)sbuflen(sb); - return sb->b; -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.h deleted file mode 100644 index 3969f8e..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_profile.h +++ /dev/null @@ -1,21 +0,0 @@ -/* -** Low-overhead profiling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_PROFILE_H -#define _LJ_PROFILE_H - -#include "lj_obj.h" - -#if LJ_HASPROFILE - -LJ_FUNC void LJ_FASTCALL lj_profile_interpreter(lua_State *L); -#if !LJ_PROFILE_SIGPROF -LJ_FUNC void LJ_FASTCALL lj_profile_hook_enter(global_State *g); -LJ_FUNC void LJ_FASTCALL lj_profile_hook_leave(global_State *g); -#endif - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.c deleted file mode 100644 index 79ee617..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.c +++ /dev/null @@ -1,2838 +0,0 @@ -/* -** Trace recorder (bytecode -> SSA IR). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_record_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_err.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_meta.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_bc.h" -#include "lj_ff.h" -#if LJ_HASPROFILE -#include "lj_debug.h" -#endif -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_ircall.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_record.h" -#include "lj_ffrecord.h" -#include "lj_snap.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_prng.h" - -/* Some local macros to save typing. Undef'd at the end. */ -#define IR(ref) (&J->cur.ir[(ref)]) - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Emit raw IR without passing through optimizations. */ -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- Sanity checks ------------------------------------------------------- */ - -#ifdef LUA_USE_ASSERT -/* Sanity check the whole IR -- sloooow. */ -static void rec_check_ir(jit_State *J) -{ - IRRef i, nins = J->cur.nins, nk = J->cur.nk; - lj_assertJ(nk <= REF_BIAS && nins >= REF_BIAS && nins < 65536, - "inconsistent IR layout"); - for (i = nk; i < nins; i++) { - IRIns *ir = IR(i); - uint32_t mode = lj_ir_mode[ir->o]; - IRRef op1 = ir->op1; - IRRef op2 = ir->op2; - const char *err = NULL; - switch (irm_op1(mode)) { - case IRMnone: - if (op1 != 0) err = "IRMnone op1 used"; - break; - case IRMref: - if (op1 < nk || (i >= REF_BIAS ? op1 >= i : op1 <= i)) - err = "IRMref op1 out of range"; - break; - case IRMlit: break; - case IRMcst: - if (i >= REF_BIAS) { err = "constant in IR range"; break; } - if (irt_is64(ir->t) && ir->o != IR_KNULL) - i++; - continue; - } - switch (irm_op2(mode)) { - case IRMnone: - if (op2) err = "IRMnone op2 used"; - break; - case IRMref: - if (op2 < nk || (i >= REF_BIAS ? op2 >= i : op2 <= i)) - err = "IRMref op2 out of range"; - break; - case IRMlit: break; - case IRMcst: err = "IRMcst op2"; break; - } - if (!err && ir->prev) { - if (ir->prev < nk || (i >= REF_BIAS ? ir->prev >= i : ir->prev <= i)) - err = "chain out of range"; - else if (ir->o != IR_NOP && IR(ir->prev)->o != ir->o) - err = "chain to different op"; - } - lj_assertJ(!err, "bad IR %04d op %d(%04d,%04d): %s", - i-REF_BIAS, - ir->o, - irm_op1(mode) == IRMref ? op1-REF_BIAS : op1, - irm_op2(mode) == IRMref ? op2-REF_BIAS : op2, - err); - } -} - -/* Compare stack slots and frames of the recorder and the VM. */ -static void rec_check_slots(jit_State *J) -{ - BCReg s, nslots = J->baseslot + J->maxslot; - int32_t depth = 0; - cTValue *base = J->L->base - J->baseslot; - lj_assertJ(J->baseslot >= 1+LJ_FR2, "bad baseslot"); - lj_assertJ(J->baseslot == 1+LJ_FR2 || (J->slot[J->baseslot-1] & TREF_FRAME), - "baseslot does not point to frame"); - lj_assertJ(nslots <= LJ_MAX_JSLOTS, "slot overflow"); - for (s = 0; s < nslots; s++) { - TRef tr = J->slot[s]; - if (tr) { - cTValue *tv = &base[s]; - IRRef ref = tref_ref(tr); - IRIns *ir = NULL; /* Silence compiler. */ - if (!LJ_FR2 || ref || !(tr & (TREF_FRAME | TREF_CONT))) { - lj_assertJ(ref >= J->cur.nk && ref < J->cur.nins, - "slot %d ref %04d out of range", s, ref - REF_BIAS); - ir = IR(ref); - lj_assertJ(irt_t(ir->t) == tref_t(tr), "slot %d IR type mismatch", s); - } - if (s == 0) { - lj_assertJ(tref_isfunc(tr), "frame slot 0 is not a function"); -#if LJ_FR2 - } else if (s == 1) { - lj_assertJ((tr & ~TREF_FRAME) == 0, "bad frame slot 1"); -#endif - } else if ((tr & TREF_FRAME)) { - GCfunc *fn = gco2func(frame_gc(tv)); - BCReg delta = (BCReg)(tv - frame_prev(tv)); -#if LJ_FR2 - lj_assertJ(!ref || ir_knum(ir)->u64 == tv->u64, - "frame slot %d PC mismatch", s); - tr = J->slot[s-1]; - ir = IR(tref_ref(tr)); -#endif - lj_assertJ(tref_isfunc(tr), - "frame slot %d is not a function", s-LJ_FR2); - lj_assertJ(!tref_isk(tr) || fn == ir_kfunc(ir), - "frame slot %d function mismatch", s-LJ_FR2); - lj_assertJ(s > delta + LJ_FR2 ? (J->slot[s-delta] & TREF_FRAME) - : (s == delta + LJ_FR2), - "frame slot %d broken chain", s-LJ_FR2); - depth++; - } else if ((tr & TREF_CONT)) { -#if LJ_FR2 - lj_assertJ(!ref || ir_knum(ir)->u64 == tv->u64, - "cont slot %d continuation mismatch", s); -#else - lj_assertJ(ir_kptr(ir) == gcrefp(tv->gcr, void), - "cont slot %d continuation mismatch", s); -#endif - lj_assertJ((J->slot[s+1+LJ_FR2] & TREF_FRAME), - "cont slot %d not followed by frame", s); - depth++; - } else if ((tr & TREF_KEYINDEX)) { - lj_assertJ(tref_isint(tr), "keyindex slot %d bad type %d", - s, tref_type(tr)); - } else { - /* Number repr. may differ, but other types must be the same. */ - lj_assertJ(tvisnumber(tv) ? tref_isnumber(tr) : - itype2irt(tv) == tref_type(tr), - "slot %d type mismatch: stack type %d vs IR type %d", - s, itypemap(tv), tref_type(tr)); - if (tref_isk(tr)) { /* Compare constants. */ - TValue tvk; - lj_ir_kvalue(J->L, &tvk, ir); - lj_assertJ((tvisnum(&tvk) && tvisnan(&tvk)) ? - (tvisnum(tv) && tvisnan(tv)) : - lj_obj_equal(tv, &tvk), - "slot %d const mismatch: stack %016llx vs IR %016llx", - s, tv->u64, tvk.u64); - } - } - } - } - lj_assertJ(J->framedepth == depth, - "frame depth mismatch %d vs %d", J->framedepth, depth); -} -#endif - -/* -- Type handling and specialization ------------------------------------ */ - -/* Note: these functions return tagged references (TRef). */ - -/* Specialize a slot to a specific type. Note: slot can be negative! */ -static TRef sloadt(jit_State *J, int32_t slot, IRType t, int mode) -{ - /* Caller may set IRT_GUARD in t. */ - TRef ref = emitir_raw(IRT(IR_SLOAD, t), (int32_t)J->baseslot+slot, mode); - J->base[slot] = ref; - return ref; -} - -/* Specialize a slot to the runtime type. Note: slot can be negative! */ -static TRef sload(jit_State *J, int32_t slot) -{ - IRType t = itype2irt(&J->L->base[slot]); - TRef ref = emitir_raw(IRTG(IR_SLOAD, t), (int32_t)J->baseslot+slot, - IRSLOAD_TYPECHECK); - if (irtype_ispri(t)) ref = TREF_PRI(t); /* Canonicalize primitive refs. */ - J->base[slot] = ref; - return ref; -} - -/* Get TRef from slot. Load slot and specialize if not done already. */ -#define getslot(J, s) (J->base[(s)] ? J->base[(s)] : sload(J, (int32_t)(s))) - -/* Get TRef for current function. */ -static TRef getcurrf(jit_State *J) -{ - if (J->base[-1-LJ_FR2]) - return J->base[-1-LJ_FR2]; - /* Non-base frame functions ought to be loaded already. */ - lj_assertJ(J->baseslot == 1+LJ_FR2, "bad baseslot"); - return sloadt(J, -1-LJ_FR2, IRT_FUNC, IRSLOAD_READONLY); -} - -/* Compare for raw object equality. -** Returns 0 if the objects are the same. -** Returns 1 if they are different, but the same type. -** Returns 2 for two different types. -** Comparisons between primitives always return 1 -- no caller cares about it. -*/ -int lj_record_objcmp(jit_State *J, TRef a, TRef b, cTValue *av, cTValue *bv) -{ - int diff = !lj_obj_equal(av, bv); - if (!tref_isk2(a, b)) { /* Shortcut, also handles primitives. */ - IRType ta = tref_isinteger(a) ? IRT_INT : tref_type(a); - IRType tb = tref_isinteger(b) ? IRT_INT : tref_type(b); - if (ta != tb) { - /* Widen mixed number/int comparisons to number/number comparison. */ - if (ta == IRT_INT && tb == IRT_NUM) { - a = emitir(IRTN(IR_CONV), a, IRCONV_NUM_INT); - ta = IRT_NUM; - } else if (ta == IRT_NUM && tb == IRT_INT) { - b = emitir(IRTN(IR_CONV), b, IRCONV_NUM_INT); - } else { - return 2; /* Two different types are never equal. */ - } - } - emitir(IRTG(diff ? IR_NE : IR_EQ, ta), a, b); - } - return diff; -} - -/* Constify a value. Returns 0 for non-representable object types. */ -TRef lj_record_constify(jit_State *J, cTValue *o) -{ - if (tvisgcv(o)) - return lj_ir_kgc(J, gcV(o), itype2irt(o)); - else if (tvisint(o)) - return lj_ir_kint(J, intV(o)); - else if (tvisnum(o)) - return lj_ir_knumint(J, numV(o)); - else if (tvisbool(o)) - return TREF_PRI(itype2irt(o)); - else - return 0; /* Can't represent lightuserdata (pointless). */ -} - -/* Emit a VLOAD with the correct type. */ -TRef lj_record_vload(jit_State *J, TRef ref, MSize idx, IRType t) -{ - TRef tr = emitir(IRTG(IR_VLOAD, t), ref, idx); - if (irtype_ispri(t)) tr = TREF_PRI(t); /* Canonicalize primitives. */ - return tr; -} - -/* -- Record loop ops ----------------------------------------------------- */ - -/* Loop event. */ -typedef enum { - LOOPEV_LEAVE, /* Loop is left or not entered. */ - LOOPEV_ENTERLO, /* Loop is entered with a low iteration count left. */ - LOOPEV_ENTER /* Loop is entered. */ -} LoopEvent; - -/* Canonicalize slots: convert integers to numbers. */ -static void canonicalize_slots(jit_State *J) -{ - BCReg s; - if (LJ_DUALNUM) return; - for (s = J->baseslot+J->maxslot-1; s >= 1; s--) { - TRef tr = J->slot[s]; - if (tref_isinteger(tr) && !(tr & TREF_KEYINDEX)) { - IRIns *ir = IR(tref_ref(tr)); - if (!(ir->o == IR_SLOAD && (ir->op2 & (IRSLOAD_READONLY)))) - J->slot[s] = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); - } - } -} - -/* Stop recording. */ -void lj_record_stop(jit_State *J, TraceLink linktype, TraceNo lnk) -{ -#ifdef LUAJIT_ENABLE_TABLE_BUMP - if (J->retryrec) - lj_trace_err(J, LJ_TRERR_RETRY); -#endif - lj_trace_end(J); - J->cur.linktype = (uint8_t)linktype; - J->cur.link = (uint16_t)lnk; - /* Looping back at the same stack level? */ - if (lnk == J->cur.traceno && J->framedepth + J->retdepth == 0) { - if ((J->flags & JIT_F_OPT_LOOP)) /* Shall we try to create a loop? */ - goto nocanon; /* Do not canonicalize or we lose the narrowing. */ - if (J->cur.root) /* Otherwise ensure we always link to the root trace. */ - J->cur.link = J->cur.root; - } - canonicalize_slots(J); -nocanon: - /* Note: all loop ops must set J->pc to the following instruction! */ - lj_snap_add(J); /* Add loop snapshot. */ - J->needsnap = 0; - J->mergesnap = 1; /* In case recording continues. */ -} - -/* Search bytecode backwards for a int/num constant slot initializer. */ -static TRef find_kinit(jit_State *J, const BCIns *endpc, BCReg slot, IRType t) -{ - /* This algorithm is rather simplistic and assumes quite a bit about - ** how the bytecode is generated. It works fine for FORI initializers, - ** but it won't necessarily work in other cases (e.g. iterator arguments). - ** It doesn't do anything fancy, either (like backpropagating MOVs). - */ - const BCIns *pc, *startpc = proto_bc(J->pt); - for (pc = endpc-1; pc > startpc; pc--) { - BCIns ins = *pc; - BCOp op = bc_op(ins); - /* First try to find the last instruction that stores to this slot. */ - if (bcmode_a(op) == BCMbase && bc_a(ins) <= slot) { - return 0; /* Multiple results, e.g. from a CALL or KNIL. */ - } else if (bcmode_a(op) == BCMdst && bc_a(ins) == slot) { - if (op == BC_KSHORT || op == BC_KNUM) { /* Found const. initializer. */ - /* Now try to verify there's no forward jump across it. */ - const BCIns *kpc = pc; - for (; pc > startpc; pc--) - if (bc_op(*pc) == BC_JMP) { - const BCIns *target = pc+bc_j(*pc)+1; - if (target > kpc && target <= endpc) - return 0; /* Conditional assignment. */ - } - if (op == BC_KSHORT) { - int32_t k = (int32_t)(int16_t)bc_d(ins); - return t == IRT_INT ? lj_ir_kint(J, k) : lj_ir_knum(J, (lua_Number)k); - } else { - cTValue *tv = proto_knumtv(J->pt, bc_d(ins)); - if (t == IRT_INT) { - int32_t k = numberVint(tv); - if (tvisint(tv) || numV(tv) == (lua_Number)k) /* -0 is ok here. */ - return lj_ir_kint(J, k); - return 0; /* Type mismatch. */ - } else { - return lj_ir_knum(J, numberVnum(tv)); - } - } - } - return 0; /* Non-constant initializer. */ - } - } - return 0; /* No assignment to this slot found? */ -} - -/* Load and optionally convert a FORI argument from a slot. */ -static TRef fori_load(jit_State *J, BCReg slot, IRType t, int mode) -{ - int conv = (tvisint(&J->L->base[slot]) != (t==IRT_INT)) ? IRSLOAD_CONVERT : 0; - return sloadt(J, (int32_t)slot, - t + (((mode & IRSLOAD_TYPECHECK) || - (conv && t == IRT_INT && !(mode >> 16))) ? - IRT_GUARD : 0), - mode + conv); -} - -/* Peek before FORI to find a const initializer. Otherwise load from slot. */ -static TRef fori_arg(jit_State *J, const BCIns *fori, BCReg slot, - IRType t, int mode) -{ - TRef tr = J->base[slot]; - if (!tr) { - tr = find_kinit(J, fori, slot, t); - if (!tr) - tr = fori_load(J, slot, t, mode); - } - return tr; -} - -/* Return the direction of the FOR loop iterator. -** It's important to exactly reproduce the semantics of the interpreter. -*/ -static int rec_for_direction(cTValue *o) -{ - return (tvisint(o) ? intV(o) : (int32_t)o->u32.hi) >= 0; -} - -/* Simulate the runtime behavior of the FOR loop iterator. */ -static LoopEvent rec_for_iter(IROp *op, cTValue *o, int isforl) -{ - lua_Number stopv = numberVnum(&o[FORL_STOP]); - lua_Number idxv = numberVnum(&o[FORL_IDX]); - lua_Number stepv = numberVnum(&o[FORL_STEP]); - if (isforl) - idxv += stepv; - if (rec_for_direction(&o[FORL_STEP])) { - if (idxv <= stopv) { - *op = IR_LE; - return idxv + 2*stepv > stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER; - } - *op = IR_GT; return LOOPEV_LEAVE; - } else { - if (stopv <= idxv) { - *op = IR_GE; - return idxv + 2*stepv < stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER; - } - *op = IR_LT; return LOOPEV_LEAVE; - } -} - -/* Record checks for FOR loop overflow and step direction. */ -static void rec_for_check(jit_State *J, IRType t, int dir, - TRef stop, TRef step, int init) -{ - if (!tref_isk(step)) { - /* Non-constant step: need a guard for the direction. */ - TRef zero = (t == IRT_INT) ? lj_ir_kint(J, 0) : lj_ir_knum_zero(J); - emitir(IRTG(dir ? IR_GE : IR_LT, t), step, zero); - /* Add hoistable overflow checks for a narrowed FORL index. */ - if (init && t == IRT_INT) { - if (tref_isk(stop)) { - /* Constant stop: optimize check away or to a range check for step. */ - int32_t k = IR(tref_ref(stop))->i; - if (dir) { - if (k > 0) - emitir(IRTGI(IR_LE), step, lj_ir_kint(J, (int32_t)0x7fffffff-k)); - } else { - if (k < 0) - emitir(IRTGI(IR_GE), step, lj_ir_kint(J, (int32_t)0x80000000-k)); - } - } else { - /* Stop+step variable: need full overflow check. */ - TRef tr = emitir(IRTGI(IR_ADDOV), step, stop); - emitir(IRTI(IR_USE), tr, 0); /* ADDOV is weak. Avoid dead result. */ - } - } - } else if (init && t == IRT_INT && !tref_isk(stop)) { - /* Constant step: optimize overflow check to a range check for stop. */ - int32_t k = IR(tref_ref(step))->i; - k = (int32_t)(dir ? 0x7fffffff : 0x80000000) - k; - emitir(IRTGI(dir ? IR_LE : IR_GE), stop, lj_ir_kint(J, k)); - } -} - -/* Record a FORL instruction. */ -static void rec_for_loop(jit_State *J, const BCIns *fori, ScEvEntry *scev, - int init) -{ - BCReg ra = bc_a(*fori); - cTValue *tv = &J->L->base[ra]; - TRef idx = J->base[ra+FORL_IDX]; - IRType t = idx ? tref_type(idx) : - (init || LJ_DUALNUM) ? lj_opt_narrow_forl(J, tv) : IRT_NUM; - int mode = IRSLOAD_INHERIT + - ((!LJ_DUALNUM || tvisint(tv) == (t == IRT_INT)) ? IRSLOAD_READONLY : 0); - TRef stop = fori_arg(J, fori, ra+FORL_STOP, t, mode); - TRef step = fori_arg(J, fori, ra+FORL_STEP, t, mode); - int tc, dir = rec_for_direction(&tv[FORL_STEP]); - lj_assertJ(bc_op(*fori) == BC_FORI || bc_op(*fori) == BC_JFORI, - "bad bytecode %d instead of FORI/JFORI", bc_op(*fori)); - scev->t.irt = t; - scev->dir = dir; - scev->stop = tref_ref(stop); - scev->step = tref_ref(step); - rec_for_check(J, t, dir, stop, step, init); - scev->start = tref_ref(find_kinit(J, fori, ra+FORL_IDX, IRT_INT)); - tc = (LJ_DUALNUM && - !(scev->start && irref_isk(scev->stop) && irref_isk(scev->step) && - tvisint(&tv[FORL_IDX]) == (t == IRT_INT))) ? - IRSLOAD_TYPECHECK : 0; - if (tc) { - J->base[ra+FORL_STOP] = stop; - J->base[ra+FORL_STEP] = step; - } - if (!idx) - idx = fori_load(J, ra+FORL_IDX, t, - IRSLOAD_INHERIT + tc + (J->scev.start << 16)); - if (!init) - J->base[ra+FORL_IDX] = idx = emitir(IRT(IR_ADD, t), idx, step); - J->base[ra+FORL_EXT] = idx; - scev->idx = tref_ref(idx); - setmref(scev->pc, fori); - J->maxslot = ra+FORL_EXT+1; -} - -/* Record FORL/JFORL or FORI/JFORI. */ -static LoopEvent rec_for(jit_State *J, const BCIns *fori, int isforl) -{ - BCReg ra = bc_a(*fori); - TValue *tv = &J->L->base[ra]; - TRef *tr = &J->base[ra]; - IROp op; - LoopEvent ev; - TRef stop; - IRType t; - if (isforl) { /* Handle FORL/JFORL opcodes. */ - TRef idx = tr[FORL_IDX]; - if (mref(J->scev.pc, const BCIns) == fori && tref_ref(idx) == J->scev.idx) { - t = J->scev.t.irt; - stop = J->scev.stop; - idx = emitir(IRT(IR_ADD, t), idx, J->scev.step); - tr[FORL_EXT] = tr[FORL_IDX] = idx; - } else { - ScEvEntry scev; - rec_for_loop(J, fori, &scev, 0); - t = scev.t.irt; - stop = scev.stop; - } - } else { /* Handle FORI/JFORI opcodes. */ - BCReg i; - lj_meta_for(J->L, tv); - t = (LJ_DUALNUM || tref_isint(tr[FORL_IDX])) ? lj_opt_narrow_forl(J, tv) : - IRT_NUM; - for (i = FORL_IDX; i <= FORL_STEP; i++) { - if (!tr[i]) sload(J, ra+i); - lj_assertJ(tref_isnumber_str(tr[i]), "bad FORI argument type"); - if (tref_isstr(tr[i])) - tr[i] = emitir(IRTG(IR_STRTO, IRT_NUM), tr[i], 0); - if (t == IRT_INT) { - if (!tref_isinteger(tr[i])) - tr[i] = emitir(IRTGI(IR_CONV), tr[i], IRCONV_INT_NUM|IRCONV_CHECK); - } else { - if (!tref_isnum(tr[i])) - tr[i] = emitir(IRTN(IR_CONV), tr[i], IRCONV_NUM_INT); - } - } - tr[FORL_EXT] = tr[FORL_IDX]; - stop = tr[FORL_STOP]; - rec_for_check(J, t, rec_for_direction(&tv[FORL_STEP]), - stop, tr[FORL_STEP], 1); - } - - ev = rec_for_iter(&op, tv, isforl); - if (ev == LOOPEV_LEAVE) { - J->maxslot = ra+FORL_EXT+1; - J->pc = fori+1; - } else { - J->maxslot = ra; - J->pc = fori+bc_j(*fori)+1; - } - lj_snap_add(J); - - emitir(IRTG(op, t), tr[FORL_IDX], stop); - - if (ev == LOOPEV_LEAVE) { - J->maxslot = ra; - J->pc = fori+bc_j(*fori)+1; - } else { - J->maxslot = ra+FORL_EXT+1; - J->pc = fori+1; - } - J->needsnap = 1; - return ev; -} - -/* Record ITERL/JITERL. */ -static LoopEvent rec_iterl(jit_State *J, const BCIns iterins) -{ - BCReg ra = bc_a(iterins); - if (!tref_isnil(getslot(J, ra))) { /* Looping back? */ - J->base[ra-1] = J->base[ra]; /* Copy result of ITERC to control var. */ - J->maxslot = ra-1+bc_b(J->pc[-1]); - J->pc += bc_j(iterins)+1; - return LOOPEV_ENTER; - } else { - J->maxslot = ra-3; - J->pc++; - return LOOPEV_LEAVE; - } -} - -/* Record LOOP/JLOOP. Now, that was easy. */ -static LoopEvent rec_loop(jit_State *J, BCReg ra, int skip) -{ - if (ra < J->maxslot) J->maxslot = ra; - J->pc += skip; - return LOOPEV_ENTER; -} - -/* Check if a loop repeatedly failed to trace because it didn't loop back. */ -static int innerloopleft(jit_State *J, const BCIns *pc) -{ - ptrdiff_t i; - for (i = 0; i < PENALTY_SLOTS; i++) - if (mref(J->penalty[i].pc, const BCIns) == pc) { - if ((J->penalty[i].reason == LJ_TRERR_LLEAVE || - J->penalty[i].reason == LJ_TRERR_LINNER) && - J->penalty[i].val >= 2*PENALTY_MIN) - return 1; - break; - } - return 0; -} - -/* Handle the case when an interpreted loop op is hit. */ -static void rec_loop_interp(jit_State *J, const BCIns *pc, LoopEvent ev) -{ - if (J->parent == 0 && J->exitno == 0) { - if (pc == J->startpc && J->framedepth + J->retdepth == 0) { - if (bc_op(J->cur.startins) == BC_ITERN) return; /* See rec_itern(). */ - /* Same loop? */ - if (ev == LOOPEV_LEAVE) /* Must loop back to form a root trace. */ - lj_trace_err(J, LJ_TRERR_LLEAVE); - lj_record_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Looping trace. */ - } else if (ev != LOOPEV_LEAVE) { /* Entering inner loop? */ - /* It's usually better to abort here and wait until the inner loop - ** is traced. But if the inner loop repeatedly didn't loop back, - ** this indicates a low trip count. In this case try unrolling - ** an inner loop even in a root trace. But it's better to be a bit - ** more conservative here and only do it for very short loops. - */ - if (bc_j(*pc) != -1 && !innerloopleft(J, pc)) - lj_trace_err(J, LJ_TRERR_LINNER); /* Root trace hit an inner loop. */ - if ((ev != LOOPEV_ENTERLO && - J->loopref && J->cur.nins - J->loopref > 24) || --J->loopunroll < 0) - lj_trace_err(J, LJ_TRERR_LUNROLL); /* Limit loop unrolling. */ - J->loopref = J->cur.nins; - } - } else if (ev != LOOPEV_LEAVE) { /* Side trace enters an inner loop. */ - J->loopref = J->cur.nins; - if (--J->loopunroll < 0) - lj_trace_err(J, LJ_TRERR_LUNROLL); /* Limit loop unrolling. */ - } /* Side trace continues across a loop that's left or not entered. */ -} - -/* Handle the case when an already compiled loop op is hit. */ -static void rec_loop_jit(jit_State *J, TraceNo lnk, LoopEvent ev) -{ - if (J->parent == 0 && J->exitno == 0) { /* Root trace hit an inner loop. */ - /* Better let the inner loop spawn a side trace back here. */ - lj_trace_err(J, LJ_TRERR_LINNER); - } else if (ev != LOOPEV_LEAVE) { /* Side trace enters a compiled loop. */ - J->instunroll = 0; /* Cannot continue across a compiled loop op. */ - if (J->pc == J->startpc && J->framedepth + J->retdepth == 0) - lj_record_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Form extra loop. */ - else - lj_record_stop(J, LJ_TRLINK_ROOT, lnk); /* Link to the loop. */ - } /* Side trace continues across a loop that's left or not entered. */ -} - -/* Record ITERN. */ -static LoopEvent rec_itern(jit_State *J, BCReg ra, BCReg rb) -{ -#if LJ_BE - /* YAGNI: Disabled on big-endian due to issues with lj_vm_next, - ** IR_HIOP, RID_RETLO/RID_RETHI and ra_destpair. - */ - UNUSED(ra); UNUSED(rb); - setintV(&J->errinfo, (int32_t)BC_ITERN); - lj_trace_err_info(J, LJ_TRERR_NYIBC); -#else - RecordIndex ix; - /* Since ITERN is recorded at the start, we need our own loop detection. */ - if (J->pc == J->startpc && - J->framedepth + J->retdepth == 0 && J->parent == 0 && J->exitno == 0) { - IRRef ref = REF_FIRST + LJ_HASPROFILE; -#ifdef LUAJIT_ENABLE_CHECKHOOK - ref += 3; -#endif - if (J->cur.nins > ref || - (LJ_HASPROFILE && J->cur.nins == ref && J->cur.ir[ref-1].o != IR_PROF)) { - J->instunroll = 0; /* Cannot continue unrolling across an ITERN. */ - lj_record_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Looping trace. */ - return LOOPEV_ENTER; - } - } - J->maxslot = ra; - lj_snap_add(J); /* Required to make JLOOP the first ins in a side-trace. */ - ix.tab = getslot(J, ra-2); - ix.key = J->base[ra-1] ? J->base[ra-1] : - sloadt(J, (int32_t)(ra-1), IRT_GUARD|IRT_INT, - IRSLOAD_TYPECHECK|IRSLOAD_KEYINDEX); - copyTV(J->L, &ix.tabv, &J->L->base[ra-2]); - copyTV(J->L, &ix.keyv, &J->L->base[ra-1]); - ix.idxchain = (rb < 3); /* Omit value type check, if unused. */ - ix.mobj = 1; /* We need the next index, too. */ - J->maxslot = ra + lj_record_next(J, &ix); - J->needsnap = 1; - if (!tref_isnil(ix.key)) { /* Looping back? */ - J->base[ra-1] = ix.mobj | TREF_KEYINDEX; /* Control var has next index. */ - J->base[ra] = ix.key; - J->base[ra+1] = ix.val; - J->pc += bc_j(J->pc[1])+2; - return LOOPEV_ENTER; - } else { - J->maxslot = ra-3; - J->pc += 2; - return LOOPEV_LEAVE; - } -#endif -} - -/* Record ISNEXT. */ -static void rec_isnext(jit_State *J, BCReg ra) -{ - cTValue *b = &J->L->base[ra-3]; - if (tvisfunc(b) && funcV(b)->c.ffid == FF_next && - tvistab(b+1) && tvisnil(b+2)) { - /* These checks are folded away for a compiled pairs(). */ - TRef func = getslot(J, ra-3); - TRef trid = emitir(IRT(IR_FLOAD, IRT_U8), func, IRFL_FUNC_FFID); - emitir(IRTGI(IR_EQ), trid, lj_ir_kint(J, FF_next)); - (void)getslot(J, ra-2); /* Type check for table. */ - (void)getslot(J, ra-1); /* Type check for nil key. */ - J->base[ra-1] = lj_ir_kint(J, 0) | TREF_KEYINDEX; - J->maxslot = ra; - } else { /* Abort trace. Interpreter will despecialize bytecode. */ - lj_trace_err(J, LJ_TRERR_RECERR); - } -} - -/* -- Record profiler hook checks ----------------------------------------- */ - -#if LJ_HASPROFILE - -/* Need to insert profiler hook check? */ -static int rec_profile_need(jit_State *J, GCproto *pt, const BCIns *pc) -{ - GCproto *ppt; - lj_assertJ(J->prof_mode == 'f' || J->prof_mode == 'l', - "bad profiler mode %c", J->prof_mode); - if (!pt) - return 0; - ppt = J->prev_pt; - J->prev_pt = pt; - if (pt != ppt && ppt) { - J->prev_line = -1; - return 1; - } - if (J->prof_mode == 'l') { - BCLine line = lj_debug_line(pt, proto_bcpos(pt, pc)); - BCLine pline = J->prev_line; - J->prev_line = line; - if (pline != line) - return 1; - } - return 0; -} - -static void rec_profile_ins(jit_State *J, const BCIns *pc) -{ - if (J->prof_mode && rec_profile_need(J, J->pt, pc)) { - emitir(IRTG(IR_PROF, IRT_NIL), 0, 0); - lj_snap_add(J); - } -} - -static void rec_profile_ret(jit_State *J) -{ - if (J->prof_mode == 'f') { - emitir(IRTG(IR_PROF, IRT_NIL), 0, 0); - J->prev_pt = NULL; - lj_snap_add(J); - } -} - -#endif - -/* -- Record calls and returns -------------------------------------------- */ - -/* Specialize to the runtime value of the called function or its prototype. */ -static TRef rec_call_specialize(jit_State *J, GCfunc *fn, TRef tr) -{ - TRef kfunc; - if (isluafunc(fn)) { - GCproto *pt = funcproto(fn); - /* Too many closures created? Probably not a monomorphic function. */ - if (pt->flags >= PROTO_CLC_POLY) { /* Specialize to prototype instead. */ - TRef trpt = emitir(IRT(IR_FLOAD, IRT_PGC), tr, IRFL_FUNC_PC); - emitir(IRTG(IR_EQ, IRT_PGC), trpt, lj_ir_kptr(J, proto_bc(pt))); - (void)lj_ir_kgc(J, obj2gco(pt), IRT_PROTO); /* Prevent GC of proto. */ - return tr; - } - } else { - /* Don't specialize to non-monomorphic builtins. */ - switch (fn->c.ffid) { - case FF_coroutine_wrap_aux: - case FF_string_gmatch_aux: - /* NYI: io_file_iter doesn't have an ffid, yet. */ - { /* Specialize to the ffid. */ - TRef trid = emitir(IRT(IR_FLOAD, IRT_U8), tr, IRFL_FUNC_FFID); - emitir(IRTGI(IR_EQ), trid, lj_ir_kint(J, fn->c.ffid)); - } - return tr; - default: - /* NYI: don't specialize to non-monomorphic C functions. */ - break; - } - } - /* Otherwise specialize to the function (closure) value itself. */ - kfunc = lj_ir_kfunc(J, fn); - emitir(IRTG(IR_EQ, IRT_FUNC), tr, kfunc); - return kfunc; -} - -/* Record call setup. */ -static void rec_call_setup(jit_State *J, BCReg func, ptrdiff_t nargs) -{ - RecordIndex ix; - TValue *functv = &J->L->base[func]; - TRef kfunc, *fbase = &J->base[func]; - ptrdiff_t i; - (void)getslot(J, func); /* Ensure func has a reference. */ - for (i = 1; i <= nargs; i++) - (void)getslot(J, func+LJ_FR2+i); /* Ensure all args have a reference. */ - if (!tref_isfunc(fbase[0])) { /* Resolve __call metamethod. */ - ix.tab = fbase[0]; - copyTV(J->L, &ix.tabv, functv); - if (!lj_record_mm_lookup(J, &ix, MM_call) || !tref_isfunc(ix.mobj)) - lj_trace_err(J, LJ_TRERR_NOMM); - for (i = ++nargs; i > LJ_FR2; i--) /* Shift arguments up. */ - fbase[i+LJ_FR2] = fbase[i+LJ_FR2-1]; -#if LJ_FR2 - fbase[2] = fbase[0]; -#endif - fbase[0] = ix.mobj; /* Replace function. */ - functv = &ix.mobjv; - } - kfunc = rec_call_specialize(J, funcV(functv), fbase[0]); -#if LJ_FR2 - fbase[0] = kfunc; - fbase[1] = TREF_FRAME; -#else - fbase[0] = kfunc | TREF_FRAME; -#endif - J->maxslot = (BCReg)nargs; -} - -/* Record call. */ -void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs) -{ - rec_call_setup(J, func, nargs); - /* Bump frame. */ - J->framedepth++; - J->base += func+1+LJ_FR2; - J->baseslot += func+1+LJ_FR2; - if (J->baseslot + J->maxslot >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); -} - -/* Record tail call. */ -void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs) -{ - rec_call_setup(J, func, nargs); - if (frame_isvarg(J->L->base - 1)) { - BCReg cbase = (BCReg)frame_delta(J->L->base - 1); - if (--J->framedepth < 0) - lj_trace_err(J, LJ_TRERR_NYIRETL); - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - func += cbase; - } - /* Move func + args down. */ - if (LJ_FR2 && J->baseslot == 2) - J->base[func+1] = TREF_FRAME; - memmove(&J->base[-1-LJ_FR2], &J->base[func], sizeof(TRef)*(J->maxslot+1+LJ_FR2)); - /* Note: the new TREF_FRAME is now at J->base[-1] (even for slot #0). */ - /* Tailcalls can form a loop, so count towards the loop unroll limit. */ - if (++J->tailcalled > J->loopunroll) - lj_trace_err(J, LJ_TRERR_LUNROLL); -} - -/* Check unroll limits for down-recursion. */ -static int check_downrec_unroll(jit_State *J, GCproto *pt) -{ - IRRef ptref; - for (ptref = J->chain[IR_KGC]; ptref; ptref = IR(ptref)->prev) - if (ir_kgc(IR(ptref)) == obj2gco(pt)) { - int count = 0; - IRRef ref; - for (ref = J->chain[IR_RETF]; ref; ref = IR(ref)->prev) - if (IR(ref)->op1 == ptref) - count++; - if (count) { - if (J->pc == J->startpc) { - if (count + J->tailcalled > J->param[JIT_P_recunroll]) - return 1; - } else { - lj_trace_err(J, LJ_TRERR_DOWNREC); - } - } - } - return 0; -} - -static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot); - -/* Record return. */ -void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) -{ - TValue *frame = J->L->base - 1; - ptrdiff_t i; - for (i = 0; i < gotresults; i++) - (void)getslot(J, rbase+i); /* Ensure all results have a reference. */ - while (frame_ispcall(frame)) { /* Immediately resolve pcall() returns. */ - BCReg cbase = (BCReg)frame_delta(frame); - if (--J->framedepth <= 0) - lj_trace_err(J, LJ_TRERR_NYIRETL); - lj_assertJ(J->baseslot > 1+LJ_FR2, "bad baseslot for return"); - gotresults++; - rbase += cbase; - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - J->base[--rbase] = TREF_TRUE; /* Prepend true to results. */ - frame = frame_prevd(frame); - J->needsnap = 1; /* Stop catching on-trace errors. */ - } - /* Return to lower frame via interpreter for unhandled cases. */ - if (J->framedepth == 0 && J->pt && bc_isret(bc_op(*J->pc)) && - (!frame_islua(frame) || - (J->parent == 0 && J->exitno == 0 && - !bc_isret(bc_op(J->cur.startins))))) { - /* NYI: specialize to frame type and return directly, not via RET*. */ - for (i = 0; i < (ptrdiff_t)rbase; i++) - J->base[i] = 0; /* Purge dead slots. */ - J->maxslot = rbase + (BCReg)gotresults; - lj_record_stop(J, LJ_TRLINK_RETURN, 0); /* Return to interpreter. */ - return; - } - if (frame_isvarg(frame)) { - BCReg cbase = (BCReg)frame_delta(frame); - if (--J->framedepth < 0) /* NYI: return of vararg func to lower frame. */ - lj_trace_err(J, LJ_TRERR_NYIRETL); - lj_assertJ(J->baseslot > 1+LJ_FR2, "bad baseslot for return"); - rbase += cbase; - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - frame = frame_prevd(frame); - } - if (frame_islua(frame)) { /* Return to Lua frame. */ - BCIns callins = *(frame_pc(frame)-1); - ptrdiff_t nresults = bc_b(callins) ? (ptrdiff_t)bc_b(callins)-1 :gotresults; - BCReg cbase = bc_a(callins); - GCproto *pt = funcproto(frame_func(frame - (cbase+1+LJ_FR2))); - if ((pt->flags & PROTO_NOJIT)) - lj_trace_err(J, LJ_TRERR_CJITOFF); - if (J->framedepth == 0 && J->pt && frame == J->L->base - 1) { - if (check_downrec_unroll(J, pt)) { - J->maxslot = (BCReg)(rbase + gotresults); - lj_snap_purge(J); - lj_record_stop(J, LJ_TRLINK_DOWNREC, J->cur.traceno); /* Down-rec. */ - return; - } - lj_snap_add(J); - } - for (i = 0; i < nresults; i++) /* Adjust results. */ - J->base[i-1-LJ_FR2] = i < gotresults ? J->base[rbase+i] : TREF_NIL; - J->maxslot = cbase+(BCReg)nresults; - if (J->framedepth > 0) { /* Return to a frame that is part of the trace. */ - J->framedepth--; - lj_assertJ(J->baseslot > cbase+1+LJ_FR2, "bad baseslot for return"); - J->baseslot -= cbase+1+LJ_FR2; - J->base -= cbase+1+LJ_FR2; - } else if (J->parent == 0 && J->exitno == 0 && - !bc_isret(bc_op(J->cur.startins))) { - /* Return to lower frame would leave the loop in a root trace. */ - lj_trace_err(J, LJ_TRERR_LLEAVE); - } else if (J->needsnap) { /* Tailcalled to ff with side-effects. */ - lj_trace_err(J, LJ_TRERR_NYIRETL); /* No way to insert snapshot here. */ - } else { /* Return to lower frame. Guard for the target we return to. */ - TRef trpt = lj_ir_kgc(J, obj2gco(pt), IRT_PROTO); - TRef trpc = lj_ir_kptr(J, (void *)frame_pc(frame)); - emitir(IRTG(IR_RETF, IRT_PGC), trpt, trpc); - J->retdepth++; - J->needsnap = 1; - lj_assertJ(J->baseslot == 1+LJ_FR2, "bad baseslot for return"); - /* Shift result slots up and clear the slots of the new frame below. */ - memmove(J->base + cbase, J->base-1-LJ_FR2, sizeof(TRef)*nresults); - memset(J->base-1-LJ_FR2, 0, sizeof(TRef)*(cbase+1+LJ_FR2)); - } - } else if (frame_iscont(frame)) { /* Return to continuation frame. */ - ASMFunction cont = frame_contf(frame); - BCReg cbase = (BCReg)frame_delta(frame); - if ((J->framedepth -= 2) < 0) - lj_trace_err(J, LJ_TRERR_NYIRETL); - J->baseslot -= (BCReg)cbase; - J->base -= cbase; - J->maxslot = cbase-(2<base[dst] = gotresults ? J->base[cbase+rbase] : TREF_NIL; - if (dst >= J->maxslot) { - J->maxslot = dst+1; - } - } else if (cont == lj_cont_nop) { - /* Nothing to do here. */ - } else if (cont == lj_cont_cat) { - BCReg bslot = bc_b(*(frame_contpc(frame)-1)); - TRef tr = gotresults ? J->base[cbase+rbase] : TREF_NIL; - if (bslot != J->maxslot) { /* Concatenate the remainder. */ - TValue *b = J->L->base, save; /* Simulate lower frame and result. */ - /* Can't handle MM_concat + CALLT + fast func side-effects. */ - if (J->postproc != LJ_POST_NONE) - lj_trace_err(J, LJ_TRERR_NYIRETL); - J->base[J->maxslot] = tr; - copyTV(J->L, &save, b-(2<L, b-(2<L->base = b - cbase; - tr = rec_cat(J, bslot, cbase-(2<L->base + cbase; /* Undo. */ - J->L->base = b; - copyTV(J->L, b-(2<base[dst] = tr; - if (dst >= J->maxslot) { - J->maxslot = dst+1; - } - } /* Otherwise continue with another __concat call. */ - } else { - /* Result type already specialized. */ - lj_assertJ(cont == lj_cont_condf || cont == lj_cont_condt, - "bad continuation type"); - } - } else { - lj_trace_err(J, LJ_TRERR_NYIRETL); /* NYI: handle return to C frame. */ - } - lj_assertJ(J->baseslot >= 1+LJ_FR2, "bad baseslot for return"); -} - -/* -- Metamethod handling ------------------------------------------------- */ - -/* Prepare to record call to metamethod. */ -static BCReg rec_mm_prep(jit_State *J, ASMFunction cont) -{ - BCReg s, top = cont == lj_cont_cat ? J->maxslot : curr_proto(J->L)->framesize; -#if LJ_FR2 - J->base[top] = lj_ir_k64(J, IR_KNUM, u64ptr(contptr(cont))); - J->base[top+1] = TREF_CONT; -#else - J->base[top] = lj_ir_kptr(J, contptr(cont)) | TREF_CONT; -#endif - J->framedepth++; - for (s = J->maxslot; s < top; s++) - J->base[s] = 0; /* Clear frame gap to avoid resurrecting previous refs. */ - return top+1+LJ_FR2; -} - -/* Record metamethod lookup. */ -int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm) -{ - RecordIndex mix; - GCtab *mt; - if (tref_istab(ix->tab)) { - mt = tabref(tabV(&ix->tabv)->metatable); - mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META); - } else if (tref_isudata(ix->tab)) { - int udtype = udataV(&ix->tabv)->udtype; - mt = tabref(udataV(&ix->tabv)->metatable); - /* The metatables of special userdata objects are treated as immutable. */ - if (udtype != UDTYPE_USERDATA) { - cTValue *mo; - if (LJ_HASFFI && udtype == UDTYPE_FFI_CLIB) { - /* Specialize to the C library namespace object. */ - emitir(IRTG(IR_EQ, IRT_PGC), ix->tab, lj_ir_kptr(J, udataV(&ix->tabv))); - } else { - /* Specialize to the type of userdata. */ - TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), ix->tab, IRFL_UDATA_UDTYPE); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, udtype)); - } - immutable_mt: - mo = lj_tab_getstr(mt, mmname_str(J2G(J), mm)); - if (!mo || tvisnil(mo)) - return 0; /* No metamethod. */ - /* Treat metamethod or index table as immutable, too. */ - if (!(tvisfunc(mo) || tvistab(mo))) - lj_trace_err(J, LJ_TRERR_BADTYPE); - copyTV(J->L, &ix->mobjv, mo); - ix->mobj = lj_ir_kgc(J, gcV(mo), tvisfunc(mo) ? IRT_FUNC : IRT_TAB); - ix->mtv = mt; - ix->mt = TREF_NIL; /* Dummy value for comparison semantics. */ - return 1; /* Got metamethod or index table. */ - } - mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_UDATA_META); - } else { - /* Specialize to base metatable. Must flush mcode in lua_setmetatable(). */ - mt = tabref(basemt_obj(J2G(J), &ix->tabv)); - if (mt == NULL) { - ix->mt = TREF_NIL; - return 0; /* No metamethod. */ - } - /* The cdata metatable is treated as immutable. */ - if (LJ_HASFFI && tref_iscdata(ix->tab)) goto immutable_mt; - ix->mt = mix.tab = lj_ir_ggfload(J, IRT_TAB, - GG_OFS(g.gcroot[GCROOT_BASEMT+itypemap(&ix->tabv)])); - goto nocheck; - } - ix->mt = mt ? mix.tab : TREF_NIL; - emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mix.tab, lj_ir_knull(J, IRT_TAB)); -nocheck: - if (mt) { - GCstr *mmstr = mmname_str(J2G(J), mm); - cTValue *mo = lj_tab_getstr(mt, mmstr); - if (mo && !tvisnil(mo)) - copyTV(J->L, &ix->mobjv, mo); - ix->mtv = mt; - settabV(J->L, &mix.tabv, mt); - setstrV(J->L, &mix.keyv, mmstr); - mix.key = lj_ir_kstr(J, mmstr); - mix.val = 0; - mix.idxchain = 0; - ix->mobj = lj_record_idx(J, &mix); - return !tref_isnil(ix->mobj); /* 1 if metamethod found, 0 if not. */ - } - return 0; /* No metamethod. */ -} - -/* Record call to arithmetic metamethod. */ -static TRef rec_mm_arith(jit_State *J, RecordIndex *ix, MMS mm) -{ - /* Set up metamethod call first to save ix->tab and ix->tabv. */ - BCReg func = rec_mm_prep(J, mm == MM_concat ? lj_cont_cat : lj_cont_ra); - TRef *base = J->base + func; - TValue *basev = J->L->base + func; - base[1+LJ_FR2] = ix->tab; base[2+LJ_FR2] = ix->key; - copyTV(J->L, basev+1+LJ_FR2, &ix->tabv); - copyTV(J->L, basev+2+LJ_FR2, &ix->keyv); - if (!lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ - if (mm != MM_unm) { - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, &ix->keyv); - if (lj_record_mm_lookup(J, ix, mm)) /* Lookup mm on 2nd operand. */ - goto ok; - } - lj_trace_err(J, LJ_TRERR_NOMM); - } -ok: - base[0] = ix->mobj; -#if LJ_FR2 - base[1] = 0; -#endif - copyTV(J->L, basev+0, &ix->mobjv); - lj_record_call(J, func, 2); - return 0; /* No result yet. */ -} - -/* Record call to __len metamethod. */ -static TRef rec_mm_len(jit_State *J, TRef tr, TValue *tv) -{ - RecordIndex ix; - ix.tab = tr; - copyTV(J->L, &ix.tabv, tv); - if (lj_record_mm_lookup(J, &ix, MM_len)) { - BCReg func = rec_mm_prep(J, lj_cont_ra); - TRef *base = J->base + func; - TValue *basev = J->L->base + func; - base[0] = ix.mobj; copyTV(J->L, basev+0, &ix.mobjv); - base += LJ_FR2; - basev += LJ_FR2; - base[1] = tr; copyTV(J->L, basev+1, tv); -#if LJ_52 - base[2] = tr; copyTV(J->L, basev+2, tv); -#else - base[2] = TREF_NIL; setnilV(basev+2); -#endif - lj_record_call(J, func, 2); - } else { - if (LJ_52 && tref_istab(tr)) - return emitir(IRTI(IR_ALEN), tr, TREF_NIL); - lj_trace_err(J, LJ_TRERR_NOMM); - } - return 0; /* No result yet. */ -} - -/* Call a comparison metamethod. */ -static void rec_mm_callcomp(jit_State *J, RecordIndex *ix, int op) -{ - BCReg func = rec_mm_prep(J, (op&1) ? lj_cont_condf : lj_cont_condt); - TRef *base = J->base + func + LJ_FR2; - TValue *tv = J->L->base + func + LJ_FR2; - base[-LJ_FR2] = ix->mobj; base[1] = ix->val; base[2] = ix->key; - copyTV(J->L, tv-LJ_FR2, &ix->mobjv); - copyTV(J->L, tv+1, &ix->valv); - copyTV(J->L, tv+2, &ix->keyv); - lj_record_call(J, func, 2); -} - -/* Record call to equality comparison metamethod (for tab and udata only). */ -static void rec_mm_equal(jit_State *J, RecordIndex *ix, int op) -{ - ix->tab = ix->val; - copyTV(J->L, &ix->tabv, &ix->valv); - if (lj_record_mm_lookup(J, ix, MM_eq)) { /* Lookup mm on 1st operand. */ - cTValue *bv; - TRef mo1 = ix->mobj; - TValue mo1v; - copyTV(J->L, &mo1v, &ix->mobjv); - /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */ - bv = &ix->keyv; - if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else { /* Lookup metamethod on 2nd operand and compare both. */ - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, bv); - if (!lj_record_mm_lookup(J, ix, MM_eq) || - lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv)) - return; - } - rec_mm_callcomp(J, ix, op); - } -} - -/* Record call to ordered comparison metamethods (for arbitrary objects). */ -static void rec_mm_comp(jit_State *J, RecordIndex *ix, int op) -{ - ix->tab = ix->val; - copyTV(J->L, &ix->tabv, &ix->valv); - while (1) { - MMS mm = (op & 2) ? MM_le : MM_lt; /* Try __le + __lt or only __lt. */ -#if LJ_52 - if (!lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, &ix->keyv); - if (!lj_record_mm_lookup(J, ix, mm)) /* Lookup mm on 2nd operand. */ - goto nomatch; - } - rec_mm_callcomp(J, ix, op); - return; -#else - if (lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ - cTValue *bv; - TRef mo1 = ix->mobj; - TValue mo1v; - copyTV(J->L, &mo1v, &ix->mobjv); - /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */ - bv = &ix->keyv; - if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) { - TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META); - emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); - } else { /* Lookup metamethod on 2nd operand and compare both. */ - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, bv); - if (!lj_record_mm_lookup(J, ix, mm) || - lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv)) - goto nomatch; - } - rec_mm_callcomp(J, ix, op); - return; - } -#endif - nomatch: - /* Lookup failed. Retry with __lt and swapped operands. */ - if (!(op & 2)) break; /* Already at __lt. Interpreter will throw. */ - ix->tab = ix->key; ix->key = ix->val; ix->val = ix->tab; - copyTV(J->L, &ix->tabv, &ix->keyv); - copyTV(J->L, &ix->keyv, &ix->valv); - copyTV(J->L, &ix->valv, &ix->tabv); - op ^= 3; - } -} - -#if LJ_HASFFI -/* Setup call to cdata comparison metamethod. */ -static void rec_mm_comp_cdata(jit_State *J, RecordIndex *ix, int op, MMS mm) -{ - lj_snap_add(J); - if (tref_iscdata(ix->val)) { - ix->tab = ix->val; - copyTV(J->L, &ix->tabv, &ix->valv); - } else { - lj_assertJ(tref_iscdata(ix->key), "cdata expected"); - ix->tab = ix->key; - copyTV(J->L, &ix->tabv, &ix->keyv); - } - lj_record_mm_lookup(J, ix, mm); - rec_mm_callcomp(J, ix, op); -} -#endif - -/* -- Indexed access ------------------------------------------------------ */ - -#ifdef LUAJIT_ENABLE_TABLE_BUMP -/* Bump table allocations in bytecode when they grow during recording. */ -static void rec_idx_bump(jit_State *J, RecordIndex *ix) -{ - RBCHashEntry *rbc = &J->rbchash[(ix->tab & (RBCHASH_SLOTS-1))]; - if (tref_ref(ix->tab) == rbc->ref) { - const BCIns *pc = mref(rbc->pc, const BCIns); - GCtab *tb = tabV(&ix->tabv); - uint32_t nhbits; - IRIns *ir; - if (!tvisnil(&ix->keyv)) - (void)lj_tab_set(J->L, tb, &ix->keyv); /* Grow table right now. */ - nhbits = tb->hmask > 0 ? lj_fls(tb->hmask)+1 : 0; - ir = IR(tref_ref(ix->tab)); - if (ir->o == IR_TNEW) { - uint32_t ah = bc_d(*pc); - uint32_t asize = ah & 0x7ff, hbits = ah >> 11; - if (nhbits > hbits) hbits = nhbits; - if (tb->asize > asize) { - asize = tb->asize <= 0x7ff ? tb->asize : 0x7ff; - } - if ((asize | (hbits<<11)) != ah) { /* Has the size changed? */ - /* Patch bytecode, but continue recording (for more patching). */ - setbc_d(pc, (asize | (hbits<<11))); - /* Patching TNEW operands is only safe if the trace is aborted. */ - ir->op1 = asize; ir->op2 = hbits; - J->retryrec = 1; /* Abort the trace at the end of recording. */ - } - } else if (ir->o == IR_TDUP) { - GCtab *tpl = gco2tab(proto_kgc(&gcref(rbc->pt)->pt, ~(ptrdiff_t)bc_d(*pc))); - /* Grow template table, but preserve keys with nil values. */ - if ((tb->asize > tpl->asize && (1u << nhbits)-1 == tpl->hmask) || - (tb->asize == tpl->asize && (1u << nhbits)-1 > tpl->hmask)) { - Node *node = noderef(tpl->node); - uint32_t i, hmask = tpl->hmask, asize; - TValue *array; - for (i = 0; i <= hmask; i++) { - if (!tvisnil(&node[i].key) && tvisnil(&node[i].val)) - settabV(J->L, &node[i].val, tpl); - } - if (!tvisnil(&ix->keyv) && tref_isk(ix->key)) { - TValue *o = lj_tab_set(J->L, tpl, &ix->keyv); - if (tvisnil(o)) settabV(J->L, o, tpl); - } - lj_tab_resize(J->L, tpl, tb->asize, nhbits); - node = noderef(tpl->node); - hmask = tpl->hmask; - for (i = 0; i <= hmask; i++) { - /* This is safe, since template tables only hold immutable values. */ - if (tvistab(&node[i].val)) - setnilV(&node[i].val); - } - /* The shape of the table may have changed. Clean up array part, too. */ - asize = tpl->asize; - array = tvref(tpl->array); - for (i = 0; i < asize; i++) { - if (tvistab(&array[i])) - setnilV(&array[i]); - } - J->retryrec = 1; /* Abort the trace at the end of recording. */ - } - } - } -} -#endif - -/* Record bounds-check. */ -static void rec_idx_abc(jit_State *J, TRef asizeref, TRef ikey, uint32_t asize) -{ - /* Try to emit invariant bounds checks. */ - if ((J->flags & (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) == - (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) { - IRRef ref = tref_ref(ikey); - IRIns *ir = IR(ref); - int32_t ofs = 0; - IRRef ofsref = 0; - /* Handle constant offsets. */ - if (ir->o == IR_ADD && irref_isk(ir->op2)) { - ofsref = ir->op2; - ofs = IR(ofsref)->i; - ref = ir->op1; - ir = IR(ref); - } - /* Got scalar evolution analysis results for this reference? */ - if (ref == J->scev.idx) { - int32_t stop; - lj_assertJ(irt_isint(J->scev.t) && ir->o == IR_SLOAD, - "only int SCEV supported"); - stop = numberVint(&(J->L->base - J->baseslot)[ir->op1 + FORL_STOP]); - /* Runtime value for stop of loop is within bounds? */ - if ((uint64_t)stop + ofs < (uint64_t)asize) { - /* Emit invariant bounds check for stop. */ - emitir(IRTG(IR_ABC, IRT_P32), asizeref, ofs == 0 ? J->scev.stop : - emitir(IRTI(IR_ADD), J->scev.stop, ofsref)); - /* Emit invariant bounds check for start, if not const or negative. */ - if (!(J->scev.dir && J->scev.start && - (int64_t)IR(J->scev.start)->i + ofs >= 0)) - emitir(IRTG(IR_ABC, IRT_P32), asizeref, ikey); - return; - } - } - } - emitir(IRTGI(IR_ABC), asizeref, ikey); /* Emit regular bounds check. */ -} - -/* Record indexed key lookup. */ -static TRef rec_idx_key(jit_State *J, RecordIndex *ix, IRRef *rbref, - IRType1 *rbguard) -{ - TRef key; - GCtab *t = tabV(&ix->tabv); - ix->oldv = lj_tab_get(J->L, t, &ix->keyv); /* Lookup previous value. */ - *rbref = 0; - rbguard->irt = 0; - - /* Integer keys are looked up in the array part first. */ - key = ix->key; - if (tref_isnumber(key)) { - int32_t k = numberVint(&ix->keyv); - if (!tvisint(&ix->keyv) && numV(&ix->keyv) != (lua_Number)k) - k = LJ_MAX_ASIZE; - if ((MSize)k < LJ_MAX_ASIZE) { /* Potential array key? */ - TRef ikey = lj_opt_narrow_index(J, key); - TRef asizeref = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE); - if ((MSize)k < t->asize) { /* Currently an array key? */ - TRef arrayref; - rec_idx_abc(J, asizeref, ikey, t->asize); - arrayref = emitir(IRT(IR_FLOAD, IRT_PGC), ix->tab, IRFL_TAB_ARRAY); - return emitir(IRT(IR_AREF, IRT_PGC), arrayref, ikey); - } else { /* Currently not in array (may be an array extension)? */ - emitir(IRTGI(IR_ULE), asizeref, ikey); /* Inv. bounds check. */ - if (k == 0 && tref_isk(key)) - key = lj_ir_knum_zero(J); /* Canonicalize 0 or +-0.0 to +0.0. */ - /* And continue with the hash lookup. */ - } - } else if (!tref_isk(key)) { - /* We can rule out const numbers which failed the integerness test - ** above. But all other numbers are potential array keys. - */ - if (t->asize == 0) { /* True sparse tables have an empty array part. */ - /* Guard that the array part stays empty. */ - TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE); - emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0)); - } else { - lj_trace_err(J, LJ_TRERR_NYITMIX); - } - } - } - - /* Otherwise the key is located in the hash part. */ - if (t->hmask == 0) { /* Shortcut for empty hash part. */ - /* Guard that the hash part stays empty. */ - TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK); - emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0)); - return lj_ir_kkptr(J, niltvg(J2G(J))); - } - if (tref_isinteger(key)) /* Hash keys are based on numbers, not ints. */ - key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT); - if (tref_isk(key)) { - /* Optimize lookup of constant hash keys. */ - GCSize hslot = (GCSize)((char *)ix->oldv-(char *)&noderef(t->node)[0].val); - if (hslot <= t->hmask*(GCSize)sizeof(Node) && - hslot <= 65535*(GCSize)sizeof(Node)) { - TRef node, kslot, hm; - *rbref = J->cur.nins; /* Mark possible rollback point. */ - *rbguard = J->guardemit; - hm = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK); - emitir(IRTGI(IR_EQ), hm, lj_ir_kint(J, (int32_t)t->hmask)); - node = emitir(IRT(IR_FLOAD, IRT_PGC), ix->tab, IRFL_TAB_NODE); - kslot = lj_ir_kslot(J, key, (IRRef)(hslot / sizeof(Node))); - return emitir(IRTG(IR_HREFK, IRT_PGC), node, kslot); - } - } - /* Fall back to a regular hash lookup. */ - return emitir(IRT(IR_HREF, IRT_PGC), ix->tab, key); -} - -/* Determine whether a key is NOT one of the fast metamethod names. */ -static int nommstr(jit_State *J, TRef key) -{ - if (tref_isstr(key)) { - if (tref_isk(key)) { - GCstr *str = ir_kstr(IR(tref_ref(key))); - uint32_t mm; - for (mm = 0; mm <= MM_FAST; mm++) - if (mmname_str(J2G(J), mm) == str) - return 0; /* MUST be one the fast metamethod names. */ - } else { - return 0; /* Variable string key MAY be a metamethod name. */ - } - } - return 1; /* CANNOT be a metamethod name. */ -} - -/* Record indexed load/store. */ -TRef lj_record_idx(jit_State *J, RecordIndex *ix) -{ - TRef xref; - IROp xrefop, loadop; - IRRef rbref; - IRType1 rbguard; - cTValue *oldv; - - while (!tref_istab(ix->tab)) { /* Handle non-table lookup. */ - /* Never call raw lj_record_idx() on non-table. */ - lj_assertJ(ix->idxchain != 0, "bad usage"); - if (!lj_record_mm_lookup(J, ix, ix->val ? MM_newindex : MM_index)) - lj_trace_err(J, LJ_TRERR_NOMM); - handlemm: - if (tref_isfunc(ix->mobj)) { /* Handle metamethod call. */ - BCReg func = rec_mm_prep(J, ix->val ? lj_cont_nop : lj_cont_ra); - TRef *base = J->base + func + LJ_FR2; - TValue *tv = J->L->base + func + LJ_FR2; - base[-LJ_FR2] = ix->mobj; base[1] = ix->tab; base[2] = ix->key; - setfuncV(J->L, tv-LJ_FR2, funcV(&ix->mobjv)); - copyTV(J->L, tv+1, &ix->tabv); - copyTV(J->L, tv+2, &ix->keyv); - if (ix->val) { - base[3] = ix->val; - copyTV(J->L, tv+3, &ix->valv); - lj_record_call(J, func, 3); /* mobj(tab, key, val) */ - return 0; - } else { - lj_record_call(J, func, 2); /* res = mobj(tab, key) */ - return 0; /* No result yet. */ - } - } -#if LJ_HASBUFFER - /* The index table of buffer objects is treated as immutable. */ - if (ix->mt == TREF_NIL && !ix->val && - tref_isudata(ix->tab) && udataV(&ix->tabv)->udtype == UDTYPE_BUFFER && - tref_istab(ix->mobj) && tref_isstr(ix->key) && tref_isk(ix->key)) { - cTValue *val = lj_tab_getstr(tabV(&ix->mobjv), strV(&ix->keyv)); - TRef tr = lj_record_constify(J, val); - if (tr) return tr; /* Specialize to the value, i.e. a method. */ - } -#endif - /* Otherwise retry lookup with metaobject. */ - ix->tab = ix->mobj; - copyTV(J->L, &ix->tabv, &ix->mobjv); - if (--ix->idxchain == 0) - lj_trace_err(J, LJ_TRERR_IDXLOOP); - } - - /* First catch nil and NaN keys for tables. */ - if (tvisnil(&ix->keyv) || (tvisnum(&ix->keyv) && tvisnan(&ix->keyv))) { - if (ix->val) /* Better fail early. */ - lj_trace_err(J, LJ_TRERR_STORENN); - if (tref_isk(ix->key)) { - if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_index)) - goto handlemm; - return TREF_NIL; - } - } - - /* Record the key lookup. */ - xref = rec_idx_key(J, ix, &rbref, &rbguard); - xrefop = IR(tref_ref(xref))->o; - loadop = xrefop == IR_AREF ? IR_ALOAD : IR_HLOAD; - /* The lj_meta_tset() inconsistency is gone, but better play safe. */ - oldv = xrefop == IR_KKPTR ? (cTValue *)ir_kptr(IR(tref_ref(xref))) : ix->oldv; - - if (ix->val == 0) { /* Indexed load */ - IRType t = itype2irt(oldv); - TRef res; - if (oldv == niltvg(J2G(J))) { - emitir(IRTG(IR_EQ, IRT_PGC), xref, lj_ir_kkptr(J, niltvg(J2G(J)))); - res = TREF_NIL; - } else { - res = emitir(IRTG(loadop, t), xref, 0); - } - if (tref_ref(res) < rbref) { /* HREFK + load forwarded? */ - lj_ir_rollback(J, rbref); /* Rollback to eliminate hmask guard. */ - J->guardemit = rbguard; - } - if (t == IRT_NIL && ix->idxchain && lj_record_mm_lookup(J, ix, MM_index)) - goto handlemm; - if (irtype_ispri(t)) res = TREF_PRI(t); /* Canonicalize primitives. */ - return res; - } else { /* Indexed store. */ - GCtab *mt = tabref(tabV(&ix->tabv)->metatable); - int keybarrier = tref_isgcv(ix->key) && !tref_isnil(ix->val); - if (tref_ref(xref) < rbref) { /* HREFK forwarded? */ - lj_ir_rollback(J, rbref); /* Rollback to eliminate hmask guard. */ - J->guardemit = rbguard; - } - if (tvisnil(oldv)) { /* Previous value was nil? */ - /* Need to duplicate the hasmm check for the early guards. */ - int hasmm = 0; - if (ix->idxchain && mt) { - cTValue *mo = lj_tab_getstr(mt, mmname_str(J2G(J), MM_newindex)); - hasmm = mo && !tvisnil(mo); - } - if (hasmm) - emitir(IRTG(loadop, IRT_NIL), xref, 0); /* Guard for nil value. */ - else if (xrefop == IR_HREF) - emitir(IRTG(oldv == niltvg(J2G(J)) ? IR_EQ : IR_NE, IRT_PGC), - xref, lj_ir_kkptr(J, niltvg(J2G(J)))); - if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_newindex)) { - lj_assertJ(hasmm, "inconsistent metamethod handling"); - goto handlemm; - } - lj_assertJ(!hasmm, "inconsistent metamethod handling"); - if (oldv == niltvg(J2G(J))) { /* Need to insert a new key. */ - TRef key = ix->key; - if (tref_isinteger(key)) /* NEWREF needs a TValue as a key. */ - key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT); - xref = emitir(IRT(IR_NEWREF, IRT_PGC), ix->tab, key); - keybarrier = 0; /* NEWREF already takes care of the key barrier. */ -#ifdef LUAJIT_ENABLE_TABLE_BUMP - if ((J->flags & JIT_F_OPT_SINK)) /* Avoid a separate flag. */ - rec_idx_bump(J, ix); -#endif - } - } else if (!lj_opt_fwd_wasnonnil(J, loadop, tref_ref(xref))) { - /* Cannot derive that the previous value was non-nil, must do checks. */ - if (xrefop == IR_HREF) /* Guard against store to niltv. */ - emitir(IRTG(IR_NE, IRT_PGC), xref, lj_ir_kkptr(J, niltvg(J2G(J)))); - if (ix->idxchain) { /* Metamethod lookup required? */ - /* A check for NULL metatable is cheaper (hoistable) than a load. */ - if (!mt) { - TRef mtref = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META); - emitir(IRTG(IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB)); - } else { - IRType t = itype2irt(oldv); - emitir(IRTG(loadop, t), xref, 0); /* Guard for non-nil value. */ - } - } - } else { - keybarrier = 0; /* Previous non-nil value kept the key alive. */ - } - /* Convert int to number before storing. */ - if (!LJ_DUALNUM && tref_isinteger(ix->val)) - ix->val = emitir(IRTN(IR_CONV), ix->val, IRCONV_NUM_INT); - emitir(IRT(loadop+IRDELTA_L2S, tref_type(ix->val)), xref, ix->val); - if (keybarrier || tref_isgcv(ix->val)) - emitir(IRT(IR_TBAR, IRT_NIL), ix->tab, 0); - /* Invalidate neg. metamethod cache for stores with certain string keys. */ - if (!nommstr(J, ix->key)) { - TRef fref = emitir(IRT(IR_FREF, IRT_PGC), ix->tab, IRFL_TAB_NOMM); - emitir(IRT(IR_FSTORE, IRT_U8), fref, lj_ir_kint(J, 0)); - } - J->needsnap = 1; - return 0; - } -} - -/* Determine result type of table traversal. */ -static IRType rec_next_types(GCtab *t, uint32_t idx) -{ - for (; idx < t->asize; idx++) { - cTValue *a = arrayslot(t, idx); - if (LJ_LIKELY(!tvisnil(a))) - return (LJ_DUALNUM ? IRT_INT : IRT_NUM) + (itype2irt(a) << 8); - } - idx -= t->asize; - for (; idx <= t->hmask; idx++) { - Node *n = &noderef(t->node)[idx]; - if (!tvisnil(&n->val)) - return itype2irt(&n->key) + (itype2irt(&n->val) << 8); - } - return IRT_NIL + (IRT_NIL << 8); -} - -/* Record a table traversal step aka next(). */ -int lj_record_next(jit_State *J, RecordIndex *ix) -{ - IRType t, tkey, tval; - TRef trvk; - t = rec_next_types(tabV(&ix->tabv), ix->keyv.u32.lo); - tkey = (t & 0xff); tval = (t >> 8); - trvk = lj_ir_call(J, IRCALL_lj_vm_next, ix->tab, ix->key); - if (ix->mobj || tkey == IRT_NIL) { - TRef idx = emitir(IRTI(IR_HIOP), trvk, trvk); - /* Always check for invalid key from next() for nil result. */ - if (!ix->mobj) emitir(IRTGI(IR_NE), idx, lj_ir_kint(J, -1)); - ix->mobj = idx; - } - ix->key = lj_record_vload(J, trvk, 1, tkey); - if (tkey == IRT_NIL || ix->idxchain) { /* Omit value type check. */ - ix->val = TREF_NIL; - return 1; - } else { /* Need value. */ - ix->val = lj_record_vload(J, trvk, 0, tval); - return 2; - } -} - -static void rec_tsetm(jit_State *J, BCReg ra, BCReg rn, int32_t i) -{ - RecordIndex ix; - cTValue *basev = J->L->base; - GCtab *t = tabV(&basev[ra-1]); - settabV(J->L, &ix.tabv, t); - ix.tab = getslot(J, ra-1); - ix.idxchain = 0; -#ifdef LUAJIT_ENABLE_TABLE_BUMP - if ((J->flags & JIT_F_OPT_SINK)) { - if (t->asize < i+rn-ra) - lj_tab_reasize(J->L, t, i+rn-ra); - setnilV(&ix.keyv); - rec_idx_bump(J, &ix); - } -#endif - for (; ra < rn; i++, ra++) { - setintV(&ix.keyv, i); - ix.key = lj_ir_kint(J, i); - copyTV(J->L, &ix.valv, &basev[ra]); - ix.val = getslot(J, ra); - lj_record_idx(J, &ix); - } -} - -/* -- Upvalue access ------------------------------------------------------ */ - -/* Check whether upvalue is immutable and ok to constify. */ -static int rec_upvalue_constify(jit_State *J, GCupval *uvp) -{ - if (uvp->immutable) { - cTValue *o = uvval(uvp); - /* Don't constify objects that may retain large amounts of memory. */ -#if LJ_HASFFI - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - if (!cdataisv(cd) && !(cd->marked & LJ_GC_CDATA_FIN)) { - CType *ct = ctype_raw(ctype_ctsG(J2G(J)), cd->ctypeid); - if (!ctype_hassize(ct->info) || ct->size <= 16) - return 1; - } - return 0; - } -#else - UNUSED(J); -#endif - if (!(tvistab(o) || tvisudata(o) || tvisthread(o))) - return 1; - } - return 0; -} - -/* Record upvalue load/store. */ -static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val) -{ - GCupval *uvp = &gcref(J->fn->l.uvptr[uv])->uv; - TRef fn = getcurrf(J); - IRRef uref; - int needbarrier = 0; - if (rec_upvalue_constify(J, uvp)) { /* Try to constify immutable upvalue. */ - TRef tr, kfunc; - lj_assertJ(val == 0, "bad usage"); - if (!tref_isk(fn)) { /* Late specialization of current function. */ - if (J->pt->flags >= PROTO_CLC_POLY) - goto noconstify; - kfunc = lj_ir_kfunc(J, J->fn); - emitir(IRTG(IR_EQ, IRT_FUNC), fn, kfunc); -#if LJ_FR2 - J->base[-2] = kfunc; -#else - J->base[-1] = kfunc | TREF_FRAME; -#endif - fn = kfunc; - } - tr = lj_record_constify(J, uvval(uvp)); - if (tr) - return tr; - } -noconstify: - /* Note: this effectively limits LJ_MAX_UPVAL to 127. */ - uv = (uv << 8) | (hashrot(uvp->dhash, uvp->dhash + HASH_BIAS) & 0xff); - if (!uvp->closed) { - uref = tref_ref(emitir(IRTG(IR_UREFO, IRT_PGC), fn, uv)); - /* In current stack? */ - if (uvval(uvp) >= tvref(J->L->stack) && - uvval(uvp) < tvref(J->L->maxstack)) { - int32_t slot = (int32_t)(uvval(uvp) - (J->L->base - J->baseslot)); - if (slot >= 0) { /* Aliases an SSA slot? */ - emitir(IRTG(IR_EQ, IRT_PGC), - REF_BASE, - emitir(IRT(IR_ADD, IRT_PGC), uref, - lj_ir_kint(J, (slot - 1 - LJ_FR2) * -8))); - slot -= (int32_t)J->baseslot; /* Note: slot number may be negative! */ - if (val == 0) { - return getslot(J, slot); - } else { - J->base[slot] = val; - if (slot >= (int32_t)J->maxslot) J->maxslot = (BCReg)(slot+1); - return 0; - } - } - } - emitir(IRTG(IR_UGT, IRT_PGC), - emitir(IRT(IR_SUB, IRT_PGC), uref, REF_BASE), - lj_ir_kint(J, (J->baseslot + J->maxslot) * 8)); - } else { - needbarrier = 1; - uref = tref_ref(emitir(IRTG(IR_UREFC, IRT_PGC), fn, uv)); - } - if (val == 0) { /* Upvalue load */ - IRType t = itype2irt(uvval(uvp)); - TRef res = emitir(IRTG(IR_ULOAD, t), uref, 0); - if (irtype_ispri(t)) res = TREF_PRI(t); /* Canonicalize primitive refs. */ - return res; - } else { /* Upvalue store. */ - /* Convert int to number before storing. */ - if (!LJ_DUALNUM && tref_isinteger(val)) - val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT); - emitir(IRT(IR_USTORE, tref_type(val)), uref, val); - if (needbarrier && tref_isgcv(val)) - emitir(IRT(IR_OBAR, IRT_NIL), uref, val); - J->needsnap = 1; - return 0; - } -} - -/* -- Record calls to Lua functions --------------------------------------- */ - -/* Check unroll limits for calls. */ -static void check_call_unroll(jit_State *J, TraceNo lnk) -{ - cTValue *frame = J->L->base - 1; - void *pc = mref(frame_func(frame)->l.pc, void); - int32_t depth = J->framedepth; - int32_t count = 0; - if ((J->pt->flags & PROTO_VARARG)) depth--; /* Vararg frame still missing. */ - for (; depth > 0; depth--) { /* Count frames with same prototype. */ - if (frame_iscont(frame)) depth--; - frame = frame_prev(frame); - if (mref(frame_func(frame)->l.pc, void) == pc) - count++; - } - if (J->pc == J->startpc) { - if (count + J->tailcalled > J->param[JIT_P_recunroll]) { - J->pc++; - if (J->framedepth + J->retdepth == 0) - lj_record_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno); /* Tail-rec. */ - else - lj_record_stop(J, LJ_TRLINK_UPREC, J->cur.traceno); /* Up-recursion. */ - } - } else { - if (count > J->param[JIT_P_callunroll]) { - if (lnk) { /* Possible tail- or up-recursion. */ - lj_trace_flush(J, lnk); /* Flush trace that only returns. */ - /* Set a small, pseudo-random hotcount for a quick retry of JFUNC*. */ - hotcount_set(J2GG(J), J->pc+1, lj_prng_u64(&J2G(J)->prng) & 15u); - } - lj_trace_err(J, LJ_TRERR_CUNROLL); - } - } -} - -/* Record Lua function setup. */ -static void rec_func_setup(jit_State *J) -{ - GCproto *pt = J->pt; - BCReg s, numparams = pt->numparams; - if ((pt->flags & PROTO_NOJIT)) - lj_trace_err(J, LJ_TRERR_CJITOFF); - if (J->baseslot + pt->framesize >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - /* Fill up missing parameters with nil. */ - for (s = J->maxslot; s < numparams; s++) - J->base[s] = TREF_NIL; - /* The remaining slots should never be read before they are written. */ - J->maxslot = numparams; -} - -/* Record Lua vararg function setup. */ -static void rec_func_vararg(jit_State *J) -{ - GCproto *pt = J->pt; - BCReg s, fixargs, vframe = J->maxslot+1+LJ_FR2; - lj_assertJ((pt->flags & PROTO_VARARG), "FUNCV in non-vararg function"); - if (J->baseslot + vframe + pt->framesize >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - J->base[vframe-1-LJ_FR2] = J->base[-1-LJ_FR2]; /* Copy function up. */ -#if LJ_FR2 - J->base[vframe-1] = TREF_FRAME; -#endif - /* Copy fixarg slots up and set their original slots to nil. */ - fixargs = pt->numparams < J->maxslot ? pt->numparams : J->maxslot; - for (s = 0; s < fixargs; s++) { - J->base[vframe+s] = J->base[s]; - J->base[s] = TREF_NIL; - } - J->maxslot = fixargs; - J->framedepth++; - J->base += vframe; - J->baseslot += vframe; -} - -/* Record entry to a Lua function. */ -static void rec_func_lua(jit_State *J) -{ - rec_func_setup(J); - check_call_unroll(J, 0); -} - -/* Record entry to an already compiled function. */ -static void rec_func_jit(jit_State *J, TraceNo lnk) -{ - GCtrace *T; - rec_func_setup(J); - T = traceref(J, lnk); - if (T->linktype == LJ_TRLINK_RETURN) { /* Trace returns to interpreter? */ - check_call_unroll(J, lnk); - /* Temporarily unpatch JFUNC* to continue recording across function. */ - J->patchins = *J->pc; - J->patchpc = (BCIns *)J->pc; - *J->patchpc = T->startins; - return; - } - J->instunroll = 0; /* Cannot continue across a compiled function. */ - if (J->pc == J->startpc && J->framedepth + J->retdepth == 0) - lj_record_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno); /* Extra tail-rec. */ - else - lj_record_stop(J, LJ_TRLINK_ROOT, lnk); /* Link to the function. */ -} - -/* -- Vararg handling ----------------------------------------------------- */ - -/* Detect y = select(x, ...) idiom. */ -static int select_detect(jit_State *J) -{ - BCIns ins = J->pc[1]; - if (bc_op(ins) == BC_CALLM && bc_b(ins) == 2 && bc_c(ins) == 1) { - cTValue *func = &J->L->base[bc_a(ins)]; - if (tvisfunc(func) && funcV(func)->c.ffid == FF_select) { - TRef kfunc = lj_ir_kfunc(J, funcV(func)); - emitir(IRTG(IR_EQ, IRT_FUNC), getslot(J, bc_a(ins)), kfunc); - return 1; - } - } - return 0; -} - -/* Record vararg instruction. */ -static void rec_varg(jit_State *J, BCReg dst, ptrdiff_t nresults) -{ - int32_t numparams = J->pt->numparams; - ptrdiff_t nvararg = frame_delta(J->L->base-1) - numparams - 1 - LJ_FR2; - lj_assertJ(frame_isvarg(J->L->base-1), "VARG in non-vararg frame"); - if (LJ_FR2 && dst > J->maxslot) - J->base[dst-1] = 0; /* Prevent resurrection of unrelated slot. */ - if (J->framedepth > 0) { /* Simple case: varargs defined on-trace. */ - ptrdiff_t i; - if (nvararg < 0) nvararg = 0; - if (nresults == -1) { - nresults = nvararg; - J->maxslot = dst + (BCReg)nvararg; - } else if (dst + nresults > J->maxslot) { - J->maxslot = dst + (BCReg)nresults; - } - if (J->baseslot + J->maxslot >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - for (i = 0; i < nresults; i++) - J->base[dst+i] = i < nvararg ? getslot(J, i - nvararg - 1 - LJ_FR2) : TREF_NIL; - } else { /* Unknown number of varargs passed to trace. */ - TRef fr = emitir(IRTI(IR_SLOAD), LJ_FR2, IRSLOAD_READONLY|IRSLOAD_FRAME); - int32_t frofs = 8*(1+LJ_FR2+numparams)+FRAME_VARG; - if (nresults >= 0) { /* Known fixed number of results. */ - ptrdiff_t i; - if (nvararg > 0) { - ptrdiff_t nload = nvararg >= nresults ? nresults : nvararg; - TRef vbase; - if (nvararg >= nresults) - emitir(IRTGI(IR_GE), fr, lj_ir_kint(J, frofs+8*(int32_t)nresults)); - else - emitir(IRTGI(IR_EQ), fr, - lj_ir_kint(J, (int32_t)frame_ftsz(J->L->base-1))); - vbase = emitir(IRT(IR_SUB, IRT_IGC), REF_BASE, fr); - vbase = emitir(IRT(IR_ADD, IRT_PGC), vbase, lj_ir_kint(J, frofs-8*(1+LJ_FR2))); - for (i = 0; i < nload; i++) { - IRType t = itype2irt(&J->L->base[i-1-LJ_FR2-nvararg]); - J->base[dst+i] = lj_record_vload(J, vbase, (MSize)i, t); - } - } else { - emitir(IRTGI(IR_LE), fr, lj_ir_kint(J, frofs)); - nvararg = 0; - } - for (i = nvararg; i < nresults; i++) - J->base[dst+i] = TREF_NIL; - if (dst + (BCReg)nresults > J->maxslot) - J->maxslot = dst + (BCReg)nresults; - } else if (select_detect(J)) { /* y = select(x, ...) */ - TRef tridx = J->base[dst-1]; - TRef tr = TREF_NIL; - ptrdiff_t idx = lj_ffrecord_select_mode(J, tridx, &J->L->base[dst-1]); - if (idx < 0) goto nyivarg; - if (idx != 0 && !tref_isinteger(tridx)) - tridx = emitir(IRTGI(IR_CONV), tridx, IRCONV_INT_NUM|IRCONV_INDEX); - if (idx != 0 && tref_isk(tridx)) { - emitir(IRTGI(idx <= nvararg ? IR_GE : IR_LT), - fr, lj_ir_kint(J, frofs+8*(int32_t)idx)); - frofs -= 8; /* Bias for 1-based index. */ - } else if (idx <= nvararg) { /* Compute size. */ - TRef tmp = emitir(IRTI(IR_ADD), fr, lj_ir_kint(J, -frofs)); - if (numparams) - emitir(IRTGI(IR_GE), tmp, lj_ir_kint(J, 0)); - tr = emitir(IRTI(IR_BSHR), tmp, lj_ir_kint(J, 3)); - if (idx != 0) { - tridx = emitir(IRTI(IR_ADD), tridx, lj_ir_kint(J, -1)); - rec_idx_abc(J, tr, tridx, (uint32_t)nvararg); - } - } else { - TRef tmp = lj_ir_kint(J, frofs); - if (idx != 0) { - TRef tmp2 = emitir(IRTI(IR_BSHL), tridx, lj_ir_kint(J, 3)); - tmp = emitir(IRTI(IR_ADD), tmp2, tmp); - } else { - tr = lj_ir_kint(J, 0); - } - emitir(IRTGI(IR_LT), fr, tmp); - } - if (idx != 0 && idx <= nvararg) { - IRType t; - TRef aref, vbase = emitir(IRT(IR_SUB, IRT_IGC), REF_BASE, fr); - vbase = emitir(IRT(IR_ADD, IRT_PGC), vbase, - lj_ir_kint(J, frofs-(8<L->base[idx-2-LJ_FR2-nvararg]); - aref = emitir(IRT(IR_AREF, IRT_PGC), vbase, tridx); - tr = lj_record_vload(J, aref, 0, t); - } - J->base[dst-2-LJ_FR2] = tr; - J->maxslot = dst-1-LJ_FR2; - J->bcskip = 2; /* Skip CALLM + select. */ - } else { - nyivarg: - setintV(&J->errinfo, BC_VARG); - lj_trace_err_info(J, LJ_TRERR_NYIBC); - } - } -} - -/* -- Record allocations -------------------------------------------------- */ - -static TRef rec_tnew(jit_State *J, uint32_t ah) -{ - uint32_t asize = ah & 0x7ff; - uint32_t hbits = ah >> 11; - TRef tr; - if (asize == 0x7ff) asize = 0x801; - tr = emitir(IRTG(IR_TNEW, IRT_TAB), asize, hbits); -#ifdef LUAJIT_ENABLE_TABLE_BUMP - J->rbchash[(tr & (RBCHASH_SLOTS-1))].ref = tref_ref(tr); - setmref(J->rbchash[(tr & (RBCHASH_SLOTS-1))].pc, J->pc); - setgcref(J->rbchash[(tr & (RBCHASH_SLOTS-1))].pt, obj2gco(J->pt)); -#endif - return tr; -} - -/* -- Concatenation ------------------------------------------------------- */ - -static TRef rec_cat(jit_State *J, BCReg baseslot, BCReg topslot) -{ - TRef *top = &J->base[topslot]; - TValue savetv[5+LJ_FR2]; - BCReg s; - RecordIndex ix; - lj_assertJ(baseslot < topslot, "bad CAT arg"); - for (s = baseslot; s <= topslot; s++) - (void)getslot(J, s); /* Ensure all arguments have a reference. */ - if (tref_isnumber_str(top[0]) && tref_isnumber_str(top[-1])) { - TRef tr, hdr, *trp, *xbase, *base = &J->base[baseslot]; - /* First convert numbers to strings. */ - for (trp = top; trp >= base; trp--) { - if (tref_isnumber(*trp)) - *trp = emitir(IRT(IR_TOSTR, IRT_STR), *trp, - tref_isnum(*trp) ? IRTOSTR_NUM : IRTOSTR_INT); - else if (!tref_isstr(*trp)) - break; - } - xbase = ++trp; - tr = hdr = emitir(IRT(IR_BUFHDR, IRT_PGC), - lj_ir_kptr(J, &J2G(J)->tmpbuf), IRBUFHDR_RESET); - do { - tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, *trp++); - } while (trp <= top); - tr = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); - J->maxslot = (BCReg)(xbase - J->base); - if (xbase == base) return tr; /* Return simple concatenation result. */ - /* Pass partial result. */ - topslot = J->maxslot--; - *xbase = tr; - top = xbase; - setstrV(J->L, &ix.keyv, &J2G(J)->strempty); /* Simulate string result. */ - } else { - J->maxslot = topslot-1; - copyTV(J->L, &ix.keyv, &J->L->base[topslot]); - } - copyTV(J->L, &ix.tabv, &J->L->base[topslot-1]); - ix.tab = top[-1]; - ix.key = top[0]; - memcpy(savetv, &J->L->base[topslot-1], sizeof(savetv)); /* Save slots. */ - rec_mm_arith(J, &ix, MM_concat); /* Call __concat metamethod. */ - memcpy(&J->L->base[topslot-1], savetv, sizeof(savetv)); /* Restore slots. */ - return 0; /* No result yet. */ -} - -/* -- Record bytecode ops ------------------------------------------------- */ - -/* Prepare for comparison. */ -static void rec_comp_prep(jit_State *J) -{ - /* Prevent merging with snapshot #0 (GC exit) since we fixup the PC. */ - if (J->cur.nsnap == 1 && J->cur.snap[0].ref == J->cur.nins) - emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0); - lj_snap_add(J); -} - -/* Fixup comparison. */ -static void rec_comp_fixup(jit_State *J, const BCIns *pc, int cond) -{ - BCIns jmpins = pc[1]; - const BCIns *npc = pc + 2 + (cond ? bc_j(jmpins) : 0); - SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; - /* Set PC to opposite target to avoid re-recording the comp. in side trace. */ -#if LJ_FR2 - SnapEntry *flink = &J->cur.snapmap[snap->mapofs + snap->nent]; - uint64_t pcbase; - memcpy(&pcbase, flink, sizeof(uint64_t)); - pcbase = (pcbase & 0xff) | (u64ptr(npc) << 8); - memcpy(flink, &pcbase, sizeof(uint64_t)); -#else - J->cur.snapmap[snap->mapofs + snap->nent] = SNAP_MKPC(npc); -#endif - J->needsnap = 1; - if (bc_a(jmpins) < J->maxslot) J->maxslot = bc_a(jmpins); - lj_snap_shrink(J); /* Shrink last snapshot if possible. */ -} - -/* Record the next bytecode instruction (_before_ it's executed). */ -void lj_record_ins(jit_State *J) -{ - cTValue *lbase; - RecordIndex ix; - const BCIns *pc; - BCIns ins; - BCOp op; - TRef ra, rb, rc; - - /* Perform post-processing action before recording the next instruction. */ - if (LJ_UNLIKELY(J->postproc != LJ_POST_NONE)) { - switch (J->postproc) { - case LJ_POST_FIXCOMP: /* Fixup comparison. */ - pc = (const BCIns *)(uintptr_t)J2G(J)->tmptv.u64; - rec_comp_fixup(J, pc, (!tvistruecond(&J2G(J)->tmptv2) ^ (bc_op(*pc)&1))); - /* fallthrough */ - case LJ_POST_FIXGUARD: /* Fixup and emit pending guard. */ - case LJ_POST_FIXGUARDSNAP: /* Fixup and emit pending guard and snapshot. */ - if (!tvistruecond(&J2G(J)->tmptv2)) { - J->fold.ins.o ^= 1; /* Flip guard to opposite. */ - if (J->postproc == LJ_POST_FIXGUARDSNAP) { - SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; - J->cur.snapmap[snap->mapofs+snap->nent-1]--; /* False -> true. */ - } - } - lj_opt_fold(J); /* Emit pending guard. */ - /* fallthrough */ - case LJ_POST_FIXBOOL: - if (!tvistruecond(&J2G(J)->tmptv2)) { - BCReg s; - TValue *tv = J->L->base; - for (s = 0; s < J->maxslot; s++) /* Fixup stack slot (if any). */ - if (J->base[s] == TREF_TRUE && tvisfalse(&tv[s])) { - J->base[s] = TREF_FALSE; - break; - } - } - break; - case LJ_POST_FIXCONST: - { - BCReg s; - TValue *tv = J->L->base; - for (s = 0; s < J->maxslot; s++) /* Constify stack slots (if any). */ - if (J->base[s] == TREF_NIL && !tvisnil(&tv[s])) - J->base[s] = lj_record_constify(J, &tv[s]); - } - break; - case LJ_POST_FFRETRY: /* Suppress recording of retried fast function. */ - if (bc_op(*J->pc) >= BC__MAX) - return; - break; - default: lj_assertJ(0, "bad post-processing mode"); break; - } - J->postproc = LJ_POST_NONE; - } - - /* Need snapshot before recording next bytecode (e.g. after a store). */ - if (J->needsnap) { - J->needsnap = 0; - if (J->pt) lj_snap_purge(J); - lj_snap_add(J); - J->mergesnap = 1; - } - - /* Skip some bytecodes. */ - if (LJ_UNLIKELY(J->bcskip > 0)) { - J->bcskip--; - return; - } - - /* Record only closed loops for root traces. */ - pc = J->pc; - if (J->framedepth == 0 && - (MSize)((char *)pc - (char *)J->bc_min) >= J->bc_extent) - lj_trace_err(J, LJ_TRERR_LLEAVE); - -#ifdef LUA_USE_ASSERT - rec_check_slots(J); - rec_check_ir(J); -#endif - -#if LJ_HASPROFILE - rec_profile_ins(J, pc); -#endif - - /* Keep a copy of the runtime values of var/num/str operands. */ -#define rav (&ix.valv) -#define rbv (&ix.tabv) -#define rcv (&ix.keyv) - - lbase = J->L->base; - ins = *pc; - op = bc_op(ins); - ra = bc_a(ins); - ix.val = 0; - switch (bcmode_a(op)) { - case BCMvar: - copyTV(J->L, rav, &lbase[ra]); ix.val = ra = getslot(J, ra); break; - default: break; /* Handled later. */ - } - rb = bc_b(ins); - rc = bc_c(ins); - switch (bcmode_b(op)) { - case BCMnone: rb = 0; rc = bc_d(ins); break; /* Upgrade rc to 'rd'. */ - case BCMvar: - copyTV(J->L, rbv, &lbase[rb]); ix.tab = rb = getslot(J, rb); break; - default: break; /* Handled later. */ - } - switch (bcmode_c(op)) { - case BCMvar: - copyTV(J->L, rcv, &lbase[rc]); ix.key = rc = getslot(J, rc); break; - case BCMpri: setpriV(rcv, ~rc); ix.key = rc = TREF_PRI(IRT_NIL+rc); break; - case BCMnum: { cTValue *tv = proto_knumtv(J->pt, rc); - copyTV(J->L, rcv, tv); ix.key = rc = tvisint(tv) ? lj_ir_kint(J, intV(tv)) : - tv->u32.hi == LJ_KEYINDEX ? (lj_ir_kint(J, 0) | TREF_KEYINDEX) : - lj_ir_knumint(J, numV(tv)); } break; - case BCMstr: { GCstr *s = gco2str(proto_kgc(J->pt, ~(ptrdiff_t)rc)); - setstrV(J->L, rcv, s); ix.key = rc = lj_ir_kstr(J, s); } break; - default: break; /* Handled later. */ - } - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: -#if LJ_HASFFI - if (tref_iscdata(ra) || tref_iscdata(rc)) { - rec_mm_comp_cdata(J, &ix, op, ((int)op & 2) ? MM_le : MM_lt); - break; - } -#endif - /* Emit nothing for two numeric or string consts. */ - if (!(tref_isk2(ra,rc) && tref_isnumber_str(ra) && tref_isnumber_str(rc))) { - IRType ta = tref_isinteger(ra) ? IRT_INT : tref_type(ra); - IRType tc = tref_isinteger(rc) ? IRT_INT : tref_type(rc); - int irop; - if (ta != tc) { - /* Widen mixed number/int comparisons to number/number comparison. */ - if (ta == IRT_INT && tc == IRT_NUM) { - ra = emitir(IRTN(IR_CONV), ra, IRCONV_NUM_INT); - ta = IRT_NUM; - } else if (ta == IRT_NUM && tc == IRT_INT) { - rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); - } else if (LJ_52) { - ta = IRT_NIL; /* Force metamethod for different types. */ - } else if (!((ta == IRT_FALSE || ta == IRT_TRUE) && - (tc == IRT_FALSE || tc == IRT_TRUE))) { - break; /* Interpreter will throw for two different types. */ - } - } - rec_comp_prep(J); - irop = (int)op - (int)BC_ISLT + (int)IR_LT; - if (ta == IRT_NUM) { - if ((irop & 1)) irop ^= 4; /* ISGE/ISGT are unordered. */ - if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop)) - irop ^= 5; - } else if (ta == IRT_INT) { - if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop)) - irop ^= 1; - } else if (ta == IRT_STR) { - if (!lj_ir_strcmp(strV(rav), strV(rcv), (IROp)irop)) irop ^= 1; - ra = lj_ir_call(J, IRCALL_lj_str_cmp, ra, rc); - rc = lj_ir_kint(J, 0); - ta = IRT_INT; - } else { - rec_mm_comp(J, &ix, (int)op); - break; - } - emitir(IRTG(irop, ta), ra, rc); - rec_comp_fixup(J, J->pc, ((int)op ^ irop) & 1); - } - break; - - case BC_ISEQV: case BC_ISNEV: - case BC_ISEQS: case BC_ISNES: - case BC_ISEQN: case BC_ISNEN: - case BC_ISEQP: case BC_ISNEP: -#if LJ_HASFFI - if (tref_iscdata(ra) || tref_iscdata(rc)) { - rec_mm_comp_cdata(J, &ix, op, MM_eq); - break; - } -#endif - /* Emit nothing for two non-table, non-udata consts. */ - if (!(tref_isk2(ra, rc) && !(tref_istab(ra) || tref_isudata(ra)))) { - int diff; - rec_comp_prep(J); - diff = lj_record_objcmp(J, ra, rc, rav, rcv); - if (diff == 2 || !(tref_istab(ra) || tref_isudata(ra))) - rec_comp_fixup(J, J->pc, ((int)op & 1) == !diff); - else if (diff == 1) /* Only check __eq if different, but same type. */ - rec_mm_equal(J, &ix, (int)op); - } - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: - if ((op & 1) == tref_istruecond(rc)) - rc = 0; /* Don't store if condition is not true. */ - /* fallthrough */ - case BC_IST: case BC_ISF: /* Type specialization suffices. */ - if (bc_a(pc[1]) < J->maxslot) - J->maxslot = bc_a(pc[1]); /* Shrink used slots. */ - break; - - case BC_ISTYPE: case BC_ISNUM: - /* These coercions need to correspond with lj_meta_istype(). */ - if (LJ_DUALNUM && rc == ~LJ_TNUMX+1) - ra = lj_opt_narrow_toint(J, ra); - else if (rc == ~LJ_TNUMX+2) - ra = lj_ir_tonum(J, ra); - else if (rc == ~LJ_TSTR+1) - ra = lj_ir_tostr(J, ra); - /* else: type specialization suffices. */ - J->base[bc_a(ins)] = ra; - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_NOT: - /* Type specialization already forces const result. */ - rc = tref_istruecond(rc) ? TREF_FALSE : TREF_TRUE; - break; - - case BC_LEN: - if (tref_isstr(rc)) - rc = emitir(IRTI(IR_FLOAD), rc, IRFL_STR_LEN); - else if (!LJ_52 && tref_istab(rc)) - rc = emitir(IRTI(IR_ALEN), rc, TREF_NIL); - else - rc = rec_mm_len(J, rc, rcv); - break; - - /* -- Arithmetic ops ---------------------------------------------------- */ - - case BC_UNM: - if (tref_isnumber_str(rc)) { - rc = lj_opt_narrow_unm(J, rc, rcv); - } else { - ix.tab = rc; - copyTV(J->L, &ix.tabv, rcv); - rc = rec_mm_arith(J, &ix, MM_unm); - } - break; - - case BC_ADDNV: case BC_SUBNV: case BC_MULNV: case BC_DIVNV: case BC_MODNV: - /* Swap rb/rc and rbv/rcv. rav is temp. */ - ix.tab = rc; ix.key = rc = rb; rb = ix.tab; - copyTV(J->L, rav, rbv); - copyTV(J->L, rbv, rcv); - copyTV(J->L, rcv, rav); - if (op == BC_MODNV) - goto recmod; - /* fallthrough */ - case BC_ADDVN: case BC_SUBVN: case BC_MULVN: case BC_DIVVN: - case BC_ADDVV: case BC_SUBVV: case BC_MULVV: case BC_DIVVV: { - MMS mm = bcmode_mm(op); - if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) - rc = lj_opt_narrow_arith(J, rb, rc, rbv, rcv, - (int)mm - (int)MM_add + (int)IR_ADD); - else - rc = rec_mm_arith(J, &ix, mm); - break; - } - - case BC_MODVN: case BC_MODVV: - recmod: - if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) - rc = lj_opt_narrow_mod(J, rb, rc, rbv, rcv); - else - rc = rec_mm_arith(J, &ix, MM_mod); - break; - - case BC_POW: - if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) - rc = lj_opt_narrow_arith(J, rb, rc, rbv, rcv, IR_POW); - else - rc = rec_mm_arith(J, &ix, MM_pow); - break; - - /* -- Miscellaneous ops ------------------------------------------------- */ - - case BC_CAT: - rc = rec_cat(J, rb, rc); - break; - - /* -- Constant and move ops --------------------------------------------- */ - - case BC_MOV: - /* Clear gap of method call to avoid resurrecting previous refs. */ - if (ra > J->maxslot) { -#if LJ_FR2 - memset(J->base + J->maxslot, 0, (ra - J->maxslot) * sizeof(TRef)); -#else - J->base[ra-1] = 0; -#endif - } - break; - case BC_KSTR: case BC_KNUM: case BC_KPRI: - break; - case BC_KSHORT: - rc = lj_ir_kint(J, (int32_t)(int16_t)rc); - break; - case BC_KNIL: - if (LJ_FR2 && ra > J->maxslot) - J->base[ra-1] = 0; - while (ra <= rc) - J->base[ra++] = TREF_NIL; - if (rc >= J->maxslot) J->maxslot = rc+1; - break; -#if LJ_HASFFI - case BC_KCDATA: - rc = lj_ir_kgc(J, proto_kgc(J->pt, ~(ptrdiff_t)rc), IRT_CDATA); - break; -#endif - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - rc = rec_upvalue(J, rc, 0); - break; - case BC_USETV: case BC_USETS: case BC_USETN: case BC_USETP: - rec_upvalue(J, ra, rc); - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_GGET: case BC_GSET: - settabV(J->L, &ix.tabv, tabref(J->fn->l.env)); - ix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), getcurrf(J), IRFL_FUNC_ENV); - ix.idxchain = LJ_MAX_IDXCHAIN; - rc = lj_record_idx(J, &ix); - break; - - case BC_TGETB: case BC_TSETB: - setintV(&ix.keyv, (int32_t)rc); - ix.key = lj_ir_kint(J, (int32_t)rc); - /* fallthrough */ - case BC_TGETV: case BC_TGETS: case BC_TSETV: case BC_TSETS: - ix.idxchain = LJ_MAX_IDXCHAIN; - rc = lj_record_idx(J, &ix); - break; - case BC_TGETR: case BC_TSETR: - ix.idxchain = 0; - rc = lj_record_idx(J, &ix); - break; - - case BC_TSETM: - rec_tsetm(J, ra, (BCReg)(J->L->top - J->L->base), (int32_t)rcv->u32.lo); - break; - - case BC_TNEW: - rc = rec_tnew(J, rc); - break; - case BC_TDUP: - rc = emitir(IRTG(IR_TDUP, IRT_TAB), - lj_ir_ktab(J, gco2tab(proto_kgc(J->pt, ~(ptrdiff_t)rc))), 0); -#ifdef LUAJIT_ENABLE_TABLE_BUMP - J->rbchash[(rc & (RBCHASH_SLOTS-1))].ref = tref_ref(rc); - setmref(J->rbchash[(rc & (RBCHASH_SLOTS-1))].pc, pc); - setgcref(J->rbchash[(rc & (RBCHASH_SLOTS-1))].pt, obj2gco(J->pt)); -#endif - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_ITERC: - J->base[ra] = getslot(J, ra-3); - J->base[ra+1+LJ_FR2] = getslot(J, ra-2); - J->base[ra+2+LJ_FR2] = getslot(J, ra-1); - { /* Do the actual copy now because lj_record_call needs the values. */ - TValue *b = &J->L->base[ra]; - copyTV(J->L, b, b-3); - copyTV(J->L, b+1+LJ_FR2, b-2); - copyTV(J->L, b+2+LJ_FR2, b-1); - } - lj_record_call(J, ra, (ptrdiff_t)rc-1); - break; - - /* L->top is set to L->base+ra+rc+NARGS-1+1. See lj_dispatch_ins(). */ - case BC_CALLM: - rc = (BCReg)(J->L->top - J->L->base) - ra - LJ_FR2; - /* fallthrough */ - case BC_CALL: - lj_record_call(J, ra, (ptrdiff_t)rc-1); - break; - - case BC_CALLMT: - rc = (BCReg)(J->L->top - J->L->base) - ra - LJ_FR2; - /* fallthrough */ - case BC_CALLT: - lj_record_tailcall(J, ra, (ptrdiff_t)rc-1); - break; - - case BC_VARG: - rec_varg(J, ra, (ptrdiff_t)rb-1); - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - /* L->top is set to L->base+ra+rc+NRESULTS-1, see lj_dispatch_ins(). */ - rc = (BCReg)(J->L->top - J->L->base) - ra + 1; - /* fallthrough */ - case BC_RET: case BC_RET0: case BC_RET1: -#if LJ_HASPROFILE - rec_profile_ret(J); -#endif - lj_record_ret(J, ra, (ptrdiff_t)rc-1); - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORI: - if (rec_for(J, pc, 0) != LOOPEV_LEAVE) - J->loopref = J->cur.nins; - break; - case BC_JFORI: - lj_assertJ(bc_op(pc[(ptrdiff_t)rc-BCBIAS_J]) == BC_JFORL, - "JFORI does not point to JFORL"); - if (rec_for(J, pc, 0) != LOOPEV_LEAVE) /* Link to existing loop. */ - lj_record_stop(J, LJ_TRLINK_ROOT, bc_d(pc[(ptrdiff_t)rc-BCBIAS_J])); - /* Continue tracing if the loop is not entered. */ - break; - - case BC_FORL: - rec_loop_interp(J, pc, rec_for(J, pc+((ptrdiff_t)rc-BCBIAS_J), 1)); - break; - case BC_ITERL: - rec_loop_interp(J, pc, rec_iterl(J, *pc)); - break; - case BC_ITERN: - rec_loop_interp(J, pc, rec_itern(J, ra, rb)); - break; - case BC_LOOP: - rec_loop_interp(J, pc, rec_loop(J, ra, 1)); - break; - - case BC_JFORL: - rec_loop_jit(J, rc, rec_for(J, pc+bc_j(traceref(J, rc)->startins), 1)); - break; - case BC_JITERL: - rec_loop_jit(J, rc, rec_iterl(J, traceref(J, rc)->startins)); - break; - case BC_JLOOP: - rec_loop_jit(J, rc, rec_loop(J, ra, - !bc_isret(bc_op(traceref(J, rc)->startins)) && - bc_op(traceref(J, rc)->startins) != BC_ITERN)); - break; - - case BC_IFORL: - case BC_IITERL: - case BC_ILOOP: - case BC_IFUNCF: - case BC_IFUNCV: - lj_trace_err(J, LJ_TRERR_BLACKL); - break; - - case BC_JMP: - if (ra < J->maxslot) - J->maxslot = ra; /* Shrink used slots. */ - break; - - case BC_ISNEXT: - rec_isnext(J, ra); - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - rec_func_lua(J); - break; - case BC_JFUNCF: - rec_func_jit(J, rc); - break; - - case BC_FUNCV: - rec_func_vararg(J); - rec_func_lua(J); - break; - case BC_JFUNCV: - /* Cannot happen. No hotcall counting for varag funcs. */ - lj_assertJ(0, "unsupported vararg hotcall"); - break; - - case BC_FUNCC: - case BC_FUNCCW: - lj_ffrecord_func(J); - break; - - default: - if (op >= BC__MAX) { - lj_ffrecord_func(J); - break; - } - /* fallthrough */ - case BC_UCLO: - case BC_FNEW: - setintV(&J->errinfo, (int32_t)op); - lj_trace_err_info(J, LJ_TRERR_NYIBC); - break; - } - - /* rc == 0 if we have no result yet, e.g. pending __index metamethod call. */ - if (bcmode_a(op) == BCMdst && rc) { - J->base[ra] = rc; - if (ra >= J->maxslot) { -#if LJ_FR2 - if (ra > J->maxslot) J->base[ra-1] = 0; -#endif - J->maxslot = ra+1; - } - } - -#undef rav -#undef rbv -#undef rcv - - /* Limit the number of recorded IR instructions and constants. */ - if (J->cur.nins > REF_FIRST+(IRRef)J->param[JIT_P_maxrecord] || - J->cur.nk < REF_BIAS-(IRRef)J->param[JIT_P_maxirconst]) - lj_trace_err(J, LJ_TRERR_TRACEOV); -} - -/* -- Recording setup ----------------------------------------------------- */ - -/* Setup recording for a root trace started by a hot loop. */ -static const BCIns *rec_setup_root(jit_State *J) -{ - /* Determine the next PC and the bytecode range for the loop. */ - const BCIns *pcj, *pc = J->pc; - BCIns ins = *pc; - BCReg ra = bc_a(ins); - switch (bc_op(ins)) { - case BC_FORL: - J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); - pc += 1+bc_j(ins); - J->bc_min = pc; - break; - case BC_ITERL: - if (bc_op(pc[-1]) == BC_JLOOP) - lj_trace_err(J, LJ_TRERR_LINNER); - lj_assertJ(bc_op(pc[-1]) == BC_ITERC, "no ITERC before ITERL"); - J->maxslot = ra + bc_b(pc[-1]) - 1; - J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); - pc += 1+bc_j(ins); - lj_assertJ(bc_op(pc[-1]) == BC_JMP, "ITERL does not point to JMP+1"); - J->bc_min = pc; - break; - case BC_ITERN: - lj_assertJ(bc_op(pc[1]) == BC_ITERL, "no ITERL after ITERN"); - J->maxslot = ra; - J->bc_extent = (MSize)(-bc_j(pc[1]))*sizeof(BCIns); - J->bc_min = pc+2 + bc_j(pc[1]); - J->state = LJ_TRACE_RECORD_1ST; /* Record the first ITERN, too. */ - break; - case BC_LOOP: - /* Only check BC range for real loops, but not for "repeat until true". */ - pcj = pc + bc_j(ins); - ins = *pcj; - if (bc_op(ins) == BC_JMP && bc_j(ins) < 0) { - J->bc_min = pcj+1 + bc_j(ins); - J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); - } - J->maxslot = ra; - pc++; - break; - case BC_RET: - case BC_RET0: - case BC_RET1: - /* No bytecode range check for down-recursive root traces. */ - J->maxslot = ra + bc_d(ins) - 1; - break; - case BC_FUNCF: - /* No bytecode range check for root traces started by a hot call. */ - J->maxslot = J->pt->numparams; - pc++; - break; - case BC_CALLM: - case BC_CALL: - case BC_ITERC: - /* No bytecode range check for stitched traces. */ - pc++; - break; - default: - lj_assertJ(0, "bad root trace start bytecode %d", bc_op(ins)); - break; - } - return pc; -} - -/* Setup for recording a new trace. */ -void lj_record_setup(jit_State *J) -{ - uint32_t i; - - /* Initialize state related to current trace. */ - memset(J->slot, 0, sizeof(J->slot)); - memset(J->chain, 0, sizeof(J->chain)); -#ifdef LUAJIT_ENABLE_TABLE_BUMP - memset(J->rbchash, 0, sizeof(J->rbchash)); -#endif - memset(J->bpropcache, 0, sizeof(J->bpropcache)); - J->scev.idx = REF_NIL; - setmref(J->scev.pc, NULL); - - J->baseslot = 1+LJ_FR2; /* Invoking function is at base[-1-LJ_FR2]. */ - J->base = J->slot + J->baseslot; - J->maxslot = 0; - J->framedepth = 0; - J->retdepth = 0; - - J->instunroll = J->param[JIT_P_instunroll]; - J->loopunroll = J->param[JIT_P_loopunroll]; - J->tailcalled = 0; - J->loopref = 0; - - J->bc_min = NULL; /* Means no limit. */ - J->bc_extent = ~(MSize)0; - - /* Emit instructions for fixed references. Also triggers initial IR alloc. */ - emitir_raw(IRT(IR_BASE, IRT_PGC), J->parent, J->exitno); - for (i = 0; i <= 2; i++) { - IRIns *ir = IR(REF_NIL-i); - ir->i = 0; - ir->t.irt = (uint8_t)(IRT_NIL+i); - ir->o = IR_KPRI; - ir->prev = 0; - } - J->cur.nk = REF_TRUE; - - J->startpc = J->pc; - setmref(J->cur.startpc, J->pc); - if (J->parent) { /* Side trace. */ - GCtrace *T = traceref(J, J->parent); - TraceNo root = T->root ? T->root : J->parent; - J->cur.root = (uint16_t)root; - J->cur.startins = BCINS_AD(BC_JMP, 0, 0); - /* Check whether we could at least potentially form an extra loop. */ - if (J->exitno == 0 && T->snap[0].nent == 0) { - /* We can narrow a FORL for some side traces, too. */ - if (J->pc > proto_bc(J->pt) && bc_op(J->pc[-1]) == BC_JFORI && - bc_d(J->pc[bc_j(J->pc[-1])-1]) == root) { - lj_snap_add(J); - rec_for_loop(J, J->pc-1, &J->scev, 1); - goto sidecheck; - } - } else { - J->startpc = NULL; /* Prevent forming an extra loop. */ - } - lj_snap_replay(J, T); - sidecheck: - if ((traceref(J, J->cur.root)->nchild >= J->param[JIT_P_maxside] || - T->snap[J->exitno].count >= J->param[JIT_P_hotexit] + - J->param[JIT_P_tryside])) { - if (bc_op(*J->pc) == BC_JLOOP) { - BCIns startins = traceref(J, bc_d(*J->pc))->startins; - if (bc_op(startins) == BC_ITERN) - rec_itern(J, bc_a(startins), bc_b(startins)); - } - lj_record_stop(J, LJ_TRLINK_INTERP, 0); - } - } else { /* Root trace. */ - J->cur.root = 0; - J->cur.startins = *J->pc; - J->pc = rec_setup_root(J); - /* Note: the loop instruction itself is recorded at the end and not - ** at the start! So snapshot #0 needs to point to the *next* instruction. - ** The one exception is BC_ITERN, which sets LJ_TRACE_RECORD_1ST. - */ - lj_snap_add(J); - if (bc_op(J->cur.startins) == BC_FORL) - rec_for_loop(J, J->pc-1, &J->scev, 1); - else if (bc_op(J->cur.startins) == BC_ITERC) - J->startpc = NULL; - if (1 + J->pt->framesize >= LJ_MAX_JSLOTS) - lj_trace_err(J, LJ_TRERR_STACKOV); - } -#if LJ_HASPROFILE - J->prev_pt = NULL; - J->prev_line = -1; -#endif -#ifdef LUAJIT_ENABLE_CHECKHOOK - /* Regularly check for instruction/line hooks from compiled code and - ** exit to the interpreter if the hooks are set. - ** - ** This is a compile-time option and disabled by default, since the - ** hook checks may be quite expensive in tight loops. - ** - ** Note this is only useful if hooks are *not* set most of the time. - ** Use this only if you want to *asynchronously* interrupt the execution. - ** - ** You can set the instruction hook via lua_sethook() with a count of 1 - ** from a signal handler or another native thread. Please have a look - ** at the first few functions in luajit.c for an example (Ctrl-C handler). - */ - { - TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), - lj_ir_kptr(J, &J2G(J)->hookmask), IRXLOAD_VOLATILE); - tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (LUA_MASKLINE|LUA_MASKCOUNT))); - emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); - } -#endif -} - -#undef IR -#undef emitir_raw -#undef emitir - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.h deleted file mode 100644 index ab2f4c8..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_record.h +++ /dev/null @@ -1,47 +0,0 @@ -/* -** Trace recorder (bytecode -> SSA IR). -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_RECORD_H -#define _LJ_RECORD_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -/* Context for recording an indexed load/store. */ -typedef struct RecordIndex { - TValue tabv; /* Runtime value of table (or indexed object). */ - TValue keyv; /* Runtime value of key. */ - TValue valv; /* Runtime value of stored value. */ - TValue mobjv; /* Runtime value of metamethod object. */ - GCtab *mtv; /* Runtime value of metatable object. */ - cTValue *oldv; /* Runtime value of previously stored value. */ - TRef tab; /* Table (or indexed object) reference. */ - TRef key; /* Key reference. */ - TRef val; /* Value reference for a store or 0 for a load. */ - TRef mt; /* Metatable reference. */ - TRef mobj; /* Metamethod object reference. */ - int idxchain; /* Index indirections left or 0 for raw lookup. */ -} RecordIndex; - -LJ_FUNC int lj_record_objcmp(jit_State *J, TRef a, TRef b, - cTValue *av, cTValue *bv); -LJ_FUNC void lj_record_stop(jit_State *J, TraceLink linktype, TraceNo lnk); -LJ_FUNC TRef lj_record_constify(jit_State *J, cTValue *o); -LJ_FUNC TRef lj_record_vload(jit_State *J, TRef ref, MSize idx, IRType t); - -LJ_FUNC void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs); -LJ_FUNC void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs); -LJ_FUNC void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults); - -LJ_FUNC int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm); -LJ_FUNC TRef lj_record_idx(jit_State *J, RecordIndex *ix); -LJ_FUNC int lj_record_next(jit_State *J, RecordIndex *ix); - -LJ_FUNC void lj_record_ins(jit_State *J); -LJ_FUNC void lj_record_setup(jit_State *J); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.c deleted file mode 100644 index f7e5182..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.c +++ /dev/null @@ -1,539 +0,0 @@ -/* -** Object de/serialization. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_serialize_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASBUFFER -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_udata.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#endif -#if LJ_HASJIT -#include "lj_ir.h" -#endif -#include "lj_serialize.h" - -/* Tags for internal serialization format. */ -enum { - SER_TAG_NIL, /* 0x00 */ - SER_TAG_FALSE, - SER_TAG_TRUE, - SER_TAG_NULL, - SER_TAG_LIGHTUD32, - SER_TAG_LIGHTUD64, - SER_TAG_INT, - SER_TAG_NUM, - SER_TAG_TAB, /* 0x08 */ - SER_TAG_DICT_MT = SER_TAG_TAB+6, - SER_TAG_DICT_STR, - SER_TAG_INT64, /* 0x10 */ - SER_TAG_UINT64, - SER_TAG_COMPLEX, - SER_TAG_0x13, - SER_TAG_0x14, - SER_TAG_0x15, - SER_TAG_0x16, - SER_TAG_0x17, - SER_TAG_0x18, /* 0x18 */ - SER_TAG_0x19, - SER_TAG_0x1a, - SER_TAG_0x1b, - SER_TAG_0x1c, - SER_TAG_0x1d, - SER_TAG_0x1e, - SER_TAG_0x1f, - SER_TAG_STR, /* 0x20 + str->len */ -}; -LJ_STATIC_ASSERT((SER_TAG_TAB & 7) == 0); - -/* -- Helper functions ---------------------------------------------------- */ - -static LJ_AINLINE char *serialize_more(char *w, SBufExt *sbx, MSize sz) -{ - if (LJ_UNLIKELY(sz > (MSize)(sbx->e - w))) { - sbx->w = w; - w = lj_buf_more2((SBuf *)sbx, sz); - } - return w; -} - -/* Write U124 to buffer. */ -static LJ_NOINLINE char *serialize_wu124_(char *w, uint32_t v) -{ - if (v < 0x1fe0) { - v -= 0xe0; - *w++ = (char)(0xe0 | (v >> 8)); *w++ = (char)v; - } else { - *w++ = (char)0xff; -#if LJ_BE - v = lj_bswap(v); -#endif - memcpy(w, &v, 4); w += 4; - } - return w; -} - -static LJ_AINLINE char *serialize_wu124(char *w, uint32_t v) -{ - if (LJ_LIKELY(v < 0xe0)) { - *w++ = (char)v; - return w; - } else { - return serialize_wu124_(w, v); - } -} - -static LJ_NOINLINE char *serialize_ru124_(char *r, char *w, uint32_t *pv) -{ - uint32_t v = *pv; - if (v != 0xff) { - if (r >= w) return NULL; - v = ((v & 0x1f) << 8) + *(uint8_t *)r + 0xe0; r++; - } else { - if (r + 4 > w) return NULL; - v = lj_getu32(r); r += 4; -#if LJ_BE - v = lj_bswap(v); -#endif - } - *pv = v; - return r; -} - -static LJ_AINLINE char *serialize_ru124(char *r, char *w, uint32_t *pv) -{ - if (LJ_LIKELY(r < w)) { - uint32_t v = *(uint8_t *)r; r++; - *pv = v; - if (LJ_UNLIKELY(v >= 0xe0)) { - r = serialize_ru124_(r, w, pv); - } - return r; - } - return NULL; -} - -/* Prepare string dictionary for use (once). */ -void LJ_FASTCALL lj_serialize_dict_prep_str(lua_State *L, GCtab *dict) -{ - if (!dict->hmask) { /* No hash part means not prepared, yet. */ - MSize i, len = lj_tab_len(dict); - if (!len) return; - lj_tab_resize(L, dict, dict->asize, hsize2hbits(len)); - for (i = 1; i <= len && i < dict->asize; i++) { - cTValue *o = arrayslot(dict, i); - if (tvisstr(o)) { - if (!lj_tab_getstr(dict, strV(o))) { /* Ignore dups. */ - lj_tab_newkey(L, dict, o)->u64 = (uint64_t)(i-1); - } - } else if (!tvisfalse(o)) { - lj_err_caller(L, LJ_ERR_BUFFER_BADOPT); - } - } - } -} - -/* Prepare metatable dictionary for use (once). */ -void LJ_FASTCALL lj_serialize_dict_prep_mt(lua_State *L, GCtab *dict) -{ - if (!dict->hmask) { /* No hash part means not prepared, yet. */ - MSize i, len = lj_tab_len(dict); - if (!len) return; - lj_tab_resize(L, dict, dict->asize, hsize2hbits(len)); - for (i = 1; i <= len && i < dict->asize; i++) { - cTValue *o = arrayslot(dict, i); - if (tvistab(o)) { - if (tvisnil(lj_tab_get(L, dict, o))) { /* Ignore dups. */ - lj_tab_newkey(L, dict, o)->u64 = (uint64_t)(i-1); - } - } else if (!tvisfalse(o)) { - lj_err_caller(L, LJ_ERR_BUFFER_BADOPT); - } - } - } -} - -/* -- Internal serializer ------------------------------------------------- */ - -/* Put serialized object into buffer. */ -static char *serialize_put(char *w, SBufExt *sbx, cTValue *o) -{ - if (LJ_LIKELY(tvisstr(o))) { - const GCstr *str = strV(o); - MSize len = str->len; - w = serialize_more(w, sbx, 5+len); - w = serialize_wu124(w, SER_TAG_STR + len); - w = lj_buf_wmem(w, strdata(str), len); - } else if (tvisint(o)) { - uint32_t x = LJ_BE ? lj_bswap((uint32_t)intV(o)) : (uint32_t)intV(o); - w = serialize_more(w, sbx, 1+4); - *w++ = SER_TAG_INT; memcpy(w, &x, 4); w += 4; - } else if (tvisnum(o)) { - uint64_t x = LJ_BE ? lj_bswap64(o->u64) : o->u64; - w = serialize_more(w, sbx, 1+sizeof(lua_Number)); - *w++ = SER_TAG_NUM; memcpy(w, &x, 8); w += 8; - } else if (tvispri(o)) { - w = serialize_more(w, sbx, 1); - *w++ = (char)(SER_TAG_NIL + ~itype(o)); - } else if (tvistab(o)) { - const GCtab *t = tabV(o); - uint32_t narray = 0, nhash = 0, one = 2; - if (sbx->depth <= 0) lj_err_caller(sbufL(sbx), LJ_ERR_BUFFER_DEPTH); - sbx->depth--; - if (t->asize > 0) { /* Determine max. length of array part. */ - ptrdiff_t i; - TValue *array = tvref(t->array); - for (i = (ptrdiff_t)t->asize-1; i >= 0; i--) - if (!tvisnil(&array[i])) - break; - narray = (uint32_t)(i+1); - if (narray && tvisnil(&array[0])) one = 4; - } - if (t->hmask > 0) { /* Count number of used hash slots. */ - uint32_t i, hmask = t->hmask; - Node *node = noderef(t->node); - for (i = 0; i <= hmask; i++) - nhash += !tvisnil(&node[i].val); - } - /* Write metatable index. */ - if (LJ_UNLIKELY(tabref(sbx->dict_mt)) && tabref(t->metatable)) { - TValue mto; - Node *n; - settabV(sbufL(sbx), &mto, tabref(t->metatable)); - n = hashgcref(tabref(sbx->dict_mt), mto.gcr); - do { - if (n->key.u64 == mto.u64) { - uint32_t idx = n->val.u32.lo; - w = serialize_more(w, sbx, 1+5); - *w++ = SER_TAG_DICT_MT; - w = serialize_wu124(w, idx); - break; - } - } while ((n = nextnode(n))); - } - /* Write number of array slots and hash slots. */ - w = serialize_more(w, sbx, 1+2*5); - *w++ = (char)(SER_TAG_TAB + (nhash ? 1 : 0) + (narray ? one : 0)); - if (narray) w = serialize_wu124(w, narray); - if (nhash) w = serialize_wu124(w, nhash); - if (narray) { /* Write array entries. */ - cTValue *oa = tvref(t->array) + (one >> 2); - cTValue *oe = tvref(t->array) + narray; - while (oa < oe) w = serialize_put(w, sbx, oa++); - } - if (nhash) { /* Write hash entries. */ - const Node *node = noderef(t->node) + t->hmask; - GCtab *dict_str = tabref(sbx->dict_str); - if (LJ_UNLIKELY(dict_str)) { - for (;; node--) - if (!tvisnil(&node->val)) { - if (LJ_LIKELY(tvisstr(&node->key))) { - /* Inlined lj_tab_getstr is 30% faster. */ - const GCstr *str = strV(&node->key); - Node *n = hashstr(dict_str, str); - do { - if (tvisstr(&n->key) && strV(&n->key) == str) { - uint32_t idx = n->val.u32.lo; - w = serialize_more(w, sbx, 1+5); - *w++ = SER_TAG_DICT_STR; - w = serialize_wu124(w, idx); - break; - } - n = nextnode(n); - if (!n) { - MSize len = str->len; - w = serialize_more(w, sbx, 5+len); - w = serialize_wu124(w, SER_TAG_STR + len); - w = lj_buf_wmem(w, strdata(str), len); - break; - } - } while (1); - } else { - w = serialize_put(w, sbx, &node->key); - } - w = serialize_put(w, sbx, &node->val); - if (--nhash == 0) break; - } - } else { - for (;; node--) - if (!tvisnil(&node->val)) { - w = serialize_put(w, sbx, &node->key); - w = serialize_put(w, sbx, &node->val); - if (--nhash == 0) break; - } - } - } - sbx->depth++; -#if LJ_HASFFI - } else if (tviscdata(o)) { - CTState *cts = ctype_cts(sbufL(sbx)); - CType *s = ctype_raw(cts, cdataV(o)->ctypeid); - uint8_t *sp = cdataptr(cdataV(o)); - if (ctype_isinteger(s->info) && s->size == 8) { - w = serialize_more(w, sbx, 1+8); - *w++ = (s->info & CTF_UNSIGNED) ? SER_TAG_UINT64 : SER_TAG_INT64; -#if LJ_BE - { uint64_t u = lj_bswap64(*(uint64_t *)sp); memcpy(w, &u, 8); } -#else - memcpy(w, sp, 8); -#endif - w += 8; - } else if (ctype_iscomplex(s->info) && s->size == 16) { - w = serialize_more(w, sbx, 1+16); - *w++ = SER_TAG_COMPLEX; -#if LJ_BE - { /* Only swap the doubles. The re/im order stays the same. */ - uint64_t u = lj_bswap64(((uint64_t *)sp)[0]); memcpy(w, &u, 8); - u = lj_bswap64(((uint64_t *)sp)[1]); memcpy(w+8, &u, 8); - } -#else - memcpy(w, sp, 16); -#endif - w += 16; - } else { - goto badenc; /* NYI other cdata */ - } -#endif - } else if (tvislightud(o)) { - uintptr_t ud = (uintptr_t)lightudV(G(sbufL(sbx)), o); - w = serialize_more(w, sbx, 1+sizeof(ud)); - if (ud == 0) { - *w++ = SER_TAG_NULL; - } else if (LJ_32 || checku32(ud)) { -#if LJ_BE && LJ_64 - ud = lj_bswap64(ud); -#elif LJ_BE - ud = lj_bswap(ud); -#endif - *w++ = SER_TAG_LIGHTUD32; memcpy(w, &ud, 4); w += 4; -#if LJ_64 - } else { -#if LJ_BE - ud = lj_bswap64(ud); -#endif - *w++ = SER_TAG_LIGHTUD64; memcpy(w, &ud, 8); w += 8; -#endif - } - } else { - /* NYI userdata */ -#if LJ_HASFFI - badenc: -#endif - lj_err_callerv(sbufL(sbx), LJ_ERR_BUFFER_BADENC, lj_typename(o)); - } - return w; -} - -/* Get serialized object from buffer. */ -static char *serialize_get(char *r, SBufExt *sbx, TValue *o) -{ - char *w = sbx->w; - uint32_t tp; - r = serialize_ru124(r, w, &tp); if (LJ_UNLIKELY(!r)) goto eob; - if (LJ_LIKELY(tp >= SER_TAG_STR)) { - uint32_t len = tp - SER_TAG_STR; - if (LJ_UNLIKELY(len > (uint32_t)(w - r))) goto eob; - setstrV(sbufL(sbx), o, lj_str_new(sbufL(sbx), r, len)); - r += len; - } else if (tp == SER_TAG_INT) { - if (LJ_UNLIKELY(r + 4 > w)) goto eob; - setintV(o, (int32_t)(LJ_BE ? lj_bswap(lj_getu32(r)) : lj_getu32(r))); - r += 4; - } else if (tp == SER_TAG_NUM) { - if (LJ_UNLIKELY(r + 8 > w)) goto eob; - memcpy(o, r, 8); r += 8; -#if LJ_BE - o->u64 = lj_bswap64(o->u64); -#endif - if (!tvisnum(o)) setnanV(o); /* Fix non-canonical NaNs. */ - } else if (tp <= SER_TAG_TRUE) { - setpriV(o, ~tp); - } else if (tp == SER_TAG_DICT_STR) { - GCtab *dict_str; - uint32_t idx; - r = serialize_ru124(r, w, &idx); if (LJ_UNLIKELY(!r)) goto eob; - idx++; - dict_str = tabref(sbx->dict_str); - if (dict_str && idx < dict_str->asize && tvisstr(arrayslot(dict_str, idx))) - copyTV(sbufL(sbx), o, arrayslot(dict_str, idx)); - else - lj_err_callerv(sbufL(sbx), LJ_ERR_BUFFER_BADDICTX, idx); - } else if (tp >= SER_TAG_TAB && tp <= SER_TAG_DICT_MT) { - uint32_t narray = 0, nhash = 0; - GCtab *t, *mt = NULL; - if (sbx->depth <= 0) lj_err_caller(sbufL(sbx), LJ_ERR_BUFFER_DEPTH); - sbx->depth--; - if (tp == SER_TAG_DICT_MT) { - GCtab *dict_mt; - uint32_t idx; - r = serialize_ru124(r, w, &idx); if (LJ_UNLIKELY(!r)) goto eob; - idx++; - dict_mt = tabref(sbx->dict_mt); - if (dict_mt && idx < dict_mt->asize && tvistab(arrayslot(dict_mt, idx))) - mt = tabV(arrayslot(dict_mt, idx)); - else - lj_err_callerv(sbufL(sbx), LJ_ERR_BUFFER_BADDICTX, idx); - r = serialize_ru124(r, w, &tp); if (LJ_UNLIKELY(!r)) goto eob; - if (!(tp >= SER_TAG_TAB && tp < SER_TAG_DICT_MT)) goto badtag; - } - if (tp >= SER_TAG_TAB+2) { - r = serialize_ru124(r, w, &narray); if (LJ_UNLIKELY(!r)) goto eob; - } - if ((tp & 1)) { - r = serialize_ru124(r, w, &nhash); if (LJ_UNLIKELY(!r)) goto eob; - } - t = lj_tab_new(sbufL(sbx), narray, hsize2hbits(nhash)); - /* NOBARRIER: The table is new (marked white). */ - setgcref(t->metatable, obj2gco(mt)); - settabV(sbufL(sbx), o, t); - if (narray) { - TValue *oa = tvref(t->array) + (tp >= SER_TAG_TAB+4); - TValue *oe = tvref(t->array) + narray; - while (oa < oe) r = serialize_get(r, sbx, oa++); - } - if (nhash) { - do { - TValue k, *v; - r = serialize_get(r, sbx, &k); - v = lj_tab_set(sbufL(sbx), t, &k); - if (LJ_UNLIKELY(!tvisnil(v))) - lj_err_caller(sbufL(sbx), LJ_ERR_BUFFER_DUPKEY); - r = serialize_get(r, sbx, v); - } while (--nhash); - } - sbx->depth++; -#if LJ_HASFFI - } else if (tp >= SER_TAG_INT64 && tp <= SER_TAG_COMPLEX) { - uint32_t sz = tp == SER_TAG_COMPLEX ? 16 : 8; - GCcdata *cd; - if (LJ_UNLIKELY(r + sz > w)) goto eob; - if (LJ_UNLIKELY(!ctype_ctsG(G(sbufL(sbx))))) goto badtag; - cd = lj_cdata_new_(sbufL(sbx), - tp == SER_TAG_INT64 ? CTID_INT64 : - tp == SER_TAG_UINT64 ? CTID_UINT64 : CTID_COMPLEX_DOUBLE, - sz); - memcpy(cdataptr(cd), r, sz); r += sz; -#if LJ_BE - *(uint64_t *)cdataptr(cd) = lj_bswap64(*(uint64_t *)cdataptr(cd)); - if (sz == 16) - ((uint64_t *)cdataptr(cd))[1] = lj_bswap64(((uint64_t *)cdataptr(cd))[1]); -#endif - if (sz == 16) { /* Fix non-canonical NaNs. */ - TValue *cdo = (TValue *)cdataptr(cd); - if (!tvisnum(&cdo[0])) setnanV(&cdo[0]); - if (!tvisnum(&cdo[1])) setnanV(&cdo[1]); - } - setcdataV(sbufL(sbx), o, cd); -#endif - } else if (tp <= (LJ_64 ? SER_TAG_LIGHTUD64 : SER_TAG_LIGHTUD32)) { - uintptr_t ud = 0; - if (tp == SER_TAG_LIGHTUD32) { - if (LJ_UNLIKELY(r + 4 > w)) goto eob; - ud = (uintptr_t)(LJ_BE ? lj_bswap(lj_getu32(r)) : lj_getu32(r)); - r += 4; - } -#if LJ_64 - else if (tp == SER_TAG_LIGHTUD64) { - if (LJ_UNLIKELY(r + 8 > w)) goto eob; - memcpy(&ud, r, 8); r += 8; -#if LJ_BE - ud = lj_bswap64(ud); -#endif - } - setrawlightudV(o, lj_lightud_intern(sbufL(sbx), (void *)ud)); -#else - setrawlightudV(o, (void *)ud); -#endif - } else { -badtag: - lj_err_callerv(sbufL(sbx), LJ_ERR_BUFFER_BADDEC, tp); - } - return r; -eob: - lj_err_caller(sbufL(sbx), LJ_ERR_BUFFER_EOB); - return NULL; -} - -/* -- External serialization API ------------------------------------------ */ - -/* Encode to buffer. */ -SBufExt * LJ_FASTCALL lj_serialize_put(SBufExt *sbx, cTValue *o) -{ - sbx->depth = LJ_SERIALIZE_DEPTH; - sbx->w = serialize_put(sbx->w, sbx, o); - return sbx; -} - -/* Decode from buffer. */ -char * LJ_FASTCALL lj_serialize_get(SBufExt *sbx, TValue *o) -{ - sbx->depth = LJ_SERIALIZE_DEPTH; - return serialize_get(sbx->r, sbx, o); -} - -/* Stand-alone encoding, borrowing from global temporary buffer. */ -GCstr * LJ_FASTCALL lj_serialize_encode(lua_State *L, cTValue *o) -{ - SBufExt sbx; - char *w; - memset(&sbx, 0, sizeof(SBufExt)); - lj_bufx_set_borrow(L, &sbx, &G(L)->tmpbuf); - sbx.depth = LJ_SERIALIZE_DEPTH; - w = serialize_put(sbx.w, &sbx, o); - return lj_str_new(L, sbx.b, (size_t)(w - sbx.b)); -} - -/* Stand-alone decoding, copy-on-write from string. */ -void lj_serialize_decode(lua_State *L, TValue *o, GCstr *str) -{ - SBufExt sbx; - char *r; - memset(&sbx, 0, sizeof(SBufExt)); - lj_bufx_set_cow(L, &sbx, strdata(str), str->len); - /* No need to set sbx.cowref here. */ - sbx.depth = LJ_SERIALIZE_DEPTH; - r = serialize_get(sbx.r, &sbx, o); - if (r != sbx.w) lj_err_caller(L, LJ_ERR_BUFFER_LEFTOV); -} - -#if LJ_HASJIT -/* Peek into buffer to find the result IRType for specialization purposes. */ -LJ_FUNC MSize LJ_FASTCALL lj_serialize_peektype(SBufExt *sbx) -{ - uint32_t tp; - if (serialize_ru124(sbx->r, sbx->w, &tp)) { - /* This must match the handling of all tags in the decoder above. */ - switch (tp) { - case SER_TAG_NIL: return IRT_NIL; - case SER_TAG_FALSE: return IRT_FALSE; - case SER_TAG_TRUE: return IRT_TRUE; - case SER_TAG_NULL: case SER_TAG_LIGHTUD32: case SER_TAG_LIGHTUD64: - return IRT_LIGHTUD; - case SER_TAG_INT: return LJ_DUALNUM ? IRT_INT : IRT_NUM; - case SER_TAG_NUM: return IRT_NUM; - case SER_TAG_TAB: case SER_TAG_TAB+1: case SER_TAG_TAB+2: - case SER_TAG_TAB+3: case SER_TAG_TAB+4: case SER_TAG_TAB+5: - case SER_TAG_DICT_MT: - return IRT_TAB; - case SER_TAG_INT64: case SER_TAG_UINT64: case SER_TAG_COMPLEX: - return IRT_CDATA; - case SER_TAG_DICT_STR: - default: - return IRT_STR; - } - } - return IRT_NIL; /* Will fail on actual decode. */ -} -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.h deleted file mode 100644 index d3f4275..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_serialize.h +++ /dev/null @@ -1,28 +0,0 @@ -/* -** Object de/serialization. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_SERIALIZE_H -#define _LJ_SERIALIZE_H - -#include "lj_obj.h" -#include "lj_buf.h" - -#if LJ_HASBUFFER - -#define LJ_SERIALIZE_DEPTH 100 /* Default depth. */ - -LJ_FUNC void LJ_FASTCALL lj_serialize_dict_prep_str(lua_State *L, GCtab *dict); -LJ_FUNC void LJ_FASTCALL lj_serialize_dict_prep_mt(lua_State *L, GCtab *dict); -LJ_FUNC SBufExt * LJ_FASTCALL lj_serialize_put(SBufExt *sbx, cTValue *o); -LJ_FUNC char * LJ_FASTCALL lj_serialize_get(SBufExt *sbx, TValue *o); -LJ_FUNC GCstr * LJ_FASTCALL lj_serialize_encode(lua_State *L, cTValue *o); -LJ_FUNC void lj_serialize_decode(lua_State *L, TValue *o, GCstr *str); -#if LJ_HASJIT -LJ_FUNC MSize LJ_FASTCALL lj_serialize_peektype(SBufExt *sbx); -#endif - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.c deleted file mode 100644 index 4140fdb..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.c +++ /dev/null @@ -1,996 +0,0 @@ -/* -** Snapshot handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_snap_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_frame.h" -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_target.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#include "lj_cdata.h" -#endif - -/* Pass IR on to next optimization in chain (FOLD). */ -#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) - -/* Emit raw IR without passing through optimizations. */ -#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) - -/* -- Snapshot buffer allocation ------------------------------------------ */ - -/* Grow snapshot buffer. */ -void lj_snap_grow_buf_(jit_State *J, MSize need) -{ - MSize maxsnap = (MSize)J->param[JIT_P_maxsnap]; - if (need > maxsnap) - lj_trace_err(J, LJ_TRERR_SNAPOV); - lj_mem_growvec(J->L, J->snapbuf, J->sizesnap, maxsnap, SnapShot); - J->cur.snap = J->snapbuf; -} - -/* Grow snapshot map buffer. */ -void lj_snap_grow_map_(jit_State *J, MSize need) -{ - if (need < 2*J->sizesnapmap) - need = 2*J->sizesnapmap; - else if (need < 64) - need = 64; - J->snapmapbuf = (SnapEntry *)lj_mem_realloc(J->L, J->snapmapbuf, - J->sizesnapmap*sizeof(SnapEntry), need*sizeof(SnapEntry)); - J->cur.snapmap = J->snapmapbuf; - J->sizesnapmap = need; -} - -/* -- Snapshot generation ------------------------------------------------- */ - -/* Add all modified slots to the snapshot. */ -static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots) -{ - IRRef retf = J->chain[IR_RETF]; /* Limits SLOAD restore elimination. */ - BCReg s; - MSize n = 0; - for (s = 0; s < nslots; s++) { - TRef tr = J->slot[s]; - IRRef ref = tref_ref(tr); -#if LJ_FR2 - if (s == 1) { /* Ignore slot 1 in LJ_FR2 mode, except if tailcalled. */ - if ((tr & TREF_FRAME)) - map[n++] = SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL); - continue; - } - if ((tr & (TREF_FRAME | TREF_CONT)) && !ref) { - cTValue *base = J->L->base - J->baseslot; - tr = J->slot[s] = (tr & 0xff0000) | lj_ir_k64(J, IR_KNUM, base[s].u64); - ref = tref_ref(tr); - } -#endif - if (ref) { - SnapEntry sn = SNAP_TR(s, tr); - IRIns *ir = &J->cur.ir[ref]; - if ((LJ_FR2 || !(sn & (SNAP_CONT|SNAP_FRAME))) && - ir->o == IR_SLOAD && ir->op1 == s && ref > retf) { - /* - ** No need to snapshot unmodified non-inherited slots. - ** But always snapshot the function below a frame in LJ_FR2 mode. - */ - if (!(ir->op2 & IRSLOAD_INHERIT) && - (!LJ_FR2 || s == 0 || s+1 == nslots || - !(J->slot[s+1] & (TREF_CONT|TREF_FRAME)))) - continue; - /* No need to restore readonly slots and unmodified non-parent slots. */ - if (!(LJ_DUALNUM && (ir->op2 & IRSLOAD_CONVERT)) && - (ir->op2 & (IRSLOAD_READONLY|IRSLOAD_PARENT)) != IRSLOAD_PARENT) - sn |= SNAP_NORESTORE; - } - if (LJ_SOFTFP32 && irt_isnum(ir->t)) - sn |= SNAP_SOFTFPNUM; - map[n++] = sn; - } - } - return n; -} - -/* Add frame links at the end of the snapshot. */ -static MSize snapshot_framelinks(jit_State *J, SnapEntry *map, uint8_t *topslot) -{ - cTValue *frame = J->L->base - 1; - cTValue *lim = J->L->base - J->baseslot + LJ_FR2; - GCfunc *fn = frame_func(frame); - cTValue *ftop = isluafunc(fn) ? (frame+funcproto(fn)->framesize) : J->L->top; -#if LJ_FR2 - uint64_t pcbase = (u64ptr(J->pc) << 8) | (J->baseslot - 2); - lj_assertJ(2 <= J->baseslot && J->baseslot <= 257, "bad baseslot"); - memcpy(map, &pcbase, sizeof(uint64_t)); -#else - MSize f = 0; - map[f++] = SNAP_MKPC(J->pc); /* The current PC is always the first entry. */ -#endif - lj_assertJ(!J->pt || - (J->pc >= proto_bc(J->pt) && - J->pc < proto_bc(J->pt) + J->pt->sizebc), "bad snapshot PC"); - while (frame > lim) { /* Backwards traversal of all frames above base. */ - if (frame_islua(frame)) { -#if !LJ_FR2 - map[f++] = SNAP_MKPC(frame_pc(frame)); -#endif - frame = frame_prevl(frame); - } else if (frame_iscont(frame)) { -#if !LJ_FR2 - map[f++] = SNAP_MKFTSZ(frame_ftsz(frame)); - map[f++] = SNAP_MKPC(frame_contpc(frame)); -#endif - frame = frame_prevd(frame); - } else { - lj_assertJ(!frame_isc(frame), "broken frame chain"); -#if !LJ_FR2 - map[f++] = SNAP_MKFTSZ(frame_ftsz(frame)); -#endif - frame = frame_prevd(frame); - continue; - } - if (frame + funcproto(frame_func(frame))->framesize > ftop) - ftop = frame + funcproto(frame_func(frame))->framesize; - } - *topslot = (uint8_t)(ftop - lim); -#if LJ_FR2 - lj_assertJ(sizeof(SnapEntry) * 2 == sizeof(uint64_t), "bad SnapEntry def"); - return 2; -#else - lj_assertJ(f == (MSize)(1 + J->framedepth), "miscalculated snapshot size"); - return f; -#endif -} - -/* Take a snapshot of the current stack. */ -static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap) -{ - BCReg nslots = J->baseslot + J->maxslot; - MSize nent; - SnapEntry *p; - /* Conservative estimate. */ - lj_snap_grow_map(J, nsnapmap + nslots + (MSize)(LJ_FR2?2:J->framedepth+1)); - p = &J->cur.snapmap[nsnapmap]; - nent = snapshot_slots(J, p, nslots); - snap->nent = (uint8_t)nent; - nent += snapshot_framelinks(J, p + nent, &snap->topslot); - snap->mapofs = (uint32_t)nsnapmap; - snap->ref = (IRRef1)J->cur.nins; - snap->mcofs = 0; - snap->nslots = (uint8_t)nslots; - snap->count = 0; - J->cur.nsnapmap = (uint32_t)(nsnapmap + nent); -} - -/* Add or merge a snapshot. */ -void lj_snap_add(jit_State *J) -{ - MSize nsnap = J->cur.nsnap; - MSize nsnapmap = J->cur.nsnapmap; - /* Merge if no ins. inbetween or if requested and no guard inbetween. */ - if ((nsnap > 0 && J->cur.snap[nsnap-1].ref == J->cur.nins) || - (J->mergesnap && !irt_isguard(J->guardemit))) { - if (nsnap == 1) { /* But preserve snap #0 PC. */ - emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0); - goto nomerge; - } - nsnapmap = J->cur.snap[--nsnap].mapofs; - } else { - nomerge: - lj_snap_grow_buf(J, nsnap+1); - J->cur.nsnap = (uint16_t)(nsnap+1); - } - J->mergesnap = 0; - J->guardemit.irt = 0; - snapshot_stack(J, &J->cur.snap[nsnap], nsnapmap); -} - -/* -- Snapshot modification ----------------------------------------------- */ - -#define SNAP_USEDEF_SLOTS (LJ_MAX_JSLOTS+LJ_STACK_EXTRA) - -/* Find unused slots with reaching-definitions bytecode data-flow analysis. */ -static BCReg snap_usedef(jit_State *J, uint8_t *udf, - const BCIns *pc, BCReg maxslot) -{ - BCReg s; - GCobj *o; - - if (maxslot == 0) return 0; -#ifdef LUAJIT_USE_VALGRIND - /* Avoid errors for harmless reads beyond maxslot. */ - memset(udf, 1, SNAP_USEDEF_SLOTS); -#else - memset(udf, 1, maxslot); -#endif - - /* Treat open upvalues as used. */ - o = gcref(J->L->openupval); - while (o) { - if (uvval(gco2uv(o)) < J->L->base) break; - udf[uvval(gco2uv(o)) - J->L->base] = 0; - o = gcref(o->gch.nextgc); - } - -#define USE_SLOT(s) udf[(s)] &= ~1 -#define DEF_SLOT(s) udf[(s)] *= 3 - - /* Scan through following bytecode and check for uses/defs. */ - lj_assertJ(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc, - "snapshot PC out of range"); - for (;;) { - BCIns ins = *pc++; - BCOp op = bc_op(ins); - switch (bcmode_b(op)) { - case BCMvar: USE_SLOT(bc_b(ins)); break; - default: break; - } - switch (bcmode_c(op)) { - case BCMvar: USE_SLOT(bc_c(ins)); break; - case BCMrbase: - lj_assertJ(op == BC_CAT, "unhandled op %d with RC rbase", op); - for (s = bc_b(ins); s <= bc_c(ins); s++) USE_SLOT(s); - for (; s < maxslot; s++) DEF_SLOT(s); - break; - case BCMjump: - handle_jump: { - BCReg minslot = bc_a(ins); - if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT; - else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1; - else if (op == BC_UCLO) { - ptrdiff_t delta = bc_j(ins); - if (delta < 0) return maxslot; /* Prevent loop. */ - pc += delta; - break; - } - for (s = minslot; s < maxslot; s++) DEF_SLOT(s); - return minslot < maxslot ? minslot : maxslot; - } - case BCMlit: - if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { - goto handle_jump; - } else if (bc_isret(op)) { - BCReg top = op == BC_RETM ? maxslot : (bc_a(ins) + bc_d(ins)-1); - for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s); - for (; s < top; s++) USE_SLOT(s); - for (; s < maxslot; s++) DEF_SLOT(s); - return 0; - } - break; - case BCMfunc: return maxslot; /* NYI: will abort, anyway. */ - default: break; - } - switch (bcmode_a(op)) { - case BCMvar: USE_SLOT(bc_a(ins)); break; - case BCMdst: - if (!(op == BC_ISTC || op == BC_ISFC)) DEF_SLOT(bc_a(ins)); - break; - case BCMbase: - if (op >= BC_CALLM && op <= BC_ITERN) { - BCReg top = (op == BC_CALLM || op == BC_CALLMT || bc_c(ins) == 0) ? - maxslot : (bc_a(ins) + bc_c(ins)+LJ_FR2); - if (LJ_FR2) DEF_SLOT(bc_a(ins)+1); - s = bc_a(ins) - ((op == BC_ITERC || op == BC_ITERN) ? 3 : 0); - for (; s < top; s++) USE_SLOT(s); - for (; s < maxslot; s++) DEF_SLOT(s); - if (op == BC_CALLT || op == BC_CALLMT) { - for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s); - return 0; - } - } else if (op == BC_VARG) { - return maxslot; /* NYI: punt. */ - } else if (op == BC_KNIL) { - for (s = bc_a(ins); s <= bc_d(ins); s++) DEF_SLOT(s); - } else if (op == BC_TSETM) { - for (s = bc_a(ins)-1; s < maxslot; s++) USE_SLOT(s); - } - break; - default: break; - } - lj_assertJ(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc, - "use/def analysis PC out of range"); - } - -#undef USE_SLOT -#undef DEF_SLOT - - return 0; /* unreachable */ -} - -/* Mark slots used by upvalues of child prototypes as used. */ -static void snap_useuv(GCproto *pt, uint8_t *udf) -{ - /* This is a coarse check, because it's difficult to correlate the lifetime - ** of slots and closures. But the number of false positives is quite low. - ** A false positive may cause a slot not to be purged, which is just - ** a missed optimization. - */ - if ((pt->flags & PROTO_CHILD)) { - ptrdiff_t i, j, n = pt->sizekgc; - GCRef *kr = mref(pt->k, GCRef) - 1; - for (i = 0; i < n; i++, kr--) { - GCobj *o = gcref(*kr); - if (o->gch.gct == ~LJ_TPROTO) { - for (j = 0; j < gco2pt(o)->sizeuv; j++) { - uint32_t v = proto_uv(gco2pt(o))[j]; - if ((v & PROTO_UV_LOCAL)) { - udf[(v & 0xff)] = 0; - } - } - } - } - } -} - -/* Purge dead slots before the next snapshot. */ -void lj_snap_purge(jit_State *J) -{ - uint8_t udf[SNAP_USEDEF_SLOTS]; - BCReg s, maxslot = J->maxslot; - if (bc_op(*J->pc) == BC_FUNCV && maxslot > J->pt->numparams) - maxslot = J->pt->numparams; - s = snap_usedef(J, udf, J->pc, maxslot); - if (s < maxslot) { - snap_useuv(J->pt, udf); - for (; s < maxslot; s++) - if (udf[s] != 0) - J->base[s] = 0; /* Purge dead slots. */ - } -} - -/* Shrink last snapshot. */ -void lj_snap_shrink(jit_State *J) -{ - SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; - SnapEntry *map = &J->cur.snapmap[snap->mapofs]; - MSize n, m, nlim, nent = snap->nent; - uint8_t udf[SNAP_USEDEF_SLOTS]; - BCReg maxslot = J->maxslot; - BCReg baseslot = J->baseslot; - BCReg minslot = snap_usedef(J, udf, snap_pc(&map[nent]), maxslot); - if (minslot < maxslot) snap_useuv(J->pt, udf); - maxslot += baseslot; - minslot += baseslot; - snap->nslots = (uint8_t)maxslot; - for (n = m = 0; n < nent; n++) { /* Remove unused slots from snapshot. */ - BCReg s = snap_slot(map[n]); - if (s < minslot || (s < maxslot && udf[s-baseslot] == 0)) - map[m++] = map[n]; /* Only copy used slots. */ - } - snap->nent = (uint8_t)m; - nlim = J->cur.nsnapmap - snap->mapofs - 1; - while (n <= nlim) map[m++] = map[n++]; /* Move PC + frame links down. */ - J->cur.nsnapmap = (uint32_t)(snap->mapofs + m); /* Free up space in map. */ -} - -/* -- Snapshot access ----------------------------------------------------- */ - -/* Initialize a Bloom Filter with all renamed refs. -** There are very few renames (often none), so the filter has -** very few bits set. This makes it suitable for negative filtering. -*/ -static BloomFilter snap_renamefilter(GCtrace *T, SnapNo lim) -{ - BloomFilter rfilt = 0; - IRIns *ir; - for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--) - if (ir->op2 <= lim) - bloomset(rfilt, ir->op1); - return rfilt; -} - -/* Process matching renames to find the original RegSP. */ -static RegSP snap_renameref(GCtrace *T, SnapNo lim, IRRef ref, RegSP rs) -{ - IRIns *ir; - for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--) - if (ir->op1 == ref && ir->op2 <= lim) - rs = ir->prev; - return rs; -} - -/* Copy RegSP from parent snapshot to the parent links of the IR. */ -IRIns *lj_snap_regspmap(jit_State *J, GCtrace *T, SnapNo snapno, IRIns *ir) -{ - SnapShot *snap = &T->snap[snapno]; - SnapEntry *map = &T->snapmap[snap->mapofs]; - BloomFilter rfilt = snap_renamefilter(T, snapno); - MSize n = 0; - IRRef ref = 0; - UNUSED(J); - for ( ; ; ir++) { - uint32_t rs; - if (ir->o == IR_SLOAD) { - if (!(ir->op2 & IRSLOAD_PARENT)) break; - for ( ; ; n++) { - lj_assertJ(n < snap->nent, "slot %d not found in snapshot", ir->op1); - if (snap_slot(map[n]) == ir->op1) { - ref = snap_ref(map[n++]); - break; - } - } - } else if (LJ_SOFTFP32 && ir->o == IR_HIOP) { - ref++; - } else if (ir->o == IR_PVAL) { - ref = ir->op1 + REF_BIAS; - } else { - break; - } - rs = T->ir[ref].prev; - if (bloomtest(rfilt, ref)) - rs = snap_renameref(T, snapno, ref, rs); - ir->prev = (uint16_t)rs; - lj_assertJ(regsp_used(rs), "unused IR %04d in snapshot", ref - REF_BIAS); - } - return ir; -} - -/* -- Snapshot replay ----------------------------------------------------- */ - -/* Replay constant from parent trace. */ -static TRef snap_replay_const(jit_State *J, IRIns *ir) -{ - /* Only have to deal with constants that can occur in stack slots. */ - switch ((IROp)ir->o) { - case IR_KPRI: return TREF_PRI(irt_type(ir->t)); - case IR_KINT: return lj_ir_kint(J, ir->i); - case IR_KGC: return lj_ir_kgc(J, ir_kgc(ir), irt_t(ir->t)); - case IR_KNUM: case IR_KINT64: - return lj_ir_k64(J, (IROp)ir->o, ir_k64(ir)->u64); - case IR_KPTR: return lj_ir_kptr(J, ir_kptr(ir)); /* Continuation. */ - default: lj_assertJ(0, "bad IR constant op %d", ir->o); return TREF_NIL; - } -} - -/* De-duplicate parent reference. */ -static TRef snap_dedup(jit_State *J, SnapEntry *map, MSize nmax, IRRef ref) -{ - MSize j; - for (j = 0; j < nmax; j++) - if (snap_ref(map[j]) == ref) - return J->slot[snap_slot(map[j])] & ~(SNAP_KEYINDEX|SNAP_CONT|SNAP_FRAME); - return 0; -} - -/* Emit parent reference with de-duplication. */ -static TRef snap_pref(jit_State *J, GCtrace *T, SnapEntry *map, MSize nmax, - BloomFilter seen, IRRef ref) -{ - IRIns *ir = &T->ir[ref]; - TRef tr; - if (irref_isk(ref)) - tr = snap_replay_const(J, ir); - else if (!regsp_used(ir->prev)) - tr = 0; - else if (!bloomtest(seen, ref) || (tr = snap_dedup(J, map, nmax, ref)) == 0) - tr = emitir(IRT(IR_PVAL, irt_type(ir->t)), ref - REF_BIAS, 0); - return tr; -} - -/* Check whether a sunk store corresponds to an allocation. Slow path. */ -static int snap_sunk_store2(GCtrace *T, IRIns *ira, IRIns *irs) -{ - if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE || irs->o == IR_XSTORE) { - IRIns *irk = &T->ir[irs->op1]; - if (irk->o == IR_AREF || irk->o == IR_HREFK) - irk = &T->ir[irk->op1]; - return (&T->ir[irk->op1] == ira); - } - return 0; -} - -/* Check whether a sunk store corresponds to an allocation. Fast path. */ -static LJ_AINLINE int snap_sunk_store(GCtrace *T, IRIns *ira, IRIns *irs) -{ - if (irs->s != 255) - return (ira + irs->s == irs); /* Fast check. */ - return snap_sunk_store2(T, ira, irs); -} - -/* Replay snapshot state to setup side trace. */ -void lj_snap_replay(jit_State *J, GCtrace *T) -{ - SnapShot *snap = &T->snap[J->exitno]; - SnapEntry *map = &T->snapmap[snap->mapofs]; - MSize n, nent = snap->nent; - BloomFilter seen = 0; - int pass23 = 0; - J->framedepth = 0; - /* Emit IR for slots inherited from parent snapshot. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - BCReg s = snap_slot(sn); - IRRef ref = snap_ref(sn); - IRIns *ir = &T->ir[ref]; - TRef tr; - /* The bloom filter avoids O(nent^2) overhead for de-duping slots. */ - if (bloomtest(seen, ref) && (tr = snap_dedup(J, map, n, ref)) != 0) - goto setslot; - bloomset(seen, ref); - if (irref_isk(ref)) { - /* See special treatment of LJ_FR2 slot 1 in snapshot_slots() above. */ - if (LJ_FR2 && (sn == SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL))) - tr = 0; - else - tr = snap_replay_const(J, ir); - } else if (!regsp_used(ir->prev)) { - pass23 = 1; - lj_assertJ(s != 0, "unused slot 0 in snapshot"); - tr = s; - } else { - IRType t = irt_type(ir->t); - uint32_t mode = IRSLOAD_INHERIT|IRSLOAD_PARENT; - if (LJ_SOFTFP32 && (sn & SNAP_SOFTFPNUM)) t = IRT_NUM; - if (ir->o == IR_SLOAD) mode |= (ir->op2 & IRSLOAD_READONLY); - if ((sn & SNAP_KEYINDEX)) mode |= IRSLOAD_KEYINDEX; - tr = emitir_raw(IRT(IR_SLOAD, t), s, mode); - } - setslot: - /* Same as TREF_* flags. */ - J->slot[s] = tr | (sn&(SNAP_KEYINDEX|SNAP_CONT|SNAP_FRAME)); - J->framedepth += ((sn & (SNAP_CONT|SNAP_FRAME)) && (s != LJ_FR2)); - if ((sn & SNAP_FRAME)) - J->baseslot = s+1; - } - if (pass23) { - IRIns *irlast = &T->ir[snap->ref]; - pass23 = 0; - /* Emit dependent PVALs. */ - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - IRRef refp = snap_ref(sn); - IRIns *ir = &T->ir[refp]; - if (regsp_reg(ir->r) == RID_SUNK) { - if (J->slot[snap_slot(sn)] != snap_slot(sn)) continue; - pass23 = 1; - lj_assertJ(ir->o == IR_TNEW || ir->o == IR_TDUP || - ir->o == IR_CNEW || ir->o == IR_CNEWI, - "sunk parent IR %04d has bad op %d", refp - REF_BIAS, ir->o); - if (ir->op1 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op1); - if (ir->op2 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op2); - if (LJ_HASFFI && ir->o == IR_CNEWI) { - if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) - snap_pref(J, T, map, nent, seen, (ir+1)->op2); - } else { - IRIns *irs; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) { - if (snap_pref(J, T, map, nent, seen, irs->op2) == 0) - snap_pref(J, T, map, nent, seen, T->ir[irs->op2].op1); - else if ((LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) && - irs+1 < irlast && (irs+1)->o == IR_HIOP) - snap_pref(J, T, map, nent, seen, (irs+1)->op2); - } - } - } else if (!irref_isk(refp) && !regsp_used(ir->prev)) { - lj_assertJ(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT, - "sunk parent IR %04d has bad op %d", refp - REF_BIAS, ir->o); - J->slot[snap_slot(sn)] = snap_pref(J, T, map, nent, seen, ir->op1); - } - } - /* Replay sunk instructions. */ - for (n = 0; pass23 && n < nent; n++) { - SnapEntry sn = map[n]; - IRRef refp = snap_ref(sn); - IRIns *ir = &T->ir[refp]; - if (regsp_reg(ir->r) == RID_SUNK) { - TRef op1, op2; - if (J->slot[snap_slot(sn)] != snap_slot(sn)) { /* De-dup allocs. */ - J->slot[snap_slot(sn)] = J->slot[J->slot[snap_slot(sn)]]; - continue; - } - op1 = ir->op1; - if (op1 >= T->nk) op1 = snap_pref(J, T, map, nent, seen, op1); - op2 = ir->op2; - if (op2 >= T->nk) op2 = snap_pref(J, T, map, nent, seen, op2); - if (LJ_HASFFI && ir->o == IR_CNEWI) { - if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) { - lj_needsplit(J); /* Emit joining HIOP. */ - op2 = emitir_raw(IRT(IR_HIOP, IRT_I64), op2, - snap_pref(J, T, map, nent, seen, (ir+1)->op2)); - } - J->slot[snap_slot(sn)] = emitir(ir->ot & ~(IRT_MARK|IRT_ISPHI), op1, op2); - } else { - IRIns *irs; - TRef tr = emitir(ir->ot, op1, op2); - J->slot[snap_slot(sn)] = tr; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) { - IRIns *irr = &T->ir[irs->op1]; - TRef val, key = irr->op2, tmp = tr; - if (irr->o != IR_FREF) { - IRIns *irk = &T->ir[key]; - if (irr->o == IR_HREFK) - key = lj_ir_kslot(J, snap_replay_const(J, &T->ir[irk->op1]), - irk->op2); - else - key = snap_replay_const(J, irk); - if (irr->o == IR_HREFK || irr->o == IR_AREF) { - IRIns *irf = &T->ir[irr->op1]; - tmp = emitir(irf->ot, tmp, irf->op2); - } - } - tmp = emitir(irr->ot, tmp, key); - val = snap_pref(J, T, map, nent, seen, irs->op2); - if (val == 0) { - IRIns *irc = &T->ir[irs->op2]; - lj_assertJ(irc->o == IR_CONV && irc->op2 == IRCONV_NUM_INT, - "sunk store for parent IR %04d with bad op %d", - refp - REF_BIAS, irc->o); - val = snap_pref(J, T, map, nent, seen, irc->op1); - val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT); - } else if ((LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) && - irs+1 < irlast && (irs+1)->o == IR_HIOP) { - IRType t = IRT_I64; - if (LJ_SOFTFP32 && irt_type((irs+1)->t) == IRT_SOFTFP) - t = IRT_NUM; - lj_needsplit(J); - if (irref_isk(irs->op2) && irref_isk((irs+1)->op2)) { - uint64_t k = (uint32_t)T->ir[irs->op2].i + - ((uint64_t)T->ir[(irs+1)->op2].i << 32); - val = lj_ir_k64(J, t == IRT_I64 ? IR_KINT64 : IR_KNUM, k); - } else { - val = emitir_raw(IRT(IR_HIOP, t), val, - snap_pref(J, T, map, nent, seen, (irs+1)->op2)); - } - tmp = emitir(IRT(irs->o, t), tmp, val); - continue; - } - tmp = emitir(irs->ot, tmp, val); - } else if (LJ_HASFFI && irs->o == IR_XBAR && ir->o == IR_CNEW) { - emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); - } - } - } - } - } - J->base = J->slot + J->baseslot; - J->maxslot = snap->nslots - J->baseslot; - lj_snap_add(J); - if (pass23) /* Need explicit GC step _after_ initial snapshot. */ - emitir_raw(IRTG(IR_GCSTEP, IRT_NIL), 0, 0); -} - -/* -- Snapshot restore ---------------------------------------------------- */ - -static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRIns *ir, TValue *o); - -/* Restore a value from the trace exit state. */ -static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRRef ref, TValue *o) -{ - IRIns *ir = &T->ir[ref]; - IRType1 t = ir->t; - RegSP rs = ir->prev; - if (irref_isk(ref)) { /* Restore constant slot. */ - if (ir->o == IR_KPTR) { - o->u64 = (uint64_t)(uintptr_t)ir_kptr(ir); - } else { - lj_assertJ(!(ir->o == IR_KKPTR || ir->o == IR_KNULL), - "restore of const from IR %04d with bad op %d", - ref - REF_BIAS, ir->o); - lj_ir_kvalue(J->L, o, ir); - } - return; - } - if (LJ_UNLIKELY(bloomtest(rfilt, ref))) - rs = snap_renameref(T, snapno, ref, rs); - if (ra_hasspill(regsp_spill(rs))) { /* Restore from spill slot. */ - int32_t *sps = &ex->spill[regsp_spill(rs)]; - if (irt_isinteger(t)) { - setintV(o, *sps); -#if !LJ_SOFTFP32 - } else if (irt_isnum(t)) { - o->u64 = *(uint64_t *)sps; -#endif -#if LJ_64 && !LJ_GC64 - } else if (irt_islightud(t)) { - /* 64 bit lightuserdata which may escape already has the tag bits. */ - o->u64 = *(uint64_t *)sps; -#endif - } else { - lj_assertJ(!irt_ispri(t), "PRI ref with spill slot"); - setgcV(J->L, o, (GCobj *)(uintptr_t)*(GCSize *)sps, irt_toitype(t)); - } - } else { /* Restore from register. */ - Reg r = regsp_reg(rs); - if (ra_noreg(r)) { - lj_assertJ(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT, - "restore from IR %04d has no reg", ref - REF_BIAS); - snap_restoreval(J, T, ex, snapno, rfilt, ir->op1, o); - if (LJ_DUALNUM) setnumV(o, (lua_Number)intV(o)); - return; - } else if (irt_isinteger(t)) { - setintV(o, (int32_t)ex->gpr[r-RID_MIN_GPR]); -#if !LJ_SOFTFP - } else if (irt_isnum(t)) { - setnumV(o, ex->fpr[r-RID_MIN_FPR]); -#elif LJ_64 /* && LJ_SOFTFP */ - } else if (irt_isnum(t)) { - o->u64 = ex->gpr[r-RID_MIN_GPR]; -#endif -#if LJ_64 && !LJ_GC64 - } else if (irt_is64(t)) { - /* 64 bit values that already have the tag bits. */ - o->u64 = ex->gpr[r-RID_MIN_GPR]; -#endif - } else if (irt_ispri(t)) { - setpriV(o, irt_toitype(t)); - } else { - setgcV(J->L, o, (GCobj *)ex->gpr[r-RID_MIN_GPR], irt_toitype(t)); - } - } -} - -#if LJ_HASFFI -/* Restore raw data from the trace exit state. */ -static void snap_restoredata(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRRef ref, void *dst, CTSize sz) -{ - IRIns *ir = &T->ir[ref]; - RegSP rs = ir->prev; - int32_t *src; - uint64_t tmp; - UNUSED(J); - if (irref_isk(ref)) { - if (ir_isk64(ir)) { - src = (int32_t *)&ir[1]; - } else if (sz == 8) { - tmp = (uint64_t)(uint32_t)ir->i; - src = (int32_t *)&tmp; - } else { - src = &ir->i; - } - } else { - if (LJ_UNLIKELY(bloomtest(rfilt, ref))) - rs = snap_renameref(T, snapno, ref, rs); - if (ra_hasspill(regsp_spill(rs))) { - src = &ex->spill[regsp_spill(rs)]; - if (sz == 8 && !irt_is64(ir->t)) { - tmp = (uint64_t)(uint32_t)*src; - src = (int32_t *)&tmp; - } - } else { - Reg r = regsp_reg(rs); - if (ra_noreg(r)) { - /* Note: this assumes CNEWI is never used for SOFTFP split numbers. */ - lj_assertJ(sz == 8 && ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT, - "restore from IR %04d has no reg", ref - REF_BIAS); - snap_restoredata(J, T, ex, snapno, rfilt, ir->op1, dst, 4); - *(lua_Number *)dst = (lua_Number)*(int32_t *)dst; - return; - } - src = (int32_t *)&ex->gpr[r-RID_MIN_GPR]; -#if !LJ_SOFTFP - if (r >= RID_MAX_GPR) { - src = (int32_t *)&ex->fpr[r-RID_MIN_FPR]; -#if LJ_TARGET_PPC - if (sz == 4) { /* PPC FPRs are always doubles. */ - *(float *)dst = (float)*(double *)src; - return; - } -#else - if (LJ_BE && sz == 4) src++; -#endif - } else -#endif - if (LJ_64 && LJ_BE && sz == 4) src++; - } - } - lj_assertJ(sz == 1 || sz == 2 || sz == 4 || sz == 8, - "restore from IR %04d with bad size %d", ref - REF_BIAS, sz); - if (sz == 4) *(int32_t *)dst = *src; - else if (sz == 8) *(int64_t *)dst = *(int64_t *)src; - else if (sz == 1) *(int8_t *)dst = (int8_t)*src; - else *(int16_t *)dst = (int16_t)*src; -} -#endif - -/* Unsink allocation from the trace exit state. Unsink sunk stores. */ -static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex, - SnapNo snapno, BloomFilter rfilt, - IRIns *ir, TValue *o) -{ - lj_assertJ(ir->o == IR_TNEW || ir->o == IR_TDUP || - ir->o == IR_CNEW || ir->o == IR_CNEWI, - "sunk allocation with bad op %d", ir->o); -#if LJ_HASFFI - if (ir->o == IR_CNEW || ir->o == IR_CNEWI) { - CTState *cts = ctype_cts(J->L); - CTypeID id = (CTypeID)T->ir[ir->op1].i; - CTSize sz; - CTInfo info = lj_ctype_info(cts, id, &sz); - GCcdata *cd = lj_cdata_newx(cts, id, sz, info); - setcdataV(J->L, o, cd); - if (ir->o == IR_CNEWI) { - uint8_t *p = (uint8_t *)cdataptr(cd); - lj_assertJ(sz == 4 || sz == 8, "sunk cdata with bad size %d", sz); - if (LJ_32 && sz == 8 && ir+1 < T->ir + T->nins && (ir+1)->o == IR_HIOP) { - snap_restoredata(J, T, ex, snapno, rfilt, (ir+1)->op2, - LJ_LE ? p+4 : p, 4); - if (LJ_BE) p += 4; - sz = 4; - } - snap_restoredata(J, T, ex, snapno, rfilt, ir->op2, p, sz); - } else { - IRIns *irs, *irlast = &T->ir[T->snap[snapno].ref]; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) { - IRIns *iro = &T->ir[T->ir[irs->op1].op2]; - uint8_t *p = (uint8_t *)cd; - CTSize szs; - lj_assertJ(irs->o == IR_XSTORE, "sunk store with bad op %d", irs->o); - lj_assertJ(T->ir[irs->op1].o == IR_ADD, - "sunk store with bad add op %d", T->ir[irs->op1].o); - lj_assertJ(iro->o == IR_KINT || iro->o == IR_KINT64, - "sunk store with bad const offset op %d", iro->o); - if (irt_is64(irs->t)) szs = 8; - else if (irt_isi8(irs->t) || irt_isu8(irs->t)) szs = 1; - else if (irt_isi16(irs->t) || irt_isu16(irs->t)) szs = 2; - else szs = 4; - if (LJ_64 && iro->o == IR_KINT64) - p += (int64_t)ir_k64(iro)->u64; - else - p += iro->i; - lj_assertJ(p >= (uint8_t *)cdataptr(cd) && - p + szs <= (uint8_t *)cdataptr(cd) + sz, - "sunk store with offset out of range"); - if (LJ_32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) { - lj_assertJ(szs == 4, "sunk store with bad size %d", szs); - snap_restoredata(J, T, ex, snapno, rfilt, (irs+1)->op2, - LJ_LE ? p+4 : p, 4); - if (LJ_BE) p += 4; - } - snap_restoredata(J, T, ex, snapno, rfilt, irs->op2, p, szs); - } - } - } else -#endif - { - IRIns *irs, *irlast; - GCtab *t = ir->o == IR_TNEW ? lj_tab_new(J->L, ir->op1, ir->op2) : - lj_tab_dup(J->L, ir_ktab(&T->ir[ir->op1])); - settabV(J->L, o, t); - irlast = &T->ir[T->snap[snapno].ref]; - for (irs = ir+1; irs < irlast; irs++) - if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) { - IRIns *irk = &T->ir[irs->op1]; - TValue tmp, *val; - lj_assertJ(irs->o == IR_ASTORE || irs->o == IR_HSTORE || - irs->o == IR_FSTORE, - "sunk store with bad op %d", irs->o); - if (irk->o == IR_FREF) { - lj_assertJ(irk->op2 == IRFL_TAB_META, - "sunk store with bad field %d", irk->op2); - snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, &tmp); - /* NOBARRIER: The table is new (marked white). */ - setgcref(t->metatable, obj2gco(tabV(&tmp))); - } else { - irk = &T->ir[irk->op2]; - if (irk->o == IR_KSLOT) irk = &T->ir[irk->op1]; - lj_ir_kvalue(J->L, &tmp, irk); - val = lj_tab_set(J->L, t, &tmp); - /* NOBARRIER: The table is new (marked white). */ - snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, val); - if (LJ_SOFTFP32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) { - snap_restoreval(J, T, ex, snapno, rfilt, (irs+1)->op2, &tmp); - val->u32.hi = tmp.u32.lo; - } - } - } - } -} - -/* Restore interpreter state from exit state with the help of a snapshot. */ -const BCIns *lj_snap_restore(jit_State *J, void *exptr) -{ - ExitState *ex = (ExitState *)exptr; - SnapNo snapno = J->exitno; /* For now, snapno == exitno. */ - GCtrace *T = traceref(J, J->parent); - SnapShot *snap = &T->snap[snapno]; - MSize n, nent = snap->nent; - SnapEntry *map = &T->snapmap[snap->mapofs]; -#if !LJ_FR2 || defined(LUA_USE_ASSERT) - SnapEntry *flinks = &T->snapmap[snap_nextofs(T, snap)-1-LJ_FR2]; -#endif -#if !LJ_FR2 - ptrdiff_t ftsz0; -#endif - TValue *frame; - BloomFilter rfilt = snap_renamefilter(T, snapno); - const BCIns *pc = snap_pc(&map[nent]); - lua_State *L = J->L; - - /* Set interpreter PC to the next PC to get correct error messages. */ - setcframe_pc(cframe_raw(L->cframe), pc+1); - - /* Make sure the stack is big enough for the slots from the snapshot. */ - if (LJ_UNLIKELY(L->base + snap->topslot >= tvref(L->maxstack))) { - L->top = curr_topL(L); - lj_state_growstack(L, snap->topslot - curr_proto(L)->framesize); - } - - /* Fill stack slots with data from the registers and spill slots. */ - frame = L->base-1-LJ_FR2; -#if !LJ_FR2 - ftsz0 = frame_ftsz(frame); /* Preserve link to previous frame in slot #0. */ -#endif - for (n = 0; n < nent; n++) { - SnapEntry sn = map[n]; - if (!(sn & SNAP_NORESTORE)) { - TValue *o = &frame[snap_slot(sn)]; - IRRef ref = snap_ref(sn); - IRIns *ir = &T->ir[ref]; - if (ir->r == RID_SUNK) { - MSize j; - for (j = 0; j < n; j++) - if (snap_ref(map[j]) == ref) { /* De-duplicate sunk allocations. */ - copyTV(L, o, &frame[snap_slot(map[j])]); - goto dupslot; - } - snap_unsink(J, T, ex, snapno, rfilt, ir, o); - dupslot: - continue; - } - snap_restoreval(J, T, ex, snapno, rfilt, ref, o); - if (LJ_SOFTFP32 && (sn & SNAP_SOFTFPNUM) && tvisint(o)) { - TValue tmp; - snap_restoreval(J, T, ex, snapno, rfilt, ref+1, &tmp); - o->u32.hi = tmp.u32.lo; -#if !LJ_FR2 - } else if ((sn & (SNAP_CONT|SNAP_FRAME))) { - /* Overwrite tag with frame link. */ - setframe_ftsz(o, snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0); - L->base = o+1; -#endif - } else if ((sn & SNAP_KEYINDEX)) { - /* A IRT_INT key index slot is restored as a number. Undo this. */ - o->u32.lo = (uint32_t)(LJ_DUALNUM ? intV(o) : lj_num2int(numV(o))); - o->u32.hi = LJ_KEYINDEX; - } - } - } -#if LJ_FR2 - L->base += (map[nent+LJ_BE] & 0xff); -#endif - lj_assertJ(map + nent == flinks, "inconsistent frames in snapshot"); - - /* Compute current stack top. */ - switch (bc_op(*pc)) { - default: - if (bc_op(*pc) < BC_FUNCF) { - L->top = curr_topL(L); - break; - } - /* fallthrough */ - case BC_CALLM: case BC_CALLMT: case BC_RETM: case BC_TSETM: - L->top = frame + snap->nslots; - break; - } - return pc; -} - -#undef emitir_raw -#undef emitir - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.h deleted file mode 100644 index b7dabed..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_snap.h +++ /dev/null @@ -1,35 +0,0 @@ -/* -** Snapshot handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_SNAP_H -#define _LJ_SNAP_H - -#include "lj_obj.h" -#include "lj_jit.h" - -#if LJ_HASJIT -LJ_FUNC void lj_snap_add(jit_State *J); -LJ_FUNC void lj_snap_purge(jit_State *J); -LJ_FUNC void lj_snap_shrink(jit_State *J); -LJ_FUNC IRIns *lj_snap_regspmap(jit_State *J, GCtrace *T, SnapNo snapno, - IRIns *ir); -LJ_FUNC void lj_snap_replay(jit_State *J, GCtrace *T); -LJ_FUNC const BCIns *lj_snap_restore(jit_State *J, void *exptr); -LJ_FUNC void lj_snap_grow_buf_(jit_State *J, MSize need); -LJ_FUNC void lj_snap_grow_map_(jit_State *J, MSize need); - -static LJ_AINLINE void lj_snap_grow_buf(jit_State *J, MSize need) -{ - if (LJ_UNLIKELY(need > J->sizesnap)) lj_snap_grow_buf_(J, need); -} - -static LJ_AINLINE void lj_snap_grow_map(jit_State *J, MSize need) -{ - if (LJ_UNLIKELY(need > J->sizesnapmap)) lj_snap_grow_map_(J, need); -} - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.c deleted file mode 100644 index 0b9c46b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.c +++ /dev/null @@ -1,335 +0,0 @@ -/* -** State and stack handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_state_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_func.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_frame.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_trace.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_prng.h" -#include "lj_lex.h" -#include "lj_alloc.h" -#include "luajit.h" - -/* -- Stack handling ------------------------------------------------------ */ - -/* Stack sizes. */ -#define LJ_STACK_MIN LUA_MINSTACK /* Min. stack size. */ -#define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */ -#define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */ -#define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA) - -/* Explanation of LJ_STACK_EXTRA: -** -** Calls to metamethods store their arguments beyond the current top -** without checking for the stack limit. This avoids stack resizes which -** would invalidate passed TValue pointers. The stack check is performed -** later by the function header. This can safely resize the stack or raise -** an error. Thus we need some extra slots beyond the current stack limit. -** -** Most metamethods need 4 slots above top (cont, mobj, arg1, arg2) plus -** one extra slot if mobj is not a function. Only lj_meta_tset needs 5 -** slots above top, but then mobj is always a function. So we can get by -** with 5 extra slots. -** LJ_FR2: We need 2 more slots for the frame PC and the continuation PC. -*/ - -/* Resize stack slots and adjust pointers in state. */ -static void resizestack(lua_State *L, MSize n) -{ - TValue *st, *oldst = tvref(L->stack); - ptrdiff_t delta; - MSize oldsize = L->stacksize; - MSize realsize = n + 1 + LJ_STACK_EXTRA; - GCobj *up; - lj_assertL((MSize)(tvref(L->maxstack)-oldst) == L->stacksize-LJ_STACK_EXTRA-1, - "inconsistent stack size"); - st = (TValue *)lj_mem_realloc(L, tvref(L->stack), - (MSize)(oldsize*sizeof(TValue)), - (MSize)(realsize*sizeof(TValue))); - setmref(L->stack, st); - delta = (char *)st - (char *)oldst; - setmref(L->maxstack, st + n); - while (oldsize < realsize) /* Clear new slots. */ - setnilV(st + oldsize++); - L->stacksize = realsize; - if ((size_t)(mref(G(L)->jit_base, char) - (char *)oldst) < oldsize) - setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta); - L->base = (TValue *)((char *)L->base + delta); - L->top = (TValue *)((char *)L->top + delta); - for (up = gcref(L->openupval); up != NULL; up = gcnext(up)) - setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta)); -} - -/* Relimit stack after error, in case the limit was overdrawn. */ -void lj_state_relimitstack(lua_State *L) -{ - if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1) - resizestack(L, LJ_STACK_MAX); -} - -/* Try to shrink the stack (called from GC). */ -void lj_state_shrinkstack(lua_State *L, MSize used) -{ - if (L->stacksize > LJ_STACK_MAXEX) - return; /* Avoid stack shrinking while handling stack overflow. */ - if (4*used < L->stacksize && - 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize && - /* Don't shrink stack of live trace. */ - (tvref(G(L)->jit_base) == NULL || obj2gco(L) != gcref(G(L)->cur_L))) - resizestack(L, L->stacksize >> 1); -} - -/* Try to grow stack. */ -void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) -{ - MSize n; - if (L->stacksize > LJ_STACK_MAXEX) /* Overflow while handling overflow? */ - lj_err_throw(L, LUA_ERRERR); - n = L->stacksize + need; - if (n > LJ_STACK_MAX) { - n += 2*LUA_MINSTACK; - } else if (n < 2*L->stacksize) { - n = 2*L->stacksize; - if (n >= LJ_STACK_MAX) - n = LJ_STACK_MAX; - } - resizestack(L, n); - if (L->stacksize > LJ_STACK_MAXEX) - lj_err_msg(L, LJ_ERR_STKOV); -} - -void LJ_FASTCALL lj_state_growstack1(lua_State *L) -{ - lj_state_growstack(L, 1); -} - -/* Allocate basic stack for new state. */ -static void stack_init(lua_State *L1, lua_State *L) -{ - TValue *stend, *st = lj_mem_newvec(L, LJ_STACK_START+LJ_STACK_EXTRA, TValue); - setmref(L1->stack, st); - L1->stacksize = LJ_STACK_START + LJ_STACK_EXTRA; - stend = st + L1->stacksize; - setmref(L1->maxstack, stend - LJ_STACK_EXTRA - 1); - setthreadV(L1, st++, L1); /* Needed for curr_funcisL() on empty stack. */ - if (LJ_FR2) setnilV(st++); - L1->base = L1->top = st; - while (st < stend) /* Clear new slots. */ - setnilV(st++); -} - -/* -- State handling ------------------------------------------------------ */ - -/* Open parts that may cause memory-allocation errors. */ -static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud) -{ - global_State *g = G(L); - UNUSED(dummy); - UNUSED(ud); - stack_init(L, L); - /* NOBARRIER: State initialization, all objects are white. */ - setgcref(L->env, obj2gco(lj_tab_new(L, 0, LJ_MIN_GLOBAL))); - settabV(L, registry(L), lj_tab_new(L, 0, LJ_MIN_REGISTRY)); - lj_str_init(L); - lj_meta_init(L); - lj_lex_init(L); - fixstring(lj_err_str(L, LJ_ERR_ERRMEM)); /* Preallocate memory error msg. */ - g->gc.threshold = 4*g->gc.total; - lj_trace_initstate(g); - lj_err_verify(); - return NULL; -} - -static void close_state(lua_State *L) -{ - global_State *g = G(L); - lj_func_closeuv(L, tvref(L->stack)); - lj_gc_freeall(g); - lj_assertG(gcref(g->gc.root) == obj2gco(L), - "main thread is not first GC object"); - lj_assertG(g->str.num == 0, "leaked %d strings", g->str.num); - lj_trace_freestate(g); -#if LJ_HASFFI - lj_ctype_freestate(g); -#endif - lj_str_freetab(g); - lj_buf_free(g, &g->tmpbuf); - lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); -#if LJ_64 - if (mref(g->gc.lightudseg, uint32_t)) { - MSize segnum = g->gc.lightudnum ? (2 << lj_fls(g->gc.lightudnum)) : 2; - lj_mem_freevec(g, mref(g->gc.lightudseg, uint32_t), segnum, uint32_t); - } -#endif - lj_assertG(g->gc.total == sizeof(GG_State), - "memory leak of %lld bytes", - (long long)(g->gc.total - sizeof(GG_State))); -#ifndef LUAJIT_USE_SYSMALLOC - if (g->allocf == lj_alloc_f) - lj_alloc_destroy(g->allocd); - else -#endif - g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0); -} - -#if LJ_64 && !LJ_GC64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC)) -lua_State *lj_state_newstate(lua_Alloc allocf, void *allocd) -#else -LUA_API lua_State *lua_newstate(lua_Alloc allocf, void *allocd) -#endif -{ - PRNGState prng; - GG_State *GG; - lua_State *L; - global_State *g; - /* We need the PRNG for the memory allocator, so initialize this first. */ - if (!lj_prng_seed_secure(&prng)) { - lj_assertX(0, "secure PRNG seeding failed"); - /* Can only return NULL here, so this errors with "not enough memory". */ - return NULL; - } -#ifndef LUAJIT_USE_SYSMALLOC - if (allocf == LJ_ALLOCF_INTERNAL) { - allocd = lj_alloc_create(&prng); - if (!allocd) return NULL; - allocf = lj_alloc_f; - } -#endif - GG = (GG_State *)allocf(allocd, NULL, 0, sizeof(GG_State)); - if (GG == NULL || !checkptrGC(GG)) return NULL; - memset(GG, 0, sizeof(GG_State)); - L = &GG->L; - g = &GG->g; - L->gct = ~LJ_TTHREAD; - L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */ - L->dummy_ffid = FF_C; - setmref(L->glref, g); - g->gc.currentwhite = LJ_GC_WHITE0 | LJ_GC_FIXED; - g->strempty.marked = LJ_GC_WHITE0; - g->strempty.gct = ~LJ_TSTR; - g->allocf = allocf; - g->allocd = allocd; - g->prng = prng; -#ifndef LUAJIT_USE_SYSMALLOC - if (allocf == lj_alloc_f) { - lj_alloc_setprng(allocd, &g->prng); - } -#endif - setgcref(g->mainthref, obj2gco(L)); - setgcref(g->uvhead.prev, obj2gco(&g->uvhead)); - setgcref(g->uvhead.next, obj2gco(&g->uvhead)); - g->str.mask = ~(MSize)0; - setnilV(registry(L)); - setnilV(&g->nilnode.val); - setnilV(&g->nilnode.key); -#if !LJ_GC64 - setmref(g->nilnode.freetop, &g->nilnode); -#endif - lj_buf_init(NULL, &g->tmpbuf); - g->gc.state = GCSpause; - setgcref(g->gc.root, obj2gco(L)); - setmref(g->gc.sweep, &g->gc.root); - g->gc.total = sizeof(GG_State); - g->gc.pause = LUAI_GCPAUSE; - g->gc.stepmul = LUAI_GCMUL; - lj_dispatch_init((GG_State *)L); - L->status = LUA_ERRERR+1; /* Avoid touching the stack upon memory error. */ - if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) { - /* Memory allocation error: free partial state. */ - close_state(L); - return NULL; - } - L->status = LUA_OK; - return L; -} - -static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud) -{ - UNUSED(dummy); - UNUSED(ud); - lj_gc_finalize_cdata(L); - lj_gc_finalize_udata(L); - /* Frame pop omitted. */ - return NULL; -} - -LUA_API void lua_close(lua_State *L) -{ - global_State *g = G(L); - int i; - L = mainthread(g); /* Only the main thread can be closed. */ -#if LJ_HASPROFILE - luaJIT_profile_stop(L); -#endif - setgcrefnull(g->cur_L); - lj_func_closeuv(L, tvref(L->stack)); - lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */ -#if LJ_HASJIT - G2J(g)->flags &= ~JIT_F_ON; - G2J(g)->state = LJ_TRACE_IDLE; - lj_dispatch_update(g); -#endif - for (i = 0;;) { - hook_enter(g); - L->status = LUA_OK; - L->base = L->top = tvref(L->stack) + 1 + LJ_FR2; - L->cframe = NULL; - if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == LUA_OK) { - if (++i >= 10) break; - lj_gc_separateudata(g, 1); /* Separate udata again. */ - if (gcref(g->gc.mmudata) == NULL) /* Until nothing is left to do. */ - break; - } - } - close_state(L); -} - -lua_State *lj_state_new(lua_State *L) -{ - lua_State *L1 = lj_mem_newobj(L, lua_State); - L1->gct = ~LJ_TTHREAD; - L1->dummy_ffid = FF_C; - L1->status = LUA_OK; - L1->stacksize = 0; - setmref(L1->stack, NULL); - L1->cframe = NULL; - /* NOBARRIER: The lua_State is new (marked white). */ - setgcrefnull(L1->openupval); - setmrefr(L1->glref, L->glref); - setgcrefr(L1->env, L->env); - stack_init(L1, L); /* init stack */ - lj_assertL(iswhite(obj2gco(L1)), "new thread object is not white"); - return L1; -} - -void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L) -{ - lj_assertG(L != mainthread(g), "free of main thread"); - if (obj2gco(L) == gcref(g->cur_L)) - setgcrefnull(g->cur_L); - lj_func_closeuv(L, tvref(L->stack)); - lj_assertG(gcref(L->openupval) == NULL, "stale open upvalues"); - lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); - lj_mem_freet(g, L); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.h deleted file mode 100644 index d22b7a6..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_state.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -** State and stack handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STATE_H -#define _LJ_STATE_H - -#include "lj_obj.h" - -#define incr_top(L) \ - (++L->top >= tvref(L->maxstack) && (lj_state_growstack1(L), 0)) - -#define savestack(L, p) ((char *)(p) - mref(L->stack, char)) -#define restorestack(L, n) ((TValue *)(mref(L->stack, char) + (n))) - -LJ_FUNC void lj_state_relimitstack(lua_State *L); -LJ_FUNC void lj_state_shrinkstack(lua_State *L, MSize used); -LJ_FUNCA void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need); -LJ_FUNC void LJ_FASTCALL lj_state_growstack1(lua_State *L); - -static LJ_AINLINE void lj_state_checkstack(lua_State *L, MSize need) -{ - if ((mref(L->maxstack, char) - (char *)L->top) <= - (ptrdiff_t)need*(ptrdiff_t)sizeof(TValue)) - lj_state_growstack(L, need); -} - -LJ_FUNC lua_State *lj_state_new(lua_State *L); -LJ_FUNC void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L); -#if LJ_64 && !LJ_GC64 && !(defined(LUAJIT_USE_VALGRIND) && defined(LUAJIT_USE_SYSMALLOC)) -LJ_FUNC lua_State *lj_state_newstate(lua_Alloc f, void *ud); -#endif - -#define LJ_ALLOCF_INTERNAL ((lua_Alloc)(void *)(uintptr_t)(1237<<4)) - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.c deleted file mode 100644 index a5282da..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.c +++ /dev/null @@ -1,370 +0,0 @@ -/* -** String handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_str_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_str.h" -#include "lj_char.h" -#include "lj_prng.h" - -/* -- String helpers ------------------------------------------------------ */ - -/* Ordered compare of strings. Assumes string data is 4-byte aligned. */ -int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b) -{ - MSize i, n = a->len > b->len ? b->len : a->len; - for (i = 0; i < n; i += 4) { - /* Note: innocuous access up to end of string + 3. */ - uint32_t va = *(const uint32_t *)(strdata(a)+i); - uint32_t vb = *(const uint32_t *)(strdata(b)+i); - if (va != vb) { -#if LJ_LE - va = lj_bswap(va); vb = lj_bswap(vb); -#endif - i -= n; - if ((int32_t)i >= -3) { - va >>= 32+(i<<3); vb >>= 32+(i<<3); - if (va == vb) break; - } - return va < vb ? -1 : 1; - } - } - return (int32_t)(a->len - b->len); -} - -/* Find fixed string p inside string s. */ -const char *lj_str_find(const char *s, const char *p, MSize slen, MSize plen) -{ - if (plen <= slen) { - if (plen == 0) { - return s; - } else { - int c = *(const uint8_t *)p++; - plen--; slen -= plen; - while (slen) { - const char *q = (const char *)memchr(s, c, slen); - if (!q) break; - if (memcmp(q+1, p, plen) == 0) return q; - q++; slen -= (MSize)(q-s); s = q; - } - } - } - return NULL; -} - -/* Check whether a string has a pattern matching character. */ -int lj_str_haspattern(GCstr *s) -{ - const char *p = strdata(s), *q = p + s->len; - while (p < q) { - int c = *(const uint8_t *)p++; - if (lj_char_ispunct(c) && strchr("^$*+?.([%-", c)) - return 1; /* Found a pattern matching char. */ - } - return 0; /* No pattern matching chars found. */ -} - -/* -- String hashing ------------------------------------------------------ */ - -/* Keyed sparse ARX string hash. Constant time. */ -static StrHash hash_sparse(uint64_t seed, const char *str, MSize len) -{ - /* Constants taken from lookup3 hash by Bob Jenkins. */ - StrHash a, b, h = len ^ (StrHash)seed; - if (len >= 4) { /* Caveat: unaligned access! */ - a = lj_getu32(str); - h ^= lj_getu32(str+len-4); - b = lj_getu32(str+(len>>1)-2); - h ^= b; h -= lj_rol(b, 14); - b += lj_getu32(str+(len>>2)-1); - } else { - a = *(const uint8_t *)str; - h ^= *(const uint8_t *)(str+len-1); - b = *(const uint8_t *)(str+(len>>1)); - h ^= b; h -= lj_rol(b, 14); - } - a ^= h; a -= lj_rol(h, 11); - b ^= a; b -= lj_rol(a, 25); - h ^= b; h -= lj_rol(b, 16); - return h; -} - -#if LUAJIT_SECURITY_STRHASH -/* Keyed dense ARX string hash. Linear time. */ -static LJ_NOINLINE StrHash hash_dense(uint64_t seed, StrHash h, - const char *str, MSize len) -{ - StrHash b = lj_bswap(lj_rol(h ^ (StrHash)(seed >> 32), 4)); - if (len > 12) { - StrHash a = (StrHash)seed; - const char *pe = str+len-12, *p = pe, *q = str; - do { - a += lj_getu32(p); - b += lj_getu32(p+4); - h += lj_getu32(p+8); - p = q; q += 12; - h ^= b; h -= lj_rol(b, 14); - a ^= h; a -= lj_rol(h, 11); - b ^= a; b -= lj_rol(a, 25); - } while (p < pe); - h ^= b; h -= lj_rol(b, 16); - a ^= h; a -= lj_rol(h, 4); - b ^= a; b -= lj_rol(a, 14); - } - return b; -} -#endif - -/* -- String interning ---------------------------------------------------- */ - -#define LJ_STR_MAXCOLL 32 - -/* Resize the string interning hash table (grow and shrink). */ -void lj_str_resize(lua_State *L, MSize newmask) -{ - global_State *g = G(L); - GCRef *newtab, *oldtab = g->str.tab; - MSize i; - - /* No resizing during GC traversal or if already too big. */ - if (g->gc.state == GCSsweepstring || newmask >= LJ_MAX_STRTAB-1) - return; - - newtab = lj_mem_newvec(L, newmask+1, GCRef); - memset(newtab, 0, (newmask+1)*sizeof(GCRef)); - -#if LUAJIT_SECURITY_STRHASH - /* Check which chains need secondary hashes. */ - if (g->str.second) { - int newsecond = 0; - /* Compute primary chain lengths. */ - for (i = g->str.mask; i != ~(MSize)0; i--) { - GCobj *o = (GCobj *)(gcrefu(oldtab[i]) & ~(uintptr_t)1); - while (o) { - GCstr *s = gco2str(o); - MSize hash = s->hashalg ? hash_sparse(g->str.seed, strdata(s), s->len) : - s->hash; - hash &= newmask; - setgcrefp(newtab[hash], gcrefu(newtab[hash]) + 1); - o = gcnext(o); - } - } - /* Mark secondary chains. */ - for (i = newmask; i != ~(MSize)0; i--) { - int secondary = gcrefu(newtab[i]) > LJ_STR_MAXCOLL; - newsecond |= secondary; - setgcrefp(newtab[i], secondary); - } - g->str.second = newsecond; - } -#endif - - /* Reinsert all strings from the old table into the new table. */ - for (i = g->str.mask; i != ~(MSize)0; i--) { - GCobj *o = (GCobj *)(gcrefu(oldtab[i]) & ~(uintptr_t)1); - while (o) { - GCobj *next = gcnext(o); - GCstr *s = gco2str(o); - MSize hash = s->hash; -#if LUAJIT_SECURITY_STRHASH - uintptr_t u; - if (LJ_LIKELY(!s->hashalg)) { /* String hashed with primary hash. */ - hash &= newmask; - u = gcrefu(newtab[hash]); - if (LJ_UNLIKELY(u & 1)) { /* Switch string to secondary hash. */ - s->hash = hash = hash_dense(g->str.seed, s->hash, strdata(s), s->len); - s->hashalg = 1; - hash &= newmask; - u = gcrefu(newtab[hash]); - } - } else { /* String hashed with secondary hash. */ - MSize shash = hash_sparse(g->str.seed, strdata(s), s->len); - u = gcrefu(newtab[shash & newmask]); - if (u & 1) { - hash &= newmask; - u = gcrefu(newtab[hash]); - } else { /* Revert string back to primary hash. */ - s->hash = shash; - s->hashalg = 0; - hash = (shash & newmask); - } - } - /* NOBARRIER: The string table is a GC root. */ - setgcrefp(o->gch.nextgc, (u & ~(uintptr_t)1)); - setgcrefp(newtab[hash], ((uintptr_t)o | (u & 1))); -#else - hash &= newmask; - /* NOBARRIER: The string table is a GC root. */ - setgcrefr(o->gch.nextgc, newtab[hash]); - setgcref(newtab[hash], o); -#endif - o = next; - } - } - - /* Free old table and replace with new table. */ - lj_str_freetab(g); - g->str.tab = newtab; - g->str.mask = newmask; -} - -#if LUAJIT_SECURITY_STRHASH -/* Rehash and rechain all strings in a chain. */ -static LJ_NOINLINE GCstr *lj_str_rehash_chain(lua_State *L, StrHash hashc, - const char *str, MSize len) -{ - global_State *g = G(L); - int ow = g->gc.state == GCSsweepstring ? otherwhite(g) : 0; /* Sweeping? */ - GCRef *strtab = g->str.tab; - MSize strmask = g->str.mask; - GCobj *o = gcref(strtab[hashc & strmask]); - setgcrefp(strtab[hashc & strmask], (void *)((uintptr_t)1)); - g->str.second = 1; - while (o) { - uintptr_t u; - GCobj *next = gcnext(o); - GCstr *s = gco2str(o); - StrHash hash; - if (ow) { /* Must sweep while rechaining. */ - if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* String alive? */ - lj_assertG(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED), - "sweep of undead string"); - makewhite(g, o); - } else { /* Free dead string. */ - lj_assertG(isdead(g, o) || ow == LJ_GC_SFIXED, - "sweep of unlive string"); - lj_str_free(g, s); - o = next; - continue; - } - } - hash = s->hash; - if (!s->hashalg) { /* Rehash with secondary hash. */ - hash = hash_dense(g->str.seed, hash, strdata(s), s->len); - s->hash = hash; - s->hashalg = 1; - } - /* Rechain. */ - hash &= strmask; - u = gcrefu(strtab[hash]); - setgcrefp(o->gch.nextgc, (u & ~(uintptr_t)1)); - setgcrefp(strtab[hash], ((uintptr_t)o | (u & 1))); - o = next; - } - /* Try to insert the pending string again. */ - return lj_str_new(L, str, len); -} -#endif - -/* Reseed String ID from PRNG after random interval < 2^bits. */ -#if LUAJIT_SECURITY_STRID == 1 -#define STRID_RESEED_INTERVAL 8 -#elif LUAJIT_SECURITY_STRID == 2 -#define STRID_RESEED_INTERVAL 4 -#elif LUAJIT_SECURITY_STRID >= 3 -#define STRID_RESEED_INTERVAL 0 -#endif - -/* Allocate a new string and add to string interning table. */ -static GCstr *lj_str_alloc(lua_State *L, const char *str, MSize len, - StrHash hash, int hashalg) -{ - GCstr *s = lj_mem_newt(L, lj_str_size(len), GCstr); - global_State *g = G(L); - uintptr_t u; - newwhite(g, s); - s->gct = ~LJ_TSTR; - s->len = len; - s->hash = hash; -#ifndef STRID_RESEED_INTERVAL - s->sid = g->str.id++; -#elif STRID_RESEED_INTERVAL - if (!g->str.idreseed--) { - uint64_t r = lj_prng_u64(&g->prng); - g->str.id = (StrID)r; - g->str.idreseed = (uint8_t)(r >> (64 - STRID_RESEED_INTERVAL)); - } - s->sid = g->str.id++; -#else - s->sid = (StrID)lj_prng_u64(&g->prng); -#endif - s->reserved = 0; - s->hashalg = (uint8_t)hashalg; - /* Clear last 4 bytes of allocated memory. Implies zero-termination, too. */ - *(uint32_t *)(strdatawr(s)+(len & ~(MSize)3)) = 0; - memcpy(strdatawr(s), str, len); - /* Add to string hash table. */ - hash &= g->str.mask; - u = gcrefu(g->str.tab[hash]); - setgcrefp(s->nextgc, (u & ~(uintptr_t)1)); - /* NOBARRIER: The string table is a GC root. */ - setgcrefp(g->str.tab[hash], ((uintptr_t)s | (u & 1))); - if (g->str.num++ > g->str.mask) /* Allow a 100% load factor. */ - lj_str_resize(L, (g->str.mask<<1)+1); /* Grow string table. */ - return s; /* Return newly interned string. */ -} - -/* Intern a string and return string object. */ -GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx) -{ - global_State *g = G(L); - if (lenx-1 < LJ_MAX_STR-1) { - MSize len = (MSize)lenx; - StrHash hash = hash_sparse(g->str.seed, str, len); - MSize coll = 0; - int hashalg = 0; - /* Check if the string has already been interned. */ - GCobj *o = gcref(g->str.tab[hash & g->str.mask]); -#if LUAJIT_SECURITY_STRHASH - if (LJ_UNLIKELY((uintptr_t)o & 1)) { /* Secondary hash for this chain? */ - hashalg = 1; - hash = hash_dense(g->str.seed, hash, str, len); - o = (GCobj *)(gcrefu(g->str.tab[hash & g->str.mask]) & ~(uintptr_t)1); - } -#endif - while (o != NULL) { - GCstr *sx = gco2str(o); - if (sx->hash == hash && sx->len == len) { - if (memcmp(str, strdata(sx), len) == 0) { - if (isdead(g, o)) flipwhite(o); /* Resurrect if dead. */ - return sx; /* Return existing string. */ - } - coll++; - } - coll++; - o = gcnext(o); - } -#if LUAJIT_SECURITY_STRHASH - /* Rehash chain if there are too many collisions. */ - if (LJ_UNLIKELY(coll > LJ_STR_MAXCOLL) && !hashalg) { - return lj_str_rehash_chain(L, hash, str, len); - } -#endif - /* Otherwise allocate a new string. */ - return lj_str_alloc(L, str, len, hash, hashalg); - } else { - if (lenx) - lj_err_msg(L, LJ_ERR_STROV); - return &g->strempty; - } -} - -void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s) -{ - g->str.num--; - lj_mem_free(g, s, lj_str_size(s->len)); -} - -void LJ_FASTCALL lj_str_init(lua_State *L) -{ - global_State *g = G(L); - g->str.seed = lj_prng_u64(&g->prng); - lj_str_resize(L, LJ_MIN_STRTAB-1); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.h deleted file mode 100644 index 28edb5a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_str.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -** String handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STR_H -#define _LJ_STR_H - -#include - -#include "lj_obj.h" - -/* String helpers. */ -LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b); -LJ_FUNC const char *lj_str_find(const char *s, const char *f, - MSize slen, MSize flen); -LJ_FUNC int lj_str_haspattern(GCstr *s); - -/* String interning. */ -LJ_FUNC void lj_str_resize(lua_State *L, MSize newmask); -LJ_FUNCA GCstr *lj_str_new(lua_State *L, const char *str, size_t len); -LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s); -LJ_FUNC void LJ_FASTCALL lj_str_init(lua_State *L); -#define lj_str_freetab(g) \ - (lj_mem_freevec(g, g->str.tab, g->str.mask+1, GCRef)) - -#define lj_str_newz(L, s) (lj_str_new(L, s, strlen(s))) -#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) -#define lj_str_size(len) (sizeof(GCstr) + (((len)+4) & ~(MSize)3)) - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.c deleted file mode 100644 index 5c80829..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.c +++ /dev/null @@ -1,606 +0,0 @@ -/* -** String formatting. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lj_strfmt_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_err.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_meta.h" -#include "lj_state.h" -#include "lj_char.h" -#include "lj_strfmt.h" -#if LJ_HASFFI -#include "lj_ctype.h" -#endif -#include "lj_lib.h" - -/* -- Format parser ------------------------------------------------------- */ - -static const uint8_t strfmt_map[('x'-'A')+1] = { - STRFMT_A,0,0,0,STRFMT_E,STRFMT_F,STRFMT_G,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,STRFMT_X,0,0, - 0,0,0,0,0,0, - STRFMT_A,0,STRFMT_C,STRFMT_D,STRFMT_E,STRFMT_F,STRFMT_G,0,STRFMT_I,0,0,0,0, - 0,STRFMT_O,STRFMT_P,STRFMT_Q,0,STRFMT_S,0,STRFMT_U,0,0,STRFMT_X -}; - -SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs) -{ - const uint8_t *p = fs->p, *e = fs->e; - fs->str = (const char *)p; - for (; p < e; p++) { - if (*p == '%') { /* Escape char? */ - if (p[1] == '%') { /* '%%'? */ - fs->p = ++p+1; - goto retlit; - } else { - SFormat sf = 0; - uint32_t c; - if (p != (const uint8_t *)fs->str) - break; - for (p++; (uint32_t)*p - ' ' <= (uint32_t)('0' - ' '); p++) { - /* Parse flags. */ - if (*p == '-') sf |= STRFMT_F_LEFT; - else if (*p == '+') sf |= STRFMT_F_PLUS; - else if (*p == '0') sf |= STRFMT_F_ZERO; - else if (*p == ' ') sf |= STRFMT_F_SPACE; - else if (*p == '#') sf |= STRFMT_F_ALT; - else break; - } - if ((uint32_t)*p - '0' < 10) { /* Parse width. */ - uint32_t width = (uint32_t)*p++ - '0'; - if ((uint32_t)*p - '0' < 10) - width = (uint32_t)*p++ - '0' + width*10; - sf |= (width << STRFMT_SH_WIDTH); - } - if (*p == '.') { /* Parse precision. */ - uint32_t prec = 0; - p++; - if ((uint32_t)*p - '0' < 10) { - prec = (uint32_t)*p++ - '0'; - if ((uint32_t)*p - '0' < 10) - prec = (uint32_t)*p++ - '0' + prec*10; - } - sf |= ((prec+1) << STRFMT_SH_PREC); - } - /* Parse conversion. */ - c = (uint32_t)*p - 'A'; - if (LJ_LIKELY(c <= (uint32_t)('x' - 'A'))) { - uint32_t sx = strfmt_map[c]; - if (sx) { - fs->p = p+1; - return (sf | sx | ((c & 0x20) ? 0 : STRFMT_F_UPPER)); - } - } - /* Return error location. */ - if (*p >= 32) p++; - fs->len = (MSize)(p - (const uint8_t *)fs->str); - fs->p = fs->e; - return STRFMT_ERR; - } - } - } - fs->p = p; -retlit: - fs->len = (MSize)(p - (const uint8_t *)fs->str); - return fs->len ? STRFMT_LIT : STRFMT_EOF; -} - -/* -- Raw conversions ----------------------------------------------------- */ - -#define WINT_R(x, sh, sc) \ - { uint32_t d = (x*(((1<>sh; x -= d*sc; *p++ = (char)('0'+d); } - -/* Write integer to buffer. */ -char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k) -{ - uint32_t u = (uint32_t)k; - if (k < 0) { u = (uint32_t)-k; *p++ = '-'; } - if (u < 10000) { - if (u < 10) goto dig1; - if (u < 100) goto dig2; - if (u < 1000) goto dig3; - } else { - uint32_t v = u / 10000; u -= v * 10000; - if (v < 10000) { - if (v < 10) goto dig5; - if (v < 100) goto dig6; - if (v < 1000) goto dig7; - } else { - uint32_t w = v / 10000; v -= w * 10000; - if (w >= 10) WINT_R(w, 10, 10) - *p++ = (char)('0'+w); - } - WINT_R(v, 23, 1000) - dig7: WINT_R(v, 12, 100) - dig6: WINT_R(v, 10, 10) - dig5: *p++ = (char)('0'+v); - } - WINT_R(u, 23, 1000) - dig3: WINT_R(u, 12, 100) - dig2: WINT_R(u, 10, 10) - dig1: *p++ = (char)('0'+u); - return p; -} -#undef WINT_R - -/* Write pointer to buffer. */ -char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v) -{ - ptrdiff_t x = (ptrdiff_t)v; - MSize i, n = STRFMT_MAXBUF_PTR; - if (x == 0) { - *p++ = 'N'; *p++ = 'U'; *p++ = 'L'; *p++ = 'L'; - return p; - } -#if LJ_64 - /* Shorten output for 64 bit pointers. */ - n = 2+2*4+((x >> 32) ? 2+2*(lj_fls((uint32_t)(x >> 32))>>3) : 0); -#endif - p[0] = '0'; - p[1] = 'x'; - for (i = n-1; i >= 2; i--, x >>= 4) - p[i] = "0123456789abcdef"[(x & 15)]; - return p+n; -} - -/* Write ULEB128 to buffer. */ -char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v) -{ - for (; v >= 0x80; v >>= 7) - *p++ = (char)((v & 0x7f) | 0x80); - *p++ = (char)v; - return p; -} - -/* Return string or write number to tmp buffer and return pointer to start. */ -const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp) -{ - SBuf *sb; - if (tvisstr(o)) { - *lenp = strV(o)->len; - return strVdata(o); - } else if (tvisbuf(o)) { - SBufExt *sbx = bufV(o); - *lenp = sbufxlen(sbx); - return sbx->r; - } else if (tvisint(o)) { - sb = lj_strfmt_putint(lj_buf_tmp_(L), intV(o)); - } else if (tvisnum(o)) { - sb = lj_strfmt_putfnum(lj_buf_tmp_(L), STRFMT_G14, o->n); - } else { - return NULL; - } - *lenp = sbuflen(sb); - return sb->b; -} - -/* -- Unformatted conversions to buffer ----------------------------------- */ - -/* Add integer to buffer. */ -SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k) -{ - sb->w = lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k); - return sb; -} - -#if LJ_HASJIT -/* Add number to buffer. */ -SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o) -{ - return lj_strfmt_putfnum(sb, STRFMT_G14, o->n); -} -#endif - -SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v) -{ - sb->w = lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), v); - return sb; -} - -/* Add quoted string to buffer. */ -static SBuf *strfmt_putquotedlen(SBuf *sb, const char *s, MSize len) -{ - lj_buf_putb(sb, '"'); - while (len--) { - uint32_t c = (uint32_t)(uint8_t)*s++; - char *w = lj_buf_more(sb, 4); - if (c == '"' || c == '\\' || c == '\n') { - *w++ = '\\'; - } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */ - uint32_t d; - *w++ = '\\'; - if (c >= 100 || lj_char_isdigit((uint8_t)*s)) { - *w++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100; - goto tens; - } else if (c >= 10) { - tens: - d = (c * 205) >> 11; c -= d * 10; *w++ = (char)('0'+d); - } - c += '0'; - } - *w++ = (char)c; - sb->w = w; - } - lj_buf_putb(sb, '"'); - return sb; -} - -#if LJ_HASJIT -SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str) -{ - return strfmt_putquotedlen(sb, strdata(str), str->len); -} -#endif - -/* -- Formatted conversions to buffer ------------------------------------- */ - -/* Add formatted char to buffer. */ -SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat sf, int32_t c) -{ - MSize width = STRFMT_WIDTH(sf); - char *w = lj_buf_more(sb, width > 1 ? width : 1); - if ((sf & STRFMT_F_LEFT)) *w++ = (char)c; - while (width-- > 1) *w++ = ' '; - if (!(sf & STRFMT_F_LEFT)) *w++ = (char)c; - sb->w = w; - return sb; -} - -/* Add formatted string to buffer. */ -static SBuf *strfmt_putfstrlen(SBuf *sb, SFormat sf, const char *s, MSize len) -{ - MSize width = STRFMT_WIDTH(sf); - char *w; - if (len > STRFMT_PREC(sf)) len = STRFMT_PREC(sf); - w = lj_buf_more(sb, width > len ? width : len); - if ((sf & STRFMT_F_LEFT)) w = lj_buf_wmem(w, s, len); - while (width-- > len) *w++ = ' '; - if (!(sf & STRFMT_F_LEFT)) w = lj_buf_wmem(w, s, len); - sb->w = w; - return sb; -} - -#if LJ_HASJIT -SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str) -{ - return strfmt_putfstrlen(sb, sf, strdata(str), str->len); -} -#endif - -/* Add formatted signed/unsigned integer to buffer. */ -SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k) -{ - char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *w; -#ifdef LUA_USE_ASSERT - char *ws; -#endif - MSize prefix = 0, len, prec, pprec, width, need; - - /* Figure out signed prefixes. */ - if (STRFMT_TYPE(sf) == STRFMT_INT) { - if ((int64_t)k < 0) { - k = (uint64_t)-(int64_t)k; - prefix = 256 + '-'; - } else if ((sf & STRFMT_F_PLUS)) { - prefix = 256 + '+'; - } else if ((sf & STRFMT_F_SPACE)) { - prefix = 256 + ' '; - } - } - - /* Convert number and store to fixed-size buffer in reverse order. */ - prec = STRFMT_PREC(sf); - if ((int32_t)prec >= 0) sf &= ~STRFMT_F_ZERO; - if (k == 0) { /* Special-case zero argument. */ - if (prec != 0 || - (sf & (STRFMT_T_OCT|STRFMT_F_ALT)) == (STRFMT_T_OCT|STRFMT_F_ALT)) - *--q = '0'; - } else if (!(sf & (STRFMT_T_HEX|STRFMT_T_OCT))) { /* Decimal. */ - uint32_t k2; - while ((k >> 32)) { *--q = (char)('0' + k % 10); k /= 10; } - k2 = (uint32_t)k; - do { *--q = (char)('0' + k2 % 10); k2 /= 10; } while (k2); - } else if ((sf & STRFMT_T_HEX)) { /* Hex. */ - const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEF" : - "0123456789abcdef"; - do { *--q = hexdig[(k & 15)]; k >>= 4; } while (k); - if ((sf & STRFMT_F_ALT)) prefix = 512 + ((sf & STRFMT_F_UPPER) ? 'X' : 'x'); - } else { /* Octal. */ - do { *--q = (char)('0' + (uint32_t)(k & 7)); k >>= 3; } while (k); - if ((sf & STRFMT_F_ALT)) *--q = '0'; - } - - /* Calculate sizes. */ - len = (MSize)(buf + sizeof(buf) - q); - if ((int32_t)len >= (int32_t)prec) prec = len; - width = STRFMT_WIDTH(sf); - pprec = prec + (prefix >> 8); - need = width > pprec ? width : pprec; - w = lj_buf_more(sb, need); -#ifdef LUA_USE_ASSERT - ws = w; -#endif - - /* Format number with leading/trailing whitespace and zeros. */ - if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == 0) - while (width-- > pprec) *w++ = ' '; - if (prefix) { - if ((char)prefix >= 'X') *w++ = '0'; - *w++ = (char)prefix; - } - if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == STRFMT_F_ZERO) - while (width-- > pprec) *w++ = '0'; - while (prec-- > len) *w++ = '0'; - while (q < buf + sizeof(buf)) *w++ = *q++; /* Add number itself. */ - if ((sf & STRFMT_F_LEFT)) - while (width-- > pprec) *w++ = ' '; - - lj_assertX(need == (MSize)(w - ws), "miscalculated format size"); - sb->w = w; - return sb; -} - -/* Add number formatted as signed integer to buffer. */ -SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n) -{ - int64_t k = (int64_t)n; - if (checki32(k) && sf == STRFMT_INT) - return lj_strfmt_putint(sb, (int32_t)k); /* Shortcut for plain %d. */ - else - return lj_strfmt_putfxint(sb, sf, (uint64_t)k); -} - -/* Add number formatted as unsigned integer to buffer. */ -SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n) -{ - int64_t k; - if (n >= 9223372036854775808.0) - k = (int64_t)(n - 18446744073709551616.0); - else - k = (int64_t)n; - return lj_strfmt_putfxint(sb, sf, (uint64_t)k); -} - -/* Format stack arguments to buffer. */ -int lj_strfmt_putarg(lua_State *L, SBuf *sb, int arg, int retry) -{ - int narg = (int)(L->top - L->base); - GCstr *fmt = lj_lib_checkstr(L, arg); - FormatState fs; - SFormat sf; - lj_strfmt_init(&fs, strdata(fmt), fmt->len); - while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) { - if (sf == STRFMT_LIT) { - lj_buf_putmem(sb, fs.str, fs.len); - } else if (sf == STRFMT_ERR) { - lj_err_callerv(L, LJ_ERR_STRFMT, - strdata(lj_str_new(L, fs.str, fs.len))); - } else { - TValue *o = &L->base[arg++]; - if (arg > narg) - lj_err_arg(L, arg, LJ_ERR_NOVAL); - switch (STRFMT_TYPE(sf)) { - case STRFMT_INT: - if (tvisint(o)) { - int32_t k = intV(o); - if (sf == STRFMT_INT) - lj_strfmt_putint(sb, k); /* Shortcut for plain %d. */ - else - lj_strfmt_putfxint(sb, sf, k); - break; - } -#if LJ_HASFFI - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - if (cd->ctypeid == CTID_INT64 || cd->ctypeid == CTID_UINT64) { - lj_strfmt_putfxint(sb, sf, *(uint64_t *)cdataptr(cd)); - break; - } - } -#endif - lj_strfmt_putfnum_int(sb, sf, lj_lib_checknum(L, arg)); - break; - case STRFMT_UINT: - if (tvisint(o)) { - lj_strfmt_putfxint(sb, sf, intV(o)); - break; - } -#if LJ_HASFFI - if (tviscdata(o)) { - GCcdata *cd = cdataV(o); - if (cd->ctypeid == CTID_INT64 || cd->ctypeid == CTID_UINT64) { - lj_strfmt_putfxint(sb, sf, *(uint64_t *)cdataptr(cd)); - break; - } - } -#endif - lj_strfmt_putfnum_uint(sb, sf, lj_lib_checknum(L, arg)); - break; - case STRFMT_NUM: - lj_strfmt_putfnum(sb, sf, lj_lib_checknum(L, arg)); - break; - case STRFMT_STR: { - MSize len; - const char *s; - cTValue *mo; - if (LJ_UNLIKELY(!tvisstr(o) && !tvisbuf(o)) && retry >= 0 && - !tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { - /* Call __tostring metamethod once. */ - copyTV(L, L->top++, mo); - copyTV(L, L->top++, o); - lua_call(L, 1, 1); - o = &L->base[arg-1]; /* Stack may have been reallocated. */ - copyTV(L, o, --L->top); /* Replace inline for retry. */ - if (retry < 2) { /* Global buffer may have been overwritten. */ - retry = 1; - break; - } - } - if (LJ_LIKELY(tvisstr(o))) { - len = strV(o)->len; - s = strVdata(o); -#if LJ_HASBUFFER - } else if (tvisbuf(o)) { - SBufExt *sbx = bufV(o); - if (sbx == (SBufExt *)sb) lj_err_arg(L, arg+1, LJ_ERR_BUFFER_SELF); - len = sbufxlen(sbx); - s = sbx->r; -#endif - } else { - GCstr *str = lj_strfmt_obj(L, o); - len = str->len; - s = strdata(str); - } - if ((sf & STRFMT_T_QUOTED)) - strfmt_putquotedlen(sb, s, len); /* No formatting. */ - else - strfmt_putfstrlen(sb, sf, s, len); - break; - } - case STRFMT_CHAR: - lj_strfmt_putfchar(sb, sf, lj_lib_checkint(L, arg)); - break; - case STRFMT_PTR: /* No formatting. */ - lj_strfmt_putptr(sb, lj_obj_ptr(G(L), o)); - break; - default: - lj_assertL(0, "bad string format type"); - break; - } - } - } - return retry; -} - -/* -- Conversions to strings ---------------------------------------------- */ - -/* Convert integer to string. */ -GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k) -{ - char buf[STRFMT_MAXBUF_INT]; - MSize len = (MSize)(lj_strfmt_wint(buf, k) - buf); - return lj_str_new(L, buf, len); -} - -/* Convert integer or number to string. */ -GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o) -{ - return tvisint(o) ? lj_strfmt_int(L, intV(o)) : lj_strfmt_num(L, o); -} - -#if LJ_HASJIT -/* Convert char value to string. */ -GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c) -{ - char buf[1]; - buf[0] = c; - return lj_str_new(L, buf, 1); -} -#endif - -/* Raw conversion of object to string. */ -GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o) -{ - if (tvisstr(o)) { - return strV(o); - } else if (tvisnumber(o)) { - return lj_strfmt_number(L, o); - } else if (tvisnil(o)) { - return lj_str_newlit(L, "nil"); - } else if (tvisfalse(o)) { - return lj_str_newlit(L, "false"); - } else if (tvistrue(o)) { - return lj_str_newlit(L, "true"); - } else { - char buf[8+2+2+16], *p = buf; - p = lj_buf_wmem(p, lj_typename(o), (MSize)strlen(lj_typename(o))); - *p++ = ':'; *p++ = ' '; - if (tvisfunc(o) && isffunc(funcV(o))) { - p = lj_buf_wmem(p, "builtin#", 8); - p = lj_strfmt_wint(p, funcV(o)->c.ffid); - } else { - p = lj_strfmt_wptr(p, lj_obj_ptr(G(L), o)); - } - return lj_str_new(L, buf, (size_t)(p - buf)); - } -} - -/* -- Internal string formatting ------------------------------------------ */ - -/* -** These functions are only used for lua_pushfstring(), lua_pushvfstring() -** and for internal string formatting (e.g. error messages). Caveat: unlike -** string.format(), only a limited subset of formats and flags are supported! -** -** LuaJIT has support for a couple more formats than Lua 5.1/5.2: -** - %d %u %o %x with full formatting, 32 bit integers only. -** - %f and other FP formats are really %.14g. -** - %s %c %p without formatting. -*/ - -/* Push formatted message as a string object to Lua stack. va_list variant. */ -const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp) -{ - SBuf *sb = lj_buf_tmp_(L); - FormatState fs; - SFormat sf; - GCstr *str; - lj_strfmt_init(&fs, fmt, (MSize)strlen(fmt)); - while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) { - switch (STRFMT_TYPE(sf)) { - case STRFMT_LIT: - lj_buf_putmem(sb, fs.str, fs.len); - break; - case STRFMT_INT: - lj_strfmt_putfxint(sb, sf, va_arg(argp, int32_t)); - break; - case STRFMT_UINT: - lj_strfmt_putfxint(sb, sf, va_arg(argp, uint32_t)); - break; - case STRFMT_NUM: - lj_strfmt_putfnum(sb, STRFMT_G14, va_arg(argp, lua_Number)); - break; - case STRFMT_STR: { - const char *s = va_arg(argp, char *); - if (s == NULL) s = "(null)"; - lj_buf_putmem(sb, s, (MSize)strlen(s)); - break; - } - case STRFMT_CHAR: - lj_buf_putb(sb, va_arg(argp, int)); - break; - case STRFMT_PTR: - lj_strfmt_putptr(sb, va_arg(argp, void *)); - break; - case STRFMT_ERR: - default: - lj_buf_putb(sb, '?'); - lj_assertL(0, "bad string format near offset %d", fs.len); - break; - } - } - str = lj_buf_str(L, sb); - setstrV(L, L->top, str); - incr_top(L); - return strdata(str); -} - -/* Push formatted message as a string object to Lua stack. Vararg variant. */ -const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...) -{ - const char *msg; - va_list argp; - va_start(argp, fmt); - msg = lj_strfmt_pushvf(L, fmt, argp); - va_end(argp); - return msg; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.h deleted file mode 100644 index a452960..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt.h +++ /dev/null @@ -1,131 +0,0 @@ -/* -** String formatting. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STRFMT_H -#define _LJ_STRFMT_H - -#include "lj_obj.h" - -typedef uint32_t SFormat; /* Format indicator. */ - -/* Format parser state. */ -typedef struct FormatState { - const uint8_t *p; /* Current format string pointer. */ - const uint8_t *e; /* End of format string. */ - const char *str; /* Returned literal string. */ - MSize len; /* Size of literal string. */ -} FormatState; - -/* Format types (max. 16). */ -typedef enum FormatType { - STRFMT_EOF, STRFMT_ERR, STRFMT_LIT, - STRFMT_INT, STRFMT_UINT, STRFMT_NUM, STRFMT_STR, STRFMT_CHAR, STRFMT_PTR -} FormatType; - -/* Format subtypes (bits are reused). */ -#define STRFMT_T_HEX 0x0010 /* STRFMT_UINT */ -#define STRFMT_T_OCT 0x0020 /* STRFMT_UINT */ -#define STRFMT_T_FP_A 0x0000 /* STRFMT_NUM */ -#define STRFMT_T_FP_E 0x0010 /* STRFMT_NUM */ -#define STRFMT_T_FP_F 0x0020 /* STRFMT_NUM */ -#define STRFMT_T_FP_G 0x0030 /* STRFMT_NUM */ -#define STRFMT_T_QUOTED 0x0010 /* STRFMT_STR */ - -/* Format flags. */ -#define STRFMT_F_LEFT 0x0100 -#define STRFMT_F_PLUS 0x0200 -#define STRFMT_F_ZERO 0x0400 -#define STRFMT_F_SPACE 0x0800 -#define STRFMT_F_ALT 0x1000 -#define STRFMT_F_UPPER 0x2000 - -/* Format indicator fields. */ -#define STRFMT_SH_WIDTH 16 -#define STRFMT_SH_PREC 24 - -#define STRFMT_TYPE(sf) ((FormatType)((sf) & 15)) -#define STRFMT_WIDTH(sf) (((sf) >> STRFMT_SH_WIDTH) & 255u) -#define STRFMT_PREC(sf) ((((sf) >> STRFMT_SH_PREC) & 255u) - 1u) -#define STRFMT_FP(sf) (((sf) >> 4) & 3) - -/* Formats for conversion characters. */ -#define STRFMT_A (STRFMT_NUM|STRFMT_T_FP_A) -#define STRFMT_C (STRFMT_CHAR) -#define STRFMT_D (STRFMT_INT) -#define STRFMT_E (STRFMT_NUM|STRFMT_T_FP_E) -#define STRFMT_F (STRFMT_NUM|STRFMT_T_FP_F) -#define STRFMT_G (STRFMT_NUM|STRFMT_T_FP_G) -#define STRFMT_I STRFMT_D -#define STRFMT_O (STRFMT_UINT|STRFMT_T_OCT) -#define STRFMT_P (STRFMT_PTR) -#define STRFMT_Q (STRFMT_STR|STRFMT_T_QUOTED) -#define STRFMT_S (STRFMT_STR) -#define STRFMT_U (STRFMT_UINT) -#define STRFMT_X (STRFMT_UINT|STRFMT_T_HEX) -#define STRFMT_G14 (STRFMT_G | ((14+1) << STRFMT_SH_PREC)) - -/* Maximum buffer sizes for conversions. */ -#define STRFMT_MAXBUF_XINT (1+22) /* '0' prefix + uint64_t in octal. */ -#define STRFMT_MAXBUF_INT (1+10) /* Sign + int32_t in decimal. */ -#define STRFMT_MAXBUF_NUM 32 /* Must correspond with STRFMT_G14. */ -#define STRFMT_MAXBUF_PTR (2+2*sizeof(ptrdiff_t)) /* "0x" + hex ptr. */ - -/* Format parser. */ -LJ_FUNC SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs); - -static LJ_AINLINE void lj_strfmt_init(FormatState *fs, const char *p, MSize len) -{ - fs->p = (const uint8_t *)p; - fs->e = (const uint8_t *)p + len; - /* Must be NUL-terminated. May have NULs inside, too. */ - lj_assertX(*fs->e == 0, "format not NUL-terminated"); -} - -/* Raw conversions. */ -LJ_FUNC char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k); -LJ_FUNC char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v); -LJ_FUNC char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v); -LJ_FUNC const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp); - -/* Unformatted conversions to buffer. */ -LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k); -#if LJ_HASJIT -LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o); -#endif -LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v); -#if LJ_HASJIT -LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str); -#endif - -/* Formatted conversions to buffer. */ -LJ_FUNC SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k); -LJ_FUNC SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n); -LJ_FUNC SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n); -LJ_FUNC SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat, lua_Number n); -LJ_FUNC SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat, int32_t c); -#if LJ_HASJIT -LJ_FUNC SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat, GCstr *str); -#endif -LJ_FUNC int lj_strfmt_putarg(lua_State *L, SBuf *sb, int arg, int retry); - -/* Conversions to strings. */ -LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k); -LJ_FUNCA GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o); -LJ_FUNCA GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o); -#if LJ_HASJIT -LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c); -#endif -LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o); - -/* Internal string formatting. */ -LJ_FUNC const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, - va_list argp); -LJ_FUNC const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...) -#if defined(__GNUC__) || defined(__clang__) - __attribute__ ((format (printf, 2, 3))) -#endif - ; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt_num.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt_num.c deleted file mode 100644 index 3c60695..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strfmt_num.c +++ /dev/null @@ -1,592 +0,0 @@ -/* -** String formatting for floating-point numbers. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** Contributed by Peter Cawley. -*/ - -#include - -#define lj_strfmt_num_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_buf.h" -#include "lj_str.h" -#include "lj_strfmt.h" - -/* -- Precomputed tables -------------------------------------------------- */ - -/* Rescale factors to push the exponent of a number towards zero. */ -#define RESCALE_EXPONENTS(P, N) \ - P(308), P(289), P(270), P(250), P(231), P(212), P(193), P(173), P(154), \ - P(135), P(115), P(96), P(77), P(58), P(38), P(0), P(0), P(0), N(39), N(58), \ - N(77), N(96), N(116), N(135), N(154), N(174), N(193), N(212), N(231), \ - N(251), N(270), N(289) - -#define ONE_E_P(X) 1e+0 ## X -#define ONE_E_N(X) 1e-0 ## X -static const int16_t rescale_e[] = { RESCALE_EXPONENTS(-, +) }; -static const double rescale_n[] = { RESCALE_EXPONENTS(ONE_E_P, ONE_E_N) }; -#undef ONE_E_N -#undef ONE_E_P - -/* -** For p in range -70 through 57, this table encodes pairs (m, e) such that -** 4*2^p <= (uint8_t)m*10^e, and is the smallest value for which this holds. -*/ -static const int8_t four_ulp_m_e[] = { - 34, -21, 68, -21, 14, -20, 28, -20, 55, -20, 2, -19, 3, -19, 5, -19, 9, -19, - -82, -18, 35, -18, 7, -17, -117, -17, 28, -17, 56, -17, 112, -16, -33, -16, - 45, -16, 89, -16, -78, -15, 36, -15, 72, -15, -113, -14, 29, -14, 57, -14, - 114, -13, -28, -13, 46, -13, 91, -12, -74, -12, 37, -12, 73, -12, 15, -11, 3, - -11, 59, -11, 2, -10, 3, -10, 5, -10, 1, -9, -69, -9, 38, -9, 75, -9, 15, -7, - 3, -7, 6, -7, 12, -6, -17, -7, 48, -7, 96, -7, -65, -6, 39, -6, 77, -6, -103, - -5, 31, -5, 62, -5, 123, -4, -11, -4, 49, -4, 98, -4, -60, -3, 4, -2, 79, -3, - 16, -2, 32, -2, 63, -2, 2, -1, 25, 0, 5, 1, 1, 2, 2, 2, 4, 2, 8, 2, 16, 2, - 32, 2, 64, 2, -128, 2, 26, 2, 52, 2, 103, 3, -51, 3, 41, 4, 82, 4, -92, 4, - 33, 4, 66, 4, -124, 5, 27, 5, 53, 5, 105, 6, 21, 6, 42, 6, 84, 6, 17, 7, 34, - 7, 68, 7, 2, 8, 3, 8, 6, 8, 108, 9, -41, 9, 43, 10, 86, 9, -84, 10, 35, 10, - 69, 10, -118, 11, 28, 11, 55, 12, 11, 13, 22, 13, 44, 13, 88, 13, -80, 13, - 36, 13, 71, 13, -115, 14, 29, 14, 57, 14, 113, 15, -30, 15, 46, 15, 91, 15, - 19, 16, 37, 16, 73, 16, 2, 17, 3, 17, 6, 17 -}; - -/* min(2^32-1, 10^e-1) for e in range 0 through 10 */ -static uint32_t ndigits_dec_threshold[] = { - 0, 9U, 99U, 999U, 9999U, 99999U, 999999U, - 9999999U, 99999999U, 999999999U, 0xffffffffU -}; - -/* -- Helper functions ---------------------------------------------------- */ - -/* Compute the number of digits in the decimal representation of x. */ -static MSize ndigits_dec(uint32_t x) -{ - MSize t = ((lj_fls(x | 1) * 77) >> 8) + 1; /* 2^8/77 is roughly log2(10) */ - return t + (x > ndigits_dec_threshold[t]); -} - -#define WINT_R(x, sh, sc) \ - { uint32_t d = (x*(((1<>sh; x -= d*sc; *p++ = (char)('0'+d); } - -/* Write 9-digit unsigned integer to buffer. */ -static char *lj_strfmt_wuint9(char *p, uint32_t u) -{ - uint32_t v = u / 10000, w; - u -= v * 10000; - w = v / 10000; - v -= w * 10000; - *p++ = (char)('0'+w); - WINT_R(v, 23, 1000) - WINT_R(v, 12, 100) - WINT_R(v, 10, 10) - *p++ = (char)('0'+v); - WINT_R(u, 23, 1000) - WINT_R(u, 12, 100) - WINT_R(u, 10, 10) - *p++ = (char)('0'+u); - return p; -} -#undef WINT_R - -/* -- Extended precision arithmetic --------------------------------------- */ - -/* -** The "nd" format is a fixed-precision decimal representation for numbers. It -** consists of up to 64 uint32_t values, with each uint32_t storing a value -** in the range [0, 1e9). A number in "nd" format consists of three variables: -** -** uint32_t nd[64]; -** uint32_t ndlo; -** uint32_t ndhi; -** -** The integral part of the number is stored in nd[0 ... ndhi], the value of -** which is sum{i in [0, ndhi] | nd[i] * 10^(9*i)}. If the fractional part of -** the number is zero, ndlo is zero. Otherwise, the fractional part is stored -** in nd[ndlo ... 63], the value of which is taken to be -** sum{i in [ndlo, 63] | nd[i] * 10^(9*(i-64))}. -** -** If the array part had 128 elements rather than 64, then every double would -** have an exact representation in "nd" format. With 64 elements, all integral -** doubles have an exact representation, and all non-integral doubles have -** enough digits to make both %.99e and %.99f do the right thing. -*/ - -#if LJ_64 -#define ND_MUL2K_MAX_SHIFT 29 -#define ND_MUL2K_DIV1E9(val) ((uint32_t)((val) / 1000000000)) -#else -#define ND_MUL2K_MAX_SHIFT 11 -#define ND_MUL2K_DIV1E9(val) ((uint32_t)((val) >> 9) / 1953125) -#endif - -/* Multiply nd by 2^k and add carry_in (ndlo is assumed to be zero). */ -static uint32_t nd_mul2k(uint32_t* nd, uint32_t ndhi, uint32_t k, - uint32_t carry_in, SFormat sf) -{ - uint32_t i, ndlo = 0, start = 1; - /* Performance hacks. */ - if (k > ND_MUL2K_MAX_SHIFT*2 && STRFMT_FP(sf) != STRFMT_FP(STRFMT_T_FP_F)) { - start = ndhi - (STRFMT_PREC(sf) + 17) / 8; - } - /* Real logic. */ - while (k >= ND_MUL2K_MAX_SHIFT) { - for (i = ndlo; i <= ndhi; i++) { - uint64_t val = ((uint64_t)nd[i] << ND_MUL2K_MAX_SHIFT) | carry_in; - carry_in = ND_MUL2K_DIV1E9(val); - nd[i] = (uint32_t)val - carry_in * 1000000000; - } - if (carry_in) { - nd[++ndhi] = carry_in; carry_in = 0; - if (start++ == ndlo) ++ndlo; - } - k -= ND_MUL2K_MAX_SHIFT; - } - if (k) { - for (i = ndlo; i <= ndhi; i++) { - uint64_t val = ((uint64_t)nd[i] << k) | carry_in; - carry_in = ND_MUL2K_DIV1E9(val); - nd[i] = (uint32_t)val - carry_in * 1000000000; - } - if (carry_in) nd[++ndhi] = carry_in; - } - return ndhi; -} - -/* Divide nd by 2^k (ndlo is assumed to be zero). */ -static uint32_t nd_div2k(uint32_t* nd, uint32_t ndhi, uint32_t k, SFormat sf) -{ - uint32_t ndlo = 0, stop1 = ~0, stop2 = ~0; - /* Performance hacks. */ - if (!ndhi) { - if (!nd[0]) { - return 0; - } else { - uint32_t s = lj_ffs(nd[0]); - if (s >= k) { nd[0] >>= k; return 0; } - nd[0] >>= s; k -= s; - } - } - if (k > 18) { - if (STRFMT_FP(sf) == STRFMT_FP(STRFMT_T_FP_F)) { - stop1 = 63 - (int32_t)STRFMT_PREC(sf) / 9; - } else { - int32_t floorlog2 = ndhi * 29 + lj_fls(nd[ndhi]) - k; - int32_t floorlog10 = (int32_t)(floorlog2 * 0.30102999566398114); - stop1 = 62 + (floorlog10 - (int32_t)STRFMT_PREC(sf)) / 9; - stop2 = 61 + ndhi - (int32_t)STRFMT_PREC(sf) / 8; - } - } - /* Real logic. */ - while (k >= 9) { - uint32_t i = ndhi, carry = 0; - for (;;) { - uint32_t val = nd[i]; - nd[i] = (val >> 9) + carry; - carry = (val & 0x1ff) * 1953125; - if (i == ndlo) break; - i = (i - 1) & 0x3f; - } - if (ndlo != stop1 && ndlo != stop2) { - if (carry) { ndlo = (ndlo - 1) & 0x3f; nd[ndlo] = carry; } - if (!nd[ndhi]) { ndhi = (ndhi - 1) & 0x3f; stop2--; } - } else if (!nd[ndhi]) { - if (ndhi != ndlo) { ndhi = (ndhi - 1) & 0x3f; stop2--; } - else return ndlo; - } - k -= 9; - } - if (k) { - uint32_t mask = (1U << k) - 1, mul = 1000000000 >> k, i = ndhi, carry = 0; - for (;;) { - uint32_t val = nd[i]; - nd[i] = (val >> k) + carry; - carry = (val & mask) * mul; - if (i == ndlo) break; - i = (i - 1) & 0x3f; - } - if (carry) { ndlo = (ndlo - 1) & 0x3f; nd[ndlo] = carry; } - } - return ndlo; -} - -/* Add m*10^e to nd (assumes ndlo <= e/9 <= ndhi and 0 <= m <= 9). */ -static uint32_t nd_add_m10e(uint32_t* nd, uint32_t ndhi, uint8_t m, int32_t e) -{ - uint32_t i, carry; - if (e >= 0) { - i = (uint32_t)e/9; - carry = m * (ndigits_dec_threshold[e - (int32_t)i*9] + 1); - } else { - int32_t f = (e-8)/9; - i = (uint32_t)(64 + f); - carry = m * (ndigits_dec_threshold[e - f*9] + 1); - } - for (;;) { - uint32_t val = nd[i] + carry; - if (LJ_UNLIKELY(val >= 1000000000)) { - val -= 1000000000; - nd[i] = val; - if (LJ_UNLIKELY(i == ndhi)) { - ndhi = (ndhi + 1) & 0x3f; - nd[ndhi] = 1; - break; - } - carry = 1; - i = (i + 1) & 0x3f; - } else { - nd[i] = val; - break; - } - } - return ndhi; -} - -/* Test whether two "nd" values are equal in their most significant digits. */ -static int nd_similar(uint32_t* nd, uint32_t ndhi, uint32_t* ref, MSize hilen, - MSize prec) -{ - char nd9[9], ref9[9]; - if (hilen <= prec) { - if (LJ_UNLIKELY(nd[ndhi] != *ref)) return 0; - prec -= hilen; ref--; ndhi = (ndhi - 1) & 0x3f; - if (prec >= 9) { - if (LJ_UNLIKELY(nd[ndhi] != *ref)) return 0; - prec -= 9; ref--; ndhi = (ndhi - 1) & 0x3f; - } - } else { - prec -= hilen - 9; - } - lj_assertX(prec < 9, "bad precision %d", prec); - lj_strfmt_wuint9(nd9, nd[ndhi]); - lj_strfmt_wuint9(ref9, *ref); - return !memcmp(nd9, ref9, prec) && (nd9[prec] < '5') == (ref9[prec] < '5'); -} - -/* -- Formatted conversions to buffer ------------------------------------- */ - -/* Write formatted floating-point number to either sb or p. */ -static char *lj_strfmt_wfnum(SBuf *sb, SFormat sf, lua_Number n, char *p) -{ - MSize width = STRFMT_WIDTH(sf), prec = STRFMT_PREC(sf), len; - TValue t; - t.n = n; - if (LJ_UNLIKELY((t.u32.hi << 1) >= 0xffe00000)) { - /* Handle non-finite values uniformly for %a, %e, %f, %g. */ - int prefix = 0, ch = (sf & STRFMT_F_UPPER) ? 0x202020 : 0; - if (((t.u32.hi & 0x000fffff) | t.u32.lo) != 0) { - ch ^= ('n' << 16) | ('a' << 8) | 'n'; - if ((sf & STRFMT_F_SPACE)) prefix = ' '; - } else { - ch ^= ('i' << 16) | ('n' << 8) | 'f'; - if ((t.u32.hi & 0x80000000)) prefix = '-'; - else if ((sf & STRFMT_F_PLUS)) prefix = '+'; - else if ((sf & STRFMT_F_SPACE)) prefix = ' '; - } - len = 3 + (prefix != 0); - if (!p) p = lj_buf_more(sb, width > len ? width : len); - if (!(sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' '; - if (prefix) *p++ = prefix; - *p++ = (char)(ch >> 16); *p++ = (char)(ch >> 8); *p++ = (char)ch; - } else if (STRFMT_FP(sf) == STRFMT_FP(STRFMT_T_FP_A)) { - /* %a */ - const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEFPX" - : "0123456789abcdefpx"; - int32_t e = (t.u32.hi >> 20) & 0x7ff; - char prefix = 0, eprefix = '+'; - if (t.u32.hi & 0x80000000) prefix = '-'; - else if ((sf & STRFMT_F_PLUS)) prefix = '+'; - else if ((sf & STRFMT_F_SPACE)) prefix = ' '; - t.u32.hi &= 0xfffff; - if (e) { - t.u32.hi |= 0x100000; - e -= 1023; - } else if (t.u32.lo | t.u32.hi) { - /* Non-zero denormal - normalise it. */ - uint32_t shift = t.u32.hi ? 20-lj_fls(t.u32.hi) : 52-lj_fls(t.u32.lo); - e = -1022 - shift; - t.u64 <<= shift; - } - /* abs(n) == t.u64 * 2^(e - 52) */ - /* If n != 0, bit 52 of t.u64 is set, and is the highest set bit. */ - if ((int32_t)prec < 0) { - /* Default precision: use smallest precision giving exact result. */ - prec = t.u32.lo ? 13-lj_ffs(t.u32.lo)/4 : 5-lj_ffs(t.u32.hi|0x100000)/4; - } else if (prec < 13) { - /* Precision is sufficiently low as to maybe require rounding. */ - t.u64 += (((uint64_t)1) << (51 - prec*4)); - } - if (e < 0) { - eprefix = '-'; - e = -e; - } - len = 5 + ndigits_dec((uint32_t)e) + prec + (prefix != 0) - + ((prec | (sf & STRFMT_F_ALT)) != 0); - if (!p) p = lj_buf_more(sb, width > len ? width : len); - if (!(sf & (STRFMT_F_LEFT | STRFMT_F_ZERO))) { - while (width-- > len) *p++ = ' '; - } - if (prefix) *p++ = prefix; - *p++ = '0'; - *p++ = hexdig[17]; /* x or X */ - if ((sf & (STRFMT_F_LEFT | STRFMT_F_ZERO)) == STRFMT_F_ZERO) { - while (width-- > len) *p++ = '0'; - } - *p++ = '0' + (t.u32.hi >> 20); /* Usually '1', sometimes '0' or '2'. */ - if ((prec | (sf & STRFMT_F_ALT))) { - /* Emit fractional part. */ - char *q = p + 1 + prec; - *p = '.'; - if (prec < 13) t.u64 >>= (52 - prec*4); - else while (prec > 13) p[prec--] = '0'; - while (prec) { p[prec--] = hexdig[t.u64 & 15]; t.u64 >>= 4; } - p = q; - } - *p++ = hexdig[16]; /* p or P */ - *p++ = eprefix; /* + or - */ - p = lj_strfmt_wint(p, e); - } else { - /* %e or %f or %g - begin by converting n to "nd" format. */ - uint32_t nd[64]; - uint32_t ndhi = 0, ndlo, i; - int32_t e = (t.u32.hi >> 20) & 0x7ff, ndebias = 0; - char prefix = 0, *q; - if (t.u32.hi & 0x80000000) prefix = '-'; - else if ((sf & STRFMT_F_PLUS)) prefix = '+'; - else if ((sf & STRFMT_F_SPACE)) prefix = ' '; - prec += ((int32_t)prec >> 31) & 7; /* Default precision is 6. */ - if (STRFMT_FP(sf) == STRFMT_FP(STRFMT_T_FP_G)) { - /* %g - decrement precision if non-zero (to make it like %e). */ - prec--; - prec ^= (uint32_t)((int32_t)prec >> 31); - } - if ((sf & STRFMT_T_FP_E) && prec < 14 && n != 0) { - /* Precision is sufficiently low that rescaling will probably work. */ - if ((ndebias = rescale_e[e >> 6])) { - t.n = n * rescale_n[e >> 6]; - if (LJ_UNLIKELY(!e)) t.n *= 1e10, ndebias -= 10; - t.u64 -= 2; /* Convert 2ulp below (later we convert 2ulp above). */ - nd[0] = 0x100000 | (t.u32.hi & 0xfffff); - e = ((t.u32.hi >> 20) & 0x7ff) - 1075 - (ND_MUL2K_MAX_SHIFT < 29); - goto load_t_lo; rescale_failed: - t.n = n; - e = (t.u32.hi >> 20) & 0x7ff; - ndebias = ndhi = 0; - } - } - nd[0] = t.u32.hi & 0xfffff; - if (e == 0) e++; else nd[0] |= 0x100000; - e -= 1043; - if (t.u32.lo) { - e -= 32 + (ND_MUL2K_MAX_SHIFT < 29); load_t_lo: -#if ND_MUL2K_MAX_SHIFT >= 29 - nd[0] = (nd[0] << 3) | (t.u32.lo >> 29); - ndhi = nd_mul2k(nd, ndhi, 29, t.u32.lo & 0x1fffffff, sf); -#elif ND_MUL2K_MAX_SHIFT >= 11 - ndhi = nd_mul2k(nd, ndhi, 11, t.u32.lo >> 21, sf); - ndhi = nd_mul2k(nd, ndhi, 11, (t.u32.lo >> 10) & 0x7ff, sf); - ndhi = nd_mul2k(nd, ndhi, 11, (t.u32.lo << 1) & 0x7ff, sf); -#else -#error "ND_MUL2K_MAX_SHIFT too small" -#endif - } - if (e >= 0) { - ndhi = nd_mul2k(nd, ndhi, (uint32_t)e, 0, sf); - ndlo = 0; - } else { - ndlo = nd_div2k(nd, ndhi, (uint32_t)-e, sf); - if (ndhi && !nd[ndhi]) ndhi--; - } - /* abs(n) == nd * 10^ndebias (for slightly loose interpretation of ==) */ - if ((sf & STRFMT_T_FP_E)) { - /* %e or %g - assume %e and start by calculating nd's exponent (nde). */ - char eprefix = '+'; - int32_t nde = -1; - MSize hilen; - if (ndlo && !nd[ndhi]) { - ndhi = 64; do {} while (!nd[--ndhi]); - nde -= 64 * 9; - } - hilen = ndigits_dec(nd[ndhi]); - nde += ndhi * 9 + hilen; - if (ndebias) { - /* - ** Rescaling was performed, but this introduced some error, and might - ** have pushed us across a rounding boundary. We check whether this - ** error affected the result by introducing even more error (2ulp in - ** either direction), and seeing whether a rounding boundary was - ** crossed. Having already converted the -2ulp case, we save off its - ** most significant digits, convert the +2ulp case, and compare them. - */ - int32_t eidx = e + 70 + (ND_MUL2K_MAX_SHIFT < 29) - + (t.u32.lo >= 0xfffffffe && !(~t.u32.hi << 12)); - const int8_t *m_e = four_ulp_m_e + eidx * 2; - lj_assertG_(G(sbufL(sb)), 0 <= eidx && eidx < 128, "bad eidx %d", eidx); - nd[33] = nd[ndhi]; - nd[32] = nd[(ndhi - 1) & 0x3f]; - nd[31] = nd[(ndhi - 2) & 0x3f]; - nd_add_m10e(nd, ndhi, (uint8_t)*m_e, m_e[1]); - if (LJ_UNLIKELY(!nd_similar(nd, ndhi, nd + 33, hilen, prec + 1))) { - goto rescale_failed; - } - } - if ((int32_t)(prec - nde) < (0x3f & -(int32_t)ndlo) * 9) { - /* Precision is sufficiently low as to maybe require rounding. */ - ndhi = nd_add_m10e(nd, ndhi, 5, nde - prec - 1); - nde += (hilen != ndigits_dec(nd[ndhi])); - } - nde += ndebias; - if ((sf & STRFMT_T_FP_F)) { - /* %g */ - if ((int32_t)prec >= nde && nde >= -4) { - if (nde < 0) ndhi = 0; - prec -= nde; - goto g_format_like_f; - } else if (!(sf & STRFMT_F_ALT) && prec && width > 5) { - /* Decrease precision in order to strip trailing zeroes. */ - char tail[9]; - uint32_t maxprec = hilen - 1 + ((ndhi - ndlo) & 0x3f) * 9; - if (prec >= maxprec) prec = maxprec; - else ndlo = (ndhi - (((int32_t)(prec - hilen) + 9) / 9)) & 0x3f; - i = prec - hilen - (((ndhi - ndlo) & 0x3f) * 9) + 10; - lj_strfmt_wuint9(tail, nd[ndlo]); - while (prec && tail[--i] == '0') { - prec--; - if (!i) { - if (ndlo == ndhi) { prec = 0; break; } - lj_strfmt_wuint9(tail, nd[++ndlo]); - i = 9; - } - } - } - } - if (nde < 0) { - /* Make nde non-negative. */ - eprefix = '-'; - nde = -nde; - } - len = 3 + prec + (prefix != 0) + ndigits_dec((uint32_t)nde) + (nde < 10) - + ((prec | (sf & STRFMT_F_ALT)) != 0); - if (!p) p = lj_buf_more(sb, (width > len ? width : len) + 5); - if (!(sf & (STRFMT_F_LEFT | STRFMT_F_ZERO))) { - while (width-- > len) *p++ = ' '; - } - if (prefix) *p++ = prefix; - if ((sf & (STRFMT_F_LEFT | STRFMT_F_ZERO)) == STRFMT_F_ZERO) { - while (width-- > len) *p++ = '0'; - } - q = lj_strfmt_wint(p + 1, nd[ndhi]); - p[0] = p[1]; /* Put leading digit in the correct place. */ - if ((prec | (sf & STRFMT_F_ALT))) { - /* Emit fractional part. */ - p[1] = '.'; p += 2; - prec -= (MSize)(q - p); p = q; /* Account for digits already emitted. */ - /* Then emit chunks of 9 digits (this may emit 8 digits too many). */ - for (i = ndhi; (int32_t)prec > 0 && i != ndlo; prec -= 9) { - i = (i - 1) & 0x3f; - p = lj_strfmt_wuint9(p, nd[i]); - } - if ((sf & STRFMT_T_FP_F) && !(sf & STRFMT_F_ALT)) { - /* %g (and not %#g) - strip trailing zeroes. */ - p += (int32_t)prec & ((int32_t)prec >> 31); - while (p[-1] == '0') p--; - if (p[-1] == '.') p--; - } else { - /* %e (or %#g) - emit trailing zeroes. */ - while ((int32_t)prec > 0) { *p++ = '0'; prec--; } - p += (int32_t)prec; - } - } else { - p++; - } - *p++ = (sf & STRFMT_F_UPPER) ? 'E' : 'e'; - *p++ = eprefix; /* + or - */ - if (nde < 10) *p++ = '0'; /* Always at least two digits of exponent. */ - p = lj_strfmt_wint(p, nde); - } else { - /* %f (or, shortly, %g in %f style) */ - if (prec < (MSize)(0x3f & -(int32_t)ndlo) * 9) { - /* Precision is sufficiently low as to maybe require rounding. */ - ndhi = nd_add_m10e(nd, ndhi, 5, 0 - prec - 1); - } - g_format_like_f: - if ((sf & STRFMT_T_FP_E) && !(sf & STRFMT_F_ALT) && prec && width) { - /* Decrease precision in order to strip trailing zeroes. */ - if (ndlo) { - /* nd has a fractional part; we need to look at its digits. */ - char tail[9]; - uint32_t maxprec = (64 - ndlo) * 9; - if (prec >= maxprec) prec = maxprec; - else ndlo = 64 - (prec + 8) / 9; - i = prec - ((63 - ndlo) * 9); - lj_strfmt_wuint9(tail, nd[ndlo]); - while (prec && tail[--i] == '0') { - prec--; - if (!i) { - if (ndlo == 63) { prec = 0; break; } - lj_strfmt_wuint9(tail, nd[++ndlo]); - i = 9; - } - } - } else { - /* nd has no fractional part, so precision goes straight to zero. */ - prec = 0; - } - } - len = ndhi * 9 + ndigits_dec(nd[ndhi]) + prec + (prefix != 0) - + ((prec | (sf & STRFMT_F_ALT)) != 0); - if (!p) p = lj_buf_more(sb, (width > len ? width : len) + 8); - if (!(sf & (STRFMT_F_LEFT | STRFMT_F_ZERO))) { - while (width-- > len) *p++ = ' '; - } - if (prefix) *p++ = prefix; - if ((sf & (STRFMT_F_LEFT | STRFMT_F_ZERO)) == STRFMT_F_ZERO) { - while (width-- > len) *p++ = '0'; - } - /* Emit integer part. */ - p = lj_strfmt_wint(p, nd[ndhi]); - i = ndhi; - while (i) p = lj_strfmt_wuint9(p, nd[--i]); - if ((prec | (sf & STRFMT_F_ALT))) { - /* Emit fractional part. */ - *p++ = '.'; - /* Emit chunks of 9 digits (this may emit 8 digits too many). */ - while ((int32_t)prec > 0 && i != ndlo) { - i = (i - 1) & 0x3f; - p = lj_strfmt_wuint9(p, nd[i]); - prec -= 9; - } - if ((sf & STRFMT_T_FP_E) && !(sf & STRFMT_F_ALT)) { - /* %g (and not %#g) - strip trailing zeroes. */ - p += (int32_t)prec & ((int32_t)prec >> 31); - while (p[-1] == '0') p--; - if (p[-1] == '.') p--; - } else { - /* %f (or %#g) - emit trailing zeroes. */ - while ((int32_t)prec > 0) { *p++ = '0'; prec--; } - p += (int32_t)prec; - } - } - } - } - if ((sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' '; - return p; -} - -/* Add formatted floating-point number to buffer. */ -SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat sf, lua_Number n) -{ - sb->w = lj_strfmt_wfnum(sb, sf, n, NULL); - return sb; -} - -/* -- Conversions to strings ---------------------------------------------- */ - -/* Convert number to string. */ -GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o) -{ - char buf[STRFMT_MAXBUF_NUM]; - MSize len = (MSize)(lj_strfmt_wfnum(NULL, STRFMT_G14, o->n, buf) - buf); - return lj_str_new(L, buf, len); -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.c deleted file mode 100644 index 1d1c1c7..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.c +++ /dev/null @@ -1,558 +0,0 @@ -/* -** String scanning. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lj_strscan_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_char.h" -#include "lj_strscan.h" - -/* -- Scanning numbers ---------------------------------------------------- */ - -/* -** Rationale for the builtin string to number conversion library: -** -** It removes a dependency on libc's strtod(), which is a true portability -** nightmare. Mainly due to the plethora of supported OS and toolchain -** combinations. Sadly, the various implementations -** a) are often buggy, incomplete (no hex floats) and/or imprecise, -** b) sometimes crash or hang on certain inputs, -** c) return non-standard NaNs that need to be filtered out, and -** d) fail if the locale-specific decimal separator is not a dot, -** which can only be fixed with atrocious workarounds. -** -** Also, most of the strtod() implementations are hopelessly bloated, -** which is not just an I-cache hog, but a problem for static linkage -** on embedded systems, too. -** -** OTOH the builtin conversion function is very compact. Even though it -** does a lot more, like parsing long longs, octal or imaginary numbers -** and returning the result in different formats: -** a) It needs less than 3 KB (!) of machine code (on x64 with -Os), -** b) it doesn't perform any dynamic allocation and, -** c) it needs only around 600 bytes of stack space. -** -** The builtin function is faster than strtod() for typical inputs, e.g. -** "123", "1.5" or "1e6". Arguably, it's slower for very large exponents, -** which are not very common (this could be fixed, if needed). -** -** And most importantly, the builtin function is equally precise on all -** platforms. It correctly converts and rounds any input to a double. -** If this is not the case, please send a bug report -- but PLEASE verify -** that the implementation you're comparing to is not the culprit! -** -** The implementation quickly pre-scans the entire string first and -** handles simple integers on-the-fly. Otherwise, it dispatches to the -** base-specific parser. Hex and octal is straightforward. -** -** Decimal to binary conversion uses a fixed-length circular buffer in -** base 100. Some simple cases are handled directly. For other cases, the -** number in the buffer is up-scaled or down-scaled until the integer part -** is in the proper range. Then the integer part is rounded and converted -** to a double which is finally rescaled to the result. Denormals need -** special treatment to prevent incorrect 'double rounding'. -*/ - -/* Definitions for circular decimal digit buffer (base 100 = 2 digits/byte). */ -#define STRSCAN_DIG 1024 -#define STRSCAN_MAXDIG 800 /* 772 + extra are sufficient. */ -#define STRSCAN_DDIG (STRSCAN_DIG/2) -#define STRSCAN_DMASK (STRSCAN_DDIG-1) -#define STRSCAN_MAXEXP (1 << 20) - -/* Helpers for circular buffer. */ -#define DNEXT(a) (((a)+1) & STRSCAN_DMASK) -#define DPREV(a) (((a)-1) & STRSCAN_DMASK) -#define DLEN(lo, hi) ((int32_t)(((lo)-(hi)) & STRSCAN_DMASK)) - -#define casecmp(c, k) (((c) | 0x20) == k) - -/* Final conversion to double. */ -static void strscan_double(uint64_t x, TValue *o, int32_t ex2, int32_t neg) -{ - double n; - - /* Avoid double rounding for denormals. */ - if (LJ_UNLIKELY(ex2 <= -1075 && x != 0)) { - /* NYI: all of this generates way too much code on 32 bit CPUs. */ -#if (defined(__GNUC__) || defined(__clang__)) && LJ_64 - int32_t b = (int32_t)(__builtin_clzll(x)^63); -#else - int32_t b = (x>>32) ? 32+(int32_t)lj_fls((uint32_t)(x>>32)) : - (int32_t)lj_fls((uint32_t)x); -#endif - if ((int32_t)b + ex2 <= -1023 && (int32_t)b + ex2 >= -1075) { - uint64_t rb = (uint64_t)1 << (-1075-ex2); - if ((x & rb) && ((x & (rb+rb+rb-1)))) x += rb+rb; - x = (x & ~(rb+rb-1)); - } - } - - /* Convert to double using a signed int64_t conversion, then rescale. */ - lj_assertX((int64_t)x >= 0, "bad double conversion"); - n = (double)(int64_t)x; - if (neg) n = -n; - if (ex2) n = ldexp(n, ex2); - o->n = n; -} - -/* Parse hexadecimal number. */ -static StrScanFmt strscan_hex(const uint8_t *p, TValue *o, - StrScanFmt fmt, uint32_t opt, - int32_t ex2, int32_t neg, uint32_t dig) -{ - uint64_t x = 0; - uint32_t i; - - /* Scan hex digits. */ - for (i = dig > 16 ? 16 : dig ; i; i--, p++) { - uint32_t d = (*p != '.' ? *p : *++p); if (d > '9') d += 9; - x = (x << 4) + (d & 15); - } - - /* Summarize rounding-effect of excess digits. */ - for (i = 16; i < dig; i++, p++) - x |= ((*p != '.' ? *p : *++p) != '0'), ex2 += 4; - - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg && - !(x == 0 && neg)) { - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_INT; /* Fast path for 32 bit integers. */ - } - if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; } - /* fallthrough */ - case STRSCAN_U32: - if (dig > 8) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_U32; - case STRSCAN_I64: - case STRSCAN_U64: - if (dig > 16) return STRSCAN_ERROR; - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - return fmt; - default: - break; - } - - /* Reduce range, then convert to double. */ - if ((x & U64x(c0000000,0000000))) { x = (x >> 2) | (x & 3); ex2 += 2; } - strscan_double(x, o, ex2, neg); - return fmt; -} - -/* Parse octal number. */ -static StrScanFmt strscan_oct(const uint8_t *p, TValue *o, - StrScanFmt fmt, int32_t neg, uint32_t dig) -{ - uint64_t x = 0; - - /* Scan octal digits. */ - if (dig > 22 || (dig == 22 && *p > '1')) return STRSCAN_ERROR; - while (dig-- > 0) { - if (!(*p >= '0' && *p <= '7')) return STRSCAN_ERROR; - x = (x << 3) + (*p++ & 7); - } - - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (x >= 0x80000000u+neg) fmt = STRSCAN_U32; - /* fallthrough */ - case STRSCAN_U32: - if ((x >> 32)) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - break; - default: - case STRSCAN_I64: - case STRSCAN_U64: - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - break; - } - return fmt; -} - -/* Parse decimal number. */ -static StrScanFmt strscan_dec(const uint8_t *p, TValue *o, - StrScanFmt fmt, uint32_t opt, - int32_t ex10, int32_t neg, uint32_t dig) -{ - uint8_t xi[STRSCAN_DDIG], *xip = xi; - - if (dig) { - uint32_t i = dig; - if (i > STRSCAN_MAXDIG) { - ex10 += (int32_t)(i - STRSCAN_MAXDIG); - i = STRSCAN_MAXDIG; - } - /* Scan unaligned leading digit. */ - if (((ex10^i) & 1)) - *xip++ = ((*p != '.' ? *p : *++p) & 15), i--, p++; - /* Scan aligned double-digits. */ - for ( ; i > 1; i -= 2) { - uint32_t d = 10 * ((*p != '.' ? *p : *++p) & 15); p++; - *xip++ = d + ((*p != '.' ? *p : *++p) & 15); p++; - } - /* Scan and realign trailing digit. */ - if (i) *xip++ = 10 * ((*p != '.' ? *p : *++p) & 15), ex10--, dig++, p++; - - /* Summarize rounding-effect of excess digits. */ - if (dig > STRSCAN_MAXDIG) { - do { - if ((*p != '.' ? *p : *++p) != '0') { xip[-1] |= 1; break; } - p++; - } while (--dig > STRSCAN_MAXDIG); - dig = STRSCAN_MAXDIG; - } else { /* Simplify exponent. */ - while (ex10 > 0 && dig <= 18) *xip++ = 0, ex10 -= 2, dig += 2; - } - } else { /* Only got zeros. */ - ex10 = 0; - xi[0] = 0; - } - - /* Fast path for numbers in integer format (but handles e.g. 1e6, too). */ - if (dig <= 20 && ex10 == 0) { - uint8_t *xis; - uint64_t x = xi[0]; - double n; - for (xis = xi+1; xis < xip; xis++) x = x * 100 + *xis; - if (!(dig == 20 && (xi[0] > 18 || (int64_t)x >= 0))) { /* No overflow? */ - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_INT; /* Fast path for 32 bit integers. */ - } - if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; goto plainnumber; } - /* fallthrough */ - case STRSCAN_U32: - if ((x >> 32) != 0) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_U32; - case STRSCAN_I64: - case STRSCAN_U64: - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - return fmt; - default: - plainnumber: /* Fast path for plain numbers < 2^63. */ - if ((int64_t)x < 0) break; - n = (double)(int64_t)x; - if (neg) n = -n; - o->n = n; - return fmt; - } - } - } - - /* Slow non-integer path. */ - if (fmt == STRSCAN_INT) { - if ((opt & STRSCAN_OPT_C)) return STRSCAN_ERROR; - fmt = STRSCAN_NUM; - } else if (fmt > STRSCAN_INT) { - return STRSCAN_ERROR; - } - { - uint32_t hi = 0, lo = (uint32_t)(xip-xi); - int32_t ex2 = 0, idig = (int32_t)lo + (ex10 >> 1); - - lj_assertX(lo > 0 && (ex10 & 1) == 0, "bad lo %d ex10 %d", lo, ex10); - - /* Handle simple overflow/underflow. */ - if (idig > 310/2) { if (neg) setminfV(o); else setpinfV(o); return fmt; } - else if (idig < -326/2) { o->n = neg ? -0.0 : 0.0; return fmt; } - - /* Scale up until we have at least 17 or 18 integer part digits. */ - while (idig < 9 && idig < DLEN(lo, hi)) { - uint32_t i, cy = 0; - ex2 -= 6; - for (i = DPREV(lo); ; i = DPREV(i)) { - uint32_t d = (xi[i] << 6) + cy; - cy = (((d >> 2) * 5243) >> 17); d = d - cy * 100; /* Div/mod 100. */ - xi[i] = (uint8_t)d; - if (i == hi) break; - if (d == 0 && i == DPREV(lo)) lo = i; - } - if (cy) { - hi = DPREV(hi); - if (xi[DPREV(lo)] == 0) lo = DPREV(lo); - else if (hi == lo) { lo = DPREV(lo); xi[DPREV(lo)] |= xi[lo]; } - xi[hi] = (uint8_t)cy; idig++; - } - } - - /* Scale down until no more than 17 or 18 integer part digits remain. */ - while (idig > 9) { - uint32_t i = hi, cy = 0; - ex2 += 6; - do { - cy += xi[i]; - xi[i] = (cy >> 6); - cy = 100 * (cy & 0x3f); - if (xi[i] == 0 && i == hi) hi = DNEXT(hi), idig--; - i = DNEXT(i); - } while (i != lo); - while (cy) { - if (hi == lo) { xi[DPREV(lo)] |= 1; break; } - xi[lo] = (cy >> 6); lo = DNEXT(lo); - cy = 100 * (cy & 0x3f); - } - } - - /* Collect integer part digits and convert to rescaled double. */ - { - uint64_t x = xi[hi]; - uint32_t i; - for (i = DNEXT(hi); --idig > 0 && i != lo; i = DNEXT(i)) - x = x * 100 + xi[i]; - if (i == lo) { - while (--idig >= 0) x = x * 100; - } else { /* Gather round bit from remaining digits. */ - x <<= 1; ex2--; - do { - if (xi[i]) { x |= 1; break; } - i = DNEXT(i); - } while (i != lo); - } - strscan_double(x, o, ex2, neg); - } - } - return fmt; -} - -/* Parse binary number. */ -static StrScanFmt strscan_bin(const uint8_t *p, TValue *o, - StrScanFmt fmt, uint32_t opt, - int32_t ex2, int32_t neg, uint32_t dig) -{ - uint64_t x = 0; - uint32_t i; - - if (ex2 || dig > 64) return STRSCAN_ERROR; - - /* Scan binary digits. */ - for (i = dig; i; i--, p++) { - if ((*p & ~1) != '0') return STRSCAN_ERROR; - x = (x << 1) | (*p & 1); - } - - /* Format-specific handling. */ - switch (fmt) { - case STRSCAN_INT: - if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_INT; /* Fast path for 32 bit integers. */ - } - if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; } - /* fallthrough */ - case STRSCAN_U32: - if (dig > 32) return STRSCAN_ERROR; - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_U32; - case STRSCAN_I64: - case STRSCAN_U64: - o->u64 = neg ? (uint64_t)-(int64_t)x : x; - return fmt; - default: - break; - } - - /* Reduce range, then convert to double. */ - if ((x & U64x(c0000000,0000000))) { x = (x >> 2) | (x & 3); ex2 += 2; } - strscan_double(x, o, ex2, neg); - return fmt; -} - -/* Scan string containing a number. Returns format. Returns value in o. */ -StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o, - uint32_t opt) -{ - int32_t neg = 0; - const uint8_t *pe = p + len; - - /* Remove leading space, parse sign and non-numbers. */ - if (LJ_UNLIKELY(!lj_char_isdigit(*p))) { - while (lj_char_isspace(*p)) p++; - if (*p == '+' || *p == '-') neg = (*p++ == '-'); - if (LJ_UNLIKELY(*p >= 'A')) { /* Parse "inf", "infinity" or "nan". */ - TValue tmp; - setnanV(&tmp); - if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'f')) { - if (neg) setminfV(&tmp); else setpinfV(&tmp); - p += 3; - if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'i') && - casecmp(p[3],'t') && casecmp(p[4],'y')) p += 5; - } else if (casecmp(p[0],'n') && casecmp(p[1],'a') && casecmp(p[2],'n')) { - p += 3; - } - while (lj_char_isspace(*p)) p++; - if (*p || p < pe) return STRSCAN_ERROR; - o->u64 = tmp.u64; - return STRSCAN_NUM; - } - } - - /* Parse regular number. */ - { - StrScanFmt fmt = STRSCAN_INT; - int cmask = LJ_CHAR_DIGIT; - int base = (opt & STRSCAN_OPT_C) && *p == '0' ? 0 : 10; - const uint8_t *sp, *dp = NULL; - uint32_t dig = 0, hasdig = 0, x = 0; - int32_t ex = 0; - - /* Determine base and skip leading zeros. */ - if (LJ_UNLIKELY(*p <= '0')) { - if (*p == '0') { - if (casecmp(p[1], 'x')) - base = 16, cmask = LJ_CHAR_XDIGIT, p += 2; - else if (casecmp(p[1], 'b')) - base = 2, cmask = LJ_CHAR_DIGIT, p += 2; - } - for ( ; ; p++) { - if (*p == '0') { - hasdig = 1; - } else if (*p == '.') { - if (dp) return STRSCAN_ERROR; - dp = p; - } else { - break; - } - } - } - - /* Preliminary digit and decimal point scan. */ - for (sp = p; ; p++) { - if (LJ_LIKELY(lj_char_isa(*p, cmask))) { - x = x * 10 + (*p & 15); /* For fast path below. */ - dig++; - } else if (*p == '.') { - if (dp) return STRSCAN_ERROR; - dp = p; - } else { - break; - } - } - if (!(hasdig | dig)) return STRSCAN_ERROR; - - /* Handle decimal point. */ - if (dp) { - if (base == 2) return STRSCAN_ERROR; - fmt = STRSCAN_NUM; - if (dig) { - ex = (int32_t)(dp-(p-1)); dp = p-1; - while (ex < 0 && *dp-- == '0') ex++, dig--; /* Skip trailing zeros. */ - if (ex <= -STRSCAN_MAXEXP) return STRSCAN_ERROR; - if (base == 16) ex *= 4; - } - } - - /* Parse exponent. */ - if (base >= 10 && casecmp(*p, (uint32_t)(base == 16 ? 'p' : 'e'))) { - uint32_t xx; - int negx = 0; - fmt = STRSCAN_NUM; p++; - if (*p == '+' || *p == '-') negx = (*p++ == '-'); - if (!lj_char_isdigit(*p)) return STRSCAN_ERROR; - xx = (*p++ & 15); - while (lj_char_isdigit(*p)) { - xx = xx * 10 + (*p & 15); - if (xx >= STRSCAN_MAXEXP) return STRSCAN_ERROR; - p++; - } - ex += negx ? -(int32_t)xx : (int32_t)xx; - } - - /* Parse suffix. */ - if (*p) { - /* I (IMAG), U (U32), LL (I64), ULL/LLU (U64), L (long), UL/LU (ulong). */ - /* NYI: f (float). Not needed until cp_number() handles non-integers. */ - if (casecmp(*p, 'i')) { - if (!(opt & STRSCAN_OPT_IMAG)) return STRSCAN_ERROR; - p++; fmt = STRSCAN_IMAG; - } else if (fmt == STRSCAN_INT) { - if (casecmp(*p, 'u')) p++, fmt = STRSCAN_U32; - if (casecmp(*p, 'l')) { - p++; - if (casecmp(*p, 'l')) p++, fmt += STRSCAN_I64 - STRSCAN_INT; - else if (!(opt & STRSCAN_OPT_C)) return STRSCAN_ERROR; - else if (sizeof(long) == 8) fmt += STRSCAN_I64 - STRSCAN_INT; - } - if (casecmp(*p, 'u') && (fmt == STRSCAN_INT || fmt == STRSCAN_I64)) - p++, fmt += STRSCAN_U32 - STRSCAN_INT; - if ((fmt == STRSCAN_U32 && !(opt & STRSCAN_OPT_C)) || - (fmt >= STRSCAN_I64 && !(opt & STRSCAN_OPT_LL))) - return STRSCAN_ERROR; - } - while (lj_char_isspace(*p)) p++; - if (*p) return STRSCAN_ERROR; - } - if (p < pe) return STRSCAN_ERROR; - - /* Fast path for decimal 32 bit integers. */ - if (fmt == STRSCAN_INT && base == 10 && - (dig < 10 || (dig == 10 && *sp <= '2' && x < 0x80000000u+neg))) { - if ((opt & STRSCAN_OPT_TONUM)) { - o->n = neg ? -(double)x : (double)x; - return STRSCAN_NUM; - } else if (x == 0 && neg) { - o->n = -0.0; - return STRSCAN_NUM; - } else { - o->i = neg ? -(int32_t)x : (int32_t)x; - return STRSCAN_INT; - } - } - - /* Dispatch to base-specific parser. */ - if (base == 0 && !(fmt == STRSCAN_NUM || fmt == STRSCAN_IMAG)) - return strscan_oct(sp, o, fmt, neg, dig); - if (base == 16) - fmt = strscan_hex(sp, o, fmt, opt, ex, neg, dig); - else if (base == 2) - fmt = strscan_bin(sp, o, fmt, opt, ex, neg, dig); - else - fmt = strscan_dec(sp, o, fmt, opt, ex, neg, dig); - - /* Try to convert number to integer, if requested. */ - if (fmt == STRSCAN_NUM && (opt & STRSCAN_OPT_TOINT) && !tvismzero(o)) { - double n = o->n; - int32_t i = lj_num2int(n); - if (n == (lua_Number)i) { o->i = i; return STRSCAN_INT; } - } - return fmt; - } -} - -int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o) -{ - StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), str->len, o, - STRSCAN_OPT_TONUM); - lj_assertX(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM, "bad scan format"); - return (fmt != STRSCAN_ERROR); -} - -#if LJ_DUALNUM -int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o) -{ - StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), str->len, o, - STRSCAN_OPT_TOINT); - lj_assertX(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM || fmt == STRSCAN_INT, - "bad scan format"); - if (fmt == STRSCAN_INT) setitype(o, LJ_TISNUM); - return (fmt != STRSCAN_ERROR); -} -#endif - -#undef DNEXT -#undef DPREV -#undef DLEN - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.h deleted file mode 100644 index 8ed3154..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_strscan.h +++ /dev/null @@ -1,40 +0,0 @@ -/* -** String scanning. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_STRSCAN_H -#define _LJ_STRSCAN_H - -#include "lj_obj.h" - -/* Options for accepted/returned formats. */ -#define STRSCAN_OPT_TOINT 0x01 /* Convert to int32_t, if possible. */ -#define STRSCAN_OPT_TONUM 0x02 /* Always convert to double. */ -#define STRSCAN_OPT_IMAG 0x04 -#define STRSCAN_OPT_LL 0x08 -#define STRSCAN_OPT_C 0x10 - -/* Returned format. */ -typedef enum { - STRSCAN_ERROR, - STRSCAN_NUM, STRSCAN_IMAG, - STRSCAN_INT, STRSCAN_U32, STRSCAN_I64, STRSCAN_U64, -} StrScanFmt; - -LJ_FUNC StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o, - uint32_t opt); -LJ_FUNC int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o); -#if LJ_DUALNUM -LJ_FUNC int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o); -#else -#define lj_strscan_number(s, o) lj_strscan_num((s), (o)) -#endif - -/* Check for number or convert string to number/int in-place (!). */ -static LJ_AINLINE int lj_strscan_numberobj(TValue *o) -{ - return tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), o)); -} - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.c deleted file mode 100644 index c3609b3..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.c +++ /dev/null @@ -1,693 +0,0 @@ -/* -** Table handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#define lj_tab_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_tab.h" - -/* -- Object hashing ------------------------------------------------------ */ - -/* Hash an arbitrary key and return its anchor position in the hash table. */ -static Node *hashkey(const GCtab *t, cTValue *key) -{ - lj_assertX(!tvisint(key), "attempt to hash integer"); - if (tvisstr(key)) - return hashstr(t, strV(key)); - else if (tvisnum(key)) - return hashnum(t, key); - else if (tvisbool(key)) - return hashmask(t, boolV(key)); - else - return hashgcref(t, key->gcr); - /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */ -} - -/* -- Table creation and destruction -------------------------------------- */ - -/* Create new hash part for table. */ -static LJ_AINLINE void newhpart(lua_State *L, GCtab *t, uint32_t hbits) -{ - uint32_t hsize; - Node *node; - lj_assertL(hbits != 0, "zero hash size"); - if (hbits > LJ_MAX_HBITS) - lj_err_msg(L, LJ_ERR_TABOV); - hsize = 1u << hbits; - node = lj_mem_newvec(L, hsize, Node); - setmref(t->node, node); - setfreetop(t, node, &node[hsize]); - t->hmask = hsize-1; -} - -/* -** Q: Why all of these copies of t->hmask, t->node etc. to local variables? -** A: Because alias analysis for C is _really_ tough. -** Even state-of-the-art C compilers won't produce good code without this. -*/ - -/* Clear hash part of table. */ -static LJ_AINLINE void clearhpart(GCtab *t) -{ - uint32_t i, hmask = t->hmask; - Node *node = noderef(t->node); - lj_assertX(t->hmask != 0, "empty hash part"); - for (i = 0; i <= hmask; i++) { - Node *n = &node[i]; - setmref(n->next, NULL); - setnilV(&n->key); - setnilV(&n->val); - } -} - -/* Clear array part of table. */ -static LJ_AINLINE void clearapart(GCtab *t) -{ - uint32_t i, asize = t->asize; - TValue *array = tvref(t->array); - for (i = 0; i < asize; i++) - setnilV(&array[i]); -} - -/* Create a new table. Note: the slots are not initialized (yet). */ -static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits) -{ - GCtab *t; - /* First try to colocate the array part. */ - if (LJ_MAX_COLOSIZE != 0 && asize > 0 && asize <= LJ_MAX_COLOSIZE) { - Node *nilnode; - lj_assertL((sizeof(GCtab) & 7) == 0, "bad GCtab size"); - t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize)); - t->gct = ~LJ_TTAB; - t->nomm = (uint8_t)~0; - t->colo = (int8_t)asize; - setmref(t->array, (TValue *)((char *)t + sizeof(GCtab))); - setgcrefnull(t->metatable); - t->asize = asize; - t->hmask = 0; - nilnode = &G(L)->nilnode; - setmref(t->node, nilnode); -#if LJ_GC64 - setmref(t->freetop, nilnode); -#endif - } else { /* Otherwise separately allocate the array part. */ - Node *nilnode; - t = lj_mem_newobj(L, GCtab); - t->gct = ~LJ_TTAB; - t->nomm = (uint8_t)~0; - t->colo = 0; - setmref(t->array, NULL); - setgcrefnull(t->metatable); - t->asize = 0; /* In case the array allocation fails. */ - t->hmask = 0; - nilnode = &G(L)->nilnode; - setmref(t->node, nilnode); -#if LJ_GC64 - setmref(t->freetop, nilnode); -#endif - if (asize > 0) { - if (asize > LJ_MAX_ASIZE) - lj_err_msg(L, LJ_ERR_TABOV); - setmref(t->array, lj_mem_newvec(L, asize, TValue)); - t->asize = asize; - } - } - if (hbits) - newhpart(L, t, hbits); - return t; -} - -/* Create a new table. -** -** IMPORTANT NOTE: The API differs from lua_createtable()! -** -** The array size is non-inclusive. E.g. asize=128 creates array slots -** for 0..127, but not for 128. If you need slots 1..128, pass asize=129 -** (slot 0 is wasted in this case). -** -** The hash size is given in hash bits. hbits=0 means no hash part. -** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on. -*/ -GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits) -{ - GCtab *t = newtab(L, asize, hbits); - clearapart(t); - if (t->hmask > 0) clearhpart(t); - return t; -} - -/* The API of this function conforms to lua_createtable(). */ -GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h) -{ - return lj_tab_new(L, (uint32_t)(a > 0 ? a+1 : 0), hsize2hbits(h)); -} - -#if LJ_HASJIT -GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize) -{ - GCtab *t = newtab(L, ahsize & 0xffffff, ahsize >> 24); - clearapart(t); - if (t->hmask > 0) clearhpart(t); - return t; -} -#endif - -/* Duplicate a table. */ -GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt) -{ - GCtab *t; - uint32_t asize, hmask; - t = newtab(L, kt->asize, kt->hmask > 0 ? lj_fls(kt->hmask)+1 : 0); - lj_assertL(kt->asize == t->asize && kt->hmask == t->hmask, - "mismatched size of table and template"); - t->nomm = 0; /* Keys with metamethod names may be present. */ - asize = kt->asize; - if (asize > 0) { - TValue *array = tvref(t->array); - TValue *karray = tvref(kt->array); - if (asize < 64) { /* An inlined loop beats memcpy for < 512 bytes. */ - uint32_t i; - for (i = 0; i < asize; i++) - copyTV(L, &array[i], &karray[i]); - } else { - memcpy(array, karray, asize*sizeof(TValue)); - } - } - hmask = kt->hmask; - if (hmask > 0) { - uint32_t i; - Node *node = noderef(t->node); - Node *knode = noderef(kt->node); - ptrdiff_t d = (char *)node - (char *)knode; - setfreetop(t, node, (Node *)((char *)getfreetop(kt, knode) + d)); - for (i = 0; i <= hmask; i++) { - Node *kn = &knode[i]; - Node *n = &node[i]; - Node *next = nextnode(kn); - /* Don't use copyTV here, since it asserts on a copy of a dead key. */ - n->val = kn->val; n->key = kn->key; - setmref(n->next, next == NULL? next : (Node *)((char *)next + d)); - } - } - return t; -} - -/* Clear a table. */ -void LJ_FASTCALL lj_tab_clear(GCtab *t) -{ - clearapart(t); - if (t->hmask > 0) { - Node *node = noderef(t->node); - setfreetop(t, node, &node[t->hmask+1]); - clearhpart(t); - } -} - -/* Free a table. */ -void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t) -{ - if (t->hmask > 0) - lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node); - if (t->asize > 0 && LJ_MAX_COLOSIZE != 0 && t->colo <= 0) - lj_mem_freevec(g, tvref(t->array), t->asize, TValue); - if (LJ_MAX_COLOSIZE != 0 && t->colo) - lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f)); - else - lj_mem_freet(g, t); -} - -/* -- Table resizing ------------------------------------------------------ */ - -/* Resize a table to fit the new array/hash part sizes. */ -void lj_tab_resize(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits) -{ - Node *oldnode = noderef(t->node); - uint32_t oldasize = t->asize; - uint32_t oldhmask = t->hmask; - if (asize > oldasize) { /* Array part grows? */ - TValue *array; - uint32_t i; - if (asize > LJ_MAX_ASIZE) - lj_err_msg(L, LJ_ERR_TABOV); - if (LJ_MAX_COLOSIZE != 0 && t->colo > 0) { - /* A colocated array must be separated and copied. */ - TValue *oarray = tvref(t->array); - array = lj_mem_newvec(L, asize, TValue); - t->colo = (int8_t)(t->colo | 0x80); /* Mark as separated (colo < 0). */ - for (i = 0; i < oldasize; i++) - copyTV(L, &array[i], &oarray[i]); - } else { - array = (TValue *)lj_mem_realloc(L, tvref(t->array), - oldasize*sizeof(TValue), asize*sizeof(TValue)); - } - setmref(t->array, array); - t->asize = asize; - for (i = oldasize; i < asize; i++) /* Clear newly allocated slots. */ - setnilV(&array[i]); - } - /* Create new (empty) hash part. */ - if (hbits) { - newhpart(L, t, hbits); - clearhpart(t); - } else { - global_State *g = G(L); - setmref(t->node, &g->nilnode); -#if LJ_GC64 - setmref(t->freetop, &g->nilnode); -#endif - t->hmask = 0; - } - if (asize < oldasize) { /* Array part shrinks? */ - TValue *array = tvref(t->array); - uint32_t i; - t->asize = asize; /* Note: This 'shrinks' even colocated arrays. */ - for (i = asize; i < oldasize; i++) /* Reinsert old array values. */ - if (!tvisnil(&array[i])) - copyTV(L, lj_tab_setinth(L, t, (int32_t)i), &array[i]); - /* Physically shrink only separated arrays. */ - if (LJ_MAX_COLOSIZE != 0 && t->colo <= 0) - setmref(t->array, lj_mem_realloc(L, array, - oldasize*sizeof(TValue), asize*sizeof(TValue))); - } - if (oldhmask > 0) { /* Reinsert pairs from old hash part. */ - global_State *g; - uint32_t i; - for (i = 0; i <= oldhmask; i++) { - Node *n = &oldnode[i]; - if (!tvisnil(&n->val)) - copyTV(L, lj_tab_set(L, t, &n->key), &n->val); - } - g = G(L); - lj_mem_freevec(g, oldnode, oldhmask+1, Node); - } -} - -static uint32_t countint(cTValue *key, uint32_t *bins) -{ - lj_assertX(!tvisint(key), "bad integer key"); - if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) { - bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++; - return 1; - } - } - return 0; -} - -static uint32_t countarray(const GCtab *t, uint32_t *bins) -{ - uint32_t na, b, i; - if (t->asize == 0) return 0; - for (na = i = b = 0; b < LJ_MAX_ABITS; b++) { - uint32_t n, top = 2u << b; - TValue *array; - if (top >= t->asize) { - top = t->asize-1; - if (i > top) - break; - } - array = tvref(t->array); - for (n = 0; i <= top; i++) - if (!tvisnil(&array[i])) - n++; - bins[b] += n; - na += n; - } - return na; -} - -static uint32_t counthash(const GCtab *t, uint32_t *bins, uint32_t *narray) -{ - uint32_t total, na, i, hmask = t->hmask; - Node *node = noderef(t->node); - for (total = na = 0, i = 0; i <= hmask; i++) { - Node *n = &node[i]; - if (!tvisnil(&n->val)) { - na += countint(&n->key, bins); - total++; - } - } - *narray += na; - return total; -} - -static uint32_t bestasize(uint32_t bins[], uint32_t *narray) -{ - uint32_t b, sum, na = 0, sz = 0, nn = *narray; - for (b = 0, sum = 0; 2*nn > (1u< 0 && 2*(sum += bins[b]) > (1u<hmask > 0 ? lj_fls(t->hmask)+1 : 0); -} - -/* -- Table getters ------------------------------------------------------- */ - -cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key) -{ - TValue k; - Node *n; - k.n = (lua_Number)key; - n = hashnum(t, &k); - do { - if (tvisnum(&n->key) && n->key.n == k.n) - return &n->val; - } while ((n = nextnode(n))); - return NULL; -} - -cTValue *lj_tab_getstr(GCtab *t, const GCstr *key) -{ - Node *n = hashstr(t, key); - do { - if (tvisstr(&n->key) && strV(&n->key) == key) - return &n->val; - } while ((n = nextnode(n))); - return NULL; -} - -cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key) -{ - if (tvisstr(key)) { - cTValue *tv = lj_tab_getstr(t, strV(key)); - if (tv) - return tv; - } else if (tvisint(key)) { - cTValue *tv = lj_tab_getint(t, intV(key)); - if (tv) - return tv; - } else if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if (nk == (lua_Number)k) { - cTValue *tv = lj_tab_getint(t, k); - if (tv) - return tv; - } else { - goto genlookup; /* Else use the generic lookup. */ - } - } else if (!tvisnil(key)) { - Node *n; - genlookup: - n = hashkey(t, key); - do { - if (lj_obj_equal(&n->key, key)) - return &n->val; - } while ((n = nextnode(n))); - } - return niltv(L); -} - -/* -- Table setters ------------------------------------------------------- */ - -/* Insert new key. Use Brent's variation to optimize the chain length. */ -TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key) -{ - Node *n = hashkey(t, key); - if (!tvisnil(&n->val) || t->hmask == 0) { - Node *nodebase = noderef(t->node); - Node *collide, *freenode = getfreetop(t, nodebase); - lj_assertL(freenode >= nodebase && freenode <= nodebase+t->hmask+1, - "bad freenode"); - do { - if (freenode == nodebase) { /* No free node found? */ - rehashtab(L, t, key); /* Rehash table. */ - return lj_tab_set(L, t, key); /* Retry key insertion. */ - } - } while (!tvisnil(&(--freenode)->key)); - setfreetop(t, nodebase, freenode); - lj_assertL(freenode != &G(L)->nilnode, "store to fallback hash"); - collide = hashkey(t, &n->key); - if (collide != n) { /* Colliding node not the main node? */ - while (noderef(collide->next) != n) /* Find predecessor. */ - collide = nextnode(collide); - setmref(collide->next, freenode); /* Relink chain. */ - /* Copy colliding node into free node and free main node. */ - freenode->val = n->val; - freenode->key = n->key; - freenode->next = n->next; - setmref(n->next, NULL); - setnilV(&n->val); - /* Rechain pseudo-resurrected string keys with colliding hashes. */ - while (nextnode(freenode)) { - Node *nn = nextnode(freenode); - if (!tvisnil(&nn->val) && hashkey(t, &nn->key) == n) { - freenode->next = nn->next; - nn->next = n->next; - setmref(n->next, nn); - /* - ** Rechaining a resurrected string key creates a new dilemma: - ** Another string key may have originally been resurrected via - ** _any_ of the previous nodes as a chain anchor. Including - ** a node that had to be moved, which makes them unreachable. - ** It's not feasible to check for all previous nodes, so rechain - ** any string key that's currently in a non-main positions. - */ - while ((nn = nextnode(freenode))) { - if (!tvisnil(&nn->val)) { - Node *mn = hashkey(t, &nn->key); - if (mn != freenode && mn != nn) { - freenode->next = nn->next; - nn->next = mn->next; - setmref(mn->next, nn); - } else { - freenode = nn; - } - } else { - freenode = nn; - } - } - break; - } else { - freenode = nn; - } - } - } else { /* Otherwise use free node. */ - setmrefr(freenode->next, n->next); /* Insert into chain. */ - setmref(n->next, freenode); - n = freenode; - } - } - n->key.u64 = key->u64; - if (LJ_UNLIKELY(tvismzero(&n->key))) - n->key.u64 = 0; - lj_gc_anybarriert(L, t); - lj_assertL(tvisnil(&n->val), "new hash slot is not empty"); - return &n->val; -} - -TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key) -{ - TValue k; - Node *n; - k.n = (lua_Number)key; - n = hashnum(t, &k); - do { - if (tvisnum(&n->key) && n->key.n == k.n) - return &n->val; - } while ((n = nextnode(n))); - return lj_tab_newkey(L, t, &k); -} - -TValue *lj_tab_setstr(lua_State *L, GCtab *t, const GCstr *key) -{ - TValue k; - Node *n = hashstr(t, key); - do { - if (tvisstr(&n->key) && strV(&n->key) == key) - return &n->val; - } while ((n = nextnode(n))); - setstrV(L, &k, key); - return lj_tab_newkey(L, t, &k); -} - -TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key) -{ - Node *n; - t->nomm = 0; /* Invalidate negative metamethod cache. */ - if (tvisstr(key)) { - return lj_tab_setstr(L, t, strV(key)); - } else if (tvisint(key)) { - return lj_tab_setint(L, t, intV(key)); - } else if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if (nk == (lua_Number)k) - return lj_tab_setint(L, t, k); - if (tvisnan(key)) - lj_err_msg(L, LJ_ERR_NANIDX); - /* Else use the generic lookup. */ - } else if (tvisnil(key)) { - lj_err_msg(L, LJ_ERR_NILIDX); - } - n = hashkey(t, key); - do { - if (lj_obj_equal(&n->key, key)) - return &n->val; - } while ((n = nextnode(n))); - return lj_tab_newkey(L, t, key); -} - -/* -- Table traversal ----------------------------------------------------- */ - -/* Table traversal indexes: -** -** Array key index: [0 .. t->asize-1] -** Hash key index: [t->asize .. t->asize+t->hmask] -** Invalid key: ~0 -*/ - -/* Get the successor traversal index of a key. */ -uint32_t LJ_FASTCALL lj_tab_keyindex(GCtab *t, cTValue *key) -{ - TValue tmp; - if (tvisint(key)) { - int32_t k = intV(key); - if ((uint32_t)k < t->asize) - return (uint32_t)k + 1; - setnumV(&tmp, (lua_Number)k); - key = &tmp; - } else if (tvisnum(key)) { - lua_Number nk = numV(key); - int32_t k = lj_num2int(nk); - if ((uint32_t)k < t->asize && nk == (lua_Number)k) - return (uint32_t)k + 1; - } - if (!tvisnil(key)) { - Node *n = hashkey(t, key); - do { - if (lj_obj_equal(&n->key, key)) - return t->asize + (uint32_t)((n+1) - noderef(t->node)); - } while ((n = nextnode(n))); - if (key->u32.hi == LJ_KEYINDEX) /* Despecialized ITERN while running. */ - return key->u32.lo; - return ~0u; /* Invalid key to next. */ - } - return 0; /* A nil key starts the traversal. */ -} - -/* Get the next key/value pair of a table traversal. */ -int lj_tab_next(GCtab *t, cTValue *key, TValue *o) -{ - uint32_t idx = lj_tab_keyindex(t, key); /* Find successor index of key. */ - /* First traverse the array part. */ - for (; idx < t->asize; idx++) { - cTValue *a = arrayslot(t, idx); - if (LJ_LIKELY(!tvisnil(a))) { - setintV(o, idx); - o[1] = *a; - return 1; - } - } - idx -= t->asize; - /* Then traverse the hash part. */ - for (; idx <= t->hmask; idx++) { - Node *n = &noderef(t->node)[idx]; - if (!tvisnil(&n->val)) { - o[0] = n->key; - o[1] = n->val; - return 1; - } - } - return (int32_t)idx < 0 ? -1 : 0; /* Invalid key or end of traversal. */ -} - -/* -- Table length calculation -------------------------------------------- */ - -/* Compute table length. Slow path with mixed array/hash lookups. */ -LJ_NOINLINE static MSize tab_len_slow(GCtab *t, size_t hi) -{ - cTValue *tv; - size_t lo = hi; - hi++; - /* Widening search for an upper bound. */ - while ((tv = lj_tab_getint(t, (int32_t)hi)) && !tvisnil(tv)) { - lo = hi; - hi += hi; - if (hi > (size_t)(INT_MAX-2)) { /* Punt and do a linear search. */ - lo = 1; - while ((tv = lj_tab_getint(t, (int32_t)lo)) && !tvisnil(tv)) lo++; - return (MSize)(lo - 1); - } - } - /* Binary search to find a non-nil to nil transition. */ - while (hi - lo > 1) { - size_t mid = (lo+hi) >> 1; - cTValue *tvb = lj_tab_getint(t, (int32_t)mid); - if (tvb && !tvisnil(tvb)) lo = mid; else hi = mid; - } - return (MSize)lo; -} - -/* Compute table length. Fast path. */ -MSize LJ_FASTCALL lj_tab_len(GCtab *t) -{ - size_t hi = (size_t)t->asize; - if (hi) hi--; - /* In a growing array the last array element is very likely nil. */ - if (hi > 0 && LJ_LIKELY(tvisnil(arrayslot(t, hi)))) { - /* Binary search to find a non-nil to nil transition in the array. */ - size_t lo = 0; - while (hi - lo > 1) { - size_t mid = (lo+hi) >> 1; - if (tvisnil(arrayslot(t, mid))) hi = mid; else lo = mid; - } - return (MSize)lo; - } - /* Without a hash part, there's an implicit nil after the last element. */ - return t->hmask ? tab_len_slow(t, hi) : (MSize)hi; -} - -#if LJ_HASJIT -/* Verify hinted table length or compute it. */ -MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint) -{ - size_t asize = (size_t)t->asize; - cTValue *tv = arrayslot(t, hint); - if (LJ_LIKELY(hint+1 < asize)) { - if (LJ_LIKELY(!tvisnil(tv) && tvisnil(tv+1))) return (MSize)hint; - } else if (hint+1 <= asize && LJ_LIKELY(t->hmask == 0) && !tvisnil(tv)) { - return (MSize)hint; - } - return lj_tab_len(t); -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.h deleted file mode 100644 index 2a3f76b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_tab.h +++ /dev/null @@ -1,96 +0,0 @@ -/* -** Table handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TAB_H -#define _LJ_TAB_H - -#include "lj_obj.h" - -/* Hash constants. Tuned using a brute force search. */ -#define HASH_BIAS (-0x04c11db7) -#define HASH_ROT1 14 -#define HASH_ROT2 5 -#define HASH_ROT3 13 - -/* Scramble the bits of numbers and pointers. */ -static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi) -{ -#if LJ_TARGET_X86ORX64 - /* Prefer variant that compiles well for a 2-operand CPU. */ - lo ^= hi; hi = lj_rol(hi, HASH_ROT1); - lo -= hi; hi = lj_rol(hi, HASH_ROT2); - hi ^= lo; hi -= lj_rol(lo, HASH_ROT3); -#else - lo ^= hi; - lo = lo - lj_rol(hi, HASH_ROT1); - hi = lo ^ lj_rol(hi, HASH_ROT1 + HASH_ROT2); - hi = hi - lj_rol(lo, HASH_ROT3); -#endif - return hi; -} - -/* Hash values are masked with the table hash mask and used as an index. */ -static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash) -{ - Node *n = noderef(t->node); - return &n[hash & t->hmask]; -} - -/* String IDs are generated when a string is interned. */ -#define hashstr(t, s) hashmask(t, (s)->sid) - -#define hashlohi(t, lo, hi) hashmask((t), hashrot((lo), (hi))) -#define hashnum(t, o) hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1)) -#if LJ_GC64 -#define hashgcref(t, r) \ - hashlohi((t), (uint32_t)gcrefu(r), (uint32_t)(gcrefu(r) >> 32)) -#else -#define hashgcref(t, r) hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS) -#endif - -#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0) - -LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits); -LJ_FUNC GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h); -#if LJ_HASJIT -LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize); -#endif -LJ_FUNCA GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt); -LJ_FUNC void LJ_FASTCALL lj_tab_clear(GCtab *t); -LJ_FUNC void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t); -#if LJ_HASFFI -LJ_FUNC void lj_tab_rehash(lua_State *L, GCtab *t); -#endif -LJ_FUNC void lj_tab_resize(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits); -LJ_FUNCA void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize); - -/* Caveat: all getters except lj_tab_get() can return NULL! */ - -LJ_FUNCA cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key); -LJ_FUNC cTValue *lj_tab_getstr(GCtab *t, const GCstr *key); -LJ_FUNCA cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key); - -/* Caveat: all setters require a write barrier for the stored value. */ - -LJ_FUNCA TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key); -LJ_FUNCA TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key); -LJ_FUNC TValue *lj_tab_setstr(lua_State *L, GCtab *t, const GCstr *key); -LJ_FUNC TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key); - -#define inarray(t, key) ((MSize)(key) < (MSize)(t)->asize) -#define arrayslot(t, i) (&tvref((t)->array)[(i)]) -#define lj_tab_getint(t, key) \ - (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_getinth((t), (key))) -#define lj_tab_setint(L, t, key) \ - (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_setinth(L, (t), (key))) - -LJ_FUNC uint32_t LJ_FASTCALL lj_tab_keyindex(GCtab *t, cTValue *key); -LJ_FUNCA int lj_tab_next(GCtab *t, cTValue *key, TValue *o); -LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t); -#if LJ_HASJIT -LJ_FUNC MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target.h deleted file mode 100644 index 1971692..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target.h +++ /dev/null @@ -1,165 +0,0 @@ -/* -** Definitions for target CPU. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_H -#define _LJ_TARGET_H - -#include "lj_def.h" -#include "lj_arch.h" - -/* -- Registers and spill slots ------------------------------------------- */ - -/* Register type (uint8_t in ir->r). */ -typedef uint32_t Reg; - -/* The hi-bit is NOT set for an allocated register. This means the value -** can be directly used without masking. The hi-bit is set for a register -** allocation hint or for RID_INIT, RID_SINK or RID_SUNK. -*/ -#define RID_NONE 0x80 -#define RID_MASK 0x7f -#define RID_INIT (RID_NONE|RID_MASK) -#define RID_SINK (RID_INIT-1) -#define RID_SUNK (RID_INIT-2) - -#define ra_noreg(r) ((r) & RID_NONE) -#define ra_hasreg(r) (!((r) & RID_NONE)) - -/* The ra_hashint() macro assumes a previous test for ra_noreg(). */ -#define ra_hashint(r) ((r) < RID_SUNK) -#define ra_gethint(r) ((Reg)((r) & RID_MASK)) -#define ra_sethint(rr, r) rr = (uint8_t)((r)|RID_NONE) -#define ra_samehint(r1, r2) (ra_gethint((r1)^(r2)) == 0) - -/* Spill slot 0 means no spill slot has been allocated. */ -#define SPS_NONE 0 - -#define ra_hasspill(s) ((s) != SPS_NONE) - -/* Combined register and spill slot (uint16_t in ir->prev). */ -typedef uint32_t RegSP; - -#define REGSP(r, s) ((r) + ((s) << 8)) -#define REGSP_HINT(r) ((r)|RID_NONE) -#define REGSP_INIT REGSP(RID_INIT, 0) - -#define regsp_reg(rs) ((rs) & 255) -#define regsp_spill(rs) ((rs) >> 8) -#define regsp_used(rs) \ - (((rs) & ~REGSP(RID_MASK, 0)) != REGSP(RID_NONE, 0)) - -/* -- Register sets ------------------------------------------------------- */ - -/* Bitset for registers. 32 registers suffice for most architectures. -** Note that one set holds bits for both GPRs and FPRs. -*/ -#if LJ_TARGET_PPC || LJ_TARGET_MIPS || LJ_TARGET_ARM64 -typedef uint64_t RegSet; -#else -typedef uint32_t RegSet; -#endif - -#define RID2RSET(r) (((RegSet)1) << (r)) -#define RSET_EMPTY ((RegSet)0) -#define RSET_RANGE(lo, hi) ((RID2RSET((hi)-(lo))-1) << (lo)) - -#define rset_test(rs, r) ((int)((rs) >> (r)) & 1) -#define rset_set(rs, r) (rs |= RID2RSET(r)) -#define rset_clear(rs, r) (rs &= ~RID2RSET(r)) -#define rset_exclude(rs, r) (rs & ~RID2RSET(r)) -#if LJ_TARGET_PPC || LJ_TARGET_MIPS || LJ_TARGET_ARM64 -#define rset_picktop(rs) ((Reg)(__builtin_clzll(rs)^63)) -#define rset_pickbot(rs) ((Reg)__builtin_ctzll(rs)) -#else -#define rset_picktop(rs) ((Reg)lj_fls(rs)) -#define rset_pickbot(rs) ((Reg)lj_ffs(rs)) -#endif - -/* -- Register allocation cost -------------------------------------------- */ - -/* The register allocation heuristic keeps track of the cost for allocating -** a specific register: -** -** A free register (obviously) has a cost of 0 and a 1-bit in the free mask. -** -** An already allocated register has the (non-zero) IR reference in the lowest -** bits and the result of a blended cost-model in the higher bits. -** -** The allocator first checks the free mask for a hit. Otherwise an (unrolled) -** linear search for the minimum cost is used. The search doesn't need to -** keep track of the position of the minimum, which makes it very fast. -** The lowest bits of the minimum cost show the desired IR reference whose -** register is the one to evict. -** -** Without the cost-model this degenerates to the standard heuristics for -** (reverse) linear-scan register allocation. Since code generation is done -** in reverse, a live interval extends from the last use to the first def. -** For an SSA IR the IR reference is the first (and only) def and thus -** trivially marks the end of the interval. The LSRA heuristics says to pick -** the register whose live interval has the furthest extent, i.e. the lowest -** IR reference in our case. -** -** A cost-model should take into account other factors, like spill-cost and -** restore- or rematerialization-cost, which depend on the kind of instruction. -** E.g. constants have zero spill costs, variant instructions have higher -** costs than invariants and PHIs should preferably never be spilled. -** -** Here's a first cut at simple, but effective blended cost-model for R-LSRA: -** - Due to careful design of the IR, constants already have lower IR -** references than invariants and invariants have lower IR references -** than variants. -** - The cost in the upper 16 bits is the sum of the IR reference and a -** weighted score. The score currently only takes into account whether -** the IRT_ISPHI bit is set in the instruction type. -** - The PHI weight is the minimum distance (in IR instructions) a PHI -** reference has to be further apart from a non-PHI reference to be spilled. -** - It should be a power of two (for speed) and must be between 2 and 32768. -** Good values for the PHI weight seem to be between 40 and 150. -** - Further study is required. -*/ -#define REGCOST_PHI_WEIGHT 64 - -/* Cost for allocating a specific register. */ -typedef uint32_t RegCost; - -/* Note: assumes 16 bit IRRef1. */ -#define REGCOST(cost, ref) ((RegCost)(ref) + ((RegCost)(cost) << 16)) -#define regcost_ref(rc) ((IRRef1)(rc)) - -#define REGCOST_T(t) \ - ((RegCost)((t)&IRT_ISPHI) * (((RegCost)(REGCOST_PHI_WEIGHT)<<16)/IRT_ISPHI)) -#define REGCOST_REF_T(ref, t) (REGCOST((ref), (ref)) + REGCOST_T((t))) - -/* -- Target-specific definitions ----------------------------------------- */ - -#if LJ_TARGET_X86ORX64 -#include "lj_target_x86.h" -#elif LJ_TARGET_ARM -#include "lj_target_arm.h" -#elif LJ_TARGET_ARM64 -#include "lj_target_arm64.h" -#elif LJ_TARGET_PPC -#include "lj_target_ppc.h" -#elif LJ_TARGET_MIPS -#include "lj_target_mips.h" -#else -#error "Missing include for target CPU" -#endif - -#ifdef EXITSTUBS_PER_GROUP -/* Return the address of an exit stub. */ -static LJ_AINLINE char *exitstub_addr_(char **group, uint32_t exitno) -{ - lj_assertX(group[exitno / EXITSTUBS_PER_GROUP] != NULL, - "exit stub group for exit %d uninitialized", exitno); - return (char *)group[exitno / EXITSTUBS_PER_GROUP] + - EXITSTUB_SPACING*(exitno % EXITSTUBS_PER_GROUP); -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_addr(J, exitno) \ - ((MCode *)exitstub_addr_((char **)((J)->exitstubgroup), (exitno))) -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm.h deleted file mode 100644 index 48f487a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm.h +++ /dev/null @@ -1,271 +0,0 @@ -/* -** Definitions for ARM CPUs. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_ARM_H -#define _LJ_TARGET_ARM_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ - _(R8) _(R9) _(R10) _(R11) _(R12) _(SP) _(LR) _(PC) -#if LJ_SOFTFP -#define FPRDEF(_) -#else -#define FPRDEF(_) \ - _(D0) _(D1) _(D2) _(D3) _(D4) _(D5) _(D6) _(D7) \ - _(D8) _(D9) _(D10) _(D11) _(D12) _(D13) _(D14) _(D15) -#endif -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_TMP = RID_LR, - - /* Calling conventions. */ - RID_RET = RID_R0, - RID_RETLO = RID_R0, - RID_RETHI = RID_R1, -#if LJ_SOFTFP - RID_FPRET = RID_R0, -#else - RID_FPRET = RID_D0, -#endif - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_R9, /* Interpreter BASE. */ - RID_LPC = RID_R6, /* Interpreter PC. */ - RID_DISPATCH = RID_R7, /* Interpreter DISPATCH table. */ - RID_LREG = RID_R8, /* Interpreter L. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_R0, - RID_MAX_GPR = RID_PC+1, - RID_MIN_FPR = RID_MAX_GPR, -#if LJ_SOFTFP - RID_MAX_FPR = RID_MIN_FPR, -#else - RID_MAX_FPR = RID_D15+1, -#endif - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_R0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except sp, lr and pc. */ -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_R12+1)) -#define RSET_GPREVEN \ - (RID2RSET(RID_R0)|RID2RSET(RID_R2)|RID2RSET(RID_R4)|RID2RSET(RID_R6)| \ - RID2RSET(RID_R8)|RID2RSET(RID_R10)) -#define RSET_GPRODD \ - (RID2RSET(RID_R1)|RID2RSET(RID_R3)|RID2RSET(RID_R5)|RID2RSET(RID_R7)| \ - RID2RSET(RID_R9)|RID2RSET(RID_R11)) -#if LJ_SOFTFP -#define RSET_FPR 0 -#else -#define RSET_FPR (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)) -#endif -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -/* ABI-specific register sets. lr is an implicit scratch register. */ -#define RSET_SCRATCH_GPR_ (RSET_RANGE(RID_R0, RID_R3+1)|RID2RSET(RID_R12)) -#ifdef __APPLE__ -#define RSET_SCRATCH_GPR (RSET_SCRATCH_GPR_|RID2RSET(RID_R9)) -#else -#define RSET_SCRATCH_GPR RSET_SCRATCH_GPR_ -#endif -#if LJ_SOFTFP -#define RSET_SCRATCH_FPR 0 -#else -#define RSET_SCRATCH_FPR (RSET_RANGE(RID_D0, RID_D7+1)) -#endif -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_R0 -#define REGARG_LASTGPR RID_R3 -#define REGARG_NUMGPR 4 -#if LJ_ABI_SOFTFP -#define REGARG_FIRSTFPR 0 -#define REGARG_LASTFPR 0 -#define REGARG_NUMFPR 0 -#else -#define REGARG_FIRSTFPR RID_D0 -#define REGARG_LASTFPR RID_D7 -#define REGARG_NUMFPR 8 -#endif - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. -*/ -#define SPS_FIXED 2 -#define SPS_FIRST 2 - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { -#if !LJ_SOFTFP - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ -#endif - int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* PC after instruction that caused an exit. Used to find the trace number. */ -#define EXITSTATE_PCREG RID_PC -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -#define EXITSTUB_SPACING 4 -#define EXITSTUBS_PER_GROUP 32 - -/* -- Instructions -------------------------------------------------------- */ - -/* Instruction fields. */ -#define ARMF_CC(ai, cc) (((ai) ^ ARMI_CCAL) | ((cc) << 28)) -#define ARMF_N(r) ((r) << 16) -#define ARMF_D(r) ((r) << 12) -#define ARMF_S(r) ((r) << 8) -#define ARMF_M(r) (r) -#define ARMF_SH(sh, n) (((sh) << 5) | ((n) << 7)) -#define ARMF_RSH(sh, r) (0x10 | ((sh) << 5) | ARMF_S(r)) - -typedef enum ARMIns { - ARMI_CCAL = 0xe0000000, - ARMI_S = 0x000100000, - ARMI_K12 = 0x02000000, - ARMI_KNEG = 0x00200000, - ARMI_LS_W = 0x00200000, - ARMI_LS_U = 0x00800000, - ARMI_LS_P = 0x01000000, - ARMI_LS_R = 0x02000000, - ARMI_LSX_I = 0x00400000, - - ARMI_AND = 0xe0000000, - ARMI_EOR = 0xe0200000, - ARMI_SUB = 0xe0400000, - ARMI_RSB = 0xe0600000, - ARMI_ADD = 0xe0800000, - ARMI_ADC = 0xe0a00000, - ARMI_SBC = 0xe0c00000, - ARMI_RSC = 0xe0e00000, - ARMI_TST = 0xe1100000, - ARMI_TEQ = 0xe1300000, - ARMI_CMP = 0xe1500000, - ARMI_CMN = 0xe1700000, - ARMI_ORR = 0xe1800000, - ARMI_MOV = 0xe1a00000, - ARMI_BIC = 0xe1c00000, - ARMI_MVN = 0xe1e00000, - - ARMI_NOP = 0xe1a00000, - - ARMI_MUL = 0xe0000090, - ARMI_SMULL = 0xe0c00090, - - ARMI_LDR = 0xe4100000, - ARMI_LDRB = 0xe4500000, - ARMI_LDRH = 0xe01000b0, - ARMI_LDRSB = 0xe01000d0, - ARMI_LDRSH = 0xe01000f0, - ARMI_LDRD = 0xe00000d0, - ARMI_STR = 0xe4000000, - ARMI_STRB = 0xe4400000, - ARMI_STRH = 0xe00000b0, - ARMI_STRD = 0xe00000f0, - ARMI_PUSH = 0xe92d0000, - - ARMI_B = 0xea000000, - ARMI_BL = 0xeb000000, - ARMI_BLX = 0xfa000000, - ARMI_BLXr = 0xe12fff30, - - /* ARMv6 */ - ARMI_REV = 0xe6bf0f30, - ARMI_SXTB = 0xe6af0070, - ARMI_SXTH = 0xe6bf0070, - ARMI_UXTB = 0xe6ef0070, - ARMI_UXTH = 0xe6ff0070, - - /* ARMv6T2 */ - ARMI_MOVW = 0xe3000000, - ARMI_MOVT = 0xe3400000, - ARMI_BFI = 0xe7c00010, - - /* VFP */ - ARMI_VMOV_D = 0xeeb00b40, - ARMI_VMOV_S = 0xeeb00a40, - ARMI_VMOVI_D = 0xeeb00b00, - - ARMI_VMOV_R_S = 0xee100a10, - ARMI_VMOV_S_R = 0xee000a10, - ARMI_VMOV_RR_D = 0xec500b10, - ARMI_VMOV_D_RR = 0xec400b10, - - ARMI_VADD_D = 0xee300b00, - ARMI_VSUB_D = 0xee300b40, - ARMI_VMUL_D = 0xee200b00, - ARMI_VMLA_D = 0xee000b00, - ARMI_VMLS_D = 0xee000b40, - ARMI_VNMLS_D = 0xee100b00, - ARMI_VDIV_D = 0xee800b00, - - ARMI_VABS_D = 0xeeb00bc0, - ARMI_VNEG_D = 0xeeb10b40, - ARMI_VSQRT_D = 0xeeb10bc0, - - ARMI_VCMP_D = 0xeeb40b40, - ARMI_VCMPZ_D = 0xeeb50b40, - - ARMI_VMRS = 0xeef1fa10, - - ARMI_VCVT_S32_F32 = 0xeebd0ac0, - ARMI_VCVT_S32_F64 = 0xeebd0bc0, - ARMI_VCVT_U32_F32 = 0xeebc0ac0, - ARMI_VCVT_U32_F64 = 0xeebc0bc0, - ARMI_VCVT_F32_S32 = 0xeeb80ac0, - ARMI_VCVT_F64_S32 = 0xeeb80bc0, - ARMI_VCVT_F32_U32 = 0xeeb80a40, - ARMI_VCVT_F64_U32 = 0xeeb80b40, - ARMI_VCVT_F32_F64 = 0xeeb70bc0, - ARMI_VCVT_F64_F32 = 0xeeb70ac0, - - ARMI_VLDR_S = 0xed100a00, - ARMI_VLDR_D = 0xed100b00, - ARMI_VSTR_S = 0xed000a00, - ARMI_VSTR_D = 0xed000b00, -} ARMIns; - -typedef enum ARMShift { - ARMSH_LSL, ARMSH_LSR, ARMSH_ASR, ARMSH_ROR -} ARMShift; - -/* ARM condition codes. */ -typedef enum ARMCC { - CC_EQ, CC_NE, CC_CS, CC_CC, CC_MI, CC_PL, CC_VS, CC_VC, - CC_HI, CC_LS, CC_GE, CC_LT, CC_GT, CC_LE, CC_AL, - CC_HS = CC_CS, CC_LO = CC_CC -} ARMCC; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm64.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm64.h deleted file mode 100644 index d45af2e..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_arm64.h +++ /dev/null @@ -1,336 +0,0 @@ -/* -** Definitions for ARM64 CPUs. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_ARM64_H -#define _LJ_TARGET_ARM64_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(X0) _(X1) _(X2) _(X3) _(X4) _(X5) _(X6) _(X7) \ - _(X8) _(X9) _(X10) _(X11) _(X12) _(X13) _(X14) _(X15) \ - _(X16) _(X17) _(X18) _(X19) _(X20) _(X21) _(X22) _(X23) \ - _(X24) _(X25) _(X26) _(X27) _(X28) _(FP) _(LR) _(SP) -#define FPRDEF(_) \ - _(D0) _(D1) _(D2) _(D3) _(D4) _(D5) _(D6) _(D7) \ - _(D8) _(D9) _(D10) _(D11) _(D12) _(D13) _(D14) _(D15) \ - _(D16) _(D17) _(D18) _(D19) _(D20) _(D21) _(D22) _(D23) \ - _(D24) _(D25) _(D26) _(D27) _(D28) _(D29) _(D30) _(D31) -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_TMP = RID_LR, - RID_ZERO = RID_SP, - - /* Calling conventions. */ - RID_RET = RID_X0, - RID_RETLO = RID_X0, - RID_RETHI = RID_X1, - RID_FPRET = RID_D0, - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_X19, /* Interpreter BASE. */ - RID_LPC = RID_X21, /* Interpreter PC. */ - RID_GL = RID_X22, /* Interpreter GL. */ - RID_LREG = RID_X23, /* Interpreter L. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_X0, - RID_MAX_GPR = RID_SP+1, - RID_MIN_FPR = RID_MAX_GPR, - RID_MAX_FPR = RID_D31+1, - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_X0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except for x18, fp, lr and sp. */ -#define RSET_FIXED \ - (RID2RSET(RID_X18)|RID2RSET(RID_FP)|RID2RSET(RID_LR)|RID2RSET(RID_SP)|\ - RID2RSET(RID_GL)) -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) -#define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR) -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -/* lr is an implicit scratch register. */ -#define RSET_SCRATCH_GPR (RSET_RANGE(RID_X0, RID_X17+1)) -#define RSET_SCRATCH_FPR \ - (RSET_RANGE(RID_D0, RID_D7+1)|RSET_RANGE(RID_D16, RID_D31+1)) -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_X0 -#define REGARG_LASTGPR RID_X7 -#define REGARG_NUMGPR 8 -#define REGARG_FIRSTFPR RID_D0 -#define REGARG_LASTFPR RID_D7 -#define REGARG_NUMFPR 8 - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the vm_arm64.dasc file. -** Pre-allocate some slots to avoid sp adjust in every root trace. -** -** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. -*/ -#define SPS_FIXED 4 -#define SPS_FIRST 2 - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ - intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -/* Return the address of a per-trace exit stub. */ -static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p, uint32_t exitno) -{ - while (*p == (LJ_LE ? 0xd503201f : 0x1f2003d5)) p++; /* Skip A64I_NOP. */ - return p + 3 + exitno; -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_trace_addr(T, exitno) \ - exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode), (exitno)) - -/* -- Instructions -------------------------------------------------------- */ - -/* ARM64 instructions are always little-endian. Swap for ARM64BE. */ -#if LJ_BE -#define A64I_LE(x) (lj_bswap(x)) -#else -#define A64I_LE(x) (x) -#endif - -/* Instruction fields. */ -#define A64F_D(r) (r) -#define A64F_N(r) ((r) << 5) -#define A64F_A(r) ((r) << 10) -#define A64F_M(r) ((r) << 16) -#define A64F_IMMS(x) ((x) << 10) -#define A64F_IMMR(x) ((x) << 16) -#define A64F_U16(x) ((x) << 5) -#define A64F_U12(x) ((x) << 10) -#define A64F_S26(x) (((uint32_t)(x) & 0x03ffffffu)) -#define A64F_S19(x) (((uint32_t)(x) & 0x7ffffu) << 5) -#define A64F_S14(x) (((uint32_t)(x) & 0x3fffu) << 5) -#define A64F_S9(x) ((x) << 12) -#define A64F_BIT(x) ((x) << 19) -#define A64F_SH(sh, x) (((sh) << 22) | ((x) << 10)) -#define A64F_EX(ex) (A64I_EX | ((ex) << 13)) -#define A64F_EXSH(ex,x) (A64I_EX | ((ex) << 13) | ((x) << 10)) -#define A64F_FP8(x) ((x) << 13) -#define A64F_CC(cc) ((cc) << 12) -#define A64F_LSL16(x) (((x) / 16) << 21) -#define A64F_BSH(sh) ((sh) << 10) - -/* Check for valid field range. */ -#define A64F_S_OK(x, b) ((((x) + (1 << (b-1))) >> (b)) == 0) - -typedef enum A64Ins { - A64I_S = 0x20000000, - A64I_X = 0x80000000, - A64I_EX = 0x00200000, - A64I_ON = 0x00200000, - A64I_K12 = 0x1a000000, - A64I_K13 = 0x18000000, - A64I_LS_U = 0x01000000, - A64I_LS_S = 0x00800000, - A64I_LS_R = 0x01200800, - A64I_LS_SH = 0x00001000, - A64I_LS_UXTWx = 0x00004000, - A64I_LS_SXTWx = 0x0000c000, - A64I_LS_SXTXx = 0x0000e000, - A64I_LS_LSLx = 0x00006000, - - A64I_ADDw = 0x0b000000, - A64I_ADDx = 0x8b000000, - A64I_ADDSw = 0x2b000000, - A64I_ADDSx = 0xab000000, - A64I_NEGw = 0x4b0003e0, - A64I_NEGx = 0xcb0003e0, - A64I_SUBw = 0x4b000000, - A64I_SUBx = 0xcb000000, - A64I_SUBSw = 0x6b000000, - A64I_SUBSx = 0xeb000000, - - A64I_MULw = 0x1b007c00, - A64I_MULx = 0x9b007c00, - A64I_SMULL = 0x9b207c00, - - A64I_ANDw = 0x0a000000, - A64I_ANDx = 0x8a000000, - A64I_ANDSw = 0x6a000000, - A64I_ANDSx = 0xea000000, - A64I_EORw = 0x4a000000, - A64I_EORx = 0xca000000, - A64I_ORRw = 0x2a000000, - A64I_ORRx = 0xaa000000, - A64I_TSTw = 0x6a00001f, - A64I_TSTx = 0xea00001f, - - A64I_CMPw = 0x6b00001f, - A64I_CMPx = 0xeb00001f, - A64I_CMNw = 0x2b00001f, - A64I_CMNx = 0xab00001f, - A64I_CCMPw = 0x7a400000, - A64I_CCMPx = 0xfa400000, - A64I_CSELw = 0x1a800000, - A64I_CSELx = 0x9a800000, - - A64I_ASRw = 0x13007c00, - A64I_ASRx = 0x9340fc00, - A64I_LSLx = 0xd3400000, - A64I_LSRx = 0xd340fc00, - A64I_SHRw = 0x1ac02000, - A64I_SHRx = 0x9ac02000, /* lsl/lsr/asr/ror x0, x0, x0 */ - A64I_REVw = 0x5ac00800, - A64I_REVx = 0xdac00c00, - - A64I_EXTRw = 0x13800000, - A64I_EXTRx = 0x93c00000, - A64I_BFMw = 0x33000000, - A64I_BFMx = 0xb3400000, - A64I_SBFMw = 0x13000000, - A64I_SBFMx = 0x93400000, - A64I_SXTBw = 0x13001c00, - A64I_SXTHw = 0x13003c00, - A64I_SXTW = 0x93407c00, - A64I_UBFMw = 0x53000000, - A64I_UBFMx = 0xd3400000, - A64I_UXTBw = 0x53001c00, - A64I_UXTHw = 0x53003c00, - - A64I_MOVw = 0x2a0003e0, - A64I_MOVx = 0xaa0003e0, - A64I_MVNw = 0x2a2003e0, - A64I_MVNx = 0xaa2003e0, - A64I_MOVKw = 0x72800000, - A64I_MOVKx = 0xf2800000, - A64I_MOVZw = 0x52800000, - A64I_MOVZx = 0xd2800000, - A64I_MOVNw = 0x12800000, - A64I_MOVNx = 0x92800000, - - A64I_LDRB = 0x39400000, - A64I_LDRH = 0x79400000, - A64I_LDRw = 0xb9400000, - A64I_LDRx = 0xf9400000, - A64I_LDRLw = 0x18000000, - A64I_LDRLx = 0x58000000, - A64I_STRB = 0x39000000, - A64I_STRH = 0x79000000, - A64I_STRw = 0xb9000000, - A64I_STRx = 0xf9000000, - A64I_STPw = 0x29000000, - A64I_STPx = 0xa9000000, - A64I_LDPw = 0x29400000, - A64I_LDPx = 0xa9400000, - - A64I_B = 0x14000000, - A64I_BCC = 0x54000000, - A64I_BL = 0x94000000, - A64I_BR = 0xd61f0000, - A64I_BLR = 0xd63f0000, - A64I_TBZ = 0x36000000, - A64I_TBNZ = 0x37000000, - A64I_CBZ = 0x34000000, - A64I_CBNZ = 0x35000000, - - A64I_NOP = 0xd503201f, - - /* FP */ - A64I_FADDd = 0x1e602800, - A64I_FSUBd = 0x1e603800, - A64I_FMADDd = 0x1f400000, - A64I_FMSUBd = 0x1f408000, - A64I_FNMADDd = 0x1f600000, - A64I_FNMSUBd = 0x1f608000, - A64I_FMULd = 0x1e600800, - A64I_FDIVd = 0x1e601800, - A64I_FNEGd = 0x1e614000, - A64I_FABS = 0x1e60c000, - A64I_FSQRTd = 0x1e61c000, - A64I_LDRs = 0xbd400000, - A64I_LDRd = 0xfd400000, - A64I_STRs = 0xbd000000, - A64I_STRd = 0xfd000000, - A64I_LDPs = 0x2d400000, - A64I_LDPd = 0x6d400000, - A64I_STPs = 0x2d000000, - A64I_STPd = 0x6d000000, - A64I_FCMPd = 0x1e602000, - A64I_FCMPZd = 0x1e602008, - A64I_FCSELd = 0x1e600c00, - A64I_FRINTMd = 0x1e654000, - A64I_FRINTPd = 0x1e64c000, - A64I_FRINTZd = 0x1e65c000, - - A64I_FCVT_F32_F64 = 0x1e624000, - A64I_FCVT_F64_F32 = 0x1e22c000, - A64I_FCVT_F32_S32 = 0x1e220000, - A64I_FCVT_F64_S32 = 0x1e620000, - A64I_FCVT_F32_U32 = 0x1e230000, - A64I_FCVT_F64_U32 = 0x1e630000, - A64I_FCVT_F32_S64 = 0x9e220000, - A64I_FCVT_F64_S64 = 0x9e620000, - A64I_FCVT_F32_U64 = 0x9e230000, - A64I_FCVT_F64_U64 = 0x9e630000, - A64I_FCVT_S32_F64 = 0x1e780000, - A64I_FCVT_S32_F32 = 0x1e380000, - A64I_FCVT_U32_F64 = 0x1e790000, - A64I_FCVT_U32_F32 = 0x1e390000, - A64I_FCVT_S64_F64 = 0x9e780000, - A64I_FCVT_S64_F32 = 0x9e380000, - A64I_FCVT_U64_F64 = 0x9e790000, - A64I_FCVT_U64_F32 = 0x9e390000, - - A64I_FMOV_S = 0x1e204000, - A64I_FMOV_D = 0x1e604000, - A64I_FMOV_R_S = 0x1e260000, - A64I_FMOV_S_R = 0x1e270000, - A64I_FMOV_R_D = 0x9e660000, - A64I_FMOV_D_R = 0x9e670000, - A64I_FMOV_DI = 0x1e601000, -} A64Ins; - -typedef enum A64Shift { - A64SH_LSL, A64SH_LSR, A64SH_ASR, A64SH_ROR -} A64Shift; - -typedef enum A64Extend { - A64EX_UXTB, A64EX_UXTH, A64EX_UXTW, A64EX_UXTX, - A64EX_SXTB, A64EX_SXTH, A64EX_SXTW, A64EX_SXTX, -} A64Extend; - -/* ARM condition codes. */ -typedef enum A64CC { - CC_EQ, CC_NE, CC_CS, CC_CC, CC_MI, CC_PL, CC_VS, CC_VC, - CC_HI, CC_LS, CC_GE, CC_LT, CC_GT, CC_LE, CC_AL, - CC_HS = CC_CS, CC_LO = CC_CC -} A64CC; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_mips.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_mips.h deleted file mode 100644 index da72d61..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_mips.h +++ /dev/null @@ -1,417 +0,0 @@ -/* -** Definitions for MIPS CPUs. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_MIPS_H -#define _LJ_TARGET_MIPS_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ - _(R8) _(R9) _(R10) _(R11) _(R12) _(R13) _(R14) _(R15) \ - _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ - _(R24) _(R25) _(SYS1) _(SYS2) _(R28) _(SP) _(R30) _(RA) -#if LJ_SOFTFP -#define FPRDEF(_) -#else -#define FPRDEF(_) \ - _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ - _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ - _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ - _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) -#endif -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_ZERO = RID_R0, - RID_TMP = RID_RA, - RID_GP = RID_R28, - - /* Calling conventions. */ - RID_RET = RID_R2, -#if LJ_LE - RID_RETHI = RID_R3, - RID_RETLO = RID_R2, -#else - RID_RETHI = RID_R2, - RID_RETLO = RID_R3, -#endif -#if LJ_SOFTFP - RID_FPRET = RID_R2, -#else - RID_FPRET = RID_F0, -#endif - RID_CFUNCADDR = RID_R25, - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_R16, /* Interpreter BASE. */ - RID_LPC = RID_R18, /* Interpreter PC. */ - RID_DISPATCH = RID_R19, /* Interpreter DISPATCH table. */ - RID_LREG = RID_R20, /* Interpreter L. */ - RID_JGL = RID_R30, /* On-trace: global_State + 32768. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_R0, - RID_MAX_GPR = RID_RA+1, - RID_MIN_FPR = RID_MAX_GPR, -#if LJ_SOFTFP - RID_MAX_FPR = RID_MIN_FPR, -#else - RID_MAX_FPR = RID_F31+1, -#endif - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR /* Only even regs are used. */ -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_R0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except ZERO, TMP, SP, SYS1, SYS2, JGL and GP. */ -#define RSET_FIXED \ - (RID2RSET(RID_ZERO)|RID2RSET(RID_TMP)|RID2RSET(RID_SP)|\ - RID2RSET(RID_SYS1)|RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)|RID2RSET(RID_GP)) -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) -#if LJ_SOFTFP -#define RSET_FPR 0 -#else -#if LJ_32 -#define RSET_FPR \ - (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\ - RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\ - RID2RSET(RID_F16)|RID2RSET(RID_F18)|RID2RSET(RID_F20)|RID2RSET(RID_F22)|\ - RID2RSET(RID_F24)|RID2RSET(RID_F26)|RID2RSET(RID_F28)|RID2RSET(RID_F30)) -#else -#define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR) -#endif -#endif -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -#define RSET_SCRATCH_GPR \ - (RSET_RANGE(RID_R1, RID_R15+1)|\ - RID2RSET(RID_R24)|RID2RSET(RID_R25)) -#if LJ_SOFTFP -#define RSET_SCRATCH_FPR 0 -#else -#if LJ_32 -#define RSET_SCRATCH_FPR \ - (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\ - RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\ - RID2RSET(RID_F16)|RID2RSET(RID_F18)) -#else -#define RSET_SCRATCH_FPR RSET_RANGE(RID_F0, RID_F24) -#endif -#endif -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_R4 -#if LJ_32 -#define REGARG_LASTGPR RID_R7 -#define REGARG_NUMGPR 4 -#else -#define REGARG_LASTGPR RID_R11 -#define REGARG_NUMGPR 8 -#endif -#if LJ_ABI_SOFTFP -#define REGARG_FIRSTFPR 0 -#define REGARG_LASTFPR 0 -#define REGARG_NUMFPR 0 -#else -#define REGARG_FIRSTFPR RID_F12 -#if LJ_32 -#define REGARG_LASTFPR RID_F14 -#define REGARG_NUMFPR 2 -#else -#define REGARG_LASTFPR RID_F19 -#define REGARG_NUMFPR 8 -#endif -#endif - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. -*/ -#if LJ_32 -#define SPS_FIXED 5 -#else -#define SPS_FIXED 4 -#endif -#define SPS_FIRST 4 - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { -#if !LJ_SOFTFP - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ -#endif - intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -/* Return the address of a per-trace exit stub. */ -static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p) -{ - while (*p == 0x00000000) p++; /* Skip MIPSI_NOP. */ - return p; -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_trace_addr(T, exitno) \ - exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode)) - -/* -- Instructions -------------------------------------------------------- */ - -/* Instruction fields. */ -#define MIPSF_S(r) ((r) << 21) -#define MIPSF_T(r) ((r) << 16) -#define MIPSF_D(r) ((r) << 11) -#define MIPSF_R(r) ((r) << 21) -#define MIPSF_H(r) ((r) << 16) -#define MIPSF_G(r) ((r) << 11) -#define MIPSF_F(r) ((r) << 6) -#define MIPSF_A(n) ((n) << 6) -#define MIPSF_M(n) ((n) << 11) -#define MIPSF_L(n) ((n) << 6) - -typedef enum MIPSIns { - MIPSI_D = 0x38, - MIPSI_DV = 0x10, - MIPSI_D32 = 0x3c, - /* Integer instructions. */ - MIPSI_MOVE = 0x00000025, - MIPSI_NOP = 0x00000000, - - MIPSI_LI = 0x24000000, - MIPSI_LU = 0x34000000, - MIPSI_LUI = 0x3c000000, - - MIPSI_AND = 0x00000024, - MIPSI_ANDI = 0x30000000, - MIPSI_OR = 0x00000025, - MIPSI_ORI = 0x34000000, - MIPSI_XOR = 0x00000026, - MIPSI_XORI = 0x38000000, - MIPSI_NOR = 0x00000027, - - MIPSI_SLT = 0x0000002a, - MIPSI_SLTU = 0x0000002b, - MIPSI_SLTI = 0x28000000, - MIPSI_SLTIU = 0x2c000000, - - MIPSI_ADDU = 0x00000021, - MIPSI_ADDIU = 0x24000000, - MIPSI_SUB = 0x00000022, - MIPSI_SUBU = 0x00000023, - -#if !LJ_TARGET_MIPSR6 - MIPSI_MUL = 0x70000002, - MIPSI_DIV = 0x0000001a, - MIPSI_DIVU = 0x0000001b, - - MIPSI_MOVZ = 0x0000000a, - MIPSI_MOVN = 0x0000000b, - MIPSI_MFHI = 0x00000010, - MIPSI_MFLO = 0x00000012, - MIPSI_MULT = 0x00000018, -#else - MIPSI_MUL = 0x00000098, - MIPSI_MUH = 0x000000d8, - MIPSI_DIV = 0x0000009a, - MIPSI_DIVU = 0x0000009b, - - MIPSI_SELEQZ = 0x00000035, - MIPSI_SELNEZ = 0x00000037, -#endif - - MIPSI_SLL = 0x00000000, - MIPSI_SRL = 0x00000002, - MIPSI_SRA = 0x00000003, - MIPSI_ROTR = 0x00200002, /* MIPSXXR2 */ - MIPSI_DROTR = 0x0020003a, - MIPSI_DROTR32 = 0x0020003e, - MIPSI_SLLV = 0x00000004, - MIPSI_SRLV = 0x00000006, - MIPSI_SRAV = 0x00000007, - MIPSI_ROTRV = 0x00000046, /* MIPSXXR2 */ - MIPSI_DROTRV = 0x00000056, - - MIPSI_INS = 0x7c000004, /* MIPSXXR2 */ - - MIPSI_SEB = 0x7c000420, /* MIPSXXR2 */ - MIPSI_SEH = 0x7c000620, /* MIPSXXR2 */ - MIPSI_WSBH = 0x7c0000a0, /* MIPSXXR2 */ - MIPSI_DSBH = 0x7c0000a4, - - MIPSI_B = 0x10000000, - MIPSI_J = 0x08000000, - MIPSI_JAL = 0x0c000000, -#if !LJ_TARGET_MIPSR6 - MIPSI_JALX = 0x74000000, - MIPSI_JR = 0x00000008, -#else - MIPSI_JR = 0x00000009, - MIPSI_BALC = 0xe8000000, -#endif - MIPSI_JALR = 0x0000f809, - - MIPSI_BEQ = 0x10000000, - MIPSI_BNE = 0x14000000, - MIPSI_BLEZ = 0x18000000, - MIPSI_BGTZ = 0x1c000000, - MIPSI_BLTZ = 0x04000000, - MIPSI_BGEZ = 0x04010000, - - /* Load/store instructions. */ - MIPSI_LW = 0x8c000000, - MIPSI_LD = 0xdc000000, - MIPSI_SW = 0xac000000, - MIPSI_SD = 0xfc000000, - MIPSI_LB = 0x80000000, - MIPSI_SB = 0xa0000000, - MIPSI_LH = 0x84000000, - MIPSI_SH = 0xa4000000, - MIPSI_LBU = 0x90000000, - MIPSI_LHU = 0x94000000, - MIPSI_LWC1 = 0xc4000000, - MIPSI_SWC1 = 0xe4000000, - MIPSI_LDC1 = 0xd4000000, - MIPSI_SDC1 = 0xf4000000, - - /* MIPS64 instructions. */ - MIPSI_DADD = 0x0000002c, - MIPSI_DADDU = 0x0000002d, - MIPSI_DADDIU = 0x64000000, - MIPSI_DSUB = 0x0000002e, - MIPSI_DSUBU = 0x0000002f, -#if !LJ_TARGET_MIPSR6 - MIPSI_DDIV = 0x0000001e, - MIPSI_DDIVU = 0x0000001f, - MIPSI_DMULT = 0x0000001c, - MIPSI_DMULTU = 0x0000001d, -#else - MIPSI_DDIV = 0x0000009e, - MIPSI_DMOD = 0x000000de, - MIPSI_DDIVU = 0x0000009f, - MIPSI_DMODU = 0x000000df, - MIPSI_DMUL = 0x0000009c, - MIPSI_DMUH = 0x000000dc, -#endif - - MIPSI_DSLL = 0x00000038, - MIPSI_DSRL = 0x0000003a, - MIPSI_DSLLV = 0x00000014, - MIPSI_DSRLV = 0x00000016, - MIPSI_DSRA = 0x0000003b, - MIPSI_DSRAV = 0x00000017, - MIPSI_DSRA32 = 0x0000003f, - MIPSI_DSLL32 = 0x0000003c, - MIPSI_DSRL32 = 0x0000003e, - MIPSI_DSHD = 0x7c000164, - - MIPSI_AADDU = LJ_32 ? MIPSI_ADDU : MIPSI_DADDU, - MIPSI_AADDIU = LJ_32 ? MIPSI_ADDIU : MIPSI_DADDIU, - MIPSI_ASUBU = LJ_32 ? MIPSI_SUBU : MIPSI_DSUBU, - MIPSI_AL = LJ_32 ? MIPSI_LW : MIPSI_LD, - MIPSI_AS = LJ_32 ? MIPSI_SW : MIPSI_SD, -#if LJ_TARGET_MIPSR6 - MIPSI_LSA = 0x00000005, - MIPSI_DLSA = 0x00000015, - MIPSI_ALSA = LJ_32 ? MIPSI_LSA : MIPSI_DLSA, -#endif - - /* Extract/insert instructions. */ - MIPSI_DEXTM = 0x7c000001, - MIPSI_DEXTU = 0x7c000002, - MIPSI_DEXT = 0x7c000003, - MIPSI_DINSM = 0x7c000005, - MIPSI_DINSU = 0x7c000006, - MIPSI_DINS = 0x7c000007, - - MIPSI_FLOOR_D = 0x4620000b, - - /* FP instructions. */ - MIPSI_MOV_S = 0x46000006, - MIPSI_MOV_D = 0x46200006, -#if !LJ_TARGET_MIPSR6 - MIPSI_MOVT_D = 0x46210011, - MIPSI_MOVF_D = 0x46200011, -#else - MIPSI_MIN_D = 0x4620001C, - MIPSI_MAX_D = 0x4620001E, - MIPSI_SEL_D = 0x46200010, -#endif - - MIPSI_ABS_D = 0x46200005, - MIPSI_NEG_D = 0x46200007, - - MIPSI_ADD_D = 0x46200000, - MIPSI_SUB_D = 0x46200001, - MIPSI_MUL_D = 0x46200002, - MIPSI_DIV_D = 0x46200003, - MIPSI_SQRT_D = 0x46200004, - - MIPSI_ADD_S = 0x46000000, - MIPSI_SUB_S = 0x46000001, - - MIPSI_CVT_D_S = 0x46000021, - MIPSI_CVT_W_S = 0x46000024, - MIPSI_CVT_S_D = 0x46200020, - MIPSI_CVT_W_D = 0x46200024, - MIPSI_CVT_S_W = 0x46800020, - MIPSI_CVT_D_W = 0x46800021, - MIPSI_CVT_S_L = 0x46a00020, - MIPSI_CVT_D_L = 0x46a00021, - - MIPSI_TRUNC_W_S = 0x4600000d, - MIPSI_TRUNC_W_D = 0x4620000d, - MIPSI_TRUNC_L_S = 0x46000009, - MIPSI_TRUNC_L_D = 0x46200009, - MIPSI_FLOOR_W_S = 0x4600000f, - MIPSI_FLOOR_W_D = 0x4620000f, - - MIPSI_MFC1 = 0x44000000, - MIPSI_MTC1 = 0x44800000, - MIPSI_DMTC1 = 0x44a00000, - MIPSI_DMFC1 = 0x44200000, - -#if !LJ_TARGET_MIPSR6 - MIPSI_BC1F = 0x45000000, - MIPSI_BC1T = 0x45010000, - MIPSI_C_EQ_D = 0x46200032, - MIPSI_C_OLT_S = 0x46000034, - MIPSI_C_OLT_D = 0x46200034, - MIPSI_C_ULT_D = 0x46200035, - MIPSI_C_OLE_D = 0x46200036, - MIPSI_C_ULE_D = 0x46200037, -#else - MIPSI_BC1EQZ = 0x45200000, - MIPSI_BC1NEZ = 0x45a00000, - MIPSI_CMP_EQ_D = 0x46a00002, - MIPSI_CMP_LT_S = 0x46800004, - MIPSI_CMP_LT_D = 0x46a00004, -#endif - -} MIPSIns; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_ppc.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_ppc.h deleted file mode 100644 index bc9802a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_ppc.h +++ /dev/null @@ -1,280 +0,0 @@ -/* -** Definitions for PPC CPUs. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_PPC_H -#define _LJ_TARGET_PPC_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#define GPRDEF(_) \ - _(R0) _(SP) _(SYS1) _(R3) _(R4) _(R5) _(R6) _(R7) \ - _(R8) _(R9) _(R10) _(R11) _(R12) _(SYS2) _(R14) _(R15) \ - _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ - _(R24) _(R25) _(R26) _(R27) _(R28) _(R29) _(R30) _(R31) -#define FPRDEF(_) \ - _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ - _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ - _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ - _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) -#define VRIDDEF(_) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_TMP = RID_R0, - - /* Calling conventions. */ - RID_RET = RID_R3, - RID_RETHI = RID_R3, - RID_RETLO = RID_R4, - RID_FPRET = RID_F1, - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_R14, /* Interpreter BASE. */ - RID_LPC = RID_R16, /* Interpreter PC. */ - RID_DISPATCH = RID_R17, /* Interpreter DISPATCH table. */ - RID_LREG = RID_R18, /* Interpreter L. */ - RID_JGL = RID_R31, /* On-trace: global_State + 32768. */ - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_R0, - RID_MAX_GPR = RID_R31+1, - RID_MIN_FPR = RID_F0, - RID_MAX_FPR = RID_F31+1, - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR -}; - -#define RID_NUM_KREF RID_NUM_GPR -#define RID_MIN_KREF RID_R0 - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except TMP, SP, SYS1, SYS2 and JGL. */ -#define RSET_FIXED \ - (RID2RSET(RID_TMP)|RID2RSET(RID_SP)|RID2RSET(RID_SYS1)|\ - RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)) -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) -#define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR) -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -#define RSET_SCRATCH_GPR (RSET_RANGE(RID_R3, RID_R12+1)) -#define RSET_SCRATCH_FPR (RSET_RANGE(RID_F0, RID_F13+1)) -#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) -#define REGARG_FIRSTGPR RID_R3 -#define REGARG_LASTGPR RID_R10 -#define REGARG_NUMGPR 8 -#define REGARG_FIRSTFPR RID_F1 -#define REGARG_LASTFPR RID_F8 -#define REGARG_NUMFPR 8 - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. -** [sp+12] tmplo word \ -** [sp+ 8] tmphi word / tmp dword, parameter area for callee -** [sp+ 4] tmpw, LR of callee -** [sp+ 0] stack chain -*/ -#define SPS_FIXED 7 -#define SPS_FIRST 4 - -/* Stack offsets for temporary slots. Used for FP<->int conversions etc. */ -#define SPOFS_TMPW 4 -#define SPOFS_TMP 8 -#define SPOFS_TMPHI 8 -#define SPOFS_TMPLO 12 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ - intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Highest exit + 1 indicates stack check. */ -#define EXITSTATE_CHECKEXIT 1 - -/* Return the address of a per-trace exit stub. */ -static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p, uint32_t exitno) -{ - while (*p == 0x60000000) p++; /* Skip PPCI_NOP. */ - return p + 3 + exitno; -} -/* Avoid dependence on lj_jit.h if only including lj_target.h. */ -#define exitstub_trace_addr(T, exitno) \ - exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode), (exitno)) - -/* -- Instructions -------------------------------------------------------- */ - -/* Instruction fields. */ -#define PPCF_CC(cc) ((((cc) & 3) << 16) | (((cc) & 4) << 22)) -#define PPCF_T(r) ((r) << 21) -#define PPCF_A(r) ((r) << 16) -#define PPCF_B(r) ((r) << 11) -#define PPCF_C(r) ((r) << 6) -#define PPCF_MB(n) ((n) << 6) -#define PPCF_ME(n) ((n) << 1) -#define PPCF_Y 0x00200000 -#define PPCF_DOT 0x00000001 - -typedef enum PPCIns { - /* Integer instructions. */ - PPCI_MR = 0x7c000378, - PPCI_NOP = 0x60000000, - - PPCI_LI = 0x38000000, - PPCI_LIS = 0x3c000000, - - PPCI_ADD = 0x7c000214, - PPCI_ADDC = 0x7c000014, - PPCI_ADDO = 0x7c000614, - PPCI_ADDE = 0x7c000114, - PPCI_ADDZE = 0x7c000194, - PPCI_ADDME = 0x7c0001d4, - PPCI_ADDI = 0x38000000, - PPCI_ADDIS = 0x3c000000, - PPCI_ADDIC = 0x30000000, - PPCI_ADDICDOT = 0x34000000, - - PPCI_SUBF = 0x7c000050, - PPCI_SUBFC = 0x7c000010, - PPCI_SUBFO = 0x7c000450, - PPCI_SUBFE = 0x7c000110, - PPCI_SUBFZE = 0x7c000190, - PPCI_SUBFME = 0x7c0001d0, - PPCI_SUBFIC = 0x20000000, - - PPCI_NEG = 0x7c0000d0, - - PPCI_AND = 0x7c000038, - PPCI_ANDC = 0x7c000078, - PPCI_NAND = 0x7c0003b8, - PPCI_ANDIDOT = 0x70000000, - PPCI_ANDISDOT = 0x74000000, - - PPCI_OR = 0x7c000378, - PPCI_NOR = 0x7c0000f8, - PPCI_ORI = 0x60000000, - PPCI_ORIS = 0x64000000, - - PPCI_XOR = 0x7c000278, - PPCI_EQV = 0x7c000238, - PPCI_XORI = 0x68000000, - PPCI_XORIS = 0x6c000000, - - PPCI_CMPW = 0x7c000000, - PPCI_CMPLW = 0x7c000040, - PPCI_CMPWI = 0x2c000000, - PPCI_CMPLWI = 0x28000000, - - PPCI_MULLW = 0x7c0001d6, - PPCI_MULLI = 0x1c000000, - PPCI_MULLWO = 0x7c0005d6, - - PPCI_EXTSB = 0x7c000774, - PPCI_EXTSH = 0x7c000734, - - PPCI_SLW = 0x7c000030, - PPCI_SRW = 0x7c000430, - PPCI_SRAW = 0x7c000630, - PPCI_SRAWI = 0x7c000670, - - PPCI_RLWNM = 0x5c000000, - PPCI_RLWINM = 0x54000000, - PPCI_RLWIMI = 0x50000000, - - PPCI_B = 0x48000000, - PPCI_BL = 0x48000001, - PPCI_BC = 0x40800000, - PPCI_BCL = 0x40800001, - PPCI_BCTR = 0x4e800420, - PPCI_BCTRL = 0x4e800421, - - PPCI_CRANDC = 0x4c000102, - PPCI_CRXOR = 0x4c000182, - PPCI_CRAND = 0x4c000202, - PPCI_CREQV = 0x4c000242, - PPCI_CRORC = 0x4c000342, - PPCI_CROR = 0x4c000382, - - PPCI_MFLR = 0x7c0802a6, - PPCI_MTCTR = 0x7c0903a6, - - PPCI_MCRXR = 0x7c000400, - - /* Load/store instructions. */ - PPCI_LWZ = 0x80000000, - PPCI_LBZ = 0x88000000, - PPCI_STW = 0x90000000, - PPCI_STB = 0x98000000, - PPCI_LHZ = 0xa0000000, - PPCI_LHA = 0xa8000000, - PPCI_STH = 0xb0000000, - - PPCI_STWU = 0x94000000, - - PPCI_LFS = 0xc0000000, - PPCI_LFD = 0xc8000000, - PPCI_STFS = 0xd0000000, - PPCI_STFD = 0xd8000000, - - PPCI_LWZX = 0x7c00002e, - PPCI_LBZX = 0x7c0000ae, - PPCI_STWX = 0x7c00012e, - PPCI_STBX = 0x7c0001ae, - PPCI_LHZX = 0x7c00022e, - PPCI_LHAX = 0x7c0002ae, - PPCI_STHX = 0x7c00032e, - - PPCI_LWBRX = 0x7c00042c, - PPCI_STWBRX = 0x7c00052c, - - PPCI_LFSX = 0x7c00042e, - PPCI_LFDX = 0x7c0004ae, - PPCI_STFSX = 0x7c00052e, - PPCI_STFDX = 0x7c0005ae, - - /* FP instructions. */ - PPCI_FMR = 0xfc000090, - PPCI_FNEG = 0xfc000050, - PPCI_FABS = 0xfc000210, - - PPCI_FRSP = 0xfc000018, - PPCI_FCTIWZ = 0xfc00001e, - - PPCI_FADD = 0xfc00002a, - PPCI_FSUB = 0xfc000028, - PPCI_FMUL = 0xfc000032, - PPCI_FDIV = 0xfc000024, - PPCI_FSQRT = 0xfc00002c, - - PPCI_FMADD = 0xfc00003a, - PPCI_FMSUB = 0xfc000038, - PPCI_FNMSUB = 0xfc00003c, - - PPCI_FCMPU = 0xfc000000, - PPCI_FSEL = 0xfc00002e, -} PPCIns; - -typedef enum PPCCC { - CC_GE, CC_LE, CC_NE, CC_NS, CC_LT, CC_GT, CC_EQ, CC_SO -} PPCCC; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_x86.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_x86.h deleted file mode 100644 index 69cb8ca..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_target_x86.h +++ /dev/null @@ -1,357 +0,0 @@ -/* -** Definitions for x86 and x64 CPUs. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TARGET_X86_H -#define _LJ_TARGET_X86_H - -/* -- Registers IDs ------------------------------------------------------- */ - -#if LJ_64 -#define GPRDEF(_) \ - _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) \ - _(R8D) _(R9D) _(R10D) _(R11D) _(R12D) _(R13D) _(R14D) _(R15D) -#define FPRDEF(_) \ - _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) \ - _(XMM8) _(XMM9) _(XMM10) _(XMM11) _(XMM12) _(XMM13) _(XMM14) _(XMM15) -#else -#define GPRDEF(_) \ - _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) -#define FPRDEF(_) \ - _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) -#endif -#define VRIDDEF(_) \ - _(MRM) _(RIP) - -#define RIDENUM(name) RID_##name, - -enum { - GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ - FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ - RID_MAX, - RID_MRM = RID_MAX, /* Pseudo-id for ModRM operand. */ - RID_RIP = RID_MAX+5, /* Pseudo-id for RIP (x64 only), rm bits = 5. */ - - /* Calling conventions. */ - RID_SP = RID_ESP, - RID_RET = RID_EAX, -#if LJ_64 - RID_FPRET = RID_XMM0, -#endif - RID_RETLO = RID_EAX, - RID_RETHI = RID_EDX, - - /* These definitions must match with the *.dasc file(s): */ - RID_BASE = RID_EDX, /* Interpreter BASE. */ -#if LJ_64 && !LJ_ABI_WIN - RID_LPC = RID_EBX, /* Interpreter PC. */ - RID_DISPATCH = RID_R14D, /* Interpreter DISPATCH table. */ -#else - RID_LPC = RID_ESI, /* Interpreter PC. */ - RID_DISPATCH = RID_EBX, /* Interpreter DISPATCH table. */ -#endif - - /* Register ranges [min, max) and number of registers. */ - RID_MIN_GPR = RID_EAX, - RID_MIN_FPR = RID_XMM0, - RID_MAX_GPR = RID_MIN_FPR, - RID_MAX_FPR = RID_MAX, - RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, - RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR, -}; - -/* -- Register sets ------------------------------------------------------- */ - -/* Make use of all registers, except the stack pointer (and maybe DISPATCH). */ -#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) \ - - RID2RSET(RID_ESP) \ - - LJ_GC64*RID2RSET(RID_DISPATCH)) -#define RSET_FPR (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)) -#define RSET_ALL (RSET_GPR|RSET_FPR) -#define RSET_INIT RSET_ALL - -#if LJ_64 -/* Note: this requires the use of FORCE_REX! */ -#define RSET_GPR8 RSET_GPR -#else -#define RSET_GPR8 (RSET_RANGE(RID_EAX, RID_EBX+1)) -#endif - -/* ABI-specific register sets. */ -#define RSET_ACD (RID2RSET(RID_EAX)|RID2RSET(RID_ECX)|RID2RSET(RID_EDX)) -#if LJ_64 -#if LJ_ABI_WIN -/* Windows x64 ABI. */ -#define RSET_SCRATCH \ - (RSET_ACD|RSET_RANGE(RID_R8D, RID_R11D+1)|RSET_RANGE(RID_XMM0, RID_XMM5+1)) -#define REGARG_GPRS \ - (RID_ECX|((RID_EDX|((RID_R8D|(RID_R9D<<5))<<5))<<5)) -#define REGARG_NUMGPR 4 -#define REGARG_NUMFPR 4 -#define REGARG_FIRSTFPR RID_XMM0 -#define REGARG_LASTFPR RID_XMM3 -#define STACKARG_OFS (4*8) -#else -/* The rest of the civilized x64 world has a common ABI. */ -#define RSET_SCRATCH \ - (RSET_ACD|RSET_RANGE(RID_ESI, RID_R11D+1)|RSET_FPR) -#define REGARG_GPRS \ - (RID_EDI|((RID_ESI|((RID_EDX|((RID_ECX|((RID_R8D|(RID_R9D \ - <<5))<<5))<<5))<<5))<<5)) -#define REGARG_NUMGPR 6 -#define REGARG_NUMFPR 8 -#define REGARG_FIRSTFPR RID_XMM0 -#define REGARG_LASTFPR RID_XMM7 -#define STACKARG_OFS 0 -#endif -#else -/* Common x86 ABI. */ -#define RSET_SCRATCH (RSET_ACD|RSET_FPR) -#define REGARG_GPRS (RID_ECX|(RID_EDX<<5)) /* Fastcall only. */ -#define REGARG_NUMGPR 2 /* Fastcall only. */ -#define REGARG_NUMFPR 0 -#define STACKARG_OFS 0 -#endif - -#if LJ_64 -/* Prefer the low 8 regs of each type to reduce REX prefixes. */ -#undef rset_picktop -#define rset_picktop(rs) (lj_fls(lj_bswap(rs)) ^ 0x18) -#endif - -/* -- Spill slots --------------------------------------------------------- */ - -/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. -** -** SPS_FIXED: Available fixed spill slots in interpreter frame. -** This definition must match with the *.dasc file(s). -** -** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. -*/ -#if LJ_64 -#if LJ_ABI_WIN -#define SPS_FIXED (4*2) -#define SPS_FIRST (4*2) /* Don't use callee register save area. */ -#else -#if LJ_GC64 -#define SPS_FIXED 2 -#else -#define SPS_FIXED 4 -#endif -#define SPS_FIRST 2 -#endif -#else -#define SPS_FIXED 6 -#define SPS_FIRST 2 -#endif - -#define SPOFS_TMP 0 - -#define sps_scale(slot) (4 * (int32_t)(slot)) -#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) - -/* -- Exit state ---------------------------------------------------------- */ - -/* This definition must match with the *.dasc file(s). */ -typedef struct { - lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ - intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ - int32_t spill[256]; /* Spill slots. */ -} ExitState; - -/* Limited by the range of a short fwd jump (127): (2+2)*(32-1)-2 = 122. */ -#define EXITSTUB_SPACING (2+2) -#define EXITSTUBS_PER_GROUP 32 - -#define EXITTRACE_VMSTATE 1 /* g->vmstate has traceno on exit. */ - -/* -- x86 ModRM operand encoding ------------------------------------------ */ - -typedef enum { - XM_OFS0 = 0x00, XM_OFS8 = 0x40, XM_OFS32 = 0x80, XM_REG = 0xc0, - XM_SCALE1 = 0x00, XM_SCALE2 = 0x40, XM_SCALE4 = 0x80, XM_SCALE8 = 0xc0, - XM_MASK = 0xc0 -} x86Mode; - -/* Structure to hold variable ModRM operand. */ -typedef struct { - int32_t ofs; /* Offset. */ - uint8_t base; /* Base register or RID_NONE. */ - uint8_t idx; /* Index register or RID_NONE. */ - uint8_t scale; /* Index scale (XM_SCALE1 .. XM_SCALE8). */ -} x86ModRM; - -/* -- Opcodes ------------------------------------------------------------- */ - -/* Macros to construct variable-length x86 opcodes. -(len+1) is in LSB. */ -#define XO_(o) ((uint32_t)(0x0000fe + (0x##o<<24))) -#define XO_FPU(a,b) ((uint32_t)(0x00fd + (0x##a<<16)+(0x##b<<24))) -#define XO_0f(o) ((uint32_t)(0x0f00fd + (0x##o<<24))) -#define XO_66(o) ((uint32_t)(0x6600fd + (0x##o<<24))) -#define XO_660f(o) ((uint32_t)(0x0f66fc + (0x##o<<24))) -#define XO_f20f(o) ((uint32_t)(0x0ff2fc + (0x##o<<24))) -#define XO_f30f(o) ((uint32_t)(0x0ff3fc + (0x##o<<24))) - -#define XV_660f38(o) ((uint32_t)(0x79e2c4 + (0x##o<<24))) -#define XV_f20f38(o) ((uint32_t)(0x7be2c4 + (0x##o<<24))) -#define XV_f20f3a(o) ((uint32_t)(0x7be3c4 + (0x##o<<24))) -#define XV_f30f38(o) ((uint32_t)(0x7ae2c4 + (0x##o<<24))) - -/* This list of x86 opcodes is not intended to be complete. Opcodes are only -** included when needed. Take a look at DynASM or jit.dis_x86 to see the -** whole mess. -*/ -typedef enum { - /* Fixed length opcodes. XI_* prefix. */ - XI_O16 = 0x66, - XI_NOP = 0x90, - XI_XCHGa = 0x90, - XI_CALL = 0xe8, - XI_JMP = 0xe9, - XI_JMPs = 0xeb, - XI_PUSH = 0x50, /* Really 50+r. */ - XI_JCCs = 0x70, /* Really 7x. */ - XI_JCCn = 0x80, /* Really 0f8x. */ - XI_LEA = 0x8d, - XI_MOVrib = 0xb0, /* Really b0+r. */ - XI_MOVri = 0xb8, /* Really b8+r. */ - XI_ARITHib = 0x80, - XI_ARITHi = 0x81, - XI_ARITHi8 = 0x83, - XI_PUSHi8 = 0x6a, - XI_TESTb = 0x84, - XI_TEST = 0x85, - XI_INT3 = 0xcc, - XI_MOVmi = 0xc7, - XI_GROUP5 = 0xff, - - /* Note: little-endian byte-order! */ - XI_FLDZ = 0xeed9, - XI_FLD1 = 0xe8d9, - XI_FDUP = 0xc0d9, /* Really fld st0. */ - XI_FPOP = 0xd8dd, /* Really fstp st0. */ - XI_FPOP1 = 0xd9dd, /* Really fstp st1. */ - XI_FRNDINT = 0xfcd9, - XI_FSCALE = 0xfdd9, - XI_FYL2X = 0xf1d9, - - /* VEX-encoded instructions. XV_* prefix. */ - XV_RORX = XV_f20f3a(f0), - XV_SARX = XV_f30f38(f7), - XV_SHLX = XV_660f38(f7), - XV_SHRX = XV_f20f38(f7), - - /* Variable-length opcodes. XO_* prefix. */ - XO_OR = XO_(0b), - XO_MOV = XO_(8b), - XO_MOVto = XO_(89), - XO_MOVtow = XO_66(89), - XO_MOVtob = XO_(88), - XO_MOVmi = XO_(c7), - XO_MOVmib = XO_(c6), - XO_LEA = XO_(8d), - XO_ARITHib = XO_(80), - XO_ARITHi = XO_(81), - XO_ARITHi8 = XO_(83), - XO_ARITHiw8 = XO_66(83), - XO_SHIFTi = XO_(c1), - XO_SHIFT1 = XO_(d1), - XO_SHIFTcl = XO_(d3), - XO_IMUL = XO_0f(af), - XO_IMULi = XO_(69), - XO_IMULi8 = XO_(6b), - XO_CMP = XO_(3b), - XO_TESTb = XO_(84), - XO_TEST = XO_(85), - XO_GROUP3b = XO_(f6), - XO_GROUP3 = XO_(f7), - XO_GROUP5b = XO_(fe), - XO_GROUP5 = XO_(ff), - XO_MOVZXb = XO_0f(b6), - XO_MOVZXw = XO_0f(b7), - XO_MOVSXb = XO_0f(be), - XO_MOVSXw = XO_0f(bf), - XO_MOVSXd = XO_(63), - XO_BSWAP = XO_0f(c8), - XO_CMOV = XO_0f(40), - - XO_MOVSD = XO_f20f(10), - XO_MOVSDto = XO_f20f(11), - XO_MOVSS = XO_f30f(10), - XO_MOVSSto = XO_f30f(11), - XO_MOVLPD = XO_660f(12), - XO_MOVAPS = XO_0f(28), - XO_XORPS = XO_0f(57), - XO_ANDPS = XO_0f(54), - XO_ADDSD = XO_f20f(58), - XO_SUBSD = XO_f20f(5c), - XO_MULSD = XO_f20f(59), - XO_DIVSD = XO_f20f(5e), - XO_SQRTSD = XO_f20f(51), - XO_MINSD = XO_f20f(5d), - XO_MAXSD = XO_f20f(5f), - XO_ROUNDSD = 0x0b3a0ffc, /* Really 66 0f 3a 0b. See asm_fpmath. */ - XO_UCOMISD = XO_660f(2e), - XO_CVTSI2SD = XO_f20f(2a), - XO_CVTTSD2SI= XO_f20f(2c), - XO_CVTSI2SS = XO_f30f(2a), - XO_CVTTSS2SI= XO_f30f(2c), - XO_CVTSS2SD = XO_f30f(5a), - XO_CVTSD2SS = XO_f20f(5a), - XO_ADDSS = XO_f30f(58), - XO_MOVD = XO_660f(6e), - XO_MOVDto = XO_660f(7e), - - XO_FLDd = XO_(d9), XOg_FLDd = 0, - XO_FLDq = XO_(dd), XOg_FLDq = 0, - XO_FILDd = XO_(db), XOg_FILDd = 0, - XO_FILDq = XO_(df), XOg_FILDq = 5, - XO_FSTPd = XO_(d9), XOg_FSTPd = 3, - XO_FSTPq = XO_(dd), XOg_FSTPq = 3, - XO_FISTPq = XO_(df), XOg_FISTPq = 7, - XO_FISTTPq = XO_(dd), XOg_FISTTPq = 1, - XO_FADDq = XO_(dc), XOg_FADDq = 0, - XO_FLDCW = XO_(d9), XOg_FLDCW = 5, - XO_FNSTCW = XO_(d9), XOg_FNSTCW = 7 -} x86Op; - -/* x86 opcode groups. */ -typedef uint32_t x86Group; - -#define XG_(i8, i, g) ((x86Group)(((i8) << 16) + ((i) << 8) + (g))) -#define XG_ARITHi(g) XG_(XI_ARITHi8, XI_ARITHi, g) -#define XG_TOXOi(xg) ((x86Op)(0x000000fe + (((xg)<<16) & 0xff000000))) -#define XG_TOXOi8(xg) ((x86Op)(0x000000fe + (((xg)<<8) & 0xff000000))) - -#define XO_ARITH(a) ((x86Op)(0x030000fe + ((a)<<27))) -#define XO_ARITHw(a) ((x86Op)(0x036600fd + ((a)<<27))) - -typedef enum { - XOg_ADD, XOg_OR, XOg_ADC, XOg_SBB, XOg_AND, XOg_SUB, XOg_XOR, XOg_CMP, - XOg_X_IMUL -} x86Arith; - -typedef enum { - XOg_ROL, XOg_ROR, XOg_RCL, XOg_RCR, XOg_SHL, XOg_SHR, XOg_SAL, XOg_SAR -} x86Shift; - -typedef enum { - XOg_TEST, XOg_TEST_, XOg_NOT, XOg_NEG, XOg_MUL, XOg_IMUL, XOg_DIV, XOg_IDIV -} x86Group3; - -typedef enum { - XOg_INC, XOg_DEC, XOg_CALL, XOg_CALLfar, XOg_JMP, XOg_JMPfar, XOg_PUSH -} x86Group5; - -/* x86 condition codes. */ -typedef enum { - CC_O, CC_NO, CC_B, CC_NB, CC_E, CC_NE, CC_BE, CC_NBE, - CC_S, CC_NS, CC_P, CC_NP, CC_L, CC_NL, CC_LE, CC_NLE, - CC_C = CC_B, CC_NAE = CC_C, CC_NC = CC_NB, CC_AE = CC_NB, - CC_Z = CC_E, CC_NZ = CC_NE, CC_NA = CC_BE, CC_A = CC_NBE, - CC_PE = CC_P, CC_PO = CC_NP, CC_NGE = CC_L, CC_GE = CC_NL, - CC_NG = CC_LE, CC_G = CC_NLE -} x86CC; - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.c deleted file mode 100644 index 5164181..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.c +++ /dev/null @@ -1,987 +0,0 @@ -/* -** Trace management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_trace_c -#define LUA_CORE - -#include "lj_obj.h" - -#if LJ_HASJIT - -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_debug.h" -#include "lj_str.h" -#include "lj_frame.h" -#include "lj_state.h" -#include "lj_bc.h" -#include "lj_ir.h" -#include "lj_jit.h" -#include "lj_iropt.h" -#include "lj_mcode.h" -#include "lj_trace.h" -#include "lj_snap.h" -#include "lj_gdbjit.h" -#include "lj_record.h" -#include "lj_asm.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_vmevent.h" -#include "lj_target.h" -#include "lj_prng.h" - -/* -- Error handling ------------------------------------------------------ */ - -/* Synchronous abort with error message. */ -void lj_trace_err(jit_State *J, TraceError e) -{ - setnilV(&J->errinfo); /* No error info. */ - setintV(J->L->top++, (int32_t)e); - lj_err_throw(J->L, LUA_ERRRUN); -} - -/* Synchronous abort with error message and error info. */ -void lj_trace_err_info(jit_State *J, TraceError e) -{ - setintV(J->L->top++, (int32_t)e); - lj_err_throw(J->L, LUA_ERRRUN); -} - -/* -- Trace management ---------------------------------------------------- */ - -/* The current trace is first assembled in J->cur. The variable length -** arrays point to shared, growable buffers (J->irbuf etc.). When trace -** recording ends successfully, the current trace and its data structures -** are copied to a new (compact) GCtrace object. -*/ - -/* Find a free trace number. */ -static TraceNo trace_findfree(jit_State *J) -{ - MSize osz, lim; - if (J->freetrace == 0) - J->freetrace = 1; - for (; J->freetrace < J->sizetrace; J->freetrace++) - if (traceref(J, J->freetrace) == NULL) - return J->freetrace++; - /* Need to grow trace array. */ - lim = (MSize)J->param[JIT_P_maxtrace] + 1; - if (lim < 2) lim = 2; else if (lim > 65535) lim = 65535; - osz = J->sizetrace; - if (osz >= lim) - return 0; /* Too many traces. */ - lj_mem_growvec(J->L, J->trace, J->sizetrace, lim, GCRef); - for (; osz < J->sizetrace; osz++) - setgcrefnull(J->trace[osz]); - return J->freetrace; -} - -#define TRACE_APPENDVEC(field, szfield, tp) \ - T->field = (tp *)p; \ - memcpy(p, J->cur.field, J->cur.szfield*sizeof(tp)); \ - p += J->cur.szfield*sizeof(tp); - -#ifdef LUAJIT_USE_PERFTOOLS -/* -** Create symbol table of JIT-compiled code. For use with Linux perf tools. -** Example usage: -** perf record -f -e cycles luajit test.lua -** perf report -s symbol -** rm perf.data /tmp/perf-*.map -*/ -#include -#include - -static void perftools_addtrace(GCtrace *T) -{ - static FILE *fp; - GCproto *pt = &gcref(T->startpt)->pt; - const BCIns *startpc = mref(T->startpc, const BCIns); - const char *name = proto_chunknamestr(pt); - BCLine lineno; - if (name[0] == '@' || name[0] == '=') - name++; - else - name = "(string)"; - lj_assertX(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc, - "trace PC out of range"); - lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); - if (!fp) { - char fname[40]; - sprintf(fname, "/tmp/perf-%d.map", getpid()); - if (!(fp = fopen(fname, "w"))) return; - setlinebuf(fp); - } - fprintf(fp, "%lx %x TRACE_%d::%s:%u\n", - (long)T->mcode, T->szmcode, T->traceno, name, lineno); -} -#endif - -/* Allocate space for copy of T. */ -GCtrace * LJ_FASTCALL lj_trace_alloc(lua_State *L, GCtrace *T) -{ - size_t sztr = ((sizeof(GCtrace)+7)&~7); - size_t szins = (T->nins-T->nk)*sizeof(IRIns); - size_t sz = sztr + szins + - T->nsnap*sizeof(SnapShot) + - T->nsnapmap*sizeof(SnapEntry); - GCtrace *T2 = lj_mem_newt(L, (MSize)sz, GCtrace); - char *p = (char *)T2 + sztr; - T2->gct = ~LJ_TTRACE; - T2->marked = 0; - T2->traceno = 0; - T2->ir = (IRIns *)p - T->nk; - T2->nins = T->nins; - T2->nk = T->nk; - T2->nsnap = T->nsnap; - T2->nsnapmap = T->nsnapmap; - memcpy(p, T->ir + T->nk, szins); - return T2; -} - -/* Save current trace by copying and compacting it. */ -static void trace_save(jit_State *J, GCtrace *T) -{ - size_t sztr = ((sizeof(GCtrace)+7)&~7); - size_t szins = (J->cur.nins-J->cur.nk)*sizeof(IRIns); - char *p = (char *)T + sztr; - memcpy(T, &J->cur, sizeof(GCtrace)); - setgcrefr(T->nextgc, J2G(J)->gc.root); - setgcrefp(J2G(J)->gc.root, T); - newwhite(J2G(J), T); - T->gct = ~LJ_TTRACE; - T->ir = (IRIns *)p - J->cur.nk; /* The IR has already been copied above. */ - p += szins; - TRACE_APPENDVEC(snap, nsnap, SnapShot) - TRACE_APPENDVEC(snapmap, nsnapmap, SnapEntry) - J->cur.traceno = 0; - J->curfinal = NULL; - setgcrefp(J->trace[T->traceno], T); - lj_gc_barriertrace(J2G(J), T->traceno); - lj_gdbjit_addtrace(J, T); -#ifdef LUAJIT_USE_PERFTOOLS - perftools_addtrace(T); -#endif -} - -void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T) -{ - jit_State *J = G2J(g); - if (T->traceno) { - lj_gdbjit_deltrace(J, T); - if (T->traceno < J->freetrace) - J->freetrace = T->traceno; - setgcrefnull(J->trace[T->traceno]); - } - lj_mem_free(g, T, - ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + - T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry)); -} - -/* Re-enable compiling a prototype by unpatching any modified bytecode. */ -void lj_trace_reenableproto(GCproto *pt) -{ - if ((pt->flags & PROTO_ILOOP)) { - BCIns *bc = proto_bc(pt); - BCPos i, sizebc = pt->sizebc; - pt->flags &= ~PROTO_ILOOP; - if (bc_op(bc[0]) == BC_IFUNCF) - setbc_op(&bc[0], BC_FUNCF); - for (i = 1; i < sizebc; i++) { - BCOp op = bc_op(bc[i]); - if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP) - setbc_op(&bc[i], (int)op+(int)BC_LOOP-(int)BC_ILOOP); - } - } -} - -/* Unpatch the bytecode modified by a root trace. */ -static void trace_unpatch(jit_State *J, GCtrace *T) -{ - BCOp op = bc_op(T->startins); - BCIns *pc = mref(T->startpc, BCIns); - UNUSED(J); - if (op == BC_JMP) - return; /* No need to unpatch branches in parent traces (yet). */ - switch (bc_op(*pc)) { - case BC_JFORL: - lj_assertJ(traceref(J, bc_d(*pc)) == T, "JFORL references other trace"); - *pc = T->startins; - pc += bc_j(T->startins); - lj_assertJ(bc_op(*pc) == BC_JFORI, "FORL does not point to JFORI"); - setbc_op(pc, BC_FORI); - break; - case BC_JITERL: - case BC_JLOOP: - lj_assertJ(op == BC_ITERL || op == BC_ITERN || op == BC_LOOP || - bc_isret(op), "bad original bytecode %d", op); - *pc = T->startins; - break; - case BC_JMP: - lj_assertJ(op == BC_ITERL, "bad original bytecode %d", op); - pc += bc_j(*pc)+2; - if (bc_op(*pc) == BC_JITERL) { - lj_assertJ(traceref(J, bc_d(*pc)) == T, "JITERL references other trace"); - *pc = T->startins; - } - break; - case BC_JFUNCF: - lj_assertJ(op == BC_FUNCF, "bad original bytecode %d", op); - *pc = T->startins; - break; - default: /* Already unpatched. */ - break; - } -} - -/* Flush a root trace. */ -static void trace_flushroot(jit_State *J, GCtrace *T) -{ - GCproto *pt = &gcref(T->startpt)->pt; - lj_assertJ(T->root == 0, "not a root trace"); - lj_assertJ(pt != NULL, "trace has no prototype"); - /* First unpatch any modified bytecode. */ - trace_unpatch(J, T); - /* Unlink root trace from chain anchored in prototype. */ - if (pt->trace == T->traceno) { /* Trace is first in chain. Easy. */ - pt->trace = T->nextroot; - } else if (pt->trace) { /* Otherwise search in chain of root traces. */ - GCtrace *T2 = traceref(J, pt->trace); - if (T2) { - for (; T2->nextroot; T2 = traceref(J, T2->nextroot)) - if (T2->nextroot == T->traceno) { - T2->nextroot = T->nextroot; /* Unlink from chain. */ - break; - } - } - } -} - -/* Flush a trace. Only root traces are considered. */ -void lj_trace_flush(jit_State *J, TraceNo traceno) -{ - if (traceno > 0 && traceno < J->sizetrace) { - GCtrace *T = traceref(J, traceno); - if (T && T->root == 0) - trace_flushroot(J, T); - } -} - -/* Flush all traces associated with a prototype. */ -void lj_trace_flushproto(global_State *g, GCproto *pt) -{ - while (pt->trace != 0) - trace_flushroot(G2J(g), traceref(G2J(g), pt->trace)); -} - -/* Flush all traces. */ -int lj_trace_flushall(lua_State *L) -{ - jit_State *J = L2J(L); - ptrdiff_t i; - if ((J2G(J)->hookmask & HOOK_GC)) - return 1; - for (i = (ptrdiff_t)J->sizetrace-1; i > 0; i--) { - GCtrace *T = traceref(J, i); - if (T) { - if (T->root == 0) - trace_flushroot(J, T); - lj_gdbjit_deltrace(J, T); - T->traceno = T->link = 0; /* Blacklist the link for cont_stitch. */ - setgcrefnull(J->trace[i]); - } - } - J->cur.traceno = 0; - J->freetrace = 0; - /* Clear penalty cache. */ - memset(J->penalty, 0, sizeof(J->penalty)); - /* Free the whole machine code and invalidate all exit stub groups. */ - lj_mcode_free(J); - memset(J->exitstubgroup, 0, sizeof(J->exitstubgroup)); - lj_vmevent_send(L, TRACE, - setstrV(L, L->top++, lj_str_newlit(L, "flush")); - ); - return 0; -} - -/* Initialize JIT compiler state. */ -void lj_trace_initstate(global_State *g) -{ - jit_State *J = G2J(g); - TValue *tv; - - /* Initialize aligned SIMD constants. */ - tv = LJ_KSIMD(J, LJ_KSIMD_ABS); - tv[0].u64 = U64x(7fffffff,ffffffff); - tv[1].u64 = U64x(7fffffff,ffffffff); - tv = LJ_KSIMD(J, LJ_KSIMD_NEG); - tv[0].u64 = U64x(80000000,00000000); - tv[1].u64 = U64x(80000000,00000000); - - /* Initialize 32/64 bit constants. */ -#if LJ_TARGET_X86ORX64 - J->k64[LJ_K64_TOBIT].u64 = U64x(43380000,00000000); -#if LJ_32 - J->k64[LJ_K64_M2P64_31].u64 = U64x(c1e00000,00000000); -#endif - J->k64[LJ_K64_2P64].u64 = U64x(43f00000,00000000); - J->k32[LJ_K32_M2P64_31] = LJ_64 ? 0xdf800000 : 0xcf000000; -#endif -#if LJ_TARGET_X86ORX64 || LJ_TARGET_MIPS64 - J->k64[LJ_K64_M2P64].u64 = U64x(c3f00000,00000000); -#endif -#if LJ_TARGET_PPC - J->k32[LJ_K32_2P52_2P31] = 0x59800004; - J->k32[LJ_K32_2P52] = 0x59800000; -#endif -#if LJ_TARGET_PPC || LJ_TARGET_MIPS - J->k32[LJ_K32_2P31] = 0x4f000000; -#endif -#if LJ_TARGET_MIPS - J->k64[LJ_K64_2P31].u64 = U64x(41e00000,00000000); -#if LJ_64 - J->k64[LJ_K64_2P63].u64 = U64x(43e00000,00000000); - J->k32[LJ_K32_2P63] = 0x5f000000; - J->k32[LJ_K32_M2P64] = 0xdf800000; -#endif -#endif -} - -/* Free everything associated with the JIT compiler state. */ -void lj_trace_freestate(global_State *g) -{ - jit_State *J = G2J(g); -#ifdef LUA_USE_ASSERT - { /* This assumes all traces have already been freed. */ - ptrdiff_t i; - for (i = 1; i < (ptrdiff_t)J->sizetrace; i++) - lj_assertG(i == (ptrdiff_t)J->cur.traceno || traceref(J, i) == NULL, - "trace still allocated"); - } -#endif - lj_mcode_free(J); - lj_mem_freevec(g, J->snapmapbuf, J->sizesnapmap, SnapEntry); - lj_mem_freevec(g, J->snapbuf, J->sizesnap, SnapShot); - lj_mem_freevec(g, J->irbuf + J->irbotlim, J->irtoplim - J->irbotlim, IRIns); - lj_mem_freevec(g, J->trace, J->sizetrace, GCRef); -} - -/* -- Penalties and blacklisting ------------------------------------------ */ - -/* Blacklist a bytecode instruction. */ -static void blacklist_pc(GCproto *pt, BCIns *pc) -{ - if (bc_op(*pc) == BC_ITERN) { - setbc_op(pc, BC_ITERC); - setbc_op(pc+1+bc_j(pc[1]), BC_JMP); - } else { - setbc_op(pc, (int)bc_op(*pc)+(int)BC_ILOOP-(int)BC_LOOP); - pt->flags |= PROTO_ILOOP; - } -} - -/* Penalize a bytecode instruction. */ -static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e) -{ - uint32_t i, val = PENALTY_MIN; - for (i = 0; i < PENALTY_SLOTS; i++) - if (mref(J->penalty[i].pc, const BCIns) == pc) { /* Cache slot found? */ - /* First try to bump its hotcount several times. */ - val = ((uint32_t)J->penalty[i].val << 1) + - (lj_prng_u64(&J2G(J)->prng) & ((1u< PENALTY_MAX) { - blacklist_pc(pt, pc); /* Blacklist it, if that didn't help. */ - return; - } - goto setpenalty; - } - /* Assign a new penalty cache slot. */ - i = J->penaltyslot; - J->penaltyslot = (J->penaltyslot + 1) & (PENALTY_SLOTS-1); - setmref(J->penalty[i].pc, pc); -setpenalty: - J->penalty[i].val = (uint16_t)val; - J->penalty[i].reason = e; - hotcount_set(J2GG(J), pc+1, val); -} - -/* -- Trace compiler state machine ---------------------------------------- */ - -/* Start tracing. */ -static void trace_start(jit_State *J) -{ - lua_State *L; - TraceNo traceno; - - if ((J->pt->flags & PROTO_NOJIT)) { /* JIT disabled for this proto? */ - if (J->parent == 0 && J->exitno == 0 && bc_op(*J->pc) != BC_ITERN) { - /* Lazy bytecode patching to disable hotcount events. */ - lj_assertJ(bc_op(*J->pc) == BC_FORL || bc_op(*J->pc) == BC_ITERL || - bc_op(*J->pc) == BC_LOOP || bc_op(*J->pc) == BC_FUNCF, - "bad hot bytecode %d", bc_op(*J->pc)); - setbc_op(J->pc, (int)bc_op(*J->pc)+(int)BC_ILOOP-(int)BC_LOOP); - J->pt->flags |= PROTO_ILOOP; - } - J->state = LJ_TRACE_IDLE; /* Silently ignored. */ - return; - } - - /* Get a new trace number. */ - traceno = trace_findfree(J); - if (LJ_UNLIKELY(traceno == 0)) { /* No free trace? */ - lj_assertJ((J2G(J)->hookmask & HOOK_GC) == 0, - "recorder called from GC hook"); - lj_trace_flushall(J->L); - J->state = LJ_TRACE_IDLE; /* Silently ignored. */ - return; - } - setgcrefp(J->trace[traceno], &J->cur); - - /* Setup enough of the current trace to be able to send the vmevent. */ - memset(&J->cur, 0, sizeof(GCtrace)); - J->cur.traceno = traceno; - J->cur.nins = J->cur.nk = REF_BASE; - J->cur.ir = J->irbuf; - J->cur.snap = J->snapbuf; - J->cur.snapmap = J->snapmapbuf; - J->mergesnap = 0; - J->needsnap = 0; - J->bcskip = 0; - J->guardemit.irt = 0; - J->postproc = LJ_POST_NONE; - lj_resetsplit(J); - J->retryrec = 0; - J->ktrace = 0; - setgcref(J->cur.startpt, obj2gco(J->pt)); - - L = J->L; - lj_vmevent_send(L, TRACE, - setstrV(L, L->top++, lj_str_newlit(L, "start")); - setintV(L->top++, traceno); - setfuncV(L, L->top++, J->fn); - setintV(L->top++, proto_bcpos(J->pt, J->pc)); - if (J->parent) { - setintV(L->top++, J->parent); - setintV(L->top++, J->exitno); - } else { - BCOp op = bc_op(*J->pc); - if (op == BC_CALLM || op == BC_CALL || op == BC_ITERC) { - setintV(L->top++, J->exitno); /* Parent of stitched trace. */ - setintV(L->top++, -1); - } - } - ); - lj_record_setup(J); -} - -/* Stop tracing. */ -static void trace_stop(jit_State *J) -{ - BCIns *pc = mref(J->cur.startpc, BCIns); - BCOp op = bc_op(J->cur.startins); - GCproto *pt = &gcref(J->cur.startpt)->pt; - TraceNo traceno = J->cur.traceno; - GCtrace *T = J->curfinal; - lua_State *L; - - switch (op) { - case BC_FORL: - setbc_op(pc+bc_j(J->cur.startins), BC_JFORI); /* Patch FORI, too. */ - /* fallthrough */ - case BC_LOOP: - case BC_ITERL: - case BC_FUNCF: - /* Patch bytecode of starting instruction in root trace. */ - setbc_op(pc, (int)op+(int)BC_JLOOP-(int)BC_LOOP); - setbc_d(pc, traceno); - addroot: - /* Add to root trace chain in prototype. */ - J->cur.nextroot = pt->trace; - pt->trace = (TraceNo1)traceno; - break; - case BC_ITERN: - case BC_RET: - case BC_RET0: - case BC_RET1: - *pc = BCINS_AD(BC_JLOOP, J->cur.snap[0].nslots, traceno); - goto addroot; - case BC_JMP: - /* Patch exit branch in parent to side trace entry. */ - lj_assertJ(J->parent != 0 && J->cur.root != 0, "not a side trace"); - lj_asm_patchexit(J, traceref(J, J->parent), J->exitno, J->cur.mcode); - /* Avoid compiling a side trace twice (stack resizing uses parent exit). */ - { - SnapShot *snap = &traceref(J, J->parent)->snap[J->exitno]; - snap->count = SNAPCOUNT_DONE; - if (J->cur.topslot > snap->topslot) snap->topslot = J->cur.topslot; - } - /* Add to side trace chain in root trace. */ - { - GCtrace *root = traceref(J, J->cur.root); - root->nchild++; - J->cur.nextside = root->nextside; - root->nextside = (TraceNo1)traceno; - } - break; - case BC_CALLM: - case BC_CALL: - case BC_ITERC: - /* Trace stitching: patch link of previous trace. */ - traceref(J, J->exitno)->link = traceno; - break; - default: - lj_assertJ(0, "bad stop bytecode %d", op); - break; - } - - /* Commit new mcode only after all patching is done. */ - lj_mcode_commit(J, J->cur.mcode); - J->postproc = LJ_POST_NONE; - trace_save(J, T); - - L = J->L; - lj_vmevent_send(L, TRACE, - setstrV(L, L->top++, lj_str_newlit(L, "stop")); - setintV(L->top++, traceno); - setfuncV(L, L->top++, J->fn); - ); -} - -/* Start a new root trace for down-recursion. */ -static int trace_downrec(jit_State *J) -{ - /* Restart recording at the return instruction. */ - lj_assertJ(J->pt != NULL, "no active prototype"); - lj_assertJ(bc_isret(bc_op(*J->pc)), "not at a return bytecode"); - if (bc_op(*J->pc) == BC_RETM) - return 0; /* NYI: down-recursion with RETM. */ - J->parent = 0; - J->exitno = 0; - J->state = LJ_TRACE_RECORD; - trace_start(J); - return 1; -} - -/* Abort tracing. */ -static int trace_abort(jit_State *J) -{ - lua_State *L = J->L; - TraceError e = LJ_TRERR_RECERR; - TraceNo traceno; - - J->postproc = LJ_POST_NONE; - lj_mcode_abort(J); - if (J->curfinal) { - lj_trace_free(J2G(J), J->curfinal); - J->curfinal = NULL; - } - if (tvisnumber(L->top-1)) - e = (TraceError)numberVint(L->top-1); - if (e == LJ_TRERR_MCODELM) { - L->top--; /* Remove error object */ - J->state = LJ_TRACE_ASM; - return 1; /* Retry ASM with new MCode area. */ - } - /* Penalize or blacklist starting bytecode instruction. */ - if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) { - if (J->exitno == 0) { - BCIns *startpc = mref(J->cur.startpc, BCIns); - if (e == LJ_TRERR_RETRY) - hotcount_set(J2GG(J), startpc+1, 1); /* Immediate retry. */ - else - penalty_pc(J, &gcref(J->cur.startpt)->pt, startpc, e); - } else { - traceref(J, J->exitno)->link = J->exitno; /* Self-link is blacklisted. */ - } - } - - /* Is there anything to abort? */ - traceno = J->cur.traceno; - if (traceno) { - ptrdiff_t errobj = savestack(L, L->top-1); /* Stack may be resized. */ - J->cur.link = 0; - J->cur.linktype = LJ_TRLINK_NONE; - lj_vmevent_send(L, TRACE, - TValue *frame; - const BCIns *pc; - GCfunc *fn; - setstrV(L, L->top++, lj_str_newlit(L, "abort")); - setintV(L->top++, traceno); - /* Find original Lua function call to generate a better error message. */ - frame = J->L->base-1; - pc = J->pc; - while (!isluafunc(frame_func(frame))) { - pc = (frame_iscont(frame) ? frame_contpc(frame) : frame_pc(frame)) - 1; - frame = frame_prev(frame); - } - fn = frame_func(frame); - setfuncV(L, L->top++, fn); - setintV(L->top++, proto_bcpos(funcproto(fn), pc)); - copyTV(L, L->top++, restorestack(L, errobj)); - copyTV(L, L->top++, &J->errinfo); - ); - /* Drop aborted trace after the vmevent (which may still access it). */ - setgcrefnull(J->trace[traceno]); - if (traceno < J->freetrace) - J->freetrace = traceno; - J->cur.traceno = 0; - } - L->top--; /* Remove error object */ - if (e == LJ_TRERR_DOWNREC) - return trace_downrec(J); - else if (e == LJ_TRERR_MCODEAL) - lj_trace_flushall(L); - return 0; -} - -/* Perform pending re-patch of a bytecode instruction. */ -static LJ_AINLINE void trace_pendpatch(jit_State *J, int force) -{ - if (LJ_UNLIKELY(J->patchpc)) { - if (force || J->bcskip == 0) { - *J->patchpc = J->patchins; - J->patchpc = NULL; - } else { - J->bcskip = 0; - } - } -} - -/* State machine for the trace compiler. Protected callback. */ -static TValue *trace_state(lua_State *L, lua_CFunction dummy, void *ud) -{ - jit_State *J = (jit_State *)ud; - UNUSED(dummy); - do { - retry: - switch (J->state) { - case LJ_TRACE_START: - J->state = LJ_TRACE_RECORD; /* trace_start() may change state. */ - trace_start(J); - lj_dispatch_update(J2G(J)); - if (J->state != LJ_TRACE_RECORD_1ST) - break; - /* fallthrough */ - - case LJ_TRACE_RECORD_1ST: - J->state = LJ_TRACE_RECORD; - /* fallthrough */ - case LJ_TRACE_RECORD: - trace_pendpatch(J, 0); - setvmstate(J2G(J), RECORD); - lj_vmevent_send_(L, RECORD, - /* Save/restore state for trace recorder. */ - TValue savetv = J2G(J)->tmptv; - TValue savetv2 = J2G(J)->tmptv2; - TraceNo parent = J->parent; - ExitNo exitno = J->exitno; - setintV(L->top++, J->cur.traceno); - setfuncV(L, L->top++, J->fn); - setintV(L->top++, J->pt ? (int32_t)proto_bcpos(J->pt, J->pc) : -1); - setintV(L->top++, J->framedepth); - , - J2G(J)->tmptv = savetv; - J2G(J)->tmptv2 = savetv2; - J->parent = parent; - J->exitno = exitno; - ); - lj_record_ins(J); - break; - - case LJ_TRACE_END: - trace_pendpatch(J, 1); - J->loopref = 0; - if ((J->flags & JIT_F_OPT_LOOP) && - J->cur.link == J->cur.traceno && J->framedepth + J->retdepth == 0) { - setvmstate(J2G(J), OPT); - lj_opt_dce(J); - if (lj_opt_loop(J)) { /* Loop optimization failed? */ - J->cur.link = 0; - J->cur.linktype = LJ_TRLINK_NONE; - J->loopref = J->cur.nins; - J->state = LJ_TRACE_RECORD; /* Try to continue recording. */ - break; - } - J->loopref = J->chain[IR_LOOP]; /* Needed by assembler. */ - } - lj_opt_split(J); - lj_opt_sink(J); - if (!J->loopref) J->cur.snap[J->cur.nsnap-1].count = SNAPCOUNT_DONE; - J->state = LJ_TRACE_ASM; - break; - - case LJ_TRACE_ASM: - setvmstate(J2G(J), ASM); - lj_asm_trace(J, &J->cur); - trace_stop(J); - setvmstate(J2G(J), INTERP); - J->state = LJ_TRACE_IDLE; - lj_dispatch_update(J2G(J)); - return NULL; - - default: /* Trace aborted asynchronously. */ - setintV(L->top++, (int32_t)LJ_TRERR_RECERR); - /* fallthrough */ - case LJ_TRACE_ERR: - trace_pendpatch(J, 1); - if (trace_abort(J)) - goto retry; - setvmstate(J2G(J), INTERP); - J->state = LJ_TRACE_IDLE; - lj_dispatch_update(J2G(J)); - return NULL; - } - } while (J->state > LJ_TRACE_RECORD); - return NULL; -} - -/* -- Event handling ------------------------------------------------------ */ - -/* A bytecode instruction is about to be executed. Record it. */ -void lj_trace_ins(jit_State *J, const BCIns *pc) -{ - /* Note: J->L must already be set. pc is the true bytecode PC here. */ - J->pc = pc; - J->fn = curr_func(J->L); - J->pt = isluafunc(J->fn) ? funcproto(J->fn) : NULL; - while (lj_vm_cpcall(J->L, NULL, (void *)J, trace_state) != 0) - J->state = LJ_TRACE_ERR; -} - -/* A hotcount triggered. Start recording a root trace. */ -void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc) -{ - /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */ - ERRNO_SAVE - /* Reset hotcount. */ - hotcount_set(J2GG(J), pc, J->param[JIT_P_hotloop]*HOTCOUNT_LOOP); - /* Only start a new trace if not recording or inside __gc call or vmevent. */ - if (J->state == LJ_TRACE_IDLE && - !(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT))) { - J->parent = 0; /* Root trace. */ - J->exitno = 0; - J->state = LJ_TRACE_START; - lj_trace_ins(J, pc-1); - } - ERRNO_RESTORE -} - -/* Check for a hot side exit. If yes, start recording a side trace. */ -static void trace_hotside(jit_State *J, const BCIns *pc) -{ - SnapShot *snap = &traceref(J, J->parent)->snap[J->exitno]; - if (!(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT)) && - isluafunc(curr_func(J->L)) && - snap->count != SNAPCOUNT_DONE && - ++snap->count >= J->param[JIT_P_hotexit]) { - lj_assertJ(J->state == LJ_TRACE_IDLE, "hot side exit while recording"); - /* J->parent is non-zero for a side trace. */ - J->state = LJ_TRACE_START; - lj_trace_ins(J, pc); - } -} - -/* Stitch a new trace to the previous trace. */ -void LJ_FASTCALL lj_trace_stitch(jit_State *J, const BCIns *pc) -{ - /* Only start a new trace if not recording or inside __gc call or vmevent. */ - if (J->state == LJ_TRACE_IDLE && - !(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT))) { - J->parent = 0; /* Have to treat it like a root trace. */ - /* J->exitno is set to the invoking trace. */ - J->state = LJ_TRACE_START; - lj_trace_ins(J, pc); - } -} - - -/* Tiny struct to pass data to protected call. */ -typedef struct ExitDataCP { - jit_State *J; - void *exptr; /* Pointer to exit state. */ - const BCIns *pc; /* Restart interpreter at this PC. */ -} ExitDataCP; - -/* Need to protect lj_snap_restore because it may throw. */ -static TValue *trace_exit_cp(lua_State *L, lua_CFunction dummy, void *ud) -{ - ExitDataCP *exd = (ExitDataCP *)ud; - /* Always catch error here and don't call error function. */ - cframe_errfunc(L->cframe) = 0; - cframe_nres(L->cframe) = -2*LUAI_MAXSTACK*(int)sizeof(TValue); - exd->pc = lj_snap_restore(exd->J, exd->exptr); - UNUSED(dummy); - return NULL; -} - -#ifndef LUAJIT_DISABLE_VMEVENT -/* Push all registers from exit state. */ -static void trace_exit_regs(lua_State *L, ExitState *ex) -{ - int32_t i; - setintV(L->top++, RID_NUM_GPR); - setintV(L->top++, RID_NUM_FPR); - for (i = 0; i < RID_NUM_GPR; i++) { - if (sizeof(ex->gpr[i]) == sizeof(int32_t)) - setintV(L->top++, (int32_t)ex->gpr[i]); - else - setnumV(L->top++, (lua_Number)ex->gpr[i]); - } -#if !LJ_SOFTFP - for (i = 0; i < RID_NUM_FPR; i++) { - setnumV(L->top, ex->fpr[i]); - if (LJ_UNLIKELY(tvisnan(L->top))) - setnanV(L->top); - L->top++; - } -#endif -} -#endif - -#if defined(EXITSTATE_PCREG) || (LJ_UNWIND_JIT && !EXITTRACE_VMSTATE) -/* Determine trace number from pc of exit instruction. */ -static TraceNo trace_exit_find(jit_State *J, MCode *pc) -{ - TraceNo traceno; - for (traceno = 1; traceno < J->sizetrace; traceno++) { - GCtrace *T = traceref(J, traceno); - if (T && pc >= T->mcode && pc < (MCode *)((char *)T->mcode + T->szmcode)) - return traceno; - } - lj_assertJ(0, "bad exit pc"); - return 0; -} -#endif - -/* A trace exited. Restore interpreter state. */ -int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr) -{ - ERRNO_SAVE - lua_State *L = J->L; - ExitState *ex = (ExitState *)exptr; - ExitDataCP exd; - int errcode, exitcode = J->exitcode; - TValue exiterr; - const BCIns *pc; - void *cf; - GCtrace *T; - - setnilV(&exiterr); - if (exitcode) { /* Trace unwound with error code. */ - J->exitcode = 0; - copyTV(L, &exiterr, L->top-1); - } - -#ifdef EXITSTATE_PCREG - J->parent = trace_exit_find(J, (MCode *)(intptr_t)ex->gpr[EXITSTATE_PCREG]); -#endif - T = traceref(J, J->parent); UNUSED(T); -#ifdef EXITSTATE_CHECKEXIT - if (J->exitno == T->nsnap) { /* Treat stack check like a parent exit. */ - lj_assertJ(T->root != 0, "stack check in root trace"); - J->exitno = T->ir[REF_BASE].op2; - J->parent = T->ir[REF_BASE].op1; - T = traceref(J, J->parent); - } -#endif - lj_assertJ(T != NULL && J->exitno < T->nsnap, "bad trace or exit number"); - exd.J = J; - exd.exptr = exptr; - errcode = lj_vm_cpcall(L, NULL, &exd, trace_exit_cp); - if (errcode) - return -errcode; /* Return negated error code. */ - - if (exitcode) copyTV(L, L->top++, &exiterr); /* Anchor the error object. */ - - if (!(LJ_HASPROFILE && (G(L)->hookmask & HOOK_PROFILE))) - lj_vmevent_send(L, TEXIT, - lj_state_checkstack(L, 4+RID_NUM_GPR+RID_NUM_FPR+LUA_MINSTACK); - setintV(L->top++, J->parent); - setintV(L->top++, J->exitno); - trace_exit_regs(L, ex); - ); - - pc = exd.pc; - cf = cframe_raw(L->cframe); - setcframe_pc(cf, pc); - if (exitcode) { - return -exitcode; - } else if (LJ_HASPROFILE && (G(L)->hookmask & HOOK_PROFILE)) { - /* Just exit to interpreter. */ - } else if (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize) { - if (!(G(L)->hookmask & HOOK_GC)) - lj_gc_step(L); /* Exited because of GC: drive GC forward. */ - } else { - trace_hotside(J, pc); - } - if (bc_op(*pc) == BC_JLOOP) { - BCIns *retpc = &traceref(J, bc_d(*pc))->startins; - int isret = bc_isret(bc_op(*retpc)); - if (isret || bc_op(*retpc) == BC_ITERN) { - if (J->state == LJ_TRACE_RECORD) { - J->patchins = *pc; - J->patchpc = (BCIns *)pc; - *J->patchpc = *retpc; - J->bcskip = 1; - } else if (isret) { - pc = retpc; - setcframe_pc(cf, pc); - } - } - } - /* Return MULTRES or 0. */ - ERRNO_RESTORE - switch (bc_op(*pc)) { - case BC_CALLM: case BC_CALLMT: - return (int)((BCReg)(L->top - L->base) - bc_a(*pc) - bc_c(*pc) - LJ_FR2); - case BC_RETM: - return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc) - bc_d(*pc)); - case BC_TSETM: - return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc)); - default: - if (bc_op(*pc) >= BC_FUNCF) - return (int)((BCReg)(L->top - L->base) + 1); - return 0; - } -} - -#if LJ_UNWIND_JIT -/* Given an mcode address determine trace exit address for unwinding. */ -uintptr_t LJ_FASTCALL lj_trace_unwind(jit_State *J, uintptr_t addr, ExitNo *ep) -{ -#if EXITTRACE_VMSTATE - TraceNo traceno = J2G(J)->vmstate; -#else - TraceNo traceno = trace_exit_find(J, (MCode *)addr); -#endif - GCtrace *T = traceref(J, traceno); - if (T -#if EXITTRACE_VMSTATE - && addr >= (uintptr_t)T->mcode && addr < (uintptr_t)T->mcode + T->szmcode -#endif - ) { - SnapShot *snap = T->snap; - SnapNo lo = 0, exitno = T->nsnap; - uintptr_t ofs = (uintptr_t)((MCode *)addr - T->mcode); /* MCode units! */ - /* Rightmost binary search for mcode offset to determine exit number. */ - do { - SnapNo mid = (lo+exitno) >> 1; - if (ofs < snap[mid].mcofs) exitno = mid; else lo = mid + 1; - } while (lo < exitno); - exitno--; - *ep = exitno; -#ifdef EXITSTUBS_PER_GROUP - return (uintptr_t)exitstub_addr(J, exitno); -#else - return (uintptr_t)exitstub_trace_addr(T, exitno); -#endif - } - /* Cannot correlate addr with trace/exit. This will be fatal. */ - lj_assertJ(0, "bad exit pc"); - return 0; -} -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.h deleted file mode 100644 index 3d7f76f..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_trace.h +++ /dev/null @@ -1,58 +0,0 @@ -/* -** Trace management. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_TRACE_H -#define _LJ_TRACE_H - -#include "lj_obj.h" - -#if LJ_HASJIT -#include "lj_jit.h" -#include "lj_dispatch.h" - -/* Trace errors. */ -typedef enum { -#define TREDEF(name, msg) LJ_TRERR_##name, -#include "lj_traceerr.h" - LJ_TRERR__MAX -} TraceError; - -LJ_FUNC_NORET void lj_trace_err(jit_State *J, TraceError e); -LJ_FUNC_NORET void lj_trace_err_info(jit_State *J, TraceError e); - -/* Trace management. */ -LJ_FUNC GCtrace * LJ_FASTCALL lj_trace_alloc(lua_State *L, GCtrace *T); -LJ_FUNC void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T); -LJ_FUNC void lj_trace_reenableproto(GCproto *pt); -LJ_FUNC void lj_trace_flushproto(global_State *g, GCproto *pt); -LJ_FUNC void lj_trace_flush(jit_State *J, TraceNo traceno); -LJ_FUNC int lj_trace_flushall(lua_State *L); -LJ_FUNC void lj_trace_initstate(global_State *g); -LJ_FUNC void lj_trace_freestate(global_State *g); - -/* Event handling. */ -LJ_FUNC void lj_trace_ins(jit_State *J, const BCIns *pc); -LJ_FUNCA void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc); -LJ_FUNCA void LJ_FASTCALL lj_trace_stitch(jit_State *J, const BCIns *pc); -LJ_FUNCA int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr); -#if LJ_UNWIND_EXT -LJ_FUNC uintptr_t LJ_FASTCALL lj_trace_unwind(jit_State *J, uintptr_t addr, ExitNo *ep); -#endif - -/* Signal asynchronous abort of trace or end of trace. */ -#define lj_trace_abort(g) (G2J(g)->state &= ~LJ_TRACE_ACTIVE) -#define lj_trace_end(J) (J->state = LJ_TRACE_END) - -#else - -#define lj_trace_flushall(L) (UNUSED(L), 0) -#define lj_trace_initstate(g) UNUSED(g) -#define lj_trace_freestate(g) UNUSED(g) -#define lj_trace_abort(g) UNUSED(g) -#define lj_trace_end(J) UNUSED(J) - -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_traceerr.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_traceerr.h deleted file mode 100644 index 8ed8ac8..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_traceerr.h +++ /dev/null @@ -1,61 +0,0 @@ -/* -** Trace compiler error messages. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -/* This file may be included multiple times with different TREDEF macros. */ - -/* Recording. */ -TREDEF(RECERR, "error thrown or hook called during recording") -TREDEF(TRACEUV, "trace too short") -TREDEF(TRACEOV, "trace too long") -TREDEF(STACKOV, "trace too deep") -TREDEF(SNAPOV, "too many snapshots") -TREDEF(BLACKL, "blacklisted") -TREDEF(RETRY, "retry recording") -TREDEF(NYIBC, "NYI: bytecode %d") - -/* Recording loop ops. */ -TREDEF(LLEAVE, "leaving loop in root trace") -TREDEF(LINNER, "inner loop in root trace") -TREDEF(LUNROLL, "loop unroll limit reached") - -/* Recording calls/returns. */ -TREDEF(BADTYPE, "bad argument type") -TREDEF(CJITOFF, "JIT compilation disabled for function") -TREDEF(CUNROLL, "call unroll limit reached") -TREDEF(DOWNREC, "down-recursion, restarting") -TREDEF(NYIFFU, "NYI: unsupported variant of FastFunc %s") -TREDEF(NYIRETL, "NYI: return to lower frame") - -/* Recording indexed load/store. */ -TREDEF(STORENN, "store with nil or NaN key") -TREDEF(NOMM, "missing metamethod") -TREDEF(IDXLOOP, "looping index lookup") -TREDEF(NYITMIX, "NYI: mixed sparse/dense table") - -/* Recording C data operations. */ -TREDEF(NOCACHE, "symbol not in cache") -TREDEF(NYICONV, "NYI: unsupported C type conversion") -TREDEF(NYICALL, "NYI: unsupported C function type") - -/* Optimizations. */ -TREDEF(GFAIL, "guard would always fail") -TREDEF(PHIOV, "too many PHIs") -TREDEF(TYPEINS, "persistent type instability") - -/* Assembler. */ -TREDEF(MCODEAL, "failed to allocate mcode memory") -TREDEF(MCODEOV, "machine code too long") -TREDEF(MCODELM, "hit mcode limit (retrying)") -TREDEF(SPILLOV, "too many spill slots") -TREDEF(BADRA, "inconsistent register allocation") -TREDEF(NYIIR, "NYI: cannot assemble IR instruction %d") -TREDEF(NYIPHI, "NYI: PHI shuffling too complex") -TREDEF(NYICOAL, "NYI: register coalescing too complex") - -#undef TREDEF - -/* Detecting unused error messages: - awk -F, '/^TREDEF/ { gsub(/TREDEF./, ""); printf "grep -q LJ_TRERR_%s *.[ch] || echo %s\n", $1, $1}' lj_traceerr.h | sh -*/ diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.c deleted file mode 100644 index ee4a145..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.c +++ /dev/null @@ -1,62 +0,0 @@ -/* -** Userdata handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_udata_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_gc.h" -#include "lj_err.h" -#include "lj_udata.h" - -GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env) -{ - GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata); - global_State *g = G(L); - newwhite(g, ud); /* Not finalized. */ - ud->gct = ~LJ_TUDATA; - ud->udtype = UDTYPE_USERDATA; - ud->len = sz; - /* NOBARRIER: The GCudata is new (marked white). */ - setgcrefnull(ud->metatable); - setgcref(ud->env, obj2gco(env)); - /* Chain to userdata list (after main thread). */ - setgcrefr(ud->nextgc, mainthread(g)->nextgc); - setgcref(mainthread(g)->nextgc, obj2gco(ud)); - return ud; -} - -void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud) -{ - lj_mem_free(g, ud, sizeudata(ud)); -} - -#if LJ_64 -void *lj_lightud_intern(lua_State *L, void *p) -{ - global_State *g = G(L); - uint64_t u = (uint64_t)p; - uint32_t up = lightudup(u); - uint32_t *segmap = mref(g->gc.lightudseg, uint32_t); - MSize segnum = g->gc.lightudnum; - if (segmap) { - MSize seg; - for (seg = 0; seg <= segnum; seg++) - if (segmap[seg] == up) /* Fast path. */ - return (void *)(((uint64_t)seg << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); - segnum++; - /* Leave last segment unused to avoid clash with ITERN key. */ - if (segnum >= (1 << LJ_LIGHTUD_BITS_SEG)-1) lj_err_msg(L, LJ_ERR_BADLU); - } - if (!((segnum-1) & segnum) && segnum != 1) { - lj_mem_reallocvec(L, segmap, segnum, segnum ? 2*segnum : 2u, uint32_t); - setmref(g->gc.lightudseg, segmap); - } - g->gc.lightudnum = segnum; - segmap[segnum] = up; - return (void *)(((uint64_t)segnum << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); -} -#endif - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.h deleted file mode 100644 index 503c9e3..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_udata.h +++ /dev/null @@ -1,17 +0,0 @@ -/* -** Userdata handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_UDATA_H -#define _LJ_UDATA_H - -#include "lj_obj.h" - -LJ_FUNC GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env); -LJ_FUNC void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud); -#if LJ_64 -LJ_FUNC void * LJ_FASTCALL lj_lightud_intern(lua_State *L, void *p); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vm.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vm.h deleted file mode 100644 index c66db00..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vm.h +++ /dev/null @@ -1,116 +0,0 @@ -/* -** Assembler VM interface definitions. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_VM_H -#define _LJ_VM_H - -#include "lj_obj.h" - -/* Entry points for ASM parts of VM. */ -LJ_ASMF void lj_vm_call(lua_State *L, TValue *base, int nres1); -LJ_ASMF int lj_vm_pcall(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); -typedef TValue *(*lua_CPFunction)(lua_State *L, lua_CFunction func, void *ud); -LJ_ASMF int lj_vm_cpcall(lua_State *L, lua_CFunction func, void *ud, - lua_CPFunction cp); -LJ_ASMF int lj_vm_resume(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); -LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_c(void *cframe, int errcode); -LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_ff(void *cframe); -#if LJ_ABI_WIN && LJ_TARGET_X86 -LJ_ASMF_NORET void LJ_FASTCALL lj_vm_rtlunwind(void *cframe, void *excptrec, - void *unwinder, int errcode); -#endif -LJ_ASMF void lj_vm_unwind_c_eh(void); -LJ_ASMF void lj_vm_unwind_ff_eh(void); -#if LJ_TARGET_X86ORX64 -LJ_ASMF void lj_vm_unwind_rethrow(void); -#endif -#if LJ_TARGET_MIPS -LJ_ASMF void lj_vm_unwind_stub(void); -#endif - -/* Miscellaneous functions. */ -#if LJ_TARGET_X86ORX64 -LJ_ASMF int lj_vm_cpuid(uint32_t f, uint32_t res[4]); -#endif -#if LJ_TARGET_PPC -void lj_vm_cachesync(void *start, void *end); -#endif -LJ_ASMF double lj_vm_foldarith(double x, double y, int op); -#if LJ_HASJIT -LJ_ASMF double lj_vm_foldfpm(double x, int op); -#endif -#if !LJ_ARCH_HASFPU -/* Declared in lj_obj.h: LJ_ASMF int32_t lj_vm_tobit(double x); */ -#endif - -/* Dispatch targets for recording and hooks. */ -LJ_ASMF void lj_vm_record(void); -LJ_ASMF void lj_vm_inshook(void); -LJ_ASMF void lj_vm_rethook(void); -LJ_ASMF void lj_vm_callhook(void); -LJ_ASMF void lj_vm_profhook(void); -LJ_ASMF void lj_vm_IITERN(void); - -/* Trace exit handling. */ -LJ_ASMF void lj_vm_exit_handler(void); -LJ_ASMF void lj_vm_exit_interp(void); - -/* Internal math helper functions. */ -#if LJ_TARGET_PPC || LJ_TARGET_ARM64 || (LJ_TARGET_MIPS && LJ_ABI_SOFTFP) -#define lj_vm_floor floor -#define lj_vm_ceil ceil -#else -LJ_ASMF double lj_vm_floor(double); -LJ_ASMF double lj_vm_ceil(double); -#if LJ_TARGET_ARM -LJ_ASMF double lj_vm_floor_sf(double); -LJ_ASMF double lj_vm_ceil_sf(double); -#endif -#endif -#ifdef LUAJIT_NO_LOG2 -LJ_ASMF double lj_vm_log2(double); -#else -#define lj_vm_log2 log2 -#endif -#if !(defined(_LJ_DISPATCH_H) && LJ_TARGET_MIPS) -LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t, int32_t); -#endif - -#if LJ_HASJIT -#if LJ_TARGET_X86ORX64 -LJ_ASMF void lj_vm_floor_sse(void); -LJ_ASMF void lj_vm_ceil_sse(void); -LJ_ASMF void lj_vm_trunc_sse(void); -#endif -#if LJ_TARGET_PPC || LJ_TARGET_ARM64 -#define lj_vm_trunc trunc -#else -LJ_ASMF double lj_vm_trunc(double); -#if LJ_TARGET_ARM -LJ_ASMF double lj_vm_trunc_sf(double); -#endif -#endif -#if LJ_HASFFI -LJ_ASMF int lj_vm_errno(void); -#endif -LJ_ASMF TValue *lj_vm_next(GCtab *t, uint32_t idx); -#endif - -/* Continuations for metamethods. */ -LJ_ASMF void lj_cont_cat(void); /* Continue with concatenation. */ -LJ_ASMF void lj_cont_ra(void); /* Store result in RA from instruction. */ -LJ_ASMF void lj_cont_nop(void); /* Do nothing, just continue execution. */ -LJ_ASMF void lj_cont_condt(void); /* Branch if result is true. */ -LJ_ASMF void lj_cont_condf(void); /* Branch if result is false. */ -LJ_ASMF void lj_cont_hook(void); /* Continue from hook yield. */ -LJ_ASMF void lj_cont_stitch(void); /* Trace stitching. */ - -/* Start of the ASM code. */ -LJ_ASMF char lj_vm_asm_begin[]; - -/* Bytecode offsets are relative to lj_vm_asm_begin. */ -#define makeasmfunc(ofs) ((ASMFunction)(lj_vm_asm_begin + (ofs))) - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.c deleted file mode 100644 index c8491d8..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.c +++ /dev/null @@ -1,58 +0,0 @@ -/* -** VM event handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#include - -#define lj_vmevent_c -#define LUA_CORE - -#include "lj_obj.h" -#include "lj_str.h" -#include "lj_tab.h" -#include "lj_state.h" -#include "lj_dispatch.h" -#include "lj_vm.h" -#include "lj_vmevent.h" - -ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev) -{ - global_State *g = G(L); - GCstr *s = lj_str_newlit(L, LJ_VMEVENTS_REGKEY); - cTValue *tv = lj_tab_getstr(tabV(registry(L)), s); - if (tvistab(tv)) { - int hash = VMEVENT_HASH(ev); - tv = lj_tab_getint(tabV(tv), hash); - if (tv && tvisfunc(tv)) { - lj_state_checkstack(L, LUA_MINSTACK); - setfuncV(L, L->top++, funcV(tv)); - if (LJ_FR2) setnilV(L->top++); - return savestack(L, L->top); - } - } - g->vmevmask &= ~VMEVENT_MASK(ev); /* No handler: cache this fact. */ - return 0; -} - -void lj_vmevent_call(lua_State *L, ptrdiff_t argbase) -{ - global_State *g = G(L); - uint8_t oldmask = g->vmevmask; - uint8_t oldh = hook_save(g); - int status; - g->vmevmask = 0; /* Disable all events. */ - hook_vmevent(g); - status = lj_vm_pcall(L, restorestack(L, argbase), 0+1, 0); - if (LJ_UNLIKELY(status)) { - /* Really shouldn't use stderr here, but where else to complain? */ - L->top--; - fputs("VM handler failed: ", stderr); - fputs(tvisstr(L->top) ? strVdata(L->top) : "?", stderr); - fputc('\n', stderr); - } - hook_restore(g, oldh); - if (g->vmevmask != VMEVENT_NOCACHE) - g->vmevmask = oldmask; /* Restore event mask, but not if not modified. */ -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.h deleted file mode 100644 index 40f9c63..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmevent.h +++ /dev/null @@ -1,59 +0,0 @@ -/* -** VM event handling. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LJ_VMEVENT_H -#define _LJ_VMEVENT_H - -#include "lj_obj.h" - -/* Registry key for VM event handler table. */ -#define LJ_VMEVENTS_REGKEY "_VMEVENTS" -#define LJ_VMEVENTS_HSIZE 4 - -#define VMEVENT_MASK(ev) ((uint8_t)1 << ((int)(ev) & 7)) -#define VMEVENT_HASH(ev) ((int)(ev) & ~7) -#define VMEVENT_HASHIDX(h) ((int)(h) << 3) -#define VMEVENT_NOCACHE 255 - -#define VMEVENT_DEF(name, hash) \ - LJ_VMEVENT_##name##_, \ - LJ_VMEVENT_##name = ((LJ_VMEVENT_##name##_) & 7)|((hash) << 3) - -/* VM event IDs. */ -typedef enum { - VMEVENT_DEF(BC, 0x00003883), - VMEVENT_DEF(TRACE, 0xb2d91467), - VMEVENT_DEF(RECORD, 0x9284bf4f), - VMEVENT_DEF(TEXIT, 0xb29df2b0), - LJ_VMEVENT__MAX -} VMEvent; - -#ifdef LUAJIT_DISABLE_VMEVENT -#define lj_vmevent_send(L, ev, args) UNUSED(L) -#define lj_vmevent_send_(L, ev, args, post) UNUSED(L) -#else -#define lj_vmevent_send(L, ev, args) \ - if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ - ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ - if (argbase) { \ - args \ - lj_vmevent_call(L, argbase); \ - } \ - } -#define lj_vmevent_send_(L, ev, args, post) \ - if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ - ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ - if (argbase) { \ - args \ - lj_vmevent_call(L, argbase); \ - post \ - } \ - } - -LJ_FUNC ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev); -LJ_FUNC void lj_vmevent_call(lua_State *L, ptrdiff_t argbase); -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmmath.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmmath.c deleted file mode 100644 index d0febd8..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lj_vmmath.c +++ /dev/null @@ -1,118 +0,0 @@ -/* -** Math helper functions for assembler VM. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define lj_vmmath_c -#define LUA_CORE - -#include -#include - -#include "lj_obj.h" -#include "lj_ir.h" -#include "lj_vm.h" - -/* -- Wrapper functions --------------------------------------------------- */ - -#if LJ_TARGET_X86 && __ELF__ && __PIC__ -/* Wrapper functions to deal with the ELF/x86 PIC disaster. */ -LJ_FUNCA double lj_wrap_log(double x) { return log(x); } -LJ_FUNCA double lj_wrap_log10(double x) { return log10(x); } -LJ_FUNCA double lj_wrap_exp(double x) { return exp(x); } -LJ_FUNCA double lj_wrap_sin(double x) { return sin(x); } -LJ_FUNCA double lj_wrap_cos(double x) { return cos(x); } -LJ_FUNCA double lj_wrap_tan(double x) { return tan(x); } -LJ_FUNCA double lj_wrap_asin(double x) { return asin(x); } -LJ_FUNCA double lj_wrap_acos(double x) { return acos(x); } -LJ_FUNCA double lj_wrap_atan(double x) { return atan(x); } -LJ_FUNCA double lj_wrap_sinh(double x) { return sinh(x); } -LJ_FUNCA double lj_wrap_cosh(double x) { return cosh(x); } -LJ_FUNCA double lj_wrap_tanh(double x) { return tanh(x); } -LJ_FUNCA double lj_wrap_atan2(double x, double y) { return atan2(x, y); } -LJ_FUNCA double lj_wrap_pow(double x, double y) { return pow(x, y); } -LJ_FUNCA double lj_wrap_fmod(double x, double y) { return fmod(x, y); } -#endif - -/* -- Helper functions ---------------------------------------------------- */ - -/* Required to prevent the C compiler from applying FMA optimizations. -** -** Yes, there's -ffp-contract and the FP_CONTRACT pragma ... in theory. -** But the current state of C compilers is a mess in this regard. -** Also, this function is not performance sensitive at all. -*/ -LJ_NOINLINE static double lj_vm_floormul(double x, double y) -{ - return lj_vm_floor(x / y) * y; -} - -double lj_vm_foldarith(double x, double y, int op) -{ - switch (op) { - case IR_ADD - IR_ADD: return x+y; break; - case IR_SUB - IR_ADD: return x-y; break; - case IR_MUL - IR_ADD: return x*y; break; - case IR_DIV - IR_ADD: return x/y; break; - case IR_MOD - IR_ADD: return x-lj_vm_floormul(x, y); break; - case IR_POW - IR_ADD: return pow(x, y); break; - case IR_NEG - IR_ADD: return -x; break; - case IR_ABS - IR_ADD: return fabs(x); break; -#if LJ_HASJIT - case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; - case IR_MIN - IR_ADD: return x < y ? x : y; break; - case IR_MAX - IR_ADD: return x > y ? x : y; break; -#endif - default: return x; - } -} - -/* -- Helper functions for generated machine code ------------------------- */ - -#if (LJ_HASJIT && !(LJ_TARGET_ARM || LJ_TARGET_ARM64 || LJ_TARGET_PPC)) || LJ_TARGET_MIPS -int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b) -{ - uint32_t y, ua, ub; - /* This must be checked before using this function. */ - lj_assertX(b != 0, "modulo with zero divisor"); - ua = a < 0 ? (uint32_t)-a : (uint32_t)a; - ub = b < 0 ? (uint32_t)-b : (uint32_t)b; - y = ua % ub; - if (y != 0 && (a^b) < 0) y = y - ub; - if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y; - return (int32_t)y; -} -#endif - -#if LJ_HASJIT - -#ifdef LUAJIT_NO_LOG2 -double lj_vm_log2(double a) -{ - return log(a) * 1.4426950408889634074; -} -#endif - -/* Computes fpm(x) for extended math functions. */ -double lj_vm_foldfpm(double x, int fpm) -{ - switch (fpm) { - case IRFPM_FLOOR: return lj_vm_floor(x); - case IRFPM_CEIL: return lj_vm_ceil(x); - case IRFPM_TRUNC: return lj_vm_trunc(x); - case IRFPM_SQRT: return sqrt(x); - case IRFPM_LOG: return log(x); - case IRFPM_LOG2: return lj_vm_log2(x); - default: lj_assertX(0, "bad fpm %d", fpm); - } - return 0; -} - -#if LJ_HASFFI -int lj_vm_errno(void) -{ - return errno; -} -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ljamalg.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ljamalg.c deleted file mode 100644 index cae8356..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ljamalg.c +++ /dev/null @@ -1,91 +0,0 @@ -/* -** LuaJIT core and libraries amalgamation. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#define ljamalg_c -#define LUA_CORE - -/* To get the mremap prototype. Must be defined before any system includes. */ -#if defined(__linux__) && !defined(_GNU_SOURCE) -#define _GNU_SOURCE -#endif - -#ifndef WINVER -#define WINVER 0x0501 -#endif - -#include "lua.h" -#include "lauxlib.h" - -#include "lj_assert.c" -#include "lj_gc.c" -#include "lj_err.c" -#include "lj_char.c" -#include "lj_bc.c" -#include "lj_obj.c" -#include "lj_buf.c" -#include "lj_str.c" -#include "lj_tab.c" -#include "lj_func.c" -#include "lj_udata.c" -#include "lj_meta.c" -#include "lj_debug.c" -#include "lj_prng.c" -#include "lj_state.c" -#include "lj_dispatch.c" -#include "lj_vmevent.c" -#include "lj_vmmath.c" -#include "lj_strscan.c" -#include "lj_strfmt.c" -#include "lj_strfmt_num.c" -#include "lj_serialize.c" -#include "lj_api.c" -#include "lj_profile.c" -#include "lj_lex.c" -#include "lj_parse.c" -#include "lj_bcread.c" -#include "lj_bcwrite.c" -#include "lj_load.c" -#include "lj_ctype.c" -#include "lj_cdata.c" -#include "lj_cconv.c" -#include "lj_ccall.c" -#include "lj_ccallback.c" -#include "lj_carith.c" -#include "lj_clib.c" -#include "lj_cparse.c" -#include "lj_lib.c" -#include "lj_ir.c" -#include "lj_opt_mem.c" -#include "lj_opt_fold.c" -#include "lj_opt_narrow.c" -#include "lj_opt_dce.c" -#include "lj_opt_loop.c" -#include "lj_opt_split.c" -#include "lj_opt_sink.c" -#include "lj_mcode.c" -#include "lj_snap.c" -#include "lj_record.c" -#include "lj_crecord.c" -#include "lj_ffrecord.c" -#include "lj_asm.c" -#include "lj_trace.c" -#include "lj_gdbjit.c" -#include "lj_alloc.c" - -#include "lib_aux.c" -#include "lib_base.c" -#include "lib_math.c" -#include "lib_string.c" -#include "lib_table.c" -#include "lib_io.c" -#include "lib_os.c" -#include "lib_package.c" -#include "lib_debug.c" -#include "lib_bit.c" -#include "lib_jit.c" -#include "lib_ffi.c" -#include "lib_buffer.c" -#include "lib_init.c" - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.h deleted file mode 100644 index 6d1634d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.h +++ /dev/null @@ -1,402 +0,0 @@ -/* -** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ -** Lua - An Extensible Extension Language -** Lua.org, PUC-Rio, Brazil (https://www.lua.org) -** See Copyright Notice at the end of this file -*/ - - -#ifndef lua_h -#define lua_h - -#include -#include - - -#include "luaconf.h" - - -#define LUA_VERSION "Lua 5.1" -#define LUA_RELEASE "Lua 5.1.4" -#define LUA_VERSION_NUM 501 -#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" - - -/* mark for precompiled code (`Lua') */ -#define LUA_SIGNATURE "\033Lua" - -/* option for multiple returns in `lua_pcall' and `lua_call' */ -#define LUA_MULTRET (-1) - - -/* -** pseudo-indices -*/ -#define LUA_REGISTRYINDEX (-10000) -#define LUA_ENVIRONINDEX (-10001) -#define LUA_GLOBALSINDEX (-10002) -#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) - - -/* thread status */ -#define LUA_OK 0 -#define LUA_YIELD 1 -#define LUA_ERRRUN 2 -#define LUA_ERRSYNTAX 3 -#define LUA_ERRMEM 4 -#define LUA_ERRERR 5 - - -typedef struct lua_State lua_State; - -typedef int (*lua_CFunction) (lua_State *L); - - -/* -** functions that read/write blocks when loading/dumping Lua chunks -*/ -typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); - -typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); - - -/* -** prototype for memory-allocation functions -*/ -typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); - - -/* -** basic types -*/ -#define LUA_TNONE (-1) - -#define LUA_TNIL 0 -#define LUA_TBOOLEAN 1 -#define LUA_TLIGHTUSERDATA 2 -#define LUA_TNUMBER 3 -#define LUA_TSTRING 4 -#define LUA_TTABLE 5 -#define LUA_TFUNCTION 6 -#define LUA_TUSERDATA 7 -#define LUA_TTHREAD 8 - - - -/* minimum Lua stack available to a C function */ -#define LUA_MINSTACK 20 - - -/* -** generic extra include file -*/ -#if defined(LUA_USER_H) -#include LUA_USER_H -#endif - - -/* type of numbers in Lua */ -typedef LUA_NUMBER lua_Number; - - -/* type for integer functions */ -typedef LUA_INTEGER lua_Integer; - - - -/* -** state manipulation -*/ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); -LUA_API void (lua_close) (lua_State *L); -LUA_API lua_State *(lua_newthread) (lua_State *L); - -LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); - - -/* -** basic stack manipulation -*/ -LUA_API int (lua_gettop) (lua_State *L); -LUA_API void (lua_settop) (lua_State *L, int idx); -LUA_API void (lua_pushvalue) (lua_State *L, int idx); -LUA_API void (lua_remove) (lua_State *L, int idx); -LUA_API void (lua_insert) (lua_State *L, int idx); -LUA_API void (lua_replace) (lua_State *L, int idx); -LUA_API int (lua_checkstack) (lua_State *L, int sz); - -LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); - - -/* -** access functions (stack -> C) -*/ - -LUA_API int (lua_isnumber) (lua_State *L, int idx); -LUA_API int (lua_isstring) (lua_State *L, int idx); -LUA_API int (lua_iscfunction) (lua_State *L, int idx); -LUA_API int (lua_isuserdata) (lua_State *L, int idx); -LUA_API int (lua_type) (lua_State *L, int idx); -LUA_API const char *(lua_typename) (lua_State *L, int tp); - -LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); -LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); - -LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); -LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); -LUA_API int (lua_toboolean) (lua_State *L, int idx); -LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API size_t (lua_objlen) (lua_State *L, int idx); -LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); -LUA_API void *(lua_touserdata) (lua_State *L, int idx); -LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); -LUA_API const void *(lua_topointer) (lua_State *L, int idx); - - -/* -** push functions (C -> stack) -*/ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); -LUA_API void (lua_pushstring) (lua_State *L, const char *s); -LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, - va_list argp); -LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); -LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); -LUA_API void (lua_pushboolean) (lua_State *L, int b); -LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); -LUA_API int (lua_pushthread) (lua_State *L); - - -/* -** get functions (Lua -> stack) -*/ -LUA_API void (lua_gettable) (lua_State *L, int idx); -LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawget) (lua_State *L, int idx); -LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); -LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API void (lua_getfenv) (lua_State *L, int idx); - - -/* -** set functions (stack -> Lua) -*/ -LUA_API void (lua_settable) (lua_State *L, int idx); -LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); -LUA_API void (lua_rawset) (lua_State *L, int idx); -LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); -LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API int (lua_setfenv) (lua_State *L, int idx); - - -/* -** `load' and `call' functions (load and run Lua code) -*/ -LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); -LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); -LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); -LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname); - -LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); - - -/* -** coroutine functions -*/ -LUA_API int (lua_yield) (lua_State *L, int nresults); -LUA_API int (lua_resume) (lua_State *L, int narg); -LUA_API int (lua_status) (lua_State *L); - -/* -** garbage-collection function and options -*/ - -#define LUA_GCSTOP 0 -#define LUA_GCRESTART 1 -#define LUA_GCCOLLECT 2 -#define LUA_GCCOUNT 3 -#define LUA_GCCOUNTB 4 -#define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 -#define LUA_GCISRUNNING 9 - -LUA_API int (lua_gc) (lua_State *L, int what, int data); - - -/* -** miscellaneous functions -*/ - -LUA_API int (lua_error) (lua_State *L); - -LUA_API int (lua_next) (lua_State *L, int idx); - -LUA_API void (lua_concat) (lua_State *L, int n); - -LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); -LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); - - - -/* -** =============================================================== -** some useful macros -** =============================================================== -*/ - -#define lua_pop(L,n) lua_settop(L, -(n)-1) - -#define lua_newtable(L) lua_createtable(L, 0, 0) - -#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) - -#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) - -#define lua_strlen(L,i) lua_objlen(L, (i)) - -#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) -#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) -#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) -#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) -#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) -#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) -#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) -#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) - -#define lua_pushliteral(L, s) \ - lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) - -#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) -#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) - -#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) - - - -/* -** compatibility macros and functions -*/ - -#define lua_open() luaL_newstate() - -#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) - -#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) - -#define lua_Chunkreader lua_Reader -#define lua_Chunkwriter lua_Writer - - -/* hack */ -LUA_API void lua_setlevel (lua_State *from, lua_State *to); - - -/* -** {====================================================================== -** Debug API -** ======================================================================= -*/ - - -/* -** Event codes -*/ -#define LUA_HOOKCALL 0 -#define LUA_HOOKRET 1 -#define LUA_HOOKLINE 2 -#define LUA_HOOKCOUNT 3 -#define LUA_HOOKTAILRET 4 - - -/* -** Event masks -*/ -#define LUA_MASKCALL (1 << LUA_HOOKCALL) -#define LUA_MASKRET (1 << LUA_HOOKRET) -#define LUA_MASKLINE (1 << LUA_HOOKLINE) -#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) - -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debuger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); -LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); -LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); -LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); -LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); -LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); -LUA_API lua_Hook lua_gethook (lua_State *L); -LUA_API int lua_gethookmask (lua_State *L); -LUA_API int lua_gethookcount (lua_State *L); - -/* From Lua 5.2. */ -LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); -LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); -LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, - const char *chunkname, const char *mode); -LUA_API const lua_Number *lua_version (lua_State *L); -LUA_API void lua_copy (lua_State *L, int fromidx, int toidx); -LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum); -LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum); - -/* From Lua 5.3. */ -LUA_API int lua_isyieldable (lua_State *L); - - -struct lua_Debug { - int event; - const char *name; /* (n) */ - const char *namewhat; /* (n) `global', `local', `field', `method' */ - const char *what; /* (S) `Lua', `C', `main', `tail' */ - const char *source; /* (S) */ - int currentline; /* (l) */ - int nups; /* (u) number of upvalues */ - int linedefined; /* (S) */ - int lastlinedefined; /* (S) */ - char short_src[LUA_IDSIZE]; /* (S) */ - /* private part */ - int i_ci; /* active function */ -}; - -/* }====================================================================== */ - - -/****************************************************************************** -* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -******************************************************************************/ - - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.hpp b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.hpp deleted file mode 100644 index 07e9002..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lua.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// C++ wrapper for LuaJIT header files. - -extern "C" { -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#include "luajit.h" -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luaconf.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luaconf.h deleted file mode 100644 index e8790c1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luaconf.h +++ /dev/null @@ -1,152 +0,0 @@ -/* -** Configuration header. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef luaconf_h -#define luaconf_h - -#ifndef WINVER -#define WINVER 0x0501 -#endif -#include -#include - -/* Default path for loading Lua and C modules with require(). */ -#if defined(_WIN32) -/* -** In Windows, any exclamation mark ('!') in the path is replaced by the -** path of the directory of the executable file of the current process. -*/ -#define LUA_LDIR "!\\lua\\" -#define LUA_CDIR "!\\" -#define LUA_PATH_DEFAULT \ - ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" -#define LUA_CPATH_DEFAULT \ - ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" -#else -/* -** Note to distribution maintainers: do NOT patch the following lines! -** Please read ../doc/install.html#distro and pass PREFIX=/usr instead. -*/ -#ifndef LUA_MULTILIB -#define LUA_MULTILIB "lib" -#endif -#ifndef LUA_LMULTILIB -#define LUA_LMULTILIB "lib" -#endif -#define LUA_LROOT "/usr/local" -#define LUA_LUADIR "/lua/5.1/" -#define LUA_LJDIR "/luajit-2.1.0-beta3/" - -#ifdef LUA_ROOT -#define LUA_JROOT LUA_ROOT -#define LUA_RLDIR LUA_ROOT "/share" LUA_LUADIR -#define LUA_RCDIR LUA_ROOT "/" LUA_MULTILIB LUA_LUADIR -#define LUA_RLPATH ";" LUA_RLDIR "?.lua;" LUA_RLDIR "?/init.lua" -#define LUA_RCPATH ";" LUA_RCDIR "?.so" -#else -#define LUA_JROOT LUA_LROOT -#define LUA_RLPATH -#define LUA_RCPATH -#endif - -#define LUA_JPATH ";" LUA_JROOT "/share" LUA_LJDIR "?.lua" -#define LUA_LLDIR LUA_LROOT "/share" LUA_LUADIR -#define LUA_LCDIR LUA_LROOT "/" LUA_LMULTILIB LUA_LUADIR -#define LUA_LLPATH ";" LUA_LLDIR "?.lua;" LUA_LLDIR "?/init.lua" -#define LUA_LCPATH1 ";" LUA_LCDIR "?.so" -#define LUA_LCPATH2 ";" LUA_LCDIR "loadall.so" - -#define LUA_PATH_DEFAULT "./?.lua" LUA_JPATH LUA_LLPATH LUA_RLPATH -#define LUA_CPATH_DEFAULT "./?.so" LUA_LCPATH1 LUA_RCPATH LUA_LCPATH2 -#endif - -/* Environment variable names for path overrides and initialization code. */ -#define LUA_PATH "LUA_PATH" -#define LUA_CPATH "LUA_CPATH" -#define LUA_INIT "LUA_INIT" - -/* Special file system characters. */ -#if defined(_WIN32) -#define LUA_DIRSEP "\\" -#else -#define LUA_DIRSEP "/" -#endif -#define LUA_PATHSEP ";" -#define LUA_PATH_MARK "?" -#define LUA_EXECDIR "!" -#define LUA_IGMARK "-" -#define LUA_PATH_CONFIG \ - LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \ - LUA_EXECDIR "\n" LUA_IGMARK "\n" - -/* Quoting in error messages. */ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - -/* Various tunables. */ -#define LUAI_MAXSTACK 65500 /* Max. # of stack slots for a thread (<64K). */ -#define LUAI_MAXCSTACK 8000 /* Max. # of stack slots for a C func (<10K). */ -#define LUAI_GCPAUSE 200 /* Pause GC until memory is at 200%. */ -#define LUAI_GCMUL 200 /* Run GC at 200% of allocation speed. */ -#define LUA_MAXCAPTURES 32 /* Max. pattern captures. */ - -/* Configuration for the frontend (the luajit executable). */ -#if defined(luajit_c) -#define LUA_PROGNAME "luajit" /* Fallback frontend name. */ -#define LUA_PROMPT "> " /* Interactive prompt. */ -#define LUA_PROMPT2 ">> " /* Continuation prompt. */ -#define LUA_MAXINPUT 512 /* Max. input line length. */ -#endif - -/* Note: changing the following defines breaks the Lua 5.1 ABI. */ -#define LUA_INTEGER ptrdiff_t -#define LUA_IDSIZE 60 /* Size of lua_Debug.short_src. */ -/* -** Size of lauxlib and io.* on-stack buffers. Weird workaround to avoid using -** unreasonable amounts of stack space, but still retain ABI compatibility. -** Blame Lua for depending on BUFSIZ in the ABI, blame **** for wrecking it. -*/ -#define LUAL_BUFFERSIZE (BUFSIZ > 16384 ? 8192 : BUFSIZ) - -/* The following defines are here only for compatibility with luaconf.h -** from the standard Lua distribution. They must not be changed for LuaJIT. -*/ -#define LUA_NUMBER_DOUBLE -#define LUA_NUMBER double -#define LUAI_UACNUMBER double -#define LUA_NUMBER_SCAN "%lf" -#define LUA_NUMBER_FMT "%.14g" -#define lua_number2str(s, n) sprintf((s), LUA_NUMBER_FMT, (n)) -#define LUAI_MAXNUMBER2STR 32 -#define LUA_INTFRMLEN "l" -#define LUA_INTFRM_T long - -/* Linkage of public API functions. */ -#if defined(LUA_BUILD_AS_DLL) -#if defined(LUA_CORE) || defined(LUA_LIB) -#define LUA_API __declspec(dllexport) -#else -#define LUA_API __declspec(dllimport) -#endif -#else -#define LUA_API extern -#endif - -#define LUALIB_API LUA_API - -/* Compatibility support for assertions. */ -#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) -#include -#endif -#ifdef LUA_USE_ASSERT -#define lua_assert(x) assert(x) -#endif -#ifdef LUA_USE_APICHECK -#define luai_apicheck(L, o) { (void)L; assert(o); } -#else -#define luai_apicheck(L, o) { (void)L; } -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.c b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.c deleted file mode 100644 index 6dd6402..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.c +++ /dev/null @@ -1,586 +0,0 @@ -/* -** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -** -** Major portions taken verbatim or adapted from the Lua interpreter. -** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h -*/ - -#include -#include -#include - -#define luajit_c - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#include "luajit.h" - -#include "lj_arch.h" - -#if LJ_TARGET_POSIX -#include -#define lua_stdin_is_tty() isatty(0) -#elif LJ_TARGET_WINDOWS -#include -#ifdef __BORLANDC__ -#define lua_stdin_is_tty() isatty(_fileno(stdin)) -#else -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) -#endif -#else -#define lua_stdin_is_tty() 1 -#endif - -#if !LJ_TARGET_CONSOLE -#include -#endif - -static lua_State *globalL = NULL; -static const char *progname = LUA_PROGNAME; -static char *empty_argv[2] = { NULL, NULL }; - -#if !LJ_TARGET_CONSOLE -static void lstop(lua_State *L, lua_Debug *ar) -{ - (void)ar; /* unused arg. */ - lua_sethook(L, NULL, 0, 0); - /* Avoid luaL_error -- a C hook doesn't add an extra frame. */ - luaL_where(L, 0); - lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1)); - lua_error(L); -} - -static void laction(int i) -{ - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); -} -#endif - -static void print_usage(void) -{ - fputs("usage: ", stderr); - fputs(progname, stderr); - fputs(" [options]... [script [args]...].\n" - "Available options are:\n" - " -e chunk Execute string " LUA_QL("chunk") ".\n" - " -l name Require library " LUA_QL("name") ".\n" - " -b ... Save or list bytecode.\n" - " -j cmd Perform LuaJIT control command.\n" - " -O[opt] Control LuaJIT optimizations.\n" - " -i Enter interactive mode after executing " LUA_QL("script") ".\n" - " -v Show version information.\n" - " -E Ignore environment variables.\n" - " -- Stop handling options.\n" - " - Execute stdin and stop handling options.\n", stderr); - fflush(stderr); -} - -static void l_message(const char *msg) -{ - if (progname) { fputs(progname, stderr); fputc(':', stderr); fputc(' ', stderr); } - fputs(msg, stderr); fputc('\n', stderr); - fflush(stderr); -} - -static int report(lua_State *L, int status) -{ - if (status && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; - l_message(msg); - lua_pop(L, 1); - } - return status; -} - -static int traceback(lua_State *L) -{ - if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */ - if (lua_isnoneornil(L, 1) || - !luaL_callmeta(L, 1, "__tostring") || - !lua_isstring(L, -1)) - return 1; /* Return non-string error object. */ - lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */ - } - luaL_traceback(L, L, lua_tostring(L, 1), 1); - return 1; -} - -static int docall(lua_State *L, int narg, int clear) -{ - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ -#if !LJ_TARGET_CONSOLE - signal(SIGINT, laction); -#endif - status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); -#if !LJ_TARGET_CONSOLE - signal(SIGINT, SIG_DFL); -#endif - lua_remove(L, base); /* remove traceback function */ - /* force a complete garbage collection in case of errors */ - if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0); - return status; -} - -static void print_version(void) -{ - fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout); -} - -static void print_jit_status(lua_State *L) -{ - int n; - const char *s; - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ - lua_remove(L, -2); - lua_getfield(L, -1, "status"); - lua_remove(L, -2); - n = lua_gettop(L); - lua_call(L, 0, LUA_MULTRET); - fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout); - for (n++; (s = lua_tostring(L, n)); n++) { - putc(' ', stdout); - fputs(s, stdout); - } - putc('\n', stdout); - lua_settop(L, 0); /* clear stack */ -} - -static void createargtable(lua_State *L, char **argv, int argc, int argf) -{ - int i; - lua_createtable(L, argc - argf, argf); - for (i = 0; i < argc; i++) { - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i - argf); - } - lua_setglobal(L, "arg"); -} - -static int dofile(lua_State *L, const char *name) -{ - int status = luaL_loadfile(L, name) || docall(L, 0, 1); - return report(L, status); -} - -static int dostring(lua_State *L, const char *s, const char *name) -{ - int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); - return report(L, status); -} - -static int dolibrary(lua_State *L, const char *name) -{ - lua_getglobal(L, "require"); - lua_pushstring(L, name); - return report(L, docall(L, 1, 1)); -} - -static void write_prompt(lua_State *L, int firstline) -{ - const char *p; - lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2; - fputs(p, stdout); - fflush(stdout); - lua_pop(L, 1); /* remove global */ -} - -static int incomplete(lua_State *L, int status) -{ - if (status == LUA_ERRSYNTAX) { - size_t lmsg; - const char *msg = lua_tolstring(L, -1, &lmsg); - const char *tp = msg + lmsg - (sizeof(LUA_QL("")) - 1); - if (strstr(msg, LUA_QL("")) == tp) { - lua_pop(L, 1); - return 1; - } - } - return 0; /* else... */ -} - -static int pushline(lua_State *L, int firstline) -{ - char buf[LUA_MAXINPUT]; - write_prompt(L, firstline); - if (fgets(buf, LUA_MAXINPUT, stdin)) { - size_t len = strlen(buf); - if (len > 0 && buf[len-1] == '\n') - buf[len-1] = '\0'; - if (firstline && buf[0] == '=') - lua_pushfstring(L, "return %s", buf+1); - else - lua_pushstring(L, buf); - return 1; - } - return 0; -} - -static int loadline(lua_State *L) -{ - int status; - lua_settop(L, 0); - if (!pushline(L, 1)) - return -1; /* no input */ - for (;;) { /* repeat until gets a complete line */ - status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); - if (!incomplete(L, status)) break; /* cannot try to add lines? */ - if (!pushline(L, 0)) /* no more input? */ - return -1; - lua_pushliteral(L, "\n"); /* add a new line... */ - lua_insert(L, -2); /* ...between the two lines */ - lua_concat(L, 3); /* join them */ - } - lua_remove(L, 1); /* remove line */ - return status; -} - -static void dotty(lua_State *L) -{ - int status; - const char *oldprogname = progname; - progname = NULL; - while ((status = loadline(L)) != -1) { - if (status == LUA_OK) status = docall(L, 0, 0); - report(L, status); - if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ - lua_getglobal(L, "print"); - lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) - l_message(lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", - lua_tostring(L, -1))); - } - } - lua_settop(L, 0); /* clear stack */ - fputs("\n", stdout); - fflush(stdout); - progname = oldprogname; -} - -static int handle_script(lua_State *L, char **argx) -{ - int status; - const char *fname = argx[0]; - if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - if (status == LUA_OK) { - /* Fetch args from arg table. LUA_INIT or -e might have changed them. */ - int narg = 0; - lua_getglobal(L, "arg"); - if (lua_istable(L, -1)) { - do { - narg++; - lua_rawgeti(L, -narg, narg); - } while (!lua_isnil(L, -1)); - lua_pop(L, 1); - lua_remove(L, -narg); - narg--; - } else { - lua_pop(L, 1); - } - status = docall(L, narg, 0); - } - return report(L, status); -} - -/* Load add-on module. */ -static int loadjitmodule(lua_State *L) -{ - lua_getglobal(L, "require"); - lua_pushliteral(L, "jit."); - lua_pushvalue(L, -3); - lua_concat(L, 2); - if (lua_pcall(L, 1, 1, 0)) { - const char *msg = lua_tostring(L, -1); - if (msg && !strncmp(msg, "module ", 7)) - goto nomodule; - return report(L, 1); - } - lua_getfield(L, -1, "start"); - if (lua_isnil(L, -1)) { - nomodule: - l_message("unknown luaJIT command or jit.* modules not installed"); - return 1; - } - lua_remove(L, -2); /* Drop module table. */ - return 0; -} - -/* Run command with options. */ -static int runcmdopt(lua_State *L, const char *opt) -{ - int narg = 0; - if (opt && *opt) { - for (;;) { /* Split arguments. */ - const char *p = strchr(opt, ','); - narg++; - if (!p) break; - if (p == opt) - lua_pushnil(L); - else - lua_pushlstring(L, opt, (size_t)(p - opt)); - opt = p + 1; - } - if (*opt) - lua_pushstring(L, opt); - else - lua_pushnil(L); - } - return report(L, lua_pcall(L, narg, 0, 0)); -} - -/* JIT engine control command: try jit library first or load add-on module. */ -static int dojitcmd(lua_State *L, const char *cmd) -{ - const char *opt = strchr(cmd, '='); - lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ - lua_remove(L, -2); - lua_pushvalue(L, -2); - lua_gettable(L, -2); /* Lookup library function. */ - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); /* Drop non-function and jit.* table, keep module name. */ - if (loadjitmodule(L)) - return 1; - } else { - lua_remove(L, -2); /* Drop jit.* table. */ - } - lua_remove(L, -2); /* Drop module name. */ - return runcmdopt(L, opt ? opt+1 : opt); -} - -/* Optimization flags. */ -static int dojitopt(lua_State *L, const char *opt) -{ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit.opt"); /* Get jit.opt.* module table. */ - lua_remove(L, -2); - lua_getfield(L, -1, "start"); - lua_remove(L, -2); - return runcmdopt(L, opt); -} - -/* Save or list bytecode. */ -static int dobytecode(lua_State *L, char **argv) -{ - int narg = 0; - lua_pushliteral(L, "bcsave"); - if (loadjitmodule(L)) - return 1; - if (argv[0][2]) { - narg++; - argv[0][1] = '-'; - lua_pushstring(L, argv[0]+1); - } - for (argv++; *argv != NULL; narg++, argv++) - lua_pushstring(L, *argv); - report(L, lua_pcall(L, narg, 0, 0)); - return -1; -} - -/* check that argument has no extra characters at the end */ -#define notail(x) {if ((x)[2] != '\0') return -1;} - -#define FLAGS_INTERACTIVE 1 -#define FLAGS_VERSION 2 -#define FLAGS_EXEC 4 -#define FLAGS_OPTION 8 -#define FLAGS_NOENV 16 - -static int collectargs(char **argv, int *flags) -{ - int i; - for (i = 1; argv[i] != NULL; i++) { - if (argv[i][0] != '-') /* Not an option? */ - return i; - switch (argv[i][1]) { /* Check option. */ - case '-': - notail(argv[i]); - return i+1; - case '\0': - return i; - case 'i': - notail(argv[i]); - *flags |= FLAGS_INTERACTIVE; - /* fallthrough */ - case 'v': - notail(argv[i]); - *flags |= FLAGS_VERSION; - break; - case 'e': - *flags |= FLAGS_EXEC; - /* fallthrough */ - case 'j': /* LuaJIT extension */ - case 'l': - *flags |= FLAGS_OPTION; - if (argv[i][2] == '\0') { - i++; - if (argv[i] == NULL) return -1; - } - break; - case 'O': break; /* LuaJIT extension */ - case 'b': /* LuaJIT extension */ - if (*flags) return -1; - *flags |= FLAGS_EXEC; - return i+1; - case 'E': - *flags |= FLAGS_NOENV; - break; - default: return -1; /* invalid option */ - } - } - return i; -} - -static int runargs(lua_State *L, char **argv, int argn) -{ - int i; - for (i = 1; i < argn; i++) { - if (argv[i] == NULL) continue; - lua_assert(argv[i][0] == '-'); - switch (argv[i][1]) { - case 'e': { - const char *chunk = argv[i] + 2; - if (*chunk == '\0') chunk = argv[++i]; - lua_assert(chunk != NULL); - if (dostring(L, chunk, "=(command line)") != 0) - return 1; - break; - } - case 'l': { - const char *filename = argv[i] + 2; - if (*filename == '\0') filename = argv[++i]; - lua_assert(filename != NULL); - if (dolibrary(L, filename)) - return 1; - break; - } - case 'j': { /* LuaJIT extension. */ - const char *cmd = argv[i] + 2; - if (*cmd == '\0') cmd = argv[++i]; - lua_assert(cmd != NULL); - if (dojitcmd(L, cmd)) - return 1; - break; - } - case 'O': /* LuaJIT extension. */ - if (dojitopt(L, argv[i] + 2)) - return 1; - break; - case 'b': /* LuaJIT extension. */ - return dobytecode(L, argv+i); - default: break; - } - } - return LUA_OK; -} - -static int handle_luainit(lua_State *L) -{ -#if LJ_TARGET_CONSOLE - const char *init = NULL; -#else - const char *init = getenv(LUA_INIT); -#endif - if (init == NULL) - return LUA_OK; - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, "=" LUA_INIT); -} - -static struct Smain { - char **argv; - int argc; - int status; -} smain; - -static int pmain(lua_State *L) -{ - struct Smain *s = &smain; - char **argv = s->argv; - int argn; - int flags = 0; - globalL = L; - LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */ - - argn = collectargs(argv, &flags); - if (argn < 0) { /* Invalid args? */ - print_usage(); - s->status = 1; - return 0; - } - - if ((flags & FLAGS_NOENV)) { - lua_pushboolean(L, 1); - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } - - /* Stop collector during library initialization. */ - lua_gc(L, LUA_GCSTOP, 0); - luaL_openlibs(L); - lua_gc(L, LUA_GCRESTART, -1); - - createargtable(L, argv, s->argc, argn); - - if (!(flags & FLAGS_NOENV)) { - s->status = handle_luainit(L); - if (s->status != LUA_OK) return 0; - } - - if ((flags & FLAGS_VERSION)) print_version(); - - s->status = runargs(L, argv, argn); - if (s->status != LUA_OK) return 0; - - if (s->argc > argn) { - s->status = handle_script(L, argv + argn); - if (s->status != LUA_OK) return 0; - } - - if ((flags & FLAGS_INTERACTIVE)) { - print_jit_status(L); - dotty(L); - } else if (s->argc == argn && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) { - if (lua_stdin_is_tty()) { - print_version(); - print_jit_status(L); - dotty(L); - } else { - dofile(L, NULL); /* Executes stdin as a file. */ - } - } - return 0; -} - -int main(int argc, char **argv) -{ - int status; - lua_State *L; - if (!argv[0]) argv = empty_argv; else if (argv[0][0]) progname = argv[0]; - L = lua_open(); - if (L == NULL) { - l_message("cannot create state: not enough memory"); - return EXIT_FAILURE; - } - smain.argc = argc; - smain.argv = argv; - status = lua_cpcall(L, pmain, NULL); - report(L, status); - lua_close(L); - return (status || smain.status > 0) ? EXIT_FAILURE : EXIT_SUCCESS; -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.h deleted file mode 100644 index 31f1eb1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/luajit.h +++ /dev/null @@ -1,79 +0,0 @@ -/* -** LuaJIT -- a Just-In-Time Compiler for Lua. https://luajit.org/ -** -** Copyright (C) 2005-2022 Mike Pall. All rights reserved. -** -** Permission is hereby granted, free of charge, to any person obtaining -** a copy of this software and associated documentation files (the -** "Software"), to deal in the Software without restriction, including -** without limitation the rights to use, copy, modify, merge, publish, -** distribute, sublicense, and/or sell copies of the Software, and to -** permit persons to whom the Software is furnished to do so, subject to -** the following conditions: -** -** The above copyright notice and this permission notice shall be -** included in all copies or substantial portions of the Software. -** -** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -** -** [ MIT license: https://www.opensource.org/licenses/mit-license.php ] -*/ - -#ifndef _LUAJIT_H -#define _LUAJIT_H - -#include "lua.h" - -#define LUAJIT_VERSION "LuaJIT 2.1.0-beta3" -#define LUAJIT_VERSION_NUM 20100 /* Version 2.1.0 = 02.01.00. */ -#define LUAJIT_VERSION_SYM luaJIT_version_2_1_0_beta3 -#define LUAJIT_COPYRIGHT "Copyright (C) 2005-2022 Mike Pall" -#define LUAJIT_URL "https://luajit.org/" - -/* Modes for luaJIT_setmode. */ -#define LUAJIT_MODE_MASK 0x00ff - -enum { - LUAJIT_MODE_ENGINE, /* Set mode for whole JIT engine. */ - LUAJIT_MODE_DEBUG, /* Set debug mode (idx = level). */ - - LUAJIT_MODE_FUNC, /* Change mode for a function. */ - LUAJIT_MODE_ALLFUNC, /* Recurse into subroutine protos. */ - LUAJIT_MODE_ALLSUBFUNC, /* Change only the subroutines. */ - - LUAJIT_MODE_TRACE, /* Flush a compiled trace. */ - - LUAJIT_MODE_WRAPCFUNC = 0x10, /* Set wrapper mode for C function calls. */ - - LUAJIT_MODE_MAX -}; - -/* Flags or'ed in to the mode. */ -#define LUAJIT_MODE_OFF 0x0000 /* Turn feature off. */ -#define LUAJIT_MODE_ON 0x0100 /* Turn feature on. */ -#define LUAJIT_MODE_FLUSH 0x0200 /* Flush JIT-compiled code. */ - -/* LuaJIT public C API. */ - -/* Control the JIT engine. */ -LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode); - -/* Low-overhead profiling API. */ -typedef void (*luaJIT_profile_callback)(void *data, lua_State *L, - int samples, int vmstate); -LUA_API void luaJIT_profile_start(lua_State *L, const char *mode, - luaJIT_profile_callback cb, void *data); -LUA_API void luaJIT_profile_stop(lua_State *L); -LUA_API const char *luaJIT_profile_dumpstack(lua_State *L, const char *fmt, - int depth, size_t *len); - -/* Enforce (dynamic) linker error for version mismatches. Call from main. */ -LUA_API void LUAJIT_VERSION_SYM(void); - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lualib.h b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lualib.h deleted file mode 100644 index 8774845..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/lualib.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -** Standard library header. -** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -*/ - -#ifndef _LUALIB_H -#define _LUALIB_H - -#include "lua.h" - -#define LUA_FILEHANDLE "FILE*" - -#define LUA_COLIBNAME "coroutine" -#define LUA_MATHLIBNAME "math" -#define LUA_STRLIBNAME "string" -#define LUA_TABLIBNAME "table" -#define LUA_IOLIBNAME "io" -#define LUA_OSLIBNAME "os" -#define LUA_LOADLIBNAME "package" -#define LUA_DBLIBNAME "debug" -#define LUA_BITLIBNAME "bit" -#define LUA_JITLIBNAME "jit" -#define LUA_FFILIBNAME "ffi" - -LUALIB_API int luaopen_base(lua_State *L); -LUALIB_API int luaopen_math(lua_State *L); -LUALIB_API int luaopen_string(lua_State *L); -LUALIB_API int luaopen_table(lua_State *L); -LUALIB_API int luaopen_io(lua_State *L); -LUALIB_API int luaopen_os(lua_State *L); -LUALIB_API int luaopen_package(lua_State *L); -LUALIB_API int luaopen_debug(lua_State *L); -LUALIB_API int luaopen_bit(lua_State *L); -LUALIB_API int luaopen_jit(lua_State *L); -LUALIB_API int luaopen_ffi(lua_State *L); -LUALIB_API int luaopen_string_buffer(lua_State *L); - -LUALIB_API void luaL_openlibs(lua_State *L); - -#ifndef lua_assert -#define lua_assert(x) ((void)0) -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/msvcbuild.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/msvcbuild.bat deleted file mode 100644 index aab4ef1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/msvcbuild.bat +++ /dev/null @@ -1,127 +0,0 @@ -@rem Script to build LuaJIT with MSVC. -@rem Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -@rem -@rem Open a "Visual Studio Command Prompt" (either x86 or x64). -@rem Then cd to this directory and run this script. Use the following -@rem options (in order), if needed. The default is a dynamic release build. -@rem -@rem nogc64 disable LJ_GC64 mode for x64 -@rem debug emit debug symbols -@rem amalg amalgamated build -@rem static static linkage - -@if not defined INCLUDE goto :FAIL - -@setlocal -@rem Add more debug flags here, e.g. DEBUGCFLAGS=/DLUA_USE_APICHECK -@set DEBUGCFLAGS= -@set LJCOMPILE=cl /nologo /c /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_STDIO_INLINE=__declspec(dllexport)__inline -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set LJLIB=lib /nologo /nodefaultlib -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set DASC=vm_x64.dasc -@set LJDLLNAME=lua51.dll -@set LJLIBNAME=lua51.lib -@set BUILDTYPE=release -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@set DASMFLAGS=-D WIN -D JIT -D FFI -D P64 -@set LJARCH=x64 -@minilua -@if errorlevel 8 goto :X64 -@set DASC=vm_x86.dasc -@set DASMFLAGS=-D WIN -D JIT -D FFI -@set LJARCH=x86 -@set LJCOMPILE=%LJCOMPILE% /arch:SSE2 -:X64 -@if "%1" neq "nogc64" goto :GC64 -@shift -@set DASC=vm_x86.dasc -@set LJCOMPILE=%LJCOMPILE% /DLUAJIT_DISABLE_GC64 -:GC64 -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC% -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m peobj -o lj_vm.obj -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set BUILDTYPE=debug -@set LJCOMPILE=%LJCOMPILE% /Zi %DEBUGCFLAGS% -@set LJLINK=%LJLINK% /opt:ref /opt:icf /incremental:no -:NODEBUG -@set LJLINK=%LJLINK% /%BUILDTYPE% -@if "%1"=="amalg" goto :AMALGDLL -@if "%1"=="static" goto :STATIC -%LJCOMPILE% /MD /DLUA_BUILD_AS_DLL lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLINK% /DLL /out:%LJDLLNAME% lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :MTDLL -:STATIC -%LJCOMPILE% lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:%LJLIBNAME% lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :MTDLL -:AMALGDLL -%LJCOMPILE% /MD /DLUA_BUILD_AS_DLL ljamalg.c -@if errorlevel 1 goto :BAD -%LJLINK% /DLL /out:%LJDLLNAME% ljamalg.obj lj_vm.obj -@if errorlevel 1 goto :BAD -:MTDLL -if exist %LJDLLNAME%.manifest^ - %LJMT% -manifest %LJDLLNAME%.manifest -outputresource:%LJDLLNAME%;2 - -%LJCOMPILE% luajit.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:luajit.exe luajit.obj %LJLIBNAME% -@if errorlevel 1 goto :BAD -if exist luajit.exe.manifest^ - %LJMT% -manifest luajit.exe.manifest -outputresource:luajit.exe - -@del *.obj *.manifest minilua.exe buildvm.exe -@del host\buildvm_arch.h -@del lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h lj_folddef.h -@echo. -@echo === Successfully built LuaJIT for Windows/%LJARCH% === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo You must open a "Visual Studio Command Prompt" to run this script -:END diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/nxbuild.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/nxbuild.bat deleted file mode 100644 index e387439..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/nxbuild.bat +++ /dev/null @@ -1,160 +0,0 @@ -@rem Script to build LuaJIT with NintendoSDK + NX Addon. -@rem Donated to the public domain by Swyter. -@rem -@rem To run this script you must open a "Native Tools Command Prompt for VS". -@rem -@rem Either the x86 version for NX32, or x64 for the NX64 target. -@rem This is because the pointer size of the LuaJIT host tools (buildvm.exe) -@rem must match the cross-compiled target (32 or 64 bits). -@rem -@rem Then cd to this directory and run this script. -@rem -@rem Recommended invocation: -@rem -@rem nxbuild # release build, amalgamated -@rem nxbuild debug # debug build, amalgamated -@rem -@rem Additional command-line options (not generally recommended): -@rem -@rem noamalg # (after debug) non-amalgamated build - -@if not defined INCLUDE goto :FAIL -@if not defined NINTENDO_SDK_ROOT goto :FAIL -@if not defined PLATFORM goto :FAIL - -@if "%platform%" == "x86" goto :DO_NX32 -@if "%platform%" == "x64" goto :DO_NX64 - -@echo Error: Current host platform is %platform%! -@echo. -@goto :FAIL - -@setlocal - -:DO_NX32 -@set DASC=vm_arm.dasc -@set DASMFLAGS= -D HFABI -D FPU -@set DASMTARGET= -D LUAJIT_TARGET=LUAJIT_ARCH_ARM -@set HOST_PTR_SIZE=4 -goto :BEGIN - -:DO_NX64 -@set DASC=vm_arm64.dasc -@set DASMFLAGS= -D ENDIAN_LE -@set DASMTARGET= -D LUAJIT_TARGET=LUAJIT_ARCH_ARM64 -@set HOST_PTR_SIZE=8 - -:BEGIN -@rem ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /wo4146 /wo4244 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Check that we have the right 32/64 bit host compiler to generate the right virtual machine files. -@minilua -@if "%ERRORLEVEL%" == "%HOST_PTR_SIZE%" goto :PASSED_PTR_CHECK - -@echo The pointer size of the host in bytes (%HOST_PTR_SIZE%) does not match the expected value (%errorlevel%). -@echo Check that the script is being ran under the correct x86/x64 VS prompt. -@goto :BAD - -:PASSED_PTR_CHECK -@set DASMFLAGS=%DASMFLAGS% %DASMTARGET% -D LJ_TARGET_NX -D LUAJIT_OS=LUAJIT_OS_OTHER -D LUAJIT_DISABLE_JIT -D LUAJIT_DISABLE_FFI -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC% -@if errorlevel 1 goto :BAD -%LJCOMPILE% /I "." /I %DASMDIR% %DASMTARGET% -D LJ_TARGET_NX -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m elfasm -o lj_vm.s -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@rem ---- Cross compiler ---- -@if "%platform%" neq "x64" goto :NX32_CROSSBUILD -@set LJCOMPILE="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\aarch64\bin\clang" -Wall -I%NINTENDO_SDK_ROOT%\Include %DASMTARGET% -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -c -@set LJLIB="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\aarch64\bin\aarch64-nintendo-nx-elf-ar" rc -@set TARGETLIB_SUFFIX=nx64 - -%NINTENDO_SDK_ROOT%\Compilers\NX\nx\aarch64\bin\aarch64-nintendo-nx-elf-as -o lj_vm.o lj_vm.s -goto :DEBUGCHECK - -:NX32_CROSSBUILD -@set LJCOMPILE="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\armv7l\bin\clang" -Wall -I%NINTENDO_SDK_ROOT%\Include %DASMTARGET% -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -c -@set LJLIB="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\armv7l\bin\armv7l-nintendo-nx-eabihf-ar" rc -@set TARGETLIB_SUFFIX=nx32 - -%NINTENDO_SDK_ROOT%\Compilers\NX\nx\armv7l\bin\armv7l-nintendo-nx-eabihf-as -o lj_vm.o lj_vm.s -:DEBUGCHECK - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set LJCOMPILE=%LJCOMPILE% -DNN_SDK_BUILD_DEBUG -g -O0 -@set TARGETLIB=libluajitD_%TARGETLIB_SUFFIX%.a -goto :BUILD -:NODEBUG -@set LJCOMPILE=%LJCOMPILE% -DNN_SDK_BUILD_RELEASE -O3 -@set TARGETLIB=libluajit_%TARGETLIB_SUFFIX%.a -:BUILD -del %TARGETLIB% -@set LJCOMPILE=%LJCOMPILE% -fPIC -@if "%1" neq "noamalg" goto :AMALG -for %%f in (lj_*.c lib_*.c) do ( - %LJCOMPILE% %%f - @if errorlevel 1 goto :BAD -) - -%LJLIB% %TARGETLIB% lj_*.o lib_*.o -@if errorlevel 1 goto :BAD -@goto :NOAMALG -:AMALG -%LJCOMPILE% ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB% %TARGETLIB% ljamalg.o lj_vm.o -@if errorlevel 1 goto :BAD -:NOAMALG - -@del *.o *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for Nintendo Switch (%TARGETLIB_SUFFIX%) === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Native Tools Command Prompt for VS". -@echo. -@echo Either the x86 version for NX32, or x64 for the NX64 target. -@echo This is because the pointer size of the LuaJIT host tools (buildvm.exe) -@echo must match the cross-compiled target (32 or 64 bits). -@echo. -@echo Keep in mind that NintendoSDK + NX Addon must be installed, too. -:END diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps4build.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps4build.bat deleted file mode 100644 index ddbc198..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps4build.bat +++ /dev/null @@ -1,123 +0,0 @@ -@rem Script to build LuaJIT with the PS4 SDK. -@rem Donated to the public domain. -@rem -@rem Open a "Visual Studio .NET Command Prompt" (64 bit host compiler) -@rem or "VS2015 x64 Native Tools Command Prompt". -@rem -@rem Then cd to this directory and run this script. -@rem -@rem Recommended invocation: -@rem -@rem ps4build release build, amalgamated, 64-bit GC -@rem ps4build debug debug build, amalgamated, 64-bit GC -@rem -@rem Additional command-line options (not generally recommended): -@rem -@rem gc32 (before debug) 32-bit GC -@rem noamalg (after debug) non-amalgamated build - -@if not defined INCLUDE goto :FAIL -@if not defined SCE_ORBIS_SDK_DIR goto :FAIL - -@setlocal -@rem ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c -@set GC64= -@set DASC=vm_x64.dasc - -@if "%1" neq "gc32" goto :NOGC32 -@shift -@set GC64=-DLUAJIT_DISABLE_GC64 -@set DASC=vm_x86.dasc -:NOGC32 - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Check for 64 bit host compiler. -@minilua -@if not errorlevel 8 goto :FAIL - -@set DASMFLAGS=-D P64 -D NO_UNWIND -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC% -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% %GC64% -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -DLUAJIT_NO_UNWIND host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m elfasm -o lj_vm.s -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@rem ---- Cross compiler ---- -@set LJCOMPILE="%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-clang" -c -Wall -DLUAJIT_DISABLE_FFI %GC64% -@set LJLIB="%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-ar" rcus -@set INCLUDE="" - -"%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-as" -o lj_vm.o lj_vm.s - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set LJCOMPILE=%LJCOMPILE% -g -O0 -@set TARGETLIB=libluajitD_ps4.a -goto :BUILD -:NODEBUG -@set LJCOMPILE=%LJCOMPILE% -O2 -@set TARGETLIB=libluajit_ps4.a -:BUILD -del %TARGETLIB% -@if "%1" neq "noamalg" goto :AMALG -for %%f in (lj_*.c lib_*.c) do ( - %LJCOMPILE% %%f - @if errorlevel 1 goto :BAD -) - -%LJLIB% %TARGETLIB% lj_*.o lib_*.o -@if errorlevel 1 goto :BAD -@goto :NOAMALG -:AMALG -%LJCOMPILE% ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB% %TARGETLIB% ljamalg.o lj_vm.o -@if errorlevel 1 goto :BAD -:NOAMALG - -@del *.o *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for PS4 === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Visual Studio .NET Command Prompt" -@echo (64 bit host compiler). The PS4 Orbis SDK must be installed, too. -:END diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps5build.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps5build.bat deleted file mode 100644 index c0ce2f1..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/ps5build.bat +++ /dev/null @@ -1,123 +0,0 @@ -@rem Script to build LuaJIT with the PS5 SDK. -@rem Donated to the public domain. -@rem -@rem Open a "Visual Studio .NET Command Prompt" (64 bit host compiler) -@rem or "VS20xx x64 Native Tools Command Prompt". -@rem -@rem Then cd to this directory and run this script. -@rem -@rem Recommended invocation: -@rem -@rem ps5build release build, amalgamated, 64-bit GC -@rem ps5build debug debug build, amalgamated, 64-bit GC -@rem -@rem Additional command-line options (not generally recommended): -@rem -@rem gc32 (before debug) 32-bit GC -@rem noamalg (after debug) non-amalgamated build - -@if not defined INCLUDE goto :FAIL -@if not defined SCE_PROSPERO_SDK_DIR goto :FAIL - -@setlocal -@rem ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c -@set GC64= -@set DASC=vm_x64.dasc - -@if "%1" neq "gc32" goto :NOGC32 -@shift -@set GC64=-DLUAJIT_DISABLE_GC64 -@set DASC=vm_x86.dasc -:NOGC32 - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Check for 64 bit host compiler. -@minilua -@if not errorlevel 8 goto :FAIL - -@set DASMFLAGS=-D P64 -D NO_UNWIND -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC% -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% %GC64% -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_NO_UNWIND host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m elfasm -o lj_vm.s -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@rem ---- Cross compiler ---- -@set LJCOMPILE="%SCE_PROSPERO_SDK_DIR%\host_tools\bin\prospero-clang" -c -Wall -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC %GC64% -@set LJLIB="%SCE_PROSPERO_SDK_DIR%\host_tools\bin\prospero-llvm-ar" rcus -@set INCLUDE="" - -"%SCE_PROSPERO_SDK_DIR%\host_tools\bin\prospero-clang" -c -o lj_vm.o lj_vm.s - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set LJCOMPILE=%LJCOMPILE% -g -O0 -@set TARGETLIB=libluajitD_ps5.a -goto :BUILD -:NODEBUG -@set LJCOMPILE=%LJCOMPILE% -O2 -@set TARGETLIB=libluajit_ps5.a -:BUILD -del %TARGETLIB% -@if "%1" neq "noamalg" goto :AMALG -for %%f in (lj_*.c lib_*.c) do ( - %LJCOMPILE% %%f - @if errorlevel 1 goto :BAD -) - -%LJLIB% %TARGETLIB% lj_*.o lib_*.o -@if errorlevel 1 goto :BAD -@goto :NOAMALG -:AMALG -%LJCOMPILE% ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB% %TARGETLIB% ljamalg.o lj_vm.o -@if errorlevel 1 goto :BAD -:NOAMALG - -@del *.o *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for PS5 === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Visual Studio .NET Command Prompt" -@echo (64 bit host compiler). The PS5 Prospero SDK must be installed, too. -:END diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/psvitabuild.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/psvitabuild.bat deleted file mode 100644 index 29220b2..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/psvitabuild.bat +++ /dev/null @@ -1,93 +0,0 @@ -@rem Script to build LuaJIT with the PS Vita SDK. -@rem Donated to the public domain. -@rem -@rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler) -@rem Then cd to this directory and run this script. - -@if not defined INCLUDE goto :FAIL -@if not defined SCE_PSP2_SDK_DIR goto :FAIL - -@setlocal -@rem ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Check for 32 bit host compiler. -@minilua -@if errorlevel 8 goto :FAIL - -@set DASMFLAGS=-D FPU -D HFABI -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_arm.dasc -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% -DLUAJIT_TARGET=LUAJIT_ARCH_ARM -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLJ_TARGET_PSVITA=1 host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m elfasm -o lj_vm.s -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@rem ---- Cross compiler ---- -@set LJCOMPILE="%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2snc" -c -w -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -@set LJLIB="%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2ld32" -r --output= -@set INCLUDE="" - -"%SCE_PSP2_SDK_DIR%\host_tools\build\bin\psp2as" -o lj_vm.o lj_vm.s - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set LJCOMPILE=%LJCOMPILE% -g -O0 -@set TARGETLIB=libluajitD.a -goto :BUILD -:NODEBUG -@set LJCOMPILE=%LJCOMPILE% -O2 -@set TARGETLIB=libluajit.a -:BUILD -del %TARGETLIB% - -%LJCOMPILE% ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB%%TARGETLIB% ljamalg.o lj_vm.o -@if errorlevel 1 goto :BAD - -@del *.o *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for PS Vita === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Visual Studio .NET Command Prompt" -@echo (32 bit host compiler). The PS Vita SDK must be installed, too. -:END diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm.dasc deleted file mode 100644 index 770c160..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm.dasc +++ /dev/null @@ -1,4663 +0,0 @@ -|// Low-level VM code for ARM CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -| -|.arch arm -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -| -|// The following must be C callee-save. -|.define MASKR8, r4 // 255*8 constant for fast bytecode decoding. -|.define KBASE, r5 // Constants of current Lua function. -|.define PC, r6 // Next PC. -|.define DISPATCH, r7 // Opcode dispatch table. -|.define LREG, r8 // Register holding lua_State (also in SAVE_L). -| -|// C callee-save in EABI, but often refetched. Temporary in iOS 3.0+. -|.define BASE, r9 // Base of current Lua stack frame. -| -|// The following temporaries are not saved across C calls, except for RA/RC. -|.define RA, r10 // Callee-save. -|.define RC, r11 // Callee-save. -|.define RB, r12 -|.define OP, r12 // Overlaps RB, must not be lr. -|.define INS, lr -| -|// Calling conventions. Also used as temporaries. -|.define CARG1, r0 -|.define CARG2, r1 -|.define CARG3, r2 -|.define CARG4, r3 -|.define CARG12, r0 // For 1st soft-fp double. -|.define CARG34, r2 // For 2nd soft-fp double. -| -|.define CRET1, r0 -|.define CRET2, r1 -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.define SAVE_R4, [sp, #28] -|.define CFRAME_SPACE, #28 -|.define SAVE_ERRF, [sp, #24] -|.define SAVE_NRES, [sp, #20] -|.define SAVE_CFRAME, [sp, #16] -|.define SAVE_L, [sp, #12] -|.define SAVE_PC, [sp, #8] -|.define SAVE_MULTRES, [sp, #4] -|.define ARG5, [sp] -| -|.define TMPDhi, [sp, #4] -|.define TMPDlo, [sp] -|.define TMPD, [sp] -|.define TMPDp, sp -| -|.if FPU -|.macro saveregs -| push {r5, r6, r7, r8, r9, r10, r11, lr} -| vpush {d8-d15} -| sub sp, sp, CFRAME_SPACE+4 -| str r4, SAVE_R4 -|.endmacro -|.macro restoreregs_ret -| ldr r4, SAVE_R4 -| add sp, sp, CFRAME_SPACE+4 -| vpop {d8-d15} -| pop {r5, r6, r7, r8, r9, r10, r11, pc} -|.endmacro -|.else -|.macro saveregs -| push {r4, r5, r6, r7, r8, r9, r10, r11, lr} -| sub sp, sp, CFRAME_SPACE -|.endmacro -|.macro restoreregs_ret -| add sp, sp, CFRAME_SPACE -| pop {r4, r5, r6, r7, r8, r9, r10, r11, pc} -|.endmacro -|.endif -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; ud; .endmacro -| -|//----------------------------------------------------------------------- -| -|// Access to frame relative to BASE. -|.define FRAME_FUNC, #-8 -|.define FRAME_PC, #-4 -| -|.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro -|.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro -|.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro -|.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro -|.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| ldrb OP, [PC] -|.endmacro -|.macro ins_NEXT2 -| ldr INS, [PC], #4 -|.endmacro -|// Instruction decode+dispatch. -|.macro ins_NEXT3 -| ldr OP, [DISPATCH, OP, lsl #2] -| decode_RA8 RA, INS -| decode_RD RC, INS -| bx OP -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -| ins_NEXT3 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -| .define ins_next3, ins_NEXT3 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| .endmacro -| .macro ins_next3 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Avoid register name substitution for field name. -#define field_pc pc -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| ldr PC, LFUNC:CARG3->field_pc -| ldrb OP, [PC] // STALL: load PC. early PC. -| ldr INS, [PC], #4 -| ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP. -| decode_RA8 RA, INS -| add RA, RA, BASE -| bx OP -|.endmacro -| -|.macro ins_call -| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| str PC, [BASE, FRAME_PC] -| ins_callt // STALL: locked PC. -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checktp, reg, tp; cmn reg, #-tp; .endmacro -|.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro -|.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro -|.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro -|.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro -|.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro hotcheck, delta -| lsr CARG1, PC, #1 -| and CARG1, CARG1, #126 -| sub CARG1, CARG1, #-GG_DISP2HOT -| ldrh CARG2, [DISPATCH, CARG1] -| subs CARG2, CARG2, #delta -| strh CARG2, [DISPATCH, CARG1] -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP -| blo ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL -| blo ->vm_hotcall -|.endmacro -| -|// Set current VM state. -|.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro -|.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp -| ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)] -| bic mark, mark, #LJ_GC_BLACK // black2gray(tab) -| str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)] -| strb mark, tab->marked -| str tmp, tab->gclist -|.endmacro -| -|.macro .IOS, a, b -|.if IOS -| a, b -|.endif -|.endmacro -| -|//----------------------------------------------------------------------- - -#if !LJ_DUALNUM -#error "Only dual-number mode supported for ARM target" -#endif - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: RB = previous base. - | tst PC, #FRAME_P - | beq ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame. - | mvn CARG2, #~LJ_TTRUE - | mov BASE, RB - | // Prepending may overwrite the pcall frame, so do it at the end. - | str CARG2, [RA, FRAME_PC] // Prepend true to results. - | sub RA, RA, #8 - | - |->vm_returnc: - | adds RC, RC, #8 // RC = (nresults+1)*8. - | mov CRET1, #LUA_YIELD - | beq ->vm_unwind_c_eh - | str RC, SAVE_MULTRES - | ands CARG1, PC, #FRAME_TYPE - | beq ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return - | // CARG1 = PC & FRAME_TYPE - | bic RB, PC, #FRAME_TYPEP - | cmp CARG1, #FRAME_C - | sub RB, BASE, RB // RB = previous base. - | bne ->vm_returnp - | - | str RB, L->base - | ldr KBASE, SAVE_NRES - | mv_vmstate CARG4, C - | sub BASE, BASE, #8 - | subs CARG3, RC, #8 - | lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8 - | st_vmstate CARG4 - | beq >2 - |1: - | subs CARG3, CARG3, #8 - | ldrd CARG12, [RA], #8 - | strd CARG12, [BASE], #8 - | bne <1 - |2: - | cmp KBASE, RC // More/less results wanted? - | bne >6 - |3: - | str BASE, L->top // Store new top. - | - |->vm_leave_cp: - | ldr RC, SAVE_CFRAME // Restore previous C frame. - | mov CRET1, #0 // Ok return status for vm_pcall. - | str RC, L->cframe - | - |->vm_leave_unw: - | restoreregs_ret - | - |6: - | blt >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | ldr CARG3, L->maxstack - | mvn CARG2, #~LJ_TNIL - | cmp BASE, CARG3 - | bhs >8 - | str CARG2, [BASE, #4] - | add RC, RC, #8 - | add BASE, BASE, #8 - | b <2 - | - |7: // Less results wanted. - | sub CARG1, RC, KBASE - | cmp KBASE, #0 // LUA_MULTRET+1 case? - | subne BASE, BASE, CARG1 // Either keep top or shrink it. - | b <3 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | str BASE, L->top // Save current top held in BASE (yes). - | lsr CARG2, KBASE, #3 - | mov CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->top // Need the (realloced) L->top in BASE. - | b <2 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mov sp, CARG1 - | mov CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | ldr L, SAVE_L - | mv_vmstate CARG4, C - | ldr GL:CARG3, L->glref - | str CARG4, GL:CARG3->vmstate - | b ->vm_leave_unw - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated. - | mov sp, CARG1 - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | ldr L, SAVE_L - | mov MASKR8, #255 - | mov RC, #16 // 2 results: false + error message. - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | ldr BASE, L->base - | ldr DISPATCH, L->glref // Setup pointer to dispatch table. - | mvn CARG1, #~LJ_TFALSE - | sub RA, BASE, #8 // Results start at BASE-8. - | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame. - | add DISPATCH, DISPATCH, #GG_G2DISP - | mv_vmstate CARG2, INTERP - | str CARG1, [BASE, #-4] // Prepend false to error message. - | st_vmstate CARG2 - | b ->vm_returnc - | - |->vm_unwind_ext: // Complete external unwind. -#if !LJ_NO_UNWIND - | push {r0, r1, r2, lr} - | bl extern _Unwind_Complete - | ldr r0, [sp] - | bl extern _Unwind_DeleteException - | pop {r0, r1, r2, lr} - | mov r0, r1 - | bx r2 -#endif - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | // CARG1 = L - | mov CARG2, #LUA_MINSTACK - | b >2 - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | add RC, BASE, RC - | sub RA, RA, BASE - | mov CARG1, L - | str BASE, L->base - | add PC, PC, #4 // Must point after first instruction. - | str RC, L->top - | lsr CARG2, RA, #3 - |2: - | // L->base = new base, L->top = top - | str PC, SAVE_PC - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | ldr RC, L->top - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | sub NARGS8:RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mov L, CARG1 - | ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table. - | mov BASE, CARG2 - | add DISPATCH, DISPATCH, #GG_G2DISP - | str L, SAVE_L - | mov PC, #FRAME_CP - | str CARG3, SAVE_NRES - | add CARG2, sp, #CFRAME_RESUME - | ldrb CARG1, L->status - | str CARG3, SAVE_ERRF - | str L, SAVE_PC // Any value outside of bytecode is ok. - | str CARG3, SAVE_CFRAME - | cmp CARG1, #0 - | str CARG2, L->cframe - | beq >3 - | - | // Resume after yield (like a return). - | str L, [DISPATCH, #DISPATCH_GL(cur_L)] - | mov RA, BASE - | ldr BASE, L->base - | ldr CARG1, L->top - | mov MASKR8, #255 - | strb CARG3, L->status - | sub RC, CARG1, BASE - | ldr PC, [BASE, FRAME_PC] - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | mv_vmstate CARG2, INTERP - | add RC, RC, #8 - | ands CARG1, PC, #FRAME_TYPE - | st_vmstate CARG2 - | str RC, SAVE_MULTRES - | beq ->BC_RET_Z - | b ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | mov PC, #FRAME_CP - | str CARG4, SAVE_ERRF - | b >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | mov PC, #FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | ldr RC, L:CARG1->cframe - | str CARG3, SAVE_NRES - | mov L, CARG1 - | str CARG1, SAVE_L - | ldr DISPATCH, L->glref // Setup pointer to dispatch table. - | mov BASE, CARG2 - | str CARG1, SAVE_PC // Any value outside of bytecode is ok. - | str RC, SAVE_CFRAME - | add DISPATCH, DISPATCH, #GG_G2DISP - | str sp, L->cframe // Add our C frame to cframe chain. - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | str L, [DISPATCH, #DISPATCH_GL(cur_L)] - | ldr RB, L->base // RB = old base (for vmeta_call). - | ldr CARG1, L->top - | mov MASKR8, #255 - | add PC, PC, BASE - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | sub PC, PC, RB // PC = frame delta + frame type - | mv_vmstate CARG2, INTERP - | sub NARGS8:RC, CARG1, BASE - | st_vmstate CARG2 - | - |->vm_call_dispatch: - | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC - | ldrd CARG34, [BASE, FRAME_FUNC] - | checkfunc CARG4, ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mov L, CARG1 - | ldr RA, L:CARG1->stack - | str CARG1, SAVE_L - | ldr DISPATCH, L->glref // Setup pointer to dispatch table. - | ldr RB, L->top - | str CARG1, SAVE_PC // Any value outside of bytecode is ok. - | ldr RC, L->cframe - | add DISPATCH, DISPATCH, #GG_G2DISP - | sub RA, RA, RB // Compute -savestack(L, L->top). - | mov RB, #0 - | str RA, SAVE_NRES // Neg. delta means cframe w/o frame. - | str RB, SAVE_ERRF // No error function. - | str RC, SAVE_CFRAME - | str sp, L->cframe // Add our C frame to cframe chain. - | str L, [DISPATCH, #DISPATCH_GL(cur_L)] - | blx CARG4 // (lua_State *L, lua_CFunction func, void *ud) - | movs BASE, CRET1 - | mov PC, #FRAME_CP - | bne <3 // Else continue with the call. - | b ->vm_leave_cp // No base? Just remove C frame. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8 - | ldr LFUNC:CARG3, [RB, FRAME_FUNC] - | ldr CARG1, [BASE, #-16] // Get continuation. - | mov CARG4, BASE - | mov BASE, RB // Restore caller BASE. - |.if FFI - | cmp CARG1, #1 - |.endif - | ldr PC, [CARG4, #-12] // Restore PC from [cont|PC]. - | mvn INS, #~LJ_TNIL - | add CARG2, RA, RC - | str INS, [CARG2, #-4] // Ensure one valid arg. - |.if FFI - | bls >1 - |.endif - | ldr CARG3, LFUNC:CARG3->field_pc - | ldr KBASE, [CARG3, #PC2PROTO(k)] - | // BASE = base, RA = resultptr, CARG4 = meta base - | bx CARG1 - | - |.if FFI - |1: - | beq ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - | sub CARG4, CARG4, #16 - | sub RC, CARG4, BASE - | b ->vm_call_tail - |.endif - | - |->cont_cat: // RA = resultptr, CARG4 = meta base - | ldr INS, [PC, #-4] - | sub CARG2, CARG4, #16 - | ldrd CARG34, [RA] - | str BASE, L->base - | decode_RB8 RC, INS - | decode_RA8 RA, INS - | add CARG1, BASE, RC - | subs CARG1, CARG2, CARG1 - | strdne CARG34, [CARG2] - | movne CARG3, CARG1 - | bne ->BC_CAT_Z - | strd CARG34, [BASE, RA] - | b ->cont_nop - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | add CARG2, BASE, RB - | b >2 - | - |->vmeta_tgets: - | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv) - | mvn CARG4, #~LJ_TTAB - | str TAB:RB, [CARG2] - | str CARG4, [CARG2, #4] - |2: - | mvn CARG4, #~LJ_TSTR - | str STR:RC, TMPDlo - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tgetb: // RC = index - | decode_RB8 RB, INS - | str RC, TMPDlo - | mvn CARG4, #~LJ_TISNUM - | add CARG2, BASE, RB - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tgetv: - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | beq >3 - | ldrd CARG34, [CRET1] - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | rsb CARG1, BASE, #FRAME_CONT - | ldr BASE, L->top - | mov NARGS8:RC, #16 // 2 args for func(t, k). - | str PC, [BASE, #-12] // [cont|PC] - | add PC, CARG1, BASE - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | b ->vm_call_dispatch_f - | - |->vmeta_tgetr: - | .IOS mov RC, BASE - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | .IOS mov BASE, RC - | cmp CRET1, #0 - | ldrdne CARG12, [CRET1] - | mvneq CARG2, #~LJ_TNIL - | b ->BC_TGETR_Z - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | add CARG2, BASE, RB - | b >2 - | - |->vmeta_tsets: - | sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv) - | mvn CARG4, #~LJ_TTAB - | str TAB:RB, [CARG2] - | str CARG4, [CARG2, #4] - |2: - | mvn CARG4, #~LJ_TSTR - | str STR:RC, TMPDlo - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tsetb: // RC = index - | decode_RB8 RB, INS - | str RC, TMPDlo - | mvn CARG4, #~LJ_TISNUM - | add CARG2, BASE, RB - | str CARG4, TMPDhi - | mov CARG3, TMPDp - | b >1 - | - |->vmeta_tsetv: - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | ldrd CARG34, [BASE, RA] - | beq >3 - | ins_next1 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | strd CARG34, [CRET1] - | ins_next2 - | ins_next3 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | rsb CARG1, BASE, #FRAME_CONT - | ldr BASE, L->top - | mov NARGS8:RC, #24 // 3 args for func(t, k, v). - | strd CARG34, [BASE, #16] // Copy value to third argument. - | str PC, [BASE, #-12] // [cont|PC] - | add PC, CARG1, BASE - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | b ->vm_call_dispatch_f - | - |->vmeta_tsetr: - | str BASE, L->base - | .IOS mov RC, BASE - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - | // Returns TValue *. - | .IOS mov BASE, RC - | b ->BC_TSETR_Z - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | mov CARG1, L - | sub PC, PC, #4 - | mov CARG2, RA - | str BASE, L->base - | mov CARG3, RC - | str PC, SAVE_PC - | decode_OP CARG4, INS - | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // Returns 0/1 or TValue * (metamethod). - |3: - | .IOS ldr BASE, L->base - | cmp CRET1, #1 - | bhi ->vmeta_binop - |4: - | ldrh RB, [PC, #2] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | subhs PC, RB, #0x20000 - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | ldr INS, [PC, #-4] - | ldrd CARG12, [RA] - | decode_RA8 CARG3, INS - | strd CARG12, [BASE, CARG3] - | b ->cont_nop - | - |->cont_condt: // RA = resultptr - | ldr CARG2, [RA, #4] - | mvn CARG1, #~LJ_TTRUE - | cmp CARG1, CARG2 // Branch if result is true. - | b <4 - | - |->cont_condf: // RA = resultptr - | ldr CARG2, [RA, #4] - | checktp CARG2, LJ_TFALSE // Branch if result is false. - | b <4 - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - | - |->vmeta_equal_cd: - |.if FFI - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | mov CARG2, INS - | str PC, SAVE_PC - | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |.endif - | - |->vmeta_istype: - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | lsr CARG2, RA, #3 - | mov CARG3, RC - | str PC, SAVE_PC - | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - | .IOS ldr BASE, L->base - | b ->cont_nop - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vn: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG3, BASE, RB - | add CARG4, KBASE, RC - | b >1 - | - |->vmeta_arith_nv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG4, BASE, RB - | add CARG3, KBASE, RC - | b >1 - | - |->vmeta_unm: - | ldr INS, [PC, #-8] - | sub PC, PC, #4 - | add CARG3, BASE, RC - | add CARG4, BASE, RC - | b >1 - | - |->vmeta_arith_vv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG3, BASE, RB - | add CARG4, BASE, RC - |1: - | decode_OP OP, INS - | add CARG2, BASE, RA - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | str OP, ARG5 - | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // Returns NULL (finished) or TValue * (metamethod). - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | beq ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | sub CARG2, CRET1, BASE - | str PC, [CRET1, #-12] // [cont|PC] - | add PC, CARG2, #FRAME_CONT - | mov BASE, CRET1 - | mov NARGS8:RC, #16 // 2 args for func(o1, o2). - | b ->vm_call_dispatch - | - |->vmeta_len: - | add CARG2, BASE, RC - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_len // (lua_State *L, TValue *o) - | // Returns NULL (retry) or TValue * (metamethod base). - | .IOS ldr BASE, L->base -#if LJ_52 - | cmp CRET1, #0 - | bne ->vmeta_binop // Binop call for compatibility. - | ldr TAB:CARG1, [BASE, RC] - | b ->BC_LEN_Z -#else - | b ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // RB = old base, BASE = new base, RC = nargs*8 - | mov CARG1, L - | str RB, L->base // This is the callers base! - | sub CARG2, BASE, #8 - | str PC, SAVE_PC - | add CARG3, BASE, NARGS8:RC - | .IOS mov RA, BASE - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | .IOS mov BASE, RA - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | mov CARG1, L - | str BASE, L->base - | sub CARG2, RA, #8 - | str PC, SAVE_PC - | add CARG3, RA, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | .IOS ldr BASE, L->base - | ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here. - | ldr PC, [BASE, FRAME_PC] - | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. - | b ->BC_CALLT2_Z - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mov CARG1, L - | str BASE, L->base - | mov CARG2, RA - | str PC, SAVE_PC - | bl extern lj_meta_for // (lua_State *L, TValue *base) - | .IOS ldr BASE, L->base - |.if JIT - | ldrb OP, [PC, #-4] - |.endif - | ldr INS, [PC, #-4] - |.if JIT - | cmp OP, #BC_JFORI - |.endif - | decode_RA8 RA, INS - | decode_RD RC, INS - |.if JIT - | beq =>BC_JFORI - |.endif - | b =>BC_FORI - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | ldrd CARG12, [BASE] - | ldrd CARG34, [BASE, #8] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - | .ffunc_1 name - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name - | .ffunc_2 name - | checktp CARG2, LJ_TISNUM - | cmnlo CARG4, #-LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |.macro .ffunc_d, name - | .ffunc name - | ldr CARG2, [BASE, #4] - | cmp NARGS8:RC, #8 - | vldr d0, [BASE] - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |.macro .ffunc_dd, name - | .ffunc name - | ldr CARG2, [BASE, #4] - | ldr CARG4, [BASE, #12] - | cmp NARGS8:RC, #16 - | vldr d0, [BASE] - | vldr d1, [BASE, #8] - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | cmnlo CARG4, #-LJ_TISNUM - | bhs ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2. - |.macro ffgccheck - | ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)] - | cmp CARG1, CARG2 - | blge ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | checktp CARG2, LJ_TTRUE - | bhi ->fff_fallback - | ldr PC, [BASE, FRAME_PC] - | strd CARG12, [BASE, #-8] - | mov RB, BASE - | subs RA, NARGS8:RC, #8 - | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8. - | beq ->fff_res // Done if exactly 1 argument. - |1: - | ldrd CARG12, [RB, #8] - | subs RA, RA, #8 - | strd CARG12, [RB], #8 - | bne <1 - | b ->fff_res - | - |.ffunc type - | ldr CARG2, [BASE, #4] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | mvnlo CARG2, #~LJ_TISNUM - | rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1 - | lsl CARG4, CARG4, #3 - | ldrd CARG12, [CFUNC:CARG3, CARG4] - | b ->fff_restv - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | checktp CARG2, LJ_TTAB - | cmnne CARG2, #-LJ_TUDATA - | bne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | ldr TAB:RB, TAB:CARG1->metatable - |2: - | mvn CARG2, #~LJ_TNIL - | ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])] - | cmp TAB:RB, #0 - | beq ->fff_restv - | ldr CARG3, TAB:RB->hmask - | ldr CARG4, STR:RC->sid - | ldr NODE:INS, TAB:RB->node - | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask - | add CARG3, CARG3, CARG3, lsl #1 - | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 - |3: // Rearranged logic, because we expect _not_ to find the key. - | ldrd CARG34, NODE:INS->key // STALL: early NODE:INS. - | ldrd CARG12, NODE:INS->val - | ldr NODE:INS, NODE:INS->next - | checktp CARG4, LJ_TSTR - | cmpeq CARG3, STR:RC - | beq >5 - | cmp NODE:INS, #0 - | bne <3 - |4: - | mov CARG1, RB // Use metatable as default result. - | mvn CARG2, #~LJ_TTAB - | b ->fff_restv - |5: - | checktp CARG2, LJ_TNIL - | bne ->fff_restv - | b <4 - | - |6: - | checktp CARG2, LJ_TISNUM - | mvnhs CARG2, CARG2 - | movlo CARG2, #~LJ_TISNUM - | add CARG4, DISPATCH, CARG2, lsl #2 - | ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])] - | b <2 - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | checktp CARG2, LJ_TTAB - | ldreq TAB:RB, TAB:CARG1->metatable - | checktpeq CARG4, LJ_TTAB - | ldrbeq CARG4, TAB:CARG1->marked - | cmpeq TAB:RB, #0 - | bne ->fff_fallback - | tst CARG4, #LJ_GC_BLACK // isblack(table) - | str TAB:CARG3, TAB:CARG1->metatable - | beq ->fff_restv - | barrierback TAB:CARG1, CARG4, CARG3 - | b ->fff_restv - | - |.ffunc rawget - | ldrd CARG34, [BASE] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | mov CARG2, CARG3 - | checktab CARG4, ->fff_fallback - | mov CARG1, L - | add CARG3, BASE, #8 - | .IOS mov RA, BASE - | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // Returns cTValue *. - | .IOS mov BASE, RA - | ldrd CARG12, [CRET1] - | b ->fff_restv - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 - | bne ->fff_fallback - | checktp CARG2, LJ_TISNUM - | bls ->fff_restv - | b ->fff_fallback - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | checktp CARG2, LJ_TSTR - | // A __tostring method in the string base metatable is ignored. - | beq ->fff_restv - | // Handle numbers inline, unless a number base metatable is present. - | ldr CARG4, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])] - | str BASE, L->base - | checktp CARG2, LJ_TISNUM - | cmpls CARG4, #0 - | str PC, SAVE_PC // Redundant (but a defined value). - | bhi ->fff_fallback - | ffgccheck - | mov CARG1, L - | mov CARG2, BASE - | bl extern lj_strfmt_number // (lua_State *L, cTValue *o) - | // Returns GCstr *. - | ldr BASE, L->base - | mvn CARG2, #~LJ_TSTR - | b ->fff_restv - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | mvn CARG4, #~LJ_TNIL - | checktab CARG2, ->fff_fallback - | strd CARG34, [BASE, NARGS8:RC] // Set missing 2nd arg to nil. - | ldr PC, [BASE, FRAME_PC] - | add CARG2, BASE, #8 - | sub CARG3, BASE, #8 - | bl extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - | // Returns 1=found, 0=end, -1=error. - | .IOS ldr BASE, L->base - | cmp CRET1, #0 - | mov RC, #(2+1)*8 - | bgt ->fff_res // Found key/value. - | bmi ->fff_fallback // Invalid key. - | // End of traversal: return nil. - | mvn CRET2, #~LJ_TNIL - | b ->fff_restv - | - |.ffunc_1 pairs - | checktab CARG2, ->fff_fallback -#if LJ_52 - | ldr TAB:RB, TAB:CARG1->metatable -#endif - | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0] - | ldr PC, [BASE, FRAME_PC] -#if LJ_52 - | cmp TAB:RB, #0 - | bne ->fff_fallback -#endif - | mvn CARG2, #~LJ_TNIL - | mov RC, #(3+1)*8 - | strd CFUNC:CARG34, [BASE, #-8] - | str CARG2, [BASE, #12] - | b ->fff_res - | - |.ffunc_2 ipairs_aux - | checktp CARG2, LJ_TTAB - | checktpeq CARG4, LJ_TISNUM - | bne ->fff_fallback - | ldr RB, TAB:CARG1->asize - | ldr RC, TAB:CARG1->array - | add CARG3, CARG3, #1 - | ldr PC, [BASE, FRAME_PC] - | cmp CARG3, RB - | add RC, RC, CARG3, lsl #3 - | strd CARG34, [BASE, #-8] - | ldrdlo CARG12, [RC] - | mov RC, #(0+1)*8 - | bhs >2 // Not in array part? - |1: - | checktp CARG2, LJ_TNIL - | movne RC, #(2+1)*8 - | strdne CARG12, [BASE] - | b ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | ldr RB, TAB:CARG1->hmask - | mov CARG2, CARG3 - | cmp RB, #0 - | beq ->fff_res - | .IOS mov RA, BASE - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | .IOS mov BASE, RA - | cmp CRET1, #0 - | beq ->fff_res - | ldrd CARG12, [CRET1] - | b <1 - | - |.ffunc_1 ipairs - | checktab CARG2, ->fff_fallback -#if LJ_52 - | ldr TAB:RB, TAB:CARG1->metatable -#endif - | ldrd CFUNC:CARG34, CFUNC:CARG3->upvalue[0] - | ldr PC, [BASE, FRAME_PC] -#if LJ_52 - | cmp TAB:RB, #0 - | bne ->fff_fallback -#endif - | mov CARG1, #0 - | mvn CARG2, #~LJ_TISNUM - | mov RC, #(3+1)*8 - | strd CFUNC:CARG34, [BASE, #-8] - | strd CARG12, [BASE, #8] - | b ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - | tst RA, #HOOK_ACTIVE // Remember active hook before pcall. - | mov RB, BASE - | add BASE, BASE, #8 - | moveq PC, #8+FRAME_PCALL - | movne PC, #8+FRAME_PCALLH - | sub NARGS8:RC, NARGS8:RC, #8 - | b ->vm_call_dispatch - | - |.ffunc_2 xpcall - | ldrb RA, [DISPATCH, #DISPATCH_GL(hookmask)] - | checkfunc CARG4, ->fff_fallback // Traceback must be a function. - | mov RB, BASE - | strd CARG12, [BASE, #8] // Swap function and traceback. - | strd CARG34, [BASE] - | tst RA, #HOOK_ACTIVE // Remember active hook before pcall. - | add BASE, BASE, #16 - | moveq PC, #16+FRAME_PCALL - | movne PC, #16+FRAME_PCALLH - | sub NARGS8:RC, NARGS8:RC, #16 - | b ->vm_call_dispatch - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | checktp CARG2, LJ_TTHREAD - | bne ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr - |.endif - | ldr PC, [BASE, FRAME_PC] - | str BASE, L->base - | ldr CARG2, L:CARG1->top - | ldrb RA, L:CARG1->status - | ldr RB, L:CARG1->base - | add CARG3, CARG2, NARGS8:RC - | add CARG4, CARG2, RA - | str PC, SAVE_PC - | cmp CARG4, RB - | beq ->fff_fallback - | ldr CARG4, L:CARG1->maxstack - | ldr RB, L:CARG1->cframe - | cmp RA, #LUA_YIELD - | cmpls CARG3, CARG4 - | cmpls RB, #0 - | bhi ->fff_fallback - |1: - |.if resume - | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC. - | add BASE, BASE, #8 - | sub NARGS8:RC, NARGS8:RC, #8 - |.endif - | str CARG3, L:CARG1->top - | str BASE, L->top - |2: // Move args to coroutine. - | ldrd CARG34, [BASE, RB] - | cmp RB, NARGS8:RC - | strdne CARG34, [CARG2, RB] - | add RB, RB, #8 - | bne <2 - | - | mov CARG3, #0 - | mov L:RA, L:CARG1 - | mov CARG4, #0 - | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | // Returns thread status. - |4: - | ldr CARG3, L:RA->base - | mv_vmstate CARG2, INTERP - | ldr CARG4, L:RA->top - | cmp CRET1, #LUA_YIELD - | ldr BASE, L->base - | str L, [DISPATCH, #DISPATCH_GL(cur_L)] - | st_vmstate CARG2 - | bhi >8 - | subs RC, CARG4, CARG3 - | ldr CARG1, L->maxstack - | add CARG2, BASE, RC - | beq >6 // No results? - | cmp CARG2, CARG1 - | mov RB, #0 - | bhi >9 // Need to grow stack? - | - | sub CARG4, RC, #8 - | str CARG3, L:RA->top // Clear coroutine stack. - |5: // Move results from coroutine. - | ldrd CARG12, [CARG3, RB] - | cmp RB, CARG4 - | strd CARG12, [BASE, RB] - | add RB, RB, #8 - | bne <5 - |6: - |.if resume - | mvn CARG3, #~LJ_TTRUE - | add RC, RC, #16 - |7: - | str CARG3, [BASE, #-4] // Prepend true/false to results. - | sub RA, BASE, #8 - |.else - | mov RA, BASE - | add RC, RC, #8 - |.endif - | ands CARG1, PC, #FRAME_TYPE - | str PC, SAVE_PC - | str RC, SAVE_MULTRES - | beq ->BC_RET_Z - | b ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | ldrd CARG12, [CARG4, #-8]! - | mvn CARG3, #~LJ_TFALSE - | mov RC, #(2+1)*8 - | str CARG4, L:RA->top // Remove error from coroutine stack. - | strd CARG12, [BASE] // Copy error message. - | b <7 - |.else - | mov CARG1, L - | mov CARG2, L:RA - | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - | // Never returns. - |.endif - | - |9: // Handle stack expansion on return from yield. - | mov CARG1, L - | lsr CARG2, RC, #3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | mov CRET1, #0 - | b <4 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | ldr CARG1, L->cframe - | add CARG2, BASE, NARGS8:RC - | str BASE, L->base - | tst CARG1, #CFRAME_RESUME - | str CARG2, L->top - | mov CRET1, #LUA_YIELD - | mov CARG3, #0 - | beq ->fff_fallback - | str CARG3, L->cframe - | strb CRET1, L->status - | b ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.macro math_round, func - | .ffunc_1 math_ .. func - | checktp CARG2, LJ_TISNUM - | beq ->fff_restv - | bhi ->fff_fallback - | // Round FP value and normalize result. - | lsl CARG3, CARG2, #1 - | adds RB, CARG3, #0x00200000 - | bpl >2 // |x| < 1? - | mvn CARG4, #0x3e0 - | subs RB, CARG4, RB, asr #21 - | lsl CARG4, CARG2, #11 - | lsl CARG3, CARG1, #11 - | orr CARG4, CARG4, #0x80000000 - | rsb INS, RB, #32 - | orr CARG4, CARG4, CARG1, lsr #21 - | bls >3 // |x| >= 2^31? - | orr CARG3, CARG3, CARG4, lsl INS - | lsr CARG1, CARG4, RB - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 - | addne CARG1, CARG1, #1 - |.else - | bics CARG3, CARG3, CARG2, asr #31 - | addsne CARG1, CARG1, #1 - | ldrdvs CARG12, >9 - | bvs ->fff_restv - |.endif - | cmp CARG2, #0 - | rsblt CARG1, CARG1, #0 - |1: - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |2: // |x| < 1 - | bcs ->fff_restv // |x| is not finite. - | orr CARG3, CARG3, CARG1 // ztest = abs(hi) | lo - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 // return (ztest & sign) == 0 ? 0 : -1 - | moveq CARG1, #0 - | mvnne CARG1, #0 - |.else - | bics CARG3, CARG3, CARG2, asr #31 // return (ztest & ~sign) == 0 ? 0 : 1 - | moveq CARG1, #0 - | movne CARG1, #1 - |.endif - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |3: // |x| >= 2^31. Check for x == -(2^31). - | cmpeq CARG4, #0x80000000 - |.if "func" == "floor" - | cmpeq CARG3, #0 - |.endif - | bne >4 - | cmp CARG2, #0 - | movmi CARG1, #0x80000000 - | bmi <1 - |4: - | bl ->vm_..func.._sf - | b ->fff_restv - |.endmacro - | - | math_round floor - | math_round ceil - | - |.align 8 - |9: - | .long 0x00000000, 0x41e00000 // 2^31. - | - |.ffunc_1 math_abs - | checktp CARG2, LJ_TISNUM - | bhi ->fff_fallback - | bicne CARG2, CARG2, #0x80000000 - | bne ->fff_restv - | cmp CARG1, #0 - | rsbslt CARG1, CARG1, #0 - | ldrdvs CARG12, <9 - | // Fallthrough. - | - |->fff_restv: - | // CARG12 = TValue result. - | ldr PC, [BASE, FRAME_PC] - | strd CARG12, [BASE, #-8] - |->fff_res1: - | // PC = return. - | mov RC, #(1+1)*8 - |->fff_res: - | // RC = (nresults+1)*8, PC = return. - | ands CARG1, PC, #FRAME_TYPE - | ldreq INS, [PC, #-4] - | str RC, SAVE_MULTRES - | sub RA, BASE, #8 - | bne ->vm_return - | decode_RB8 RB, INS - |5: - | cmp RB, RC // More results expected? - | bhi >6 - | decode_RA8 CARG1, INS - | ins_next1 - | ins_next2 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | sub BASE, RA, CARG1 - | ins_next3 - | - |6: // Fill up results with nil. - | add CARG2, RA, RC - | mvn CARG1, #~LJ_TNIL - | add RC, RC, #8 - | str CARG1, [CARG2, #-4] - | b <5 - | - |.macro math_extern, func - |.if HFABI - | .ffunc_d math_ .. func - |.else - | .ffunc_n math_ .. func - |.endif - | .IOS mov RA, BASE - | bl extern func - | .IOS mov BASE, RA - |.if HFABI - | b ->fff_resd - |.else - | b ->fff_restv - |.endif - |.endmacro - | - |.macro math_extern2, func - |.if HFABI - | .ffunc_dd math_ .. func - |.else - | .ffunc_nn math_ .. func - |.endif - | .IOS mov RA, BASE - | bl extern func - | .IOS mov BASE, RA - |.if HFABI - | b ->fff_resd - |.else - | b ->fff_restv - |.endif - |.endmacro - | - |.if FPU - | .ffunc_d math_sqrt - | vsqrt.f64 d0, d0 - |->fff_resd: - | ldr PC, [BASE, FRAME_PC] - | vstr d0, [BASE, #-8] - | b ->fff_res1 - |.else - | math_extern sqrt - |.endif - | - |.ffunc math_log - |.if HFABI - | ldr CARG2, [BASE, #4] - | cmp NARGS8:RC, #8 // Need exactly 1 argument. - | vldr d0, [BASE] - | bne ->fff_fallback - |.else - | ldrd CARG12, [BASE] - | cmp NARGS8:RC, #8 // Need exactly 1 argument. - | bne ->fff_fallback - |.endif - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - | .IOS mov RA, BASE - | bl extern log - | .IOS mov BASE, RA - |.if HFABI - | b ->fff_resd - |.else - | b ->fff_restv - |.endif - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.if HFABI - | .ffunc math_ldexp - | ldr CARG4, [BASE, #4] - | ldrd CARG12, [BASE, #8] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | vldr d0, [BASE] - | checktp CARG4, LJ_TISNUM - | bhs ->fff_fallback - | checktp CARG2, LJ_TISNUM - | bne ->fff_fallback - | .IOS mov RA, BASE - | bl extern ldexp // (double x, int exp) - | .IOS mov BASE, RA - | b ->fff_resd - |.else - |.ffunc_2 math_ldexp - | checktp CARG2, LJ_TISNUM - | bhs ->fff_fallback - | checktp CARG4, LJ_TISNUM - | bne ->fff_fallback - | .IOS mov RA, BASE - | bl extern ldexp // (double x, int exp) - | .IOS mov BASE, RA - | b ->fff_restv - |.endif - | - |.if HFABI - |.ffunc_d math_frexp - | mov CARG1, sp - | .IOS mov RA, BASE - | bl extern frexp - | .IOS mov BASE, RA - | ldr CARG3, [sp] - | mvn CARG4, #~LJ_TISNUM - | ldr PC, [BASE, FRAME_PC] - | vstr d0, [BASE, #-8] - | mov RC, #(2+1)*8 - | strd CARG34, [BASE] - | b ->fff_res - |.else - |.ffunc_n math_frexp - | mov CARG3, sp - | .IOS mov RA, BASE - | bl extern frexp - | .IOS mov BASE, RA - | ldr CARG3, [sp] - | mvn CARG4, #~LJ_TISNUM - | ldr PC, [BASE, FRAME_PC] - | strd CARG12, [BASE, #-8] - | mov RC, #(2+1)*8 - | strd CARG34, [BASE] - | b ->fff_res - |.endif - | - |.if HFABI - |.ffunc_d math_modf - | sub CARG1, BASE, #8 - | ldr PC, [BASE, FRAME_PC] - | .IOS mov RA, BASE - | bl extern modf - | .IOS mov BASE, RA - | mov RC, #(2+1)*8 - | vstr d0, [BASE] - | b ->fff_res - |.else - |.ffunc_n math_modf - | sub CARG3, BASE, #8 - | ldr PC, [BASE, FRAME_PC] - | .IOS mov RA, BASE - | bl extern modf - | .IOS mov BASE, RA - | mov RC, #(2+1)*8 - | strd CARG12, [BASE] - | b ->fff_res - |.endif - | - |.macro math_minmax, name, cond, fcond - |.if FPU - | .ffunc_1 name - | add RB, BASE, RC - | checktp CARG2, LJ_TISNUM - | add RA, BASE, #8 - | bne >4 - |1: // Handle integers. - | ldrd CARG34, [RA] - | cmp RA, RB - | bhs ->fff_restv - | checktp CARG4, LJ_TISNUM - | bne >3 - | cmp CARG1, CARG3 - | add RA, RA, #8 - | mov..cond CARG1, CARG3 - | b <1 - |3: // Convert intermediate result to number and continue below. - | vmov s4, CARG1 - | bhi ->fff_fallback - | vldr d1, [RA] - | vcvt.f64.s32 d0, s4 - | b >6 - | - |4: - | vldr d0, [BASE] - | bhi ->fff_fallback - |5: // Handle numbers. - | ldrd CARG34, [RA] - | vldr d1, [RA] - | cmp RA, RB - | bhs ->fff_resd - | checktp CARG4, LJ_TISNUM - | bhs >7 - |6: - | vcmp.f64 d0, d1 - | vmrs - | add RA, RA, #8 - | vmov..fcond.f64 d0, d1 - | b <5 - |7: // Convert integer to number and continue above. - | vmov s4, CARG3 - | bhi ->fff_fallback - | vcvt.f64.s32 d1, s4 - | b <6 - | - |.else - | - | .ffunc_1 name - | checktp CARG2, LJ_TISNUM - | mov RA, #8 - | bne >4 - |1: // Handle integers. - | ldrd CARG34, [BASE, RA] - | cmp RA, RC - | bhs ->fff_restv - | checktp CARG4, LJ_TISNUM - | bne >3 - | cmp CARG1, CARG3 - | add RA, RA, #8 - | mov..cond CARG1, CARG3 - | b <1 - |3: // Convert intermediate result to number and continue below. - | bhi ->fff_fallback - | bl extern __aeabi_i2d - | ldrd CARG34, [BASE, RA] - | b >6 - | - |4: - | bhi ->fff_fallback - |5: // Handle numbers. - | ldrd CARG34, [BASE, RA] - | cmp RA, RC - | bhs ->fff_restv - | checktp CARG4, LJ_TISNUM - | bhs >7 - |6: - | bl extern __aeabi_cdcmple - | add RA, RA, #8 - | mov..fcond CARG1, CARG3 - | mov..fcond CARG2, CARG4 - | b <5 - |7: // Convert integer to number and continue above. - | bhi ->fff_fallback - | strd CARG12, TMPD - | mov CARG1, CARG3 - | bl extern __aeabi_i2d - | ldrd CARG34, TMPD - | b <6 - |.endif - |.endmacro - | - | math_minmax math_min, gt, pl - | math_minmax math_max, lt, le - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | ldrd CARG12, [BASE] - | ldr PC, [BASE, FRAME_PC] - | cmp NARGS8:RC, #8 - | checktpeq CARG2, LJ_TSTR // Need exactly 1 argument. - | bne ->fff_fallback - | ldr CARG3, STR:CARG1->len - | ldrb CARG1, STR:CARG1[1] // Access is always ok (NUL at end). - | mvn CARG2, #~LJ_TISNUM - | cmp CARG3, #0 - | moveq RC, #(0+1)*8 - | movne RC, #(1+1)*8 - | strd CARG12, [BASE, #-8] - | b ->fff_res - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | ldrd CARG12, [BASE] - | ldr PC, [BASE, FRAME_PC] - | cmp NARGS8:RC, #8 // Need exactly 1 argument. - | checktpeq CARG2, LJ_TISNUM - | bicseq CARG4, CARG1, #255 - | mov CARG3, #1 - | bne ->fff_fallback - | str CARG1, TMPD - | mov CARG2, TMPDp // Points to stack. Little-endian. - |->fff_newstr: - | // CARG2 = str, CARG3 = len. - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_str_new // (lua_State *L, char *str, size_t l) - |->fff_resstr: - | // Returns GCstr *. - | ldr BASE, L->base - | mvn CARG2, #~LJ_TSTR - | b ->fff_restv - | - |.ffunc string_sub - | ffgccheck - | ldrd CARG12, [BASE] - | ldrd CARG34, [BASE, #16] - | cmp NARGS8:RC, #16 - | mvn RB, #0 - | beq >1 - | blo ->fff_fallback - | checktp CARG4, LJ_TISNUM - | mov RB, CARG3 - | bne ->fff_fallback - |1: - | ldrd CARG34, [BASE, #8] - | checktp CARG2, LJ_TSTR - | ldreq CARG2, STR:CARG1->len - | checktpeq CARG4, LJ_TISNUM - | bne ->fff_fallback - | // CARG1 = str, CARG2 = str->len, CARG3 = start, RB = end - | add CARG4, CARG2, #1 - | cmp CARG3, #0 // if (start < 0) start += len+1 - | addlt CARG3, CARG3, CARG4 - | cmp CARG3, #1 // if (start < 1) start = 1 - | movlt CARG3, #1 - | cmp RB, #0 // if (end < 0) end += len+1 - | addlt RB, RB, CARG4 - | bic RB, RB, RB, asr #31 // if (end < 0) end = 0 - | cmp RB, CARG2 // if (end > len) end = len - | add CARG1, STR:CARG1, #sizeof(GCstr)-1 - | movgt RB, CARG2 - | add CARG2, CARG1, CARG3 - | subs CARG3, RB, CARG3 // len = end - start - | add CARG3, CARG3, #1 // len += 1 - | bge ->fff_newstr - |->fff_emptystr: - | sub STR:CARG1, DISPATCH, #-DISPATCH_GL(strempty) - | mvn CARG2, #~LJ_TSTR - | b ->fff_restv - | - |.macro ffstring_op, name - | .ffunc string_ .. name - | ffgccheck - | ldr CARG3, [BASE, #4] - | cmp NARGS8:RC, #8 - | ldr STR:CARG2, [BASE] - | blo ->fff_fallback - | sub SBUF:CARG1, DISPATCH, #-DISPATCH_GL(tmpbuf) - | checkstr CARG3, ->fff_fallback - | ldr CARG4, SBUF:CARG1->b - | str BASE, L->base - | str PC, SAVE_PC - | str L, SBUF:CARG1->L - | str CARG4, SBUF:CARG1->w - | bl extern lj_buf_putstr_ .. name - | bl extern lj_buf_tostr - | b ->fff_resstr - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |// FP number to bit conversion for soft-float. Clobbers r0-r3. - |->vm_tobit_fb: - | bhi ->fff_fallback - |->vm_tobit: - | lsl RB, CARG2, #1 - | adds RB, RB, #0x00200000 - | movpl CARG1, #0 // |x| < 1? - | bxpl lr - | mvn CARG4, #0x3e0 - | subs RB, CARG4, RB, asr #21 - | bmi >1 // |x| >= 2^32? - | lsl CARG4, CARG2, #11 - | orr CARG4, CARG4, #0x80000000 - | orr CARG4, CARG4, CARG1, lsr #21 - | cmp CARG2, #0 - | lsr CARG1, CARG4, RB - | rsblt CARG1, CARG1, #0 - | bx lr - |1: - | add RB, RB, #21 - | lsr CARG4, CARG1, RB - | rsb RB, RB, #20 - | lsl CARG1, CARG2, #12 - | cmp CARG2, #0 - | orr CARG1, CARG4, CARG1, lsl RB - | rsblt CARG1, CARG1, #0 - | bx lr - | - |.macro .ffunc_bit, name - | .ffunc_1 bit_..name - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - |.endmacro - | - |.ffunc_bit tobit - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | mov CARG3, CARG1 - | mov RA, #8 - |1: - | ldrd CARG12, [BASE, RA] - | cmp RA, NARGS8:RC - | add RA, RA, #8 - | bge >2 - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - | ins CARG3, CARG3, CARG1 - | b <1 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, orr - |.ffunc_bit_op bxor, eor - | - |2: - | mvn CARG4, #~LJ_TISNUM - | ldr PC, [BASE, FRAME_PC] - | strd CARG34, [BASE, #-8] - | b ->fff_res1 - | - |.ffunc_bit bswap - | eor CARG3, CARG1, CARG1, ror #16 - | bic CARG3, CARG3, #0x00ff0000 - | ror CARG1, CARG1, #8 - | mvn CARG2, #~LJ_TISNUM - | eor CARG1, CARG1, CARG3, lsr #8 - | b ->fff_restv - | - |.ffunc_bit bnot - | mvn CARG1, CARG1 - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - | - |.macro .ffunc_bit_sh, name, ins, shmod - | .ffunc bit_..name - | ldrd CARG12, [BASE, #8] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - |.if shmod == 0 - | and RA, CARG1, #31 - |.else - | rsb RA, CARG1, #0 - |.endif - | ldrd CARG12, [BASE] - | checktp CARG2, LJ_TISNUM - | blne ->vm_tobit_fb - | ins CARG1, CARG1, RA - | mvn CARG2, #~LJ_TISNUM - | b ->fff_restv - |.endmacro - | - |.ffunc_bit_sh lshift, lsl, 0 - |.ffunc_bit_sh rshift, lsr, 0 - |.ffunc_bit_sh arshift, asr, 0 - |.ffunc_bit_sh rol, ror, 1 - |.ffunc_bit_sh ror, ror, 0 - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RC = nargs*8 - | ldr CARG3, [BASE, FRAME_FUNC] - | ldr CARG2, L->maxstack - | add CARG1, BASE, NARGS8:RC - | ldr PC, [BASE, FRAME_PC] // Fallback may overwrite PC. - | str CARG1, L->top - | ldr CARG3, CFUNC:CARG3->f - | str BASE, L->base - | add CARG1, CARG1, #8*LUA_MINSTACK - | str PC, SAVE_PC // Redundant (but a defined value). - | cmp CARG1, CARG2 - | mov CARG1, L - | bhi >5 // Need to grow stack. - | blx CARG3 // (lua_State *L) - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | ldr BASE, L->base - | cmp CRET1, #0 - | lsl RC, CRET1, #3 - | sub RA, BASE, #8 - | bgt ->fff_res // Returned nresults+1? - |1: // Returned 0 or -1: retry fast path. - | ldr CARG1, L->top - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | sub NARGS8:RC, CARG1, BASE - | bne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | ands CARG1, PC, #FRAME_TYPE - | bic CARG2, PC, #FRAME_TYPEP - | ldreq INS, [PC, #-4] - | andeq CARG2, MASKR8, INS, lsr #5 // Conditional decode_RA8. - | addeq CARG2, CARG2, #8 - | sub RB, BASE, CARG2 - | b ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | mov CARG2, #LUA_MINSTACK - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | cmp CARG1, CARG1 // Set zero-flag to force retry. - | b <1 - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | mov RA, lr - | str BASE, L->base - | add CARG2, BASE, NARGS8:RC - | str PC, SAVE_PC // Redundant (but a defined value). - | str CARG2, L->top - | mov CARG1, L - | bl extern lj_gc_step // (lua_State *L) - | ldr BASE, L->base - | mov lr, RA // Help return address predictor. - | ldr CFUNC:CARG3, [BASE, FRAME_FUNC] - | bx lr - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] - | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent. - | bne >5 - | // Decrement the hookcount for consistency, but always do the call. - | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | tst CARG1, #HOOK_ACTIVE - | bne >1 - | sub CARG2, CARG2, #1 - | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT - | strne CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | b >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] - | tst CARG1, #HOOK_ACTIVE // Hook already active? - | beq >1 - |5: // Re-dispatch to static ins. - | decode_OP OP, INS - | add OP, DISPATCH, OP, lsl #2 - | ldr pc, [OP, #GG_DISP2STATIC] - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | ldrb CARG1, [DISPATCH, #DISPATCH_GL(hookmask)] - | ldr CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | tst CARG1, #HOOK_ACTIVE // Hook already active? - | bne <5 - | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT - | beq <5 - | subs CARG2, CARG2, #1 - | str CARG2, [DISPATCH, #DISPATCH_GL(hookcount)] - | beq >1 - | tst CARG1, #LUA_MASKLINE - | beq <5 - |1: - | mov CARG1, L - | str BASE, L->base - | mov CARG2, PC - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | ldr BASE, L->base - |4: // Re-dispatch to static ins. - | ldrb OP, [PC, #-4] - | ldr INS, [PC, #-4] - | add OP, DISPATCH, OP, lsl #2 - | ldr OP, [OP, #GG_DISP2STATIC] - | decode_RA8 RA, INS - | decode_RD RC, INS - | bx OP - | - |->cont_hook: // Continue from hook yield. - | ldr CARG1, [CARG4, #-24] - | add PC, PC, #4 - | str CARG1, SAVE_MULTRES // Restore MULTRES for *M ins. - | b <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L). - | sub CARG1, DISPATCH, #-GG_DISP2J - | str PC, SAVE_PC - | ldr CARG3, LFUNC:CARG3->field_pc - | mov CARG2, PC - | str L, [DISPATCH, #DISPATCH_J(L)] - | ldrb CARG3, [CARG3, #PC2PROTO(framesize)] - | str BASE, L->base - | add CARG3, BASE, CARG3, lsl #3 - | str CARG3, L->top - | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc) - | b <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mov CARG2, PC - |.if JIT - | b >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | orr CARG2, PC, #1 - |1: - |.endif - | add CARG4, BASE, RC - | str PC, SAVE_PC - | mov CARG1, L - | str BASE, L->base - | sub RA, RA, BASE - | str CARG4, L->top - | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // Returns ASMFunction. - | ldr BASE, L->base - | ldr CARG4, L->top - | mov CARG2, #0 - | add RA, BASE, RA - | sub NARGS8:RC, CARG4, BASE - | str CARG2, SAVE_PC // Invalidate for subsequent line hook. - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | ldr INS, [PC, #-4] - | bx CRET1 - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // RA = resultptr, CARG4 = meta base - | ldr RB, SAVE_MULTRES - | ldr INS, [PC, #-4] - | ldr TRACE:CARG3, [CARG4, #-24] // Save previous trace. - | subs RB, RB, #8 - | decode_RA8 RC, INS // Call base. - | beq >2 - |1: // Move results down. - | ldrd CARG12, [RA] - | add RA, RA, #8 - | subs RB, RB, #8 - | strd CARG12, [BASE, RC] - | add RC, RC, #8 - | bne <1 - |2: - | decode_RA8 RA, INS - | decode_RB8 RB, INS - | add RA, RA, RB - |3: - | cmp RA, RC - | mvn CARG2, #~LJ_TNIL - | bhi >9 // More results wanted? - | - | ldrh RA, TRACE:CARG3->traceno - | ldrh RC, TRACE:CARG3->link - | cmp RC, RA - | beq ->cont_nop // Blacklisted. - | cmp RC, #0 - | bne =>BC_JLOOP // Jump to stitched trace. - | - | // Stitch a new trace to the previous trace. - | str RA, [DISPATCH, #DISPATCH_J(exitno)] - | str L, [DISPATCH, #DISPATCH_J(L)] - | str BASE, L->base - | sub CARG1, DISPATCH, #-GG_DISP2J - | mov CARG2, PC - | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc) - | ldr BASE, L->base - | b ->cont_nop - | - |9: // Fill up results with nil. - | strd CARG12, [BASE, RC] - | add RC, RC, #8 - | b <3 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | mov CARG1, L - | str BASE, L->base - | mov CARG2, PC - | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc) - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | ldr BASE, L->base - | sub PC, PC, #4 - | b ->cont_nop -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_exit_handler: - |.if JIT - | sub sp, sp, #12 - | push {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12} - | ldr CARG1, [sp, #64] // Load original value of lr. - | ldr DISPATCH, [lr] // Load DISPATCH. - | add CARG3, sp, #64 // Recompute original value of sp. - | mv_vmstate CARG4, EXIT - | str CARG3, [sp, #52] // Store sp in RID_SP - | st_vmstate CARG4 - | ldr CARG2, [CARG1, #-4]! // Get exit instruction. - | str CARG1, [sp, #56] // Store exit pc in RID_LR and RID_PC. - | str CARG1, [sp, #60] - |.if FPU - | vpush {d0-d15} - |.endif - | lsl CARG2, CARG2, #8 - | add CARG1, CARG1, CARG2, asr #6 - | ldr CARG2, [lr, #4] // Load exit stub group offset. - | sub CARG1, CARG1, lr - | ldr L, [DISPATCH, #DISPATCH_GL(cur_L)] - | add CARG1, CARG2, CARG1, lsr #2 // Compute exit number. - | ldr BASE, [DISPATCH, #DISPATCH_GL(jit_base)] - | str CARG1, [DISPATCH, #DISPATCH_J(exitno)] - | mov CARG4, #0 - | str BASE, L->base - | str L, [DISPATCH, #DISPATCH_J(L)] - | str CARG4, [DISPATCH, #DISPATCH_GL(jit_base)] - | sub CARG1, DISPATCH, #-GG_DISP2J - | mov CARG2, sp - | bl extern lj_trace_exit // (jit_State *J, ExitState *ex) - | // Returns MULTRES (unscaled) or negated error code. - | ldr CARG2, L->cframe - | ldr BASE, L->base - | bic CARG2, CARG2, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated. - | mov sp, CARG2 - | ldr PC, SAVE_PC // Get SAVE_PC. - | str L, SAVE_L // Set SAVE_L (on-trace resume/yield). - | b >1 - |.endif - |->vm_exit_interp: - | // CARG1 = MULTRES or negated error code, BASE, PC and DISPATCH set. - |.if JIT - | ldr L, SAVE_L - |1: - | cmp CARG1, #0 - | blt >9 // Check for error from exit. - | lsl RC, CARG1, #3 - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | str RC, SAVE_MULTRES - | mov CARG3, #0 - | str BASE, L->base - | ldr CARG2, LFUNC:CARG2->field_pc - | str CARG3, [DISPATCH, #DISPATCH_GL(jit_base)] - | mv_vmstate CARG4, INTERP - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | // Modified copy of ins_next which handles function header dispatch, too. - | ldrb OP, [PC] - | mov MASKR8, #255 - | ldr INS, [PC], #4 - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | st_vmstate CARG4 - | cmp OP, #BC_FUNCC+2 // Fast function? - | bhs >4 - |2: - | cmp OP, #BC_FUNCF // Function header? - | ldr OP, [DISPATCH, OP, lsl #2] - | decode_RA8 RA, INS - | lsrlo RC, INS, #16 // No: Decode operands A*8 and D. - | subhs RC, RC, #8 - | addhs RA, RA, BASE // Yes: RA = BASE+framesize*8, RC = nargs*8 - | ldrhs CARG3, [BASE, FRAME_FUNC] - | bx OP - | - |4: // Check frame below fast function. - | ldr CARG1, [BASE, FRAME_PC] - | ands CARG2, CARG1, #FRAME_TYPE - | bne <2 // Trace stitching continuation? - | // Otherwise set KBASE for Lua function below fast function. - | ldr CARG3, [CARG1, #-4] - | decode_RA8 CARG1, CARG3 - | sub CARG2, BASE, CARG1 - | ldr LFUNC:CARG3, [CARG2, #-16] - | ldr CARG3, LFUNC:CARG3->field_pc - | ldr KBASE, [CARG3, #PC2PROTO(k)] - | b <2 - | - |9: // Rethrow error from the right C frame. - | rsb CARG2, CARG1, #0 - | mov CARG1, L - | bl extern lj_err_trace // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// FP value rounding. Called from JIT code. - |// - |// double lj_vm_floor/ceil/trunc(double x); - |.macro vm_round, func, hf - |.if hf == 1 - | vmov CARG1, CARG2, d0 - |.endif - | lsl CARG3, CARG2, #1 - | adds RB, CARG3, #0x00200000 - | bpl >2 // |x| < 1? - | mvn CARG4, #0x3cc - | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0. - | bxlo lr // |x| >= 2^52: done. - | mvn CARG4, #1 - | bic CARG3, CARG1, CARG4, lsl RB // ztest = lo & ~lomask - | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask - | subs RB, RB, #32 - | bicpl CARG4, CARG2, CARG4, lsl RB // |x| <= 2^20: ztest |= hi & ~himask - | orrpl CARG3, CARG3, CARG4 - | mvnpl CARG4, #1 - | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0) - |.else - | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0) - |.endif - |.if hf == 1 - | vmoveq d0, CARG1, CARG2 - |.endif - | bxeq lr // iszero: done. - | mvn CARG4, #1 - | cmp RB, #0 - | lslpl CARG3, CARG4, RB - | mvnmi CARG3, #0 - | add RB, RB, #32 - | subs CARG1, CARG1, CARG4, lsl RB // lo = lo-lomask - | sbc CARG2, CARG2, CARG3 // hi = hi-himask+carry - |.if hf == 1 - | vmov d0, CARG1, CARG2 - |.endif - | bx lr - | - |2: // |x| < 1: - | bxcs lr // |x| is not finite. - | orr CARG3, CARG3, CARG1 // ztest = (2*hi) | lo - |.if "func" == "floor" - | tst CARG3, CARG2, asr #31 // iszero = ((ztest & signmask) == 0) - |.else - | bics CARG3, CARG3, CARG2, asr #31 // iszero = ((ztest & ~signmask) == 0) - |.endif - | mov CARG1, #0 // lo = 0 - | and CARG2, CARG2, #0x80000000 - | ldrne CARG4, <9 // hi = sign(x) | (iszero ? 0.0 : 1.0) - | orrne CARG2, CARG2, CARG4 - |.if hf == 1 - | vmov d0, CARG1, CARG2 - |.endif - | bx lr - |.endmacro - | - |9: - | .long 0x3ff00000 // hiword(+1.0) - | - |->vm_floor: - |.if HFABI - | vm_round floor, 1 - |.endif - |->vm_floor_sf: - | vm_round floor, 0 - | - |->vm_ceil: - |.if HFABI - | vm_round ceil, 1 - |.endif - |->vm_ceil_sf: - | vm_round ceil, 0 - | - |.macro vm_trunc, hf - |.if JIT - |.if hf == 1 - | vmov CARG1, CARG2, d0 - |.endif - | lsl CARG3, CARG2, #1 - | adds RB, CARG3, #0x00200000 - | andpl CARG2, CARG2, #0x80000000 // |x| < 1? hi = sign(x), lo = 0. - | movpl CARG1, #0 - |.if hf == 1 - | vmovpl d0, CARG1, CARG2 - |.endif - | bxpl lr - | mvn CARG4, #0x3cc - | subs RB, CARG4, RB, asr #21 // 2^0: RB = 51, 2^51: RB = 0. - | bxlo lr // |x| >= 2^52: already done. - | mvn CARG4, #1 - | and CARG1, CARG1, CARG4, lsl RB // lo &= lomask - | subs RB, RB, #32 - | andpl CARG2, CARG2, CARG4, lsl RB // |x| <= 2^20: hi &= himask - |.if hf == 1 - | vmov d0, CARG1, CARG2 - |.endif - | bx lr - |.endif - |.endmacro - | - |->vm_trunc: - |.if HFABI - | vm_trunc 1 - |.endif - |->vm_trunc_sf: - | vm_trunc 0 - | - | // double lj_vm_mod(double dividend, double divisor); - |->vm_mod: - |.if FPU - | // Special calling convention. Also, RC (r11) is not preserved. - | vdiv.f64 d0, d6, d7 - | mov RC, lr - | vmov CARG1, CARG2, d0 - | bl ->vm_floor_sf - | vmov d0, CARG1, CARG2 - | vmul.f64 d0, d0, d7 - | mov lr, RC - | vsub.f64 d6, d6, d0 - | bx lr - |.else - | push {r0, r1, r2, r3, r4, lr} - | bl extern __aeabi_ddiv - | bl ->vm_floor_sf - | ldrd CARG34, [sp, #8] - | bl extern __aeabi_dmul - | ldrd CARG34, [sp] - | eor CARG2, CARG2, #0x80000000 - | bl extern __aeabi_dadd - | add sp, sp, #20 - | pop {pc} - |.endif - | - | // int lj_vm_modi(int dividend, int divisor); - |->vm_modi: - | ands RB, CARG1, #0x80000000 - | rsbmi CARG1, CARG1, #0 // a = |dividend| - | eor RB, RB, CARG2, asr #1 // Keep signdiff and sign(divisor). - | cmp CARG2, #0 - | rsbmi CARG2, CARG2, #0 // b = |divisor| - | subs CARG4, CARG2, #1 - | cmpne CARG1, CARG2 - | moveq CARG1, #0 // if (b == 1 || a == b) a = 0 - | tsthi CARG2, CARG4 - | andeq CARG1, CARG1, CARG4 // else if ((b & (b-1)) == 0) a &= b-1 - | bls >1 - | // Use repeated subtraction to get the remainder. - | clz CARG3, CARG1 - | clz CARG4, CARG2 - | sub CARG4, CARG4, CARG3 - | rsbs CARG3, CARG4, #31 // entry = (31-(clz(b)-clz(a)))*8 - | addne pc, pc, CARG3, lsl #3 // Duff's device. - | nop - { - int i; - for (i = 31; i >= 0; i--) { - | cmp CARG1, CARG2, lsl #i - | subhs CARG1, CARG1, CARG2, lsl #i - } - } - |1: - | cmp CARG1, #0 - | cmpne RB, #0 - | submi CARG1, CARG1, CARG2 // if (y != 0 && signdiff) y = y - b - | eors CARG2, CARG1, RB, lsl #1 - | rsbmi CARG1, CARG1, #0 // if (sign(divisor) != sign(y)) y = -y - | bx lr - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |.define NEXT_TAB, TAB:CARG1 - |.define NEXT_RES, CARG1 - |.define NEXT_IDX, CARG2 - |.define NEXT_TMP0, CARG3 - |.define NEXT_TMP1, CARG4 - |.define NEXT_LIM, r12 - |.define NEXT_RES_PTR, sp - |.define NEXT_RES_VAL, [sp] - |.define NEXT_RES_KEY_I, [sp, #8] - |.define NEXT_RES_KEY_IT, [sp, #12] - | - |// TValue *lj_vm_next(GCtab *t, uint32_t idx) - |// Next idx returned in CRET2. - |->vm_next: - |.if JIT - | ldr NEXT_TMP0, NEXT_TAB->array - | ldr NEXT_LIM, NEXT_TAB->asize - | add NEXT_TMP0, NEXT_TMP0, NEXT_IDX, lsl #3 - |1: // Traverse array part. - | subs NEXT_TMP1, NEXT_IDX, NEXT_LIM - | bhs >5 - | ldr NEXT_TMP1, [NEXT_TMP0, #4] - | str NEXT_IDX, NEXT_RES_KEY_I - | add NEXT_TMP0, NEXT_TMP0, #8 - | add NEXT_IDX, NEXT_IDX, #1 - | checktp NEXT_TMP1, LJ_TNIL - | beq <1 // Skip holes in array part. - | ldr NEXT_TMP0, [NEXT_TMP0, #-8] - | mov NEXT_RES, NEXT_RES_PTR - | strd NEXT_TMP0, NEXT_RES_VAL // Stores NEXT_TMP1, too. - | mvn NEXT_TMP0, #~LJ_TISNUM - | str NEXT_TMP0, NEXT_RES_KEY_IT - | bx lr - | - |5: // Traverse hash part. - | ldr NEXT_TMP0, NEXT_TAB->hmask - | ldr NODE:NEXT_RES, NEXT_TAB->node - | add NEXT_TMP1, NEXT_TMP1, NEXT_TMP1, lsl #1 - | add NEXT_LIM, NEXT_LIM, NEXT_TMP0 - | add NODE:NEXT_RES, NODE:NEXT_RES, NEXT_TMP1, lsl #3 - |6: - | cmp NEXT_IDX, NEXT_LIM - | bhi >9 - | ldr NEXT_TMP1, NODE:NEXT_RES->val.it - | checktp NEXT_TMP1, LJ_TNIL - | add NEXT_IDX, NEXT_IDX, #1 - | bxne lr - | // Skip holes in hash part. - | add NEXT_RES, NEXT_RES, #sizeof(Node) - | b <6 - | - |9: // End of iteration. Set the key to nil (not the value). - | mvn NEXT_TMP0, #0 - | mov NEXT_RES, NEXT_RES_PTR - | str NEXT_TMP0, NEXT_RES_KEY_IT - | bx lr - |.endif - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. - |// Saveregs already performed. Callback slot number in [sp], g in r12. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | ldr CTSTATE, GL:r12->ctype_state - | add DISPATCH, r12, #GG_G2DISP - |.if FPU - | str r4, SAVE_R4 - | add r4, sp, CFRAME_SPACE+4+8*8 - | vstmdb r4!, {d8-d15} - |.endif - |.if HFABI - | add r12, CTSTATE, #offsetof(CTState, cb.fpr[8]) - |.endif - | strd CARG34, CTSTATE->cb.gpr[2] - | strd CARG12, CTSTATE->cb.gpr[0] - |.if HFABI - | vstmdb r12!, {d0-d7} - |.endif - | ldr CARG4, [sp] - | add CARG3, sp, #CFRAME_SIZE - | mov CARG1, CTSTATE - | lsr CARG4, CARG4, #3 - | str CARG3, CTSTATE->cb.stack - | mov CARG2, sp - | str CARG4, CTSTATE->cb.slot - | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok. - | bl extern lj_ccallback_enter // (CTState *cts, void *cf) - | // Returns lua_State *. - | ldr BASE, L:CRET1->base - | mv_vmstate CARG2, INTERP - | ldr RC, L:CRET1->top - | mov MASKR8, #255 - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | mov L, CRET1 - | sub RC, RC, BASE - | lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8. - | st_vmstate CARG2 - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | ldr CTSTATE, [DISPATCH, #DISPATCH_GL(ctype_state)] - | str BASE, L->base - | str CARG4, L->top - | str L, CTSTATE->L - | mov CARG1, CTSTATE - | mov CARG2, RA - | bl extern lj_ccallback_leave // (CTState *cts, TValue *o) - | ldrd CARG12, CTSTATE->cb.gpr[0] - |.if HFABI - | vldr d0, CTSTATE->cb.fpr[0] - |.endif - | b ->vm_leave_unw - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, r4 - | push {CCSTATE, r5, r11, lr} - | mov CCSTATE, CARG1 - | ldr CARG1, CCSTATE:CARG1->spadj - | ldrb CARG2, CCSTATE->nsp - | add CARG3, CCSTATE, #offsetof(CCallState, stack) - |.if HFABI - | add RB, CCSTATE, #offsetof(CCallState, fpr[0]) - |.endif - | mov r11, sp - | sub sp, sp, CARG1 // Readjust stack. - | subs CARG2, CARG2, #1 - |.if HFABI - | vldm RB, {d0-d7} - |.endif - | ldr RB, CCSTATE->func - | bmi >2 - |1: // Copy stack slots. - | ldr CARG4, [CARG3, CARG2, lsl #2] - | str CARG4, [sp, CARG2, lsl #2] - | subs CARG2, CARG2, #1 - | bpl <1 - |2: - | ldrd CARG12, CCSTATE->gpr[0] - | ldrd CARG34, CCSTATE->gpr[2] - | blx RB - | mov sp, r11 - |.if HFABI - | add r12, CCSTATE, #offsetof(CCallState, fpr[4]) - |.endif - | strd CRET1, CCSTATE->gpr[0] - |.if HFABI - | vstmdb r12!, {d0-d3} - |.endif - | pop {CCSTATE, r5, r11, pc} - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RC = src2, JMP with RC = target - | lsl RC, RC, #3 - | ldrd CARG12, [RA, BASE]! - | ldrh RB, [PC, #2] - | ldrd CARG34, [RC, BASE]! - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TISNUM - | bne >3 - | checktp CARG4, LJ_TISNUM - | bne >4 - | cmp CARG1, CARG3 - if (op == BC_ISLT) { - | sublt PC, RB, #0x20000 - } else if (op == BC_ISGE) { - | subge PC, RB, #0x20000 - } else if (op == BC_ISLE) { - | suble PC, RB, #0x20000 - } else { - | subgt PC, RB, #0x20000 - } - |1: - | ins_next - | - |3: // CARG12 is not an integer. - |.if FPU - | vldr d0, [RA] - | bhi ->vmeta_comp - | // d0 is a number. - | checktp CARG4, LJ_TISNUM - | vldr d1, [RC] - | blo >5 - | bhi ->vmeta_comp - | // d0 is a number, CARG3 is an integer. - | vmov s4, CARG3 - | vcvt.f64.s32 d1, s4 - | b >5 - |4: // CARG1 is an integer, CARG34 is not an integer. - | vldr d1, [RC] - | bhi ->vmeta_comp - | // CARG1 is an integer, d1 is a number. - | vmov s4, CARG1 - | vcvt.f64.s32 d0, s4 - |5: // d0 and d1 are numbers. - | vcmp.f64 d0, d1 - | vmrs - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - if (op == BC_ISLT) { - | sublo PC, RB, #0x20000 - } else if (op == BC_ISGE) { - | subhs PC, RB, #0x20000 - } else if (op == BC_ISLE) { - | subls PC, RB, #0x20000 - } else { - | subhi PC, RB, #0x20000 - } - | b <1 - |.else - | bhi ->vmeta_comp - | // CARG12 is a number. - | checktp CARG4, LJ_TISNUM - | movlo RA, RB // Save RB. - | blo >5 - | bhi ->vmeta_comp - | // CARG12 is a number, CARG3 is an integer. - | mov CARG1, CARG3 - | mov RC, RA - | mov RA, RB // Save RB. - | bl extern __aeabi_i2d - | mov CARG3, CARG1 - | mov CARG4, CARG2 - | ldrd CARG12, [RC] // Restore first operand. - | b >5 - |4: // CARG1 is an integer, CARG34 is not an integer. - | bhi ->vmeta_comp - | // CARG1 is an integer, CARG34 is a number. - | mov RA, RB // Save RB. - | bl extern __aeabi_i2d - | ldrd CARG34, [RC] // Restore second operand. - |5: // CARG12 and CARG34 are numbers. - | bl extern __aeabi_cdcmple - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - if (op == BC_ISLT) { - | sublo PC, RA, #0x20000 - } else if (op == BC_ISGE) { - | subhs PC, RA, #0x20000 - } else if (op == BC_ISLE) { - | subls PC, RA, #0x20000 - } else { - | subhi PC, RA, #0x20000 - } - | b <1 - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RC = src2, JMP with RC = target - | lsl RC, RC, #3 - | ldrd CARG12, [RA, BASE]! - | ldrh RB, [PC, #2] - | ldrd CARG34, [RC, BASE]! - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TISNUM - | cmnls CARG4, #-LJ_TISNUM - if (vk) { - | bls ->BC_ISEQN_Z - } else { - | bls ->BC_ISNEN_Z - } - | // Either or both types are not numbers. - |.if FFI - | checktp CARG2, LJ_TCDATA - | checktpne CARG4, LJ_TCDATA - | beq ->vmeta_equal_cd - |.endif - | cmp CARG2, CARG4 // Compare types. - | bne >2 // Not the same type? - | checktp CARG2, LJ_TISPRI - | bhs >1 // Same type and primitive type? - | - | // Same types and not a primitive type. Compare GCobj or pvalue. - | cmp CARG1, CARG3 - if (vk) { - | bne >3 // Different GCobjs or pvalues? - |1: // Branch if same. - | sub PC, RB, #0x20000 - |2: // Different. - | ins_next - |3: - | checktp CARG2, LJ_TISTABUD - | bhi <2 // Different objects and not table/ud? - } else { - | beq >1 // Same GCobjs or pvalues? - | checktp CARG2, LJ_TISTABUD - | bhi >2 // Different objects and not table/ud? - } - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | ldr TAB:RA, TAB:CARG1->metatable - | cmp TAB:RA, #0 - if (vk) { - | beq <2 // No metatable? - } else { - | beq >2 // No metatable? - } - | ldrb RA, TAB:RA->nomm - | mov CARG4, #1-vk // ne = 0 or 1. - | mov CARG2, CARG1 - | tst RA, #1<vmeta_equal // 'no __eq' flag not set? - if (vk) { - | b <2 - } else { - |2: // Branch if different. - | sub PC, RB, #0x20000 - |1: // Same. - | ins_next - } - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RC = str_const (~), JMP with RC = target - | mvn RC, RC - | ldrd CARG12, [BASE, RA] - | ldrh RB, [PC, #2] - | ldr STR:CARG3, [KBASE, RC, lsl #2] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TSTR - |.if FFI - | bne >7 - | cmp CARG1, CARG3 - |.else - | cmpeq CARG1, CARG3 - |.endif - if (vk) { - | subeq PC, RB, #0x20000 - |1: - } else { - |1: - | subne PC, RB, #0x20000 - } - | ins_next - | - |.if FFI - |7: - | checktp CARG2, LJ_TCDATA - | bne <1 - | b ->vmeta_equal_cd - |.endif - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RC = num_const (~), JMP with RC = target - | lsl RC, RC, #3 - | ldrd CARG12, [RA, BASE]! - | ldrh RB, [PC, #2] - | ldrd CARG34, [RC, KBASE]! - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | checktp CARG2, LJ_TISNUM - | bne >3 - | checktp CARG4, LJ_TISNUM - | bne >4 - | cmp CARG1, CARG3 - if (vk) { - | subeq PC, RB, #0x20000 - |1: - } else { - |1: - | subne PC, RB, #0x20000 - } - |2: - | ins_next - | - |3: // CARG12 is not an integer. - |.if FFI - | bhi >7 - |.else - if (!vk) { - | subhi PC, RB, #0x20000 - } - | bhi <2 - |.endif - |.if FPU - | checktp CARG4, LJ_TISNUM - | vmov s4, CARG3 - | vldr d0, [RA] - | vldrlo d1, [RC] - | vcvths.f64.s32 d1, s4 - | b >5 - |4: // CARG1 is an integer, d1 is a number. - | vmov s4, CARG1 - | vldr d1, [RC] - | vcvt.f64.s32 d0, s4 - |5: // d0 and d1 are numbers. - | vcmp.f64 d0, d1 - | vmrs - if (vk) { - | subeq PC, RB, #0x20000 - } else { - | subne PC, RB, #0x20000 - } - | b <2 - |.else - | // CARG12 is a number. - | checktp CARG4, LJ_TISNUM - | movlo RA, RB // Save RB. - | blo >5 - | // CARG12 is a number, CARG3 is an integer. - | mov CARG1, CARG3 - | mov RC, RA - |4: // CARG1 is an integer, CARG34 is a number. - | mov RA, RB // Save RB. - | bl extern __aeabi_i2d - | ldrd CARG34, [RC] // Restore other operand. - |5: // CARG12 and CARG34 are numbers. - | bl extern __aeabi_cdcmpeq - if (vk) { - | subeq PC, RA, #0x20000 - } else { - | subne PC, RA, #0x20000 - } - | b <2 - |.endif - | - |.if FFI - |7: - | checktp CARG2, LJ_TCDATA - | bne <1 - | b ->vmeta_equal_cd - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RC = primitive_type (~), JMP with RC = target - | ldrd CARG12, [BASE, RA] - | ldrh RB, [PC, #2] - | add PC, PC, #4 - | mvn RC, RC - | add RB, PC, RB, lsl #2 - |.if FFI - | checktp CARG2, LJ_TCDATA - | beq ->vmeta_equal_cd - |.endif - | cmp CARG2, RC - if (vk) { - | subeq PC, RB, #0x20000 - } else { - | subne PC, RB, #0x20000 - } - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RC = src, JMP with RC = target - | add RC, BASE, RC, lsl #3 - | ldrh RB, [PC, #2] - | ldrd CARG12, [RC] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | checktp CARG2, LJ_TTRUE - if (op == BC_ISTC || op == BC_IST) { - | subls PC, RB, #0x20000 - if (op == BC_ISTC) { - | strdls CARG12, [BASE, RA] - } - } else { - | subhi PC, RB, #0x20000 - if (op == BC_ISFC) { - | strdhi CARG12, [BASE, RA] - } - } - | ins_next - break; - - case BC_ISTYPE: - | // RA = src*8, RC = -type - | ldrd CARG12, [BASE, RA] - | ins_next1 - | cmn CARG2, RC - | ins_next2 - | bne ->vmeta_istype - | ins_next3 - break; - case BC_ISNUM: - | // RA = src*8, RC = -(TISNUM-1) - | ldrd CARG12, [BASE, RA] - | ins_next1 - | checktp CARG2, LJ_TISNUM - | ins_next2 - | bhs ->vmeta_istype - | ins_next3 - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RC = src - | lsl RC, RC, #3 - | ins_next1 - | ldrd CARG12, [BASE, RC] - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_NOT: - | // RA = dst*8, RC = src - | add RC, BASE, RC, lsl #3 - | ins_next1 - | ldr CARG1, [RC, #4] - | add RA, BASE, RA - | ins_next2 - | checktp CARG1, LJ_TTRUE - | mvnls CARG2, #~LJ_TFALSE - | mvnhi CARG2, #~LJ_TTRUE - | str CARG2, [RA, #4] - | ins_next3 - break; - case BC_UNM: - | // RA = dst*8, RC = src - | lsl RC, RC, #3 - | ldrd CARG12, [BASE, RC] - | ins_next1 - | ins_next2 - | checktp CARG2, LJ_TISNUM - | bhi ->vmeta_unm - | eorne CARG2, CARG2, #0x80000000 - | bne >5 - | rsbseq CARG1, CARG1, #0 - | ldrdvs CARG12, >9 - |5: - | strd CARG12, [BASE, RA] - | ins_next3 - | - |.align 8 - |9: - | .long 0x00000000, 0x41e00000 // 2^31. - break; - case BC_LEN: - | // RA = dst*8, RC = src - | lsl RC, RC, #3 - | ldrd CARG12, [BASE, RC] - | checkstr CARG2, >2 - | ldr CARG1, STR:CARG1->len - |1: - | mvn CARG2, #~LJ_TISNUM - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |2: - | checktab CARG2, ->vmeta_len -#if LJ_52 - | ldr TAB:CARG3, TAB:CARG1->metatable - | cmp TAB:CARG3, #0 - | bne >9 - |3: -#endif - |->BC_LEN_Z: - | .IOS mov RC, BASE - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | .IOS mov BASE, RC - | b <1 -#if LJ_52 - |9: - | ldrb CARG4, TAB:CARG3->nomm - | tst CARG4, #1<vmeta_len -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithcheck, cond, ncond, target - ||if (vk == 1) { - | cmn CARG4, #-LJ_TISNUM - | cmn..cond CARG2, #-LJ_TISNUM - ||} else { - | cmn CARG2, #-LJ_TISNUM - | cmn..cond CARG4, #-LJ_TISNUM - ||} - | b..ncond target - |.endmacro - |.macro ins_arithcheck_int, target - | ins_arithcheck eq, ne, target - |.endmacro - |.macro ins_arithcheck_num, target - | ins_arithcheck lo, hs, target - |.endmacro - | - |.macro ins_arithpre - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | .if FPU - | ldrd CARG12, [RB, BASE]! - | ldrd CARG34, [RC, KBASE]! - | .else - | ldrd CARG12, [BASE, RB] - | ldrd CARG34, [KBASE, RC] - | .endif - || break; - ||case 1: - | .if FPU - | ldrd CARG34, [RB, BASE]! - | ldrd CARG12, [RC, KBASE]! - | .else - | ldrd CARG34, [BASE, RB] - | ldrd CARG12, [KBASE, RC] - | .endif - || break; - ||default: - | .if FPU - | ldrd CARG12, [RB, BASE]! - | ldrd CARG34, [RC, BASE]! - | .else - | ldrd CARG12, [BASE, RB] - | ldrd CARG34, [BASE, RC] - | .endif - || break; - ||} - |.endmacro - | - |.macro ins_arithpre_fpu, reg1, reg2 - |.if FPU - ||if (vk == 1) { - | vldr reg2, [RB] - | vldr reg1, [RC] - ||} else { - | vldr reg1, [RB] - | vldr reg2, [RC] - ||} - |.endif - |.endmacro - | - |.macro ins_arithpost_fpu, reg - | ins_next1 - | add RA, BASE, RA - | ins_next2 - | vstr reg, [RA] - | ins_next3 - |.endmacro - | - |.macro ins_arithfallback, ins - ||switch (vk) { - ||case 0: - | ins ->vmeta_arith_vn - || break; - ||case 1: - | ins ->vmeta_arith_nv - || break; - ||default: - | ins ->vmeta_arith_vv - || break; - ||} - |.endmacro - | - |.macro ins_arithdn, intins, fpins, fpcall - | ins_arithpre - |.if "intins" ~= "vm_modi" and not FPU - | ins_next1 - |.endif - | ins_arithcheck_int >5 - |.if "intins" == "smull" - | smull CARG1, RC, CARG3, CARG1 - | cmp RC, CARG1, asr #31 - | ins_arithfallback bne - |.elif "intins" == "vm_modi" - | movs CARG2, CARG3 - | ins_arithfallback beq - | bl ->vm_modi - | mvn CARG2, #~LJ_TISNUM - |.else - | intins CARG1, CARG1, CARG3 - | ins_arithfallback bvs - |.endif - |4: - |.if "intins" == "vm_modi" or FPU - | ins_next1 - |.endif - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |5: // FP variant. - | ins_arithpre_fpu d6, d7 - | ins_arithfallback ins_arithcheck_num - |.if FPU - |.if "intins" == "vm_modi" - | bl fpcall - |.else - | fpins d6, d6, d7 - |.endif - | ins_arithpost_fpu d6 - |.else - | bl fpcall - |.if "intins" ~= "vm_modi" - | ins_next1 - |.endif - | b <4 - |.endif - |.endmacro - | - |.macro ins_arithfp, fpins, fpcall - | ins_arithpre - |.if "fpins" ~= "extern" or HFABI - | ins_arithpre_fpu d0, d1 - |.endif - | ins_arithfallback ins_arithcheck_num - |.if "fpins" == "extern" - | .IOS mov RC, BASE - | bl fpcall - | .IOS mov BASE, RC - |.elif FPU - | fpins d0, d0, d1 - |.else - | bl fpcall - |.endif - |.if ("fpins" ~= "extern" or HFABI) and FPU - | ins_arithpost_fpu d0 - |.else - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |.endif - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arithdn adds, vadd.f64, extern __aeabi_dadd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arithdn subs, vsub.f64, extern __aeabi_dsub - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arithdn smull, vmul.f64, extern __aeabi_dmul - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arithfp vdiv.f64, extern __aeabi_ddiv - break; - case BC_MODVN: case BC_MODNV: case BC_MODVV: - | ins_arithdn vm_modi, vm_mod, ->vm_mod - break; - case BC_POW: - | // NYI: (partial) integer arithmetic. - | ins_arithfp extern, extern pow - break; - - case BC_CAT: - | decode_RB8 RC, INS - | decode_RC8 RB, INS - | // RA = dst*8, RC = src_start*8, RB = src_end*8 (note: RB/RC swapped!) - | sub CARG3, RB, RC - | str BASE, L->base - | add CARG2, BASE, RB - |->BC_CAT_Z: - | // RA = dst*8, RC = src_start*8, CARG2 = top-1 - | mov CARG1, L - | str PC, SAVE_PC - | lsr CARG3, CARG3, #3 - | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // Returns NULL (finished) or TValue * (metamethod). - | ldr BASE, L->base - | cmp CRET1, #0 - | bne ->vmeta_binop - | ldrd CARG34, [BASE, RC] - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] // Copy result to RA. - | ins_next3 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RC = str_const (~) - | mvn RC, RC - | ins_next1 - | ldr CARG1, [KBASE, RC, lsl #2] - | mvn CARG2, #~LJ_TSTR - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RC = cdata_const (~) - | mvn RC, RC - | ins_next1 - | ldr CARG1, [KBASE, RC, lsl #2] - | mvn CARG2, #~LJ_TCDATA - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, (RC = int16_literal) - | mov CARG1, INS, asr #16 // Refetch sign-extended reg. - | mvn CARG2, #~LJ_TISNUM - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_KNUM: - | // RA = dst*8, RC = num_const - | lsl RC, RC, #3 - | ins_next1 - | ldrd CARG12, [KBASE, RC] - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - case BC_KPRI: - | // RA = dst*8, RC = primitive_type (~) - | add RA, BASE, RA - | mvn RC, RC - | ins_next1 - | ins_next2 - | str RC, [RA, #4] - | ins_next3 - break; - case BC_KNIL: - | // RA = base*8, RC = end - | add RA, BASE, RA - | add RC, BASE, RC, lsl #3 - | mvn CARG1, #~LJ_TNIL - | str CARG1, [RA, #4] - | add RA, RA, #8 - |1: - | str CARG1, [RA, #4] - | cmp RA, RC - | add RA, RA, #8 - | blt <1 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RC = uvnum - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsl RC, RC, #2 - | add RC, RC, #offsetof(GCfuncL, uvptr) - | ldr UPVAL:CARG2, [LFUNC:CARG2, RC] - | ldr CARG2, UPVAL:CARG2->v - | ldrd CARG34, [CARG2] - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - break; - case BC_USETV: - | // RA = uvnum*8, RC = src - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | lsl RC, RC, #3 - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | ldrd CARG34, [BASE, RC] - | ldrb RB, UPVAL:CARG2->marked - | ldrb RC, UPVAL:CARG2->closed - | ldr CARG2, UPVAL:CARG2->v - | tst RB, #LJ_GC_BLACK // isblack(uv) - | add RB, CARG4, #-LJ_TISGCV - | cmpne RC, #0 - | strd CARG34, [CARG2] - | bne >2 // Upvalue is closed and black? - |1: - | ins_next - | - |2: // Check if new value is collectable. - | cmn RB, #-(LJ_TNUMX - LJ_TISGCV) - | ldrbhi RC, GCOBJ:CARG3->gch.marked - | bls <1 // tvisgcv(v) - | sub CARG1, DISPATCH, #-GG_DISP2G - | tst RC, #LJ_GC_WHITES - | // Crossed a write barrier. Move the barrier forward. - |.if IOS - | beq <1 - | mov RC, BASE - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | mov BASE, RC - |.else - | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv) - |.endif - | b <1 - break; - case BC_USETS: - | // RA = uvnum*8, RC = str_const (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | mvn RC, RC - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | ldr STR:CARG3, [KBASE, RC, lsl #2] - | ldrb RB, UPVAL:CARG2->marked - | ldrb RC, UPVAL:CARG2->closed - | ldr CARG2, UPVAL:CARG2->v - | mvn CARG4, #~LJ_TSTR - | tst RB, #LJ_GC_BLACK // isblack(uv) - | ldrb RB, STR:CARG3->marked - | strd CARG34, [CARG2] - | bne >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | tst RB, #LJ_GC_WHITES // iswhite(str) - | cmpne RC, #0 - | sub CARG1, DISPATCH, #-GG_DISP2G - | // Crossed a write barrier. Move the barrier forward. - |.if IOS - | beq <1 - | mov RC, BASE - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | mov BASE, RC - |.else - | blne extern lj_gc_barrieruv // (global_State *g, TValue *tv) - |.endif - | b <1 - break; - case BC_USETN: - | // RA = uvnum*8, RC = num_const - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | lsl RC, RC, #3 - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | ldrd CARG34, [KBASE, RC] - | ldr CARG2, UPVAL:CARG2->v - | ins_next1 - | ins_next2 - | strd CARG34, [CARG2] - | ins_next3 - break; - case BC_USETP: - | // RA = uvnum*8, RC = primitive_type (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | lsr RA, RA, #1 - | add RA, RA, #offsetof(GCfuncL, uvptr) - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA] - | mvn RC, RC - | ldr CARG2, UPVAL:CARG2->v - | ins_next1 - | ins_next2 - | str RC, [CARG2, #4] - | ins_next3 - break; - - case BC_UCLO: - | // RA = level*8, RC = target - | ldr CARG3, L->openupval - | add RC, PC, RC, lsl #2 - | str BASE, L->base - | cmp CARG3, #0 - | sub PC, RC, #0x20000 - | beq >1 - | mov CARG1, L - | add CARG2, BASE, RA - | bl extern lj_func_closeuv // (lua_State *L, TValue *level) - | ldr BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RC = proto_const (~) (holding function prototype) - | mvn RC, RC - | str BASE, L->base - | ldr CARG2, [KBASE, RC, lsl #2] - | str PC, SAVE_PC - | ldr CARG3, [BASE, FRAME_FUNC] - | mov CARG1, L - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | bl extern lj_func_newL_gc - | // Returns GCfuncL *. - | ldr BASE, L->base - | mvn CARG2, #~LJ_TFUNC - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RC = (hbits|asize) | tab_const (~) - if (op == BC_TDUP) { - | mvn RC, RC - } - | ldr CARG3, [DISPATCH, #DISPATCH_GL(gc.total)] - | ldr CARG4, [DISPATCH, #DISPATCH_GL(gc.threshold)] - | str BASE, L->base - | str PC, SAVE_PC - | cmp CARG3, CARG4 - | mov CARG1, L - | bhs >5 - |1: - if (op == BC_TNEW) { - | lsl CARG2, RC, #21 - | lsr CARG3, RC, #11 - | asr RC, CARG2, #21 - | lsr CARG2, CARG2, #21 - | cmn RC, #1 - | addeq CARG2, CARG2, #2 - | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Returns GCtab *. - } else { - | ldr CARG2, [KBASE, RC, lsl #2] - | bl extern lj_tab_dup // (lua_State *L, Table *kt) - | // Returns GCtab *. - } - | ldr BASE, L->base - | mvn CARG2, #~LJ_TTAB - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - |5: - | bl extern lj_gc_step_fixtop // (lua_State *L) - | mov CARG1, L - | b <1 - break; - - case BC_GGET: - | // RA = dst*8, RC = str_const (~) - case BC_GSET: - | // RA = dst*8, RC = str_const (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | mvn RC, RC - | ldr TAB:CARG1, LFUNC:CARG2->env - | ldr STR:RC, [KBASE, RC, lsl #2] - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - break; - - case BC_TGETV: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = dst*8, RB = table*8, RC = key*8 - | ldrd TAB:CARG12, [BASE, RB] - | ldrd CARG34, [BASE, RC] - | checktab CARG2, ->vmeta_tgetv // STALL: load CARG12. - | checktp CARG4, LJ_TISNUM // Integer key? - | ldreq CARG4, TAB:CARG1->array - | ldreq CARG2, TAB:CARG1->asize - | bne >9 - | - | add CARG4, CARG4, CARG3, lsl #3 - | cmp CARG3, CARG2 // In array part? - | ldrdlo CARG34, [CARG4] - | bhs ->vmeta_tgetv - | ins_next1 // Overwrites RB! - | checktp CARG4, LJ_TNIL - | beq >5 - |1: - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG2, TAB:CARG1->metatable - | cmp TAB:CARG2, #0 - | beq <1 // No metatable: done. - | ldrb CARG2, TAB:CARG2->nomm - | tst CARG2, #1<vmeta_tgetv - | - |9: - | checktp CARG4, LJ_TSTR // String key? - | moveq STR:RC, CARG3 - | beq ->BC_TGETS_Z - | b ->vmeta_tgetv - break; - case BC_TGETS: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = dst*8, RB = table*8, RC = str_const (~) - | ldrd CARG12, [BASE, RB] - | mvn RC, RC - | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC. - | checktab CARG2, ->vmeta_tgets1 - |->BC_TGETS_Z: - | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8 - | ldr CARG3, TAB:CARG1->hmask - | ldr CARG4, STR:RC->sid - | ldr NODE:INS, TAB:CARG1->node - | mov TAB:RB, TAB:CARG1 - | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask - | add CARG3, CARG3, CARG3, lsl #1 - | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 - |1: - | ldrd CARG12, NODE:INS->key // STALL: early NODE:INS. - | ldrd CARG34, NODE:INS->val - | ldr NODE:INS, NODE:INS->next - | checktp CARG2, LJ_TSTR - | cmpeq CARG1, STR:RC - | bne >4 - | checktp CARG4, LJ_TNIL - | beq >5 - |3: - | ins_next1 - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |4: // Follow hash chain. - | cmp NODE:INS, #0 - | bne <1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG1, TAB:RB->metatable - | mov CARG3, #0 // Optional clear of undef. value (during load stall). - | mvn CARG4, #~LJ_TNIL - | cmp TAB:CARG1, #0 - | beq <3 // No metatable: done. - | ldrb CARG2, TAB:CARG1->nomm - | tst CARG2, #1<vmeta_tgets - break; - case BC_TGETB: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = dst*8, RB = table*8, RC = index - | ldrd CARG12, [BASE, RB] - | checktab CARG2, ->vmeta_tgetb // STALL: load CARG12. - | ldr CARG3, TAB:CARG1->asize - | ldr CARG4, TAB:CARG1->array - | lsl CARG2, RC, #3 - | cmp RC, CARG3 - | ldrdlo CARG34, [CARG4, CARG2] - | bhs ->vmeta_tgetb - | ins_next1 // Overwrites RB! - | checktp CARG4, LJ_TNIL - | beq >5 - |1: - | ins_next2 - | strd CARG34, [BASE, RA] - | ins_next3 - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG2, TAB:CARG1->metatable - | cmp TAB:CARG2, #0 - | beq <1 // No metatable: done. - | ldrb CARG2, TAB:CARG2->nomm - | tst CARG2, #1<vmeta_tgetb - break; - case BC_TGETR: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = dst*8, RB = table*8, RC = key*8 - | ldr TAB:CARG1, [BASE, RB] - | ldr CARG2, [BASE, RC] - | ldr CARG4, TAB:CARG1->array - | ldr CARG3, TAB:CARG1->asize - | add CARG4, CARG4, CARG2, lsl #3 - | cmp CARG2, CARG3 // In array part? - | bhs ->vmeta_tgetr - | ldrd CARG12, [CARG4] - |->BC_TGETR_Z: - | ins_next1 - | ins_next2 - | strd CARG12, [BASE, RA] - | ins_next3 - break; - - case BC_TSETV: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = src*8, RB = table*8, RC = key*8 - | ldrd TAB:CARG12, [BASE, RB] - | ldrd CARG34, [BASE, RC] - | checktab CARG2, ->vmeta_tsetv // STALL: load CARG12. - | checktp CARG4, LJ_TISNUM // Integer key? - | ldreq CARG2, TAB:CARG1->array - | ldreq CARG4, TAB:CARG1->asize - | bne >9 - | - | add CARG2, CARG2, CARG3, lsl #3 - | cmp CARG3, CARG4 // In array part? - | ldrlo INS, [CARG2, #4] - | bhs ->vmeta_tsetv - | ins_next1 // Overwrites RB! - | checktp INS, LJ_TNIL - | ldrb INS, TAB:CARG1->marked - | ldrd CARG34, [BASE, RA] - | beq >5 - |1: - | tst INS, #LJ_GC_BLACK // isblack(table) - | strd CARG34, [CARG2] - | bne >7 - |2: - | ins_next2 - | ins_next3 - | - |5: // Check for __newindex if previous value is nil. - | ldr TAB:RA, TAB:CARG1->metatable - | cmp TAB:RA, #0 - | beq <1 // No metatable: done. - | ldrb RA, TAB:RA->nomm - | tst RA, #1<vmeta_tsetv - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG1, INS, CARG3 - | b <2 - | - |9: - | checktp CARG4, LJ_TSTR // String key? - | moveq STR:RC, CARG3 - | beq ->BC_TSETS_Z - | b ->vmeta_tsetv - break; - case BC_TSETS: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = src*8, RB = table*8, RC = str_const (~) - | ldrd CARG12, [BASE, RB] - | mvn RC, RC - | ldr STR:RC, [KBASE, RC, lsl #2] // STALL: early RC. - | checktab CARG2, ->vmeta_tsets1 - |->BC_TSETS_Z: - | // (TAB:RB =) TAB:CARG1 = GCtab *, STR:RC = GCstr *, RA = dst*8 - | ldr CARG3, TAB:CARG1->hmask - | ldr CARG4, STR:RC->sid - | ldr NODE:INS, TAB:CARG1->node - | mov TAB:RB, TAB:CARG1 - | and CARG3, CARG3, CARG4 // idx = str->sid & tab->hmask - | add CARG3, CARG3, CARG3, lsl #1 - | mov CARG4, #0 - | add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8 - | strb CARG4, TAB:RB->nomm // Clear metamethod cache. - |1: - | ldrd CARG12, NODE:INS->key - | ldr CARG4, NODE:INS->val.it - | ldr NODE:CARG3, NODE:INS->next - | checktp CARG2, LJ_TSTR - | cmpeq CARG1, STR:RC - | bne >5 - | ldrb CARG2, TAB:RB->marked - | checktp CARG4, LJ_TNIL // Key found, but nil value? - | ldrd CARG34, [BASE, RA] - | beq >4 - |2: - | tst CARG2, #LJ_GC_BLACK // isblack(table) - | strd CARG34, NODE:INS->val - | bne >7 - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | ldr TAB:CARG1, TAB:RB->metatable - | cmp TAB:CARG1, #0 - | beq <2 // No metatable: done. - | ldrb CARG1, TAB:CARG1->nomm - | tst CARG1, #1<vmeta_tsets - | - |5: // Follow hash chain. - | movs NODE:INS, NODE:CARG3 - | bne <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | ldr TAB:CARG1, TAB:RB->metatable - | mov CARG3, TMPDp - | str PC, SAVE_PC - | cmp TAB:CARG1, #0 // No metatable: continue. - | str BASE, L->base - | ldrbne CARG2, TAB:CARG1->nomm - | mov CARG1, L - | beq >6 - | tst CARG2, #1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | mvn CARG4, #~LJ_TSTR - | str STR:RC, TMPDlo - | mov CARG2, TAB:RB - | str CARG4, TMPDhi - | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Returns TValue *. - | ldr BASE, L->base - | ldrd CARG34, [BASE, RA] - | strd CARG34, [CRET1] - | b <3 // No 2nd write barrier needed. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, CARG2, CARG3 - | b <3 - break; - case BC_TSETB: - | decode_RB8 RB, INS - | and RC, RC, #255 - | // RA = src*8, RB = table*8, RC = index - | ldrd CARG12, [BASE, RB] - | checktab CARG2, ->vmeta_tsetb // STALL: load CARG12. - | ldr CARG3, TAB:CARG1->asize - | ldr RB, TAB:CARG1->array - | lsl CARG2, RC, #3 - | cmp RC, CARG3 - | ldrdlo CARG34, [CARG2, RB]! - | bhs ->vmeta_tsetb - | ins_next1 // Overwrites RB! - | checktp CARG4, LJ_TNIL - | ldrb INS, TAB:CARG1->marked - | ldrd CARG34, [BASE, RA] - | beq >5 - |1: - | tst INS, #LJ_GC_BLACK // isblack(table) - | strd CARG34, [CARG2] - | bne >7 - |2: - | ins_next2 - | ins_next3 - | - |5: // Check for __newindex if previous value is nil. - | ldr TAB:RA, TAB:CARG1->metatable - | cmp TAB:RA, #0 - | beq <1 // No metatable: done. - | ldrb RA, TAB:RA->nomm - | tst RA, #1<vmeta_tsetb - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG1, INS, CARG3 - | b <2 - break; - case BC_TSETR: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = src*8, RB = table*8, RC = key*8 - | ldr TAB:CARG2, [BASE, RB] - | ldr CARG3, [BASE, RC] - | ldrb INS, TAB:CARG2->marked - | ldr CARG1, TAB:CARG2->array - | ldr CARG4, TAB:CARG2->asize - | tst INS, #LJ_GC_BLACK // isblack(table) - | add CARG1, CARG1, CARG3, lsl #3 - | bne >7 - |2: - | cmp CARG3, CARG4 // In array part? - | bhs ->vmeta_tsetr - |->BC_TSETR_Z: - | ldrd CARG34, [BASE, RA] - | ins_next1 - | ins_next2 - | strd CARG34, [CARG1] - | ins_next3 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, INS, RB - | b <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RC = num_const (start index) - | add RA, BASE, RA - |1: - | ldr RB, SAVE_MULTRES - | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table. - | ldr CARG1, [KBASE, RC, lsl #3] // Integer constant is in lo-word. - | subs RB, RB, #8 - | ldr CARG4, TAB:CARG2->asize - | beq >4 // Nothing to copy? - | add CARG3, CARG1, RB, lsr #3 - | cmp CARG3, CARG4 - | ldr CARG4, TAB:CARG2->array - | add RB, RA, RB - | bhi >5 - | add INS, CARG4, CARG1, lsl #3 - | ldrb CARG1, TAB:CARG2->marked - |3: // Copy result slots to table. - | ldrd CARG34, [RA], #8 - | strd CARG34, [INS], #8 - | cmp RA, RB - | blo <3 - | tst CARG1, #LJ_GC_BLACK // isblack(table) - | bne >7 - |4: - | ins_next - | - |5: // Need to resize array part. - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | // Must not reallocate the stack. - | .IOS ldr BASE, L->base - | b <1 - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, CARG1, CARG3 - | b <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = nresults+1,) RC = extra_nargs - | ldr CARG1, SAVE_MULTRES - | decode_RC8 NARGS8:RC, INS - | add NARGS8:RC, NARGS8:RC, CARG1 - | b ->BC_CALL_Z - break; - case BC_CALL: - | decode_RC8 NARGS8:RC, INS - | // RA = base*8, (RB = nresults+1,) RC = (nargs+1)*8 - |->BC_CALL_Z: - | mov RB, BASE // Save old BASE for vmeta_call. - | ldrd CARG34, [BASE, RA]! - | sub NARGS8:RC, NARGS8:RC, #8 - | add BASE, BASE, #8 - | checkfunc CARG4, ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs - | ldr CARG1, SAVE_MULTRES - | add NARGS8:RC, CARG1, RC, lsl #3 - | b ->BC_CALLT1_Z - break; - case BC_CALLT: - | lsl NARGS8:RC, RC, #3 - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - |->BC_CALLT1_Z: - | ldrd LFUNC:CARG34, [RA, BASE]! - | sub NARGS8:RC, NARGS8:RC, #8 - | add RA, RA, #8 - | checkfunc CARG4, ->vmeta_callt - | ldr PC, [BASE, FRAME_PC] - |->BC_CALLT2_Z: - | mov RB, #0 - | ldrb CARG4, LFUNC:CARG3->ffid - | tst PC, #FRAME_TYPE - | bne >7 - |1: - | str LFUNC:CARG3, [BASE, FRAME_FUNC] // Copy function down, but keep PC. - | cmp NARGS8:RC, #0 - | beq >3 - |2: - | ldrd CARG12, [RA, RB] - | add INS, RB, #8 - | cmp INS, NARGS8:RC - | strd CARG12, [BASE, RB] - | mov RB, INS - | bne <2 - |3: - | cmp CARG4, #1 // (> FF_C) Calling a fast function? - | bhi >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | ldr INS, [PC, #-4] - | decode_RA8 RA, INS - | sub CARG1, BASE, RA - | ldr LFUNC:CARG1, [CARG1, #-16] - | ldr CARG1, LFUNC:CARG1->field_pc - | ldr KBASE, [CARG1, #PC2PROTO(k)] - | b <4 - | - |7: // Tailcall from a vararg function. - | eor PC, PC, #FRAME_VARG - | tst PC, #FRAME_TYPEP // Vararg frame below? - | movne CARG4, #0 // Clear ffid if no Lua function below. - | bne <1 - | sub BASE, BASE, PC - | ldr PC, [BASE, FRAME_PC] - | tst PC, #FRAME_TYPE - | movne CARG4, #0 // Clear ffid if no Lua function below. - | b <1 - break; - - case BC_ITERC: - | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1)) - | add RA, BASE, RA - | mov RB, BASE // Save old BASE for vmeta_call. - | ldrd CARG34, [RA, #-16] - | ldrd CARG12, [RA, #-8] - | add BASE, RA, #8 - | strd CARG34, [RA, #8] // Copy state. - | strd CARG12, [RA, #16] // Copy control var. - | // STALL: locked CARG34. - | ldrd LFUNC:CARG34, [RA, #-24] - | mov NARGS8:RC, #16 // Iterators get 2 arguments. - | // STALL: load CARG34. - | strd LFUNC:CARG34, [RA] // Copy callable. - | checkfunc CARG4, ->vmeta_call - | ins_call - break; - - case BC_ITERN: - |.if JIT - | hotloop - |.endif - |->vm_IITERN: - | // RA = base*8, (RB = nresults+1, RC = nargs+1 (2+1)) - | add RA, BASE, RA - | ldr TAB:RB, [RA, #-16] - | ldr CARG1, [RA, #-8] // Get index from control var. - | ldr INS, TAB:RB->asize - | ldr CARG2, TAB:RB->array - | add PC, PC, #4 - |1: // Traverse array part. - | subs RC, CARG1, INS - | add CARG3, CARG2, CARG1, lsl #3 - | bhs >5 // Index points after array part? - | ldrd CARG34, [CARG3] - | checktp CARG4, LJ_TNIL - | addeq CARG1, CARG1, #1 // Skip holes in array part. - | beq <1 - | ldrh RC, [PC, #-2] - | mvn CARG2, #~LJ_TISNUM - | strd CARG34, [RA, #8] - | add RC, PC, RC, lsl #2 - | add RB, CARG1, #1 - | strd CARG12, [RA] - | sub PC, RC, #0x20000 - | str RB, [RA, #-8] // Update control var. - |3: - | ins_next - | - |5: // Traverse hash part. - | ldr CARG4, TAB:RB->hmask - | ldr NODE:RB, TAB:RB->node - |6: - | add CARG1, RC, RC, lsl #1 - | cmp RC, CARG4 // End of iteration? Branch to ITERL+1. - | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8 - | bhi <3 - | ldrd CARG12, NODE:CARG3->val - | checktp CARG2, LJ_TNIL - | add RC, RC, #1 - | beq <6 // Skip holes in hash part. - | ldrh RB, [PC, #-2] - | add RC, RC, INS - | ldrd CARG34, NODE:CARG3->key - | str RC, [RA, #-8] // Update control var. - | strd CARG12, [RA, #8] - | add RC, PC, RB, lsl #2 - | sub PC, RC, #0x20000 - | strd CARG34, [RA] - | b <3 - break; - - case BC_ISNEXT: - | // RA = base*8, RC = target (points to ITERN) - | add RA, BASE, RA - | add RC, PC, RC, lsl #2 - | ldrd CFUNC:CARG12, [RA, #-24] - | ldr CARG3, [RA, #-12] - | ldr CARG4, [RA, #-4] - | checktp CARG2, LJ_TFUNC - | ldrbeq CARG1, CFUNC:CARG1->ffid - | checktpeq CARG3, LJ_TTAB - | checktpeq CARG4, LJ_TNIL - | cmpeq CARG1, #FF_next_N - | subeq PC, RC, #0x20000 - | bne >5 - | ins_next1 - | ins_next2 - | mov CARG1, #0 - | mvn CARG2, #~LJ_KEYINDEX - | strd CARG1, [RA, #-8] // Initialize control var. - |1: - | ins_next3 - |5: // Despecialize bytecode if any of the checks fail. - | mov CARG1, #BC_JMP - | mov OP, #BC_ITERC - | strb CARG1, [PC, #-4] - | sub PC, RC, #0x20000 - |.if JIT - | ldrb CARG1, [PC] - | cmp CARG1, #BC_ITERN - | bne >6 - |.endif - | strb OP, [PC] // Subsumes ins_next1. - | ins_next2 - | b <1 - |.if JIT - |6: // Unpatch JLOOP. - | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)] - | ldrh CARG2, [PC, #2] - | ldr TRACE:CARG1, [CARG1, CARG2, lsl #2] - | // Subsumes ins_next1 and ins_next2. - | ldr INS, TRACE:CARG1->startins - | bfi INS, OP, #0, #8 - | str INS, [PC], #4 - | b <1 - |.endif - break; - - case BC_VARG: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | ldr CARG1, [BASE, FRAME_PC] - | add RC, BASE, RC - | add RA, BASE, RA - | add RC, RC, #FRAME_VARG - | add CARG4, RA, RB - | sub CARG3, BASE, #8 // CARG3 = vtop - | sub RC, RC, CARG1 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | cmp RB, #0 - | sub CARG1, CARG3, RC - | beq >5 // Copy all varargs? - | sub CARG4, CARG4, #16 - |1: // Copy vararg slots to destination slots. - | cmp RC, CARG3 - | ldrdlo CARG12, [RC], #8 - | mvnhs CARG2, #~LJ_TNIL - | cmp RA, CARG4 - | strd CARG12, [RA], #8 - | blo <1 - |2: - | ins_next - | - |5: // Copy all varargs. - | ldr CARG4, L->maxstack - | cmp CARG1, #0 - | movle RB, #8 // MULTRES = (0+1)*8 - | addgt RB, CARG1, #8 - | add CARG2, RA, CARG1 - | str RB, SAVE_MULTRES - | ble <2 - | cmp CARG2, CARG4 - | bhi >7 - |6: - | ldrd CARG12, [RC], #8 - | strd CARG12, [RA], #8 - | cmp RC, CARG3 - | blo <6 - | b <2 - | - |7: // Grow stack for varargs. - | lsr CARG2, CARG1, #3 - | str RA, L->top - | mov CARG1, L - | str BASE, L->base - | sub RC, RC, BASE // Need delta, because BASE may change. - | str PC, SAVE_PC - | sub RA, RA, BASE - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | add RA, BASE, RA - | add RC, BASE, RC - | sub CARG3, BASE, #8 - | b <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RC = extra results - | ldr CARG1, SAVE_MULTRES - | ldr PC, [BASE, FRAME_PC] - | add RA, BASE, RA - | add RC, CARG1, RC, lsl #3 - | b ->BC_RETM_Z - break; - - case BC_RET: - | // RA = results*8, RC = nresults+1 - | ldr PC, [BASE, FRAME_PC] - | lsl RC, RC, #3 - | add RA, BASE, RA - |->BC_RETM_Z: - | str RC, SAVE_MULTRES - |1: - | ands CARG1, PC, #FRAME_TYPE - | eor CARG2, PC, #FRAME_VARG - | bne ->BC_RETV2_Z - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return - | ldr INS, [PC, #-4] - | subs CARG4, RC, #8 - | sub CARG3, BASE, #8 - | beq >3 - |2: - | ldrd CARG12, [RA], #8 - | add BASE, BASE, #8 - | subs CARG4, CARG4, #8 - | strd CARG12, [BASE, #-16] - | bne <2 - |3: - | decode_RA8 RA, INS - | sub CARG4, CARG3, RA - | decode_RB8 RB, INS - | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC] - |5: - | cmp RB, RC // More results expected? - | bhi >6 - | mov BASE, CARG4 - | ldr CARG2, LFUNC:CARG1->field_pc - | ins_next1 - | ins_next2 - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | ins_next3 - | - |6: // Fill up results with nil. - | mvn CARG2, #~LJ_TNIL - | add BASE, BASE, #8 - | add RC, RC, #8 - | str CARG2, [BASE, #-12] - | b <5 - | - |->BC_RETV1_Z: // Non-standard return case. - | add RA, BASE, RA - |->BC_RETV2_Z: - | tst CARG2, #FRAME_TYPEP - | bne ->vm_return - | // Return from vararg function: relocate BASE down. - | sub BASE, BASE, CARG2 - | ldr PC, [BASE, FRAME_PC] - | b <1 - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RC = nresults+1 - | ldr PC, [BASE, FRAME_PC] - | lsl RC, RC, #3 - | str RC, SAVE_MULTRES - | ands CARG1, PC, #FRAME_TYPE - | eor CARG2, PC, #FRAME_VARG - | ldreq INS, [PC, #-4] - | bne ->BC_RETV1_Z - if (op == BC_RET1) { - | ldrd CARG12, [BASE, RA] - } - | sub CARG4, BASE, #8 - | decode_RA8 RA, INS - if (op == BC_RET1) { - | strd CARG12, [CARG4] - } - | sub BASE, CARG4, RA - | decode_RB8 RB, INS - | ldr LFUNC:CARG1, [BASE, FRAME_FUNC] - |5: - | cmp RB, RC - | bhi >6 - | ldr CARG2, LFUNC:CARG1->field_pc - | ins_next1 - | ins_next2 - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | ins_next3 - | - |6: // Fill up results with nil. - | sub CARG2, CARG4, #4 - | mvn CARG3, #~LJ_TNIL - | str CARG3, [CARG2, RC] - | add RC, RC, #8 - | b <5 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4] - |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12] - |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20] - |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28] - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RC = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | ldrd CARG12, [RA, BASE]! - if (op != BC_JFORL) { - | add RC, PC, RC, lsl #2 - } - if (!vk) { - | ldrd CARG34, FOR_STOP - | checktp CARG2, LJ_TISNUM - | ldr RB, FOR_TSTEP - | bne >5 - | checktp CARG4, LJ_TISNUM - | ldr CARG4, FOR_STEP - | checktpeq RB, LJ_TISNUM - | bne ->vmeta_for - | cmp CARG4, #0 - | blt >4 - | cmp CARG1, CARG3 - } else { - | ldrd CARG34, FOR_STEP - | checktp CARG2, LJ_TISNUM - | bne >5 - | adds CARG1, CARG1, CARG3 - | ldr CARG4, FOR_STOP - if (op == BC_IFORL) { - | addvs RC, PC, #0x20000 // Overflow: prevent branch. - } else { - | bvs >2 // Overflow: do not enter mcode. - } - | cmp CARG3, #0 - | blt >4 - | cmp CARG1, CARG4 - } - |1: - if (op == BC_FORI) { - | subgt PC, RC, #0x20000 - } else if (op == BC_JFORI) { - | sub PC, RC, #0x20000 - | ldrhle RC, [PC, #-2] - } else if (op == BC_IFORL) { - | suble PC, RC, #0x20000 - } - if (vk) { - | strd CARG12, FOR_IDX - } - |2: - | ins_next1 - | ins_next2 - | strd CARG12, FOR_EXT - if (op == BC_JFORI || op == BC_JFORL) { - | ble =>BC_JLOOP - } - |3: - | ins_next3 - | - |4: // Invert check for negative step. - if (!vk) { - | cmp CARG3, CARG1 - } else { - | cmp CARG4, CARG1 - } - | b <1 - | - |5: // FP loop. - if (!vk) { - | cmnlo CARG4, #-LJ_TISNUM - | cmnlo RB, #-LJ_TISNUM - | bhs ->vmeta_for - |.if FPU - | vldr d0, FOR_IDX - | vldr d1, FOR_STOP - | cmp RB, #0 - | vstr d0, FOR_EXT - |.else - | cmp RB, #0 - | strd CARG12, FOR_EXT - | blt >8 - |.endif - } else { - |.if FPU - | vldr d0, FOR_IDX - | vldr d2, FOR_STEP - | vldr d1, FOR_STOP - | cmp CARG4, #0 - | vadd.f64 d0, d0, d2 - |.else - | cmp CARG4, #0 - | blt >8 - | bl extern __aeabi_dadd - | strd CARG12, FOR_IDX - | ldrd CARG34, FOR_STOP - | strd CARG12, FOR_EXT - |.endif - } - |6: - |.if FPU - | vcmpge.f64 d0, d1 - | vcmplt.f64 d1, d0 - | vmrs - |.else - | bl extern __aeabi_cdcmple - |.endif - if (vk) { - |.if FPU - | vstr d0, FOR_IDX - | vstr d0, FOR_EXT - |.endif - } - if (op == BC_FORI) { - | subhi PC, RC, #0x20000 - } else if (op == BC_JFORI) { - | sub PC, RC, #0x20000 - | ldrhls RC, [PC, #-2] - | bls =>BC_JLOOP - } else if (op == BC_IFORL) { - | subls PC, RC, #0x20000 - } else { - | bls =>BC_JLOOP - } - | ins_next1 - | ins_next2 - | b <3 - | - |.if not FPU - |8: // Invert check for negative step. - if (vk) { - | bl extern __aeabi_dadd - | strd CARG12, FOR_IDX - | strd CARG12, FOR_EXT - } - | mov CARG3, CARG1 - | mov CARG4, CARG2 - | ldrd CARG12, FOR_STOP - | b <6 - |.endif - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RC = target - | ldrd CARG12, [RA, BASE]! - if (op == BC_JITERL) { - | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil. - | strdne CARG12, [RA, #-8] - | bne =>BC_JLOOP - } else { - | add RC, PC, RC, lsl #2 - | // STALL: load CARG12. - | cmn CARG2, #-LJ_TNIL // Stop if iterator returned nil. - | subne PC, RC, #0x20000 // Otherwise save control var + branch. - | strdne CARG12, [RA, #-8] - } - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RC = target (loop extent) - | // Note: RA/RC is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RC = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base (ignored), RC = traceno - | ldr CARG1, [DISPATCH, #DISPATCH_J(trace)] - | mov CARG2, #0 // Traces on ARM don't store the trace number, so use 0. - | ldr TRACE:RC, [CARG1, RC, lsl #2] - | st_vmstate CARG2 - | ldr RA, TRACE:RC->mcode - | str BASE, [DISPATCH, #DISPATCH_GL(jit_base)] - | str L, [DISPATCH, #DISPATCH_GL(tmpbuf.L)] - | bx RA - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RC = target - | add RC, PC, RC, lsl #2 - | sub PC, RC, #0x20000 - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 - | ldr CARG1, L->maxstack - | ldrb CARG2, [PC, #-4+PC2PROTO(numparams)] - | ldr KBASE, [PC, #-4+PC2PROTO(k)] - | cmp RA, CARG1 - | bhi ->vm_growstack_l - if (op != BC_JFUNCF) { - | ins_next1 - | ins_next2 - } - |2: - | cmp NARGS8:RC, CARG2, lsl #3 // Check for missing parameters. - | mvn CARG4, #~LJ_TNIL - | blo >3 - if (op == BC_JFUNCF) { - | decode_RD RC, INS - | b =>BC_JLOOP - } else { - | ins_next3 - } - | - |3: // Clear missing parameters. - | strd CARG34, [BASE, NARGS8:RC] - | add NARGS8:RC, NARGS8:RC, #8 - | b <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 - | ldr CARG1, L->maxstack - | add CARG4, BASE, RC - | add RA, RA, RC - | str LFUNC:CARG3, [CARG4] // Store copy of LFUNC. - | add CARG2, RC, #8+FRAME_VARG - | ldr KBASE, [PC, #-4+PC2PROTO(k)] - | cmp RA, CARG1 - | str CARG2, [CARG4, #4] // Store delta + FRAME_VARG. - | bhs ->vm_growstack_l - | ldrb RB, [PC, #-4+PC2PROTO(numparams)] - | mov RA, BASE - | mov RC, CARG4 - | cmp RB, #0 - | add BASE, CARG4, #8 - | beq >3 - | mvn CARG3, #~LJ_TNIL - |1: - | cmp RA, RC // Less args than parameters? - | ldrdlo CARG12, [RA], #8 - | movhs CARG2, CARG3 - | strlo CARG3, [RA, #-4] // Clear old fixarg slot (help the GC). - |2: - | subs RB, RB, #1 - | strd CARG12, [CARG4, #8]! - | bne <1 - |3: - | ins_next - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | ldr CARG4, CFUNC:CARG3->f - } else { - | ldr CARG4, [DISPATCH, #DISPATCH_GL(wrapf)] - } - | add CARG2, RA, NARGS8:RC - | ldr CARG1, L->maxstack - | add RC, BASE, NARGS8:RC - | str BASE, L->base - | cmp CARG2, CARG1 - | str RC, L->top - if (op == BC_FUNCCW) { - | ldr CARG2, CFUNC:CARG3->f - } - | mv_vmstate CARG3, C - | mov CARG1, L - | bhi ->vm_growstack_c // Need to grow stack. - | st_vmstate CARG3 - | blx CARG4 // (lua_State *L [, lua_CFunction f]) - | // Returns nresults. - | ldr BASE, L->base - | mv_vmstate CARG3, INTERP - | ldr CRET2, L->top - | str L, [DISPATCH, #DISPATCH_GL(cur_L)] - | lsl RC, CRET1, #3 - | st_vmstate CARG3 - | ldr PC, [BASE, FRAME_PC] - | sub RA, CRET2, RC // RA = L->top - nresults*8 - | b ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 0xe\n" /* Return address is in lr. */ - "\t.byte 0xc\n\t.uleb128 0xd\n\t.uleb128 0\n" /* def_cfa sp */ - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x8e\n\t.uleb128 1\n", /* offset lr */ - fcofs, CFRAME_SIZE); - for (i = 11; i >= (LJ_ARCH_HASFPU ? 5 : 4); i--) /* offset r4-r11 */ - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2+(11-i)); -#if LJ_ARCH_HASFPU - for (i = 15; i >= 8; i--) /* offset d8-d15 */ - fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 %d, %d\n", - 64+2*i, 10+2*(15-i)); - fprintf(ctx->fp, "\t.byte 0x84\n\t.uleb128 %d\n", 25); /* offset r4 */ -#endif - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" - "\t.long lj_vm_ffi_call\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x8e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x8b\n\t.uleb128 2\n" /* offset r11 */ - "\t.byte 0x85\n\t.uleb128 3\n" /* offset r5 */ - "\t.byte 0x84\n\t.uleb128 4\n" /* offset r4 */ - "\t.byte 0xd\n\t.uleb128 0xb\n" /* def_cfa_register r11 */ - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif - break; - default: - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm64.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm64.dasc deleted file mode 100644 index 36a036a..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_arm64.dasc +++ /dev/null @@ -1,4160 +0,0 @@ -|// Low-level VM code for ARM64 CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -| -|.arch arm64 -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// ARM64 registers and the AAPCS64 ABI 1.0 at a glance: -|// -|// x0-x17 temp, x19-x28 callee-saved, x29 fp, x30 lr -|// x18 is reserved on most platforms. Don't use it, save it or restore it. -|// x31 doesn't exist. Register number 31 either means xzr/wzr (zero) or sp, -|// depending on the instruction. -|// v0-v7 temp, v8-v15 callee-saved (only d8-d15 preserved), v16-v31 temp -|// -|// x0-x7/v0-v7 hold parameters and results. -| -|// Fixed register assignments for the interpreter. -| -|// The following must be C callee-save. -|.define BASE, x19 // Base of current Lua stack frame. -|.define KBASE, x20 // Constants of current Lua function. -|.define PC, x21 // Next PC. -|.define GLREG, x22 // Global state. -|.define LREG, x23 // Register holding lua_State (also in SAVE_L). -|.define TISNUM, x24 // Constant LJ_TISNUM << 47. -|.define TISNUMhi, x25 // Constant LJ_TISNUM << 15. -|.define TISNIL, x26 // Constant -1LL. -|.define fp, x29 // Yes, we have to maintain a frame pointer. -| -|.define ST_INTERP, w26 // Constant -1. -| -|// The following temporaries are not saved across C calls, except for RA/RC. -|.define RA, x27 -|.define RC, x28 -|.define RB, x17 -|.define RAw, w27 -|.define RCw, w28 -|.define RBw, w17 -|.define INS, x16 -|.define INSw, w16 -|.define ITYPE, x15 -|.define TMP0, x8 -|.define TMP1, x9 -|.define TMP2, x10 -|.define TMP3, x11 -|.define TMP0w, w8 -|.define TMP1w, w9 -|.define TMP2w, w10 -|.define TMP3w, w11 -| -|// Calling conventions. Also used as temporaries. -|.define CARG1, x0 -|.define CARG2, x1 -|.define CARG3, x2 -|.define CARG4, x3 -|.define CARG5, x4 -|.define CARG1w, w0 -|.define CARG2w, w1 -|.define CARG3w, w2 -|.define CARG4w, w3 -|.define CARG5w, w4 -| -|.define FARG1, d0 -|.define FARG2, d1 -| -|.define CRET1, x0 -|.define CRET1w, w0 -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -| -|.define CFRAME_SPACE, 208 -|//----- 16 byte aligned, <-- sp entering interpreter -|.define SAVE_FP_LR_, 192 -|.define SAVE_GPR_, 112 // 112+10*8: 64 bit GPR saves -|.define SAVE_FPR_, 48 // 48+8*8: 64 bit FPR saves -|// Unused [sp, #44] // 32 bit values -|.define SAVE_NRES, [sp, #40] -|.define SAVE_ERRF, [sp, #36] -|.define SAVE_MULTRES, [sp, #32] -|.define TMPD, [sp, #24] // 64 bit values -|.define SAVE_L, [sp, #16] -|.define SAVE_PC, [sp, #8] -|.define SAVE_CFRAME, [sp, #0] -|//----- 16 byte aligned, <-- sp while in interpreter. -| -|.define TMPDofs, #24 -| -|.macro save_, gpr1, gpr2, fpr1, fpr2 -| stp d..fpr2, d..fpr1, [sp, # SAVE_FPR_+(14-fpr1)*8] -| stp x..gpr2, x..gpr1, [sp, # SAVE_GPR_+(27-gpr1)*8] -|.endmacro -|.macro rest_, gpr1, gpr2, fpr1, fpr2 -| ldp d..fpr2, d..fpr1, [sp, # SAVE_FPR_+(14-fpr1)*8] -| ldp x..gpr2, x..gpr1, [sp, # SAVE_GPR_+(27-gpr1)*8] -|.endmacro -| -|.macro saveregs -| sub sp, sp, # CFRAME_SPACE -| stp fp, lr, [sp, # SAVE_FP_LR_] -| add fp, sp, # SAVE_FP_LR_ -| stp x20, x19, [sp, # SAVE_GPR_+(27-19)*8] -| save_ 21, 22, 8, 9 -| save_ 23, 24, 10, 11 -| save_ 25, 26, 12, 13 -| save_ 27, 28, 14, 15 -|.endmacro -|.macro restoreregs -| ldp x20, x19, [sp, # SAVE_GPR_+(27-19)*8] -| rest_ 21, 22, 8, 9 -| rest_ 23, 24, 10, 11 -| rest_ 25, 26, 12, 13 -| rest_ 27, 28, 14, 15 -| ldp fp, lr, [sp, # SAVE_FP_LR_] -| add sp, sp, # CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State, GLREG -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; brk; .endmacro -| -|//----------------------------------------------------------------------- -| -|// Access to frame relative to BASE. -|.define FRAME_FUNC, #-16 -|.define FRAME_PC, #-8 -| -|// Endian-specific defines. -|.if ENDIAN_LE -|.define LO, 0 -|.define OFS_RD, 2 -|.define OFS_RB, 3 -|.define OFS_RA, 1 -|.define OFS_OP, 0 -|.else -|.define LO, 4 -|.define OFS_RD, 0 -|.define OFS_RB, 0 -|.define OFS_RA, 2 -|.define OFS_OP, 3 -|.endif -| -|.macro decode_RA, dst, ins; ubfx dst, ins, #8, #8; .endmacro -|.macro decode_RB, dst, ins; ubfx dst, ins, #24, #8; .endmacro -|.macro decode_RC, dst, ins; ubfx dst, ins, #16, #8; .endmacro -|.macro decode_RD, dst, ins; ubfx dst, ins, #16, #16; .endmacro -|.macro decode_RC8RD, dst, src; ubfiz dst, src, #3, #8; .endmacro -| -|// Instruction decode+dispatch. -|.macro ins_NEXT -| ldr INSw, [PC], #4 -| add TMP1, GL, INS, uxtb #3 -| decode_RA RA, INS -| ldr TMP0, [TMP1, #GG_G2DISP] -| decode_RD RC, INS -| br TMP0 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| ldr PC, LFUNC:CARG3->pc -| ldr INSw, [PC], #4 -| add TMP1, GL, INS, uxtb #3 -| decode_RA RA, INS -| ldr TMP0, [TMP1, #GG_G2DISP] -| add RA, BASE, RA, lsl #3 -| br TMP0 -|.endmacro -| -|.macro ins_call -| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| str PC, [BASE, FRAME_PC] -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to check the TValue type and extract the GCobj. Branch on failure. -|.macro checktp, reg, tp, target -| asr ITYPE, reg, #47 -| cmn ITYPE, #-tp -| and reg, reg, #LJ_GCVMASK -| bne target -|.endmacro -|.macro checktp, dst, reg, tp, target -| asr ITYPE, reg, #47 -| cmn ITYPE, #-tp -| and dst, reg, #LJ_GCVMASK -| bne target -|.endmacro -|.macro checkstr, reg, target; checktp reg, LJ_TSTR, target; .endmacro -|.macro checktab, reg, target; checktp reg, LJ_TTAB, target; .endmacro -|.macro checkfunc, reg, target; checktp reg, LJ_TFUNC, target; .endmacro -|.macro checkint, reg, target -| cmp TISNUMhi, reg, lsr #32 -| bne target -|.endmacro -|.macro checknum, reg, target -| cmp TISNUMhi, reg, lsr #32 -| bls target -|.endmacro -|.macro checknumber, reg, target -| cmp TISNUMhi, reg, lsr #32 -| blo target -|.endmacro -| -|.macro mov_false, reg; movn reg, #0x8000, lsl #32; .endmacro -|.macro mov_true, reg; movn reg, #0x0001, lsl #48; .endmacro -| -#define GL_J(field) (GG_G2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro hotcheck, delta -| lsr CARG1, PC, #1 -| and CARG1, CARG1, #126 -| add CARG1, CARG1, #GG_G2DISP+GG_DISP2HOT -| ldrh CARG2w, [GL, CARG1] -| subs CARG2, CARG2, #delta -| strh CARG2w, [GL, CARG1] -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP -| blo ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL -| blo ->vm_hotcall -|.endmacro -| -|// Set current VM state. -|.macro mv_vmstate, reg, st; movn reg, #LJ_VMST_..st; .endmacro -|.macro st_vmstate, reg; str reg, GL->vmstate; .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp -| ldr tmp, GL->gc.grayagain -| and mark, mark, #~LJ_GC_BLACK // black2gray(tab) -| str tab, GL->gc.grayagain -| strb mark, tab->marked -| str tmp, tab->gclist -|.endmacro -| -|//----------------------------------------------------------------------- - -#if !LJ_DUALNUM -#error "Only dual-number mode supported for ARM64 target" -#endif - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: RB = previous base. - | tbz PC, #2, ->cont_dispatch // (PC & FRAME_P) == 0? - | - | // Return from pcall or xpcall fast func. - | ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame. - | mov_true TMP0 - | mov BASE, RB - | // Prepending may overwrite the pcall frame, so do it at the end. - | str TMP0, [RA, #-8]! // Prepend true to results. - | - |->vm_returnc: - | adds RC, RC, #8 // RC = (nresults+1)*8. - | mov CRET1, #LUA_YIELD - | beq ->vm_unwind_c_eh - | str RCw, SAVE_MULTRES - | ands CARG1, PC, #FRAME_TYPE - | beq ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return - | // CARG1 = PC & FRAME_TYPE - | and RB, PC, #~FRAME_TYPEP - | cmp CARG1, #FRAME_C - | sub RB, BASE, RB // RB = previous base. - | bne ->vm_returnp - | - | str RB, L->base - | ldrsw CARG2, SAVE_NRES // CARG2 = nresults+1. - | mv_vmstate TMP0w, C - | sub BASE, BASE, #16 - | subs TMP2, RC, #8 - | st_vmstate TMP0w - | beq >2 - |1: - | subs TMP2, TMP2, #8 - | ldr TMP0, [RA], #8 - | str TMP0, [BASE], #8 - | bne <1 - |2: - | cmp RC, CARG2, lsl #3 // More/less results wanted? - | bne >6 - |3: - | str BASE, L->top // Store new top. - | - |->vm_leave_cp: - | ldr RC, SAVE_CFRAME // Restore previous C frame. - | mov CRET1, #0 // Ok return status for vm_pcall. - | str RC, L->cframe - | - |->vm_leave_unw: - | restoreregs - | ret - | - |6: - | bgt >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | ldr CARG3, L->maxstack - | cmp BASE, CARG3 - | bhs >8 - | str TISNIL, [BASE], #8 - | add RC, RC, #8 - | b <2 - | - |7: // Less results wanted. - | cbz CARG2, <3 // LUA_MULTRET+1 case? - | sub CARG1, RC, CARG2, lsl #3 - | sub BASE, BASE, CARG1 // Shrink top. - | b <3 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | str BASE, L->top // Save current top held in BASE (yes). - | mov CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->top // Need the (realloced) L->top in BASE. - | ldrsw CARG2, SAVE_NRES - | b <2 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mov sp, CARG1 - | mov CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | ldr L, SAVE_L - | mv_vmstate TMP0w, C - | ldr GL, L->glref - | st_vmstate TMP0w - | b ->vm_leave_unw - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | and sp, CARG1, #CFRAME_RAWMASK - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | ldr L, SAVE_L - | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48 - | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16 - | movn TISNIL, #0 - | mov RC, #16 // 2 results: false + error message. - | ldr BASE, L->base - | ldr GL, L->glref // Setup pointer to global state. - | mov_false TMP0 - | sub RA, BASE, #8 // Results start at BASE-8. - | ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame. - | str TMP0, [BASE, #-8] // Prepend false to error message. - | st_vmstate ST_INTERP - | b ->vm_returnc - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | // CARG1 = L - | mov CARG2, #LUA_MINSTACK - | b >2 - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | add RC, BASE, RC - | sub RA, RA, BASE - | mov CARG1, L - | stp BASE, RC, L->base - | add PC, PC, #4 // Must point after first instruction. - | lsr CARG2, RA, #3 - |2: - | // L->base = new base, L->top = top - | str PC, SAVE_PC - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldp BASE, RC, L->base - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | sub NARGS8:RC, RC, BASE - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mov L, CARG1 - | ldr GL, L->glref // Setup pointer to global state. - | mov BASE, CARG2 - | str L, SAVE_L - | mov PC, #FRAME_CP - | str wzr, SAVE_NRES - | add TMP0, sp, #CFRAME_RESUME - | ldrb TMP1w, L->status - | str wzr, SAVE_ERRF - | str L, SAVE_PC // Any value outside of bytecode is ok. - | str xzr, SAVE_CFRAME - | str TMP0, L->cframe - | cbz TMP1w, >3 - | - | // Resume after yield (like a return). - | str L, GL->cur_L - | mov RA, BASE - | ldp BASE, CARG1, L->base - | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48 - | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16 - | ldr PC, [BASE, FRAME_PC] - | strb wzr, L->status - | movn TISNIL, #0 - | sub RC, CARG1, BASE - | ands CARG1, PC, #FRAME_TYPE - | add RC, RC, #8 - | st_vmstate ST_INTERP - | str RCw, SAVE_MULTRES - | beq ->BC_RET_Z - | b ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | mov PC, #FRAME_CP - | str CARG4w, SAVE_ERRF - | b >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | mov PC, #FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | ldr RC, L:CARG1->cframe - | str CARG3w, SAVE_NRES - | mov L, CARG1 - | str CARG1, SAVE_L - | ldr GL, L->glref // Setup pointer to global state. - | mov BASE, CARG2 - | str CARG1, SAVE_PC // Any value outside of bytecode is ok. - | add TMP0, sp, #0 - | str RC, SAVE_CFRAME - | str TMP0, L->cframe // Add our C frame to cframe chain. - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | str L, GL->cur_L - | ldp RB, CARG1, L->base // RB = old base (for vmeta_call). - | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48 - | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16 - | add PC, PC, BASE - | movn TISNIL, #0 - | sub PC, PC, RB // PC = frame delta + frame type - | sub NARGS8:RC, CARG1, BASE - | st_vmstate ST_INTERP - | - |->vm_call_dispatch: - | // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC - | ldr CARG3, [BASE, FRAME_FUNC] - | checkfunc CARG3, ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, CARG3 = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mov L, CARG1 - | ldr RA, L:CARG1->stack - | str CARG1, SAVE_L - | ldr GL, L->glref // Setup pointer to global state. - | ldr RB, L->top - | str CARG1, SAVE_PC // Any value outside of bytecode is ok. - | ldr RC, L->cframe - | sub RA, RA, RB // Compute -savestack(L, L->top). - | str RAw, SAVE_NRES // Neg. delta means cframe w/o frame. - | str wzr, SAVE_ERRF // No error function. - | add TMP0, sp, #0 - | str RC, SAVE_CFRAME - | str TMP0, L->cframe // Add our C frame to cframe chain. - | str L, GL->cur_L - | blr CARG4 // (lua_State *L, lua_CFunction func, void *ud) - | mov BASE, CRET1 - | mov PC, #FRAME_CP - | cbnz BASE, <3 // Else continue with the call. - | b ->vm_leave_cp // No base? Just remove C frame. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RC = (nresults+1)*8 - | ldr LFUNC:CARG3, [RB, FRAME_FUNC] - | ldr CARG1, [BASE, #-32] // Get continuation. - | mov CARG4, BASE - | mov BASE, RB // Restore caller BASE. - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - |.if FFI - | cmp CARG1, #1 - |.endif - | ldr PC, [CARG4, #-24] // Restore PC from [cont|PC]. - | add TMP0, RA, RC - | str TISNIL, [TMP0, #-8] // Ensure one valid arg. - |.if FFI - | bls >1 - |.endif - | ldr CARG3, LFUNC:CARG3->pc - | ldr KBASE, [CARG3, #PC2PROTO(k)] - | // BASE = base, RA = resultptr, CARG4 = meta base - | br CARG1 - | - |.if FFI - |1: - | beq ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - | sub CARG4, CARG4, #32 - | sub RC, CARG4, BASE - | b ->vm_call_tail - |.endif - | - |->cont_cat: // RA = resultptr, CARG4 = meta base - | ldr INSw, [PC, #-4] - | sub CARG2, CARG4, #32 - | ldr TMP0, [RA] - | str BASE, L->base - | decode_RB RB, INS - | decode_RA RA, INS - | add TMP1, BASE, RB, lsl #3 - | subs TMP1, CARG2, TMP1 - | beq >1 - | str TMP0, [CARG2] - | lsr CARG3, TMP1, #3 - | b ->BC_CAT_Z - | - |1: - | str TMP0, [BASE, RA, lsl #3] - | b ->cont_nop - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | movn CARG4, #~LJ_TSTR - | add CARG2, BASE, RB, lsl #3 - | add CARG4, STR:RC, CARG4, lsl #47 - | b >2 - | - |->vmeta_tgets: - | movk CARG2, #(LJ_TTAB>>1)&0xffff, lsl #48 - | str CARG2, GL->tmptv - | add CARG2, GL, #offsetof(global_State, tmptv) - |2: - | add CARG3, sp, TMPDofs - | str CARG4, TMPD - | b >1 - | - |->vmeta_tgetb: // RB = table, RC = index - | add RC, RC, TISNUM - | add CARG2, BASE, RB, lsl #3 - | add CARG3, sp, TMPDofs - | str RC, TMPD - | b >1 - | - |->vmeta_tgetv: // RB = table, RC = key - | add CARG2, BASE, RB, lsl #3 - | add CARG3, BASE, RC, lsl #3 - |1: - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cbz CRET1, >3 - | ldr TMP0, [CRET1] - | str TMP0, [BASE, RA, lsl #3] - | ins_next - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | sub TMP1, BASE, #FRAME_CONT - | ldr BASE, L->top - | mov NARGS8:RC, #16 // 2 args for func(t, k). - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | str PC, [BASE, #-24] // [cont|PC] - | sub PC, BASE, TMP1 - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | b ->vm_call_dispatch_f - | - |->vmeta_tgetr: - | sxtw CARG2, TMP1w - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | mov TMP0, TISNIL - | cbz CRET1, ->BC_TGETR_Z - | ldr TMP0, [CRET1] - | b ->BC_TGETR_Z - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | movn CARG4, #~LJ_TSTR - | add CARG2, BASE, RB, lsl #3 - | add CARG4, STR:RC, CARG4, lsl #47 - | b >2 - | - |->vmeta_tsets: - | movk CARG2, #(LJ_TTAB>>1)&0xffff, lsl #48 - | str CARG2, GL->tmptv - | add CARG2, GL, #offsetof(global_State, tmptv) - |2: - | add CARG3, sp, TMPDofs - | str CARG4, TMPD - | b >1 - | - |->vmeta_tsetb: // RB = table, RC = index - | add RC, RC, TISNUM - | add CARG2, BASE, RB, lsl #3 - | add CARG3, sp, TMPDofs - | str RC, TMPD - | b >1 - | - |->vmeta_tsetv: - | add CARG2, BASE, RB, lsl #3 - | add CARG3, BASE, RC, lsl #3 - |1: - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | ldr TMP0, [BASE, RA, lsl #3] - | cbz CRET1, >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | str TMP0, [CRET1] - | ins_next - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | sub TMP1, BASE, #FRAME_CONT - | ldr BASE, L->top - | mov NARGS8:RC, #24 // 3 args for func(t, k, v). - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | str TMP0, [BASE, #16] // Copy value to third argument. - | str PC, [BASE, #-24] // [cont|PC] - | sub PC, BASE, TMP1 - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | b ->vm_call_dispatch_f - | - |->vmeta_tsetr: - | sxtw CARG3, TMP1w - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - | // Returns TValue *. - | b ->BC_TSETR_Z - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | add CARG2, BASE, RA, lsl #3 - | sub PC, PC, #4 - | add CARG3, BASE, RC, lsl #3 - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | uxtb CARG4w, INSw - | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // Returns 0/1 or TValue * (metamethod). - |3: - | cmp CRET1, #1 - | bhi ->vmeta_binop - |4: - | ldrh RBw, [PC, # OFS_RD] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | sub RB, RB, #0x20000 - | csel PC, PC, RB, lo - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | ldr INSw, [PC, #-4] - | ldr TMP0, [RA] - | decode_RA TMP1, INS - | str TMP0, [BASE, TMP1, lsl #3] - | b ->cont_nop - | - |->cont_condt: // RA = resultptr - | ldr TMP0, [RA] - | mov_true TMP1 - | cmp TMP1, TMP0 // Branch if result is true. - | b <4 - | - |->cont_condf: // RA = resultptr - | ldr TMP0, [RA] - | mov_false TMP1 - | cmp TMP0, TMP1 // Branch if result is false. - | b <4 - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | and TAB:CARG3, CARG3, #LJ_GCVMASK - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - | - |->vmeta_equal_cd: - |.if FFI - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | mov CARG2, INS - | str PC, SAVE_PC - | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |.endif - | - |->vmeta_istype: - | sub PC, PC, #4 - | str BASE, L->base - | mov CARG1, L - | mov CARG2, RA - | mov CARG3, RC - | str PC, SAVE_PC - | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - | b ->cont_nop - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vn: - | add CARG3, BASE, RB, lsl #3 - | add CARG4, KBASE, RC, lsl #3 - | b >1 - | - |->vmeta_arith_nv: - | add CARG4, BASE, RB, lsl #3 - | add CARG3, KBASE, RC, lsl #3 - | b >1 - | - |->vmeta_unm: - | add CARG3, BASE, RC, lsl #3 - | mov CARG4, CARG3 - | b >1 - | - |->vmeta_arith_vv: - | add CARG3, BASE, RB, lsl #3 - | add CARG4, BASE, RC, lsl #3 - |1: - | uxtb CARG5w, INSw - | add CARG2, BASE, RA, lsl #3 - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // Returns NULL (finished) or TValue * (metamethod). - | cbz CRET1, ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | sub TMP1, CRET1, BASE - | str PC, [CRET1, #-24] // [cont|PC] - | add PC, TMP1, #FRAME_CONT - | mov BASE, CRET1 - | mov NARGS8:RC, #16 // 2 args for func(o1, o2). - | b ->vm_call_dispatch - | - |->vmeta_len: - | add CARG2, BASE, RC, lsl #3 -#if LJ_52 - | mov TAB:RC, TAB:CARG1 // Save table (ignored for other types). -#endif - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_len // (lua_State *L, TValue *o) - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | cbnz CRET1, ->vmeta_binop // Binop call for compatibility. - | mov TAB:CARG1, TAB:RC - | b ->BC_LEN_Z -#else - | b ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // RB = old base, BASE = new base, RC = nargs*8 - | mov CARG1, L - | str RB, L->base // This is the callers base! - | sub CARG2, BASE, #16 - | str PC, SAVE_PC - | add CARG3, BASE, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here. - | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | mov CARG1, L - | str BASE, L->base - | sub CARG2, RA, #16 - | str PC, SAVE_PC - | add CARG3, RA, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | ldr TMP1, [RA, FRAME_FUNC] // Guaranteed to be a function here. - | ldr PC, [BASE, FRAME_PC] - | add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now. - | and LFUNC:CARG3, TMP1, #LJ_GCVMASK - | b ->BC_CALLT2_Z - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mov CARG1, L - | str BASE, L->base - | mov CARG2, RA - | str PC, SAVE_PC - | bl extern lj_meta_for // (lua_State *L, TValue *base) - | ldr INSw, [PC, #-4] - |.if JIT - | uxtb TMP0w, INSw - |.endif - | decode_RA RA, INS - | decode_RD RC, INS - |.if JIT - | cmp TMP0, #BC_JFORI - | beq =>BC_JFORI - |.endif - | b =>BC_FORI - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | ldr CARG1, [BASE] - | cmp NARGS8:RC, #8 - | blo ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | ldp CARG1, CARG2, [BASE] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - | .ffunc name - | ldr CARG1, [BASE] - | cmp NARGS8:RC, #8 - | ldr FARG1, [BASE] - | blo ->fff_fallback - | checknum CARG1, ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name - | .ffunc name - | ldp CARG1, CARG2, [BASE] - | cmp NARGS8:RC, #16 - | ldp FARG1, FARG2, [BASE] - | blo ->fff_fallback - | checknum CARG1, ->fff_fallback - | checknum CARG2, ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses CARG1 and CARG2. - |.macro ffgccheck - | ldp CARG1, CARG2, GL->gc.total // Assumes threshold follows total. - | cmp CARG1, CARG2 - | blt >1 - | bl ->fff_gcstep - |1: - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | ldr PC, [BASE, FRAME_PC] - | mov_false TMP1 - | cmp CARG1, TMP1 - | bhs ->fff_fallback - | str CARG1, [BASE, #-16] - | sub RB, BASE, #8 - | subs RA, NARGS8:RC, #8 - | add RC, NARGS8:RC, #8 // Compute (nresults+1)*8. - | cbz RA, ->fff_res // Done if exactly 1 argument. - |1: - | ldr CARG1, [RB, #16] - | sub RA, RA, #8 - | str CARG1, [RB], #8 - | cbnz RA, <1 - | b ->fff_res - | - |.ffunc_1 type - | mov TMP0, #~LJ_TISNUM - | asr ITYPE, CARG1, #47 - | cmn ITYPE, #~LJ_TISNUM - | csinv TMP1, TMP0, ITYPE, lo - | add TMP1, TMP1, #offsetof(GCfuncC, upvalue)/8 - | ldr CARG1, [CFUNC:CARG3, TMP1, lsl #3] - | b ->fff_restv - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | asr ITYPE, CARG1, #47 - | cmn ITYPE, #-LJ_TTAB - | ccmn ITYPE, #-LJ_TUDATA, #4, ne - | and TAB:CARG1, CARG1, #LJ_GCVMASK - | bne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | ldr TAB:RB, TAB:CARG1->metatable - |2: - | mov CARG1, TISNIL - | ldr STR:RC, GL->gcroot[GCROOT_MMNAME+MM_metatable] - | cbz TAB:RB, ->fff_restv - | ldr TMP1w, TAB:RB->hmask - | ldr TMP2w, STR:RC->sid - | ldr NODE:CARG3, TAB:RB->node - | and TMP1w, TMP1w, TMP2w // idx = str->sid & tab->hmask - | add TMP1, TMP1, TMP1, lsl #1 - | movn CARG4, #~LJ_TSTR - | add NODE:CARG3, NODE:CARG3, TMP1, lsl #3 // node = tab->node + idx*3*8 - | add CARG4, STR:RC, CARG4, lsl #47 // Tagged key to look for. - |3: // Rearranged logic, because we expect _not_ to find the key. - | ldp CARG1, TMP0, NODE:CARG3->val - | ldr NODE:CARG3, NODE:CARG3->next - | cmp TMP0, CARG4 - | beq >5 - | cbnz NODE:CARG3, <3 - |4: - | mov CARG1, RB // Use metatable as default result. - | movk CARG1, #(LJ_TTAB>>1)&0xffff, lsl #48 - | b ->fff_restv - |5: - | cmp TMP0, TISNIL - | bne ->fff_restv - | b <4 - | - |6: - | movn TMP0, #~LJ_TISNUM - | cmp ITYPE, TMP0 - | csel ITYPE, ITYPE, TMP0, hs - | sub TMP1, GL, ITYPE, lsl #3 - | ldr TAB:RB, [TMP1, #offsetof(global_State, gcroot[GCROOT_BASEMT])-8] - | b <2 - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | checktp TMP1, CARG1, LJ_TTAB, ->fff_fallback - | ldr TAB:TMP0, TAB:TMP1->metatable - | asr ITYPE, CARG2, #47 - | ldrb TMP2w, TAB:TMP1->marked - | cmn ITYPE, #-LJ_TTAB - | and TAB:CARG2, CARG2, #LJ_GCVMASK - | ccmp TAB:TMP0, #0, #0, eq - | bne ->fff_fallback - | str TAB:CARG2, TAB:TMP1->metatable - | tbz TMP2w, #2, ->fff_restv // isblack(table) - | barrierback TAB:TMP1, TMP2w, TMP0 - | b ->fff_restv - | - |.ffunc rawget - | ldr CARG2, [BASE] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | checktab CARG2, ->fff_fallback - | mov CARG1, L - | add CARG3, BASE, #8 - | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // Returns cTValue *. - | ldr CARG1, [CRET1] - | b ->fff_restv - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | ldr CARG1, [BASE] - | cmp NARGS8:RC, #8 - | bne ->fff_fallback - | checknumber CARG1, ->fff_fallback - | b ->fff_restv - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | asr ITYPE, CARG1, #47 - | cmn ITYPE, #-LJ_TSTR - | // A __tostring method in the string base metatable is ignored. - | beq ->fff_restv - | // Handle numbers inline, unless a number base metatable is present. - | ldr TMP1, GL->gcroot[GCROOT_BASEMT_NUM] - | str BASE, L->base - | cmn ITYPE, #-LJ_TISNUM - | ccmp TMP1, #0, #0, ls - | str PC, SAVE_PC // Redundant (but a defined value). - | bne ->fff_fallback - | ffgccheck - | mov CARG1, L - | mov CARG2, BASE - | bl extern lj_strfmt_number // (lua_State *L, cTValue *o) - | // Returns GCstr *. - | movn TMP1, #~LJ_TSTR - | ldr BASE, L->base - | add CARG1, CARG1, TMP1, lsl #47 - | b ->fff_restv - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | checktp CARG1, LJ_TTAB, ->fff_fallback - | str TISNIL, [BASE, NARGS8:RC] // Set missing 2nd arg to nil. - | ldr PC, [BASE, FRAME_PC] - | add CARG2, BASE, #8 - | sub CARG3, BASE, #16 - | bl extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - | // Returns 1=found, 0=end, -1=error. - | mov RC, #(2+1)*8 - | tbnz CRET1w, #31, ->fff_fallback // Invalid key. - | cbnz CRET1, ->fff_res // Found key/value. - | // End of traversal: return nil. - | str TISNIL, [BASE, #-16] - | b ->fff_res1 - | - |.ffunc_1 pairs - | checktp TMP1, CARG1, LJ_TTAB, ->fff_fallback -#if LJ_52 - | ldr TAB:CARG2, TAB:TMP1->metatable -#endif - | ldr CFUNC:CARG4, CFUNC:CARG3->upvalue[0] - | ldr PC, [BASE, FRAME_PC] -#if LJ_52 - | cbnz TAB:CARG2, ->fff_fallback -#endif - | mov RC, #(3+1)*8 - | stp CARG1, TISNIL, [BASE, #-8] - | str CFUNC:CARG4, [BASE, #-16] - | b ->fff_res - | - |.ffunc_2 ipairs_aux - | checktab CARG1, ->fff_fallback - | checkint CARG2, ->fff_fallback - | ldr TMP1w, TAB:CARG1->asize - | ldr CARG3, TAB:CARG1->array - | ldr TMP0w, TAB:CARG1->hmask - | add CARG2w, CARG2w, #1 - | cmp CARG2w, TMP1w - | ldr PC, [BASE, FRAME_PC] - | add TMP2, CARG2, TISNUM - | mov RC, #(0+1)*8 - | str TMP2, [BASE, #-16] - | bhs >2 // Not in array part? - | ldr TMP0, [CARG3, CARG2, lsl #3] - |1: - | mov TMP1, #(2+1)*8 - | cmp TMP0, TISNIL - | str TMP0, [BASE, #-8] - | csel RC, RC, TMP1, eq - | b ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | cbz TMP0w, ->fff_res - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | cbz CRET1, ->fff_res - | ldr TMP0, [CRET1] - | b <1 - | - |.ffunc_1 ipairs - | checktp TMP1, CARG1, LJ_TTAB, ->fff_fallback -#if LJ_52 - | ldr TAB:CARG2, TAB:TMP1->metatable -#endif - | ldr CFUNC:CARG4, CFUNC:CARG3->upvalue[0] - | ldr PC, [BASE, FRAME_PC] -#if LJ_52 - | cbnz TAB:CARG2, ->fff_fallback -#endif - | mov RC, #(3+1)*8 - | stp CARG1, TISNUM, [BASE, #-8] - | str CFUNC:CARG4, [BASE, #-16] - | b ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | cmp NARGS8:RC, #8 - | ldrb TMP0w, GL->hookmask - | blo ->fff_fallback - | sub NARGS8:RC, NARGS8:RC, #8 - | mov RB, BASE - | add BASE, BASE, #16 - | ubfx TMP0w, TMP0w, #HOOK_ACTIVE_SHIFT, #1 - | add PC, TMP0, #16+FRAME_PCALL - | beq ->vm_call_dispatch - |1: - | add TMP2, BASE, NARGS8:RC - |2: - | ldr TMP0, [TMP2, #-16] - | str TMP0, [TMP2, #-8]! - | cmp TMP2, BASE - | bne <2 - | b ->vm_call_dispatch - | - |.ffunc xpcall - | ldp CARG1, CARG2, [BASE] - | ldrb TMP0w, GL->hookmask - | subs NARGS8:TMP1, NARGS8:RC, #16 - | blo ->fff_fallback - | mov RB, BASE - | asr ITYPE, CARG2, #47 - | ubfx TMP0w, TMP0w, #HOOK_ACTIVE_SHIFT, #1 - | cmn ITYPE, #-LJ_TFUNC - | add PC, TMP0, #24+FRAME_PCALL - | bne ->fff_fallback // Traceback must be a function. - | mov NARGS8:RC, NARGS8:TMP1 - | add BASE, BASE, #24 - | stp CARG2, CARG1, [RB] // Swap function and traceback. - | cbz NARGS8:RC, ->vm_call_dispatch - | b <1 - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | checktp CARG1, LJ_TTHREAD, ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | ldr L:CARG1, CFUNC:CARG3->upvalue[0].gcr - | and L:CARG1, CARG1, #LJ_GCVMASK - |.endif - | ldr PC, [BASE, FRAME_PC] - | str BASE, L->base - | ldp RB, CARG2, L:CARG1->base - | ldrb TMP1w, L:CARG1->status - | add TMP0, CARG2, TMP1 - | str PC, SAVE_PC - | cmp TMP0, RB - | beq ->fff_fallback - | cmp TMP1, #LUA_YIELD - | add TMP0, CARG2, #8 - | csel CARG2, CARG2, TMP0, hs - | ldr CARG4, L:CARG1->maxstack - | add CARG3, CARG2, NARGS8:RC - | ldr RB, L:CARG1->cframe - | ccmp CARG3, CARG4, #2, ls - | ccmp RB, #0, #2, ls - | bhi ->fff_fallback - |.if resume - | sub CARG3, CARG3, #8 // Keep resumed thread in stack for GC. - | add BASE, BASE, #8 - | sub NARGS8:RC, NARGS8:RC, #8 - |.endif - | str CARG3, L:CARG1->top - | str BASE, L->top - | cbz NARGS8:RC, >3 - |2: // Move args to coroutine. - | ldr TMP0, [BASE, RB] - | cmp RB, NARGS8:RC - | str TMP0, [CARG2, RB] - | add RB, RB, #8 - | bne <2 - |3: - | mov CARG3, #0 - | mov L:RA, L:CARG1 - | mov CARG4, #0 - | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | // Returns thread status. - |4: - | ldp CARG3, CARG4, L:RA->base - | cmp CRET1, #LUA_YIELD - | ldr BASE, L->base - | str L, GL->cur_L - | st_vmstate ST_INTERP - | bhi >8 - | sub RC, CARG4, CARG3 - | ldr CARG1, L->maxstack - | add CARG2, BASE, RC - | cbz RC, >6 // No results? - | cmp CARG2, CARG1 - | mov RB, #0 - | bhi >9 // Need to grow stack? - | - | sub CARG4, RC, #8 - | str CARG3, L:RA->top // Clear coroutine stack. - |5: // Move results from coroutine. - | ldr TMP0, [CARG3, RB] - | cmp RB, CARG4 - | str TMP0, [BASE, RB] - | add RB, RB, #8 - | bne <5 - |6: - |.if resume - | mov_true TMP1 - | add RC, RC, #16 - |7: - | str TMP1, [BASE, #-8] // Prepend true/false to results. - | sub RA, BASE, #8 - |.else - | mov RA, BASE - | add RC, RC, #8 - |.endif - | ands CARG1, PC, #FRAME_TYPE - | str PC, SAVE_PC - | str RCw, SAVE_MULTRES - | beq ->BC_RET_Z - | b ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | ldr TMP0, [CARG4, #-8]! - | mov_false TMP1 - | mov RC, #(2+1)*8 - | str CARG4, L:RA->top // Remove error from coroutine stack. - | str TMP0, [BASE] // Copy error message. - | b <7 - |.else - | mov CARG1, L - | mov CARG2, L:RA - | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - | // Never returns. - |.endif - | - |9: // Handle stack expansion on return from yield. - | mov CARG1, L - | lsr CARG2, RC, #3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | mov CRET1, #0 - | b <4 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | ldr TMP0, L->cframe - | add TMP1, BASE, NARGS8:RC - | mov CRET1, #LUA_YIELD - | stp BASE, TMP1, L->base - | tbz TMP0, #0, ->fff_fallback - | str xzr, L->cframe - | strb CRET1w, L->status - | b ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.macro math_round, func, round - | .ffunc math_ .. func - | ldr CARG1, [BASE] - | cmp NARGS8:RC, #8 - | ldr d0, [BASE] - | blo ->fff_fallback - | cmp TISNUMhi, CARG1, lsr #32 - | beq ->fff_restv - | blo ->fff_fallback - | round d0, d0 - | b ->fff_resn - |.endmacro - | - | math_round floor, frintm - | math_round ceil, frintp - | - |.ffunc_1 math_abs - | checknumber CARG1, ->fff_fallback - | and CARG1, CARG1, #U64x(7fffffff,ffffffff) - | bne ->fff_restv - | eor CARG2w, CARG1w, CARG1w, asr #31 - | movz CARG3, #0x41e0, lsl #48 // 2^31. - | subs CARG1w, CARG2w, CARG1w, asr #31 - | add CARG1, CARG1, TISNUM - | csel CARG1, CARG1, CARG3, pl - | // Fallthrough. - | - |->fff_restv: - | // CARG1 = TValue result. - | ldr PC, [BASE, FRAME_PC] - | str CARG1, [BASE, #-16] - |->fff_res1: - | // PC = return. - | mov RC, #(1+1)*8 - |->fff_res: - | // RC = (nresults+1)*8, PC = return. - | ands CARG1, PC, #FRAME_TYPE - | str RCw, SAVE_MULTRES - | sub RA, BASE, #16 - | bne ->vm_return - | ldr INSw, [PC, #-4] - | decode_RB RB, INS - |5: - | cmp RC, RB, lsl #3 // More results expected? - | blo >6 - | decode_RA TMP1, INS - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | sub BASE, RA, TMP1, lsl #3 - | ins_next - | - |6: // Fill up results with nil. - | add TMP1, RA, RC - | add RC, RC, #8 - | str TISNIL, [TMP1, #-8] - | b <5 - | - |.macro math_extern, func - | .ffunc_n math_ .. func - | bl extern func - | b ->fff_resn - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - | bl extern func - | b ->fff_resn - |.endmacro - | - |.ffunc_n math_sqrt - | fsqrt d0, d0 - |->fff_resn: - | ldr PC, [BASE, FRAME_PC] - | str d0, [BASE, #-16] - | b ->fff_res1 - | - |.ffunc math_log - | ldr CARG1, [BASE] - | cmp NARGS8:RC, #8 - | ldr FARG1, [BASE] - | bne ->fff_fallback // Need exactly 1 argument. - | checknum CARG1, ->fff_fallback - | bl extern log - | b ->fff_resn - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.ffunc_2 math_ldexp - | ldr FARG1, [BASE] - | checknum CARG1, ->fff_fallback - | checkint CARG2, ->fff_fallback - | sxtw CARG1, CARG2w - | bl extern ldexp // (double x, int exp) - | b ->fff_resn - | - |.ffunc_n math_frexp - | add CARG1, sp, TMPDofs - | bl extern frexp - | ldr CARG2w, TMPD - | ldr PC, [BASE, FRAME_PC] - | str d0, [BASE, #-16] - | mov RC, #(2+1)*8 - | add CARG2, CARG2, TISNUM - | str CARG2, [BASE, #-8] - | b ->fff_res - | - |.ffunc_n math_modf - | sub CARG1, BASE, #16 - | ldr PC, [BASE, FRAME_PC] - | bl extern modf - | mov RC, #(2+1)*8 - | str d0, [BASE, #-8] - | b ->fff_res - | - |.macro math_minmax, name, cond, fcond - | .ffunc_1 name - | add RB, BASE, RC - | add RA, BASE, #8 - | checkint CARG1, >4 - |1: // Handle integers. - | ldr CARG2, [RA] - | cmp RA, RB - | bhs ->fff_restv - | checkint CARG2, >3 - | cmp CARG1w, CARG2w - | add RA, RA, #8 - | csel CARG1, CARG2, CARG1, cond - | b <1 - |3: // Convert intermediate result to number and continue below. - | scvtf d0, CARG1w - | blo ->fff_fallback - | ldr d1, [RA] - | b >6 - | - |4: - | ldr d0, [BASE] - | blo ->fff_fallback - |5: // Handle numbers. - | ldr CARG2, [RA] - | ldr d1, [RA] - | cmp RA, RB - | bhs ->fff_resn - | checknum CARG2, >7 - |6: - | fcmp d0, d1 - | add RA, RA, #8 - | fcsel d0, d1, d0, fcond - | b <5 - |7: // Convert integer to number and continue above. - | scvtf d1, CARG2w - | blo ->fff_fallback - | b <6 - |.endmacro - | - | math_minmax math_min, gt, pl - | math_minmax math_max, lt, le - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | ldp PC, CARG1, [BASE, FRAME_PC] - | cmp NARGS8:RC, #8 - | asr ITYPE, CARG1, #47 - | ccmn ITYPE, #-LJ_TSTR, #0, eq - | and STR:CARG1, CARG1, #LJ_GCVMASK - | bne ->fff_fallback - | ldrb TMP0w, STR:CARG1[1] // Access is always ok (NUL at end). - | ldr CARG3w, STR:CARG1->len - | add TMP0, TMP0, TISNUM - | str TMP0, [BASE, #-16] - | mov RC, #(0+1)*8 - | cbz CARG3, ->fff_res - | b ->fff_res1 - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | ldp PC, CARG1, [BASE, FRAME_PC] - | cmp CARG1w, #255 - | ccmp NARGS8:RC, #8, #0, ls // Need exactly 1 argument. - | bne ->fff_fallback - | checkint CARG1, ->fff_fallback - | mov CARG3, #1 - | // Point to the char inside the integer in the stack slot. - |.if ENDIAN_LE - | mov CARG2, BASE - |.else - | add CARG2, BASE, #7 - |.endif - |->fff_newstr: - | // CARG2 = str, CARG3 = len. - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_str_new // (lua_State *L, char *str, size_t l) - |->fff_resstr: - | // Returns GCstr *. - | ldr BASE, L->base - | movn TMP1, #~LJ_TSTR - | add CARG1, CARG1, TMP1, lsl #47 - | b ->fff_restv - | - |.ffunc string_sub - | ffgccheck - | ldr CARG1, [BASE] - | ldr CARG3, [BASE, #16] - | cmp NARGS8:RC, #16 - | movn RB, #0 - | beq >1 - | blo ->fff_fallback - | checkint CARG3, ->fff_fallback - | sxtw RB, CARG3w - |1: - | ldr CARG2, [BASE, #8] - | checkstr CARG1, ->fff_fallback - | ldr TMP1w, STR:CARG1->len - | checkint CARG2, ->fff_fallback - | sxtw CARG2, CARG2w - | // CARG1 = str, TMP1 = str->len, CARG2 = start, RB = end - | add TMP2, RB, TMP1 - | cmp RB, #0 - | add TMP0, CARG2, TMP1 - | csinc RB, RB, TMP2, ge // if (end < 0) end += len+1 - | cmp CARG2, #0 - | csinc CARG2, CARG2, TMP0, ge // if (start < 0) start += len+1 - | cmp RB, #0 - | csel RB, RB, xzr, ge // if (end < 0) end = 0 - | cmp CARG2, #1 - | csinc CARG2, CARG2, xzr, ge // if (start < 1) start = 1 - | cmp RB, TMP1 - | csel RB, RB, TMP1, le // if (end > len) end = len - | add CARG1, STR:CARG1, #sizeof(GCstr)-1 - | subs CARG3, RB, CARG2 // len = end - start - | add CARG2, CARG1, CARG2 - | add CARG3, CARG3, #1 // len += 1 - | bge ->fff_newstr - | add STR:CARG1, GL, #offsetof(global_State, strempty) - | movn TMP1, #~LJ_TSTR - | add CARG1, CARG1, TMP1, lsl #47 - | b ->fff_restv - | - |.macro ffstring_op, name - | .ffunc string_ .. name - | ffgccheck - | ldr CARG2, [BASE] - | cmp NARGS8:RC, #8 - | asr ITYPE, CARG2, #47 - | ccmn ITYPE, #-LJ_TSTR, #0, hs - | and STR:CARG2, CARG2, #LJ_GCVMASK - | bne ->fff_fallback - | ldr TMP0, GL->tmpbuf.b - | add SBUF:CARG1, GL, #offsetof(global_State, tmpbuf) - | str BASE, L->base - | str PC, SAVE_PC - | str L, GL->tmpbuf.L - | str TMP0, GL->tmpbuf.w - | bl extern lj_buf_putstr_ .. name - | bl extern lj_buf_tostr - | b ->fff_resstr - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |// FP number to bit conversion for soft-float. Clobbers CARG1-CARG3 - |->vm_tobit_fb: - | bls ->fff_fallback - | add CARG2, CARG1, CARG1 - | mov CARG3, #1076 - | sub CARG3, CARG3, CARG2, lsr #53 - | cmp CARG3, #53 - | bhi >1 - | and CARG2, CARG2, #U64x(001fffff,ffffffff) - | orr CARG2, CARG2, #U64x(00200000,00000000) - | cmp CARG1, #0 - | lsr CARG2, CARG2, CARG3 - | cneg CARG1w, CARG2w, mi - | br lr - |1: - | mov CARG1w, #0 - | br lr - | - |.macro .ffunc_bit, name - | .ffunc_1 bit_..name - | adr lr, >1 - | checkint CARG1, ->vm_tobit_fb - |1: - |.endmacro - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | mov RA, #8 - | mov TMP0w, CARG1w - | adr lr, >2 - |1: - | ldr CARG1, [BASE, RA] - | cmp RA, NARGS8:RC - | add RA, RA, #8 - | bge >9 - | checkint CARG1, ->vm_tobit_fb - |2: - | ins TMP0w, TMP0w, CARG1w - | b <1 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, orr - |.ffunc_bit_op bxor, eor - | - |.ffunc_bit tobit - | mov TMP0w, CARG1w - |9: // Label reused by .ffunc_bit_op users. - | add CARG1, TMP0, TISNUM - | b ->fff_restv - | - |.ffunc_bit bswap - | rev TMP0w, CARG1w - | add CARG1, TMP0, TISNUM - | b ->fff_restv - | - |.ffunc_bit bnot - | mvn TMP0w, CARG1w - | add CARG1, TMP0, TISNUM - | b ->fff_restv - | - |.macro .ffunc_bit_sh, name, ins, shmod - | .ffunc bit_..name - | ldp TMP0, CARG1, [BASE] - | cmp NARGS8:RC, #16 - | blo ->fff_fallback - | adr lr, >1 - | checkint CARG1, ->vm_tobit_fb - |1: - |.if shmod == 0 - | mov TMP1, CARG1 - |.else - | neg TMP1, CARG1 - |.endif - | mov CARG1, TMP0 - | adr lr, >2 - | checkint CARG1, ->vm_tobit_fb - |2: - | ins TMP0w, CARG1w, TMP1w - | add CARG1, TMP0, TISNUM - | b ->fff_restv - |.endmacro - | - |.ffunc_bit_sh lshift, lsl, 0 - |.ffunc_bit_sh rshift, lsr, 0 - |.ffunc_bit_sh arshift, asr, 0 - |.ffunc_bit_sh rol, ror, 1 - |.ffunc_bit_sh ror, ror, 0 - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RC = nargs*8 - | ldp CFUNC:CARG3, PC, [BASE, FRAME_FUNC] // Fallback may overwrite PC. - | ldr TMP2, L->maxstack - | add TMP1, BASE, NARGS8:RC - | stp BASE, TMP1, L->base - | and CFUNC:CARG3, CARG3, #LJ_GCVMASK - | add TMP1, TMP1, #8*LUA_MINSTACK - | ldr CARG3, CFUNC:CARG3->f - | str PC, SAVE_PC // Redundant (but a defined value). - | cmp TMP1, TMP2 - | mov CARG1, L - | bhi >5 // Need to grow stack. - | blr CARG3 // (lua_State *L) - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | ldr BASE, L->base - | cmp CRET1w, #0 - | lsl RC, CRET1, #3 - | sub RA, BASE, #16 - | bgt ->fff_res // Returned nresults+1? - |1: // Returned 0 or -1: retry fast path. - | ldr CARG1, L->top - | ldr CFUNC:CARG3, [BASE, FRAME_FUNC] - | sub NARGS8:RC, CARG1, BASE - | bne ->vm_call_tail // Returned -1? - | and CFUNC:CARG3, CARG3, #LJ_GCVMASK - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | ands TMP0, PC, #FRAME_TYPE - | and TMP1, PC, #~FRAME_TYPEP - | bne >3 - | ldrb RAw, [PC, #-4+OFS_RA] - | lsl RA, RA, #3 - | add TMP1, RA, #16 - |3: - | sub RB, BASE, TMP1 - | b ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | mov CARG2, #LUA_MINSTACK - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldr BASE, L->base - | cmp CARG1, CARG1 // Set zero-flag to force retry. - | b <1 - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | add CARG2, BASE, NARGS8:RC // Calculate L->top. - | mov RA, lr - | stp BASE, CARG2, L->base - | str PC, SAVE_PC // Redundant (but a defined value). - | mov CARG1, L - | bl extern lj_gc_step // (lua_State *L) - | ldp BASE, CARG2, L->base - | ldr CFUNC:CARG3, [BASE, FRAME_FUNC] - | mov lr, RA // Help return address predictor. - | sub NARGS8:RC, CARG2, BASE // Calculate nargs*8. - | and CFUNC:CARG3, CARG3, #LJ_GCVMASK - | ret - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | ldrb CARG1w, GL->hookmask - | tst CARG1, #HOOK_VMEVENT // No recording while in vmevent. - | bne >5 - | // Decrement the hookcount for consistency, but always do the call. - | ldr CARG2w, GL->hookcount - | tst CARG1, #HOOK_ACTIVE - | bne >1 - | sub CARG2w, CARG2w, #1 - | tst CARG1, #LUA_MASKLINE|LUA_MASKCOUNT - | beq >1 - | str CARG2w, GL->hookcount - | b >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | ldrb TMP2w, GL->hookmask - | tbz TMP2w, #HOOK_ACTIVE_SHIFT, >1 // Hook already active? - |5: // Re-dispatch to static ins. - | ldr TMP0, [TMP1, #GG_G2DISP+GG_DISP2STATIC] - | br TMP0 - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | ldrb TMP2w, GL->hookmask - | ldr TMP3w, GL->hookcount - | tbnz TMP2w, #HOOK_ACTIVE_SHIFT, <5 // Hook already active? - | tst TMP2w, #LUA_MASKLINE|LUA_MASKCOUNT - | beq <5 - | sub TMP3w, TMP3w, #1 - | str TMP3w, GL->hookcount - | cbz TMP3w, >1 - | tbz TMP2w, #LUA_HOOKLINE, <5 - |1: - | mov CARG1, L - | str BASE, L->base - | mov CARG2, PC - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | ldr BASE, L->base - |4: // Re-dispatch to static ins. - | ldr INSw, [PC, #-4] - | add TMP1, GL, INS, uxtb #3 - | decode_RA RA, INS - | ldr TMP0, [TMP1, #GG_G2DISP+GG_DISP2STATIC] - | decode_RD RC, INS - | br TMP0 - | - |->cont_hook: // Continue from hook yield. - | ldr CARG1, [CARG4, #-40] - | add PC, PC, #4 - | str CARG1w, SAVE_MULTRES // Restore MULTRES for *M ins. - | b <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Same as curr_topL(L). - | add CARG1, GL, #GG_G2DISP+GG_DISP2J - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | str PC, SAVE_PC - | ldr CARG3, LFUNC:CARG3->pc - | mov CARG2, PC - | str L, [GL, #GL_J(L)] - | ldrb CARG3w, [CARG3, #PC2PROTO(framesize)] - | str BASE, L->base - | add CARG3, BASE, CARG3, lsl #3 - | str CARG3, L->top - | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc) - | b <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mov CARG2, PC - |.if JIT - | b >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | orr CARG2, PC, #1 - |1: - |.endif - | add TMP1, BASE, NARGS8:RC - | str PC, SAVE_PC - | mov CARG1, L - | sub RA, RA, BASE - | stp BASE, TMP1, L->base - | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // Returns ASMFunction. - | ldp BASE, TMP1, L->base - | str xzr, SAVE_PC // Invalidate for subsequent line hook. - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | add RA, BASE, RA - | sub NARGS8:RC, TMP1, BASE - | ldr INSw, [PC, #-4] - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | br CRET1 - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // RA = resultptr, CARG4 = meta base - | ldr RBw, SAVE_MULTRES - | ldr INSw, [PC, #-4] - | ldr TRACE:CARG3, [CARG4, #-40] // Save previous trace. - | subs RB, RB, #8 - | decode_RA RC, INS // Call base. - | and CARG3, CARG3, #LJ_GCVMASK - | beq >2 - |1: // Move results down. - | ldr CARG1, [RA] - | add RA, RA, #8 - | subs RB, RB, #8 - | str CARG1, [BASE, RC, lsl #3] - | add RC, RC, #1 - | bne <1 - |2: - | decode_RA RA, INS - | decode_RB RB, INS - | add RA, RA, RB - |3: - | cmp RA, RC - | bhi >9 // More results wanted? - | - | ldrh RAw, TRACE:CARG3->traceno - | ldrh RCw, TRACE:CARG3->link - | cmp RCw, RAw - | beq ->cont_nop // Blacklisted. - | cmp RCw, #0 - | bne =>BC_JLOOP // Jump to stitched trace. - | - | // Stitch a new trace to the previous trace. - | mov CARG1, #GL_J(exitno) - | str RAw, [GL, CARG1] - | mov CARG1, #GL_J(L) - | str L, [GL, CARG1] - | str BASE, L->base - | add CARG1, GL, #GG_G2J - | mov CARG2, PC - | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc) - | ldr BASE, L->base - | b ->cont_nop - | - |9: // Fill up results with nil. - | str TISNIL, [BASE, RC, lsl #3] - | add RC, RC, #1 - | b <3 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | mov CARG1, L - | str BASE, L->base - | mov CARG2, PC - | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc) - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | ldr BASE, L->base - | sub PC, PC, #4 - | b ->cont_nop -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro savex_, a, b - | stp d..a, d..b, [sp, #a*8] - | stp x..a, x..b, [sp, #32*8+a*8] - |.endmacro - | - |->vm_exit_handler: - |.if JIT - | sub sp, sp, #(64*8) - | savex_, 0, 1 - | savex_, 2, 3 - | savex_, 4, 5 - | savex_, 6, 7 - | savex_, 8, 9 - | savex_, 10, 11 - | savex_, 12, 13 - | savex_, 14, 15 - | savex_, 16, 17 - | savex_, 18, 19 - | savex_, 20, 21 - | savex_, 22, 23 - | savex_, 24, 25 - | savex_, 26, 27 - | savex_, 28, 29 - | stp d30, d31, [sp, #30*8] - | ldr CARG1, [sp, #64*8] // Load original value of lr. - | add CARG3, sp, #64*8 // Recompute original value of sp. - | mv_vmstate CARG4w, EXIT - | stp xzr, CARG3, [sp, #62*8] // Store 0/sp in RID_LR/RID_SP. - | sub CARG1, CARG1, lr - | ldr L, GL->cur_L - | lsr CARG1, CARG1, #2 - | ldr BASE, GL->jit_base - | sub CARG1, CARG1, #2 - | ldr CARG2w, [lr] // Load trace number. - | st_vmstate CARG4w - |.if ENDIAN_BE - | rev32 CARG2, CARG2 - |.endif - | str BASE, L->base - | ubfx CARG2w, CARG2w, #5, #16 - | str CARG1w, [GL, #GL_J(exitno)] - | str CARG2w, [GL, #GL_J(parent)] - | str L, [GL, #GL_J(L)] - | str xzr, GL->jit_base - | add CARG1, GL, #GG_G2J - | mov CARG2, sp - | bl extern lj_trace_exit // (jit_State *J, ExitState *ex) - | // Returns MULTRES (unscaled) or negated error code. - | ldr CARG2, L->cframe - | ldr BASE, L->base - | and sp, CARG2, #CFRAME_RAWMASK - | ldr PC, SAVE_PC // Get SAVE_PC. - | str L, SAVE_L // Set SAVE_L (on-trace resume/yield). - | b >1 - |.endif - | - |->vm_exit_interp: - | // CARG1 = MULTRES or negated error code, BASE, PC and GL set. - |.if JIT - | ldr L, SAVE_L - |1: - | cmp CARG1w, #0 - | blt >9 // Check for error from exit. - | lsl RC, CARG1, #3 - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48 - | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16 - | movn TISNIL, #0 - | and LFUNC:CARG2, CARG2, #LJ_GCVMASK - | str RCw, SAVE_MULTRES - | str BASE, L->base - | ldr CARG2, LFUNC:CARG2->pc - | str xzr, GL->jit_base - | mv_vmstate CARG4w, INTERP - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | // Modified copy of ins_next which handles function header dispatch, too. - | ldrb RBw, [PC, # OFS_OP] - | ldr INSw, [PC], #4 - | st_vmstate CARG4w - | cmp RBw, #BC_FUNCC+2 // Fast function? - | add TMP1, GL, INS, uxtb #3 - | bhs >4 - |2: - | cmp RBw, #BC_FUNCF // Function header? - | add TMP0, GL, RB, uxtb #3 - | ldr RB, [TMP0, #GG_G2DISP] - | decode_RA RA, INS - | lsr TMP0, INS, #16 - | csel RC, TMP0, RC, lo - | blo >5 - | ldr CARG3, [BASE, FRAME_FUNC] - | sub RC, RC, #8 - | add RA, BASE, RA, lsl #3 // Yes: RA = BASE+framesize*8, RC = nargs*8 - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - |5: - | br RB - | - |4: // Check frame below fast function. - | ldr CARG1, [BASE, FRAME_PC] - | ands CARG2, CARG1, #FRAME_TYPE - | bne <2 // Trace stitching continuation? - | // Otherwise set KBASE for Lua function below fast function. - | ldr CARG3w, [CARG1, #-4] - | decode_RA CARG1, CARG3 - | sub CARG2, BASE, CARG1, lsl #3 - | ldr LFUNC:CARG3, [CARG2, #-32] - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | ldr CARG3, LFUNC:CARG3->pc - | ldr KBASE, [CARG3, #PC2PROTO(k)] - | b <2 - | - |9: // Rethrow error from the right C frame. - | neg CARG2w, CARG1w - | mov CARG1, L - | bl extern lj_err_trace // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - | // int lj_vm_modi(int dividend, int divisor); - |->vm_modi: - | eor CARG4w, CARG1w, CARG2w - | cmp CARG4w, #0 - | eor CARG3w, CARG1w, CARG1w, asr #31 - | eor CARG4w, CARG2w, CARG2w, asr #31 - | sub CARG3w, CARG3w, CARG1w, asr #31 - | sub CARG4w, CARG4w, CARG2w, asr #31 - | udiv CARG1w, CARG3w, CARG4w - | msub CARG1w, CARG1w, CARG4w, CARG3w - | ccmp CARG1w, #0, #4, mi - | sub CARG3w, CARG1w, CARG4w - | csel CARG1w, CARG1w, CARG3w, eq - | eor CARG3w, CARG1w, CARG2w - | cmp CARG3w, #0 - | cneg CARG1w, CARG1w, mi - | ret - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |.define NEXT_TAB, TAB:CARG1 - |.define NEXT_RES, CARG1 - |.define NEXT_IDX, CARG2w - |.define NEXT_LIM, CARG3w - |.define NEXT_TMP0, TMP0 - |.define NEXT_TMP0w, TMP0w - |.define NEXT_TMP1, TMP1 - |.define NEXT_TMP1w, TMP1w - |.define NEXT_RES_PTR, sp - |.define NEXT_RES_VAL, [sp] - |.define NEXT_RES_KEY, [sp, #8] - | - |// TValue *lj_vm_next(GCtab *t, uint32_t idx) - |// Next idx returned in CRET2w. - |->vm_next: - |.if JIT - | ldr NEXT_LIM, NEXT_TAB->asize - | ldr NEXT_TMP1, NEXT_TAB->array - |1: // Traverse array part. - | subs NEXT_TMP0w, NEXT_IDX, NEXT_LIM - | bhs >5 // Index points after array part? - | ldr NEXT_TMP0, [NEXT_TMP1, NEXT_IDX, uxtw #3] - | cmn NEXT_TMP0, #-LJ_TNIL - | cinc NEXT_IDX, NEXT_IDX, eq - | beq <1 // Skip holes in array part. - | str NEXT_TMP0, NEXT_RES_VAL - | movz NEXT_TMP0w, #(LJ_TISNUM>>1)&0xffff, lsl #16 - | stp NEXT_IDX, NEXT_TMP0w, NEXT_RES_KEY - | add NEXT_IDX, NEXT_IDX, #1 - | mov NEXT_RES, NEXT_RES_PTR - |4: - | ret - | - |5: // Traverse hash part. - | ldr NEXT_TMP1w, NEXT_TAB->hmask - | ldr NODE:NEXT_RES, NEXT_TAB->node - | add NEXT_TMP0w, NEXT_TMP0w, NEXT_TMP0w, lsl #1 - | add NEXT_LIM, NEXT_LIM, NEXT_TMP1w - | add NODE:NEXT_RES, NODE:NEXT_RES, NEXT_TMP0w, uxtw #3 - |6: - | cmp NEXT_IDX, NEXT_LIM - | bhi >9 - | ldr NEXT_TMP0, NODE:NEXT_RES->val - | cmn NEXT_TMP0, #-LJ_TNIL - | add NEXT_IDX, NEXT_IDX, #1 - | bne <4 - | // Skip holes in hash part. - | add NODE:NEXT_RES, NODE:NEXT_RES, #sizeof(Node) - | b <6 - | - |9: // End of iteration. Set the key to nil (not the value). - | movn NEXT_TMP0, #0 - | str NEXT_TMP0, NEXT_RES_KEY - | mov NEXT_RES, NEXT_RES_PTR - | ret - |.endif - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. - |// Saveregs already performed. Callback slot number in [sp], g in r12. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs - | ldr CTSTATE, GL:x10->ctype_state - | mov GL, x10 - | add x10, sp, # CFRAME_SPACE - | str w9, CTSTATE->cb.slot - | stp x0, x1, CTSTATE->cb.gpr[0] - | stp d0, d1, CTSTATE->cb.fpr[0] - | stp x2, x3, CTSTATE->cb.gpr[2] - | stp d2, d3, CTSTATE->cb.fpr[2] - | stp x4, x5, CTSTATE->cb.gpr[4] - | stp d4, d5, CTSTATE->cb.fpr[4] - | stp x6, x7, CTSTATE->cb.gpr[6] - | stp d6, d7, CTSTATE->cb.fpr[6] - | str x10, CTSTATE->cb.stack - | mov CARG1, CTSTATE - | str CTSTATE, SAVE_PC // Any value outside of bytecode is ok. - | mov CARG2, sp - | bl extern lj_ccallback_enter // (CTState *cts, void *cf) - | // Returns lua_State *. - | ldp BASE, RC, L:CRET1->base - | movz TISNUM, #(LJ_TISNUM>>1)&0xffff, lsl #48 - | movz TISNUMhi, #(LJ_TISNUM>>1)&0xffff, lsl #16 - | movn TISNIL, #0 - | mov L, CRET1 - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | sub RC, RC, BASE - | st_vmstate ST_INTERP - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | ldr CTSTATE, GL->ctype_state - | stp BASE, CARG4, L->base - | str L, CTSTATE->L - | mov CARG1, CTSTATE - | mov CARG2, RA - | bl extern lj_ccallback_leave // (CTState *cts, TValue *o) - | ldp x0, x1, CTSTATE->cb.gpr[0] - | ldp d0, d1, CTSTATE->cb.fpr[0] - | b ->vm_leave_unw - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, x19 - | stp x20, CCSTATE, [sp, #-32]! - | stp fp, lr, [sp, #16] - | add fp, sp, #16 - | mov CCSTATE, x0 - | ldr TMP0w, CCSTATE:x0->spadj - | ldrb TMP1w, CCSTATE->nsp - | add TMP2, CCSTATE, #offsetof(CCallState, stack) - | subs TMP1, TMP1, #1 - | ldr TMP3, CCSTATE->func - | sub sp, sp, TMP0 - | bmi >2 - |1: // Copy stack slots - | ldr TMP0, [TMP2, TMP1, lsl #3] - | str TMP0, [sp, TMP1, lsl #3] - | subs TMP1, TMP1, #1 - | bpl <1 - |2: - | ldp x0, x1, CCSTATE->gpr[0] - | ldp d0, d1, CCSTATE->fpr[0] - | ldp x2, x3, CCSTATE->gpr[2] - | ldp d2, d3, CCSTATE->fpr[2] - | ldp x4, x5, CCSTATE->gpr[4] - | ldp d4, d5, CCSTATE->fpr[4] - | ldp x6, x7, CCSTATE->gpr[6] - | ldp d6, d7, CCSTATE->fpr[6] - | ldr x8, CCSTATE->retp - | blr TMP3 - | sub sp, fp, #16 - | stp x0, x1, CCSTATE->gpr[0] - | stp d0, d1, CCSTATE->fpr[0] - | stp d2, d3, CCSTATE->fpr[2] - | ldp fp, lr, [sp, #16] - | ldp x20, CCSTATE, [sp], #32 - | ret - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1, RC = src2, JMP with RC = target - | ldr CARG1, [BASE, RA, lsl #3] - | ldrh RBw, [PC, # OFS_RD] - | ldr CARG2, [BASE, RC, lsl #3] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | sub RB, RB, #0x20000 - | checkint CARG1, >3 - | checkint CARG2, >4 - | cmp CARG1w, CARG2w - if (op == BC_ISLT) { - | csel PC, RB, PC, lt - } else if (op == BC_ISGE) { - | csel PC, RB, PC, ge - } else if (op == BC_ISLE) { - | csel PC, RB, PC, le - } else { - | csel PC, RB, PC, gt - } - |1: - | ins_next - | - |3: // RA not int. - | ldr FARG1, [BASE, RA, lsl #3] - | blo ->vmeta_comp - | ldr FARG2, [BASE, RC, lsl #3] - | cmp TISNUMhi, CARG2, lsr #32 - | bhi >5 - | bne ->vmeta_comp - | // RA number, RC int. - | scvtf FARG2, CARG2w - | b >5 - | - |4: // RA int, RC not int - | ldr FARG2, [BASE, RC, lsl #3] - | blo ->vmeta_comp - | // RA int, RC number. - | scvtf FARG1, CARG1w - | - |5: // RA number, RC number - | fcmp FARG1, FARG2 - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - if (op == BC_ISLT) { - | csel PC, RB, PC, lo - } else if (op == BC_ISGE) { - | csel PC, RB, PC, hs - } else if (op == BC_ISLE) { - | csel PC, RB, PC, ls - } else { - | csel PC, RB, PC, hi - } - | b <1 - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1, RC = src2, JMP with RC = target - | ldr CARG1, [BASE, RA, lsl #3] - | add RC, BASE, RC, lsl #3 - | ldrh RBw, [PC, # OFS_RD] - | ldr CARG3, [RC] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | sub RB, RB, #0x20000 - | asr ITYPE, CARG3, #47 - | cmn ITYPE, #-LJ_TISNUM - if (vk) { - | bls ->BC_ISEQN_Z - } else { - | bls ->BC_ISNEN_Z - } - | // RC is not a number. - | asr TMP0, CARG1, #47 - |.if FFI - | // Check if RC or RA is a cdata. - | cmn ITYPE, #-LJ_TCDATA - | ccmn TMP0, #-LJ_TCDATA, #4, ne - | beq ->vmeta_equal_cd - |.endif - | cmp CARG1, CARG3 - | bne >2 - | // Tag and value are equal. - if (vk) { - |->BC_ISEQV_Z: - | mov PC, RB // Perform branch. - } - |1: - | ins_next - | - |2: // Check if the tags are the same and it's a table or userdata. - | cmp ITYPE, TMP0 - | ccmn ITYPE, #-LJ_TISTABUD, #2, eq - if (vk) { - | bhi <1 - } else { - | bhi ->BC_ISEQV_Z // Reuse code from opposite instruction. - } - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | and TAB:CARG2, CARG1, #LJ_GCVMASK - | ldr TAB:TMP2, TAB:CARG2->metatable - if (vk) { - | cbz TAB:TMP2, <1 // No metatable? - | ldrb TMP1w, TAB:TMP2->nomm - | mov CARG4, #0 // ne = 0 - | tbnz TMP1w, #MM_eq, <1 // 'no __eq' flag set: done. - } else { - | cbz TAB:TMP2, ->BC_ISEQV_Z // No metatable? - | ldrb TMP1w, TAB:TMP2->nomm - | mov CARG4, #1 // ne = 1. - | tbnz TMP1w, #MM_eq, ->BC_ISEQV_Z // 'no __eq' flag set: done. - } - | b ->vmeta_equal - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src, RC = str_const (~), JMP with RC = target - | ldr CARG1, [BASE, RA, lsl #3] - | mvn RC, RC - | ldrh RBw, [PC, # OFS_RD] - | ldr CARG2, [KBASE, RC, lsl #3] - | add PC, PC, #4 - | movn TMP0, #~LJ_TSTR - |.if FFI - | asr ITYPE, CARG1, #47 - |.endif - | add RB, PC, RB, lsl #2 - | add CARG2, CARG2, TMP0, lsl #47 - | sub RB, RB, #0x20000 - |.if FFI - | cmn ITYPE, #-LJ_TCDATA - | beq ->vmeta_equal_cd - |.endif - | cmp CARG1, CARG2 - if (vk) { - | csel PC, RB, PC, eq - } else { - | csel PC, RB, PC, ne - } - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src, RC = num_const (~), JMP with RC = target - | ldr CARG1, [BASE, RA, lsl #3] - | add RC, KBASE, RC, lsl #3 - | ldrh RBw, [PC, # OFS_RD] - | ldr CARG3, [RC] - | add PC, PC, #4 - | add RB, PC, RB, lsl #2 - | sub RB, RB, #0x20000 - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | checkint CARG1, >4 - | checkint CARG3, >6 - | cmp CARG1w, CARG3w - |1: - if (vk) { - | csel PC, RB, PC, eq - |2: - } else { - |2: - | csel PC, RB, PC, ne - } - |3: - | ins_next - | - |4: // RA not int. - |.if FFI - | blo >7 - |.else - | blo <2 - |.endif - | ldr FARG1, [BASE, RA, lsl #3] - | ldr FARG2, [RC] - | cmp TISNUMhi, CARG3, lsr #32 - | bne >5 - | // RA number, RC int. - | scvtf FARG2, CARG3w - |5: - | // RA number, RC number. - | fcmp FARG1, FARG2 - | b <1 - | - |6: // RA int, RC number - | ldr FARG2, [RC] - | scvtf FARG1, CARG1w - | fcmp FARG1, FARG2 - | b <1 - | - |.if FFI - |7: - | asr ITYPE, CARG1, #47 - | cmn ITYPE, #-LJ_TCDATA - | bne <2 - | b ->vmeta_equal_cd - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src, RC = primitive_type (~), JMP with RC = target - | ldr TMP0, [BASE, RA, lsl #3] - | ldrh RBw, [PC, # OFS_RD] - | add PC, PC, #4 - | add RC, RC, #1 - | add RB, PC, RB, lsl #2 - |.if FFI - | asr ITYPE, TMP0, #47 - | cmn ITYPE, #-LJ_TCDATA - | beq ->vmeta_equal_cd - | cmn RC, ITYPE - |.else - | cmn RC, TMP0, asr #47 - |.endif - | sub RB, RB, #0x20000 - if (vk) { - | csel PC, RB, PC, eq - } else { - | csel PC, RB, PC, ne - } - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst or unused, RC = src, JMP with RC = target - | ldrh RBw, [PC, # OFS_RD] - | ldr TMP0, [BASE, RC, lsl #3] - | add PC, PC, #4 - | mov_false TMP1 - | add RB, PC, RB, lsl #2 - | cmp TMP0, TMP1 - | sub RB, RB, #0x20000 - if (op == BC_ISTC || op == BC_IST) { - if (op == BC_ISTC) { - | csel RA, RA, RC, lo - } - | csel PC, RB, PC, lo - } else { - if (op == BC_ISFC) { - | csel RA, RA, RC, hs - } - | csel PC, RB, PC, hs - } - if (op == BC_ISTC || op == BC_ISFC) { - | str TMP0, [BASE, RA, lsl #3] - } - | ins_next - break; - - case BC_ISTYPE: - | // RA = src, RC = -type - | ldr TMP0, [BASE, RA, lsl #3] - | cmn RC, TMP0, asr #47 - | bne ->vmeta_istype - | ins_next - break; - case BC_ISNUM: - | // RA = src, RC = -(TISNUM-1) - | ldr TMP0, [BASE, RA] - | checknum TMP0, ->vmeta_istype - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst, RC = src - | ldr TMP0, [BASE, RC, lsl #3] - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_NOT: - | // RA = dst, RC = src - | ldr TMP0, [BASE, RC, lsl #3] - | mov_false TMP1 - | mov_true TMP2 - | cmp TMP0, TMP1 - | csel TMP0, TMP1, TMP2, lo - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_UNM: - | // RA = dst, RC = src - | ldr TMP0, [BASE, RC, lsl #3] - | asr ITYPE, TMP0, #47 - | cmn ITYPE, #-LJ_TISNUM - | bhi ->vmeta_unm - | eor TMP0, TMP0, #U64x(80000000,00000000) - | bne >5 - | negs TMP0w, TMP0w - | movz CARG3, #0x41e0, lsl #48 // 2^31. - | add TMP0, TMP0, TISNUM - | csel TMP0, TMP0, CARG3, vc - |5: - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_LEN: - | // RA = dst, RC = src - | ldr CARG1, [BASE, RC, lsl #3] - | asr ITYPE, CARG1, #47 - | cmn ITYPE, #-LJ_TSTR - | and CARG1, CARG1, #LJ_GCVMASK - | bne >2 - | ldr CARG1w, STR:CARG1->len - |1: - | add CARG1, CARG1, TISNUM - | str CARG1, [BASE, RA, lsl #3] - | ins_next - | - |2: - | cmn ITYPE, #-LJ_TTAB - | bne ->vmeta_len -#if LJ_52 - | ldr TAB:CARG2, TAB:CARG1->metatable - | cbnz TAB:CARG2, >9 - |3: -#endif - |->BC_LEN_Z: - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | b <1 - | -#if LJ_52 - |9: - | ldrb TMP1w, TAB:CARG2->nomm - | tbnz TMP1w, #MM_len, <3 // 'no __len' flag set: done. - | b ->vmeta_len -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithcheck_int, target - | checkint CARG1, target - | checkint CARG2, target - |.endmacro - | - |.macro ins_arithcheck_num, target - | checknum CARG1, target - | checknum CARG2, target - |.endmacro - | - |.macro ins_arithcheck_nzdiv, target - | cbz CARG2w, target - |.endmacro - | - |.macro ins_arithhead - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||if (vk == 1) { - | and RC, RC, #255 - | decode_RB RB, INS - ||} else { - | decode_RB RB, INS - | and RC, RC, #255 - ||} - |.endmacro - | - |.macro ins_arithload, reg1, reg2 - | // RA = dst, RB = src1, RC = src2 | num_const - ||switch (vk) { - ||case 0: - | ldr reg1, [BASE, RB, lsl #3] - | ldr reg2, [KBASE, RC, lsl #3] - || break; - ||case 1: - | ldr reg1, [KBASE, RC, lsl #3] - | ldr reg2, [BASE, RB, lsl #3] - || break; - ||default: - | ldr reg1, [BASE, RB, lsl #3] - | ldr reg2, [BASE, RC, lsl #3] - || break; - ||} - |.endmacro - | - |.macro ins_arithfallback, ins - ||switch (vk) { - ||case 0: - | ins ->vmeta_arith_vn - || break; - ||case 1: - | ins ->vmeta_arith_nv - || break; - ||default: - | ins ->vmeta_arith_vv - || break; - ||} - |.endmacro - | - |.macro ins_arithmod, res, reg1, reg2 - | fdiv d2, reg1, reg2 - | frintm d2, d2 - | // Cannot use fmsub, because FMA is not enabled by default. - | fmul d2, d2, reg2 - | fsub res, reg1, d2 - |.endmacro - | - |.macro ins_arithdn, intins, fpins - | ins_arithhead - | ins_arithload CARG1, CARG2 - | ins_arithcheck_int >5 - |.if "intins" == "smull" - | smull CARG1, CARG1w, CARG2w - | cmp CARG1, CARG1, sxtw - | mov CARG1w, CARG1w - | ins_arithfallback bne - |.elif "intins" == "ins_arithmodi" - | ins_arithfallback ins_arithcheck_nzdiv - | bl ->vm_modi - |.else - | intins CARG1w, CARG1w, CARG2w - | ins_arithfallback bvs - |.endif - | add CARG1, CARG1, TISNUM - | str CARG1, [BASE, RA, lsl #3] - |4: - | ins_next - | - |5: // FP variant. - | ins_arithload FARG1, FARG2 - | ins_arithfallback ins_arithcheck_num - | fpins FARG1, FARG1, FARG2 - | str FARG1, [BASE, RA, lsl #3] - | b <4 - |.endmacro - | - |.macro ins_arithfp, fpins - | ins_arithhead - | ins_arithload CARG1, CARG2 - | ins_arithload FARG1, FARG2 - | ins_arithfallback ins_arithcheck_num - |.if "fpins" == "fpow" - | bl extern pow - |.else - | fpins FARG1, FARG1, FARG2 - |.endif - | str FARG1, [BASE, RA, lsl #3] - | ins_next - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arithdn adds, fadd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arithdn subs, fsub - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arithdn smull, fmul - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arithfp fdiv - break; - case BC_MODVN: case BC_MODNV: case BC_MODVV: - | ins_arithdn ins_arithmodi, ins_arithmod - break; - case BC_POW: - | // NYI: (partial) integer arithmetic. - | ins_arithfp fpow - break; - - case BC_CAT: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = dst, RB = src_start, RC = src_end - | str BASE, L->base - | sub CARG3, RC, RB - | add CARG2, BASE, RC, lsl #3 - |->BC_CAT_Z: - | // RA = dst, CARG2 = top-1, CARG3 = left - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // Returns NULL (finished) or TValue * (metamethod). - | ldrb RBw, [PC, #-4+OFS_RB] - | ldr BASE, L->base - | cbnz CRET1, ->vmeta_binop - | ldr TMP0, [BASE, RB, lsl #3] - | str TMP0, [BASE, RA, lsl #3] // Copy result to RA. - | ins_next - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst, RC = str_const (~) - | mvn RC, RC - | ldr TMP0, [KBASE, RC, lsl #3] - | movn TMP1, #~LJ_TSTR - | add TMP0, TMP0, TMP1, lsl #47 - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_KCDATA: - |.if FFI - | // RA = dst, RC = cdata_const (~) - | mvn RC, RC - | ldr TMP0, [KBASE, RC, lsl #3] - | movn TMP1, #~LJ_TCDATA - | add TMP0, TMP0, TMP1, lsl #47 - | str TMP0, [BASE, RA, lsl #3] - | ins_next - |.endif - break; - case BC_KSHORT: - | // RA = dst, RC = int16_literal - | sxth RCw, RCw - | add TMP0, RC, TISNUM - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_KNUM: - | // RA = dst, RC = num_const - | ldr TMP0, [KBASE, RC, lsl #3] - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_KPRI: - | // RA = dst, RC = primitive_type (~) - | mvn TMP0, RC, lsl #47 - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_KNIL: - | // RA = base, RC = end - | add RA, BASE, RA, lsl #3 - | add RC, BASE, RC, lsl #3 - | str TISNIL, [RA], #8 - |1: - | cmp RA, RC - | str TISNIL, [RA], #8 - | blt <1 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst, RC = uvnum - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | add RC, RC, #offsetof(GCfuncL, uvptr)/8 - | and LFUNC:CARG2, CARG2, #LJ_GCVMASK - | ldr UPVAL:CARG2, [LFUNC:CARG2, RC, lsl #3] - | ldr CARG2, UPVAL:CARG2->v - | ldr TMP0, [CARG2] - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - case BC_USETV: - | // RA = uvnum, RC = src - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | add RA, RA, #offsetof(GCfuncL, uvptr)/8 - | and LFUNC:CARG2, CARG2, #LJ_GCVMASK - | ldr UPVAL:CARG1, [LFUNC:CARG2, RA, lsl #3] - | ldr CARG3, [BASE, RC, lsl #3] - | ldr CARG2, UPVAL:CARG1->v - | ldrb TMP2w, UPVAL:CARG1->marked - | ldrb TMP0w, UPVAL:CARG1->closed - | asr ITYPE, CARG3, #47 - | str CARG3, [CARG2] - | add ITYPE, ITYPE, #-LJ_TISGCV - | tst TMP2w, #LJ_GC_BLACK // isblack(uv) - | ccmp TMP0w, #0, #4, ne // && uv->closed - | ccmn ITYPE, #-(LJ_TNUMX - LJ_TISGCV), #0, ne // && tvisgcv(v) - | bhi >2 - |1: - | ins_next - | - |2: // Check if new value is white. - | and GCOBJ:CARG3, CARG3, #LJ_GCVMASK - | ldrb TMP1w, GCOBJ:CARG3->gch.marked - | tst TMP1w, #LJ_GC_WHITES // iswhite(str) - | beq <1 - | // Crossed a write barrier. Move the barrier forward. - | mov CARG1, GL - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETS: - | // RA = uvnum, RC = str_const (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | add RA, RA, #offsetof(GCfuncL, uvptr)/8 - | mvn RC, RC - | and LFUNC:CARG2, CARG2, #LJ_GCVMASK - | ldr UPVAL:CARG1, [LFUNC:CARG2, RA, lsl #3] - | ldr STR:CARG3, [KBASE, RC, lsl #3] - | movn TMP0, #~LJ_TSTR - | ldr CARG2, UPVAL:CARG1->v - | ldrb TMP2w, UPVAL:CARG1->marked - | add TMP0, STR:CARG3, TMP0, lsl #47 - | ldrb TMP1w, STR:CARG3->marked - | str TMP0, [CARG2] - | tbnz TMP2w, #2, >2 // isblack(uv) - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | ldrb TMP0w, UPVAL:CARG1->closed - | tst TMP1w, #LJ_GC_WHITES // iswhite(str) - | ccmp TMP0w, #0, #4, ne - | beq <1 - | // Crossed a write barrier. Move the barrier forward. - | mov CARG1, GL - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETN: - | // RA = uvnum, RC = num_const - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | add RA, RA, #offsetof(GCfuncL, uvptr)/8 - | and LFUNC:CARG2, CARG2, #LJ_GCVMASK - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA, lsl #3] - | ldr TMP0, [KBASE, RC, lsl #3] - | ldr CARG2, UPVAL:CARG2->v - | str TMP0, [CARG2] - | ins_next - break; - case BC_USETP: - | // RA = uvnum, RC = primitive_type (~) - | ldr LFUNC:CARG2, [BASE, FRAME_FUNC] - | add RA, RA, #offsetof(GCfuncL, uvptr)/8 - | and LFUNC:CARG2, CARG2, #LJ_GCVMASK - | ldr UPVAL:CARG2, [LFUNC:CARG2, RA, lsl #3] - | mvn TMP0, RC, lsl #47 - | ldr CARG2, UPVAL:CARG2->v - | str TMP0, [CARG2] - | ins_next - break; - - case BC_UCLO: - | // RA = level, RC = target - | ldr CARG3, L->openupval - | add RC, PC, RC, lsl #2 - | str BASE, L->base - | sub PC, RC, #0x20000 - | cbz CARG3, >1 - | mov CARG1, L - | add CARG2, BASE, RA, lsl #3 - | bl extern lj_func_closeuv // (lua_State *L, TValue *level) - | ldr BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst, RC = proto_const (~) (holding function prototype) - | mvn RC, RC - | str BASE, L->base - | ldr LFUNC:CARG3, [BASE, FRAME_FUNC] - | str PC, SAVE_PC - | ldr CARG2, [KBASE, RC, lsl #3] - | mov CARG1, L - | and LFUNC:CARG3, CARG3, #LJ_GCVMASK - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | bl extern lj_func_newL_gc - | // Returns GCfuncL *. - | ldr BASE, L->base - | movn TMP0, #~LJ_TFUNC - | add CRET1, CRET1, TMP0, lsl #47 - | str CRET1, [BASE, RA, lsl #3] - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst, RC = (hbits|asize) | tab_const (~) - | ldp CARG3, CARG4, GL->gc.total // Assumes threshold follows total. - | str BASE, L->base - | str PC, SAVE_PC - | mov CARG1, L - | cmp CARG3, CARG4 - | bhs >5 - |1: - if (op == BC_TNEW) { - | and CARG2, RC, #0x7ff - | lsr CARG3, RC, #11 - | cmp CARG2, #0x7ff - | mov TMP0, #0x801 - | csel CARG2, CARG2, TMP0, ne - | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Returns GCtab *. - } else { - | mvn RC, RC - | ldr CARG2, [KBASE, RC, lsl #3] - | bl extern lj_tab_dup // (lua_State *L, Table *kt) - | // Returns GCtab *. - } - | ldr BASE, L->base - | movk CRET1, #(LJ_TTAB>>1)&0xffff, lsl #48 - | str CRET1, [BASE, RA, lsl #3] - | ins_next - | - |5: - | bl extern lj_gc_step_fixtop // (lua_State *L) - | mov CARG1, L - | b <1 - break; - - case BC_GGET: - | // RA = dst, RC = str_const (~) - case BC_GSET: - | // RA = src, RC = str_const (~) - | ldr LFUNC:CARG1, [BASE, FRAME_FUNC] - | mvn RC, RC - | and LFUNC:CARG1, CARG1, #LJ_GCVMASK - | ldr TAB:CARG2, LFUNC:CARG1->env - | ldr STR:RC, [KBASE, RC, lsl #3] - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - break; - - case BC_TGETV: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = dst, RB = table, RC = key - | ldr CARG2, [BASE, RB, lsl #3] - | ldr TMP1, [BASE, RC, lsl #3] - | checktab CARG2, ->vmeta_tgetv - | checkint TMP1, >9 // Integer key? - | ldr CARG3, TAB:CARG2->array - | ldr CARG1w, TAB:CARG2->asize - | add CARG3, CARG3, TMP1, uxtw #3 - | cmp TMP1w, CARG1w // In array part? - | bhs ->vmeta_tgetv - | ldr TMP0, [CARG3] - | cmp TMP0, TISNIL - | beq >5 - |1: - | str TMP0, [BASE, RA, lsl #3] - | ins_next - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, <1 // No metatable: done. - | ldrb TMP1w, TAB:CARG1->nomm - | tbnz TMP1w, #MM_index, <1 // 'no __index' flag set: done. - | b ->vmeta_tgetv - | - |9: - | asr ITYPE, TMP1, #47 - | cmn ITYPE, #-LJ_TSTR // String key? - | bne ->vmeta_tgetv - | and STR:RC, TMP1, #LJ_GCVMASK - | b ->BC_TGETS_Z - break; - case BC_TGETS: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = dst, RB = table, RC = str_const (~) - | ldr CARG2, [BASE, RB, lsl #3] - | mvn RC, RC - | ldr STR:RC, [KBASE, RC, lsl #3] - | checktab CARG2, ->vmeta_tgets1 - |->BC_TGETS_Z: - | // TAB:CARG2 = GCtab *, STR:RC = GCstr *, RA = dst - | ldr TMP1w, TAB:CARG2->hmask - | ldr TMP2w, STR:RC->sid - | ldr NODE:CARG3, TAB:CARG2->node - | and TMP1w, TMP1w, TMP2w // idx = str->sid & tab->hmask - | add TMP1, TMP1, TMP1, lsl #1 - | movn CARG4, #~LJ_TSTR - | add NODE:CARG3, NODE:CARG3, TMP1, lsl #3 // node = tab->node + idx*3*8 - | add CARG4, STR:RC, CARG4, lsl #47 // Tagged key to look for. - |1: - | ldp TMP0, CARG1, NODE:CARG3->val - | ldr NODE:CARG3, NODE:CARG3->next - | cmp CARG1, CARG4 - | bne >4 - | cmp TMP0, TISNIL - | beq >5 - |3: - | str TMP0, [BASE, RA, lsl #3] - | ins_next - | - |4: // Follow hash chain. - | cbnz NODE:CARG3, <1 - | // End of hash chain: key not found, nil result. - | mov TMP0, TISNIL - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, <3 // No metatable: done. - | ldrb TMP1w, TAB:CARG1->nomm - | tbnz TMP1w, #MM_index, <3 // 'no __index' flag set: done. - | b ->vmeta_tgets - break; - case BC_TGETB: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = dst, RB = table, RC = index - | ldr CARG2, [BASE, RB, lsl #3] - | checktab CARG2, ->vmeta_tgetb - | ldr CARG3, TAB:CARG2->array - | ldr CARG1w, TAB:CARG2->asize - | add CARG3, CARG3, RC, lsl #3 - | cmp RCw, CARG1w // In array part? - | bhs ->vmeta_tgetb - | ldr TMP0, [CARG3] - | cmp TMP0, TISNIL - | beq >5 - |1: - | str TMP0, [BASE, RA, lsl #3] - | ins_next - | - |5: // Check for __index if table value is nil. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, <1 // No metatable: done. - | ldrb TMP1w, TAB:CARG1->nomm - | tbnz TMP1w, #MM_index, <1 // 'no __index' flag set: done. - | b ->vmeta_tgetb - break; - case BC_TGETR: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = dst, RB = table, RC = key - | ldr CARG1, [BASE, RB, lsl #3] - | ldr TMP1, [BASE, RC, lsl #3] - | and TAB:CARG1, CARG1, #LJ_GCVMASK - | ldr CARG3, TAB:CARG1->array - | ldr TMP2w, TAB:CARG1->asize - | add CARG3, CARG3, TMP1w, uxtw #3 - | cmp TMP1w, TMP2w // In array part? - | bhs ->vmeta_tgetr - | ldr TMP0, [CARG3] - |->BC_TGETR_Z: - | str TMP0, [BASE, RA, lsl #3] - | ins_next - break; - - case BC_TSETV: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = src, RB = table, RC = key - | ldr CARG2, [BASE, RB, lsl #3] - | ldr TMP1, [BASE, RC, lsl #3] - | checktab CARG2, ->vmeta_tsetv - | checkint TMP1, >9 // Integer key? - | ldr CARG3, TAB:CARG2->array - | ldr CARG1w, TAB:CARG2->asize - | add CARG3, CARG3, TMP1, uxtw #3 - | cmp TMP1w, CARG1w // In array part? - | bhs ->vmeta_tsetv - | ldr TMP1, [CARG3] - | ldr TMP0, [BASE, RA, lsl #3] - | ldrb TMP2w, TAB:CARG2->marked - | cmp TMP1, TISNIL // Previous value is nil? - | beq >5 - |1: - | str TMP0, [CARG3] - | tbnz TMP2w, #2, >7 // isblack(table) - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, <1 // No metatable: done. - | ldrb TMP1w, TAB:CARG1->nomm - | tbnz TMP1w, #MM_newindex, <1 // 'no __newindex' flag set: done. - | b ->vmeta_tsetv - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP2w, TMP1 - | b <2 - | - |9: - | asr ITYPE, TMP1, #47 - | cmn ITYPE, #-LJ_TSTR // String key? - | bne ->vmeta_tsetv - | and STR:RC, TMP1, #LJ_GCVMASK - | b ->BC_TSETS_Z - break; - case BC_TSETS: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = dst, RB = table, RC = str_const (~) - | ldr CARG2, [BASE, RB, lsl #3] - | mvn RC, RC - | ldr STR:RC, [KBASE, RC, lsl #3] - | checktab CARG2, ->vmeta_tsets1 - |->BC_TSETS_Z: - | // TAB:CARG2 = GCtab *, STR:RC = GCstr *, RA = src - | ldr TMP1w, TAB:CARG2->hmask - | ldr TMP2w, STR:RC->sid - | ldr NODE:CARG3, TAB:CARG2->node - | and TMP1w, TMP1w, TMP2w // idx = str->sid & tab->hmask - | add TMP1, TMP1, TMP1, lsl #1 - | movn CARG4, #~LJ_TSTR - | add NODE:CARG3, NODE:CARG3, TMP1, lsl #3 // node = tab->node + idx*3*8 - | add CARG4, STR:RC, CARG4, lsl #47 // Tagged key to look for. - | strb wzr, TAB:CARG2->nomm // Clear metamethod cache. - |1: - | ldp TMP1, CARG1, NODE:CARG3->val - | ldr NODE:TMP3, NODE:CARG3->next - | ldrb TMP2w, TAB:CARG2->marked - | cmp CARG1, CARG4 - | bne >5 - | ldr TMP0, [BASE, RA, lsl #3] - | cmp TMP1, TISNIL // Previous value is nil? - | beq >4 - |2: - | str TMP0, NODE:CARG3->val - | tbnz TMP2w, #2, >7 // isblack(table) - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, <2 // No metatable: done. - | ldrb TMP1w, TAB:CARG1->nomm - | tbnz TMP1w, #MM_newindex, <2 // 'no __newindex' flag set: done. - | b ->vmeta_tsets - | - |5: // Follow hash chain. - | mov NODE:CARG3, NODE:TMP3 - | cbnz NODE:TMP3, <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, >6 // No metatable: continue. - | ldrb TMP1w, TAB:CARG1->nomm - | // 'no __newindex' flag NOT set: check. - | tbz TMP1w, #MM_newindex, ->vmeta_tsets - |6: - | movn TMP1, #~LJ_TSTR - | str PC, SAVE_PC - | add TMP0, STR:RC, TMP1, lsl #47 - | str BASE, L->base - | mov CARG1, L - | str TMP0, TMPD - | add CARG3, sp, TMPDofs - | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Returns TValue *. - | ldr BASE, L->base - | ldr TMP0, [BASE, RA, lsl #3] - | str TMP0, [CRET1] - | b <3 // No 2nd write barrier needed. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP2w, TMP1 - | b <3 - break; - case BC_TSETB: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = src, RB = table, RC = index - | ldr CARG2, [BASE, RB, lsl #3] - | checktab CARG2, ->vmeta_tsetb - | ldr CARG3, TAB:CARG2->array - | ldr CARG1w, TAB:CARG2->asize - | add CARG3, CARG3, RC, lsl #3 - | cmp RCw, CARG1w // In array part? - | bhs ->vmeta_tsetb - | ldr TMP1, [CARG3] - | ldr TMP0, [BASE, RA, lsl #3] - | ldrb TMP2w, TAB:CARG2->marked - | cmp TMP1, TISNIL // Previous value is nil? - | beq >5 - |1: - | str TMP0, [CARG3] - | tbnz TMP2w, #2, >7 // isblack(table) - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | ldr TAB:CARG1, TAB:CARG2->metatable - | cbz TAB:CARG1, <1 // No metatable: done. - | ldrb TMP1w, TAB:CARG1->nomm - | tbnz TMP1w, #MM_newindex, <1 // 'no __newindex' flag set: done. - | b ->vmeta_tsetb - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP2w, TMP1 - | b <2 - break; - case BC_TSETR: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = src, RB = table, RC = key - | ldr CARG2, [BASE, RB, lsl #3] - | ldr TMP1, [BASE, RC, lsl #3] - | and TAB:CARG2, CARG2, #LJ_GCVMASK - | ldr CARG1, TAB:CARG2->array - | ldrb TMP2w, TAB:CARG2->marked - | ldr CARG4w, TAB:CARG2->asize - | add CARG1, CARG1, TMP1, uxtw #3 - | tbnz TMP2w, #2, >7 // isblack(table) - |2: - | cmp TMP1w, CARG4w // In array part? - | bhs ->vmeta_tsetr - |->BC_TSETR_Z: - | ldr TMP0, [BASE, RA, lsl #3] - | str TMP0, [CARG1] - | ins_next - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP2w, TMP0 - | b <2 - break; - - case BC_TSETM: - | // RA = base (table at base-1), RC = num_const (start index) - | add RA, BASE, RA, lsl #3 - |1: - | ldr RBw, SAVE_MULTRES - | ldr TAB:CARG2, [RA, #-8] // Guaranteed to be a table. - | ldr TMP1, [KBASE, RC, lsl #3] // Integer constant is in lo-word. - | sub RB, RB, #8 - | cbz RB, >4 // Nothing to copy? - | and TAB:CARG2, CARG2, #LJ_GCVMASK - | ldr CARG1w, TAB:CARG2->asize - | add CARG3w, TMP1w, RBw, lsr #3 - | ldr CARG4, TAB:CARG2->array - | cmp CARG3, CARG1 - | add RB, RA, RB - | bhi >5 - | add TMP1, CARG4, TMP1w, uxtw #3 - | ldrb TMP2w, TAB:CARG2->marked - |3: // Copy result slots to table. - | ldr TMP0, [RA], #8 - | str TMP0, [TMP1], #8 - | cmp RA, RB - | blo <3 - | tbnz TMP2w, #2, >7 // isblack(table) - |4: - | ins_next - | - |5: // Need to resize array part. - | str BASE, L->base - | mov CARG1, L - | str PC, SAVE_PC - | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | // Must not reallocate the stack. - | b <1 - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP2w, TMP1 - | b <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base, (RB = nresults+1,) RC = extra_nargs - | ldr TMP0w, SAVE_MULTRES - | decode_RC8RD NARGS8:RC, RC - | add NARGS8:RC, NARGS8:RC, TMP0 - | b ->BC_CALL_Z - break; - case BC_CALL: - | decode_RC8RD NARGS8:RC, RC - | // RA = base, (RB = nresults+1,) RC = (nargs+1)*8 - |->BC_CALL_Z: - | mov RB, BASE // Save old BASE for vmeta_call. - | add BASE, BASE, RA, lsl #3 - | ldr CARG3, [BASE] - | sub NARGS8:RC, NARGS8:RC, #8 - | add BASE, BASE, #16 - | checkfunc CARG3, ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base, (RB = 0,) RC = extra_nargs - | ldr TMP0w, SAVE_MULTRES - | add NARGS8:RC, TMP0, RC, lsl #3 - | b ->BC_CALLT1_Z - break; - case BC_CALLT: - | lsl NARGS8:RC, RC, #3 - | // RA = base, (RB = 0,) RC = (nargs+1)*8 - |->BC_CALLT1_Z: - | add RA, BASE, RA, lsl #3 - | ldr TMP1, [RA] - | sub NARGS8:RC, NARGS8:RC, #8 - | add RA, RA, #16 - | checktp CARG3, TMP1, LJ_TFUNC, ->vmeta_callt - | ldr PC, [BASE, FRAME_PC] - |->BC_CALLT2_Z: - | mov RB, #0 - | ldrb TMP2w, LFUNC:CARG3->ffid - | tst PC, #FRAME_TYPE - | bne >7 - |1: - | str TMP1, [BASE, FRAME_FUNC] // Copy function down, but keep PC. - | cbz NARGS8:RC, >3 - |2: - | ldr TMP0, [RA, RB] - | add TMP1, RB, #8 - | cmp TMP1, NARGS8:RC - | str TMP0, [BASE, RB] - | mov RB, TMP1 - | bne <2 - |3: - | cmp TMP2, #1 // (> FF_C) Calling a fast function? - | bhi >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | ldrb RAw, [PC, #-4+OFS_RA] - | sub CARG1, BASE, RA, lsl #3 - | ldr LFUNC:CARG1, [CARG1, #-32] - | and LFUNC:CARG1, CARG1, #LJ_GCVMASK - | ldr CARG1, LFUNC:CARG1->pc - | ldr KBASE, [CARG1, #PC2PROTO(k)] - | b <4 - | - |7: // Tailcall from a vararg function. - | eor PC, PC, #FRAME_VARG - | tst PC, #FRAME_TYPEP // Vararg frame below? - | csel TMP2, RB, TMP2, ne // Clear ffid if no Lua function below. - | bne <1 - | sub BASE, BASE, PC - | ldr PC, [BASE, FRAME_PC] - | tst PC, #FRAME_TYPE - | csel TMP2, RB, TMP2, ne // Clear ffid if no Lua function below. - | b <1 - break; - - case BC_ITERC: - | // RA = base, (RB = nresults+1, RC = nargs+1 (2+1)) - | add RA, BASE, RA, lsl #3 - | ldr CARG3, [RA, #-24] - | mov RB, BASE // Save old BASE for vmeta_call. - | ldp CARG1, CARG2, [RA, #-16] - | add BASE, RA, #16 - | mov NARGS8:RC, #16 // Iterators get 2 arguments. - | str CARG3, [RA] // Copy callable. - | stp CARG1, CARG2, [RA, #16] // Copy state and control var. - | checkfunc CARG3, ->vmeta_call - | ins_call - break; - - case BC_ITERN: - |.if JIT - | hotloop - |.endif - |->vm_IITERN: - | // RA = base, (RB = nresults+1, RC = nargs+1 (2+1)) - | add RA, BASE, RA, lsl #3 - | ldr TAB:RB, [RA, #-16] - | ldrh TMP3w, [PC, # OFS_RD] - | ldr CARG1w, [RA, #-8+LO] // Get index from control var. - | add PC, PC, #4 - | add TMP3, PC, TMP3, lsl #2 - | and TAB:RB, RB, #LJ_GCVMASK - | sub TMP3, TMP3, #0x20000 - | ldr TMP1w, TAB:RB->asize - | ldr CARG2, TAB:RB->array - |1: // Traverse array part. - | subs RC, CARG1, TMP1 - | add CARG3, CARG2, CARG1, lsl #3 - | bhs >5 // Index points after array part? - | ldr TMP0, [CARG3] - | cmp TMP0, TISNIL - | cinc CARG1, CARG1, eq // Skip holes in array part. - | beq <1 - | add CARG1, CARG1, TISNUM - | stp CARG1, TMP0, [RA] - | add CARG1, CARG1, #1 - |3: - | str CARG1w, [RA, #-8+LO] // Update control var. - | mov PC, TMP3 - |4: - | ins_next - | - |5: // Traverse hash part. - | ldr TMP2w, TAB:RB->hmask - | ldr NODE:RB, TAB:RB->node - |6: - | add CARG1, RC, RC, lsl #1 - | cmp RC, TMP2 // End of iteration? Branch to ITERN+1. - | add NODE:CARG3, NODE:RB, CARG1, lsl #3 // node = tab->node + idx*3*8 - | bhi <4 - | ldp TMP0, CARG1, NODE:CARG3->val - | cmp TMP0, TISNIL - | add RC, RC, #1 - | beq <6 // Skip holes in hash part. - | stp CARG1, TMP0, [RA] - | add CARG1, RC, TMP1 - | b <3 - break; - - case BC_ISNEXT: - | // RA = base, RC = target (points to ITERN) - | add RA, BASE, RA, lsl #3 - | ldr CFUNC:CARG1, [RA, #-24] - | add RC, PC, RC, lsl #2 - | ldp TAB:CARG3, CARG4, [RA, #-16] - | sub RC, RC, #0x20000 - | checkfunc CFUNC:CARG1, >5 - | asr TMP0, TAB:CARG3, #47 - | ldrb TMP1w, CFUNC:CARG1->ffid - | cmn TMP0, #-LJ_TTAB - | ccmp CARG4, TISNIL, #0, eq - | ccmp TMP1w, #FF_next_N, #0, eq - | bne >5 - | mov TMP0w, #0xfffe7fff // LJ_KEYINDEX - | lsl TMP0, TMP0, #32 - | str TMP0, [RA, #-8] // Initialize control var. - |1: - | mov PC, RC - | ins_next - | - |5: // Despecialize bytecode if any of the checks fail. - |.if JIT - | ldrb TMP2w, [RC, # OFS_OP] - |.endif - | mov TMP0, #BC_JMP - | mov TMP1, #BC_ITERC - | strb TMP0w, [PC, #-4+OFS_OP] - |.if JIT - | cmp TMP2w, #BC_ITERN - | bne >6 - |.endif - | strb TMP1w, [RC, # OFS_OP] - | b <1 - |.if JIT - |6: // Unpatch JLOOP. - | ldr RA, [GL, #GL_J(trace)] - | ldrh TMP2w, [RC, # OFS_RD] - | ldr TRACE:RA, [RA, TMP2, lsl #3] - | ldr TMP2w, TRACE:RA->startins - | bfxil TMP2w, TMP1w, #0, #8 - | str TMP2w, [RC] - | b <1 - |.endif - break; - - case BC_VARG: - | decode_RB RB, INS - | and RC, RC, #255 - | // RA = base, RB = (nresults+1), RC = numparams - | ldr TMP1, [BASE, FRAME_PC] - | add RC, BASE, RC, lsl #3 - | add RA, BASE, RA, lsl #3 - | add RC, RC, #FRAME_VARG - | add TMP2, RA, RB, lsl #3 - | sub RC, RC, TMP1 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | sub TMP3, BASE, #16 // TMP3 = vtop - | cbz RB, >5 - | sub TMP2, TMP2, #16 - |1: // Copy vararg slots to destination slots. - | cmp RC, TMP3 - | ldr TMP0, [RC], #8 - | csel TMP0, TMP0, TISNIL, lo - | cmp RA, TMP2 - | str TMP0, [RA], #8 - | blo <1 - |2: - | ins_next - | - |5: // Copy all varargs. - | ldr TMP0, L->maxstack - | subs TMP2, TMP3, RC - | csel RB, xzr, TMP2, le // MULTRES = (max(vtop-vbase,0)+1)*8 - | add RB, RB, #8 - | add TMP1, RA, TMP2 - | str RBw, SAVE_MULTRES - | ble <2 // Nothing to copy. - | cmp TMP1, TMP0 - | bhi >7 - |6: - | ldr TMP0, [RC], #8 - | str TMP0, [RA], #8 - | cmp RC, TMP3 - | blo <6 - | b <2 - | - |7: // Grow stack for varargs. - | lsr CARG2, TMP2, #3 - | stp BASE, RA, L->base - | mov CARG1, L - | sub RC, RC, BASE // Need delta, because BASE may change. - | str PC, SAVE_PC - | bl extern lj_state_growstack // (lua_State *L, int n) - | ldp BASE, RA, L->base - | add RC, BASE, RC - | sub TMP3, BASE, #16 - | b <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results, RC = extra results - | ldr TMP0w, SAVE_MULTRES - | ldr PC, [BASE, FRAME_PC] - | add RA, BASE, RA, lsl #3 - | add RC, TMP0, RC, lsl #3 - | b ->BC_RETM_Z - break; - - case BC_RET: - | // RA = results, RC = nresults+1 - | ldr PC, [BASE, FRAME_PC] - | lsl RC, RC, #3 - | add RA, BASE, RA, lsl #3 - |->BC_RETM_Z: - | str RCw, SAVE_MULTRES - |1: - | ands CARG1, PC, #FRAME_TYPE - | eor CARG2, PC, #FRAME_VARG - | bne ->BC_RETV2_Z - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RC = (nresults+1)*8, PC = return - | ldr INSw, [PC, #-4] - | subs TMP1, RC, #8 - | sub CARG3, BASE, #16 - | beq >3 - |2: - | ldr TMP0, [RA], #8 - | add BASE, BASE, #8 - | sub TMP1, TMP1, #8 - | str TMP0, [BASE, #-24] - | cbnz TMP1, <2 - |3: - | decode_RA RA, INS - | sub CARG4, CARG3, RA, lsl #3 - | decode_RB RB, INS - | ldr LFUNC:CARG1, [CARG4, FRAME_FUNC] - |5: - | cmp RC, RB, lsl #3 // More results expected? - | blo >6 - | and LFUNC:CARG1, CARG1, #LJ_GCVMASK - | mov BASE, CARG4 - | ldr CARG2, LFUNC:CARG1->pc - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | ins_next - | - |6: // Fill up results with nil. - | add BASE, BASE, #8 - | add RC, RC, #8 - | str TISNIL, [BASE, #-24] - | b <5 - | - |->BC_RETV1_Z: // Non-standard return case. - | add RA, BASE, RA, lsl #3 - |->BC_RETV2_Z: - | tst CARG2, #FRAME_TYPEP - | bne ->vm_return - | // Return from vararg function: relocate BASE down. - | sub BASE, BASE, CARG2 - | ldr PC, [BASE, FRAME_PC] - | b <1 - break; - - case BC_RET0: case BC_RET1: - | // RA = results, RC = nresults+1 - | ldr PC, [BASE, FRAME_PC] - | lsl RC, RC, #3 - | str RCw, SAVE_MULTRES - | ands CARG1, PC, #FRAME_TYPE - | eor CARG2, PC, #FRAME_VARG - | bne ->BC_RETV1_Z - | ldr INSw, [PC, #-4] - if (op == BC_RET1) { - | ldr TMP0, [BASE, RA, lsl #3] - } - | sub CARG4, BASE, #16 - | decode_RA RA, INS - | sub BASE, CARG4, RA, lsl #3 - if (op == BC_RET1) { - | str TMP0, [CARG4], #8 - } - | decode_RB RB, INS - | ldr LFUNC:CARG1, [BASE, FRAME_FUNC] - |5: - | cmp RC, RB, lsl #3 - | blo >6 - | and LFUNC:CARG1, CARG1, #LJ_GCVMASK - | ldr CARG2, LFUNC:CARG1->pc - | ldr KBASE, [CARG2, #PC2PROTO(k)] - | ins_next - | - |6: // Fill up results with nil. - | add RC, RC, #8 - | str TISNIL, [CARG4], #8 - | b <5 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - |.define FOR_IDX, [RA]; .define FOR_TIDX, [RA, #4] - |.define FOR_STOP, [RA, #8]; .define FOR_TSTOP, [RA, #12] - |.define FOR_STEP, [RA, #16]; .define FOR_TSTEP, [RA, #20] - |.define FOR_EXT, [RA, #24]; .define FOR_TEXT, [RA, #28] - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base, RC = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | add RA, BASE, RA, lsl #3 - | ldp CARG1, CARG2, FOR_IDX // CARG1 = IDX, CARG2 = STOP - | ldr CARG3, FOR_STEP // CARG3 = STEP - if (op != BC_JFORL) { - | add RC, PC, RC, lsl #2 - | sub RC, RC, #0x20000 - } - | checkint CARG1, >5 - if (!vk) { - | checkint CARG2, ->vmeta_for - | checkint CARG3, ->vmeta_for - | tbnz CARG3w, #31, >4 - | cmp CARG1w, CARG2w - } else { - | adds CARG1w, CARG1w, CARG3w - | bvs >2 - | add TMP0, CARG1, TISNUM - | tbnz CARG3w, #31, >4 - | cmp CARG1w, CARG2w - } - |1: - if (op == BC_FORI) { - | csel PC, RC, PC, gt - } else if (op == BC_JFORI) { - | mov PC, RC - | ldrh RCw, [RC, #-4+OFS_RD] - } else if (op == BC_IFORL) { - | csel PC, RC, PC, le - } - if (vk) { - | str TMP0, FOR_IDX - | str TMP0, FOR_EXT - } else { - | str CARG1, FOR_EXT - } - if (op == BC_JFORI || op == BC_JFORL) { - | ble =>BC_JLOOP - } - |2: - | ins_next - | - |4: // Invert check for negative step. - | cmp CARG2w, CARG1w - | b <1 - | - |5: // FP loop. - | ldp d0, d1, FOR_IDX - | blo ->vmeta_for - if (!vk) { - | checknum CARG2, ->vmeta_for - | checknum CARG3, ->vmeta_for - | str d0, FOR_EXT - } else { - | ldr d2, FOR_STEP - | fadd d0, d0, d2 - } - | tbnz CARG3, #63, >7 - | fcmp d0, d1 - |6: - if (vk) { - | str d0, FOR_IDX - | str d0, FOR_EXT - } - if (op == BC_FORI) { - | csel PC, RC, PC, hi - } else if (op == BC_JFORI) { - | ldrh RCw, [RC, #-4+OFS_RD] - | bls =>BC_JLOOP - } else if (op == BC_IFORL) { - | csel PC, RC, PC, ls - } else { - | bls =>BC_JLOOP - } - | b <2 - | - |7: // Invert check for negative step. - | fcmp d1, d0 - | b <6 - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base, RC = target - | ldr CARG1, [BASE, RA, lsl #3] - | add TMP1, BASE, RA, lsl #3 - | cmp CARG1, TISNIL - | beq >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | str CARG1, [TMP1, #-8] - | b =>BC_JLOOP - } else { - | add TMP0, PC, RC, lsl #2 // Otherwise save control var + branch. - | sub PC, TMP0, #0x20000 - | str CARG1, [TMP1, #-8] - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base, RC = target (loop extent) - | // Note: RA/RC is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base, RC = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base (ignored), RC = traceno - | ldr CARG1, [GL, #GL_J(trace)] - | mov CARG2w, #0 // Traces on ARM64 don't store the trace #, so use 0. - | ldr TRACE:RC, [CARG1, RC, lsl #3] - | st_vmstate CARG2w - | ldr RA, TRACE:RC->mcode - | str BASE, GL->jit_base - | str L, GL->tmpbuf.L - | sub sp, sp, #16 // See SPS_FIXED. Avoids sp adjust in every root trace. - | br RA - |.endif - break; - - case BC_JMP: - | // RA = base (only used by trace recorder), RC = target - | add RC, PC, RC, lsl #2 - | sub PC, RC, #0x20000 - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 - | ldr CARG1, L->maxstack - | ldrb TMP1w, [PC, #-4+PC2PROTO(numparams)] - | ldr KBASE, [PC, #-4+PC2PROTO(k)] - | cmp RA, CARG1 - | bhi ->vm_growstack_l - |2: - | cmp NARGS8:RC, TMP1, lsl #3 // Check for missing parameters. - | blo >3 - if (op == BC_JFUNCF) { - | decode_RD RC, INS - | b =>BC_JLOOP - } else { - | ins_next - } - | - |3: // Clear missing parameters. - | str TISNIL, [BASE, NARGS8:RC] - | add NARGS8:RC, NARGS8:RC, #8 - | b <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = LFUNC, RC = nargs*8 - | ldr CARG1, L->maxstack - | movn TMP0, #~LJ_TFUNC - | add TMP2, BASE, RC - | add LFUNC:CARG3, CARG3, TMP0, lsl #47 - | add RA, RA, RC - | add TMP0, RC, #16+FRAME_VARG - | str LFUNC:CARG3, [TMP2], #8 // Store (tagged) copy of LFUNC. - | ldr KBASE, [PC, #-4+PC2PROTO(k)] - | cmp RA, CARG1 - | str TMP0, [TMP2], #8 // Store delta + FRAME_VARG. - | bhs ->vm_growstack_l - | sub RC, TMP2, #16 - | ldrb TMP1w, [PC, #-4+PC2PROTO(numparams)] - | mov RA, BASE - | mov BASE, TMP2 - | cbz TMP1, >2 - |1: - | cmp RA, RC // Less args than parameters? - | bhs >3 - | ldr TMP0, [RA] - | sub TMP1, TMP1, #1 - | str TISNIL, [RA], #8 // Clear old fixarg slot (help the GC). - | str TMP0, [TMP2], #8 - | cbnz TMP1, <1 - |2: - | ins_next - | - |3: - | sub TMP1, TMP1, #1 - | str TISNIL, [TMP2], #8 - | cbz TMP1, <2 - | b <3 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, CARG3 = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | ldr CARG4, CFUNC:CARG3->f - } else { - | ldr CARG4, GL->wrapf - } - | add CARG2, RA, NARGS8:RC - | ldr CARG1, L->maxstack - | add RC, BASE, NARGS8:RC - | cmp CARG2, CARG1 - | stp BASE, RC, L->base - if (op == BC_FUNCCW) { - | ldr CARG2, CFUNC:CARG3->f - } - | mv_vmstate TMP0w, C - | mov CARG1, L - | bhi ->vm_growstack_c // Need to grow stack. - | st_vmstate TMP0w - | blr CARG4 // (lua_State *L [, lua_CFunction f]) - | // Returns nresults. - | ldp BASE, TMP1, L->base - | str L, GL->cur_L - | sbfiz RC, CRET1, #3, #32 - | st_vmstate ST_INTERP - | ldr PC, [BASE, FRAME_PC] - | sub RA, TMP1, RC // RA = L->top - nresults*8 - | b ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",%%progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 30\n" /* Return address is in lr. */ - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 16\n" /* def_cfa fp 16 */ - "\t.align 3\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.quad .Lbegin\n" - "\t.quad %d\n" - "\t.byte 0x9e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x9d\n\t.uleb128 2\n", /* offset fp */ - fcofs); - for (i = 19; i <= 28; i++) /* offset x19-x28 */ - fprintf(ctx->fp, "\t.byte 0x%x\n\t.uleb128 %d\n", 0x80+i, i+(3-19)); - for (i = 8; i <= 15; i++) /* offset d8-d15 */ - fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 0x%x\n\t.uleb128 %d\n", - 64+i, i+(3+(28-19+1)-8)); - fprintf(ctx->fp, - "\t.align 3\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" - "\t.quad lj_vm_ffi_call\n" - "\t.quad %d\n" - "\t.byte 0x9e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x9d\n\t.uleb128 2\n" /* offset fp */ - "\t.byte 0x93\n\t.uleb128 3\n" /* offset x19 */ - "\t.byte 0x94\n\t.uleb128 4\n" /* offset x20 */ - "\t.align 3\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",%%progbits\n"); - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 30\n" /* Return address is in lr. */ - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 16\n" /* def_cfa fp 16 */ - "\t.align 3\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.long .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.long .LASFDE2-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x9e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x9d\n\t.uleb128 2\n", /* offset fp */ - fcofs); - for (i = 19; i <= 28; i++) /* offset x19-x28 */ - fprintf(ctx->fp, "\t.byte 0x%x\n\t.uleb128 %d\n", 0x80+i, i+(3-19)); - for (i = 8; i <= 15; i++) /* offset d8-d15 */ - fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 0x%x\n\t.uleb128 %d\n", - 64+i, i+(3+(28-19+1)-8)); - fprintf(ctx->fp, - "\t.align 3\n" - ".LEFDE2:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.long .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 30\n" /* Return address is in lr. */ - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 16\n" /* def_cfa fp 16 */ - "\t.align 3\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.long .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.long .LASFDE3-.Lframe2\n" - "\t.long lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x9e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x9d\n\t.uleb128 2\n" /* offset fp */ - "\t.byte 0x93\n\t.uleb128 3\n" /* offset x19 */ - "\t.byte 0x94\n\t.uleb128 4\n" /* offset x20 */ - "\t.align 3\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif -#endif - break; -#if !LJ_NO_UNWIND - case BUILD_machasm: { -#if LJ_HASFFI - int fcsize = 0; -#endif - int j; - fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n"); - fprintf(ctx->fp, - "EH_frame1:\n" - "\t.set L$set$x,LECIEX-LSCIEX\n" - "\t.long L$set$x\n" - "LSCIEX:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zPR\\0\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 30\n" /* Return address is in lr. */ - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */ - "\t.long _lj_err_unwind_dwarf@GOT-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 16\n" /* def_cfa fp 16 */ - "\t.align 3\n" - "LECIEX:\n\n"); - for (j = 0; j < ctx->nsym; j++) { - const char *name = ctx->sym[j].name; - int32_t size = ctx->sym[j+1].ofs - ctx->sym[j].ofs; - if (size == 0) continue; -#if LJ_HASFFI - if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; } -#endif - fprintf(ctx->fp, - "LSFDE%d:\n" - "\t.set L$set$%d,LEFDE%d-LASFDE%d\n" - "\t.long L$set$%d\n" - "LASFDE%d:\n" - "\t.long LASFDE%d-EH_frame1\n" - "\t.long %s-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x9e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x9d\n\t.uleb128 2\n", /* offset fp */ - j, j, j, j, j, j, j, name, size); - for (i = 19; i <= 28; i++) /* offset x19-x28 */ - fprintf(ctx->fp, "\t.byte 0x%x\n\t.uleb128 %d\n", 0x80+i, i+(3-19)); - for (i = 8; i <= 15; i++) /* offset d8-d15 */ - fprintf(ctx->fp, "\t.byte 5\n\t.uleb128 0x%x\n\t.uleb128 %d\n", - 64+i, i+(3+(28-19+1)-8)); - fprintf(ctx->fp, - "\t.align 3\n" - "LEFDE%d:\n\n", j); - } -#if LJ_HASFFI - if (fcsize) { - fprintf(ctx->fp, - "EH_frame2:\n" - "\t.set L$set$y,LECIEY-LSCIEY\n" - "\t.long L$set$y\n" - "LSCIEY:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zR\\0\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 30\n" /* Return address is in lr. */ - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 16\n" /* def_cfa fp 16 */ - "\t.align 3\n" - "LECIEY:\n\n"); - fprintf(ctx->fp, - "LSFDEY:\n" - "\t.set L$set$yy,LEFDEY-LASFDEY\n" - "\t.long L$set$yy\n" - "LASFDEY:\n" - "\t.long LASFDEY-EH_frame2\n" - "\t.long _lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x9e\n\t.uleb128 1\n" /* offset lr */ - "\t.byte 0x9d\n\t.uleb128 2\n" /* offset fp */ - "\t.byte 0x93\n\t.uleb128 3\n" /* offset x19 */ - "\t.byte 0x94\n\t.uleb128 4\n" /* offset x20 */ - "\t.align 3\n" - "LEFDEY:\n\n", fcsize); - } -#endif - fprintf(ctx->fp, ".subsections_via_symbols\n"); - } - break; -#endif - default: - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips.dasc deleted file mode 100644 index 34645bf..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips.dasc +++ /dev/null @@ -1,5392 +0,0 @@ -|// Low-level VM code for MIPS CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -|// -|// MIPS soft-float support contributed by Djordje Kovacevic and -|// Stefan Pejic from RT-RK.com, sponsored by Cisco Systems, Inc. -| -|.arch mips -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -|// Don't use: r0 = 0, r26/r27 = reserved, r28 = gp, r29 = sp, r31 = ra -| -|.macro .FPU, a, b -|.if FPU -| a, b -|.endif -|.endmacro -| -|// The following must be C callee-save (but BASE is often refetched). -|.define BASE, r16 // Base of current Lua stack frame. -|.define KBASE, r17 // Constants of current Lua function. -|.define PC, r18 // Next PC. -|.define DISPATCH, r19 // Opcode dispatch table. -|.define LREG, r20 // Register holding lua_State (also in SAVE_L). -|.define MULTRES, r21 // Size of multi-result: (nresults+1)*8. -| -|.define JGL, r30 // On-trace: global_State + 32768. -| -|// Constants for type-comparisons, stores and conversions. C callee-save. -|.define TISNUM, r22 -|.define TISNIL, r30 -|.if FPU -|.define TOBIT, f30 // 2^52 + 2^51. -|.endif -| -|// The following temporaries are not saved across C calls, except for RA. -|.define RA, r23 // Callee-save. -|.define RB, r8 -|.define RC, r9 -|.define RD, r10 -|.define INS, r11 -| -|.define AT, r1 // Assembler temporary. -|.define TMP0, r12 -|.define TMP1, r13 -|.define TMP2, r14 -|.define TMP3, r15 -| -|// MIPS o32 calling convention. -|.define CFUNCADDR, r25 -|.define CARG1, r4 -|.define CARG2, r5 -|.define CARG3, r6 -|.define CARG4, r7 -| -|.define CRET1, r2 -|.define CRET2, r3 -| -|.if ENDIAN_LE -|.define SFRETLO, CRET1 -|.define SFRETHI, CRET2 -|.define SFARG1LO, CARG1 -|.define SFARG1HI, CARG2 -|.define SFARG2LO, CARG3 -|.define SFARG2HI, CARG4 -|.else -|.define SFRETLO, CRET2 -|.define SFRETHI, CRET1 -|.define SFARG1LO, CARG2 -|.define SFARG1HI, CARG1 -|.define SFARG2LO, CARG4 -|.define SFARG2HI, CARG3 -|.endif -| -|.if FPU -|.define FARG1, f12 -|.define FARG2, f14 -| -|.define FRET1, f0 -|.define FRET2, f2 -|.endif -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.if FPU // MIPS32 hard-float. -| -|.define CFRAME_SPACE, 112 // Delta for sp. -| -|.define SAVE_ERRF, 124(sp) // 32 bit C frame info. -|.define SAVE_NRES, 120(sp) -|.define SAVE_CFRAME, 116(sp) -|.define SAVE_L, 112(sp) -|//----- 8 byte aligned, ^^^^ 16 byte register save area, owned by interpreter. -|.define SAVE_GPR_, 72 // .. 72+10*4: 32 bit GPR saves. -|.define SAVE_FPR_, 24 // .. 24+6*8: 64 bit FPR saves. -| -|.else // MIPS32 soft-float -| -|.define CFRAME_SPACE, 64 // Delta for sp. -| -|.define SAVE_ERRF, 76(sp) // 32 bit C frame info. -|.define SAVE_NRES, 72(sp) -|.define SAVE_CFRAME, 68(sp) -|.define SAVE_L, 64(sp) -|//----- 8 byte aligned, ^^^^ 16 byte register save area, owned by interpreter. -|.define SAVE_GPR_, 24 // .. 24+10*4: 32 bit GPR saves. -| -|.endif -| -|.define SAVE_PC, 20(sp) -|.define ARG5, 16(sp) -|.define CSAVE_4, 12(sp) -|.define CSAVE_3, 8(sp) -|.define CSAVE_2, 4(sp) -|.define CSAVE_1, 0(sp) -|//----- 8 byte aligned, ^^^^ 16 byte register save area, owned by callee. -| -|.define ARG5_OFS, 16 -|.define SAVE_MULTRES, ARG5 -| -|//----------------------------------------------------------------------- -| -|.macro saveregs -| addiu sp, sp, -CFRAME_SPACE -| sw ra, SAVE_GPR_+9*4(sp) -| sw r30, SAVE_GPR_+8*4(sp) -| .FPU sdc1 f30, SAVE_FPR_+5*8(sp) -| sw r23, SAVE_GPR_+7*4(sp) -| sw r22, SAVE_GPR_+6*4(sp) -| .FPU sdc1 f28, SAVE_FPR_+4*8(sp) -| sw r21, SAVE_GPR_+5*4(sp) -| sw r20, SAVE_GPR_+4*4(sp) -| .FPU sdc1 f26, SAVE_FPR_+3*8(sp) -| sw r19, SAVE_GPR_+3*4(sp) -| sw r18, SAVE_GPR_+2*4(sp) -| .FPU sdc1 f24, SAVE_FPR_+2*8(sp) -| sw r17, SAVE_GPR_+1*4(sp) -| sw r16, SAVE_GPR_+0*4(sp) -| .FPU sdc1 f22, SAVE_FPR_+1*8(sp) -| .FPU sdc1 f20, SAVE_FPR_+0*8(sp) -|.endmacro -| -|.macro restoreregs_ret -| lw ra, SAVE_GPR_+9*4(sp) -| lw r30, SAVE_GPR_+8*4(sp) -| .FPU ldc1 f30, SAVE_FPR_+5*8(sp) -| lw r23, SAVE_GPR_+7*4(sp) -| lw r22, SAVE_GPR_+6*4(sp) -| .FPU ldc1 f28, SAVE_FPR_+4*8(sp) -| lw r21, SAVE_GPR_+5*4(sp) -| lw r20, SAVE_GPR_+4*4(sp) -| .FPU ldc1 f26, SAVE_FPR_+3*8(sp) -| lw r19, SAVE_GPR_+3*4(sp) -| lw r18, SAVE_GPR_+2*4(sp) -| .FPU ldc1 f24, SAVE_FPR_+2*8(sp) -| lw r17, SAVE_GPR_+1*4(sp) -| lw r16, SAVE_GPR_+0*4(sp) -| .FPU ldc1 f22, SAVE_FPR_+1*8(sp) -| .FPU ldc1 f20, SAVE_FPR_+0*8(sp) -| jr ra -| addiu sp, sp, CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; .long 0xec1cf0f0; .endmacro -| -|// Macros to mark delay slots. -|.macro ., a; a; .endmacro -|.macro ., a,b; a,b; .endmacro -|.macro ., a,b,c; a,b,c; .endmacro -| -|//----------------------------------------------------------------------- -| -|// Endian-specific defines. -|.if ENDIAN_LE -|.define FRAME_PC, -4 -|.define FRAME_FUNC, -8 -|.define HI, 4 -|.define LO, 0 -|.define OFS_RD, 2 -|.define OFS_RA, 1 -|.define OFS_OP, 0 -|.else -|.define FRAME_PC, -8 -|.define FRAME_FUNC, -4 -|.define HI, 0 -|.define LO, 4 -|.define OFS_RD, 0 -|.define OFS_RA, 2 -|.define OFS_OP, 3 -|.endif -| -|// Instruction decode. -|.macro decode_OP1, dst, ins; andi dst, ins, 0xff; .endmacro -|.macro decode_OP4a, dst, ins; andi dst, ins, 0xff; .endmacro -|.macro decode_OP4b, dst; sll dst, dst, 2; .endmacro -|.macro decode_RC4a, dst, ins; srl dst, ins, 14; .endmacro -|.macro decode_RC4b, dst; andi dst, dst, 0x3fc; .endmacro -|.macro decode_RD4b, dst; sll dst, dst, 2; .endmacro -|.macro decode_RA8a, dst, ins; srl dst, ins, 5; .endmacro -|.macro decode_RA8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RB8a, dst, ins; srl dst, ins, 21; .endmacro -|.macro decode_RB8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RD8a, dst, ins; srl dst, ins, 16; .endmacro -|.macro decode_RD8b, dst; sll dst, dst, 3; .endmacro -|.macro decode_RDtoRC8, dst, src; andi dst, src, 0x7f8; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| lw INS, 0(PC) -| addiu PC, PC, 4 -|.endmacro -|// Instruction decode+dispatch. -|.macro ins_NEXT2 -| decode_OP4a TMP1, INS -| decode_OP4b TMP1 -| addu TMP0, DISPATCH, TMP1 -| decode_RD8a RD, INS -| lw AT, 0(TMP0) -| decode_RA8a RA, INS -| decode_RD8b RD -| jr AT -| decode_RA8b RA -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| lw PC, LFUNC:RB->pc -| lw INS, 0(PC) -| addiu PC, PC, 4 -| decode_OP4a TMP1, INS -| decode_RA8a RA, INS -| decode_OP4b TMP1 -| decode_RA8b RA -| addu TMP0, DISPATCH, TMP1 -| lw TMP0, 0(TMP0) -| jr TMP0 -| addu RA, RA, BASE -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| sw PC, FRAME_PC(BASE) -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|.macro branch_RD -| srl TMP0, RD, 1 -| lui AT, (-(BCBIAS_J*4 >> 16) & 65535) -| addu TMP0, TMP0, AT -| addu PC, PC, TMP0 -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -#define GG_DISP2GOT (GG_OFS(got) - GG_OFS(dispatch)) -#define DISPATCH_GOT(name) (GG_DISP2GOT + 4*LJ_GOT_##name) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro load_got, func -| lw CFUNCADDR, DISPATCH_GOT(func)(DISPATCH) -|.endmacro -|// Much faster. Sadly, there's no easy way to force the required code layout. -|// .macro call_intern, func; bal extern func; .endmacro -|.macro call_intern, func; jalr CFUNCADDR; .endmacro -|.macro call_extern; jalr CFUNCADDR; .endmacro -|.macro jmp_extern; jr CFUNCADDR; .endmacro -| -|.macro hotcheck, delta, target -| srl TMP1, PC, 1 -| andi TMP1, TMP1, 126 -| addu TMP1, TMP1, DISPATCH -| lhu TMP2, GG_DISP2HOT(TMP1) -| addiu TMP2, TMP2, -delta -| bltz TMP2, target -|. sh TMP2, GG_DISP2HOT(TMP1) -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP, ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL, ->vm_hotcall -|.endmacro -| -|// Set current VM state. Uses TMP0. -|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro -|.macro st_vmstate; sw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp, target -| lw tmp, DISPATCH_GL(gc.grayagain)(DISPATCH) -| andi mark, mark, ~LJ_GC_BLACK & 255 // black2gray(tab) -| sw tab, DISPATCH_GL(gc.grayagain)(DISPATCH) -| sb mark, tab->marked -| b target -|. sw tmp, tab->gclist -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: TMP2 = previous base. - | andi AT, PC, FRAME_P - | beqz AT, ->cont_dispatch - |. li TMP1, LJ_TTRUE - | - | // Return from pcall or xpcall fast func. - | lw PC, FRAME_PC(TMP2) // Fetch PC of previous frame. - | move BASE, TMP2 // Restore caller base. - | // Prepending may overwrite the pcall frame, so do it at the end. - | sw TMP1, FRAME_PC(RA) // Prepend true to results. - | addiu RA, RA, -8 - | - |->vm_returnc: - | addiu RD, RD, 8 // RD = (nresults+1)*8. - | andi TMP0, PC, FRAME_TYPE - | beqz RD, ->vm_unwind_c_eh - |. li CRET1, LUA_YIELD - | beqz TMP0, ->BC_RET_Z // Handle regular return to Lua. - |. move MULTRES, RD - | - |->vm_return: - | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return - | // TMP0 = PC & FRAME_TYPE - | li TMP2, -8 - | xori AT, TMP0, FRAME_C - | and TMP2, PC, TMP2 - | bnez AT, ->vm_returnp - |. subu TMP2, BASE, TMP2 // TMP2 = previous base. - | - | addiu TMP1, RD, -8 - | sw TMP2, L->base - | li_vmstate C - | lw TMP2, SAVE_NRES - | addiu BASE, BASE, -8 - | st_vmstate - | beqz TMP1, >2 - |. sll TMP2, TMP2, 3 - |1: - | addiu TMP1, TMP1, -8 - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | addiu RA, RA, 8 - | sw SFRETHI, HI(BASE) - | sw SFRETLO, LO(BASE) - | bnez TMP1, <1 - |. addiu BASE, BASE, 8 - | - |2: - | bne TMP2, RD, >6 - |3: - |. sw BASE, L->top // Store new top. - | - |->vm_leave_cp: - | lw TMP0, SAVE_CFRAME // Restore previous C frame. - | move CRET1, r0 // Ok return status for vm_pcall. - | sw TMP0, L->cframe - | - |->vm_leave_unw: - | restoreregs_ret - | - |6: - | lw TMP1, L->maxstack - | slt AT, TMP2, RD - | bnez AT, >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - |. slt AT, BASE, TMP1 - | beqz AT, >8 - |. nop - | sw TISNIL, HI(BASE) - | addiu RD, RD, 8 - | b <2 - |. addiu BASE, BASE, 8 - | - |7: // Less results wanted. - | subu TMP0, RD, TMP2 - | subu TMP0, BASE, TMP0 // Either keep top or shrink it. - | b <3 - |. movn BASE, TMP0, TMP2 // LUA_MULTRET+1 case? - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | load_got lj_state_growstack - | move MULTRES, RD - | srl CARG2, TMP2, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw TMP2, SAVE_NRES - | lw BASE, L->top // Need the (realloced) L->top in BASE. - | move RD, MULTRES - | b <2 - |. sll TMP2, TMP2, 3 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | move sp, CARG1 - | move CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | lw L, SAVE_L - | li TMP0, ~LJ_VMST_C - | lw GL:TMP1, L->glref - | b ->vm_leave_unw - |. sw TMP0, GL:TMP1->vmstate - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | li AT, -4 - | and sp, CARG1, AT - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | lw L, SAVE_L - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | li TISNIL, LJ_TNIL - | lw BASE, L->base - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | .FPU mtc1 TMP3, TOBIT - | li TMP1, LJ_TFALSE - | li_vmstate INTERP - | lw PC, FRAME_PC(BASE) // Fetch PC of previous frame. - | .FPU cvt.d.s TOBIT, TOBIT - | addiu RA, BASE, -8 // Results start at BASE-8. - | addiu DISPATCH, DISPATCH, GG_G2DISP - | sw TMP1, HI(RA) // Prepend false to error message. - | st_vmstate - | b ->vm_returnc - |. li RD, 16 // 2 results: false + error message. - | - |->vm_unwind_stub: // Jump to exit stub from unwinder. - | jr CARG1 - |. move ra, CARG2 - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | b >2 - |. li CARG2, LUA_MINSTACK - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | addu RC, BASE, RC - | subu RA, RA, BASE - | sw BASE, L->base - | addiu PC, PC, 4 // Must point after first instruction. - | sw RC, L->top - | srl CARG2, RA, 3 - |2: - | // L->base = new base, L->top = top - | load_got lj_state_growstack - | sw PC, SAVE_PC - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw BASE, L->base - | lw RC, L->top - | lw LFUNC:RB, FRAME_FUNC(BASE) - | subu RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | move L, CARG1 - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | move BASE, CARG2 - | lbu TMP1, L->status - | sw L, SAVE_L - | li PC, FRAME_CP - | addiu TMP0, sp, CFRAME_RESUME - | addiu DISPATCH, DISPATCH, GG_G2DISP - | sw r0, SAVE_NRES - | sw r0, SAVE_ERRF - | sw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sw r0, SAVE_CFRAME - | beqz TMP1, >3 - |. sw TMP0, L->cframe - | - | // Resume after yield (like a return). - | sw L, DISPATCH_GL(cur_L)(DISPATCH) - | move RA, BASE - | lw BASE, L->base - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lw TMP1, L->top - | lw PC, FRAME_PC(BASE) - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | subu RD, TMP1, BASE - | .FPU mtc1 TMP3, TOBIT - | sb r0, L->status - | .FPU cvt.d.s TOBIT, TOBIT - | li_vmstate INTERP - | addiu RD, RD, 8 - | st_vmstate - | move MULTRES, RD - | andi TMP0, PC, FRAME_TYPE - | beqz TMP0, ->BC_RET_Z - |. li TISNIL, LJ_TNIL - | b ->vm_return - |. nop - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | sw CARG4, SAVE_ERRF - | b >1 - |. li PC, FRAME_CP - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | li PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | lw TMP1, L:CARG1->cframe - | move L, CARG1 - | sw CARG3, SAVE_NRES - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | sw CARG1, SAVE_L - | move BASE, CARG2 - | addiu DISPATCH, DISPATCH, GG_G2DISP - | sw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sw TMP1, SAVE_CFRAME - | sw sp, L->cframe // Add our C frame to cframe chain. - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | sw L, DISPATCH_GL(cur_L)(DISPATCH) - | lw TMP2, L->base // TMP2 = old base (used in vmeta_call). - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | lw TMP1, L->top - | .FPU mtc1 TMP3, TOBIT - | addu PC, PC, BASE - | subu NARGS8:RC, TMP1, BASE - | subu PC, PC, TMP2 // PC = frame delta + frame type - | .FPU cvt.d.s TOBIT, TOBIT - | li_vmstate INTERP - | li TISNIL, LJ_TNIL - | st_vmstate - | - |->vm_call_dispatch: - | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC - | lw TMP0, FRAME_PC(BASE) - | li AT, LJ_TFUNC - | bne TMP0, AT, ->vmeta_call - |. lw LFUNC:RB, FRAME_FUNC(BASE) - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | move L, CARG1 - | lw TMP0, L:CARG1->stack - | sw CARG1, SAVE_L - | lw TMP1, L->top - | lw DISPATCH, L->glref // Setup pointer to dispatch table. - | sw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | subu TMP0, TMP0, TMP1 // Compute -savestack(L, L->top). - | lw TMP1, L->cframe - | addiu DISPATCH, DISPATCH, GG_G2DISP - | sw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame. - | sw r0, SAVE_ERRF // No error function. - | sw TMP1, SAVE_CFRAME - | sw sp, L->cframe // Add our C frame to cframe chain. - | sw L, DISPATCH_GL(cur_L)(DISPATCH) - | jalr CARG4 // (lua_State *L, lua_CFunction func, void *ud) - |. move CFUNCADDR, CARG4 - | move BASE, CRET1 - | bnez CRET1, <3 // Else continue with the call. - |. li PC, FRAME_CP - | b ->vm_leave_cp // No base? Just remove C frame. - |. nop - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the - |// stack, so BASE doesn't need to be reloaded across these calls. - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8 - | lw TMP0, -16+LO(BASE) // Continuation. - | move RB, BASE - | move BASE, TMP2 // Restore caller BASE. - | lw LFUNC:TMP1, FRAME_FUNC(TMP2) - |.if FFI - | sltiu AT, TMP0, 2 - |.endif - | lw PC, -16+HI(RB) // Restore PC from [cont|PC]. - | addu TMP2, RA, RD - |.if FFI - | bnez AT, >1 - |.endif - |. sw TISNIL, -8+HI(TMP2) // Ensure one valid arg. - | lw TMP1, LFUNC:TMP1->pc - | // BASE = base, RA = resultptr, RB = meta base - | jr TMP0 // Jump to continuation. - |. lw KBASE, PC2PROTO(k)(TMP1) - | - |.if FFI - |1: - | bnez TMP0, ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - |. addiu TMP1, RB, -16 - | b ->vm_call_tail - |. subu RC, TMP1, BASE - |.endif - | - |->cont_cat: // RA = resultptr, RB = meta base - | lw INS, -4(PC) - | addiu CARG2, RB, -16 - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | decode_RB8a MULTRES, INS - | decode_RA8a RA, INS - | decode_RB8b MULTRES - | decode_RA8b RA - | addu TMP1, BASE, MULTRES - | sw BASE, L->base - | subu CARG3, CARG2, TMP1 - | sw SFRETHI, HI(CARG2) - | bne TMP1, CARG2, ->BC_CAT_Z - |. sw SFRETLO, LO(CARG2) - | addu RA, BASE, RA - | sw SFRETHI, HI(RA) - | b ->cont_nop - |. sw SFRETLO, LO(RA) - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP0, HI(CARG3) - | - |->vmeta_tgets: - | addiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TTAB - | sw TAB:RB, LO(CARG2) - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv2) - | sw TMP0, HI(CARG2) - | li TMP1, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP1, HI(CARG3) - | - |->vmeta_tgetb: // TMP0 = index - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | sw TMP0, LO(CARG3) - | sw TISNUM, HI(CARG3) - | - |->vmeta_tgetv: - |1: - | load_got lj_meta_tget - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - |. move CARG1, L - | // Returns TValue * (finished) or NULL (metamethod). - | beqz CRET1, >3 - |. addiu TMP1, BASE, -FRAME_CONT - | lw SFARG1HI, HI(CRET1) - | lw SFARG2HI, LO(CRET1) - | ins_next1 - | sw SFARG1HI, HI(RA) - | sw SFARG2HI, LO(RA) - | ins_next2 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | lw BASE, L->top - | sw PC, -16+HI(BASE) // [cont|PC] - | subu PC, BASE, TMP1 - | lw LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | b ->vm_call_dispatch_f - |. li NARGS8:RC, 16 // 2 args for func(t, k). - | - |->vmeta_tgetr: - | load_got lj_tab_getinth - | call_intern lj_tab_getinth // (GCtab *t, int32_t key) - |. nop - | // Returns cTValue * or NULL. - | beqz CRET1, ->BC_TGETR_Z - |. move SFARG2HI, TISNIL - | lw SFARG2HI, HI(CRET1) - | b ->BC_TGETR_Z - |. lw SFARG2LO, LO(CRET1) - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP0, HI(CARG3) - | - |->vmeta_tsets: - | addiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TTAB - | sw TAB:RB, LO(CARG2) - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv2) - | sw TMP0, HI(CARG2) - | li TMP1, LJ_TSTR - | sw STR:RC, LO(CARG3) - | b >1 - |. sw TMP1, HI(CARG3) - | - |->vmeta_tsetb: // TMP0 = index - | addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | sw TMP0, LO(CARG3) - | sw TISNUM, HI(CARG3) - | - |->vmeta_tsetv: - |1: - | load_got lj_meta_tset - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - |. move CARG1, L - | // Returns TValue * (finished) or NULL (metamethod). - | lw SFARG1HI, HI(RA) - | beqz CRET1, >3 - |. lw SFARG1LO, LO(RA) - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | ins_next1 - | sw SFARG1HI, HI(CRET1) - | sw SFARG1LO, LO(CRET1) - | ins_next2 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | addiu TMP1, BASE, -FRAME_CONT - | lw BASE, L->top - | sw PC, -16+HI(BASE) // [cont|PC] - | subu PC, BASE, TMP1 - | lw LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | sw SFARG1HI, 16+HI(BASE) // Copy value to third argument. - | sw SFARG1LO, 16+LO(BASE) - | b ->vm_call_dispatch_f - |. li NARGS8:RC, 24 // 3 args for func(t, k, v) - | - |->vmeta_tsetr: - | load_got lj_tab_setinth - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - |. move CARG1, L - | // Returns TValue *. - | b ->BC_TSETR_Z - |. nop - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | // RA/RD point to o1/o2. - | move CARG2, RA - | move CARG3, RD - | load_got lj_meta_comp - | addiu PC, PC, -4 - | sw BASE, L->base - | sw PC, SAVE_PC - | decode_OP1 CARG4, INS - | call_intern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - |3: - | sltiu AT, CRET1, 2 - | beqz AT, ->vmeta_binop - | negu TMP2, CRET1 - |4: - | lhu RD, OFS_RD(PC) - | addiu PC, PC, 4 - | lui TMP1, (-(BCBIAS_J*4 >> 16) & 65535) - | sll RD, RD, 2 - | addu RD, RD, TMP1 - | and RD, RD, TMP2 - | addu PC, PC, RD - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | lbu TMP1, -4+OFS_RA(PC) - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | sll TMP1, TMP1, 3 - | addu TMP1, BASE, TMP1 - | sw SFRETHI, HI(TMP1) - | b ->cont_nop - |. sw SFRETLO, LO(TMP1) - | - |->cont_condt: // RA = resultptr - | lw TMP0, HI(RA) - | sltiu AT, TMP0, LJ_TISTRUECOND - | b <4 - |. negu TMP2, AT // Branch if result is true. - | - |->cont_condf: // RA = resultptr - | lw TMP0, HI(RA) - | sltiu AT, TMP0, LJ_TISTRUECOND - | b <4 - |. addiu TMP2, AT, -1 // Branch if result is false. - | - |->vmeta_equal: - | // SFARG1LO/SFARG2LO point to o1/o2. TMP0 is set to 0/1. - | load_got lj_meta_equal - | move CARG2, SFARG1LO - | move CARG3, SFARG2LO - | move CARG4, TMP0 - | addiu PC, PC, -4 - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |. nop - | - |->vmeta_equal_cd: - |.if FFI - | load_got lj_meta_equal_cd - | move CARG2, INS - | addiu PC, PC, -4 - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_equal_cd // (lua_State *L, BCIns op) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |. nop - |.endif - | - |->vmeta_istype: - | load_got lj_meta_istype - | addiu PC, PC, -4 - | sw BASE, L->base - | srl CARG2, RA, 3 - | srl CARG3, RD, 3 - | sw PC, SAVE_PC - | call_intern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - |. move CARG1, L - | b ->cont_nop - |. nop - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_unm: - | move RC, RB - | - |->vmeta_arith: - | load_got lj_meta_arith - | decode_OP1 TMP0, INS - | sw BASE, L->base - | move CARG2, RA - | sw PC, SAVE_PC - | move CARG3, RB - | move CARG4, RC - | sw TMP0, ARG5 - | call_intern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - |. move CARG1, L - | // Returns NULL (finished) or TValue * (metamethod). - | beqz CRET1, ->cont_nop - |. nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | subu TMP1, CRET1, BASE - | sw PC, -16+HI(CRET1) // [cont|PC] - | move TMP2, BASE - | addiu PC, TMP1, FRAME_CONT - | move BASE, CRET1 - | b ->vm_call_dispatch - |. li NARGS8:RC, 16 // 2 args for func(o1, o2). - | - |->vmeta_len: - | // CARG2 already set by BC_LEN. -#if LJ_52 - | move MULTRES, CARG1 -#endif - | load_got lj_meta_len - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_meta_len // (lua_State *L, TValue *o) - |. move CARG1, L - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | bnez CRET1, ->vmeta_binop // Binop call for compatibility. - |. nop - | b ->BC_LEN_Z - |. move CARG1, MULTRES -#else - | b ->vmeta_binop // Binop call for compatibility. - |. nop -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // TMP2 = old base, BASE = new base, RC = nargs*8 - | load_got lj_meta_call - | sw TMP2, L->base // This is the callers base! - | addiu CARG2, BASE, -8 - | sw PC, SAVE_PC - | addu CARG3, BASE, RC - | move MULTRES, NARGS8:RC - | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - |. move CARG1, L - | lw LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | addiu NARGS8:RC, MULTRES, 8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | load_got lj_meta_call - | sw BASE, L->base - | addiu CARG2, RA, -8 - | sw PC, SAVE_PC - | addu CARG3, RA, RC - | move MULTRES, NARGS8:RC - | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - |. move CARG1, L - | lw TMP1, FRAME_PC(BASE) - | lw LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here. - | b ->BC_CALLT_Z - |. addiu NARGS8:RC, MULTRES, 8 // Got one more argument now. - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | load_got lj_meta_for - | sw BASE, L->base - | move CARG2, RA - | sw PC, SAVE_PC - | move MULTRES, INS - | call_intern lj_meta_for // (lua_State *L, TValue *base) - |. move CARG1, L - |.if JIT - | decode_OP1 TMP0, MULTRES - | li AT, BC_JFORI - |.endif - | decode_RA8a RA, MULTRES - | decode_RD8a RD, MULTRES - | decode_RA8b RA - |.if JIT - | beq TMP0, AT, =>BC_JFORI - |. decode_RD8b RD - | b =>BC_FORI - |. nop - |.else - | b =>BC_FORI - |. decode_RD8b RD - |.endif - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | lw SFARG1HI, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. lw SFARG1LO, LO(BASE) - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | sltiu AT, NARGS8:RC, 16 - | lw SFARG1HI, HI(BASE) - | bnez AT, ->fff_fallback - |. lw SFARG2HI, 8+HI(BASE) - | lw SFARG1LO, LO(BASE) - | lw SFARG2LO, 8+LO(BASE) - |.endmacro - | - |.macro .ffunc_n, name // Caveat: has delay slot! - |->ff_ .. name: - | lw SFARG1HI, HI(BASE) - |.if FPU - | ldc1 FARG1, 0(BASE) - |.else - | lw SFARG1LO, LO(BASE) - |.endif - | beqz NARGS8:RC, ->fff_fallback - |. sltiu AT, SFARG1HI, LJ_TISNUM - | beqz AT, ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name // Caveat: has delay slot! - |->ff_ .. name: - | sltiu AT, NARGS8:RC, 16 - | lw SFARG1HI, HI(BASE) - | bnez AT, ->fff_fallback - |. lw SFARG2HI, 8+HI(BASE) - | sltiu TMP0, SFARG1HI, LJ_TISNUM - |.if FPU - | ldc1 FARG1, 0(BASE) - |.else - | lw SFARG1LO, LO(BASE) - |.endif - | sltiu TMP1, SFARG2HI, LJ_TISNUM - |.if FPU - | ldc1 FARG2, 8(BASE) - |.else - | lw SFARG2LO, 8+LO(BASE) - |.endif - | and TMP0, TMP0, TMP1 - | beqz TMP0, ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1 and has delay slot! - |.macro ffgccheck - | lw TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lw TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | subu AT, TMP0, TMP1 - | bgezal AT, ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | sltiu AT, SFARG1HI, LJ_TISTRUECOND - | beqz AT, ->fff_fallback - |. addiu RA, BASE, -8 - | lw PC, FRAME_PC(BASE) - | addiu RD, NARGS8:RC, 8 // Compute (nresults+1)*8. - | addu TMP2, RA, NARGS8:RC - | sw SFARG1HI, HI(RA) - | addiu TMP1, BASE, 8 - | beq BASE, TMP2, ->fff_res // Done if exactly 1 argument. - |. sw SFARG1LO, LO(RA) - |1: - | lw SFRETHI, HI(TMP1) - | lw SFRETLO, LO(TMP1) - | sw SFRETHI, -8+HI(TMP1) - | sw SFRETLO, -8+LO(TMP1) - | bne TMP1, TMP2, <1 - |. addiu TMP1, TMP1, 8 - | b ->fff_res - |. nop - | - |.ffunc type - | lw SFARG1HI, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. sltiu TMP0, SFARG1HI, LJ_TISNUM - | movn SFARG1HI, TISNUM, TMP0 - | not TMP1, SFARG1HI - | sll TMP1, TMP1, 3 - | addu TMP1, CFUNC:RB, TMP1 - | lw SFARG1HI, CFUNC:TMP1->upvalue[0].u32.hi - | b ->fff_restv - |. lw SFARG1LO, CFUNC:TMP1->upvalue[0].u32.lo - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | li AT, LJ_TTAB - | bne SFARG1HI, AT, >6 - |. li AT, LJ_TUDATA - |1: // Field metatable must be at same offset for GCtab and GCudata! - | lw TAB:SFARG1LO, TAB:SFARG1LO->metatable - |2: - | lw STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH) - | beqz TAB:SFARG1LO, ->fff_restv - |. li SFARG1HI, LJ_TNIL - | lw TMP0, TAB:SFARG1LO->hmask - | li SFARG1HI, LJ_TTAB // Use metatable as default result. - | lw TMP1, STR:RC->sid - | lw NODE:TMP2, TAB:SFARG1LO->node - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | addu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - | li AT, LJ_TSTR - |3: // Rearranged logic, because we expect _not_ to find the key. - | lw CARG4, offsetof(Node, key)+HI(NODE:TMP2) - | lw TMP0, offsetof(Node, key)+LO(NODE:TMP2) - | lw NODE:TMP3, NODE:TMP2->next - | bne CARG4, AT, >4 - |. lw CARG3, offsetof(Node, val)+HI(NODE:TMP2) - | beq TMP0, STR:RC, >5 - |. lw TMP1, offsetof(Node, val)+LO(NODE:TMP2) - |4: - | beqz NODE:TMP3, ->fff_restv // Not found, keep default result. - |. move NODE:TMP2, NODE:TMP3 - | b <3 - |. nop - |5: - | beq CARG3, TISNIL, ->fff_restv // Ditto for nil value. - |. nop - | move SFARG1HI, CARG3 // Return value of mt.__metatable. - | b ->fff_restv - |. move SFARG1LO, TMP1 - | - |6: - | beq SFARG1HI, AT, <1 - |. sltu AT, TISNUM, SFARG1HI - | movz SFARG1HI, TISNUM, AT - | not TMP1, SFARG1HI - | sll TMP1, TMP1, 2 - | addu TMP1, DISPATCH, TMP1 - | b <2 - |. lw TAB:SFARG1LO, DISPATCH_GL(gcroot[GCROOT_BASEMT])(TMP1) - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | li AT, LJ_TTAB - | bne SFARG1HI, AT, ->fff_fallback - |. addiu SFARG2HI, SFARG2HI, -LJ_TTAB - | lw TAB:TMP1, TAB:SFARG1LO->metatable - | lbu TMP3, TAB:SFARG1LO->marked - | or AT, SFARG2HI, TAB:TMP1 - | bnez AT, ->fff_fallback - |. andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | beqz AT, ->fff_restv - |. sw TAB:SFARG2LO, TAB:SFARG1LO->metatable - | barrierback TAB:SFARG1LO, TMP3, TMP0, ->fff_restv - | - |.ffunc rawget - | lw CARG4, HI(BASE) - | sltiu AT, NARGS8:RC, 16 - | lw TAB:CARG2, LO(BASE) - | load_got lj_tab_get - | addiu CARG4, CARG4, -LJ_TTAB - | or AT, AT, CARG4 - | bnez AT, ->fff_fallback - | addiu CARG3, BASE, 8 - | call_intern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - |. move CARG1, L - | // Returns cTValue *. - | lw SFARG1HI, HI(CRET1) - | b ->fff_restv - |. lw SFARG1LO, LO(CRET1) - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | lw CARG1, HI(BASE) - | xori AT, NARGS8:RC, 8 // Exactly one number argument. - | sltu TMP0, TISNUM, CARG1 - | or AT, AT, TMP0 - | bnez AT, ->fff_fallback - |. lw SFARG1HI, HI(BASE) - | b ->fff_restv - |. lw SFARG1LO, LO(BASE) - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | li AT, LJ_TSTR - | // A __tostring method in the string base metatable is ignored. - | beq SFARG1HI, AT, ->fff_restv // String key? - | // Handle numbers inline, unless a number base metatable is present. - |. lw TMP1, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH) - | sltu TMP0, TISNUM, SFARG1HI - | or TMP0, TMP0, TMP1 - | bnez TMP0, ->fff_fallback - |. sw BASE, L->base // Add frame since C call can throw. - | ffgccheck - |. sw PC, SAVE_PC // Redundant (but a defined value). - | load_got lj_strfmt_number - | move CARG1, L - | call_intern lj_strfmt_number // (lua_State *L, cTValue *o) - |. move CARG2, BASE - | // Returns GCstr *. - | li SFARG1HI, LJ_TSTR - | b ->fff_restv - |. move SFARG1LO, CRET1 - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc next - | lw CARG2, HI(BASE) - | lw TAB:CARG1, LO(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. addu TMP2, BASE, NARGS8:RC - | li AT, LJ_TTAB - | sw TISNIL, HI(TMP2) // Set missing 2nd arg to nil. - | bne CARG2, AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) - | load_got lj_tab_next - | addiu CARG2, BASE, 8 - | call_intern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - |. addiu CARG3, BASE, -8 - | // Returns 1=found, 0=end, -1=error. - | addiu RA, BASE, -8 - | bgtz CRET1, ->fff_res // Found key/value. - |. li RD, (2+1)*8 - | beqz CRET1, ->fff_restv // End of traversal: return nil. - |. li SFARG1HI, LJ_TNIL - | lw CFUNC:RB, FRAME_FUNC(BASE) - | b ->fff_fallback // Invalid key. - |. li RC, 2*8 - | - |.ffunc_1 pairs - | li AT, LJ_TTAB - | bne SFARG1HI, AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) -#if LJ_52 - | lw TAB:TMP2, TAB:SFARG1LO->metatable - | lw TMP0, CFUNC:RB->upvalue[0].u32.hi - | lw TMP1, CFUNC:RB->upvalue[0].u32.lo - | bnez TAB:TMP2, ->fff_fallback -#else - | lw TMP0, CFUNC:RB->upvalue[0].u32.hi - | lw TMP1, CFUNC:RB->upvalue[0].u32.lo -#endif - |. addiu RA, BASE, -8 - | sw TISNIL, 8+HI(BASE) - | sw TMP0, HI(RA) - | sw TMP1, LO(RA) - | b ->fff_res - |. li RD, (3+1)*8 - | - |.ffunc ipairs_aux - | sltiu AT, NARGS8:RC, 16 - | lw CARG3, HI(BASE) - | lw TAB:CARG1, LO(BASE) - | lw CARG4, 8+HI(BASE) - | bnez AT, ->fff_fallback - |. addiu CARG3, CARG3, -LJ_TTAB - | xor CARG4, CARG4, TISNUM - | and AT, CARG3, CARG4 - | bnez AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) - | lw TMP2, 8+LO(BASE) - | lw TMP0, TAB:CARG1->asize - | lw TMP1, TAB:CARG1->array - | addiu TMP2, TMP2, 1 - | sw TISNUM, -8+HI(BASE) - | sltu AT, TMP2, TMP0 - | sw TMP2, -8+LO(BASE) - | beqz AT, >2 // Not in array part? - |. addiu RA, BASE, -8 - | sll TMP3, TMP2, 3 - | addu TMP3, TMP1, TMP3 - | lw TMP1, HI(TMP3) - | lw TMP2, LO(TMP3) - |1: - | beq TMP1, TISNIL, ->fff_res // End of iteration, return 0 results. - |. li RD, (0+1)*8 - | sw TMP1, 8+HI(RA) - | sw TMP2, 8+LO(RA) - | b ->fff_res - |. li RD, (2+1)*8 - | - |2: // Check for empty hash part first. Otherwise call C function. - | lw TMP0, TAB:CARG1->hmask - | load_got lj_tab_getinth - | beqz TMP0, ->fff_res - |. li RD, (0+1)*8 - | call_intern lj_tab_getinth // (GCtab *t, int32_t key) - |. move CARG2, TMP2 - | // Returns cTValue * or NULL. - | beqz CRET1, ->fff_res - |. li RD, (0+1)*8 - | lw TMP1, HI(CRET1) - | b <1 - |. lw TMP2, LO(CRET1) - | - |.ffunc_1 ipairs - | li AT, LJ_TTAB - | bne SFARG1HI, AT, ->fff_fallback - |. lw PC, FRAME_PC(BASE) -#if LJ_52 - | lw TAB:TMP2, TAB:SFARG1LO->metatable - | lw TMP0, CFUNC:RB->upvalue[0].u32.hi - | lw TMP1, CFUNC:RB->upvalue[0].u32.lo - | bnez TAB:TMP2, ->fff_fallback -#else - | lw TMP0, CFUNC:RB->upvalue[0].u32.hi - | lw TMP1, CFUNC:RB->upvalue[0].u32.lo -#endif - |. addiu RA, BASE, -8 - | sw TISNUM, 8+HI(BASE) - | sw r0, 8+LO(BASE) - | sw TMP0, HI(RA) - | sw TMP1, LO(RA) - | b ->fff_res - |. li RD, (3+1)*8 - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | beqz NARGS8:RC, ->fff_fallback - | move TMP2, BASE - | addiu BASE, BASE, 8 - | // Remember active hook before pcall. - | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT - | andi TMP3, TMP3, 1 - | addiu PC, TMP3, 8+FRAME_PCALL - | b ->vm_call_dispatch - |. addiu NARGS8:RC, NARGS8:RC, -8 - | - |.ffunc xpcall - | sltiu AT, NARGS8:RC, 16 - | lw CARG4, 8+HI(BASE) - | bnez AT, ->fff_fallback - |. lw CARG3, 8+LO(BASE) - | lw CARG1, LO(BASE) - | lw CARG2, HI(BASE) - | lbu TMP1, DISPATCH_GL(hookmask)(DISPATCH) - | li AT, LJ_TFUNC - | move TMP2, BASE - | bne CARG4, AT, ->fff_fallback // Traceback must be a function. - | addiu BASE, BASE, 16 - | // Remember active hook before pcall. - | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT - | sw CARG3, LO(TMP2) // Swap function and traceback. - | sw CARG4, HI(TMP2) - | andi TMP3, TMP3, 1 - | sw CARG1, 8+LO(TMP2) - | sw CARG2, 8+HI(TMP2) - | addiu PC, TMP3, 16+FRAME_PCALL - | b ->vm_call_dispatch - |. addiu NARGS8:RC, NARGS8:RC, -16 - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc coroutine_resume - | lw CARG3, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. lw CARG1, LO(BASE) - | li AT, LJ_TTHREAD - | bne CARG3, AT, ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | lw L:CARG1, CFUNC:RB->upvalue[0].gcr - |.endif - | lbu TMP0, L:CARG1->status - | lw TMP1, L:CARG1->cframe - | lw CARG2, L:CARG1->top - | lw TMP2, L:CARG1->base - | addiu TMP3, TMP0, -LUA_YIELD - | bgtz TMP3, ->fff_fallback // st > LUA_YIELD? - |. xor TMP2, TMP2, CARG2 - | bnez TMP1, ->fff_fallback // cframe != 0? - |. or AT, TMP2, TMP0 - | lw TMP0, L:CARG1->maxstack - | beqz AT, ->fff_fallback // base == top && st == 0? - |. lw PC, FRAME_PC(BASE) - | addu TMP2, CARG2, NARGS8:RC - | sltu AT, TMP0, TMP2 - | bnez AT, ->fff_fallback // Stack overflow? - |. sw PC, SAVE_PC - | sw BASE, L->base - |1: - |.if resume - | addiu BASE, BASE, 8 // Keep resumed thread in stack for GC. - | addiu NARGS8:RC, NARGS8:RC, -8 - | addiu TMP2, TMP2, -8 - |.endif - | sw TMP2, L:CARG1->top - | addu TMP1, BASE, NARGS8:RC - | move CARG3, CARG2 - | sw BASE, L->top - |2: // Move args to coroutine. - | lw SFRETHI, HI(BASE) - | lw SFRETLO, LO(BASE) - | sltu AT, BASE, TMP1 - | beqz AT, >3 - |. addiu BASE, BASE, 8 - | sw SFRETHI, HI(CARG3) - | sw SFRETLO, LO(CARG3) - | b <2 - |. addiu CARG3, CARG3, 8 - |3: - | bal ->vm_resume // (lua_State *L, TValue *base, 0, 0) - |. move L:RA, L:CARG1 - | // Returns thread status. - |4: - | lw TMP2, L:RA->base - | sltiu AT, CRET1, LUA_YIELD+1 - | lw TMP3, L:RA->top - | li_vmstate INTERP - | lw BASE, L->base - | sw L, DISPATCH_GL(cur_L)(DISPATCH) - | st_vmstate - | beqz AT, >8 - |. subu RD, TMP3, TMP2 - | lw TMP0, L->maxstack - | beqz RD, >6 // No results? - |. addu TMP1, BASE, RD - | sltu AT, TMP0, TMP1 - | bnez AT, >9 // Need to grow stack? - |. addu TMP3, TMP2, RD - | sw TMP2, L:RA->top // Clear coroutine stack. - | move TMP1, BASE - |5: // Move results from coroutine. - | lw SFRETHI, HI(TMP2) - | lw SFRETLO, LO(TMP2) - | addiu TMP2, TMP2, 8 - | sltu AT, TMP2, TMP3 - | sw SFRETHI, HI(TMP1) - | sw SFRETLO, LO(TMP1) - | bnez AT, <5 - |. addiu TMP1, TMP1, 8 - |6: - | andi TMP0, PC, FRAME_TYPE - |.if resume - | li TMP1, LJ_TTRUE - | addiu RA, BASE, -8 - | sw TMP1, -8+HI(BASE) // Prepend true to results. - | addiu RD, RD, 16 - |.else - | move RA, BASE - | addiu RD, RD, 8 - |.endif - |7: - | sw PC, SAVE_PC - | beqz TMP0, ->BC_RET_Z - |. move MULTRES, RD - | b ->vm_return - |. nop - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | addiu TMP3, TMP3, -8 - | li TMP1, LJ_TFALSE - | lw SFRETHI, HI(TMP3) - | lw SFRETLO, LO(TMP3) - | sw TMP3, L:RA->top // Remove error from coroutine stack. - | li RD, (2+1)*8 - | sw TMP1, -8+HI(BASE) // Prepend false to results. - | addiu RA, BASE, -8 - | sw SFRETHI, HI(BASE) // Copy error message. - | sw SFRETLO, LO(BASE) - | b <7 - |. andi TMP0, PC, FRAME_TYPE - |.else - | load_got lj_ffh_coroutine_wrap_err - | move CARG2, L:RA - | call_intern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - |. move CARG1, L - |.endif - | - |9: // Handle stack expansion on return from yield. - | load_got lj_state_growstack - | srl CARG2, RD, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | b <4 - |. li CRET1, 0 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | lw TMP0, L->cframe - | addu TMP1, BASE, NARGS8:RC - | sw BASE, L->base - | andi TMP0, TMP0, CFRAME_RESUME - | sw TMP1, L->top - | beqz TMP0, ->fff_fallback - |. li CRET1, LUA_YIELD - | sw r0, L->cframe - | b ->vm_leave_unw - |. sb CRET1, L->status - | - |//-- Math library ------------------------------------------------------- - | - |.ffunc_1 math_abs - | bne SFARG1HI, TISNUM, >1 - |. sra TMP0, SFARG1LO, 31 - | xor TMP1, SFARG1LO, TMP0 - | subu SFARG1LO, TMP1, TMP0 - | bgez SFARG1LO, ->fff_restv - |. nop - | lui SFARG1HI, 0x41e0 // 2^31 as a double. - | b ->fff_restv - |. li SFARG1LO, 0 - |1: - | sltiu AT, SFARG1HI, LJ_TISNUM - | beqz AT, ->fff_fallback - |. sll SFARG1HI, SFARG1HI, 1 - | srl SFARG1HI, SFARG1HI, 1 - |// fallthrough - | - |->fff_restv: - | // SFARG1LO/SFARG1HI = TValue result. - | lw PC, FRAME_PC(BASE) - | sw SFARG1HI, -8+HI(BASE) - | addiu RA, BASE, -8 - | sw SFARG1LO, -8+LO(BASE) - |->fff_res1: - | // RA = results, PC = return. - | li RD, (1+1)*8 - |->fff_res: - | // RA = results, RD = (nresults+1)*8, PC = return. - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->vm_return - |. move MULTRES, RD - | lw INS, -4(PC) - | decode_RB8a RB, INS - | decode_RB8b RB - |5: - | sltu AT, RD, RB - | bnez AT, >6 // More results expected? - |. decode_RA8a TMP0, INS - | decode_RA8b TMP0 - | ins_next1 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | subu BASE, RA, TMP0 - | ins_next2 - | - |6: // Fill up results with nil. - | addu TMP1, RA, RD - | addiu RD, RD, 8 - | b <5 - |. sw TISNIL, -8+HI(TMP1) - | - |.macro math_extern, func - | .ffunc math_ .. func - | lw SFARG1HI, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. load_got func - | sltiu AT, SFARG1HI, LJ_TISNUM - | beqz AT, ->fff_fallback - |.if FPU - |. ldc1 FARG1, 0(BASE) - |.else - |. lw SFARG1LO, LO(BASE) - |.endif - | call_extern - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - |. load_got func - | call_extern - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - |// TODO: Return integer type if result is integer (own sf implementation). - |.macro math_round, func - |->ff_math_ .. func: - | lw SFARG1HI, HI(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. lw SFARG1LO, LO(BASE) - | beq SFARG1HI, TISNUM, ->fff_restv - |. sltu AT, SFARG1HI, TISNUM - | beqz AT, ->fff_fallback - |.if FPU - |. ldc1 FARG1, 0(BASE) - | bal ->vm_ .. func - |.else - |. load_got func - | call_extern - |.endif - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - | math_round floor - | math_round ceil - | - |.ffunc math_log - | li AT, 8 - | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument. - |. lw SFARG1HI, HI(BASE) - | sltiu AT, SFARG1HI, LJ_TISNUM - | beqz AT, ->fff_fallback - |. load_got log - |.if FPU - | call_extern - |. ldc1 FARG1, 0(BASE) - |.else - | call_extern - |. lw SFARG1LO, LO(BASE) - |.endif - | b ->fff_resn - |. nop - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.if FPU - |.ffunc_n math_sqrt - |. sqrt.d FRET1, FARG1 - |// fallthrough to ->fff_resn - |.else - | math_extern sqrt - |.endif - | - |->fff_resn: - | lw PC, FRAME_PC(BASE) - | addiu RA, BASE, -8 - |.if FPU - | b ->fff_res1 - |. sdc1 FRET1, -8(BASE) - |.else - | sw SFRETHI, -8+HI(BASE) - | b ->fff_res1 - |. sw SFRETLO, -8+LO(BASE) - |.endif - | - | - |.ffunc math_ldexp - | sltiu AT, NARGS8:RC, 16 - | lw SFARG1HI, HI(BASE) - | bnez AT, ->fff_fallback - |. lw CARG4, 8+HI(BASE) - | bne CARG4, TISNUM, ->fff_fallback - | load_got ldexp - |. sltu AT, SFARG1HI, TISNUM - | beqz AT, ->fff_fallback - |.if FPU - |. ldc1 FARG1, 0(BASE) - |.else - |. lw SFARG1LO, LO(BASE) - |.endif - | call_extern - |. lw CARG3, 8+LO(BASE) - | b ->fff_resn - |. nop - | - |.ffunc_n math_frexp - | load_got frexp - | lw PC, FRAME_PC(BASE) - | call_extern - |. addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | lw TMP1, DISPATCH_GL(tmptv)(DISPATCH) - | addiu RA, BASE, -8 - |.if FPU - | mtc1 TMP1, FARG2 - | sdc1 FRET1, 0(RA) - | cvt.d.w FARG2, FARG2 - | sdc1 FARG2, 8(RA) - |.else - | sw SFRETLO, LO(RA) - | sw SFRETHI, HI(RA) - | sw TMP1, 8+LO(RA) - | sw TISNUM, 8+HI(RA) - |.endif - | b ->fff_res - |. li RD, (2+1)*8 - | - |.ffunc_n math_modf - | load_got modf - | lw PC, FRAME_PC(BASE) - | call_extern - |. addiu CARG3, BASE, -8 - | addiu RA, BASE, -8 - |.if FPU - | sdc1 FRET1, 0(BASE) - |.else - | sw SFRETLO, LO(BASE) - | sw SFRETHI, HI(BASE) - |.endif - | b ->fff_res - |. li RD, (2+1)*8 - | - |.macro math_minmax, name, intins, ismax - | .ffunc_1 name - | addu TMP3, BASE, NARGS8:RC - | bne SFARG1HI, TISNUM, >5 - |. addiu TMP2, BASE, 8 - |1: // Handle integers. - |. lw SFARG2HI, HI(TMP2) - | beq TMP2, TMP3, ->fff_restv - |. lw SFARG2LO, LO(TMP2) - | bne SFARG2HI, TISNUM, >3 - |. slt AT, SFARG1LO, SFARG2LO - | intins SFARG1LO, SFARG2LO, AT - | b <1 - |. addiu TMP2, TMP2, 8 - | - |3: // Convert intermediate result to number and continue with number loop. - | sltiu AT, SFARG2HI, LJ_TISNUM - | beqz AT, ->fff_fallback - |.if FPU - |. mtc1 SFARG1LO, FRET1 - | cvt.d.w FRET1, FRET1 - | b >7 - |. ldc1 FARG1, 0(TMP2) - |.else - |. nop - | bal ->vm_sfi2d_1 - |. nop - | b >7 - |. nop - |.endif - | - |5: - |. sltiu AT, SFARG1HI, LJ_TISNUM - | beqz AT, ->fff_fallback - |.if FPU - |. ldc1 FRET1, 0(BASE) - |.endif - | - |6: // Handle numbers. - |. lw SFARG2HI, HI(TMP2) - |.if FPU - | beq TMP2, TMP3, ->fff_resn - |.else - | beq TMP2, TMP3, ->fff_restv - |.endif - |. sltiu AT, SFARG2HI, LJ_TISNUM - | beqz AT, >8 - |.if FPU - |. ldc1 FARG1, 0(TMP2) - |.else - |. lw SFARG2LO, LO(TMP2) - |.endif - |7: - |.if FPU - |.if ismax - | c.olt.d FARG1, FRET1 - |.else - | c.olt.d FRET1, FARG1 - |.endif - | movf.d FRET1, FARG1 - |.else - |.if ismax - | bal ->vm_sfcmpogt - |.else - | bal ->vm_sfcmpolt - |.endif - |. nop - | movz SFARG1LO, SFARG2LO, CRET1 - | movz SFARG1HI, SFARG2HI, CRET1 - |.endif - | b <6 - |. addiu TMP2, TMP2, 8 - | - |8: // Convert integer to number and continue with number loop. - | bne SFARG2HI, TISNUM, ->fff_fallback - |.if FPU - |. lwc1 FARG1, LO(TMP2) - | b <7 - |. cvt.d.w FARG1, FARG1 - |.else - |. nop - | bal ->vm_sfi2d_2 - |. nop - | b <7 - |. nop - |.endif - | - |.endmacro - | - | math_minmax math_min, movz, 0 - | math_minmax math_max, movn, 1 - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | lw CARG3, HI(BASE) - | lw STR:CARG1, LO(BASE) - | xori AT, NARGS8:RC, 8 - | addiu CARG3, CARG3, -LJ_TSTR - | or AT, AT, CARG3 - | bnez AT, ->fff_fallback // Need exactly 1 string argument. - |. nop - | lw TMP0, STR:CARG1->len - | addiu RA, BASE, -8 - | lw PC, FRAME_PC(BASE) - | sltu RD, r0, TMP0 - | lbu TMP1, STR:CARG1[1] // Access is always ok (NUL at end). - | addiu RD, RD, 1 - | sll RD, RD, 3 // RD = ((str->len != 0)+1)*8 - | sw TISNUM, HI(RA) - | b ->fff_res - |. sw TMP1, LO(RA) - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - |. nop - | lw CARG3, HI(BASE) - | lw CARG1, LO(BASE) - | li TMP1, 255 - | xori AT, NARGS8:RC, 8 // Exactly 1 argument. - | xor TMP0, CARG3, TISNUM // Integer. - | sltu TMP1, TMP1, CARG1 // !(255 < n). - | or AT, AT, TMP0 - | or AT, AT, TMP1 - | bnez AT, ->fff_fallback - |. li CARG3, 1 - | addiu CARG2, sp, ARG5_OFS - | sb CARG1, ARG5 - |->fff_newstr: - | load_got lj_str_new - | sw BASE, L->base - | sw PC, SAVE_PC - | call_intern lj_str_new // (lua_State *L, char *str, size_t l) - |. move CARG1, L - | // Returns GCstr *. - | lw BASE, L->base - |->fff_resstr: - | move SFARG1LO, CRET1 - | b ->fff_restv - |. li SFARG1HI, LJ_TSTR - | - |.ffunc string_sub - | ffgccheck - |. nop - | addiu AT, NARGS8:RC, -16 - | lw CARG3, 16+HI(BASE) - | lw TMP0, HI(BASE) - | lw STR:CARG1, LO(BASE) - | bltz AT, ->fff_fallback - |. lw CARG2, 8+HI(BASE) - | beqz AT, >1 - |. li CARG4, -1 - | bne CARG3, TISNUM, ->fff_fallback - |. lw CARG4, 16+LO(BASE) - |1: - | bne CARG2, TISNUM, ->fff_fallback - |. li AT, LJ_TSTR - | bne TMP0, AT, ->fff_fallback - |. lw CARG3, 8+LO(BASE) - | lw CARG2, STR:CARG1->len - | // STR:CARG1 = str, CARG2 = str->len, CARG3 = start, CARG4 = end - | slt AT, CARG4, r0 - | addiu TMP0, CARG2, 1 - | addu TMP1, CARG4, TMP0 - | slt TMP3, CARG3, r0 - | movn CARG4, TMP1, AT // if (end < 0) end += len+1 - | addu TMP1, CARG3, TMP0 - | movn CARG3, TMP1, TMP3 // if (start < 0) start += len+1 - | li TMP2, 1 - | slt AT, CARG4, r0 - | slt TMP3, r0, CARG3 - | movn CARG4, r0, AT // if (end < 0) end = 0 - | movz CARG3, TMP2, TMP3 // if (start < 1) start = 1 - | slt AT, CARG2, CARG4 - | movn CARG4, CARG2, AT // if (end > len) end = len - | addu CARG2, STR:CARG1, CARG3 - | subu CARG3, CARG4, CARG3 // len = end - start - | addiu CARG2, CARG2, sizeof(GCstr)-1 - | bgez CARG3, ->fff_newstr - |. addiu CARG3, CARG3, 1 // len++ - |->fff_emptystr: // Return empty string. - | addiu STR:SFARG1LO, DISPATCH, DISPATCH_GL(strempty) - | b ->fff_restv - |. li SFARG1HI, LJ_TSTR - | - |.macro ffstring_op, name - | .ffunc string_ .. name - | ffgccheck - |. nop - | lw CARG3, HI(BASE) - | lw STR:CARG2, LO(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. li AT, LJ_TSTR - | bne CARG3, AT, ->fff_fallback - |. addiu SBUF:CARG1, DISPATCH, DISPATCH_GL(tmpbuf) - | load_got lj_buf_putstr_ .. name - | lw TMP0, SBUF:CARG1->b - | sw L, SBUF:CARG1->L - | sw BASE, L->base - | sw TMP0, SBUF:CARG1->w - | call_intern extern lj_buf_putstr_ .. name - |. sw PC, SAVE_PC - | load_got lj_buf_tostr - | call_intern lj_buf_tostr - |. move SBUF:CARG1, SBUF:CRET1 - | b ->fff_resstr - |. lw BASE, L->base - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |->vm_tobit_fb: - | beqz TMP1, ->fff_fallback - |.if FPU - |. ldc1 FARG1, 0(BASE) - | add.d FARG1, FARG1, TOBIT - | jr ra - |. mfc1 CRET1, FARG1 - |.else - |// FP number to bit conversion for soft-float. - |->vm_tobit: - | sll TMP0, SFARG1HI, 1 - | lui AT, 0x0020 - | addu TMP0, TMP0, AT - | slt AT, TMP0, r0 - | movz SFARG1LO, r0, AT - | beqz AT, >2 - |. li TMP1, 0x3e0 - | not TMP1, TMP1 - | sra TMP0, TMP0, 21 - | subu TMP0, TMP1, TMP0 - | slt AT, TMP0, r0 - | bnez AT, >1 - |. sll TMP1, SFARG1HI, 11 - | lui AT, 0x8000 - | or TMP1, TMP1, AT - | srl AT, SFARG1LO, 21 - | or TMP1, TMP1, AT - | slt AT, SFARG1HI, r0 - | beqz AT, >2 - |. srlv SFARG1LO, TMP1, TMP0 - | subu SFARG1LO, r0, SFARG1LO - |2: - | jr ra - |. move CRET1, SFARG1LO - |1: - | addiu TMP0, TMP0, 21 - | srlv TMP1, SFARG1LO, TMP0 - | li AT, 20 - | subu TMP0, AT, TMP0 - | sll SFARG1LO, SFARG1HI, 12 - | sllv AT, SFARG1LO, TMP0 - | or SFARG1LO, TMP1, AT - | slt AT, SFARG1HI, r0 - | beqz AT, <2 - |. nop - | jr ra - |. subu CRET1, r0, SFARG1LO - |.endif - | - |.macro .ffunc_bit, name - | .ffunc_1 bit_..name - | beq SFARG1HI, TISNUM, >6 - |. move CRET1, SFARG1LO - | bal ->vm_tobit_fb - |. sltu TMP1, SFARG1HI, TISNUM - |6: - |.endmacro - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | addiu TMP2, BASE, 8 - | addu TMP3, BASE, NARGS8:RC - |1: - | lw SFARG1HI, HI(TMP2) - | beq TMP2, TMP3, ->fff_resi - |. lw SFARG1LO, LO(TMP2) - |.if FPU - | bne SFARG1HI, TISNUM, >2 - |. addiu TMP2, TMP2, 8 - | b <1 - |. ins CRET1, CRET1, SFARG1LO - |2: - | ldc1 FARG1, -8(TMP2) - | sltu TMP1, SFARG1HI, TISNUM - | beqz TMP1, ->fff_fallback - |. add.d FARG1, FARG1, TOBIT - | mfc1 SFARG1LO, FARG1 - | b <1 - |. ins CRET1, CRET1, SFARG1LO - |.else - | beq SFARG1HI, TISNUM, >2 - |. move CRET2, CRET1 - | bal ->vm_tobit_fb - |. sltu TMP1, SFARG1HI, TISNUM - | move SFARG1LO, CRET2 - |2: - | ins CRET1, CRET1, SFARG1LO - | b <1 - |. addiu TMP2, TMP2, 8 - |.endif - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, or - |.ffunc_bit_op bxor, xor - | - |.ffunc_bit bswap - | srl TMP0, CRET1, 24 - | srl TMP2, CRET1, 8 - | sll TMP1, CRET1, 24 - | andi TMP2, TMP2, 0xff00 - | or TMP0, TMP0, TMP1 - | andi CRET1, CRET1, 0xff00 - | or TMP0, TMP0, TMP2 - | sll CRET1, CRET1, 8 - | b ->fff_resi - |. or CRET1, TMP0, CRET1 - | - |.ffunc_bit bnot - | b ->fff_resi - |. not CRET1, CRET1 - | - |.macro .ffunc_bit_sh, name, ins, shmod - | .ffunc_2 bit_..name - | beq SFARG1HI, TISNUM, >1 - |. nop - | bal ->vm_tobit_fb - |. sltu TMP1, SFARG1HI, TISNUM - | move SFARG1LO, CRET1 - |1: - | bne SFARG2HI, TISNUM, ->fff_fallback - |. nop - |.if shmod == 1 - | li AT, 32 - | subu TMP0, AT, SFARG2LO - | sllv SFARG2LO, SFARG1LO, SFARG2LO - | srlv SFARG1LO, SFARG1LO, TMP0 - |.elif shmod == 2 - | li AT, 32 - | subu TMP0, AT, SFARG2LO - | srlv SFARG2LO, SFARG1LO, SFARG2LO - | sllv SFARG1LO, SFARG1LO, TMP0 - |.endif - | b ->fff_resi - |. ins CRET1, SFARG1LO, SFARG2LO - |.endmacro - | - |.ffunc_bit_sh lshift, sllv, 0 - |.ffunc_bit_sh rshift, srlv, 0 - |.ffunc_bit_sh arshift, srav, 0 - |// Can't use rotrv, since it's only in MIPS32R2. - |.ffunc_bit_sh rol, or, 1 - |.ffunc_bit_sh ror, or, 2 - | - |.ffunc_bit tobit - |->fff_resi: - | lw PC, FRAME_PC(BASE) - | addiu RA, BASE, -8 - | sw TISNUM, -8+HI(BASE) - | b ->fff_res1 - |. sw CRET1, -8+LO(BASE) - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RB = CFUNC, RC = nargs*8 - | lw TMP3, CFUNC:RB->f - | addu TMP1, BASE, NARGS8:RC - | lw PC, FRAME_PC(BASE) // Fallback may overwrite PC. - | addiu TMP0, TMP1, 8*LUA_MINSTACK - | lw TMP2, L->maxstack - | sw PC, SAVE_PC // Redundant (but a defined value). - | sltu AT, TMP2, TMP0 - | sw BASE, L->base - | sw TMP1, L->top - | bnez AT, >5 // Need to grow stack. - |. move CFUNCADDR, TMP3 - | jalr TMP3 // (lua_State *L) - |. move CARG1, L - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | lw BASE, L->base - | sll RD, CRET1, 3 - | bgtz CRET1, ->fff_res // Returned nresults+1? - |. addiu RA, BASE, -8 - |1: // Returned 0 or -1: retry fast path. - | lw TMP0, L->top - | lw LFUNC:RB, FRAME_FUNC(BASE) - | bnez CRET1, ->vm_call_tail // Returned -1? - |. subu NARGS8:RC, TMP0, BASE - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | andi TMP0, PC, FRAME_TYPE - | li AT, -4 - | bnez TMP0, >3 - |. and TMP1, PC, AT - | lbu TMP1, OFS_RA(PC) - | sll TMP1, TMP1, 3 - | addiu TMP1, TMP1, 8 - |3: - | b ->vm_call_dispatch // Resolve again for tailcall. - |. subu TMP2, BASE, TMP1 - | - |5: // Grow stack for fallback handler. - | load_got lj_state_growstack - | li CARG2, LUA_MINSTACK - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw BASE, L->base - | b <1 - |. li CRET1, 0 // Force retry. - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | move MULTRES, ra - | load_got lj_gc_step - | sw BASE, L->base - | addu TMP0, BASE, NARGS8:RC - | sw PC, SAVE_PC // Redundant (but a defined value). - | sw TMP0, L->top - | call_intern lj_gc_step // (lua_State *L) - |. move CARG1, L - | lw BASE, L->base - | move ra, MULTRES - | lw TMP0, L->top - | lw CFUNC:RB, FRAME_FUNC(BASE) - | jr ra - |. subu NARGS8:RC, TMP0, BASE - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi AT, TMP3, HOOK_VMEVENT // No recording while in vmevent. - | bnez AT, >5 - | // Decrement the hookcount for consistency, but always do the call. - |. lw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE - | bnez AT, >1 - |. addiu TMP2, TMP2, -1 - | andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqz AT, >1 - |. nop - | b >1 - |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE // Hook already active? - | beqz AT, >1 - |5: // Re-dispatch to static ins. - |. lw AT, GG_DISP2STATIC(TMP0) // Assumes TMP0 holds DISPATCH+OP*4. - | jr AT - |. nop - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | lw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE // Hook already active? - | bnez AT, <5 - |. andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqz AT, <5 - |. addiu TMP2, TMP2, -1 - | beqz TMP2, >1 - |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, LUA_MASKLINE - | beqz AT, <5 - |1: - |. load_got lj_dispatch_ins - | sw MULTRES, SAVE_MULTRES - | move CARG2, PC - | sw BASE, L->base - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | call_intern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |. move CARG1, L - |3: - | lw BASE, L->base - |4: // Re-dispatch to static ins. - | lw INS, -4(PC) - | decode_OP4a TMP1, INS - | decode_OP4b TMP1 - | addu TMP0, DISPATCH, TMP1 - | decode_RD8a RD, INS - | lw AT, GG_DISP2STATIC(TMP0) - | decode_RA8a RA, INS - | decode_RD8b RD - | jr AT - | decode_RA8b RA - | - |->cont_hook: // Continue from hook yield. - | addiu PC, PC, 4 - | b <4 - |. lw MULTRES, -24+LO(RB) // Restore MULTRES for *M ins. - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | lw LFUNC:TMP1, FRAME_FUNC(BASE) - | addiu CARG1, DISPATCH, GG_DISP2J - | sw PC, SAVE_PC - | lw TMP1, LFUNC:TMP1->pc - | move CARG2, PC - | sw L, DISPATCH_J(L)(DISPATCH) - | lbu TMP1, PC2PROTO(framesize)(TMP1) - | load_got lj_trace_hot - | sw BASE, L->base - | sll TMP1, TMP1, 3 - | addu TMP1, BASE, TMP1 - | call_intern lj_trace_hot // (jit_State *J, const BCIns *pc) - |. sw TMP1, L->top - | b <3 - |. nop - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - |.if JIT - | b >1 - |.endif - |. move CARG2, PC - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | ori CARG2, PC, 1 - |1: - |.endif - | load_got lj_dispatch_call - | addu TMP0, BASE, RC - | sw PC, SAVE_PC - | sw BASE, L->base - | subu RA, RA, BASE - | sw TMP0, L->top - | call_intern lj_dispatch_call // (lua_State *L, const BCIns *pc) - |. move CARG1, L - | // Returns ASMFunction. - | lw BASE, L->base - | lw TMP0, L->top - | sw r0, SAVE_PC // Invalidate for subsequent line hook. - | subu NARGS8:RC, TMP0, BASE - | addu RA, BASE, RA - | lw LFUNC:RB, FRAME_FUNC(BASE) - | jr CRET1 - |. lw INS, -4(PC) - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // RA = resultptr, RB = meta base - | lw INS, -4(PC) - | lw TMP2, -24+LO(RB) // Save previous trace. - | decode_RA8a RC, INS - | addiu AT, MULTRES, -8 - | decode_RA8b RC - | beqz AT, >2 - |. addu RC, BASE, RC // Call base. - |1: // Move results down. - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | addiu AT, AT, -8 - | addiu RA, RA, 8 - | sw SFRETHI, HI(RC) - | sw SFRETLO, LO(RC) - | bnez AT, <1 - |. addiu RC, RC, 8 - |2: - | decode_RA8a RA, INS - | decode_RB8a RB, INS - | decode_RA8b RA - | decode_RB8b RB - | addu RA, RA, RB - | addu RA, BASE, RA - |3: - | sltu AT, RC, RA - | bnez AT, >9 // More results wanted? - |. nop - | - | lhu TMP3, TRACE:TMP2->traceno - | lhu RD, TRACE:TMP2->link - | beq RD, TMP3, ->cont_nop // Blacklisted. - |. load_got lj_dispatch_stitch - | bnez RD, =>BC_JLOOP // Jump to stitched trace. - |. sll RD, RD, 3 - | - | // Stitch a new trace to the previous trace. - | sw TMP3, DISPATCH_J(exitno)(DISPATCH) - | sw L, DISPATCH_J(L)(DISPATCH) - | sw BASE, L->base - | addiu CARG1, DISPATCH, GG_DISP2J - | call_intern lj_dispatch_stitch // (jit_State *J, const BCIns *pc) - |. move CARG2, PC - | b ->cont_nop - |. lw BASE, L->base - | - |9: - | sw TISNIL, HI(RC) - | b <3 - |. addiu RC, RC, 8 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | load_got lj_dispatch_profile - | sw MULTRES, SAVE_MULTRES - | move CARG2, PC - | sw BASE, L->base - | call_intern lj_dispatch_profile // (lua_State *L, const BCIns *pc) - |. move CARG1, L - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | addiu PC, PC, -4 - | b ->cont_nop - |. lw BASE, L->base -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro savex_, a, b - |.if FPU - | sdc1 f..a, 16+a*8(sp) - | sw r..a, 16+32*8+a*4(sp) - | sw r..b, 16+32*8+b*4(sp) - |.else - | sw r..a, 16+a*4(sp) - | sw r..b, 16+b*4(sp) - |.endif - |.endmacro - | - |->vm_exit_handler: - |.if JIT - |.if FPU - | addiu sp, sp, -(16+32*8+32*4) - |.else - | addiu sp, sp, -(16+32*4) - |.endif - | savex_ 0, 1 - | savex_ 2, 3 - | savex_ 4, 5 - | savex_ 6, 7 - | savex_ 8, 9 - | savex_ 10, 11 - | savex_ 12, 13 - | savex_ 14, 15 - | savex_ 16, 17 - | savex_ 18, 19 - | savex_ 20, 21 - | savex_ 22, 23 - | savex_ 24, 25 - | savex_ 26, 27 - |.if FPU - | sdc1 f28, 16+28*8(sp) - | sdc1 f30, 16+30*8(sp) - | sw r28, 16+32*8+28*4(sp) - | sw r30, 16+32*8+30*4(sp) - | sw r0, 16+32*8+31*4(sp) // Clear RID_TMP. - | addiu TMP2, sp, 16+32*8+32*4 // Recompute original value of sp. - | sw TMP2, 16+32*8+29*4(sp) // Store sp in RID_SP - |.else - | sw r28, 16+28*4(sp) - | sw r30, 16+30*4(sp) - | sw r0, 16+31*4(sp) // Clear RID_TMP. - | addiu TMP2, sp, 16+32*4 // Recompute original value of sp. - | sw TMP2, 16+29*4(sp) // Store sp in RID_SP - |.endif - | li_vmstate EXIT - | addiu DISPATCH, JGL, -GG_DISP2G-32768 - | lw TMP1, 0(TMP2) // Load exit number. - | st_vmstate - | lw L, DISPATCH_GL(cur_L)(DISPATCH) - | lw BASE, DISPATCH_GL(jit_base)(DISPATCH) - | load_got lj_trace_exit - | sw L, DISPATCH_J(L)(DISPATCH) - | sw ra, DISPATCH_J(parent)(DISPATCH) // Store trace number. - | sw BASE, L->base - | sw TMP1, DISPATCH_J(exitno)(DISPATCH) // Store exit number. - | addiu CARG1, DISPATCH, GG_DISP2J - | sw r0, DISPATCH_GL(jit_base)(DISPATCH) - | call_intern lj_trace_exit // (jit_State *J, ExitState *ex) - |. addiu CARG2, sp, 16 - | // Returns MULTRES (unscaled) or negated error code. - | lw TMP1, L->cframe - | li AT, -4 - | lw BASE, L->base - | and sp, TMP1, AT - | lw PC, SAVE_PC // Get SAVE_PC. - | b >1 - |. sw L, SAVE_L // Set SAVE_L (on-trace resume/yield). - |.endif - |->vm_exit_interp: - |.if JIT - | // CRET1 = MULTRES or negated error code, BASE, PC and JGL set. - | lw L, SAVE_L - | addiu DISPATCH, JGL, -GG_DISP2G-32768 - | sw BASE, L->base - |1: - | bltz CRET1, >9 // Check for error from exit. - |. lw LFUNC:RB, FRAME_FUNC(BASE) - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | sll MULTRES, CRET1, 3 - | li TISNIL, LJ_TNIL - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | sw MULTRES, SAVE_MULTRES - | .FPU mtc1 TMP3, TOBIT - | lw TMP1, LFUNC:RB->pc - | sw r0, DISPATCH_GL(jit_base)(DISPATCH) - | lw KBASE, PC2PROTO(k)(TMP1) - | .FPU cvt.d.s TOBIT, TOBIT - | // Modified copy of ins_next which handles function header dispatch, too. - | lw INS, 0(PC) - | addiu PC, PC, 4 - | // Assumes TISNIL == ~LJ_VMST_INTERP == -1 - | sw TISNIL, DISPATCH_GL(vmstate)(DISPATCH) - | decode_OP4a TMP1, INS - | decode_OP4b TMP1 - | sltiu TMP2, TMP1, BC_FUNCF*4 - | addu TMP0, DISPATCH, TMP1 - | decode_RD8a RD, INS - | lw AT, 0(TMP0) - | decode_RA8a RA, INS - | beqz TMP2, >2 - |. decode_RA8b RA - | jr AT - |. decode_RD8b RD - |2: - | sltiu TMP2, TMP1, (BC_FUNCC+2)*4 // Fast function? - | bnez TMP2, >3 - |. lw TMP1, FRAME_PC(BASE) - | // Check frame below fast function. - | andi TMP0, TMP1, FRAME_TYPE - | bnez TMP0, >3 // Trace stitching continuation? - |. nop - | // Otherwise set KBASE for Lua function below fast function. - | lw TMP2, -4(TMP1) - | decode_RA8a TMP0, TMP2 - | decode_RA8b TMP0 - | subu TMP1, BASE, TMP0 - | lw LFUNC:TMP2, -8+FRAME_FUNC(TMP1) - | lw TMP1, LFUNC:TMP2->pc - | lw KBASE, PC2PROTO(k)(TMP1) - |3: - | addiu RC, MULTRES, -8 - | jr AT - |. addu RA, RA, BASE - | - |9: // Rethrow error from the right C frame. - | load_got lj_err_trace - | sub CARG2, r0, CRET1 - | call_intern lj_err_trace // (lua_State *L, int errcode) - |. move CARG1, L - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Hard-float round to integer. - |// Modifies AT, TMP0, FRET1, FRET2, f4. Keeps all others incl. FARG1. - |.macro vm_round_hf, func - | lui TMP0, 0x4330 // Hiword of 2^52 (double). - | mtc1 r0, f4 - | mtc1 TMP0, f5 - | abs.d FRET2, FARG1 // |x| - | mfc1 AT, f13 - | c.olt.d 0, FRET2, f4 - | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52 - | bc1f 0, >1 // Truncate only if |x| < 2^52. - |. sub.d FRET1, FRET1, f4 - | slt AT, AT, r0 - |.if "func" == "ceil" - | lui TMP0, 0xbff0 // Hiword of -1 (double). Preserves -0. - |.else - | lui TMP0, 0x3ff0 // Hiword of +1 (double). - |.endif - |.if "func" == "trunc" - | mtc1 TMP0, f5 - | c.olt.d 0, FRET2, FRET1 // |x| < result? - | sub.d FRET2, FRET1, f4 - | movt.d FRET1, FRET2, 0 // If yes, subtract +1. - | neg.d FRET2, FRET1 - | jr ra - |. movn.d FRET1, FRET2, AT // Merge sign bit back in. - |.else - | neg.d FRET2, FRET1 - | mtc1 TMP0, f5 - | movn.d FRET1, FRET2, AT // Merge sign bit back in. - |.if "func" == "ceil" - | c.olt.d 0, FRET1, FARG1 // x > result? - |.else - | c.olt.d 0, FARG1, FRET1 // x < result? - |.endif - | sub.d FRET2, FRET1, f4 // If yes, subtract +-1. - | jr ra - |. movt.d FRET1, FRET2, 0 - |.endif - |1: - | jr ra - |. mov.d FRET1, FARG1 - |.endmacro - | - |.macro vm_round, func - |.if FPU - | vm_round_hf, func - |.endif - |.endmacro - | - |->vm_floor: - | vm_round floor - |->vm_ceil: - | vm_round ceil - |->vm_trunc: - |.if JIT - | vm_round trunc - |.endif - | - |// Soft-float integer to number conversion. - |.macro sfi2d, AHI, ALO - |.if not FPU - | beqz ALO, >9 // Handle zero first. - |. sra TMP0, ALO, 31 - | xor TMP1, ALO, TMP0 - | subu TMP1, TMP1, TMP0 // Absolute value in TMP1. - | clz AHI, TMP1 - | andi TMP0, TMP0, 0x800 // Mask sign bit. - | li AT, 0x3ff+31-1 - | sllv TMP1, TMP1, AHI // Align mantissa left with leading 1. - | subu AHI, AT, AHI // Exponent - 1 in AHI. - | sll ALO, TMP1, 21 - | or AHI, AHI, TMP0 // Sign | Exponent. - | srl TMP1, TMP1, 11 - | sll AHI, AHI, 20 // Align left. - | jr ra - |. addu AHI, AHI, TMP1 // Add mantissa, increment exponent. - |9: - | jr ra - |. li AHI, 0 - |.endif - |.endmacro - | - |// Input SFARG1LO. Output: SFARG1*. Temporaries: AT, TMP0, TMP1. - |->vm_sfi2d_1: - | sfi2d SFARG1HI, SFARG1LO - | - |// Input SFARG2LO. Output: SFARG2*. Temporaries: AT, TMP0, TMP1. - |->vm_sfi2d_2: - | sfi2d SFARG2HI, SFARG2LO - | - |// Soft-float comparison. Equivalent to c.eq.d. - |// Input: SFARG*. Output: CRET1. Temporaries: AT, TMP0, TMP1. - |->vm_sfcmpeq: - |.if not FPU - | sll AT, SFARG1HI, 1 - | sll TMP0, SFARG2HI, 1 - | or CRET1, SFARG1LO, SFARG2LO - | or TMP1, AT, TMP0 - | or TMP1, TMP1, CRET1 - | beqz TMP1, >8 // Both args +-0: return 1. - |. sltu CRET1, r0, SFARG1LO - | lui TMP1, 0xffe0 - | addu AT, AT, CRET1 - | sltu CRET1, r0, SFARG2LO - | sltu AT, TMP1, AT - | addu TMP0, TMP0, CRET1 - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0; - |. xor TMP0, SFARG1HI, SFARG2HI - | xor TMP1, SFARG1LO, SFARG2LO - | or AT, TMP0, TMP1 - | jr ra - |. sltiu CRET1, AT, 1 // Same values: return 1. - |8: - | jr ra - |. li CRET1, 1 - |9: - | jr ra - |. li CRET1, 0 - |.endif - | - |// Soft-float comparison. Equivalent to c.ult.d and c.olt.d. - |// Input: SFARG*. Output: CRET1. Temporaries: AT, TMP0, TMP1, CRET2. - |->vm_sfcmpult: - |.if not FPU - | b >1 - |. li CRET2, 1 - |.endif - | - |->vm_sfcmpolt: - |.if not FPU - | li CRET2, 0 - |1: - | sll AT, SFARG1HI, 1 - | sll TMP0, SFARG2HI, 1 - | or CRET1, SFARG1LO, SFARG2LO - | or TMP1, AT, TMP0 - | or TMP1, TMP1, CRET1 - | beqz TMP1, >8 // Both args +-0: return 0. - |. sltu CRET1, r0, SFARG1LO - | lui TMP1, 0xffe0 - | addu AT, AT, CRET1 - | sltu CRET1, r0, SFARG2LO - | sltu AT, TMP1, AT - | addu TMP0, TMP0, CRET1 - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0 or 1; - |. and AT, SFARG1HI, SFARG2HI - | bltz AT, >5 // Both args negative? - |. nop - | beq SFARG1HI, SFARG2HI, >8 - |. sltu CRET1, SFARG1LO, SFARG2LO - | jr ra - |. slt CRET1, SFARG1HI, SFARG2HI - |5: // Swap conditions if both operands are negative. - | beq SFARG1HI, SFARG2HI, >8 - |. sltu CRET1, SFARG2LO, SFARG1LO - | jr ra - |. slt CRET1, SFARG2HI, SFARG1HI - |8: - | jr ra - |. nop - |9: - | jr ra - |. move CRET1, CRET2 - |.endif - | - |->vm_sfcmpogt: - |.if not FPU - | sll AT, SFARG2HI, 1 - | sll TMP0, SFARG1HI, 1 - | or CRET1, SFARG2LO, SFARG1LO - | or TMP1, AT, TMP0 - | or TMP1, TMP1, CRET1 - | beqz TMP1, >8 // Both args +-0: return 0. - |. sltu CRET1, r0, SFARG2LO - | lui TMP1, 0xffe0 - | addu AT, AT, CRET1 - | sltu CRET1, r0, SFARG1LO - | sltu AT, TMP1, AT - | addu TMP0, TMP0, CRET1 - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0 or 1; - |. and AT, SFARG2HI, SFARG1HI - | bltz AT, >5 // Both args negative? - |. nop - | beq SFARG2HI, SFARG1HI, >8 - |. sltu CRET1, SFARG2LO, SFARG1LO - | jr ra - |. slt CRET1, SFARG2HI, SFARG1HI - |5: // Swap conditions if both operands are negative. - | beq SFARG2HI, SFARG1HI, >8 - |. sltu CRET1, SFARG1LO, SFARG2LO - | jr ra - |. slt CRET1, SFARG1HI, SFARG2HI - |8: - | jr ra - |. nop - |9: - | jr ra - |. li CRET1, 0 - |.endif - | - |// Soft-float comparison. Equivalent to c.ole.d a, b or c.ole.d b, a. - |// Input: SFARG*, TMP3. Output: CRET1. Temporaries: AT, TMP0, TMP1. - |->vm_sfcmpolex: - |.if not FPU - | sll AT, SFARG1HI, 1 - | sll TMP0, SFARG2HI, 1 - | or CRET1, SFARG1LO, SFARG2LO - | or TMP1, AT, TMP0 - | or TMP1, TMP1, CRET1 - | beqz TMP1, >8 // Both args +-0: return 1. - |. sltu CRET1, r0, SFARG1LO - | lui TMP1, 0xffe0 - | addu AT, AT, CRET1 - | sltu CRET1, r0, SFARG2LO - | sltu AT, TMP1, AT - | addu TMP0, TMP0, CRET1 - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0; - |. and AT, SFARG1HI, SFARG2HI - | xor AT, AT, TMP3 - | bltz AT, >5 // Both args negative? - |. nop - | beq SFARG1HI, SFARG2HI, >6 - |. sltu CRET1, SFARG2LO, SFARG1LO - | jr ra - |. slt CRET1, SFARG2HI, SFARG1HI - |5: // Swap conditions if both operands are negative. - | beq SFARG1HI, SFARG2HI, >6 - |. sltu CRET1, SFARG1LO, SFARG2LO - | slt CRET1, SFARG1HI, SFARG2HI - |6: - | jr ra - |. nop - |8: - | jr ra - |. li CRET1, 1 - |9: - | jr ra - |. li CRET1, 0 - |.endif - | - |.macro sfmin_max, name, fpcall - |->vm_sf .. name: - |.if JIT and not FPU - | move TMP2, ra - | bal ->fpcall - |. nop - | move TMP0, CRET1 - | move SFRETHI, SFARG1HI - | move SFRETLO, SFARG1LO - | move ra, TMP2 - | movz SFRETHI, SFARG2HI, TMP0 - | jr ra - |. movz SFRETLO, SFARG2LO, TMP0 - |.endif - |.endmacro - | - | sfmin_max min, vm_sfcmpolt - | sfmin_max max, vm_sfcmpogt - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |.define NEXT_TAB, TAB:CARG1 - |.define NEXT_IDX, CARG2 - |.define NEXT_ASIZE, CARG3 - |.define NEXT_NIL, CARG4 - |.define NEXT_TMP0, r12 - |.define NEXT_TMP1, r13 - |.define NEXT_TMP2, r14 - |.define NEXT_RES_VK, CRET1 - |.define NEXT_RES_IDX, CRET2 - |.define NEXT_RES_PTR, sp - |.define NEXT_RES_VAL_I, 0(sp) - |.define NEXT_RES_VAL_IT, 4(sp) - |.define NEXT_RES_KEY_I, 8(sp) - |.define NEXT_RES_KEY_IT, 12(sp) - | - |// TValue *lj_vm_next(GCtab *t, uint32_t idx) - |// Next idx returned in CRET2. - |->vm_next: - |.if JIT and ENDIAN_LE - | lw NEXT_ASIZE, NEXT_TAB->asize - | lw NEXT_TMP0, NEXT_TAB->array - | li NEXT_NIL, LJ_TNIL - |1: // Traverse array part. - | sltu AT, NEXT_IDX, NEXT_ASIZE - | sll NEXT_TMP1, NEXT_IDX, 3 - | beqz AT, >5 - |. addu NEXT_TMP1, NEXT_TMP0, NEXT_TMP1 - | lw NEXT_TMP2, 4(NEXT_TMP1) - | sw NEXT_IDX, NEXT_RES_KEY_I - | beq NEXT_TMP2, NEXT_NIL, <1 - |. addiu NEXT_IDX, NEXT_IDX, 1 - | lw NEXT_TMP0, 0(NEXT_TMP1) - | li AT, LJ_TISNUM - | sw NEXT_TMP2, NEXT_RES_VAL_IT - | sw AT, NEXT_RES_KEY_IT - | sw NEXT_TMP0, NEXT_RES_VAL_I - | move NEXT_RES_VK, NEXT_RES_PTR - | jr ra - |. move NEXT_RES_IDX, NEXT_IDX - | - |5: // Traverse hash part. - | subu NEXT_RES_IDX, NEXT_IDX, NEXT_ASIZE - | lw NODE:NEXT_RES_VK, NEXT_TAB->node - | sll NEXT_TMP2, NEXT_RES_IDX, 5 - | lw NEXT_TMP0, NEXT_TAB->hmask - | sll AT, NEXT_RES_IDX, 3 - | subu AT, NEXT_TMP2, AT - | addu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, AT - |6: - | sltu AT, NEXT_TMP0, NEXT_RES_IDX - | bnez AT, >8 - |. nop - | lw NEXT_TMP2, NODE:NEXT_RES_VK->val.it - | bne NEXT_TMP2, NEXT_NIL, >9 - |. addiu NEXT_RES_IDX, NEXT_RES_IDX, 1 - | // Skip holes in hash part. - | b <6 - |. addiu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, sizeof(Node) - | - |8: // End of iteration. Set the key to nil (not the value). - | sw NEXT_NIL, NEXT_RES_KEY_IT - | move NEXT_RES_VK, NEXT_RES_PTR - |9: - | jr ra - |. addu NEXT_RES_IDX, NEXT_RES_IDX, NEXT_ASIZE - |.endif - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in r1, g in r2. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs - | lw CTSTATE, GL:r2->ctype_state - | addiu DISPATCH, r2, GG_G2DISP - | load_got lj_ccallback_enter - | sw r1, CTSTATE->cb.slot - | sw CARG1, CTSTATE->cb.gpr[0] - | sw CARG2, CTSTATE->cb.gpr[1] - | .FPU sdc1 FARG1, CTSTATE->cb.fpr[0] - | sw CARG3, CTSTATE->cb.gpr[2] - | sw CARG4, CTSTATE->cb.gpr[3] - | .FPU sdc1 FARG2, CTSTATE->cb.fpr[1] - | addiu TMP0, sp, CFRAME_SPACE+16 - | sw TMP0, CTSTATE->cb.stack - | sw r0, SAVE_PC // Any value outside of bytecode is ok. - | move CARG2, sp - | call_intern lj_ccallback_enter // (CTState *cts, void *cf) - |. move CARG1, CTSTATE - | // Returns lua_State *. - | lw BASE, L:CRET1->base - | lw RC, L:CRET1->top - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | move L, CRET1 - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | lw LFUNC:RB, FRAME_FUNC(BASE) - | .FPU mtc1 TMP3, TOBIT - | li_vmstate INTERP - | li TISNIL, LJ_TNIL - | subu RC, RC, BASE - | st_vmstate - | .FPU cvt.d.s TOBIT, TOBIT - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | load_got lj_ccallback_leave - | lw CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH) - | sw BASE, L->base - | sw RB, L->top - | sw L, CTSTATE->L - | move CARG2, RA - | call_intern lj_ccallback_leave // (CTState *cts, TValue *o) - |. move CARG1, CTSTATE - | .FPU ldc1 FRET1, CTSTATE->cb.fpr[0] - | lw CRET1, CTSTATE->cb.gpr[0] - | .FPU ldc1 FRET2, CTSTATE->cb.fpr[1] - | b ->vm_leave_unw - |. lw CRET2, CTSTATE->cb.gpr[1] - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, CARG1 - | lw TMP1, CCSTATE->spadj - | lbu CARG2, CCSTATE->nsp - | move TMP2, sp - | subu sp, sp, TMP1 - | sw ra, -4(TMP2) - | sll CARG2, CARG2, 2 - | sw r16, -8(TMP2) - | sw CCSTATE, -12(TMP2) - | move r16, TMP2 - | addiu TMP1, CCSTATE, offsetof(CCallState, stack) - | addiu TMP2, sp, 16 - | beqz CARG2, >2 - |. addu TMP3, TMP1, CARG2 - |1: - | lw TMP0, 0(TMP1) - | addiu TMP1, TMP1, 4 - | sltu AT, TMP1, TMP3 - | sw TMP0, 0(TMP2) - | bnez AT, <1 - |. addiu TMP2, TMP2, 4 - |2: - | lw CFUNCADDR, CCSTATE->func - | lw CARG2, CCSTATE->gpr[1] - | lw CARG3, CCSTATE->gpr[2] - | lw CARG4, CCSTATE->gpr[3] - | .FPU ldc1 FARG1, CCSTATE->fpr[0] - | .FPU ldc1 FARG2, CCSTATE->fpr[1] - | jalr CFUNCADDR - |. lw CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1. - | lw CCSTATE:TMP1, -12(r16) - | lw TMP2, -8(r16) - | lw ra, -4(r16) - | sw CRET1, CCSTATE:TMP1->gpr[0] - | sw CRET2, CCSTATE:TMP1->gpr[1] - |.if FPU - | sdc1 FRET1, CCSTATE:TMP1->fpr[0] - | sdc1 FRET2, CCSTATE:TMP1->fpr[1] - |.else - | sw CARG1, CCSTATE:TMP1->gpr[2] // Soft-float: complex double .im part. - | sw CARG2, CCSTATE:TMP1->gpr[3] - |.endif - | move sp, r16 - | jr ra - |. move r16, TMP2 - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RD = src2*8, JMP with RD = target - |.macro bc_comp, FRA, FRD, RAHI, RALO, RDHI, RDLO, movop, fmovop, fcomp, sfcomp - | addu RA, BASE, RA - | addu RD, BASE, RD - | lw RAHI, HI(RA) - | lw RDHI, HI(RD) - | lhu TMP2, OFS_RD(PC) - | addiu PC, PC, 4 - | bne RAHI, TISNUM, >2 - |. lw RALO, LO(RA) - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | lw RDLO, LO(RD) - | bne RDHI, TISNUM, >5 - |. decode_RD4b TMP2 - | slt AT, SFARG1LO, SFARG2LO - | addu TMP2, TMP2, TMP3 - | movop TMP2, r0, AT - |1: - | addu PC, PC, TMP2 - | ins_next - | - |2: // RA is not an integer. - | sltiu AT, RAHI, LJ_TISNUM - | beqz AT, ->vmeta_comp - |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sltiu AT, RDHI, LJ_TISNUM - |.if FPU - | ldc1 FRA, 0(RA) - | ldc1 FRD, 0(RD) - |.else - | lw RDLO, LO(RD) - |.endif - | beqz AT, >4 - |. decode_RD4b TMP2 - |3: // RA and RD are both numbers. - |.if FPU - | fcomp f20, f22 - | addu TMP2, TMP2, TMP3 - | b <1 - |. fmovop TMP2, r0 - |.else - | bal sfcomp - |. addu TMP2, TMP2, TMP3 - | b <1 - |. movop TMP2, r0, CRET1 - |.endif - | - |4: // RA is a number, RD is not a number. - | bne RDHI, TISNUM, ->vmeta_comp - | // RA is a number, RD is an integer. Convert RD to a number. - |.if FPU - |. lwc1 FRD, LO(RD) - | b <3 - |. cvt.d.w FRD, FRD - |.else - |. nop - |.if "RDHI" == "SFARG1HI" - | bal ->vm_sfi2d_1 - |.else - | bal ->vm_sfi2d_2 - |.endif - |. nop - | b <3 - |. nop - |.endif - | - |5: // RA is an integer, RD is not an integer - | sltiu AT, RDHI, LJ_TISNUM - | beqz AT, ->vmeta_comp - | // RA is an integer, RD is a number. Convert RA to a number. - |.if FPU - |. mtc1 RALO, FRA - | ldc1 FRD, 0(RD) - | b <3 - | cvt.d.w FRA, FRA - |.else - |. nop - |.if "RAHI" == "SFARG1HI" - | bal ->vm_sfi2d_1 - |.else - | bal ->vm_sfi2d_2 - |.endif - |. nop - | b <3 - |. nop - |.endif - |.endmacro - | - if (op == BC_ISLT) { - | bc_comp f20, f22, SFARG1HI, SFARG1LO, SFARG2HI, SFARG2LO, movz, movf, c.olt.d, ->vm_sfcmpolt - } else if (op == BC_ISGE) { - | bc_comp f20, f22, SFARG1HI, SFARG1LO, SFARG2HI, SFARG2LO, movn, movt, c.olt.d, ->vm_sfcmpolt - } else if (op == BC_ISLE) { - | bc_comp f22, f20, SFARG2HI, SFARG2LO, SFARG1HI, SFARG1LO, movn, movt, c.ult.d, ->vm_sfcmpult - } else { - | bc_comp f22, f20, SFARG2HI, SFARG2LO, SFARG1HI, SFARG1LO, movz, movf, c.ult.d, ->vm_sfcmpult - } - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RD = src2*8, JMP with RD = target - | addu RA, BASE, RA - | addiu PC, PC, 4 - | addu RD, BASE, RD - | lw SFARG1HI, HI(RA) - | lhu TMP2, -4+OFS_RD(PC) - | lw SFARG2HI, HI(RD) - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sltu AT, TISNUM, SFARG1HI - | sltu TMP0, TISNUM, SFARG2HI - | or AT, AT, TMP0 - if (vk) { - | beqz AT, ->BC_ISEQN_Z - } else { - | beqz AT, ->BC_ISNEN_Z - } - |. decode_RD4b TMP2 - | // Either or both types are not numbers. - | lw SFARG1LO, LO(RA) - | lw SFARG2LO, LO(RD) - | addu TMP2, TMP2, TMP3 - |.if FFI - | li TMP3, LJ_TCDATA - | beq SFARG1HI, TMP3, ->vmeta_equal_cd - |.endif - |. sltiu AT, SFARG1HI, LJ_TISPRI // Not a primitive? - |.if FFI - | beq SFARG2HI, TMP3, ->vmeta_equal_cd - |.endif - |. xor TMP3, SFARG1LO, SFARG2LO // Same tv? - | xor SFARG2HI, SFARG2HI, SFARG1HI // Same type? - | sltiu TMP0, SFARG1HI, LJ_TISTABUD+1 // Table or userdata? - | movz TMP3, r0, AT // Ignore tv if primitive. - | movn TMP0, r0, SFARG2HI // Tab/ud and same type? - | or AT, SFARG2HI, TMP3 // Same type && (pri||same tv). - | movz TMP0, r0, AT - | beqz TMP0, >1 // Done if not tab/ud or not same type or same tv. - if (vk) { - |. movn TMP2, r0, AT - } else { - |. movz TMP2, r0, AT - } - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | lw TAB:TMP1, TAB:SFARG1LO->metatable - | beqz TAB:TMP1, >1 // No metatable? - |. nop - | lbu TMP1, TAB:TMP1->nomm - | andi TMP1, TMP1, 1<1 // Or 'no __eq' flag set? - |. nop - | b ->vmeta_equal // Handle __eq metamethod. - |. li TMP0, 1-vk // ne = 0 or 1. - |1: - | addu PC, PC, TMP2 - | ins_next - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RD = str_const*8 (~), JMP with RD = target - | addu RA, BASE, RA - | addiu PC, PC, 4 - | lw TMP0, HI(RA) - | srl RD, RD, 1 - | lw STR:TMP3, LO(RA) - | subu RD, KBASE, RD - | lhu TMP2, -4+OFS_RD(PC) - |.if FFI - | li AT, LJ_TCDATA - | beq TMP0, AT, ->vmeta_equal_cd - |.endif - |. lw STR:TMP1, -4(RD) // KBASE-4-str_const*4 - | addiu TMP0, TMP0, -LJ_TSTR - | decode_RD4b TMP2 - | xor TMP1, STR:TMP1, STR:TMP3 - | or TMP0, TMP0, TMP1 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - if (vk) { - | movn TMP2, r0, TMP0 - } else { - | movz TMP2, r0, TMP0 - } - | addu PC, PC, TMP2 - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RD = num_const*8, JMP with RD = target - | addu RA, BASE, RA - | addu RD, KBASE, RD - | lw SFARG1HI, HI(RA) - | lw SFARG2HI, HI(RD) - | lhu TMP2, OFS_RD(PC) - | addiu PC, PC, 4 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | decode_RD4b TMP2 - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | bne SFARG1HI, TISNUM, >3 - |. lw SFARG1LO, LO(RA) - | lw SFARG2LO, LO(RD) - | addu TMP2, TMP2, TMP3 - | bne SFARG2HI, TISNUM, >6 - |. xor AT, SFARG1LO, SFARG2LO - if (vk) { - | movn TMP2, r0, AT - |1: - | addu PC, PC, TMP2 - |2: - } else { - | movz TMP2, r0, AT - |1: - |2: - | addu PC, PC, TMP2 - } - | ins_next - | - |3: // RA is not an integer. - | sltiu AT, SFARG1HI, LJ_TISNUM - |.if FFI - | beqz AT, >8 - |.else - | beqz AT, <2 - |.endif - |. addu TMP2, TMP2, TMP3 - | sltiu AT, SFARG2HI, LJ_TISNUM - |.if FPU - | ldc1 f20, 0(RA) - | ldc1 f22, 0(RD) - |.endif - | beqz AT, >5 - |. lw SFARG2LO, LO(RD) - |4: // RA and RD are both numbers. - |.if FPU - | c.eq.d f20, f22 - | b <1 - if (vk) { - |. movf TMP2, r0 - } else { - |. movt TMP2, r0 - } - |.else - | bal ->vm_sfcmpeq - |. nop - | b <1 - if (vk) { - |. movz TMP2, r0, CRET1 - } else { - |. movn TMP2, r0, CRET1 - } - |.endif - | - |5: // RA is a number, RD is not a number. - |.if FFI - | bne SFARG2HI, TISNUM, >9 - |.else - | bne SFARG2HI, TISNUM, <2 - |.endif - | // RA is a number, RD is an integer. Convert RD to a number. - |.if FPU - |. lwc1 f22, LO(RD) - | b <4 - |. cvt.d.w f22, f22 - |.else - |. nop - | bal ->vm_sfi2d_2 - |. nop - | b <4 - |. nop - |.endif - | - |6: // RA is an integer, RD is not an integer - | sltiu AT, SFARG2HI, LJ_TISNUM - |.if FFI - | beqz AT, >9 - |.else - | beqz AT, <2 - |.endif - | // RA is an integer, RD is a number. Convert RA to a number. - |.if FPU - |. mtc1 SFARG1LO, f20 - | ldc1 f22, 0(RD) - | b <4 - | cvt.d.w f20, f20 - |.else - |. nop - | bal ->vm_sfi2d_1 - |. nop - | b <4 - |. nop - |.endif - | - |.if FFI - |8: - | li AT, LJ_TCDATA - | bne SFARG1HI, AT, <2 - |. nop - | b ->vmeta_equal_cd - |. nop - |9: - | li AT, LJ_TCDATA - | bne SFARG2HI, AT, <2 - |. nop - | b ->vmeta_equal_cd - |. nop - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target - | addu RA, BASE, RA - | srl TMP1, RD, 3 - | lw TMP0, HI(RA) - | lhu TMP2, OFS_RD(PC) - | not TMP1, TMP1 - | addiu PC, PC, 4 - |.if FFI - | li AT, LJ_TCDATA - | beq TMP0, AT, ->vmeta_equal_cd - |.endif - |. xor TMP0, TMP0, TMP1 - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - if (vk) { - | movn TMP2, r0, TMP0 - } else { - | movz TMP2, r0, TMP0 - } - | addu PC, PC, TMP2 - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RD = src*8, JMP with RD = target - | addu RD, BASE, RD - | lhu TMP2, OFS_RD(PC) - | lw TMP0, HI(RD) - | addiu PC, PC, 4 - if (op == BC_IST || op == BC_ISF) { - | sltiu TMP0, TMP0, LJ_TISTRUECOND - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - if (op == BC_IST) { - | movz TMP2, r0, TMP0 - } else { - | movn TMP2, r0, TMP0 - } - | addu PC, PC, TMP2 - } else { - | sltiu TMP0, TMP0, LJ_TISTRUECOND - | lw SFRETHI, HI(RD) - | lw SFRETLO, LO(RD) - if (op == BC_ISTC) { - | beqz TMP0, >1 - } else { - | bnez TMP0, >1 - } - |. addu RA, BASE, RA - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | addu PC, PC, TMP2 - |1: - } - | ins_next - break; - - case BC_ISTYPE: - | // RA = src*8, RD = -type*8 - | addu TMP2, BASE, RA - | srl TMP1, RD, 3 - | lw TMP0, HI(TMP2) - | ins_next1 - | addu AT, TMP0, TMP1 - | bnez AT, ->vmeta_istype - |. ins_next2 - break; - case BC_ISNUM: - | // RA = src*8, RD = -(TISNUM-1)*8 - | addu TMP2, BASE, RA - | lw TMP0, HI(TMP2) - | ins_next1 - | sltiu AT, TMP0, LJ_TISNUM - | beqz AT, ->vmeta_istype - |. ins_next2 - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RD = src*8 - | addu RD, BASE, RD - | addu RA, BASE, RA - | lw SFRETHI, HI(RD) - | lw SFRETLO, LO(RD) - | ins_next1 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - break; - case BC_NOT: - | // RA = dst*8, RD = src*8 - | addu RD, BASE, RD - | addu RA, BASE, RA - | lw TMP0, HI(RD) - | li TMP1, LJ_TFALSE - | sltiu TMP0, TMP0, LJ_TISTRUECOND - | addiu TMP1, TMP0, LJ_TTRUE - | ins_next1 - | sw TMP1, HI(RA) - | ins_next2 - break; - case BC_UNM: - | // RA = dst*8, RD = src*8 - | addu RB, BASE, RD - | lw SFARG1HI, HI(RB) - | addu RA, BASE, RA - | bne SFARG1HI, TISNUM, >2 - |. lw SFARG1LO, LO(RB) - | lui TMP1, 0x8000 - | beq SFARG1LO, TMP1, ->vmeta_unm // Meta handler deals with -2^31. - |. negu SFARG1LO, SFARG1LO - |1: - | ins_next1 - | sw SFARG1HI, HI(RA) - | sw SFARG1LO, LO(RA) - | ins_next2 - |2: - | sltiu AT, SFARG1HI, LJ_TISNUM - | beqz AT, ->vmeta_unm - |. lui TMP1, 0x8000 - | b <1 - |. xor SFARG1HI, SFARG1HI, TMP1 - break; - case BC_LEN: - | // RA = dst*8, RD = src*8 - | addu CARG2, BASE, RD - | addu RA, BASE, RA - | lw TMP0, HI(CARG2) - | lw CARG1, LO(CARG2) - | li AT, LJ_TSTR - | bne TMP0, AT, >2 - |. li AT, LJ_TTAB - | lw CRET1, STR:CARG1->len - |1: - | ins_next1 - | sw TISNUM, HI(RA) - | sw CRET1, LO(RA) - | ins_next2 - |2: - | bne TMP0, AT, ->vmeta_len - |. nop -#if LJ_52 - | lw TAB:TMP2, TAB:CARG1->metatable - | bnez TAB:TMP2, >9 - |. nop - |3: -#endif - |->BC_LEN_Z: - | load_got lj_tab_len - | call_intern lj_tab_len // (GCtab *t) - |. nop - | // Returns uint32_t (but less than 2^31). - | b <1 - |. nop -#if LJ_52 - |9: - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_len - |. nop -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro fpmod, a, b, c - | bal ->vm_floor // floor(b/c) - |. div.d FARG1, b, c - | mul.d a, FRET1, c - | sub.d a, b, a // b - floor(b/c)*c - |.endmacro - - |.macro sfpmod - | addiu sp, sp, -16 - | - | load_got __divdf3 - | sw SFARG1HI, HI(sp) - | sw SFARG1LO, LO(sp) - | sw SFARG2HI, 8+HI(sp) - | call_extern - |. sw SFARG2LO, 8+LO(sp) - | - | load_got floor - | move SFARG1HI, SFRETHI - | call_extern - |. move SFARG1LO, SFRETLO - | - | load_got __muldf3 - | move SFARG1HI, SFRETHI - | move SFARG1LO, SFRETLO - | lw SFARG2HI, 8+HI(sp) - | call_extern - |. lw SFARG2LO, 8+LO(sp) - | - | load_got __subdf3 - | lw SFARG1HI, HI(sp) - | lw SFARG1LO, LO(sp) - | move SFARG2HI, SFRETHI - | call_extern - |. move SFARG2LO, SFRETLO - | - | addiu sp, sp, 16 - |.endmacro - - |.macro ins_arithpre, label - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||switch (vk) { - ||case 0: - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | // RA = dst*8, RB = src1*8, RC = num_const*8 - | addu RB, BASE, RB - |.if "label" ~= "none" - | b label - |.endif - |. addu RC, KBASE, RC - || break; - ||case 1: - | decode_RB8a RC, INS - | decode_RB8b RC - | decode_RDtoRC8 RB, RD - | // RA = dst*8, RB = num_const*8, RC = src1*8 - | addu RC, BASE, RC - |.if "label" ~= "none" - | b label - |.endif - |. addu RB, KBASE, RB - || break; - ||default: - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | // RA = dst*8, RB = src1*8, RC = src2*8 - | addu RB, BASE, RB - |.if "label" ~= "none" - | b label - |.endif - |. addu RC, BASE, RC - || break; - ||} - |.endmacro - | - |.macro ins_arith, intins, fpins, fpcall, label - | ins_arithpre none - | - |.if "label" ~= "none" - |label: - |.endif - | - | lw SFARG1HI, HI(RB) - | lw SFARG2HI, HI(RC) - | - |.if "intins" ~= "div" - | - | // Check for two integers. - | lw SFARG1LO, LO(RB) - | bne SFARG1HI, TISNUM, >5 - |. lw SFARG2LO, LO(RC) - | bne SFARG2HI, TISNUM, >5 - | - |.if "intins" == "addu" - |. intins CRET1, SFARG1LO, SFARG2LO - | xor TMP1, CRET1, SFARG1LO // ((y^a) & (y^b)) < 0: overflow. - | xor TMP2, CRET1, SFARG2LO - | and TMP1, TMP1, TMP2 - | bltz TMP1, ->vmeta_arith - |. addu RA, BASE, RA - |.elif "intins" == "subu" - |. intins CRET1, SFARG1LO, SFARG2LO - | xor TMP1, CRET1, SFARG1LO // ((y^a) & (a^b)) < 0: overflow. - | xor TMP2, SFARG1LO, SFARG2LO - | and TMP1, TMP1, TMP2 - | bltz TMP1, ->vmeta_arith - |. addu RA, BASE, RA - |.elif "intins" == "mult" - |. intins SFARG1LO, SFARG2LO - | mflo CRET1 - | mfhi TMP2 - | sra TMP1, CRET1, 31 - | bne TMP1, TMP2, ->vmeta_arith - |. addu RA, BASE, RA - |.else - |. load_got lj_vm_modi - | beqz SFARG2LO, ->vmeta_arith - |. addu RA, BASE, RA - |.if ENDIAN_BE - | move CARG1, SFARG1LO - |.endif - | call_extern - |. move CARG2, SFARG2LO - |.endif - | - | ins_next1 - | sw TISNUM, HI(RA) - | sw CRET1, LO(RA) - |3: - | ins_next2 - | - |.elif not FPU - | - | lw SFARG1LO, LO(RB) - | lw SFARG2LO, LO(RC) - | - |.endif - | - |5: // Check for two numbers. - | .FPU ldc1 f20, 0(RB) - | sltiu AT, SFARG1HI, LJ_TISNUM - | sltiu TMP0, SFARG2HI, LJ_TISNUM - | .FPU ldc1 f22, 0(RC) - | and AT, AT, TMP0 - | beqz AT, ->vmeta_arith - |. addu RA, BASE, RA - | - |.if FPU - | fpins FRET1, f20, f22 - |.elif "fpcall" == "sfpmod" - | sfpmod - |.else - | load_got fpcall - | call_extern - |. nop - |.endif - | - | ins_next1 - |.if not FPU - | sw SFRETHI, HI(RA) - |.endif - |.if "intins" ~= "div" - | b <3 - |.endif - |.if FPU - |. sdc1 FRET1, 0(RA) - |.else - |. sw SFRETLO, LO(RA) - |.endif - |.if "intins" == "div" - | ins_next2 - |.endif - | - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith addu, add.d, __adddf3, none - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith subu, sub.d, __subdf3, none - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith mult, mul.d, __muldf3, none - break; - case BC_DIVVN: - | ins_arith div, div.d, __divdf3, ->BC_DIVVN_Z - break; - case BC_DIVNV: case BC_DIVVV: - | ins_arithpre ->BC_DIVVN_Z - break; - case BC_MODVN: - | ins_arith modi, fpmod, sfpmod, ->BC_MODVN_Z - break; - case BC_MODNV: case BC_MODVV: - | ins_arithpre ->BC_MODVN_Z - break; - case BC_POW: - | ins_arithpre none - | lw SFARG1HI, HI(RB) - | lw SFARG2HI, HI(RC) - | sltiu AT, SFARG1HI, LJ_TISNUM - | sltiu TMP0, SFARG2HI, LJ_TISNUM - | and AT, AT, TMP0 - | load_got pow - | beqz AT, ->vmeta_arith - |. addu RA, BASE, RA - |.if FPU - | ldc1 FARG1, 0(RB) - | ldc1 FARG2, 0(RC) - |.else - | lw SFARG1LO, LO(RB) - | lw SFARG2LO, LO(RC) - |.endif - | call_extern - |. nop - | ins_next1 - |.if FPU - | sdc1 FRET1, 0(RA) - |.else - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - |.endif - | ins_next2 - break; - - case BC_CAT: - | // RA = dst*8, RB = src_start*8, RC = src_end*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | subu CARG3, RC, RB - | sw BASE, L->base - | addu CARG2, BASE, RC - | move MULTRES, RB - |->BC_CAT_Z: - | load_got lj_meta_cat - | srl CARG3, CARG3, 3 - | sw PC, SAVE_PC - | call_intern lj_meta_cat // (lua_State *L, TValue *top, int left) - |. move CARG1, L - | // Returns NULL (finished) or TValue * (metamethod). - | bnez CRET1, ->vmeta_binop - |. lw BASE, L->base - | addu RB, BASE, MULTRES - | lw SFRETHI, HI(RB) - | lw SFRETLO, LO(RB) - | addu RA, BASE, RA - | ins_next1 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RD = str_const*8 (~) - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | ins_next1 - | lw TMP0, -4(TMP1) // KBASE-4-str_const*4 - | addu RA, BASE, RA - | li TMP2, LJ_TSTR - | sw TMP0, LO(RA) - | sw TMP2, HI(RA) - | ins_next2 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RD = cdata_const*8 (~) - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | ins_next1 - | lw TMP0, -4(TMP1) // KBASE-4-cdata_const*4 - | addu RA, BASE, RA - | li TMP2, LJ_TCDATA - | sw TMP0, LO(RA) - | sw TMP2, HI(RA) - | ins_next2 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, RD = int16_literal*8 - | sra RD, INS, 16 - | addu RA, BASE, RA - | ins_next1 - | sw TISNUM, HI(RA) - | sw RD, LO(RA) - | ins_next2 - break; - case BC_KNUM: - | // RA = dst*8, RD = num_const*8 - | addu RD, KBASE, RD - | addu RA, BASE, RA - | lw SFRETHI, HI(RD) - | lw SFRETLO, LO(RD) - | ins_next1 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - break; - case BC_KPRI: - | // RA = dst*8, RD = primitive_type*8 (~) - | srl TMP1, RD, 3 - | addu RA, BASE, RA - | not TMP0, TMP1 - | ins_next1 - | sw TMP0, HI(RA) - | ins_next2 - break; - case BC_KNIL: - | // RA = base*8, RD = end*8 - | addu RA, BASE, RA - | sw TISNIL, HI(RA) - | addiu RA, RA, 8 - | addu RD, BASE, RD - |1: - | sw TISNIL, HI(RA) - | slt AT, RA, RD - | bnez AT, <1 - |. addiu RA, RA, 8 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RD = uvnum*8 - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RD, RD, 1 - | addu RD, RD, LFUNC:RB - | lw UPVAL:RB, LFUNC:RD->uvptr - | ins_next1 - | lw TMP1, UPVAL:RB->v - | lw SFRETHI, HI(TMP1) - | lw SFRETLO, LO(TMP1) - | addu RA, BASE, RA - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - break; - case BC_USETV: - | // RA = uvnum*8, RD = src*8 - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | addu RD, BASE, RD - | addu RA, RA, LFUNC:RB - | lw UPVAL:RB, LFUNC:RA->uvptr - | lw SFRETHI, HI(RD) - | lw SFRETLO, LO(RD) - | lbu TMP3, UPVAL:RB->marked - | lw CARG2, UPVAL:RB->v - | andi TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbu TMP0, UPVAL:RB->closed - | sw SFRETHI, HI(CARG2) - | sw SFRETLO, LO(CARG2) - | li AT, LJ_GC_BLACK|1 - | or TMP3, TMP3, TMP0 - | beq TMP3, AT, >2 // Upvalue is closed and black? - |. addiu TMP2, SFRETHI, -(LJ_TNUMX+1) - |1: - | ins_next - | - |2: // Check if new value is collectable. - | sltiu AT, TMP2, LJ_TISGCV - (LJ_TNUMX+1) - | beqz AT, <1 // tvisgcv(v) - |. nop - | lbu TMP3, GCOBJ:SFRETLO->gch.marked - | andi TMP3, TMP3, LJ_GC_WHITES // iswhite(v) - | beqz TMP3, <1 - |. load_got lj_gc_barrieruv - | // Crossed a write barrier. Move the barrier forward. - | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv) - |. addiu CARG1, DISPATCH, GG_DISP2G - | b <1 - |. nop - break; - case BC_USETS: - | // RA = uvnum*8, RD = str_const*8 (~) - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | srl TMP1, RD, 1 - | addu RA, RA, LFUNC:RB - | subu TMP1, KBASE, TMP1 - | lw UPVAL:RB, LFUNC:RA->uvptr - | lw STR:TMP1, -4(TMP1) // KBASE-4-str_const*4 - | lbu TMP2, UPVAL:RB->marked - | lw CARG2, UPVAL:RB->v - | lbu TMP3, STR:TMP1->marked - | andi AT, TMP2, LJ_GC_BLACK // isblack(uv) - | lbu TMP2, UPVAL:RB->closed - | li TMP0, LJ_TSTR - | sw STR:TMP1, LO(CARG2) - | bnez AT, >2 - |. sw TMP0, HI(CARG2) - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | beqz TMP2, <1 - |. andi AT, TMP3, LJ_GC_WHITES // iswhite(str) - | beqz AT, <1 - |. load_got lj_gc_barrieruv - | // Crossed a write barrier. Move the barrier forward. - | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv) - |. addiu CARG1, DISPATCH, GG_DISP2G - | b <1 - |. nop - break; - case BC_USETN: - | // RA = uvnum*8, RD = num_const*8 - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | addu RD, KBASE, RD - | addu RA, RA, LFUNC:RB - | lw UPVAL:RB, LFUNC:RA->uvptr - | lw SFRETHI, HI(RD) - | lw SFRETLO, LO(RD) - | lw TMP1, UPVAL:RB->v - | ins_next1 - | sw SFRETHI, HI(TMP1) - | sw SFRETLO, LO(TMP1) - | ins_next2 - break; - case BC_USETP: - | // RA = uvnum*8, RD = primitive_type*8 (~) - | lw LFUNC:RB, FRAME_FUNC(BASE) - | srl RA, RA, 1 - | srl TMP0, RD, 3 - | addu RA, RA, LFUNC:RB - | not TMP0, TMP0 - | lw UPVAL:RB, LFUNC:RA->uvptr - | ins_next1 - | lw TMP1, UPVAL:RB->v - | sw TMP0, HI(TMP1) - | ins_next2 - break; - - case BC_UCLO: - | // RA = level*8, RD = target - | lw TMP2, L->openupval - | branch_RD // Do this first since RD is not saved. - | load_got lj_func_closeuv - | sw BASE, L->base - | beqz TMP2, >1 - |. move CARG1, L - | call_intern lj_func_closeuv // (lua_State *L, TValue *level) - |. addu CARG2, BASE, RA - | lw BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype) - | srl TMP1, RD, 1 - | load_got lj_func_newL_gc - | subu TMP1, KBASE, TMP1 - | lw CARG3, FRAME_FUNC(BASE) - | lw CARG2, -4(TMP1) // KBASE-4-tab_const*4 - | sw BASE, L->base - | sw PC, SAVE_PC - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | call_intern lj_func_newL_gc - |. move CARG1, L - | // Returns GCfuncL *. - | lw BASE, L->base - | li TMP0, LJ_TFUNC - | ins_next1 - | addu RA, BASE, RA - | sw LFUNC:CRET1, LO(RA) - | sw TMP0, HI(RA) - | ins_next2 - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~) - | lw TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lw TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | sw BASE, L->base - | sw PC, SAVE_PC - | sltu AT, TMP0, TMP1 - | beqz AT, >5 - |1: - if (op == BC_TNEW) { - | load_got lj_tab_new - | srl CARG2, RD, 3 - | andi CARG2, CARG2, 0x7ff - | li TMP0, 0x801 - | addiu AT, CARG2, -0x7ff - | srl CARG3, RD, 14 - | movz CARG2, TMP0, AT - | // (lua_State *L, int32_t asize, uint32_t hbits) - | call_intern lj_tab_new - |. move CARG1, L - | // Returns Table *. - } else { - | load_got lj_tab_dup - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | move CARG1, L - | call_intern lj_tab_dup // (lua_State *L, Table *kt) - |. lw CARG2, -4(TMP1) // KBASE-4-str_const*4 - | // Returns Table *. - } - | lw BASE, L->base - | ins_next1 - | addu RA, BASE, RA - | li TMP0, LJ_TTAB - | sw TAB:CRET1, LO(RA) - | sw TMP0, HI(RA) - | ins_next2 - |5: - | load_got lj_gc_step_fixtop - | move MULTRES, RD - | call_intern lj_gc_step_fixtop // (lua_State *L) - |. move CARG1, L - | b <1 - |. move RD, MULTRES - break; - - case BC_GGET: - | // RA = dst*8, RD = str_const*8 (~) - case BC_GSET: - | // RA = src*8, RD = str_const*8 (~) - | lw LFUNC:TMP2, FRAME_FUNC(BASE) - | srl TMP1, RD, 1 - | subu TMP1, KBASE, TMP1 - | lw TAB:RB, LFUNC:TMP2->env - | lw STR:RC, -4(TMP1) // KBASE-4-str_const*4 - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - |. addu RA, BASE, RA - break; - - case BC_TGETV: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu CARG2, BASE, RB - | addu CARG3, BASE, RC - | lw TMP1, HI(CARG2) - | lw TMP2, HI(CARG3) - | lw TAB:RB, LO(CARG2) - | li AT, LJ_TTAB - | bne TMP1, AT, ->vmeta_tgetv - |. addu RA, BASE, RA - | bne TMP2, TISNUM, >5 - |. lw RC, LO(CARG3) - | lw TMP0, TAB:RB->asize - | lw TMP1, TAB:RB->array - | sltu AT, RC, TMP0 - | sll TMP2, RC, 3 - | beqz AT, ->vmeta_tgetv // Integer key and in array part? - |. addu TMP2, TMP1, TMP2 - | lw SFRETHI, HI(TMP2) - | beq SFRETHI, TISNIL, >2 - |. lw SFRETLO, LO(TMP2) - |1: - | ins_next1 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - | - |2: // Check for __index if table value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_tgetv - |. nop - | - |5: - | li AT, LJ_TSTR - | bne TMP2, AT, ->vmeta_tgetv - |. nop - | b ->BC_TGETS_Z // String key? - |. nop - break; - case BC_TGETS: - | // RA = dst*8, RB = table*8, RC = str_const*4 (~) - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RC4a RC, INS - | lw TMP0, HI(CARG2) - | decode_RC4b RC - | li AT, LJ_TTAB - | lw TAB:RB, LO(CARG2) - | subu CARG3, KBASE, RC - | lw STR:RC, -4(CARG3) // KBASE-4-str_const*4 - | bne TMP0, AT, ->vmeta_tgets1 - |. addu RA, BASE, RA - |->BC_TGETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8 - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->sid - | lw NODE:TMP2, TAB:RB->node - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | addu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | lw CARG1, offsetof(Node, key)+HI(NODE:TMP2) - | lw TMP0, offsetof(Node, key)+LO(NODE:TMP2) - | lw NODE:TMP1, NODE:TMP2->next - | lw SFRETHI, offsetof(Node, val)+HI(NODE:TMP2) - | addiu CARG1, CARG1, -LJ_TSTR - | xor TMP0, TMP0, STR:RC - | or AT, CARG1, TMP0 - | bnez AT, >4 - |. lw TAB:TMP3, TAB:RB->metatable - | beq SFRETHI, TISNIL, >5 // Key found, but nil value? - |. lw SFRETLO, offsetof(Node, val)+LO(NODE:TMP2) - |3: - | ins_next1 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - | - |4: // Follow hash chain. - | bnez NODE:TMP1, <1 - |. move NODE:TMP2, NODE:TMP1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | beqz TAB:TMP3, <3 // No metatable: done. - |. li SFRETHI, LJ_TNIL - | lbu TMP0, TAB:TMP3->nomm - | andi TMP0, TMP0, 1<vmeta_tgets - |. nop - break; - case BC_TGETB: - | // RA = dst*8, RB = table*8, RC = index*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RDtoRC8 RC, RD - | lw CARG1, HI(CARG2) - | li AT, LJ_TTAB - | lw TAB:RB, LO(CARG2) - | addu RA, BASE, RA - | bne CARG1, AT, ->vmeta_tgetb - |. srl TMP0, RC, 3 - | lw TMP1, TAB:RB->asize - | lw TMP2, TAB:RB->array - | sltu AT, TMP0, TMP1 - | beqz AT, ->vmeta_tgetb - |. addu RC, TMP2, RC - | lw SFRETHI, HI(RC) - | beq SFRETHI, TISNIL, >5 - |. lw SFRETLO, LO(RC) - |1: - | ins_next1 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | ins_next2 - | - |5: // Check for __index if table value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP1, TAB:TMP2->nomm - | andi TMP1, TMP1, 1<vmeta_tgetb // Caveat: preserve TMP0 and CARG2! - |. nop - break; - case BC_TGETR: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu RB, BASE, RB - | addu RC, BASE, RC - | lw TAB:CARG1, LO(RB) - | lw CARG2, LO(RC) - | addu RA, BASE, RA - | lw TMP0, TAB:CARG1->asize - | lw TMP1, TAB:CARG1->array - | sltu AT, CARG2, TMP0 - | sll TMP2, CARG2, 3 - | beqz AT, ->vmeta_tgetr // In array part? - |. addu CRET1, TMP1, TMP2 - | lw SFARG2HI, HI(CRET1) - | lw SFARG2LO, LO(CRET1) - |->BC_TGETR_Z: - | ins_next1 - | sw SFARG2HI, HI(RA) - | sw SFARG2LO, LO(RA) - | ins_next2 - break; - - case BC_TSETV: - | // RA = src*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu CARG2, BASE, RB - | addu CARG3, BASE, RC - | lw TMP1, HI(CARG2) - | lw TMP2, HI(CARG3) - | lw TAB:RB, LO(CARG2) - | li AT, LJ_TTAB - | bne TMP1, AT, ->vmeta_tsetv - |. addu RA, BASE, RA - | bne TMP2, TISNUM, >5 - |. lw RC, LO(CARG3) - | lw TMP0, TAB:RB->asize - | lw TMP1, TAB:RB->array - | sltu AT, RC, TMP0 - | sll TMP2, RC, 3 - | beqz AT, ->vmeta_tsetv // Integer key and in array part? - |. addu TMP1, TMP1, TMP2 - | lw TMP0, HI(TMP1) - | lbu TMP3, TAB:RB->marked - | lw SFRETHI, HI(RA) - | beq TMP0, TISNIL, >3 - |. lw SFRETLO, LO(RA) - |1: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | sw SFRETHI, HI(TMP1) - | bnez AT, >7 - |. sw SFRETLO, LO(TMP1) - |2: - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP2, TAB:TMP2->nomm - | andi TMP2, TMP2, 1<vmeta_tsetv - |. nop - | - |5: - | li AT, LJ_TSTR - | bne TMP2, AT, ->vmeta_tsetv - |. nop - | b ->BC_TSETS_Z // String key? - |. nop - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <2 - break; - case BC_TSETS: - | // RA = src*8, RB = table*8, RC = str_const*8 (~) - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RC4a RC, INS - | lw TMP0, HI(CARG2) - | decode_RC4b RC - | li AT, LJ_TTAB - | subu CARG3, KBASE, RC - | lw TAB:RB, LO(CARG2) - | lw STR:RC, -4(CARG3) // KBASE-4-str_const*4 - | bne TMP0, AT, ->vmeta_tsets1 - |. addu RA, BASE, RA - |->BC_TSETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = BASE+src*8 - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->sid - | lw NODE:TMP2, TAB:RB->node - | sb r0, TAB:RB->nomm // Clear metamethod cache. - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | addu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |.if FPU - | ldc1 f20, 0(RA) - |.else - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - |.endif - |1: - | lw CARG1, offsetof(Node, key)+HI(NODE:TMP2) - | lw TMP0, offsetof(Node, key)+LO(NODE:TMP2) - | li AT, LJ_TSTR - | lw NODE:TMP1, NODE:TMP2->next - | bne CARG1, AT, >5 - |. lw CARG2, offsetof(Node, val)+HI(NODE:TMP2) - | bne TMP0, STR:RC, >5 - |. lbu TMP3, TAB:RB->marked - | beq CARG2, TISNIL, >4 // Key found, but nil value? - |. lw TAB:TMP0, TAB:RB->metatable - |2: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - |.if FPU - | bnez AT, >7 - |. sdc1 f20, NODE:TMP2->val - |.else - | sw SFRETHI, NODE:TMP2->val.u32.hi - | bnez AT, >7 - |. sw SFRETLO, NODE:TMP2->val.u32.lo - |.endif - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | beqz TAB:TMP0, <2 // No metatable: done. - |. nop - | lbu TMP0, TAB:TMP0->nomm - | andi TMP0, TMP0, 1<vmeta_tsets - |. nop - | - |5: // Follow hash chain. - | bnez NODE:TMP1, <1 - |. move NODE:TMP2, NODE:TMP1 - | // End of hash chain: key not found, add a new one - | - | // But check for __newindex first. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, >6 // No metatable: continue. - |. addiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |. li AT, LJ_TSTR - |6: - | load_got lj_tab_newkey - | sw STR:RC, LO(CARG3) - | sw AT, HI(CARG3) - | sw BASE, L->base - | move CARG2, TAB:RB - | sw PC, SAVE_PC - | call_intern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k - |. move CARG1, L - | // Returns TValue *. - | lw BASE, L->base - |.if FPU - | b <3 // No 2nd write barrier needed. - |. sdc1 f20, 0(CRET1) - |.else - | lw SFARG1HI, HI(RA) - | lw SFARG1LO, LO(RA) - | sw SFARG1HI, HI(CRET1) - | b <3 // No 2nd write barrier needed. - |. sw SFARG1LO, LO(CRET1) - |.endif - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <3 - break; - case BC_TSETB: - | // RA = src*8, RB = table*8, RC = index*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | addu CARG2, BASE, RB - | decode_RDtoRC8 RC, RD - | lw CARG1, HI(CARG2) - | li AT, LJ_TTAB - | lw TAB:RB, LO(CARG2) - | addu RA, BASE, RA - | bne CARG1, AT, ->vmeta_tsetb - |. srl TMP0, RC, 3 - | lw TMP1, TAB:RB->asize - | lw TMP2, TAB:RB->array - | sltu AT, TMP0, TMP1 - | beqz AT, ->vmeta_tsetb - |. addu RC, TMP2, RC - | lw TMP1, HI(RC) - | lbu TMP3, TAB:RB->marked - | beq TMP1, TISNIL, >5 - |1: - |. lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | sw SFRETHI, HI(RC) - | bnez AT, >7 - |. sw SFRETLO, LO(RC) - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | lw TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP1, TAB:TMP2->nomm - | andi TMP1, TMP1, 1<vmeta_tsetb // Caveat: preserve TMP0 and CARG2! - |. nop - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <2 - break; - case BC_TSETR: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | addu CARG1, BASE, RB - | addu CARG3, BASE, RC - | lw TAB:CARG2, LO(CARG1) - | lw CARG3, LO(CARG3) - | lbu TMP3, TAB:CARG2->marked - | lw TMP0, TAB:CARG2->asize - | lw TMP1, TAB:CARG2->array - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. addu RA, BASE, RA - |2: - | sltu AT, CARG3, TMP0 - | sll TMP2, CARG3, 3 - | beqz AT, ->vmeta_tsetr // In array part? - |. addu CRET1, TMP1, TMP2 - |->BC_TSETR_Z: - | lw SFARG1HI, HI(RA) - | lw SFARG1LO, LO(RA) - | ins_next1 - | sw SFARG1HI, HI(CRET1) - | sw SFARG1LO, LO(CRET1) - | ins_next2 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, CRET1, <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RD = num_const*8 (start index) - | addu RA, BASE, RA - |1: - | addu TMP3, KBASE, RD - | lw TAB:CARG2, -8+LO(RA) // Guaranteed to be a table. - | addiu TMP0, MULTRES, -8 - | lw TMP3, LO(TMP3) // Integer constant is in lo-word. - | beqz TMP0, >4 // Nothing to copy? - |. srl CARG3, TMP0, 3 - | addu CARG3, CARG3, TMP3 - | lw TMP2, TAB:CARG2->asize - | sll TMP1, TMP3, 3 - | lbu TMP3, TAB:CARG2->marked - | lw CARG1, TAB:CARG2->array - | sltu AT, TMP2, CARG3 - | bnez AT, >5 - |. addu TMP2, RA, TMP0 - | addu TMP1, TMP1, CARG1 - | andi TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |3: // Copy result slots to table. - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | addiu RA, RA, 8 - | sltu AT, RA, TMP2 - | sw SFRETHI, HI(TMP1) - | sw SFRETLO, LO(TMP1) - | bnez AT, <3 - |. addiu TMP1, TMP1, 8 - | bnez TMP0, >7 - |. nop - |4: - | ins_next - | - |5: // Need to resize array part. - | load_got lj_tab_reasize - | sw BASE, L->base - | sw PC, SAVE_PC - | move BASE, RD - | call_intern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - |. move CARG1, L - | // Must not reallocate the stack. - | move RD, BASE - | b <1 - |. lw BASE, L->base // Reload BASE for lack of a saved register. - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP0, <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8 - | decode_RDtoRC8 NARGS8:RC, RD - | b ->BC_CALL_Z - |. addu NARGS8:RC, NARGS8:RC, MULTRES - break; - case BC_CALL: - | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8 - | decode_RDtoRC8 NARGS8:RC, RD - |->BC_CALL_Z: - | move TMP2, BASE - | addu BASE, BASE, RA - | li AT, LJ_TFUNC - | lw TMP0, HI(BASE) - | lw LFUNC:RB, LO(BASE) - | addiu BASE, BASE, 8 - | bne TMP0, AT, ->vmeta_call - |. addiu NARGS8:RC, NARGS8:RC, -8 - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs*8 - | addu NARGS8:RD, NARGS8:RD, MULTRES // BC_CALLT gets RC from RD. - | // Fall through. Assumes BC_CALLT follows. - break; - case BC_CALLT: - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - | addu RA, BASE, RA - | li AT, LJ_TFUNC - | lw TMP0, HI(RA) - | lw LFUNC:RB, LO(RA) - | move NARGS8:RC, RD - | lw TMP1, FRAME_PC(BASE) - | addiu RA, RA, 8 - | bne TMP0, AT, ->vmeta_callt - |. addiu NARGS8:RC, NARGS8:RC, -8 - |->BC_CALLT_Z: - | andi TMP0, TMP1, FRAME_TYPE // Caveat: preserve TMP0 until the 'or'. - | lbu TMP3, LFUNC:RB->ffid - | bnez TMP0, >7 - |. xori TMP2, TMP1, FRAME_VARG - |1: - | sw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC. - | sltiu AT, TMP3, 2 // (> FF_C) Calling a fast function? - | move TMP2, BASE - | beqz NARGS8:RC, >3 - |. move TMP3, NARGS8:RC - |2: - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | addiu RA, RA, 8 - | addiu TMP3, TMP3, -8 - | sw SFRETHI, HI(TMP2) - | sw SFRETLO, LO(TMP2) - | bnez TMP3, <2 - |. addiu TMP2, TMP2, 8 - |3: - | or TMP0, TMP0, AT - | beqz TMP0, >5 - |. nop - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | lw INS, -4(TMP1) - | decode_RA8a RA, INS - | decode_RA8b RA - | subu TMP1, BASE, RA - | lw LFUNC:TMP1, -8+FRAME_FUNC(TMP1) - | lw TMP1, LFUNC:TMP1->pc - | b <4 - |. lw KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE. - | - |7: // Tailcall from a vararg function. - | andi AT, TMP2, FRAME_TYPEP - | bnez AT, <1 // Vararg frame below? - |. subu TMP2, BASE, TMP2 // Relocate BASE down. - | move BASE, TMP2 - | lw TMP1, FRAME_PC(TMP2) - | b <1 - |. andi TMP0, TMP1, FRAME_TYPE - break; - - case BC_ITERC: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8)) - | move TMP2, BASE - | addu BASE, BASE, RA - | li AT, LJ_TFUNC - | lw TMP1, -24+HI(BASE) - | lw LFUNC:RB, -24+LO(BASE) - | lw SFARG1HI, -16+HI(BASE) - | lw SFARG1LO, -16+LO(BASE) - | lw SFARG2HI, -8+HI(BASE) - | lw SFARG2LO, -8+LO(BASE) - | sw TMP1, HI(BASE) // Copy callable. - | sw LFUNC:RB, LO(BASE) - | sw SFARG1HI, 8+HI(BASE) // Copy state. - | sw SFARG1LO, 8+LO(BASE) - | sw SFARG2HI, 16+HI(BASE) // Copy control var. - | sw SFARG2LO, 16+LO(BASE) - | addiu BASE, BASE, 8 - | bne TMP1, AT, ->vmeta_call - |. li NARGS8:RC, 16 // Iterators get 2 arguments. - | ins_call - break; - - case BC_ITERN: - |.if JIT and ENDIAN_LE - | hotloop - |.endif - |->vm_IITERN: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8) - | addu RA, BASE, RA - | lw TAB:RB, -16+LO(RA) - | lw RC, -8+LO(RA) // Get index from control var. - | lw TMP0, TAB:RB->asize - | lw TMP1, TAB:RB->array - | addiu PC, PC, 4 - |1: // Traverse array part. - | sltu AT, RC, TMP0 - | beqz AT, >5 // Index points after array part? - |. sll TMP3, RC, 3 - | addu TMP3, TMP1, TMP3 - | lw SFARG1HI, HI(TMP3) - | lw SFARG1LO, LO(TMP3) - | lhu RD, -4+OFS_RD(PC) - | sw TISNUM, HI(RA) - | sw RC, LO(RA) - | beq SFARG1HI, TISNIL, <1 // Skip holes in array part. - |. addiu RC, RC, 1 - | sw SFARG1HI, 8+HI(RA) - | sw SFARG1LO, 8+LO(RA) - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | decode_RD4b RD - | addu RD, RD, TMP3 - | sw RC, -8+LO(RA) // Update control var. - | addu PC, PC, RD - |3: - | ins_next - | - |5: // Traverse hash part. - | lw TMP1, TAB:RB->hmask - | subu RC, RC, TMP0 - | lw TMP2, TAB:RB->node - |6: - | sltu AT, TMP1, RC // End of iteration? Branch to ITERL+1. - | bnez AT, <3 - |. sll TMP3, RC, 5 - | sll RB, RC, 3 - | subu TMP3, TMP3, RB - | addu NODE:TMP3, TMP3, TMP2 - | lw SFARG1HI, NODE:TMP3->val.u32.hi - | lw SFARG1LO, NODE:TMP3->val.u32.lo - | lhu RD, -4+OFS_RD(PC) - | beq SFARG1HI, TISNIL, <6 // Skip holes in hash part. - |. addiu RC, RC, 1 - | lw SFARG2HI, NODE:TMP3->key.u32.hi - | lw SFARG2LO, NODE:TMP3->key.u32.lo - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sw SFARG1HI, 8+HI(RA) - | sw SFARG1LO, 8+LO(RA) - | addu RC, RC, TMP0 - | decode_RD4b RD - | addu RD, RD, TMP3 - | sw SFARG2HI, HI(RA) - | sw SFARG2LO, LO(RA) - | addu PC, PC, RD - | b <3 - |. sw RC, -8+LO(RA) // Update control var. - break; - - case BC_ISNEXT: - | // RA = base*8, RD = target (points to ITERN) - | addu RA, BASE, RA - | srl TMP0, RD, 1 - | lw CARG1, -24+HI(RA) - | lw CFUNC:CARG2, -24+LO(RA) - | addu TMP0, PC, TMP0 - | lw CARG3, -16+HI(RA) - | lw CARG4, -8+HI(RA) - | li AT, LJ_TFUNC - | bne CARG1, AT, >5 - |. lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535) - | lbu CARG2, CFUNC:CARG2->ffid - | addiu CARG3, CARG3, -LJ_TTAB - | addiu CARG4, CARG4, -LJ_TNIL - | or CARG3, CARG3, CARG4 - | addiu CARG2, CARG2, -FF_next_N - | or CARG2, CARG2, CARG3 - | bnez CARG2, >5 - |. lui TMP1, (LJ_KEYINDEX >> 16) - | addu PC, TMP0, TMP2 - | ori TMP1, TMP1, (LJ_KEYINDEX & 0xffff) - | sw r0, -8+LO(RA) // Initialize control var. - | sw TMP1, -8+HI(RA) - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | li TMP3, BC_JMP - | li TMP1, BC_ITERC - | sb TMP3, -4+OFS_OP(PC) - | addu PC, TMP0, TMP2 - |.if JIT - | lb TMP0, OFS_OP(PC) - | li AT, BC_ITERN - | bne TMP0, AT, >6 - |. lhu TMP2, OFS_RD(PC) - |.endif - | b <1 - |. sb TMP1, OFS_OP(PC) - |.if JIT - |6: // Unpatch JLOOP. - | lw TMP0, DISPATCH_J(trace)(DISPATCH) - | sll TMP2, TMP2, 2 - | addu TMP0, TMP0, TMP2 - | lw TRACE:TMP2, 0(TMP0) - | lw TMP0, TRACE:TMP2->startins - | li AT, -256 - | and TMP0, TMP0, AT - | or TMP0, TMP0, TMP1 - | b <1 - |. sw TMP0, 0(PC) - |.endif - break; - - case BC_VARG: - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | lw TMP0, FRAME_PC(BASE) - | decode_RDtoRC8 RC, RD - | decode_RB8a RB, INS - | addu RC, BASE, RC - | decode_RB8b RB - | addu RA, BASE, RA - | addiu RC, RC, FRAME_VARG - | addu TMP2, RA, RB - | addiu TMP3, BASE, -8 // TMP3 = vtop - | subu RC, RC, TMP0 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | beqz RB, >5 // Copy all varargs? - |. subu TMP1, TMP3, RC - | addiu TMP2, TMP2, -16 - |1: // Copy vararg slots to destination slots. - | lw CARG1, HI(RC) - | sltu AT, RC, TMP3 - | lw CARG2, LO(RC) - | addiu RC, RC, 8 - | movz CARG1, TISNIL, AT - | sw CARG1, HI(RA) - | sw CARG2, LO(RA) - | sltu AT, RA, TMP2 - | bnez AT, <1 - |. addiu RA, RA, 8 - |3: - | ins_next - | - |5: // Copy all varargs. - | lw TMP0, L->maxstack - | blez TMP1, <3 // No vararg slots? - |. li MULTRES, 8 // MULTRES = (0+1)*8 - | addu TMP2, RA, TMP1 - | sltu AT, TMP0, TMP2 - | bnez AT, >7 - |. addiu MULTRES, TMP1, 8 - |6: - | lw SFRETHI, HI(RC) - | lw SFRETLO, LO(RC) - | addiu RC, RC, 8 - | sw SFRETHI, HI(RA) - | sw SFRETLO, LO(RA) - | sltu AT, RC, TMP3 - | bnez AT, <6 // More vararg slots? - |. addiu RA, RA, 8 - | b <3 - |. nop - | - |7: // Grow stack for varargs. - | load_got lj_state_growstack - | sw RA, L->top - | subu RA, RA, BASE - | sw BASE, L->base - | subu BASE, RC, BASE // Need delta, because BASE may change. - | sw PC, SAVE_PC - | srl CARG2, TMP1, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | move RC, BASE - | lw BASE, L->base - | addu RA, BASE, RA - | addu RC, BASE, RC - | b <6 - |. addiu TMP3, BASE, -8 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RD = extra_nresults*8 - | addu RD, RD, MULTRES // MULTRES >= 8, so RD >= 8. - | // Fall through. Assumes BC_RET follows. - break; - - case BC_RET: - | // RA = results*8, RD = (nresults+1)*8 - | lw PC, FRAME_PC(BASE) - | addu RA, BASE, RA - | move MULTRES, RD - |1: - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->BC_RETV_Z - |. xori TMP1, PC, FRAME_VARG - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return - | lw INS, -4(PC) - | addiu TMP2, BASE, -8 - | addiu RC, RD, -8 - | decode_RA8a TMP0, INS - | decode_RB8a RB, INS - | decode_RA8b TMP0 - | decode_RB8b RB - | addu TMP3, TMP2, RB - | beqz RC, >3 - |. subu BASE, TMP2, TMP0 - |2: - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - | addiu RA, RA, 8 - | addiu RC, RC, -8 - | sw SFRETHI, HI(TMP2) - | sw SFRETLO, LO(TMP2) - | bnez RC, <2 - |. addiu TMP2, TMP2, 8 - |3: - | addiu TMP3, TMP3, -8 - |5: - | sltu AT, TMP2, TMP3 - | bnez AT, >6 - |. lw LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lw TMP1, LFUNC:TMP1->pc - | lw KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | sw TISNIL, HI(TMP2) - | b <5 - |. addiu TMP2, TMP2, 8 - | - |->BC_RETV_Z: // Non-standard return case. - | andi TMP2, TMP1, FRAME_TYPEP - | bnez TMP2, ->vm_return - |. nop - | // Return from vararg function: relocate BASE down. - | subu BASE, BASE, TMP1 - | b <1 - |. lw PC, FRAME_PC(BASE) - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RD = (nresults+1)*8 - | lw PC, FRAME_PC(BASE) - | addu RA, BASE, RA - | move MULTRES, RD - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->BC_RETV_Z - |. xori TMP1, PC, FRAME_VARG - | - | lw INS, -4(PC) - | addiu TMP2, BASE, -8 - if (op == BC_RET1) { - | lw SFRETHI, HI(RA) - | lw SFRETLO, LO(RA) - } - | decode_RB8a RB, INS - | decode_RA8a RA, INS - | decode_RB8b RB - | decode_RA8b RA - if (op == BC_RET1) { - | sw SFRETHI, HI(TMP2) - | sw SFRETLO, LO(TMP2) - } - | subu BASE, TMP2, RA - |5: - | sltu AT, RD, RB - | bnez AT, >6 - |. lw LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lw TMP1, LFUNC:TMP1->pc - | lw KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | addiu TMP2, TMP2, 8 - | addiu RD, RD, 8 - | b <5 - if (op == BC_RET1) { - |. sw TISNIL, HI(TMP2) - } else { - |. sw TISNIL, -8+HI(TMP2) - } - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RD = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | addu RA, BASE, RA - | lw SFARG1HI, FORL_IDX*8+HI(RA) - | lw SFARG1LO, FORL_IDX*8+LO(RA) - if (op != BC_JFORL) { - | srl RD, RD, 1 - | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, RD, TMP2 - } - if (!vk) { - | lw SFARG2HI, FORL_STOP*8+HI(RA) - | lw SFARG2LO, FORL_STOP*8+LO(RA) - | bne SFARG1HI, TISNUM, >5 - |. lw SFRETHI, FORL_STEP*8+HI(RA) - | xor AT, SFARG2HI, TISNUM - | lw SFRETLO, FORL_STEP*8+LO(RA) - | xor TMP0, SFRETHI, TISNUM - | or AT, AT, TMP0 - | bnez AT, ->vmeta_for - |. slt AT, SFRETLO, r0 - | slt CRET1, SFARG2LO, SFARG1LO - | slt TMP1, SFARG1LO, SFARG2LO - | movn CRET1, TMP1, AT - } else { - | bne SFARG1HI, TISNUM, >5 - |. lw SFARG2LO, FORL_STEP*8+LO(RA) - | lw SFRETLO, FORL_STOP*8+LO(RA) - | move TMP3, SFARG1LO - | addu SFARG1LO, SFARG1LO, SFARG2LO - | xor TMP0, SFARG1LO, TMP3 - | xor TMP1, SFARG1LO, SFARG2LO - | and TMP0, TMP0, TMP1 - | slt TMP1, SFARG1LO, SFRETLO - | slt CRET1, SFRETLO, SFARG1LO - | slt AT, SFARG2LO, r0 - | slt TMP0, TMP0, r0 // ((y^a) & (y^b)) < 0: overflow. - | movn CRET1, TMP1, AT - | or CRET1, CRET1, TMP0 - } - |1: - if (op == BC_FORI) { - | movz TMP2, r0, CRET1 - | addu PC, PC, TMP2 - } else if (op == BC_JFORI) { - | addu PC, PC, TMP2 - | lhu RD, -4+OFS_RD(PC) - } else if (op == BC_IFORL) { - | movn TMP2, r0, CRET1 - | addu PC, PC, TMP2 - } - if (vk) { - | sw SFARG1HI, FORL_IDX*8+HI(RA) - | sw SFARG1LO, FORL_IDX*8+LO(RA) - } - | ins_next1 - | sw SFARG1HI, FORL_EXT*8+HI(RA) - | sw SFARG1LO, FORL_EXT*8+LO(RA) - |2: - if (op == BC_JFORI) { - | beqz CRET1, =>BC_JLOOP - |. decode_RD8b RD - } else if (op == BC_JFORL) { - | beqz CRET1, =>BC_JLOOP - } - | ins_next2 - | - |5: // FP loop. - |.if FPU - if (!vk) { - | ldc1 f0, FORL_IDX*8(RA) - | ldc1 f2, FORL_STOP*8(RA) - | sltiu TMP0, SFARG1HI, LJ_TISNUM - | sltiu TMP1, SFARG2HI, LJ_TISNUM - | sltiu AT, SFRETHI, LJ_TISNUM - | and TMP0, TMP0, TMP1 - | and AT, AT, TMP0 - | beqz AT, ->vmeta_for - |. slt TMP3, SFRETHI, r0 - | c.ole.d 0, f0, f2 - | c.ole.d 1, f2, f0 - | li CRET1, 1 - | movt CRET1, r0, 0 - | movt AT, r0, 1 - | b <1 - |. movn CRET1, AT, TMP3 - } else { - | ldc1 f0, FORL_IDX*8(RA) - | ldc1 f4, FORL_STEP*8(RA) - | ldc1 f2, FORL_STOP*8(RA) - | lw SFARG2HI, FORL_STEP*8+HI(RA) - | add.d f0, f0, f4 - | c.ole.d 0, f0, f2 - | c.ole.d 1, f2, f0 - | slt TMP3, SFARG2HI, r0 - | li CRET1, 1 - | li AT, 1 - | movt CRET1, r0, 0 - | movt AT, r0, 1 - | movn CRET1, AT, TMP3 - if (op == BC_IFORL) { - | movn TMP2, r0, CRET1 - | addu PC, PC, TMP2 - } - | sdc1 f0, FORL_IDX*8(RA) - | ins_next1 - | b <2 - |. sdc1 f0, FORL_EXT*8(RA) - } - |.else - if (!vk) { - | sltiu TMP0, SFARG1HI, LJ_TISNUM - | sltiu TMP1, SFARG2HI, LJ_TISNUM - | sltiu AT, SFRETHI, LJ_TISNUM - | and TMP0, TMP0, TMP1 - | and AT, AT, TMP0 - | beqz AT, ->vmeta_for - |. nop - | bal ->vm_sfcmpolex - |. move TMP3, SFRETHI - | b <1 - |. nop - } else { - | lw SFARG2HI, FORL_STEP*8+HI(RA) - | load_got __adddf3 - | call_extern - |. sw TMP2, ARG5 - | lw SFARG2HI, FORL_STOP*8+HI(RA) - | lw SFARG2LO, FORL_STOP*8+LO(RA) - | move SFARG1HI, SFRETHI - | move SFARG1LO, SFRETLO - | bal ->vm_sfcmpolex - |. lw TMP3, FORL_STEP*8+HI(RA) - if ( op == BC_JFORL ) { - | lhu RD, -4+OFS_RD(PC) - | lw TMP2, ARG5 - | b <1 - |. decode_RD8b RD - } else { - | b <1 - |. lw TMP2, ARG5 - } - } - |.endif - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RD = target - | addu RA, BASE, RA - | lw TMP1, HI(RA) - | beq TMP1, TISNIL, >1 // Stop if iterator returned nil. - |. lw TMP2, LO(RA) - if (op == BC_JITERL) { - | sw TMP1, -8+HI(RA) - | b =>BC_JLOOP - |. sw TMP2, -8+LO(RA) - } else { - | branch_RD // Otherwise save control var + branch. - | sw TMP1, -8+HI(RA) - | sw TMP2, -8+LO(RA) - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base*8 (ignored), RD = traceno*8 - | lw TMP1, DISPATCH_J(trace)(DISPATCH) - | srl RD, RD, 1 - | li AT, 0 - | addu TMP1, TMP1, RD - | // Traces on MIPS don't store the trace number, so use 0. - | sw AT, DISPATCH_GL(vmstate)(DISPATCH) - | lw TRACE:TMP2, 0(TMP1) - | sw BASE, DISPATCH_GL(jit_base)(DISPATCH) - | lw TMP2, TRACE:TMP2->mcode - | sw L, DISPATCH_GL(tmpbuf.L)(DISPATCH) - | jr TMP2 - |. addiu JGL, DISPATCH, GG_DISP2G+32768 - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RD = target - | branch_RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lw TMP2, L->maxstack - | lbu TMP1, -4+PC2PROTO(numparams)(PC) - | lw KBASE, -4+PC2PROTO(k)(PC) - | sltu AT, TMP2, RA - | bnez AT, ->vm_growstack_l - |. sll TMP1, TMP1, 3 - if (op != BC_JFUNCF) { - | ins_next1 - } - |2: - | sltu AT, NARGS8:RC, TMP1 // Check for missing parameters. - | bnez AT, >3 - |. addu AT, BASE, NARGS8:RC - if (op == BC_JFUNCF) { - | decode_RD8a RD, INS - | b =>BC_JLOOP - |. decode_RD8b RD - } else { - | ins_next2 - } - | - |3: // Clear missing parameters. - | sw TISNIL, HI(AT) - | b <2 - |. addiu NARGS8:RC, NARGS8:RC, 8 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | addu TMP1, BASE, RC - | lw TMP2, L->maxstack - | addu TMP0, RA, RC - | sw LFUNC:RB, LO(TMP1) // Store copy of LFUNC. - | addiu TMP3, RC, 8+FRAME_VARG - | sltu AT, TMP0, TMP2 - | lw KBASE, -4+PC2PROTO(k)(PC) - | beqz AT, ->vm_growstack_l - |. sw TMP3, HI(TMP1) // Store delta + FRAME_VARG. - | lbu TMP2, -4+PC2PROTO(numparams)(PC) - | move RA, BASE - | move RC, TMP1 - | ins_next1 - | beqz TMP2, >3 - |. addiu BASE, TMP1, 8 - |1: - | lw TMP0, HI(RA) - | lw TMP3, LO(RA) - | sltu AT, RA, RC // Less args than parameters? - | move CARG1, TMP0 - | movz TMP0, TISNIL, AT // Clear missing parameters. - | movn CARG1, TISNIL, AT // Clear old fixarg slot (help the GC). - | sw TMP3, 8+LO(TMP1) - | addiu TMP2, TMP2, -1 - | sw TMP0, 8+HI(TMP1) - | addiu TMP1, TMP1, 8 - | sw CARG1, HI(RA) - | bnez TMP2, <1 - |. addiu RA, RA, 8 - |3: - | ins_next2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | lw CFUNCADDR, CFUNC:RB->f - } else { - | lw CFUNCADDR, DISPATCH_GL(wrapf)(DISPATCH) - } - | addu TMP1, RA, NARGS8:RC - | lw TMP2, L->maxstack - | addu RC, BASE, NARGS8:RC - | sw BASE, L->base - | sltu AT, TMP2, TMP1 - | sw RC, L->top - | li_vmstate C - if (op == BC_FUNCCW) { - | lw CARG2, CFUNC:RB->f - } - | bnez AT, ->vm_growstack_c // Need to grow stack. - |. move CARG1, L - | jalr CFUNCADDR // (lua_State *L [, lua_CFunction f]) - |. st_vmstate - | // Returns nresults. - | lw BASE, L->base - | sll RD, CRET1, 3 - | lw TMP1, L->top - | li_vmstate INTERP - | lw PC, FRAME_PC(BASE) // Fetch PC of caller. - | subu RA, TMP1, RD // RA = L->top - nresults*8 - | sw L, DISPATCH_GL(cur_L)(DISPATCH) - | b ->vm_returnc - |. st_vmstate - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.4byte .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.4byte 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.4byte .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.4byte .Lframe0\n" - "\t.4byte .Lbegin\n" - "\t.4byte %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x9f\n\t.sleb128 1\n" - "\t.byte 0x9e\n\t.sleb128 2\n", - fcofs, CFRAME_SIZE); - for (i = 23; i >= 16; i--) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 26-i); -#if !LJ_SOFTFP - for (i = 30; i >= 20; i -= 2) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 42-i); -#endif - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.4byte .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.4byte .Lframe0\n" - "\t.4byte lj_vm_ffi_call\n" - "\t.4byte %d\n" - "\t.byte 0x9f\n\t.uleb128 1\n" - "\t.byte 0x90\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0x10\n" - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND - fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n"); - fprintf(ctx->fp, - "\t.globl lj_err_unwind_dwarf\n" - ".Lframe1:\n" - "\t.4byte .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.4byte 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0\n" - "\t.4byte lj_err_unwind_dwarf\n" - "\t.byte 0\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.4byte .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.4byte .LASFDE2-.Lframe1\n" - "\t.4byte .Lbegin\n" - "\t.4byte %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x9f\n\t.sleb128 1\n" - "\t.byte 0x9e\n\t.sleb128 2\n", - fcofs, CFRAME_SIZE); - for (i = 23; i >= 16; i--) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 26-i); -#if !LJ_SOFTFP - for (i = 30; i >= 20; i -= 2) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 42-i); -#endif - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE2:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.4byte .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.4byte 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.4byte .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.4byte .LASFDE3-.Lframe2\n" - "\t.4byte lj_vm_ffi_call\n" - "\t.4byte %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x9f\n\t.uleb128 1\n" - "\t.byte 0x90\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0x10\n" - "\t.align 2\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif -#endif - break; - default: - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips64.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips64.dasc deleted file mode 100644 index 651bc42..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_mips64.dasc +++ /dev/null @@ -1,5538 +0,0 @@ -|// Low-level VM code for MIPS64 CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -|// -|// Contributed by Djordje Kovacevic and Stefan Pejic from RT-RK.com. -|// Sponsored by Cisco Systems, Inc. -| -|.arch mips64 -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -|// Don't use: r0 = 0, r26/r27 = reserved, r28 = gp, r29 = sp, r31 = ra -| -|.macro .FPU, a, b -|.if FPU -| a, b -|.endif -|.endmacro -| -|// The following must be C callee-save (but BASE is often refetched). -|.define BASE, r16 // Base of current Lua stack frame. -|.define KBASE, r17 // Constants of current Lua function. -|.define PC, r18 // Next PC. -|.define DISPATCH, r19 // Opcode dispatch table. -|.define LREG, r20 // Register holding lua_State (also in SAVE_L). -|.define MULTRES, r21 // Size of multi-result: (nresults+1)*8. -| -|.define JGL, r30 // On-trace: global_State + 32768. -| -|// Constants for type-comparisons, stores and conversions. C callee-save. -|.define TISNIL, r30 -|.define TISNUM, r22 -|.if FPU -|.define TOBIT, f30 // 2^52 + 2^51. -|.endif -| -|// The following temporaries are not saved across C calls, except for RA. -|.define RA, r23 // Callee-save. -|.define RB, r8 -|.define RC, r9 -|.define RD, r10 -|.define INS, r11 -| -|.define AT, r1 // Assembler temporary. -|.define TMP0, r12 -|.define TMP1, r13 -|.define TMP2, r14 -|.define TMP3, r15 -| -|// MIPS n64 calling convention. -|.define CFUNCADDR, r25 -|.define CARG1, r4 -|.define CARG2, r5 -|.define CARG3, r6 -|.define CARG4, r7 -|.define CARG5, r8 -|.define CARG6, r9 -|.define CARG7, r10 -|.define CARG8, r11 -| -|.define CRET1, r2 -|.define CRET2, r3 -| -|.if FPU -|.define FARG1, f12 -|.define FARG2, f13 -|.define FARG3, f14 -|.define FARG4, f15 -|.define FARG5, f16 -|.define FARG6, f17 -|.define FARG7, f18 -|.define FARG8, f19 -| -|.define FRET1, f0 -|.define FRET2, f2 -| -|.define FTMP0, f20 -|.define FTMP1, f21 -|.define FTMP2, f22 -|.endif -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.if FPU // MIPS64 hard-float. -| -|.define CFRAME_SPACE, 192 // Delta for sp. -| -|//----- 16 byte aligned, <-- sp entering interpreter -|.define SAVE_ERRF, 188(sp) // 32 bit values. -|.define SAVE_NRES, 184(sp) -|.define SAVE_CFRAME, 176(sp) // 64 bit values. -|.define SAVE_L, 168(sp) -|.define SAVE_PC, 160(sp) -|//----- 16 byte aligned -|.define SAVE_GPR_, 80 // .. 80+10*8: 64 bit GPR saves. -|.define SAVE_FPR_, 16 // .. 16+8*8: 64 bit FPR saves. -| -|.else // MIPS64 soft-float -| -|.define CFRAME_SPACE, 128 // Delta for sp. -| -|//----- 16 byte aligned, <-- sp entering interpreter -|.define SAVE_ERRF, 124(sp) // 32 bit values. -|.define SAVE_NRES, 120(sp) -|.define SAVE_CFRAME, 112(sp) // 64 bit values. -|.define SAVE_L, 104(sp) -|.define SAVE_PC, 96(sp) -|//----- 16 byte aligned -|.define SAVE_GPR_, 16 // .. 16+10*8: 64 bit GPR saves. -| -|.endif -| -|.define TMPX, 8(sp) // Unused by interpreter, temp for JIT code. -|.define TMPD, 0(sp) -|//----- 16 byte aligned -| -|.define TMPD_OFS, 0 -| -|.define SAVE_MULTRES, TMPD -| -|//----------------------------------------------------------------------- -| -|.macro saveregs -| daddiu sp, sp, -CFRAME_SPACE -| sd ra, SAVE_GPR_+9*8(sp) -| sd r30, SAVE_GPR_+8*8(sp) -| .FPU sdc1 f31, SAVE_FPR_+7*8(sp) -| sd r23, SAVE_GPR_+7*8(sp) -| .FPU sdc1 f30, SAVE_FPR_+6*8(sp) -| sd r22, SAVE_GPR_+6*8(sp) -| .FPU sdc1 f29, SAVE_FPR_+5*8(sp) -| sd r21, SAVE_GPR_+5*8(sp) -| .FPU sdc1 f28, SAVE_FPR_+4*8(sp) -| sd r20, SAVE_GPR_+4*8(sp) -| .FPU sdc1 f27, SAVE_FPR_+3*8(sp) -| sd r19, SAVE_GPR_+3*8(sp) -| .FPU sdc1 f26, SAVE_FPR_+2*8(sp) -| sd r18, SAVE_GPR_+2*8(sp) -| .FPU sdc1 f25, SAVE_FPR_+1*8(sp) -| sd r17, SAVE_GPR_+1*8(sp) -| .FPU sdc1 f24, SAVE_FPR_+0*8(sp) -| sd r16, SAVE_GPR_+0*8(sp) -|.endmacro -| -|.macro restoreregs_ret -| ld ra, SAVE_GPR_+9*8(sp) -| ld r30, SAVE_GPR_+8*8(sp) -| ld r23, SAVE_GPR_+7*8(sp) -| .FPU ldc1 f31, SAVE_FPR_+7*8(sp) -| ld r22, SAVE_GPR_+6*8(sp) -| .FPU ldc1 f30, SAVE_FPR_+6*8(sp) -| ld r21, SAVE_GPR_+5*8(sp) -| .FPU ldc1 f29, SAVE_FPR_+5*8(sp) -| ld r20, SAVE_GPR_+4*8(sp) -| .FPU ldc1 f28, SAVE_FPR_+4*8(sp) -| ld r19, SAVE_GPR_+3*8(sp) -| .FPU ldc1 f27, SAVE_FPR_+3*8(sp) -| ld r18, SAVE_GPR_+2*8(sp) -| .FPU ldc1 f26, SAVE_FPR_+2*8(sp) -| ld r17, SAVE_GPR_+1*8(sp) -| .FPU ldc1 f25, SAVE_FPR_+1*8(sp) -| ld r16, SAVE_GPR_+0*8(sp) -| .FPU ldc1 f24, SAVE_FPR_+0*8(sp) -| jr ra -| daddiu sp, sp, CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; .long 0xec1cf0f0; .endmacro -| -|// Macros to mark delay slots. -|.macro ., a; a; .endmacro -|.macro ., a,b; a,b; .endmacro -|.macro ., a,b,c; a,b,c; .endmacro -|.macro ., a,b,c,d; a,b,c,d; .endmacro -| -|.define FRAME_PC, -8 -|.define FRAME_FUNC, -16 -| -|//----------------------------------------------------------------------- -| -|// Endian-specific defines. -|.if ENDIAN_LE -|.define HI, 4 -|.define LO, 0 -|.define OFS_RD, 2 -|.define OFS_RA, 1 -|.define OFS_OP, 0 -|.else -|.define HI, 0 -|.define LO, 4 -|.define OFS_RD, 0 -|.define OFS_RA, 2 -|.define OFS_OP, 3 -|.endif -| -|// Instruction decode. -|.macro decode_OP1, dst, ins; andi dst, ins, 0xff; .endmacro -|.macro decode_OP8a, dst, ins; andi dst, ins, 0xff; .endmacro -|.macro decode_OP8b, dst; sll dst, dst, 3; .endmacro -|.macro decode_RC8a, dst, ins; srl dst, ins, 13; .endmacro -|.macro decode_RC8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RD4b, dst; sll dst, dst, 2; .endmacro -|.macro decode_RA8a, dst, ins; srl dst, ins, 5; .endmacro -|.macro decode_RA8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RB8a, dst, ins; srl dst, ins, 21; .endmacro -|.macro decode_RB8b, dst; andi dst, dst, 0x7f8; .endmacro -|.macro decode_RD8a, dst, ins; srl dst, ins, 16; .endmacro -|.macro decode_RD8b, dst; sll dst, dst, 3; .endmacro -|.macro decode_RDtoRC8, dst, src; andi dst, src, 0x7f8; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| lw INS, 0(PC) -| daddiu PC, PC, 4 -|.endmacro -|// Instruction decode+dispatch. -|.macro ins_NEXT2 -| decode_OP8a TMP1, INS -| decode_OP8b TMP1 -| daddu TMP0, DISPATCH, TMP1 -| decode_RD8a RD, INS -| ld AT, 0(TMP0) -| decode_RA8a RA, INS -| decode_RD8b RD -| jr AT -| decode_RA8b RA -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| ld PC, LFUNC:RB->pc -| lw INS, 0(PC) -| daddiu PC, PC, 4 -| decode_OP8a TMP1, INS -| decode_RA8a RA, INS -| decode_OP8b TMP1 -| decode_RA8b RA -| daddu TMP0, DISPATCH, TMP1 -| ld TMP0, 0(TMP0) -| jr TMP0 -| daddu RA, RA, BASE -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| sd PC, FRAME_PC(BASE) -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|.macro branch_RD -| srl TMP0, RD, 1 -| lui AT, (-(BCBIAS_J*4 >> 16) & 65535) -| addu TMP0, TMP0, AT -| daddu PC, PC, TMP0 -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -#define GG_DISP2GOT (GG_OFS(got) - GG_OFS(dispatch)) -#define DISPATCH_GOT(name) (GG_DISP2GOT + sizeof(void*)*LJ_GOT_##name) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro load_got, func -| ld CFUNCADDR, DISPATCH_GOT(func)(DISPATCH) -|.endmacro -|// Much faster. Sadly, there's no easy way to force the required code layout. -|// .macro call_intern, func; bal extern func; .endmacro -|.macro call_intern, func; jalr CFUNCADDR; .endmacro -|.macro call_extern; jalr CFUNCADDR; .endmacro -|.macro jmp_extern; jr CFUNCADDR; .endmacro -| -|.macro hotcheck, delta, target -| dsrl TMP1, PC, 1 -| andi TMP1, TMP1, 126 -| daddu TMP1, TMP1, DISPATCH -| lhu TMP2, GG_DISP2HOT(TMP1) -| addiu TMP2, TMP2, -delta -| bltz TMP2, target -|. sh TMP2, GG_DISP2HOT(TMP1) -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP, ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL, ->vm_hotcall -|.endmacro -| -|// Set current VM state. Uses TMP0. -|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro -|.macro st_vmstate; sw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp, target -| ld tmp, DISPATCH_GL(gc.grayagain)(DISPATCH) -| andi mark, mark, ~LJ_GC_BLACK & 255 // black2gray(tab) -| sd tab, DISPATCH_GL(gc.grayagain)(DISPATCH) -| sb mark, tab->marked -| b target -|. sd tmp, tab->gclist -|.endmacro -| -|// Clear type tag. Isolate lowest 14+32+1=47 bits of reg. -|.macro cleartp, reg; dextm reg, reg, 0, 14; .endmacro -|.macro cleartp, dst, reg; dextm dst, reg, 0, 14; .endmacro -| -|// Set type tag: Merge 17 type bits into bits [15+32=47, 31+32+1=64) of dst. -|.macro settp, dst, tp; dinsu dst, tp, 15, 31; .endmacro -| -|// Extract (negative) type tag. -|.macro gettp, dst, src; dsra dst, src, 47; .endmacro -| -|// Macros to check the TValue type and extract the GCobj. Branch on failure. -|.macro checktp, reg, tp, target -| gettp AT, reg -| daddiu AT, AT, tp -| bnez AT, target -|. cleartp reg -|.endmacro -|.macro checktp, dst, reg, tp, target -| gettp AT, reg -| daddiu AT, AT, tp -| bnez AT, target -|. cleartp dst, reg -|.endmacro -|.macro checkstr, reg, target; checktp reg, -LJ_TSTR, target; .endmacro -|.macro checktab, reg, target; checktp reg, -LJ_TTAB, target; .endmacro -|.macro checkfunc, reg, target; checktp reg, -LJ_TFUNC, target; .endmacro -|.macro checkint, reg, target // Caveat: has delay slot! -| gettp AT, reg -| bne AT, TISNUM, target -|.endmacro -|.macro checknum, reg, target // Caveat: has delay slot! -| gettp AT, reg -| sltiu AT, AT, LJ_TISNUM -| beqz AT, target -|.endmacro -| -|.macro mov_false, reg -| lu reg, 0x8000 -| dsll reg, reg, 32 -| not reg, reg -|.endmacro -|.macro mov_true, reg -| li reg, 0x0001 -| dsll reg, reg, 48 -| not reg, reg -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: TMP2 = previous base. - | andi AT, PC, FRAME_P - | beqz AT, ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - |. mov_true TMP1 - | ld PC, FRAME_PC(TMP2) // Fetch PC of previous frame. - | move BASE, TMP2 // Restore caller base. - | // Prepending may overwrite the pcall frame, so do it at the end. - | sd TMP1, -8(RA) // Prepend true to results. - | daddiu RA, RA, -8 - | - |->vm_returnc: - | addiu RD, RD, 8 // RD = (nresults+1)*8. - | andi TMP0, PC, FRAME_TYPE - | beqz RD, ->vm_unwind_c_eh - |. li CRET1, LUA_YIELD - | beqz TMP0, ->BC_RET_Z // Handle regular return to Lua. - |. move MULTRES, RD - | - |->vm_return: - | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return - | // TMP0 = PC & FRAME_TYPE - | li TMP2, -8 - | xori AT, TMP0, FRAME_C - | and TMP2, PC, TMP2 - | bnez AT, ->vm_returnp - | dsubu TMP2, BASE, TMP2 // TMP2 = previous base. - | - | addiu TMP1, RD, -8 - | sd TMP2, L->base - | li_vmstate C - | lw TMP2, SAVE_NRES - | daddiu BASE, BASE, -16 - | st_vmstate - | beqz TMP1, >2 - |. sll TMP2, TMP2, 3 - |1: - | addiu TMP1, TMP1, -8 - | ld CRET1, 0(RA) - | daddiu RA, RA, 8 - | sd CRET1, 0(BASE) - | bnez TMP1, <1 - |. daddiu BASE, BASE, 8 - | - |2: - | bne TMP2, RD, >6 - |3: - |. sd BASE, L->top // Store new top. - | - |->vm_leave_cp: - | ld TMP0, SAVE_CFRAME // Restore previous C frame. - | move CRET1, r0 // Ok return status for vm_pcall. - | sd TMP0, L->cframe - | - |->vm_leave_unw: - | restoreregs_ret - | - |6: - | ld TMP1, L->maxstack - | slt AT, TMP2, RD - | bnez AT, >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - |. slt AT, BASE, TMP1 - | beqz AT, >8 - |. nop - | sd TISNIL, 0(BASE) - | addiu RD, RD, 8 - | b <2 - |. daddiu BASE, BASE, 8 - | - |7: // Less results wanted. - | subu TMP0, RD, TMP2 - | dsubu TMP0, BASE, TMP0 // Either keep top or shrink it. - |.if MIPSR6 - | selnez TMP0, TMP0, TMP2 // LUA_MULTRET+1 case? - | seleqz BASE, BASE, TMP2 - | b <3 - |. or BASE, BASE, TMP0 - |.else - | b <3 - |. movn BASE, TMP0, TMP2 // LUA_MULTRET+1 case? - |.endif - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | load_got lj_state_growstack - | move MULTRES, RD - | srl CARG2, TMP2, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | lw TMP2, SAVE_NRES - | ld BASE, L->top // Need the (realloced) L->top in BASE. - | move RD, MULTRES - | b <2 - |. sll TMP2, TMP2, 3 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | move sp, CARG1 - | move CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | ld L, SAVE_L - | li TMP0, ~LJ_VMST_C - | ld GL:TMP1, L->glref - | b ->vm_leave_unw - |. sw TMP0, GL:TMP1->vmstate - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | li AT, -4 - | and sp, CARG1, AT - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | ld L, SAVE_L - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | li TISNIL, LJ_TNIL - | li TISNUM, LJ_TISNUM - | ld BASE, L->base - | ld DISPATCH, L->glref // Setup pointer to dispatch table. - | .FPU mtc1 TMP3, TOBIT - | mov_false TMP1 - | li_vmstate INTERP - | ld PC, FRAME_PC(BASE) // Fetch PC of previous frame. - | .FPU cvt.d.s TOBIT, TOBIT - | daddiu RA, BASE, -8 // Results start at BASE-8. - | daddiu DISPATCH, DISPATCH, GG_G2DISP - | sd TMP1, 0(RA) // Prepend false to error message. - | st_vmstate - | b ->vm_returnc - |. li RD, 16 // 2 results: false + error message. - | - |->vm_unwind_stub: // Jump to exit stub from unwinder. - | jr CARG1 - |. move ra, CARG2 - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | b >2 - |. li CARG2, LUA_MINSTACK - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | daddu RC, BASE, RC - | dsubu RA, RA, BASE - | sd BASE, L->base - | daddiu PC, PC, 4 // Must point after first instruction. - | sd RC, L->top - | srl CARG2, RA, 3 - |2: - | // L->base = new base, L->top = top - | load_got lj_state_growstack - | sd PC, SAVE_PC - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | ld BASE, L->base - | ld RC, L->top - | ld LFUNC:RB, FRAME_FUNC(BASE) - | dsubu RC, RC, BASE - | cleartp LFUNC:RB - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | move L, CARG1 - | ld DISPATCH, L->glref // Setup pointer to dispatch table. - | move BASE, CARG2 - | lbu TMP1, L->status - | sd L, SAVE_L - | li PC, FRAME_CP - | daddiu TMP0, sp, CFRAME_RESUME - | daddiu DISPATCH, DISPATCH, GG_G2DISP - | sw r0, SAVE_NRES - | sw r0, SAVE_ERRF - | sd CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sd r0, SAVE_CFRAME - | beqz TMP1, >3 - |. sd TMP0, L->cframe - | - | // Resume after yield (like a return). - | sd L, DISPATCH_GL(cur_L)(DISPATCH) - | move RA, BASE - | ld BASE, L->base - | ld TMP1, L->top - | ld PC, FRAME_PC(BASE) - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | dsubu RD, TMP1, BASE - | .FPU mtc1 TMP3, TOBIT - | sb r0, L->status - | .FPU cvt.d.s TOBIT, TOBIT - | li_vmstate INTERP - | daddiu RD, RD, 8 - | st_vmstate - | move MULTRES, RD - | andi TMP0, PC, FRAME_TYPE - | li TISNIL, LJ_TNIL - | beqz TMP0, ->BC_RET_Z - |. li TISNUM, LJ_TISNUM - | b ->vm_return - |. nop - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | sw CARG4, SAVE_ERRF - | b >1 - |. li PC, FRAME_CP - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | li PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | ld TMP1, L:CARG1->cframe - | move L, CARG1 - | sw CARG3, SAVE_NRES - | ld DISPATCH, L->glref // Setup pointer to dispatch table. - | sd CARG1, SAVE_L - | move BASE, CARG2 - | daddiu DISPATCH, DISPATCH, GG_G2DISP - | sd CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sd TMP1, SAVE_CFRAME - | sd sp, L->cframe // Add our C frame to cframe chain. - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | sd L, DISPATCH_GL(cur_L)(DISPATCH) - | ld TMP2, L->base // TMP2 = old base (used in vmeta_call). - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | ld TMP1, L->top - | .FPU mtc1 TMP3, TOBIT - | daddu PC, PC, BASE - | dsubu NARGS8:RC, TMP1, BASE - | li TISNUM, LJ_TISNUM - | dsubu PC, PC, TMP2 // PC = frame delta + frame type - | .FPU cvt.d.s TOBIT, TOBIT - | li_vmstate INTERP - | li TISNIL, LJ_TNIL - | st_vmstate - | - |->vm_call_dispatch: - | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC - | ld LFUNC:RB, FRAME_FUNC(BASE) - | checkfunc LFUNC:RB, ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | move L, CARG1 - | ld TMP0, L:CARG1->stack - | sd CARG1, SAVE_L - | ld TMP1, L->top - | ld DISPATCH, L->glref // Setup pointer to dispatch table. - | sd CARG1, SAVE_PC // Any value outside of bytecode is ok. - | dsubu TMP0, TMP0, TMP1 // Compute -savestack(L, L->top). - | ld TMP1, L->cframe - | daddiu DISPATCH, DISPATCH, GG_G2DISP - | sw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame. - | sw r0, SAVE_ERRF // No error function. - | sd TMP1, SAVE_CFRAME - | sd sp, L->cframe // Add our C frame to cframe chain. - | sd L, DISPATCH_GL(cur_L)(DISPATCH) - | jalr CARG4 // (lua_State *L, lua_CFunction func, void *ud) - |. move CFUNCADDR, CARG4 - | move BASE, CRET1 - | bnez CRET1, <3 // Else continue with the call. - |. li PC, FRAME_CP - | b ->vm_leave_cp // No base? Just remove C frame. - |. nop - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the - |// stack, so BASE doesn't need to be reloaded across these calls. - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8 - | ld TMP0, -32(BASE) // Continuation. - | move RB, BASE - | move BASE, TMP2 // Restore caller BASE. - | ld LFUNC:TMP1, FRAME_FUNC(TMP2) - |.if FFI - | sltiu AT, TMP0, 2 - |.endif - | ld PC, -24(RB) // Restore PC from [cont|PC]. - | cleartp LFUNC:TMP1 - | daddu TMP2, RA, RD - |.if FFI - | bnez AT, >1 - |.endif - |. sd TISNIL, -8(TMP2) // Ensure one valid arg. - | ld TMP1, LFUNC:TMP1->pc - | // BASE = base, RA = resultptr, RB = meta base - | jr TMP0 // Jump to continuation. - |. ld KBASE, PC2PROTO(k)(TMP1) - | - |.if FFI - |1: - | bnez TMP0, ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - |. daddiu TMP1, RB, -32 - | b ->vm_call_tail - |. dsubu RC, TMP1, BASE - |.endif - | - |->cont_cat: // RA = resultptr, RB = meta base - | lw INS, -4(PC) - | daddiu CARG2, RB, -32 - | ld CRET1, 0(RA) - | decode_RB8a MULTRES, INS - | decode_RA8a RA, INS - | decode_RB8b MULTRES - | decode_RA8b RA - | daddu TMP1, BASE, MULTRES - | sd BASE, L->base - | dsubu CARG3, CARG2, TMP1 - | bne TMP1, CARG2, ->BC_CAT_Z - |. sd CRET1, 0(CARG2) - | daddu RA, BASE, RA - | b ->cont_nop - |. sd CRET1, 0(RA) - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TSTR - | settp STR:RC, TMP0 - | b >1 - |. sd STR:RC, 0(CARG3) - | - |->vmeta_tgets: - | daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TTAB - | li TMP1, LJ_TSTR - | settp TAB:RB, TMP0 - | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv2) - | sd TAB:RB, 0(CARG2) - | settp STR:RC, TMP1 - | b >1 - |. sd STR:RC, 0(CARG3) - | - |->vmeta_tgetb: // TMP0 = index - | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | settp TMP0, TISNUM - | sd TMP0, 0(CARG3) - | - |->vmeta_tgetv: - |1: - | load_got lj_meta_tget - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - |. move CARG1, L - | // Returns TValue * (finished) or NULL (metamethod). - | beqz CRET1, >3 - |. daddiu TMP1, BASE, -FRAME_CONT - | ld CARG1, 0(CRET1) - | ins_next1 - | sd CARG1, 0(RA) - | ins_next2 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | ld BASE, L->top - | sd PC, -24(BASE) // [cont|PC] - | dsubu PC, BASE, TMP1 - | ld LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | cleartp LFUNC:RB - | b ->vm_call_dispatch_f - |. li NARGS8:RC, 16 // 2 args for func(t, k). - | - |->vmeta_tgetr: - | load_got lj_tab_getinth - | call_intern lj_tab_getinth // (GCtab *t, int32_t key) - |. nop - | // Returns cTValue * or NULL. - | beqz CRET1, ->BC_TGETR_Z - |. move CARG2, TISNIL - | b ->BC_TGETR_Z - |. ld CARG2, 0(CRET1) - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TSTR - | settp STR:RC, TMP0 - | b >1 - |. sd STR:RC, 0(CARG3) - | - |->vmeta_tsets: - | daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | li TMP0, LJ_TTAB - | li TMP1, LJ_TSTR - | settp TAB:RB, TMP0 - | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv2) - | sd TAB:RB, 0(CARG2) - | settp STR:RC, TMP1 - | b >1 - |. sd STR:RC, 0(CARG3) - | - |->vmeta_tsetb: // TMP0 = index - | daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | settp TMP0, TISNUM - | sd TMP0, 0(CARG3) - | - |->vmeta_tsetv: - |1: - | load_got lj_meta_tset - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - |. move CARG1, L - | // Returns TValue * (finished) or NULL (metamethod). - | beqz CRET1, >3 - |. ld CARG1, 0(RA) - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | ins_next1 - | sd CARG1, 0(CRET1) - | ins_next2 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | daddiu TMP1, BASE, -FRAME_CONT - | ld BASE, L->top - | sd PC, -24(BASE) // [cont|PC] - | dsubu PC, BASE, TMP1 - | ld LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | cleartp LFUNC:RB - | sd CARG1, 16(BASE) // Copy value to third argument. - | b ->vm_call_dispatch_f - |. li NARGS8:RC, 24 // 3 args for func(t, k, v) - | - |->vmeta_tsetr: - | load_got lj_tab_setinth - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - |. move CARG1, L - | // Returns TValue *. - | b ->BC_TSETR_Z - |. nop - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | // RA/RD point to o1/o2. - | move CARG2, RA - | move CARG3, RD - | load_got lj_meta_comp - | daddiu PC, PC, -4 - | sd BASE, L->base - | sd PC, SAVE_PC - | decode_OP1 CARG4, INS - | call_intern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - |3: - | sltiu AT, CRET1, 2 - | beqz AT, ->vmeta_binop - | negu TMP2, CRET1 - |4: - | lhu RD, OFS_RD(PC) - | daddiu PC, PC, 4 - | lui TMP1, (-(BCBIAS_J*4 >> 16) & 65535) - | sll RD, RD, 2 - | addu RD, RD, TMP1 - | and RD, RD, TMP2 - | daddu PC, PC, RD - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | lbu TMP1, -4+OFS_RA(PC) - | ld CRET1, 0(RA) - | sll TMP1, TMP1, 3 - | daddu TMP1, BASE, TMP1 - | b ->cont_nop - |. sd CRET1, 0(TMP1) - | - |->cont_condt: // RA = resultptr - | ld TMP0, 0(RA) - | gettp TMP0, TMP0 - | sltiu AT, TMP0, LJ_TISTRUECOND - | b <4 - |. negu TMP2, AT // Branch if result is true. - | - |->cont_condf: // RA = resultptr - | ld TMP0, 0(RA) - | gettp TMP0, TMP0 - | sltiu AT, TMP0, LJ_TISTRUECOND - | b <4 - |. addiu TMP2, AT, -1 // Branch if result is false. - | - |->vmeta_equal: - | // CARG1/CARG2 point to o1/o2. TMP0 is set to 0/1. - | load_got lj_meta_equal - | cleartp LFUNC:CARG3, CARG2 - | cleartp LFUNC:CARG2, CARG1 - | move CARG4, TMP0 - | daddiu PC, PC, -4 - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |. nop - | - |->vmeta_equal_cd: - |.if FFI - | load_got lj_meta_equal_cd - | move CARG2, INS - | daddiu PC, PC, -4 - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_meta_equal_cd // (lua_State *L, BCIns op) - |. move CARG1, L - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |. nop - |.endif - | - |->vmeta_istype: - | load_got lj_meta_istype - | daddiu PC, PC, -4 - | sd BASE, L->base - | srl CARG2, RA, 3 - | srl CARG3, RD, 3 - | sd PC, SAVE_PC - | call_intern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - |. move CARG1, L - | b ->cont_nop - |. nop - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_unm: - | move RC, RB - | - |->vmeta_arith: - | load_got lj_meta_arith - | sd BASE, L->base - | move CARG2, RA - | sd PC, SAVE_PC - | move CARG3, RB - | move CARG4, RC - | decode_OP1 CARG5, INS // CARG5 == RB. - | call_intern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - |. move CARG1, L - | // Returns NULL (finished) or TValue * (metamethod). - | beqz CRET1, ->cont_nop - |. nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | dsubu TMP1, CRET1, BASE - | sd PC, -24(CRET1) // [cont|PC] - | move TMP2, BASE - | daddiu PC, TMP1, FRAME_CONT - | move BASE, CRET1 - | b ->vm_call_dispatch - |. li NARGS8:RC, 16 // 2 args for func(o1, o2). - | - |->vmeta_len: - | // CARG2 already set by BC_LEN. -#if LJ_52 - | move MULTRES, CARG1 -#endif - | load_got lj_meta_len - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_meta_len // (lua_State *L, TValue *o) - |. move CARG1, L - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | bnez CRET1, ->vmeta_binop // Binop call for compatibility. - |. nop - | b ->BC_LEN_Z - |. move CARG1, MULTRES -#else - | b ->vmeta_binop // Binop call for compatibility. - |. nop -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // TMP2 = old base, BASE = new base, RC = nargs*8 - | load_got lj_meta_call - | sd TMP2, L->base // This is the callers base! - | daddiu CARG2, BASE, -16 - | sd PC, SAVE_PC - | daddu CARG3, BASE, RC - | move MULTRES, NARGS8:RC - | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - |. move CARG1, L - | ld LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | daddiu NARGS8:RC, MULTRES, 8 // Got one more argument now. - | cleartp LFUNC:RB - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | load_got lj_meta_call - | sd BASE, L->base - | daddiu CARG2, RA, -16 - | sd PC, SAVE_PC - | daddu CARG3, RA, RC - | move MULTRES, NARGS8:RC - | call_intern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - |. move CARG1, L - | ld RB, FRAME_FUNC(RA) // Guaranteed to be a function here. - | ld TMP1, FRAME_PC(BASE) - | daddiu NARGS8:RC, MULTRES, 8 // Got one more argument now. - | b ->BC_CALLT_Z - |. cleartp LFUNC:CARG3, RB - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | load_got lj_meta_for - | sd BASE, L->base - | move CARG2, RA - | sd PC, SAVE_PC - | move MULTRES, INS - | call_intern lj_meta_for // (lua_State *L, TValue *base) - |. move CARG1, L - |.if JIT - | decode_OP1 TMP0, MULTRES - | li AT, BC_JFORI - |.endif - | decode_RA8a RA, MULTRES - | decode_RD8a RD, MULTRES - | decode_RA8b RA - |.if JIT - | beq TMP0, AT, =>BC_JFORI - |. decode_RD8b RD - | b =>BC_FORI - |. nop - |.else - | b =>BC_FORI - |. decode_RD8b RD - |.endif - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | beqz NARGS8:RC, ->fff_fallback - |. ld CARG1, 0(BASE) - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | sltiu AT, NARGS8:RC, 16 - | ld CARG1, 0(BASE) - | bnez AT, ->fff_fallback - |. ld CARG2, 8(BASE) - |.endmacro - | - |.macro .ffunc_n, name // Caveat: has delay slot! - |->ff_ .. name: - | ld CARG1, 0(BASE) - | beqz NARGS8:RC, ->fff_fallback - | // Either ldc1 or the 1st instruction of checknum is in the delay slot. - | .FPU ldc1 FARG1, 0(BASE) - | checknum CARG1, ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name // Caveat: has delay slot! - |->ff_ .. name: - | ld CARG1, 0(BASE) - | sltiu AT, NARGS8:RC, 16 - | ld CARG2, 8(BASE) - | bnez AT, ->fff_fallback - |. gettp TMP0, CARG1 - | gettp TMP1, CARG2 - | sltiu TMP0, TMP0, LJ_TISNUM - | sltiu TMP1, TMP1, LJ_TISNUM - | .FPU ldc1 FARG1, 0(BASE) - | and TMP0, TMP0, TMP1 - | .FPU ldc1 FARG2, 8(BASE) - | beqz TMP0, ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1 and has delay slot! - |// MIPSR6: no delay slot, but a forbidden slot. - |.macro ffgccheck - | ld TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | ld TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | dsubu AT, TMP0, TMP1 - |.if MIPSR6 - | bgezalc AT, ->fff_gcstep - |.else - | bgezal AT, ->fff_gcstep - |.endif - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - |.ffunc_1 assert - | gettp AT, CARG1 - | sltiu AT, AT, LJ_TISTRUECOND - | beqz AT, ->fff_fallback - |. daddiu RA, BASE, -16 - | ld PC, FRAME_PC(BASE) - | addiu RD, NARGS8:RC, 8 // Compute (nresults+1)*8. - | daddu TMP2, RA, RD - | daddiu TMP1, BASE, 8 - | beq BASE, TMP2, ->fff_res // Done if exactly 1 argument. - |. sd CARG1, 0(RA) - |1: - | ld CRET1, 0(TMP1) - | sd CRET1, -16(TMP1) - | bne TMP1, TMP2, <1 - |. daddiu TMP1, TMP1, 8 - | b ->fff_res - |. nop - | - |.ffunc_1 type - | gettp TMP0, CARG1 - | sltu TMP1, TISNUM, TMP0 - | not TMP2, TMP0 - | li TMP3, ~LJ_TISNUM - |.if MIPSR6 - | selnez TMP2, TMP2, TMP1 - | seleqz TMP3, TMP3, TMP1 - | or TMP2, TMP2, TMP3 - |.else - | movz TMP2, TMP3, TMP1 - |.endif - | dsll TMP2, TMP2, 3 - | daddu TMP2, CFUNC:RB, TMP2 - | b ->fff_restv - |. ld CARG1, CFUNC:TMP2->upvalue - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | gettp TMP2, CARG1 - | daddiu TMP0, TMP2, -LJ_TTAB - | daddiu TMP1, TMP2, -LJ_TUDATA - |.if MIPSR6 - | selnez TMP0, TMP1, TMP0 - |.else - | movn TMP0, TMP1, TMP0 - |.endif - | bnez TMP0, >6 - |. cleartp TAB:CARG1 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | ld TAB:RB, TAB:CARG1->metatable - |2: - | ld STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH) - | beqz TAB:RB, ->fff_restv - |. li CARG1, LJ_TNIL - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->sid - | ld NODE:TMP2, TAB:RB->node - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | dsll TMP0, TMP1, 5 - | dsll TMP1, TMP1, 3 - | dsubu TMP1, TMP0, TMP1 - | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - | li CARG4, LJ_TSTR - | settp STR:RC, CARG4 // Tagged key to look for. - |3: // Rearranged logic, because we expect _not_ to find the key. - | ld TMP0, NODE:TMP2->key - | ld CARG1, NODE:TMP2->val - | ld NODE:TMP2, NODE:TMP2->next - | beq RC, TMP0, >5 - |. li AT, LJ_TTAB - | bnez NODE:TMP2, <3 - |. nop - |4: - | move CARG1, RB - | b ->fff_restv // Not found, keep default result. - |. settp CARG1, AT - |5: - | bne CARG1, TISNIL, ->fff_restv - |. nop - | b <4 // Ditto for nil value. - |. nop - | - |6: - | sltiu AT, TMP2, LJ_TISNUM - |.if MIPSR6 - | selnez TMP0, TISNUM, AT - | seleqz AT, TMP2, AT - | or TMP2, TMP0, AT - |.else - | movn TMP2, TISNUM, AT - |.endif - | dsll TMP2, TMP2, 3 - | dsubu TMP0, DISPATCH, TMP2 - | b <2 - |. ld TAB:RB, DISPATCH_GL(gcroot[GCROOT_BASEMT])-8(TMP0) - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | checktp TMP1, CARG1, -LJ_TTAB, ->fff_fallback - | gettp TMP3, CARG2 - | ld TAB:TMP0, TAB:TMP1->metatable - | lbu TMP2, TAB:TMP1->marked - | daddiu AT, TMP3, -LJ_TTAB - | cleartp TAB:CARG2 - | or AT, AT, TAB:TMP0 - | bnez AT, ->fff_fallback - |. andi AT, TMP2, LJ_GC_BLACK // isblack(table) - | beqz AT, ->fff_restv - |. sd TAB:CARG2, TAB:TMP1->metatable - | barrierback TAB:TMP1, TMP2, TMP0, ->fff_restv - | - |.ffunc rawget - | ld CARG2, 0(BASE) - | sltiu AT, NARGS8:RC, 16 - | load_got lj_tab_get - | gettp TMP0, CARG2 - | cleartp CARG2 - | daddiu TMP0, TMP0, -LJ_TTAB - | or AT, AT, TMP0 - | bnez AT, ->fff_fallback - |. daddiu CARG3, BASE, 8 - | call_intern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - |. move CARG1, L - | b ->fff_restv - |. ld CARG1, 0(CRET1) - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | ld CARG1, 0(BASE) - | xori AT, NARGS8:RC, 8 // Exactly one number argument. - | gettp TMP1, CARG1 - | sltu TMP0, TISNUM, TMP1 - | or AT, AT, TMP0 - | bnez AT, ->fff_fallback - |. nop - | b ->fff_restv - |. nop - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | gettp TMP0, CARG1 - | daddiu AT, TMP0, -LJ_TSTR - | // A __tostring method in the string base metatable is ignored. - | beqz AT, ->fff_restv // String key? - | // Handle numbers inline, unless a number base metatable is present. - |. ld TMP1, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH) - | sltu TMP0, TISNUM, TMP0 - | or TMP0, TMP0, TMP1 - | bnez TMP0, ->fff_fallback - |. sd BASE, L->base // Add frame since C call can throw. - |.if MIPSR6 - | sd PC, SAVE_PC // Redundant (but a defined value). - | ffgccheck - |.else - | ffgccheck - |. sd PC, SAVE_PC // Redundant (but a defined value). - |.endif - | load_got lj_strfmt_number - | move CARG1, L - | call_intern lj_strfmt_number // (lua_State *L, cTValue *o) - |. move CARG2, BASE - | // Returns GCstr *. - | li AT, LJ_TSTR - | settp CRET1, AT - | b ->fff_restv - |. move CARG1, CRET1 - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | checktp CARG1, -LJ_TTAB, ->fff_fallback - | daddu TMP2, BASE, NARGS8:RC - | sd TISNIL, 0(TMP2) // Set missing 2nd arg to nil. - | load_got lj_tab_next - | ld PC, FRAME_PC(BASE) - | daddiu CARG2, BASE, 8 - | call_intern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - |. daddiu CARG3, BASE, -16 - | // Returns 1=found, 0=end, -1=error. - | daddiu RA, BASE, -16 - | bgtz CRET1, ->fff_res // Found key/value. - |. li RD, (2+1)*8 - | beqz CRET1, ->fff_restv // End of traversal: return nil. - |. move CARG1, TISNIL - | ld CFUNC:RB, FRAME_FUNC(BASE) - | cleartp CFUNC:RB - | b ->fff_fallback // Invalid key. - |. li RC, 2*8 - | - |.ffunc_1 pairs - | checktp TAB:TMP1, CARG1, -LJ_TTAB, ->fff_fallback - | ld PC, FRAME_PC(BASE) -#if LJ_52 - | ld TAB:TMP2, TAB:TMP1->metatable - | ld TMP0, CFUNC:RB->upvalue[0] - | bnez TAB:TMP2, ->fff_fallback -#else - | ld TMP0, CFUNC:RB->upvalue[0] -#endif - |. daddiu RA, BASE, -16 - | sd TISNIL, 0(BASE) - | sd CARG1, -8(BASE) - | sd TMP0, 0(RA) - | b ->fff_res - |. li RD, (3+1)*8 - | - |.ffunc_2 ipairs_aux - | checktab CARG1, ->fff_fallback - | checkint CARG2, ->fff_fallback - |. lw TMP0, TAB:CARG1->asize - | ld TMP1, TAB:CARG1->array - | ld PC, FRAME_PC(BASE) - | sextw TMP2, CARG2 - | addiu TMP2, TMP2, 1 - | sltu AT, TMP2, TMP0 - | daddiu RA, BASE, -16 - | zextw TMP0, TMP2 - | settp TMP0, TISNUM - | beqz AT, >2 // Not in array part? - |. sd TMP0, 0(RA) - | dsll TMP3, TMP2, 3 - | daddu TMP3, TMP1, TMP3 - | ld TMP1, 0(TMP3) - |1: - | beq TMP1, TISNIL, ->fff_res // End of iteration, return 0 results. - |. li RD, (0+1)*8 - | sd TMP1, -8(BASE) - | b ->fff_res - |. li RD, (2+1)*8 - |2: // Check for empty hash part first. Otherwise call C function. - | lw TMP0, TAB:CARG1->hmask - | load_got lj_tab_getinth - | beqz TMP0, ->fff_res - |. li RD, (0+1)*8 - | call_intern lj_tab_getinth // (GCtab *t, int32_t key) - |. move CARG2, TMP2 - | // Returns cTValue * or NULL. - | beqz CRET1, ->fff_res - |. li RD, (0+1)*8 - | b <1 - |. ld TMP1, 0(CRET1) - | - |.ffunc_1 ipairs - | checktp TAB:TMP1, CARG1, -LJ_TTAB, ->fff_fallback - | ld PC, FRAME_PC(BASE) -#if LJ_52 - | ld TAB:TMP2, TAB:TMP1->metatable - | ld CFUNC:TMP0, CFUNC:RB->upvalue[0] - | bnez TAB:TMP2, ->fff_fallback -#else - | ld TMP0, CFUNC:RB->upvalue[0] -#endif - | daddiu RA, BASE, -16 - | dsll AT, TISNUM, 47 - | sd CARG1, -8(BASE) - | sd AT, 0(BASE) - | sd CFUNC:TMP0, 0(RA) - | b ->fff_res - |. li RD, (3+1)*8 - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | daddiu NARGS8:RC, NARGS8:RC, -8 - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | bltz NARGS8:RC, ->fff_fallback - |. move TMP2, BASE - | daddiu BASE, BASE, 16 - | // Remember active hook before pcall. - | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT - | andi TMP3, TMP3, 1 - | daddiu PC, TMP3, 16+FRAME_PCALL - | beqz NARGS8:RC, ->vm_call_dispatch - |1: - |. daddu TMP0, BASE, NARGS8:RC - |2: - | ld TMP1, -16(TMP0) - | sd TMP1, -8(TMP0) - | daddiu TMP0, TMP0, -8 - | bne TMP0, BASE, <2 - |. nop - | b ->vm_call_dispatch - |. nop - | - |.ffunc xpcall - | daddiu NARGS8:TMP0, NARGS8:RC, -16 - | ld CARG1, 0(BASE) - | ld CARG2, 8(BASE) - | bltz NARGS8:TMP0, ->fff_fallback - |. lbu TMP1, DISPATCH_GL(hookmask)(DISPATCH) - | gettp AT, CARG2 - | daddiu AT, AT, -LJ_TFUNC - | bnez AT, ->fff_fallback // Traceback must be a function. - |. move TMP2, BASE - | move NARGS8:RC, NARGS8:TMP0 - | daddiu BASE, BASE, 24 - | // Remember active hook before pcall. - | srl TMP3, TMP3, HOOK_ACTIVE_SHIFT - | sd CARG2, 0(TMP2) // Swap function and traceback. - | andi TMP3, TMP3, 1 - | sd CARG1, 8(TMP2) - | beqz NARGS8:RC, ->vm_call_dispatch - |. daddiu PC, TMP3, 24+FRAME_PCALL - | b <1 - |. nop - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | checktp CARG1, CARG1, -LJ_TTHREAD, ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | ld L:CARG1, CFUNC:RB->upvalue[0].gcr - | cleartp L:CARG1 - |.endif - | lbu TMP0, L:CARG1->status - | ld TMP1, L:CARG1->cframe - | ld CARG2, L:CARG1->top - | ld TMP2, L:CARG1->base - | addiu AT, TMP0, -LUA_YIELD - | daddu CARG3, CARG2, TMP0 - | daddiu TMP3, CARG2, 8 - |.if MIPSR6 - | seleqz CARG2, CARG2, AT - | selnez TMP3, TMP3, AT - | bgtz AT, ->fff_fallback // st > LUA_YIELD? - |. or CARG2, TMP3, CARG2 - |.else - | bgtz AT, ->fff_fallback // st > LUA_YIELD? - |. movn CARG2, TMP3, AT - |.endif - | xor TMP2, TMP2, CARG3 - | bnez TMP1, ->fff_fallback // cframe != 0? - |. or AT, TMP2, TMP0 - | ld TMP0, L:CARG1->maxstack - | beqz AT, ->fff_fallback // base == top && st == 0? - |. ld PC, FRAME_PC(BASE) - | daddu TMP2, CARG2, NARGS8:RC - | sltu AT, TMP0, TMP2 - | bnez AT, ->fff_fallback // Stack overflow? - |. sd PC, SAVE_PC - | sd BASE, L->base - |1: - |.if resume - | daddiu BASE, BASE, 8 // Keep resumed thread in stack for GC. - | daddiu NARGS8:RC, NARGS8:RC, -8 - | daddiu TMP2, TMP2, -8 - |.endif - | sd TMP2, L:CARG1->top - | daddu TMP1, BASE, NARGS8:RC - | move CARG3, CARG2 - | sd BASE, L->top - |2: // Move args to coroutine. - | ld CRET1, 0(BASE) - | sltu AT, BASE, TMP1 - | beqz AT, >3 - |. daddiu BASE, BASE, 8 - | sd CRET1, 0(CARG3) - | b <2 - |. daddiu CARG3, CARG3, 8 - |3: - | bal ->vm_resume // (lua_State *L, TValue *base, 0, 0) - |. move L:RA, L:CARG1 - | // Returns thread status. - |4: - | ld TMP2, L:RA->base - | sltiu AT, CRET1, LUA_YIELD+1 - | ld TMP3, L:RA->top - | li_vmstate INTERP - | ld BASE, L->base - | sd L, DISPATCH_GL(cur_L)(DISPATCH) - | st_vmstate - | beqz AT, >8 - |. dsubu RD, TMP3, TMP2 - | ld TMP0, L->maxstack - | beqz RD, >6 // No results? - |. daddu TMP1, BASE, RD - | sltu AT, TMP0, TMP1 - | bnez AT, >9 // Need to grow stack? - |. daddu TMP3, TMP2, RD - | sd TMP2, L:RA->top // Clear coroutine stack. - | move TMP1, BASE - |5: // Move results from coroutine. - | ld CRET1, 0(TMP2) - | daddiu TMP2, TMP2, 8 - | sltu AT, TMP2, TMP3 - | sd CRET1, 0(TMP1) - | bnez AT, <5 - |. daddiu TMP1, TMP1, 8 - |6: - | andi TMP0, PC, FRAME_TYPE - |.if resume - | mov_true TMP1 - | daddiu RA, BASE, -8 - | sd TMP1, -8(BASE) // Prepend true to results. - | daddiu RD, RD, 16 - |.else - | move RA, BASE - | daddiu RD, RD, 8 - |.endif - |7: - | sd PC, SAVE_PC - | beqz TMP0, ->BC_RET_Z - |. move MULTRES, RD - | b ->vm_return - |. nop - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | daddiu TMP3, TMP3, -8 - | mov_false TMP1 - | ld CRET1, 0(TMP3) - | sd TMP3, L:RA->top // Remove error from coroutine stack. - | li RD, (2+1)*8 - | sd TMP1, -8(BASE) // Prepend false to results. - | daddiu RA, BASE, -8 - | sd CRET1, 0(BASE) // Copy error message. - | b <7 - |. andi TMP0, PC, FRAME_TYPE - |.else - | load_got lj_ffh_coroutine_wrap_err - | move CARG2, L:RA - | call_intern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - |. move CARG1, L - |.endif - | - |9: // Handle stack expansion on return from yield. - | load_got lj_state_growstack - | srl CARG2, RD, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | b <4 - |. li CRET1, 0 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | ld TMP0, L->cframe - | daddu TMP1, BASE, NARGS8:RC - | sd BASE, L->base - | andi TMP0, TMP0, CFRAME_RESUME - | sd TMP1, L->top - | beqz TMP0, ->fff_fallback - |. li CRET1, LUA_YIELD - | sd r0, L->cframe - | b ->vm_leave_unw - |. sb CRET1, L->status - | - |//-- Math library ------------------------------------------------------- - | - |.ffunc_1 math_abs - | gettp CARG2, CARG1 - | daddiu AT, CARG2, -LJ_TISNUM - | bnez AT, >1 - |. sextw TMP1, CARG1 - | sra TMP0, TMP1, 31 // Extract sign. - | xor TMP1, TMP1, TMP0 - | dsubu CARG1, TMP1, TMP0 - | dsll TMP3, CARG1, 32 - | bgez TMP3, ->fff_restv - |. settp CARG1, TISNUM - | li CARG1, 0x41e0 // 2^31 as a double. - | b ->fff_restv - |. dsll CARG1, CARG1, 48 - |1: - | sltiu AT, CARG2, LJ_TISNUM - | beqz AT, ->fff_fallback - |. dextm CARG1, CARG1, 0, 30 - |// fallthrough - | - |->fff_restv: - | // CARG1 = TValue result. - | ld PC, FRAME_PC(BASE) - | daddiu RA, BASE, -16 - | sd CARG1, -16(BASE) - |->fff_res1: - | // RA = results, PC = return. - | li RD, (1+1)*8 - |->fff_res: - | // RA = results, RD = (nresults+1)*8, PC = return. - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->vm_return - |. move MULTRES, RD - | lw INS, -4(PC) - | decode_RB8a RB, INS - | decode_RB8b RB - |5: - | sltu AT, RD, RB - | bnez AT, >6 // More results expected? - |. decode_RA8a TMP0, INS - | decode_RA8b TMP0 - | ins_next1 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | dsubu BASE, RA, TMP0 - | ins_next2 - | - |6: // Fill up results with nil. - | daddu TMP1, RA, RD - | daddiu RD, RD, 8 - | b <5 - |. sd TISNIL, -8(TMP1) - | - |.macro math_extern, func - | .ffunc_n math_ .. func - | load_got func - | call_extern - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - |. load_got func - | call_extern - |. nop - | b ->fff_resn - |. nop - |.endmacro - | - |// TODO: Return integer type if result is integer (own sf implementation). - |.macro math_round, func - |->ff_math_ .. func: - | ld CARG1, 0(BASE) - | beqz NARGS8:RC, ->fff_fallback - |. gettp TMP0, CARG1 - | beq TMP0, TISNUM, ->fff_restv - |. sltu AT, TMP0, TISNUM - | beqz AT, ->fff_fallback - |.if FPU - |. ldc1 FARG1, 0(BASE) - | bal ->vm_ .. func - |. nop - |.else - |. load_got func - | call_extern - |. nop - |.endif - | b ->fff_resn - |. nop - |.endmacro - | - | math_round floor - | math_round ceil - | - |.ffunc math_log - | li AT, 8 - | bne NARGS8:RC, AT, ->fff_fallback // Exactly 1 argument. - |. ld CARG1, 0(BASE) - | checknum CARG1, ->fff_fallback - |. load_got log - |.if FPU - | call_extern - |. ldc1 FARG1, 0(BASE) - |.else - | call_extern - |. nop - |.endif - | b ->fff_resn - |. nop - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.if FPU - |.ffunc_n math_sqrt - |. sqrt.d FRET1, FARG1 - |// fallthrough to ->fff_resn - |.else - | math_extern sqrt - |.endif - | - |->fff_resn: - | ld PC, FRAME_PC(BASE) - | daddiu RA, BASE, -16 - | b ->fff_res1 - |.if FPU - |. sdc1 FRET1, 0(RA) - |.else - |. sd CRET1, 0(RA) - |.endif - | - | - |.ffunc_2 math_ldexp - | checknum CARG1, ->fff_fallback - | checkint CARG2, ->fff_fallback - |. load_got ldexp - | .FPU ldc1 FARG1, 0(BASE) - | call_extern - |. lw CARG2, 8+LO(BASE) - | b ->fff_resn - |. nop - | - |.ffunc_n math_frexp - | load_got frexp - | ld PC, FRAME_PC(BASE) - | call_extern - |. daddiu CARG2, DISPATCH, DISPATCH_GL(tmptv) - | lw TMP1, DISPATCH_GL(tmptv)(DISPATCH) - | daddiu RA, BASE, -16 - |.if FPU - | mtc1 TMP1, FARG2 - | sdc1 FRET1, 0(RA) - | cvt.d.w FARG2, FARG2 - | sdc1 FARG2, 8(RA) - |.else - | sd CRET1, 0(RA) - | zextw TMP1, TMP1 - | settp TMP1, TISNUM - | sd TMP1, 8(RA) - |.endif - | b ->fff_res - |. li RD, (2+1)*8 - | - |.ffunc_n math_modf - | load_got modf - | ld PC, FRAME_PC(BASE) - | call_extern - |. daddiu CARG2, BASE, -16 - | daddiu RA, BASE, -16 - |.if FPU - | sdc1 FRET1, -8(BASE) - |.else - | sd CRET1, -8(BASE) - |.endif - | b ->fff_res - |. li RD, (2+1)*8 - | - |.macro math_minmax, name, intins, intinsc, fpins - | .ffunc_1 name - | daddu TMP3, BASE, NARGS8:RC - | checkint CARG1, >5 - |. daddiu TMP2, BASE, 8 - |1: // Handle integers. - | beq TMP2, TMP3, ->fff_restv - |. ld CARG2, 0(TMP2) - | checkint CARG2, >3 - |. sextw CARG1, CARG1 - | lw CARG2, LO(TMP2) - |. slt AT, CARG1, CARG2 - |.if MIPSR6 - | intins TMP1, CARG2, AT - | intinsc CARG1, CARG1, AT - | or CARG1, CARG1, TMP1 - |.else - | intins CARG1, CARG2, AT - |.endif - | daddiu TMP2, TMP2, 8 - | zextw CARG1, CARG1 - | b <1 - |. settp CARG1, TISNUM - | - |3: // Convert intermediate result to number and continue with number loop. - | checknum CARG2, ->fff_fallback - |.if FPU - |. mtc1 CARG1, FRET1 - | cvt.d.w FRET1, FRET1 - | b >7 - |. ldc1 FARG1, 0(TMP2) - |.else - |. nop - | bal ->vm_sfi2d_1 - |. nop - | b >7 - |. nop - |.endif - | - |5: - | .FPU ldc1 FRET1, 0(BASE) - | checknum CARG1, ->fff_fallback - |6: // Handle numbers. - |. ld CARG2, 0(TMP2) - | beq TMP2, TMP3, ->fff_resn - |.if FPU - | ldc1 FARG1, 0(TMP2) - |.else - | move CRET1, CARG1 - |.endif - | checknum CARG2, >8 - |. nop - |7: - |.if FPU - |.if MIPSR6 - | fpins FRET1, FRET1, FARG1 - |.else - |.if fpins // ismax - | c.olt.d FARG1, FRET1 - |.else - | c.olt.d FRET1, FARG1 - |.endif - | movf.d FRET1, FARG1 - |.endif - |.else - |.if fpins // ismax - | bal ->vm_sfcmpogt - |.else - | bal ->vm_sfcmpolt - |.endif - |. nop - |.if MIPSR6 - | seleqz AT, CARG2, CRET1 - | selnez CARG1, CARG1, CRET1 - | or CARG1, CARG1, AT - |.else - | movz CARG1, CARG2, CRET1 - |.endif - |.endif - | b <6 - |. daddiu TMP2, TMP2, 8 - | - |8: // Convert integer to number and continue with number loop. - | checkint CARG2, ->fff_fallback - |.if FPU - |. lwc1 FARG1, LO(TMP2) - | b <7 - |. cvt.d.w FARG1, FARG1 - |.else - |. lw CARG2, LO(TMP2) - | bal ->vm_sfi2d_2 - |. nop - | b <7 - |. nop - |.endif - | - |.endmacro - | - |.if MIPSR6 - | math_minmax math_min, seleqz, selnez, min.d - | math_minmax math_max, selnez, seleqz, max.d - |.else - | math_minmax math_min, movz, _, 0 - | math_minmax math_max, movn, _, 1 - |.endif - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | ld CARG1, 0(BASE) - | gettp TMP0, CARG1 - | xori AT, NARGS8:RC, 8 - | daddiu TMP0, TMP0, -LJ_TSTR - | or AT, AT, TMP0 - | bnez AT, ->fff_fallback // Need exactly 1 string argument. - |. cleartp STR:CARG1 - | lw TMP0, STR:CARG1->len - | daddiu RA, BASE, -16 - | ld PC, FRAME_PC(BASE) - | sltu RD, r0, TMP0 - | lbu TMP1, STR:CARG1[1] // Access is always ok (NUL at end). - | addiu RD, RD, 1 - | sll RD, RD, 3 // RD = ((str->len != 0)+1)*8 - | settp TMP1, TISNUM - | b ->fff_res - |. sd TMP1, 0(RA) - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - |.if not MIPSR6 - |. nop - |.endif - | ld CARG1, 0(BASE) - | gettp TMP0, CARG1 - | xori AT, NARGS8:RC, 8 // Exactly 1 argument. - | daddiu TMP0, TMP0, -LJ_TISNUM // Integer. - | li TMP1, 255 - | sextw CARG1, CARG1 - | or AT, AT, TMP0 - | sltu TMP1, TMP1, CARG1 // !(255 < n). - | or AT, AT, TMP1 - | bnez AT, ->fff_fallback - |. li CARG3, 1 - | daddiu CARG2, sp, TMPD_OFS - | sb CARG1, TMPD - |->fff_newstr: - | load_got lj_str_new - | sd BASE, L->base - | sd PC, SAVE_PC - | call_intern lj_str_new // (lua_State *L, char *str, size_t l) - |. move CARG1, L - | // Returns GCstr *. - | ld BASE, L->base - |->fff_resstr: - | li AT, LJ_TSTR - | settp CRET1, AT - | b ->fff_restv - |. move CARG1, CRET1 - | - |.ffunc string_sub - | ffgccheck - |.if not MIPSR6 - |. nop - |.endif - | addiu AT, NARGS8:RC, -16 - | ld TMP0, 0(BASE) - | bltz AT, ->fff_fallback - |. gettp TMP3, TMP0 - | cleartp STR:CARG1, TMP0 - | ld CARG2, 8(BASE) - | beqz AT, >1 - |. li CARG4, -1 - | ld CARG3, 16(BASE) - | checkint CARG3, ->fff_fallback - |. sextw CARG4, CARG3 - |1: - | checkint CARG2, ->fff_fallback - |. li AT, LJ_TSTR - | bne TMP3, AT, ->fff_fallback - |. sextw CARG3, CARG2 - | lw CARG2, STR:CARG1->len - | // STR:CARG1 = str, CARG2 = str->len, CARG3 = start, CARG4 = end - | slt AT, CARG4, r0 - | addiu TMP0, CARG2, 1 - | addu TMP1, CARG4, TMP0 - | slt TMP3, CARG3, r0 - |.if MIPSR6 - | seleqz CARG4, CARG4, AT - | selnez TMP1, TMP1, AT - | or CARG4, TMP1, CARG4 // if (end < 0) end += len+1 - |.else - | movn CARG4, TMP1, AT // if (end < 0) end += len+1 - |.endif - | addu TMP1, CARG3, TMP0 - |.if MIPSR6 - | selnez TMP1, TMP1, TMP3 - | seleqz CARG3, CARG3, TMP3 - | or CARG3, TMP1, CARG3 // if (start < 0) start += len+1 - | li TMP2, 1 - | slt AT, CARG4, r0 - | slt TMP3, r0, CARG3 - | seleqz CARG4, CARG4, AT // if (end < 0) end = 0 - | selnez CARG3, CARG3, TMP3 - | seleqz TMP2, TMP2, TMP3 - | or CARG3, TMP2, CARG3 // if (start < 1) start = 1 - | slt AT, CARG2, CARG4 - | seleqz CARG4, CARG4, AT - | selnez CARG2, CARG2, AT - | or CARG4, CARG2, CARG4 // if (end > len) end = len - |.else - | movn CARG3, TMP1, TMP3 // if (start < 0) start += len+1 - | li TMP2, 1 - | slt AT, CARG4, r0 - | slt TMP3, r0, CARG3 - | movn CARG4, r0, AT // if (end < 0) end = 0 - | movz CARG3, TMP2, TMP3 // if (start < 1) start = 1 - | slt AT, CARG2, CARG4 - | movn CARG4, CARG2, AT // if (end > len) end = len - |.endif - | daddu CARG2, STR:CARG1, CARG3 - | subu CARG3, CARG4, CARG3 // len = end - start - | daddiu CARG2, CARG2, sizeof(GCstr)-1 - | bgez CARG3, ->fff_newstr - |. addiu CARG3, CARG3, 1 // len++ - |->fff_emptystr: // Return empty string. - | li AT, LJ_TSTR - | daddiu STR:CARG1, DISPATCH, DISPATCH_GL(strempty) - | b ->fff_restv - |. settp CARG1, AT - | - |.macro ffstring_op, name - | .ffunc string_ .. name - | ffgccheck - |. nop - | beqz NARGS8:RC, ->fff_fallback - |. ld CARG2, 0(BASE) - | checkstr STR:CARG2, ->fff_fallback - | daddiu SBUF:CARG1, DISPATCH, DISPATCH_GL(tmpbuf) - | load_got lj_buf_putstr_ .. name - | ld TMP0, SBUF:CARG1->b - | sd L, SBUF:CARG1->L - | sd BASE, L->base - | sd TMP0, SBUF:CARG1->w - | call_intern extern lj_buf_putstr_ .. name - |. sd PC, SAVE_PC - | load_got lj_buf_tostr - | call_intern lj_buf_tostr - |. move SBUF:CARG1, SBUF:CRET1 - | b ->fff_resstr - |. ld BASE, L->base - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |->vm_tobit_fb: - | beqz TMP1, ->fff_fallback - |.if FPU - |. ldc1 FARG1, 0(BASE) - | add.d FARG1, FARG1, TOBIT - | mfc1 CRET1, FARG1 - | jr ra - |. zextw CRET1, CRET1 - |.else - |// FP number to bit conversion for soft-float. - |->vm_tobit: - | dsll TMP0, CARG1, 1 - | li CARG3, 1076 - | dsrl AT, TMP0, 53 - | dsubu CARG3, CARG3, AT - | sltiu AT, CARG3, 54 - | beqz AT, >1 - |. dextm TMP0, TMP0, 0, 20 - | dinsu TMP0, AT, 21, 21 - | slt AT, CARG1, r0 - | dsrlv CRET1, TMP0, CARG3 - | dsubu TMP0, r0, CRET1 - |.if MIPSR6 - | selnez TMP0, TMP0, AT - | seleqz CRET1, CRET1, AT - | or CRET1, CRET1, TMP0 - |.else - | movn CRET1, TMP0, AT - |.endif - | jr ra - |. zextw CRET1, CRET1 - |1: - | jr ra - |. move CRET1, r0 - | - |// FP number to int conversion with a check for soft-float. - |// Modifies CARG1, CRET1, CRET2, TMP0, AT. - |->vm_tointg: - |.if JIT - | dsll CRET2, CARG1, 1 - | beqz CRET2, >2 - |. li TMP0, 1076 - | dsrl AT, CRET2, 53 - | dsubu TMP0, TMP0, AT - | sltiu AT, TMP0, 54 - | beqz AT, >1 - |. dextm CRET2, CRET2, 0, 20 - | dinsu CRET2, AT, 21, 21 - | slt AT, CARG1, r0 - | dsrlv CRET1, CRET2, TMP0 - | dsubu CARG1, r0, CRET1 - |.if MIPSR6 - | seleqz CRET1, CRET1, AT - | selnez CARG1, CARG1, AT - | or CRET1, CRET1, CARG1 - |.else - | movn CRET1, CARG1, AT - |.endif - | li CARG1, 64 - | subu TMP0, CARG1, TMP0 - | dsllv CRET2, CRET2, TMP0 // Integer check. - | sextw AT, CRET1 - | xor AT, CRET1, AT // Range check. - |.if MIPSR6 - | seleqz AT, AT, CRET2 - | selnez CRET2, CRET2, CRET2 - | jr ra - |. or CRET2, AT, CRET2 - |.else - | jr ra - |. movz CRET2, AT, CRET2 - |.endif - |1: - | jr ra - |. li CRET2, 1 - |2: - | jr ra - |. move CRET1, r0 - |.endif - |.endif - | - |.macro .ffunc_bit, name - | .ffunc_1 bit_..name - | gettp TMP0, CARG1 - | beq TMP0, TISNUM, >6 - |. zextw CRET1, CARG1 - | bal ->vm_tobit_fb - |. sltiu TMP1, TMP0, LJ_TISNUM - |6: - |.endmacro - | - |.macro .ffunc_bit_op, name, bins - | .ffunc_bit name - | daddiu TMP2, BASE, 8 - | daddu TMP3, BASE, NARGS8:RC - |1: - | beq TMP2, TMP3, ->fff_resi - |. ld CARG1, 0(TMP2) - | gettp TMP0, CARG1 - |.if FPU - | bne TMP0, TISNUM, >2 - |. daddiu TMP2, TMP2, 8 - | zextw CARG1, CARG1 - | b <1 - |. bins CRET1, CRET1, CARG1 - |2: - | ldc1 FARG1, -8(TMP2) - | sltiu AT, TMP0, LJ_TISNUM - | beqz AT, ->fff_fallback - |. add.d FARG1, FARG1, TOBIT - | mfc1 CARG1, FARG1 - | zextw CARG1, CARG1 - | b <1 - |. bins CRET1, CRET1, CARG1 - |.else - | beq TMP0, TISNUM, >2 - |. move CRET2, CRET1 - | bal ->vm_tobit_fb - |. sltiu TMP1, TMP0, LJ_TISNUM - | move CARG1, CRET2 - |2: - | zextw CARG1, CARG1 - | bins CRET1, CRET1, CARG1 - | b <1 - |. daddiu TMP2, TMP2, 8 - |.endif - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, or - |.ffunc_bit_op bxor, xor - | - |.ffunc_bit bswap - | dsrl TMP0, CRET1, 8 - | dsrl TMP1, CRET1, 24 - | andi TMP2, TMP0, 0xff00 - | dins TMP1, CRET1, 24, 31 - | dins TMP2, TMP0, 16, 23 - | b ->fff_resi - |. or CRET1, TMP1, TMP2 - | - |.ffunc_bit bnot - | not CRET1, CRET1 - | b ->fff_resi - |. zextw CRET1, CRET1 - | - |.macro .ffunc_bit_sh, name, shins, shmod - | .ffunc_2 bit_..name - | gettp TMP0, CARG1 - | beq TMP0, TISNUM, >1 - |. nop - | bal ->vm_tobit_fb - |. sltiu TMP1, TMP0, LJ_TISNUM - | move CARG1, CRET1 - |1: - | gettp TMP0, CARG2 - | bne TMP0, TISNUM, ->fff_fallback - |. zextw CARG2, CARG2 - | sextw CARG1, CARG1 - |.if shmod == 1 - | negu CARG2, CARG2 - |.endif - | shins CRET1, CARG1, CARG2 - | b ->fff_resi - |. zextw CRET1, CRET1 - |.endmacro - | - |.ffunc_bit_sh lshift, sllv, 0 - |.ffunc_bit_sh rshift, srlv, 0 - |.ffunc_bit_sh arshift, srav, 0 - |.ffunc_bit_sh rol, rotrv, 1 - |.ffunc_bit_sh ror, rotrv, 0 - | - |.ffunc_bit tobit - |->fff_resi: - | ld PC, FRAME_PC(BASE) - | daddiu RA, BASE, -16 - | settp CRET1, TISNUM - | b ->fff_res1 - |. sd CRET1, -16(BASE) - | - |//----------------------------------------------------------------------- - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RB = CFUNC, RC = nargs*8 - | ld TMP3, CFUNC:RB->f - | daddu TMP1, BASE, NARGS8:RC - | ld PC, FRAME_PC(BASE) // Fallback may overwrite PC. - | daddiu TMP0, TMP1, 8*LUA_MINSTACK - | ld TMP2, L->maxstack - | sd PC, SAVE_PC // Redundant (but a defined value). - | sltu AT, TMP2, TMP0 - | sd BASE, L->base - | sd TMP1, L->top - | bnez AT, >5 // Need to grow stack. - |. move CFUNCADDR, TMP3 - | jalr TMP3 // (lua_State *L) - |. move CARG1, L - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | ld BASE, L->base - | sll RD, CRET1, 3 - | bgtz CRET1, ->fff_res // Returned nresults+1? - |. daddiu RA, BASE, -16 - |1: // Returned 0 or -1: retry fast path. - | ld LFUNC:RB, FRAME_FUNC(BASE) - | ld TMP0, L->top - | cleartp LFUNC:RB - | bnez CRET1, ->vm_call_tail // Returned -1? - |. dsubu NARGS8:RC, TMP0, BASE - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | andi TMP0, PC, FRAME_TYPE - | li AT, -4 - | bnez TMP0, >3 - |. and TMP1, PC, AT - | lbu TMP1, OFS_RA(PC) - | sll TMP1, TMP1, 3 - | addiu TMP1, TMP1, 16 - |3: - | b ->vm_call_dispatch // Resolve again for tailcall. - |. dsubu TMP2, BASE, TMP1 - | - |5: // Grow stack for fallback handler. - | load_got lj_state_growstack - | li CARG2, LUA_MINSTACK - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | ld BASE, L->base - | b <1 - |. li CRET1, 0 // Force retry. - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | move MULTRES, ra - | load_got lj_gc_step - | sd BASE, L->base - | daddu TMP0, BASE, NARGS8:RC - | sd PC, SAVE_PC // Redundant (but a defined value). - | sd TMP0, L->top - | call_intern lj_gc_step // (lua_State *L) - |. move CARG1, L - | ld BASE, L->base - | move ra, MULTRES - | ld TMP0, L->top - | ld CFUNC:RB, FRAME_FUNC(BASE) - | cleartp CFUNC:RB - | jr ra - |. dsubu NARGS8:RC, TMP0, BASE - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi AT, TMP3, HOOK_VMEVENT // No recording while in vmevent. - | bnez AT, >5 - | // Decrement the hookcount for consistency, but always do the call. - |. lw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE - | bnez AT, >1 - |. addiu TMP2, TMP2, -1 - | andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqz AT, >1 - |. nop - | b >1 - |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE // Hook already active? - | beqz AT, >1 - |5: // Re-dispatch to static ins. - |. ld AT, GG_DISP2STATIC(TMP0) // Assumes TMP0 holds DISPATCH+OP*4. - | jr AT - |. nop - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | lbu TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | lw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, HOOK_ACTIVE // Hook already active? - | bnez AT, <5 - |. andi AT, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqz AT, <5 - |. addiu TMP2, TMP2, -1 - | beqz TMP2, >1 - |. sw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andi AT, TMP3, LUA_MASKLINE - | beqz AT, <5 - |1: - |. load_got lj_dispatch_ins - | sw MULTRES, SAVE_MULTRES - | move CARG2, PC - | sd BASE, L->base - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | call_intern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |. move CARG1, L - |3: - | ld BASE, L->base - |4: // Re-dispatch to static ins. - | lw INS, -4(PC) - | decode_OP8a TMP1, INS - | decode_OP8b TMP1 - | daddu TMP0, DISPATCH, TMP1 - | decode_RD8a RD, INS - | ld AT, GG_DISP2STATIC(TMP0) - | decode_RA8a RA, INS - | decode_RD8b RD - | jr AT - | decode_RA8b RA - | - |->cont_hook: // Continue from hook yield. - | daddiu PC, PC, 4 - | b <4 - |. lw MULTRES, -24+LO(RB) // Restore MULTRES for *M ins. - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | ld LFUNC:TMP1, FRAME_FUNC(BASE) - | daddiu CARG1, DISPATCH, GG_DISP2J - | cleartp LFUNC:TMP1 - | sd PC, SAVE_PC - | ld TMP1, LFUNC:TMP1->pc - | move CARG2, PC - | sd L, DISPATCH_J(L)(DISPATCH) - | lbu TMP1, PC2PROTO(framesize)(TMP1) - | load_got lj_trace_hot - | sd BASE, L->base - | dsll TMP1, TMP1, 3 - | daddu TMP1, BASE, TMP1 - | call_intern lj_trace_hot // (jit_State *J, const BCIns *pc) - |. sd TMP1, L->top - | b <3 - |. nop - |.endif - | - | - |->vm_callhook: // Dispatch target for call hooks. - |.if JIT - | b >1 - |.endif - |. move CARG2, PC - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | ori CARG2, PC, 1 - |1: - |.endif - | load_got lj_dispatch_call - | daddu TMP0, BASE, RC - | sd PC, SAVE_PC - | sd BASE, L->base - | dsubu RA, RA, BASE - | sd TMP0, L->top - | call_intern lj_dispatch_call // (lua_State *L, const BCIns *pc) - |. move CARG1, L - | // Returns ASMFunction. - | ld BASE, L->base - | ld TMP0, L->top - | sd r0, SAVE_PC // Invalidate for subsequent line hook. - | dsubu NARGS8:RC, TMP0, BASE - | daddu RA, BASE, RA - | ld LFUNC:RB, FRAME_FUNC(BASE) - | cleartp LFUNC:RB - | jr CRET1 - |. lw INS, -4(PC) - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // RA = resultptr, RB = meta base - | lw INS, -4(PC) - | ld TRACE:TMP2, -40(RB) // Save previous trace. - | decode_RA8a RC, INS - | daddiu AT, MULTRES, -8 - | cleartp TRACE:TMP2 - | decode_RA8b RC - | beqz AT, >2 - |. daddu RC, BASE, RC // Call base. - |1: // Move results down. - | ld CARG1, 0(RA) - | daddiu AT, AT, -8 - | daddiu RA, RA, 8 - | sd CARG1, 0(RC) - | bnez AT, <1 - |. daddiu RC, RC, 8 - |2: - | decode_RA8a RA, INS - | decode_RB8a RB, INS - | decode_RA8b RA - | decode_RB8b RB - | daddu RA, RA, RB - | daddu RA, BASE, RA - |3: - | sltu AT, RC, RA - | bnez AT, >9 // More results wanted? - |. nop - | - | lhu TMP3, TRACE:TMP2->traceno - | lhu RD, TRACE:TMP2->link - | beq RD, TMP3, ->cont_nop // Blacklisted. - |. load_got lj_dispatch_stitch - | bnez RD, =>BC_JLOOP // Jump to stitched trace. - |. sll RD, RD, 3 - | - | // Stitch a new trace to the previous trace. - | sw TMP3, DISPATCH_J(exitno)(DISPATCH) - | sd L, DISPATCH_J(L)(DISPATCH) - | sd BASE, L->base - | daddiu CARG1, DISPATCH, GG_DISP2J - | call_intern lj_dispatch_stitch // (jit_State *J, const BCIns *pc) - |. move CARG2, PC - | b ->cont_nop - |. ld BASE, L->base - | - |9: - | sd TISNIL, 0(RC) - | b <3 - |. daddiu RC, RC, 8 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | load_got lj_dispatch_profile - | sw MULTRES, SAVE_MULTRES - | move CARG2, PC - | sd BASE, L->base - | call_intern lj_dispatch_profile // (lua_State *L, const BCIns *pc) - |. move CARG1, L - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | daddiu PC, PC, -4 - | b ->cont_nop - |. ld BASE, L->base -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro savex_, a, b - |.if FPU - | sdc1 f..a, a*8(sp) - | sdc1 f..b, b*8(sp) - | sd r..a, 32*8+a*8(sp) - | sd r..b, 32*8+b*8(sp) - |.else - | sd r..a, a*8(sp) - | sd r..b, b*8(sp) - |.endif - |.endmacro - | - |->vm_exit_handler: - |.if JIT - |.if FPU - | daddiu sp, sp, -(32*8+32*8) - |.else - | daddiu sp, sp, -(32*8) - |.endif - | savex_ 0, 1 - | savex_ 2, 3 - | savex_ 4, 5 - | savex_ 6, 7 - | savex_ 8, 9 - | savex_ 10, 11 - | savex_ 12, 13 - | savex_ 14, 15 - | savex_ 16, 17 - | savex_ 18, 19 - | savex_ 20, 21 - | savex_ 22, 23 - | savex_ 24, 25 - | savex_ 26, 27 - | savex_ 28, 30 - |.if FPU - | sdc1 f29, 29*8(sp) - | sdc1 f31, 31*8(sp) - | sd r0, 32*8+31*8(sp) // Clear RID_TMP. - | daddiu TMP2, sp, 32*8+32*8 // Recompute original value of sp. - | sd TMP2, 32*8+29*8(sp) // Store sp in RID_SP - |.else - | sd r0, 31*8(sp) // Clear RID_TMP. - | daddiu TMP2, sp, 32*8 // Recompute original value of sp. - | sd TMP2, 29*8(sp) // Store sp in RID_SP - |.endif - | li_vmstate EXIT - | daddiu DISPATCH, JGL, -GG_DISP2G-32768 - | lw TMP1, 0(TMP2) // Load exit number. - | st_vmstate - | ld L, DISPATCH_GL(cur_L)(DISPATCH) - | ld BASE, DISPATCH_GL(jit_base)(DISPATCH) - | load_got lj_trace_exit - | sd L, DISPATCH_J(L)(DISPATCH) - | sw ra, DISPATCH_J(parent)(DISPATCH) // Store trace number. - | sd BASE, L->base - | sw TMP1, DISPATCH_J(exitno)(DISPATCH) // Store exit number. - | daddiu CARG1, DISPATCH, GG_DISP2J - | sd r0, DISPATCH_GL(jit_base)(DISPATCH) - | call_intern lj_trace_exit // (jit_State *J, ExitState *ex) - |. move CARG2, sp - | // Returns MULTRES (unscaled) or negated error code. - | ld TMP1, L->cframe - | li AT, -4 - | ld BASE, L->base - | and sp, TMP1, AT - | ld PC, SAVE_PC // Get SAVE_PC. - | b >1 - |. sd L, SAVE_L // Set SAVE_L (on-trace resume/yield). - |.endif - |->vm_exit_interp: - |.if JIT - | // CRET1 = MULTRES or negated error code, BASE, PC and JGL set. - | ld L, SAVE_L - | daddiu DISPATCH, JGL, -GG_DISP2G-32768 - | sd BASE, L->base - |1: - | bltz CRET1, >9 // Check for error from exit. - |. ld LFUNC:RB, FRAME_FUNC(BASE) - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | dsll MULTRES, CRET1, 3 - | cleartp LFUNC:RB - | sw MULTRES, SAVE_MULTRES - | li TISNIL, LJ_TNIL - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | .FPU mtc1 TMP3, TOBIT - | ld TMP1, LFUNC:RB->pc - | sd r0, DISPATCH_GL(jit_base)(DISPATCH) - | ld KBASE, PC2PROTO(k)(TMP1) - | .FPU cvt.d.s TOBIT, TOBIT - | // Modified copy of ins_next which handles function header dispatch, too. - | lw INS, 0(PC) - | daddiu PC, PC, 4 - | // Assumes TISNIL == ~LJ_VMST_INTERP == -1 - | sw TISNIL, DISPATCH_GL(vmstate)(DISPATCH) - | decode_OP8a TMP1, INS - | decode_OP8b TMP1 - | sltiu TMP2, TMP1, BC_FUNCF*8 - | daddu TMP0, DISPATCH, TMP1 - | decode_RD8a RD, INS - | ld AT, 0(TMP0) - | decode_RA8a RA, INS - | beqz TMP2, >2 - |. decode_RA8b RA - | jr AT - |. decode_RD8b RD - |2: - | sltiu TMP2, TMP1, (BC_FUNCC+2)*8 // Fast function? - | bnez TMP2, >3 - |. ld TMP1, FRAME_PC(BASE) - | // Check frame below fast function. - | andi TMP0, TMP1, FRAME_TYPE - | bnez TMP0, >3 // Trace stitching continuation? - |. nop - | // Otherwise set KBASE for Lua function below fast function. - | lw TMP2, -4(TMP1) - | decode_RA8a TMP0, TMP2 - | decode_RA8b TMP0 - | dsubu TMP1, BASE, TMP0 - | ld LFUNC:TMP2, -32(TMP1) - | cleartp LFUNC:TMP2 - | ld TMP1, LFUNC:TMP2->pc - | ld KBASE, PC2PROTO(k)(TMP1) - |3: - | daddiu RC, MULTRES, -8 - | jr AT - |. daddu RA, RA, BASE - | - |9: // Rethrow error from the right C frame. - | load_got lj_err_trace - | sub CARG2, r0, CRET1 - | call_intern lj_err_trace // (lua_State *L, int errcode) - |. move CARG1, L - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Hard-float round to integer. - |// Modifies AT, TMP0, FRET1, FRET2, f4. Keeps all others incl. FARG1. - |// MIPSR6: Modifies FTMP1, too. - |.macro vm_round_hf, func - | lui TMP0, 0x4330 // Hiword of 2^52 (double). - | dsll TMP0, TMP0, 32 - | dmtc1 TMP0, f4 - | abs.d FRET2, FARG1 // |x| - | dmfc1 AT, FARG1 - |.if MIPSR6 - | cmp.lt.d FTMP1, FRET2, f4 - | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52 - | bc1eqz FTMP1, >1 // Truncate only if |x| < 2^52. - |.else - | c.olt.d 0, FRET2, f4 - | add.d FRET1, FRET2, f4 // (|x| + 2^52) - 2^52 - | bc1f 0, >1 // Truncate only if |x| < 2^52. - |.endif - |. sub.d FRET1, FRET1, f4 - | slt AT, AT, r0 - |.if "func" == "ceil" - | lui TMP0, 0xbff0 // Hiword of -1 (double). Preserves -0. - |.else - | lui TMP0, 0x3ff0 // Hiword of +1 (double). - |.endif - |.if "func" == "trunc" - | dsll TMP0, TMP0, 32 - | dmtc1 TMP0, f4 - |.if MIPSR6 - | cmp.lt.d FTMP1, FRET2, FRET1 // |x| < result? - | sub.d FRET2, FRET1, f4 - | sel.d FTMP1, FRET1, FRET2 // If yes, subtract +1. - | dmtc1 AT, FRET1 - | neg.d FRET2, FTMP1 - | jr ra - |. sel.d FRET1, FTMP1, FRET2 // Merge sign bit back in. - |.else - | c.olt.d 0, FRET2, FRET1 // |x| < result? - | sub.d FRET2, FRET1, f4 - | movt.d FRET1, FRET2, 0 // If yes, subtract +1. - | neg.d FRET2, FRET1 - | jr ra - |. movn.d FRET1, FRET2, AT // Merge sign bit back in. - |.endif - |.else - | neg.d FRET2, FRET1 - | dsll TMP0, TMP0, 32 - | dmtc1 TMP0, f4 - |.if MIPSR6 - | dmtc1 AT, FTMP1 - | sel.d FTMP1, FRET1, FRET2 - |.if "func" == "ceil" - | cmp.lt.d FRET1, FTMP1, FARG1 // x > result? - |.else - | cmp.lt.d FRET1, FARG1, FTMP1 // x < result? - |.endif - | sub.d FRET2, FTMP1, f4 // If yes, subtract +-1. - | jr ra - |. sel.d FRET1, FTMP1, FRET2 - |.else - | movn.d FRET1, FRET2, AT // Merge sign bit back in. - |.if "func" == "ceil" - | c.olt.d 0, FRET1, FARG1 // x > result? - |.else - | c.olt.d 0, FARG1, FRET1 // x < result? - |.endif - | sub.d FRET2, FRET1, f4 // If yes, subtract +-1. - | jr ra - |. movt.d FRET1, FRET2, 0 - |.endif - |.endif - |1: - | jr ra - |. mov.d FRET1, FARG1 - |.endmacro - | - |.macro vm_round, func - |.if FPU - | vm_round_hf, func - |.endif - |.endmacro - | - |->vm_floor: - | vm_round floor - |->vm_ceil: - | vm_round ceil - |->vm_trunc: - |.if JIT - | vm_round trunc - |.endif - | - |// Soft-float integer to number conversion. - |.macro sfi2d, ARG - |.if not FPU - | beqz ARG, >9 // Handle zero first. - |. sra TMP0, ARG, 31 - | xor TMP1, ARG, TMP0 - | dsubu TMP1, TMP1, TMP0 // Absolute value in TMP1. - | dclz ARG, TMP1 - | addiu ARG, ARG, -11 - | li AT, 0x3ff+63-11-1 - | dsllv TMP1, TMP1, ARG // Align mantissa left with leading 1. - | subu ARG, AT, ARG // Exponent - 1. - | ins ARG, TMP0, 11, 11 // Sign | Exponent. - | dsll ARG, ARG, 52 // Align left. - | jr ra - |. daddu ARG, ARG, TMP1 // Add mantissa, increment exponent. - |9: - | jr ra - |. nop - |.endif - |.endmacro - | - |// Input CARG1. Output: CARG1. Temporaries: AT, TMP0, TMP1. - |->vm_sfi2d_1: - | sfi2d CARG1 - | - |// Input CARG2. Output: CARG2. Temporaries: AT, TMP0, TMP1. - |->vm_sfi2d_2: - | sfi2d CARG2 - | - |// Soft-float comparison. Equivalent to c.eq.d. - |// Input: CARG*. Output: CRET1. Temporaries: AT, TMP0, TMP1. - |->vm_sfcmpeq: - |.if not FPU - | dsll AT, CARG1, 1 - | dsll TMP0, CARG2, 1 - | or TMP1, AT, TMP0 - | beqz TMP1, >8 // Both args +-0: return 1. - |. lui TMP1, 0xffe0 - | dsll TMP1, TMP1, 32 - | sltu AT, TMP1, AT - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0; - |. xor AT, CARG1, CARG2 - | jr ra - |. sltiu CRET1, AT, 1 // Same values: return 1. - |8: - | jr ra - |. li CRET1, 1 - |9: - | jr ra - |. li CRET1, 0 - |.endif - | - |// Soft-float comparison. Equivalent to c.ult.d and c.olt.d. - |// Input: CARG1, CARG2. Output: CRET1. Temporaries: AT, TMP0, TMP1, CRET2. - |->vm_sfcmpult: - |.if not FPU - | b >1 - |. li CRET2, 1 - |.endif - | - |->vm_sfcmpolt: - |.if not FPU - | li CRET2, 0 - |1: - | dsll AT, CARG1, 1 - | dsll TMP0, CARG2, 1 - | or TMP1, AT, TMP0 - | beqz TMP1, >8 // Both args +-0: return 0. - |. lui TMP1, 0xffe0 - | dsll TMP1, TMP1, 32 - | sltu AT, TMP1, AT - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0 or 1; - |. and AT, CARG1, CARG2 - | bltz AT, >5 // Both args negative? - |. nop - | jr ra - |. slt CRET1, CARG1, CARG2 - |5: // Swap conditions if both operands are negative. - | jr ra - |. slt CRET1, CARG2, CARG1 - |8: - | jr ra - |. li CRET1, 0 - |9: - | jr ra - |. move CRET1, CRET2 - |.endif - | - |->vm_sfcmpogt: - |.if not FPU - | dsll AT, CARG2, 1 - | dsll TMP0, CARG1, 1 - | or TMP1, AT, TMP0 - | beqz TMP1, >8 // Both args +-0: return 0. - |. lui TMP1, 0xffe0 - | dsll TMP1, TMP1, 32 - | sltu AT, TMP1, AT - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0 or 1; - |. and AT, CARG2, CARG1 - | bltz AT, >5 // Both args negative? - |. nop - | jr ra - |. slt CRET1, CARG2, CARG1 - |5: // Swap conditions if both operands are negative. - | jr ra - |. slt CRET1, CARG1, CARG2 - |8: - | jr ra - |. li CRET1, 0 - |9: - | jr ra - |. li CRET1, 0 - |.endif - | - |// Soft-float comparison. Equivalent to c.ole.d a, b or c.ole.d b, a. - |// Input: CARG1, CARG2, TMP3. Output: CRET1. Temporaries: AT, TMP0, TMP1. - |->vm_sfcmpolex: - |.if not FPU - | dsll AT, CARG1, 1 - | dsll TMP0, CARG2, 1 - | or TMP1, AT, TMP0 - | beqz TMP1, >8 // Both args +-0: return 1. - |. lui TMP1, 0xffe0 - | dsll TMP1, TMP1, 32 - | sltu AT, TMP1, AT - | sltu TMP0, TMP1, TMP0 - | or TMP1, AT, TMP0 - | bnez TMP1, >9 // Either arg is NaN: return 0; - |. and AT, CARG1, CARG2 - | xor AT, AT, TMP3 - | bltz AT, >5 // Both args negative? - |. nop - | jr ra - |. slt CRET1, CARG2, CARG1 - |5: // Swap conditions if both operands are negative. - | jr ra - |. slt CRET1, CARG1, CARG2 - |8: - | jr ra - |. li CRET1, 1 - |9: - | jr ra - |. li CRET1, 0 - |.endif - | - |.macro sfmin_max, name, fpcall - |->vm_sf .. name: - |.if JIT and not FPU - | move TMP2, ra - | bal ->fpcall - |. nop - | move ra, TMP2 - | move TMP0, CRET1 - | move CRET1, CARG1 - |.if MIPSR6 - | selnez CRET1, CRET1, TMP0 - | seleqz TMP0, CARG2, TMP0 - | jr ra - |. or CRET1, CRET1, TMP0 - |.else - | jr ra - |. movz CRET1, CARG2, TMP0 - |.endif - |.endif - |.endmacro - | - | sfmin_max min, vm_sfcmpolt - | sfmin_max max, vm_sfcmpogt - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |.define NEXT_TAB, TAB:CARG1 - |.define NEXT_IDX, CARG2 - |.define NEXT_ASIZE, CARG3 - |.define NEXT_NIL, CARG4 - |.define NEXT_TMP0, r12 - |.define NEXT_TMP1, r13 - |.define NEXT_TMP2, r14 - |.define NEXT_RES_VK, CRET1 - |.define NEXT_RES_IDX, CRET2 - |.define NEXT_RES_PTR, sp - |.define NEXT_RES_VAL, 0(sp) - |.define NEXT_RES_KEY, 8(sp) - | - |// TValue *lj_vm_next(GCtab *t, uint32_t idx) - |// Next idx returned in CRET2. - |->vm_next: - |.if JIT and ENDIAN_LE - | lw NEXT_ASIZE, NEXT_TAB->asize - | ld NEXT_TMP0, NEXT_TAB->array - | li NEXT_NIL, LJ_TNIL - |1: // Traverse array part. - | sltu AT, NEXT_IDX, NEXT_ASIZE - | sll NEXT_TMP1, NEXT_IDX, 3 - | beqz AT, >5 - |. daddu NEXT_TMP1, NEXT_TMP0, NEXT_TMP1 - | li AT, LJ_TISNUM - | ld NEXT_TMP2, 0(NEXT_TMP1) - | dsll AT, AT, 47 - | or NEXT_TMP1, NEXT_IDX, AT - | beq NEXT_TMP2, NEXT_NIL, <1 - |. addiu NEXT_IDX, NEXT_IDX, 1 - | sd NEXT_TMP2, NEXT_RES_VAL - | sd NEXT_TMP1, NEXT_RES_KEY - | move NEXT_RES_VK, NEXT_RES_PTR - | jr ra - |. move NEXT_RES_IDX, NEXT_IDX - | - |5: // Traverse hash part. - | subu NEXT_RES_IDX, NEXT_IDX, NEXT_ASIZE - | ld NODE:NEXT_RES_VK, NEXT_TAB->node - | sll NEXT_TMP2, NEXT_RES_IDX, 5 - | lw NEXT_TMP0, NEXT_TAB->hmask - | sll AT, NEXT_RES_IDX, 3 - | subu AT, NEXT_TMP2, AT - | daddu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, AT - |6: - | sltu AT, NEXT_TMP0, NEXT_RES_IDX - | bnez AT, >8 - |. nop - | ld NEXT_TMP2, NODE:NEXT_RES_VK->val - | bne NEXT_TMP2, NEXT_NIL, >9 - |. addiu NEXT_RES_IDX, NEXT_RES_IDX, 1 - | // Skip holes in hash part. - | b <6 - |. daddiu NODE:NEXT_RES_VK, NODE:NEXT_RES_VK, sizeof(Node) - | - |8: // End of iteration. Set the key to nil (not the value). - | sd NEXT_NIL, NEXT_RES_KEY - | move NEXT_RES_VK, NEXT_RES_PTR - |9: - | jr ra - |. addu NEXT_RES_IDX, NEXT_RES_IDX, NEXT_ASIZE - |.endif - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in r1, g in r2. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs - | ld CTSTATE, GL:r2->ctype_state - | daddiu DISPATCH, r2, GG_G2DISP - | load_got lj_ccallback_enter - | sw r1, CTSTATE->cb.slot - | sd CARG1, CTSTATE->cb.gpr[0] - | .FPU sdc1 FARG1, CTSTATE->cb.fpr[0] - | sd CARG2, CTSTATE->cb.gpr[1] - | .FPU sdc1 FARG2, CTSTATE->cb.fpr[1] - | sd CARG3, CTSTATE->cb.gpr[2] - | .FPU sdc1 FARG3, CTSTATE->cb.fpr[2] - | sd CARG4, CTSTATE->cb.gpr[3] - | .FPU sdc1 FARG4, CTSTATE->cb.fpr[3] - | sd CARG5, CTSTATE->cb.gpr[4] - | .FPU sdc1 FARG5, CTSTATE->cb.fpr[4] - | sd CARG6, CTSTATE->cb.gpr[5] - | .FPU sdc1 FARG6, CTSTATE->cb.fpr[5] - | sd CARG7, CTSTATE->cb.gpr[6] - | .FPU sdc1 FARG7, CTSTATE->cb.fpr[6] - | sd CARG8, CTSTATE->cb.gpr[7] - | .FPU sdc1 FARG8, CTSTATE->cb.fpr[7] - | daddiu TMP0, sp, CFRAME_SPACE - | sd TMP0, CTSTATE->cb.stack - | sd r0, SAVE_PC // Any value outside of bytecode is ok. - | move CARG2, sp - | call_intern lj_ccallback_enter // (CTState *cts, void *cf) - |. move CARG1, CTSTATE - | // Returns lua_State *. - | ld BASE, L:CRET1->base - | ld RC, L:CRET1->top - | move L, CRET1 - | .FPU lui TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | ld LFUNC:RB, FRAME_FUNC(BASE) - | .FPU mtc1 TMP3, TOBIT - | li TISNIL, LJ_TNIL - | li TISNUM, LJ_TISNUM - | li_vmstate INTERP - | subu RC, RC, BASE - | cleartp LFUNC:RB - | st_vmstate - | .FPU cvt.d.s TOBIT, TOBIT - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | load_got lj_ccallback_leave - | ld CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH) - | sd BASE, L->base - | sd RB, L->top - | sd L, CTSTATE->L - | move CARG2, RA - | call_intern lj_ccallback_leave // (CTState *cts, TValue *o) - |. move CARG1, CTSTATE - | .FPU ldc1 FRET1, CTSTATE->cb.fpr[0] - | ld CRET1, CTSTATE->cb.gpr[0] - | .FPU ldc1 FRET2, CTSTATE->cb.fpr[1] - | b ->vm_leave_unw - |. ld CRET2, CTSTATE->cb.gpr[1] - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, CARG1 - | lw TMP1, CCSTATE->spadj - | lbu CARG2, CCSTATE->nsp - | move TMP2, sp - | dsubu sp, sp, TMP1 - | sd ra, -8(TMP2) - | sll CARG2, CARG2, 3 - | sd r16, -16(TMP2) - | sd CCSTATE, -24(TMP2) - | move r16, TMP2 - | daddiu TMP1, CCSTATE, offsetof(CCallState, stack) - | move TMP2, sp - | beqz CARG2, >2 - |. daddu TMP3, TMP1, CARG2 - |1: - | ld TMP0, 0(TMP1) - | daddiu TMP1, TMP1, 8 - | sltu AT, TMP1, TMP3 - | sd TMP0, 0(TMP2) - | bnez AT, <1 - |. daddiu TMP2, TMP2, 8 - |2: - | ld CFUNCADDR, CCSTATE->func - | .FPU ldc1 FARG1, CCSTATE->gpr[0] - | ld CARG2, CCSTATE->gpr[1] - | .FPU ldc1 FARG2, CCSTATE->gpr[1] - | ld CARG3, CCSTATE->gpr[2] - | .FPU ldc1 FARG3, CCSTATE->gpr[2] - | ld CARG4, CCSTATE->gpr[3] - | .FPU ldc1 FARG4, CCSTATE->gpr[3] - | ld CARG5, CCSTATE->gpr[4] - | .FPU ldc1 FARG5, CCSTATE->gpr[4] - | ld CARG6, CCSTATE->gpr[5] - | .FPU ldc1 FARG6, CCSTATE->gpr[5] - | ld CARG7, CCSTATE->gpr[6] - | .FPU ldc1 FARG7, CCSTATE->gpr[6] - | ld CARG8, CCSTATE->gpr[7] - | .FPU ldc1 FARG8, CCSTATE->gpr[7] - | jalr CFUNCADDR - |. ld CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1. - | ld CCSTATE:TMP1, -24(r16) - | ld TMP2, -16(r16) - | ld ra, -8(r16) - | sd CRET1, CCSTATE:TMP1->gpr[0] - | sd CRET2, CCSTATE:TMP1->gpr[1] - |.if FPU - | sdc1 FRET1, CCSTATE:TMP1->fpr[0] - | sdc1 FRET2, CCSTATE:TMP1->fpr[1] - |.else - | sd CARG1, CCSTATE:TMP1->gpr[2] // 2nd FP struct field for soft-float. - |.endif - | move sp, r16 - | jr ra - |. move r16, TMP2 - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RD = src2*8, JMP with RD = target - |.macro bc_comp, FRA, FRD, ARGRA, ARGRD, movop, fmovop, fcomp, sfcomp - | daddu RA, BASE, RA - | daddu RD, BASE, RD - | ld ARGRA, 0(RA) - | ld ARGRD, 0(RD) - | lhu TMP2, OFS_RD(PC) - | gettp CARG3, ARGRA - | gettp CARG4, ARGRD - | bne CARG3, TISNUM, >2 - |. daddiu PC, PC, 4 - | bne CARG4, TISNUM, >5 - |. decode_RD4b TMP2 - | sextw ARGRA, ARGRA - | sextw ARGRD, ARGRD - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | slt AT, CARG1, CARG2 - | addu TMP2, TMP2, TMP3 - |.if MIPSR6 - | movop TMP2, TMP2, AT - |.else - | movop TMP2, r0, AT - |.endif - |1: - | daddu PC, PC, TMP2 - | ins_next - | - |2: // RA is not an integer. - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->vmeta_comp - |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sltiu AT, CARG4, LJ_TISNUM - | beqz AT, >4 - |. decode_RD4b TMP2 - |.if FPU - | ldc1 FRA, 0(RA) - | ldc1 FRD, 0(RD) - |.endif - |3: // RA and RD are both numbers. - |.if FPU - |.if MIPSR6 - | fcomp FTMP0, FTMP0, FTMP2 - | addu TMP2, TMP2, TMP3 - | mfc1 TMP3, FTMP0 - | b <1 - |. fmovop TMP2, TMP2, TMP3 - |.else - | fcomp FTMP0, FTMP2 - | addu TMP2, TMP2, TMP3 - | b <1 - |. fmovop TMP2, r0 - |.endif - |.else - | bal sfcomp - |. addu TMP2, TMP2, TMP3 - | b <1 - |.if MIPSR6 - |. movop TMP2, TMP2, CRET1 - |.else - |. movop TMP2, r0, CRET1 - |.endif - |.endif - | - |4: // RA is a number, RD is not a number. - | bne CARG4, TISNUM, ->vmeta_comp - | // RA is a number, RD is an integer. Convert RD to a number. - |.if FPU - |. lwc1 FRD, LO(RD) - | ldc1 FRA, 0(RA) - | b <3 - |. cvt.d.w FRD, FRD - |.else - |.if "ARGRD" == "CARG1" - |. sextw CARG1, CARG1 - | bal ->vm_sfi2d_1 - |. nop - |.else - |. sextw CARG2, CARG2 - | bal ->vm_sfi2d_2 - |. nop - |.endif - | b <3 - |. nop - |.endif - | - |5: // RA is an integer, RD is not an integer - | sltiu AT, CARG4, LJ_TISNUM - | beqz AT, ->vmeta_comp - |. lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | // RA is an integer, RD is a number. Convert RA to a number. - |.if FPU - | lwc1 FRA, LO(RA) - | ldc1 FRD, 0(RD) - | b <3 - | cvt.d.w FRA, FRA - |.else - |.if "ARGRA" == "CARG1" - | bal ->vm_sfi2d_1 - |. sextw CARG1, CARG1 - |.else - | bal ->vm_sfi2d_2 - |. sextw CARG2, CARG2 - |.endif - | b <3 - |. nop - |.endif - |.endmacro - | - |.if MIPSR6 - if (op == BC_ISLT) { - | bc_comp FTMP0, FTMP2, CARG1, CARG2, selnez, selnez, cmp.lt.d, ->vm_sfcmpolt - } else if (op == BC_ISGE) { - | bc_comp FTMP0, FTMP2, CARG1, CARG2, seleqz, seleqz, cmp.lt.d, ->vm_sfcmpolt - } else if (op == BC_ISLE) { - | bc_comp FTMP2, FTMP0, CARG2, CARG1, seleqz, seleqz, cmp.ult.d, ->vm_sfcmpult - } else { - | bc_comp FTMP2, FTMP0, CARG2, CARG1, selnez, selnez, cmp.ult.d, ->vm_sfcmpult - } - |.else - if (op == BC_ISLT) { - | bc_comp FTMP0, FTMP2, CARG1, CARG2, movz, movf, c.olt.d, ->vm_sfcmpolt - } else if (op == BC_ISGE) { - | bc_comp FTMP0, FTMP2, CARG1, CARG2, movn, movt, c.olt.d, ->vm_sfcmpolt - } else if (op == BC_ISLE) { - | bc_comp FTMP2, FTMP0, CARG2, CARG1, movn, movt, c.ult.d, ->vm_sfcmpult - } else { - | bc_comp FTMP2, FTMP0, CARG2, CARG1, movz, movf, c.ult.d, ->vm_sfcmpult - } - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RD = src2*8, JMP with RD = target - | daddu RA, BASE, RA - | daddiu PC, PC, 4 - | daddu RD, BASE, RD - | ld CARG1, 0(RA) - | lhu TMP2, -4+OFS_RD(PC) - | ld CARG2, 0(RD) - | gettp CARG3, CARG1 - | gettp CARG4, CARG2 - | sltu AT, TISNUM, CARG3 - | sltu TMP1, TISNUM, CARG4 - | or AT, AT, TMP1 - if (vk) { - | beqz AT, ->BC_ISEQN_Z - } else { - | beqz AT, ->BC_ISNEN_Z - } - | // Either or both types are not numbers. - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - |.if FFI - |. li AT, LJ_TCDATA - | beq CARG3, AT, ->vmeta_equal_cd - |.endif - | decode_RD4b TMP2 - |.if FFI - | beq CARG4, AT, ->vmeta_equal_cd - |. nop - |.endif - | bne CARG1, CARG2, >2 - |. addu TMP2, TMP2, TMP3 - | // Tag and value are equal. - if (vk) { - |->BC_ISEQV_Z: - | daddu PC, PC, TMP2 - } - |1: - | ins_next - | - |2: // Check if the tags are the same and it's a table or userdata. - | xor AT, CARG3, CARG4 // Same type? - | sltiu TMP0, CARG3, LJ_TISTABUD+1 // Table or userdata? - |.if MIPSR6 - | seleqz TMP0, TMP0, AT - |.else - | movn TMP0, r0, AT - |.endif - if (vk) { - | beqz TMP0, <1 - } else { - | beqz TMP0, ->BC_ISEQV_Z // Reuse code from opposite instruction. - } - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - |. cleartp TAB:TMP1, CARG1 - | ld TAB:TMP3, TAB:TMP1->metatable - if (vk) { - | beqz TAB:TMP3, <1 // No metatable? - |. nop - | lbu TMP3, TAB:TMP3->nomm - | andi TMP3, TMP3, 1<1 // Or 'no __eq' flag set? - } else { - | beqz TAB:TMP3,->BC_ISEQV_Z // No metatable? - |. nop - | lbu TMP3, TAB:TMP3->nomm - | andi TMP3, TMP3, 1<BC_ISEQV_Z // Or 'no __eq' flag set? - } - |. nop - | b ->vmeta_equal // Handle __eq metamethod. - |. li TMP0, 1-vk // ne = 0 or 1. - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RD = str_const*8 (~), JMP with RD = target - | daddu RA, BASE, RA - | daddiu PC, PC, 4 - | ld CARG1, 0(RA) - | dsubu RD, KBASE, RD - | lhu TMP2, -4+OFS_RD(PC) - | ld CARG2, -8(RD) // KBASE-8-str_const*8 - |.if FFI - | gettp TMP0, CARG1 - | li AT, LJ_TCDATA - |.endif - | li TMP1, LJ_TSTR - | decode_RD4b TMP2 - |.if FFI - | beq TMP0, AT, ->vmeta_equal_cd - |.endif - |. settp CARG2, TMP1 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | xor TMP1, CARG1, CARG2 - | addu TMP2, TMP2, TMP3 - |.if MIPSR6 - if (vk) { - | seleqz TMP2, TMP2, TMP1 - } else { - | selnez TMP2, TMP2, TMP1 - } - |.else - if (vk) { - | movn TMP2, r0, TMP1 - } else { - | movz TMP2, r0, TMP1 - } - |.endif - | daddu PC, PC, TMP2 - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RD = num_const*8, JMP with RD = target - | daddu RA, BASE, RA - | daddu RD, KBASE, RD - | ld CARG1, 0(RA) - | ld CARG2, 0(RD) - | lhu TMP2, OFS_RD(PC) - | gettp CARG3, CARG1 - | gettp CARG4, CARG2 - | daddiu PC, PC, 4 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | bne CARG3, TISNUM, >3 - |. decode_RD4b TMP2 - | bne CARG4, TISNUM, >6 - |. addu TMP2, TMP2, TMP3 - | xor AT, CARG1, CARG2 - |.if MIPSR6 - if (vk) { - | seleqz TMP2, TMP2, AT - |1: - | daddu PC, PC, TMP2 - |2: - } else { - | selnez TMP2, TMP2, AT - |1: - |2: - | daddu PC, PC, TMP2 - } - |.else - if (vk) { - | movn TMP2, r0, AT - |1: - | daddu PC, PC, TMP2 - |2: - } else { - | movz TMP2, r0, AT - |1: - |2: - | daddu PC, PC, TMP2 - } - |.endif - | ins_next - | - |3: // RA is not an integer. - | sltu AT, CARG3, TISNUM - |.if FFI - | beqz AT, >8 - |.else - | beqz AT, <2 - |.endif - |. addu TMP2, TMP2, TMP3 - | sltu AT, CARG4, TISNUM - |.if FPU - | ldc1 FTMP0, 0(RA) - | ldc1 FTMP2, 0(RD) - |.endif - | beqz AT, >5 - |. nop - |4: // RA and RD are both numbers. - |.if FPU - |.if MIPSR6 - | cmp.eq.d FTMP0, FTMP0, FTMP2 - | dmfc1 TMP1, FTMP0 - | b <1 - if (vk) { - |. selnez TMP2, TMP2, TMP1 - } else { - |. seleqz TMP2, TMP2, TMP1 - } - |.else - | c.eq.d FTMP0, FTMP2 - | b <1 - if (vk) { - |. movf TMP2, r0 - } else { - |. movt TMP2, r0 - } - |.endif - |.else - | bal ->vm_sfcmpeq - |. nop - | b <1 - |.if MIPSR6 - if (vk) { - |. selnez TMP2, TMP2, CRET1 - } else { - |. seleqz TMP2, TMP2, CRET1 - } - |.else - if (vk) { - |. movz TMP2, r0, CRET1 - } else { - |. movn TMP2, r0, CRET1 - } - |.endif - |.endif - | - |5: // RA is a number, RD is not a number. - |.if FFI - | bne CARG4, TISNUM, >9 - |.else - | bne CARG4, TISNUM, <2 - |.endif - | // RA is a number, RD is an integer. Convert RD to a number. - |.if FPU - |. lwc1 FTMP2, LO(RD) - | b <4 - |. cvt.d.w FTMP2, FTMP2 - |.else - |. sextw CARG2, CARG2 - | bal ->vm_sfi2d_2 - |. nop - | b <4 - |. nop - |.endif - | - |6: // RA is an integer, RD is not an integer - | sltu AT, CARG4, TISNUM - |.if FFI - | beqz AT, >9 - |.else - | beqz AT, <2 - |.endif - | // RA is an integer, RD is a number. Convert RA to a number. - |.if FPU - |. lwc1 FTMP0, LO(RA) - | ldc1 FTMP2, 0(RD) - | b <4 - | cvt.d.w FTMP0, FTMP0 - |.else - |. sextw CARG1, CARG1 - | bal ->vm_sfi2d_1 - |. nop - | b <4 - |. nop - |.endif - | - |.if FFI - |8: - | li AT, LJ_TCDATA - | bne CARG3, AT, <2 - |. nop - | b ->vmeta_equal_cd - |. nop - |9: - | li AT, LJ_TCDATA - | bne CARG4, AT, <2 - |. nop - | b ->vmeta_equal_cd - |. nop - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target - | daddu RA, BASE, RA - | srl TMP1, RD, 3 - | ld TMP0, 0(RA) - | lhu TMP2, OFS_RD(PC) - | not TMP1, TMP1 - | gettp TMP0, TMP0 - | daddiu PC, PC, 4 - |.if FFI - | li AT, LJ_TCDATA - | beq TMP0, AT, ->vmeta_equal_cd - |.endif - |. xor TMP0, TMP0, TMP1 - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - |.if MIPSR6 - if (vk) { - | seleqz TMP2, TMP2, TMP0 - } else { - | selnez TMP2, TMP2, TMP0 - } - |.else - if (vk) { - | movn TMP2, r0, TMP0 - } else { - | movz TMP2, r0, TMP0 - } - |.endif - | daddu PC, PC, TMP2 - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RD = src*8, JMP with RD = target - | daddu RD, BASE, RD - | lhu TMP2, OFS_RD(PC) - | ld TMP0, 0(RD) - | daddiu PC, PC, 4 - | gettp TMP0, TMP0 - | sltiu TMP0, TMP0, LJ_TISTRUECOND - if (op == BC_IST || op == BC_ISF) { - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - |.if MIPSR6 - if (op == BC_IST) { - | selnez TMP2, TMP2, TMP0; - } else { - | seleqz TMP2, TMP2, TMP0; - } - |.else - if (op == BC_IST) { - | movz TMP2, r0, TMP0 - } else { - | movn TMP2, r0, TMP0 - } - |.endif - | daddu PC, PC, TMP2 - } else { - | ld CRET1, 0(RD) - if (op == BC_ISTC) { - | beqz TMP0, >1 - } else { - | bnez TMP0, >1 - } - |. daddu RA, BASE, RA - | decode_RD4b TMP2 - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | addu TMP2, TMP2, TMP3 - | sd CRET1, 0(RA) - | daddu PC, PC, TMP2 - |1: - } - | ins_next - break; - - case BC_ISTYPE: - | // RA = src*8, RD = -type*8 - | daddu TMP2, BASE, RA - | srl TMP1, RD, 3 - | ld TMP0, 0(TMP2) - | ins_next1 - | gettp TMP0, TMP0 - | daddu AT, TMP0, TMP1 - | bnez AT, ->vmeta_istype - |. ins_next2 - break; - case BC_ISNUM: - | // RA = src*8, RD = -(TISNUM-1)*8 - | daddu TMP2, BASE, RA - | ld TMP0, 0(TMP2) - | ins_next1 - | checknum TMP0, ->vmeta_istype - |. ins_next2 - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RD = src*8 - | daddu RD, BASE, RD - | daddu RA, BASE, RA - | ld CRET1, 0(RD) - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - break; - case BC_NOT: - | // RA = dst*8, RD = src*8 - | daddu RD, BASE, RD - | daddu RA, BASE, RA - | ld TMP0, 0(RD) - | li AT, LJ_TTRUE - | gettp TMP0, TMP0 - | sltu TMP0, AT, TMP0 - | addiu TMP0, TMP0, 1 - | dsll TMP0, TMP0, 47 - | not TMP0, TMP0 - | ins_next1 - | sd TMP0, 0(RA) - | ins_next2 - break; - case BC_UNM: - | // RA = dst*8, RD = src*8 - | daddu RB, BASE, RD - | ld CARG1, 0(RB) - | daddu RA, BASE, RA - | gettp CARG3, CARG1 - | bne CARG3, TISNUM, >2 - |. lui TMP1, 0x8000 - | sextw CARG1, CARG1 - | beq CARG1, TMP1, ->vmeta_unm // Meta handler deals with -2^31. - |. negu CARG1, CARG1 - | zextw CARG1, CARG1 - | settp CARG1, TISNUM - |1: - | ins_next1 - | sd CARG1, 0(RA) - | ins_next2 - |2: - | sltiu AT, CARG3, LJ_TISNUM - | beqz AT, ->vmeta_unm - |. dsll TMP1, TMP1, 32 - | b <1 - |. xor CARG1, CARG1, TMP1 - break; - case BC_LEN: - | // RA = dst*8, RD = src*8 - | daddu CARG2, BASE, RD - | daddu RA, BASE, RA - | ld TMP0, 0(CARG2) - | gettp TMP1, TMP0 - | daddiu AT, TMP1, -LJ_TSTR - | bnez AT, >2 - |. cleartp STR:CARG1, TMP0 - | lw CRET1, STR:CARG1->len - |1: - | settp CRET1, TISNUM - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - |2: - | daddiu AT, TMP1, -LJ_TTAB - | bnez AT, ->vmeta_len - |. nop -#if LJ_52 - | ld TAB:TMP2, TAB:CARG1->metatable - | bnez TAB:TMP2, >9 - |. nop - |3: -#endif - |->BC_LEN_Z: - | load_got lj_tab_len - | call_intern lj_tab_len // (GCtab *t) - |. nop - | // Returns uint32_t (but less than 2^31). - | b <1 - |. nop -#if LJ_52 - |9: - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_len - |. nop -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro fpmod, a, b, c - | bal ->vm_floor // floor(b/c) - |. div.d FARG1, b, c - | mul.d a, FRET1, c - | sub.d a, b, a // b - floor(b/c)*c - |.endmacro - - |.macro sfpmod - | daddiu sp, sp, -16 - | - | load_got __divdf3 - | sd CARG1, 0(sp) - | call_extern - |. sd CARG2, 8(sp) - | - | load_got floor - | call_extern - |. move CARG1, CRET1 - | - | load_got __muldf3 - | move CARG1, CRET1 - | call_extern - |. ld CARG2, 8(sp) - | - | load_got __subdf3 - | ld CARG1, 0(sp) - | call_extern - |. move CARG2, CRET1 - | - | daddiu sp, sp, 16 - |.endmacro - - |.macro ins_arithpre, label - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||switch (vk) { - ||case 0: - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | // RA = dst*8, RB = src1*8, RC = num_const*8 - | daddu RB, BASE, RB - |.if "label" ~= "none" - | b label - |.endif - |. daddu RC, KBASE, RC - || break; - ||case 1: - | decode_RB8a RC, INS - | decode_RB8b RC - | decode_RDtoRC8 RB, RD - | // RA = dst*8, RB = num_const*8, RC = src1*8 - | daddu RC, BASE, RC - |.if "label" ~= "none" - | b label - |.endif - |. daddu RB, KBASE, RB - || break; - ||default: - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | // RA = dst*8, RB = src1*8, RC = src2*8 - | daddu RB, BASE, RB - |.if "label" ~= "none" - | b label - |.endif - |. daddu RC, BASE, RC - || break; - ||} - |.endmacro - | - |.macro ins_arith, intins, fpins, fpcall, label - | ins_arithpre none - | - |.if "label" ~= "none" - |label: - |.endif - | - |// Used in 5. - | ld CARG1, 0(RB) - | ld CARG2, 0(RC) - | gettp TMP0, CARG1 - | gettp TMP1, CARG2 - | - |.if "intins" ~= "div" - | - | // Check for two integers. - | sextw CARG3, CARG1 - | bne TMP0, TISNUM, >5 - |. sextw CARG4, CARG2 - | bne TMP1, TISNUM, >5 - | - |.if "intins" == "addu" - |. intins CRET1, CARG3, CARG4 - | xor TMP1, CRET1, CARG3 // ((y^a) & (y^b)) < 0: overflow. - | xor TMP2, CRET1, CARG4 - | and TMP1, TMP1, TMP2 - | bltz TMP1, ->vmeta_arith - |. daddu RA, BASE, RA - |.elif "intins" == "subu" - |. intins CRET1, CARG3, CARG4 - | xor TMP1, CRET1, CARG3 // ((y^a) & (a^b)) < 0: overflow. - | xor TMP2, CARG3, CARG4 - | and TMP1, TMP1, TMP2 - | bltz TMP1, ->vmeta_arith - |. daddu RA, BASE, RA - |.elif "intins" == "mult" - |.if MIPSR6 - |. nop - | mul CRET1, CARG3, CARG4 - | muh TMP2, CARG3, CARG4 - |.else - |. intins CARG3, CARG4 - | mflo CRET1 - | mfhi TMP2 - |.endif - | sra TMP1, CRET1, 31 - | bne TMP1, TMP2, ->vmeta_arith - |. daddu RA, BASE, RA - |.else - |. load_got lj_vm_modi - | beqz CARG4, ->vmeta_arith - |. daddu RA, BASE, RA - | move CARG1, CARG3 - | call_extern - |. move CARG2, CARG4 - |.endif - | - | zextw CRET1, CRET1 - | settp CRET1, TISNUM - | ins_next1 - | sd CRET1, 0(RA) - |3: - | ins_next2 - | - |.endif - | - |5: // Check for two numbers. - | .FPU ldc1 FTMP0, 0(RB) - | sltu AT, TMP0, TISNUM - | sltu TMP0, TMP1, TISNUM - | .FPU ldc1 FTMP2, 0(RC) - | and AT, AT, TMP0 - | beqz AT, ->vmeta_arith - |. daddu RA, BASE, RA - | - |.if FPU - | fpins FRET1, FTMP0, FTMP2 - |.elif "fpcall" == "sfpmod" - | sfpmod - |.else - | load_got fpcall - | call_extern - |. nop - |.endif - | - | ins_next1 - |.if "intins" ~= "div" - | b <3 - |.endif - |.if FPU - |. sdc1 FRET1, 0(RA) - |.else - |. sd CRET1, 0(RA) - |.endif - |.if "intins" == "div" - | ins_next2 - |.endif - | - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith addu, add.d, __adddf3, none - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith subu, sub.d, __subdf3, none - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith mult, mul.d, __muldf3, none - break; - case BC_DIVVN: - | ins_arith div, div.d, __divdf3, ->BC_DIVVN_Z - break; - case BC_DIVNV: case BC_DIVVV: - | ins_arithpre ->BC_DIVVN_Z - break; - case BC_MODVN: - | ins_arith modi, fpmod, sfpmod, ->BC_MODVN_Z - break; - case BC_MODNV: case BC_MODVV: - | ins_arithpre ->BC_MODVN_Z - break; - case BC_POW: - | ins_arithpre none - | ld CARG1, 0(RB) - | ld CARG2, 0(RC) - | gettp TMP0, CARG1 - | gettp TMP1, CARG2 - | sltiu TMP0, TMP0, LJ_TISNUM - | sltiu TMP1, TMP1, LJ_TISNUM - | and AT, TMP0, TMP1 - | load_got pow - | beqz AT, ->vmeta_arith - |. daddu RA, BASE, RA - |.if FPU - | ldc1 FARG1, 0(RB) - | ldc1 FARG2, 0(RC) - |.endif - | call_extern - |. nop - | ins_next1 - |.if FPU - | sdc1 FRET1, 0(RA) - |.else - | sd CRET1, 0(RA) - |.endif - | ins_next2 - break; - - case BC_CAT: - | // RA = dst*8, RB = src_start*8, RC = src_end*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | dsubu CARG3, RC, RB - | sd BASE, L->base - | daddu CARG2, BASE, RC - | move MULTRES, RB - |->BC_CAT_Z: - | load_got lj_meta_cat - | srl CARG3, CARG3, 3 - | sd PC, SAVE_PC - | call_intern lj_meta_cat // (lua_State *L, TValue *top, int left) - |. move CARG1, L - | // Returns NULL (finished) or TValue * (metamethod). - | bnez CRET1, ->vmeta_binop - |. ld BASE, L->base - | daddu RB, BASE, MULTRES - | ld CRET1, 0(RB) - | daddu RA, BASE, RA - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RD = str_const*8 (~) - | dsubu TMP1, KBASE, RD - | ins_next1 - | li TMP2, LJ_TSTR - | ld TMP0, -8(TMP1) // KBASE-8-str_const*8 - | daddu RA, BASE, RA - | settp TMP0, TMP2 - | sd TMP0, 0(RA) - | ins_next2 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RD = cdata_const*8 (~) - | dsubu TMP1, KBASE, RD - | ins_next1 - | ld TMP0, -8(TMP1) // KBASE-8-cdata_const*8 - | li TMP2, LJ_TCDATA - | daddu RA, BASE, RA - | settp TMP0, TMP2 - | sd TMP0, 0(RA) - | ins_next2 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, RD = int16_literal*8 - | sra RD, INS, 16 - | daddu RA, BASE, RA - | zextw RD, RD - | ins_next1 - | settp RD, TISNUM - | sd RD, 0(RA) - | ins_next2 - break; - case BC_KNUM: - | // RA = dst*8, RD = num_const*8 - | daddu RD, KBASE, RD - | daddu RA, BASE, RA - | ld CRET1, 0(RD) - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - break; - case BC_KPRI: - | // RA = dst*8, RD = primitive_type*8 (~) - | daddu RA, BASE, RA - | dsll TMP0, RD, 44 - | not TMP0, TMP0 - | ins_next1 - | sd TMP0, 0(RA) - | ins_next2 - break; - case BC_KNIL: - | // RA = base*8, RD = end*8 - | daddu RA, BASE, RA - | sd TISNIL, 0(RA) - | daddiu RA, RA, 8 - | daddu RD, BASE, RD - |1: - | sd TISNIL, 0(RA) - | slt AT, RA, RD - | bnez AT, <1 - |. daddiu RA, RA, 8 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RD = uvnum*8 - | ld LFUNC:RB, FRAME_FUNC(BASE) - | daddu RA, BASE, RA - | cleartp LFUNC:RB - | daddu RD, RD, LFUNC:RB - | ld UPVAL:RB, LFUNC:RD->uvptr - | ins_next1 - | ld TMP1, UPVAL:RB->v - | ld CRET1, 0(TMP1) - | sd CRET1, 0(RA) - | ins_next2 - break; - case BC_USETV: - | // RA = uvnum*8, RD = src*8 - | ld LFUNC:RB, FRAME_FUNC(BASE) - | daddu RD, BASE, RD - | cleartp LFUNC:RB - | daddu RA, RA, LFUNC:RB - | ld UPVAL:RB, LFUNC:RA->uvptr - | ld CRET1, 0(RD) - | lbu TMP3, UPVAL:RB->marked - | ld CARG2, UPVAL:RB->v - | andi TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbu TMP0, UPVAL:RB->closed - | gettp TMP2, CRET1 - | sd CRET1, 0(CARG2) - | li AT, LJ_GC_BLACK|1 - | or TMP3, TMP3, TMP0 - | beq TMP3, AT, >2 // Upvalue is closed and black? - |. daddiu TMP2, TMP2, -(LJ_TNUMX+1) - |1: - | ins_next - | - |2: // Check if new value is collectable. - | sltiu AT, TMP2, LJ_TISGCV - (LJ_TNUMX+1) - | beqz AT, <1 // tvisgcv(v) - |. cleartp GCOBJ:CRET1, CRET1 - | lbu TMP3, GCOBJ:CRET1->gch.marked - | andi TMP3, TMP3, LJ_GC_WHITES // iswhite(v) - | beqz TMP3, <1 - |. load_got lj_gc_barrieruv - | // Crossed a write barrier. Move the barrier forward. - | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv) - |. daddiu CARG1, DISPATCH, GG_DISP2G - | b <1 - |. nop - break; - case BC_USETS: - | // RA = uvnum*8, RD = str_const*8 (~) - | ld LFUNC:RB, FRAME_FUNC(BASE) - | dsubu TMP1, KBASE, RD - | cleartp LFUNC:RB - | daddu RA, RA, LFUNC:RB - | ld UPVAL:RB, LFUNC:RA->uvptr - | ld STR:TMP1, -8(TMP1) // KBASE-8-str_const*8 - | lbu TMP2, UPVAL:RB->marked - | ld CARG2, UPVAL:RB->v - | lbu TMP3, STR:TMP1->marked - | andi AT, TMP2, LJ_GC_BLACK // isblack(uv) - | lbu TMP2, UPVAL:RB->closed - | li TMP0, LJ_TSTR - | settp TMP1, TMP0 - | bnez AT, >2 - |. sd TMP1, 0(CARG2) - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | beqz TMP2, <1 - |. andi AT, TMP3, LJ_GC_WHITES // iswhite(str) - | beqz AT, <1 - |. load_got lj_gc_barrieruv - | // Crossed a write barrier. Move the barrier forward. - | call_intern lj_gc_barrieruv // (global_State *g, TValue *tv) - |. daddiu CARG1, DISPATCH, GG_DISP2G - | b <1 - |. nop - break; - case BC_USETN: - | // RA = uvnum*8, RD = num_const*8 - | ld LFUNC:RB, FRAME_FUNC(BASE) - | daddu RD, KBASE, RD - | cleartp LFUNC:RB - | daddu RA, RA, LFUNC:RB - | ld UPVAL:RB, LFUNC:RA->uvptr - | ld CRET1, 0(RD) - | ld TMP1, UPVAL:RB->v - | ins_next1 - | sd CRET1, 0(TMP1) - | ins_next2 - break; - case BC_USETP: - | // RA = uvnum*8, RD = primitive_type*8 (~) - | ld LFUNC:RB, FRAME_FUNC(BASE) - | dsll TMP0, RD, 44 - | cleartp LFUNC:RB - | daddu RA, RA, LFUNC:RB - | not TMP0, TMP0 - | ld UPVAL:RB, LFUNC:RA->uvptr - | ins_next1 - | ld TMP1, UPVAL:RB->v - | sd TMP0, 0(TMP1) - | ins_next2 - break; - - case BC_UCLO: - | // RA = level*8, RD = target - | ld TMP2, L->openupval - | branch_RD // Do this first since RD is not saved. - | load_got lj_func_closeuv - | sd BASE, L->base - | beqz TMP2, >1 - |. move CARG1, L - | call_intern lj_func_closeuv // (lua_State *L, TValue *level) - |. daddu CARG2, BASE, RA - | ld BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype) - | load_got lj_func_newL_gc - | dsubu TMP1, KBASE, RD - | ld CARG3, FRAME_FUNC(BASE) - | ld CARG2, -8(TMP1) // KBASE-8-tab_const*8 - | sd BASE, L->base - | sd PC, SAVE_PC - | cleartp CARG3 - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | call_intern lj_func_newL_gc - |. move CARG1, L - | // Returns GCfuncL *. - | li TMP0, LJ_TFUNC - | ld BASE, L->base - | ins_next1 - | settp CRET1, TMP0 - | daddu RA, BASE, RA - | sd CRET1, 0(RA) - | ins_next2 - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~) - | ld TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | ld TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | sd BASE, L->base - | sd PC, SAVE_PC - | sltu AT, TMP0, TMP1 - | beqz AT, >5 - |1: - if (op == BC_TNEW) { - | load_got lj_tab_new - | srl CARG2, RD, 3 - | andi CARG2, CARG2, 0x7ff - | li TMP0, 0x801 - | addiu AT, CARG2, -0x7ff - | srl CARG3, RD, 14 - |.if MIPSR6 - | seleqz TMP0, TMP0, AT - | selnez CARG2, CARG2, AT - | or CARG2, CARG2, TMP0 - |.else - | movz CARG2, TMP0, AT - |.endif - | // (lua_State *L, int32_t asize, uint32_t hbits) - | call_intern lj_tab_new - |. move CARG1, L - | // Returns Table *. - } else { - | load_got lj_tab_dup - | dsubu TMP1, KBASE, RD - | move CARG1, L - | call_intern lj_tab_dup // (lua_State *L, Table *kt) - |. ld CARG2, -8(TMP1) // KBASE-8-str_const*8 - | // Returns Table *. - } - | li TMP0, LJ_TTAB - | ld BASE, L->base - | ins_next1 - | daddu RA, BASE, RA - | settp CRET1, TMP0 - | sd CRET1, 0(RA) - | ins_next2 - |5: - | load_got lj_gc_step_fixtop - | move MULTRES, RD - | call_intern lj_gc_step_fixtop // (lua_State *L) - |. move CARG1, L - | b <1 - |. move RD, MULTRES - break; - - case BC_GGET: - | // RA = dst*8, RD = str_const*8 (~) - case BC_GSET: - | // RA = src*8, RD = str_const*8 (~) - | ld LFUNC:TMP2, FRAME_FUNC(BASE) - | dsubu TMP1, KBASE, RD - | ld STR:RC, -8(TMP1) // KBASE-8-str_const*8 - | cleartp LFUNC:TMP2 - | ld TAB:RB, LFUNC:TMP2->env - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - |. daddu RA, BASE, RA - break; - - case BC_TGETV: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | daddu CARG2, BASE, RB - | daddu CARG3, BASE, RC - | ld TAB:RB, 0(CARG2) - | ld TMP2, 0(CARG3) - | daddu RA, BASE, RA - | checktab TAB:RB, ->vmeta_tgetv - | gettp TMP3, TMP2 - | bne TMP3, TISNUM, >5 // Integer key? - |. lw TMP0, TAB:RB->asize - | sextw TMP2, TMP2 - | ld TMP1, TAB:RB->array - | sltu AT, TMP2, TMP0 - | sll TMP2, TMP2, 3 - | beqz AT, ->vmeta_tgetv // Integer key and in array part? - |. daddu TMP2, TMP1, TMP2 - | ld AT, 0(TMP2) - | beq AT, TISNIL, >2 - |. ld CRET1, 0(TMP2) - |1: - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - | - |2: // Check for __index if table value is nil. - | ld TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_tgetv - |. nop - | - |5: - | li AT, LJ_TSTR - | bne TMP3, AT, ->vmeta_tgetv - |. cleartp RC, TMP2 - | b ->BC_TGETS_Z // String key? - |. nop - break; - case BC_TGETS: - | // RA = dst*8, RB = table*8, RC = str_const*8 (~) - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RC8a RC, INS - | daddu CARG2, BASE, RB - | decode_RC8b RC - | ld TAB:RB, 0(CARG2) - | dsubu CARG3, KBASE, RC - | daddu RA, BASE, RA - | ld STR:RC, -8(CARG3) // KBASE-8-str_const*8 - | checktab TAB:RB, ->vmeta_tgets1 - |->BC_TGETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8 - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->sid - | ld NODE:TMP2, TAB:RB->node - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | li TMP3, LJ_TSTR - | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - | settp STR:RC, TMP3 // Tagged key to look for. - |1: - | ld CARG1, NODE:TMP2->key - | ld CRET1, NODE:TMP2->val - | ld NODE:TMP1, NODE:TMP2->next - | bne CARG1, RC, >4 - |. ld TAB:TMP3, TAB:RB->metatable - | beq CRET1, TISNIL, >5 // Key found, but nil value? - |. nop - |3: - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - | - |4: // Follow hash chain. - | bnez NODE:TMP1, <1 - |. move NODE:TMP2, NODE:TMP1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | beqz TAB:TMP3, <3 // No metatable: done. - |. move CRET1, TISNIL - | lbu TMP0, TAB:TMP3->nomm - | andi TMP0, TMP0, 1<vmeta_tgets - |. nop - break; - case BC_TGETB: - | // RA = dst*8, RB = table*8, RC = index*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | daddu CARG2, BASE, RB - | decode_RDtoRC8 RC, RD - | ld TAB:RB, 0(CARG2) - | daddu RA, BASE, RA - | srl TMP0, RC, 3 - | checktab TAB:RB, ->vmeta_tgetb - | lw TMP1, TAB:RB->asize - | ld TMP2, TAB:RB->array - | sltu AT, TMP0, TMP1 - | beqz AT, ->vmeta_tgetb - |. daddu RC, TMP2, RC - | ld AT, 0(RC) - | beq AT, TISNIL, >5 - |. ld CRET1, 0(RC) - |1: - | ins_next1 - | sd CRET1, 0(RA) - | ins_next2 - | - |5: // Check for __index if table value is nil. - | ld TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP1, TAB:TMP2->nomm - | andi TMP1, TMP1, 1<vmeta_tgetb // Caveat: preserve TMP0 and CARG2! - |. nop - break; - case BC_TGETR: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | daddu RB, BASE, RB - | daddu RC, BASE, RC - | ld TAB:CARG1, 0(RB) - | lw CARG2, LO(RC) - | daddu RA, BASE, RA - | cleartp TAB:CARG1 - | lw TMP0, TAB:CARG1->asize - | ld TMP1, TAB:CARG1->array - | sltu AT, CARG2, TMP0 - | sll TMP2, CARG2, 3 - | beqz AT, ->vmeta_tgetr // In array part? - |. daddu CRET1, TMP1, TMP2 - | ld CARG2, 0(CRET1) - |->BC_TGETR_Z: - | ins_next1 - | sd CARG2, 0(RA) - | ins_next2 - break; - - case BC_TSETV: - | // RA = src*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | daddu CARG2, BASE, RB - | daddu CARG3, BASE, RC - | ld RB, 0(CARG2) - | ld TMP2, 0(CARG3) - | daddu RA, BASE, RA - | checktab RB, ->vmeta_tsetv - | checkint TMP2, >5 - |. sextw RC, TMP2 - | lw TMP0, TAB:RB->asize - | ld TMP1, TAB:RB->array - | sltu AT, RC, TMP0 - | sll TMP2, RC, 3 - | beqz AT, ->vmeta_tsetv // Integer key and in array part? - |. daddu TMP1, TMP1, TMP2 - | ld TMP0, 0(TMP1) - | lbu TMP3, TAB:RB->marked - | beq TMP0, TISNIL, >3 - |. ld CRET1, 0(RA) - |1: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. sd CRET1, 0(TMP1) - |2: - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | ld TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP2, TAB:TMP2->nomm - | andi TMP2, TMP2, 1<vmeta_tsetv - |. nop - | - |5: - | gettp AT, TMP2 - | daddiu AT, AT, -LJ_TSTR - | bnez AT, ->vmeta_tsetv - |. nop - | b ->BC_TSETS_Z // String key? - |. cleartp STR:RC, TMP2 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <2 - break; - case BC_TSETS: - | // RA = src*8, RB = table*8, RC = str_const*8 (~) - | decode_RB8a RB, INS - | decode_RB8b RB - | daddu CARG2, BASE, RB - | decode_RC8a RC, INS - | ld TAB:RB, 0(CARG2) - | decode_RC8b RC - | dsubu CARG3, KBASE, RC - | ld RC, -8(CARG3) // KBASE-8-str_const*8 - | daddu RA, BASE, RA - | cleartp STR:RC - | checktab TAB:RB, ->vmeta_tsets1 - |->BC_TSETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = BASE+src*8 - | lw TMP0, TAB:RB->hmask - | lw TMP1, STR:RC->sid - | ld NODE:TMP2, TAB:RB->node - | sb r0, TAB:RB->nomm // Clear metamethod cache. - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | sll TMP0, TMP1, 5 - | sll TMP1, TMP1, 3 - | subu TMP1, TMP0, TMP1 - | li TMP3, LJ_TSTR - | daddu NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - | settp STR:RC, TMP3 // Tagged key to look for. - |.if FPU - | ldc1 FTMP0, 0(RA) - |.else - | ld CRET1, 0(RA) - |.endif - |1: - | ld TMP0, NODE:TMP2->key - | ld CARG2, NODE:TMP2->val - | ld NODE:TMP1, NODE:TMP2->next - | bne TMP0, RC, >5 - |. lbu TMP3, TAB:RB->marked - | beq CARG2, TISNIL, >4 // Key found, but nil value? - |. ld TAB:TMP0, TAB:RB->metatable - |2: - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |.if FPU - |. sdc1 FTMP0, NODE:TMP2->val - |.else - |. sd CRET1, NODE:TMP2->val - |.endif - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | beqz TAB:TMP0, <2 // No metatable: done. - |. nop - | lbu TMP0, TAB:TMP0->nomm - | andi TMP0, TMP0, 1<vmeta_tsets - |. nop - | - |5: // Follow hash chain. - | bnez NODE:TMP1, <1 - |. move NODE:TMP2, NODE:TMP1 - | // End of hash chain: key not found, add a new one - | - | // But check for __newindex first. - | ld TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, >6 // No metatable: continue. - |. daddiu CARG3, DISPATCH, DISPATCH_GL(tmptv) - | lbu TMP0, TAB:TMP2->nomm - | andi TMP0, TMP0, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | load_got lj_tab_newkey - | sd RC, 0(CARG3) - | sd BASE, L->base - | move CARG2, TAB:RB - | sd PC, SAVE_PC - | call_intern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k - |. move CARG1, L - | // Returns TValue *. - | ld BASE, L->base - |.if FPU - | b <3 // No 2nd write barrier needed. - |. sdc1 FTMP0, 0(CRET1) - |.else - | ld CARG1, 0(RA) - | b <3 // No 2nd write barrier needed. - |. sd CARG1, 0(CRET1) - |.endif - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <3 - break; - case BC_TSETB: - | // RA = src*8, RB = table*8, RC = index*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | daddu CARG2, BASE, RB - | decode_RDtoRC8 RC, RD - | ld TAB:RB, 0(CARG2) - | daddu RA, BASE, RA - | srl TMP0, RC, 3 - | checktab RB, ->vmeta_tsetb - | lw TMP1, TAB:RB->asize - | ld TMP2, TAB:RB->array - | sltu AT, TMP0, TMP1 - | beqz AT, ->vmeta_tsetb - |. daddu RC, TMP2, RC - | ld TMP1, 0(RC) - | lbu TMP3, TAB:RB->marked - | beq TMP1, TISNIL, >5 - |1: - |. ld CRET1, 0(RA) - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. sd CRET1, 0(RC) - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | ld TAB:TMP2, TAB:RB->metatable - | beqz TAB:TMP2, <1 // No metatable: done. - |. nop - | lbu TMP1, TAB:TMP2->nomm - | andi TMP1, TMP1, 1<vmeta_tsetb // Caveat: preserve TMP0 and CARG2! - |. nop - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0, <2 - break; - case BC_TSETR: - | // RA = dst*8, RB = table*8, RC = key*8 - | decode_RB8a RB, INS - | decode_RB8b RB - | decode_RDtoRC8 RC, RD - | daddu CARG1, BASE, RB - | daddu CARG3, BASE, RC - | ld TAB:CARG2, 0(CARG1) - | lw CARG3, LO(CARG3) - | cleartp TAB:CARG2 - | lbu TMP3, TAB:CARG2->marked - | lw TMP0, TAB:CARG2->asize - | ld TMP1, TAB:CARG2->array - | andi AT, TMP3, LJ_GC_BLACK // isblack(table) - | bnez AT, >7 - |. daddu RA, BASE, RA - |2: - | sltu AT, CARG3, TMP0 - | sll TMP2, CARG3, 3 - | beqz AT, ->vmeta_tsetr // In array part? - |. daddu CRET1, TMP1, TMP2 - |->BC_TSETR_Z: - | ld CARG1, 0(RA) - | ins_next1 - | sd CARG1, 0(CRET1) - | ins_next2 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, CRET1, <2 - break; - - case BC_TSETM: - | // RA = base*8 (table at base-1), RD = num_const*8 (start index) - | daddu RA, BASE, RA - |1: - | daddu TMP3, KBASE, RD - | ld TAB:CARG2, -8(RA) // Guaranteed to be a table. - | addiu TMP0, MULTRES, -8 - | lw TMP3, LO(TMP3) // Integer constant is in lo-word. - | beqz TMP0, >4 // Nothing to copy? - |. srl CARG3, TMP0, 3 - | cleartp CARG2 - | addu CARG3, CARG3, TMP3 - | lw TMP2, TAB:CARG2->asize - | sll TMP1, TMP3, 3 - | lbu TMP3, TAB:CARG2->marked - | ld CARG1, TAB:CARG2->array - | sltu AT, TMP2, CARG3 - | bnez AT, >5 - |. daddu TMP2, RA, TMP0 - | daddu TMP1, TMP1, CARG1 - | andi TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |3: // Copy result slots to table. - | ld CRET1, 0(RA) - | daddiu RA, RA, 8 - | sltu AT, RA, TMP2 - | sd CRET1, 0(TMP1) - | bnez AT, <3 - |. daddiu TMP1, TMP1, 8 - | bnez TMP0, >7 - |. nop - |4: - | ins_next - | - |5: // Need to resize array part. - | load_got lj_tab_reasize - | sd BASE, L->base - | sd PC, SAVE_PC - | move BASE, RD - | call_intern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - |. move CARG1, L - | // Must not reallocate the stack. - | move RD, BASE - | b <1 - |. ld BASE, L->base // Reload BASE for lack of a saved register. - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP0, <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8 - | decode_RDtoRC8 NARGS8:RC, RD - | b ->BC_CALL_Z - |. addu NARGS8:RC, NARGS8:RC, MULTRES - break; - case BC_CALL: - | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8 - | decode_RDtoRC8 NARGS8:RC, RD - |->BC_CALL_Z: - | move TMP2, BASE - | daddu BASE, BASE, RA - | ld LFUNC:RB, 0(BASE) - | daddiu BASE, BASE, 16 - | addiu NARGS8:RC, NARGS8:RC, -8 - | checkfunc RB, ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs*8 - | addu NARGS8:RD, NARGS8:RD, MULTRES // BC_CALLT gets RC from RD. - | // Fall through. Assumes BC_CALLT follows. - break; - case BC_CALLT: - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - | daddu RA, BASE, RA - | ld RB, 0(RA) - | move NARGS8:RC, RD - | ld TMP1, FRAME_PC(BASE) - | daddiu RA, RA, 16 - | addiu NARGS8:RC, NARGS8:RC, -8 - | checktp CARG3, RB, -LJ_TFUNC, ->vmeta_callt - |->BC_CALLT_Z: - | andi TMP0, TMP1, FRAME_TYPE // Caveat: preserve TMP0 until the 'or'. - | lbu TMP3, LFUNC:CARG3->ffid - | bnez TMP0, >7 - |. xori TMP2, TMP1, FRAME_VARG - |1: - | sd RB, FRAME_FUNC(BASE) // Copy function down, but keep PC. - | sltiu AT, TMP3, 2 // (> FF_C) Calling a fast function? - | move TMP2, BASE - | move RB, CARG3 - | beqz NARGS8:RC, >3 - |. move TMP3, NARGS8:RC - |2: - | ld CRET1, 0(RA) - | daddiu RA, RA, 8 - | addiu TMP3, TMP3, -8 - | sd CRET1, 0(TMP2) - | bnez TMP3, <2 - |. daddiu TMP2, TMP2, 8 - |3: - | or TMP0, TMP0, AT - | beqz TMP0, >5 - |. nop - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | lw INS, -4(TMP1) - | decode_RA8a RA, INS - | decode_RA8b RA - | dsubu TMP1, BASE, RA - | ld TMP1, -32(TMP1) - | cleartp LFUNC:TMP1 - | ld TMP1, LFUNC:TMP1->pc - | b <4 - |. ld KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE. - | - |7: // Tailcall from a vararg function. - | andi AT, TMP2, FRAME_TYPEP - | bnez AT, <1 // Vararg frame below? - |. dsubu TMP2, BASE, TMP2 // Relocate BASE down. - | move BASE, TMP2 - | ld TMP1, FRAME_PC(TMP2) - | b <1 - |. andi TMP0, TMP1, FRAME_TYPE - break; - - case BC_ITERC: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8)) - | move TMP2, BASE // Save old BASE fir vmeta_call. - | daddu BASE, BASE, RA - | ld RB, -24(BASE) - | ld CARG1, -16(BASE) - | ld CARG2, -8(BASE) - | li NARGS8:RC, 16 // Iterators get 2 arguments. - | sd RB, 0(BASE) // Copy callable. - | sd CARG1, 16(BASE) // Copy state. - | sd CARG2, 24(BASE) // Copy control var. - | daddiu BASE, BASE, 16 - | checkfunc RB, ->vmeta_call - | ins_call - break; - - case BC_ITERN: - |.if JIT and ENDIAN_LE - | hotloop - |.endif - |->vm_IITERN: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8) - | daddu RA, BASE, RA - | ld TAB:RB, -16(RA) - | lw RC, -8+LO(RA) // Get index from control var. - | cleartp TAB:RB - | daddiu PC, PC, 4 - | lw TMP0, TAB:RB->asize - | ld TMP1, TAB:RB->array - | dsll CARG3, TISNUM, 47 - |1: // Traverse array part. - | sltu AT, RC, TMP0 - | beqz AT, >5 // Index points after array part? - |. sll TMP3, RC, 3 - | daddu TMP3, TMP1, TMP3 - | ld CARG1, 0(TMP3) - | lhu RD, -4+OFS_RD(PC) - | or TMP2, RC, CARG3 - | beq CARG1, TISNIL, <1 // Skip holes in array part. - |. addiu RC, RC, 1 - | sd TMP2, 0(RA) - | sd CARG1, 8(RA) - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | decode_RD4b RD - | daddu RD, RD, TMP3 - | sw RC, -8+LO(RA) // Update control var. - | daddu PC, PC, RD - |3: - | ins_next - | - |5: // Traverse hash part. - | lw TMP1, TAB:RB->hmask - | subu RC, RC, TMP0 - | ld TMP2, TAB:RB->node - |6: - | sltu AT, TMP1, RC // End of iteration? Branch to ITERL+1. - | bnez AT, <3 - |. sll TMP3, RC, 5 - | sll RB, RC, 3 - | subu TMP3, TMP3, RB - | daddu NODE:TMP3, TMP3, TMP2 - | ld CARG1, 0(NODE:TMP3) - | lhu RD, -4+OFS_RD(PC) - | beq CARG1, TISNIL, <6 // Skip holes in hash part. - |. addiu RC, RC, 1 - | ld CARG2, NODE:TMP3->key - | lui TMP3, (-(BCBIAS_J*4 >> 16) & 65535) - | sd CARG1, 8(RA) - | addu RC, RC, TMP0 - | decode_RD4b RD - | addu RD, RD, TMP3 - | sd CARG2, 0(RA) - | daddu PC, PC, RD - | b <3 - |. sw RC, -8+LO(RA) // Update control var. - break; - - case BC_ISNEXT: - | // RA = base*8, RD = target (points to ITERN) - | daddu RA, BASE, RA - | srl TMP0, RD, 1 - | ld CFUNC:CARG1, -24(RA) - | daddu TMP0, PC, TMP0 - | ld CARG2, -16(RA) - | ld CARG3, -8(RA) - | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535) - | checkfunc CFUNC:CARG1, >5 - | gettp CARG2, CARG2 - | daddiu CARG2, CARG2, -LJ_TTAB - | lbu TMP1, CFUNC:CARG1->ffid - | daddiu CARG3, CARG3, -LJ_TNIL - | or AT, CARG2, CARG3 - | daddiu TMP1, TMP1, -FF_next_N - | or AT, AT, TMP1 - | bnez AT, >5 - |. lui TMP1, (LJ_KEYINDEX >> 16) - | daddu PC, TMP0, TMP2 - | ori TMP1, TMP1, (LJ_KEYINDEX & 0xffff) - | dsll TMP1, TMP1, 32 - | sd TMP1, -8(RA) - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | li TMP3, BC_JMP - | li TMP1, BC_ITERC - | sb TMP3, -4+OFS_OP(PC) - | daddu PC, TMP0, TMP2 - |.if JIT - | lb TMP0, OFS_OP(PC) - | li AT, BC_ITERN - | bne TMP0, AT, >6 - |. lhu TMP2, OFS_RD(PC) - |.endif - | b <1 - |. sb TMP1, OFS_OP(PC) - |.if JIT - |6: // Unpatch JLOOP. - | ld TMP0, DISPATCH_J(trace)(DISPATCH) - | sll TMP2, TMP2, 3 - | daddu TMP0, TMP0, TMP2 - | ld TRACE:TMP2, 0(TMP0) - | lw TMP0, TRACE:TMP2->startins - | li AT, -256 - | and TMP0, TMP0, AT - | or TMP0, TMP0, TMP1 - | b <1 - |. sw TMP0, 0(PC) - |.endif - break; - - case BC_VARG: - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | ld TMP0, FRAME_PC(BASE) - | decode_RDtoRC8 RC, RD - | decode_RB8a RB, INS - | daddu RC, BASE, RC - | decode_RB8b RB - | daddu RA, BASE, RA - | daddiu RC, RC, FRAME_VARG - | daddu TMP2, RA, RB - | daddiu TMP3, BASE, -16 // TMP3 = vtop - | dsubu RC, RC, TMP0 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | beqz RB, >5 // Copy all varargs? - |. dsubu TMP1, TMP3, RC - | daddiu TMP2, TMP2, -16 - |1: // Copy vararg slots to destination slots. - | ld CARG1, 0(RC) - | sltu AT, RC, TMP3 - | daddiu RC, RC, 8 - |.if MIPSR6 - | selnez CARG1, CARG1, AT - | seleqz AT, TISNIL, AT - | or CARG1, CARG1, AT - |.else - | movz CARG1, TISNIL, AT - |.endif - | sd CARG1, 0(RA) - | sltu AT, RA, TMP2 - | bnez AT, <1 - |. daddiu RA, RA, 8 - |3: - | ins_next - | - |5: // Copy all varargs. - | ld TMP0, L->maxstack - | blez TMP1, <3 // No vararg slots? - |. li MULTRES, 8 // MULTRES = (0+1)*8 - | daddu TMP2, RA, TMP1 - | sltu AT, TMP0, TMP2 - | bnez AT, >7 - |. daddiu MULTRES, TMP1, 8 - |6: - | ld CRET1, 0(RC) - | daddiu RC, RC, 8 - | sd CRET1, 0(RA) - | sltu AT, RC, TMP3 - | bnez AT, <6 // More vararg slots? - |. daddiu RA, RA, 8 - | b <3 - |. nop - | - |7: // Grow stack for varargs. - | load_got lj_state_growstack - | sd RA, L->top - | dsubu RA, RA, BASE - | sd BASE, L->base - | dsubu BASE, RC, BASE // Need delta, because BASE may change. - | sd PC, SAVE_PC - | srl CARG2, TMP1, 3 - | call_intern lj_state_growstack // (lua_State *L, int n) - |. move CARG1, L - | move RC, BASE - | ld BASE, L->base - | daddu RA, BASE, RA - | daddu RC, BASE, RC - | b <6 - |. daddiu TMP3, BASE, -16 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RD = extra_nresults*8 - | addu RD, RD, MULTRES // MULTRES >= 8, so RD >= 8. - | // Fall through. Assumes BC_RET follows. - break; - - case BC_RET: - | // RA = results*8, RD = (nresults+1)*8 - | ld PC, FRAME_PC(BASE) - | daddu RA, BASE, RA - | move MULTRES, RD - |1: - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->BC_RETV_Z - |. xori TMP1, PC, FRAME_VARG - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return - | lw INS, -4(PC) - | daddiu TMP2, BASE, -16 - | daddiu RC, RD, -8 - | decode_RA8a TMP0, INS - | decode_RB8a RB, INS - | decode_RA8b TMP0 - | decode_RB8b RB - | daddu TMP3, TMP2, RB - | beqz RC, >3 - |. dsubu BASE, TMP2, TMP0 - |2: - | ld CRET1, 0(RA) - | daddiu RA, RA, 8 - | daddiu RC, RC, -8 - | sd CRET1, 0(TMP2) - | bnez RC, <2 - |. daddiu TMP2, TMP2, 8 - |3: - | daddiu TMP3, TMP3, -8 - |5: - | sltu AT, TMP2, TMP3 - | bnez AT, >6 - |. ld LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | cleartp LFUNC:TMP1 - | ld TMP1, LFUNC:TMP1->pc - | ld KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | sd TISNIL, 0(TMP2) - | b <5 - |. daddiu TMP2, TMP2, 8 - | - |->BC_RETV_Z: // Non-standard return case. - | andi TMP2, TMP1, FRAME_TYPEP - | bnez TMP2, ->vm_return - |. nop - | // Return from vararg function: relocate BASE down. - | dsubu BASE, BASE, TMP1 - | b <1 - |. ld PC, FRAME_PC(BASE) - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RD = (nresults+1)*8 - | ld PC, FRAME_PC(BASE) - | daddu RA, BASE, RA - | move MULTRES, RD - | andi TMP0, PC, FRAME_TYPE - | bnez TMP0, ->BC_RETV_Z - |. xori TMP1, PC, FRAME_VARG - | lw INS, -4(PC) - | daddiu TMP2, BASE, -16 - if (op == BC_RET1) { - | ld CRET1, 0(RA) - } - | decode_RB8a RB, INS - | decode_RA8a RA, INS - | decode_RB8b RB - | decode_RA8b RA - | dsubu BASE, TMP2, RA - if (op == BC_RET1) { - | sd CRET1, 0(TMP2) - } - |5: - | sltu AT, RD, RB - | bnez AT, >6 - |. ld TMP1, FRAME_FUNC(BASE) - | ins_next1 - | cleartp LFUNC:TMP1 - | ld TMP1, LFUNC:TMP1->pc - | ld KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | daddiu TMP2, TMP2, 8 - | daddiu RD, RD, 8 - | b <5 - if (op == BC_RET1) { - |. sd TISNIL, 0(TMP2) - } else { - |. sd TISNIL, -8(TMP2) - } - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RD = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - | daddu RA, BASE, RA - | ld CARG1, FORL_IDX*8(RA) // IDX CARG1 - CARG3 type - | gettp CARG3, CARG1 - if (op != BC_JFORL) { - | srl RD, RD, 1 - | lui TMP2, (-(BCBIAS_J*4 >> 16) & 65535) - | daddu TMP2, RD, TMP2 - } - if (!vk) { - | ld CARG2, FORL_STOP*8(RA) // STOP CARG2 - CARG4 type - | ld CRET1, FORL_STEP*8(RA) // STEP CRET1 - CRET2 type - | gettp CARG4, CARG2 - | bne CARG3, TISNUM, >5 - |. gettp CRET2, CRET1 - | bne CARG4, TISNUM, ->vmeta_for - |. sextw CARG3, CARG1 - | bne CRET2, TISNUM, ->vmeta_for - |. sextw CARG2, CARG2 - | dext AT, CRET1, 31, 0 - | slt CRET1, CARG2, CARG3 - | slt TMP1, CARG3, CARG2 - |.if MIPSR6 - | selnez TMP1, TMP1, AT - | seleqz CRET1, CRET1, AT - | or CRET1, CRET1, TMP1 - |.else - | movn CRET1, TMP1, AT - |.endif - } else { - | bne CARG3, TISNUM, >5 - |. ld CARG2, FORL_STEP*8(RA) // STEP CARG2 - CARG4 type - | ld CRET1, FORL_STOP*8(RA) // STOP CRET1 - CRET2 type - | sextw TMP3, CARG1 - | sextw CARG2, CARG2 - | sextw CRET1, CRET1 - | addu CARG1, TMP3, CARG2 - | xor TMP0, CARG1, TMP3 - | xor TMP1, CARG1, CARG2 - | and TMP0, TMP0, TMP1 - | slt TMP1, CARG1, CRET1 - | slt CRET1, CRET1, CARG1 - | slt AT, CARG2, r0 - | slt TMP0, TMP0, r0 // ((y^a) & (y^b)) < 0: overflow. - |.if MIPSR6 - | selnez TMP1, TMP1, AT - | seleqz CRET1, CRET1, AT - | or CRET1, CRET1, TMP1 - |.else - | movn CRET1, TMP1, AT - |.endif - | or CRET1, CRET1, TMP0 - | zextw CARG1, CARG1 - | settp CARG1, TISNUM - } - |1: - if (op == BC_FORI) { - |.if MIPSR6 - | selnez TMP2, TMP2, CRET1 - |.else - | movz TMP2, r0, CRET1 - |.endif - | daddu PC, PC, TMP2 - } else if (op == BC_JFORI) { - | daddu PC, PC, TMP2 - | lhu RD, -4+OFS_RD(PC) - } else if (op == BC_IFORL) { - |.if MIPSR6 - | seleqz TMP2, TMP2, CRET1 - |.else - | movn TMP2, r0, CRET1 - |.endif - | daddu PC, PC, TMP2 - } - if (vk) { - | sd CARG1, FORL_IDX*8(RA) - } - | ins_next1 - | sd CARG1, FORL_EXT*8(RA) - |2: - if (op == BC_JFORI) { - | beqz CRET1, =>BC_JLOOP - |. decode_RD8b RD - } else if (op == BC_JFORL) { - | beqz CRET1, =>BC_JLOOP - } - | ins_next2 - | - |5: // FP loop. - |.if FPU - if (!vk) { - | ldc1 f0, FORL_IDX*8(RA) - | ldc1 f2, FORL_STOP*8(RA) - | sltiu TMP0, CARG3, LJ_TISNUM - | sltiu TMP1, CARG4, LJ_TISNUM - | sltiu AT, CRET2, LJ_TISNUM - | ld TMP3, FORL_STEP*8(RA) - | and TMP0, TMP0, TMP1 - | and AT, AT, TMP0 - | beqz AT, ->vmeta_for - |. slt TMP3, TMP3, r0 - |.if MIPSR6 - | dmtc1 TMP3, FTMP2 - | cmp.lt.d FTMP0, f0, f2 - | cmp.lt.d FTMP1, f2, f0 - | sel.d FTMP2, FTMP1, FTMP0 - | b <1 - |. dmfc1 CRET1, FTMP2 - |.else - | c.ole.d 0, f0, f2 - | c.ole.d 1, f2, f0 - | li CRET1, 1 - | movt CRET1, r0, 0 - | movt AT, r0, 1 - | b <1 - |. movn CRET1, AT, TMP3 - |.endif - } else { - | ldc1 f0, FORL_IDX*8(RA) - | ldc1 f4, FORL_STEP*8(RA) - | ldc1 f2, FORL_STOP*8(RA) - | ld TMP3, FORL_STEP*8(RA) - | add.d f0, f0, f4 - |.if MIPSR6 - | slt TMP3, TMP3, r0 - | dmtc1 TMP3, FTMP2 - | cmp.lt.d FTMP0, f0, f2 - | cmp.lt.d FTMP1, f2, f0 - | sel.d FTMP2, FTMP1, FTMP0 - | dmfc1 CRET1, FTMP2 - if (op == BC_IFORL) { - | seleqz TMP2, TMP2, CRET1 - | daddu PC, PC, TMP2 - } - |.else - | c.ole.d 0, f0, f2 - | c.ole.d 1, f2, f0 - | slt TMP3, TMP3, r0 - | li CRET1, 1 - | li AT, 1 - | movt CRET1, r0, 0 - | movt AT, r0, 1 - | movn CRET1, AT, TMP3 - if (op == BC_IFORL) { - | movn TMP2, r0, CRET1 - | daddu PC, PC, TMP2 - } - |.endif - | sdc1 f0, FORL_IDX*8(RA) - | ins_next1 - | b <2 - |. sdc1 f0, FORL_EXT*8(RA) - } - |.else - if (!vk) { - | sltiu TMP0, CARG3, LJ_TISNUM - | sltiu TMP1, CARG4, LJ_TISNUM - | sltiu AT, CRET2, LJ_TISNUM - | and TMP0, TMP0, TMP1 - | and AT, AT, TMP0 - | beqz AT, ->vmeta_for - |. nop - | bal ->vm_sfcmpolex - |. lw TMP3, FORL_STEP*8+HI(RA) - | b <1 - |. nop - } else { - | load_got __adddf3 - | call_extern - |. sw TMP2, TMPD - | ld CARG2, FORL_STOP*8(RA) - | move CARG1, CRET1 - if ( op == BC_JFORL ) { - | lhu RD, -4+OFS_RD(PC) - | decode_RD8b RD - } - | bal ->vm_sfcmpolex - |. lw TMP3, FORL_STEP*8+HI(RA) - | b <1 - |. lw TMP2, TMPD - } - |.endif - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RD = target - | daddu RA, BASE, RA - | ld TMP1, 0(RA) - | beq TMP1, TISNIL, >1 // Stop if iterator returned nil. - |. nop - if (op == BC_JITERL) { - | b =>BC_JLOOP - |. sd TMP1, -8(RA) - } else { - | branch_RD // Otherwise save control var + branch. - | sd TMP1, -8(RA) - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base*8 (ignored), RD = traceno*8 - | ld TMP1, DISPATCH_J(trace)(DISPATCH) - | li AT, 0 - | daddu TMP1, TMP1, RD - | // Traces on MIPS don't store the trace number, so use 0. - | sd AT, DISPATCH_GL(vmstate)(DISPATCH) - | ld TRACE:TMP2, 0(TMP1) - | sd BASE, DISPATCH_GL(jit_base)(DISPATCH) - | ld TMP2, TRACE:TMP2->mcode - | sd L, DISPATCH_GL(tmpbuf.L)(DISPATCH) - | jr TMP2 - |. daddiu JGL, DISPATCH, GG_DISP2G+32768 - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RD = target - | branch_RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | ld TMP2, L->maxstack - | lbu TMP1, -4+PC2PROTO(numparams)(PC) - | ld KBASE, -4+PC2PROTO(k)(PC) - | sltu AT, TMP2, RA - | bnez AT, ->vm_growstack_l - |. sll TMP1, TMP1, 3 - if (op != BC_JFUNCF) { - | ins_next1 - } - |2: - | sltu AT, NARGS8:RC, TMP1 // Check for missing parameters. - | bnez AT, >3 - |. daddu AT, BASE, NARGS8:RC - if (op == BC_JFUNCF) { - | decode_RD8a RD, INS - | b =>BC_JLOOP - |. decode_RD8b RD - } else { - | ins_next2 - } - | - |3: // Clear missing parameters. - | sd TISNIL, 0(AT) - | b <2 - |. addiu NARGS8:RC, NARGS8:RC, 8 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | li TMP0, LJ_TFUNC - | daddu TMP1, BASE, RC - | ld TMP2, L->maxstack - | settp LFUNC:RB, TMP0 - | daddu TMP0, RA, RC - | sd LFUNC:RB, 0(TMP1) // Store (tagged) copy of LFUNC. - | daddiu TMP3, RC, 16+FRAME_VARG - | sltu AT, TMP0, TMP2 - | ld KBASE, -4+PC2PROTO(k)(PC) - | beqz AT, ->vm_growstack_l - |. sd TMP3, 8(TMP1) // Store delta + FRAME_VARG. - | lbu TMP2, -4+PC2PROTO(numparams)(PC) - | move RA, BASE - | move RC, TMP1 - | ins_next1 - | beqz TMP2, >3 - |. daddiu BASE, TMP1, 16 - |1: - | ld TMP0, 0(RA) - | sltu AT, RA, RC // Less args than parameters? - | move CARG1, TMP0 - |.if MIPSR6 - | selnez TMP0, TMP0, AT - | seleqz TMP3, TISNIL, AT - | or TMP0, TMP0, TMP3 - | seleqz TMP3, CARG1, AT - | selnez CARG1, TISNIL, AT - | or CARG1, CARG1, TMP3 - |.else - | movz TMP0, TISNIL, AT // Clear missing parameters. - | movn CARG1, TISNIL, AT // Clear old fixarg slot (help the GC). - |.endif - | addiu TMP2, TMP2, -1 - | sd TMP0, 16(TMP1) - | daddiu TMP1, TMP1, 8 - | sd CARG1, 0(RA) - | bnez TMP2, <1 - |. daddiu RA, RA, 8 - |3: - | ins_next2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | ld CFUNCADDR, CFUNC:RB->f - } else { - | ld CFUNCADDR, DISPATCH_GL(wrapf)(DISPATCH) - } - | daddu TMP1, RA, NARGS8:RC - | ld TMP2, L->maxstack - | daddu RC, BASE, NARGS8:RC - | sd BASE, L->base - | sltu AT, TMP2, TMP1 - | sd RC, L->top - | li_vmstate C - if (op == BC_FUNCCW) { - | ld CARG2, CFUNC:RB->f - } - | bnez AT, ->vm_growstack_c // Need to grow stack. - |. move CARG1, L - | jalr CFUNCADDR // (lua_State *L [, lua_CFunction f]) - |. st_vmstate - | // Returns nresults. - | ld BASE, L->base - | sll RD, CRET1, 3 - | ld TMP1, L->top - | li_vmstate INTERP - | ld PC, FRAME_PC(BASE) // Fetch PC of caller. - | dsubu RA, TMP1, RD // RA = L->top - nresults*8 - | sd L, DISPATCH_GL(cur_L)(DISPATCH) - | b ->vm_returnc - |. st_vmstate - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.4byte .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.4byte 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 31\n" - "\t.byte 0xc\n\t.uleb128 29\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.4byte .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.4byte .Lframe0\n" - "\t.8byte .Lbegin\n" - "\t.8byte %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x9f\n\t.sleb128 2*5\n" - "\t.byte 0x9e\n\t.sleb128 2*6\n", - fcofs, CFRAME_SIZE); - for (i = 23; i >= 16; i--) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+i, 2*(30-i)); -#if !LJ_SOFTFP - for (i = 31; i >= 24; i--) - fprintf(ctx->fp, "\t.byte %d\n\t.uleb128 %d\n", 0x80+32+i, 2*(46-i)); -#endif - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.4byte .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.4byte .Lframe0\n" - "\t.4byte lj_vm_ffi_call\n" - "\t.4byte %d\n" - "\t.byte 0x9f\n\t.uleb128 2*1\n" - "\t.byte 0x90\n\t.uleb128 2*2\n" - "\t.byte 0xd\n\t.uleb128 0x10\n" - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND - /* NYI */ -#endif - break; - default: - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_ppc.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_ppc.dasc deleted file mode 100644 index 3cad37d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_ppc.dasc +++ /dev/null @@ -1,6041 +0,0 @@ -|// Low-level VM code for PowerPC 32 bit or 32on64 bit mode. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -| -|.arch ppc -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|// Note: The ragged indentation of the instructions is intentional. -|// The starting columns indicate data dependencies. -| -|//----------------------------------------------------------------------- -| -|// DynASM defines used by the PPC port: -|// -|// P64 64 bit pointers (only for GPR64 testing). -|// GPR64 64 bit registers (but possibly 32 bit pointers, e.g. PS3). -|// Affects reg saves, stack layout, carry/overflow/dot flags etc. -|// FRAME32 Use 32 bit frame layout, even with GPR64 (Xbox 360). -|// TOC Need table of contents (64 bit or 32 bit variant, e.g. PS3). -|// Function pointers are really a struct: code, TOC, env (optional). -|// TOCENV Function pointers have an environment pointer, too (not on PS3). -|// PPE Power Processor Element of Cell (PS3) or Xenon (Xbox 360). -|// Must avoid (slow) micro-coded instructions. -| -|.if P64 -|.define TOC, 1 -|.define TOCENV, 1 -|.macro lpx, a, b, c; ldx a, b, c; .endmacro -|.macro lp, a, b; ld a, b; .endmacro -|.macro stp, a, b; std a, b; .endmacro -|.define decode_OPP, decode_OP8 -|.if FFI -|// Missing: Calling conventions, 64 bit regs, TOC. -|.error lib_ffi not yet implemented for PPC64 -|.endif -|.else -|.macro lpx, a, b, c; lwzx a, b, c; .endmacro -|.macro lp, a, b; lwz a, b; .endmacro -|.macro stp, a, b; stw a, b; .endmacro -|.define decode_OPP, decode_OP4 -|.endif -| -|// Convenience macros for TOC handling. -|.if TOC -|// Linker needs a TOC patch area for every external call relocation. -|.macro blex, target; bl extern target@plt; nop; .endmacro -|.macro .toc, a, b; a, b; .endmacro -|.if P64 -|.define TOC_OFS, 8 -|.define ENV_OFS, 16 -|.else -|.define TOC_OFS, 4 -|.define ENV_OFS, 8 -|.endif -|.else // No TOC. -|.macro blex, target; bl extern target@plt; .endmacro -|.macro .toc, a, b; .endmacro -|.endif -|.macro .tocenv, a, b; .if TOCENV; a, b; .endif; .endmacro -| -|.macro .gpr64, a, b; .if GPR64; a, b; .endif; .endmacro -| -|.macro andix., y, a, i -|.if PPE -| rlwinm y, a, 0, 31-lj_fls(i), 31-lj_ffs(i) -| cmpwi y, 0 -|.else -| andi. y, a, i -|.endif -|.endmacro -| -|.macro clrso, reg -|.if PPE -| li reg, 0 -| mtxer reg -|.else -| mcrxr cr0 -|.endif -|.endmacro -| -|.macro checkov, reg, noov -|.if PPE -| mfxer reg -| add reg, reg, reg -| cmpwi reg, 0 -| li reg, 0 -| mtxer reg -| bgey noov -|.else -| mcrxr cr0 -| bley noov -|.endif -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Fixed register assignments for the interpreter. -|// Don't use: r1 = sp, r2 and r13 = reserved (TOC, TLS or SDATA) -| -|.macro .FPU, a, b -|.if FPU -| a, b -|.endif -|.endmacro -| -|.macro .FPU, a, b, c -|.if FPU -| a, b, c -|.endif -|.endmacro -| -|// The following must be C callee-save (but BASE is often refetched). -|.define BASE, r14 // Base of current Lua stack frame. -|.define KBASE, r15 // Constants of current Lua function. -|.define PC, r16 // Next PC. -|.define DISPATCH, r17 // Opcode dispatch table. -|.define LREG, r18 // Register holding lua_State (also in SAVE_L). -|.define MULTRES, r19 // Size of multi-result: (nresults+1)*8. -|.define JGL, r31 // On-trace: global_State + 32768. -| -|// Constants for type-comparisons, stores and conversions. C callee-save. -|.define TISNUM, r22 -|.define TISNIL, r23 -|.define ZERO, r24 -|.if FPU -|.define TOBIT, f30 // 2^52 + 2^51. -|.define TONUM, f31 // 2^52 + 2^51 + 2^31. -|.endif -| -|// The following temporaries are not saved across C calls, except for RA. -|.define RA, r20 // Callee-save. -|.define RB, r10 -|.define RC, r11 -|.define RD, r12 -|.define INS, r7 // Overlaps CARG5. -| -|.define TMP0, r0 -|.define TMP1, r8 -|.define TMP2, r9 -|.define TMP3, r6 // Overlaps CARG4. -| -|// Saved temporaries. -|.define SAVE0, r21 -|.define SAVE1, r25 -| -|// Calling conventions. -|.define CARG1, r3 -|.define CARG2, r4 -|.define CARG3, r5 -|.define CARG4, r6 // Overlaps TMP3. -|.define CARG5, r7 // Overlaps INS. -| -|.if FPU -|.define FARG1, f1 -|.define FARG2, f2 -|.endif -| -|.define CRET1, r3 -|.define CRET2, r4 -| -|.define TOCREG, r2 // TOC register (only used by C code). -|.define ENVREG, r11 // Environment pointer (nested C functions). -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|.if GPR64 -|.if FRAME32 -| -|// 456(sp) // \ 32/64 bit C frame info -|.define TONUM_LO, 452(sp) // | -|.define TONUM_HI, 448(sp) // | -|.define TMPD_LO, 444(sp) // | -|.define TMPD_HI, 440(sp) // | -|.define SAVE_CR, 432(sp) // | 64 bit CR save. -|.define SAVE_ERRF, 424(sp) // > Parameter save area. -|.define SAVE_NRES, 420(sp) // | -|.define SAVE_L, 416(sp) // | -|.define SAVE_PC, 412(sp) // | -|.define SAVE_MULTRES, 408(sp) // | -|.define SAVE_CFRAME, 400(sp) // / 64 bit C frame chain. -|// 392(sp) // Reserved. -|.define CFRAME_SPACE, 384 // Delta for sp. -|// Back chain for sp: 384(sp) <-- sp entering interpreter -|.define SAVE_LR, 376(sp) // 32 bit LR stored in hi-part. -|.define SAVE_GPR_, 232 // .. 232+18*8: 64 bit GPR saves. -|.define SAVE_FPR_, 88 // .. 88+18*8: 64 bit FPR saves. -|// 80(sp) // Needed for 16 byte stack frame alignment. -|// 16(sp) // Callee parameter save area (ABI mandated). -|// 8(sp) // Reserved -|// Back chain for sp: 0(sp) <-- sp while in interpreter -|// 32 bit sp stored in hi-part of 0(sp). -| -|.define TMPD_BLO, 447(sp) -|.define TMPD, TMPD_HI -|.define TONUM_D, TONUM_HI -| -|.else -| -|// 508(sp) // \ 32 bit C frame info. -|.define SAVE_ERRF, 472(sp) // | -|.define SAVE_NRES, 468(sp) // | -|.define SAVE_L, 464(sp) // > Parameter save area. -|.define SAVE_PC, 460(sp) // | -|.define SAVE_MULTRES, 456(sp) // | -|.define SAVE_CFRAME, 448(sp) // / 64 bit C frame chain. -|.define SAVE_LR, 416(sp) -|.define CFRAME_SPACE, 400 // Delta for sp. -|// Back chain for sp: 400(sp) <-- sp entering interpreter -|.define SAVE_FPR_, 256 // .. 256+18*8: 64 bit FPR saves. -|.define SAVE_GPR_, 112 // .. 112+18*8: 64 bit GPR saves. -|// 48(sp) // Callee parameter save area (ABI mandated). -|.define SAVE_TOC, 40(sp) // TOC save area. -|.define TMPD_LO, 36(sp) // \ Link editor temp (ABI mandated). -|.define TMPD_HI, 32(sp) // / -|.define TONUM_LO, 28(sp) // \ Compiler temp (ABI mandated). -|.define TONUM_HI, 24(sp) // / -|// Next frame lr: 16(sp) -|.define SAVE_CR, 8(sp) // 64 bit CR save. -|// Back chain for sp: 0(sp) <-- sp while in interpreter -| -|.define TMPD_BLO, 39(sp) -|.define TMPD, TMPD_HI -|.define TONUM_D, TONUM_HI -| -|.endif -|.else -| -|.if FPU -|.define SAVE_LR, 276(sp) -|.define CFRAME_SPACE, 272 // Delta for sp. -|// Back chain for sp: 272(sp) <-- sp entering interpreter -|.define SAVE_FPR_, 128 // .. 128+18*8: 64 bit FPR saves. -|.else -|.define SAVE_LR, 132(sp) -|.define CFRAME_SPACE, 128 // Delta for sp. -|// Back chain for sp: 128(sp) <-- sp entering interpreter -|.endif -|.define SAVE_GPR_, 56 // .. 56+18*4: 32 bit GPR saves. -|.define SAVE_CR, 52(sp) // 32 bit CR save. -|.define SAVE_ERRF, 48(sp) // 32 bit C frame info. -|.define SAVE_NRES, 44(sp) -|.define SAVE_CFRAME, 40(sp) -|.define SAVE_L, 36(sp) -|.define SAVE_PC, 32(sp) -|.define SAVE_MULTRES, 28(sp) -|.define UNUSED1, 24(sp) -|.if FPU -|.define TMPD_LO, 20(sp) -|.define TMPD_HI, 16(sp) -|.define TONUM_LO, 12(sp) -|.define TONUM_HI, 8(sp) -|.else -|.define SFSAVE_4, 20(sp) -|.define SFSAVE_3, 16(sp) -|.define SFSAVE_2, 12(sp) -|.define SFSAVE_1, 8(sp) -|.endif -|// Next frame lr: 4(sp) -|// Back chain for sp: 0(sp) <-- sp while in interpreter -| -|.if FPU -|.define TMPD_BLO, 23(sp) -|.define TMPD, TMPD_HI -|.define TONUM_D, TONUM_HI -|.endif -| -|.endif -| -|.macro save_, reg -|.if GPR64 -| std r..reg, SAVE_GPR_+(reg-14)*8(sp) -|.else -| stw r..reg, SAVE_GPR_+(reg-14)*4(sp) -|.endif -| .FPU stfd f..reg, SAVE_FPR_+(reg-14)*8(sp) -|.endmacro -|.macro rest_, reg -|.if GPR64 -| ld r..reg, SAVE_GPR_+(reg-14)*8(sp) -|.else -| lwz r..reg, SAVE_GPR_+(reg-14)*4(sp) -|.endif -| .FPU lfd f..reg, SAVE_FPR_+(reg-14)*8(sp) -|.endmacro -| -|.macro saveregs -|.if GPR64 and not FRAME32 -| stdu sp, -CFRAME_SPACE(sp) -|.else -| stwu sp, -CFRAME_SPACE(sp) -|.endif -| save_ 14; save_ 15; save_ 16 -| mflr r0 -| save_ 17; save_ 18; save_ 19; save_ 20; save_ 21; save_ 22 -|.if GPR64 and not FRAME32 -| std r0, SAVE_LR -|.else -| stw r0, SAVE_LR -|.endif -| save_ 23; save_ 24; save_ 25 -| mfcr r0 -| save_ 26; save_ 27; save_ 28; save_ 29; save_ 30; save_ 31 -|.if GPR64 -| std r0, SAVE_CR -|.else -| stw r0, SAVE_CR -|.endif -| .toc std TOCREG, SAVE_TOC -|.endmacro -| -|.macro restoreregs -|.if GPR64 and not FRAME32 -| ld r0, SAVE_LR -|.else -| lwz r0, SAVE_LR -|.endif -|.if GPR64 -| ld r12, SAVE_CR -|.else -| lwz r12, SAVE_CR -|.endif -| rest_ 14; rest_ 15; rest_ 16; rest_ 17; rest_ 18; rest_ 19 -| mtlr r0; -|.if PPE; mtocrf 0x20, r12; .else; mtcrf 0x38, r12; .endif -| rest_ 20; rest_ 21; rest_ 22; rest_ 23; rest_ 24; rest_ 25 -|.if PPE; mtocrf 0x10, r12; .endif -| rest_ 26; rest_ 27; rest_ 28; rest_ 29; rest_ 30; rest_ 31 -|.if PPE; mtocrf 0x08, r12; .endif -| addi sp, sp, CFRAME_SPACE -|.endmacro -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State, LREG -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS8, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|//----------------------------------------------------------------------- -| -|// Trap for not-yet-implemented parts. -|.macro NYI; tw 4, sp, sp; .endmacro -| -|.if FPU -|// int/FP conversions. -|.macro tonum_i, freg, reg -| xoris reg, reg, 0x8000 -| stw reg, TONUM_LO -| lfd freg, TONUM_D -| fsub freg, freg, TONUM -|.endmacro -| -|.macro tonum_u, freg, reg -| stw reg, TONUM_LO -| lfd freg, TONUM_D -| fsub freg, freg, TOBIT -|.endmacro -| -|.macro toint, reg, freg, tmpfreg -| fctiwz tmpfreg, freg -| stfd tmpfreg, TMPD -| lwz reg, TMPD_LO -|.endmacro -| -|.macro toint, reg, freg -| toint reg, freg, freg -|.endmacro -|.endif -| -|//----------------------------------------------------------------------- -| -|// Access to frame relative to BASE. -|.define FRAME_PC, -8 -|.define FRAME_FUNC, -4 -| -|// Instruction decode. -|.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro -|.macro decode_OP8, dst, ins; rlwinm dst, ins, 3, 21, 28; .endmacro -|.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro -|.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro -|.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro -|.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro -| -|.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro -|.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro -| -|// Instruction fetch. -|.macro ins_NEXT1 -| lwz INS, 0(PC) -| addi PC, PC, 4 -|.endmacro -|// Instruction decode+dispatch. Note: optimized for e300! -|.macro ins_NEXT2 -| decode_OPP TMP1, INS -| lpx TMP0, DISPATCH, TMP1 -| mtctr TMP0 -| decode_RB8 RB, INS -| decode_RD8 RD, INS -| decode_RA8 RA, INS -| decode_RC8 RC, INS -| bctr -|.endmacro -|.macro ins_NEXT -| ins_NEXT1 -| ins_NEXT2 -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -| .define ins_next1, ins_NEXT1 -| .define ins_next2, ins_NEXT2 -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| .macro ins_next -| b ->ins_next -| .endmacro -| .macro ins_next1 -| .endmacro -| .macro ins_next2 -| b ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC -| lwz PC, LFUNC:RB->pc -| lwz INS, 0(PC) -| addi PC, PC, 4 -| decode_OPP TMP1, INS -| decode_RA8 RA, INS -| lpx TMP0, DISPATCH, TMP1 -| add RA, RA, BASE -| mtctr TMP0 -| bctr -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC -| stw PC, FRAME_PC(BASE) -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checknum, reg; cmplw reg, TISNUM; .endmacro -|.macro checknum, cr, reg; cmplw cr, reg, TISNUM; .endmacro -|.macro checkstr, reg; cmpwi reg, LJ_TSTR; .endmacro -|.macro checktab, reg; cmpwi reg, LJ_TTAB; .endmacro -|.macro checkfunc, reg; cmpwi reg, LJ_TFUNC; .endmacro -|.macro checknil, reg; cmpwi reg, LJ_TNIL; .endmacro -| -|.macro branch_RD -| srwi TMP0, RD, 1 -| addis PC, PC, -(BCBIAS_J*4 >> 16) -| add PC, PC, TMP0 -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|.macro hotcheck, delta, target -| rlwinm TMP1, PC, 31, 25, 30 -| addi TMP1, TMP1, GG_DISP2HOT -| lhzx TMP2, DISPATCH, TMP1 -| addic. TMP2, TMP2, -delta -| sthx TMP2, DISPATCH, TMP1 -| blt target -|.endmacro -| -|.macro hotloop -| hotcheck HOTCOUNT_LOOP, ->vm_hotloop -|.endmacro -| -|.macro hotcall -| hotcheck HOTCOUNT_CALL, ->vm_hotcall -|.endmacro -| -|// Set current VM state. Uses TMP0. -|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro -|.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro -| -|// Move table write barrier back. Overwrites mark and tmp. -|.macro barrierback, tab, mark, tmp -| lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH) -| // Assumes LJ_GC_BLACK is 0x04. -| rlwinm mark, mark, 0, 30, 28 // black2gray(tab) -| stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH) -| stb mark, tab->marked -| stw tmp, tab->gclist -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | // See vm_return. Also: TMP2 = previous base. - | andix. TMP0, PC, FRAME_P - | li TMP1, LJ_TTRUE - | beq ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame. - | mr BASE, TMP2 // Restore caller base. - | // Prepending may overwrite the pcall frame, so do it at the end. - | stwu TMP1, FRAME_PC(RA) // Prepend true to results. - | - |->vm_returnc: - | addi RD, RD, 8 // RD = (nresults+1)*8. - | andix. TMP0, PC, FRAME_TYPE - | cmpwi cr1, RD, 0 - | li CRET1, LUA_YIELD - | beq cr1, ->vm_unwind_c_eh - | mr MULTRES, RD - | beq ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return - | // TMP0 = PC & FRAME_TYPE - | cmpwi TMP0, FRAME_C - | rlwinm TMP2, PC, 0, 0, 28 - | li_vmstate C - | sub TMP2, BASE, TMP2 // TMP2 = previous base. - | bney ->vm_returnp - | - | addic. TMP1, RD, -8 - | stp TMP2, L->base - | lwz TMP2, SAVE_NRES - | subi BASE, BASE, 8 - | st_vmstate - | slwi TMP2, TMP2, 3 - | beq >2 - |1: - | addic. TMP1, TMP1, -8 - |.if FPU - | lfd f0, 0(RA) - |.else - | lwz CARG1, 0(RA) - | lwz CARG2, 4(RA) - |.endif - | addi RA, RA, 8 - |.if FPU - | stfd f0, 0(BASE) - |.else - | stw CARG1, 0(BASE) - | stw CARG2, 4(BASE) - |.endif - | addi BASE, BASE, 8 - | bney <1 - | - |2: - | cmpw TMP2, RD // More/less results wanted? - | bne >6 - |3: - | stp BASE, L->top // Store new top. - | - |->vm_leave_cp: - | lp TMP0, SAVE_CFRAME // Restore previous C frame. - | li CRET1, 0 // Ok return status for vm_pcall. - | stp TMP0, L->cframe - | - |->vm_leave_unw: - | restoreregs - | blr - | - |6: - | ble >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | lwz TMP1, L->maxstack - | cmplw BASE, TMP1 - | bge >8 - | stw TISNIL, 0(BASE) - | addi RD, RD, 8 - | addi BASE, BASE, 8 - | b <2 - | - |7: // Less results wanted. - | subfic TMP3, TMP2, 0 // LUA_MULTRET+1 case? - | sub TMP0, RD, TMP2 - | subfe TMP1, TMP1, TMP1 // TMP1 = TMP2 == 0 ? 0 : -1 - | and TMP0, TMP0, TMP1 - | sub BASE, BASE, TMP0 // Either keep top or shrink it. - | b <3 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | stp BASE, L->top // Save current top held in BASE (yes). - | mr SAVE0, RD - | srwi CARG2, TMP2, 3 - | mr CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | lwz TMP2, SAVE_NRES - | mr RD, SAVE0 - | slwi TMP2, TMP2, 3 - | lp BASE, L->top // Need the (realloced) L->top in BASE. - | b <2 - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mr sp, CARG1 - | mr CRET1, CARG2 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | lwz L, SAVE_L - | .toc ld TOCREG, SAVE_TOC - | li TMP0, ~LJ_VMST_C - | lwz GL:TMP1, L->glref - | stw TMP0, GL:TMP1->vmstate - | b ->vm_leave_unw - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - |.if GPR64 - | rldicr sp, CARG1, 0, 61 - |.else - | rlwinm sp, CARG1, 0, 0, 29 - |.endif - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | lwz L, SAVE_L - | .toc ld TOCREG, SAVE_TOC - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp BASE, L->base - | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | li ZERO, 0 - | .FPU stw TMP3, TMPD - | li TMP1, LJ_TFALSE - | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | li TISNIL, LJ_TNIL - | li_vmstate INTERP - | .FPU lfs TOBIT, TMPD - | lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame. - | la RA, -8(BASE) // Results start at BASE-8. - | .FPU stw TMP3, TMPD - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw TMP1, 0(RA) // Prepend false to error message. - | li RD, 16 // 2 results: false + error message. - | st_vmstate - | .FPU lfs TONUM, TMPD - | b ->vm_returnc - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | li CARG2, LUA_MINSTACK - | b >2 - | - |->vm_growstack_l: // Grow stack for Lua function. - | // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC - | add RC, BASE, RC - | sub RA, RA, BASE - | stp BASE, L->base - | addi PC, PC, 4 // Must point after first instruction. - | stp RC, L->top - | srwi CARG2, RA, 3 - |2: - | // L->base = new base, L->top = top - | stw PC, SAVE_PC - | mr CARG1, L - | bl extern lj_state_growstack // (lua_State *L, int n) - | lp BASE, L->base - | lp RC, L->top - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | sub RC, RC, BASE - | // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mr L, CARG1 - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | mr BASE, CARG2 - | lbz TMP1, L->status - | stw L, SAVE_L - | li PC, FRAME_CP - | addi TMP0, sp, CFRAME_RESUME - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw CARG3, SAVE_NRES - | cmplwi TMP1, 0 - | stw CARG3, SAVE_ERRF - | stp CARG3, SAVE_CFRAME - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | stp TMP0, L->cframe - | beq >3 - | - | // Resume after yield (like a return). - | stw L, DISPATCH_GL(cur_L)(DISPATCH) - | mr RA, BASE - | lp BASE, L->base - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp TMP1, L->top - | lwz PC, FRAME_PC(BASE) - | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | stb CARG3, L->status - | .FPU stw TMP3, TMPD - | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | .FPU lfs TOBIT, TMPD - | sub RD, TMP1, BASE - | .FPU stw TMP3, TMPD - | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | addi RD, RD, 8 - | .FPU stw TMP0, TONUM_HI - | li_vmstate INTERP - | li ZERO, 0 - | st_vmstate - | andix. TMP0, PC, FRAME_TYPE - | mr MULTRES, RD - | .FPU lfs TONUM, TMPD - | li TISNIL, LJ_TNIL - | beq ->BC_RET_Z - | b ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | li PC, FRAME_CP - | stw CARG4, SAVE_ERRF - | b >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | li PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | lp TMP1, L:CARG1->cframe - | mr L, CARG1 - | stw CARG3, SAVE_NRES - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | stw CARG1, SAVE_L - | mr BASE, CARG2 - | addi DISPATCH, DISPATCH, GG_G2DISP - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | stp TMP1, SAVE_CFRAME - | stp sp, L->cframe // Add our C frame to cframe chain. - | - |3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype). - | stw L, DISPATCH_GL(cur_L)(DISPATCH) - | lp TMP2, L->base // TMP2 = old base (used in vmeta_call). - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp TMP1, L->top - | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | add PC, PC, BASE - | .FPU stw TMP3, TMPD - | li ZERO, 0 - | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | .FPU lfs TOBIT, TMPD - | sub PC, PC, TMP2 // PC = frame delta + frame type - | .FPU stw TMP3, TMPD - | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | sub NARGS8:RC, TMP1, BASE - | .FPU stw TMP0, TONUM_HI - | li_vmstate INTERP - | .FPU lfs TONUM, TMPD - | li TISNIL, LJ_TNIL - | st_vmstate - | - |->vm_call_dispatch: - | // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC - | lwz TMP0, FRAME_PC(BASE) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | checkfunc TMP0; bne ->vmeta_call - | - |->vm_call_dispatch_f: - | ins_call - | // BASE = new base, RB = func, RC = nargs*8, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mr L, CARG1 - | lwz TMP0, L:CARG1->stack - | stw CARG1, SAVE_L - | lp TMP1, L->top - | lwz DISPATCH, L->glref // Setup pointer to dispatch table. - | stw CARG1, SAVE_PC // Any value outside of bytecode is ok. - | sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top). - | lp TMP1, L->cframe - | addi DISPATCH, DISPATCH, GG_G2DISP - | .toc lp CARG4, 0(CARG4) - | li TMP2, 0 - | stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame. - | stw TMP2, SAVE_ERRF // No error function. - | stp TMP1, SAVE_CFRAME - | stp sp, L->cframe // Add our C frame to cframe chain. - | stw L, DISPATCH_GL(cur_L)(DISPATCH) - | mtctr CARG4 - | bctrl // (lua_State *L, lua_CFunction func, void *ud) - |.if PPE - | mr BASE, CRET1 - | cmpwi CRET1, 0 - |.else - | mr. BASE, CRET1 - |.endif - | li PC, FRAME_CP - | bne <3 // Else continue with the call. - | b ->vm_leave_cp // No base? Just remove C frame. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the - |// stack, so BASE doesn't need to be reloaded across these calls. - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultptr, RD = (nresults+1)*8 - | lwz TMP0, -12(BASE) // Continuation. - | mr RB, BASE - | mr BASE, TMP2 // Restore caller BASE. - | lwz LFUNC:TMP1, FRAME_FUNC(TMP2) - |.if FFI - | cmplwi TMP0, 1 - |.endif - | lwz PC, -16(RB) // Restore PC from [cont|PC]. - | subi TMP2, RD, 8 - | stwx TISNIL, RA, TMP2 // Ensure one valid arg. - |.if FFI - | ble >1 - |.endif - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | // BASE = base, RA = resultptr, RB = meta base - | mtctr TMP0 - | bctr // Jump to continuation. - | - |.if FFI - |1: - | beq ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: tailcall from C function. - | subi TMP1, RB, 16 - | sub RC, TMP1, BASE - | b ->vm_call_tail - |.endif - | - |->cont_cat: // RA = resultptr, RB = meta base - | lwz INS, -4(PC) - | subi CARG2, RB, 16 - | decode_RB8 SAVE0, INS - |.if FPU - | lfd f0, 0(RA) - |.else - | lwz TMP2, 0(RA) - | lwz TMP3, 4(RA) - |.endif - | add TMP1, BASE, SAVE0 - | stp BASE, L->base - | cmplw TMP1, CARG2 - | sub CARG3, CARG2, TMP1 - | decode_RA8 RA, INS - |.if FPU - | stfd f0, 0(CARG2) - |.else - | stw TMP2, 0(CARG2) - | stw TMP3, 4(CARG2) - |.endif - | bney ->BC_CAT_Z - |.if FPU - | stfdx f0, BASE, RA - |.else - | stwux TMP2, RA, BASE - | stw TMP3, 4(RA) - |.endif - | b ->cont_nop - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets1: - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TSTR - | decode_RB8 RB, INS - | stw STR:RC, 4(CARG3) - | add CARG2, BASE, RB - | stw TMP0, 0(CARG3) - | b >1 - | - |->vmeta_tgets: - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TTAB - | stw TAB:RB, 4(CARG2) - | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH) - | stw TMP0, 0(CARG2) - | li TMP1, LJ_TSTR - | stw STR:RC, 4(CARG3) - | stw TMP1, 0(CARG3) - | b >1 - | - |->vmeta_tgetb: // TMP0 = index - |.if not DUALNUM - | tonum_u f0, TMP0 - |.endif - | decode_RB8 RB, INS - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | add CARG2, BASE, RB - |.if DUALNUM - | stw TISNUM, 0(CARG3) - | stw TMP0, 4(CARG3) - |.else - | stfd f0, 0(CARG3) - |.endif - | b >1 - | - |->vmeta_tgetv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cmplwi CRET1, 0 - | beq >3 - |.if FPU - | lfd f0, 0(CRET1) - |.else - | lwz TMP0, 0(CRET1) - | lwz TMP1, 4(CRET1) - |.endif - | ins_next1 - |.if FPU - | stfdx f0, BASE, RA - |.else - | stwux TMP0, RA, BASE - | stw TMP1, 4(RA) - |.endif - | ins_next2 - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | subfic TMP1, BASE, FRAME_CONT - | lp BASE, L->top - | stw PC, -16(BASE) // [cont|PC] - | add PC, TMP1, BASE - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | li NARGS8:RC, 16 // 2 args for func(t, k). - | b ->vm_call_dispatch_f - | - |->vmeta_tgetr: - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | cmplwi CRET1, 0 - | beq >1 - |.if FPU - | lfd f14, 0(CRET1) - |.else - | lwz SAVE0, 0(CRET1) - | lwz SAVE1, 4(CRET1) - |.endif - | b ->BC_TGETR_Z - |1: - | stwx TISNIL, BASE, RA - | b ->cont_nop - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets1: - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TSTR - | decode_RB8 RB, INS - | stw STR:RC, 4(CARG3) - | add CARG2, BASE, RB - | stw TMP0, 0(CARG3) - | b >1 - | - |->vmeta_tsets: - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - | li TMP0, LJ_TTAB - | stw TAB:RB, 4(CARG2) - | la CARG3, DISPATCH_GL(tmptv2)(DISPATCH) - | stw TMP0, 0(CARG2) - | li TMP1, LJ_TSTR - | stw STR:RC, 4(CARG3) - | stw TMP1, 0(CARG3) - | b >1 - | - |->vmeta_tsetb: // TMP0 = index - |.if not DUALNUM - | tonum_u f0, TMP0 - |.endif - | decode_RB8 RB, INS - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | add CARG2, BASE, RB - |.if DUALNUM - | stw TISNUM, 0(CARG3) - | stw TMP0, 4(CARG3) - |.else - | stfd f0, 0(CARG3) - |.endif - | b >1 - | - |->vmeta_tsetv: - | decode_RB8 RB, INS - | decode_RC8 RC, INS - | add CARG2, BASE, RB - | add CARG3, BASE, RC - |1: - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // Returns TValue * (finished) or NULL (metamethod). - | cmplwi CRET1, 0 - |.if FPU - | lfdx f0, BASE, RA - |.else - | lwzux TMP2, RA, BASE - | lwz TMP3, 4(RA) - |.endif - | beq >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | ins_next1 - |.if FPU - | stfd f0, 0(CRET1) - |.else - | stw TMP2, 0(CRET1) - | stw TMP3, 4(CRET1) - |.endif - | ins_next2 - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | subfic TMP1, BASE, FRAME_CONT - | lp BASE, L->top - | stw PC, -16(BASE) // [cont|PC] - | add PC, TMP1, BASE - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | li NARGS8:RC, 24 // 3 args for func(t, k, v) - |.if FPU - | stfd f0, 16(BASE) // Copy value to third argument. - |.else - | stw TMP2, 16(BASE) - | stw TMP3, 20(BASE) - |.endif - | b ->vm_call_dispatch_f - | - |->vmeta_tsetr: - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - | // Returns TValue *. - |.if FPU - | stfd f14, 0(CRET1) - |.else - | stw SAVE0, 0(CRET1) - | stw SAVE1, 4(CRET1) - |.endif - | b ->cont_nop - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | mr CARG1, L - | subi PC, PC, 4 - |.if DUALNUM - | mr CARG2, RA - |.else - | add CARG2, BASE, RA - |.endif - | stw PC, SAVE_PC - |.if DUALNUM - | mr CARG3, RD - |.else - | add CARG3, BASE, RD - |.endif - | stp BASE, L->base - | decode_OP1 CARG4, INS - | bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // Returns 0/1 or TValue * (metamethod). - |3: - | cmplwi CRET1, 1 - | bgt ->vmeta_binop - | subfic CRET1, CRET1, 0 - |4: - | lwz INS, 0(PC) - | addi PC, PC, 4 - | decode_RD4 TMP2, INS - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | and TMP2, TMP2, CRET1 - | add PC, PC, TMP2 - |->cont_nop: - | ins_next - | - |->cont_ra: // RA = resultptr - | lwz INS, -4(PC) - |.if FPU - | lfd f0, 0(RA) - |.else - | lwz CARG1, 0(RA) - | lwz CARG2, 4(RA) - |.endif - | decode_RA8 TMP1, INS - |.if FPU - | stfdx f0, BASE, TMP1 - |.else - | stwux CARG1, TMP1, BASE - | stw CARG2, 4(TMP1) - |.endif - | b ->cont_nop - | - |->cont_condt: // RA = resultptr - | lwz TMP0, 0(RA) - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is true. - | subfe CRET1, CRET1, CRET1 - | not CRET1, CRET1 - | b <4 - | - |->cont_condf: // RA = resultptr - | lwz TMP0, 0(RA) - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TTRUE // Branch if result is false. - | subfe CRET1, CRET1, CRET1 - | b <4 - | - |->vmeta_equal: - | // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV. - | subi PC, PC, 4 - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - | - |->vmeta_equal_cd: - |.if FFI - | mr CARG2, INS - | subi PC, PC, 4 - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_equal_cd // (lua_State *L, BCIns op) - | // Returns 0/1 or TValue * (metamethod). - | b <3 - |.endif - | - |->vmeta_istype: - | subi PC, PC, 4 - | stp BASE, L->base - | srwi CARG2, RA, 3 - | mr CARG1, L - | srwi CARG3, RD, 3 - | stw PC, SAVE_PC - | bl extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - | b ->cont_nop - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_nv: - | add CARG3, KBASE, RC - | add CARG4, BASE, RB - | b >1 - |->vmeta_arith_nv2: - |.if DUALNUM - | mr CARG3, RC - | mr CARG4, RB - | b >1 - |.endif - | - |->vmeta_unm: - | mr CARG3, RD - | mr CARG4, RD - | b >1 - | - |->vmeta_arith_vn: - | add CARG3, BASE, RB - | add CARG4, KBASE, RC - | b >1 - | - |->vmeta_arith_vv: - | add CARG3, BASE, RB - | add CARG4, BASE, RC - |.if DUALNUM - | b >1 - |.endif - |->vmeta_arith_vn2: - |->vmeta_arith_vv2: - |.if DUALNUM - | mr CARG3, RB - | mr CARG4, RC - |.endif - |1: - | add CARG2, BASE, RA - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS. - | bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // Returns NULL (finished) or TValue * (metamethod). - | cmplwi CRET1, 0 - | beq ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2 - | sub TMP1, CRET1, BASE - | stw PC, -16(CRET1) // [cont|PC] - | mr TMP2, BASE - | addi PC, TMP1, FRAME_CONT - | mr BASE, CRET1 - | li NARGS8:RC, 16 // 2 args for func(o1, o2). - | b ->vm_call_dispatch - | - |->vmeta_len: -#if LJ_52 - | mr SAVE0, CARG1 -#endif - | mr CARG2, RD - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | bl extern lj_meta_len // (lua_State *L, TValue *o) - | // Returns NULL (retry) or TValue * (metamethod base). -#if LJ_52 - | cmplwi CRET1, 0 - | bne ->vmeta_binop // Binop call for compatibility. - | mr CARG1, SAVE0 - | b ->BC_LEN_Z -#else - | b ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call: // Resolve and call __call metamethod. - | // TMP2 = old base, BASE = new base, RC = nargs*8 - | mr CARG1, L - | stp TMP2, L->base // This is the callers base! - | subi CARG2, BASE, 8 - | stw PC, SAVE_PC - | add CARG3, BASE, RC - | mr SAVE0, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here. - | addi NARGS8:RC, SAVE0, 8 // Got one more argument now. - | ins_call - | - |->vmeta_callt: // Resolve __call for BC_CALLT. - | // BASE = old base, RA = new base, RC = nargs*8 - | mr CARG1, L - | stp BASE, L->base - | subi CARG2, RA, 8 - | stw PC, SAVE_PC - | add CARG3, RA, RC - | mr SAVE0, NARGS8:RC - | bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | lwz TMP1, FRAME_PC(BASE) - | addi NARGS8:RC, SAVE0, 8 // Got one more argument now. - | lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here. - | b ->BC_CALLT_Z - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mr CARG1, L - | stp BASE, L->base - | mr CARG2, RA - | stw PC, SAVE_PC - | mr SAVE0, INS - | bl extern lj_meta_for // (lua_State *L, TValue *base) - |.if JIT - | decode_OP1 TMP0, SAVE0 - |.endif - | decode_RA8 RA, SAVE0 - |.if JIT - | cmpwi TMP0, BC_JFORI - |.endif - | decode_RD8 RD, SAVE0 - |.if JIT - | beqy =>BC_JFORI - |.endif - | b =>BC_FORI - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz CARG1, 4(BASE) - | blt ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 0(BASE) - | lwz CARG4, 8(BASE) - | lwz CARG1, 4(BASE) - | lwz CARG2, 12(BASE) - | blt ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - |.if FPU - | lfd FARG1, 0(BASE) - |.else - | lwz CARG2, 4(BASE) - |.endif - | blt ->fff_fallback - | checknum CARG1; bge ->fff_fallback - |.endmacro - | - |.macro .ffunc_nn, name - |->ff_ .. name: - | cmplwi NARGS8:RC, 16 - | lwz CARG1, 0(BASE) - |.if FPU - | lfd FARG1, 0(BASE) - | lwz CARG3, 8(BASE) - | lfd FARG2, 8(BASE) - |.else - | lwz CARG2, 4(BASE) - | lwz CARG3, 8(BASE) - | lwz CARG4, 12(BASE) - |.endif - | blt ->fff_fallback - | checknum CARG1; bge ->fff_fallback - | checknum CARG3; bge ->fff_fallback - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses TMP0 and TMP1. - |.macro ffgccheck - | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | cmplw TMP0, TMP1 - | bgel ->fff_gcstep - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | li TMP1, LJ_TFALSE - | la RA, -8(BASE) - | cmplw cr1, CARG3, TMP1 - | lwz PC, FRAME_PC(BASE) - | bge cr1, ->fff_fallback - | stw CARG3, 0(RA) - | addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8. - | addi TMP1, BASE, 8 - | add TMP2, RA, NARGS8:RC - | stw CARG1, 4(RA) - | beq ->fff_res // Done if exactly 1 argument. - |1: - | cmplw TMP1, TMP2 - |.if FPU - | lfd f0, 0(TMP1) - | stfd f0, 0(TMP1) - |.else - | lwz CARG1, 0(TMP1) - | lwz CARG2, 4(TMP1) - | stw CARG1, -8(TMP1) - | stw CARG2, -4(TMP1) - |.endif - | addi TMP1, TMP1, 8 - | bney <1 - | b ->fff_res - | - |.ffunc type - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - | blt ->fff_fallback - | .gpr64 extsw CARG1, CARG1 - | subfc TMP0, TISNUM, CARG1 - | subfe TMP2, CARG1, CARG1 - | orc TMP1, TMP2, TMP0 - | addi TMP1, TMP1, ~LJ_TISNUM+1 - | slwi TMP1, TMP1, 3 - |.if FPU - | la TMP2, CFUNC:RB->upvalue - | lfdx FARG1, TMP2, TMP1 - |.else - | add TMP1, CFUNC:RB, TMP1 - | lwz CARG1, CFUNC:TMP1->upvalue[0].u32.hi - | lwz CARG2, CFUNC:TMP1->upvalue[0].u32.lo - |.endif - | b ->fff_resn - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | checktab CARG3; bne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | lwz TAB:CARG1, TAB:CARG1->metatable - |2: - | li CARG3, LJ_TNIL - | cmplwi TAB:CARG1, 0 - | lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH) - | beq ->fff_restv - | lwz TMP0, TAB:CARG1->hmask - | li CARG3, LJ_TTAB // Use metatable as default result. - | lwz TMP1, STR:RC->sid - | lwz NODE:TMP2, TAB:CARG1->node - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |3: // Rearranged logic, because we expect _not_ to find the key. - | lwz CARG4, NODE:TMP2->key - | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2) - | lwz CARG2, NODE:TMP2->val - | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2) - | checkstr CARG4; bne >4 - | cmpw TMP0, STR:RC; beq >5 - |4: - | lwz NODE:TMP2, NODE:TMP2->next - | cmplwi NODE:TMP2, 0 - | beq ->fff_restv // Not found, keep default result. - | b <3 - |5: - | checknil CARG2 - | beq ->fff_restv // Ditto for nil value. - | mr CARG3, CARG2 // Return value of mt.__metatable. - | mr CARG1, TMP1 - | b ->fff_restv - | - |6: - | cmpwi CARG3, LJ_TUDATA; beq <1 - | .gpr64 extsw CARG3, CARG3 - | subfc TMP0, TISNUM, CARG3 - | subfe TMP2, CARG3, CARG3 - | orc TMP1, TMP2, TMP0 - | addi TMP1, TMP1, ~LJ_TISNUM+1 - | slwi TMP1, TMP1, 2 - | la TMP2, DISPATCH_GL(gcroot[GCROOT_BASEMT])(DISPATCH) - | lwzx TAB:CARG1, TMP2, TMP1 - | b <2 - | - |.ffunc_2 setmetatable - | // Fast path: no mt for table yet and not clearing the mt. - | checktab CARG3; bne ->fff_fallback - | lwz TAB:TMP1, TAB:CARG1->metatable - | checktab CARG4; bne ->fff_fallback - | cmplwi TAB:TMP1, 0 - | lbz TMP3, TAB:CARG1->marked - | bne ->fff_fallback - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - | stw TAB:CARG2, TAB:CARG1->metatable - | beq ->fff_restv - | barrierback TAB:CARG1, TMP3, TMP0 - | b ->fff_restv - | - |.ffunc rawget - | cmplwi NARGS8:RC, 16 - | lwz CARG4, 0(BASE) - | lwz TAB:CARG2, 4(BASE) - | blt ->fff_fallback - | checktab CARG4; bne ->fff_fallback - | la CARG3, 8(BASE) - | mr CARG1, L - | bl extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // Returns cTValue *. - |.if FPU - | lfd FARG1, 0(CRET1) - |.else - | lwz CARG2, 4(CRET1) - | lwz CARG1, 0(CRET1) // Caveat: CARG1 == CRET1. - |.endif - | b ->fff_resn - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - |.if FPU - | lfd FARG1, 0(BASE) - |.else - | lwz CARG2, 4(BASE) - |.endif - | bne ->fff_fallback // Exactly one argument. - | checknum CARG1; bgt ->fff_fallback - | b ->fff_resn - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | checkstr CARG3 - | // A __tostring method in the string base metatable is ignored. - | beq ->fff_restv // String key? - | // Handle numbers inline, unless a number base metatable is present. - | lwz TMP0, DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])(DISPATCH) - | checknum CARG3 - | cmplwi cr1, TMP0, 0 - | stp BASE, L->base // Add frame since C call can throw. - | crorc 4*cr0+eq, 4*cr0+gt, 4*cr1+eq - | stw PC, SAVE_PC // Redundant (but a defined value). - | beq ->fff_fallback - | ffgccheck - | mr CARG1, L - | mr CARG2, BASE - |.if DUALNUM - | bl extern lj_strfmt_number // (lua_State *L, cTValue *o) - |.else - | bl extern lj_strfmt_num // (lua_State *L, lua_Number *np) - |.endif - | // Returns GCstr *. - | li CARG3, LJ_TSTR - | b ->fff_restv - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | stwx TISNIL, BASE, NARGS8:RC // Set missing 2nd arg to nil. - | checktab CARG3 - | lwz PC, FRAME_PC(BASE) - | bne ->fff_fallback - | la CARG2, 8(BASE) - | la CARG3, -8(BASE) - | bl extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - | // Returns 1=found, 0=end, -1=error. - | cmpwi CRET1, 0 - | la RA, -8(BASE) - | li RD, (2+1)*8 - | bgt ->fff_res // Found key/value. - | li CARG3, LJ_TNIL - | beq ->fff_restv // End of traversal: return nil. - | lwz CFUNC:RB, FRAME_FUNC(BASE) - | li NARGS8:RC, 2*8 - | b ->fff_fallback // Invalid key. - | - |.ffunc_1 pairs - | checktab CARG3 - | lwz PC, FRAME_PC(BASE) - | bne ->fff_fallback -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - |.if FPU - | lfd f0, CFUNC:RB->upvalue[0] - |.else - | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi - | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo - |.endif - | cmplwi TAB:TMP2, 0 - | la RA, -8(BASE) - | bne ->fff_fallback -#else - |.if FPU - | lfd f0, CFUNC:RB->upvalue[0] - |.else - | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi - | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo - |.endif - | la RA, -8(BASE) -#endif - | stw TISNIL, 8(BASE) - | li RD, (3+1)*8 - |.if FPU - | stfd f0, 0(RA) - |.else - | stw TMP0, 0(RA) - | stw TMP1, 4(RA) - |.endif - | b ->fff_res - | - |.ffunc ipairs_aux - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 0(BASE) - | lwz TAB:CARG1, 4(BASE) - | lwz CARG4, 8(BASE) - |.if DUALNUM - | lwz TMP2, 12(BASE) - |.else - | lfd FARG2, 8(BASE) - |.endif - | blt ->fff_fallback - | checktab CARG3 - | checknum cr1, CARG4 - | lwz PC, FRAME_PC(BASE) - |.if DUALNUM - | bne ->fff_fallback - | bne cr1, ->fff_fallback - |.else - | lus TMP0, 0x3ff0 - | stw ZERO, TMPD_LO - | bne ->fff_fallback - | stw TMP0, TMPD_HI - | bge cr1, ->fff_fallback - | lfd FARG1, TMPD - | toint TMP2, FARG2, f0 - |.endif - | lwz TMP0, TAB:CARG1->asize - | lwz TMP1, TAB:CARG1->array - |.if not DUALNUM - | fadd FARG2, FARG2, FARG1 - |.endif - | addi TMP2, TMP2, 1 - | la RA, -8(BASE) - | cmplw TMP0, TMP2 - |.if DUALNUM - | stw TISNUM, 0(RA) - | slwi TMP3, TMP2, 3 - | stw TMP2, 4(RA) - |.else - | slwi TMP3, TMP2, 3 - | stfd FARG2, 0(RA) - |.endif - | ble >2 // Not in array part? - |.if FPU - | lwzx TMP2, TMP1, TMP3 - | lfdx f0, TMP1, TMP3 - |.else - | lwzux TMP2, TMP1, TMP3 - | lwz TMP3, 4(TMP1) - |.endif - |1: - | checknil TMP2 - | li RD, (0+1)*8 - | beq ->fff_res // End of iteration, return 0 results. - | li RD, (2+1)*8 - |.if FPU - | stfd f0, 8(RA) - |.else - | stw TMP2, 8(RA) - | stw TMP3, 12(RA) - |.endif - | b ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | lwz TMP0, TAB:CARG1->hmask - | cmplwi TMP0, 0 - | li RD, (0+1)*8 - | beq ->fff_res - | mr CARG2, TMP2 - | bl extern lj_tab_getinth // (GCtab *t, int32_t key) - | // Returns cTValue * or NULL. - | cmplwi CRET1, 0 - | li RD, (0+1)*8 - | beq ->fff_res - | lwz TMP2, 0(CRET1) - |.if FPU - | lfd f0, 0(CRET1) - |.else - | lwz TMP3, 4(CRET1) - |.endif - | b <1 - | - |.ffunc_1 ipairs - | checktab CARG3 - | lwz PC, FRAME_PC(BASE) - | bne ->fff_fallback -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - |.if FPU - | lfd f0, CFUNC:RB->upvalue[0] - |.else - | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi - | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo - |.endif - | cmplwi TAB:TMP2, 0 - | la RA, -8(BASE) - | bne ->fff_fallback -#else - |.if FPU - | lfd f0, CFUNC:RB->upvalue[0] - |.else - | lwz TMP0, CFUNC:RB->upvalue[0].u32.hi - | lwz TMP1, CFUNC:RB->upvalue[0].u32.lo - |.endif - | la RA, -8(BASE) -#endif - |.if DUALNUM - | stw TISNUM, 8(BASE) - |.else - | stw ZERO, 8(BASE) - |.endif - | stw ZERO, 12(BASE) - | li RD, (3+1)*8 - |.if FPU - | stfd f0, 0(RA) - |.else - | stw TMP0, 0(RA) - | stw TMP1, 4(RA) - |.endif - | b ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc pcall - | cmplwi NARGS8:RC, 8 - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | blt ->fff_fallback - | mr TMP2, BASE - | la BASE, 8(BASE) - | // Remember active hook before pcall. - | rlwinm TMP3, TMP3, 32-HOOK_ACTIVE_SHIFT, 31, 31 - | subi NARGS8:RC, NARGS8:RC, 8 - | addi PC, TMP3, 8+FRAME_PCALL - | b ->vm_call_dispatch - | - |.ffunc xpcall - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 8(BASE) - |.if FPU - | lfd FARG2, 8(BASE) - | lfd FARG1, 0(BASE) - |.else - | lwz CARG1, 0(BASE) - | lwz CARG2, 4(BASE) - | lwz CARG4, 12(BASE) - |.endif - | blt ->fff_fallback - | lbz TMP1, DISPATCH_GL(hookmask)(DISPATCH) - | mr TMP2, BASE - | checkfunc CARG3; bne ->fff_fallback // Traceback must be a function. - | la BASE, 16(BASE) - | // Remember active hook before pcall. - | rlwinm TMP1, TMP1, 32-HOOK_ACTIVE_SHIFT, 31, 31 - |.if FPU - | stfd FARG2, 0(TMP2) // Swap function and traceback. - | stfd FARG1, 8(TMP2) - |.else - | stw CARG3, 0(TMP2) - | stw CARG4, 4(TMP2) - | stw CARG1, 8(TMP2) - | stw CARG2, 12(TMP2) - |.endif - | subi NARGS8:RC, NARGS8:RC, 16 - | addi PC, TMP1, 16+FRAME_PCALL - | b ->vm_call_dispatch - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | cmpwi CARG3, LJ_TTHREAD; bne ->fff_fallback - |.else - |.ffunc coroutine_wrap_aux - | lwz L:CARG1, CFUNC:RB->upvalue[0].gcr - |.endif - | lbz TMP0, L:CARG1->status - | lp TMP1, L:CARG1->cframe - | lp CARG2, L:CARG1->top - | cmplwi cr0, TMP0, LUA_YIELD - | lp TMP2, L:CARG1->base - | cmplwi cr1, TMP1, 0 - | lwz TMP0, L:CARG1->maxstack - | cmplw cr7, CARG2, TMP2 - | lwz PC, FRAME_PC(BASE) - | crorc 4*cr6+lt, 4*cr0+gt, 4*cr1+eq // st>LUA_YIELD || cframe!=0 - | add TMP2, CARG2, NARGS8:RC - | crandc 4*cr6+gt, 4*cr7+eq, 4*cr0+eq // base==top && st!=LUA_YIELD - | cmplw cr1, TMP2, TMP0 - | cror 4*cr6+lt, 4*cr6+lt, 4*cr6+gt - | stw PC, SAVE_PC - | cror 4*cr6+lt, 4*cr6+lt, 4*cr1+gt // cond1 || cond2 || stackov - | stp BASE, L->base - | blt cr6, ->fff_fallback - |1: - |.if resume - | addi BASE, BASE, 8 // Keep resumed thread in stack for GC. - | subi NARGS8:RC, NARGS8:RC, 8 - | subi TMP2, TMP2, 8 - |.endif - | stp TMP2, L:CARG1->top - | li TMP1, 0 - | stp BASE, L->top - |2: // Move args to coroutine. - | cmpw TMP1, NARGS8:RC - |.if FPU - | lfdx f0, BASE, TMP1 - |.else - | add CARG3, BASE, TMP1 - | lwz TMP2, 0(CARG3) - | lwz TMP3, 4(CARG3) - |.endif - | beq >3 - |.if FPU - | stfdx f0, CARG2, TMP1 - |.else - | add CARG3, CARG2, TMP1 - | stw TMP2, 0(CARG3) - | stw TMP3, 4(CARG3) - |.endif - | addi TMP1, TMP1, 8 - | b <2 - |3: - | li CARG3, 0 - | mr L:SAVE0, L:CARG1 - | li CARG4, 0 - | bl ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | // Returns thread status. - |4: - | lp TMP2, L:SAVE0->base - | cmplwi CRET1, LUA_YIELD - | lp TMP3, L:SAVE0->top - | li_vmstate INTERP - | lp BASE, L->base - | stw L, DISPATCH_GL(cur_L)(DISPATCH) - | st_vmstate - | bgt >8 - | sub RD, TMP3, TMP2 - | lwz TMP0, L->maxstack - | cmplwi RD, 0 - | add TMP1, BASE, RD - | beq >6 // No results? - | cmplw TMP1, TMP0 - | li TMP1, 0 - | bgt >9 // Need to grow stack? - | - | subi TMP3, RD, 8 - | stp TMP2, L:SAVE0->top // Clear coroutine stack. - |5: // Move results from coroutine. - | cmplw TMP1, TMP3 - |.if FPU - | lfdx f0, TMP2, TMP1 - | stfdx f0, BASE, TMP1 - |.else - | add CARG3, TMP2, TMP1 - | lwz CARG1, 0(CARG3) - | lwz CARG2, 4(CARG3) - | add CARG3, BASE, TMP1 - | stw CARG1, 0(CARG3) - | stw CARG2, 4(CARG3) - |.endif - | addi TMP1, TMP1, 8 - | bne <5 - |6: - | andix. TMP0, PC, FRAME_TYPE - |.if resume - | li TMP1, LJ_TTRUE - | la RA, -8(BASE) - | stw TMP1, -8(BASE) // Prepend true to results. - | addi RD, RD, 16 - |.else - | mr RA, BASE - | addi RD, RD, 8 - |.endif - |7: - | stw PC, SAVE_PC - | mr MULTRES, RD - | beq ->BC_RET_Z - | b ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | andix. TMP0, PC, FRAME_TYPE - | la TMP3, -8(TMP3) - | li TMP1, LJ_TFALSE - |.if FPU - | lfd f0, 0(TMP3) - |.else - | lwz CARG1, 0(TMP3) - | lwz CARG2, 4(TMP3) - |.endif - | stp TMP3, L:SAVE0->top // Remove error from coroutine stack. - | li RD, (2+1)*8 - | stw TMP1, -8(BASE) // Prepend false to results. - | la RA, -8(BASE) - |.if FPU - | stfd f0, 0(BASE) // Copy error message. - |.else - | stw CARG1, 0(BASE) // Copy error message. - | stw CARG2, 4(BASE) - |.endif - | b <7 - |.else - | mr CARG1, L - | mr CARG2, L:SAVE0 - | bl extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - |.endif - | - |9: // Handle stack expansion on return from yield. - | mr CARG1, L - | srwi CARG2, RD, 3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | li CRET1, 0 - | b <4 - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | lp TMP0, L->cframe - | add TMP1, BASE, NARGS8:RC - | stp BASE, L->base - | andix. TMP0, TMP0, CFRAME_RESUME - | stp TMP1, L->top - | li CRET1, LUA_YIELD - | beq ->fff_fallback - | stp ZERO, L->cframe - | stb CRET1, L->status - | b ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.ffunc_1 math_abs - | checknum CARG3 - |.if DUALNUM - | bne >2 - | srawi TMP1, CARG1, 31 - | xor TMP2, TMP1, CARG1 - |.if GPR64 - | lus TMP0, 0x8000 - | sub CARG1, TMP2, TMP1 - | cmplw CARG1, TMP0 - | beq >1 - |.else - | sub. CARG1, TMP2, TMP1 - | blt >1 - |.endif - |->fff_resi: - | lwz PC, FRAME_PC(BASE) - | la RA, -8(BASE) - | stw TISNUM, -8(BASE) - | stw CRET1, -4(BASE) - | b ->fff_res1 - |1: - | lus CARG3, 0x41e0 // 2^31. - | li CARG1, 0 - | b ->fff_restv - |2: - |.endif - | bge ->fff_fallback - | rlwinm CARG3, CARG3, 0, 1, 31 - | // Fallthrough. - | - |->fff_restv: - | // CARG3/CARG1 = TValue result. - | lwz PC, FRAME_PC(BASE) - | stw CARG3, -8(BASE) - | la RA, -8(BASE) - | stw CARG1, -4(BASE) - |->fff_res1: - | // RA = results, PC = return. - | li RD, (1+1)*8 - |->fff_res: - | // RA = results, RD = (nresults+1)*8, PC = return. - | andix. TMP0, PC, FRAME_TYPE - | mr MULTRES, RD - | bney ->vm_return - | lwz INS, -4(PC) - | decode_RB8 RB, INS - |5: - | cmplw RB, RD // More results expected? - | decode_RA8 TMP0, INS - | bgt >6 - | ins_next1 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | sub BASE, RA, TMP0 - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | stwx TISNIL, RA, TMP1 - | b <5 - | - |.macro math_extern, func - | .ffunc_n math_ .. func - | blex func - | b ->fff_resn - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - | blex func - | b ->fff_resn - |.endmacro - | - |.macro math_round, func - | .ffunc_1 math_ .. func - | checknum CARG3; beqy ->fff_restv - | rlwinm TMP2, CARG3, 12, 21, 31 - | bge ->fff_fallback - | addic. TMP2, TMP2, -1023 // exp = exponent(x) - 1023 - | cmplwi cr1, TMP2, 31 // 0 <= exp < 31? - | subfic TMP0, TMP2, 31 - | blt >3 - | slwi TMP1, CARG3, 11 - | srwi TMP3, CARG1, 21 - | oris TMP1, TMP1, 0x8000 - | addi TMP2, TMP2, 1 - | or TMP1, TMP1, TMP3 - | slwi CARG2, CARG1, 11 - | bge cr1, >4 - | slw TMP3, TMP1, TMP2 - | srw RD, TMP1, TMP0 - | or TMP3, TMP3, CARG2 - | srawi TMP2, CARG3, 31 - |.if "func" == "floor" - | and TMP1, TMP3, TMP2 - | addic TMP0, TMP1, -1 - | subfe TMP1, TMP0, TMP1 - | add CARG1, RD, TMP1 - | xor CARG1, CARG1, TMP2 - | sub CARG1, CARG1, TMP2 - | b ->fff_resi - |.else - | andc TMP1, TMP3, TMP2 - | addic TMP0, TMP1, -1 - | subfe TMP1, TMP0, TMP1 - | add CARG1, RD, TMP1 - | cmpw CARG1, RD - | xor CARG1, CARG1, TMP2 - | sub CARG1, CARG1, TMP2 - | bge ->fff_resi - | // Overflow to 2^31. - | lus CARG3, 0x41e0 // 2^31. - | li CARG1, 0 - | b ->fff_restv - |.endif - |3: // |x| < 1 - | slwi TMP2, CARG3, 1 - | srawi TMP1, CARG3, 31 - | or TMP2, CARG1, TMP2 // ztest = (hi+hi) | lo - |.if "func" == "floor" - | and TMP1, TMP2, TMP1 // (ztest & sign) == 0 ? 0 : -1 - | subfic TMP2, TMP1, 0 - | subfe CARG1, CARG1, CARG1 - |.else - | andc TMP1, TMP2, TMP1 // (ztest & ~sign) == 0 ? 0 : 1 - | addic TMP2, TMP1, -1 - | subfe CARG1, TMP2, TMP1 - |.endif - | b ->fff_resi - |4: // exp >= 31. Check for -(2^31). - | xoris TMP1, TMP1, 0x8000 - | srawi TMP2, CARG3, 31 - |.if "func" == "floor" - | or TMP1, TMP1, CARG2 - |.endif - |.if PPE - | orc TMP1, TMP1, TMP2 - | cmpwi TMP1, 0 - |.else - | orc. TMP1, TMP1, TMP2 - |.endif - | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | lus CARG1, 0x8000 // -(2^31). - | beqy ->fff_resi - |5: - |.if FPU - | lfd FARG1, 0(BASE) - |.else - | lwz CARG1, 0(BASE) - | lwz CARG2, 4(BASE) - |.endif - | blex func - | b ->fff_resn - |.endmacro - | - |.if DUALNUM - | math_round floor - | math_round ceil - |.else - | // NYI: use internal implementation. - | math_extern floor - | math_extern ceil - |.endif - | - |.if SQRT - |.ffunc_n math_sqrt - | fsqrt FARG1, FARG1 - | b ->fff_resn - |.else - | math_extern sqrt - |.endif - | - |.ffunc math_log - | cmplwi NARGS8:RC, 8 - | lwz CARG1, 0(BASE) - | bne ->fff_fallback // Need exactly 1 argument. - | checknum CARG1; bge ->fff_fallback - |.if FPU - | lfd FARG1, 0(BASE) - |.else - | lwz CARG2, 4(BASE) - |.endif - | blex log - | b ->fff_resn - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.if DUALNUM - |.ffunc math_ldexp - | cmplwi NARGS8:RC, 16 - | lwz TMP0, 0(BASE) - |.if FPU - | lfd FARG1, 0(BASE) - |.else - | lwz CARG1, 0(BASE) - | lwz CARG2, 4(BASE) - |.endif - | lwz TMP1, 8(BASE) - |.if GPR64 - | lwz CARG2, 12(BASE) - |.elif FPU - | lwz CARG1, 12(BASE) - |.else - | lwz CARG3, 12(BASE) - |.endif - | blt ->fff_fallback - | checknum TMP0; bge ->fff_fallback - | checknum TMP1; bne ->fff_fallback - |.else - |.ffunc_nn math_ldexp - |.if GPR64 - | toint CARG2, FARG2 - |.else - | toint CARG1, FARG2 - |.endif - |.endif - | blex ldexp - | b ->fff_resn - | - |.ffunc_n math_frexp - |.if GPR64 - | la CARG2, DISPATCH_GL(tmptv)(DISPATCH) - |.elif FPU - | la CARG1, DISPATCH_GL(tmptv)(DISPATCH) - |.else - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - |.endif - | lwz PC, FRAME_PC(BASE) - | blex frexp - | lwz TMP1, DISPATCH_GL(tmptv)(DISPATCH) - | la RA, -8(BASE) - |.if not DUALNUM - | tonum_i FARG2, TMP1 - |.endif - |.if FPU - | stfd FARG1, 0(RA) - |.else - | stw CRET1, 0(RA) - | stw CRET2, 4(RA) - |.endif - | li RD, (2+1)*8 - |.if DUALNUM - | stw TISNUM, 8(RA) - | stw TMP1, 12(RA) - |.else - | stfd FARG2, 8(RA) - |.endif - | b ->fff_res - | - |.ffunc_n math_modf - |.if GPR64 - | la CARG2, -8(BASE) - |.elif FPU - | la CARG1, -8(BASE) - |.else - | la CARG3, -8(BASE) - |.endif - | lwz PC, FRAME_PC(BASE) - | blex modf - | la RA, -8(BASE) - |.if FPU - | stfd FARG1, 0(BASE) - |.else - | stw CRET1, 0(BASE) - | stw CRET2, 4(BASE) - |.endif - | li RD, (2+1)*8 - | b ->fff_res - | - |.macro math_minmax, name, ismax - |.if DUALNUM - | .ffunc_1 name - | checknum CARG3 - | addi SAVE0, BASE, 8 - | add SAVE1, BASE, NARGS8:RC - | bne >4 - |1: // Handle integers. - | lwz CARG4, 0(SAVE0) - | cmplw cr1, SAVE0, SAVE1 - | lwz CARG2, 4(SAVE0) - | bge cr1, ->fff_resi - | checknum CARG4 - | xoris TMP0, CARG1, 0x8000 - | xoris TMP3, CARG2, 0x8000 - | bne >3 - | subfc TMP3, TMP3, TMP0 - | subfe TMP0, TMP0, TMP0 - |.if ismax - | andc TMP3, TMP3, TMP0 - |.else - | and TMP3, TMP3, TMP0 - |.endif - | add CARG1, TMP3, CARG2 - |.if GPR64 - | rldicl CARG1, CARG1, 0, 32 - |.endif - | addi SAVE0, SAVE0, 8 - | b <1 - |3: - | bge ->fff_fallback - | // Convert intermediate result to number and continue below. - |.if FPU - | tonum_i FARG1, CARG1 - | lfd FARG2, 0(SAVE0) - |.else - | mr CARG2, CARG1 - | bl ->vm_sfi2d_1 - | lwz CARG3, 0(SAVE0) - | lwz CARG4, 4(SAVE0) - |.endif - | b >6 - |4: - |.if FPU - | lfd FARG1, 0(BASE) - |.else - | lwz CARG1, 0(BASE) - | lwz CARG2, 4(BASE) - |.endif - | bge ->fff_fallback - |5: // Handle numbers. - | lwz CARG3, 0(SAVE0) - | cmplw cr1, SAVE0, SAVE1 - |.if FPU - | lfd FARG2, 0(SAVE0) - |.else - | lwz CARG4, 4(SAVE0) - |.endif - | bge cr1, ->fff_resn - | checknum CARG3; bge >7 - |6: - | addi SAVE0, SAVE0, 8 - |.if FPU - |.if ismax - | fsub f0, FARG1, FARG2 - |.else - | fsub f0, FARG2, FARG1 - |.endif - | fsel FARG1, f0, FARG1, FARG2 - |.else - | stw CARG1, SFSAVE_1 - | stw CARG2, SFSAVE_2 - | stw CARG3, SFSAVE_3 - | stw CARG4, SFSAVE_4 - | blex __ledf2 - | cmpwi CRET1, 0 - |.if ismax - | blt >8 - |.else - | bge >8 - |.endif - | lwz CARG1, SFSAVE_1 - | lwz CARG2, SFSAVE_2 - | b <5 - |8: - | lwz CARG1, SFSAVE_3 - | lwz CARG2, SFSAVE_4 - |.endif - | b <5 - |7: // Convert integer to number and continue above. - | lwz CARG3, 4(SAVE0) - | bne ->fff_fallback - |.if FPU - | tonum_i FARG2, CARG3 - |.else - | bl ->vm_sfi2d_2 - |.endif - | b <6 - |.else - | .ffunc_n name - | li TMP1, 8 - |1: - | lwzx CARG2, BASE, TMP1 - | lfdx FARG2, BASE, TMP1 - | cmplw cr1, TMP1, NARGS8:RC - | checknum CARG2 - | bge cr1, ->fff_resn - | bge ->fff_fallback - |.if ismax - | fsub f0, FARG1, FARG2 - |.else - | fsub f0, FARG2, FARG1 - |.endif - | addi TMP1, TMP1, 8 - | fsel FARG1, f0, FARG1, FARG2 - | b <1 - |.endif - |.endmacro - | - | math_minmax math_min, 0 - | math_minmax math_max, 1 - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | bne ->fff_fallback // Need exactly 1 argument. - | checkstr CARG3 - | bne ->fff_fallback - | lwz TMP0, STR:CARG1->len - |.if DUALNUM - | lbz CARG1, STR:CARG1[1] // Access is always ok (NUL at end). - | li RD, (0+1)*8 - | lwz PC, FRAME_PC(BASE) - | cmplwi TMP0, 0 - | la RA, -8(BASE) - | beqy ->fff_res - | b ->fff_resi - |.else - | lbz TMP1, STR:CARG1[1] // Access is always ok (NUL at end). - | addic TMP3, TMP0, -1 // RD = ((str->len != 0)+1)*8 - | subfe RD, TMP3, TMP0 - | stw TMP1, TONUM_LO // Inlined tonum_u f0, TMP1. - | addi RD, RD, 1 - | lfd f0, TONUM_D - | la RA, -8(BASE) - | lwz PC, FRAME_PC(BASE) - | fsub f0, f0, TOBIT - | slwi RD, RD, 3 - | stfd f0, 0(RA) - | b ->fff_res - |.endif - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - |.if DUALNUM - | lwz TMP0, 4(BASE) - | bne ->fff_fallback // Exactly 1 argument. - | checknum CARG3; bne ->fff_fallback - | la CARG2, 7(BASE) - |.else - | lfd FARG1, 0(BASE) - | bne ->fff_fallback // Exactly 1 argument. - | checknum CARG3; bge ->fff_fallback - | toint TMP0, FARG1 - | la CARG2, TMPD_BLO - |.endif - | li CARG3, 1 - | cmplwi TMP0, 255; bgt ->fff_fallback - |->fff_newstr: - | mr CARG1, L - | stp BASE, L->base - | stw PC, SAVE_PC - | bl extern lj_str_new // (lua_State *L, char *str, size_t l) - |->fff_resstr: - | // Returns GCstr *. - | lp BASE, L->base - | li CARG3, LJ_TSTR - | b ->fff_restv - | - |.ffunc string_sub - | ffgccheck - | cmplwi NARGS8:RC, 16 - | lwz CARG3, 16(BASE) - |.if not DUALNUM - | lfd f0, 16(BASE) - |.endif - | lwz TMP0, 0(BASE) - | lwz STR:CARG1, 4(BASE) - | blt ->fff_fallback - | lwz CARG2, 8(BASE) - |.if DUALNUM - | lwz TMP1, 12(BASE) - |.else - | lfd f1, 8(BASE) - |.endif - | li TMP2, -1 - | beq >1 - |.if DUALNUM - | checknum CARG3 - | lwz TMP2, 20(BASE) - | bne ->fff_fallback - |1: - | checknum CARG2; bne ->fff_fallback - |.else - | checknum CARG3; bge ->fff_fallback - | toint TMP2, f0 - |1: - | checknum CARG2; bge ->fff_fallback - |.endif - | checkstr TMP0; bne ->fff_fallback - |.if not DUALNUM - | toint TMP1, f1 - |.endif - | lwz TMP0, STR:CARG1->len - | cmplw TMP0, TMP2 // len < end? (unsigned compare) - | addi TMP3, TMP2, 1 - | blt >5 - |2: - | cmpwi TMP1, 0 // start <= 0? - | add TMP3, TMP1, TMP0 - | ble >7 - |3: - | sub CARG3, TMP2, TMP1 - | addi CARG2, STR:CARG1, #STR-1 - | srawi TMP0, CARG3, 31 - | addi CARG3, CARG3, 1 - | add CARG2, CARG2, TMP1 - | andc CARG3, CARG3, TMP0 - |.if GPR64 - | rldicl CARG2, CARG2, 0, 32 - | rldicl CARG3, CARG3, 0, 32 - |.endif - | b ->fff_newstr - | - |5: // Negative end or overflow. - | cmpw TMP0, TMP2 // len >= end? (signed compare) - | add TMP2, TMP0, TMP3 // Negative end: end = end+len+1. - | bge <2 - | mr TMP2, TMP0 // Overflow: end = len. - | b <2 - | - |7: // Negative start or underflow. - | .gpr64 extsw TMP1, TMP1 - | addic CARG3, TMP1, -1 - | subfe CARG3, CARG3, CARG3 - | srawi CARG2, TMP3, 31 // Note: modifies carry. - | andc TMP3, TMP3, CARG3 - | andc TMP1, TMP3, CARG2 - | addi TMP1, TMP1, 1 // start = 1 + (start ? start+len : 0) - | b <3 - | - |.macro ffstring_op, name - | .ffunc string_ .. name - | ffgccheck - | cmplwi NARGS8:RC, 8 - | lwz CARG3, 0(BASE) - | lwz STR:CARG2, 4(BASE) - | blt ->fff_fallback - | checkstr CARG3 - | la SBUF:CARG1, DISPATCH_GL(tmpbuf)(DISPATCH) - | bne ->fff_fallback - | lwz TMP0, SBUF:CARG1->b - | stw L, SBUF:CARG1->L - | stp BASE, L->base - | stw PC, SAVE_PC - | stw TMP0, SBUF:CARG1->w - | bl extern lj_buf_putstr_ .. name - | bl extern lj_buf_tostr - | b ->fff_resstr - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |.macro .ffunc_bit, name - |.if DUALNUM - | .ffunc_1 bit_..name - | checknum CARG3; bnel ->fff_tobit_fb - |.else - | .ffunc_n bit_..name - | fadd FARG1, FARG1, TOBIT - | stfd FARG1, TMPD - | lwz CARG1, TMPD_LO - |.endif - |.endmacro - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name - | addi SAVE0, BASE, 8 - | add SAVE1, BASE, NARGS8:RC - |1: - | lwz CARG4, 0(SAVE0) - | cmplw cr1, SAVE0, SAVE1 - |.if DUALNUM - | lwz CARG2, 4(SAVE0) - |.else - | lfd FARG1, 0(SAVE0) - |.endif - | bgey cr1, ->fff_resi - | checknum CARG4 - |.if DUALNUM - |.if FPU - | bnel ->fff_bitop_fb - |.else - | beq >3 - | stw CARG1, SFSAVE_1 - | bl ->fff_bitop_fb - | mr CARG2, CARG1 - | lwz CARG1, SFSAVE_1 - |3: - |.endif - |.else - | fadd FARG1, FARG1, TOBIT - | bge ->fff_fallback - | stfd FARG1, TMPD - | lwz CARG2, TMPD_LO - |.endif - | ins CARG1, CARG1, CARG2 - | addi SAVE0, SAVE0, 8 - | b <1 - |.endmacro - | - |.ffunc_bit_op band, and - |.ffunc_bit_op bor, or - |.ffunc_bit_op bxor, xor - | - |.ffunc_bit bswap - | rotlwi TMP0, CARG1, 8 - | rlwimi TMP0, CARG1, 24, 0, 7 - | rlwimi TMP0, CARG1, 24, 16, 23 - | mr CRET1, TMP0 - | b ->fff_resi - | - |.ffunc_bit bnot - | not CRET1, CARG1 - | b ->fff_resi - | - |.macro .ffunc_bit_sh, name, ins, shmod - |.if DUALNUM - | .ffunc_2 bit_..name - |.if FPU - | checknum CARG3; bnel ->fff_tobit_fb - |.else - | checknum CARG3; beq >1 - | bl ->fff_tobit_fb - | lwz CARG2, 12(BASE) // Conversion polluted CARG2. - |1: - |.endif - | // Note: no inline conversion from number for 2nd argument! - | checknum CARG4; bne ->fff_fallback - |.else - | .ffunc_nn bit_..name - | fadd FARG1, FARG1, TOBIT - | fadd FARG2, FARG2, TOBIT - | stfd FARG1, TMPD - | lwz CARG1, TMPD_LO - | stfd FARG2, TMPD - | lwz CARG2, TMPD_LO - |.endif - |.if shmod == 1 - | rlwinm CARG2, CARG2, 0, 27, 31 - |.elif shmod == 2 - | neg CARG2, CARG2 - |.endif - | ins CRET1, CARG1, CARG2 - | b ->fff_resi - |.endmacro - | - |.ffunc_bit_sh lshift, slw, 1 - |.ffunc_bit_sh rshift, srw, 1 - |.ffunc_bit_sh arshift, sraw, 1 - |.ffunc_bit_sh rol, rotlw, 0 - |.ffunc_bit_sh ror, rotlw, 2 - | - |.ffunc_bit tobit - |.if DUALNUM - | b ->fff_resi - |.else - |->fff_resi: - | tonum_i FARG1, CRET1 - |.endif - |->fff_resn: - | lwz PC, FRAME_PC(BASE) - | la RA, -8(BASE) - |.if FPU - | stfd FARG1, -8(BASE) - |.else - | stw CARG1, -8(BASE) - | stw CARG2, -4(BASE) - |.endif - | b ->fff_res1 - | - |// Fallback FP number to bit conversion. - |->fff_tobit_fb: - |.if DUALNUM - |.if FPU - | lfd FARG1, 0(BASE) - | bgt ->fff_fallback - | fadd FARG1, FARG1, TOBIT - | stfd FARG1, TMPD - | lwz CARG1, TMPD_LO - | blr - |.else - | bgt ->fff_fallback - | mr CARG2, CARG1 - | mr CARG1, CARG3 - |// Modifies: CARG1, CARG2, TMP0, TMP1, TMP2. - |->vm_tobit: - | slwi TMP2, CARG1, 1 - | addis TMP2, TMP2, 0x0020 - | cmpwi TMP2, 0 - | bge >2 - | li TMP1, 0x3e0 - | srawi TMP2, TMP2, 21 - | not TMP1, TMP1 - | sub. TMP2, TMP1, TMP2 - | cmpwi cr7, CARG1, 0 - | blt >1 - | slwi TMP1, CARG1, 11 - | srwi TMP0, CARG2, 21 - | oris TMP1, TMP1, 0x8000 - | or TMP1, TMP1, TMP0 - | srw CARG1, TMP1, TMP2 - | bclr 4, 28 // Return if cr7[lt] == 0, no hint. - | neg CARG1, CARG1 - | blr - |1: - | addi TMP2, TMP2, 21 - | srw TMP1, CARG2, TMP2 - | slwi CARG2, CARG1, 12 - | subfic TMP2, TMP2, 20 - | slw TMP0, CARG2, TMP2 - | or CARG1, TMP1, TMP0 - | bclr 4, 28 // Return if cr7[lt] == 0, no hint. - | neg CARG1, CARG1 - | blr - |2: - | li CARG1, 0 - | blr - |.endif - |.endif - |->fff_bitop_fb: - |.if DUALNUM - |.if FPU - | lfd FARG1, 0(SAVE0) - | bgt ->fff_fallback - | fadd FARG1, FARG1, TOBIT - | stfd FARG1, TMPD - | lwz CARG2, TMPD_LO - | blr - |.else - | bgt ->fff_fallback - | mr CARG1, CARG4 - | b ->vm_tobit - |.endif - |.endif - | - |//----------------------------------------------------------------------- - | - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RB = CFUNC, RC = nargs*8 - | lp TMP3, CFUNC:RB->f - | add TMP1, BASE, NARGS8:RC - | lwz PC, FRAME_PC(BASE) // Fallback may overwrite PC. - | addi TMP0, TMP1, 8*LUA_MINSTACK - | lwz TMP2, L->maxstack - | stw PC, SAVE_PC // Redundant (but a defined value). - | .toc lp TMP3, 0(TMP3) - | cmplw TMP0, TMP2 - | stp BASE, L->base - | stp TMP1, L->top - | mr CARG1, L - | bgt >5 // Need to grow stack. - | mtctr TMP3 - | bctrl // (lua_State *L) - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | lp BASE, L->base - | cmpwi CRET1, 0 - | slwi RD, CRET1, 3 - | la RA, -8(BASE) - | bgt ->fff_res // Returned nresults+1? - |1: // Returned 0 or -1: retry fast path. - | lp TMP0, L->top - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | sub NARGS8:RC, TMP0, BASE - | bne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | andix. TMP0, PC, FRAME_TYPE - | rlwinm TMP1, PC, 0, 0, 28 - | bne >3 - | lwz INS, -4(PC) - | decode_RA8 TMP1, INS - | addi TMP1, TMP1, 8 - |3: - | sub TMP2, BASE, TMP1 - | b ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | li CARG2, LUA_MINSTACK - | bl extern lj_state_growstack // (lua_State *L, int n) - | lp BASE, L->base - | cmpw TMP0, TMP0 // Set 4*cr0+eq to force retry. - | b <1 - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RC = nargs*8 - | mflr SAVE0 - | stp BASE, L->base - | add TMP0, BASE, NARGS8:RC - | stw PC, SAVE_PC // Redundant (but a defined value). - | stp TMP0, L->top - | mr CARG1, L - | bl extern lj_gc_step // (lua_State *L) - | lp BASE, L->base - | mtlr SAVE0 - | lp TMP0, L->top - | sub NARGS8:RC, TMP0, BASE - | lwz CFUNC:RB, FRAME_FUNC(BASE) - | blr - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andix. TMP0, TMP3, HOOK_VMEVENT // No recording while in vmevent. - | bne >5 - | // Decrement the hookcount for consistency, but always do the call. - | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andix. TMP0, TMP3, HOOK_ACTIVE - | bne >1 - | subi TMP2, TMP2, 1 - | andi. TMP0, TMP3, LUA_MASKLINE|LUA_MASKCOUNT - | beqy >1 - | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | b >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active? - | beq >1 - |5: // Re-dispatch to static ins. - | addi TMP1, TMP1, GG_DISP2STATIC // Assumes decode_OPP TMP1, INS. - | lpx TMP0, DISPATCH, TMP1 - | mtctr TMP0 - | bctr - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | lbz TMP3, DISPATCH_GL(hookmask)(DISPATCH) - | lwz TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | andix. TMP0, TMP3, HOOK_ACTIVE // Hook already active? - | rlwinm TMP0, TMP3, 31-LUA_HOOKLINE, 31, 0 - | bne <5 - | - | cmpwi cr1, TMP0, 0 - | addic. TMP2, TMP2, -1 - | beq cr1, <5 - | stw TMP2, DISPATCH_GL(hookcount)(DISPATCH) - | beq >1 - | bge cr1, <5 - |1: - | mr CARG1, L - | stw MULTRES, SAVE_MULTRES - | mr CARG2, PC - | stp BASE, L->base - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | bl extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | lp BASE, L->base - |4: // Re-dispatch to static ins. - | lwz INS, -4(PC) - | decode_OPP TMP1, INS - | decode_RB8 RB, INS - | addi TMP1, TMP1, GG_DISP2STATIC - | decode_RD8 RD, INS - | lpx TMP0, DISPATCH, TMP1 - | decode_RA8 RA, INS - | decode_RC8 RC, INS - | mtctr TMP0 - | bctr - | - |->cont_hook: // Continue from hook yield. - | addi PC, PC, 4 - | lwz MULTRES, -20(RB) // Restore MULTRES for *M ins. - | b <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | addi CARG1, DISPATCH, GG_DISP2J - | stw PC, SAVE_PC - | lwz TMP1, LFUNC:TMP1->pc - | mr CARG2, PC - | stw L, DISPATCH_J(L)(DISPATCH) - | lbz TMP1, PC2PROTO(framesize)(TMP1) - | stp BASE, L->base - | slwi TMP1, TMP1, 3 - | add TMP1, BASE, TMP1 - | stp TMP1, L->top - | bl extern lj_trace_hot // (jit_State *J, const BCIns *pc) - | b <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mr CARG2, PC - |.if JIT - | b >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | ori CARG2, PC, 1 - |1: - |.endif - | add TMP0, BASE, RC - | stw PC, SAVE_PC - | mr CARG1, L - | stp BASE, L->base - | sub RA, RA, BASE - | stp TMP0, L->top - | bl extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // Returns ASMFunction. - | lp BASE, L->base - | lp TMP0, L->top - | stw ZERO, SAVE_PC // Invalidate for subsequent line hook. - | sub NARGS8:RC, TMP0, BASE - | add RA, BASE, RA - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | lwz INS, -4(PC) - | mtctr CRET1 - | bctr - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // RA = resultptr, RB = meta base - | lwz INS, -4(PC) - | lwz TRACE:TMP2, -20(RB) // Save previous trace. - | addic. TMP1, MULTRES, -8 - | decode_RA8 RC, INS // Call base. - | beq >2 - |1: // Move results down. - |.if FPU - | lfd f0, 0(RA) - |.else - | lwz CARG1, 0(RA) - | lwz CARG2, 4(RA) - |.endif - | addic. TMP1, TMP1, -8 - | addi RA, RA, 8 - |.if FPU - | stfdx f0, BASE, RC - |.else - | add CARG3, BASE, RC - | stw CARG1, 0(CARG3) - | stw CARG2, 4(CARG3) - |.endif - | addi RC, RC, 8 - | bne <1 - |2: - | decode_RA8 RA, INS - | decode_RB8 RB, INS - | add RA, RA, RB - |3: - | cmplw RA, RC - | bgt >9 // More results wanted? - | - | lhz TMP3, TRACE:TMP2->traceno - | lhz RD, TRACE:TMP2->link - | cmpw RD, TMP3 - | cmpwi cr1, RD, 0 - | beq ->cont_nop // Blacklisted. - | slwi RD, RD, 3 - | bne cr1, =>BC_JLOOP // Jump to stitched trace. - | - | // Stitch a new trace to the previous trace. - | stw TMP3, DISPATCH_J(exitno)(DISPATCH) - | stp L, DISPATCH_J(L)(DISPATCH) - | stp BASE, L->base - | addi CARG1, DISPATCH, GG_DISP2J - | mr CARG2, PC - | bl extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc) - | lp BASE, L->base - | b ->cont_nop - | - |9: - | stwx TISNIL, BASE, RC - | addi RC, RC, 8 - | b <3 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | mr CARG1, L - | stw MULTRES, SAVE_MULTRES - | mr CARG2, PC - | stp BASE, L->base - | bl extern lj_dispatch_profile // (lua_State *L, const BCIns *pc) - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | lp BASE, L->base - | subi PC, PC, 4 - | b ->cont_nop -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro savex_, a, b, c, d - |.if FPU - | stfd f..a, 16+a*8(sp) - | stfd f..b, 16+b*8(sp) - | stfd f..c, 16+c*8(sp) - | stfd f..d, 16+d*8(sp) - |.endif - |.endmacro - | - |->vm_exit_handler: - |.if JIT - | addi sp, sp, -(16+32*8+32*4) - | stmw r2, 16+32*8+2*4(sp) - | addi DISPATCH, JGL, -GG_DISP2G-32768 - | li CARG2, ~LJ_VMST_EXIT - | lwz CARG1, 16+32*8+32*4(sp) // Get stack chain. - | stw CARG2, DISPATCH_GL(vmstate)(DISPATCH) - | savex_ 0,1,2,3 - | stw CARG1, 0(sp) // Store extended stack chain. - | clrso TMP1 - | savex_ 4,5,6,7 - | addi CARG2, sp, 16+32*8+32*4 // Recompute original value of sp. - | savex_ 8,9,10,11 - | stw CARG2, 16+32*8+1*4(sp) // Store sp in RID_SP. - | savex_ 12,13,14,15 - | mflr CARG3 - | li TMP1, 0 - | savex_ 16,17,18,19 - | stw TMP1, 16+32*8+0*4(sp) // Clear RID_TMP. - | savex_ 20,21,22,23 - | lhz CARG4, 2(CARG3) // Load trace number. - | savex_ 24,25,26,27 - | lwz L, DISPATCH_GL(cur_L)(DISPATCH) - | savex_ 28,29,30,31 - | sub CARG3, TMP0, CARG3 // Compute exit number. - | lp BASE, DISPATCH_GL(jit_base)(DISPATCH) - | srwi CARG3, CARG3, 2 - | stp L, DISPATCH_J(L)(DISPATCH) - | subi CARG3, CARG3, 2 - | stp BASE, L->base - | stw CARG4, DISPATCH_J(parent)(DISPATCH) - | stw TMP1, DISPATCH_GL(jit_base)(DISPATCH) - | addi CARG1, DISPATCH, GG_DISP2J - | stw CARG3, DISPATCH_J(exitno)(DISPATCH) - | addi CARG2, sp, 16 - | bl extern lj_trace_exit // (jit_State *J, ExitState *ex) - | // Returns MULTRES (unscaled) or negated error code. - | lp TMP1, L->cframe - | lwz TMP2, 0(sp) - | lp BASE, L->base - |.if GPR64 - | rldicr sp, TMP1, 0, 61 - |.else - | rlwinm sp, TMP1, 0, 0, 29 - |.endif - | lwz PC, SAVE_PC // Get SAVE_PC. - | stw TMP2, 0(sp) - | stw L, SAVE_L // Set SAVE_L (on-trace resume/yield). - | b >1 - |.endif - |->vm_exit_interp: - |.if JIT - | // CARG1 = MULTRES or negated error code, BASE, PC and JGL set. - | lwz L, SAVE_L - | addi DISPATCH, JGL, -GG_DISP2G-32768 - | stp BASE, L->base - |1: - | cmpwi CARG1, 0 - | blt >9 // Check for error from exit. - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | slwi MULTRES, CARG1, 3 - | li TMP2, 0 - | stw MULTRES, SAVE_MULTRES - | lwz TMP1, LFUNC:RB->pc - | stw TMP2, DISPATCH_GL(jit_base)(DISPATCH) - | lwz KBASE, PC2PROTO(k)(TMP1) - | // Setup type comparison constants. - | li TISNUM, LJ_TISNUM - | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | .FPU stw TMP3, TMPD - | li ZERO, 0 - | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | .FPU lfs TOBIT, TMPD - | .FPU stw TMP3, TMPD - | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | li TISNIL, LJ_TNIL - | .FPU stw TMP0, TONUM_HI - | .FPU lfs TONUM, TMPD - | // Modified copy of ins_next which handles function header dispatch, too. - | lwz INS, 0(PC) - | addi PC, PC, 4 - | // Assumes TISNIL == ~LJ_VMST_INTERP == -1. - | stw TISNIL, DISPATCH_GL(vmstate)(DISPATCH) - | decode_OPP TMP1, INS - | decode_RA8 RA, INS - | lpx TMP0, DISPATCH, TMP1 - | mtctr TMP0 - | cmplwi TMP1, BC_FUNCF*4 // Function header? - | bge >2 - | decode_RB8 RB, INS - | decode_RD8 RD, INS - | decode_RC8 RC, INS - | bctr - |2: - | cmplwi TMP1, (BC_FUNCC+2)*4 // Fast function? - | blt >3 - | // Check frame below fast function. - | lwz TMP1, FRAME_PC(BASE) - | andix. TMP0, TMP1, FRAME_TYPE - | bney >3 // Trace stitching continuation? - | // Otherwise set KBASE for Lua function below fast function. - | lwz TMP2, -4(TMP1) - | decode_RA8 TMP0, TMP2 - | sub TMP1, BASE, TMP0 - | lwz LFUNC:TMP2, -12(TMP1) - | lwz TMP1, LFUNC:TMP2->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - |3: - | subi RC, MULTRES, 8 - | add RA, RA, BASE - | bctr - | - |9: // Rethrow error from the right C frame. - | neg CARG2, CARG1 - | mr CARG1, L - | bl extern lj_err_trace // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// NYI: Use internal implementations of floor, ceil, trunc, sfcmp. - | - |.macro sfi2d, AHI, ALO - |.if not FPU - | mr. AHI, ALO - | bclr 12, 2 // Handle zero first. - | srawi TMP0, ALO, 31 - | xor TMP1, ALO, TMP0 - | sub TMP1, TMP1, TMP0 // Absolute value in TMP1. - | cntlzw AHI, TMP1 - | andix. TMP0, TMP0, 0x800 // Mask sign bit. - | slw TMP1, TMP1, AHI // Align mantissa left with leading 1. - | subfic AHI, AHI, 0x3ff+31-1 // Exponent -1 in AHI. - | slwi ALO, TMP1, 21 - | or AHI, AHI, TMP0 // Sign | Exponent. - | srwi TMP1, TMP1, 11 - | slwi AHI, AHI, 20 // Align left. - | add AHI, AHI, TMP1 // Add mantissa, increment exponent. - | blr - |.endif - |.endmacro - | - |// Input: CARG2. Output: CARG1, CARG2. Temporaries: TMP0, TMP1. - |->vm_sfi2d_1: - | sfi2d CARG1, CARG2 - | - |// Input: CARG4. Output: CARG3, CARG4. Temporaries: TMP0, TMP1. - |->vm_sfi2d_2: - | sfi2d CARG3, CARG4 - | - |->vm_modi: - | divwo. TMP0, CARG1, CARG2 - | bso >1 - |.if GPR64 - | xor CARG3, CARG1, CARG2 - | cmpwi CARG3, 0 - |.else - | xor. CARG3, CARG1, CARG2 - |.endif - | mullw TMP0, TMP0, CARG2 - | sub CARG1, CARG1, TMP0 - | bgelr - | cmpwi CARG1, 0; beqlr - | add CARG1, CARG1, CARG2 - | blr - |1: - | cmpwi CARG2, 0 - | li CARG1, 0 - | beqlr - | clrso TMP0 // Clear SO for -2147483648 % -1 and return 0. - | blr - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |// void lj_vm_cachesync(void *start, void *end) - |// Flush D-Cache and invalidate I-Cache. Assumes 32 byte cache line size. - |// This is a good lower bound, except for very ancient PPC models. - |->vm_cachesync: - |.if JIT or FFI - | // Compute start of first cache line and number of cache lines. - | rlwinm CARG1, CARG1, 0, 0, 26 - | sub CARG2, CARG2, CARG1 - | addi CARG2, CARG2, 31 - | rlwinm. CARG2, CARG2, 27, 5, 31 - | beqlr - | mtctr CARG2 - | mr CARG3, CARG1 - |1: // Flush D-Cache. - | dcbst r0, CARG1 - | addi CARG1, CARG1, 32 - | bdnz <1 - | sync - | mtctr CARG2 - |1: // Invalidate I-Cache. - | icbi r0, CARG3 - | addi CARG3, CARG3, 32 - | bdnz <1 - | isync - | blr - |.endif - | - |->vm_next: - |.if JIT - | NYI // On big-endian. - |.endif - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in r11, g in r12. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs - | lwz CTSTATE, GL:r12->ctype_state - | addi DISPATCH, r12, GG_G2DISP - | stw r11, CTSTATE->cb.slot - | stw r3, CTSTATE->cb.gpr[0] - | .FPU stfd f1, CTSTATE->cb.fpr[0] - | stw r4, CTSTATE->cb.gpr[1] - | .FPU stfd f2, CTSTATE->cb.fpr[1] - | stw r5, CTSTATE->cb.gpr[2] - | .FPU stfd f3, CTSTATE->cb.fpr[2] - | stw r6, CTSTATE->cb.gpr[3] - | .FPU stfd f4, CTSTATE->cb.fpr[3] - | stw r7, CTSTATE->cb.gpr[4] - | .FPU stfd f5, CTSTATE->cb.fpr[4] - | stw r8, CTSTATE->cb.gpr[5] - | .FPU stfd f6, CTSTATE->cb.fpr[5] - | stw r9, CTSTATE->cb.gpr[6] - | .FPU stfd f7, CTSTATE->cb.fpr[6] - | stw r10, CTSTATE->cb.gpr[7] - | .FPU stfd f8, CTSTATE->cb.fpr[7] - | addi TMP0, sp, CFRAME_SPACE+8 - | stw TMP0, CTSTATE->cb.stack - | mr CARG1, CTSTATE - | stw CTSTATE, SAVE_PC // Any value outside of bytecode is ok. - | mr CARG2, sp - | bl extern lj_ccallback_enter // (CTState *cts, void *cf) - | // Returns lua_State *. - | lp BASE, L:CRET1->base - | li TISNUM, LJ_TISNUM // Setup type comparison constants. - | lp RC, L:CRET1->top - | .FPU lus TMP3, 0x59c0 // TOBIT = 2^52 + 2^51 (float). - | li ZERO, 0 - | mr L, CRET1 - | .FPU stw TMP3, TMPD - | .FPU lus TMP0, 0x4338 // Hiword of 2^52 + 2^51 (double) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | .FPU ori TMP3, TMP3, 0x0004 // TONUM = 2^52 + 2^51 + 2^31 (float). - | .FPU stw TMP0, TONUM_HI - | li TISNIL, LJ_TNIL - | li_vmstate INTERP - | .FPU lfs TOBIT, TMPD - | .FPU stw TMP3, TMPD - | sub RC, RC, BASE - | st_vmstate - | .FPU lfs TONUM, TMPD - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | lwz CTSTATE, DISPATCH_GL(ctype_state)(DISPATCH) - | stp BASE, L->base - | stp RB, L->top - | stp L, CTSTATE->L - | mr CARG1, CTSTATE - | mr CARG2, RA - | bl extern lj_ccallback_leave // (CTState *cts, TValue *o) - | lwz CRET1, CTSTATE->cb.gpr[0] - | .FPU lfd FARG1, CTSTATE->cb.fpr[0] - | lwz CRET2, CTSTATE->cb.gpr[1] - | b ->vm_leave_unw - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, CARG1 - | lwz TMP1, CCSTATE->spadj - | mflr TMP0 - | lbz CARG2, CCSTATE->nsp - | lbz CARG3, CCSTATE->nfpr - | neg TMP1, TMP1 - | stw TMP0, 4(sp) - | cmpwi cr1, CARG3, 0 - | mr TMP2, sp - | addic. CARG2, CARG2, -1 - | stwux sp, sp, TMP1 - | crnot 4*cr1+eq, 4*cr1+eq // For vararg calls. - | stw r14, -4(TMP2) - | stw CCSTATE, -8(TMP2) - | mr r14, TMP2 - | la TMP1, CCSTATE->stack - | slwi CARG2, CARG2, 2 - | blty >2 - | la TMP2, 8(sp) - |1: - | lwzx TMP0, TMP1, CARG2 - | stwx TMP0, TMP2, CARG2 - | addic. CARG2, CARG2, -4 - | bge <1 - |2: - | bney cr1, >3 - | .FPU lfd f1, CCSTATE->fpr[0] - | .FPU lfd f2, CCSTATE->fpr[1] - | .FPU lfd f3, CCSTATE->fpr[2] - | .FPU lfd f4, CCSTATE->fpr[3] - | .FPU lfd f5, CCSTATE->fpr[4] - | .FPU lfd f6, CCSTATE->fpr[5] - | .FPU lfd f7, CCSTATE->fpr[6] - | .FPU lfd f8, CCSTATE->fpr[7] - |3: - | lp TMP0, CCSTATE->func - | lwz CARG2, CCSTATE->gpr[1] - | lwz CARG3, CCSTATE->gpr[2] - | lwz CARG4, CCSTATE->gpr[3] - | lwz CARG5, CCSTATE->gpr[4] - | mtctr TMP0 - | lwz r8, CCSTATE->gpr[5] - | lwz r9, CCSTATE->gpr[6] - | lwz r10, CCSTATE->gpr[7] - | lwz CARG1, CCSTATE->gpr[0] // Do this last, since CCSTATE is CARG1. - | bctrl - | lwz CCSTATE:TMP1, -8(r14) - | lwz TMP2, -4(r14) - | lwz TMP0, 4(r14) - | stw CARG1, CCSTATE:TMP1->gpr[0] - | .FPU stfd FARG1, CCSTATE:TMP1->fpr[0] - | stw CARG2, CCSTATE:TMP1->gpr[1] - | mtlr TMP0 - | stw CARG3, CCSTATE:TMP1->gpr[2] - | mr sp, r14 - | stw CARG4, CCSTATE:TMP1->gpr[3] - | mr r14, TMP2 - | blr - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1*8, RD = src2*8, JMP with RD = target - |.if DUALNUM - | lwzux CARG1, RA, BASE - | addi PC, PC, 4 - | lwz CARG2, 4(RA) - | lwzux CARG3, RD, BASE - | lwz TMP2, -4(PC) - | checknum cr0, CARG1 - | lwz CARG4, 4(RD) - | decode_RD4 TMP2, TMP2 - | checknum cr1, CARG3 - | addis SAVE0, TMP2, -(BCBIAS_J*4 >> 16) - | bne cr0, >7 - | bne cr1, >8 - | cmpw CARG2, CARG4 - if (op == BC_ISLT) { - | bge >2 - } else if (op == BC_ISGE) { - | blt >2 - } else if (op == BC_ISLE) { - | bgt >2 - } else { - | ble >2 - } - |1: - | add PC, PC, SAVE0 - |2: - | ins_next - | - |7: // RA is not an integer. - | bgt cr0, ->vmeta_comp - | // RA is a number. - | .FPU lfd f0, 0(RA) - | bgt cr1, ->vmeta_comp - | blt cr1, >4 - | // RA is a number, RD is an integer. - |.if FPU - | tonum_i f1, CARG4 - |.else - | bl ->vm_sfi2d_2 - |.endif - | b >5 - | - |8: // RA is an integer, RD is not an integer. - | bgt cr1, ->vmeta_comp - | // RA is an integer, RD is a number. - |.if FPU - | tonum_i f0, CARG2 - |.else - | bl ->vm_sfi2d_1 - |.endif - |4: - | .FPU lfd f1, 0(RD) - |5: - |.if FPU - | fcmpu cr0, f0, f1 - |.else - | blex __ledf2 - | cmpwi CRET1, 0 - |.endif - if (op == BC_ISLT) { - | bge <2 - } else if (op == BC_ISGE) { - | blt <2 - } else if (op == BC_ISLE) { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | bge <2 - } else { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | blt <2 - } - | b <1 - |.else - | lwzx TMP0, BASE, RA - | addi PC, PC, 4 - | lfdx f0, BASE, RA - | lwzx TMP1, BASE, RD - | checknum cr0, TMP0 - | lwz TMP2, -4(PC) - | lfdx f1, BASE, RD - | checknum cr1, TMP1 - | decode_RD4 TMP2, TMP2 - | bge cr0, ->vmeta_comp - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | bge cr1, ->vmeta_comp - | fcmpu cr0, f0, f1 - if (op == BC_ISLT) { - | bge >1 - } else if (op == BC_ISGE) { - | blt >1 - } else if (op == BC_ISLE) { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | bge >1 - } else { - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+eq - | blt >1 - } - | add PC, PC, TMP2 - |1: - | ins_next - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | // RA = src1*8, RD = src2*8, JMP with RD = target - |.if DUALNUM - | lwzux CARG1, RA, BASE - | addi PC, PC, 4 - | lwz CARG2, 4(RA) - | lwzux CARG3, RD, BASE - | checknum cr0, CARG1 - | lwz SAVE0, -4(PC) - | checknum cr1, CARG3 - | decode_RD4 SAVE0, SAVE0 - | lwz CARG4, 4(RD) - | cror 4*cr7+gt, 4*cr0+gt, 4*cr1+gt - | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16) - if (vk) { - | ble cr7, ->BC_ISEQN_Z - } else { - | ble cr7, ->BC_ISNEN_Z - } - |.else - | lwzux CARG1, RA, BASE - | lwz SAVE0, 0(PC) - | lfd f0, 0(RA) - | addi PC, PC, 4 - | lwzux CARG3, RD, BASE - | checknum cr0, CARG1 - | decode_RD4 SAVE0, SAVE0 - | lfd f1, 0(RD) - | checknum cr1, CARG3 - | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16) - | bge cr0, >5 - | bge cr1, >5 - | fcmpu cr0, f0, f1 - if (vk) { - | bne >1 - | add PC, PC, SAVE0 - } else { - | beq >1 - | add PC, PC, SAVE0 - } - |1: - | ins_next - |.endif - |5: // Either or both types are not numbers. - |.if not DUALNUM - | lwz CARG2, 4(RA) - | lwz CARG4, 4(RD) - |.endif - |.if FFI - | cmpwi cr7, CARG1, LJ_TCDATA - | cmpwi cr5, CARG3, LJ_TCDATA - |.endif - | not TMP2, CARG1 - | cmplw CARG1, CARG3 - | cmplwi cr1, TMP2, ~LJ_TISPRI // Primitive? - |.if FFI - | cror 4*cr7+eq, 4*cr7+eq, 4*cr5+eq - |.endif - | cmplwi cr6, TMP2, ~LJ_TISTABUD // Table or userdata? - |.if FFI - | beq cr7, ->vmeta_equal_cd - |.endif - | cmplw cr5, CARG2, CARG4 - | crandc 4*cr0+gt, 4*cr0+eq, 4*cr1+gt // 2: Same type and primitive. - | crorc 4*cr0+lt, 4*cr5+eq, 4*cr0+eq // 1: Same tv or different type. - | crand 4*cr0+eq, 4*cr0+eq, 4*cr5+eq // 0: Same type and same tv. - | mr SAVE1, PC - | cror 4*cr0+eq, 4*cr0+eq, 4*cr0+gt // 0 or 2. - | cror 4*cr0+lt, 4*cr0+lt, 4*cr0+gt // 1 or 2. - if (vk) { - | bne cr0, >6 - | add PC, PC, SAVE0 - |6: - } else { - | beq cr0, >6 - | add PC, PC, SAVE0 - |6: - } - |.if DUALNUM - | bge cr0, >2 // Done if 1 or 2. - |1: - | ins_next - |2: - |.else - | blt cr0, <1 // Done if 1 or 2. - |.endif - | blt cr6, <1 // Done if not tab/ud. - | - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | mr CARG3, CARG4 - | lwz TAB:TMP2, TAB:CARG2->metatable - | li CARG4, 1-vk // ne = 0 or 1. - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable? - | lbz TMP2, TAB:TMP2->nomm - | andix. TMP2, TMP2, 1<vmeta_equal // Handle __eq metamethod. - break; - - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | // RA = src*8, RD = str_const*8 (~), JMP with RD = target - | lwzux TMP0, RA, BASE - | srwi RD, RD, 1 - | lwz STR:TMP3, 4(RA) - | lwz TMP2, 0(PC) - | subfic RD, RD, -4 - | addi PC, PC, 4 - |.if FFI - | cmpwi TMP0, LJ_TCDATA - |.endif - | lwzx STR:TMP1, KBASE, RD // KBASE-4-str_const*4 - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TSTR - |.if FFI - | beq ->vmeta_equal_cd - |.endif - | sub TMP1, STR:TMP1, STR:TMP3 - | or TMP0, TMP0, TMP1 - | decode_RD4 TMP2, TMP2 - | subfic TMP0, TMP0, 0 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | subfe TMP1, TMP1, TMP1 - if (vk) { - | andc TMP2, TMP2, TMP1 - } else { - | and TMP2, TMP2, TMP1 - } - | add PC, PC, TMP2 - | ins_next - break; - - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | // RA = src*8, RD = num_const*8, JMP with RD = target - |.if DUALNUM - | lwzux CARG1, RA, BASE - | addi PC, PC, 4 - | lwz CARG2, 4(RA) - | lwzux CARG3, RD, KBASE - | checknum cr0, CARG1 - | lwz SAVE0, -4(PC) - | checknum cr1, CARG3 - | decode_RD4 SAVE0, SAVE0 - | lwz CARG4, 4(RD) - | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16) - if (vk) { - |->BC_ISEQN_Z: - } else { - |->BC_ISNEN_Z: - } - | bne cr0, >7 - | bne cr1, >8 - | cmpw CARG2, CARG4 - |4: - |.else - if (vk) { - |->BC_ISEQN_Z: // Dummy label. - } else { - |->BC_ISNEN_Z: // Dummy label. - } - | lwzx CARG1, BASE, RA - | addi PC, PC, 4 - | lfdx f0, BASE, RA - | lwz SAVE0, -4(PC) - | lfdx f1, KBASE, RD - | decode_RD4 SAVE0, SAVE0 - | checknum CARG1 - | addis SAVE0, SAVE0, -(BCBIAS_J*4 >> 16) - | bge >3 - | fcmpu cr0, f0, f1 - |.endif - if (vk) { - | bne >1 - | add PC, PC, SAVE0 - |1: - |.if not FFI - |3: - |.endif - } else { - | beq >2 - |1: - |.if not FFI - |3: - |.endif - | add PC, PC, SAVE0 - |2: - } - | ins_next - |.if FFI - |3: - | cmpwi CARG1, LJ_TCDATA - | beq ->vmeta_equal_cd - | b <1 - |.endif - |.if DUALNUM - |7: // RA is not an integer. - | bge cr0, <3 - | // RA is a number. - | .FPU lfd f0, 0(RA) - | blt cr1, >1 - | // RA is a number, RD is an integer. - |.if FPU - | tonum_i f1, CARG4 - |.else - | bl ->vm_sfi2d_2 - |.endif - | b >2 - | - |8: // RA is an integer, RD is a number. - |.if FPU - | tonum_i f0, CARG2 - |.else - | bl ->vm_sfi2d_1 - |.endif - |1: - | .FPU lfd f1, 0(RD) - |2: - |.if FPU - | fcmpu cr0, f0, f1 - |.else - | blex __ledf2 - | cmpwi CRET1, 0 - |.endif - | b <4 - |.endif - break; - - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | // RA = src*8, RD = primitive_type*8 (~), JMP with RD = target - | lwzx TMP0, BASE, RA - | srwi TMP1, RD, 3 - | lwz TMP2, 0(PC) - | not TMP1, TMP1 - | addi PC, PC, 4 - |.if FFI - | cmpwi TMP0, LJ_TCDATA - |.endif - | sub TMP0, TMP0, TMP1 - |.if FFI - | beq ->vmeta_equal_cd - |.endif - | decode_RD4 TMP2, TMP2 - | .gpr64 extsw TMP0, TMP0 - | addic TMP0, TMP0, -1 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - | subfe TMP1, TMP1, TMP1 - if (vk) { - | and TMP2, TMP2, TMP1 - } else { - | andc TMP2, TMP2, TMP1 - } - | add PC, PC, TMP2 - | ins_next - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | // RA = dst*8 or unused, RD = src*8, JMP with RD = target - | lwzx TMP0, BASE, RD - | lwz INS, 0(PC) - | addi PC, PC, 4 - if (op == BC_IST || op == BC_ISF) { - | .gpr64 extsw TMP0, TMP0 - | subfic TMP0, TMP0, LJ_TTRUE - | decode_RD4 TMP2, INS - | subfe TMP1, TMP1, TMP1 - | addis TMP2, TMP2, -(BCBIAS_J*4 >> 16) - if (op == BC_IST) { - | andc TMP2, TMP2, TMP1 - } else { - | and TMP2, TMP2, TMP1 - } - | add PC, PC, TMP2 - } else { - | li TMP1, LJ_TFALSE - |.if FPU - | lfdx f0, BASE, RD - |.else - | lwzux CARG1, RD, BASE - | lwz CARG2, 4(RD) - |.endif - | cmplw TMP0, TMP1 - if (op == BC_ISTC) { - | bge >1 - } else { - | blt >1 - } - | addis PC, PC, -(BCBIAS_J*4 >> 16) - | decode_RD4 TMP2, INS - |.if FPU - | stfdx f0, BASE, RA - |.else - | stwux CARG1, RA, BASE - | stw CARG2, 4(RA) - |.endif - | add PC, PC, TMP2 - |1: - } - | ins_next - break; - - case BC_ISTYPE: - | // RA = src*8, RD = -type*8 - | lwzx TMP0, BASE, RA - | srwi TMP1, RD, 3 - | ins_next1 - |.if not PPE and not GPR64 - | add. TMP0, TMP0, TMP1 - |.else - | neg TMP1, TMP1 - | cmpw TMP0, TMP1 - |.endif - | bne ->vmeta_istype - | ins_next2 - break; - case BC_ISNUM: - | // RA = src*8, RD = -(TISNUM-1)*8 - | lwzx TMP0, BASE, RA - | ins_next1 - | checknum TMP0 - | bge ->vmeta_istype - | ins_next2 - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | // RA = dst*8, RD = src*8 - | ins_next1 - |.if FPU - | lfdx f0, BASE, RD - | stfdx f0, BASE, RA - |.else - | lwzux TMP0, RD, BASE - | lwz TMP1, 4(RD) - | stwux TMP0, RA, BASE - | stw TMP1, 4(RA) - |.endif - | ins_next2 - break; - case BC_NOT: - | // RA = dst*8, RD = src*8 - | ins_next1 - | lwzx TMP0, BASE, RD - | .gpr64 extsw TMP0, TMP0 - | subfic TMP1, TMP0, LJ_TTRUE - | adde TMP0, TMP0, TMP1 - | stwx TMP0, BASE, RA - | ins_next2 - break; - case BC_UNM: - | // RA = dst*8, RD = src*8 - | lwzux TMP1, RD, BASE - | lwz TMP0, 4(RD) - | checknum TMP1 - |.if DUALNUM - | bne >5 - |.if GPR64 - | lus TMP2, 0x8000 - | neg TMP0, TMP0 - | cmplw TMP0, TMP2 - | beq >4 - |.else - | nego. TMP0, TMP0 - | bso >4 - |1: - |.endif - | ins_next1 - | stwux TISNUM, RA, BASE - | stw TMP0, 4(RA) - |3: - | ins_next2 - |4: - |.if not GPR64 - | // Potential overflow. - | checkov TMP1, <1 // Ignore unrelated overflow. - |.endif - | lus TMP1, 0x41e0 // 2^31. - | li TMP0, 0 - | b >7 - |.endif - |5: - | bge ->vmeta_unm - | xoris TMP1, TMP1, 0x8000 - |7: - | ins_next1 - | stwux TMP1, RA, BASE - | stw TMP0, 4(RA) - |.if DUALNUM - | b <3 - |.else - | ins_next2 - |.endif - break; - case BC_LEN: - | // RA = dst*8, RD = src*8 - | lwzux TMP0, RD, BASE - | lwz CARG1, 4(RD) - | checkstr TMP0; bne >2 - | lwz CRET1, STR:CARG1->len - |1: - |.if DUALNUM - | ins_next1 - | stwux TISNUM, RA, BASE - | stw CRET1, 4(RA) - |.else - | tonum_u f0, CRET1 // Result is a non-negative integer. - | ins_next1 - | stfdx f0, BASE, RA - |.endif - | ins_next2 - |2: - | checktab TMP0; bne ->vmeta_len -#if LJ_52 - | lwz TAB:TMP2, TAB:CARG1->metatable - | cmplwi TAB:TMP2, 0 - | bne >9 - |3: -#endif - |->BC_LEN_Z: - | bl extern lj_tab_len // (GCtab *t) - | // Returns uint32_t (but less than 2^31). - | b <1 -#if LJ_52 - |9: - | lbz TMP0, TAB:TMP2->nomm - | andix. TMP0, TMP0, 1<vmeta_len -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | lwzx CARG1, BASE, RB - | .if DUALNUM - | lwzx CARG3, KBASE, RC - | .endif - | .if FPU - | lfdx f14, BASE, RB - | lfdx f15, KBASE, RC - | .else - | add TMP1, BASE, RB - | add TMP2, KBASE, RC - | lwz CARG2, 4(TMP1) - | lwz CARG4, 4(TMP2) - | .endif - | .if DUALNUM - | checknum cr0, CARG1 - | checknum cr1, CARG3 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_vn - | .else - | checknum CARG1; bge ->vmeta_arith_vn - | .endif - || break; - ||case 1: - | lwzx CARG1, BASE, RB - | .if DUALNUM - | lwzx CARG3, KBASE, RC - | .endif - | .if FPU - | lfdx f15, BASE, RB - | lfdx f14, KBASE, RC - | .else - | add TMP1, BASE, RB - | add TMP2, KBASE, RC - | lwz CARG2, 4(TMP1) - | lwz CARG4, 4(TMP2) - | .endif - | .if DUALNUM - | checknum cr0, CARG1 - | checknum cr1, CARG3 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_nv - | .else - | checknum CARG1; bge ->vmeta_arith_nv - | .endif - || break; - ||default: - | lwzx CARG1, BASE, RB - | lwzx CARG3, BASE, RC - | .if FPU - | lfdx f14, BASE, RB - | lfdx f15, BASE, RC - | .else - | add TMP1, BASE, RB - | add TMP2, BASE, RC - | lwz CARG2, 4(TMP1) - | lwz CARG4, 4(TMP2) - | .endif - | checknum cr0, CARG1 - | checknum cr1, CARG3 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_vv - || break; - ||} - |.endmacro - | - |.macro ins_arithfallback, ins - ||switch (vk) { - ||case 0: - | ins ->vmeta_arith_vn2 - || break; - ||case 1: - | ins ->vmeta_arith_nv2 - || break; - ||default: - | ins ->vmeta_arith_vv2 - || break; - ||} - |.endmacro - | - |.macro intmod, a, b, c - | bl ->vm_modi - |.endmacro - | - |.macro fpmod, a, b, c - |->BC_MODVN_Z: - | fdiv FARG1, b, c - | // NYI: Use internal implementation of floor. - | blex floor // floor(b/c) - | fmul a, FARG1, c - | fsub a, b, a // b - floor(b/c)*c - |.endmacro - | - |.macro sfpmod - |->BC_MODVN_Z: - | stw CARG1, SFSAVE_1 - | stw CARG2, SFSAVE_2 - | mr SAVE0, CARG3 - | mr SAVE1, CARG4 - | blex __divdf3 - | blex floor - | mr CARG3, SAVE0 - | mr CARG4, SAVE1 - | blex __muldf3 - | mr CARG3, CRET1 - | mr CARG4, CRET2 - | lwz CARG1, SFSAVE_1 - | lwz CARG2, SFSAVE_2 - | blex __subdf3 - |.endmacro - | - |.macro ins_arithfp, fpins - | ins_arithpre - |.if "fpins" == "fpmod_" - | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - |.elif FPU - | fpins f0, f14, f15 - | ins_next1 - | stfdx f0, BASE, RA - | ins_next2 - |.else - | blex __divdf3 // Only soft-float div uses this macro. - | ins_next1 - | stwux CRET1, RA, BASE - | stw CRET2, 4(RA) - | ins_next2 - |.endif - |.endmacro - | - |.macro ins_arithdn, intins, fpins, fpcall - | // RA = dst*8, RB = src1*8, RC = src2*8 | num_const*8 - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | lwzux CARG1, RB, BASE - | lwzux CARG3, RC, KBASE - | lwz CARG2, 4(RB) - | checknum cr0, CARG1 - | lwz CARG4, 4(RC) - | checknum cr1, CARG3 - || break; - ||case 1: - | lwzux CARG3, RB, BASE - | lwzux CARG1, RC, KBASE - | lwz CARG4, 4(RB) - | checknum cr0, CARG3 - | lwz CARG2, 4(RC) - | checknum cr1, CARG1 - || break; - ||default: - | lwzux CARG1, RB, BASE - | lwzux CARG3, RC, BASE - | lwz CARG2, 4(RB) - | checknum cr0, CARG1 - | lwz CARG4, 4(RC) - | checknum cr1, CARG3 - || break; - ||} - | bne >5 - | bne cr1, >5 - |.if "intins" == "intmod" - | mr CARG1, CARG2 - | mr CARG2, CARG4 - |.endif - | intins CARG1, CARG2, CARG4 - | bso >4 - |1: - | ins_next1 - | stwux TISNUM, RA, BASE - | stw CARG1, 4(RA) - |2: - | ins_next2 - |4: // Overflow. - | checkov TMP0, <1 // Ignore unrelated overflow. - | ins_arithfallback b - |5: // FP variant. - |.if FPU - ||if (vk == 1) { - | lfd f15, 0(RB) - | lfd f14, 0(RC) - ||} else { - | lfd f14, 0(RB) - | lfd f15, 0(RC) - ||} - |.endif - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | ins_arithfallback bge - |.if "fpins" == "fpmod_" - | b ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - |.else - |.if FPU - | fpins f0, f14, f15 - | stfdx f0, BASE, RA - |.else - |.if "fpcall" == "sfpmod" - | sfpmod - |.else - | blex fpcall - |.endif - | stwux CRET1, RA, BASE - | stw CRET2, 4(RA) - |.endif - | ins_next1 - | b <2 - |.endif - |.endmacro - | - |.macro ins_arith, intins, fpins, fpcall - |.if DUALNUM - | ins_arithdn intins, fpins, fpcall - |.else - | ins_arithfp fpins - |.endif - |.endmacro - - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - |.if GPR64 - |.macro addo32., y, a, b - | // Need to check overflow for (a<<32) + (b<<32). - | rldicr TMP0, a, 32, 31 - | rldicr TMP1, b, 32, 31 - | addo. TMP0, TMP0, TMP1 - | add y, a, b - |.endmacro - | ins_arith addo32., fadd, __adddf3 - |.else - | ins_arith addo., fadd, __adddf3 - |.endif - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - |.if GPR64 - |.macro subo32., y, a, b - | // Need to check overflow for (a<<32) - (b<<32). - | rldicr TMP0, a, 32, 31 - | rldicr TMP1, b, 32, 31 - | subo. TMP0, TMP0, TMP1 - | sub y, a, b - |.endmacro - | ins_arith subo32., fsub, __subdf3 - |.else - | ins_arith subo., fsub, __subdf3 - |.endif - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith mullwo., fmul, __muldf3 - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arithfp fdiv - break; - case BC_MODVN: - | ins_arith intmod, fpmod, sfpmod - break; - case BC_MODNV: case BC_MODVV: - | ins_arith intmod, fpmod_, sfpmod - break; - case BC_POW: - | // NYI: (partial) integer arithmetic. - | lwzx CARG1, BASE, RB - | lwzx CARG3, BASE, RC - |.if FPU - | lfdx FARG1, BASE, RB - | lfdx FARG2, BASE, RC - |.else - | add TMP1, BASE, RB - | add TMP2, BASE, RC - | lwz CARG2, 4(TMP1) - | lwz CARG4, 4(TMP2) - |.endif - | checknum cr0, CARG1 - | checknum cr1, CARG3 - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - | bge ->vmeta_arith_vv - | blex pow - | ins_next1 - |.if FPU - | stfdx FARG1, BASE, RA - |.else - | stwux CARG1, RA, BASE - | stw CARG2, 4(RA) - |.endif - | ins_next2 - break; - - case BC_CAT: - | // RA = dst*8, RB = src_start*8, RC = src_end*8 - | sub CARG3, RC, RB - | stp BASE, L->base - | add CARG2, BASE, RC - | mr SAVE0, RB - |->BC_CAT_Z: - | stw PC, SAVE_PC - | mr CARG1, L - | srwi CARG3, CARG3, 3 - | bl extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // Returns NULL (finished) or TValue * (metamethod). - | cmplwi CRET1, 0 - | lp BASE, L->base - | bne ->vmeta_binop - | ins_next1 - |.if FPU - | lfdx f0, BASE, SAVE0 // Copy result from RB to RA. - | stfdx f0, BASE, RA - |.else - | lwzux TMP0, SAVE0, BASE - | lwz TMP1, 4(SAVE0) - | stwux TMP0, RA, BASE - | stw TMP1, 4(RA) - |.endif - | ins_next2 - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | // RA = dst*8, RD = str_const*8 (~) - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | ins_next1 - | lwzx TMP0, KBASE, TMP1 // KBASE-4-str_const*4 - | li TMP2, LJ_TSTR - | stwux TMP2, RA, BASE - | stw TMP0, 4(RA) - | ins_next2 - break; - case BC_KCDATA: - |.if FFI - | // RA = dst*8, RD = cdata_const*8 (~) - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | ins_next1 - | lwzx TMP0, KBASE, TMP1 // KBASE-4-cdata_const*4 - | li TMP2, LJ_TCDATA - | stwux TMP2, RA, BASE - | stw TMP0, 4(RA) - | ins_next2 - |.endif - break; - case BC_KSHORT: - | // RA = dst*8, RD = int16_literal*8 - |.if DUALNUM - | slwi RD, RD, 13 - | srawi RD, RD, 16 - | ins_next1 - | stwux TISNUM, RA, BASE - | stw RD, 4(RA) - | ins_next2 - |.else - | // The soft-float approach is faster. - | slwi RD, RD, 13 - | srawi TMP1, RD, 31 - | xor TMP2, TMP1, RD - | sub TMP2, TMP2, TMP1 // TMP2 = abs(x) - | cntlzw TMP3, TMP2 - | subfic TMP1, TMP3, 0x40d // TMP1 = exponent-1 - | slw TMP2, TMP2, TMP3 // TMP2 = left aligned mantissa - | subfic TMP3, RD, 0 - | slwi TMP1, TMP1, 20 - | rlwimi RD, TMP2, 21, 1, 31 // hi = sign(x) | (mantissa>>11) - | subfe TMP0, TMP0, TMP0 - | add RD, RD, TMP1 // hi = hi + exponent-1 - | and RD, RD, TMP0 // hi = x == 0 ? 0 : hi - | ins_next1 - | stwux RD, RA, BASE - | stw ZERO, 4(RA) - | ins_next2 - |.endif - break; - case BC_KNUM: - | // RA = dst*8, RD = num_const*8 - | ins_next1 - |.if FPU - | lfdx f0, KBASE, RD - | stfdx f0, BASE, RA - |.else - | lwzux TMP0, RD, KBASE - | lwz TMP1, 4(RD) - | stwux TMP0, RA, BASE - | stw TMP1, 4(RA) - |.endif - | ins_next2 - break; - case BC_KPRI: - | // RA = dst*8, RD = primitive_type*8 (~) - | srwi TMP1, RD, 3 - | not TMP0, TMP1 - | ins_next1 - | stwx TMP0, BASE, RA - | ins_next2 - break; - case BC_KNIL: - | // RA = base*8, RD = end*8 - | stwx TISNIL, BASE, RA - | addi RA, RA, 8 - |1: - | stwx TISNIL, BASE, RA - | cmpw RA, RD - | addi RA, RA, 8 - | blt <1 - | ins_next_ - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | // RA = dst*8, RD = uvnum*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RD, RD, 1 - | addi RD, RD, offsetof(GCfuncL, uvptr) - | lwzx UPVAL:RB, LFUNC:RB, RD - | ins_next1 - | lwz TMP1, UPVAL:RB->v - |.if FPU - | lfd f0, 0(TMP1) - | stfdx f0, BASE, RA - |.else - | lwz TMP2, 0(TMP1) - | lwz TMP3, 4(TMP1) - | stwux TMP2, RA, BASE - | stw TMP3, 4(RA) - |.endif - | ins_next2 - break; - case BC_USETV: - | // RA = uvnum*8, RD = src*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - |.if FPU - | lfdux f0, RD, BASE - |.else - | lwzux CARG1, RD, BASE - | lwz CARG3, 4(RD) - |.endif - | lwzx UPVAL:RB, LFUNC:RB, RA - | lbz TMP3, UPVAL:RB->marked - | lwz CARG2, UPVAL:RB->v - | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbz TMP0, UPVAL:RB->closed - | lwz TMP2, 0(RD) - |.if FPU - | stfd f0, 0(CARG2) - |.else - | stw CARG1, 0(CARG2) - | stw CARG3, 4(CARG2) - |.endif - | cmplwi cr1, TMP0, 0 - | lwz TMP1, 4(RD) - | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | subi TMP2, TMP2, (LJ_TNUMX+1) - | bne >2 // Upvalue is closed and black? - |1: - | ins_next - | - |2: // Check if new value is collectable. - | cmplwi TMP2, LJ_TISGCV - (LJ_TNUMX+1) - | bge <1 // tvisgcv(v) - | lbz TMP3, GCOBJ:TMP1->gch.marked - | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(v) - | la CARG1, GG_DISP2G(DISPATCH) - | // Crossed a write barrier. Move the barrier forward. - | beq <1 - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETS: - | // RA = uvnum*8, RD = str_const*8 (~) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi TMP1, RD, 1 - | srwi RA, RA, 1 - | subfic TMP1, TMP1, -4 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | lwzx STR:TMP1, KBASE, TMP1 // KBASE-4-str_const*4 - | lwzx UPVAL:RB, LFUNC:RB, RA - | lbz TMP3, UPVAL:RB->marked - | lwz CARG2, UPVAL:RB->v - | andix. TMP3, TMP3, LJ_GC_BLACK // isblack(uv) - | lbz TMP3, STR:TMP1->marked - | lbz TMP2, UPVAL:RB->closed - | li TMP0, LJ_TSTR - | stw STR:TMP1, 4(CARG2) - | stw TMP0, 0(CARG2) - | bne >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | andix. TMP3, TMP3, LJ_GC_WHITES // iswhite(str) - | cmplwi cr1, TMP2, 0 - | cror 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | la CARG1, GG_DISP2G(DISPATCH) - | // Crossed a write barrier. Move the barrier forward. - | beq <1 - | bl extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | b <1 - break; - case BC_USETN: - | // RA = uvnum*8, RD = num_const*8 - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | addi RA, RA, offsetof(GCfuncL, uvptr) - |.if FPU - | lfdx f0, KBASE, RD - |.else - | lwzux TMP2, RD, KBASE - | lwz TMP3, 4(RD) - |.endif - | lwzx UPVAL:RB, LFUNC:RB, RA - | ins_next1 - | lwz TMP1, UPVAL:RB->v - |.if FPU - | stfd f0, 0(TMP1) - |.else - | stw TMP2, 0(TMP1) - | stw TMP3, 4(TMP1) - |.endif - | ins_next2 - break; - case BC_USETP: - | // RA = uvnum*8, RD = primitive_type*8 (~) - | lwz LFUNC:RB, FRAME_FUNC(BASE) - | srwi RA, RA, 1 - | srwi TMP0, RD, 3 - | addi RA, RA, offsetof(GCfuncL, uvptr) - | not TMP0, TMP0 - | lwzx UPVAL:RB, LFUNC:RB, RA - | ins_next1 - | lwz TMP1, UPVAL:RB->v - | stw TMP0, 0(TMP1) - | ins_next2 - break; - - case BC_UCLO: - | // RA = level*8, RD = target - | lwz TMP1, L->openupval - | branch_RD // Do this first since RD is not saved. - | stp BASE, L->base - | cmplwi TMP1, 0 - | mr CARG1, L - | beq >1 - | add CARG2, BASE, RA - | bl extern lj_func_closeuv // (lua_State *L, TValue *level) - | lp BASE, L->base - |1: - | ins_next - break; - - case BC_FNEW: - | // RA = dst*8, RD = proto_const*8 (~) (holding function prototype) - | srwi TMP1, RD, 1 - | stp BASE, L->base - | subfic TMP1, TMP1, -4 - | stw PC, SAVE_PC - | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4 - | mr CARG1, L - | lwz CARG3, FRAME_FUNC(BASE) - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | bl extern lj_func_newL_gc - | // Returns GCfuncL *. - | lp BASE, L->base - | li TMP0, LJ_TFUNC - | stwux TMP0, RA, BASE - | stw LFUNC:CRET1, 4(RA) - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - case BC_TDUP: - | // RA = dst*8, RD = (hbits|asize)*8 | tab_const*8 (~) - | lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH) - | mr CARG1, L - | lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH) - | stp BASE, L->base - | cmplw TMP0, TMP1 - | stw PC, SAVE_PC - | bge >5 - |1: - if (op == BC_TNEW) { - | rlwinm CARG2, RD, 29, 21, 31 - | rlwinm CARG3, RD, 18, 27, 31 - | cmpwi CARG2, 0x7ff; beq >3 - |2: - | bl extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Returns Table *. - } else { - | srwi TMP1, RD, 1 - | subfic TMP1, TMP1, -4 - | lwzx CARG2, KBASE, TMP1 // KBASE-4-tab_const*4 - | bl extern lj_tab_dup // (lua_State *L, Table *kt) - | // Returns Table *. - } - | lp BASE, L->base - | li TMP0, LJ_TTAB - | stwux TMP0, RA, BASE - | stw TAB:CRET1, 4(RA) - | ins_next - if (op == BC_TNEW) { - |3: - | li CARG2, 0x801 - | b <2 - } - |5: - | mr SAVE0, RD - | bl extern lj_gc_step_fixtop // (lua_State *L) - | mr RD, SAVE0 - | mr CARG1, L - | b <1 - break; - - case BC_GGET: - | // RA = dst*8, RD = str_const*8 (~) - case BC_GSET: - | // RA = src*8, RD = str_const*8 (~) - | lwz LFUNC:TMP2, FRAME_FUNC(BASE) - | srwi TMP1, RD, 1 - | lwz TAB:RB, LFUNC:TMP2->env - | subfic TMP1, TMP1, -4 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - if (op == BC_GGET) { - | b ->BC_TGETS_Z - } else { - | b ->BC_TSETS_Z - } - break; - - case BC_TGETV: - | // RA = dst*8, RB = table*8, RC = key*8 - | lwzux CARG1, RB, BASE - | lwzux CARG2, RC, BASE - | lwz TAB:RB, 4(RB) - |.if DUALNUM - | lwz RC, 4(RC) - |.else - | lfd f0, 0(RC) - |.endif - | checktab CARG1 - | checknum cr1, CARG2 - | bne ->vmeta_tgetv - |.if DUALNUM - | lwz TMP0, TAB:RB->asize - | bne cr1, >5 - | lwz TMP1, TAB:RB->array - | cmplw TMP0, RC - | slwi TMP2, RC, 3 - |.else - | bge cr1, >5 - | // Convert number key to integer, check for integerness and range. - | fctiwz f1, f0 - | fadd f2, f0, TOBIT - | stfd f1, TMPD - | lwz TMP0, TAB:RB->asize - | fsub f2, f2, TOBIT - | lwz TMP2, TMPD_LO - | lwz TMP1, TAB:RB->array - | fcmpu cr1, f0, f2 - | cmplw cr0, TMP0, TMP2 - | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq - | slwi TMP2, TMP2, 3 - |.endif - | ble ->vmeta_tgetv // Integer key and in array part? - | lwzx TMP0, TMP1, TMP2 - |.if FPU - | lfdx f14, TMP1, TMP2 - |.else - | lwzux SAVE0, TMP1, TMP2 - | lwz SAVE1, 4(TMP1) - |.endif - | checknil TMP0; beq >2 - |1: - | ins_next1 - |.if FPU - | stfdx f14, BASE, RA - |.else - | stwux SAVE0, RA, BASE - | stw SAVE1, 4(RA) - |.endif - | ins_next2 - | - |2: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP0, TAB:TMP2->nomm - | andix. TMP0, TMP0, 1<vmeta_tgetv - | - |5: - | checkstr CARG2; bne ->vmeta_tgetv - |.if not DUALNUM - | lwz STR:RC, 4(RC) - |.endif - | b ->BC_TGETS_Z // String key? - break; - case BC_TGETS: - | // RA = dst*8, RB = table*8, RC = str_const*8 (~) - | lwzux CARG1, RB, BASE - | srwi TMP1, RC, 1 - | lwz TAB:RB, 4(RB) - | subfic TMP1, TMP1, -4 - | checktab CARG1 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - | bne ->vmeta_tgets1 - |->BC_TGETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = dst*8 - | lwz TMP0, TAB:RB->hmask - | lwz TMP1, STR:RC->sid - | lwz NODE:TMP2, TAB:RB->node - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | lwz CARG1, NODE:TMP2->key - | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2) - | lwz CARG2, NODE:TMP2->val - | lwz TMP1, 4+offsetof(Node, val)(NODE:TMP2) - | checkstr CARG1; bne >4 - | cmpw TMP0, STR:RC; bne >4 - | checknil CARG2; beq >5 // Key found, but nil value? - |3: - | stwux CARG2, RA, BASE - | stw TMP1, 4(RA) - | ins_next - | - |4: // Follow hash chain. - | lwz NODE:TMP2, NODE:TMP2->next - | cmplwi NODE:TMP2, 0 - | bne <1 - | // End of hash chain: key not found, nil result. - | li CARG2, LJ_TNIL - | - |5: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <3 // No metatable: done. - | lbz TMP0, TAB:TMP2->nomm - | andix. TMP0, TMP0, 1<vmeta_tgets - break; - case BC_TGETB: - | // RA = dst*8, RB = table*8, RC = index*8 - | lwzux CARG1, RB, BASE - | srwi TMP0, RC, 3 - | lwz TAB:RB, 4(RB) - | checktab CARG1; bne ->vmeta_tgetb - | lwz TMP1, TAB:RB->asize - | lwz TMP2, TAB:RB->array - | cmplw TMP0, TMP1; bge ->vmeta_tgetb - |.if FPU - | lwzx TMP1, TMP2, RC - | lfdx f0, TMP2, RC - |.else - | lwzux TMP1, TMP2, RC - | lwz TMP3, 4(TMP2) - |.endif - | checknil TMP1; beq >5 - |1: - | ins_next1 - |.if FPU - | stfdx f0, BASE, RA - |.else - | stwux TMP1, RA, BASE - | stw TMP3, 4(RA) - |.endif - | ins_next2 - | - |5: // Check for __index if table value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP2, TAB:TMP2->nomm - | andix. TMP2, TMP2, 1<vmeta_tgetb // Caveat: preserve TMP0! - break; - case BC_TGETR: - | // RA = dst*8, RB = table*8, RC = key*8 - | add RB, BASE, RB - | lwz TAB:CARG1, 4(RB) - |.if DUALNUM - | add RC, BASE, RC - | lwz TMP0, TAB:CARG1->asize - | lwz CARG2, 4(RC) - | lwz TMP1, TAB:CARG1->array - |.else - | lfdx f0, BASE, RC - | lwz TMP0, TAB:CARG1->asize - | toint CARG2, f0 - | lwz TMP1, TAB:CARG1->array - |.endif - | cmplw TMP0, CARG2 - | slwi TMP2, CARG2, 3 - | ble ->vmeta_tgetr // In array part? - |.if FPU - | lfdx f14, TMP1, TMP2 - |.else - | lwzux SAVE0, TMP2, TMP1 - | lwz SAVE1, 4(TMP2) - |.endif - |->BC_TGETR_Z: - | ins_next1 - |.if FPU - | stfdx f14, BASE, RA - |.else - | stwux SAVE0, RA, BASE - | stw SAVE1, 4(RA) - |.endif - | ins_next2 - break; - - case BC_TSETV: - | // RA = src*8, RB = table*8, RC = key*8 - | lwzux CARG1, RB, BASE - | lwzux CARG2, RC, BASE - | lwz TAB:RB, 4(RB) - |.if DUALNUM - | lwz RC, 4(RC) - |.else - | lfd f0, 0(RC) - |.endif - | checktab CARG1 - | checknum cr1, CARG2 - | bne ->vmeta_tsetv - |.if DUALNUM - | lwz TMP0, TAB:RB->asize - | bne cr1, >5 - | lwz TMP1, TAB:RB->array - | cmplw TMP0, RC - | slwi TMP0, RC, 3 - |.else - | bge cr1, >5 - | // Convert number key to integer, check for integerness and range. - | fctiwz f1, f0 - | fadd f2, f0, TOBIT - | stfd f1, TMPD - | lwz TMP0, TAB:RB->asize - | fsub f2, f2, TOBIT - | lwz TMP2, TMPD_LO - | lwz TMP1, TAB:RB->array - | fcmpu cr1, f0, f2 - | cmplw cr0, TMP0, TMP2 - | crand 4*cr0+gt, 4*cr0+gt, 4*cr1+eq - | slwi TMP0, TMP2, 3 - |.endif - | ble ->vmeta_tsetv // Integer key and in array part? - | lwzx TMP2, TMP1, TMP0 - | lbz TMP3, TAB:RB->marked - |.if FPU - | lfdx f14, BASE, RA - |.else - | add SAVE1, BASE, RA - | lwz SAVE0, 0(SAVE1) - | lwz SAVE1, 4(SAVE1) - |.endif - | checknil TMP2; beq >3 - |1: - | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table) - |.if FPU - | stfdx f14, TMP1, TMP0 - |.else - | stwux SAVE0, TMP1, TMP0 - | stw SAVE1, 4(TMP1) - |.endif - | bne >7 - |2: - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | lwz TAB:TMP2, TAB:RB->metatable - | cmplwi TAB:TMP2, 0 - | beq <1 // No metatable: done. - | lbz TMP2, TAB:TMP2->nomm - | andix. TMP2, TMP2, 1<vmeta_tsetv - | - |5: - | checkstr CARG2; bne ->vmeta_tsetv - |.if not DUALNUM - | lwz STR:RC, 4(RC) - |.endif - | b ->BC_TSETS_Z // String key? - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <2 - break; - case BC_TSETS: - | // RA = src*8, RB = table*8, RC = str_const*8 (~) - | lwzux CARG1, RB, BASE - | srwi TMP1, RC, 1 - | lwz TAB:RB, 4(RB) - | subfic TMP1, TMP1, -4 - | checktab CARG1 - | lwzx STR:RC, KBASE, TMP1 // KBASE-4-str_const*4 - | bne ->vmeta_tsets1 - |->BC_TSETS_Z: - | // TAB:RB = GCtab *, STR:RC = GCstr *, RA = src*8 - | lwz TMP0, TAB:RB->hmask - | lwz TMP1, STR:RC->sid - | lwz NODE:TMP2, TAB:RB->node - | stb ZERO, TAB:RB->nomm // Clear metamethod cache. - | and TMP1, TMP1, TMP0 // idx = str->sid & tab->hmask - |.if FPU - | lfdx f14, BASE, RA - |.else - | add CARG2, BASE, RA - | lwz SAVE0, 0(CARG2) - | lwz SAVE1, 4(CARG2) - |.endif - | slwi TMP0, TMP1, 5 - | slwi TMP1, TMP1, 3 - | sub TMP1, TMP0, TMP1 - | lbz TMP3, TAB:RB->marked - | add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8) - |1: - | lwz CARG1, NODE:TMP2->key - | lwz TMP0, 4+offsetof(Node, key)(NODE:TMP2) - | lwz CARG2, NODE:TMP2->val - | lwz NODE:TMP1, NODE:TMP2->next - | checkstr CARG1; bne >5 - | cmpw TMP0, STR:RC; bne >5 - | checknil CARG2; beq >4 // Key found, but nil value? - |2: - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |.if FPU - | stfd f14, NODE:TMP2->val - |.else - | stw SAVE0, NODE:TMP2->val.u32.hi - | stw SAVE1, NODE:TMP2->val.u32.lo - |.endif - | bne >7 - |3: - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | lwz TAB:TMP1, TAB:RB->metatable - | cmplwi TAB:TMP1, 0 - | beq <2 // No metatable: done. - | lbz TMP0, TAB:TMP1->nomm - | andix. TMP0, TMP0, 1<vmeta_tsets - | - |5: // Follow hash chain. - | cmplwi NODE:TMP1, 0 - | mr NODE:TMP2, NODE:TMP1 - | bne <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | lwz TAB:TMP1, TAB:RB->metatable - | la CARG3, DISPATCH_GL(tmptv)(DISPATCH) - | stw PC, SAVE_PC - | mr CARG1, L - | cmplwi TAB:TMP1, 0 - | stp BASE, L->base - | beq >6 // No metatable: continue. - | lbz TMP0, TAB:TMP1->nomm - | andix. TMP0, TMP0, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | li TMP0, LJ_TSTR - | stw STR:RC, 4(CARG3) - | mr CARG2, TAB:RB - | stw TMP0, 0(CARG3) - | bl extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Returns TValue *. - | lp BASE, L->base - |.if FPU - | stfd f14, 0(CRET1) - |.else - | stw SAVE0, 0(CRET1) - | stw SAVE1, 4(CRET1) - |.endif - | b <3 // No 2nd write barrier needed. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <3 - break; - case BC_TSETB: - | // RA = src*8, RB = table*8, RC = index*8 - | lwzux CARG1, RB, BASE - | srwi TMP0, RC, 3 - | lwz TAB:RB, 4(RB) - | checktab CARG1; bne ->vmeta_tsetb - | lwz TMP1, TAB:RB->asize - | lwz TMP2, TAB:RB->array - | lbz TMP3, TAB:RB->marked - | cmplw TMP0, TMP1 - |.if FPU - | lfdx f14, BASE, RA - |.else - | add CARG2, BASE, RA - | lwz SAVE0, 0(CARG2) - | lwz SAVE1, 4(CARG2) - |.endif - | bge ->vmeta_tsetb - | lwzx TMP1, TMP2, RC - | checknil TMP1; beq >5 - |1: - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |.if FPU - | stfdx f14, TMP2, RC - |.else - | stwux SAVE0, RC, TMP2 - | stw SAVE1, 4(RC) - |.endif - | bne >7 - |2: - | ins_next - | - |5: // Check for __newindex if previous value is nil. - | lwz TAB:TMP1, TAB:RB->metatable - | cmplwi TAB:TMP1, 0 - | beq <1 // No metatable: done. - | lbz TMP1, TAB:TMP1->nomm - | andix. TMP1, TMP1, 1<vmeta_tsetb // Caveat: preserve TMP0! - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMP3, TMP0 - | b <2 - break; - case BC_TSETR: - | // RA = dst*8, RB = table*8, RC = key*8 - | add RB, BASE, RB - | lwz TAB:CARG2, 4(RB) - |.if DUALNUM - | add RC, BASE, RC - | lbz TMP3, TAB:CARG2->marked - | lwz TMP0, TAB:CARG2->asize - | lwz CARG3, 4(RC) - | lwz TMP1, TAB:CARG2->array - |.else - | lfdx f0, BASE, RC - | lbz TMP3, TAB:CARG2->marked - | lwz TMP0, TAB:CARG2->asize - | toint CARG3, f0 - | lwz TMP1, TAB:CARG2->array - |.endif - | andix. TMP2, TMP3, LJ_GC_BLACK // isblack(table) - | bne >7 - |2: - | cmplw TMP0, CARG3 - | slwi TMP2, CARG3, 3 - |.if FPU - | lfdx f14, BASE, RA - |.else - | lwzux SAVE0, RA, BASE - | lwz SAVE1, 4(RA) - |.endif - | ble ->vmeta_tsetr // In array part? - | ins_next1 - |.if FPU - | stfdx f14, TMP1, TMP2 - |.else - | stwux SAVE0, TMP1, TMP2 - | stw SAVE1, 4(TMP1) - |.endif - | ins_next2 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP2 - | b <2 - break; - - - case BC_TSETM: - | // RA = base*8 (table at base-1), RD = num_const*8 (start index) - | add RA, BASE, RA - |1: - | add TMP3, KBASE, RD - | lwz TAB:CARG2, -4(RA) // Guaranteed to be a table. - | addic. TMP0, MULTRES, -8 - | lwz TMP3, 4(TMP3) // Integer constant is in lo-word. - | srwi CARG3, TMP0, 3 - | beq >4 // Nothing to copy? - | add CARG3, CARG3, TMP3 - | lwz TMP2, TAB:CARG2->asize - | slwi TMP1, TMP3, 3 - | lbz TMP3, TAB:CARG2->marked - | cmplw CARG3, TMP2 - | add TMP2, RA, TMP0 - | lwz TMP0, TAB:CARG2->array - | bgt >5 - | add TMP1, TMP1, TMP0 - | andix. TMP0, TMP3, LJ_GC_BLACK // isblack(table) - |3: // Copy result slots to table. - |.if FPU - | lfd f0, 0(RA) - |.else - | lwz SAVE0, 0(RA) - | lwz SAVE1, 4(RA) - |.endif - | addi RA, RA, 8 - | cmpw cr1, RA, TMP2 - |.if FPU - | stfd f0, 0(TMP1) - |.else - | stw SAVE0, 0(TMP1) - | stw SAVE1, 4(TMP1) - |.endif - | addi TMP1, TMP1, 8 - | blt cr1, <3 - | bne >7 - |4: - | ins_next - | - |5: // Need to resize array part. - | stp BASE, L->base - | mr CARG1, L - | stw PC, SAVE_PC - | mr SAVE0, RD - | bl extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | // Must not reallocate the stack. - | mr RD, SAVE0 - | b <1 - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:CARG2, TMP3, TMP0 - | b <4 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALLM: - | // RA = base*8, (RB = (nresults+1)*8,) RC = extra_nargs*8 - | add NARGS8:RC, NARGS8:RC, MULTRES - | // Fall through. Assumes BC_CALL follows. - break; - case BC_CALL: - | // RA = base*8, (RB = (nresults+1)*8,) RC = (nargs+1)*8 - | mr TMP2, BASE - | lwzux TMP0, BASE, RA - | lwz LFUNC:RB, 4(BASE) - | subi NARGS8:RC, NARGS8:RC, 8 - | addi BASE, BASE, 8 - | checkfunc TMP0; bne ->vmeta_call - | ins_call - break; - - case BC_CALLMT: - | // RA = base*8, (RB = 0,) RC = extra_nargs*8 - | add NARGS8:RC, NARGS8:RC, MULTRES - | // Fall through. Assumes BC_CALLT follows. - break; - case BC_CALLT: - | // RA = base*8, (RB = 0,) RC = (nargs+1)*8 - | lwzux TMP0, RA, BASE - | lwz LFUNC:RB, 4(RA) - | subi NARGS8:RC, NARGS8:RC, 8 - | lwz TMP1, FRAME_PC(BASE) - | checkfunc TMP0 - | addi RA, RA, 8 - | bne ->vmeta_callt - |->BC_CALLT_Z: - | andix. TMP0, TMP1, FRAME_TYPE // Caveat: preserve cr0 until the crand. - | lbz TMP3, LFUNC:RB->ffid - | xori TMP2, TMP1, FRAME_VARG - | cmplwi cr1, NARGS8:RC, 0 - | bne >7 - |1: - | stw LFUNC:RB, FRAME_FUNC(BASE) // Copy function down, but keep PC. - | li TMP2, 0 - | cmplwi cr7, TMP3, 1 // (> FF_C) Calling a fast function? - | beq cr1, >3 - |2: - | addi TMP3, TMP2, 8 - |.if FPU - | lfdx f0, RA, TMP2 - |.else - | add CARG3, RA, TMP2 - | lwz CARG1, 0(CARG3) - | lwz CARG2, 4(CARG3) - |.endif - | cmplw cr1, TMP3, NARGS8:RC - |.if FPU - | stfdx f0, BASE, TMP2 - |.else - | stwux CARG1, TMP2, BASE - | stw CARG2, 4(TMP2) - |.endif - | mr TMP2, TMP3 - | bne cr1, <2 - |3: - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+gt - | beq >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function with a Lua frame below. - | lwz INS, -4(TMP1) - | decode_RA8 RA, INS - | sub TMP1, BASE, RA - | lwz LFUNC:TMP1, FRAME_FUNC-8(TMP1) - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) // Need to prepare KBASE. - | b <4 - | - |7: // Tailcall from a vararg function. - | andix. TMP0, TMP2, FRAME_TYPEP - | bne <1 // Vararg frame below? - | sub BASE, BASE, TMP2 // Relocate BASE down. - | lwz TMP1, FRAME_PC(BASE) - | andix. TMP0, TMP1, FRAME_TYPE - | b <1 - break; - - case BC_ITERC: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 ((2+1)*8)) - | mr TMP2, BASE - | add BASE, BASE, RA - | lwz TMP1, -24(BASE) - | lwz LFUNC:RB, -20(BASE) - |.if FPU - | lfd f1, -8(BASE) - | lfd f0, -16(BASE) - |.else - | lwz CARG1, -8(BASE) - | lwz CARG2, -4(BASE) - | lwz CARG3, -16(BASE) - | lwz CARG4, -12(BASE) - |.endif - | stw TMP1, 0(BASE) // Copy callable. - | stw LFUNC:RB, 4(BASE) - | checkfunc TMP1 - | li NARGS8:RC, 16 // Iterators get 2 arguments. - |.if FPU - | stfd f1, 16(BASE) // Copy control var. - | stfdu f0, 8(BASE) // Copy state. - |.else - | stw CARG1, 16(BASE) // Copy control var. - | stw CARG2, 20(BASE) - | stwu CARG3, 8(BASE) // Copy state. - | stw CARG4, 4(BASE) - |.endif - | bne ->vmeta_call - | ins_call - break; - - case BC_ITERN: - | // RA = base*8, (RB = (nresults+1)*8, RC = (nargs+1)*8 (2+1)*8) - |.if JIT - | // NYI on big-endian - |.endif - |->vm_IITERN: - | add RA, BASE, RA - | lwz TAB:RB, -12(RA) - | lwz RC, -4(RA) // Get index from control var. - | lwz TMP0, TAB:RB->asize - | lwz TMP1, TAB:RB->array - | addi PC, PC, 4 - |1: // Traverse array part. - | cmplw RC, TMP0 - | slwi TMP3, RC, 3 - | bge >5 // Index points after array part? - | lwzx TMP2, TMP1, TMP3 - |.if FPU - | lfdx f0, TMP1, TMP3 - |.else - | lwzux CARG1, TMP3, TMP1 - | lwz CARG2, 4(TMP3) - |.endif - | checknil TMP2 - | lwz INS, -4(PC) - | beq >4 - |.if DUALNUM - | stw RC, 4(RA) - | stw TISNUM, 0(RA) - |.else - | tonum_u f1, RC - |.endif - | addi RC, RC, 1 - | addis TMP3, PC, -(BCBIAS_J*4 >> 16) - |.if FPU - | stfd f0, 8(RA) - |.else - | stw CARG1, 8(RA) - | stw CARG2, 12(RA) - |.endif - | decode_RD4 TMP1, INS - | stw RC, -4(RA) // Update control var. - | add PC, TMP1, TMP3 - |.if not DUALNUM - | stfd f1, 0(RA) - |.endif - |3: - | ins_next - | - |4: // Skip holes in array part. - | addi RC, RC, 1 - | b <1 - | - |5: // Traverse hash part. - | lwz TMP1, TAB:RB->hmask - | sub RC, RC, TMP0 - | lwz TMP2, TAB:RB->node - |6: - | cmplw RC, TMP1 // End of iteration? Branch to ITERL+1. - | slwi TMP3, RC, 5 - | bgty <3 - | slwi RB, RC, 3 - | sub TMP3, TMP3, RB - | lwzx RB, TMP2, TMP3 - |.if FPU - | lfdx f0, TMP2, TMP3 - |.else - | add CARG3, TMP2, TMP3 - | lwz CARG1, 0(CARG3) - | lwz CARG2, 4(CARG3) - |.endif - | add NODE:TMP3, TMP2, TMP3 - | checknil RB - | lwz INS, -4(PC) - | beq >7 - |.if FPU - | lfd f1, NODE:TMP3->key - |.else - | lwz CARG3, NODE:TMP3->key.u32.hi - | lwz CARG4, NODE:TMP3->key.u32.lo - |.endif - | addis TMP2, PC, -(BCBIAS_J*4 >> 16) - |.if FPU - | stfd f0, 8(RA) - |.else - | stw CARG1, 8(RA) - | stw CARG2, 12(RA) - |.endif - | add RC, RC, TMP0 - | decode_RD4 TMP1, INS - |.if FPU - | stfd f1, 0(RA) - |.else - | stw CARG3, 0(RA) - | stw CARG4, 4(RA) - |.endif - | addi RC, RC, 1 - | add PC, TMP1, TMP2 - | stw RC, -4(RA) // Update control var. - | b <3 - | - |7: // Skip holes in hash part. - | addi RC, RC, 1 - | b <6 - break; - - case BC_ISNEXT: - | // RA = base*8, RD = target (points to ITERN) - | add RA, BASE, RA - | lwz TMP0, -24(RA) - | lwz CFUNC:TMP1, -20(RA) - | lwz TMP2, -16(RA) - | lwz TMP3, -8(RA) - | cmpwi cr0, TMP2, LJ_TTAB - | cmpwi cr1, TMP0, LJ_TFUNC - | cmpwi cr6, TMP3, LJ_TNIL - | bne cr1, >5 - | lbz TMP1, CFUNC:TMP1->ffid - | crand 4*cr0+eq, 4*cr0+eq, 4*cr6+eq - | cmpwi cr7, TMP1, FF_next_N - | srwi TMP0, RD, 1 - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq - | add TMP3, PC, TMP0 - | bne cr0, >5 - | lus TMP1, (LJ_KEYINDEX >> 16) - | ori TMP1, TMP1, (LJ_KEYINDEX & 0xffff) - | stw ZERO, -4(RA) // Initialize control var. - | stw TMP1, -8(RA) - | addis PC, TMP3, -(BCBIAS_J*4 >> 16) - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | li TMP0, BC_JMP - | li TMP1, BC_ITERC - | stb TMP0, -1(PC) - | addis PC, TMP3, -(BCBIAS_J*4 >> 16) - | // NYI on big-endian: unpatch JLOOP. - | stb TMP1, 3(PC) - | b <1 - break; - - case BC_VARG: - | // RA = base*8, RB = (nresults+1)*8, RC = numparams*8 - | lwz TMP0, FRAME_PC(BASE) - | add RC, BASE, RC - | add RA, BASE, RA - | addi RC, RC, FRAME_VARG - | add TMP2, RA, RB - | subi TMP3, BASE, 8 // TMP3 = vtop - | sub RC, RC, TMP0 // RC = vbase - | // Note: RC may now be even _above_ BASE if nargs was < numparams. - | cmplwi cr1, RB, 0 - |.if PPE - | sub TMP1, TMP3, RC - | cmpwi TMP1, 0 - |.else - | sub. TMP1, TMP3, RC - |.endif - | beq cr1, >5 // Copy all varargs? - | subi TMP2, TMP2, 16 - | ble >2 // No vararg slots? - |1: // Copy vararg slots to destination slots. - |.if FPU - | lfd f0, 0(RC) - |.else - | lwz CARG1, 0(RC) - | lwz CARG2, 4(RC) - |.endif - | addi RC, RC, 8 - |.if FPU - | stfd f0, 0(RA) - |.else - | stw CARG1, 0(RA) - | stw CARG2, 4(RA) - |.endif - | cmplw RA, TMP2 - | cmplw cr1, RC, TMP3 - | bge >3 // All destination slots filled? - | addi RA, RA, 8 - | blt cr1, <1 // More vararg slots? - |2: // Fill up remainder with nil. - | stw TISNIL, 0(RA) - | cmplw RA, TMP2 - | addi RA, RA, 8 - | blt <2 - |3: - | ins_next - | - |5: // Copy all varargs. - | lwz TMP0, L->maxstack - | li MULTRES, 8 // MULTRES = (0+1)*8 - | bley <3 // No vararg slots? - | add TMP2, RA, TMP1 - | cmplw TMP2, TMP0 - | addi MULTRES, TMP1, 8 - | bgt >7 - |6: - |.if FPU - | lfd f0, 0(RC) - |.else - | lwz CARG1, 0(RC) - | lwz CARG2, 4(RC) - |.endif - | addi RC, RC, 8 - |.if FPU - | stfd f0, 0(RA) - |.else - | stw CARG1, 0(RA) - | stw CARG2, 4(RA) - |.endif - | cmplw RC, TMP3 - | addi RA, RA, 8 - | blt <6 // More vararg slots? - | b <3 - | - |7: // Grow stack for varargs. - | mr CARG1, L - | stp RA, L->top - | sub SAVE0, RC, BASE // Need delta, because BASE may change. - | stp BASE, L->base - | sub RA, RA, BASE - | stw PC, SAVE_PC - | srwi CARG2, TMP1, 3 - | bl extern lj_state_growstack // (lua_State *L, int n) - | lp BASE, L->base - | add RA, BASE, RA - | add RC, BASE, SAVE0 - | subi TMP3, BASE, 8 - | b <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | // RA = results*8, RD = extra_nresults*8 - | add RD, RD, MULTRES // MULTRES >= 8, so RD >= 8. - | // Fall through. Assumes BC_RET follows. - break; - - case BC_RET: - | // RA = results*8, RD = (nresults+1)*8 - | lwz PC, FRAME_PC(BASE) - | add RA, BASE, RA - | mr MULTRES, RD - |1: - | andix. TMP0, PC, FRAME_TYPE - | xori TMP1, PC, FRAME_VARG - | bne ->BC_RETV_Z - | - |->BC_RET_Z: - | // BASE = base, RA = resultptr, RD = (nresults+1)*8, PC = return - | lwz INS, -4(PC) - | cmpwi RD, 8 - | subi TMP2, BASE, 8 - | subi RC, RD, 8 - | decode_RB8 RB, INS - | beq >3 - | li TMP1, 0 - |2: - | addi TMP3, TMP1, 8 - |.if FPU - | lfdx f0, RA, TMP1 - |.else - | add CARG3, RA, TMP1 - | lwz CARG1, 0(CARG3) - | lwz CARG2, 4(CARG3) - |.endif - | cmpw TMP3, RC - |.if FPU - | stfdx f0, TMP2, TMP1 - |.else - | add CARG3, TMP2, TMP1 - | stw CARG1, 0(CARG3) - | stw CARG2, 4(CARG3) - |.endif - | beq >3 - | addi TMP1, TMP3, 8 - |.if FPU - | lfdx f1, RA, TMP3 - |.else - | add CARG3, RA, TMP3 - | lwz CARG1, 0(CARG3) - | lwz CARG2, 4(CARG3) - |.endif - | cmpw TMP1, RC - |.if FPU - | stfdx f1, TMP2, TMP3 - |.else - | add CARG3, TMP2, TMP3 - | stw CARG1, 0(CARG3) - | stw CARG2, 4(CARG3) - |.endif - | bne <2 - |3: - |5: - | cmplw RB, RD - | decode_RA8 RA, INS - | bgt >6 - | sub BASE, TMP2, RA - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | stwx TISNIL, TMP2, TMP1 - | b <5 - | - |->BC_RETV_Z: // Non-standard return case. - | andix. TMP2, TMP1, FRAME_TYPEP - | bne ->vm_return - | // Return from vararg function: relocate BASE down. - | sub BASE, BASE, TMP1 - | lwz PC, FRAME_PC(BASE) - | b <1 - break; - - case BC_RET0: case BC_RET1: - | // RA = results*8, RD = (nresults+1)*8 - | lwz PC, FRAME_PC(BASE) - | add RA, BASE, RA - | mr MULTRES, RD - | andix. TMP0, PC, FRAME_TYPE - | xori TMP1, PC, FRAME_VARG - | bney ->BC_RETV_Z - | - | lwz INS, -4(PC) - | subi TMP2, BASE, 8 - | decode_RB8 RB, INS - if (op == BC_RET1) { - |.if FPU - | lfd f0, 0(RA) - | stfd f0, 0(TMP2) - |.else - | lwz CARG1, 0(RA) - | lwz CARG2, 4(RA) - | stw CARG1, 0(TMP2) - | stw CARG2, 4(TMP2) - |.endif - } - |5: - | cmplw RB, RD - | decode_RA8 RA, INS - | bgt >6 - | sub BASE, TMP2, RA - | lwz LFUNC:TMP1, FRAME_FUNC(BASE) - | ins_next1 - | lwz TMP1, LFUNC:TMP1->pc - | lwz KBASE, PC2PROTO(k)(TMP1) - | ins_next2 - | - |6: // Fill up results with nil. - | subi TMP1, RD, 8 - | addi RD, RD, 8 - | stwx TISNIL, TMP2, TMP1 - | b <5 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - case BC_FORL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IFORL follows. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - | // RA = base*8, RD = target (after end of loop or start of loop) - vk = (op == BC_IFORL || op == BC_JFORL); - |.if DUALNUM - | // Integer loop. - | lwzux TMP1, RA, BASE - | lwz CARG1, FORL_IDX*8+4(RA) - | cmplw cr0, TMP1, TISNUM - if (vk) { - | lwz CARG3, FORL_STEP*8+4(RA) - | bne >9 - |.if GPR64 - | // Need to check overflow for (a<<32) + (b<<32). - | rldicr TMP0, CARG1, 32, 31 - | rldicr TMP2, CARG3, 32, 31 - | add CARG1, CARG1, CARG3 - | addo. TMP0, TMP0, TMP2 - |.else - | addo. CARG1, CARG1, CARG3 - |.endif - | cmpwi cr6, CARG3, 0 - | lwz CARG2, FORL_STOP*8+4(RA) - | bso >6 - |4: - | stw CARG1, FORL_IDX*8+4(RA) - } else { - | lwz SAVE0, FORL_STEP*8(RA) - | lwz CARG3, FORL_STEP*8+4(RA) - | lwz TMP2, FORL_STOP*8(RA) - | lwz CARG2, FORL_STOP*8+4(RA) - | cmplw cr7, SAVE0, TISNUM - | cmplw cr1, TMP2, TISNUM - | crand 4*cr0+eq, 4*cr0+eq, 4*cr7+eq - | crand 4*cr0+eq, 4*cr0+eq, 4*cr1+eq - | cmpwi cr6, CARG3, 0 - | bne >9 - } - | blt cr6, >5 - | cmpw CARG1, CARG2 - |1: - | stw TISNUM, FORL_EXT*8(RA) - if (op != BC_JFORL) { - | srwi RD, RD, 1 - } - | stw CARG1, FORL_EXT*8+4(RA) - if (op != BC_JFORL) { - | add RD, PC, RD - } - if (op == BC_FORI) { - | bgt >3 // See FP loop below. - } else if (op == BC_JFORI) { - | addis PC, RD, -(BCBIAS_J*4 >> 16) - | bley >7 - } else if (op == BC_IFORL) { - | bgt >2 - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else { - | bley =>BC_JLOOP - } - |2: - | ins_next - |5: // Invert check for negative step. - | cmpw CARG2, CARG1 - | b <1 - if (vk) { - |6: // Potential overflow. - | checkov TMP0, <4 // Ignore unrelated overflow. - | b <2 - } - |.endif - if (vk) { - |.if DUALNUM - |9: // FP loop. - |.if FPU - | lfd f1, FORL_IDX*8(RA) - |.else - | lwz CARG1, FORL_IDX*8(RA) - | lwz CARG2, FORL_IDX*8+4(RA) - |.endif - |.else - | lfdux f1, RA, BASE - |.endif - |.if FPU - | lfd f3, FORL_STEP*8(RA) - | lfd f2, FORL_STOP*8(RA) - | fadd f1, f1, f3 - | stfd f1, FORL_IDX*8(RA) - |.else - | lwz CARG3, FORL_STEP*8(RA) - | lwz CARG4, FORL_STEP*8+4(RA) - | mr SAVE1, RD - | blex __adddf3 - | mr RD, SAVE1 - | stw CRET1, FORL_IDX*8(RA) - | stw CRET2, FORL_IDX*8+4(RA) - | lwz CARG3, FORL_STOP*8(RA) - | lwz CARG4, FORL_STOP*8+4(RA) - |.endif - | lwz SAVE0, FORL_STEP*8(RA) - } else { - |.if DUALNUM - |9: // FP loop. - |.else - | lwzux TMP1, RA, BASE - | lwz SAVE0, FORL_STEP*8(RA) - | lwz TMP2, FORL_STOP*8(RA) - | cmplw cr0, TMP1, TISNUM - | cmplw cr7, SAVE0, TISNUM - | cmplw cr1, TMP2, TISNUM - |.endif - |.if FPU - | lfd f1, FORL_IDX*8(RA) - |.else - | lwz CARG1, FORL_IDX*8(RA) - | lwz CARG2, FORL_IDX*8+4(RA) - |.endif - | crand 4*cr0+lt, 4*cr0+lt, 4*cr7+lt - | crand 4*cr0+lt, 4*cr0+lt, 4*cr1+lt - |.if FPU - | lfd f2, FORL_STOP*8(RA) - |.else - | lwz CARG3, FORL_STOP*8(RA) - | lwz CARG4, FORL_STOP*8+4(RA) - |.endif - | bge ->vmeta_for - } - | cmpwi cr6, SAVE0, 0 - if (op != BC_JFORL) { - | srwi RD, RD, 1 - } - |.if FPU - | stfd f1, FORL_EXT*8(RA) - |.else - | stw CARG1, FORL_EXT*8(RA) - | stw CARG2, FORL_EXT*8+4(RA) - |.endif - if (op != BC_JFORL) { - | add RD, PC, RD - } - |.if FPU - | fcmpu cr0, f1, f2 - |.else - | mr SAVE1, RD - | blex __ledf2 - | cmpwi CRET1, 0 - | mr RD, SAVE1 - |.endif - if (op == BC_JFORI) { - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } - | blt cr6, >5 - if (op == BC_FORI) { - | bgt >3 - } else if (op == BC_IFORL) { - |.if DUALNUM - | bgty <2 - |.else - | bgt >2 - |.endif - |1: - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else if (op == BC_JFORI) { - | bley >7 - } else { - | bley =>BC_JLOOP - } - |.if DUALNUM - | b <2 - |.else - |2: - | ins_next - |.endif - |5: // Negative step. - if (op == BC_FORI) { - | bge <2 - |3: // Used by integer loop, too. - | addis PC, RD, -(BCBIAS_J*4 >> 16) - } else if (op == BC_IFORL) { - | bgey <1 - } else if (op == BC_JFORI) { - | bgey >7 - } else { - | bgey =>BC_JLOOP - } - | b <2 - if (op == BC_JFORI) { - |7: - | lwz INS, -4(PC) - | decode_RD8 RD, INS - | b =>BC_JLOOP - } - break; - - case BC_ITERL: - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_IITERL follows. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | // RA = base*8, RD = target - | lwzux TMP1, RA, BASE - | lwz TMP2, 4(RA) - | checknil TMP1; beq >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | stw TMP1, -8(RA) - | stw TMP2, -4(RA) - | b =>BC_JLOOP - } else { - | branch_RD // Otherwise save control var + branch. - | stw TMP1, -8(RA) - | stw TMP2, -4(RA) - } - |1: - | ins_next - break; - - case BC_LOOP: - | // RA = base*8, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop - |.endif - | // Fall through. Assumes BC_ILOOP follows. - break; - - case BC_ILOOP: - | // RA = base*8, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | // RA = base*8 (ignored), RD = traceno*8 - | lwz TMP1, DISPATCH_J(trace)(DISPATCH) - | srwi RD, RD, 1 - | // Traces on PPC don't store the trace number, so use 0. - | stw ZERO, DISPATCH_GL(vmstate)(DISPATCH) - | lwzx TRACE:TMP2, TMP1, RD - | clrso TMP1 - | lp TMP2, TRACE:TMP2->mcode - | stw BASE, DISPATCH_GL(jit_base)(DISPATCH) - | mtctr TMP2 - | addi JGL, DISPATCH, GG_DISP2G+32768 - | stw L, DISPATCH_GL(tmpbuf.L)(DISPATCH) - | bctr - |.endif - break; - - case BC_JMP: - | // RA = base*8 (only used by trace recorder), RD = target - | branch_RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - case BC_FUNCF: - |.if JIT - | hotcall - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lwz TMP2, L->maxstack - | lbz TMP1, -4+PC2PROTO(numparams)(PC) - | lwz KBASE, -4+PC2PROTO(k)(PC) - | cmplw RA, TMP2 - | slwi TMP1, TMP1, 3 - | bgt ->vm_growstack_l - if (op != BC_JFUNCF) { - | ins_next1 - } - |2: - | cmplw NARGS8:RC, TMP1 // Check for missing parameters. - | blt >3 - if (op == BC_JFUNCF) { - | decode_RD8 RD, INS - | b =>BC_JLOOP - } else { - | ins_next2 - } - | - |3: // Clear missing parameters. - | stwx TISNIL, BASE, NARGS8:RC - | addi NARGS8:RC, NARGS8:RC, 8 - | b <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | NYI // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | // BASE = new base, RA = BASE+framesize*8, RB = LFUNC, RC = nargs*8 - | lwz TMP2, L->maxstack - | add TMP1, BASE, RC - | add TMP0, RA, RC - | stw LFUNC:RB, 4(TMP1) // Store copy of LFUNC. - | addi TMP3, RC, 8+FRAME_VARG - | lwz KBASE, -4+PC2PROTO(k)(PC) - | cmplw TMP0, TMP2 - | stw TMP3, 0(TMP1) // Store delta + FRAME_VARG. - | bge ->vm_growstack_l - | lbz TMP2, -4+PC2PROTO(numparams)(PC) - | mr RA, BASE - | mr RC, TMP1 - | ins_next1 - | cmpwi TMP2, 0 - | addi BASE, TMP1, 8 - | beq >3 - |1: - | cmplw RA, RC // Less args than parameters? - | lwz TMP0, 0(RA) - | lwz TMP3, 4(RA) - | bge >4 - | stw TISNIL, 0(RA) // Clear old fixarg slot (help the GC). - | addi RA, RA, 8 - |2: - | addic. TMP2, TMP2, -1 - | stw TMP0, 8(TMP1) - | stw TMP3, 12(TMP1) - | addi TMP1, TMP1, 8 - | bne <1 - |3: - | ins_next2 - | - |4: // Clear missing parameters. - | li TMP0, LJ_TNIL - | b <2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | // BASE = new base, RA = BASE+framesize*8, RB = CFUNC, RC = nargs*8 - if (op == BC_FUNCC) { - | lp RD, CFUNC:RB->f - } else { - | lp RD, DISPATCH_GL(wrapf)(DISPATCH) - } - | add TMP1, RA, NARGS8:RC - | lwz TMP2, L->maxstack - | .toc lp TMP3, 0(RD) - | add RC, BASE, NARGS8:RC - | stp BASE, L->base - | cmplw TMP1, TMP2 - | stp RC, L->top - | li_vmstate C - |.if TOC - | mtctr TMP3 - |.else - | mtctr RD - |.endif - if (op == BC_FUNCCW) { - | lp CARG2, CFUNC:RB->f - } - | mr CARG1, L - | bgt ->vm_growstack_c // Need to grow stack. - | .toc lp TOCREG, TOC_OFS(RD) - | .tocenv lp ENVREG, ENV_OFS(RD) - | st_vmstate - | bctrl // (lua_State *L [, lua_CFunction f]) - | // Returns nresults. - | lp BASE, L->base - | .toc ld TOCREG, SAVE_TOC - | slwi RD, CRET1, 3 - | lp TMP1, L->top - | li_vmstate INTERP - | lwz PC, FRAME_PC(BASE) // Fetch PC of caller. - | stw L, DISPATCH_GL(cur_L)(DISPATCH) - | sub RA, TMP1, RD // RA = L->top - nresults*8 - | st_vmstate - | b ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - - dasm_growpc(Dst, BC__MAX); - - build_subroutines(ctx); - - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - int i; - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n", - fcofs, CFRAME_SIZE); - for (i = 14; i <= 31; i++) - fprintf(ctx->fp, - "\t.byte %d\n\t.uleb128 %d\n" - "\t.byte %d\n\t.uleb128 %d\n", - 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i)); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE0:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" -#if LJ_TARGET_PS3 - "\t.long .lj_vm_ffi_call\n" -#else - "\t.long lj_vm_ffi_call\n" -#endif - "\t.long %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x8e\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0xe\n" - "\t.align 2\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.long .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.long .LASFDE2-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x5\n\t.uleb128 70\n\t.uleb128 55\n", - fcofs, CFRAME_SIZE); - for (i = 14; i <= 31; i++) - fprintf(ctx->fp, - "\t.byte %d\n\t.uleb128 %d\n" - "\t.byte %d\n\t.uleb128 %d\n", - 0x80+i, 37+(31-i), 0x80+32+i, 2+2*(31-i)); - fprintf(ctx->fp, - "\t.align 2\n" - ".LEFDE2:\n\n"); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.long .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -4\n" - "\t.byte 65\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 1\n\t.uleb128 0\n" - "\t.align 2\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.long .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.long .LASFDE3-.Lframe2\n" - "\t.long lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0x11\n\t.uleb128 65\n\t.sleb128 -1\n" - "\t.byte 0x8e\n\t.uleb128 2\n" - "\t.byte 0xd\n\t.uleb128 0xe\n" - "\t.align 2\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif -#endif - break; - default: - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x64.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x64.dasc deleted file mode 100644 index 03d9655..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x64.dasc +++ /dev/null @@ -1,4935 +0,0 @@ -|// Low-level VM code for x64 CPUs in LJ_GC64 mode. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -| -|.arch x64 -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|//----------------------------------------------------------------------- -| -|.if WIN -|.define X64WIN, 1 // Windows/x64 calling conventions. -|.endif -| -|// Fixed register assignments for the interpreter. -|// This is very fragile and has many dependencies. Caveat emptor. -|.define BASE, rdx // Not C callee-save, refetched anyway. -|.if X64WIN -|.define KBASE, rdi // Must be C callee-save. -|.define PC, rsi // Must be C callee-save. -|.define DISPATCH, rbx // Must be C callee-save. -|.define KBASEd, edi -|.define PCd, esi -|.define DISPATCHd, ebx -|.else -|.define KBASE, r15 // Must be C callee-save. -|.define PC, rbx // Must be C callee-save. -|.define DISPATCH, r14 // Must be C callee-save. -|.define KBASEd, r15d -|.define PCd, ebx -|.define DISPATCHd, r14d -|.endif -| -|.define RA, rcx -|.define RAd, ecx -|.define RAH, ch -|.define RAL, cl -|.define RB, rbp // Must be rbp (C callee-save). -|.define RBd, ebp -|.define RC, rax // Must be rax. -|.define RCd, eax -|.define RCW, ax -|.define RCH, ah -|.define RCL, al -|.define OP, RBd -|.define RD, RC -|.define RDd, RCd -|.define RDW, RCW -|.define RDL, RCL -|.define TMPR, r10 -|.define TMPRd, r10d -|.define ITYPE, r11 -|.define ITYPEd, r11d -| -|.if X64WIN -|.define CARG1, rcx // x64/WIN64 C call arguments. -|.define CARG2, rdx -|.define CARG3, r8 -|.define CARG4, r9 -|.define CARG1d, ecx -|.define CARG2d, edx -|.define CARG3d, r8d -|.define CARG4d, r9d -|.else -|.define CARG1, rdi // x64/POSIX C call arguments. -|.define CARG2, rsi -|.define CARG3, rdx -|.define CARG4, rcx -|.define CARG5, r8 -|.define CARG6, r9 -|.define CARG1d, edi -|.define CARG2d, esi -|.define CARG3d, edx -|.define CARG4d, ecx -|.define CARG5d, r8d -|.define CARG6d, r9d -|.endif -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|//----------------------------------------------------------------------- -|.if X64WIN // x64/Windows stack layout -| -|.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--). -|.macro saveregs_ -| push rdi; push rsi; push rbx -| sub rsp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push rbp; saveregs_ -|.endmacro -|.macro restoreregs -| add rsp, CFRAME_SPACE -| pop rbx; pop rsi; pop rdi; pop rbp -|.endmacro -| -|.define SAVE_CFRAME, aword [rsp+aword*13] -|.define SAVE_PC, aword [rsp+aword*12] -|.define SAVE_L, aword [rsp+aword*11] -|.define SAVE_ERRF, dword [rsp+dword*21] -|.define SAVE_NRES, dword [rsp+dword*20] -|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter -|.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*8] -|.define SAVE_R3, aword [rsp+aword*7] -|.define SAVE_R2, aword [rsp+aword*6] -|.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves. -|.define ARG5, aword [rsp+aword*4] -|.define CSAVE_4, aword [rsp+aword*3] -|.define CSAVE_3, aword [rsp+aword*2] -|.define CSAVE_2, aword [rsp+aword*1] -|.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter. -|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee -| -|.define ARG5d, dword [rsp+dword*8] -|.define TMP1, ARG5 // TMP1 overlaps ARG5 -|.define TMP1d, ARG5d -|.define TMP1hi, dword [rsp+dword*9] -|.define MULTRES, TMP1d // MULTRES overlaps TMP1d. -| -|//----------------------------------------------------------------------- -|.else // x64/POSIX stack layout -| -|.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--). -|.macro saveregs_ -| push rbx; push r15; push r14 -|.if NO_UNWIND -| push r13; push r12 -|.endif -| sub rsp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push rbp; saveregs_ -|.endmacro -|.macro restoreregs -| add rsp, CFRAME_SPACE -|.if NO_UNWIND -| pop r12; pop r13 -|.endif -| pop r14; pop r15; pop rbx; pop rbp -|.endmacro -| -|//----- 16 byte aligned, -|.if NO_UNWIND -|.define SAVE_RET, aword [rsp+aword*11] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*10] -|.define SAVE_R3, aword [rsp+aword*9] -|.define SAVE_R2, aword [rsp+aword*8] -|.define SAVE_R1, aword [rsp+aword*7] -|.define SAVE_RU2, aword [rsp+aword*6] -|.define SAVE_RU1, aword [rsp+aword*5] //<-- rsp after register saves. -|.else -|.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*8] -|.define SAVE_R3, aword [rsp+aword*7] -|.define SAVE_R2, aword [rsp+aword*6] -|.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves. -|.endif -|.define SAVE_CFRAME, aword [rsp+aword*4] -|.define SAVE_PC, aword [rsp+aword*3] -|.define SAVE_L, aword [rsp+aword*2] -|.define SAVE_ERRF, dword [rsp+dword*3] -|.define SAVE_NRES, dword [rsp+dword*2] -|.define TMP1, aword [rsp] //<-- rsp while in interpreter. -|//----- 16 byte aligned -| -|.define TMP1d, dword [rsp] -|.define TMP1hi, dword [rsp+dword*1] -|.define MULTRES, TMP1d // MULTRES overlaps TMP1d. -| -|.endif -| -|//----------------------------------------------------------------------- -| -|// Instruction headers. -|.macro ins_A; .endmacro -|.macro ins_AD; .endmacro -|.macro ins_AJ; .endmacro -|.macro ins_ABC; movzx RBd, RCH; movzx RCd, RCL; .endmacro -|.macro ins_AB_; movzx RBd, RCH; .endmacro -|.macro ins_A_C; movzx RCd, RCL; .endmacro -|.macro ins_AND; not RD; .endmacro -| -|// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster). -|.macro ins_NEXT -| mov RCd, [PC] -| movzx RAd, RCH -| movzx OP, RCL -| add PC, 4 -| shr RCd, 16 -| jmp aword [DISPATCH+OP*8] -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| // Around 10%-30% slower on Core2, a lot more slower on P4. -| .macro ins_next -| jmp ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-8] = PC -| mov PC, LFUNC:RB->pc -| mov RAd, [PC] -| movzx OP, RAL -| movzx RAd, RAH -| add PC, 4 -| jmp aword [DISPATCH+OP*8] -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC, RD = nargs+1 -| mov [BASE-8], PC -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to clear or set tags. -|.macro cleartp, reg; shl reg, 17; shr reg, 17; .endmacro -|.macro settp, reg, tp -| mov64 ITYPE, ((uint64_t)tp<<47) -| or reg, ITYPE -|.endmacro -|.macro settp, dst, reg, tp -| mov64 dst, ((uint64_t)tp<<47) -| or dst, reg -|.endmacro -|.macro setint, reg -| settp reg, LJ_TISNUM -|.endmacro -|.macro setint, dst, reg -| settp dst, reg, LJ_TISNUM -|.endmacro -| -|// Macros to test operand types. -|.macro checktp_nc, reg, tp, target -| mov ITYPE, reg -| sar ITYPE, 47 -| cmp ITYPEd, tp -| jne target -|.endmacro -|.macro checktp, reg, tp, target -| mov ITYPE, reg -| cleartp reg -| sar ITYPE, 47 -| cmp ITYPEd, tp -| jne target -|.endmacro -|.macro checktptp, src, tp, target -| mov ITYPE, src -| sar ITYPE, 47 -| cmp ITYPEd, tp -| jne target -|.endmacro -|.macro checkstr, reg, target; checktp reg, LJ_TSTR, target; .endmacro -|.macro checktab, reg, target; checktp reg, LJ_TTAB, target; .endmacro -|.macro checkfunc, reg, target; checktp reg, LJ_TFUNC, target; .endmacro -| -|.macro checknumx, reg, target, jump -| mov ITYPE, reg -| sar ITYPE, 47 -| cmp ITYPEd, LJ_TISNUM -| jump target -|.endmacro -|.macro checkint, reg, target; checknumx reg, target, jne; .endmacro -|.macro checkinttp, src, target; checknumx src, target, jne; .endmacro -|.macro checknum, reg, target; checknumx reg, target, jae; .endmacro -|.macro checknumtp, src, target; checknumx src, target, jae; .endmacro -|.macro checknumber, src, target; checknumx src, target, ja; .endmacro -| -|.macro mov_false, reg; mov64 reg, (int64_t)~((uint64_t)1<<47); .endmacro -|.macro mov_true, reg; mov64 reg, (int64_t)~((uint64_t)2<<47); .endmacro -| -|// These operands must be used with movzx. -|.define PC_OP, byte [PC-4] -|.define PC_RA, byte [PC-3] -|.define PC_RB, byte [PC-1] -|.define PC_RC, byte [PC-2] -|.define PC_RD, word [PC-2] -| -|.macro branchPC, reg -| lea PC, [PC+reg*4-BCBIAS_J*4] -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|// Decrement hashed hotcount and trigger trace recorder if zero. -|.macro hotloop, reg -| mov reg, PCd -| shr reg, 1 -| and reg, HOTCOUNT_PCMASK -| sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP -| jb ->vm_hotloop -|.endmacro -| -|.macro hotcall, reg -| mov reg, PCd -| shr reg, 1 -| and reg, HOTCOUNT_PCMASK -| sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL -| jb ->vm_hotcall -|.endmacro -| -|// Set current VM state. -|.macro set_vmstate, st -| mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st -|.endmacro -| -|.macro fpop1; fstp st1; .endmacro -| -|// Synthesize SSE FP constants. -|.macro sseconst_abs, reg, tmp // Synthesize abs mask. -| mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp -|.endmacro -| -|.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const. -| mov64 tmp, U64x(val,00000000); movd reg, tmp -|.endmacro -| -|.macro sseconst_sign, reg, tmp // Synthesize sign mask. -| sseconst_hi reg, tmp, 80000000 -|.endmacro -|.macro sseconst_1, reg, tmp // Synthesize 1.0. -| sseconst_hi reg, tmp, 3ff00000 -|.endmacro -|.macro sseconst_2p52, reg, tmp // Synthesize 2^52. -| sseconst_hi reg, tmp, 43300000 -|.endmacro -|.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51. -| sseconst_hi reg, tmp, 43380000 -|.endmacro -| -|// Move table write barrier back. Overwrites reg. -|.macro barrierback, tab, reg -| and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab) -| mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)] -| mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab -| mov tab->gclist, reg -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | test PCd, FRAME_P - | jz ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | and PC, -8 - | sub BASE, PC // Restore caller base. - | lea RA, [RA+PC-8] // Rebase RA and prepend one result. - | mov PC, [BASE-8] // Fetch PC of previous frame. - | // Prepending may overwrite the pcall frame, so do it at the end. - | mov_true ITYPE - | mov aword [BASE+RA], ITYPE // Prepend true to results. - | - |->vm_returnc: - | add RDd, 1 // RD = nresults+1 - | jz ->vm_unwind_yield - | mov MULTRES, RDd - | test PC, FRAME_TYPE - | jz ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return - | xor PC, FRAME_C - | test PCd, FRAME_TYPE - | jnz ->vm_returnp - | - | // Return to C. - | set_vmstate C - | and PC, -8 - | sub PC, BASE - | neg PC // Previous base = BASE - delta. - | - | sub RDd, 1 - | jz >2 - |1: // Move results down. - | mov RB, [BASE+RA] - | mov [BASE-16], RB - | add BASE, 8 - | sub RDd, 1 - | jnz <1 - |2: - | mov L:RB, SAVE_L - | mov L:RB->base, PC - |3: - | mov RDd, MULTRES - | mov RAd, SAVE_NRES // RA = wanted nresults+1 - |4: - | cmp RAd, RDd - | jne >6 // More/less results wanted? - |5: - | sub BASE, 16 - | mov L:RB->top, BASE - | - |->vm_leave_cp: - | mov RA, SAVE_CFRAME // Restore previous C frame. - | mov L:RB->cframe, RA - | xor eax, eax // Ok return status for vm_pcall. - | - |->vm_leave_unw: - | restoreregs - | ret - | - |6: - | jb >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | cmp BASE, L:RB->maxstack - | ja >8 - | mov aword [BASE-16], LJ_TNIL - | add BASE, 8 - | add RDd, 1 - | jmp <4 - | - |7: // Less results wanted. - | test RAd, RAd - | jz <5 // But check for LUA_MULTRET+1. - | sub RA, RD // Negative result! - | lea BASE, [BASE+RA*8] // Correct top. - | jmp <5 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | mov L:RB->top, BASE // Save current top held in BASE (yes). - | mov MULTRES, RDd // Need to fill only remainder with nil. - | mov CARG2d, RAd - | mov CARG1, L:RB - | call extern lj_state_growstack // (lua_State *L, int n) - | mov BASE, L:RB->top // Need the (realloced) L->top in BASE. - | jmp <3 - | - |->vm_unwind_yield: - | mov al, LUA_YIELD - | jmp ->vm_unwind_c_eh - | - |->vm_unwind_c: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - | mov eax, CARG2d // Error return status for vm_pcall. - | mov rsp, CARG1 - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | mov L:RB, SAVE_L - | mov GL:RB, L:RB->glref - | mov dword GL:RB->vmstate, ~LJ_VMST_C - | jmp ->vm_leave_unw - | - |->vm_unwind_rethrow: - |.if not X64WIN - | mov CARG1, SAVE_L - | mov CARG2d, eax - | restoreregs - | jmp extern lj_err_throw // (lua_State *L, int errcode) - |.endif - | - |->vm_unwind_ff: // Unwind C stack, return from ff pcall. - | // (void *cframe) - | and CARG1, CFRAME_RAWMASK - | mov rsp, CARG1 - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | mov L:RB, SAVE_L - | mov RDd, 1+1 // Really 1+2 results, incr. later. - | mov BASE, L:RB->base - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | mov PC, [BASE-8] // Fetch PC of previous frame. - | mov_false RA - | mov RB, [BASE] - | mov [BASE-16], RA // Prepend false to error message. - | mov [BASE-8], RB - | mov RA, -16 // Results start at BASE+RA = BASE-16. - | set_vmstate INTERP - | jmp ->vm_returnc // Increments RD/MULTRES and returns. - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | mov CARG2d, LUA_MINSTACK - | jmp >2 - | - |->vm_growstack_v: // Grow stack for vararg Lua function. - | sub RD, 16 // LJ_FR2 - | jmp >1 - | - |->vm_growstack_f: // Grow stack for fixarg Lua function. - | // BASE = new base, RD = nargs+1, RB = L, PC = first PC - | lea RD, [BASE+NARGS:RD*8-8] - |1: - | movzx RAd, byte [PC-4+PC2PROTO(framesize)] - | add PC, 4 // Must point after first instruction. - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov SAVE_PC, PC - | mov CARG2, RA - |2: - | // RB = L, L->base = new base, L->top = top - | mov CARG1, L:RB - | call extern lj_state_growstack // (lua_State *L, int n) - | mov BASE, L:RB->base - | mov RD, L:RB->top - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | sub RD, BASE - | shr RDd, 3 - | add NARGS:RDd, 1 - | // BASE = new base, RB = LFUNC, RD = nargs+1 - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - | mov L:RB, CARG1 // Caveat: CARG1 may be RA. - | mov SAVE_L, CARG1 - | mov RA, CARG2 - | mov PCd, FRAME_CP - | xor RDd, RDd - | lea KBASE, [esp+CFRAME_RESUME] - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | mov SAVE_PC, RD // Any value outside of bytecode is ok. - | mov SAVE_CFRAME, RD - | mov SAVE_NRES, RDd - | mov SAVE_ERRF, RDd - | mov L:RB->cframe, KBASE - | cmp byte L:RB->status, RDL - | je >2 // Initial resume (like a call). - | - | // Resume after yield (like a return). - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | mov byte L:RB->status, RDL - | mov BASE, L:RB->base - | mov RD, L:RB->top - | sub RD, RA - | shr RDd, 3 - | add RDd, 1 // RD = nresults+1 - | sub RA, BASE // RA = resultofs - | mov PC, [BASE-8] - | mov MULTRES, RDd - | test PCd, FRAME_TYPE - | jz ->BC_RET_Z - | jmp ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | mov PCd, FRAME_CP - | mov SAVE_ERRF, CARG4d - | jmp >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | mov PCd, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - | mov SAVE_NRES, CARG3d - | mov L:RB, CARG1 // Caveat: CARG1 may be RA. - | mov SAVE_L, CARG1 - | mov RA, CARG2 - | - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | mov KBASE, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASE - | mov SAVE_PC, L:RB // Any value outside of bytecode is ok. - | add DISPATCH, GG_G2DISP - | mov L:RB->cframe, rsp - | - |2: // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype). - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | mov BASE, L:RB->base // BASE = old base (used in vmeta_call). - | add PC, RA - | sub PC, BASE // PC = frame delta + frame type - | - | mov RD, L:RB->top - | sub RD, RA - | shr NARGS:RDd, 3 - | add NARGS:RDd, 1 // RD = nargs+1 - | - |->vm_call_dispatch: - | mov LFUNC:RB, [RA-16] - | checkfunc LFUNC:RB, ->vmeta_call // Ensure KBASE defined and != BASE. - | - |->vm_call_dispatch_f: - | mov BASE, RA - | ins_call - | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - | mov L:RB, CARG1 // Caveat: CARG1 may be RA. - | mov SAVE_L, CARG1 - | mov SAVE_PC, L:RB // Any value outside of bytecode is ok. - | - | mov KBASE, L:RB->stack // Compute -savestack(L, L->top). - | sub KBASE, L:RB->top - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | mov SAVE_ERRF, 0 // No error function. - | mov SAVE_NRES, KBASEd // Neg. delta means cframe w/o frame. - | add DISPATCH, GG_G2DISP - | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe). - | - | mov KBASE, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASE - | mov L:RB->cframe, rsp - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | - | call CARG4 // (lua_State *L, lua_CFunction func, void *ud) - | // TValue * (new base) or NULL returned in eax (RC). - | test RC, RC - | jz ->vm_leave_cp // No base? Just remove C frame. - | mov RA, RC - | mov PCd, FRAME_CP - | jmp <2 // Else continue with the call. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES) - | add RA, BASE - | and PC, -8 - | mov RB, BASE - | sub BASE, PC // Restore caller BASE. - | mov aword [RA+RD*8-8], LJ_TNIL // Ensure one valid arg. - | mov RC, RA // ... in [RC] - | mov PC, [RB-24] // Restore PC from [cont|PC]. - | mov RA, qword [RB-32] // May be negative on WIN64 with debug. - |.if FFI - | cmp RA, 1 - | jbe >1 - |.endif - | mov LFUNC:KBASE, [BASE-16] - | cleartp LFUNC:KBASE - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | // BASE = base, RC = result, RB = meta base - | jmp RA // Jump to continuation. - | - |.if FFI - |1: - | je ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: Tail call from C function. - | sub RB, BASE - | shr RBd, 3 - | lea RDd, [RBd-3] - | jmp ->vm_call_tail - |.endif - | - |->cont_cat: // BASE = base, RC = result, RB = mbase - | movzx RAd, PC_RB - | sub RB, 32 - | lea RA, [BASE+RA*8] - | sub RA, RB - | je ->cont_ra - | neg RA - | shr RAd, 3 - |.if X64WIN - | mov CARG3d, RAd - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE - | mov RC, [RC] - | mov [RB], RC - | mov CARG2, RB - |.else - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE - | mov CARG3d, RAd - | mov RA, [RC] - | mov [RB], RA - | mov CARG2, RB - |.endif - | jmp ->BC_CAT_Z - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets: - | settp STR:RC, LJ_TSTR // STR:RC = GCstr * - | mov TMP1, STR:RC - | lea RC, TMP1 - | cmp PC_OP, BC_GGET - | jne >1 - | settp TAB:RA, TAB:RB, LJ_TTAB // TAB:RB = GCtab * - | lea RB, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv. - | mov [RB], TAB:RA - | jmp >2 - | - |->vmeta_tgetb: - | movzx RCd, PC_RC - |.if DUALNUM - | setint RC - | mov TMP1, RC - |.else - | cvtsi2sd xmm0, RCd - | movsd TMP1, xmm0 - |.endif - | lea RC, TMP1 - | jmp >1 - | - |->vmeta_tgetv: - | movzx RCd, PC_RC // Reload TValue *k from RC. - | lea RC, [BASE+RC*8] - |1: - | movzx RBd, PC_RB // Reload TValue *t from RB. - | lea RB, [BASE+RB*8] - |2: - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE // Caveat: CARG2/CARG3 may be BASE. - | mov CARG2, RB - | mov CARG3, RC - | mov L:RB, L:CARG1 - | mov SAVE_PC, PC - | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // TValue * (finished) or NULL (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz >3 - |->cont_ra: // BASE = base, RC = result - | movzx RAd, PC_RA - | mov RB, [RC] - | mov [BASE+RA*8], RB - | ins_next - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | mov RA, L:RB->top - | mov [RA-24], PC // [cont|PC] - | lea PC, [RA+FRAME_CONT] - | sub PC, BASE - | mov LFUNC:RB, [RA-16] // Guaranteed to be a function here. - | mov NARGS:RDd, 2+1 // 2 args for func(t, k). - | cleartp LFUNC:RB - | jmp ->vm_call_dispatch_f - | - |->vmeta_tgetr: - | mov CARG1, TAB:RB - | mov RB, BASE // Save BASE. - | mov CARG2d, RCd // Caveat: CARG2 == BASE - | call extern lj_tab_getinth // (GCtab *t, int32_t key) - | // cTValue * or NULL returned in eax (RC). - | movzx RAd, PC_RA - | mov BASE, RB // Restore BASE. - | test RC, RC - | jnz ->BC_TGETR_Z - | mov ITYPE, LJ_TNIL - | jmp ->BC_TGETR2_Z - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets: - | settp STR:RC, LJ_TSTR // STR:RC = GCstr * - | mov TMP1, STR:RC - | lea RC, TMP1 - | cmp PC_OP, BC_GSET - | jne >1 - | settp TAB:RA, TAB:RB, LJ_TTAB // TAB:RB = GCtab * - | lea RB, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv. - | mov [RB], TAB:RA - | jmp >2 - | - |->vmeta_tsetb: - | movzx RCd, PC_RC - |.if DUALNUM - | setint RC - | mov TMP1, RC - |.else - | cvtsi2sd xmm0, RCd - | movsd TMP1, xmm0 - |.endif - | lea RC, TMP1 - | jmp >1 - | - |->vmeta_tsetv: - | movzx RCd, PC_RC // Reload TValue *k from RC. - | lea RC, [BASE+RC*8] - |1: - | movzx RBd, PC_RB // Reload TValue *t from RB. - | lea RB, [BASE+RB*8] - |2: - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE // Caveat: CARG2/CARG3 may be BASE. - | mov CARG2, RB - | mov CARG3, RC - | mov L:RB, L:CARG1 - | mov SAVE_PC, PC - | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // TValue * (finished) or NULL (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | movzx RAd, PC_RA - | mov RB, [BASE+RA*8] - | mov [RC], RB - |->cont_nop: // BASE = base, (RC = result) - | ins_next - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | mov RA, L:RB->top - | mov [RA-24], PC // [cont|PC] - | movzx RCd, PC_RA - | // Copy value to third argument. - | mov RB, [BASE+RC*8] - | mov [RA+16], RB - | lea PC, [RA+FRAME_CONT] - | sub PC, BASE - | mov LFUNC:RB, [RA-16] // Guaranteed to be a function here. - | mov NARGS:RDd, 3+1 // 3 args for func(t, k, v). - | cleartp LFUNC:RB - | jmp ->vm_call_dispatch_f - | - |->vmeta_tsetr: - |.if X64WIN - | mov L:CARG1, SAVE_L - | mov CARG3d, RCd - | mov L:CARG1->base, BASE - | xchg CARG2, TAB:RB // Caveat: CARG2 == BASE. - |.else - | mov L:CARG1, SAVE_L - | mov CARG2, TAB:RB - | mov L:CARG1->base, BASE - | mov RB, BASE // Save BASE. - | mov CARG3d, RCd // Caveat: CARG3 == BASE. - |.endif - | mov SAVE_PC, PC - | call extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - | // TValue * returned in eax (RC). - | movzx RAd, PC_RA - | mov BASE, RB // Restore BASE. - | jmp ->BC_TSETR_Z - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - | movzx RDd, PC_RD - | movzx RAd, PC_RA - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2/CARG3 == BASE. - |.if X64WIN - | lea CARG3, [BASE+RD*8] - | lea CARG2, [BASE+RA*8] - |.else - | lea CARG2, [BASE+RA*8] - | lea CARG3, [BASE+RD*8] - |.endif - | mov CARG1, L:RB // Caveat: CARG1/CARG4 == RA. - | movzx CARG4d, PC_OP - | mov SAVE_PC, PC - | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - |3: - | mov BASE, L:RB->base - | cmp RC, 1 - | ja ->vmeta_binop - |4: - | lea PC, [PC+4] - | jb >6 - |5: - | movzx RDd, PC_RD - | branchPC RD - |6: - | ins_next - | - |->cont_condt: // BASE = base, RC = result - | add PC, 4 - | mov ITYPE, [RC] - | sar ITYPE, 47 - | cmp ITYPEd, LJ_TISTRUECOND // Branch if result is true. - | jb <5 - | jmp <6 - | - |->cont_condf: // BASE = base, RC = result - | mov ITYPE, [RC] - | sar ITYPE, 47 - | cmp ITYPEd, LJ_TISTRUECOND // Branch if result is false. - | jmp <4 - | - |->vmeta_equal: - | cleartp TAB:RD - | sub PC, 4 - |.if X64WIN - | mov CARG3, RD - | mov CARG4d, RBd - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2 == BASE. - | mov CARG2, RA - | mov CARG1, L:RB // Caveat: CARG1 == RA. - |.else - | mov CARG2, RA - | mov CARG4d, RBd // Caveat: CARG4 == RA. - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG3 == BASE. - | mov CARG3, RD - | mov CARG1, L:RB - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - | jmp <3 - | - |->vmeta_equal_cd: - |.if FFI - | sub PC, 4 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov CARG1, L:RB - | mov CARG2d, dword [PC-4] - | mov SAVE_PC, PC - | call extern lj_meta_equal_cd // (lua_State *L, BCIns ins) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - | jmp <3 - |.endif - | - |->vmeta_istype: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2/CARG3 may be BASE. - | mov CARG2d, RAd - | mov CARG3d, RDd - | mov L:CARG1, L:RB - | mov SAVE_PC, PC - | call extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - | mov BASE, L:RB->base - | jmp <6 - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vno: - |.if DUALNUM - | movzx RBd, PC_RB - | movzx RCd, PC_RC - |.endif - |->vmeta_arith_vn: - | lea RC, [KBASE+RC*8] - | jmp >1 - | - |->vmeta_arith_nvo: - |.if DUALNUM - | movzx RBd, PC_RB - | movzx RCd, PC_RC - |.endif - |->vmeta_arith_nv: - | lea TMPR, [KBASE+RC*8] - | lea RC, [BASE+RB*8] - | mov RB, TMPR - | jmp >2 - | - |->vmeta_unm: - | lea RC, [BASE+RD*8] - | mov RB, RC - | jmp >2 - | - |->vmeta_arith_vvo: - |.if DUALNUM - | movzx RBd, PC_RB - | movzx RCd, PC_RC - |.endif - |->vmeta_arith_vv: - | lea RC, [BASE+RC*8] - |1: - | lea RB, [BASE+RB*8] - |2: - | lea RA, [BASE+RA*8] - |.if X64WIN - | mov CARG3, RB - | mov CARG4, RC - | movzx RCd, PC_OP - | mov ARG5d, RCd - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2 == BASE. - | mov CARG2, RA - | mov CARG1, L:RB // Caveat: CARG1 == RA. - |.else - | movzx CARG5d, PC_OP - | mov CARG2, RA - | mov CARG4, RC // Caveat: CARG4 == RA. - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE // Caveat: CARG3 == BASE. - | mov CARG3, RB - | mov L:RB, L:CARG1 - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // NULL (finished) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = base, RC = new base, stack = cont/func/o1/o2 - | mov RA, RC - | sub RC, BASE - | mov [RA-24], PC // [cont|PC] - | lea PC, [RC+FRAME_CONT] - | mov NARGS:RDd, 2+1 // 2 args for func(o1, o2). - | jmp ->vm_call_dispatch - | - |->vmeta_len: - | movzx RDd, PC_RD - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | lea CARG2, [BASE+RD*8] // Caveat: CARG2 == BASE - | mov L:CARG1, L:RB - | mov SAVE_PC, PC - | call extern lj_meta_len // (lua_State *L, TValue *o) - | // NULL (retry) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base -#if LJ_52 - | test RC, RC - | jne ->vmeta_binop // Binop call for compatibility. - | movzx RDd, PC_RD - | mov TAB:CARG1, [BASE+RD*8] - | cleartp TAB:CARG1 - | jmp ->BC_LEN_Z -#else - | jmp ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call_ra: - | lea RA, [BASE+RA*8+16] - |->vmeta_call: // Resolve and call __call metamethod. - | // BASE = old base, RA = new base, RC = nargs+1, PC = return - | mov TMP1d, NARGS:RDd // Save RA, RC for us. - | mov RB, RA - |.if X64WIN - | mov L:TMPR, SAVE_L - | mov L:TMPR->base, BASE // Caveat: CARG2 is BASE. - | lea CARG2, [RA-16] - | lea CARG3, [RA+NARGS:RD*8-8] - | mov CARG1, L:TMPR // Caveat: CARG1 is RA. - |.else - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE // Caveat: CARG3 is BASE. - | lea CARG2, [RA-16] - | lea CARG3, [RA+NARGS:RD*8-8] - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | mov RA, RB - | mov L:RB, SAVE_L - | mov BASE, L:RB->base - | mov NARGS:RDd, TMP1d - | mov LFUNC:RB, [RA-16] - | add NARGS:RDd, 1 - | // This is fragile. L->base must not move, KBASE must always be defined. - | cmp KBASE, BASE // Continue with CALLT if flag set. - | je ->BC_CALLT_Z - | cleartp LFUNC:RB - | mov BASE, RA - | ins_call // Otherwise call resolved metamethod. - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov CARG2, RA // Caveat: CARG2 == BASE - | mov L:CARG1, L:RB // Caveat: CARG1 == RA - | mov SAVE_PC, PC - | call extern lj_meta_for // (lua_State *L, TValue *base) - | mov BASE, L:RB->base - | mov RCd, [PC-4] - | movzx RAd, RCH - | movzx OP, RCL - | shr RCd, 16 - | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI. - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | cmp NARGS:RDd, 1+1; jb ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | cmp NARGS:RDd, 2+1; jb ->fff_fallback - |.endmacro - | - |.macro .ffunc_n, name, op - | .ffunc_1 name - | checknumtp [BASE], ->fff_fallback - | op xmm0, qword [BASE] - |.endmacro - | - |.macro .ffunc_n, name - | .ffunc_n name, movsd - |.endmacro - | - |.macro .ffunc_nn, name - | .ffunc_2 name - | checknumtp [BASE], ->fff_fallback - | checknumtp [BASE+8], ->fff_fallback - | movsd xmm0, qword [BASE] - | movsd xmm1, qword [BASE+8] - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses label 1. - |.macro ffgccheck - | mov RB, [DISPATCH+DISPATCH_GL(gc.total)] - | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)] - | jb >1 - | call ->fff_gcstep - |1: - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | mov ITYPE, [BASE] - | mov RB, ITYPE - | sar ITYPE, 47 - | cmp ITYPEd, LJ_TISTRUECOND; jae ->fff_fallback - | mov PC, [BASE-8] - | mov MULTRES, RDd - | mov RB, [BASE] - | mov [BASE-16], RB - | sub RDd, 2 - | jz >2 - | mov RA, BASE - |1: - | add RA, 8 - | mov RB, [RA] - | mov [RA-16], RB - | sub RDd, 1 - | jnz <1 - |2: - | mov RDd, MULTRES - | jmp ->fff_res_ - | - |.ffunc_1 type - | mov RC, [BASE] - | sar RC, 47 - | mov RBd, LJ_TISNUM - | cmp RCd, RBd - | cmovb RCd, RBd - | not RCd - |2: - | mov CFUNC:RB, [BASE-16] - | cleartp CFUNC:RB - | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))] - | mov PC, [BASE-8] - | settp STR:RC, LJ_TSTR - | mov [BASE-16], STR:RC - | jmp ->fff_res1 - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | mov TAB:RB, [BASE] - | mov PC, [BASE-8] - | checktab TAB:RB, >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | mov TAB:RB, TAB:RB->metatable - |2: - | test TAB:RB, TAB:RB - | mov aword [BASE-16], LJ_TNIL - | jz ->fff_res1 - | settp TAB:RC, TAB:RB, LJ_TTAB - | mov [BASE-16], TAB:RC // Store metatable as default result. - | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+8*(GCROOT_MMNAME+MM_metatable)] - | mov RAd, TAB:RB->hmask - | and RAd, STR:RC->sid - | settp STR:RC, LJ_TSTR - | imul RAd, #NODE - | add NODE:RA, TAB:RB->node - |3: // Rearranged logic, because we expect _not_ to find the key. - | cmp NODE:RA->key, STR:RC - | je >5 - |4: - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <3 - | jmp ->fff_res1 // Not found, keep default result. - |5: - | mov RB, NODE:RA->val - | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value. - | mov [BASE-16], RB // Return value of mt.__metatable. - | jmp ->fff_res1 - | - |6: - | cmp ITYPEd, LJ_TUDATA; je <1 - | cmp ITYPEd, LJ_TISNUM; ja >7 - | mov ITYPEd, LJ_TISNUM - |7: - | not ITYPEd - | mov TAB:RB, [DISPATCH+ITYPE*8+DISPATCH_GL(gcroot[GCROOT_BASEMT])] - | jmp <2 - | - |.ffunc_2 setmetatable - | mov TAB:RB, [BASE] - | mov TAB:TMPR, TAB:RB - | checktab TAB:RB, ->fff_fallback - | // Fast path: no mt for table yet and not clearing the mt. - | cmp aword TAB:RB->metatable, 0; jne ->fff_fallback - | mov TAB:RA, [BASE+8] - | checktab TAB:RA, ->fff_fallback - | mov TAB:RB->metatable, TAB:RA - | mov PC, [BASE-8] - | mov [BASE-16], TAB:TMPR // Return original table. - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jz >1 - | // Possible write barrier. Table is black, but skip iswhite(mt) check. - | barrierback TAB:RB, RC - |1: - | jmp ->fff_res1 - | - |.ffunc_2 rawget - |.if X64WIN - | mov TAB:RA, [BASE] - | checktab TAB:RA, ->fff_fallback - | mov RB, BASE // Save BASE. - | lea CARG3, [BASE+8] - | mov CARG2, TAB:RA // Caveat: CARG2 == BASE. - | mov CARG1, SAVE_L - |.else - | mov TAB:CARG2, [BASE] - | checktab TAB:CARG2, ->fff_fallback - | mov RB, BASE // Save BASE. - | lea CARG3, [BASE+8] // Caveat: CARG3 == BASE. - | mov CARG1, SAVE_L - |.endif - | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // cTValue * returned in eax (RD). - | mov BASE, RB // Restore BASE. - | // Copy table slot. - | mov RB, [RD] - | mov PC, [BASE-8] - | mov [BASE-16], RB - | jmp ->fff_res1 - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | cmp NARGS:RDd, 1+1; jne ->fff_fallback // Exactly one argument. - | mov RB, [BASE] - | checknumber RB, ->fff_fallback - | mov PC, [BASE-8] - | mov [BASE-16], RB - | jmp ->fff_res1 - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | mov PC, [BASE-8] - | mov STR:RB, [BASE] - | checktp_nc STR:RB, LJ_TSTR, >3 - | // A __tostring method in the string base metatable is ignored. - |2: - | mov [BASE-16], STR:RB - | jmp ->fff_res1 - |3: // Handle numbers inline, unless a number base metatable is present. - | cmp ITYPEd, LJ_TISNUM; ja ->fff_fallback_1 - | cmp aword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0 - | jne ->fff_fallback - | ffgccheck // Caveat: uses label 1. - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Add frame since C call can throw. - | mov SAVE_PC, PC // Redundant (but a defined value). - |.if not X64WIN - | mov CARG2, BASE // Otherwise: CARG2 == BASE - |.endif - | mov L:CARG1, L:RB - |.if DUALNUM - | call extern lj_strfmt_number // (lua_State *L, cTValue *o) - |.else - | call extern lj_strfmt_num // (lua_State *L, lua_Number *np) - |.endif - | // GCstr returned in eax (RD). - | mov BASE, L:RB->base - | settp STR:RB, RD, LJ_TSTR - | jmp <2 - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | je >2 // Missing 2nd arg? - |1: - | mov CARG1, [BASE] - | mov PC, [BASE-8] - | checktab CARG1, ->fff_fallback - | mov RB, BASE // Save BASE. - |.if X64WIN - | lea CARG3, [BASE-16] - | lea CARG2, [BASE+8] // Caveat: CARG2 == BASE. - |.else - | lea CARG2, [BASE+8] - | lea CARG3, [BASE-16] // Caveat: CARG3 == BASE. - |.endif - | call extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - | // 1=found, 0=end, -1=error returned in eax (RD). - | mov BASE, RB // Restore BASE. - | test RDd, RDd; jg ->fff_res2 // Found key/value. - | js ->fff_fallback_2 // Invalid key. - | // End of traversal: return nil. - | mov aword [BASE-16], LJ_TNIL - | jmp ->fff_res1 - |2: // Set missing 2nd arg to nil. - | mov aword [BASE+8], LJ_TNIL - | jmp <1 - | - |.ffunc_1 pairs - | mov TAB:RB, [BASE] - | mov TMPR, TAB:RB - | checktab TAB:RB, ->fff_fallback -#if LJ_52 - | cmp aword TAB:RB->metatable, 0; jne ->fff_fallback -#endif - | mov CFUNC:RD, [BASE-16] - | cleartp CFUNC:RD - | mov CFUNC:RD, CFUNC:RD->upvalue[0] - | settp CFUNC:RD, LJ_TFUNC - | mov PC, [BASE-8] - | mov [BASE-16], CFUNC:RD - | mov [BASE-8], TMPR - | mov aword [BASE], LJ_TNIL - | mov RDd, 1+3 - | jmp ->fff_res - | - |.ffunc_2 ipairs_aux - | mov TAB:RB, [BASE] - | checktab TAB:RB, ->fff_fallback - |.if DUALNUM - | mov RA, [BASE+8] - | checkint RA, ->fff_fallback - |.else - | checknumtp [BASE+8], ->fff_fallback - | movsd xmm0, qword [BASE+8] - |.endif - | mov PC, [BASE-8] - |.if DUALNUM - | add RAd, 1 - | setint ITYPE, RA - | mov [BASE-16], ITYPE - |.else - | sseconst_1 xmm1, TMPR - | addsd xmm0, xmm1 - | cvttsd2si RAd, xmm0 - | movsd qword [BASE-16], xmm0 - |.endif - | cmp RAd, TAB:RB->asize; jae >2 // Not in array part? - | mov RD, TAB:RB->array - | lea RD, [RD+RA*8] - |1: - | cmp aword [RD], LJ_TNIL; je ->fff_res0 - | // Copy array slot. - | mov RB, [RD] - | mov [BASE-8], RB - |->fff_res2: - | mov RDd, 1+2 - | jmp ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | cmp dword TAB:RB->hmask, 0; je ->fff_res0 - |.if X64WIN - | mov TMPR, BASE - | mov CARG2d, RAd - | mov CARG1, TAB:RB - | mov RB, TMPR - |.else - | mov CARG1, TAB:RB - | mov RB, BASE // Save BASE. - | mov CARG2d, RAd // Caveat: CARG2 == BASE - |.endif - | call extern lj_tab_getinth // (GCtab *t, int32_t key) - | // cTValue * or NULL returned in eax (RD). - | mov BASE, RB - | test RD, RD - | jnz <1 - |->fff_res0: - | mov RDd, 1+0 - | jmp ->fff_res - | - |.ffunc_1 ipairs - | mov TAB:RB, [BASE] - | mov TMPR, TAB:RB - | checktab TAB:RB, ->fff_fallback -#if LJ_52 - | cmp aword TAB:RB->metatable, 0; jne ->fff_fallback -#endif - | mov CFUNC:RD, [BASE-16] - | cleartp CFUNC:RD - | mov CFUNC:RD, CFUNC:RD->upvalue[0] - | settp CFUNC:RD, LJ_TFUNC - | mov PC, [BASE-8] - | mov [BASE-16], CFUNC:RD - | mov [BASE-8], TMPR - |.if DUALNUM - | mov64 RD, ((uint64_t)LJ_TISNUM<<47) - | mov [BASE], RD - |.else - | mov qword [BASE], 0 - |.endif - | mov RDd, 1+3 - | jmp ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc_1 pcall - | lea RA, [BASE+16] - | sub NARGS:RDd, 1 - | mov PCd, 16+FRAME_PCALL - |1: - | movzx RBd, byte [DISPATCH+DISPATCH_GL(hookmask)] - | shr RB, HOOK_ACTIVE_SHIFT - | and RB, 1 - | add PC, RB // Remember active hook before pcall. - | // Note: this does a (harmless) copy of the function to the PC slot, too. - | mov KBASE, RD - |2: - | mov RB, [RA+KBASE*8-24] - | mov [RA+KBASE*8-16], RB - | sub KBASE, 1 - | ja <2 - | jmp ->vm_call_dispatch - | - |.ffunc_2 xpcall - | mov LFUNC:RA, [BASE+8] - | checktp_nc LFUNC:RA, LJ_TFUNC, ->fff_fallback - | mov LFUNC:RB, [BASE] // Swap function and traceback. - | mov [BASE], LFUNC:RA - | mov [BASE+8], LFUNC:RB - | lea RA, [BASE+24] - | sub NARGS:RDd, 2 - | mov PCd, 24+FRAME_PCALL - | jmp <1 - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | mov L:RB, [BASE] - | cleartp L:RB - |.else - |.ffunc coroutine_wrap_aux - | mov CFUNC:RB, [BASE-16] - | cleartp CFUNC:RB - | mov L:RB, CFUNC:RB->upvalue[0].gcr - | cleartp L:RB - |.endif - | mov PC, [BASE-8] - | mov SAVE_PC, PC - | mov TMP1, L:RB - |.if resume - | checktptp [BASE], LJ_TTHREAD, ->fff_fallback - |.endif - | cmp aword L:RB->cframe, 0; jne ->fff_fallback - | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback - | mov RA, L:RB->top - | je >1 // Status != LUA_YIELD (i.e. 0)? - | cmp RA, L:RB->base // Check for presence of initial func. - | je ->fff_fallback - | mov PC, [RA-8] // Move initial function up. - | mov [RA], PC - | add RA, 8 - |1: - |.if resume - | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread). - |.else - | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1). - |.endif - | cmp PC, L:RB->maxstack; ja ->fff_fallback - | mov L:RB->top, PC - | - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - |.if resume - | add BASE, 8 // Keep resumed thread in stack for GC. - |.endif - | mov L:RB->top, BASE - |.if resume - | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move. - |.else - | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move. - |.endif - | sub RB, PC // Relative to PC. - | - | cmp PC, RA - | je >3 - |2: // Move args to coroutine. - | mov RC, [PC+RB] - | mov [PC-8], RC - | sub PC, 8 - | cmp PC, RA - | jne <2 - |3: - | mov CARG2, RA - | mov CARG1, TMP1 - | call ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | - | mov L:RB, SAVE_L - | mov L:PC, TMP1 - | mov BASE, L:RB->base - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | - | cmp eax, LUA_YIELD - | ja >8 - |4: - | mov RA, L:PC->base - | mov KBASE, L:PC->top - | mov L:PC->top, RA // Clear coroutine stack. - | mov PC, KBASE - | sub PC, RA - | je >6 // No results? - | lea RD, [BASE+PC] - | shr PCd, 3 - | cmp RD, L:RB->maxstack - | ja >9 // Need to grow stack? - | - | mov RB, BASE - | sub RB, RA - |5: // Move results from coroutine. - | mov RD, [RA] - | mov [RA+RB], RD - | add RA, 8 - | cmp RA, KBASE - | jne <5 - |6: - |.if resume - | lea RDd, [PCd+2] // nresults+1 = 1 + true + results. - | mov_true ITYPE // Prepend true to results. - | mov [BASE-8], ITYPE - |.else - | lea RDd, [PCd+1] // nresults+1 = 1 + results. - |.endif - |7: - | mov PC, SAVE_PC - | mov MULTRES, RDd - |.if resume - | mov RA, -8 - |.else - | xor RAd, RAd - |.endif - | test PCd, FRAME_TYPE - | jz ->BC_RET_Z - | jmp ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | mov_false ITYPE // Prepend false to results. - | mov [BASE-8], ITYPE - | mov RA, L:PC->top - | sub RA, 8 - | mov L:PC->top, RA // Clear error from coroutine stack. - | // Copy error message. - | mov RD, [RA] - | mov [BASE], RD - | mov RDd, 1+2 // nresults+1 = 1 + false + error. - | jmp <7 - |.else - | mov CARG2, L:PC - | mov CARG1, L:RB - | call extern lj_ffh_coroutine_wrap_err // (lua_State *L, lua_State *co) - | // Error function does not return. - |.endif - | - |9: // Handle stack expansion on return from yield. - | mov L:RA, TMP1 - | mov L:RA->top, KBASE // Undo coroutine stack clearing. - | mov CARG2, PC - | mov CARG1, L:RB - | call extern lj_state_growstack // (lua_State *L, int n) - | mov L:PC, TMP1 - | mov BASE, L:RB->base - | jmp <4 // Retry the stack move. - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | mov L:RB, SAVE_L - | test aword L:RB->cframe, CFRAME_RESUME - | jz ->fff_fallback - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB->top, RD - | xor RDd, RDd - | mov aword L:RB->cframe, RD - | mov al, LUA_YIELD - | mov byte L:RB->status, al - | jmp ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - | .ffunc_1 math_abs - | mov RB, [BASE] - |.if DUALNUM - | checkint RB, >3 - | cmp RBd, 0; jns ->fff_resi - | neg RBd; js >2 - |->fff_resbit: - |->fff_resi: - | setint RB - |->fff_resRB: - | mov PC, [BASE-8] - | mov [BASE-16], RB - | jmp ->fff_res1 - |2: - | mov64 RB, U64x(41e00000,00000000) // 2^31. - | jmp ->fff_resRB - |3: - | ja ->fff_fallback - |.else - | checknum RB, ->fff_fallback - |.endif - | shl RB, 1 - | shr RB, 1 - | mov PC, [BASE-8] - | mov [BASE-16], RB - | jmp ->fff_res1 - | - |.ffunc_n math_sqrt, sqrtsd - |->fff_resxmm0: - | mov PC, [BASE-8] - | movsd qword [BASE-16], xmm0 - | // fallthrough - | - |->fff_res1: - | mov RDd, 1+1 - |->fff_res: - | mov MULTRES, RDd - |->fff_res_: - | test PCd, FRAME_TYPE - | jnz >7 - |5: - | cmp PC_RB, RDL // More results expected? - | ja >6 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | movzx RAd, PC_RA - | neg RA - | lea BASE, [BASE+RA*8-16] // base = base - (RA+2)*8 - | ins_next - | - |6: // Fill up results with nil. - | mov aword [BASE+RD*8-24], LJ_TNIL - | add RD, 1 - | jmp <5 - | - |7: // Non-standard return case. - | mov RA, -16 // Results start at BASE+RA = BASE-16. - | jmp ->vm_return - | - |.macro math_round, func - | .ffunc math_ .. func - |.if DUALNUM - | mov RB, [BASE] - | checknumx RB, ->fff_resRB, je - | ja ->fff_fallback - |.else - | checknumtp [BASE], ->fff_fallback - |.endif - | movsd xmm0, qword [BASE] - | call ->vm_ .. func .. _sse - |.if DUALNUM - | cvttsd2si RBd, xmm0 - | cmp RBd, 0x80000000 - | jne ->fff_resi - | cvtsi2sd xmm1, RBd - | ucomisd xmm0, xmm1 - | jp ->fff_resxmm0 - | je ->fff_resi - |.endif - | jmp ->fff_resxmm0 - |.endmacro - | - | math_round floor - | math_round ceil - | - |.ffunc math_log - | cmp NARGS:RDd, 1+1; jne ->fff_fallback // Exactly one argument. - | checknumtp [BASE], ->fff_fallback - | movsd xmm0, qword [BASE] - | mov RB, BASE - | call extern log - | mov BASE, RB - | jmp ->fff_resxmm0 - | - |.macro math_extern, func - | .ffunc_n math_ .. func - | mov RB, BASE - | call extern func - | mov BASE, RB - | jmp ->fff_resxmm0 - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nn math_ .. func - | mov RB, BASE - | call extern func - | mov BASE, RB - | jmp ->fff_resxmm0 - |.endmacro - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.ffunc_2 math_ldexp - | checknumtp [BASE], ->fff_fallback - | checknumtp [BASE+8], ->fff_fallback - | fld qword [BASE+8] - | fld qword [BASE] - | fscale - | fpop1 - | mov PC, [BASE-8] - | fstp qword [BASE-16] - | jmp ->fff_res1 - | - |.ffunc_n math_frexp - | mov RB, BASE - |.if X64WIN - | lea CARG2, TMP1 // Caveat: CARG2 == BASE - |.else - | lea CARG1, TMP1 - |.endif - | call extern frexp - | mov BASE, RB - | mov RBd, TMP1d - | mov PC, [BASE-8] - | movsd qword [BASE-16], xmm0 - |.if DUALNUM - | setint RB - | mov [BASE-8], RB - |.else - | cvtsi2sd xmm1, RBd - | movsd qword [BASE-8], xmm1 - |.endif - | mov RDd, 1+2 - | jmp ->fff_res - | - |.ffunc_n math_modf - | mov RB, BASE - |.if X64WIN - | lea CARG2, [BASE-16] // Caveat: CARG2 == BASE - |.else - | lea CARG1, [BASE-16] - |.endif - | call extern modf - | mov BASE, RB - | mov PC, [BASE-8] - | movsd qword [BASE-8], xmm0 - | mov RDd, 1+2 - | jmp ->fff_res - | - |.macro math_minmax, name, cmovop, sseop - | .ffunc_1 name - | mov RAd, 2 - |.if DUALNUM - | mov RB, [BASE] - | checkint RB, >4 - |1: // Handle integers. - | cmp RAd, RDd; jae ->fff_resRB - | mov TMPR, [BASE+RA*8-8] - | checkint TMPR, >3 - | cmp RBd, TMPRd - | cmovop RB, TMPR - | add RAd, 1 - | jmp <1 - |3: - | ja ->fff_fallback - | // Convert intermediate result to number and continue below. - | cvtsi2sd xmm0, RBd - | jmp >6 - |4: - | ja ->fff_fallback - |.else - | checknumtp [BASE], ->fff_fallback - |.endif - | - | movsd xmm0, qword [BASE] - |5: // Handle numbers or integers. - | cmp RAd, RDd; jae ->fff_resxmm0 - |.if DUALNUM - | mov RB, [BASE+RA*8-8] - | checknumx RB, >6, jb - | ja ->fff_fallback - | cvtsi2sd xmm1, RBd - | jmp >7 - |.else - | checknumtp [BASE+RA*8-8], ->fff_fallback - |.endif - |6: - | movsd xmm1, qword [BASE+RA*8-8] - |7: - | sseop xmm0, xmm1 - | add RAd, 1 - | jmp <5 - |.endmacro - | - | math_minmax math_min, cmovg, minsd - | math_minmax math_max, cmovl, maxsd - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | cmp NARGS:RDd, 1+1; jne ->fff_fallback - | mov STR:RB, [BASE] - | checkstr STR:RB, ->fff_fallback - | mov PC, [BASE-8] - | cmp dword STR:RB->len, 1 - | jb ->fff_res0 // Return no results for empty string. - | movzx RBd, byte STR:RB[1] - |.if DUALNUM - | jmp ->fff_resi - |.else - | cvtsi2sd xmm0, RBd; jmp ->fff_resxmm0 - |.endif - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | cmp NARGS:RDd, 1+1; jne ->fff_fallback // *Exactly* 1 arg. - |.if DUALNUM - | mov RB, [BASE] - | checkint RB, ->fff_fallback - |.else - | checknumtp [BASE], ->fff_fallback - | cvttsd2si RBd, qword [BASE] - |.endif - | cmp RBd, 255; ja ->fff_fallback - | mov TMP1d, RBd - | mov TMPRd, 1 - | lea RD, TMP1 // Points to stack. Little-endian. - |->fff_newstr: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov CARG3d, TMPRd // Zero-extended to size_t. - | mov CARG2, RD - | mov CARG1, L:RB - | mov SAVE_PC, PC - | call extern lj_str_new // (lua_State *L, char *str, size_t l) - |->fff_resstr: - | // GCstr * returned in eax (RD). - | mov BASE, L:RB->base - | mov PC, [BASE-8] - | settp STR:RD, LJ_TSTR - | mov [BASE-16], STR:RD - | jmp ->fff_res1 - | - |.ffunc string_sub - | ffgccheck - | mov TMPRd, -1 - | cmp NARGS:RDd, 1+2; jb ->fff_fallback - | jna >1 - |.if DUALNUM - | mov TMPR, [BASE+16] - | checkint TMPR, ->fff_fallback - |.else - | checknumtp [BASE+16], ->fff_fallback - | cvttsd2si TMPRd, qword [BASE+16] - |.endif - |1: - | mov STR:RB, [BASE] - | checkstr STR:RB, ->fff_fallback - |.if DUALNUM - | mov ITYPE, [BASE+8] - | mov RAd, ITYPEd // Must clear hiword for lea below. - | sar ITYPE, 47 - | cmp ITYPEd, LJ_TISNUM - | jne ->fff_fallback - |.else - | checknumtp [BASE+8], ->fff_fallback - | cvttsd2si RAd, qword [BASE+8] - |.endif - | mov RCd, STR:RB->len - | cmp RCd, TMPRd // len < end? (unsigned compare) - | jb >5 - |2: - | test RAd, RAd // start <= 0? - | jle >7 - |3: - | sub TMPRd, RAd // start > end? - | jl ->fff_emptystr - | lea RD, [STR:RB+RAd+#STR-1] - | add TMPRd, 1 - |4: - | jmp ->fff_newstr - | - |5: // Negative end or overflow. - | jl >6 - | lea TMPRd, [TMPRd+RCd+1] // end = end+(len+1) - | jmp <2 - |6: // Overflow. - | mov TMPRd, RCd // end = len - | jmp <2 - | - |7: // Negative start or underflow. - | je >8 - | add RAd, RCd // start = start+(len+1) - | add RAd, 1 - | jg <3 // start > 0? - |8: // Underflow. - | mov RAd, 1 // start = 1 - | jmp <3 - | - |->fff_emptystr: // Range underflow. - | xor TMPRd, TMPRd // Zero length. Any ptr in RD is ok. - | jmp <4 - | - |.macro ffstring_op, name - | .ffunc_1 string_ .. name - | ffgccheck - |.if X64WIN - | mov STR:TMPR, [BASE] - | checkstr STR:TMPR, ->fff_fallback - |.else - | mov STR:CARG2, [BASE] - | checkstr STR:CARG2, ->fff_fallback - |.endif - | mov L:RB, SAVE_L - | lea SBUF:CARG1, [DISPATCH+DISPATCH_GL(tmpbuf)] - | mov L:RB->base, BASE - |.if X64WIN - | mov STR:CARG2, STR:TMPR // Caveat: CARG2 == BASE - |.endif - | mov RC, SBUF:CARG1->b - | mov SBUF:CARG1->L, L:RB - | mov SBUF:CARG1->w, RC - | mov SAVE_PC, PC - | call extern lj_buf_putstr_ .. name - | mov CARG1, rax - | call extern lj_buf_tostr - | jmp ->fff_resstr - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |.macro .ffunc_bit, name, kind, fdef - | fdef name - |.if kind == 2 - | sseconst_tobit xmm1, RB - |.endif - |.if DUALNUM - | mov RB, [BASE] - | checkint RB, >1 - |.if kind > 0 - | jmp >2 - |.else - | jmp ->fff_resbit - |.endif - |1: - | ja ->fff_fallback - | movd xmm0, RB - |.else - | checknumtp [BASE], ->fff_fallback - | movsd xmm0, qword [BASE] - |.endif - |.if kind < 2 - | sseconst_tobit xmm1, RB - |.endif - | addsd xmm0, xmm1 - | movd RBd, xmm0 - |2: - |.endmacro - | - |.macro .ffunc_bit, name, kind - | .ffunc_bit name, kind, .ffunc_1 - |.endmacro - | - |.ffunc_bit bit_tobit, 0 - | jmp ->fff_resbit - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name, 2 - | mov TMPRd, NARGS:RDd // Save for fallback. - | lea RD, [BASE+NARGS:RD*8-16] - |1: - | cmp RD, BASE - | jbe ->fff_resbit - |.if DUALNUM - | mov RA, [RD] - | checkint RA, >2 - | ins RBd, RAd - | sub RD, 8 - | jmp <1 - |2: - | ja ->fff_fallback_bit_op - | movd xmm0, RA - |.else - | checknumtp [RD], ->fff_fallback_bit_op - | movsd xmm0, qword [RD] - |.endif - | addsd xmm0, xmm1 - | movd RAd, xmm0 - | ins RBd, RAd - | sub RD, 8 - | jmp <1 - |.endmacro - | - |.ffunc_bit_op bit_band, and - |.ffunc_bit_op bit_bor, or - |.ffunc_bit_op bit_bxor, xor - | - |.ffunc_bit bit_bswap, 1 - | bswap RBd - | jmp ->fff_resbit - | - |.ffunc_bit bit_bnot, 1 - | not RBd - |.if DUALNUM - | jmp ->fff_resbit - |.else - |->fff_resbit: - | cvtsi2sd xmm0, RBd - | jmp ->fff_resxmm0 - |.endif - | - |->fff_fallback_bit_op: - | mov NARGS:RDd, TMPRd // Restore for fallback - | jmp ->fff_fallback - | - |.macro .ffunc_bit_sh, name, ins - |.if DUALNUM - | .ffunc_bit name, 1, .ffunc_2 - | // Note: no inline conversion from number for 2nd argument! - | mov RA, [BASE+8] - | checkint RA, ->fff_fallback - |.else - | .ffunc_nn name - | sseconst_tobit xmm2, RB - | addsd xmm0, xmm2 - | addsd xmm1, xmm2 - | movd RBd, xmm0 - | movd RAd, xmm1 - |.endif - | ins RBd, cl // Assumes RA is ecx. - | jmp ->fff_resbit - |.endmacro - | - |.ffunc_bit_sh bit_lshift, shl - |.ffunc_bit_sh bit_rshift, shr - |.ffunc_bit_sh bit_arshift, sar - |.ffunc_bit_sh bit_rol, rol - |.ffunc_bit_sh bit_ror, ror - | - |//----------------------------------------------------------------------- - | - |->fff_fallback_2: - | mov NARGS:RDd, 1+2 // Other args are ignored, anyway. - | jmp ->fff_fallback - |->fff_fallback_1: - | mov NARGS:RDd, 1+1 // Other args are ignored, anyway. - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RD = nargs+1 - | mov L:RB, SAVE_L - | mov PC, [BASE-8] // Fallback may overwrite PC. - | mov SAVE_PC, PC // Redundant (but a defined value). - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler. - | mov L:RB->top, RD - | mov CFUNC:RD, [BASE-16] - | cleartp CFUNC:RD - | cmp RA, L:RB->maxstack - | ja >5 // Need to grow stack. - | mov CARG1, L:RB - | call aword CFUNC:RD->f // (lua_State *L) - | mov BASE, L:RB->base - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | test RDd, RDd; jg ->fff_res // Returned nresults+1? - |1: - | mov RA, L:RB->top - | sub RA, BASE - | shr RAd, 3 - | test RDd, RDd - | lea NARGS:RDd, [RAd+1] - | mov LFUNC:RB, [BASE-16] - | jne ->vm_call_tail // Returned -1? - | cleartp LFUNC:RB - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | mov RA, BASE - | test PCd, FRAME_TYPE - | jnz >3 - | movzx RBd, PC_RA - | neg RB - | lea BASE, [BASE+RB*8-16] // base = base - (RB+2)*8 - | jmp ->vm_call_dispatch // Resolve again for tailcall. - |3: - | mov RB, PC - | and RB, -8 - | sub BASE, RB - | jmp ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | mov CARG2d, LUA_MINSTACK - | mov CARG1, L:RB - | call extern lj_state_growstack // (lua_State *L, int n) - | mov BASE, L:RB->base - | xor RDd, RDd // Simulate a return 0. - | jmp <1 // Dumb retry (goes through ff first). - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RD = nargs+1 - | pop RB // Must keep stack at same level. - | mov TMP1, RB // Save return address - | mov L:RB, SAVE_L - | mov SAVE_PC, PC // Redundant (but a defined value). - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | mov CARG1, L:RB - | mov L:RB->top, RD - | call extern lj_gc_step // (lua_State *L) - | mov BASE, L:RB->base - | mov RD, L:RB->top - | sub RD, BASE - | shr RDd, 3 - | add NARGS:RDd, 1 - | mov RB, TMP1 - | push RB // Restore return address. - | ret - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_VMEVENT // No recording while in vmevent. - | jnz >5 - | // Decrement the hookcount for consistency, but always do the call. - | test RDL, HOOK_ACTIVE - | jnz >1 - | test RDL, LUA_MASKLINE|LUA_MASKCOUNT - | jz >1 - | dec dword [DISPATCH+DISPATCH_GL(hookcount)] - | jmp >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_ACTIVE // Hook already active? - | jnz >5 - | jmp >1 - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | movzx RDd, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_ACTIVE // Hook already active? - | jnz >5 - | - | test RDL, LUA_MASKLINE|LUA_MASKCOUNT - | jz >5 - | dec dword [DISPATCH+DISPATCH_GL(hookcount)] - | jz >1 - | test RDL, LUA_MASKLINE - | jz >5 - |1: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov CARG2, PC // Caveat: CARG2 == BASE - | mov CARG1, L:RB - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | call extern lj_dispatch_ins // (lua_State *L, const BCIns *pc) - |3: - | mov BASE, L:RB->base - |4: - | movzx RAd, PC_RA - |5: - | movzx OP, PC_OP - | movzx RDd, PC_RD - | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins. - | - |->cont_hook: // Continue from hook yield. - | add PC, 4 - | mov RA, [RB-40] - | mov MULTRES, RAd // Restore MULTRES for *M ins. - | jmp <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | mov LFUNC:RB, [BASE-16] // Same as curr_topL(L). - | cleartp LFUNC:RB - | mov RB, LFUNC:RB->pc - | movzx RDd, byte [RB+PC2PROTO(framesize)] - | lea RD, [BASE+RD*8] - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov CARG2, PC - | lea CARG1, [DISPATCH+GG_DISP2J] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RB - | mov SAVE_PC, PC - | call extern lj_trace_hot // (jit_State *J, const BCIns *pc) - | jmp <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mov SAVE_PC, PC - |.if JIT - | jmp >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | mov SAVE_PC, PC - | or PC, 1 // Marker for hot call. - |1: - |.endif - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov CARG2, PC - | mov CARG1, L:RB - | call extern lj_dispatch_call // (lua_State *L, const BCIns *pc) - | // ASMFunction returned in eax/rax (RD). - | mov SAVE_PC, 0 // Invalidate for subsequent line hook. - |.if JIT - | and PC, -2 - |.endif - | mov BASE, L:RB->base - | mov RA, RD - | mov RD, L:RB->top - | sub RD, BASE - | mov RB, RA - | movzx RAd, PC_RA - | shr RDd, 3 - | add NARGS:RDd, 1 - | jmp RB - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // BASE = base, RC = result, RB = mbase - | mov TRACE:ITYPE, [RB-40] // Save previous trace. - | cleartp TRACE:ITYPE - | mov TMPRd, MULTRES - | movzx RAd, PC_RA - | lea RA, [BASE+RA*8] // Call base. - | sub TMPRd, 1 - | jz >2 - |1: // Move results down. - | mov RB, [RC] - | mov [RA], RB - | add RC, 8 - | add RA, 8 - | sub TMPRd, 1 - | jnz <1 - |2: - | movzx RCd, PC_RA - | movzx RBd, PC_RB - | add RC, RB - | lea RC, [BASE+RC*8-8] - |3: - | cmp RC, RA - | ja >9 // More results wanted? - | - | test TRACE:ITYPE, TRACE:ITYPE - | jz ->cont_nop - | movzx RBd, word TRACE:ITYPE->traceno - | movzx RDd, word TRACE:ITYPE->link - | cmp RDd, RBd - | je ->cont_nop // Blacklisted. - | test RDd, RDd - | jne =>BC_JLOOP // Jump to stitched trace. - | - | // Stitch a new trace to the previous trace. - | mov [DISPATCH+DISPATCH_J(exitno)], RB - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov CARG2, PC - | lea CARG1, [DISPATCH+GG_DISP2J] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RB - | call extern lj_dispatch_stitch // (jit_State *J, const BCIns *pc) - | mov BASE, L:RB->base - | jmp ->cont_nop - | - |9: // Fill up results with nil. - | mov aword [RA], LJ_TNIL - | add RA, 8 - | jmp <3 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov CARG2, PC // Caveat: CARG2 == BASE - | mov CARG1, L:RB - | call extern lj_dispatch_profile // (lua_State *L, const BCIns *pc) - | mov BASE, L:RB->base - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | sub PC, 4 - | jmp ->cont_nop -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Called from an exit stub with the exit number on the stack. - |// The 16 bit exit number is stored with two (sign-extended) push imm8. - |->vm_exit_handler: - |.if JIT - | push r13; push r12 - | push r11; push r10; push r9; push r8 - | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp - | push rbx; push rdx; push rcx; push rax - | movzx RCd, byte [rbp-8] // Reconstruct exit number. - | mov RCH, byte [rbp-16] - | mov [rbp-8], r15; mov [rbp-16], r14 - | // DISPATCH is preserved on-trace in LJ_GC64 mode. - | mov RAd, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number. - | set_vmstate EXIT - | mov [DISPATCH+DISPATCH_J(exitno)], RCd - | mov [DISPATCH+DISPATCH_J(parent)], RAd - |.if X64WIN - | sub rsp, 16*8+4*8 // Room for SSE regs + save area. - |.else - | sub rsp, 16*8 // Room for SSE regs. - |.endif - | add rbp, -128 - | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14 - | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12 - | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10 - | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8 - | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6 - | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4 - | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2 - | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0 - | // Caveat: RB is rbp. - | mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)] - | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RB - | mov L:RB->base, BASE - |.if X64WIN - | lea CARG2, [rsp+4*8] - |.else - | mov CARG2, rsp - |.endif - | lea CARG1, [DISPATCH+GG_DISP2J] - | mov qword [DISPATCH+DISPATCH_GL(jit_base)], 0 - | call extern lj_trace_exit // (jit_State *J, ExitState *ex) - | // MULTRES or negated error code returned in eax (RD). - | mov RA, L:RB->cframe - | and RA, CFRAME_RAWMASK - | mov [RA+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield). - | mov BASE, L:RB->base - | mov PC, [RA+CFRAME_OFS_PC] // Get SAVE_PC. - | jmp >1 - |.endif - |->vm_exit_interp: - | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set. - |.if JIT - | // Restore additional callee-save registers only used in compiled code. - |.if X64WIN - | lea RA, [rsp+10*16+4*8] - |1: - | movdqa xmm15, [RA-10*16] - | movdqa xmm14, [RA-9*16] - | movdqa xmm13, [RA-8*16] - | movdqa xmm12, [RA-7*16] - | movdqa xmm11, [RA-6*16] - | movdqa xmm10, [RA-5*16] - | movdqa xmm9, [RA-4*16] - | movdqa xmm8, [RA-3*16] - | movdqa xmm7, [RA-2*16] - | mov rsp, RA // Reposition stack to C frame. - | movdqa xmm6, [RA-1*16] - | mov r15, CSAVE_1 - | mov r14, CSAVE_2 - | mov r13, CSAVE_3 - | mov r12, CSAVE_4 - |.else - | lea RA, [rsp+16] - |1: - | mov r13, [RA-8] - | mov r12, [RA] - | mov rsp, RA // Reposition stack to C frame. - |.endif - | test RDd, RDd; js >9 // Check for error from exit. - | mov L:RB, SAVE_L - | mov MULTRES, RDd - | mov LFUNC:KBASE, [BASE-16] - | cleartp LFUNC:KBASE - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | mov L:RB->base, BASE - | mov qword [DISPATCH+DISPATCH_GL(jit_base)], 0 - | set_vmstate INTERP - | // Modified copy of ins_next which handles function header dispatch, too. - | mov RCd, [PC] - | movzx RAd, RCH - | movzx OP, RCL - | add PC, 4 - | shr RCd, 16 - | cmp OP, BC_FUNCF // Function header? - | jb >3 - | cmp OP, BC_FUNCC+2 // Fast function? - | jae >4 - |2: - | mov RCd, MULTRES // RC/RD holds nres+1. - |3: - | jmp aword [DISPATCH+OP*8] - | - |4: // Check frame below fast function. - | mov RC, [BASE-8] - | test RCd, FRAME_TYPE - | jnz <2 // Trace stitching continuation? - | // Otherwise set KBASE for Lua function below fast function. - | movzx RCd, byte [RC-3] - | neg RC - | mov LFUNC:KBASE, [BASE+RC*8-32] - | cleartp LFUNC:KBASE - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | jmp <2 - | - |9: // Rethrow error from the right C frame. - | mov CARG2d, RDd - | mov CARG1, L:RB - | neg CARG2d - | call extern lj_err_trace // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// FP value rounding. Called by math.floor/math.ceil fast functions - |// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified. - |.macro vm_round, name, mode, cond - |->name: - |->name .. _sse: - | sseconst_abs xmm2, RD - | sseconst_2p52 xmm3, RD - | movaps xmm1, xmm0 - | andpd xmm1, xmm2 // |x| - | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|. - | jbe >1 - | andnpd xmm2, xmm0 // Isolate sign bit. - |.if mode == 2 // trunc(x)? - | movaps xmm0, xmm1 - | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52 - | subsd xmm1, xmm3 - | sseconst_1 xmm3, RD - | cmpsd xmm0, xmm1, 1 // |x| < result? - | andpd xmm0, xmm3 - | subsd xmm1, xmm0 // If yes, subtract -1. - | orpd xmm1, xmm2 // Merge sign bit back in. - |.else - | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52 - | subsd xmm1, xmm3 - | orpd xmm1, xmm2 // Merge sign bit back in. - | sseconst_1 xmm3, RD - | .if mode == 1 // ceil(x)? - | cmpsd xmm0, xmm1, 6 // x > result? - | andpd xmm0, xmm3 - | addsd xmm1, xmm0 // If yes, add 1. - | orpd xmm1, xmm2 // Merge sign bit back in (again). - | .else // floor(x)? - | cmpsd xmm0, xmm1, 1 // x < result? - | andpd xmm0, xmm3 - | subsd xmm1, xmm0 // If yes, subtract 1. - | .endif - |.endif - | movaps xmm0, xmm1 - |1: - | ret - |.endmacro - | - | vm_round vm_floor, 0, 1 - | vm_round vm_ceil, 1, JIT - | vm_round vm_trunc, 2, JIT - | - |// FP modulo x%y. Called by BC_MOD* and vm_arith. - |->vm_mod: - |// Args in xmm0/xmm1, return value in xmm0. - |// Caveat: xmm0-xmm5 and RC (eax) modified! - | movaps xmm5, xmm0 - | divsd xmm0, xmm1 - | sseconst_abs xmm2, RD - | sseconst_2p52 xmm3, RD - | movaps xmm4, xmm0 - | andpd xmm4, xmm2 // |x/y| - | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|. - | jbe >1 - | andnpd xmm2, xmm0 // Isolate sign bit. - | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52 - | subsd xmm4, xmm3 - | orpd xmm4, xmm2 // Merge sign bit back in. - | sseconst_1 xmm2, RD - | cmpsd xmm0, xmm4, 1 // x/y < result? - | andpd xmm0, xmm2 - | subsd xmm4, xmm0 // If yes, subtract 1.0. - | movaps xmm0, xmm5 - | mulsd xmm1, xmm4 - | subsd xmm0, xmm1 - | ret - |1: - | mulsd xmm1, xmm0 - | movaps xmm0, xmm5 - | subsd xmm0, xmm1 - | ret - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |// int lj_vm_cpuid(uint32_t f, uint32_t res[4]) - |->vm_cpuid: - | mov eax, CARG1d - | .if X64WIN; push rsi; mov rsi, CARG2; .endif - | push rbx - | xor ecx, ecx - | cpuid - | mov [rsi], eax - | mov [rsi+4], ebx - | mov [rsi+8], ecx - | mov [rsi+12], edx - | pop rbx - | .if X64WIN; pop rsi; .endif - | ret - | - |.define NEXT_TAB, TAB:CARG1 - |.define NEXT_IDX, CARG2d - |.define NEXT_IDXa, CARG2 - |.define NEXT_PTR, RC - |.define NEXT_PTRd, RCd - |.define NEXT_TMP, CARG3 - |.define NEXT_ASIZE, CARG4d - |.macro NEXT_RES_IDXL, op2; lea edx, [NEXT_IDX+op2]; .endmacro - |.if X64WIN - |.define NEXT_RES_PTR, [rsp+aword*5] - |.macro NEXT_RES_IDX, op2; add NEXT_IDX, op2; .endmacro - |.else - |.define NEXT_RES_PTR, [rsp+aword*1] - |.macro NEXT_RES_IDX, op2; lea edx, [NEXT_IDX+op2]; .endmacro - |.endif - | - |// TValue *lj_vm_next(GCtab *t, uint32_t idx) - |// Next idx returned in edx. - |->vm_next: - |.if JIT - | mov NEXT_ASIZE, NEXT_TAB->asize - |1: // Traverse array part. - | cmp NEXT_IDX, NEXT_ASIZE; jae >5 - | mov NEXT_TMP, NEXT_TAB->array - | mov NEXT_TMP, qword [NEXT_TMP+NEXT_IDX*8] - | cmp NEXT_TMP, LJ_TNIL; je >2 - | lea NEXT_PTR, NEXT_RES_PTR - | mov qword [NEXT_PTR], NEXT_TMP - |.if DUALNUM - | setint NEXT_TMP, NEXT_IDXa - | mov qword [NEXT_PTR+qword*1], NEXT_TMP - |.else - | cvtsi2sd xmm0, NEXT_IDX - | movsd qword [NEXT_PTR+qword*1], xmm0 - |.endif - | NEXT_RES_IDX 1 - | ret - |2: // Skip holes in array part. - | add NEXT_IDX, 1 - | jmp <1 - | - |5: // Traverse hash part. - | sub NEXT_IDX, NEXT_ASIZE - |6: - | cmp NEXT_IDX, NEXT_TAB->hmask; ja >9 - | imul NEXT_PTRd, NEXT_IDX, #NODE - | add NODE:NEXT_PTR, NEXT_TAB->node - | cmp qword NODE:NEXT_PTR->val, LJ_TNIL; je >7 - | NEXT_RES_IDXL NEXT_ASIZE+1 - | ret - |7: // Skip holes in hash part. - | add NEXT_IDX, 1 - | jmp <6 - | - |9: // End of iteration. Set the key to nil (not the value). - | NEXT_RES_IDX NEXT_ASIZE - | lea NEXT_PTR, NEXT_RES_PTR - | mov qword [NEXT_PTR+qword*1], LJ_TNIL - | ret - |.endif - | - |//----------------------------------------------------------------------- - |//-- Assertions --------------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->assert_bad_for_arg_type: -#ifdef LUA_USE_ASSERT - | int3 -#endif - | int3 - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in ah/al. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - | saveregs_ // ebp/rbp already saved. ebp now holds global_State *. - | lea DISPATCH, [ebp+GG_G2DISP] - | mov CTSTATE, GL:ebp->ctype_state - | movzx eax, ax - | mov CTSTATE->cb.slot, eax - | mov CTSTATE->cb.gpr[0], CARG1 - | mov CTSTATE->cb.gpr[1], CARG2 - | mov CTSTATE->cb.gpr[2], CARG3 - | mov CTSTATE->cb.gpr[3], CARG4 - | movsd qword CTSTATE->cb.fpr[0], xmm0 - | movsd qword CTSTATE->cb.fpr[1], xmm1 - | movsd qword CTSTATE->cb.fpr[2], xmm2 - | movsd qword CTSTATE->cb.fpr[3], xmm3 - |.if X64WIN - | lea rax, [rsp+CFRAME_SIZE+4*8] - |.else - | lea rax, [rsp+CFRAME_SIZE] - | mov CTSTATE->cb.gpr[4], CARG5 - | mov CTSTATE->cb.gpr[5], CARG6 - | movsd qword CTSTATE->cb.fpr[4], xmm4 - | movsd qword CTSTATE->cb.fpr[5], xmm5 - | movsd qword CTSTATE->cb.fpr[6], xmm6 - | movsd qword CTSTATE->cb.fpr[7], xmm7 - |.endif - | mov CTSTATE->cb.stack, rax - | mov CARG2, rsp - | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok. - | mov CARG1, CTSTATE - | call extern lj_ccallback_enter // (CTState *cts, void *cf) - | // lua_State * returned in eax (RD). - | set_vmstate INTERP - | mov BASE, L:RD->base - | mov RD, L:RD->top - | sub RD, BASE - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | shr RD, 3 - | add RD, 1 - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | mov L:RA, SAVE_L - | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)] - | mov aword CTSTATE->L, L:RA - | mov L:RA->base, BASE - | mov L:RA->top, RB - | mov CARG1, CTSTATE - | mov CARG2, RC - | call extern lj_ccallback_leave // (CTState *cts, TValue *o) - | mov rax, CTSTATE->cb.gpr[0] - | movsd xmm0, qword CTSTATE->cb.fpr[0] - | jmp ->vm_leave_unw - |.endif - | - |->vm_ffi_call: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - | .type CCSTATE, CCallState, rbx - | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1 - | - | // Readjust stack. - | mov eax, CCSTATE->spadj - | sub rsp, rax - | - | // Copy stack slots. - | movzx ecx, byte CCSTATE->nsp - | sub ecx, 1 - | js >2 - |1: - | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)] - | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax - | sub ecx, 1 - | jns <1 - |2: - | - | movzx eax, byte CCSTATE->nfpr - | mov CARG1, CCSTATE->gpr[0] - | mov CARG2, CCSTATE->gpr[1] - | mov CARG3, CCSTATE->gpr[2] - | mov CARG4, CCSTATE->gpr[3] - |.if not X64WIN - | mov CARG5, CCSTATE->gpr[4] - | mov CARG6, CCSTATE->gpr[5] - |.endif - | test eax, eax; jz >5 - | movaps xmm0, CCSTATE->fpr[0] - | movaps xmm1, CCSTATE->fpr[1] - | movaps xmm2, CCSTATE->fpr[2] - | movaps xmm3, CCSTATE->fpr[3] - |.if not X64WIN - | cmp eax, 4; jbe >5 - | movaps xmm4, CCSTATE->fpr[4] - | movaps xmm5, CCSTATE->fpr[5] - | movaps xmm6, CCSTATE->fpr[6] - | movaps xmm7, CCSTATE->fpr[7] - |.endif - |5: - | - | call aword CCSTATE->func - | - | mov CCSTATE->gpr[0], rax - | movaps CCSTATE->fpr[0], xmm0 - |.if not X64WIN - | mov CCSTATE->gpr[1], rdx - | movaps CCSTATE->fpr[1], xmm1 - |.endif - | - | mov rbx, [rbp-8]; leave; ret - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |// Note: aligning all instructions does not pay off. - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - |.macro jmp_comp, lt, ge, le, gt, target - ||switch (op) { - ||case BC_ISLT: - | lt target - ||break; - ||case BC_ISGE: - | ge target - ||break; - ||case BC_ISLE: - | le target - ||break; - ||case BC_ISGT: - | gt target - ||break; - ||default: break; /* Shut up GCC. */ - ||} - |.endmacro - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1, RD = src2, JMP with RD = target - | ins_AD - | mov ITYPE, [BASE+RA*8] - | mov RB, [BASE+RD*8] - | mov RA, ITYPE - | mov RD, RB - | sar ITYPE, 47 - | sar RB, 47 - |.if DUALNUM - | cmp ITYPEd, LJ_TISNUM; jne >7 - | cmp RBd, LJ_TISNUM; jne >8 - | add PC, 4 - | cmp RAd, RDd - | jmp_comp jge, jl, jg, jle, >9 - |6: - | movzx RDd, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RA is not an integer. - | ja ->vmeta_comp - | // RA is a number. - | cmp RBd, LJ_TISNUM; jb >1; jne ->vmeta_comp - | // RA is a number, RD is an integer. - | cvtsi2sd xmm0, RDd - | jmp >2 - | - |8: // RA is an integer, RD is not an integer. - | ja ->vmeta_comp - | // RA is an integer, RD is a number. - | cvtsi2sd xmm1, RAd - | movd xmm0, RD - | jmp >3 - |.else - | cmp ITYPEd, LJ_TISNUM; jae ->vmeta_comp - | cmp RBd, LJ_TISNUM; jae ->vmeta_comp - |.endif - |1: - | movd xmm0, RD - |2: - | movd xmm1, RA - |3: - | add PC, 4 - | ucomisd xmm0, xmm1 - | // Unordered: all of ZF CF PF set, ordered: PF clear. - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - |.if DUALNUM - | jmp_comp jbe, ja, jb, jae, <9 - | jmp <6 - |.else - | jmp_comp jbe, ja, jb, jae, >1 - | movzx RDd, PC_RD - | branchPC RD - |1: - | ins_next - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | ins_AD // RA = src1, RD = src2, JMP with RD = target - | mov RB, [BASE+RD*8] - | mov ITYPE, [BASE+RA*8] - | add PC, 4 - | mov RD, RB - | mov RA, ITYPE - | sar RB, 47 - | sar ITYPE, 47 - |.if DUALNUM - | cmp RBd, LJ_TISNUM; jne >7 - | cmp ITYPEd, LJ_TISNUM; jne >8 - | cmp RDd, RAd - if (vk) { - | jne >9 - } else { - | je >9 - } - | movzx RDd, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RD is not an integer. - | ja >5 - | // RD is a number. - | movd xmm1, RD - | cmp ITYPEd, LJ_TISNUM; jb >1; jne >5 - | // RD is a number, RA is an integer. - | cvtsi2sd xmm0, RAd - | jmp >2 - | - |8: // RD is an integer, RA is not an integer. - | ja >5 - | // RD is an integer, RA is a number. - | cvtsi2sd xmm1, RDd - | jmp >1 - | - |.else - | cmp RBd, LJ_TISNUM; jae >5 - | cmp ITYPEd, LJ_TISNUM; jae >5 - | movd xmm1, RD - |.endif - |1: - | movd xmm0, RA - |2: - | ucomisd xmm0, xmm1 - |4: - iseqne_fp: - if (vk) { - | jp >2 // Unordered means not equal. - | jne >2 - } else { - | jp >2 // Unordered means not equal. - | je >1 - } - iseqne_end: - if (vk) { - |1: // EQ: Branch to the target. - | movzx RDd, PC_RD - | branchPC RD - |2: // NE: Fallthrough to next instruction. - |.if not FFI - |3: - |.endif - } else { - |.if not FFI - |3: - |.endif - |2: // NE: Branch to the target. - | movzx RDd, PC_RD - | branchPC RD - |1: // EQ: Fallthrough to next instruction. - } - if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV || - op == BC_ISEQN || op == BC_ISNEN)) { - | jmp <9 - } else { - | ins_next - } - | - if (op == BC_ISEQV || op == BC_ISNEV) { - |5: // Either or both types are not numbers. - |.if FFI - | cmp RBd, LJ_TCDATA; je ->vmeta_equal_cd - | cmp ITYPEd, LJ_TCDATA; je ->vmeta_equal_cd - |.endif - | cmp RA, RD - | je <1 // Same GCobjs or pvalues? - | cmp RBd, ITYPEd - | jne <2 // Not the same type? - | cmp RBd, LJ_TISTABUD - | ja <2 // Different objects and not table/ud? - | - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | cleartp TAB:RA - | mov TAB:RB, TAB:RA->metatable - | test TAB:RB, TAB:RB - | jz <2 // No metatable? - | test byte TAB:RB->nomm, 1<vmeta_equal // Handle __eq metamethod. - } else { - |.if FFI - |3: - | cmp ITYPEd, LJ_TCDATA - if (LJ_DUALNUM && vk) { - | jne <9 - } else { - | jne <2 - } - | jmp ->vmeta_equal_cd - |.endif - } - break; - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | ins_AND // RA = src, RD = str const, JMP with RD = target - | mov RB, [BASE+RA*8] - | add PC, 4 - | checkstr RB, >3 - | cmp RB, [KBASE+RD*8] - iseqne_test: - if (vk) { - | jne >2 - } else { - | je >1 - } - goto iseqne_end; - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | ins_AD // RA = src, RD = num const, JMP with RD = target - | mov RB, [BASE+RA*8] - | add PC, 4 - |.if DUALNUM - | checkint RB, >7 - | mov RD, [KBASE+RD*8] - | checkint RD, >8 - | cmp RBd, RDd - if (vk) { - | jne >9 - } else { - | je >9 - } - | movzx RDd, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RA is not an integer. - | ja >3 - | // RA is a number. - | mov RD, [KBASE+RD*8] - | checkint RD, >1 - | // RA is a number, RD is an integer. - | cvtsi2sd xmm0, RDd - | jmp >2 - | - |8: // RA is an integer, RD is a number. - | cvtsi2sd xmm0, RBd - | movd xmm1, RD - | ucomisd xmm0, xmm1 - | jmp >4 - |1: - | movd xmm0, RD - |.else - | checknum RB, >3 - |1: - | movsd xmm0, qword [KBASE+RD*8] - |.endif - |2: - | ucomisd xmm0, qword [BASE+RA*8] - |4: - goto iseqne_fp; - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target - | mov RB, [BASE+RA*8] - | sar RB, 47 - | add PC, 4 - | cmp RBd, RDd - if (!LJ_HASFFI) goto iseqne_test; - if (vk) { - | jne >3 - | movzx RDd, PC_RD - | branchPC RD - |2: - | ins_next - |3: - | cmp RBd, LJ_TCDATA; jne <2 - | jmp ->vmeta_equal_cd - } else { - | je >2 - | cmp RBd, LJ_TCDATA; je ->vmeta_equal_cd - | movzx RDd, PC_RD - | branchPC RD - |2: - | ins_next - } - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | ins_AD // RA = dst or unused, RD = src, JMP with RD = target - | mov ITYPE, [BASE+RD*8] - | add PC, 4 - if (op == BC_ISTC || op == BC_ISFC) { - | mov RB, ITYPE - } - | sar ITYPE, 47 - | cmp ITYPEd, LJ_TISTRUECOND - if (op == BC_IST || op == BC_ISTC) { - | jae >1 - } else { - | jb >1 - } - if (op == BC_ISTC || op == BC_ISFC) { - | mov [BASE+RA*8], RB - } - | movzx RDd, PC_RD - | branchPC RD - |1: // Fallthrough to the next instruction. - | ins_next - break; - - case BC_ISTYPE: - | ins_AD // RA = src, RD = -type - | mov RB, [BASE+RA*8] - | sar RB, 47 - | add RBd, RDd - | jne ->vmeta_istype - | ins_next - break; - case BC_ISNUM: - | ins_AD // RA = src, RD = -(TISNUM-1) - | checknumtp [BASE+RA*8], ->vmeta_istype - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | ins_AD // RA = dst, RD = src - | mov RB, [BASE+RD*8] - | mov [BASE+RA*8], RB - | ins_next_ - break; - case BC_NOT: - | ins_AD // RA = dst, RD = src - | mov RB, [BASE+RD*8] - | sar RB, 47 - | mov RCd, 2 - | cmp RB, LJ_TISTRUECOND - | sbb RCd, 0 - | shl RC, 47 - | not RC - | mov [BASE+RA*8], RC - | ins_next - break; - case BC_UNM: - | ins_AD // RA = dst, RD = src - | mov RB, [BASE+RD*8] - |.if DUALNUM - | checkint RB, >5 - | neg RBd - | jo >4 - | setint RB - |9: - | mov [BASE+RA*8], RB - | ins_next - |4: - | mov64 RB, U64x(41e00000,00000000) // 2^31. - | jmp <9 - |5: - | ja ->vmeta_unm - |.else - | checknum RB, ->vmeta_unm - |.endif - | mov64 RD, U64x(80000000,00000000) - | xor RB, RD - |.if DUALNUM - | jmp <9 - |.else - | mov [BASE+RA*8], RB - | ins_next - |.endif - break; - case BC_LEN: - | ins_AD // RA = dst, RD = src - | mov RD, [BASE+RD*8] - | checkstr RD, >2 - |.if DUALNUM - | mov RDd, dword STR:RD->len - |1: - | setint RD - | mov [BASE+RA*8], RD - |.else - | xorps xmm0, xmm0 - | cvtsi2sd xmm0, dword STR:RD->len - |1: - | movsd qword [BASE+RA*8], xmm0 - |.endif - | ins_next - |2: - | cmp ITYPEd, LJ_TTAB; jne ->vmeta_len - | mov TAB:CARG1, TAB:RD -#if LJ_52 - | mov TAB:RB, TAB:RD->metatable - | cmp TAB:RB, 0 - | jnz >9 - |3: -#endif - |->BC_LEN_Z: - | mov RB, BASE // Save BASE. - | call extern lj_tab_len // (GCtab *t) - | // Length of table returned in eax (RD). - |.if DUALNUM - | // Nothing to do. - |.else - | cvtsi2sd xmm0, RDd - |.endif - | mov BASE, RB // Restore BASE. - | movzx RAd, PC_RA - | jmp <1 -#if LJ_52 - |9: // Check for __len. - | test byte TAB:RB->nomm, 1<vmeta_len // 'no __len' flag NOT set: check. -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre, sseins, ssereg - | ins_ABC - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | checknumtp [BASE+RB*8], ->vmeta_arith_vn - | .if DUALNUM - | checknumtp [KBASE+RC*8], ->vmeta_arith_vn - | .endif - | movsd xmm0, qword [BASE+RB*8] - | sseins ssereg, qword [KBASE+RC*8] - || break; - ||case 1: - | checknumtp [BASE+RB*8], ->vmeta_arith_nv - | .if DUALNUM - | checknumtp [KBASE+RC*8], ->vmeta_arith_nv - | .endif - | movsd xmm0, qword [KBASE+RC*8] - | sseins ssereg, qword [BASE+RB*8] - || break; - ||default: - | checknumtp [BASE+RB*8], ->vmeta_arith_vv - | checknumtp [BASE+RC*8], ->vmeta_arith_vv - | movsd xmm0, qword [BASE+RB*8] - | sseins ssereg, qword [BASE+RC*8] - || break; - ||} - |.endmacro - | - |.macro ins_arithdn, intins - | ins_ABC - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | mov RB, [BASE+RB*8] - | mov RC, [KBASE+RC*8] - | checkint RB, ->vmeta_arith_vno - | checkint RC, ->vmeta_arith_vno - | intins RBd, RCd; jo ->vmeta_arith_vno - || break; - ||case 1: - | mov RB, [BASE+RB*8] - | mov RC, [KBASE+RC*8] - | checkint RB, ->vmeta_arith_nvo - | checkint RC, ->vmeta_arith_nvo - | intins RCd, RBd; jo ->vmeta_arith_nvo - || break; - ||default: - | mov RB, [BASE+RB*8] - | mov RC, [BASE+RC*8] - | checkint RB, ->vmeta_arith_vvo - | checkint RC, ->vmeta_arith_vvo - | intins RBd, RCd; jo ->vmeta_arith_vvo - || break; - ||} - ||if (vk == 1) { - | setint RC - | mov [BASE+RA*8], RC - ||} else { - | setint RB - | mov [BASE+RA*8], RB - ||} - | ins_next - |.endmacro - | - |.macro ins_arithpost - | movsd qword [BASE+RA*8], xmm0 - |.endmacro - | - |.macro ins_arith, sseins - | ins_arithpre sseins, xmm0 - | ins_arithpost - | ins_next - |.endmacro - | - |.macro ins_arith, intins, sseins - |.if DUALNUM - | ins_arithdn intins - |.else - | ins_arith, sseins - |.endif - |.endmacro - - | // RA = dst, RB = src1 or num const, RC = src2 or num const - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith add, addsd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith sub, subsd - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith imul, mulsd - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arith divsd - break; - case BC_MODVN: - | ins_arithpre movsd, xmm1 - |->BC_MODVN_Z: - | call ->vm_mod - | ins_arithpost - | ins_next - break; - case BC_MODNV: case BC_MODVV: - | ins_arithpre movsd, xmm1 - | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - break; - case BC_POW: - | ins_arithpre movsd, xmm1 - | mov RB, BASE - | call extern pow - | movzx RAd, PC_RA - | mov BASE, RB - | ins_arithpost - | ins_next - break; - - case BC_CAT: - | ins_ABC // RA = dst, RB = src_start, RC = src_end - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE - | lea CARG2, [BASE+RC*8] - | mov CARG3d, RCd - | sub CARG3d, RBd - |->BC_CAT_Z: - | mov L:RB, L:CARG1 - | mov SAVE_PC, PC - | call extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // NULL (finished) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jnz ->vmeta_binop - | movzx RBd, PC_RB // Copy result to Stk[RA] from Stk[RB]. - | movzx RAd, PC_RA - | mov RC, [BASE+RB*8] - | mov [BASE+RA*8], RC - | ins_next - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | ins_AND // RA = dst, RD = str const (~) - | mov RD, [KBASE+RD*8] - | settp RD, LJ_TSTR - | mov [BASE+RA*8], RD - | ins_next - break; - case BC_KCDATA: - |.if FFI - | ins_AND // RA = dst, RD = cdata const (~) - | mov RD, [KBASE+RD*8] - | settp RD, LJ_TCDATA - | mov [BASE+RA*8], RD - | ins_next - |.endif - break; - case BC_KSHORT: - | ins_AD // RA = dst, RD = signed int16 literal - |.if DUALNUM - | movsx RDd, RDW - | setint RD - | mov [BASE+RA*8], RD - |.else - | movsx RDd, RDW // Sign-extend literal. - | cvtsi2sd xmm0, RDd - | movsd qword [BASE+RA*8], xmm0 - |.endif - | ins_next - break; - case BC_KNUM: - | ins_AD // RA = dst, RD = num const - | movsd xmm0, qword [KBASE+RD*8] - | movsd qword [BASE+RA*8], xmm0 - | ins_next - break; - case BC_KPRI: - | ins_AD // RA = dst, RD = primitive type (~) - | shl RD, 47 - | not RD - | mov [BASE+RA*8], RD - | ins_next - break; - case BC_KNIL: - | ins_AD // RA = dst_start, RD = dst_end - | lea RA, [BASE+RA*8+8] - | lea RD, [BASE+RD*8] - | mov RB, LJ_TNIL - | mov [RA-8], RB // Sets minimum 2 slots. - |1: - | mov [RA], RB - | add RA, 8 - | cmp RA, RD - | jbe <1 - | ins_next - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | ins_AD // RA = dst, RD = upvalue # - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | mov UPVAL:RB, [LFUNC:RB+RD*8+offsetof(GCfuncL, uvptr)] - | mov RB, UPVAL:RB->v - | mov RD, [RB] - | mov [BASE+RA*8], RD - | ins_next - break; - case BC_USETV: -#define TV2MARKOFS \ - ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)) - | ins_AD // RA = upvalue #, RD = src - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)] - | cmp byte UPVAL:RB->closed, 0 - | mov RB, UPVAL:RB->v - | mov RA, [BASE+RD*8] - | mov [RB], RA - | jz >1 - | // Check barrier for closed upvalue. - | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv) - | jnz >2 - |1: - | ins_next - | - |2: // Upvalue is black. Check if new value is collectable and white. - | mov RD, RA - | sar RD, 47 - | sub RDd, LJ_TISGCV - | cmp RDd, LJ_TNUMX - LJ_TISGCV // tvisgcv(v) - | jbe <1 - | cleartp GCOBJ:RA - | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v) - | jz <1 - | // Crossed a write barrier. Move the barrier forward. - |.if not X64WIN - | mov CARG2, RB - | mov RB, BASE // Save BASE. - |.else - | xchg CARG2, RB // Save BASE (CARG2 == BASE). - |.endif - | lea GL:CARG1, [DISPATCH+GG_DISP2G] - | call extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | mov BASE, RB // Restore BASE. - | jmp <1 - break; -#undef TV2MARKOFS - case BC_USETS: - | ins_AND // RA = upvalue #, RD = str const (~) - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)] - | mov STR:RA, [KBASE+RD*8] - | mov RD, UPVAL:RB->v - | settp STR:ITYPE, STR:RA, LJ_TSTR - | mov [RD], STR:ITYPE - | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv) - | jnz >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str) - | jz <1 - | cmp byte UPVAL:RB->closed, 0 - | jz <1 - | // Crossed a write barrier. Move the barrier forward. - | mov RB, BASE // Save BASE (CARG2 == BASE). - | mov CARG2, RD - | lea GL:CARG1, [DISPATCH+GG_DISP2G] - | call extern lj_gc_barrieruv // (global_State *g, TValue *tv) - | mov BASE, RB // Restore BASE. - | jmp <1 - break; - case BC_USETN: - | ins_AD // RA = upvalue #, RD = num const - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | movsd xmm0, qword [KBASE+RD*8] - | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)] - | mov RA, UPVAL:RB->v - | movsd qword [RA], xmm0 - | ins_next - break; - case BC_USETP: - | ins_AD // RA = upvalue #, RD = primitive type (~) - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | mov UPVAL:RB, [LFUNC:RB+RA*8+offsetof(GCfuncL, uvptr)] - | shl RD, 47 - | not RD - | mov RA, UPVAL:RB->v - | mov [RA], RD - | ins_next - break; - case BC_UCLO: - | ins_AD // RA = level, RD = target - | branchPC RD // Do this first to free RD. - | mov L:RB, SAVE_L - | cmp aword L:RB->openupval, 0 - | je >1 - | mov L:RB->base, BASE - | lea CARG2, [BASE+RA*8] // Caveat: CARG2 == BASE - | mov L:CARG1, L:RB // Caveat: CARG1 == RA - | call extern lj_func_closeuv // (lua_State *L, TValue *level) - | mov BASE, L:RB->base - |1: - | ins_next - break; - - case BC_FNEW: - | ins_AND // RA = dst, RD = proto const (~) (holding function prototype) - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2/CARG3 may be BASE. - | mov CARG3, [BASE-16] - | cleartp CARG3 - | mov CARG2, [KBASE+RD*8] // Fetch GCproto *. - | mov CARG1, L:RB - | mov SAVE_PC, PC - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | call extern lj_func_newL_gc - | // GCfuncL * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RAd, PC_RA - | settp LFUNC:RC, LJ_TFUNC - | mov [BASE+RA*8], LFUNC:RC - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - | ins_AD // RA = dst, RD = hbits|asize - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov RA, [DISPATCH+DISPATCH_GL(gc.total)] - | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)] - | mov SAVE_PC, PC - | jae >5 - |1: - | mov CARG3d, RDd - | and RDd, 0x7ff - | shr CARG3d, 11 - | cmp RDd, 0x7ff - | je >3 - |2: - | mov L:CARG1, L:RB - | mov CARG2d, RDd - | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Table * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RAd, PC_RA - | settp TAB:RC, LJ_TTAB - | mov [BASE+RA*8], TAB:RC - | ins_next - |3: // Turn 0x7ff into 0x801. - | mov RDd, 0x801 - | jmp <2 - |5: - | mov L:CARG1, L:RB - | call extern lj_gc_step_fixtop // (lua_State *L) - | movzx RDd, PC_RD - | jmp <1 - break; - case BC_TDUP: - | ins_AND // RA = dst, RD = table const (~) (holding template table) - | mov L:RB, SAVE_L - | mov RA, [DISPATCH+DISPATCH_GL(gc.total)] - | mov SAVE_PC, PC - | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)] - | mov L:RB->base, BASE - | jae >3 - |2: - | mov TAB:CARG2, [KBASE+RD*8] // Caveat: CARG2 == BASE - | mov L:CARG1, L:RB // Caveat: CARG1 == RA - | call extern lj_tab_dup // (lua_State *L, Table *kt) - | // Table * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RAd, PC_RA - | settp TAB:RC, LJ_TTAB - | mov [BASE+RA*8], TAB:RC - | ins_next - |3: - | mov L:CARG1, L:RB - | call extern lj_gc_step_fixtop // (lua_State *L) - | movzx RDd, PC_RD // Need to reload RD. - | not RD - | jmp <2 - break; - - case BC_GGET: - | ins_AND // RA = dst, RD = str const (~) - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | mov TAB:RB, LFUNC:RB->env - | mov STR:RC, [KBASE+RD*8] - | jmp ->BC_TGETS_Z - break; - case BC_GSET: - | ins_AND // RA = src, RD = str const (~) - | mov LFUNC:RB, [BASE-16] - | cleartp LFUNC:RB - | mov TAB:RB, LFUNC:RB->env - | mov STR:RC, [KBASE+RD*8] - | jmp ->BC_TSETS_Z - break; - - case BC_TGETV: - | ins_ABC // RA = dst, RB = table, RC = key - | mov TAB:RB, [BASE+RB*8] - | mov RC, [BASE+RC*8] - | checktab TAB:RB, ->vmeta_tgetv - | - | // Integer key? - |.if DUALNUM - | checkint RC, >5 - |.else - | // Convert number to int and back and compare. - | checknum RC, >5 - | movd xmm0, RC - | cvttsd2si RCd, xmm0 - | cvtsi2sd xmm1, RCd - | ucomisd xmm0, xmm1 - | jne ->vmeta_tgetv // Generic numeric key? Use fallback. - |.endif - | cmp RCd, TAB:RB->asize // Takes care of unordered, too. - | jae ->vmeta_tgetv // Not in array part? Use fallback. - | shl RCd, 3 - | add RC, TAB:RB->array - | // Get array slot. - | mov ITYPE, [RC] - | cmp ITYPE, LJ_TNIL // Avoid overwriting RB in fastpath. - | je >2 - |1: - | mov [BASE+RA*8], ITYPE - | ins_next - | - |2: // Check for __index if table value is nil. - | mov TAB:TMPR, TAB:RB->metatable - | test TAB:TMPR, TAB:TMPR - | jz <1 - | test byte TAB:TMPR->nomm, 1<vmeta_tgetv // 'no __index' flag NOT set: check. - | jmp <1 - | - |5: // String key? - | cmp ITYPEd, LJ_TSTR; jne ->vmeta_tgetv - | cleartp STR:RC - | jmp ->BC_TGETS_Z - break; - case BC_TGETS: - | ins_ABC // RA = dst, RB = table, RC = str const (~) - | mov TAB:RB, [BASE+RB*8] - | not RC - | mov STR:RC, [KBASE+RC*8] - | checktab TAB:RB, ->vmeta_tgets - |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr * - | mov TMPRd, TAB:RB->hmask - | and TMPRd, STR:RC->sid - | imul TMPRd, #NODE - | add NODE:TMPR, TAB:RB->node - | settp ITYPE, STR:RC, LJ_TSTR - |1: - | cmp NODE:TMPR->key, ITYPE - | jne >4 - | // Get node value. - | mov ITYPE, NODE:TMPR->val - | cmp ITYPE, LJ_TNIL - | je >5 // Key found, but nil value? - |2: - | mov [BASE+RA*8], ITYPE - | ins_next - | - |4: // Follow hash chain. - | mov NODE:TMPR, NODE:TMPR->next - | test NODE:TMPR, NODE:TMPR - | jnz <1 - | // End of hash chain: key not found, nil result. - | mov ITYPE, LJ_TNIL - | - |5: // Check for __index if table value is nil. - | mov TAB:TMPR, TAB:RB->metatable - | test TAB:TMPR, TAB:TMPR - | jz <2 // No metatable: done. - | test byte TAB:TMPR->nomm, 1<vmeta_tgets // Caveat: preserve STR:RC. - break; - case BC_TGETB: - | ins_ABC // RA = dst, RB = table, RC = byte literal - | mov TAB:RB, [BASE+RB*8] - | checktab TAB:RB, ->vmeta_tgetb - | cmp RCd, TAB:RB->asize - | jae ->vmeta_tgetb - | shl RCd, 3 - | add RC, TAB:RB->array - | // Get array slot. - | mov ITYPE, [RC] - | cmp ITYPE, LJ_TNIL - | je >2 - |1: - | mov [BASE+RA*8], ITYPE - | ins_next - | - |2: // Check for __index if table value is nil. - | mov TAB:TMPR, TAB:RB->metatable - | test TAB:TMPR, TAB:TMPR - | jz <1 - | test byte TAB:TMPR->nomm, 1<vmeta_tgetb // 'no __index' flag NOT set: check. - | jmp <1 - break; - case BC_TGETR: - | ins_ABC // RA = dst, RB = table, RC = key - | mov TAB:RB, [BASE+RB*8] - | cleartp TAB:RB - |.if DUALNUM - | mov RCd, dword [BASE+RC*8] - |.else - | cvttsd2si RCd, qword [BASE+RC*8] - |.endif - | cmp RCd, TAB:RB->asize - | jae ->vmeta_tgetr // Not in array part? Use fallback. - | shl RCd, 3 - | add RC, TAB:RB->array - | // Get array slot. - |->BC_TGETR_Z: - | mov ITYPE, [RC] - |->BC_TGETR2_Z: - | mov [BASE+RA*8], ITYPE - | ins_next - break; - - case BC_TSETV: - | ins_ABC // RA = src, RB = table, RC = key - | mov TAB:RB, [BASE+RB*8] - | mov RC, [BASE+RC*8] - | checktab TAB:RB, ->vmeta_tsetv - | - | // Integer key? - |.if DUALNUM - | checkint RC, >5 - |.else - | // Convert number to int and back and compare. - | checknum RC, >5 - | movd xmm0, RC - | cvttsd2si RCd, xmm0 - | cvtsi2sd xmm1, RCd - | ucomisd xmm0, xmm1 - | jne ->vmeta_tsetv // Generic numeric key? Use fallback. - |.endif - | cmp RCd, TAB:RB->asize // Takes care of unordered, too. - | jae ->vmeta_tsetv - | shl RCd, 3 - | add RC, TAB:RB->array - | cmp aword [RC], LJ_TNIL - | je >3 // Previous value is nil? - |1: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: // Set array slot. - | mov RB, [BASE+RA*8] - | mov [RC], RB - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | mov TAB:TMPR, TAB:RB->metatable - | test TAB:TMPR, TAB:TMPR - | jz <1 - | test byte TAB:TMPR->nomm, 1<vmeta_tsetv // 'no __newindex' flag NOT set: check. - | jmp <1 - | - |5: // String key? - | cmp ITYPEd, LJ_TSTR; jne ->vmeta_tsetv - | cleartp STR:RC - | jmp ->BC_TSETS_Z - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMPR - | jmp <2 - break; - case BC_TSETS: - | ins_ABC // RA = src, RB = table, RC = str const (~) - | mov TAB:RB, [BASE+RB*8] - | not RC - | mov STR:RC, [KBASE+RC*8] - | checktab TAB:RB, ->vmeta_tsets - |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr * - | mov TMPRd, TAB:RB->hmask - | and TMPRd, STR:RC->sid - | imul TMPRd, #NODE - | mov byte TAB:RB->nomm, 0 // Clear metamethod cache. - | add NODE:TMPR, TAB:RB->node - | settp ITYPE, STR:RC, LJ_TSTR - |1: - | cmp NODE:TMPR->key, ITYPE - | jne >5 - | // Ok, key found. Assumes: offsetof(Node, val) == 0 - | cmp aword [TMPR], LJ_TNIL - | je >4 // Previous value is nil? - |2: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |3: // Set node value. - | mov ITYPE, [BASE+RA*8] - | mov [TMPR], ITYPE - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | mov TAB:ITYPE, TAB:RB->metatable - | test TAB:ITYPE, TAB:ITYPE - | jz <2 - | test byte TAB:ITYPE->nomm, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - | jmp <2 - | - |5: // Follow hash chain. - | mov NODE:TMPR, NODE:TMPR->next - | test NODE:TMPR, NODE:TMPR - | jnz <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | mov TAB:TMPR, TAB:RB->metatable - | test TAB:TMPR, TAB:TMPR - | jz >6 // No metatable: continue. - | test byte TAB:TMPR->nomm, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | mov TMP1, ITYPE - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE - | lea CARG3, TMP1 - | mov CARG2, TAB:RB - | mov SAVE_PC, PC - | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Handles write barrier for the new key. TValue * returned in eax (RC). - | mov L:CARG1, SAVE_L - | mov BASE, L:CARG1->base - | mov TMPR, rax - | movzx RAd, PC_RA - | jmp <2 // Must check write barrier for value. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, ITYPE - | jmp <3 - break; - case BC_TSETB: - | ins_ABC // RA = src, RB = table, RC = byte literal - | mov TAB:RB, [BASE+RB*8] - | checktab TAB:RB, ->vmeta_tsetb - | cmp RCd, TAB:RB->asize - | jae ->vmeta_tsetb - | shl RCd, 3 - | add RC, TAB:RB->array - | cmp aword [RC], LJ_TNIL - | je >3 // Previous value is nil? - |1: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: // Set array slot. - | mov ITYPE, [BASE+RA*8] - | mov [RC], ITYPE - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | mov TAB:TMPR, TAB:RB->metatable - | test TAB:TMPR, TAB:TMPR - | jz <1 - | test byte TAB:TMPR->nomm, 1<vmeta_tsetb // 'no __newindex' flag NOT set: check. - | jmp <1 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMPR - | jmp <2 - break; - case BC_TSETR: - | ins_ABC // RA = src, RB = table, RC = key - | mov TAB:RB, [BASE+RB*8] - | cleartp TAB:RB - |.if DUALNUM - | mov RC, [BASE+RC*8] - |.else - | cvttsd2si RCd, qword [BASE+RC*8] - |.endif - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: - | cmp RCd, TAB:RB->asize - | jae ->vmeta_tsetr - | shl RCd, 3 - | add RC, TAB:RB->array - | // Set array slot. - |->BC_TSETR_Z: - | mov ITYPE, [BASE+RA*8] - | mov [RC], ITYPE - | ins_next - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, TMPR - | jmp <2 - break; - - case BC_TSETM: - | ins_AD // RA = base (table at base-1), RD = num const (start index) - |1: - | mov TMPRd, dword [KBASE+RD*8] // Integer constant is in lo-word. - | lea RA, [BASE+RA*8] - | mov TAB:RB, [RA-8] // Guaranteed to be a table. - | cleartp TAB:RB - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: - | mov RDd, MULTRES - | sub RDd, 1 - | jz >4 // Nothing to copy? - | add RDd, TMPRd // Compute needed size. - | cmp RDd, TAB:RB->asize - | ja >5 // Doesn't fit into array part? - | sub RDd, TMPRd - | shl TMPRd, 3 - | add TMPR, TAB:RB->array - |3: // Copy result slots to table. - | mov RB, [RA] - | add RA, 8 - | mov [TMPR], RB - | add TMPR, 8 - | sub RDd, 1 - | jnz <3 - |4: - | ins_next - | - |5: // Need to resize array part. - | mov L:CARG1, SAVE_L - | mov L:CARG1->base, BASE // Caveat: CARG2/CARG3 may be BASE. - | mov CARG2, TAB:RB - | mov CARG3d, RDd - | mov L:RB, L:CARG1 - | mov SAVE_PC, PC - | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | mov BASE, L:RB->base - | movzx RAd, PC_RA // Restore RA. - | movzx RDd, PC_RD // Restore RD. - | jmp <1 // Retry. - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:RB, RD - | jmp <2 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALL: case BC_CALLM: - | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs - if (op == BC_CALLM) { - | add NARGS:RDd, MULTRES - } - | mov LFUNC:RB, [BASE+RA*8] - | checkfunc LFUNC:RB, ->vmeta_call_ra - | lea BASE, [BASE+RA*8+16] - | ins_call - break; - - case BC_CALLMT: - | ins_AD // RA = base, RD = extra_nargs - | add NARGS:RDd, MULTRES - | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op. - break; - case BC_CALLT: - | ins_AD // RA = base, RD = nargs+1 - | lea RA, [BASE+RA*8+16] - | mov KBASE, BASE // Use KBASE for move + vmeta_call hint. - | mov LFUNC:RB, [RA-16] - | checktp_nc LFUNC:RB, LJ_TFUNC, ->vmeta_call - |->BC_CALLT_Z: - | mov PC, [BASE-8] - | test PCd, FRAME_TYPE - | jnz >7 - |1: - | mov [BASE-16], LFUNC:RB // Copy func+tag down, reloaded below. - | mov MULTRES, NARGS:RDd - | sub NARGS:RDd, 1 - | jz >3 - |2: // Move args down. - | mov RB, [RA] - | add RA, 8 - | mov [KBASE], RB - | add KBASE, 8 - | sub NARGS:RDd, 1 - | jnz <2 - | - | mov LFUNC:RB, [BASE-16] - |3: - | cleartp LFUNC:RB - | mov NARGS:RDd, MULTRES - | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function? - | ja >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function. - | test PCd, FRAME_TYPE // Lua frame below? - | jnz <4 - | movzx RAd, PC_RA - | neg RA - | mov LFUNC:KBASE, [BASE+RA*8-32] // Need to prepare KBASE. - | cleartp LFUNC:KBASE - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | jmp <4 - | - |7: // Tailcall from a vararg function. - | sub PC, FRAME_VARG - | test PCd, FRAME_TYPEP - | jnz >8 // Vararg frame below? - | sub BASE, PC // Need to relocate BASE/KBASE down. - | mov KBASE, BASE - | mov PC, [BASE-8] - | jmp <1 - |8: - | add PCd, FRAME_VARG - | jmp <1 - break; - - case BC_ITERC: - | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1) - | lea RA, [BASE+RA*8+16] // fb = base+2 - | mov RB, [RA-32] // Copy state. fb[0] = fb[-4]. - | mov RC, [RA-24] // Copy control var. fb[1] = fb[-3]. - | mov [RA], RB - | mov [RA+8], RC - | mov LFUNC:RB, [RA-40] // Copy callable. fb[-2] = fb[-5] - | mov [RA-16], LFUNC:RB - | mov NARGS:RDd, 2+1 // Handle like a regular 2-arg call. - | checkfunc LFUNC:RB, ->vmeta_call - | mov BASE, RA - | ins_call - break; - - case BC_ITERN: - |.if JIT - | hotloop RBd - |.endif - |->vm_IITERN: - | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1)) - | mov TAB:RB, [BASE+RA*8-16] - | cleartp TAB:RB - | mov RCd, [BASE+RA*8-8] // Get index from control var. - | mov TMPRd, TAB:RB->asize - | add PC, 4 - | mov ITYPE, TAB:RB->array - |1: // Traverse array part. - | cmp RCd, TMPRd; jae >5 // Index points after array part? - | cmp aword [ITYPE+RC*8], LJ_TNIL; je >4 - |.if not DUALNUM - | cvtsi2sd xmm0, RCd - |.endif - | // Copy array slot to returned value. - | mov RB, [ITYPE+RC*8] - | mov [BASE+RA*8+8], RB - | // Return array index as a numeric key. - |.if DUALNUM - | setint ITYPE, RC - | mov [BASE+RA*8], ITYPE - |.else - | movsd qword [BASE+RA*8], xmm0 - |.endif - | add RCd, 1 - | mov [BASE+RA*8-8], RCd // Update control var. - |2: - | movzx RDd, PC_RD // Get target from ITERL. - | branchPC RD - |3: - | ins_next - | - |4: // Skip holes in array part. - | add RCd, 1 - | jmp <1 - | - |5: // Traverse hash part. - | sub RCd, TMPRd - |6: - | cmp RCd, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1. - | imul ITYPEd, RCd, #NODE - | add NODE:ITYPE, TAB:RB->node - | cmp aword NODE:ITYPE->val, LJ_TNIL; je >7 - | lea TMPRd, [RCd+TMPRd+1] - | // Copy key and value from hash slot. - | mov RB, NODE:ITYPE->key - | mov RC, NODE:ITYPE->val - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+8], RC - | mov [BASE+RA*8-8], TMPRd - | jmp <2 - | - |7: // Skip holes in hash part. - | add RCd, 1 - | jmp <6 - break; - - case BC_ISNEXT: - | ins_AD // RA = base, RD = target (points to ITERN) - | mov CFUNC:RB, [BASE+RA*8-24] - | checkfunc CFUNC:RB, >5 - | checktptp [BASE+RA*8-16], LJ_TTAB, >5 - | cmp aword [BASE+RA*8-8], LJ_TNIL; jne >5 - | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5 - | branchPC RD - | mov64 TMPR, ((uint64_t)LJ_KEYINDEX << 32) - | mov [BASE+RA*8-8], TMPR // Initialize control var. - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | mov PC_OP, BC_JMP - | branchPC RD - |.if JIT - | cmp byte [PC], BC_ITERN - | jne >6 - |.endif - | mov byte [PC], BC_ITERC - | jmp <1 - |.if JIT - |6: // Unpatch JLOOP. - | mov RA, [DISPATCH+DISPATCH_J(trace)] - | movzx RCd, word [PC+2] - | mov TRACE:RA, [RA+RC*8] - | mov eax, TRACE:RA->startins - | mov al, BC_ITERC - | mov dword [PC], eax - | jmp <1 - |.endif - break; - - case BC_VARG: - | ins_ABC // RA = base, RB = nresults+1, RC = numparams - | lea TMPR, [BASE+RC*8+(16+FRAME_VARG)] - | lea RA, [BASE+RA*8] - | sub TMPR, [BASE-8] - | // Note: TMPR may now be even _above_ BASE if nargs was < numparams. - | test RB, RB - | jz >5 // Copy all varargs? - | lea RB, [RA+RB*8-8] - | cmp TMPR, BASE // No vararg slots? - | jnb >2 - |1: // Copy vararg slots to destination slots. - | mov RC, [TMPR-16] - | add TMPR, 8 - | mov [RA], RC - | add RA, 8 - | cmp RA, RB // All destination slots filled? - | jnb >3 - | cmp TMPR, BASE // No more vararg slots? - | jb <1 - |2: // Fill up remainder with nil. - | mov aword [RA], LJ_TNIL - | add RA, 8 - | cmp RA, RB - | jb <2 - |3: - | ins_next - | - |5: // Copy all varargs. - | mov MULTRES, 1 // MULTRES = 0+1 - | mov RC, BASE - | sub RC, TMPR - | jbe <3 // No vararg slots? - | mov RBd, RCd - | shr RBd, 3 - | add RBd, 1 - | mov MULTRES, RBd // MULTRES = #varargs+1 - | mov L:RB, SAVE_L - | add RC, RA - | cmp RC, L:RB->maxstack - | ja >7 // Need to grow stack? - |6: // Copy all vararg slots. - | mov RC, [TMPR-16] - | add TMPR, 8 - | mov [RA], RC - | add RA, 8 - | cmp TMPR, BASE // No more vararg slots? - | jb <6 - | jmp <3 - | - |7: // Grow stack for varargs. - | mov L:RB->base, BASE - | mov L:RB->top, RA - | mov SAVE_PC, PC - | sub TMPR, BASE // Need delta, because BASE may change. - | mov TMP1hi, TMPRd - | mov CARG2d, MULTRES - | sub CARG2d, 1 - | mov CARG1, L:RB - | call extern lj_state_growstack // (lua_State *L, int n) - | mov BASE, L:RB->base - | movsxd TMPR, TMP1hi - | mov RA, L:RB->top - | add TMPR, BASE - | jmp <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | ins_AD // RA = results, RD = extra_nresults - | add RDd, MULTRES // MULTRES >=1, so RD >=1. - | // Fall through. Assumes BC_RET follows and ins_AD is a no-op. - break; - - case BC_RET: case BC_RET0: case BC_RET1: - | ins_AD // RA = results, RD = nresults+1 - if (op != BC_RET0) { - | shl RAd, 3 - } - |1: - | mov PC, [BASE-8] - | mov MULTRES, RDd // Save nresults+1. - | test PCd, FRAME_TYPE // Check frame type marker. - | jnz >7 // Not returning to a fixarg Lua func? - switch (op) { - case BC_RET: - |->BC_RET_Z: - | mov KBASE, BASE // Use KBASE for result move. - | sub RDd, 1 - | jz >3 - |2: // Move results down. - | mov RB, [KBASE+RA] - | mov [KBASE-16], RB - | add KBASE, 8 - | sub RDd, 1 - | jnz <2 - |3: - | mov RDd, MULTRES // Note: MULTRES may be >255. - | movzx RBd, PC_RB // So cannot compare with RDL! - |5: - | cmp RBd, RDd // More results expected? - | ja >6 - break; - case BC_RET1: - | mov RB, [BASE+RA] - | mov [BASE-16], RB - /* fallthrough */ - case BC_RET0: - |5: - | cmp PC_RB, RDL // More results expected? - | ja >6 - default: - break; - } - | movzx RAd, PC_RA - | neg RA - | lea BASE, [BASE+RA*8-16] // base = base - (RA+2)*8 - | mov LFUNC:KBASE, [BASE-16] - | cleartp LFUNC:KBASE - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | ins_next - | - |6: // Fill up results with nil. - if (op == BC_RET) { - | mov aword [KBASE-16], LJ_TNIL // Note: relies on shifted base. - | add KBASE, 8 - } else { - | mov aword [BASE+RD*8-24], LJ_TNIL - } - | add RD, 1 - | jmp <5 - | - |7: // Non-standard return case. - | lea RB, [PC-FRAME_VARG] - | test RBd, FRAME_TYPEP - | jnz ->vm_return - | // Return from vararg function: relocate BASE down and RA up. - | sub BASE, RB - if (op != BC_RET0) { - | add RA, RB - } - | jmp <1 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - |.define FOR_IDX, [RA] - |.define FOR_STOP, [RA+8] - |.define FOR_STEP, [RA+16] - |.define FOR_EXT, [RA+24] - - case BC_FORL: - |.if JIT - | hotloop RBd - |.endif - | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - vk = (op == BC_IFORL || op == BC_JFORL); - | ins_AJ // RA = base, RD = target (after end of loop or start of loop) - | lea RA, [BASE+RA*8] - if (LJ_DUALNUM) { - | mov RB, FOR_IDX - | checkint RB, >9 - | mov TMPR, FOR_STOP - if (!vk) { - | checkint TMPR, ->vmeta_for - | mov ITYPE, FOR_STEP - | test ITYPEd, ITYPEd; js >5 - | sar ITYPE, 47; - | cmp ITYPEd, LJ_TISNUM; jne ->vmeta_for - } else { -#ifdef LUA_USE_ASSERT - | checkinttp FOR_STOP, ->assert_bad_for_arg_type - | checkinttp FOR_STEP, ->assert_bad_for_arg_type -#endif - | mov ITYPE, FOR_STEP - | test ITYPEd, ITYPEd; js >5 - | add RBd, ITYPEd; jo >1 - | setint RB - | mov FOR_IDX, RB - } - | cmp RBd, TMPRd - | mov FOR_EXT, RB - if (op == BC_FORI) { - | jle >7 - |1: - |6: - | branchPC RD - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RDd, PC_RD - | jle =>BC_JLOOP - |1: - |6: - } else if (op == BC_IFORL) { - | jg >7 - |6: - | branchPC RD - |1: - } else { - | jle =>BC_JLOOP - |1: - |6: - } - |7: - | ins_next - | - |5: // Invert check for negative step. - if (!vk) { - | sar ITYPE, 47; - | cmp ITYPEd, LJ_TISNUM; jne ->vmeta_for - } else { - | add RBd, ITYPEd; jo <1 - | setint RB - | mov FOR_IDX, RB - } - | cmp RBd, TMPRd - | mov FOR_EXT, RB - if (op == BC_FORI) { - | jge <7 - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RDd, PC_RD - | jge =>BC_JLOOP - } else if (op == BC_IFORL) { - | jl <7 - } else { - | jge =>BC_JLOOP - } - | jmp <6 - |9: // Fallback to FP variant. - if (!vk) { - | jae ->vmeta_for - } - } else if (!vk) { - | checknumtp FOR_IDX, ->vmeta_for - } - if (!vk) { - | checknumtp FOR_STOP, ->vmeta_for - } else { -#ifdef LUA_USE_ASSERT - | checknumtp FOR_STOP, ->assert_bad_for_arg_type - | checknumtp FOR_STEP, ->assert_bad_for_arg_type -#endif - } - | mov RB, FOR_STEP - if (!vk) { - | checknum RB, ->vmeta_for - } - | movsd xmm0, qword FOR_IDX - | movsd xmm1, qword FOR_STOP - if (vk) { - | addsd xmm0, qword FOR_STEP - | movsd qword FOR_IDX, xmm0 - | test RB, RB; js >3 - } else { - | jl >3 - } - | ucomisd xmm1, xmm0 - |1: - | movsd qword FOR_EXT, xmm0 - if (op == BC_FORI) { - |.if DUALNUM - | jnb <7 - |.else - | jnb >2 - | branchPC RD - |.endif - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RDd, PC_RD - | jnb =>BC_JLOOP - } else if (op == BC_IFORL) { - |.if DUALNUM - | jb <7 - |.else - | jb >2 - | branchPC RD - |.endif - } else { - | jnb =>BC_JLOOP - } - |.if DUALNUM - | jmp <6 - |.else - |2: - | ins_next - |.endif - | - |3: // Invert comparison if step is negative. - | ucomisd xmm0, xmm1 - | jmp <1 - break; - - case BC_ITERL: - |.if JIT - | hotloop RBd - |.endif - | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | ins_AJ // RA = base, RD = target - | lea RA, [BASE+RA*8] - | mov RB, [RA] - | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | mov [RA-8], RB - | jmp =>BC_JLOOP - } else { - | branchPC RD // Otherwise save control var + branch. - | mov [RA-8], RB - } - |1: - | ins_next - break; - - case BC_LOOP: - | ins_A // RA = base, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop RBd - |.endif - | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op. - break; - - case BC_ILOOP: - | ins_A // RA = base, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | ins_AD // RA = base (ignored), RD = traceno - | mov RA, [DISPATCH+DISPATCH_J(trace)] - | mov TRACE:RD, [RA+RD*8] - | mov RD, TRACE:RD->mcode - | mov L:RB, SAVE_L - | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE - | mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB - | // Save additional callee-save registers only used in compiled code. - |.if X64WIN - | mov CSAVE_4, r12 - | mov CSAVE_3, r13 - | mov CSAVE_2, r14 - | mov CSAVE_1, r15 - | mov RA, rsp - | sub rsp, 10*16+4*8 - | movdqa [RA-1*16], xmm6 - | movdqa [RA-2*16], xmm7 - | movdqa [RA-3*16], xmm8 - | movdqa [RA-4*16], xmm9 - | movdqa [RA-5*16], xmm10 - | movdqa [RA-6*16], xmm11 - | movdqa [RA-7*16], xmm12 - | movdqa [RA-8*16], xmm13 - | movdqa [RA-9*16], xmm14 - | movdqa [RA-10*16], xmm15 - |.else - | sub rsp, 16 - | mov [rsp+16], r12 - | mov [rsp+8], r13 - |.endif - | jmp RD - |.endif - break; - - case BC_JMP: - | ins_AJ // RA = unused, RD = target - | branchPC RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - /* - ** Reminder: A function may be called with func/args above L->maxstack, - ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot, - ** too. This means all FUNC* ops (including fast functions) must check - ** for stack overflow _before_ adding more slots! - */ - - case BC_FUNCF: - |.if JIT - | hotcall RBd - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | ins_AD // BASE = new base, RA = framesize, RD = nargs+1 - | mov KBASE, [PC-4+PC2PROTO(k)] - | mov L:RB, SAVE_L - | lea RA, [BASE+RA*8] // Top of frame. - | cmp RA, L:RB->maxstack - | ja ->vm_growstack_f - | movzx RAd, byte [PC-4+PC2PROTO(numparams)] - | cmp NARGS:RDd, RAd // Check for missing parameters. - | jbe >3 - |2: - if (op == BC_JFUNCF) { - | movzx RDd, PC_RD - | jmp =>BC_JLOOP - } else { - | ins_next - } - | - |3: // Clear missing parameters. - | mov aword [BASE+NARGS:RD*8-8], LJ_TNIL - | add NARGS:RDd, 1 - | cmp NARGS:RDd, RAd - | jbe <3 - | jmp <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | int3 // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | ins_AD // BASE = new base, RA = framesize, RD = nargs+1 - | lea RBd, [NARGS:RD*8+FRAME_VARG+8] - | lea RD, [BASE+NARGS:RD*8+8] - | mov LFUNC:KBASE, [BASE-16] - | mov [RD-8], RB // Store delta + FRAME_VARG. - | mov [RD-16], LFUNC:KBASE // Store copy of LFUNC. - | mov L:RB, SAVE_L - | lea RA, [RD+RA*8] - | cmp RA, L:RB->maxstack - | ja ->vm_growstack_v // Need to grow stack. - | mov RA, BASE - | mov BASE, RD - | movzx RBd, byte [PC-4+PC2PROTO(numparams)] - | test RBd, RBd - | jz >2 - | add RA, 8 - |1: // Copy fixarg slots up to new frame. - | add RA, 8 - | cmp RA, BASE - | jnb >3 // Less args than parameters? - | mov KBASE, [RA-16] - | mov [RD], KBASE - | add RD, 8 - | mov aword [RA-16], LJ_TNIL // Clear old fixarg slot (help the GC). - | sub RBd, 1 - | jnz <1 - |2: - if (op == BC_JFUNCV) { - | movzx RDd, PC_RD - | jmp =>BC_JLOOP - } else { - | mov KBASE, [PC-4+PC2PROTO(k)] - | ins_next - } - | - |3: // Clear missing parameters. - | mov aword [RD], LJ_TNIL - | add RD, 8 - | sub RBd, 1 - | jnz <3 - | jmp <2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1 - | mov CFUNC:RB, [BASE-16] - | cleartp CFUNC:RB - | mov KBASE, CFUNC:RB->f - | mov L:RB, SAVE_L - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB->base, BASE - | lea RA, [RD+8*LUA_MINSTACK] - | cmp RA, L:RB->maxstack - | mov L:RB->top, RD - if (op == BC_FUNCC) { - | mov CARG1, L:RB // Caveat: CARG1 may be RA. - } else { - | mov CARG2, KBASE - | mov CARG1, L:RB // Caveat: CARG1 may be RA. - } - | ja ->vm_growstack_c // Need to grow stack. - | set_vmstate C - if (op == BC_FUNCC) { - | call KBASE // (lua_State *L) - } else { - | // (lua_State *L, lua_CFunction f) - | call aword [DISPATCH+DISPATCH_GL(wrapf)] - } - | // nresults returned in eax (RD). - | mov BASE, L:RB->base - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | lea RA, [BASE+RD*8] - | neg RA - | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8 - | mov PC, [BASE-8] // Fetch PC of caller. - | jmp ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - dasm_growpc(Dst, BC__MAX); - build_subroutines(ctx); - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 0x10\n" - "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n" - "\t.byte 0x80+0x10\n\t.uleb128 0x1\n" - "\t.align 8\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" - "\t.quad .Lbegin\n" - "\t.quad %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */ -#if LJ_NO_UNWIND - "\t.byte 0x8d\n\t.uleb128 0x6\n" /* offset r13 */ - "\t.byte 0x8c\n\t.uleb128 0x7\n" /* offset r12 */ -#endif - "\t.align 8\n" - ".LEFDE0:\n\n", fcofs, CFRAME_SIZE); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" - "\t.quad lj_vm_ffi_call\n" - "\t.quad %d\n" - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.align 8\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND -#if LJ_TARGET_SOLARIS - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n"); -#else - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n"); -#endif - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 0x10\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n" - "\t.byte 0x80+0x10\n\t.uleb128 0x1\n" - "\t.align 8\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.long .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.long .LASFDE2-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */ - "\t.align 8\n" - ".LEFDE2:\n\n", fcofs, CFRAME_SIZE); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.long .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -8\n" - "\t.byte 0x10\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 0x7\n\t.uleb128 8\n" - "\t.byte 0x80+0x10\n\t.uleb128 0x1\n" - "\t.align 8\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.long .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.long .LASFDE3-.Lframe2\n" - "\t.long lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.align 8\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif -#endif - break; -#if !LJ_NO_UNWIND - /* Mental note: never let Apple design an assembler. - ** Or a linker. Or a plastic case. But I digress. - */ - case BUILD_machasm: { -#if LJ_HASFFI - int fcsize = 0; -#endif - int i; - fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n"); - fprintf(ctx->fp, - "EH_frame1:\n" - "\t.set L$set$x,LECIEX-LSCIEX\n" - "\t.long L$set$x\n" - "LSCIEX:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zPR\\0\"\n" - "\t.byte 0x1\n" - "\t.byte 128-8\n" - "\t.byte 0x10\n" - "\t.byte 6\n" /* augmentation length */ - "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */ - "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte 0x7\n\t.byte 8\n" - "\t.byte 0x80+0x10\n\t.byte 0x1\n" - "\t.align 3\n" - "LECIEX:\n\n"); - for (i = 0; i < ctx->nsym; i++) { - const char *name = ctx->sym[i].name; - int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs; - if (size == 0) continue; -#if LJ_HASFFI - if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; } -#endif - fprintf(ctx->fp, - "%s.eh:\n" - "LSFDE%d:\n" - "\t.set L$set$%d,LEFDE%d-LASFDE%d\n" - "\t.long L$set$%d\n" - "LASFDE%d:\n" - "\t.long LASFDE%d-EH_frame1\n" - "\t.long %s-.\n" - "\t.long %d\n" - "\t.byte 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */ - "\t.align 3\n" - "LEFDE%d:\n\n", - name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i); - } -#if LJ_HASFFI - if (fcsize) { - fprintf(ctx->fp, - "EH_frame2:\n" - "\t.set L$set$y,LECIEY-LSCIEY\n" - "\t.long L$set$y\n" - "LSCIEY:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zR\\0\"\n" - "\t.byte 0x1\n" - "\t.byte 128-8\n" - "\t.byte 0x10\n" - "\t.byte 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte 0x7\n\t.byte 8\n" - "\t.byte 0x80+0x10\n\t.byte 0x1\n" - "\t.align 3\n" - "LECIEY:\n\n"); - fprintf(ctx->fp, - "_lj_vm_ffi_call.eh:\n" - "LSFDEY:\n" - "\t.set L$set$yy,LEFDEY-LASFDEY\n" - "\t.long L$set$yy\n" - "LASFDEY:\n" - "\t.long LASFDEY-EH_frame2\n" - "\t.long _lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.byte 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.byte 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */ - "\t.align 3\n" - "LEFDEY:\n\n", fcsize); - } -#endif - fprintf(ctx->fp, ".subsections_via_symbols\n"); - } - break; -#endif - default: /* Difficult for other modes. */ - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x86.dasc b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x86.dasc deleted file mode 100644 index 18ca87b..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/vm_x86.dasc +++ /dev/null @@ -1,5825 +0,0 @@ -|// Low-level VM code for x86 CPUs. -|// Bytecode interpreter, fast functions and helper functions. -|// Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h -| -|.if P64 -|.arch x64 -|.else -|.arch x86 -|.endif -|.section code_op, code_sub -| -|.actionlist build_actionlist -|.globals GLOB_ -|.globalnames globnames -|.externnames extnames -| -|//----------------------------------------------------------------------- -| -|.if P64 -|.define X64, 1 -|.if WIN -|.define X64WIN, 1 -|.endif -|.endif -| -|// Fixed register assignments for the interpreter. -|// This is very fragile and has many dependencies. Caveat emptor. -|.define BASE, edx // Not C callee-save, refetched anyway. -|.if not X64 -|.define KBASE, edi // Must be C callee-save. -|.define KBASEa, KBASE -|.define PC, esi // Must be C callee-save. -|.define PCa, PC -|.define DISPATCH, ebx // Must be C callee-save. -|.elif X64WIN -|.define KBASE, edi // Must be C callee-save. -|.define KBASEa, rdi -|.define PC, esi // Must be C callee-save. -|.define PCa, rsi -|.define DISPATCH, ebx // Must be C callee-save. -|.else -|.define KBASE, r15d // Must be C callee-save. -|.define KBASEa, r15 -|.define PC, ebx // Must be C callee-save. -|.define PCa, rbx -|.define DISPATCH, r14d // Must be C callee-save. -|.endif -| -|.define RA, ecx -|.define RAH, ch -|.define RAL, cl -|.define RB, ebp // Must be ebp (C callee-save). -|.define RC, eax // Must be eax. -|.define RCW, ax -|.define RCH, ah -|.define RCL, al -|.define OP, RB -|.define RD, RC -|.define RDW, RCW -|.define RDL, RCL -|.if X64 -|.define RAa, rcx -|.define RBa, rbp -|.define RCa, rax -|.define RDa, rax -|.else -|.define RAa, RA -|.define RBa, RB -|.define RCa, RC -|.define RDa, RD -|.endif -| -|.if not X64 -|.define FCARG1, ecx // x86 fastcall arguments. -|.define FCARG2, edx -|.elif X64WIN -|.define CARG1, rcx // x64/WIN64 C call arguments. -|.define CARG2, rdx -|.define CARG3, r8 -|.define CARG4, r9 -|.define CARG1d, ecx -|.define CARG2d, edx -|.define CARG3d, r8d -|.define CARG4d, r9d -|.define FCARG1, CARG1d // Upwards compatible to x86 fastcall. -|.define FCARG2, CARG2d -|.else -|.define CARG1, rdi // x64/POSIX C call arguments. -|.define CARG2, rsi -|.define CARG3, rdx -|.define CARG4, rcx -|.define CARG5, r8 -|.define CARG6, r9 -|.define CARG1d, edi -|.define CARG2d, esi -|.define CARG3d, edx -|.define CARG4d, ecx -|.define CARG5d, r8d -|.define CARG6d, r9d -|.define FCARG1, CARG1d // Simulate x86 fastcall. -|.define FCARG2, CARG2d -|.endif -| -|// Type definitions. Some of these are only used for documentation. -|.type L, lua_State -|.type GL, global_State -|.type TVALUE, TValue -|.type GCOBJ, GCobj -|.type STR, GCstr -|.type TAB, GCtab -|.type LFUNC, GCfuncL -|.type CFUNC, GCfuncC -|.type PROTO, GCproto -|.type UPVAL, GCupval -|.type NODE, Node -|.type NARGS, int -|.type TRACE, GCtrace -|.type SBUF, SBuf -| -|// Stack layout while in interpreter. Must match with lj_frame.h. -|//----------------------------------------------------------------------- -|.if not X64 // x86 stack layout. -| -|.if WIN -| -|.define CFRAME_SPACE, aword*9 // Delta for esp (see <--). -|.macro saveregs_ -| push edi; push esi; push ebx -| push extern lj_err_unwind_win -| fs; push dword [0] -| fs; mov [0], esp -| sub esp, CFRAME_SPACE -|.endmacro -|.macro restoreregs -| add esp, CFRAME_SPACE -| fs; pop dword [0] -| pop edi // Short for esp += 4. -| pop ebx; pop esi; pop edi; pop ebp -|.endmacro -| -|.else -| -|.define CFRAME_SPACE, aword*7 // Delta for esp (see <--). -|.macro saveregs_ -| push edi; push esi; push ebx -| sub esp, CFRAME_SPACE -|.endmacro -|.macro restoreregs -| add esp, CFRAME_SPACE -| pop ebx; pop esi; pop edi; pop ebp -|.endmacro -| -|.endif -| -|.macro saveregs -| push ebp; saveregs_ -|.endmacro -| -|.if WIN -|.define SAVE_ERRF, aword [esp+aword*19] // vm_pcall/vm_cpcall only. -|.define SAVE_NRES, aword [esp+aword*18] -|.define SAVE_CFRAME, aword [esp+aword*17] -|.define SAVE_L, aword [esp+aword*16] -|//----- 16 byte aligned, ^^^ arguments from C caller -|.define SAVE_RET, aword [esp+aword*15] //<-- esp entering interpreter. -|.define SAVE_R4, aword [esp+aword*14] -|.define SAVE_R3, aword [esp+aword*13] -|.define SAVE_R2, aword [esp+aword*12] -|//----- 16 byte aligned -|.define SAVE_R1, aword [esp+aword*11] -|.define SEH_FUNC, aword [esp+aword*10] -|.define SEH_NEXT, aword [esp+aword*9] //<-- esp after register saves. -|.define UNUSED2, aword [esp+aword*8] -|//----- 16 byte aligned -|.define UNUSED1, aword [esp+aword*7] -|.define SAVE_PC, aword [esp+aword*6] -|.define TMP2, aword [esp+aword*5] -|.define TMP1, aword [esp+aword*4] -|//----- 16 byte aligned -|.define ARG4, aword [esp+aword*3] -|.define ARG3, aword [esp+aword*2] -|.define ARG2, aword [esp+aword*1] -|.define ARG1, aword [esp] //<-- esp while in interpreter. -|//----- 16 byte aligned, ^^^ arguments for C callee -|.else -|.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only. -|.define SAVE_NRES, aword [esp+aword*14] -|.define SAVE_CFRAME, aword [esp+aword*13] -|.define SAVE_L, aword [esp+aword*12] -|//----- 16 byte aligned, ^^^ arguments from C caller -|.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter. -|.define SAVE_R4, aword [esp+aword*10] -|.define SAVE_R3, aword [esp+aword*9] -|.define SAVE_R2, aword [esp+aword*8] -|//----- 16 byte aligned -|.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves. -|.define SAVE_PC, aword [esp+aword*6] -|.define TMP2, aword [esp+aword*5] -|.define TMP1, aword [esp+aword*4] -|//----- 16 byte aligned -|.define ARG4, aword [esp+aword*3] -|.define ARG3, aword [esp+aword*2] -|.define ARG2, aword [esp+aword*1] -|.define ARG1, aword [esp] //<-- esp while in interpreter. -|//----- 16 byte aligned, ^^^ arguments for C callee -|.endif -| -|// FPARGx overlaps ARGx and ARG(x+1) on x86. -|.define FPARG3, qword [esp+qword*1] -|.define FPARG1, qword [esp] -|// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ). -|.define TMPQ, qword [esp+aword*4] -|.define TMP3, ARG4 -|.define ARG5, TMP1 -|.define TMPa, TMP1 -|.define MULTRES, TMP2 -| -|// Arguments for vm_call and vm_pcall. -|.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME! -| -|// Arguments for vm_cpcall. -|.define INARG_CP_CALL, SAVE_ERRF -|.define INARG_CP_UD, SAVE_NRES -|.define INARG_CP_FUNC, SAVE_CFRAME -| -|//----------------------------------------------------------------------- -|.elif X64WIN // x64/Windows stack layout -| -|.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--). -|.macro saveregs_ -| push rdi; push rsi; push rbx -| sub rsp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push rbp; saveregs_ -|.endmacro -|.macro restoreregs -| add rsp, CFRAME_SPACE -| pop rbx; pop rsi; pop rdi; pop rbp -|.endmacro -| -|.define SAVE_CFRAME, aword [rsp+aword*13] -|.define SAVE_PC, dword [rsp+dword*25] -|.define SAVE_L, dword [rsp+dword*24] -|.define SAVE_ERRF, dword [rsp+dword*23] -|.define SAVE_NRES, dword [rsp+dword*22] -|.define TMP2, dword [rsp+dword*21] -|.define TMP1, dword [rsp+dword*20] -|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter -|.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*8] -|.define SAVE_R3, aword [rsp+aword*7] -|.define SAVE_R2, aword [rsp+aword*6] -|.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves. -|.define ARG5, aword [rsp+aword*4] -|.define CSAVE_4, aword [rsp+aword*3] -|.define CSAVE_3, aword [rsp+aword*2] -|.define CSAVE_2, aword [rsp+aword*1] -|.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter. -|//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee -| -|// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ). -|.define TMPQ, qword [rsp+aword*10] -|.define MULTRES, TMP2 -|.define TMPa, ARG5 -|.define ARG5d, dword [rsp+aword*4] -|.define TMP3, ARG5d -| -|//----------------------------------------------------------------------- -|.else // x64/POSIX stack layout -| -|.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--). -|.macro saveregs_ -| push rbx; push r15; push r14 -|.if NO_UNWIND -| push r13; push r12 -|.endif -| sub rsp, CFRAME_SPACE -|.endmacro -|.macro saveregs -| push rbp; saveregs_ -|.endmacro -|.macro restoreregs -| add rsp, CFRAME_SPACE -|.if NO_UNWIND -| pop r12; pop r13 -|.endif -| pop r14; pop r15; pop rbx; pop rbp -|.endmacro -| -|//----- 16 byte aligned, -|.if NO_UNWIND -|.define SAVE_RET, aword [rsp+aword*11] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*10] -|.define SAVE_R3, aword [rsp+aword*9] -|.define SAVE_R2, aword [rsp+aword*8] -|.define SAVE_R1, aword [rsp+aword*7] -|.define SAVE_RU2, aword [rsp+aword*6] -|.define SAVE_RU1, aword [rsp+aword*5] //<-- rsp after register saves. -|.else -|.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter. -|.define SAVE_R4, aword [rsp+aword*8] -|.define SAVE_R3, aword [rsp+aword*7] -|.define SAVE_R2, aword [rsp+aword*6] -|.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves. -|.endif -|.define SAVE_CFRAME, aword [rsp+aword*4] -|.define SAVE_PC, dword [rsp+dword*7] -|.define SAVE_L, dword [rsp+dword*6] -|.define SAVE_ERRF, dword [rsp+dword*5] -|.define SAVE_NRES, dword [rsp+dword*4] -|.define TMPa, aword [rsp+aword*1] -|.define TMP2, dword [rsp+dword*1] -|.define TMP1, dword [rsp] //<-- rsp while in interpreter. -|//----- 16 byte aligned -| -|// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ). -|.define TMPQ, qword [rsp] -|.define TMP3, dword [rsp+aword*1] -|.define MULTRES, TMP2 -| -|.endif -| -|//----------------------------------------------------------------------- -| -|// Instruction headers. -|.macro ins_A; .endmacro -|.macro ins_AD; .endmacro -|.macro ins_AJ; .endmacro -|.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro -|.macro ins_AB_; movzx RB, RCH; .endmacro -|.macro ins_A_C; movzx RC, RCL; .endmacro -|.macro ins_AND; not RDa; .endmacro -| -|// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster). -|.macro ins_NEXT -| mov RC, [PC] -| movzx RA, RCH -| movzx OP, RCL -| add PC, 4 -| shr RC, 16 -|.if X64 -| jmp aword [DISPATCH+OP*8] -|.else -| jmp aword [DISPATCH+OP*4] -|.endif -|.endmacro -| -|// Instruction footer. -|.if 1 -| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use. -| .define ins_next, ins_NEXT -| .define ins_next_, ins_NEXT -|.else -| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch. -| // Affects only certain kinds of benchmarks (and only with -j off). -| // Around 10%-30% slower on Core2, a lot more slower on P4. -| .macro ins_next -| jmp ->ins_next -| .endmacro -| .macro ins_next_ -| ->ins_next: -| ins_NEXT -| .endmacro -|.endif -| -|// Call decode and dispatch. -|.macro ins_callt -| // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC -| mov PC, LFUNC:RB->pc -| mov RA, [PC] -| movzx OP, RAL -| movzx RA, RAH -| add PC, 4 -|.if X64 -| jmp aword [DISPATCH+OP*8] -|.else -| jmp aword [DISPATCH+OP*4] -|.endif -|.endmacro -| -|.macro ins_call -| // BASE = new base, RB = LFUNC, RD = nargs+1 -| mov [BASE-4], PC -| ins_callt -|.endmacro -| -|//----------------------------------------------------------------------- -| -|// Macros to test operand types. -|.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro -|.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro -|.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro -|.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro -|.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro -| -|// These operands must be used with movzx. -|.define PC_OP, byte [PC-4] -|.define PC_RA, byte [PC-3] -|.define PC_RB, byte [PC-1] -|.define PC_RC, byte [PC-2] -|.define PC_RD, word [PC-2] -| -|.macro branchPC, reg -| lea PC, [PC+reg*4-BCBIAS_J*4] -|.endmacro -| -|// Assumes DISPATCH is relative to GL. -#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field)) -#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field)) -| -#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto)) -| -|// Decrement hashed hotcount and trigger trace recorder if zero. -|.macro hotloop, reg -| mov reg, PC -| shr reg, 1 -| and reg, HOTCOUNT_PCMASK -| sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP -| jb ->vm_hotloop -|.endmacro -| -|.macro hotcall, reg -| mov reg, PC -| shr reg, 1 -| and reg, HOTCOUNT_PCMASK -| sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL -| jb ->vm_hotcall -|.endmacro -| -|// Set current VM state. -|.macro set_vmstate, st -| mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st -|.endmacro -| -|// x87 compares. -|.macro fcomparepp // Compare and pop st0 >< st1. -| fucomip st1 -| fpop -|.endmacro -| -|.macro fpop1; fstp st1; .endmacro -| -|// Synthesize SSE FP constants. -|.macro sseconst_abs, reg, tmp // Synthesize abs mask. -|.if X64 -| mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp -|.else -| pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1 -|.endif -|.endmacro -| -|.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const. -|.if X64 -| mov64 tmp, U64x(val,00000000); movd reg, tmp -|.else -| mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51 -|.endif -|.endmacro -| -|.macro sseconst_sign, reg, tmp // Synthesize sign mask. -| sseconst_hi reg, tmp, 80000000 -|.endmacro -|.macro sseconst_1, reg, tmp // Synthesize 1.0. -| sseconst_hi reg, tmp, 3ff00000 -|.endmacro -|.macro sseconst_2p52, reg, tmp // Synthesize 2^52. -| sseconst_hi reg, tmp, 43300000 -|.endmacro -|.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51. -| sseconst_hi reg, tmp, 43380000 -|.endmacro -| -|// Move table write barrier back. Overwrites reg. -|.macro barrierback, tab, reg -| and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab) -| mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)] -| mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab -| mov tab->gclist, reg -|.endmacro -| -|//----------------------------------------------------------------------- - -/* Generate subroutines used by opcodes and other parts of the VM. */ -/* The .code_sub section should be last to help static branch prediction. */ -static void build_subroutines(BuildCtx *ctx) -{ - |.code_sub - | - |//----------------------------------------------------------------------- - |//-- Return handling ---------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_returnp: - | test PC, FRAME_P - | jz ->cont_dispatch - | - | // Return from pcall or xpcall fast func. - | and PC, -8 - | sub BASE, PC // Restore caller base. - | lea RAa, [RA+PC-8] // Rebase RA and prepend one result. - | mov PC, [BASE-4] // Fetch PC of previous frame. - | // Prepending may overwrite the pcall frame, so do it at the end. - | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results. - | - |->vm_returnc: - | add RD, 1 // RD = nresults+1 - | jz ->vm_unwind_yield - | mov MULTRES, RD - | test PC, FRAME_TYPE - | jz ->BC_RET_Z // Handle regular return to Lua. - | - |->vm_return: - | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return - | xor PC, FRAME_C - | test PC, FRAME_TYPE - | jnz ->vm_returnp - | - | // Return to C. - | set_vmstate C - | and PC, -8 - | sub PC, BASE - | neg PC // Previous base = BASE - delta. - | - | sub RD, 1 - | jz >2 - |1: // Move results down. - |.if X64 - | mov RBa, [BASE+RA] - | mov [BASE-8], RBa - |.else - | mov RB, [BASE+RA] - | mov [BASE-8], RB - | mov RB, [BASE+RA+4] - | mov [BASE-4], RB - |.endif - | add BASE, 8 - | sub RD, 1 - | jnz <1 - |2: - | mov L:RB, SAVE_L - | mov L:RB->base, PC - |3: - | mov RD, MULTRES - | mov RA, SAVE_NRES // RA = wanted nresults+1 - |4: - | cmp RA, RD - | jne >6 // More/less results wanted? - |5: - | sub BASE, 8 - | mov L:RB->top, BASE - | - |->vm_leave_cp: - | mov RAa, SAVE_CFRAME // Restore previous C frame. - | mov L:RB->cframe, RAa - | xor eax, eax // Ok return status for vm_pcall. - | - |->vm_leave_unw: - | restoreregs - | ret - | - |6: - | jb >7 // Less results wanted? - | // More results wanted. Check stack size and fill up results with nil. - | cmp BASE, L:RB->maxstack - | ja >8 - | mov dword [BASE-4], LJ_TNIL - | add BASE, 8 - | add RD, 1 - | jmp <4 - | - |7: // Less results wanted. - | test RA, RA - | jz <5 // But check for LUA_MULTRET+1. - | sub RA, RD // Negative result! - | lea BASE, [BASE+RA*8] // Correct top. - | jmp <5 - | - |8: // Corner case: need to grow stack for filling up results. - | // This can happen if: - | // - A C function grows the stack (a lot). - | // - The GC shrinks the stack in between. - | // - A return back from a lua_call() with (high) nresults adjustment. - | mov L:RB->top, BASE // Save current top held in BASE (yes). - | mov MULTRES, RD // Need to fill only remainder with nil. - | mov FCARG2, RA - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->top // Need the (realloced) L->top in BASE. - | jmp <3 - | - |->vm_unwind_yield: - | mov al, LUA_YIELD - | jmp ->vm_unwind_c_eh - | - |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall. - | // (void *cframe, int errcode) - |.if X64 - | mov eax, CARG2d // Error return status for vm_pcall. - | mov rsp, CARG1 - |.else - | mov eax, FCARG2 // Error return status for vm_pcall. - | mov esp, FCARG1 - |.if WIN - | lea FCARG1, SEH_NEXT - | fs; mov [0], FCARG1 - |.endif - |.endif - |->vm_unwind_c_eh: // Landing pad for external unwinder. - | mov L:RB, SAVE_L - | mov GL:RB, L:RB->glref - | mov dword GL:RB->vmstate, ~LJ_VMST_C - | jmp ->vm_leave_unw - | - |->vm_unwind_rethrow: - |.if X64 and not X64WIN - | mov FCARG1, SAVE_L - | mov FCARG2, eax - | restoreregs - | jmp extern lj_err_throw@8 // (lua_State *L, int errcode) - |.endif - | - |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall. - | // (void *cframe) - |.if X64 - | and CARG1, CFRAME_RAWMASK - | mov rsp, CARG1 - |.else - | and FCARG1, CFRAME_RAWMASK - | mov esp, FCARG1 - |.if WIN - | lea FCARG1, SEH_NEXT - | fs; mov [0], FCARG1 - |.endif - |.endif - |->vm_unwind_ff_eh: // Landing pad for external unwinder. - | mov L:RB, SAVE_L - | mov RAa, -8 // Results start at BASE+RA = BASE-8. - | mov RD, 1+1 // Really 1+2 results, incr. later. - | mov BASE, L:RB->base - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | mov PC, [BASE-4] // Fetch PC of previous frame. - | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message. - | set_vmstate INTERP - | jmp ->vm_returnc // Increments RD/MULTRES and returns. - | - |.if WIN and not X64 - |->vm_rtlunwind@16: // Thin layer around RtlUnwind. - | // (void *cframe, void *excptrec, void *unwinder, int errcode) - | mov [esp], FCARG1 // Return value for RtlUnwind. - | push FCARG2 // Exception record for RtlUnwind. - | push 0 // Ignored by RtlUnwind. - | push dword [FCARG1+CFRAME_OFS_SEH] - | call extern RtlUnwind@16 // Violates ABI (clobbers too much). - | mov FCARG1, eax - | mov FCARG2, [esp+4] // errcode (for vm_unwind_c). - | ret // Jump to unwinder. - |.endif - | - |//----------------------------------------------------------------------- - |//-- Grow stack for calls ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_growstack_c: // Grow stack for C function. - | mov FCARG2, LUA_MINSTACK - | jmp >2 - | - |->vm_growstack_v: // Grow stack for vararg Lua function. - | sub RD, 8 - | jmp >1 - | - |->vm_growstack_f: // Grow stack for fixarg Lua function. - | // BASE = new base, RD = nargs+1, RB = L, PC = first PC - | lea RD, [BASE+NARGS:RD*8-8] - |1: - | movzx RA, byte [PC-4+PC2PROTO(framesize)] - | add PC, 4 // Must point after first instruction. - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov SAVE_PC, PC - | mov FCARG2, RA - |2: - | // RB = L, L->base = new base, L->top = top - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->base - | mov RD, L:RB->top - | mov LFUNC:RB, [BASE-8] - | sub RD, BASE - | shr RD, 3 - | add NARGS:RD, 1 - | // BASE = new base, RB = LFUNC, RD = nargs+1 - | ins_callt // Just retry the call. - | - |//----------------------------------------------------------------------- - |//-- Entry points into the assembler VM --------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_resume: // Setup C frame and resume thread. - | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0) - | saveregs - |.if X64 - | mov L:RB, CARG1d // Caveat: CARG1d may be RA. - | mov SAVE_L, CARG1d - | mov RA, CARG2d - |.else - | mov L:RB, SAVE_L - | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME! - |.endif - | mov PC, FRAME_CP - | xor RD, RD - | lea KBASEa, [esp+CFRAME_RESUME] - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | add DISPATCH, GG_G2DISP - | mov SAVE_PC, RD // Any value outside of bytecode is ok. - | mov SAVE_CFRAME, RDa - |.if X64 - | mov SAVE_NRES, RD - | mov SAVE_ERRF, RD - |.endif - | mov L:RB->cframe, KBASEa - | cmp byte L:RB->status, RDL - | je >2 // Initial resume (like a call). - | - | // Resume after yield (like a return). - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | mov byte L:RB->status, RDL - | mov BASE, L:RB->base - | mov RD, L:RB->top - | sub RD, RA - | shr RD, 3 - | add RD, 1 // RD = nresults+1 - | sub RA, BASE // RA = resultofs - | mov PC, [BASE-4] - | mov MULTRES, RD - | test PC, FRAME_TYPE - | jz ->BC_RET_Z - | jmp ->vm_return - | - |->vm_pcall: // Setup protected C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef) - | saveregs - | mov PC, FRAME_CP - |.if X64 - | mov SAVE_ERRF, CARG4d - |.endif - | jmp >1 - | - |->vm_call: // Setup C frame and enter VM. - | // (lua_State *L, TValue *base, int nres1) - | saveregs - | mov PC, FRAME_C - | - |1: // Entry point for vm_pcall above (PC = ftype). - |.if X64 - | mov SAVE_NRES, CARG3d - | mov L:RB, CARG1d // Caveat: CARG1d may be RA. - | mov SAVE_L, CARG1d - | mov RA, CARG2d - |.else - | mov L:RB, SAVE_L - | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME! - |.endif - | - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASEa - | mov SAVE_PC, L:RB // Any value outside of bytecode is ok. - | add DISPATCH, GG_G2DISP - |.if X64 - | mov L:RB->cframe, rsp - |.else - | mov L:RB->cframe, esp - |.endif - | - |2: // Entry point for vm_resume/vm_cpcall (RA = base, RB = L, PC = ftype). - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | mov BASE, L:RB->base // BASE = old base (used in vmeta_call). - | add PC, RA - | sub PC, BASE // PC = frame delta + frame type - | - | mov RD, L:RB->top - | sub RD, RA - | shr NARGS:RD, 3 - | add NARGS:RD, 1 // RD = nargs+1 - | - |->vm_call_dispatch: - | mov LFUNC:RB, [RA-8] - | cmp dword [RA-4], LJ_TFUNC - | jne ->vmeta_call // Ensure KBASE defined and != BASE. - | - |->vm_call_dispatch_f: - | mov BASE, RA - | ins_call - | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC - | - |->vm_cpcall: // Setup protected C frame, call C. - | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp) - | saveregs - |.if X64 - | mov L:RB, CARG1d // Caveat: CARG1d may be RA. - | mov SAVE_L, CARG1d - |.else - | mov L:RB, SAVE_L - | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap! - | mov RC, INARG_CP_UD // Get args before they are overwritten. - | mov RA, INARG_CP_FUNC - | mov BASE, INARG_CP_CALL - |.endif - | mov SAVE_PC, L:RB // Any value outside of bytecode is ok. - | - | mov KBASE, L:RB->stack // Compute -savestack(L, L->top). - | sub KBASE, L:RB->top - | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table. - | mov SAVE_ERRF, 0 // No error function. - | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame. - | add DISPATCH, GG_G2DISP - | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe). - | - |.if X64 - | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASEa - | mov L:RB->cframe, rsp - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | - | call CARG4 // (lua_State *L, lua_CFunction func, void *ud) - |.else - | mov ARG3, RC // Have to copy args downwards. - | mov ARG2, RA - | mov ARG1, L:RB - | - | mov KBASE, L:RB->cframe // Add our C frame to cframe chain. - | mov SAVE_CFRAME, KBASE - | mov L:RB->cframe, esp - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | - | call BASE // (lua_State *L, lua_CFunction func, void *ud) - |.endif - | // TValue * (new base) or NULL returned in eax (RC). - | test RC, RC - | jz ->vm_leave_cp // No base? Just remove C frame. - | mov RA, RC - | mov PC, FRAME_CP - | jmp <2 // Else continue with the call. - | - |//----------------------------------------------------------------------- - |//-- Metamethod handling ------------------------------------------------ - |//----------------------------------------------------------------------- - | - |//-- Continuation dispatch ---------------------------------------------- - | - |->cont_dispatch: - | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES) - | add RA, BASE - | and PC, -8 - | mov RB, BASE - | sub BASE, PC // Restore caller BASE. - | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg. - | mov RC, RA // ... in [RC] - | mov PC, [RB-12] // Restore PC from [cont|PC]. - |.if X64 - | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug. - |.if FFI - | cmp RA, 1 - | jbe >1 - |.endif - | lea KBASEa, qword [=>0] - | add RAa, KBASEa - |.else - | mov RA, dword [RB-16] - |.if FFI - | cmp RA, 1 - | jbe >1 - |.endif - |.endif - | mov LFUNC:KBASE, [BASE-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | // BASE = base, RC = result, RB = meta base - | jmp RAa // Jump to continuation. - | - |.if FFI - |1: - | je ->cont_ffi_callback // cont = 1: return from FFI callback. - | // cont = 0: Tail call from C function. - | sub RB, BASE - | shr RB, 3 - | lea RD, [RB-1] - | jmp ->vm_call_tail - |.endif - | - |->cont_cat: // BASE = base, RC = result, RB = mbase - | movzx RA, PC_RB - | sub RB, 16 - | lea RA, [BASE+RA*8] - | sub RA, RB - | je ->cont_ra - | neg RA - | shr RA, 3 - |.if X64WIN - | mov CARG3d, RA - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | mov RCa, [RC] - | mov [RB], RCa - | mov CARG2d, RB - |.elif X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | mov CARG3d, RA - | mov RAa, [RC] - | mov [RB], RAa - | mov CARG2d, RB - |.else - | mov ARG3, RA - | mov RA, [RC+4] - | mov RC, [RC] - | mov [RB+4], RA - | mov [RB], RC - | mov ARG2, RB - |.endif - | jmp ->BC_CAT_Z - | - |//-- Table indexing metamethods ----------------------------------------- - | - |->vmeta_tgets: - | mov TMP1, RC // RC = GCstr * - | mov TMP2, LJ_TSTR - | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2. - | cmp PC_OP, BC_GGET - | jne >1 - | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv. - | mov [RA], TAB:RB // RB = GCtab * - | mov dword [RA+4], LJ_TTAB - | mov RB, RA - | jmp >2 - | - |->vmeta_tgetb: - | movzx RC, PC_RC - |.if DUALNUM - | mov TMP2, LJ_TISNUM - | mov TMP1, RC - |.else - | cvtsi2sd xmm0, RC - | movsd TMPQ, xmm0 - |.endif - | lea RCa, TMPQ // Store temp. TValue in TMPQ. - | jmp >1 - | - |->vmeta_tgetv: - | movzx RC, PC_RC // Reload TValue *k from RC. - | lea RC, [BASE+RC*8] - |1: - | movzx RB, PC_RB // Reload TValue *t from RB. - | lea RB, [BASE+RB*8] - |2: - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RB - | mov CARG3, RCa // May be 64 bit ptr to stack. - | mov L:RB, L:CARG1d - |.else - | mov ARG2, RB - | mov L:RB, SAVE_L - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k) - | // TValue * (finished) or NULL (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz >3 - |->cont_ra: // BASE = base, RC = result - | movzx RA, PC_RA - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC+4] - | mov RC, [RC] - | mov [BASE+RA*8+4], RB - | mov [BASE+RA*8], RC - |.endif - | ins_next - | - |3: // Call __index metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k - | mov RA, L:RB->top - | mov [RA-12], PC // [cont|PC] - | lea PC, [RA+FRAME_CONT] - | sub PC, BASE - | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here. - | mov NARGS:RD, 2+1 // 2 args for func(t, k). - | jmp ->vm_call_dispatch_f - | - |->vmeta_tgetr: - | mov FCARG1, TAB:RB - | mov RB, BASE // Save BASE. - | mov FCARG2, RC // Caveat: FCARG2 == BASE - | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key) - | // cTValue * or NULL returned in eax (RC). - | movzx RA, PC_RA - | mov BASE, RB // Restore BASE. - | test RC, RC - | jnz ->BC_TGETR_Z - | mov dword [BASE+RA*8+4], LJ_TNIL - | jmp ->BC_TGETR2_Z - | - |//----------------------------------------------------------------------- - | - |->vmeta_tsets: - | mov TMP1, RC // RC = GCstr * - | mov TMP2, LJ_TSTR - | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2. - | cmp PC_OP, BC_GSET - | jne >1 - | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv. - | mov [RA], TAB:RB // RB = GCtab * - | mov dword [RA+4], LJ_TTAB - | mov RB, RA - | jmp >2 - | - |->vmeta_tsetb: - | movzx RC, PC_RC - |.if DUALNUM - | mov TMP2, LJ_TISNUM - | mov TMP1, RC - |.else - | cvtsi2sd xmm0, RC - | movsd TMPQ, xmm0 - |.endif - | lea RCa, TMPQ // Store temp. TValue in TMPQ. - | jmp >1 - | - |->vmeta_tsetv: - | movzx RC, PC_RC // Reload TValue *k from RC. - | lea RC, [BASE+RC*8] - |1: - | movzx RB, PC_RB // Reload TValue *t from RB. - | lea RB, [BASE+RB*8] - |2: - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RB - | mov CARG3, RCa // May be 64 bit ptr to stack. - | mov L:RB, L:CARG1d - |.else - | mov ARG2, RB - | mov L:RB, SAVE_L - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k) - | // TValue * (finished) or NULL (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz >3 - | // NOBARRIER: lj_meta_tset ensures the table is not black. - | movzx RA, PC_RA - |.if X64 - | mov RBa, [BASE+RA*8] - | mov [RC], RBa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - |->cont_nop: // BASE = base, (RC = result) - | ins_next - | - |3: // Call __newindex metamethod. - | // BASE = base, L->top = new base, stack = cont/func/t/k/(v) - | mov RA, L:RB->top - | mov [RA-12], PC // [cont|PC] - | movzx RC, PC_RA - | // Copy value to third argument. - |.if X64 - | mov RBa, [BASE+RC*8] - | mov [RA+16], RBa - |.else - | mov RB, [BASE+RC*8+4] - | mov RC, [BASE+RC*8] - | mov [RA+20], RB - | mov [RA+16], RC - |.endif - | lea PC, [RA+FRAME_CONT] - | sub PC, BASE - | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here. - | mov NARGS:RD, 3+1 // 3 args for func(t, k, v). - | jmp ->vm_call_dispatch_f - | - |->vmeta_tsetr: - |.if X64WIN - | mov L:CARG1d, SAVE_L - | mov CARG3d, RC - | mov L:CARG1d->base, BASE - | xchg CARG2d, TAB:RB // Caveat: CARG2d == BASE. - |.elif X64 - | mov L:CARG1d, SAVE_L - | mov CARG2d, TAB:RB - | mov L:CARG1d->base, BASE - | mov RB, BASE // Save BASE. - | mov CARG3d, RC // Caveat: CARG3d == BASE. - |.else - | mov L:RA, SAVE_L - | mov ARG2, TAB:RB - | mov RB, BASE // Save BASE. - | mov ARG3, RC - | mov ARG1, L:RA - | mov L:RA->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_tab_setinth // (lua_State *L, GCtab *t, int32_t key) - | // TValue * returned in eax (RC). - | movzx RA, PC_RA - | mov BASE, RB // Restore BASE. - | jmp ->BC_TSETR_Z - | - |//-- Comparison metamethods --------------------------------------------- - | - |->vmeta_comp: - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE. - |.if X64WIN - | lea CARG3d, [BASE+RD*8] - | lea CARG2d, [BASE+RA*8] - |.else - | lea CARG2d, [BASE+RA*8] - | lea CARG3d, [BASE+RD*8] - |.endif - | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA. - | movzx CARG4d, PC_OP - |.else - | movzx RB, PC_OP - | lea RD, [BASE+RD*8] - | lea RA, [BASE+RA*8] - | mov ARG4, RB - | mov L:RB, SAVE_L - | mov ARG3, RD - | mov ARG2, RA - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - |3: - | mov BASE, L:RB->base - | cmp RC, 1 - | ja ->vmeta_binop - |4: - | lea PC, [PC+4] - | jb >6 - |5: - | movzx RD, PC_RD - | branchPC RD - |6: - | ins_next - | - |->cont_condt: // BASE = base, RC = result - | add PC, 4 - | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true. - | jb <5 - | jmp <6 - | - |->cont_condf: // BASE = base, RC = result - | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false. - | jmp <4 - | - |->vmeta_equal: - | sub PC, 4 - |.if X64WIN - | mov CARG3d, RD - | mov CARG4d, RB - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d == BASE. - | mov CARG2d, RA - | mov CARG1d, L:RB // Caveat: CARG1d == RA. - |.elif X64 - | mov CARG2d, RA - | mov CARG4d, RB // Caveat: CARG4d == RA. - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG3d == BASE. - | mov CARG3d, RD - | mov CARG1d, L:RB - |.else - | mov ARG4, RB - | mov L:RB, SAVE_L - | mov ARG3, RD - | mov ARG2, RA - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - | jmp <3 - | - |->vmeta_equal_cd: - |.if FFI - | sub PC, 4 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG1, L:RB - | mov FCARG2, dword [PC-4] - | mov SAVE_PC, PC - | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins) - | // 0/1 or TValue * (metamethod) returned in eax (RC). - | jmp <3 - |.endif - | - |->vmeta_istype: - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RA - | movzx CARG3d, PC_RD - | mov L:CARG1d, L:RB - |.else - | movzx RD, PC_RD - | mov ARG2, RA - | mov L:RB, SAVE_L - | mov ARG3, RD - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_istype // (lua_State *L, BCReg ra, BCReg tp) - | mov BASE, L:RB->base - | jmp <6 - | - |//-- Arithmetic metamethods --------------------------------------------- - | - |->vmeta_arith_vno: - |.if DUALNUM - | movzx RB, PC_RB - |.endif - |->vmeta_arith_vn: - | lea RC, [KBASE+RC*8] - | jmp >1 - | - |->vmeta_arith_nvo: - |.if DUALNUM - | movzx RC, PC_RC - |.endif - |->vmeta_arith_nv: - | lea RC, [KBASE+RC*8] - | lea RB, [BASE+RB*8] - | xchg RB, RC - | jmp >2 - | - |->vmeta_unm: - | lea RC, [BASE+RD*8] - | mov RB, RC - | jmp >2 - | - |->vmeta_arith_vvo: - |.if DUALNUM - | movzx RB, PC_RB - |.endif - |->vmeta_arith_vv: - | lea RC, [BASE+RC*8] - |1: - | lea RB, [BASE+RB*8] - |2: - | lea RA, [BASE+RA*8] - |.if X64WIN - | mov CARG3d, RB - | mov CARG4d, RC - | movzx RC, PC_OP - | mov ARG5d, RC - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d == BASE. - | mov CARG2d, RA - | mov CARG1d, L:RB // Caveat: CARG1d == RA. - |.elif X64 - | movzx CARG5d, PC_OP - | mov CARG2d, RA - | mov CARG4d, RC // Caveat: CARG4d == RA. - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE. - | mov CARG3d, RB - | mov L:RB, L:CARG1d - |.else - | mov ARG3, RB - | mov L:RB, SAVE_L - | mov ARG4, RC - | movzx RC, PC_OP - | mov ARG2, RA - | mov ARG5, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op) - | // NULL (finished) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jz ->cont_nop - | - | // Call metamethod for binary op. - |->vmeta_binop: - | // BASE = base, RC = new base, stack = cont/func/o1/o2 - | mov RA, RC - | sub RC, BASE - | mov [RA-12], PC // [cont|PC] - | lea PC, [RC+FRAME_CONT] - | mov NARGS:RD, 2+1 // 2 args for func(o1, o2). - | jmp ->vm_call_dispatch - | - |->vmeta_len: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB - | mov SAVE_PC, PC - | call extern lj_meta_len@8 // (lua_State *L, TValue *o) - | // NULL (retry) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base -#if LJ_52 - | test RC, RC - | jne ->vmeta_binop // Binop call for compatibility. - | movzx RD, PC_RD - | mov TAB:FCARG1, [BASE+RD*8] - | jmp ->BC_LEN_Z -#else - | jmp ->vmeta_binop // Binop call for compatibility. -#endif - | - |//-- Call metamethod ---------------------------------------------------- - | - |->vmeta_call_ra: - | lea RA, [BASE+RA*8+8] - |->vmeta_call: // Resolve and call __call metamethod. - | // BASE = old base, RA = new base, RC = nargs+1, PC = return - | mov TMP2, RA // Save RA, RC for us. - | mov TMP1, NARGS:RD - | sub RA, 8 - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, RA - | lea CARG3d, [RA+NARGS:RD*8] - | mov CARG1d, L:RB // Caveat: CARG1d may be RA. - |.else - | lea RC, [RA+NARGS:RD*8] - | mov L:RB, SAVE_L - | mov ARG2, RA - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE // This is the callers base! - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top) - | mov BASE, L:RB->base - | mov RA, TMP2 - | mov NARGS:RD, TMP1 - | mov LFUNC:RB, [RA-8] - | add NARGS:RD, 1 - | // This is fragile. L->base must not move, KBASE must always be defined. - |.if x64 - | cmp KBASEa, rdx // Continue with CALLT if flag set. - |.else - | cmp KBASE, BASE // Continue with CALLT if flag set. - |.endif - | je ->BC_CALLT_Z - | mov BASE, RA - | ins_call // Otherwise call resolved metamethod. - | - |//-- Argument coercion for 'for' statement ------------------------------ - | - |->vmeta_for: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG2, RA // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA - | mov SAVE_PC, PC - | call extern lj_meta_for@8 // (lua_State *L, TValue *base) - | mov BASE, L:RB->base - | mov RC, [PC-4] - | movzx RA, RCH - | movzx OP, RCL - | shr RC, 16 - |.if X64 - | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI. - |.else - | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI. - |.endif - | - |//----------------------------------------------------------------------- - |//-- Fast functions ----------------------------------------------------- - |//----------------------------------------------------------------------- - | - |.macro .ffunc, name - |->ff_ .. name: - |.endmacro - | - |.macro .ffunc_1, name - |->ff_ .. name: - | cmp NARGS:RD, 1+1; jb ->fff_fallback - |.endmacro - | - |.macro .ffunc_2, name - |->ff_ .. name: - | cmp NARGS:RD, 2+1; jb ->fff_fallback - |.endmacro - | - |.macro .ffunc_nsse, name, op - | .ffunc_1 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | op xmm0, qword [BASE] - |.endmacro - | - |.macro .ffunc_nsse, name - | .ffunc_nsse name, movsd - |.endmacro - | - |.macro .ffunc_nnsse, name - | .ffunc_2 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback - | movsd xmm0, qword [BASE] - | movsd xmm1, qword [BASE+8] - |.endmacro - | - |.macro .ffunc_nnr, name - | .ffunc_2 name - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback - | fld qword [BASE+8] - | fld qword [BASE] - |.endmacro - | - |// Inlined GC threshold check. Caveat: uses label 1. - |.macro ffgccheck - | mov RB, [DISPATCH+DISPATCH_GL(gc.total)] - | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)] - | jb >1 - | call ->fff_gcstep - |1: - |.endmacro - | - |//-- Base library: checks ----------------------------------------------- - | - |.ffunc_1 assert - | mov RB, [BASE+4] - | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback - | mov PC, [BASE-4] - | mov MULTRES, RD - | mov [BASE-4], RB - | mov RB, [BASE] - | mov [BASE-8], RB - | sub RD, 2 - | jz >2 - | mov RA, BASE - |1: - | add RA, 8 - |.if X64 - | mov RBa, [RA] - | mov [RA-8], RBa - |.else - | mov RB, [RA+4] - | mov [RA-4], RB - | mov RB, [RA] - | mov [RA-8], RB - |.endif - | sub RD, 1 - | jnz <1 - |2: - | mov RD, MULTRES - | jmp ->fff_res_ - | - |.ffunc_1 type - | mov RB, [BASE+4] - |.if X64 - | mov RA, RB - | sar RA, 15 - | cmp RA, -2 - | je >3 - |.endif - | mov RC, ~LJ_TNUMX - | not RB - | cmp RC, RB - | cmova RC, RB - |2: - | mov CFUNC:RB, [BASE-8] - | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))] - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TSTR - | mov [BASE-8], STR:RC - | jmp ->fff_res1 - |.if X64 - |3: - | mov RC, ~LJ_TLIGHTUD - | jmp <2 - |.endif - | - |//-- Base library: getters and setters --------------------------------- - | - |.ffunc_1 getmetatable - | mov RB, [BASE+4] - | mov PC, [BASE-4] - | cmp RB, LJ_TTAB; jne >6 - |1: // Field metatable must be at same offset for GCtab and GCudata! - | mov TAB:RB, [BASE] - | mov TAB:RB, TAB:RB->metatable - |2: - | test TAB:RB, TAB:RB - | mov dword [BASE-4], LJ_TNIL - | jz ->fff_res1 - | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)] - | mov dword [BASE-4], LJ_TTAB // Store metatable as default result. - | mov [BASE-8], TAB:RB - | mov RA, TAB:RB->hmask - | and RA, STR:RC->sid - | imul RA, #NODE - | add NODE:RA, TAB:RB->node - |3: // Rearranged logic, because we expect _not_ to find the key. - | cmp dword NODE:RA->key.it, LJ_TSTR - | jne >4 - | cmp dword NODE:RA->key.gcr, STR:RC - | je >5 - |4: - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <3 - | jmp ->fff_res1 // Not found, keep default result. - |5: - | mov RB, [RA+4] - | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value. - | mov RC, [RA] - | mov [BASE-4], RB // Return value of mt.__metatable. - | mov [BASE-8], RC - | jmp ->fff_res1 - | - |6: - | cmp RB, LJ_TUDATA; je <1 - |.if X64 - | cmp RB, LJ_TNUMX; ja >8 - | cmp RB, LJ_TISNUM; jbe >7 - | mov RB, LJ_TLIGHTUD - | jmp >8 - |7: - |.else - | cmp RB, LJ_TISNUM; ja >8 - |.endif - | mov RB, LJ_TNUMX - |8: - | not RB - | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])] - | jmp <2 - | - |.ffunc_2 setmetatable - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | // Fast path: no mt for table yet and not clearing the mt. - | mov TAB:RB, [BASE] - | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback - | mov TAB:RC, [BASE+8] - | mov TAB:RB->metatable, TAB:RC - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TTAB // Return original table. - | mov [BASE-8], TAB:RB - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jz >1 - | // Possible write barrier. Table is black, but skip iswhite(mt) check. - | barrierback TAB:RB, RC - |1: - | jmp ->fff_res1 - | - |.ffunc_2 rawget - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - |.if X64WIN - | mov RB, BASE // Save BASE. - | lea CARG3d, [BASE+8] - | mov CARG2d, [BASE] // Caveat: CARG2d == BASE. - | mov CARG1d, SAVE_L - |.elif X64 - | mov RB, BASE // Save BASE. - | mov CARG2d, [BASE] - | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE. - | mov CARG1d, SAVE_L - |.else - | mov TAB:RD, [BASE] - | mov L:RB, SAVE_L - | mov ARG2, TAB:RD - | mov ARG1, L:RB - | mov RB, BASE // Save BASE. - | add BASE, 8 - | mov ARG3, BASE - |.endif - | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key) - | // cTValue * returned in eax (RD). - | mov BASE, RB // Restore BASE. - | // Copy table slot. - |.if X64 - | mov RBa, [RD] - | mov PC, [BASE-4] - | mov [BASE-8], RBa - |.else - | mov RB, [RD] - | mov RD, [RD+4] - | mov PC, [BASE-4] - | mov [BASE-8], RB - | mov [BASE-4], RD - |.endif - | jmp ->fff_res1 - | - |//-- Base library: conversions ------------------------------------------ - | - |.ffunc tonumber - | // Only handles the number case inline (without a base argument). - | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument. - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne >1 - | mov RB, dword [BASE]; jmp ->fff_resi - |1: - | ja ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0 - | - |.ffunc_1 tostring - | // Only handles the string or number case inline. - | mov PC, [BASE-4] - | cmp dword [BASE+4], LJ_TSTR; jne >3 - | // A __tostring method in the string base metatable is ignored. - | mov STR:RD, [BASE] - |2: - | mov dword [BASE-4], LJ_TSTR - | mov [BASE-8], STR:RD - | jmp ->fff_res1 - |3: // Handle numbers inline, unless a number base metatable is present. - | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback - | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0 - | jne ->fff_fallback - | ffgccheck // Caveat: uses label 1. - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Add frame since C call can throw. - | mov SAVE_PC, PC // Redundant (but a defined value). - |.if X64 and not X64WIN - | mov FCARG2, BASE // Otherwise: FCARG2 == BASE - |.endif - | mov L:FCARG1, L:RB - |.if DUALNUM - | call extern lj_strfmt_number@8 // (lua_State *L, cTValue *o) - |.else - | call extern lj_strfmt_num@8 // (lua_State *L, lua_Number *np) - |.endif - | // GCstr returned in eax (RD). - | mov BASE, L:RB->base - | jmp <2 - | - |//-- Base library: iterators ------------------------------------------- - | - |.ffunc_1 next - | je >2 // Missing 2nd arg? - |1: - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | mov PC, [BASE-4] - | mov RB, BASE // Save BASE. - |.if X64WIN - | mov CARG1d, [BASE] - | lea CARG3d, [BASE-8] - | lea CARG2d, [BASE+8] // Caveat: CARG2d == BASE. - |.elif X64 - | mov CARG1d, [BASE] - | lea CARG2d, [BASE+8] - | lea CARG3d, [BASE-8] // Caveat: CARG3d == BASE. - |.else - | mov TAB:RD, [BASE] - | mov ARG1, TAB:RD - | add BASE, 8 - | mov ARG2, BASE - | sub BASE, 8+8 - | mov ARG3, BASE - |.endif - | call extern lj_tab_next // (GCtab *t, cTValue *key, TValue *o) - | // 1=found, 0=end, -1=error returned in eax (RD). - | mov BASE, RB // Restore BASE. - | test RD, RD; jg ->fff_res2 // Found key/value. - | js ->fff_fallback_2 // Invalid key. - | // End of traversal: return nil. - | mov dword [BASE-4], LJ_TNIL - | jmp ->fff_res1 - |2: // Set missing 2nd arg to nil. - | mov dword [BASE+12], LJ_TNIL - | jmp <1 - | - |.ffunc_1 pairs - | mov TAB:RB, [BASE] - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback -#if LJ_52 - | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback -#endif - | mov CFUNC:RB, [BASE-8] - | mov CFUNC:RD, CFUNC:RB->upvalue[0] - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TFUNC - | mov [BASE-8], CFUNC:RD - | mov dword [BASE+12], LJ_TNIL - | mov RD, 1+3 - | jmp ->fff_res - | - |.ffunc_2 ipairs_aux - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | mov PC, [BASE-4] - |.if DUALNUM - | mov RD, dword [BASE+8] - | add RD, 1 - | mov dword [BASE-4], LJ_TISNUM - | mov dword [BASE-8], RD - |.else - | movsd xmm0, qword [BASE+8] - | sseconst_1 xmm1, RBa - | addsd xmm0, xmm1 - | cvttsd2si RD, xmm0 - | movsd qword [BASE-8], xmm0 - |.endif - | mov TAB:RB, [BASE] - | cmp RD, TAB:RB->asize; jae >2 // Not in array part? - | shl RD, 3 - | add RD, TAB:RB->array - |1: - | cmp dword [RD+4], LJ_TNIL; je ->fff_res0 - | // Copy array slot. - |.if X64 - | mov RBa, [RD] - | mov [BASE], RBa - |.else - | mov RB, [RD] - | mov RD, [RD+4] - | mov [BASE], RB - | mov [BASE+4], RD - |.endif - |->fff_res2: - | mov RD, 1+2 - | jmp ->fff_res - |2: // Check for empty hash part first. Otherwise call C function. - | cmp dword TAB:RB->hmask, 0; je ->fff_res0 - | mov FCARG1, TAB:RB - | mov RB, BASE // Save BASE. - | mov FCARG2, RD // Caveat: FCARG2 == BASE - | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key) - | // cTValue * or NULL returned in eax (RD). - | mov BASE, RB - | test RD, RD - | jnz <1 - |->fff_res0: - | mov RD, 1+0 - | jmp ->fff_res - | - |.ffunc_1 ipairs - | mov TAB:RB, [BASE] - | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback -#if LJ_52 - | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback -#endif - | mov CFUNC:RB, [BASE-8] - | mov CFUNC:RD, CFUNC:RB->upvalue[0] - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TFUNC - | mov [BASE-8], CFUNC:RD - |.if DUALNUM - | mov dword [BASE+12], LJ_TISNUM - | mov dword [BASE+8], 0 - |.else - | xorps xmm0, xmm0 - | movsd qword [BASE+8], xmm0 - |.endif - | mov RD, 1+3 - | jmp ->fff_res - | - |//-- Base library: catch errors ---------------------------------------- - | - |.ffunc_1 pcall - | lea RA, [BASE+8] - | sub NARGS:RD, 1 - | mov PC, 8+FRAME_PCALL - |1: - | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)] - | shr RB, HOOK_ACTIVE_SHIFT - | and RB, 1 - | add PC, RB // Remember active hook before pcall. - | jmp ->vm_call_dispatch - | - |.ffunc_2 xpcall - | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback - | mov RB, [BASE+4] // Swap function and traceback. - | mov [BASE+12], RB - | mov dword [BASE+4], LJ_TFUNC - | mov LFUNC:RB, [BASE] - | mov PC, [BASE+8] - | mov [BASE+8], LFUNC:RB - | mov [BASE], PC - | lea RA, [BASE+16] - | sub NARGS:RD, 2 - | mov PC, 16+FRAME_PCALL - | jmp <1 - | - |//-- Coroutine library -------------------------------------------------- - | - |.macro coroutine_resume_wrap, resume - |.if resume - |.ffunc_1 coroutine_resume - | mov L:RB, [BASE] - |.else - |.ffunc coroutine_wrap_aux - | mov CFUNC:RB, [BASE-8] - | mov L:RB, CFUNC:RB->upvalue[0].gcr - |.endif - | mov PC, [BASE-4] - | mov SAVE_PC, PC - |.if X64 - | mov TMP1, L:RB - |.else - | mov ARG1, L:RB - |.endif - |.if resume - | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback - |.endif - | cmp aword L:RB->cframe, 0; jne ->fff_fallback - | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback - | mov RA, L:RB->top - | je >1 // Status != LUA_YIELD (i.e. 0)? - | cmp RA, L:RB->base // Check for presence of initial func. - | je ->fff_fallback - |1: - |.if resume - | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread). - |.else - | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1). - |.endif - | cmp PC, L:RB->maxstack; ja ->fff_fallback - | mov L:RB->top, PC - | - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - |.if resume - | add BASE, 8 // Keep resumed thread in stack for GC. - |.endif - | mov L:RB->top, BASE - |.if resume - | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move. - |.else - | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move. - |.endif - | sub RBa, PCa // Relative to PC. - | - | cmp PC, RA - | je >3 - |2: // Move args to coroutine. - |.if X64 - | mov RCa, [PC+RB] - | mov [PC-8], RCa - |.else - | mov RC, [PC+RB+4] - | mov [PC-4], RC - | mov RC, [PC+RB] - | mov [PC-8], RC - |.endif - | sub PC, 8 - | cmp PC, RA - | jne <2 - |3: - |.if X64 - | mov CARG2d, RA - | mov CARG1d, TMP1 - |.else - | mov ARG2, RA - | xor RA, RA - | mov ARG4, RA - | mov ARG3, RA - |.endif - | call ->vm_resume // (lua_State *L, TValue *base, 0, 0) - | - | mov L:RB, SAVE_L - |.if X64 - | mov L:PC, TMP1 - |.else - | mov L:PC, ARG1 // The callee doesn't modify SAVE_L. - |.endif - | mov BASE, L:RB->base - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | - | cmp eax, LUA_YIELD - | ja >8 - |4: - | mov RA, L:PC->base - | mov KBASE, L:PC->top - | mov L:PC->top, RA // Clear coroutine stack. - | mov PC, KBASE - | sub PC, RA - | je >6 // No results? - | lea RD, [BASE+PC] - | shr PC, 3 - | cmp RD, L:RB->maxstack - | ja >9 // Need to grow stack? - | - | mov RB, BASE - | sub RBa, RAa - |5: // Move results from coroutine. - |.if X64 - | mov RDa, [RA] - | mov [RA+RB], RDa - |.else - | mov RD, [RA] - | mov [RA+RB], RD - | mov RD, [RA+4] - | mov [RA+RB+4], RD - |.endif - | add RA, 8 - | cmp RA, KBASE - | jne <5 - |6: - |.if resume - | lea RD, [PC+2] // nresults+1 = 1 + true + results. - | mov dword [BASE-4], LJ_TTRUE // Prepend true to results. - |.else - | lea RD, [PC+1] // nresults+1 = 1 + results. - |.endif - |7: - | mov PC, SAVE_PC - | mov MULTRES, RD - |.if resume - | mov RAa, -8 - |.else - | xor RA, RA - |.endif - | test PC, FRAME_TYPE - | jz ->BC_RET_Z - | jmp ->vm_return - | - |8: // Coroutine returned with error (at co->top-1). - |.if resume - | mov dword [BASE-4], LJ_TFALSE // Prepend false to results. - | mov RA, L:PC->top - | sub RA, 8 - | mov L:PC->top, RA // Clear error from coroutine stack. - | // Copy error message. - |.if X64 - | mov RDa, [RA] - | mov [BASE], RDa - |.else - | mov RD, [RA] - | mov [BASE], RD - | mov RD, [RA+4] - | mov [BASE+4], RD - |.endif - | mov RD, 1+2 // nresults+1 = 1 + false + error. - | jmp <7 - |.else - | mov FCARG2, L:PC - | mov FCARG1, L:RB - | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co) - | // Error function does not return. - |.endif - | - |9: // Handle stack expansion on return from yield. - |.if X64 - | mov L:RA, TMP1 - |.else - | mov L:RA, ARG1 // The callee doesn't modify SAVE_L. - |.endif - | mov L:RA->top, KBASE // Undo coroutine stack clearing. - | mov FCARG2, PC - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - |.if X64 - | mov L:PC, TMP1 - |.else - | mov L:PC, ARG1 - |.endif - | mov BASE, L:RB->base - | jmp <4 // Retry the stack move. - |.endmacro - | - | coroutine_resume_wrap 1 // coroutine.resume - | coroutine_resume_wrap 0 // coroutine.wrap - | - |.ffunc coroutine_yield - | mov L:RB, SAVE_L - | test aword L:RB->cframe, CFRAME_RESUME - | jz ->fff_fallback - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB->top, RD - | xor RD, RD - | mov aword L:RB->cframe, RDa - | mov al, LUA_YIELD - | mov byte L:RB->status, al - | jmp ->vm_leave_unw - | - |//-- Math library ------------------------------------------------------- - | - |.if not DUALNUM - |->fff_resi: // Dummy. - |.endif - | - |->fff_resn: - | mov PC, [BASE-4] - | fstp qword [BASE-8] - | jmp ->fff_res1 - | - | .ffunc_1 math_abs - |.if DUALNUM - | cmp dword [BASE+4], LJ_TISNUM; jne >2 - | mov RB, dword [BASE] - | cmp RB, 0; jns ->fff_resi - | neg RB; js >1 - |->fff_resbit: - |->fff_resi: - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TISNUM - | mov dword [BASE-8], RB - | jmp ->fff_res1 - |1: - | mov PC, [BASE-4] - | mov dword [BASE-4], 0x41e00000 // 2^31. - | mov dword [BASE-8], 0 - | jmp ->fff_res1 - |2: - | ja ->fff_fallback - |.else - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - |.endif - | movsd xmm0, qword [BASE] - | sseconst_abs xmm1, RDa - | andps xmm0, xmm1 - |->fff_resxmm0: - | mov PC, [BASE-4] - | movsd qword [BASE-8], xmm0 - | // fallthrough - | - |->fff_res1: - | mov RD, 1+1 - |->fff_res: - | mov MULTRES, RD - |->fff_res_: - | test PC, FRAME_TYPE - | jnz >7 - |5: - | cmp PC_RB, RDL // More results expected? - | ja >6 - | // Adjust BASE. KBASE is assumed to be set for the calling frame. - | movzx RA, PC_RA - | not RAa // Note: ~RA = -(RA+1) - | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8 - | ins_next - | - |6: // Fill up results with nil. - | mov dword [BASE+RD*8-12], LJ_TNIL - | add RD, 1 - | jmp <5 - | - |7: // Non-standard return case. - | mov RAa, -8 // Results start at BASE+RA = BASE-8. - | jmp ->vm_return - | - |.if X64 - |.define fff_resfp, fff_resxmm0 - |.else - |.define fff_resfp, fff_resn - |.endif - | - |.macro math_round, func - | .ffunc math_ .. func - |.if DUALNUM - | cmp dword [BASE+4], LJ_TISNUM; jne >1 - | mov RB, dword [BASE]; jmp ->fff_resi - |1: - | ja ->fff_fallback - |.else - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - |.endif - | movsd xmm0, qword [BASE] - | call ->vm_ .. func .. _sse - |.if DUALNUM - | cvttsd2si RB, xmm0 - | cmp RB, 0x80000000 - | jne ->fff_resi - | cvtsi2sd xmm1, RB - | ucomisd xmm0, xmm1 - | jp ->fff_resxmm0 - | je ->fff_resi - |.endif - | jmp ->fff_resxmm0 - |.endmacro - | - | math_round floor - | math_round ceil - | - |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0 - | - |.ffunc math_log - | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument. - | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback - | movsd xmm0, qword [BASE] - |.if not X64 - | movsd FPARG1, xmm0 - |.endif - | mov RB, BASE - | call extern log - | mov BASE, RB - | jmp ->fff_resfp - | - |.macro math_extern, func - | .ffunc_nsse math_ .. func - |.if not X64 - | movsd FPARG1, xmm0 - |.endif - | mov RB, BASE - | call extern func - | mov BASE, RB - | jmp ->fff_resfp - |.endmacro - | - |.macro math_extern2, func - | .ffunc_nnsse math_ .. func - |.if not X64 - | movsd FPARG1, xmm0 - | movsd FPARG3, xmm1 - |.endif - | mov RB, BASE - | call extern func - | mov BASE, RB - | jmp ->fff_resfp - |.endmacro - | - | math_extern log10 - | math_extern exp - | math_extern sin - | math_extern cos - | math_extern tan - | math_extern asin - | math_extern acos - | math_extern atan - | math_extern sinh - | math_extern cosh - | math_extern tanh - | math_extern2 pow - | math_extern2 atan2 - | math_extern2 fmod - | - |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn - | - |.ffunc_1 math_frexp - | mov RB, [BASE+4] - | cmp RB, LJ_TISNUM; jae ->fff_fallback - | mov PC, [BASE-4] - | mov RC, [BASE] - | mov [BASE-4], RB; mov [BASE-8], RC - | shl RB, 1; cmp RB, 0xffe00000; jae >3 - | or RC, RB; jz >3 - | mov RC, 1022 - | cmp RB, 0x00200000; jb >4 - |1: - | shr RB, 21; sub RB, RC // Extract and unbias exponent. - | cvtsi2sd xmm0, RB - | mov RB, [BASE-4] - | and RB, 0x800fffff // Mask off exponent. - | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0. - | mov [BASE-4], RB - |2: - | movsd qword [BASE], xmm0 - | mov RD, 1+2 - | jmp ->fff_res - |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0. - | xorps xmm0, xmm0; jmp <2 - |4: // Handle denormals by multiplying with 2^54 and adjusting the bias. - | movsd xmm0, qword [BASE] - | sseconst_hi xmm1, RBa, 43500000 // 2^54. - | mulsd xmm0, xmm1 - | movsd qword [BASE-8], xmm0 - | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1 - | - |.ffunc_nsse math_modf - | mov RB, [BASE+4] - | mov PC, [BASE-4] - | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf? - | movaps xmm4, xmm0 - | call ->vm_trunc_sse - | subsd xmm4, xmm0 - |1: - | movsd qword [BASE-8], xmm0 - | movsd qword [BASE], xmm4 - | mov RC, [BASE-4]; mov RB, [BASE+4] - | xor RC, RB; js >3 // Need to adjust sign? - |2: - | mov RD, 1+2 - | jmp ->fff_res - |3: - | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction. - | jmp <2 - |4: - | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0. - | - |.macro math_minmax, name, cmovop, sseop - | .ffunc_1 name - | mov RA, 2 - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne >4 - | mov RB, dword [BASE] - |1: // Handle integers. - | cmp RA, RD; jae ->fff_resi - | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3 - | cmp RB, dword [BASE+RA*8-8] - | cmovop RB, dword [BASE+RA*8-8] - | add RA, 1 - | jmp <1 - |3: - | ja ->fff_fallback - | // Convert intermediate result to number and continue below. - | cvtsi2sd xmm0, RB - | jmp >6 - |4: - | ja ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | - | movsd xmm0, qword [BASE] - |5: // Handle numbers or integers. - | cmp RA, RD; jae ->fff_resxmm0 - | cmp dword [BASE+RA*8-4], LJ_TISNUM - |.if DUALNUM - | jb >6 - | ja ->fff_fallback - | cvtsi2sd xmm1, dword [BASE+RA*8-8] - | jmp >7 - |.else - | jae ->fff_fallback - |.endif - |6: - | movsd xmm1, qword [BASE+RA*8-8] - |7: - | sseop xmm0, xmm1 - | add RA, 1 - | jmp <5 - |.endmacro - | - | math_minmax math_min, cmovg, minsd - | math_minmax math_max, cmovl, maxsd - | - |//-- String library ----------------------------------------------------- - | - |.ffunc string_byte // Only handle the 1-arg case here. - | cmp NARGS:RD, 1+1; jne ->fff_fallback - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | mov STR:RB, [BASE] - | mov PC, [BASE-4] - | cmp dword STR:RB->len, 1 - | jb ->fff_res0 // Return no results for empty string. - | movzx RB, byte STR:RB[1] - |.if DUALNUM - | jmp ->fff_resi - |.else - | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0 - |.endif - | - |.ffunc string_char // Only handle the 1-arg case here. - | ffgccheck - | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg. - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - | mov RB, dword [BASE] - | cmp RB, 255; ja ->fff_fallback - | mov TMP2, RB - |.else - | jae ->fff_fallback - | cvttsd2si RB, qword [BASE] - | cmp RB, 255; ja ->fff_fallback - | mov TMP2, RB - |.endif - |.if X64 - | mov TMP3, 1 - |.else - | mov ARG3, 1 - |.endif - | lea RDa, TMP2 // Points to stack. Little-endian. - |->fff_newstr: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - |.if X64 - | mov CARG3d, TMP3 // Zero-extended to size_t. - | mov CARG2, RDa // May be 64 bit ptr to stack. - | mov CARG1d, L:RB - |.else - | mov ARG2, RD - | mov ARG1, L:RB - |.endif - | mov SAVE_PC, PC - | call extern lj_str_new // (lua_State *L, char *str, size_t l) - |->fff_resstr: - | // GCstr * returned in eax (RD). - | mov BASE, L:RB->base - | mov PC, [BASE-4] - | mov dword [BASE-4], LJ_TSTR - | mov [BASE-8], STR:RD - | jmp ->fff_res1 - | - |.ffunc string_sub - | ffgccheck - | mov TMP2, -1 - | cmp NARGS:RD, 1+2; jb ->fff_fallback - | jna >1 - | cmp dword [BASE+20], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - | mov RB, dword [BASE+16] - | mov TMP2, RB - |.else - | jae ->fff_fallback - | cvttsd2si RB, qword [BASE+16] - | mov TMP2, RB - |.endif - |1: - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | cmp dword [BASE+12], LJ_TISNUM - |.if DUALNUM - | jne ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | mov STR:RB, [BASE] - | mov TMP3, STR:RB - | mov RB, STR:RB->len - |.if DUALNUM - | mov RA, dword [BASE+8] - |.else - | cvttsd2si RA, qword [BASE+8] - |.endif - | mov RC, TMP2 - | cmp RB, RC // len < end? (unsigned compare) - | jb >5 - |2: - | test RA, RA // start <= 0? - | jle >7 - |3: - | mov STR:RB, TMP3 - | sub RC, RA // start > end? - | jl ->fff_emptystr - | lea RB, [STR:RB+RA+#STR-1] - | add RC, 1 - |4: - |.if X64 - | mov TMP3, RC - |.else - | mov ARG3, RC - |.endif - | mov RD, RB - | jmp ->fff_newstr - | - |5: // Negative end or overflow. - | jl >6 - | lea RC, [RC+RB+1] // end = end+(len+1) - | jmp <2 - |6: // Overflow. - | mov RC, RB // end = len - | jmp <2 - | - |7: // Negative start or underflow. - | je >8 - | add RA, RB // start = start+(len+1) - | add RA, 1 - | jg <3 // start > 0? - |8: // Underflow. - | mov RA, 1 // start = 1 - | jmp <3 - | - |->fff_emptystr: // Range underflow. - | xor RC, RC // Zero length. Any ptr in RB is ok. - | jmp <4 - | - |.macro ffstring_op, name - | .ffunc_1 string_ .. name - | ffgccheck - | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback - | mov L:RB, SAVE_L - | lea SBUF:FCARG1, [DISPATCH+DISPATCH_GL(tmpbuf)] - | mov L:RB->base, BASE - | mov STR:FCARG2, [BASE] // Caveat: FCARG2 == BASE - | mov RCa, SBUF:FCARG1->b - | mov SBUF:FCARG1->L, L:RB - | mov SBUF:FCARG1->w, RCa - | mov SAVE_PC, PC - | call extern lj_buf_putstr_ .. name .. @8 - | mov FCARG1, eax - | call extern lj_buf_tostr@4 - | jmp ->fff_resstr - |.endmacro - | - |ffstring_op reverse - |ffstring_op lower - |ffstring_op upper - | - |//-- Bit library -------------------------------------------------------- - | - |.macro .ffunc_bit, name, kind, fdef - | fdef name - |.if kind == 2 - | sseconst_tobit xmm1, RBa - |.endif - | cmp dword [BASE+4], LJ_TISNUM - |.if DUALNUM - | jne >1 - | mov RB, dword [BASE] - |.if kind > 0 - | jmp >2 - |.else - | jmp ->fff_resbit - |.endif - |1: - | ja ->fff_fallback - |.else - | jae ->fff_fallback - |.endif - | movsd xmm0, qword [BASE] - |.if kind < 2 - | sseconst_tobit xmm1, RBa - |.endif - | addsd xmm0, xmm1 - | movd RB, xmm0 - |2: - |.endmacro - | - |.macro .ffunc_bit, name, kind - | .ffunc_bit name, kind, .ffunc_1 - |.endmacro - | - |.ffunc_bit bit_tobit, 0 - | jmp ->fff_resbit - | - |.macro .ffunc_bit_op, name, ins - | .ffunc_bit name, 2 - | mov TMP2, NARGS:RD // Save for fallback. - | lea RD, [BASE+NARGS:RD*8-16] - |1: - | cmp RD, BASE - | jbe ->fff_resbit - | cmp dword [RD+4], LJ_TISNUM - |.if DUALNUM - | jne >2 - | ins RB, dword [RD] - | sub RD, 8 - | jmp <1 - |2: - | ja ->fff_fallback_bit_op - |.else - | jae ->fff_fallback_bit_op - |.endif - | movsd xmm0, qword [RD] - | addsd xmm0, xmm1 - | movd RA, xmm0 - | ins RB, RA - | sub RD, 8 - | jmp <1 - |.endmacro - | - |.ffunc_bit_op bit_band, and - |.ffunc_bit_op bit_bor, or - |.ffunc_bit_op bit_bxor, xor - | - |.ffunc_bit bit_bswap, 1 - | bswap RB - | jmp ->fff_resbit - | - |.ffunc_bit bit_bnot, 1 - | not RB - |.if DUALNUM - | jmp ->fff_resbit - |.else - |->fff_resbit: - | cvtsi2sd xmm0, RB - | jmp ->fff_resxmm0 - |.endif - | - |->fff_fallback_bit_op: - | mov NARGS:RD, TMP2 // Restore for fallback - | jmp ->fff_fallback - | - |.macro .ffunc_bit_sh, name, ins - |.if DUALNUM - | .ffunc_bit name, 1, .ffunc_2 - | // Note: no inline conversion from number for 2nd argument! - | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback - | mov RA, dword [BASE+8] - |.else - | .ffunc_nnsse name - | sseconst_tobit xmm2, RBa - | addsd xmm0, xmm2 - | addsd xmm1, xmm2 - | movd RB, xmm0 - | movd RA, xmm1 - |.endif - | ins RB, cl // Assumes RA is ecx. - | jmp ->fff_resbit - |.endmacro - | - |.ffunc_bit_sh bit_lshift, shl - |.ffunc_bit_sh bit_rshift, shr - |.ffunc_bit_sh bit_arshift, sar - |.ffunc_bit_sh bit_rol, rol - |.ffunc_bit_sh bit_ror, ror - | - |//----------------------------------------------------------------------- - | - |->fff_fallback_2: - | mov NARGS:RD, 1+2 // Other args are ignored, anyway. - | jmp ->fff_fallback - |->fff_fallback_1: - | mov NARGS:RD, 1+1 // Other args are ignored, anyway. - |->fff_fallback: // Call fast function fallback handler. - | // BASE = new base, RD = nargs+1 - | mov L:RB, SAVE_L - | mov PC, [BASE-4] // Fallback may overwrite PC. - | mov SAVE_PC, PC // Redundant (but a defined value). - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler. - | mov L:RB->top, RD - | mov CFUNC:RD, [BASE-8] - | cmp RA, L:RB->maxstack - | ja >5 // Need to grow stack. - |.if X64 - | mov CARG1d, L:RB - |.else - | mov ARG1, L:RB - |.endif - | call aword CFUNC:RD->f // (lua_State *L) - | mov BASE, L:RB->base - | // Either throws an error, or recovers and returns -1, 0 or nresults+1. - | test RD, RD; jg ->fff_res // Returned nresults+1? - |1: - | mov RA, L:RB->top - | sub RA, BASE - | shr RA, 3 - | test RD, RD - | lea NARGS:RD, [RA+1] - | mov LFUNC:RB, [BASE-8] - | jne ->vm_call_tail // Returned -1? - | ins_callt // Returned 0: retry fast path. - | - |// Reconstruct previous base for vmeta_call during tailcall. - |->vm_call_tail: - | mov RA, BASE - | test PC, FRAME_TYPE - | jnz >3 - | movzx RB, PC_RA - | not RBa // Note: ~RB = -(RB+1) - | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8 - | jmp ->vm_call_dispatch // Resolve again for tailcall. - |3: - | mov RB, PC - | and RB, -8 - | sub BASE, RB - | jmp ->vm_call_dispatch // Resolve again for tailcall. - | - |5: // Grow stack for fallback handler. - | mov FCARG2, LUA_MINSTACK - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->base - | xor RD, RD // Simulate a return 0. - | jmp <1 // Dumb retry (goes through ff first). - | - |->fff_gcstep: // Call GC step function. - | // BASE = new base, RD = nargs+1 - | pop RBa // Must keep stack at same level. - | mov TMPa, RBa // Save return address - | mov L:RB, SAVE_L - | mov SAVE_PC, PC // Redundant (but a defined value). - | mov L:RB->base, BASE - | lea RD, [BASE+NARGS:RD*8-8] - | mov FCARG1, L:RB - | mov L:RB->top, RD - | call extern lj_gc_step@4 // (lua_State *L) - | mov BASE, L:RB->base - | mov RD, L:RB->top - | sub RD, BASE - | shr RD, 3 - | add NARGS:RD, 1 - | mov RBa, TMPa - | push RBa // Restore return address. - | ret - | - |//----------------------------------------------------------------------- - |//-- Special dispatch targets ------------------------------------------- - |//----------------------------------------------------------------------- - | - |->vm_record: // Dispatch target for recording phase. - |.if JIT - | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_VMEVENT // No recording while in vmevent. - | jnz >5 - | // Decrement the hookcount for consistency, but always do the call. - | test RDL, HOOK_ACTIVE - | jnz >1 - | test RDL, LUA_MASKLINE|LUA_MASKCOUNT - | jz >1 - | dec dword [DISPATCH+DISPATCH_GL(hookcount)] - | jmp >1 - |.endif - | - |->vm_rethook: // Dispatch target for return hooks. - | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_ACTIVE // Hook already active? - | jnz >5 - | jmp >1 - | - |->vm_inshook: // Dispatch target for instr/line hooks. - | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)] - | test RDL, HOOK_ACTIVE // Hook already active? - | jnz >5 - | - | test RDL, LUA_MASKLINE|LUA_MASKCOUNT - | jz >5 - | dec dword [DISPATCH+DISPATCH_GL(hookcount)] - | jz >1 - | test RDL, LUA_MASKLINE - | jz >5 - |1: - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG2, PC // Caveat: FCARG2 == BASE - | mov FCARG1, L:RB - | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC. - | call extern lj_dispatch_ins@8 // (lua_State *L, const BCIns *pc) - |3: - | mov BASE, L:RB->base - |4: - | movzx RA, PC_RA - |5: - | movzx OP, PC_OP - | movzx RD, PC_RD - |.if X64 - | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins. - |.else - | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins. - |.endif - | - |->cont_hook: // Continue from hook yield. - | add PC, 4 - | mov RA, [RB-24] - | mov MULTRES, RA // Restore MULTRES for *M ins. - | jmp <4 - | - |->vm_hotloop: // Hot loop counter underflow. - |.if JIT - | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L). - | mov RB, LFUNC:RB->pc - | movzx RD, byte [RB+PC2PROTO(framesize)] - | lea RD, [BASE+RD*8] - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov FCARG2, PC - | lea FCARG1, [DISPATCH+GG_DISP2J] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa - | mov SAVE_PC, PC - | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc) - | jmp <3 - |.endif - | - |->vm_callhook: // Dispatch target for call hooks. - | mov SAVE_PC, PC - |.if JIT - | jmp >1 - |.endif - | - |->vm_hotcall: // Hot call counter underflow. - |.if JIT - | mov SAVE_PC, PC - | or PC, 1 // Marker for hot call. - |1: - |.endif - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov L:RB->top, RD - | mov FCARG2, PC - | mov FCARG1, L:RB - | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc) - | // ASMFunction returned in eax/rax (RDa). - | mov SAVE_PC, 0 // Invalidate for subsequent line hook. - |.if JIT - | and PC, -2 - |.endif - | mov BASE, L:RB->base - | mov RAa, RDa - | mov RD, L:RB->top - | sub RD, BASE - | mov RBa, RAa - | movzx RA, PC_RA - | shr RD, 3 - | add NARGS:RD, 1 - | jmp RBa - | - |->cont_stitch: // Trace stitching. - |.if JIT - | // BASE = base, RC = result, RB = mbase - | mov TRACE:RA, [RB-24] // Save previous trace. - | mov TMP1, TRACE:RA - | mov TMP3, DISPATCH // Need one more register. - | mov DISPATCH, MULTRES - | movzx RA, PC_RA - | lea RA, [BASE+RA*8] // Call base. - | sub DISPATCH, 1 - | jz >2 - |1: // Move results down. - |.if X64 - | mov RBa, [RC] - | mov [RA], RBa - |.else - | mov RB, [RC] - | mov [RA], RB - | mov RB, [RC+4] - | mov [RA+4], RB - |.endif - | add RC, 8 - | add RA, 8 - | sub DISPATCH, 1 - | jnz <1 - |2: - | movzx RC, PC_RA - | movzx RB, PC_RB - | add RC, RB - | lea RC, [BASE+RC*8-8] - |3: - | cmp RC, RA - | ja >9 // More results wanted? - | - | mov DISPATCH, TMP3 - | mov TRACE:RD, TMP1 // Get previous trace. - | movzx RB, word TRACE:RD->traceno - | movzx RD, word TRACE:RD->link - | cmp RD, RB - | je ->cont_nop // Blacklisted. - | test RD, RD - | jne =>BC_JLOOP // Jump to stitched trace. - | - | // Stitch a new trace to the previous trace. - | mov [DISPATCH+DISPATCH_J(exitno)], RB - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG2, PC - | lea FCARG1, [DISPATCH+GG_DISP2J] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa - | call extern lj_dispatch_stitch@8 // (jit_State *J, const BCIns *pc) - | mov BASE, L:RB->base - | jmp ->cont_nop - | - |9: // Fill up results with nil. - | mov dword [RA+4], LJ_TNIL - | add RA, 8 - | jmp <3 - |.endif - | - |->vm_profhook: // Dispatch target for profiler hook. -#if LJ_HASPROFILE - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov FCARG2, PC // Caveat: FCARG2 == BASE - | mov FCARG1, L:RB - | call extern lj_dispatch_profile@8 // (lua_State *L, const BCIns *pc) - | mov BASE, L:RB->base - | // HOOK_PROFILE is off again, so re-dispatch to dynamic instruction. - | sub PC, 4 - | jmp ->cont_nop -#endif - | - |//----------------------------------------------------------------------- - |//-- Trace exit handler ------------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Called from an exit stub with the exit number on the stack. - |// The 16 bit exit number is stored with two (sign-extended) push imm8. - |->vm_exit_handler: - |.if JIT - |.if X64 - | push r13; push r12 - | push r11; push r10; push r9; push r8 - | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp - | push rbx; push rdx; push rcx; push rax - | movzx RC, byte [rbp-8] // Reconstruct exit number. - | mov RCH, byte [rbp-16] - | mov [rbp-8], r15; mov [rbp-16], r14 - |.else - | push ebp; lea ebp, [esp+12]; push ebp - | push ebx; push edx; push ecx; push eax - | movzx RC, byte [ebp-4] // Reconstruct exit number. - | mov RCH, byte [ebp-8] - | mov [ebp-4], edi; mov [ebp-8], esi - |.endif - | // Caveat: DISPATCH is ebx. - | mov DISPATCH, [ebp] - | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number. - | set_vmstate EXIT - | mov [DISPATCH+DISPATCH_J(exitno)], RC - | mov [DISPATCH+DISPATCH_J(parent)], RA - |.if X64 - |.if X64WIN - | sub rsp, 16*8+4*8 // Room for SSE regs + save area. - |.else - | sub rsp, 16*8 // Room for SSE regs. - |.endif - | add rbp, -128 - | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14 - | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12 - | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10 - | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8 - | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6 - | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4 - | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2 - | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0 - |.else - | sub esp, 8*8+16 // Room for SSE regs + args. - | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6 - | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4 - | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2 - | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0 - |.endif - | // Caveat: RB is ebp. - | mov L:RB, [DISPATCH+DISPATCH_GL(cur_L)] - | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)] - | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa - | mov L:RB->base, BASE - |.if X64WIN - | lea CARG2, [rsp+4*8] - |.elif X64 - | mov CARG2, rsp - |.else - | lea FCARG2, [esp+16] - |.endif - | lea FCARG1, [DISPATCH+GG_DISP2J] - | mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0 - | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex) - | // MULTRES or negated error code returned in eax (RD). - | mov RAa, L:RB->cframe - | and RAa, CFRAME_RAWMASK - |.if X64WIN - | // Reposition stack later. - |.elif X64 - | mov rsp, RAa // Reposition stack to C frame. - |.else - | mov esp, RAa // Reposition stack to C frame. - |.endif - | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield). - | mov BASE, L:RB->base - | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC. - |.if X64 - | jmp >1 - |.endif - |.endif - |->vm_exit_interp: - | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set. - |.if JIT - |.if X64 - | // Restore additional callee-save registers only used in compiled code. - |.if X64WIN - | lea RAa, [rsp+9*16+4*8] - |1: - | movdqa xmm15, [RAa-9*16] - | movdqa xmm14, [RAa-8*16] - | movdqa xmm13, [RAa-7*16] - | movdqa xmm12, [RAa-6*16] - | movdqa xmm11, [RAa-5*16] - | movdqa xmm10, [RAa-4*16] - | movdqa xmm9, [RAa-3*16] - | movdqa xmm8, [RAa-2*16] - | movdqa xmm7, [RAa-1*16] - | mov rsp, RAa // Reposition stack to C frame. - | movdqa xmm6, [RAa] - | mov r15, CSAVE_3 - | mov r14, CSAVE_4 - |.else - | add rsp, 16 // Reposition stack to C frame. - |1: - |.endif - | mov r13, TMPa - | mov r12, TMPQ - |.endif - | test RD, RD; js >9 // Check for error from exit. - | mov L:RB, SAVE_L - | mov MULTRES, RD - | mov LFUNC:KBASE, [BASE-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | mov L:RB->base, BASE - | mov dword [DISPATCH+DISPATCH_GL(jit_base)], 0 - | set_vmstate INTERP - | // Modified copy of ins_next which handles function header dispatch, too. - | mov RC, [PC] - | movzx RA, RCH - | movzx OP, RCL - | add PC, 4 - | shr RC, 16 - | cmp OP, BC_FUNCF // Function header? - | jb >3 - | cmp OP, BC_FUNCC+2 // Fast function? - | jae >4 - |2: - | mov RC, MULTRES // RC/RD holds nres+1. - |3: - |.if X64 - | jmp aword [DISPATCH+OP*8] - |.else - | jmp aword [DISPATCH+OP*4] - |.endif - | - |4: // Check frame below fast function. - | mov RC, [BASE-4] - | test RC, FRAME_TYPE - | jnz <2 // Trace stitching continuation? - | // Otherwise set KBASE for Lua function below fast function. - | movzx RC, byte [RC-3] - | not RCa - | mov LFUNC:KBASE, [BASE+RC*8-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | jmp <2 - | - |9: // Rethrow error from the right C frame. - | mov FCARG2, RD - | mov FCARG1, L:RB - | neg FCARG2 - | call extern lj_err_trace@8 // (lua_State *L, int errcode) - |.endif - | - |//----------------------------------------------------------------------- - |//-- Math helper functions ---------------------------------------------- - |//----------------------------------------------------------------------- - | - |// FP value rounding. Called by math.floor/math.ceil fast functions - |// and from JIT code. arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified. - |.macro vm_round, name, mode, cond - |->name: - |.if not X64 and cond - | movsd xmm0, qword [esp+4] - | call ->name .. _sse - | movsd qword [esp+4], xmm0 // Overwrite callee-owned arg. - | fld qword [esp+4] - | ret - |.endif - | - |->name .. _sse: - | sseconst_abs xmm2, RDa - | sseconst_2p52 xmm3, RDa - | movaps xmm1, xmm0 - | andpd xmm1, xmm2 // |x| - | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|. - | jbe >1 - | andnpd xmm2, xmm0 // Isolate sign bit. - |.if mode == 2 // trunc(x)? - | movaps xmm0, xmm1 - | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52 - | subsd xmm1, xmm3 - | sseconst_1 xmm3, RDa - | cmpsd xmm0, xmm1, 1 // |x| < result? - | andpd xmm0, xmm3 - | subsd xmm1, xmm0 // If yes, subtract -1. - | orpd xmm1, xmm2 // Merge sign bit back in. - |.else - | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52 - | subsd xmm1, xmm3 - | orpd xmm1, xmm2 // Merge sign bit back in. - | sseconst_1 xmm3, RDa - | .if mode == 1 // ceil(x)? - | cmpsd xmm0, xmm1, 6 // x > result? - | andpd xmm0, xmm3 - | addsd xmm1, xmm0 // If yes, add 1. - | orpd xmm1, xmm2 // Merge sign bit back in (again). - | .else // floor(x)? - | cmpsd xmm0, xmm1, 1 // x < result? - | andpd xmm0, xmm3 - | subsd xmm1, xmm0 // If yes, subtract 1. - | .endif - |.endif - | movaps xmm0, xmm1 - |1: - | ret - |.endmacro - | - | vm_round vm_floor, 0, 1 - | vm_round vm_ceil, 1, JIT - | vm_round vm_trunc, 2, JIT - | - |// FP modulo x%y. Called by BC_MOD* and vm_arith. - |->vm_mod: - |// Args in xmm0/xmm1, return value in xmm0. - |// Caveat: xmm0-xmm5 and RC (eax) modified! - | movaps xmm5, xmm0 - | divsd xmm0, xmm1 - | sseconst_abs xmm2, RDa - | sseconst_2p52 xmm3, RDa - | movaps xmm4, xmm0 - | andpd xmm4, xmm2 // |x/y| - | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|. - | jbe >1 - | andnpd xmm2, xmm0 // Isolate sign bit. - | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52 - | subsd xmm4, xmm3 - | orpd xmm4, xmm2 // Merge sign bit back in. - | sseconst_1 xmm2, RDa - | cmpsd xmm0, xmm4, 1 // x/y < result? - | andpd xmm0, xmm2 - | subsd xmm4, xmm0 // If yes, subtract 1.0. - | movaps xmm0, xmm5 - | mulsd xmm1, xmm4 - | subsd xmm0, xmm1 - | ret - |1: - | mulsd xmm1, xmm0 - | movaps xmm0, xmm5 - | subsd xmm0, xmm1 - | ret - | - |//----------------------------------------------------------------------- - |//-- Miscellaneous functions -------------------------------------------- - |//----------------------------------------------------------------------- - | - |// int lj_vm_cpuid(uint32_t f, uint32_t res[4]) - |->vm_cpuid: - |.if X64 - | mov eax, CARG1d - | .if X64WIN; push rsi; mov rsi, CARG2; .endif - | push rbx - | xor ecx, ecx - | cpuid - | mov [rsi], eax - | mov [rsi+4], ebx - | mov [rsi+8], ecx - | mov [rsi+12], edx - | pop rbx - | .if X64WIN; pop rsi; .endif - | ret - |.else - | pushfd - | pop edx - | mov ecx, edx - | xor edx, 0x00200000 // Toggle ID bit in flags. - | push edx - | popfd - | pushfd - | pop edx - | xor eax, eax // Zero means no features supported. - | cmp ecx, edx - | jz >1 // No ID toggle means no CPUID support. - | mov eax, [esp+4] // Argument 1 is function number. - | push edi - | push ebx - | xor ecx, ecx - | cpuid - | mov edi, [esp+16] // Argument 2 is result area. - | mov [edi], eax - | mov [edi+4], ebx - | mov [edi+8], ecx - | mov [edi+12], edx - | pop ebx - | pop edi - |1: - | ret - |.endif - | - |.define NEXT_TAB, TAB:FCARG1 - |.define NEXT_IDX, FCARG2 - |.define NEXT_PTR, RCa - |.define NEXT_PTRd, RC - |.macro NEXT_RES_IDXL, op2; lea edx, [NEXT_IDX+op2]; .endmacro - |.if X64 - |.define NEXT_TMP, CARG3d - |.define NEXT_TMPq, CARG3 - |.define NEXT_ASIZE, CARG4d - |.macro NEXT_ENTER; .endmacro - |.macro NEXT_LEAVE; ret; .endmacro - |.if X64WIN - |.define NEXT_RES_PTR, [rsp+aword*5] - |.macro NEXT_RES_IDX, op2; add NEXT_IDX, op2; .endmacro - |.else - |.define NEXT_RES_PTR, [rsp+aword*1] - |.macro NEXT_RES_IDX, op2; lea edx, [NEXT_IDX+op2]; .endmacro - |.endif - |.else - |.define NEXT_ASIZE, esi - |.define NEXT_TMP, edi - |.macro NEXT_ENTER; push esi; push edi; .endmacro - |.macro NEXT_LEAVE; pop edi; pop esi; ret; .endmacro - |.define NEXT_RES_PTR, [esp+dword*3] - |.macro NEXT_RES_IDX, op2; add NEXT_IDX, op2; .endmacro - |.endif - | - |// TValue *lj_vm_next(GCtab *t, uint32_t idx) - |// Next idx returned in edx. - |->vm_next: - |.if JIT - | NEXT_ENTER - | mov NEXT_ASIZE, NEXT_TAB->asize - |1: // Traverse array part. - | cmp NEXT_IDX, NEXT_ASIZE; jae >5 - | mov NEXT_TMP, NEXT_TAB->array - | cmp dword [NEXT_TMP+NEXT_IDX*8+4], LJ_TNIL; je >2 - | lea NEXT_PTR, NEXT_RES_PTR - |.if X64 - | mov NEXT_TMPq, qword [NEXT_TMP+NEXT_IDX*8] - | mov qword [NEXT_PTR], NEXT_TMPq - |.else - | mov NEXT_ASIZE, dword [NEXT_TMP+NEXT_IDX*8+4] - | mov NEXT_TMP, dword [NEXT_TMP+NEXT_IDX*8] - | mov dword [NEXT_PTR+4], NEXT_ASIZE - | mov dword [NEXT_PTR], NEXT_TMP - |.endif - |.if DUALNUM - | mov dword [NEXT_PTR+dword*3], LJ_TISNUM - | mov dword [NEXT_PTR+dword*2], NEXT_IDX - |.else - | cvtsi2sd xmm0, NEXT_IDX - | movsd qword [NEXT_PTR+dword*2], xmm0 - |.endif - | NEXT_RES_IDX 1 - | NEXT_LEAVE - |2: // Skip holes in array part. - | add NEXT_IDX, 1 - | jmp <1 - | - |5: // Traverse hash part. - | sub NEXT_IDX, NEXT_ASIZE - |6: - | cmp NEXT_IDX, NEXT_TAB->hmask; ja >9 - | imul NEXT_PTRd, NEXT_IDX, #NODE - | add NODE:NEXT_PTRd, dword NEXT_TAB->node - | cmp dword NODE:NEXT_PTR->val.it, LJ_TNIL; je >7 - | NEXT_RES_IDXL NEXT_ASIZE+1 - | NEXT_LEAVE - |7: // Skip holes in hash part. - | add NEXT_IDX, 1 - | jmp <6 - | - |9: // End of iteration. Set the key to nil (not the value). - | NEXT_RES_IDX NEXT_ASIZE - | lea NEXT_PTR, NEXT_RES_PTR - | mov dword [NEXT_PTR+dword*3], LJ_TNIL - | NEXT_LEAVE - |.endif - | - |//----------------------------------------------------------------------- - |//-- Assertions --------------------------------------------------------- - |//----------------------------------------------------------------------- - | - |->assert_bad_for_arg_type: -#ifdef LUA_USE_ASSERT - | int3 -#endif - | int3 - | - |//----------------------------------------------------------------------- - |//-- FFI helper functions ----------------------------------------------- - |//----------------------------------------------------------------------- - | - |// Handler for callback functions. Callback slot number in ah/al. - |->vm_ffi_callback: - |.if FFI - |.type CTSTATE, CTState, PC - |.if not X64 - | sub esp, 16 // Leave room for SAVE_ERRF etc. - |.endif - | saveregs_ // ebp/rbp already saved. ebp now holds global_State *. - | lea DISPATCH, [ebp+GG_G2DISP] - | mov CTSTATE, GL:ebp->ctype_state - | movzx eax, ax - | mov CTSTATE->cb.slot, eax - |.if X64 - | mov CTSTATE->cb.gpr[0], CARG1 - | mov CTSTATE->cb.gpr[1], CARG2 - | mov CTSTATE->cb.gpr[2], CARG3 - | mov CTSTATE->cb.gpr[3], CARG4 - | movsd qword CTSTATE->cb.fpr[0], xmm0 - | movsd qword CTSTATE->cb.fpr[1], xmm1 - | movsd qword CTSTATE->cb.fpr[2], xmm2 - | movsd qword CTSTATE->cb.fpr[3], xmm3 - |.if X64WIN - | lea rax, [rsp+CFRAME_SIZE+4*8] - |.else - | lea rax, [rsp+CFRAME_SIZE] - | mov CTSTATE->cb.gpr[4], CARG5 - | mov CTSTATE->cb.gpr[5], CARG6 - | movsd qword CTSTATE->cb.fpr[4], xmm4 - | movsd qword CTSTATE->cb.fpr[5], xmm5 - | movsd qword CTSTATE->cb.fpr[6], xmm6 - | movsd qword CTSTATE->cb.fpr[7], xmm7 - |.endif - | mov CTSTATE->cb.stack, rax - | mov CARG2, rsp - |.else - | lea eax, [esp+CFRAME_SIZE+16] - | mov CTSTATE->cb.gpr[0], FCARG1 - | mov CTSTATE->cb.gpr[1], FCARG2 - | mov CTSTATE->cb.stack, eax - | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp. - | mov FCARG2, [esp+CFRAME_SIZE+8] - | mov SAVE_RET, FCARG1 - | mov SAVE_R4, FCARG2 - | mov FCARG2, esp - |.endif - | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok. - | mov FCARG1, CTSTATE - | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf) - | // lua_State * returned in eax (RD). - | set_vmstate INTERP - | mov BASE, L:RD->base - | mov RD, L:RD->top - | sub RD, BASE - | mov LFUNC:RB, [BASE-8] - | shr RD, 3 - | add RD, 1 - | ins_callt - |.endif - | - |->cont_ffi_callback: // Return from FFI callback. - |.if FFI - | mov L:RA, SAVE_L - | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)] - | mov aword CTSTATE->L, L:RAa - | mov L:RA->base, BASE - | mov L:RA->top, RB - | mov FCARG1, CTSTATE - | mov FCARG2, RC - | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o) - |.if X64 - | mov rax, CTSTATE->cb.gpr[0] - | movsd xmm0, qword CTSTATE->cb.fpr[0] - | jmp ->vm_leave_unw - |.else - | mov L:RB, SAVE_L - | mov eax, CTSTATE->cb.gpr[0] - | mov edx, CTSTATE->cb.gpr[1] - | cmp dword CTSTATE->cb.gpr[2], 1 - | jb >7 - | je >6 - | fld qword CTSTATE->cb.fpr[0].d - | jmp >7 - |6: - | fld dword CTSTATE->cb.fpr[0].f - |7: - | mov ecx, L:RB->top - | movzx ecx, word [ecx+6] // Get stack adjustment and copy up. - | mov SAVE_L, ecx // Must be one slot above SAVE_RET - | restoreregs - | pop ecx // Move return addr from SAVE_RET. - | add esp, [esp] // Adjust stack. - | add esp, 16 - | push ecx - | ret - |.endif - |.endif - | - |->vm_ffi_call@4: // Call C function via FFI. - | // Caveat: needs special frame unwinding, see below. - |.if FFI - |.if X64 - | .type CCSTATE, CCallState, rbx - | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1 - |.else - | .type CCSTATE, CCallState, ebx - | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1 - |.endif - | - | // Readjust stack. - |.if X64 - | mov eax, CCSTATE->spadj - | sub rsp, rax - |.else - | sub esp, CCSTATE->spadj - |.if WIN - | mov CCSTATE->spadj, esp - |.endif - |.endif - | - | // Copy stack slots. - | movzx ecx, byte CCSTATE->nsp - | sub ecx, 1 - | js >2 - |1: - |.if X64 - | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)] - | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax - |.else - | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)] - | mov [esp+ecx*4], eax - |.endif - | sub ecx, 1 - | jns <1 - |2: - | - |.if X64 - | movzx eax, byte CCSTATE->nfpr - | mov CARG1, CCSTATE->gpr[0] - | mov CARG2, CCSTATE->gpr[1] - | mov CARG3, CCSTATE->gpr[2] - | mov CARG4, CCSTATE->gpr[3] - |.if not X64WIN - | mov CARG5, CCSTATE->gpr[4] - | mov CARG6, CCSTATE->gpr[5] - |.endif - | test eax, eax; jz >5 - | movaps xmm0, CCSTATE->fpr[0] - | movaps xmm1, CCSTATE->fpr[1] - | movaps xmm2, CCSTATE->fpr[2] - | movaps xmm3, CCSTATE->fpr[3] - |.if not X64WIN - | cmp eax, 4; jbe >5 - | movaps xmm4, CCSTATE->fpr[4] - | movaps xmm5, CCSTATE->fpr[5] - | movaps xmm6, CCSTATE->fpr[6] - | movaps xmm7, CCSTATE->fpr[7] - |.endif - |5: - |.else - | mov FCARG1, CCSTATE->gpr[0] - | mov FCARG2, CCSTATE->gpr[1] - |.endif - | - | call aword CCSTATE->func - | - |.if X64 - | mov CCSTATE->gpr[0], rax - | movaps CCSTATE->fpr[0], xmm0 - |.if not X64WIN - | mov CCSTATE->gpr[1], rdx - | movaps CCSTATE->fpr[1], xmm1 - |.endif - |.else - | mov CCSTATE->gpr[0], eax - | mov CCSTATE->gpr[1], edx - | cmp byte CCSTATE->resx87, 1 - | jb >7 - | je >6 - | fstp qword CCSTATE->fpr[0].d[0] - | jmp >7 - |6: - | fstp dword CCSTATE->fpr[0].f[0] - |7: - |.if WIN - | sub CCSTATE->spadj, esp - |.endif - |.endif - | - |.if X64 - | mov rbx, [rbp-8]; leave; ret - |.else - | mov ebx, [ebp-4]; leave; ret - |.endif - |.endif - |// Note: vm_ffi_call must be the last function in this object file! - | - |//----------------------------------------------------------------------- -} - -/* Generate the code for a single instruction. */ -static void build_ins(BuildCtx *ctx, BCOp op, int defop) -{ - int vk = 0; - |// Note: aligning all instructions does not pay off. - |=>defop: - - switch (op) { - - /* -- Comparison ops ---------------------------------------------------- */ - - /* Remember: all ops branch for a true comparison, fall through otherwise. */ - - |.macro jmp_comp, lt, ge, le, gt, target - ||switch (op) { - ||case BC_ISLT: - | lt target - ||break; - ||case BC_ISGE: - | ge target - ||break; - ||case BC_ISLE: - | le target - ||break; - ||case BC_ISGT: - | gt target - ||break; - ||default: break; /* Shut up GCC. */ - ||} - |.endmacro - - case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: - | // RA = src1, RD = src2, JMP with RD = target - | ins_AD - |.if DUALNUM - | checkint RA, >7 - | checkint RD, >8 - | mov RB, dword [BASE+RA*8] - | add PC, 4 - | cmp RB, dword [BASE+RD*8] - | jmp_comp jge, jl, jg, jle, >9 - |6: - | movzx RD, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RA is not an integer. - | ja ->vmeta_comp - | // RA is a number. - | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp - | // RA is a number, RD is an integer. - | cvtsi2sd xmm0, dword [BASE+RD*8] - | jmp >2 - | - |8: // RA is an integer, RD is not an integer. - | ja ->vmeta_comp - | // RA is an integer, RD is a number. - | cvtsi2sd xmm1, dword [BASE+RA*8] - | movsd xmm0, qword [BASE+RD*8] - | add PC, 4 - | ucomisd xmm0, xmm1 - | jmp_comp jbe, ja, jb, jae, <9 - | jmp <6 - |.else - | checknum RA, ->vmeta_comp - | checknum RD, ->vmeta_comp - |.endif - |1: - | movsd xmm0, qword [BASE+RD*8] - |2: - | add PC, 4 - | ucomisd xmm0, qword [BASE+RA*8] - |3: - | // Unordered: all of ZF CF PF set, ordered: PF clear. - | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't. - |.if DUALNUM - | jmp_comp jbe, ja, jb, jae, <9 - | jmp <6 - |.else - | jmp_comp jbe, ja, jb, jae, >1 - | movzx RD, PC_RD - | branchPC RD - |1: - | ins_next - |.endif - break; - - case BC_ISEQV: case BC_ISNEV: - vk = op == BC_ISEQV; - | ins_AD // RA = src1, RD = src2, JMP with RD = target - | mov RB, [BASE+RD*8+4] - | add PC, 4 - |.if DUALNUM - | cmp RB, LJ_TISNUM; jne >7 - | checkint RA, >8 - | mov RB, dword [BASE+RD*8] - | cmp RB, dword [BASE+RA*8] - if (vk) { - | jne >9 - } else { - | je >9 - } - | movzx RD, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RD is not an integer. - | ja >5 - | // RD is a number. - | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5 - | // RD is a number, RA is an integer. - | cvtsi2sd xmm0, dword [BASE+RA*8] - | jmp >2 - | - |8: // RD is an integer, RA is not an integer. - | ja >5 - | // RD is an integer, RA is a number. - | cvtsi2sd xmm0, dword [BASE+RD*8] - | ucomisd xmm0, qword [BASE+RA*8] - | jmp >4 - | - |.else - | cmp RB, LJ_TISNUM; jae >5 - | checknum RA, >5 - |.endif - |1: - | movsd xmm0, qword [BASE+RA*8] - |2: - | ucomisd xmm0, qword [BASE+RD*8] - |4: - iseqne_fp: - if (vk) { - | jp >2 // Unordered means not equal. - | jne >2 - } else { - | jp >2 // Unordered means not equal. - | je >1 - } - iseqne_end: - if (vk) { - |1: // EQ: Branch to the target. - | movzx RD, PC_RD - | branchPC RD - |2: // NE: Fallthrough to next instruction. - |.if not FFI - |3: - |.endif - } else { - |.if not FFI - |3: - |.endif - |2: // NE: Branch to the target. - | movzx RD, PC_RD - | branchPC RD - |1: // EQ: Fallthrough to next instruction. - } - if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV || - op == BC_ISEQN || op == BC_ISNEN)) { - | jmp <9 - } else { - | ins_next - } - | - if (op == BC_ISEQV || op == BC_ISNEV) { - |5: // Either or both types are not numbers. - |.if FFI - | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd - | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd - |.endif - | checktp RA, RB // Compare types. - | jne <2 // Not the same type? - | cmp RB, LJ_TISPRI - | jae <1 // Same type and primitive type? - | - | // Same types and not a primitive type. Compare GCobj or pvalue. - | mov RA, [BASE+RA*8] - | mov RD, [BASE+RD*8] - | cmp RA, RD - | je <1 // Same GCobjs or pvalues? - | cmp RB, LJ_TISTABUD - | ja <2 // Different objects and not table/ud? - |.if X64 - | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata. - | jb <2 - |.endif - | - | // Different tables or userdatas. Need to check __eq metamethod. - | // Field metatable must be at same offset for GCtab and GCudata! - | mov TAB:RB, TAB:RA->metatable - | test TAB:RB, TAB:RB - | jz <2 // No metatable? - | test byte TAB:RB->nomm, 1<vmeta_equal // Handle __eq metamethod. - } else { - |.if FFI - |3: - | cmp RB, LJ_TCDATA - if (LJ_DUALNUM && vk) { - | jne <9 - } else { - | jne <2 - } - | jmp ->vmeta_equal_cd - |.endif - } - break; - case BC_ISEQS: case BC_ISNES: - vk = op == BC_ISEQS; - | ins_AND // RA = src, RD = str const, JMP with RD = target - | mov RB, [BASE+RA*8+4] - | add PC, 4 - | cmp RB, LJ_TSTR; jne >3 - | mov RA, [BASE+RA*8] - | cmp RA, [KBASE+RD*4] - iseqne_test: - if (vk) { - | jne >2 - } else { - | je >1 - } - goto iseqne_end; - case BC_ISEQN: case BC_ISNEN: - vk = op == BC_ISEQN; - | ins_AD // RA = src, RD = num const, JMP with RD = target - | mov RB, [BASE+RA*8+4] - | add PC, 4 - |.if DUALNUM - | cmp RB, LJ_TISNUM; jne >7 - | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8 - | mov RB, dword [KBASE+RD*8] - | cmp RB, dword [BASE+RA*8] - if (vk) { - | jne >9 - } else { - | je >9 - } - | movzx RD, PC_RD - | branchPC RD - |9: - | ins_next - | - |7: // RA is not an integer. - | ja >3 - | // RA is a number. - | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1 - | // RA is a number, RD is an integer. - | cvtsi2sd xmm0, dword [KBASE+RD*8] - | jmp >2 - | - |8: // RA is an integer, RD is a number. - | cvtsi2sd xmm0, dword [BASE+RA*8] - | ucomisd xmm0, qword [KBASE+RD*8] - | jmp >4 - |.else - | cmp RB, LJ_TISNUM; jae >3 - |.endif - |1: - | movsd xmm0, qword [KBASE+RD*8] - |2: - | ucomisd xmm0, qword [BASE+RA*8] - |4: - goto iseqne_fp; - case BC_ISEQP: case BC_ISNEP: - vk = op == BC_ISEQP; - | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target - | mov RB, [BASE+RA*8+4] - | add PC, 4 - | cmp RB, RD - if (!LJ_HASFFI) goto iseqne_test; - if (vk) { - | jne >3 - | movzx RD, PC_RD - | branchPC RD - |2: - | ins_next - |3: - | cmp RB, LJ_TCDATA; jne <2 - | jmp ->vmeta_equal_cd - } else { - | je >2 - | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd - | movzx RD, PC_RD - | branchPC RD - |2: - | ins_next - } - break; - - /* -- Unary test and copy ops ------------------------------------------- */ - - case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF: - | ins_AD // RA = dst or unused, RD = src, JMP with RD = target - | mov RB, [BASE+RD*8+4] - | add PC, 4 - | cmp RB, LJ_TISTRUECOND - if (op == BC_IST || op == BC_ISTC) { - | jae >1 - } else { - | jb >1 - } - if (op == BC_ISTC || op == BC_ISFC) { - | mov [BASE+RA*8+4], RB - | mov RB, [BASE+RD*8] - | mov [BASE+RA*8], RB - } - | movzx RD, PC_RD - | branchPC RD - |1: // Fallthrough to the next instruction. - | ins_next - break; - - case BC_ISTYPE: - | ins_AD // RA = src, RD = -type - | add RD, [BASE+RA*8+4] - | jne ->vmeta_istype - | ins_next - break; - case BC_ISNUM: - | ins_AD // RA = src, RD = -(TISNUM-1) - | checknum RA, ->vmeta_istype - | ins_next - break; - - /* -- Unary ops --------------------------------------------------------- */ - - case BC_MOV: - | ins_AD // RA = dst, RD = src - |.if X64 - | mov RBa, [BASE+RD*8] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [BASE+RD*8+4] - | mov RD, [BASE+RD*8] - | mov [BASE+RA*8+4], RB - | mov [BASE+RA*8], RD - |.endif - | ins_next_ - break; - case BC_NOT: - | ins_AD // RA = dst, RD = src - | xor RB, RB - | checktp RD, LJ_TISTRUECOND - | adc RB, LJ_TTRUE - | mov [BASE+RA*8+4], RB - | ins_next - break; - case BC_UNM: - | ins_AD // RA = dst, RD = src - |.if DUALNUM - | checkint RD, >5 - | mov RB, [BASE+RD*8] - | neg RB - | jo >4 - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RB - |9: - | ins_next - |4: - | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31. - | mov dword [BASE+RA*8], 0 - | jmp <9 - |5: - | ja ->vmeta_unm - |.else - | checknum RD, ->vmeta_unm - |.endif - | movsd xmm0, qword [BASE+RD*8] - | sseconst_sign xmm1, RDa - | xorps xmm0, xmm1 - | movsd qword [BASE+RA*8], xmm0 - |.if DUALNUM - | jmp <9 - |.else - | ins_next - |.endif - break; - case BC_LEN: - | ins_AD // RA = dst, RD = src - | checkstr RD, >2 - | mov STR:RD, [BASE+RD*8] - |.if DUALNUM - | mov RD, dword STR:RD->len - |1: - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RD - |.else - | xorps xmm0, xmm0 - | cvtsi2sd xmm0, dword STR:RD->len - |1: - | movsd qword [BASE+RA*8], xmm0 - |.endif - | ins_next - |2: - | checktab RD, ->vmeta_len - | mov TAB:FCARG1, [BASE+RD*8] -#if LJ_52 - | mov TAB:RB, TAB:FCARG1->metatable - | cmp TAB:RB, 0 - | jnz >9 - |3: -#endif - |->BC_LEN_Z: - | mov RB, BASE // Save BASE. - | call extern lj_tab_len@4 // (GCtab *t) - | // Length of table returned in eax (RD). - |.if DUALNUM - | // Nothing to do. - |.else - | cvtsi2sd xmm0, RD - |.endif - | mov BASE, RB // Restore BASE. - | movzx RA, PC_RA - | jmp <1 -#if LJ_52 - |9: // Check for __len. - | test byte TAB:RB->nomm, 1<vmeta_len // 'no __len' flag NOT set: check. -#endif - break; - - /* -- Binary ops -------------------------------------------------------- */ - - |.macro ins_arithpre, sseins, ssereg - | ins_ABC - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | checknum RB, ->vmeta_arith_vn - | .if DUALNUM - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn - | .endif - | movsd xmm0, qword [BASE+RB*8] - | sseins ssereg, qword [KBASE+RC*8] - || break; - ||case 1: - | checknum RB, ->vmeta_arith_nv - | .if DUALNUM - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv - | .endif - | movsd xmm0, qword [KBASE+RC*8] - | sseins ssereg, qword [BASE+RB*8] - || break; - ||default: - | checknum RB, ->vmeta_arith_vv - | checknum RC, ->vmeta_arith_vv - | movsd xmm0, qword [BASE+RB*8] - | sseins ssereg, qword [BASE+RC*8] - || break; - ||} - |.endmacro - | - |.macro ins_arithdn, intins - | ins_ABC - ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN); - ||switch (vk) { - ||case 0: - | checkint RB, ->vmeta_arith_vn - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn - | mov RB, [BASE+RB*8] - | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno - || break; - ||case 1: - | checkint RB, ->vmeta_arith_nv - | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv - | mov RC, [KBASE+RC*8] - | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo - || break; - ||default: - | checkint RB, ->vmeta_arith_vv - | checkint RC, ->vmeta_arith_vv - | mov RB, [BASE+RB*8] - | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo - || break; - ||} - | mov dword [BASE+RA*8+4], LJ_TISNUM - ||if (vk == 1) { - | mov dword [BASE+RA*8], RC - ||} else { - | mov dword [BASE+RA*8], RB - ||} - | ins_next - |.endmacro - | - |.macro ins_arithpost - | movsd qword [BASE+RA*8], xmm0 - |.endmacro - | - |.macro ins_arith, sseins - | ins_arithpre sseins, xmm0 - | ins_arithpost - | ins_next - |.endmacro - | - |.macro ins_arith, intins, sseins - |.if DUALNUM - | ins_arithdn intins - |.else - | ins_arith, sseins - |.endif - |.endmacro - - | // RA = dst, RB = src1 or num const, RC = src2 or num const - case BC_ADDVN: case BC_ADDNV: case BC_ADDVV: - | ins_arith add, addsd - break; - case BC_SUBVN: case BC_SUBNV: case BC_SUBVV: - | ins_arith sub, subsd - break; - case BC_MULVN: case BC_MULNV: case BC_MULVV: - | ins_arith imul, mulsd - break; - case BC_DIVVN: case BC_DIVNV: case BC_DIVVV: - | ins_arith divsd - break; - case BC_MODVN: - | ins_arithpre movsd, xmm1 - |->BC_MODVN_Z: - | call ->vm_mod - | ins_arithpost - | ins_next - break; - case BC_MODNV: case BC_MODVV: - | ins_arithpre movsd, xmm1 - | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway. - break; - case BC_POW: - | ins_arithpre movsd, xmm1 - | mov RB, BASE - |.if not X64 - | movsd FPARG1, xmm0 - | movsd FPARG3, xmm1 - |.endif - | call extern pow - | movzx RA, PC_RA - | mov BASE, RB - |.if X64 - | ins_arithpost - |.else - | fstp qword [BASE+RA*8] - |.endif - | ins_next - break; - - case BC_CAT: - | ins_ABC // RA = dst, RB = src_start, RC = src_end - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | lea CARG2d, [BASE+RC*8] - | mov CARG3d, RC - | sub CARG3d, RB - |->BC_CAT_Z: - | mov L:RB, L:CARG1d - |.else - | lea RA, [BASE+RC*8] - | sub RC, RB - | mov ARG2, RA - | mov ARG3, RC - |->BC_CAT_Z: - | mov L:RB, SAVE_L - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_meta_cat // (lua_State *L, TValue *top, int left) - | // NULL (finished) or TValue * (metamethod) returned in eax (RC). - | mov BASE, L:RB->base - | test RC, RC - | jnz ->vmeta_binop - | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB]. - | movzx RA, PC_RA - |.if X64 - | mov RCa, [BASE+RB*8] - | mov [BASE+RA*8], RCa - |.else - | mov RC, [BASE+RB*8+4] - | mov RB, [BASE+RB*8] - | mov [BASE+RA*8+4], RC - | mov [BASE+RA*8], RB - |.endif - | ins_next - break; - - /* -- Constant ops ------------------------------------------------------ */ - - case BC_KSTR: - | ins_AND // RA = dst, RD = str const (~) - | mov RD, [KBASE+RD*4] - | mov dword [BASE+RA*8+4], LJ_TSTR - | mov [BASE+RA*8], RD - | ins_next - break; - case BC_KCDATA: - |.if FFI - | ins_AND // RA = dst, RD = cdata const (~) - | mov RD, [KBASE+RD*4] - | mov dword [BASE+RA*8+4], LJ_TCDATA - | mov [BASE+RA*8], RD - | ins_next - |.endif - break; - case BC_KSHORT: - | ins_AD // RA = dst, RD = signed int16 literal - |.if DUALNUM - | movsx RD, RDW - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RD - |.else - | movsx RD, RDW // Sign-extend literal. - | cvtsi2sd xmm0, RD - | movsd qword [BASE+RA*8], xmm0 - |.endif - | ins_next - break; - case BC_KNUM: - | ins_AD // RA = dst, RD = num const - | movsd xmm0, qword [KBASE+RD*8] - | movsd qword [BASE+RA*8], xmm0 - | ins_next - break; - case BC_KPRI: - | ins_AND // RA = dst, RD = primitive type (~) - | mov [BASE+RA*8+4], RD - | ins_next - break; - case BC_KNIL: - | ins_AD // RA = dst_start, RD = dst_end - | lea RA, [BASE+RA*8+12] - | lea RD, [BASE+RD*8+4] - | mov RB, LJ_TNIL - | mov [RA-8], RB // Sets minimum 2 slots. - |1: - | mov [RA], RB - | add RA, 8 - | cmp RA, RD - | jbe <1 - | ins_next - break; - - /* -- Upvalue and function ops ------------------------------------------ */ - - case BC_UGET: - | ins_AD // RA = dst, RD = upvalue # - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)] - | mov RB, UPVAL:RB->v - |.if X64 - | mov RDa, [RB] - | mov [BASE+RA*8], RDa - |.else - | mov RD, [RB+4] - | mov RB, [RB] - | mov [BASE+RA*8+4], RD - | mov [BASE+RA*8], RB - |.endif - | ins_next - break; - case BC_USETV: -#define TV2MARKOFS \ - ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)) - | ins_AD // RA = upvalue #, RD = src - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | cmp byte UPVAL:RB->closed, 0 - | mov RB, UPVAL:RB->v - | mov RA, [BASE+RD*8] - | mov RD, [BASE+RD*8+4] - | mov [RB], RA - | mov [RB+4], RD - | jz >1 - | // Check barrier for closed upvalue. - | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv) - | jnz >2 - |1: - | ins_next - | - |2: // Upvalue is black. Check if new value is collectable and white. - | sub RD, LJ_TISGCV - | cmp RD, LJ_TNUMX - LJ_TISGCV // tvisgcv(v) - | jbe <1 - | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v) - | jz <1 - | // Crossed a write barrier. Move the barrier forward. - |.if X64 and not X64WIN - | mov FCARG2, RB - | mov RB, BASE // Save BASE. - |.else - | xchg FCARG2, RB // Save BASE (FCARG2 == BASE). - |.endif - | lea GL:FCARG1, [DISPATCH+GG_DISP2G] - | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv) - | mov BASE, RB // Restore BASE. - | jmp <1 - break; -#undef TV2MARKOFS - case BC_USETS: - | ins_AND // RA = upvalue #, RD = str const (~) - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | mov GCOBJ:RA, [KBASE+RD*4] - | mov RD, UPVAL:RB->v - | mov [RD], GCOBJ:RA - | mov dword [RD+4], LJ_TSTR - | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv) - | jnz >2 - |1: - | ins_next - | - |2: // Check if string is white and ensure upvalue is closed. - | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str) - | jz <1 - | cmp byte UPVAL:RB->closed, 0 - | jz <1 - | // Crossed a write barrier. Move the barrier forward. - | mov RB, BASE // Save BASE (FCARG2 == BASE). - | mov FCARG2, RD - | lea GL:FCARG1, [DISPATCH+GG_DISP2G] - | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv) - | mov BASE, RB // Restore BASE. - | jmp <1 - break; - case BC_USETN: - | ins_AD // RA = upvalue #, RD = num const - | mov LFUNC:RB, [BASE-8] - | movsd xmm0, qword [KBASE+RD*8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | mov RA, UPVAL:RB->v - | movsd qword [RA], xmm0 - | ins_next - break; - case BC_USETP: - | ins_AND // RA = upvalue #, RD = primitive type (~) - | mov LFUNC:RB, [BASE-8] - | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)] - | mov RA, UPVAL:RB->v - | mov [RA+4], RD - | ins_next - break; - case BC_UCLO: - | ins_AD // RA = level, RD = target - | branchPC RD // Do this first to free RD. - | mov L:RB, SAVE_L - | cmp dword L:RB->openupval, 0 - | je >1 - | mov L:RB->base, BASE - | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA - | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level) - | mov BASE, L:RB->base - |1: - | ins_next - break; - - case BC_FNEW: - | ins_AND // RA = dst, RD = proto const (~) (holding function prototype) - |.if X64 - | mov L:RB, SAVE_L - | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG3d, [BASE-8] - | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *. - | mov CARG1d, L:RB - |.else - | mov LFUNC:RA, [BASE-8] - | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *. - | mov L:RB, SAVE_L - | mov ARG3, LFUNC:RA - | mov ARG2, PROTO:RD - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | // (lua_State *L, GCproto *pt, GCfuncL *parent) - | call extern lj_func_newL_gc - | // GCfuncL * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RA, PC_RA - | mov [BASE+RA*8], LFUNC:RC - | mov dword [BASE+RA*8+4], LJ_TFUNC - | ins_next - break; - - /* -- Table ops --------------------------------------------------------- */ - - case BC_TNEW: - | ins_AD // RA = dst, RD = hbits|asize - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov RA, [DISPATCH+DISPATCH_GL(gc.total)] - | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)] - | mov SAVE_PC, PC - | jae >5 - |1: - |.if X64 - | mov CARG3d, RD - | and RD, 0x7ff - | shr CARG3d, 11 - |.else - | mov RA, RD - | and RD, 0x7ff - | shr RA, 11 - | mov ARG3, RA - |.endif - | cmp RD, 0x7ff - | je >3 - |2: - |.if X64 - | mov L:CARG1d, L:RB - | mov CARG2d, RD - |.else - | mov ARG1, L:RB - | mov ARG2, RD - |.endif - | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits) - | // Table * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RA, PC_RA - | mov [BASE+RA*8], TAB:RC - | mov dword [BASE+RA*8+4], LJ_TTAB - | ins_next - |3: // Turn 0x7ff into 0x801. - | mov RD, 0x801 - | jmp <2 - |5: - | mov L:FCARG1, L:RB - | call extern lj_gc_step_fixtop@4 // (lua_State *L) - | movzx RD, PC_RD - | jmp <1 - break; - case BC_TDUP: - | ins_AND // RA = dst, RD = table const (~) (holding template table) - | mov L:RB, SAVE_L - | mov RA, [DISPATCH+DISPATCH_GL(gc.total)] - | mov SAVE_PC, PC - | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)] - | mov L:RB->base, BASE - | jae >3 - |2: - | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE - | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA - | call extern lj_tab_dup@8 // (lua_State *L, Table *kt) - | // Table * returned in eax (RC). - | mov BASE, L:RB->base - | movzx RA, PC_RA - | mov [BASE+RA*8], TAB:RC - | mov dword [BASE+RA*8+4], LJ_TTAB - | ins_next - |3: - | mov L:FCARG1, L:RB - | call extern lj_gc_step_fixtop@4 // (lua_State *L) - | movzx RD, PC_RD // Need to reload RD. - | not RDa - | jmp <2 - break; - - case BC_GGET: - | ins_AND // RA = dst, RD = str const (~) - | mov LFUNC:RB, [BASE-8] - | mov TAB:RB, LFUNC:RB->env - | mov STR:RC, [KBASE+RD*4] - | jmp ->BC_TGETS_Z - break; - case BC_GSET: - | ins_AND // RA = src, RD = str const (~) - | mov LFUNC:RB, [BASE-8] - | mov TAB:RB, LFUNC:RB->env - | mov STR:RC, [KBASE+RD*4] - | jmp ->BC_TSETS_Z - break; - - case BC_TGETV: - | ins_ABC // RA = dst, RB = table, RC = key - | checktab RB, ->vmeta_tgetv - | mov TAB:RB, [BASE+RB*8] - | - | // Integer key? - |.if DUALNUM - | checkint RC, >5 - | mov RC, dword [BASE+RC*8] - |.else - | // Convert number to int and back and compare. - | checknum RC, >5 - | movsd xmm0, qword [BASE+RC*8] - | cvttsd2si RC, xmm0 - | cvtsi2sd xmm1, RC - | ucomisd xmm0, xmm1 - | jne ->vmeta_tgetv // Generic numeric key? Use fallback. - |.endif - | cmp RC, TAB:RB->asize // Takes care of unordered, too. - | jae ->vmeta_tgetv // Not in array part? Use fallback. - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath. - | je >2 - | // Get array slot. - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC] - | mov RC, [RC+4] - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - |.endif - |1: - | ins_next - | - |2: // Check for __index if table value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz >3 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tgetv // 'no __index' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - |3: - | mov dword [BASE+RA*8+4], LJ_TNIL - | jmp <1 - | - |5: // String key? - | checkstr RC, ->vmeta_tgetv - | mov STR:RC, [BASE+RC*8] - | jmp ->BC_TGETS_Z - break; - case BC_TGETS: - | ins_ABC // RA = dst, RB = table, RC = str const (~) - | not RCa - | mov STR:RC, [KBASE+RC*4] - | checktab RB, ->vmeta_tgets - | mov TAB:RB, [BASE+RB*8] - |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA. - | mov RA, TAB:RB->hmask - | and RA, STR:RC->sid - | imul RA, #NODE - | add NODE:RA, TAB:RB->node - |1: - | cmp dword NODE:RA->key.it, LJ_TSTR - | jne >4 - | cmp dword NODE:RA->key.gcr, STR:RC - | jne >4 - | // Ok, key found. Assumes: offsetof(Node, val) == 0 - | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath. - | je >5 // Key found, but nil value? - | movzx RC, PC_RA - | // Get node value. - |.if X64 - | mov RBa, [RA] - | mov [BASE+RC*8], RBa - |.else - | mov RB, [RA] - | mov RA, [RA+4] - | mov [BASE+RC*8], RB - | mov [BASE+RC*8+4], RA - |.endif - |2: - | ins_next - | - |3: - | movzx RC, PC_RA - | mov dword [BASE+RC*8+4], LJ_TNIL - | jmp <2 - | - |4: // Follow hash chain. - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <1 - | // End of hash chain: key not found, nil result. - | - |5: // Check for __index if table value is nil. - | mov TAB:RA, TAB:RB->metatable - | test TAB:RA, TAB:RA - | jz <3 // No metatable: done. - | test byte TAB:RA->nomm, 1<vmeta_tgets // Caveat: preserve STR:RC. - break; - case BC_TGETB: - | ins_ABC // RA = dst, RB = table, RC = byte literal - | checktab RB, ->vmeta_tgetb - | mov TAB:RB, [BASE+RB*8] - | cmp RC, TAB:RB->asize - | jae ->vmeta_tgetb - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath. - | je >2 - | // Get array slot. - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC] - | mov RC, [RC+4] - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - |.endif - |1: - | ins_next - | - |2: // Check for __index if table value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz >3 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tgetb // 'no __index' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - |3: - | mov dword [BASE+RA*8+4], LJ_TNIL - | jmp <1 - break; - case BC_TGETR: - | ins_ABC // RA = dst, RB = table, RC = key - | mov TAB:RB, [BASE+RB*8] - |.if DUALNUM - | mov RC, dword [BASE+RC*8] - |.else - | cvttsd2si RC, qword [BASE+RC*8] - |.endif - | cmp RC, TAB:RB->asize - | jae ->vmeta_tgetr // Not in array part? Use fallback. - | shl RC, 3 - | add RC, TAB:RB->array - | // Get array slot. - |->BC_TGETR_Z: - |.if X64 - | mov RBa, [RC] - | mov [BASE+RA*8], RBa - |.else - | mov RB, [RC] - | mov RC, [RC+4] - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - |.endif - |->BC_TGETR2_Z: - | ins_next - break; - - case BC_TSETV: - | ins_ABC // RA = src, RB = table, RC = key - | checktab RB, ->vmeta_tsetv - | mov TAB:RB, [BASE+RB*8] - | - | // Integer key? - |.if DUALNUM - | checkint RC, >5 - | mov RC, dword [BASE+RC*8] - |.else - | // Convert number to int and back and compare. - | checknum RC, >5 - | movsd xmm0, qword [BASE+RC*8] - | cvttsd2si RC, xmm0 - | cvtsi2sd xmm1, RC - | ucomisd xmm0, xmm1 - | jne ->vmeta_tsetv // Generic numeric key? Use fallback. - |.endif - | cmp RC, TAB:RB->asize // Takes care of unordered, too. - | jae ->vmeta_tsetv - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL - | je >3 // Previous value is nil? - |1: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: // Set array slot. - |.if X64 - | mov RBa, [BASE+RA*8] - | mov [RC], RBa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz <1 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tsetv // 'no __newindex' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - | jmp <1 - | - |5: // String key? - | checkstr RC, ->vmeta_tsetv - | mov STR:RC, [BASE+RC*8] - | jmp ->BC_TSETS_Z - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RA - | movzx RA, PC_RA // Restore RA. - | jmp <2 - break; - case BC_TSETS: - | ins_ABC // RA = src, RB = table, RC = str const (~) - | not RCa - | mov STR:RC, [KBASE+RC*4] - | checktab RB, ->vmeta_tsets - | mov TAB:RB, [BASE+RB*8] - |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA. - | mov RA, TAB:RB->hmask - | and RA, STR:RC->sid - | imul RA, #NODE - | mov byte TAB:RB->nomm, 0 // Clear metamethod cache. - | add NODE:RA, TAB:RB->node - |1: - | cmp dword NODE:RA->key.it, LJ_TSTR - | jne >5 - | cmp dword NODE:RA->key.gcr, STR:RC - | jne >5 - | // Ok, key found. Assumes: offsetof(Node, val) == 0 - | cmp dword [RA+4], LJ_TNIL - | je >4 // Previous value is nil? - |2: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |3: // Set node value. - | movzx RC, PC_RA - |.if X64 - | mov RBa, [BASE+RC*8] - | mov [RA], RBa - |.else - | mov RB, [BASE+RC*8+4] - | mov RC, [BASE+RC*8] - | mov [RA+4], RB - | mov [RA], RC - |.endif - | ins_next - | - |4: // Check for __newindex if previous value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz <2 - | mov TMP1, RA // Save RA. - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - | mov RA, TMP1 // Restore RA. - | jmp <2 - | - |5: // Follow hash chain. - | mov NODE:RA, NODE:RA->next - | test NODE:RA, NODE:RA - | jnz <1 - | // End of hash chain: key not found, add a new one. - | - | // But check for __newindex first. - | mov TAB:RA, TAB:RB->metatable - | test TAB:RA, TAB:RA - | jz >6 // No metatable: continue. - | test byte TAB:RA->nomm, 1<vmeta_tsets // 'no __newindex' flag NOT set: check. - |6: - | mov TMP1, STR:RC - | mov TMP2, LJ_TSTR - | mov TMP3, TAB:RB // Save TAB:RB for us. - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE - | lea CARG3, TMP1 - | mov CARG2d, TAB:RB - | mov L:RB, L:CARG1d - |.else - | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2. - | mov ARG2, TAB:RB - | mov L:RB, SAVE_L - | mov ARG3, RC - | mov ARG1, L:RB - | mov L:RB->base, BASE - |.endif - | mov SAVE_PC, PC - | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k) - | // Handles write barrier for the new key. TValue * returned in eax (RC). - | mov BASE, L:RB->base - | mov TAB:RB, TMP3 // Need TAB:RB for barrier. - | mov RA, eax - | jmp <2 // Must check write barrier for value. - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RC // Destroys STR:RC. - | jmp <3 - break; - case BC_TSETB: - | ins_ABC // RA = src, RB = table, RC = byte literal - | checktab RB, ->vmeta_tsetb - | mov TAB:RB, [BASE+RB*8] - | cmp RC, TAB:RB->asize - | jae ->vmeta_tsetb - | shl RC, 3 - | add RC, TAB:RB->array - | cmp dword [RC+4], LJ_TNIL - | je >3 // Previous value is nil? - |1: - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: // Set array slot. - |.if X64 - | mov RAa, [BASE+RA*8] - | mov [RC], RAa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - | ins_next - | - |3: // Check for __newindex if previous value is nil. - | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath. - | jz <1 - | mov TAB:RA, TAB:RB->metatable - | test byte TAB:RA->nomm, 1<vmeta_tsetb // 'no __newindex' flag NOT set: check. - | movzx RA, PC_RA // Restore RA. - | jmp <1 - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RA - | movzx RA, PC_RA // Restore RA. - | jmp <2 - break; - case BC_TSETR: - | ins_ABC // RA = src, RB = table, RC = key - | mov TAB:RB, [BASE+RB*8] - |.if DUALNUM - | mov RC, dword [BASE+RC*8] - |.else - | cvttsd2si RC, qword [BASE+RC*8] - |.endif - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: - | cmp RC, TAB:RB->asize - | jae ->vmeta_tsetr - | shl RC, 3 - | add RC, TAB:RB->array - | // Set array slot. - |->BC_TSETR_Z: - |.if X64 - | mov RBa, [BASE+RA*8] - | mov [RC], RBa - |.else - | mov RB, [BASE+RA*8+4] - | mov RA, [BASE+RA*8] - | mov [RC+4], RB - | mov [RC], RA - |.endif - | ins_next - | - |7: // Possible table write barrier for the value. Skip valiswhite check. - | barrierback TAB:RB, RA - | movzx RA, PC_RA // Restore RA. - | jmp <2 - break; - - case BC_TSETM: - | ins_AD // RA = base (table at base-1), RD = num const (start index) - | mov TMP1, KBASE // Need one more free register. - | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word. - |1: - | lea RA, [BASE+RA*8] - | mov TAB:RB, [RA-8] // Guaranteed to be a table. - | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table) - | jnz >7 - |2: - | mov RD, MULTRES - | sub RD, 1 - | jz >4 // Nothing to copy? - | add RD, KBASE // Compute needed size. - | cmp RD, TAB:RB->asize - | ja >5 // Doesn't fit into array part? - | sub RD, KBASE - | shl KBASE, 3 - | add KBASE, TAB:RB->array - |3: // Copy result slots to table. - |.if X64 - | mov RBa, [RA] - | add RA, 8 - | mov [KBASE], RBa - |.else - | mov RB, [RA] - | mov [KBASE], RB - | mov RB, [RA+4] - | add RA, 8 - | mov [KBASE+4], RB - |.endif - | add KBASE, 8 - | sub RD, 1 - | jnz <3 - |4: - | mov KBASE, TMP1 - | ins_next - | - |5: // Need to resize array part. - |.if X64 - | mov L:CARG1d, SAVE_L - | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE. - | mov CARG2d, TAB:RB - | mov CARG3d, RD - | mov L:RB, L:CARG1d - |.else - | mov ARG2, TAB:RB - | mov L:RB, SAVE_L - | mov L:RB->base, BASE - | mov ARG3, RD - | mov ARG1, L:RB - |.endif - | mov SAVE_PC, PC - | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize) - | mov BASE, L:RB->base - | movzx RA, PC_RA // Restore RA. - | jmp <1 // Retry. - | - |7: // Possible table write barrier for any value. Skip valiswhite check. - | barrierback TAB:RB, RD - | jmp <2 - break; - - /* -- Calls and vararg handling ----------------------------------------- */ - - case BC_CALL: case BC_CALLM: - | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs - if (op == BC_CALLM) { - | add NARGS:RD, MULTRES - } - | cmp dword [BASE+RA*8+4], LJ_TFUNC - | mov LFUNC:RB, [BASE+RA*8] - | jne ->vmeta_call_ra - | lea BASE, [BASE+RA*8+8] - | ins_call - break; - - case BC_CALLMT: - | ins_AD // RA = base, RD = extra_nargs - | add NARGS:RD, MULTRES - | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op. - break; - case BC_CALLT: - | ins_AD // RA = base, RD = nargs+1 - | lea RA, [BASE+RA*8+8] - | mov KBASE, BASE // Use KBASE for move + vmeta_call hint. - | mov LFUNC:RB, [RA-8] - | cmp dword [RA-4], LJ_TFUNC - | jne ->vmeta_call - |->BC_CALLT_Z: - | mov PC, [BASE-4] - | test PC, FRAME_TYPE - | jnz >7 - |1: - | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below. - | mov MULTRES, NARGS:RD - | sub NARGS:RD, 1 - | jz >3 - |2: // Move args down. - |.if X64 - | mov RBa, [RA] - | add RA, 8 - | mov [KBASE], RBa - |.else - | mov RB, [RA] - | mov [KBASE], RB - | mov RB, [RA+4] - | add RA, 8 - | mov [KBASE+4], RB - |.endif - | add KBASE, 8 - | sub NARGS:RD, 1 - | jnz <2 - | - | mov LFUNC:RB, [BASE-8] - |3: - | mov NARGS:RD, MULTRES - | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function? - | ja >5 - |4: - | ins_callt - | - |5: // Tailcall to a fast function. - | test PC, FRAME_TYPE // Lua frame below? - | jnz <4 - | movzx RA, PC_RA - | not RAa - | mov LFUNC:KBASE, [BASE+RA*8-8] // Need to prepare KBASE. - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | jmp <4 - | - |7: // Tailcall from a vararg function. - | sub PC, FRAME_VARG - | test PC, FRAME_TYPEP - | jnz >8 // Vararg frame below? - | sub BASE, PC // Need to relocate BASE/KBASE down. - | mov KBASE, BASE - | mov PC, [BASE-4] - | jmp <1 - |8: - | add PC, FRAME_VARG - | jmp <1 - break; - - case BC_ITERC: - | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1) - | lea RA, [BASE+RA*8+8] // fb = base+1 - |.if X64 - | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3]. - | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2]. - | mov [RA], RBa - | mov [RA+8], RCa - |.else - | mov RB, [RA-24] // Copy state. fb[0] = fb[-3]. - | mov RC, [RA-20] - | mov [RA], RB - | mov [RA+4], RC - | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2]. - | mov RC, [RA-12] - | mov [RA+8], RB - | mov [RA+12], RC - |.endif - | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4] - | mov RC, [RA-28] - | mov [RA-8], LFUNC:RB - | mov [RA-4], RC - | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call. - | mov NARGS:RD, 2+1 - | jne ->vmeta_call - | mov BASE, RA - | ins_call - break; - - case BC_ITERN: - |.if JIT - | hotloop RB - |.endif - |->vm_IITERN: - | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1)) - | mov TMP1, KBASE // Need two more free registers. - | mov TMP2, DISPATCH - | mov TAB:RB, [BASE+RA*8-16] - | mov RC, [BASE+RA*8-8] // Get index from control var. - | mov DISPATCH, TAB:RB->asize - | add PC, 4 - | mov KBASE, TAB:RB->array - |1: // Traverse array part. - | cmp RC, DISPATCH; jae >5 // Index points after array part? - | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4 - |.if DUALNUM - | mov dword [BASE+RA*8+4], LJ_TISNUM - | mov dword [BASE+RA*8], RC - |.else - | cvtsi2sd xmm0, RC - |.endif - | // Copy array slot to returned value. - |.if X64 - | mov RBa, [KBASE+RC*8] - | mov [BASE+RA*8+8], RBa - |.else - | mov RB, [KBASE+RC*8+4] - | mov [BASE+RA*8+12], RB - | mov RB, [KBASE+RC*8] - | mov [BASE+RA*8+8], RB - |.endif - | add RC, 1 - | // Return array index as a numeric key. - |.if DUALNUM - | // See above. - |.else - | movsd qword [BASE+RA*8], xmm0 - |.endif - | mov [BASE+RA*8-8], RC // Update control var. - |2: - | movzx RD, PC_RD // Get target from ITERL. - | branchPC RD - |3: - | mov DISPATCH, TMP2 - | mov KBASE, TMP1 - | ins_next - | - |4: // Skip holes in array part. - | add RC, 1 - | jmp <1 - | - |5: // Traverse hash part. - | sub RC, DISPATCH - |6: - | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1. - | imul KBASE, RC, #NODE - | add NODE:KBASE, TAB:RB->node - | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7 - | lea DISPATCH, [RC+DISPATCH+1] - | // Copy key and value from hash slot. - |.if X64 - | mov RBa, NODE:KBASE->key - | mov RCa, NODE:KBASE->val - | mov [BASE+RA*8], RBa - | mov [BASE+RA*8+8], RCa - |.else - | mov RB, NODE:KBASE->key.gcr - | mov RC, NODE:KBASE->key.it - | mov [BASE+RA*8], RB - | mov [BASE+RA*8+4], RC - | mov RB, NODE:KBASE->val.gcr - | mov RC, NODE:KBASE->val.it - | mov [BASE+RA*8+8], RB - | mov [BASE+RA*8+12], RC - |.endif - | mov [BASE+RA*8-8], DISPATCH - | jmp <2 - | - |7: // Skip holes in hash part. - | add RC, 1 - | jmp <6 - break; - - case BC_ISNEXT: - | ins_AD // RA = base, RD = target (points to ITERN) - | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5 - | mov CFUNC:RB, [BASE+RA*8-24] - | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5 - | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5 - | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5 - | branchPC RD - | mov dword [BASE+RA*8-8], 0 // Initialize control var. - | mov dword [BASE+RA*8-4], LJ_KEYINDEX - |1: - | ins_next - |5: // Despecialize bytecode if any of the checks fail. - | mov PC_OP, BC_JMP - | branchPC RD - |.if JIT - | cmp byte [PC], BC_ITERN - | jne >6 - |.endif - | mov byte [PC], BC_ITERC - | jmp <1 - |.if JIT - |6: // Unpatch JLOOP. - | mov RA, [DISPATCH+DISPATCH_J(trace)] - | movzx RC, word [PC+2] - | mov TRACE:RA, [RA+RC*4] - | mov eax, TRACE:RA->startins - | mov al, BC_ITERC - | mov dword [PC], eax - | jmp <1 - |.endif - break; - - case BC_VARG: - | ins_ABC // RA = base, RB = nresults+1, RC = numparams - | mov TMP1, KBASE // Need one more free register. - | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)] - | lea RA, [BASE+RA*8] - | sub KBASE, [BASE-4] - | // Note: KBASE may now be even _above_ BASE if nargs was < numparams. - | test RB, RB - | jz >5 // Copy all varargs? - | lea RB, [RA+RB*8-8] - | cmp KBASE, BASE // No vararg slots? - | jnb >2 - |1: // Copy vararg slots to destination slots. - |.if X64 - | mov RCa, [KBASE-8] - | add KBASE, 8 - | mov [RA], RCa - |.else - | mov RC, [KBASE-8] - | mov [RA], RC - | mov RC, [KBASE-4] - | add KBASE, 8 - | mov [RA+4], RC - |.endif - | add RA, 8 - | cmp RA, RB // All destination slots filled? - | jnb >3 - | cmp KBASE, BASE // No more vararg slots? - | jb <1 - |2: // Fill up remainder with nil. - | mov dword [RA+4], LJ_TNIL - | add RA, 8 - | cmp RA, RB - | jb <2 - |3: - | mov KBASE, TMP1 - | ins_next - | - |5: // Copy all varargs. - | mov MULTRES, 1 // MULTRES = 0+1 - | mov RC, BASE - | sub RC, KBASE - | jbe <3 // No vararg slots? - | mov RB, RC - | shr RB, 3 - | add RB, 1 - | mov MULTRES, RB // MULTRES = #varargs+1 - | mov L:RB, SAVE_L - | add RC, RA - | cmp RC, L:RB->maxstack - | ja >7 // Need to grow stack? - |6: // Copy all vararg slots. - |.if X64 - | mov RCa, [KBASE-8] - | add KBASE, 8 - | mov [RA], RCa - |.else - | mov RC, [KBASE-8] - | mov [RA], RC - | mov RC, [KBASE-4] - | add KBASE, 8 - | mov [RA+4], RC - |.endif - | add RA, 8 - | cmp KBASE, BASE // No more vararg slots? - | jb <6 - | jmp <3 - | - |7: // Grow stack for varargs. - | mov L:RB->base, BASE - | mov L:RB->top, RA - | mov SAVE_PC, PC - | sub KBASE, BASE // Need delta, because BASE may change. - | mov FCARG2, MULTRES - | sub FCARG2, 1 - | mov FCARG1, L:RB - | call extern lj_state_growstack@8 // (lua_State *L, int n) - | mov BASE, L:RB->base - | mov RA, L:RB->top - | add KBASE, BASE - | jmp <6 - break; - - /* -- Returns ----------------------------------------------------------- */ - - case BC_RETM: - | ins_AD // RA = results, RD = extra_nresults - | add RD, MULTRES // MULTRES >=1, so RD >=1. - | // Fall through. Assumes BC_RET follows and ins_AD is a no-op. - break; - - case BC_RET: case BC_RET0: case BC_RET1: - | ins_AD // RA = results, RD = nresults+1 - if (op != BC_RET0) { - | shl RA, 3 - } - |1: - | mov PC, [BASE-4] - | mov MULTRES, RD // Save nresults+1. - | test PC, FRAME_TYPE // Check frame type marker. - | jnz >7 // Not returning to a fixarg Lua func? - switch (op) { - case BC_RET: - |->BC_RET_Z: - | mov KBASE, BASE // Use KBASE for result move. - | sub RD, 1 - | jz >3 - |2: // Move results down. - |.if X64 - | mov RBa, [KBASE+RA] - | mov [KBASE-8], RBa - |.else - | mov RB, [KBASE+RA] - | mov [KBASE-8], RB - | mov RB, [KBASE+RA+4] - | mov [KBASE-4], RB - |.endif - | add KBASE, 8 - | sub RD, 1 - | jnz <2 - |3: - | mov RD, MULTRES // Note: MULTRES may be >255. - | movzx RB, PC_RB // So cannot compare with RDL! - |5: - | cmp RB, RD // More results expected? - | ja >6 - break; - case BC_RET1: - |.if X64 - | mov RBa, [BASE+RA] - | mov [BASE-8], RBa - |.else - | mov RB, [BASE+RA+4] - | mov [BASE-4], RB - | mov RB, [BASE+RA] - | mov [BASE-8], RB - |.endif - /* fallthrough */ - case BC_RET0: - |5: - | cmp PC_RB, RDL // More results expected? - | ja >6 - default: - break; - } - | movzx RA, PC_RA - | not RAa // Note: ~RA = -(RA+1) - | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8 - | mov LFUNC:KBASE, [BASE-8] - | mov KBASE, LFUNC:KBASE->pc - | mov KBASE, [KBASE+PC2PROTO(k)] - | ins_next - | - |6: // Fill up results with nil. - if (op == BC_RET) { - | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base. - | add KBASE, 8 - } else { - | mov dword [BASE+RD*8-12], LJ_TNIL - } - | add RD, 1 - | jmp <5 - | - |7: // Non-standard return case. - | lea RB, [PC-FRAME_VARG] - | test RB, FRAME_TYPEP - | jnz ->vm_return - | // Return from vararg function: relocate BASE down and RA up. - | sub BASE, RB - if (op != BC_RET0) { - | add RA, RB - } - | jmp <1 - break; - - /* -- Loops and branches ------------------------------------------------ */ - - |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4] - |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12] - |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20] - |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28] - - case BC_FORL: - |.if JIT - | hotloop RB - |.endif - | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op. - break; - - case BC_JFORI: - case BC_JFORL: -#if !LJ_HASJIT - break; -#endif - case BC_FORI: - case BC_IFORL: - vk = (op == BC_IFORL || op == BC_JFORL); - | ins_AJ // RA = base, RD = target (after end of loop or start of loop) - | lea RA, [BASE+RA*8] - if (LJ_DUALNUM) { - | cmp FOR_TIDX, LJ_TISNUM; jne >9 - if (!vk) { - | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for - | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for - | mov RB, dword FOR_IDX - | cmp dword FOR_STEP, 0; jl >5 - } else { -#ifdef LUA_USE_ASSERT - | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type - | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type -#endif - | mov RB, dword FOR_STEP - | test RB, RB; js >5 - | add RB, dword FOR_IDX; jo >1 - | mov dword FOR_IDX, RB - } - | cmp RB, dword FOR_STOP - | mov FOR_TEXT, LJ_TISNUM - | mov dword FOR_EXT, RB - if (op == BC_FORI) { - | jle >7 - |1: - |6: - | branchPC RD - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RD, PC_RD - | jle =>BC_JLOOP - |1: - |6: - } else if (op == BC_IFORL) { - | jg >7 - |6: - | branchPC RD - |1: - } else { - | jle =>BC_JLOOP - |1: - |6: - } - |7: - | ins_next - | - |5: // Invert check for negative step. - if (vk) { - | add RB, dword FOR_IDX; jo <1 - | mov dword FOR_IDX, RB - } - | cmp RB, dword FOR_STOP - | mov FOR_TEXT, LJ_TISNUM - | mov dword FOR_EXT, RB - if (op == BC_FORI) { - | jge <7 - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RD, PC_RD - | jge =>BC_JLOOP - } else if (op == BC_IFORL) { - | jl <7 - } else { - | jge =>BC_JLOOP - } - | jmp <6 - |9: // Fallback to FP variant. - } else if (!vk) { - | cmp FOR_TIDX, LJ_TISNUM - } - if (!vk) { - | jae ->vmeta_for - | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for - } else { -#ifdef LUA_USE_ASSERT - | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type - | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type -#endif - } - | mov RB, FOR_TSTEP // Load type/hiword of for step. - if (!vk) { - | cmp RB, LJ_TISNUM; jae ->vmeta_for - } - | movsd xmm0, qword FOR_IDX - | movsd xmm1, qword FOR_STOP - if (vk) { - | addsd xmm0, qword FOR_STEP - | movsd qword FOR_IDX, xmm0 - | test RB, RB; js >3 - } else { - | jl >3 - } - | ucomisd xmm1, xmm0 - |1: - | movsd qword FOR_EXT, xmm0 - if (op == BC_FORI) { - |.if DUALNUM - | jnb <7 - |.else - | jnb >2 - | branchPC RD - |.endif - } else if (op == BC_JFORI) { - | branchPC RD - | movzx RD, PC_RD - | jnb =>BC_JLOOP - } else if (op == BC_IFORL) { - |.if DUALNUM - | jb <7 - |.else - | jb >2 - | branchPC RD - |.endif - } else { - | jnb =>BC_JLOOP - } - |.if DUALNUM - | jmp <6 - |.else - |2: - | ins_next - |.endif - | - |3: // Invert comparison if step is negative. - | ucomisd xmm0, xmm1 - | jmp <1 - break; - - case BC_ITERL: - |.if JIT - | hotloop RB - |.endif - | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op. - break; - - case BC_JITERL: -#if !LJ_HASJIT - break; -#endif - case BC_IITERL: - | ins_AJ // RA = base, RD = target - | lea RA, [BASE+RA*8] - | mov RB, [RA+4] - | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil. - if (op == BC_JITERL) { - | mov [RA-4], RB - | mov RB, [RA] - | mov [RA-8], RB - | jmp =>BC_JLOOP - } else { - | branchPC RD // Otherwise save control var + branch. - | mov RD, [RA] - | mov [RA-4], RB - | mov [RA-8], RD - } - |1: - | ins_next - break; - - case BC_LOOP: - | ins_A // RA = base, RD = target (loop extent) - | // Note: RA/RD is only used by trace recorder to determine scope/extent - | // This opcode does NOT jump, it's only purpose is to detect a hot loop. - |.if JIT - | hotloop RB - |.endif - | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op. - break; - - case BC_ILOOP: - | ins_A // RA = base, RD = target (loop extent) - | ins_next - break; - - case BC_JLOOP: - |.if JIT - | ins_AD // RA = base (ignored), RD = traceno - | mov RA, [DISPATCH+DISPATCH_J(trace)] - | mov TRACE:RD, [RA+RD*4] - | mov RDa, TRACE:RD->mcode - | mov L:RB, SAVE_L - | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE - | mov [DISPATCH+DISPATCH_GL(tmpbuf.L)], L:RB - | // Save additional callee-save registers only used in compiled code. - |.if X64WIN - | mov TMPQ, r12 - | mov TMPa, r13 - | mov CSAVE_4, r14 - | mov CSAVE_3, r15 - | mov RAa, rsp - | sub rsp, 9*16+4*8 - | movdqa [RAa], xmm6 - | movdqa [RAa-1*16], xmm7 - | movdqa [RAa-2*16], xmm8 - | movdqa [RAa-3*16], xmm9 - | movdqa [RAa-4*16], xmm10 - | movdqa [RAa-5*16], xmm11 - | movdqa [RAa-6*16], xmm12 - | movdqa [RAa-7*16], xmm13 - | movdqa [RAa-8*16], xmm14 - | movdqa [RAa-9*16], xmm15 - |.elif X64 - | mov TMPQ, r12 - | mov TMPa, r13 - | sub rsp, 16 - |.endif - | jmp RDa - |.endif - break; - - case BC_JMP: - | ins_AJ // RA = unused, RD = target - | branchPC RD - | ins_next - break; - - /* -- Function headers -------------------------------------------------- */ - - /* - ** Reminder: A function may be called with func/args above L->maxstack, - ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot, - ** too. This means all FUNC* ops (including fast functions) must check - ** for stack overflow _before_ adding more slots! - */ - - case BC_FUNCF: - |.if JIT - | hotcall RB - |.endif - case BC_FUNCV: /* NYI: compiled vararg functions. */ - | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op. - break; - - case BC_JFUNCF: -#if !LJ_HASJIT - break; -#endif - case BC_IFUNCF: - | ins_AD // BASE = new base, RA = framesize, RD = nargs+1 - | mov KBASE, [PC-4+PC2PROTO(k)] - | mov L:RB, SAVE_L - | lea RA, [BASE+RA*8] // Top of frame. - | cmp RA, L:RB->maxstack - | ja ->vm_growstack_f - | movzx RA, byte [PC-4+PC2PROTO(numparams)] - | cmp NARGS:RD, RA // Check for missing parameters. - | jbe >3 - |2: - if (op == BC_JFUNCF) { - | movzx RD, PC_RD - | jmp =>BC_JLOOP - } else { - | ins_next - } - | - |3: // Clear missing parameters. - | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL - | add NARGS:RD, 1 - | cmp NARGS:RD, RA - | jbe <3 - | jmp <2 - break; - - case BC_JFUNCV: -#if !LJ_HASJIT - break; -#endif - | int3 // NYI: compiled vararg functions - break; /* NYI: compiled vararg functions. */ - - case BC_IFUNCV: - | ins_AD // BASE = new base, RA = framesize, RD = nargs+1 - | lea RB, [NARGS:RD*8+FRAME_VARG] - | lea RD, [BASE+NARGS:RD*8] - | mov LFUNC:KBASE, [BASE-8] - | mov [RD-4], RB // Store delta + FRAME_VARG. - | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC. - | mov L:RB, SAVE_L - | lea RA, [RD+RA*8] - | cmp RA, L:RB->maxstack - | ja ->vm_growstack_v // Need to grow stack. - | mov RA, BASE - | mov BASE, RD - | movzx RB, byte [PC-4+PC2PROTO(numparams)] - | test RB, RB - | jz >2 - |1: // Copy fixarg slots up to new frame. - | add RA, 8 - | cmp RA, BASE - | jnb >3 // Less args than parameters? - | mov KBASE, [RA-8] - | mov [RD], KBASE - | mov KBASE, [RA-4] - | mov [RD+4], KBASE - | add RD, 8 - | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC). - | sub RB, 1 - | jnz <1 - |2: - if (op == BC_JFUNCV) { - | movzx RD, PC_RD - | jmp =>BC_JLOOP - } else { - | mov KBASE, [PC-4+PC2PROTO(k)] - | ins_next - } - | - |3: // Clear missing parameters. - | mov dword [RD+4], LJ_TNIL - | add RD, 8 - | sub RB, 1 - | jnz <3 - | jmp <2 - break; - - case BC_FUNCC: - case BC_FUNCCW: - | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1 - | mov CFUNC:RB, [BASE-8] - | mov KBASEa, CFUNC:RB->f - | mov L:RB, SAVE_L - | lea RD, [BASE+NARGS:RD*8-8] - | mov L:RB->base, BASE - | lea RA, [RD+8*LUA_MINSTACK] - | cmp RA, L:RB->maxstack - | mov L:RB->top, RD - if (op == BC_FUNCC) { - |.if X64 - | mov CARG1d, L:RB // Caveat: CARG1d may be RA. - |.else - | mov ARG1, L:RB - |.endif - } else { - |.if X64 - | mov CARG2, KBASEa - | mov CARG1d, L:RB // Caveat: CARG1d may be RA. - |.else - | mov ARG2, KBASEa - | mov ARG1, L:RB - |.endif - } - | ja ->vm_growstack_c // Need to grow stack. - | set_vmstate C - if (op == BC_FUNCC) { - | call KBASEa // (lua_State *L) - } else { - | // (lua_State *L, lua_CFunction f) - | call aword [DISPATCH+DISPATCH_GL(wrapf)] - } - | // nresults returned in eax (RD). - | mov BASE, L:RB->base - | mov [DISPATCH+DISPATCH_GL(cur_L)], L:RB - | set_vmstate INTERP - | lea RA, [BASE+RD*8] - | neg RA - | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8 - | mov PC, [BASE-4] // Fetch PC of caller. - | jmp ->vm_returnc - break; - - /* ---------------------------------------------------------------------- */ - - default: - fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]); - exit(2); - break; - } -} - -static int build_backend(BuildCtx *ctx) -{ - int op; - dasm_growpc(Dst, BC__MAX); - build_subroutines(ctx); - |.code_op - for (op = 0; op < BC__MAX; op++) - build_ins(ctx, (BCOp)op, op); - return BC__MAX; -} - -/* Emit pseudo frame-info for all assembler functions. */ -static void emit_asm_debug(BuildCtx *ctx) -{ - int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code); -#if LJ_64 -#define SZPTR "8" -#define BSZPTR "3" -#define REG_SP "0x7" -#define REG_RA "0x10" -#else -#define SZPTR "4" -#define BSZPTR "2" -#define REG_SP "0x4" -#define REG_RA "0x8" -#endif - switch (ctx->mode) { - case BUILD_elfasm: - fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n"); - fprintf(ctx->fp, - ".Lframe0:\n" - "\t.long .LECIE0-.LSCIE0\n" - ".LSCIE0:\n" - "\t.long 0xffffffff\n" - "\t.byte 0x1\n" - "\t.string \"\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n" - "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n" - "\t.align " SZPTR "\n" - ".LECIE0:\n\n"); - fprintf(ctx->fp, - ".LSFDE0:\n" - "\t.long .LEFDE0-.LASFDE0\n" - ".LASFDE0:\n" - "\t.long .Lframe0\n" -#if LJ_64 - "\t.quad .Lbegin\n" - "\t.quad %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */ -#if LJ_NO_UNWIND - "\t.byte 0x8d\n\t.uleb128 0x6\n" /* offset r13 */ - "\t.byte 0x8c\n\t.uleb128 0x7\n" /* offset r12 */ -#endif -#else - "\t.long .Lbegin\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */ - "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */ - "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE0:\n\n", fcofs, CFRAME_SIZE); -#if LJ_HASFFI - fprintf(ctx->fp, - ".LSFDE1:\n" - "\t.long .LEFDE1-.LASFDE1\n" - ".LASFDE1:\n" - "\t.long .Lframe0\n" -#if LJ_64 - "\t.quad lj_vm_ffi_call\n" - "\t.quad %d\n" - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ -#else - "\t.long lj_vm_ffi_call\n" - "\t.long %d\n" - "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */ - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE1:\n\n", (int)ctx->codesz - fcofs); -#endif -#if !LJ_NO_UNWIND -#if LJ_TARGET_SOLARIS -#if LJ_64 - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n"); -#else - fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n"); -#endif -#else - fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n"); -#endif - fprintf(ctx->fp, - ".Lframe1:\n" - "\t.long .LECIE1-.LSCIE1\n" - ".LSCIE1:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zPR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.uleb128 6\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.long lj_err_unwind_dwarf-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n" - "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n" - "\t.align " SZPTR "\n" - ".LECIE1:\n\n"); - fprintf(ctx->fp, - ".LSFDE2:\n" - "\t.long .LEFDE2-.LASFDE2\n" - ".LASFDE2:\n" - "\t.long .LASFDE2-.Lframe1\n" - "\t.long .Lbegin-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */ -#if LJ_64 - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */ -#else - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */ - "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */ - "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE2:\n\n", fcofs, CFRAME_SIZE); -#if LJ_HASFFI - fprintf(ctx->fp, - ".Lframe2:\n" - "\t.long .LECIE2-.LSCIE2\n" - ".LSCIE2:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.string \"zR\"\n" - "\t.uleb128 0x1\n" - "\t.sleb128 -" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.uleb128 1\n" /* augmentation length */ - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n" - "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n" - "\t.align " SZPTR "\n" - ".LECIE2:\n\n"); - fprintf(ctx->fp, - ".LSFDE3:\n" - "\t.long .LEFDE3-.LASFDE3\n" - ".LASFDE3:\n" - "\t.long .LASFDE3-.Lframe2\n" - "\t.long lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.uleb128 0\n" /* augmentation length */ -#if LJ_64 - "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */ -#else - "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */ - "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */ - "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */ - "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */ -#endif - "\t.align " SZPTR "\n" - ".LEFDE3:\n\n", (int)ctx->codesz - fcofs); -#endif -#endif - break; -#if !LJ_NO_UNWIND - /* Mental note: never let Apple design an assembler. - ** Or a linker. Or a plastic case. But I digress. - */ - case BUILD_machasm: { -#if LJ_HASFFI - int fcsize = 0; -#endif - int i; - fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n"); - fprintf(ctx->fp, - "EH_frame1:\n" - "\t.set L$set$x,LECIEX-LSCIEX\n" - "\t.long L$set$x\n" - "LSCIEX:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zPR\\0\"\n" - "\t.byte 0x1\n" - "\t.byte 128-" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.byte 6\n" /* augmentation length */ - "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */ -#if LJ_64 - "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n" -#else - "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n" - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */ -#endif - "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n" - "\t.align " BSZPTR "\n" - "LECIEX:\n\n"); - for (i = 0; i < ctx->nsym; i++) { - const char *name = ctx->sym[i].name; - int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs; - if (size == 0) continue; -#if LJ_HASFFI - if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; } -#endif - fprintf(ctx->fp, - "%s.eh:\n" - "LSFDE%d:\n" - "\t.set L$set$%d,LEFDE%d-LASFDE%d\n" - "\t.long L$set$%d\n" - "LASFDE%d:\n" - "\t.long LASFDE%d-EH_frame1\n" - "\t.long %s-.\n" - "\t.long %d\n" - "\t.byte 0\n" /* augmentation length */ - "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */ -#if LJ_64 - "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */ - "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */ - "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */ -#else - "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/ - "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */ - "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */ - "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */ -#endif - "\t.align " BSZPTR "\n" - "LEFDE%d:\n\n", - name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i); - } -#if LJ_HASFFI - if (fcsize) { - fprintf(ctx->fp, - "EH_frame2:\n" - "\t.set L$set$y,LECIEY-LSCIEY\n" - "\t.long L$set$y\n" - "LSCIEY:\n" - "\t.long 0\n" - "\t.byte 0x1\n" - "\t.ascii \"zR\\0\"\n" - "\t.byte 0x1\n" - "\t.byte 128-" SZPTR "\n" - "\t.byte " REG_RA "\n" - "\t.byte 1\n" /* augmentation length */ -#if LJ_64 - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n" -#else - "\t.byte 0x1b\n" /* pcrel|sdata4 */ - "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */ -#endif - "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n" - "\t.align " BSZPTR "\n" - "LECIEY:\n\n"); - fprintf(ctx->fp, - "_lj_vm_ffi_call.eh:\n" - "LSFDEY:\n" - "\t.set L$set$yy,LEFDEY-LASFDEY\n" - "\t.long L$set$yy\n" - "LASFDEY:\n" - "\t.long LASFDEY-EH_frame2\n" - "\t.long _lj_vm_ffi_call-.\n" - "\t.long %d\n" - "\t.byte 0\n" /* augmentation length */ -#if LJ_64 - "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */ - "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */ - "\t.byte 0xd\n\t.byte 0x6\n" /* def_cfa_register rbp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */ -#else - "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */ - "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/ - "\t.byte 0xd\n\t.byte 0x4\n" /* def_cfa_register ebp */ - "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */ -#endif - "\t.align " BSZPTR "\n" - "LEFDEY:\n\n", fcsize); - } -#endif -#if !LJ_64 - fprintf(ctx->fp, - "\t.non_lazy_symbol_pointer\n" - "L_lj_err_unwind_dwarf$non_lazy_ptr:\n" - ".indirect_symbol _lj_err_unwind_dwarf\n" - ".long 0\n\n"); - fprintf(ctx->fp, "\t.section __IMPORT,__jump_table,symbol_stubs,pure_instructions+self_modifying_code,5\n"); - { - const char *const *xn; - for (xn = ctx->extnames; *xn; xn++) - if (strncmp(*xn, LABEL_PREFIX, sizeof(LABEL_PREFIX)-1)) - fprintf(ctx->fp, "L_%s$stub:\n\t.indirect_symbol _%s\n\t.ascii \"\\364\\364\\364\\364\\364\"\n", *xn, *xn); - } -#endif - fprintf(ctx->fp, ".subsections_via_symbols\n"); - } - break; -#endif - default: /* Difficult for other modes. */ - break; - } -} - diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xb1build.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xb1build.bat deleted file mode 100644 index a16160d..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xb1build.bat +++ /dev/null @@ -1,101 +0,0 @@ -@rem Script to build LuaJIT with the Xbox One SDK. -@rem Donated to the public domain. -@rem -@rem Open a "Visual Studio .NET Command Prompt" (64 bit host compiler) -@rem Then cd to this directory and run this script. - -@if not defined INCLUDE goto :FAIL -@if not defined DurangoXDK goto :FAIL - -@setlocal -@echo ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Error out for 64 bit host compiler -@minilua -@if not errorlevel 8 goto :FAIL - -@set DASMFLAGS=-D WIN -D FFI -D P64 -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x64.dasc -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% /D_DURANGO host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m peobj -o lj_vm.obj -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@echo ---- Cross compiler ---- - -@set CWD=%cd% -@call "%DurangoXDK%\xdk\DurangoVars.cmd" XDK -@cd /D "%CWD%" -@shift - -@set LJCOMPILE="cl" /nologo /c /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /D_LIB /D_UNICODE /D_DURANGO -@set LJLIB="lib" /nologo - -@if "%1"=="debug" ( - @shift - @set LJCOMPILE=%LJCOMPILE% /Zi /MDd /Od - @set LJLINK=%LJLINK% /debug -) else ( - @set LJCOMPILE=%LJCOMPILE% /MD /O2 /DNDEBUG -) - -@if "%1"=="amalg" goto :AMALG -%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:luajit.lib lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :NOAMALG -:AMALG -%LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:luajit.lib ljamalg.obj lj_vm.obj -@if errorlevel 1 goto :BAD -:NOAMALG - -@del *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for Xbox One === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Visual Studio .NET Command Prompt" -@echo (64 bit host compiler). The Xbox One SDK must be installed, too. -:END diff --git a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xedkbuild.bat b/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xedkbuild.bat deleted file mode 100644 index b54e473..0000000 --- a/LuaBridge3/Tests/Lua/LuaJIT.2.1/src/xedkbuild.bat +++ /dev/null @@ -1,92 +0,0 @@ -@rem Script to build LuaJIT with the Xbox 360 SDK. -@rem Donated to the public domain. -@rem -@rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler) -@rem Then cd to this directory and run this script. - -@if not defined INCLUDE goto :FAIL -@if not defined XEDK goto :FAIL - -@setlocal -@rem ---- Host compiler ---- -@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE -@set LJLINK=link /nologo -@set LJMT=mt /nologo -@set DASMDIR=..\dynasm -@set DASM=%DASMDIR%\dynasm.lua -@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c - -%LJCOMPILE% host\minilua.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:minilua.exe minilua.obj -@if errorlevel 1 goto :BAD -if exist minilua.exe.manifest^ - %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe - -@rem Error out for 64 bit host compiler -@minilua -@if errorlevel 8 goto :FAIL - -@set DASMFLAGS=-D GPR64 -D FRAME32 -D PPE -D SQRT -D DUALNUM -minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_ppc.dasc -@if errorlevel 1 goto :BAD - -%LJCOMPILE% /I "." /I %DASMDIR% /D_XBOX_VER=200 /DLUAJIT_TARGET=LUAJIT_ARCH_PPC host\buildvm*.c -@if errorlevel 1 goto :BAD -%LJLINK% /out:buildvm.exe buildvm*.obj -@if errorlevel 1 goto :BAD -if exist buildvm.exe.manifest^ - %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe - -buildvm -m peobj -o lj_vm.obj -@if errorlevel 1 goto :BAD -buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m libdef -o lj_libdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m recdef -o lj_recdef.h %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% -@if errorlevel 1 goto :BAD -buildvm -m folddef -o lj_folddef.h lj_opt_fold.c -@if errorlevel 1 goto :BAD - -@rem ---- Cross compiler ---- -@set LJCOMPILE="%XEDK%\bin\win32\cl" /nologo /c /MT /O2 /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /DNDEBUG /D_XBOX /D_LIB /DLUAJIT_USE_SYSMALLOC -@set LJLIB="%XEDK%\bin\win32\lib" /nologo -@set "INCLUDE=%XEDK%\include\xbox" - -@if "%1" neq "debug" goto :NODEBUG -@shift -@set "LJCOMPILE=%LJCOMPILE% /Zi" -:NODEBUG -@if "%1"=="amalg" goto :AMALG -%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:luajit20.lib lj_*.obj lib_*.obj -@if errorlevel 1 goto :BAD -@goto :NOAMALG -:AMALG -%LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c -@if errorlevel 1 goto :BAD -%LJLIB% /OUT:luajit20.lib ljamalg.obj lj_vm.obj -@if errorlevel 1 goto :BAD -:NOAMALG - -@del *.obj *.manifest minilua.exe buildvm.exe -@echo. -@echo === Successfully built LuaJIT for Xbox 360 === - -@goto :END -:BAD -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* -@goto :END -:FAIL -@echo To run this script you must open a "Visual Studio .NET Command Prompt" -@echo (32 bit host compiler). The Xbox 360 SDK must be installed, too. -:END diff --git a/LuaBridge3/Tests/Lua/LuaLibrary.h b/LuaBridge3/Tests/Lua/LuaLibrary.h deleted file mode 100644 index 01ffdcc..0000000 --- a/LuaBridge3/Tests/Lua/LuaLibrary.h +++ /dev/null @@ -1,70 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2020, Lucio Asnaghi - Copyright (C) 2012, Vinnie Falco - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#pragma once - -// This determines which version of Lua to use. -// The value is the same as LUA_VERSION_NUM in lua.h - -#ifndef LUABRIDGEDEMO_LUA_VERSION -#define LUABRIDGEDEMO_LUA_VERSION 504 // By default use 5.4 -#endif - -#if !defined(LUALIBRARY_SOURCE) - -#if LUABRIDGEDEMO_LUAU -#include "../../ThirdParty/luau/VM/include/lua.h" -#include "../../ThirdParty/luau/VM/include/luaconf.h" -#include "../../ThirdParty/luau/VM/include/lualib.h" - -#elif LUABRIDGEDEMO_LUAJIT -#include "LuaJIT.2.1/src/lua.hpp" - -#elif LUABRIDGEDEMO_LUA_VERSION >= 504 -#include "Lua.5.4.4/src/lua.hpp" - -#elif LUABRIDGEDEMO_LUA_VERSION >= 503 -#include "Lua.5.3.6/src/lua.hpp" - -#elif LUABRIDGEDEMO_LUA_VERSION >= 502 -#include "Lua.5.2.4/src/lua.hpp" - -#elif LUABRIDGEDEMO_LUA_VERSION >= 501 -extern "C" { -#include "Lua.5.1.5/src/lua.h" -#include "Lua.5.1.5/src/lualib.h" -#include "Lua.5.1.5/src/lauxlib.h" -} // extern "C" - -#else -#error "Unknown LUA_VERSION_NUM" - -#endif // LUABRIDGEDEMO_* - -#endif // LUALIBRARY_SOURCE diff --git a/LuaBridge3/Tests/Lua/LuaLibrary5.1.5.cpp b/LuaBridge3/Tests/Lua/LuaLibrary5.1.5.cpp deleted file mode 100644 index 697051a..0000000 --- a/LuaBridge3/Tests/Lua/LuaLibrary5.1.5.cpp +++ /dev/null @@ -1,124 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2020, Lucio Asnaghi - Copyright (C) 2012, Vinnie Falco - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#define LUALIBRARY_SOURCE -#include "LuaLibrary.h" - -#if LUABRIDGEDEMO_LUA_VERSION == 501 - -#if _MSC_VER -#pragma push_macro("_CRT_SECURE_NO_WARNINGS") -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* luaconf.h only declares some things if it is being included - from certain source files. We define all the relevant macros - and include luaconf.h once so we get all the declarations. -*/ -#define LUA_CORE -#define loslib_c -#include "Lua.5.1.5/src/luaconf.h" -#undef LUA_CORE -#undef loslib_c - -#if _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4244) /* Possible loss of data */ -#pragma warning (disable: 4297) /* Function assumed not to throw an exception but does */ -#pragma warning (disable: 4334) /* Result of 32-bit shift implicitly converted to 64 bits */ -#pragma warning (disable: 4702) /* Unreachable code */ -#elif __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpragmas" -#pragma GCC diagnostic ignored "-Wmisleading-indentation" -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#pragma GCC diagnostic ignored "-Wempty-body" -#pragma GCC diagnostic ignored "-Wstring-plus-int" -#endif - -/* Include this early to prevent the conflict with luai_hashnum - and supress the warning caused by #define lua_assert -*/ -#include "Lua.5.1.5/src/lapi.c" -#include "Lua.5.1.5/src/lauxlib.c" -#include "Lua.5.1.5/src/lbaselib.c" -#include "Lua.5.1.5/src/lcode.c" -#include "Lua.5.1.5/src/ldblib.c" -#include "Lua.5.1.5/src/ldebug.c" -#include "Lua.5.1.5/src/ldo.c" -#include "Lua.5.1.5/src/ldump.c" -#include "Lua.5.1.5/src/lfunc.c" -#include "Lua.5.1.5/src/lgc.c" -#include "Lua.5.1.5/src/linit.c" -#include "Lua.5.1.5/src/liolib.c" -#include "Lua.5.1.5/src/llex.c" -#include "Lua.5.1.5/src/lmathlib.c" -#include "Lua.5.1.5/src/lmem.c" -#include "Lua.5.1.5/src/lobject.c" -#include "Lua.5.1.5/src/lopcodes.c" -#include "Lua.5.1.5/src/loslib.c" -#include "Lua.5.1.5/src/lparser.c" -#include "Lua.5.1.5/src/lstate.c" -#include "Lua.5.1.5/src/lstring.c" -#include "Lua.5.1.5/src/lstrlib.c" -#include "Lua.5.1.5/src/ltable.c" -#include "Lua.5.1.5/src/ltablib.c" -#include "Lua.5.1.5/src/ltm.c" -#include "Lua.5.1.5/src/lundump.c" -#include "Lua.5.1.5/src/lvm.c" -#include "Lua.5.1.5/src/lzio.c" -//#include "Lua.5.1.5/src/print.c" - -/* loadlib.c includes Windows.h, which defines the LoadString macro, - so include it last to prevent errors. -*/ -#include "Lua.5.1.5/src/loadlib.c" - -#if _MSC_VER -#pragma warning (pop) -#elif __GNUC__ -#pragma GCC diagnostic pop -#endif - -#ifdef __cplusplus -} -#endif - -#if _MSC_VER -#pragma pop_macro("_CRT_SECURE_NO_WARNINGS") -#endif - -#endif diff --git a/LuaBridge3/Tests/Lua/LuaLibrary5.2.4.cpp b/LuaBridge3/Tests/Lua/LuaLibrary5.2.4.cpp deleted file mode 100644 index 24d0e91..0000000 --- a/LuaBridge3/Tests/Lua/LuaLibrary5.2.4.cpp +++ /dev/null @@ -1,136 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2020, Lucio Asnaghi - Copyright (C) 2012, Vinnie Falco - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#define LUALIBRARY_SOURCE -#include "Lua/LuaLibrary.h" - -#if LUABRIDGEDEMO_LUA_VERSION == 502 - -#if _MSC_VER -#pragma push_macro("_CRT_SECURE_NO_WARNINGS") -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* luaconf.h only declares some things if it is being included - from certain source files. We define all the relevant macros - and include luaconf.h once so we get all the declarations. -*/ -#define lobject_c -#define lvm_c -#define LUA_CORE -#define LUA_LIB -#include "Lua.5.2.4/src/luaconf.h" -#undef lobject_c -#undef lvm_c -#undef LUA_CORE -#undef LUA_LIB - -#if _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4244) /* Possible loss of data */ -#pragma warning (disable: 4297) /* Function assumed not to throw an exception but does */ -#pragma warning (disable: 4334) /* Result of 32-bit shift implicitly converted to 64 bits */ -#pragma warning (disable: 4702) /* Unreachable code */ -#elif __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpragmas" -#pragma GCC diagnostic ignored "-Wmisleading-indentation" -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#pragma GCC diagnostic ignored "-Wempty-body" -#pragma GCC diagnostic ignored "-Wstring-plus-int" -#endif - -/* Include this early to prevent the conflict with luai_hashnum - and supress the warning caused by #define lua_assert -*/ -#include "Lua.5.2.4/src/ltable.c" - -#include "Lua.5.2.4/src/lauxlib.c" -#include "Lua.5.2.4/src/lbaselib.c" - -#include "Lua.5.2.4/src/lbitlib.c" -#include "Lua.5.2.4/src/lcorolib.c" -#include "Lua.5.2.4/src/ldblib.c" -#include "Lua.5.2.4/src/linit.c" -#include "Lua.5.2.4/src/liolib.c" -#include "Lua.5.2.4/src/lmathlib.c" -#include "Lua.5.2.4/src/loslib.c" -#include "Lua.5.2.4/src/lstrlib.c" -#include "Lua.5.2.4/src/ltablib.c" - -#include "Lua.5.2.4/src/lapi.c" -#include "Lua.5.2.4/src/lcode.c" -#include "Lua.5.2.4/src/lctype.c" -#include "Lua.5.2.4/src/ldebug.c" -#include "Lua.5.2.4/src/ldo.c" -#include "Lua.5.2.4/src/ldump.c" -#include "Lua.5.2.4/src/lfunc.c" -#include "Lua.5.2.4/src/lgc.c" -#include "Lua.5.2.4/src/llex.c" -#include "Lua.5.2.4/src/lmem.c" -#include "Lua.5.2.4/src/lobject.c" -#include "Lua.5.2.4/src/lopcodes.c" -#include "Lua.5.2.4/src/lparser.c" -#include "Lua.5.2.4/src/lstate.c" -#include "Lua.5.2.4/src/lstring.c" -#include "Lua.5.2.4/src/ltm.c" -#include "Lua.5.2.4/src/lundump.c" -#include "Lua.5.2.4/src/lvm.c" -#include "Lua.5.2.4/src/lzio.c" - -/* loadlib.c includes Windows.h, which defines the LoadString macro, - so include it last to prevent errors. -*/ -#include "Lua.5.2.4/src/loadlib.c" - -#if _MSC_VER -#pragma warning (pop) -#elif __GNUC__ -#pragma GCC diagnostic pop -#endif - -#ifdef __cplusplus -} -#endif - -#if _MSC_VER -#pragma pop_macro("_CRT_SECURE_NO_WARNINGS") -#endif - -#else -void dummy_symbol_lua52() {} - -#endif // LUABRIDGEDEMO_LUA_VERSION == 502 diff --git a/LuaBridge3/Tests/Lua/LuaLibrary5.3.6.cpp b/LuaBridge3/Tests/Lua/LuaLibrary5.3.6.cpp deleted file mode 100644 index 1f75b9b..0000000 --- a/LuaBridge3/Tests/Lua/LuaLibrary5.3.6.cpp +++ /dev/null @@ -1,137 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2020, Lucio Asnaghi - Copyright (C) 2012, Vinnie Falco - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#define LUALIBRARY_SOURCE -#include "Lua/LuaLibrary.h" - -#if LUABRIDGEDEMO_LUA_VERSION == 503 - -#if _MSC_VER -#pragma push_macro("_CRT_SECURE_NO_WARNINGS") -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* luaconf.h only declares some things if it is being included - from certain source files. We define all the relevant macros - and include luaconf.h once so we get all the declarations. -*/ -#define lobject_c -#define lvm_c -#define LUA_CORE -#define LUA_LIB -#include "Lua.5.3.6/src/luaconf.h" -#undef lobject_c -#undef lvm_c -#undef LUA_CORE -#undef LUA_LIB - -#if _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4244) /* Possible loss of data */ -#pragma warning (disable: 4297) /* Function assumed not to throw an exception but does */ -#pragma warning (disable: 4334) /* Result of 32-bit shift implicitly converted to 64 bits */ -#pragma warning (disable: 4702) /* Unreachable code */ -#elif __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpragmas" -#pragma GCC diagnostic ignored "-Wmisleading-indentation" -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#pragma GCC diagnostic ignored "-Wempty-body" -#pragma GCC diagnostic ignored "-Wstring-plus-int" -#endif - -/* Include this early to prevent the conflict with luai_hashnum - and supress the warning caused by #define lua_assert -*/ -#include "Lua.5.3.6/src/ltable.c" - -#include "Lua.5.3.6/src/lauxlib.c" -#include "Lua.5.3.6/src/lbaselib.c" - -#include "Lua.5.3.6/src/lbitlib.c" -#include "Lua.5.3.6/src/lcorolib.c" -#include "Lua.5.3.6/src/ldblib.c" -#include "Lua.5.3.6/src/linit.c" -#include "Lua.5.3.6/src/liolib.c" -#include "Lua.5.3.6/src/lmathlib.c" -#include "Lua.5.3.6/src/loslib.c" -#include "Lua.5.3.6/src/lstrlib.c" -#include "Lua.5.3.6/src/ltablib.c" - -#include "Lua.5.3.6/src/lapi.c" -#include "Lua.5.3.6/src/lcode.c" -#include "Lua.5.3.6/src/lctype.c" -#include "Lua.5.3.6/src/ldebug.c" -#include "Lua.5.3.6/src/ldo.c" -#include "Lua.5.3.6/src/ldump.c" -#include "Lua.5.3.6/src/lfunc.c" -#include "Lua.5.3.6/src/lgc.c" -#include "Lua.5.3.6/src/llex.c" -#include "Lua.5.3.6/src/lmem.c" -#include "Lua.5.3.6/src/lobject.c" -#include "Lua.5.3.6/src/lopcodes.c" -#include "Lua.5.3.6/src/lparser.c" -#include "Lua.5.3.6/src/lstate.c" -#include "Lua.5.3.6/src/lstring.c" -#include "Lua.5.3.6/src/ltm.c" -#include "Lua.5.3.6/src/lundump.c" -#include "Lua.5.3.6/src/lvm.c" -#include "Lua.5.3.6/src/lzio.c" -#include "Lua.5.3.6/src/lutf8lib.c" - -/* loadlib.c includes Windows.h, which defines the LoadString macro, - so include it last to prevent errors. -*/ -#include "Lua.5.3.6/src/loadlib.c" - -#if _MSC_VER -#pragma warning (pop) -#elif __GNUC__ -#pragma GCC diagnostic pop -#endif - -#ifdef __cplusplus -} -#endif - -#if _MSC_VER -#pragma pop_macro("_CRT_SECURE_NO_WARNINGS") -#endif - -#else -void dummy_symbol_lua53() {} - -#endif // LUABRIDGEDEMO_LUA_VERSION == 503 diff --git a/LuaBridge3/Tests/Lua/LuaLibrary5.4.4.cpp b/LuaBridge3/Tests/Lua/LuaLibrary5.4.4.cpp deleted file mode 100644 index 56d907a..0000000 --- a/LuaBridge3/Tests/Lua/LuaLibrary5.4.4.cpp +++ /dev/null @@ -1,138 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2020, Lucio Asnaghi - Copyright (C) 2012, Vinnie Falco - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#define LUALIBRARY_SOURCE -#include "Lua/LuaLibrary.h" - -#if LUABRIDGEDEMO_LUA_VERSION == 504 - -#if _MSC_VER -#pragma push_macro("_CRT_SECURE_NO_WARNINGS") -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* luaconf.h only declares some things if it is being included - from certain source files. We define all the relevant macros - and include luaconf.h once so we get all the declarations. -*/ -#define lobject_c -#define lvm_c -#define LUA_CORE -#define LUA_LIB -#include "Lua.5.4.4/src/luaconf.h" -#undef lobject_c -#undef lvm_c -#undef LUA_CORE -#undef LUA_LIB - -#if _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4244) /* Possible loss of data */ -#pragma warning (disable: 4297) /* Function assumed not to throw an exception but does */ -#pragma warning (disable: 4310) /* Cast truncates constant value */ -#pragma warning (disable: 4334) /* Result of 32-bit shift implicitly converted to 64 bits */ -#pragma warning (disable: 4701) /* Potentially uninitialized local variable */ -#pragma warning (disable: 4702) /* Unreachable code */ -#elif __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpragmas" -#pragma GCC diagnostic ignored "-Wmisleading-indentation" -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#pragma GCC diagnostic ignored "-Wempty-body" -#pragma GCC diagnostic ignored "-Wstring-plus-int" -#endif - -/* Include this early to prevent the conflict with luai_hashnum - and supress the warning caused by #define lua_assert -*/ -#include "Lua.5.4.4/src/ltable.c" - -#include "Lua.5.4.4/src/lauxlib.c" -#include "Lua.5.4.4/src/lbaselib.c" - -#include "Lua.5.4.4/src/lcorolib.c" -#include "Lua.5.4.4/src/ldblib.c" -#include "Lua.5.4.4/src/linit.c" -#include "Lua.5.4.4/src/liolib.c" -#include "Lua.5.4.4/src/lmathlib.c" -#include "Lua.5.4.4/src/loslib.c" -#include "Lua.5.4.4/src/lstrlib.c" -#include "Lua.5.4.4/src/ltablib.c" - -#include "Lua.5.4.4/src/lapi.c" -#include "Lua.5.4.4/src/lcode.c" -#include "Lua.5.4.4/src/lctype.c" -#include "Lua.5.4.4/src/ldebug.c" -#include "Lua.5.4.4/src/ldo.c" -#include "Lua.5.4.4/src/ldump.c" -#include "Lua.5.4.4/src/lfunc.c" -#include "Lua.5.4.4/src/lgc.c" -#include "Lua.5.4.4/src/llex.c" -#include "Lua.5.4.4/src/lmem.c" -#include "Lua.5.4.4/src/lobject.c" -#include "Lua.5.4.4/src/lopcodes.c" -#include "Lua.5.4.4/src/lparser.c" -#include "Lua.5.4.4/src/lstate.c" -#include "Lua.5.4.4/src/lstring.c" -#include "Lua.5.4.4/src/ltm.c" -#include "Lua.5.4.4/src/lundump.c" -#include "Lua.5.4.4/src/lvm.c" -#include "Lua.5.4.4/src/lzio.c" -#include "Lua.5.4.4/src/lutf8lib.c" - -/* loadlib.c includes Windows.h, which defines the LoadString macro, - so include it last to prevent errors. -*/ -#include "Lua.5.4.4/src/loadlib.c" - -#if _MSC_VER -#pragma warning (pop) -#elif __GNUC__ -#pragma GCC diagnostic pop -#endif - -#ifdef __cplusplus -} -#endif - -#if _MSC_VER -#pragma pop_macro("_CRT_SECURE_NO_WARNINGS") -#endif - -#else -void dummy_symbol_lua54() {} - -#endif // LUABRIDGEDEMO_LUA_VERSION = 504 diff --git a/LuaBridge3/Tests/Lua/Luau.cpp b/LuaBridge3/Tests/Lua/Luau.cpp deleted file mode 100644 index a5a2632..0000000 --- a/LuaBridge3/Tests/Lua/Luau.cpp +++ /dev/null @@ -1,86 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2021, Lucio Asnaghi - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#define LUALIBRARY_SOURCE -#include "Lua/LuaLibrary.h" - -#if LUABRIDGEDEMO_LUAU - -#if _MSC_VER -#pragma push_macro("_CRT_SECURE_NO_WARNINGS") -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif -#endif - -// Ast -#include "../../ThirdParty/luau/Ast/src/Parser.cpp" - -// Vm -#include "../../ThirdParty/luau/VM/src/ltm.cpp" -#include "../../ThirdParty/luau/VM/src/lvmexecute.cpp" -#include "../../ThirdParty/luau/VM/src/lfunc.cpp" -#include "../../ThirdParty/luau/VM/src/lbitlib.cpp" -#include "../../ThirdParty/luau/VM/src/lvmload.cpp" -#include "../../ThirdParty/luau/VM/src/lapi.cpp" -#include "../../ThirdParty/luau/VM/src/ldebug.cpp" -#include "../../ThirdParty/luau/VM/src/lbaselib.cpp" -#include "../../ThirdParty/luau/VM/src/loslib.cpp" -#include "../../ThirdParty/luau/VM/src/lutf8lib.cpp" -#include "../../ThirdParty/luau/VM/src/lstring.cpp" -#include "../../ThirdParty/luau/VM/src/lmem.cpp" -#include "../../ThirdParty/luau/VM/src/lcorolib.cpp" -#include "../../ThirdParty/luau/VM/src/lstate.cpp" -#include "../../ThirdParty/luau/VM/src/lgc.cpp" -#include "../../ThirdParty/luau/VM/src/ldo.cpp" -#include "../../ThirdParty/luau/VM/src/ltablib.cpp" -#include "../../ThirdParty/luau/VM/src/lstrlib.cpp" -#include "../../ThirdParty/luau/VM/src/lobject.cpp" -#include "../../ThirdParty/luau/VM/src/laux.cpp" -#include "../../ThirdParty/luau/VM/src/ltable.cpp" -#include "../../ThirdParty/luau/VM/src/lvmutils.cpp" -#include "../../ThirdParty/luau/VM/src/linit.cpp" -#include "../../ThirdParty/luau/VM/src/lmathlib.cpp" -#include "../../ThirdParty/luau/VM/src/lbuiltins.cpp" -#include "../../ThirdParty/luau/VM/src/ldblib.cpp" -#include "../../ThirdParty/luau/VM/src/lnumprint.cpp" -#include "../../ThirdParty/luau/VM/src/ludata.cpp" - -/* lperf.cpp includes Windows.h, which defines the min and max macro, - so include it last to prevent errors. -*/ -#include "../../ThirdParty/luau/VM/src/lperf.cpp" - -#if _MSC_VER -#pragma pop_macro("_CRT_SECURE_NO_WARNINGS") -#endif - -#else -void dummy_symbol_luau() {} - -#endif // LUABRIDGEDEMO_LUAU diff --git a/LuaBridge3/Tests/Lua/LuauSplit.cpp b/LuaBridge3/Tests/Lua/LuauSplit.cpp deleted file mode 100644 index 53acce7..0000000 --- a/LuaBridge3/Tests/Lua/LuauSplit.cpp +++ /dev/null @@ -1,67 +0,0 @@ -//============================================================================== -/* - https://github.com/kunitoki/LuaBridge3 - - Copyright (C) 2021, Lucio Asnaghi - - License: The MIT License (http://www.opensource.org/licenses/mit-license.php) - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -//============================================================================== - -#define LUALIBRARY_SOURCE -#include "Lua/LuaLibrary.h" - -#if LUABRIDGEDEMO_LUAU - -#if _MSC_VER -#pragma push_macro("_CRT_SECURE_NO_WARNINGS") -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif -#endif - -// Compiler -#include "../../ThirdParty/luau/Compiler/src/BuiltinFolding.cpp" -#include "../../ThirdParty/luau/Compiler/src/Builtins.cpp" -#include "../../ThirdParty/luau/Compiler/src/BytecodeBuilder.cpp" -#include "../../ThirdParty/luau/Compiler/src/Compiler.cpp" -#include "../../ThirdParty/luau/Compiler/src/ConstantFolding.cpp" -#include "../../ThirdParty/luau/Compiler/src/CostModel.cpp" -#include "../../ThirdParty/luau/Compiler/src/lcode.cpp" -#include "../../ThirdParty/luau/Compiler/src/TableShape.cpp" -#include "../../ThirdParty/luau/Compiler/src/ValueTracking.cpp" - -// Ast -#include "../../ThirdParty/luau/Ast/src/Ast.cpp" -#include "../../ThirdParty/luau/Ast/src/Confusables.cpp" -#include "../../ThirdParty/luau/Ast/src/StringUtils.cpp" -#include "../../ThirdParty/luau/Ast/src/Location.cpp" -#include "../../ThirdParty/luau/Ast/src/Lexer.cpp" -#include "../../ThirdParty/luau/Ast/src/TimeTrace.cpp" - -#if _MSC_VER -#pragma pop_macro("_CRT_SECURE_NO_WARNINGS") -#endif - -#else -void dummy_symbol_luau2() {} - -#endif // LUABRIDGEDEMO_LUAU diff --git a/LuaBridge3/Tests/README.md b/LuaBridge3/Tests/README.md deleted file mode 100644 index 782e462..0000000 --- a/LuaBridge3/Tests/README.md +++ /dev/null @@ -1,80 +0,0 @@ -# LuaBridge Unit Tests - -## Linux and MacOS - -Have CMake 3.5+ and a compiler supporting C++17 installed. - -Run cmake to generate Makefiles: - -```sh -mkdir build -cd build -cmake -G "Unix Makefiles" ../ -``` - -Build the project: - -```sh -cmake --build . -- -j8 -``` - -Run tests from the `build` directory: - -```sh -./Tests/LuaBridgeTests51 -./Tests/LuaBridgeTests51Noexcept -./Tests/LuaBridgeTests52 -./Tests/LuaBridgeTests52Noexcept -./Tests/LuaBridgeTests53 -./Tests/LuaBridgeTests53Noexcept -./Tests/LuaBridgeTests54 -./Tests/LuaBridgeTests54Noexcept -``` - -## macOS - -Have CMake 3.5+ and Xcode 11+ installed. - -Run cmake to generate Makefiles: - -```sh -mkdir build -cd build -cmake -G "Xcode" ../ -``` - -Build the project: - -```sh -cmake --build . -- -j8 -``` - -Run tests from the `build` directory: - -```sh -./Tests/LuaBridgeTests51 -./Tests/LuaBridgeTests51Noexcept -./Tests/LuaBridgeTests52 -./Tests/LuaBridgeTests52Noexcept -./Tests/LuaBridgeTests53 -./Tests/LuaBridgeTests53Noexcept -./Tests/LuaBridgeTests54 -./Tests/LuaBridgeTests54Noexcept -``` - -# Windows - -Have CMake 3.5+ and MSVC 2019 installed. - -Run cmake to generate MSVC solution and project files (run `cmake -G` to see all variants): - -```cmd -mkdir build -cd build -cmake -G "Visual Studio 16" ../ -``` - -Open the solution `LuaBridge.sln` in MSVC. - -Set `LuaBridgeTests51` or `LuaBridgeTests52` or `LuaBridgeTests53` or `LuaBridgeTests54` or their corresponding Noexcept version -as a startup project and run. diff --git a/LuaBridge3/Tests/Source/AmalgamateTests.cpp b/LuaBridge3/Tests/Source/AmalgamateTests.cpp deleted file mode 100644 index cc76d09..0000000 --- a/LuaBridge3/Tests/Source/AmalgamateTests.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2021, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "Lua/LuaLibrary.h" - -#define luabridge luabridge_amalgamated -#include "../../Distribution/LuaBridge/LuaBridge.h" -#undef luabridge - -#if _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4099) /* Type name first seen using 'class' now seen using 'struct' */ -#endif - -#include - -TEST(AmalgamateTests, CompilationWorks) -{ - auto L = luaL_newstate(); - luaL_openlibs(L); - - luabridge_amalgamated::getGlobalNamespace(L) - .beginClass("Test") - .endClass(); - - lua_close(L); -} - -#if _MSC_VER -#pragma warning (pop) -#endif diff --git a/LuaBridge3/Tests/Source/ArrayTests.cpp b/LuaBridge3/Tests/Source/ArrayTests.cpp deleted file mode 100644 index d255446..0000000 --- a/LuaBridge3/Tests/Source/ArrayTests.cpp +++ /dev/null @@ -1,168 +0,0 @@ -// https://github.com/vinniefalco/LuaBridge -// Copyright 2021, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" -#include "TestTypes.h" - -#include "LuaBridge/Array.h" - -#include -#include - -namespace { -template -void checkEquals(const std::array& expected, const std::array& actual) -{ - using U = std::decay_t; - - if constexpr (std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_FLOAT_EQ(expected[i], actual[i]); - } - else if constexpr (std::is_same_v || std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_DOUBLE_EQ(expected[i], actual[i]); - } - else if constexpr (std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_STREQ(expected[i], actual[i]); - } - else - { - ASSERT_EQ(expected, actual); - } -} -} // namespace - -template -struct ArrayTest : TestBase -{ -}; - -TYPED_TEST_SUITE_P(ArrayTest); - -TYPED_TEST_P(ArrayTest, LuaRef) -{ - using Traits = TypeTraits; - - this->runLua("result = {" + Traits::list() + "}"); - - std::array expected; - std::copy_n(Traits::values().begin(), 3, expected.begin()); - - std::array actual = this->result(); - - checkEquals(expected, actual); -} - -REGISTER_TYPED_TEST_SUITE_P(ArrayTest, LuaRef); - -INSTANTIATE_TYPED_TEST_SUITE_P(ArrayTest, ArrayTest, TestTypes); - -namespace { -struct Data -{ - /* explicit */ Data(int i = -1000) : i(i) {} - - int i; -}; - -bool operator==(const Data& lhs, const Data& rhs) -{ - return lhs.i == rhs.i; -} - -std::ostream& operator<<(std::ostream& lhs, const Data& rhs) -{ - lhs << "{" << rhs.i << "}"; - return lhs; -} - -template -std::array processValues(const std::array& data) -{ - return data; -} - -template -std::array processPointers(const std::array& data) -{ - std::array result; - std::size_t arrayIndex = 0; - for (const auto* item : data) - { - result[arrayIndex] = *item; - ++arrayIndex; - } - return result; -} -} // namespace - -struct ArrayTests : TestBase -{ -}; - -TEST_F(ArrayTests, PassFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Data") - .addConstructor() - .endClass() - .addFunction("processValues", &processValues<3>) - .addFunction("processPointers", &processPointers<3>); - - resetResult(); - runLua("result = processValues ({Data (-1), Data (2), Data (5)})"); - - ASSERT_EQ((std::array({-1, 2, 5})), (result>())); - - resetResult(); - runLua("result = processPointers ({Data (-3), Data (4), Data (9)})"); - - ASSERT_EQ((std::array({-3, 4, 9})), (result>())); -} - -TEST_F(ArrayTests, FailOnWrongSize) -{ - runLua("result = { 1, 2, 3, 4 }"); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_ANY_THROW((result>())); -#else - auto castResult = result().cast>(); - ASSERT_FALSE(castResult); -#endif - - auto result = luabridge::push(L, std::array{ 5, 6, 7, 8 }); - ASSERT_TRUE(result); - - EXPECT_TRUE((luabridge::isInstance>(L, -1))); - EXPECT_FALSE((luabridge::isInstance>(L, -1))); -} - -#if !LUABRIDGE_HAS_EXCEPTIONS -TEST_F(ArrayTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) -{ - class Unregistered {}; - - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::array v; - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); -} -#endif diff --git a/LuaBridge3/Tests/Source/ClassTests.cpp b/LuaBridge3/Tests/Source/ClassTests.cpp deleted file mode 100644 index 31bc037..0000000 --- a/LuaBridge3/Tests/Source/ClassTests.cpp +++ /dev/null @@ -1,2617 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include -#include -#include -#include -#include - -struct ClassTests : TestBase -{ - template - T variable(const std::string& name) - { - runLua("result = " + name); - return result(); - } -}; - -namespace { -struct EmptyBase -{ -}; - -template -struct Class : Base -{ - Class() : data() {} - - Class(T data) : data(data) {} - - static Class staticFunction(Class value) { return value; } - - std::string toString() const - { - std::ostringstream stream; - stream << data; - return stream.str(); - } - - bool operator==(const Class& rhs) const { return data == rhs.data; } - - bool operator<(const Class& rhs) const { return data < rhs.data; } - - bool operator<=(const Class& rhs) const { return data <= rhs.data; } - - Class operator+(const Class& rhs) const - { - return Class(data + rhs.data); - } - - Class operator-(const Class& rhs) const - { - return Class(data - rhs.data); - } - - Class operator*(const Class& rhs) const - { - return Class(data * rhs.data); - } - - Class operator/(const Class& rhs) const - { - return Class(data / rhs.data); - } - - Class operator%(const Class& rhs) const - { - return Class(data % rhs.data); - } - - Class operator()(T param) { return Class(param); } - - int len() const { return data; } - - Class negate() const { return Class(-data); } - - T method(T value) { return value; } - - T methodState(T value, lua_State*) { return value; } - - T constMethod(T value) const { return value; } - - T getData() const { return data; } - T getDataNoexcept() const noexcept { return data; } - - void setData(T data) { this->data = data; } - void setDataNoexcept(T data) noexcept { this->data = data; } - - T getDataState(lua_State*) const { return data; } - T getDataStateNoexcept(lua_State*) const noexcept { return data; } - - void setDataState(T data, lua_State*) { this->data = data; } - void setDataStateNoexcept(T data, lua_State*) noexcept { this->data = data; } - - static T getStaticData() { return staticData; } - static void setStaticData(T data) { staticData = data; } - - static T getStaticDataNoexcept() noexcept { return staticData; } - static void setStaticDataNoexcept(T data) noexcept { staticData = data; } - - mutable T data; - static T staticData; - static const T staticConstData; -}; - -template -T Class::staticData = {}; - -template -const T Class::staticConstData = {}; -} // namespace - -TEST_F(ClassTests, IsInstance) -{ - using BaseClass = Class; - using OtherClass = Class; - using DerivedClass = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("BaseClass") - .endClass() - .deriveClass("DerivedClass") - .endClass() - .beginClass("OtherClass") - .endClass(); - - BaseClass base; - auto result1 = luabridge::push(L, base); - ASSERT_TRUE(result1); - - DerivedClass derived; - auto result2 = luabridge::push(L, derived); - ASSERT_TRUE(result2); - - OtherClass other; - auto result3 = luabridge::push(L, other); - ASSERT_TRUE(result3); - - ASSERT_TRUE(luabridge::isInstance(L, -3)); - ASSERT_FALSE(luabridge::isInstance(L, -3)); // BaseClass is not DerivedClass - ASSERT_FALSE(luabridge::isInstance(L, -3)); - - ASSERT_TRUE(luabridge::isInstance(L, -2)); - ASSERT_TRUE(luabridge::isInstance(L, -2)); // DerivedClass is BaseClass - ASSERT_FALSE(luabridge::isInstance(L, -2)); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); -} - -TEST_F(ClassTests, PassingUnregisteredClassToLuaThrows) -{ - using Unregistered = Class; - - runLua("function process_fn (value) end"); - - auto process_fn = luabridge::getGlobal(L, "process_fn"); - ASSERT_TRUE(process_fn.isFunction()); - - Unregistered value(1); - const Unregistered constValue(2); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(process_fn(value), std::exception); - ASSERT_THROW(process_fn(constValue), std::exception); - ASSERT_THROW(process_fn(&value), std::exception); - ASSERT_THROW(process_fn(&constValue), std::exception); -#else - EXPECT_FALSE(process_fn(value)); - EXPECT_FALSE(process_fn(constValue)); - EXPECT_FALSE(process_fn(&value)); - EXPECT_FALSE(process_fn(&constValue)); -#endif -} - -TEST_F(ClassTests, PassWrongClassFromLuaThrows) -{ - using Right = Class; - using WrongBase = Class; - using Wrong = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Right") - .endClass() - .beginClass("WrongBase") - .endClass() - .beginClass("Wrong") - .addConstructor() - .endClass() - .addFunction("processRight", &Right::staticFunction); - - // bad argument #1 to 'processRight' (Right expected, got Wrong) -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = processRight (Wrong (5))"), std::exception); -#else - ASSERT_FALSE(runLua("result = processRight (Wrong (5))")); -#endif - - ASSERT_TRUE(result().isNil()); -} - -TEST_F(ClassTests, PassDerivedClassInsteadOfBase) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .endClass() - .deriveClass("Derived") - .addConstructor() - .endClass() - .addFunction("processBase", &Base::staticFunction); - - runLua("result = processBase (Derived (3.14))"); - ASSERT_EQ(0, result().data); -} - -namespace { -template -T processNonConst(Class* object) -{ - return object->data; -} -} // namespace - -TEST_F(ClassTests, PassConstClassInsteadOfNonConstThrows) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .endClass() - .deriveClass("Derived") - .endClass() - .addFunction("processNonConst", &processNonConst); - - const Derived constObject(1.2f); - luabridge::setGlobal(L, &constObject, "constObject"); - - // bad argument #1 to 'processNonConst' (Derived expected, got const Derived) -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = processNonConst (constObject)"), std::exception); -#else - ASSERT_FALSE(runLua("result = processNonConst (constObject)")); -#endif - - ASSERT_TRUE(result().isNil()); -} - -TEST_F(ClassTests, PassOtherTypeInsteadOfNonConstThrows) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() // Show that it does't matter - .endClass() - .addFunction("processNonConst", &processNonConst); - - // bad argument #1 to 'processNonConst' (Int expected, got number) -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = processNonConst (1)"), std::exception); -#else - ASSERT_FALSE(runLua("result = processNonConst (1)")); -#endif - - ASSERT_TRUE(result().isNil()); -} - -TEST_F(ClassTests, PassRegisteredClassInsteadOfUnregisteredThrows) -{ - using Int = Class; - using Float = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Float") - .addConstructor() - .endClass() - .addFunction("processUnregisteredInt", &Int::staticFunction); - - // bad argument #1 to 'processUnregisteredInt' (unregistered class expected, got Float) -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = processUnregisteredInt (Float (1.2))"), std::exception); -#else - ASSERT_FALSE(runLua("result = processUnregisteredInt (Float (1.2))")); -#endif - - ASSERT_TRUE(result().isNil()); -} - -namespace { -Class& returnRef() -{ - static Class value(1); - return value; -} - -const Class& returnConstRef() -{ - return returnRef(); -} - -Class* returnPtr() -{ - return &returnRef(); -} - -const Class* returnConstPtr() -{ - return &returnConstRef(); -} - -Class returnValue() -{ - return Class(2); -} - -void addHelperFunctions(lua_State* L) -{ - luabridge::getGlobalNamespace(L) - .addFunction("returnRef", &returnRef) - .addFunction("returnConstRef", &returnConstRef) - .addFunction("returnPtr", &returnPtr) - .addFunction("returnConstPtr", &returnConstPtr) - .addFunction("returnValue", &returnValue); -} -} // namespace - -TEST_F(ClassTests, PassingUnregisteredClassFromLuaThrows) -{ - addHelperFunctions(L); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = returnRef ()"), std::exception); - ASSERT_THROW(runLua("result = returnConstRef ()"), std::exception); - ASSERT_THROW(runLua("result = returnPtr ()"), std::exception); - ASSERT_THROW(runLua("result = returnConstPtr ()"), std::exception); - ASSERT_THROW(runLua("result = returnValue ()"), std::exception); -#else - ASSERT_FALSE(runLua("result = returnRef ()")); - ASSERT_FALSE(runLua("result = returnConstRef ()")); - ASSERT_FALSE(runLua("result = returnPtr ()")); - ASSERT_FALSE(runLua("result = returnConstPtr ()")); - ASSERT_FALSE(runLua("result = returnValue ()")); -#endif -} - -#if LUABRIDGE_HAS_EXCEPTIONS -TEST_F(ClassTests, DeriveFromUnregisteredClassThrows) -{ - using Base = Class; - using Derived = Class; - - EXPECT_EQ(0, lua_gettop(L)); - - EXPECT_THROW((luabridge::getGlobalNamespace(L).deriveClass("Derived")), std::exception); - - EXPECT_EQ(0, lua_gettop(L)); -} -#endif - -struct ClassFunctions : ClassTests -{ -}; - -TEST_F(ClassFunctions, MemberFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", &Int::method) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():method (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():method (3)"); - ASSERT_EQ(3, result()); -} - -TEST_F(ClassFunctions, MemberFunctions_PassState) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", &Int::methodState) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():method (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():method (3)"); - ASSERT_EQ(3, result()); -} - -TEST_F(ClassFunctions, ConstMemberFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("constMethod", &Int::constMethod) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():constMethod (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ():constMethod (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnPtr ():constMethod (3)"); - ASSERT_EQ(3, result()); - - runLua("result = returnConstPtr ():constMethod (4)"); - ASSERT_EQ(4, result()); - - runLua("result = returnValue ():constMethod (5)"); - ASSERT_EQ(5, result()); -} - -namespace { -struct ClassWithTemplateMembers -{ - template - int templateMethod(Args&&...) - { - return static_cast(sizeof...(Args)); - } - - template - int templateMethodNoexcept(Args&&...) noexcept - { - return static_cast(sizeof...(Args)); - } -}; - -template -T proxyFunction(Class* object, T value) -{ - object->data = value; - return value; -} - -template -T proxyFunctionNoexcept(Class* object, T value) noexcept -{ - object->data = value; - return value; -} - -template -T proxyFunctionState(Class* object, T value, lua_State*) -{ - object->data = value; - return value; -} - -template -T proxyFunctionStateNoexcept(Class* object, T value, lua_State*) noexcept -{ - object->data = value; - return value; -} - -template -T proxyConstFunction(const Class* object, T value) -{ - return value; -} - -template -T proxyConstFunctionNoexcept(const Class* object, T value) noexcept -{ - return value; -} - -int proxyCFunctionState(lua_State* L) -{ - using Int = Class; - - auto ref = luabridge::LuaRef::fromStack(L, 1); - if (!ref.isUserdata() || !ref.isInstance()) { - return 0; - } - - auto arg = luabridge::LuaRef::fromStack(L, 2); - if (!arg.isNumber()) { - return 0; - } - - [[maybe_unused]] auto result = luabridge::push(L, arg.unsafe_cast() + 1000); - - return 1; -} -} // namespace - -TEST_F(ClassFunctions, ClassWithTemplateMembers) -{ - luabridge::getGlobalNamespace(L) - .beginClass("ClassWithTemplateMembers") - .addConstructor() - .addFunction("method", &ClassWithTemplateMembers::templateMethod) - .addFunction("methodNoexcept", &ClassWithTemplateMembers::templateMethodNoexcept) - .endClass(); - - runLua("local a = ClassWithTemplateMembers(); result = a:method()"); - ASSERT_EQ(0, result()); - - runLua("local a = ClassWithTemplateMembers(); result = a:methodNoexcept()"); - ASSERT_EQ(0, result()); -} - -TEST_F(ClassFunctions, ProxyFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", &proxyFunction) - .addFunction("methodNoexcept", &proxyFunctionNoexcept) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():method (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():method (3)"); - ASSERT_EQ(3, result()); - - runLua("result = returnRef ():methodNoexcept (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().methodNoexcept"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():methodNoexcept (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().methodNoexcept"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():methodNoexcept (3)"); - ASSERT_EQ(3, result()); -} - -TEST_F(ClassFunctions, ProxyFunctions_PassState) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", &proxyFunctionState) - .addFunction("methodNoexcept", &proxyFunctionStateNoexcept) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():method (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():method (3)"); - ASSERT_EQ(3, result()); - - runLua("result = returnRef ():methodNoexcept (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().methodNoexcept"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():methodNoexcept (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().methodNoexcept"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():methodNoexcept (3)"); - ASSERT_EQ(3, result()); -} - -TEST_F(ClassFunctions, ConstProxyFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("constMethod", &proxyConstFunction) - .addFunction("constMethodNoexcept", &proxyConstFunctionNoexcept) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():constMethod (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ():constMethod (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnPtr ():constMethod (3)"); - ASSERT_EQ(3, result()); - - runLua("result = returnConstPtr ():constMethod (4)"); - ASSERT_EQ(4, result()); - - runLua("result = returnValue ():constMethod (5)"); - ASSERT_EQ(5, result()); - - runLua("result = returnRef ():constMethodNoexcept (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ():constMethodNoexcept (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnPtr ():constMethodNoexcept (3)"); - ASSERT_EQ(3, result()); - - runLua("result = returnConstPtr ():constMethodNoexcept (4)"); - ASSERT_EQ(4, result()); - - runLua("result = returnValue ():constMethodNoexcept (5)"); - ASSERT_EQ(5, result()); -} - -TEST_F(ClassFunctions, ProxyCFunction) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", &proxyCFunctionState) - .endClass(); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1000)"); - ASSERT_EQ(2000, result()); -} - -TEST_F(ClassFunctions, StdFunctions) -{ - using Int = Class; - - auto sharedData = std::make_shared(); - std::weak_ptr data = sharedData; // Check __gc meta-method - - std::function function = [sharedData](Int* object, int value) { - object->data = value; - return value; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", std::move(function)) - .endClass(); - - function = nullptr; - sharedData.reset(); - ASSERT_FALSE(data.expired()); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():method (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():method (3)"); - ASSERT_EQ(3, result()); - - runLua("result = nil"); - closeLuaState(); // Force garbage collection - - ASSERT_TRUE(data.expired()); -} - -TEST_F(ClassFunctions, StdFunctions_PassState) -{ - using Int = Class; - - auto sharedData = std::make_shared(); - std::weak_ptr data = sharedData; // Check __gc meta-method - - std::function function = - [sharedData](Int* object, int value, lua_State*) { - object->data = value; - return value; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("method", std::move(function)) - .endClass(); - - function = nullptr; - sharedData.reset(); - ASSERT_FALSE(data.expired()); - - addHelperFunctions(L); - - runLua("result = returnRef ():method (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnPtr ():method (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnConstPtr ().method"); // Don't call, just get - ASSERT_TRUE(result().isNil()); - - runLua("result = returnValue ():method (3)"); - ASSERT_EQ(3, result()); - - runLua("result = nil"); - closeLuaState(); // Force garbage collection - - ASSERT_TRUE(data.expired()); -} - -TEST_F(ClassFunctions, ConstStdFunctions) -{ - using Int = Class; - - auto sharedData = std::make_shared(); - std::weak_ptr data = sharedData; // Check __gc meta-method - - std::function function = [sharedData](const Int* object, int value) { - object->data = value; - return value; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("constMethod", std::move(function)) - .endClass(); - - function = nullptr; - sharedData.reset(); - ASSERT_FALSE(data.expired()); - - addHelperFunctions(L); - - runLua("result = returnRef ():constMethod (1)"); - ASSERT_EQ(1, result()); - - runLua("result = returnConstRef ():constMethod (2)"); - ASSERT_EQ(2, result()); - - runLua("result = returnPtr ():constMethod (3)"); - ASSERT_EQ(3, result()); - - runLua("result = returnConstPtr ():constMethod (4)"); - ASSERT_EQ(4, result()); - - runLua("result = returnValue ():constMethod (5)"); - ASSERT_EQ(5, result()); - - runLua("result = nil"); - closeLuaState(); // Force garbage collection - - ASSERT_TRUE(data.expired()); -} - -struct ClassProperties : ClassTests -{ -}; - -TEST_F(ClassProperties, FieldPointers) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &Int::data, true) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - - runLua("result.data = 2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(2, result()["data"].cast()); - - runLua("result = Int (42).data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); -} - -TEST_F(ClassProperties, FieldPointers_ReadOnly) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &Int::data, false) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result.data = 2"), std::exception); -#else - ASSERT_FALSE(runLua("result.data = 2")); -#endif - - runLua("result = Int (42).data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); -} - -TEST_F(ClassProperties, MemberFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &Int::getData, &Int::setData) - .addProperty("dataNoexcept", &Int::getDataNoexcept, &Int::setDataNoexcept) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - ASSERT_TRUE(result()["dataNoexcept"].isNumber()); - ASSERT_EQ(501, result()["dataNoexcept"].cast()); - - runLua("result.data = -2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(-2, result()["data"].cast()); - - runLua("result.dataNoexcept = -2"); - ASSERT_TRUE(result()["dataNoexcept"].isNumber()); - ASSERT_EQ(-2, result()["dataNoexcept"].cast()); -} - -TEST_F(ClassProperties, MemberFunctions_PassState) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &Int::getDataState, &Int::setDataState) - .addProperty("dataNoexcept", &Int::getDataStateNoexcept, &Int::setDataStateNoexcept) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - ASSERT_TRUE(result()["dataNoexcept"].isNumber()); - ASSERT_EQ(501, result()["dataNoexcept"].cast()); - - runLua("result.data = -2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(-2, result()["data"].cast()); - - runLua("result.dataNoexcept = -2"); - ASSERT_TRUE(result()["dataNoexcept"].isNumber()); - ASSERT_EQ(-2, result()["dataNoexcept"].cast()); -} - -TEST_F(ClassProperties, MemberFunctions_ReadOnly) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &Int::getData) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result.data = -2"), std::exception); -#else - ASSERT_FALSE(runLua("result.data = -2")); -#endif - - ASSERT_EQ(501, result()["data"].cast()); -} - -TEST_F(ClassProperties, MemberFunctions_Derived) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addProperty("data", &Base::getData, &Base::setData) - .endClass() - .deriveClass("Derived") - .endClass(); - - Derived derived(12); - derived.Base::data = "abc"; - luabridge::setGlobal(L, &derived, "derived"); - - runLua("result = derived.data"); - ASSERT_TRUE(result().isString()); - ASSERT_EQ("abc", result()); - - runLua("derived.data = 5"); // Lua just casts integer to string - ASSERT_EQ("5", derived.Base::data); - ASSERT_EQ(12, derived.data); - - runLua("derived.data = '123'"); - ASSERT_EQ("123", derived.Base::data); - ASSERT_EQ(12, derived.data); -} - -TEST_F(ClassProperties, MemberFunctions_Overridden) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addProperty("data", &Base::getData, &Base::setData) - .endClass() - .deriveClass("Derived") - .addProperty("data", &Derived::getData, &Derived::setData) - .endClass(); - - Derived derived(50); - derived.Base::data = 1.23f; - luabridge::setGlobal(L, static_cast(&derived), "base"); - luabridge::setGlobal(L, &derived, "derived"); - - runLua("result = base.data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1.23f, result()); - - runLua("result = derived.data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(50, result()); - - runLua("base.data = -3.14"); - ASSERT_EQ(-3.14f, derived.Base::data); - ASSERT_EQ(50, derived.data); - - runLua("derived.data = 7"); - ASSERT_EQ(-3.14f, derived.Base::data); - ASSERT_EQ(7, derived.data); -} - -namespace { -template -T getData(const Class* object) -{ - return object->data; -} - -template -void setData(Class* object, T data) -{ - object->data = data; -} - -template -T getDataNoexcept(const Class* object) noexcept -{ - return object->data; -} - -template -void setDataNoexcept(Class* object, T data) noexcept -{ - object->data = data; -} -} // namespace - -TEST_F(ClassProperties, ProxyFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &getData, &setData) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - - runLua("result.data = -2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(-2, result()["data"].cast()); -} - -TEST_F(ClassProperties, ProxyFunctionsNoexcept) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &getDataNoexcept, &setDataNoexcept) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - - runLua("result.data = -2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(-2, result()["data"].cast()); -} - -TEST_F(ClassProperties, ProxyFunctions_ReadOnly) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &getData) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result.data = -2"), std::exception); -#else - ASSERT_FALSE(runLua("result.data = -2")); -#endif - - ASSERT_EQ(501, result()["data"].cast()); -} - -TEST_F(ClassProperties, ProxyFunctions_Derived) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addProperty("data", &getData, &setData) - .endClass() - .deriveClass("Derived") - .endClass(); - - Derived derived(12); - derived.Base::data = "abc"; - luabridge::setGlobal(L, &derived, "derived"); - - runLua("result = derived.data"); - ASSERT_TRUE(result().isString()); - ASSERT_EQ("abc", result()); - - runLua("derived.data = 5"); // Lua just casts integer to string - ASSERT_EQ("5", derived.Base::data); - ASSERT_EQ(12, derived.data); - - runLua("derived.data = '123'"); - ASSERT_EQ("123", derived.Base::data); - ASSERT_EQ(12, derived.data); -} - -TEST_F(ClassProperties, ProxyFunctions_Overridden) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addProperty("data", &getData, &setData) - .endClass() - .deriveClass("Derived") - .addProperty("data", &getData, &setData) - .endClass(); - - Derived derived(50); - derived.Base::data = 1.23f; - luabridge::setGlobal(L, static_cast(&derived), "base"); - luabridge::setGlobal(L, &derived, "derived"); - - runLua("result = base.data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1.23f, result()); - - runLua("result = derived.data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(50, result()); - - runLua("base.data = -3.14"); - ASSERT_EQ(-3.14f, derived.Base::data); - ASSERT_EQ(50, derived.data); - - runLua("derived.data = 7"); - ASSERT_EQ(-3.14f, derived.Base::data); - ASSERT_EQ(7, derived.data); -} - -namespace { -template -int getDataC(lua_State* L) -{ - auto objectRef = luabridge::LuaRef::fromStack(L, 1); - auto* object = objectRef.unsafe_cast*>(); - - [[maybe_unused]] auto result = luabridge::Stack::push(L, object->data); - - return 1; -} - -template -int setDataC(lua_State* L) -{ - auto objectRef = luabridge::LuaRef::fromStack(L, 1); - auto* object = objectRef.unsafe_cast*>(); - auto valueRef = luabridge::LuaRef::fromStack(L, 2); - T value = valueRef.unsafe_cast(); - object->data = value; - return 0; -} -} // namespace - -TEST_F(ClassProperties, ProxyCFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &getDataC, &setDataC) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - - runLua("result.data = -2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(-2, result()["data"].cast()); -} - -TEST_F(ClassProperties, ProxyCFunctions_ReadOnly) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", &getDataC) - .endClass(); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result.data = -2"), std::exception); -#else - ASSERT_FALSE(runLua("result.data = -2")); -#endif - - ASSERT_EQ(501, result()["data"].cast()); -} - -TEST_F(ClassProperties, ProxyCFunctions_Derived) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addProperty("data", &getDataC, &setDataC) - .endClass() - .deriveClass("Derived") - .endClass(); - - Derived derived(12); - derived.Base::data = "abc"; - luabridge::setGlobal(L, &derived, "derived"); - - runLua("result = derived.data"); - ASSERT_TRUE(result().isString()); - ASSERT_EQ("abc", result()); - - runLua("derived.data = 5"); // Lua just casts integer to string - ASSERT_EQ("5", derived.Base::data); - ASSERT_EQ(12, derived.data); - - runLua("derived.data = '123'"); - ASSERT_EQ("123", derived.Base::data); - ASSERT_EQ(12, derived.data); -} - -TEST_F(ClassProperties, ProxyCFunctions_Overridden) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addProperty("data", &getDataC, &setDataC) - .endClass() - .deriveClass("Derived") - .addProperty("data", &getData, &setData) - .endClass(); - - Derived derived(50); - derived.Base::data = 1.23f; - luabridge::setGlobal(L, static_cast(&derived), "base"); - luabridge::setGlobal(L, &derived, "derived"); - - runLua("result = base.data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1.23f, result()); - - runLua("result = derived.data"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(50, result()); - - runLua("base.data = -3.14"); - ASSERT_EQ(-3.14f, derived.Base::data); - ASSERT_EQ(50, derived.data); - - runLua("derived.data = 7"); - ASSERT_EQ(-3.14f, derived.Base::data); - ASSERT_EQ(7, derived.data); -} - -TEST_F(ClassProperties, StdFunctions) -{ - using Int = Class; - - auto sharedGetterData = std::make_shared(); - std::weak_ptr getterData = sharedGetterData; // Check __gc meta-method - - auto sharedSetterData = std::make_shared(); - std::weak_ptr setterData = sharedGetterData; // Check __gc meta-method - - std::function getter = [sharedGetterData](const Int* object) { - return object->data; - }; - - std::function setter = [sharedSetterData](Int* object, int value) { - object->data = value; - }; - - int data2 = 1; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", std::move(getter), std::move(setter)) - .addProperty("data2", [&data2](const Int*) { return data2; }, [data2 = std::addressof(data2)](Int*, int v) { *data2 = v; }) - .endClass(); - - getter = nullptr; - sharedGetterData.reset(); - ASSERT_FALSE(getterData.expired()); - - setter = nullptr; - sharedSetterData.reset(); - ASSERT_FALSE(setterData.expired()); - - runLua("result = Int (501)"); - ASSERT_EQ(501, result()["data"].cast()); - - runLua("result.data = -2"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(-2, result()["data"].cast()); - - runLua("result.data2 = -2"); - ASSERT_TRUE(result()["data2"].isNumber()); - ASSERT_EQ(-2, result()["data2"].cast()); - - runLua("result = nil"); - closeLuaState(); // Force garbage collection - - ASSERT_TRUE(getterData.expired()); - ASSERT_TRUE(setterData.expired()); -} - -TEST_F(ClassProperties, StdFunctions_ReadOnly) -{ - using Int = Class; - - auto sharedGetterData = std::make_shared(); - std::weak_ptr getterData = sharedGetterData; // Check __gc meta-method - - std::function getter = [sharedGetterData](const Int* object) { - return object->data; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addProperty("data", std::move(getter)) - .endClass(); - - getter = nullptr; - sharedGetterData.reset(); - ASSERT_FALSE(getterData.expired()); - - runLua("result = Int (501)"); - ASSERT_TRUE(result()["data"].isNumber()); - ASSERT_EQ(501, result()["data"].cast()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result.data = -2"), std::exception); -#else - ASSERT_FALSE(runLua("result.data = -2")); -#endif - - ASSERT_EQ(501, result()["data"].cast()); - - runLua("result = nil"); - closeLuaState(); // Force garbage collection - - ASSERT_TRUE(getterData.expired()); -} - -struct ClassStaticFunctions : ClassTests -{ -}; - -TEST_F(ClassStaticFunctions, Functions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addStaticFunction("static", &Int::staticFunction) - .endClass(); - - runLua("result = Int.static (Int (35))"); - ASSERT_EQ(35, result().data); -} - -TEST_F(ClassStaticFunctions, Functions_Derived) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addConstructor() - .addStaticFunction("static", &Base::staticFunction) - .endClass() - .deriveClass("Derived") - .endClass(); - - runLua("result = Derived.static (Base ('abc'))"); - ASSERT_EQ("abc", result().data); -} - -TEST_F(ClassStaticFunctions, Functions_Overridden) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addConstructor() - .addStaticFunction("staticFunction", &Base::staticFunction) - .endClass() - .deriveClass("Derived") - .addConstructor() - .addStaticFunction("staticFunction", &Derived::staticFunction) - .endClass(); - - runLua("result = Base.staticFunction (Base ('abc'))"); - ASSERT_EQ("abc", result().data); - - runLua("result = Derived.staticFunction (Derived (123))"); - ASSERT_EQ(123, result().data); -} - -TEST_F(ClassStaticFunctions, StdFunctions) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addStaticFunction("static", std::function(&Int::staticFunction)) - .endClass(); - - runLua("result = Int.static (Int (35))"); - ASSERT_EQ(35, result().data); -} - -struct ClassStaticProperties : ClassTests -{ -}; - -TEST_F(ClassStaticProperties, FieldPointers) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", &Int::staticData, true) - .endClass(); - - Int::staticData = 10; - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - - runLua("Int.staticData = 20"); - ASSERT_EQ(20, Int::staticData); -} - -TEST_F(ClassStaticProperties, FieldPointers_Const) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticConstData", &Int::staticConstData) - .endClass(); - - runLua("result = Int.staticConstData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(0, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("Int.staticConstData = 20"), std::exception); -#else - ASSERT_FALSE(runLua("Int.staticConstData = 20")); -#endif - - ASSERT_EQ(0, Int::staticConstData); -} - -TEST_F(ClassStaticProperties, FieldPointers_ReadOnly) -{ - using Int = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", &Int::staticData, false) - .endClass(); - - Int::staticData = 10; - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("Int.staticData = 20"), std::exception); -#else - ASSERT_FALSE(runLua("Int.staticData = 20")); -#endif - - ASSERT_EQ(10, Int::staticData); -} - -TEST_F(ClassStaticProperties, FieldPointers_GetterOnly) -{ - using Int = Class; - - int value = 10; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", [&value] { return value; }) - .endClass(); - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - ASSERT_EQ(10, value); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("Int.staticData = 20"), std::exception); -#else - ASSERT_FALSE(runLua("Int.staticData = 20")); -#endif - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - ASSERT_EQ(10, value); -} - -TEST_F(ClassStaticProperties, FieldPointers_GetterSetter) -{ - using Int = Class; - - int value = 10; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", [&value] { return value; }, [&value](int x) { value = x; }) - .endClass(); - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - ASSERT_EQ(10, value); - - runLua("Int.staticData = 20; result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(20, result()); - ASSERT_EQ(20, value); -} - -TEST_F(ClassStaticProperties, FieldPointers_StaticMembers) -{ - using Int = Class; - - Int::staticData = 10; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", &Int::getStaticData, &Int::setStaticData) - .endClass(); - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - - runLua("Int.staticData = 20; result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(20, result()); -} - -TEST_F(ClassStaticProperties, FieldPointers_StaticMembersReadOnly) -{ - using Int = Class; - - Int::staticData = 10; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", &Int::getStaticData) - .endClass(); - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("Int.staticData = 20"), std::exception); -#else - ASSERT_FALSE(runLua("Int.staticData = 20")); -#endif -} - -TEST_F(ClassStaticProperties, FieldPointers_StaticMembersNoexcept) -{ - using Int = Class; - - Int::staticData = 10; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", &Int::getStaticDataNoexcept, &Int::setStaticDataNoexcept) - .endClass(); - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - - runLua("Int.staticData = 20; result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(20, result()); -} - -TEST_F(ClassStaticProperties, FieldPointers_StaticMembersReadonlyNoexcept) -{ - using Int = Class; - - Int::staticData = 10; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addStaticProperty("staticData", &Int::getStaticDataNoexcept) - .endClass(); - - runLua("result = Int.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(10, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("Int.staticData = 20"), std::exception); -#else - ASSERT_FALSE(runLua("Int.staticData = 20")); -#endif -} - -TEST_F(ClassStaticProperties, FieldPointers_Derived) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addStaticProperty("staticData", &Base::staticData, true) - .endClass() - .deriveClass("Derived") - .endClass(); - - Base::staticData = 1.23f; - Derived::staticData = 50; - - runLua("result = Derived.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1.23f, result()); - - runLua("Derived.staticData = -3.14"); - ASSERT_EQ(-3.14f, Base::staticData); - ASSERT_EQ(50, Derived::staticData); -} - -TEST_F(ClassStaticProperties, FieldPointers_Overridden) -{ - using Base = Class; - using Derived = Class; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addStaticProperty("staticData", &Base::staticData, true) - .endClass() - .deriveClass("Derived") - .addStaticProperty("staticData", &Derived::staticData, true) - .endClass(); - - Base::staticData = 1.23f; - Derived::staticData = 50; - - runLua("result = Base.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1.23f, result()); - - runLua("result = Derived.staticData"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(50, result()); - - runLua("Base.staticData = -3.14"); - ASSERT_EQ(-3.14f, Base::staticData); - ASSERT_EQ(50, Derived::staticData); - - runLua("Derived.staticData = 7"); - ASSERT_EQ(-3.14f, Base::staticData); - ASSERT_EQ(7, Derived::staticData); -} - -struct ClassMetaMethods : ClassTests -{ -}; - -TEST_F(ClassMetaMethods, __call) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__call", &Int::operator()) - .endClass(); - - runLua("result = Int (1) (-1)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(-1, result().data); - - runLua("result = Int (2) (5)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(5, result().data); -} - -TEST_F(ClassMetaMethods, __tostring) -{ - typedef Class Int; - typedef Class StringClass; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__tostring", &Int::toString) - .endClass() - .beginClass("String") - .addConstructor() - .addFunction("__tostring", &StringClass::toString) - .endClass(); - - runLua("result = tostring (Int (-123))"); - ASSERT_EQ("-123", result()); - -#if LUA_VERSION_NUM >= 502 - // Lua 5.1 string.format doesn't use __tostring - runLua("result = string.format ('%s%s', String ('abc'), Int (-123))"); - ASSERT_EQ("abc-123", result()); -#endif -} - -TEST_F(ClassMetaMethods, __eq) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__eq", &Int::operator==) - .endClass(); - - runLua("result = Int (1) == Int (1)"); - ASSERT_EQ(true, result()); - - runLua("result = Int (1) ~= Int (1)"); - ASSERT_EQ(false, result()); - - runLua("result = Int (1) == Int (2)"); - ASSERT_EQ(false, result()); - - runLua("result = Int (1) ~= Int (2)"); - ASSERT_EQ(true, result()); -} - -TEST_F(ClassMetaMethods, __lt) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__lt", &Int::operator<) - .endClass(); - - runLua("result = Int (1) < Int (1)"); - ASSERT_EQ(false, result()); - - runLua("result = Int (1) < Int (2)"); - ASSERT_EQ(true, result()); - - runLua("result = Int (2) < Int (1)"); - ASSERT_EQ(false, result()); -} - -TEST_F(ClassMetaMethods, __le) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__le", &Int::operator<=) - .endClass(); - - runLua("result = Int (1) <= Int (1)"); - ASSERT_EQ(true, result()); - - runLua("result = Int (1) <= Int (2)"); - ASSERT_EQ(true, result()); - - runLua("result = Int (2) <= Int (1)"); - ASSERT_EQ(false, result()); -} - -TEST_F(ClassMetaMethods, __add) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__add", &Int::operator+) - .endClass(); - - runLua("result = Int (1) + Int (2)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(3, result().data); -} - -TEST_F(ClassMetaMethods, __sub) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__sub", &Int::operator-) - .endClass(); - - runLua("result = Int (1) - Int (2)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(-1, result().data); -} - -TEST_F(ClassMetaMethods, __mul) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__mul", &Int::operator*) - .endClass(); - - runLua("result = Int (-2) * Int (-5)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(10, result().data); -} - -TEST_F(ClassMetaMethods, __div) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__div", &Int::operator/) - .endClass(); - - runLua("result = Int (10) / Int (2)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(5, result().data); -} - -TEST_F(ClassMetaMethods, __mod) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__mod", &Int::operator%) - .endClass(); - - runLua("result = Int (7) % Int (2)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(1, result().data); -} - -TEST_F(ClassMetaMethods, __pow) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__pow", &Int::operator-) - .endClass(); - - runLua("result = Int (5) ^ Int (2)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(3, result().data); -} - -TEST_F(ClassMetaMethods, __unm) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__unm", &Int::negate) - .endClass(); - - runLua("result = -Int (-3)"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(3, result().data); -} - -TEST_F(ClassMetaMethods, __concat) -{ - typedef Class String; - - luabridge::getGlobalNamespace(L) - .beginClass("String") - .addConstructor() - .addFunction("__concat", &String::operator+) - .endClass(); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = String ('a') + String ('b')"), std::exception); -#else - ASSERT_FALSE(runLua("result = String ('a') + String ('b')")); -#endif - - runLua("result = String ('ab') .. String ('cd')"); - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ("abcd", result().data); -} - -TEST_F(ClassMetaMethods, __len) -{ - typedef Class Int; - - luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addConstructor() - .addFunction("__len", &Int::len) - .endClass(); - - runLua("result = #Int (1)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1, result()); - - runLua("result = #Int (5)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(5, result()); -} - -namespace { -struct Table -{ - int index(const std::string& key) - { -#if LUABRIDGE_HAS_EXCEPTIONS - return map.at(key); -#else - auto it = map.find(key); - return it != map.end() ? it->second : -1; -#endif - } - - void newIndex(const std::string& key, int value) - { - map.emplace(key, value); - } - - std::map map; -}; -} // namespace - -TEST_F(ClassMetaMethods, __index) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Table") - .addFunction("__index", &Table::index) - .endClass(); - - Table t{{{"a", 1}, {"b", 2}}}; - - luabridge::setGlobal(L, &t, "t"); - - runLua("result = t.a"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(1, result()); - - runLua("result = t.b"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(2, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("result = t.c"), std::exception); // at ("c") throws -#else - ASSERT_TRUE(runLua("result = t.c")); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(-1, result()); -#endif -} - -TEST_F(ClassMetaMethods, __newindex) -{ - luabridge::getGlobalNamespace(L) - .beginClass
("Table") - .addFunction("__newindex", &Table::newIndex) - .endClass(); - - Table t; - - luabridge::setGlobal(L, &t, "t"); - - runLua("t.a = 1\n" - "t ['b'] = 2"); - - ASSERT_EQ((std::map{{"a", 1}, {"b", 2}}), t.map); -} - -#if LUABRIDGE_HAS_EXCEPTIONS -TEST_F(ClassMetaMethods, __gcForbidden) -{ - using Int = Class; - - ASSERT_THROW(luabridge::getGlobalNamespace(L) - .beginClass("Int") - .addFunction("__gc", &Int::method) - .endClass(), - std::exception); -} -#endif - -TEST_F(ClassMetaMethods, SimulateArray) -{ - using ContainerType = std::vector; - - ContainerType data(1); - data[0] = "abcdefg"; - - luabridge::getGlobalNamespace(L) - .beginTable("xyz") - .addFunction("a", +[] { return "abcdefg"; }) - .addMetaFunction("__index", [&data](luabridge::LuaRef, int index, lua_State* L) - { - if (index < 0 || index >= static_cast(data.size())) - luaL_error(L, "Invalid index access in table %d", index); - - return data[index]; - }) - .addMetaFunction("__newindex", [&data](luabridge::LuaRef, int index, luabridge::LuaRef ref, lua_State* L) - { - if (index < 0) - luaL_error(L, "Invalid index access in table %d", index); - - if (! ref.isString()) - luaL_error(L, "Invalid value provided to set table at index %d", index); - - if (index >= static_cast(data.size())) - data.resize(index + 1); - - data[index] = ref.unsafe_cast(); - }) - .endTable(); - - runLua("xyz[0] = '123'; result = xyz[0]"); - ASSERT_EQ("123", result()); -} - -TEST_F(ClassTests, EnclosedClassProperties) -{ - typedef Class Inner; - typedef Class Outer; - - luabridge::getGlobalNamespace(L) - .beginClass("Inner") - .addProperty("data", &Inner::data) - .endClass() - .beginClass("Outer") - .addProperty("data", &Outer::data) - .endClass(); - - Outer outer(Inner(0)); - luabridge::setGlobal(L, &outer, "outer"); - - outer.data.data = 1; - runLua("outer.data.data = 10"); - ASSERT_EQ(10, outer.data.data); - - runLua("result = outer.data.data"); - ASSERT_EQ(10, result()); -} - -namespace { -struct InnerClass -{ - ~InnerClass() { ++destructorCallCount; } - - static unsigned destructorCallCount; -}; - -unsigned InnerClass::destructorCallCount; - -struct OuterClass -{ - OuterClass() - { -#if LUABRIDGE_HAS_EXCEPTIONS - throw std::runtime_error("Exception"); -#endif - } - - ~OuterClass() { ++destructorCallCount; } - - static unsigned destructorCallCount; - InnerClass inner; -}; - -unsigned OuterClass::destructorCallCount; -} // namespace - -TEST_F(ClassTests, ConstructorWithReferences) -{ - struct InnerClass - { - InnerClass() = default; - }; - - struct OuterClass - { - OuterClass(const InnerClass& x) : y(x) {} - - private: - [[maybe_unused]] InnerClass y; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("InnerClass") - .addConstructor() - .endClass() - .beginClass("OuterClass") - .addConstructor() - .endClass(); - - runLua("x = InnerClass () result = OuterClass (x)"); -} - -TEST_F(ClassTests, DestructorIsNotCalledIfConstructorThrows) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - luabridge::getGlobalNamespace(L) - .beginClass("OuterClass") - .addConstructor() - .endClass(); - - InnerClass::destructorCallCount = 0; - OuterClass::destructorCallCount = 0; - ASSERT_THROW(runLua("result = OuterClass ()"), std::exception); - ASSERT_EQ(1, InnerClass::destructorCallCount); - - closeLuaState(); // Force garbage collection - - ASSERT_EQ(1, InnerClass::destructorCallCount); - ASSERT_EQ(0, OuterClass::destructorCallCount); -#endif -} - -TEST_F(ClassTests, DestructorIsCalledOnce) -{ - luabridge::getGlobalNamespace(L) - .beginClass("InnerClass") - .addConstructor() - .endClass(); - - InnerClass::destructorCallCount = 0; - runLua("result = InnerClass ()"); - - closeLuaState(); // Force garbage collection - - ASSERT_EQ(1, InnerClass::destructorCallCount); -} - -TEST_F(ClassTests, ConstructorTakesMoreThanEightArgs) -{ - struct WideClass - { - WideClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) - { - a1_ = a1; - a2_ = a2; - a3_ = a3; - a4_ = a4; - a5_ = a5; - a6_ = a6; - a7_ = a7; - a8_ = a8; - a9_ = a9; - a10_ = a10; - } - - int a1_, a2_, a3_, a4_, a5_, a6_, a7_, a8_, a9_, a10_; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("WideClass") - .addConstructor() - .endClass(); - - runLua("result = WideClass (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)"); - - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(1, result().a1_); - ASSERT_EQ(2, result().a2_); - ASSERT_EQ(3, result().a3_); - ASSERT_EQ(4, result().a4_); - ASSERT_EQ(5, result().a5_); - ASSERT_EQ(6, result().a6_); - ASSERT_EQ(7, result().a7_); - ASSERT_EQ(8, result().a8_); - ASSERT_EQ(9, result().a9_); - ASSERT_EQ(10, result().a10_); -} - -TEST_F(ClassTests, MethodTakesMoreThanEightArgs) -{ - struct WideClass - { - WideClass() = default; - - void testLotsOfArgs(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) - { - a1_ = a1; - a2_ = a2; - a3_ = a3; - a4_ = a4; - a5_ = a5; - a6_ = a6; - a7_ = a7; - a8_ = a8; - a9_ = a9; - a10_ = a10; - } - - int a1_, a2_, a3_, a4_, a5_, a6_, a7_, a8_, a9_, a10_; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("WideClass") - .addConstructor() - .addFunction("testLotsOfArgs", &WideClass::testLotsOfArgs) - .endClass(); - - runLua("result = WideClass () result:testLotsOfArgs (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)"); - - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(1, result().a1_); - ASSERT_EQ(2, result().a2_); - ASSERT_EQ(3, result().a3_); - ASSERT_EQ(4, result().a4_); - ASSERT_EQ(5, result().a5_); - ASSERT_EQ(6, result().a6_); - ASSERT_EQ(7, result().a7_); - ASSERT_EQ(8, result().a8_); - ASSERT_EQ(9, result().a9_); - ASSERT_EQ(10, result().a10_); -} - -TEST_F(ClassTests, ConstructorFactory) -{ - struct FactoryConstructibleClass - { - FactoryConstructibleClass() = default; - FactoryConstructibleClass(int x) : x_(x) {} - - int x_ = 33; - }; - - { - luabridge::getGlobalNamespace(L) - .beginClass("FactoryConstructibleClass") - .addConstructor([](void* ptr) { return new(ptr) FactoryConstructibleClass(); }) - .addProperty("x", &FactoryConstructibleClass::x_) - .endClass(); - - runLua("obj = FactoryConstructibleClass (); result = obj.x"); - - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(33, result()); - } - - { - luabridge::getGlobalNamespace(L) - .beginClass("FactoryConstructibleClass2") - .addConstructor([](void* ptr, int x) { return new(ptr) FactoryConstructibleClass(x); }) - .addProperty("x", &FactoryConstructibleClass::x_) - .endClass(); - - runLua("obj = FactoryConstructibleClass2 (42); result = obj.x"); - - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); - } - - { - luabridge::getGlobalNamespace(L) - .beginClass("FactoryConstructibleClass3") - .addConstructor([](void* ptr, lua_State* L) { return new(ptr) FactoryConstructibleClass(static_cast(luaL_checkinteger(L, 2))); }) - .addProperty("x", &FactoryConstructibleClass::x_) - .endClass(); - - runLua("obj = FactoryConstructibleClass3 (42); result = obj.x"); - - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); - } -} - -namespace { -class BaseExampleClass -{ -public: - BaseExampleClass() = default; - virtual ~BaseExampleClass() = default; - - virtual void virtualFunction(int arg) = 0; - virtual int virtualCFunction(lua_State*) = 0; - virtual void virtualFunctionConst(int arg) const = 0; - virtual int virtualCFunctionConst(lua_State*) const = 0; - - void baseFunction(int arg) { baseFunction_ = arg; } - int baseCFunction(lua_State*) { return baseCFunction_ = 1; } - void baseFunctionConst(int arg) const { baseFunctionConst_ = arg; } - int baseCFunctionConst(lua_State*) const { return baseCFunctionConst_ = 1; } - - int virtualFunction_ = 0; - int virtualCFunction_ = 0; - mutable int virtualFunctionConst_ = 0; - mutable int virtualCFunctionConst_ = 0; - int baseFunction_ = 0; - int baseCFunction_ = 0; - mutable int baseFunctionConst_ = 0; - mutable int baseCFunctionConst_ = 0; -}; - -class DerivedExampleClass : public BaseExampleClass -{ -public: - DerivedExampleClass() = default; - - void virtualFunction(int arg) override - { - virtualFunction_ = arg; - } - - int virtualCFunction(lua_State*) override - { - return virtualCFunction_ = 1; - } - - void virtualFunctionConst(int arg) const override - { - virtualFunctionConst_ = arg; - } - - int virtualCFunctionConst(lua_State*) const override - { - return virtualCFunctionConst_ = 1; - } -}; -} // namespace - -TEST_F(ClassTests, NonVirtualMethodInBaseClassCannotBeExposed) -{ - luabridge::getGlobalNamespace(L) - .beginClass("DerivedExampleClass") - .addConstructor() - .addFunction("baseFunction", &BaseExampleClass::baseFunction) - .addFunction("baseCFunction", &BaseExampleClass::baseCFunction) - .addFunction("baseFunctionConst", &BaseExampleClass::baseFunctionConst) - .addFunction("baseCFunctionConst", &BaseExampleClass::baseCFunctionConst) - .addFunction("virtualFunction", &DerivedExampleClass::virtualFunction) - .addFunction("virtualCFunction", &DerivedExampleClass::virtualCFunction) - .addFunction("virtualFunctionConst", &DerivedExampleClass::virtualFunctionConst) - .addFunction("virtualCFunctionConst", &DerivedExampleClass::virtualCFunctionConst) - .endClass(); - - runLua(R"( - result = DerivedExampleClass () - result:baseFunction(1) - result:baseCFunction() - result:baseFunctionConst(1) - result:baseCFunctionConst() - result:virtualFunction(1) - result:virtualCFunction() - result:virtualFunctionConst(1) - result:virtualCFunctionConst() - )"); - - ASSERT_TRUE(result().isUserdata()); - ASSERT_EQ(1, result().baseFunction_); - ASSERT_EQ(1, result().baseCFunction_); - ASSERT_EQ(1, result().baseFunctionConst_); - ASSERT_EQ(1, result().baseCFunctionConst_); - ASSERT_EQ(1, result().virtualFunction_); - ASSERT_EQ(1, result().virtualCFunction_); - ASSERT_EQ(1, result().virtualFunctionConst_); - ASSERT_EQ(1, result().virtualCFunctionConst_); -} - -TEST_F(ClassTests, NilCanBeConvertedToNullptrButNotToReference) -{ - struct X {}; - - bool result = false, resultConst = false, called = false; - - luabridge::getGlobalNamespace(L) - .addFunction("TakeNullptr", [&result](X* iAmNullptr) { result = (iAmNullptr == nullptr); }) - .addFunction("TakeConstNullptr", [&resultConst](const X* iAmNullptr) { resultConst = (iAmNullptr == nullptr); }) - .addFunction("TakeReference", [&called](const X& iAmNullptr) { called = true; }) - .beginClass("X") - .endClass(); - - runLua("TakeNullptr(nil)"); - EXPECT_TRUE(result); - - runLua("TakeConstNullptr(nil)"); - EXPECT_TRUE(resultConst); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(runLua("TakeReference(nil)"), std::exception); -#else - EXPECT_FALSE(runLua("TakeReference(nil)")); -#endif - EXPECT_FALSE(called); -} - -namespace { -struct OverridableX -{ - luabridge::LuaRef indexMetaMethod(const luabridge::LuaRef& key, lua_State* L); - luabridge::LuaRef newIndexMetaMethod(const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* L); - - std::unordered_map data; -}; - -luabridge::LuaRef indexMetaMethodFunction(OverridableX& x, const luabridge::LuaRef& key, lua_State* L) -{ - if (key.tostring() == "xyz") - { - if (!luabridge::push(L, "123")) - lua_pushnil(L); - } - else - { - auto it = x.data.find(key); - if (it != x.data.end()) - return it->second; - - lua_pushnil(L); - } - - return luabridge::LuaRef::fromStack(L); -} - -luabridge::LuaRef OverridableX::indexMetaMethod(const luabridge::LuaRef& key, lua_State* L) -{ - return indexMetaMethodFunction(*this, key, L); -} - -luabridge::LuaRef newIndexMetaMethodFunction(OverridableX& x, const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* L) -{ - x.data.emplace(std::make_pair(key, value)); - return value; -} - -luabridge::LuaRef OverridableX::newIndexMetaMethod(const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* L) -{ - return newIndexMetaMethodFunction(*this, key, value, L); -} -} // namespace - -TEST_F(ClassTests, IndexFallbackMetaMethodMemberFptr) -{ - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addIndexMetaMethod(&OverridableX::indexMetaMethod) - .endClass(); - - OverridableX x; - luabridge::setGlobal(L, &x, "x"); - - runLua("result = x.xyz"); - ASSERT_EQ("123", result()); -} - -TEST_F(ClassTests, IndexFallbackMetaMethodFunctionPtr) -{ - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addIndexMetaMethod(&indexMetaMethodFunction) - .endClass(); - - OverridableX x; - luabridge::setGlobal(L, &x, "x"); - - runLua("result = x.xyz"); - ASSERT_EQ("123", result()); -} - -TEST_F(ClassTests, IndexFallbackMetaMethodFreeFunctor) -{ - std::string capture = "123"; - - auto indexMetaMethod = [&capture](OverridableX&, luabridge::LuaRef key, lua_State* L) -> luabridge::LuaRef - { - if (key.tostring() == "xyz") - { - if (!luabridge::push(L, capture + "123")) - lua_pushnil(L); - } - else - { - if (!luabridge::push(L, 456)) - lua_pushnil(L); - } - - return luabridge::LuaRef::fromStack(L); - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addIndexMetaMethod(indexMetaMethod) - .endClass(); - - OverridableX x; - luabridge::setGlobal(L, &x, "x"); - - runLua("result = x.xyz"); - ASSERT_EQ("123123", result()); -} - -TEST_F(ClassTests, NewIndexFallbackMetaMethodMemberFptr) -{ - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addIndexMetaMethod(&OverridableX::indexMetaMethod) - .addNewIndexMetaMethod(&OverridableX::newIndexMetaMethod) - .endClass(); - - OverridableX x; - luabridge::setGlobal(L, &x, "x"); - - runLua("x.qwertyuiop = 123; result = x.qwertyuiop"); - ASSERT_EQ(123, result()); -} - -TEST_F(ClassTests, NewIndexFallbackMetaMethodFunctionPtr) -{ - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addIndexMetaMethod(&indexMetaMethodFunction) - .addNewIndexMetaMethod(&newIndexMetaMethodFunction) - .endClass(); - - OverridableX x; - luabridge::setGlobal(L, &x, "x"); - - runLua("x.qwertyuiop = 123; result = x.qwertyuiop"); - ASSERT_EQ(123, result()); -} - -TEST_F(ClassTests, NewIndexFallbackMetaMethodFreeFunctor) -{ - int capture = 123; - - auto newIndexMetaMethod = [&capture](OverridableX& x, const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State* L) -> luabridge::LuaRef - { - if (!luabridge::push(L, capture + value.unsafe_cast())) - lua_pushnil(L); - - auto v = luabridge::LuaRef::fromStack(L); - x.data.emplace(std::make_pair(key, v)); - return v; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addIndexMetaMethod(&indexMetaMethodFunction) - .addNewIndexMetaMethod(newIndexMetaMethod) - .endClass(); - - OverridableX x; - luabridge::setGlobal(L, &x, "x"); - - runLua("x.qwertyuiop = 123; result = x.qwertyuiop"); - ASSERT_EQ(246, result()); -} diff --git a/LuaBridge3/Tests/Source/DynamicLibraryTests.cpp b/LuaBridge3/Tests/Source/DynamicLibraryTests.cpp deleted file mode 100644 index f2f089d..0000000 --- a/LuaBridge3/Tests/Source/DynamicLibraryTests.cpp +++ /dev/null @@ -1,215 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "TestBase.h" -#include "SharedCode.h" - -#include -#include - -#if _WIN32 -#include -#elif __APPLE__ -#include -#include -#else -#include -#endif - -namespace { -template -struct ScopedGuard -{ - ScopedGuard(F func) - : func_(std::move(func)) - { - } - - ~ScopedGuard() - { - func_(); - } - -private: - F func_; -}; - -template -ScopedGuard(F) -> ScopedGuard; - -std::optional getExecutablePath() -{ -#if _WIN32 - TCHAR path[MAX_PATH]; - auto pathLength = GetModuleFileName(NULL, path, MAX_PATH); - if (pathLength > 0) - return std::filesystem::canonical(std::string_view(path, static_cast(pathLength))); - -#elif __APPLE__ - char path[1024]; - std::uint32_t pathLength = sizeof(path); - if (_NSGetExecutablePath(path, &pathLength) == 0) - return std::filesystem::canonical(std::string_view(path, static_cast(pathLength))); - -#else - char path[1024]; - auto pathLength = readlink("/proc/self/exe", path, sizeof(path)); - if (pathLength > 0) - return std::filesystem::canonical(std::string_view(path, static_cast(pathLength))); - -#endif - - return std::nullopt; -} - -auto openSharedLibrary(const char* library_path) -{ -#if _WIN32 - return LoadLibrary(library_path); -#else - return dlopen(library_path, RTLD_NOW | RTLD_LAZY); -#endif -} - -template -void closeSharedLibrary(T handle) -{ -#if _WIN32 - FreeLibrary(handle); -#else - dlclose(handle); -#endif -} - -template -auto lookupSharedLibrarySymbol(T handle, const char* procedure_name) -{ -#if _WIN32 - return reinterpret_cast(GetProcAddress(handle, procedure_name)); -#else - return reinterpret_cast(dlsym(handle, procedure_name)); -#endif -} - -int callSharedClassMethod(xyz::ISharedClass* s) -{ - return s->publicMethod("1337"); -} -} // namespace - -struct DynamicLibraryTests : TestBase -{ - auto loadSharedLibrary() -> decltype(openSharedLibrary(std::declval())) - { - auto executablePath = getExecutablePath(); - if (! executablePath.has_value()) - return nullptr; - - auto libraryPath = executablePath->remove_filename() / LUABRIDGEDEMO_SHARED_LIBRARY; - if (! std::filesystem::exists(libraryPath)) - return nullptr; - - return openSharedLibrary(libraryPath.string().c_str()); - } -}; - -TEST_F(DynamicLibraryTests, ExampleUsageFromLibrary) -{ - auto dll = loadSharedLibrary(); - ASSERT_NE(nullptr, dll); - - auto unloadDll = ScopedGuard([dll] { closeSharedLibrary(dll); }); - - auto allocator = lookupSharedLibrarySymbol(dll, "allocator"); - ASSERT_NE(nullptr, allocator); - - auto deallocator = lookupSharedLibrarySymbol(dll, "deallocator"); - ASSERT_NE(nullptr, deallocator); - - luabridge::getGlobalNamespace(L) - .beginClass("SharedClass") - .addFactory(allocator, deallocator) - .addFunction("publicMethod", &xyz::ISharedClass::publicMethod) - .endClass(); - - luabridge::getGlobalNamespace(L) - .addFunction("callSharedClassMethod", &callSharedClassMethod); - - xyz::ISharedClass* shared = allocator(); - auto unloadSharedClass = ScopedGuard([shared, deallocator] { deallocator(shared); }); - luabridge::setGlobal(L, shared, "shared"); - - runLua("result = shared:publicMethod('42')"); - EXPECT_EQ(84, result()); - - runLua("result = callSharedClassMethod(shared)"); - EXPECT_EQ(1379, result()); - - runLua("a = SharedClass(); result = callSharedClassMethod(a)"); - EXPECT_EQ(1379, result()); - - runLua("b = SharedClass(); result = b"); - auto ptr = result(); - ASSERT_NE(nullptr, ptr); - EXPECT_EQ(1379, callSharedClassMethod(ptr)); - - closeLuaState(); // Force garbage collection before we unload the deleter -} - -#if !LUABRIDGE_ON_LUAU -namespace { -void* allocFunction(void*, void* ptr, std::size_t osize, std::size_t nsize) -{ - static std::set allocs; - - void* nptr = nullptr; - - if (nsize > 0) - { - nptr = new uint8_t[nsize]; - - if (ptr != nullptr) - std::memcpy(nptr, ptr, nsize < osize ? nsize : osize); - - allocs.emplace(nptr); - } - - auto node = allocs.extract(ptr); - if (! node.empty() && ptr != nullptr) - delete[] static_cast(ptr); - - return nptr; -} -} // namespace - -TEST_F(DynamicLibraryTests, ExampleRegistrationFromLibrary) -{ - auto dll = loadSharedLibrary(); - ASSERT_NE(nullptr, dll); - - auto unloadDll = ScopedGuard([dll] { closeSharedLibrary(dll); }); - - auto registerAnotherClass = lookupSharedLibrarySymbol(dll, "registerAnotherClass"); - ASSERT_NE(nullptr, registerAnotherClass); - - closeLuaState(); - L = createNewLuaState(&allocFunction); - - registerAnotherClass(L); - - runLua("a = dll.AnotherClass(); result = a.value"); - EXPECT_EQ(30, result()); - - runLua("b = dll.AnotherClass(); b:publicMethod(12); result = b.value"); - EXPECT_EQ(12, result()); - - runLua("c = dll.AnotherClass(); result = c:publicMethod(12)"); - EXPECT_EQ(12, result()); - - runLua("d = dll.AnotherClass(); result = d:publicConstMethod(12)"); - EXPECT_EQ(42, result()); - - closeLuaState(); // Force garbage collection -} -#endif diff --git a/LuaBridge3/Tests/Source/ExceptionTests.cpp b/LuaBridge3/Tests/Source/ExceptionTests.cpp deleted file mode 100644 index a573ba5..0000000 --- a/LuaBridge3/Tests/Source/ExceptionTests.cpp +++ /dev/null @@ -1,131 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include - -#if LUABRIDGE_HAS_EXCEPTIONS -namespace { -static void throwError() -{ - throw std::runtime_error("runtime error"); -} - -static int standaloneError() -{ - throwError(); - return 0; -} - -class ErrorClass -{ -public: - int classError() const - { - throwError(); - return 0; - } -}; -} // namespace - -struct ExceptionTests : TestBase -{ -}; - -TEST_F(ExceptionTests, PassFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("test") - .addFunction("standaloneError", standaloneError) - .beginClass("ErrorClass") - .addConstructor() - .addStaticFunction("staticFunctionError", &standaloneError) - .addStaticProperty("staticPropertyError", &standaloneError) - .addFunction("memberFunctionError", &ErrorClass::classError) - .addProperty("memberPropertyError", &ErrorClass::classError) - .addProperty("memberPropertyError2", [&](ErrorClass*) { throwError(); return 1; }) - .endClass() - .endNamespace(); - - // Standalone function error will report line - { - auto result = runLuaCaptureError(R"( - - - test.standaloneError() - )"); - - EXPECT_FALSE(std::get<0>(result)); - EXPECT_TRUE(std::get<1>(result).find("...") != std::string::npos || std::get<1>(result).find("code") != std::string::npos); - EXPECT_TRUE(std::get<1>(result).find("4") != std::string::npos); - } - - // Class static function error will report line - { - auto result = runLuaCaptureError(R"( - - - local x = test.ErrorClass.staticFunctionError() - )"); - - EXPECT_FALSE(std::get<0>(result)); - EXPECT_TRUE(std::get<1>(result).find("...") != std::string::npos || std::get<1>(result).find("code") != std::string::npos); - EXPECT_TRUE(std::get<1>(result).find("4") != std::string::npos); - } - - // Class static property error will report line - { - auto result = runLuaCaptureError(R"( - - - local x = test.ErrorClass.staticPropertyError - )"); - - EXPECT_FALSE(std::get<0>(result)); - EXPECT_TRUE(std::get<1>(result).find("...") != std::string::npos || std::get<1>(result).find("code") != std::string::npos); - EXPECT_TRUE(std::get<1>(result).find("4") != std::string::npos); - } - - // Class member function error will report line - { - auto result = runLuaCaptureError(R"( - - local errorClass = test.ErrorClass() - local x = errorClass:memberFunctionError() - )"); - - EXPECT_FALSE(std::get<0>(result)); - EXPECT_TRUE(std::get<1>(result).find("...") != std::string::npos || std::get<1>(result).find("code") != std::string::npos); - EXPECT_TRUE(std::get<1>(result).find("4") != std::string::npos); - } - - // Class property error will report line - { - auto result = runLuaCaptureError(R"( - - local errorClass = test.ErrorClass() - local y = errorClass.memberPropertyError - )"); - - EXPECT_FALSE(std::get<0>(result)); - EXPECT_TRUE(std::get<1>(result).find("...") != std::string::npos || std::get<1>(result).find("code") != std::string::npos); - EXPECT_TRUE(std::get<1>(result).find("4") != std::string::npos); - } - - // Class property error by lambda will report line - { - auto result = runLuaCaptureError(R"( - - local errorClass = test.ErrorClass() - local y = errorClass.memberPropertyError2 - )"); - - EXPECT_FALSE(std::get<0>(result)); - EXPECT_TRUE(std::get<1>(result).find("...") != std::string::npos || std::get<1>(result).find("code") != std::string::npos); - EXPECT_TRUE(std::get<1>(result).find("4") != std::string::npos); - } -} - -#endif diff --git a/LuaBridge3/Tests/Source/IssueTests.cpp b/LuaBridge3/Tests/Source/IssueTests.cpp deleted file mode 100644 index 9874db1..0000000 --- a/LuaBridge3/Tests/Source/IssueTests.cpp +++ /dev/null @@ -1,268 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -struct IssueTests : TestBase -{ -}; - -namespace { -struct AbstractClass -{ - virtual int sum(int a, int b) = 0; -}; - -struct ConcreteClass : AbstractClass -{ - int sum(int a, int b) override { return a + b; } - - static AbstractClass& get() - { - static ConcreteClass instance; - return instance; - } -}; -} // namespace - -TEST_F(IssueTests, Issue87) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Class") - .addFunction("sum", &AbstractClass::sum) - .endClass() - .addFunction("getAbstractClass", &ConcreteClass::get); - - runLua("result = getAbstractClass ():sum (1, 2)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(3, result()); -} - -TEST_F(IssueTests, Issue121) -{ - runLua(R"( - first = { - second = { - actual = "data" - } - } - )"); - auto first = luabridge::getGlobal(L, "first"); - ASSERT_TRUE(first.isTable()); - ASSERT_EQ(0, first.length()); - ASSERT_TRUE(first["second"].isTable()); - ASSERT_EQ(0, first["second"].length()); -} - -namespace { -void pushArgs(lua_State*) -{ -} - -template -void pushArgs(lua_State* L, Arg arg, Args... args) -{ - [[maybe_unused]] auto result = luabridge::Stack::push(L, arg); - - pushArgs(L, args...); -} - -template -std::vector callFunction(const luabridge::LuaRef& function, Args... args) -{ - assert(function.isFunction()); - - lua_State* L = function.state(); - int originalTop = lua_gettop(L); - function.push(L); - pushArgs(L, args...); - - luabridge::pcall(L, sizeof...(args), LUA_MULTRET); - - std::vector results; - int top = lua_gettop(L); - results.reserve(top - originalTop); - for (int i = originalTop + 1; i <= top; ++i) - { - results.push_back(luabridge::LuaRef::fromStack(L, i)); - } - return results; -} -} // namespace - -TEST_F(IssueTests, Issue160) -{ - runLua("function isConnected (arg1, arg2) " - " return 1, 'srvname', 'ip:10.0.0.1', arg1, arg2 " - "end"); - - luabridge::LuaRef f_isConnected = luabridge::getGlobal(L, "isConnected"); - - auto v = callFunction(f_isConnected, 2, "abc"); - ASSERT_EQ(5u, v.size()); - ASSERT_EQ(1, v[0].unsafe_cast()); - ASSERT_EQ("srvname", v[1].unsafe_cast()); - ASSERT_EQ("ip:10.0.0.1", v[2].unsafe_cast()); - ASSERT_EQ(2, v[3].unsafe_cast()); - ASSERT_EQ("abc", v[4].unsafe_cast()); -} - -namespace { -struct Vector -{ - float getX() const { return x; } - - float x = 0; -}; - -struct WideVector : Vector -{ - WideVector(float, float, float, float w) { x = w; } -}; -} // namespace - -TEST_F(IssueTests, Issue178) -{ - luabridge::getGlobalNamespace(L) - .beginClass("WideVector") - .addConstructor() - .addProperty("x", &Vector::x, true) - .endClass(); - - runLua("result = WideVector (0, 1, 2, 3).x"); - - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(3.f, result()); -} - -namespace { -enum class MyEnum -{ - VALUE0, - VALUE1, -}; - -template -struct EnumWrapper -{ - static auto push(lua_State* L, T value) -> std::enable_if_t, bool> - { - lua_pushnumber(L, static_cast(value)); - return true; - } - - static auto get(lua_State* L, int index) -> std::enable_if_t, T> - { - return static_cast(lua_tointeger(L, index)); - } -}; -} // namespace - -namespace luabridge { -template<> -struct Stack : EnumWrapper -{ -}; -} // namespace luabridge - -TEST_F(IssueTests, Issue127) -{ - runLua("result = 1"); - ASSERT_EQ(MyEnum::VALUE1, result()); -} - -TEST_F(IssueTests, Issue8) -{ - runLua(R"(function HelloWorld(args) return args end)"); - - luabridge::LuaRef func = luabridge::getGlobal(L, "HelloWorld"); - - { - auto result = func("helloworld"); - ASSERT_EQ(1, result.size()); - ASSERT_STREQ("helloworld", result[0].unsafe_cast()); - } - - { - const char* str = "helloworld"; - auto result = func(str); - ASSERT_EQ(1, result.size()); - ASSERT_STREQ("helloworld", result[0].unsafe_cast()); - } - - { - std::string str = "helloworld"; - auto result = func(std::move(str)); - ASSERT_EQ(1, result.size()); - ASSERT_STREQ("helloworld", result[0].unsafe_cast()); - } -} - -namespace { -struct SomeClass -{ - luabridge::LuaRef override_; - - SomeClass(lua_State* L) - : override_(luabridge::main_thread(L)) - { - } - - void SomeMember() - { - if (override_.isFunction()) - override_(); - } -}; -} // namespace - -TEST_F(IssueTests, IssueMainThread) -{ - luabridge::getGlobalNamespace(L) - .beginClass("SomeClass") - .addConstructor() - .addFunction("SomeMember", &SomeClass::SomeMember) - .addProperty("SomeMemberOveride", &SomeClass::override_) - .endClass(); - - const char* source = R"( - function test() - c:SomeMember() - c.SomeMemberOveride = MyHandler - c:SomeMember() - --This is pretty cool too! - c:SomeMemberOveride() - --Revert to C++ version - c.SomeMemberOveride = nil - c:SomeMember() - return - end - - function MyHandler() - print 'SomeMember Overidden by Lua' - end - )"; - - const char* threadSource = "c = SomeClass()"; - - lua_State* thread = lua_newthread(L); - - if (!runLua(threadSource, thread)) - { - FAIL(); - return; - } - - lua_pop(L, 1); - lua_gc(L, LUA_GCCOLLECT, 0); - - if (!runLua(source, L)) - { - FAIL(); - return; - } - - luabridge::LuaRef test = luabridge::getGlobal(L, "test"); - test(); -} diff --git a/LuaBridge3/Tests/Source/IteratorTests.cpp b/LuaBridge3/Tests/Source/IteratorTests.cpp deleted file mode 100644 index c251026..0000000 --- a/LuaBridge3/Tests/Source/IteratorTests.cpp +++ /dev/null @@ -1,93 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2018, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include "LuaBridge/detail/Iterator.h" - -struct IteratorTests : TestBase -{ -}; - -TEST_F(IteratorTests, DictionaryIteration) -{ - runLua("result = {" - " bool = true," - " int = 5," - " c = 3.14," - " [true] = 'D'," - " [8] = 'abc'," - " fn = function (i)" - " result = i + 1" - " end" - "}"); - - std::map expected{ - {{L, "bool"}, {L, true}}, - {{L, "int"}, {L, 5}}, - {{L, 'c'}, {L, 3.14}}, - {{L, true}, {L, 'D'}}, - {{L, 8}, {L, "abc"}}, - {{L, "fn"}, {L, result()["fn"]}}, - }; - - std::map actual; - - for (luabridge::Iterator iterator(result()); !iterator.isNil(); ++iterator) - { - actual.emplace(iterator.key(), iterator.value()); - } - - ASSERT_EQ(expected, actual); - - actual.clear(); - - for (auto&& pair : pairs(result())) - { - actual.emplace(pair.first, pair.second); - } - - ASSERT_EQ(expected, actual); -} - -TEST_F(IteratorTests, SequenceIteration) -{ - runLua("result = {" - " true," - " 5," - " 3.14," - " 'D'," - " 'abc'," - " function (i)" - " result = i + 1" - " end" - "}"); - - std::map expected{ - {{L, 1}, {L, true}}, - {{L, 2}, {L, 5}}, - {{L, 3}, {L, 3.14}}, - {{L, 4}, {L, 'D'}}, - {{L, 5}, {L, "abc"}}, - {{L, 6}, {L, result()[6]}}, - }; - - std::map actual; - - for (luabridge::Iterator iterator(result()); !iterator.isNil(); ++iterator) - { - actual.emplace(iterator.key(), iterator.value()); - } - - ASSERT_EQ(expected, actual); - - actual.clear(); - - for (auto&& pair : pairs(result())) - { - actual.emplace(pair.first, pair.second); - } - - ASSERT_EQ(expected, actual); -} diff --git a/LuaBridge3/Tests/Source/LegacyTests.cpp b/LuaBridge3/Tests/Source/LegacyTests.cpp deleted file mode 100644 index ebef9c7..0000000 --- a/LuaBridge3/Tests/Source/LegacyTests.cpp +++ /dev/null @@ -1,268 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -// A set of tests of different types' communication with Lua - -#include "TestBase.h" -#include "LegacyTests.h" - -#include "LuaBridge/RefCountedObject.h" - -#include -#include -#include -#include - -using namespace std; -using namespace luabridge; - -namespace { - -//============================================================================== - -/* - * Test classes - */ - -bool g_success = true; - -bool testSucceeded() -{ - bool b = g_success; - g_success = false; - return b; -} - -typedef int fn_type; -enum -{ - FN_CTOR, - FN_DTOR, - FN_STATIC, - FN_VIRTUAL, - FN_PROPGET, - FN_PROPSET, - FN_STATIC_PROPGET, - FN_STATIC_PROPSET, - FN_OPERATOR, - NUM_FN_TYPES -}; - -struct fn_called -{ - bool called[NUM_FN_TYPES]; - fn_called() { memset(called, 0, NUM_FN_TYPES * sizeof(bool)); } -}; - -fn_called A_functions, B_functions; - -bool testAFnCalled(fn_type f) -{ - bool b = A_functions.called[f]; - A_functions.called[f] = false; - return b; -} - -bool testBFnCalled(fn_type f) -{ - bool b = B_functions.called[f]; - B_functions.called[f] = false; - return b; -} - -class A -{ -protected: - string name; - mutable bool success; - -public: - A(string const& name_) : name(name_), success(false), testProp(47) - { - A_functions.called[FN_CTOR] = true; - } - virtual ~A() { A_functions.called[FN_DTOR] = true; } - - virtual void testVirtual() { A_functions.called[FN_VIRTUAL] = true; } - - const char* getName() const { return name.c_str(); } - - void setSuccess() const { success = true; } - - bool testSucceeded() const - { - bool b = success; - success = false; - return b; - } - - static void testStatic() { A_functions.called[FN_STATIC] = true; } - - int testProp; - int testPropGet() const - { - A_functions.called[FN_PROPGET] = true; - return testProp; - } - void testPropSet(int x) - { - A_functions.called[FN_PROPSET] = true; - testProp = x; - } - - static int testStaticProp; - static int testStaticPropGet() - { - A_functions.called[FN_STATIC_PROPGET] = true; - return testStaticProp; - } - static void testStaticPropSet(int x) - { - A_functions.called[FN_STATIC_PROPSET] = true; - testStaticProp = x; - } - - A operator+(A const& other) - { - A_functions.called[FN_OPERATOR] = true; - return A(name + " + " + other.name); - } -}; - -int A::testStaticProp = 47; - -class B : public A -{ -public: - explicit B(string const& name_) : A(name_) { B_functions.called[FN_CTOR] = true; } - - virtual ~B() { B_functions.called[FN_DTOR] = true; } - - virtual void testVirtual() { B_functions.called[FN_VIRTUAL] = true; } - - static void testStatic2() { B_functions.called[FN_STATIC] = true; } -}; - -/* - * Test functions - */ - -int testRetInt() -{ - return 47; -} - -float testRetFloat() -{ - return 47.0f; -} - -char const* testRetConstCharPtr() -{ - return "Hello, world"; -} - -string testRetStdString() -{ - static string ret("Hello, world"); - return ret; -} - -void testParamInt(int a) -{ - g_success = (a == 47); -} - -void testParamBool(bool b) -{ - g_success = b; -} - -void testParamFloat(float f) -{ - g_success = (f == 47.0f); -} - -void testParamConstCharPtr(char const* str) -{ - g_success = !strcmp(str, "Hello, world"); -} - -void testParamStdString(string str) -{ - g_success = !strcmp(str.c_str(), "Hello, world"); -} - -void testParamStdStringRef(const string& str) -{ - g_success = !strcmp(str.c_str(), "Hello, world"); -} - -void testParamAPtr(A* a) -{ - a->setSuccess(); -} - -void testParamAPtrConst(A* const a) -{ - a->setSuccess(); -} - -void testParamConstAPtr(const A* a) -{ - a->setSuccess(); -} - -// add our own functions and classes to a Lua environment -void addToState(lua_State* L) -{ - getGlobalNamespace(L) - .addFunction("testSucceeded", &testSucceeded) - .addFunction("testAFnCalled", &testAFnCalled) - .addFunction("testBFnCalled", &testBFnCalled) - .addFunction("testRetInt", &testRetInt) - .addFunction("testRetFloat", &testRetFloat) - .addFunction("testRetConstCharPtr", &testRetConstCharPtr) - .addFunction("testRetStdString", &testRetStdString) - .addFunction("testParamInt", &testParamInt) - .addFunction("testParamBool", &testParamBool) - .addFunction("testParamFloat", &testParamFloat) - .addFunction("testParamConstCharPtr", &testParamConstCharPtr) - .addFunction("testParamStdString", &testParamStdString) - .addFunction("testParamStdStringRef", &testParamStdStringRef) - .beginClass("A") - .addConstructor() - .addFunction("testVirtual", &A::testVirtual) - .addFunction("getName", &A::getName) - .addFunction("testSucceeded", &A::testSucceeded) - .addFunction("__add", &A::operator+) - .addProperty("testProp", &A::testProp) - .addProperty("testProp2", &A::testPropGet, &A::testPropSet) - .addStaticFunction("testStatic", &A::testStatic) - .addStaticProperty("testStaticProp", &A::testStaticProp) - .addStaticProperty("testStaticProp2", &A::testStaticPropGet, &A::testStaticPropSet) - .endClass() - .deriveClass("B") - .addConstructor() - .addStaticFunction("testStatic2", &B::testStatic2) - .endClass() - .addFunction("testParamAPtr", &testParamAPtr) - .addFunction("testParamAPtrConst", &testParamAPtrConst) - .addFunction("testParamConstAPtr", &testParamConstAPtr); -} -} // namespace - -struct LegacyTests : TestBase -{ -}; - -TEST_F(LegacyTests, AllTests) -{ - addToState(L); - - // Execute lua files in order - runLua(BinaryData::Tests_lua); -} diff --git a/LuaBridge3/Tests/Source/LegacyTests.h b/LuaBridge3/Tests/Source/LegacyTests.h deleted file mode 100644 index 7406ba2..0000000 --- a/LuaBridge3/Tests/Source/LegacyTests.h +++ /dev/null @@ -1,107 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -namespace BinaryData { - -//================== Tests.lua ================== - -static constexpr const char Tests_lua[] = R"_( - --- test lua script to be run with the luabridge test program - --- enum from C++ -FN_CTOR = 0 -FN_DTOR = 1 -FN_STATIC = 2 -FN_VIRTUAL = 3 -FN_PROPGET = 4 -FN_PROPSET = 5 -FN_STATIC_PROPGET = 6 -FN_STATIC_PROPSET = 7 -FN_OPERATOR = 8 -NUM_FN_TYPES = 9 - --- function to print contents of a table -function printtable (t) - for k, v in pairs(t) do - if (type(v) == "table") then - print(k .. " =>", "(table)"); - elseif (type(v) == "function") then - print(k .. " =>", "(function)"); - elseif (type(v) == "userdata") then - print(k .. " =>", "(userdata)"); - else - print(k .. " =>", v); - end - end -end - -function assert (expr) - if (not expr) then error("assert failed", 2) end -end - --- test functions registered from C++ - -assert(testSucceeded()); -assert(testRetInt() == 47); -assert(testRetFloat() == 47.0); -assert(testRetConstCharPtr() == "Hello, world"); -assert(testRetStdString() == "Hello, world"); - -testParamInt(47); assert(testSucceeded()); -testParamBool(true); assert(testSucceeded()); -testParamFloat(47.0); assert(testSucceeded()); -testParamConstCharPtr("Hello, world"); assert(testSucceeded()); -testParamStdString("Hello, world"); assert(testSucceeded()); -testParamStdStringRef("Hello, world"); assert(testSucceeded()); - --- test static methods of classes registered from C++ - -A.testStatic(); assert(testAFnCalled(FN_STATIC)); -B.testStatic(); assert(testAFnCalled(FN_STATIC)); -B.testStatic2(); assert(testBFnCalled(FN_STATIC)); - --- test static properties of classes registered from C++ - -assert(A.testStaticProp == 47); -assert(A.testStaticProp2 == 47); assert(testAFnCalled(FN_STATIC_PROPGET)); -A.testStaticProp = 48; assert(A.testStaticProp == 48); -A.testStaticProp2 = 49; assert(testAFnCalled(FN_STATIC_PROPSET) and A.testStaticProp2 == 49); - --- test classes registered from C++ - -object1 = A("object1"); assert(testAFnCalled(FN_CTOR)); -object1:testVirtual(); assert(testAFnCalled(FN_VIRTUAL)); - -object2 = B("object2"); assert(testAFnCalled(FN_CTOR) and testBFnCalled(FN_CTOR)); -object2:testVirtual(); assert(testBFnCalled(FN_VIRTUAL) and not testAFnCalled(FN_VIRTUAL)); - --- test functions taking and returning objects - -testParamAPtr(object1); assert(object1:testSucceeded()); -testParamAPtrConst(object1); assert(object1:testSucceeded()); -testParamConstAPtr(object1); assert(object1:testSucceeded()); - -testParamAPtr(object2); assert(object2:testSucceeded()); -testParamAPtrConst(object2); assert(object2:testSucceeded()); -testParamConstAPtr(object2); assert(object2:testSucceeded()); - --- test properties - -assert(object1.testProp == 47); -assert(object1.testProp2 == 47); assert(testAFnCalled(FN_PROPGET)); -assert(object2.testProp == 47); -assert(object2.testProp2 == 47); assert(testAFnCalled(FN_PROPGET)); - -object1.testProp = 48; assert(object1.testProp == 48); -object1.testProp2 = 49; assert(testAFnCalled(FN_PROPSET) and object1.testProp2 == 49); - --- test operator overload - -object1a = object1 + object1; assert(testAFnCalled(FN_OPERATOR)); -assert(object1a:getName() == "object1 + object1"); - -)_"; - -} // namespace BinaryData diff --git a/LuaBridge3/Tests/Source/ListTests.cpp b/LuaBridge3/Tests/Source/ListTests.cpp deleted file mode 100644 index afa75d3..0000000 --- a/LuaBridge3/Tests/Source/ListTests.cpp +++ /dev/null @@ -1,150 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" -#include "TestTypes.h" - -#include "LuaBridge/List.h" - -#include - -namespace { -template -std::list toList(const std::vector& vector) -{ - return {vector.begin(), vector.end()}; -} - -template -void checkEquals(const std::list& expected, const std::list& actual) -{ - using U = std::decay_t; - - if constexpr (std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_FLOAT_EQ((*std::next(expected.begin(), i)), (*std::next(actual.begin(), i))); - } - else if constexpr (std::is_same_v || std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_DOUBLE_EQ((*std::next(expected.begin(), i)), (*std::next(actual.begin(), i))); - } - else if constexpr (std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_STREQ((*std::next(expected.begin(), i)), (*std::next(actual.begin(), i))); - } - else - { - ASSERT_EQ(expected, actual); - } -} -} // namespace - -template -struct ListTest : TestBase -{ -}; - -TYPED_TEST_SUITE_P(ListTest); - -TYPED_TEST_P(ListTest, LuaRef) -{ - using Traits = TypeTraits; - - this->runLua("result = {" + Traits::list() + "}"); - - std::list expected = toList(Traits::values()); - std::list actual = this->result(); - - checkEquals(expected, actual); -} - -REGISTER_TYPED_TEST_SUITE_P(ListTest, LuaRef); - -INSTANTIATE_TYPED_TEST_SUITE_P(ListTest, ListTest, TestTypes); - -struct ListTests : TestBase -{ -}; - -TEST_F(ListTests, PassToFunction) -{ - runLua("function foo (list) " - " result = list " - "end"); - - auto foo = luabridge::getGlobal(L, "foo"); - - resetResult(); - - std::list lvalue{ 10, 20, 30 }; - foo(lvalue); - ASSERT_TRUE(result().isTable()); - ASSERT_EQ(lvalue, result>()); - - resetResult(); - - const std::list constLvalue = lvalue; - foo(constLvalue); - ASSERT_TRUE(result().isTable()); - ASSERT_EQ(lvalue, result>()); -} - -TEST_F(ListTests, UnregisteredClass) -{ - struct Unregistered {}; - -#if LUABRIDGE_HAS_EXCEPTIONS - [[maybe_unused]] luabridge::Result r; - ASSERT_THROW((r = luabridge::push(L, std::list{ Unregistered() })), std::exception); -#else - ASSERT_FALSE((luabridge::push(L, std::list{ Unregistered() }))); -#endif -} - -TEST_F(ListTests, IsInstance) -{ - ASSERT_TRUE((luabridge::push(L, std::list{ 1, 2, 3 }))); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - lua_pop(L, 1); - - ASSERT_TRUE((luabridge::push(L, 1))); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); -} - -TEST_F(ListTests, StackOverflow) -{ - exhaustStackSpace(); - - std::list value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -#if !LUABRIDGE_HAS_EXCEPTIONS -TEST_F(ListTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) -{ - class Unregistered {}; - - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::list v; - v.emplace_back(); - v.emplace_back(); - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); -} -#endif diff --git a/LuaBridge3/Tests/Source/LuaRefTests.cpp b/LuaBridge3/Tests/Source/LuaRefTests.cpp deleted file mode 100644 index b2cbf22..0000000 --- a/LuaBridge3/Tests/Source/LuaRefTests.cpp +++ /dev/null @@ -1,685 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include - -struct LuaRefTests : TestBase -{ -}; - -TEST_F(LuaRefTests, TypeCheck) -{ - { - luabridge::LuaRef none(L); - EXPECT_FALSE(none.isValid()); - EXPECT_TRUE(none.isNil()); - std::stringstream ss; - none.print(ss); - EXPECT_EQ("nil", ss.str()); - EXPECT_NE(0u, none.hash()); - } - - { - runLua("result = nil"); - EXPECT_EQ(LUA_TNIL, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isNil()); - std::stringstream ss; - result().print(ss); - EXPECT_EQ("nil", ss.str()); - EXPECT_NE(0u, result().hash()); - } - - { - runLua("result = true"); - EXPECT_EQ(LUA_TBOOLEAN, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isBool()); - std::stringstream ss; - result().print(ss); - EXPECT_EQ("true", ss.str()); - EXPECT_NE(0u, result().hash()); - } - - { - runLua("result = 11"); - EXPECT_EQ(LUA_TNUMBER, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isNumber()); - std::stringstream ss; - result().print(ss); - EXPECT_EQ("11", ss.str()); - EXPECT_NE(0u, result().hash()); - } - - { - runLua("result = 3.1"); - EXPECT_EQ(LUA_TNUMBER, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isNumber()); - std::stringstream ss; - result().print(ss); - EXPECT_EQ("3.1", ss.str()); - EXPECT_NE(0u, result().hash()); - } - - { - runLua("result = 'abcd'"); - EXPECT_EQ(LUA_TSTRING, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isString()); - std::stringstream ss; - result().print(ss); - EXPECT_EQ("\"abcd\"", ss.str()); - EXPECT_NE(0u, result().hash()); - } - - { - runLua("result = {}"); - EXPECT_EQ(LUA_TTABLE, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isTable()); - std::stringstream ss; - result().print(ss); - EXPECT_NE(std::string::npos, ss.str().find("table:")); - EXPECT_NE(0u, result().hash()); - } - - { - runLua("result = function () end"); - EXPECT_EQ(LUA_TFUNCTION, result().type()); - EXPECT_TRUE(result().isValid()); - EXPECT_TRUE(result().isFunction()); - EXPECT_TRUE(result().isCallable()); - std::stringstream ss; - result().print(ss); - EXPECT_NE(std::string::npos, ss.str().find("function:")); - EXPECT_NE(0u, result().hash()); - } - - { - void* x = nullptr; - lua_pushlightuserdata(L, x); - auto result = luabridge::LuaRef::fromStack(L); - EXPECT_EQ(LUA_TLIGHTUSERDATA, result.type()); - EXPECT_TRUE(result.isValid()); - EXPECT_TRUE(result.isLightUserdata()); - std::stringstream ss; - result.print(ss); - EXPECT_NE(std::string::npos, ss.str().find("userdata:")); - EXPECT_NE(0u, result.hash()); - lua_pop(L, 1); - } - - { - lua_newuserdata(L, 100); - auto result = luabridge::LuaRef::fromStack(L); - EXPECT_EQ(LUA_TUSERDATA, result.type()); - EXPECT_TRUE(result.isValid()); - EXPECT_TRUE(result.isUserdata()); - std::stringstream ss; - result.print(ss); - EXPECT_NE(std::string::npos, ss.str().find("userdata:")); - EXPECT_NE(0u, result.hash()); - } - - { - auto threadL = lua_newthread(L); - lua_pushthread(threadL); - auto result = luabridge::LuaRef::fromStack(L); - EXPECT_EQ(LUA_TTHREAD, result.type()); - EXPECT_TRUE(result.isValid()); - EXPECT_TRUE(result.isThread()); - std::stringstream ss; - result.print(ss); - EXPECT_NE(std::string::npos, ss.str().find("thread:")); - EXPECT_NE(0u, result.hash()); - } -} - -TEST_F(LuaRefTests, ValueAccess) -{ - runLua("result = true"); - EXPECT_TRUE(result().isBool()); - ASSERT_TRUE(result()); - - runLua("result = 7"); - EXPECT_TRUE(result().isNumber()); - ASSERT_EQ(7u, result()); - ASSERT_EQ(7, result()); - ASSERT_EQ(7u, result()); - ASSERT_EQ(7, result()); - ASSERT_EQ(7u, result()); - ASSERT_EQ(7, result()); - ASSERT_EQ(7u, result()); - ASSERT_EQ(7, result()); - ASSERT_EQ(7u, result()); - - runLua("result = 3.14"); - EXPECT_TRUE(result().isNumber()); - ASSERT_FLOAT_EQ(3.14f, result()); - ASSERT_DOUBLE_EQ(3.14, result()); - - runLua("result = 'D'"); - EXPECT_TRUE(result().isString()); - ASSERT_EQ("D", result()); - ASSERT_EQ('D', result()); - ASSERT_EQ("D", result()); - ASSERT_STREQ("D", result()); - - runLua("result = 'abc'"); - EXPECT_TRUE(result().isString()); - ASSERT_EQ("abc", result()); - ASSERT_STREQ("abc", result()); - - runLua("result = function (i) " - " result = i + 1 " - " return i " - "end"); - EXPECT_TRUE(result().isFunction()); - auto fnResult = result()(41); // Replaces result variable - EXPECT_TRUE(fnResult); - EXPECT_TRUE(fnResult.size()); - EXPECT_TRUE(fnResult[0].isNumber()); - ASSERT_EQ(41, fnResult[0].unsafe_cast()); - ASSERT_EQ(41, *fnResult[0].cast()); - ASSERT_EQ(41, luabridge::unsafe_cast(fnResult[0])); - ASSERT_EQ(41, *luabridge::cast(fnResult[0])); - EXPECT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); -} - -TEST_F(LuaRefTests, DictionaryRead) -{ - runLua("result = {" - " bool = true," - " int = 5," - " c = 3.14," - " [true] = 'D'," - " [8] = 'abc'," - " fn = function (i) " - " result = i + 1 " - " return i " - " end" - "}"); - - EXPECT_TRUE(result()["bool"].isBool()); - EXPECT_TRUE(result()["bool"].cast()); - - EXPECT_TRUE(result()["int"].isNumber()); - ASSERT_EQ(5u, result()["int"].unsafe_cast()); - ASSERT_EQ(5, result()["int"].unsafe_cast()); - ASSERT_EQ(5u, result()["int"].unsafe_cast()); - ASSERT_EQ(5, result()["int"].unsafe_cast()); - ASSERT_EQ(5u, result()["int"].unsafe_cast()); - ASSERT_EQ(5, result()["int"].unsafe_cast()); - ASSERT_EQ(5u, result()["int"].unsafe_cast()); - ASSERT_EQ(5, result()["int"].unsafe_cast()); - ASSERT_EQ(5u, result()["int"].unsafe_cast()); - - EXPECT_TRUE(result()['c'].isNumber()); - ASSERT_FLOAT_EQ(3.14f, result()['c'].unsafe_cast()); - ASSERT_DOUBLE_EQ(3.14, result()['c'].unsafe_cast()); - - EXPECT_TRUE(result()[true].isString()); - ASSERT_EQ('D', result()[true].unsafe_cast()); - ASSERT_EQ("D", result()[true].unsafe_cast()); - ASSERT_STREQ("D", result()[true].unsafe_cast()); - - EXPECT_TRUE(result()[8].isString()); - ASSERT_EQ("abc", result()[8].unsafe_cast()); - ASSERT_STREQ("abc", result()[8].unsafe_cast()); - - EXPECT_TRUE(result()["fn"].isFunction()); - auto fnResult = result()["fn"](41); // Replaces result variable - EXPECT_TRUE(fnResult); - EXPECT_TRUE(fnResult.size()); - EXPECT_TRUE(fnResult[0].isNumber()); - ASSERT_EQ(41, fnResult[0].unsafe_cast()); - EXPECT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); -} - -TEST_F(LuaRefTests, DictionaryWrite) -{ - runLua("result = {a = 5}"); - EXPECT_TRUE(result()["a"].isNumber()); - ASSERT_EQ(5, result()["a"].unsafe_cast()); - - result()["a"] = 7; - ASSERT_EQ(7, result()["a"].unsafe_cast()); - - runLua("result = result.a"); - ASSERT_EQ(7, result()); - - runLua("result = {a = {b = 1}}"); - ASSERT_EQ(1, result()["a"]["b"].unsafe_cast()); - - result()["a"]["b"] = 2; - ASSERT_EQ(2, result()["a"]["b"].unsafe_cast()); -} - -struct Class -{ -}; - -TEST_F(LuaRefTests, Comparison) -{ - runLua("function foo () end " - "local m = {} " - "m.__eq = function (l, r) return l.a == r.a end " - "m.__lt = function (l, r) return l.a < r.a end " - "m.__le = function (l, r) return l.a <= r.a end " - "t1 = {a = 1} setmetatable (t1, m) " - "t2 = {a = 2} setmetatable (t2, m) " - "t3 = {a = 1} setmetatable (t3, m) " - "t4 = {a = 2} "); - - luabridge::getGlobalNamespace(L).beginClass("Class").endClass(); - - luabridge::LuaRef invalid(L); - luabridge::LuaRef nil(L, luabridge::LuaNil()); - luabridge::LuaRef boolFalse(L, false); - luabridge::LuaRef boolTrue(L, true); - luabridge::LuaRef minus5(L, -5); - luabridge::LuaRef numPi(L, 3.14); - luabridge::LuaRef stringA(L, 'a'); - luabridge::LuaRef stringAB(L, "ab"); - luabridge::LuaRef t1 = luabridge::getGlobal(L, "t1"); - luabridge::LuaRef t2 = luabridge::getGlobal(L, "t2"); - luabridge::LuaRef t3 = luabridge::getGlobal(L, "t3"); - luabridge::LuaRef t4 = luabridge::getGlobal(L, "t4"); - - EXPECT_FALSE(invalid.isValid()); - EXPECT_TRUE(nil.isValid()); - - EXPECT_TRUE(nil == invalid); - EXPECT_TRUE(invalid == nil); - - EXPECT_TRUE(nil == nil); - - EXPECT_TRUE(nil < boolFalse); - - EXPECT_TRUE(boolFalse == boolFalse); - EXPECT_TRUE(boolTrue == boolTrue); - - EXPECT_TRUE(boolTrue < minus5); - - EXPECT_TRUE(minus5 == minus5); - EXPECT_FALSE(minus5 == numPi); - EXPECT_TRUE(minus5 < numPi); - EXPECT_TRUE(minus5 <= numPi); - EXPECT_FALSE(minus5 > numPi); - EXPECT_FALSE(minus5 >= numPi); - - EXPECT_TRUE(numPi < stringA); - - EXPECT_TRUE(stringA == stringA); - EXPECT_FALSE(stringA == stringAB); - EXPECT_TRUE(stringA < stringAB); - EXPECT_TRUE(stringA <= stringAB); - EXPECT_FALSE(stringA > stringAB); - EXPECT_FALSE(stringA >= stringAB); - - EXPECT_TRUE(stringA < t1); - - EXPECT_TRUE(t1 == t1); - EXPECT_FALSE(t1 == t2); - EXPECT_TRUE(t1 == t3); - EXPECT_FALSE(t1.rawequal(t3)); - EXPECT_FALSE(t1 == t4); - EXPECT_TRUE(t2 == t2); - EXPECT_FALSE(t2 == t3); - -#if LUABRIDGEDEMO_LUA_VERSION >= 503 && !LUABRIDGE_ON_LUAU && !LUABRIDGE_ON_LUAJIT - // This has changed in lua 5.3 and is quite a behaviour change - EXPECT_TRUE(t2 == t4); -#else - EXPECT_FALSE(t2 == t4); -#endif - - EXPECT_TRUE(t3 == t3); - EXPECT_FALSE(t3 == t4); - - EXPECT_FALSE(t1 < t1); - EXPECT_TRUE(t1 < t2); - EXPECT_FALSE(t1 < t3); - EXPECT_FALSE(t2 < t3); - - EXPECT_TRUE(t1 <= t1); - EXPECT_TRUE(t1 <= t2); - EXPECT_TRUE(t1 <= t3); - EXPECT_FALSE(t2 <= t3); - - EXPECT_FALSE(t1 > t1); - EXPECT_FALSE(t1 > t2); - EXPECT_FALSE(t1 > t3); - EXPECT_TRUE(t2 > t3); - - EXPECT_TRUE(t1 >= t1); - EXPECT_FALSE(t1 >= t2); - EXPECT_TRUE(t1 >= t3); - EXPECT_TRUE(t2 >= t3); -} - -TEST_F(LuaRefTests, ComparisonNumbers) -{ - runLua("result = 7"); - EXPECT_TRUE(result().isNumber()); - - EXPECT_EQ(7, result()); - EXPECT_EQ(result(), 7); - EXPECT_NE(8, result()); - EXPECT_NE(result(), 8); - - EXPECT_GT(8, result()); - EXPECT_LT(result(), 8); - - EXPECT_GE(8, result()); - EXPECT_GE(7, result()); - EXPECT_LE(result(), 8); - EXPECT_LE(result(), 7); -} - -TEST_F(LuaRefTests, ComparisonLuaRef) -{ - runLua("result = 7"); - auto seven = result(); - - runLua("result = 8"); - auto eight = result(); - - EXPECT_EQ(seven, seven); - EXPECT_NE(seven, eight); - EXPECT_NE(eight, seven); - - EXPECT_GT(eight, seven); - EXPECT_LT(seven, eight); - - EXPECT_GE(eight, seven); - EXPECT_LE(seven, eight); -} - -TEST_F(LuaRefTests, ComparisonLuaRefInvalidTypes) -{ - runLua("result = 7"); - auto seven = result(); - - runLua("result = '8'"); - auto eight = result(); - - EXPECT_NE(seven, eight); - EXPECT_NE(eight, seven); - - EXPECT_GT(eight, seven); - EXPECT_LT(seven, eight); - - EXPECT_GE(eight, seven); - EXPECT_LE(seven, eight); -} - -TEST_F(LuaRefTests, Assignment) -{ - runLua("value = {a = 5}"); - auto value = luabridge::getGlobal(L, "value"); - EXPECT_TRUE(value.isTable()); - EXPECT_TRUE(value["a"].isNumber()); - ASSERT_EQ(5, value["a"].unsafe_cast()); - - value = value["a"]; - EXPECT_TRUE(value.isNumber()); - ASSERT_EQ(5, value.unsafe_cast()); - -#if __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wself-assign-overloaded" -#endif - - value = value; - -#if __clang__ -#pragma clang diagnostic pop -#endif - - ASSERT_EQ(LUA_TNUMBER, value.type()); - EXPECT_TRUE(value.isNumber()); - ASSERT_EQ(5, value.unsafe_cast()); - - runLua("t = {a = {b = 5}}"); - auto table = luabridge::getGlobal(L, "t"); - luabridge::LuaRef entry = table["a"]; - luabridge::LuaRef b1 = entry["b"]; - luabridge::LuaRef b2 = table["a"]["b"]; - EXPECT_TRUE(b1 == b2); - - runLua("c1 = 1"); - auto c1 = luabridge::getGlobal(L, "c1"); - ASSERT_EQ(1, c1.unsafe_cast()); - c1 = 11; - ASSERT_EQ(11, c1.unsafe_cast()); -} - -TEST_F(LuaRefTests, MoveConstructAndAssign) -{ - runLua("value = {a = 5}"); - auto value = luabridge::getGlobal(L, "value"); - EXPECT_TRUE(value.isTable()); - EXPECT_TRUE(value["a"].isNumber()); - EXPECT_TRUE(value.rawget("a").isNumber()); - ASSERT_EQ(5, value["a"].unsafe_cast()); - - luabridge::LuaRef moveConstructed = std::move(value); - EXPECT_TRUE(moveConstructed.isTable()); - EXPECT_TRUE(moveConstructed["a"].isNumber()); - ASSERT_EQ(5, moveConstructed["a"].unsafe_cast()); - EXPECT_FALSE(value.isValid()); - - luabridge::LuaRef moveAssigned(L); - moveAssigned = std::move(moveConstructed); - EXPECT_TRUE(moveAssigned.isTable()); - EXPECT_TRUE(moveAssigned["a"].isNumber()); - ASSERT_EQ(5, moveAssigned["a"].unsafe_cast()); - EXPECT_FALSE(moveConstructed.isValid()); -} - -TEST_F(LuaRefTests, Callable) -{ - runLua("function f () end"); - auto f = luabridge::getGlobal(L, "f"); - EXPECT_TRUE(f.isCallable()); - - runLua("x = 1"); - auto x = luabridge::getGlobal(L, "x"); - EXPECT_FALSE(x.isCallable()); - - runLua("meta1 = { __call = function(self) return 5 end }"); - auto meta1 = luabridge::getGlobal(L, "meta1"); - EXPECT_FALSE(meta1.isCallable()); - - runLua("meta2 = { __call = function(self) self.i = self.i + 100 end }; obj = { i = 100 }; setmetatable(obj, meta2)"); - auto obj = luabridge::getGlobal(L, "obj"); - EXPECT_TRUE(obj.isCallable()); - EXPECT_EQ(100, obj["i"].unsafe_cast()); - obj(); - EXPECT_EQ(200, obj["i"].unsafe_cast()); -} - -TEST_F(LuaRefTests, Pop) -{ - lua_pushstring(L, "hello"); - luabridge::LuaRef ref1 = luabridge::LuaRef::fromStack(L); - - lua_pushstring(L, "world!"); - luabridge::LuaRef ref2 = luabridge::LuaRef::fromStack(L); - - ref1.push(); - ref2.pop(); - - EXPECT_EQ(ref1.unsafe_cast(), ref2.unsafe_cast()); - EXPECT_EQ("hello", ref1.tostring()); - EXPECT_EQ("hello", ref2.tostring()); -} - -TEST_F(LuaRefTests, IsInstance) -{ - struct Base - { - }; - - struct Derived : Base - { - }; - - struct Other - { - }; - - struct Unknown : Base - { - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addConstructor() - .endClass() - .deriveClass("Derived") - .addConstructor() - .endClass() - .beginClass("Other") - .addConstructor() - .endClass(); - - runLua("result = Base ()"); - EXPECT_TRUE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_TRUE(result().isUserdata()); - - runLua("result = Derived ()"); - EXPECT_TRUE(result().isInstance()); - EXPECT_TRUE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_TRUE(result().isUserdata()); - - runLua("result = Other ()"); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_TRUE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_TRUE(result().isUserdata()); - - runLua("result = 3.14"); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isInstance()); - EXPECT_FALSE(result().isUserdata()); -} - -TEST_F(LuaRefTests, Print) -{ - { - runLua("result = true"); - std::ostringstream stream; - stream << result(); - ASSERT_EQ("true", stream.str()); - } - { - runLua("result = false"); - std::ostringstream stream; - stream << result(); - ASSERT_EQ("false", stream.str()); - } - { - runLua("result = 5"); - std::ostringstream stream; - stream << result(); - ASSERT_EQ("5", stream.str()); - } - { - runLua("result = 'abc'"); - std::ostringstream stream; - stream << result(); - ASSERT_EQ("\"abc\"", stream.str()); - } - - runLua("result = {" - " true_ = true," - " false_ = false," - " five = 5," - " abc = 'abc'" - "}"); - { - std::ostringstream stream; - stream << result()["true_"]; - ASSERT_EQ("true", stream.str()); - } - { - std::ostringstream stream; - stream << result()["false_"]; - ASSERT_EQ("false", stream.str()); - } - { - std::ostringstream stream; - stream << result()["five"]; - ASSERT_EQ("5", stream.str()); - } - { - std::ostringstream stream; - stream << result()["abc"]; - ASSERT_EQ("\"abc\"", stream.str()); - } -} - -TEST_F(LuaRefTests, RegisterLambdaInTable) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("Entities") - .addFunction("GetLocalHero", [&]() { - auto table = luabridge::newTable(L); - table.push(L); - - luabridge::getNamespaceFromStack(L) - .addProperty("index", [] { return 150; }) - .addFunction("Health", [&] { return 500; }); - - table.pop(); - return table; - }) - .endNamespace(); - - runLua("result = Entities.GetLocalHero().Health()"); - ASSERT_EQ(500, result()); -} - -TEST_F(LuaRefTests, HookTesting) -{ - std::map hooklist; - - auto hook = [&](const std::string& name, luabridge::LuaRef cb) { - hooklist.emplace(name, std::move(cb)); - }; - - luabridge::getGlobalNamespace(L) - .addFunction("Hook", hook); - - runLua(R"( - function hook1(type, packet) - print("lol") - end - - Hook("hook1", hook1) - )"); - - for (auto& func : hooklist) { - func.second(0, "x"); - } -} diff --git a/LuaBridge3/Tests/Source/MapTests.cpp b/LuaBridge3/Tests/Source/MapTests.cpp deleted file mode 100644 index 8c53f6e..0000000 --- a/LuaBridge3/Tests/Source/MapTests.cpp +++ /dev/null @@ -1,270 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include "LuaBridge/Map.h" - -#include - -namespace { -struct Unregistered -{ - bool operator<(const Unregistered& other) const - { - return true; - } -}; - -struct Data -{ - /* explicit */ Data(int i) : i(i) {} - - int i; -}; - -bool operator==(const Data& lhs, const Data& rhs) -{ - return lhs.i == rhs.i; -} - -bool operator<(const Data& lhs, const Data& rhs) -{ - return lhs.i < rhs.i; -} - -std::ostream& operator<<(std::ostream& lhs, const Data& rhs) -{ - lhs << "{" << rhs.i << "}"; - return lhs; -} - -std::map processValues(const std::map& data) -{ - return data; -} - -std::map processPointers(const std::map& data) -{ - std::map result; - - for (const auto& item : data) - result.emplace(item.first, *item.second); - - return result; -} -} // namespace - -namespace std { -template <> -struct hash -{ - std::size_t operator()(const Unregistered& value) const - { - return 0; - } -}; -} // namespace std - -struct MapTests : TestBase -{ -}; - -TEST_F(MapTests, LuaRef) -{ - { - using Map = std::map; - - const Map expected { {1, 'a'}, {2, 'b'}, {3, 'c'} }; - - runLua("result = {'a', 'b', 'c'}"); - - Map actual = result(); - EXPECT_EQ(expected, actual); - EXPECT_EQ(expected, result()); - } - - { - using Map = std::map; - - const Map expected { {1, "abcdef"}, {2, "bcdef"}, {3, "cdef"} }; - - runLua("result = {'abcdef', 'bcdef', 'cdef'}"); - - Map actual = result(); - EXPECT_EQ(expected, actual); - EXPECT_EQ(expected, result()); - } - - { - using Map = std::map; - - const Map expected { - { luabridge::LuaRef(L, false), luabridge::LuaRef(L, true) }, - { luabridge::LuaRef(L, 'a'), luabridge::LuaRef(L, "abc") }, - { luabridge::LuaRef(L, 1), luabridge::LuaRef(L, 5) }, - { luabridge::LuaRef(L, 3.14), luabridge::LuaRef(L, -1.1) }, - }; - - runLua("result = {[false] = true, a = 'abc', [1] = 5, [3.14] = -1.1}"); - - auto resultRef = result(); - EXPECT_TRUE(resultRef.isInstance()); - - Map actual = resultRef; - EXPECT_EQ(expected, actual); - - EXPECT_EQ(expected, result()); - } -} - -TEST_F(MapTests, CastToMap) -{ - using StrToInt = std::map; - runLua("result = {[1] = 2, a = 3}"); - ASSERT_EQ((StrToInt{{"1", 2}, {"a", 3}}), result()); - - using IntToInt = std::map; - runLua("result = {[1] = 2, a = 3}"); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_ANY_THROW((result())); -#else - ASSERT_DEATH((result()), ""); -#endif -} - -TEST_F(MapTests, PassToFunction) -{ - runLua("function foo (map) " - " result = map " - "end"); - - auto foo = luabridge::getGlobal(L, "foo"); - using Int2Bool = std::map; - - resetResult(); - - Int2Bool lvalue{{10, false}, {20, true}, {30, true}}; - foo(lvalue); - ASSERT_TRUE(result().isTable()); - ASSERT_EQ(lvalue, result()); - - resetResult(); - - const Int2Bool constLvalue = lvalue; - foo(constLvalue); - ASSERT_TRUE(result().isTable()); - ASSERT_EQ(constLvalue, result()); -} - -TEST_F(MapTests, PassFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Data") - .addConstructor() - .endClass() - .addFunction("processValues", &processValues) - .addFunction("processPointers", &processPointers); - - { - resetResult(); - runLua("result = processValues ({[Data (-1)] = Data (2)})"); - std::map expected{{Data(-1), Data(2)}}; - const auto actual = result>(); - ASSERT_EQ(expected, actual); - } - - { - resetResult(); - runLua("result = processPointers ({[Data (3)] = Data (-4)})"); - std::map expected{{Data(3), Data(-4)}}; - const auto actual = result>(); - ASSERT_EQ(expected, actual); - } -} - -TEST_F(MapTests, UnregisteredClass) -{ - { -#if LUABRIDGE_HAS_EXCEPTIONS - [[maybe_unused]] luabridge::Result r; - ASSERT_THROW((r = luabridge::push(L, std::map{ { Unregistered(), 1 } })), std::exception); -#else - ASSERT_FALSE((luabridge::push(L, std::map{ { Unregistered(), 1 } }))); -#endif - } - - { -#if LUABRIDGE_HAS_EXCEPTIONS - [[maybe_unused]] luabridge::Result r; - ASSERT_THROW((r = luabridge::push(L, std::map{ { 1, Unregistered() } })), std::exception); -#else - ASSERT_FALSE((luabridge::push(L, std::map{ { 1, Unregistered() } }))); -#endif - } -} - -TEST_F(MapTests, IsInstance) -{ - ASSERT_TRUE((luabridge::push(L, std::map{ { "x", 1 }, { "y", 2 }, { "z", 3 } }))); - EXPECT_TRUE((luabridge::isInstance>(L, -1))); - - lua_pop(L, 1); - - ASSERT_TRUE((luabridge::push(L, 1))); - EXPECT_FALSE((luabridge::isInstance>(L, -1))); -} - -TEST_F(MapTests, StackOverflow) -{ - exhaustStackSpace(); - - std::map value{ { "x", 1 }, { "y", 2 }, { "z", 3 } }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -#if !LUABRIDGE_HAS_EXCEPTIONS -TEST_F(MapTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) -{ - { - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::map v; - v.emplace(std::make_pair(1, Unregistered{})); - v.emplace(std::make_pair(2, Unregistered{})); - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); - } - - { - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::map v; - v.emplace(std::make_pair(Unregistered{}, 1)); - v.emplace(std::make_pair(Unregistered{}, 2)); - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); - } -} -#endif diff --git a/LuaBridge3/Tests/Source/NamespaceTests.cpp b/LuaBridge3/Tests/Source/NamespaceTests.cpp deleted file mode 100644 index 2b6a9d7..0000000 --- a/LuaBridge3/Tests/Source/NamespaceTests.cpp +++ /dev/null @@ -1,532 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include - -struct NamespaceTests : TestBase -{ - template - T variable(const std::string& name) - { - runLua("result = " + name); - return result(); - } -}; - -namespace { -enum class A { x, y }; -} // namespace - -TEST_F(NamespaceTests, Variables) -{ - int int_ = -10; - int stored = 42; - auto any = luabridge::newTable(L); - any["a"] = 1; - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(luabridge::getGlobalNamespace(L).addProperty("int", &int_), std::logic_error); -#endif - - runLua("result = int"); - ASSERT_TRUE(result().isNil()); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("int", &int_) - .addProperty("any", &any) - .addProperty("fnc_get", [stored] { return stored; }) - .addProperty("fnc_getset", [stored] { return stored; }, [&stored](int v) { stored = v; }) - .addProperty("A_x", A::x) - .addProperty("A_y", A::y) - .endNamespace(); - - ASSERT_EQ(-10, variable("ns.int")); - ASSERT_EQ(any, variable("ns.any")); - - runLua("ns.int = -20"); - ASSERT_EQ(-20, int_); - - runLua("ns.any = {b = 2}"); - ASSERT_TRUE(any.isTable()); - ASSERT_TRUE(any["b"].isNumber()); - ASSERT_EQ(2, any["b"].cast()); - - runLua("result = ns.fnc_get"); - ASSERT_EQ(stored, result()); - - runLua("result = ns.fnc_getset"); - ASSERT_EQ(stored, result()); - - runLua("ns.fnc_getset = 1337"); - ASSERT_EQ(1337, stored); - - runLua("result = ns.A_x"); - ASSERT_EQ(A::x, static_cast(result())); - - runLua("result = ns.A_y"); - ASSERT_EQ(A::y, static_cast(result())); -} - -TEST_F(NamespaceTests, ReadOnlyVariables) -{ - int int_ = -10; - auto any = luabridge::newTable(L); - any["a"] = 1; - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(luabridge::getGlobalNamespace(L).addProperty("int", &int_), std::logic_error); -#endif - - runLua("result = int"); - ASSERT_TRUE(result().isNil()); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("int", &int_, false) - .addProperty("any", &any, false) - .endNamespace(); - - ASSERT_EQ(-10, variable("ns.int")); - ASSERT_EQ(any, variable("ns.any")); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("ns.int = -20"), std::runtime_error); -#else - ASSERT_FALSE(runLua("ns.int = -20")); -#endif - - ASSERT_EQ(-10, variable("ns.int")); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("ns.any = {b = 2}"), std::runtime_error); -#else - ASSERT_FALSE(runLua("ns.any = {b = 2}")); -#endif - - ASSERT_EQ(any, variable("ns.any")); -} - -namespace { -template -struct Property -{ - static T value; -}; - -template -T Property::value; - -template -void setProperty(const T& value) -{ - Property::value = value; -} - -template -void setPropertyNoexcept(const T& value) noexcept -{ - Property::value = value; -} - -template -const T& getProperty() -{ - return Property::value; -} - -template -const T& getPropertyNoexcept() noexcept -{ - return Property::value; -} -} // namespace - -TEST_F(NamespaceTests, Properties) -{ - setProperty(-10); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW( - luabridge::getGlobalNamespace(L).addProperty("int", &getProperty, &setProperty), - std::logic_error); -#endif - - runLua("result = int"); - ASSERT_TRUE(result().isNil()); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("int", &getProperty, &setProperty) - .addProperty("int2", &getPropertyNoexcept, &setPropertyNoexcept) - .endNamespace(); - - ASSERT_EQ(-10, variable("ns.int")); - ASSERT_EQ(-10, variable("ns.int2")); - - runLua("ns.int = -20"); - ASSERT_EQ(-20, getProperty()); - - runLua("ns.int2 = -20"); - ASSERT_EQ(-20, getPropertyNoexcept()); -} - -TEST_F(NamespaceTests, ReadOnlyProperties) -{ - setProperty(-10); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(luabridge::getGlobalNamespace(L).addProperty("int", &getProperty), - std::logic_error); -#endif - - runLua("result = int"); - ASSERT_TRUE(result().isNil()); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("int", &getProperty) - .addProperty("int2", &getPropertyNoexcept) - .endNamespace(); - - ASSERT_EQ(-10, variable("ns.int")); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("ns.int = -20"), std::runtime_error); - ASSERT_THROW(runLua("ns.int2 = -20"), std::runtime_error); -#else - ASSERT_FALSE(runLua("ns.int = -20")); - ASSERT_FALSE(runLua("ns.int2 = -20")); -#endif - - ASSERT_EQ(-10, getProperty()); - ASSERT_EQ(-10, getPropertyNoexcept()); -} - -TEST_F(NamespaceTests, AddVariable) -{ - int int_ = -10; - auto any = luabridge::newTable(L); - any["a"] = 1; - - enum class A { a, b, c, d }; - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(luabridge::getGlobalNamespace(L).addVariable("int", &int_), std::logic_error); -#endif - - runLua("result = int"); - ASSERT_TRUE(result().isNil()); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addVariable("int", int_) - .addVariable("any", any) - .beginNamespace("A") - .addVariable("a", A::a) - .addVariable("b", A::b) - .addVariable("c", A::c) - .addVariable("d", A::d) - .endNamespace() - .endNamespace(); - - ASSERT_EQ(-10, variable("ns.int")); - ASSERT_EQ(any, variable("ns.any")); - - runLua("ns.A.a = 2; result = ns.A.a"); - ASSERT_EQ(A::c, static_cast(result())); - - runLua("ns.int = -20"); - ASSERT_EQ(-10, int_); - - runLua("result = ns.int"); - ASSERT_EQ(-20, result()); - - runLua("ns.any = {a = 42, b = 2}"); - ASSERT_TRUE(any.isTable()); - ASSERT_TRUE(any["b"].isNil()); - - runLua("result = ns.any.a"); - ASSERT_EQ(42, result()); -} - -namespace { -template -struct Storage -{ - static T value; -}; - -template -T Storage::value; - -template -int getDataC(lua_State* L) -{ - [[maybe_unused]] auto result = luabridge::Stack::push(L, Storage::value); - - return 1; -} - -template -int setDataC(lua_State* L) -{ - auto result = luabridge::Stack::get(L, -1); - if (! result) - luaL_error(L, "%s", result.error().message().c_str()); - - Storage::value = *result; - - return 0; -} -} // namespace - -TEST_F(NamespaceTests, NamespaceFromStack) -{ - // Create environment table - lua_newtable(L); - - luabridge::getNamespaceFromStack(L) - .addFunction("Function", [](int x) { return x; }); - - int tableReference = luabridge::luaL_ref(L, LUA_REGISTRYINDEX); - - // Load a script - std::string script = "result = Function (42)"; - auto success = luaL_loadstring(L, script.c_str()) == LUABRIDGE_LUA_OK; - ASSERT_TRUE(success); - - // Register - lua_rawgeti(L, LUA_REGISTRYINDEX, tableReference); - - // Set as up value -#if LUA_VERSION_NUM < 502 - ASSERT_EQ(1, lua_setfenv(L, -2)); -#else - auto newUpvalueName = lua_setupvalue(L, -2, 1); - - ASSERT_TRUE(newUpvalueName); - ASSERT_STREQ("_ENV", newUpvalueName); - - if (! newUpvalueName) - lua_pop(L, -1); -#endif - - success = lua_pcall(L, 0, 0, 0) == LUABRIDGE_LUA_OK; - ASSERT_TRUE(success); - - lua_rawgeti(L, LUA_REGISTRYINDEX, tableReference); - auto resultTable = luabridge::LuaRef::fromStack(L); - - ASSERT_TRUE(resultTable.isTable()); - ASSERT_TRUE(resultTable["result"].cast()); - ASSERT_EQ(42, resultTable["result"].cast()); -} - - -TEST_F(NamespaceTests, Properties_ProxyCFunctions) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("value", &getDataC, &setDataC) - .endNamespace(); - - Storage::value = 1; - runLua("ns.value = 2"); - ASSERT_EQ(2, Storage::value); - - Storage::value = 3; - runLua("result = ns.value"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(3, result()); -} - -TEST_F(NamespaceTests, Properties_ProxyCFunctions_ReadOnly) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("value", &getDataC) - .endNamespace(); - - Storage::value = 1; - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("ns.value = 2"), std::exception); -#else - ASSERT_FALSE(runLua("ns.value = 2")); -#endif - - ASSERT_EQ(1, Storage::value); - - Storage::value = 3; - runLua("result = ns.value"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(3, result()); -} - -namespace { -struct Class {}; -} // namespace - -TEST_F(NamespaceTests, LuaStackIntegrity) -{ - const int initialStackIndex = 0; - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... - - { - auto ns2 = - luabridge::getGlobalNamespace(L).beginNamespace("namespace").beginNamespace("ns2"); - - EXPECT_EQ( - initialStackIndex + 3, - lua_gettop(L)); // Stack: ..., global namespace table (gns), namespace table (ns), ns2 - - ns2.endNamespace(); // Stack: ... - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... - } - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... - - { - auto globalNs = luabridge::getGlobalNamespace(L); - EXPECT_EQ(initialStackIndex + 1, lua_gettop(L)); // Stack: ..., gns - - { - auto ns = luabridge::getGlobalNamespace(L).beginNamespace("namespace"); - // both globalNs an ns are active - EXPECT_EQ(initialStackIndex + 3, lua_gettop(L)); // Stack: ..., gns, gns, ns - } - - EXPECT_EQ(initialStackIndex + 1, lua_gettop(L)); // Stack: ..., gns - - { - auto ns = globalNs.beginNamespace("namespace"); - // globalNs became inactive - EXPECT_EQ(initialStackIndex + 2, lua_gettop(L)); // Stack: ..., gns, ns - } - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(globalNs.beginNamespace("namespace"), std::exception); - EXPECT_THROW(globalNs.beginClass("Class"), std::exception); -#endif - } - - { - auto globalNs = luabridge::getGlobalNamespace(L).beginNamespace("namespace").endNamespace(); - // globalNs is active - EXPECT_EQ(initialStackIndex + 1, lua_gettop(L)); // Stack: ..., gns - } - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // StacK: ... - - { - auto cls = - luabridge::getGlobalNamespace(L).beginNamespace("namespace").beginClass("Class"); - EXPECT_EQ(initialStackIndex + 5, lua_gettop(L)); // Stack: ..., gns, ns, const table, class table, static table - - { - auto ns = cls.endClass(); - EXPECT_EQ(initialStackIndex + 2, lua_gettop(L)); // Stack: ..., gns, ns - } - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... - } - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // StacK: ... - - // Test class continuation - { - auto cls = - luabridge::getGlobalNamespace(L).beginNamespace("namespace").beginClass("Class"); - EXPECT_EQ(initialStackIndex + 5, lua_gettop(L)); // Stack: ..., gns, ns, const table, class table, static table - } - - EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... -} - -TEST_F(NamespaceTests, LuaStackAdditionalIntegrity) -{ - [[maybe_unused]] auto globalNs = luabridge::getGlobalNamespace(L); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(globalNs.endNamespace(), std::exception); -#endif -} - -namespace { -template -T Function(T param) -{ - return param; -} - -int LuaFunction(lua_State* L) -{ - lua_pushnumber(L, 42); - return 1; -} -} // namespace - -TEST_F(NamespaceTests, Functions) -{ - luabridge::getGlobalNamespace(L).addFunction("Function", &Function); - - runLua("result = Function (3.14)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(3.14, result()); -} - -TEST_F(NamespaceTests, LuaFunctions) -{ - luabridge::getGlobalNamespace(L).addFunction("Function", &LuaFunction); - - runLua("result = Function ()"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); -} - -TEST_F(NamespaceTests, StdFunctions) -{ - luabridge::getGlobalNamespace(L).addFunction("Function", std::function(&Function)); - - runLua("result = Function (12)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(12, result()); -} - -TEST_F(NamespaceTests, CapturingLambdas) -{ - int x = 30; - - luabridge::getGlobalNamespace(L).addFunction("Function", [x](int v) -> int { return x + Function(v); }); - - runLua("result = Function (12)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(42, result()); -} - -#ifdef _M_IX86 // Windows 32bit only - -namespace { -int __stdcall StdCall(int i) -{ - return i + 10; -} -} // namespace - -TEST_F(NamespaceTests, StdCallFunctions) -{ - luabridge::getGlobalNamespace(L).addFunction("StdCall", &StdCall); - - runLua("result = StdCall (2)"); - ASSERT_TRUE(result().isNumber()); - ASSERT_EQ(12, result()); -} - -#endif // _M_IX86 diff --git a/LuaBridge3/Tests/Source/ObjCTests.mm b/LuaBridge3/Tests/Source/ObjCTests.mm deleted file mode 100644 index 2b6b5f8..0000000 --- a/LuaBridge3/Tests/Source/ObjCTests.mm +++ /dev/null @@ -1,8 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "Lua/LuaLibrary.h" - -// This is here just to sanity check we can compile on objc -#include "LuaBridge/LuaBridge.h" diff --git a/LuaBridge3/Tests/Source/OptionalTests.cpp b/LuaBridge3/Tests/Source/OptionalTests.cpp deleted file mode 100644 index 21363b9..0000000 --- a/LuaBridge3/Tests/Source/OptionalTests.cpp +++ /dev/null @@ -1,346 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2021, Stefan Frings -// Copyright 2020, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "TestBase.h" -#include "TestTypes.h" - -#include - -namespace { -template -std::string toLuaSrcString(const T& value) -{ - return std::to_string(value); -} - -template<> -std::string toLuaSrcString(const bool& value) -{ - return value ? "true" : "false"; -} - -template<> -std::string toLuaSrcString(const char& value) -{ - return "'" + std::string(&value, 1) + "'"; -} - -template<> -std::string toLuaSrcString(const char* const& value) -{ - return "'" + std::string(value) + "'"; -} - -template<> -std::string toLuaSrcString(const std::string& value) -{ - return "'" + value + "'"; -} - -template<> -std::string toLuaSrcString(const std::string_view& value) -{ - return toLuaSrcString(std::string(value)); -} - -template -std::optional optCast(luabridge::LuaRef const& ref) -{ - // NOTE cast to std::optional: https://stackoverflow.com/a/45865802 - return ref.unsafe_cast>(); -} - -template -void checkEquals(T expected, T actual) -{ - using U = std::decay_t; - - if constexpr (std::is_same_v) - { - ASSERT_FLOAT_EQ(expected, actual); - } - else if constexpr (std::is_same_v || std::is_same_v) - { - ASSERT_DOUBLE_EQ(expected, actual); - } - else if constexpr (std::is_same_v) - { - ASSERT_STREQ(expected, actual); - } - else - { - ASSERT_EQ(expected, actual); - } -} -} // namespace - -template -struct OptionalTest : TestBase -{ -}; - -TYPED_TEST_SUITE_P(OptionalTest); - -TYPED_TEST_P(OptionalTest, LuaRefPresent) -{ - using Traits = TypeTraits; - - for (TypeParam const& value : Traits::values()) - { - std::string const luaSrc = "result = " + toLuaSrcString(value); - - SCOPED_TRACE(luaSrc); - this->runLua(luaSrc); - - std::optional const actual = optCast(this->result()); - ASSERT_TRUE(actual.has_value()); - - checkEquals(value, actual.value()); - } -} - -TYPED_TEST_P(OptionalTest, LuaRefNotPresent) -{ - this->runLua("result = nil"); - - std::optional const actual = optCast(this->result()); - ASSERT_FALSE(actual.has_value()); -} - -TYPED_TEST_P(OptionalTest, LuaRefIsInstancePresent) -{ - using Traits = TypeTraits; - - for (TypeParam const& value : Traits::values()) - { - std::string const luaSrc = "result = " + toLuaSrcString(value); - - SCOPED_TRACE(luaSrc); - this->runLua(luaSrc); - - luabridge::LuaRef const actualRef = this->result(); - ASSERT_TRUE(actualRef.isInstance>()); - - // if isInstance returns true a cast without issues is possible - [[maybe_unused]] std::optional const actual = optCast(actualRef); - } -} - -TYPED_TEST_P(OptionalTest, LuaRefIsInstancePresentWrongType) -{ - this->runLua("function func() end; result = func"); - - luabridge::LuaRef const actualRef = this->result(); - ASSERT_FALSE(actualRef.isInstance>()); -} - -TYPED_TEST_P(OptionalTest, LuaRefIsInstanceNotPresent) -{ - this->runLua("result = nil"); - - luabridge::LuaRef const actualRef = this->result(); - ASSERT_TRUE(actualRef.isInstance>()); - - // if isInstance returns true a cast without issues is possible - [[maybe_unused]] std::optional const actual = optCast(actualRef); -} - -REGISTER_TYPED_TEST_SUITE_P(OptionalTest, - LuaRefPresent, - LuaRefNotPresent, - LuaRefIsInstancePresent, - LuaRefIsInstancePresentWrongType, - LuaRefIsInstanceNotPresent); - -INSTANTIATE_TYPED_TEST_SUITE_P(OptionalTest, OptionalTest, TestTypes); - -namespace { -struct Data -{ - explicit Data(int i) : i(i) {} - - int i; -}; - -bool operator==(Data const& lhs, Data const& rhs) -{ - return lhs.i == rhs.i; -} - -template -bool operator==(std::optional const& lhs, std::optional const& rhs) -{ - if (lhs.has_value() != rhs.has_value()) - { - return false; - } - - if (lhs.has_value()) - { - assert(rhs.has_value()); - return lhs.value() == rhs.value(); - } - - assert(!lhs.has_value()); - assert(!rhs.has_value()); - return true; -} - -std::optional processValue(std::optional const& data) -{ - return data; -} - -std::optional processPointer(std::optional const& data) -{ - std::optional result; - if (data) - { - result = **data; - } - return result; -} -} // namespace - -struct OptionalTests : TestBase -{ -}; - -template -void testPassFromLua(OptionalTests const& fixture, - std::string const& functionName, - std::string const& valueString, - std::optional const expected) -{ - fixture.resetResult(); - - std::string const luaSrc = "result = " + functionName + "(" + valueString + ")"; - - SCOPED_TRACE(luaSrc); - fixture.runLua(luaSrc); - - std::optional const actual = optCast(fixture.result()); - ASSERT_EQ(expected, actual); -} - -TEST_F(OptionalTests, PassFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Data") - .addConstructor() - .endClass() - .addFunction("processValue", &processValue) - .addFunction("processPointer", &processPointer); - - testPassFromLua(*this, "processValue", "Data(-1)", Data(-1)); - testPassFromLua(*this, "processValue", "Data(2)", Data(2)); - testPassFromLua(*this, "processValue", "nil", std::nullopt); - - testPassFromLua(*this, "processPointer", "Data(-1)", Data(-1)); - testPassFromLua(*this, "processPointer", "Data(2)", Data(2)); - testPassFromLua(*this, "processPointer", "nil", std::nullopt); -} - -TEST_F(OptionalTests, PassToFunction) -{ - runLua("function foo (opt) " - " result = opt " - "end"); - - auto foo = luabridge::getGlobal(L, "foo"); - - { - resetResult(); - - std::optional lvalue{ 10 }; - foo(lvalue); - EXPECT_FALSE(result().isNil()); - EXPECT_EQ(lvalue, result>()); - - resetResult(); - - const std::optional constLvalue = lvalue; - foo(constLvalue); - EXPECT_FALSE(result().isNil()); - EXPECT_EQ(lvalue, result>()); - } - - { - resetResult(); - - std::optional lvalue; - foo(lvalue); - EXPECT_TRUE(result().isNil()); - EXPECT_FALSE(result>()); - - resetResult(); - - const std::optional constLvalue = lvalue; - foo(constLvalue); - EXPECT_TRUE(result().isNil()); - EXPECT_FALSE(result>()); - } -} - -TEST_F(OptionalTests, FromCppApi) -{ - struct OptionalClass - { - OptionalClass() = default; - - void testOptionalAsArg(std::optional v) - { - x = v; - } - - std::optional testOptionalAsReturn() - { - return "abcdef"; - } - - std::optional x; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("OptionalClass") - .addConstructor() - .addFunction("testOptionalAsArg", &OptionalClass::testOptionalAsArg) - .addFunction("testOptionalAsReturn", &OptionalClass::testOptionalAsReturn) - .endClass(); - - resetResult(); - runLua("result = OptionalClass () result:testOptionalAsArg (1337)"); - - EXPECT_TRUE(result().isUserdata()); - EXPECT_EQ(1337, *result().x); - - resetResult(); - runLua("x = OptionalClass () result = x:testOptionalAsReturn ()"); - - EXPECT_FALSE(result().isNil()); - EXPECT_EQ("abcdef", *result>()); -} - -#if !LUABRIDGE_HAS_EXCEPTIONS -TEST_F(OptionalTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) -{ - class Unregistered {}; - - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::optional v = Unregistered{}; - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); -} -#endif diff --git a/LuaBridge3/Tests/Source/OverloadTests.cpp b/LuaBridge3/Tests/Source/OverloadTests.cpp deleted file mode 100644 index 9132990..0000000 --- a/LuaBridge3/Tests/Source/OverloadTests.cpp +++ /dev/null @@ -1,873 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include - -#include -#include -#include -#include - -struct OverloadTests : TestBase -{ -}; - -TEST_F(OverloadTests, NoMatchingArity) -{ - int x = 100; - - luabridge::getGlobalNamespace(L) - .addFunction("test", - [x](int v) -> int { - return v + x; - }, - [x](std::string v) -> int { - return v.size() ? int(v[0]) : x; - }); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = test ()")); - EXPECT_ANY_THROW(runLua("result = test (255, 255)")); - EXPECT_ANY_THROW(runLua("result = test ('', '')")); - EXPECT_ANY_THROW(runLua("result = test ('0', '0')")); -#else - EXPECT_FALSE(runLua("result = test ()")); - EXPECT_FALSE(runLua("result = test (255, 255)")); - EXPECT_FALSE(runLua("result = test ('', '')")); - EXPECT_FALSE(runLua("result = test ('0', '0')")); -#endif -} - -TEST_F(OverloadTests, NoMatchingArityWithState) -{ - int x = 100; - - luabridge::getGlobalNamespace(L) - .addFunction("test", - [x](int v, lua_State*) -> int { - return v + x; - }, - [x](std::string v, lua_State*) -> int { - return v.size() ? int(v[0]) : x; - }); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = test ()")); - EXPECT_ANY_THROW(runLua("result = test (255, 255)")); - EXPECT_ANY_THROW(runLua("result = test ('', '')")); - EXPECT_ANY_THROW(runLua("result = test ('0', '0')")); -#else - EXPECT_FALSE(runLua("result = test ()")); - EXPECT_FALSE(runLua("result = test (255, 255)")); - EXPECT_FALSE(runLua("result = test ('', '')")); - EXPECT_FALSE(runLua("result = test ('0', '0')")); -#endif -} - -TEST_F(OverloadTests, SingleArgumentOverloads) -{ - int x = 100; - - luabridge::getGlobalNamespace(L) - .addFunction("test", - [x](int v) -> int { - return v + x; - }, - [x](std::string v) -> int { - return v.size() ? int(v[0]) : x; - }); - - runLua("result = test (255)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(355, result()); - - runLua("result = test ('')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(100, result()); - - runLua("result = test ('0')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(48, result()); -} - -TEST_F(OverloadTests, SingleArgumentOverloadsWithState) -{ - int x = 100; - - luabridge::getGlobalNamespace(L) - .addFunction("test", - [x](int v, lua_State*) -> int { - return v + x; - }, - [x](std::string v, lua_State*) -> int { - return v.size() ? int(v[0]) : x; - }); - - runLua("result = test (255)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(355, result()); - - runLua("result = test ('')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(100, result()); - - runLua("result = test ('0')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(48, result()); -} - -TEST_F(OverloadTests, IntegerTypeFallbackOverloads) -{ - luabridge::getGlobalNamespace(L) - .addFunction("test", - [](int8_t v) -> int { - return 1; - }, - [](int16_t v) -> int { - return 2; - }, - [](int32_t v) -> int { - return 3; - }, - [](int64_t v) -> int { - return 4; - }); - - runLua("result = test (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("result = test (127)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("result = test (128)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = test (32767)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = test (32768)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("result = test (2147483647)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - if constexpr (sizeof(lua_Integer) >= sizeof(int64_t)) - { - runLua("result = test (2147483648)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); - } -} - -TEST_F(OverloadTests, UnregisteredClass) -{ - struct Unregistered {}; - - luabridge::getGlobalNamespace(L) - .addFunction("testUnregistered", - [](Unregistered*) { return 1; }, - [](int) { return 2; }); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = testUnregistered ({})")); -#else - EXPECT_FALSE(runLua("result = testUnregistered ({})")); -#endif - - runLua("result = testUnregistered (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); -} - -TEST_F(OverloadTests, BaseAndDerivedClassOverloads) -{ - struct Base - { - virtual ~Base() {} - - virtual int test1() { return 1; } - virtual int test2(int) { return 2; } - - int test3() { return 3; } - int test4(int) { return 4; } - }; - - struct Derived : Base - { - int test1() override { return 5; } - int test2(int) override { return 6; } - - int test5() { return 7; } - int test6(int) { return 8; } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Base") - .addConstructor() - .addFunction("testx", &Base::test1, &Base::test2) - .addFunction("testy", &Base::test3, &Base::test4) - .endClass() - .deriveClass("Derived") - .addConstructor() - .addFunction("testx", &Derived::test1, &Derived::test2) - .addFunction("testz", &Derived::test5, &Derived::test6) - .endClass() - .addFunction("testBase", - [](Base* b) { return b->test3(); }, - [](Derived* d) { return d->test5(); }) - .addFunction("testDerived", - [](Derived* d) { return d->test5(); }, - [](Base* b) { return b->test3(); }); - - runLua("b = Base(); result = b:testx ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("b = Base(); result = b:testx (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("b = Derived(); result = b:testx ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(5, result()); - - runLua("b = Derived(); result = b:testx (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(6, result()); - - runLua("b = Derived(); result = b:testy ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("b = Derived(); result = b:testy (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); - - runLua("b = Base(); result = testBase (b)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("b = Derived(); result = testBase (b)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("b = Base(); result = testDerived (b)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("b = Derived(); result = testDerived (b)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(7, result()); -} - -TEST_F(OverloadTests, NoMatchingArityClass) -{ - int x = 100; - - struct X {}; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addFunction("test", - [x](X*, int v) -> int { - return v + x; - }, - [x](X*, std::string v) -> int { - return v.size() ? int(v[0]) : x; - }) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = y:test ()")); - EXPECT_ANY_THROW(runLua("result = y:test (255, 255)")); - EXPECT_ANY_THROW(runLua("result = y:test ('', '')")); - EXPECT_ANY_THROW(runLua("result = y:test ('0', '0')")); -#else - EXPECT_FALSE(runLua("result = y:test ()")); - EXPECT_FALSE(runLua("result = y:test (255, 255)")); - EXPECT_FALSE(runLua("result = y:test ('', '')")); - EXPECT_FALSE(runLua("result = y:test ('0', '0')")); -#endif -} - -TEST_F(OverloadTests, NoMatchingArityStaticFunctionClass) -{ - int x = 100; - - struct X {}; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addStaticFunction("test", - [x](int v) -> int { - return v + x; - }, - [x](std::string v) -> int { - return v.size() ? int(v[0]) : x; - }) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = X.test ()")); - EXPECT_ANY_THROW(runLua("result = X.test (255, 255)")); - EXPECT_ANY_THROW(runLua("result = X.test ('', '')")); - EXPECT_ANY_THROW(runLua("result = X.test ('0', '0')")); -#else - EXPECT_FALSE(runLua("result = X.test ()")); - EXPECT_FALSE(runLua("result = X.test (255, 255)")); - EXPECT_FALSE(runLua("result = X.test ('', '')")); - EXPECT_FALSE(runLua("result = X.test ('0', '0')")); -#endif -} - -TEST_F(OverloadTests, NoMatchingArityWithStateClass) -{ - int x = 100; - - struct X {}; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addFunction("test", - [x](X*, int v, lua_State*) -> int { - return v + x; - }, - [x](X*, std::string v, lua_State*) -> int { - return v.size() ? int(v[0]) : x; - }) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = y:test ()")); - EXPECT_ANY_THROW(runLua("result = y:test (255, 255)")); - EXPECT_ANY_THROW(runLua("result = y:test ('', '')")); - EXPECT_ANY_THROW(runLua("result = y:test ('0', '0')")); -#else - EXPECT_FALSE(runLua("result = y:test ()")); - EXPECT_FALSE(runLua("result = y:test (255, 255)")); - EXPECT_FALSE(runLua("result = y:test ('', '')")); - EXPECT_FALSE(runLua("result = y:test ('0', '0')")); -#endif -} - -TEST_F(OverloadTests, NoMatchingArityWithStateStaticFunctionClass) -{ - int x = 100; - - struct X {}; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addStaticFunction("test", - [x](int v, lua_State*) -> int { - return v + x; - }, - [x](std::string v, lua_State*) -> int { - return v.size() ? int(v[0]) : x; - }) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(runLua("result = X.test ()")); - EXPECT_ANY_THROW(runLua("result = X.test (255, 255)")); - EXPECT_ANY_THROW(runLua("result = X.test ('', '')")); - EXPECT_ANY_THROW(runLua("result = X.test ('0', '0')")); -#else - EXPECT_FALSE(runLua("result = X.test ()")); - EXPECT_FALSE(runLua("result = X.test (255, 255)")); - EXPECT_FALSE(runLua("result = X.test ('', '')")); - EXPECT_FALSE(runLua("result = X.test ('0', '0')")); -#endif -} - -TEST_F(OverloadTests, MixedFunctionTypesClass) -{ - int x = 100; - - struct X - { - int test() - { - return 42; - } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addFunction("test", - [x](X*, int v, lua_State*) -> int { - return v + x; - }, - +[](X*, std::string v, lua_State*) -> int { - return v.size() ? int(v[0]) : 1337; - }, - &X::test) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - - runLua("result = y:test ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(42, result()); - - runLua("result = y:test (255)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(355, result()); - - runLua("result = y:test ('')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1337, result()); - - runLua("result = y:test ('0')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(48, result()); -} - -TEST_F(OverloadTests, MixedStaticFunctionTypesClass) -{ - int x = 100; - - struct X - { - static int test() - { - return 42; - } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addStaticFunction("test", - [x](int v, lua_State*) -> int { - return v + x; - }, - +[](std::string v, lua_State*) -> int { - return v.size() ? int(v[0]) : 1337; - }, - &X::test) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - - runLua("result = X.test ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(42, result()); - - runLua("result = X.test (255)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(355, result()); - - runLua("result = X.test ('')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1337, result()); - - runLua("result = X.test ('0')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(48, result()); -} - -TEST_F(OverloadTests, OverloadOperatorClass) -{ - struct X - { - X(int x) : value(x) {} - - X operator*(const X& rhs) const - { - return { value * rhs.value }; - } - - X operator*(int x) const - { - return { value * x }; - } - - int value = 0; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addConstructor() - .addProperty("value", &X::value) - .addFunction("__mul", - luabridge::overload(&X::operator*), - luabridge::overload(&X::operator*)) - .endClass(); - - runLua("x1 = X(2); x2 = X(3); result = x1 * x2"); - ASSERT_TRUE(result().isUserdata()); - auto result1 = result(); - EXPECT_EQ(6, result1.value); - - runLua("x1 = X(2); result = x1 * 3"); - ASSERT_TRUE(result().isUserdata()); - auto result2 = result(); - EXPECT_EQ(6, result2.value); -} - -TEST_F(OverloadTests, LuaCFunctionFallback) -{ - struct X - { - int test(std::string) - { - return 3; - } - - int testLua(lua_State* L) - { - lua_pushnumber(L, 4); - return 1; - } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addFunction("test", - [](X*, char v, lua_State*) -> int { - return 1; - }, - +[](X*, int v) -> int { - return 2; - }, - &X::test, - &X::testLua) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - - runLua("result = y:test ('2')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("result = y:test (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = y:test ('123456')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("result = y:test ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); -} - -TEST_F(OverloadTests, LuaCStaticFunctionFallback) -{ - struct X - { - static int test(std::string) - { - return 3; - } - - static int testLua(lua_State* L) - { - lua_pushnumber(L, 4); - return 1; - } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addStaticFunction("test", - [](char v, lua_State*) -> int { - return 1; - }, - +[](int v) -> int { - return 2; - }, - &X::test, - &X::testLua) - .endClass(); - - X y; - luabridge::setGlobal(L, &y, "y"); - - runLua("result = X.test ('2')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("result = X.test (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = X.test ('123456')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("result = X.test ()"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); -} - -TEST_F(OverloadTests, ConstAndNonConstOverloadClass) -{ - struct X - { - int test1(std::string) { return 1; } - int test1(std::string) const { return 2; } - int test2(std::string) { return 3; } - int test2(std::string, int) { return 4; } - int test3(std::string) const { return 5; } - int test3(std::string, int) const { return 6; } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addFunction("test1", - luabridge::nonConstOverload(&X::test1), - luabridge::constOverload(&X::test1)) - .addFunction("test2", - luabridge::overload(&X::test2), - luabridge::overload(&X::test2)) - .addFunction("test3", - luabridge::constOverload(&X::test3), - luabridge::constOverload(&X::test3)) - .endClass(); - - // Non const object - X x; - luabridge::setGlobal(L, &x, "x"); - - runLua("result = x:test1 ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("result = x.test2"); - EXPECT_FALSE(result().isNil()); - runLua("result = x:test2 ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("result = x.test3"); - EXPECT_FALSE(result().isNil()); - runLua("result = x:test3 ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(5, result()); - runLua("result = x:test3 ('abc', 1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(6, result()); - - // Const object - const X y; - luabridge::setGlobal(L, &y, "y"); - - runLua("result = y:test1 ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = y.test2"); - EXPECT_TRUE(result().isNil()); -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_ANY_THROW(runLua("result = y:test2 ('abc')")); - ASSERT_ANY_THROW(runLua("result = y:test2 ('abc', 1)")); -#else - ASSERT_FALSE(runLua("result = y:test2 ('abc')")); - ASSERT_FALSE(runLua("result = y:test2 ('abc', 1)")); -#endif - - runLua("result = y.test3"); - EXPECT_FALSE(result().isNil()); - runLua("result = y:test3 ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(5, result()); - runLua("result = y:test3 ('abc', 1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(6, result()); -} - -TEST_F(OverloadTests, LuaCFunctionArityCheck) -{ - struct X - { - int test1(int) - { - return 1; - } - - int test2(lua_State* L) - { - lua_pushinteger(L, 2); - return 1; - } - - static int test3(int) - { - return 3; - } - - static int test4(lua_State* L) - { - lua_pushinteger(L, 4); - return 1; - } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addFunction("test_a", &X::test2, &X::test1) - .addFunction("test_b", &X::test1, &X::test2) - .addStaticFunction("test_c", &X::test4, &X::test3) - .addStaticFunction("test_d", &X::test3, &X::test4) - .endClass(); - - X x; - luabridge::setGlobal(L, &x, "x"); - - // Non-static - runLua("result = x:test_a ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = x:test_a (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = x:test_a (1, 2, 3)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = x:test_b ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = x:test_b (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("result = x:test_b (1, 2, 3)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - // Static - runLua("result = X.test_c ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); - - runLua("result = X.test_c (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); - - runLua("result = X.test_c (1, 2, 3)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); - - runLua("result = X.test_d ('abc')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); - - runLua("result = X.test_d (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(3, result()); - - runLua("result = X.test_d (1, 2, 3)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(4, result()); -} - -TEST_F(OverloadTests, ConstructorOverloading) -{ - struct X - { - X(int x) - : value(x) - { - } - - X(int x, int y) - : value(x + y) - { - } - - int value = 0; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("X") - .addConstructor( - [](void* ptr, int x) { return new (ptr) X(x); }, - [](void* ptr, int x, int y) { return new (ptr) X(x, y); }) - .addProperty("value", &X::value) - .endClass() - .beginClass("Y") - .addConstructor() - .addProperty("value", &X::value) - .endClass(); - - runLua("x = X(1); result = x.value"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("x = X(1, 10); result = x.value"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(11, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_ANY_THROW(runLua("x = X(1, 10, 100); result = x.value")); -#else - ASSERT_FALSE(runLua("x = X(1, 10, 100); result = x.value")); -#endif - - runLua("y = Y(1); result = y.value"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(1, result()); - - runLua("y = Y(1, 10); result = y.value"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(11, result()); - -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_ANY_THROW(runLua("y = Y(1, 10, 100); result = y.value")); -#else - ASSERT_FALSE(runLua("y = Y(1, 10, 100); result = y.value")); -#endif -} - -TEST_F(OverloadTests, FunctionFailureVsArgumentConversion) -{ - luabridge::getGlobalNamespace(L) - .addFunction("test", - +[](int, lua_State* L) -> int { - lua_pushinteger(L, 1); - luaL_error(L, "failure"); - return 1; - }, - +[](lua_State* L) -> int { - lua_pushinteger(L, 2); - return 1; - }); - - runLua("result = test ('abcdefg')"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); - - runLua("result = test (1)"); - ASSERT_TRUE(result().isNumber()); - EXPECT_EQ(2, result()); -} diff --git a/LuaBridge3/Tests/Source/PairTests.cpp b/LuaBridge3/Tests/Source/PairTests.cpp deleted file mode 100644 index 028973a..0000000 --- a/LuaBridge3/Tests/Source/PairTests.cpp +++ /dev/null @@ -1,157 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// Copyright 2022, Stefan Frings -// SPDX-License-Identifier: MIT - -#include "TestBase.h" -#include "TestTypes.h" - -template -struct PairTest : TestBase -{ -}; - -TYPED_TEST_SUITE_P(PairTest); - -namespace { -template -std::string toLuaSrcString(T const& value) -{ - return std::to_string(value); -} - -template <> -std::string toLuaSrcString(bool const& value) -{ - return value ? "true" : "false"; -} - -template <> -std::string toLuaSrcString(std::string const& value) -{ - return "'" + value + "'"; -} -} // namespace - -TYPED_TEST_P(PairTest, push) -{ - using Pair = TypeParam; - using T1 = typename Pair::first_type; - using T2 = typename Pair::second_type; - using Traits1 = TypeTraits; - using Traits2 = TypeTraits; - - assert(Traits1::values().size() == Traits2::values().size()); - - for (std::size_t n = 0; n < Traits1::values().size(); ++n) - { - T1 const value1 = Traits1::values()[n]; - T2 const value2 = Traits2::values()[n]; - - Pair const data(value1, value2); - - this->runLua("result = nil; function func(data) result = data end"); - - luabridge::LuaRef func = luabridge::getGlobal(this->L, "func"); - func(data); - - Pair const actual = this->result(); - ASSERT_EQ(actual, data); - } -} - -TYPED_TEST_P(PairTest, get) -{ - using Pair = TypeParam; - using T1 = typename Pair::first_type; - using T2 = typename Pair::second_type; - using Traits1 = TypeTraits; - using Traits2 = TypeTraits; - - assert(Traits1::values().size() == Traits2::values().size()); - - for (std::size_t n = 0; n < Traits1::values().size(); ++n) - { - T1 const value1 = Traits1::values()[n]; - T2 const value2 = Traits2::values()[n]; - - std::string const luaSrc = - "result = {" + toLuaSrcString(value1) + ", " + toLuaSrcString(value2) + "}"; - - SCOPED_TRACE(luaSrc); - this->runLua(luaSrc); - - Pair const actual = this->result(); - ASSERT_EQ(actual.first, value1); - ASSERT_EQ(actual.second, value2); - } -} - -TYPED_TEST_P(PairTest, isInstance) -{ - using Pair = TypeParam; - using T1 = typename Pair::first_type; - using T2 = typename Pair::second_type; - using Traits1 = TypeTraits; - using Traits2 = TypeTraits; - - assert(Traits1::values().size() == Traits2::values().size()); - - for (std::size_t n = 0; n < Traits1::values().size(); ++n) - { - T1 const value1 = Traits1::values()[n]; - T2 const value2 = Traits2::values()[n]; - - std::string const luaSrc = - "result = {" + toLuaSrcString(value1) + ", " + toLuaSrcString(value2) + "}"; - - SCOPED_TRACE(luaSrc); - this->runLua(luaSrc); - - luabridge::LuaRef ref = luabridge::getGlobal(this->L, "result"); - ASSERT_TRUE(ref.isInstance()); - ASSERT_FALSE(ref.isInstance()); - ASSERT_FALSE(ref.isInstance()); - } -} - -REGISTER_TYPED_TEST_SUITE_P(PairTest, push, get, isInstance); - -using TestTypesCartesianProduct = ::testing::Types, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair, - std::pair>; - -INSTANTIATE_TYPED_TEST_SUITE_P(PairTest, PairTest, TestTypesCartesianProduct); diff --git a/LuaBridge3/Tests/Source/PerformanceTests.cpp b/LuaBridge3/Tests/Source/PerformanceTests.cpp deleted file mode 100644 index 3bd53f6..0000000 --- a/LuaBridge3/Tests/Source/PerformanceTests.cpp +++ /dev/null @@ -1,131 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2020, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include -#include -#include -#include -#include -#include - -using namespace std; -using namespace luabridge; - -namespace { - -//------------------------------------------------------------------------------ -/** - Simple stopwatch for measuring elapsed time. -*/ -class Stopwatch -{ -private: - clock_t m_start; - -public: - Stopwatch() { start(); } - - void start() { m_start = clock(); } - - double getElapsedSeconds() - { - clock_t now; - - now = clock(); - - return (double(now - m_start)) / CLOCKS_PER_SEC; - } -}; - -//------------------------------------------------------------------------------ -/** - Classes used for performance tests. -*/ - -struct A -{ - A() : data(0), prop(0) {} - - virtual ~A() = default; - - void mf1() {} - - void mf2(A*) {} - - void mf3(A&) {} - - virtual void vf1() {} - - int data; - - int prop; - int getprop() const { return prop; } - void setprop(int v) { prop = v; } -}; - -//------------------------------------------------------------------------------ - -void addToState(lua_State* L) -{ - getGlobalNamespace(L) - .beginClass("A") - .addConstructor() - .addFunction("mf1", &A::mf1) - .addFunction("mf2", &A::mf2) - .addFunction("mf3", &A::mf3) - .addFunction("vf1", &A::vf1) - .addProperty("data", &A::data) - .addProperty("prop", &A::getprop, &A::setprop) - .endClass(); -} - -void runTests(lua_State* L) -{ -#if 0 // TODO - cout.precision(4); - - int result; - - luaL_dostring(L, "a = A()"); - - int const trials = 5; - - for (int trial = 0; trial < trials; ++trial) - { - result = luaL_loadstring(L, "a:mf1 ()"); - if (result != 0) - lua_error(L); - - int const N = 1000000; - - Stopwatch sw; - - sw.start(); - for (int i = 0; i < N; ++i) - { - lua_pushvalue(L, -1); - lua_call(L, 0, 0); - } - - double const seconds = sw.getElapsedSeconds(); - - cout << "Elapsed time: " << seconds << endl; - } -#endif -} -} // namespace - -struct PerformanceTests : TestBase -{ -}; - -TEST_F(PerformanceTests, AllTests) -{ - addToState(L); - runTests(L); -} diff --git a/LuaBridge3/Tests/Source/RefCountedPtrTests.cpp b/LuaBridge3/Tests/Source/RefCountedPtrTests.cpp deleted file mode 100644 index 57094fb..0000000 --- a/LuaBridge3/Tests/Source/RefCountedPtrTests.cpp +++ /dev/null @@ -1,131 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include "LuaBridge/RefCountedObject.h" - -struct RefCountedPtrTests : TestBase -{ - template - T variable(const std::string& name) - { - runLua("result = " + name); - return result(); - } -}; - -namespace { - -struct RefCounted : luabridge::RefCountedObject -{ - explicit RefCounted(bool& deleted) : deleted(deleted) { deleted = false; } - - ~RefCounted() { deleted = true; } - - bool isDeleted() const { return deleted; } - - bool& deleted; -}; - -struct RefCountedStatic : luabridge::RefCountedObject -{ - explicit RefCountedStatic() { constructed = true; } - - ~RefCountedStatic() { deleted = true; } - - static bool constructed; - static bool deleted; -}; - -bool RefCountedStatic::constructed = false; -bool RefCountedStatic::deleted = false; - -} // namespace - -TEST_F(RefCountedPtrTests, Operators) -{ - bool deleted1 = false; - auto* raw_ptr1 = new RefCounted(deleted1); - luabridge::RefCountedObjectPtr ptr1(raw_ptr1); - - bool deleted2 = false; - auto* raw_ptr2 = new RefCounted(deleted2); - luabridge::RefCountedObjectPtr ptr2(raw_ptr2); - - ASSERT_TRUE(raw_ptr1 == ptr1.getObject()); - ASSERT_TRUE(ptr1.getObject() == raw_ptr1); -} - -TEST_F(RefCountedPtrTests, LastReferenceInLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Class") - .addProperty("deleted", &RefCounted::isDeleted) - .endClass(); - - bool deleted = false; - - luabridge::RefCountedObjectPtr object(new RefCounted(deleted)); - - luabridge::setGlobal(L, object, "object"); - runLua("result = object.deleted"); - ASSERT_EQ(true, result().isBool()); - ASSERT_EQ(false, result()); - - object = nullptr; - runLua("result = object.deleted"); - ASSERT_EQ(true, result().isBool()); - ASSERT_EQ(false, result()); - ASSERT_EQ(false, deleted); - - runLua("object = nil"); - lua_gc(L, LUA_GCCOLLECT, 1); - - ASSERT_EQ(true, deleted); -} - -TEST_F(RefCountedPtrTests, LastReferenceInCpp) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Class") - .addProperty("deleted", &RefCounted::isDeleted) - .endClass(); - - bool deleted = false; - - luabridge::RefCountedObjectPtr object(new RefCounted(deleted)); - - luabridge::setGlobal(L, object, "object"); - runLua("result = object.deleted"); - ASSERT_EQ(true, result().isBool()); - ASSERT_EQ(false, result()); - - runLua("object = nil"); - lua_gc(L, LUA_GCCOLLECT, 1); - ASSERT_EQ(false, deleted); - - object = nullptr; - ASSERT_EQ(true, deleted); -} - -TEST_F(RefCountedPtrTests, LastReferenceInstantiatedFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Class") - .addConstructor() - .endClass(); - - EXPECT_FALSE(RefCountedStatic::constructed); - EXPECT_FALSE(RefCountedStatic::deleted); - - runLua("local o = Class(); result = false"); - - EXPECT_TRUE(RefCountedStatic::constructed); - EXPECT_FALSE(RefCountedStatic::deleted); - - lua_gc(L, LUA_GCCOLLECT, 1); - - EXPECT_TRUE(RefCountedStatic::deleted); -} diff --git a/LuaBridge3/Tests/Source/ScopeGuardTests.cpp b/LuaBridge3/Tests/Source/ScopeGuardTests.cpp deleted file mode 100644 index c80cf4f..0000000 --- a/LuaBridge3/Tests/Source/ScopeGuardTests.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -TEST(ScopeGuardTests, ExecuteAtScopeExit) -{ - bool executed = false; - - { - const luabridge::detail::ScopeGuard sg([&] { executed = true; }); - luabridge::unused(sg); - } - - EXPECT_TRUE(executed); -} - -TEST(ScopeGuardTests, ResetDoesNotExecuteAtScopeExit) -{ - bool executed = false; - - { - luabridge::detail::ScopeGuard sg([&] { executed = true; }); - sg.reset(); - } - - EXPECT_FALSE(executed); -} diff --git a/LuaBridge3/Tests/Source/SharedCode.cpp b/LuaBridge3/Tests/Source/SharedCode.cpp deleted file mode 100644 index 873ee06..0000000 --- a/LuaBridge3/Tests/Source/SharedCode.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#include "SharedCode.h" -#include "LuaBridge/LuaBridge.h" - -namespace xyz { -ISharedClass::ISharedClass() = default; -ISharedClass::~ISharedClass() = default; - -class SharedClass : public ISharedClass -{ -public: - virtual int publicMethod(const std::string& s) const override - { - return std::stoi(s) + value; - } - -private: - int value = 42; -}; - -class LUABRIDGEDEMO_SHARED_API AnotherClass -{ -public: - AnotherClass() = default; - ~AnotherClass() = default; - - int publicMethod(const std::string& s) - { - return value = std::stoi(s); - } - - int publicConstMethod(const std::string& s) const - { - return value + std::stoi(s); - } - - int value = 30; -}; -} // namespace xyz - -extern "C" { -LUABRIDGEDEMO_SHARED_API xyz::ISharedClass* allocator() -{ - return new xyz::SharedClass(); -} - -LUABRIDGEDEMO_SHARED_API void deallocator(xyz::ISharedClass* ptr) -{ - delete ptr; -} - -LUABRIDGEDEMO_SHARED_API void registerAnotherClass(lua_State* L) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("dll") - .beginClass("AnotherClass") - .addConstructor() - .addFunction("publicMethod", &xyz::AnotherClass::publicMethod) - .addFunction("publicConstMethod", &xyz::AnotherClass::publicConstMethod) - .addProperty("value", &xyz::AnotherClass::value) - .endClass() - .endNamespace(); -} - -} // extern "C" diff --git a/LuaBridge3/Tests/Source/SharedCode.h b/LuaBridge3/Tests/Source/SharedCode.h deleted file mode 100644 index 59f6a64..0000000 --- a/LuaBridge3/Tests/Source/SharedCode.h +++ /dev/null @@ -1,32 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Lua/LuaLibrary.h" - -#if _WIN32 -#if LUABRIDGEDEMO_SHARED_EXPORT -#define LUABRIDGEDEMO_SHARED_API __declspec(dllexport) -#else -#define LUABRIDGEDEMO_SHARED_API __declspec(dllimport) -#endif -#else -#define LUABRIDGEDEMO_SHARED_API -#endif - -#include - -namespace xyz { - -class LUABRIDGEDEMO_SHARED_API ISharedClass -{ -public: - ISharedClass(); - virtual ~ISharedClass(); - - virtual int publicMethod(const std::string& s) const = 0; -}; - -} // namespace xyz diff --git a/LuaBridge3/Tests/Source/StackTests.cpp b/LuaBridge3/Tests/Source/StackTests.cpp deleted file mode 100644 index cd283e9..0000000 --- a/LuaBridge3/Tests/Source/StackTests.cpp +++ /dev/null @@ -1,2318 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2021, Lucio Asnaghi -// Copyright 2020, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -struct StackTests : TestBase -{ -}; - -TEST_F(StackTests, Void) -{ - ASSERT_TRUE(luabridge::Stack::push(L)); -} - -TEST_F(StackTests, VoidStackOverflow) -{ - exhaustStackSpace(); - - ASSERT_TRUE(luabridge::Stack::push(L)); -} - -TEST_F(StackTests, NullptrType) -{ - { - ASSERT_TRUE(luabridge::push(L, nullptr)); - } - - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(nullptr, result); - } -} - -TEST_F(StackTests, NullptrStackOverflow) -{ - exhaustStackSpace(); - - auto result = luabridge::Stack::push(L, nullptr); - ASSERT_FALSE(result); - EXPECT_FALSE(result.message().empty()); -} - -TEST_F(StackTests, NullptrInvalidType) -{ - (void)luabridge::Stack::push(L, 42); - - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); -} - -TEST_F(StackTests, LuaStateType) -{ - { - auto result = luabridge::get(L, -1); - EXPECT_TRUE(result); - EXPECT_EQ(L, *result); - } -} - -TEST_F(StackTests, LuaCFunctionType) -{ - lua_CFunction value = +[](lua_State*) { return 0; }; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, LuaCFunctionStackOverflow) -{ - exhaustStackSpace(); - - lua_CFunction value = +[](lua_State*) { return 0; }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, LuaCFunctionInvalidType) -{ - (void)luabridge::Stack::push(L, 42); - - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); -} - -TEST_F(StackTests, BoolType) -{ - bool value = true; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, BoolStackOverflow) -{ - exhaustStackSpace(); - - bool value = true; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, BoolInvalidType) -{ - { - (void)luabridge::Stack::push(L, 0); - auto result = luabridge::Stack::get(L, -1); - ASSERT_TRUE(result); - EXPECT_TRUE(*result); - } - - { - (void)luabridge::Stack::push(L, 42); - auto result = luabridge::Stack::get(L, -1); - ASSERT_TRUE(result); - EXPECT_TRUE(*result); - } - - { - (void)luabridge::Stack::push(L, nullptr); - auto result = luabridge::Stack::get(L, -1); - ASSERT_TRUE(result); - EXPECT_FALSE(*result); - } -} - -TEST_F(StackTests, CharType) -{ - char value = 'a'; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, CharStackOverflow) -{ - exhaustStackSpace(); - - char value = 'a'; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, CharInvalidType) -{ - { - (void)luabridge::Stack::push(L, 1024); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, ""); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, "123456"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, StdByteType) -{ - std::byte value{ 128 }; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, StdByteStackOVerflow) -{ - exhaustStackSpace(); - - std::byte value { 128 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, StdByteInvalidType) -{ - { - (void)luabridge::Stack::push(L, "1024"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 1024); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Int8Type) -{ - int8_t value = 127; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Int8StackOverflow) -{ - exhaustStackSpace(); - - int8_t value = 127; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Int8InvalidType) -{ - { - (void)luabridge::Stack::push(L, "1024"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 1024); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Int16Type) -{ - int16_t value = 32767; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Int16StackOverflow) -{ - exhaustStackSpace(); - - int16_t value = 32767; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Int16InvalidType) -{ - { - (void)luabridge::Stack::push(L, "1000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 1000000); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Int32Type) -{ - int32_t value = 2147483647; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Int32StackOverflow) -{ - exhaustStackSpace(); - - int32_t value = 2147483647; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Int32InvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 2147483648ll); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Int64Type) -{ - constexpr int64_t max_integral = static_cast(std::numeric_limits::max()); - - int64_t value = max_integral < 4294967296ll ? max_integral : 4294967296ll; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(uint32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Int64StackOverflow) -{ - exhaustStackSpace(); - - int64_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Int64InvalidType) -{ - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); -} - -#if 0 // defined(__SIZEOF_INT128__) -TEST_F(StackTests, Int128Type) -{ - constexpr __int128_t max_integral = static_cast<__int128_t>(std::numeric_limits::max()); - - __int128_t value = max_integral < 4294967296ll ? max_integral : 4294967296ll; - - { - ASSERT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance<__int128_t>(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(uint32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance<__uint128_t>(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = luabridge::get<__int128_t>(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Int128StackOverflow) -{ - exhaustStackSpace(); - - __int128_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} -#endif - -TEST_F(StackTests, Uint8Type) -{ - uint8_t value = 128; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Uint8StackOverflow) -{ - exhaustStackSpace(); - - uint8_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Uint8InvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 2147483648ll); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Uint16Type) -{ - uint16_t value = 32768; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Uint16StackOverflow) -{ - exhaustStackSpace(); - - uint16_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Uint16InvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 2147483648ll); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Uint32Type) -{ - constexpr uint32_t max_integral = static_cast(std::numeric_limits::max()); - - uint32_t value = max_integral < 2147483648u ? max_integral : 2147483648u; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - if constexpr (sizeof(lua_Integer) == sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Uint32StackOverflow) -{ - exhaustStackSpace(); - - uint32_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Uint32InvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } - - { - (void)luabridge::Stack::push(L, 9223372036854775808ull); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, Uint64Type) -{ - uint64_t value = static_cast(std::numeric_limits::max()); - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) >= sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) >= sizeof(uint32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - if constexpr (sizeof(lua_Integer) >= sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Uint64StackOverflow) -{ - exhaustStackSpace(); - - uint64_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Uint64InvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -#if 0 // defined(__SIZEOF_INT128__) -TEST_F(StackTests, Uint128Type) -{ - __uint128_t value = static_cast<__uint128_t>(std::numeric_limits::max()); - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) >= sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance<__int128_t>(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - if constexpr (sizeof(lua_Integer) >= sizeof(uint32_t)) - { - EXPECT_TRUE(luabridge::isInstance(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance<__uint128_t>(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - if constexpr (sizeof(lua_Integer) >= sizeof(int32_t)) - { - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - } - else - { - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - } - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get<__uint128_t>(L, -1); - EXPECT_EQ(value, result); - } -} - -TEST_F(StackTests, Uint128StackOverflow) -{ - exhaustStackSpace(); - - __uint128_t value = 42; - - ASSERT_FALSE(luabridge::push(L, value)); -} -#endif - -TEST_F(StackTests, IntTypeNotFittingPush) -{ - std::error_code ec; - - if constexpr (sizeof(uint32_t) == sizeof(lua_Integer) && ! std::is_unsigned_v) - { - uint32_t value = 4294967295u; - - auto result = luabridge::push(L, value); - EXPECT_FALSE(result); - EXPECT_STREQ("luabridge", static_cast(result).category().name()); - } - - if constexpr (sizeof(uint64_t) == sizeof(lua_Integer) && ! std::is_unsigned_v) - { - uint64_t value = 9223372036854775808ull; - - auto result = luabridge::push(L, value); - EXPECT_FALSE(result); - EXPECT_STREQ("luabridge", static_cast(result).category().name()); - } - -#if 0 // defined(__SIZEOF_INT128__) - if constexpr (sizeof(uint64_t) == sizeof(lua_Integer) && ! std::is_unsigned_v) - { - __uint128_t value = __uint128_t(9223372036854775808ull) + __uint128_t(9223372036854775808ull); - - auto result = luabridge::push(L, value); - EXPECT_FALSE(result); - EXPECT_STREQ("luabridge", static_cast(result).category().name()); - } -#endif -} - -TEST_F(StackTests, IntTypeNotFittingIsInstance) -{ - if constexpr (sizeof(uint32_t) == sizeof(lua_Integer)) - { - const luabridge::StackRestore sr(L); - - EXPECT_TRUE(luabridge::push(L, 2147483647)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - } - - if constexpr (sizeof(uint64_t) == sizeof(lua_Integer)) - { - const luabridge::StackRestore sr(L); - - EXPECT_TRUE(luabridge::push(L, 9223372036854775807ll)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - } - -#if 0 // defined(__SIZEOF_INT128__) - if constexpr (sizeof(uint64_t) == sizeof(lua_Integer)) - { - const luabridge::StackRestore sr(L); - - EXPECT_TRUE(luabridge::push(L, __int128_t(9223372036854775807ll))); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - EXPECT_FALSE(luabridge::isInstance(L, 1)); - } -#endif -} - -TEST_F(StackTests, FloatType) -{ - float value = 123.5678f; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_FLOAT_EQ(value, result); - } -} - -TEST_F(StackTests, FloatStackOverflow) -{ - exhaustStackSpace(); - - float value = 42.0f; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, FloatInvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, DoubleType) -{ - double value = 123.5678; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_DOUBLE_EQ(value, result); - } -} - -TEST_F(StackTests, DoubleStackOverflow) -{ - exhaustStackSpace(); - - double value = 42.0; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, DoubleInvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, LongDoubleType) -{ - long double value = 123.5678l; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_DOUBLE_EQ(value, result); - } -} - -TEST_F(StackTests, LongDoubleStackOverflow) -{ - exhaustStackSpace(); - - long double value = 42.0l; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, LongDoubleInvalidType) -{ - { - (void)luabridge::Stack::push(L, "100000000"); - auto result = luabridge::Stack::get(L, -1); - ASSERT_FALSE(result); - } -} - -TEST_F(StackTests, FloatTypeNotFittingPush) -{ - if constexpr (sizeof(double) > sizeof(lua_Number)) - { - double value = std::numeric_limits::max(); - - auto result = luabridge::push(L, value); - EXPECT_FALSE(result); - EXPECT_STREQ("luabridge", static_cast(result).category().name()); - } - - if constexpr (sizeof(long double) > sizeof(double) && sizeof(long double) > sizeof(lua_Number)) - { - long double value = std::numeric_limits::max(); - - auto result = luabridge::push(L, value); - EXPECT_FALSE(result); - EXPECT_STREQ("luabridge", static_cast(result).category().name()); - } -} - -TEST_F(StackTests, FloatTypeNotFittingIsInstance) -{ - const luabridge::StackRestore sr(L); - - EXPECT_TRUE(luabridge::push(L, std::numeric_limits::max())); - - EXPECT_FALSE(luabridge::isInstance(L, 1)); - - if constexpr (sizeof(double) == sizeof(lua_Number)) - { - EXPECT_TRUE(luabridge::isInstance(L, 1)); - } - - if constexpr (sizeof(long double) > sizeof(double) && sizeof(long double) == sizeof(lua_Number)) - { - EXPECT_FALSE(luabridge::isInstance(L, 1)); - } - - EXPECT_TRUE(luabridge::isInstance(L, 1)); -} - -TEST_F(StackTests, CharArrayType) -{ - char value[] = "xyz"; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_STREQ(value, result); - } -} - -TEST_F(StackTests, CharArrayStackOverflow) -{ - exhaustStackSpace(); - - char value[] = "xyz"; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstCharArrayType) -{ - const char value[] = "xyz"; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_STREQ(value, result); - } -} - -TEST_F(StackTests, ConstCharArrayStackOverflow) -{ - exhaustStackSpace(); - - const char value[] = "xyz"; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstCharLiteralType) -{ - EXPECT_TRUE(luabridge::push(L, "xyz")); - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_STREQ("xyz", result); - } -} - -TEST_F(StackTests, ConstCharLiteralStackOverflow) -{ - exhaustStackSpace(); - - ASSERT_FALSE(luabridge::push(L, "xyz")); -} - -TEST_F(StackTests, ConstCharPointerType) -{ - // Normal value - const char* value = "xyz"; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = *luabridge::get(L, -1); - EXPECT_STREQ(value, result); - } - - // Nullptr value - const char* nullptr_value = nullptr; - - { - EXPECT_TRUE(luabridge::push(L, nullptr_value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = luabridge::get(L, -1); - EXPECT_FALSE(result); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_STREQ("", result); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ("", result); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ("", result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_STREQ("", *result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_STREQ("", result->data()); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_STREQ("", result->data()); - } -} - -TEST_F(StackTests, ConstCharPointerStackOverflow) -{ - exhaustStackSpace(); - - const char* value = "xyz"; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstCharPointerInvalidType) -{ - ASSERT_TRUE(luabridge::push(L, luabridge::LuaNil())); - - { - auto result = luabridge::get(L, -1); - EXPECT_FALSE(result); - } -} - -TEST_F(StackTests, StringViewType) -{ - // Normal value - std::string_view value = "xyz"; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - { - auto result = luabridge::get(L, -1); - EXPECT_FALSE(result); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } - - { - auto result = *luabridge::get(L, -1); - EXPECT_EQ(value, result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(value, *result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(value, *result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(value, *result); - } -} - -TEST_F(StackTests, StringViewStackOverflow) -{ - exhaustStackSpace(); - - std::string_view value = "xyz"; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, StringViewInvalidType) -{ - ASSERT_TRUE(luabridge::push(L, luabridge::LuaNil())); - - { - auto result = luabridge::get(L, -1); - EXPECT_FALSE(result); - } -} - -TEST_F(StackTests, StringType) -{ - // Normal value - std::string value = "xyz"; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance>(L, -1)); - - EXPECT_EQ(value, (*luabridge::get(L, -1))); - EXPECT_EQ(value, (*luabridge::get(L, -1))); - - { - auto result = luabridge::get(L, -1); - EXPECT_FALSE(result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(value, *result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(value, *result); - } - - { - auto result = *luabridge::get>(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(value, *result); - } -} - -TEST_F(StackTests, StringStackOverflow) -{ - exhaustStackSpace(); - - std::string value = "xyz"; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, StringInvalidType) -{ - ASSERT_TRUE(luabridge::push(L, luabridge::LuaNil())); - - { - auto result = luabridge::get(L, -1); - EXPECT_FALSE(result); - } -} - -TEST_F(StackTests, IntArrayType) -{ - int value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 }; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); -} - -TEST_F(StackTests, IntArrayStackOverflow) -{ - exhaustStackSpace(); - - int value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstIntArrayType) -{ - const int value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - { - EXPECT_TRUE(luabridge::push(L, value)); - } - - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_FALSE(luabridge::isInstance>(L, -1)); - EXPECT_TRUE(luabridge::isInstance(L, -1)); -} - -TEST_F(StackTests, ConstIntArrayStackOverflow) -{ - exhaustStackSpace(); - - const int value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstLongArrayStackOverflow) -{ - exhaustStackSpace(); - - const long value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstLongLongArrayStackOverflow) -{ - exhaustStackSpace(); - - const long long value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, ConstUnsignedLongLongArrayStackOverflow) -{ - exhaustStackSpace(); - - const unsigned long long value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, OptionalStackOverflow) -{ - exhaustStackSpace(); - - std::optional value = 1; - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Pair) -{ - { - auto value = std::make_pair(1, "one"); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(1, std::get<0>(*result)); - EXPECT_STREQ("one", std::get<1>(*result)); - } - - { - auto value = std::make_pair(1, std::string("one")); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get>(L, -1); - ASSERT_FALSE(result); - EXPECT_EQ(luabridge::ErrorCode::InvalidTypeCast, result.error()); - } - - { - auto value = std::make_tuple(1, 2, 3); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get>(L, -1); - ASSERT_FALSE(result); - EXPECT_EQ(luabridge::ErrorCode::InvalidTableSizeInCast, result.error()); - } -} - -TEST_F(StackTests, PairNesting) -{ - auto value = std::make_pair(1, std::make_pair(1, std::make_pair(1, std::string("one")))); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(1, std::get<0>(*result)); - EXPECT_EQ(1, std::get<0>(std::get<1>(*result))); - EXPECT_EQ(1, std::get<0>(std::get<1>(std::get<1>(*result)))); - EXPECT_EQ("one", std::get<1>(std::get<1>(std::get<1>(*result)))); -} - -TEST_F(StackTests, PairStackOverflow) -{ - exhaustStackSpace(); - - auto value = std::make_pair(1, "one"); - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, Tuple) -{ - { - auto value = std::make_tuple(1, 1.0f, "one"); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(1, std::get<0>(*result)); - EXPECT_FLOAT_EQ(1.0f, std::get<1>(*result)); - EXPECT_STREQ("one", std::get<2>(*result)); - } - - { - auto value = std::make_tuple(1, std::string("one")); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get>(L, -1); - ASSERT_FALSE(result); - EXPECT_EQ(luabridge::ErrorCode::InvalidTypeCast, result.error()); - } - - { - auto value = std::make_tuple(1, 2, 3); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get>(L, -1); - ASSERT_FALSE(result); - EXPECT_EQ(luabridge::ErrorCode::InvalidTableSizeInCast, result.error()); - } -} - -TEST_F(StackTests, TupleNesting) -{ - auto value = std::make_tuple(1, std::make_tuple(1, std::make_tuple(1, std::string("one")))); - ASSERT_TRUE(luabridge::push(L, value)); - - auto result = luabridge::get(L, -1); - ASSERT_TRUE(result); - EXPECT_EQ(1, std::get<0>(*result)); - EXPECT_EQ(1, std::get<0>(std::get<1>(*result))); - EXPECT_EQ(1, std::get<0>(std::get<1>(std::get<1>(*result)))); - EXPECT_EQ("one", std::get<1>(std::get<1>(std::get<1>(*result)))); -} - -TEST_F(StackTests, TupleStackOverflow) -{ - exhaustStackSpace(); - - auto value = std::make_tuple(1, 1.0f, "one"); - - ASSERT_FALSE(luabridge::push(L, value)); -} - -TEST_F(StackTests, NilStackOverflow) -{ - exhaustStackSpace(); - - ASSERT_FALSE(luabridge::push(L, luabridge::LuaNil())); -} - -TEST_F(StackTests, Nil) -{ - (void)luabridge::push(L, luabridge::LuaNil()); - - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); -} - -TEST_F(StackTests, Bool) -{ - (void)luabridge::push(L, true); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - - ASSERT_EQ(true, *luabridge::get(L, -1)); -} - -TEST_F(StackTests, Int) -{ - (void)luabridge::push(L, 5); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - - ASSERT_EQ(5, *luabridge::get(L, -1)); - ASSERT_NEAR(5.f, *luabridge::get(L, -1), 1e-5); - ASSERT_NEAR(5.0, *luabridge::get(L, -1), 1e-6); -} - -TEST_F(StackTests, Float) -{ - (void)luabridge::push(L, 3.14f); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - - ASSERT_NEAR(3.14f, *luabridge::get(L, -1), 1e-5); - ASSERT_NEAR(3.14, *luabridge::get(L, -1), 1e-6); -} - -TEST_F(StackTests, CString) -{ - (void)luabridge::push(L, "abc"); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - - ASSERT_STREQ("abc", *luabridge::get(L, -1)); - ASSERT_EQ("abc", *luabridge::get(L, -1)); - ASSERT_EQ("abc", *luabridge::get(L, -1)); -} - -TEST_F(StackTests, StdString) -{ - (void)luabridge::push(L, std::string("abc")); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - - ASSERT_STREQ("abc", *luabridge::get(L, -1)); - ASSERT_EQ("abc", *luabridge::get(L, -1)); - ASSERT_EQ("abc", *luabridge::get(L, -1)); -} - -TEST_F(StackTests, StdStringView) -{ - (void)luabridge::push(L, std::string_view("abc")); - - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_FALSE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - ASSERT_TRUE(luabridge::isInstance(L, -1)); - - ASSERT_STREQ("abc", *luabridge::get(L, -1)); - ASSERT_EQ("abc", *luabridge::get(L, -1)); - ASSERT_EQ("abc", *luabridge::get(L, -1)); -} - -TEST_F(StackTests, ResultCheck) -{ - struct Unregistered {}; - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_ANY_THROW(luabridge::push(L, std::optional(Unregistered{})).error()); -#else - EXPECT_EQ(luabridge::ErrorCode::ClassNotRegistered, luabridge::push(L, std::optional(Unregistered{})).error()); -#endif -} - -TEST_F(StackTests, TypeResultCheck) -{ - (void)luabridge::push(L, std::string_view("abc")); - - EXPECT_EQ(std::string{ "abc" }, luabridge::get(L, -1)); - EXPECT_EQ(luabridge::get(L, -1), std::string{ "abc" }); - EXPECT_NE(std::string{ "123" }, luabridge::get(L, -1)); - EXPECT_NE(luabridge::get(L, -1), std::string{ "123" }); - - EXPECT_EQ(luabridge::ErrorCode::InvalidTypeCast, luabridge::get(L, -1).error()); - EXPECT_EQ(luabridge::ErrorCode::InvalidTypeCast, static_cast(luabridge::get(L, -1))); -} diff --git a/LuaBridge3/Tests/Source/TestBase.h b/LuaBridge3/Tests/Source/TestBase.h deleted file mode 100644 index 80a91c4..0000000 --- a/LuaBridge3/Tests/Source/TestBase.h +++ /dev/null @@ -1,228 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -#pragma once - -#include "Lua/LuaLibrary.h" - -#include "LuaBridge/LuaBridge.h" - -#include - -#if LUABRIDGE_ON_LUAU -#include "../../ThirdParty/luau/Compiler/include/luacode.h" -#include "../../ThirdParty/luau/Common/include/Luau/Common.h" -#endif - -#if LUABRIDGE_HAS_EXCEPTIONS -#include -#endif - -#include - -// Uncomment this if you want errors to be printed when lua fails to compile or run -//#define LUABRIDGE_TESTS_PRINT_ERRORS 1 - -// Traceback function, when a runtime error occurs, this will append the call stack to the error message -inline int traceback(lua_State* L) -{ - // look up Lua's 'debug.traceback' function - lua_getglobal(L, "debug"); - if (!lua_istable(L, -1)) - { - lua_pop(L, 1); - return 1; - } - - lua_getfield(L, -1, "traceback"); - if (!lua_isfunction(L, -1)) - { - lua_pop(L, 2); - return 1; - } - - lua_pushvalue(L, 1); - lua_pushinteger(L, 2); - lua_call(L, 2, 1); - - lua_getglobal(L, "print"); - if (!lua_isfunction(L, -1)) - { - lua_pop(L, 1); - return 1; - } - - lua_pushvalue(L, 1); - lua_call(L, 1, 0); - - return 1; -} - -#if LUABRIDGE_ON_LUAU -inline int luaL_loadstring(lua_State *L, const char *s) -{ - std::size_t bytecodeSize = 0; - - auto bytecode = std::shared_ptr( - luau_compile(s, std::strlen(s), nullptr, &bytecodeSize), - [](char* x) { std::free(x); } - ); - - return luau_load(L, "code", bytecode.get(), bytecodeSize, 0); -} -#endif - -// Base test class. Introduces the global 'result' variable, used for checking of C++ - Lua interoperation. -struct TestBase : public ::testing::Test -{ - lua_State* L = nullptr; - - void SetUp() override - { - L = createNewLuaState(); - } - - void TearDown() override - { - closeLuaState(); - } - - lua_State* createNewLuaState(lua_Alloc alloc = nullptr) const - { - lua_State* l; - - if (alloc) - l = lua_newstate(alloc, nullptr); - else - l = luaL_newstate(); - - luaL_openlibs(l); - - luabridge::registerMainThread(l); - -#if LUABRIDGE_HAS_EXCEPTIONS - luabridge::enableExceptions(l); -#endif - - return l; - } - - void closeLuaState() - { - if (L != nullptr) - { - lua_close(L); - L = nullptr; - } - } - - bool runLua(const std::string& script, lua_State* overrideState = nullptr) const - { - auto stateToUse = overrideState ? overrideState : L; - - lua_settop(stateToUse, 0); - - luabridge::lua_pushcfunction_x(stateToUse, &traceback); - - if (luaL_loadstring(stateToUse, script.c_str()) != LUABRIDGE_LUA_OK) - { - [[maybe_unused]] auto errorString = lua_tostring(stateToUse, -1); - -#if LUABRIDGE_HAS_EXCEPTIONS - throw std::runtime_error(errorString ? errorString : "Unknown lua compilation error"); -#else -#if LUABRIDGE_TESTS_PRINT_ERRORS - std::cerr << "===== Lua Compile Error =====\n"; - std::cerr << errorString ? errorString : "Unknown lua compilation error" << "\n"; -#endif // LUABRIDGE_TESTS_PRINT_ERRORS - return false; -#endif // LUABRIDGE_HAS_EXCEPTIONS - } - - if (lua_pcall(stateToUse, 0, 0, -2) != LUABRIDGE_LUA_OK) - { - [[maybe_unused]] auto errorString = lua_tostring(stateToUse, -1); - -#if LUABRIDGE_HAS_EXCEPTIONS - throw std::runtime_error(errorString ? errorString : "Unknown lua runtime error"); -#else -#if LUABRIDGE_TESTS_PRINT_ERRORS - std::cerr << "===== Lua Call Error =====\n"; - std::cerr << (errorString ? errorString : "Unknown lua runtime error") << "\n"; -#endif // LUABRIDGE_TESTS_PRINT_ERRORS - return false; -#endif // LUABRIDGE_HAS_EXCEPTIONS - } - - return true; - } - - std::tuple runLuaCaptureError(const std::string& script) const - { - lua_settop(L, 0); - - if (luaL_loadstring(L, script.c_str()) != LUABRIDGE_LUA_OK) - { - auto errorString = lua_tostring(L, -1); - -#if LUABRIDGE_TESTS_PRINT_ERRORS - std::cerr << "===== Lua Compile Error =====\n"; - std::cerr << errorString ? errorString : "Unknown lua compilation error" << "\n"; -#endif // LUABRIDGE_TESTS_PRINT_ERRORS - - return std::make_tuple(false, errorString); - } - - if (lua_pcall(L, 0, 0, 0) != LUABRIDGE_LUA_OK) - { - auto errorString = lua_tostring(L, -1); - -#if LUABRIDGE_TESTS_PRINT_ERRORS - std::cerr << "===== Lua Call Error =====\n"; - std::cerr << (errorString ? errorString : "Unknown lua runtime error") << "\n"; -#endif // LUABRIDGE_TESTS_PRINT_ERRORS - - return std::make_tuple(false, errorString); - } - - return std::make_tuple(true, ""); - } - - template - T result() const - { - if constexpr (std::is_same_v) - return luabridge::getGlobal(L, "result"); - - return luabridge::getGlobal(L, "result").unsafe_cast(); - } - - void resetResult() const - { - luabridge::setGlobal(L, luabridge::LuaRef(L), "result"); - } - - void exhaustStackSpace() - { - for (int i = 0; i < 2000000; ++i) - { - if (!lua_checkstack(L, 1)) - break; - - lua_pushnil(L); - } - } - - void printStack() const - { - std::cerr << "===== Stack =====\n"; - for (int i = 1; i <= lua_gettop(L); ++i) - { - std::cerr << "@" << i << " = " << luabridge::LuaRef::fromStack(L, i) << "\n"; - } - } -}; diff --git a/LuaBridge3/Tests/Source/TestTypes.h b/LuaBridge3/Tests/Source/TestTypes.h deleted file mode 100644 index 541d912..0000000 --- a/LuaBridge3/Tests/Source/TestTypes.h +++ /dev/null @@ -1,161 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#pragma once - -#include - -#include -#include -#include -#include - -using TestTypes = ::testing::Types; - -template -struct TypeTraits; - -template<> -struct TypeTraits -{ - static std::vector values() { return {true, false, true}; } - static std::string list() { return "true, false, true"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {'a', 'b', 'c'}; } - static std::string list() { return "'a', 'b', 'c'"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, -2, 3}; } - static std::string list() { return "1, -2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, 2, 3}; } - static std::string list() { return "1, 2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, -2, 3}; } - static std::string list() { return "1, -2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, 2, 3}; } - static std::string list() { return "1, 2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, -2, 3}; } - static std::string list() { return "1, -2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, 2, 3}; } - static std::string list() { return "1, 2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, -2, 3}; } - static std::string list() { return "1, -2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, 2, 3}; } - static std::string list() { return "1, 2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, -2, 3}; } - static std::string list() { return "1, -2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1, 2, 3}; } - static std::string list() { return "1, 2, 3"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1.2f, -2.5f, 3.14f}; } - static std::string list() { return "1.2, -2.5, 3.14"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1.2, -2.5, 3.14}; } - static std::string list() { return "1.2, -2.5, 3.14"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {1.2l, -2.5l, 3.14l}; } - static std::string list() { return "1.2, -2.5, 3.14"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {"", "a", "xyz"}; } - static std::string list() { return "'', 'a', 'xyz'"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {"", "a", "xyz"}; } - static std::string list() { return "'', 'a', 'xyz'"; } -}; - -template<> -struct TypeTraits -{ - static std::vector values() { return {"", "a", "xyz"}; } - static std::string list() { return "'', 'a', 'xyz'"; } -}; diff --git a/LuaBridge3/Tests/Source/Tests.cpp b/LuaBridge3/Tests/Source/Tests.cpp deleted file mode 100644 index f8cc545..0000000 --- a/LuaBridge3/Tests/Source/Tests.cpp +++ /dev/null @@ -1,551 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2021, Lucio Asnaghi -// Copyright 2019, Dmitry Tarakanov -// Copyright 2012, Vinnie Falco -// Copyright 2007, Nathan Reed -// SPDX-License-Identifier: MIT - -// A set of tests of different types' communication with Lua - -#include "TestBase.h" - -#include "LuaBridge/Set.h" -#include "LuaBridge/List.h" - -#include - -#include -#include -#include -#include - -namespace { -template -T identityCFunction(T value) -{ - return value; -} -} // namespace - -struct LuaBridgeTest : TestBase -{ -}; - -TEST_F(LuaBridgeTest, LambdaGlobalNamespace) -{ - int x = 100; - - luabridge::getGlobalNamespace(L) - .addFunction("test", [x](int v) -> int { - return v + x; - }) - .addFunction("test2", [x](lua_State* L, int v) -> int { - return v + (L != nullptr ? x : 0); - }); - - runLua("result = test (255)"); - - ASSERT_EQ(true, result().isNumber()); - EXPECT_EQ(355, result()); - - resetResult(); - runLua("result = test2 (nil, 255)"); - ASSERT_EQ(true, result().isNumber()); - EXPECT_EQ(355, result()); -} - -TEST_F(LuaBridgeTest, LambdaClassMethods) -{ - int x = 100; - - struct Inner - { - Inner() = default; - - int normalMethod0() const { return 42; } - int normalMethod1(int) const { return 42; } - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Inner") - .addConstructor() - .addFunction("test", [x](Inner*, int v) -> int { return v + x; }) - .addFunction("test2", [x](const Inner*, int v) -> int { return v + x; }) - .addFunction("normalMethod0", &Inner::normalMethod0) - .addFunction("normalMethod1", &Inner::normalMethod1) - .endClass(); - - runLua("x = Inner () result = x:test (255)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(355, result()); - - resetResult(); - runLua("x = Inner () result = x:test (255)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(355, result()); -} - -TEST_F(LuaBridgeTest, CFunction) -{ - luabridge::getGlobalNamespace(L) - .addFunction("boolFn", &identityCFunction) - .addFunction("ucharFn", &identityCFunction) - .addFunction("shortFn", &identityCFunction) - .addFunction("ushortFn", &identityCFunction) - .addFunction("intFn", &identityCFunction) - .addFunction("uintFn", &identityCFunction) - .addFunction("longFn", &identityCFunction) - .addFunction("ulongFn", &identityCFunction) - .addFunction("longlongFn", &identityCFunction) - .addFunction("ulonglongFn", &identityCFunction) - .addFunction("floatFn", &identityCFunction) - .addFunction("doubleFn", &identityCFunction) - .addFunction("charFn", &identityCFunction) - .addFunction("cstringFn", &identityCFunction) - .addFunction("stringFn", &identityCFunction); - - { - runLua("result = ucharFn (255)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(255u, result()); - } - - { - runLua("result = boolFn (false)"); - EXPECT_EQ(true, result().isBool()); - EXPECT_EQ(false, result()); - } - { - runLua("result = boolFn (true)"); - EXPECT_EQ(true, result().isBool()); - EXPECT_EQ(true, result()); - } - - { - runLua("result = shortFn (-32768)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(-32768, result()); - } - - { - runLua("result = ushortFn (32767)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(32767u, result()); - } - { - runLua("result = intFn (-500)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(-500, result()); - } - - { - runLua("result = uintFn (42)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(42u, result()); - } - - { - runLua("result = longFn (-8000)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(-8000, result()); - } - - { - runLua("result = ulongFn (9000)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(9000u, result()); - } - - { - runLua("result = longlongFn (-8000)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(-8000, result()); - } - - { - runLua("result = ulonglongFn (9000)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_EQ(9000u, result()); - } - - { - runLua("result = floatFn (3.14)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_FLOAT_EQ(3.14f, result()); - } - - { - runLua("result = doubleFn (-12.3)"); - EXPECT_EQ(true, result().isNumber()); - EXPECT_DOUBLE_EQ(-12.3, result()); - } - - { - runLua("result = charFn ('a')"); - EXPECT_EQ(true, result().isString()); - EXPECT_EQ('a', result()); - } - - { - runLua("result = cstringFn ('abc')"); - EXPECT_EQ(true, result().isString()); - EXPECT_STREQ("abc", result()); - } - - { - runLua("result = stringFn ('lua')"); - EXPECT_EQ(true, result().isString()); - EXPECT_EQ("lua", result()); - } -} - -TEST_F(LuaBridgeTest, Tuple) -{ - std::tuple t = std::make_tuple(1, 2.0f); - - luabridge::getGlobalNamespace(L) - .beginNamespace("tuple") - .addProperty("t", &t) - .endNamespace(); - - { - resetResult(); - runLua("result = { 1, 2 }"); - EXPECT_EQ(true, result().isTable()); - EXPECT_EQ((std::make_tuple(1, 2)), (result>())); - } - - { - resetResult(); - runLua("tuple.t = { 2, 4.0 }"); - EXPECT_EQ(2, std::get<0>(t)); - EXPECT_FLOAT_EQ(4.0f, std::get<1>(t)); - } - - { - resetResult(); - runLua("result = tuple.t"); - EXPECT_EQ(true, result().isTable()); - EXPECT_EQ(2, std::get<0>(result>())); - EXPECT_FLOAT_EQ(4.0f, std::get<1>(result>())); - } -} - -TEST_F(LuaBridgeTest, TupleAsFunctionReturnValue) -{ - int x = 100; - - struct Inner - { - Inner() = default; - }; - - luabridge::getGlobalNamespace(L) - .beginClass("Inner") - .addConstructor() - .addFunction("test", [x](Inner*) { return std::make_tuple(x, 42); }) - .endClass(); - - runLua("x = Inner () result = x:test ()"); - EXPECT_EQ(true, result().isTable()); - EXPECT_EQ(std::make_tuple(x, 42), (result>())); -} - -namespace { -template -struct TestClass -{ - TestClass(T data) : data(data), constData(data) {} - - T getValue() { return data; } - T* getPtr() { return &data; } - T const* getConstPtr() { return &data; } - T& getRef() { return data; } - T const& getConstRef() { return data; } - T getValueConst() const { return data; } - T* getPtrConst() const { return &data; } - T const* getConstPtrConst() const { return &data; } - T& getRefConst() const { return data; } - T const& getConstRefConst() const { return data; } - - mutable T data; - mutable T constData; -}; -} // namespace - -TEST_F(LuaBridgeTest, ClassFunction) -{ - typedef TestClass Inner; - typedef TestClass Outer; - - luabridge::getGlobalNamespace(L) - .beginClass("Inner") - .addConstructor() - .addProperty("data", &Inner::data) - .endClass() - .beginClass("Outer") - .addConstructor() - .addFunction("getValue", &Outer::getValue) - .addFunction("getPtr", &Outer::getPtr) - .addFunction("getConstPtr", &Outer::getConstPtr) - .addFunction("getRef", &Outer::getRef) - .addFunction("getConstRef", &Outer::getConstRef) - .addFunction("getValueConst", &Outer::getValueConst) - .addFunction("getPtrConst", &Outer::getPtrConst) - .addFunction("getConstPtrConst", &Outer::getConstPtrConst) - .addFunction("getRefConst", &Outer::getRefConst) - .addFunction("getConstRefConst", &Outer::getConstRefConst) - .endClass(); - - Outer outer(Inner(0)); - luabridge::setGlobal(L, &outer, "outer"); - - outer.data.data = 0; - runLua("outer:getValue ().data = 1"); - EXPECT_EQ(0, outer.data.data); - - outer.data.data = 1; - runLua("outer:getPtr ().data = 10"); - EXPECT_EQ(10, outer.data.data); - - outer.data.data = 2; -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("outer:getConstPtr ().data = 20"), std::runtime_error); -#else - EXPECT_FALSE(runLua("outer:getConstPtr ().data = 20")); -#endif - - outer.data.data = 3; - runLua("outer:getRef().data = 30"); - EXPECT_EQ(30, outer.data.data); - - outer.data.data = 4; -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(runLua("outer:getConstPtr ().data = 40"), std::runtime_error); -#else - EXPECT_FALSE(runLua("outer:getConstPtr ().data = 40")); -#endif - - outer.data.data = 5; - runLua("outer:getValueConst ().data = 50"); - EXPECT_EQ(5, outer.data.data); - - outer.data.data = 6; - runLua("outer:getPtrConst ().data = 60"); - EXPECT_EQ(60, outer.data.data); - - outer.data.data = 7; -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(runLua("outer:getConstPtr ().data = 70"), std::runtime_error); -#else - EXPECT_FALSE(runLua("outer:getConstPtr ().data = 70")); -#endif - - outer.data.data = 8; - runLua("outer:getRef().data = 80"); - EXPECT_EQ(80, outer.data.data); - - outer.data.data = 9; -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(runLua("outer:getConstPtr ().data = 90"), std::runtime_error); -#else - EXPECT_FALSE(runLua("outer:getConstPtr ().data = 90")); -#endif -} - -TEST_F(LuaBridgeTest, PropertyGetterFailOnUnregistredClass) -{ - struct Clazz {} clazz; - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("clazz", &clazz) - .endNamespace(); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(runLua("result = ns.clazz"), std::runtime_error); -#else - EXPECT_FALSE(runLua("result = ns.clazz")); -#endif -} - -TEST_F(LuaBridgeTest, CallReturnLuaResult) -{ - runLua("function f1 (arg0, arg1) end"); - runLua("function f2 (arg0, arg1) return arg0; end"); - runLua("function f3 (arg0, arg1) return arg0, arg1; end"); - runLua("function f4 () error('Something bad happened'); return arg0, arg1; end"); - - { - auto f1 = luabridge::getGlobal(L, "f1"); - auto result = luabridge::call(f1, 1, 2); - EXPECT_FALSE(result.hasFailed()); - EXPECT_TRUE(result.wasOk()); - EXPECT_EQ(std::error_code(), result.errorCode()); - } - - { - auto f2 = luabridge::getGlobal(L, "f2"); - auto result = luabridge::call(f2, 1, 2); - EXPECT_FALSE(result.hasFailed()); - EXPECT_TRUE(result.wasOk()); - EXPECT_EQ(std::error_code(), result.errorCode()); - EXPECT_EQ(1u, result.size()); - EXPECT_EQ(result[0], 1); - } - - { - auto f3 = luabridge::getGlobal(L, "f3"); - auto result = luabridge::call(f3, 1, 2); - EXPECT_FALSE(result.hasFailed()); - EXPECT_TRUE(result.wasOk()); - EXPECT_EQ(std::error_code(), result.errorCode()); - EXPECT_EQ(2u, result.size()); - EXPECT_EQ(result[0], 1); - EXPECT_EQ(result[1], 2); - } - -#if ! LUABRIDGE_HAS_EXCEPTIONS - { - auto f3 = luabridge::getGlobal(L, "f4"); - auto result = luabridge::call(f3); - EXPECT_TRUE(result.hasFailed()); - EXPECT_FALSE(result.wasOk()); - EXPECT_EQ(0u, result.size()); - EXPECT_NE(std::error_code(), result.errorCode()); - EXPECT_NE(std::string::npos, result.errorMessage().find("Something bad happened")); - } -#endif -} - -TEST_F(LuaBridgeTest, InvokePassingUnregisteredClassShouldThrowAndRestoreStack) -{ - class Unregistered {} unregistered; - - { - runLua("function f1 (unregistered) end"); - - auto f1 = luabridge::getGlobal(L, "f1"); - -#if LUABRIDGE_HAS_EXCEPTIONS - EXPECT_THROW(luabridge::call(f1, unregistered), luabridge::LuaException); -#else - int stackTop = lua_gettop(L); - - auto result = luabridge::call(f1, unregistered); - EXPECT_TRUE(result.hasFailed()); - EXPECT_FALSE(result.wasOk()); - EXPECT_EQ(luabridge::makeErrorCode(luabridge::ErrorCode::ClassNotRegistered), result.errorCode()); - - EXPECT_EQ(stackTop, lua_gettop(L)); -#endif - } -} - -namespace { -class A : public std::enable_shared_from_this -{ -public: - A() = default; - - A(int newX) - : x(newX) - { - } - - int x = 42; -}; -} // namespace - -TEST_F(LuaBridgeTest, StdSharedPtrSingle) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("test") - .beginClass("A") - .addConstructorFrom, void(*)(int)>() - .endClass() - .endNamespace(); - - std::shared_ptr a = std::make_shared(1); - luabridge::setGlobal(L, a, "a"); - - std::shared_ptr a2 = *luabridge::getGlobal>(L, "a"); - EXPECT_EQ(1, a2->x); - - EXPECT_TRUE(runLua("result = a")); - auto a3 = result>(); - EXPECT_EQ(1, a3->x); - - EXPECT_TRUE(runLua("result = test.A(2)")); - auto a4 = result>(); - EXPECT_EQ(2, a4->x); -} - -TEST_F(LuaBridgeTest, StdSharedPtrMultiple) -{ - luabridge::getGlobalNamespace(L) - .beginNamespace("test") - .beginClass("A") - .addConstructorFrom, void(*)(), void(*)(int)>() - .endClass() - .endNamespace(); - - EXPECT_TRUE(runLua("result = test.A()")); - auto a1 = result>(); - EXPECT_EQ(42, a1->x); - - EXPECT_TRUE(runLua("result = test.A(2)")); - auto a2 = result>(); - EXPECT_EQ(2, a2->x); -} - -#if LUABRIDGE_HAS_EXCEPTIONS -namespace { -template -std::string call_callback_get_exception(const luabridge::LuaRef& fn, Args&&... args) -{ - assert(fn.isCallable()); - - try { - fn(std::forward(args)...); - return {}; - } catch (const std::exception& e) { - return e.what(); - } -} -} // namespace - -TEST_F(LuaBridgeTest, Exception) -{ - luabridge::LuaRef cb1(L); - luabridge::LuaRef cb2(L); - - luabridge::getGlobalNamespace(L) - .beginNamespace("ns") - .addProperty("cb1", &cb1) - .addProperty("cb2", &cb2) - .endNamespace(); - - auto text = R"( - function ns.cb1() - local x = 42 - return x - 1337 - end - - function ns.cb2() - local y = 42 - this.will.fail() - return y - 1337 - end - )"; - - EXPECT_TRUE(runLua(text)); - - EXPECT_EQ("", call_callback_get_exception(cb1)); - - const auto error = call_callback_get_exception(cb2); - EXPECT_NE(std::string::npos, error.find("The lua function invocation raised an error")); - EXPECT_NE(std::string::npos, error.find("attempt to index")); - EXPECT_NE(std::string::npos, error.find(" nil ")); -} -#endif diff --git a/LuaBridge3/Tests/Source/Tests.lua b/LuaBridge3/Tests/Source/Tests.lua deleted file mode 100644 index 7bc4aa9..0000000 --- a/LuaBridge3/Tests/Source/Tests.lua +++ /dev/null @@ -1,107 +0,0 @@ --- test lua script to be run with the luabridge test program - -print("Running LuaBridge tests:"); - --- enum from C++ -FN_CTOR = 0 -FN_DTOR = 1 -FN_STATIC = 2 -FN_VIRTUAL = 3 -FN_PROPGET = 4 -FN_PROPSET = 5 -FN_STATIC_PROPGET = 6 -FN_STATIC_PROPSET = 7 -FN_OPERATOR = 8 -NUM_FN_TYPES = 9 - --- function to print contents of a table -function printtable (t) - for k, v in pairs(t) do - if (type(v) == "table") then - print(k .. " =>", "(table)"); - elseif (type(v) == "function") then - print(k .. " =>", "(function)"); - elseif (type(v) == "userdata") then - print(k .. " =>", "(userdata)"); - else - print(k .. " =>", v); - end - end -end - -function assert (expr) - if (not expr) then error("assert failed", 2) end -end - --- test functions registered from C++ - -assert(testSucceeded()); -assert(testRetInt() == 47); -assert(testRetFloat() == 47.0); -assert(testRetConstCharPtr() == "Hello, world"); -assert(testRetStdString() == "Hello, world"); - -testParamInt(47); assert(testSucceeded()); -testParamBool(true); assert(testSucceeded()); -testParamFloat(47.0); assert(testSucceeded()); -testParamConstCharPtr("Hello, world"); assert(testSucceeded()); -testParamStdString("Hello, world"); assert(testSucceeded()); -testParamStdStringRef("Hello, world"); assert(testSucceeded()); - --- test static methods of classes registered from C++ - -A.testStatic(); assert(testAFnCalled(FN_STATIC)); -B.testStatic(); assert(testAFnCalled(FN_STATIC)); -B.testStatic2(); assert(testBFnCalled(FN_STATIC)); - --- test static properties of classes registered from C++ - -assert(A.testStaticProp == 47); -assert(A.testStaticProp2 == 47);assert(testAFnCalled(FN_STATIC_PROPGET)); -A.testStaticProp = 48; assert(A.testStaticProp == 48); -A.testStaticProp2 = 49; assert(testAFnCalled(FN_STATIC_PROPSET) and A.testStaticProp2 == 49); - --- test classes registered from C++ - -object1 = A("object1"); assert(testAFnCalled(FN_CTOR)); -object1:testVirtual(); assert(testAFnCalled(FN_VIRTUAL)); - -object2 = B("object2"); assert(testAFnCalled(FN_CTOR) and testBFnCalled(FN_CTOR)); -object2:testVirtual(); assert(testBFnCalled(FN_VIRTUAL) and not testAFnCalled(FN_VIRTUAL)); - --- test functions taking and returning objects - -testParamAPtr(object1); assert(object1:testSucceeded()); -testParamAPtrConst(object1); assert(object1:testSucceeded()); -testParamConstAPtr(object1); assert(object1:testSucceeded()); -testParamSharedPtrA(object1); assert(object1:testSucceeded()); - -testParamAPtr(object2); assert(object2:testSucceeded()); -testParamAPtrConst(object2); assert(object2:testSucceeded()); -testParamConstAPtr(object2); assert(object2:testSucceeded()); -testParamSharedPtrA(object2); assert(object2:testSucceeded()); - -result = testRetSharedPtrA(); assert(result:getName() == "from C"); - --- test constness - -constA = testRetSharedPtrConstA(); assert(constA:getName() == "const A"); -assert(constA.testVirtual == nil); -testParamConstAPtr(constA); assert(constA:testSucceeded()); -assert(pcall(testParamAPtr, constA) == false, "attempt to call nil value"); - --- test properties - -assert(object1.testProp == 47); -assert(object1.testProp2 == 47); assert(testAFnCalled(FN_PROPGET)); -assert(object2.testProp == 47); -assert(object2.testProp2 == 47); assert(testAFnCalled(FN_PROPGET)); - -object1.testProp = 48; assert(object1.testProp == 48); -object1.testProp2 = 49; assert(testAFnCalled(FN_PROPSET) and object1.testProp2 == 49); - --- test operator overload -object1a = object1 + object1; assert(testAFnCalled(FN_OPERATOR)); -assert(object1a:getName() == "object1 + object1"); - -print("All tests succeeded."); diff --git a/LuaBridge3/Tests/Source/TestsMain.cpp b/LuaBridge3/Tests/Source/TestsMain.cpp deleted file mode 100644 index b9c9842..0000000 --- a/LuaBridge3/Tests/Source/TestsMain.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2018, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include - -int main(int argc, char** argv) -{ - // Disable performance tests by default - if (argc == 1) - { - testing::GTEST_FLAG(filter) = "-PerformanceTests.AllTests"; - } - - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/LuaBridge3/Tests/Source/UnorderedMapTests.cpp b/LuaBridge3/Tests/Source/UnorderedMapTests.cpp deleted file mode 100644 index 72bf98c..0000000 --- a/LuaBridge3/Tests/Source/UnorderedMapTests.cpp +++ /dev/null @@ -1,204 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -#include "LuaBridge/UnorderedMap.h" - -#include - -struct UnorderedMapTests : TestBase -{ -}; - -namespace { -struct Unregistered -{ -}; - -struct Data -{ - /* explicit */ Data(int i) : i(i) {} - - int i; -}; -} // namespace - -namespace std { -template <> -struct hash -{ - size_t operator()(const Data& value) const noexcept - { - return 0; // Don't care about hash collisions - } -}; - -template <> -struct hash -{ - size_t operator()(const Unregistered& value) const noexcept - { - return 0; // Don't care about hash collisions - } -}; -} // namespace std - -namespace { -[[maybe_unused]] bool operator==(const Unregistered& lhs, const Unregistered& rhs) -{ - return true; -} - -bool operator==(const Data& lhs, const Data& rhs) -{ - return lhs.i == rhs.i; -} - -std::ostream& operator<<(std::ostream& lhs, const Data& rhs) -{ - lhs << "{" << rhs.i << "}"; - return lhs; -} - -std::unordered_map processValues(const std::unordered_map& data) -{ - return data; -} - -std::unordered_map processPointers(const std::unordered_map& data) -{ - std::unordered_map result; - for (const auto& item : data) - { - result.emplace(item.first, *item.second); - } - return result; -} -} // namespace - -TEST_F(UnorderedMapTests, LuaRef) -{ - { - runLua("result = {[false] = true, a = 'abc', [1] = 5, [3.14] = -1.1}"); - - using Map = std::unordered_map; - - Map expected { - { luabridge::LuaRef(L, false), luabridge::LuaRef(L, true) }, - { luabridge::LuaRef(L, 'a'), luabridge::LuaRef(L, "abc") }, - { luabridge::LuaRef(L, 1), luabridge::LuaRef(L, 5) }, - { luabridge::LuaRef(L, 3.14), luabridge::LuaRef(L, -1.1) }, - }; - - Map actual = result(); - - ASSERT_EQ(expected, actual); - ASSERT_EQ(expected, result()); - } - - { - runLua("result = {'a', 'b', 'c'}"); - - using Int2Char = std::unordered_map; - Int2Char expected{{1, 'a'}, {2, 'b'}, {3, 'c'}}; - Int2Char actual = result(); - ASSERT_EQ(expected, actual); - ASSERT_EQ(expected, result()); - } -} - -TEST_F(UnorderedMapTests, PassToFunction) -{ - runLua("function foo (map) " - " result = map " - "end"); - - auto foo = luabridge::getGlobal(L, "foo"); - using Int2Bool = std::unordered_map; - - resetResult(); - - Int2Bool lvalue{{10, false}, {20, true}, {30, true}}; - foo(lvalue); - ASSERT_TRUE(result().isTable()); - ASSERT_EQ(lvalue, result()); - - resetResult(); - - const Int2Bool constLvalue = lvalue; - foo(constLvalue); - ASSERT_TRUE(result().isTable()); - ASSERT_EQ(constLvalue, result()); -} - -TEST_F(UnorderedMapTests, PassFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Data") - .addConstructor() - .endClass() - .addFunction("processValues", &processValues) - .addFunction("processPointers", &processPointers); - - { - resetResult(); - runLua("result = processValues ({[Data (-1)] = Data (2)})"); - std::unordered_map expected{{Data(-1), Data(2)}}; - const auto actual = result>(); - ASSERT_EQ(expected, actual); - } - - { - resetResult(); - runLua("result = processPointers ({[Data (3)] = Data (-4)})"); - std::unordered_map expected{{Data(3), Data(-4)}}; - const auto actual = result>(); - ASSERT_EQ(expected, actual); - } -} - -#if !LUABRIDGE_HAS_EXCEPTIONS -TEST_F(UnorderedMapTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) -{ - { - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::unordered_map v; - v.emplace(std::make_pair(1, Unregistered{})); - v.emplace(std::make_pair(2, Unregistered{})); - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); - } - - { - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::unordered_map v; - v.emplace(std::make_pair(Unregistered{}, 1)); - v.emplace(std::make_pair(Unregistered{}, 2)); - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); - } -} -#endif - diff --git a/LuaBridge3/Tests/Source/UserdataTests.cpp b/LuaBridge3/Tests/Source/UserdataTests.cpp deleted file mode 100644 index 5308879..0000000 --- a/LuaBridge3/Tests/Source/UserdataTests.cpp +++ /dev/null @@ -1,156 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2022, Lucio Asnaghi -// Copyright 2022, Stefan Frings -// SPDX-License-Identifier: MIT - -#include "TestBase.h" - -namespace { -class TestClass -{ -public: - explicit inline TestClass(int i) : m_i(i) {} - - inline int get() const { return m_i; } - -private: - int m_i; -}; - -int testFunctionObject(TestClass object) -{ - return object.get(); -} - -int testFunctionObjectConst(const TestClass object) -{ - return object.get(); -} - -int testFunctionRef(TestClass& object) -{ - return object.get(); -} - -int testFunctionRefConst(const TestClass& object) -{ - return object.get(); -} -} // namespace - -struct UserDataTest : TestBase -{ - void SetUp() override - { - TestBase::SetUp(); - - luabridge::getGlobalNamespace(L) - .beginClass("TestClass") - .addConstructor() - .endClass() - .addFunction("testFunctionObject", testFunctionObject) - .addFunction("testFunctionObjectConst", testFunctionObjectConst) - .addFunction("testFunctionRef", testFunctionRef) - .addFunction("testFunctionRefConst", testFunctionRefConst); - } -}; - -TEST_F(UserDataTest, Object) -{ - runLua("object = TestClass(123); result = testFunctionObject(object)"); - - ASSERT_EQ(result(), 123); -} - -TEST_F(UserDataTest, ObjectConst) -{ - runLua("object = TestClass(123); result = testFunctionObjectConst(object)"); - - ASSERT_EQ(result(), 123); -} - -TEST_F(UserDataTest, Ref) -{ - runLua("object = TestClass(123); result = testFunctionRef(object)"); - - ASSERT_EQ(result(), 123); -} - -TEST_F(UserDataTest, RefConst) -{ - runLua("object = TestClass(123); result = testFunctionRefConst(object)"); - - ASSERT_EQ(result(), 123); -} - -TEST_F(UserDataTest, FailNumberObject) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionObject(132)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionObject(132)")); -#endif -} - -TEST_F(UserDataTest, FailNumberObjectConst) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionObjectConst(132)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionObjectConst(132)")); -#endif -} - -TEST_F(UserDataTest, FailNumberRef) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionRef(132)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionRef(132)")); -#endif -} - -TEST_F(UserDataTest, FailNumberRefConst) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionRefConst(132)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionRefConst(132)")); -#endif -} - -TEST_F(UserDataTest, FailNilObject) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionObject(nil)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionObject(nil)")); -#endif -} - -TEST_F(UserDataTest, FailNilObjectConst) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionObjectConst(nil)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionObjectConst(nil)")); -#endif -} - -TEST_F(UserDataTest, FailNilRef) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionRef(nil)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionRef(nil)")); -#endif -} - -TEST_F(UserDataTest, FailNilRefConst) -{ -#if LUABRIDGE_HAS_EXCEPTIONS - ASSERT_THROW(runLua("testFunctionRefConst(nil)"), std::runtime_error); -#else - EXPECT_FALSE(runLua("testFunctionRefConst(nil)")); -#endif -} diff --git a/LuaBridge3/Tests/Source/VectorTests.cpp b/LuaBridge3/Tests/Source/VectorTests.cpp deleted file mode 100644 index 5997278..0000000 --- a/LuaBridge3/Tests/Source/VectorTests.cpp +++ /dev/null @@ -1,144 +0,0 @@ -// https://github.com/kunitoki/LuaBridge3 -// Copyright 2019, Dmitry Tarakanov -// SPDX-License-Identifier: MIT - -#include "TestBase.h" -#include "TestTypes.h" - -#include "LuaBridge/Vector.h" - -#include - -namespace { -template -void checkEquals(const std::vector& expected, const std::vector& actual) -{ - using U = std::decay_t; - - if constexpr (std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_FLOAT_EQ(expected[i], actual[i]); - } - else if constexpr (std::is_same_v || std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_DOUBLE_EQ(expected[i], actual[i]); - } - else if constexpr (std::is_same_v) - { - for (std::size_t i = 0; i < expected.size(); ++i) - ASSERT_STREQ(expected[i], actual[i]); - } - else - { - ASSERT_EQ(expected, actual); - } -} -} // namespace - -template -struct VectorTest : TestBase -{ -}; - -TYPED_TEST_SUITE_P(VectorTest); - -TYPED_TEST_P(VectorTest, LuaRef) -{ - using Traits = TypeTraits; - - this->runLua("result = {" + Traits::list() + "}"); - - std::vector expected(Traits::values()); - std::vector actual = this->result(); - - checkEquals(expected, actual); -} - -REGISTER_TYPED_TEST_SUITE_P(VectorTest, LuaRef); - -INSTANTIATE_TYPED_TEST_SUITE_P(VectorTest, VectorTest, TestTypes); - -namespace { -struct Data -{ - /* explicit */ Data(int i) : i(i) {} - - int i; -}; - -bool operator==(const Data& lhs, const Data& rhs) -{ - return lhs.i == rhs.i; -} - -std::ostream& operator<<(std::ostream& lhs, const Data& rhs) -{ - lhs << "{" << rhs.i << "}"; - return lhs; -} - -std::vector processValues(const std::vector& data) -{ - return data; -} - -std::vector processPointers(const std::vector& data) -{ - std::vector result; - for (const auto* item : data) - { - result.emplace_back(*item); - } - return result; -} -} // namespace - -struct VectorTests : TestBase -{ -}; - -TEST_F(VectorTests, PassFromLua) -{ - luabridge::getGlobalNamespace(L) - .beginClass("Data") - .addConstructor() - .endClass() - .addFunction("processValues", &processValues) - .addFunction("processPointers", &processPointers); - - resetResult(); - runLua("result = processValues ({Data (-1), Data (2)})"); - - ASSERT_EQ(std::vector({-1, 2}), result>()); - - resetResult(); - runLua("result = processPointers ({Data (-3), Data (4)})"); - - ASSERT_EQ(std::vector({-3, 4}), result>()); -} - -#if !LUABRIDGE_HAS_EXCEPTIONS -TEST_F(VectorTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) -{ - class Unregistered {}; - - const int initialStackSize = lua_gettop(L); - - lua_pushnumber(L, 1); - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - std::vector v; - v.emplace_back(); - v.emplace_back(); - - auto result = luabridge::Stack::push(L, v); - EXPECT_FALSE(result); - - EXPECT_EQ(1, lua_gettop(L) - initialStackSize); - - lua_pop(L, 1); - EXPECT_EQ(0, lua_gettop(L) - initialStackSize); -} -#endif diff --git a/LuaBridge3/_config.yml b/LuaBridge3/_config.yml deleted file mode 100644 index e04f5c5..0000000 --- a/LuaBridge3/_config.yml +++ /dev/null @@ -1,2 +0,0 @@ -theme: jekyll-theme-cayman -exclude: ThirdParty diff --git a/LuaBridge3/amalgamate.py b/LuaBridge3/amalgamate.py deleted file mode 100644 index 75b0623..0000000 --- a/LuaBridge3/amalgamate.py +++ /dev/null @@ -1,217 +0,0 @@ -import os -import re -import argparse -import datetime -from collections import deque - -PARSE_FILE = 0 -EXTERNAL_FILE = 1 -ALREADY_SCANNED = 2 - -CPP_HEADER_FILE_EXT = set([".hpp" , ".h" , ".hxx" , ".hh" , ".inl"]) - -PRAGMA_ONCE_MATCHER = re.compile(r'#pragma once') -INCLUDE_FILE_MATCHER = re.compile(r'#include\s*[<\"]([\w.\\/]*)[>\"]') -LOCAL_INCLUDE_FILE_MATCHER = re.compile(r'#include\s*\"([\w.\\/]*)\"') - - -def IsCppHeaderFile(ext): - return ext in CPP_HEADER_FILE_EXT - -def AdjustFileExtension(ext): - if ext[0] != '.': - ext = '.' + ext - -def RemoveComments(text): - def BlotOutNonNewlines(strIn): - if strIn.startswith("/*"): - return "\n" - else: - return "" - - def Replacer(match): - s = match.group(0) - if s.startswith('/'): - return BlotOutNonNewlines(s) - else: - return s - - pattern = re.compile( - r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', - re.DOTALL | re.MULTILINE - ) - - return re.sub(pattern, Replacer, text) - -class SourceInfo: - def __init__(self, baseDir , outputDir, outputName): - self.includeDirs = list() - self.headerQueue = deque() - self.systemHeaders = set() - self.scannedFiles = set() - - self.baseDir = baseDir - self.outputDir = outputDir - - self.outputName = outputName - self.headerFileExt = ".h" - - self.AddIncludeDirectory(self.baseDir) - self.AddIncludeDirectory(os.path.join(self.baseDir, "detail")) - - def LogMessage(self, message): - print(message) - - def AddIncludeDirectory(self, path): - if not os.path.exists(path): - return False - - self.LogMessage(f"Include Directory Added: {path}") - - self.includeDirs.append(path) - return True - - def GetAbsoluteSourcePath(self, pwd, include): - if os.path.isabs(include): - return include - - for includeDir in self.includeDirs: - absPath = os.path.normpath(os.path.join(includeDir, include)) - if os.path.exists(absPath): - return absPath - - return None - - def ShouldParseFile(self , path , ext): - if (path in self.scannedFiles): return ALREADY_SCANNED - - if not IsCppHeaderFile(ext) or not os.path.exists(path): - return EXTERNAL_FILE - - return PARSE_FILE - - def ScanSourceFile(self, path , depth): - dirpath, filename = os.path.split(path) - ext = os.path.splitext(filename)[1] - - info = self.ShouldParseFile(path, ext) - if info != PARSE_FILE: - return info - - self.LogMessage(f"Scan file: {path}") - self.scannedFiles.add(path) - - with open (path , "r") as src: - lines = src.readlines() - - for line in lines: - includeResult = INCLUDE_FILE_MATCHER.findall(line) - if not includeResult: - continue - - localResult = LOCAL_INCLUDE_FILE_MATCHER.findall(line) - if localResult: - includeFile = self.GetAbsoluteSourcePath(dirpath, localResult[0]) - if includeFile is None: - continue - - call = self.ScanSourceFile(includeFile , depth + 1) - if call == EXTERNAL_FILE: - self.scannedFiles.add(includeFile) - else: - self.systemHeaders.add(includeResult[0]) - - self.AddFileToQueue(path, ext) - - return info - - def ParseDirectories(self): - all_files = [] - - for sourceDirectory in self.includeDirs: - for root, _, files in os.walk(sourceDirectory): - for filename in files: - all_files.append(os.path.join(root, filename)) - - for path in sorted(all_files): - self.ScanSourceFile(path, 0) - - - def WriteBeginFileHeader(self, filename, stream): - stream.write(f"\n// Begin File: {filename}\n") - - def WriteEndFileHeader(self, filename, stream): - stream.write(f"\n// End File: {filename}\n") - - def AddFileToQueue(self, filename, ext): - if IsCppHeaderFile(ext): - self.LogMessage(f"Enqueue header file: {filename}") - self.headerQueue.append(filename) - - def AmalgamateQueue(self, queue, stream): - while (len(queue) > 0): - path = queue.popleft() - self.WriteFileToStream(path, stream) - - def WriteFileToStream(self, path, stream): - self.LogMessage(f"Write File: {path}") - - with open (path, 'r') as source: - self.WriteBeginFileHeader(path, stream) - - lastLineWasEmpty = False - - text = RemoveComments(source.read()) - lines = text.replace("\r", "\n").split("\n") - - for line in lines: - result = INCLUDE_FILE_MATCHER.findall(line) - if result: - continue - - result = PRAGMA_ONCE_MATCHER.findall(line) - if result: - continue - - stripped_line = line.strip() - if stripped_line or not lastLineWasEmpty: - stream.write(f"{line}\n") - - lastLineWasEmpty = not stripped_line - - self.WriteEndFileHeader(path, stream) - - def WriteAlgamationFiles(self): - headerPath = os.path.join(self.outputDir, self.outputName + self.headerFileExt) - - self.LogMessage(f"Creating source Amalgamation: {headerPath}") - - with open (headerPath , 'w') as headerAmalgamation: - current_year = datetime.date.today().year - headerAmalgamation.write(f"// https://github.com/kunitoki/LuaBridge3\n") - headerAmalgamation.write(f"// Copyright {current_year}, Lucio Asnaghi\n") - headerAmalgamation.write(f"// SPDX-License-Identifier: MIT\n\n") - headerAmalgamation.write(f"// clang-format off\n\n") - headerAmalgamation.write(f"#pragma once\n\n") - - for header in sorted(list(self.systemHeaders)): - headerAmalgamation.write(f"#include <{header}>\n") - headerAmalgamation.write("\n") - - self.AmalgamateQueue(self.headerQueue, headerAmalgamation) - - headerAmalgamation.write("// clang-format on\n\n") - - return headerPath - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description='Amalgamate LuaBridge.') - parser.add_argument('--base', action='store', default="Source/LuaBridge/") - parser.add_argument('--output', action='store', default="Distribution/LuaBridge/") - parser.add_argument('--name', action='store', default="LuaBridge") - - args = parser.parse_args() - - sourceInfo = SourceInfo(args.base, args.output, args.name) - sourceInfo.ParseDirectories() - sourceInfo.WriteAlgamationFiles() diff --git a/LuaBridge3/logo.png b/LuaBridge3/logo.png deleted file mode 100644 index 9d7ae0892a76d39616ff1e54af52c8e6b13f5dcb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20688 zcmZ^K1z6q8w&;e9dnxYj?k=UcyTiua-K|)0cPqu+rD$f5S14N0P5lp zpN*j3>m(*pD)In;Hx&R7_yquXd~XUo1OVJv0Dxl<0Kk_H0AM?2w<`<0BT~(@q|N2! z0d(&$8~_>u9RPfXAl`QX1Rmh8w08&~1A+hFunGjtziA)=fN(1S)W2zT-pjvR{QL2j z=bsXo7zTiSuc5ymU-BXUiyFc?ANXG|!2KNu5LFeEmVPf)O`Ofl>|HD!T+Pge-Yo^d zJ4$K0003B&e-8+06^cs$0J6YJP0LkFUXIVi!H&tu)WO({$nP0jgK zK1=+Y{kPH zQ{BbPSMoBCe{YWV zM?pn7*%b0;IoJK|^4B>{S*A8_Nm4c?Qg~qyfc-Z%o9N0`%W64VX3TtEJ?7jBGa8y& zO_;w9Zt{hNS__PXs1U*Qqt0`TY7*&_qjq*HQV5Gtl@v+A^mO7E*&LrI0LRY&zvaCC zm_+R+E+wgJYrD1^b^PtKK6;|GUvM)vLCnW8+_)K?IL9 z>a`2gf25#H(sh?E2aLG5I8AU=2oQ3Bitqr84OtY8UWYFXZ)~g-Sbv>J5j{7;6Tmxg zI)3*<M|Vc=@ueL_1`+e-m<6J>51ZMadS1t6i1_xT|^)UE&w4HBZF7iS7^Y!bM8 zRe02xa~$=)ij4mqI3@+rf7k@490mb*^s>=hXHSueeJUW17;U zdDukqtHZ#GxNMp825B>e^Ly_;WTlRXTgXAB`rM4=?CuHPyazG}Bm?Tv-E~JR;oqw} z3paxi$!<{f25wiPpajm0Cw?u9Y#d|m3MlGm^-sIb@JW5>QPYZ}{~J7?C|UA@-u=A_ z!+LnW&>StS?Jw!eab`jBkg=R@;t#V=^$z5HW?*9z)MjcANP%?eG(=={`S;vbUuEyxiysaAHFlFi3M2yA zM%dqd`GmcfNFr`AWVIb(%J`uGHRAuhw8@!^SlD) zg3`kODy)+R^#Iz4`*krRgd4SWxBa$0#!-;9D;=7bHLNn2(Ms1tHE0{G*DHp!WoF_; z2H#Em^PqX54}ySrkK>9=oxL}~4i%zRSAdX{;6Oaq^~j|nK3IUCQ+t%8=;p#t*`O}u zVFXkj;MNBo|9dZrk_9E9{AJBr-UXx9BtCJUYTopZWAr6aO8zj*g=S%vJ5CUHXE{Ka zN*c&ctNG_KXN%5LCSnhJ#SGCO(BhvrMl+|1bhM5GCn62bwoeV7!=QzSahV)9R;9dH z_^Ty}GAIe=U5icT%5JqJ+_sMyl}Jaoc}XiK5C6w%63X-gh%rP4mH@XC|4EB;jR*sQ zzv1$z21#x9SO9UQ6L_EY)~e%w^GcL~@*Z}f`MgSE*f{k4bNz$W_GJUbtu^bu3EA9L z#!_iVa#`E(0I6SWn;}+YsKsjNMzbU%H(7 zLRPN7sCal9tJkUeK@GVl)u^tuN@Hhh_HP*drdG*ZUr4F&E->s3eU;dr?TAV?>;|?$ zIO%K~LPxwIkO(1?bP@2p1Q6P?$=z#3cY=q*$n^#;sl#bh< z<8-V}Ve~#~|4d48=rAMcmbOew2tN8u24V93H}Fco8?p`JYB`nbVYIJe?^i0Ng2$)` ztdq|kF1Vw(K{pd_zomzbxF58>zC=Z+e+GfY8UqJrQ57$_O-xOs$~anbSN`02aeSV< z5W4`-ku#0P*cV-<9idH3$^B>y(c<0+XZ0D9rFc>bocvp@`YDEpPqYTv6?uHK9uL7c zHzhZ;3V+p)`VZi<&PrAhtO>P*`yXe#_KxKnBo9Th5$SkYiRKv18Lm0bf%9DZ;ns{S zcbJa41G~w-iSU>8e$t-0vE(xNWGBb2oHfFD@)M04lNs23#7=4Xw6R78F`4<2B!zUi zFoo8Qecl>6rg}wr1>`AtYJYX#g_zgRffK41PW8;t7KptMD+UktxYUStq@0uj&Iz!` zz>{z`wl*al`x_QcLiwR5PMqbZ3ipvh zhc_9Oq-<`@{M)(pc{-D?R3qV&?eL)NGE=Fp(vHc1OKh*Z9`I&W&o(2=iRpKTBim%K z;?tLG1J=njE|%0+Gf$oFH%2e>K4J=bM${{#Bfc*btrN@bS-a)44#`J_ldj?|byWsL zK0Dkhvnr7a%;XGq;im+|$}mT?8Egy=$zynenpAPwk?m4{u8c=j*F5E3YtOL(!}c!* zy$sKv#j$YbqXV|#yI2E9r zmMv4spWViKn{D&ym*JnSjXU3Fft7|*V6B^QPTFU}Ki#l78S?q3;5weR3RXncIA z?qz%ZgcRuB!)EAsfq2cwaN|;Z-`kHt;t*ss>;7Z_rAY%`7a@&C3rx%(@*0-xhU~B$6@*c%A8!N8&y#Uz|N;@mXHj zN)9>daS?dwPvH6_Fz}zzxuPFI^o+Rb#~V@~$AuTsyG$kwy znX%aWko7#4(muVsgtS(QWIFa}Hq(%572{EGR%_1@A6|n>#cg$Frq~OGIC|;XM=BoD za~@a5ew+5`P2qk6fEM7|^nAUxCWPj3*PH2>iY^iVvtqzDb<3(uWpU z=-r;aqBQPx4yQSj|B8y18@NMpQ;0{mh( zl7s|iPIKy`I&p`i+b!rS(#T!Ar$MSgp&cU#^Depygj@pel1Uy*SUbbG?(iJq=Ltl9 zS>V1=H$yJmV5(Pj8)fcjU$EXA9i13fzZFTyLX!k@NEaFcMSWwQx6jtBLF}otVHk>_ z03yMS^+Axo{WXQdX|(mUZYp4GcIqkDX9MwiVM0vD3)tAzqIYZOO-F%WwG-fL-x-yI z81*eiVo@MFqQdoqrz%!X^Ko|KQ?7PQ$_|99AI+svM&C=gN3@ttE8z|-5{S|oPL^3u zXgZgC_WkSdo_@(HwEk#nrB-NbLz3RyfGRtm;M)j~j={d*;iXOR1KTBdZ_TfUu7aDx z(D4J4;f70J(OdcMu1_AIEXdG(hQLO&hAa!UGj8np)p5V$WD%>(P7o!)k88WRf(@@T zHWFBf4{-KhV!gNd^n!H%@uQt8Y%~2yupz`P;JWzFAYgf4G7sYY&Goo|^003Lb)`Rw zz7ZZYuJmKHcBfl~#k3kQ`y#UyH8D890P#Rhbs6hro*(3>f$&yTF^J9!EH?5gSFTpJ zqD@2r`@UY}-UYR3BoA{a?6q{(%r4S!G;vCq_~cmQ*n+koH;O1qq{Vb&I9hnQa~-W7 z1h}fnlxODTOsd7HKZ8|50e?Pb6;6;y`^8M*39Nfc9VX5^z?fF%A;)(otI~CSkL??u z35Ec{xCeQ~{(a+aoB5_tS=1x8<~-!^7Cs&y z%>fXx@v11K+=dMk28E|{JVlb3j8Ci5 zc}ch?&3K8)-MnxIaWfaW8IaH*9meGD+5+EA=Nchd1BY05u(Cc+b_(?*40VGa3Y^2& z4}=notw`@-#o<(iXFIiHY43#x#4!35;1>|ovv2cA*{P}@$_hyH#Je1;vtf@nx(#=~ z9Z^0kWv$E`6Q3I&4x&@1{T6J2z_@Gj82O58vb%vj9uJnp0{>~}LTbO2`fzwtSaYZT zEek!A1(Ku-2rx*_AT<=SNnCaNV|qV5vj>t)+tmTZS{ebfU>>TW?lpVZu!3Y@N$dYcU)cFw3N}OuYVqxuOD6|aG1T|`CDa3ATq?^3ABtvR*MP7E3WSx z0e~)??V^6iH9c3Tk9#8ljyWHE(6M||TOqHSu4lR3d(bCit?E$4vw_;0+iFje!oIn_ zhpXDP(T*7Sl~42d?jxK5e){FQ{AS(??F6C+J=R;@TEN*_c2G%=w9<;T&aUBP4w;7m zm!U)&7ELBMVRh={rehT6(6J+rOF2iOOJpjx-+tp5wfXmv+*k!(MElehRsf%eF)tp8 zaKblb00c9I9;C#T&72(Y&aLOQWx={KOu`{!M=oQqS;zGpU|NJa4E1JFykBk=6$!{x zA~KpL-Oy2x3FJooq+Yi%}8ZvKJsVG9X=;+t{!D1@UByR#Hd{1fAqf?4F7>zez0 zf5UvePfLc9JY1zu=JMVI@5kLt7jrsnMCjDK6ei8dJf;g&`b1`dW(|#tisX#^03<9& zY&=m8-9aXzui4E5Qd(OpTlJ(tPc?#yLhXl0(Q~@V@dtC)=nvb8?55)(6?HUX-!ZM? z)%$zy2HA+Sa>;y_`Aye5vCV|$!7qsL(DhwDR&RB(XcO}nqfxOoO( z!v}Wz>T>sxSU=O_u|pD~zlEz=T3kB!`E^;m-tkfEb|$$9pL2jYNP4e`tSw^(5TMI< zZy7GK2Bot zuZLz??RBX!v2OGcNJJLfH@LAXf03CP1QM*~*d+X62Op0LIRY+ZL=Y1KD2(0e^|yK9 zY4vKQdEOTfcIecE&!6(3;s<@)Jqhbwm*?4>?p^nCBeP{rVBJ2?#-)mZ{P~c<%fSLQ z2_xj@`Kl2eUo#{~N{GuFn`;8KoVdPx1p3MYDjeP(C?7`J?M8EcBG+z{R(Ckoe)Z7g zq><%MAwk(J+{g*KEArC5jtS6XP?f|0BP(`9QuauzgoW}lo;H2rLGta{*M-#4Wle9} z9&>X3rH*K7s~u#$=xcGkHH1m;6kwJEvedbDo|!|52%7u(W^9cTsIe7gM7GNI1C%it zI;j`*aGx;BcIU?yM!nN1oe#ysGXDjl97Ai#h|xgT{~6tseD7Eo#dF{6lcG27BHEG4 z)`1g@MjahVaQ$K1h}E&o8a)0YlJO8Cv`S+aJjQ9fYnQENx)rq098c`bLoM&!2_jZP zlobw=)-0LD7UtXN_(y590u>~`Z^KWoO4L05Jr4(zUVXlkPuF2uV-KeThpSO8;Oz-I zymN61+f`b;{$HbUh8tl)@@F68!dYhLoNCctFW&mscLV(V3P~s>m+<84U%e0@+y1N{2mkt!r=um z_3Ipq4V8%1uS0dqbi1LcUX*-ISG8kBo%;Jf*#aCUKNuP$+`BP_-fJOq2du8i9A~f1NR&S9?Hm!KlO;NS9%c?n zOo-v%+d)k$#h#2g9Jfs)a-|rfU+y+Awc1L_P58uTf9*Ot*(m{Aq@lr<97OV(BV3b0 z+}W(Iy)6moztY-vzy2*A_a)^eLDC?BSoo?u6T+k?`=`c22qmcfv%k}lfR)u*qVJqf zDl!`q|wX@ovDT5-uX&&rjy?$;k3z8waGWCkCvXX{S0 zCAuxT8pkR~)1hPcUOuD0F<3%n}{wESp6z#d0=o@^ZRUTd5{Ls7%P{D9KE1 zB*s!|<|{mt$EMs}nnoS4Lts3)BVi@7yU-H~Z1~XJUFUKv-@M5&4ReRXd*yQ#9zy#B zOIo`u+-0*#Uc~}=7@gWxx5*9*D;;<|mm;0zN95C(1@F3i&tDJB0@OPp^?Z7*#96XY zy4x^JD>@Zb9AZ1j+tlu+#Qg(2>e_L*9`6sm=!f}@7cdy1pLgJrMTE}W*yy{$)E>x$GtgZ_ zx_Px_FOcDNPqp5DC8!4j%iIFw^_;PEq#}O8#}amdU04ni5ta_?XwH(ZNMZ)8vbRkM z@G)&#yF#yp#U@o#&>{@VVQRn+iDTz!EXFR^FUeNu=n&FlO`_znq<(%#Cfqv`+jZ>BKA$8KOtFdZK0Cf1 z)<72kh1QL)aUwUiA+!pc>gm`9Hx4Dlv_xH>88ou#c~w{)W)tgS4*CF)chUR=Z`AVw zOr<4!aN_NDZs2G!w%)#}Hxlk4i1tiv^U-dP(O%IjOkKG-0n?(b=0N1yb5Fe4Yi3dK z7M=aFP$PPuu!Tz$Q5#bsLD|zJdA+Yd=x;MlI>$Jm`j9LZ>|fqd+V597`~siP0K;Eu z62AkbwXYAj;Z4sh?ODCapq8>}T#4?tu5IM?goqvaGJE0qQ%=FV+F_#J%c8oHx#7vM zb)u&~=B`|gKhyutqA;q3M2sWH0gvp5uWxx-z#pHJ6QNYPYxR~Cjf{_BTs2rPmOa=X zhGY0cNnVN5VeTC&xQ7{9Re1(jjDM2qBt$Y{(I=$xVk65p_xJ|YkHy)5SMdR7MZ<3F zTVCG@4(CdG?=xe+$t&#-Y8ADS*V34zn>B@7b9;Q+721!^iV#5qZO$`zDg*Lgp{~;- zo(1qa(*SPbFCo&Ko^f|bb#uAW1t@N!It|q%>HZv&4vX4uYVT87s8~Az0#}*7Ci5^E z{^$%&lo_PY;VBa`Jl5L#KeBXoJ2nKVYXXkZvxnK_I9)Z$v5nIJb&210ASR%kiHPg~ zb4rCN>+@?&lx=-iG}6t)Zz62a)H~^Lt)F5O6b4 zJN655%p#geRmg=Y`1+GC`lf{JDnH+<2z5|9Nd;Vg+x-yA7N4-tH_k~HqBd#|dvkxi zXtao>;Vjm}_OnZ>ra8T{Ksalt-!b9qqOYc6P3wUIi&oh^*gEdsMAwtE7Ygb*heO4n zT1V=M$LA*rg2k8|GHnZYM%MYzv!~t6CGL^*M0f}{>?2q!v+t(h<+I6Qn;*HqZZBj3 zCU}Oy0(#G@@Yo|>s;Q)gOmaw=B5?6Gf-@fiZhrD#HysGaYn$=Df6;TaF-u+)r=jPs zwm~Kf-ADs&a&G0X>tHoTXX%;3jW1n&G|DxBR}pPQz??JvDjvEjfHTsXWe`b5O(4M> zq4-#hD#QFxYPmpx+2^f8&qXDGNK`u(Ka0?x2|Num1}bF-gDtC-Df?!T;tMF9_Z@4Bn_-mfZg*l&~n`P*sYWw;x@;wDbC z)(@J;S(~-*x{7U^_n&U@UUBxBNHjH=aI0;s(%$q!`(&?D-!rE5c8T7UpuXmOG z=ule1g*ICZA%C&x?h#-XfBdrJchSIWYKAb zMSPhM(9Qv>3;2aieqtnsB>h7LTg7ElCX^{540ZN#O|?ls*ZU&Qxzs$5tvOlzjL*Z) zYqq?mQnO&XljD#w=@S{WB~E8jwli1B0oMpm8>-% zs>-EM8#?o!LyV>Gb7^Q(SwpK=L_T$8#aw6@)ZXwg646);<24RDM(D2gFp!v4_UF(! zNx3z#6W@vtOm}4oQ+HGc|13f&%#`+#eyrlj4_@#IfT@;Q^FGxt8;s=mmE!{YygM+e z;_>JVw;z>i>MavSP`yHp+VyJK^-B)?F{%}4DS@-m!Cdb#dw>p-w02n0d2_wPt4czy z9Y!wEK2q|wU0a6OyR8% zhdy*F2DG*&9ea1w@88t}BC=QB-V+oNRcr-T7(tlWvcaJUE=uVZLbAQ$Z7!mi>B ziVvUAQWnY8-UUtzK=10K6#Wv~*BE2kWWb@#IYb*;BYzZLY0vZ{MErrIGw!-(T5KLp z`N?1@Lv0&L*`K+!ZZIU$>5sgli~I3r@GP{4ZNWt+^_47%w2qPOr6<2nPQ3x#F7jCl z&Z+(E?t}VKAOwVnJ&e*Qg7a=>S>bLBb_HcOjXWN|6B%mbc*xCt1>HjOD*p=&*EJo! z28o3NgXReBn+QLvMtO;XZz0$^pFMaEXVU+RZZ+QoV@L8*EDvH>@9vgHd1JX3;P>S- zYrX5Eb1Q!(`L7;euiwcQ>h9VLGd-9=pp~k?#5!CSia*x5SS*^9Yok4fhacre|By6D zpRMypiBE-UF>)Sx-?d~}hO+xv;lvbL8BI=pJ$#8vkVtH4rR9WX*XuL;?yEeksf}IX zGU9iH2AWpr3hYM%$VPlwr|X-?c9e(N@QI`=v(PT@qo>vIPblr+lb;XC^NS|p%;Ttx z*u9BZ_V%d(L5f6JPhVX6;vsjEs17hsFn!NOV-~QgtRH{Tj}EQ`D&M5$B#`5ybd#>F zLIRO_7}CkXul8S`$L<(EBVd}n2_0ShyfZqAXtFz8Rjf&;PLddl=BJ(=c!Wc{g5NKU zy4hW~!QSSLb?JNE2?eAoO%^a`cqEa)D-9Mp-T!_(Hh&oXcH}(JVTH9v`n?lAQJsga zF%wsZg+@EBS2{TUBaO-=rifACEL8Qf+@7z2Sbv9-X8%y{IKkzvEe_0(<#Egn@mkmK zEpW7GnhYuoKJIW_Cc>h{Mptdwv}h~xX(NwEdsBn9#IELW1epyf2Fvz$m^;4%X|q}R z{dSt?(V6oODGPUrZwFcxFk_P$Y!Vs)*PjT!IDT3NQDT60XGeT+Xn_|wKJc}?aS#y> zbRxOMTcp(1&@(c@m2kYYzQK5$Nj(MB*xHul8E`L7(O zlBeIr)5#VyYVv<(v#ZtnHZ61i)P=6}6E{X-3S;09pikT3@rWInNgiJTMKUO(D`3-6 zNtO=mh2IzGS!@_1kL2j`fVy%Hyq5^P*ThoLB*_h5qSLbl(eVn6me`kjBe}Zw&A!;%!rSSiR9TF_UMg}!E01m;nST{@k^pAuCQ+V{=N5ou)*=IuTrr2 zhtK0CyJ~}}>8qBs+oAwNJVF-|%OzidYcWHHZ;bDD_Tjtv~!UI zRywmXs0#@cYk&vJz0lr=uM*~3RR&y*1FmWeS;V?tw{9qZK{`1Eq}+?^&#G1VE-7G9 z%*Ce-iROFEJav zoSpAt^(7*3&1f?y&}B;ZFJs8{YKvyTE94_G5riqZ^3U^y{{rn?Ureu{yAO3U&)vrs zezf>CXtKaIXp42Ja-3 z`ps6j9^R#a8$3a-J6HXMfP!+ER~uZeze$K%o=+Cq+_$)~O0Xo40F+Womk zhN^y{&U)DAaXd$mcsEIIvG7EF0g)-&Q;A7UWE7~R-;`xW)$?3zR=>NBeFY`tqLpyF zsKef9=<#jthei4M-0}CWiUEun%{|T>tqafC)MeQ1+Wu~syn9U{(;%KzTBEqeD2d+~ z0g7iJ$uTScRkL^|Na~n^m3W{R?1?*gqJ>LKWwwl6)#VmIyA1^kNoeJaU@WN_?jUYS z#u@f}r@m=GN{%2Y?JrTcExbx)M2j=Grr#t#LEyoq$9hD=shrQuds2a(hv!PJvnv7B z{%8wA0e7I&n8Ju0v0qg56o2*|AF9OVp`JG2;O>=|YvFxi;xgc%EUxKQ@eE^N$n2SW z5K`9-Zgno{L4TtYd1eA`AL1yg%L?Q!t74kJnb1nO;HT~XI;0Io3A9a|x}K0{u7Abc z(SduAw8Y9!K_IDds2bG3lK&c=Qbc-mY%MZfHg+}Tl0a57|PR`>|n_l_9649;gF8j*)aq+{~zD=0;PcY;hi!1}zes~srIadYjR1qM>P5rYRHgrRL zyoJ7^=(n~t^ln*XTc&jEMl?_A7huPMHWD`p zH1IgqyE$#BG1%t7Hi9x4j0f>=dL;*=A(f%8-JH2(eI#%}91WCq&R*We{GCyuDS(A|k z@=b!-go2QhPu#>Zj*ciQgzI-|=S1x=bD!FhejDalsI%z`TUnv`{0ZFH$Qw$&XB_eht}(GLKHuv}aDd*vX#pSzlw^ zL9G7{L4xJrr6#dJ>~B=+we41Qy$3zJYm-i0ZC--be4(Hd0-LOg zUJKI`9o1Pko6+rVYrYBIW6Vs>#hKLJ-6MiuuWI-tjm^t-`G=p&U)u|uKM3#yF2i^K zfGLN=!;a+iaYwnMvXVByP5ly=AlBrwc`&JV)3`wW(Fx|}M|<4~BKnNs56{`cE%VV;JkIZPd_obdd+fLUNRI4;#=TA08CHT7 zlG0F}9`?Y~y7rJ^_TOWg{4bv8k-}Dsjke$dR1nUbu{yI7nbaTy!&o?I;;X*%GL*K9Y(2yKKXeqc<1=6gB@OL5= zDyZ&PUya{JCaU8Ex{C3yL8jmzVj#+e0^?EVnZ2l!=8>OeCZ?~u)8*|cvcOPd8Fv)S zBIv)fzlpPwo%Ai;mtAV6-=&h+wZpTsiR<(*%a6EuAg zrSlZ#cK{$J;RjUxmN>%l^_G&K|7?gzsg@1wnS-yI#`uxXvUpbcAF_`sXN3 zhM;%lN|O~yAK}|T`#jn}u zB3w3!1edA-0m-=7w+Op`F5Jz~t=+9iwQDBD*{k2__Sy#_hhj?Q;X1l>*GZ!oa4*p2 zJAt7kLI5?R;e>v8ohx)RsmDkBb(Pjor_F$BFGOfnYQorkVp4!I!e!}2LTkHaJd0)n zDxzXQCpb@PoOn~Qi=f}udE&Ql6#A)el2;U3C98Bzl!!Q@jiUI(m5@%5Emzcd;0$Ifol+w4@MX$C7_arssu(7Tl9ABTf} z^t@&XIWhfs+ip|c!v|aiGn{*u@S}LB!ALOBv0+btfl?!Y_)2|ZYMk=@CE4p-_mc=G zp`(<}1~8Z$k2W;~-;ruY+(tkqZ`#tTAfu<~q0xT+phe6t;nzei3a+kz_+YA>a#*l5 zm|oOWabAO0ty|K~pjY6M8$3UyK|e;1MZ*32f!9t4E$_+z96^adY#CZyWDXb`UqGI- z_7ELtOd!@^QIT<$C@o8VR)K4FwDPNG=T~a^NDZ$pt1U!@w>a|yJWm7`tdT1cSt&vF z4gNjt8mt`1KL;CvFDJeBjTBuk%@>=1Cfop%Jv~2a<2Z_aRr3ITvcNMKADiMp##c05_fb6aH53p^E*`X&T zkwWzv#0uyq5BQ8PxdiLV_`>LK*f+!0VJ)8#Y{$so4y1F8P(X;~OCBLA&i z_g6qPWmj3V`YZH#vcv1n15Yb}B=#A$UUAh8;_L${_2VT%y$mam`qv#5BLAb)2tRkN zvB}rD_&NT(%{5NSU}8~DYSoLSiD<7GKm1j;3@b0v&HlT_F}`+P)BVG?MYnSXAq4mg zpL$b|ajF;I7?e?>WG9HI>;_}c;m_KyQcpITcLn)p)g2VMHg?zIUUjNpsaY8LS6RhB z?rHUjg6+_xA_bg%zrp8h7i4zg4Fd~*Uokr#&BB^v>Prmf50BH-?%y{s+92lq;D(tw zG5PcHvq2l;6=qtVdIaCiNn4Ju?}nv2LaS!^moPcfvQBOLeF@#*n3-E={}uzvnF7qb zgd8SR+r@Bo_xVvivcnJkar4~ZU&IEK+yal8a>LRo2(7}g1Ay%?yI|f)GC>4-9|@=C zXZIa{-nW3fMDAwd-+O5Y`9rZ#*K;c?_f;t3LnWSpD!A4KnyDf38y1my?AqsD?pIUL zPRZ&ixZvfe2H84~$Tu7gNLyuhTm~;BBi$V1(*A9qPtcm+Q+8QU8OB4z9EWC^mHU>6 zf_Brr+<3Dd*>4wBkB7J)ldQWSR4lXJ&@z`4?or*Sjz{(>I@MHG_Lx{3BW?CT4P`dv zBVP(*X_kOLYWAx2=50&xNUI__hw(;UdpMQ17X@Pe>fseV)|<{3{kXU3)m5Kxq?ZPz z5a=X71!ia1wBwD9+oqa0%D6b}>9x~k^B{Rk;MrO7n5Dne*A+|Kdu>8{9ak~dKRKp! zG#a(|9_DtBpc<}Fv43Srf^sr(TU%@}F?envz8O5qT-sP9J9vK7Zu8jVuisHB>S)-> ze|{pb*gdJSPhrI&sfz3sRyY9qZD-XjhW{)0pm{oaGL7N1kvecIBbjgUcOYvI&R|qkV~9Pt+%^$Qe?;~sKlHh8J^?1+Jv>>We@NlOswgsgiNW* zJr3mt(C!rO^LKt@?ZEvMeb9@kKsv1?d53|TOao@bRv=U(&5xLNb`9J35L#G7(x5et z;arZ7BH-*a6|}6Mu5NV3{E?BKI_P&FyP$iZJ#5Ze|JJN}Y;oUq+}Fe_YX`!KuisXv zltpD1w7clypoXLDDtIi?Cqr@cG&|j^7DGitPN1H7F^GDf+wjwdDN!rYQT%Z8+O7hZ zXO}LP8aT*aEE(dxs@g?UH-5X$InlOsf3ICp--U}pb}wr z8cSx>phu|6tkvjGtp@deN5KxB!(vP@c{=<$mz;1_O%Kc?tX4NY+kCdU0|K*KE#~=1 zOGZ}QiGmild||>GI(mz_Ji3L2CE8dDT?TER@5k2D=RE+2&I!{x4Z_?R@ap-9xXp#Z)Rb2O6q&xA0Y>%fsM+s?z&I6f>v zI(=Q;*pQ5bs4Bb=+z!U!s~d|0CQ>y& zqMhKX_A9_me=2ABL#qzf-nJaWXvF*fiAxgT&o`(o_KLELK?*ZcJn>01p@T$x@Ca^K z8A-d^0zZBSsn^yUy(iI*&?8|6ZP%-$CYQ*p@_&WWm+QGfc9wc5Iigg4nyG}+CzmHql6^8*O-8qj9FpMbjcfPTOEx-sPyJug^f}V8_|L&-k>w1r?acX$?qDEk^aszHZ1(K*6qdBuRm#NWx~&7`i?To3B;p#(`7nCkOE zB*P^&q;X;h`(_uN>yRqY#`p*c7iMY?U@L-$k4op9EUfqCldzw=L@p9@Ab-(!vQzBO z-q!7Mgj|yILC(m~SLNVU6z~to>ltXZoXXJrolz?A>a?uVfv3eihvuc#aas<3x6)YU zcY;eUoBlSMmL?FrltsD&f3aukex@C#6bA9a;i5$>4}OOh4H|_GW@Lb^z(75qIe&?0a*Hv*0(0#_>o-zGmP^gCA zO(8C55ZBNli7JQ-kY$G=DGj-PZlcNjA{3|A>1}+_<&2xM1%6uwpE2D9?#2pd-yolBk6N9|ihFU9b$W9a$1r&Aj4S5y)d-SK1fo!! zddk>Yay8EYAFda9h%lx1qikB*<(EXAh2NW7T>(6|Cs5yaRV1#yALY&Tr^)64d6< zsTo3&zP+rSIrH`sq>T$`732D(dOlFOLv)w_@ZfVR}%kgX%|Z?|1E%dkF$g;a4_=bwcmX3chI=y2|fe)If( zUu^|qk;*q@a3+WQVfKo|a6=Eu&@?qBwh#3B2#9Y`Z;S(f$4S==Zf)+@-TTw_arXzY z{`=S+#$4S?&kLQ0l46P(t1aPs^Vlu3y~gd_ZIoF6eKSK7gA3%Y2&(y4QpT$l!1l(K zh;P1T#!w!05Giwzf75UbM~Yc>&i?gp{*~R)@P!_>Uk%!;a`GImk59+=i~USt9z8kN zjQ9Of9YN!-)iBJ$f7dewQ#h&wW($-qF)Y~P?U_TQ#|_$I$+c_Pr~1FB!J*4sD%M-A z(h8itDsT>iyrE^v$sGjg}N`i7I8+Q?$1n(io^N2 zcj7}F=8?iPMx4ZT@7CH8yhC~2(n<7lIwpl#Rj96caIjzA@P_X3X#KE1pJU7aAO zx5T;Y=4sxd8xxPFkJr*CDfV^HF$IqwgPYGxV78npjwBD`23b1O0C+!UbV;3L>C-rb<>>H_q{H}ltN?@)WI9t$o6GLo50e2agr8aEhCO;ERNUZ(( zLEKAAKZ3m4adKmSd3l;r(i&gwPOd(FgI_c`H27k4f4G?$hg05DiI>|48)rFXPv!(XE*P*Y@%>W6bciKMxPs1N z;C{Cm-eL<}HqnMk64(;lj5AHxx>Qf~~uy71K?)OxpvzEfdV`3jvH@fbSX`gW1q0W&x*JAtGO|&gYZ9lihXQv|(!Oys{5Tqi9s= zgG)hGcwKLEd=C%pglz7ggP!+3%9>4lNdR#Ap-HLwgs=VySHJ%Kr|^34peFGlNrDy- z4Mh_6M~Fa$?JP~v=*{dDIat)wcHtXh2$pa&gJyLemrk|tn`;JFyBueuP{W_fQBTmB z_ruRlW|KE{S62*Vg#hZU1N9qqebH?s3sEY`4PF6e^(h>j$NgCqL_a^JzT`z%pk1N>Fl4}v0VD&R6 z(cFTMi)(VJIo!&*t#)mEJp+fCz%~P6w&&W00GtD?=glfYXJ1s55qnV^bLA03tL9Z+ zpUUNwMhuBB(mPBpRa@QRuhpK%cIw2a&wWeIebCcxiwYaNZ7uZ*#c$hITG5*|xZdGw zS}C`qwzZ<0IkOTY-FjXqToAJslwW4s-j`o8WW%`VDfgv~XD}#=>E6!uV>C@`1nBP1 z3ICq}G!D!0V~KBsQrSHaI=fPLXzDIO6mY4(5Ou>}c6$__hZ}m*Jl`?5$ zanueX=Dd+WmD1|a&Yn0@{^zCz*rGRG4gr|%1}Ke(6@BY@Q>7GN2MzdYnq!sQZC*L7 zTevgRum*yG0B%AWwvXn!k@y#@=#+Jf{dO)YDv*yZI~_YmVmDC`VF%iYucKmg3Take z5&CPxN?cb1jy$*E)Av6wPrtoaEq$gGR0eVs+pk_>^EPg^T{ygb1@u1~?V|;T&-Y&5jB^+!$Qa~d;)Ep<{6eW{ zk4(pIuQy}M>mr=Rz^_XDFE8&rEI(hhS<)fP?|*!m+BlHWomd?ZK+dwUr=jwYNi{VQ z_gBJaJ~pfWEpxw5yhT1U2lK>n0Q9LeJB7-^Fco06efKf>%eu|-}-yqMg-w7a}AJ?aOl%fT^K6h$`@E^k- zbe?y;u>%{1rOPLl&XwV?n(ZN+rDl)Ad$srusCxukM3=+G&~CXL(y>+a;VOMTW>-9w z!Zg?px;QsWmd%-}#*n(Wb13p6xaGpdm2Pz&!8|4U_jB0S>FM|P$t?>f!vI@~FN>Uj zEUq|vhJ5y_Ht?k3kLUD83)DIDRrmBe+XbgW%UAzlJ~rCwk-Hy%8!Og#$Uj{)H)hGH zdIM^wp<}-F(sN*h``umhb{~<4cOH}NsQWs&AsJE<_ZAY*rGqS(( z%!xh_-)0?7lL=Q3+i?o~{|jc0mx@vxE)oVD?HL-rssI1{@+SCPi{<7evo(;vxkK)L zb+cTA{Kmrt5{p%?qy)Y@Fy{37mOU~8KJiX=fT+}0w(N(CuM=N}892(&0K9W;4=5m~ z;8+<-Xt=*(?o@e-74nb<^K8815pDjDLA$7XcTe%yE%6nzC&>2z;>GAGPbCni6gjv; zf8F@vTsdv@EcyA$E%NvqTV(2(;W)8rtH|L7x$e`K=myZ#(F-7tug;Vz1FX}b?U&A< zCJ(f>%Z(4dBwzT$O8MH=3#5cfZ-6AeEPP)d9Qacm8|ctQz+ir>A8d)vZA-oG%^y|+ za#tiwp6Z#EAD_jNHRGzb12^msmd=vN!wLZ!x=d*Rxc&Ib`T`CbkAN z`%CzG!$aFAs*2?H4+@`!f3U+<3SQwdmi<+A^1_Bas^LuP0Ekl>qZ6@Z;)-UXpRyI58cS#6bpURbguPfz5&- zt`17Z>=c6d_dmY$)U(8GCPX`YogC$FfyIF_MY;8Y)8&Exd0IaE^qZK;(Ifx+;YB#+ z40QlYi?cg8g(}w2$vhf66ac$R>u0cymKJAj@DT!Sr5o{?+lG<<+yC+gj+w_OEYFLL z(JH5P#hEJa;>4eCKe`&n(&3y{=v{4Fr~Gc$F_EJUa^ak@a_O0qb;kYpmu-Qc*@v@u z+=*5281rYU27JP^Zd)->%D^*q8B)-xs%OY!zq0<+W7mHOKJ<~la-d4Sa^EvJ!3n#Y zf@uOg{qW_bg>uK`XDc5+%Mxox;hGUdycqSfV!;9V)#}ai__4?3TeHVwu)bJLx1?qM z($QLxs^$QTJsf>@4lDy*Hh+9|BYN~2xqb08xp?MS7|mLCNwA=wK_~y4m$qRx*GBlx z&sS+EZ*2~Qqpn?LIMqUq;Ap}NXHU_7@Sy0%s114?@N>WdIk{wIn?hW5)--tn&#r!G z4Zs|q&H!^BPN8N$1QaQ@A9hPz4Q!sG?i8bORJ>9Ss*EpzUk=mV5avx9E#Jnc_-{Vg zBR9^(0Nk_+^*oy{jPb=dojltlSI^!2p|v3sN^k&j^m*Qzo^BGc&u zY05$}dKnA--o&NrW8TO@nSi=U<)?QXfiAO&nDe}Sbf-^t_HmyK40tXWQ79vGGGhM9 z;ymiHV~S-mzJy*nQj6mm`L%+RmM|ZGGv-`>1eoXVSlvKe>;=@uALiD>r~ISqmdfHO z}G0TKOYnF3x7%BeIvB~;S- zNedbt7mZ}WBF%uJG0@-0=WMWzfuYL4p5i>o-Yt3K`a{3zL^yx`~KK z-kP{b3w-5b=8Buf9h@)uZbE(0l5~=do@&{t065k>s0*x9A`bza?F&-0uIy-&aU@&l zLCnECCgQifep>N0(0~~qO&CX^Zsb6A{FjUEHQZ|k&fdhA48J`2gxcm@fyI(`ViRt%+vh)E2PgrF!jQ~mVleO8mM|4k7Nf8kh5_1hRPA* ztADS-yfa42oOT0OY^o`Sr})68lhT+*Kr6#wmd3C^AXC5M1UIKVoOp417Ab9Scw%0D z+Mzk0YydaB^@fjVG@`u^m5P31mwUK4z2Q3?&1la}d9>#0*s2?bV#m@Q+shZO940R| znwV(E*@o<8A5=h2#wjc#$R_~i*F3aF#^TF8VPw^1Acb_axv+z>|QIz>ooQQ=iJ!!XL2C?Lp0S!Lz`p_88v1 zCjn0ao&*M`1cn@tvl@(b?ynO6!WREC@6MBeCjn0aLqh_w?J6`#%B-yJAd7sEuC0000PbVXQnQ*UN; zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBUzfKW_SMF0SD004FX0D1rbeFFe~1pt3; zZfCc=sHm;2t+24Lw6wIky1Kr;zJr3nhlaw5h{KAB#g2`}kdMcbk;s*l z%9)tUnV8IUFZ%FWHq zz`)+{)Y#bA-QC^6!QjWo+9?7?d{^?@a5(5=;-t5>GbaI_VDoV@bLHb_4V`f`Styti-G8>!9+A2NyY+2TnUsguJj#zE&y3W z_Kj$eOg|ND;CL3}m4(iCYMMsAKMa6cT+IayvSZ6w#Wksj(+ju}$@_nZc6|W#g-9~q zm(|}aWEhMrK9JOc4~_h5Xa3>Lw@+XHEkfuDKq}h6YQm5~GDUyJGi3V3#g#TmB}Xi8 zXKq434>&Y?dL0HT6u3T z07|il6tCjw9^+o0Ztg~^R!dM6g&RxUwY7XQkQHw~9|N+2kgR_x2;9Bu4nS4P)T_xD zP9_P(L=cSR{K9f1xIm{~3G7>Rr7pa^7zGL$T7S_W3Yh8wfU4qjg3WmUAV9}U^c9&M z)^Y9sx@E?k?yJRf#$VGwk7#95teBzk)XZJd{I!3>0td5xvVqO1P0oT*K9C_%7rn zV1Pjic&uxI7XYz3nuE<2{vjNIWl0%zym-o5|px7jWUl{$(QWG#Tsgy zjz9YmvNIsdFsyX|ke3Rzf|SDLzPwXpGk!PE&rZ+Io0?;wp~o>225#dRYIXn+BgR#V z6fj;5R2;&YcJkZfnOotZ0peb74ux;cJl;Oh959?%mCt;20?>#Y5E4oaBKeiFb@;Qb znJ}V}1tR@`kc1i(p4mFHKIi-W z?|7g)LW?CRV9m=68?i+aZ>2)L99(+Z3Zy|bg-nwfH$~FcRaL>aP7f5v7r)h4ZRO8v-4AJ4O z(sb5xFtA~&!VYtVme0RBb_Y-mNbxO>mx7oAfi^>>*$y0_v!;gT1M3@R# z_sR!WqN~whCYBV&-wBbZ?qtsnFQy6#481g5#uUA^r7=_hv)&2GLK+tn!9L#nA46mE zKIV_iA8^iqKV!;{nF<1P9ywtqUBitaDSyfZj({0p3cn-!M($~-nvwi%gaDNFoD_oa z?_No1HYz2{m57k#HSe^Onogr5Bm1UVD3SH;zjX;-JRL#oyfVJg?2wl4C#8i2-v2aj zS&~Zk7X&va9?h@KQBO_5Dq#H5Wbt_W+1mQfDfTW*8g$_LYC{Y2$=ZTXOf?*ke+dXL0ZXT_BPNiSyN ztt3nvhqhG0+4Hq)9U*)TGus%u5@JnX%}obZZ_pxVKUiyXsB~bUC4dXZqmtD8oq~0IKnTBrWn8FiB>RLpo(=fJoqP7XTY97R&HuX8`d;xw^l2gR25=L0R?@ z`)P_f?pB%YvPoDksMPHMN~{LM8L9Qz4)hoMsRX9nC_ZLsBDvx>UP(y6w*#QfCzUvX zRv^3sedHfT5b^@R(sp=TFt?Z;z(I_;pSsxx#7DmJAA?Z><%@iHFh~S}- zkrIrTmdxA15mmB@Xe?c3NMAwO>V#aafcZK{M?|M#ifPAJv$~RvN9txWE&F9aM&V0G z32XxM847slgwZK0W|2S}g*?R`kx+kuCr*!Vj=**~I-+&cu0KYIZ>xDlLj4zU_x5l+ z3%GmHTmJxv_CmP}2B<3tpV<Q-fNSUPjs_6&@B>Co zJh5ZuKBw~`AR{ECZNpq*)Ce9}0jPXAeAbUz<)i|(Fuojg*MW9j1mW<#`OU4Lzn@Tm z6(|t`zf=(~SOMhtg{LFa2{8*c0C+QC_${E9W=OFE6~aNori593Ep(SlMqL@OE23GO zDo$+4@U^EIrWZL4ehJ!lx-wuh#ddk_^iseJ?6qvS%merpv?xSw49MKjvMh6WoLNx7 zrYC^xir~YQjtn5K@q)OC%h1pRSr*t3OQdSe!whu*9duw|aIoEoCT|qOSwJ-U@afOb zpF9|W(82%@KwtKA1@NL112X*mid3J5pwLkhf1|&F(`F5zy%~q14s7uNmOUNO4wSjB zrV|5n)n~p1^Bqe)sMGq1f6O%GRRg@_02qs%ZUFv(ZB2J#pasAAhFbG-S9S)yI_C-_?9vN!)DCCeEal&{1swGhNC%e z02+SMTI=cT2bc&R&^N=>{ao(M-5nc3JpxB!@l-W6-kpKBsIv|{F;nOMGhGKrxKZ)X zAxKjq8Idb~NCCjAutGHs?EqQ{GBN?^&kkSdIzYwzqKA-1(*w~?;F!=6 zEISn@xu7a^*c7W7@(0I&0`8OR2>|A5rlF$e3h;>20R{YoJC0>KbBE`R(HR@v>K$zqltz$s1wwoiY z727k1bpR-If$>I9FTD$lo-#jh)99o=^s|>Nw?Z{Hq-RZA`V=!y7$68L}-=LrwHG&J%Hb#!CYzR!(JO+721+XF0z?xlbd9vvBg)M=ZiNG$3PkB0jJq>ec> z8fF1PY~ZZK+#3|tMPZxJD>Eyg(=flxT?2Ftk`65Eg=xD?fyPN>;F5vZpQ@DH0jLpS zMTeQ9_fn1AS~cf-$8!5qy+!DMOJ`mZcI#Y?@vR!Sg<5O1jD+QUINI!HPibsP+_Lbs zceBo|U%;LE`q;UL=mMaa7A@fw^wX-f^$TP^5#q-$uXeYmlr1?pP|%5yR#yb9i}Oz` zbh)Wj!)s0mIx%9}iM4^54^+F|Rm(z}d@TmZ`19iURqJ+7M*#Ib)w1{o9rj{%W`MYX zXBPcCNUA;nRQpyf9K7s>3hIsl8pL|HJ@ns81cknm1;J4mDBUqY=qDL+r}5o%U`480 z5M15^fIA$`vh+s#R9!NG~SoeRf}tysM#09;F}&IZ8} z#^as!xo_`HPfgu@IJds@w&}93mZV_O;xz$yTBZADU#njYEHyp^fa-oXTO88dcQxx> z_`bdC>VR1@U-y5AjH3Sle;9zqN?)puKh@z&2TIXv{%6}><>LUrjmmX~FGQt-j|9NT zW*XfLUyDiU2L6};8qr{;i~n+Ltkyo74w#SJiv_ZNuFD;KWF_P}3+&N#)eGffP|AHW z0LCt$$kJ-bLFyD&HJU1__!9%DYI76KX#X0Q@h24?F%5U;syvRS$oOKpp@9002ovPDHLkV1iW&!*l=u diff --git a/README.md b/README.md index dcc11a7..b7f39af 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,26 @@ -# KeineAhnung Bot +# SomeBot -Der KeineAhnung Bot ist ein simpler Discord Bot mit der D++ Discord Library! +The SomeBot is a simple Discord Bot with the D++ Discord Library! ## Features -Der Bot ist komplett Modular und erlaubt das Erstellen von verschiedenen Konfigurationen fuer verschiedene Bot-Tokens. Libdpp erlaubt ausserdem Auto-Sharding und startet automatisch die richtige Anzahl an Shards. +The bot is completely modular and allows the creation of different configurations for different bot tokens. D++ also allows auto-sharding and automatically starts the right number of shards. -### Module +### Modules - * Captcha: Gibt Leuten eine bestimmte Rolle, wenn sie ein Captcha beantworten - * Embedmaker: Erstellt Benutzerdefinierte Embeds - * Fun: Ein paar unnoetige Spasskommandos - * Globalchat: verbindet Textkanaele auf verschiedenen Servern miteinander - * Info: Zeigt Informationen ueber den Bot, den Server und Benutzer an - * Lastseen: Trackt Benutzeraktivitaet und zeigt an, wann ein Benutzer zuletzt aktiv war - * Levels: Vergibt Levels mit benutzerdefinierter Geschwindigkeit und vergibt Rollen - * Management: Erlaubt, den Bot von einem Zentralen Discord Server zu verwalten (Bans, Globalchat Moderatoin, etc.) - * Ticket: Erlaubt das erstellen und verwalten von Support-Tickets und speichert Logs + * Captcha: Gives people a specific role when they answer a captcha + * Embedmaker: Creates user-defined embeds + * Fun: A couple of fun commands + * Globalchat: Connects text channels on different servers with each other + * Info: Displays information about the bot, server and user + * Lastseen: Tracks user activity and shows when a user was last active + * Levels: Assigns levels with user-defined speed and assigns roles + * Management: Allows the bot to be managed from a central Discord server (bans, global chat moderation, etc.) + * Ticket: Allows the creation and management of support tickets and saves logs +## Configuration -## Konfiguration - -Hier ist eine Beispielkonfiguration: +Here is an example configuration: [ { @@ -29,35 +28,38 @@ Hier ist eine Beispielkonfiguration: "management_guild_id": "1073679409623617556", "token": "ODAyODYzNTU0MjY4NjI2OTY1r7zrQOCRI", "owner_id": "609486822715818000", - "private": false + "private": false, + "modules_mode": "blacklist", + "modules": ["Globalchat"] }, { "id": "exxo", "token": "MTA0ODk3NjA3MDA4NDQxMTM5MgsCvig8wEA", "owner_id": "", "private": true, + "modules_mode": "whitelist", "modules": ["Captcha", "Levels", "Info", "Globalchat", "Lastseen", "Ticket", "Embedmaker"] } ] -Und hier eine Erklaerung fuer die verschiedenen Optionen: +And here is an explanation of the different options: { - "id": "", - "token": "", - "owner_id": "", - "management_guild_id": "", - "private": , - "modules_mode": "", - "modules": "", - "command_alias_mode", "" + "id": "", + "token": "", + "owner_id": "", + "management_guild_id": "", + "private": , + "modules_mode": "", + "modules": "", + "command_alias_mode", "" } -Die Konfiguration muss als `config.json` im CWD des Bots gespeichert werden. +The configuration must be saved as `config.json` in the bot's working directory. ## Building -Der Bot wird mit CMake gebuiled und benoetigt einige Submodules: +The bot is built with CMake and requires some submodules: git submodule update --init --depth 1 --recursive mkdir build @@ -65,8 +67,8 @@ Der Bot wird mit CMake gebuiled und benoetigt einige Submodules: cmake .. make -j$(nproc) -Das Executable wird unter `./KeineAhnung` erstellt. +The executable is created under `./SomeBot`. -## Hinweise +## Usage -Das Executable benoetigt keine Argumente, aber wenn es Probleme mit den Slashcommands gibt (nach dem deaktivieren von Modulen), koennen sie mit `--reregister-commands` zurueckgesetzt werden. +The executable takes no arguments, but if there are problems with the slash commands (after deactivating modules), they can be reset with `--reregister-commands`. diff --git a/ideas-keineahnung-scripting.md b/ideas-keineahnung-scripting.md deleted file mode 100644 index a6c61f3..0000000 --- a/ideas-keineahnung-scripting.md +++ /dev/null @@ -1,51 +0,0 @@ -# KeineAhnung Bot custom commands - -## Language - -Custom commands are going to be written in Luau - -### Functions - -Commands are going to have to have the following global functions: - -#### build_metadata() - -Required to return a table with the following pieces of information: - - - `publisher_id`: Example: "tuxifan" - - `package_id`: Example: "moderation" - - `command_id`: Example: "warn" - - `version_num`: Version number used internally, example: 011806 - - `version_str`: Version string visible to user, example: "1.18.6" - - `disabled`: True if this commands only purpose is to provide/manage storage, example: false - - `parameters`: List of parameters accepted by command, to be thought of - -#### initialize() - -Called when command is added to a guild. Should be used to Initialize storage. - -#### on_call(context, parameters) - -Called when someone executes the function. Must be used to process the command. - -#### deinitialize() - -Called when command is removed from guild. Must not interact with the Discord API. Should be used to clean up used non-private storage. - -## Storage - -### Private - -There is going to be private storage, only accessible by the command itself. Removed on command removal. - -### Secret - -There is going to be secret storage, accessible by all commands on the same guild that have the same secret assigned to them by the user. Removed on removal of last command with same secret. - -### Shared - -There is going to be public storage, accessible by all commands on the same guild, identified the `publisher_id`, `package_id` and the `command_id`. Removed on removal of last command with the same identification. - -### Global - -There is going to be protected storage, accessible by all commands on the same guild, identified the `publisher_id` and the `package_id`. Removed on removal of last command with the same identification. diff --git a/luau b/luau deleted file mode 160000 index 36e0e64..0000000 --- a/luau +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 36e0e6471567eb0d41d80e54b73db791efbb808a diff --git a/main.cpp b/main.cpp index ba9e560..7891142 100644 --- a/main.cpp +++ b/main.cpp @@ -11,15 +11,11 @@ #include #include #include -#ifdef __linux__ -# include -# include -# include -#endif #include #include #include +#include @@ -349,7 +345,7 @@ int main(int argc, char **argv) { logfile << " -------- Cut here -------- " << std::endl; bot.start(); } catch (std::exception& e) { - bot.cluster.log(dpp::loglevel::ll_critical, "BOT STOPPED DUE TO UNCAUGHT EXCEPTION: "+std::string(e.what())); + bot.cluster.log(dpp::loglevel::ll_critical, "BOT RESTARTING IN RESPONSE TO LEAKED EXCEPTION: "+std::string(e.what())); goto start; } // Remove bot from bot list diff --git a/modules/captcha.cpp b/modules/captcha.cpp index a064928..69f6573 100644 --- a/modules/captcha.cpp +++ b/modules/captcha.cpp @@ -71,11 +71,11 @@ class Captcha { msg.set_channel_id(channel_id) .add_embed(dpp::embed() .set_title("Captcha") - .set_description("Bitte verifiziere dich!") - .set_footer("Du hast 1 Minute und 30 Sekunden lang Zeit, das Captcha zu lösen.", "https://whatemoji.org/wp-content/uploads/2020/07/Alarm-Clock-Emoji.png")) + .set_description("Please verify yourself!") + .set_footer("You have 1 minute and 30 seconds to solve the captcha.", "https://whatemoji.org/wp-content/uploads/2020/07/Alarm-Clock-Emoji.png")) .add_component(dpp::component() .add_component(dpp::component() - .set_label("Ich bin kein Roboter") + .set_label("I'm not a robot") .set_type(dpp::component_type::cot_button) .set_emoji("✅") .set_style(dpp::component_style::cos_primary) @@ -111,10 +111,10 @@ public: " UNIQUE(id)" ");"; - bot->add_chatcommand(Bot::ChatCommand({"captcha"}, "Lege einen Captcha-Kanal fest", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "channel", "Kanal in dem das Captcha gestellt wird", true)).add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Rolle die nach dem Lösen gegeben wird", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"captcha"}, "Configure a captcha channel", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "channel", "Channel in which the challenge is shown", true)).add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Role given after solving challenge", true))), [&](const dpp::slashcommand_t& event) { // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_channels | dpp::permissions::p_manage_roles)) { - event.reply(dpp::message("Du brauchst die Kanal- und Rollenverwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need channel and role management authorization to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Update database @@ -124,18 +124,18 @@ public: set_guild_settings(event.command.guild_id, channel_id, role_id); send_embed(channel_id); // Send reply - event.reply(dpp::message("Okay.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done.").set_flags(dpp::message_flags::m_ephemeral)); }); bot->add_chatcommand(Bot::ChatCommand({"no_captcha", "kein_captcha", "captcha_stop"}, "Deaktiviere das Captcha"), [&](const dpp::slashcommand_t& event) { // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_channels | dpp::permissions::p_manage_roles)) { - event.reply(dpp::message("Du brauchst die Kanal- und Rollenverwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need channel and role management authorization to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Update database reset_guild_settings(event.command.guild_id); // Send reply - event.reply(dpp::message("Okay.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done.").set_flags(dpp::message_flags::m_ephemeral)); }); bot->cluster.on_guild_member_add([this](const dpp::guild_member_add_t& event) { // Ghost ping @@ -166,7 +166,7 @@ public: bot->cluster.request("https://captcha-api.akshit.me/v2/generate", dpp::http_method::m_get, [this, event](const dpp::http_request_completion_t& ccb) { // Check for error if (ccb.status != 200) { - event.reply("Fehler beim laden des Captcha, versuche es später nochmal oder kontaktiere die Leitung."); + event.reply("Error loading the captcha, try again later or contact the management."); return; } // Deserialize JSON @@ -186,7 +186,7 @@ public: // Create message dpp::message msg; msg.set_channel_id(event.command.channel_id) - .set_content("> Läuft ab in \nBitte gebe die Zeichen in diesem Bild ein:") + .set_content("> Expires in \nPlease enter the characters shown in this image:") .add_file("captcha.jpeg", jpeg_binary_data) .set_flags(dpp::message_flags::m_ephemeral); // Upload captcha @@ -202,7 +202,7 @@ public: uuids.erase(res); } // Delete message - event.edit_original_response(dpp::message().set_content("Die Zeit ist abgelaufen.")); + event.edit_original_response(dpp::message().set_content("The time has expired.")); }).detach(); }); } @@ -228,7 +228,7 @@ public: auto res = uuids.find(uuid_key); if (res == uuids.end()) { // UUID isn't known - event.reply("Bitte erstelle ein neues Captcha.", true, autodelete_message_cb()); + event.reply("Please start a new captcha.", true, autodelete_message_cb()); return; } // Lowercase captcha @@ -244,26 +244,26 @@ public: // Verification successful, assign role bot->cluster.guild_member_add_role(event.msg.guild_id, event.msg.author.id, settings.second, [this, event](const dpp::confirmation_callback_t& ccb) { if (ccb.is_error()) { - event.reply("Verifizierung erfolgreich, aber Rolle konnte nicht zugewiesen werden.", true, autodelete_message_cb()); + event.reply("Verification successful, but role could not be assigned.", true, autodelete_message_cb()); } else { - event.reply("Verifizierung erfolgreich, Rolle wurde zugewiesen.", true, autodelete_message_cb()); + event.reply("Verification successful, role has been assigned.", true, autodelete_message_cb()); } }); // Remove UUID from list uuids.erase(res); } else if (ccb.status == 429) { // Too many tries - event.reply("Zu viele Versuche. Erstelle ein neues Captcha.", true, autodelete_message_cb()); + event.reply("Too many attempts. Create a new captcha.", true, autodelete_message_cb()); uuids.erase(res); } else if (ccb.status == 404) { // Not found (shouldn't ever be triggered) - event.reply("Captcha ist abgelaufen. Erstelle ein neues Captcha.", true, autodelete_message_cb()); + event.reply("Captcha has expired. Create a new captcha.", true, autodelete_message_cb()); } else if (ccb.status == 401) { // Wrong response - event.reply("Falsche Antwort. Versuche es erneut.", true, autodelete_message_cb()); + event.reply("Wrong answer. Try again.", true, autodelete_message_cb()); } else{ // Other error - event.reply("Ungültige Server-Antwort "+std::to_string(ccb.status)+". Erstelle ein neues Captcha.```json\n"+json_body+"``````json\n"+ccb.body+"\n```", true, autodelete_message_cb()); + event.reply("Invalid server response "+std::to_string(ccb.status)+". Create a new captcha.```json\n"+json_body+"``````json\n"+ccb.body+"\n```", true, autodelete_message_cb()); uuids.erase(res); } }, json_body, "application/json", {{"Content-Type", "application/json"}}); diff --git a/modules/custom.cpp b/modules/custom.cpp deleted file mode 100644 index c87607e..0000000 --- a/modules/custom.cpp +++ /dev/null @@ -1,619 +0,0 @@ -#include "../bot.hpp" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - - - -class LuaExecution { - std::unique_ptr state; - - static int assertionHandler(const char* expr, const char* file, int line, const char* function) { - printf("%s<%s>(%d): ASSERTION FAILED: %s\n", file, function, line, expr); - return 1; - } - - struct BaseContext { - dpp::snowflake guild_id; - Bot *bot; - } baseContext; - struct Context : public BaseContext { - const dpp::message *msg; - }; - -public: - LuaExecution(Bot *bot, dpp::snowflake guild_id) : state(luaL_newstate(), lua_close), baseContext{guild_id, bot} { - // Set assertion handler - Luau::assertHandler() = assertionHandler; - - // Get state - auto state = this->state.get(); - - // Enable exceptions - luabridge::LuaException::enableExceptions(state); - - // Load initial libraries - luaL_openlibs(state); - - // Do bindings - using namespace dpp; - luabridge::getGlobalNamespace(state) - .beginClass("Context") - .addProperty("guild_id", &Context::guild_id, false) - .addProperty("message", &Context::msg, true) - .endClass() - .beginClass("Snowflake") - .addConstructor() - .addProperty("integer", &snowflake::operator uint64_t) - .addProperty("string", +[] (const snowflake *o) -> std::string { return std::to_string(*o); }) - .addProperty("creation_time", &snowflake::get_creation_time) - .addProperty("worker_id", &snowflake::get_worker_id) - .addProperty("process_id", &snowflake::get_process_id) - .addProperty("increment", &snowflake::get_increment) - .endClass() - .beginClass("Message") - .addProperty("id", &message::id, false) - .addProperty("channel_id", &message::channel_id, true) - .addProperty("guild_id", &message::guild_id, false) - .addProperty("author", &message::author, true) - .addProperty("member", &message::member, true) - .addProperty("content", &message::content, true) - .addProperty("components", &message::components, true) - .addProperty("sent", &message::sent, true) - .addProperty("edited", &message::edited, true) - .addProperty("mentions", &message::mentions, true) - .addProperty("mention_roles", &message::mention_roles, true) - .addProperty("mention_channels", &message::mention_channels, true) - .addProperty("attachments", &message::attachments, true) - .addProperty("embeds", &message::embeds, true) - .addProperty("reactions", &message::reactions, true) - .addProperty("nonce", &message::nonce, true) - .addProperty("webhook_id", &message::webhook_id, true) - .addProperty("stickers", &message::stickers, true) - .addProperty("file_name", &message::filename, true) - .addProperty("file_content", &message::filecontent, true) - .addProperty("reference", &message::message_reference, true) - .addProperty("interaction", &message::interaction, true) - .addProperty("allowed_mentions", &message::allowed_mentions, true) - .addProperty("type", &message::type, true) - .addProperty("flags", &message::flags, true) - .addProperty("pinned", &message::pinned, true) - .addProperty("tts", &message::tts, true) - .addProperty("mention_everyone", &message::mention_everyone, true) - .addProperty("is_crossposted", &message::is_crossposted) - .addProperty("is_crosspost", &message::is_crosspost) - .addProperty("suppress_embeds", &message::suppress_embeds) - .addProperty("is_source_message_deleted", &message::is_source_message_deleted) - .addProperty("is_urgent", &message::is_urgent) - .addProperty("has_thread", &message::has_thread) - .addProperty("is_ephemeral", &message::is_ephemeral) - .addProperty("is_loading", &message::is_loading) - .addProperty("is_thread_mention_failed", &message::is_thread_mention_failed) - .addProperty("is_dm", &message::is_dm) - .addFunction("add_component", &message::add_component) - .addFunction("add_embed", &message::add_embed) - .addFunction("send", [cluster = &baseContext.bot->cluster] (const message *o) -> std::optional { - try { - return cluster->message_create_sync(*o); - } catch (...) { - return {}; - } - }) - .endClass() - .beginClass("MessageRef") - .addProperty("message_id", &message::message_ref::message_id, true) - .addProperty("channel_id", &message::message_ref::channel_id, true) - .addProperty("guild_id", &message::message_ref::guild_id, false) - .addProperty("fail_if_not_exists", &message::message_ref::fail_if_not_exists, true) - .endClass() - .beginClass("MessageInteraction") - .addProperty("id", &message::message_interaction_struct::id, false) - .addProperty("type", &message::message_interaction_struct::type, true) - .addProperty("name", &message::message_interaction_struct::name, true) - .addProperty("user", &message::message_interaction_struct::usr, true) - .endClass() - .beginClass("MessageAllowedRef") - .addProperty("parse_users", &message::allowed_ref::parse_users, true) - .addProperty("parse_everyone", &message::allowed_ref::parse_everyone, true) - .addProperty("parse_roles", &message::allowed_ref::parse_roles, true) - .addProperty("replied_user", &message::allowed_ref::replied_user, true) - .addProperty("users", &message::allowed_ref::users, true) - .addProperty("roles", &message::allowed_ref::roles, true) - .endClass() - .beginClass("Embed") - .addProperty("title", &embed::title, true) - .addProperty("type", &embed::type, true) - .addProperty("description", &embed::description, true) - .addProperty("url", &embed::url, true) - .addProperty("timestamp", &embed::timestamp, true) - .addProperty("color", &embed::color, true) - .addProperty("footer", &embed::footer, true) - .addProperty("image", &embed::image, true) - .addProperty("thumbnail", &embed::thumbnail, true) - .addProperty("video", &embed::video, true) - .addProperty("provider", &embed::provider, true) - .addProperty("author", &embed::author, true) - .addProperty("fields", &embed::fields, true) - .endClass() - .beginClass("EmbedFooter") - .addProperty("text", &embed_footer::text, true) - .addProperty("icon_url", &embed_footer::icon_url, true) - .addProperty("proxy_url", &embed_footer::proxy_url, true) - .endClass() - .beginClass("EmbedImage") - .addProperty("url", &embed_image::url, true) - .addProperty("proxy_url", &embed_image::proxy_url, true) - .addProperty("height", &embed_image::height, false) - .addProperty("width", &embed_image::width, false) - .endClass() - .beginClass("EmbedProvider") - .addProperty("name", &embed_provider::name, true) - .addProperty("url", &embed_provider::url, true) - .endClass() - .beginClass("EmbedAuthor") - .addProperty("name", &embed_author::name, true) - .addProperty("url", &embed_author::url, true) - .addProperty("icon_url", &embed_author::icon_url, true) - .addProperty("proxy_icon_url", &embed_author::proxy_icon_url, true) - .endClass() - .beginClass("EmbedField") - .addProperty("name", &embed_field::name, true) - .addProperty("value", &embed_field::value, true) - .addProperty("is_inline", &embed_field::is_inline, true) - .endClass() - .beginClass("Reaction") - .addProperty("count", &reaction::count, false) - .addProperty("me", &reaction::me, true) - .addProperty("emoji_id", &reaction::emoji_id, true) - .addProperty("emoji_name", &reaction::emoji_name, true) - .endClass() - .beginClass("Attachment") - .addProperty("id", &attachment::id, false) - .addProperty("size", &attachment::size, false) - .addProperty("filename", &attachment::filename, true) - .addProperty("description", &attachment::description, true) - .addProperty("url", &attachment::url, false) - .addProperty("proxy_url", &attachment::proxy_url, false) - .addProperty("width", &attachment::width, false) - .addProperty("height", &attachment::height, false) - .addProperty("content_type", &attachment::content_type, false) - .addProperty("ephemeral", &attachment::ephemeral, true) - .addProperty("owner", &attachment::owner, false) - .endClass() - .beginClass("Sticker") - .addProperty("id", &sticker::id, false) - .addProperty("pack_id", &sticker::pack_id, true) - .addProperty("name", &sticker::name, true) - .addProperty("description", &sticker::description, true) - .addProperty("tags", &sticker::tags, true) - .addProperty("asset", &sticker::asset, true) - .addProperty("type", &sticker::type, true) - .addProperty("format_type", &sticker::format_type, true) - .addProperty("available", &sticker::available, true) - .addProperty("guild_id", &sticker::guild_id, false) - .addProperty("sticker_user", &sticker::sticker_user, false) - .addProperty("sort_value", &sticker::sort_value, true) - .addProperty("file_name", &sticker::filename, true) - .addProperty("file_content", &sticker::filecontent, true) - .addFunction("get_url", &sticker::get_url) - .endClass() - .beginClass("StickerPack") - .addProperty("id", &sticker_pack::id, false) - .addProperty("stickers", &sticker_pack::stickers, true) - .addProperty("name", &sticker_pack::name, true) - .addProperty("description", &sticker_pack::description, true) - .addProperty("sku_id", &sticker_pack::sku_id, true) - .addProperty("cover_sticker_id", &sticker_pack::cover_sticker_id, true) - .addProperty("banner_asset_id", &sticker_pack::banner_asset_id, true) - .endClass() - .beginClass("Channel") - .addProperty("id", &channel::id, false) - .addProperty("name", &channel::name, true) - .addProperty("topic", &channel::topic, true) - .addProperty("rtc_region", &channel::rtc_region, true) - .addProperty("recipients", &channel::recipients, true) - .addProperty("permission_overwrites", &channel::permission_overwrites, true) - .addProperty("available_tags", &channel::available_tags, true) - .addProperty("icon", &channel::icon, true) - .addProperty("icon_url", +[] (const channel *o) { return o->get_icon_url(); }) - .addProperty("owner_id", &channel::owner_id, true) - .addProperty("parent_id", &channel::parent_id, true) - .addProperty("guild_id", &channel::guild_id, false) - .addProperty("last_message_id", &channel::last_message_id, true) - .addProperty("last_pin_timestamp", &channel::last_pin_timestamp, true) - .addProperty("permissions", &channel::permissions, true) - .addProperty("position", &channel::position, true) - .addProperty("bitrate", &channel::bitrate, true) - .addProperty("rate_limit_per_user", &channel::rate_limit_per_user, true) - .addProperty("default_thread_rate_limit_per_user", &channel::default_thread_rate_limit_per_user, true) - .addProperty("default_auto_archive_duration", &channel::default_auto_archive_duration, true) - .addProperty("default_sort_order", &channel::default_sort_order, true) - .addProperty("flags", &channel::flags, true) - .addProperty("type", +[] (const channel* o) { return o->get_type(); }, +[] (channel* o, channel_type v) { o->set_type(v); }) - .addProperty("is_nsfw", +[] (const channel* o) { return o->is_nsfw(); }, +[] (channel* o, channel_type v) { o->set_nsfw(v); }) - .addProperty("is_locked_permissions", +[] (const channel* o) { return o->is_locked_permissions(); }, +[] (channel* o, channel_type v) { o->set_lock_permissions(v); }) - .addProperty("is_text_channel", &channel::is_text_channel) - .addProperty("is_dm", &channel::is_dm) - .addProperty("is_voice_channel", &channel::is_voice_channel) - .addProperty("is_group_dm", &channel::is_group_dm) - .addProperty("is_category", &channel::is_category) - .addProperty("is_forum", &channel::is_forum) - .addProperty("is_news_channel", &channel::is_news_channel) - .addProperty("is_store_channel", &channel::is_store_channel) - .addProperty("is_stage_channel", &channel::is_stage_channel) - .addProperty("is_video_auto", &channel::is_video_auto) - .addProperty("is_video_720p", &channel::is_video_720p) - .addProperty("is_pinned_thread", &channel::is_pinned_thread) - .addProperty("is_tag_required", &channel::is_tag_required) - .addProperty("mention", &channel::get_mention) - .addFunction("add_flag", &channel::add_flag) - .addFunction("remove_flag", &channel::remove_flag) - .addFunction("add_permission_overwrite", &channel::add_permission_overwrite) - .addFunction("get_user_permissions", luabridge::overload(&channel::get_user_permissions)) - .addFunction("get_member_permissions", luabridge::overload(&channel::get_user_permissions)) - .addFunction("get_icon_url", &channel::get_icon_url) - .addFunction("get_members", &channel::get_members) - .addFunction("get_voice_members", &channel::get_voice_members) - .addFunction("create", [cluster = &baseContext.bot->cluster] (const channel *o) -> std::optional { - try { - return cluster->channel_create_sync(*o); - } catch (...) { - return {}; - } - }) - .addFunction("edit", [cluster = &baseContext.bot->cluster] (const channel *o) -> std::optional { - try { - return cluster->channel_edit_sync(*o); - } catch (...) { - return {}; - } - }) - .endClass() - .beginClass("User") - .addProperty("id", &user::id, false) - .addProperty("username", &user::username, true) - .addProperty("avatar", &user::avatar, true) - .addProperty("avatar_url", +[] (const user *o) { return o->get_avatar_url(); }) - .addProperty("flags", &user::flags, true) - .addProperty("discriminator", &user::discriminator, true) - .addProperty("mention", &user::get_mention) - .addProperty("is_bot", &user::is_bot) - .addProperty("is_mfa_enabled", &user::is_mfa_enabled) - .addProperty("has_nitro_full", &user::has_nitro_full) - .addProperty("has_nitro_classic", &user::has_nitro_classic) - .addProperty("has_nitro_basic", &user::has_nitro_basic) - .addProperty("is_discord_employee", &user::is_discord_employee) - .addProperty("is_partnered_owner", &user::is_partnered_owner) - .addProperty("has_hypesquad_events", &user::has_hypesquad_events) - .addProperty("is_bughunter_1", &user::is_bughunter_1) - .addProperty("is_house_bravery", &user::is_house_bravery) - .addProperty("is_house_brilliance", &user::is_house_brilliance) - .addProperty("is_house_balance", &user::is_house_balance) - .addProperty("is_early_supporter", &user::is_early_supporter) - .addProperty("is_team_user", &user::is_team_user) - .addProperty("is_bughunter_2", &user::is_bughunter_2) - .addProperty("is_verified_bot", &user::is_verified_bot) - .addProperty("is_verified_bot_dev", &user::is_verified_bot_dev) - .addProperty("is_certified_moderator", &user::is_certified_moderator) - .addProperty("is_bot_http_interactions", &user::is_bot_http_interactions) - .addProperty("has_animated_icon", &user::has_animated_icon) - .addProperty("formatted_username", &user::format_username) - .addFunction("get_avatar_url", &user::get_avatar_url) - .addFunction("kick", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const user *o) -> bool { - try { - cluster->guild_member_kick_sync(o->id, guild_id); - return true; - } catch (...) { - return false; - } - }) - .addFunction("ban", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const user *o, unsigned delete_message_settings) -> bool { - try { - cluster->guild_ban_add_sync(guild_id, o->id, delete_message_settings); - return true; - } catch (...) { - return false; - } - }) - .endClass() - .beginClass("Member") - .addProperty("nickname", &guild_member::get_nickname) - .addProperty("roles", &guild_member::get_roles) - .addProperty("guild_id", &guild_member::guild_id, false) - .addProperty("user_id", &guild_member::user_id, false) - .addProperty("avatar", &guild_member::avatar, false) - .addProperty("avatar_url", +[] (const guild_member *o) { return o->get_avatar_url(); }) - .addProperty("communication_disabled_until", &guild_member::communication_disabled_until, false) - .addProperty("joined_at", &guild_member::joined_at, false) - .addProperty("premium_since", &guild_member::premium_since, false) - .addProperty("is_communication_disabled", &guild_member::is_communication_disabled) - .addProperty("is_muted", +[] (const guild_member* o) { return o->is_muted(); }, +[] (guild_member* o, bool v) { o->set_mute(v); }) - .addProperty("is_deaf", +[] (const guild_member* o) { return o->is_deaf(); }, +[] (guild_member* o, bool v) { o->set_deaf(v); }) - .addProperty("is_pending", &guild_member::is_pending) - .addProperty("has_animated_guild_avatar", &guild_member::has_animated_guild_avatar) - .addProperty("user", &guild_member::get_user) - .addFunction("get_avatar_url", &guild_member::get_avatar_url) - .addFunction("mention", &guild_member::get_mention) - .addFunction("kick", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild_member *o) -> bool { - try { - cluster->guild_member_kick_sync(o->user_id, guild_id); - return true; - } catch (...) { - return false; - } - }) - .addFunction("ban", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild_member *o, unsigned delete_message_settings) -> bool { - try { - cluster->guild_ban_add_sync(guild_id, o->user_id, delete_message_settings); - return true; - } catch (...) { - return false; - } - }) - .addFunction("edit", [cluster = &baseContext.bot->cluster] (const guild_member *o) -> std::optional { - try { - return cluster->guild_edit_member_sync(*o); - } catch (...) { - return {}; - } - }) - .endClass() - .beginClass("Guild") - .addProperty("id", &guild::id, false) - .addProperty("name", &guild::name, true) - .addProperty("vanity_url_code", &guild::vanity_url_code, true) - .addProperty("roles", &guild::roles, false) - .addProperty("channels", &guild::channels, false) - .addProperty("threads", &guild::threads, false) - .addProperty("emojis", &guild::emojis, false) - .addProperty("voice_members", &guild::voice_members, false) - .addProperty("members", &guild::members, false) - .addProperty("welcome_screen", &guild::welcome_screen, true) - .addProperty("icon", &guild::icon, true) - .addProperty("splash", &guild::splash, true) - .addProperty("discovery_splash", &guild::discovery_splash, true) - .addProperty("banner", &guild::banner, true) - .addProperty("owner_id", &guild::owner_id, true) - .addProperty("afk_channel_id", &guild::afk_channel_id, true) - .addProperty("application_id", &guild::application_id, true) - .addProperty("system_channel_id", &guild::system_channel_id, true) - .addProperty("rules_channel_id", &guild::rules_channel_id, true) - .addProperty("public_updates_channel_id", &guild::public_updates_channel_id, true) - .addProperty("widget_channel_id", &guild::widget_channel_id, true) - .addProperty("member_count", &guild::member_count, false) - .addProperty("flags", &guild::flags, false) - .addProperty("max_presences", &guild::max_presences, false) - .addProperty("max_members", &guild::max_members, false) - .addProperty("shard_id", &guild::shard_id, false) - .addProperty("premium_subscription_count", &guild::premium_subscription_count, false) - .addProperty("afk_timeout", &guild::afk_timeout, true) - .addProperty("max_video_channel_users", &guild::max_video_channel_users, false) - .addProperty("default_message_notifications", &guild::default_message_notifications, true) - .addProperty("premium_tier", &guild::premium_tier, false) - .addProperty("verification_level", &guild::verification_level, false) - .addProperty("explicit_content_filter", &guild::explicit_content_filter, true) - .addProperty("mfa_level", &guild::mfa_level, true) - .addProperty("nsfw_level", &guild::nsfw_level, true) - .addProperty("flags_extra", &guild::flags_extra, false) - .addProperty("discovery_splash_url", +[] (const guild *o) { return o->get_discovery_splash_url(); }) - .addProperty("icon_url", +[] (const guild *o) { return o->get_icon_url(); }) - .addProperty("splash_url", +[] (const guild *o) { return o->get_splash_url(); }) - .addProperty("banner_url", +[] (const guild *o) { return o->get_banner_url(); }) - .addProperty("is_large", &guild::is_large) - .addProperty("is_unavailable", &guild::is_unavailable) - .addProperty("is_widget_enabled", &guild::widget_enabled) - .addProperty("has_invite_splash", &guild::has_invite_splash) - .addProperty("has_vip_regions", &guild::has_vip_regions) - .addProperty("has_vanity_url", &guild::has_vanity_url) - .addProperty("is_verified", &guild::is_verified) - .addProperty("is_partnered", &guild::is_partnered) - .addProperty("is_community", &guild::is_community) - .addProperty("has_news", &guild::has_news) - .addProperty("is_discoverable", &guild::is_discoverable) - .addProperty("is_featureable", &guild::is_featureable) - .addProperty("has_animated_banner", &guild::has_animated_banner) - .addProperty("has_auto_moderation", &guild::has_auto_moderation) - .addProperty("has_support_server", &guild::has_support_server) - .addProperty("has_animated_icon", &guild::has_animated_icon) - .addProperty("has_banner", &guild::has_banner) - .addProperty("is_welcome_screen_enabled", &guild::is_welcome_screen_enabled) - .addProperty("has_member_verification_gate", &guild::has_member_verification_gate) - .addProperty("is_preview_enabled", &guild::is_preview_enabled) - .addProperty("has_animated_icon_hash", &guild::has_animated_icon_hash) - .addProperty("has_animated_banner_hash", &guild::has_animated_banner_hash) - .addProperty("has_monetization_enabled", &guild::has_monetization_enabled) - .addProperty("has_more_stickers", &guild::has_more_stickers) - .addProperty("has_role_icons", &guild::has_role_icons) - .addProperty("has_seven_day_thread_archive", &guild::has_seven_day_thread_archive) - .addProperty("has_three_day_thread_archive", &guild::has_three_day_thread_archive) - .addProperty("has_ticketed_events", &guild::has_ticketed_events) - .addProperty("has_channel_banners", &guild::has_channel_banners) - .addProperty("has_premium_progress_bar_enabled", &guild::has_premium_progress_bar_enabled) - .addProperty("has_invites_disabled", &guild::has_invites_disabled) - .addFunction("get_base_user_permissions", luabridge::overload(&guild::base_permissions)) - .addFunction("get_base_member_permissions", luabridge::overload(&guild::base_permissions)) - .addFunction("get_permission_overwrites", luabridge::overload(&guild::permission_overwrites)) - .addFunction("kick", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild *, snowflake user_id) -> bool { - try { - cluster->guild_member_kick_sync(user_id, guild_id); - return true; - } catch (...) { - return false; - } - }) - .addFunction("ban", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild *, snowflake user_id, unsigned delete_message_settings) -> bool { - try { - cluster->guild_ban_add_sync(guild_id, user_id, delete_message_settings); - return true; - } catch (...) { - return false; - } - }) - .addFunction("edit", [cluster = &baseContext.bot->cluster] (const guild *o) -> std::optional { - try { - return cluster->guild_edit_sync(*o); - } catch (...) { - return {}; - } - }) - .endClass() - .beginClass("IconHash") - .addProperty("first", &utility::iconhash::first, true) - .addProperty("second", &utility::iconhash::second, true) - .addProperty("string", &utility::iconhash::to_string, &utility::iconhash::set) - .endClass() - .beginClass("Permission") - .addProperty("integer", &permission::operator uint64_t) - .addFunction("has", &permission::has) - .addFunction("add", &permission::add) - .addFunction("set", &permission::set) - .addFunction("remove", &permission::remove) - .endClass() - .addFunction("fetch_guild", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] () -> std::optional { - try { - return cluster->guild_get_sync(guild_id); - } catch (...) { - return {}; - } - }) - .addFunction("fetch_channel", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const dpp::snowflake id) -> std::optional { - try { - auto channel = cluster->channel_get_sync(id); - if (channel.guild_id != guild_id) { - return {}; - } - return channel; - } catch (...) { - return {}; - } - }) - .addFunction("fetch_user", [cluster = &baseContext.bot->cluster] (const dpp::snowflake id) -> std::optional { - try { - return cluster->user_get_sync(id); - } catch (...) { - return {}; - } - }) - .addFunction("fetch_member", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const dpp::snowflake user_id) -> std::optional { - try { - return cluster->guild_get_member_sync(guild_id, user_id); - } catch (...) { - return {}; - } - }) - .addFunction("fetch_message", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const dpp::snowflake id, const dpp::snowflake channel_id) -> std::optional { - try { - auto msg = cluster->message_get_sync(id, channel_id); - if (msg.guild_id != guild_id) { - return {}; - } - return msg; - } catch (...) { - return {}; - } - }); - } - - std::string execute(const std::string& source, const dpp::message *msg) noexcept { - // Get state - auto state = this->state.get(); - - // We need bytecode - std::string bytecode = Luau::compile(source, Luau::CompileOptions()); - - // We can then load that bytecode - if (luau_load(state, "code.lua", bytecode.data(), bytecode.size(), 0) != 0) { - size_t len; - const char* msg = lua_tolstring(state, -1, &len); - - std::string error(msg, len); - lua_pop(state, 1); - - return error; - } - - // ... and start executing - int status = lua_resume(state, NULL, 0); - - // Check status - if (status != 0) { - // Error! - std::string error; - - // Get error string - if (status == LUA_YIELD) { - error = "Thread yielded unexpectedly"; - } else if (const char* str = lua_tostring(state, -1)) { - error = str; - } - - // Append backtrace - error += "\nStack backtrace:\n"; - error += lua_debugtrace(state); - - // Clean up and return - lua_pop(state, 1); - return error; - } - - // Now, we call into the code - luabridge::LuaRef on_call = luabridge::getGlobal(state, "on_call"); - if (!on_call.isCallable()) { - return "'on_call' is not callable"; - } - try { - luabridge::LuaResult result = on_call(Context{baseContext, msg}); - } catch (const luabridge::LuaException& e) { - return e.what(); - } - - // Finally, everything seems alright - return {}; - } -}; - -class Custom { - Bot *bot; - -public: - Custom(Bot *_bot) : bot(_bot) { - bot->add_messagecommand(Bot::MessageCommand({"Evaluate"}, "Evaluiere Lua code"), [&](const dpp::message_context_menu_t& event) { - // Check that user has the correct permissions - if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_administrator)) { - event.reply(dpp::message("Du musst Administrator sein, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); - return; - } - // Continue in new thread... - std::thread([this, event] () { - // Construct lua execution - LuaExecution exec(bot, event.command.guild_id); - - // Get code to execute - const auto& msg = event.get_message(); - const auto& code = msg.content; - - // Start execution - auto error = exec.execute(code, &msg); - - // Report outcome - if (error.empty()) { - event.reply("Success."); - } else { - event.reply("Error:\n```lua\n"+error+"\n```"); - } - }).detach(); - }); - } -}; -BOT_ADD_MODULE(Custom); diff --git a/modules/embedmaker.cpp b/modules/embedmaker.cpp index 55f7199..781c516 100644 --- a/modules/embedmaker.cpp +++ b/modules/embedmaker.cpp @@ -19,19 +19,19 @@ public: Embedmaker(Bot *_bot) : bot(_bot) { bot->cluster.intents |= dpp::intents::i_guild_messages | dpp::intents::i_message_content; - bot->add_chatcommand(Bot::ChatCommand({"make_embed", "embed_erstellen"}, "Erstelle ein Embed", dpp::slashcommand() - .add_option(dpp::command_option(dpp::command_option_type::co_string, "title", "Titel", true)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "description", "Beschreibung", true)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "image", "Bild", false)) + bot->add_chatcommand(Bot::ChatCommand({"make_embed"}, "Create an embed", dpp::slashcommand() + .add_option(dpp::command_option(dpp::command_option_type::co_string, "title", "Title", true)) + .add_option(dpp::command_option(dpp::command_option_type::co_string, "description", "Description", true)) + .add_option(dpp::command_option(dpp::command_option_type::co_string, "image", "Image", false)) .add_option(dpp::command_option(dpp::command_option_type::co_string, "video", "Video", false)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "thumbnail", "Kleines Bild", false)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Farbe", false)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "footer_text", "Fußzeilen-Test", false)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "footer_image", "Fußzeilen-Bild", false)) - .add_option(dpp::command_option(dpp::command_option_type::co_string, "content", "Nachricht über Embed", false))), [&](const dpp::slashcommand_t& event) { + .add_option(dpp::command_option(dpp::command_option_type::co_string, "thumbnail", "Tiny picture", false)) + .add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Color", false)) + .add_option(dpp::command_option(dpp::command_option_type::co_string, "footer_text", "Footer text", false)) + .add_option(dpp::command_option(dpp::command_option_type::co_string, "footer_image", "Tiny footer image", false)) + .add_option(dpp::command_option(dpp::command_option_type::co_string, "content", "Text", false))), [&](const dpp::slashcommand_t& event) { // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_messages)) { - event.reply(dpp::message("Du brauchst die Nachrichtenwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need message management authorization to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get color @@ -49,8 +49,8 @@ public: // Send embed bot->cluster.message_create(dpp::message().set_content(strOrEmpty(event.get_parameter("content"))).set_channel_id(event.command.channel_id).add_embed(embed)); // Report success - event.reply(dpp::message(std::string("Das Embed wurde erstellt!") - +(color==-1U?"\n**Achtung:** Die Angegebene Farbe wurde nicht erkannt! Versuche es mit einem Farbcode erneut.":"")).set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message(std::string("The embed was created!") + +(color==-1U?"\n**Attention:** The specified color was not recognized! Try again with a color code.":"")).set_flags(dpp::message_flags::m_ephemeral)); }); } }; diff --git a/modules/globalchat.cpp b/modules/globalchat.cpp index de57fdb..e441d6d 100644 --- a/modules/globalchat.cpp +++ b/modules/globalchat.cpp @@ -75,7 +75,7 @@ class Globalchat { // Filter bad messages bool is_bad = is_bad_message(original_msg.content); if (is_bad) { - original_msg.content = "*Nachricht entspricht nicht den Richtlinien*"; + original_msg.content = "*Message does not comply with the rules*"; } // Get user info unsigned color = Util::get_color_of_user(original_msg.author); @@ -97,7 +97,7 @@ class Globalchat { try { guild_name = guildCache.at(original_msg.guild_id).name; } catch (...) { - guild_name = "Unbekannt (extern?)"; + guild_name = "Unknown (external?)"; auto [bots, bots_lock] = bot->get_locked_property>("main_all_instances"); for (auto bot : *bots) { try { @@ -113,7 +113,7 @@ class Globalchat { .set_thumbnail(original_msg.author.get_avatar_url()) .set_color(color) .add_field(original_msg.member.get_nickname().empty()?original_msg.author.username:original_msg.member.get_nickname(), original_msg.content+"\n⠀") - .set_footer(dpp::embed_footer().set_text("Gesendet von: "+guild_name)) + .set_footer(dpp::embed_footer().set_text("Sent from: "+guild_name)) ); // Process message reference std::string replied_to_primary_id; @@ -244,15 +244,15 @@ class Globalchat { } void set_globalchat_channel_topic(dpp::channel channel) { - channel.set_topic("Dies ist der Globalchat des *"+bot->cluster.me.username+"* Bots. Viel Spaß!\n" + channel.set_topic("This is the globalchat of the *"+bot->cluster.me.username+"* bot. Have fun!\n" "\n" - "**Regeln:**\n" + "**Rules:**\n" " **0.** Common Sense\n" - " **1.** Keine Links\n" - " **2.** Kein Trolling\n" - " **3.** Keine Diskussionen über Moderationsentscheidungen\n" - " **4.** Keine hitzigen religiösen Diskussionen\n" - " **5.** Keine ernsthaften Beleidigungen"); + " **1.** No links and invites\n" + " **2.** No trolling\n" + " **3.** No discussions about moderation decisions\n" + " **4.** No heated religious or political discussions\n" + " **5.** No serious insults"); bot->cluster.channel_edit(channel); } @@ -343,11 +343,11 @@ public: } }); - bot->add_chatcommand(Bot::ChatCommand({"set_gc", "globalchat"}, "Verwende den Globalchat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "channel", "Der Kanal in dem der Globalchat laufen soll", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"set_gc", "globalchat"}, "Set up the globalchat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "channel", "The channel in which the globalchat should be", true))), [&](const dpp::slashcommand_t& event) { auto channel_id = std::get(event.get_parameter("channel")); // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_channels)) { - event.reply(dpp::message("Du brauchst die Kanalverwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need the channel management permissions to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Update globalchat ID @@ -360,24 +360,24 @@ public: set_globalchat_channel_topic(ccb.get()); }); // Reply - event.reply("Der Globalchat läuft jetzt in <#"+std::to_string(channel_id)+">"); + event.reply("The globalchat is now set up in <#"+std::to_string(channel_id)+">"); }); - bot->add_chatcommand(Bot::ChatCommand({"reset_gc", "globalchat_stop"}, "Stoppe den Globalchat"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"reset_gc", "globalchat_stop"}, "Stop the Globalchat"), [&](const dpp::slashcommand_t& event) { // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_channels)) { - event.reply("Du brauchst die Kanalverwaltungsberechtigung, um dieses Kommando zu verwenden."); + event.reply("You need the channel management permissions to use this command."); return; } // Update globalchat ID unset_globalchat_channel(event.command.guild_id); // Reply - event.reply("Der Globalchat wurde gestoppt"); + event.reply("The globalchat has been stopped"); }); - bot->add_chatcommand(Bot::ChatCommand({"gc_color", "globalchat_color", "globalchat_farbe"}, "Setze Farbe im Globalchat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Farbcode oder Name", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"gc_color", "globalchat_color"}, "Set your color in globalchat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Color code or name", true))), [&](const dpp::slashcommand_t& event) { auto color_str = std::get(event.get_parameter("color")); auto color_code = Util::str_to_color(color_str); if (color_code == -1U) { - event.reply(dpp::message("Farbe ist ungültig. Versuche es mit einem Farbcode.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Color is invalid. Try a color code.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Set user color in database @@ -388,24 +388,24 @@ public: << color_code << std::to_string(event.command.usr.id); // Send success reply - event.reply(dpp::message().add_embed(dpp::embed().set_title("Deine Farbe im Globalchat wurde festgelegt!").set_color(color_code).set_image("https://singlecolorimage.com/get/"+Util::int_as_hex(color_code)+"/400x100")).set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message().add_embed(dpp::embed().set_title("Your color in the global chat has been set!").set_color(color_code).set_image("https://singlecolorimage.com/get/"+Util::int_as_hex(color_code)+"/400x100")).set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_chatcommand(Bot::ChatCommand({"gc_ban", "globalchat_ban"}, "Verbanne jemanden aus dem Globalchat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "user", "Benutzer", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"gc_ban", "globalchat_ban"}, "Ban someone from the global chat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "user", "Benutzer", true))), [&](const dpp::slashcommand_t& event) { auto user_id = std::get(event.get_parameter("user")); // Set user to banned db_add_user(user_id); ban_user(user_id); // Report success - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); }, bot->config.management_guild_id); - bot->add_chatcommand(Bot::ChatCommand({"gc_unban", "globalchat_unban"}, "Entbanne jemanden aus dem Globalchat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "user", "Benutzer", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"gc_unban", "globalchat_unban"}, "Unban someone from the global chat", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "user", "Benutzer", true))), [&](const dpp::slashcommand_t& event) { auto user_id = std::get(event.get_parameter("user")); // Set user to non-banned unban_user(user_id); // Report success - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); }, bot->config.management_guild_id); - bot->add_messagecommand(Bot::MessageCommand({"GC Ban"}, "Verbanne den Sender der Nachricht"), [&](const dpp::message_context_menu_t& event) { + bot->add_messagecommand(Bot::MessageCommand({"GC Ban"}, "Ban sender from the global chat"), [&](const dpp::message_context_menu_t& event) { const auto& target_message = event.get_message(); // Get message info auto messageinfo = GlobalchatExternal::MessageInfo::decode_message(target_message); @@ -413,36 +413,36 @@ public: db_add_user(messageinfo.author); ban_user(messageinfo.author); // Report success - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); }, bot->config.management_guild_id); - bot->add_messagecommand(Bot::MessageCommand({"GC Unban"}, "Entbanne den Sender der Nachricht"), [&](const dpp::message_context_menu_t& event) { + bot->add_messagecommand(Bot::MessageCommand({"GC Unban"}, "Unban sender from the global chat"), [&](const dpp::message_context_menu_t& event) { const auto& target_message = event.get_message(); // Get message info auto messageinfo = GlobalchatExternal::MessageInfo::decode_message(target_message); // Ban sender unban_user(messageinfo.author); // Report success - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); }, bot->config.management_guild_id); - bot->add_messagecommand(Bot::MessageCommand({"GC Nachricht löschen"}, "Lösche eine Nachricht im Globalchat"), [&](const dpp::message_context_menu_t& event) { + bot->add_messagecommand(Bot::MessageCommand({"GC Nachricht löschen"}, "Delete a message in the global chat"), [&](const dpp::message_context_menu_t& event) { const auto& target_message = event.get_message(); // Get message ID GlobalchatExternal::MessageInfo messageinfo; try { messageinfo = GlobalchatExternal::MessageInfo::decode_message(target_message); } catch (GlobalchatExternal::MessageInfo::DecodeError& e) { - event.reply(dpp::message("Fehler beim Dekodieren der Nachricht. Ist sie wirklich Teil des Globalchats?").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Error decoding the message. Is it really part of the global chat?").set_flags(dpp::message_flags::m_ephemeral)); return; } const auto& [author_id, primary_id, channel_id, guild_id] = messageinfo; // Check for error if (primary_id.empty()) { - event.reply(dpp::message("Ich konnte diese Nachricht nicht identifizieren. Möglicherweise ist sie kein Teil des Globalchats, das Embed wurde gelöscht oder sie ist zu alt.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("I could not identify this message. It may not be part of Globalchat, the embed may have been deleted or it may be too old.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Check if user is allowed to delete this message if (event.command.guild_id != bot->config.management_guild_id && dpp::snowflake(author_id) != event.command.get_issuing_user().id) { - event.reply(dpp::message("Du darfst diese Nachricht nicht löschen.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You must not delete this message.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Delete message everywhere @@ -471,14 +471,14 @@ public: return; } auto msg = ccb.get(); - msg.set_content(msg.content+"\n*(gelöscht)*"); + msg.set_content(msg.content+"\n*(deleted)*"); bot->cluster.message_edit(msg); }); } }; } // Report success - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); }); } }; diff --git a/modules/globalchat.hpp b/modules/globalchat.hpp index 446963f..2cb0ffd 100644 --- a/modules/globalchat.hpp +++ b/modules/globalchat.hpp @@ -7,7 +7,7 @@ struct MessageInfo { dpp::snowflake author, message, channel, guild; struct DecodeError : public std::logic_error { - DecodeError() : std::logic_error("Fehler beim dekodieren des Globalchat-Nachrichtencodes") {} + DecodeError() : std::logic_error("Error decoding the global chat message code") {} }; std::string encode() const; diff --git a/modules/info.cpp b/modules/info.cpp index 3b4c3c3..13c941c 100644 --- a/modules/info.cpp +++ b/modules/info.cpp @@ -67,26 +67,26 @@ namespace InfoExternal { dpp::embed generate_user_info_embed(const dpp::user& user) { dpp::embed embed; embed.set_title(user.format_username()) - .add_field("Benutzername", user.username, true) - .add_field("Tag", dpp::leading_zeroes(user.discriminator, 4), true) + .add_field("Username", user.username, true) + .add_field("Discriminator", user.discriminator?dpp::leading_zeroes(user.discriminator, 4):"None", true) .add_field("ID", '`'+std::to_string(user.id)+'`', true) - .add_field("Ist Bot", user.is_bot()?("Ja, "+std::string(user.is_verified_bot()?"":"aber k")+"ein verifizierter."):"Nein", true) - .add_field("Alter des Kontos", "(user.get_creation_time()))+":R>", true) + .add_field("Is Bot", user.is_bot()?("Yes, "+std::string(user.is_verified_bot()?"but not ":"it's ")+"a verified bot."):"Nein", true) + .add_field("Account age", "(user.get_creation_time()))+":R>", true) .set_image(user.get_avatar_url()); return embed; } dpp::embed generate_guild_info_embed(const dpp::guild& guild) { dpp::embed embed; - static const char *nfsw_level_texts[] = {"Nicht angegeben", "Ja, kann NSFW beinhalten", "Nein", "Ja, ist NSFW"}; + static const char *nfsw_level_texts[] = {"Not specified", "Yes, may contain NSFW", "No", "Yes, is NSFW"}; embed.set_title(guild.name) - .add_field("Beschreibung", guild.description.empty()?"Keine":guild.description, true) + .add_field("Description", guild.description.empty()?"None":guild.description, true) .add_field("ID", '`'+std::to_string(guild.id)+'`', true) - .add_field("Besitzer", "<@"+std::to_string(guild.owner_id)+'>', true) - .add_field("Alter", "(guild.get_creation_time()))+":R>", true) - .add_field("Ist NSFW", nfsw_level_texts[static_cast(guild.nsfw_level)], true) - .add_field("Mitlieder", std::to_string(guild.member_count), true) - .add_field("Rollen", std::to_string(guild.roles.size()), true) + .add_field("Owner", "<@"+std::to_string(guild.owner_id)+'>', true) + .add_field("Age", "(guild.get_creation_time()))+":R>", true) + .add_field("Is NSFW", nfsw_level_texts[static_cast(guild.nsfw_level)], true) + .add_field("Members", std::to_string(guild.member_count), true) + .add_field("Roles", std::to_string(guild.roles.size()), true) .add_field("Emojis", std::to_string(guild.emojis.size()), true) .set_image(guild.get_icon_url()); return embed; @@ -103,10 +103,9 @@ public: Info(Bot *_bot) : bot(_bot) { bot->cluster.intents |= dpp::intents::i_guilds; - bot->add_chatcommand(Bot::ChatCommand({"help", "hilfe"}, "Bekomme Hilfe"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"help"}, "Get help"), [&](const dpp::slashcommand_t& event) { dpp::embed embed; - embed.set_title("Kommandos") - .set_footer(dpp::embed_footer().set_text("Bot entwickelt von 353535#3535")); + embed.set_title("Commands"); for (const auto& command : bot->get_commands().chat_commands) { std::string nameCollection; switch (bot->config.command_alias_mode) { @@ -128,10 +127,9 @@ public: } event.reply(dpp::message().add_embed(embed).set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_chatcommand(Bot::ChatCommand({"admin_help", "admin_hilfe"}, "Bekomme Admin-Hilfe"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"admin_help"}, "Get help for admins"), [&](const dpp::slashcommand_t& event) { dpp::embed embed; - embed.set_title("Kommandos") - .set_footer(dpp::embed_footer().set_text("Bot entwickelt von 353535#3535")); + embed.set_title("Commands"); for (const auto& [command, guild_id] : bot->get_commands().guild_chat_commands) { if (guild_id != dpp::snowflake(bot->config.management_guild_id)) { continue; @@ -146,43 +144,43 @@ public: } event.reply(dpp::message().add_embed(embed).set_flags(dpp::message_flags::m_ephemeral)); }, bot->config.management_guild_id); - bot->add_chatcommand(Bot::ChatCommand({"about", "info"}, "Über mich"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"about", "info"}, "About me"), [&](const dpp::slashcommand_t& event) { auto [bots, bots_lock] = bot->get_locked_property>("main_all_instances"); std::ostringstream info; if (!bot->config._private) { - info << "**Server-Anzahl**: " << server_count << "\n"; + info << "**Guild count**: " << server_count << "\n"; } - info << "**Instanzen**: " << bots->size() << "\n" - "**Instanz-ID**: " << bot->config.id << "\n" + info << "**Instance count**: " << bots->size() << "\n" + "**Instance ID**: " << bot->config.id << "\n" # ifdef SYSINFO_WORKS - "**VM RAM-Benutzung**: " << SysInfo::memUsed() << " MB / " << SysInfo::memTotal() << " MB\n" + "**Virtual memory usage**: " << SysInfo::memUsed() << " MB / " << SysInfo::memTotal() << " MB\n" "**Kernel**: " << SysInfo::kernelNameAndVersion() << "\n" # endif "**Compiler**: " COMPILER_ID " " COMPILER_VERSION " (" COMPILER_PLATFORM ")\n" - "**C++ Standart**: " << __cplusplus << "\n" + "**C++ standard**: " << __cplusplus << "\n" ; event.reply(dpp::message() .add_embed(dpp::embed() - .set_title("Über mich") + .set_title("About me") .set_description(std::move(info).str()) ) .add_embed(InfoExternal::generate_user_info_embed(bot->cluster.me))); }); if (!bot->config._private) { - bot->add_chatcommand(Bot::ChatCommand({"invite", "einladen"}, "Lade mich auf deinen Server ein"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"invite"}, "Invite me to your server"), [&](const dpp::slashcommand_t& event) { event.reply(dpp::utility::bot_invite_url(bot->cluster.me.id, dpp::permissions::p_administrator)); }); } - bot->add_chatcommand(Bot::ChatCommand({"serverinfo"}, "Erhalte Infos über den Server"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"serverinfo"}, "Learn about a server"), [&](const dpp::slashcommand_t& event) { event.reply(dpp::message().add_embed(InfoExternal::generate_guild_info_embed(event.command.get_guild())).set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_usercommand(Bot::UserCommand({"Benutzerinfos"}, "Erhalte Infos über den Benutzer"), [&](const dpp::user_context_menu_t& event) { + bot->add_usercommand(Bot::UserCommand({"userinfo"}, "Learn about a user"), [&](const dpp::user_context_menu_t& event) { event.reply(dpp::message().add_embed(InfoExternal::generate_user_info_embed(event.get_user())).set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_messagecommand(Bot::MessageCommand({"Senderinfos"}, "Erhalte Infos über den Absender der Nachricht"), [&](const dpp::message_context_menu_t& event) { + bot->add_messagecommand(Bot::MessageCommand({"Sender info"}, "Learn about the sender of the message"), [&](const dpp::message_context_menu_t& event) { std::thread([=]() { const auto& target_message = event.get_message(); std::optional target; diff --git a/modules/lastseen.cpp b/modules/lastseen.cpp index 5ebfca4..8500b8b 100644 --- a/modules/lastseen.cpp +++ b/modules/lastseen.cpp @@ -43,9 +43,9 @@ class Lastseen { auto time = get_last_online_time(user_id); // Send reply if (time) { - return "Ich habe "+username+" zuletzt gesehen!"; + return "Last time I've seen "+username+" was !"; } else { - return "Ich kann dir nicht sagen, wann ich "+username+" zuletzt online gesehen habe."; + return "I can't tell you when I last saw "+username+" online."; } } @@ -60,11 +60,11 @@ public: " UNIQUE(id)" ");"; - bot->add_chatcommand(Bot::ChatCommand({"last-seen", "last-online", "zuletzt-online"}, "Sehe, wann eine Person zuletzt online war", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "user", "Zielbenutzer", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"last-seen", "last-online"}, "See when a person was last online", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "user", "Zielbenutzer", true))), [&](const dpp::slashcommand_t& event) { event.reply(dpp::message(last_seen_string(std::get(event.get_parameter("user")))).set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_usercommand(Bot::UserCommand({"Zuletzt gesehen..."}, "Sehe, wann eine Person zuletzt online war"), [&](const dpp::user_context_menu_t& event) { + bot->add_usercommand(Bot::UserCommand({"Last seen..."}, "See when a person was last online"), [&](const dpp::user_context_menu_t& event) { event.reply(dpp::message(last_seen_string(event.get_user().id, event.get_user().username)).set_flags(dpp::message_flags::m_ephemeral)); }); diff --git a/modules/levels.cpp b/modules/levels.cpp index 05ebf80..75af800 100644 --- a/modules/levels.cpp +++ b/modules/levels.cpp @@ -67,7 +67,7 @@ class Levels { xp_percentage = 99; } // Create message string - auto msg_str = "**<@"+std::to_string(target_id)+">**"+(target_is_sender?", du bist":" ist")+" Level **"+std::to_string(level)+"** und **"+std::to_string(xp_percentage)+"%** beim nächsten."; + auto msg_str = "**<@"+std::to_string(target_id)+">**"+(target_is_sender?", you are":" is")+" level **"+std::to_string(level)+"** and **"+std::to_string(xp_percentage)+"%** to the next."; // Create base message dpp::message base_msg; if (!target_is_sender) { @@ -76,7 +76,7 @@ class Levels { // Send current level if (embed_enabled) { dpp::embed embed; - embed.set_title("Dein Level") + embed.set_title("Your level") .set_description(msg_str) .set_color(color); return base_msg.add_embed(embed); @@ -111,13 +111,13 @@ public: " UNIQUE(guild_id, user_id)" ");"; - bot->add_chatcommand(Bot::ChatCommand({"current_level", "level"}, "Zeigt dein Level an (oder das von jemand anderem)", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "target", "Benutzer dessen Level du sehen möchtest", false))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"current_level", "level"}, "Shows your level (or someone else's)", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "target", "Benutzer dessen Level du sehen möchtest", false))), [&](const dpp::slashcommand_t& event) { // Get guild settings db_add_guild(event.command.guild_id); auto [enabled, embed_enabled, speed, color] = get_guild_data(event.command.guild_id); // Make sure levels are enabled here if (!enabled) { - event.reply(dpp::message("Dieser Server hat keine Levels aktiviert.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("This server doesn't have levels enabled.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get target @@ -136,13 +136,13 @@ public: // Get level info message and reply with it event.reply(get_level_info_message(event.command.guild_id, target_id, target_is_sender, embed_enabled)); }); - bot->add_chatcommand(Bot::ChatCommand({"levels_leaderboard", "levels_bestenliste"}, "Zeigt die Level-Bestenliste dieses Servers an"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"levels_leaderboard", "leaderboard"}, "Shows the level-leaderboard of this server"), [&](const dpp::slashcommand_t& event) { // Get guild settings db_add_guild(event.command.guild_id); auto [enabled, embed_enabled, speed, color] = get_guild_data(event.command.guild_id); // Make sure levels are enabled here if (!enabled) { - event.reply(dpp::message("Dieser Server hat keine Levels aktiviert.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("This server doesn't have levels enabled.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get all levels on this server sorted by highest-first @@ -160,16 +160,16 @@ public: }; // Create embed dpp::embed embed; - embed.set_title("Bestenliste") + embed.set_title("Leaderboard") .set_description(std::move(leaderboard_str).str()) .set_color(color); // Send result event.reply(dpp::message().add_embed(embed)); }); - bot->add_chatcommand(Bot::ChatCommand({"levels_settings", "levels_einstellungen"}, "Konfiguiere Levels", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_boolean, "enabled", "Ob Levels aktiviert sein sollen", false)).add_option(dpp::command_option(dpp::command_option_type::co_boolean, "embed", "Ob Levels Embeds verwenden sollen", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Embed-Farbe der Levels", false)).add_option(dpp::command_option(dpp::command_option_type::co_number, "speed", "Level-Speed: 0,5=halb, 1,0=normal, 2,0=doppelt, ...", false))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"levels_settings"}, "Configure levels", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_boolean, "enabled", "Whether levels should be enabled", false)).add_option(dpp::command_option(dpp::command_option_type::co_boolean, "embed", "Whether levels should be shown in embeds", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Embed color of levels", false)).add_option(dpp::command_option(dpp::command_option_type::co_number, "speed", "Level speed: 0.5=half, 1.0=normal, 2.0=double, ...", false))), [&](const dpp::slashcommand_t& event) { // Check that user has enough permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_administrator)) { - event.reply(dpp::message("Du brauchst Administrationsrechte, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need administration rights to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Go on @@ -217,14 +217,14 @@ public: } } // Report success - event.reply(dpp::message(std::string("Okay!")+ - (bad_color?"\n**Warnung:** Ich konnte die Farbe nicht identifizieren. Versuche es erneut mit einem HTML-Farbcode!":"")+ - (bad_speed?"\n**Warnung:** Die angegebene Geschwindigkeit ist nicht erlaubt!":"")).set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message(std::string("Done!")+ + (bad_color?"\n**Warning:** I could not identify the color. Try again with a color code!":"")+ + (bad_speed?"\n**Warning:** The specified speed is not allowed!":"")).set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_chatcommand(Bot::ChatCommand({"levels_add_role", "levels_rolle"}, "Füge eine Level-Rolle hinzu", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_integer, "threshold", "Mindestlevel", true)).add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Rolle", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"levels_add_role", "level_role"}, "Add a level role", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_integer, "threshold", "Minimum level to have this role", true)).add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Role to assign", true))), [&](const dpp::slashcommand_t& event) { // Check that user has enough permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_roles)) { - event.reply(dpp::message("Du brauchst Rollenverwaltungsrechte, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need role management rights to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get values @@ -232,7 +232,7 @@ public: auto role_id = std::get(event.get_parameter("role")); // Check threshold if (threshold <= 0) { - event.reply(dpp::message("Für dieses Level kannst du keine Rolle festlegen!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You cannot set a role for this level!").set_flags(dpp::message_flags::m_ephemeral)); return; } // Insert into database @@ -241,7 +241,7 @@ public: << std::to_string(role_id) << threshold; // Report success - event.reply(dpp::message("Leute mit mindestens dem Level "+std::to_string(threshold)+" werden ab jetzt die Rolle <@&"+std::to_string(role_id)+"> zugewiesen!").set_allowed_mentions(true, false, false, true, {}, {})); + event.reply(dpp::message("People with at least the level "+std::to_string(threshold)+" are now assigned the role <@&"+std::to_string(role_id)+">!").set_allowed_mentions(true, false, false, true, {}, {})); // Assign role where needed bot->db << "SELECT user_id FROM levels " "WHERE guild_id = ? AND level >= ?;" @@ -251,10 +251,10 @@ public: bot->cluster.guild_member_add_role(event.command.guild_id, user_id_str, role_id); }; }); - bot->add_chatcommand(Bot::ChatCommand({"levels_remove_role", "levels_rolle_entfernen"}, "Entferne eine Level-Rolle", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Rolle", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"levels_remove_role", "level_role_remove"}, "Remove a level role", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Role to no longer be assigned", true))), [&](const dpp::slashcommand_t& event) { // Check that user has enough permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_roles)) { - event.reply(dpp::message("Du brauchst Rollenverwaltungsrechte, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need role management rights to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get values @@ -264,16 +264,16 @@ public: << std::to_string(role_id) << std::to_string(event.command.guild_id); // Report success - event.reply(dpp::message("Die Rolle <@&"+std::to_string(role_id)+"> ist jetzt keine Level-Rolle mehr.").set_allowed_mentions(true, false, false, true, {}, {})); + event.reply(dpp::message("The role <@&"+std::to_string(role_id)+"> is now no longer a level role.").set_allowed_mentions(true, false, false, true, {}, {})); }); - bot->add_usercommand(Bot::UserCommand({"Level anzeigen"}, "Zeige das Level des Mitglieds an"), [&](const dpp::user_context_menu_t& event) { + bot->add_usercommand(Bot::UserCommand({"Level anzeigen"}, "Shows the level of a member"), [&](const dpp::user_context_menu_t& event) { // Get guild settings db_add_guild(event.command.guild_id); auto [enabled, embed_enabled, speed, color] = get_guild_data(event.command.guild_id); // Make sure levels are enabled here if (!enabled) { - event.reply(dpp::message("Dieser Server hat keine Levels aktiviert.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("This server doesn't have levels enabled.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get target @@ -307,13 +307,13 @@ public: xp -= 1.0; level++; // Notify user about levelup - std::string msg_str = "Du bist jetzt Level **"+std::to_string(level)+"**."; + std::string msg_str = "You are now level **"+std::to_string(level)+"**."; dpp::message msg; msg.set_channel_id(message.msg.channel_id) .set_allowed_mentions(true, false, false, true, {}, {}); if (embed_enabled) { dpp::embed embed; - embed.set_title("× Level Up!") + embed.set_title("× Level up!") .set_description(msg_str) .set_thumbnail(message.msg.author.get_avatar_url()) .set_color(color); diff --git a/modules/management.cpp b/modules/management.cpp index 08f5774..20d7d13 100644 --- a/modules/management.cpp +++ b/modules/management.cpp @@ -14,7 +14,7 @@ public: Management(Bot *_bot) : bot(_bot) { bot->cluster.intents |= dpp::intents::i_guilds; - bot->add_chatcommand(Bot::ChatCommand({"guild_list", "server_list" "server_liste"}, "Sehe die Liste der Server, auf denen der Bot ist"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"guild_list", "server_list"}, "Shows lists of servers the bot is on"), [&](const dpp::slashcommand_t& event) { event.thinking(); std::thread([this, event]() { // Get each server @@ -26,8 +26,8 @@ public: } // Compose embed dpp::embed embed; - embed.set_title("Server Liste") - .set_description("Ich bin auf den folgenden Servern:"); + embed.set_title("Server list") + .set_description("I'm on the following servers:"); for (const auto& guild : detailed_guilds) { embed.add_field(guild.name, std::to_string(guild.id)); } @@ -35,25 +35,25 @@ public: event.edit_original_response(dpp::message().add_embed(embed)); }).detach(); }, bot->config.management_guild_id); - bot->add_chatcommand(Bot::ChatCommand({"guild_leave", "server_leave", "server_verlassen"}, "Entferne den Bot aus einem Server", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_string, "guild_id", "ID des Servers", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"guild_leave", "server_leave"}, "Remove the bot from a server", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_string, "guild_id", "Server ID", true))), [&](const dpp::slashcommand_t& event) { // Get guild ID dpp::snowflake guild_id; try { guild_id = std::get(event.get_parameter("guild_id")); } catch (...) { - event.reply(dpp::message("Dies ist keine gültige ID.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("This is not a valid ID.").set_flags(dpp::message_flags::m_ephemeral)); } // Make sure guild ID isn't management server if (guild_id == dpp::snowflake(bot->config.management_guild_id)) { - event.reply(dpp::message("Ich werde den Management Server nicht verlassen.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("I will not leave the management server.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Try to leave server bot->cluster.current_user_leave_guild(guild_id, [event, guild_id](const dpp::confirmation_callback_t& ccb) { if (ccb.is_error()) { - event.reply("Ich habe erfolgreich den Server `"+std::to_string(guild_id)+"` verlassen!"); + event.reply("I couldn't leave the server `"+std::to_string(guild_id)+"` because: "+ccb.get_error().message); } else { - event.reply(dpp::message("Ich habe erfolgreich den angegebenen Server verlassen.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("I have successfully left the specified server.").set_flags(dpp::message_flags::m_ephemeral)); } }); }, bot->config.management_guild_id); diff --git a/modules/moderation.cpp b/modules/moderation.cpp index f0bc269..8887de2 100644 --- a/modules/moderation.cpp +++ b/modules/moderation.cpp @@ -52,11 +52,11 @@ public: " UNIQUE(guild_id, user_id)" ");"; - bot->add_chatcommand(Bot::ChatCommand({"purge"}, "Lösche die letzen Nachrichten", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_integer, "amount", "Menge", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"purge"}, "Delete recent messages", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_integer, "amount", "Amount of messages to delete", true))), [&](const dpp::slashcommand_t& event) { unsigned long amount = std::get(event.get_parameter("amount")); // Check that user has the correct permissions if (!has_moderation_role(event.command.member) && !event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_messages)) { - event.reply(dpp::message("Du brauchst die Nachrichtenverwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need message management permission to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Start message deletion in new thread @@ -98,67 +98,67 @@ public: } } // Report sucess or errors that might have occured - std::string message = "Auf Anfrage von "+member.get_mention()+" wurden **"+std::to_string(messages.size()-errors)+"** Nachrichten gelöscht."; + std::string message = "On request of "+member.get_mention()+" **"+std::to_string(messages.size()-errors)+"** messages were deleted."; if (errors) { - message += "\n**Warnung:** "+std::to_string(errors)+" Nachrichten konnten nicht gelöscht werden, da sie möglicherweise zu alt sind."; + message += "\n**Warning:** "+std::to_string(errors)+" messages could not be deleted as they may be too old."; } bot->cluster.message_create(dpp::message(message).set_channel_id(channel_id)); }).detach(); // Report success - event.reply(dpp::message("Okay! Vorgang wird gestartet...").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done! Starting to delete messages...").set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_chatcommand(Bot::ChatCommand({"ban"}, "Verbanne jemanden vom Server", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "member", "Mitglied", true)).add_option(dpp::command_option(dpp::command_option_type::co_string, "reason", "Grund", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"ban"}, "Ban someone from the server", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "member", "Member to ban", true)).add_option(dpp::command_option(dpp::command_option_type::co_string, "reason", "For what reason?", true))), [&](const dpp::slashcommand_t& event) { auto member_id = std::get(event.get_parameter("member")); auto reason = std::get(event.get_parameter("reason")); // Check that user has the correct permissions if (!has_moderation_role(event.command.member) && !event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_ban_members)) { - event.reply(dpp::message("Du brauchst die Banberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need the ban permission to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Send ban DM dpp::embed embed; - embed.set_title("Du wurdest gebannt") - .set_description("Du wurdest von "+event.command.get_guild().name+" gebannt!\n\n"+reason); + embed.set_title("You've been banned") + .set_description("You have been banned from "+event.command.get_guild().name+"!\n\n> "+reason); bot->cluster.direct_message_create(member_id, dpp::message().add_embed(embed), [this, member_id, event] (const dpp::confirmation_callback_t& ccb) { // Ban bot->cluster.guild_ban_add(event.command.guild_id, member_id, 0, [this, event, dm_ccb = ccb] (const dpp::confirmation_callback_t& ccb) { if (ccb.is_error()) { - event.reply(dpp::message("Ich kann diese Person nicht verbannen.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("I am not able to ban this person.").set_flags(dpp::message_flags::m_ephemeral)); // Delete DM message because it's not valid if (!dm_ccb.is_error()) { auto dm_msg = dm_ccb.get(); bot->cluster.message_delete(dm_msg.id, dm_msg.channel_id); } } else { - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); } }); }); }); - bot->add_chatcommand(Bot::ChatCommand({"kick"}, "Kicke jemanden vom Server", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "member", "Mitglied", true)).add_option(dpp::command_option(dpp::command_option_type::co_string, "reason", "Grund", true))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"kick"}, "Kick someone from the server", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_user, "member", "Member to kick", true)).add_option(dpp::command_option(dpp::command_option_type::co_string, "reason", "For what reason?", true))), [&](const dpp::slashcommand_t& event) { auto member_id = std::get(event.get_parameter("member")); auto reason = std::get(event.get_parameter("reason")); // Check that user has the correct permissions if (!has_moderation_role(event.command.member) && !event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_kick_members)) { - event.reply(dpp::message("Du brauchst die Kickberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need the kick permissoin to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Send kick DM dpp::embed embed; - embed.set_title("Du wurdest gekickt") - .set_description("Du wurdest von "+event.command.get_guild().name+" gekickt!\n\n"+reason); + embed.set_title("You've been kicked") + .set_description("You have been kicked from "+event.command.get_guild().name+"!\n\n> "+reason); bot->cluster.direct_message_create(member_id, dpp::message().add_embed(embed), [this, member_id, event] (const dpp::confirmation_callback_t& ccb) { // Kick bot->cluster.guild_member_kick(event.command.guild_id, member_id, [this, event, dm_ccb = ccb] (const dpp::confirmation_callback_t& ccb) { if (ccb.is_error()) { - event.reply(dpp::message("Ich kann diese Person nicht kicken.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("I am not able to kick this person.").set_flags(dpp::message_flags::m_ephemeral)); // Delete DM message because it's not valid if (!dm_ccb.is_error()) { auto dm_msg = dm_ccb.get(); bot->cluster.message_delete(dm_msg.id, dm_msg.channel_id); } } else { - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); } }); }); diff --git a/modules/ticket.cpp b/modules/ticket.cpp index 9237e2f..befe800 100644 --- a/modules/ticket.cpp +++ b/modules/ticket.cpp @@ -27,7 +27,7 @@ class Ticket { } std::string build_channel_topic(dpp::snowflake author_id, dpp::snowflake moderator_id = 0) { - return "Ticket erstellt von <@"+std::to_string(author_id)+">"+(moderator_id?", übernommen von <@"+std::to_string(moderator_id)+">":""); + return "Ticket created by <@"+std::to_string(author_id)+">"+(moderator_id?", taken care of by <@"+std::to_string(moderator_id)+">":""); } void close_ticket(const dpp::interaction_create_t& event) { @@ -39,7 +39,7 @@ class Ticket { << std::to_string(event.command.channel_id) >> std::tie(author_id, author_full_name); } catch (...) { - event.reply(dpp::message("Fehler. Ist dies wirklich ein Ticketkanal?").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Error. Is this actually a ticket channel?").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get guild from database @@ -53,7 +53,7 @@ class Ticket { // Check permissions if (!any_close) { if (!can_change_ticket(event, management_role_id)) { - event.reply(dpp::message("Du darfst dieses Ticket nicht schließen!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You can not close this ticket!").set_flags(dpp::message_flags::m_ephemeral)); return; } } @@ -96,13 +96,13 @@ class Ticket { << std::to_string(channel_id) >> std::tie(title, description); } catch (...) { - title = "Fehlerhaftes Ticket"; - description = "Informationen konnten nicht aus der Datenbank geladen werden."; + title = "Invalid ticket"; + description = "Information could not be retrieved from the database."; } // Begin log std::ostringstream log; log << title << '\n' << description << "\n\n" - "Ticket erstellt von " << author_full_name << " (" << author_id << ")\n"; + "Ticket created by " << author_full_name << " (" << author_id << ")\n"; // Collect all messages constexpr unsigned fetch_block = 100; size_t last_message_count = fetch_block; @@ -144,13 +144,13 @@ class Ticket { auto res = messages.find(message.message_reference.message_id); if (res != messages.end()) { auto reference_msg_no = std::distance(messages.begin(), res)-1; - content = "(Antwort auf "+dpp::leading_zeroes(reference_msg_no, 3)+") "+content; + content = "(Reply to "+dpp::leading_zeroes(reference_msg_no, 3)+") "+content; } } // Append message to log log << dpp::leading_zeroes(no++, 3) << " <" << message.author.format_username() << "> " << content << '\n'; } - log << "Ticket wurde von " << closing_user.format_username() + " (" << closing_user.id << ") geschlossen."; + log << "Ticket has been closed by " << closing_user.format_username() + " (" << closing_user.id << ")."; return log.str(); } @@ -184,10 +184,10 @@ public: " UNIQUE(guild_id, channel_id, author_id)" ");"; - bot->add_chatcommand(Bot::ChatCommand({"ticket_settings", "ticket_einstellungen"}, "Lege die Ticket-Einstellungen fest", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "category", "Kategorie in der Tickets erstellt werden", false)).add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Rolle, die Tickets verwalten kann", false)).add_option(dpp::command_option(dpp::command_option_type::co_channel, "logs_channel", "Kanal in dem Ticket-Logs gespeichert werden", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "channel_name_prefix", "Text, mit dem der Ticketkanalname anfangen soll", false)).add_option(dpp::command_option(dpp::command_option_type::co_boolean, "any_close", "Ob jeder Tickets schließen darf", false)).add_option(dpp::command_option(dpp::command_option_type::co_boolean, "ping", "Ob Rolle über neue Tickets benachrichtigt werden", false))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"ticket_settings", "ticket_setup"}, "Set up tickets", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "category", "Category in which tickets are created", false)).add_option(dpp::command_option(dpp::command_option_type::co_role, "role", "Role that can manage tickets", false)).add_option(dpp::command_option(dpp::command_option_type::co_channel, "logs_channel", "Channel in which ticket logs are saved", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "channel_name_prefix", "Text to prefix channel names with", false)).add_option(dpp::command_option(dpp::command_option_type::co_boolean, "any_close", "Whether everyone is allowed to close tickets", false)).add_option(dpp::command_option(dpp::command_option_type::co_boolean, "ping", "Whether role should be notified about new tickets", false))), [&](const dpp::slashcommand_t& event) { // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_channels | dpp::permissions::p_manage_roles)) { - event.reply(dpp::message("Du brauchst die Kanal- und Rollenverwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need channel and role management permissions to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get parameters @@ -266,12 +266,12 @@ public: << std::to_string(event.command.guild_id); } // Send reply - event.reply(dpp::message("Okay.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); }); - bot->add_chatcommand(Bot::ChatCommand({"tickets", "ticket_embed"}, "Erstelle ein Ticket-Embed", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "channel", "Kanal, in dem Tickets erstellt werden können sollen", true)).add_option(dpp::command_option(dpp::command_option_type::co_string, "title", "Überschrift", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "text", "Beschreibung", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "footer_text", "Fußzeile", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Farbe", false))), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"tickets", "ticket_embed"}, "Create a ticket creation embed", dpp::slashcommand().add_option(dpp::command_option(dpp::command_option_type::co_channel, "channel", "Channel in which tickets should be creatable", true)).add_option(dpp::command_option(dpp::command_option_type::co_string, "title", "Title", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "text", "Description", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "footer_text", "Footer", false)).add_option(dpp::command_option(dpp::command_option_type::co_string, "color", "Color", false))), [&](const dpp::slashcommand_t& event) { // Check that user has the correct permissions if (!event.command.get_guild().base_permissions(event.command.member).has(dpp::permissions::p_manage_channels | dpp::permissions::p_manage_roles)) { - event.reply(dpp::message("Du brauchst die Kanal- und Rollenverwaltungsberechtigung, um dieses Kommando zu verwenden.").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You need channel and role management permissions to use this command.").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get parameters @@ -280,8 +280,8 @@ public: text_opt = event.get_parameter("text"), color_opt = event.get_parameter("color"); auto footer_text_opt = event.get_parameter("footer_text"); - auto title = title_opt.index()!=0?std::get(title_opt):"Support Tickets", - text = text_opt.index()!=0?std::get(text_opt):"Klicke den Knopf an, um ein Ticket zu erstellen!", + auto title = title_opt.index()!=0?std::get(title_opt):"Support tickets", + text = text_opt.index()!=0?std::get(text_opt):"Click to create ticket!", footer_text = footer_text_opt.index()!=0?std::get(footer_text_opt):""; unsigned color = Util::str_to_color(color_opt.index()!=0?std::get(color_opt):"#ff2217"); // Send embed there @@ -294,7 +294,7 @@ public: .set_color(color!=-1U?color:0xffffff)) .add_component(dpp::component() .add_component(dpp::component() - .set_label("Ticket erstellen") + .set_label("Create ticket") .set_type(dpp::component_type::cot_button) .set_emoji("🎟️") .set_style(dpp::component_style::cos_primary) @@ -308,34 +308,34 @@ public: << std::to_string(event.command.guild_id) >> std::tie(management_role_id); // Report success and potentially warn about missing ticket management role configuration - event.reply("Ticketerstellung ist nun in <#"+std::to_string(channel_id)+"> möglich!\n" - "**Tipp:** Um Tickets zu deaktivieren, reicht es, das erstellte Embed zu löschen."+ - (color==-1U?"\n**Fehler:** Die angegebene Farbe wurde nicht erkannt. Versuche es mit einem HTML-Farbcode!":"")+ - (management_role_id=="0"?"\n**Warnung:** Du solltest mit `/ticket_einstellungen` eine Ticketverwaltungs-Rolle festlegen!":"")); + event.reply("Ticket creation is now possible in <#"+std::to_string(channel_id)+">!\n" + "**Tip:** To disable tickets, it is sufficient to delete the created embed."+ + (color==-1U?"\n**Error:** The specified color was not recognized. Try a color code!":"")+ + (management_role_id=="0"?"\n**Warning:** You should define a ticket management role using `/ticket_settings`!":"")); }); - bot->add_chatcommand(Bot::ChatCommand({"ticket_close", "ticket_schliessen"}, "Schließe ein Ticket"), [&](const dpp::slashcommand_t& event) { + bot->add_chatcommand(Bot::ChatCommand({"ticket_close"}, "Close a ticket"), [&](const dpp::slashcommand_t& event) { close_ticket(event); }); bot->cluster.on_button_click([this](const dpp::button_click_t& event) { if (event.custom_id == "ticket_create") { // Create and send ticket creation embed - dpp::interaction_modal_response modal("ticket_modal", "Supportticket Erstellung"); + dpp::interaction_modal_response modal("ticket_modal", "Support ticket creation"); modal.add_component(dpp::component() - .set_label("Zusammenfassung") + .set_label("Summary") .set_id("title") .set_type(dpp::cot_text) - .set_placeholder("Zusammenfassung deines Anliegens") + .set_placeholder("Summary of your request") .set_min_length(5) .set_max_length(50) .set_text_style(dpp::text_short) .set_required(true)) .add_row() .add_component(dpp::component() - .set_label("Beschreibung") + .set_label("Description") .set_id("description") .set_type(dpp::cot_text) - .set_placeholder("Ausführliche Beschreibung deines Anliegens") + .set_placeholder("Detailed description of your request") .set_min_length(20) .set_max_length(2000) .set_text_style(dpp::text_paragraph) @@ -351,7 +351,7 @@ public: >> std::tie(management_role_id); // Check permissions if (!can_change_ticket(event, management_role_id)) { - event.reply(dpp::message("Du darfst dieses Ticket nicht übernehmen!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("You may not take care of this ticket!").set_flags(dpp::message_flags::m_ephemeral)); return; } // Get ticket info from database @@ -362,12 +362,12 @@ public: << std::to_string(event.command.channel_id) >> std::tie(author_id); } catch (...) { - event.reply(dpp::message("Fehler. Ist dies wirklich ein Ticketkanal?").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Error. Is this really a ticket channel?").set_flags(dpp::message_flags::m_ephemeral)); return; } // Send message - event.reply(dpp::message("Okay!").set_flags(dpp::message_flags::m_ephemeral)); - bot->cluster.message_create(dpp::message().set_channel_id(event.command.channel_id).set_content("Dieses Ticket wird jetzt von "+event.command.usr.get_mention()+" übernommen!")); + event.reply(dpp::message("Done!").set_flags(dpp::message_flags::m_ephemeral)); + bot->cluster.message_create(dpp::message().set_channel_id(event.command.channel_id).set_content("This ticket is not taken care of by "+event.command.usr.get_mention()+"!")); // Update channel topic bot->cluster.channel_get(event.command.channel_id, [this, author_id, event](const dpp::confirmation_callback_t& ccb) { if (!ccb.is_error()) { @@ -416,7 +416,7 @@ public: bot->cluster.channel_create(new_channel, [this, event, title, description, management_role, ping](const dpp::confirmation_callback_t& ccb) { // Check for error if (ccb.is_error()) { - event.reply("**FEHLER:** Ticket-Einstellungen sind ungültig.\n**An Server-Administration:** Bitte prüfe ob die Ziel-Kategorie noch existiert und setzte gegebenenfalls eine neue!```json"+ccb.get_error().message+"```"); + event.reply("**ERROR:** Ticket settings are invalid.\n**To server administration:** Please check if the target category still exists and set a new one if necessary!```json"+ccb.get_error().message+"```"); return; } // Get created channel @@ -440,13 +440,13 @@ public: .set_footer("Ticket #"+Util::int_as_hex(channel.id), "")) .add_component(dpp::component() .add_component(dpp::component() - .set_label("Ticket übernehmen") + .set_label("Take care of ticket") .set_type(dpp::component_type::cot_button) .set_emoji("✅") .set_style(dpp::component_style::cos_primary) .set_id("ticket_take")) .add_component(dpp::component() - .set_label("Ticket schließen") + .set_label("Close ticket") .set_type(dpp::component_type::cot_button) .set_emoji("🗑️") .set_style(dpp::component_style::cos_danger) @@ -458,10 +458,10 @@ public: } }); // Report success - event.reply(dpp::message("Dein Ticket wurde erstellt in "+channel.get_mention()+"!").set_flags(dpp::message_flags::m_ephemeral)); + event.reply(dpp::message("Your ticket has been created in "+channel.get_mention()+"!").set_flags(dpp::message_flags::m_ephemeral)); }); } catch (std::exception& e) { - event.reply(std::string("Ein interner Fehler ist aufgetreten: `")+e.what()+'`'); + event.reply(std::string("An internal error has occured: `")+e.what()+'`'); } } }); diff --git a/modules/join2create.cpp b/modules/wip/join2create.cpp similarity index 100% rename from modules/join2create.cpp rename to modules/wip/join2create.cpp diff --git a/util.cpp b/util.cpp index 7a46078..e18c403 100644 --- a/util.cpp +++ b/util.cpp @@ -110,25 +110,25 @@ unsigned str_to_color(std::string color_str) { try { return std::stoul(color_str, nullptr, 16); } catch (...) { - if (color_str == "green" || color_str == "grün") { + if (color_str == "green") { return 0x00ff00; - } else if (color_str == "red" || color_str == "rot") { + } else if (color_str == "red") { return 0xff0000; - } else if (color_str == "blue" || color_str == "blau") { + } else if (color_str == "blue") { return 0x0000ff; - } else if (color_str == "gelb" || color_str == "yellow") { + } else if (color_str == "yellow") { return 0xffff00; - } else if (color_str == "red" || color_str == "rot") { + } else if (color_str == "red") { return 0xff0000; - } else if (color_str == "magenta" || color_str == "magentarot") { + } else if (color_str == "magenta") { return 0xff00ff; } else if (color_str == "aqua") { return 0x00ffff; - } else if (color_str == "white" || color_str == "weiß") { + } else if (color_str == "white") { return 0xffffff; - } else if (color_str == "black" || color_str == "schwarz") { + } else if (color_str == "black") { return 0x000000; - } else if (color_str == "grey" || color_str == "gray" || color_str == "grau") { + } else if (color_str == "grey" || color_str == "gray") { return 0x888888; } }